[2] | 1 | <?php |
---|
[362] | 2 | // +-----------------------------------------------------------------------+ |
---|
[8728] | 3 | // | Piwigo - a PHP based photo gallery | |
---|
[2297] | 4 | // +-----------------------------------------------------------------------+ |
---|
[19703] | 5 | // | Copyright(C) 2008-2013 Piwigo Team http://piwigo.org | |
---|
[2297] | 6 | // | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net | |
---|
| 7 | // | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick | |
---|
| 8 | // +-----------------------------------------------------------------------+ |
---|
| 9 | // | This program is free software; you can redistribute it and/or modify | |
---|
| 10 | // | it under the terms of the GNU General Public License as published by | |
---|
| 11 | // | the Free Software Foundation | |
---|
| 12 | // | | |
---|
| 13 | // | This program is distributed in the hope that it will be useful, but | |
---|
| 14 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
| 15 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
| 16 | // | General Public License for more details. | |
---|
| 17 | // | | |
---|
| 18 | // | You should have received a copy of the GNU General Public License | |
---|
| 19 | // | along with this program; if not, write to the Free Software | |
---|
| 20 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
| 21 | // | USA. | |
---|
| 22 | // +-----------------------------------------------------------------------+ |
---|
[405] | 23 | |
---|
[20325] | 24 | include_once( PHPWG_ROOT_PATH .'include/functions_plugins.inc.php' ); |
---|
[394] | 25 | include_once( PHPWG_ROOT_PATH .'include/functions_user.inc.php' ); |
---|
[1992] | 26 | include_once( PHPWG_ROOT_PATH .'include/functions_cookie.inc.php' ); |
---|
[394] | 27 | include_once( PHPWG_ROOT_PATH .'include/functions_session.inc.php' ); |
---|
| 28 | include_once( PHPWG_ROOT_PATH .'include/functions_category.inc.php' ); |
---|
[477] | 29 | include_once( PHPWG_ROOT_PATH .'include/functions_html.inc.php' ); |
---|
[1119] | 30 | include_once( PHPWG_ROOT_PATH .'include/functions_tag.inc.php' ); |
---|
[1109] | 31 | include_once( PHPWG_ROOT_PATH .'include/functions_url.inc.php' ); |
---|
[12798] | 32 | include_once( PHPWG_ROOT_PATH .'include/derivative_params.inc.php'); |
---|
| 33 | include_once( PHPWG_ROOT_PATH .'include/derivative_std_params.inc.php'); |
---|
| 34 | include_once( PHPWG_ROOT_PATH .'include/derivative.inc.php'); |
---|
[23384] | 35 | //require_once( PHPWG_ROOT_PATH .'include/smarty/libs/Smarty.class.php'); |
---|
| 36 | require_once( PHPWG_ROOT_PATH .'include/smarty/libs/SmartyBC.class.php'); |
---|
[12920] | 37 | include_once( PHPWG_ROOT_PATH .'include/template.class.php'); |
---|
[2] | 38 | |
---|
| 39 | //----------------------------------------------------------- generic functions |
---|
| 40 | |
---|
[8802] | 41 | /** |
---|
| 42 | * stupidly returns the current microsecond since Unix epoch |
---|
| 43 | */ |
---|
| 44 | function micro_seconds() |
---|
| 45 | { |
---|
| 46 | $t1 = explode(' ', microtime()); |
---|
| 47 | $t2 = explode('.', $t1[0]); |
---|
| 48 | $t2 = $t1[1].substr($t2[1], 0, 6); |
---|
| 49 | return $t2; |
---|
| 50 | } |
---|
| 51 | |
---|
[2] | 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 |
---|
| 55 | function get_moment() |
---|
| 56 | { |
---|
[12920] | 57 | return microtime(true); |
---|
[2] | 58 | } |
---|
| 59 | |
---|
| 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 | // - The replace_space function replaces space and '-' characters |
---|
| 68 | // by their HTML equivalent &nbsb; and − |
---|
| 69 | // - The function does not replace characters in HTML tags |
---|
| 70 | // - This function was created because IE5 does not respect the |
---|
| 71 | // CSS "white-space: nowrap;" property unless space and minus |
---|
| 72 | // characters are replaced like this function does. |
---|
[15] | 73 | // - Example : |
---|
| 74 | // <div class="foo">My friend</div> |
---|
| 75 | // ( 01234567891111111111222222222233 ) |
---|
| 76 | // ( 0123456789012345678901 ) |
---|
| 77 | // becomes : |
---|
| 78 | // <div class="foo">My friend</div> |
---|
[2] | 79 | function replace_space( $string ) |
---|
| 80 | { |
---|
[15] | 81 | //return $string; |
---|
| 82 | $return_string = ''; |
---|
| 83 | // $remaining is the rest of the string where to replace spaces characters |
---|
[2] | 84 | $remaining = $string; |
---|
[15] | 85 | // $start represents the position of the next '<' character |
---|
| 86 | // $end represents the position of the next '>' character |
---|
[3136] | 87 | ; // -> 0 |
---|
[15] | 88 | $end = strpos ( $remaining, '>' ); // -> 16 |
---|
| 89 | // as long as a '<' and his friend '>' are found, we loop |
---|
[3136] | 90 | while ( ($start=strpos( $remaining, '<' )) !==false |
---|
| 91 | and ($end=strpos( $remaining, '>' )) !== false ) |
---|
[2] | 92 | { |
---|
[15] | 93 | // $treatment is the part of the string to treat |
---|
| 94 | // In the first loop of our example, this variable is empty, but in the |
---|
| 95 | // second loop, it equals 'My friend' |
---|
[2] | 96 | $treatment = substr ( $remaining, 0, $start ); |
---|
[15] | 97 | // Replacement of ' ' by his equivalent ' ' |
---|
[6] | 98 | $treatment = str_replace( ' ', ' ', $treatment ); |
---|
| 99 | $treatment = str_replace( '-', '−', $treatment ); |
---|
[15] | 100 | // composing the string to return by adding the treated string and the |
---|
| 101 | // following HTML tag -> 'My friend</div>' |
---|
| 102 | $return_string.= $treatment.substr( $remaining, $start, $end-$start+1 ); |
---|
| 103 | // the remaining string is deplaced to the part after the '>' of this |
---|
| 104 | // loop |
---|
[2] | 105 | $remaining = substr ( $remaining, $end + 1, strlen( $remaining ) ); |
---|
| 106 | } |
---|
[6] | 107 | $treatment = str_replace( ' ', ' ', $remaining ); |
---|
| 108 | $treatment = str_replace( '-', '−', $treatment ); |
---|
[2] | 109 | $return_string.= $treatment; |
---|
[15] | 110 | |
---|
[2] | 111 | return $return_string; |
---|
| 112 | } |
---|
| 113 | |
---|
[13] | 114 | // get_extension returns the part of the string after the last "." |
---|
| 115 | function get_extension( $filename ) |
---|
| 116 | { |
---|
| 117 | return substr( strrchr( $filename, '.' ), 1, strlen ( $filename ) ); |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | // get_filename_wo_extension returns the part of the string before the last |
---|
| 121 | // ".". |
---|
| 122 | // get_filename_wo_extension( 'test.tar.gz' ) -> 'test.tar' |
---|
| 123 | function get_filename_wo_extension( $filename ) |
---|
| 124 | { |
---|
[1589] | 125 | $pos = strrpos( $filename, '.' ); |
---|
| 126 | return ($pos===false) ? $filename : substr( $filename, 0, $pos); |
---|
[13] | 127 | } |
---|
| 128 | |
---|
[2497] | 129 | define('MKGETDIR_NONE', 0); |
---|
| 130 | define('MKGETDIR_RECURSIVE', 1); |
---|
| 131 | define('MKGETDIR_DIE_ON_ERROR', 2); |
---|
| 132 | define('MKGETDIR_PROTECT_INDEX', 4); |
---|
| 133 | define('MKGETDIR_PROTECT_HTACCESS', 8); |
---|
| 134 | define('MKGETDIR_DEFAULT', 7); |
---|
[1631] | 135 | /** |
---|
[2497] | 136 | * creates directory if not exists; ensures that directory is writable |
---|
| 137 | * @param: |
---|
| 138 | * string $dir |
---|
| 139 | * int $flags combination of MKGETDIR_xxx |
---|
| 140 | * @return bool false on error else true |
---|
| 141 | */ |
---|
| 142 | function mkgetdir($dir, $flags=MKGETDIR_DEFAULT) |
---|
| 143 | { |
---|
| 144 | if ( !is_dir($dir) ) |
---|
| 145 | { |
---|
[12796] | 146 | global $conf; |
---|
[6385] | 147 | if (substr(PHP_OS, 0, 3) == 'WIN') |
---|
| 148 | { |
---|
| 149 | $dir = str_replace('/', DIRECTORY_SEPARATOR, $dir); |
---|
| 150 | } |
---|
[2497] | 151 | $umask = umask(0); |
---|
[12796] | 152 | $mkd = @mkdir($dir, $conf['chmod_value'], ($flags&MKGETDIR_RECURSIVE) ? true:false ); |
---|
[2497] | 153 | umask($umask); |
---|
| 154 | if ($mkd==false) |
---|
| 155 | { |
---|
[5021] | 156 | !($flags&MKGETDIR_DIE_ON_ERROR) or fatal_error( "$dir ".l10n('no write access')); |
---|
[2497] | 157 | return false; |
---|
| 158 | } |
---|
| 159 | if( $flags&MKGETDIR_PROTECT_HTACCESS ) |
---|
| 160 | { |
---|
| 161 | $file = $dir.'/.htaccess'; |
---|
| 162 | file_exists($file) or @file_put_contents( $file, 'deny from all' ); |
---|
| 163 | } |
---|
| 164 | if( $flags&MKGETDIR_PROTECT_INDEX ) |
---|
| 165 | { |
---|
| 166 | $file = $dir.'/index.htm'; |
---|
| 167 | file_exists($file) or @file_put_contents( $file, 'Not allowed!' ); |
---|
| 168 | } |
---|
| 169 | } |
---|
| 170 | if ( !is_writable($dir) ) |
---|
| 171 | { |
---|
[5021] | 172 | !($flags&MKGETDIR_DIE_ON_ERROR) or fatal_error( "$dir ".l10n('no write access')); |
---|
[3136] | 173 | return false; |
---|
[2497] | 174 | } |
---|
| 175 | return true; |
---|
| 176 | } |
---|
| 177 | |
---|
[17748] | 178 | /* returns 0 if $str is Ascii, 1 if utf-8, -1 otherwise */ |
---|
| 179 | function qualify_utf8($Str) |
---|
| 180 | { |
---|
| 181 | $ret = 0; |
---|
[2123] | 182 | for ($i=0; $i<strlen($Str); $i++) { |
---|
| 183 | if (ord($Str[$i]) < 0x80) continue; # 0bbbbbbb |
---|
[17748] | 184 | $ret = 1; |
---|
| 185 | if ((ord($Str[$i]) & 0xE0) == 0xC0) $n=1; # 110bbbbb |
---|
[2123] | 186 | elseif ((ord($Str[$i]) & 0xF0) == 0xE0) $n=2; # 1110bbbb |
---|
| 187 | elseif ((ord($Str[$i]) & 0xF8) == 0xF0) $n=3; # 11110bbb |
---|
| 188 | elseif ((ord($Str[$i]) & 0xFC) == 0xF8) $n=4; # 111110bb |
---|
| 189 | elseif ((ord($Str[$i]) & 0xFE) == 0xFC) $n=5; # 1111110b |
---|
[17748] | 190 | else return -1; # Does not match any model |
---|
[2123] | 191 | for ($j=0; $j<$n; $j++) { # n bytes matching 10bbbbbb follow ? |
---|
| 192 | if ((++$i == strlen($Str)) || ((ord($Str[$i]) & 0xC0) != 0x80)) |
---|
[17748] | 193 | return -1; |
---|
[2123] | 194 | } |
---|
| 195 | } |
---|
[17748] | 196 | return $ret; |
---|
[2123] | 197 | } |
---|
| 198 | |
---|
| 199 | /* Remove accents from a UTF-8 or ISO-859-1 string (from wordpress) |
---|
| 200 | * @param string sstring - an UTF-8 or ISO-8859-1 string |
---|
| 201 | */ |
---|
| 202 | function remove_accents($string) |
---|
| 203 | { |
---|
[17748] | 204 | $utf = qualify_utf8($string); |
---|
| 205 | if ( $utf == 0 ) |
---|
| 206 | return $string; // ascii |
---|
[2123] | 207 | |
---|
[17748] | 208 | if ( $utf > 0 ) { |
---|
[2123] | 209 | $chars = array( |
---|
| 210 | // Decompositions for Latin-1 Supplement |
---|
[6947] | 211 | "\xc3\x80"=>'A', "\xc3\x81"=>'A', |
---|
| 212 | "\xc3\x82"=>'A', "\xc3\x83"=>'A', |
---|
| 213 | "\xc3\x84"=>'A', "\xc3\x85"=>'A', |
---|
| 214 | "\xc3\x87"=>'C', "\xc3\x88"=>'E', |
---|
| 215 | "\xc3\x89"=>'E', "\xc3\x8a"=>'E', |
---|
| 216 | "\xc3\x8b"=>'E', "\xc3\x8c"=>'I', |
---|
| 217 | "\xc3\x8d"=>'I', "\xc3\x8e"=>'I', |
---|
| 218 | "\xc3\x8f"=>'I', "\xc3\x91"=>'N', |
---|
| 219 | "\xc3\x92"=>'O', "\xc3\x93"=>'O', |
---|
| 220 | "\xc3\x94"=>'O', "\xc3\x95"=>'O', |
---|
| 221 | "\xc3\x96"=>'O', "\xc3\x99"=>'U', |
---|
| 222 | "\xc3\x9a"=>'U', "\xc3\x9b"=>'U', |
---|
| 223 | "\xc3\x9c"=>'U', "\xc3\x9d"=>'Y', |
---|
| 224 | "\xc3\x9f"=>'s', "\xc3\xa0"=>'a', |
---|
| 225 | "\xc3\xa1"=>'a', "\xc3\xa2"=>'a', |
---|
| 226 | "\xc3\xa3"=>'a', "\xc3\xa4"=>'a', |
---|
| 227 | "\xc3\xa5"=>'a', "\xc3\xa7"=>'c', |
---|
| 228 | "\xc3\xa8"=>'e', "\xc3\xa9"=>'e', |
---|
| 229 | "\xc3\xaa"=>'e', "\xc3\xab"=>'e', |
---|
| 230 | "\xc3\xac"=>'i', "\xc3\xad"=>'i', |
---|
| 231 | "\xc3\xae"=>'i', "\xc3\xaf"=>'i', |
---|
| 232 | "\xc3\xb1"=>'n', "\xc3\xb2"=>'o', |
---|
| 233 | "\xc3\xb3"=>'o', "\xc3\xb4"=>'o', |
---|
| 234 | "\xc3\xb5"=>'o', "\xc3\xb6"=>'o', |
---|
| 235 | "\xc3\xb9"=>'u', "\xc3\xba"=>'u', |
---|
| 236 | "\xc3\xbb"=>'u', "\xc3\xbc"=>'u', |
---|
| 237 | "\xc3\xbd"=>'y', "\xc3\xbf"=>'y', |
---|
[2123] | 238 | // Decompositions for Latin Extended-A |
---|
[6947] | 239 | "\xc4\x80"=>'A', "\xc4\x81"=>'a', |
---|
| 240 | "\xc4\x82"=>'A', "\xc4\x83"=>'a', |
---|
| 241 | "\xc4\x84"=>'A', "\xc4\x85"=>'a', |
---|
| 242 | "\xc4\x86"=>'C', "\xc4\x87"=>'c', |
---|
| 243 | "\xc4\x88"=>'C', "\xc4\x89"=>'c', |
---|
| 244 | "\xc4\x8a"=>'C', "\xc4\x8b"=>'c', |
---|
| 245 | "\xc4\x8c"=>'C', "\xc4\x8d"=>'c', |
---|
| 246 | "\xc4\x8e"=>'D', "\xc4\x8f"=>'d', |
---|
| 247 | "\xc4\x90"=>'D', "\xc4\x91"=>'d', |
---|
| 248 | "\xc4\x92"=>'E', "\xc4\x93"=>'e', |
---|
| 249 | "\xc4\x94"=>'E', "\xc4\x95"=>'e', |
---|
| 250 | "\xc4\x96"=>'E', "\xc4\x97"=>'e', |
---|
| 251 | "\xc4\x98"=>'E', "\xc4\x99"=>'e', |
---|
| 252 | "\xc4\x9a"=>'E', "\xc4\x9b"=>'e', |
---|
| 253 | "\xc4\x9c"=>'G', "\xc4\x9d"=>'g', |
---|
| 254 | "\xc4\x9e"=>'G', "\xc4\x9f"=>'g', |
---|
| 255 | "\xc4\xa0"=>'G', "\xc4\xa1"=>'g', |
---|
| 256 | "\xc4\xa2"=>'G', "\xc4\xa3"=>'g', |
---|
| 257 | "\xc4\xa4"=>'H', "\xc4\xa5"=>'h', |
---|
| 258 | "\xc4\xa6"=>'H', "\xc4\xa7"=>'h', |
---|
| 259 | "\xc4\xa8"=>'I', "\xc4\xa9"=>'i', |
---|
| 260 | "\xc4\xaa"=>'I', "\xc4\xab"=>'i', |
---|
| 261 | "\xc4\xac"=>'I', "\xc4\xad"=>'i', |
---|
| 262 | "\xc4\xae"=>'I', "\xc4\xaf"=>'i', |
---|
| 263 | "\xc4\xb0"=>'I', "\xc4\xb1"=>'i', |
---|
| 264 | "\xc4\xb2"=>'IJ', "\xc4\xb3"=>'ij', |
---|
| 265 | "\xc4\xb4"=>'J', "\xc4\xb5"=>'j', |
---|
| 266 | "\xc4\xb6"=>'K', "\xc4\xb7"=>'k', |
---|
| 267 | "\xc4\xb8"=>'k', "\xc4\xb9"=>'L', |
---|
| 268 | "\xc4\xba"=>'l', "\xc4\xbb"=>'L', |
---|
| 269 | "\xc4\xbc"=>'l', "\xc4\xbd"=>'L', |
---|
| 270 | "\xc4\xbe"=>'l', "\xc4\xbf"=>'L', |
---|
| 271 | "\xc5\x80"=>'l', "\xc5\x81"=>'L', |
---|
| 272 | "\xc5\x82"=>'l', "\xc5\x83"=>'N', |
---|
| 273 | "\xc5\x84"=>'n', "\xc5\x85"=>'N', |
---|
| 274 | "\xc5\x86"=>'n', "\xc5\x87"=>'N', |
---|
| 275 | "\xc5\x88"=>'n', "\xc5\x89"=>'N', |
---|
| 276 | "\xc5\x8a"=>'n', "\xc5\x8b"=>'N', |
---|
| 277 | "\xc5\x8c"=>'O', "\xc5\x8d"=>'o', |
---|
| 278 | "\xc5\x8e"=>'O', "\xc5\x8f"=>'o', |
---|
| 279 | "\xc5\x90"=>'O', "\xc5\x91"=>'o', |
---|
| 280 | "\xc5\x92"=>'OE', "\xc5\x93"=>'oe', |
---|
| 281 | "\xc5\x94"=>'R', "\xc5\x95"=>'r', |
---|
| 282 | "\xc5\x96"=>'R', "\xc5\x97"=>'r', |
---|
| 283 | "\xc5\x98"=>'R', "\xc5\x99"=>'r', |
---|
| 284 | "\xc5\x9a"=>'S', "\xc5\x9b"=>'s', |
---|
| 285 | "\xc5\x9c"=>'S', "\xc5\x9d"=>'s', |
---|
| 286 | "\xc5\x9e"=>'S', "\xc5\x9f"=>'s', |
---|
| 287 | "\xc5\xa0"=>'S', "\xc5\xa1"=>'s', |
---|
| 288 | "\xc5\xa2"=>'T', "\xc5\xa3"=>'t', |
---|
| 289 | "\xc5\xa4"=>'T', "\xc5\xa5"=>'t', |
---|
| 290 | "\xc5\xa6"=>'T', "\xc5\xa7"=>'t', |
---|
| 291 | "\xc5\xa8"=>'U', "\xc5\xa9"=>'u', |
---|
| 292 | "\xc5\xaa"=>'U', "\xc5\xab"=>'u', |
---|
| 293 | "\xc5\xac"=>'U', "\xc5\xad"=>'u', |
---|
| 294 | "\xc5\xae"=>'U', "\xc5\xaf"=>'u', |
---|
| 295 | "\xc5\xb0"=>'U', "\xc5\xb1"=>'u', |
---|
| 296 | "\xc5\xb2"=>'U', "\xc5\xb3"=>'u', |
---|
| 297 | "\xc5\xb4"=>'W', "\xc5\xb5"=>'w', |
---|
| 298 | "\xc5\xb6"=>'Y', "\xc5\xb7"=>'y', |
---|
| 299 | "\xc5\xb8"=>'Y', "\xc5\xb9"=>'Z', |
---|
| 300 | "\xc5\xba"=>'z', "\xc5\xbb"=>'Z', |
---|
| 301 | "\xc5\xbc"=>'z', "\xc5\xbd"=>'Z', |
---|
| 302 | "\xc5\xbe"=>'z', "\xc5\xbf"=>'s', |
---|
[17748] | 303 | // Decompositions for Latin Extended-B |
---|
| 304 | "\xc8\x98"=>'S', "\xc8\x99"=>'s', |
---|
| 305 | "\xc8\x9a"=>'T', "\xc8\x9b"=>'t', |
---|
[2123] | 306 | // Euro Sign |
---|
[6947] | 307 | "\xe2\x82\xac"=>'E', |
---|
[2123] | 308 | // GBP (Pound) Sign |
---|
[6947] | 309 | "\xc2\xa3"=>''); |
---|
[2123] | 310 | |
---|
| 311 | $string = strtr($string, $chars); |
---|
| 312 | } else { |
---|
| 313 | // Assume ISO-8859-1 if not UTF-8 |
---|
| 314 | $chars['in'] = chr(128).chr(131).chr(138).chr(142).chr(154).chr(158) |
---|
| 315 | .chr(159).chr(162).chr(165).chr(181).chr(192).chr(193).chr(194) |
---|
| 316 | .chr(195).chr(196).chr(197).chr(199).chr(200).chr(201).chr(202) |
---|
| 317 | .chr(203).chr(204).chr(205).chr(206).chr(207).chr(209).chr(210) |
---|
| 318 | .chr(211).chr(212).chr(213).chr(214).chr(216).chr(217).chr(218) |
---|
| 319 | .chr(219).chr(220).chr(221).chr(224).chr(225).chr(226).chr(227) |
---|
| 320 | .chr(228).chr(229).chr(231).chr(232).chr(233).chr(234).chr(235) |
---|
| 321 | .chr(236).chr(237).chr(238).chr(239).chr(241).chr(242).chr(243) |
---|
| 322 | .chr(244).chr(245).chr(246).chr(248).chr(249).chr(250).chr(251) |
---|
| 323 | .chr(252).chr(253).chr(255); |
---|
| 324 | |
---|
| 325 | $chars['out'] = "EfSZszYcYuAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy"; |
---|
| 326 | |
---|
| 327 | $string = strtr($string, $chars['in'], $chars['out']); |
---|
| 328 | $double_chars['in'] = array(chr(140), chr(156), chr(198), chr(208), chr(222), chr(223), chr(230), chr(240), chr(254)); |
---|
| 329 | $double_chars['out'] = array('OE', 'oe', 'AE', 'DH', 'TH', 'ss', 'ae', 'dh', 'th'); |
---|
| 330 | $string = str_replace($double_chars['in'], $double_chars['out'], $string); |
---|
| 331 | } |
---|
| 332 | |
---|
| 333 | return $string; |
---|
| 334 | } |
---|
| 335 | |
---|
[17748] | 336 | if (function_exists('mb_strtolower') && defined('PWG_CHARSET')) |
---|
| 337 | { |
---|
| 338 | function transliterate($term) |
---|
| 339 | { |
---|
| 340 | return remove_accents( mb_strtolower($term, PWG_CHARSET) ); |
---|
| 341 | } |
---|
| 342 | } |
---|
| 343 | else |
---|
| 344 | { |
---|
| 345 | function transliterate($term) |
---|
| 346 | { |
---|
| 347 | return remove_accents( strtolower($term) ); |
---|
| 348 | } |
---|
| 349 | } |
---|
| 350 | |
---|
| 351 | |
---|
| 352 | |
---|
[1119] | 353 | /** |
---|
| 354 | * simplify a string to insert it into an URL |
---|
| 355 | * |
---|
| 356 | * @param string |
---|
| 357 | * @return string |
---|
| 358 | */ |
---|
| 359 | function str2url($str) |
---|
| 360 | { |
---|
[17748] | 361 | $str = $safe = transliterate($str); |
---|
| 362 | $str = preg_replace('/[^\x80-\xffa-z0-9_\s\'\:\/\[\],-]/','',$str); |
---|
[1131] | 363 | $str = preg_replace('/[\s\'\:\/\[\],-]+/',' ',trim($str)); |
---|
[1119] | 364 | $res = str_replace(' ','_',$str); |
---|
[1131] | 365 | |
---|
[6060] | 366 | if (empty($res)) |
---|
| 367 | { |
---|
[17748] | 368 | $res = str_replace(' ','_', $safe); |
---|
[6060] | 369 | } |
---|
| 370 | |
---|
[1119] | 371 | return $res; |
---|
| 372 | } |
---|
| 373 | |
---|
[2339] | 374 | //-------------------------------------------- Piwigo specific functions |
---|
[2] | 375 | |
---|
[512] | 376 | /** |
---|
| 377 | * returns an array with a list of {language_code => language_name} |
---|
| 378 | * |
---|
| 379 | * @returns array |
---|
| 380 | */ |
---|
[5357] | 381 | function get_languages() |
---|
[2] | 382 | { |
---|
[5357] | 383 | $query = ' |
---|
| 384 | SELECT id, name |
---|
| 385 | FROM '.LANGUAGES_TABLE.' |
---|
| 386 | ORDER BY name ASC |
---|
| 387 | ;'; |
---|
| 388 | $result = pwg_query($query); |
---|
[2127] | 389 | |
---|
[2] | 390 | $languages = array(); |
---|
[5357] | 391 | while ($row = pwg_db_fetch_assoc($result)) |
---|
[2] | 392 | { |
---|
[5357] | 393 | if (is_dir(PHPWG_ROOT_PATH.'language/'.$row['id'])) |
---|
[2] | 394 | { |
---|
[5357] | 395 | $languages[ $row['id'] ] = $row['name']; |
---|
[2] | 396 | } |
---|
| 397 | } |
---|
[512] | 398 | |
---|
[2] | 399 | return $languages; |
---|
| 400 | } |
---|
| 401 | |
---|
[1844] | 402 | function pwg_log($image_id = null, $image_type = null) |
---|
[2] | 403 | { |
---|
[1727] | 404 | global $conf, $user, $page; |
---|
[2] | 405 | |
---|
[2620] | 406 | $do_log = $conf['log']; |
---|
| 407 | if (is_admin()) |
---|
[2] | 408 | { |
---|
[2620] | 409 | $do_log = $conf['history_admin']; |
---|
[1565] | 410 | } |
---|
[2620] | 411 | if (is_a_guest()) |
---|
[1565] | 412 | { |
---|
[2620] | 413 | $do_log = $conf['history_guest']; |
---|
[1565] | 414 | } |
---|
[1880] | 415 | |
---|
| 416 | $do_log = trigger_event('pwg_log_allowed', $do_log, $image_id, $image_type); |
---|
[2089] | 417 | |
---|
[1880] | 418 | if (!$do_log) |
---|
| 419 | { |
---|
[1727] | 420 | return false; |
---|
[1565] | 421 | } |
---|
| 422 | |
---|
[1727] | 423 | $tags_string = null; |
---|
[3136] | 424 | if ('tags'==@$page['section']) |
---|
[1565] | 425 | { |
---|
[3136] | 426 | $tags_string = implode(',', $page['tag_ids']); |
---|
[1565] | 427 | } |
---|
| 428 | |
---|
| 429 | $query = ' |
---|
[725] | 430 | INSERT INTO '.HISTORY_TABLE.' |
---|
[1727] | 431 | ( |
---|
| 432 | date, |
---|
| 433 | time, |
---|
| 434 | user_id, |
---|
| 435 | IP, |
---|
| 436 | section, |
---|
| 437 | category_id, |
---|
| 438 | image_id, |
---|
[1844] | 439 | image_type, |
---|
[1727] | 440 | tag_ids |
---|
| 441 | ) |
---|
[725] | 442 | VALUES |
---|
[1727] | 443 | ( |
---|
[4367] | 444 | CURRENT_DATE, |
---|
| 445 | CURRENT_TIME, |
---|
[1727] | 446 | '.$user['id'].', |
---|
| 447 | \''.$_SERVER['REMOTE_ADDR'].'\', |
---|
| 448 | '.(isset($page['section']) ? "'".$page['section']."'" : 'NULL').', |
---|
[2221] | 449 | '.(isset($page['category']['id']) ? $page['category']['id'] : 'NULL').', |
---|
[1727] | 450 | '.(isset($image_id) ? $image_id : 'NULL').', |
---|
[2086] | 451 | '.(isset($image_type) ? "'".$image_type."'" : 'NULL').', |
---|
[1727] | 452 | '.(isset($tags_string) ? "'".$tags_string."'" : 'NULL').' |
---|
| 453 | ) |
---|
[725] | 454 | ;'; |
---|
[1565] | 455 | pwg_query($query); |
---|
[1727] | 456 | |
---|
| 457 | return true; |
---|
[2] | 458 | } |
---|
| 459 | |
---|
[24099] | 460 | /** |
---|
| 461 | * converts a string into a DateTime object |
---|
| 462 | * @param: mixed, datetime string or timestamp int |
---|
| 463 | * @param: string, input format |
---|
| 464 | * @return: DateTime or false |
---|
| 465 | */ |
---|
| 466 | function str2DateTime($original, $format=null) |
---|
[61] | 467 | { |
---|
[24099] | 468 | if (!empty($format))// from known date format |
---|
[4966] | 469 | { |
---|
[24099] | 470 | return DateTime::createFromFormat('!'.$format, $original); // ! char to reset fields to UNIX epoch |
---|
[4966] | 471 | } |
---|
[24099] | 472 | else |
---|
[3117] | 473 | { |
---|
[24099] | 474 | $date = new DateTime(); |
---|
| 475 | |
---|
| 476 | $t = trim($original, '0123456789'); |
---|
| 477 | if (empty($t)) // from timestamp |
---|
| 478 | { |
---|
| 479 | $date->setTimestamp($original); |
---|
| 480 | } |
---|
| 481 | else // from unknown date format (assuming something like Y-m-d H:i:s) |
---|
| 482 | { |
---|
| 483 | $ymdhms = array(); |
---|
| 484 | $tok = strtok($original, '- :/'); |
---|
| 485 | while ($tok !== false) |
---|
| 486 | { |
---|
| 487 | $ymdhms[] = $tok; |
---|
| 488 | $tok = strtok('- :/'); |
---|
| 489 | } |
---|
| 490 | |
---|
| 491 | if (count($ymdhms)<3) return false; |
---|
| 492 | if (!isset($ymdhms[3])) $ymdhms[3] = 0; |
---|
| 493 | if (!isset($ymdhms[4])) $ymdhms[4] = 0; |
---|
| 494 | if (!isset($ymdhms[5])) $ymdhms[5] = 0; |
---|
| 495 | |
---|
| 496 | $date->setDate($ymdhms[0], $ymdhms[1], $ymdhms[2]); |
---|
| 497 | $date->setTime($ymdhms[3], $ymdhms[4], $ymdhms[5]); |
---|
| 498 | } |
---|
| 499 | |
---|
| 500 | return $date; |
---|
[3117] | 501 | } |
---|
[24099] | 502 | } |
---|
[1086] | 503 | |
---|
[24099] | 504 | /** |
---|
| 505 | * returns a formatted date for display |
---|
| 506 | * @param: mixed, datetime string or timestamp int |
---|
| 507 | * @param: bool, show time |
---|
| 508 | * @param: bool, show day name |
---|
| 509 | * @param: string, input format |
---|
| 510 | * @return: string |
---|
| 511 | */ |
---|
| 512 | function format_date($original, $show_time=false, $show_day_name=true, $format=null) |
---|
| 513 | { |
---|
| 514 | global $lang; |
---|
| 515 | |
---|
| 516 | $date = str2DateTime($original, $format); |
---|
[3117] | 517 | |
---|
[24099] | 518 | if (!$date) |
---|
[61] | 519 | { |
---|
[24099] | 520 | return l10n('N/A'); |
---|
[61] | 521 | } |
---|
[15573] | 522 | |
---|
[24099] | 523 | $print = ''; |
---|
| 524 | if ($show_day_name) |
---|
[15573] | 525 | { |
---|
[24099] | 526 | $print.= $lang['day'][ $date->format('w') ]; |
---|
[15573] | 527 | } |
---|
[24099] | 528 | |
---|
| 529 | $print.= ' '.$date->format('d'); |
---|
| 530 | $print.= ' '.$lang['month'][ $date->format('n') ]; |
---|
| 531 | $print.= ' '.$date->format('Y'); |
---|
| 532 | |
---|
| 533 | if ($show_time) |
---|
[15573] | 534 | { |
---|
[24099] | 535 | $temp = $date->format('H:i'); |
---|
| 536 | if ($temp != '00:00') |
---|
| 537 | { |
---|
| 538 | $print.= ' '.$temp; |
---|
| 539 | } |
---|
[15573] | 540 | } |
---|
[18729] | 541 | |
---|
[24099] | 542 | return trim($print); |
---|
[61] | 543 | } |
---|
[85] | 544 | |
---|
[13085] | 545 | /** |
---|
[24099] | 546 | * Works out the time since the given date |
---|
| 547 | * @param: mixed, datetime string or timestamp int |
---|
| 548 | * @param: string, stop (year,month,week,day,hour,minute,second) |
---|
| 549 | * @param: string, input format |
---|
| 550 | * @param: bool, append text ("ago" or "in the future") |
---|
| 551 | * @param: bool, display weeks |
---|
| 552 | * @return: string |
---|
[13085] | 553 | */ |
---|
[24099] | 554 | function time_since($original, $stop='minute', $format=null, $with_text=true, $with_week=true) |
---|
[13085] | 555 | { |
---|
[24099] | 556 | $date = str2DateTime($original, $format); |
---|
| 557 | |
---|
| 558 | if (!$date) |
---|
[13085] | 559 | { |
---|
[24099] | 560 | return l10n('N/A'); |
---|
[13085] | 561 | } |
---|
[24099] | 562 | |
---|
| 563 | $now = new DateTime(); |
---|
| 564 | $diff = $now->diff($date); |
---|
| 565 | |
---|
| 566 | if ($with_week) |
---|
| 567 | { |
---|
| 568 | // DateInterval does not compute the number of weeks |
---|
| 569 | $diff->w = (int)floor($diff->d/7); |
---|
| 570 | $diff->d = $diff->d - $diff->w*7; |
---|
| 571 | } |
---|
| 572 | else |
---|
| 573 | { |
---|
| 574 | $diff->w = 0; |
---|
| 575 | } |
---|
| 576 | |
---|
[13085] | 577 | $chunks = array( |
---|
[24099] | 578 | 'year' => 'y', |
---|
| 579 | 'month' => 'm', |
---|
| 580 | 'week' => 'w', |
---|
| 581 | 'day' => 'd', |
---|
| 582 | 'hour' => 'h', |
---|
| 583 | 'minute' => 'i', |
---|
| 584 | 'second' => 's', |
---|
[13085] | 585 | ); |
---|
[24099] | 586 | |
---|
| 587 | $j = array_search($stop, array_keys($chunks)); |
---|
| 588 | |
---|
| 589 | $print = ''; $i=0; |
---|
| 590 | foreach ($chunks as $name => $var) |
---|
[13085] | 591 | { |
---|
[24099] | 592 | if ($diff->{$var} != 0) |
---|
[13085] | 593 | { |
---|
[24099] | 594 | $print.= ' '.l10n_dec('%d '.$name, '%d '.$name.'s', $diff->{$var}); |
---|
[13085] | 595 | } |
---|
[24099] | 596 | if (!empty($print) && $i >= $j) |
---|
[13085] | 597 | { |
---|
| 598 | break; |
---|
| 599 | } |
---|
[24099] | 600 | $i++; |
---|
[13085] | 601 | } |
---|
[24099] | 602 | |
---|
| 603 | $print = trim($print); |
---|
| 604 | |
---|
| 605 | if ($with_text) |
---|
[13085] | 606 | { |
---|
[24099] | 607 | if ($diff->invert) |
---|
| 608 | { |
---|
| 609 | $print = sprintf(l10n('%s ago'), $print); |
---|
| 610 | } |
---|
| 611 | else |
---|
| 612 | { |
---|
| 613 | $print = sprintf(l10n('%s in the future'), $print); |
---|
| 614 | } |
---|
[13085] | 615 | } |
---|
[18729] | 616 | |
---|
[13085] | 617 | return $print; |
---|
| 618 | } |
---|
| 619 | |
---|
[24099] | 620 | /** |
---|
| 621 | * transform a date string from a format to another (MySQL to d/M/Y for instance) |
---|
| 622 | * @param: string, date |
---|
| 623 | * @param: string, input format |
---|
| 624 | * @param: string, output format |
---|
| 625 | * @param: string, default value if inout is empty |
---|
| 626 | * @return: string |
---|
| 627 | */ |
---|
| 628 | function transform_date($original, $format_in, $format_out, $default=null) |
---|
| 629 | { |
---|
| 630 | if (empty($original)) return $default; |
---|
| 631 | $date = str2DateTime($original, $format_in); |
---|
| 632 | return $date->format($format_out); |
---|
| 633 | } |
---|
| 634 | |
---|
[345] | 635 | function pwg_debug( $string ) |
---|
| 636 | { |
---|
[1033] | 637 | global $debug,$t2,$page; |
---|
[345] | 638 | |
---|
| 639 | $now = explode( ' ', microtime() ); |
---|
| 640 | $now2 = explode( '.', $now[0] ); |
---|
| 641 | $now2 = $now[1].'.'.$now2[1]; |
---|
| 642 | $time = number_format( $now2 - $t2, 3, '.', ' ').' s'; |
---|
[1012] | 643 | $debug .= '<p>'; |
---|
[345] | 644 | $debug.= '['.$time.', '; |
---|
[1033] | 645 | $debug.= $page['count_queries'].' queries] : '.$string; |
---|
[1012] | 646 | $debug.= "</p>\n"; |
---|
[345] | 647 | } |
---|
[351] | 648 | |
---|
[405] | 649 | /** |
---|
[1649] | 650 | * Redirects to the given URL (HTTP method) |
---|
[405] | 651 | * |
---|
| 652 | * Note : once this function called, the execution doesn't go further |
---|
| 653 | * (presence of an exit() instruction. |
---|
| 654 | * |
---|
| 655 | * @param string $url |
---|
[1649] | 656 | * @return void |
---|
| 657 | */ |
---|
| 658 | function redirect_http( $url ) |
---|
| 659 | { |
---|
| 660 | if (ob_get_length () !== FALSE) |
---|
| 661 | { |
---|
| 662 | ob_clean(); |
---|
| 663 | } |
---|
[2218] | 664 | // default url is on html format |
---|
| 665 | $url = html_entity_decode($url); |
---|
[1649] | 666 | header('Request-URI: '.$url); |
---|
| 667 | header('Content-Location: '.$url); |
---|
| 668 | header('Location: '.$url); |
---|
| 669 | exit(); |
---|
| 670 | } |
---|
| 671 | |
---|
| 672 | /** |
---|
| 673 | * Redirects to the given URL (HTML method) |
---|
| 674 | * |
---|
| 675 | * Note : once this function called, the execution doesn't go further |
---|
| 676 | * (presence of an exit() instruction. |
---|
| 677 | * |
---|
| 678 | * @param string $url |
---|
[1156] | 679 | * @param string $title_msg |
---|
| 680 | * @param integer $refreh_time |
---|
[405] | 681 | * @return void |
---|
| 682 | */ |
---|
[1649] | 683 | function redirect_html( $url , $msg = '', $refresh_time = 0) |
---|
[405] | 684 | { |
---|
[1567] | 685 | global $user, $template, $lang_info, $conf, $lang, $t2, $page, $debug; |
---|
[405] | 686 | |
---|
[6668] | 687 | if (!isset($lang_info) || !isset($template) ) |
---|
[1568] | 688 | { |
---|
| 689 | $user = build_user( $conf['guest_id'], true); |
---|
[2126] | 690 | load_language('common.lang'); |
---|
[1699] | 691 | trigger_action('loading_lang'); |
---|
[8722] | 692 | load_language('lang', PHPWG_ROOT_PATH.PWG_LOCAL_DIR, array('no_fallback'=>true, 'local'=>true) ); |
---|
[5123] | 693 | $template = new Template(PHPWG_ROOT_PATH.'themes', get_default_theme()); |
---|
[1298] | 694 | } |
---|
[6944] | 695 | elseif (defined('IN_ADMIN') and IN_ADMIN) |
---|
| 696 | { |
---|
| 697 | $template = new Template(PHPWG_ROOT_PATH.'themes', get_default_theme()); |
---|
| 698 | } |
---|
[1298] | 699 | |
---|
[1508] | 700 | if (empty($msg)) |
---|
[1156] | 701 | { |
---|
[5021] | 702 | $msg = nl2br(l10n('Redirection...')); |
---|
[1156] | 703 | } |
---|
[688] | 704 | |
---|
[1567] | 705 | $refresh = $refresh_time; |
---|
| 706 | $url_link = $url; |
---|
| 707 | $title = 'redirection'; |
---|
[1086] | 708 | |
---|
[1567] | 709 | $template->set_filenames( array( 'redirect' => 'redirect.tpl' ) ); |
---|
| 710 | |
---|
| 711 | include( PHPWG_ROOT_PATH.'include/page_header.php' ); |
---|
| 712 | |
---|
| 713 | $template->set_filenames( array( 'redirect' => 'redirect.tpl' ) ); |
---|
[2521] | 714 | $template->assign('REDIRECT_MSG', $msg); |
---|
| 715 | |
---|
[688] | 716 | $template->parse('redirect'); |
---|
[1567] | 717 | |
---|
| 718 | include( PHPWG_ROOT_PATH.'include/page_tail.php' ); |
---|
| 719 | |
---|
[405] | 720 | exit(); |
---|
| 721 | } |
---|
[507] | 722 | |
---|
| 723 | /** |
---|
[1649] | 724 | * Redirects to the given URL (Switch to HTTP method or HTML method) |
---|
| 725 | * |
---|
| 726 | * Note : once this function called, the execution doesn't go further |
---|
| 727 | * (presence of an exit() instruction. |
---|
| 728 | * |
---|
| 729 | * @param string $url |
---|
| 730 | * @param string $title_msg |
---|
| 731 | * @param integer $refreh_time |
---|
| 732 | * @return void |
---|
| 733 | */ |
---|
| 734 | function redirect( $url , $msg = '', $refresh_time = 0) |
---|
| 735 | { |
---|
| 736 | global $conf; |
---|
| 737 | |
---|
| 738 | // with RefeshTime <> 0, only html must be used |
---|
[1846] | 739 | if ($conf['default_redirect_method']=='http' |
---|
| 740 | and $refresh_time==0 |
---|
| 741 | and !headers_sent() |
---|
| 742 | ) |
---|
[1649] | 743 | { |
---|
| 744 | redirect_http($url); |
---|
| 745 | } |
---|
| 746 | else |
---|
| 747 | { |
---|
| 748 | redirect_html($url, $msg, $refresh_time); |
---|
| 749 | } |
---|
| 750 | } |
---|
| 751 | |
---|
| 752 | /** |
---|
[507] | 753 | * returns $_SERVER['QUERY_STRING'] whitout keys given in parameters |
---|
| 754 | * |
---|
| 755 | * @param array $rejects |
---|
[2089] | 756 | * @param boolean $escape - if true escape & to & (for html) |
---|
[507] | 757 | * @returns string |
---|
| 758 | */ |
---|
[2089] | 759 | function get_query_string_diff($rejects=array(), $escape=true) |
---|
[507] | 760 | { |
---|
[12023] | 761 | if (empty($_SERVER['QUERY_STRING'])) |
---|
| 762 | { |
---|
| 763 | return ''; |
---|
| 764 | } |
---|
[12920] | 765 | |
---|
[24833] | 766 | parse_str($_SERVER['QUERY_STRING'], $vars); |
---|
[1086] | 767 | |
---|
[24833] | 768 | $vars = array_diff_key($vars, array_flip($rejects)); |
---|
| 769 | |
---|
| 770 | return '?' . http_build_query($vars, '', $escape ? '&' : '&'); |
---|
[507] | 771 | } |
---|
[512] | 772 | |
---|
[24833] | 773 | /** |
---|
| 774 | * returns true if the url is absolute (begins with http) |
---|
| 775 | * @param string $url |
---|
| 776 | * @returns boolean |
---|
| 777 | */ |
---|
[1020] | 778 | function url_is_remote($url) |
---|
| 779 | { |
---|
[1766] | 780 | if ( strncmp($url, 'http://', 7)==0 |
---|
| 781 | or strncmp($url, 'https://', 8)==0 ) |
---|
[1020] | 782 | { |
---|
| 783 | return true; |
---|
| 784 | } |
---|
| 785 | return false; |
---|
| 786 | } |
---|
| 787 | |
---|
[512] | 788 | /** |
---|
[5123] | 789 | * returns available themes |
---|
[512] | 790 | */ |
---|
[13242] | 791 | function get_pwg_themes($show_mobile=false) |
---|
[512] | 792 | { |
---|
[5306] | 793 | global $conf; |
---|
| 794 | |
---|
[960] | 795 | $themes = array(); |
---|
[579] | 796 | |
---|
[5153] | 797 | $query = ' |
---|
| 798 | SELECT |
---|
| 799 | id, |
---|
| 800 | name |
---|
| 801 | FROM '.THEMES_TABLE.' |
---|
| 802 | ORDER BY name ASC |
---|
| 803 | ;'; |
---|
| 804 | $result = pwg_query($query); |
---|
| 805 | while ($row = pwg_db_fetch_assoc($result)) |
---|
[960] | 806 | { |
---|
[13242] | 807 | if ($row['id'] == $conf['mobile_theme']) |
---|
| 808 | { |
---|
| 809 | if (!$show_mobile) |
---|
| 810 | { |
---|
| 811 | continue; |
---|
| 812 | } |
---|
| 813 | $row['name'] .= ' ('.l10n('Mobile').')'; |
---|
| 814 | } |
---|
[5982] | 815 | if (check_theme_installed($row['id'])) |
---|
[5306] | 816 | { |
---|
| 817 | $themes[ $row['id'] ] = $row['name']; |
---|
| 818 | } |
---|
[960] | 819 | } |
---|
| 820 | |
---|
[5102] | 821 | // plugins want remove some themes based on user status maybe? |
---|
| 822 | $themes = trigger_event('get_pwg_themes', $themes); |
---|
[5200] | 823 | |
---|
[960] | 824 | return $themes; |
---|
| 825 | } |
---|
| 826 | |
---|
[5982] | 827 | function check_theme_installed($theme_id) |
---|
| 828 | { |
---|
| 829 | global $conf; |
---|
| 830 | |
---|
| 831 | return file_exists($conf['themes_dir'].'/'.$theme_id.'/'.'themeconf.inc.php'); |
---|
| 832 | } |
---|
| 833 | |
---|
[12831] | 834 | /** Transforms an original path to its pwg representative */ |
---|
| 835 | function original_to_representative($path, $representative_ext) |
---|
| 836 | { |
---|
| 837 | $pos = strrpos($path, '/'); |
---|
| 838 | $path = substr_replace($path, 'pwg_representative/', $pos+1, 0); |
---|
| 839 | $pos = strrpos($path, '.'); |
---|
| 840 | return substr_replace($path, $representative_ext, $pos+1); |
---|
| 841 | } |
---|
| 842 | |
---|
[12855] | 843 | /** |
---|
| 844 | * @param element_info array containing element information from db; |
---|
| 845 | * at least 'id', 'path' should be present |
---|
| 846 | */ |
---|
| 847 | function get_element_path($element_info) |
---|
| 848 | { |
---|
| 849 | $path = $element_info['path']; |
---|
| 850 | if ( !url_is_remote($path) ) |
---|
| 851 | { |
---|
| 852 | $path = PHPWG_ROOT_PATH.$path; |
---|
| 853 | } |
---|
| 854 | return $path; |
---|
| 855 | } |
---|
[12831] | 856 | |
---|
[12855] | 857 | |
---|
[11996] | 858 | /** |
---|
[764] | 859 | * fill the current user caddie with given elements, if not already in |
---|
| 860 | * caddie |
---|
| 861 | * |
---|
| 862 | * @param array elements_id |
---|
| 863 | */ |
---|
| 864 | function fill_caddie($elements_id) |
---|
| 865 | { |
---|
| 866 | global $user; |
---|
[1086] | 867 | |
---|
[764] | 868 | $query = ' |
---|
| 869 | SELECT element_id |
---|
| 870 | FROM '.CADDIE_TABLE.' |
---|
| 871 | WHERE user_id = '.$user['id'].' |
---|
| 872 | ;'; |
---|
| 873 | $in_caddie = array_from_query($query, 'element_id'); |
---|
| 874 | |
---|
| 875 | $caddiables = array_diff($elements_id, $in_caddie); |
---|
| 876 | |
---|
| 877 | $datas = array(); |
---|
| 878 | |
---|
| 879 | foreach ($caddiables as $caddiable) |
---|
| 880 | { |
---|
| 881 | array_push($datas, array('element_id' => $caddiable, |
---|
| 882 | 'user_id' => $user['id'])); |
---|
| 883 | } |
---|
| 884 | |
---|
| 885 | if (count($caddiables) > 0) |
---|
| 886 | { |
---|
| 887 | mass_inserts(CADDIE_TABLE, array('element_id','user_id'), $datas); |
---|
| 888 | } |
---|
| 889 | } |
---|
[793] | 890 | |
---|
| 891 | /** |
---|
| 892 | * returns the element name from its filename |
---|
| 893 | * |
---|
| 894 | * @param string filename |
---|
| 895 | * @return string name |
---|
| 896 | */ |
---|
| 897 | function get_name_from_file($filename) |
---|
| 898 | { |
---|
| 899 | return str_replace('_',' ',get_filename_wo_extension($filename)); |
---|
| 900 | } |
---|
| 901 | |
---|
| 902 | /** |
---|
| 903 | * returns the corresponding value from $lang if existing. Else, the key is |
---|
| 904 | * returned |
---|
| 905 | * |
---|
| 906 | * @param string key |
---|
| 907 | * @return string |
---|
| 908 | */ |
---|
[13240] | 909 | function l10n($key) |
---|
[793] | 910 | { |
---|
[5156] | 911 | global $lang, $conf; |
---|
[793] | 912 | |
---|
[13258] | 913 | if ( ($val=@$lang[$key]) == null) |
---|
| 914 | { |
---|
| 915 | if ($conf['debug_l10n'] and !isset($lang[$key]) and !empty($key)) |
---|
| 916 | { |
---|
| 917 | trigger_error('[l10n] language key "'.$key.'" is not defined', E_USER_WARNING); |
---|
| 918 | } |
---|
| 919 | $val = $key; |
---|
| 920 | } |
---|
[13240] | 921 | return $val; |
---|
[793] | 922 | } |
---|
[960] | 923 | |
---|
| 924 | /** |
---|
[1637] | 925 | * returns the prinft value for strings including %d |
---|
| 926 | * return is concorded with decimal value (singular, plural) |
---|
| 927 | * |
---|
| 928 | * @param singular string key |
---|
| 929 | * @param plural string key |
---|
| 930 | * @param decimal value |
---|
| 931 | * @return string |
---|
| 932 | */ |
---|
[5156] | 933 | function l10n_dec($singular_fmt_key, $plural_fmt_key, $decimal) |
---|
[1637] | 934 | { |
---|
[5156] | 935 | global $lang_info; |
---|
[1864] | 936 | |
---|
[5156] | 937 | return |
---|
| 938 | sprintf( |
---|
| 939 | l10n(( |
---|
| 940 | (($decimal > 1) or ($decimal == 0 and $lang_info['zero_plural'])) |
---|
| 941 | ? $plural_fmt_key |
---|
| 942 | : $singular_fmt_key |
---|
| 943 | )), $decimal); |
---|
[1637] | 944 | } |
---|
[5021] | 945 | |
---|
[1908] | 946 | /* |
---|
| 947 | * returns a single element to use with l10n_args |
---|
| 948 | * |
---|
| 949 | * @param string key: translation key |
---|
[19225] | 950 | * @param mixed args: arguments to use on sprintf($key, args) |
---|
[1908] | 951 | * if args is a array, each values are used on sprintf |
---|
| 952 | * @return string |
---|
| 953 | */ |
---|
| 954 | function get_l10n_args($key, $args) |
---|
| 955 | { |
---|
| 956 | if (is_array($args)) |
---|
| 957 | { |
---|
| 958 | $key_arg = array_merge(array($key), $args); |
---|
| 959 | } |
---|
| 960 | else |
---|
| 961 | { |
---|
| 962 | $key_arg = array($key, $args); |
---|
| 963 | } |
---|
| 964 | return array('key_args' => $key_arg); |
---|
| 965 | } |
---|
[1637] | 966 | |
---|
[1908] | 967 | /* |
---|
[19225] | 968 | * returns a string formated with l10n elements |
---|
[1908] | 969 | * |
---|
[19225] | 970 | * @param array $key_args: l10n_args element or array of l10n_args elements |
---|
| 971 | * @param string $sep: used when translated elements are concatened |
---|
[1908] | 972 | * @return string |
---|
| 973 | */ |
---|
| 974 | function l10n_args($key_args, $sep = "\n") |
---|
| 975 | { |
---|
| 976 | if (is_array($key_args)) |
---|
| 977 | { |
---|
| 978 | foreach ($key_args as $key => $element) |
---|
| 979 | { |
---|
| 980 | if (isset($result)) |
---|
| 981 | { |
---|
| 982 | $result .= $sep; |
---|
| 983 | } |
---|
| 984 | else |
---|
| 985 | { |
---|
| 986 | $result = ''; |
---|
| 987 | } |
---|
| 988 | |
---|
| 989 | if ($key === 'key_args') |
---|
| 990 | { |
---|
[19225] | 991 | array_unshift($element, l10n(array_shift($element))); // translate the key |
---|
[1908] | 992 | $result .= call_user_func_array('sprintf', $element); |
---|
| 993 | } |
---|
| 994 | else |
---|
| 995 | { |
---|
| 996 | $result .= l10n_args($element, $sep); |
---|
| 997 | } |
---|
| 998 | } |
---|
| 999 | } |
---|
| 1000 | else |
---|
| 1001 | { |
---|
[2502] | 1002 | fatal_error('l10n_args: Invalid arguments'); |
---|
[1908] | 1003 | } |
---|
| 1004 | |
---|
| 1005 | return $result; |
---|
| 1006 | } |
---|
| 1007 | |
---|
[1637] | 1008 | /** |
---|
[1008] | 1009 | * returns the corresponding value from $themeconf if existing. Else, the |
---|
| 1010 | * key is returned |
---|
[960] | 1011 | * |
---|
| 1012 | * @param string key |
---|
| 1013 | * @return string |
---|
| 1014 | */ |
---|
| 1015 | function get_themeconf($key) |
---|
| 1016 | { |
---|
[1568] | 1017 | global $template; |
---|
[960] | 1018 | |
---|
[1568] | 1019 | return $template->get_themeconf($key); |
---|
[960] | 1020 | } |
---|
[1008] | 1021 | |
---|
| 1022 | /** |
---|
[1021] | 1023 | * Returns webmaster mail address depending on $conf['webmaster_id'] |
---|
| 1024 | * |
---|
| 1025 | * @return string |
---|
| 1026 | */ |
---|
| 1027 | function get_webmaster_mail_address() |
---|
| 1028 | { |
---|
| 1029 | global $conf; |
---|
| 1030 | |
---|
| 1031 | $query = ' |
---|
| 1032 | SELECT '.$conf['user_fields']['email'].' |
---|
| 1033 | FROM '.USERS_TABLE.' |
---|
| 1034 | WHERE '.$conf['user_fields']['id'].' = '.$conf['webmaster_id'].' |
---|
| 1035 | ;'; |
---|
[4325] | 1036 | list($email) = pwg_db_fetch_row(pwg_query($query)); |
---|
[1021] | 1037 | |
---|
[23887] | 1038 | $email = trigger_event('get_webmaster_mail_address', $email); |
---|
| 1039 | |
---|
[1021] | 1040 | return $email; |
---|
| 1041 | } |
---|
[1027] | 1042 | |
---|
| 1043 | /** |
---|
[1284] | 1044 | * Add configuration parameters from database to global $conf array |
---|
| 1045 | * |
---|
| 1046 | * @return void |
---|
| 1047 | */ |
---|
[1748] | 1048 | function load_conf_from_db($condition = '') |
---|
[1284] | 1049 | { |
---|
| 1050 | global $conf; |
---|
[1565] | 1051 | |
---|
[1284] | 1052 | $query = ' |
---|
[1748] | 1053 | SELECT param, value |
---|
[1284] | 1054 | FROM '.CONFIG_TABLE.' |
---|
[1748] | 1055 | '.(!empty($condition) ? 'WHERE '.$condition : '').' |
---|
[1284] | 1056 | ;'; |
---|
| 1057 | $result = pwg_query($query); |
---|
| 1058 | |
---|
[4325] | 1059 | if ((pwg_db_num_rows($result) == 0) and !empty($condition)) |
---|
[1284] | 1060 | { |
---|
[2502] | 1061 | fatal_error('No configuration data'); |
---|
[1284] | 1062 | } |
---|
| 1063 | |
---|
[4325] | 1064 | while ($row = pwg_db_fetch_assoc($result)) |
---|
[1284] | 1065 | { |
---|
[12878] | 1066 | $val = isset($row['value']) ? $row['value'] : ''; |
---|
| 1067 | // If the field is true or false, the variable is transformed into a boolean value. |
---|
| 1068 | if ($val == 'true') |
---|
[1284] | 1069 | { |
---|
[12878] | 1070 | $val = true; |
---|
[1284] | 1071 | } |
---|
[12878] | 1072 | elseif ($val == 'false') |
---|
| 1073 | { |
---|
| 1074 | $val = false; |
---|
| 1075 | } |
---|
| 1076 | $conf[ $row['param'] ] = $val; |
---|
[1284] | 1077 | } |
---|
[23372] | 1078 | |
---|
| 1079 | trigger_action('load_conf', $condition); |
---|
[1284] | 1080 | } |
---|
[1687] | 1081 | |
---|
[24350] | 1082 | /** |
---|
| 1083 | * Add or update a config parameter |
---|
| 1084 | * @param string $param |
---|
| 1085 | * @param string $value |
---|
| 1086 | */ |
---|
[5138] | 1087 | function conf_update_param($param, $value) |
---|
| 1088 | { |
---|
| 1089 | $query = ' |
---|
[11162] | 1090 | SELECT |
---|
| 1091 | param, |
---|
| 1092 | value |
---|
[5138] | 1093 | FROM '.CONFIG_TABLE.' |
---|
[5781] | 1094 | WHERE param = \''.$param.'\' |
---|
[5138] | 1095 | ;'; |
---|
[11162] | 1096 | $params = array_from_query($query, 'param'); |
---|
[5138] | 1097 | |
---|
[11162] | 1098 | if (count($params) == 0) |
---|
| 1099 | { |
---|
| 1100 | $query = ' |
---|
[5138] | 1101 | INSERT |
---|
| 1102 | INTO '.CONFIG_TABLE.' |
---|
[5781] | 1103 | (param, value) |
---|
| 1104 | VALUES(\''.$param.'\', \''.$value.'\') |
---|
[5138] | 1105 | ;'; |
---|
[11162] | 1106 | pwg_query($query); |
---|
| 1107 | } |
---|
| 1108 | else |
---|
| 1109 | { |
---|
| 1110 | $query = ' |
---|
| 1111 | UPDATE '.CONFIG_TABLE.' |
---|
| 1112 | SET value = \''.$value.'\' |
---|
| 1113 | WHERE param = \''.$param.'\' |
---|
| 1114 | ;'; |
---|
| 1115 | pwg_query($query); |
---|
| 1116 | } |
---|
[5138] | 1117 | } |
---|
| 1118 | |
---|
[1687] | 1119 | /** |
---|
[24350] | 1120 | * Delete on or more config parameters |
---|
| 1121 | * @since 2.6 |
---|
| 1122 | * @param string|string[] $params |
---|
| 1123 | */ |
---|
| 1124 | function conf_delete_param($params) |
---|
| 1125 | { |
---|
| 1126 | global $conf; |
---|
| 1127 | |
---|
| 1128 | if (!is_array($params)) |
---|
| 1129 | { |
---|
| 1130 | $params = array($params); |
---|
| 1131 | } |
---|
| 1132 | if (empty($params)) |
---|
| 1133 | { |
---|
| 1134 | return; |
---|
| 1135 | } |
---|
| 1136 | |
---|
| 1137 | $query = ' |
---|
| 1138 | DELETE FROM '.CONFIG_TABLE.' |
---|
| 1139 | WHERE param IN(\''. implode('\',\'', $params) .'\') |
---|
| 1140 | ;'; |
---|
| 1141 | pwg_query($query); |
---|
| 1142 | |
---|
| 1143 | foreach ($params as $param) |
---|
| 1144 | { |
---|
| 1145 | unset($conf[$param]); |
---|
| 1146 | } |
---|
| 1147 | } |
---|
| 1148 | |
---|
| 1149 | /** |
---|
[1727] | 1150 | * Prepends and appends a string at each value of the given array. |
---|
| 1151 | * |
---|
| 1152 | * @param array |
---|
| 1153 | * @param string prefix to each array values |
---|
| 1154 | * @param string suffix to each array values |
---|
| 1155 | */ |
---|
| 1156 | function prepend_append_array_items($array, $prepend_str, $append_str) |
---|
| 1157 | { |
---|
| 1158 | array_walk( |
---|
| 1159 | $array, |
---|
| 1160 | create_function('&$s', '$s = "'.$prepend_str.'".$s."'.$append_str.'";') |
---|
| 1161 | ); |
---|
| 1162 | |
---|
| 1163 | return $array; |
---|
| 1164 | } |
---|
| 1165 | |
---|
| 1166 | /** |
---|
| 1167 | * creates an hashed based on a query, this function is a very common |
---|
| 1168 | * pattern used here. Among the selected columns fetched, choose one to be |
---|
| 1169 | * the key, another one to be the value. |
---|
| 1170 | * |
---|
| 1171 | * @param string $query |
---|
| 1172 | * @param string $keyname |
---|
| 1173 | * @param string $valuename |
---|
| 1174 | * @return array |
---|
| 1175 | */ |
---|
| 1176 | function simple_hash_from_query($query, $keyname, $valuename) |
---|
| 1177 | { |
---|
| 1178 | $array = array(); |
---|
| 1179 | |
---|
| 1180 | $result = pwg_query($query); |
---|
[4325] | 1181 | while ($row = pwg_db_fetch_assoc($result)) |
---|
[1727] | 1182 | { |
---|
| 1183 | $array[ $row[$keyname] ] = $row[$valuename]; |
---|
| 1184 | } |
---|
| 1185 | |
---|
| 1186 | return $array; |
---|
| 1187 | } |
---|
| 1188 | |
---|
| 1189 | /** |
---|
[1866] | 1190 | * creates an hashed based on a query, this function is a very common |
---|
| 1191 | * pattern used here. The key is given as parameter, the value is an associative |
---|
| 1192 | * array. |
---|
| 1193 | * |
---|
| 1194 | * @param string $query |
---|
| 1195 | * @param string $keyname |
---|
| 1196 | * @return array |
---|
| 1197 | */ |
---|
| 1198 | function hash_from_query($query, $keyname) |
---|
| 1199 | { |
---|
| 1200 | $array = array(); |
---|
| 1201 | $result = pwg_query($query); |
---|
[4325] | 1202 | while ($row = pwg_db_fetch_assoc($result)) |
---|
[1866] | 1203 | { |
---|
| 1204 | $array[ $row[$keyname] ] = $row; |
---|
| 1205 | } |
---|
| 1206 | return $array; |
---|
| 1207 | } |
---|
| 1208 | |
---|
| 1209 | /** |
---|
[1687] | 1210 | * Return basename of the current script |
---|
[1690] | 1211 | * Lower case convertion is applied on return value |
---|
| 1212 | * Return value is without file extention ".php" |
---|
[1687] | 1213 | * |
---|
| 1214 | * @param void |
---|
| 1215 | * |
---|
| 1216 | * @return script basename |
---|
| 1217 | */ |
---|
| 1218 | function script_basename() |
---|
| 1219 | { |
---|
[2071] | 1220 | global $conf; |
---|
| 1221 | |
---|
| 1222 | foreach (array('SCRIPT_NAME', 'SCRIPT_FILENAME', 'PHP_SELF') as $value) |
---|
[1687] | 1223 | { |
---|
[3136] | 1224 | if (!empty($_SERVER[$value])) |
---|
[2071] | 1225 | { |
---|
| 1226 | $filename = strtolower($_SERVER[$value]); |
---|
[3136] | 1227 | if ($conf['php_extension_in_urls'] and get_extension($filename)!=='php') |
---|
| 1228 | continue; |
---|
| 1229 | $basename = basename($filename, '.php'); |
---|
| 1230 | if (!empty($basename)) |
---|
[2071] | 1231 | { |
---|
| 1232 | return $basename; |
---|
| 1233 | } |
---|
| 1234 | } |
---|
[1687] | 1235 | } |
---|
[2071] | 1236 | return ''; |
---|
[1687] | 1237 | } |
---|
| 1238 | |
---|
[1722] | 1239 | /** |
---|
| 1240 | * Return value for the current page define on $conf['filter_pages'] |
---|
| 1241 | * Îf value is not defined, default value are returned |
---|
| 1242 | * |
---|
| 1243 | * @param value name |
---|
| 1244 | * |
---|
| 1245 | * @return filter page value |
---|
| 1246 | */ |
---|
| 1247 | function get_filter_page_value($value_name) |
---|
| 1248 | { |
---|
| 1249 | global $conf; |
---|
| 1250 | |
---|
| 1251 | $page_name = script_basename(); |
---|
| 1252 | |
---|
| 1253 | if (isset($conf['filter_pages'][$page_name][$value_name])) |
---|
| 1254 | { |
---|
| 1255 | return $conf['filter_pages'][$page_name][$value_name]; |
---|
| 1256 | } |
---|
| 1257 | else if (isset($conf['filter_pages']['default'][$value_name])) |
---|
| 1258 | { |
---|
| 1259 | return $conf['filter_pages']['default'][$value_name]; |
---|
| 1260 | } |
---|
| 1261 | else |
---|
| 1262 | { |
---|
| 1263 | return null; |
---|
| 1264 | } |
---|
| 1265 | } |
---|
| 1266 | |
---|
[2126] | 1267 | /** |
---|
| 1268 | * returns the character set of data sent to browsers / received from forms |
---|
| 1269 | */ |
---|
| 1270 | function get_pwg_charset() |
---|
| 1271 | { |
---|
[5982] | 1272 | $pwg_charset = 'utf-8'; |
---|
| 1273 | if (defined('PWG_CHARSET')) |
---|
| 1274 | { |
---|
| 1275 | $pwg_charset = PWG_CHARSET; |
---|
| 1276 | } |
---|
| 1277 | return $pwg_charset; |
---|
[2126] | 1278 | } |
---|
| 1279 | |
---|
| 1280 | /** |
---|
| 1281 | * includes a language file or returns the content of a language file |
---|
| 1282 | * availability of the file |
---|
| 1283 | * |
---|
| 1284 | * in descending order of preference: |
---|
| 1285 | * param language, user language, default language |
---|
[2339] | 1286 | * Piwigo default language. |
---|
[2126] | 1287 | * |
---|
| 1288 | * @param string filename |
---|
| 1289 | * @param string dirname |
---|
[2479] | 1290 | * @param mixed options can contain |
---|
| 1291 | * language - language to load (if empty uses user language) |
---|
| 1292 | * return - if true the file content is returned otherwise the file is evaluated as php |
---|
| 1293 | * target_charset - |
---|
| 1294 | * no_fallback - the language must be respected |
---|
[5208] | 1295 | * local - if true, get local language file |
---|
[2479] | 1296 | * @return boolean success status or a string if options['return'] is true |
---|
[2126] | 1297 | */ |
---|
[2479] | 1298 | function load_language($filename, $dirname = '', |
---|
| 1299 | $options = array() ) |
---|
[2126] | 1300 | { |
---|
[23823] | 1301 | global $user, $language_files; |
---|
| 1302 | |
---|
| 1303 | if ( !empty($dirname) and !empty($filename) ) |
---|
| 1304 | { |
---|
| 1305 | if ( empty($language_files[$dirname]) or !in_array($filename,$language_files[$dirname]) ) |
---|
| 1306 | { |
---|
| 1307 | $language_files[$dirname][] = $filename; |
---|
| 1308 | } |
---|
| 1309 | } |
---|
[2127] | 1310 | |
---|
[2479] | 1311 | if (! @$options['return'] ) |
---|
[2127] | 1312 | { |
---|
| 1313 | $filename .= '.php'; //MAYBE to do .. load .po and .mo localization files |
---|
| 1314 | } |
---|
| 1315 | if (empty($dirname)) |
---|
| 1316 | { |
---|
| 1317 | $dirname = PHPWG_ROOT_PATH; |
---|
| 1318 | } |
---|
| 1319 | $dirname .= 'language/'; |
---|
| 1320 | |
---|
| 1321 | $languages = array(); |
---|
[2479] | 1322 | if ( !empty($options['language']) ) |
---|
[2127] | 1323 | { |
---|
[2479] | 1324 | $languages[] = $options['language']; |
---|
[2127] | 1325 | } |
---|
| 1326 | if ( !empty($user['language']) ) |
---|
| 1327 | { |
---|
| 1328 | $languages[] = $user['language']; |
---|
| 1329 | } |
---|
[2479] | 1330 | if ( ! @$options['no_fallback'] ) |
---|
[2132] | 1331 | { |
---|
[2479] | 1332 | if ( defined('PHPWG_INSTALLED') ) |
---|
| 1333 | { |
---|
| 1334 | $languages[] = get_default_language(); |
---|
| 1335 | } |
---|
| 1336 | $languages[] = PHPWG_DEFAULT_LANGUAGE; |
---|
[2132] | 1337 | } |
---|
[2479] | 1338 | |
---|
[2127] | 1339 | $languages = array_unique($languages); |
---|
| 1340 | |
---|
[18629] | 1341 | /*Note: target charset is always utf-8 |
---|
[2479] | 1342 | if ( empty($options['target_charset']) ) |
---|
[2127] | 1343 | { |
---|
| 1344 | $target_charset = get_pwg_charset(); |
---|
| 1345 | } |
---|
[2479] | 1346 | else |
---|
| 1347 | { |
---|
| 1348 | $target_charset = $options['target_charset']; |
---|
| 1349 | } |
---|
[18629] | 1350 | $target_charset = strtolower($target_charset);*/ |
---|
[20325] | 1351 | $source_file = ''; |
---|
| 1352 | $selected_language = ''; |
---|
[2127] | 1353 | foreach ($languages as $language) |
---|
| 1354 | { |
---|
[5208] | 1355 | $f = @$options['local'] ? |
---|
| 1356 | $dirname.$language.'.'.$filename: |
---|
| 1357 | $dirname.$language.'/'.$filename; |
---|
| 1358 | |
---|
[2127] | 1359 | if (file_exists($f)) |
---|
| 1360 | { |
---|
[20325] | 1361 | $selected_language = $language; |
---|
[2127] | 1362 | $source_file = $f; |
---|
| 1363 | break; |
---|
| 1364 | } |
---|
| 1365 | } |
---|
| 1366 | |
---|
| 1367 | if ( !empty($source_file) ) |
---|
| 1368 | { |
---|
[2479] | 1369 | if (! @$options['return'] ) |
---|
[2127] | 1370 | { |
---|
| 1371 | @include($source_file); |
---|
| 1372 | $load_lang = @$lang; |
---|
| 1373 | $load_lang_info = @$lang_info; |
---|
| 1374 | |
---|
| 1375 | global $lang, $lang_info; |
---|
| 1376 | if ( !isset($lang) ) $lang=array(); |
---|
| 1377 | if ( !isset($lang_info) ) $lang_info=array(); |
---|
[20325] | 1378 | |
---|
| 1379 | $parent_language = !empty($load_lang_info['parent']) ? $load_lang_info['parent'] : ( |
---|
| 1380 | !empty($lang_info['parent']) ? $lang_info['parent'] : null ); |
---|
| 1381 | if (!empty($parent_language)) |
---|
| 1382 | { |
---|
| 1383 | @include(str_replace($selected_language, $parent_language, $source_file)); |
---|
| 1384 | } |
---|
[2127] | 1385 | |
---|
[18629] | 1386 | /* Note: target charset is always utf-8 |
---|
[5200] | 1387 | if ( 'utf-8'!=$target_charset) |
---|
[2127] | 1388 | { |
---|
| 1389 | if ( is_array($load_lang) ) |
---|
| 1390 | { |
---|
| 1391 | foreach ($load_lang as $k => $v) |
---|
| 1392 | { |
---|
| 1393 | if ( is_array($v) ) |
---|
| 1394 | { |
---|
[6355] | 1395 | $func = create_function('$v', 'return convert_charset($v, "utf-8", "'.$target_charset.'");' ); |
---|
[2127] | 1396 | $lang[$k] = array_map($func, $v); |
---|
| 1397 | } |
---|
| 1398 | else |
---|
[6355] | 1399 | $lang[$k] = convert_charset($v, 'utf-8', $target_charset); |
---|
[2127] | 1400 | } |
---|
| 1401 | } |
---|
| 1402 | if ( is_array($load_lang_info) ) |
---|
| 1403 | { |
---|
| 1404 | foreach ($load_lang_info as $k => $v) |
---|
| 1405 | { |
---|
[6355] | 1406 | $lang_info[$k] = convert_charset($v, 'utf-8', $target_charset); |
---|
[2127] | 1407 | } |
---|
| 1408 | } |
---|
| 1409 | } |
---|
| 1410 | else |
---|
[18629] | 1411 | {*/ |
---|
[2134] | 1412 | $lang = array_merge( $lang, (array)$load_lang ); |
---|
| 1413 | $lang_info = array_merge( $lang_info, (array)$load_lang_info ); |
---|
[18629] | 1414 | //} |
---|
[2127] | 1415 | return true; |
---|
| 1416 | } |
---|
| 1417 | else |
---|
| 1418 | { |
---|
| 1419 | $content = @file_get_contents($source_file); |
---|
[18629] | 1420 | //Note: target charset is always utf-8 $content = convert_charset($content, 'utf-8', $target_charset); |
---|
[2127] | 1421 | return $content; |
---|
| 1422 | } |
---|
| 1423 | } |
---|
| 1424 | return false; |
---|
[2126] | 1425 | } |
---|
| 1426 | |
---|
[2127] | 1427 | /** |
---|
[6355] | 1428 | * converts a string from a character set to another character set |
---|
[2127] | 1429 | * @param string str the string to be converted |
---|
[6355] | 1430 | * @param string source_charset the character set in which the string is encoded |
---|
[2127] | 1431 | * @param string dest_charset the destination character set |
---|
| 1432 | */ |
---|
[6355] | 1433 | function convert_charset($str, $source_charset, $dest_charset) |
---|
[2127] | 1434 | { |
---|
[6355] | 1435 | if ($source_charset==$dest_charset) |
---|
| 1436 | return $str; |
---|
| 1437 | if ($source_charset=='iso-8859-1' and $dest_charset=='utf-8') |
---|
[5200] | 1438 | { |
---|
[6355] | 1439 | return utf8_encode($str); |
---|
[2127] | 1440 | } |
---|
[6355] | 1441 | if ($source_charset=='utf-8' and $dest_charset=='iso-8859-1') |
---|
[2127] | 1442 | { |
---|
| 1443 | return utf8_decode($str); |
---|
| 1444 | } |
---|
| 1445 | if (function_exists('iconv')) |
---|
| 1446 | { |
---|
[6355] | 1447 | return iconv($source_charset, $dest_charset, $str); |
---|
[2127] | 1448 | } |
---|
| 1449 | if (function_exists('mb_convert_encoding')) |
---|
| 1450 | { |
---|
[6355] | 1451 | return mb_convert_encoding( $str, $dest_charset, $source_charset ); |
---|
[2127] | 1452 | } |
---|
| 1453 | return $str; //??? |
---|
| 1454 | } |
---|
[2917] | 1455 | |
---|
| 1456 | /** |
---|
| 1457 | * makes sure a index.htm protects the directory from browser file listing |
---|
| 1458 | * |
---|
| 1459 | * @param string dir directory |
---|
| 1460 | */ |
---|
| 1461 | function secure_directory($dir) |
---|
| 1462 | { |
---|
| 1463 | $file = $dir.'/index.htm'; |
---|
| 1464 | if (!file_exists($file)) |
---|
| 1465 | { |
---|
| 1466 | @file_put_contents($file, 'Not allowed!'); |
---|
| 1467 | } |
---|
| 1468 | } |
---|
[3145] | 1469 | |
---|
| 1470 | /** |
---|
[7495] | 1471 | * returns a "secret key" that is to be sent back when a user posts a form |
---|
[3145] | 1472 | * |
---|
[7495] | 1473 | * @param int valid_after_seconds - key validity start time from now |
---|
[3145] | 1474 | */ |
---|
[7495] | 1475 | function get_ephemeral_key($valid_after_seconds, $aditionnal_data_to_hash = '') |
---|
[3145] | 1476 | { |
---|
[7495] | 1477 | global $conf; |
---|
| 1478 | $time = round(microtime(true), 1); |
---|
| 1479 | return $time.':'.$valid_after_seconds.':' |
---|
| 1480 | .hash_hmac( |
---|
[12920] | 1481 | 'md5', |
---|
| 1482 | $time.substr($_SERVER['REMOTE_ADDR'],0,5).$valid_after_seconds.$aditionnal_data_to_hash, |
---|
[7495] | 1483 | $conf['secret_key']); |
---|
| 1484 | } |
---|
[3145] | 1485 | |
---|
[7495] | 1486 | function verify_ephemeral_key($key, $aditionnal_data_to_hash = '') |
---|
| 1487 | { |
---|
| 1488 | global $conf; |
---|
| 1489 | $time = microtime(true); |
---|
| 1490 | $key = explode( ':', @$key ); |
---|
| 1491 | if ( count($key)!=3 |
---|
| 1492 | or $key[0]>$time-(float)$key[1] // page must have been retrieved more than X sec ago |
---|
| 1493 | or $key[0]<$time-3600 // 60 minutes expiration |
---|
| 1494 | or hash_hmac( |
---|
| 1495 | 'md5', $key[0].substr($_SERVER['REMOTE_ADDR'],0,5).$key[1].$aditionnal_data_to_hash, $conf['secret_key'] |
---|
| 1496 | ) != $key[2] |
---|
| 1497 | ) |
---|
| 1498 | { |
---|
| 1499 | return false; |
---|
| 1500 | } |
---|
| 1501 | return true; |
---|
[3145] | 1502 | } |
---|
[3172] | 1503 | |
---|
| 1504 | /** |
---|
| 1505 | * return an array which will be sent to template to display navigation bar |
---|
| 1506 | */ |
---|
[18165] | 1507 | function create_navigation_bar($url, $nb_element, $start, $nb_element_page, $clean_url = false, $param_name='start') |
---|
[3172] | 1508 | { |
---|
| 1509 | global $conf; |
---|
| 1510 | |
---|
[3173] | 1511 | $navbar = array(); |
---|
[3172] | 1512 | $pages_around = $conf['paginate_pages_around']; |
---|
[18165] | 1513 | $start_str = $clean_url ? '/'.$param_name.'-' : (strpos($url, '?')===false ? '?':'&').$param_name.'='; |
---|
[3172] | 1514 | |
---|
| 1515 | if (!isset($start) or !is_numeric($start) or (is_numeric($start) and $start < 0)) |
---|
| 1516 | { |
---|
| 1517 | $start = 0; |
---|
| 1518 | } |
---|
| 1519 | |
---|
| 1520 | // navigation bar useful only if more than one page to display ! |
---|
| 1521 | if ($nb_element > $nb_element_page) |
---|
| 1522 | { |
---|
[18729] | 1523 | $url_start = $url.$start_str; |
---|
| 1524 | |
---|
| 1525 | $cur_page = $navbar['CURRENT_PAGE'] = $start / $nb_element_page + 1; |
---|
[3172] | 1526 | $maximum = ceil($nb_element / $nb_element_page); |
---|
[18729] | 1527 | |
---|
| 1528 | $start = $nb_element_page * round( $start / $nb_element_page ); |
---|
[3173] | 1529 | $previous = $start - $nb_element_page; |
---|
| 1530 | $next = $start + $nb_element_page; |
---|
| 1531 | $last = ($maximum - 1) * $nb_element_page; |
---|
[3172] | 1532 | |
---|
[3173] | 1533 | // link to first page and previous page? |
---|
[3172] | 1534 | if ($cur_page != 1) |
---|
| 1535 | { |
---|
| 1536 | $navbar['URL_FIRST'] = $url; |
---|
[18729] | 1537 | $navbar['URL_PREV'] = $previous > 0 ? $url_start.$previous : $url; |
---|
[3172] | 1538 | } |
---|
[3173] | 1539 | // link on next page and last page? |
---|
| 1540 | if ($cur_page != $maximum) |
---|
[3172] | 1541 | { |
---|
[18729] | 1542 | $navbar['URL_NEXT'] = $url_start.($next < $last ? $next : $last); |
---|
| 1543 | $navbar['URL_LAST'] = $url_start.$last; |
---|
[3172] | 1544 | } |
---|
| 1545 | |
---|
| 1546 | // pages to display |
---|
| 1547 | $navbar['pages'] = array(); |
---|
| 1548 | $navbar['pages'][1] = $url; |
---|
[18729] | 1549 | for ($i = max( floor($cur_page) - $pages_around , 2), $stop = min( ceil($cur_page) + $pages_around + 1, $maximum); |
---|
[3173] | 1550 | $i < $stop; $i++) |
---|
[3172] | 1551 | { |
---|
[3173] | 1552 | $navbar['pages'][$i] = $url.$start_str.(($i - 1) * $nb_element_page); |
---|
[3172] | 1553 | } |
---|
[18729] | 1554 | $navbar['pages'][$maximum] = $url_start.$last; |
---|
[22653] | 1555 | $navbar['NB_PAGE']=$maximum; |
---|
[3172] | 1556 | } |
---|
| 1557 | return $navbar; |
---|
| 1558 | } |
---|
[3188] | 1559 | |
---|
| 1560 | /** |
---|
| 1561 | * return an array which will be sent to template to display recent icon |
---|
| 1562 | */ |
---|
| 1563 | function get_icon($date, $is_child_date = false) |
---|
| 1564 | { |
---|
| 1565 | global $cache, $user; |
---|
| 1566 | |
---|
| 1567 | if (empty($date)) |
---|
| 1568 | { |
---|
| 1569 | return false; |
---|
| 1570 | } |
---|
| 1571 | |
---|
| 1572 | if (!isset($cache['get_icon']['title'])) |
---|
| 1573 | { |
---|
| 1574 | $cache['get_icon']['title'] = sprintf( |
---|
[8665] | 1575 | l10n('photos posted during the last %d days'), |
---|
[3188] | 1576 | $user['recent_period'] |
---|
| 1577 | ); |
---|
| 1578 | } |
---|
| 1579 | |
---|
| 1580 | $icon = array( |
---|
| 1581 | 'TITLE' => $cache['get_icon']['title'], |
---|
| 1582 | 'IS_CHILD_DATE' => $is_child_date, |
---|
| 1583 | ); |
---|
| 1584 | |
---|
| 1585 | if (isset($cache['get_icon'][$date])) |
---|
| 1586 | { |
---|
| 1587 | return $cache['get_icon'][$date] ? $icon : array(); |
---|
| 1588 | } |
---|
| 1589 | |
---|
| 1590 | if (!isset($cache['get_icon']['sql_recent_date'])) |
---|
| 1591 | { |
---|
| 1592 | // Use MySql date in order to standardize all recent "actions/queries" |
---|
[4367] | 1593 | $cache['get_icon']['sql_recent_date'] = pwg_db_get_recent_period($user['recent_period']); |
---|
[3188] | 1594 | } |
---|
| 1595 | |
---|
| 1596 | $cache['get_icon'][$date] = $date > $cache['get_icon']['sql_recent_date']; |
---|
| 1597 | |
---|
| 1598 | return $cache['get_icon'][$date] ? $icon : array(); |
---|
| 1599 | } |
---|
[5195] | 1600 | |
---|
| 1601 | /** |
---|
| 1602 | * check token comming from form posted or get params to prevent csrf attacks |
---|
| 1603 | * if pwg_token is empty action doesn't require token |
---|
| 1604 | * else pwg_token is compare to server token |
---|
| 1605 | * |
---|
| 1606 | * @return void access denied if token given is not equal to server token |
---|
| 1607 | */ |
---|
| 1608 | function check_pwg_token() |
---|
| 1609 | { |
---|
[5335] | 1610 | if (!empty($_REQUEST['pwg_token'])) |
---|
[5195] | 1611 | { |
---|
[5335] | 1612 | if (get_pwg_token() != $_REQUEST['pwg_token']) |
---|
| 1613 | { |
---|
| 1614 | access_denied(); |
---|
| 1615 | } |
---|
[5195] | 1616 | } |
---|
[5335] | 1617 | else |
---|
| 1618 | bad_request('missing token'); |
---|
[5195] | 1619 | } |
---|
| 1620 | |
---|
| 1621 | function get_pwg_token() |
---|
| 1622 | { |
---|
| 1623 | global $conf; |
---|
| 1624 | |
---|
| 1625 | return hash_hmac('md5', session_id(), $conf['secret_key']); |
---|
| 1626 | } |
---|
| 1627 | |
---|
| 1628 | /* |
---|
| 1629 | * breaks the script execution if the given value doesn't match the given |
---|
| 1630 | * pattern. This should happen only during hacking attempts. |
---|
| 1631 | * |
---|
| 1632 | * @param string param_name |
---|
| 1633 | * @param array param_array |
---|
| 1634 | * @param boolean is_array |
---|
| 1635 | * @param string pattern |
---|
[24009] | 1636 | * @param boolean mandatory |
---|
[5195] | 1637 | * |
---|
| 1638 | * @return void |
---|
| 1639 | */ |
---|
[24009] | 1640 | function check_input_parameter($param_name, $param_array, $is_array, $pattern, $mandatory=false) |
---|
[5195] | 1641 | { |
---|
| 1642 | $param_value = null; |
---|
| 1643 | if (isset($param_array[$param_name])) |
---|
| 1644 | { |
---|
| 1645 | $param_value = $param_array[$param_name]; |
---|
| 1646 | } |
---|
| 1647 | |
---|
| 1648 | // it's ok if the input parameter is null |
---|
| 1649 | if (empty($param_value)) |
---|
| 1650 | { |
---|
[24009] | 1651 | if ($mandatory) |
---|
| 1652 | { |
---|
| 1653 | fatal_error('[Hacking attempt] the input parameter "'.$param_name.'" is not valid'); |
---|
| 1654 | } |
---|
[5195] | 1655 | return true; |
---|
| 1656 | } |
---|
| 1657 | |
---|
| 1658 | if ($is_array) |
---|
| 1659 | { |
---|
| 1660 | if (!is_array($param_value)) |
---|
| 1661 | { |
---|
| 1662 | fatal_error('[Hacking attempt] the input parameter "'.$param_name.'" should be an array'); |
---|
| 1663 | } |
---|
| 1664 | |
---|
| 1665 | foreach ($param_value as $item_to_check) |
---|
| 1666 | { |
---|
| 1667 | if (!preg_match($pattern, $item_to_check)) |
---|
| 1668 | { |
---|
| 1669 | fatal_error('[Hacking attempt] an item is not valid in input parameter "'.$param_name.'"'); |
---|
| 1670 | } |
---|
| 1671 | } |
---|
| 1672 | } |
---|
| 1673 | else |
---|
| 1674 | { |
---|
| 1675 | if (!preg_match($pattern, $param_value)) |
---|
| 1676 | { |
---|
| 1677 | fatal_error('[Hacking attempt] the input parameter "'.$param_name.'" is not valid'); |
---|
| 1678 | } |
---|
| 1679 | } |
---|
| 1680 | } |
---|
[6025] | 1681 | |
---|
| 1682 | |
---|
| 1683 | function get_privacy_level_options() |
---|
| 1684 | { |
---|
| 1685 | global $conf; |
---|
[6355] | 1686 | |
---|
[6025] | 1687 | $options = array(); |
---|
[18629] | 1688 | $label = ''; |
---|
[6025] | 1689 | foreach (array_reverse($conf['available_permission_levels']) as $level) |
---|
| 1690 | { |
---|
| 1691 | if (0 == $level) |
---|
| 1692 | { |
---|
| 1693 | $label = l10n('Everybody'); |
---|
| 1694 | } |
---|
| 1695 | else |
---|
| 1696 | { |
---|
[18629] | 1697 | if (strlen($label)) |
---|
[6025] | 1698 | { |
---|
[18629] | 1699 | $label .= ', '; |
---|
[6025] | 1700 | } |
---|
[18629] | 1701 | $label .= l10n( sprintf('Level %d',$level) ); |
---|
[6025] | 1702 | } |
---|
| 1703 | $options[$level] = $label; |
---|
| 1704 | } |
---|
| 1705 | return $options; |
---|
| 1706 | } |
---|
[11511] | 1707 | |
---|
| 1708 | |
---|
| 1709 | /** |
---|
| 1710 | * return the branch from the version. For example version 2.2.4 is for branch 2.2 |
---|
| 1711 | */ |
---|
| 1712 | function get_branch_from_version($version) |
---|
| 1713 | { |
---|
| 1714 | return implode('.', array_slice(explode('.', $version), 0, 2)); |
---|
| 1715 | } |
---|
[13234] | 1716 | |
---|
| 1717 | /** |
---|
| 1718 | * return the device type: mobile, tablet or desktop |
---|
| 1719 | */ |
---|
| 1720 | function get_device() |
---|
| 1721 | { |
---|
| 1722 | $device = pwg_get_session_var('device'); |
---|
| 1723 | |
---|
| 1724 | if (is_null($device)) |
---|
| 1725 | { |
---|
| 1726 | include_once(PHPWG_ROOT_PATH.'include/mdetect.php'); |
---|
| 1727 | $uagent_obj = new uagent_info(); |
---|
[18967] | 1728 | if ($uagent_obj->DetectSmartphone()) |
---|
[13234] | 1729 | { |
---|
| 1730 | $device = 'mobile'; |
---|
| 1731 | } |
---|
| 1732 | elseif ($uagent_obj->DetectTierTablet()) |
---|
| 1733 | { |
---|
| 1734 | $device = 'tablet'; |
---|
| 1735 | } |
---|
| 1736 | else |
---|
| 1737 | { |
---|
| 1738 | $device = 'desktop'; |
---|
| 1739 | } |
---|
| 1740 | pwg_set_session_var('device', $device); |
---|
| 1741 | } |
---|
| 1742 | |
---|
| 1743 | return $device; |
---|
| 1744 | } |
---|
| 1745 | |
---|
| 1746 | /** |
---|
| 1747 | * return true if mobile theme should be loaded |
---|
| 1748 | */ |
---|
| 1749 | function mobile_theme() |
---|
| 1750 | { |
---|
| 1751 | global $conf; |
---|
| 1752 | |
---|
| 1753 | if (empty($conf['mobile_theme'])) |
---|
| 1754 | { |
---|
| 1755 | return false; |
---|
| 1756 | } |
---|
| 1757 | |
---|
| 1758 | if (isset($_GET['mobile'])) |
---|
| 1759 | { |
---|
| 1760 | $is_mobile_theme = get_boolean($_GET['mobile']); |
---|
| 1761 | pwg_set_session_var('mobile_theme', $is_mobile_theme); |
---|
| 1762 | } |
---|
| 1763 | else |
---|
| 1764 | { |
---|
| 1765 | $is_mobile_theme = pwg_get_session_var('mobile_theme'); |
---|
| 1766 | } |
---|
| 1767 | |
---|
| 1768 | if (is_null($is_mobile_theme)) |
---|
| 1769 | { |
---|
| 1770 | $is_mobile_theme = (get_device() == 'mobile'); |
---|
| 1771 | pwg_set_session_var('mobile_theme', $is_mobile_theme); |
---|
| 1772 | } |
---|
| 1773 | |
---|
| 1774 | return $is_mobile_theme; |
---|
| 1775 | } |
---|
[17351] | 1776 | |
---|
| 1777 | /** |
---|
| 1778 | * check url format |
---|
| 1779 | */ |
---|
| 1780 | function url_check_format($url) |
---|
| 1781 | { |
---|
| 1782 | if (version_compare(PHP_VERSION, '5.2.0') >= 0) |
---|
| 1783 | { |
---|
| 1784 | return filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED | FILTER_FLAG_HOST_REQUIRED)!==false; |
---|
| 1785 | } |
---|
| 1786 | else |
---|
| 1787 | { |
---|
| 1788 | // http://mathiasbynens.be/demo/url-regex @imme_emosol |
---|
| 1789 | return (bool)preg_match('@^https?://(-\.)?([^\s/?\.#-]+\.?)+(/[^\s]*)?$@iS', $url); |
---|
| 1790 | } |
---|
| 1791 | } |
---|
[18164] | 1792 | |
---|
| 1793 | /** |
---|
| 1794 | * check email format |
---|
| 1795 | */ |
---|
| 1796 | function email_check_format($mail_address) |
---|
| 1797 | { |
---|
| 1798 | if (version_compare(PHP_VERSION, '5.2.0') >= 0) |
---|
| 1799 | { |
---|
| 1800 | return filter_var($mail_address, FILTER_VALIDATE_EMAIL)!==false; |
---|
| 1801 | } |
---|
| 1802 | else |
---|
| 1803 | { |
---|
| 1804 | $atom = '[-a-z0-9!#$%&\'*+\\/=?^_`{|}~]'; // before arobase |
---|
| 1805 | $domain = '([a-z0-9]([-a-z0-9]*[a-z0-9]+)?)'; // domain name |
---|
| 1806 | $regex = '/^' . $atom . '+' . '(\.' . $atom . '+)*' . '@' . '(' . $domain . '{1,63}\.)+' . $domain . '{2,63}$/i'; |
---|
| 1807 | |
---|
| 1808 | return (bool)preg_match($regex, $mail_address); |
---|
| 1809 | } |
---|
| 1810 | } |
---|
[21817] | 1811 | |
---|
| 1812 | /** returns the number of available comments for the connected user */ |
---|
| 1813 | function get_nb_available_comments() |
---|
| 1814 | { |
---|
| 1815 | global $user; |
---|
| 1816 | if (!isset($user['nb_available_comments'])) |
---|
| 1817 | { |
---|
| 1818 | $where = array(); |
---|
| 1819 | if ( !is_admin() ) |
---|
| 1820 | $where[] = 'validated=\'true\''; |
---|
| 1821 | $where[] = get_sql_condition_FandF |
---|
| 1822 | ( |
---|
| 1823 | array |
---|
| 1824 | ( |
---|
| 1825 | 'forbidden_categories' => 'category_id', |
---|
| 1826 | 'visible_categories' => 'category_id', |
---|
| 1827 | 'visible_images' => 'ic.image_id' |
---|
| 1828 | ), |
---|
| 1829 | '', true |
---|
| 1830 | ); |
---|
| 1831 | |
---|
| 1832 | $query = ' |
---|
| 1833 | SELECT COUNT(DISTINCT(com.id)) |
---|
| 1834 | FROM '.IMAGE_CATEGORY_TABLE.' AS ic |
---|
| 1835 | INNER JOIN '.COMMENTS_TABLE.' AS com |
---|
| 1836 | ON ic.image_id = com.image_id |
---|
| 1837 | WHERE '.implode(' |
---|
| 1838 | AND ', $where); |
---|
| 1839 | list($user['nb_available_comments']) = pwg_db_fetch_row(pwg_query($query)); |
---|
| 1840 | |
---|
| 1841 | single_update(USER_CACHE_TABLE, |
---|
| 1842 | array('nb_available_comments'=>$user['nb_available_comments']), |
---|
| 1843 | array('user_id'=>$user['id']) |
---|
| 1844 | ); |
---|
| 1845 | } |
---|
| 1846 | return $user['nb_available_comments']; |
---|
| 1847 | } |
---|
| 1848 | |
---|
[2126] | 1849 | ?> |
---|