Passed
Push — master ( 81ba93...c6c854 )
by Michael
03:30
created

sf_attachmentImage()   C

Complexity

Conditions 15
Paths 104

Size

Total Lines 57
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 39
c 0
b 0
f 0
dl 0
loc 57
rs 5.8833
cc 15
nc 104
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
 * You may not change or alter any portion of this comment or credits
4
 * of supporting developers from this source code or any supporting source code
5
 * which is considered copyrighted (c) material of the original comment or credit authors.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * @copyright      {@link https://xoops.org/ XOOPS Project}
14
 * @license        {@link http://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
15
 * @package
16
 * @since
17
 * @author         XOOPS Development Team, phppp (D.J., [email protected])
18
 */
19
20
use XoopsModules\Smartfaq;
21
22
if (!defined('NEWBB_FUNCTIONS_IMAGE')) :
23
    define('NEWBB_FUNCTIONS_IMAGE', true);
24
25
    /**
26
     * @param $source
27
     * @return string
28
     */
29
    function sf_attachmentImage($source)
30
    {
31
        /** @var Smartfaq\Helper $helper */
32
        $helper = Smartfaq\Helper::getInstance();
33
34
        $img_path   = XOOPS_ROOT_PATH . '/' . $helper->getConfig('dir_attachments');
35
        $img_url    = XOOPS_URL . '/' . $helper->getConfig('dir_attachments');
36
        $thumb_path = $img_path . '/thumbs';
37
        $thumb_url  = $img_url . '/thumbs';
38
39
        $thumb     = $thumb_path . '/' . $source;
40
        $image     = $img_path . '/' . $source;
41
        $thumb_url = $thumb_url . '/' . $source;
42
        $image_url = $img_url . '/' . $source;
43
44
        $imginfo  = @getimagesize($image);
45
        $img_info = (count($imginfo) > 0) ? $imginfo[0] . 'X' . $imginfo[1] . ' px' : '';
0 ignored issues
show
Bug introduced by
It seems like $imginfo can also be of type false; however, parameter $var of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
        $img_info = (count(/** @scrutinizer ignore-type */ $imginfo) > 0) ? $imginfo[0] . 'X' . $imginfo[1] . ' px' : '';
Loading history...
46
47
        if ($helper->getConfig('max_image_width') > 0 && $helper->getConfig('max_image_height') > 0) {
48
            if ($imginfo[0] > $helper->getConfig('max_image_width')
49
                || $imginfo[1] > $helper->getConfig('max_image_height')) {
50
                //if (!file_exists($thumb_path.'/'.$source) && $imginfo[0] > $helper->getConfig('max_img_width')) {
51
                if (!file_exists($thumb_path . '/' . $source)) {
52
                    sf_createThumbnail($source, $helper->getConfig('max_image_width'));
53
                }
54
            }
55
56
            if ($imginfo[0] > $helper->getConfig('max_image_width')
57
                || $imginfo[1] > $helper->getConfig('max_image_height')) {
58
                $pseudo_width  = $helper->getConfig('max_image_width');
59
                $pseudo_height = $helper->getConfig('max_image_width') * ($imginfo[1] / $imginfo[0]);
60
                $pseudo_size   = "width='" . $pseudo_width . "px' height='" . $pseudo_height . "px'";
61
            }
62
            // irmtfan to fix Undefined variable: pseudo_height
63
            if (!empty($pseudo_height) && $helper->getConfig('max_image_height') > 0
64
                && $pseudo_height > $helper->getConfig('max_image_height')) {
65
                $pseudo_height = $helper->getConfig('max_image_height');
66
                $pseudo_width  = $helper->getConfig('max_image_height') * ($imginfo[0] / $imginfo[1]);
67
                $pseudo_size   = "width='" . $pseudo_width . "px' height='" . $pseudo_height . "px'";
68
            }
69
        }
70
71
        if (file_exists($thumb)) {
72
            $attachmentImage = '<a href="' . $image_url . '" title="' . $source . ' ' . $img_info . '" target="_blank">';
73
            $attachmentImage .= '<img src="' . $thumb_url . '" alt="' . $source . ' ' . $img_info . '">';
74
            $attachmentImage .= '</a>';
75
        } elseif (!empty($pseudo_size)) {
76
            $attachmentImage = '<a href="' . $image_url . '" title="' . $source . ' ' . $img_info . '" target="_blank">';
77
            $attachmentImage .= '<img src="' . $image_url . '" ' . $pseudo_size . ' alt="' . $source . ' ' . $img_info . '">';
78
            $attachmentImage .= '</a>';
79
        } elseif (file_exists($image)) {
80
            $attachmentImage = '<img src="' . $image_url . '" alt="' . $source . ' ' . $img_info . '">';
81
        } else {
82
            $attachmentImage = '';
83
        }
84
85
        return $attachmentImage;
86
    }
87
88
    /**
89
     * @param $source
90
     * @param $thumb_width
91
     * @return bool
92
     */
93
    function sf_createThumbnail($source, $thumb_width)
94
    {
95
        /** @var Smartfaq\Helper $helper */
96
        $helper = Smartfaq\Helper::getInstance();
97
98
        $img_path   = XOOPS_ROOT_PATH . '/' . $helper->getConfig('dir_attachments');
99
        $thumb_path = $img_path . '/thumbs';
100
        $src_file   = $img_path . '/' . $source;
101
        $new_file   = $thumb_path . '/' . $source;
102
        //$imageLibs = sf_getImageLibs();
103
104
        if (!filesize($src_file) || !is_readable($src_file)) {
105
            return false;
106
        }
107
108
        if (!is_dir($thumb_path) || !is_writable($thumb_path)) {
109
            return false;
110
        }
111
112
        $imginfo = @getimagesize($src_file);
113
114
        if (null === $imginfo) {
115
            return false;
116
        }
117
        if ($imginfo[0] < $thumb_width) {
118
            return false;
119
        }
120
121
        $newWidth  = (int)min($imginfo[0], $thumb_width);
122
        $newHeight = (int)($imginfo[1] * $newWidth / $imginfo[0]);
123
124
        if (1 == $helper->getConfig('image_lib') or 0 == $helper->getConfig('image_lib')) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $helper->getConfig('image_lib') of type mixed|null to 0; this is ambiguous as not only 0 == 0 is true, but null == 0 is true, too. Consider using a strict comparison ===.
Loading history...
125
            if (preg_match('#[A-Z]:|\\\\#Ai', __FILE__)) {
126
                $cur_dir     = __DIR__;
127
                $src_file_im = '"' . $cur_dir . '\\' . str_replace('/', '\\', $src_file) . '"';
128
                $new_file_im = '"' . $cur_dir . '\\' . str_replace('/', '\\', $new_file) . '"';
129
            } else {
130
                $src_file_im = @escapeshellarg($src_file);
131
                $new_file_im = @escapeshellarg($new_file);
132
            }
133
            $path           = empty($helper->getConfig('path_magick')) ? '' : $helper->getConfig('path_magick') . '/';
134
            $magick_command = $path . 'convert -quality 85 -antialias -sample ' . $newWidth . 'x' . $newHeight . ' ' . $src_file_im . ' +profile "*" ' . str_replace('\\', '/', $new_file_im) . '';
135
136
            @passthru($magick_command);
0 ignored issues
show
Bug introduced by
Are you sure the usage of passthru($magick_command) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Security Best Practice introduced by
It seems like you do not handle an error condition for passthru(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

136
            /** @scrutinizer ignore-unhandled */ @passthru($magick_command);

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...
137
            if (file_exists($new_file)) {
138
                return true;
139
            }
140
        }
141
142
        if (2 == $helper->getConfig('image_lib') or 0 == $helper->getConfig('image_lib')) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $helper->getConfig('image_lib') of type mixed|null to 0; this is ambiguous as not only 0 == 0 is true, but null == 0 is true, too. Consider using a strict comparison ===.
Loading history...
143
            $path = empty($helper->getConfig('path_netpbm')) ? '' : $helper->getConfig('path_netpbm') . '/';
144
            if (preg_match("/\.png/i", $source)) {
145
                $cmd = $path . "pngtopnm $src_file | " . $path . "pnmscale -xysize $newWidth $newHeight | " . $path . "pnmtopng > $new_file";
146
            } elseif (preg_match("/\.(jpg|jpeg)/i", $source)) {
147
                $cmd = $path . "jpegtopnm $src_file | " . $path . "pnmscale -xysize $newWidth $newHeight | " . $path . "ppmtojpeg -quality=90 > $new_file";
148
            } elseif (preg_match("/\.gif/i", $source)) {
149
                $cmd = $path . "giftopnm $src_file | " . $path . "pnmscale -xysize $newWidth $newHeight | ppmquant 256 | " . $path . "ppmtogif > $new_file";
150
            }
151
152
            @exec($cmd, $output, $retval);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $cmd does not seem to be defined for all execution paths leading up to this point.
Loading history...
Security Best Practice introduced by
It seems like you do not handle an error condition for exec(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

152
            /** @scrutinizer ignore-unhandled */ @exec($cmd, $output, $retval);

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...
153
            if (file_exists($new_file)) {
154
                return true;
155
            }
156
        }
157
158
        $type            = $imginfo[2];
159
        $supported_types = [];
160
161
        if (!extension_loaded('gd')) {
162
            return false;
163
        }
164
        if (function_exists('imagegif')) {
165
            $supported_types[] = 1;
166
        }
167
        if (function_exists('imagejpeg')) {
168
            $supported_types[] = 2;
169
        }
170
        if (function_exists('imagepng')) {
171
            $supported_types[] = 3;
172
        }
173
174
        $imageCreateFunction = function_exists('imagecreatetruecolor') ? 'imagecreatetruecolor' : 'imagecreate';
175
176
        if (in_array($type, $supported_types)) {
177
            switch ($type) {
178
                case 1:
179
                    if (!function_exists('imagecreatefromgif')) {
180
                        return false;
181
                    }
182
                    $im     = imagecreatefromgif($src_file);
183
                    $new_im = imagecreate($newWidth, $newHeight);
184
                    imagecopyresized($new_im, $im, 0, 0, 0, 0, $newWidth, $newHeight, $imginfo[0], $imginfo[1]);
0 ignored issues
show
Bug introduced by
It seems like $new_im can also be of type false; however, parameter $dst_image of imagecopyresized() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

184
                    imagecopyresized(/** @scrutinizer ignore-type */ $new_im, $im, 0, 0, 0, 0, $newWidth, $newHeight, $imginfo[0], $imginfo[1]);
Loading history...
Bug introduced by
It seems like $im can also be of type false; however, parameter $src_image of imagecopyresized() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

184
                    imagecopyresized($new_im, /** @scrutinizer ignore-type */ $im, 0, 0, 0, 0, $newWidth, $newHeight, $imginfo[0], $imginfo[1]);
Loading history...
185
                    imagegif($new_im, $new_file);
0 ignored issues
show
Bug introduced by
It seems like $new_im can also be of type false; however, parameter $image of imagegif() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

185
                    imagegif(/** @scrutinizer ignore-type */ $new_im, $new_file);
Loading history...
186
                    imagedestroy($im);
0 ignored issues
show
Bug introduced by
It seems like $im can also be of type false; however, parameter $image of imagedestroy() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

186
                    imagedestroy(/** @scrutinizer ignore-type */ $im);
Loading history...
187
                    imagedestroy($new_im);
188
                    break;
189
                case 2:
190
                    $im     = imagecreatefromjpeg($src_file);
191
                    $new_im = $imageCreateFunction($newWidth, $newHeight);
192
                    imagecopyresized($new_im, $im, 0, 0, 0, 0, $newWidth, $newHeight, $imginfo[0], $imginfo[1]);
193
                    imagejpeg($new_im, $new_file, 90);
194
                    imagedestroy($im);
195
                    imagedestroy($new_im);
196
                    break;
197
                case 3:
198
                    $im     = imagecreatefrompng($src_file);
199
                    $new_im = $imageCreateFunction($newWidth, $newHeight);
200
                    imagecopyresized($new_im, $im, 0, 0, 0, 0, $newWidth, $newHeight, $imginfo[0], $imginfo[1]);
201
                    imagepng($new_im, $new_file);
202
                    imagedestroy($im);
203
                    imagedestroy($new_im);
204
                    break;
205
            }
206
        }
207
208
        if (file_exists($new_file)) {
209
            return true;
210
        }
211
212
        return false;
213
    }
214
215
endif;
216