Passed
Push — master ( 6af1fb...f5fe55 )
by Michael
02:53 queued 10s
created

FileChecker::compareFiles()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 14
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 14
rs 9.6111
cc 5
nc 4
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace XoopsModules\Adslight\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: Adslight
18
 *
19
 * @category        Module
20
 * @author          XOOPS Development Team <https://xoops.org>
21
 * @copyright       {@link https://xoops.org/ XOOPS Project}
22
 * @license         GPL 2.0 or later
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)
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) {
0 ignored issues
show
introduced by
The condition null === $redirectFile is always false.
Loading history...
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
        } else {
65
            if (self::compareFiles($file_path, $original_file_path)) {
66
                $path_status = "<img src='$pathIcon16/1.png' >";
67
                $path_status .= "$file_path (" . constant('CO_' . $moduleDirNameUpper . '_' . 'AVAILABLE') . ') ';
68
            } else {
69
                $path_status = "<img src='$pathIcon16/0.png' >";
70
                $path_status .= "$file_path (" . constant('CO_' . $moduleDirNameUpper . '_' . 'NOTAVAILABLE') . ') ';
71
                $path_status .= "<form action='" . $_SERVER['SCRIPT_NAME'] . "' method='post'>";
72
                $path_status .= "<input type='hidden' name='op' value='copyfile'>";
73
                $path_status .= "<input type='hidden' name='file_path' value='$file_path'>";
74
                $path_status .= "<input type='hidden' name='original_file_path' value='$original_file_path'>";
75
                $path_status .= "<input type='hidden' name='redirect' value='$redirectFile'>";
76
                $path_status .= "<button class='submit' onClick='this.form.submit();'>" . constant('CO_' . $moduleDirNameUpper . '_' . 'CREATETHEFILE') . '</button>';
77
                $path_status .= '</form>';
78
            }
79
        }
80
81
        return $path_status;
82
    }
83
84
    /**
85
     * @param   $source_path
86
     * @param   $destination_path
87
     *
88
     * @return bool
89
     */
90
    public static function copyFile($source_path, $destination_path)
91
    {
92
        $source_path      = str_replace('..', '', $source_path);
93
        $destination_path = str_replace('..', '', $destination_path);
94
95
        return @copy($source_path, $destination_path);
96
    }
97
98
    /**
99
     * @param   $file1_path
100
     * @param   $file2_path
101
     *
102
     * @return bool
103
     */
104
    public static function compareFiles($file1_path, $file2_path)
105
    {
106
        if (!self::fileExists($file1_path) || !self::fileExists($file2_path)) {
107
            return false;
108
        }
109
        if (filetype($file1_path) !== filetype($file2_path)) {
110
            return false;
111
        }
112
        if (filesize($file1_path) !== filesize($file2_path)) {
113
            return false;
114
        }
115
        $crc1 = mb_strtoupper(dechex(crc32(file_get_contents($file1_path))));
116
        $crc2 = mb_strtoupper(dechex(crc32(file_get_contents($file2_path))));
117
        return !($crc1 !== $crc2);
118
    }
119
120
    /**
121
     * @param   $file_path
122
     *
123
     * @return bool
124
     */
125
    public static function fileExists($file_path)
126
    {
127
        return is_file($file_path);
128
    }
129
130
    /**
131
     * @param     $target
132
     * @param int $mode
133
     *
134
     * @return bool
135
     */
136
    public static function setFilePermissions($target, $mode = 0777)
137
    {
138
        $target = str_replace('..', '', $target);
139
140
        return @chmod($target, (int)$mode);
141
    }
142
}
143
144
$op = Request::getString('op', '', 'POST');
145
$moduleDirName      = basename(dirname(__DIR__, 2));
146
$moduleDirNameUpper = mb_strtoupper($moduleDirName);
147
switch ($op) {
148
    case 'copyfile':
149
        if (\Xmf\Request::hasVar('original_file_path', 'POST')) {
150
            $original_file_path = $_POST['original_file_path'];
151
        }
152
        if (\Xmf\Request::hasVar('file_path', 'POST')) {
153
            $file_path = $_POST['file_path'];
154
        }
155
        if (\Xmf\Request::hasVar('redirect', 'POST')) {
156
            $redirect = $_POST['redirect'];
157
        }
158
        $msg = FileChecker::copyFile($original_file_path, $file_path) ? constant('CO_' . $moduleDirNameUpper . '_' . 'FILECOPIED') : constant('CO_' . $moduleDirNameUpper . '_' . 'FILENOTCOPIED');
159
        redirect_header($redirect, 2, $msg . ': ' . $file_path);
160
        break;
161
}
162