functions.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. // mPDF 6
  3. // Function only available PHP >=5.5.0
  4. if(!function_exists('imagepalettetotruecolor')) {
  5. function imagepalettetotruecolor(&$src) {
  6. if(imageistruecolor($src)) {
  7. return(true);
  8. }
  9. $dst = imagecreatetruecolor(imagesx($src), imagesy($src));
  10. imagecopy($dst, $src, 0, 0, 0, 0, imagesx($src), imagesy($src));
  11. imagedestroy($src);
  12. $src = $dst;
  13. return(true);
  14. }
  15. }
  16. // mPDF 5.7
  17. // Replace a section of an array with the elements in reverse
  18. function array_splice_reverse(&$arr, $offset, $length) {
  19. $tmp = (array_reverse(array_slice($arr, $offset, $length)));
  20. array_splice($arr, $offset, $length, $tmp);
  21. }
  22. function array_insert(&$array, $value, $offset) {
  23. if (is_array($array)) {
  24. $array = array_values($array);
  25. $offset = intval($offset);
  26. if ($offset < 0 || $offset >= count($array)) { array_push($array, $value); }
  27. else if ($offset == 0) { array_unshift($array, $value); }
  28. else {
  29. $temp = array_slice($array, 0, $offset);
  30. array_push($temp, $value);
  31. $array = array_slice($array, $offset);
  32. $array = array_merge($temp, $array);
  33. }
  34. }
  35. else { $array = array($value); }
  36. return count($array);
  37. }
  38. // mPDF 5.7.4 URLs
  39. function urldecode_parts($url) {
  40. $file=$url;
  41. $query='';
  42. if (preg_match('/[?]/',$url)) {
  43. $bits = preg_split('/[?]/',$url,2);
  44. $file=$bits[0];
  45. $query='?'.$bits[1];
  46. }
  47. $file = rawurldecode($file);
  48. $query = urldecode($query);
  49. return $file.$query;
  50. }
  51. function _strspn($str1, $str2, $start=null, $length=null) {
  52. $numargs = func_num_args();
  53. if ($numargs == 2) {
  54. return strspn($str1, $str2);
  55. }
  56. else if ($numargs == 3) {
  57. return strspn($str1, $str2, $start);
  58. }
  59. else {
  60. return strspn($str1, $str2, $start, $length);
  61. }
  62. }
  63. function _strcspn($str1, $str2, $start=null, $length=null) {
  64. $numargs = func_num_args();
  65. if ($numargs == 2) {
  66. return strcspn($str1, $str2);
  67. }
  68. else if ($numargs == 3) {
  69. return strcspn($str1, $str2, $start);
  70. }
  71. else {
  72. return strcspn($str1, $str2, $start, $length);
  73. }
  74. }
  75. function _fgets (&$h, $force=false) {
  76. $startpos = ftell($h);
  77. $s = fgets($h, 1024);
  78. if ($force && preg_match("/^([^\r\n]*[\r\n]{1,2})(.)/",trim($s), $ns)) {
  79. $s = $ns[1];
  80. fseek($h,$startpos+strlen($s));
  81. }
  82. return $s;
  83. }
  84. // For PHP4 compatability
  85. if(!function_exists('str_ireplace')) {
  86. function str_ireplace($search,$replace,$subject) {
  87. $search = preg_quote($search, "/");
  88. return preg_replace("/".$search."/i", $replace, $subject);
  89. }
  90. }
  91. if(!function_exists('htmlspecialchars_decode')) {
  92. function htmlspecialchars_decode ($str) {
  93. return strtr($str, array_flip(get_html_translation_table(HTML_SPECIALCHARS)));
  94. }
  95. }
  96. function PreparePreText($text,$ff='//FF//') {
  97. $text = htmlspecialchars($text);
  98. if ($ff) { $text = str_replace($ff,'</pre><formfeed /><pre>',$text); }
  99. return ('<pre>'.$text.'</pre>');
  100. }
  101. if(!function_exists('strcode2utf')){
  102. function strcode2utf($str,$lo=true) {
  103. //converts all the &#nnn; and &#xhhh; in a string to Unicode
  104. // mPDF 5.7
  105. if ($lo) {
  106. $str = preg_replace_callback('/\&\#([0-9]+)\;/m', 'code2utf_lo_callback', $str);
  107. $str = preg_replace_callback('/\&\#x([0-9a-fA-F]+)\;/m', 'codeHex2utf_lo_callback', $str);
  108. }
  109. else {
  110. $str = preg_replace_callback('/\&\#([0-9]+)\;/m', 'code2utf_callback', $str);
  111. $str = preg_replace_callback('/\&\#x([0-9a-fA-F]+)\;/m', 'codeHex2utf_callback', $str);
  112. }
  113. return $str;
  114. }
  115. }
  116. function code2utf_callback($matches) {
  117. return code2utf($matches[1], 0);
  118. }
  119. function code2utf_lo_callback($matches) {
  120. return code2utf($matches[1], 1);
  121. }
  122. function codeHex2utf_callback($matches) {
  123. return codeHex2utf($matches[1], 0);
  124. }
  125. function codeHex2utf_lo_callback($matches) {
  126. return codeHex2utf($matches[1], 1);
  127. }
  128. if(!function_exists('code2utf')){
  129. function code2utf($num,$lo=true){
  130. //Returns the utf string corresponding to the unicode value
  131. if ($num<128) {
  132. if ($lo) return chr($num);
  133. else return '&#'.$num.';';
  134. }
  135. if ($num<2048) return chr(($num>>6)+192).chr(($num&63)+128);
  136. if ($num<65536) return chr(($num>>12)+224).chr((($num>>6)&63)+128).chr(($num&63)+128);
  137. if ($num<2097152) return chr(($num>>18)+240).chr((($num>>12)&63)+128).chr((($num>>6)&63)+128) .chr(($num&63)+128);
  138. return '?';
  139. }
  140. }
  141. if(!function_exists('codeHex2utf')){
  142. function codeHex2utf($hex,$lo=true){
  143. $num = hexdec($hex);
  144. if (($num<128) && !$lo) return '&#x'.$hex.';';
  145. return code2utf($num,$lo);
  146. }
  147. }