Passed
Push — master ( ad29f5...3d7fba )
by Goffy
04:21
created

DirectoryChecker::getDirectoryStatus()   B

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
cc 6
eloc 42
c 1
b 0
f 0
nc 9
nop 3
dl 0
loc 50
rs 8.6257
1
<?php
2
3
namespace XoopsModules\Wggithub\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
 * Wggithub 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;
0 ignored issues
show
Bug introduced by
The type Xmf\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
use XoopsModules\Wggithub;
25
26
27
require_once \dirname(__DIR__, 4) . '/mainfile.php';
28
$moduleDirName      = \basename(\dirname(__DIR__, 2));
29
$moduleDirNameUpper = \mb_strtoupper($moduleDirName);
30
\xoops_loadLanguage('directorychecker', $moduleDirName);
0 ignored issues
show
Bug introduced by
The function xoops_loadLanguage was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

30
/** @scrutinizer ignore-call */ 
31
\xoops_loadLanguage('directorychecker', $moduleDirName);
Loading history...
31
32
/**
33
 * Class DirectoryChecker
34
 * check status of a directory
35
 */
36
class DirectoryChecker
37
{
38
    /**
39
     * @param     $path
40
     * @param int $mode
41
     * @param     $redirectFile
42
     *
43
     * @return bool|string
44
     */
45
    public static function getDirectoryStatus($path, $mode = 0777, $redirectFile = null)
46
    {
47
        $pathIcon16 = \Xmf\Module\Admin::iconUrl('', '16');
0 ignored issues
show
Bug introduced by
The type Xmf\Module\Admin was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
48
49
        if (empty($path)) {
50
            return false;
51
        }
52
        if (null === $redirectFile) {
53
            $redirectFile = $_SERVER['SCRIPT_NAME'];
54
        }
55
        $moduleDirName      = \basename(\dirname(__DIR__, 2));
56
        $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
57
        if (!@\is_dir($path)) {
58
            $path_status = "<img src='$pathIcon16/0.png' >";
59
            $path_status .= "$path (" . \constant('CO_' . $moduleDirNameUpper . '_' . 'DC_NOTAVAILABLE') . ') ';
60
            $path_status .= "<form action='" . $_SERVER['SCRIPT_NAME'] . "' method='post'>";
61
            $path_status .= "<input type='hidden' name='op' value='createdir'>";
62
            $path_status .= "<input type='hidden' name='path' value='$path'>";
63
            $path_status .= "<input type='hidden' name='redirect' value='$redirectFile'>";
64
            $path_status .= "<button class='submit' onClick='this.form.submit();'>" . \constant('CO_' . $moduleDirNameUpper . '_' . 'DC_CREATETHEDIR') . '</button>';
65
            $path_status .= '</form>';
66
        } elseif (@\is_writable($path)) {
67
            $path_status = "<img src='$pathIcon16/1.png' >";
68
            $path_status .= "$path (" . \constant('CO_' . $moduleDirNameUpper . '_' . 'DC_AVAILABLE') . ') ';
69
            $currentMode = \mb_substr(\decoct(\fileperms($path)), 2);
70
            if ($currentMode != \decoct($mode)) {
71
                $path_status = "<img src='$pathIcon16/0.png' >";
72
                $path_status .= $path . \sprintf(\constant('CO_' . $moduleDirNameUpper . '_' . 'DC_NOTWRITABLE'), \decoct($mode), $currentMode);
73
                $path_status .= "<form action='" . $_SERVER['SCRIPT_NAME'] . "' method='post'>";
74
                $path_status .= "<input type='hidden' name='op' value='setdirperm'>";
75
                $path_status .= "<input type='hidden' name='mode' value='$mode'>";
76
                $path_status .= "<input type='hidden' name='path' value='$path'>";
77
                $path_status .= "<input type='hidden' name='redirect' value='$redirectFile'>";
78
                $path_status .= "<button class='submit' onClick='this.form.submit();'>" . \constant('CO_' . $moduleDirNameUpper . '_' . 'DC_SETMPERM') . '</button>';
79
                $path_status .= '</form>';
80
            }
81
        } else {
82
            $currentMode = \mb_substr(\decoct(\fileperms($path)), 2);
83
            $path_status = "<img src='$pathIcon16/0.png' >";
84
            $path_status .= $path . \sprintf(\constant('CO_' . $moduleDirNameUpper . '_' . 'DC_NOTWRITABLE'), \decoct($mode), $currentMode);
85
            $path_status .= "<form action='" . $_SERVER['SCRIPT_NAME'] . "' method='post'>";
86
            $path_status .= "<input type='hidden' name='op' value='setdirperm'>";
87
            $path_status .= "<input type='hidden' name='mode' value='$mode'>";
88
            $path_status .= "<input type='hidden' name='path' value='$path'>";
89
            $path_status .= "<input type='hidden' name='redirect' value='$redirectFile'>";
90
            $path_status .= "<button class='submit' onClick='this.form.submit();'>" . \constant('CO_' . $moduleDirNameUpper . '_' . 'DC_SETMPERM') . '</button>';
91
            $path_status .= '</form>';
92
        }
93
94
        return $path_status;
95
    }
96
97
    /**
98
     * @param     $target
99
     * @param int $mode
100
     *
101
     * @return bool
102
     */
103
    public static function createDirectory($target, $mode = 0777)
104
    {
105
        $target = \str_replace('..', '', $target);
106
107
        // http://www.php.net/manual/en/function.mkdir.php
108
        return \is_dir($target) || (self::createDirectory(\dirname($target), $mode) && !\mkdir($target, $mode) && !\is_dir($target));
109
    }
110
111
    /**
112
     * @param     $target
113
     * @param int $mode
114
     *
115
     * @return bool
116
     */
117
    public static function setDirectoryPermissions($target, $mode = 0777)
118
    {
119
        $target = \str_replace('..', '', $target);
120
121
        return @\chmod($target, (int)$mode);
122
    }
123
124
    /**
125
     * @param   $dir_path
126
     *
127
     * @return bool
128
     */
129
    public static function dirExists($dir_path)
130
    {
131
        return \is_dir($dir_path);
132
    }
133
}
134
135
$op = Request::getString('op', '', 'POST');
136
switch ($op) {
137
    case 'createdir':
138
        if (\Xmf\Request::hasVar('path', 'POST')) {
139
            $path = $_POST['path'];
140
        }
141
        if (\Xmf\Request::hasVar('redirect', 'POST')) {
142
            $redirect = $_POST['redirect'];
143
        }
144
        $msg = DirectoryChecker::createDirectory($path) ? \constant('CO_' . $moduleDirNameUpper . '_' . 'DC_DIRCREATED') : \constant('CO_' . $moduleDirNameUpper . '_' . 'DC_DIRNOTCREATED');
145
        \redirect_header($redirect, 2, $msg . ': ' . $path);
0 ignored issues
show
Bug introduced by
The function redirect_header was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

145
        /** @scrutinizer ignore-call */ 
146
        \redirect_header($redirect, 2, $msg . ': ' . $path);
Loading history...
146
        break;
147
    case 'setdirperm':
148
        if (\Xmf\Request::hasVar('path', 'POST')) {
149
            $path = $_POST['path'];
150
        }
151
        if (\Xmf\Request::hasVar('redirect', 'POST')) {
152
            $redirect = $_POST['redirect'];
153
        }
154
        if (\Xmf\Request::hasVar('mode', 'POST')) {
155
            $mode = $_POST['mode'];
156
        }
157
        $msg = DirectoryChecker::setDirectoryPermissions($path, $mode) ? \constant('CO_' . $moduleDirNameUpper . '_' . 'DC_PERMSET') : \constant('CO_' . $moduleDirNameUpper . '_' . 'DC_PERMNOTSET');
158
        \redirect_header($redirect, 2, $msg . ': ' . $path);
159
        break;
160
}
161