pdf_parser::closeFile()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 6
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 21 and the first side effect is on line 20.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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
if (!defined ('PDF_TYPE_NULL'))
21
    define ('PDF_TYPE_NULL', 0);
22
if (!defined ('PDF_TYPE_NUMERIC'))
23
    define ('PDF_TYPE_NUMERIC', 1);
24
if (!defined ('PDF_TYPE_TOKEN'))
25
    define ('PDF_TYPE_TOKEN', 2);
26
if (!defined ('PDF_TYPE_HEX'))
27
    define ('PDF_TYPE_HEX', 3);
28
if (!defined ('PDF_TYPE_STRING'))
29
    define ('PDF_TYPE_STRING', 4);
30
if (!defined ('PDF_TYPE_DICTIONARY'))
31
    define ('PDF_TYPE_DICTIONARY', 5);
32
if (!defined ('PDF_TYPE_ARRAY'))
33
    define ('PDF_TYPE_ARRAY', 6);
34
if (!defined ('PDF_TYPE_OBJDEC'))
35
    define ('PDF_TYPE_OBJDEC', 7);
36
if (!defined ('PDF_TYPE_OBJREF'))
37
    define ('PDF_TYPE_OBJREF', 8);
38
if (!defined ('PDF_TYPE_OBJECT'))
39
    define ('PDF_TYPE_OBJECT', 9);
40
if (!defined ('PDF_TYPE_STREAM'))
41
    define ('PDF_TYPE_STREAM', 10);
42
if (!defined ('PDF_TYPE_BOOLEAN'))
43
    define ('PDF_TYPE_BOOLEAN', 11);
44
if (!defined ('PDF_TYPE_REAL'))
45
    define ('PDF_TYPE_REAL', 12);
46
    
47
require_once("pdf_context.php");
48
require_once("wrapper_functions.php");
49
50
class pdf_parser {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
51
	
52
	/**
53
     * Filename
54
     * @var string
55
     */
56
    var $filename;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $filename.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
57
    
58
    /**
59
     * File resource
60
     * @var resource
61
     */
62
    var $f;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $f.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
63
    
64
    /**
65
     * PDF Context
66
     * @var object pdf_context-Instance
67
     */
68
    var $c;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $c.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
69
    
70
    /**
71
     * xref-Data
72
     * @var array
73
     */
74
    var $xref;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $xref.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
75
76
    /**
77
     * root-Object
78
     * @var array
79
     */
80
    var $root;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $root.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
81
	
82
    /**
83
     * PDF version of the loaded document
84
     * @var string
85
     */
86
    var $pdfVersion;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $pdfVersion.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
87
    
88
    /**
89
     * Constructor
90
     *
91
     * @param string $filename  Source-Filename
92
     */
93
	function pdf_parser($filename) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Coding Style Best Practice introduced by
Please use __construct() instead of a PHP4-style constructor that is named after the class.
Loading history...
94
        $this->filename = $filename;
95
        
96
        $this->f = @fopen($this->filename, "rb");
97
98
        if (!$this->f)
99
            $this->error(sprintf("Cannot open %s !", $filename));
100
101
        $this->getPDFVersion();
102
103
        $this->c =& new pdf_context($this->f);
104
        // Read xref-Data
105
        $this->pdf_read_xref($this->xref, $this->pdf_find_xref());
106
107
        // Check for Encryption
108
        $this->getEncryption();
109
110
        // Read root
111
        $this->pdf_read_root();
112
    }
113
    
114
    /**
115
     * Close the opened file
116
     */
117
    function closeFile() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
118
    	if (isset($this->f)) {
119
    	    fclose($this->f);	
120
    		unset($this->f);
121
    	}	
122
    }
123
    
124
    /**
125
     * Print Error and die
126
     *
127
     * @param string $msg  Error-Message
128
     */
129
    function error($msg) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
130
    	die("<b>PDF-Parser Error:</b> ".$msg);	
0 ignored issues
show
Coding Style Compatibility introduced by
The method error() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
131
    }
132
    
133
    /**
134
     * Check Trailer for Encryption
135
     */
136
    function getEncryption() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
137
        if (isset($this->xref['trailer'][1]['/Encrypt'])) {
138
            $this->error("File is encrypted!");
139
        }
