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

Documentation generated on Tue, 17 Apr 2018 04:25:53 +0200 by phpDocumentor 1.4.3