29require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
30require_once DOL_DOCUMENT_ROOT.
'/core/lib/images.lib.php';
45 protected $fk_element;
60 public function __construct($options =
null, $fk_element =
null, $element =
null)
65 $hookmanager->initHooks(array(
'fileupload'));
67 $element_prop = getElementProperties($element);
70 $this->fk_element = $fk_element;
71 $this->element = $element;
73 $pathname = str_replace(
'/class',
'', $element_prop[
'classpath']);
80 if (empty($dir_output)) {
81 setEventMessage(
'The element '.$element.
' is not supported for uploading file. dir_output is unknown.',
'errors');
82 throw new Exception(
'The element '.$element.
' is not supported for uploading file. dir_output is unknown.');
85 $object_ref =
'UndefinedReference';
88 if ($pathname !==
null && $filename !==
null) {
90 $object = fetchObjectByElement($fk_element, $element);
96 $savingDocMask = $object_ref .
'-__file__';
100 if ($element ==
'invoice_supplier') {
102 } elseif ($element ==
'project_task') {
103 $parentForeignKey =
'fk_project';
104 $parentClass =
'Project';
105 $parentElement =
'projet';
106 $parentObject =
'project';
109 $parent =
new $parentClass(
$db);
110 if (
$object->$parentForeignKey !==
null) {
111 $parent->fetch((
int)
$object->$parentForeignKey);
112 if (!empty($parent->socid)) {
113 $parent->fetch_thirdparty();
115 $object->$parentObject = clone $parent;
122 $this->options = array(
123 'script_url' => $_SERVER[
'PHP_SELF'],
124 'upload_dir' => $dir_output.
'/'.$object_ref.
'/',
125 'upload_url' => DOL_URL_ROOT.
'/document.php?modulepart='.$element.
'&attachment=1&file=/'.$object_ref.
'/',
126 'saving_doc_mask' => $savingDocMask,
127 'param_name' =>
'files',
130 'delete_type' =>
'DELETE',
133 'max_file_size' =>
null,
134 'min_file_size' => 1,
135 'accept_file_types' =>
'/.+$/i',
137 'max_number_of_files' =>
null,
140 'max_height' =>
null,
144 'discard_aborted_uploads' =>
true,
145 'image_versions' => array(
158 'thumbnail' => array(
159 'upload_dir' => $dir_output.
'/'.$object_ref.
'/thumbs/',
160 'upload_url' => DOL_URL_ROOT.
'/document.php?modulepart='.urlencode($element).
'&attachment=1&file='.urlencode(
'/'.$object_ref.
'/thumbs/'),
169 $hookmanager->executeHooks(
170 'overrideUploadOptions',
172 'options' => &$options,
173 'element' => $element
180 $this->options = array_replace_recursive($this->options, $options);
184 if (empty($pathname) || empty($filename)) {
185 if (!array_key_exists(
"upload_dir", $this->options)) {
186 setEventMessage(
'If $fk_element = null or $element = null you must specify upload_dir on $options',
'errors');
187 throw new Exception(
'If $fk_element = null or $element = null you must specify upload_dir on $options');
188 } elseif (!is_dir($this->options[
'upload_dir'])) {
189 setEventMessage(
'The directory '.$this->options[
'upload_dir'].
' doesn\'t exists',
'errors');
190 throw new Exception(
'The directory '.$this->options[
'upload_dir'].
' doesn\'t exists');
191 } elseif (!is_writable($this->options[
'upload_dir'])) {
192 setEventMessage(
'The directory '.$this->options[
'upload_dir'].
' is not writable',
'errors');
193 throw new Exception(
'The directory '.$this->options[
'upload_dir'].
' is not writable');
205 $https = !empty($_SERVER[
'HTTPS']) && $_SERVER[
'HTTPS'] !==
'off';
207 ($https ?
'https://' :
'http://').
208 (!empty($_SERVER[
'REMOTE_USER']) ? $_SERVER[
'REMOTE_USER'].
'@' :
'').
209 (isset($_SERVER[
'HTTP_HOST']) ? $_SERVER[
'HTTP_HOST'] : ($_SERVER[
'SERVER_NAME'].
210 ($https && $_SERVER[
'SERVER_PORT'] === 443 ||
211 $_SERVER[
'SERVER_PORT'] === 80 ?
'' :
':'.$_SERVER[
'SERVER_PORT']))).
212 substr($_SERVER[
'SCRIPT_NAME'], 0, strrpos($_SERVER[
'SCRIPT_NAME'],
'/'));
223 $file->delete_url = $this->options[
'script_url'].
'?file='.urlencode((
string) ($file->name)).
'&fk_element='.urlencode((
string) ($this->fk_element)).
'&element='.urlencode((
string) ($this->element));
224 $file->delete_type = $this->options[
'delete_type'];
225 if ($file->delete_type !==
'DELETE') {
226 $file->delete_url .=
'&_method=DELETE';
238 $file_path = $this->options[
'upload_dir'].dol_sanitizeFileName($file_name);
240 if (
dol_is_file($file_path) && $file_name[0] !==
'.') {
242 $file->name = $file_name;
243 $file->mime = dol_mimetype($file_name,
'', 2);
244 $file->size = filesize($file_path);
245 $file->url = $this->options[
'upload_url'].urlencode($file->name);
247 foreach ($this->options[
'image_versions'] as $version => $options) {
248 if (
dol_is_file($options[
'upload_dir'].$file_name)) {
249 $tmp = explode(
'.', $file->name);
252 $keyforfile = $version.
'_url';
253 $file->$keyforfile = $options[
'upload_url'].urlencode($tmp[0].
'_mini.'.$tmp[1]);
269 return array_values(array_filter(array_map(array($this,
'getFileObject'), scandir($this->options[
'upload_dir']))));
281 global $maxwidthmini, $maxheightmini, $maxwidthsmall, $maxheightsmall;
283 $file_path = $this->options[
'upload_dir'].$file_name;
284 $new_file_path = $options[
'upload_dir'].$file_name;
286 if (
dol_mkdir($options[
'upload_dir']) >= 0) {
287 list($img_width, $img_height) = @getimagesize($file_path);
288 if (!$img_width || !$img_height) {
292 $res =
vignette($file_path, $maxwidthmini, $maxheightmini,
'_mini');
293 if (preg_match(
'/error/i', $res)) {
297 $res =
vignette($file_path, $maxwidthsmall, $maxheightsmall,
'_small');
298 if (preg_match(
'/error/i', $res)) {
317 protected function validate($uploaded_file, $file, $error, $index)
320 $file->error = $error;
324 $file->error =
'missingFileName';
327 if (!preg_match($this->options[
'accept_file_types'], $file->name)) {
328 $file->error =
'acceptFileTypes';
331 if ($uploaded_file && is_uploaded_file($uploaded_file)) {
334 $file_size = $_SERVER[
'CONTENT_LENGTH'];
336 if ($this->options[
'max_file_size'] && (
337 $file_size > $this->options[
'max_file_size'] ||
338 $file->size > $this->options[
'max_file_size']
341 $file->error =
'maxFileSize';
344 if ($this->options[
'min_file_size'] &&
345 $file_size < $this->options[
'min_file_size']) {
346 $file->error =
'minFileSize';
349 if (is_numeric($this->options[
'max_number_of_files']) && (
350 count($this->
getFileObjects()) >= $this->options[
'max_number_of_files']
353 $file->error =
'maxNumberOfFiles';
356 list($img_width, $img_height) = @getimagesize($uploaded_file);
357 if (is_numeric($img_width)) {
358 if ($this->options[
'max_width'] && $img_width > $this->options[
'max_width'] ||
359 $this->options[
'max_height'] && $img_height > $this->options[
'max_height']) {
360 $file->error =
'maxResolution';
363 if ($this->options[
'min_width'] && $img_width < $this->options[
'min_width'] ||
364 $this->options[
'min_height'] && $img_height < $this->options[
'min_height']) {
365 $file->error =
'minResolution';
380 $index = isset($matches[1]) ? intval($matches[1]) + 1 : 1;
381 $ext = isset($matches[2]) ? $matches[2] :
'';
382 return ' ('.$index.
')'.$ext;
393 return preg_replace_callback(
'/(?:(?: \(([\d]+)\))?(\.[^.]+))?$/', array($this,
'upcountNameCallback'), $name, 1);
409 $file_name = preg_replace(
'/ {2,}/',
' ', $file_name);
412 if (strpos($file_name,
'.') ===
false && preg_match(
'/^image\/(gif|jpe?g|png)/', $type, $matches)) {
413 $file_name .=
'.'.$matches[1];
415 if ($this->options[
'discard_aborted_uploads']) {
416 while (
dol_is_file($this->options[
'upload_dir'].$file_name)) {
439 $file->name = $this->
trimFileName($name, $type, $index);
440 $file->mime = dol_mimetype($file->name,
'', 2);
441 $file->size = intval($size);
446 $uploaded_file = preg_replace(
'/\s*(http|ftp|sftp|)s?:/i',
'', $uploaded_file);
447 $uploaded_file = realpath($uploaded_file);
449 $validate = $this->
validate($uploaded_file, $file, $error, $index);
452 if (
dol_mkdir($this->options[
'upload_dir']) >= 0) {
454 $fileNameWithoutExt = preg_replace(
'/\.[^\.]+$/',
'', $file->name);
455 $savingDocMask = $this->options[
'saving_doc_mask'];
456 if ($savingDocMask && strpos($savingDocMask, $fileNameWithoutExt) !== 0) {
457 $fileNameWithPrefix = preg_replace(
'/__file__/', $file->name, $savingDocMask);
458 $file->name = $fileNameWithPrefix;
461 $file_path =
dol_sanitizePathName($this->options[
'upload_dir']).dol_sanitizeFileName($file->name);
462 $append_file = !$this->options[
'discard_aborted_uploads'] &&
dol_is_file($file_path) && $file->size >
dol_filesize($file_path);
466 if ($uploaded_file && is_uploaded_file($uploaded_file)) {
469 file_put_contents($file_path, fopen($uploaded_file,
'r'), FILE_APPEND);
476 file_put_contents($file_path, fopen(
'php://input',
'r'), $append_file ? FILE_APPEND : 0);
481 if ($file_size === $file->size) {
482 $file->url = $this->options[
'upload_url'].urlencode($file->name);
483 foreach ($this->options[
'image_versions'] as $version => $options) {
485 $tmp = explode(
'.', $file->name);
488 $keyforfile = $version.
'_url';
489 $file->$keyforfile = $options[
'upload_url'].urlencode($tmp[0].
'_mini.'.$tmp[1]);
492 } elseif ($this->options[
'discard_aborted_uploads']) {
494 $file->error =
'abort';
496 $file->size = $file_size;
499 $file->error =
'failedtocreatedestdir';
536 $upload = isset($_FILES[$this->options[
'param_name']]) ? $_FILES[$this->options[
'param_name']] :
null;
539 if ($upload && is_array($upload[
'tmp_name'])) {
542 foreach ($upload[
'tmp_name'] as $index => $value) {
544 $upload[
'tmp_name'][$index],
545 isset($_SERVER[
'HTTP_X_FILE_NAME']) ? $_SERVER[
'HTTP_X_FILE_NAME'] : $upload[
'name'][$index],
546 isset($_SERVER[
'HTTP_X_FILE_SIZE']) ? $_SERVER[
'HTTP_X_FILE_SIZE'] : $upload[
'size'][$index],
547 isset($_SERVER[
'HTTP_X_FILE_TYPE']) ? $_SERVER[
'HTTP_X_FILE_TYPE'] : $upload[
'type'][$index],
548 $upload[
'error'][$index],
551 if (!empty($tmpres->error)) {
556 } elseif ($upload || isset($_SERVER[
'HTTP_X_FILE_NAME'])) {
560 isset($upload[
'tmp_name']) ? $upload[
'tmp_name'] :
null,
561 isset($_SERVER[
'HTTP_X_FILE_NAME']) ? $_SERVER[
'HTTP_X_FILE_NAME'] : (isset($upload[
'name']) ? $upload[
'name'] :
null),
562 isset($_SERVER[
'HTTP_X_FILE_SIZE']) ? $_SERVER[
'HTTP_X_FILE_SIZE'] : (isset($upload[
'size']) ? $upload[
'size'] :
null),
563 isset($_SERVER[
'HTTP_X_FILE_TYPE']) ? $_SERVER[
'HTTP_X_FILE_TYPE'] : (isset($upload[
'type']) ? $upload[
'type'] :
null),
564 isset($upload[
'error']) ? $upload[
'error'] :
null,
567 if (!empty($tmpres->error)) {
573 header(
'Vary: Accept');
574 $json = json_encode($info);
584 if (isset($_SERVER[
'HTTP_ACCEPT']) && (strpos($_SERVER[
'HTTP_ACCEPT'],
'application/json') !==
false)) {
585 header(
'Content-type: application/json');
587 header(
'Content-type: text/plain');
if(! $sortfield) if(! $sortorder) $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_is_file($pathoffile)
Return if path is a file.
dol_move_uploaded_file($src_file, $dest_file, $allowoverwrite, $disablevirusscan=0, $uploaderrorcode=0, $nohook=0, $keyforsourcefile='addedfile', $upload_dir='', $mode=0)
Check validity of a file upload from an GUI page, and move it to its final destination.
setEventMessage($mesgs, $style='mesgs', $noduplicate=0, $attop=0)
Set event message in dol_events session object.
dol_sanitizePathName($str, $newstr='_', $unaccent=0, $allowdash=0)
Clean a string to use it as a path name.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0, $allowdash=0)
Clean a string to use it as a file name.
dolChmod($filepath, $newmask='')
Change mod of a file.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
if(!function_exists( 'dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
get_exdir($num, $level, $alpha, $withoutslash, $object, $modulepart='')
Return a path to have a the directory according to object where files are stored.
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).
print $langs trans("Show") . '< td style="' . $timeColor . '" align="center"> s</td > badge status0 badge status4 badge status3 Error badge status8< td align="center">< span class="badge ' . $badge . '"></span ></td >< td align="center">< a href="#" class="button button-small" onclick="openLogModal(this)" data-req="' . dol_escape_htmltag($reqSafe) . '" data-res="' . dol_escape_htmltag($resSafe) . '" data-err="' . dol_escape_htmltag($errSafe) . '">< span class="fa fa-search-plus"></span ></a ></td ></tr >< tr >< td colspan="' . $colspan . '" class="opacitymedium"></td ></tr ></table ></div ></form > logModal none logModal none s a JSON string
buildzip.php