DirectoryChecker::getDirectoryStatus()   B
last analyzed

Complexity

Conditions 6
Paths 9

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 9
nop 3
dl 0
loc 51
rs 8.4468
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace XoopsModules\Smallworld\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
 * Smallworld 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\Smallworld;
25
use XoopsModules\Smallworld\Constants;
26
27
//defined('XOOPS_ROOT_PATH') || die('XOOPS root path not defined');
28
29
require_once dirname(dirname(dirname(dirname(__DIR__)))) . '/mainfile.php';
30
$moduleDirName      = basename(dirname(dirname(__DIR__)));
31
$moduleDirNameUpper = mb_strtoupper($moduleDirName);
32
xoops_loadLanguage('directorychecker', $moduleDirName);
33
34
/**
35
 * Class DirectoryChecker
36
 * check status of a directory
37
 */
38
class DirectoryChecker
39
{
40
    /**
41
     * @param     $path
42
     * @param int $mode
43
     * @param     $redirectFile
44
     *
45
     * @return bool|string
46
     */
47
    public static function getDirectoryStatus($path, $mode = 0777, $redirectFile = null)
48
    {
49
        global $pathIcon16;
50
51
        if (empty($path)) {
52
            return false;
53
        }
54
        if (null === $redirectFile) {
55
            $redirectFile = $_SERVER['SCRIPT_NAME'];
56
        }
57
        $moduleDirName      = basename(dirname(dirname(__DIR__)));
58
        $moduleDirNameUpper = mb_strtoupper($moduleDirName);
59
        if (!@is_dir($path)) {
60
            $path_status = "<img src='$pathIcon16/0.png' >";
61
            $path_status .= "$path (" . constant('CO_' . $moduleDirNameUpper . '_' . 'DC_NOTAVAILABLE') . ') ';
62
            $path_status .= "<form action='" . $_SERVER['SCRIPT_NAME'] . "' method='post'>";
63
            $path_status .= "<input type='hidden' name='op' value='createdir'>";
64
            $path_status .= "<input type='hidden' name='path' value='$path'>";
65
            $path_status .= "<input type='hidden' name='redirect' value='$redirectFile'>";
66
            $path_status .= "<button class='submit' onClick='this.form.submit();'>" . constant('CO_' . $moduleDirNameUpper . '_' . 'DC_CREATETHEDIR') . '</button>';
67
            $path_status .= '</form>';
68
        } elseif (@is_writable($path)) {
69
            $path_status = "<img src='$pathIcon16/1.png' >";
70
            $path_status .= "$path (" . constant('CO_' . $moduleDirNameUpper . '_' . 'DC_AVAILABLE') . ') ';
71
            $currentMode = mb_substr(decoct(fileperms($path)), 2);
72
            if ($currentMode != decoct($mode)) {
73
                $path_status = "<img src='$pathIcon16/0.png' >";
74
                $path_status .= $path . sprintf(constant('CO_' . $moduleDirNameUpper . '_' . 'DC_NOTWRITABLE'), decoct($mode), $currentMode);
75
                $path_status .= "<form action='" . $_SERVER['SCRIPT_NAME'] . "' method='post'>";
76
                $path_status .= "<input type='hidden' name='op' value='setdirperm'>";
77
                $path_status .= "<input type='hidden' name='mode' value='$mode'>";
78
                $path_status .= "<input type='hidden' name='path' value='$path'>";
79
                $path_status .= "<input type='hidden' name='redirect' value='$redirectFile'>";
80
                $path_status .= "<button class='submit' onClick='this.form.submit();'>" . constant('CO_' . $moduleDirNameUpper . '_' . 'DC_SETMPERM') . '</button>';
81
                $path_status .= '</form>';
82
            }
83
        } else {
84
            $currentMode = mb_substr(decoct(fileperms($path)), 2);
85
            $path_status = "<img src='$pathIcon16/0.png' >";
86
            $path_status .= $path . sprintf(constant('CO_' . $moduleDirNameUpper . '_' . 'DC_NOTWRITABLE'), decoct($mode), $currentMode);
87
            $path_status .= "<form action='" . $_SERVER['SCRIPT_NAME'] . "' method='post'>";
88
            $path_status .= "<input type='hidden' name='op' value='setdirperm'>";
89
            $path_status .= "<input type='hidden' name='mode' value='$mode'>";
90
            $path_status .= "<input type='hidden' name='path' value='$path'>";
91
            $path_status .= "<input type='hidden' name='redirect' value='$redirectFile'>";
92
            $path_status .= "<button class='submit' onClick='this.form.submit();'>" . constant('CO_' . $moduleDirNameUpper . '_' . 'DC_SETMPERM') . '</button>';
93
            $path_status .= '</form>';
94
        }
95
96
        return $path_status;
97
    }
98
99
    /**
100
     * @param     $target
101
     * @param int $mode
102
     *
103
     * @return bool
104
     */
105
    public static function createDirectory($target, $mode = 0777)
106
    {
107
        $target = str_replace('..', '', $target);
108
109
        // http://www.php.net/manual/en/function.mkdir.php
110
        return is_dir($target) || (self::createDirectory(dirname($target), $mode) && !mkdir($target, $mode) && !is_dir($target));
111
    }
112
113
    /**
114
     * @param     $target
115
     * @param int $mode
116
     *
117
     * @return bool
118
     */
119
    public static function setDirectoryPermissions($target, $mode = 0777)
120
    {
121
        $target = str_replace('..', '', $target);
122
123
        return @chmod($target, (int)$mode);
124
    }
125
126
    /**
127
     * @param   $dir_path
128
     *
129
     * @return bool
130
     */
131
    public static function dirExists($dir_path)
132
    {
133
        return is_dir($dir_path);
134
    }
135
}
136
137
$op = Request::getString('op', '', 'POST');
138
switch ($op) {
139
    case 'createdir':
140
        if (\Xmf\Request::hasVar('path', 'POST')) {
141
            $path = $_POST['path'];
142
        }
143
        if (\Xmf\Request::hasVar('redirect', 'POST')) {
144
            $redirect = $_POST['redirect'];
145
        }
146
        $msg = DirectoryChecker::createDirectory($path) ? constant('CO_' . $moduleDirNameUpper . '_' . 'DC_DIRCREATED') : constant('CO_' . $moduleDirNameUpper . '_' . 'DC_DIRNOTCREATED');
147
        redirect_header($redirect, Constants::REDIRECT_DELAY_MEDIUM, $msg . ': ' . $path);
148
        break;
149 View Code Duplication
    case 'setdirperm':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
150
        if (\Xmf\Request::hasVar('path', 'POST')) {
151
            $path = $_POST['path'];
152
        }
153
        if (\Xmf\Request::hasVar('redirect', 'POST')) {
154
            $redirect = $_POST['redirect'];
155
        }
156
        if (\Xmf\Request::hasVar('mode', 'POST')) {
157
            $mode = $_POST['mode'];
158
        }
159
        $msg = DirectoryChecker::setDirectoryPermissions($path, $mode) ? constant('CO_' . $moduleDirNameUpper . '_' . 'DC_PERMSET') : constant('CO_' . $moduleDirNameUpper . '_' . 'DC_PERMNOTSET');
160
        redirect_header($redirect, Constants::REDIRECT_DELAY_MEDIUM, $msg . ': ' . $path);
161
        break;
162
}
163