Passed
Pull Request — master (#41)
by Michael
13:46
created

FileChecker::copyFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 6
rs 10
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\News\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
 *
17
 * @copyright       XOOPS Project (https://xoops.org)
18
 * @license         GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
19
 * @author          Xoops Development Team
20
 */
21
22
use Xmf\Request;
23
use XoopsModules\News;
24
//defined('XOOPS_ROOT_PATH') || die('XOOPS root path not defined');
25
26
require_once \dirname(__DIR__, 4) . '/mainfile.php';
27
$moduleDirName = \basename(\dirname(__DIR__, 2));
28
$moduleDirNameUpper = \mb_strtoupper($moduleDirName);
29
\xoops_loadLanguage('filechecker', $moduleDirName);
30
31
/**
32
 * Class FileChecker
33
 * check status of a directory
34
 */
35
class FileChecker
36
{
37
    /**
38
     * @param string      $file_path
39
     * @param string|null $original_file_path
40
     * @param string|null $redirectFile
41
     * @return bool|string
42
     */
43
    public static function getFileStatus(string $file_path, ?string $original_file_path = null, ?string $redirectFile = null)
44
    {
45
        global $pathIcon16;
46
47
        if (empty($file_path)) {
48
            return false;
49
        }
50
        if (null === $redirectFile) {
51
            $redirectFile = $_SERVER['SCRIPT_NAME'];
52
        }
53
        $moduleDirName      = \basename(\dirname(__DIR__, 2));
54
        $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
55
        if (null === $original_file_path) {
56
            if (self::fileExists($file_path)) {
57
                $path_status = "<img src='$pathIcon16/1.png' >";
58
                $path_status .= "$file_path (" . \constant('CO_' . $moduleDirNameUpper . '_' . 'FC_AVAILABLE') . ') ';
59
            } else {
60
                $path_status = "<img src='$pathIcon16/0.png' >";
61
                $path_status .= "$file_path (" . \constant('CO_' . $moduleDirNameUpper . '_' . 'FC_NOTAVAILABLE') . ') ';
62
            }
63
        } elseif (self::compareFiles($file_path, $original_file_path)) {
64
                $path_status = "<img src='$pathIcon16/1.png' >";
65
                $path_status .= "$file_path (" . \constant('CO_' . $moduleDirNameUpper . '_' . 'FC_AVAILABLE') . ') ';
66
            } else {
67
                $path_status = "<img src='$pathIcon16/0.png' >";
68
                $path_status .= "$file_path (" . \constant('CO_' . $moduleDirNameUpper . '_' . 'FC_NOTAVAILABLE') . ') ';
69
                $path_status .= "<form action='" . $_SERVER['SCRIPT_NAME'] . "' method='post'>";
70
                $path_status .= "<input type='hidden' name='op' value='copyfile'>";
71
                $path_status .= "<input type='hidden' name='file_path' value='$file_path'>";
72
                $path_status .= "<input type='hidden' name='original_file_path' value='$original_file_path'>";
73
                $path_status .= "<input type='hidden' name='redirect' value='$redirectFile'>";
74
                $path_status .= "<button class='submit' onClick='this.form.submit();'>" . \constant('CO_' . $moduleDirNameUpper . '_' . 'FC_CREATETHEFILE') . '</button>';
75
                $path_status .= '</form>';
76
            }
77
78
79
        return $path_status;
80
    }
81
82
    /**
83
     * @param   string $source_path
84
     * @param   string $destination_path
85
     *
86
     * @return bool
87
     */
88
    public static function copyFile(string $source_path, string $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   string $file1_path
98
     * @param   string $file2_path
99
     *
100
     * @return bool
101
     */
102
    public static function compareFiles(string $file1_path, string $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 string $file_path
121
     *
122
     * @return bool
123
     */
124
    public static function fileExists(string $file_path): bool
125
    {
126
        return \is_file($file_path);
127
    }
128
129
    /**
130
     * @param string $target
131
     * @param int $mode
132
     *
133
     * @return bool
134
     */
135
    public static function setFilePermissions(string $target, int $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
switch ($op) {
145
    case 'copyfile':
146
        if (Request::hasVar('original_file_path', 'POST')) {
147
            $original_file_path = $_POST['original_file_path'];
148
        }
149
        if (Request::hasVar('file_path', 'POST')) {
150
            $file_path = $_POST['file_path'];
151
        }
152
        if (Request::hasVar('redirect', 'POST')) {
153
            $redirect = $_POST['redirect'];
154
        }
155
        $msg = FileChecker::copyFile($original_file_path, $file_path) ? \constant('CO_' . $moduleDirNameUpper . '_' . 'FC_FILECOPIED') : \constant('CO_' . $moduleDirNameUpper . '_' . 'FC_FILENOTCOPIED');
156
        \redirect_header($redirect, 2, $msg . ': ' . $file_path);
157
        break;
158
}
159