New upstream version 0.6.27
This commit is contained in:
@@ -1,4 +1,12 @@
|
||||
<?php defined('SYSPATH') OR die('No direct access allowed.');
|
||||
<?php
|
||||
|
||||
// phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
defined('SYSPATH') or die('No direct access allowed.');
|
||||
// phpcs:enable PSR1.Files.SideEffects
|
||||
// phpcs:disable Squiz.Classes.ValidClassName.NotCamelCaps
|
||||
|
||||
|
||||
/**
|
||||
* Upload helper class for working with the global $_FILES
|
||||
* array and Validation library.
|
||||
@@ -10,153 +18,158 @@
|
||||
* @copyright (c) 2007-2008 Kohana Team
|
||||
* @license http://kohanaphp.com/license.html
|
||||
*/
|
||||
class upload_Core {
|
||||
class upload_Core
|
||||
{
|
||||
/**
|
||||
* Save an uploaded file to a new location.
|
||||
*
|
||||
* @param mixed name of $_FILE input or array of upload data
|
||||
* @param string new filename
|
||||
* @param string new directory
|
||||
* @param integer chmod mask
|
||||
* @return string full path to new file
|
||||
*/
|
||||
public static function save($file, $filename = null, $directory = null, $chmod = 0644)
|
||||
{
|
||||
// Load file data from FILES if not passed as array
|
||||
$file = is_array($file) ? $file : $_FILES[$file];
|
||||
|
||||
/**
|
||||
* Save an uploaded file to a new location.
|
||||
*
|
||||
* @param mixed name of $_FILE input or array of upload data
|
||||
* @param string new filename
|
||||
* @param string new directory
|
||||
* @param integer chmod mask
|
||||
* @return string full path to new file
|
||||
*/
|
||||
public static function save($file, $filename = NULL, $directory = NULL, $chmod = 0644)
|
||||
{
|
||||
// Load file data from FILES if not passed as array
|
||||
$file = is_array($file) ? $file : $_FILES[$file];
|
||||
if ($filename === null) {
|
||||
// Use the default filename, with a timestamp pre-pended
|
||||
$filename = time() . $file['name'];
|
||||
}
|
||||
|
||||
if ($filename === NULL)
|
||||
{
|
||||
// Use the default filename, with a timestamp pre-pended
|
||||
$filename = time().$file['name'];
|
||||
}
|
||||
if (Kohana::config('upload.remove_spaces') === true) {
|
||||
// Remove spaces from the filename
|
||||
$filename = preg_replace('/\s+/', '_', $filename);
|
||||
}
|
||||
|
||||
if (Kohana::config('upload.remove_spaces') === TRUE)
|
||||
{
|
||||
// Remove spaces from the filename
|
||||
$filename = preg_replace('/\s+/', '_', $filename);
|
||||
}
|
||||
if ($directory === null) {
|
||||
// Use the pre-configured upload directory
|
||||
$directory = Kohana::config('upload.directory', true);
|
||||
}
|
||||
|
||||
if ($directory === NULL)
|
||||
{
|
||||
// Use the pre-configured upload directory
|
||||
$directory = Kohana::config('upload.directory', TRUE);
|
||||
}
|
||||
// Make sure the directory ends with a slash
|
||||
$directory = rtrim($directory, '/') . '/';
|
||||
|
||||
// Make sure the directory ends with a slash
|
||||
$directory = rtrim($directory, '/').'/';
|
||||
if (! is_dir($directory) and Kohana::config('upload.create_directories') === true) {
|
||||
// Create the upload directory
|
||||
mkdir($directory, 0777, true);
|
||||
}
|
||||
|
||||
if ( ! is_dir($directory) AND Kohana::config('upload.create_directories') === TRUE)
|
||||
{
|
||||
// Create the upload directory
|
||||
mkdir($directory, 0777, TRUE);
|
||||
}
|
||||
if (! is_writable($directory)) {
|
||||
throw new Kohana_Exception('upload.not_writable', $directory);
|
||||
}
|
||||
|
||||
if ( ! is_writable($directory))
|
||||
throw new Kohana_Exception('upload.not_writable', $directory);
|
||||
if (is_uploaded_file($file['tmp_name']) and move_uploaded_file($file['tmp_name'], $filename = $directory . $filename)) {
|
||||
if ($chmod !== false) {
|
||||
// Set permissions on filename
|
||||
chmod($filename, $chmod);
|
||||
}
|
||||
|
||||
if (is_uploaded_file($file['tmp_name']) AND move_uploaded_file($file['tmp_name'], $filename = $directory.$filename))
|
||||
{
|
||||
if ($chmod !== FALSE)
|
||||
{
|
||||
// Set permissions on filename
|
||||
chmod($filename, $chmod);
|
||||
}
|
||||
// Return new file path
|
||||
return $filename;
|
||||
}
|
||||
|
||||
// Return new file path
|
||||
return $filename;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
/* Validation Rules */
|
||||
|
||||
/* Validation Rules */
|
||||
/**
|
||||
* Tests if input data is valid file type, even if no upload is present.
|
||||
*
|
||||
* @param array $_FILES item
|
||||
* @return bool
|
||||
*/
|
||||
public static function valid($file)
|
||||
{
|
||||
return (is_array($file)
|
||||
and isset($file['error'])
|
||||
and isset($file['name'])
|
||||
and isset($file['type'])
|
||||
and isset($file['tmp_name'])
|
||||
and isset($file['size']));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if input data is valid file type, even if no upload is present.
|
||||
*
|
||||
* @param array $_FILES item
|
||||
* @return bool
|
||||
*/
|
||||
public static function valid($file)
|
||||
{
|
||||
return (is_array($file)
|
||||
AND isset($file['error'])
|
||||
AND isset($file['name'])
|
||||
AND isset($file['type'])
|
||||
AND isset($file['tmp_name'])
|
||||
AND isset($file['size']));
|
||||
}
|
||||
/**
|
||||
* Tests if input data has valid upload data.
|
||||
*
|
||||
* @param array $_FILES item
|
||||
* @return bool
|
||||
*/
|
||||
public static function required(array $file)
|
||||
{
|
||||
return (isset($file['tmp_name'])
|
||||
and isset($file['error'])
|
||||
and is_uploaded_file($file['tmp_name'])
|
||||
and (int) $file['error'] === UPLOAD_ERR_OK);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if input data has valid upload data.
|
||||
*
|
||||
* @param array $_FILES item
|
||||
* @return bool
|
||||
*/
|
||||
public static function required(array $file)
|
||||
{
|
||||
return (isset($file['tmp_name'])
|
||||
AND isset($file['error'])
|
||||
AND is_uploaded_file($file['tmp_name'])
|
||||
AND (int) $file['error'] === UPLOAD_ERR_OK);
|
||||
}
|
||||
/**
|
||||
* Validation rule to test if an uploaded file is allowed by extension.
|
||||
*
|
||||
* @param array $_FILES item
|
||||
* @param array allowed file extensions
|
||||
* @return bool
|
||||
*/
|
||||
public static function type(array $file, array $allowed_types)
|
||||
{
|
||||
if ((int) $file['error'] !== UPLOAD_ERR_OK) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validation rule to test if an uploaded file is allowed by extension.
|
||||
*
|
||||
* @param array $_FILES item
|
||||
* @param array allowed file extensions
|
||||
* @return bool
|
||||
*/
|
||||
public static function type(array $file, array $allowed_types)
|
||||
{
|
||||
if ((int) $file['error'] !== UPLOAD_ERR_OK)
|
||||
return TRUE;
|
||||
// Get the default extension of the file
|
||||
$extension = strtolower(substr(strrchr($file['name'], '.'), 1));
|
||||
|
||||
// Get the default extension of the file
|
||||
$extension = strtolower(substr(strrchr($file['name'], '.'), 1));
|
||||
// Get the mime types for the extension
|
||||
$mime_types = Kohana::config('mimes.' . $extension);
|
||||
|
||||
// Get the mime types for the extension
|
||||
$mime_types = Kohana::config('mimes.'.$extension);
|
||||
// Make sure there is an extension, that the extension is allowed, and that mime types exist
|
||||
return ( ! empty($extension) and in_array($extension, $allowed_types) and is_array($mime_types));
|
||||
}
|
||||
|
||||
// Make sure there is an extension, that the extension is allowed, and that mime types exist
|
||||
return ( ! empty($extension) AND in_array($extension, $allowed_types) AND is_array($mime_types));
|
||||
}
|
||||
/**
|
||||
* Validation rule to test if an uploaded file is allowed by file size.
|
||||
* File sizes are defined as: SB, where S is the size (1, 15, 300, etc) and
|
||||
* B is the byte modifier: (B)ytes, (K)ilobytes, (M)egabytes, (G)igabytes.
|
||||
* Eg: to limit the size to 1MB or less, you would use "1M".
|
||||
*
|
||||
* @param array $_FILES item
|
||||
* @param array maximum file size
|
||||
* @return bool
|
||||
*/
|
||||
public static function size(array $file, array $size)
|
||||
{
|
||||
if ((int) $file['error'] !== UPLOAD_ERR_OK) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validation rule to test if an uploaded file is allowed by file size.
|
||||
* File sizes are defined as: SB, where S is the size (1, 15, 300, etc) and
|
||||
* B is the byte modifier: (B)ytes, (K)ilobytes, (M)egabytes, (G)igabytes.
|
||||
* Eg: to limit the size to 1MB or less, you would use "1M".
|
||||
*
|
||||
* @param array $_FILES item
|
||||
* @param array maximum file size
|
||||
* @return bool
|
||||
*/
|
||||
public static function size(array $file, array $size)
|
||||
{
|
||||
if ((int) $file['error'] !== UPLOAD_ERR_OK)
|
||||
return TRUE;
|
||||
// Only one size is allowed
|
||||
$size = strtoupper($size[0]);
|
||||
|
||||
// Only one size is allowed
|
||||
$size = strtoupper($size[0]);
|
||||
if (! preg_match('/[0-9]++[BKMG]/', $size)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( ! preg_match('/[0-9]++[BKMG]/', $size))
|
||||
return FALSE;
|
||||
// Make the size into a power of 1024
|
||||
switch (substr($size, -1)) {
|
||||
case 'G':
|
||||
$size = intval($size) * pow(1024, 3);
|
||||
break;
|
||||
case 'M':
|
||||
$size = intval($size) * pow(1024, 2);
|
||||
break;
|
||||
case 'K':
|
||||
$size = intval($size) * pow(1024, 1);
|
||||
break;
|
||||
default:
|
||||
$size = intval($size);
|
||||
break;
|
||||
}
|
||||
|
||||
// Make the size into a power of 1024
|
||||
switch (substr($size, -1))
|
||||
{
|
||||
case 'G': $size = intval($size) * pow(1024, 3); break;
|
||||
case 'M': $size = intval($size) * pow(1024, 2); break;
|
||||
case 'K': $size = intval($size) * pow(1024, 1); break;
|
||||
default: $size = intval($size); break;
|
||||
}
|
||||
|
||||
// Test that the file is under or equal to the max size
|
||||
return ($file['size'] <= $size);
|
||||
}
|
||||
|
||||
} // End upload
|
||||
// Test that the file is under or equal to the max size
|
||||
return ($file['size'] <= $size);
|
||||
}
|
||||
}
|
||||
// End upload
|
||||
|
||||
Reference in New Issue
Block a user