140
    }
141
    
142
	/**
143
     * Find/Return /Root
144
     *
145
     * @return array
146
     */
147
    function pdf_find_root() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
148
        if ($this->xref['trailer'][1]['/Root'][0] != PDF_TYPE_OBJREF) {
149
            $this->error("Wrong Type of Root-Element! Must be an indirect reference");
150
        }
151
        return $this->xref['trailer'][1]['/Root'];
152
    }
153
154
    /**
155
     * Read the /Root
156
     */
157
    function pdf_read_root() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
158
        // read root
159
        $this->root = $this->pdf_resolve_object($this->c, $this->pdf_find_root());
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->pdf_resolve_objec...$this->pdf_find_root()) can also be of type false or null. However, the property $root is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
160
    }
161
    
162
    /**
163
     * Get PDF-Version
164
     *
165
     * And reset the PDF Version used in FPDI if needed
166
     */
167
    function getPDFVersion() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
168
        fseek($this->f, 0);
169
        preg_match("/\d\.\d/",fread($this->f,16),$m);
170
        if (isset($m[0]))
171
            $this->pdfVersion = $m[0];
172
        return $this->pdfVersion;
173
    }
174
    
175
    /**
176
     * Find the xref-Table
177
     */
178
    function pdf_find_xref() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
179
       	$toRead = 1500;
180
                
181
        $stat = fseek ($this->f, -$toRead, SEEK_END);
182
        if ($stat === -1) {
183
            fseek ($this->f, 0);
184
        }
185
       	$data = fread($this->f, $toRead);
186
        
187
        $pos = strlen($data) - strpos(strrev($data), strrev('startxref')); 
188
        $data = substr($data, $pos);
189
        
190
        if (!preg_match('/\s*(\d+).*$/s', $data, $matches)) {
191
            $this->error("Unable to find pointer to xref table");
192
    	}
193
194
    	return (int) $matches[1];
195
    }
196
197
    /**
198
     * Read xref-table
199
     *
200
     * @param array $result Array of xref-table
201
     * @param integer $offset of xref-table
202
     */
203
    function pdf_read_xref(&$result, $offset) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
204
        fseek($this->f, $o_pos = $offset-20); // set some bytes backwards to fetch errorious docs
205
            
206
        $data = fread($this->f, 100);
207
        
208
        $xrefPos = strpos($data, 'xref');
209
        
210
        if ($xrefPos === false) {
211
            $this->error('Unable to find xref table.');
212
        }
213
        
214
        if (!isset($result['xref_location'])) {
215
            $result['xref_location'] = $o_pos+$xrefPos;
216
            $result['max_object'] = 0;
217
    	}
218
219
    	$cylces = -1;
220
        $bytesPerCycle = 100;
221
        
222
    	fseek($this->f, $o_pos = $o_pos+$xrefPos+4); // set the handle directly after the "xref"-keyword
223
        $data = fread($this->f, $bytesPerCycle);
224
        
225
        while (($trailerPos = strpos($data, 'trailer', max($bytesPerCycle*$cylces++, 0))) === false && !feof($this->f)) {
226
            $data .= fread($this->f, $bytesPerCycle);
227
        }
228
        
229
        if ($trailerPos === false) {
230
            $this->error('Trailer keyword not found after xref table');
231
        }
232
        
233
        $data = substr($data, 0, $trailerPos);
234
        
235
        // get Line-Ending
236
        preg_match_all("/(\r\n|\n|\r)/", substr($data, 0, 100), $m); // check the first 100 bytes for linebreaks
237
238
        $differentLineEndings = count(array_unique($m[0]));
239
        if ($differentLineEndings > 1) {
240
            $lines = preg_split("/(\r\n|\n|\r)/", $data, -1, PREG_SPLIT_NO_EMPTY);
241
        } else {
242
            $lines = explode($m[0][1], $data);
243
        }
244
        
245
        $data = $differentLineEndings = $m = null;
246
        unset($data, $differentLineEndings, $m);
247
        
248
        $linesCount = count($lines);
249
        
250
        $start = 1;
251
        
