Completed
Pull Request — develop (#716)
by Agel_Nash
06:45 queued 44s
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
     * @TODO: not work if $string contains folder name with dot
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
212
     */
213
    function getExtension($string)
214
    {
215
        $pos = explode($string, '.');
216
        if ($pos !== false) {
217
            $ext = substr($string, $pos);
218
            $ext = strtolower($ext);
219
        } else {
220
            $ext = false;
221
        }
222
223
        return $ext;
224
    }
225
}
226
227
if(!function_exists('checkExtension')) {
228
    /**
229
     * @param string $path
230
     * @return bool
231
     */
232
    function checkExtension($path = '')
233
    {
234
        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...
235
236
        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...
237
            return true;
238
        } else {
239
            return false;
240
        }
241
    }
242
}
243
244
if(!function_exists('mkdirs')) {
245
    /**
246
     * recursive mkdir function
247
     *
248
     * @param string $strPath
249
     * @param int $mode
250
     * @return bool
251
     */
252
    function mkdirs($strPath, $mode)
253
    {
254
        if (is_dir($strPath)) {
255
            return true;
256
        }
257
        $pStrPath = dirname($strPath);
258
        if (!mkdirs($pStrPath, $mode)) {
259
            return false;
260
        }
261
262
        return @mkdir($strPath);
263
    }
264
}
265
266
if(!function_exists('logFileChange')) {
267
    /**
268
     * @param string $type
269
     * @param string $filename
270
     */
271
    function logFileChange($type, $filename)
272
    {
273
        //global $_lang;
274
275
        $log = new EvolutionCMS\Legacy\LogHandler();
276
277
        switch ($type) {
278
            case 'upload':
279
                $string = 'Uploaded File';
280
                break;
281
            case 'delete':
282
                $string = 'Deleted File';
283
                break;
284
            case 'modify':
285
                $string = 'Modified File';
286
                break;
287
            default:
288
                $string = 'Viewing File';
289
                break;
290
        }
291
292
        $string = sprintf($string, $filename);
293
        $log->initAndWriteLog($string, '', '', '', $type, $filename);
294
295
        // HACK: change the global action to prevent double logging
296
        // @see index.php @ 915
297
        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...
298
        $action = 1;
299
    }
300
}
301
302
if(!function_exists('unzip')) {
303
    /**
304
     * by patrick_allaert - php user notes
305
     *
306
     * @param string $file
307
     * @param string $path
308
     * @return bool|int
309
     */
310
    function unzip($file, $path)
311
    {
312
        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...
313
314
        if (!$token_check) {
315
            return false;
316
        }
317
318
        // added by Raymond
319
        if (!extension_loaded('zip')) {
320
            return 0;
321
        }
322
        // end mod
323
        $zip = zip_open($file);
324
        if ($zip) {
325
            $old_umask = umask(0);
326
            $path = rtrim($path, '/') . '/';
327
            while ($zip_entry = zip_read($zip)) {
328
                if (zip_entry_filesize($zip_entry) > 0) {
329
                    // str_replace must be used under windows to convert "/" into "\"
330
                    $zip_entry_name = zip_entry_name($zip_entry);
331
                    $complete_path = $path . str_replace('\\', '/', dirname($zip_entry_name));
332
                    $complete_name = $path . str_replace('\\', '/', $zip_entry_name);
333
                    if (!file_exists($complete_path)) {
334
                        $tmp = '';
335
                        foreach (explode('/', $complete_path) AS $k) {
336
                            $tmp .= $k . '/';
337
                            if (!is_dir($tmp)) {
338
                                mkdir($tmp, 0777);
339
                            }
340
                        }
341
                    }
342
                    if (zip_entry_open($zip, $zip_entry, 'r')) {
343
                        file_put_contents($complete_name, zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)));
344
                        zip_entry_close($zip_entry);
345
                    }
346
                }
347
            }
348
            umask($old_umask);
349
            zip_close($zip);
350
351
            return true;
352
        }
