Changeset 1015 in subversion
- Timestamp:
- Feb 4, 2008 1:16:46 PM (5 years ago)
- Location:
- branches/devel-vnext/program/include
- Files:
-
- 3 edited
-
globals.php (modified) (10 diffs)
-
rcube.php (modified) (47 diffs)
-
rcube_user.php (modified) (18 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/devel-vnext/program/include/globals.php
r979 r1015 27 27 28 28 /** 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 31 33 * @see rcube::rep_specialchars_output() 32 34 */ 33 function Q($str , $mode = 'strict', $newlines = TRUE) {35 function Q($str = '', $mode = 'strict', $newlines = TRUE) { 34 36 return rcube::rep_specialchars_output($str, 'html', $mode, $newlines); 35 37 } 36 38 37 39 /** 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 40 44 * @see rcube::rep_specialchars_output() 41 45 */ 42 function JQ($str ) {46 function JQ($str = '') { 43 47 return rcube::rep_specialchars_output($str, 'js'); 44 48 } 45 49 50 /** 51 * Remove all non-ascii and non-word chars 52 * except . and - 53 */ 54 function asciiwords($str = '') { 55 return preg_replace('/[^a-z0-9.-_]/i', '', $str); 56 } 46 57 47 58 /** … … 51 62 * @return string Dequoted string 52 63 */ 53 function strip_quotes($str ) {64 function strip_quotes($str = '') { 54 65 return preg_replace('/[\'"]/', '', $str); 55 66 } … … 61 72 * @return string Stripped string 62 73 */ 63 function strip_newlines($str ) {74 function strip_newlines($str = '') { 64 75 return preg_replace('/[\r\n]/', '', $str); 65 76 } 66 67 77 68 78 /** … … 342 352 } 343 353 return strtolower($str); 344 }345 354 } 346 355 … … 374 383 return strrpos($haystack, $needle, $offset); 375 384 } 376 377 385 378 386 /** … … 393 401 $str = rc_substr($str, 0, $first_part_length) . $place_holder . rc_substr($str, $second_starting_location, $length); 394 402 } 395 396 403 return $str; 397 404 } 398 399 405 400 406 /** … … 405 411 } 406 412 407 408 413 /** 409 414 * Remove slash at the end of the string … … 412 417 return preg_replace('/\/$/', '', $str); 413 418 } 414 415 419 416 420 /** … … 435 439 return true; 436 440 } 437 438 441 439 442 /** … … 491 494 return $index; 492 495 } 496 497 ?> -
branches/devel-vnext/program/include/rcube.php
r981 r1015 413 413 } 414 414 415 416 415 /** 417 416 * Init output object for GUI and add common scripts. … … 445 444 } 446 445 447 448 446 /** 449 447 * Create an output object for JSON responses … … 459 457 self::set_locale($registry->get('user_lang', 'core')); 460 458 } 461 462 459 463 460 /** … … 489 486 $OUTPUT->set_charset($charset); 490 487 } 491 492 488 493 489 /** … … 524 520 } 525 521 } 526 527 522 return $host; 528 523 } … … 544 539 $IMAP = $registry->get('IMAP', 'core'); 545 540 $DB = $registry->get('DB', 'core'); 546 $USER = $registry->get('USER', 'core');541 $USER = $registry->get('USER', 'core'); 547 542 $user_lang = $registry->get('user_lang', 'core'); 548 543 … … 695 690 696 691 /** 697 * Create new entry in users and identities table698 *699 * @param string User name700 * @param string IMAP host701 * @return mixed New user ID or False on failure702 */703 //TODO check this with rcube_user function704 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 virtusertable714 if (!empty($CONFIG['virtuser_file']) && strstr($user, '@') === FALSE) {715 $user_email = self::user2email($user);716 }717 else { // failover718 $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 table745 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 records764 $_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 mailboxes776 $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 FALSE789 );790 }791 return $user_id;792 }793 794 /**795 * Load virtuser table in array796 *797 * @return array Virtuser table entries798 */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 file807 $a_lines = file($CONFIG['virtuser_file']);808 return $a_lines;809 }810 811 /**812 * Find matches of the given pattern in virtuser table813 *814 * @param string Regular expression to search for815 * @return array Matching entries816 */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 matches825 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 table839 *840 * @param string E-mail address to resolve841 * @return string Resolved IMAP username842 */843 //TODO check this with rcube_user function844 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 table862 *863 * @param string User name864 * @return string Resolved e-mail address865 */866 //TODO check this with rcube_user function867 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 record885 *886 * @param mixed User prefs to save887 * @return boolean True on success, False on failure888 */889 //TODO check this with rcube_user function890 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 /**919 692 * Overwrite action variable 920 693 * 921 694 * @param string New action value 922 695 */ 923 static function override_action($action) 924 { 696 public static function override_action($action = null) { 925 697 $registry = rcube_registry::get_instance(); 926 698 $OUTPUT = $registry->get('OUTPUT', 'core'); … … 937 709 * @return The application URL 938 710 */ 939 static function url($action, $p=array(), $task=null) 940 { 711 public static function url($action = null, $p = array(), $task = null) { 941 712 $registry = rcube_registry::get_instance(); 942 713 $COMM_PATH = $registry->get('COMM_PATH', 'core'); … … 946 717 $base = $COMM_PATH; 947 718 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)) { 952 724 foreach ($p as $key => $val) { 953 725 $qstring .= '&'.urlencode($key).'='.urlencode($val); … … 963 735 * @return string Encryprted string 964 736 */ 965 static function encrypt_passwd($pass) 966 { 737 private static function encrypt_passwd($pass) { 967 738 $cypher = des(self::get_des_key(), $pass, 1, 0, NULL); 968 739 return base64_encode($cypher); … … 975 746 * @return string Plain password 976 747 */ 977 static function decrypt_passwd($cypher) 978 { 748 public static function decrypt_passwd($cypher) { 979 749 $pass = des(self::get_des_key(), base64_decode($cypher), 0, 0, NULL); 980 750 return preg_replace('/\x00/', '', $pass); … … 986 756 * @return string DES encryption key 987 757 */ 988 static function get_des_key() 989 { 758 private static function get_des_key() { 990 759 $registry = rcube_registry::get_instance(); 991 760 $CONFIG = $registry->get_all('config'); … … 994 763 995 764 // make sure the key is exactly 24 chars long 996 if ($len <24) {765 if ($len < 24) { 997 766 $key .= str_repeat('_', 24-$len); 998 } 999 else if ($len>24) { 767 } else if ($len > 24) { 1000 768 substr($key, 0, 24); 1001 769 } … … 1007 775 * Remove temp files older than two days 1008 776 */ 1009 function temp_gc() 1010 { 777 public function temp_gc() { 1011 778 $registry = rcube_registry::get_instance(); 1012 779 $CONFIG = $registry->get_all('config'); … … 1018 785 } 1019 786 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 } 1025 794 } 1026 795 closedir($dir); 1027 796 } 1028 1029 797 1030 798 /** … … 1032 800 * Remove all expired message cache records 1033 801 */ 1034 static function message_cache_gc() 1035 { 802 public static function message_cache_gc() { 1036 803 $registry = rcube_registry::get_instance(); 1037 804 $DB = $registry->get('DB', 'core'); … … 1050 817 } 1051 818 1052 1053 819 /** 1054 820 * Check if a specific template exists … … 1057 823 * @return bool True if template exists 1058 824 */ 1059 static function template_exists($name) 1060 { 825 public static function template_exists($name) { 1061 826 $registry = rcube_registry::get_instance(); 1062 827 $CONFIG = $registry->get_all('config'); 1063 $skin_path = $CONFIG['skin_path'];1064 828 1065 829 // check template file 1066 return is_file( "$skin_path/templates/$name.html");830 return is_file($CONFIG['skin_path'].'/templates/'.$name.'html'); 1067 831 } 1068 832 … … 1073 837 * @deprecated 1074 838 */ 1075 static function parse_template($name='main', $exit=true) 1076 { 839 static function parse_template($name='main', $exit=true) { 1077 840 $registry = rcube_registry::get_instance(); 1078 841 $OUTPUT = $registry->get('OUTPUT', 'core'); … … 1205 968 // use value from post 1206 969 if (!empty($_POST[$fname])) { 1207 $value = $_POST[$fname];970 $value = self::get_input_value($fname, RCUBE_INPUT_POST); 1208 971 } 1209 972 $out = $input->show($value); … … 1219 982 * @return string Resolved SMTP host 1220 983 */ 1221 static function mail_domain($host) 1222 { 984 public static function mail_domain($host) { 1223 985 $registry = rcube_registry::get_instance(); 1224 986 $CONFIG = $registry->get_all('config'); … … 1229 991 $domain = $CONFIG['mail_domain'][$host]; 1230 992 } 1231 } 1232 else if (!empty($CONFIG['mail_domain'])) { 993 } else if (!empty($CONFIG['mail_domain'])) { 1233 994 $domain = $CONFIG['mail_domain']; 1234 995 } … … 1588 1349 if (($attrib['uppercase'] && strtolower($attrib['uppercase']=='first')) || $attrib['ucfirst']) { 1589 1350 return ucfirst($text); 1590 } 1591 elseif ($attrib['uppercase']) { 1351 } elseif ($attrib['uppercase']) { 1592 1352 return strtoupper($text); 1593 } 1594 elseif ($attrib['lowercase']) { 1353 } elseif ($attrib['lowercase']) { 1595 1354 return strtolower($text); 1596 1355 } … … 1609 1368 * @return string Field value or NULL if not available 1610 1369 */ 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) { 1613 1371 try { 1614 1372 $registry = rcube_registry::get_instance(); 1615 1373 $output = $registry->get('OUTPUT', 'core'); 1616 } 1617 catch (rcube_registry_exception $e) { 1374 } catch (rcube_registry_exception $e) { 1618 1375 $output = NULL; 1619 1376 } … … 1623 1380 if ($source == rcube::INPUT_GET && isset($_GET[$fname])) { 1624 1381 $value = $_GET[$fname]; 1625 } 1626 else if ($source == rcube::INPUT_POST && isset($_POST[$fname])) { 1382 } else if ($source == rcube::INPUT_POST && isset($_POST[$fname])) { 1627 1383 $value = $_POST[$fname]; 1628 } 1629 else if ($source == rcube::INPUT_GPC) { 1384 } else if ($source == rcube::INPUT_GPC) { 1630 1385 if (isset($_POST[$fname])) { 1631 1386 $value = $_POST[$fname]; 1632 } 1633 else if (isset($_GET[$fname])) { 1387 } else if (isset($_GET[$fname])) { 1634 1388 $value = $_GET[$fname]; 1635 } 1636 else if (isset($_COOKIE[$fname])) { 1389 } else if (isset($_COOKIE[$fname])) { 1637 1390 $value = $_COOKIE[$fname]; 1638 1391 } … … 1640 1393 1641 1394 // 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 } 1644 1398 1645 1399 // remove HTML tags if not allowed … … 1661 1415 * @return mixed Header value or null if not available 1662 1416 */ 1663 static function get_request_header($name) 1664 { 1417 public static function get_request_header($name) { 1665 1418 if (function_exists('getallheaders')) { 1666 1419 $hdrs = getallheaders(); 1667 1420 $hdrs = array_change_key_case($hdrs, CASE_UPPER); 1668 1421 $key = strtoupper($name); 1669 } 1670 else { 1422 } else { 1671 1423 $key = 'HTTP_' . strtoupper(strtr($name, '-', '_')); 1672 1424 $hdrs = array_change_key_case($_SERVER, CASE_UPPER); 1673 1425 } 1426 1674 1427 if (isset($hdrs[$key])) { 1675 1428 return $hdrs[$key]; … … 1688 1441 * @return Converted string 1689 1442 */ 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 1692 1457 $registry = rcube_registry::get_instance(); 1693 1458 $MBSTRING = $registry->get('MBSTRING', 'core'); 1694 1459 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 }1701 1460 // convert charset using mbstring module 1702 1461 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'); 1705 1463 1706 1464 // 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))) != '') { 1708 1466 return $out; 1709 1467 } 1710 1468 } 1711 1469 1712 // convert charset using iconv module1713 if (function_exists('iconv') && $from!='UTF-7' && $to!='UTF-7')1714 return iconv($from, $to, $str);1715 1716 1470 $conv = new utf8(); 1717 1471 1718 1472 // convert string to UTF-8 1719 if ($from =='UTF-7') {1473 if ($from == 'UTF-7') { 1720 1474 $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')) { 1723 1476 $str = utf8_encode($str); 1724 } 1725 else if ($from!='UTF-8') { 1477 } else if ($from != 'UTF-8') { 1726 1478 $conv->loadCharset($from); 1727 1479 $str = $conv->strToUtf8($str); … … 1729 1481 1730 1482 // encode string for output 1731 if ($to =='UTF-7') {1483 if ($to == 'UTF-7') { 1732 1484 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')) { 1735 1486 return utf8_decode($str); 1736 } 1737 else if ($to!='UTF-8') { 1487 } else if ($to!='UTF-8') { 1738 1488 $conv->loadCharset($to); 1739 1489 return $conv->utf8ToStr($str); … … 1754 1504 * @return The quoted string 1755 1505 */ 1756 static function rep_specialchars_output($str, $enctype='', $mode='', $newlines=TRUE) 1757 { 1506 public static function rep_specialchars_output($str, $enctype='', $mode='', $newlines=TRUE) { 1758 1507 static $html_encode_arr, $js_rep_table, $xml_rep_table; 1759 1508 … … 1766 1515 // encode for plaintext 1767 1516 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)); 1773 1518 } 1774 1519 … … 1793 1538 unset($encode_arr['>']); 1794 1539 unset($encode_arr['&']); 1795 } 1796 else if ($mode == 'remove') { 1540 } else if ($mode == 'remove') { 1797 1541 $str = strip_tags($str); 1798 1542 } 1799 1543 // avoid douple quotation of & 1800 $out = preg_replace( 1801 '/&([a-z]{2,5}|#[0-9]{2,4});/', 1802 '&\\1;', 1803 strtr($str, $encode_arr) 1804 ); 1544 $out = preg_replace('/&([a-z]{2,5}|#[0-9]{2,4});/', '&\\1;', strtr($str, $encode_arr)); 1805 1545 return $newlines ? nl2br($out) : $out; 1806 1546 } … … 1815 1555 $xml_rep_table['&'] = '&'; 1816 1556 1817 for ($c=160; $c<256; $c++)// can be increased to support more charsets1818 {1557 // can be increased to support more charsets 1558 for ($c=160; $c<256; $c++) { 1819 1559 $xml_rep_table[Chr($c)] = "&#$c;"; 1820 1560 … … 1836 1576 $str = self::charset_convert($str, RCMAIL_CHARSET, $out_charset); 1837 1577 } 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))); 1843 1579 } 1844 1580 … … 1846 1582 return $str; 1847 1583 } 1848 1849 1584 1850 1585 /** … … 1857 1592 * @deprecated 1858 1593 */ 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')) { 1861 1595 return html::attrib_string($attrib, $allowed); 1862 1596 } … … 1869 1603 * @return array Key-value pairs of parsed attributes 1870 1604 */ 1871 function parse_attrib_string($str) 1872 { 1605 public function parse_attrib_string($str = '') { 1873 1606 $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); 1880 1608 1881 1609 // convert attributes to an associative array (name => value) … … 1891 1619 /****** debugging functions ********/ 1892 1620 1893 1894 1621 /** 1895 1622 * tfk_debug … … 1898 1625 * @return void 1899 1626 */ 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'); 1904 1630 if ($fp !== false) { 1905 @fwrite($fp, $str); 1906 @fclose($fp); 1907 } 1908 else { 1631 fwrite($fp, $str); 1632 fclose($fp); 1633 } else { 1909 1634 die('Could not open logs/debug.tfk.'); 1910 1635 } 1911 1636 } 1912 1637 1913 1914 1638 /** 1915 1639 * Print or write debug messages … … 1917 1641 * @param mixed Debug message or data 1918 1642 */ 1919 function console($msg) 1920 { 1643 public function console($msg = '') { 1921 1644 $registry = rcube_registry::get_instance(); 1922 1645 $CONFIG = $registry->get_all('config'); … … 1926 1649 if (!($CONFIG['debug_level'] & 4)) { 1927 1650 write_log('console', $msg); 1928 } 1929 elseif ($GLOBALS['REMOTE_REQUEST']) { 1651 } elseif ($GLOBALS['REMOTE_REQUEST']) { 1930 1652 echo "/*\n $msg \n*/\n"; 1931 } 1932 else { 1653 } else { 1933 1654 echo '<div style="background:#eee; border:1px solid #ccc; '; 1934 1655 echo 'margin-bottom:3px; padding:6px"><pre>'; … … 1938 1659 } 1939 1660 1940 1941 1661 /** 1942 1662 * Append a line to a logfile in the logs directory. … … 1946 1666 * @param string Line to append 1947 1667 */ 1948 function write_log($name, $line) 1949 { 1668 private function write_log($name, $line) { 1950 1669 $registry = rcube_registry::get_instance(); 1951 1670 $log_dir = $registry->get('log_dir', 'config'); … … 1954 1673 $line = var_export($line, true); 1955 1674 } 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); 1961 1676 1962 1677 if (empty($log_dir)) { … … 1964 1679 } 1965 1680 // try to open specific log file for writing 1966 if ($fp = @fopen($log_dir . '/' . $name, 'a')) {1681 if ($fp = fopen($log_dir . '/' . $name, 'a')) { 1967 1682 fwrite($fp, $log_entry); 1968 1683 fclose($fp); … … 1971 1686 1972 1687 1973 static function timer() 1974 { 1975 list($usec, $sec) = explode(" ", microtime()); 1688 public static function timer() { 1689 list($usec, $sec) = explode(' ', microtime()); 1976 1690 return ((float)$usec + (float)$sec); 1977 1691 } 1978 1692 1979 static function print_time($timer, $label='Timer') 1980 { 1693 /** 1694 * not used 1695 */ 1696 private static function print_time($timer, $label='Timer') { 1981 1697 static $print_count = 0; 1982 1698 … … 1994 1710 } 1995 1711 1712 ?> -
branches/devel-vnext/program/include/rcube_user.php
r957 r1015 19 19 $Id: rcube_user.inc 933 2007-11-29 14:17:32Z thomasb $ 20 20 21 */ 22 21 */ 23 22 24 23 /** … … 28 27 * @author Thomas Bruederli <roundcube@gmail.com> 29 28 */ 30 class rcube_user 31 { 32 var $ID = null; 33 var $data = null; 29 class rcube_user { 30 public $ID = null; 31 public $data = null; 34 32 35 33 /** … … 38 36 * @param object DB Database connection 39 37 */ 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); 47 45 $sql_arr = $DB->fetch_assoc($sql_result); 48 46 } 49 47 50 if (!empty($sql_arr)) 51 { 48 if (!empty($sql_arr)) { 52 49 $this->ID = $sql_arr['user_id']; 53 50 $this->data = $sql_arr; … … 56 53 57 54 /** 58 * PHP 4 object constructor59 *60 * @see rcube_user::__construct61 */62 function rcube_user($id = null, $sql_arr = null)63 {64 $this->__construct($id, $sql_arr);65 }66 67 /**68 55 * Build a user name string (as e-mail address) 69 56 * 70 57 * @return string Full user name 71 58 */ 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 } 77 62 78 63 /** … … 81 66 * @return array Hash array with prefs 82 67 */ 83 function get_prefs()84 {85 if ($this->ID && $this->data['preferences'])86 return unserialize($this->data['preferences']);87 else88 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 } 89 74 } 90 75 … … 96 81 * @return boolean True on success, False on failure 97 82 */ 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 } 120 100 return true; 121 101 } 122 123 102 return false; 124 103 } … … 130 109 * @return array Hash array with all cols of the 131 110 */ 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) : ''); 137 116 return $DB->fetch_assoc($sql_result); 138 117 } 139 118 140 141 119 /** 142 120 * Return a list of all identities linked with this user … … 144 122 * @return array List of identities 145 123 */ 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'; 150 133 // 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 161 137 /** 162 138 * Update a specific identity record … … 166 142 * @return boolean True if saved successfully, false if nothing changed 167 143 */ 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'); 174 151 175 152 $write_sql = array(); 176 153 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); 193 164 return $DB->affected_rows(); 194 165 } … … 201 172 * @return int The inserted identity ID or false on error 202 173 */ 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'); 209 181 210 182 $insert_cols = $insert_values = array(); 211 foreach ((array)$data as $col => $value) 212 { 183 foreach ((array)$data as $col => $value) { 213 184 $insert_cols[] = $DB->quoteIdentifier($col); 214 185 $insert_values[] = $DB->quote($value); 215 186 } 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')); 224 193 } 225 194 … … 230 199 * @return boolean True if deleted successfully, false if nothing changed 231 200 */ 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); 247 215 return $DB->affected_rows(); 248 216 } 249 217 250 251 218 /** 252 219 * Make this identity the default one for this user … … 254 221 * @param int The identity ID 255 222 */ 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); 270 235 } 271 236 } … … 275 240 * Update user's last_login timestamp 276 241 */ 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); 288 252 } 289 253 } … … 291 255 * Clear the saved object state 292 256 */ 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 } 299 261 300 262 /** … … 306 268 * @static 307 269 */ 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'); 311 273 312 274 // 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); 319 279 320 280 // 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 326 288 /** 327 289 * Create a new user record and return a rcube_user instance … … 332 294 * @static 333 295 */ 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'); 337 301 338 302 $user_email = ''; 339 303 340 304 // 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 : ''; 361 333 362 334 // 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, ?, ?)'; 369 345 $DB->query( 370 "INSERT INTO ".get_table_name('identities')." 371 (user_id, del, standard, name, email) 372 VALUES (?, 0, 1, ?, ?)", 346 $_query, 373 347 $user_id, 374 348 strip_newlines($user_name), 375 preg_replace('/^@/', $user . '@', $sql_arr[0])); 349 preg_replace('/^@/', $user . '@', $sql_arr[0]) 350 ); 376 351 } 377 } 378 else 379 { 352 } else { 380 353 // 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, ?, ?)'; 381 357 $DB->query( 382 "INSERT INTO ".get_table_name('identities')." 383 (user_id, del, standard, name, email) 384 VALUES (?, 0, 1, ?, ?)", 358 $_query, 385 359 $user_id, 386 360 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 { 392 368 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 } 406 424 407 425 /** … … 410 428 * @param string E-mail address to resolve 411 429 * @return string Resolved IMAP username 412 * @static 413 */ 414 function email2user($email) 415 { 430 */ 431 public static function email2user($email = null) { 416 432 $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++) { 421 436 $data = $r[$i]; 422 437 $arr = preg_split('/\s+/', $data); 423 if (count($arr) > 0) 424 { 438 if (count($arr) > 0) { 425 439 $user = trim($arr[count($arr)-1]); 426 440 break; 427 441 } 428 442 } 429 430 443 return $user; 431 444 } 432 433 445 434 446 /** … … 437 449 * @param string User name 438 450 * @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]; 449 458 $arr = preg_split('/\s+/', $data); 450 if (count($arr) > 0) 451 { 459 if (count($arr) > 0) { 452 460 $email = trim($arr[0]); 453 461 break; 454 462 } 455 463 } 456 457 464 return $email; 458 465 }
Note: See TracChangeset
for help on using the changeset viewer.
