[1575] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * DOMPDF - PHP5 HTML to PDF renderer |
---|
| 4 | * |
---|
| 5 | * File: $RCSfile: functions.inc.php,v $ |
---|
| 6 | * Created on: 2004-08-04 |
---|
| 7 | * |
---|
| 8 | * Copyright (c) 2004 - Benj Carson <benjcarson@digitaljunkies.ca> |
---|
| 9 | * |
---|
| 10 | * This library is free software; you can redistribute it and/or |
---|
| 11 | * modify it under the terms of the GNU Lesser General Public |
---|
| 12 | * License as published by the Free Software Foundation; either |
---|
| 13 | * version 2.1 of the License, or (at your option) any later version. |
---|
| 14 | * |
---|
| 15 | * This library is distributed in the hope that it will be useful, |
---|
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 18 | * Lesser General Public License for more details. |
---|
| 19 | * |
---|
| 20 | * You should have received a copy of the GNU Lesser General Public License |
---|
| 21 | * along with this library in the file LICENSE.LGPL; if not, write to the |
---|
| 22 | * Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
---|
| 23 | * 02111-1307 USA |
---|
| 24 | * |
---|
| 25 | * Alternatively, you may distribute this software under the terms of the |
---|
| 26 | * PHP License, version 3.0 or later. A copy of this license should have |
---|
| 27 | * been distributed with this file in the file LICENSE.PHP . If this is not |
---|
| 28 | * the case, you can obtain a copy at http://www.php.net/license/3_0.txt. |
---|
| 29 | * |
---|
| 30 | * The latest version of DOMPDF might be available at: |
---|
| 31 | * http://www.digitaljunkies.ca/dompdf |
---|
| 32 | * |
---|
| 33 | * @link http://www.digitaljunkies.ca/dompdf |
---|
| 34 | * @copyright 2004 Benj Carson |
---|
| 35 | * @author Benj Carson <benjcarson@digitaljunkies.ca> |
---|
| 36 | * @package dompdf |
---|
| 37 | * @version 0.5.1 |
---|
| 38 | */ |
---|
| 39 | |
---|
| 40 | /* $Id: functions.inc.php,v 1.11 2006/07/07 21:31:03 benjcarson Exp $ */ |
---|
| 41 | |
---|
| 42 | /** |
---|
| 43 | * print_r wrapper for html/cli output |
---|
| 44 | * |
---|
| 45 | * Wraps print_r() output in < pre > tags if the current sapi is not |
---|
| 46 | * 'cli'. Returns the output string instead of displaying it if $return is |
---|
| 47 | * true. |
---|
| 48 | * |
---|
| 49 | * @param mixed $mixed variable or expression to display |
---|
| 50 | * @param bool $return |
---|
| 51 | * |
---|
| 52 | */ |
---|
| 53 | if ( !function_exists("pre_r") ) { |
---|
| 54 | function pre_r($mixed, $return = false) { |
---|
| 55 | if ($return) |
---|
| 56 | return "<pre>" . print_r($mixed, true) . "</pre>"; |
---|
| 57 | |
---|
| 58 | if ( php_sapi_name() != "cli") |
---|
| 59 | echo ("<pre>"); |
---|
| 60 | print_r($mixed); |
---|
| 61 | |
---|
| 62 | if ( php_sapi_name() != "cli") |
---|
| 63 | echo("</pre>"); |
---|
| 64 | else |
---|
| 65 | echo ("\n"); |
---|
| 66 | flush(); |
---|
| 67 | |
---|
| 68 | } |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | /** |
---|
| 72 | * var_dump wrapper for html/cli output |
---|
| 73 | * |
---|
| 74 | * Wraps var_dump() output in < pre > tags if the current sapi is not |
---|
| 75 | * 'cli'. |
---|
| 76 | * |
---|
| 77 | * @param mixed $mixed variable or expression to display. |
---|
| 78 | */ |
---|
| 79 | if ( !function_exists("pre_var_dump") ) { |
---|
| 80 | function pre_var_dump($mixed) { |
---|
| 81 | if ( php_sapi_name() != "cli") |
---|
| 82 | echo("<pre>"); |
---|
| 83 | var_dump($mixed); |
---|
| 84 | if ( php_sapi_name() != "cli") |
---|
| 85 | echo("</pre>"); |
---|
| 86 | } |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | /** |
---|
| 90 | * builds a full url given a protocol, hostname, base path and url |
---|
| 91 | * |
---|
| 92 | * @param string $protocol |
---|
| 93 | * @param string $host |
---|
| 94 | * @param string $base_path |
---|
| 95 | * @param string $url |
---|
| 96 | * @return string |
---|
| 97 | */ |
---|
| 98 | function build_url($protocol, $host, $base_path, $url) { |
---|
| 99 | if ( mb_strlen($url) == 0 ) |
---|
| 100 | return $protocol . $host . rtrim($base_path, "/\\") . "/"; |
---|
| 101 | |
---|
| 102 | // Is the url already fully qualified? |
---|
| 103 | if ( mb_strpos($url, "://") !== false ) |
---|
| 104 | return $url; |
---|
| 105 | |
---|
| 106 | $ret = $protocol; |
---|
| 107 | |
---|
| 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} === "/" ) |
---|
| 117 | // Absolute path |
---|
| 118 | $ret .= $host . $url; |
---|
| 119 | else { |
---|
| 120 | // Relative path |
---|
| 121 | |
---|
| 122 | $base_path = $base_path !== "" ? rtrim($base_path, "/\\") . "/" : ""; |
---|
| 123 | $ret .= $host . $base_path . $url; |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | return $ret; |
---|
| 127 | |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | /** |
---|
| 131 | * parse a full url or pathname and return an array(protocol, host, path, |
---|
| 132 | * file + query + fragment) |
---|
| 133 | * |
---|
| 134 | * @param string $url |
---|
| 135 | * @return array |
---|
| 136 | */ |
---|
| 137 | function explode_url($url) { |
---|
| 138 | $protocol = ""; |
---|
| 139 | $host = ""; |
---|
| 140 | $path = ""; |
---|
| 141 | $file = ""; |
---|
| 142 | |
---|
| 143 | $arr = parse_url($url); |
---|
| 144 | |
---|
| 145 | if ( isset($arr["scheme"]) && |
---|
| 146 | $arr["scheme"] != "file" && |
---|
| 147 | mb_strlen($arr["scheme"]) > 1 ) // Exclude windows drive letters... |
---|
| 148 | { |
---|
| 149 | $protocol = $arr["scheme"] . "://"; |
---|
| 150 | |
---|
| 151 | if ( isset($arr["user"]) ) { |
---|
| 152 | $host .= $arr["user"]; |
---|
| 153 | |
---|
| 154 | if ( isset($arr["pass"]) ) |
---|
| 155 | $host .= "@" . $arr["pass"]; |
---|
| 156 | |
---|
| 157 | $host .= ":"; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | if ( isset($arr["host"]) ) |
---|
| 161 | $host .= $arr["host"]; |
---|
| 162 | |
---|
| 163 | if ( isset($arr["port"]) ) |
---|
| 164 | $host .= ":" . $arr["port"]; |
---|
| 165 | |
---|
| 166 | if ( isset($arr["path"]) && $arr["path"] !== "" ) { |
---|
| 167 | // Do we have a trailing slash? |
---|
| 168 | if ( $arr["path"]{ mb_strlen($arr["path"]) - 1 } == "/" ) { |
---|
| 169 | $path = $arr["path"]; |
---|
| 170 | $file = ""; |
---|
| 171 | } else { |
---|
| 172 | $path = dirname($arr["path"]) . "/"; |
---|
| 173 | $file = basename($arr["path"]); |
---|
| 174 | } |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | if ( isset($arr["query"]) ) |
---|
| 178 | $file .= "?" . $arr["query"]; |
---|
| 179 | |
---|
| 180 | if ( isset($arr["fragment"]) ) |
---|
| 181 | $file .= "#" . $arr["fragment"]; |
---|
| 182 | |
---|
| 183 | } else { |
---|
| 184 | |
---|
| 185 | $i = mb_strpos($url, "file://"); |
---|
| 186 | if ( $i !== false) |
---|
| 187 | $url = mb_substr($url, $i + 7); |
---|
| 188 | |
---|
| 189 | $protocol = ""; // "file://"; ? why doesn't this work... It's because of |
---|
| 190 | // network filenames like //COMPU/SHARENAME |
---|
| 191 | |
---|
| 192 | $host = ""; // localhost, really |
---|
| 193 | $file = basename($url); |
---|
| 194 | |
---|
| 195 | $path = dirname($url); |
---|
| 196 | |
---|
| 197 | // Check that the path exists |
---|
| 198 | if ( $path !== false ) { |
---|
| 199 | $path .= '/'; |
---|
| 200 | |
---|
| 201 | } else { |
---|
| 202 | // generate a url to access the file if no real path found. |
---|
| 203 | $protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https://' : 'http://'; |
---|
| 204 | |
---|
| 205 | $host = isset($_SERVER["HTTP_HOST"]) ? $_SERVER["HTTP_HOST"] : php_uname("n"); |
---|
| 206 | |
---|
| 207 | if ( substr($arr["path"], 0, 1) == '/' ) { |
---|
| 208 | $path = dirname($arr["path"]); |
---|
| 209 | } else { |
---|
| 210 | $path = '/' . rtrim(dirname($_SERVER["SCRIPT_NAME"]), '/') . '/' . $arr["path"]; |
---|
| 211 | } |
---|
| 212 | } |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | $ret = array($protocol, $host, $path, $file, |
---|
| 216 | "protocol" => $protocol, |
---|
| 217 | "host" => $host, |
---|
| 218 | "path" => $path, |
---|
| 219 | "file" => $file); |
---|
| 220 | return $ret; |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | /** |
---|
| 224 | * converts decimal numbers to roman numerals |
---|
| 225 | * |
---|
| 226 | * @param int $num |
---|
| 227 | * @return string |
---|
| 228 | */ |
---|
| 229 | function dec2roman($num) { |
---|
| 230 | |
---|
| 231 | static $ones = array("", "i", "ii", "iii", "iv", "v", |
---|
| 232 | "vi", "vii", "viii", "ix"); |
---|
| 233 | static $tens = array("", "x", "xx", "xxx", "xl", "l", |
---|
| 234 | "lx", "lxx", "lxxx", "xc"); |
---|
| 235 | static $hund = array("", "c", "cc", "ccc", "cd", "d", |
---|
| 236 | "dc", "dcc", "dccc", "cm"); |
---|
| 237 | static $thou = array("", "m", "mm", "mmm"); |
---|
| 238 | |
---|
| 239 | if ( !is_numeric($num) ) |
---|
| 240 | throw new DOMPDF_Exception("dec2roman() requires a numeric argument."); |
---|
| 241 | |
---|
| 242 | if ( $num > 4000 || $num < 0 ) |
---|
| 243 | return "(out of range)"; |
---|
| 244 | |
---|
| 245 | $num = strrev((string)$num); |
---|
| 246 | |
---|
| 247 | $ret = ""; |
---|
| 248 | switch (mb_strlen($num)) { |
---|
| 249 | |
---|
| 250 | case 4: |
---|
| 251 | $ret .= $thou[$num{3}]; |
---|
| 252 | |
---|
| 253 | case 3: |
---|
| 254 | $ret .= $hund[$num{2}]; |
---|
| 255 | |
---|
| 256 | case 2: |
---|
| 257 | $ret .= $tens[$num{1}]; |
---|
| 258 | |
---|
| 259 | case 1: |
---|
| 260 | $ret .= $ones[$num{0}]; |
---|
| 261 | |
---|
| 262 | default: |
---|
| 263 | break; |
---|
| 264 | } |
---|
| 265 | return $ret; |
---|
| 266 | |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | /** |
---|
| 270 | * Determines whether $value is a percentage or not |
---|
| 271 | * |
---|
| 272 | * @param float $value |
---|
| 273 | * @return bool |
---|
| 274 | */ |
---|
| 275 | function is_percent($value) { return false !== mb_strpos($value, "%"); } |
---|
| 276 | |
---|
| 277 | /** |
---|
| 278 | * mb_string compatibility |
---|
| 279 | */ |
---|
| 280 | |
---|
| 281 | if ( !function_exists("mb_strlen") ) { |
---|
| 282 | function mb_strlen($str) { |
---|
| 283 | return strlen($str); |
---|
| 284 | } |
---|
| 285 | } |
---|
| 286 | |
---|
| 287 | if ( !function_exists("mb_strpos") ) { |
---|
| 288 | function mb_strpos($haystack, $needle, $offset = 0) { |
---|
| 289 | return strpos($haystack, $needle, $offset); |
---|
| 290 | } |
---|
| 291 | } |
---|
| 292 | |
---|
| 293 | if ( !function_exists("mb_strrpos") ) { |
---|
| 294 | function mb_strrpos($haystack, $needle, $offset = 0) { |
---|
| 295 | return strrpos($haystack, $needle, $offset); |
---|
| 296 | } |
---|
| 297 | } |
---|
| 298 | |
---|
| 299 | if ( !function_exists("mb_substr") ) { |
---|
| 300 | function mb_substr($str, $start, $length = null) { |
---|
| 301 | if ( is_null($length) ) |
---|
| 302 | return substr($str, $start); |
---|
| 303 | else |
---|
| 304 | return substr($str, $start, $length); |
---|
| 305 | } |
---|
| 306 | } |
---|
| 307 | |
---|
| 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 | if ( !function_exists("mb_substr_count") ) { |
---|
| 321 | function mb_substr_count($haystack, $needle) { |
---|
| 322 | return substr_count($haystack, $needle); |
---|
| 323 | } |
---|
| 324 | } |
---|
| 325 | |
---|
| 326 | |
---|
| 327 | /** |
---|
| 328 | * Stores warnings in an array for display later |
---|
| 329 | * |
---|
| 330 | * This function allows warnings generated by the DomDocument parser |
---|
| 331 | * and CSS loader ({@link Stylesheet}) to be captured and displayed |
---|
| 332 | * later. Without this function, errors are displayed immediately and |
---|
| 333 | * PDF streaming is impossible. |
---|
| 334 | * |
---|
| 335 | * @see http://www.php.net/manual/en/function.set-error_handler.php |
---|
| 336 | * |
---|
| 337 | * @param int $errno |
---|
| 338 | * @param string $errstr |
---|
| 339 | * @param string $errfile |
---|
| 340 | * @param string $errline |
---|
| 341 | */ |
---|
| 342 | function record_warnings($errno, $errstr, $errfile, $errline) { |
---|
| 343 | |
---|
| 344 | if ( !($errno & (E_WARNING | E_NOTICE | E_USER_NOTICE | E_USER_WARNING )) ) // Not a warning or notice |
---|
| 345 | throw new DOMPDF_Exception($errstr . " $errno"); |
---|
| 346 | |
---|
| 347 | global $_dompdf_warnings; |
---|
| 348 | global $_dompdf_show_warnings; |
---|
| 349 | |
---|
| 350 | if ( $_dompdf_show_warnings ) |
---|
| 351 | echo $errstr . "\n"; |
---|
| 352 | |
---|
| 353 | $_dompdf_warnings[] = $errstr; |
---|
| 354 | } |
---|
| 355 | |
---|
| 356 | /** |
---|
| 357 | * Dump memory usage |
---|
| 358 | */ |
---|
| 359 | function print_memusage() { |
---|
| 360 | global $memusage; |
---|
| 361 | echo ("Memory Usage\n"); |
---|
| 362 | $prev = 0; |
---|
| 363 | $initial = reset($memusage); |
---|
| 364 | echo (str_pad("Initial:", 40) . $initial . "\n\n"); |
---|
| 365 | |
---|
| 366 | foreach ($memusage as $key=>$mem) { |
---|
| 367 | $mem -= $initial; |
---|
| 368 | echo (str_pad("$key:" , 40)); |
---|
| 369 | echo (str_pad("$mem", 12) . "(diff: " . ($mem - $prev) . ")\n"); |
---|
| 370 | $prev = $mem; |
---|
| 371 | } |
---|
| 372 | |
---|
| 373 | echo ("\n" . str_pad("Total:", 40) . memory_get_usage()) . "\n"; |
---|
| 374 | } |
---|
| 375 | |
---|
| 376 | /** |
---|
| 377 | * Initialize memory profiling code |
---|
| 378 | */ |
---|
| 379 | function enable_mem_profile() { |
---|
| 380 | global $memusage; |
---|
| 381 | $memusage = array("Startup" => memory_get_usage()); |
---|
| 382 | register_shutdown_function("print_memusage"); |
---|
| 383 | } |
---|
| 384 | |
---|
| 385 | /** |
---|
| 386 | * Record the current memory usage |
---|
| 387 | * |
---|
| 388 | * @param string $location a meaningful location |
---|
| 389 | */ |
---|
| 390 | function mark_memusage($location) { |
---|
| 391 | global $memusage; |
---|
| 392 | if ( isset($memusage) ) |
---|
| 393 | $memusage[$location] = memory_get_usage(); |
---|
| 394 | } |
---|
| 395 | |
---|
| 396 | |
---|
| 397 | ?> |
---|