[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/assign/feedback/editpdf/fpdi/ -> fpdi_bridge.php (source)

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


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1