DirectoryChecker::createDirectory()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 2
nc 4
nop 2
dl 0
loc 21
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Xoopstube;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
15
/**
16
 * Xoopstube module
17
 *
18
 * @copyright       XOOPS Project (https://xoops.org)
19
 * @license         GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
20
 * @package         wfdownload
21
 * @since           3.23
22
 * @author          Xoops Development Team
23
 */
24
25
use Xmf\Request;
26
27
require_once \dirname(__DIR__, 3) . '/mainfile.php';
28
29
/**
30
 * Class DirectoryChecker
31
 * check status of a directory
32
 */
33
class DirectoryChecker
34
{
35
    /**
36
     * @param       $path
37
     * @param int   $mode
38
     * @param array $languageConstants
39
     * @param       $redirectFile
40
     *
41
     * @return bool|string
42
     */
43
    public static function getDirectoryStatus($path, $mode, array $languageConstants, $redirectFile)
44
    {
45
        global $pathIcon16;
46
47
        $languageConstants1 = [$languageConstants[5], $languageConstants[6]];
48
        $languageConstants2 = [$languageConstants[7], $languageConstants[8]];
49
50
        $myWords1 = \urlencode(json_encode($languageConstants1));
51
        $myWords2 = \urlencode(json_encode($languageConstants2));
52
53
        if (empty($path)) {
54
            return false;
55
        }
56
        if (!@\is_dir($path)) {
57
            $path_status = "<img src='"
58
                           . $pathIcon16
59
                           . "/0.png'   >"
60
                           . $path
61
                           . ' ( '
62
                           . $languageConstants[1]
63
                           . ' ) '
64
                           . '<a href='
65
                           . $_SERVER['SCRIPT_NAME']
66
                           . "?op=dashboard&dircheck=createdir&amp;path=$path&amp;redirect=$redirectFile&amp;languageConstants=$myWords1>"
67
                           . $languageConstants[2]
68
                           . '</a>';
69
        } elseif (@\is_writable($path)) {
70
            $path_status = "<img src='" . $pathIcon16 . "/1.png'   >" . $path . ' ( ' . $languageConstants[0] . ' ) ';
71
            $currentMode = mb_substr(\decoct(\fileperms($path)), 2);
72
            if ($currentMode !== \decoct($mode)) {
73
                $path_status = "<img src='"
74
                               . $pathIcon16
75
                               . "/0.png'   >"
76
                               . $path
77
                               . \sprintf($languageConstants[3], \decoct($mode), $currentMode)
78
                               . '<a href='
79
                               . $_SERVER['SCRIPT_NAME']
80
                               . "?op=dashboard&dircheck=setperm&amp;mode=$mode&amp;path=$path&amp;redirect=$redirectFile&amp;languageConstants=$myWords2> "
81
                               . $languageConstants[4]
82
                               . '</a>';
83
            }
84
        } else {
85
            $currentMode = mb_substr(\decoct(\fileperms($path)), 2);
86
            $path_status = "<img src='" . $pathIcon16 . "/0.png'   >" . $path . \sprintf($languageConstants[3], \decoct($mode), $currentMode) . '<a href=' . $_SERVER['SCRIPT_NAME'] . "?mode&amp;path=$path&amp;redirect=$redirectFile&amp;languageConstants=$myWords2> " . $languageConstants[4] . '</a>';
87
        }
88
89
        return $path_status;
90
    }
91
92
    /**
93
     * @param     $target
94
     * @param int $mode
95
     *
96
     * @return bool
97
     */
98
    public static function createDirectory($target, $mode = 0777)
99
    {
100
        $target = \str_replace('..', '', $target);
101
        // https://www.php.net/manual/en/function.mkdir.php
102
        /*
103
                $dirs = [];
104
105
                while (realpath($target) === false && !empty($target)) {
106
                    array_unshift($dirs, basename($target));
107
                    $target = dirname($target);
108
                };
109
                $target = realpath($target);
110
        // this next test should handle bug on BSD with PHP < 5.3.0
111
                if (!is_dir($target)) {
112
                    array_unshift($dirs, basename($target));
113
                    $target = realpath(dirname($target));
114
                }
115
116
        */
117
118
        return \is_dir($target) || (self::createDirectory(\dirname($target), $mode) && !\mkdir($target, $mode) && !\is_dir($target));
119
    }
120
121
    /**
122
     * @param     $target
123
     * @param int $mode
124
     *
125
     * @return bool
126
     */
127
    public static function setDirectoryPermissions($target, $mode = 0777)
128
    {
129
        $target = \str_replace('..', '', $target);
130
131
        return @\chmod($target, (int)$mode);
132
    }
133
}
134
135
$dircheck = Request::getString('dircheck', '', 'GET') ? \filter_input(\INPUT_GET, 'dircheck', \FILTER_SANITIZE_STRING) : '';
136
137
switch ($dircheck) {
138
    case 'createdir':
139
        $languageConstants = [];
140
        if (Request::hasVar('path', 'GET')) {
141
            $path = \filter_input(\INPUT_GET, 'path', \FILTER_SANITIZE_STRING);
142
        }
143
        if (Request::hasVar('redirect', 'GET')) {
144
            $redirect = \filter_input(\INPUT_GET, 'redirect', \FILTER_SANITIZE_STRING);
145
        }
146
        if (Request::hasVar('languageConstants', 'GET')) {
147
            $languageConstants = json_decode(Request::getString('languageConstants', '', 'GET'));
148
        }
149
        $result = DirectoryChecker::createDirectory($path);
150
        $msg    = $result ? $languageConstants[0] : $languageConstants[1];
151
        \redirect_header($redirect, 2, $msg . ': ' . $path);
152
153
        break;
154
    case 'setperm':
155
        $languageConstants = [];
156
        if (Request::hasVar('path', 'GET')) {
157
            $path = \filter_input(\INPUT_GET, 'path', \FILTER_SANITIZE_STRING);
158
        }
159
        if (Request::hasVar('mode', 'GET')) {
160
            $mode = \filter_input(\INPUT_GET, 'mode', \FILTER_SANITIZE_STRING);
161
        }
162
        if (Request::hasVar('redirect', 'GET')) {
163
            $redirect = \filter_input(\INPUT_GET, 'redirect', \FILTER_SANITIZE_STRING);
164
        }
165
        if (Request::hasVar('languageConstants', 'GET')) {
166
            $languageConstants = json_decode(Request::getString('languageConstants', '', 'GET'));
167
        }
168
        $result = DirectoryChecker::setDirectoryPermissions($path, $mode);
169
        $msg    = $result ? $languageConstants[0] : $languageConstants[1];
170
        \redirect_header($redirect, 2, $msg . ': ' . $path);
171
172
        break;
173
}
174