FileChecker::compareFiles()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 4
nop 2
dl 0
loc 14
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace XoopsModules\Countdown\Common;
6
7
/*
8
 You may not change or alter any portion of this comment or credits
9
 of supporting developers from this source code or any supporting source code
10
 which is considered copyrighted (c) material of the original comment or credit authors.
11
 
12
 This program is distributed in the hope that it will be useful,
13
 but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
*/
16
/**
17
 * Module: Countdown
18
 *
19
 * @category        Module
20
 * @package         countdown
21
 * @author          XOOPS Development Team <https://xoops.org>
22
 * @copyright       {@link https://xoops.org/ XOOPS Project}
23
 * @license         GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
24
 */
25
require_once dirname(dirname(dirname(dirname(__DIR__)))) . '/mainfile.php';
26
$moduleDirName = basename(dirname(dirname(__DIR__)));
27
xoops_loadLanguage('common', $moduleDirName);
28
29
/**
30
 * Class FileChecker
31
 * check status of a directory
32
 */
33
class FileChecker
34
{
35
    /**
36
     * @param             $file_path
37
     * @param null|string $original_file_path
38
     * @param             $redirectFile
39
     *
40
     * @return bool|string
41
     * @internal param int $mode
42
     * @internal param $path
43
     */
44
    public static function getFileStatus($file_path, $original_file_path = null, $redirectFile)
45
    {
46
        global $pathIcon16;
47
        $moduleDirName      = basename(dirname(dirname(__DIR__)));
48
        $moduleDirNameUpper = mb_strtoupper($moduleDirName);
49
        if (empty($file_path)) {
50
            return false;
51
        }
52
        if (null === $redirectFile) {
53
            $redirectFile = $_SERVER['SCRIPT_NAME'];
54
        }
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 . '_' . 'AVAILABLE') . ') ';
59
            } else {
60
                $path_status = "<img src='$pathIcon16/0.png' >";
61
                $path_status .= "$file_path (" . constant('CO_' . $moduleDirNameUpper . '_' . 'NOTAVAILABLE') . ') ';
62
            }
63
        } else {
64
            if (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)
89
    {
90
        $source_path      = str_replace('..', '', $source_path);
91
        $destination_path = str_replace('..', '', $destination_path);
92
        return @copy($source_path, $destination_path);
93
    }
94
95
    /**
96
     * @param   $file1_path
97
     * @param   $file2_path
98
     *
99
     * @return bool
100
     */
101
    public static function compareFiles($file1_path, $file2_path)
102
    {
103
        if (!self::fileExists($file1_path) || !self::fileExists($file2_path)) {
104
            return false;
105
        }
106
        if (filetype($file1_path) !== filetype($file2_path)) {
107
            return false;
108
        }
109
        if (filesize($file1_path) !== filesize($file2_path)) {
110
            return false;
111
        }
112
        $crc1 = mb_strtoupper(dechex(crc32(file_get_contents($file1_path))));
113
        $crc2 = mb_strtoupper(dechex(crc32(file_get_contents($file2_path))));
114
        return !($crc1 !== $crc2);
115
    }
116
117
    /**
118
     * @param   $file_path
119
     *
120
     * @return bool
121
     */
122
    public static function fileExists($file_path)
123
    {
124
        return is_file($file_path);
125
    }
126
127
    /**
128
     * @param     $target
129
     * @param int $mode
130
     *
131
     * @return bool
132
     */
133
    public static function setFilePermissions($target, $mode = 0777)
134
    {
135
        $target = str_replace('..', '', $target);
136
        return @chmod($target, (int)$mode);
137
    }
138
}
139
140
$op                 = \Xmf\Request::getString('op', '', 'POST');
141
$moduleDirName      = basename(dirname(dirname(__DIR__)));
142
$moduleDirNameUpper = mb_strtoupper($moduleDirName);
143
switch ($op) {
144
    case 'copyfile':
145
        if (\Xmf\Request::hasVar('original_file_path', 'POST')) {
146
            $original_file_path = $_POST['original_file_path'];
147
        }
148
        if (\Xmf\Request::hasVar('file_path', 'POST')) {
149
            $file_path = $_POST['file_path'];
150
        }
151
        if (\Xmf\Request::hasVar('redirect', 'POST')) {
152
            $redirect = $_POST['redirect'];
153
        }
154
        $msg = FileChecker::copyFile($original_file_path, $file_path) ? constant('CO_' . $moduleDirNameUpper . '_' . 'FILECOPIED') : constant('CO_' . $moduleDirNameUpper . '_' . 'FILENOTCOPIED');
155
        redirect_header($redirect, 2, $msg . ': ' . $file_path);
156
        break;
157
}
158