New files from unstable branch
[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             case 'pageNumber':
47                 return $this->pageNumber;
48             default:
49                 // Error handling
50                 $this->Error('Cannot access protected property '.get_class($this).':$'.$name.' / Undefined property: '.get_class($this).'::$'.$name);
51         }
52     }
53
54     function __set($name, $value) {
55         switch ($name) {
56             case 'PDFVersion':
57                 $this->PDFVersion = $value;
58                 break;
59             default:
60                 // Error handling
61                 $this->Error('Cannot access protected property '.get_class($this).':$'.$name.' / Undefined property: '.get_class($this).'::$'.$name);
62         }
63     }
64
65     /**
66      * Encryption of imported data by FPDI
67      *
68      * @param array $value
69      */
70     function pdf_write_value(&$value) {
71         switch ($value[0]) {
72                 case PDF_TYPE_STRING :
73                                 if ($this->encrypted) {
74                                     $value[1] = $this->_unescape($value[1]);
75                     $value[1] = $this->_RC4($this->_objectkey($this->_current_obj_id), $value[1]);
76                         $value[1] = $this->_escape($value[1]);
77                 } 
78                         break;
79                         
80                         case PDF_TYPE_STREAM :
81                             if ($this->encrypted) {
82                                 $value[2][1] = $this->_RC4($this->_objectkey($this->_current_obj_id), $value[2][1]);
83                 }
84                 break;
85                 
86             case PDF_TYPE_HEX :
87                 if ($this->encrypted) {
88                         $value[1] = $this->hex2str($value[1]);
89                         $value[1] = $this->_RC4($this->_objectkey($this->_current_obj_id), $value[1]);
90                     
91                         // remake hexstring of encrypted string
92                                 $value[1] = $this->str2hex($value[1]);
93                 }
94                 break;
95         }
96     }
97     
98     /**
99      * Unescapes a PDF string
100      *
101      * @param string $s
102      * @return string
103      */
104     function _unescape($s) {
105         return strtr($s, array(
106             '\\\\' => "\\",
107             '\)' => ')',
108             '\(' => '(',
109             '\\f' => chr(0x0C),
110             '\\b' => chr(0x08),
111             '\\t' => chr(0x09),
112             '\\r' => chr(0x0D),
113             '\\n' => chr(0x0A),
114         ));
115     }
116     
117     /**
118      * Hexadecimal to string
119      *
120      * @param string $hex
121      * @return string
122      */
123     function hex2str($hex) {
124         return pack("H*", str_replace(array("\r", "\n", " "), "", $hex));
125     }
126     
127     /**
128      * String to hexadecimal
129      *
130      * @param string $str
131      * @return string
132      */
133     function str2hex($str) {
134         return current(unpack("H*", $str));
135     }
136 }