26require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
27require_once DOL_DOCUMENT_ROOT.
'/core/lib/images.lib.php';
36 protected $fk_element;
47 public function __construct($options =
null, $fk_element =
null, $element =
null)
52 $hookmanager->initHooks(array(
'fileupload'));
57 $this->fk_element = $fk_element;
58 $this->element = $element;
60 $pathname = str_replace(
'/class',
'', $element_prop[
'classpath']);
66 if (empty($dir_output)) {
67 setEventMessage(
'The element '.$element.
' is not supported for uploading file. dir_output is unknown.',
'errors');
68 throw new Exception(
'The element '.$element.
' is not supported for uploading file. dir_output is unknown.');
72 if ($pathname !==
null && $filename !==
null) {
79 if ($element ==
'invoice_supplier') {
81 } elseif ($element ==
'project_task') {
82 $parentForeignKey =
'fk_project';
83 $parentClass =
'Project';
84 $parentElement =
'projet';
85 $parentObject =
'project';
88 $parent =
new $parentClass($db);
89 $parent->fetch(
$object->$parentForeignKey);
90 if (!empty($parent->socid)) {
91 $parent->fetch_thirdparty();
93 $object->$parentObject = clone $parent;
99 $this->options = array(
100 'script_url' => $_SERVER[
'PHP_SELF'],
101 'upload_dir' => $dir_output.
'/'.$object_ref.
'/',
102 'upload_url' => DOL_URL_ROOT.
'/document.php?modulepart='.$element.
'&attachment=1&file=/'.$object_ref.
'/',
103 'param_name' =>
'files',
106 'delete_type' =>
'DELETE',
109 'max_file_size' =>
null,
110 'min_file_size' => 1,
111 'accept_file_types' =>
'/.+$/i',
113 'max_number_of_files' =>
null,
116 'max_height' =>
null,
120 'discard_aborted_uploads' =>
true,
121 'image_versions' => array(
134 'thumbnail' => array(
135 'upload_dir' => $dir_output.
'/'.$object_ref.
'/thumbs/',
136 'upload_url' => DOL_URL_ROOT.
'/document.php?modulepart='.urlencode($element).
'&attachment=1&file='.urlencode(
'/'.$object_ref.
'/thumbs/'),
145 $hookmanager->executeHooks(
146 'overrideUploadOptions',
148 'options' => &$options,
149 'element' => $element
156 $this->options = array_replace_recursive($this->options, $options);
161 if ($pathname ===
null || $filename ===
null) {
162 if (!array_key_exists(
"upload_dir", $this->options)) {
163 setEventMessage(
'If $fk_element = null or $element = null you must specify upload_dir on $options',
'errors');
164 throw new Exception(
'If $fk_element = null or $element = null you must specify upload_dir on $options');
165 } elseif (!is_dir($this->options[
'upload_dir'])) {
166 setEventMessage(
'The directory '.$this->options[
'upload_dir'].
' doesn\'t exists',
'errors');
167 throw new Exception(
'The directory '.$this->options[
'upload_dir'].
' doesn\'t exists');
168 } elseif (!is_writable($this->options[
'upload_dir'])) {
169 setEventMessage(
'The directory '.$this->options[
'upload_dir'].
' is not writable',
'errors');
170 throw new Exception(
'The directory '.$this->options[
'upload_dir'].
' is not writable');
182 $https = !empty($_SERVER[
'HTTPS']) && $_SERVER[
'HTTPS'] !==
'off';
184 ($https ?
'https://' :
'http://').
185 (!empty($_SERVER[
'REMOTE_USER']) ? $_SERVER[
'REMOTE_USER'].
'@' :
'').
186 (isset($_SERVER[
'HTTP_HOST']) ? $_SERVER[
'HTTP_HOST'] : ($_SERVER[
'SERVER_NAME'].
187 ($https && $_SERVER[
'SERVER_PORT'] === 443 ||
188 $_SERVER[
'SERVER_PORT'] === 80 ?
'' :
':'.$_SERVER[
'SERVER_PORT']))).
189 substr($_SERVER[
'SCRIPT_NAME'], 0, strrpos($_SERVER[
'SCRIPT_NAME'],
'/'));
200 $file->delete_url = $this->options[
'script_url'].
'?file='.urlencode((
string) ($file->name)).
'&fk_element='.urlencode((
string) ($this->fk_element)).
'&element='.urlencode((
string) ($this->element));
201 $file->delete_type = $this->options[
'delete_type'];
202 if ($file->delete_type !==
'DELETE') {
203 $file->delete_url .=
'&_method=DELETE';
215 $file_path = $this->options[
'upload_dir'].dol_sanitizeFileName($file_name);
217 if (
dol_is_file($file_path) && $file_name[0] !==
'.') {
219 $file->name = $file_name;
221 $file->size = filesize($file_path);
222 $file->url = $this->options[
'upload_url'].urlencode($file->name);
224 foreach ($this->options[
'image_versions'] as $version => $options) {
225 if (
dol_is_file($options[
'upload_dir'].$file_name)) {
226 $tmp = explode(
'.', $file->name);
229 $keyforfile = $version.
'_url';
230 $file->$keyforfile = $options[
'upload_url'].urlencode($tmp[0].
'_mini.'.$tmp[1]);
246 return array_values(array_filter(array_map(array($this,
'getFileObject'), scandir($this->options[
'upload_dir']))));
258 global $maxwidthmini, $maxheightmini, $maxwidthsmall, $maxheightsmall;
260 $file_path = $this->options[
'upload_dir'].$file_name;
261 $new_file_path = $options[
'upload_dir'].$file_name;
263 if (
dol_mkdir($options[
'upload_dir']) >= 0) {
264 list($img_width, $img_height) = @getimagesize($file_path);
265 if (!$img_width || !$img_height) {
269 $res =
vignette($file_path, $maxwidthmini, $maxheightmini,
'_mini');
270 if (preg_match(
'/error/i', $res)) {
274 $res =
vignette($file_path, $maxwidthsmall, $maxheightsmall,
'_small');
275 if (preg_match(
'/error/i', $res)) {
294 protected function validate($uploaded_file, $file, $error, $index)
297 $file->error = $error;
301 $file->error =
'missingFileName';
304 if (!preg_match($this->options[
'accept_file_types'], $file->name)) {
305 $file->error =
'acceptFileTypes';
308 if ($uploaded_file && is_uploaded_file($uploaded_file)) {
311 $file_size = $_SERVER[
'CONTENT_LENGTH'];
313 if ($this->options[
'max_file_size'] && (
314 $file_size > $this->options[
'max_file_size'] ||
315 $file->size > $this->options[
'max_file_size']
318 $file->error =
'maxFileSize';
321 if ($this->options[
'min_file_size'] &&
322 $file_size < $this->options[
'min_file_size']) {
323 $file->error =
'minFileSize';
326 if (is_numeric($this->options[
'max_number_of_files']) && (
327 count($this->
getFileObjects()) >= $this->options[
'max_number_of_files']
330 $file->error =
'maxNumberOfFiles';
333 list($img_width, $img_height) = @getimagesize($uploaded_file);
334 if (is_numeric($img_width)) {
335 if ($this->options[
'max_width'] && $img_width > $this->options[
'max_width'] ||
336 $this->options[
'max_height'] && $img_height > $this->options[
'max_height']) {
337 $file->error =
'maxResolution';
340 if ($this->options[
'min_width'] && $img_width < $this->options[
'min_width'] ||
341 $this->options[
'min_height'] && $img_height < $this->options[
'min_height']) {
342 $file->error =
'minResolution';
357 $index = isset($matches[1]) ? intval($matches[1]) + 1 : 1;
358 $ext = isset($matches[2]) ? $matches[2] :
'';
359 return ' ('.$index.
')'.$ext;
370 return preg_replace_callback(
'/(?:(?: \(([\d]+)\))?(\.[^.]+))?$/', array($this,
'upcountNameCallback'), $name, 1);
388 if (strpos($file_name,
'.') ===
false && preg_match(
'/^image\/(gif|jpe?g|png)/', $type, $matches)) {
389 $file_name .=
'.'.$matches[1];
391 if ($this->options[
'discard_aborted_uploads']) {
392 while (
dol_is_file($this->options[
'upload_dir'].$file_name)) {
414 $file->name = $this->
trimFileName($name, $type, $index);
416 $file->size = intval($size);
421 $uploaded_file = preg_replace(
'/\s*(http|ftp)s?:/i',
'', $uploaded_file);
422 $uploaded_file = realpath($uploaded_file);
424 $validate = $this->
validate($uploaded_file, $file, $error, $index);
427 if (
dol_mkdir($this->options[
'upload_dir']) >= 0) {
428 $file_path =
dol_sanitizePathName($this->options[
'upload_dir']).dol_sanitizeFileName($file->name);
429 $append_file = !$this->options[
'discard_aborted_uploads'] &&
dol_is_file($file_path) && $file->size >
dol_filesize($file_path);
433 if ($uploaded_file && is_uploaded_file($uploaded_file)) {
436 file_put_contents($file_path, fopen($uploaded_file,
'r'), FILE_APPEND);
442 file_put_contents($file_path, fopen(
'php://input',
'r'), $append_file ? FILE_APPEND : 0);
445 if ($file_size === $file->size) {
446 $file->url = $this->options[
'upload_url'].urlencode($file->name);
447 foreach ($this->options[
'image_versions'] as $version => $options) {
449 $tmp = explode(
'.', $file->name);
452 $keyforfile = $version.
'_url';
453 $file->$keyforfile = $options[
'upload_url'].urlencode($tmp[0].
'_mini.'.$tmp[1]);
456 } elseif ($this->options[
'discard_aborted_uploads']) {
458 $file->error =
'abort';
460 $file->size = $file_size;
463 $file->error =
'failedtocreatedestdir';
500 $upload = isset($_FILES[$this->options[
'param_name']]) ? $_FILES[$this->options[
'param_name']] :
null;
503 if ($upload && is_array($upload[
'tmp_name'])) {
506 foreach ($upload[
'tmp_name'] as $index => $value) {
508 $upload[
'tmp_name'][$index],
509 isset($_SERVER[
'HTTP_X_FILE_NAME']) ? $_SERVER[
'HTTP_X_FILE_NAME'] : $upload[
'name'][$index],
510 isset($_SERVER[
'HTTP_X_FILE_SIZE']) ? $_SERVER[
'HTTP_X_FILE_SIZE'] : $upload[
'size'][$index],
511 isset($_SERVER[
'HTTP_X_FILE_TYPE']) ? $_SERVER[
'HTTP_X_FILE_TYPE'] : $upload[
'type'][$index],
512 $upload[
'error'][$index],
515 if (!empty($tmpres->error)) {
520 } elseif ($upload || isset($_SERVER[
'HTTP_X_FILE_NAME'])) {
524 isset($upload[
'tmp_name']) ? $upload[
'tmp_name'] :
null,
525 isset($_SERVER[
'HTTP_X_FILE_NAME']) ? $_SERVER[
'HTTP_X_FILE_NAME'] : (isset($upload[
'name']) ? $upload[
'name'] :
null),
526 isset($_SERVER[
'HTTP_X_FILE_SIZE']) ? $_SERVER[
'HTTP_X_FILE_SIZE'] : (isset($upload[
'size']) ? $upload[
'size'] :
null),
527 isset($_SERVER[
'HTTP_X_FILE_TYPE']) ? $_SERVER[
'HTTP_X_FILE_TYPE'] : (isset($upload[
'type']) ? $upload[
'type'] :
null),
528 isset($upload[
'error']) ? $upload[
'error'] :
null,
531 if (!empty($tmpres->error)) {
537 header(
'Vary: Accept');
538 $json = json_encode($info);
548 if (isset($_SERVER[
'HTTP_ACCEPT']) && (strpos($_SERVER[
'HTTP_ACCEPT'],
'application/json') !==
false)) {
549 header(
'Content-type: application/json');
551 header(
'Content-type: text/plain');
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
This class is used to manage file upload using ajax.
getFileObjects()
getFileObjects
setFileDeleteUrl($file)
Set delete url.
__construct($options=null, $fk_element=null, $element=null)
Constructor.
handleFileUpload($uploaded_file, $name, $size, $type, $error, $index)
handleFileUpload.
upcountName($name)
Enter description here ...
getFileObject($file_name)
getFileObject
upcountNameCallback($matches)
Enter description here ...
createScaledImage($file_name, $options)
Create thumbs of a file uploaded.
getFullUrl()
Return full URL.
trimFileName($name, $type, $index)
trimFileName
validate($uploaded_file, $file, $error, $index)
Make validation on an uploaded file.
dol_filesize($pathoffile)
Return size of a file.
dol_move_uploaded_file($src_file, $dest_file, $allowoverwrite, $disablevirusscan=0, $uploaderrorcode=0, $nohook=0, $varfiles='addedfile', $upload_dir='')
Check validity of a file upload from an GUI page, and move it to its final destination.
dol_is_file($pathoffile)
Return if path is a file.
dol_mimetype($file, $default='application/octet-stream', $mode=0)
Return MIME type of a file from its name with extension.
setEventMessage($mesgs, $style='mesgs', $noduplicate=0)
Set event message in dol_events session object.
getElementProperties($elementType)
Get an array with properties of an element.
if(!function_exists( 'dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
fetchObjectByElement($element_id, $element_type, $element_ref='', $useCache=0, $maxCacheByType=10)
Fetch an object from its id and element_type Inclusion of classes is automatic.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
get_exdir($num, $level, $alpha, $withoutslash, $object, $modulepart='')
Return a path to have a the directory according to object where files are stored.
dol_sanitizePathName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a path name.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
vignette($file, $maxWidth=160, $maxHeight=120, $extName='_small', $quality=50, $outdir='thumbs', $targetformat=0)
Create a thumbnail from an image file (Supported extensions are gif, jpg, png and bmp).