b2evolution PHP Cross Reference Blogging Systems

Source: /inc/tools/model/_email.funcs.php - 541 lines - 15226 bytes - Summary - Text - Print

Description: This file implements functions to work with email tools. This file is part of the b2evolution/evocms project - {@link http://b2evolution.net/}. See also {@link http://sourceforge.net/projects/evocms/}.

   1  <?php
   2  /**

   3   * This file implements functions to work with email tools.

   4   *

   5   * This file is part of the b2evolution/evocms project - {@link http://b2evolution.net/}.

   6   * See also {@link http://sourceforge.net/projects/evocms/}.

   7   *

   8   * @copyright (c)2003-2014 by Francois Planque - {@link http://fplanque.com/}.

   9   *

  10   * @license http://b2evolution.net/about/license.html GNU General Public License (GPL)

  11   *

  12   * @package evocore

  13   *

  14   * {@internal Below is a list of authors who have contributed to design/coding of this file: }}

  15   * @author fplanque: Francois PLANQUE.

  16   *

  17   * @version $Id: _email.funcs.php 1500 2012-07-10 11:38:31Z yura $

  18   */
  19  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  20  
  21  
  22  /**

  23   * Get array of status titles for email address

  24   *

  25   * @return array Status titles

  26   */
  27  function emblk_get_status_titles()
  28  {
  29      return array(
  30              'unknown'     => TS_('Unknown'),
  31              'warning'     => TS_('Warning'),
  32              'suspicious1' => TS_('Suspicious 1'),
  33              'suspicious2' => TS_('Suspicious 2'),
  34              'suspicious3' => TS_('Suspicious 3'),
  35              'prmerror'    => TS_('Permanent error'),
  36              'spammer'     => TS_('Spammer'),
  37          );
  38  }
  39  
  40  
  41  /**

  42   * Get array of status colors for email address

  43   *

  44   * @return array Status colors

  45   */
  46  function emblk_get_status_colors()
  47  {
  48      return array(
  49              ''            => '808080',
  50              'unknown'     => '808080',
  51              'warning'     => 'FFFF00',
  52              'suspicious1' => 'FFC800',
  53              'suspicious2' => 'FFA500',
  54              'suspicious3' => 'FF8C00',
  55              'prmerror'    => 'FF0000',
  56              'spammer'     => '990000',
  57          );
  58  }
  59  
  60  
  61  /**

  62   * Get array of status icons for email address

  63   *

  64   * @return array Status icons

  65   */
  66  function emblk_get_status_icons()
  67  {
  68      return array(
  69              'unknown'     => get_icon( 'bullet_white', 'imgtag', array( 'title' => emblk_get_status_title( 'unknown' ) ) ),
  70              'warning'     => get_icon( 'bullet_yellow', 'imgtag', array( 'title' => emblk_get_status_title( 'warning' ) ) ),
  71              'suspicious1' => get_icon( 'bullet_orange', 'imgtag', array( 'title' => emblk_get_status_title( 'suspicious1' ) ) ),
  72              'suspicious2' => get_icon( 'bullet_orange', 'imgtag', array( 'title' => emblk_get_status_title( 'suspicious2' ) ) ),
  73              'suspicious3' => get_icon( 'bullet_orange', 'imgtag', array( 'title' => emblk_get_status_title( 'suspicious3' ) ) ),
  74              'prmerror'    => get_icon( 'bullet_red', 'imgtag', array( 'title' => emblk_get_status_title( 'prmerror' ) ) ),
  75              'spammer'     => get_icon( 'bullet_brown', 'imgtag', array( 'title' => emblk_get_status_title( 'spammer' ) ) ),
  76          );
  77  }
  78  
  79  
  80  /**

  81   * Get status levels of email address

  82   *

  83   * @return array Status levels

  84   */
  85  function emblk_get_status_levels()
  86  {
  87      $levels = array(
  88              'unknown'     => 1,
  89              'warning'     => 2,
  90              'suspicious1' => 3,
  91              'suspicious2' => 4,
  92              'suspicious3' => 5,
  93              'prmerror'    => 6,
  94              'spammer'     => 7,
  95          );
  96  
  97      return $levels;
  98  }
  99  
 100  
 101  /**

 102   * Get status level of email address by status value

 103   *

 104   * @param string Status value

 105   * @return integer Status level

 106   */
 107  function emblk_get_status_level( $status )
 108  {
 109      $levels = emblk_get_status_levels();
 110  
 111      return isset( $levels[ $status ] ) ? $levels[ $status ] : 0;
 112  }
 113  
 114  
 115  /**

 116   * Get statuses of email address by status value which have a level less or equal then level of the given status

 117   *

 118   * @param string Status value

 119   * @return array Statuses

 120   */
 121  function emblk_get_statuses_less_level( $status )
 122  {
 123      $levels = emblk_get_status_levels();
 124      $current_level = emblk_get_status_level( $status );
 125  
 126      $statuses = array();
 127      foreach( $levels as $status => $level )
 128      {
 129          if( $level <= $current_level )
 130          {    // Add this status into array if the level is less or equal then current level
 131              $statuses[] = $status;
 132          }
 133      }
 134  
 135      return $statuses;
 136  }
 137  
 138  
 139  /**

 140   * Get status title of email address by status value

 141   *

 142   * @param string Status value

 143   * @return string Status title

 144   */
 145  function emblk_get_status_title( $status )
 146  {
 147      $emblk_statuses = emblk_get_status_titles();
 148  
 149      return isset( $emblk_statuses[ $status ] ) ? $emblk_statuses[ $status ] : $status;
 150  }
 151  
 152  
 153  /**

 154   * Get status color of email address by status value

 155   *

 156   * @param string Status value

 157   * @return string Color value

 158   */
 159  function emblk_get_status_color( $status )
 160  {
 161      if( $status == 'NULL' )
 162      {
 163          $status = '';
 164      }
 165  
 166      $emblk_statuses = emblk_get_status_colors();
 167  
 168      return isset( $emblk_statuses[ $status ] ) ? '#'.$emblk_statuses[ $status ] : 'none';
 169  }
 170  
 171  
 172  /**

 173   * Get status icon of email address by status value

 174   *

 175   * @param string Status value

 176   * @return string Icon

 177   */
 178  function emblk_get_status_icon( $status )
 179  {
 180      $emblk_statuses = emblk_get_status_icons();
 181  
 182      return isset( $emblk_statuses[ $status ] ) ? $emblk_statuses[ $status ] : '';
 183  }
 184  
 185  
 186  /**

 187   * Get result info of email log

 188   *

 189   * @param string Result ( 'ok', 'error', 'blocked' )

 190   * @param boolean Params

 191   * @return string Result info

 192   */
 193  function emlog_result_info( $result, $params = array() )
 194  {
 195      $params = array_merge( array(
 196              'display_icon'  => true,    // Display icon
 197              'display_text'  => true,    // Display text
 198              'link_blocked'  => false, // TRUE - to display 'Blocked' as link to go to page with Blocked email adresses with filter by email
 199              'email'         => '',    // Email address to filter
 200          ), $params );
 201  
 202      $result_info = '';
 203  
 204      switch( $result )
 205      {
 206          case 'ok':
 207              if( $params['display_icon'] )
 208              {
 209                  $result_info .= get_icon( 'bullet_green', 'imgtag', array( 'alt' => T_('Ok') ) );
 210              }
 211              if( $params['display_text'] )
 212              {
 213                  $result_info .= ' '.T_('Ok');
 214              }
 215              break;
 216  
 217          case 'error':
 218              if( $params['display_icon'] )
 219              {
 220                  $result_info .= get_icon( 'bullet_red', 'imgtag', array( 'alt' => T_('Error') ) );
 221              }
 222              if( $params['display_text'] )
 223              {
 224                  $result_info .= ' '.T_('Error');
 225              }
 226              break;
 227  
 228          case 'blocked':
 229              if( $params['display_icon'] )
 230              {
 231                  $result_info .= get_icon( 'bullet_black', 'imgtag', array( 'alt' => T_('Blocked') ) );
 232              }
 233              if( $params['display_text'] )
 234              {
 235                  if( $params['link_blocked'] && !empty( $params['email'] ) )
 236                  {    // Create a link for email address
 237                      global $admin_url;
 238                      $result_info .= ' <a href="'.$admin_url.'?ctrl=email&amp;tab=blocked&amp;email='.$params['email'].'">'.T_('Blocked').'</a>';
 239                  }
 240                  else
 241                  {    // Display only text
 242                      $result_info .= ' '.T_('Blocked');
 243                  }
 244              }
 245              break;
 246      }
 247  
 248      return $result_info;
 249  }
 250  
 251  
 252  /**

 253   * Add a mail log

 254   *

 255   * @param integer User ID

 256   * @param string To (email address)

 257   * @param string Subject

 258   * @param string Message

 259   * @param string Headers

 260   * @param string Result type ( 'ok', 'error', 'blocked' )

 261   */
 262  function mail_log( $user_ID, $to, $subject, $message, $headers, $result )
 263  {
 264      global $DB, $servertimenow;
 265  
 266      if( empty( $user_ID ) )
 267      {
 268          $user_ID = NULL;
 269      }
 270  
 271      $DB->query( 'INSERT DELAYED INTO T_email__log
 272          ( emlog_user_ID, emlog_to, emlog_result, emlog_subject, emlog_message, emlog_headers )
 273          VALUES
 274          ( '.$DB->quote( $user_ID ).',
 275            '.$DB->quote( $to ).',
 276            '.$DB->quote( $result ).',
 277            '.$DB->quote( evo_substr( $subject, 0, 255 ) ).',
 278            '.$DB->quote( $message ).',
 279            '.$DB->quote( $headers ).' )' );
 280  
 281      if( $result == 'ok' )
 282      {    // Save a report about sending of this message in the table T_email__blocked
 283          // The mail sending is susccess. Update last sent date and increase a counter

 284          $DB->query( 'INSERT INTO T_email__blocked ( emblk_address, emblk_sent_count, emblk_sent_last_returnerror, emblk_last_sent_ts )
 285              VALUES( '.$DB->quote( $to ).', 1, 1, '.$DB->quote( date( 'Y-m-d H:i:s', $servertimenow ) ).' )
 286              ON DUPLICATE KEY UPDATE
 287                  emblk_sent_count = emblk_sent_count + 1,
 288                  emblk_sent_last_returnerror = emblk_sent_last_returnerror + 1,
 289                  emblk_last_sent_ts = '.$DB->quote( date( 'Y-m-d H:i:s', $servertimenow ) ) );
 290      }
 291  }
 292  
 293  
 294  /**

 295   * Load the blocked emails from DB in cache

 296   *

 297   * @param array User IDs

 298   * @param array Blocked statuses to know what emails are blocked to send

 299   *     'unknown'     - Unknown

 300   *     'warning'     - Warning

 301   *     'suspicious1' - Suspicious 1

 302   *     'suspicious2' - Suspicious 2

 303   *     'suspicious3' - Suspicious 3

 304   *     'prmerror'    - Permament error

 305   *     'spammer'     - Spammer

 306   */
 307  function load_blocked_emails( $user_IDs, $blocked_statuses = array() )
 308  {
 309      global $DB, $cache_mail_is_blocked_status;
 310  
 311      if( $user_IDs )
 312      { // No users, Exit here
 313          return;
 314      }
 315  
 316      if( !isset( $cache_mail_is_blocked_status ) )
 317      { // Init array first time
 318          $cache_mail_is_blocked_status = array();
 319      }
 320  
 321      $status_filter_name = implode( '_', $blocked_statuses );
 322      if( !isset( $cache_mail_is_blocked_status[ $status_filter_name ] ) )
 323      { // Init subarray for each filter by statuses
 324          $cache_mail_is_blocked_status[ $status_filter_name ] = array();
 325      }
 326  
 327      $SQL = new SQL();
 328      $SQL->SELECT( 'user_email, emblk_ID' );
 329      $SQL->FROM( 'T_users' );
 330      $SQL->FROM_add( 'LEFT JOIN T_email__blocked
 331           ON user_email = emblk_address
 332          AND '.get_mail_blocked_condition( true, $blocked_statuses ) );
 333      $SQL->WHERE( 'user_ID IN ( '.$DB->quote( $user_IDs ).' )' );
 334      $blocked_emails = $DB->get_assoc( $SQL->get() );
 335  
 336      foreach( $blocked_emails as $email => $email_blocked_ID )
 337      { // The blocked email has TRUE value; Trust emails - FALSE
 338          $cache_mail_is_blocked_status[ $status_filter_name ][ $email ] = (boolean) $email_blocked_ID;
 339      }
 340  }
 341  
 342  
 343  /**

 344   * Check if the email address is blocked

 345   *

 346   * @param string Email address

 347   * @param array Blocked statuses to know what emails are blocked to send

 348   *     'unknown'     - Unknown

 349   *     'warning'     - Warning

 350   *     'suspicious1' - Suspicious 1

 351   *     'suspicious2' - Suspicious 2

 352   *     'suspicious3' - Suspicious 3

 353   *     'prmerror'    - Permament error

 354   *     'spammer'     - Spammer

 355   * @return boolean TRUE

 356   */
 357  function mail_is_blocked( $email, $blocked_statuses = array() )
 358  {
 359      global $cache_mail_is_blocked_status;
 360  
 361      if( !isset( $cache_mail_is_blocked_status ) )
 362      { // Init array first time
 363          $cache_mail_is_blocked_status = array();
 364      }
 365  
 366      $status_filter_name = implode( '_', $blocked_statuses );
 367      if( !isset( $cache_mail_is_blocked_status[ $status_filter_name ] ) )
 368      { // Init subarray for each filter by statuses
 369          $cache_mail_is_blocked_status[ $status_filter_name ] = array();
 370      }
 371  
 372      if( !isset( $cache_mail_is_blocked_status[ $status_filter_name ][ $email ] ) )
 373      { // If we check status of this email first time - get it from DB and store in cache
 374          global $DB;
 375          $SQL = new SQL();
 376          $SQL->SELECT( 'emblk_ID' );
 377          $SQL->FROM( 'T_email__blocked' );
 378          $SQL->WHERE( 'emblk_address = '.$DB->quote( $email ) );
 379          $SQL->WHERE_and( get_mail_blocked_condition( true, $blocked_statuses ) );
 380          $cache_mail_is_blocked_status[ $status_filter_name ][ $email ] = (boolean) $DB->get_var( $SQL->get() );
 381      }
 382  
 383      // Get email block status from cache variable

 384      return $cache_mail_is_blocked_status[ $status_filter_name ][ $email ];
 385  }
 386  
 387  
 388  /**

 389   * Get where conditino to check if a mail is blocked or not

 390   *

 391   * @param boolean set true for blocked emails and false for not blocked emails

 392   * @param array Blocked statuses to know what emails are blocked to send

 393   *     'unknown'     - Unknown

 394   *     'warning'     - Warning

 395   *     'suspicious1' - Suspicious 1

 396   *     'suspicious2' - Suspicious 2

 397   *     'suspicious3' - Suspicious 3

 398   *     'prmerror'    - Permament error

 399   *     'spammer'     - Spammer

 400   * @return string the where condition

 401   */
 402  function get_mail_blocked_condition( $is_blocked = true, $blocked_statuses = array() )
 403  {
 404      global $DB;
 405  
 406      if( empty( $blocked_statuses ) )
 407      {    // Default the blocked statuses
 408          $blocked_statuses = array( 'prmerror', 'spammer' );
 409      }
 410  
 411      $operator = $is_blocked ? 'IN' : 'NOT IN';
 412      return 'emblk_status '.$operator.' ( '.$DB->quote( $blocked_statuses ).' )';
 413  }
 414  
 415  
 416  /**

 417   * Memorize the blocked emails in cache array in order to display the message 

 418   * @see blocked_emails_display()

 419   *

 420   * @param string Email address

 421   */
 422  function blocked_emails_memorize( $email )
 423  {
 424      global $current_User, $cache_blocked_emails;
 425  
 426      if( empty( $email ) )
 427      { // Empty email, Exit here
 428          return;
 429      }
 430  
 431      if( is_logged_in() && $current_User->check_perm( 'users', 'view' ) )
 432      { // User has permissions to view other users
 433          if( mail_is_blocked( $email ) )
 434          { // Check if the email address is blocked
 435              if( isset( $cache_blocked_emails[ $email ] ) )
 436              { // Icrease a count of blocked email
 437                  $cache_blocked_emails[ $email ]++;
 438              }
 439              else
 440              {
 441                  $cache_blocked_emails[ $email ] = 1;
 442              }
 443          }
 444      }
 445  }
 446  
 447  
 448  /**

 449   * Display the blocked emails from cache array

 450   */
 451  function blocked_emails_display()
 452  {
 453      global $Messages, $cache_blocked_emails;
 454  
 455      if( !empty( $cache_blocked_emails ) && is_array( $cache_blocked_emails ) )
 456      { // Display the messages about the blocked emails (grouped by email)
 457          foreach( $cache_blocked_emails as $blocked_email => $blocked_emails_count )
 458          {
 459              $Messages->add( sprintf( T_('We could not send %d email to %s because this address is blocked.'), $blocked_emails_count, $blocked_email ) );
 460          }
 461      }
 462  }
 463  
 464  
 465  /**

 466   * Parse message of mail if content is in multipart or HTML format

 467   *

 468   * @param string Headers

 469   * @param string Message

 470   * @return array|boolean Mail data or FALSE when message has only Plain Text content

 471   */
 472  function mail_log_parse_message( $headers, $message )
 473  {
 474      preg_match( '/Content-Type: ([^;]+);/i', $headers, $header_matches );
 475  
 476      if( empty( $header_matches[1] ) )
 477      { // Incorrect headers, Exit here
 478          return false;
 479      }
 480  
 481      // Get only <body> content of html email message

 482      $html_search_body = '#.+<body[^>]*>(.+)</body>.+#is';
 483      $html_replace_body = '$1';
 484  
 485      $data = array();
 486  
 487      if( $header_matches[1] == 'text/html' )
 488      { // Message has one content in HTML format
 489          $data['html'] = array(
 490                  'type' => $header_matches[0],
 491                  'content' => preg_replace( $html_search_body, $html_replace_body, $message ),
 492              );
 493  
 494          return $data;
 495      }
 496      elseif( $header_matches[1] != 'multipart/mixed' )
 497      { // Message content is not multipart
 498          return false;
 499      }
 500  
 501      preg_match( '/Content-Type: multipart\/alternative; boundary="([^;]+)"/i', $message, $boundary_matches );
 502  
 503      if( empty( $boundary_matches ) || empty( $boundary_matches[1] ) )
 504      { // No found boundary delimiter of message contents
 505          return false;
 506      }
 507  
 508      $boundary_delimiter = '--'.$boundary_matches[1];
 509  
 510      $contents = explode( $boundary_delimiter, $message );
 511      unset( $contents[0] );
 512      unset( $contents[3] );
 513  
 514      foreach( $contents as $content )
 515      {
 516          preg_match( '/(Content-Type: ([^;]+);(.+)){1}\n([\s\S\n]+)/i', $content, $type_matches );
 517  
 518          switch( $type_matches[2] )
 519          {
 520              case 'text/html':
 521                  // Get data of Plain Text content

 522                  $data['html'] = array(
 523                          'type' => $type_matches[1],
 524                          'content' => preg_replace( $html_search_body, $html_replace_body, $type_matches[4] ),
 525                      );
 526                  break;
 527  
 528              case 'text/plain':
 529                  // Get data of HTML content

 530                  $data['text'] = array(
 531                          'type' => $type_matches[1],
 532                          'content' => $type_matches[4],
 533                      );
 534                  break;
 535          }
 536      }
 537  
 538      return $data;
 539  }
 540  
 541  ?>

title

Description

title

Description

title

Description

title

title

Body