1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace XoopsModules\Modulebuilder\Files; |
4
|
|
|
|
5
|
|
|
use XoopsModules\Modulebuilder; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class CreateClone |
9
|
|
|
*/ |
10
|
|
|
class CreateClone |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Delete a file or recursively delete a directory |
14
|
|
|
* |
15
|
|
|
* @param string $path Path to file or directory |
16
|
|
|
* |
17
|
|
|
* public static function deleteFileFolder($path) { |
18
|
|
|
* |
19
|
|
|
* if (is_file($path)) { |
20
|
|
|
* return @\unlink($path); |
21
|
|
|
* } |
22
|
|
|
* elseif (\is_dir($path)) { |
23
|
|
|
* $scan = glob(r\trim($path,'/').'/*'); |
24
|
|
|
* foreach($scan as $index=>$path) { |
25
|
|
|
* self::deleteFileFolder($path); |
26
|
|
|
* } |
27
|
|
|
* return @\rmdir($path); |
28
|
|
|
* } |
29
|
|
|
* }*/ |
30
|
|
|
|
31
|
|
|
// recursive cloning script |
32
|
|
|
/** |
33
|
|
|
* @param $src_path |
34
|
|
|
* @param $dst_path |
35
|
|
|
* @param bool $replace_code |
36
|
|
|
* @param array $patKeys |
37
|
|
|
* @param array $patValues |
38
|
|
|
*/ |
39
|
|
|
public static function cloneFileFolder($src_path, $dst_path, $replace_code = false, $patKeys = [], $patValues = []) |
40
|
|
|
{ |
41
|
|
|
// open the source directory |
42
|
|
|
$dir = \opendir($src_path); |
43
|
|
|
// Make the destination directory if not exist |
44
|
|
|
@\mkdir($dst_path); |
|
|
|
|
45
|
|
|
// Loop through the files in source directory |
46
|
|
|
while ($file = \readdir($dir)) { |
47
|
|
|
if (($file != '.') && ($file != '..')) { |
48
|
|
|
if (\is_dir($src_path . '/' . $file)) { |
49
|
|
|
// Recursively calling custom copy function for sub directory |
50
|
|
|
self::cloneFileFolder($src_path . '/' . $file, $dst_path . '/' . $file, $replace_code, $patKeys, $patValues); |
51
|
|
|
} else { |
52
|
|
|
self::cloneFile($src_path . '/' . $file, $dst_path . '/' . $file, $replace_code, $patKeys, $patValues); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
\closedir($dir); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param $src_file |
61
|
|
|
* @param $dst_file |
62
|
|
|
* @param bool $replace_code |
63
|
|
|
* @param array $patKeys |
64
|
|
|
* @param array $patValues |
65
|
|
|
*/ |
66
|
|
|
public static function cloneFile($src_file, $dst_file, $replace_code = false, $patKeys = [], $patValues = []) |
67
|
|
|
{ |
68
|
|
|
if ($replace_code) { |
69
|
|
|
$noChangeExtensions = ['jpeg', 'jpg', 'gif', 'png', 'zip', 'ttf']; |
70
|
|
|
if (\in_array(\mb_strtolower(\pathinfo($src_file, PATHINFO_EXTENSION)), $noChangeExtensions)) { |
|
|
|
|
71
|
|
|
// image |
72
|
|
|
\copy($src_file, $dst_file); |
73
|
|
|
} else { |
74
|
|
|
// file, read it and replace text |
75
|
|
|
$content = file_get_contents($src_file); |
76
|
|
|
$content = \str_replace($patKeys, $patValues, $content); |
77
|
|
|
//check file name whether it contains replace code |
78
|
|
|
$path_parts = \pathinfo($dst_file); |
79
|
|
|
$path = $path_parts['dirname']; |
80
|
|
|
$file = $path_parts['basename']; |
81
|
|
|
$dst_file = $path . '/' . \str_replace($patKeys, $patValues, $file); |
82
|
|
|
file_put_contents($dst_file, $content); |
83
|
|
|
} |
84
|
|
|
} else { |
85
|
|
|
\copy($src_file, $dst_file); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: