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