weekly release 3.0dev
[moodle.git] / mod / assign / feedback / editpdf / fpdi / fpdi2tcpdf_bridge.php
1 <?php
2 //
3 //  FPDI - Version 1.4.4
4 //
5 //    Copyright 2004-2013 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 //
20 global $CFG;
21 require_once($CFG->libdir.'/pdflib.php');
23 /**
24  * This class is used as a bridge between TCPDF and FPDI
25  * and will create the possibility to use both FPDF and TCPDF
26  * via one FPDI version.
27  * 
28  * We'll simply remap TCPDF to FPDF again.
29  * 
30  * It'll be loaded and extended by FPDF_TPL.
31  * Modified to extend the moodle TCPDF wrapper instead.
32  */
33 class FPDF extends pdf {
34     
35         function _putstream($s, $n=0) {
36                 $this->_out($this->_getstream($s));
37         }
38         
39         function _getxobjectdict() {
40         $out = parent::_getxobjectdict();
41         if (count($this->tpls)) {
42             foreach($this->tpls as $tplidx => $tpl) {
43                 $out .= sprintf('%s%d %d 0 R', $this->tplprefix, $tplidx, $tpl['n']);
44             }
45         }
46         
47         return $out;
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->_encrypt_data($this->_current_obj_id, $value[1]);
61                         $value[1] = TCPDF_STATIC::_escape($value[1]);
62                 } 
63                         break;
64                         
65                         case PDF_TYPE_STREAM:
66                             if ($this->encrypted) {
67                                 $value[2][1] = $this->_encrypt_data($this->_current_obj_id, $value[2][1]);
68                                 $value[1][1]['/Length'] = array(
69                         PDF_TYPE_NUMERIC,
70                         strlen($value[2][1])
71                     );
72                 }
73                 break;
74                 
75             case PDF_TYPE_HEX:
76                 if ($this->encrypted) {
77                         $value[1] = $this->hex2str($value[1]);
78                         $value[1] = $this->_encrypt_data($this->_current_obj_id, $value[1]);
79                     
80                         // remake hexstring of encrypted string
81                                 $value[1] = $this->str2hex($value[1]);
82                 }
83                 break;
84         }
85     }
86     
87     /**
88      * Unescapes a PDF string
89      *
90      * @param string $s
91      * @return string
92      */
93     function _unescape($s) {
94         $out = '';
95         for ($count = 0, $n = strlen($s); $count < $n; $count++) {
96             if ($s[$count] != '\\' || $count == $n-1) {
97                 $out .= $s[$count];
98             } else {
99                 switch ($s[++$count]) {
100                     case ')':
101                     case '(':
102                     case '\\':
103                         $out .= $s[$count];
104                         break;
105                     case 'f':
106                         $out .= chr(0x0C);
107                         break;
108                     case 'b':
109                         $out .= chr(0x08);
110                         break;
111                     case 't':
112                         $out .= chr(0x09);
113                         break;
114                     case 'r':
115                         $out .= chr(0x0D);
116                         break;
117                     case 'n':
118                         $out .= chr(0x0A);
119                         break;
120                     case "\r":
121                         if ($count != $n-1 && $s[$count+1] == "\n")
122                             $count++;
123                         break;
124                     case "\n":
125                         break;
126                     default:
127                         // Octal-Values
128                         if (ord($s[$count]) >= ord('0') &&
129                             ord($s[$count]) <= ord('9')) {
130                             $oct = ''. $s[$count];
131                                 
132                             if (ord($s[$count+1]) >= ord('0') &&
133                                 ord($s[$count+1]) <= ord('9')) {
134                                 $oct .= $s[++$count];
135                                 
136                                 if (ord($s[$count+1]) >= ord('0') &&
137                                     ord($s[$count+1]) <= ord('9')) {
138                                     $oct .= $s[++$count];    
139                                 }                            
140                             }
141                             
142                             $out .= chr(octdec($oct));
143                         } else {
144                             $out .= $s[$count];
145                         }
146                 }
147             }
148         }
149         return $out;
150     }
151     
152     /**
153      * Hexadecimal to string
154      *
155      * @param string $hex
156      * @return string
157      */
158     function hex2str($hex) {
159         return pack('H*', str_replace(array("\r", "\n", ' '), '', $hex));
160     }
161     
162     /**
163      * String to hexadecimal
164      *
165      * @param string $str
166      * @return string
167      */
168     function str2hex($str) {
169         return current(unpack('H*', $str));
170     }