252
        for ($i = 0; $i < $linesCount; $i++) {
253
            $line = trim($lines[$i]);
254
            if ($line) {
255
                $pieces = explode(" ", $line);
256
                $c = count($pieces);
257
                switch($c) {
258
                    case 2:
259
                        $start = (int)$pieces[0];
260
                        $end   = $start+(int)$pieces[1];
261
                        if ($end > $result['max_object'])
262
                            $result['max_object'] = $end;
263
                        break;
264
                    case 3:
265
                        if (!isset($result['xref'][$start]))
266
                            $result['xref'][$start] = array();
267
                        
268
                        if (!array_key_exists($gen = (int) $pieces[1], $result['xref'][$start])) {
269
                	        $result['xref'][$start][$gen] = $pieces[2] == 'n' ? (int) $pieces[0] : null;
270
                	    }
271
                        $start++;
272
                        break;
273
                    default:
274
                        $this->error('Unexpected data in xref table');
275
                }
276
            }
277
        }
278
        
279
        $lines = $pieces = $line = $start = $end = $gen = null;
280
        unset($lines, $pieces, $line, $start, $end, $gen);
281
        
282
        fseek($this->f, $o_pos+$trailerPos+7);
283
        
284
        $c =&  new pdf_context($this->f);
285
	    $trailer = $this->pdf_read_value($c);
286
	    
287
	    $c = null;
288
	    unset($c);
289
	    
290
	    if (!isset($result['trailer'])) {
291
            $result['trailer'] = $trailer;          
292
	    }
293
	    
294
	    if (isset($trailer[1]['/Prev'])) {
295
	    	$this->pdf_read_xref($result, $trailer[1]['/Prev'][1]);
296
	    } 
297
	    
298
	    $trailer = null;
299
	    unset($trailer);
300
        
301
        return true;
302
    }
303
    
304
    /**
305
     * Reads an Value
306
     *
307
     * @param object $c pdf_context
308
     * @param string $token a Token
309
     * @return mixed
310
     */
311
    function pdf_read_value(&$c, $token = null) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
312
    	if (is_null($token)) {
313
    	    $token = $this->pdf_read_token($c);
314
    	}
315
    	
316
        if ($token === false) {
317
    	    return false;
318
    	}
