PageRenderTime 23ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/assign/feedback/editpdf/fpdi/fpdi_bridge.php

https://github.com/pauln/moodle
PHP | 208 lines | 116 code | 19 blank | 73 comment | 19 complexity | 980f614c11a36df1c22ec62a325cdffb MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of FPDI
  4. *
  5. * @package FPDI
  6. * @copyright Copyright (c) 2015 Setasign - Jan Slabon (http://www.setasign.com)
  7. * @license http://opensource.org/licenses/mit-license The MIT License
  8. * @version 1.6.1
  9. */
  10. /**
  11. * This file is used as a bridge between TCPDF or FPDF
  12. * It will dynamically create the class extending the available
  13. * class FPDF or TCPDF.
  14. *
  15. * This way it is possible to use FPDI for both FPDF and TCPDF with one FPDI version.
  16. */
  17. if (!class_exists('TCPDF', false)) {
  18. /**
  19. * Class fpdi_bridge
  20. */
  21. class fpdi_bridge extends FPDF
  22. {
  23. // empty body
  24. }
  25. } else {
  26. /**
  27. * Class fpdi_bridge
  28. *
  29. * This has been modified to use the Moodle pdf class which in turn extends the TCPDF class.
  30. */
  31. class fpdi_bridge extends pdf
  32. {
  33. /**
  34. * Array of Tpl-Data
  35. *
  36. * @var array
  37. */
  38. protected $_tpls = array();
  39. /**
  40. * Name-prefix of Templates used in Resources-Dictionary
  41. *
  42. * @var string A String defining the Prefix used as Template-Object-Names. Have to begin with an /
  43. */
  44. public $tplPrefix = "/TPL";
  45. /**
  46. * Current Object Id.
  47. *
  48. * @var integer
  49. */
  50. protected $_currentObjId;
  51. /**
  52. * Return XObjects Dictionary.
  53. *
  54. * Overwritten to add additional XObjects to the resources dictionary of TCPDF
  55. *
  56. * @return string
  57. */
  58. protected function _getxobjectdict()
  59. {
  60. $out = parent::_getxobjectdict();
  61. foreach ($this->_tpls as $tplIdx => $tpl) {
  62. $out .= sprintf('%s%d %d 0 R', $this->tplPrefix, $tplIdx, $tpl['n']);
  63. }
  64. return $out;
  65. }
  66. /**
  67. * Writes a PDF value to the resulting document.
  68. *
  69. * Prepares the value for encryption of imported data by FPDI
  70. *
  71. * @param array $value
  72. */
  73. protected function _prepareValue(&$value)
  74. {
  75. switch ($value[0]) {
  76. case pdf_parser::TYPE_STRING:
  77. if ($this->encrypted) {
  78. $value[1] = $this->_unescape($value[1]);
  79. $value[1] = $this->_encrypt_data($this->_currentObjId, $value[1]);
  80. $value[1] = TCPDF_STATIC::_escape($value[1]);
  81. }
  82. break;
  83. case pdf_parser::TYPE_STREAM:
  84. if ($this->encrypted) {
  85. $value[2][1] = $this->_encrypt_data($this->_currentObjId, $value[2][1]);
  86. $value[1][1]['/Length'] = array(
  87. pdf_parser::TYPE_NUMERIC,
  88. strlen($value[2][1])
  89. );
  90. }
  91. break;
  92. case pdf_parser::TYPE_HEX:
  93. if ($this->encrypted) {
  94. $value[1] = $this->hex2str($value[1]);
  95. $value[1] = $this->_encrypt_data($this->_currentObjId, $value[1]);
  96. // remake hexstring of encrypted string
  97. $value[1] = $this->str2hex($value[1]);
  98. }
  99. break;
  100. }
  101. }
  102. /**
  103. * Un-escapes a PDF string
  104. *
  105. * @param string $s
  106. * @return string
  107. */
  108. protected function _unescape($s)
  109. {
  110. $out = '';
  111. for ($count = 0, $n = strlen($s); $count < $n; $count++) {
  112. if ($s[$count] != '\\' || $count == $n-1) {
  113. $out .= $s[$count];
  114. } else {
  115. switch ($s[++$count]) {
  116. case ')':
  117. case '(':
  118. case '\\':
  119. $out .= $s[$count];
  120. break;
  121. case 'f':
  122. $out .= chr(0x0C);
  123. break;
  124. case 'b':
  125. $out .= chr(0x08);
  126. break;
  127. case 't':
  128. $out .= chr(0x09);
  129. break;
  130. case 'r':
  131. $out .= chr(0x0D);
  132. break;
  133. case 'n':
  134. $out .= chr(0x0A);
  135. break;
  136. case "\r":
  137. if ($count != $n-1 && $s[$count+1] == "\n")
  138. $count++;
  139. break;
  140. case "\n":
  141. break;
  142. default:
  143. // Octal-Values
  144. if (ord($s[$count]) >= ord('0') &&
  145. ord($s[$count]) <= ord('9')) {
  146. $oct = ''. $s[$count];
  147. if (ord($s[$count+1]) >= ord('0') &&
  148. ord($s[$count+1]) <= ord('9')) {
  149. $oct .= $s[++$count];
  150. if (ord($s[$count+1]) >= ord('0') &&
  151. ord($s[$count+1]) <= ord('9')) {
  152. $oct .= $s[++$count];
  153. }
  154. }
  155. $out .= chr(octdec($oct));
  156. } else {
  157. $out .= $s[$count];
  158. }
  159. }
  160. }
  161. }
  162. return $out;
  163. }
  164. /**
  165. * Hexadecimal to string
  166. *
  167. * @param string $data
  168. * @return string
  169. */
  170. public function hex2str($data)
  171. {
  172. $data = preg_replace('/[^0-9A-Fa-f]/', '', rtrim($data, '>'));
  173. if ((strlen($data) % 2) == 1) {
  174. $data .= '0';
  175. }
  176. return pack('H*', $data);
  177. }
  178. /**
  179. * String to hexadecimal
  180. *
  181. * @param string $str
  182. * @return string
  183. */
  184. public function str2hex($str)
  185. {
  186. return current(unpack('H*', $str));
  187. }
  188. }
  189. }