Completed
Push — master ( 8ca430...3024c9 )
by Michael
03:12
created

DirectoryChecker::createDirectory()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 3
c 2
b 0
f 0
nc 3
nop 2
dl 0
loc 22
rs 9.2
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 30 and the first side effect is on line 24.

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
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
/**
12
 * WF-Downloads module
13
 *
14
 * @copyright       The XOOPS Project http://sourceforge.net/projects/xoops/
15
 * @license         GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
16
 * @package         wfdownload
17
 * @since           3.23
18
 * @author          Xoops Development Team
19
 * @version         svn:$id$
20
 */
21
22
//defined('XOOPS_ROOT_PATH') || die('XOOPS Root Path not defined');
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% 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...
23
24
require_once dirname(dirname(dirname(__DIR__))) . '/include/cp_header.php';
25
26
/**
27
 * Class DirectoryChecker
28
 * check status of a directory
29
 */
30
class DirectoryChecker
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
31
{
32
    /**
33
     * @param       $path
34
     * @param int   $mode
35
     * @param array $languageConstants
36
     * @param       $redirectFile
37
     *
38
     * @return bool|string
39
     */
40
    public static function getDirectoryStatus($path, $mode = 0777, $languageConstants = array(), $redirectFile)
0 ignored issues
show
Coding Style introduced by
getDirectoryStatus 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...
41
    {
42
        global $pathIcon16;
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...
43
44
        $languageConstants1 = array($languageConstants[5], $languageConstants[6]);
45
        $languageConstants2 = array($languageConstants[7], $languageConstants[8]);
46
47
        $myWords1 = urlencode(json_encode($languageConstants1));
48
        $myWords2 = urlencode(json_encode($languageConstants2));
49
50
        if (empty($path)) {
51
            return false;
52
        }
53
        if (!@is_dir($path)) {
54
            $path_status
55
                = "<img src='" . $pathIcon16 . "/0.png'   >" . $path . ' ( ' . $languageConstants[1] . ' ) ' . "<a href=" . $_SERVER['PHP_SELF'] . "?op=dashboard&dircheck=createdir&amp;path=$path&amp;redirect=$redirectFile&amp;languageConstants=$myWords1>"
56
                . $languageConstants[2] . '</a>';
57
        } elseif (@is_writable($path)) {
58
            $path_status = "<img src='" . $pathIcon16 . "/1.png'   >" . $path . ' ( ' . $languageConstants[0] . ' ) ';
59
            $currentMode = (substr(decoct(fileperms($path)), 2));
60
            if ($currentMode != decoct($mode)) {
61
                $path_status = "<img src='" . $pathIcon16 . "/0.png'   >" . $path . sprintf(
62
                        $languageConstants[3],
63
                        decoct($mode),
64
                        $currentMode
65
                    ) . "<a href=" . $_SERVER['PHP_SELF'] . "?op=dashboard&dircheck=setperm&amp;mode=$mode&amp;path=$path&amp;redirect=$redirectFile&amp;languageConstants=$myWords2> "
66
                    . $languageConstants[4] . '</a>';
67
            }
68
        } else {
69
            $currentMode = (substr(decoct(fileperms($path)), 2));
70
            $path_status = "<img src='" . $pathIcon16 . "/0.png'   >" . $path . sprintf(
71
                    $languageConstants[3],
72
                    decoct($mode),
73
                    $currentMode
74
                ) . "<a href=" . $_SERVER['PHP_SELF'] . "?mode&amp;path=$path&amp;redirect=$redirectFile&amp;languageConstants=$myWords2> " . $languageConstants[4] . '</a>';
75
        }
76
77
        return $path_status;
78
    }
79
80
    /**
81
     * @param     $target
82
     * @param int $mode
83
     *
84
     * @return bool
85
     */
86
    public static function createDirectory($target, $mode = 0777)
87
    {
88
        $target = str_replace("..", "", $target);
89
        // http://www.php.net/manual/en/function.mkdir.php
90
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
49% 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...
91
                $dirs = array();
92
93
                while (realpath($target) === false && !empty($target)) {
94
                    array_unshift($dirs, basename($target));
95
                    $target = dirname($target);
96
                };
97
                $target = realpath($target);
98
        // this next test should handle bug on BSD with PHP < 5.3.0
99
                if (!is_dir($target)) {
100
                    array_unshift($dirs, basename($target));
101
                    $target = realpath(dirname($target));
102
                }
103
104
        */
105
106
        return is_dir($target) or (self::createDirectory(dirname($target), $mode) and mkdir($target, $mode));
0 ignored issues
show
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...
Comprehensibility Best Practice introduced by
Using logical operators such as and 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...
107
    }
108
109
    /**
110
     * @param     $target
111
     * @param int $mode
112
     *
113
     * @return bool
114
     */
115
    public static function setDirectoryPermissions($target, $mode = 0777)
116
    {
117
        $target = str_replace("..", "", $target);
118
119
        return @chmod($target, (int)$mode);
120
    }
121
}
122
123
$dircheck = (isset($_GET['dircheck'])) ? filter_input(INPUT_GET, 'dircheck', FILTER_SANITIZE_STRING) : "";
124
125
switch ($dircheck) {
126
    case "createdir":
127
        $languageConstants = array();
128
        if (isset($_GET['path'])) {
129
            $path = filter_input(INPUT_GET, 'path', FILTER_SANITIZE_STRING);
130
        }
131
        if (isset($_GET['redirect'])) {
132
            $redirect = filter_input(INPUT_GET, 'redirect', FILTER_SANITIZE_STRING);
133
        }
134
        if (isset($_GET['languageConstants'])) {
135
            $languageConstants = json_decode($_GET['languageConstants']);
136
        }
137
        $result = DirectoryChecker::createDirectory($path);
138
        $msg    = ($result) ? $languageConstants[0] : $languageConstants[1];
139
        redirect_header($redirect, 2, $msg . ': ' . $path);
140
        exit();
141
        break;
142
    case "setperm":
143
        $languageConstants = array();
144
        if (isset($_GET['path'])) {
145
            $path = filter_input(INPUT_GET, 'path', FILTER_SANITIZE_STRING);
146
        }
147
        if (isset($_GET['mode'])) {
148
            $mode = filter_input(INPUT_GET, 'mode', FILTER_SANITIZE_STRING);
149
        }
150
        if (isset($_GET['redirect'])) {
151
            $redirect = filter_input(INPUT_GET, 'redirect', FILTER_SANITIZE_STRING);
152
        }
153
        if (isset($_GET['languageConstants'])) {
154
            $languageConstants = json_decode($_GET['languageConstants']);
155
        }
156
        $result = DirectoryChecker::setDirectoryPermissions($path, $mode);
157
        $msg    = ($result) ? $languageConstants[0] : $languageConstants[1];
158
        redirect_header($redirect, 2, $msg . ': ' . $path);
159
        exit();
160
        break;
161
}
162