dolibarr 25.0.0-alpha
flowjs-server.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2023-2026 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
4 * Copyright (C) 2024-2026 Frédéric France <frederic.france@free.fr>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
25if (!defined('NOTOKENRENEWAL')) {
26 define('NOTOKENRENEWAL', '1'); // Disables token renewal
27}
28// If there is no need to load and show top and left menu
29if (!defined('NOREQUIREMENU')) {
30 define('NOREQUIREMENU', '1');
31}
32if (!defined('NOREQUIREHTML')) {
33 define('NOREQUIREHTML', '1');
34}
35if (!defined('NOREQUIREAJAX')) {
36 define('NOREQUIREAJAX', '1');
37}
38if (!defined('NOREQUIRESOC')) {
39 define('NOREQUIRESOC', '1');
40}
41
42//if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN','1'); // Required to know date format for dol_print_date
43
44// Load Dolibarr environment
45require '../../main.inc.php';
46require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
47
56$action = GETPOST('action', 'aZ09');
57
58$module = GETPOST('module', 'aZ09arobase');
59$uploaddirname = dol_sanitizeFileName(GETPOST('uploaddirname', 'alpha'));
60
61$flowFilename = dol_sanitizeFileName(GETPOST('flowFilename', 'alpha'));
62$flowIdentifier = dol_sanitizeFileName(GETPOST('flowIdentifier', 'alpha'));
63$flowChunkNumber = GETPOST('flowChunkNumber', 'alpha');
64$flowChunkSize = GETPOST('flowChunkSize', 'alpha');
65$flowTotalSize = GETPOST('flowTotalSize', 'alpha');
66
67$result = restrictedArea($user, ($module ? $module : 'unknown'), 0, '', '', 'fk_soc', 'rowid', 0, 1); // Call with mode return. Use 'unknown' if module not defined to be sure to have an error when module is not set
68
69if (!$result) {
70 httponly_accessforbidden("No permission on module ".$module);
71}
72
73if ($action != 'upload') {
74 httponly_accessforbidden("Param action must be 'upload'");
75}
76
77if (!empty($conf->$module->dir_temp)) {
78 $upload_dir = $conf->$module->dir_temp;
79 if (!empty($uploaddirname)) {
80 $upload_dir .= "/".$uploaddirname;
81 }
82} else {
83 httponly_accessforbidden("Param module does not has a dir_temp directory. Module does not exists or is not activated.");
84}
85
86
87/*
88 * Action
89 */
90
92
93$result = false;
94
95if (!empty($upload_dir)) {
96 $temp_dir = $upload_dir.'/'.$flowIdentifier;
97} else {
98 $temp_dir = DOL_DATA_ROOT.'/'.$module.'/temp/'.$flowIdentifier;
99 $upload_dir = DOL_DATA_ROOT.'/'.$module.'/temp/';
100}
101
102if ($module != "test" && !isModEnabled($module)) {
103 echo json_encode("The module ".$module." is not enabled");
104 header("HTTP/1.0 400");
105 die();
106}
107
108if ($_SERVER['REQUEST_METHOD'] === 'GET') {
109 $chunk_file = $temp_dir.'/'.$flowFilename.'.part'.$flowChunkNumber;
110 if (file_exists($chunk_file)) {
111 header("HTTP/1.0 200 Ok");
112 } else {
113 header("HTTP/1.0 404 Not Found");
114 }
115} else {
116 // loop through files and move the chunks to a temporarily created directory
117 if (file_exists($upload_dir.'/'.$flowFilename)) {
118 echo json_encode('File '.$flowIdentifier.' was already uploaded');
119 header("HTTP/1.0 200 Ok");
120 die();
121 } elseif (!empty($_FILES)) {
122 foreach ($_FILES as $file) {
123 // check the error status
124 if ($file['error'] != 0) {
125 dol_syslog('error '.$file['error'].' in file '.$flowFilename);
126 continue;
127 }
128
129 // init the destination file (format <filename.ext>.part<#chunk>
130 // the file is stored in a temporary directory
131 $dest_file = $temp_dir.'/'.$flowFilename.'.part'.$flowChunkNumber;
132
133 // create the temporary directory
134 if (!dol_is_dir($temp_dir)) {
135 dol_mkdir($temp_dir);
136 }
137
138 // move the temporary file
139 // Note: the returned value is a string when the file was refused and, in PHP 8, such a string compares as greater than 0
140 $resultupload = dol_move_uploaded_file($file['tmp_name'], $dest_file, 0);
141 if (!is_numeric($resultupload) || $resultupload <= 0) {
142 dol_syslog('Error saving (move_uploaded_file) chunk '.$flowChunkNumber.' for file '.$flowFilename);
143 } else {
144 // check if all the parts present, and create the final destination file
145 $result = createFileFromChunks($temp_dir, $upload_dir, $flowFilename, $flowChunkSize, $flowTotalSize);
146 }
147 }
148 }
149}
150if ($result) {
151 echo json_encode('File '.$flowIdentifier.' uploaded');
152} else {
153 echo json_encode('Error while uploading file '.$flowIdentifier);
154}
155
156
167function createFileFromChunks($temp_dir, $upload_dir, $fileName, $chunkSize, $totalSize)
168{
169 dol_syslog(__FUNCTION__, LOG_DEBUG);
170
171 // count all the parts of this file
172 $total_files = 0;
173 $files = dol_dir_list($temp_dir, 'files');
174 foreach ($files as $file) {
175 if (stripos($file["name"], $fileName) !== false) {
176 $total_files++;
177 }
178 }
179
180 // check that all the parts are present
181 // the size of the last part is between chunkSize and 2*$chunkSize
182 if ($total_files * (float) $chunkSize >= ((float) $totalSize - (float) $chunkSize + 1)) {
183 // Security: disallow file with executable extensions, rename them like dol_move_uploaded_file() does
184 $destFileName = $fileName;
185 if (isAFileWithExecutableContent($destFileName) && !getDolGlobalString('MAIN_DOCUMENT_IS_OUTSIDE_WEBROOT_SO_NOEXE_NOT_REQUIRED')) {
186 $destFileName .= '.noexe';
187 }
188
189 // create the final destination file
190 if (($fp = fopen($upload_dir.'/'.$destFileName, 'w')) !== false) {
191 for ($i = 1; $i <= $total_files; $i++) {
192 fwrite($fp, file_get_contents($temp_dir.'/'.$fileName.'.part'.$i));
193 dol_syslog('writing chunk '.$i);
194 }
195 fclose($fp);
196 } else {
197 dol_syslog('cannot create the destination file');
198 return false;
199 }
200
201 // Security: scan the assembled file (a virus may be split across chunks)
202 $checkvirusarray = dolCheckVirus($upload_dir.'/'.$destFileName, $upload_dir.'/'.$fileName);
203 if (count($checkvirusarray)) {
204 dol_syslog('File "'.$destFileName.'" KO with antivirus: errors='.implode(',', $checkvirusarray), LOG_WARNING);
205 dol_delete_file($upload_dir.'/'.$destFileName, 0, 1);
206 return false;
207 }
208
209 // rename the temporary directory (to avoid access from other
210 // concurrent chunks uploads)
211 @rename($temp_dir, $temp_dir.'_UNUSED');
212 }
213
214 return true;
215}
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(! $sortfield) if(! $sortorder) $module
Definition list.php:193
dol_delete_file($file, $disableglob=0, $nophperrors=0, $nohook=0, $object=null, $allowdotdot=false, $indexdatabase=1, $nolog=0)
Remove a file or several files with a mask.
dolCheckVirus($src_file, $dest_file='')
Check virus into a file.
dol_dir_list($utf8_path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0, $nbsecondsold=0)
Scan a directory and return a list of files/directories.
Definition files.lib.php:66
dol_is_dir($folder)
Test if filename is a directory.
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.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0, $allowdash=0)
Clean a string to use it as a file name.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
isAFileWithExecutableContent($filename)
Return if a file can contains executable content.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
if(!defined( 'NOREQUIREMENU')) if(!empty(GETPOST('seteventmessages', 'alpha'))) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
httponly_accessforbidden($message='1', $http_response_code=403, $stringalreadysanitized=0)
Show a message to say access is forbidden and stop program.
restrictedArea(User $user, $features, $object=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $isdraft=0, $nodie=0, $mode='')
Check permissions of a user to show a page and an object.