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-2020 The SquirrelMail Project Team
  9.  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  10.  * @version $Id: fetch.php 14845 2020-01-07 08:09:34Z pdontthink $
  11.  * @package plugins
  12.  * @subpackage mail_fetch
  13.  */
  14.  
  15. /**
  16.  * Include the SquirrelMail initialization file.
  17.  */
  18. require('../../include/init.php');
  19.  
  20. include_once(SM_PATH 'functions/imap_general.php');
  21. include_once(SM_PATH 'plugins/mail_fetch/functions.php' );
  22.  
  23. // don't load this page if this plugin is not enabled
  24. //
  25. global $plugins;
  26. if (!in_array('mail_fetch'$plugins)) exit;
  27.  
  28. /* globals */
  29. sqgetGlobalVar('delimiter',  $delimiter,  SQ_SESSION);
  30. global $imap_stream_options// in case not defined in config
  31. /* end globals */
  32.  
  33. /**
  34.  * @param string $msg message
  35.  */
  36. function Mail_Fetch_Status($msg{
  37.     echo html_tag'table',
  38.              html_tag'tr',
  39.                  html_tag'td'sm_encode_html_special_chars$msg 'left' )
  40.                  ),
  41.              '''''width="90%"' );
  42.     flush();
  43. }
  44.  
  45. /**
  46.  * @return array 
  47.  */
  48. function Mail_Fetch_Servers({
  49.     global $data_dir$username;
  50.  
  51.     $mailfetch array();
  52.     $mailfetch['server_number'getPref($data_dir$username"mailfetch_server_number");
  53.     if (!isset($mailfetch['server_number']|| ($mailfetch['server_number'1)) {
  54.         $mailfetch['server_number'0;
  55.     }
  56.     $mailfetch['cypher'getPref($data_dir$username"mailfetch_cypher");
  57.  
  58.     for ($i 0$i $mailfetch['server_number']$i++{
  59.         $mailfetch[$i]['server'getPref($data_dir$username"mailfetch_server_$i");
  60.         $mailfetch[$i]['port']   getPref($data_dir$username"mailfetch_port_$i");
  61.         $mailfetch[$i]['alias']  getPref($data_dir$username"mailfetch_alias_$i");
  62.         $mailfetch[$i]['user']   getPref($data_dir$username"mailfetch_user_$i");
  63.         $mailfetch[$i]['pass']   getPref($data_dir$username"mailfetch_pass_$i");
  64.         if($mailfetch['cypher'== 'on'{
  65.             $mailfetch[$i]['pass'decrypt($mailfetch[$i]['pass']);
  66.         }
  67.         if ($mailfetch[$i]['pass'== ''{
  68.             sqgetGlobalVar("pass_$i"$mailfetch[$i]['pass']SQ_POST);
  69.         }
  70.         $mailfetch[$i]['lmos']   getPref($data_dir$username"mailfetch_lmos_$i");
  71.         $mailfetch[$i]['login']  getPref($data_dir$username"mailfetch_login_$i");
  72.         $mailfetch[$i]['uidl']   getPref($data_dir$username"mailfetch_uidl_$i");
  73.         $mailfetch[$i]['subfolder'getPref($data_dir$username"mailfetch_subfolder_$i");
  74.         if($mailfetch[$i]['alias'== ''{
  75.             $mailfetch[$i]['alias'== $mailfetch[$i]['server'];
  76.         }
  77.         // Authentication type (added in 1.5.2)
  78.         $mailfetch[$i]['auth'getPref($data_dir$username"mailfetch_auth_$i",MAIL_FETCH_AUTH_USER);
  79.         // Connection type (added in 1.5.2)
  80.         $mailfetch[$i]['type'getPref($data_dir$username"mailfetch_type_$i",MAIL_FETCH_USE_PLAIN);
  81.     }
  82.     return $mailfetch;
  83. }
  84.  
  85. /**
  86.  * @param array $mailfetch 
  87.  */
  88. function Mail_Fetch_Select_Server($mailfetch{
  89.     global $PHP_SELF;
  90.  
  91.     echo '<font size="-5"><br /></font>' .
  92.         '<form action="'.$PHP_SELF.'" method="post" target="_self">' .
  93.         html_tag'table''''center''''width="70%" cols="2"' .
  94.         html_tag'tr' .
  95.         html_tag'td'_("Select Server:"' &nbsp; &nbsp;''right' .
  96.         html_tag'td''''left' .
  97.         '<select name="server_to_fetch" size="1">' .
  98.         '<option value="all" selected="selected">..' _("All""...\n";
  99.     for ($i 0;$i $mailfetch['server_number'];$i++{
  100.         echo "<option value=\"$i\">.
  101.             sm_encode_html_special_chars($mailfetch[$i]['alias'].
  102.             '</option>' "\n";
  103.     }
  104.     echo            '</select>' .
  105.         '</td>' .
  106.         '</tr>';
  107.  
  108.     //if password not set, ask for it
  109.     for ($i 0;$i $mailfetch['server_number'];$i++{
  110.         if ($mailfetch[$i]['pass'== ''{
  111.             echo html_tag'tr',
  112.                      html_tag'td'_("Password for"' <b>' .
  113.                          sm_encode_html_special_chars($mailfetch[$i]['alias'].
  114.                          '</b>: &nbsp; &nbsp; ',
  115.                          'right' .
  116.                      html_tag'td''<input type="password" name="pass_' $i '" />''left' )
  117.                            );
  118.         }
  119.     }
  120.     echo html_tag'tr',
  121.              html_tag'td''&nbsp;' .
  122.              html_tag'td''<input type="submit" name="submit_mailfetch" value="' _("Fetch Mail")'" />''left' )
  123.                    .
  124.         '</table></form>';
  125. }
  126.  
  127. $mailfetch Mail_Fetch_Servers();
  128.  
  129. echo '<br />';
  130.  
  131. echo html_tag'table',
  132.          html_tag'tr',
  133.              html_tag'td''<b>' _("Remote POP server Fetching Mail"'</b>''center'$color[0)
  134.                    ,
  135.                'center''''width="95%" cols="1"' );
  136.  
  137.  
  138. /* there are no servers defined yet... */
  139. if($mailfetch['server_number'== 0{
  140. //FIXME: do not echo directly to browser -- use templates only
  141.     echo '<p>' _("No POP3 servers configured yet."'</p>';
  142.     echo makeInternalLink('plugins/mail_fetch/options.php',
  143.                         _("Click here to go to the options page.") );
  144.     $oTemplate->display('footer.tpl');
  145.     exit();
  146. }
  147.  
  148. // get $server_to_fetch from globals, if not set display a choice to the user
  149. if (sqgetGlobalVar('server_to_fetch'$server_to_fetchSQ_POST) ) {
  150.     Mail_Fetch_Select_Server($mailfetch);
  151.     $oTemplate->display('footer.tpl');
  152.     exit();
  153. }
  154.  
  155. if $server_to_fetch == 'all' {
  156.     $i_start 0;
  157.     $i_stop  $mailfetch['server_number'];
  158. else {
  159.     $i_start $server_to_fetch;
  160.     $i_stop  $i_start+1;
  161. }
  162.  
  163. for ($i_loop=$i_start;$i_loop<$i_stop;$i_loop++{
  164.     $mailfetch_server $mailfetch[$i_loop]['server'];
  165.     $mailfetch_port   $mailfetch[$i_loop]['port'];
  166.     $mailfetch_user   $mailfetch[$i_loop]['user'];
  167.     $mailfetch_pass   $mailfetch[$i_loop]['pass'];
  168.     $mailfetch_lmos   $mailfetch[$i_loop]['lmos'];
  169.     $mailfetch_login  $mailfetch[$i_loop]['login'];
  170.     $mailfetch_uidl   $mailfetch[$i_loop]['uidl'];
  171.     $mailfetch_subfolder $mailfetch[$i_loop]['subfolder'];
  172.     $mailfetch_auth $mailfetch[$i_loop]['auth'];
  173.     $mailfetch_type $mailfetch[$i_loop]['type'];
  174.  
  175.     echo '<br />' .
  176.         html_tag'table',
  177.             html_tag'tr',
  178.                 html_tag'td''<b>' .
  179.                     sprintf(_("Fetching from %s"),
  180.                         sm_encode_html_special_chars($mailfetch[$i_loop]['alias'])) .
  181.                     '</b>',
  182.                 'center' ,
  183.             ''$color[9,
  184.         '''''width="90%"' );
  185.  
  186.     flush();
  187.  
  188.     $pop3 new mail_fetch(array('host'    => $mailfetch_server,
  189.                                  'port'    => $mailfetch_port,
  190.                                  'auth'    => $mailfetch_auth,
  191.                                  'tls'     => $mailfetch_type,
  192.                                  'timeout' => 60));
  193.  
  194.     if (!empty($pop3->error)) {
  195.         Mail_Fetch_Status($pop3->error);
  196.         continue;
  197.     }
  198.  
  199.     Mail_Fetch_Status(_("Opening IMAP server"));
  200.     $imap_stream sqimap_login($usernamefalse$imapServerAddress$imapPort10$imap_stream_options);
  201.  
  202.     // check if destination folder is not set, is not subscribed and is not \noselect folder
  203.     if($mailfetch_subfolder == '' ||
  204.        mail_fetch_check_folder($imap_stream,$mailfetch_subfolder)) {
  205.         $mailfetch_subfolder 'INBOX';
  206.     }
  207.  
  208.     Mail_Fetch_Status(_("Opening POP server"));
  209.  
  210.     /* log into pop server*/
  211.     if ($pop3->login($mailfetch_user$mailfetch_pass)) {
  212.         Mail_Fetch_Status(_("Login Failed:"' ' sm_encode_html_special_chars($pop3->error));
  213.         continue;
  214.     }
  215.  
  216.     $aMsgStat $pop3->command_stat();
  217.     if (is_bool($aMsgStat)) {
  218.         Mail_Fetch_Status(_("Can't get mailbox status:"' ' sm_encode_html_special_chars($pop3->error) );
  219.         continue;
  220.     }
  221.  
  222.     $Count $aMsgStat['count'];
  223.  
  224.     $i 1;
  225.  
  226.     if ($Count>0{
  227.         // If we leave messages on server, try using UIDL
  228.         if ($mailfetch_lmos == 'on'{
  229.             Mail_Fetch_Status(_("Fetching UIDL..."));
  230.             $msglist $pop3->command_uidl();
  231.             if (is_bool($msglist)) {
  232.                 Mail_Fetch_Status(_("Server does not support UIDL."' '.sm_encode_html_special_chars($pop3->error));
  233.                 // User asked to leave messages on server, but we can't do that.
  234.                 $pop3->command_quit();
  235.                 continue;
  236.                 // $mailfetch_lmos = 'off';
  237.             else {
  238.                 // calculate number of new messages
  239.                 for ($j 1$j <= sizeof($msglist)$j++{
  240.                     // do strict comparison ('1111.10' should not be equal to '1111.100')
  241.                     if ($msglist[$j=== $mailfetch_uidl{
  242.                         $i $j+1;
  243.                         break;
  244.                     }
  245.                 }
  246.             }
  247.         }
  248.         // fetch list of messages with LIST
  249.         // we can use else control, but we can also set $mailfetch_lmos 
  250.         // to off if server does not support UIDL.
  251.         if ($mailfetch_lmos != 'on'{
  252.             Mail_Fetch_Status(_("Fetching list of messages..."));
  253.             $msglist $pop3->command_list();
  254.         }
  255.     }
  256.  
  257.     if ($Count $i{
  258.         Mail_Fetch_Status(_("Login OK: No new messages"));
  259.         $pop3->command_quit();
  260.         continue;
  261.     }
  262.     if ($Count == 0{
  263.         Mail_Fetch_Status(_("Login OK: Inbox EMPTY"));
  264.         $pop3->command_quit();
  265.         continue;
  266.     else {
  267.         $newmsgcount $Count $i 1;
  268.         Mail_Fetch_Status(sprintf(ngettext("Login OK: Inbox contains %s message",
  269.                                            "Login OK: Inbox contains %s messages",$newmsgcount)$newmsgcount));
  270.     }
  271.  
  272.     if ($mailfetch_lmos == 'on'{
  273.         Mail_Fetch_Status(_("Leaving messages on server..."));
  274.     else {
  275.         Mail_Fetch_Status(_("Deleting messages from server..."));
  276.     }
  277.  
  278.     for ($i <= $Count$i++{
  279.         Mail_Fetch_Status(sprintf(_("Fetching message %s.")$i));
  280.  
  281.         if (!ini_get('safe_mode'))
  282.             set_time_limit(20)// 20 seconds per message max
  283.  
  284.         $Message $pop3->command_retr($i);
  285.  
  286.         if (is_bool($Message)) {
  287.             Mail_Fetch_Status(sm_encode_html_special_chars($pop3->error));
  288.             continue;
  289.         }
  290.  
  291.         fputs($imap_stream"A3$i APPEND \"$mailfetch_subfolder\" {strlen($Message"}\r\n");
  292.         $Line fgets($imap_stream1024);
  293.         if (substr($Line01== '+'{
  294.             fputs($imap_stream$Message);
  295.             fputs($imap_stream"\r\n");
  296.             sqimap_read_data($imap_stream"A3$i"false$response$message);
  297.             $response=(implode('',$response));
  298.             $message=(implode('',$message));
  299.             if ($response != 'OK'{
  300.                 Mail_Fetch_Status(_("Error Appending Message!")." ".sm_encode_html_special_chars($message) );
  301.                 Mail_Fetch_Status(_("Closing POP"));
  302.                 $pop3->command_quit();
  303.                 Mail_Fetch_Status(_("Logging out from IMAP"));
  304.                 sqimap_logout($imap_stream);
  305.  
  306.                 if ($mailfetch_lmos == 'on'{
  307.                     Mail_Fetch_Status(_("Saving UIDL"));
  308.                     setPref($data_dir,$username,"mailfetch_uidl_$i_loop"$msglist[$i-1]);
  309.                 }
  310.                 exit;
  311.             else {
  312.                 Mail_Fetch_Status(_("Message appended to mailbox"));
  313.             }
  314.  
  315.             if ($mailfetch_lmos != 'on'{
  316.                 if$pop3->command_dele($i) ) {
  317.                     Mail_Fetch_Status(sprintf(_("Message %d deleted from remote server!")$i));
  318.                 else {
  319.                     Mail_Fetch_Status(_("Delete failed:"sm_encode_html_special_chars($pop3->error) );
  320.                 }
  321.             }
  322.         else {
  323.             echo $Line;
  324.             Mail_Fetch_Status(_("Error Appending Message!"));
  325.             Mail_Fetch_Status(_("Closing POP"));
  326.             $pop3->command_quit();
  327.             Mail_Fetch_Status(_("Logging out from IMAP"));
  328.             sqimap_logout($imap_stream);
  329.  
  330.             // not gurantee corect!
  331.             if ($mailfetch_lmos == 'on'{
  332.                 Mail_Fetch_Status(_("Saving UIDL"));
  333.                 setPref($data_dir,$username,"mailfetch_uidl_$i_loop"$msglist[$i-1]);
  334.             }
  335.             exit;
  336.         }
  337.     }
  338.  
  339.     Mail_Fetch_Status(_("Closing POP"));
  340.     $pop3->command_quit();
  341.     Mail_Fetch_Status(_("Logging out from IMAP"));
  342.     sqimap_logout($imap_stream);
  343.     if ($mailfetch_lmos == 'on' && is_array($msglist)) {
  344.         Mail_Fetch_Status(_("Saving UIDL"));
  345.         setPref($data_dir,$username,"mailfetch_uidl_$i_loop"array_pop($msglist));
  346.     }
  347.  
  348.     Mail_Fetch_Status(_("Done"));
  349. }
  350.  
  351. $oTemplate->display('footer.tpl');

Documentation generated on Mon, 13 Jan 2020 04:22:27 +0100 by phpDocumentor 1.4.3