DirectoryChecker::getDirectoryStatus()   B
last analyzed

Complexity

Conditions 6
Paths 9

Size

Total Lines 50
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 42
c 1
b 0
f 0
dl 0
loc 50
rs 8.6257
cc 6
nc 9
nop 3
1
<?php
2
3
namespace XoopsModules\Groupmanager\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
 * Groups 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
 * @author          Xoops Development Team
21
 */
22
23
use Xmf\Request;
24
25
require_once \dirname(__DIR__, 4) . '/mainfile.php';
26
$moduleDirName      = \basename(\dirname(__DIR__, 2));
27
$moduleDirNameUpper = mb_strtoupper($moduleDirName);
28
\xoops_loadLanguage('directorychecker', $moduleDirName);
29
30
/**
31
 * Class DirectoryChecker
32
 * check status of a directory
33
 */
34
class DirectoryChecker
35
{
36
    /**
37
     * @param     $path
38
     * @param int $mode
39
     * @param     $redirectFile
40
     *
41
     * @return bool|string
42
     */
43
    public static function getDirectoryStatus($path, $mode = 0777, $redirectFile = null)
44
    {
45
        global $pathIcon16;
46
47
        if (empty($path)) {
48
            return false;
49
        }
50
        if (null === $redirectFile) {
51
            $redirectFile = $_SERVER['SCRIPT_NAME'];
52
        }
53
        $moduleDirName      = \basename(\dirname(__DIR__, 2));
54
        $moduleDirNameUpper = mb_strtoupper($moduleDirName);
55
        if (!@\is_dir($path)) {
56
            $path_status = "<img src='$pathIcon16/0.png' >";
57
            $path_status .= "$path (" . \constant('CO_' . $moduleDirNameUpper . '_' . 'DC_NOTAVAILABLE') . ') ';
58
            $path_status .= "<form action='" . $_SERVER['SCRIPT_NAME'] . "' method='post'>";
59
            $path_status .= "<input type='hidden' name='op' value='createdir'>";
60
            $path_status .= "<input type='hidden' name='path' value='$path'>";
61
            $path_status .= "<input type='hidden' name='redirect' value='$redirectFile'>";
62
            $path_status .= "<button class='submit' onClick='this.form.submit();'>" . \constant('CO_' . $moduleDirNameUpper . '_' . 'DC_CREATETHEDIR') . '</button>';
63
            $path_status .= '</form>';
64
        } elseif (@\is_writable($path)) {
65
            $path_status = "<img src='$pathIcon16/1.png' >";
66
            $path_status .= "$path (" . \constant('CO_' . $moduleDirNameUpper . '_' . 'DC_AVAILABLE') . ') ';
67
            $currentMode = mb_substr(\decoct(\fileperms($path)), 2);
68
            if ($currentMode != \decoct($mode)) {
69
                $path_status = "<img src='$pathIcon16/0.png' >";
70
                $path_status .= $path . \sprintf(\constant('CO_' . $moduleDirNameUpper . '_' . 'DC_NOTWRITABLE'), \decoct($mode), $currentMode);
71
                $path_status .= "<form action='" . $_SERVER['SCRIPT_NAME'] . "' method='post'>";
72
                $path_status .= "<input type='hidden' name='op' value='setdirperm'>";
73
                $path_status .= "<input type='hidden' name='mode' value='$mode'>";
74
                $path_status .= "<input type='hidden' name='path' value='$path'>";
75
                $path_status .= "<input type='hidden' name='redirect' value='$redirectFile'>";
76
                $path_status .= "<button class='submit' onClick='this.form.submit();'>" . \constant('CO_' . $moduleDirNameUpper . '_' . 'DC_SETMPERM') . '</button>';
77
                $path_status .= '</form>';
78
            }
79
        } else {
80
            $currentMode = mb_substr(\decoct(\fileperms($path)), 2);
81
            $path_status = "<img src='$pathIcon16/0.png' >";
82
            $path_status .= $path . \sprintf(\constant('CO_' . $moduleDirNameUpper . '_' . 'DC_NOTWRITABLE'), \decoct($mode), $currentMode);
83
            $path_status .= "<form action='" . $_SERVER['SCRIPT_NAME'] . "' method='post'>";
84
            $path_status .= "<input type='hidden' name='op' value='setdirperm'>";
85
            $path_status .= "<input type='hidden' name='mode' value='$mode'>";
86
            $path_status .= "<input type='hidden' name='path' value='$path'>";
87
            $path_status .= "<input type='hidden' name='redirect' value='$redirectFile'>";
88
            $path_status .= "<button class='submit' onClick='this.form.submit();'>" . \constant('CO_' . $moduleDirNameUpper . '_' . 'DC_SETMPERM') . '</button>';
89
            $path_status .= '</form>';
90
        }
91
92
        return $path_status;
93
    }
94
95
    /**
96
     * @param     $target
97
     * @param int $mode
98
     *
99
     * @return bool
100
     */
101
    public static function createDirectory($target, $mode = 0777)
102
    {
103
        $target = \str_replace('..', '', $target);
104
105
        // http://www.php.net/manual/en/function.mkdir.php
106
        return \is_dir($target) || (self::createDirectory(\dirname($target), $mode) && !\mkdir($target, $mode) && !\is_dir($target));
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
     * @param   $dir_path
124
     *
125
     * @return bool
126
     */
127
    public static function dirExists($dir_path)
128
    {
129
        return \is_dir($dir_path);
130
    }
131
}
132
133
$op = Request::getString('op', '', 'POST');
134
$path = '';
135
switch ($op) {
136
    case 'createdir':
137
        if (\Xmf\Request::hasVar('path', 'POST')) {
138
            $path = $_POST['path'];
139
        }
140
        if (\Xmf\Request::hasVar('redirect', 'POST')) {
141
            $redirect = $_POST['redirect'];
142
        }
143
        $msg = DirectoryChecker::createDirectory($path) ? \constant('CO_' . $moduleDirNameUpper . '_' . 'DC_DIRCREATED') : \constant('CO_' . $moduleDirNameUpper . '_' . 'DC_DIRNOTCREATED');
144
        \redirect_header($redirect, 2, $msg . ': ' . $path);
145
        break;
146
    case 'setdirperm':
147
        if (\Xmf\Request::hasVar('path', 'POST')) {
148
            $path = $_POST['path'];
149
        }
150
        if (\Xmf\Request::hasVar('redirect', 'POST')) {
151
            $redirect = $_POST['redirect'];
152
        }
153
        if (\Xmf\Request::hasVar('mode', 'POST')) {
154
            $mode = $_POST['mode'];
155
        }
156
        $msg = DirectoryChecker::setDirectoryPermissions($path, $mode) ? \constant('CO_' . $moduleDirNameUpper . '_' . 'DC_PERMSET') : \constant('CO_' . $moduleDirNameUpper . '_' . 'DC_PERMNOTSET');
157
        \redirect_header($redirect, 2, $msg . ': ' . $path);
158
        break;
159
}
160