[ Index ]
 

Code source de eZ Publish 3.9.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/ezutils/classes/ -> ezmail.php (source)

   1  <?php
   2  //
   3  // $Id: ezmail.php,v 1.44.2.7 2002/06/10 16:41:45 fh Exp $
   4  //
   5  // Definition of eZMail class
   6  //
   7  // Created on: <15-Mar-2001 20:40:06 fh>
   8  //
   9  // SOFTWARE NAME: eZ publish
  10  // SOFTWARE RELEASE: 3.9.0
  11  // BUILD VERSION: 17785
  12  // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS
  13  // SOFTWARE LICENSE: GNU General Public License v2.0
  14  // NOTICE: >
  15  //   This program is free software; you can redistribute it and/or
  16  //   modify it under the terms of version 2.0  of the GNU General
  17  //   Public License as published by the Free Software Foundation.
  18  //
  19  //   This program is distributed in the hope that it will be useful,
  20  //   but WITHOUT ANY WARRANTY; without even the implied warranty of
  21  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22  //   GNU General Public License for more details.
  23  //
  24  //   You should have received a copy of version 2.0 of the GNU General
  25  //   Public License along with this program; if not, write to the Free
  26  //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  27  //   MA 02110-1301, USA.
  28  //
  29  //
  30  
  31  /*! \defgroup eZUtils Utility classes */
  32  
  33  /*!
  34    \class eZMail ezmail.php
  35    \ingroup eZUtils
  36    \brief Mail handler
  37  
  38    Class for storing the details about en email and providing
  39    text serialization.
  40  
  41   \note It's important to note that most methods that return values do an automatic conversion if not specified.
  42  
  43  */
  44  
  45  include_once ( 'lib/ezi18n/classes/eztextcodec.php' );
  46  include_once ( 'lib/ezutils/classes/ezini.php' );
  47  
  48  define( 'EZ_MAIL_REGEXP', '([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z_])*@(((([0-9a-zA-Z])+([-\w]*[0-9a-zA-Z])*\.)+[a-zA-Z]{2,9})|(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)))' );
  49  
  50  class eZMail
  51  {
  52      /*!
  53        Constructs a new eZMail object.
  54      */
  55      function eZMail()
  56      {
  57          $this->ReceiverElements = array();
  58          $this->From = false;
  59          $this->CcElements = array();
  60          $this->BccElements = array();
  61          $this->ReplyTo = false;
  62          $this->Subject = false;
  63          $this->BodyText = false;
  64          $this->ExtraHeaders = array();
  65          $this->TextCodec = false;
  66          $this->MessageID = false;
  67  
  68          // Sets some default values
  69          include_once ( 'lib/version.php' );
  70          $version = eZPublishSDK::version();
  71  
  72          $this->MIMEVersion = '1.0';
  73          $this->ContentType = array( 'type' => 'text/plain',
  74                                      'charset' => eZTextCodec::internalCharset(),
  75                                      'transfer-encoding' => '8bit',
  76                                      'disposition' => 'inline',
  77                                      'boundary' => false );
  78          $this->UserAgent = "eZ publish, Version $version";
  79  
  80          $ini =& eZINI::instance();
  81  
  82          if ( $ini->hasVariable( 'MailSettings', 'ContentType' ) )
  83              $this->setContentType( $ini->variable( 'MailSettings', 'ContentType' ) );
  84  
  85          if (! defined( 'EZ_MAIL_LINE_SEPARATOR' ) )
  86          {
  87              $ini =& eZINI::instance( 'site.ini' );
  88              $ending = $ini->variable( 'MailSettings', 'HeaderLineEnding' );
  89              if ( $ending == 'auto' )
  90              {
  91                  $sys =& eZSys::instance();
  92                  // For windows we use \r\n which is the endline defined in RFC 2045
  93                  if ( $sys->osType() == 'win32' )
  94                  {
  95                      $separator = "\r\n";
  96                  }
  97                  else // for linux/unix/mac we use \n only due to some mail transfer agents destroying
  98                       // emails containing \r\n
  99                  {
 100                      $separator = "\n";
 101                  }
 102              }
 103              else
 104              {
 105                  $separator = urldecode( $ending );
 106              }
 107              define( 'EZ_MAIL_LINE_SEPARATOR', $separator );
 108          }
 109      }
 110  
 111      /*!
 112        Returns the receiver addresses as text with only the email address.
 113      */
 114      function receiverEmailText( $convert = true )
 115      {
 116          return $this->composeEmailItems( $this->ReceiverElements, true, 'email', $convert );
 117      }
 118  
 119      /*!
 120        Returns the receiver addresses as text.
 121      */
 122      function receiverText( $convert = true )
 123      {
 124          return $this->composeEmailItems( $this->ReceiverElements, true, false, $convert );
 125      }
 126  
 127      /*!
 128        Returns the receiver cc addresses as an array with texts.
 129      */
 130      function ccReceiverTextList( $convert = true )
 131      {
 132          return $this->composeEmailItems( $this->CcElements, false, 'email', $convert );
 133      }
 134  
 135      function bccReceiverTextList( $convert = true )
 136      {
 137          return $this->composeEmailItems( $this->BccElements, false, 'email', $convert );
 138      }
 139  
 140      /*!
 141        Returns the receiver addresses as an array with texts.
 142      */
 143      function receiverTextList( $convert = true )
 144      {
 145          return $this->composeEmailItems( $this->ReceiverElements, false, 'email', $convert );
 146      }
 147  
 148      /*!
 149        Returns the receiver addresses.
 150      */
 151      function receiverElements()
 152      {
 153          return $this->ReceiverElements;
 154      }
 155  
 156      /*!
 157        Returns the addresses which should get a carbon copy.
 158       */
 159      function ccElements()
 160      {
 161          return $this->CcElements;
 162      }
 163  
 164      /*!
 165        Returns the addresses which should get a blind carbon copy.
 166       */
 167      function bccElements()
 168      {
 169          return $this->BccElements;
 170      }
 171  
 172  
 173      /*!
 174        Returns the receiver address.
 175      */
 176      function replyTo( $convert = true )
 177      {
 178          if ( !$convert )
 179              return $this->ReplyTo;
 180          return $this->convertHeaderText( $this->ReplyTo );
 181      }
 182  
 183      /*!
 184        Returns the sender address.
 185      */
 186      function sender( $convert = true )
 187      {
 188          if ( !$convert )
 189              return $this->From;
 190  
 191          if ( is_array( $this->From ) )
 192          {
 193              $convertedSender = $this->From;
 194              if ( $this->From['name'] )
 195              {
 196                  $convertedSender['name'] = $this->convertHeaderText( $this->From['name'] );
 197              }
 198              return $convertedSender;
 199          }
 200          else if ( is_string( $this->From ) )
 201          {
 202              return $this->convertHeaderText( $this->From );
 203          }
 204  
 205          return $this->From;
 206      }
 207  
 208      /*!
 209        Returns the sender address as text.
 210      */
 211      function senderText( $convert = true )
 212      {
 213          $text = eZMail::composeEmailName( $this->From );
 214          if ( !$convert )
 215              return $text;
 216          return $this->convertHeaderText( $text );
 217      }
 218  
 219      /*!
 220       \return the MIME version for this email, normally this is 1.0.
 221       \note The value is returned as a string.
 222      */
 223      function mimeVersion()
 224      {
 225          return $this->MIMEVersion;
 226      }
 227  
 228      /*!
 229       \return the content type for this email, this is normally text/plain.
 230      */
 231      function contentType()
 232      {
 233          return $this->ContentType['type'];
 234      }
 235  
 236      /*!
 237       \return the charset for this email, this is normally taken from the internal charset.
 238       \sa usedCharset
 239      */
 240      function contentCharset()
 241      {
 242          return $this->ContentType['charset'];
 243      }
 244  
 245      /*!
 246       \return the content transfer encoding, normally this is 8bit.
 247      */
 248      function contentTransferEncoding()
 249      {
 250          return $this->ContentType['transfer-encoding'];
 251      }
 252  
 253      /*!
 254       \return the content disposition, normally this is inline.
 255      */
 256      function contentDisposition()
 257      {
 258          return $this->ContentType['disposition'];
 259      }
 260  
 261      /*!
 262       \return the user agent for this email, the user agent is automatically created if not specfied.
 263      */
 264     function userAgent( $convert = true )
 265      {
 266          if ( !$convert )
 267              return $this->UserAgent;
 268          return $this->convertHeaderText( $this->UserAgent );
 269      }
 270  
 271      /*!
 272       Sets the MIME version to \a $version.
 273      */
 274      function setMIMEVersion( $version )
 275      {
 276          $this->MIMEVersion = $version;
 277      }
 278  
 279      /*!
 280       Sets the various content variables, any parameter which is set to something other than \c false
 281       will overwrite the old value.
 282      */
 283      function setContentType( $type = false, $charset = false,
 284                               $transferEncoding = false, $disposition = false, $boundary = false )
 285      {
 286          if ( $type )
 287              $this->ContentType['type'] = $type;
 288          if ( $charset )
 289              $this->ContentType['charset'] = $charset;
 290          if ( $transferEncoding )
 291              $this->ContentType['transfer-encoding'] = $transferEncoding;
 292          if ( $disposition )
 293              $this->ContentType['disposition'] = $disposition;
 294          if ( $boundary )
 295              $this->ContentType['boundary'] = $boundary;
 296      }
 297  
 298      /*!
 299       Sets the user agent for the email to \a $agent.
 300      */
 301      function setUserAgent( $agent )
 302      {
 303          $this->UserAgent = $agent;
 304      }
 305  
 306      /*!
 307        Sets the receiver addresses.
 308      */
 309      function setReceiverElements( $toElements )
 310      {
 311          $this->ReceiverElements = $toElements;
 312      }
 313  
 314      /*!
 315        Sets the receiver address.
 316        \note This will remove all other receivers
 317        \sa addReceiver, setReceiverElements
 318      */
 319      function setReceiver( $email, $name = false )
 320      {
 321          $this->ReceiverElements = array( array( 'name' => $name,
 322                                                  'email' => $email ) );
 323      }
 324  
 325      /*!
 326        Sets the receiver address, the email and name will be extracted from \a $text.
 327        \note This will remove all other receivers
 328        \sa addReceiver, setReceiverElements
 329      */
 330      function setReceiverText( $text )
 331      {
 332          $this->extractEmail( $text, $email, $name );
 333          $this->ReceiverElements = array( array( 'name' => $name,
 334                                                  'email' => $email ) );
 335      }
 336  
 337      /*!
 338        Adds a new receiver address.
 339      */
 340      function addReceiver( $email, $name = false )
 341      {
 342          $this->ReceiverElements[] = array( 'name' => $name,
 343                                             'email' => $email );
 344      }
 345  
 346  
 347      /*!
 348        Sets the receiver address.
 349      */
 350      function setReplyTo( $email, $name = false )
 351      {
 352          $this->ReplyTo = array( 'name' => $name,
 353                                  'email' => $email );
 354      }
 355  
 356      /*!
 357        Sets the sender address.
 358      */
 359      function setSender( $email, $name = false )
 360      {
 361          $this->From = array( 'name' => $name,
 362                               'email' => $email );
 363      }
 364  
 365      /*!
 366        Sets the sender address, the email and name will be extracted from \a $text.
 367      */
 368      function setSenderText( $text )
 369      {
 370          $this->extractEmail( $text, $email, $name );
 371          $this->From = array( 'name' => $name,
 372                               'email' => $email );
 373      }
 374  
 375      /*!
 376        Sets the cc addresses.
 377       */
 378      function setCcElements( $newCc )
 379      {
 380          $this->CcElements = $newCc;
 381      }
 382  
 383      /*!
 384        Adds a new Cc address.
 385      */
 386      function addCc( $email, $name = false )
 387      {
 388          $this->CcElements[] = array( 'name' => $name,
 389                                       'email' => $email );
 390      }
 391  
 392      /*!
 393        Sets the bcc addresses.
 394       */
 395      function setBccElements( $newBcc )
 396      {
 397          $this->BccElements = $newBcc;
 398      }
 399  
 400      /*!
 401        Adds a new Bcc address.
 402      */
 403      function addBcc( $email, $name = false )
 404      {
 405          $this->BccElements[] = array( 'name' => $name,
 406                                        'email' => $email );
 407      }
 408  
 409      /*!
 410       Return the extra headers
 411      */
 412      function extraHeaders()
 413      {
 414          return $this->ExtraHeaders;
 415      }
 416  
 417      /*!
 418       Adds the headers \a $headerName with header value \a $headerValue to the extra headers.
 419      */
 420      function addExtraHeader( $headerName, $headerValue )
 421      {
 422          return $this->ExtraHeaders[] = array( 'name' => $headerName,
 423                                                'content' => $headerValue );
 424      }
 425  
 426      /*!
 427       Similar to addExtraHeader() but will overwrite existing entries.
 428      */
 429      function setExtraHeader( $headerName, $headerValue )
 430      {
 431          for ( $i = 0; $i < count( $this->ExtraHeaders ); ++$i )
 432          {
 433              $extraHeader =& $this->ExtraHeaders[$i];
 434              if ( isset( $extraHeader['name'] ) and
 435                   $extraHeader['name'] == $headerName )
 436              {
 437                  $extraHeader = array( 'name' => $headerName,
 438                                        'content' => $headerValue );
 439                  return true;
 440              }
 441          }
 442          $this->addExtraHeader( $headerName, $headerValue );
 443      }
 444  
 445      /*!
 446       Sets the extra headers to \a $headers.
 447      */
 448      function setExtraHeaders( $headers )
 449      {
 450          return $this->ExtraHeaders = $headers;
 451      }
 452  
 453      /*!
 454        Returns the message ID format : <number@serverID>
 455        Read in the RFC's if you want to know more about it..
 456       */
 457      function messageID()
 458      {
 459          return $this->MessageID;
 460      }
 461  
 462      /*!
 463        Sets the message ID. This is a server setting only so BE CAREFUL WITH THIS.
 464      */
 465      function setMessageID( $newMessageID )
 466      {
 467          $this->MessageID = $newMessageID;
 468      }
 469  
 470      /*!
 471        Returns the messageID that this message is a reply to.
 472      */
 473      function references()
 474      {
 475          return $this->References;
 476      }
 477  
 478      /*!
 479        Sets the messageID that this message is a reply to.
 480      */
 481      function setReferences( $newReference )
 482      {
 483          $this->References = $newReference;
 484      }
 485  
 486      /*!
 487        Returns the subject.
 488      */
 489      function subject( $convert = true )
 490      {
 491          if ( !$convert )
 492              return $this->Subject;
 493          return $this->convertHeaderText( $this->Subject );
 494      }
 495  
 496      /*!
 497        Sets the subject of the mail.
 498      */
 499      function setSubject( $newSubject )
 500      {
 501          $this->Subject = trim( $newSubject );
 502      }
 503  
 504      /*!
 505        returns the body.
 506      */
 507      function body( $convert = true )
 508      {
 509          if ( !$convert )
 510              return $this->BodyText;
 511          return $this->convertText( $this->BodyText );
 512      }
 513  
 514      /*!
 515        Sets the body.
 516      */
 517      function setBody( $newBody )
 518      {
 519          $newBody = preg_replace( "/\r\n|\r|\n/", EZ_MAIL_LINE_SEPARATOR, $newBody );
 520          $this->BodyText = $newBody;
 521      }
 522  
 523      /*!
 524        \static
 525        Splits a list of email addresses into an array where each entry is an email address.
 526      */
 527      function &splitList( $emails )
 528      {
 529          $emails = preg_split( "/[,;]/", $emails );
 530          return $emails;
 531      }
 532  
 533      /*!
 534        \static
 535        Static function for validating e-mail addresses.
 536  
 537        Returns true if successful, false if not.
 538      */
 539      function validate( $address )
 540      {
 541          $pos = ( ereg( '^' . EZ_MAIL_REGEXP . '$', $address) );
 542          return $pos;
 543      }
 544  
 545      function extractEmail( $text, &$email, &$name )
 546      {
 547          if ( preg_match( "/([^<]+)<" . EZ_MAIL_REGEXP . ">/", $text, $matches ) )
 548          {
 549              $email = $matches[2];
 550              $name = $matches[1];
 551          }
 552          else
 553          {
 554              $email = $text;
 555              $name = false;
 556          }
 557      }
 558  
 559      /*!
 560        \static
 561        Static function for extracting an e-mail from text
 562  
 563        Returns the first valid e-mail in address, returns false if no e-mail addresses found
 564      */
 565      function stripEmail( $address )
 566      {
 567          $res = ereg( EZ_MAIL_REGEXP, $address, $email );
 568          if ( $res )
 569              return $email[0];
 570          else
 571              return 0;
 572      }
 573  
 574      /*!
 575       \static
 576       \returns a text which does not contain newlines, newlines are converted to spaces.
 577      */
 578      function blankNewlines( $text )
 579      {
 580          return preg_replace( "/\r\n|\r|\n/", ' ', $text );
 581      }
 582  
 583      /*!
 584       \static
 585       \returns the header content as a simple string, will deflate arrays.
 586       \sa blankNewLines
 587      */
 588      function contentString( $content )
 589      {
 590          if ( is_array( $content ) )
 591              return implode( '; ', $content );
 592          else
 593              return (string)$content;
 594      }
 595  
 596      /*!
 597       \static
 598       Composes a text out of the email and name and returns it.
 599  
 600       Example: John Doe <john@doe.com> or just john@doe.com
 601      */
 602      function composeEmailName( $item, $key = false, $convert = true )
 603      {
 604          if ( $key !== false and
 605               isset( $item[$key] ) )
 606              return $item[$key];
 607          if ( $item['name'] )
 608          {
 609              if ( $convert )
 610                  $item['name'] = $this->convertHeaderText( $item['name'] );
 611              $text = $item['name'] . ' <' . $item['email'] . '>';
 612          }
 613          else
 614              $text = $item['email'];
 615          return $text;
 616      }
 617  
 618      /*!
 619       \static
 620       Composes an email text out of all items in \a $items and returns it.
 621       All items are comma separated.
 622      */
 623      function composeEmailItems( $items, $join = true, $key = false, $convert = true )
 624      {
 625          $textElements = array();
 626          foreach ( $items as $item )
 627          {
 628              $textElements[] = eZMail::composeEmailName( $item, $key, $convert );
 629          }
 630  
 631          if ( $convert )
 632          {
 633              foreach ( array_keys( $textElements ) as $elementKey )
 634              {
 635                  $element =& $textElements[$elementKey];
 636                  $element = $this->convertHeaderText( $element );
 637              }
 638          }
 639  
 640          if ( $join )
 641              $text = implode( ', ', $textElements );
 642          else
 643              $text = $textElements;
 644  
 645          return $text;
 646      }
 647  
 648      /*!
 649       \return an array with headers, each header item is an associative array with the keys \c name and \c content.
 650               \c content will either be a string or an array with strings.
 651  
 652       The parameter \a $parameters contains optional parameters, they can be:
 653       - exclude-headers - \c Array of header names which will not be included in the result array.
 654       \sa contentString, blankNewLines
 655      */
 656      function headers( $parameters = array() )
 657      {
 658          $parameters = array_merge( array( 'exclude-headers' => false ),
 659                                     $parameters );
 660          $excludeHeaders = array();
 661          if ( $parameters['exclude-headers'] )
 662          {
 663              foreach ( $parameters['exclude-headers'] as $excludeHeader )
 664              {
 665                  $excludeHeaders[] = strtolower( $excludeHeader );
 666              }
 667          }
 668          $headers = array();
 669          $headerNames = array();
 670          if ( !in_array( 'to', $excludeHeaders ) )
 671          {
 672              $headers[] = array( 'name' => 'To',
 673                                  'content' => $this->composeEmailItems( $this->ReceiverElements ) );
 674              $headerNames[] = 'to';
 675          }
 676          if ( !in_array( 'date', $excludeHeaders ) )
 677          {
 678              $headers[] = array( 'name' => 'Date',
 679                                  'content' => date( 'r' ) );
 680              $headerNames[] = 'date';
 681          }
 682          if ( $this->Subject !== false and
 683               !in_array( 'subject', $excludeHeaders ) )
 684          {
 685              $headers[] = array( 'name' => 'Subject',
 686                                  'content' => $this->subject() );
 687              $headerNames[] = 'subject';
 688          }
 689          if ( $this->From !== false and
 690               !in_array( 'from', $excludeHeaders ) )
 691          {
 692              $headers[] = array( 'name' => 'From',
 693                                  'content' => $this->composeEmailName( $this->From ) );
 694              $headerNames[] = 'from';
 695          }
 696          if ( count( $this->CcElements ) > 0 and
 697               !in_array( 'cc', $excludeHeaders ) )
 698          {
 699              $headers[] = array( 'name' => 'Cc',
 700                                  'content' => $this->composeEmailItems( $this->CcElements ) );
 701              $headerNames[] = 'cc';
 702          }
 703          if ( count( $this->BccElements ) > 0 and
 704               !in_array( 'bcc', $excludeHeaders ) )
 705          {
 706              $headers[] = array( 'name' => 'Bcc',
 707                                  'content' => $this->composeEmailItems( $this->BccElements ) );
 708              $headerNames[] = 'bcc';
 709          }
 710          if ( $this->ReplyTo !== false and
 711               !in_array( 'reply-to', $excludeHeaders ) )
 712          {
 713              $headers[] = array( 'name' => 'Reply-To',
 714                                  'content' => $this->composeEmailName( $this->ReplyTo ) );
 715              $headerNames[] = 'reply-to';
 716          }
 717          if ( !in_array( 'mime-version', $excludeHeaders ) )
 718          {
 719              $headers[] = array( 'name' => 'MIME-Version',
 720                                  'content' => $this->MIMEVersion );
 721              $headerNames[] = 'mime-version';
 722          }
 723          if ( !in_array( 'content-type', $excludeHeaders ) )
 724          {
 725              $charset = $this->usedCharset();
 726              if ( $this->ContentType['boundary'] )
 727              {
 728              $headers[] = array( 'name' => 'Content-Type',
 729                                  'content' => array( $this->ContentType['type'], 'charset='. $charset, 'boundary="'. $this->ContentType['boundary'] . '"' ) );
 730              }
 731              else
 732              {
 733                  $headers[] = array( 'name' => 'Content-Type',
 734                                      'content' => array( $this->ContentType['type'], 'charset='. $charset ) );
 735              }
 736              $headerNames[] = 'content-type';
 737          }
 738          if ( !in_array( 'content-transfer-encoding', $excludeHeaders ) )
 739          {
 740              $headers[] = array( 'name' => 'Content-Transfer-Encoding',
 741                                  'content' => $this->ContentType['transfer-encoding'] );
 742              $headerNames[] = 'content-transfer-encoding';
 743          }
 744          if ( !in_array( 'content-disposition', $excludeHeaders ) )
 745          {
 746              $headers[] = array( 'name' => 'Content-Disposition',
 747                                  'content' => $this->ContentType['disposition'] );
 748              $headerNames[] = 'content-disposition';
 749          }
 750          if ( !in_array( 'user-agent', $excludeHeaders ) )
 751          {
 752              $headers[] = array( 'name' => 'User-Agent',
 753                                  'content' => $this->UserAgent );
 754              $headerNames[] = 'user-agent';
 755          }
 756          if ( !in_array( 'message-id', $excludeHeaders ) )
 757          {
 758              if ( $this->MessageID )
 759              {
 760                  $headers[] = array( 'name' => 'Message-Id',
 761                                      'content' => $this->MessageID );
 762                  $headerNames[] = 'message-id';
 763              }
 764          }
 765  
 766          $extraHeaders = $this->ExtraHeaders;
 767          foreach ( $extraHeaders as $extraHeader )
 768          {
 769              if ( !in_array( strtolower( $extraHeader['name'] ), $excludeHeaders ) and
 770                   !in_array( strtolower( $extraHeader['name'] ), $headerNames ) )
 771                  $headers[] = $extraHeader;
 772          }
 773          return $headers;
 774      }
 775  
 776      /*!
 777       Extracts all headers and generates a text string out of it.
 778       The parameter \a $parameters will be passed to the headers() function.
 779      */
 780      function headerTextList( $parameters = array() )
 781      {
 782          $convert = true;
 783          if ( isset( $parameters['convert'] ) )
 784              $convert = $parameters['convert'];
 785          $textElements = array();
 786          $headers = $this->headers( $parameters );
 787          foreach ( $headers as $header )
 788          {
 789              $headerText = $this->blankNewlines( $header['name'] ) . ': ';
 790              $contentText = $this->blankNewlines( $this->contentString( $header['content'] ) );
 791              $headerText .= $contentText;
 792              $textElements[] = $headerText;
 793          }
 794          return $textElements;
 795      }
 796  
 797      /*!
 798       Composes a text field out of all the headers and returns it.
 799       The parameter \a $parameters will be passed to the headers() function.
 800      */
 801      function headerText( $parameters = array() )
 802      {
 803          $convert = true;
 804          if ( isset( $parameters['convert'] ) )
 805              $convert = $parameters['convert'];
 806          $text = '';
 807          $headers = $this->headers( $parameters );
 808          $headerCount = 0;
 809          foreach ( $headers as $header )
 810          {
 811              if ( $headerCount++ > 0 )
 812                  $text .= EZ_MAIL_LINE_SEPARATOR;
 813              $text .= $this->blankNewlines( $header['name'] ) . ': ';
 814              $contentText = $this->blankNewlines( $this->contentString( $header['content'] ) );
 815              $text .= $contentText;
 816          }
 817          return $text;
 818      }
 819  
 820      /*!
 821       Calls convertText with \a $isHeader set to \c true.
 822      */
 823      function convertHeaderText( $text )
 824      {
 825          $charset = $this->contentCharset();
 826          if ( $charset != 'us-ascii' )
 827          {
 828              $newText = $this->encodeMimeHeader( $text );
 829              return $newText;
 830          }
 831          return $text;
 832      }
 833  
 834      /*!
 835        Encodes $str using mb_encode_mimeheader() if it is aviable, or does base64 encodin of a header if not.
 836       */
 837      function encodeMimeHeader( $str )
 838      {
 839          if ( !$this->TextCodec )
 840          {
 841               $this->TextCodec =& eZTextCodec::instance( $this->contentCharset(), $this->outputCharset() );
 842          }
 843  
 844          if ( function_exists( "mb_encode_mimeheader" ) )
 845          {
 846              $encoded = mb_encode_mimeheader( $str, $this->TextCodec->InputCharsetCode, "B", EZ_MAIL_LINE_SEPARATOR );
 847          }
 848          else
 849          {
 850              if (  0 == preg_match_all( '/[\000-\010\013\014\016-\037\177-\377]/', $str, $matches ) )
 851                  return $str;
 852  
 853              $maxlen = 75 - 7 - strlen( $this->TextCodec->InputCharsetCode );
 854  
 855              $encoding = 'B';
 856              $encoded = base64_encode( $str );
 857              $maxlen -= $maxlen % 4;
 858              $encoded = trim( chunk_split( $encoded, $maxlen, "\n" ) );
 859  
 860              $encoded = preg_replace( '/^(.*)$/m', " =?".$this->TextCodec->InputCharsetCode."?$encoding?\\1?=", $encoded );
 861  
 862              $encoded = trim( str_replace( "\n", EZ_MAIL_LINE_SEPARATOR, $encoded ) );
 863          }
 864  
 865          return $encoded;
 866      }
 867  
 868  
 869      /*!
 870       Converts the text \a $text to a suitable output format.
 871       \note Header conversion is not supported yet, for now it will only return original text when \a $isHeader is set to \c true.
 872      */
 873      function convertText( $text, $isHeader = false )
 874      {
 875  
 876          $charset = $this->contentCharset();
 877          if ( $this->isAllowedCharset( $charset ) )
 878              return $text;
 879          $outputCharset = $this->outputCharset();
 880          if ( !$this->TextCodec )
 881          {
 882              $this->TextCodec =& eZTextCodec::instance( $charset, $outputCharset );
 883          }
 884          $newText = $this->TextCodec->convertString( $text );
 885          return $newText;
 886      }
 887  
 888      /*!
 889       \return \c true if the charset \a $charset is allowed as output charset.
 890       \sa allowedCharsets.
 891      */
 892      function isAllowedCharset( $charset )
 893      {
 894          include_once ( 'lib/ezi18n/classes/ezcharsetinfo.php' );
 895          $realCharset = eZCharsetInfo::realCharsetCode( $charset );
 896          $charsets = $this->allowedCharsets();
 897          foreach ( $charsets as $charsetName )
 898          {
 899              $realName = eZCharsetInfo::realCharsetCode( $charsetName );
 900              if ( $realName == $realCharset )
 901                  return true;
 902          }
 903          return false;
 904      }
 905  
 906      /*!
 907       \return an array with charsets that can be used directly as output charsets.
 908      */
 909      function allowedCharsets()
 910      {
 911          $ini =& eZINI::instance();
 912          $charsets = $ini->variable( 'MailSettings', 'AllowedCharsets' );
 913          return $charsets;
 914      }
 915  
 916      /*!
 917       \return the charset which will be used for output.
 918      */
 919      function usedCharset()
 920      {
 921          $charset = $this->contentCharset();
 922          if ( $this->isAllowedCharset( $charset ) )
 923              return $charset;
 924          return $this->outputCharset();
 925      }
 926  
 927      /*!
 928       \return the default output charset.
 929      */
 930      function outputCharset()
 931      {
 932          $ini =& eZINI::instance();
 933          $outputCharset = $ini->variable( 'MailSettings', 'OutputCharset' );
 934          return $outputCharset;
 935      }
 936  
 937  /*
 938  //        $subj = "=?$iso?B?" . trim( chunk_split( base64_encode( $subj ))) . "?=";
 939  */
 940  
 941      /// \privatesection
 942      var $ReceiverElements;
 943      var $From;
 944      var $CcElements;
 945      var $BccElements;
 946      var $ContentType;
 947      var $UserAgent;
 948      var $ReplyTo;
 949      var $Subject;
 950      var $BodyText;
 951      var $ExtraHeaders;
 952      var $TextCodec;
 953      var $MessageID;
 954  }
 955  
 956  ?>


Généré le : Sat Feb 24 10:30:04 2007 par Balluche grâce à PHPXref 0.7