Files
@ 7ec91347b83f
Branch filter:
Location: hot67beta/libraries/joomla/filesystem/file.php
7ec91347b83f
10.2 KiB
text/x-php
there we go
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 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 | <?php
/**
* @version $Id: file.php 10707 2008-08-21 09:52:47Z eddieajau $
* @package Joomla.Framework
* @subpackage FileSystem
* @copyright Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
* @license GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/
// Check to ensure this file is within the rest of the framework
defined('JPATH_BASE') or die();
jimport('joomla.filesystem.path');
/**
* A File handling class
*
* @static
* @package Joomla.Framework
* @subpackage FileSystem
* @since 1.5
*/
class JFile
{
/**
* Gets the extension of a file name
*
* @param string $file The file name
* @return string The file extension
* @since 1.5
*/
function getExt($file) {
$dot = strrpos($file, '.') + 1;
return substr($file, $dot);
}
/**
* Strips the last extension off a file name
*
* @param string $file The file name
* @return string The file name without the extension
* @since 1.5
*/
function stripExt($file) {
return preg_replace('#\.[^.]*$#', '', $file);
}
/**
* Makes file name safe to use
*
* @param string $file The name of the file [not full path]
* @return string The sanitised string
* @since 1.5
*/
function makeSafe($file) {
$regex = array('#(\.){2,}#', '#[^A-Za-z0-9\.\_\- ]#', '#^\.#');
return preg_replace($regex, '', $file);
}
/**
* Copies a file
*
* @param string $src The path to the source file
* @param string $dest The path to the destination file
* @param string $path An optional base path to prefix to the file names
* @return boolean True on success
* @since 1.5
*/
function copy($src, $dest, $path = null)
{
// Initialize variables
jimport('joomla.client.helper');
$FTPOptions = JClientHelper::getCredentials('ftp');
// Prepend a base path if it exists
if ($path) {
$src = JPath::clean($path.DS.$src);
$dest = JPath::clean($path.DS.$dest);
}
//Check src path
if (!is_readable($src)) {
JError::raiseWarning(21, 'JFile::copy: '.JText::_('Cannot find or read file' . ": '$src'"));
return false;
}
if ($FTPOptions['enabled'] == 1) {
// Connect the FTP client
jimport('joomla.client.ftp');
$ftp = & JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $FTPOptions['user'], $FTPOptions['pass']);
// If the parent folder doesn't exist we must create it
if (!file_exists(dirname($dest))) {
jimport('joomla.filesystem.folder');
JFolder::create(dirname($dest));
}
//Translate the destination path for the FTP account
$dest = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $dest), '/');
if (!$ftp->store($src, $dest)) {
// FTP connector throws an error
return false;
}
$ret = true;
} else {
if (!@ copy($src, $dest)) {
JError::raiseWarning(21, JText::_('Copy failed'));
return false;
}
$ret = true;
}
return $ret;
}
/**
* Delete a file or array of files
*
* @param mixed $file The file name or an array of file names
* @return boolean True on success
* @since 1.5
*/
function delete($file)
{
// Initialize variables
jimport('joomla.client.helper');
$FTPOptions = JClientHelper::getCredentials('ftp');
if (is_array($file)) {
$files = $file;
} else {
$files[] = $file;
}
// Do NOT use ftp if it is not enabled
if ($FTPOptions['enabled'] == 1)
{
// Connect the FTP client
jimport('joomla.client.ftp');
$ftp = & JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $FTPOptions['user'], $FTPOptions['pass']);
}
foreach ($files as $file)
{
$file = JPath::clean($file);
// Try making the file writeable first. If it's read-only, it can't be deleted
// on Windows, even if the parent folder is writeable
@chmod($file, 0777);
// In case of restricted permissions we zap it one way or the other
// as long as the owner is either the webserver or the ftp
if (@unlink($file)) {
// Do nothing
} elseif ($FTPOptions['enabled'] == 1) {
$file = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $file), '/');
if (!$ftp->delete($file)) {
// FTP connector throws an error
return false;
}
} else {
$filename = basename($file);
JError::raiseWarning('SOME_ERROR_CODE', JText::_('Delete failed') . ": '$filename'");
return false;
}
}
return true;
}
/**
* Moves a file
*
* @param string $src The path to the source file
* @param string $dest The path to the destination file
* @param string $path An optional base path to prefix to the file names
* @return boolean True on success
* @since 1.5
*/
function move($src, $dest, $path = '')
{
// Initialize variables
jimport('joomla.client.helper');
$FTPOptions = JClientHelper::getCredentials('ftp');
if ($path) {
$src = JPath::clean($path.DS.$src);
$dest = JPath::clean($path.DS.$dest);
}
//Check src path
if (!is_readable($src) && !is_writable($src)) {
return JText::_('Cannot find source file');
}
if ($FTPOptions['enabled'] == 1) {
// Connect the FTP client
jimport('joomla.client.ftp');
$ftp = & JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $FTPOptions['user'], $FTPOptions['pass']);
//Translate path for the FTP account
$src = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $src), '/');
$dest = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $dest), '/');
// Use FTP rename to simulate move
if (!$ftp->rename($src, $dest)) {
JError::raiseWarning(21, JText::_('Rename failed'));
return false;
}
} else {
if (!@ rename($src, $dest)) {
JError::raiseWarning(21, JText::_('Rename failed'));
return false;
}
}
return true;
}
/**
* Read the contents of a file
*
* @param string $filename The full file path
* @param boolean $incpath Use include path
* @param int $amount Amount of file to read
* @param int $chunksize Size of chunks to read
* @param int $offset Offset of the file
* @return mixed Returns file contents or boolean False if failed
* @since 1.5
*/
function read($filename, $incpath = false, $amount = 0, $chunksize = 8192, $offset = 0)
{
// Initialize variables
$data = null;
if($amount && $chunksize > $amount) { $chunksize = $amount; }
if (false === $fh = fopen($filename, 'rb', $incpath)) {
JError::raiseWarning(21, 'JFile::read: '.JText::_('Unable to open file') . ": '$filename'");
return false;
}
clearstatcache();
if($offset) fseek($fh, $offset);
if ($fsize = @ filesize($filename)) {
if($amount && $fsize > $amount) {
$data = fread($fh, $amount);
} else {
$data = fread($fh, $fsize);
}
} else {
$data = '';
$x = 0;
// While its:
// 1: Not the end of the file AND
// 2a: No Max Amount set OR
// 2b: The length of the data is less than the max amount we want
while (!feof($fh) && (!$amount || strlen($data) < $amount)) {
$data .= fread($fh, $chunksize);
}
}
fclose($fh);
return $data;
}
/**
* Write contents to a file
*
* @param string $file The full file path
* @param string $buffer The buffer to write
* @return boolean True on success
* @since 1.5
*/
function write($file, $buffer)
{
// Initialize variables
jimport('joomla.client.helper');
$FTPOptions = JClientHelper::getCredentials('ftp');
// If the destination directory doesn't exist we need to create it
if (!file_exists(dirname($file))) {
jimport('joomla.filesystem.folder');
JFolder::create(dirname($file));
}
if ($FTPOptions['enabled'] == 1) {
// Connect the FTP client
jimport('joomla.client.ftp');
$ftp = & JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $FTPOptions['user'], $FTPOptions['pass']);
// Translate path for the FTP account and use FTP write buffer to file
$file = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $file), '/');
$ret = $ftp->write($file, $buffer);
} else {
$file = JPath::clean($file);
$ret = file_put_contents($file, $buffer);
}
return $ret;
}
/**
* Moves an uploaded file to a destination folder
*
* @param string $src The name of the php (temporary) uploaded file
* @param string $dest The path (including filename) to move the uploaded file to
* @return boolean True on success
* @since 1.5
*/
function upload($src, $dest)
{
// Initialize variables
jimport('joomla.client.helper');
$FTPOptions = JClientHelper::getCredentials('ftp');
$ret = false;
// Ensure that the path is valid and clean
$dest = JPath::clean($dest);
// Create the destination directory if it does not exist
$baseDir = dirname($dest);
if (!file_exists($baseDir)) {
jimport('joomla.filesystem.folder');
JFolder::create($baseDir);
}
if ($FTPOptions['enabled'] == 1) {
// Connect the FTP client
jimport('joomla.client.ftp');
$ftp = & JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $FTPOptions['user'], $FTPOptions['pass']);
//Translate path for the FTP account
$dest = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $dest), '/');
// Copy the file to the destination directory
if ($ftp->store($src, $dest)) {
$ftp->chmod($dest, 0777);
$ret = true;
} else {
JError::raiseWarning(21, JText::_('WARNFS_ERR02'));
}
} else {
if (is_writeable($baseDir) && move_uploaded_file($src, $dest)) { // Short circuit to prevent file permission errors
if (JPath::setPermissions($dest)) {
$ret = true;
} else {
JError::raiseWarning(21, JText::_('WARNFS_ERR01'));
}
} else {
JError::raiseWarning(21, JText::_('WARNFS_ERR02'));
}
}
return $ret;
}
/**
* Wrapper for the standard file_exists function
*
* @param string $file File path
* @return boolean True if path is a file
* @since 1.5
*/
function exists($file)
{
return is_file(JPath::clean($file));
}
/**
* Returns the name, sans any path
*
* param string $file File path
* @return string filename
* @since 1.5
*/
function getName($file) {
$slash = strrpos($file, DS) + 1;
return substr($file, $slash);
}
}
|