File.php
6.76 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?php
/**
* @package Regular Labs Library
* @version 18.2.10140
*
* @author Peter van Westen <info@regularlabs.com>
* @link http://www.regularlabs.com
* @copyright Copyright © 2018 Regular Labs All Rights Reserved
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
*/
namespace RegularLabs\Library;
defined('_JEXEC') or die;
use JClientFtp;
use JClientHelper;
use JFactory;
use JFilesystemWrapperPath;
use JFolder;
use JLog;
use JText;
use JUri;
/**
* Class File
* @package RegularLabs\Library
*/
class File
{
/**
* Find a matching media file in the different possible extension media folders for given type
*
* @param string $type (css/js/...)
* @param string $file
*
* @return bool|string
*/
public static function getMediaFile($type, $file)
{
// If http is present in filename
if (strpos($file, 'http') === 0 || strpos($file, '//') === 0)
{
return $file;
}
$files = [];
// Detect debug mode
if (JFactory::getConfig()->get('debug') || JFactory::getApplication()->input->get('debug'))
{
$files[] = str_replace(['.min.', '-min.'], '.', $file);
}
$files[] = $file;
/*
* Loop on 1 or 2 files and break on first find.
* Add the content of the MD5SUM file located in the same folder to url to ensure cache browser refresh
* This MD5SUM file must represent the signature of the folder content
*/
foreach ($files as $check_file)
{
$file_found = self::findMediaFileByFile($check_file, $type);
if ( ! $file_found)
{
continue;
}
return $file_found;
}
return false;
}
/**
* Find a matching media file in the different possible extension media folders for given type
*
* @param string $file
* @param string $type (css/js/...)
*
* @return bool|string
*/
private static function findMediaFileByFile($file, $type)
{
$template = JFactory::getApplication()->getTemplate();
// If the file is in the template folder
$file_found = self::getFileUrl('/templates/' . $template . '/' . $type . '/' . $file);
if ($file_found)
{
return $file_found;
}
// Try to deal with system files in the media folder
if (strpos($file, '/') === false)
{
$file_found = self::getFileUrl('/media/system/' . $type . '/' . $file);
if ( ! $file_found)
{
return false;
}
return $file_found;
}
$paths = [];
// If the file contains any /: it can be in a media extension subfolder
// Divide the file extracting the extension as the first part before /
list($extension, $file) = explode('/', $file, 2);
$paths[] = '/media/' . $extension . '/' . $type;
$paths[] = '/templates/' . $template . '/' . $type . '/system';
$paths[] = '/media/system/' . $type;
foreach ($paths as $path)
{
$file_found = self::getFileUrl($path . '/' . $file);
if ( ! $file_found)
{
continue;
}
return $file_found;
}
return false;
}
/**
* Get the url for the file
*
* @param string $path
*
* @return bool|string
*/
private static function getFileUrl($path)
{
if ( ! file_exists(JPATH_ROOT . $path))
{
return false;
}
return JUri::root(true) . $path;
}
/**
* Delete a file or array of files
*
* @param mixed $file The file name or an array of file names
* @param boolean $show_messages Whether or not to show error messages
*
* @return boolean True on success
*
* @since 11.1
*/
public static function delete($file, $show_messages = false)
{
$FTPOptions = JClientHelper::getCredentials('ftp');
$pathObject = new JFilesystemWrapperPath;
$files = is_array($file) ? $file : [$file];
if ($FTPOptions['enabled'] == 1)
{
// Connect the FTP client
$ftp = JClientFtp::getInstance($FTPOptions['host'], $FTPOptions['port'], [], $FTPOptions['user'], $FTPOptions['pass']);
}
foreach ($files as $file)
{
$file = $pathObject->clean($file);
if ( ! is_file($file))
{
continue;
}
// Try making the file writable first. If it's read-only, it can't be deleted
// on Windows, even if the parent folder is writable
@chmod($file, 0777);
if ($FTPOptions['enabled'] == 1)
{
$file = $pathObject->clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $file), '/');
if ( ! $ftp->delete($file))
{
// FTP connector throws an error
return false;
}
}
// Try the unlink twice in case something was blocking it on first try
if ( ! @unlink($file) && ! @unlink($file))
{
$show_messages && JLog::add(JText::sprintf('JLIB_FILESYSTEM_DELETE_FAILED', basename($file)), JLog::WARNING, 'jerror');
return false;
}
}
return true;
}
/**
* Delete a folder.
*
* @param string $path The path to the folder to delete.
* @param boolean $show_messages Whether or not to show error messages
*
* @return boolean True on success.
*/
public static function deleteFolder($path, $show_messages = false)
{
@set_time_limit(ini_get('max_execution_time'));
$pathObject = new JFilesystemWrapperPath;
if ( ! $path)
{
$show_messages && JLog::add(__METHOD__ . ': ' . JText::_('JLIB_FILESYSTEM_ERROR_DELETE_BASE_DIRECTORY'), JLog::WARNING, 'jerror');
return false;
}
$FTPOptions = JClientHelper::getCredentials('ftp');
// Check to make sure the path valid and clean
$path = $pathObject->clean($path);
if ( ! is_dir($path))
{
$show_messages && JLog::add(JText::sprintf('JLIB_FILESYSTEM_ERROR_PATH_IS_NOT_A_FOLDER', $path), JLog::WARNING, 'jerror');
return false;
}
// Remove all the files in folder if they exist; disable all filtering
$files = JFolder::files($path, '.', false, true, [], []);
if ( ! empty($files))
{
if (self::delete($files, $show_messages) !== true)
{
// JFile::delete throws an error
return false;
}
}
// Remove sub-folders of folder; disable all filtering
$folders = JFolder::folders($path, '.', false, true, [], []);
foreach ($folders as $folder)
{
if (is_link($folder))
{
// Don't descend into linked directories, just delete the link.
if (self::delete($folder, $show_messages) !== true)
{
return false;
}
continue;
}
if ( ! self::deleteFolder($folder, $show_messages))
{
return false;
}
}
if (@rmdir($path))
{
return true;
}
if ($FTPOptions['enabled'] == 1)
{
// Connect the FTP client
$ftp = JClientFtp::getInstance($FTPOptions['host'], $FTPOptions['port'], [], $FTPOptions['user'], $FTPOptions['pass']);
// Translate path and delete
$path = $pathObject->clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $path), '/');
// FTP connector throws an error
return $ftp->delete($path);
}
if ( ! @rmdir($path))
{
$show_messages && JLog::add(JText::sprintf('JLIB_FILESYSTEM_ERROR_FOLDER_DELETE', $path), JLog::WARNING, 'jerror');
return false;
}
return true;
}
}