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