Passed
Push — master ( 891b58...df23ea )
by Goffy
03:28
created

cloneFileFolder()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 9
nc 4
nop 4
dl 0
loc 18
rs 9.6111
c 1
b 0
f 0
1
<?php
2
3
/*
4
 You may not change or alter any portion of this comment or credits
5
 of supporting developers from this source code or any supporting source code
6
 which is considered copyrighted (c) material of the original comment or credit authors.
7
8
 This program is distributed in the hope that it will be useful,
9
 but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 */
12
13
/**
14
 * modulebuilder module.
15
 *
16
 * @copyright       XOOPS Project (https://xoops.org)
17
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
18
 *
19
 * @since           2.5.5
20
 *
21
 * @author          Txmod Xoops <[email protected]>
22
 *
23
 */
24
25
// Define main template
26
$templateMain = 'modulebuilder_index.tpl';
27
28
include __DIR__ . '/header.php';
29
// Recovered value of argument op in the URL $
30
$op    = \Xmf\Request::getString('op', 'list');
31
32
switch ($op) {
33
    case 'list':
34
    default:
35
        
36
37
        break;
38
    case 'fqnreplacer':
39
        $src_path = TDMC_PATH ;
40
        $dst_path = TDMC_UPLOAD_REPOSITORY_PATH . '/fqnreplacer';
41
42
        $patterns = [
43
            //remove backslash if alreadyin order to avoid \\
44
            '\array_diff('             => 'array_diff(',
45
            '\array_filter('           => 'array_filter(',
46
            '\array_key_exists('       => 'array_key_exists(',
47
            '\array_keys('             => 'array_keys(',
48
            '\array_search('           => 'array_search(',
49
            '\array_slice('            => 'array_slice(',
50
            '\array_unshift('          => 'array_unshift(',
51
            '\assert('                 => 'assert(',
52
            '\basename('               => 'basename(',
53
            '\boolval('                => 'boolval(',
54
            '\call_user_func('         => 'call_user_func(',
55
            '\call_user_func_array('   => 'call_user_func_array(',
56
            '\chr('                    => 'chr(',
57
            '\class_exists('           => 'class_exists(',
58
            '\closedir('               => 'closedir(',
59
            '\constant('               => 'constant(',
60
            '\copy('                   => 'copy(',
61
            '\count('                  => 'count(',
62
            '\curl_close('             => 'curl_close(',
63
            '\curl_error('             => 'curl_error(',
64
            '\curl_exec('              => 'curl_exec(',
65
            '\curl_file_create('       => 'curl_file_create(',
66
            '\curl_getinfo('           => 'curl_getinfo(',
67
            '\curl_init('              => 'curl_init(',
68
            '\curl_setopt('            => 'curl_setopt(',
69
            '\define('                 => 'define(',
70
            '\defined('                => 'defined(',
71
            '\dirname('                => 'dirname(',
72
            '\doubleval('              => 'doubleval(',
73
            '\explode('                => 'explode(',
74
            '\extension_loaded('       => 'extension_loaded(',
75
            '\file_exists('            => 'file_exists(',
76
            '\finfo_open('             => 'finfo_open(',
77
            '\floatval('               => 'floatval(',
78
            '\floor('                  => 'floor(',
79
            '\formatTimestamp('        => 'formatTimestamp(',
80
            '\func_get_args('          => 'func_get_args(',
81
            '\func_num_args('          => 'func_num_args(',
82
            '\function_exists('        => 'function_exists(',
83
            '\get_called_class('       => 'get_called_class(',
84
            '\get_class('              => 'get_class(',
85
            '\getimagesize('           => 'getimagesize(',
86
            '\gettype('                => 'gettype(',
87
            '\imagecopyresampled('     => 'imagecopyresampled(',
88
            '\imagecreatefromgif('     => 'imagecreatefromgif(',
89
            '\imagecreatefromjpeg('    => 'imagecreatefromjpeg(',
90
            '\imagecreatefrompng('     => 'imagecreatefrompng(',
91
            '\imagecreatefromstring('  => 'imagecreatefromstring(',
92
            '\imagecreatetruecolor('   => 'imagecreatetruecolor(',
93
            '\imagedestroy('           => 'imagedestroy(',
94
            '\imagegif('               => 'imagegif(',
95
            '\imagejpeg('              => 'imagejpeg(',
96
            '\imagepng('               => 'imagepng(',
97
            '\imagerotate('            => 'imagerotate(',
98
            '\imagesx('                => 'imagesx(',
99
            '\imagesy('                => 'imagesy(',
100
            '\implode('                => 'implode(',
101
            '\in_array('               => 'in_array(',
102
            '\ini_get('                => 'ini_get(',
103
            '\intval('                 => 'intval(',
104
            '\is_array('               => 'is_array(',
105
            '\is_bool('                => 'is_bool(',
106
            '\is_callable('            => 'is_callable(',
107
            '\is_dir('                 => 'is_dir(',
108
            '\is_double('              => 'is_double(',
109
            '\is_float('               => 'is_float(',
110
            '\is_int('                 => 'is_int(',
111
            '\is_integer('             => 'is_integer(',
112
            '\is_link('                => 'is_link(',
113
            '\is_long('                => 'is_long(',
114
            '\is_null('                => 'is_null(',
115
            '\is_object('              => 'is_object(',
116
            '\is_real('                => 'is_real(',
117
            '\is_resource('            => 'is_resource(',
118
            '\is_string('              => 'is_string(',
119
            '\json_decode('            => 'json_decode(',
120
            '\json_encode('            => 'json_encode(',
121
            '\mime_content_type('      => 'mime_content_type(',
122
            '\mkdir('                  => 'mkdir(',
123
            '\opendir('                => 'opendir(',
124
            '\ord('                    => 'ord(',
125
            '\pathinfo('               => 'pathinfo(',
126
            '\preg_match('             => 'preg_match(',
127
            '\preg_match_all('         => 'preg_match_all(',
128
            '\preg_replace('           => 'preg_replace(',
129
            '\readdir('                => 'readdir(',
130
            '\readlink('               => 'readlink(',
131
            '\redirect_header('        => 'redirect_header(',
132
            '\rename('                 => 'rename(',
133
            '\rmdir('                  => 'rmdir(',
134
            '\round('                  => 'round(',
135
            '\scandir('                => 'scandir(',
136
            '\sprintf('                => 'sprintf(',
137
            '\str_replace('            => 'str_replace(',
138
            '\strip_tags('             => 'strip_tags(',
139
            '\strlen('                 => 'strlen(',
140
            '\strpos('                 => 'strpos(',
141
            '\strtotime('              => 'strtotime(',
142
            '\strval('                 => 'strval(',
143
            '\substr('                 => 'substr(',
144
            '\symlink('                => 'symlink(',
145
            '\time()'                  => 'time()',
146
            '\trigger_error('          => 'trigger_error(',
147
            '\trim('                   => 'trim(',
148
            '\ucfirst('                => 'ucfirst(',
149
            '\unlink('                 => 'unlink(',
150
            '\version_compare('        => 'version_compare(',
151
            '\xoops_getHandler('       => 'xoops_getHandler(',
152
            '\xoops_load('             => 'xoops_load(',
153
            '\xoops_loadLanguage('     => 'xoops_loadLanguage(',
154
155
            //add backslash to all functions
156
            'array_diff('              => '\array_diff(',
157
            'array_filter('            => '\array_filter(',
158
            'array_key_exists('        => '\array_key_exists(',
159
            'array_keys('              => '\array_keys(',
160
            'array_search('            => '\array_search(',
161
            'array_slice('             => '\array_slice(',
162
            'array_unshift('           => '\array_unshift(',
163
            'assert('                  => '\assert(',
164
            'basename('                => '\basename(',
165
            'boolval('                 => '\boolval(',
166
            'call_user_func('          => '\call_user_func(',
167
            'call_user_func_array('    => '\call_user_func_array(',
168
            'chr('                     => '\chr(',
169
            'class_exists('            => '\class_exists(',
170
            'closedir('                => '\closedir(',
171
            'constant('                => '\constant(',
172
            'copy('                    => '\copy(',
173
            'count('                   => '\count(',
174
            'curl_close('              => '\curl_close(',
175
            'curl_error('              => '\curl_error(',
176
            'curl_exec('               => '\curl_exec(',
177
            'curl_file_create('        => '\curl_file_create(',
178
            'curl_getinfo('            => '\curl_getinfo(',
179
            'curl_init('               => '\curl_init(',
180
            'curl_setopt('             => '\curl_setopt(',
181
            'define('                  => '\define(',
182
            'defined('                 => '\defined(',
183
            'dirname('                 => '\dirname(',
184
            'doubleval('               => '\doubleval(',
185
            'explode('                 => '\explode(',
186
            'extension_loaded('        => '\extension_loaded(',
187
            'file_exists('             => '\file_exists(',
188
            'finfo_open('              => '\finfo_open(',
189
            'floatval('                => '\floatval(',
190
            'floor('                   => '\floor(',
191
            'formatTimestamp('         => '\formatTimestamp(',
192
            'func_get_args('           => '\func_get_args(',
193
            'func_num_args('           => '\func_num_args(',
194
            'function_exists('         => '\function_exists(',
195
            'get_called_class('        => '\get_called_class(',
196
            'get_class('               => '\get_class(',
197
            'getimagesize('            => '\getimagesize(',
198
            'gettype('                 => '\gettype(',
199
            'imagecopyresampled('      => '\imagecopyresampled(',
200
            'imagecreatefromgif('      => '\imagecreatefromgif(',
201
            'imagecreatefromjpeg('     => '\imagecreatefromjpeg(',
202
            'imagecreatefrompng('      => '\imagecreatefrompng(',
203
            'imagecreatefromstring('   => '\imagecreatefromstring(',
204
            'imagecreatetruecolor('    => '\imagecreatetruecolor(',
205
            'imagedestroy('            => '\imagedestroy(',
206
            'imagegif('                => '\imagegif(',
207
            'imagejpeg('               => '\imagejpeg(',
208
            'imagepng('                => '\imagepng(',
209
            'imagerotate('             => '\imagerotate(',
210
            'imagesx('                 => '\imagesx(',
211
            'imagesy('                 => '\imagesy(',
212
            'implode('                 => '\implode(',
213
            'in_array('                => '\in_array(',
214
            'ini_get('                 => '\ini_get(',
215
            'intval('                  => '\intval(',
216
            'is_array('                => '\is_array(',
217
            'is_bool('                 => '\is_bool(',
218
            'is_callable('             => '\is_callable(',
219
            'is_dir('                  => '\is_dir(',
220
            'is_double('               => '\is_double(',
221
            'is_float('                => '\is_float(',
222
            'is_int('                  => '\is_int(',
223
            'is_integer('              => '\is_integer(',
224
            'is_link('                 => '\is_link(',
225
            'is_long('                 => '\is_long(',
226
            'is_null('                 => '\is_null(',
227
            'is_object('               => '\is_object(',
228
            'is_real('                 => '\is_real(',
229
            'is_resource('             => '\is_resource(',
230
            'is_string('               => '\is_string(',
231
            'json_decode('             => '\json_decode(',
232
            'json_encode('             => '\json_encode(',
233
            'mime_content_type('       => '\mime_content_type(',
234
            'mkdir('                   => '\mkdir(',
235
            'opendir('                 => '\opendir(',
236
            'ord('                     => '\ord(',
237
            'pathinfo('                => '\pathinfo(',
238
            'preg_match('              => '\preg_match(',
239
            'preg_match_all('          => '\preg_match_all(',
240
            'preg_replace('            => '\preg_replace(',
241
            'readdir('                 => '\readdir(',
242
            'readlink('                => '\readlink(',
243
            'redirect_header('         => '\redirect_header(',
244
            'rename('                  => '\rename(',
245
            'rmdir('                   => '\rmdir(',
246
            'round('                   => '\round(',
247
            'scandir('                 => '\scandir(',
248
            'sprintf('                 => '\sprintf(',
249
            'str_replace('             => '\str_replace(',
250
            'strip_tags('              => '\strip_tags(',
251
            'strlen('                  => '\strlen(',
252
            'strpos('                  => '\strpos(',
253
            'strtotime('               => '\strtotime(',
254
            'strval('                  => '\strval(',
255
            'substr('                  => '\substr(',
256
            'symlink('                 => '\symlink(',
257
            'time()'                   => '\time()',
258
            'trigger_error('           => '\trigger_error(',
259
            'trim('                    => '\trim(',
260
            'ucfirst('                 => '\ucfirst(',
261
            'unlink('                  => '\unlink(',
262
            'version_compare('         => '\version_compare(',
263
            'xoops_getHandler('        => '\xoops_getHandler(',
264
            'xoops_load('              => '\xoops_load(',
265
            'xoops_loadLanguage('      => '\xoops_loadLanguage(',
266
267
            //correct errors
268
            'mb_\strlen('              => 'mb_strlen(',
269
            'mb_\substr('              => 'mb_substr(',
270
            'x\copy'                   => 'xcopy',
271
            'r\rmdir'                  => 'rrmdir',
272
            'r\copy'                   => 'rcopy',
273
            '\dirname()'               => 'dirname()',
274
            'assw\ord'                 => 'assword',
275
            'mb_\strpos'               => 'mb_strpos',
276
        ];
277
278
        $patKeys   = \array_keys($patterns);
279
        $patValues = array_values($patterns);
280
        cloneFileFolder($src_path, $dst_path, $patKeys, $patValues);
281
282
        break;
283
}
284
285
include __DIR__ . '/footer.php';
286
287
// recursive cloning script
288
/**
289
 * @param $src_path
290
 * @param $dst_path
291
 * @param array $patKeys
292
 * @param array $patValues
293
 */
