Completed
Pull Request — develop (#716)
by Agel_Nash
06:26
created

files.php ➔ ls()   F

Complexity

Conditions 36
Paths > 20000

Size

Total Lines 117
Code Lines 90

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 36
eloc 90
nc 221229
nop 1
dl 0
loc 117
rs 2
c 0
b 0
f 0

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
if(!function_exists('add_dot')) {
3
    /**
4
     * @param array $array
5
     * @return array
6
     */
7
    function add_dot($array)
0 ignored issues
show
Coding Style introduced by
As per coding-style, this function should be in camelCase.

CamelCase (...) is the practice of writing compound words or phrases such that
each word or abbreviation begins with a capital letter.

Learn more about camelCase.

Loading history...
8
    {
9
        $count = count($array);
10
        for ($i = 0; $i < $count; $i++) {
11
            $array[$i] = '.' . strtolower(trim($array[$i])); // add a dot :)
12
        }
13
14
        return $array;
15
    }
16
}
17
18 View Code Duplication
if(!function_exists('determineIcon')) {
19
    /**
20
     * @param string $file
21
     * @param string $selFile
22
     * @param string $mode
23
     * @return string
24
     */
25
    function determineIcon($file, $selFile, $mode)
26
    {
27
        $icons = array(
28
            'default' => 'fa fa-file-o',
29
            'edit'    => 'fa fa-pencil-square-o',
30
            'view'    => 'fa fa-eye'
31
        );
32
        $icon = $icons['default'];
33
        if ($file == $selFile) {
34
            $icon = isset($icons[$mode]) ? $icons[$mode] : $icons['default'];
35
        }
36
37
        return '<i class="' . $icon . ' FilesPage"></i>';
38
    }
39
}
40
41 View Code Duplication
if(!function_exists('markRow')) {
42
    /**
43
     * @param string $file
44
     * @param string $selFile
45
     * @param string $mode
46
     * @return string
47
     */
48
    function markRow($file, $selFile, $mode)
49
    {
50
        $classNames = array(
51
            'default' => '',
52
            'edit'    => 'editRow',
53
            'view'    => 'viewRow'
54
        );
55
        if ($file == $selFile) {
56
            $class = isset($classNames[$mode]) ? $classNames[$mode] : $classNames['default'];
57
58
            return ' class="' . $class . '"';
59
        }
60
61
        return '';
62
    }
63
}
64
65
if(!function_exists('ls')) {
66
    /**
67
     * @param string $curpath
68
     */
69
    function ls($curpath)
0 ignored issues
show
Coding Style introduced by
This method's name is shorter than the configured minimum length of 3 characters.

Even though PHP does not care about the name of your methods, it is generally a good practice to choose method names which can be easily understood by other human readers.

Loading history...
Coding Style introduced by
ls uses the super-global variable $_REQUEST 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...
70
    {
71
        global $_lang, $theme_image_path, $_style;
72
        global $excludes, $protected_path, $editablefiles, $inlineviewablefiles, $viewablefiles, $enablefileunzip, $enablefiledownload, $uploadablefiles, $folders, $files, $filesizes, $len, $dirs_array, $files_array, $webstart_path, $modx;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
73
        $dircounter = 0;
74
        $filecounter = 0;
75
        $curpath = str_replace('//', '/', $curpath . '/');
76
77
        if (!is_dir($curpath)) {
78
            echo 'Invalid path "', $curpath, '"<br />';
79
80
            return;
81
        }
82
        $dir = scandir($curpath);
83
84
        // first, get info
85
        foreach ($dir as $file) {
86
            $newpath = $curpath . $file;
87
            if ($file === '..' || $file === '.') {
88
                continue;
89
            }
90
            if (is_dir($newpath)) {
91
                $dirs_array[$dircounter]['dir'] = $newpath;
92
                $dirs_array[$dircounter]['stats'] = lstat($newpath);
93
                if ($file === '..' || $file === '.') {
94
                    continue;
95
                } elseif (!in_array($file, $excludes) && !in_array($newpath, $protected_path)) {
96
                    $dirs_array[$dircounter]['text'] = '<i class="' . $_style['files_folder'] . ' FilesFolder"></i> <a href="index.php?a=31&mode=drill&path=' . urlencode($newpath) . '"><b>' . $file . '</b></a>';
97
98
                    $dfiles = scandir($newpath);
99
                    foreach ($dfiles as $i => $infile) {
100
                        switch ($infile) {
101
                            case '..':
102
                            case '.':
103
                                unset($dfiles[$i]);
104
                                break;
105
                        }
106
                    }
107
                    $file_exists = (0 < count($dfiles)) ? 'file_exists' : '';
108
109
                    $dirs_array[$dircounter]['delete'] = is_writable($curpath) ? '<a href="javascript: deleteFolder(\'' . urlencode($file) . '\',\'' . $file_exists . '\');"><i class="' . $_style['files_delete'] . '" title="' . $_lang['file_delete_folder'] . '"></i></a>' : '';
110
                } else {
111
                    $dirs_array[$dircounter]['text'] = '<span><i class="' . $_style['files_deleted_folder'] . ' FilesDeletedFolder"></i> ' . $file . '</span>';
112
                    $dirs_array[$dircounter]['delete'] = is_writable($curpath) ? '<span class="disabled"><i class="' . $_style['files_delete'] . '" title="' . $_lang['file_delete_folder'] . '"></i></span>' : '';
113
                }
114
115
                $dirs_array[$dircounter]['rename'] = is_writable($curpath) ? '<a href="javascript:renameFolder(\'' . urlencode($file) . '\');"><i class="' . $_style['files_rename'] . '" title="' . $_lang['rename'] . '"></i></a> ' : '';
116
117
                // increment the counter
118
                $dircounter++;
119
            } else {
120
                $type = getExtension($newpath);
121
                $files_array[$filecounter]['file'] = $newpath;
122
                $files_array[$filecounter]['stats'] = lstat($newpath);
123
                $files_array[$filecounter]['text'] = determineIcon($newpath, $_REQUEST['path'],
124
                        $_REQUEST['mode']) . ' ' . $file;
125
                $files_array[$filecounter]['view'] = (in_array($type,
126
                    $viewablefiles)) ? '<a href="javascript:;" onclick="viewfile(\'' . $webstart_path . substr($newpath,
127
                        $len,
128
                        strlen($newpath)) . '\');"><i class="' . $_style['files_view'] . '" title="' . $_lang['files_viewfile'] . '"></i></a>' : (($enablefiledownload && in_array($type,
129
                        $uploadablefiles)) ? '<a href="' . $webstart_path . implode('/', array_map('rawurlencode',
130
                        explode('/', substr($newpath, $len,
131
                            strlen($newpath))))) . '" style="cursor:pointer;"><i class="' . $_style['files_download'] . '" title="' . $_lang['file_download_file'] . '"></i></a>' : '<span class="disabled"><i class="' . $_style['files_view'] . '" title="' . $_lang['files_viewfile'] . '"></i></span>');
132
                $files_array[$filecounter]['view'] = (in_array($type,
133
                    $inlineviewablefiles)) ? '<a href="index.php?a=31&mode=view&path=' . urlencode($newpath) . '"><i class="' . $_style['files_view'] . '" title="' . $_lang['files_viewfile'] . '"></i></a>' : $files_array[$filecounter]['view'];
134
                $files_array[$filecounter]['unzip'] = ($enablefileunzip && $type == '.zip') ? '<a href="javascript:unzipFile(\'' . urlencode($file) . '\');"><i class="' . $_style['files_unzip'] . '" title="' . $_lang['file_download_unzip'] . '"></i></a>' : '';
135
                $files_array[$filecounter]['edit'] = (in_array($type,
136
                        $editablefiles) && is_writable($curpath) && is_writable($newpath)) ? '<a href="index.php?a=31&mode=edit&path=' . urlencode($newpath) . '#file_editfile"><i class="' . $_style['files_edit'] . '" title="' . $_lang['files_editfile'] . '"></i></a>' : '<span class="disabled"><i class="' . $_style['files_edit'] . '" title="' . $_lang['files_editfile'] . '"></i></span>';
137
                $files_array[$filecounter]['duplicate'] = (in_array($type,
138
                        $editablefiles) && is_writable($curpath) && is_writable($newpath)) ? '<a href="javascript:duplicateFile(\'' . urlencode($file) . '\');"><i class="' . $_style['files_duplicate'] . '" title="' . $_lang['duplicate'] . '"></i></a>' : '<span class="disabled"><i class="' . $_style['files_duplicate'] . '" align="absmiddle" title="' . $_lang['duplicate'] . '"></i></span>';
139
                $files_array[$filecounter]['rename'] = (in_array($type,
140
                        $editablefiles) && is_writable($curpath) && is_writable($newpath)) ? '<a href="javascript:renameFile(\'' . urlencode($file) . '\');"><i class="' . $_style['files_rename'] . '" align="absmiddle" title="' . $_lang['rename'] . '"></i></a>' : '<span class="disabled"><i class="' . $_style['files_rename'] . '" align="absmiddle" title="' . $_lang['rename'] . '"></i></span>';
141
                $files_array[$filecounter]['delete'] = is_writable($curpath) && is_writable($newpath) ? '<a href="javascript:deleteFile(\'' . urlencode($file) . '\');"><i class="' . $_style['files_delete'] . '" title="' . $_lang['file_delete_file'] . '"></i></a>' : '<span class="disabled"><i class="' . $_style['files_delete'] . '" title="' . $_lang['file_delete_file'] . '"></i></span>';
142
143
                // increment the counter
144
                $filecounter++;
145
            }
146
        }
147
148
        // dump array entries for directories
149
        $folders = count($dirs_array);
150
        sort($dirs_array); // sorting the array alphabetically (Thanks pxl8r!)
151
        for ($i = 0; $i < $folders; $i++) {
152
            $filesizes += $dirs_array[$i]['stats']['7'];
153
            echo '<tr>';
154
            echo '<td>' . $dirs_array[$i]['text'] . '</td>';
155
            echo '<td class="text-nowrap">' . $modx->toDateFormat($dirs_array[$i]['stats']['9']) . '</td>';
156
            echo '<td class="text-right">' . $modx->nicesize($dirs_array[$i]['stats']['7']) . '</td>';
157
            echo '<td class="actions text-right">';
158
            echo $dirs_array[$i]['rename'];
159
            echo $dirs_array[$i]['delete'];
160
            echo '</td>';
161
            echo '</tr>';
162
        }
163
164
        // dump array entries for files
165
        $files = count($files_array);
166
        sort($files_array); // sorting the array alphabetically (Thanks pxl8r!)
167
        for ($i = 0; $i < $files; $i++) {
168
            $filesizes += $files_array[$i]['stats']['7'];
169
            echo '<tr ' . markRow($files_array[$i]['file'], $_REQUEST['path'], $_REQUEST['mode']) . '>';
170
            echo '<td>' . $files_array[$i]['text'] . '</td>';
171
            echo '<td class="text-nowrap">' . $modx->toDateFormat($files_array[$i]['stats']['9']) . '</td>';
172
            echo '<td class="text-right">' . $modx->nicesize($files_array[$i]['stats']['7']) . '</td>';
173
            echo '<td class="actions text-right">';
174
            echo $files_array[$i]['unzip'];
175
            echo $files_array[$i]['view'];
176
            echo $files_array[$i]['edit'];
177
            echo $files_array[$i]['duplicate'];
178
            echo $files_array[$i]['rename'];
179
            echo $files_array[$i]['delete'];
180
            echo '</td>';
181
            echo '</tr>';
182
        }
183
184
        return;
185
    }
186
}
187
188 View Code Duplication
if(!function_exists('removeLastPath')) {
189
    /**
190
     * @param string $string
191
     * @return bool|string
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string|false.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
192
     */
193
    function removeLastPath($string)
194
    {
195
        $pos = strrpos($string, '/');
196
        if ($pos !== false) {
197
            $path = substr($string, 0, $pos);
198
        } else {
199
            $path = false;
200
        }
201
202
        return $path;
203
    }
204
}
205
206 View Code Duplication
if(!function_exists('getExtension')) {
207
    /**
208
     * @param string $string
209
     * @return bool|string
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string|false.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
210
     */
211
    function getExtension($string)
212
    {
213
        $pos = strrpos($string, '.');
214
        if ($pos !== false) {
215
            $ext = substr($string, $pos);
216
            $ext = strtolower($ext);
217
        } else {
218
            $ext = false;
219
        }
220
221
        return $ext;
222
    }
223
}
224
225
if(!function_exists('checkExtension')) {
226
    /**
227
     * @param string $path
228
     * @return bool
229
     */
230
    function checkExtension($path = '')
231
    {
232
        global $uploadablefiles;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
233
234
        if (in_array(getExtension($path), $uploadablefiles)) {
0 ignored issues
show
Coding Style introduced by
The if-else statement can be simplified to return in_array(getExten...th), $uploadablefiles);.
Loading history...
235
            return true;
236
        } else {
237
            return false;
238
        }
239
    }
240
}
241
242
if(!function_exists('mkdirs')) {
243
    /**
244
     * recursive mkdir function
245
     *
246
     * @param string $strPath
247
     * @param int $mode
248
     * @return bool
249
     */
250
    function mkdirs($strPath, $mode)
251
    {
252
        if (is_dir($strPath)) {
253
            return true;
254
        }
255
        $pStrPath = dirname($strPath);
256
        if (!mkdirs($pStrPath, $mode)) {
257
            return false;
258
        }
259
260
        return @mkdir($strPath);
261
    }
262
}
263
264
if(!function_exists('logFileChange')) {
265
    /**
266
     * @param string $type
267
     * @param string $filename
268
     */
269
    function logFileChange($type, $filename)
270
    {
271
        //global $_lang;
272
273
        $log = new EvolutionCMS\Legacy\LogHandler();
274
275
        switch ($type) {
276
            case 'upload':
277
                $string = 'Uploaded File';
278
                break;
279
            case 'delete':
280
                $string = 'Deleted File';
281
                break;
282
            case 'modify':
283
                $string = 'Modified File';
284
                break;
285
            default:
286
                $string = 'Viewing File';
287
                break;
288
        }
289
290
        $string = sprintf($string, $filename);
291
        $log->initAndWriteLog($string, '', '', '', $type, $filename);
292
293
        // HACK: change the global action to prevent double logging
294
        // @see index.php @ 915
295
        global $action;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
296
        $action = 1;
297
    }
298
}
299
300
if(!function_exists('unzip')) {
301
    /**
302
     * by patrick_allaert - php user notes
303
     *
304
     * @param string $file
305
     * @param string $path
306
     * @return bool|int
307
     */
308
    function unzip($file, $path)
309
    {
310
        global $newfolderaccessmode, $token_check;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
311
312
        if (!$token_check) {
313
            return false;
314
        }
315
316
        // added by Raymond
317
        if (!extension_loaded('zip')) {
318
            return 0;
319
        }
320
        // end mod
321
        $zip = zip_open($file);
322
        if ($zip) {
323
            $old_umask = umask(0);
324
            $path = rtrim($path, '/') . '/';
325
            while ($zip_entry = zip_read($zip)) {
326
                if (zip_entry_filesize($zip_entry) > 0) {
327
                    // str_replace must be used under windows to convert "/" into "\"
328
                    $zip_entry_name = zip_entry_name($zip_entry);
329
                    $complete_path = $path . str_replace('\\', '/', dirname($zip_entry_name));
330
                    $complete_name = $path . str_replace('\\', '/', $zip_entry_name);
331
                    if (!file_exists($complete_path)) {
332
                        $tmp = '';
333
                        foreach (explode('/', $complete_path) AS $k) {
334
                            $tmp .= $k . '/';
335
                            if (!is_dir($tmp)) {
336
                                mkdir($tmp, 0777);
337
                            }
338
                        }
339
                    }
340
                    if (zip_entry_open($zip, $zip_entry, 'r')) {
341
                        file_put_contents($complete_name, zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)));
342
                        zip_entry_close($zip_entry);
343
                    }
344
                }
345
            }
346
            umask($old_umask);
347
            zip_close($zip);
348
349
            return true;
350
        }
