dolibarr  18.0.0-alpha
flowjs-server.php
1 <?php
2 /* Copyright (C) 2012 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 $module = GETPOST('module', 'aZ09');
50 $upload_dir = GETPOST('upload_dir', 'alpha');
51 $flowFilename = GETPOST('flowFilename', 'alpha');
52 $flowIdentifier = GETPOST('flowIdentifier', 'alpha');
53 $flowChunkNumber = GETPOST('flowChunkNumber', 'alpha');
54 $flowChunkSize = GETPOST('flowChunkSize', 'alpha');
55 $flowTotalSize = GETPOST('flowTotalSize', 'alpha');
56 
57 /*
58  * Action
59  */
60 
61 
62 top_httphead();
63 dol_syslog(join(',', $_GET));
64 
65 $result = false;
66 
67 if (!empty($upload_dir)) {
68  $temp_dir = $upload_dir.'/'.$flowIdentifier;
69 } else {
70  $temp_dir = DOL_DATA_ROOT.'/'.$module.'/temp/'.$flowIdentifier;
71  $upload_dir = DOL_DATA_ROOT.'/'.$module.'/temp/';
72 }
73 
74 if ($module != "test" && !isModEnabled($module)) {
75  echo json_encode("The module ".$module." is not enabled");
76  header("HTTP/1.0 400");
77  die();
78 }
79 
80 if ($_SERVER['REQUEST_METHOD'] === 'GET') {
81  $chunk_file = $temp_dir.'/'.$flowFilename.'.part'.$flowChunkNumber;
82  if (file_exists($chunk_file)) {
83  header("HTTP/1.0 200 Ok");
84  } else {
85  header("HTTP/1.0 404 Not Found");
86  }
87 } else {
88  // loop through files and move the chunks to a temporarily created directory
89  if (file_exists($upload_dir.'/'.$flowFilename)) {
90  echo json_encode('File '.$flowIdentifier.' was already uploaded');
91  header("HTTP/1.0 200 Ok");
92  die();
93  } elseif (!empty($_FILES)) foreach ($_FILES as $file) {
94  // check the error status
95  if ($file['error'] != 0) {
96  dol_syslog('error '.$file['error'].' in file '.$flowFilename);
97  continue;
98  }
99 
100  // init the destination file (format <filename.ext>.part<#chunk>
101  // the file is stored in a temporary directory
102  $dest_file = $temp_dir.'/'.$flowFilename.'.part'.$flowChunkNumber;
103 
104  // create the temporary directory
105  if (!dol_is_dir($temp_dir)) {
106  dol_mkdir($temp_dir);
107  }
108 
109  // move the temporary file
110  if (!dol_move_uploaded_file($file['tmp_name'], $dest_file, 0)) {
111  dol_syslog('Error saving (move_uploaded_file) chunk '.$flowChunkNumber.' for file '.$flowFilename);
112  } else {
113  // check if all the parts present, and create the final destination file
114  $result = createFileFromChunks($temp_dir, $upload_dir, $flowFilename, $flowChunkSize, $flowTotalSize);
115  }
116  }
117 }
118 if ($result) {
119  echo json_encode('File '.$flowIdentifier.' uploaded');
120 } else {
121  echo json_encode('Error while uploading file '.$flowIdentifier);
122 }
123 
124 
135 function createFileFromChunks($temp_dir, $upload_dir, $fileName, $chunkSize, $totalSize)
136 {
137 
138  dol_syslog(__METHOD__, LOG_DEBUG);
139  // count all the parts of this file
140  $total_files = 0;
141  $files = dol_dir_list($temp_dir, 'files');
142  foreach ($files as $file) {
143  if (stripos($file["name"], $fileName) !== false) {
144  $total_files++;
145  }
146  }
147 
148  // check that all the parts are present
149  // the size of the last part is between chunkSize and 2*$chunkSize
150  if ($total_files * $chunkSize >= ($totalSize - $chunkSize + 1)) {
151  // create the final destination file
152  if (($fp = fopen($upload_dir.'/'.$fileName, 'w')) !== false) {
153  for ($i=1; $i<=$total_files; $i++) {
154  fwrite($fp, file_get_contents($temp_dir.'/'.$fileName.'.part'.$i));
155  dol_syslog('writing chunk '.$i);
156  }
157  fclose($fp);
158  } else {
159  dol_syslog('cannot create the destination file');
160  return false;
161  }
162 
163  // rename the temporary directory (to avoid access from other
164  // concurrent chunks uploads)
165  @rename($temp_dir, $temp_dir.'_UNUSED');
166  }
167  return true;
168 }
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:530
dol_dir_list
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:61
top_httphead
if(!defined('NOREQUIREMENU')) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
Definition: main.inc.php:1470
dol_move_uploaded_file
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:1124
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1639
isModEnabled
isModEnabled($module)
Is Dolibarr module enabled.
Definition: functions.lib.php:147
dol_is_dir
dol_is_dir($folder)
Test if filename is a directory.
Definition: files.lib.php:451
dol_mkdir
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
Definition: functions.lib.php:6811