353
        zip_close($zip);
354
    }
355
}
356
357
if(!function_exists('rrmdir')) {
358
    /**
359
     * @param string $dir
360
     * @return bool
361
     */
362
    function rrmdir($dir)
363
    {
364
        foreach (glob($dir . '/*') as $file) {
365
            if (is_dir($file)) {
366
                rrmdir($file);
367
            } else {
368
                unlink($file);
369
            }
370
        }
371
372
        return rmdir($dir);
373
    }
374
}
375
376
if(!function_exists('fileupload')) {
377
    /**
378
     * @return string
379
     */
380
    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...
381
    {
382
        $modx = evolutionCMS();
383
        global $_lang, $startpath, $filemanager_path, $uploadablefiles, $new_file_permissions;
384
        $msg = '';
385
        foreach ($_FILES['userfile']['name'] as $i => $name) {
386
            if (empty($_FILES['userfile']['tmp_name'][$i])) {
387
                continue;
388
            }
389
            $userfile = array();
390
391
            $userfile['tmp_name'] = $_FILES['userfile']['tmp_name'][$i];
392
            $userfile['error'] = $_FILES['userfile']['error'][$i];
393
            $name = $_FILES['userfile']['name'][$i];
394
            if ($modx->config['clean_uploaded_filename'] == 1) {
395
                $nameparts = explode('.', $name);
396
                $nameparts = array_map(array(
397
                    $modx,
398
                    'stripAlias'
399
                ), $nameparts, array('file_manager'));
400
                $name = implode('.', $nameparts);
401
            }
402
            $userfile['name'] = $name;
403
            $userfile['type'] = $_FILES['userfile']['type'][$i];
404
405
            // this seems to be an upload action.
406
            $path = $modx->config['site_url'] . substr($startpath, strlen($filemanager_path), strlen($startpath));
407
            $path = rtrim($path, '/') . '/' . $userfile['name'];
408
            $msg .= $path;
409
            if ($userfile['error'] == 0) {
410
                $img = (strpos($userfile['type'],
411
                        'image') !== false) ? '<br /><img src="' . $path . '" height="75" />' : '';
412
                $msg .= "<p>" . $_lang['files_file_type'] . $userfile['type'] . ", " . $modx->nicesize(filesize($userfile['tmp_name'])) . $img . '</p>';
413
            }
414
415
            $userfilename = $userfile['tmp_name'];
416
417
            if (is_uploaded_file($userfilename)) {
418
                // file is uploaded file, process it!
419
                if (!checkExtension($userfile['name'])) {
420
                    $msg .= '<p><span class="warning">' . $_lang['files_filetype_notok'] . '</span></p>';
421
                } else {
422
                    if (@move_uploaded_file($userfile['tmp_name'], $_POST['path'] . '/' . $userfile['name'])) {
423
                        // Ryan: Repair broken permissions issue with file manager
424
                        if (strtoupper(substr(PHP_OS, 0, 3)) != 'WIN') {
425
                            @chmod($_POST['path'] . "/" . $userfile['name'], $new_file_permissions);
426
                        }
427
                        // Ryan: End
428
                        $msg .= '<p><span class="success">' . $_lang['files_upload_ok'] . '</span></p><hr/>';
429
430
                        // invoke OnFileManagerUpload event
431
                        $modx->invokeEvent('OnFileManagerUpload', array(
432
                            'filepath' => $_POST['path'],
433
                            'filename' => $userfile['name']
434
                        ));
435
                        // Log the change
436
                        logFileChange('upload', $_POST['path'] . '/' . $userfile['name']);
437
                    } else {
438
                        $msg .= '<p><span class="warning">' . $_lang['files_upload_copyfailed'] . '</span> ' . $_lang["files_upload_permissions_error"] . '</p>';
439
                    }
440
                }
441
            } else {
442
                $msg .= '<br /><span class="warning"><b>' . $_lang['files_upload_error'] . ':</b>';
443
                switch ($userfile['error']) {
444
                    case 0: //no error; possible file attack!
445
                        $msg .= $_lang['files_upload_error0'];
446
                        break;
447
                    case 1: //uploaded file exceeds the upload_max_filesize directive in php.ini
448
                        $msg .= $_lang['files_upload_error1'];
449
                        break;
450
                    case 2: //uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the html form
451
                        $msg .= $_lang['files_upload_error2'];
452
                        break;
453
                    case 3: //uploaded file was only partially uploaded
454
                        $msg .= $_lang['files_upload_error3'];
455
                        break;
456
                    case 4: //no file was uploaded
457
                        $msg .= $_lang['files_upload_error4'];
458
                        break;
459
                    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...
460
                        $msg .= $_lang['files_upload_error5'];
461
                        break;
462
                }
463
                $msg .= '</span><br />';
464
            }
465
        }
466
467
        return $msg . '<br/>';
468
    }
469
}
470
471
if(!function_exists('textsave')) {
472
    /**
473
     * @return string
474
     */
475
    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...
476
    {
477
        global $_lang;
478
479
        $msg = $_lang['editing_file'];
480
        $filename = $_POST['path'];
481
        $content = $_POST['content'];
482
483
        // Write $content to our opened file.
484
        if (file_put_contents($filename, $content) === false) {
485
            $msg .= '<span class="warning"><b>' . $_lang['file_not_saved'] . '</b></span><br /><br />';
486
        } else {
487
            $msg .= '<span class="success"><b>' . $_lang['file_saved'] . '</b></span><br /><br />';
488
            $_REQUEST['mode'] = 'edit';
489
        }
490
        // Log the change
491
        logFileChange('modify', $filename);
492
493
        return $msg;
494
    }
495
}
496
497
if(!function_exists('delete_file')) {
498
    /**
499
     * @return string
500
     */
501
    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...
502
    {
503
        global $_lang, $token_check;
504
505
        $msg = sprintf($_lang['deleting_file'], str_replace('\\', '/', $_REQUEST['path']));
506
507
        $file = $_REQUEST['path'];
508
        if (!$token_check || !@unlink($file)) {
509
            $msg .= '<span class="warning"><b>' . $_lang['file_not_deleted'] . '</b></span><br /><br />';
510
        } else {
511
            $msg .= '<span class="success"><b>' . $_lang['file_deleted'] . '</b></span><br /><br />';
512
        }
513
514
        // Log the change
515
        logFileChange('delete', $file);
516
517
        return $msg;
518
    }
519
}
520
521 View Code Duplication
if(!function_exists('parsePlaceholder')) {
522
    /**
523
     * @param string $tpl
524
     * @param array $ph
525
     * @return string
526
     */
527
    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...
528
    {
529
        foreach ($ph as $k => $v) {
530
            $k = "[+{$k}+]";
531
            $tpl = str_replace($k, $v, $tpl);
532
        }
533
534
        return $tpl;
535
    }
536
}
537
538
if(!function_exists('checkToken')) {
539
    /**
540
     * @return bool
541
     */
542
    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...
543
    {
544
        if (isset($_POST['token']) && !empty($_POST['token'])) {
545
            $token = $_POST['token'];
546
        } elseif (isset($_GET['token']) && !empty($_GET['token'])) {
547
            $token = $_GET['token'];
548
        } else {
549
            $token = false;
550
        }
551
552
        if (isset($_SESSION['token']) && !empty($_SESSION['token']) && $_SESSION['token'] === $token) {
553
            $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...
554
        } else {
555
            $rs = false;
556
        }
557
        $_SESSION['token'] = '';
558
559
        return $rs;
560
    }
561
}
562
563
if(!function_exists('makeToken')) {
564
    /**
565
     * @return string
566
     */
567
    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...
568
    {
569
        $newToken = uniqid('');
570
        $_SESSION['token'] = $newToken;
571
572
        return $newToken;
573
    }
574
}
575