- Timestamp:
- Nov 10, 2013, 6:02:30 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/functions.inc.php
r25425 r25426 1 <?php 1 <?php 2 2 // +-----------------------------------------------------------------------+ 3 3 // | Piwigo - a PHP based photo gallery | … … 22 22 // +-----------------------------------------------------------------------+ 23 23 24 /** 25 * @package functions\___ 26 */ 27 24 28 include_once( PHPWG_ROOT_PATH .'include/functions_plugins.inc.php' ); 25 29 include_once( PHPWG_ROOT_PATH .'include/functions_user.inc.php' ); … … 37 41 include_once( PHPWG_ROOT_PATH .'include/template.class.php'); 38 42 39 //----------------------------------------------------------- generic functions 40 41 /** 42 * stupidly returns the current microsecond since Unix epoch 43 44 /** 45 * returns the current microsecond since Unix epoch 46 * 47 * @return int 43 48 */ 44 49 function micro_seconds() … … 50 55 } 51 56 52 // The function get_moment returns a float value coresponding to the number 53 // of seconds since the unix epoch (1st January 1970) and the microseconds 54 // are precised : e.g. 1052343429.89276600 57 /** 58 * returns a float value coresponding to the number of seconds since 59 * the unix epoch (1st January 1970) and the microseconds are precised 60 * e.g. 1052343429.89276600 61 * 62 * @return float 63 */ 55 64 function get_moment() 56 65 { … … 58 67 } 59 68 60 // The function get_elapsed_time returns the number of seconds (with 3 61 // decimals precision) between the start time and the end time given. 62 function get_elapsed_time( $start, $end ) 63 { 64 return number_format( $end - $start, 3, '.', ' ').' s'; 65 } 66 67 // get_extension returns the part of the string after the last "." 69 /** 70 * returns the number of seconds (with 3 decimals precision) 71 * between the start time and the end time given 72 * 73 * @param float $start 74 * @param float $end 75 * @return string "$TIME s" 76 */ 77 function get_elapsed_time($start, $end) 78 { 79 return number_format($end - $start, 3, '.', ' ').' s'; 80 } 81 82 /** 83 * returns the part of the string after the last "." 84 * 85 * @param string $filename 86 * @return string 87 */ 68 88 function get_extension( $filename ) 69 89 { … … 71 91 } 72 92 73 // get_filename_wo_extension returns the part of the string before the last 74 // ".". 75 // get_filename_wo_extension( 'test.tar.gz' ) -> 'test.tar' 93 /** 94 * returns the part of the string before the last ".". 95 * get_filename_wo_extension( 'test.tar.gz' ) = 'test.tar' 96 * 97 * @param string $filename 98 * @return string 99 */ 76 100 function get_filename_wo_extension( $filename ) 77 101 { … … 87 111 define('MKGETDIR_DEFAULT', 7); 88 112 /** 89 * creates directory if not exists ;ensures that directory is writable90 * @param:91 * string $dir92 * int $flags combination of MKGETDIR_xxx93 * @return bool false on error else true113 * creates directory if not exists and ensures that directory is writable 114 * 115 * @param string $dir 116 * @param int $flags combination of MKGETDIR_xxx 117 * @return bool 94 118 */ 95 119 function mkgetdir($dir, $flags=MKGETDIR_DEFAULT) … … 129 153 } 130 154 131 /* returns 0 if $str is Ascii, 1 if utf-8, -1 otherwise */ 155 /** 156 * finds out if a string is in ASCII, UTF-8 or other encoding 157 * 158 * @param string $str 159 * @return int *0* if _$str_ is ASCII, *1* if UTF-8, *-1* otherwise 160 */ 132 161 function qualify_utf8($Str) 133 162 { 134 163 $ret = 0; 135 for ($i=0; $i<strlen($Str); $i++) { 164 for ($i=0; $i<strlen($Str); $i++) 165 { 136 166 if (ord($Str[$i]) < 0x80) continue; # 0bbbbbbb 137 167 $ret = 1; … … 142 172 elseif ((ord($Str[$i]) & 0xFE) == 0xFC) $n=5; # 1111110b 143 173 else return -1; # Does not match any model 144 for ($j=0; $j<$n; $j++) { # n bytes matching 10bbbbbb follow ? 174 for ($j=0; $j<$n; $j++) 175 { # n bytes matching 10bbbbbb follow ? 145 176 if ((++$i == strlen($Str)) || ((ord($Str[$i]) & 0xC0) != 0x80)) 146 177 return -1; … … 150 181 } 151 182 152 /* Remove accents from a UTF-8 or ISO-859-1 string (from wordpress) 153 * @param string sstring - an UTF-8 or ISO-8859-1 string 183 /** 184 * Remove accents from a UTF-8 or ISO-8859-1 string (from wordpress) 185 * 186 * @param string $string 187 * @return string 154 188 */ 155 189 function remove_accents($string) … … 157 191 $utf = qualify_utf8($string); 158 192 if ( $utf == 0 ) 193 { 159 194 return $string; // ascii 160 161 if ( $utf > 0 ) { 195 } 196 197 if ( $utf > 0 ) 198 { 162 199 $chars = array( 163 200 // Decompositions for Latin-1 Supplement … … 263 300 264 301 $string = strtr($string, $chars); 265 } else { 302 } 303 else 304 { 266 305 // Assume ISO-8859-1 if not UTF-8 267 306 $chars['in'] = chr(128).chr(131).chr(138).chr(142).chr(154).chr(158) … … 289 328 if (function_exists('mb_strtolower') && defined('PWG_CHARSET')) 290 329 { 330 /** 331 * removes accents from a string and converts it to lower case 332 * 333 * @param string $term 334 * @return string 335 */ 291 336 function transliterate($term) 292 337 { … … 296 341 else 297 342 { 343 /** 344 * @ignore 345 */ 298 346 function transliterate($term) 299 347 { … … 302 350 } 303 351 304 305 306 352 /** 307 353 * simplify a string to insert it into an URL 308 354 * 309 * @param string 355 * @param string $str 310 356 * @return string 311 357 */ … … 325 371 } 326 372 327 //-------------------------------------------- Piwigo specific functions328 329 373 /** 330 374 * returns an array with a list of {language_code => language_name} 331 375 * 332 * @return s array376 * @return string[] 333 377 */ 334 378 function get_languages() … … 353 397 } 354 398 399 /** 400 * log the visit into history table 401 * 402 * @param int $image_id 403 * @param string $image_type 404 * @return bool 405 */ 355 406 function pwg_log($image_id = null, $image_type = null) 356 407 { … … 412 463 413 464 /** 414 * Computes the difference between two dates 465 * Computes the difference between two dates. 415 466 * returns a DateInterval object or a stdClass with the same attributes 416 467 * http://stephenharris.info/date-intervals-in-php-5-2 … … 418 469 * @param DateTime $date1 419 470 * @param DateTime $date2 420 * @return DateInterval 471 * @return DateInterval|stdClass 421 472 */ 422 473 function dateDiff($date1, $date2) … … 482 533 /** 483 534 * converts a string into a DateTime object 484 * @param: mixed, datetime string or timestamp int 485 * @param: string, input format 486 * @return: DateTime or false 535 * 536 * @param int|string timestamp or datetime string 537 * @param string $format input format respecting date() syntax 538 * @return DateTime|false 487 539 */ 488 540 function str2DateTime($original, $format=null) … … 525 577 526 578 /** 527 * returns a formatted date for display 528 * @param: mixed, datetime string or timestamp int 529 * @param: bool, show time 530 * @param: bool, show day name 531 * @param: string, input format 532 * @return: string 579 * returns a formatted and localized date for display 580 * 581 * @param int|string timestamp or datetime string 582 * @param bool $show_time 583 * @param bool $show_day_name 584 * @param string $format input format respecting date() syntax 585 * @return string 533 586 */ 534 587 function format_date($original, $show_time=false, $show_day_name=true, $format=null) … … 567 620 /** 568 621 * Works out the time since the given date 569 * @param: mixed, datetime string or timestamp int 570 * @param: string, stop (year,month,week,day,hour,minute,second) 571 * @param: string, input format 572 * @param: bool, append text ("ago" or "in the future") 573 * @param: bool, display weeks 574 * @return: string 622 * 623 * @param int|string timestamp or datetime string 624 * @param string $stop year,month,week,day,hour,minute,second 625 * @param string $format input format respecting date() syntax 626 * @param bool $with_text append "ago" or "in the future" 627 * @param bool $with_weeks 628 * @return string 575 629 */ 576 630 function time_since($original, $stop='minute', $format=null, $with_text=true, $with_week=true) … … 638 692 /** 639 693 * transform a date string from a format to another (MySQL to d/M/Y for instance) 640 * @param: string, date 641 * @param: string, input format 642 * @param: string, output format 643 * @param: string, default value if inout is empty 644 * @return: string 694 * 695 * @param string $original 696 * @param string $format_in respecting date() syntax 697 * @param string $format_out respecting date() syntax 698 * @param string $default if _$original_ is empty 699 * @return string 645 700 */ 646 701 function transform_date($original, $format_in, $format_out, $default=null) … … 651 706 } 652 707 708 /** 709 * append a variable to _$debug_ global 710 * 711 * @param string $string 712 */ 653 713 function pwg_debug( $string ) 654 714 { … … 666 726 667 727 /** 668 * Redirects to the given URL (HTTP method) 669 * 670 * Note : once this function called, the execution doesn't go further 728 * Redirects to the given URL (HTTP method). 729 * once this function called, the execution doesn't go further 671 730 * (presence of an exit() instruction. 672 731 * … … 689 748 690 749 /** 691 * Redirects to the given URL (HTML method) 692 * 693 * Note : once this function called, the execution doesn't go further 750 * Redirects to the given URL (HTML method). 751 * once this function called, the execution doesn't go further 694 752 * (presence of an exit() instruction. 695 753 * 696 754 * @param string $url 697 * @param string $ title_msg698 * @param integer $refre h_time755 * @param string $msg 756 * @param integer $refresh_time 699 757 * @return void 700 758 */ … … 740 798 741 799 /** 742 * Redirects to the given URL (Switch to HTTP method or HTML method) 743 * 744 * Note : once this function called, the execution doesn't go further 800 * Redirects to the given URL (automatically choose HTTP or HTML method). 801 * once this function called, the execution doesn't go further 745 802 * (presence of an exit() instruction. 746 803 * 747 804 * @param string $url 748 * @param string $ title_msg749 * @param integer $refre h_time805 * @param string $msg 806 * @param integer $refresh_time 750 807 * @return void 751 808 */ … … 769 826 770 827 /** 771 * returns $_SERVER['QUERY_STRING'] whit out keys given in parameters772 * 773 * @param array$rejects774 * @param boolean $escape - if true escape & to & (for html)828 * returns $_SERVER['QUERY_STRING'] whithout keys given in parameters 829 * 830 * @param string[] $rejects 831 * @param boolean $escape escape *&* to *&* 775 832 * @returns string 776 833 */ … … 791 848 /** 792 849 * returns true if the url is absolute (begins with http) 850 * 793 851 * @param string $url 794 852 * @returns boolean … … 806 864 /** 807 865 * returns available themes 866 * 867 * @param bool $show_mobile 868 * @return array 808 869 */ 809 870 function get_pwg_themes($show_mobile=false) … … 843 904 } 844 905 906 /** 907 * check if a theme is installed (directory exsists) 908 * 909 * @param string $theme_id 910 * @return bool 911 */ 845 912 function check_theme_installed($theme_id) 846 913 { … … 850 917 } 851 918 852 /** Transforms an original path to its pwg representative */ 919 /** 920 * Transforms an original path to its pwg representative 921 * 922 * @param string $path 923 * @param string $representative_ext 924 * @return string 925 */ 853 926 function original_to_representative($path, $representative_ext) 854 927 { … … 860 933 861 934 /** 862 * @param element_info array containing element information from db; 863 * at least 'id', 'path' should be present 935 * get the full path of an image 936 * 937 * @param array $element_info element information from db (at least 'path') 938 * @return strinf 864 939 */ 865 940 function get_element_path($element_info) … … 875 950 876 951 /** 877 * fill the current user caddie with given elements, if not already in 878 * caddie 879 * 880 * @param array elements_id 952 * fill the current user caddie with given elements, if not already in caddie 953 * 954 * @param int[] $elements_id 881 955 */ 882 956 function fill_caddie($elements_id) … … 910 984 911 985 /** 912 * returns the element name from its filename 913 * 914 * @param string filename 986 * returns the element name from its filename. 987 * removes file extension and replace underscores by spaces 988 * 989 * @param string $filename 915 990 * @return string name 916 991 */ … … 921 996 922 997 /** 923 * translation function 924 * returns the corresponding value from $lang if existing,else the key is returned998 * translation function. 999 * returns the corresponding value from _$lang_ if existing else the key is returned 925 1000 * if more than one parameter is provided sprintf is applied 1001 * 926 1002 * @param string $key 927 1003 * @param mixed $args,... optional arguments … … 952 1028 /** 953 1029 * returns the printf value for strings including %d 954 * return is concorded with decimal value (singular, plural)955 * 956 * @param s ingular stringkey957 * @param plural stringkey958 * @param decimal value1030 * returned value is concorded with decimal value (singular, plural) 1031 * 1032 * @param string $singular_key 1033 * @param string $plural_key 1034 * @param int $decimal 959 1035 * @return string 960 1036 */ 961 function l10n_dec($singular_ fmt_key, $plural_fmt_key, $decimal)1037 function l10n_dec($singular_key, $plural_key, $decimal) 962 1038 { 963 1039 global $lang_info; … … 967 1043 l10n(( 968 1044 (($decimal > 1) or ($decimal == 0 and $lang_info['zero_plural'])) 969 ? $plural_ fmt_key970 : $singular_ fmt_key1045 ? $plural_key 1046 : $singular_key 971 1047 )), $decimal); 972 1048 } 973 1049 974 /* 1050 /** 975 1051 * returns a single element to use with l10n_args 976 1052 * 977 * @param string key:translation key978 * @param mixed args:arguments to use on sprintf($key, args)1053 * @param string $key translation key 1054 * @param mixed $args arguments to use on sprintf($key, args) 979 1055 * if args is a array, each values are used on sprintf 980 1056 * @return string … … 993 1069 } 994 1070 995 /* 996 * returns a string formated with l10n elements 997 * 998 * @param array $key_args: l10n_args element or array of l10n_args elements 999 * @param string $sep: used when translated elements are concatened 1071 /** 1072 * returns a string formated with l10n elements. 1073 * it is usefull to "prepare" a text and translate it later 1074 * @see get_l10n_args() 1075 * 1076 * @param array $key_args one l10n_args element or array of l10n_args elements 1077 * @param string $sep used when translated elements are concatened 1000 1078 * @return string 1001 1079 */ … … 1035 1113 1036 1114 /** 1037 * returns the corresponding value from $themeconf if existing. Else, the 1038 * key is returned 1039 * 1040 * @param string key 1115 * returns the corresponding value from $themeconf if existing or an empty string 1116 * 1117 * @param string $key 1041 1118 * @return string 1042 1119 */ … … 1072 1149 * Add configuration parameters from database to global $conf array 1073 1150 * 1151 * @param string $condition SQL condition 1074 1152 * @return void 1075 1153 */ … … 1110 1188 /** 1111 1189 * Add or update a config parameter 1190 * 1112 1191 * @param string $param 1113 1192 * @param string $value … … 1146 1225 1147 1226 /** 1148 * Delete on or more config parameters1227 * Delete one or more config parameters 1149 1228 * @since 2.6 1229 * 1150 1230 * @param string|string[] $params 1151 1231 */ … … 1176 1256 1177 1257 /** 1178 * Prepends and appends a string at each value of the given array. 1179 * 1180 * @param array 1181 * @param string prefix to each array values 1182 * @param string suffix to each array values 1258 * Prepends and appends strings at each value of the given array. 1259 * 1260 * @param array $array 1261 * @param string $prepend_str 1262 * @param string $append_str 1263 * @return array 1183 1264 */ 1184 1265 function prepend_append_array_items($array, $prepend_str, $append_str) … … 1193 1274 1194 1275 /** 1195 * creates an hashed based on a query, this function is a very common 1196 * pattern used here. Among the selected columns fetched, choose one to be 1197 * the key, another one to be the value. 1276 * creates an simple hashmap based on a SQL query. 1277 * choose one to be the key, another one to be the value. 1198 1278 * 1199 1279 * @param string $query … … 1216 1296 1217 1297 /** 1218 * creates an hashed based on a query, this function is a very common 1219 * pattern used here. The key is given as parameter, the value is an associative 1220 * array. 1298 * creates an associative array based on a SQL query. 1299 * choose one to be the key 1221 1300 * 1222 1301 * @param string $query … … 1236 1315 1237 1316 /** 1238 * Return basename of the current script 1239 * Lower case convertion is applied on return value 1240 * Return value is without file extention ".php" 1241 * 1242 * @param void 1243 * 1244 * @return script basename 1317 * Return the basename of the current script. 1318 * The lowercase case filename of the current script without extension 1319 * 1320 * @return string 1245 1321 */ 1246 1322 function script_basename() … … 1266 1342 1267 1343 /** 1268 * Return value for the current page define on $conf['filter_pages'] 1269 * Îf value is not defined, default value are returned 1270 * 1271 * @param value name 1272 * 1273 * @return filter page value 1344 * Return $conf['filter_pages'] value for the current page 1345 * 1346 * @param string $value_name 1347 * @return mixed 1274 1348 */ 1275 1349 function get_filter_page_value($value_name) … … 1294 1368 1295 1369 /** 1296 * returns the character set of data sent to browsers / received from forms 1370 * return the character set used by Piwigo 1371 * @return string 1297 1372 */ 1298 1373 function get_pwg_charset() … … 1307 1382 1308 1383 /** 1309 * returns the parent language of the current language or any language 1384 * returns the parent (fallback) language of a language. 1385 * if _$lang_id_ is null it applies to the current language 1310 1386 * @since 2.6 1387 * 1311 1388 * @param string $lang_id 1312 * @return string 1389 * @return string|null 1313 1390 */ 1314 1391 function get_parent_language($lang_id=null) … … 1333 1410 /** 1334 1411 * includes a language file or returns the content of a language file 1335 * availability of the file 1336 * 1337 * in descending order of preference: 1412 * 1413 * tries to load in descending order: 1338 1414 * param language, user language, default language 1339 * Piwigo default language. 1340 * 1341 * @param string filename 1342 * @param string dirname 1415 * 1416 * @param string $filename 1417 * @param string $dirname 1343 1418 * @param mixed options can contain 1344 * language - language to load (if empty uses user language) 1345 * return - if true the file content is returned otherwise the file is evaluated as php 1346 * target_charset - 1347 * no_fallback - the language must be respected 1348 * local - if true, get local language file 1349 * @return boolean success status or a string if options['return'] is true 1350 */ 1351 function load_language($filename, $dirname = '', 1352 $options = array() ) 1419 * @option string language - language to load 1420 * @option bool return - if true the file content is returned 1421 * @option bool no_fallback - if true do not load default language 1422 * @option bool local - if true load file from local directory 1423 * @return boolean|string 1424 */ 1425 function load_language($filename, $dirname = '', $options = array()) 1353 1426 { 1354 1427 global $user, $language_files; … … 1484 1557 /** 1485 1558 * converts a string from a character set to another character set 1486 * @param string str the string to be converted 1487 * @param string source_charset the character set in which the string is encoded 1488 * @param string dest_charset the destination character set 1559 * 1560 * @param string $str 1561 * @param string $source_charset 1562 * @param string $dest_charset 1489 1563 */ 1490 1564 function convert_charset($str, $source_charset, $dest_charset) … … 1508 1582 return mb_convert_encoding( $str, $dest_charset, $source_charset ); 1509 1583 } 1510 return $str; // ???1584 return $str; // TODO 1511 1585 } 1512 1586 … … 1514 1588 * makes sure a index.htm protects the directory from browser file listing 1515 1589 * 1516 * @param string dir directory1590 * @param string $dir 1517 1591 */ 1518 1592 function secure_directory($dir) … … 1528 1602 * returns a "secret key" that is to be sent back when a user posts a form 1529 1603 * 1530 * @param int valid_after_seconds - key validity start time from now 1604 * @param int $valid_after_seconds - key validity start time from now 1605 * @param string $aditionnal_data_to_hash 1606 * @return string 1531 1607 */ 1532 1608 function get_ephemeral_key($valid_after_seconds, $aditionnal_data_to_hash = '') … … 1541 1617 } 1542 1618 1619 /** 1620 * verify a key sent back with a form 1621 * 1622 * @param string $key 1623 * @param string $aditionnal_data_to_hash 1624 * @return bool 1625 */ 1543 1626 function verify_ephemeral_key($key, $aditionnal_data_to_hash = '') 1544 1627 { … … 1561 1644 /** 1562 1645 * return an array which will be sent to template to display navigation bar 1646 * 1647 * @param string $url base url of all links 1648 * @param int $nb_elements 1649 * @param int $start 1650 * @param int $nb_element_page 1651 * @param bool $clean_url 1652 * @param string $param_name 1653 * @return array 1563 1654 */ 1564 1655 function create_navigation_bar($url, $nb_element, $start, $nb_element_page, $clean_url = false, $param_name='start') … … 1617 1708 /** 1618 1709 * return an array which will be sent to template to display recent icon 1710 * 1711 * @param string $date 1712 * @param bool $is_child_date 1713 * @return array 1619 1714 */ 1620 1715 function get_icon($date, $is_child_date = false) … … 1657 1752 1658 1753 /** 1659 * check token comming from form posted or get params to prevent csrf attacks 1754 * check token comming from form posted or get params to prevent csrf attacks. 1660 1755 * if pwg_token is empty action doesn't require token 1661 1756 * else pwg_token is compare to server token … … 1673 1768 } 1674 1769 else 1770 { 1675 1771 bad_request('missing token'); 1676 } 1677 1772 } 1773 } 1774 1775 /** 1776 * get pwg_token used to prevent csrf attacks 1777 * 1778 * @return string 1779 */ 1678 1780 function get_pwg_token() 1679 1781 { … … 1687 1789 * pattern. This should happen only during hacking attempts. 1688 1790 * 1689 * @param string param_name 1690 * @param array param_array 1691 * @param boolean is_array 1692 * @param string pattern 1693 * @param boolean mandatory 1694 * 1695 * @return void 1791 * @param string $param_name 1792 * @param array $param_array 1793 * @param boolean $is_array 1794 * @param string $pattern 1795 * @param boolean $mandatory 1696 1796 */ 1697 1797 function check_input_parameter($param_name, $param_array, $is_array, $pattern, $mandatory=false) … … 1737 1837 } 1738 1838 1739 1839 /** 1840 * get localized privacy level values 1841 * 1842 * @return string[] 1843 */ 1740 1844 function get_privacy_level_options() 1741 1845 { … … 1766 1870 /** 1767 1871 * return the branch from the version. For example version 2.2.4 is for branch 2.2 1872 * 1873 * @param string $version 1874 * @return string 1768 1875 */ 1769 1876 function get_branch_from_version($version) … … 1774 1881 /** 1775 1882 * return the device type: mobile, tablet or desktop 1883 * 1884 * @return string 1776 1885 */ 1777 1886 function get_device() … … 1803 1912 /** 1804 1913 * return true if mobile theme should be loaded 1914 * 1915 * @return bool 1805 1916 */ 1806 1917 function mobile_theme() … … 1834 1945 /** 1835 1946 * check url format 1947 * 1948 * @param string $url 1949 * @return bool 1836 1950 */ 1837 1951 function url_check_format($url) … … 1850 1964 /** 1851 1965 * check email format 1966 * 1967 * @param string $mail_address 1968 * @return bool 1852 1969 */ 1853 1970 function email_check_format($mail_address) … … 1867 1984 } 1868 1985 1869 /** returns the number of available comments for the connected user */ 1986 /** 1987 * returns the number of available comments for the connected user 1988 * 1989 * @return int 1990 */ 1870 1991 function get_nb_available_comments() 1871 1992 {
Note: See TracChangeset
for help on using the changeset viewer.