dolibarr  9.0.0
file_card.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2008-2017 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 <http://www.gnu.org/licenses/>.
16  */
17 
24 require '../main.inc.php';
25 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
26 require_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmdirectory.class.php';
27 require_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmfiles.class.php';
28 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
29 require_once DOL_DOCUMENT_ROOT.'/core/lib/ecm.lib.php';
30 
31 // Load translation files required by page
32 $langs->loadLangs(array('ecm', 'companies', 'other', 'users', 'orders', 'propal', 'bills', 'contracts', 'categories'));
33 
34 $action = GETPOST('action', 'aZ09');
35 $cancel = GETPOST('cancel', 'alpha');
36 
37 if (!$user->rights->ecm->setup) accessforbidden();
38 
39 // Get parameters
40 $socid = GETPOST("socid","int");
41 
42 // Security check
43 if ($user->societe_id > 0)
44 {
45  $action = '';
46  $socid = $user->societe_id;
47 }
48 
49 $sortfield = GETPOST("sortfield",'alpha');
50 $sortorder = GETPOST("sortorder",'alpha');
51 $page = GETPOST("page",'int');
52 if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1
53 $offset = $conf->liste_limit * $page;
54 $pageprev = $page - 1;
55 $pagenext = $page + 1;
56 if (! $sortorder) $sortorder="ASC";
57 if (! $sortfield) $sortfield="label";
58 
59 $section=GETPOST("section",'alpha');
60 if (! $section)
61 {
62  dol_print_error('','Error, section parameter missing');
63  exit;
64 }
65 $urlfile=GETPOST("urlfile");
66 if (! $urlfile)
67 {
68  dol_print_error('',"ErrorParamNotDefined");
69  exit;
70 }
71 
72 // Load ecm object
73 $ecmdir = new EcmDirectory($db);
74 $result=$ecmdir->fetch(GETPOST("section",'alpha'));
75 if (! $result > 0)
76 {
77  dol_print_error($db,$ecmdir->error);
78  exit;
79 }
80 $relativepath=$ecmdir->getRelativePath();
81 $upload_dir = $conf->ecm->dir_output.'/'.$relativepath;
82 
83 $fullpath=$conf->ecm->dir_output.'/'.$relativepath.$urlfile;
84 
85 $file = new stdClass();
86 $file->section_id=$ecmdir->id;
87 $file->label=$urlfile;
88 
89 $relativetodocument = 'ecm/'.$relativepath; // $relativepath is relative to ECM dir, we need relative to document
90 $filepath=$relativepath.$file->label;
91 $filepathtodocument=$relativetodocument.$file->label;
92 
93 // Try to load object from index
94 $object = new ECMFiles($db);
95 $result=$object->fetch(0, '', $filepathtodocument);
96 if ($result < 0)
97 {
98  dol_print_error($db, $object->error, $object->errors);
99  exit;
100 }
101 
102 
103 
104 /*
105  * Actions
106  */
107 
108 if ($cancel)
109 {
110  $action ='';
111  if ($backtopage)
112  {
113  header("Location: ".$backtopage);
114  exit;
115  }
116  else
117  {
118  header('Location: '.$_SERVER["PHP_SELF"].'?urlfile='.urlencode($urlfile).'&section='.urlencode($section).($module?'&module='.urlencode($module):''));
119  exit;
120  }
121 }
122 
123 // Rename file
124 if ($action == 'update')
125 {
126  $error=0;
127 
128  $oldlabel=GETPOST('urlfile', 'alpha');
129  $newlabel=GETPOST('label', 'alpha');
130  $shareenabled = GETPOST('shareenabled', 'alpha');
131 
132  //$db->begin();
133 
134  $olddir=$ecmdir->getRelativePath(0); // Relative to ecm
135  $olddirrelativetodocument = 'ecm/'.$olddir; // Relative to document
136  $newdirrelativetodocument = 'ecm/'.$olddir;
137  $olddir=$conf->ecm->dir_output.'/'.$olddir;
138  $newdir=$olddir;
139 
140  $oldfile=$olddir.$oldlabel;
141  $newfile=$newdir.$newlabel;
142 
143  // Now we update index of file
144  $db->begin();
145 
146  //print $oldfile.' - '.$newfile;
147  if ($newlabel != $oldlabel)
148  {
149  $result=dol_move($oldfile, $newfile); // This include update of database
150  if (! $result)
151  {
152  $langs->load('errors');
153  setEventMessages($langs->trans('ErrorFailToRenameFile',$oldfile,$newfile), null, 'errors');
154  $error++;
155  }
156 
157  // Reload object after the move
158  $result=$object->fetch(0, '', $newdirrelativetodocument.$newlabel);
159  if ($result < 0)
160  {
161  dol_print_error($db, $object->error, $object->errors);
162  exit;
163  }
164  }
165 
166  if (! $error)
167  {
168  if ($shareenabled)
169  {
170  require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
171  $object->share = getRandomPassword(true);
172  }
173  else
174  {
175  $object->share = '';
176  }
177 
178  if ($object->id > 0)
179  {
180  // Call update to set the share key
181  $result = $object->update($user);
182  if ($result < 0)
183  {
184  setEventMessages($object->error, $object->errors, 'warnings');
185  }
186  }
187  else
188  {
189  // Call create to insert record
190  $object->entity = $conf->entity;
191  $object->filepath = preg_replace('/[\\/]+$/', '', $newdirrelativetodocument);
192  $object->filename = $newlabel;
193  $object->label = md5_file(dol_osencode($newfile)); // hash of file content
194  $object->fullpath_orig = '';
195  $object->gen_or_uploaded = 'unknown';
196  $object->description = ''; // indexed content
197  $object->keyword = ''; // keyword content
198  $result = $object->create($user);
199  if ($result < 0)
200  {
201  setEventMessages($object->error, $object->errors, 'warnings');
202  }
203  }
204  }
205 
206  if (!$error)
207  {
208  $db->commit();
209 
210  $urlfile=$newlabel;
211  header('Location: '.$_SERVER["PHP_SELF"].'?urlfile='.urlencode($urlfile).'&section='.urlencode($section));
212  exit;
213  }
214  else
215  {
216  $db->rollback();
217  }
218 }
219 
220 
221 
222 /*
223  * View
224  */
225 
226 $form=new Form($db);
227 
228 llxHeader();
229 
230 $head = ecm_file_prepare_head($file);
231 
232 if ($action == 'edit')
233 {
234  print '<form name="update" action="'.$_SERVER["PHP_SELF"].'" method="POST">';
235  print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
236  print '<input type="hidden" name="section" value="'.$section.'">';
237  print '<input type="hidden" name="urlfile" value="'.$urlfile.'">';
238  print '<input type="hidden" name="module" value="'.$module.'">';
239  print '<input type="hidden" name="action" value="update">';
240  print '<input type="hidden" name="id" value="'.$object->id.'">';
241 }
242 
243 dol_fiche_head($head, 'card', $langs->trans("File"), -1, 'generic');
244 
245 
246 $s='';
247 $tmpecmdir=new EcmDirectory($db); // Need to create a new one
248 $tmpecmdir->fetch($ecmdir->id);
249 $result = 1;
250 $i=0;
251 while ($tmpecmdir && $result > 0)
252 {
253  $tmpecmdir->ref=$tmpecmdir->label;
254  $s=$tmpecmdir->getNomUrl(1).$s;
255  if ($tmpecmdir->fk_parent)
256  {
257  $s=' -> '.$s;
258  $result=$tmpecmdir->fetch($tmpecmdir->fk_parent);
259  }
260  else
261  {
262  $tmpecmdir=0;
263  }
264  $i++;
265 }
266 
267 $s = img_picto('','object_dir').' <a href="'.DOL_URL_ROOT.'/ecm/index.php">'.$langs->trans("ECMRoot").'</a> -> '.$s.' -> ';
268 if ($action == 'edit') $s .= '<input type="text" name="label" class="quatrevingtpercent" value="'.$urlfile.'">';
269 else $s .= $urlfile;
270 
271 $object->ref=''; // Force to hide ref
272 dol_banner_tab($object, '', $morehtml, 0, '', '', $s);
273 
274 print '<div class="fichecenter">';
275 
276 print '<div class="underbanner clearboth"></div>';
277 print '<table class="border" width="100%">';
278 print '<tr><td class="titlefield">'.$langs->trans("ECMCreationDate").'</td><td>';
279 print dol_print_date(dol_filemtime($fullpath),'dayhour');
280 print '</td></tr>';
281 /*print '<tr><td>'.$langs->trans("ECMDirectoryForFiles").'</td><td>';
282 print '/ecm/'.$relativepath;
283 print '</td></tr>';
284 print '<tr><td>'.$langs->trans("ECMNbOfDocs").'</td><td>';
285 print count($filearray);
286 print '</td></tr>';
287 print '<tr><td>'.$langs->trans("TotalSizeOfAttachedFiles").'</td><td>';
288 print dol_print_size($totalsize);
289 print '</td></tr>';
290 */
291 
292 print '<tr><td>'.$langs->trans("HashOfFileContent").'</td><td>';
293 $object = new EcmFiles($db);
294 //$filenametosearch=basename($filepath);
295 //$filedirtosearch=basedir($filepath);
296 $object->fetch(0, '', $filepathtodocument);
297 if (! empty($object->label))
298 {
299  print $object->label;
300 }
301 else
302 {
303  print img_warning().' '.$langs->trans("FileNotYetIndexedInDatabase");
304 }
305 print '</td></tr>';
306 
307 // Define $urlwithroot
308 $urlwithouturlroot=preg_replace('/'.preg_quote(DOL_URL_ROOT,'/').'$/i','',trim($dolibarr_main_url_root));
309 $urlwithroot=$urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
310 //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
311 
312 // Link for internal download
313 print '<tr><td>'.$langs->trans("DirectDownloadInternalLink").'</td><td>';
314 $modulepart='ecm';
315 $forcedownload=1;
316 $rellink='/document.php?modulepart='.$modulepart;
317 if ($forcedownload) $rellink.='&attachment=1';
318 if (! empty($object->entity)) $rellink.='&entity='.$object->entity;
319 $rellink.='&file='.urlencode($filepath);
320 $fulllink=$urlwithroot.$rellink;
321 print img_picto('','object_globe.png').' ';
322 if ($action != 'edit') print '<input type="text" class="quatrevingtpercent" id="downloadinternallink" name="downloadinternellink" value="'.dol_escape_htmltag($fulllink).'">';
323 else print $fulllink;
324 if ($action != 'edit') print ' <a href="'.$fulllink.'">'.$langs->trans("Download").'</a>'; // No target here.
325 print '</td></tr>';
326 
327 // Link for direct external download
328 print '<tr><td>';
329 if ($action != 'edit') print $langs->trans("DirectDownloadLink");
330 else print $langs->trans("FileSharedViaALink");
331 print '</td><td>';
332 if (! empty($object->share))
333 {
334  if ($action != 'edit')
335  {
336  $forcedownload=0;
337 
338  $paramlink='';
339  if (! empty($object->share)) $paramlink.=($paramlink?'&':'').'hashp='.$object->share; // Hash for public share
340  if ($forcedownload) $paramlink.=($paramlink?'&':'').'attachment=1';
341 
342  $fulllink=$urlwithroot.'/document.php'.($paramlink?'?'.$paramlink:'');
343  //if (! empty($object->ref)) $fulllink.='&hashn='.$object->ref; // Hash of file path
344  //elseif (! empty($object->label)) $fulllink.='&hashc='.$object->label; // Hash of file content
345 
346  print img_picto('','object_globe.png').' ';
347  if ($action != 'edit') print '<input type="text" class="quatrevingtpercent" id="downloadlink" name="downloadexternallink" value="'.dol_escape_htmltag($fulllink).'">';
348  else print $fulllink;
349  if ($action != 'edit') print ' <a href="'.$fulllink.'">'.$langs->trans("Download").'</a>'; // No target here
350  }
351  else
352  {
353  print '<input type="checkbox" name="shareenabled"'.($object->share?' checked="checked"':'').' /> ';
354  }
355 }
356 else
357 {
358  if ($action != 'edit')
359  {
360  print '<span class="opacitymedium">'.$langs->trans("FileNotShared").'</span>';
361  }
362  else
363  {
364  print '<input type="checkbox" name="shareenabled"'.($object->share?' checked="checked"':'').' /> ';
365  }
366 }
367 print '</td></tr>';
368 
369 print '</table>';
370 print '</div>';
371 
372 print ajax_autoselect('downloadinternallink');
373 print ajax_autoselect('downloadlink');
374 
375 dol_fiche_end();
376 
377 if ($action == 'edit')
378 {
379  print '<div class="center">';
380  print '<input type="submit" class="button" name="submit" value="'.$langs->trans("Save").'">';
381  print ' &nbsp; &nbsp; ';
382  print '<input type="submit" class="button" name="cancel" value="'.$langs->trans("Cancel").'">';
383  print '</div>';
384 
385  print '</form>';
386 }
387 
388 
389 // Confirmation de la suppression d'une ligne categorie
390 if ($action == 'delete_file')
391 {
392  print $form->formconfirm($_SERVER["PHP_SELF"].'?section='.urlencode($section), $langs->trans('DeleteFile'), $langs->trans('ConfirmDeleteFile',$urlfile), 'confirm_deletefile', '', 1, 1);
393 }
394 
395 if ($action != 'edit')
396 {
397  // Actions buttons
398  print '<div class="tabsAction">';
399 
400  if ($user->rights->ecm->setup)
401  {
402  print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?action=edit&section='.urlencode($section).'&urlfile='.urlencode($urlfile).'">'.$langs->trans('Edit').'</a>';
403 
404  //print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?action=cancel&section='.$section.'&urlfile='.urlencode($urlfile).'&backtopage='.urlencode($backtourl).'">'.$langs->trans('Cancel').'</a>';
405  }
406 /*
407  if ($user->rights->ecm->setup)
408  {
409  print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?action=delete_file&section='.$section.'&urlfile='.urlencode($urlfile).'">'.$langs->trans('Delete').'</a>';
410  }
411  else
412  {
413  print '<a class="butActionRefused" href="#" title="'.$langs->trans("NotAllowed").'">'.$langs->trans('Delete').'</a>';
414  }
415 */
416  print '</div>';
417 }
418 
419 
420 // End of page
421 llxFooter();
422 $db->close();
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
img_warning($titlealt='default', $moreatt='')
Show warning logo.
llxFooter()
Empty footer.
Definition: wrapper.php:56
GETPOST($paramname, $check='none', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
print
Draft customers invoices.
Definition: index.php:91
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
dol_banner_tab($object, $paramid, $morehtml='', $shownav=1, $fieldid='rowid', $fieldref='ref', $morehtmlref='', $moreparam='', $nodbprefix=0, $morehtmlleft='', $morehtmlstatus='', $onlybanner=0, $morehtmlright='')
Show tab footer of a card.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
dol_move($srcfile, $destfile, $newmask=0, $overwriteifexists=1, $testvirus=0, $indexdatabase=1)
Move a file into another name.
Definition: files.lib.php:814
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
Class to manage generation of HTML components Only common components must be here.
dol_fiche_end($notab=0)
Show tab footer of a card.
llxHeader()
Empty header.
Definition: wrapper.php:44
if(GETPOST('cancel', 'alpha')) if(! GETPOST( 'confirmmassaction', 'alpha') &&$massaction !='presend' &&$massaction !='confirm_presend')
Draft customers invoices.
Definition: list.php:156
ecm_file_prepare_head($object)
Prepare array with list of tabs.
Definition: ecm.lib.php:105
getRandomPassword($generic=false)
Return a generated password using default module.
dol_print_date($time, $format='', $tzoutput='tzserver', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
ajax_autoselect($htmlname, $addlink='')
Make content of an input box selected when we click into input field.
dol_filemtime($pathoffile)
Return time of a file.
Definition: files.lib.php:564
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='')
Show picto whatever it&#39;s its name (generic function)
Class to manage ECM directories.
Class to manage ECM files.
dol_fiche_head($links=array(), $active='0', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='')
Show tab header of a card.