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 mailfetch 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 1999-2020 The SquirrelMail Project Team
  13.  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  14.  * @version $Id: functions.php 14840 2020-01-07 07:42:38Z pdontthink $
  15.  * @package plugins
  16.  * @subpackage mail_fetch
  17.  */
  18.  
  19. /** declare plugin globals */
  20. global $mail_fetch_allow_unsubscribed;
  21.  
  22. /**
  23.  * Controls use of unsubscribed folders in plugin
  24.  * @global boolean $mail_fetch_allow_unsubscribed 
  25.  * @since 1.5.1 and 1.4.5
  26.  */
  27. $mail_fetch_allow_unsubscribed = false;
  28.  
  29. /**
  30.   * Validate a requested POP3 port number
  31.   *
  32.   * Allowable port numbers are configured in config.php
  33.   * (see config_example.php for an example and more
  34.   * rules about how the list of allowable port numbers
  35.   * can be specified)
  36.   *
  37.   * @param int $requested_port The port number given by the user
  38.   *
  39.   * @return string An error string is returned if the port
  40.   *                 number is not allowable, otherwise an
  41.   *                 empty string is returned.
  42.   *
  43.   */
  44. function validate_mail_fetch_port_number($requested_port) {
  45.     global $mail_fetch_allowable_ports;
  46.     @include_once(SM_PATH . 'plugins/mail_fetch/config.php');
  47.     if (empty($mail_fetch_allowable_ports))
  48.         $mail_fetch_allowable_ports = array(110, 995);
  49.  
  50.     if (in_array('ALL', $mail_fetch_allowable_ports))
  51.         return '';
  52.  
  53.     if (!in_array($requested_port, $mail_fetch_allowable_ports)) {
  54.         sq_change_text_domain('mail_fetch');
  55.         $error = _("Sorry, that port number is not allowed");
  56.         sq_change_text_domain('squirrelmail');
  57.         return $error;
  58.     }
  59.  
  60.     return '';
  61. }
  62.  
  63. /**
  64.   * Validate a requested POP3 server address
  65.   *
  66.   * Blocked server addresses are configured in config.php
  67.   * (see config_example.php for more details)
  68.   *
  69.   * @param int $requested_address The server address given by the user
  70.   *
  71.   * @return string An error string is returned if the server
  72.   *                 address is not allowable, otherwise an
  73.   *                 empty string is returned.
  74.   *
  75.   */
  76. function validate_mail_fetch_server_address($requested_address) {
  77.     global $mail_fetch_block_server_pattern;
  78.     @include_once(SM_PATH . 'plugins/mail_fetch/config.php');
  79.     if (empty($mail_fetch_block_server_pattern))
  80.         $mail_fetch_block_server_pattern = '/(^10\.)|(^192\.)|(^127\.)|(^localhost)/';
  81.  
  82.     if ($mail_fetch_block_server_pattern == 'UNRESTRICTED')
  83.         return '';
  84.  
  85.     if (preg_match($mail_fetch_block_server_pattern, $requested_address)) {
  86.         sq_change_text_domain('mail_fetch');
  87.         $error = _("Sorry, that server address is not allowed");
  88.         sq_change_text_domain('squirrelmail');
  89.         return $error;
  90.     }
  91.  
  92.     return '';
  93. }
  94.  
  95. /** 
  96.  * hex2bin() only exists since PHP 5.4
  97.  */
  98. if ( ! function_exists('hex2bin') ) {
  99.     function hex2bin( $data ) {
  100.         /* Original code by [email protected] */
  101.  
  102.         $len = strlen($data);
  103.         $newdata = '';
  104.         for( $i=0; $i < $len; $i += 2 ) {
  105.             $newdata .= pack( "C", hexdec( substr( $data, $i, 2) ) );
  106.         }
  107.         return $newdata;
  108.     }
  109. }
  110.  
  111. function mf_keyED( $txt ) {
  112.     global $MF_TIT;
  113.  
  114.     if( !isset( $MF_TIT ) ) {
  115.         $MF_TIT = "MailFetch Secure for SquirrelMail 1.x";
  116.     }
  117.  
  118.     $encrypt_key = md5( $MF_TIT );
  119.     $ctr = 0;
  120.     $tmp = "";
  121.     for( $i = 0; $i < strlen( $txt ); $i++ ) {
  122.         if( $ctr == strlen( $encrypt_key ) ) $ctr=0;
  123.         $tmp.= substr( $txt, $i, 1 ) ^ substr( $encrypt_key, $ctr, 1 );
  124.         $ctr++;
  125.     }
  126.     return $tmp;
  127. }
  128.  
  129. function encrypt( $txt ) {
  130.     srand( (double) microtime() * 1000000 );
  131.     $encrypt_key = md5( rand( 0, 32000 ) );
  132.     $ctr = 0;
  133.     $tmp = "";
  134.     for( $i = 0; $i < strlen( $txt ); $i++ ) {
  135.         if ($ctr==strlen($encrypt_key)) $ctr=0;
  136.         $tmp.= substr($encrypt_key,$ctr,1) .
  137.             (substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1));
  138.         $ctr++;
  139.     }
  140.     return bin2hex( mf_keyED( $tmp ) );
  141. }
  142.  
  143. function decrypt( $txt ) {
  144.     $txt = mf_keyED( hex2bin( $txt ) );
  145.     $tmp = '';
  146.     for ( $i=0; $i < strlen( $txt ); $i++ ) {
  147.         $md5 = substr( $txt, $i, 1 );
  148.         $i++;
  149.         $tmp.= ( substr( $txt, $i, 1 ) ^ $md5 );
  150.     }
  151.     return $tmp;
  152. }
  153.  
  154. /**
  155.  * check mail folder
  156.  * @param stream $imap_stream imap connection resource
  157.  * @param string $imap_folder imap folder name
  158.  * @return boolean true, when folder can be used to store messages.
  159.  * @since 1.5.1 and 1.4.5
  160.  */
  161. function mail_fetch_check_folder($imap_stream,$imap_folder) {
  162.  
  163.     // check if folder is subscribed or only exists.
  164.     if (sqimap_mailbox_is_subscribed($imap_stream,$imap_folder)) {
  165.         $ret = true;
  166.     } elseif ($mail_fetch_allow_unsubscribed && sqimap_mailbox_exists($imap_stream,$imap_folder)) {
  167.         $ret = true;
  168.     } else {
  169.         $ret = false;
  170.     }
  171.  
  172.     // make sure that folder can store messages
  173.     if ($ret && mail_fetch_check_noselect($imap_stream,$imap_folder)) {
  174.         $ret = false;
  175.     }
  176.  
  177.     return $ret;
  178. }
  179.  
  180. /**
  181.  * Checks if folder is noselect (can't store messages)
  182.  * 
  183.  * Function does not check if folder subscribed.
  184.  * @param stream $imap_stream imap connection resource
  185.  * @param string $imap_folder imap folder name
  186.  * @return boolean true, when folder has noselect flag. false in any other case.
  187.  * @since 1.5.1 and 1.4.5
  188.  */
  189. function mail_fetch_check_noselect($imap_stream,$imap_folder) {
  190.     $boxes=sqimap_mailbox_list($imap_stream);
  191.     foreach($boxes as $box) {
  192.         if ($box['unformatted']==$imap_folder) {
  193.             return (bool) check_is_noselect($box['raw']);
  194.         }
  195.     }
  196.     return false;
  197. }

Documentation generated on Mon, 13 Jan 2020 04:24:39 +0100 by phpDocumentor 1.4.3