DirectoryChecker::dirExists()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Chess\Common;
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
 * Chess module
17
 *
18
 * @copyright       XOOPS Project (https://xoops.org)
19
 * @license         GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
20
 * @author          Xoops Development Team
21
 */
22
23
use Xmf\Request;
24
use XoopsModules\Chess;
25
26
//defined('XOOPS_ROOT_PATH') || die('XOOPS root path not defined');
27
28
require_once \dirname(__DIR__, 4) . '/mainfile.php';
29
$moduleDirName      = \basename(\dirname(__DIR__, 2));
30
$moduleDirNameUpper = mb_strtoupper($moduleDirName);
31
\xoops_loadLanguage('directorychecker', $moduleDirName);
32
33
/**
34
 * Class DirectoryChecker
35
 * check status of a directory
36
 */
37
class DirectoryChecker
38
{
39
    /**
40
     * @param     $path
41
     * @param int $mode
42
     * @param     $redirectFile
43
     *
44
     * @return bool|string
45
     */
46
    public static function getDirectoryStatus($path, $mode = 0777, $redirectFile = null)
47
    {
48
        global $pathIcon16;
49
50
        if (empty($path)) {
51
            return false;
52
        }
53
54
        if (null === $redirectFile) {
55
            $redirectFile = $_SERVER['SCRIPT_NAME'];
56
        }
57
58
        $moduleDirName = \basename(\dirname(__DIR__, 2));
59
60
        $moduleDirNameUpper = mb_strtoupper($moduleDirName);
61
62
        if (!@\is_dir($path)) {
63
            $path_status = "<img src='$pathIcon16/0.png' >";
64
65
            $path_status .= "$path (" . \constant('CO_' . $moduleDirNameUpper . '_' . 'DC_NOTAVAILABLE') . ') ';
66
67
            $path_status .= "<form action='" . $_SERVER['SCRIPT_NAME'] . "' method='post'>";
68
69
            $path_status .= "<input type='hidden' name='op' value='createdir'>";
70
71
            $path_status .= "<input type='hidden' name='path' value='$path'>";
72
73
            $path_status .= "<input type='hidden' name='redirect' value='$redirectFile'>";
74
75
            $path_status .= "<button class='submit' onClick='this.form.submit();'>" . \constant('CO_' . $moduleDirNameUpper . '_' . 'DC_CREATETHEDIR') . '</button>';
76
77
            $path_status .= '</form>';
78
        } elseif (@\is_writable($path)) {
79
            $path_status = "<img src='$pathIcon16/1.png' >";
80
81
            $path_status .= "$path (" . \constant('CO_' . $moduleDirNameUpper . '_' . 'DC_AVAILABLE') . ') ';
82
83
            $currentMode = mb_substr(\decoct(\fileperms($path)), 2);
84
85
            if ($currentMode != \decoct($mode)) {
86
                $path_status = "<img src='$pathIcon16/0.png' >";
87
88
                $path_status .= $path . \sprintf(\constant('CO_' . $moduleDirNameUpper . '_' . 'DC_NOTWRITABLE'), \decoct($mode), $currentMode);
89
90
                $path_status .= "<form action='" . $_SERVER['SCRIPT_NAME'] . "' method='post'>";
91
92
                $path_status .= "<input type='hidden' name='op' value='setdirperm'>";
93
94
                $path_status .= "<input type='hidden' name='mode' value='$mode'>";
95
96
                $path_status .= "<input type='hidden' name='path' value='$path'>";
97
98
                $path_status .= "<input type='hidden' name='redirect' value='$redirectFile'>";
99
100
                $path_status .= "<button class='submit' onClick='this.form.submit();'>" . \constant('CO_' . $moduleDirNameUpper . '_' . 'DC_SETMPERM') . '</button>';
101
102
                $path_status .= '</form>';
103
            }
104
        } else {
105
            $currentMode = mb_substr(\decoct(\fileperms($path)), 2);
106
107
            $path_status = "<img src='$pathIcon16/0.png' >";
108
109
            $path_status .= $path . \sprintf(\constant('CO_' . $moduleDirNameUpper . '_' . 'DC_NOTWRITABLE'), \decoct($mode), $currentMode);
110
111
            $path_status .= "<form action='" . $_SERVER['SCRIPT_NAME'] . "' method='post'>";
112
113
            $path_status .= "<input type='hidden' name='op' value='setdirperm'>";
114
115
            $path_status .= "<input type='hidden' name='mode' value='$mode'>";
116
117
            $path_status .= "<input type='hidden' name='path' value='$path'>";
118
119
            $path_status .= "<input type='hidden' name='redirect' value='$redirectFile'>";
120
121
            $path_status .= "<button class='submit' onClick='this.form.submit();'>" . \constant('CO_' . $moduleDirNameUpper . '_' . 'DC_SETMPERM') . '</button>';
122
123
            $path_status .= '</form>';
124
        }
125
126
        return $path_status;
127
    }
128
129
    /**
130
     * @param     $target
131
     * @param int $mode
132
     *
133
     * @return bool
134
     */
135
    public static function createDirectory($target, $mode = 0777)
136
    {
137
        $target = \str_replace('..', '', $target);
138
139
        // http://www.php.net/manual/en/function.mkdir.php
140
141
        return \is_dir($target) || (self::createDirectory(\dirname($target), $mode) && !\mkdir($target, $mode) && !\is_dir($target));
142
    }
143
144
    /**
145
     * @param     $target
146
     * @param int $mode
147
     *
148
     * @return bool
149
     */
150
    public static function setDirectoryPermissions($target, $mode = 0777)
151
    {
152
        $target = \str_replace('..', '', $target);
153
154
        return @\chmod($target, (int)$mode);
155
    }
156
157
    /**
158
     * @param   $dir_path
159
     *
160
     * @return bool
161
     */
162
    public static function dirExists($dir_path)
163
    {
164
        return \is_dir($dir_path);
165
    }
166
}
167
168
$op = Request::getString('op', '', 'POST');
169
switch ($op) {
170
    case 'createdir':
171
        if (\Xmf\Request::hasVar('path', 'POST')) {
172
            $path = $_POST['path'];
173
        }
174
        if (\Xmf\Request::hasVar('redirect', 'POST')) {
175
            $redirect = $_POST['redirect'];
176
        }
177
        $msg = DirectoryChecker::createDirectory($path) ? \constant('CO_' . $moduleDirNameUpper . '_' . 'DC_DIRCREATED') : \constant('CO_' . $moduleDirNameUpper . '_' . 'DC_DIRNOTCREATED');
178
        \redirect_header($redirect, 2, $msg . ': ' . $path);
179
        break;
180
    case 'setdirperm':
181
        if (\Xmf\Request::hasVar('path', 'POST')) {
182
            $path = $_POST['path'];
183
        }
184
        if (\Xmf\Request::hasVar('redirect', 'POST')) {
185
            $redirect = $_POST['redirect'];
186
        }
187
        if (\Xmf\Request::hasVar('mode', 'POST')) {
188
            $mode = $_POST['mode'];
189
        }
190
        $msg = DirectoryChecker::setDirectoryPermissions($path, $mode) ? \constant('CO_' . $moduleDirNameUpper . '_' . 'DC_PERMSET') : \constant('CO_' . $moduleDirNameUpper . '_' . 'DC_PERMNOTSET');
191
        \redirect_header($redirect, 2, $msg . ': ' . $path);
192
        break;
193
}
194