FileChecker::compareFiles()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 15
rs 9.6111
cc 5
nc 4
nop 2
1
<?php
2
3
namespace XoopsModules\Groupmanager\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
 * Groups module
17
 *
18
 * @copyright       XOOPS Project (https://xoops.org)
19
 * @license         GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
20
 * @author          Xoops Development Team
21
 */
22
23
use Xmf\Request;
24
25
require_once \dirname(__DIR__, 4) . '/mainfile.php';
26
$moduleDirName      = \basename(\dirname(__DIR__, 2));
27
$moduleDirNameUpper = mb_strtoupper($moduleDirName);
28
\xoops_loadLanguage('filechecker', $moduleDirName);
29
30
/**
31
 * Class FileChecker
32
 * check status of a directory
33
 */
34
class FileChecker
35
{
36
    /**
37
     * @param string      $file_path
38
     * @param string|null $original_file_path
39
     * @param string      $redirectFile
40
     * @return bool|string
41
     */
42
    public static function getFileStatus($file_path, $original_file_path, $redirectFile)
43
    {
44
        global $pathIcon16;
45
46
        if (empty($file_path)) {
47
            return false;
48
        }
49
        if (null === $redirectFile) {
0 ignored issues
show
introduced by
The condition null === $redirectFile is always false.
Loading history...
50
            $redirectFile = $_SERVER['SCRIPT_NAME'];
51
        }
52
        $moduleDirName      = \basename(\dirname(__DIR__, 2));
53
        $moduleDirNameUpper = mb_strtoupper($moduleDirName);
54
        if (null === $original_file_path) {
55
            if (self::fileExists($file_path)) {
56
                $path_status = "<img src='$pathIcon16/1.png' >";
57
                $path_status .= "$file_path (" . \constant('CO_' . $moduleDirNameUpper . '_' . 'FC_AVAILABLE') . ') ';
58
            } else {
59
                $path_status = "<img src='$pathIcon16/0.png' >";
60
                $path_status .= "$file_path (" . \constant('CO_' . $moduleDirNameUpper . '_' . 'FC_NOTAVAILABLE') . ') ';
61
            }
62
        } elseif (self::compareFiles($file_path, $original_file_path)) {
63
                $path_status = "<img src='$pathIcon16/1.png' >";
64
                $path_status .= "$file_path (" . \constant('CO_' . $moduleDirNameUpper . '_' . 'FC_AVAILABLE') . ') ';
65
            } else {
66
                $path_status = "<img src='$pathIcon16/0.png' >";
67
                $path_status .= "$file_path (" . \constant('CO_' . $moduleDirNameUpper . '_' . 'FC_NOTAVAILABLE') . ') ';
68
                $path_status .= "<form action='" . $_SERVER['SCRIPT_NAME'] . "' method='post'>";
69
                $path_status .= "<input type='hidden' name='op' value='copyfile'>";
70
                $path_status .= "<input type='hidden' name='file_path' value='$file_path'>";
71
                $path_status .= "<input type='hidden' name='original_file_path' value='$original_file_path'>";
72
                $path_status .= "<input type='hidden' name='redirect' value='$redirectFile'>";
73
                $path_status .= "<button class='submit' onClick='this.form.submit();'>" . \constant('CO_' . $moduleDirNameUpper . '_' . 'FC_CREATETHEFILE') . '</button>';
74
                $path_status .= '</form>';
75
        }
76
77
        return $path_status;
78
    }
79
80
    /**
81
     * @param   $source_path
82
     * @param   $destination_path
83
     *
84
     * @return bool
85
     */
86
    public static function copyFile($source_path, $destination_path)
87
    {
88
        $source_path      = \str_replace('..', '', $source_path);
89
        $destination_path = \str_replace('..', '', $destination_path);
90
91
        return @\copy($source_path, $destination_path);
92
    }
93
94
    /**
95
     * @param   $file1_path
96
     * @param   $file2_path
97
     *
98
     * @return bool
99
     */
100
    public static function compareFiles($file1_path, $file2_path)
101
    {
102
        if (!self::fileExists($file1_path) || !self::fileExists($file2_path)) {
103
            return false;
104
        }
105
        if (\filetype($file1_path) !== \filetype($file2_path)) {
106
            return false;
107
        }
108
        if (\filesize($file1_path) !== \filesize($file2_path)) {
109
            return false;
110
        }
111
        $crc1 = mb_strtoupper(\dechex(\crc32(file_get_contents($file1_path))));
112
        $crc2 = mb_strtoupper(\dechex(\crc32(file_get_contents($file2_path))));
113
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
137
        return @\chmod($target, (int)$mode);
138
    }
139
}
140
141
$op = Request::getString('op', '', 'POST');
142
switch ($op) {
143
    case 'copyfile':
144
        if (\Xmf\Request::hasVar('original_file_path', 'POST')) {
145
            $original_file_path = $_POST['original_file_path'];
146
        }
147
        if (\Xmf\Request::hasVar('file_path', 'POST')) {
148
            $file_path = $_POST['file_path'];
149
        }
150
        if (\Xmf\Request::hasVar('redirect', 'POST')) {
151
            $redirect = $_POST['redirect'];
152
        }
153
        $msg = FileChecker::copyFile($original_file_path, $file_path) ? \constant('CO_' . $moduleDirNameUpper . '_' . 'FC_FILECOPIED') : \constant('CO_' . $moduleDirNameUpper . '_' . 'FC_FILENOTCOPIED');
154
        \redirect_header($redirect, 2, $msg . ': ' . $file_path);
155
        break;
156
}
157