dolibarr 23.0.3
upload_page.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2005-2017 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024-2025 Frédéric France <frederic.france@free.fr>
4 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5 *
6 * This file is a modified version of datepicker.php from phpBSM to fix some
7 * bugs, to add new features and to dramatically increase speed.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
28require_once '../main.inc.php';
36require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
37require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
38
39
40if (GETPOST('lang', 'aZ09')) {
41 $langs->setDefaultLang(GETPOST('lang', 'aZ09')); // If language was forced on URL by the main.inc.php
42}
43
44$langs->loadLangs(array("main", "other"));
45
46$action = GETPOST('action', 'aZ09');
47$modulepart = GETPOST('modulepart', 'aZ09');
48
49$upload_dir = $conf->admin->dir_temp.'/import';
50
51// Delete the temporary files that are used when uploading files
52dol_delete_file($upload_dir.'/upload_page-by'.$user->id.'-*');
53
54$error = 0;
55
56
57/*
58 * Actions
59 */
60
61if (getDolGlobalString('MAIN_USE_TOP_MENU_IMPORT_FILE') && !is_numeric(getDolGlobalString('MAIN_USE_TOP_MENU_IMPORT_FILE'))) {
62 $urlforuploadpage = getDolGlobalString('MAIN_USE_TOP_MENU_IMPORT_FILE');
63
64 header("Location: ".$urlforuploadpage);
65 exit(1);
66}
67
68if ($action == 'uploadfile') { // Test on permission not required here. Done later
69 if (!$modulepart) { // Should not happen
70 print 'Error, modulepart param is empty';
71 exit(1);
72 }
73
74 $arrayobject = getElementProperties($modulepart);
75
76 $module = $arrayobject['module'];
77 $element = $arrayobject['element'];
78 $dir_output = $arrayobject['dir_output'];
79 $dir_temp = $arrayobject['dir_temp'];
80
81 $permlevel1 = 'read';
82 $permlevel2 = '';
83 $fileprefix = 'unknown';
84 if (in_array($modulepart, array('fournisseur', 'invoice_supplier'))) {
85 $permlevel1 = 'facture';
86 $permlevel2 = 'read';
87 $fileprefix = 'upload_page-by'.$user->id.'-'.$modulepart.'-'.(GETPOSTINT('socid') > 0 ? GETPOSTINT('socid') : 0).'-'.(GETPOSTINT('search_prodid') > 0 ? GETPOSTINT('search_prodid') : 0);
88 } elseif ($modulepart == 'expensereport') {
89 $fileprefix = 'upload_page-by'.$user->id.'-'.$modulepart.'-'.(GETPOSTINT('userexpensereportid') > 0 ? GETPOSTINT('userexpensereportid') : 0).'-'.(GETPOSTINT('search_prodid') > 0 ? GETPOSTINT('search_prodid') : 0);
90 } elseif ($modulepart == 'salaries') {
91 $fileprefix = 'upload_page-by'.$user->id.'-'.$modulepart.'-'.(GETPOSTINT('usersalaryid') > 0 ? GETPOSTINT('usersalaryid') : 0);
92 }
93
94 if ($permlevel2) {
95 $permissiontoadd = $user->hasRight($module, $permlevel1, $permlevel2); // Used by actions_linkedfiles
96 } else {
97 $permissiontoadd = $user->hasRight($module, $permlevel1); // Used by actions_linkedfiles
98 }
99 $forceFullTextIndexation = '0'; // Used by actions_linkedfiles
100
101
102 if (!empty($_FILES['userfile']['name'])) {
103 $_FILES['userfile']['name'] = $fileprefix.'-'.$_FILES['userfile']['name'];
104
105 include DOL_DOCUMENT_ROOT.'/core/actions_linkedfiles.inc.php';
106
107 // @phpstan-ignore-next-line $error may have been modified by actions_linkedfiles.inc.php
108 if (!$error) {
109 header("Location: ".DOL_URL_ROOT.'/core/upload_page2.php?file='.urlencode($fileprefix));
110 exit;
111 }
112 }
113}
114
115
116/*
117 * View
118 */
119
120$form = new Form($db);
121
122// Important: Following code is to avoid page request by browser and PHP CPU at each Dolibarr page access.
123/*
124if (empty($dolibarr_nocache) && GETPOSTINT('cache')) {
125 header('Cache-Control: max-age='.GETPOSTINT('cache').', public');
126 // For a .php, we must set an Expires to avoid to have it forced to an expired value by the web server
127 header('Expires: '.gmdate('D, d M Y H:i:s', dol_now('gmt') + GETPOSTINT('cache')).' GMT');
128 // HTTP/1.0
129 header('Pragma: token=public');
130} else {
131 // HTTP/1.0
132 header('Cache-Control: no-cache');
133}
134*/
135
136$title = $langs->trans("UploadFile");
137$help_url = '';
138
139$arrayofjs = array();
140$arrayofcss = array();
141
142llxHeader('', $title, $help_url, '', 0, 0, $arrayofjs, $arrayofcss, '', 'mod-upload page-card');
143
144print load_fiche_titre('', '', '', 0, '', '', '<h2>'.img_picto('', 'upload').' '.$title.'</h2>');
145
146
147// Instantiate hooks of thirdparty module
148$hookmanager->initHooks(array('uploadform'));
149
150// Define $uploadform
151$uploadform = '';
152
153
154$uploadform = '<div class="display-flex">';
155
156// Form to upload a supplier invoice
157if (isModEnabled('supplier_invoice')) {
158 $langs->load("bills");
159 $uploadform .= '
160 <div id="supplierinvoice" class="flex-item flex-item-uploadfile">'.img_picto('', 'bill', 'class="fa-2x"').'<br>
161 <div>'.$langs->trans("SupplierInvoice").'<br><br>';
162
163 $uploadform .= img_picto('', 'company', 'class="pictofixedwidth"');
164 $uploadform .= $form->select_company(GETPOSTINT('socid'), 'socid', '(statut:=:0)', $langs->transnoentitiesnoconv("Supplier"), 0, 0, array(), 0, 'maxwidth200 disableautoopen');
165
166 $uploadform .= '<br>';
167
168 $prodid = GETPOSTINT('prodid');
169 $prodtext = $langs->trans("RefOrLabel");
170
171 //$uploadform .= $form->select_produits_fournisseurs(0, $prodid, 'prodid', '', 0, 0, 1, 2, $prodtext, 0, array(), GETPOSTINT('socid'), '1', 0, 'maxwidth200 disableautoopen', 0, '', null, 1);
172 $uploadform .= img_picto('', 'product', 'class="pictofixedwidth"');
173 $uploadform .= $form->select_produits_fournisseurs(0, $prodid, 'prodid', '', '', array(), 1, 1, 'maxwidth200 disableautoopen', $prodtext, 1);
174
175 $uploadform .= '<br>';
176
177 $uploadform .= '<br>
178 <small class="opacitymedium">'.$langs->trans("OrClickToSelectAFile").'...</small>
179 </div>
180 </div>';
181}
182
183// Form to upload an expense report
184if (isModEnabled('expensereport')) {
185 $langs->load("expensereport");
186 $uploadform .= '
187 <div id="userexpensereport" class="flex-item flex-item-uploadfile">'.img_picto('', 'expensereport', 'class="fa-2x"').'<br>
188 <div>'.$langs->trans("ExpenseReport").'<br><br>';
189
190 $uploadform .= img_picto('', 'user', 'class="pictofixedwidth"');
191 //$uploadform .= '<span class="disableautoopen">';
192 $uploadform .= $form->select_dolusers(GETPOSTINT('userexpensereportid') > 0 ? GETPOSTINT('userexpensereportid') : $user->id, 'userexpensereportid', $langs->transnoentitiesnoconv("User"), null, 0, 'hierarchyme', '', '', 0, 0, '', 0, '', 'maxwidth200 disableautoopen', 1);
193 //$uploadform .= '</span>';
194
195 $uploadform .= '<br>';
196
197 $uploadform .= '<br>
198 <small class="opacitymedium">'.$langs->trans("OrClickToSelectAFile").'...</small>
199 </div>
200 </div>';
201}
202
203
204// Form to upload a salary document
205if (isModEnabled('salaries')) {
206 $langs->load("salaries");
207 $uploadform .= '
208 <div id="userpayroll" class="flex-item flex-item-uploadfile">'.img_picto('', 'salary', 'class="fa-2x"').'<br>
209 <div>'.$langs->trans("UserPaySlip").'<br><br>';
210
211
212 $uploadform .= img_picto('', 'user', 'class="pictofixedwidth"');
213 //$uploadform .= '<span class="disableautoopen">';
214 $uploadform .= $form->select_dolusers(GETPOSTINT('usersalaryid') > 0 ? GETPOSTINT('usersalaryid') : $user->id, 'usersalaryid', $langs->transnoentitiesnoconv("Employee"), null, 0, 'hierarchyme', '', '', 0, 0, '', 0, '', 'maxwidth200 disableautoopen', 1);
215 //$uploadform .= '</span>';
216
217 $uploadform .= '<br>';
218
219 $uploadform .= '<br>
220 <small class="opacitymedium">'.$langs->trans("OrClickToSelectAFile").'...</small>
221 </div>
222 </div>';
223}
224
225
226
227$uploadform .= '</div>';
228
229
230// Execute hook printSearchForm
231$parameters = array('uploadform' => $uploadform);
232$reshook = $hookmanager->executeHooks('printUploadForm', $parameters); // Note that $action and $object may have been modified by some hooks
233if (empty($reshook)) {
234 $uploadform .= $hookmanager->resPrint;
235} else {
236 $uploadform = $hookmanager->resPrint;
237}
238
239$uploadform .= '<br>';
240
241
242// Show all forms
243print "\n";
244print "<!-- Begin UploadForm -->\n";
245print '<form id="uploadform" enctype="multipart/form-data" method="POST" action="'.dolBuildUrl($_SERVER["PHP_SELF"]).'">';
246print '<input type="hidden" name="token" value="'.newToken().'">';
247print '<input type="hidden" name="action" value="uploadfile">';
248print '<input type="hidden" name="sendit" value="1">';
249print '<input type="hidden" name="modulepart" id="modulepart" value="">';
250print '<input type="hidden" name="overwritefile" value="1">';
251
252print '<div class="center"><div class="center" style="padding: 10px;">';
253print '<style>.menu_titre { padding-top: 7px; }</style>';
254print '<div id="blockupload" class="center">'."\n";
255//print '<input name="filenamePDF" id="filenamePDF" type="hideobject">';
256print $uploadform;
257
258
259$accept = '.pdf,image/*';
260$disablemulti = 1;
261$perm = 1;
262$capture = 1;
263
264$maxfilesizearray = getMaxFileSizeArray();
265$max = $maxfilesizearray['max'];
266$maxmin = $maxfilesizearray['maxmin'];
267$maxphptoshow = $maxfilesizearray['maxphptoshow'];
268$maxphptoshowparam = $maxfilesizearray['maxphptoshowparam'];
269$out = '';
270if ($maxmin > 0) {
271 $out .= '<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxmin * 1024).'">'; // MAX_FILE_SIZE must precede the field type=file
272}
273$out .= '<input class="hideobject" type="file" id="fileInput" value=""';
274// @phpstan-ignore-next-line
275$out .= ((getDolGlobalString('MAIN_DISABLE_MULTIPLE_FILEUPLOAD') || $disablemulti) ? ' name="userfile"' : ' name="userfile[]" multiple');
276// @phpstan-ignore-next-line
277$out .= (!getDolGlobalString('MAIN_UPLOAD_DOC') || empty($perm) ? ' disabled' : '');
278// @phpstan-ignore-next-line
279$out .= (!empty($accept) ? ' accept="'.$accept.'"' : ' accept=""');
280// @phpstan-ignore-next-line
281$out .= (!empty($capture) ? ' capture="capture"' : '');
282$out .= '>';
283
284print $out;
285
286
287print "<script>
288$(document).ready(function() {
289 jQuery('#supplierinvoice:not(.disableautoopen)').on('click', function(event) {
290 console.log('Click on link supplierinvoice to open input file');
291 console.log(event);
292 if (!event.target.closest('.disableautoopen')) {
293 $('#modulepart').val('invoice_supplier');
294 $('#fileInput').click();
295 }
296 });
297
298 jQuery('#userexpensereport:not(.disableautoopen)').on('click', function(event) {
299 console.log('Click on link userexpensereport to open input file');
300 console.log(event);
301 if (!event.target.closest('.disableautoopen')) {
302 $('#modulepart').val('expensereport');
303 $('#fileInput').click();
304 }
305 });
306
307 jQuery('#userpayroll:not(.disableautoopen)').on('click', function(event) {
308 console.log('Click on link userpayroll to open input file');
309 console.log(event);
310 if (!event.target.closest('.disableautoopen')) {
311 $('#modulepart').val('salaries');
312 $('#fileInput').click();
313 }
314 });
315
316 jQuery('#fileInput').on('change', function(event) {
317 console.log(event);
318 console.log('A file was selected, we submit the form');
319 $('#uploadform').submit();
320 });
321});
322</script>";
323
324print '</div>'."\n";
325print '</div></div>';
326
327print '</form>';
328print "\n<!-- End UploadForm -->\n";
329
330
331
332// End of page
333llxFooter();
334$db->close();
llxFooter($comment='', $zone='private', $disabledoutputofmessages=0)
Empty footer.
Definition wrapper.php:91
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:73
Class to manage generation of HTML components Only common components must be here.
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.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (generic function)
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
getElementProperties($elementType)
Get an array with properties of an element.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='', $morecssonpicto='widthpictotitle')
Load a title with picto.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
getMaxFileSizeArray()
Return the max allowed for file upload.