Source for file fetch.php

Documentation is available at fetch.php

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

Documentation generated on Sat, 07 Oct 2006 16:31:09 +0300 by phpDocumentor 1.3.0RC6