Completed
Push — master ( ed35ab...871ad1 )
by Michael
05:54
created

utils.php ➔ new_thumbnails_creation()   C

Complexity

Conditions 14
Paths 4

Size

Total Lines 28
Code Lines 19

Duplication

Lines 7
Ratio 25 %

Importance

Changes 0
Metric Value
cc 14
eloc 19
nc 4
nop 16
dl 7
loc 28
rs 5.0864
c 0
b 0
f 0

How to fix   Complexity    Many Parameters   

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:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 5 and the first side effect is on line 3.

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

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

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

Loading history...
2
3
if($_SESSION["verify"] != "RESPONSIVEfilemanager") die('forbiden');
4
5
function deleteDir($dir) {
6
    if (!file_exists($dir)) return true;
7
    if (!is_dir($dir)) return unlink($dir);
8
    foreach (scandir($dir) as $item) {
9
        if ($item == '.' || $item == '..') continue;
10
        if (!deleteDir($dir.DIRECTORY_SEPARATOR.$item)) return false;
11
    }
12
13
    return rmdir($dir);
14
}
15
16
function duplicate_file($old_path,$name){
17 View Code Duplication
    if(file_exists($old_path)){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
18
    $info=pathinfo($old_path);
19
    $new_path=$info['dirname']."/".$name.".".$info['extension'];
20
    if(file_exists($new_path)) return false;
21
22
    return copy($old_path,$new_path);
23
    }
24
}
25
26
function rename_file($old_path,$name,$transliteration){
27
    $name=fix_filename($name,$transliteration);
28 View Code Duplication
    if(file_exists($old_path)){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
29
    $info=pathinfo($old_path);
30
    $new_path=$info['dirname']."/".$name.".".$info['extension'];
31
    if(file_exists($new_path)) return false;
32
33
    return rename($old_path,$new_path);
34
    }
35
}
36
37
function rename_folder($old_path,$name,$transliteration){
38
    $name=fix_filename($name,$transliteration);
39
    if(file_exists($old_path)){
40
    $new_path=fix_dirname($old_path)."/".$name;
41
    if(file_exists($new_path)) return false;
42
43
    return rename($old_path,$new_path);
44
    }
45
}
46
47 View Code Duplication
function create_img_gd($imgfile, $imgthumb, $newwidth, $newheight="") {
0 ignored issues
show
Duplication introduced by
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...
48
    if(image_check_memory_usage($imgfile,$newwidth,$newheight)){
49
    require_once('php_image_magician.php');
50
    $magicianObj = new imageLib($imgfile);
51
    $magicianObj -> resizeImage($newwidth, $newheight, 'crop');
52
    $magicianObj -> saveImage($imgthumb,80);
53
54
    return true;
55
    }
56
57
    return false;
58
}
59
60 View Code Duplication
function create_img($imgfile, $imgthumb, $newwidth, $newheight="") {
0 ignored issues
show
Duplication introduced by
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...
61
    if(image_check_memory_usage($imgfile,$newwidth,$newheight)){
62
    require_once('php_image_magician.php');
63
    $magicianObj = new imageLib($imgfile);
64
    $magicianObj -> resizeImage($newwidth, $newheight, 'auto');
65
    $magicianObj -> saveImage($imgthumb,80);
66
67
    return true;
68
    }else{
69
    return false;
70
    }
71
}
72
73
function makeSize($size) {
74
   $units = array('B','KB','MB','GB','TB');
75
   $u = 0;
76
   while ( (round($size / 1024) > 0) && ($u < 4) ) {
77
     $size = $size / 1024;
78
     ++$u;
79
   }
80
81
   return (number_format($size, 0) . " " . $units[$u]);
82
}
83
84
function foldersize($path) {
85
    $total_size = 0;
86
    $files = scandir($path);
87
    $cleanPath = rtrim($path, '/'). '/';
88
89
    foreach($files as $t) {
90
        if ($t<>"." && $t<>"..") {
91
            $currentFile = $cleanPath . $t;
92
            if (is_dir($currentFile)) {
93
                $size = foldersize($currentFile);
94
                $total_size += $size;
95
            }
96
            else {
97
                $size = filesize($currentFile);
98
                $total_size += $size;
99
            }
100
        }
101
    }
102
103
    return $total_size;
104
}
105
106
function create_folder($path=false,$path_thumbs=false){
107
    $oldumask = umask(0);
108
    if ($path && !file_exists($path))
109
        mkdir($path, 0777, true); // or even 01777 so you get the sticky bit set
110
    if($path_thumbs && !file_exists($path_thumbs))
111
        mkdir($path_thumbs, 0777, true) or die("$path_thumbs cannot be found"); // or even 01777 so you get the sticky bit set
0 ignored issues
show
Coding Style Compatibility introduced by
The function create_folder() contains an exit expression.

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

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

Loading history...
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
112
    umask($oldumask);
113
}
114
115
function check_files_extensions_on_path($path,$ext){
116
    if(!is_dir($path)){
117
    $fileinfo = pathinfo($path);
118
    if(!in_array(mb_strtolower($fileinfo['extension']),$ext))
119
        unlink($path);
120
    }else{
121
    $files = scandir($path);
122
    foreach($files as $file){
123
        check_files_extensions_on_path(trim($path,'/')."/".$file,$ext);
124
    }
125
    }
126
}
127
128
function check_files_extensions_on_phar( $phar, &$files, $basepath, $ext ) {
129
    foreach( $phar as $file )
130
    {
131
        if( $file->isFile() )
132
        {
133
            if(in_array(mb_strtolower($file->getExtension()),$ext))
134
            {
135
                $files[] = $basepath.$file->getFileName( );
136
            }
137
        }
138
        else if( $file->isDir() )
139
        {
140
            $iterator = new DirectoryIterator( $file );
141
            check_files_extensions_on_phar($iterator, $files, $basepath.$file->getFileName().'/', $ext);
142
        }
143
    }
144
}
145
146
function fix_filename($str,$transliteration){
147
    if($transliteration){
148
    if( function_exists( 'transliterator_transliterate' ) )
149
    {
150
       $str = transliterator_transliterate( 'Accents-Any', $str );
151
    }
152
    else
153
    {
154
       $str = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $str);
155
    }
156
157
    $str = preg_replace( "/[^a-zA-Z0-9\.\[\]_| -]/", '', $str );
158
    }
