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