DirectoryChecker   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 48
c 1
b 0
f 0
dl 0
loc 96
rs 10
wmc 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
A dirExists() 0 3 1
B getDirectoryStatus() 0 50 6
A setDirectoryPermissions() 0 5 1
A createDirectory() 0 6 4
1
<?php
2
3
namespace XoopsModules\Wgfilemanager\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
 * Wgfilemanager 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\Wgfilemanager;
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
        $pathIcon16 = \Xmf\Module\Admin::iconUrl('', '16');
49
50
        if (empty($path)) {
51
            return false;
52
        }
53
        if (null === $redirectFile) {
54
            $redirectFile = $_SERVER['SCRIPT_NAME'];
55
        }
56
        $moduleDirName      = \basename(\dirname(__DIR__, 2));
57
        $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
58
        if (!@\is_dir($path)) {
59
            $path_status = "<img src='$pathIcon16/0.png' >";
60
            $path_status .= "$path (" . \constant('CO_' . $moduleDirNameUpper . '_' . 'DC_NOTAVAILABLE') . ') ';
61
            $path_status .= "<form action='" . $_SERVER['SCRIPT_NAME'] . "' method='post'>";
62
            $path_status .= "<input type='hidden' name='op' value='createdir'>";
63
            $path_status .= "<input type='hidden' name='path' value='$path'>";
64
            $path_status .= "<input type='hidden' name='redirect' value='$redirectFile'>";
65
            $path_status .= "<button class='submit' onClick='this.form.submit();'>" . \constant('CO_' . $moduleDirNameUpper . '_' . 'DC_CREATETHEDIR') . '</button>';
66
            $path_status .= '</form>';
67
        } elseif (@\is_writable($path)) {
68
            $path_status = "<img src='$pathIcon16/1.png' >";
69
            $path_status .= "$path (" . \constant('CO_' . $moduleDirNameUpper . '_' . 'DC_AVAILABLE') . ') ';
70
            $currentMode = mb_substr(\decoct(\fileperms($path)), 2);
71
            if ($currentMode != \decoct($mode)) {
72
                $path_status = "<img src='$pathIcon16/0.png' >";
73
                $path_status .= $path . \sprintf(\constant('CO_' . $moduleDirNameUpper . '_' . 'DC_NOTWRITABLE'), \decoct($mode), $currentMode);
74
                $path_status .= "<form action='" . $_SERVER['SCRIPT_NAME'] . "' method='post'>";
75
                $path_status .= "<input type='hidden' name='op' value='setdirperm'>";
76
                $path_status .= "<input type='hidden' name='mode' value='$mode'>";
77
                $path_status .= "<input type='hidden' name='path' value='$path'>";
78
                $path_status .= "<input type='hidden' name='redirect' value='$redirectFile'>";
79
                $path_status .= "<button class='submit' onClick='this.form.submit();'>" . \constant('CO_' . $moduleDirNameUpper . '_' . 'DC_SETMPERM') . '</button>';
80
                $path_status .= '</form>';
81
            }
82
        } else {
83
            $currentMode = mb_substr(\decoct(\fileperms($path)), 2);
84
            $path_status = "<img src='$pathIcon16/0.png' >";
85
            $path_status .= $path . \sprintf(\constant('CO_' . $moduleDirNameUpper . '_' . 'DC_NOTWRITABLE'), \decoct($mode), $currentMode);
86
            $path_status .= "<form action='" . $_SERVER['SCRIPT_NAME'] . "' method='post'>";
87
            $path_status .= "<input type='hidden' name='op' value='setdirperm'>";
88
            $path_status .= "<input type='hidden' name='mode' value='$mode'>";
89
            $path_status .= "<input type='hidden' name='path' value='$path'>";
90
            $path_status .= "<input type='hidden' name='redirect' value='$redirectFile'>";
91
            $path_status .= "<button class='submit' onClick='this.form.submit();'>" . \constant('CO_' . $moduleDirNameUpper . '_' . 'DC_SETMPERM') . '</button>';
92
            $path_status .= '</form>';
93
        }
94
95
        return $path_status;
96
    }
97
98
    /**
99
     * @param     $target
100
     * @param int $mode
101
     *
102
     * @return bool
103
     */
104
    public static function createDirectory($target, $mode = 0777)
105
    {
106
        $target = \str_replace('..', '', $target);
107
108
        // http://www.php.net/manual/en/function.mkdir.php
109
        return \is_dir($target) || (self::createDirectory(\dirname($target), $mode) && !\mkdir($target, $mode) && !\is_dir($target));
110
    }
111
112
    /**
113
     * @param     $target
114
     * @param int $mode
115
     *
116
     * @return bool
117
     */
118
    public static function setDirectoryPermissions($target, $mode = 0777)
119
    {
120
        $target = \str_replace('..', '', $target);
121
122
        return @\chmod($target, (int)$mode);
123
    }
124
125
    /**
126
     * @param   $dir_path
127
     *
128
     * @return bool
129
     */
130
    public static function dirExists($dir_path)
131
    {
132
        return \is_dir($dir_path);
133
    }
134
}
135
136
$op = Request::getString('op', '', 'POST');
137
switch ($op) {
138
    case 'createdir':
139
        if (\Xmf\Request::hasVar('path', 'POST')) {
140
            $path = $_POST['path'];
141
        }
142
        if (\Xmf\Request::hasVar('redirect', 'POST')) {
143
            $redirect = $_POST['redirect'];
144
        }
145
        $msg = DirectoryChecker::createDirectory($path) ? \constant('CO_' . $moduleDirNameUpper . '_' . 'DC_DIRCREATED') : \constant('CO_' . $moduleDirNameUpper . '_' . 'DC_DIRNOTCREATED');
146
        \redirect_header($redirect, 2, $msg . ': ' . $path);
147
        break;
148
    case 'setdirperm':
149
        if (\Xmf\Request::hasVar('path', 'POST')) {
150
            $path = $_POST['path'];
151
        }
152
        if (\Xmf\Request::hasVar('redirect', 'POST')) {
153
            $redirect = $_POST['redirect'];
154
        }
155
        if (\Xmf\Request::hasVar('mode', 'POST')) {
156
            $mode = $_POST['mode'];
157
        }
158
        $msg = DirectoryChecker::setDirectoryPermissions($path, $mode) ? \constant('CO_' . $moduleDirNameUpper . '_' . 'DC_PERMSET') : \constant('CO_' . $moduleDirNameUpper . '_' . 'DC_PERMNOTSET');
159
        \redirect_header($redirect, 2, $msg . ': ' . $path);
160
        break;
161
}
162