294
function cloneFileFolder($src_path, $dst_path, $patKeys = [], $patValues =[])
295
{
296
    // open the source directory
297
    $dir = \opendir($src_path);
298
    // Make the destination directory if not exist
299
    @\mkdir($dst_path);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for mkdir(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

299
    /** @scrutinizer ignore-unhandled */ @\mkdir($dst_path);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
300
    // Loop through the files in source directory
301
    while( $file = \readdir($dir) ) {
0 ignored issues
show
Bug introduced by
It seems like $dir can also be of type false; however, parameter $dir_handle of readdir() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

301
    while( $file = \readdir(/** @scrutinizer ignore-type */ $dir) ) {
Loading history...
302
        if (( $file != '.' ) && ( $file != '..' )) {
303
            if ( \is_dir($src_path . '/' . $file) ) {
304
                // Recursively calling custom copy function for sub directory
305
                cloneFileFolder($src_path . '/' . $file, $dst_path . '/' . $file, $patKeys, $patValues);
306
            } else {
307
                cloneFile($src_path . '/' . $file, $dst_path . '/' . $file, $patKeys, $patValues);
308
            }
309
        }
310
    }
311
    \closedir($dir);
0 ignored issues
show
Bug introduced by
It seems like $dir can also be of type false; however, parameter $dir_handle of closedir() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

311
    \closedir(/** @scrutinizer ignore-type */ $dir);
Loading history...
312
}
313
314
/**
315
 * @param $src_file
316
 * @param $dst_file
317
 * @param array $patKeys
318
 * @param array $patValues
319
 */
320
function cloneFile($src_file, $dst_file, $patKeys = [], $patValues =[])
321
{
322
    $replace_code = false;
323
    $changeExtensions = ['php'];
324
    if (in_array(mb_strtolower(\pathinfo($src_file, PATHINFO_EXTENSION)), $changeExtensions)) {
325
        $replace_code = true;
326
    }
327
    if (\strpos( $dst_file, basename(__FILE__)) > 0) {
328
        //skip myself
329
        $replace_code = false;
330
    }
331
    if ($replace_code) {
332
        // file, read it and replace text
333
        $content = file_get_contents($src_file);
334
        $content = \str_replace($patKeys, $patValues, $content);
335
        //check file name whether it contains replace code
336
        $path_parts = \pathinfo($dst_file);
337
        $path = $path_parts['dirname'];
338
        $file =  $path_parts['basename'];
339
        $dst_file = $path . '/' . \str_replace($patKeys, $patValues, $file);
340
        file_put_contents($dst_file, $content);
341
    } else {
342
        \copy($src_file, $dst_file);
343
    }
344
}
345
346