159
    // Empty or incorrectly transliterated filename.
160
    // Here is a point: a good file UNKNOWN_LANGUAGE.jpg could become .jpg in previous code.
161
    // So we add that default 'file' name to fix that issue.
162
    if( strpos( $str, '.' ) === 0 )
163
    {
164
       $str = 'file'.$str;
165
    }
166
167
    return trim( $str );
168
}
169
170
function fix_dirname($str){
171
    return str_replace('~',' ',dirname(str_replace(' ','~',$str)));
172
}
173
174
function fix_strtoupper($str){
175
    if( function_exists( 'mb_strtoupper' ) )
176
    return mb_strtoupper($str);
177
    else
178
    return strtoupper($str);
179
}
180
181
function fix_strtolower($str){
182
    if( function_exists( 'mb_strtoupper' ) )
183
    return mb_strtolower($str);
184
    else
185
    return strtolower($str);
186
}
187
188
function fix_path($path,$transliteration){
189
    $info=pathinfo($path);
190
    $tmp_path=$info['dirname'];
191
    $str=fix_filename($info['filename'],$transliteration);
192
    if($tmp_path!="")
193
    return $tmp_path.DIRECTORY_SEPARATOR.$str;
194
    else
195
    return $str;
196
}
197
198
function base_url(){
0 ignored issues
show
Coding Style introduced by
base_url uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
199
  return sprintf(
200
    "%s://%s",
201
    isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http',
202
    $_SERVER['HTTP_HOST']
203
  );
204
}
205
206
function config_loading($current_path,$fld){
207
    if(file_exists($current_path.$fld.".config")){
208
    require_once($current_path.$fld.".config");
209
210
    return true;
211
    }
212
    echo "!!!!".$parent=fix_dirname($fld);
213
    if($parent!="." && !empty($parent)){
214
    config_loading($current_path,$parent);
215
    }
216
217
    return false;
218
}
219
220
function image_check_memory_usage($img, $max_breedte, $max_hoogte){
221
    if(file_exists($img)){
222
    $K64 = 65536;    // number of bytes in 64K
223
    $memory_usage = memory_get_usage();
224
    $memory_limit = abs(intval(str_replace('M','',ini_get('memory_limit'))*1024*1024));
225
    $image_properties = getimagesize($img);
226
    $image_width = $image_properties[0];
227
    $image_height = $image_properties[1];
228
    $image_bits = $image_properties['bits'];
229
    $image_memory_usage = $K64 + ($image_width * $image_height * ($image_bits )  * 2);
230
    $thumb_memory_usage = $K64 + ($max_breedte * $max_hoogte * ($image_bits ) * 2);
231
    $memory_needed = intval($memory_usage + $image_memory_usage + $thumb_memory_usage);
232
233
        if($memory_needed > $memory_limit){
234
                ini_set('memory_limit',(intval($memory_needed/1024/1024)+5) . 'M');
235
                if(ini_get('memory_limit') == (intval($memory_needed/1024/1024)+5) . 'M'){
236
                return true;
237
            }else{
238
                return false;
239
            }
240
        }else{
241
            return true;
242
        }
243
        }else{
244
        return false;
245
    }
246
}
247
248
function endsWith($haystack, $needle)
249
{
250
    return $needle === "" || substr($haystack, -strlen($needle)) === $needle;
251
}
252
253
function new_thumbnails_creation($targetPath,$targetFile,$name,$current_path,$relative_image_creation,$relative_path_from_current_pos,$relative_image_creation_name_to_prepend,$relative_image_creation_name_to_append,$relative_image_creation_width,$relative_image_creation_height,$fixed_image_creation,$fixed_path_from_filemanager,$fixed_image_creation_name_to_prepend,$fixed_image_creation_to_append,$fixed_image_creation_width,$fixed_image_creation_height){
254
    //create relative thumbs
255
    $all_ok=true;
256
    if($relative_image_creation){
257
    foreach($relative_path_from_current_pos as $k=>$path){
258 View Code Duplication
        if($path!="" && $path[strlen($path)-1]!="/") $path.="/";
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
259
        if (!file_exists($targetPath.$path)) create_folder($targetPath.$path,false);
0 ignored issues
show
Documentation introduced by
$targetPath . $path is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
260
        $info=pathinfo($name);
261 View Code Duplication
        if(!endsWith($targetPath,$path))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
262
        if(!create_img($targetFile, $targetPath.$path.$relative_image_creation_name_to_prepend[$k].$info['filename'].$relative_image_creation_name_to_append[$k].".".$info['extension'], $relative_image_creation_width[$k], $relative_image_creation_height[$k]))
263
            $all_ok=false;
264
    }
265
    }
266
267
    //create fixed thumbs
268
    if($fixed_image_creation){
269
    foreach($fixed_path_from_filemanager as $k=>$path){
270 View Code Duplication
        if($path!="" && $path[strlen($path)-1]!="/") $path.="/";
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
271
        $base_dir=$path.substr_replace($targetPath, '', 0, strlen($current_path));
272
        if (!file_exists($base_dir)) create_folder($base_dir,false);
0 ignored issues
show
Documentation introduced by
$base_dir is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
273
        $info=pathinfo($name);
274 View Code Duplication
        if(!create_img($targetFile, $base_dir.$fixed_image_creation_name_to_prepend[$k].$info['filename'].$fixed_image_creation_to_append[$k].".".$info['extension'], $fixed_image_creation_width[$k], $fixed_image_creation_height[$k]))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
275
        $all_ok=false;
276
    }
277
    }
278
279
    return $all_ok;
280
}
281