PageRenderTime 46ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/include/fpdi/fpdi2tcpdf_bridge.php

https://bitbucket.org/ite/on-track-code-base
PHP | 171 lines | 105 code | 13 blank | 53 comment | 17 complexity | b3641b867dab85cc4fe21c792c0e2ce6 MD5 | raw file
  1. <?php
  2. //
  3. // FPDI - Version 1.3.1
  4. //
  5. // Copyright 2004-2009 Setasign - Jan Slabon
  6. //
  7. // Licensed under the Apache License, Version 2.0 (the "License");
  8. // you may not use this file except in compliance with the License.
  9. // You may obtain a copy of the License at
  10. //
  11. // http://www.apache.org/licenses/LICENSE-2.0
  12. //
  13. // Unless required by applicable law or agreed to in writing, software
  14. // distributed under the License is distributed on an "AS IS" BASIS,
  15. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. // See the License for the specific language governing permissions and
  17. // limitations under the License.
  18. //
  19. /**
  20. * This class is used as a bridge between TCPDF and FPDI
  21. * and will create the possibility to use both FPDF and TCPDF
  22. * via one FPDI version.
  23. *
  24. * We'll simply remap TCPDF to FPDF again.
  25. *
  26. * It'll be loaded and extended by FPDF_TPL.
  27. */
  28. class FPDF extends TCPDF {
  29. function __get($name) {
  30. switch ($name) {
  31. case 'PDFVersion':
  32. return $this->PDFVersion;
  33. case 'k':
  34. return $this->k;
  35. default:
  36. // Error handling
  37. $this->Error('Cannot access protected property '.get_class($this).':$'.$name.' / Undefined property: '.get_class($this).'::$'.$name);
  38. }
  39. }
  40. function __set($name, $value) {
  41. switch ($name) {
  42. case 'PDFVersion':
  43. $this->PDFVersion = $value;
  44. break;
  45. default:
  46. // Error handling
  47. $this->Error('Cannot access protected property '.get_class($this).':$'.$name.' / Undefined property: '.get_class($this).'::$'.$name);
  48. }
  49. }
  50. /**
  51. * Encryption of imported data by FPDI
  52. *
  53. * @param array $value
  54. */
  55. function pdf_write_value(&$value) {
  56. switch ($value[0]) {
  57. case PDF_TYPE_STRING :
  58. if ($this->encrypted) {
  59. $value[1] = $this->_unescape($value[1]);
  60. $value[1] = $this->_RC4($this->_objectkey($this->_current_obj_id), $value[1]);
  61. $value[1] = $this->_escape($value[1]);
  62. }
  63. break;
  64. case PDF_TYPE_STREAM :
  65. if ($this->encrypted) {
  66. $value[2][1] = $this->_RC4($this->_objectkey($this->_current_obj_id), $value[2][1]);
  67. }
  68. break;
  69. case PDF_TYPE_HEX :
  70. if ($this->encrypted) {
  71. $value[1] = $this->hex2str($value[1]);
  72. $value[1] = $this->_RC4($this->_objectkey($this->_current_obj_id), $value[1]);
  73. // remake hexstring of encrypted string
  74. $value[1] = $this->str2hex($value[1]);
  75. }
  76. break;
  77. }
  78. }
  79. /**
  80. * Unescapes a PDF string
  81. *
  82. * @param string $s
  83. * @return string
  84. */
  85. function _unescape($s) {
  86. $out = '';
  87. for ($count = 0, $n = strlen($s); $count < $n; $count++) {
  88. if ($s[$count] != '\\' || $count == $n-1) {
  89. $out .= $s[$count];
  90. } else {
  91. switch ($s[++$count]) {
  92. case ')':
  93. case '(':
  94. case '\\':
  95. $out .= $s[$count];
  96. break;
  97. case 'f':
  98. $out .= chr(0x0C);
  99. break;
  100. case 'b':
  101. $out .= chr(0x08);
  102. break;
  103. case 't':
  104. $out .= chr(0x09);
  105. break;
  106. case 'r':
  107. $out .= chr(0x0D);
  108. break;
  109. case 'n':
  110. $out .= chr(0x0A);
  111. break;
  112. case "\r":
  113. if ($count != $n-1 && $s[$count+1] == "\n")
  114. $count++;
  115. break;
  116. case "\n":
  117. break;
  118. default:
  119. // Octal-Values
  120. if (ord($s[$count]) >= ord('0') &&
  121. ord($s[$count]) <= ord('9')) {
  122. $oct = ''. $s[$count];
  123. if (ord($s[$count+1]) >= ord('0') &&
  124. ord($s[$count+1]) <= ord('9')) {
  125. $oct .= $s[++$count];
  126. if (ord($s[$count+1]) >= ord('0') &&
  127. ord($s[$count+1]) <= ord('9')) {
  128. $oct .= $s[++$count];
  129. }
  130. }
  131. $out .= chr(octdec($oct));
  132. } else {
  133. $out .= $s[$count];
  134. }
  135. }
  136. }
  137. }
  138. return $out;
  139. }
  140. /**
  141. * Hexadecimal to string
  142. *
  143. * @param string $hex
  144. * @return string
  145. */
  146. function hex2str($hex) {
  147. return pack('H*', str_replace(array("\r", "\n", ' '), '', $hex));
  148. }
  149. /**
  150. * String to hexadecimal
  151. *
  152. * @param string $str
  153. * @return string
  154. */
  155. function str2hex($str) {
  156. return current(unpack('H*', $str));
  157. }
  158. }