Integration of fpdi class.
[fa-stable.git] / reporting / includes / fpdi / fpdi2tcpdf_bridge.php
1 <?php
2 //
3 //  FPDI - Version 1.2.1
4 //
5 //    Copyright 2004-2008 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 /**
21  * This class is used as a bridge between TCPDF and FPDI
22  * and will create the possibility to use both FPDF and TCPDF
23  * via one FPDI version.
24  * 
25  * We'll simply remap TCPDF to FPDF again.
26  * 
27  * It'll be loaded and extended by FPDF_TPL.
28  */
29 class FPDF extends TCPDF {
30     
31     /**
32      * Missing in TCPDF
33      *
34      * @var string
35      */
36     var $padding;
37     
38     function __get($name) {
39         switch ($name) {
40             case 'PDFVersion':
41                 return $this->PDFVersion;
42             case 'k':
43                 return $this->k;
44             case 'lastUsedPageBox':
45                 return $this->lastUsedPageBox;
46             default:
47                 // Error handling
48                 $this->Error('Cannot access protected property '.get_class($this).':$'.$name.' / Undefined property: '.get_class($this).'::$'.$name);
49         }
50     }
51
52     function __set($name, $value) {
53         switch ($name) {
54             case 'PDFVersion':
55                 $this->PDFVersion = $value;
56                 break;
57             default:
58                 // Error handling
59                 $this->Error('Cannot access protected property '.get_class($this).':$'.$name.' / Undefined property: '.get_class($this).'::$'.$name);
60         }
61     }
62
63     /**
64      * Encryption of imported data by FPDI
65      *
66      * @param array $value
67      */
68     function pdf_write_value(&$value) {
69         switch ($value[0]) {
70                 case PDF_TYPE_STRING :
71                                 if ($this->encrypted) {
72                                     $value[1] = $this->_unescape($value[1]);
73                     $value[1] = $this->_RC4($this->_objectkey($this->_current_obj_id), $value[1]);
74                         $value[1] = $this->_escape($value[1]);
75                 } 
76                         break;
77                         
78                         case PDF_TYPE_STREAM :
79                             if ($this->encrypted) {
80                                 $value[2][1] = $this->_RC4($this->_objectkey($this->_current_obj_id), $value[2][1]);
81                 }
82                 break;
83                 
84             case PDF_TYPE_HEX :
85                 if ($this->encrypted) {
86                         $value[1] = $this->hex2str($value[1]);
87                         $value[1] = $this->_RC4($this->_objectkey($this->_current_obj_id), $value[1]);
88                     
89                         // remake hexstring of encrypted string
90                                 $value[1] = $this->str2hex($value[1]);
91                 }
92                 break;
93         }
94     }
95     
96     /**
97      * Unescapes a PDF string
98      *
99      * @param string $s
100      * @return string
101      */
102     function _unescape($s) {
103         return strtr($s, array(
104             '\\\\' => "\\",
105             '\)' => ')',
106             '\(' => '(',
107             '\\f' => chr(0x0C),
108             '\\b' => chr(0x08),
109             '\\t' => chr(0x09),
110             '\\r' => chr(0x0D),
111             '\\n' => chr(0x0A),
112         ));
113     }
114     
115     /**
116      * Hexadecimal to string
117      *
118      * @param string $hex
119      * @return string
120      */
121     function hex2str($hex) {
122         return pack("H*", str_replace(array("\r", "\n", " "), "", $hex));
123     }
124     
125     /**
126      * String to hexadecimal
127      *
128      * @param string $str
129      * @return string
130      */
131     function str2hex($str) {
132         return current(unpack("H*", $str));
133     }
134 }