Source for file fetch.php

Documentation is available at fetch.php

  1. <?php
  2.  
  3. /**
  4.  * mail_fetch/fetch.php
  5.  *
  6.  * Fetch code.
  7.  *
  8.  * @copyright 1999-2012 The SquirrelMail Project Team
  9.  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  10.  * @version $Id: fetch.php 14345 2012-12-09 11:58:17Z kink $
  11.  * @package plugins
  12.  * @subpackage mail_fetch
  13.  */
  14.  
  15. define('SM_PATH','../../');
  16.  
  17. require_once(SM_PATH 'include/validate.php');
  18. include_once(SM_PATH 'functions/imap.php');
  19. include_once(SM_PATH 'plugins/mail_fetch/class.POP3.php');
  20. include_once(SM_PATH 'plugins/mail_fetch/functions.php' );
  21.  
  22. // don't load this page if this plugin is not enabled
  23. //
  24. global $plugins;
  25. if (!in_array('mail_fetch'$plugins)) exit;
  26.  
  27. /* globals */
  28. sqgetGlobalVar('username',   $username,   SQ_SESSION);
  29. sqgetGlobalVar('key',        $key,        SQ_COOKIE);
  30. sqgetGlobalVar('onetimepad'$onetimepadSQ_SESSION);
  31. sqgetGlobalVar('delimiter',  $delimiter,  SQ_SESSION);
  32. /* end globals */
  33.  
  34. function Mail_Fetch_Status($msg{
  35.     echo html_tag'table',
  36.              html_tag'tr',
  37.                  html_tag'td'sm_encode_html_special_chars$msg 'left' )
  38.                      ),
  39.                    '''''width="90%"' );
  40.     flush();
  41. }
  42.  
  43. displayPageHeader($color'None');
  44.  
  45. $mailfetch_server_number getPref($data_dir$username"mailfetch_server_number");
  46. if (!isset($mailfetch_server_number)) $mailfetch_server_number=0;
  47. $mailfetch_cypher getPref($data_dir$username"mailfetch_cypher");
  48. if ($mailfetch_server_number<1$mailfetch_server_number=0;
  49. for ($i=0;$i<$mailfetch_server_number;$i++{
  50.     $mailfetch_server_[$igetPref($data_dir$username"mailfetch_server_$i");
  51.     $mailfetch_port_[$igetPref($data_dir$username"mailfetch_port_$i");
  52.     $mailfetch_alias_[$igetPref($data_dir$username"mailfetch_alias_$i");
  53.     $mailfetch_user_[$igetPref($data_dir$username"mailfetch_user_$i");
  54.     $mailfetch_pass_[$igetPref($data_dir$username"mailfetch_pass_$i");
  55.     $mailfetch_lmos_[$igetPref($data_dir$username"mailfetch_lmos_$i");
  56.     $mailfetch_login_[$igetPref($data_dir$username"mailfetch_login_$i");
  57.     $mailfetch_uidl_[$igetPref($data_dir$username"mailfetch_uidl_$i");
  58.     $mailfetch_subfolder_[$igetPref($data_dir$username"mailfetch_subfolder_$i");
  59.     if$mailfetch_cypher == 'on' {
  60.         $mailfetch_pass_[$idecrypt$mailfetch_pass_[$i);
  61.     }
  62. }
  63.  
  64. echo '<br><center>';
  65.  
  66. echo html_tag'table',
  67.          html_tag'tr',
  68.              html_tag'td''<b>' _("Remote POP server Fetching Mail"'</b>''center'$color[0)
  69.                  ,
  70.          'center''''width="95%" cols="1"' );
  71.  
  72. // get $server_to_fetch from globals, if not set display a choice to the user
  73. if (sqgetGlobalVar('server_to_fetch'$server_to_fetchSQ_POST) ) {
  74.  
  75.     echo '<font size=-5><br></font>' .
  76.         "<form action=\"$PHP_SELF\" method=\"post\" target=\"_self\">.
  77.         html_tag'table''''center''''width="70%" cols="2"' .
  78.         html_tag'tr' .
  79.         html_tag'td'_("Select Server:"' &nbsp; &nbsp;''right' .
  80.         html_tag'td''''left' .
  81.         '<select name="server_to_fetch" size="1">' .
  82.         '<option value="all" selected>..' _("All""...\n";
  83.     for ($i=0;$i<$mailfetch_server_number;$i++{
  84.         echo "<option value=\"$i\">.
  85.             sm_encode_html_special_chars((($mailfetch_alias_[$i]=='')?$mailfetch_server_[$i]:$mailfetch_alias_[$i])) .
  86.             '</option>' "\n";
  87.     }
  88.     echo            '</select>' .
  89.         '</td>' .
  90.         '</tr>';
  91.  
  92.     //if password not set, ask for it
  93.     for ($i=0;$i<$mailfetch_server_number;$i++{
  94.         if ($mailfetch_pass_[$i]==''{
  95.             echo html_tag'tr',
  96.                      html_tag'td'_("Password for"' <b>' .
  97.                          sm_encode_html_special_chars((($mailfetch_alias_[$i]=='')?$mailfetch_server_[$i]:$mailfetch_alias_[$i])) .
  98.                          '</b>: &nbsp; &nbsp; ',
  99.                          'right' .
  100.                      html_tag'td''<input type="password" name="pass_' $i '">''left' )
  101.                            );
  102.         }
  103.     }
  104.     echo html_tag'tr',
  105.              html_tag'td''&nbsp;' .
  106.              html_tag'td''<input type=submit name=submit_mailfetch value="' _("Fetch Mail")'">''left' )
  107.              .
  108.         '</table></form>';
  109.     exit();
  110. }
  111.  
  112. if $server_to_fetch == 'all' {
  113.     $i_start 0;
  114.     $i_stop $mailfetch_server_number;
  115. else {
  116.     $i_start $server_to_fetch;
  117.     $i_stop $i_start+1;
  118. }
  119.  
  120. for ($i_loop=$i_start;$i_loop<$i_stop;$i_loop++{
  121.     $mailfetch_server=$mailfetch_server_[$i_loop];
  122.     $mailfetch_port=$mailfetch_port_[$i_loop];
  123.     $mailfetch_user=$mailfetch_user_[$i_loop];
  124.     if ($mailfetch_pass_[$i_loop== ''{
  125.         sqgetGlobalVar("pass_$i_loop"$mailfetch_passSQ_POST);
  126.     else {
  127.         $mailfetch_pass $mailfetch_pass_[$i_loop];
  128.     }
  129.     $mailfetch_lmos=$mailfetch_lmos_[$i_loop];
  130.     $mailfetch_login=$mailfetch_login_[$i_loop];
  131.     $mailfetch_uidl=$mailfetch_uidl_[$i_loop];
  132.     $mailfetch_subfolder=$mailfetch_subfolder_[$i_loop];
  133.  
  134.     $pop3 new POP3($mailfetch_server60);
  135.  
  136.     echo '<br>' .
  137.         html_tag'table',
  138.             html_tag'tr',
  139.                 html_tag'td''<b>' _("Fetching from ".
  140.                     sm_encode_html_special_chars((($mailfetch_alias_[$i_loop== '')?$mailfetch_server:$mailfetch_alias_[$i_loop])) .
  141.                     '</b>',
  142.                 'center' ,
  143.             ''$color[9,
  144.         '''''width="90%"' );
  145.  
  146.     flush();
  147.  
  148.     if (!$pop3->connect($mailfetch_server,$mailfetch_port)) {
  149.         Mail_Fetch_Status(_("Oops, "$pop3->ERROR );
  150.         continue;
  151.     }
  152.  
  153.     Mail_Fetch_Status(_("Opening IMAP server"));
  154.     $imap_stream sqimap_login($username$key$imapServerAddress$imapPort10);
  155.  
  156.     // check if destination folder is not set, is not subscribed and is not \noselect folder
  157.     if($mailfetch_subfolder == '' || 
  158.        mail_fetch_check_folder($imap_stream,$mailfetch_subfolder)) {
  159.         $mailfetch_subfolder 'INBOX';
  160.     }
  161.  
  162.     Mail_Fetch_Status(_("Opening POP server"));
  163.     $Count $pop3->login($mailfetch_user$mailfetch_pass);
  164.     if (($Count == false || $Count == -1&& $pop3->ERROR != ''{
  165.         Mail_Fetch_Status(_("Login Failed:"' ' $pop3->ERROR );
  166.         continue;
  167.     }
  168.  
  169.     //   register_shutdown_function($pop3->quit());
  170.  
  171.     $msglist $pop3->uidl();
  172.  
  173.     $i 1;
  174.     for ($j 1$j sizeof($msglist)$j++{
  175.         if ($msglist["$j"== $mailfetch_uidl{
  176.             $i $j+1;
  177.             break;
  178.         }
  179.     }
  180.  
  181.     if ($Count $i{
  182.         Mail_Fetch_Status(_("Login OK: No new messages"));
  183.         $pop3->quit();
  184.         continue;
  185.     }
  186.     if ($Count == 0{
  187.         Mail_Fetch_Status(_("Login OK: Inbox EMPTY"));
  188.         $pop3->quit();
  189.         continue;
  190.     else {
  191.         $newmsgcount $Count $i 1;
  192.         Mail_Fetch_Status(sprintf(_("Login OK: Inbox contains %s messages")$newmsgcount));
  193.     }
  194.  
  195.     Mail_Fetch_Status(_("Fetching UIDL..."));
  196.     // Faster to get them all at once
  197.     $mailfetch_uidl $pop3->uidl();
  198.  
  199.     if (is_array($mailfetch_uidl&& $mailfetch_lmos == 'on')
  200.         Mail_Fetch_Status(_("Server does not support UIDL."));
  201.  
  202.     if ($mailfetch_lmos == 'on'{
  203.         Mail_Fetch_Status(_("Leaving messages on server..."));
  204.     else {
  205.         Mail_Fetch_Status(_("Deleting messages from server..."));
  206.     }
  207.  
  208.     for ($i <= $Count$i++{
  209.         Mail_Fetch_Status(_("Fetching message ""$i);
  210.         if (!ini_get('safe_mode'))
  211.             set_time_limit(20)// 20 seconds per message max
  212.         $Message "";
  213.         $MessArray $pop3->get($i);
  214.  
  215.         while ( (!$MessArrayor (gettype($MessArray!= "array")) {
  216.             Mail_Fetch_Status(_("Oops, "$pop3->ERROR);
  217.             // re-connect pop3
  218.             Mail_Fetch_Status(_("Server error. Disconnect"));
  219.             $pop3->quit();
  220.             Mail_Fetch_Status(_("Reconnect from dead connection"));
  221.             if (!$pop3->connect($mailfetch_server)) {
  222.                 Mail_Fetch_Status(_("Oops, "$pop3->ERROR );
  223.                 Mail_Fetch_Status(_("Saving UIDL"));
  224.                 setPref($data_dir,$username,"mailfetch_uidl_$i_loop"$mailfetch_uidl[$i-1]);
  225.  
  226.                 continue;
  227.             }
  228.             $Count $pop3->login($mailfetch_user$mailfetch_pass);
  229.             if (($Count == false || $Count == -1&& $pop3->ERROR != ''{
  230.                 Mail_Fetch_Status(_("Login Failed:"' ' sm_encode_html_special_chars($pop3->ERROR) );
  231.                 Mail_Fetch_Status(_("Saving UIDL"));
  232.                 setPref($data_dir,$username,"mailfetch_uidl_$i_loop"$mailfetch_uidl[$i-1]);
  233.  
  234.                 continue;
  235.             }
  236.             Mail_Fetch_Status(_("Refetching message ""$i);
  237.             $MessArray $pop3->get($i);
  238.  
  239.         // end while
  240.  
  241.         while (list($lineNum$lineeach ($MessArray)) {
  242.             $Message .= $line;
  243.         }
  244.  
  245.         fputs($imap_stream"A3$i APPEND \"$mailfetch_subfolder\" {strlen($Message"}\r\n");
  246.         $Line fgets($imap_stream1024);
  247.         if (substr($Line01== '+'{
  248.             fputs($imap_stream$Message);
  249.             fputs($imap_stream"\r\n");
  250.             sqimap_read_data($imap_stream"A3$i"false$response$message);
  251.             if $response <> 'OK' {
  252.                 Mail_Fetch_Status(_("Error Appending Message!")." ".$message );
  253.                 Mail_Fetch_Status(_("Closing POP"));
  254.                 $pop3->quit();
  255.                 Mail_Fetch_Status(_("Logging out from IMAP"));
  256.                 sqimap_logout($imap_stream);
  257.  
  258.                 Mail_Fetch_Status(_("Saving UIDL"));
  259.                 setPref($data_dir,$username,"mailfetch_uidl_$i_loop"$mailfetch_uidl[$i-1]);
  260.                 exit;
  261.             else {
  262.                 Mail_Fetch_Status(_("Message appended to mailbox"));
  263.             }
  264.  
  265.             if ($mailfetch_lmos != 'on'{
  266.                 if$pop3->delete($i) ) {
  267.                     Mail_Fetch_Status(sprintf(_("Message %d deleted from remote server!")$i));
  268.                 else {
  269.                     Mail_Fetch_Status(_("Delete failed:"sm_encode_html_special_chars($pop3->ERROR) );
  270.                 }
  271.             }
  272.         else {
  273.             echo "$Line";
  274.             Mail_Fetch_Status(_("Error Appending Message!"));
  275.             Mail_Fetch_Status(_("Closing POP"));
  276.             $pop3->quit();
  277.             Mail_Fetch_Status(_("Logging out from IMAP"));
  278.             sqimap_logout($imap_stream);
  279.  
  280.             // not gurantee corect!
  281.             Mail_Fetch_Status(_("Saving UIDL"));
  282.             setPref($data_dir,$username,"mailfetch_uidl_$i_loop"$mailfetch_uidl[$i-1]);
  283.             exit;
  284.         }
  285.     }
  286.  
  287.     Mail_Fetch_Status(_("Closing POP"));
  288.     $pop3->quit();
  289.     Mail_Fetch_Status(_("Logging out from IMAP"));
  290.     sqimap_logout($imap_stream);
  291.     if (is_array($mailfetch_uidl)) {
  292.         Mail_Fetch_Status(_("Saving UIDL"));
  293.         setPref($data_dir,$username,"mailfetch_uidl_$i_loop"array_pop($mailfetch_uidl));
  294.     }
  295.  
  296.     Mail_Fetch_Status(_("Done"));
  297. }
  298. ?>
  299. </center>
  300. </body>
  301. </html>

Documentation generated on Sat, 25 May 2013 04:21:54 +0200 by phpDocumentor 1.4.3