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