Issues (1)

Security Analysis    no request data  

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.

src/GlLazyLoadImg.php (1 issue)

Labels
Severity

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
/**
4
 * Lazy loading images
5
 *
6
 * PHP version 5.4
7
 *
8
 * @category  GLICER
9
 * @package   Contact
10
 * @author    Emmanuel ROECKER <[email protected]>
11
 * @author    Rym BOUCHAGOUR <[email protected]>
12
 * @copyright 2012-2013 GLICER
13
 * @license   Proprietary property of GLICER
14
 * @link      http://www.glicer.com
15
 *
16
 * Created : 8/26/16
17
 * File : GlLazyLoadImg.php
18
 *
19
 */
20
21
namespace GlLazyLoadImg;
22
23
use GlHtml\GlHtml;
24
25
class GlLazyLoadImg
26
{
27
    const BLANK = 0;
28
    const LOSSY = 1;
29
30
    /**
31
     * @var string rootpath
32
     */
33
    private $rootpath;
34
35
    /**
36
     * @var string
37
     */
38
    private $moveToAttribute;
39
40
    /**
41
     * @var array
42
     */
43
    private $excludeAttributesList;
44
45
    /**
46
     * constructor - set root directory to relative url
47
     *
48
     * @param string $rootpath
49
     * @param int    $type
50
     * @param string $moveToAttribute
51
     * @param array  $excludeAttributesList
52
     */
53
    public function __construct(
54
        $rootpath,
55
        $type = self::BLANK,
56
        $moveToAttribute = 'data-original',
57
        array $excludeAttributesList = []
58
    ) {
59
        $this->rootpath              = $rootpath;
60
        $this->type                  = $type;
0 ignored issues
show
The property type 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...
61
        $this->moveToAttribute       = $moveToAttribute;
62
        $this->excludeAttributesList = $excludeAttributesList;
63
    }
64
65
    
66
    private function gcd($a,$b) {
67
         return ($a % $b) ? $this->gcd($b,$a % $b) : $b;
68
    }
69
    
70
    /**
71
     * create lossy gif image and encode to data uri format
72
     * minimal size is jpeg
73
     *
74
     * @param     $src          resource GD library
75
     * @param int $minwidth     min width in pixels (height autocalculte)
76
     *
77
     * @return string           data uri format
78
     */
79
    public function getLossyGifDataURI($src, $minwidth = 75)
80
    {
81
        $src = imagescale($src, $minwidth, -1, IMG_NEAREST_NEIGHBOUR);
82
83
        ob_start();
84
        imagegif($src);
85
        $data = ob_get_contents();
86
        ob_end_clean();
87
88
        imagedestroy($src);
89
90
        $base64 = base64_encode($data);
91
92
        $mime = 'image/gif';
93
94
        return ('data:' . $mime . ';base64,' . $base64);
95
    }
96
97
    /**
98
     * create blank image with same size in data uri format
99
     * minimal size is gif
100
     *
101
     * @param     $src
102
     * @param int $red   red component background color (default 255)
103
     * @param int $green green component background color (default 255)
104
     * @param int $blue  blue component background color (default 255)
105
     *
106
     * @return string            data uri format
107
     */
108
    public function getMinGifDataURI($src, $red = 255, $green = 255, $blue = 255)
109
    {
110
        $width  = imagesx($src);
111
        $height = imagesy($src);
112
113
        $gcd    = $this->gcd($width, $height);
114
        $width  = $width / $gcd;
115
        $height = $height / $gcd;
116
            
117
        $img   = imagecreatetruecolor($width, $height);
118
        $bgcol = imagecolorallocate($img, $red, $green, $blue);
119
        imageFill($img, 0, 0, $bgcol);
120
121
        ob_start();
122
        imagegif($img);
123
        $data = ob_get_contents();
124
        ob_end_clean();
125
126
        $base64 = base64_encode($data);
127
128
        imagedestroy($img);
129
130
        $mime = 'image/gif';
131
132
        return ('data:' . $mime . ';base64,' . $base64);
133
    }
134
135
136
    /**
137
     * find type of image and open it
138
     *
139
     * @param string $file
140
     *
141
     * @return bool|resource
142
     */
143
    private function openImage($file)
144
    {
145
        if (!file_exists($file)) {
146
            return false;
147
        }
148
149
        $size = getimagesize($file);
150
        switch ($size["mime"]) {
151
            case "image/jpeg":
152
                $im = imagecreatefromjpeg($file);
153
                break;
154
            case "image/gif":
155
                $im = imagecreatefromgif($file);
156
                break;
157
            case "image/png":
158
                $im = imagecreatefrompng($file);
159
                break;
160
            default:
161
                $im = false;
162
                break;
163
        }
164
165
        return $im;
166
    }
167
168
    /**
169
     * replace all src attributes from img tags with datauri and set another attribute with old src value
170
     * support jpeg, png or gif file format
171
     *
172
     * @param string $html
173
     *
174
     * @throws \Exception
175
     * @return string
176
     */
177
    public function autoDataURI($html)
178
    {        
179
        $html = new GlHtml($html);
180
        $imgs = $html->get('img');
181
        foreach ($imgs as $img) {
182
            $src          = $img->getAttribute('src');
183
            $pathimagesrc = $this->rootpath . '/' . $src;
184
185
            $imgbin = $this->openImage($pathimagesrc);
186
            if ($imgbin) {
187
                switch ($this->type) {
188
                    case self::BLANK:
189
                        $datauri = $this->getMinGifDataURI($imgbin);
190
                        break;
191
                    case self::LOSSY:
192
                        $datauri = $this->getLossyGifDataURI($imgbin);
193
                        break;
194
                    default:
195
                        throw new \Exception("Type unknown (only self::BLANK=0 or self::LOSSY=1 accepted) : " . $this->type);
196
                }
197
198
                $width  = imagesx($imgbin);
199
                $height = imagesy($imgbin);
200
                $img->setAttributes(['width' => $width, 'height' => $height]);
201
202
                if (!$img->hasAttributes($this->excludeAttributesList)) {
203
                    $img->setAttributes([$this->moveToAttribute => $src, 'src' => $datauri]);
204
                }
205
                imagedestroy($imgbin);
206
            }
207
        }
208
209
        return $html->html();
210
    }
211
}
212