| Current Path : /proc/1908984/root/proc/1908984/root/home/digilove/www/41423/ |
| Current File : //proc/1908984/root/proc/1908984/root/home/digilove/www/41423/File.php.tar |
home/digilove/public_html/libraries/src/Filesystem/File.php 0000644 00000037010 15234510143 0020056 0 ustar 00 <?php
/**
* Joomla! Content Management System
*
* @copyright (C) 2006 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\Filesystem;
defined('JPATH_PLATFORM') or die;
use Joomla\CMS\Factory;
use Joomla\CMS\Log\Log;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Filesystem\Path;
use Joomla\CMS\Filesystem\Wrapper\PathWrapper;
use Joomla\CMS\Filesystem\Wrapper\FolderWrapper;
use Joomla\CMS\Client\ClientHelper;
use Joomla\CMS\Client\FtpClient;
/**
* A File handling class
*
* @since 1.7.0
*/
class File
{
/**
* Gets the extension of a file name
*
* @param string $file The file name
*
* @return string The file extension
*
* @since 1.7.0
*/
public static function getExt($file)
{
// String manipulation should be faster than pathinfo() on newer PHP versions.
$dot = strrpos($file, '.');
if ($dot === false)
{
return '';
}
$ext = substr($file, $dot + 1);
// Extension cannot contain slashes.
if (strpos($ext, '/') !== false || (DIRECTORY_SEPARATOR === '\\' && strpos($ext, '\\') !== false))
{
return '';
}
return $ext;
}
/**
* Strips the last extension off of a file name
*
* @param string $file The file name
*
* @return string The file name without the extension
*
* @since 1.7.0
*/
public static 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.7.0
*/
public static function makeSafe($file)
{
// Remove any trailing dots, as those aren't ever valid file names.
$file = rtrim($file, '.');
$regex = array('#(\.){2,}#', '#[^A-Za-z0-9\.\_\- ]#', '#^\.#');
return trim(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
* @param boolean $useStreams True to use streams
*
* @return boolean True on success
*
* @since 1.7.0
*/
public static function copy($src, $dest, $path = null, $useStreams = false)
{
$pathObject = new PathWrapper;
// Prepend a base path if it exists
if ($path)
{
$src = $pathObject->clean($path . '/' . $src);
$dest = $pathObject->clean($path . '/' . $dest);
}
// Check src path
if (!is_readable($src))
{
Log::add(Text::sprintf('JLIB_FILESYSTEM_ERROR_JFILE_FIND_COPY', $src), Log::WARNING, 'jerror');
return false;
}
if ($useStreams)
{
$stream = Factory::getStream();
if (!$stream->copy($src, $dest))
{
Log::add(Text::sprintf('JLIB_FILESYSTEM_ERROR_JFILE_STREAMS', $src, $dest, $stream->getError()), Log::WARNING, 'jerror');
return false;
}
return true;
}
else
{
$FTPOptions = ClientHelper::getCredentials('ftp');
if ($FTPOptions['enabled'] == 1)
{
// Connect the FTP client
$ftp = FtpClient::getInstance($FTPOptions['host'], $FTPOptions['port'], array(), $FTPOptions['user'], $FTPOptions['pass']);
// If the parent folder doesn't exist we must create it
if (!file_exists(dirname($dest)))
{
$folderObject = new FolderWrapper;
$folderObject->create(dirname($dest));
}
// Translate the destination path for the FTP account
$dest = $pathObject->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))
{
Log::add(Text::sprintf('JLIB_FILESYSTEM_ERROR_COPY_FAILED_ERR01', $src, $dest), Log::WARNING, 'jerror');
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.7.0
*/
public static function delete($file)
{
$FTPOptions = ClientHelper::getCredentials('ftp');
$pathObject = new PathWrapper;
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
$ftp = FtpClient::getInstance($FTPOptions['host'], $FTPOptions['port'], array(), $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);
// 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 = $pathObject->clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $file), '/');
if (!$ftp->delete($file))
{
// FTP connector throws an error
return false;
}
}
else
{
$filename = basename($file);
Log::add(Text::sprintf('JLIB_FILESYSTEM_DELETE_FAILED', $filename), Log::WARNING, 'jerror');
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
* @param boolean $useStreams True to use streams
*
* @return boolean True on success
*
* @since 1.7.0
*/
public static function move($src, $dest, $path = '', $useStreams = false)
{
$pathObject = new PathWrapper;
if ($path)
{
$src = $pathObject->clean($path . '/' . $src);
$dest = $pathObject->clean($path . '/' . $dest);
}
// Check src path
if (!is_readable($src))
{
Log::add(Text::_('JLIB_FILESYSTEM_CANNOT_FIND_SOURCE_FILE'), Log::WARNING, 'jerror');
return false;
}
if ($useStreams)
{
$stream = Factory::getStream();
if (!$stream->move($src, $dest))
{
Log::add(Text::sprintf('JLIB_FILESYSTEM_ERROR_JFILE_MOVE_STREAMS', $stream->getError()), Log::WARNING, 'jerror');
return false;
}
return true;
}
else
{
$FTPOptions = ClientHelper::getCredentials('ftp');
if ($FTPOptions['enabled'] == 1)
{
// Connect the FTP client
$ftp = FtpClient::getInstance($FTPOptions['host'], $FTPOptions['port'], array(), $FTPOptions['user'], $FTPOptions['pass']);
// Translate path for the FTP account
$src = $pathObject->clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $src), '/');
$dest = $pathObject->clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $dest), '/');
// Use FTP rename to simulate move
if (!$ftp->rename($src, $dest))
{
Log::add(Text::_('JLIB_FILESYSTEM_ERROR_RENAME_FILE'), Log::WARNING, 'jerror');
return false;
}
}
else
{
if (!@ rename($src, $dest))
{
Log::add(Text::_('JLIB_FILESYSTEM_ERROR_RENAME_FILE'), Log::WARNING, 'jerror');
return false;
}
}
return true;
}
}
/**
* Read the contents of a file
*
* @param string $filename The full file path
* @param boolean $incpath Use include path
* @param integer $amount Amount of file to read
* @param integer $chunksize Size of chunks to read
* @param integer $offset Offset of the file
*
* @return mixed Returns file contents or boolean False if failed
*
* @since 1.7.0
* @deprecated 4.0 - Use the native file_get_contents() instead.
*/
public static function read($filename, $incpath = false, $amount = 0, $chunksize = 8192, $offset = 0)
{
Log::add(__METHOD__ . ' is deprecated. Use native file_get_contents() syntax.', Log::WARNING, 'deprecated');
$data = null;
if ($amount && $chunksize > $amount)
{
$chunksize = $amount;
}
if (false === $fh = fopen($filename, 'rb', $incpath))
{
Log::add(Text::sprintf('JLIB_FILESYSTEM_ERROR_READ_UNABLE_TO_OPEN_FILE', $filename), Log::WARNING, 'jerror');
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 = '';
/*
* While it's:
* 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
* @param boolean $useStreams Use streams
*
* @return boolean True on success
*
* @since 1.7.0
*/
public static function write($file, $buffer, $useStreams = false)
{
@set_time_limit(ini_get('max_execution_time'));
// If the destination directory doesn't exist we need to create it
if (!file_exists(dirname($file)))
{
$folderObject = new FolderWrapper;
if ($folderObject->create(dirname($file)) == false)
{
return false;
}
}
if ($useStreams)
{
$stream = Factory::getStream();
// Beef up the chunk size to a meg
$stream->set('chunksize', (1024 * 1024));
if (!$stream->writeFile($file, $buffer))
{
Log::add(Text::sprintf('JLIB_FILESYSTEM_ERROR_WRITE_STREAMS', $file, $stream->getError()), Log::WARNING, 'jerror');
return false;
}
return true;
}
else
{
$FTPOptions = ClientHelper::getCredentials('ftp');
$pathObject = new PathWrapper;
if ($FTPOptions['enabled'] == 1)
{
// Connect the FTP client
$ftp = FtpClient::getInstance($FTPOptions['host'], $FTPOptions['port'], array(), $FTPOptions['user'], $FTPOptions['pass']);
// Translate path for the FTP account and use FTP write buffer to file
$file = $pathObject->clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $file), '/');
$ret = $ftp->write($file, $buffer);
}
else
{
$file = $pathObject->clean($file);
$ret = is_int(file_put_contents($file, $buffer)) ? true : false;
}
return $ret;
}
}
/**
* Append contents to a file
*
* @param string $file The full file path
* @param string $buffer The buffer to write
* @param boolean $useStreams Use streams
*
* @return boolean True on success
*
* @since 3.6.0
*/
public static function append($file, $buffer, $useStreams = false)
{
@set_time_limit(ini_get('max_execution_time'));
// If the file doesn't exist, just write instead of append
if (!file_exists($file))
{
return self::write($file, $buffer, $useStreams);
}
if ($useStreams)
{
$stream = Factory::getStream();
// Beef up the chunk size to a meg
$stream->set('chunksize', (1024 * 1024));
if ($stream->open($file, 'ab') && $stream->write($buffer) && $stream->close())
{
return true;
}
Log::add(Text::sprintf('JLIB_FILESYSTEM_ERROR_WRITE_STREAMS', $file, $stream->getError()), Log::WARNING, 'jerror');
return false;
}
else
{
// Initialise variables.
$FTPOptions = ClientHelper::getCredentials('ftp');
if ($FTPOptions['enabled'] == 1)
{
// Connect the FTP client
$ftp = FtpClient::getInstance($FTPOptions['host'], $FTPOptions['port'], array(), $FTPOptions['user'], $FTPOptions['pass']);
// Translate path for the FTP account and use FTP write buffer to file
$file = Path::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $file), '/');
$ret = $ftp->append($file, $buffer);
}
else
{
$file = Path::clean($file);
$ret = is_int(file_put_contents($file, $buffer, FILE_APPEND));
}
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
* @param boolean $useStreams True to use streams
* @param boolean $allowUnsafe Allow the upload of unsafe files
* @param boolean $safeFileOptions Options to \JFilterInput::isSafeFile
*
* @return boolean True on success
*
* @since 1.7.0
*/
public static function upload($src, $dest, $useStreams = false, $allowUnsafe = false, $safeFileOptions = array())
{
if (!$allowUnsafe)
{
$descriptor = array(
'tmp_name' => $src,
'name' => basename($dest),
'type' => '',
'error' => '',
'size' => '',
);
$isSafe = \JFilterInput::isSafeFile($descriptor, $safeFileOptions);
if (!$isSafe)
{
Log::add(Text::sprintf('JLIB_FILESYSTEM_ERROR_WARNFS_ERR03', $dest), Log::WARNING, 'jerror');
return false;
}
}
// Ensure that the path is valid and clean
$pathObject = new PathWrapper;
$dest = $pathObject->clean($dest);
// Create the destination directory if it does not exist
$baseDir = dirname($dest);
if (!file_exists($baseDir))
{
$folderObject = new FolderWrapper;
$folderObject->create($baseDir);
}
if ($useStreams)
{
$stream = Factory::getStream();
if (!$stream->upload($src, $dest))
{
Log::add(Text::sprintf('JLIB_FILESYSTEM_ERROR_UPLOAD', $stream->getError()), Log::WARNING, 'jerror');
return false;
}
return true;
}
else
{
$FTPOptions = ClientHelper::getCredentials('ftp');
$ret = false;
if ($FTPOptions['enabled'] == 1)
{
// Connect the FTP client
$ftp = FtpClient::getInstance($FTPOptions['host'], $FTPOptions['port'], array(), $FTPOptions['user'], $FTPOptions['pass']);
// Translate path for the FTP account
$dest = $pathObject->clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $dest), '/');
// Copy the file to the destination directory
if (is_uploaded_file($src) && $ftp->store($src, $dest))
{
unlink($src);
$ret = true;
}
else
{
Log::add(Text::sprintf('JLIB_FILESYSTEM_ERROR_WARNFS_ERR04', $src, $dest), Log::WARNING, 'jerror');
}
}
else
{
if (is_writeable($baseDir) && move_uploaded_file($src, $dest))
{
// Short circuit to prevent file permission errors
if ($pathObject->setPermissions($dest))
{
$ret = true;
}
else
{
Log::add(Text::_('JLIB_FILESYSTEM_ERROR_WARNFS_ERR01'), Log::WARNING, 'jerror');
}
}
else
{
Log::add(Text::sprintf('JLIB_FILESYSTEM_ERROR_WARNFS_ERR04', $src, $dest), Log::WARNING, 'jerror');
}
}
return $ret;
}
}
/**
* Wrapper for the standard file_exists function
*
* @param string $file File path
*
* @return boolean True if path is a file
*
* @since 1.7.0
*/
public static function exists($file)
{
$pathObject = new PathWrapper;
return is_file($pathObject->clean($file));
}
/**
* Returns the name, without any path.
*
* @param string $file File path
*
* @return string filename
*
* @since 1.7.0
* @deprecated 4.0 - Use basename() instead.
*/
public static function getName($file)
{
Log::add(__METHOD__ . ' is deprecated. Use native basename() syntax.', Log::WARNING, 'deprecated');
// Convert back slashes to forward slashes
$file = str_replace('\\', '/', $file);
$slash = strrpos($file, '/');
if ($slash !== false)
{
return substr($file, $slash + 1);
}
else
{
return $file;
}
}
}
home/digilove/public_html/libraries/regularlabs/src/File.php 0000644 00000036517 15234550257 0020262 0 ustar 00 <?php
/**
* @package Regular Labs Library
* @version 23.7.24631
*
* @author Peter van Westen <info@regularlabs.com>
* @link https://regularlabs.com
* @copyright Copyright © 2023 Regular Labs All Rights Reserved
* @license GNU General Public License version 2 or later
*/
namespace RegularLabs\Library;
defined('_JEXEC') or die;
use Joomla\CMS\Client\ClientHelper as JClientHelper;
use Joomla\CMS\Client\FtpClient as JFtpClient;
use Joomla\CMS\Factory as JFactory;
use Joomla\CMS\Filesystem\Folder as JFolder;
use Joomla\CMS\Filesystem\Path as JPath;
use Joomla\CMS\Language\Text as JText;
use Joomla\CMS\Log\Log as JLog;
use Joomla\CMS\Uri\Uri as JUri;
/**
* Class File
* @package RegularLabs\Library
*/
class File
{
/**
* 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
* @param int $min_age_in_minutes Minimum last modified age in minutes
*
* @return boolean True on success
*
* @since 11.1
*/
public static function delete($file, $show_messages = false, $min_age_in_minutes = 0)
{
$FTPOptions = JClientHelper::getCredentials('ftp');
$pathObject = new JPath;
$files = is_array($file) ? $file : [$file];
if ($FTPOptions['enabled'] == 1)
{
// Connect the FTP client
$ftp = JFtpClient::getInstance($FTPOptions['host'], $FTPOptions['port'], [], $FTPOptions['user'], $FTPOptions['pass']);
}
foreach ($files as $file)
{
$file = $pathObject->clean($file);
if ( ! is_file($file))
{
continue;
}
if ($min_age_in_minutes && floor((time() - filemtime($file)) / 60) < $min_age_in_minutes)
{
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
* @param int $min_age_in_minutes Minimum last modified age in minutes
*
* @return boolean True on success.
*/
public static function deleteFolder($path, $show_messages = false, $min_age_in_minutes = 0)
{
@set_time_limit(ini_get('max_execution_time'));
$pathObject = new JPath;
if ( ! $path)
{
$show_messages && JLog::add(__METHOD__ . ': ' . JText::_('JLIB_FILESYSTEM_ERROR_DELETE_BASE_DIRECTORY'), JLog::WARNING, 'jerror');
return false;
}
// 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, $min_age_in_minutes) !== 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, $min_age_in_minutes) !== true)
{
return false;
}
continue;
}
if ( ! self::deleteFolder($folder, $show_messages, $min_age_in_minutes))
{
return false;
}
}
// Skip if folder is not empty yet
if ( ! empty(JFolder::files($path, '.', false, true, [], []))
|| ! empty(JFolder::folders($path, '.', false, true, [], [])))
{
return true;
}
if (@rmdir($path))
{
return true;
}
$FTPOptions = JClientHelper::getCredentials('ftp');
if ($FTPOptions['enabled'] == 1)
{
// Connect the FTP client
$ftp = JFtpClient::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;
}
public static function getBaseName($url, $lowercase = false)
{
$url = StringHelper::normalize($url);
$basename = ltrim(basename($url), '/');
$parts = explode('?', $basename);
$basename = $parts[0];
if ($lowercase)
{
$basename = strtolower($basename);
}
return $basename;
}
public static function getDirName($url)
{
$url = StringHelper::normalize($url);
return rtrim(dirname($url), '/');
}
public static function getExtension($url)
{
$info = pathinfo($url);
if ( ! isset($info['extension']))
{
return '';
}
$ext = explode('?', $info['extension']);
return strtolower($ext[0]);
}
public static function getFileName($url, $lowercase = false)
{
$url = StringHelper::normalize($url);
$info = pathinfo($url);
$filename = $info['filename'] ?? $url;
if ($lowercase)
{
$filename = strtolower($filename);
}
return $filename;
}
public static function getFileTypes($type = 'images')
{
switch ($type)
{
case 'image':
case 'images':
return [
'bmp',
'flif',
'gif',
'jpe',
'jpeg',
'jpg',
'png',
'tiff',
'eps',
];
case 'audio':
return [
'aif',
'aiff',
'mp3',
'wav',
];
case 'video':
case 'videos':
return [
'3g2',
'3gp',
'avi',
'divx',
'f4v',
'flv',
'm4v',
'mov',
'mp4',
'mpe',
'mpeg',
'mpg',
'ogv',
'swf',
'webm',
'wmv',
];
case 'document':
case 'documents':
return [
'doc',
'docm',
'docx',
'dotm',
'dotx',
'odb',
'odc',
'odf',
'odg',
'odi',
'odm',
'odp',
'ods',
'odt',
'onepkg',
'onetmp',
'onetoc',
'onetoc2',
'otg',
'oth',
'otp',
'ots',
'ott',
'oxt',
'pdf',
'potm',
'potx',
'ppam',
'pps',
'ppsm',
'ppsx',
'ppt',
'pptm',
'pptx',
'rtf',
'sldm',
'sldx',
'thmx',
'xla',
'xlam',
'xlc',
'xld',
'xll',
'xlm',
'xls',
'xlsb',
'xlsm',
'xlsx',
'xlt',
'xltm',
'xltx',
'xlw',
];
case 'other':
case 'others':
return [
'css',
'csv',
'js',
'json',
'tar',
'txt',
'xml',
'zip',
];
default:
case 'all':
return array_merge(
self::getFileTypes('images'),
self::getFileTypes('audio'),
self::getFileTypes('videos'),
self::getFileTypes('documents'),
self::getFileTypes('other')
);
}
}
/**
* 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;
}
// some/url/to/a/file.ext
// > some/url/to/a
public static function isDocument($url)
{
return self::isMedia($url, self::getFileTypes('documents'));
}
// some/url/to/a/file.ext
// > file.ext
public static function isExternal($url)
{
if (strpos($url, '://') === false)
{
return false;
}
// hostname: give preference to SERVER_NAME, because this includes subdomains
$hostname = ($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : $_SERVER['HTTP_HOST'];
return ! (strpos(RegEx::replace('^.*?://', '', $url), $hostname) === 0);
}
// some/url/to/a/file.ext
// > file
public static function isExternalVideo($url)
{
return (strpos($url, 'youtu.be') !== false
|| strpos($url, 'youtube.com') !== false
|| strpos($url, 'vimeo.com') !== false
);
}
// some/url/to/a/file.ext
// > ext
public static function isImage($url)
{
return self::isMedia($url, self::getFileTypes('images'));
}
public static function isInternal($url)
{
return ! self::isExternal($url);
}
public static function isMedia($url, $filetypes = [])
{
$filetype = self::getExtension($url);
if ( ! $filetype)
{
return false;
}
if ( ! is_array($filetypes))
{
$filetypes = [$filetypes];
}
if (count($filetypes) == 1 && strpos($filetypes[0], ',') !== false)
{
$filetypes = ArrayHelper::toArray($filetypes[0]);
}
$filetypes = ($filetypes ?? null) ?: self::getFileTypes();
return in_array($filetype, $filetypes);
}
public static function isVideo($url)
{
return self::isMedia($url, self::getFileTypes('videos'));
}
public static function trimFolder($folder)
{
return trim(str_replace(['\\', '//'], '/', $folder), '/');
}
/**
* 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 /
[$extension, $file] = explode('/', $file, 2);
$paths[] = '/media/' . $extension . '/' . $type;
$paths[] = '/templates/' . $template . '/' . $type . '/system';
$paths[] = '/media/system/' . $type;
$paths[] = '';
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;
}
}
home/digilove/public_html/110/libraries/src/Filesystem/File.php 0000644 00000037010 15235325103 0020360 0 ustar 00 <?php
/**
* Joomla! Content Management System
*
* @copyright (C) 2006 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\Filesystem;
defined('JPATH_PLATFORM') or die;
use Joomla\CMS\Factory;
use Joomla\CMS\Log\Log;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Filesystem\Path;
use Joomla\CMS\Filesystem\Wrapper\PathWrapper;
use Joomla\CMS\Filesystem\Wrapper\FolderWrapper;
use Joomla\CMS\Client\ClientHelper;
use Joomla\CMS\Client\FtpClient;
/**
* A File handling class
*
* @since 1.7.0
*/
class File
{
/**
* Gets the extension of a file name
*
* @param string $file The file name
*
* @return string The file extension
*
* @since 1.7.0
*/
public static function getExt($file)
{
// String manipulation should be faster than pathinfo() on newer PHP versions.
$dot = strrpos($file, '.');
if ($dot === false)
{
return '';
}
$ext = substr($file, $dot + 1);
// Extension cannot contain slashes.
if (strpos($ext, '/') !== false || (DIRECTORY_SEPARATOR === '\\' && strpos($ext, '\\') !== false))
{
return '';
}
return $ext;
}
/**
* Strips the last extension off of a file name
*
* @param string $file The file name
*
* @return string The file name without the extension
*
* @since 1.7.0
*/
public static 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.7.0
*/
public static function makeSafe($file)
{
// Remove any trailing dots, as those aren't ever valid file names.
$file = rtrim($file, '.');
$regex = array('#(\.){2,}#', '#[^A-Za-z0-9\.\_\- ]#', '#^\.#');
return trim(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
* @param boolean $useStreams True to use streams
*
* @return boolean True on success
*
* @since 1.7.0
*/
public static function copy($src, $dest, $path = null, $useStreams = false)
{
$pathObject = new PathWrapper;
// Prepend a base path if it exists
if ($path)
{
$src = $pathObject->clean($path . '/' . $src);
$dest = $pathObject->clean($path . '/' . $dest);
}
// Check src path
if (!is_readable($src))
{
Log::add(Text::sprintf('JLIB_FILESYSTEM_ERROR_JFILE_FIND_COPY', $src), Log::WARNING, 'jerror');
return false;
}
if ($useStreams)
{
$stream = Factory::getStream();
if (!$stream->copy($src, $dest))
{
Log::add(Text::sprintf('JLIB_FILESYSTEM_ERROR_JFILE_STREAMS', $src, $dest, $stream->getError()), Log::WARNING, 'jerror');
return false;
}
return true;
}
else
{
$FTPOptions = ClientHelper::getCredentials('ftp');
if ($FTPOptions['enabled'] == 1)
{
// Connect the FTP client
$ftp = FtpClient::getInstance($FTPOptions['host'], $FTPOptions['port'], array(), $FTPOptions['user'], $FTPOptions['pass']);
// If the parent folder doesn't exist we must create it
if (!file_exists(dirname($dest)))
{
$folderObject = new FolderWrapper;
$folderObject->create(dirname($dest));
}
// Translate the destination path for the FTP account
$dest = $pathObject->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))
{
Log::add(Text::sprintf('JLIB_FILESYSTEM_ERROR_COPY_FAILED_ERR01', $src, $dest), Log::WARNING, 'jerror');
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.7.0
*/
public static function delete($file)
{
$FTPOptions = ClientHelper::getCredentials('ftp');
$pathObject = new PathWrapper;
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
$ftp = FtpClient::getInstance($FTPOptions['host'], $FTPOptions['port'], array(), $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);
// 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 = $pathObject->clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $file), '/');
if (!$ftp->delete($file))
{
// FTP connector throws an error
return false;
}
}
else
{
$filename = basename($file);
Log::add(Text::sprintf('JLIB_FILESYSTEM_DELETE_FAILED', $filename), Log::WARNING, 'jerror');
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
* @param boolean $useStreams True to use streams
*
* @return boolean True on success
*
* @since 1.7.0
*/
public static function move($src, $dest, $path = '', $useStreams = false)
{
$pathObject = new PathWrapper;
if ($path)
{
$src = $pathObject->clean($path . '/' . $src);
$dest = $pathObject->clean($path . '/' . $dest);
}
// Check src path
if (!is_readable($src))
{
Log::add(Text::_('JLIB_FILESYSTEM_CANNOT_FIND_SOURCE_FILE'), Log::WARNING, 'jerror');
return false;
}
if ($useStreams)
{
$stream = Factory::getStream();
if (!$stream->move($src, $dest))
{
Log::add(Text::sprintf('JLIB_FILESYSTEM_ERROR_JFILE_MOVE_STREAMS', $stream->getError()), Log::WARNING, 'jerror');
return false;
}
return true;
}
else
{
$FTPOptions = ClientHelper::getCredentials('ftp');
if ($FTPOptions['enabled'] == 1)
{
// Connect the FTP client
$ftp = FtpClient::getInstance($FTPOptions['host'], $FTPOptions['port'], array(), $FTPOptions['user'], $FTPOptions['pass']);
// Translate path for the FTP account
$src = $pathObject->clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $src), '/');
$dest = $pathObject->clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $dest), '/');
// Use FTP rename to simulate move
if (!$ftp->rename($src, $dest))
{
Log::add(Text::_('JLIB_FILESYSTEM_ERROR_RENAME_FILE'), Log::WARNING, 'jerror');
return false;
}
}
else
{
if (!@ rename($src, $dest))
{
Log::add(Text::_('JLIB_FILESYSTEM_ERROR_RENAME_FILE'), Log::WARNING, 'jerror');
return false;
}
}
return true;
}
}
/**
* Read the contents of a file
*
* @param string $filename The full file path
* @param boolean $incpath Use include path
* @param integer $amount Amount of file to read
* @param integer $chunksize Size of chunks to read
* @param integer $offset Offset of the file
*
* @return mixed Returns file contents or boolean False if failed
*
* @since 1.7.0
* @deprecated 4.0 - Use the native file_get_contents() instead.
*/
public static function read($filename, $incpath = false, $amount = 0, $chunksize = 8192, $offset = 0)
{
Log::add(__METHOD__ . ' is deprecated. Use native file_get_contents() syntax.', Log::WARNING, 'deprecated');
$data = null;
if ($amount && $chunksize > $amount)
{
$chunksize = $amount;
}
if (false === $fh = fopen($filename, 'rb', $incpath))
{
Log::add(Text::sprintf('JLIB_FILESYSTEM_ERROR_READ_UNABLE_TO_OPEN_FILE', $filename), Log::WARNING, 'jerror');
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 = '';
/*
* While it's:
* 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
* @param boolean $useStreams Use streams
*
* @return boolean True on success
*
* @since 1.7.0
*/
public static function write($file, $buffer, $useStreams = false)
{
@set_time_limit(ini_get('max_execution_time'));
// If the destination directory doesn't exist we need to create it
if (!file_exists(dirname($file)))
{
$folderObject = new FolderWrapper;
if ($folderObject->create(dirname($file)) == false)
{
return false;
}
}
if ($useStreams)
{
$stream = Factory::getStream();
// Beef up the chunk size to a meg
$stream->set('chunksize', (1024 * 1024));
if (!$stream->writeFile($file, $buffer))
{
Log::add(Text::sprintf('JLIB_FILESYSTEM_ERROR_WRITE_STREAMS', $file, $stream->getError()), Log::WARNING, 'jerror');
return false;
}
return true;
}
else
{
$FTPOptions = ClientHelper::getCredentials('ftp');
$pathObject = new PathWrapper;
if ($FTPOptions['enabled'] == 1)
{
// Connect the FTP client
$ftp = FtpClient::getInstance($FTPOptions['host'], $FTPOptions['port'], array(), $FTPOptions['user'], $FTPOptions['pass']);
// Translate path for the FTP account and use FTP write buffer to file
$file = $pathObject->clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $file), '/');
$ret = $ftp->write($file, $buffer);
}
else
{
$file = $pathObject->clean($file);
$ret = is_int(file_put_contents($file, $buffer)) ? true : false;
}
return $ret;
}
}
/**
* Append contents to a file
*
* @param string $file The full file path
* @param string $buffer The buffer to write
* @param boolean $useStreams Use streams
*
* @return boolean True on success
*
* @since 3.6.0
*/
public static function append($file, $buffer, $useStreams = false)
{
@set_time_limit(ini_get('max_execution_time'));
// If the file doesn't exist, just write instead of append
if (!file_exists($file))
{
return self::write($file, $buffer, $useStreams);
}
if ($useStreams)
{
$stream = Factory::getStream();
// Beef up the chunk size to a meg
$stream->set('chunksize', (1024 * 1024));
if ($stream->open($file, 'ab') && $stream->write($buffer) && $stream->close())
{
return true;
}
Log::add(Text::sprintf('JLIB_FILESYSTEM_ERROR_WRITE_STREAMS', $file, $stream->getError()), Log::WARNING, 'jerror');
return false;
}
else
{
// Initialise variables.
$FTPOptions = ClientHelper::getCredentials('ftp');
if ($FTPOptions['enabled'] == 1)
{
// Connect the FTP client
$ftp = FtpClient::getInstance($FTPOptions['host'], $FTPOptions['port'], array(), $FTPOptions['user'], $FTPOptions['pass']);
// Translate path for the FTP account and use FTP write buffer to file
$file = Path::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $file), '/');
$ret = $ftp->append($file, $buffer);
}
else
{
$file = Path::clean($file);
$ret = is_int(file_put_contents($file, $buffer, FILE_APPEND));
}
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
* @param boolean $useStreams True to use streams
* @param boolean $allowUnsafe Allow the upload of unsafe files
* @param boolean $safeFileOptions Options to \JFilterInput::isSafeFile
*
* @return boolean True on success
*
* @since 1.7.0
*/
public static function upload($src, $dest, $useStreams = false, $allowUnsafe = false, $safeFileOptions = array())
{
if (!$allowUnsafe)
{
$descriptor = array(
'tmp_name' => $src,
'name' => basename($dest),
'type' => '',
'error' => '',
'size' => '',
);
$isSafe = \JFilterInput::isSafeFile($descriptor, $safeFileOptions);
if (!$isSafe)
{
Log::add(Text::sprintf('JLIB_FILESYSTEM_ERROR_WARNFS_ERR03', $dest), Log::WARNING, 'jerror');
return false;
}
}
// Ensure that the path is valid and clean
$pathObject = new PathWrapper;
$dest = $pathObject->clean($dest);
// Create the destination directory if it does not exist
$baseDir = dirname($dest);
if (!file_exists($baseDir))
{
$folderObject = new FolderWrapper;
$folderObject->create($baseDir);
}
if ($useStreams)
{
$stream = Factory::getStream();
if (!$stream->upload($src, $dest))
{
Log::add(Text::sprintf('JLIB_FILESYSTEM_ERROR_UPLOAD', $stream->getError()), Log::WARNING, 'jerror');
return false;
}
return true;
}
else
{
$FTPOptions = ClientHelper::getCredentials('ftp');
$ret = false;
if ($FTPOptions['enabled'] == 1)
{
// Connect the FTP client
$ftp = FtpClient::getInstance($FTPOptions['host'], $FTPOptions['port'], array(), $FTPOptions['user'], $FTPOptions['pass']);
// Translate path for the FTP account
$dest = $pathObject->clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $dest), '/');
// Copy the file to the destination directory
if (is_uploaded_file($src) && $ftp->store($src, $dest))
{
unlink($src);
$ret = true;
}
else
{
Log::add(Text::sprintf('JLIB_FILESYSTEM_ERROR_WARNFS_ERR04', $src, $dest), Log::WARNING, 'jerror');
}
}
else
{
if (is_writeable($baseDir) && move_uploaded_file($src, $dest))
{
// Short circuit to prevent file permission errors
if ($pathObject->setPermissions($dest))
{
$ret = true;
}
else
{
Log::add(Text::_('JLIB_FILESYSTEM_ERROR_WARNFS_ERR01'), Log::WARNING, 'jerror');
}
}
else
{
Log::add(Text::sprintf('JLIB_FILESYSTEM_ERROR_WARNFS_ERR04', $src, $dest), Log::WARNING, 'jerror');
}
}
return $ret;
}
}
/**
* Wrapper for the standard file_exists function
*
* @param string $file File path
*
* @return boolean True if path is a file
*
* @since 1.7.0
*/
public static function exists($file)
{
$pathObject = new PathWrapper;
return is_file($pathObject->clean($file));
}
/**
* Returns the name, without any path.
*
* @param string $file File path
*
* @return string filename
*
* @since 1.7.0
* @deprecated 4.0 - Use basename() instead.
*/
public static function getName($file)
{
Log::add(__METHOD__ . ' is deprecated. Use native basename() syntax.', Log::WARNING, 'deprecated');
// Convert back slashes to forward slashes
$file = str_replace('\\', '/', $file);
$slash = strrpos($file, '/');
if ($slash !== false)
{
return substr($file, $slash + 1);
}
else
{
return $file;
}
}
}