//Starting Uploader helper

This commit is contained in:
Jerome Nadaud
2013-10-25 14:19:51 +02:00
parent 139ee8f62d
commit 52795495bd
15 changed files with 3521 additions and 58 deletions
+239
View File
@@ -0,0 +1,239 @@
<?php
/*
* 2007-2013 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2013 PrestaShop SA
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
class UploaderCore
{
const DEFAULT_MAX_SIZE = 10485760;
private $_accept_types;
private $_files;
private $_max_size;
private $_name;
private $_save_path;
public function __construct($name = null)
{
$this->setName($name);
}
public function setAcceptTypes($value)
{
$this->_accept_types = $value;
return $this;
}
public function getAcceptTypes()
{
if (!isset($this->_accept_types))
$this->setAcceptTypes('/.+$/i');
return $this->_accept_types;
}
public function getFiles()
{
if (!isset($this->_files))
$this->_files = array();
return $this->_files;
}
public function setMaxSize($value)
{
$this->_max_size = intval($value);
return $this;
}
public function getMaxSize()
{
if (!isset($this->_max_size))
$this->setMaxSize(self::DEFAULT_MAX_SIZE);
return $this->_max_size;
}
public function setName($value)
{
$this->_name = $value;
return $this;
}
public function getName()
{
return $this->_name;
}
public function setSavePath($value)
{
$this->_save_path = $value;
return $this;
}
public function getSavePath()
{
if (!isset($this->_save_path))
$this->setSavePath(_PS_UPLOAD_DIR_);
return $this->_normalizeDirectory($this->_save_path);
}
public function getUniqueFileName()
{
return uniqid('', true);
}
public function process()
{
$this->files = array();
$upload = isset($_FILES[$this->getName()]) ? $_FILES[$this->getName()] : null;
if ($upload && is_array($upload['tmp_name']))
foreach ($upload['tmp_name'] as $index => $value)
$this->files[] = $this->upload(
$upload['tmp_name'][$index],
$upload['name'][$index],
$upload['size'][$index],
$upload['type'][$index],
$upload['error'][$index]
);
else
$this->files[] = $this->upload(
$upload['tmp_name'],
$upload['name'],
isset($upload['size']) ? $upload['size'] : $this->_getServerVars('CONTENT_LENGTH'),
isset($upload['type']) ? $upload['type'] : $this->_getServerVars('CONTENT_TYPE'),
isset($upload['error']) ? $upload['error'] : null
);
return $this->files;
}
public function upload($tmp_name, $name, $size, $type, $error)
{
$file = new stdClass();
$file->name = $name; //TODO: add unique file name if name is null
$file->size = intval($size);
$file->type = $type;
if ($this->validate($tmp_name, $file, $error))
{
$file_path = $this->getSavePath().$file->name;
if ($tmp_name && is_uploaded_file($tmp_name)) {
move_uploaded_file($tmp_name, $file_path);
} else {
// Non-multipart uploads (PUT method support)
file_put_contents($file_path, fopen('php://input', 'r'));
}
$file_size = $this->_getFileSize($file_path);
if ($file_size === $file->size)
{
//TODO do image processing
}
else
{
$file->size = $file_size;
unlink($file_path);
$file->error = 'abort';
}
}
return $file;
}
protected function validate($tmp_name, $file, $error)
{
if ($error)
{
$file->error = Tools::displayError($error);
return false;
}
$post_max_size = $this->_getPostMaxSizeBytes();
if ($post_max_size && ($this->_getServerVars('CONTENT_LENGTH') > $post_max_size))
{
$file->error = Tools::displayError('The uploaded file exceeds the post_max_size directive in php.ini');
return false;
}
if (!preg_match($this->getAcceptTypes(), $file->name))
{
$file->error = Tools::displayError('Filetype not allowed');
return false;
}
if ($file->size > $this->getMaxSize())
{
$file->error = Tools::displayError('File is too big');
return false;
}
return true;
}
private function _getFileSize($file_path, $clear_stat_cache = false) {
if ($clear_stat_cache)
clearstatcache(true, $file_path);
return filesize($file_path);
}
private function _getPostMaxSizeBytes() {
$post_max_size = ini_get('post_max_size');
$bytes = trim($post_max_size);
$last = strtolower($post_max_size[strlen($post_max_size) - 1]);
switch ($last)
{
case 'g': $bytes *= 1024;
case 'm': $bytes *= 1024;
case 'k': $bytes *= 1024;
}
return $bytes;
}
private function _getServerVars($var)
{
return (isset($_SERVER[$var]) ? $_SERVER[$var] : '');
}
protected function _normalizeDirectory($directory)
{
$last = $directory[strlen($directory) - 1];
if (in_array($last, array('/', '\\'))) {
$directory[strlen($directory) - 1] = DIRECTORY_SEPARATOR;
return $directory;
}
$directory .= DIRECTORY_SEPARATOR;
return $directory;
}
}
+18
View File
@@ -76,6 +76,7 @@ class HelperFormCore extends Helper
$date = true;
$tinymce = true;
$textarea_autosize = true;
$file = true;
foreach ($this->fields_form as $fieldset_key => &$fieldset)
if (isset($fieldset['form']['input']))
foreach ($fieldset['form']['input'] as $key => &$params)
@@ -110,6 +111,23 @@ class HelperFormCore extends Helper
}
break;
case 'file':
$uploader = new HelperUploader();
$uploader->setId(isset($params['id'])?$params['id']:null);
$uploader->setName($params['name']);
$uploader->setUrl(isset($params['url'])?$params['url']:null);
$uploader->setMultiple(isset($params['multiple'])?$params['multiple']:false);
$uploader->setUseAjax(isset($params['ajax'])?$params['ajax']:false);
$uploader->setDisplayImage(isset($params['display_image'])?$params['display_image']:false);
$uploader->setImage(isset($params['image'])?$params['image']:null);
$uploader->setThumb(isset($params['thumb'])?$params['thumb']:null);
$uploader->setSize(isset($params['size'])?$params['size']:null);
$uploader->setTitle(isset($params['title'])?$params['title']:null);
$uploader->setDeleteUrl(isset($params['delete_url'])?$params['delete_url']:null);
$params['file'] = $uploader->render();
break;
case 'color':
if ($color)
{
+18
View File
@@ -95,6 +95,24 @@ class HelperOptionsCore extends Helper
if ($field['type'] == 'texarea' || $field['type'] == 'textareaLang')
$this->context->controller->addJS(_PS_JS_DIR_.'jquery/plugins/jquery.autosize.min.js');
if ($field['type'] == 'file')
{
$uploader = new HelperUploader();
$uploader->setId(isset($field['id'])?$field['id']:null);
$uploader->setName($field['name']);
$uploader->setUrl(isset($field['url'])?$field['url']:null);
$uploader->setMultiple(isset($field['multiple'])?$field['multiple']:false);
$uploader->setUseAjax(isset($field['ajax'])?$field['ajax']:false);
$uploader->setDisplayImage(isset($field['display_image'])?$field['display_image']:false);
$uploader->setImage(isset($field['image'])?$field['image']:null);
$uploader->setThumb(isset($field['thumb'])?$field['thumb']:null);
$uploader->setSize(isset($field['size'])?$field['size']:null);
$uploader->setTitle(isset($field['title'])?$field['title']:null);
$uploader->setDeleteUrl(isset($field['delete_url'])?$field['delete_url']:null);
$field['file'] = $uploader->render();
}
// Cast options values if specified
if ($field['type'] == 'select' && isset($field['cast']))
+319
View File
@@ -0,0 +1,319 @@
<?php
/*
* 2007-2013 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2013 PrestaShop SA
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
class HelperUploaderCore extends Uploader
{
const DEFAULT_TEMPLATE_DIRECTORY = 'helpers/uploader';
const DEFAULT_TEMPLATE = 'simple.tpl';
const DEFAULT_AJAX_TEMPLATE = 'ajax.tpl';
private $_context;
private $_delete_url;
private $_display_image;
private $_id;
private $_image;
private $_name;
private $_multiple;
private $_size;
protected $_template;
private $_template_directory;
private $_title;
private $_thumb;
private $_url;
private $_use_ajax;
public function setContext($value)
{
$this->_context = $value;
return $this;
}
public function getContext()
{
if (!isset($this->_context))
$this->_context = Context::getContext();
return $this->_context;
}
public function setDeleteUrl($value)
{
$this->_delete_url = $value;
return $this;
}
public function getDeleteUrl()
{
return $this->_delete_url;
}
public function setDisplayImage($value)
{
$this->_display_image = $value;
return $this;
}
public function setId($value)
{
$this->_id = (string)$value;
return $this;
}
public function getId()
{
if (!isset($this->_id) || trim($this->_id) === '')
$this->_id = $this->getName();
return $this->_id;
}
public function setImage($value)
{
$this->_image = $value;
return $this;
}
public function getImage()
{
return $this->_image;
}
public function setName($value)
{
$this->_name = (string)$value;
return $this;
}
public function getName()
{
return $this->_name;
}
public function setMultiple($value)
{
$this->_multiple = (bool)$value;
return $this;
}
public function setSize($value)
{
$this->_size = $value;
return $this;
}
public function getSize()
{
return $this->_size;
}
public function setTemplate($value)
{
$this->_template = $value;
return $this;
}
public function getTemplate()
{
if (!isset($this->_template))
$this->setTemplate(self::DEFAULT_TEMPLATE);
return $this->_template;
}
public function setTemplateDirectory($value)
{
$this->_template_directory = $value;
return $this;
}
public function getTemplateDirectory()
{
if (!isset($this->_template_directory))
$this->_template_directory = self::DEFAULT_TEMPLATE_DIRECTORY;
return $this->_normalizeDirectory($this->_template_directory);
}
public function getTemplateFile($template)
{
if (preg_match_all('/((?:^|[A-Z])[a-z]+)/', get_class($this->getContext()->controller), $matches) !== FALSE)
$controllerName = strtolower($matches[0][1]);
if ($this->getContext()->controller instanceof ModuleAdminController)
return $this->_normalizeDirectory(
$this->getContext()->controller->getTemplatePath())
.$this->getTemplateDirectory().$template;
else if ($this->getContext()->controller instanceof AdminController
&& isset($controllerName) && file_exists($this->_normalizeDirectory(
$this->getContext()->smarty->getTemplateDir(0)).'controllers'
.DIRECTORY_SEPARATOR
.$controllerName
.DIRECTORY_SEPARATOR
.$this->getTemplateDirectory().$template))
return $this->_normalizeDirectory(
$this->getContext()->smarty->getTemplateDir(0)).'controllers'
.DIRECTORY_SEPARATOR
.$controllerName
.DIRECTORY_SEPARATOR
.$this->getTemplateDirectory().$template;
else if (file_exists($this->_normalizeDirectory(
$this->getContext()->smarty->getTemplateDir(1))
.$this->getTemplateDirectory().$template))
return $this->_normalizeDirectory(
$this->getContext()->smarty->getTemplateDir(1))
.$this->getTemplateDirectory().$template;
else if (file_exists($this->_normalizeDirectory(
$this->getContext()->smarty->getTemplateDir(0))
.$this->getTemplateDirectory().$template))
return $this->_normalizeDirectory(
$this->getContext()->smarty->getTemplateDir(0))
.$this->getTemplateDirectory().$template;
else
return $this->getTemplateDirectory().$template;
}
public function setTitle($value)
{
$this->_title = $value;
return $this;
}
public function getTitle()
{
return $this->_title;
}
public function setThumb($value)
{
$this->_thumb = $value;
return $this;
}
public function getThumb()
{
return $this->_thumb;
}
public function setUrl($value)
{
$this->_url = (string)$value;
return $this;
}
public function getUrl()
{
return $this->_url;
}
public function setUseAjax($value)
{
$this->_use_ajax = (bool)$value;
return $this;
}
public function displayImage()
{
return (isset($this->_display_image) && $this->_display_image);
}
public function isMultiple()
{
return (isset($this->_multiple) && $this->_multiple);
}
public function process()
{
$files = parent::process();
die(Tools::jsonEncode(array($this->getName() => $files)));
}
public function render()
{
$admin_webpath = str_ireplace(_PS_ROOT_DIR_, '', _PS_ADMIN_DIR_);
$admin_webpath = preg_replace('/^'.preg_quote(DIRECTORY_SEPARATOR, '/').'/', '', $admin_webpath);
$bo_theme = ((Validate::isLoadedObject($this->getContext()->employee)
&& $this->getContext()->employee->bo_theme) ? $this->getContext()->employee->bo_theme : 'default');
if (!file_exists(_PS_BO_ALL_THEMES_DIR_.$bo_theme.DIRECTORY_SEPARATOR
.'template'))
$bo_theme = 'default';
if ($this->getContext()->controller->ajax)
{
$html = '<script type="text/javascript" src="'.__PS_BASE_URI__.$admin_webpath
.'/themes/'.$bo_theme.'/js/vendor/jquery.ui.widget.js"></script>';
$html .= '<script type="text/javascript" src="'.__PS_BASE_URI__.$admin_webpath
.'/themes/'.$bo_theme.'/js/jquery.iframe-transport.js"></script>';
$html .= '<script type="text/javascript" src="'.__PS_BASE_URI__.$admin_webpath
.'/themes/'.$bo_theme.'/js/jquery.fileupload.js"></script>';
}
else
{
$html = '';
$this->getContext()->controller->addJs(__PS_BASE_URI__.$admin_webpath
.'/themes/'.$bo_theme.'/js/vendor/jquery.ui.widget.js');
//$context->controller->addJs('http://blueimp.github.io/JavaScript-Load-Image/js/load-image.min.js');
$this->getContext()->controller->addJs(__PS_BASE_URI__.$admin_webpath
.'/themes/'.$bo_theme.'/js/jquery.iframe-transport.js');
$this->getContext()->controller->addJs(__PS_BASE_URI__.$admin_webpath
.'/themes/'.$bo_theme.'/js/jquery.fileupload.js');
/*$context->controller->addJs(__PS_BASE_URI__.$admin_webpath
.'/themes/'.$bo_theme.'/js/jquery.fileupload-image.js');*/
$this->getContext()->controller->addJs(__PS_BASE_URI__.'/js/vendor/spin.js');
$this->getContext()->controller->addJs(__PS_BASE_URI__.'/js/vendor/ladda.js');
}
if ($this->useAjax())
$this->setTemplate(self::DEFAULT_AJAX_TEMPLATE);
$template = $this->getContext()->smarty->createTemplate(
$this->getTemplateFile($this->getTemplate()), $this->getContext()->smarty
);
$template->assign(array(
'id' => $this->getId(),
'name' => $this->getName(),
'url' => $this->getUrl(),
'multiple' => $this->isMultiple(),
'display_image' => $this->displayImage(),
'image' => $this->getImage(),
'thumb' => $this->getThumb(),
'size' => $this->getSize(),
'delete_url' => $this->getDeleteUrl(),
'title' => $this->getTitle()
));
$html .= $template->fetch();
return $html;
}
public function useAjax()
{
return (isset($this->_use_ajax) && $this->_use_ajax);
}
}