Ignore:
Timestamp:
Nov 10, 2013, 6:02:30 PM (10 years ago)
Author:
mistic100
Message:

feature 2999 : documentation of include/functions.inc.php

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/functions.inc.php

    r25425 r25426  
    1 <?php
     1<?php 
    22// +-----------------------------------------------------------------------+
    33// | Piwigo - a PHP based photo gallery                                    |
     
    2222// +-----------------------------------------------------------------------+
    2323
     24/**
     25 * @package functions\___
     26 */
     27
    2428include_once( PHPWG_ROOT_PATH .'include/functions_plugins.inc.php' );
    2529include_once( PHPWG_ROOT_PATH .'include/functions_user.inc.php' );
     
    3741include_once( PHPWG_ROOT_PATH .'include/template.class.php');
    3842
    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
    4348 */
    4449function micro_seconds()
     
    5055}
    5156
    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 */
    5564function get_moment()
    5665{
     
    5867}
    5968
    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 */
     77function 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 */
    6888function get_extension( $filename )
    6989{
     
    7191}
    7292
    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 */
    76100function get_filename_wo_extension( $filename )
    77101{
     
    87111define('MKGETDIR_DEFAULT', 7);
    88112/**
    89  * creates directory if not exists; ensures that directory is writable
    90  * @param:
    91  * string $dir
    92  * int $flags combination of MKGETDIR_xxx
    93  * @return bool false on error else true
     113 * 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
    94118 */
    95119function mkgetdir($dir, $flags=MKGETDIR_DEFAULT)
     
    129153}
    130154
    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 */
    132161function qualify_utf8($Str)
    133162{
    134163  $ret = 0;
    135   for ($i=0; $i<strlen($Str); $i++) {
     164  for ($i=0; $i<strlen($Str); $i++)
     165  {
    136166    if (ord($Str[$i]) < 0x80) continue; # 0bbbbbbb
    137167    $ret = 1;
     
    142172    elseif ((ord($Str[$i]) & 0xFE) == 0xFC) $n=5; # 1111110b
    143173    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 ?
    145176      if ((++$i == strlen($Str)) || ((ord($Str[$i]) & 0xC0) != 0x80))
    146177        return -1;
     
    150181}
    151182
    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
    154188 */
    155189function remove_accents($string)
     
    157191  $utf = qualify_utf8($string);
    158192  if ( $utf == 0 )
     193  {
    159194    return $string; // ascii
    160 
    161   if ( $utf > 0 ) {
     195  }
     196
     197  if ( $utf > 0 )
     198  {
    162199    $chars = array(
    163200    // Decompositions for Latin-1 Supplement
     
    263300
    264301    $string = strtr($string, $chars);
    265   } else {
     302  }
     303  else
     304  {
    266305    // Assume ISO-8859-1 if not UTF-8
    267306    $chars['in'] = chr(128).chr(131).chr(138).chr(142).chr(154).chr(158)
     
    289328if (function_exists('mb_strtolower') && defined('PWG_CHARSET'))
    290329{
     330  /**
     331   * removes accents from a string and converts it to lower case
     332   *
     333   * @param string $term
     334   * @return string
     335   */
    291336  function transliterate($term)
    292337  {
     
    296341else
    297342{
     343  /**
     344   * @ignore
     345   */
    298346  function transliterate($term)
    299347  {
     
    302350}
    303351
    304 
    305 
    306352/**
    307353 * simplify a string to insert it into an URL
    308354 *
    309  * @param string
     355 * @param string $str
    310356 * @return string
    311357 */
     
    325371}
    326372
    327 //-------------------------------------------- Piwigo specific functions
    328 
    329373/**
    330374 * returns an array with a list of {language_code => language_name}
    331375 *
    332  * @returns array
     376 * @return string[]
    333377 */
    334378function get_languages()
     
    353397}
    354398
     399/**
     400 * log the visit into history table
     401 *
     402 * @param int $image_id
     403 * @param string $image_type
     404 * @return bool
     405 */
    355406function pwg_log($image_id = null, $image_type = null)
    356407{
     
    412463
    413464/**
    414  * Computes the difference between two dates
     465 * Computes the difference between two dates.
    415466 * returns a DateInterval object or a stdClass with the same attributes
    416467 * http://stephenharris.info/date-intervals-in-php-5-2
     
    418469 * @param DateTime $date1
    419470 * @param DateTime $date2
    420  * @return DateInterval
     471 * @return DateInterval|stdClass
    421472 */
    422473function dateDiff($date1, $date2)
     
    482533/**
    483534 * 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
    487539 */
    488540function str2DateTime($original, $format=null)
     
    525577
    526578/**
    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
    533586 */
    534587function format_date($original, $show_time=false, $show_day_name=true, $format=null)
     
    567620/**
    568621 * 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
    575629 */
    576630function time_since($original, $stop='minute', $format=null, $with_text=true, $with_week=true)
     
    638692/**
    639693 * 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
    645700 */
    646701function transform_date($original, $format_in, $format_out, $default=null)
     
    651706}
    652707
     708/**
     709 * append a variable to _$debug_ global
     710 *
     711 * @param string $string
     712 */
    653713function pwg_debug( $string )
    654714{
     
    666726
    667727/**
    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
    671730 * (presence of an exit() instruction.
    672731 *
     
    689748
    690749/**
    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
    694752 * (presence of an exit() instruction.
    695753 *
    696754 * @param string $url
    697  * @param string $title_msg
    698  * @param integer $refreh_time
     755 * @param string $msg
     756 * @param integer $refresh_time
    699757 * @return void
    700758 */
     
    740798
    741799/**
    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
    745802 * (presence of an exit() instruction.
    746803 *
    747804 * @param string $url
    748  * @param string $title_msg
    749  * @param integer $refreh_time
     805 * @param string $msg
     806 * @param integer $refresh_time
    750807 * @return void
    751808 */
     
    769826
    770827/**
    771  * returns $_SERVER['QUERY_STRING'] whitout keys given in parameters
    772  *
    773  * @param array $rejects
    774  * @param boolean $escape - if true escape & to &amp; (for html)
     828 * returns $_SERVER['QUERY_STRING'] whithout keys given in parameters
     829 *
     830 * @param string[] $rejects
     831 * @param boolean $escape escape *&* to *&amp;*
    775832 * @returns string
    776833 */
     
    791848/**
    792849 * returns true if the url is absolute (begins with http)
     850 *
    793851 * @param string $url
    794852 * @returns boolean
     
    806864/**
    807865 * returns available themes
     866 *
     867 * @param bool $show_mobile
     868 * @return array
    808869 */
    809870function get_pwg_themes($show_mobile=false)
     
    843904}
    844905
     906/**
     907 * check if a theme is installed (directory exsists)
     908 *
     909 * @param string $theme_id
     910 * @return bool
     911 */
    845912function check_theme_installed($theme_id)
    846913{
     
    850917}
    851918
    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 */
    853926function original_to_representative($path, $representative_ext)
    854927{
     
    860933
    861934/**
    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
    864939 */
    865940function get_element_path($element_info)
     
    875950
    876951/**
    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
    881955 */
    882956function fill_caddie($elements_id)
     
    910984
    911985/**
    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
    915990 * @return string name
    916991 */
     
    921996
    922997/**
    923  * translation function
    924  * returns the corresponding value from $lang if existing, else the key is returned
     998 * translation function.
     999 * returns the corresponding value from _$lang_ if existing else the key is returned
    9251000 * if more than one parameter is provided sprintf is applied
     1001 *
    9261002 * @param string $key
    9271003 * @param mixed $args,... optional arguments
     
    9521028/**
    9531029 * returns the printf value for strings including %d
    954  * return is concorded with decimal value (singular, plural)
    955  *
    956  * @param singular string key
    957  * @param plural string key
    958  * @param decimal value
     1030 * returned value is concorded with decimal value (singular, plural)
     1031 *
     1032 * @param string $singular_key
     1033 * @param string $plural_key
     1034 * @param int $decimal
    9591035 * @return string
    9601036 */
    961 function l10n_dec($singular_fmt_key, $plural_fmt_key, $decimal)
     1037function l10n_dec($singular_key, $plural_key, $decimal)
    9621038{
    9631039  global $lang_info;
     
    9671043      l10n((
    9681044        (($decimal > 1) or ($decimal == 0 and $lang_info['zero_plural']))
    969           ? $plural_fmt_key
    970           : $singular_fmt_key
     1045          ? $plural_key
     1046          : $singular_key
    9711047        )), $decimal);
    9721048}
    9731049
    974 /*
     1050/**
    9751051 * returns a single element to use with l10n_args
    9761052 *
    977  * @param string key: translation key
    978  * @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)
    9791055 *   if args is a array, each values are used on sprintf
    9801056 * @return string
     
    9931069}
    9941070
    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
    10001078 * @return string
    10011079 */
     
    10351113
    10361114/**
    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
    10411118 * @return string
    10421119 */
     
    10721149 * Add configuration parameters from database to global $conf array
    10731150 *
     1151 * @param string $condition SQL condition
    10741152 * @return void
    10751153 */
     
    11101188/**
    11111189 * Add or update a config parameter
     1190 *
    11121191 * @param string $param
    11131192 * @param string $value
     
    11461225
    11471226/**
    1148  * Delete on or more config parameters
     1227 * Delete one or more config parameters
    11491228 * @since 2.6
     1229 *
    11501230 * @param string|string[] $params
    11511231 */
     
    11761256
    11771257/**
    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
    11831264 */
    11841265function prepend_append_array_items($array, $prepend_str, $append_str)
     
    11931274
    11941275/**
    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.
    11981278 *
    11991279 * @param string $query
     
    12161296
    12171297/**
    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
    12211300 *
    12221301 * @param string $query
     
    12361315
    12371316/**
    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
    12451321 */
    12461322function script_basename()
     
    12661342
    12671343/**
    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
    12741348 */
    12751349function get_filter_page_value($value_name)
     
    12941368
    12951369/**
    1296  * returns the character set of data sent to browsers / received from forms
     1370 * return the character set used by Piwigo
     1371 * @return string
    12971372 */
    12981373function get_pwg_charset()
     
    13071382
    13081383/**
    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
    13101386 * @since 2.6
     1387 *
    13111388 * @param string $lang_id
    1312  * @return string
     1389 * @return string|null
    13131390 */
    13141391function get_parent_language($lang_id=null)
     
    13331410/**
    13341411 * 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:
    13381414 *   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
    13431418 * @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 */
     1425function load_language($filename, $dirname = '', $options = array())
    13531426{
    13541427  global $user, $language_files;
     
    14841557/**
    14851558 * 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
    14891563 */
    14901564function convert_charset($str, $source_charset, $dest_charset)
     
    15081582    return mb_convert_encoding( $str, $dest_charset, $source_charset );
    15091583  }
    1510   return $str; //???
     1584  return $str; // TODO
    15111585}
    15121586
     
    15141588 * makes sure a index.htm protects the directory from browser file listing
    15151589 *
    1516  * @param string dir directory
     1590 * @param string $dir
    15171591 */
    15181592function secure_directory($dir)
     
    15281602 * returns a "secret key" that is to be sent back when a user posts a form
    15291603 *
    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
    15311607 */
    15321608function get_ephemeral_key($valid_after_seconds, $aditionnal_data_to_hash = '')
     
    15411617}
    15421618
     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 */
    15431626function verify_ephemeral_key($key, $aditionnal_data_to_hash = '')
    15441627{
     
    15611644/**
    15621645 * 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
    15631654 */
    15641655function create_navigation_bar($url, $nb_element, $start, $nb_element_page, $clean_url = false, $param_name='start')
     
    16171708/**
    16181709 * 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
    16191714 */
    16201715function get_icon($date, $is_child_date = false)
     
    16571752
    16581753/**
    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.
    16601755 * if pwg_token is empty action doesn't require token
    16611756 * else pwg_token is compare to server token
     
    16731768  }
    16741769  else
     1770  {
    16751771    bad_request('missing token');
    1676 }
    1677 
     1772  }
     1773}
     1774
     1775/**
     1776 * get pwg_token used to prevent csrf attacks
     1777 *
     1778 * @return string
     1779 */
    16781780function get_pwg_token()
    16791781{
     
    16871789 * pattern. This should happen only during hacking attempts.
    16881790 *
    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
    16961796 */
    16971797function check_input_parameter($param_name, $param_array, $is_array, $pattern, $mandatory=false)
     
    17371837}
    17381838
    1739 
     1839/**
     1840 * get localized privacy level values
     1841 *
     1842 * @return string[]
     1843 */
    17401844function get_privacy_level_options()
    17411845{
     
    17661870/**
    17671871 * 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
    17681875 */
    17691876function get_branch_from_version($version)
     
    17741881/**
    17751882 * return the device type: mobile, tablet or desktop
     1883 *
     1884 * @return string
    17761885 */
    17771886function get_device()
     
    18031912/**
    18041913 * return true if mobile theme should be loaded
     1914 *
     1915 * @return bool
    18051916 */
    18061917function mobile_theme()
     
    18341945/**
    18351946 * check url format
     1947 *
     1948 * @param string $url
     1949 * @return bool
    18361950 */
    18371951function url_check_format($url)
     
    18501964/**
    18511965 * check email format
     1966 *
     1967 * @param string $mail_address
     1968 * @return bool
    18521969 */
    18531970function email_check_format($mail_address)
     
    18671984}
    18681985
    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 */
    18701991function get_nb_available_comments()
    18711992{
Note: See TracChangeset for help on using the changeset viewer.