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