Projet

Général

Profil

Paste
Télécharger (6,58 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / libraries / fpdi-1.5.4 / fpdi_bridge.php @ b75b6b8b

1
<?php
2
//
3
//  FPDI - Version 1.5.4
4
//
5
//    Copyright 2004-2015 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 file is used as a bridge between TCPDF or FPDF
22
 * It will dynamically create the class extending the available
23
 * class FPDF or TCPDF.
24
 *
25
 * This way it is possible to use FPDI for both FPDF and TCPDF with one FPDI version.
26
 */
27

    
28
if (!class_exists('TCPDF', false)) {
29
    /**
30
     * Class fpdi_bridge
31
     */
32
    class fpdi_bridge extends FPDF
33
    {
34
        // empty body
35
    }
36

    
37
} else {
38

    
39
    /**
40
     * Class fpdi_bridge
41
     */
42
    class fpdi_bridge extends TCPDF
43
    {
44
        /**
45
         * Array of Tpl-Data
46
         *
47
         * @var array
48
         */
49
        protected $_tpls = array();
50

    
51
        /**
52
         * Name-prefix of Templates used in Resources-Dictionary
53
         *
54
         * @var string A String defining the Prefix used as Template-Object-Names. Have to begin with an /
55
         */
56
        public $tplPrefix = "/TPL";
57

    
58
        /**
59
         * Current Object Id.
60
         *
61
         * @var integer
62
         */
63
        protected $_currentObjId;
64

    
65
        /**
66
         * Return XObjects Dictionary.
67
         *
68
         * Overwritten to add additional XObjects to the resources dictionary of TCPDF
69
         *
70
         * @return string
71
         */
72
        protected function _getxobjectdict()
73
        {
74
            $out = parent::_getxobjectdict();
75
            foreach ($this->_tpls as $tplIdx => $tpl) {
76
                $out .= sprintf('%s%d %d 0 R', $this->tplPrefix, $tplIdx, $tpl['n']);
77
            }
78

    
79
            return $out;
80
        }
81

    
82
        /**
83
         * Writes a PDF value to the resulting document.
84
         *
85
         * Prepares the value for encryption of imported data by FPDI
86
         *
87
         * @param array $value
88
         */
89
        protected function _prepareValue(&$value)
90
        {
91
            switch ($value[0]) {
92
                case pdf_parser::TYPE_STRING:
93
                    if ($this->encrypted) {
94
                        $value[1] = $this->_unescape($value[1]);
95
                        $value[1] = $this->_encrypt_data($this->_currentObjId, $value[1]);
96
                        $value[1] = TCPDF_STATIC::_escape($value[1]);
97
                    }
98
                    break;
99

    
100
                case pdf_parser::TYPE_STREAM:
101
                    if ($this->encrypted) {
102
                        $value[2][1] = $this->_encrypt_data($this->_currentObjId, $value[2][1]);
103
                        $value[1][1]['/Length'] = array(
104
                            pdf_parser::TYPE_NUMERIC,
105
                            strlen($value[2][1])
106
                        );
107
                    }
108
                    break;
109

    
110
                case pdf_parser::TYPE_HEX:
111
                    if ($this->encrypted) {
112
                        $value[1] = $this->hex2str($value[1]);
113
                        $value[1] = $this->_encrypt_data($this->_currentObjId, $value[1]);
114

    
115
                        // remake hexstring of encrypted string
116
                        $value[1] = $this->str2hex($value[1]);
117
                    }
118
                    break;
119
            }
120
        }
121

    
122
        /**
123
         * Un-escapes a PDF string
124
         *
125
         * @param string $s
126
         * @return string
127
         */
128
        protected function _unescape($s)
129
        {
130
            $out = '';
131
            for ($count = 0, $n = strlen($s); $count < $n; $count++) {
132
                if ($s[$count] != '\\' || $count == $n-1) {
133
                    $out .= $s[$count];
134
                } else {
135
                    switch ($s[++$count]) {
136
                        case ')':
137
                        case '(':
138
                        case '\\':
139
                            $out .= $s[$count];
140
                            break;
141
                        case 'f':
142
                            $out .= chr(0x0C);
143
                            break;
144
                        case 'b':
145
                            $out .= chr(0x08);
146
                            break;
147
                        case 't':
148
                            $out .= chr(0x09);
149
                            break;
150
                        case 'r':
151
                            $out .= chr(0x0D);
152
                            break;
153
                        case 'n':
154
                            $out .= chr(0x0A);
155
                            break;
156
                        case "\r":
157
                            if ($count != $n-1 && $s[$count+1] == "\n")
158
                                $count++;
159
                            break;
160
                        case "\n":
161
                            break;
162
                        default:
163
                            // Octal-Values
164
                            if (ord($s[$count]) >= ord('0') &&
165
                                ord($s[$count]) <= ord('9')) {
166
                                $oct = ''. $s[$count];
167

    
168
                                if (ord($s[$count+1]) >= ord('0') &&
169
                                    ord($s[$count+1]) <= ord('9')) {
170
                                    $oct .= $s[++$count];
171

    
172
                                    if (ord($s[$count+1]) >= ord('0') &&
173
                                        ord($s[$count+1]) <= ord('9')) {
174
                                        $oct .= $s[++$count];
175
                                    }
176
                                }
177

    
178
                                $out .= chr(octdec($oct));
179
                            } else {
180
                                $out .= $s[$count];
181
                            }
182
                    }
183
                }
184
            }
185
            return $out;
186
        }
187

    
188
        /**
189
         * Hexadecimal to string
190
         *
191
         * @param string $data
192
         * @return string
193
         */
194
        public function hex2str($data)
195
        {
196
            $data = preg_replace('/[^0-9A-Fa-f]/', '', rtrim($data, '>'));
197
            if ((strlen($data) % 2) == 1) {
198
                $data .= '0';
199
            }
200

    
201
            return pack('H*', $data);
202
        }
203

    
204
        /**
205
         * String to hexadecimal
206
         *
207
         * @param string $str
208
         * @return string
209
         */
210
        public function str2hex($str)
211
        {
212
            return current(unpack('H*', $str));
213
        }
214
    }
215
}