319
320
       	switch ($token) {
321
            case	'<':
0 ignored issues
show
Coding Style introduced by
As per coding-style, case should be followed by a single space.

As per the PSR-2 coding standard, there must be a space after the case keyword, instead of the test immediately following it.

switch (true) {
    case!isset($a):  //wrong
        doSomething();
        break;
    case !isset($b):  //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
322
    			// This is a hex string.
323
    			// Read the value, then the terminator
324
325
                $pos = $c->offset;
326
327
    			while(1) {
328
329
                    $match = strpos ($c->buffer, '>', $pos);
330
				
331
    				// If you can't find it, try
332
    				// reading more data from the stream
333
334
    				if ($match === false) {
335
    					if (!$c->increase_length()) {
336
    						return false;
337
    					} else {
338
                        	continue;
339
                    	}
340
    				}
341
342
    				$result = substr ($c->buffer, $c->offset, $match - $c->offset);
343
    				$c->offset = $match + 1;
344
    				
345
    				return array (PDF_TYPE_HEX, $result);
346
                }
347
                
348
                break;
349 View Code Duplication
    		case	'<<':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style introduced by
As per coding-style, case should be followed by a single space.

As per the PSR-2 coding standard, there must be a space after the case keyword, instead of the test immediately following it.

switch (true) {
    case!isset($a):  //wrong
        doSomething();
        break;
    case !isset($b):  //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
350
    			// This is a dictionary.
351
352
    			$result = array();
353
354
    			// Recurse into this function until we reach
355
    			// the end of the dictionary.
356
    			while (($key = $this->pdf_read_token($c)) !== '>>') {
357
    				if ($key === false) {
358
    					return false;
359
    				}
360
					
361
    				if (($value =   $this->pdf_read_value($c)) === false) {
362
    					return false;
363
    				}
364
                    $result[$key] = $value;
365
    			}
366
				
367
    			return array (PDF_TYPE_DICTIONARY, $result);
368
369 View Code Duplication
    		case	'[':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style introduced by
As per coding-style, case should be followed by a single space.

As per the PSR-2 coding standard, there must be a space after the case keyword, instead of the test immediately following it.

switch (true) {
    case!isset($a):  //wrong
        doSomething();
        break;
    case !isset($b):  //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
370
    			// This is an array.
371
372
    			$result = array();
373
374
    			// Recurse into this function until we reach
375
    			// the end of the array.
376
    			while (($token = $this->pdf_read_token($c)) !== ']') {
377
                    if ($token === false) {
378
    					return false;
379
    				}
380
					
381
    				if (($value = $this->pdf_read_value($c, $token)) === false) {
382
                        return false;
383
    				}
384
					
385
    				$result[] = $value;
386
    			}
387
    			
388
                return array (PDF_TYPE_ARRAY, $result);
389
390
    		case	'('		:
0 ignored issues
show
Coding Style introduced by
As per coding-style, case should be followed by a single space.

As per the PSR-2 coding standard, there must be a space after the case keyword, instead of the test immediately following it.

switch (true) {
    case!isset($a):  //wrong
        doSomething();
        break;
    case !isset($b):  //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
391
                // This is a string
392
                $pos = $c->offset;
393
                
394
                $openBrackets = 1;
395
    			do {
396
                    for (; $openBrackets != 0 && $pos < $c->length; $pos++) {
397
                        switch (ord($c->buffer[$pos])) {
398
                            case 0x28: // '('
399
                                $openBrackets++;
400
                                break;
401
                            case 0x29: // ')'
402
                                $openBrackets--;
403
                                break;
404
                            case 0x5C: // backslash
405
                                $pos++;
406
                        }
407
                    }
408
    			} while($openBrackets != 0 && $c->increase_length());
409
    			
410
    			$result = substr($c->buffer, $c->offset, $pos - $c->offset - 1);
411
    			$c->offset = $pos;
412
    			
413
    			return array (PDF_TYPE_STRING, $result);
414
415
    			
416
            case "stream":
417
            	$o_pos = ftell($c->file)-strlen($c->buffer);
418
		        $o_offset = $c->offset;
419
		        
420
		        $c->reset($startpos = $o_pos + $o_offset);
421
		        
422
		        $e = 0; // ensure line breaks in front of the stream
423 View Code Duplication
		        if ($c->buffer[0] == chr(10) || $c->buffer[0] == chr(13))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
424
		        	$e++;
425 View Code Duplication
		        if ($c->buffer[1] == chr(10) && $c->buffer[0] != chr(10))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
426
		        	$e++;
427
		        
428
		        if ($this->actual_obj[1][1]['/Length'][0] == PDF_TYPE_OBJREF) {
429
		        	$tmp_c =& new pdf_context($this->f);
430
		        	$tmp_length = $this->pdf_resolve_object($tmp_c,$this->actual_obj[1][1]['/Length']);
0 ignored issues
show
Bug introduced by
The property actual_obj does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
431
		        	$length = $tmp_length[1][1];
432
		        } else {
433
		        	$length = $this->actual_obj[1][1]['/Length'][1];	
434
		        }
435
		        
436
		        if ($length > 0) {
437
    		        $c->reset($startpos+$e,$length);
438
    		        $v = $c->buffer;
439
		        } else {
440
		            $v = '';   
441
		        }
442
		        $c->reset($startpos+$e+$length+9); // 9 = strlen("endstream")
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
443
		        
444
		        return array(PDF_TYPE_STREAM, $v);
445
		        
446
	        case '%':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
447
	            // this is a comment - just jump over it
448
                $pos = $c->offset;
449
    			while(1) {
450
    			    // PHP 4.3.3 required
451
                    #$match = preg_match("/(\r\n|\r|\n)/", $c->buffer, $m, PREG_OFFSET_CAPTURE, $pos);
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
452
                    // alternative
453
                    $match = preg_match("/(\r\n|\r|\n)/", substr($c->buffer, $pos), $m);
454
					if ($match === false) {
455
    					if (!$c->increase_length()) {
456
    						return false;
457
    					} else {
458
                        	continue;
459
                    	}
460
    				}
461
462
    				// PHP 4.3.3 required
463
                    #$c->offset = $m[0][1]+strlen($m[0][0]);
0 ignored issues
show
Unused Code Comprehensibility introduced by
76% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
464
    				// alternative
465
                    $c->offset = strpos($c->buffer, $m[0], $pos)+strlen($m[0]);
466
    				
467
    				return $this->pdf_read_value($c);
468
                }
469
                
470
    		default	:
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a DEFAULT statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in the default statement.

switch ($expr) {
    default : //wrong
        doSomething();
        break;
}

switch ($expr) {
    default: //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
471
            	if (is_numeric ($token)) {
472
                    // A numeric token. Make sure that
473
    				// it is not part of something else.
474
    				if (($tok2 = $this->pdf_read_token ($c)) !== false) {
475
                        if (is_numeric ($tok2)) {
476
477
    						// Two numeric tokens in a row.
478
    						// In this case, we're probably in
479
    						// front of either an object reference
480
    						// or an object specification.
481
    						// Determine the case and return the data
482
    						if (($tok3 = $this->pdf_read_token ($c)) !== false) {
483
                                switch ($tok3) {
484
    								case	'obj'	:
0 ignored issues
show
Coding Style introduced by
As per coding-style, case should be followed by a single space.

As per the PSR-2 coding standard, there must be a space after the case keyword, instead of the test immediately following it.

switch (true) {
    case!isset($a):  //wrong
        doSomething();
        break;
    case !isset($b):  //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
485
                                        return array (PDF_TYPE_OBJDEC, (int) $token, (int) $tok2);
486
    								case	'R'		:
0 ignored issues
show
Coding Style introduced by
As per coding-style, case should be followed by a single space.

As per the PSR-2 coding standard, there must be a space after the case keyword, instead of the test immediately following it.

switch (true) {
    case!isset($a):  //wrong
        doSomething();
        break;
    case !isset($b):  //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
487
    									return array (PDF_TYPE_OBJREF, (int) $token, (int) $tok2);
488
    							}
489
    							// If we get to this point, that numeric value up
490
    							// there was just a numeric value. Push the extra
491
    							// tokens back into the stack and return the value.
492
    							array_push ($c->stack, $tok3);
493
    						}
494
    					}
495
496
    					array_push ($c->stack, $tok2);
497
    				}
498
499
    				if ($token === (string)((int)$token))
500
        				return array (PDF_TYPE_NUMERIC, (int)$token);
501
    				else 
502
    					return array (PDF_TYPE_REAL, (float)$token);
503
    			} else if ($token == 'true' || $token == 'false') {
504
                    return array (PDF_TYPE_BOOLEAN, $token == 'true');
505
    			} else {
506
507
                    // Just a token. Return it.
508
    				return array (PDF_TYPE_TOKEN, $token);
509
    			}
510
511
         }
512
    }
513
    
514
    /**
515
     * Resolve an object
516
     *
517
     * @param object $c pdf_context
518
     * @param array $obj_spec The object-data
519
     * @param boolean $encapsulate Must set to true, cause the parsing and fpdi use this method only without this para
520
     */
521
    function pdf_resolve_object(&$c, $obj_spec, $encapsulate = true) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
522
        // Exit if we get invalid data
523
    	if (!is_array($obj_spec)) {
524
            return false;
525
    	}
526
527
    	if ($obj_spec[0] == PDF_TYPE_OBJREF) {
528
529
    		// This is a reference, resolve it
530
    		if (isset($this->xref['xref'][$obj_spec[1]][$obj_spec[2]])) {
531
532
    			// Save current file position
533
    			// This is needed if you want to resolve
534
    			// references while you're reading another object
535
    			// (e.g.: if you need to determine the length
536
    			// of a stream)
537
538
    			$old_pos = ftell($c->file);
539
540
    			// Reposition the file pointer and
541
    			// load the object header.
542
				
543
    			$c->reset($this->xref['xref'][$obj_spec[1]][$obj_spec[2]]);
544
545
    			$header = $this->pdf_read_value($c,null,true);
0 ignored issues
show
Unused Code introduced by
The call to pdf_parser::pdf_read_value() has too many arguments starting with true.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
546
547
    			if ($header[0] != PDF_TYPE_OBJDEC || $header[1] != $obj_spec[1] || $header[2] != $obj_spec[2]) {
548
    				$this->error("Unable to find object ({$obj_spec[1]}, {$obj_spec[2]}) at expected location");
549
    			}
550
551
    			// If we're being asked to store all the information
552
    			// about the object, we add the object ID and generation
553
    			// number for later use
554
				$this->actual_obj =& $result;
0 ignored issues
show
Bug introduced by
The variable $result seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
555
    			if ($encapsulate) {
556
    				$result = array (
557
    					PDF_TYPE_OBJECT,
558
    					'obj' => $obj_spec[1],
559
    					'gen' => $obj_spec[2]
560
    				);
561
    			} else {
562
    				$result = array();
563
    			}
564
565
    			// Now simply read the object data until
566
    			// we encounter an end-of-object marker
567
    			while(1) {
568
                    $value = $this->pdf_read_value($c);
569
					if ($value === false || count($result) > 4) {
570
						// in this case the parser coudn't find an endobj so we break here
571
						break;
572
    				}
573
574
    				if ($value[0] == PDF_TYPE_TOKEN && $value[1] === 'endobj') {
575
    					break;
576
    				}
577
578
                    $result[] = $value;
579
    			}
580
581
    			$c->reset($old_pos);
582
583
                if (isset($result[2][0]) && $result[2][0] == PDF_TYPE_STREAM) {
584
                    $result[0] = PDF_TYPE_STREAM;
585
                }
586
587
    			return $result;
588
    		}
589
    	} else {
590
    		return $obj_spec;
591
    	}
592
    }
593
594
    
595
    
596
    /**
597
     * Reads a token from the file
598
     *
599
     * @param object $c pdf_context
600
     * @return mixed
601
     */
602
    function pdf_read_token(&$c)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
603
    {
604
    	// If there is a token available
605
    	// on the stack, pop it out and
606
    	// return it.
607
608
    	if (count($c->stack)) {
609
    		return array_pop($c->stack);
610
    	}
611
612
    	// Strip away any whitespace
613
614
    	do {
615
    		if (!$c->ensure_content()) {
616
    			return false;
617
    		}
618
    		$c->offset += _strspn($c->buffer, " \n\r\t", $c->offset);
619
    	} while ($c->offset >= $c->length - 1);
620
621
    	// Get the first character in the stream
622
623
    	$char = $c->buffer[$c->offset++];
624
625
    	switch ($char) {
626
627
    		case '['	:
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
628
    		case ']'	:
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
629
    		case '('	:
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
630
    		case ')'	:
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
631
632
    			// This is either an array or literal string
633
    			// delimiter, Return it
634
635
    			return $char;
636
637
    		case '<'	:
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
638
    		case '>'	:
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
639
640
    			// This could either be a hex string or
641
    			// dictionary delimiter. Determine the
642
    			// appropriate case and return the token
643
644
    			if ($c->buffer[$c->offset] == $char) {
645
    				if (!$c->ensure_content()) {
646
    				    return false;
647
    				}
648
    				$c->offset++;
649
    				return $char . $char;
650
    			} else {
651
    				return $char;
652
    			}
653
654
    		default		:
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a DEFAULT statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in the default statement.

switch ($expr) {
    default : //wrong
        doSomething();
        break;
}

switch ($expr) {
    default: //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
655
656
    			// This is "another" type of token (probably
657
    			// a dictionary entry or a numeric value)
658
    			// Find the end and return it.
659
660
    			if (!$c->ensure_content()) {
661
    				return false;
662
    			}
663
664
    			while(1) {
665
666
    				// Determine the length of the token
667
668
    				$pos = _strcspn($c->buffer, " []<>()\r\n\t/", $c->offset);
669
    				if ($c->offset + $pos <= $c->length - 1) {
670
    					break;
671
    				} else {
672
    					// If the script reaches this point,
673
    					// the token may span beyond the end
674
    					// of the current buffer. Therefore,
675
    					// we increase the size of the buffer
676
    					// and try again--just to be safe.
677
678
    					$c->increase_length();
679
    				}
680
    			}
681
682
    			$result = substr($c->buffer, $c->offset - 1, $pos + 1);
0 ignored issues
show
Bug introduced by
The variable $pos does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
683
684
    			$c->offset += $pos;
685
    			return $result;
686
    	}
687
    }
688
689
	
690
}