FileChecker   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 45
dl 0
loc 126
rs 10
c 0
b 0
f 0
wmc 14

5 Methods

Rating   Name   Duplication   Size   Complexity  
A compareFiles() 0 19 5
A copyFile() 0 7 1
A fileExists() 0 3 1
B getFileStatus() 0 53 6
A setFilePermissions() 0 5 1
1
<?php
2
3
namespace XoopsModules\Chess\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
 * Chess 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\Chess;
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('filechecker', $moduleDirName);
32
33
/**
34
 * Class FileChecker
35
 * check status of a directory
36
 */
37
class FileChecker
38
{
39
    /**
40
     * @param string      $file_path
41
     * @param string|null $original_file_path
42
     * @param string      $redirectFile
43
     * @return bool|string
44
     */
45
    public static function getFileStatus($file_path, $original_file_path, $redirectFile)
46
    {
47
        global $pathIcon16;
48
49
        if (empty($file_path)) {
50
            return false;
51
        }
52
53
        if (null === $redirectFile) {
0 ignored issues
show
introduced by
The condition null === $redirectFile is always false.
Loading history...
54
            $redirectFile = $_SERVER['SCRIPT_NAME'];
55
        }
56
57
        $moduleDirName = \basename(\dirname(__DIR__, 2));
58
59
        $moduleDirNameUpper = mb_strtoupper($moduleDirName);
60
61
        if (null === $original_file_path) {
62
            if (self::fileExists($file_path)) {
63
                $path_status = "<img src='$pathIcon16/1.png' >";
64
65
                $path_status .= "$file_path (" . \constant('CO_' . $moduleDirNameUpper . '_' . 'FC_AVAILABLE') . ') ';
66
            } else {
67
                $path_status = "<img src='$pathIcon16/0.png' >";
68
69
                $path_status .= "$file_path (" . \constant('CO_' . $moduleDirNameUpper . '_' . 'FC_NOTAVAILABLE') . ') ';
70
            }
71
        } else {
72
            if (self::compareFiles($file_path, $original_file_path)) {
73
                $path_status = "<img src='$pathIcon16/1.png' >";
74
75
                $path_status .= "$file_path (" . \constant('CO_' . $moduleDirNameUpper . '_' . 'FC_AVAILABLE') . ') ';
76
            } else {
77
                $path_status = "<img src='$pathIcon16/0.png' >";
78
79
                $path_status .= "$file_path (" . \constant('CO_' . $moduleDirNameUpper . '_' . 'FC_NOTAVAILABLE') . ') ';
80
81
                $path_status .= "<form action='" . $_SERVER['SCRIPT_NAME'] . "' method='post'>";
82
83
                $path_status .= "<input type='hidden' name='op' value='copyfile'>";
84
85
                $path_status .= "<input type='hidden' name='file_path' value='$file_path'>";
86
87
                $path_status .= "<input type='hidden' name='original_file_path' value='$original_file_path'>";
88
89
                $path_status .= "<input type='hidden' name='redirect' value='$redirectFile'>";
90
91
                $path_status .= "<button class='submit' onClick='this.form.submit();'>" . \constant('CO_' . $moduleDirNameUpper . '_' . 'FC_CREATETHEFILE') . '</button>';
92
93
                $path_status .= '</form>';
94
            }
95
        }
96
97
        return $path_status;
98
    }
99
100
    /**
101
     * @param   $source_path
102
     * @param   $destination_path
103
     *
104
     * @return bool
105
     */
106
    public static function copyFile($source_path, $destination_path)
107
    {
108
        $source_path = \str_replace('..', '', $source_path);
109
110
        $destination_path = \str_replace('..', '', $destination_path);
111
112
        return @\copy($source_path, $destination_path);
113
    }
114
115
    /**
116
     * @param   $file1_path
117
     * @param   $file2_path
118
     *
119
     * @return bool
120
     */
121
    public static function compareFiles($file1_path, $file2_path)
122
    {
123
        if (!self::fileExists($file1_path) || !self::fileExists($file2_path)) {
124
            return false;
125
        }
126
127
        if (\filetype($file1_path) !== \filetype($file2_path)) {
128
            return false;
129
        }
130
131
        if (\filesize($file1_path) !== \filesize($file2_path)) {
132
            return false;
133
        }
134
135
        $crc1 = mb_strtoupper(\dechex(\crc32(file_get_contents($file1_path))));
136
137
        $crc2 = mb_strtoupper(\dechex(\crc32(file_get_contents($file2_path))));
138
139
        return !($crc1 !== $crc2);
140
    }
141
142
    /**
143
     * @param   $file_path
144
     *
145
     * @return bool
146
     */
147
    public static function fileExists($file_path)
148
    {
149
        return \is_file($file_path);
150
    }
151
152
    /**
153
     * @param     $target
154
     * @param int $mode
155
     *
156
     * @return bool
157
     */
158
    public static function setFilePermissions($target, $mode = 0777)
159
    {
160
        $target = \str_replace('..', '', $target);
161
162
        return @\chmod($target, (int)$mode);
163
    }
164
}
165
166
$op = Request::getString('op', '', 'POST');
167
switch ($op) {
168
    case 'copyfile':
169
        if (\Xmf\Request::hasVar('original_file_path', 'POST')) {
170
            $original_file_path = $_POST['original_file_path'];
171
        }
172
        if (\Xmf\Request::hasVar('file_path', 'POST')) {
173
            $file_path = $_POST['file_path'];
174
        }
175
        if (\Xmf\Request::hasVar('redirect', 'POST')) {
176
            $redirect = $_POST['redirect'];
177
        }
178
        $msg = FileChecker::copyFile($original_file_path, $file_path) ? \constant('CO_' . $moduleDirNameUpper . '_' . 'FC_FILECOPIED') : \constant('CO_' . $moduleDirNameUpper . '_' . 'FC_FILENOTCOPIED');
179
        \redirect_header($redirect, 2, $msg . ': ' . $file_path);
180
        break;
181
}
182