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
wmc 12
eloc 48
c 1
b 0
f 0
dl 0
loc 96
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B getDirectoryStatus() 0 50 6
A createDirectory() 0 6 4
A setDirectoryPermissions() 0 5 1
A dirExists() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Adslight\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
 * Module: Adslight
17
 *
18
 * @category        Module
19
 * @author          XOOPS Development Team <https://xoops.org>
20
 * @copyright       {@link https://xoops.org/ XOOPS Project}
21
 * @license         GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
22
 */
23
24
use Xmf\Request;
25
26
require_once \dirname(__DIR__, 4) . '/mainfile.php';
27
$moduleDirName      = \basename(\dirname(__DIR__, 2));
28
$moduleDirNameUpper = \mb_strtoupper($moduleDirName);
29
\xoops_loadLanguage('directorychecker', $moduleDirName);
30
31
/**
32
 * Class DirectoryChecker
33
 * check status of a directory
34
 */
35
class DirectoryChecker
36
{
37
    /**
38
     * @param     $path
39
     * @param int $mode
40
     * @param     $redirectFile
41
     *
42
     * @return bool|string
43
     */
44
    public static function getDirectoryStatus($path, $mode = 0777, $redirectFile = null)
45
    {
46
        global $pathIcon16;
47
        $moduleDirName      = \basename(\dirname(__DIR__, 2));
48
        $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
49
50
        if (empty($path)) {
51
            return false;
52
        }
53
        if (null === $redirectFile) {
54
            $redirectFile = $_SERVER['SCRIPT_NAME'];
55
        }
56
        if (!@\is_dir($path)) {
57
            $path_status = "<img src='$pathIcon16/0.png' >";
58
            $path_status .= "$path (" . \constant('CO_' . $moduleDirNameUpper . '_' . 'NOTAVAILABLE') . ') ';
59
            $path_status .= "<form action='" . $_SERVER['SCRIPT_NAME'] . "' method='post'>";
60
            $path_status .= "<input type='hidden' name='op' value='createdir'>";
61
            $path_status .= "<input type='hidden' name='path' value='$path'>";
62
            $path_status .= "<input type='hidden' name='redirect' value='$redirectFile'>";
63
            $path_status .= "<button class='submit' onClick='this.form.submit();'>" . \constant('CO_' . $moduleDirNameUpper . '_' . 'CREATETHEDIR') . '</button>';
64
            $path_status .= '</form>';
65
        } elseif (@\is_writable($path)) {
66
            $path_status = "<img src='$pathIcon16/1.png' >";
67
            $path_status .= "$path (" . \constant('CO_' . $moduleDirNameUpper . '_' . 'AVAILABLE') . ') ';
68
            $currentMode = \mb_substr(\decoct(\fileperms($path)), 2);
69
            if ($currentMode != \decoct($mode)) {
70
                $path_status = "<img src='$pathIcon16/0.png' >";
71
                $path_status .= $path . \sprintf(\constant('CO_' . $moduleDirNameUpper . '_' . 'NOTWRITABLE'), \decoct($mode), $currentMode);
72
                $path_status .= "<form action='" . $_SERVER['SCRIPT_NAME'] . "' method='post'>";
73
                $path_status .= "<input type='hidden' name='op' value='setdirperm'>";
74
                $path_status .= "<input type='hidden' name='mode' value='$mode'>";
75
                $path_status .= "<input type='hidden' name='path' value='$path'>";
76
                $path_status .= "<input type='hidden' name='redirect' value='$redirectFile'>";
77
                $path_status .= "<button class='submit' onClick='this.form.submit();'>" . \constant('CO_' . $moduleDirNameUpper . '_' . 'SETMPERM') . '</button>';
78
                $path_status .= '</form>';
79
            }
80
        } else {
81
            $currentMode = \mb_substr(\decoct(\fileperms($path)), 2);
82
            $path_status = "<img src='$pathIcon16/0.png' >";
83
            $path_status .= $path . \sprintf(\constant('CO_' . $moduleDirNameUpper . '_' . 'NOTWRITABLE'), \decoct($mode), $currentMode);
84
            $path_status .= "<form action='" . $_SERVER['SCRIPT_NAME'] . "' method='post'>";
85
            $path_status .= "<input type='hidden' name='op' value='setdirperm'>";
86
            $path_status .= "<input type='hidden' name='mode' value='$mode'>";
87
            $path_status .= "<input type='hidden' name='path' value='$path'>";
88
            $path_status .= "<input type='hidden' name='redirect' value='$redirectFile'>";
89
            $path_status .= "<button class='submit' onClick='this.form.submit();'>" . \constant('CO_' . $moduleDirNameUpper . '_' . 'SETMPERM') . '</button>';
90
            $path_status .= '</form>';
91
        }
92
93
        return $path_status;
94
    }
95
96
    /**
97
     * @param     $target
98
     * @param int $mode
99
     *
100
     * @return bool
101
     */
102
    public static function createDirectory($target, $mode = 0777): bool
103
    {
104
        $target = \str_replace('..', '', $target);
105
106
        // http://www.php.net/manual/en/function.mkdir.php
107
        return \is_dir($target) || (self::createDirectory(\dirname($target), $mode) && !\mkdir($target, $mode) && !\is_dir($target));
108
    }
109
110
    /**
111
     * @param     $target
112
     * @param int $mode
113
     *
114
     * @return bool
115
     */
116
    public static function setDirectoryPermissions($target, $mode = 0777): bool
117
    {
118
        $target = \str_replace('..', '', $target);
119
120
        return @\chmod($target, (int)$mode);
121
    }
122
123
    /**
124
     * @param   $dir_path
125
     *
126
     * @return bool
127
     */
128
    public static function dirExists($dir_path): bool
129
    {
130
        return \is_dir($dir_path);
131
    }
132
}
133
134
$op = Request::getString('op', '', 'POST');
135
switch ($op) {
136
    case 'createdir':
137
        if (Request::hasVar('path', 'POST')) {
138
            $path = $_POST['path'];
139
        }
140
        if (Request::hasVar('redirect', 'POST')) {
141
            $redirect = $_POST['redirect'];
142
        }
143
        $msg = DirectoryChecker::createDirectory($path) ? \constant('CO_' . $moduleDirNameUpper . '_' . 'DIRCREATED') : \constant('CO_' . $moduleDirNameUpper . '_' . 'DIRNOTCREATED');
144
        \redirect_header($redirect, 2, $msg . ': ' . $path);
145
        break;
146
    case 'setdirperm':
147
        if (Request::hasVar('path', 'POST')) {
148
            $path = $_POST['path'];
149
        }
150
        if (Request::hasVar('redirect', 'POST')) {
151
            $redirect = $_POST['redirect'];
152
        }
153
        if (Request::hasVar('mode', 'POST')) {
154
            $mode = $_POST['mode'];
155
        }
156
        $msg = DirectoryChecker::setDirectoryPermissions($path, $mode) ? \constant('CO_' . $moduleDirNameUpper . '_' . 'PERMSET') : \constant('CO_' . $moduleDirNameUpper . '_' . 'PERMNOTSET');
157
        \redirect_header($redirect, 2, $msg . ': ' . $path);
158
        break;
159
}
160