FileChecker   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
eloc 44
c 1
b 0
f 0
dl 0
loc 104
rs 10

5 Methods

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