[ Index ]
 

Code source de b2evolution 2.1.0-beta

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/blogs/cron/ -> getmail.php (source)

   1  <?php
   2  /**

   3   * pop3-2-b2 mail to blog

   4   *

   5   * b2evolution - {@link http://b2evolution.net/}

   6   * Released under GNU GPL License - {@link http://b2evolution.net/about/license.html}

   7   * @copyright (c)2003-2007 by Francois PLANQUE - {@link http://fplanque.net/}

   8   * This file built upon code from original b2 - http://cafelog.com/

   9   *

  10   * @package htsrv

  11   */
  12  
  13  $output_debugging_info = 0;        # =1 if you want to output debugging info

  14  
  15  /**

  16   * Initialize:

  17   */
  18  require_once dirname(__FILE__).'/../conf/_config.php';
  19  
  20  require_once $inc_path.'_main.inc.php';
  21  
  22  if( !$Settings->get('eblog_enabled') )
  23  {
  24      echo T_('Blog by email feature is not enabled.');
  25      debug_info();
  26      exit();
  27  }
  28  
  29  
  30  // Get test settings

  31  param( 'test_type', 'integer', 0 );
  32  $show_messages = false;
  33  $test_connection_only = false;
  34  $str_failure = '';
  35  
  36  if ( $Settings->get('eblog_test_mode') )
  37  {
  38      $test_type = 2;
  39  }
  40  elseif ( !isset( $current_User ) )
  41  {
  42      $test_type = 0;
  43  }
  44  elseif ( !$current_User->check_perm( 'options', 'edit', true ) )
  45  {
  46      $test_type = 0;
  47  }
  48  
  49  if ( $test_type > 0 )
  50  {
  51      error_reporting (0);
  52  
  53      $page_title = T_('Blog by email');
  54      require_once( dirname(__FILE__).'/../_header.php' ); // TODO: this is a dirty hack, and now it's broken :/

  55      $show_messages = true;
  56      $str_failure = ' <font color="red">[ ' . T_('Failed') . ' ]</font>';
  57      $str_warning = ' <font color="orange">[ ' . T_('Warning') . ' ]</font>';
  58  }
  59  
  60  //---------------------------------------

  61  function echo_message( $strmessage , $color = '', $level = 0 )
  62  {
  63      global $show_messages;
  64      global $test_type;
  65  
  66      if ( $show_messages )
  67      {
  68          if ($level <= $test_type)
  69          {
  70              if ( $color )
  71              {
  72                  echo "<font color='$color'>";
  73              }
  74  
  75              echo $strmessage;
  76  
  77              if ( $color )
  78              {
  79                echo "</font>";
  80              }
  81          }
  82      }
  83  }
  84  //---------------------------------------

  85  
  86  
  87  
  88  switch ( $Settings->get('eblog_method') )
  89  {
  90      case 'pop3':
  91          //--------------------------------------------------------------------

  92          // eblog_method = POP3 (original)

  93          //--------------------------------------------------------------------

  94  
  95          // error_reporting( E_ALL );

  96  
  97          load_class('_ext/_pop3.class.php');
  98  
  99          $pop3 = new POP3();
 100          $port = $Settings->get('eblog_server_port') ? $Settings->get('eblog_server_port') : '110';
 101  
 102          echo T_('Connecting to pop server...'), "<br />\n";
 103          if( !$pop3->connect( $Settings->get('eblog_server_host'), $port ) )
 104          {
 105              echo T_('Connection failed: ').$pop3->ERROR." <br />\n";
 106              exit;
 107          }
 108  
 109          echo T_('Logging into pop server...'), "<br />\n";
 110          $Count = $pop3->login( $Settings->get('eblog_username'), $Settings->get('eblog_password') );
 111          if( (!$Count) || ($Count == -1) )
 112          {
 113              echo T_('No mail or Login Failed:'), " $pop3->ERROR <br />\n";
 114              $pop3->quit();
 115              exit;
 116          }
 117  
 118          if ( $test_type == 1 )
 119          {
 120              echo '<br /><br />' . T_('All Tests complete');
 121              $pop3->quit();
 122              exit;
 123          }
 124  
 125          // ONLY USE THIS IF YOUR PHP VERSION SUPPORTS IT! (PHP >= 3.0.4)

 126          #register_shutdown_function( $pop3->quit() );

 127  
 128          for( $iCount = 1; $iCount <= $Count; $iCount++)
 129          {
 130              printf( T_('Getting message #%d...')."<br />\n", $iCount );
 131              $MsgOne = $pop3->get($iCount);
 132              if((!$MsgOne) || (gettype($MsgOne) != 'array'))
 133              {
 134                  echo $pop3->ERROR, "<br />\n";
 135                  $pop3->quit();
 136                  exit;
 137              }
 138  
 139              echo T_('Processing...'), "<br />\n";
 140              $content = '';
 141              $content_type = '';
 142              $boundary = '';
 143              $bodysignal = 0;
 144              $dmonths = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
 145  
 146              while( list( $lineNum, $line ) = each ($MsgOne) )
 147              {
 148                  if( strlen($line) < 3 ) {
 149                      $bodysignal = 1;
 150                  }
 151                  if ($bodysignal) {
 152                      $content .= $line;
 153                  } else {
 154                      if (preg_match('/Content-Type: /', $line)) {
 155                          $content_type = trim($line);
 156                          $content_type = substr($content_type, 14, strlen($content_type)-14);
 157                          $content_type = explode(';', $content_type);
 158                          $content_type = $content_type[0];
 159                      }
 160                      if (($content_type == 'multipart/alternative') && (preg_match('/boundary="/', $line)) && ($boundary == ''))
 161                      {
 162                          $boundary = trim($line);
 163                          $boundary = explode('"', $boundary);
 164                          $boundary = $boundary[1];
 165                      }
 166                      if (preg_match('/Subject: /', $line))
 167                      {
 168                          $subject = trim($line);
 169                          $subject = substr($subject, 9, strlen($subject)-9);
 170                          if ( $Settings->get('eblog_phonemail') )
 171                          {
 172                              $subject = explode( $Settings->get('eblog_phonemail_separator'), $subject );
 173                              $subject = trim($subject[0]);
 174                          }
 175                          if (!ereg($Settings->get('eblog_subject_prefix'), $subject))
 176                          {
 177                              continue;
 178                          }
 179                      }
 180                      if (preg_match('/Date: /', $line))
 181                      { // of the form '20 Mar 2002 20:32:37'
 182                          $ddate = trim($line);
 183                          $ddate = str_replace('Date: ', '', $ddate);
 184                          if (strpos($ddate, ',')) {
 185                              $ddate = trim(substr($ddate, strpos($ddate, ',')+1, strlen($ddate)));
 186                          }
 187                          $date_arr = explode(' ', $ddate);
 188                          $date_time = explode(':', $date_arr[3]);
 189  
 190                          $ddate_H = $date_time[0];
 191                          $ddate_i = $date_time[1];
 192                          $ddate_s = $date_time[2];
 193  
 194                          $ddate_m = $date_arr[1];
 195                          $ddate_d = $date_arr[0];
 196                          $ddate_Y = $date_arr[2];
 197                          for ($i=0; $i<12; $i++) {
 198                              if ($ddate_m == $dmonths[$i]) {
 199                                  $ddate_m = $i+1;
 200                              }
 201                          }
 202                          $ddate_U = mktime($ddate_H, $ddate_i, $ddate_s, $ddate_m, $ddate_d, $ddate_Y);
 203                          $post_date = date('Y-m-d H:i:s', $ddate_U);
 204                      }
 205                  }
 206              }
 207  
 208              $ddate_today = $localtimenow;
 209              $ddate_difference_days = ($ddate_today - $ddate_U) / 86400;
 210  
 211  
 212              # starts buffering the output

 213              ob_start();
 214  
 215              if ($ddate_difference_days > 14)
 216              {
 217                  echo T_('Too old'), '<br />';
 218                  continue;
 219              }
 220  
 221              if( !preg_match('/'.$Settings->get('eblog_subject_prefix').'/', $subject))
 222              {
 223                  echo T_('Subject prefix does not match').'.<br />';
 224                  continue;
 225              }
 226  
 227              $userpassstring = '';
 228  
 229              echo '<div style="border: 1px dashed #999; padding: 10px; margin: 10px;">';
 230              echo "<p><strong>$iCount</strong></p><p><strong>Subject: </strong>$subject</p>\n";
 231  
 232              $subject = trim(str_replace($Settings->get('eblog_subject_prefix'), '', $subject));
 233  
 234              if ($content_type == 'multipart/alternative') {
 235                  $content = explode('--'.$boundary, $content);
 236                  $content = $content[2];
 237                  $content = explode('Content-Transfer-Encoding: quoted-printable', $content);
 238                  $content = strip_tags($content[1], '<img><p><br><i><b><u><em><strong><strike><font><span><div>');
 239              }
 240              $content = trim($content);
 241  
 242              echo "<p><strong>Content-type:</strong> $content_type, <strong>boundary:</strong> $boundary</p>\n";
 243              echo '<p><strong>', T_('Raw content:'), '</strong><br /><xmp>', $content, '</xmp></p>';
 244  
 245              $eblog_body_terminator = $Settings->get('eblog_body_terminator');
 246              if( ! empty($eblog_body_terminator) )
 247              {
 248                  $btpos = strpos( $content, $eblog_body_terminator );
 249                  if ($btpos) {
 250                      $content = substr($content, 0, $btpos);
 251                  }
 252              }
 253              $content = trim($content);
 254  
 255              $blah = explode("\n", $content);
 256              $firstline = $blah[0];
 257  
 258              if ( $Settings->get('eblog_phonemail') )
 259              {
 260                  $btpos = strpos($firstline, $Settings->get('eblog_phonemail_separator') );
 261                  if ($btpos) {
 262                      $userpassstring = trim(substr($firstline, 0, $btpos));
 263                      $content = trim(substr($content, $btpos+strlen($Settings->get('eblog_phonemail_separator')), strlen($content)));
 264                      $btpos = strpos($content, $Settings->get('eblog_phonemail_separator') );
 265                      if ($btpos) {
 266                          $userpassstring = trim(substr($content, 0, $btpos));
 267                          $content = trim(substr($content, $btpos+strlen($Settings->get('eblog_phonemail_separator')), strlen($content)));
 268                      }
 269                  }
 270                  $contentfirstline = $blah[1];
 271              }
 272              else
 273              {
 274                  $userpassstring = $firstline;
 275                  $contentfirstline = '';
 276              }
 277  
 278              $blah = explode(':', $userpassstring);
 279              $user_login = trim($blah[0]);
 280              $user_pass = @trim($blah[1]);
 281  
 282              $content = $contentfirstline.str_replace($firstline, '', $content);
 283              $content = trim($content);
 284  
 285              echo '<p><strong>', T_('Login:'), '</strong> ', $user_login, ', <strong>', T_('Pass:'), '</strong> ', $user_pass, '</p>';
 286  
 287              if( !user_pass_ok( $user_login, $user_pass ) )
 288              {
 289                  echo '<p><strong>', T_('Wrong login or password.'), '</strong></p></div>';
 290                  continue;
 291              }
 292  
 293              $UserCache = & get_Cache( 'UserCache' );
 294              $loop_User = & $UserCache->get_by_login( $user_login );
 295  
 296              // --- get infos from content -----------

 297              $post_title = xmlrpc_getposttitle($content);
 298              if ($post_title == '')
 299              {
 300                  $post_title = $subject;
 301              }
 302  
 303              if( ! ($post_category = xmlrpc_getpostcategory($content) ) )
 304              {
 305                  $post_category = $Settings->get('eblog_default_category');
 306              }
 307              echo '<p><strong>', T_('Category ID'), ':</strong> ',$post_category,'</p>';
 308  
 309              $content = xmlrpc_removepostdata( $content );
 310  
 311              $blog_ID = get_catblog($post_category); // TODO: should not die, if cat does not exist!

 312              echo '<p><strong>', T_('Blog ID'), ':</strong> ',$blog_ID,'</p>';
 313  
 314              // Check permission:

 315              if( ! $loop_User->check_perm( 'blog_post!published', 'edit', false, $blog_ID ) )
 316              {
 317                  echo "\n", T_('Permission denied.'), '<br />';
 318                  continue;
 319              }
 320  
 321              if (!$Settings->get('eblog_test_mode'))
 322              {
 323                  // CHECK and FORMAT content

 324                  $post_title = format_to_post( trim($post_title), 0, 0 );
 325                  $content = format_to_post( trim($content), $Settings->get('AutoBR'), 0);
 326  
 327                  if( $Messages->display( T_('Cannot post, please correct these errors:'), '', true, 'error' ) )
 328                  {
 329                      $Messages->reset();
 330                      echo '</div>';
 331                      continue;
 332                  }
 333  
 334                  // INSERT NEW POST INTO DB:

 335                  $edited_Item = & new Item();
 336                  $post_ID = $edited_Item->insert( $loop_User->ID, $post_title, $content, $post_date, $post_category,    array(), 'published', $loop_User->locale );
 337  
 338                  // Execute or schedule notifications & pings:

 339                  $edited_Item->handle_post_processing();
 340              }
 341  
 342              echo "\n<p><strong>", T_('Posted title'), ':</strong> ', $post_title, '<br />';
 343              echo "\n<strong>", T_('Posted content'), ':</strong><br /><xmp>', $content, '</xmp></p>';
 344  
 345              if(!$pop3->delete($iCount))
 346              {
 347                  echo '<p>', $pop3->ERROR, '</p></div>';
 348                  $pop3->reset();
 349                  exit;
 350              }
 351              else
 352              {
 353                  echo '<p>', T_('Mission complete, message deleted.'), '</p>';
 354              }
 355  
 356              echo '</div>';
 357              if ($output_debugging_info)
 358              {
 359                  ob_end_flush();
 360              }
 361              else
 362              {
 363                  ob_end_clean();
 364              }
 365          }
 366  
 367          echo T_('OK.'), "<br />\n";
 368  
 369          $pop3->quit();
 370  
 371          timer_stop($output_debugging_info);
 372          exit;
 373  
 374      break;
 375  
 376  
 377      case 'pop3a':
 378          //--------------------------------------------------------------------

 379          // eblog_method = POP3 through IMAP extension (experimental)

 380          //--------------------------------------------------------------------

 381  
 382          if( ! extension_loaded('imap') )
 383          {
 384              echo T_('The php_imap extension is not available to php on this server. Please configure a different email retrieval method on the Features tab.');
 385              exit;
 386          }
 387  
 388          echo_message( '&bull; ' . T_('Connecting and authenticating to mail server') );
 389  
 390          // Prepare the connection string

 391          $port = $Settings->get('eblog_server_port') ? $Settings->get('eblog_server_port') : '110';
 392  
 393          $mailserver = '{' . $Settings->get('eblog_server_host') . ':' . $port . '/pop3}INBOX';
 394  
 395          // Connect to mail server

 396          $mbox = imap_open( $mailserver, $Settings->get('eblog_username'), $Settings->get('eblog_password') )
 397              or die( $str_failure . '<div class="action_messages"><div class="log_error">' . T_('Connection failed: ') . imap_last_error() . '</div></div>' );
 398  
 399          // damn gmail... grr

 400          //$mbox = imap_open ("{pop.gmail.com:995/pop3/ssl/novalidate-cert}INBOX", "xxx@gmail.com", "xxx") or die( T_('Connection failed: ') . imap_last_error() );

 401  
 402          echo_message( ' [ ' . T_('Success') . ' ]<br />' , 'green' );
 403          if ( $test_type == 1 )
 404          {
 405              echo '<br /><br />' . T_('All Tests complete');
 406              imap_close($mbox);
 407              exit();
 408          }
 409  
 410  
 411          // Read messages from server

 412          echo_message( '&bull; ' . T_('Reading messages from server') );
 413          $imap_obj = imap_check($mbox);
 414          echo_message( ' [ ' . $imap_obj->Nmsgs . ' ' . T_('messages') .' ] <br />', 'green' );
 415  
 416          for ( $index=1; $index<= $imap_obj->Nmsgs; $index++ )
 417          {
 418              echo_message( '<br /><b>' . T_('Message') . " #$index" . '</b><br />' );
 419  
 420  
 421              //retrieve and process header

 422              $imap_header = imap_headerinfo($mbox,$index);
 423              $subject = $imap_header->subject;
 424  
 425              //echo_message( '<b>' . T_('Subject') . ':</b>' . $subject . '<br />', "green");

 426              echo_message('&bull;<b>' . T_('Subject') . ':</b>' . $subject );
 427              if( !preg_match( '/'.$Settings->get('eblog_subject_prefix') .'/', $subject ) )
 428              {
 429                  echo_message( ' [ ' . T_('Warning') . ' ] <br />', 'orange' );
 430                  echo_message( '&bull; ' . T_('The subject prefix is not ') . '"' . $Settings->get('eblog_subject_prefix') . '"<br/>', 'orange' );
 431                  continue;
 432              }
 433              else
 434              {
 435                  echo_message( ' [ ' . T_('Pass') . ' ] <br />', 'green' );
 436              }
 437  
 438              // todo: review the post_date code

 439              // of the form '20 Mar 2002 20:32:37'

 440              $ddate = trim($imap_header->Date);
 441              if (strpos($ddate, ',')) {
 442                  $ddate = trim(substr($ddate, strpos($ddate, ',')+1, strlen($ddate)));
 443              }
 444              $date_arr = explode(' ', $ddate);
 445              $date_time = explode(':', $date_arr[3]);
 446  
 447              $ddate_H = $date_time[0];
 448              $ddate_i = $date_time[1];
 449              $ddate_s = $date_time[2];
 450  
 451              $ddate_m = $date_arr[1];
 452              $ddate_d = $date_arr[0];
 453              $ddate_Y = $date_arr[2];
 454  
 455              $dmonths = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
 456  
 457              for ($i=0; $i<12; $i++) {
 458  
 459                  if ($ddate_m == $dmonths[$i]) {
 460                      $ddate_m = $i+1;
 461                  }
 462              }
 463              $ddate_U = mktime($ddate_H, $ddate_i, $ddate_s, $ddate_m, $ddate_d, $ddate_Y);
 464              $post_date = date('Y-m-d H:i:s', $ddate_U);
 465  
 466  
 467              //fetch structure

 468              $imap_structure = imap_fetchstructure ($mbox, $index);
 469              //echo var_dump($imap_structure), "<br /><br />\n\n";

 470  
 471              $strbody = "";
 472  
 473              /* part types

 474              0 text

 475              1 multipart

 476              2 message

 477              3 application

 478              4 audio

 479              5 image

 480              6 video

 481              7 other

 482              */
 483  
 484              if ( $imap_structure->type == 1 )
 485              {
 486                  // multipart message

 487                  for ( $ix=0; $ix< count($imap_structure->parts); $ix++ )
 488                  {
 489                      //foreach ( $imap_structure->parts as $part )

 490                      switch ( $imap_structure->parts[$ix]->type )
 491                      {
 492                          case 0 : // text
 493                            $strbody = imap_fetchbody($mbox,$index,$ix);
 494                          break;
 495                          case 5 : // image
 496                              // awww yeah ;)

 497                              // todo: add code to save attachments *safely*.  refer to files.php case 'file_upload'

 498  
 499                          break;
 500                          default:
 501                              echo_message( '&bull; ' . T_('Unhandled email part type') . "\n" . var_dump($part) . "\n<br/>", 'orange');
 502                          break;
 503                      }
 504  
 505                  }
 506              }
 507              else
 508              {
 509                  // single part

 510                  $strbody = imap_fetchbody ($mbox, $index,1);
 511              }
 512  
 513              // process body

 514              $a_body = split(chr(13),$strbody,2);
 515              $a_authentication = split(':',$a_body[0]);
 516              $content = $a_body[1];
 517              $user_login = trim($a_authentication[0]);
 518              $user_pass = @trim($a_authentication[1]);
 519  
 520              echo_message('&bull;<b>' . T_('Authenticating User') . ":</b> $user_login ");
 521              // authenticate user

 522              if( !user_pass_ok( $user_login, $user_pass ) )
 523              {
 524                  echo_message('[ ' . T_('Fail') .' ]<br />','orange');
 525                  echo_message( '&bull; ' . T_('Wrong login or password.') . ' ' . T_('First line of text in email must be in the format "username:password"') . '<br />','orange');
 526                  continue;
 527              }
 528              else
 529              {
 530                  echo_message('[ ' . T_('Pass') .' ]<br />','green');
 531              }
 532  
 533              $subject = trim(str_replace($Settings->get('eblog_subject_prefix'), '', $subject));
 534  
 535              // remove content after terminator

 536              $eblog_terminator = $Settings->get('eblog_body_terminator');
 537              if ( !empty( $eblog_terminator ) )
 538              {
 539                  $os_terminator = strpos( $content, $Settings->get($eblog_terminator) );
 540                  if ($os_terminator)
 541                  {
 542                      $content = substr($content, 0, $os_terminator);
 543                  }
 544              }
 545              $content = trim($content);
 546  
 547              $UserCache = & get_Cache( 'UserCache' );
 548              $loop_User = & $UserCache->get_by_login( $user_login );
 549  
 550              // --- get infos from content -----------

 551              $post_title = xmlrpc_getposttitle($content);
 552              if ($post_title == '')
 553              {
 554                  $post_title = $subject;
 555              }
 556  
 557              if( ! ($post_category = xmlrpc_getpostcategory($content) ) )
 558              {
 559                  $post_category = $Settings->get('eblog_default_category');
 560              }
 561              echo_message( '&bull;<b>' . T_('Category ID') . ':</b> ' . $post_category . '<br />','',3);
 562  
 563              $content = xmlrpc_removepostdata( $content );
 564  
 565              $blog_ID = get_catblog($post_category); // TODO: should not die, if cat does not exist!

 566              echo_message( '&bull;<b>' . T_('Blog ID') . ':</b> ' . $blog_ID . '<br />','',3);
 567  
 568              // Check permission:

 569              echo_message( '&bull;'.sprintf( T_('Checking permissions for user &laquo;%s&raquo; to post to Blog #%d'), $user_login, $blog_ID ).' ' );
 570              if(  !$loop_User->check_perm( 'blog_post!published', 'edit', false, $blog_ID ) )
 571              {
 572                  echo_message( '[ ' . T_('Permission denied') . ' ]','red' );
 573                  continue;
 574              }
 575              else
 576              {
 577                  echo_message( '[ ' . T_('Pass') . ' ]<br />' , 'green');
 578              }
 579  
 580              // todo: finish this last section

 581              if ( !$test_type > 0 )
 582              {
 583                  // CHECK and FORMAT content

 584                  $post_title = format_to_post( trim($post_title), 0, 0 );
 585                  $content = format_to_post( trim($content), $Settings->get('AutoBR'), 0);
 586  
 587                  if( $Messages->display( T_('Cannot post, please correct these errors:'), '', true, 'error' ) )
 588                  {
 589                      $Messages->reset();
 590                      continue;
 591                  }
 592  
 593                  // INSERT NEW POST INTO DB:

 594                  $edited_Item = & new Item();
 595                  $post_ID = $edited_Item->insert( $loop_User->ID, $post_title, $content, $post_date, $post_category,    array(), 'published', $loop_User->locale );
 596  
 597                  // Execute or schedule notifications & pings:

 598                  $edited_Item->handle_post_processing();
 599              }
 600  
 601              echo_message( '&bull;<b>' . T_('Post title') . ":</b> $post_title<br/>",'',3 );
 602              echo_message( '&bull;<b>' . T_('Post content') . ":</b> $content<br/>",'',3 );
 603              echo_message( '&bull;<b>' . T_('Blog by Email'). ':</b> ');
 604              echo_message( '<b>[ ' . T_('Success') . ' ]</b><br/>', 'green');
 605  
 606              if(!$pop3->delete($iCount))
 607              {
 608                  echo '<p>', $pop3->ERROR, '</p></div>';
 609                  $pop3->reset();
 610                  exit;
 611              }
 612              else
 613              {
 614                  echo '<p>', T_('Mission complete, message deleted.'), '</p>';
 615              }
 616  
 617          }
 618          echo '</div>';
 619  
 620          imap_close($mbox);
 621  
 622      break;
 623  
 624  
 625      default:
 626          echo T_('Blog by email feature not configured');
 627      break;
 628  }
 629  
 630  ?>


Généré le : Thu Nov 29 23:58:50 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics