<?php
// ----------------------------------------------------------------------------
//
// resman.class.php - PHP Resource Manager, ver.0.01 (September 18, 2005)
//
// File structure:
// resman.class.php Class definition
// basetypes.inc.php Default filetypes
// template.inc.php XHTML template
// languages/ Language descriptions
// images/ Images
// images/icons/ Filetype icons
//
// Description:
// The class allows to manage (upload, rename, delete, etc.) files inside the
// defined directory.
//
// Author:
// Vagharshak Tozalakyan <hide@address.com>
// This module was written by author on his free time.
//
// Warning:
// This class is non commercial work. It should not have unexpected results.
// However, if any damage is caused by this class the author can not be
// responsible. The use of this class is at the risk of the user.
//
// Requirements:
// PHP >= 4.1.0
//
// ----------------------------------------------------------------------------
// Sorting type constants
define('RM_SORT_NO', -1); // no sort
define('RM_SORT_TYPE', 0); // sort by file type
define('RM_SORT_NAME', 1); // sort by filename
define('RM_SORT_SIZE', 2); // sort by file size
define('RM_SORT_MODIF', 3); // sort by modification time
// Sorting order constants
define('RM_SORT_ASC', 0); // sort ascending
define('RM_SORT_DESC', 1); // sort descending
// Class definition
class ResManager
{
// Relative path to the directory in which class files are stored.
var $class_path = '';
// Full or relative path to the directory we wish to manage. No trialing slashes.
var $browse_dir = '';
// Full or relative URL of the directory we wish to manage. No trialing slashes.
var $browse_url = '';
// The array of known file types.
// $file_types[<extension>] = array(<file type>, <icon in the images/icons/>)
var $file_types = array();
// The array of shell glob style patterns, e.g. array('*.gif', '*.htm*').
var $filter = array('*');
// Show files with dot-started names (e.g. '.htaccess')?
var $show_specials = false;
// File list sorting mode, set by one of sort type constants defined above.
var $sort_type = RM_SORT_NO;
// File list sorting order, set by one of sort order constants defined above.
var $sort_order = RM_SORT_ASC;
// PHP style date format.
var $tm_format = 'd-m-y H:i:s';
// The width of file type icons in pixels.
var $icon_width = 32;
// The height of file type icons in pixels.
var $icon_height = 32;
// The array of shell glob style patterns used against uploaded files.
var $upl_filter = array('*');
// Maximal size of uploaded files in bytes.
var $upl_max_size = 2097152;
// Number of file inputs displayed.
var $upl_num = 3;
// The array of language-specific strings.
var $strings = array();
// Success message string.
var $ok_msg = '';
// Error message string.
var $err_msg = '';
/*
Description:
Class constructor.
Prototype:
void ResManager(string class_path[, string lang])
Parameters:
class_path - relative path to the directory with class files;
lang - current language.
*/
function ResManager($class_path, $lang = 'english')
{
require_once($class_path . '/basetypes.inc.php');
require_once($class_path . '/languages/' . $lang . '.inc.php');
$this->class_path = $class_path;
$this->file_types = $rm_types;
$this->strings = $rm_l;
}
/*
Description:
Print out the page.
Prototype:
void Show()
*/
function Show()
{
$this->err_msg = '';
$this->ok_msg = '';
if(isset($_GET['oper']) && isset($_GET['fname']) && $_GET['oper'] == 'delete')
{
$this->DeleteFile($_GET['fname']);
}
elseif(isset($_GET['oper']) && isset($_GET['fname']) && isset($_GET['new'])
&& $_GET['oper'] == 'rename')
{
$this->RenameFile($_GET['fname'], $_GET['new']);
}
elseif(isset($_FILES['upload']))
{
for ($i = 0; $i < count($_FILES['upload']['name']); $i++)
{
$name = $_FILES['upload']['name'][$i];
$tmp_name = $_FILES['upload']['tmp_name'][$i];
$size = $_FILES['upload']['size'][$i];
$this->UploadFile($name, $tmp_name, $size);
}
}
$tpl_files = $this->GetFiles();
$tpl_charset = $this->strings['charset'];
$tpl_title = $this->strings['title'];
$tpl_font = $this->strings['font'];
$tpl_enter_new_name = $this->strings['enter_new_name'];
$tpl_del_confirm = $this->strings['del_confirm'];
$tpl_err_msg = $this->err_msg;
$tpl_ok_msg = $this->ok_msg;
$tpl_type_hdr = $this->strings['type_hdr'];
$tpl_fname_hdr = $this->strings['fname_hdr'];
$tpl_size_hdr = $this->strings['size_hdr'];
$tpl_modif_hdr = $this->strings['modif_hdr'];
$tpl_fl_num = count($tpl_files);
$tpl_icon_width = $this->icon_width;
$tpl_icon_height = $this->icon_height;
$tpl_refresh_img = $this->class_path . '/images/refresh.png';
$tpl_rename_img = $this->class_path . '/images/rename.png';
$tpl_delete_img = $this->class_path . '/images/delete.png';
$tpl_refresh = $this->strings['refresh'];
$tpl_rename = $this->strings['rename'];
$tpl_delete = $this->strings['delete'];
$tpl_upl_max = $this->upl_max_size;
$tpl_upl_num = $this->upl_num;
$tpl_upload = $this->strings['upload'];
require_once($this->class_path . '/template.inc.php');
}
/*
Description:
Retrieve the sorted list of files.
Prototype:
array GetFiles()
Return:
$files[$i]['name'] - filename;
$files[$i]['url'] - file url;
$files[$i]['size'] - file size in bytes;
$files[$i]['modif'] - formatted modification time;
$files[$i]['_modif'] - modification time as UNIX timestamp;
$files[$i]['hint'] - file type string;
$files[$i]['icon'] - the path to the file type icon.
*/
function GetFiles()
{
$i = 0;
$files = array();
clearstatcache();
$dir_hndl = opendir($this->browse_dir);
if (!$dir_hndl)
{
return;
}
while (false !== ($fname = readdir($dir_hndl)))
{
$full_path = $this->browse_dir . '/' . $fname;
$full_url = $this->browse_url . '/' . $fname;
if (!$this->show_specials && strpos($fname, '.') === 0)
{
continue;
}
elseif ($fname == '.' || $fname == '..')
{
continue;
}
elseif (!is_file($full_path))
{
continue;
}
elseif (!$this->MatchFileName($this->filter, $fname))
{
continue;
}
$pi = pathinfo($full_path);
$ext = (isset($pi['extension']) ? $pi['extension'] : '');
$files[$i]['name'] = $fname;
$files[$i]['url'] = $full_url;
$files[$i]['size'] = filesize($full_path);
$files[$i]['_modif'] = filemtime($full_path);
$files[$i]['modif'] = date($this->tm_format, $files[$i]['_modif']);
$icons_path = $this->class_path . '/images/icons';
if (isset($this->file_types[$ext]))
{
$files[$i]['hint'] = $this->file_types[$ext][0];
$files[$i]['icon'] = $icons_path . '/' . $this->file_types[$ext][1];
}
else
{
$files[$i]['hint'] = $this->file_types['unknown'][0];
$files[$i]['icon'] = $icons_path . '/' . $this->file_types['unknown'][1];
}
$i++;
}
closedir($dir_hndl);
if ($this->sort_type != RM_SORT_NO)
{
usort($files, array($this, 'CompareFiles'));
}
return $files;
}
/*
Description:
Check if the filename matches the pattern.
Prototype:
bool MatchFileName(array filter, string fname)
Parameters:
filter - array of shell glob style patterns;
fname - filename.
Return:
True if the file matches specified patterns or false otherwise.
*/
function MatchFileName($filter, $fname)
{
$fname = basename($fname);
$num_patterns = count($filter);
if (!is_array($filter) || !$num_patterns)
{
$this->filter = array('*');
$num_patterns = 1;
}
$match = false;
for ($i = 0; $i < $num_patterns; $i++)
{
$pattern = str_replace('.', '\\.', $filter[$i]);
$pattern = str_replace('*', '(.*?)', $pattern);
$pattern = '#^' . $pattern . '$#i';
if (preg_match($pattern, $fname))
{
$match = true;
break;
}
}
return $match;
}
/*
Description:
Files comparison callback.
Prototype:
int CompareFiles(string file1, string file2)
Parameters:
file1, file2 - files to compare.
Return:
Comparison result integer.
*/
function CompareFiles($file1, $file2)
{
if ($this->sort_type == RM_SORT_TYPE)
{
$a = strtolower($file1['hint']);
$b = strtolower($file2['hint']);
}
elseif ($this->sort_type == RM_SORT_NAME)
{
$a = strtolower($file1['name']);
$b = strtolower($file2['name']);
}
elseif ($this->sort_type == RM_SORT_SIZE)
{
$a = $file1['size'];
$b = $file2['size'];
}
elseif ($this->sort_type == RM_SORT_MODIF)
{
$a = $file1['_modif'];
$b = $file2['_modif'];
}
$result = 0;
if ($a == $b)
{
$result = 0;
}
elseif ($this->sort_order == RM_SORT_DESC)
{
$result = (($a > $b) ? -1 : 1);
}
else
{
$result = (($a > $b) ? 1 : -1);
}
return $result;
}
/*
Description:
Remove the file from the browse directory.
Prototype:
void DleteFile(string fname)
Parameters:
fname - filename to delete
*/
function DeleteFile($fname)
{
$del_file = $this->browse_dir . '/' . basename($fname);
if (!@unlink($del_file))
{
$this->err_msg = $this->strings['delete_err'];
}
else
{
$this->ok_msg = $this->strings['deleted'];
}
}
/*
Description:
Rename the file within the browse directory.
Prototype:
void RenameFile(string fname, string newname)
Parameters:
fname - old filename;
newname - new filename.
*/
function RenameFile($fname, $newname)
{
$old_name = $this->browse_dir . '/' . basename($fname);
$new_name =$this->browse_dir . '/' . basename($newname);
if (file_exists($new_name))
{
$this->err_msg = $this->strings['exist_err'];
}
elseif (!@rename($old_name, $new_name))
{
$this->err_msg = $this->strings['rename_err'];
}
else
{
$this->ok_msg = $this->strings['renamed'];
}
}
/*
Description:
Upload the file to the browse directory.
Prototype:
void UploadFile(string name, string tmp_name, int size)
Parameters:
name - the original name of the file on the client machine;
tmp_name - the temporary filename of the file on the server;
size - the size, in bytes, of the uploaded file.
*/
function UploadFile($name, $tmp_name, $size)
{
if(empty($tmp_name) || $tmp_name == 'none' || !$size)
{
return;
}
$dest = $this->browse_dir . '/' . basename($name);
if (!$this->MatchFileName($this->upl_filter, $name))
{
$err = sprintf($this->strings['upl_ext_err'], $name);
$this->err_msg .= (empty($this->err_msg) ? $err : '<br />' . $err);
}
elseif ($size > $this->upl_max_size)
{
$err = sprintf($this->strings['upl_size_err'], $name);
$this->err_msg .= (empty($this->err_msg) ? $err : '<br />' . $err);
}
elseif(file_exists($dest))
{
$err = sprintf($this->strings['upl_exist_err'], $name);
$this->err_msg .= (empty($this->err_msg) ? $err : '<br />' . $err);
}
elseif (!@move_uploaded_file($tmp_name, $dest))
{
$err = sprintf($this->strings['upl_err'], $name);
$this->err_msg .= (empty($this->err_msg) ? $err : '<br />' . $err);
}
else
{
$ok = sprintf($this->strings['uploaded'], $name);
$this->ok_msg .= (empty($this->ok_msg) ? $ok : '<br />' . $ok);
}
}
}
?>