PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 3ms app.codeStats 0ms

/vivoManagement/app/Plugin/CakePdf/Vendor/dompdf/include/functions.inc.php

https://bitbucket.org/vsposato/vivo-tools
PHP | 520 lines | 286 code | 75 blank | 159 comment | 73 complexity | a49a1e8dc9dc5e1650344d3c9eb88d04 MD5 | raw file
  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.dompdf.com/
  32. *
  33. * @link http://www.dompdf.com/
  34. * @copyright 2004 Benj Carson
  35. * @author Benj Carson <benjcarson@digitaljunkies.ca>
  36. * @contributor Helmut Tischer <htischer@weihenstephan.org>
  37. * @package dompdf
  38. *
  39. * Changes
  40. * @contributor Helmut Tischer <htischer@weihenstephan.org>
  41. * @version 0.5.1.htischer.20090507
  42. * - trailing slash of base_path in build_url is no longer optional when
  43. * required. This allows paths not ending in a slash, e.g. on dynamically
  44. * created sites with page id in the url parameters.
  45. * @version 20090601
  46. * - fix windows paths
  47. * @version 20090610
  48. * - relax windows path syntax, use uniform path delimiter. Used for background images.
  49. */
  50. /* $Id: functions.inc.php 216 2010-03-11 22:49:18Z ryan.masten $ */
  51. /**
  52. * print_r wrapper for html/cli output
  53. *
  54. * Wraps print_r() output in < pre > tags if the current sapi is not
  55. * 'cli'. Returns the output string instead of displaying it if $return is
  56. * true.
  57. *
  58. * @param mixed $mixed variable or expression to display
  59. * @param bool $return
  60. *
  61. */
  62. if ( !function_exists("pre_r") ) {
  63. function pre_r($mixed, $return = false) {
  64. if ($return)
  65. return "<pre>" . print_r($mixed, true) . "</pre>";
  66. if ( php_sapi_name() !== "cli")
  67. echo ("<pre>");
  68. print_r($mixed);
  69. if ( php_sapi_name() !== "cli")
  70. echo("</pre>");
  71. else
  72. echo ("\n");
  73. flush();
  74. }
  75. }
  76. /**
  77. * var_dump wrapper for html/cli output
  78. *
  79. * Wraps var_dump() output in < pre > tags if the current sapi is not
  80. * 'cli'.
  81. *
  82. * @param mixed $mixed variable or expression to display.
  83. */
  84. if ( !function_exists("pre_var_dump") ) {
  85. function pre_var_dump($mixed) {
  86. if ( php_sapi_name() !== "cli")
  87. echo("<pre>");
  88. var_dump($mixed);
  89. if ( php_sapi_name() !== "cli")
  90. echo("</pre>");
  91. }
  92. }
  93. /**
  94. * builds a full url given a protocol, hostname, base path and url
  95. *
  96. * @param string $protocol
  97. * @param string $host
  98. * @param string $base_path
  99. * @param string $url
  100. * @return string
  101. *
  102. * Initially the trailing slash of $base_path was optional, and conditionally appended.
  103. * However on dynamically created sites, where the page is given as url parameter,
  104. * the base path might not end with an url.
  105. * Therefore do not append a slash, and **require** the $base_url to ending in a slash
  106. * when needed.
  107. * Vice versa, on using the local file system path of a file, make sure that the slash
  108. * is appended (o.k. also for Windows)
  109. */
  110. function build_url($protocol, $host, $base_path, $url) {
  111. if ( mb_strlen($url) == 0 ) {
  112. //return $protocol . $host . rtrim($base_path, "/\\") . "/";
  113. return $protocol . $host . $base_path;
  114. }
  115. // Is the url already fully qualified?
  116. if ( mb_strpos($url, "://") !== false )
  117. return $url;
  118. $ret = $protocol;
  119. if (!in_array(mb_strtolower($protocol), array("http://", "https://", "ftp://", "ftps://"))) {
  120. //On Windows local file, an abs path can begin also with a '\' or a drive letter and colon
  121. //drive: followed by a relative path would be a drive specific default folder.
  122. //not known in php app code, treat as abs path
  123. //($url[1] !== ':' || ($url[2]!=='\\' && $url[2]!=='/'))
  124. if ($url[0] !== '/' && (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN' || ($url[0] !== '\\' && $url[1] !== ':'))) {
  125. // For rel path and local acess we ignore the host, and run the path through realpath()
  126. $ret .= realpath($base_path).'/';
  127. }
  128. $ret .= $url;
  129. return $ret;
  130. }
  131. //remote urls with backslash in html/css are not really correct, but lets be genereous
  132. if ( $url[0] === '/' || $url[0] === '\\' ) {
  133. // Absolute path
  134. $ret .= $host . $url;
  135. } else {
  136. // Relative path
  137. //$base_path = $base_path !== "" ? rtrim($base_path, "/\\") . "/" : "";
  138. $ret .= $host . $base_path . $url;
  139. }
  140. return $ret;
  141. }
  142. /**
  143. * parse a full url or pathname and return an array(protocol, host, path,
  144. * file + query + fragment)
  145. *
  146. * @param string $url
  147. * @return array
  148. */
  149. function explode_url($url) {
  150. $protocol = "";
  151. $host = "";
  152. $path = "";
  153. $file = "";
  154. $arr = parse_url($url);
  155. if ( isset($arr["scheme"]) &&
  156. $arr["scheme"] !== "file" &&
  157. mb_strlen($arr["scheme"]) > 1 ) // Exclude windows drive letters...
  158. {
  159. $protocol = $arr["scheme"] . "://";
  160. if ( isset($arr["user"]) ) {
  161. $host .= $arr["user"];
  162. if ( isset($arr["pass"]) )
  163. $host .= "@" . $arr["pass"];
  164. $host .= ":";
  165. }
  166. if ( isset($arr["host"]) )
  167. $host .= $arr["host"];
  168. if ( isset($arr["port"]) )
  169. $host .= ":" . $arr["port"];
  170. if ( isset($arr["path"]) && $arr["path"] !== "" ) {
  171. // Do we have a trailing slash?
  172. if ( $arr["path"]{ mb_strlen($arr["path"]) - 1 } === "/" ) {
  173. $path = $arr["path"];
  174. $file = "";
  175. } else {
  176. $path = dirname($arr["path"]) . "/";
  177. $file = basename($arr["path"]);
  178. }
  179. }
  180. if ( isset($arr["query"]) )
  181. $file .= "?" . $arr["query"];
  182. if ( isset($arr["fragment"]) )
  183. $file .= "#" . $arr["fragment"];
  184. } else {
  185. $i = mb_strpos($url, "file://");
  186. if ( $i !== false)
  187. $url = mb_substr($url, $i + 7);
  188. $protocol = ""; // "file://"; ? why doesn't this work... It's because of
  189. // network filenames like //COMPU/SHARENAME
  190. $host = ""; // localhost, really
  191. $file = basename($url);
  192. $path = dirname($url);
  193. // Check that the path exists
  194. if ( $path !== false ) {
  195. $path .= '/';
  196. } else {
  197. // generate a url to access the file if no real path found.
  198. $protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https://' : 'http://';
  199. $host = isset($_SERVER["HTTP_HOST"]) ? $_SERVER["HTTP_HOST"] : php_uname("n");
  200. if ( substr($arr["path"], 0, 1) === '/' ) {
  201. $path = dirname($arr["path"]);
  202. } else {
  203. $path = '/' . rtrim(dirname($_SERVER["SCRIPT_NAME"]), '/') . '/' . $arr["path"];
  204. }
  205. }
  206. }
  207. $ret = array($protocol, $host, $path, $file,
  208. "protocol" => $protocol,
  209. "host" => $host,
  210. "path" => $path,
  211. "file" => $file);
  212. return $ret;
  213. }
  214. /**
  215. * converts decimal numbers to roman numerals
  216. *
  217. * @param int $num
  218. * @return string
  219. */
  220. function dec2roman($num) {
  221. static $ones = array("", "i", "ii", "iii", "iv", "v",
  222. "vi", "vii", "viii", "ix");
  223. static $tens = array("", "x", "xx", "xxx", "xl", "l",
  224. "lx", "lxx", "lxxx", "xc");
  225. static $hund = array("", "c", "cc", "ccc", "cd", "d",
  226. "dc", "dcc", "dccc", "cm");
  227. static $thou = array("", "m", "mm", "mmm");
  228. if ( !is_numeric($num) )
  229. throw new DOMPDF_Exception("dec2roman() requires a numeric argument.");
  230. if ( $num > 4000 || $num < 0 )
  231. return "(out of range)";
  232. $num = strrev((string)$num);
  233. $ret = "";
  234. switch (mb_strlen($num)) {
  235. case 4:
  236. $ret .= $thou[$num[3]];
  237. case 3:
  238. $ret .= $hund[$num[2]];
  239. case 2:
  240. $ret .= $tens[$num[1]];
  241. case 1:
  242. $ret .= $ones[$num[0]];
  243. default:
  244. break;
  245. }
  246. return $ret;
  247. }
  248. /**
  249. * Determines whether $value is a percentage or not
  250. *
  251. * @param float $value
  252. * @return bool
  253. */
  254. function is_percent($value) { return false !== mb_strpos($value, "%"); }
  255. /**
  256. * mb_string compatibility
  257. */
  258. if ( !function_exists("mb_convert_encoding") ) {
  259. function mb_convert_encoding($data, $to_encoding, $from_encoding='UTF-8') {
  260. if (str_replace('-', '', strtolower($to_encoding)) == 'utf8') {
  261. return utf8_encode($data);
  262. } else {
  263. return utf8_decode($data);
  264. }
  265. }
  266. }
  267. if ( !function_exists("mb_detect_encoding") ) {
  268. function mb_detect_encoding($data, $encoding_list=array('iso-8859-1'), $strict=false) {
  269. return 'iso-8859-1';
  270. }
  271. }
  272. if ( !function_exists("mb_detect_order") ) {
  273. function mb_detect_order($encoding_list=array('iso-8859-1')) {
  274. return 'iso-8859-1';
  275. }
  276. }
  277. if ( !function_exists("mb_internal_encoding") ) {
  278. function mb_internal_encoding($encoding=NULL) {
  279. if (isset($encoding)) {
  280. return true;
  281. } else {
  282. return 'iso-8859-1';
  283. }
  284. }
  285. }
  286. if ( !function_exists("mb_strlen") ) {
  287. function mb_strlen($str, $encoding='iso-8859-1') {
  288. if (str_replace('-', '', strtolower($encoding)) == 'utf8') {
  289. return strlen(utf8_encode($data));
  290. } else {
  291. return strlen(utf8_decode($data));
  292. }
  293. }
  294. }
  295. if ( !function_exists("mb_strpos") ) {
  296. function mb_strpos($haystack, $needle, $offset = 0) {
  297. return strpos($haystack, $needle, $offset);
  298. }
  299. }
  300. if ( !function_exists("mb_strrpos") ) {
  301. function mb_strrpos($haystack, $needle, $offset = 0) {
  302. return strrpos($haystack, $needle, $offset);
  303. }
  304. }
  305. if ( !function_exists("mb_strtolower") ) {
  306. function mb_strtolower($str) {
  307. return strtolower($str);
  308. }
  309. }
  310. if ( !function_exists("mb_strtoupper") ) {
  311. function mb_strtoupper($str) {
  312. return strtoupper($str);
  313. }
  314. }
  315. if ( !function_exists("mb_substr") ) {
  316. function mb_substr($str, $start, $length=null, $encoding='iso-8859-1') {
  317. if ( is_null($length) )
  318. return substr($str, $start);
  319. else
  320. return substr($str, $start, $length);
  321. }
  322. }
  323. if ( !function_exists("mb_substr_count") ) {
  324. function mb_substr_count($haystack, $needle) {
  325. return substr_count($haystack, $needle);
  326. }
  327. }
  328. /**
  329. * Stores warnings in an array for display later
  330. *
  331. * This function allows warnings generated by the DomDocument parser
  332. * and CSS loader ({@link Stylesheet}) to be captured and displayed
  333. * later. Without this function, errors are displayed immediately and
  334. * PDF streaming is impossible.
  335. *
  336. * @see http://www.php.net/manual/en/function.set-error_handler.php
  337. *
  338. * @param int $errno
  339. * @param string $errstr
  340. * @param string $errfile
  341. * @param string $errline
  342. */
  343. function record_warnings($errno, $errstr, $errfile, $errline) {
  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. global $_dompdf_warnings;
  347. global $_dompdf_show_warnings;
  348. if ( $_dompdf_show_warnings )
  349. echo $errstr . "\n";
  350. $_dompdf_warnings[] = $errstr;
  351. }
  352. /**
  353. * Print a useful backtrace
  354. */
  355. function bt() {
  356. $bt = debug_backtrace();
  357. array_shift($bt); // remove actual bt() call
  358. echo "\n";
  359. $i = 0;
  360. foreach ($bt as $call) {
  361. $file = basename($call["file"]) . " (" . $call["line"] . ")";
  362. if ( isset($call["class"]) ) {
  363. $func = $call["class"] . "->" . $call["function"] . "()";
  364. } else {
  365. $func = $call["function"] . "()";
  366. }
  367. echo "#" . str_pad($i, 2, " ", STR_PAD_RIGHT) . ": " . str_pad($file.":", 42) . " $func\n";
  368. $i++;
  369. }
  370. echo "\n";
  371. }
  372. /**
  373. * Print debug messages
  374. *
  375. * @param string $type The type of debug messages to print
  376. */
  377. function dompdf_debug($type, $msg) {
  378. global $_DOMPDF_DEBUG_TYPES;
  379. global $_dompdf_show_warnings;
  380. global $_dompdf_debug;
  381. if ( isset($_DOMPDF_DEBUG_TYPES[$type]) && ($_dompdf_show_warnings || $_dompdf_debug) ) {
  382. $arr = debug_backtrace();
  383. echo basename($arr[0]["file"]) . " (" . $arr[0]["line"] ."): " . $arr[1]["function"] . ": ";
  384. pre_r($msg);
  385. }
  386. }
  387. /**
  388. * Dump memory usage
  389. */
  390. if ( !function_exists("print_memusage") ) {
  391. function print_memusage() {
  392. global $memusage;
  393. echo ("Memory Usage\n");
  394. $prev = 0;
  395. $initial = reset($memusage);
  396. echo (str_pad("Initial:", 40) . $initial . "\n\n");
  397. foreach ($memusage as $key=>$mem) {
  398. $mem -= $initial;
  399. echo (str_pad("$key:" , 40));
  400. echo (str_pad("$mem", 12) . "(diff: " . ($mem - $prev) . ")\n");
  401. $prev = $mem;
  402. }
  403. echo ("\n" . str_pad("Total:", 40) . memory_get_usage()) . "\n";
  404. }
  405. }
  406. /**
  407. * Initialize memory profiling code
  408. */
  409. if ( !function_exists("enable_mem_profile") ) {
  410. function enable_mem_profile() {
  411. global $memusage;
  412. $memusage = array("Startup" => memory_get_usage());
  413. register_shutdown_function("print_memusage");
  414. }
  415. }
  416. /**
  417. * Record the current memory usage
  418. *
  419. * @param string $location a meaningful location
  420. */
  421. if ( !function_exists("mark_memusage") ) {
  422. function mark_memusage($location) {
  423. global $memusage;
  424. if ( isset($memusage) )
  425. $memusage[$location] = memory_get_usage();
  426. }
  427. }
  428. /**
  429. * Find the current system temporary directory
  430. *
  431. * @link http://us.php.net/manual/en/function.sys-get-temp-dir.php#85261
  432. */
  433. if ( !function_exists('sys_get_temp_dir')) {
  434. function sys_get_temp_dir() {
  435. if (!empty($_ENV['TMP'])) { return realpath($_ENV['TMP']); }
  436. if (!empty($_ENV['TMPDIR'])) { return realpath( $_ENV['TMPDIR']); }
  437. if (!empty($_ENV['TEMP'])) { return realpath( $_ENV['TEMP']); }
  438. $tempfile=tempnam(uniqid(rand(),TRUE),'');
  439. if (file_exists($tempfile)) {
  440. unlink($tempfile);
  441. return realpath(dirname($tempfile));
  442. }
  443. }
  444. }