351
        zip_close($zip);
352
    }
353
}
354
355
if(!function_exists('rrmdir')) {
356
    /**
357
     * @param string $dir
358
     * @return bool
359
     */
360
    function rrmdir($dir)
361
    {
362
        foreach (glob($dir . '/*') as $file) {
363
            if (is_dir($file)) {
364
                rrmdir($file);
365
            } else {
366
                unlink($file);
367
            }
368
        }
369
370
        return rmdir($dir);
371
    }
372
}
373
374
if(!function_exists('fileupload')) {
375
    /**
376
     * @return string
377
     */
378
    function fileupload()
0 ignored issues
show
Coding Style introduced by
fileupload uses the super-global variable $_FILES 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...
Coding Style introduced by
fileupload uses the super-global variable $_POST 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...
379
    {
380
        $modx = evolutionCMS();
381
        global $_lang, $startpath, $filemanager_path, $uploadablefiles, $new_file_permissions;
382
        $msg = '';
383
        foreach ($_FILES['userfile']['name'] as $i => $name) {
384
            if (empty($_FILES['userfile']['tmp_name'][$i])) {
385
                continue;
386
            }
387
            $userfile = array();
388
389
            $userfile['tmp_name'] = $_FILES['userfile']['tmp_name'][$i];
390
            $userfile['error'] = $_FILES['userfile']['error'][$i];
391
            $name = $_FILES['userfile']['name'][$i];
392
            if ($modx->config['clean_uploaded_filename'] == 1) {
393
                $nameparts = explode('.', $name);
394
                $nameparts = array_map(array(
395
                    $modx,
396
                    'stripAlias'
397
                ), $nameparts, array('file_manager'));
398
                $name = implode('.', $nameparts);
399
            }
400
            $userfile['name'] = $name;
401
            $userfile['type'] = $_FILES['userfile']['type'][$i];
402
403
            // this seems to be an upload action.
404
            $path = $modx->config['site_url'] . substr($startpath, strlen($filemanager_path), strlen($startpath));
405
            $path = rtrim($path, '/') . '/' . $userfile['name'];
406
            $msg .= $path;
407
            if ($userfile['error'] == 0) {
408
                $img = (strpos($userfile['type'],
409
                        'image') !== false) ? '<br /><img src="' . $path . '" height="75" />' : '';
410
                $msg .= "<p>" . $_lang['files_file_type'] . $userfile['type'] . ", " . $modx->nicesize(filesize($userfile['tmp_name'])) . $img . '</p>';
411
            }
412
413
            $userfilename = $userfile['tmp_name'];
414
415
            if (is_uploaded_file($userfilename)) {
416
                // file is uploaded file, process it!
417
                if (!checkExtension($userfile['name'])) {
418
                    $msg .= '<p><span class="warning">' . $_lang['files_filetype_notok'] . '</span></p>';
419
                } else {
420
                    if (@move_uploaded_file($userfile['tmp_name'], $_POST['path'] . '/' . $userfile['name'])) {
421
                        // Ryan: Repair broken permissions issue with file manager
422
                        if (strtoupper(substr(PHP_OS, 0, 3)) != 'WIN') {
423
                            @chmod($_POST['path'] . "/" . $userfile['name'], $new_file_permissions);
424
                        }
425
                        // Ryan: End
426
                        $msg .= '<p><span class="success">' . $_lang['files_upload_ok'] . '</span></p><hr/>';
427
428
                        // invoke OnFileManagerUpload event
429
                        $modx->invokeEvent('OnFileManagerUpload', array(
430
                            'filepath' => $_POST['path'],
431
                            'filename' => $userfile['name']
432
                        ));
433
                        // Log the change
434
                        logFileChange('upload', $_POST['path'] . '/' . $userfile['name']);
435
                    } else {
436
                        $msg .= '<p><span class="warning">' . $_lang['files_upload_copyfailed'] . '</span> ' . $_lang["files_upload_permissions_error"] . '</p>';
437
                    }
438
                }
439
            } else {
440
                $msg .= '<br /><span class="warning"><b>' . $_lang['files_upload_error'] . ':</b>';
441
                switch ($userfile['error']) {
442
                    case 0: //no error; possible file attack!
443
                        $msg .= $_lang['files_upload_error0'];
444
                        break;
445
                    case 1: //uploaded file exceeds the upload_max_filesize directive in php.ini
446
                        $msg .= $_lang['files_upload_error1'];
447
                        break;
448
                    case 2: //uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the html form
449
                        $msg .= $_lang['files_upload_error2'];
450
                        break;
451
                    case 3: //uploaded file was only partially uploaded
452
                        $msg .= $_lang['files_upload_error3'];
453
                        break;
454
                    case 4: //no file was uploaded
455
                        $msg .= $_lang['files_upload_error4'];
456
                        break;
457
                    default: //a default error, just in case!  :)
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

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

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

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

Loading history...
458
                        $msg .= $_lang['files_upload_error5'];
459
                        break;
460
                }
461
                $msg .= '</span><br />';
462
            }
463
        }
464
465
        return $msg . '<br/>';
466
    }
467
}
468
469
if(!function_exists('textsave')) {
470
    /**
471
     * @return string
472
     */
473
    function textsave()
0 ignored issues
show
Coding Style introduced by
textsave uses the super-global variable $_POST 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...
Coding Style introduced by
textsave uses the super-global variable $_REQUEST 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...
474
    {
475
        global $_lang;
476
477
        $msg = $_lang['editing_file'];
478
        $filename = $_POST['path'];
479
        $content = $_POST['content'];
480
481
        // Write $content to our opened file.
482
        if (file_put_contents($filename, $content) === false) {
483
            $msg .= '<span class="warning"><b>' . $_lang['file_not_saved'] . '</b></span><br /><br />';
484
        } else {
485
            $msg .= '<span class="success"><b>' . $_lang['file_saved'] . '</b></span><br /><br />';
486
            $_REQUEST['mode'] = 'edit';
487
        }
488
        // Log the change
489
        logFileChange('modify', $filename);
490
491
        return $msg;
492
    }
493
}
494
495
if(!function_exists('delete_file')) {
496
    /**
497
     * @return string
498
     */
499
    function delete_file()
0 ignored issues
show
Coding Style introduced by
delete_file uses the super-global variable $_REQUEST 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...
Coding Style introduced by
As per coding-style, this function should be in camelCase.

CamelCase (...) is the practice of writing compound words or phrases such that
each word or abbreviation begins with a capital letter.

Learn more about camelCase.

Loading history...
500
    {
501
        global $_lang, $token_check;
502
503
        $msg = sprintf($_lang['deleting_file'], str_replace('\\', '/', $_REQUEST['path']));
504
505
        $file = $_REQUEST['path'];
506
        if (!$token_check || !@unlink($file)) {
507
            $msg .= '<span class="warning"><b>' . $_lang['file_not_deleted'] . '</b></span><br /><br />';
508
        } else {
509
            $msg .= '<span class="success"><b>' . $_lang['file_deleted'] . '</b></span><br /><br />';
510
        }
511
512
        // Log the change
513
        logFileChange('delete', $file);
514
515
        return $msg;
516
    }
517
}
518
519 View Code Duplication
if(!function_exists('parsePlaceholder')) {
520
    /**
521
     * @param string $tpl
522
     * @param array $ph
523
     * @return string
524
     */
525
    function parsePlaceholder($tpl, $ph)
0 ignored issues
show
Best Practice introduced by
The function parsePlaceholder() has been defined more than once; this definition is ignored, only the first definition in manager/includes/functions/actions/bkmanager.php (L131-143) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
Comprehensibility introduced by
Avoid variables with short names like $ph. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
526
    {
527
        foreach ($ph as $k => $v) {
528
            $k = "[+{$k}+]";
529
            $tpl = str_replace($k, $v, $tpl);
530
        }
531
532
        return $tpl;
533
    }
534
}
535
536
if(!function_exists('checkToken')) {
537
    /**
538
     * @return bool
539
     */
540
    function checkToken()
0 ignored issues
show
Coding Style introduced by
checkToken uses the super-global variable $_POST 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...
Coding Style introduced by
checkToken uses the super-global variable $_GET 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...
Coding Style introduced by
checkToken uses the super-global variable $_SESSION 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...
541
    {
542
        if (isset($_POST['token']) && !empty($_POST['token'])) {
543
            $token = $_POST['token'];
544
        } elseif (isset($_GET['token']) && !empty($_GET['token'])) {
545
            $token = $_GET['token'];
546
        } else {
547
            $token = false;
548
        }
549
550
        if (isset($_SESSION['token']) && !empty($_SESSION['token']) && $_SESSION['token'] === $token) {
551
            $rs = true;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $rs. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
552
        } else {
553
            $rs = false;
554
        }
555
        $_SESSION['token'] = '';
556
557
        return $rs;
558
    }
559
}
560
561
if(!function_exists('makeToken')) {
562
    /**
563
     * @return string
564
     */
565
    function makeToken()
0 ignored issues
show
Coding Style introduced by
makeToken uses the super-global variable $_SESSION 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...
566
    {
567
        $newToken = uniqid('');
568
        $_SESSION['token'] = $newToken;
569
570
        return $newToken;
571
    }
572
}
573