dolibarr 22.0.5
flowjs-server.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2023 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
4 * Copyright (C) 2024 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}
28if (!defined('NOREQUIREMENU')) {
29 define('NOREQUIREMENU', '1');
30}
31if (!defined('NOREQUIREHTML')) {
32 define('NOREQUIREHTML', '1');
33}
34if (!defined('NOREQUIREAJAX')) {
35 define('NOREQUIREAJAX', '1');
36}
37if (!defined('NOREQUIRESOC')) {
38 define('NOREQUIRESOC', '1');
39}
40// If there is no need to load and show top and left menu
41if (!defined("NOLOGIN")) {
42 define("NOLOGIN", '1');
43}
44//if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN','1'); // Required to know date format for dol_print_date
45
46// Load Dolibarr environment
47require '../../main.inc.php';
48require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
49
58$action = GETPOST('action', 'aZ09');
59
60$module = GETPOST('module', 'aZ09arobase');
61$uploaddirname = dol_sanitizeFileName(GETPOST('uploaddirname', 'alpha'));
62
63$flowFilename = GETPOST('flowFilename', 'alpha');
64$flowIdentifier = GETPOST('flowIdentifier', 'alpha');
65$flowChunkNumber = GETPOST('flowChunkNumber', 'alpha');
66$flowChunkSize = GETPOST('flowChunkSize', 'alpha');
67$flowTotalSize = GETPOST('flowTotalSize', 'alpha');
68
69$result = restrictedArea($user, $module, 0, '', '', 'fk_soc', 'rowid', 0, 1); // Call with mode return
70
71if ($action != 'upload') {
72 httponly_accessforbidden("Param action must be 'upload'");
73}
74
75if (!empty($conf->$module->dir_temp)) {
76 $upload_dir = $conf->$module->dir_temp;
77 if (!empty($uploaddirname)) {
78 $upload_dir .= "/".$uploaddirname;
79 }
80} else {
81 httponly_accessforbidden("Param module does not has a dir_temp directory. Module does not exists or is not activated.");
82}
83
84/*
85 * Action
86 */
87
89
90$result = false;
91
92if (!empty($upload_dir)) {
93 $temp_dir = $upload_dir.'/'.$flowIdentifier;
94} else {
95 $temp_dir = DOL_DATA_ROOT.'/'.$module.'/temp/'.$flowIdentifier;
96 $upload_dir = DOL_DATA_ROOT.'/'.$module.'/temp/';
97}
98
99if ($module != "test" && !isModEnabled($module)) {
100 echo json_encode("The module ".$module." is not enabled");
101 header("HTTP/1.0 400");
102 die();
103}
104
105if ($_SERVER['REQUEST_METHOD'] === 'GET') {
106 $chunk_file = $temp_dir.'/'.$flowFilename.'.part'.$flowChunkNumber;
107 if (file_exists($chunk_file)) {
108 header("HTTP/1.0 200 Ok");
109 } else {
110 header("HTTP/1.0 404 Not Found");
111 }
112} else {
113 // loop through files and move the chunks to a temporarily created directory
114 if (file_exists($upload_dir.'/'.$flowFilename)) {
115 echo json_encode('File '.$flowIdentifier.' was already uploaded');
116 header("HTTP/1.0 200 Ok");
117 die();
118 } elseif (!empty($_FILES)) {
119 foreach ($_FILES as $file) {
120 // check the error status
121 if ($file['error'] != 0) {
122 dol_syslog('error '.$file['error'].' in file '.$flowFilename);
123 continue;
124 }
125
126 // init the destination file (format <filename.ext>.part<#chunk>
127 // the file is stored in a temporary directory
128 $dest_file = $temp_dir.'/'.$flowFilename.'.part'.$flowChunkNumber;
129
130 // create the temporary directory
131 if (!dol_is_dir($temp_dir)) {
132 dol_mkdir($temp_dir);
133 }
134
135 // move the temporary file
136 if (!dol_move_uploaded_file($file['tmp_name'], $dest_file, 0)) {
137 dol_syslog('Error saving (move_uploaded_file) chunk '.$flowChunkNumber.' for file '.$flowFilename);
138 } else {
139 // check if all the parts present, and create the final destination file
140 $result = createFileFromChunks($temp_dir, $upload_dir, $flowFilename, $flowChunkSize, $flowTotalSize);
141 }
142 }
143 }
144}
145if ($result) {
146 echo json_encode('File '.$flowIdentifier.' uploaded');
147} else {
148 echo json_encode('Error while uploading file '.$flowIdentifier);
149}
150
151
162function createFileFromChunks($temp_dir, $upload_dir, $fileName, $chunkSize, $totalSize)
163{
164 dol_syslog(__FUNCTION__, LOG_DEBUG);
165
166 // count all the parts of this file
167 $total_files = 0;
168 $files = dol_dir_list($temp_dir, 'files');
169 foreach ($files as $file) {
170 if (stripos($file["name"], $fileName) !== false) {
171 $total_files++;
172 }
173 }
174
175 // check that all the parts are present
176 // the size of the last part is between chunkSize and 2*$chunkSize
177 if ($total_files * (float) $chunkSize >= ((float) $totalSize - (float) $chunkSize + 1)) {
178 // create the final destination file
179 if (($fp = fopen($upload_dir.'/'.$fileName, 'w')) !== false) {
180 for ($i = 1; $i <= $total_files; $i++) {
181 fwrite($fp, file_get_contents($temp_dir.'/'.$fileName.'.part'.$i));
182 dol_syslog('writing chunk '.$i);
183 }
184 fclose($fp);
185 } else {
186 dol_syslog('cannot create the destination file');
187 return false;
188 }
189
190 // rename the temporary directory (to avoid access from other
191 // concurrent chunks uploads)
192 @rename($temp_dir, $temp_dir.'_UNUSED');
193 }
194
195 return true;
196}
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:63
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.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0)
Clean a string to use it as a file name.
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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
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, $mode=0)
Check permissions of a user to show a page and an object.