DirectoryChecker::getDirectoryStatus()   B
last analyzed

Complexity

Conditions 6
Paths 9

Size

Total Lines 48
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 42
nc 9
nop 3
dl 0
loc 48
rs 8.6257
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace XoopsModules\Countdown\Common;
6
7
/*
8
 You may not change or alter any portion of this comment or credits
9
 of supporting developers from this source code or any supporting source code
10
 which is considered copyrighted (c) material of the original comment or credit authors.
11
 
12
 This program is distributed in the hope that it will be useful,
13
 but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
*/
16
/**
17
 * Module: Countdown
18
 *
19
 * @category        Module
20
 * @package         countdown
21
 * @author          XOOPS Development Team <https://xoops.org>
22
 * @copyright       {@link https://xoops.org/ XOOPS Project}
23
 * @license         GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
24
 */
25
require_once dirname(dirname(dirname(dirname(__DIR__)))) . '/mainfile.php';
26
$moduleDirName = basename(dirname(dirname(__DIR__)));
27
xoops_loadLanguage('common', $moduleDirName);
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     $redirectFile
39
     *
40
     * @return bool|string
41
     */
42
    public static function getDirectoryStatus($path, $mode = 0777, $redirectFile = null)
43
    {
44
        global $pathIcon16;
45
        $moduleDirName      = basename(dirname(dirname(__DIR__)));
46
        $moduleDirNameUpper = mb_strtoupper($moduleDirName);
47
        if (empty($path)) {
48
            return false;
49
        }
50
        if (null === $redirectFile) {
51
            $redirectFile = $_SERVER['SCRIPT_NAME'];
52
        }
53
        if (!@is_dir($path)) {
54
            $path_status = "<img src='$pathIcon16/0.png' >";
55
            $path_status .= "$path (" . constant('CO_' . $moduleDirNameUpper . '_' . 'NOTAVAILABLE') . ') ';
56
            $path_status .= "<form action='" . $_SERVER['SCRIPT_NAME'] . "' method='post'>";
57
            $path_status .= "<input type='hidden' name='op' value='createdir'>";
58
            $path_status .= "<input type='hidden' name='path' value='$path'>";
59
            $path_status .= "<input type='hidden' name='redirect' value='$redirectFile'>";
60
            $path_status .= "<button class='submit' onClick='this.form.submit();'>" . constant('CO_' . $moduleDirNameUpper . '_' . 'CREATETHEDIR') . '</button>';
61
            $path_status .= '</form>';
62
        } elseif (@is_writable($path)) {
63
            $path_status = "<img src='$pathIcon16/1.png' >";
64
            $path_status .= "$path (" . constant('CO_' . $moduleDirNameUpper . '_' . 'AVAILABLE') . ') ';
65
            $currentMode = substr(decoct(fileperms($path)), 2);
66
            if ($currentMode != decoct($mode)) {
67
                $path_status = "<img src='$pathIcon16/0.png' >";
68
                $path_status .= $path . sprintf(constant('CO_' . $moduleDirNameUpper . '_' . 'NOTWRITABLE'), decoct($mode), $currentMode);
69
                $path_status .= "<form action='" . $_SERVER['SCRIPT_NAME'] . "' method='post'>";
70
                $path_status .= "<input type='hidden' name='op' value='setdirperm'>";
71
                $path_status .= "<input type='hidden' name='mode' value='$mode'>";
72
                $path_status .= "<input type='hidden' name='path' value='$path'>";
73
                $path_status .= "<input type='hidden' name='redirect' value='$redirectFile'>";
74
                $path_status .= "<button class='submit' onClick='this.form.submit();'>" . constant('CO_' . $moduleDirNameUpper . '_' . 'SETMPERM') . '</button>';
75
                $path_status .= '</form>';
76
            }
77
        } else {
78
            $currentMode = substr(decoct(fileperms($path)), 2);
79
            $path_status = "<img src='$pathIcon16/0.png' >";
80
            $path_status .= $path . sprintf(constant('CO_' . $moduleDirNameUpper . '_' . 'NOTWRITABLE'), decoct($mode), $currentMode);
81
            $path_status .= "<form action='" . $_SERVER['SCRIPT_NAME'] . "' method='post'>";
82
            $path_status .= "<input type='hidden' name='op' value='setdirperm'>";
83
            $path_status .= "<input type='hidden' name='mode' value='$mode'>";
84
            $path_status .= "<input type='hidden' name='path' value='$path'>";
85
            $path_status .= "<input type='hidden' name='redirect' value='$redirectFile'>";
86
            $path_status .= "<button class='submit' onClick='this.form.submit();'>" . constant('CO_' . $moduleDirNameUpper . '_' . 'SETMPERM') . '</button>';
87
            $path_status .= '</form>';
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
        // http://www.php.net/manual/en/function.mkdir.php
102
        return is_dir($target) || (self::createDirectory(dirname($target), $mode) && !mkdir($target, $mode) && !is_dir($target));
103
    }
104
105
    /**
106
     * @param     $target
107
     * @param int $mode
108
     *
109
     * @return bool
110
     */
111
    public static function setDirectoryPermissions($target, $mode = 0777)
112
    {
113
        $target = str_replace('..', '', $target);
114
        return @chmod($target, (int)$mode);
115
    }
116
117
    /**
118
     * @param   $dir_path
119
     *
120
     * @return bool
121
     */
122
    public static function dirExists($dir_path)
123
    {
124
        return is_dir($dir_path);
125
    }
126
}
127
128
$op                 = \Xmf\Request::getString('op', '', 'POST');
129
$moduleDirName      = basename(dirname(dirname(__DIR__)));
130
$moduleDirNameUpper = mb_strtoupper($moduleDirName);
131
switch ($op) {
132
    case 'createdir':
133
        if (\Xmf\Request::hasVar('path', 'POST')) {
134
            $path = $_POST['path'];
135
        }
136
        if (\Xmf\Request::hasVar('redirect', 'POST')) {
137
            $redirect = $_POST['redirect'];
138
        }
139
        $msg = DirectoryChecker::createDirectory($path) ? constant('CO_' . $moduleDirNameUpper . '_' . 'DIRCREATED') : constant('CO_' . $moduleDirNameUpper . '_' . 'DIRNOTCREATED');
140
        redirect_header($redirect, 2, $msg . ': ' . $path);
141
        break;
142
    case 'setdirperm':
143
        if (\Xmf\Request::hasVar('path', 'POST')) {
144
            $path = $_POST['path'];
145
        }
146
        if (\Xmf\Request::hasVar('redirect', 'POST')) {
147
            $redirect = $_POST['redirect'];
148
        }
149
        if (\Xmf\Request::hasVar('mode', 'POST')) {
150
            $mode = $_POST['mode'];
151
        }
152
        $msg = DirectoryChecker::setDirectoryPermissions($path, $mode) ? constant('CO_' . $moduleDirNameUpper . '_' . 'PERMSET') : constant('CO_' . $moduleDirNameUpper . '_' . 'PERMNOTSET');
153
        redirect_header($redirect, 2, $msg . ': ' . $path);
154
        break;
155
}
156