[ Index ] |
|
Code source de Plume CMS 1.2.2 |
1 <?php 2 /* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 3 /* 4 # ***** BEGIN LICENSE BLOCK ***** 5 # This file is part of Plume CMS, a website management application. 6 # Copyright (C) 2001-2005 Loic d'Anterroches and contributors. 7 # 8 # Plume CMS is free software; you can redistribute it and/or modify 9 # it under the terms of the GNU General Public License as published by 10 # the Free Software Foundation; either version 2 of the License, or 11 # (at your option) any later version. 12 # 13 # Plume CMS is distributed in the hope that it will be useful, 14 # but WITHOUT ANY WARRANTY; without even the implied warranty of 15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 # GNU 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 21 # 22 # ***** END LICENSE BLOCK ***** */ 23 24 /** 25 * This file contains several utility classes. 26 * These classes have only static methods. 27 */ 28 29 /** 30 * Date and time utilities. 31 * 32 * A timestamp has the format YYYYMMDDHHMMSS. This is a "MYSQL" timestamp. 33 */ 34 class date 35 { 36 37 /** 38 * Create a timestamp with an offset with respect to time or today. 39 * 40 * @param int Day offset (0) 41 * @param int Month offset (0) 42 * @param int Year offset (0) 43 * @param int Time (now) 44 * @return int Timestamp 45 */ 46 function stamp($day=0, $month=0, $year=0, $time='') 47 { 48 $time = (strlen($time)) ? $time : time(); 49 return date('YmdHis', mktime(date('H',$time), date('i',$time), 50 date('s',$time), date('m',$time) + $month, 51 date('d',$time) + $day, 52 date('Y',$time) + $year)); 53 } 54 55 /** 56 * Round a timestamp to the day, month or year. 57 * 58 * For example: 59 * 20031202123212 => 20030101000000, 20031201000000, 20031203000000 60 * 61 * @param int Timestamp 62 * @param string Level of rouding 'd', ('m') or 'y' 63 * @return int Timestamp 64 */ 65 function round($time, $f='m') 66 { 67 $n['y'] = 4; 68 $n['m'] = 6; 69 $n['d'] = 8; 70 return str_pad(str_pad(substr($time, 0, $n[$f]),8,'01'),14,'0'); 71 } 72 73 74 /** 75 * Get a timestamp rounded to the current day. 76 * 77 * @param int Day offset (0) 78 * @return int Timestamp 79 */ 80 function day($offset=0) 81 { 82 return date::round(date::stamp($offset),'d'); 83 } 84 85 /** 86 * Get a timestamp rounded to the current month. 87 * 88 * @param int Month offset (0) 89 * @return int Timestamp 90 */ 91 function month($offset=0) 92 { 93 return date::round(date::stamp(0,$offset),'m'); 94 } 95 96 /** 97 * Get a timestamp rounded to the current year. 98 * 99 * @param int Year offset (0) 100 * @return int Timestamp 101 */ 102 function year($offset=0) 103 { 104 return date::round(date::stamp(0,0,$offset),'y'); 105 } 106 107 /** 108 * Convert a timestamp to unix time. 109 * If the timestamp is invalid, return current time. 110 * If no timestamp given, return current time. Always use this 111 * method instead of time() to be able to set some time shift 112 * if needed (Server US time, but "application" time at GMT) 113 * 114 * @param int mysql timestamp or (false) 115 * @return int unix time 116 */ 117 function unix($string=false) 118 { 119 if (false === $string) 120 return time(); 121 122 $d = array(); 123 if (false === ($d = date::explode($string))) { 124 return time(); 125 } 126 return mktime($d[0], $d[1], $d[2], $d[3], $d[4], $d[5]); 127 } 128 129 /** 130 * Convert a string with the format 131 * yyyymmddhhmmss or yyyy-mm-dd hh:mm:ss 132 * into a list that can be passed to mktime 133 * 134 * @param string Timestamp or date 135 * @return mixed Array, false if not a good timestamp format. 136 */ 137 function explode($string) 138 { 139 $res = array(); 140 if (preg_match('/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/', 141 $string, $match)) { 142 $res[] = $match[4]; 143 $res[] = $match[5]; 144 $res[] = $match[6]; 145 $res[] = $match[2]; 146 $res[] = $match[3]; 147 $res[] = $match[1]; 148 } elseif (preg_match('/(\d+)-(\d+)-(\d+)\s+(\d+):(\d+):(\d+)/', 149 $string, $match)) { 150 $res[] = $match[4]; 151 $res[] = $match[5]; 152 $res[] = $match[6]; 153 $res[] = $match[2]; 154 $res[] = $match[3]; 155 $res[] = $match[1]; 156 } else { 157 $res = false; 158 } 159 return $res; 160 } 161 162 /** 163 * Check the validity of a date, if not valid try to force it 164 * to be valid. The date can be given as an array 165 * from year to seconds, or as a single string. If a array 166 * is given, an array is sent back else a MySQL timestamp. 167 * The single string format must be able to be parsed by 168 * the date::explode() method. 169 * 170 * @param string Date time or array(h,m,s,M,D,Y) 171 * @return string MySQL timestamp or array 172 */ 173 function clean($inputdate) 174 { 175 $as_array = true; 176 $bad_date = false; 177 $dt_h = $dt_i = $dt_s = $dt_m = $dt_d = $dt_y = 0; 178 179 if (!is_array($inputdate)) { 180 $as_array = false; 181 if (false !== ($list = date::explode($inputdate))) { 182 list($dt_h, $dt_i, $dt_s, $dt_m, $dt_d, $dt_y) = $list; 183 } else { 184 $bad_date = true; 185 } 186 } else { 187 list($dt_h, $dt_i, $dt_s, $dt_m, $dt_d, $dt_y) = $inputdate; 188 } 189 190 if (!$bad_date) { 191 $dt_y = (string) sprintf('%04d',$dt_y); 192 $dt_m = (string) sprintf('%02d',$dt_m); 193 $dt_d = (string) sprintf('%02d',$dt_d); 194 $dt_h = (string) sprintf('%02d',$dt_h); 195 $dt_i = (string) sprintf('%02d',$dt_i); 196 $dt_s = (string) sprintf('%02d',$dt_s); 197 198 if ($dt_d > 31 || $dt_d < 1) { $dt_d = '01'; } 199 if ($dt_h > 23 || $dt_h < 0) { $dt_h = '00'; } 200 if ($dt_i > 59 || $dt_i < 0) { $dt_i = '00'; } 201 if ($dt_s > 59 || $dt_s < 0) { $dt_s = '00'; } 202 203 if (!checkdate($dt_m, $dt_d, $dt_y)) { 204 // try to 'clean' the date 205 $date = @date('YmdHis', @mktime($dt_h, $dt_i, $dt_s, $dt_m, $dt_d, $dt_y)); 206 if (14 != strlen($date)) { 207 $bad_date = true; 208 } 209 } else { 210 $date = $dt_y.$dt_m.$dt_d.$dt_h.$dt_i.$dt_s; 211 } 212 } 213 214 if ($bad_date) { 215 $date = date::stamp(); 216 } 217 218 if (!$as_array) { 219 return $date; 220 } else { 221 return date::explode($date); 222 } 223 } 224 225 226 /** 227 * Return a date far in the future considered as 228 * the End Of Time for the application. 229 * 230 * @return string '99991231235959' 231 */ 232 function EOT() 233 { 234 return '99991231235959'; 235 } 236 237 /** 238 * Returns true if the string is the end of time 239 * 240 * @param string String date 241 * @return bool True if at end of time 242 */ 243 function isEOT($date) 244 { 245 return (date::EOT() == $date); 246 } 247 248 } //End of date class. 249 250 251 /** 252 * Utilities for domains, urls. 253 */ 254 class www 255 { 256 /** 257 * Get the current resource url. 258 */ 259 function getRequestUri() 260 { 261 $s = ''; 262 if (isset($_SERVER['HTTPS'])) { 263 $s = 's'; 264 } 265 return 'http'.$s.'://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; 266 } 267 268 /** 269 * Get the relative url of the website as 270 * automatically as possible. 271 * This is used in the installer. 272 * 273 * @return string Relative URL without trailing slash 274 */ 275 function getRelativeUrl() 276 { 277 if (preg_match('#(.*)/manager(.*)#i',$_SERVER["SCRIPT_NAME"],$match)) 278 return $match[1]; 279 if (preg_match('#(.*)/\w+\.php#i', $_SERVER["SCRIPT_NAME"], $match)) 280 return $match[1]; 281 return ''; 282 } 283 284 /** 285 * get the current full url for redirection (with / at the end) 286 * 287 * @return string Full URL with trailing / 288 */ 289 function getCurrentFullUrl() 290 { 291 return www::getCurrentHostUrl().dirname($_SERVER['PHP_SELF']).'/'; 292 } 293 294 /** 295 * get the current host url no / at the end 296 * 297 * @return string Host URL without trailing / 298 */ 299 function getCurrentHostUrl() 300 { 301 $s = ''; 302 if (isset($_SERVER['HTTPS'])) 303 $s = 's'; 304 return 'http'.$s.'://'.$_SERVER['HTTP_HOST']; 305 } 306 307 308 /** 309 * Get current managed website base url. 310 * 311 * The url will be a full url or an empty string depending of the 312 * context. 313 */ 314 function getManagedWebsiteUrl() 315 { 316 $c = config::f('context'); 317 $url = ''; 318 if ($c == 'manager' or $c == 'external') { 319 $s = ''; 320 if (config::fbool('secure')) { 321 $s = 's'; 322 } 323 $url = 'http'.$s.'://'.config::f('domain'); 324 } 325 return $url; 326 327 } 328 329 /** 330 * Get the document root of a website. 331 * If the configuration array of the website is not given, 332 * it will find the information in the global configuration. 333 * 334 * @return string Document root. 335 */ 336 function getDocumentRoot() 337 { 338 // this does not work in case of complex aliases only for 339 // the document folder. 340 return substr(config::f('xmedia_root'), 0, 341 -strlen(config::f('rel_url_files'))); 342 } 343 344 } //End of www class. 345 346 347 class Misc 348 { 349 /** 350 * Produces a random string. 351 * 352 * @param int Length of the random string to be generated. 353 * @return string Random string 354 */ 355 function getRandomString($len=35) 356 { 357 $string = ''; 358 $chars = '0123456789abcdefghijklmnopqrstuvwxyz' 359 .'ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%&*()+=-_}{[]:><?/'; 360 $lchars = strlen($chars); 361 $i = 0; 362 while ($i<$len) { 363 $string .= substr($chars, mt_rand(0, $lchars-1), 1); 364 $i++; 365 } 366 return $string; 367 } 368 369 /** 370 * Convert a string from latin1 to utf8 371 * 372 * @credit Olivier Meunier 373 * @param string Latin 1 string 374 * @return string utf-8 string 375 */ 376 function latin1_utf8($str) 377 { 378 $conv = array( 379 chr(194).chr(128) => chr(226).chr(130).chr(172), 380 chr(194).chr(130) => chr(226).chr(128).chr(154), 381 chr(194).chr(131) => chr(198).chr(146), 382 chr(194).chr(132) => chr(226).chr(128).chr(158), 383 chr(194).chr(133) => chr(226).chr(128).chr(166), 384 chr(194).chr(134) => chr(226).chr(128).chr(160), 385 chr(194).chr(135) => chr(226).chr(128).chr(161), 386 chr(194).chr(136) => chr(203).chr(134), 387 chr(194).chr(137) => chr(226).chr(128).chr(176), 388 chr(194).chr(138) => chr(197).chr(160), 389 chr(194).chr(139) => chr(226).chr(128).chr(185), 390 chr(194).chr(140) => chr(197).chr(146), 391 chr(194).chr(145) => chr(226).chr(128).chr(152), 392 chr(194).chr(146) => chr(226).chr(128).chr(153), 393 chr(194).chr(147) => chr(226).chr(128).chr(156), 394 chr(194).chr(148) => chr(226).chr(128).chr(157), 395 chr(194).chr(149) => chr(226).chr(128).chr(162), 396 chr(194).chr(150) => chr(226).chr(128).chr(147), 397 chr(194).chr(151) => chr(226).chr(128).chr(148), 398 chr(194).chr(152) => chr(203).chr(156), 399 chr(194).chr(153) => chr(226).chr(132).chr(162), 400 chr(194).chr(154) => chr(197).chr(161), 401 chr(194).chr(155) => chr(226).chr(128).chr(186), 402 chr(194).chr(156) => chr(197).chr(147), 403 chr(194).chr(159) => chr(197).chr(184) 404 ); 405 $str = utf8_encode($str); 406 return str_replace(array_keys($conv), array_values($conv), $str); 407 } 408 409 } 410 411 /** 412 * Inform if the category is an hidden category or not. 413 * 414 * @param string Category path 415 * @return bool Success 416 */ 417 function isGhostCat($path) 418 { 419 return preg_match('#/_#', $path); 420 } 421 422 423 function strlenCompare($a, $b) 424 { 425 $a = textRemoveEntities($a); 426 $b = textRemoveEntities($b); 427 if (strlen($a) == strlen($b)) { 428 return 0; 429 } 430 return (strlen($a) > strlen($b)) ? -1 : 1; 431 } 432 433 /** 434 * Give is a file is safe or not 435 * 436 * @param string file name 437 * @param string Path to the data ('') 438 * @return bool Success 439 */ 440 function isFileSafe($file_name, $file_data ='') 441 { 442 if (preg_match('/[^A-Za-z0-9\-\_\.]/', $file_name)) return false; 443 if (!preg_match('/\.(png|jpg|jpeg|gif|bmp|psd|tif|aiff|asf|avi|bz2|css|doc|eps|gz|htm|mid|mov|mp3|mpg|ogg|pdf|ppt|ps|qt|ra|ram|rm|rtf|sdd|sdw|sit|sxi|sxw|swf|tgz|txt|wav|xls|xml|wmv|zip)$/i', $file_name)) return false; 444 return true; 445 } 446 447 function prettySize($size) 448 { 449 $mb = 1024*1024; 450 if ( $size > $mb ) { 451 $mysize = sprintf('%01.2f', $size/$mb) . ' ' . __('MB'); 452 } elseif ($size >= 1024) { 453 $mysize = sprintf('%01.2f', $size/1024) . ' ' . __('KB'); 454 } else { 455 $mysize = sprintf('%01.2f', $size/1024) . ' ' . __('bytes'); 456 } 457 return $mysize; 458 } 459 460 function isImage($file_name) 461 { 462 if (preg_match('/\.(png|jpg|gif|jpeg)$/i',$file_name)) return true; 463 return false; 464 } 465 466 function getFileExtension($file_name) 467 { 468 if (preg_match('/\.([A-Za-z0-9]{2,4})$/i',$file_name, $match)) return $match[1]; 469 return 'default'; 470 } 471 472 function removeFileExtension($file_name) 473 { 474 return preg_replace('/(\.[A-Za-z0-9]{2,4})$/i', '', $file_name); 475 } 476 477 function getParentDir($current_dir) 478 { 479 if (preg_match('#(.*/)*([^/])+/$#i',$current_dir, $match)) return $match[1]; 480 return ''; 481 } 482 483 484 function cleanDirname($dir) 485 { 486 return str_replace('\\','/',$dir); 487 } 488 489 /** 490 utf 8 encode a string if the global encoding is utf-8 491 */ 492 function if_utf8($string) 493 { 494 if (strtolower($GLOBALS['_PX_config']['encoding']) == 'utf-8') return utf8_encode($string); 495 return $string; 496 } 497 498 function showDebugInfo() 499 { 500 if (config::f('debug') == false) { 501 return; 502 } 503 echo '<!-- '."\n"; 504 echo 'Loaded locale files'; 505 if (!empty($GLOBALS['_PX_locale_files'])) { 506 echo ' [original encoding]:'."\n"; 507 foreach($GLOBALS['_PX_locale_files'] as $file => $encoding) { 508 echo $file .' ['.$encoding."]\n"; 509 } 510 } else { 511 echo ': None loaded.'."\n"; 512 } 513 echo "\n"; 514 echo 'Untranslated strings:'; 515 if (!empty($GLOBALS['_PX_debug_data']['untranslated'])) { 516 echo "\n"; 517 foreach($GLOBALS['_PX_debug_data']['untranslated'] as $string) { 518 echo ' \''.$string."'\n"; 519 } 520 } else { 521 echo ' None.'."\n"; 522 } 523 echo "\n"; 524 echo sprintf('%d SQL queries:', count($GLOBALS['_PX_debug_data']['sql_queries'])); 525 if (!empty($GLOBALS['_PX_debug_data']['sql_queries'])) { 526 echo "\n"; 527 foreach($GLOBALS['_PX_debug_data']['sql_queries'] as $string) { 528 echo ' '.str_replace("\n", ' ', $string)."\n"; 529 } 530 echo 'Queries starting with * are direct mysql function calls.'."\n"; 531 } else { 532 echo ' None.'."\n"; 533 } 534 echo '-->'; 535 } 536 537 538 /* break magic_quotes */ 539 function magicStrip(&$k, $key) 540 { 541 if(get_magic_quotes_gpc()) { 542 $k = handleMagicQuotes($k); 543 } 544 } 545 546 function handleMagicQuotes(&$value) 547 { 548 if (get_magic_quotes_gpc()) { 549 if (is_array($value)) { 550 $result = array(); 551 foreach ($value as $k => $v) { 552 if (is_array($v)) { 553 $result[$k] = handleMagicQuotes($v); 554 } else { 555 $result[$k] = stripslashes($v); 556 } 557 } 558 return $result; 559 } else { 560 return stripslashes($value); 561 } 562 } 563 return $value; 564 } 565 566 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Mon Nov 26 11:57:01 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |