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']);
 
   69    if (empty($dir_output)) {
 
   70      setEventMessage(
'The element '.$element.
' is not supported for uploading file. dir_output is unknown.', 
'errors');
 
   71      throw new Exception(
'The element '.$element.
' is not supported for uploading file. dir_output is unknown.');
 
   75    if ($pathname !== 
null && $filename !== 
null) {
 
   83        $savingDocMask = $object_ref.
'-__file__';
 
   87      if ($element == 
'invoice_supplier') {
 
   89      } elseif ($element == 
'project_task') {
 
   90        $parentForeignKey = 
'fk_project';
 
   91        $parentClass = 
'Project';
 
   92        $parentElement = 
'projet';
 
   93        $parentObject = 
'project';
 
   96        $parent = 
new $parentClass($db);
 
   97        $parent->fetch(
$object->$parentForeignKey);
 
   98        if (!empty($parent->socid)) {
 
   99          $parent->fetch_thirdparty();
 
  101        $object->$parentObject = clone $parent;
 
  107    $this->options = array(
 
  108      'script_url' => $_SERVER[
'PHP_SELF'],
 
  109      'upload_dir' => $dir_output.
'/'.$object_ref.
'/',
 
  110      'upload_url' => DOL_URL_ROOT.
'/document.php?modulepart='.$element.
'&attachment=1&file=/'.$object_ref.
'/',
 
  111      'saving_doc_mask' => $savingDocMask,
 
  112      'param_name' => 
'files',
 
  115      'delete_type' => 
'DELETE',
 
  118      'max_file_size' => 
null,
 
  119      'min_file_size' => 1,
 
  120      'accept_file_types' => 
'/.+$/i',
 
  122      'max_number_of_files' => 
null,
 
  125      'max_height' => 
null,
 
  129      'discard_aborted_uploads' => 
true,
 
  130      'image_versions' => array(
 
  143        'thumbnail' => array(
 
  144          'upload_dir' => $dir_output.
'/'.$object_ref.
'/thumbs/',
 
  145          'upload_url' => DOL_URL_ROOT.
'/document.php?modulepart='.urlencode($element).
'&attachment=1&file='.urlencode(
'/'.$object_ref.
'/thumbs/'),
 
  154    $hookmanager->executeHooks(
 
  155      'overrideUploadOptions',
 
  157        'options' => &$options,
 
  158        'element' => $element
 
  165      $this->options = array_replace_recursive($this->options, $options);
 
  170    if ($pathname === 
null || $filename === 
null) {
 
  171      if (!array_key_exists(
"upload_dir", $this->options)) {
 
  172        setEventMessage(
'If $fk_element = null or $element = null you must specify upload_dir on $options', 
'errors');
 
  173        throw new Exception(
'If $fk_element = null or $element = null you must specify upload_dir on $options');
 
  174      } elseif (!is_dir($this->options[
'upload_dir'])) {
 
  175        setEventMessage(
'The directory '.$this->options[
'upload_dir'].
' doesn\'t exists', 
'errors');
 
  176        throw new Exception(
'The directory '.$this->options[
'upload_dir'].
' doesn\'t exists');
 
  177      } elseif (!is_writable($this->options[
'upload_dir'])) {
 
  178        setEventMessage(
'The directory '.$this->options[
'upload_dir'].
' is not writable', 
'errors');
 
  179        throw new Exception(
'The directory '.$this->options[
'upload_dir'].
' is not writable');
 
 
  191    $https = !empty($_SERVER[
'HTTPS']) && $_SERVER[
'HTTPS'] !== 
'off';
 
  193    ($https ? 
'https://' : 
'http://').
 
  194    (!empty($_SERVER[
'REMOTE_USER']) ? $_SERVER[
'REMOTE_USER'].
'@' : 
'').
 
  195    (isset($_SERVER[
'HTTP_HOST']) ? $_SERVER[
'HTTP_HOST'] : ($_SERVER[
'SERVER_NAME'].
 
  196        ($https && $_SERVER[
'SERVER_PORT'] === 443 ||
 
  197            $_SERVER[
'SERVER_PORT'] === 80 ? 
'' : 
':'.$_SERVER[
'SERVER_PORT']))).
 
  198            substr($_SERVER[
'SCRIPT_NAME'], 0, strrpos($_SERVER[
'SCRIPT_NAME'], 
'/'));
 
 
  209    $file->delete_url = $this->options[
'script_url'].
'?file='.urlencode((
string) ($file->name)).
'&fk_element='.urlencode((
string) ($this->fk_element)).
'&element='.urlencode((
string) ($this->element));
 
  210    $file->delete_type = $this->options[
'delete_type'];
 
  211    if ($file->delete_type !== 
'DELETE') {
 
  212      $file->delete_url .= 
'&_method=DELETE';
 
 
  224    $file_path = $this->options[
'upload_dir'].dol_sanitizeFileName($file_name);
 
  226    if (
dol_is_file($file_path) && $file_name[0] !== 
'.') {
 
  228      $file->name = $file_name;
 
  230      $file->size = filesize($file_path);
 
  231      $file->url = $this->options[
'upload_url'].urlencode($file->name);
 
  233      foreach ($this->options[
'image_versions'] as $version => $options) {
 
  234        if (
dol_is_file($options[
'upload_dir'].$file_name)) {
 
  235          $tmp = explode(
'.', $file->name);
 
  238          $keyforfile = $version.
'_url';
 
  239          $file->$keyforfile = $options[
'upload_url'].urlencode($tmp[0].
'_mini.'.$tmp[1]);
 
 
  255    return array_values(array_filter(array_map(array($this, 
'getFileObject'), scandir($this->options[
'upload_dir']))));
 
 
  267    global $maxwidthmini, $maxheightmini, $maxwidthsmall, $maxheightsmall;
 
  269    $file_path = $this->options[
'upload_dir'].$file_name;
 
  270    $new_file_path = $options[
'upload_dir'].$file_name;
 
  272    if (
dol_mkdir($options[
'upload_dir']) >= 0) {
 
  273      list($img_width, $img_height) = @getimagesize($file_path);
 
  274      if (!$img_width || !$img_height) {
 
  278      $res = 
vignette($file_path, $maxwidthmini, $maxheightmini, 
'_mini'); 
 
  279      if (preg_match(
'/error/i', $res)) {
 
  283      $res = 
vignette($file_path, $maxwidthsmall, $maxheightsmall, 
'_small'); 
 
  284      if (preg_match(
'/error/i', $res)) {
 
 
  303  protected function validate($uploaded_file, $file, $error, $index)
 
  306      $file->error = $error;
 
  310      $file->error = 
'missingFileName';
 
  313    if (!preg_match($this->options[
'accept_file_types'], $file->name)) {
 
  314      $file->error = 
'acceptFileTypes';
 
  317    if ($uploaded_file && is_uploaded_file($uploaded_file)) {
 
  320      $file_size = $_SERVER[
'CONTENT_LENGTH'];
 
  322    if ($this->options[
'max_file_size'] && (
 
  323      $file_size > $this->options[
'max_file_size'] ||
 
  324        $file->size > $this->options[
'max_file_size']
 
  327      $file->error = 
'maxFileSize';
 
  330    if ($this->options[
'min_file_size'] &&
 
  331        $file_size < $this->options[
'min_file_size']) {
 
  332      $file->error = 
'minFileSize';
 
  335    if (is_numeric($this->options[
'max_number_of_files']) && (
 
  336      count($this->
getFileObjects()) >= $this->options[
'max_number_of_files']
 
  339      $file->error = 
'maxNumberOfFiles';
 
  342    list($img_width, $img_height) = @getimagesize($uploaded_file);
 
  343    if (is_numeric($img_width)) {
 
  344      if ($this->options[
'max_width'] && $img_width > $this->options[
'max_width'] ||
 
  345          $this->options[
'max_height'] && $img_height > $this->options[
'max_height']) {
 
  346        $file->error = 
'maxResolution';
 
  349      if ($this->options[
'min_width'] && $img_width < $this->options[
'min_width'] ||
 
  350          $this->options[
'min_height'] && $img_height < $this->options[
'min_height']) {
 
  351        $file->error = 
'minResolution';
 
 
  366    $index = isset($matches[1]) ? intval($matches[1]) + 1 : 1;
 
  367    $ext = isset($matches[2]) ? $matches[2] : 
'';
 
  368    return ' ('.$index.
')'.$ext;
 
 
  379    return preg_replace_callback(
'/(?:(?: \(([\d]+)\))?(\.[^.]+))?$/', array($this, 
'upcountNameCallback'), $name, 1);
 
 
  397    if (strpos($file_name, 
'.') === 
false && preg_match(
'/^image\/(gif|jpe?g|png)/', $type, $matches)) {
 
  398      $file_name .= 
'.'.$matches[1];
 
  400    if ($this->options[
'discard_aborted_uploads']) {
 
  401      while (
dol_is_file($this->options[
'upload_dir'].$file_name)) {
 
 
  423    $file->name = $this->
trimFileName($name, $type, $index);
 
  425    $file->size = intval($size);
 
  430    $uploaded_file = preg_replace(
'/\s*(http|ftp)s?:/i', 
'', $uploaded_file);
 
  431    $uploaded_file = realpath($uploaded_file);  
 
  433    $validate = $this->
validate($uploaded_file, $file, $error, $index);
 
  436      if (
dol_mkdir($this->options[
'upload_dir']) >= 0) {
 
  438        $fileNameWithoutExt = preg_replace(
'/\.[^\.]+$/', 
'', $file->name);
 
  439        $savingDocMask = $this->options[
'saving_doc_mask'];
 
  440        if ($savingDocMask && strpos($savingDocMask, $fileNameWithoutExt) !== 0) {
 
  441          $fileNameWithPrefix = preg_replace(
'/__file__/', $file->name, $savingDocMask);
 
  442          $file->name = $fileNameWithPrefix;
 
  445        $file_path = 
dol_sanitizePathName($this->options[
'upload_dir']).dol_sanitizeFileName($file->name);
 
  446        $append_file = !$this->options[
'discard_aborted_uploads'] && 
dol_is_file($file_path) && $file->size > 
dol_filesize($file_path);
 
  450        if ($uploaded_file && is_uploaded_file($uploaded_file)) {
 
  453            file_put_contents($file_path, fopen($uploaded_file, 
'r'), FILE_APPEND);
 
  459          file_put_contents($file_path, fopen(
'php://input', 
'r'), $append_file ? FILE_APPEND : 0);
 
  462        if ($file_size === $file->size) {
 
  463          $file->url = $this->options[
'upload_url'].urlencode($file->name);
 
  464          foreach ($this->options[
'image_versions'] as $version => $options) {
 
  466              $tmp = explode(
'.', $file->name);
 
  469              $keyforfile = $version.
'_url';
 
  470              $file->$keyforfile = $options[
'upload_url'].urlencode($tmp[0].
'_mini.'.$tmp[1]);
 
  473        } elseif ($this->options[
'discard_aborted_uploads']) {
 
  475          $file->error = 
'abort';
 
  477        $file->size = $file_size;
 
  480        $file->error = 
'failedtocreatedestdir';
 
 
  517    $upload = isset($_FILES[$this->options[
'param_name']]) ? $_FILES[$this->options[
'param_name']] : 
null;
 
  520    if ($upload && is_array($upload[
'tmp_name'])) {
 
  523      foreach ($upload[
'tmp_name'] as $index => $value) {
 
  525          $upload[
'tmp_name'][$index],
 
  526          isset($_SERVER[
'HTTP_X_FILE_NAME']) ? $_SERVER[
'HTTP_X_FILE_NAME'] : $upload[
'name'][$index],
 
  527          isset($_SERVER[
'HTTP_X_FILE_SIZE']) ? $_SERVER[
'HTTP_X_FILE_SIZE'] : $upload[
'size'][$index],
 
  528          isset($_SERVER[
'HTTP_X_FILE_TYPE']) ? $_SERVER[
'HTTP_X_FILE_TYPE'] : $upload[
'type'][$index],
 
  529          $upload[
'error'][$index],
 
  532        if (!empty($tmpres->error)) {
 
  537    } elseif ($upload || isset($_SERVER[
'HTTP_X_FILE_NAME'])) {
 
  541        isset($upload[
'tmp_name']) ? $upload[
'tmp_name'] : 
null,
 
  542        isset($_SERVER[
'HTTP_X_FILE_NAME']) ? $_SERVER[
'HTTP_X_FILE_NAME'] : (isset($upload[
'name']) ? $upload[
'name'] : 
null),
 
  543        isset($_SERVER[
'HTTP_X_FILE_SIZE']) ? $_SERVER[
'HTTP_X_FILE_SIZE'] : (isset($upload[
'size']) ? $upload[
'size'] : 
null),
 
  544        isset($_SERVER[
'HTTP_X_FILE_TYPE']) ? $_SERVER[
'HTTP_X_FILE_TYPE'] : (isset($upload[
'type']) ? $upload[
'type'] : 
null),
 
  545        isset($upload[
'error']) ? $upload[
'error'] : 
null,
 
  548      if (!empty($tmpres->error)) {
 
  554    header(
'Vary: Accept');
 
  555    $json = json_encode($info);
 
  565    if (isset($_SERVER[
'HTTP_ACCEPT']) && (strpos($_SERVER[
'HTTP_ACCEPT'], 
'application/json') !== 
false)) {
 
  566      header(
'Content-type: application/json');
 
  568      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.
 
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
 
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).