dolibarr  19.0.0-dev
flowjs-server.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2023 Laurent Destailleur <eldy@users.sourceforge.net>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <https://www.gnu.org/licenses/>.
16  */
17 
23 if (!defined('NOTOKENRENEWAL')) {
24  define('NOTOKENRENEWAL', '1'); // Disables token renewal
25 }
26 if (!defined('NOREQUIREMENU')) {
27  define('NOREQUIREMENU', '1');
28 }
29 if (!defined('NOREQUIREHTML')) {
30  define('NOREQUIREHTML', '1');
31 }
32 if (!defined('NOREQUIREAJAX')) {
33  define('NOREQUIREAJAX', '1');
34 }
35 if (!defined('NOREQUIRESOC')) {
36  define('NOREQUIRESOC', '1');
37 }
38 // If there is no need to load and show top and left menu
39 if (!defined("NOLOGIN")) {
40  define("NOLOGIN", '1');
41 }
42 //if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN','1'); // Required to know date format for dol_print_date
43 
44 // Load Dolibarr environment
45 require '../../main.inc.php';
46 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
47 
48 $action = GETPOST('action', 'aZ09');
49 
50 $module = GETPOST('module', 'aZ09arobase');
51 
52 $flowFilename = GETPOST('flowFilename', 'alpha');
53 $flowIdentifier = GETPOST('flowIdentifier', 'alpha');
54 $flowChunkNumber = GETPOST('flowChunkNumber', 'alpha');
55 $flowChunkSize = GETPOST('flowChunkSize', 'alpha');
56 $flowTotalSize = GETPOST('flowTotalSize', 'alpha');
57 
58 $result = restrictedArea($user, $module, 0, '', 0, 'fk_soc', 'rowid', 0, 1); // Call with mode return
59 
60 if ($action != 'upload') {
61  httponly_accessforbidden("Param action must be 'upload'");
62 }
63 
64 if (!empty($conf->$module->dir_temp)) {
65  $upload_dir = $conf->$module->dir_temp;
66 } else {
67  httponly_accessforbidden("Param module does not has a dir_temp directory. Module does not exists or is not activated.");
68 }
69 
70 /*
71  * Action
72  */
73 
74 top_httphead();
75 
76 dol_syslog(join(',', $_GET));
77 
78 $result = false;
79 
80 if (!empty($upload_dir)) {
81  $temp_dir = $upload_dir.'/'.$flowIdentifier;
82 } else {
83  $temp_dir = DOL_DATA_ROOT.'/'.$module.'/temp/'.$flowIdentifier;
84  $upload_dir = DOL_DATA_ROOT.'/'.$module.'/temp/';
85 }
86 
87 if ($module != "test" && !isModEnabled($module)) {
88  echo json_encode("The module ".$module." is not enabled");
89  header("HTTP/1.0 400");
90  die();
91 }
92 
93 if ($_SERVER['REQUEST_METHOD'] === 'GET') {
94  $chunk_file = $temp_dir.'/'.$flowFilename.'.part'.$flowChunkNumber;
95  if (file_exists($chunk_file)) {
96  header("HTTP/1.0 200 Ok");
97  } else {
98  header("HTTP/1.0 404 Not Found");
99  }
100 } else {
101  // loop through files and move the chunks to a temporarily created directory
102  if (file_exists($upload_dir.'/'.$flowFilename)) {
103  echo json_encode('File '.$flowIdentifier.' was already uploaded');
104  header("HTTP/1.0 200 Ok");
105  die();
106  } elseif (!empty($_FILES)) foreach ($_FILES as $file) {
107  // check the error status
108  if ($file['error'] != 0) {
109  dol_syslog('error '.$file['error'].' in file '.$flowFilename);
110  continue;
111  }
112 
113  // init the destination file (format <filename.ext>.part<#chunk>
114  // the file is stored in a temporary directory
115  $dest_file = $temp_dir.'/'.$flowFilename.'.part'.$flowChunkNumber;
116 
117  // create the temporary directory
118  if (!dol_is_dir($temp_dir)) {
119  dol_mkdir($temp_dir);
120  }
121 
122  // move the temporary file
123  if (!dol_move_uploaded_file($file['tmp_name'], $dest_file, 0)) {
124  dol_syslog('Error saving (move_uploaded_file) chunk '.$flowChunkNumber.' for file '.$flowFilename);
125  } else {
126  // check if all the parts present, and create the final destination file
127  $result = createFileFromChunks($temp_dir, $upload_dir, $flowFilename, $flowChunkSize, $flowTotalSize);
128  }
129  }
130 }
131 if ($result) {
132  echo json_encode('File '.$flowIdentifier.' uploaded');
133 } else {
134  echo json_encode('Error while uploading file '.$flowIdentifier);
135 }
136 
137 
148 function createFileFromChunks($temp_dir, $upload_dir, $fileName, $chunkSize, $totalSize)
149 {
150  dol_syslog(__METHOD__, LOG_DEBUG);
151 
152  // count all the parts of this file
153  $total_files = 0;
154  $files = dol_dir_list($temp_dir, 'files');
155  foreach ($files as $file) {
156  if (stripos($file["name"], $fileName) !== false) {
157  $total_files++;
158  }
159  }
160 
161  // check that all the parts are present
162  // the size of the last part is between chunkSize and 2*$chunkSize
163  if ($total_files * $chunkSize >= ($totalSize - $chunkSize + 1)) {
164  // create the final destination file
165  if (($fp = fopen($upload_dir.'/'.$fileName, 'w')) !== false) {
166  for ($i=1; $i<=$total_files; $i++) {
167  fwrite($fp, file_get_contents($temp_dir.'/'.$fileName.'.part'.$i));
168  dol_syslog('writing chunk '.$i);
169  }
170  fclose($fp);
171  } else {
172  dol_syslog('cannot create the destination file');
173  return false;
174  }
175 
176  // rename the temporary directory (to avoid access from other
177  // concurrent chunks uploads)
178  @rename($temp_dir, $temp_dir.'_UNUSED');
179  }
180 
181  return true;
182 }
dol_move_uploaded_file($src_file, $dest_file, $allowoverwrite, $disablevirusscan=0, $uploaderrorcode=0, $nohook=0, $varfiles='addedfile', $upload_dir='')
Make control on an uploaded file from an GUI page and move it to final destination.
Definition: files.lib.php:1196
dol_dir_list($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:62
dol_is_dir($folder)
Test if filename is a directory.
Definition: files.lib.php:453
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
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.
Definition: main.inc.php:1494
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.
httponly_accessforbidden($message=1, $http_response_code=403, $stringalreadysanitized=0)
Show a message to say access is forbidden and stop program.