Source for file functions.php

Documentation is available at functions.php

  1. <?php
  2.  
  3. /**
  4.  * mail_fetch/functions.php
  5.  *
  6.  * Functions for the mail_fetch plugin.
  7.  *
  8.  * Original code from LexZEUS <[email protected]>
  9.  * and [email protected] (extracted from php manual)
  10.  * Adapted for MailFetch by Philippe Mingo <[email protected]>
  11.  *
  12.  * @copyright &copy; 1999-2006 The SquirrelMail Project Team
  13.  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  14.  * @version $Id: functions.php,v 1.21 2006/08/26 17:33:58 tokul Exp $
  15.  * @package plugins
  16.  * @subpackage mail_fetch
  17.  */
  18.  
  19.  
  20. /** pop3 class */
  21. include_once (SM_PATH 'plugins/mail_fetch/constants.php');
  22. include_once (SM_PATH 'plugins/mail_fetch/class.mail_fetch.php');
  23.  
  24. /** declare plugin globals */
  25. global $mail_fetch_allow_unsubscribed;
  26.  
  27. /**
  28.  * Controls use of unsubscribed folders in plugin
  29.  * @global boolean $mail_fetch_allow_unsubscribed 
  30.  * @since 1.5.1 and 1.4.5
  31.  */
  32. $mail_fetch_allow_unsubscribed false;
  33.  
  34. /** load site config */
  35. if (file_exists(SM_PATH 'config/mail_fetch_config.php')) {
  36.     include_once(SM_PATH 'config/mail_fetch_config.php');
  37. elseif (file_exists(SM_PATH 'plugins/mail_fetch/config.php')) {
  38.     include_once(SM_PATH 'plugins/mail_fetch/config.php');
  39. }
  40.  
  41. // hooked functions
  42.  
  43. /**
  44.  * Internal function used to fetch pop3 mails on login
  45.  * @since 1.5.1
  46.  * @private
  47.  */
  48.     include_once (SM_PATH 'functions/imap_general.php');
  49.  
  50.     global $username$data_dir$imapServerAddress$imapPort;
  51.  
  52.     $mailfetch_newlog getPref($data_dir$username'mailfetch_newlog');
  53.  
  54.     $outMsg '';
  55.  
  56.     $mailfetch_server_number getPref($data_dir$username'mailfetch_server_number');
  57.     if (!isset($mailfetch_server_number)) $mailfetch_server_number=0;
  58.     $mailfetch_cypher getPref($data_dir$username'mailfetch_cypher');
  59.     if ($mailfetch_server_number<1$mailfetch_server_number=0;
  60.  
  61.     for ($i_loop=0;$i_loop<$mailfetch_server_number;$i_loop++{
  62.  
  63.         $mailfetch_login_[$i_loopgetPref($data_dir$username"mailfetch_login_$i_loop");
  64.         $mailfetch_fref_[$i_loopgetPref($data_dir$username"mailfetch_fref_$i_loop");
  65.         $mailfetch_pass_[$i_loopgetPref($data_dir$username"mailfetch_pass_$i_loop");
  66.         if$mailfetch_cypher == 'on' )
  67.             $mailfetch_pass_[$i_loopdecrypt$mailfetch_pass_[$i_loop);
  68.  
  69.         if$mailfetch_pass_[$i_loop<> '' &&          // Empty passwords no allowed
  70.                 ( ( $mailfetch_login_[$i_loop== 'on' &&  $mailfetch_newlog == 'on' || $mailfetch_fref_[$i_loop== 'on' ) ) {
  71.  
  72.             // What the heck
  73.             $mailfetch_server_[$i_loopgetPref($data_dir$username"mailfetch_server_$i_loop");
  74.             $mailfetch_port_[$i_loopgetPref($data_dir$username "mailfetch_port_$i_loop");
  75.             $mailfetch_alias_[$i_loopgetPref($data_dir$username"mailfetch_alias_$i_loop");
  76.             $mailfetch_user_[$i_loopgetPref($data_dir$username"mailfetch_user_$i_loop");
  77.             $mailfetch_lmos_[$i_loopgetPref($data_dir$username"mailfetch_lmos_$i_loop");
  78.             $mailfetch_uidl_[$i_loopgetPref($data_dir$username"mailfetch_uidl_$i_loop");
  79.             $mailfetch_subfolder_[$i_loopgetPref($data_dir$username"mailfetch_subfolder_$i_loop");
  80.             $mailfetch_auth_[$i_loopgetPref($data_dir$username"mailfetch_auth_$i_loop",MAIL_FETCH_AUTH_USER);
  81.             $mailfetch_type_[$i_loopgetPref($data_dir$username"mailfetch_type_$i_loop",MAIL_FETCH_USE_PLAIN);
  82.  
  83.             $mailfetch_server=$mailfetch_server_[$i_loop];
  84.             $mailfetch_port=$mailfetch_port_[$i_loop];
  85.             $mailfetch_user=$mailfetch_user_[$i_loop];
  86.             $mailfetch_alias=$mailfetch_alias_[$i_loop];
  87.             $mailfetch_pass=$mailfetch_pass_[$i_loop];
  88.             $mailfetch_lmos=$mailfetch_lmos_[$i_loop];
  89.             $mailfetch_login=$mailfetch_login_[$i_loop];
  90.             $mailfetch_uidl=$mailfetch_uidl_[$i_loop];
  91.             $mailfetch_subfolder=$mailfetch_subfolder_[$i_loop];
  92.             $mailfetch_auth=$mailfetch_auth_[$i_loop];
  93.             $mailfetch_type=$mailfetch_type_[$i_loop];
  94.             // end of what the heck
  95.  
  96.  
  97.             // $outMsg .= "$mailfetch_alias checked<br />";
  98.  
  99.             // $outMsg .= "$mailfetch_alias_[$i_loop]<br />";
  100.  
  101.             // FIXME: duplicate code with different output destination.
  102.  
  103.             $pop3 new mail_fetch(array('host'    => $mailfetch_server,
  104.                                          'port'    => $mailfetch_port,
  105.                                          'auth'    => $mailfetch_auth,
  106.                                          'tls'     => $mailfetch_type,
  107.                                          'timeout' => 60));
  108.  
  109.             if (!empty($pop3->error)) {
  110.                 $outMsg .= _("Warning:"' ' $pop3->error;
  111.                 continue;
  112.             }
  113.  
  114.             $imap_stream sqimap_login($usernamefalse$imapServerAddress$imapPort10);
  115.  
  116.             /* log into pop server*/
  117.             if ($pop3->login($mailfetch_user$mailfetch_pass)) {
  118.                 $outMsg .= _("Login Failed:"' ' $pop3->error;
  119.                 continue;
  120.             }
  121.  
  122.             $aMsgStat $pop3->command_stat();
  123.             if (is_bool($aMsgStat)) {
  124.                 $outMsg .= _("Can't get mailbox status:"' ' htmlspecialchars($pop3->error);
  125.                 continue;
  126.             }
  127.  
  128.             $Count $aMsgStat['count'];
  129.  
  130.             $i 1;
  131.  
  132.             if ($Count>0{
  133.                 // If we leave messages on server, try using UIDL
  134.                 if ($mailfetch_lmos == 'on'{
  135.                     $msglist $pop3->command_uidl();
  136.                     if (is_bool($msglist)) {
  137.                         $outMsg .= _("Server does not support UIDL."' '.htmlspecialchars($pop3->error);
  138.                         // User asked to leave messages on server, but we can't do that.
  139.                         $pop3->command_quit();
  140.                         continue;
  141.                         // $mailfetch_lmos = 'off';
  142.                     else {
  143.                         // calculate number of new messages
  144.                         for ($j 1$j <= sizeof($msglist)$j++{
  145.                             // do strict comparison ('1111.10' should not be equal to '1111.100')
  146.                             if ($msglist[$j=== $mailfetch_uidl{
  147.                                 $i $j+1;
  148.                                 break;
  149.                             }
  150.                         }
  151.                     }
  152.                 }
  153.                 // fetch list of messages with LIST
  154.                 // we can use else control, but we can also set $mailfetch_lmos 
  155.                 // to off if server does not support UIDL.
  156.                 if ($mailfetch_lmos != 'on'{
  157.                     $msglist $pop3->command_list();
  158.                 }
  159.             }
  160.  
  161.             if ($Count $i{
  162.                 $pop3->command_quit();
  163.                 continue;
  164.             }
  165.             if ($Count == 0{
  166.                 $pop3->command_quit();
  167.                 continue;
  168.             }
  169.  
  170.             for ($i <= $Count$i++{
  171.                 if (!ini_get('safe_mode'))
  172.                     set_time_limit(20)// 20 seconds per message max
  173.                 $Message $pop3->command_retr($i);
  174.  
  175.                 if (is_bool($Message)) {
  176.                     $outMsg .= _("Warning:"' ' htmlspecialchars($pop3->error);
  177.                     continue;
  178.                 }
  179.  
  180.                 // check if mail folder is not null and subscribed (There is possible issue with /noselect mail folders)
  181.                 if ($mailfetch_subfolder=='' ||
  182.                     mail_fetch_check_folder($imap_stream,$mailfetch_subfolder)) {
  183.                     fputs($imap_stream"A3$i APPEND INBOX {strlen($Message"}\r\n");
  184.                 else {
  185.                     fputs($imap_stream"A3$i APPEND $mailfetch_subfolder {strlen($Message"}\r\n");
  186.                 }
  187.                 $Line fgets($imap_stream1024);
  188.                 if (substr($Line01== '+'{
  189.                     fputs($imap_stream$Message);
  190.                     fputs($imap_stream"\r\n");
  191.                     sqimap_read_data($imap_stream"A3$i"false$response$message);
  192.  
  193.                     // Check results of append command
  194.                     $response=(implode('',$response));
  195.                     $message=(implode('',$message));
  196.                     if ($response != 'OK'{
  197.                         $outMsg .= _("Error Appending Message!")." ".htmlspecialchars($message);
  198.  
  199.                         if ($mailfetch_lmos == 'on'{
  200.                             setPref($data_dir,$username,"mailfetch_uidl_$i_loop"$msglist[$i-1]);
  201.                         }
  202.                         // Destroy msg list in order to prevent UIDL update
  203.                         $msglist false;
  204.                         // if append fails, don't download other messages
  205.                         break;
  206.                     }
  207.  
  208.                     if ($mailfetch_lmos != 'on'{
  209.                         $pop3->command_dele($i);
  210.                     }
  211.                 else {
  212.                     echo "$Line";
  213.                     $outMsg .= _("Error Appending Message!");
  214.                 }
  215.             }
  216.  
  217.             $pop3->command_quit();
  218.             sqimap_logout($imap_stream);
  219.             if ($mailfetch_lmos == 'on' && is_array($msglist)) {
  220.                 setPref($data_dir,$username,"mailfetch_uidl_$i_loop"array_pop($msglist));
  221.             }
  222.         }
  223.     }
  224.  
  225.     iftrim$outMsg <> '' {
  226.         echo '<br /><font size="1">' _("Mail Fetch Result:""<br />$outMsg</font>";
  227.     }
  228.     if$mailfetch_newlog == 'on' {
  229.         setPref($data_dir$username'mailfetch_newlog''off');
  230.     }
  231. }
  232.  
  233. /**
  234.  * Internal function used to detect new logins
  235.  */
  236.     global $data_dir$username;
  237.  
  238.     setPref$data_dir$username'mailfetch_newlog''on' );
  239. }
  240.  
  241. /**
  242.  * Internal function used to register option block
  243.  * @since 1.5.1
  244.  * @private
  245.  */
  246.     global $optpage_blocks;
  247.  
  248.     $optpage_blocks[array(
  249.             'name' => _("POP3 Fetch Mail"),
  250.             'url'  => '../plugins/mail_fetch/options.php',
  251.             'desc' => _("This configures settings for downloading email from a POP3 mailbox to your account on this server."),
  252.             'js'   => false
  253.             );
  254. }
  255.  
  256. /**
  257.  * Internal function used to update mail_fetch settings
  258.  * when folders are renamed or deleted.
  259.  * @since 1.5.1
  260.  * @private
  261.  */
  262. function mail_fetch_folderact_function($args{
  263.     global $username$data_dir;
  264.  
  265.     if (empty($args|| !is_array($args)) {
  266.         return;
  267.     }
  268.  
  269.     /* Should be 3 ars, 1: old folder, 2: action, 3: new folder */
  270.     if (count($args!= 3{
  271.         return;
  272.     }
  273.  
  274.     list($old_folder$action$new_folder$args;
  275.  
  276.     $mailfetch_server_number getPref($data_dir$username'mailfetch_server_number');
  277.  
  278.     for ($i 0$i $mailfetch_server_number$i++{
  279.         $mailfetch_subfolder getPref($data_dir$username'mailfetch_subfolder_' $i);
  280.  
  281.         if ($mailfetch_subfolder != $old_folder{
  282.             continue;
  283.         }
  284.  
  285.         if ($action == 'delete'{
  286.             setPref($data_dir$username'mailfetch_subfolder_' $i'INBOX');
  287.         elseif ($action == 'rename'{
  288.             setPref($data_dir$username'mailfetch_subfolder_' $i$new_folder);
  289.         }
  290.     }
  291. }
  292. // end of hooked functions
  293.  
  294. /**
  295.  * hex2bin - document me
  296.  */
  297. function hex2bin$data {
  298.  
  299.     /* Original code by [email protected] */
  300.  
  301.     $len strlen($data);
  302.     $newdata '';
  303.     for$i=0$i $len$i += {
  304.         $newdata .= pack"C"hexdecsubstr$data$i2) ) );
  305.     }
  306.     return $newdata;
  307. }
  308.  
  309. function mf_keyED$txt {
  310.  
  311.     global $MF_TIT;
  312.  
  313.     if!isset$MF_TIT ) ) {
  314.         $MF_TIT "MailFetch Secure for SquirrelMail 1.x";
  315.     }
  316.  
  317.     $encrypt_key md5$MF_TIT );
  318.     $ctr 0;
  319.     $tmp "";
  320.     for$i 0$i strlen$txt )$i++ {
  321.         if$ctr == strlen$encrypt_key ) ) $ctr=0;
  322.         $tmp.= substr$txt$isubstr$encrypt_key$ctr);
  323.         $ctr++;
  324.     }
  325.     return $tmp;
  326. }
  327.  
  328. function encrypt$txt {
  329.  
  330.     srand(double) microtime(1000000 );
  331.     $encrypt_key md5rand032000 ) );
  332.     $ctr 0;
  333.     $tmp "";
  334.     for$i 0$i strlen$txt )$i++ {
  335.         if ($ctr==strlen($encrypt_key)) $ctr=0;
  336.         $tmp.= substr($encrypt_key,$ctr,1.
  337.             (substr($txt,$i,1substr($encrypt_key,$ctr,1));
  338.         $ctr++;
  339.     }
  340.     return bin2hexmf_keyED$tmp ) );
  341.  
  342. }
  343.  
  344. function decrypt$txt {
  345.  
  346.     $txt mf_keyEDhex2bin$txt ) );
  347.     $tmp '';
  348.     for $i=0$i strlen$txt )$i++ {
  349.         $md5 substr$txt$i);
  350.         $i++;
  351.         $tmp.= substr$txt$i$md5 );
  352.     }
  353.     return $tmp;
  354. }
  355.  
  356. /**
  357.  * check mail folder
  358.  * @param stream $imap_stream imap connection resource
  359.  * @param string $imap_folder imap folder name
  360.  * @return boolean true, when folder can be used to store messages.
  361.  * @since 1.5.1 and 1.4.5
  362.  */
  363. function mail_fetch_check_folder($imap_stream,$imap_folder{
  364.  
  365.     // check if folder is subscribed or only exists.
  366.     if (sqimap_mailbox_is_subscribed($imap_stream,$imap_folder)) {
  367.         $ret true;
  368.     elseif ($mail_fetch_allow_unsubscribed && sqimap_mailbox_exists($imap_stream,$imap_folder)) {
  369.         $ret true;
  370.     else {
  371.         $ret false;
  372.     }
  373.  
  374.     // make sure that folder can store messages
  375.     if ($ret && mail_fetch_check_noselect($imap_stream,$imap_folder)) {
  376.         $ret false;
  377.     }
  378.  
  379.     return $ret;
  380. }
  381.  
  382. /**
  383.  * Checks if folder is noselect (can't store messages)
  384.  *
  385.  * Function does not check if folder subscribed.
  386.  * @param stream $imap_stream imap connection resource
  387.  * @param string $imap_folder imap folder name
  388.  * @return boolean true, when folder has noselect flag. false in any other case.
  389.  * @since 1.5.1 and 1.4.5
  390.  */
  391. function mail_fetch_check_noselect($imap_stream,$imap_folder{
  392.     $boxes=sqimap_mailbox_list($imap_stream);
  393.     foreach($boxes as $box{
  394.         if ($box['unformatted']==$imap_folder{
  395.             return (bool) check_is_noselect($box['raw']);
  396.         }
  397.     }
  398.     return false;
  399. }

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