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