FileChecker::copyFile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Wfdownloads\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
 * Wfdownloads 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
 * @package         wfdownload
21
 * @since           3.23
22
 * @author          Xoops Development Team
23
 */
24
25
use Xmf\Request;
26
use XoopsModules\Wfdownloads;
27
28
require_once dirname(__DIR__, 4) . '/mainfile.php';
29
$moduleDirName = \basename(dirname(__DIR__, 2));
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 string|null $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
48
        if (empty($file_path)) {
49
            return false;
50
        }
51
        if (null === $redirectFile) {
52
            $redirectFile = $_SERVER['SCRIPT_NAME'];
53
        }
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 (" . \_FC_WFDOWNLOADS_AVAILABLE . ') ';
58
            } else {
59
                $path_status = "<img src='$pathIcon16/0.png' >";
60
                $path_status .= "$file_path (" . \_FC_WFDOWNLOADS_NOTAVAILABLE . ') ';
61
            }
62
        } else {
63
            if (self::compareFiles($file_path, $original_file_path)) {
64
                $path_status = "<img src='$pathIcon16/1.png' >";
65
                $path_status .= "$file_path (" . \_FC_WFDOWNLOADS_AVAILABLE . ') ';
66
            } else {
67
                $path_status = "<img src='$pathIcon16/0.png' >";
68
                $path_status .= "$file_path (" . \_FC_WFDOWNLOADS_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();'>" . \_FC_WFDOWNLOADS_CREATETHEFILE . '</button>';
75
                $path_status .= '</form>';
76
            }
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
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)
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)
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)
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) ? \_FC_WFDOWNLOADS_FILECOPIED : \_FC_WFDOWNLOADS_FILENOTCOPIED;
156
        \redirect_header($redirect, 2, $msg . ': ' . $file_path);
157
        break;
158
}
159