Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Upload classes nearly ready.

  • Loading branch information...
commit 3db07eeadbffe3eb7656fa24435dc8da364a4feb 1 parent 2557f75
@parvas authored
View
65 system/classes/File.php
@@ -2,14 +2,15 @@
class File {
- protected static $_instance;
+ protected static $_errors = array();
+
protected $_file = array();
protected $_directory;
protected $_max_size;
protected $_types = array();
protected $_allowed_mimes = array();
- protected $_errors = array();
protected $_create_folder = false;
+ protected $_remove_whitespace = false;
protected $_mimes = array(
'hqx' => 'application/mac-binhex40',
@@ -109,28 +110,34 @@ class File {
* @param type $file
* @return File
*/
- public static function instance($file = null)
+ public static function factory($field, $remove_whitespace = false)
{
- if (static::$_instance)
- {
- return static::$_instance;
- }
-
- return static::$_instance = new File($file);
+ return new File($field, $remove_whitespace);
}
- protected function __construct($file)
+ protected function __construct($field, $remove_whitespace)
{
- $this->_file = Module::instance()->files($file);
+ $this->_remove_whitespace = $remove_whitespace;
+ $this->_file = Module::instance()->files($field);
}
public function upload()
{
if ($this->_create_folder === true)
{
+ if (Filesystem::dir_check($this->_directory) === false)
+ {
+ return false;
+ }
+
mkdir($this->_directory);
$this->_create_folder = false;
}
+
+ if ($this->_remove_whitespace === true)
+ {
+ $this->_file['name'] = preg_replace('/\s+/u', '_', $this->_file['name']);
+ }
move_uploaded_file($this->_file['tmp_name'], $this->_directory . $this->_file['name']);
}
@@ -143,18 +150,14 @@ public function set_directory($directory, $create_dir = false)
{
$this->_create_folder = true;
}
- elseif (!is_writable($this->_directory))
- {
- Exceptions::exception("Directory '{$this->_directory}' is not writeable");
- }
- else
+ elseif (Filesystem::dir_check($this->_directory) === false)
{
- Exceptions::exception("Directory '{$this->_directory}' does not exist");
+ throw new Exceptions('Directory error during upload. See log for more details.');
}
}
// always upload in preconfigured directory
- $this->_directory = APP . 'assets/uploads/' . $directory . '/';
+ $this->_directory = Config::instance()->get('upload_directory') . $directory . '/';
return $this;
}
@@ -163,7 +166,7 @@ public function set_directory($directory, $create_dir = false)
* @param type $size
* @return File
*/
- public function max_size($size)
+ public function set_max_size($size)
{
$this->_max_size = $size;
@@ -215,7 +218,7 @@ public function validate()
{
return true;
}
- elseif (count($this->_errors) > 0)
+ elseif (count(static::$_errors) > 0)
{
// PHP error, $_FILES array not populated
return false;
@@ -224,7 +227,7 @@ public function validate()
$this->_validate_size();
$this->_validate_types();
- return count($this->_errors) > 0 ? false : true;
+ return count(static::$_errors) > 0 ? false : true;
}
protected function _pre_validate()
@@ -244,28 +247,28 @@ protected function _pre_validate()
case 0:
break;
case 1:
- $this->_errors[] = sprintf(I18n::instance()->line('invalid_filesize'), $this->_file['name']);
+ static::$_errors[] = sprintf(I18n::instance()->line('invalid_filesize'), $this->_file['name']);
break;
case 2:
- $this->_errors[] = $general_error;
+ static::$_errors[] = $general_error;
Log::write('error', "File '{$this->_file['name']}' exceeds the maximum size allowed by the submission form");
break;
case 3:
- $this->_errors[] = $general_error;
+ static::$_errors[] = $general_error;
Log::write('error', "File '{$this->_file['name']}' was only partially uploaded");
break;
case 4:
break;
case 6:
- $this->_errors[] = $general_error;
+ static::$_errors[] = $general_error;
Log::write('error', 'The temporary folder is missing');
break;
case 7:
- $this->_errors[] = $general_error;
+ static::$_errors[] = $general_error;
Log::write('error', "File '{$this->_file['name']}' could not be written to disk");
break;
case 8:
- $this->_errors[] = $general_error;
+ static::$_errors[] = $general_error;
Log::write('error', "File '{$this->_file['name']}' was stopped by extension");
break;
}
@@ -275,7 +278,7 @@ protected function _validate_size()
{
if (isset($this->_max_size) && $this->_file['size'] > $this->_max_size)
{
- $this->_errors[] = sprintf(I18n::instance()->line('invalid_filesize'), $this->_file['name']);
+ static::$_errors[] = sprintf(I18n::instance()->line('invalid_filesize'), $this->_file['name']);
}
}
@@ -292,16 +295,16 @@ protected function _validate_types()
if (!in_array($ext, $this->_types) || !in_array($mime_type, $this->_allowed_mimes))
{
- $this->_errors[] = sprintf(I18n::instance()->line('invalid_filetype'), $this->_file['name']);
+ static::$_errors[] = sprintf(I18n::instance()->line('invalid_filetype'), $this->_file['name']);
}
}
}
- public function get_errors()
+ public static function get_errors()
{
$errors = '<ul class="errors">';
- foreach ($this->_errors as $error)
+ foreach (static::$_errors as $error)
{
$errors .= '<li>' . $error . '</li>';
}
View
26 system/classes/Filesystem.php
@@ -9,12 +9,12 @@ class Filesystem {
* @param string $old Path to old file name.
* @param string $new Path to new file name.
* @return boolean True on successful rename, false otherwise.
- * @uses Filesystem::_file_check Writes into log file in case file is absent (or wrong path passed).
+ * @uses Filesystem::file_check Writes into log file in case file is absent (or wrong path passed).
* @static
*/
public static function rename_file($old, $new)
{
- if (static::_file_check($file) === false)
+ if (static::file_check($file) === false)
{
return false;
}
@@ -29,12 +29,12 @@ public static function rename_file($old, $new)
* @param string $old Path to old folder name.
* @param string $new Path to new folder name.
* @return boolean True on successful rename, false otherwise.
- * @uses Filesystem::_dir_check Writes into log file in case folder is absent (or wrong path passed).
+ * @uses Filesystem::dir_check Writes into log file in case folder is absent (or wrong path passed).
* @static
*/
public static function rename_folder($old, $new)
{
- if (static::_dir_check($old) === false)
+ if (static::dir_check($old) === false)
{
return false;
}
@@ -48,12 +48,12 @@ public static function rename_folder($old, $new)
* @access public
* @param string $file Path of file to be deleted.
* @return boolean True on successful delete, false otherwise.
- * @uses Filesystem::_file_check Writes into log file in case file is absent (or wrong path passed).
+ * @uses Filesystem::file_check Writes into log file in case file is absent (or wrong path passed).
* @static
*/
public static function delete_file($file)
{
- if (static::_file_check($file) === false)
+ if (static::file_check($file) === false)
{
return false;
}
@@ -67,12 +67,12 @@ public static function delete_file($file)
* @access public
* @param string $dir Path of folder to be deleted.
* @return boolean True on successful delete, false otherwise.
- * @uses Filesystem::_dir_check Writes into log file in case file is absent (or wrong path passed).
+ * @uses Filesystem::dir_check Writes into log file in case file is absent (or wrong path passed).
* @static
*/
public static function delete_folder($dir)
{
- if (static::_dir_check($dir) === false)
+ if (static::dir_check($dir) === false)
{
return false;
}
@@ -143,13 +143,13 @@ public static function move_folder($old_path, $new_path)
/**
* Checks if directory exists and is writeable.
*
- * @access private
+ * @access public
* @param string $dir Directory path to be checked.
* @return boolean True on successful check, false otherwise.
* @uses Log::write Writes in log file if directory is absent or not writeable.
* @static
*/
- private static function _dir_check($dir)
+ public static function dir_check($dir)
{
if (!is_dir($dir))
{
@@ -168,13 +168,13 @@ private static function _dir_check($dir)
/**
* Checks if file exists and is writeable.
*
- * @access private
- * @param string $dir Directory path to be checked.
+ * @access public
+ * @param string $file Directory path to be checked.
* @return boolean True on successful check, false otherwise.
* @uses Log::write Writes in log file if file is absent or not writeable.
* @static
*/
- private static function _file_check($file)
+ public static function file_check($file)
{
if (!file_exists($file))
{
View
14 system/classes/Image.php
@@ -4,6 +4,7 @@ class Image extends File {
protected $_max_width;
protected $_max_height;
+ protected $_thumbnail_dir = 'thumbnails/';
public static function instance($file = null)
{
@@ -41,6 +42,19 @@ public function set_max_height($height)
return $this;
}
+
+ public function create_thumbnail($thumb_dir = null)
+ {
+ if (!is_null($thumb_dir))
+ {
+ if (Filesystem::dir_check($this->_directory) === false)
+ {
+ throw new Exceptions('Directory error during upload. See log for more details.');
+ }
+
+ $this->_thumbnail_dir = $thumb_dir;
+ }
+ }
public function validate()
{
Please sign in to comment.
Something went wrong with that request. Please try again.