Changeset 1654 for sandbox/filemanager/tp/dompdf/include/functions.inc.php
- Timestamp:
- 11/17/09 09:02:41 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sandbox/filemanager/tp/dompdf/include/functions.inc.php
r1575 r1654 34 34 * @copyright 2004 Benj Carson 35 35 * @author Benj Carson <benjcarson@digitaljunkies.ca> 36 * @contributor Helmut Tischer <htischer@weihenstephan.org> 36 37 * @package dompdf 37 38 * @version 0.5.1 38 */ 39 40 /* $Id: functions.inc.php,v 1.11 2006/07/07 21:31:03 benjcarson Exp $ */ 39 * 40 * Changes 41 * @contributor Helmut Tischer <htischer@weihenstephan.org> 42 * @version 0.5.1.htischer.20090507 43 * - trailing slash of base_path in build_url is no longer optional when 44 * required. This allows paths not ending in a slash, e.g. on dynamically 45 * created sites with page id in the url parameters. 46 * @version 20090601 47 * - fix windows paths 48 * @version 20090610 49 * - relax windows path syntax, use uniform path delimiter. Used for background images. 50 */ 51 52 /* $Id: functions.inc.php 187 2009-10-20 19:09:52Z eclecticgeek@gmail.com $ */ 41 53 42 54 /** … … 95 107 * @param string $url 96 108 * @return string 109 * 110 * Initially the trailing slash of $base_path was optional, and conditionally appended. 111 * However on dynamically created sites, where the page is given as url parameter, 112 * the base path might not end with an url. 113 * Therefore do not append a slash, and **require** the $base_url to ending in a slash 114 * when needed. 115 * Vice versa, on using the local file system path of a file, make sure that the slash 116 * is appended (o.k. also for Windows) 97 117 */ 98 118 function build_url($protocol, $host, $base_path, $url) { 99 if ( mb_strlen($url) == 0 ) 100 return $protocol . $host . rtrim($base_path, "/\\") . "/"; 119 if ( mb_strlen($url) == 0 ) { 120 //return $protocol . $host . rtrim($base_path, "/\\") . "/"; 121 return $protocol . $host . $base_path; 122 } 101 123 102 124 // Is the url already fully qualified? … … 106 128 $ret = $protocol; 107 129 108 if ( !in_array(mb_strtolower($protocol), array("http://", "https://", 109 "ftp://", "ftps://")) ) { 110 // We ignore the host for local file access, and run the path through 111 // realpath() 112 $host = ""; 113 $base_path = realpath($base_path); 114 } 115 116 if ( $url{0} === "/" ) 130 if (!in_array(mb_strtolower($protocol), array("http://", "https://", "ftp://", "ftps://"))) { 131 //On Windows local file, an abs path can begin also with a '\' or a drive letter and colon 132 //drive: followed by a relative path would be a drive specific default folder. 133 //not known in php app code, treat as abs path 134 //($url{1} !== ':' || ($url{2}!=='\\' && $url{2}!=='/')) 135 if ($url{0} !== '/' && (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN' || ($url{0} != '\\' && $url{1} !== ':'))) { 136 // For rel path and local acess we ignore the host, and run the path through realpath() 137 $ret .= dompdf_realpath($base_path).'/'; 138 } 139 $ret .= $url; 140 return $ret; 141 } 142 143 //remote urls with backslash in html/css are not really correct, but lets be genereous 144 if ( $url{0} === '/' || $url{0} === '\\' ) { 117 145 // Absolute path 118 146 $ret .= $host . $url; 119 else {147 } else { 120 148 // Relative path 121 122 $base_path = $base_path !== "" ? rtrim($base_path, "/\\") . "/" : ""; 149 //$base_path = $base_path !== "" ? rtrim($base_path, "/\\") . "/" : ""; 123 150 $ret .= $host . $base_path . $url; 124 151 } … … 276 303 277 304 /** 305 * Canonicalize a path without checking if the file exists 306 * 307 * @param string $path The path to canonicalize 308 * @return string The canonical path, or null if the path is invalid (e.g. /../../foo) 309 */ 310 function dompdf_realpath($path) { 311 // If the path is relative, prepend the current directory 312 if ( strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ) { 313 if ( $path{0} == '/' || $path{0} == '\\' ) { 314 $path = substr(getcwd(),0,2) . $path; 315 } else if ($path{1} != ':' ) { 316 $path = getcwd() . DIRECTORY_SEPARATOR . $path; 317 } 318 } else if ($path{0} != '/') { 319 $path = getcwd() . DIRECTORY_SEPARATOR . $path; 320 } 321 $path = strtr( $path, DIRECTORY_SEPARATOR == "\\" ? "/" : DIRECTORY_SEPARATOR , DIRECTORY_SEPARATOR); 322 323 $parts = explode(DIRECTORY_SEPARATOR, $path); 324 $path = array(); 325 326 $i = 0; 327 foreach ($parts as $dir) { 328 329 if ( $dir == "." ) 330 continue; 331 332 if ( $dir == ".." ) { 333 $i--; 334 if ( $i < 0 ) 335 $i = 0; 336 337 unset($path[$i]); 338 continue; 339 } 340 341 if ( $dir == "" ) 342 continue; 343 344 $path[$i] = $dir; 345 $i++; 346 } 347 348 return (DIRECTORY_SEPARATOR === '/' ? DIRECTORY_SEPARATOR : NULL) . join(DIRECTORY_SEPARATOR, $path); 349 } 350 351 /** 278 352 * mb_string compatibility 279 353 */ 280 354 355 if ( !function_exists("mb_convert_encoding") ) { 356 function mb_convert_encoding($data, $to_encoding, $from_encoding='UTF-8') { 357 if (str_replace('-', '', strtolower($to_encoding)) == 'utf8') { 358 return utf8_encode($data); 359 } else { 360 return utf8_decode($data); 361 } 362 } 363 } 364 365 if ( !function_exists("mb_detect_encoding") ) { 366 function mb_detect_encoding($data, $encoding_list=array('iso-8859-1'), $strict=false) { 367 return 'iso-8859-1'; 368 } 369 } 370 371 if ( !function_exists("mb_internal_encoding") ) { 372 function mb_internal_encoding($encoding=NULL) { 373 if (isset($encoding)) { 374 return true; 375 } else { 376 return 'iso-8859-1'; 377 } 378 } 379 } 380 281 381 if ( !function_exists("mb_strlen") ) { 282 function mb_strlen($str ) {382 function mb_strlen($str, $encoding='iso-8859-1') { 283 383 return strlen($str); 284 384 } … … 297 397 } 298 398 399 if ( !function_exists("mb_strtolower") ) { 400 function mb_strtolower($str) { 401 return strtolower($str); 402 } 403 } 404 405 if ( !function_exists("mb_strtoupper") ) { 406 function mb_strtoupper($str) { 407 return strtoupper($str); 408 } 409 } 410 299 411 if ( !function_exists("mb_substr") ) { 300 function mb_substr($str, $start, $length = null) {412 function mb_substr($str, $start, $length=null, $encoding='iso-8859-1') { 301 413 if ( is_null($length) ) 302 414 return substr($str, $start); … … 306 418 } 307 419 308 if ( !function_exists("mb_strtolower") ) {309 function mb_strtolower($str) {310 return strtolower($str);311 }312 }313 314 if ( !function_exists("mb_strtoupper") ) {315 function mb_strtoupper($str) {316 return strtoupper($str);317 }318 }319 320 420 if ( !function_exists("mb_substr_count") ) { 321 421 function mb_substr_count($haystack, $needle) { … … 323 423 } 324 424 } 325 326 425 327 426 /** … … 355 454 356 455 /** 456 * Print a useful backtrace 457 */ 458 function bt() { 459 $bt = debug_backtrace(); 460 461 array_shift($bt); // remove actual bt() call 462 echo "\n"; 463 464 $i = 0; 465 foreach ($bt as $call) { 466 $file = basename($call["file"]) . " (" . $call["line"] . ")"; 467 if ( isset($call["class"]) ) { 468 $func = $call["class"] . "->" . $call["function"] . "()"; 469 } else { 470 $func = $call["function"] . "()"; 471 } 472 473 echo "#" . str_pad($i, 2, " ", STR_PAD_RIGHT) . ": " . str_pad($file.":", 42) . " $func\n"; 474 $i++; 475 } 476 echo "\n"; 477 } 478 479 /** 480 * Print debug messages 481 * 482 * @param string $type The type of debug messages to print 483 */ 484 function dompdf_debug($type, $msg) { 485 global $_DOMPDF_DEBUG_TYPES; 486 global $_dompdf_show_warnings; 487 global $_dompdf_debug; 488 if ( isset($_DOMPDF_DEBUG_TYPES[$type]) && ($_dompdf_show_warnings || $_dompdf_debug) ) { 489 $arr = debug_backtrace(); 490 491 echo basename($arr[0]["file"]) . " (" . $arr[0]["line"] ."): " . $arr[1]["function"] . ": "; 492 pre_r($msg); 493 } 494 } 495 496 /** 357 497 * Dump memory usage 358 498 */ 499 if ( !function_exists("print_memusage") ) { 359 500 function print_memusage() { 360 501 global $memusage; … … 373 514 echo ("\n" . str_pad("Total:", 40) . memory_get_usage()) . "\n"; 374 515 } 516 } 375 517 376 518 /** 377 519 * Initialize memory profiling code 378 520 */ 521 if ( !function_exists("enable_mem_profile") ) { 379 522 function enable_mem_profile() { 380 523 global $memusage; 381 524 $memusage = array("Startup" => memory_get_usage()); 382 525 register_shutdown_function("print_memusage"); 383 } 526 } 527 } 384 528 385 529 /** … … 388 532 * @param string $location a meaningful location 389 533 */ 534 if ( !function_exists("mark_memusage") ) { 390 535 function mark_memusage($location) { 391 536 global $memusage; … … 393 538 $memusage[$location] = memory_get_usage(); 394 539 } 395 396 540 } 541 542 /** 543 * Find the current system temporary directory 544 * 545 * @link http://us.php.net/manual/en/function.sys-get-temp-dir.php#85261 546 */ 547 if ( !function_exists('sys_get_temp_dir')) { 548 function sys_get_temp_dir() { 549 if (!empty($_ENV['TMP'])) { return realpath($_ENV['TMP']); } 550 if (!empty($_ENV['TMPDIR'])) { return realpath( $_ENV['TMPDIR']); } 551 if (!empty($_ENV['TEMP'])) { return realpath( $_ENV['TEMP']); } 552 $tempfile=tempnam(uniqid(rand(),TRUE),''); 553 if (file_exists($tempfile)) { 554 unlink($tempfile); 555 return realpath(dirname($tempfile)); 556 } 557 } 558 } 397 559 ?>
Note: See TracChangeset
for help on using the changeset viewer.