27require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
28require_once DOL_DOCUMENT_ROOT.
'/core/lib/images.lib.php';
37 protected $fk_element;
52 public function __construct($options =
null, $fk_element =
null, $element =
null)
57 $hookmanager->initHooks(array(
'fileupload'));
62 $this->fk_element = $fk_element;
63 $this->element = $element;
65 $pathname = str_replace(
'/class',
'', $element_prop[
'classpath']);
71 if (empty($dir_output)) {
72 setEventMessage(
'The element '.$element.
' is not supported for uploading file. dir_output is unknown.',
'errors');
73 throw new Exception(
'The element '.$element.
' is not supported for uploading file. dir_output is unknown.');
77 if ($pathname !==
null && $filename !==
null) {
84 if ($element ==
'invoice_supplier') {
86 } elseif ($element ==
'project_task') {
87 $parentForeignKey =
'fk_project';
88 $parentClass =
'Project';
89 $parentElement =
'projet';
90 $parentObject =
'project';
93 $parent =
new $parentClass($db);
94 $parent->fetch(
$object->$parentForeignKey);
95 if (!empty($parent->socid)) {
96 $parent->fetch_thirdparty();
98 $object->$parentObject = clone $parent;
104 $this->options = array(
105 'script_url' => $_SERVER[
'PHP_SELF'],
106 'upload_dir' => $dir_output.
'/'.$object_ref.
'/',
107 'upload_url' => DOL_URL_ROOT.
'/document.php?modulepart='.$element.
'&attachment=1&file=/'.$object_ref.
'/',
108 'param_name' =>
'files',
111 'delete_type' =>
'DELETE',
114 'max_file_size' =>
null,
115 'min_file_size' => 1,
116 'accept_file_types' =>
'/.+$/i',
118 'max_number_of_files' =>
null,
121 'max_height' =>
null,
125 'discard_aborted_uploads' =>
true,
126 'image_versions' => array(
139 'thumbnail' => array(
140 'upload_dir' => $dir_output.
'/'.$object_ref.
'/thumbs/',
141 'upload_url' => DOL_URL_ROOT.
'/document.php?modulepart='.urlencode($element).
'&attachment=1&file='.urlencode(
'/'.$object_ref.
'/thumbs/'),
150 $hookmanager->executeHooks(
151 'overrideUploadOptions',
153 'options' => &$options,
154 'element' => $element
161 $this->options = array_replace_recursive($this->options, $options);
165 if (empty($pathname) || empty($filename)) {
166 if (!array_key_exists(
"upload_dir", $this->options)) {
167 setEventMessage(
'If $fk_element = null or $element = null you must specify upload_dir on $options',
'errors');
168 throw new Exception(
'If $fk_element = null or $element = null you must specify upload_dir on $options');
169 } elseif (!is_dir($this->options[
'upload_dir'])) {
170 setEventMessage(
'The directory '.$this->options[
'upload_dir'].
' doesn\'t exists',
'errors');
171 throw new Exception(
'The directory '.$this->options[
'upload_dir'].
' doesn\'t exists');
172 } elseif (!is_writable($this->options[
'upload_dir'])) {
173 setEventMessage(
'The directory '.$this->options[
'upload_dir'].
' is not writable',
'errors');
174 throw new Exception(
'The directory '.$this->options[
'upload_dir'].
' is not writable');
186 $https = !empty($_SERVER[
'HTTPS']) && $_SERVER[
'HTTPS'] !==
'off';
188 ($https ?
'https://' :
'http://').
189 (!empty($_SERVER[
'REMOTE_USER']) ? $_SERVER[
'REMOTE_USER'].
'@' :
'').
190 (isset($_SERVER[
'HTTP_HOST']) ? $_SERVER[
'HTTP_HOST'] : ($_SERVER[
'SERVER_NAME'].
191 ($https && $_SERVER[
'SERVER_PORT'] === 443 ||
192 $_SERVER[
'SERVER_PORT'] === 80 ?
'' :
':'.$_SERVER[
'SERVER_PORT']))).
193 substr($_SERVER[
'SCRIPT_NAME'], 0, strrpos($_SERVER[
'SCRIPT_NAME'],
'/'));
204 $file->delete_url = $this->options[
'script_url'].
'?file='.urlencode((
string) ($file->name)).
'&fk_element='.urlencode((
string) ($this->fk_element)).
'&element='.urlencode((
string) ($this->element));
205 $file->delete_type = $this->options[
'delete_type'];
206 if ($file->delete_type !==
'DELETE') {
207 $file->delete_url .=
'&_method=DELETE';
219 $file_path = $this->options[
'upload_dir'].dol_sanitizeFileName($file_name);
221 if (
dol_is_file($file_path) && $file_name[0] !==
'.') {
223 $file->name = $file_name;
225 $file->size = filesize($file_path);
226 $file->url = $this->options[
'upload_url'].urlencode($file->name);
228 foreach ($this->options[
'image_versions'] as $version => $options) {
229 if (
dol_is_file($options[
'upload_dir'].$file_name)) {
230 $tmp = explode(
'.', $file->name);
233 $keyforfile = $version.
'_url';
234 $file->$keyforfile = $options[
'upload_url'].urlencode($tmp[0].
'_mini.'.$tmp[1]);
250 return array_values(array_filter(array_map(array($this,
'getFileObject'), scandir($this->options[
'upload_dir']))));
262 global $maxwidthmini, $maxheightmini, $maxwidthsmall, $maxheightsmall;
264 $file_path = $this->options[
'upload_dir'].$file_name;
265 $new_file_path = $options[
'upload_dir'].$file_name;
267 if (
dol_mkdir($options[
'upload_dir']) >= 0) {
268 list($img_width, $img_height) = @getimagesize($file_path);
269 if (!$img_width || !$img_height) {
273 $res =
vignette($file_path, $maxwidthmini, $maxheightmini,
'_mini');
274 if (preg_match(
'/error/i', $res)) {
278 $res =
vignette($file_path, $maxwidthsmall, $maxheightsmall,
'_small');
279 if (preg_match(
'/error/i', $res)) {
298 protected function validate($uploaded_file, $file, $error, $index)
301 $file->error = $error;
305 $file->error =
'missingFileName';
308 if (!preg_match($this->options[
'accept_file_types'], $file->name)) {
309 $file->error =
'acceptFileTypes';
312 if ($uploaded_file && is_uploaded_file($uploaded_file)) {
315 $file_size = $_SERVER[
'CONTENT_LENGTH'];
317 if ($this->options[
'max_file_size'] && (
318 $file_size > $this->options[
'max_file_size'] ||
319 $file->size > $this->options[
'max_file_size']
322 $file->error =
'maxFileSize';
325 if ($this->options[
'min_file_size'] &&
326 $file_size < $this->options[
'min_file_size']) {
327 $file->error =
'minFileSize';
330 if (is_numeric($this->options[
'max_number_of_files']) && (
331 count($this->
getFileObjects()) >= $this->options[
'max_number_of_files']
334 $file->error =
'maxNumberOfFiles';
337 list($img_width, $img_height) = @getimagesize($uploaded_file);
338 if (is_numeric($img_width)) {
339 if ($this->options[
'max_width'] && $img_width > $this->options[
'max_width'] ||
340 $this->options[
'max_height'] && $img_height > $this->options[
'max_height']) {
341 $file->error =
'maxResolution';
344 if ($this->options[
'min_width'] && $img_width < $this->options[
'min_width'] ||
345 $this->options[
'min_height'] && $img_height < $this->options[
'min_height']) {
346 $file->error =
'minResolution';
361 $index = isset($matches[1]) ? intval($matches[1]) + 1 : 1;
362 $ext = isset($matches[2]) ? $matches[2] :
'';
363 return ' ('.$index.
')'.$ext;
374 return preg_replace_callback(
'/(?:(?: \(([\d]+)\))?(\.[^.]+))?$/', array($this,
'upcountNameCallback'), $name, 1);
392 if (strpos($file_name,
'.') ===
false && preg_match(
'/^image\/(gif|jpe?g|png)/', $type, $matches)) {
393 $file_name .=
'.'.$matches[1];
395 if ($this->options[
'discard_aborted_uploads']) {
396 while (
dol_is_file($this->options[
'upload_dir'].$file_name)) {
418 $file->name = $this->
trimFileName($name, $type, $index);
420 $file->size = intval($size);
425 $uploaded_file = preg_replace(
'/\s*(http|ftp)s?:/i',
'', $uploaded_file);
426 $uploaded_file = realpath($uploaded_file);
428 $validate = $this->
validate($uploaded_file, $file, $error, $index);
431 if (
dol_mkdir($this->options[
'upload_dir']) >= 0) {
432 $file_path =
dol_sanitizePathName($this->options[
'upload_dir']).dol_sanitizeFileName($file->name);
433 $append_file = !$this->options[
'discard_aborted_uploads'] &&
dol_is_file($file_path) && $file->size >
dol_filesize($file_path);
437 if ($uploaded_file && is_uploaded_file($uploaded_file)) {
440 file_put_contents($file_path, fopen($uploaded_file,
'r'), FILE_APPEND);
446 file_put_contents($file_path, fopen(
'php://input',
'r'), $append_file ? FILE_APPEND : 0);
449 if ($file_size === $file->size) {
450 $file->url = $this->options[
'upload_url'].urlencode($file->name);
451 foreach ($this->options[
'image_versions'] as $version => $options) {
453 $tmp = explode(
'.', $file->name);
456 $keyforfile = $version.
'_url';
457 $file->$keyforfile = $options[
'upload_url'].urlencode($tmp[0].
'_mini.'.$tmp[1]);
460 } elseif ($this->options[
'discard_aborted_uploads']) {
462 $file->error =
'abort';
464 $file->size = $file_size;
467 $file->error =
'failedtocreatedestdir';
504 $upload = isset($_FILES[$this->options[
'param_name']]) ? $_FILES[$this->options[
'param_name']] :
null;
507 if ($upload && is_array($upload[
'tmp_name'])) {
510 foreach ($upload[
'tmp_name'] as $index => $value) {
512 $upload[
'tmp_name'][$index],
513 isset($_SERVER[
'HTTP_X_FILE_NAME']) ? $_SERVER[
'HTTP_X_FILE_NAME'] : $upload[
'name'][$index],
514 isset($_SERVER[
'HTTP_X_FILE_SIZE']) ? $_SERVER[
'HTTP_X_FILE_SIZE'] : $upload[
'size'][$index],
515 isset($_SERVER[
'HTTP_X_FILE_TYPE']) ? $_SERVER[
'HTTP_X_FILE_TYPE'] : $upload[
'type'][$index],
516 $upload[
'error'][$index],
519 if (!empty($tmpres->error)) {
524 } elseif ($upload || isset($_SERVER[
'HTTP_X_FILE_NAME'])) {
528 isset($upload[
'tmp_name']) ? $upload[
'tmp_name'] :
null,
529 isset($_SERVER[
'HTTP_X_FILE_NAME']) ? $_SERVER[
'HTTP_X_FILE_NAME'] : (isset($upload[
'name']) ? $upload[
'name'] :
null),
530 isset($_SERVER[
'HTTP_X_FILE_SIZE']) ? $_SERVER[
'HTTP_X_FILE_SIZE'] : (isset($upload[
'size']) ? $upload[
'size'] :
null),
531 isset($_SERVER[
'HTTP_X_FILE_TYPE']) ? $_SERVER[
'HTTP_X_FILE_TYPE'] : (isset($upload[
'type']) ? $upload[
'type'] :
null),
532 isset($upload[
'error']) ? $upload[
'error'] :
null,
535 if (!empty($tmpres->error)) {
541 header(
'Vary: Accept');
542 $json = json_encode($info);
552 if (isset($_SERVER[
'HTTP_ACCEPT']) && (strpos($_SERVER[
'HTTP_ACCEPT'],
'application/json') !==
false)) {
553 header(
'Content-type: application/json');
555 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, $attop=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).