-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFile.php
More file actions
274 lines (230 loc) · 7.55 KB
/
Copy pathFile.php
File metadata and controls
274 lines (230 loc) · 7.55 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
namespace Cleantalk\Common\Storage;
/**
* Class File
* Gather functions that works with files.
* All methods are static.
*
* @package Cleantalk
*/
class File{
private static $plugin = 'cleantalk-usp';
/**
* Removes content from file in tag
* Tags example:
* //Cleantalk/TAG_NAME/start
* //Cleantalk/TAG_NAME/end
*
* @param string $file_path
* @param string $tag Tag name (Not whole tag. Only tag name.)
*
* @return bool|Err
*/
public static function clean__tag( $file_path, $tag ){
$pattern = '\s*' . self::tag__php__start( $tag ) . '[\S\s]*?' . self::tag__php__end( $tag );
$pattern = Helper::convert_to_regexp( $pattern );
return Helper::is_regexp( $pattern )
? self::clean__pattern( $file_path, $pattern )
: Err::add( __CLASS__, __FUNCTION__, 'Pattern wrong', $pattern );
}
/**
* Removes variable from file
*
* @param string $file_path
* @param string $variable Variable name (without "$")
*
* @return bool|Err
*/
public static function clean__variable( $file_path, $variable ){
$pattern = '\s*$' . $variable . '\s?=[\S\s]*?;';
$pattern = Helper::convert_to_regexp( $pattern );
return Helper::is_regexp( $pattern )
? self::clean__pattern( $file_path, $pattern )
: Err::add( __CLASS__, __FUNCTION__, 'Pattern wrong', $pattern );
}
/**
* Delete given pattern from file.
* Can clean several collisions.
*
* @param string $file_path
* @param string $pattern RegExp
*
* @return bool| /Cleantalk/Err
*/
public static function clean__pattern( $file_path, $pattern ){
if( is_file( $file_path ) || is_writable( $file_path ) ){
$file_content = file_get_contents( $file_path );
if( $file_content ){
// Cleaning up
$new_content = preg_replace( '/' . $pattern . '/', "\n", $file_content );
$result = $new_content !== null ? true : false;
if($result){
if( file_put_contents( $file_path, $new_content ) ){
return true;
}else
return Err::add(__CLASS__, __FUNCTION__, 'Write error'); // Cannot write new content to template PHP file
}else
return Err::add(__CLASS__, __FUNCTION__, 'Replacement fail'); // Can't read from template PHP file
}else
return Err::add(__CLASS__, __FUNCTION__, 'Read fail'); // Can't read from template PHP file
}else
return Err::add(__CLASS__, __FUNCTION__, 'No file'); // No template PHP file
}
/**
* Remove all content from the file
*
* @param string $file_path
*
* return void;
*/
public static function clean_file_full($file_path ) {
file_put_contents( $file_path, "<?php\n" );
}
/**
* @param string $file_path File path
* @param string $variable Variable name
* @param mixed $value Value of variable
*/
public static function replace__variable( $file_path, $variable, $value ){
$injection = "\n\t\$$variable = " . var_export( $value, true ) . ";";
$needle = '\s*\$' . $variable . '\s?=[\S\s]*?;';
static::replace__code( $file_path, $injection, $needle );
}
public static function replace__code( $file_path, $injection, $needle ){
if( is_file( $file_path ) ){
if( is_writable( $file_path ) ){
$file_content = file_get_contents( $file_path );
if( $file_content ){
$new_content = preg_replace("/$needle/", $injection, $file_content, 1);
$result = $new_content !== null ? true : false;
if($result){
if( file_put_contents( $file_path, $new_content, LOCK_EX ) ){
return true;
}else
return Err::add(__CLASS__, __FUNCTION__, 'Write error'); // Cannot write new content to template PHP file
}else
return Err::add(__CLASS__, __FUNCTION__, 'Replacement fail'); // Can't read from template PHP file
}else
return Err::add(__CLASS__, __FUNCTION__, 'Read fail'); // Can't read from template PHP file
}else
return Err::add(__CLASS__, __FUNCTION__, 'No right to write in file'); // No PHP file
}else
return Err::add(__CLASS__, __FUNCTION__, 'File not found', $file_path); // No PHP file
}
public static function inject__variable( $file_path, $variable, $value, $compact = false ){
$value = var_export( $value, true );
$value = $compact ? preg_replace( '/\s*/', '', $value ) : $value;
self::inject__code( $file_path, "\$$variable = $value;" );
}
public static function inject__code( $file_path, $injection, $needle = '<\?php', $tag = null ){
if( is_file( $file_path ) ){
if( is_writable( $file_path ) ){
$file_content = file_get_contents( $file_path );
if( $file_content ){
$replacement = $tag
? self::tag__php__start( $tag ) . PHP_EOL . $injection . PHP_EOL . self::tag__php__end( $tag )
: $injection;
switch ($needle){
case 'start':
$new_content = $replacement . $file_content;
break;
case 'end':
$new_content = $file_content . $replacement;
break;
default:
$new_content = preg_replace("/$needle/", "$0" . PHP_EOL . $replacement, $file_content, 1);
}
$result = $new_content !== null && $new_content != $file_content ? true : false;
if($result){
if( file_put_contents( $file_path, $new_content ) ){
return true;
}else
return Err::add(__CLASS__, __FUNCTION__, 'Write error'); // Cannot write new content to template PHP file
}else
return Err::add(__CLASS__, __FUNCTION__, 'Replacement fail'); // Can't read from template PHP file
}else
return Err::add(__CLASS__, __FUNCTION__, 'Read fail'); // Can't read from template PHP file
}else
return Err::add(__CLASS__, __FUNCTION__, 'No right to write in file'); // No PHP file
}else
return Err::add(__CLASS__, __FUNCTION__, 'File not found', $file_path); // No PHP file
}
public static function tag__php__start( $tag ){
return '//' . self::$plugin . "/$tag/start";
}
public static function tag__php__end( $tag ){
return '//' . self::$plugin . "/$tag/end";
}
/**
* Recursive
* Copying folder or file
*
* @param string $from Path of the folder to copy
* @param string $to Path to copy in
* @param array $exceptions
*
* @return bool
*/
public static function copy( $from, $to, $exceptions = array() ){
$out = true;
if( is_dir( $from ) ){
if( ! @mkdir( $to, 0777 ) && ! is_dir( $to ) ){
return false;
}
$dd = dir( $from );
while( ( $entry = $dd->read() ) !== false ){
if( ! in_array( $entry, array_merge( $exceptions, array( '.', '..' ) ) ) )
if( ! self::copy( $from . DS . $entry, $to . DS . $entry, $exceptions ) ){
$out = false;
break;
}
}
$dd->close();
}else
$out = copy( $from, $to );
return $out;
}
/**
* Recursive
* Deleting folder or file
*
* @param string $path Path of the folder to delete
* @param array $exceptions
*
* @return bool
*/
public static function delete( $path, $exceptions = array() ){
$out = true;
// Not exists
if( ! file_exists( $path ) )
return $out;
// Directory
elseif( is_dir( $path ) ){
$dd = dir( $path );
while( ( $entry = $dd->read() ) !== false ){
if( ! in_array( $entry, array_merge( $exceptions, array( '.', '..' ) ) ) ){
if( ! self::delete( $path . DS . $entry ) ){
$out = false;
break;
}
}
}
$dd->close();
if( self::isFolderEmpty( $path ) )
$out = rmdir( $path );
// File
}else
$out = unlink( $path );
return $out;
}
/**
* Checks if the folder is empty or not
*
* @param $path
*
* @return bool
*/
public static function isFolderEmpty( $path ){
return ! count( glob( $path . '/*' ) );
}
}