Issues (1210)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

thumbs/phpthumb.gif.php (41 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
///////////////////////////////////////////////////////////////////////////////////////////////////
3
// GIF Util - (C) 2003 Yamasoft (S/C)
4
// http://www.yamasoft.com
5
// All Rights Reserved
6
// This file can be freely copied, distributed, modified, updated by anyone under the only
7
// condition to leave the original address (Yamasoft, http://www.yamasoft.com) and this header.
8
///////////////////////////////////////////////////////////////////////////////////////////////////
9
// <gif>  = gif_loadFile(filename, [index])
10
// <bool> = gif_getSize(<gif> or filename, &width, &height)
11
// <bool> = gif_outputAsPng(<gif>, filename, [bgColor])
12
// <bool> = gif_outputAsBmp(<gif>, filename, [bgcolor])
13
// <bool> = gif_outputAsJpeg(<gif>, filename, [bgcolor]) - use cjpeg if available otherwise uses GD
14
///////////////////////////////////////////////////////////////////////////////////////////////////
15
// Original code by Fabien Ezber
16
// Modified by James Heinrich <[email protected]> for use in phpThumb() - December 10, 2003
17
// * Added function gif_loadFileToGDimageResource() - this returns a GD image resource
18
// * Modified gif_outputAsJpeg() to check if it's running under Windows, or if cjpeg is not
19
//   available, in which case it will attempt to output JPEG using GD functions
20
// * added @ error-suppression to two lines where it checks: if ($this->m_img->m_bTrans)
21
//   otherwise warnings are generated if error_reporting == E_ALL
22
///////////////////////////////////////////////////////////////////////////////////////////////////
23
24
/**
25
 * @param            $lpszFileName
26
 * @param  int       $iIndex
27
 * @return bool|CGIF
28
 */
29
function gif_loadFile($lpszFileName, $iIndex = 0)
30
{
31
    $gif = new CGIF();
32
    if ($gif->loadFile($lpszFileName, $iIndex)) {
33
        return $gif;
34
    }
35
36
    return false;
37
}
38
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40
41
// Added by James Heinrich <[email protected]> - December 10, 2003
42
/**
43
 * @param                $gifFilename
44
 * @param  int           $bgColor
45
 * @return bool|resource
46
 */
47
function gif_loadFileToGDimageResource($gifFilename, $bgColor = -1)
48
{
49
    if ($gif = gif_loadFile($gifFilename)) {
50
        if (!phpthumb_functions::FunctionIsDisabled('set_time_limit')) {
51
            // shouldn't take nearly this long
52
            set_time_limit(120);
53
        }
54
        // general strategy: convert raw data to PNG then convert PNG data to GD image resource
55
        $PNGdata = $gif->getPng($bgColor);
56
        if ($img = @imagecreatefromstring($PNGdata)) {
57
58
            // excellent - PNG image data successfully converted to GD image
59
            return $img;
60
        } elseif ($img = $gif->getGD_PixelPlotterVersion()) {
61
62
            // problem: ImageCreateFromString() didn't like the PNG image data.
63
            //   This has been known to happen in PHP v4.0.6
64
            // solution: take the raw image data and create a new GD image and plot
65
            //   pixel-by-pixel on the GD image. This is extremely slow, but it does
66
            //   work and a slow solution is better than no solution, right? :)
67
            return $img;
68
        }
69
    }
70
71
    return false;
72
}
73
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75
76
/**
77
 * @param       $gif
78
 * @param       $lpszFileName
79
 * @param  int  $bgColor
80
 * @return bool
81
 */
82 View Code Duplication
function gif_outputAsBmp($gif, $lpszFileName, $bgColor = -1)
0 ignored issues
show
This function seems to be duplicated in 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...
83
{
84
    if (!isset($gif) || (@get_class($gif) <> 'cgif') || !$gif->loaded() || ($lpszFileName == '')) {
85
        return false;
86
    }
87
88
    $fd = $gif->getBmp($bgColor);
89
    if (strlen($fd) <= 0) {
90
        return false;
91
    }
92
93
    if (!($fh = @fopen($lpszFileName, 'wb'))) {
94
        return false;
95
    }
96
    @fwrite($fh, $fd, strlen($fd));
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
97
    @fflush($fh);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
98
    @fclose($fh);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
99
100
    return true;
101
}
102
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104
105
/**
106
 * @param       $gif
107
 * @param       $lpszFileName
108
 * @param  int  $bgColor
109
 * @return bool
110
 */
111 View Code Duplication
function gif_outputAsPng($gif, $lpszFileName, $bgColor = -1)
0 ignored issues
show
This function seems to be duplicated in 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...
112
{
113
    if (!isset($gif) || (@get_class($gif) <> 'cgif') || !$gif->loaded() || ($lpszFileName == '')) {
114
        return false;
115
    }
116
117
    $fd = $gif->getPng($bgColor);
118
    if (strlen($fd) <= 0) {
119
        return false;
120
    }
121
122
    if (!($fh = @fopen($lpszFileName, 'wb'))) {
123
        return false;
124
    }
125
    @fwrite($fh, $fd, strlen($fd));
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
126
    @fflush($fh);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
127
    @fclose($fh);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
128
129
    return true;
130
}
131
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133
134
/**
135
 * @param       $gif
136
 * @param       $lpszFileName
137
 * @param  int  $bgColor
138
 * @return bool
139
 */
140
function gif_outputAsJpeg($gif, $lpszFileName, $bgColor = -1)
141
{
142
    // JPEG output that does not require cjpeg added by James Heinrich <[email protected]> - December 10, 2003
143
    if ((strtoupper(substr(PHP_OS, 0, 3)) != 'WIN') && (file_exists('/usr/local/bin/cjpeg') || `which cjpeg`)) {
144
        if (gif_outputAsBmp($gif, $lpszFileName . '.bmp', $bgColor)) {
145
            exec('cjpeg ' . $lpszFileName . '.bmp >' . $lpszFileName . ' 2>/dev/null');
146
            @unlink($lpszFileName . '.bmp');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
147
148
            if (@file_exists($lpszFileName)) {
149
                if (@filesize($lpszFileName) > 0) {
150
                    return true;
151
                }
152
153
                @unlink($lpszFileName);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
154
            }
155
        }
156
    } else {
157
158
        // either Windows, or cjpeg not found in path
159
        if ($img = @imagecreatefromstring($gif->getPng($bgColor))) {
160
            if (@imagejpeg($img, $lpszFileName)) {
161
                return true;
162
            }
163
        }
164
    }
165
166
    return false;
167
}
168
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170
171
/**
172
 * @param $gif
173
 * @param $width
174
 * @param $height
175
 * @return bool
176
 */
177
function gif_getSize($gif, &$width, &$height)
178
{
179
    if (isset($gif) && (@get_class($gif) == 'cgif') && $gif->loaded()) {
180
        $width  = $gif->width();
181
        $height = $gif->height();
182
    } elseif (@file_exists($gif)) {
183
        $myGIF = new CGIF();
184
        if (!$myGIF->getSize($gif, $width, $height)) {
185
            return false;
186
        }
187
    } else {
188
        return false;
189
    }
190
191
    return true;
192
}
193
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195
196
/**
197
 * Class CGIFLZW
198
 */
199
class CGIFLZW
200
{
201
    public $MAX_LZW_BITS;
202
    public $Fresh;
203
    public $CodeSize;
204
    public $SetCodeSize;
205
    public $MaxCode;
206
    public $MaxCodeSize;
207
    public $FirstCode;
208
    public $OldCode;
209
    public $ClearCode;
210
    public $EndCode;
211
    public $Next;
212
    public $Vals;
213
    public $Stack;
214
    public $sp;
215
    public $Buf;
216
    public $CurBit;
217
    public $LastBit;
218
    public $Done;
219
    public $LastByte;
220
221
    ///////////////////////////////////////////////////////////////////////////
222
223
    // CONSTRUCTOR
224
    /**
225
     * CGIFLZW constructor.
226
     */
227
    public function __construct()
228
    {
229
        $this->MAX_LZW_BITS = 12;
230
        unset($this->Next);
231
        unset($this->Vals);
232
        unset($this->Stack);
233
        unset($this->Buf);
234
235
        $this->Next  = range(0, (1 << $this->MAX_LZW_BITS) - 1);
236
        $this->Vals  = range(0, (1 << $this->MAX_LZW_BITS) - 1);
237
        $this->Stack = range(0, (1 << ($this->MAX_LZW_BITS + 1)) - 1);
238
        $this->Buf   = range(0, 279);
239
    }
240
241
    ///////////////////////////////////////////////////////////////////////////
242
243
    /**
244
     * @param $data
245
     * @param $datLen
246
     * @return bool|string
247
     */
248
    public function deCompress($data, &$datLen)
249
    {
250
        $stLen  = strlen($data);
251
        $datLen = 0;
252
        $ret    = '';
253
254
        // INITIALIZATION
255
        $this->LZWCommand($data, true);
256
257
        while (($iIndex = $this->LZWCommand($data, false)) >= 0) {
258
            $ret .= chr($iIndex);
259
        }
260
261
        $datLen = $stLen - strlen($data);
262
263
        if ($iIndex != -2) {
264
            return false;
265
        }
266
267
        return $ret;
268
    }
269
270
    ///////////////////////////////////////////////////////////////////////////
271
272
    /**
273
     * @param $data
274
     * @param $bInit
275
     * @return int
276
     */
277
    public function LZWCommand(&$data, $bInit)
278
    {
279
        if ($bInit) {
280
            $this->SetCodeSize = ord($data{0});
281
            $data              = substr($data, 1);
282
283
            $this->CodeSize    = $this->SetCodeSize + 1;
284
            $this->ClearCode   = 1 << $this->SetCodeSize;
285
            $this->EndCode     = $this->ClearCode + 1;
286
            $this->MaxCode     = $this->ClearCode + 2;
287
            $this->MaxCodeSize = $this->ClearCode << 1;
288
289
            $this->GetCode($data, $bInit);
290
291
            $this->Fresh = 1;
292 View Code Duplication
            for ($i = 0; $i < $this->ClearCode; $i++) {
0 ignored issues
show
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...
293
                $this->Next[$i] = 0;
294
                $this->Vals[$i] = $i;
295
            }
296
297 View Code Duplication
            for (; $i < (1 << $this->MAX_LZW_BITS); $i++) {
0 ignored issues
show
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...
298
                $this->Next[$i] = 0;
299
                $this->Vals[$i] = 0;
300
            }
301
302
            $this->sp = 0;
303
304
            return 1;
305
        }
306
307
        if ($this->Fresh) {
308
            $this->Fresh = 0;
309
            do {
310
                $this->FirstCode = $this->GetCode($data, $bInit);
311
                $this->OldCode   = $this->FirstCode;
312
            } while ($this->FirstCode == $this->ClearCode);
313
314
            return $this->FirstCode;
315
        }
316
317
        if ($this->sp > 0) {
318
            $this->sp--;
319
320
            return $this->Stack[$this->sp];
321
        }
322
323
        while (($Code = $this->GetCode($data, $bInit)) >= 0) {
324
            if ($Code == $this->ClearCode) {
325 View Code Duplication
                for ($i = 0; $i < $this->ClearCode; $i++) {
0 ignored issues
show
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...
326
                    $this->Next[$i] = 0;
327
                    $this->Vals[$i] = $i;
328
                }
329
330 View Code Duplication
                for (; $i < (1 << $this->MAX_LZW_BITS); $i++) {
0 ignored issues
show
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...
331
                    $this->Next[$i] = 0;
332
                    $this->Vals[$i] = 0;
333
                }
334
335
                $this->CodeSize    = $this->SetCodeSize + 1;
336
                $this->MaxCodeSize = $this->ClearCode << 1;
337
                $this->MaxCode     = $this->ClearCode + 2;
338
                $this->sp          = 0;
339
                $this->FirstCode   = $this->GetCode($data, $bInit);
340
                $this->OldCode     = $this->FirstCode;
341
342
                return $this->FirstCode;
343
            }
344
345
            if ($Code == $this->EndCode) {
346
                return -2;
347
            }
348
349
            $InCode = $Code;
350
            if ($Code >= $this->MaxCode) {
351
                $this->Stack[$this->sp] = $this->FirstCode;
352
                $this->sp++;
353
                $Code = $this->OldCode;
354
            }
355
356
            while ($Code >= $this->ClearCode) {
357
                $this->Stack[$this->sp] = $this->Vals[$Code];
358
                $this->sp++;
359
360
                if ($Code == $this->Next[$Code]) {
361
                    // Circular table entry, big GIF Error!
362
363
                    return -1;
364
                }
365
366
                $Code = $this->Next[$Code];
367
            }
368
369
            $this->FirstCode        = $this->Vals[$Code];
370
            $this->Stack[$this->sp] = $this->FirstCode;
371
            $this->sp++;
372
373
            if (($Code = $this->MaxCode) < (1 << $this->MAX_LZW_BITS)) {
374
                $this->Next[$Code] = $this->OldCode;
375
                $this->Vals[$Code] = $this->FirstCode;
376
                $this->MaxCode++;
377
378
                if (($this->MaxCode >= $this->MaxCodeSize) && ($this->MaxCodeSize < (1 << $this->MAX_LZW_BITS))) {
379
                    $this->MaxCodeSize *= 2;
380
                    $this->CodeSize++;
381
                }
382
            }
383
384
            $this->OldCode = $InCode;
385
            if ($this->sp > 0) {
386
                $this->sp--;
387
388
                return $this->Stack[$this->sp];
389
            }
390
        }
391
392
        return $Code;
393
    }
394
395
    ///////////////////////////////////////////////////////////////////////////
396
397
    /**
398
     * @param $data
399
     * @param $bInit
400
     * @return int
401
     */
402
    public function GetCode(&$data, $bInit)
403
    {
404
        if ($bInit) {
405
            $this->CurBit   = 0;
406
            $this->LastBit  = 0;
407
            $this->Done     = 0;
408
            $this->LastByte = 2;
409
410
            return 1;
411
        }
412
413
        if (($this->CurBit + $this->CodeSize) >= $this->LastBit) {
414
            if ($this->Done) {
415
                if ($this->CurBit >= $this->LastBit) {
416
                    // Ran off the end of my bits
417
                    return 0;
418
                }
419
420
                return -1;
421
            }
422
423
            $this->Buf[0] = $this->Buf[$this->LastByte - 2];
424
            $this->Buf[1] = $this->Buf[$this->LastByte - 1];
425
426
            $Count = ord($data{0});
427
            $data  = substr($data, 1);
428
429
            if ($Count) {
430
                for ($i = 0; $i < $Count; $i++) {
431
                    $this->Buf[2 + $i] = ord($data{$i});
432
                }
433
                $data = substr($data, $Count);
434
            } else {
435
                $this->Done = 1;
436
            }
437
438
            $this->LastByte = 2 + $Count;
439
            $this->CurBit   = ($this->CurBit - $this->LastBit) + 16;
440
            $this->LastBit  = (2 + $Count) << 3;
441
        }
442
443
        $iRet = 0;
444
        for ($i = $this->CurBit, $j = 0; $j < $this->CodeSize; $i++, $j++) {
445
            $iRet |= (($this->Buf[(int)($i / 8)] & (1 << ($i % 8))) != 0) << $j;
446
        }
447
448
        $this->CurBit += $this->CodeSize;
449
450
        return $iRet;
451
    }
452
}
453
454
///////////////////////////////////////////////////////////////////////////////////////////////////
455
456
/**
457
 * Class CGIFCOLORTABLE
458
 */
459
class CGIFCOLORTABLE
460
{
461
    public $m_nColors;
462
    public $m_arColors;
463
464
    ///////////////////////////////////////////////////////////////////////////
465
466
    // CONSTRUCTOR
467
    /**
468
     * CGIFCOLORTABLE constructor.
469
     */
470
    public function __construct()
471
    {
472
        unset($this->m_nColors);
473
        unset($this->m_arColors);
474
    }
475
476
    ///////////////////////////////////////////////////////////////////////////
477
478
    /**
479
     * @param $lpData
480
     * @param $num
481
     * @return bool
482
     */
483
    public function load($lpData, $num)
484
    {
485
        $this->m_nColors  = 0;
486
        $this->m_arColors = array();
487
488
        for ($i = 0; $i < $num; $i++) {
489
            $rgb = substr($lpData, $i * 3, 3);
490
            if (strlen($rgb) < 3) {
491
                return false;
492
            }
493
494
            $this->m_arColors[] = (ord($rgb{2}) << 16) + (ord($rgb{1}) << 8) + ord($rgb{0});
495
            $this->m_nColors++;
496
        }
497
498
        return true;
499
    }
500
501
    ///////////////////////////////////////////////////////////////////////////
502
503
    /**
504
     * @return string
505
     */
506 View Code Duplication
    public function toString()
0 ignored issues
show
This method seems to be duplicated in 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...
507
    {
508
        $ret = '';
509
510
        for ($i = 0; $i < $this->m_nColors; $i++) {
511
            $ret .= chr($this->m_arColors[$i] & 0x000000FF) . // R
512
                    chr(($this->m_arColors[$i] & 0x0000FF00) >> 8) . // G
513
                    chr(($this->m_arColors[$i] & 0x00FF0000) >> 16);  // B
514
        }
515
516
        return $ret;
517
    }
518
519
    ///////////////////////////////////////////////////////////////////////////
520
521
    /**
522
     * @return string
523
     */
524 View Code Duplication
    public function toRGBQuad()
0 ignored issues
show
This method seems to be duplicated in 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...
525
    {
526
        $ret = '';
527
528
        for ($i = 0; $i < $this->m_nColors; $i++) {
529
            $ret .= chr(($this->m_arColors[$i] & 0x00FF0000) >> 16) . // B
530
                    chr(($this->m_arColors[$i] & 0x0000FF00) >> 8) . // G
531
                    chr($this->m_arColors[$i] & 0x000000FF) . // R
532
                    "\x00";
533
        }
534
535
        return $ret;
536
    }
537
538
    ///////////////////////////////////////////////////////////////////////////
539
540
    /**
541
     * @param $rgb
542
     * @return int
543
     */
544
    public function colorIndex($rgb)
545
    {
546
        $rgb = (int)$rgb & 0xFFFFFF;
547
        $r1  = ($rgb & 0x0000FF);
548
        $g1  = ($rgb & 0x00FF00) >> 8;
549
        $b1  = ($rgb & 0xFF0000) >> 16;
550
        $idx = -1;
551
552
        for ($i = 0; $i < $this->m_nColors; $i++) {
553
            $r2 = ($this->m_arColors[$i] & 0x000000FF);
554
            $g2 = ($this->m_arColors[$i] & 0x0000FF00) >> 8;
555
            $b2 = ($this->m_arColors[$i] & 0x00FF0000) >> 16;
556
            $d  = abs($r2 - $r1) + abs($g2 - $g1) + abs($b2 - $b1);
557
558
            if (($idx == -1) || ($d < $dif)) {
0 ignored issues
show
The variable $dif 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...
559
                $idx = $i;
560
                $dif = $d;
561
            }
562
        }
563
564
        return $idx;
565
    }
566
}
567
568
///////////////////////////////////////////////////////////////////////////////////////////////////
569
570
/**
571
 * Class CGIFFILEHEADER
572
 */
573
class CGIFFILEHEADER
574
{
575
    public $m_lpVer;
576
    public $m_nWidth;
577
    public $m_nHeight;
578
    public $m_bGlobalClr;
579
    public $m_nColorRes;
580
    public $m_bSorted;
581
    public $m_nTableSize;
582
    public $m_nBgColor;
583
    public $m_nPixelRatio;
584
    public $m_colorTable;
585
586
    ///////////////////////////////////////////////////////////////////////////
587
588
    // CONSTRUCTOR
589
    /**
590
     * CGIFFILEHEADER constructor.
591
     */
592
    public function __construct()
593
    {
594
        unset($this->m_lpVer);
595
        unset($this->m_nWidth);
596
        unset($this->m_nHeight);
597
        unset($this->m_bGlobalClr);
598
        unset($this->m_nColorRes);
599
        unset($this->m_bSorted);
600
        unset($this->m_nTableSize);
601
        unset($this->m_nBgColor);
602
        unset($this->m_nPixelRatio);
603
        unset($this->m_colorTable);
604
    }
605
606
    ///////////////////////////////////////////////////////////////////////////
607
608
    /**
609
     * @param $lpData
610
     * @param $hdrLen
611
     * @return bool
612
     */
613
    public function load($lpData, &$hdrLen)
614
    {
615
        $hdrLen = 0;
616
617
        $this->m_lpVer = substr($lpData, 0, 6);
618
        if (($this->m_lpVer <> 'GIF87a') && ($this->m_lpVer <> 'GIF89a')) {
619
            return false;
620
        }
621
622
        $this->m_nWidth  = $this->w2i(substr($lpData, 6, 2));
623
        $this->m_nHeight = $this->w2i(substr($lpData, 8, 2));
624
        if (!$this->m_nWidth || !$this->m_nHeight) {
625
            return false;
626
        }
627
628
        $b                   = ord(substr($lpData, 10, 1));
629
        $this->m_bGlobalClr  = ($b & 0x80) ? true : false;
630
        $this->m_nColorRes   = ($b & 0x70) >> 4;
631
        $this->m_bSorted     = ($b & 0x08) ? true : false;
632
        $this->m_nTableSize  = 2 << ($b & 0x07);
633
        $this->m_nBgColor    = ord(substr($lpData, 11, 1));
634
        $this->m_nPixelRatio = ord(substr($lpData, 12, 1));
635
        $hdrLen              = 13;
636
637 View Code Duplication
        if ($this->m_bGlobalClr) {
0 ignored issues
show
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...
638
            $this->m_colorTable = new CGIFCOLORTABLE();
639
            if (!$this->m_colorTable->load(substr($lpData, $hdrLen), $this->m_nTableSize)) {
640
                return false;
641
            }
642
            $hdrLen += 3 * $this->m_nTableSize;
643
        }
644
645
        return true;
646
    }
647
648
    ///////////////////////////////////////////////////////////////////////////
649
650
    /**
651
     * @param $str
652
     * @return int
653
     */
654
    public function w2i($str)
655
    {
656
        return ord(substr($str, 0, 1)) + (ord(substr($str, 1, 1)) << 8);
657
    }
658
}
659
660
///////////////////////////////////////////////////////////////////////////////////////////////////
661
662
/**
663
 * Class CGIFIMAGEHEADER
664
 */
665
class CGIFIMAGEHEADER
666
{
667
    public $m_nLeft;
668
    public $m_nTop;
669
    public $m_nWidth;
670
    public $m_nHeight;
671
    public $m_bLocalClr;
672
    public $m_bInterlace;
673
    public $m_bSorted;
674
    public $m_nTableSize;
675
    public $m_colorTable;
676
677
    ///////////////////////////////////////////////////////////////////////////
678
679
    // CONSTRUCTOR
680
    /**
681
     * CGIFIMAGEHEADER constructor.
682
     */
683
    public function __construct()
684
    {
685
        unset($this->m_nLeft);
686
        unset($this->m_nTop);
687
        unset($this->m_nWidth);
688
        unset($this->m_nHeight);
689
        unset($this->m_bLocalClr);
690
        unset($this->m_bInterlace);
691
        unset($this->m_bSorted);
692
        unset($this->m_nTableSize);
693
        unset($this->m_colorTable);
694
    }
695
696
    ///////////////////////////////////////////////////////////////////////////
697
698
    /**
699
     * @param $lpData
700
     * @param $hdrLen
701
     * @return bool
702
     */
703
    public function load($lpData, &$hdrLen)
704
    {
705
        $hdrLen = 0;
706
707
        $this->m_nLeft   = $this->w2i(substr($lpData, 0, 2));
708
        $this->m_nTop    = $this->w2i(substr($lpData, 2, 2));
709
        $this->m_nWidth  = $this->w2i(substr($lpData, 4, 2));
710
        $this->m_nHeight = $this->w2i(substr($lpData, 6, 2));
711
712
        if (!$this->m_nWidth || !$this->m_nHeight) {
713
            return false;
714
        }
715
716
        $b                  = ord($lpData{8});
717
        $this->m_bLocalClr  = ($b & 0x80) ? true : false;
718
        $this->m_bInterlace = ($b & 0x40) ? true : false;
719
        $this->m_bSorted    = ($b & 0x20) ? true : false;
720
        $this->m_nTableSize = 2 << ($b & 0x07);
721
        $hdrLen             = 9;
722
723 View Code Duplication
        if ($this->m_bLocalClr) {
0 ignored issues
show
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...
724
            $this->m_colorTable = new CGIFCOLORTABLE();
725
            if (!$this->m_colorTable->load(substr($lpData, $hdrLen), $this->m_nTableSize)) {
726
                return false;
727
            }
728
            $hdrLen += 3 * $this->m_nTableSize;
729
        }
730
731
        return true;
732
    }
733
734
    ///////////////////////////////////////////////////////////////////////////
735
736
    /**
737
     * @param $str
738
     * @return int
739
     */
740
    public function w2i($str)
741
    {
742
        return ord(substr($str, 0, 1)) + (ord(substr($str, 1, 1)) << 8);
743
    }
744
}
745
746
///////////////////////////////////////////////////////////////////////////////////////////////////
747
748
/**
749
 * Class CGIFIMAGE
750
 */
751
class CGIFIMAGE
752
{
753
    public $m_disp;
754
    public $m_bUser;
755
    public $m_bTrans;
756
    public $m_nDelay;
757
    public $m_nTrans;
758
    public $m_lpComm;
759
    public $m_gih;
760
    public $m_data;
761
    public $m_lzw;
762
763
    ///////////////////////////////////////////////////////////////////////////
764
765
    /**
766
     * CGIFIMAGE constructor.
767
     */
768
    public function __construct()
769
    {
770
        unset($this->m_disp);
771
        unset($this->m_bUser);
772
        unset($this->m_bTrans);
773
        unset($this->m_nDelay);
774
        unset($this->m_nTrans);
775
        unset($this->m_lpComm);
776
        unset($this->m_data);
777
        $this->m_gih = new CGIFIMAGEHEADER();
778
        $this->m_lzw = new CGIFLZW();
779
    }
780
781
    ///////////////////////////////////////////////////////////////////////////
782
783
    /**
784
     * @param $data
785
     * @param $datLen
786
     * @return bool
787
     */
788
    public function load($data, &$datLen)
789
    {
790
        $datLen = 0;
791
792
        while (true) {
793
            $b    = ord($data{0});
794
            $data = substr($data, 1);
795
            $datLen++;
796
797
            switch ($b) {
798
                case 0x21: // Extension
799
                    if (!$this->skipExt($data, $len = 0)) {
0 ignored issues
show
$len = 0 cannot be passed to skipext() as the parameter $extLen expects a reference.
Loading history...
800
                        return false;
801
                    }
802
                    $datLen += $len;
803
                    break;
804
805
                case 0x2C: // Image
806
                    // LOAD HEADER & COLOR TABLE
807
                    if (!$this->m_gih->load($data, $len = 0)) {
0 ignored issues
show
$len = 0 cannot be passed to load() as the parameter $hdrLen expects a reference.
Loading history...
808
                        return false;
809
                    }
810
                    $data   = substr($data, $len);
811
                    $datLen += $len;
812
813
                    // ALLOC BUFFER
814
                    if (!($this->m_data = $this->m_lzw->deCompress($data, $len = 0))) {
0 ignored issues
show
$len = 0 cannot be passed to decompress() as the parameter $datLen expects a reference.
Loading history...
815
                        return false;
816
                    }
817
                    $data   = substr($data, $len);
0 ignored issues
show
$data is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
818
                    $datLen += $len;
819
820
                    if ($this->m_gih->m_bInterlace) {
821
                        $this->deInterlace();
822
                    }
823
824
                    return true;
825
826
                case 0x3B: // EOF
827
                default:
828
                    return false;
829
            }
830
        }
831
832
        return false;
833
    }
834
835
    ///////////////////////////////////////////////////////////////////////////
836
837
    /**
838
     * @param $data
839
     * @param $extLen
840
     * @return bool
841
     */
842
    public function skipExt(&$data, &$extLen)
843
    {
844
        $extLen = 0;
845
846
        $b    = ord($data{0});
847
        $data = substr($data, 1);
848
        $extLen++;
849
850
        switch ($b) {
851
            case 0xF9: // Graphic Control
852
                $b              = ord($data{1});
853
                $this->m_disp   = ($b & 0x1C) >> 2;
854
                $this->m_bUser  = ($b & 0x02) ? true : false;
855
                $this->m_bTrans = ($b & 0x01) ? true : false;
856
                $this->m_nDelay = $this->w2i(substr($data, 2, 2));
857
                $this->m_nTrans = ord($data{4});
858
                break;
859
860
            case 0xFE: // Comment
861
                $this->m_lpComm = substr($data, 1, ord($data{0}));
862
                break;
863
864
            case 0x01: // Plain text
865
                break;
866
867
            case 0xFF: // Application
868
                break;
869
        }
870
871
        // SKIP DEFAULT AS DEFS MAY CHANGE
872
        $b    = ord($data{0});
873
        $data = substr($data, 1);
874
        $extLen++;
875
        while ($b > 0) {
876
            $data   = substr($data, $b);
877
            $extLen += $b;
878
            $b      = ord($data{0});
879
            $data   = substr($data, 1);
880
            $extLen++;
881
        }
882
883
        return true;
884
    }
885
886
    ///////////////////////////////////////////////////////////////////////////
887
888
    /**
889
     * @param $str
890
     * @return int
891
     */
892
    public function w2i($str)
893
    {
894
        return ord(substr($str, 0, 1)) + (ord(substr($str, 1, 1)) << 8);
895
    }
896
897
    ///////////////////////////////////////////////////////////////////////////
898
899
    public function deInterlace()
900
    {
901
        $data = $this->m_data;
902
903
        for ($i = 0; $i < 4; $i++) {
904
            switch ($i) {
905
                case 0:
906
                    $s = 8;
907
                    $y = 0;
908
                    break;
909
910
                case 1:
911
                    $s = 8;
912
                    $y = 4;
913
                    break;
914
915
                case 2:
916
                    $s = 4;
917
                    $y = 2;
918
                    break;
919
920
                case 3:
921
                    $s = 2;
922
                    $y = 1;
923
                    break;
924
            }
925
926
            for (; $y < $this->m_gih->m_nHeight; $y += $s) {
0 ignored issues
show
The variable $y 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...
The variable $s 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...
927
                $lne          = substr($this->m_data, 0, $this->m_gih->m_nWidth);
928
                $this->m_data = substr($this->m_data, $this->m_gih->m_nWidth);
929
930
                $data = substr($data, 0, $y * $this->m_gih->m_nWidth) . $lne . substr($data, ($y + 1) * $this->m_gih->m_nWidth);
931
            }
932
        }
933
934
        $this->m_data = $data;
935
    }
936
}
937
938
///////////////////////////////////////////////////////////////////////////////////////////////////
939
940
/**
941
 * Class CGIF
942
 */
943
class CGIF
944
{
945
    public $m_gfh;
946
    public $m_lpData;
947
    public $m_img;
948
    public $m_bLoaded;
949
950
    ///////////////////////////////////////////////////////////////////////////
951
952
    // CONSTRUCTOR
953
    /**
954
     * CGIF constructor.
955
     */
956
    public function __construct()
957
    {
958
        $this->m_gfh     = new CGIFFILEHEADER();
959
        $this->m_img     = new CGIFIMAGE();
960
        $this->m_lpData  = '';
961
        $this->m_bLoaded = false;
962
    }
963
964
    ///////////////////////////////////////////////////////////////////////////
965
966
    /**
967
     * @param $lpszFileName
968
     * @param $iIndex
969
     * @return bool
970
     */
971
    public function loadFile($lpszFileName, $iIndex)
972
    {
973
        if ($iIndex < 0) {
974
            return false;
975
        }
976
977
        // READ FILE
978
        if (!($fh = @fopen($lpszFileName, 'rb'))) {
979
            return false;
980
        }
981
        $this->m_lpData = @fread($fh, @filesize($lpszFileName));
982
        fclose($fh);
983
984
        // GET FILE HEADER
985
        if (!$this->m_gfh->load($this->m_lpData, $len = 0)) {
0 ignored issues
show
$len = 0 cannot be passed to load() as the parameter $hdrLen expects a reference.
Loading history...
986
            return false;
987
        }
988
        $this->m_lpData = substr($this->m_lpData, $len);
989
990
        do {
991
            if (!$this->m_img->load($this->m_lpData, $imgLen = 0)) {
0 ignored issues
show
$imgLen = 0 cannot be passed to load() as the parameter $datLen expects a reference.
Loading history...
992
                return false;
993
            }
994
            $this->m_lpData = substr($this->m_lpData, $imgLen);
995
        } while ($iIndex-- > 0);
996
997
        $this->m_bLoaded = true;
998
999
        return true;
1000
    }
1001
1002
    ///////////////////////////////////////////////////////////////////////////
1003
1004
    /**
1005
     * @param $lpszFileName
1006
     * @param $width
1007
     * @param $height
1008
     * @return bool
1009
     */
1010
    public function getSize($lpszFileName, &$width, &$height)
1011
    {
1012
        if (!($fh = @fopen($lpszFileName, 'rb'))) {
1013
            return false;
1014
        }
1015
        $data = @fread($fh, @filesize($lpszFileName));
1016
        @fclose($fh);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
1017
1018
        $gfh = new CGIFFILEHEADER();
1019
        if (!$gfh->load($data, $len = 0)) {
0 ignored issues
show
$len = 0 cannot be passed to load() as the parameter $hdrLen expects a reference.
Loading history...
1020
            return false;
1021
        }
1022
1023
        $width  = $gfh->m_nWidth;
1024
        $height = $gfh->m_nHeight;
1025
1026
        return true;
1027
    }
1028
1029
    ///////////////////////////////////////////////////////////////////////////
1030
1031
    /**
1032
     * @param $bgColor
1033
     * @return string
0 ignored issues
show
Should the return type not be false|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
1034
     */
1035
    public function getBmp($bgColor)
1036
    {
1037
        $out = '';
1038
1039
        if (!$this->m_bLoaded) {
1040
            return false;
1041
        }
1042
1043
        // PREPARE COLOR TABLE (RGBQUADs)
1044 View Code Duplication
        if ($this->m_img->m_gih->m_bLocalClr) {
0 ignored issues
show
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...
1045
            $nColors = $this->m_img->m_gih->m_nTableSize;
1046
            $rgbq    = $this->m_img->m_gih->m_colorTable->toRGBQuad();
1047
            if ($bgColor != -1) {
1048
                $bgColor = $this->m_img->m_gih->m_colorTable->colorIndex($bgColor);
1049
            }
1050
        } elseif ($this->m_gfh->m_bGlobalClr) {
1051
            $nColors = $this->m_gfh->m_nTableSize;
1052
            $rgbq    = $this->m_gfh->m_colorTable->toRGBQuad();
1053
            if ($bgColor != -1) {
1054
                $bgColor = $this->m_gfh->m_colorTable->colorIndex($bgColor);
1055
            }
1056
        } else {
1057
            $nColors = 0;
1058
            $bgColor = -1;
1059
        }
1060
1061
        // PREPARE BITMAP BITS
1062
        $data = $this->m_img->m_data;
1063
        $nPxl = ($this->m_gfh->m_nHeight - 1) * $this->m_gfh->m_nWidth;
1064
        $bmp  = '';
1065
        $nPad = ($this->m_gfh->m_nWidth % 4) ? 4 - ($this->m_gfh->m_nWidth % 4) : 0;
1066
        for ($y = 0; $y < $this->m_gfh->m_nHeight; $y++) {
1067
            for ($x = 0; $x < $this->m_gfh->m_nWidth; $x++, $nPxl++) {
1068
                if (($x >= $this->m_img->m_gih->m_nLeft)
1069
                    && ($y >= $this->m_img->m_gih->m_nTop)
1070
                    && ($x < ($this->m_img->m_gih->m_nLeft + $this->m_img->m_gih->m_nWidth))
1071
                    && ($y < ($this->m_img->m_gih->m_nTop + $this->m_img->m_gih->m_nHeight))
1072
                ) {
1073
                    // PART OF IMAGE
1074
                    if (@$this->m_img->m_bTrans && (ord($data{$nPxl}) == $this->m_img->m_nTrans)) {
1075
                        // TRANSPARENT -> BACKGROUND
1076 View Code Duplication
                        if ($bgColor == -1) {
0 ignored issues
show
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...
1077
                            $bmp .= chr($this->m_gfh->m_nBgColor);
1078
                        } else {
1079
                            $bmp .= chr($bgColor);
1080
                        }
1081
                    } else {
1082
                        $bmp .= $data{$nPxl};
1083
                    }
1084 View Code Duplication
                } else {
0 ignored issues
show
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...
1085
                    // BACKGROUND
1086
                    if ($bgColor == -1) {
1087
                        $bmp .= chr($this->m_gfh->m_nBgColor);
1088
                    } else {
1089
                        $bmp .= chr($bgColor);
1090
                    }
1091
                }
1092
            }
1093
            $nPxl -= $this->m_gfh->m_nWidth << 1;
1094
1095
            // ADD PADDING
1096
            for ($x = 0; $x < $nPad; $x++) {
1097
                $bmp .= "\x00";
1098
            }
1099
        }
1100
1101
        // BITMAPFILEHEADER
1102
        $out .= 'BM';
1103
        $out .= $this->dword(14 + 40 + ($nColors << 2) + strlen($bmp));
1104
        $out .= "\x00\x00";
1105
        $out .= "\x00\x00";
1106
        $out .= $this->dword(14 + 40 + ($nColors << 2));
1107
1108
        // BITMAPINFOHEADER
1109
        $out .= $this->dword(40);
1110
        $out .= $this->dword($this->m_gfh->m_nWidth);
1111
        $out .= $this->dword($this->m_gfh->m_nHeight);
1112
        $out .= "\x01\x00";
1113
        $out .= "\x08\x00";
1114
        $out .= "\x00\x00\x00\x00";
1115
        $out .= "\x00\x00\x00\x00";
1116
        $out .= "\x12\x0B\x00\x00";
1117
        $out .= "\x12\x0B\x00\x00";
1118
        $out .= $this->dword($nColors % 256);
1119
        $out .= "\x00\x00\x00\x00";
1120
1121
        // COLOR TABLE
1122
        if ($nColors > 0) {
1123
            $out .= $rgbq;
0 ignored issues
show
The variable $rgbq 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...
1124
        }
1125
1126
        // DATA
1127
        $out .= $bmp;
1128
1129
        return $out;
1130
    }
1131
1132
    ///////////////////////////////////////////////////////////////////////////
1133
1134
    /**
1135
     * @param $bgColor
1136
     * @return string
0 ignored issues
show
Should the return type not be false|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
1137
     */
1138
    public function getPng($bgColor)
1139
    {
1140
        $out = '';
1141
1142
        if (!$this->m_bLoaded) {
1143
            return false;
1144
        }
1145
1146
        // PREPARE COLOR TABLE (RGBQUADs)
1147 View Code Duplication
        if ($this->m_img->m_gih->m_bLocalClr) {
0 ignored issues
show
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...
1148
            $nColors = $this->m_img->m_gih->m_nTableSize;
1149
            $pal     = $this->m_img->m_gih->m_colorTable->toString();
1150
            if ($bgColor != -1) {
1151
                $bgColor = $this->m_img->m_gih->m_colorTable->colorIndex($bgColor);
1152
            }
1153
        } elseif ($this->m_gfh->m_bGlobalClr) {
1154
            $nColors = $this->m_gfh->m_nTableSize;
1155
            $pal     = $this->m_gfh->m_colorTable->toString();
1156
            if ($bgColor != -1) {
1157
                $bgColor = $this->m_gfh->m_colorTable->colorIndex($bgColor);
1158
            }
1159
        } else {
1160
            $nColors = 0;
1161
            $bgColor = -1;
1162
        }
1163
1164
        // PREPARE BITMAP BITS
1165
        $data = $this->m_img->m_data;
1166
        $nPxl = 0;
1167
        $bmp  = '';
1168
        for ($y = 0; $y < $this->m_gfh->m_nHeight; $y++) {
1169
            $bmp .= "\x00";
1170
            for ($x = 0; $x < $this->m_gfh->m_nWidth; $x++, $nPxl++) {
1171
                if (($x >= $this->m_img->m_gih->m_nLeft)
1172
                    && ($y >= $this->m_img->m_gih->m_nTop)
1173
                    && ($x < ($this->m_img->m_gih->m_nLeft + $this->m_img->m_gih->m_nWidth))
1174
                    && ($y < ($this->m_img->m_gih->m_nTop + $this->m_img->m_gih->m_nHeight))
1175
                ) {
1176
                    // PART OF IMAGE
1177
                    $bmp .= $data{$nPxl};
1178 View Code Duplication
                } else {
0 ignored issues
show
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...
1179
                    // BACKGROUND
1180
                    if ($bgColor == -1) {
1181
                        $bmp .= chr($this->m_gfh->m_nBgColor);
1182
                    } else {
1183
                        $bmp .= chr($bgColor);
1184
                    }
1185
                }
1186
            }
1187
        }
1188
        $bmp = gzcompress($bmp, 9);
1189
1190
        ///////////////////////////////////////////////////////////////////////
1191
        // SIGNATURE
1192
        $out .= "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A";
1193
        ///////////////////////////////////////////////////////////////////////
1194
        // HEADER
1195
        $out .= "\x00\x00\x00\x0D";
1196
        $tmp = 'IHDR';
1197
        $tmp .= $this->ndword($this->m_gfh->m_nWidth);
1198
        $tmp .= $this->ndword($this->m_gfh->m_nHeight);
1199
        $tmp .= "\x08\x03\x00\x00\x00";
1200
        $out .= $tmp;
1201
        $out .= $this->ndword(crc32($tmp));
1202
        ///////////////////////////////////////////////////////////////////////
1203
        // PALETTE
1204
        if ($nColors > 0) {
1205
            $out .= $this->ndword($nColors * 3);
1206
            $tmp = 'PLTE';
1207
            $tmp .= $pal;
0 ignored issues
show
The variable $pal 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...
1208
            $out .= $tmp;
1209
            $out .= $this->ndword(crc32($tmp));
1210
        }
1211
        ///////////////////////////////////////////////////////////////////////
1212
        // TRANSPARENCY
1213
        if (@$this->m_img->m_bTrans && ($nColors > 0)) {
1214
            $out .= $this->ndword($nColors);
1215
            $tmp = 'tRNS';
1216
            for ($i = 0; $i < $nColors; $i++) {
1217
                $tmp .= ($i == $this->m_img->m_nTrans) ? "\x00" : "\xFF";
1218
            }
1219
            $out .= $tmp;
1220
            $out .= $this->ndword(crc32($tmp));
1221
        }
1222
        ///////////////////////////////////////////////////////////////////////
1223
        // DATA BITS
1224
        $out .= $this->ndword(strlen($bmp));
1225
        $tmp = 'IDAT';
1226
        $tmp .= $bmp;
1227
        $out .= $tmp;
1228
        $out .= $this->ndword(crc32($tmp));
1229
        ///////////////////////////////////////////////////////////////////////
1230
        // END OF FILE
1231
        $out .= "\x00\x00\x00\x00IEND\xAE\x42\x60\x82";
1232
1233
        return $out;
1234
    }
1235
1236
    ///////////////////////////////////////////////////////////////////////////
1237
1238
    // Added by James Heinrich <[email protected]> - January 5, 2003
1239
1240
    // Takes raw image data and plots it pixel-by-pixel on a new GD image and returns that
1241
    // It's extremely slow, but the only solution when ImageCreateFromString() fails
1242
    /**
1243
     * @return bool|resource
0 ignored issues
show
Should the return type not be false|null|resource?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
1244
     */
1245
    public function getGD_PixelPlotterVersion()
1246
    {
1247
        if (!$this->m_bLoaded) {
1248
            return false;
1249
        }
1250
1251
        // PREPARE COLOR TABLE (RGBQUADs)
1252
        if ($this->m_img->m_gih->m_bLocalClr) {
1253
            $pal = $this->m_img->m_gih->m_colorTable->toString();
1254
        } elseif ($this->m_gfh->m_bGlobalClr) {
1255
            $pal = $this->m_gfh->m_colorTable->toString();
1256
        } else {
1257
            die('No color table available in getGD_PixelPlotterVersion()');
1258
        }
1259
1260
        $PlottingIMG    = imagecreate($this->m_gfh->m_nWidth, $this->m_gfh->m_nHeight);
1261
        $NumColorsInPal = floor(strlen($pal) / 3);
1262
        for ($i = 0; $i < $NumColorsInPal; $i++) {
1263
            $ThisImageColor[$i] = imagecolorallocate($PlottingIMG, ord($pal{($i * 3) + 0}), ord($pal{($i * 3) + 1}), ord($pal{($i * 3) + 2}));
0 ignored issues
show
Coding Style Comprehensibility introduced by
$ThisImageColor was never initialized. Although not strictly required by PHP, it is generally a good practice to add $ThisImageColor = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
1264
        }
1265
1266
        // PREPARE BITMAP BITS
1267
        $data = $this->m_img->m_data;
1268
        $nPxl = ($this->m_gfh->m_nHeight - 1) * $this->m_gfh->m_nWidth;
1269
        for ($y = 0; $y < $this->m_gfh->m_nHeight; $y++) {
1270
            if (!phpthumb_functions::FunctionIsDisabled('set_time_limit')) {
1271
                set_time_limit(30);
1272
            }
1273
            for ($x = 0; $x < $this->m_gfh->m_nWidth; $x++, $nPxl++) {
1274
                if (($x >= $this->m_img->m_gih->m_nLeft)
1275
                    && ($y >= $this->m_img->m_gih->m_nTop)
1276
                    && ($x < ($this->m_img->m_gih->m_nLeft + $this->m_img->m_gih->m_nWidth))
1277
                    && ($y < ($this->m_img->m_gih->m_nTop + $this->m_img->m_gih->m_nHeight))
1278
                ) {
1279
                    // PART OF IMAGE
1280
                    if (@$this->m_img->m_bTrans && (ord($data{$nPxl}) == $this->m_img->m_nTrans)) {
1281
                        imagesetpixel($PlottingIMG, $x, $this->m_gfh->m_nHeight - $y - 1, $ThisImageColor[$this->m_gfh->m_nBgColor]);
0 ignored issues
show
The variable $ThisImageColor 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...
1282
                    } else {
1283
                        imagesetpixel($PlottingIMG, $x, $this->m_gfh->m_nHeight - $y - 1, $ThisImageColor[ord($data{$nPxl})]);
1284
                    }
1285
                } else {
1286
                    // BACKGROUND
1287
                    imagesetpixel($PlottingIMG, $x, $this->m_gfh->m_nHeight - $y - 1, $ThisImageColor[$this->m_gfh->m_nBgColor]);
1288
                }
1289
            }
1290
            $nPxl -= $this->m_gfh->m_nWidth << 1;
1291
        }
1292
1293
        return $PlottingIMG;
1294
    }
1295
1296
    ///////////////////////////////////////////////////////////////////////////
1297
1298
    /**
1299
     * @param $val
1300
     * @return string
1301
     */
1302
    public function dword($val)
1303
    {
1304
        $val = (int)$val;
1305
1306
        return chr($val & 0xFF) . chr(($val & 0xFF00) >> 8) . chr(($val & 0xFF0000) >> 16) . chr(($val & 0xFF000000) >> 24);
1307
    }
1308
1309
    ///////////////////////////////////////////////////////////////////////////
1310
1311
    /**
1312
     * @param $val
1313
     * @return string
1314
     */
1315
    public function ndword($val)
1316
    {
1317
        $val = (int)$val;
1318
1319
        return chr(($val & 0xFF000000) >> 24) . chr(($val & 0xFF0000) >> 16) . chr(($val & 0xFF00) >> 8) . chr($val & 0xFF);
1320
    }
1321
1322
    ///////////////////////////////////////////////////////////////////////////
1323
1324
    public function width()
1325
    {
1326
        return $this->m_gfh->m_nWidth;
1327
    }
1328
1329
    ///////////////////////////////////////////////////////////////////////////
1330
1331
    public function height()
1332
    {
1333
        return $this->m_gfh->m_nHeight;
1334
    }
1335
1336
    ///////////////////////////////////////////////////////////////////////////
1337
1338
    public function comment()
1339
    {
1340
        return $this->m_img->m_lpComm;
1341
    }
1342
1343
    ///////////////////////////////////////////////////////////////////////////
1344
1345
    /**
1346
     * @return bool
1347
     */
1348
    public function loaded()
1349
    {
1350
        return $this->m_bLoaded;
1351
    }
1352
}
1353
1354
///////////////////////////////////////////////////////////////////////////////////////////////////
1355