Your IP : 216.73.216.240
<?php
/*
* @package Tech Fry Library
* @license https://www.gnu.org/licenses/gpl-3.0.en.html
*/
namespace TechFry\Library;
defined('_JEXEC') or die;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Image\Image;
use Joomla\CMS\Filter\OutputFilter;
use Joomla\Filesystem\Folder;
use Joomla\Filesystem\File;
// Class for Handling Folders and Files
// To Do: Add methods for create and extract ZIP file Joomla\Archive\ZIP
class TFile
{
/*
A. Files - handle_file/upload_file, file_exists, get_ext, strip_ext, make_safe, format_size_units, create, copy,
delete, move, wrtite, upload, read_csv, get_info
B. Folders - folder_exists, create_folder, copy_folder, delete_folder, move_folder, get_files, get_folders, make_safe
*/
public $base_path;
public $folder;
public $file;
public $files;
public function __construct($config = [])
{
$this->base_path = $config['base_path'] ?? JPATH_ROOT;
$this->folder = $config['folder'] ?? '';
$this->file = $config['file'] ?? '';
}
// A1. Handle uploaded file
public function handle_file($file, $options = [])
{
// $file array - name, type, tmp_name, error, size
// $options - allow_ext, max_size, save_folder, new_name
// 1. Check if file is missing
if ($file['name'] == '')
{
return;
}
// 2. Check for error code
if ($file['error'])
{
Factory::getApplication()->enqueueMessage('File Upload Error: ' . $file['error'], 'error');
return false;
}
// 3. Check for valid file extension
if (isset($options['allow_ext']))
{
$allow_ext = \is_array($options['allow_ext']) ? $options['allow_ext'] : explode(',', $options['allow_ext']);
$ext = $this->get_extension($file['name']);
if (!in_array(strtolower($ext), $allow_ext))
{
Factory::getApplication()->enqueueMessage(Text::_('TF_ERROR_INVALID_FILE_TYPE'), 'error');
return false;
}
}
// 4. Check for file size
if (isset($options['max_size']) && ($file['size'] > $options['max_size']))
{
Factory::getApplication()->enqueueMessage(Text::_('TF_ERROR_INVALID_FILE_SIZE'), 'error');
return false;
}
// 5. File name
if (isset($options['new_name']) && $options['new_name'])
{
$new_name = $options['new_name'] . '.' . $ext;
}
else
{
$name = File::stripExt($file['name']);
$name = str_replace(' ', '-', $name);
$name = strtolower($name);
$name = $this->make_safe($name);
$new_name = $name . '_' . mt_rand(100,999) . '.' . $ext;
}
// 6. Location or destination to Upload file
if (isset($options['save_folder']) && trim($options['save_folder']))
{
$upload_folder = $options['save_folder'];
}
else
{
$upload_folder = 'images/tfupload';
}
// Dynamic fields in save folder (user and dates)
$d = new TDynamic($upload_folder);
$d->replace_dates();
$upload_folder = $d->replace_user();
$src = $file['tmp_name'];
$save_path = $upload_folder . '/' . $new_name;
$dest = JPATH_SITE . '/' . $save_path;
return (File::upload($src, $dest)) ? $save_path : false;
}
// Alias for handle file
public function upload_file($file, $options = [])
{
$this->handle_file($file, $options);
}
// A2. Check file for its existence
public function file_exists($file_name)
{
$file = $this->base_path;
if ($this->folder)
{
$file .= DIRECTORY_SEPARATOR . $this->folder;
}
$file .= DIRECTORY_SEPARATOR . $file_name;
return file_exists($file) ? true : false;
}
// A3. Get extension of file
public function get_extension($file_name)
{
return File::getExt($file_name);
}
// A4. Strip or remove extension of file
public function strip_extension($file_name)
{
$file = $this->base_path;
if ($this->folder)
{
$file .= DIRECTORY_SEPARATOR . $this->folder;
}
$file .= DIRECTORY_SEPARATOR . $file_name;
return File::stripExt($file);
}
// A5. Make name of file safe to use
public function make_safe($file)
{
return File::makeSafe($file);
}
// A6. Convert bytes to KB or MB
public function format_size_units($bytes)
{
if ($bytes >= 1073741824)
{
$bytes = number_format($bytes / 1073741824, 2) . ' GB';
}
elseif ($bytes >= 1048576)
{
$bytes = number_format($bytes / 1048576, 2) . ' MB';
}
elseif ($bytes >= 1024)
{
$bytes = number_format($bytes / 1024, 2) . ' KB';
}
elseif ($bytes > 1)
{
$bytes = $bytes . ' bytes';
}
elseif ($bytes == 1)
{
$bytes = $bytes . ' byte';
}
else
{
$bytes = '0 bytes';
}
return $bytes;
}
public function create_file($file_name, $content = '', $append = false)
{
$file = $this->base_path;
if ($this->folder)
{
$file .= DIRECTORY_SEPARATOR . $this->folder;
}
$file .= DIRECTORY_SEPARATOR . $file_name;
File::write($file, $content, false, $append);
}
// A7. Copy file
public function copy_file($src_file, $dest_file)
{
$src = $this->base_path;
$dest = $this->base_path;
if ($this->folder)
{
$src .= DIRECTORY_SEPARATOR . $this->folder;
$dest .= DIRECTORY_SEPARATOR . $this->folder;
}
$src .= DIRECTORY_SEPARATOR . $src_file;
$dest .= DIRECTORY_SEPARATOR . $dest_file;
File::copy($src, $dest);
}
// A8. Delete file
public function delete_file($file_name)
{
if (!file_exists($file_name))
{
return;
}
$file = $this->base_path;
if ($this->folder)
{
$file .= DIRECTORY_SEPARATOR . $this->folder;
}
$file .= DIRECTORY_SEPARATOR . $file_name;
File::delete($file);
}
// A9. Move file; can be used for renaming; destination folder should exist
public function move_file($src_file, $dest_file)
{
$src = $this->base_path;
$dest = $this->base_path;
if ($this->folder)
{
$src .= DIRECTORY_SEPARATOR . $this->folder;
$dest .= DIRECTORY_SEPARATOR . $this->folder;
}
$src .= DIRECTORY_SEPARATOR . $src_file;
$dest .= DIRECTORY_SEPARATOR . $dest_file;
File::move($src, $dest);
}
// A10. Create file and add content to it; Also creates folders
public function write_file($file_name, $content = '')
{
$this->create_file($file_name, $content);
}
// A11. Append content to file
public function append_file($file_name, $content = '')
{
$this->create_file($file_name, $content, true);
}
// A12. Read from CSV file and store data as an array
public function read_csv($file_path)
{
if (!file_exists($file_path))
{
Factory::getApplication()->enqueueMessage('File Not Found: ' . $file_path, 'error');
return false;
}
$data = [];
if (($handle = fopen($file_path, "r")) !== false)
{
while (($row = fgetcsv($handle, null, ',', '"', "\\")) !== false)
{
$data[] = $row;
}
fclose($handle);
}
else
{
Factory::getApplication()->enqueueMessage('Failed to Open File: ' . $file_path, 'error');
}
return $data;
}
// A13. Get information about file
public function get_info($file)
{
// dirname, basename, extension, filename
$info = (object) pathinfo($file);
if (file_exists($file))
{
$info->name = basename($file);
$info->path = realpath($file);
$info->filesize = filesize($file);
$info->ext = $this->get_extension($file);
$info->last_accessed = date('d M Y H:i:s', fileatime($file));
$info->modified = date('d M Y H:i:s', filemtime($file));
}
return $info;
}
// B1. Check if folder exists
public function folder_exists($folder)
{
// To Do: use Folder::is_dir()
$path = $this->base_path . DIRECTORY_SEPARATOR . $folder;
if (\is_dir($path))
{
return true;
}
return false;
}
// B2. Create folder
public function create_folder()
{
$path = $this->base_path;
if ($this->folder)
{
$path.= DIRECTORY_SEPARATOR . $this->folder;
}
Folder::create($path);
}
// B3. Copy folder
public function copy_folder($destination)
{
if ($this->folder_exists($destination))
{
return;
}
$src = $this->base_path;
if ($this->folder)
{
$src.= DIRECTORY_SEPARATOR . $this->folder;
}
$dest = $this->base_path . DIRECTORY_SEPARATOR . $destination;
Folder::copy($src, $dest);
}
// B4. Delete folder and all its files
public function delete_folder()
{
$path = $this->base_path;
if ($this->folder)
{
$path.= DIRECTORY_SEPARATOR . $this->folder;
}
Folder::delete($path);
}
// B5. Move folder
public function move_folder($destination)
{
if ($this->folder_exists($destination))
{
return;
}
$src = $this->base_path;
if ($this->folder)
{
$src.= DIRECTORY_SEPARATOR . $this->folder;
}
$dest = $this->base_path . DIRECTORY_SEPARATOR . $destination;
Folder::move($src, $dest);
}
// B6. Get all files in a folder
public function get_files()
{
$path = $this->base_path;
if ($this->folder)
{
$path.= DIRECTORY_SEPARATOR . $this->folder;
}
return Folder::files($path);
}
// B7. Get all folders in a folder
public function get_folders()
{
$path = $this->base_path;
if ($this->folder)
{
$path.= DIRECTORY_SEPARATOR . $this->folder;
}
return Folder::folders($path);
}
}