phpBMS

root/trunk/phpbms/fpdf/fpdi_pdf_parser.php

Revision 712, 10.7 KB (checked in by brieb, 2 years ago)
  • fixed #393 - issue wth FPDI and php5.3 using depreciated object pointers during creation.
  • added link to phpBMS registration. Doing this in hopes to collect valuable information about phpBMS users and where to guide the project in the future
  • Property svn:executable set to *
Line 
1<?php
2//
3//  FPDI - Version 1.3.1
4//
5//    Copyright 2004-2009 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
20require_once('pdf_parser.php');
21
22class fpdi_pdf_parser extends pdf_parser {
23
24    /**
25     * Pages
26     * Index beginns at 0
27     *
28     * @var array
29     */
30    var $pages;
31
32    /**
33     * Page count
34     * @var integer
35     */
36    var $page_count;
37
38    /**
39     * actual page number
40     * @var integer
41     */
42    var $pageno;
43
44    /**
45     * PDF Version of imported Document
46     * @var string
47     */
48    var $pdfVersion;
49
50    /**
51     * FPDI Reference
52     * @var object
53     */
54    var $fpdi;
55
56    /**
57     * Available BoxTypes
58     *
59     * @var array
60     */
61    var $availableBoxes = array('/MediaBox', '/CropBox', '/BleedBox', '/TrimBox', '/ArtBox');
62
63    /**
64     * Constructor
65     *
66     * @param string $filename  Source-Filename
67     * @param object $fpdi      Object of type fpdi
68     */
69    function fpdi_pdf_parser($filename, &$fpdi) {
70        $this->fpdi =& $fpdi;
71
72        parent::pdf_parser($filename);
73
74        // resolve Pages-Dictonary
75        $pages = $this->pdf_resolve_object($this->c, $this->root[1][1]['/Pages']);
76
77        // Read pages
78        $this->read_pages($this->c, $pages, $this->pages);
79
80        // count pages;
81        $this->page_count = count($this->pages);
82    }
83
84    /**
85     * Overwrite parent::error()
86     *
87     * @param string $msg  Error-Message
88     */
89    function error($msg) {
90        $this->fpdi->error($msg);
91    }
92
93    /**
94     * Get pagecount from sourcefile
95     *
96     * @return int
97     */
98    function getPageCount() {
99        return $this->page_count;
100    }
101
102
103    /**
104     * Set pageno
105     *
106     * @param int $pageno Pagenumber to use
107     */
108    function setPageno($pageno) {
109        $pageno = ((int) $pageno) - 1;
110
111        if ($pageno < 0 || $pageno >= $this->getPageCount()) {
112            $this->fpdi->error('Pagenumber is wrong!');
113        }
114
115        $this->pageno = $pageno;
116    }
117
118    /**
119     * Get page-resources from current page
120     *
121     * @return array
122     */
123    function getPageResources() {
124        return $this->_getPageResources($this->pages[$this->pageno]);
125    }
126
127    /**
128     * Get page-resources from /Page
129     *
130     * @param array $obj Array of pdf-data
131     */
132    function _getPageResources ($obj) { // $obj = /Page
133        $obj = $this->pdf_resolve_object($this->c, $obj);
134
135        // If the current object has a resources
136        // dictionary associated with it, we use
137        // it. Otherwise, we move back to its
138        // parent object.
139        if (isset ($obj[1][1]['/Resources'])) {
140                $res = $this->pdf_resolve_object($this->c, $obj[1][1]['/Resources']);
141                if ($res[0] == PDF_TYPE_OBJECT)
142                return $res[1];
143            return $res;
144        } else {
145                if (!isset ($obj[1][1]['/Parent'])) {
146                        return false;
147                } else {
148                $res = $this->_getPageResources($obj[1][1]['/Parent']);
149                if ($res[0] == PDF_TYPE_OBJECT)
150                    return $res[1];
151                return $res;
152                }
153        }
154    }
155
156
157    /**
158     * Get content of current page
159     *
160     * If more /Contents is an array, the streams are concated
161     *
162     * @return string
163     */
164    function getContent() {
165        $buffer = '';
166
167        if (isset($this->pages[$this->pageno][1][1]['/Contents'])) {
168            $contents = $this->_getPageContent($this->pages[$this->pageno][1][1]['/Contents']);
169            foreach($contents AS $tmp_content) {
170                $buffer .= $this->_rebuildContentStream($tmp_content).' ';
171            }
172        }
173
174        return $buffer;
175    }
176
177
178    /**
179     * Resolve all content-objects
180     *
181     * @param array $content_ref
182     * @return array
183     */
184    function _getPageContent($content_ref) {
185        $contents = array();
186
187        if ($content_ref[0] == PDF_TYPE_OBJREF) {
188            $content = $this->pdf_resolve_object($this->c, $content_ref);
189            if ($content[1][0] == PDF_TYPE_ARRAY) {
190                $contents = $this->_getPageContent($content[1]);
191            } else {
192                $contents[] = $content;
193            }
194        } else if ($content_ref[0] == PDF_TYPE_ARRAY) {
195            foreach ($content_ref[1] AS $tmp_content_ref) {
196                $contents = array_merge($contents,$this->_getPageContent($tmp_content_ref));
197            }
198        }
199
200        return $contents;
201    }
202
203
204    /**
205     * Rebuild content-streams
206     *
207     * @param array $obj
208     * @return string
209     */
210    function _rebuildContentStream($obj) {
211        $filters = array();
212
213        if (isset($obj[1][1]['/Filter'])) {
214            $_filter = $obj[1][1]['/Filter'];
215
216            if ($_filter[0] == PDF_TYPE_TOKEN) {
217                $filters[] = $_filter;
218            } else if ($_filter[0] == PDF_TYPE_ARRAY) {
219                $filters = $_filter[1];
220            }
221        }
222
223        $stream = $obj[2][1];
224
225        foreach ($filters AS $_filter) {
226            switch ($_filter[1]) {
227                case '/FlateDecode':
228                    if (function_exists('gzuncompress')) {
229                        $stream = (strlen($stream) > 0) ? @gzuncompress($stream) : '';
230                    } else {
231                        $this->error(sprintf('To handle %s filter, please compile php with zlib support.',$_filter[1]));
232                    }
233                    if ($stream === false) {
234                        $this->error('Error while decompressing stream.');
235                    }
236                break;
237                case '/LZWDecode':
238                    include_once('filters/FilterLZW_FPDI.php');
239                    $decoder = new FilterLZW_FPDI($this->fpdi);
240                    $stream = $decoder->decode($stream);
241                    break;
242                case '/ASCII85Decode':
243                    include_once('filters/FilterASCII85_FPDI.php');
244                    $decoder = new FilterASCII85_FPDI($this->fpdi);
245                    $stream = $decoder->decode($stream);
246                    break;
247                case null:
248                    $stream = $stream;
249                break;
250                default:
251                    $this->error(sprintf('Unsupported Filter: %s',$_filter[1]));
252            }
253        }
254
255        return $stream;
256    }
257
258
259    /**
260     * Get a Box from a page
261     * Arrayformat is same as used by fpdf_tpl
262     *
263     * @param array $page a /Page
264     * @param string $box_index Type of Box @see $availableBoxes
265     * @return array
266     */
267    function getPageBox($page, $box_index) {
268        $page = $this->pdf_resolve_object($this->c,$page);
269        $box = null;
270        if (isset($page[1][1][$box_index]))
271            $box =& $page[1][1][$box_index];
272
273        if (!is_null($box) && $box[0] == PDF_TYPE_OBJREF) {
274            $tmp_box = $this->pdf_resolve_object($this->c,$box);
275            $box = $tmp_box[1];
276        }
277
278        if (!is_null($box) && $box[0] == PDF_TYPE_ARRAY) {
279            $b =& $box[1];
280            return array('x' => $b[0][1]/$this->fpdi->k,
281                         'y' => $b[1][1]/$this->fpdi->k,
282                         'w' => abs($b[0][1]-$b[2][1])/$this->fpdi->k,
283                         'h' => abs($b[1][1]-$b[3][1])/$this->fpdi->k,
284                         'llx' => min($b[0][1], $b[2][1])/$this->fpdi->k,
285                         'lly' => min($b[1][1], $b[3][1])/$this->fpdi->k,
286                         'urx' => max($b[0][1], $b[2][1])/$this->fpdi->k,
287                         'ury' => max($b[1][1], $b[3][1])/$this->fpdi->k,
288                         );
289        } else if (!isset ($page[1][1]['/Parent'])) {
290            return false;
291        } else {
292            return $this->getPageBox($this->pdf_resolve_object($this->c, $page[1][1]['/Parent']), $box_index);
293        }
294    }
295
296    function getPageBoxes($pageno) {
297        return $this->_getPageBoxes($this->pages[$pageno-1]);
298    }
299
300    /**
301     * Get all Boxes from /Page
302     *
303     * @param array a /Page
304     * @return array
305     */
306    function _getPageBoxes($page) {
307        $boxes = array();
308
309        foreach($this->availableBoxes AS $box) {
310            if ($_box = $this->getPageBox($page,$box)) {
311                $boxes[$box] = $_box;
312            }
313        }
314
315        return $boxes;
316    }
317
318    /**
319     * Get the page rotation by pageno
320     *
321     * @param integer $pageno
322     * @return array
323     */
324    function getPageRotation($pageno) {
325        return $this->_getPageRotation($this->pages[$pageno-1]);
326    }
327
328    function _getPageRotation ($obj) { // $obj = /Page
329        $obj = $this->pdf_resolve_object($this->c, $obj);
330        if (isset ($obj[1][1]['/Rotate'])) {
331                $res = $this->pdf_resolve_object($this->c, $obj[1][1]['/Rotate']);
332                if ($res[0] == PDF_TYPE_OBJECT)
333                return $res[1];
334            return $res;
335        } else {
336                if (!isset ($obj[1][1]['/Parent'])) {
337                        return false;
338                } else {
339                $res = $this->_getPageRotation($obj[1][1]['/Parent']);
340                if ($res[0] == PDF_TYPE_OBJECT)
341                    return $res[1];
342                return $res;
343                }
344        }
345    }
346
347    /**
348     * Read all /Page(es)
349     *
350     * @param object pdf_context
351     * @param array /Pages
352     * @param array the result-array
353     */
354    function read_pages (&$c, &$pages, &$result) {
355        // Get the kids dictionary
356        $kids = $this->pdf_resolve_object ($c, $pages[1][1]['/Kids']);
357
358        if (!is_array($kids))
359            $this->error('Cannot find /Kids in current /Page-Dictionary');
360        foreach ($kids[1] as $v) {
361                $pg = $this->pdf_resolve_object ($c, $v);
362            if ($pg[1][1]['/Type'][1] === '/Pages') {
363                // If one of the kids is an embedded
364                        // /Pages array, resolve it as well.
365                $this->read_pages ($c, $pg, $result);
366                } else {
367                        $result[] = $pg;
368                }
369        }
370    }
371
372
373
374    /**
375     * Get PDF-Version
376     *
377     * And reset the PDF Version used in FPDI if needed
378     */
379    function getPDFVersion() {
380        parent::getPDFVersion();
381        $this->fpdi->PDFVersion = max($this->fpdi->PDFVersion, $this->pdfVersion);
382    }
383
384}
Note: See TracBrowser for help on using the browser.
phpBMS vulnerability assesment provided by Orvant Inc. Copyright © 2010 Kreotek, LLC. All Rights reserved.