Fxi: Removed checkstyles errors

This commit is contained in:
Laurent Destailleur 2012-06-09 13:54:05 +02:00
parent fd0450457d
commit 85e7e5eddd

View File

@ -1,17 +1,17 @@
<?php <?php
/* Copyright (C) 2011-2012 Regis Houssin <regis@dolibarr.fr> /* Copyright (C) 2011-2012 Regis Houssin <regis@dolibarr.fr>
* Copyright (C) 2011 Laurent Destailleur <eldy@users.sourceforge.net> * Copyright (C) 2011-2012 Laurent Destailleur <eldy@users.sourceforge.net>
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or * the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
@ -26,8 +26,7 @@ require_once(DOL_DOCUMENT_ROOT."/core/lib/images.lib.php");
/** /**
* \file htdocs/core/class/fileupload.class.php * This class is used to manage file upload using ajax
* \brief This class is used to manage file upload using ajax
*/ */
class FileUpload class FileUpload
{ {
@ -125,9 +124,12 @@ class FileUpload
} }
/** /**
* Return full URL
* *
* @return string URL
*/ */
protected function getFullUrl() { protected function getFullUrl()
{
$https = !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off'; $https = !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off';
return return
($https ? 'https://' : 'http://'). ($https ? 'https://' : 'http://').
@ -141,9 +143,11 @@ class FileUpload
/** /**
* Set delete url * Set delete url
* *
* @param unknown_type $file * @param string $file Filename
* @return void
*/ */
protected function set_file_delete_url($file) { protected function setFileDeleteUrl($file)
{
$file->delete_url = $this->options['script_url'] $file->delete_url = $this->options['script_url']
.'?file='.rawurlencode($file->name).'&fk_element='.$this->fk_element.'&element='.$this->element; .'?file='.rawurlencode($file->name).'&fk_element='.$this->fk_element.'&element='.$this->element;
$file->delete_type = $this->options['delete_type']; $file->delete_type = $this->options['delete_type'];
@ -158,7 +162,7 @@ class FileUpload
* @param string $file_name Filename * @param string $file_name Filename
* @return stdClass|NULL * @return stdClass|NULL
*/ */
protected function get_file_object($file_name) protected function getFileObject($file_name)
{ {
$file_path = $this->options['upload_dir'].$file_name; $file_path = $this->options['upload_dir'].$file_name;
if (is_file($file_path) && $file_name[0] !== '.') if (is_file($file_path) && $file_name[0] !== '.')
@ -174,7 +178,7 @@ class FileUpload
$file->{$version.'_url'} = $options['upload_url'].rawurlencode($tmp[0].'_mini.'.$tmp[1]); $file->{$version.'_url'} = $options['upload_url'].rawurlencode($tmp[0].'_mini.'.$tmp[1]);
} }
} }
$this->set_file_delete_url($file); $this->setFileDeleteUrl($file);
return $file; return $file;
} }
return null; return null;
@ -185,9 +189,9 @@ class FileUpload
* *
* @return void * @return void
*/ */
protected function get_file_objects() protected function getFileObjects()
{ {
return array_values(array_filter(array_map(array($this, 'get_file_object'), scandir($this->options['upload_dir'])))); return array_values(array_filter(array_map(array($this, 'getFileObject'), scandir($this->options['upload_dir']))));
} }
/** /**
@ -197,7 +201,7 @@ class FileUpload
* @param string $options is array('max_width', 'max_height') * @param string $options is array('max_width', 'max_height')
* @return void * @return void
*/ */
protected function create_scaled_image($file_name, $options) protected function createScaledImage($file_name, $options)
{ {
global $maxwidthmini, $maxheightmini; global $maxwidthmini, $maxheightmini;
@ -264,7 +268,7 @@ class FileUpload
return false; return false;
} }
if (is_int($this->options['max_number_of_files']) && ( if (is_int($this->options['max_number_of_files']) && (
count($this->get_file_objects()) >= $this->options['max_number_of_files']) count($this->getFileObjects()) >= $this->options['max_number_of_files'])
) { ) {
$file->error = 'maxNumberOfFiles'; $file->error = 'maxNumberOfFiles';
return false; return false;
@ -288,9 +292,11 @@ class FileUpload
/** /**
* Enter description here ... * Enter description here ...
* *
* @param unknown_type $matches * @param int $matches ???
* @return string ???
*/ */
protected function upcount_name_callback($matches) { protected function upcountNameCallback($matches)
{
$index = isset($matches[1]) ? intval($matches[1]) + 1 : 1; $index = isset($matches[1]) ? intval($matches[1]) + 1 : 1;
$ext = isset($matches[2]) ? $matches[2] : ''; $ext = isset($matches[2]) ? $matches[2] : '';
return ' ('.$index.')'.$ext; return ' ('.$index.')'.$ext;
@ -299,25 +305,24 @@ class FileUpload
/** /**
* Enter description here ... * Enter description here ...
* *
* @param unknown_type $name * @param string $name ???
* @return string ???
*/ */
protected function upcount_name($name) { protected function upcountName($name)
return preg_replace_callback( {
'/(?:(?: \(([\d]+)\))?(\.[^.]+))?$/', return preg_replace_callback('/(?:(?: \(([\d]+)\))?(\.[^.]+))?$/', array($this, 'upcountNameCallback'), $name, 1);
array($this, 'upcount_name_callback'),
$name,
1
);
} }
/** /**
* Enter description here ... * Enter description here ...
* *
* @param unknown_type $name * @param unknown_type $name Filename
* @param unknown_type $type * @param unknown_type $type ???
* @param unknown_type $index * @param unknown_type $index ???
* @return void
*/ */
protected function trim_file_name($name, $type, $index) { protected function trimFileName($name, $type, $index)
{
// Remove path information and dots around the filename, to prevent uploading // Remove path information and dots around the filename, to prevent uploading
// into different directories or replacing hidden system files. // into different directories or replacing hidden system files.
// Also remove control characters and spaces (\x00..\x20) around the filename: // Also remove control characters and spaces (\x00..\x20) around the filename:
@ -327,9 +332,11 @@ class FileUpload
preg_match('/^image\/(gif|jpe?g|png)/', $type, $matches)) { preg_match('/^image\/(gif|jpe?g|png)/', $type, $matches)) {
$file_name .= '.'.$matches[1]; $file_name .= '.'.$matches[1];
} }
if ($this->options['discard_aborted_uploads']) { if ($this->options['discard_aborted_uploads'])
while(is_file($this->options['upload_dir'].$file_name)) { {
$file_name = $this->upcount_name($file_name); while(is_file($this->options['upload_dir'].$file_name))
{
$file_name = $this->upcountName($file_name);
} }
} }
return $file_name; return $file_name;
@ -338,19 +345,11 @@ class FileUpload
/** /**
* Enter description here ... * Enter description here ...
* *
* @param unknown_type $file * @param unknown_type $file_path ???
* @param unknown_type $index * @return int Success or not
*/ */
protected function handle_form_data($file, $index) { protected function orientImage($file_path)
// Handle form data, e.g. $_REQUEST['description'][$index] {
}
/**
* Enter description here ...
*
* @param unknown_type $file_path
*/
protected function orient_image($file_path) {
$exif = @exif_read_data($file_path); $exif = @exif_read_data($file_path);
if ($exif === false) { if ($exif === false) {
return false; return false;
@ -390,54 +389,52 @@ class FileUpload
* @param string $index Index * @param string $index Index
* @return stdClass * @return stdClass
*/ */
protected function handle_file_upload($uploaded_file, $name, $size, $type, $error, $index) protected function handleFileUpload($uploaded_file, $name, $size, $type, $error, $index)
{ {
$file = new stdClass(); $file = new stdClass();
$file->name = $this->trim_file_name($name, $type, $index); $file->name = $this->trimFileName($name, $type, $index);
$file->mime = dol_mimetype($file->name,'',2); $file->mime = dol_mimetype($file->name,'',2);
$file->size = intval($size); $file->size = intval($size);
$file->type = $type; $file->type = $type;
if ($this->validate($uploaded_file, $file, $error, $index) && dol_mkdir($this->options['upload_dir']) >= 0) { if ($this->validate($uploaded_file, $file, $error, $index) && dol_mkdir($this->options['upload_dir']) >= 0)
$this->handle_form_data($file, $index); {
$file_path = $this->options['upload_dir'].$file->name; $file_path = $this->options['upload_dir'].$file->name;
$append_file = !$this->options['discard_aborted_uploads'] && is_file($file_path) && $file->size > filesize($file_path); $append_file = !$this->options['discard_aborted_uploads'] && is_file($file_path) && $file->size > filesize($file_path);
clearstatcache(); clearstatcache();
if ($uploaded_file && is_uploaded_file($uploaded_file)) { if ($uploaded_file && is_uploaded_file($uploaded_file)) {
// multipart/formdata uploads (POST method uploads) // multipart/formdata uploads (POST method uploads)
if ($append_file) { if ($append_file)
file_put_contents( {
$file_path, file_put_contents($file_path, fopen($uploaded_file, 'r'), FILE_APPEND);
fopen($uploaded_file, 'r'),
FILE_APPEND
);
} else { } else {
dol_move_uploaded_file($uploaded_file, $file_path, 1); dol_move_uploaded_file($uploaded_file, $file_path, 1);
} }
} else { }
else
{
// Non-multipart uploads (PUT method support) // Non-multipart uploads (PUT method support)
file_put_contents( file_put_contents($file_path, fopen('php://input', 'r'), $append_file ? FILE_APPEND : 0);
$file_path,
fopen('php://input', 'r'),
$append_file ? FILE_APPEND : 0
);
} }
$file_size = filesize($file_path); $file_size = filesize($file_path);
if ($file_size === $file->size) { if ($file_size === $file->size)
{
$file->url = $this->options['upload_url'].rawurlencode($file->name); $file->url = $this->options['upload_url'].rawurlencode($file->name);
foreach($this->options['image_versions'] as $version => $options) foreach($this->options['image_versions'] as $version => $options)
{ {
if ($this->create_scaled_image($file->name, $options)) if ($this->createScaledImage($file->name, $options))
{ {
$tmp=explode('.',$file->name); $tmp=explode('.',$file->name);
$file->{$version.'_url'} = $options['upload_url'].rawurlencode($tmp[0].'_mini.'.$tmp[1]); $file->{$version.'_url'} = $options['upload_url'].rawurlencode($tmp[0].'_mini.'.$tmp[1]);
} }
} }
} else if ($this->options['discard_aborted_uploads']) { }
else if ($this->options['discard_aborted_uploads'])
{
unlink($file_path); unlink($file_path);
$file->error = 'abort'; $file->error = 'abort';
} }
$file->size = $file_size; $file->size = $file_size;
$this->set_file_delete_url($file); $this->setFileDeleteUrl($file);
} }
return $file; return $file;
} }
@ -451,10 +448,13 @@ class FileUpload
{ {
$file_name = isset($_REQUEST['file']) ? $file_name = isset($_REQUEST['file']) ?
basename(stripslashes($_REQUEST['file'])) : null; basename(stripslashes($_REQUEST['file'])) : null;
if ($file_name) { if ($file_name)
$info = $this->get_file_object($file_name); {
} else { $info = $this->getFileObject($file_name);
$info = $this->get_file_objects(); }
else
{
$info = $this->getFileObjects();
} }
header('Content-type: application/json'); header('Content-type: application/json');
echo json_encode($info); echo json_encode($info);
@ -467,40 +467,36 @@ class FileUpload
*/ */
public function post() public function post()
{ {
if (isset($_REQUEST['_method']) && $_REQUEST['_method'] === 'DELETE') { if (isset($_REQUEST['_method']) && $_REQUEST['_method'] === 'DELETE')
{
return $this->delete(); return $this->delete();
} }
$upload = isset($_FILES[$this->options['param_name']]) ? $upload = isset($_FILES[$this->options['param_name']]) ?
$_FILES[$this->options['param_name']] : null; $_FILES[$this->options['param_name']] : null;
$info = array(); $info = array();
if ($upload && is_array($upload['tmp_name'])) { if ($upload && is_array($upload['tmp_name']))
{
// param_name is an array identifier like "files[]", // param_name is an array identifier like "files[]",
// $_FILES is a multi-dimensional array: // $_FILES is a multi-dimensional array:
foreach ($upload['tmp_name'] as $index => $value) { foreach ($upload['tmp_name'] as $index => $value) {
$info[] = $this->handle_file_upload( $info[] = $this->handleFileUpload(
$upload['tmp_name'][$index], $upload['tmp_name'][$index],
isset($_SERVER['HTTP_X_FILE_NAME']) ? $_SERVER['HTTP_X_FILE_NAME'] : $upload['name'][$index], isset($_SERVER['HTTP_X_FILE_NAME']) ? $_SERVER['HTTP_X_FILE_NAME'] : $upload['name'][$index],
isset($_SERVER['HTTP_X_FILE_SIZE']) ? $_SERVER['HTTP_X_FILE_SIZE'] : $upload['size'][$index], isset($_SERVER['HTTP_X_FILE_SIZE']) ? $_SERVER['HTTP_X_FILE_SIZE'] : $upload['size'][$index],
isset($_SERVER['HTTP_X_FILE_TYPE']) ? $_SERVER['HTTP_X_FILE_TYPE'] : $upload['type'][$index], isset($_SERVER['HTTP_X_FILE_TYPE']) ? $_SERVER['HTTP_X_FILE_TYPE'] : $upload['type'][$index],
$upload['error'][$index], $upload['error'][$index],
$index $index
); );
} }
} elseif ($upload || isset($_SERVER['HTTP_X_FILE_NAME'])) { } elseif ($upload || isset($_SERVER['HTTP_X_FILE_NAME'])) {
// param_name is a single object identifier like "file", // param_name is a single object identifier like "file",
// $_FILES is a one-dimensional array: // $_FILES is a one-dimensional array:
$info[] = $this->handle_file_upload( $info[] = $this->handleFileUpload(
isset($upload['tmp_name']) ? $upload['tmp_name'] : null, isset($upload['tmp_name']) ? $upload['tmp_name'] : null,
isset($_SERVER['HTTP_X_FILE_NAME']) ? isset($_SERVER['HTTP_X_FILE_NAME']) ? $_SERVER['HTTP_X_FILE_NAME'] : (isset($upload['name']) ? $upload['name'] : null),
$_SERVER['HTTP_X_FILE_NAME'] : (isset($upload['name']) ? isset($_SERVER['HTTP_X_FILE_SIZE']) ? $_SERVER['HTTP_X_FILE_SIZE'] : (isset($upload['size']) ? $upload['size'] : null),
$upload['name'] : null), isset($_SERVER['HTTP_X_FILE_TYPE']) ? $_SERVER['HTTP_X_FILE_TYPE'] : (isset($upload['type']) ? $upload['type'] : null),
isset($_SERVER['HTTP_X_FILE_SIZE']) ? isset($upload['error']) ? $upload['error'] : null
$_SERVER['HTTP_X_FILE_SIZE'] : (isset($upload['size']) ?
$upload['size'] : null),
isset($_SERVER['HTTP_X_FILE_TYPE']) ?
$_SERVER['HTTP_X_FILE_TYPE'] : (isset($upload['type']) ?
$upload['type'] : null),
isset($upload['error']) ? $upload['error'] : null
); );
} }
header('Vary: Accept'); header('Vary: Accept');
@ -531,10 +527,13 @@ class FileUpload
basename(stripslashes($_REQUEST['file'])) : null; basename(stripslashes($_REQUEST['file'])) : null;
$file_path = $this->options['upload_dir'].$file_name; $file_path = $this->options['upload_dir'].$file_name;
$success = is_file($file_path) && $file_name[0] !== '.' && unlink($file_path); $success = is_file($file_path) && $file_name[0] !== '.' && unlink($file_path);
if ($success) { if ($success)
foreach($this->options['image_versions'] as $version => $options) { {
foreach($this->options['image_versions'] as $version => $options)
{
$file = $options['upload_dir'].$file_name; $file = $options['upload_dir'].$file_name;
if (is_file($file)) { if (is_file($file))
{
unlink($file); unlink($file);
} }
} }