[ Index ]
 

Code source de osCommerce 2.2ms2-060817

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/catalog/admin/includes/classes/ -> email.php (source)

   1  <?php
   2  /*
   3    $Id: email.php,v 1.8 2003/06/11 22:24:34 dgw_ Exp $
   4  
   5    osCommerce, Open Source E-Commerce Solutions
   6    http://www.oscommerce.com
   7  
   8    Copyright (c) 2003 osCommerce
   9  
  10    Released under the GNU General Public License
  11  
  12    mail.php - a class to assist in building mime-HTML eMails
  13  
  14    The original class was made by Richard Heyes <richard@phpguru.org>
  15    and can be found here: http://www.phpguru.org
  16  
  17    Renamed and Modified by Jan Wildeboer for osCommerce
  18  */
  19  
  20    class email {
  21      var $html;
  22      var $text;
  23      var $output;
  24      var $html_text;
  25      var $html_images;
  26      var $image_types;
  27      var $build_params;
  28      var $attachments;
  29      var $headers;
  30  
  31      function email($headers = '') {
  32        if ($headers == '') $headers = array();
  33  
  34        $this->html_images = array();
  35        $this->headers = array();
  36  
  37        if (EMAIL_LINEFEED == 'CRLF') {
  38          $this->lf = "\r\n";
  39        } else {
  40          $this->lf = "\n";
  41        }
  42  
  43  /**
  44   * If you want the auto load functionality
  45   * to find other mime-image/file types, add the
  46   * extension and content type here.
  47   */
  48  
  49        $this->image_types = array('gif' => 'image/gif',
  50                                   'jpg' => 'image/jpeg',
  51                                   'jpeg' => 'image/jpeg',
  52                                   'jpe' => 'image/jpeg',
  53                                   'bmp' => 'image/bmp',
  54                                   'png' => 'image/png',
  55                                   'tif' => 'image/tiff',
  56                                   'tiff' => 'image/tiff',
  57                                   'swf' => 'application/x-shockwave-flash');
  58  
  59        $this->build_params['html_encoding'] = 'quoted-printable';
  60        $this->build_params['text_encoding'] = '7bit';
  61        $this->build_params['html_charset'] = constant('CHARSET');
  62        $this->build_params['text_charset'] = constant('CHARSET');
  63        $this->build_params['text_wrap'] = 998;
  64  
  65  /**
  66   * Make sure the MIME version header is first.
  67   */
  68  
  69        $this->headers[] = 'MIME-Version: 1.0';
  70  
  71        reset($headers);
  72        while (list(,$value) = each($headers)) {
  73          if (tep_not_null($value)) {
  74            $this->headers[] = $value;
  75          }
  76        }
  77      }
  78  
  79  /**
  80   * This function will read a file in
  81   * from a supplied filename and return
  82   * it. This can then be given as the first
  83   * argument of the the functions
  84   * add_html_image() or add_attachment().
  85   */
  86  
  87      function get_file($filename) {
  88        $return = '';
  89  
  90        if ($fp = fopen($filename, 'rb')) {
  91          while (!feof($fp)) {
  92            $return .= fread($fp, 1024);
  93          }
  94          fclose($fp);
  95  
  96          return $return;
  97        } else {
  98          return false;
  99        }
 100      }
 101  
 102  /**
 103   * Function for extracting images from
 104   * html source. This function will look
 105   * through the html code supplied by add_html()
 106   * and find any file that ends in one of the
 107   * extensions defined in $obj->image_types.
 108   * If the file exists it will read it in and
 109   * embed it, (not an attachment).
 110   *
 111   * Function contributed by Dan Allen
 112   */
 113  
 114      function find_html_images($images_dir) {
 115  // Build the list of image extensions
 116        while (list($key, ) = each($this->image_types)) {
 117          $extensions[] = $key;
 118        }
 119  
 120        preg_match_all('/"([^"]+\.(' . implode('|', $extensions).'))"/Ui', $this->html, $images);
 121  
 122        for ($i=0; $i<count($images[1]); $i++) {
 123          if (file_exists($images_dir . $images[1][$i])) {
 124            $html_images[] = $images[1][$i];
 125            $this->html = str_replace($images[1][$i], basename($images[1][$i]), $this->html);
 126          }
 127        }
 128  
 129        if (tep_not_null($html_images)) {
 130  // If duplicate images are embedded, they may show up as attachments, so remove them.
 131          $html_images = array_unique($html_images);
 132          sort($html_images);
 133  
 134          for ($i=0; $i<count($html_images); $i++) {
 135            if ($image = $this->get_file($images_dir . $html_images[$i])) {
 136              $content_type = $this->image_types[substr($html_images[$i], strrpos($html_images[$i], '.') + 1)];
 137              $this->add_html_image($image, basename($html_images[$i]), $content_type);
 138            }
 139          }
 140        }
 141      }
 142  
 143  /**
 144   * Adds plain text. Use this function
 145   * when NOT sending html email
 146   */
 147  
 148      function add_text($text = '') {
 149        $this->text = tep_convert_linefeeds(array("\r\n", "\n", "\r"), $this->lf, $text);
 150      }
 151  
 152  /**
 153   * Adds a html part to the mail.
 154   * Also replaces image names with
 155   * content-id's.
 156   */
 157  
 158      function add_html($html, $text = NULL, $images_dir = NULL) {
 159        $this->html = tep_convert_linefeeds(array("\r\n", "\n", "\r"), '<br>', $html);
 160        $this->html_text = tep_convert_linefeeds(array("\r\n", "\n", "\r"), $this->lf, $text);
 161  
 162        if (isset($images_dir)) $this->find_html_images($images_dir);
 163      }
 164  
 165  /**
 166   * Adds an image to the list of embedded
 167   * images.
 168   */
 169  
 170      function add_html_image($file, $name = '', $c_type='application/octet-stream') {
 171        $this->html_images[] = array('body' => $file,
 172                                     'name' => $name,
 173                                     'c_type' => $c_type,
 174                                     'cid' => md5(uniqid(time())));
 175      }
 176  
 177  /**
 178   * Adds a file to the list of attachments.
 179   */
 180  
 181      function add_attachment($file, $name = '', $c_type='application/octet-stream', $encoding = 'base64') {
 182        $this->attachments[] = array('body' => $file,
 183                                     'name' => $name,
 184                                     'c_type' => $c_type,
 185                                     'encoding' => $encoding);
 186      }
 187  
 188  /**
 189   * Adds a text subpart to a mime_part object
 190   */
 191  
 192  /* HPDL PHP3 */
 193  //    function &add_text_part(&$obj, $text) {
 194      function add_text_part(&$obj, $text) {
 195        $params['content_type'] = 'text/plain';
 196        $params['encoding'] = $this->build_params['text_encoding'];
 197        $params['charset'] = $this->build_params['text_charset'];
 198  
 199        if (is_object($obj)) {
 200          return $obj->addSubpart($text, $params);
 201        } else {
 202          return new mime($text, $params);
 203        }
 204      }
 205  
 206  /**
 207   * Adds a html subpart to a mime_part object
 208   */
 209  
 210  /* HPDL PHP3 */
 211  //    function &add_html_part(&$obj) {
 212      function add_html_part(&$obj) {
 213        $params['content_type'] = 'text/html';
 214        $params['encoding'] = $this->build_params['html_encoding'];
 215        $params['charset'] = $this->build_params['html_charset'];
 216  
 217        if (is_object($obj)) {
 218          return $obj->addSubpart($this->html, $params);
 219        } else {
 220          return new mime($this->html, $params);
 221        }
 222      }
 223  
 224  /**
 225   * Starts a message with a mixed part
 226   */
 227  
 228  /* HPDL PHP3 */
 229  //    function &add_mixed_part() {
 230      function add_mixed_part() {
 231        $params['content_type'] = 'multipart/mixed';
 232  
 233        return new mime('', $params);
 234      }
 235  
 236  /**
 237   * Adds an alternative part to a mime_part object
 238   */
 239  
 240  /* HPDL PHP3 */
 241  //    function &add_alternative_part(&$obj) {
 242      function add_alternative_part(&$obj) {
 243        $params['content_type'] = 'multipart/alternative';
 244  
 245        if (is_object($obj)) {
 246          return $obj->addSubpart('', $params);
 247        } else {
 248          return new mime('', $params);
 249        }
 250      }
 251  
 252  /**
 253   * Adds a html subpart to a mime_part object
 254   */
 255  
 256  /* HPDL PHP3 */
 257  //    function &add_related_part(&$obj) {
 258      function add_related_part(&$obj) {
 259        $params['content_type'] = 'multipart/related';
 260  
 261        if (is_object($obj)) {
 262          return $obj->addSubpart('', $params);
 263        } else {
 264          return new mime('', $params);
 265        }
 266      }
 267  
 268  /**
 269   * Adds an html image subpart to a mime_part object
 270   */
 271  
 272  /* HPDL PHP3 */
 273  //    function &add_html_image_part(&$obj, $value) {
 274      function add_html_image_part(&$obj, $value) {
 275        $params['content_type'] = $value['c_type'];
 276        $params['encoding'] = 'base64';
 277        $params['disposition'] = 'inline';
 278        $params['dfilename'] = $value['name'];
 279        $params['cid'] = $value['cid'];
 280  
 281        $obj->addSubpart($value['body'], $params);
 282      }
 283  
 284  /**
 285   * Adds an attachment subpart to a mime_part object
 286   */
 287  
 288  /* HPDL PHP3 */
 289  //    function &add_attachment_part(&$obj, $value) {
 290      function add_attachment_part(&$obj, $value) {
 291        $params['content_type'] = $value['c_type'];
 292        $params['encoding'] = $value['encoding'];
 293        $params['disposition'] = 'attachment';
 294        $params['dfilename'] = $value['name'];
 295  
 296        $obj->addSubpart($value['body'], $params);
 297      }
 298  
 299  /**
 300   * Builds the multipart message from the
 301   * list ($this->_parts). $params is an
 302   * array of parameters that shape the building
 303   * of the message. Currently supported are:
 304   *
 305   * $params['html_encoding'] - The type of encoding to use on html. Valid options are
 306   *                            "7bit", "quoted-printable" or "base64" (all without quotes).
 307   *                            7bit is EXPRESSLY NOT RECOMMENDED. Default is quoted-printable
 308   * $params['text_encoding'] - The type of encoding to use on plain text Valid options are
 309   *                            "7bit", "quoted-printable" or "base64" (all without quotes).
 310   *                            Default is 7bit
 311   * $params['text_wrap']     - The character count at which to wrap 7bit encoded data.
 312   *                            Default this is 998.
 313   * $params['html_charset']  - The character set to use for a html section.
 314   *                            Default is iso-8859-1
 315   * $params['text_charset']  - The character set to use for a text section.
 316   *                          - Default is iso-8859-1
 317   */
 318  
 319  /* HPDL PHP3 */
 320  //    function build_message($params = array()) {
 321      function build_message($params = '') {
 322        if ($params == '') $params = array();
 323  
 324        if (count($params) > 0) {
 325          reset($params);
 326          while(list($key, $value) = each($params)) {
 327            $this->build_params[$key] = $value;
 328          }
 329        }
 330  
 331        if (tep_not_null($this->html_images)) {
 332          reset($this->html_images);
 333          while (list(,$value) = each($this->html_images)) {
 334            $this->html = str_replace($value['name'], 'cid:' . $value['cid'], $this->html);
 335          }
 336        }
 337  
 338        $null = NULL;
 339        $attachments = ((tep_not_null($this->attachments)) ? true : false);
 340        $html_images = ((tep_not_null($this->html_images)) ? true : false);
 341        $html = ((tep_not_null($this->html)) ? true : false);
 342        $text = ((tep_not_null($this->text)) ? true : false);
 343  
 344        switch (true) {
 345          case (($text == true) && ($attachments == false)):
 346  /* HPDL PHP3 */
 347  //          $message =& $this->add_text_part($null, $this->text);
 348            $message = $this->add_text_part($null, $this->text);
 349            break;
 350          case (($text == false) && ($attachments == true) && ($html == false)):
 351  /* HPDL PHP3 */
 352  //          $message =& $this->add_mixed_part();
 353            $message = $this->add_mixed_part();
 354  
 355            for ($i=0; $i<count($this->attachments); $i++) {
 356              $this->add_attachment_part($message, $this->attachments[$i]);
 357            }
 358            break;
 359          case (($text == true) && ($attachments == true)):
 360  /* HPDL PHP3 */
 361  //          $message =& $this->add_mixed_part();
 362            $message = $this->add_mixed_part();
 363            $this->add_text_part($message, $this->text);
 364  
 365            for ($i=0; $i<count($this->attachments); $i++) {
 366              $this->add_attachment_part($message, $this->attachments[$i]);
 367            }
 368            break;
 369          case (($html == true) && ($attachments == false) && ($html_images == false)):
 370            if (tep_not_null($this->html_text)) {
 371  /* HPDL PHP3 */
 372  //            $message =& $this->add_alternative_part($null);
 373              $message = $this->add_alternative_part($null);
 374              $this->add_text_part($message, $this->html_text);
 375              $this->add_html_part($message);
 376            } else {
 377  /* HPDL PHP3 */
 378  //            $message =& $this->add_html_part($null);
 379              $message = $this->add_html_part($null);
 380            }
 381            break;
 382          case (($html == true) && ($attachments == false) && ($html_images == true)):
 383            if (tep_not_null($this->html_text)) {
 384  /* HPDL PHP3 */
 385  //            $message =& $this->add_alternative_part($null);
 386              $message = $this->add_alternative_part($null);
 387              $this->add_text_part($message, $this->html_text);
 388  /* HPDL PHP3 */
 389  //            $related =& $this->add_related_part($message);
 390              $related = $this->add_related_part($message);
 391            } else {
 392  /* HPDL PHP3 */
 393  //            $message =& $this->add_related_part($null);
 394  //            $related =& $message;
 395              $message = $this->add_related_part($null);
 396              $related = $message;
 397            }
 398            $this->add_html_part($related);
 399  
 400            for ($i=0; $i<count($this->html_images); $i++) {
 401              $this->add_html_image_part($related, $this->html_images[$i]);
 402            }
 403            break;
 404          case (($html == true) && ($attachments == true) && ($html_images == false)):
 405  /* HPDL PHP3 */
 406  //          $message =& $this->add_mixed_part();
 407            $message = $this->add_mixed_part();
 408            if (tep_not_null($this->html_text)) {
 409  /* HPDL PHP3 */
 410  //            $alt =& $this->add_alternative_part($message);
 411              $alt = $this->add_alternative_part($message);
 412              $this->add_text_part($alt, $this->html_text);
 413              $this->add_html_part($alt);
 414            } else {
 415              $this->add_html_part($message);
 416            }
 417  
 418            for ($i=0; $i<count($this->attachments); $i++) {
 419              $this->add_attachment_part($message, $this->attachments[$i]);
 420            }
 421            break;
 422          case (($html == true) && ($attachments == true) && ($html_images == true)):
 423  /* HPDL PHP3 */
 424  //          $message =& $this->add_mixed_part();
 425            $message = $this->add_mixed_part();
 426  
 427            if (tep_not_null($this->html_text)) {
 428  /* HPDL PHP3 */
 429  //            $alt =& $this->add_alternative_part($message);
 430              $alt = $this->add_alternative_part($message);
 431              $this->add_text_part($alt, $this->html_text);
 432  /* HPDL PHP3 */
 433  //            $rel =& $this->add_related_part($alt);
 434              $rel = $this->add_related_part($alt);
 435            } else {
 436  /* HPDL PHP3 */
 437  //            $rel =& $this->add_related_part($message);
 438              $rel = $this->add_related_part($message);
 439            }
 440            $this->add_html_part($rel);
 441  
 442            for ($i=0; $i<count($this->html_images); $i++) {
 443              $this->add_html_image_part($rel, $this->html_images[$i]);
 444            }
 445  
 446            for ($i=0; $i<count($this->attachments); $i++) {
 447              $this->add_attachment_part($message, $this->attachments[$i]);
 448            }
 449            break;
 450        }
 451  
 452        if ( (isset($message)) && (is_object($message)) ) {
 453          $output = $message->encode();
 454          $this->output = $output['body'];
 455  
 456          reset($output['headers']);
 457          while (list($key, $value) = each($output['headers'])) {
 458            $headers[] = $key . ': ' . $value;
 459          }
 460  
 461          $this->headers = array_merge($this->headers, $headers);
 462  
 463          return true;
 464        } else {
 465          return false;
 466        }
 467      }
 468  
 469  /**
 470   * Sends the mail.
 471   */
 472  
 473      function send($to_name, $to_addr, $from_name, $from_addr, $subject = '', $headers = '') {
 474        if ((strstr($to_name, "\n") != false) || (strstr($to_name, "\r") != false)) {
 475          return false;
 476        }
 477  
 478        if ((strstr($to_addr, "\n") != false) || (strstr($to_addr, "\r") != false)) {
 479          return false;
 480        }
 481  
 482        if ((strstr($subject, "\n") != false) || (strstr($subject, "\r") != false)) {
 483          return false;
 484        }
 485  
 486        if ((strstr($from_name, "\n") != false) || (strstr($from_name, "\r") != false)) {
 487          return false;
 488        }
 489  
 490        if ((strstr($from_addr, "\n") != false) || (strstr($from_addr, "\r") != false)) {
 491          return false;
 492        }
 493  
 494        $to = (($to_name != '') ? '"' . $to_name . '" <' . $to_addr . '>' : $to_addr);
 495        $from = (($from_name != '') ? '"' . $from_name . '" <' . $from_addr . '>' : $from_addr);
 496  
 497        if (is_string($headers)) {
 498          $headers = explode($this->lf, trim($headers));
 499        }
 500  
 501        for ($i=0; $i<count($headers); $i++) {
 502          if (is_array($headers[$i])) {
 503            for ($j=0; $j<count($headers[$i]); $j++) {
 504              if ($headers[$i][$j] != '') {
 505                $xtra_headers[] = $headers[$i][$j];
 506              }
 507            }
 508          }
 509  
 510          if ($headers[$i] != '') {
 511            $xtra_headers[] = $headers[$i];
 512          }
 513        }
 514  
 515        if (!isset($xtra_headers)) {
 516          $xtra_headers = array();
 517        }
 518  
 519        if (EMAIL_TRANSPORT == 'smtp') {
 520          return mail($to_addr, $subject, $this->output, 'From: ' . $from . $this->lf . 'To: ' . $to . $this->lf . implode($this->lf, $this->headers) . $this->lf . implode($this->lf, $xtra_headers));
 521        } else {
 522          return mail($to, $subject, $this->output, 'From: '.$from.$this->lf.implode($this->lf, $this->headers).$this->lf.implode($this->lf, $xtra_headers));
 523        }
 524      }
 525  
 526  /**
 527   * Use this method to return the email
 528   * in message/rfc822 format. Useful for
 529   * adding an email to another email as
 530   * an attachment. there's a commented
 531   * out example in example.php.
 532   *
 533   * string get_rfc822(string To name,
 534   *       string To email,
 535   *       string From name,
 536   *       string From email,
 537   *       [string Subject,
 538   *        string Extra headers])
 539   */
 540  
 541      function get_rfc822($to_name, $to_addr, $from_name, $from_addr, $subject = '', $headers = '') {
 542  // Make up the date header as according to RFC822
 543        $date = 'Date: ' . date('D, d M y H:i:s');
 544        $to = (($to_name != '') ? 'To: "' . $to_name . '" <' . $to_addr . '>' : 'To: ' . $to_addr);
 545        $from = (($from_name != '') ? 'From: "' . $from_name . '" <' . $from_addr . '>' : 'From: ' . $from_addr);
 546  
 547        if (is_string($subject)) {
 548          $subject = 'Subject: ' . $subject;
 549        }
 550  
 551        if (is_string($headers)) {
 552          $headers = explode($this->lf, trim($headers));
 553        }
 554  
 555        for ($i=0; $i<count($headers); $i++) {
 556          if (is_array($headers[$i])) {
 557            for ($j=0; $j<count($headers[$i]); $j++) {
 558              if ($headers[$i][$j] != '') {
 559                $xtra_headers[] = $headers[$i][$j];
 560              }
 561            }
 562          }
 563  
 564          if ($headers[$i] != '') {
 565            $xtra_headers[] = $headers[$i];
 566          }
 567        }
 568  
 569        if (!isset($xtra_headers)) {
 570          $xtra_headers = array();
 571        }
 572  
 573        $headers = array_merge($this->headers, $xtra_headers);
 574  
 575        return $date . $this->lf . $from . $this->lf . $to . $this->lf . $subject . $this->lf . implode($this->lf, $headers) . $this->lf . $this->lf . $this->output;
 576      }
 577    }
 578  ?>


Généré le : Mon Nov 26 19:48:25 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics