forked from XoopsModules25x/modulebuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileChecker.php
More file actions
159 lines (141 loc) · 5.57 KB
/
FileChecker.php
File metadata and controls
159 lines (141 loc) · 5.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php declare(strict_types=1);
namespace XoopsModules\Modulebuilder\Common;
/*
You may not change or alter any portion of this comment or credits
of supporting developers from this source code or any supporting source code
which is considered copyrighted (c) material of the original comment or credit authors.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
/**
* Modulebuilder module
*
* @copyright XOOPS Project (https://xoops.org)
* @license GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
* @author Xoops Development Team
*/
use Xmf\Request;
use XoopsModules\Modulebuilder;
//\defined('XOOPS_ROOT_PATH') || die('XOOPS root path not defined');
require_once \dirname(__DIR__, 4) . '/mainfile.php';
$moduleDirName = \basename(\dirname(__DIR__, 2));
$moduleDirNameUpper = \mb_strtoupper($moduleDirName);
\xoops_loadLanguage('filechecker', $moduleDirName);
/**
* Class FileChecker
* check status of a directory
*/
class FileChecker
{
/**
* @param string $file_path
* @param string $redirectFile
* @param string|null $original_file_path
*
* @return bool|string
*/
public static function getFileStatus(string $file_path, string $redirectFile, string $original_file_path = null)
{
$pathIcon16 = \Xmf\Module\Admin::iconUrl('', '16');
if (empty($file_path)) {
return false;
}
if (null === $redirectFile) {
$redirectFile = $_SERVER['SCRIPT_NAME'];
}
$moduleDirName = \basename(\dirname(__DIR__, 2));
$moduleDirNameUpper = \mb_strtoupper($moduleDirName);
if (null === $original_file_path) {
if (self::fileExists($file_path)) {
$path_status = "<img src='$pathIcon16/1.png' >";
$path_status .= "$file_path (" . \constant('CO_' . $moduleDirNameUpper . '_' . 'FC_AVAILABLE') . ') ';
} else {
$path_status = "<img src='$pathIcon16/0.png' >";
$path_status .= "$file_path (" . \constant('CO_' . $moduleDirNameUpper . '_' . 'FC_NOTAVAILABLE') . ') ';
}
} else {
if (self::compareFiles($file_path, $original_file_path)) {
$path_status = "<img src='$pathIcon16/1.png' >";
$path_status .= "$file_path (" . \constant('CO_' . $moduleDirNameUpper . '_' . 'FC_AVAILABLE') . ') ';
} else {
$path_status = "<img src='$pathIcon16/0.png' >";
$path_status .= "$file_path (" . \constant('CO_' . $moduleDirNameUpper . '_' . 'FC_NOTAVAILABLE') . ') ';
$path_status .= "<form action='" . $_SERVER['SCRIPT_NAME'] . "' method='post'>";
$path_status .= "<input type='hidden' name='op' value='copyfile'>";
$path_status .= "<input type='hidden' name='file_path' value='$file_path'>";
$path_status .= "<input type='hidden' name='original_file_path' value='$original_file_path'>";
$path_status .= "<input type='hidden' name='redirect' value='$redirectFile'>";
$path_status .= "<button class='submit' onClick='this.form.submit();'>" . \constant('CO_' . $moduleDirNameUpper . '_' . 'FC_CREATETHEFILE') . '</button>';
$path_status .= '</form>';
}
}
return $path_status;
}
/**
* @param $source_path
* @param $destination_path
*
* @return bool
*/
public static function copyFile($source_path, $destination_path)
{
$source_path = \str_replace('..', '', $source_path);
$destination_path = \str_replace('..', '', $destination_path);
return @\copy($source_path, $destination_path);
}
/**
* @param $file1_path
* @param $file2_path
*
* @return bool
*/
public static function compareFiles($file1_path, $file2_path)
{
if (!self::fileExists($file1_path) || !self::fileExists($file2_path)) {
return false;
}
if (\filetype($file1_path) !== \filetype($file2_path)) {
return false;
}
if (\filesize($file1_path) !== \filesize($file2_path)) {
return false;
}
$crc1 = \mb_strtoupper(\dechex(\crc32(\file_get_contents($file1_path))));
$crc2 = \mb_strtoupper(\dechex(\crc32(\file_get_contents($file2_path))));
return !($crc1 !== $crc2);
}
/**
* @param $file_path
*
* @return bool
*/
public static function fileExists($file_path)
{
return \is_file($file_path);
}
/**
* @param $target
* @param int $mode
*
* @return bool
*/
public static function setFilePermissions($target, int $mode = 0777)
{
$target = \str_replace('..', '', $target);
return @\chmod($target, (int)$mode);
}
}
$op = Request::getString('op', '', 'POST');
if ($op == 'copyfile') {
if (!\Xmf\Request::hasVar('original_file_path', 'POST')
|| !\Xmf\Request::hasVar('file_path', 'POST')
|| !\Xmf\Request::hasVar('redirect', 'POST')) {
return;
}
$original_file_path = $_POST['original_file_path'];
$file_path = $_POST['file_path'];
$redirect = $_POST['redirect'];
$msg = FileChecker::copyFile($original_file_path, $file_path) ? \constant('CO_' . $moduleDirNameUpper . '_' . 'FC_FILECOPIED') : \constant('CO_' . $moduleDirNameUpper . '_' . 'FC_FILENOTCOPIED');
\redirect_header($redirect, 2, $msg . ': ' . $file_path);
}