FileChecker::copyFile()   A
last analyzed

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
2
3
namespace XoopsModules\Xoopstube\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
 * Xoopstube 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
use XoopsModules\Xoopstube;
25
26
//defined('XOOPS_ROOT_PATH') || die('XOOPS root path not defined');
27
28
require_once \dirname(__DIR__, 4) . '/mainfile.php';
29
$moduleDirName      = \basename(\dirname(__DIR__, 2));
30
$moduleDirNameUpper = mb_strtoupper($moduleDirName);
31
\xoops_loadLanguage('filechecker', $moduleDirName);
32
33
/**
34
 * Class FileChecker
35
 * check status of a directory
36
 */
37
class FileChecker
38
{
39
    /**
40
     * @param string      $file_path
41
     * @param string|null $original_file_path
42
     * @param string|null $redirectFile
43
     * @return bool|string
44
     */
45
    public static function getFileStatus($file_path, $original_file_path = null, $redirectFile = null)
46
    {
47
        global $pathIcon16;
48
49
        if (empty($file_path)) {
50
            return false;
51
        }
52
        if (null === $redirectFile) {
53
            $redirectFile = $_SERVER['SCRIPT_NAME'];
54
        }
55
        $moduleDirName      = \basename(\dirname(__DIR__, 2));
56
        $moduleDirNameUpper = mb_strtoupper($moduleDirName);
57
        if (null === $original_file_path) {
58
            if (self::fileExists($file_path)) {
59
                $path_status = "<img src='$pathIcon16/1.png' >";
60
                $path_status .= "$file_path (" . \constant('CO_' . $moduleDirNameUpper . '_' . 'FC_AVAILABLE') . ') ';
61
            } else {
62
                $path_status = "<img src='$pathIcon16/0.png' >";
63
                $path_status .= "$file_path (" . \constant('CO_' . $moduleDirNameUpper . '_' . 'FC_NOTAVAILABLE') . ') ';
64
            }
65
        } elseif (self::compareFiles($file_path, $original_file_path)) {
66
                $path_status = "<img src='$pathIcon16/1.png' >";
67
                $path_status .= "$file_path (" . \constant('CO_' . $moduleDirNameUpper . '_' . 'FC_AVAILABLE') . ') ';
68
            } else {
69
                $path_status = "<img src='$pathIcon16/0.png' >";
70
                $path_status .= "$file_path (" . \constant('CO_' . $moduleDirNameUpper . '_' . 'FC_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 . '_' . 'FC_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
118
        return !($crc1 !== $crc2);
119
    }
120
121
    /**
122
     * @param   $file_path
123
     *
124
     * @return bool
125
     */
126
    public static function fileExists($file_path)
127
    {
128
        return \is_file($file_path);
129
    }
130
131
    /**
132
     * @param     $target
133
     * @param int $mode
134
     *
135
     * @return bool
136
     */
137
    public static function setFilePermissions($target, $mode = 0777)
138
    {
139
        $target = \str_replace('..', '', $target);
140
141
        return @\chmod($target, (int)$mode);
142
    }
143
}
144
145
$op = Request::getString('op', '', 'POST');
146
switch ($op) {
147
    case 'copyfile':
148
        if (\Xmf\Request::hasVar('original_file_path', 'POST')) {
149
            $original_file_path = $_POST['original_file_path'];
150
        }
151
        if (\Xmf\Request::hasVar('file_path', 'POST')) {
152
            $file_path = $_POST['file_path'];
153
        }
154
        if (\Xmf\Request::hasVar('redirect', 'POST')) {
155
            $redirect = $_POST['redirect'];
156
        }
157
        $msg = FileChecker::copyFile($original_file_path, $file_path) ? \constant('CO_' . $moduleDirNameUpper . '_' . 'FC_FILECOPIED') : \constant('CO_' . $moduleDirNameUpper . '_' . 'FC_FILENOTCOPIED');
158
        \redirect_header($redirect, 2, $msg . ': ' . $file_path);
159
        break;
160
}
161