Changeset 1015 in subversion


Ignore:
Timestamp:
Feb 4, 2008 1:16:46 PM (5 years ago)
Author:
tomekp
Message:

more merges

Location:
branches/devel-vnext/program/include
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/devel-vnext/program/include/globals.php

    r979 r1015  
    2727 
    2828/** 
    29  * Quote a given string. Alias function for rep_specialchars_output 
    30  * 
     29 * Quote a given string. 
     30 * Shortcut function for rep_specialchars_output 
     31 * 
     32 * @return string HTML-quoted string 
    3133 * @see rcube::rep_specialchars_output() 
    3234 */ 
    33 function Q($str, $mode = 'strict', $newlines = TRUE) { 
     35function Q($str = '', $mode = 'strict', $newlines = TRUE) { 
    3436    return rcube::rep_specialchars_output($str, 'html', $mode, $newlines); 
    3537} 
    3638 
    3739/** 
    38  * Quote a given string. Alias function for rep_specialchars_output 
    39  * 
     40 * Quote a given string for javascript output. 
     41 * Shortcut function for rep_specialchars_output 
     42 * 
     43 * @return string JS-quoted string 
    4044 * @see rcube::rep_specialchars_output() 
    4145 */ 
    42 function JQ($str) { 
     46function JQ($str = '') { 
    4347    return rcube::rep_specialchars_output($str, 'js'); 
    4448} 
    4549 
     50/** 
     51 * Remove all non-ascii and non-word chars 
     52 * except . and - 
     53 */ 
     54function asciiwords($str = '') { 
     55    return preg_replace('/[^a-z0-9.-_]/i', '', $str); 
     56} 
    4657 
    4758/** 
     
    5162 * @return string Dequoted string 
    5263 */ 
    53 function strip_quotes($str) { 
     64function strip_quotes($str = '') { 
    5465    return preg_replace('/[\'"]/', '', $str); 
    5566} 
     
    6172 * @return string Stripped string 
    6273 */ 
    63 function strip_newlines($str) { 
     74function strip_newlines($str = '') { 
    6475    return preg_replace('/[\r\n]/', '', $str); 
    6576} 
    66  
    6777 
    6878/** 
     
    342352    } 
    343353    return strtolower($str); 
    344     } 
    345354} 
    346355 
     
    374383        return strrpos($haystack, $needle, $offset); 
    375384} 
    376  
    377385 
    378386/** 
     
    393401        $str = rc_substr($str, 0, $first_part_length) . $place_holder . rc_substr($str, $second_starting_location, $length); 
    394402    } 
    395  
    396403    return $str; 
    397404} 
    398  
    399405 
    400406/** 
     
    405411} 
    406412 
    407  
    408413/** 
    409414 * Remove slash at the end of the string 
     
    412417    return preg_replace('/\/$/', '', $str); 
    413418} 
    414  
    415419 
    416420/** 
     
    435439    return true; 
    436440} 
    437  
    438441 
    439442/** 
     
    491494    return $index; 
    492495} 
     496 
     497?> 
  • branches/devel-vnext/program/include/rcube.php

    r981 r1015  
    413413    } 
    414414 
    415  
    416415    /** 
    417416     * Init output object for GUI and add common scripts. 
     
    445444    } 
    446445 
    447  
    448446    /** 
    449447     * Create an output object for JSON responses 
     
    459457        self::set_locale($registry->get('user_lang', 'core')); 
    460458    } 
    461  
    462459 
    463460    /** 
     
    489486        $OUTPUT->set_charset($charset); 
    490487    } 
    491  
    492488 
    493489    /** 
     
    524520            } 
    525521        } 
    526  
    527522        return $host; 
    528523    } 
     
    544539        $IMAP      = $registry->get('IMAP', 'core'); 
    545540        $DB        = $registry->get('DB', 'core'); 
    546         $USER        = $registry->get('USER', 'core'); 
     541        $USER      = $registry->get('USER', 'core'); 
    547542        $user_lang = $registry->get('user_lang', 'core'); 
    548543 
     
    695690 
    696691    /** 
    697      * Create new entry in users and identities table 
    698      * 
    699      * @param string User name 
    700      * @param string IMAP host 
    701      * @return mixed New user ID or False on failure 
    702      */ 
    703     //TODO check this with rcube_user function 
    704     static function create_user($user, $host) 
    705     { 
    706         $registry = rcube_registry::get_instance(); 
    707         $DB       = $registry->get('DB', 'core'); 
    708         $CONFIG   = $registry->get_all('config'); 
    709         $IMAP     = $registry->get('IMAP', 'core'); 
    710  
    711         $user_email = ''; 
    712  
    713         // try to resolve user in virtusertable 
    714         if (!empty($CONFIG['virtuser_file']) && strstr($user, '@') === FALSE) { 
    715             $user_email = self::user2email($user); 
    716         } 
    717         else { // failover 
    718             $user_email = $user; 
    719         } 
    720         $_query = "INSERT INTO " . self::get_table_name('users'); 
    721         $_query.= " (created, last_login, username, mail_host, alias, language)"; 
    722         $_query.= " VALUES (" . $DB->now() . ", " . $DB->now() . ", %s, %s, %s, %s)"; 
    723  
    724         $_query = sprintf( 
    725         $_query, 
    726         $DB->quote(strip_newlines($user)), 
    727         $DB->quote(strip_newlines($host)), 
    728         $DB->quote(strip_newlines($user_email)), 
    729         $DB->quote($_SESSION['user_lang']) 
    730         ); 
    731         rcube::tfk_debug($_query); 
    732  
    733  
    734         $DB->query($_query); 
    735  
    736         if ($user_id = $DB->insert_id(self::get_sequence_name('users'))) { 
    737             $mail_domain = self::mail_domain($host); 
    738  
    739             if ($user_email=='') { 
    740                 $user_email = strstr($user, '@') ? $user : sprintf('%s@%s', $user, $mail_domain); 
    741             } 
    742             $user_name = $user!=$user_email ? $user : ''; 
    743  
    744             // try to resolve the e-mail address from the virtuser table 
    745             if ( 
    746             !empty($CONFIG['virtuser_query']) 
    747             && ($sql_result = $DB->query(preg_replace('/%u/', $user, $CONFIG['virtuser_query']))) 
    748             && ($DB->num_rows()>0) 
    749             ) { 
    750                 while ($sql_arr = $DB->fetch_array($sql_result)) { 
    751                     $_query = "INSERT INTO " . self::get_table_name('identities'); 
    752                     $_query.= " (user_id, del, standard, name, email)"; 
    753                     $_query.= " VALUES (?, 0, 1, ?, ?)"; 
    754                     $DB->query( 
    755                     $_query, 
    756                     $user_id, 
    757                     strip_newlines($user_name), 
    758                     preg_replace('/^@/', $user . '@', $sql_arr[0]) 
    759                     ); 
    760                 } 
    761             } 
    762             else { 
    763                 // also create new identity records 
    764                 $_query = "INSERT INTO " . self::get_table_name('identities'); 
    765                 $_query.= " (user_id, del, standard, name, email)"; 
    766                 $_query.= " VALUES (?, 0, 1, ?, ?)"; 
    767                 $DB->query( 
    768                 $_query, 
    769                 $user_id, 
    770                 strip_newlines($user_name), 
    771                 strip_newlines($user_email) 
    772                 ); 
    773             } 
    774  
    775             // get existing mailboxes 
    776             $a_mailboxes = $IMAP->list_mailboxes(); 
    777         } 
    778         else { 
    779             rcube_error::raise( 
    780             array( 
    781                     'code' => 500, 
    782                     'type' => 'php', 
    783                     'line' => __LINE__, 
    784                     'file' => __FILE__, 
    785                     'message' => "Failed to create new user" 
    786                     ), 
    787                     TRUE, 
    788                     FALSE 
    789                     ); 
    790         } 
    791         return $user_id; 
    792     } 
    793  
    794     /** 
    795      * Load virtuser table in array 
    796      * 
    797      * @return array Virtuser table entries 
    798      */ 
    799     function get_virtualfile() 
    800     { 
    801         $registry = rcube_registry::get_instance(); 
    802         $registry->get_all('config'); 
    803         if (empty($CONFIG['virtuser_file']) || !is_file($CONFIG['virtuser_file'])) { 
    804             return FALSE; 
    805         } 
    806         // read file 
    807         $a_lines = file($CONFIG['virtuser_file']); 
    808         return $a_lines; 
    809     } 
    810  
    811     /** 
    812      * Find matches of the given pattern in virtuser table 
    813      * 
    814      * @param string Regular expression to search for 
    815      * @return array Matching entries 
    816      */ 
    817     static function find_in_virtual($pattern) 
    818     { 
    819         $result  = array(); 
    820         $virtual = self::get_virtualfile(); 
    821         if ($virtual==FALSE) { 
    822             return $result; 
    823         } 
    824         // check each line for matches 
    825         foreach ($virtual as $line) { 
    826             $line = trim($line); 
    827             if (empty($line) || $line{0}=='#') { 
    828                 continue; 
    829             } 
    830             if (eregi($pattern, $line)) { 
    831                 $result[] = $line; 
    832             } 
    833         } 
    834         return $result; 
    835     } 
    836  
    837     /** 
    838      * Resolve username using a virtuser table 
    839      * 
    840      * @param string E-mail address to resolve 
    841      * @return string Resolved IMAP username 
    842      */ 
    843     //TODO check this with rcube_user function 
    844     static function email2user($email) 
    845     { 
    846         $user = $email; 
    847         $r = self::find_in_virtual("^$email"); 
    848  
    849         for ($i=0; $i<count($r); $i++) { 
    850             $data = $r[$i]; 
    851             $arr = preg_split('/\s+/', $data); 
    852             if (count($arr)>0) { 
    853                 $user = trim($arr[count($arr)-1]); 
    854                 break; 
    855             } 
    856         } 
    857         return $user; 
    858     } 
    859  
    860     /** 
    861      * Resolve e-mail address from virtuser table 
    862      * 
    863      * @param string User name 
    864      * @return string Resolved e-mail address 
    865      */ 
    866     //TODO check this with rcube_user function 
    867     static function user2email($user) 
    868     { 
    869         $email = ""; 
    870         $r = self::find_in_virtual("$user$"); 
    871  
    872         for ($i=0; $i<count($r); $i++) { 
    873             $data=$r[$i]; 
    874             $arr = preg_split('/\s+/', $data); 
    875             if (count($arr)>0) { 
    876                 $email = trim($arr[0]); 
    877                 break; 
    878             } 
    879         } 
    880         return $email; 
    881     } 
    882  
    883     /** 
    884      * Write the given user prefs to the user's record 
    885      * 
    886      * @param mixed User prefs to save 
    887      * @return boolean True on success, False on failure 
    888      */ 
    889     //TODO check this with rcube_user function 
    890     static function save_user_prefs($a_user_prefs) 
    891     { 
    892         $registry       = rcube_registry::get_instance(); 
    893         $DB             = $registry->get('DB', 'core'); 
    894         $CONFIG         = $registry->get_all('config'); 
    895         $user_lang = $registry->get('user_lang', 'core'); 
    896  
    897         $_query = "UPDATE " . self::get_table_name('users'); 
    898         $_query.= " SET preferences=?,"; 
    899         $_query.= " language=?"; 
    900         $_query.= " WHERE user_id=?"; 
    901         $DB->query( 
    902         $_query, 
    903         serialize($a_user_prefs), 
    904         $user_lang, 
    905         $_SESSION['user_id'] 
    906         ); 
    907  
    908         if ($DB->affected_rows()) { 
    909             $_SESSION['user_prefs'] = $a_user_prefs; 
    910             foreach ($a_user_prefs as $key => $value) 
    911             $registry->set($key, $value, 'config'); 
    912             return true; 
    913         } 
    914  
    915         return false; 
    916     } 
    917  
    918     /** 
    919692     * Overwrite action variable 
    920693     * 
    921694     * @param string New action value 
    922695     */ 
    923     static function override_action($action) 
    924     { 
     696    public static function override_action($action = null) { 
    925697        $registry = rcube_registry::get_instance(); 
    926698        $OUTPUT   = $registry->get('OUTPUT', 'core'); 
     
    937709     * @return The application URL 
    938710     */ 
    939     static function url($action, $p=array(), $task=null) 
    940     { 
     711    public static function url($action = null, $p = array(), $task = null) { 
    941712        $registry   = rcube_registry::get_instance(); 
    942713        $COMM_PATH  = $registry->get('COMM_PATH', 'core'); 
     
    946717        $base = $COMM_PATH; 
    947718 
    948         if ($task && in_array($task, $MAIN_TASKS)) { 
    949             $base = ereg_replace('_task=[a-z]+', '_task='.$task, $COMM_PATH); 
    950         } 
    951         if (is_array($p)) { 
     719        if (!empty($task) && in_array($task, $MAIN_TASKS)) { 
     720            $base = preg_replace('/_task=[a-z]+/', '_task='.$task, $COMM_PATH); 
     721        } 
     722 
     723        if (is_array($p) && !empty($p)) { 
    952724            foreach ($p as $key => $val) { 
    953725                $qstring .= '&'.urlencode($key).'='.urlencode($val); 
     
    963735     * @return string Encryprted string 
    964736     */ 
    965     static function encrypt_passwd($pass) 
    966     { 
     737    private static function encrypt_passwd($pass) { 
    967738        $cypher = des(self::get_des_key(), $pass, 1, 0, NULL); 
    968739        return base64_encode($cypher); 
     
    975746     * @return string Plain password 
    976747     */ 
    977     static function decrypt_passwd($cypher) 
    978     { 
     748    public static function decrypt_passwd($cypher) { 
    979749        $pass = des(self::get_des_key(), base64_decode($cypher), 0, 0, NULL); 
    980750        return preg_replace('/\x00/', '', $pass); 
     
    986756     * @return string DES encryption key 
    987757     */ 
    988     static function get_des_key() 
    989     { 
     758    private static function get_des_key() { 
    990759        $registry = rcube_registry::get_instance(); 
    991760        $CONFIG   = $registry->get_all('config'); 
     
    994763 
    995764        // make sure the key is exactly 24 chars long 
    996         if ($len<24) { 
     765        if ($len < 24) { 
    997766            $key .= str_repeat('_', 24-$len); 
    998         } 
    999         else if ($len>24) { 
     767        } else if ($len > 24) { 
    1000768            substr($key, 0, 24); 
    1001769        } 
     
    1007775     * Remove temp files older than two days 
    1008776     */ 
    1009     function temp_gc() 
    1010     { 
     777    public function temp_gc() { 
    1011778        $registry = rcube_registry::get_instance(); 
    1012779        $CONFIG   = $registry->get_all('config'); 
     
    1018785        } 
    1019786        while (($fname = readdir($dir)) !== false) { 
    1020             if ($fname{0} == '.') 
    1021             continue; 
    1022  
    1023             if (filemtime($tmp.'/'.$fname) < $expire) 
    1024             @unlink($tmp.'/'.$fname); 
     787            if ($fname{0} == '.') { 
     788                continue; 
     789            } 
     790 
     791            if (filemtime($tmp.'/'.$fname) < $expire) { 
     792                unlink($tmp.'/'.$fname); 
     793            } 
    1025794        } 
    1026795        closedir($dir); 
    1027796    } 
    1028  
    1029797 
    1030798    /** 
     
    1032800     * Remove all expired message cache records 
    1033801     */ 
    1034     static function message_cache_gc() 
    1035     { 
     802    public static function message_cache_gc() { 
    1036803        $registry = rcube_registry::get_instance(); 
    1037804        $DB       = $registry->get('DB', 'core'); 
     
    1050817    } 
    1051818 
    1052  
    1053819    /** 
    1054820     * Check if a specific template exists 
     
    1057823     * @return bool True if template exists 
    1058824     */ 
    1059     static function template_exists($name) 
    1060     { 
     825    public static function template_exists($name) { 
    1061826        $registry = rcube_registry::get_instance(); 
    1062827        $CONFIG   = $registry->get_all('config'); 
    1063         $skin_path = $CONFIG['skin_path']; 
    1064828 
    1065829        // check template file 
    1066         return is_file("$skin_path/templates/$name.html"); 
     830        return is_file($CONFIG['skin_path'].'/templates/'.$name.'html'); 
    1067831    } 
    1068832 
     
    1073837     * @deprecated 
    1074838     */ 
    1075     static function parse_template($name='main', $exit=true) 
    1076     { 
     839    static function parse_template($name='main', $exit=true) { 
    1077840        $registry = rcube_registry::get_instance(); 
    1078841        $OUTPUT   = $registry->get('OUTPUT', 'core'); 
     
    1205968        // use value from post 
    1206969        if (!empty($_POST[$fname])) { 
    1207             $value = $_POST[$fname]; 
     970            $value = self::get_input_value($fname, RCUBE_INPUT_POST); 
    1208971        } 
    1209972        $out = $input->show($value); 
     
    1219982     * @return string Resolved SMTP host 
    1220983     */ 
    1221     static function mail_domain($host) 
    1222     { 
     984    public static function mail_domain($host) { 
    1223985        $registry = rcube_registry::get_instance(); 
    1224986        $CONFIG   = $registry->get_all('config'); 
     
    1229991                $domain = $CONFIG['mail_domain'][$host]; 
    1230992            } 
    1231         } 
    1232         else if (!empty($CONFIG['mail_domain'])) { 
     993        } else if (!empty($CONFIG['mail_domain'])) { 
    1233994            $domain = $CONFIG['mail_domain']; 
    1234995        } 
     
    15881349        if (($attrib['uppercase'] && strtolower($attrib['uppercase']=='first')) || $attrib['ucfirst']) { 
    15891350            return ucfirst($text); 
    1590         } 
    1591         elseif ($attrib['uppercase']) { 
     1351        } elseif ($attrib['uppercase']) { 
    15921352            return strtoupper($text); 
    1593         } 
    1594         elseif ($attrib['lowercase']) { 
     1353        } elseif ($attrib['lowercase']) { 
    15951354            return strtolower($text); 
    15961355        } 
     
    16091368     * @return string   Field value or NULL if not available 
    16101369     */ 
    1611     static function get_input_value($fname, $source, $allow_html=false, $charset=null) 
    1612     { 
     1370    public static function get_input_value($fname, $source, $allow_html=false, $charset=null) { 
    16131371        try { 
    16141372            $registry = rcube_registry::get_instance(); 
    16151373            $output   = $registry->get('OUTPUT', 'core'); 
    1616         } 
    1617         catch (rcube_registry_exception $e) { 
     1374        } catch (rcube_registry_exception $e) { 
    16181375            $output = NULL; 
    16191376        } 
     
    16231380        if ($source == rcube::INPUT_GET && isset($_GET[$fname])) { 
    16241381            $value = $_GET[$fname]; 
    1625         } 
    1626         else if ($source == rcube::INPUT_POST && isset($_POST[$fname])) { 
     1382        } else if ($source == rcube::INPUT_POST && isset($_POST[$fname])) { 
    16271383            $value = $_POST[$fname]; 
    1628         } 
    1629         else if ($source == rcube::INPUT_GPC) { 
     1384        } else if ($source == rcube::INPUT_GPC) { 
    16301385            if (isset($_POST[$fname])) { 
    16311386                $value = $_POST[$fname]; 
    1632             } 
    1633             else if (isset($_GET[$fname])) { 
     1387            } else if (isset($_GET[$fname])) { 
    16341388                $value = $_GET[$fname]; 
    1635             } 
    1636             else if (isset($_COOKIE[$fname])) { 
     1389            } else if (isset($_COOKIE[$fname])) { 
    16371390                $value = $_COOKIE[$fname]; 
    16381391            } 
     
    16401393 
    16411394        // strip slashes if magic_quotes enabled 
    1642         if ((bool)get_magic_quotes_gpc()) 
    1643         $value = stripslashes($value); 
     1395        if ((bool)get_magic_quotes_gpc()) { 
     1396            $value = stripslashes($value); 
     1397        } 
    16441398 
    16451399        // remove HTML tags if not allowed 
     
    16611415     * @return mixed  Header value or null if not available 
    16621416     */ 
    1663     static function get_request_header($name) 
    1664     { 
     1417    public static function get_request_header($name) { 
    16651418        if (function_exists('getallheaders')) { 
    16661419            $hdrs = getallheaders(); 
    16671420            $hdrs = array_change_key_case($hdrs, CASE_UPPER); 
    16681421            $key  = strtoupper($name); 
    1669         } 
    1670         else { 
     1422        } else { 
    16711423            $key  = 'HTTP_' . strtoupper(strtr($name, '-', '_')); 
    16721424            $hdrs = array_change_key_case($_SERVER, CASE_UPPER); 
    16731425        } 
     1426 
    16741427        if (isset($hdrs[$key])) { 
    16751428            return $hdrs[$key]; 
     
    16881441     * @return Converted string 
    16891442     */ 
    1690     static function charset_convert($str, $from, $to=NULL) 
    1691     { 
     1443    public static function charset_convert($str, $from, $to=NULL) { 
     1444        $from = strtoupper($from); 
     1445        $to = ($to == NULL ? strtoupper(RCMAIL_CHARSET) : strtoupper($to)); 
     1446 
     1447        if ($from == $to || $str == '' || empty($from)) { 
     1448            return $str; 
     1449        } 
     1450 
     1451        // convert charset using iconv module 
     1452        if (function_exists('iconv') && $from != 'UTF-7' && $to != 'UTF-7') { 
     1453            $iconv_map = array('KS_C_5601-1987' => 'EUC-KR'); 
     1454            return iconv(($iconv_map[$from] ? $iconv_map[$from] : $from), ($iconv_map[$to] ? $iconv_map[$to] : $to) . "//IGNORE", $str); 
     1455        } 
     1456 
    16921457        $registry = rcube_registry::get_instance(); 
    16931458        $MBSTRING = $registry->get('MBSTRING', 'core'); 
    16941459 
    1695         $from = strtoupper($from); 
    1696         $to = $to==NULL ? strtoupper(RCMAIL_CHARSET) : strtoupper($to); 
    1697  
    1698         if ($from==$to || $str=='' || empty($from)) { 
    1699             return $str; 
    1700         } 
    17011460        // convert charset using mbstring module 
    17021461        if ($MBSTRING) { 
    1703             $to = $to=="UTF-7" ? "UTF7-IMAP" : $to; 
    1704             $from = $from=="UTF-7" ? "UTF7-IMAP": $from; 
     1462            $mb_map = array('UTF-7' => 'UTF7-IMAP', 'KS_C_5601-1987' => 'EUC-KR'); 
    17051463 
    17061464            // return if convert succeeded 
    1707             if (($out = @mb_convert_encoding($str, $to, $from)) != '') { 
     1465            if (($out = mb_convert_encoding($str, ($mb_map[$to] ? $mb_map[$to] : $to), ($mb_map[$from] ? $mb_map[$from] : $from))) != '') { 
    17081466                return $out; 
    17091467            } 
    17101468        } 
    17111469 
    1712         // convert charset using iconv module 
    1713         if (function_exists('iconv') && $from!='UTF-7' && $to!='UTF-7') 
    1714         return iconv($from, $to, $str); 
    1715  
    17161470        $conv = new utf8(); 
    17171471 
    17181472        // convert string to UTF-8 
    1719         if ($from=='UTF-7') { 
     1473        if ($from == 'UTF-7') { 
    17201474            $str = utf7_to_utf8($str); 
    1721         } 
    1722         else if (($from=='ISO-8859-1') && function_exists('utf8_encode')) { 
     1475        } else if (($from == 'ISO-8859-1') && function_exists('utf8_encode')) { 
    17231476            $str = utf8_encode($str); 
    1724         } 
    1725         else if ($from!='UTF-8') { 
     1477        } else if ($from != 'UTF-8') { 
    17261478            $conv->loadCharset($from); 
    17271479            $str = $conv->strToUtf8($str); 
     
    17291481 
    17301482        // encode string for output 
    1731         if ($to=='UTF-7') { 
     1483        if ($to == 'UTF-7') { 
    17321484            return utf8_to_utf7($str); 
    1733         } 
    1734         else if ($to=='ISO-8859-1' && function_exists('utf8_decode')) { 
     1485        } else if ($to == 'ISO-8859-1' && function_exists('utf8_decode')) { 
    17351486            return utf8_decode($str); 
    1736         } 
    1737         else if ($to!='UTF-8') { 
     1487        } else if ($to!='UTF-8') { 
    17381488            $conv->loadCharset($to); 
    17391489            return $conv->utf8ToStr($str); 
     
    17541504     * @return The quoted string 
    17551505     */ 
    1756     static function rep_specialchars_output($str, $enctype='', $mode='', $newlines=TRUE) 
    1757     { 
     1506    public static function rep_specialchars_output($str, $enctype='', $mode='', $newlines=TRUE) { 
    17581507        static $html_encode_arr, $js_rep_table, $xml_rep_table; 
    17591508 
     
    17661515        // encode for plaintext 
    17671516        if ($enctype == 'text') { 
    1768             return str_replace( 
    1769                 "\r\n", 
    1770                 "\n", 
    1771             $mode=='remove' ? strip_tags($str) : $str 
    1772             ); 
     1517            return str_replace("\r\n", "\n", ($mode =='remove' ? strip_tags($str) : $str)); 
    17731518        } 
    17741519 
     
    17931538                unset($encode_arr['>']); 
    17941539                unset($encode_arr['&']); 
    1795             } 
    1796             else if ($mode == 'remove') { 
     1540            } else if ($mode == 'remove') { 
    17971541                $str = strip_tags($str); 
    17981542            } 
    17991543            // avoid douple quotation of & 
    1800             $out = preg_replace( 
    1801                 '/&amp;([a-z]{2,5}|#[0-9]{2,4});/', 
    1802                 '&\\1;', 
    1803             strtr($str, $encode_arr) 
    1804             ); 
     1544            $out = preg_replace('/&amp;([a-z]{2,5}|#[0-9]{2,4});/', '&\\1;', strtr($str, $encode_arr)); 
    18051545            return $newlines ? nl2br($out) : $out; 
    18061546        } 
     
    18151555            $xml_rep_table['&'] = '&amp;'; 
    18161556 
    1817             for ($c=160; $c<256; $c++)  // can be increased to support more charsets 
    1818             { 
     1557            // can be increased to support more charsets 
     1558            for ($c=160; $c<256; $c++)   { 
    18191559                $xml_rep_table[Chr($c)] = "&#$c;"; 
    18201560 
     
    18361576                $str = self::charset_convert($str, RCMAIL_CHARSET, $out_charset); 
    18371577            } 
    1838             return preg_replace( 
    1839             array("/\r?\n/", "/\r/"), 
    1840             array('\n', '\n'), 
    1841             addslashes(strtr($str, $js_rep_table)) 
    1842             ); 
     1578            return preg_replace(array("/\r?\n/", "/\r/"), array('\n', '\n'), addslashes(strtr($str, $js_rep_table))); 
    18431579        } 
    18441580 
     
    18461582        return $str; 
    18471583    } 
    1848  
    18491584 
    18501585    /** 
     
    18571592     * @deprecated 
    18581593     */ 
    1859     static function create_attrib_string($attrib, $allowed=array('id', 'class', 'style')) 
    1860     { 
     1594    public static function create_attrib_string($attrib, $allowed=array('id', 'class', 'style')) { 
    18611595        return html::attrib_string($attrib, $allowed); 
    18621596    } 
     
    18691603     * @return array Key-value pairs of parsed attributes 
    18701604     */ 
    1871     function parse_attrib_string($str) 
    1872     { 
     1605    public function parse_attrib_string($str = '') { 
    18731606        $attrib = array(); 
    1874         preg_match_all( 
    1875             '/\s*([-_a-z]+)=(["\'])([^"]+)\2/Ui', 
    1876         stripslashes($str), 
    1877         $regs, 
    1878         PREG_SET_ORDER 
    1879         ); 
     1607        preg_match_all('/\s*([-_a-z]+)=(["\'])([^"]+)\2/Ui', stripslashes($str), $regs, PREG_SET_ORDER); 
    18801608 
    18811609        // convert attributes to an associative array (name => value) 
     
    18911619    /****** debugging functions ********/ 
    18921620 
    1893  
    18941621    /** 
    18951622     * tfk_debug 
     
    18981625     * @return void 
    18991626     */ 
    1900     static function tfk_debug($str) 
    1901     { 
    1902         $str = "\n\n" . @date('Y-m-d H:i:s') . "\n" . $str; 
    1903         $fp = @fopen(dirname(__FILE__) . '/../../logs/debug.tfk', 'a'); 
     1627    public static function tfk_debug($str = '') { 
     1628        $str = "\n\n" . date('Y-m-d H:i:s') . "\n" . $str; 
     1629        $fp = fopen(dirname(__FILE__) . '/../../logs/debug.tfk', 'a'); 
    19041630        if ($fp !== false) { 
    1905             @fwrite($fp, $str); 
    1906             @fclose($fp); 
    1907         } 
    1908         else { 
     1631            fwrite($fp, $str); 
     1632            fclose($fp); 
     1633        } else { 
    19091634            die('Could not open logs/debug.tfk.'); 
    19101635        } 
    19111636    } 
    19121637 
    1913  
    19141638    /** 
    19151639     * Print or write debug messages 
     
    19171641     * @param mixed Debug message or data 
    19181642     */ 
    1919     function console($msg) 
    1920     { 
     1643    public function console($msg = '') { 
    19211644        $registry = rcube_registry::get_instance(); 
    19221645        $CONFIG   = $registry->get_all('config'); 
     
    19261649        if (!($CONFIG['debug_level'] & 4)) { 
    19271650            write_log('console', $msg); 
    1928         } 
    1929         elseif ($GLOBALS['REMOTE_REQUEST']) { 
     1651        } elseif ($GLOBALS['REMOTE_REQUEST']) { 
    19301652            echo "/*\n $msg \n*/\n"; 
    1931         } 
    1932         else { 
     1653        } else { 
    19331654            echo '<div style="background:#eee; border:1px solid #ccc; '; 
    19341655            echo 'margin-bottom:3px; padding:6px"><pre>'; 
     
    19381659    } 
    19391660 
    1940  
    19411661    /** 
    19421662     * Append a line to a logfile in the logs directory. 
     
    19461666     * @param string Line to append 
    19471667     */ 
    1948     function write_log($name, $line) 
    1949     { 
     1668    private function write_log($name, $line) { 
    19501669        $registry = rcube_registry::get_instance(); 
    19511670        $log_dir  = $registry->get('log_dir', 'config'); 
     
    19541673            $line = var_export($line, true); 
    19551674        } 
    1956         $log_entry = sprintf( 
    1957             "[%s]: %s\n", 
    1958         date("d-M-Y H:i:s O", mktime()), 
    1959         $line 
    1960         ); 
     1675        $log_entry = sprintf("[%s]: %s\n", date("d-M-Y H:i:s O", mktime()), $line); 
    19611676 
    19621677        if (empty($log_dir)) { 
     
    19641679        } 
    19651680        // try to open specific log file for writing 
    1966         if ($fp = @fopen($log_dir . '/' . $name, 'a')) { 
     1681        if ($fp = fopen($log_dir . '/' . $name, 'a')) { 
    19671682            fwrite($fp, $log_entry); 
    19681683            fclose($fp); 
     
    19711686 
    19721687 
    1973     static function timer() 
    1974     { 
    1975         list($usec, $sec) = explode(" ", microtime()); 
     1688    public static function timer() { 
     1689        list($usec, $sec) = explode(' ', microtime()); 
    19761690        return ((float)$usec + (float)$sec); 
    19771691    } 
    19781692 
    1979     static function print_time($timer, $label='Timer') 
    1980     { 
     1693    /** 
     1694     * not used 
     1695     */ 
     1696    private static function print_time($timer, $label='Timer') { 
    19811697        static $print_count = 0; 
    19821698 
     
    19941710} 
    19951711 
     1712?> 
  • branches/devel-vnext/program/include/rcube_user.php

    r957 r1015  
    1919 $Id: rcube_user.inc 933 2007-11-29 14:17:32Z thomasb $ 
    2020 
    21  */ 
    22  
     21*/ 
    2322 
    2423/** 
     
    2827 * @author     Thomas Bruederli <roundcube@gmail.com> 
    2928 */ 
    30 class rcube_user 
    31 { 
    32     var $ID = null; 
    33     var $data = null; 
     29class rcube_user { 
     30    public $ID = null; 
     31    public $data = null; 
    3432 
    3533    /** 
     
    3836     * @param object DB Database connection 
    3937     */ 
    40     function __construct($id = null, $sql_arr = null) 
    41     { 
    42         global $DB; 
    43  
    44         if ($id && !$sql_arr) 
    45         { 
    46             $sql_result = $DB->query("SELECT * FROM ".get_table_name('users')." WHERE  user_id=?", $id); 
     38    public function __construct($id = null, $sql_arr = null) { 
     39 
     40        if (!empty($id) && empty($sql_arr)) { 
     41            $registry  = rcube_registry::get_instance(); 
     42            $DB        = $registry->get('DB', 'core'); 
     43             
     44            $sql_result = $DB->query('SELECT * FROM '.rcube::get_table_name('users').' WHERE  user_id = ?', $id); 
    4745            $sql_arr = $DB->fetch_assoc($sql_result); 
    4846        } 
    4947 
    50         if (!empty($sql_arr)) 
    51         { 
     48        if (!empty($sql_arr)) { 
    5249            $this->ID = $sql_arr['user_id']; 
    5350            $this->data = $sql_arr; 
     
    5653 
    5754    /** 
    58      * PHP 4 object constructor 
    59      * 
    60      * @see  rcube_user::__construct 
    61      */ 
    62     function rcube_user($id = null, $sql_arr = null) 
    63     { 
    64         $this->__construct($id, $sql_arr); 
    65     } 
    66  
    67     /** 
    6855     * Build a user name string (as e-mail address) 
    6956     * 
    7057     * @return string Full user name 
    7158     */ 
    72     function get_username() 
    73     { 
    74         return $this->data['username'] ? $this->data['username'] . (!strpos($this->data['username'], '@') ? '@'.$this->data['mail_host'] : '') : false; 
    75     } 
    76  
     59    public static function get_username() { 
     60        return self::$data['username'] ? self::$data['username'] . (!strpos(self::$data['username'], '@') ? '@'.self::$data['mail_host'] : '') : false; 
     61    } 
    7762 
    7863    /** 
     
    8166     * @return array Hash array with prefs 
    8267     */ 
    83     function get_prefs() 
    84     { 
    85         if ($this->ID && $this->data['preferences']) 
    86         return unserialize($this->data['preferences']); 
    87         else 
    88         return array(); 
     68    public static function get_prefs() { 
     69        if (self::$ID && self::$data['preferences']) { 
     70            return unserialize(self::$data['preferences']); 
     71        } else { 
     72            return array(); 
     73        } 
    8974    } 
    9075 
     
    9681     * @return boolean True on success, False on failure 
    9782     */ 
    98     function save_prefs($a_user_prefs) 
    99     { 
    100         global $DB, $CONFIG, $sess_user_lang; 
    101  
    102         if (!$this->ID) 
    103         return false; 
    104  
    105         // merge (partial) prefs array with existing settings 
    106         $a_user_prefs += (array)$this->get_prefs(); 
    107  
    108         $DB->query( 
    109       "UPDATE ".get_table_name('users')." 
    110        SET    preferences=?, 
    111               language=? 
    112        WHERE  user_id=?", 
    113         serialize($a_user_prefs), 
    114         $sess_user_lang, 
    115         $this->ID); 
    116  
    117         if ($DB->affected_rows()) 
    118         { 
    119             $CONFIG = array_merge($CONFIG, $a_user_prefs); 
     83    public static function save_prefs($a_user_prefs) { 
     84        $registry  = rcube_registry::get_instance(); 
     85        $DB        = $registry->get('DB', 'core'); 
     86        $CONFIG    = $registry->get_all('config'); 
     87        $user_lang = $registry->get('user_lang', 'core'); 
     88 
     89        $_query = 'UPDATE '.rcube::get_table_name('users'); 
     90        $_query.= ' SET preferences=?,'; 
     91        $_query.= ' language=?'; 
     92        $_query.= ' WHERE user_id=?'; 
     93        $DB->query($_query, serialize($a_user_prefs), $user_lang, $_SESSION['user_id']); 
     94 
     95        if ($DB->affected_rows()) { 
     96            $_SESSION['user_prefs'] = $a_user_prefs; 
     97            foreach ($a_user_prefs as $key => $value) { 
     98                $registry->set($key, $value, 'config'); 
     99            } 
    120100            return true; 
    121101        } 
    122  
    123102        return false; 
    124103    } 
     
    130109     * @return array Hash array with all cols of the 
    131110     */ 
    132     function get_identity($id = null) 
    133     { 
    134         global $DB; 
    135  
    136         $sql_result = $this->list_identities($id ? sprintf('AND identity_id=%d', $id) : ''); 
     111    public static function get_identity($identity_id = null) { 
     112        $registry  = rcube_registry::get_instance(); 
     113        $DB        = $registry->get('DB', 'core'); 
     114 
     115        $sql_result = self::list_identities($identity_id ? sprintf('AND identity_id=%d', $identity_id) : ''); 
    137116        return $DB->fetch_assoc($sql_result); 
    138117    } 
    139118 
    140  
    141119    /** 
    142120     * Return a list of all identities linked with this user 
     
    144122     * @return array List of identities 
    145123     */ 
    146     function list_identities($sql_add = '') 
    147     { 
    148         global $DB; 
    149  
     124    public static function list_identities($sql_add = '') { 
     125        $registry  = rcube_registry::get_instance(); 
     126        $DB        = $registry->get('DB', 'core'); 
     127 
     128        $_query = 'SELECT * FROM '.rcube::get_table_name('identities'); 
     129        $_query.= ' WHERE del <> 1'; 
     130        $_query.= ' AND user_id=?'; 
     131        $_query.= (!empty($sql_add) ? ' '.$sql_add : ''); 
     132        $_query.= ' ORDER BY '.$DB->quoteIdentifier('standard').' DESC, name ASC'; 
    150133        // get contacts from DB 
    151         $sql_result = $DB->query( 
    152       "SELECT * FROM ".get_table_name('identities')." 
    153         WHERE  del<>1 
    154         AND    user_id=? 
    155         $sql_add 
    156         ORDER BY ".$DB->quoteIdentifier('standard')." DESC, name ASC", 
    157         $this->ID); 
    158  
    159         return $sql_result; 
    160     } 
     134        return $DB->query($_query, self::$ID); 
     135    } 
     136 
    161137    /** 
    162138     * Update a specific identity record 
     
    166142     * @return boolean True if saved successfully, false if nothing changed 
    167143     */ 
    168     function update_identity($iid, $data) 
    169     { 
    170         global $DB; 
    171  
    172         if (!$this->ID) 
    173         return false; 
     144    public static function update_identity($identity_id = null, $data = array()) { 
     145        if (empty(self::$ID) || empty($identity_id) || empty($data) || !is_array($data)) { 
     146            return false; 
     147        } 
     148 
     149        $registry  = rcube_registry::get_instance(); 
     150        $DB        = $registry->get('DB', 'core'); 
    174151 
    175152        $write_sql = array(); 
    176153 
    177         foreach ((array)$data as $col => $value) 
    178         { 
    179             $write_sql[] = sprintf("%s=%s", 
    180             $DB->quoteIdentifier($col), 
    181             $DB->quote($value)); 
    182         } 
    183  
    184         $DB->query( 
    185       "UPDATE ".get_table_name('identities')." 
    186        SET ".join(', ', $write_sql)." 
    187        WHERE  identity_id=? 
    188        AND    user_id=? 
    189        AND    del<>1", 
    190         $iid, 
    191         $this->ID); 
    192  
     154        foreach ((array)$data as $col => $value) { 
     155            $write_sql[] = sprintf("%s=%s", $DB->quoteIdentifier($col), $DB->quote($value)); 
     156        } 
     157 
     158        $_query = 'UPDATE '.rcube::get_table_name('identities'); 
     159        $_query.= ' SET '.implode(', ', $write_sql); 
     160        $_query.= ' WHERE identity_id=?'; 
     161        $_query.= ' AND user_id=?'; 
     162        $_query.= ' AND del <> 1'; 
     163        $DB->query($_query, $identity_id, self::$ID); 
    193164        return $DB->affected_rows(); 
    194165    } 
     
    201172     * @return int  The inserted identity ID or false on error 
    202173     */ 
    203     function insert_identity($data) 
    204     { 
    205         global $DB; 
    206  
    207         if (!$this->ID) 
    208         return false; 
     174    public static function insert_identity($data = array()) { 
     175        if (!self::$ID || empty($data) || !is_array($data)) { 
     176            return false; 
     177        } 
     178 
     179        $registry  = rcube_registry::get_instance(); 
     180        $DB        = $registry->get('DB', 'core'); 
    209181 
    210182        $insert_cols = $insert_values = array(); 
    211         foreach ((array)$data as $col => $value) 
    212         { 
     183        foreach ((array)$data as $col => $value) { 
    213184            $insert_cols[] = $DB->quoteIdentifier($col); 
    214185            $insert_values[] = $DB->quote($value); 
    215186        } 
    216  
    217         $DB->query( 
    218       "INSERT INTO ".get_table_name('identities')." 
    219         (user_id, ".join(', ', $insert_cols).") 
    220        VALUES (?, ".join(', ', $insert_values).")", 
    221         $this->ID); 
    222  
    223         return $DB->insert_id(get_sequence_name('identities')); 
     187        $_query = 'INSERT INTO '.rcube::get_table_name('identities'); 
     188        $_query.= ' (user_id, '.implode(', ', $insert_cols).')'; 
     189        $_query.= ' VALUES (?, '.implode(', ', $insert_values).')'; 
     190 
     191        $DB->query($_query, self::$ID); 
     192        return $DB->insert_id(rcube::get_sequence_name('identities')); 
    224193    } 
    225194 
     
    230199     * @return boolean True if deleted successfully, false if nothing changed 
    231200     */ 
    232     function delete_identity($iid) 
    233     { 
    234         global $DB; 
    235  
    236         if (!$this->ID) 
    237         return false; 
    238  
    239         $DB->query( 
    240       "UPDATE ".get_table_name('identities')." 
    241        SET    del=1 
    242        WHERE  user_id=? 
    243        AND    identity_id=?", 
    244         $this->ID, 
    245         $iid); 
    246  
     201    public static function delete_identity($identity_id = null) { 
     202        if (!self::$ID || empty($identity_id)) { 
     203            return false; 
     204        } 
     205 
     206        $registry  = rcube_registry::get_instance(); 
     207        $DB        = $registry->get('DB', 'core'); 
     208 
     209        $_query = 'UPDATE '.rcube::get_table_name('identities'); 
     210        $_query.= ' SET del = 1'; 
     211        $_query.= ' WHERE user_id = ?'; 
     212        $_query.= ' AND identity_id = ?'; 
     213 
     214        $DB->query($_query, self::$ID, $identity_id); 
    247215        return $DB->affected_rows(); 
    248216    } 
    249217 
    250  
    251218    /** 
    252219     * Make this identity the default one for this user 
     
    254221     * @param int The identity ID 
    255222     */ 
    256     function set_default($iid) 
    257     { 
    258         global $DB; 
    259  
    260         if ($this->ID && $iid) 
    261         { 
    262             $DB->query( 
    263         "UPDATE ".get_table_name('identities')." 
    264          SET ".$DB->quoteIdentifier('standard')."='0' 
    265          WHERE  user_id=? 
    266          AND    identity_id<>? 
    267          AND    del<>1", 
    268             $this->ID, 
    269             $iid); 
     223    public static function set_default($identity_id = null) { 
     224 
     225        if (!empty(self::$ID) && !empty($identity_id)) { 
     226            $registry  = rcube_registry::get_instance(); 
     227            $DB        = $registry->get('DB', 'core'); 
     228 
     229            $_query = 'UPDATE '.rcube::get_table_name('identities'); 
     230            $_query.= ' SET '.$DB->quoteIdentifier('standard').'="0"'; 
     231            $_query.= ' WHERE user_id = ?'; 
     232            $_query.= ' AND identity_id <> ?'; 
     233            $_query.= ' AND del <> 1'; 
     234            $DB->query($_query, self::$ID, $identity_id); 
    270235        } 
    271236    } 
     
    275240     * Update user's last_login timestamp 
    276241     */ 
    277     function touch() 
    278     { 
    279         global $DB; 
    280  
    281         if ($this->ID) 
    282         { 
    283             $DB->query( 
    284         "UPDATE ".get_table_name('users')." 
    285          SET    last_login=".$DB->now()." 
    286          WHERE  user_id=?", 
    287             $this->ID); 
     242    public static function touch() { 
     243        if (!empty(self::$ID)) { 
     244            $registry  = rcube_registry::get_instance(); 
     245            $DB        = $registry->get('DB', 'core'); 
     246 
     247            $_query = 'UPDATE '.rcube::get_table_name('users'); 
     248            $_query.= ' SET last_login = '.$DB->now(); 
     249            $_query.= ' WHERE user_id = ?'; 
     250             
     251            $DB->query($_query, self::$ID); 
    288252        } 
    289253    } 
     
    291255     * Clear the saved object state 
    292256     */ 
    293     function reset() 
    294     { 
    295         $this->ID = null; 
    296         $this->data = null; 
    297     } 
    298  
     257    public static function reset() { 
     258        self::$ID = null; 
     259        self::$data = null; 
     260    } 
    299261 
    300262    /** 
     
    306268     * @static 
    307269     */ 
    308     function query($user, $host) 
    309     { 
    310         global $DB; 
     270    public static function query($user = null, $host = null) { 
     271        $registry  = rcube_registry::get_instance(); 
     272        $DB        = $registry->get('DB', 'core'); 
    311273 
    312274        // query if user already registered 
    313         $sql_result = $DB->query( 
    314       "SELECT * FROM ".get_table_name('users')." 
    315        WHERE  mail_host=? AND (username=? OR alias=?)", 
    316         $host, 
    317         $user, 
    318         $user); 
     275        $_query = 'SELECT * FROM '.rcube::get_table_name('users'); 
     276        $_query.= ' WHERE mail_host = ?'; 
     277        $_query.= ' AND (username = ? OR alias = ?)'; 
     278        $sql_result = $DB->query($_query, $host, $user, $user); 
    319279 
    320280        // user already registered -> overwrite username 
    321         if ($sql_arr = $DB->fetch_assoc($sql_result)) 
    322         return new rcube_user($sql_arr['user_id'], $sql_arr); 
    323         else 
    324         return false; 
    325     } 
     281        if ($sql_arr = $DB->fetch_assoc($sql_result)) { 
     282            return new rcube_user($sql_arr['user_id'], $sql_arr); 
     283        } else { 
     284            return false; 
     285        } 
     286    } 
     287 
    326288    /** 
    327289     * Create a new user record and return a rcube_user instance 
     
    332294     * @static 
    333295     */ 
    334     function create($user, $host) 
    335     { 
    336         global $DB, $CONFIG; 
     296    public static function create($user, $host) { 
     297        $registry = rcube_registry::get_instance(); 
     298        $DB       = $registry->get('DB', 'core'); 
     299        $CONFIG   = $registry->get_all('config'); 
     300        $IMAP     = $registry->get('IMAP', 'core'); 
    337301 
    338302        $user_email = ''; 
    339303 
    340304        // try to resolve user in virtusertable 
    341         if (!empty($CONFIG['virtuser_file']) && !strpos($user, '@')) 
    342         $user_email = self::user2email($user); 
    343  
    344         $DB->query( 
    345       "INSERT INTO ".get_table_name('users')." 
    346         (created, last_login, username, mail_host, alias, language) 
    347        VALUES (".$DB->now().", ".$DB->now().", ?, ?, ?, ?)", 
    348         strip_newlines($user), 
    349         strip_newlines($host), 
    350         strip_newlines($user_email), 
    351         $_SESSION['user_lang']); 
    352  
    353         if ($user_id = $DB->insert_id(get_sequence_name('users'))) 
    354         { 
    355             $mail_domain = rcmail_mail_domain($host); 
    356  
    357             if ($user_email=='') 
    358             $user_email = strpos($user, '@') ? $user : sprintf('%s@%s', $user, $mail_domain); 
    359  
    360             $user_name = $user != $user_email ? $user : ''; 
     305        if (!empty($CONFIG['virtuser_file']) && strstr($user, '@') === FALSE) { 
     306            $user_email = self::user2email($user); 
     307        } else { // failover 
     308            $user_email = $user; 
     309        } 
     310 
     311        $_query = 'INSERT INTO '.rcube::get_table_name('users'); 
     312        $_query.= ' (created, last_login, username, mail_host, alias, language)'; 
     313        $_query.= ' VALUES ('.$DB->now().', '.$DB->now().', %s, %s, %s, %s)'; 
     314 
     315        $_query = sprintf( 
     316        $_query, 
     317        $DB->quote(strip_newlines($user)), 
     318        $DB->quote(strip_newlines($host)), 
     319        $DB->quote(strip_newlines($user_email)), 
     320        $DB->quote($_SESSION['user_lang']) 
     321        ); 
     322        rcube::tfk_debug($_query); 
     323        // query 
     324        $DB->query($_query); 
     325 
     326        if ($user_id = $DB->insert_id(rcube::get_sequence_name('users'))) { 
     327            $mail_domain = rcube::mail_domain($host); 
     328 
     329            if ($user_email=='') { 
     330                $user_email = strstr($user, '@') ? $user : sprintf('%s@%s', $user, $mail_domain); 
     331            } 
     332            $user_name = ($user != $user_email) ? $user : ''; 
    361333 
    362334            // try to resolve the e-mail address from the virtuser table 
    363             if (!empty($CONFIG['virtuser_query']) && 
    364             ($sql_result = $DB->query(preg_replace('/%u/', $DB->escapeSimple($user), $CONFIG['virtuser_query']))) && 
    365             ($DB->num_rows()>0)) 
    366             { 
    367                 while ($sql_arr = $DB->fetch_array($sql_result)) 
    368                 { 
     335            // TODO there was $DB->escapeSimple($user) in trunk, don't know what to use instead 
     336            if ( 
     337            !empty($CONFIG['virtuser_query']) 
     338            && ($sql_result = $DB->query(preg_replace('/%u/', $user, $CONFIG['virtuser_query']))) 
     339            && ($DB->num_rows() > 0) 
     340            ) { 
     341                while ($sql_arr = $DB->fetch_array($sql_result)) { 
     342                    $_query = 'INSERT INTO '.rcube::get_table_name('identities'); 
     343                    $_query.= ' (user_id, del, standard, name, email)'; 
     344                    $_query.= ' VALUES (?, 0, 1, ?, ?)'; 
    369345                    $DB->query( 
    370             "INSERT INTO ".get_table_name('identities')." 
    371               (user_id, del, standard, name, email) 
    372              VALUES (?, 0, 1, ?, ?)", 
     346                    $_query, 
    373347                    $user_id, 
    374348                    strip_newlines($user_name), 
    375                     preg_replace('/^@/', $user . '@', $sql_arr[0])); 
     349                    preg_replace('/^@/', $user . '@', $sql_arr[0]) 
     350                    ); 
    376351                } 
    377             } 
    378             else 
    379             { 
     352            } else { 
    380353                // also create new identity records 
     354                $_query = 'INSERT INTO '.rcube::get_table_name('identities'); 
     355                $_query.= ' (user_id, del, standard, name, email)'; 
     356                $_query.= ' VALUES (?, 0, 1, ?, ?)'; 
    381357                $DB->query( 
    382           "INSERT INTO ".get_table_name('identities')." 
    383             (user_id, del, standard, name, email) 
    384            VALUES (?, 0, 1, ?, ?)", 
     358                $_query, 
    385359                $user_id, 
    386360                strip_newlines($user_name), 
    387                 strip_newlines($user_email)); 
    388             } 
    389         } 
    390         else 
    391         { 
     361                strip_newlines($user_email) 
     362                ); 
     363            } 
     364 
     365            // get existing mailboxes 
     366            $a_mailboxes = $IMAP->list_mailboxes(); 
     367        } else { 
    392368            rcube_error::raise( 
    393                     array( 
    394                         'code' => 500, 
    395                         'type' => 'php', 
    396                         'line' => __LINE__, 
    397                         'file' => __FILE__, 
    398                         'message' => 'Failed to create new user'), 
    399                     true, 
    400                     false); 
    401         } 
    402  
    403         return $user_id ? new rcube_user($user_id) : false; 
    404     } 
    405  
     369            array( 
     370                    'code' => 500, 
     371                    'type' => 'php', 
     372                    'line' => __LINE__, 
     373                    'file' => __FILE__, 
     374                    'message' => "Failed to create new user" 
     375                    ), 
     376                    TRUE, 
     377                    FALSE 
     378                    ); 
     379        } 
     380        return $user_id; 
     381    } 
     382 
     383    /** 
     384     * Load virtuser table in array 
     385     * 
     386     * @return array Virtuser table entries 
     387     */ 
     388    public static function get_virtualfile() { 
     389        $registry = rcube_registry::get_instance(); 
     390        $CONFIG   = $registry->get_all('config'); 
     391 
     392        if (empty($CONFIG['virtuser_file']) || !is_file($CONFIG['virtuser_file'])) { 
     393            return false; 
     394        } 
     395        // read file 
     396        $a_lines = file($CONFIG['virtuser_file']); 
     397        return $a_lines; 
     398    } 
     399 
     400    /** 
     401     * Find matches of the given pattern in virtuser table 
     402     * 
     403     * @param string Regular expression to search for 
     404     * @return array Matching entries 
     405     */ 
     406    public static function find_in_virtual($pattern) { 
     407        $result  = array(); 
     408        $virtual = self::get_virtualfile(); 
     409        if ($virtual == false) { 
     410            return $result; 
     411        } 
     412        // check each line for matches 
     413        foreach ($virtual as $line) { 
     414            $line = trim($line); 
     415            if (empty($line) || $line{0}=='#') { 
     416                continue; 
     417            } 
     418            if (eregi($pattern, $line)) { 
     419                $result[] = $line; 
     420            } 
     421        } 
     422        return $result; 
     423    } 
    406424 
    407425    /** 
     
    410428     * @param string E-mail address to resolve 
    411429     * @return string Resolved IMAP username 
    412      * @static 
    413      */ 
    414     function email2user($email) 
    415     { 
     430     */ 
     431    public static function email2user($email = null) { 
    416432        $user = $email; 
    417         $r = rcmail_findinvirtual("^$email"); 
    418  
    419         for ($i=0; $i<count($r); $i++) 
    420         { 
     433        $r = self::find_in_virtual("^$email"); 
     434 
     435        for ($i=0, $size = count($r); $i < $size; $i++) { 
    421436            $data = $r[$i]; 
    422437            $arr = preg_split('/\s+/', $data); 
    423             if (count($arr) > 0) 
    424             { 
     438            if (count($arr) > 0) { 
    425439                $user = trim($arr[count($arr)-1]); 
    426440                break; 
    427441            } 
    428442        } 
    429  
    430443        return $user; 
    431444    } 
    432  
    433445 
    434446    /** 
     
    437449     * @param string User name 
    438450     * @return string Resolved e-mail address 
    439      * @static 
    440      */ 
    441     function user2email($user) 
    442     { 
    443         $email = ""; 
    444         $r = rcmail_findinvirtual("$user$"); 
    445  
    446         for ($i=0; $i<count($r); $i++) 
    447         { 
    448             $data = $r[$i]; 
     451     */ 
     452    public static function user2email($user) { 
     453        $email = ''; 
     454        $r = self::find_in_virtual("$user$"); 
     455 
     456        for ($i=0, $size = count($r); $i < $size; $i++) { 
     457            $data=$r[$i]; 
    449458            $arr = preg_split('/\s+/', $data); 
    450             if (count($arr) > 0) 
    451             { 
     459            if (count($arr) > 0) { 
    452460                $email = trim($arr[0]); 
    453461                break; 
    454462            } 
    455463        } 
    456  
    457464        return $email; 
    458465    } 
Note: See TracChangeset for help on using the changeset viewer.