"Fossies" - the Fresh Open Source Software Archive

Member "openmailadmin-1.0.1/inc/functions.inc.php" (31 Jul 2006, 9524 Bytes) of package /linux/privat/old/openmailadmin-1.0.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 <?php
    2 /*
    3  * Removes unneccesary whitespace and invokes ob_end_flush().
    4  */
    5 function hsys_ob_end($remove_whitespace) {
    6     if($remove_whitespace) {
    7         $output = ob_get_clean();
    8         $output = preg_replace(array('/\>\s+\</', '/(\s{1})\s{1,}/', '/\s*\n+\s*(?:(?=.*\<textarea)|(?!.*\<\/textarea))/'), array('><', '$1', ''), $output);
    9         @ob_start('ob_gzhandler');
   10         echo($output);
   11     }
   12     ob_end_flush();
   13 }
   14 
   15 /*
   16  * Returns true if given string is in set.
   17  */
   18 function find_in_set($str, $strlist) {
   19     // TODO: increase performance of this function
   20     if(!is_array($strlist))
   21         $strlist = explode(',', str_replace(' ', '', $strlist));
   22     return(in_array($str, $strlist));
   23 }
   24 
   25 /*
   26  * if $elem gets changed, submit current form
   27  */
   28 function ChngS($elem) {
   29     return(addProp($elem, array('onchange' => 'submit()')));
   30 }
   31 
   32 // add a attribute to the given element
   33 function addProp($element, $prop = array()) {
   34     if(count($prop) < 1) {
   35         return $element;
   36     } else {
   37         $ret = '';
   38         foreach($prop as $key => $value)
   39             $ret    .= ' '.strtolower($key).'="'.$value.'"';
   40         return preg_replace('/(<[a-z]+)\s(.*)/', '$1'.$ret.' $2', $element, 1);
   41     }
   42 }
   43 
   44 /*
   45  * displays a nice error
   46  */
   47 function error($text, $width=580) {
   48     global $cfg;
   49     include('./templates/'.$cfg['theme'].'/error_box.tpl');
   50 }
   51 
   52 /*
   53  * displays a nice info-box
   54  */
   55 function info($text, $width=580) {
   56     global $cfg;
   57     include('./templates/'.$cfg['theme'].'/info_box.tpl');
   58 }
   59 
   60 /*
   61  * displays a nice caption
   62  */
   63 function caption($text, $right=null, $width=null) {
   64     global $cfg;
   65     include('./templates/'.$cfg['theme'].'/caption.tpl');
   66 }
   67 
   68 /*
   69  * returns a list of page references
   70  */
   71 function getPageList($link, $anz, $perPage, $cur = 0) {
   72     if(!is_numeric($anz) || !is_numeric($perPage) || $anz == 0 || $perPage == 0)
   73         return '';
   74     $pages = max(1, ceil($anz / $perPage));
   75     if($pages < 2)
   76         return '&nbsp;&nbsp;';
   77     $str = array();
   78     $link = str_replace('%25d', '%d', $link);
   79     for($i = 1; $i <= $pages; $i++) {
   80         if($i == $cur)
   81             $str[] = '<em>'.sprintf($link, $i, $i).'</em>';
   82         else
   83             $str[] = sprintf($link, $i, $i);
   84     }
   85     return implode('&nbsp;', $str);
   86 }
   87 
   88 /*
   89  * creates a nice self-reference
   90  */
   91 function mkSelfRef($arr_Add = array()) {
   92     $qs = array();
   93     foreach(array_merge($_GET, $arr_Add) as $key => $value) {
   94         $qs[] = urlencode(trim($key)).'='.urlencode(trim($value));
   95     }
   96     if(count($qs) > 0)
   97         return($_SERVER['PHP_SELF'].'?'.implode('&', $qs));
   98     else
   99         return $_SERVER['PHP_SELF'];
  100 }
  101 
  102 /*
  103  * Returned array holds usernames as keys and their rights as value.
  104  * Better provide the folder's name again as it may contain whitespace and
  105  * therefore lead to strange results:
  106  * "INBOX.this nouser read" user lrs -> 2 users detected (user "nouser" is incorrect)
  107  */
  108 function hsys_getACLInfo($folder, $name = null) {
  109     global $oma;
  110     $result = array(); $arr = array();
  111 
  112     if(!is_null($name)) {
  113         $folder = str_replace($name, 'aa', $folder);
  114     }
  115 
  116     if(preg_match('/\*\sACL\s[^\s]*\s(.*)/', $folder[0], $arr)) {
  117         if(preg_match_all('/([^\s]*)\s(['.implode('', $oma->imap->get_acl_available()).']*)\s?/', $arr[1], $arr)) {
  118             $result = array_combine($arr[1], $arr[2]);
  119         }
  120     }
  121 
  122     return $result;
  123 }
  124 
  125 /*
  126  * Transforms a string like 'INBOX.Archiv.Drafts' to an array['INBOX']['Archiv']['Drafts']
  127  * and adds ['^'] = string to the top.
  128  * (^ is a magic token in IMAP-Servers which cannot be part of a mailbox' name.)
  129  */
  130 function array_stepper($delimiter, $string) {
  131     $hy['^'] = $string;
  132     foreach(array_reverse(explode($delimiter, $string)) as $key=>$value) {
  133         $hy = array($value => $hy);
  134     }
  135 
  136     return $hy;
  137 }
  138 
  139 /*
  140  * Like array_merge_recursive, but acts on already build arrays
  141  * and densifies/compresses using given fields as keys.
  142  */
  143 function array_densify($arr, $dense_field) {
  144     $j      = 0;
  145     $dense  = array();
  146 
  147     for($i = 0; $i < count($arr); $i++) {
  148         $bdens  = false;
  149         foreach($arr[$i] as $key => $value) {
  150             $dense[$j][$key][]  = $value;
  151         }
  152         if(isset($arr[$i + 1])) {
  153             foreach($dense_field as $field) {
  154                 $bdens = $bdens || $arr[$i + 1][$field] != $arr[$i][$field];
  155             }
  156             if($bdens)
  157                 $j++;
  158         }
  159     }
  160     return $dense;
  161 }
  162 
  163 /* display_tree is specialized for /folders.php */
  164 function display_tree($tree) {
  165     echo('<ul class="tree">');
  166     foreach($tree as $key=>$value) {
  167         $of_interest    = isset($_GET['folder']) && stristr($_GET['folder'], $key);
  168         $this_new   = isset($value['^']) && isset($GLOBALS['to_be_created']) && $value['^'] == $GLOBALS['to_be_created'];
  169         $has_subfolders = !(isset($value['^']) && count($value) == 1);
  170 
  171         // Does this folder contain subfolders?
  172         if($has_subfolders) {
  173             echo('<li class="container">');
  174         } else {
  175             echo('<li class="leaf">');
  176         }
  177 
  178         // Is the node selectable
  179         if(isset($value['^'])) {
  180             // Is this one new? Does it lead to the selected one?
  181             if($this_new) {
  182                 echo('<span class="new_mbox">');
  183             } else if($of_interest) {
  184                 echo('<span class="act_mbox">');
  185             } else {
  186                 echo('<span class="ina_mbox">');
  187             }
  188             // imap_utf7_decode is highly buggy!
  189             echo('<a href="'.mkSelfRef(array('folder' => $value['^'])).'">'.mb_convert_encoding($key, 'UTF-8', 'UTF7-IMAP').'</a></span>');
  190             unset($value['^']);
  191         } else { // ... or just a step?
  192             if($of_interest) {
  193                 echo('<span class="act_mbox">');
  194             } else {
  195                 echo('<span class="ina_mbox">');
  196             }
  197             echo($key.'</span>');
  198         }
  199 
  200         // If it contains subfolders, display them.
  201         if($has_subfolders && count($value) > 0) {
  202             display_tree($value);
  203         }
  204 
  205         // Finally, the ending tag.
  206         echo('</li>');
  207     }
  208     echo('</ul>');
  209 }
  210 
  211 /*
  212  * Responsible for the nice ACL-rights matrix.
  213  */
  214 function hsys_ACL_matrix($ACL, $editable = false, $rights = array('l', 'r', 's', 'w', 'i', 'p', 'c', 'd', 'a')) {
  215     global $cfg, $input;
  216 
  217     $presets = array(   'above'     => txt('110'),
  218                 'lrs'       => txt('114').' (lrs)',
  219                 'lrsp'      => txt('115').' (lrsp)',
  220                 'lrswipcd'  => txt('116').' (lrswipcd)',
  221                 'lrsip'     => txt('117').' (lrsip)',
  222                 'lrswipd'   => txt('118').' (lrswipd)',
  223                 implode('', $rights)    => txt('119').' ('.implode('', $rights).')',
  224                 'none'      => txt('120'));
  225 
  226     include('./templates/'.$cfg['theme'].'/folders/acl_matrix.tpl');
  227 
  228     return true;
  229 }
  230 
  231 /*
  232  * Just a small text obfuscator.
  233  * This one encrypts a given string.
  234  */
  235 function obfuscator_encrypt($cleartext) {
  236     // If mcrypt is available, use that.
  237     if(function_exists('mcrypt_module_open')) {
  238         // Set a "secret" key.
  239         if(!isset($_COOKIE['obfuscator_key'])) {
  240             mt_srand(time());
  241             $key = substr(md5(mt_rand().$cleartext), 0, 24);
  242             setcookie('obfuscator_key', $key);
  243             // set it, in case decryption is done within this pagehit
  244             $_COOKIE['obfuscator_key'] = $key;
  245         } else {
  246             $key = $_COOKIE['obfuscator_key'];
  247         }
  248 
  249         // Here comes the encrpytion.
  250         $td = mcrypt_module_open('tripledes', '', 'ecb', '');
  251         $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
  252         mcrypt_generic_init($td, $key, $iv);
  253 
  254         $encrypted_data = mcrypt_generic($td, $cleartext);
  255         mcrypt_generic_deinit($td);
  256         mcrypt_module_close($td);
  257 
  258         return $encrypted_data;
  259     } else {    // rot13 will do the job
  260         return str_rot13($cleartext);
  261     }
  262 }
  263 
  264 /*
  265  * Just a small text obfuscator.
  266  * This one decrypts the obfuscated string.
  267  */
  268 function obfuscator_decrypt($ciphertext) {
  269     if(function_exists('mcrypt_module_open')) {
  270         // Obtain the "secret" key.
  271         $key = $_COOKIE['obfuscator_key'];
  272 
  273         // Here is the decryption.
  274         $td = mcrypt_module_open('tripledes', '', 'ecb', '');
  275         $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
  276         $key = substr($key, 0, mcrypt_enc_get_key_size($td));
  277         mcrypt_generic_init($td, $key, $iv);
  278 
  279         $decrypted_data = mdecrypt_generic($td, $ciphertext);
  280         mcrypt_generic_deinit($td);
  281         mcrypt_module_close($td);
  282 
  283         return $decrypted_data;
  284     } else {    // obviously rot13 must have been used
  285         return str_rot13($ciphertext);
  286     }
  287 }
  288 
  289 /**
  290  * For combining rows in tables with same content.
  291  *
  292  * @param   arr Array with hashes.
  293  * @param   field   Field to be counted (in hash).
  294  * @param   tagname This field will be added to the hash with number of same following rows. Only first different row wiill carry this tag.
  295  */
  296 function count_same_cols(&$arr, $fieldname, $tagname) {
  297     $c  = 0;
  298     for($i = count($arr)-1; $i >= 0; $i--) {
  299         ++$c;
  300         if(!isset($arr[$i-1][$fieldname]) || $arr[$i-1][$fieldname] != $arr[$i][$fieldname]) {
  301             $arr[$i][$tagname]  = $c;
  302             $c = 0;
  303         }
  304     }
  305 }
  306 
  307 /**
  308  * This is so we can use classes and interfaces without typeing long include lists.
  309  */
  310 function __autoload($class_name) {
  311     $subfolders = array('', 'imap/');
  312     foreach($subfolders as $sub) {
  313         if(is_readable('./inc/lib/'.$sub.$class_name.'.php')) {
  314             require_once('./inc/lib/'.$sub.$class_name.'.php');
  315         }
  316     }
  317 }
  318 
  319 /**
  320  * This one is missing in ADOdb.
  321  *
  322  * @param   needle      fieldname of table (won't be quoted!)
  323  * @param   haystack    Array with elements to be found.
  324  * @returns string      to be included in query
  325  */
  326 function db_find_in_set(ADOConnection $db, $needle, array $haystack) {
  327     $quoted = array();
  328     foreach($haystack as $element) {
  329         array_push($quoted, $db->qstr($element));
  330     }
  331     return $needle.' IN ('.implode(', ', $quoted).')';
  332 }
  333 
  334 /**
  335  * Factory for IMAP instances.
  336  */
  337 function IMAP_get_instance(array $imap_cfg, $facility = 'fake-imap') {
  338     global $cfg;
  339     switch($facility) {
  340         case 'fake-imap':
  341             global $db;
  342             $imap = new Fake_IMAP($imap_cfg, $db, $cfg['tablenames']);
  343             break;
  344         case 'cyrus':
  345             require_once('Log.php');    // from PEAR!
  346             $imap = new Cyrus_IMAP($imap_cfg,
  347                         Log::singleton($cfg['logger']['facility'], $cfg['logger']['name'], 'Cyrus_IMAP'));
  348             break;
  349         default:
  350             throw new Exception('IMAP facility "'.$facility.'" is not implemented.');
  351     }
  352     return $imap;
  353 }
  354 
  355 ?>