Skip to content
This repository
Newer
Older
100644 419 lines (377 sloc) 11.262 kb
f73b6591 »
2009-05-27 Imported the new trunk.
1 <?php
2 /**
e876cd1b »
2009-05-27 Updated SVN properties.
3 * @version $Id$
f73b6591 »
2009-05-27 Imported the new trunk.
4 * @package Joomla.Framework
5 * @subpackage FileSystem
6 * @copyright Copyright (C) 2005 - 2009 Open Source Matters, Inc. All rights reserved.
7 * @license GNU General Public License <http://www.gnu.org/copyleft/gpl.html>
8 */
9
10 // No direct access
11 defined('JPATH_BASE') or die;
12
13 jimport('joomla.filesystem.path');
14
15 /**
16 * A File handling class
17 *
18 * @static
19 * @package Joomla.Framework
20 * @subpackage FileSystem
21 * @since 1.5
22 */
23 class JFile
24 {
25 /**
26 * Gets the extension of a file name
27 *
28 * @param string $file The file name
29 * @return string The file extension
30 * @since 1.5
31 */
32 function getExt($file) {
33 $dot = strrpos($file, '.') + 1;
34 return substr($file, $dot);
35 }
36
37 /**
38 * Strips the last extension off a file name
39 *
40 * @param string $file The file name
41 * @return string The file name without the extension
42 * @since 1.5
43 */
44 function stripExt($file) {
45 return preg_replace('#\.[^.]*$#', '', $file);
46 }
47
48 /**
49 * Makes file name safe to use
50 *
51 * @param string $file The name of the file [not full path]
52 * @return string The sanitised string
53 * @since 1.5
54 */
55 function makeSafe($file) {
56 $regex = array('#(\.){2,}#', '#[^A-Za-z0-9\.\_\- ]#', '#^\.#');
57 return preg_replace($regex, '', $file);
58 }
59
60 /**
61 * Copies a file
62 *
63 * @param string $src The path to the source file
64 * @param string $dest The path to the destination file
65 * @param string $path An optional base path to prefix to the file names
66 * @return boolean True on success
67 * @since 1.5
68 */
a9bf5568 »
2009-05-31 ^ Added extra functionality to GMail auth plugin (configurable verif…
69 function copy($src, $dest, $path = null, $use_streams=false)
f73b6591 »
2009-05-27 Imported the new trunk.
70 {
71 // Prepend a base path if it exists
72 if ($path) {
73 $src = JPath::clean($path.DS.$src);
74 $dest = JPath::clean($path.DS.$dest);
75 }
76
77 //Check src path
78 if (!is_readable($src)) {
79 JError::raiseWarning(21, 'JFile::copy: ' . JText::_('Cannot find or read file') . ": '$src'");
80 return false;
81 }
82
a9bf5568 »
2009-05-31 ^ Added extra functionality to GMail auth plugin (configurable verif…
83 if($use_streams) {
84 $stream =& JFactory::getStream();
85 if(!$stream->copy($src, $dest)) {
86 JError::raiseWarning(21, 'JFile::copy('. $src .', '. $dest .'): '. $stream->getError());
87 return false;
88 }
89 return true;
90 } else {
91 // Initialize variables
92 jimport('joomla.client.helper');
93 $FTPOptions = JClientHelper::getCredentials('ftp');
94
f73b6591 »
2009-05-27 Imported the new trunk.
95 if ($FTPOptions['enabled'] == 1) {
96 // Connect the FTP client
97 jimport('joomla.client.ftp');
98 $ftp = & JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $FTPOptions['user'], $FTPOptions['pass']);
99
100 // If the parent folder doesn't exist we must create it
101 if (!file_exists(dirname($dest))) {
102 jimport('joomla.filesystem.folder');
103 JFolder::create(dirname($dest));
104 }
105
106 //Translate the destination path for the FTP account
107 $dest = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $dest), '/');
108 if (!$ftp->store($src, $dest)) {
109 // FTP connector throws an error
110 return false;
111 }
112 $ret = true;
113 } else {
114 if (!@ copy($src, $dest)) {
115 JError::raiseWarning(21, JText::_('Copy failed'));
116 return false;
117 }
118 $ret = true;
119 }
120 return $ret;
121 }
a9bf5568 »
2009-05-31 ^ Added extra functionality to GMail auth plugin (configurable verif…
122 }
f73b6591 »
2009-05-27 Imported the new trunk.
123
124 /**
125 * Delete a file or array of files
126 *
127 * @param mixed $file The file name or an array of file names
128 * @return boolean True on success
129 * @since 1.5
130 */
131 function delete($file)
132 {
133 // Initialize variables
134 jimport('joomla.client.helper');
135 $FTPOptions = JClientHelper::getCredentials('ftp');
136
137 if (is_array($file)) {
138 $files = $file;
139 } else {
140 $files[] = $file;
141 }
142
143 // Do NOT use ftp if it is not enabled
144 if ($FTPOptions['enabled'] == 1)
145 {
146 // Connect the FTP client
147 jimport('joomla.client.ftp');
148 $ftp = & JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $FTPOptions['user'], $FTPOptions['pass']);
149 }
150
151 foreach ($files as $file)
152 {
153 $file = JPath::clean($file);
154
155 // Try making the file writeable first. If it's read-only, it can't be deleted
156 // on Windows, even if the parent folder is writeable
157 @chmod($file, 0777);
158
159 // In case of restricted permissions we zap it one way or the other
160 // as long as the owner is either the webserver or the ftp
161 if (@unlink($file)) {
162 // Do nothing
163 } elseif ($FTPOptions['enabled'] == 1) {
164 $file = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $file), '/');
165 if (!$ftp->delete($file)) {
166 // FTP connector throws an error
167 return false;
168 }
169 } else {
170 $filename = basename($file);
171 JError::raiseWarning('SOME_ERROR_CODE', JText::_('Delete failed') . ": '$filename'");
172 return false;
173 }
174 }
175
176 return true;
177 }
178
179 /**
180 * Moves a file
181 *
182 * @param string $src The path to the source file
183 * @param string $dest The path to the destination file
184 * @param string $path An optional base path to prefix to the file names
185 * @return boolean True on success
186 * @since 1.5
187 */
a9bf5568 »
2009-05-31 ^ Added extra functionality to GMail auth plugin (configurable verif…
188 function move($src, $dest, $path = '', $use_streams=false)
f73b6591 »
2009-05-27 Imported the new trunk.
189 {
190
191 if ($path) {
192 $src = JPath::clean($path.DS.$src);
193 $dest = JPath::clean($path.DS.$dest);
194 }
195
196 //Check src path
a9bf5568 »
2009-05-31 ^ Added extra functionality to GMail auth plugin (configurable verif…
197 if (!is_readable($src)) { // && !is_writable($src)) { // file may not be writable by php if via ftp!
198 return JText::_('Cannot find source file');
f73b6591 »
2009-05-27 Imported the new trunk.
199 }
200
a9bf5568 »
2009-05-31 ^ Added extra functionality to GMail auth plugin (configurable verif…
201 if($use_streams) {
202 $stream =& JFactory::getStream();
203 if(!$stream->move($src, $dest)) {
204 JError::raiseWarning(21, 'JFile::move: '. $stream->getError());
205 return false;
206 }
207 return true;
208 } else {
209 // Initialize variables
210 jimport('joomla.client.helper');
211 $FTPOptions = JClientHelper::getCredentials('ftp');
212
f73b6591 »
2009-05-27 Imported the new trunk.
213 if ($FTPOptions['enabled'] == 1) {
214 // Connect the FTP client
215 jimport('joomla.client.ftp');
216 $ftp = & JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $FTPOptions['user'], $FTPOptions['pass']);
217
218 //Translate path for the FTP account
219 $src = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $src), '/');
220 $dest = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $dest), '/');
221
222 // Use FTP rename to simulate move
223 if (!$ftp->rename($src, $dest)) {
224 JError::raiseWarning(21, JText::_('Rename failed'));
225 return false;
226 }
227 } else {
228 if (!@ rename($src, $dest)) {
229 JError::raiseWarning(21, JText::_('Rename failed'));
230 return false;
231 }
232 }
233 return true;
234 }
a9bf5568 »
2009-05-31 ^ Added extra functionality to GMail auth plugin (configurable verif…
235 }
f73b6591 »
2009-05-27 Imported the new trunk.
236
237 /**
238 * Read the contents of a file
239 *
240 * @param string $filename The full file path
241 * @param boolean $incpath Use include path
242 * @param int $amount Amount of file to read
243 * @param int $chunksize Size of chunks to read
244 * @param int $offset Offset of the file
245 * @return mixed Returns file contents or boolean False if failed
246 * @since 1.5
247 */
248 function read($filename, $incpath = false, $amount = 0, $chunksize = 8192, $offset = 0)
249 {
250 // Initialize variables
251 $data = null;
252 if ($amount && $chunksize > $amount) { $chunksize = $amount; }
253 if (false === $fh = fopen($filename, 'rb', $incpath)) {
254 JError::raiseWarning(21, 'JFile::read: '.JText::_('Unable to open file') . ": '$filename'");
255 return false;
256 }
257 clearstatcache();
258 if ($offset) fseek($fh, $offset);
259 if ($fsize = @ filesize($filename)) {
260 if ($amount && $fsize > $amount) {
261 $data = fread($fh, $amount);
262 } else {
263 $data = fread($fh, $fsize);
264 }
265 } else {
266 $data = '';
267 $x = 0;
268 // While its:
269 // 1: Not the end of the file AND
270 // 2a: No Max Amount set OR
271 // 2b: The length of the data is less than the max amount we want
272 while (!feof($fh) && (!$amount || strlen($data) < $amount)) {
273 $data .= fread($fh, $chunksize);
274 }
275 }
276 fclose($fh);
277
278 return $data;
279 }
280
281 /**
282 * Write contents to a file
283 *
284 * @param string $file The full file path
285 * @param string $buffer The buffer to write
286 * @return boolean True on success
287 * @since 1.5
288 */
a9bf5568 »
2009-05-31 ^ Added extra functionality to GMail auth plugin (configurable verif…
289 function write($file, &$buffer, $use_streams=false)
f73b6591 »
2009-05-27 Imported the new trunk.
290 {
291
292 // If the destination directory doesn't exist we need to create it
293 if (!file_exists(dirname($file))) {
294 jimport('joomla.filesystem.folder');
295 JFolder::create(dirname($file));
296 }
297
a9bf5568 »
2009-05-31 ^ Added extra functionality to GMail auth plugin (configurable verif…
298 if($use_streams) {
299 $stream =& JFactory::getStream();
300 $stream->set('chunksize', (1024 * 1024 * 1024)); // beef up the chunk size to a meg
301 if(!$stream->writeFile($file, $buffer)) {
302 JError::raiseWarning(21, 'JFile::write('. $file.'): '. $stream->getError());
303 return false;
304 }
305 return true;
306 } else {
307 // Initialize variables
308 jimport('joomla.client.helper');
309 $FTPOptions = JClientHelper::getCredentials('ftp');
310
f73b6591 »
2009-05-27 Imported the new trunk.
311 if ($FTPOptions['enabled'] == 1) {
312 // Connect the FTP client
313 jimport('joomla.client.ftp');
314 $ftp = & JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $FTPOptions['user'], $FTPOptions['pass']);
315
316 // Translate path for the FTP account and use FTP write buffer to file
317 $file = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $file), '/');
318 $ret = $ftp->write($file, $buffer);
319 } else {
320 $file = JPath::clean($file);
321 $ret = file_put_contents($file, $buffer);
322 }
323 return $ret;
324 }
a9bf5568 »
2009-05-31 ^ Added extra functionality to GMail auth plugin (configurable verif…
325 }
f73b6591 »
2009-05-27 Imported the new trunk.
326
327 /**
328 * Moves an uploaded file to a destination folder
329 *
330 * @param string $src The name of the php (temporary) uploaded file
331 * @param string $dest The path (including filename) to move the uploaded file to
332 * @return boolean True on success
333 * @since 1.5
334 */
a9bf5568 »
2009-05-31 ^ Added extra functionality to GMail auth plugin (configurable verif…
335 function upload($src, $dest, $use_streams=false)
f73b6591 »
2009-05-27 Imported the new trunk.
336 {
337
338 // Ensure that the path is valid and clean
339 $dest = JPath::clean($dest);
340
341 // Create the destination directory if it does not exist
342 $baseDir = dirname($dest);
343 if (!file_exists($baseDir)) {
344 jimport('joomla.filesystem.folder');
345 JFolder::create($baseDir);
346 }
347
a9bf5568 »
2009-05-31 ^ Added extra functionality to GMail auth plugin (configurable verif…
348 if($use_streams) {
349 $stream =& JFactory::getStream();
350 if(!$stream->upload($src, $dest)) {
351 JError::raiseWarning(21, 'JFile::upload: '. $stream->getError());
352 return false;
353 }
354 return true;
355 } else {
356 // Initialize variables
357 jimport('joomla.client.helper');
358 $FTPOptions = JClientHelper::getCredentials('ftp');
359 $ret = false;
360
f73b6591 »
2009-05-27 Imported the new trunk.
361 if ($FTPOptions['enabled'] == 1) {
362 // Connect the FTP client
363 jimport('joomla.client.ftp');
364 $ftp = & JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $FTPOptions['user'], $FTPOptions['pass']);
365
366 //Translate path for the FTP account
367 $dest = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $dest), '/');
368
369 // Copy the file to the destination directory
370 if ($ftp->store($src, $dest)) {
371 $ftp->chmod($dest, 0777);
372 $ret = true;
373 } else {
374 JError::raiseWarning(21, JText::_('WARNFS_ERR02'));
375 }
376 } else {
377 if (is_writeable($baseDir) && move_uploaded_file($src, $dest)) { // Short circuit to prevent file permission errors
378 if (JPath::setPermissions($dest)) {
379 $ret = true;
380 } else {
381 JError::raiseWarning(21, JText::_('WARNFS_ERR01'));
382 }
383 } else {
384 JError::raiseWarning(21, JText::_('WARNFS_ERR02'));
385 }
386 }
387 return $ret;
388 }
a9bf5568 »
2009-05-31 ^ Added extra functionality to GMail auth plugin (configurable verif…
389 }
f73b6591 »
2009-05-27 Imported the new trunk.
390
391 /**
392 * Wrapper for the standard file_exists function
393 *
394 * @param string $file File path
395 * @return boolean True if path is a file
396 * @since 1.5
397 */
398 function exists($file)
399 {
400 return is_file(JPath::clean($file));
401 }
402
403 /**
404 * Returns the name, sans any path
405 *
406 * param string $file File path
407 * @return string filename
408 * @since 1.5
409 */
410 function getName($file) {
411 $slash = strrpos($file, DS);
412 if ($slash !== false) {
413 return substr($file, $slash + 1);
414 } else {
415 return $file;
416 }
417 }
418 }
Something went wrong with that request. Please try again.