*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
/**
* \file htdocs/ecm/file_card.php
* \ingroup ecm
* \brief Card of a file for ECM module
*/
// Load Dolibarr environment
require '../main.inc.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
require_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmdirectory.class.php';
require_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmfiles.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/ecm.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
// Load translation files required by page
$langs->loadLangs(array('ecm', 'companies', 'other', 'users', 'orders', 'propal', 'bills', 'contracts', 'categories'));
$action = GETPOST('action', 'aZ09');
$cancel = GETPOST('cancel', 'alpha');
$backtopage = GETPOST('backtopage', 'alpha');
// Get parameters
$socid = GETPOSTINT("socid");
// Security check
if ($user->socid > 0) {
$action = '';
$socid = $user->socid;
}
$limit = GETPOSTINT('limit') ? GETPOSTINT('limit') : $conf->liste_limit;
$sortfield = GETPOST('sortfield', 'aZ09comma');
$sortorder = GETPOST('sortorder', 'aZ09comma');
$page = GETPOSTISSET('pageplusone') ? (GETPOSTINT('pageplusone') - 1) : GETPOSTINT("page");
if (empty($page) || $page == -1) {
$page = 0;
} // If $page is not defined, or '' or -1
$offset = $limit * $page;
$pageprev = $page - 1;
$pagenext = $page + 1;
if (!$sortorder) {
$sortorder = "ASC";
}
if (!$sortfield) {
$sortfield = "label";
}
$section = GETPOST("section", 'alpha');
if (!$section) {
dol_print_error(null, 'Error, section parameter missing');
exit;
}
$urlfile = (string) dol_sanitizePathName(GETPOST("urlfile"), '_', 0);
if (!$urlfile) {
dol_print_error(null, "ErrorParamNotDefined");
exit;
}
// Load ecm object
$ecmdir = new EcmDirectory($db);
$result = $ecmdir->fetch(GETPOST("section", 'alpha'));
if (!($result > 0)) {
dol_print_error($db, $ecmdir->error);
exit;
}
$relativepath = $ecmdir->getRelativePath();
$upload_dir = $conf->ecm->dir_output.'/'.$relativepath;
$fullpath = $conf->ecm->dir_output.'/'.$relativepath.$urlfile;
$relativetodocument = 'ecm/'.$relativepath; // $relativepath is relative to ECM dir, we need relative to document
$filepath = $relativepath.$urlfile;
$filepathtodocument = $relativetodocument.$urlfile;
// Try to load object from index
$object = new EcmFiles($db);
$extrafields = new ExtraFields($db);
// fetch optionals attributes and labels
$extrafields->fetch_name_optionals_label($object->table_element);
$result = $object->fetch(0, '', $filepathtodocument);
if ($result < 0) {
dol_print_error($db, $object->error, $object->errors);
exit;
}
// Permissions
$permissiontoread = $user->hasRight('ecm', 'read');
$permissiontoadd = $user->hasRight('ecm', 'setup');
$permissiontoupload = $user->hasRight('ecm', 'upload');
if (!$permissiontoread) {
accessforbidden();
}
/*
* Actions
*/
if ($cancel) {
$action = '';
if ($backtopage) {
header("Location: ".$backtopage);
exit;
} else {
header('Location: '.$_SERVER["PHP_SELF"].'?urlfile='.urlencode($urlfile).'§ion='.urlencode($section).($module ? '&module='.urlencode($module) : ''));
exit;
}
}
// Rename file
if ($action == 'update' && $permissiontoadd) {
$error = 0;
$oldlabel = GETPOST('urlfile', 'alpha');
$newlabel = dol_sanitizeFileName(GETPOST('label', 'alpha'), '_', 0);
$shareenabled = GETPOST('shareenabled', 'alpha');
//$db->begin();
$olddir = $ecmdir->getRelativePath(0); // Relative to ecm
$olddirrelativetodocument = 'ecm/'.$olddir; // Relative to document
$newdirrelativetodocument = 'ecm/'.$olddir;
$olddir = $conf->ecm->dir_output.'/'.$olddir;
$newdir = $olddir;
$oldfile = $olddir.$oldlabel;
$newfile = $newdir.$newlabel;
$newfileformove = $newfile;
// If old file end with .noexe, new file must also end with .noexe
if (preg_match('/\.noexe$/', $oldfile) && !preg_match('/\.noexe$/', $newfileformove)) {
$newfileformove .= '.noexe';
}
//var_dump($oldfile);var_dump($newfile);exit;
// Now we update index of file
$db->begin();
//print $oldfile.' - '.$newfile;
if ($newlabel != $oldlabel) {
$result = dol_move($oldfile, $newfileformove); // This include update of database
if (!$result) {
$langs->load('errors');
setEventMessages($langs->trans('ErrorFailToRenameFile', $oldfile, $newfile), null, 'errors');
$error++;
}
// Reload object after the move
$result = $object->fetch(0, '', $newdirrelativetodocument.$newlabel);
if ($result < 0) {
dol_print_error($db, $object->error, $object->errors);
exit;
}
}
if (!$error) {
if ($shareenabled) {
require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
$object->share = getRandomPassword(true);
} else {
$object->share = '';
}
if ($object->id > 0) {
$ret = $extrafields->setOptionalsFromPost(null, $object);
if ($ret < 0) {
$error++;
}
if (!$error) {
// Actions on extra fields
$result = $object->insertExtraFields();
if ($result < 0) {
setEventMessages($object->error, $object->errors, 'errors');
$error++;
}
}
// Call update to set the share key
$result = $object->update($user);
if ($result < 0) {
setEventMessages($object->error, $object->errors, 'warnings');
}
} else {
// Call create to insert record
$object->entity = $conf->entity;
$object->filepath = preg_replace('/[\\/]+$/', '', $newdirrelativetodocument);
$object->filename = $newlabel;
$object->label = md5_file(dol_osencode($newfileformove)); // hash of file content
$object->fullpath_orig = '';
$object->gen_or_uploaded = 'unknown';
$object->description = ''; // indexed content
$object->keywords = ''; // keyword content
$result = $object->create($user);
if ($result < 0) {
setEventMessages($object->error, $object->errors, 'warnings');
}
}
}
if (!$error) {
$db->commit();
$urlfile = $newlabel;
// If old file end with .noexe, new file must also end with .noexe
if (preg_match('/\.noexe$/', $newfileformove)) {
$urlfile .= '.noexe';
}
header('Location: '.$_SERVER["PHP_SELF"].'?urlfile='.urlencode($urlfile).'§ion='.urlencode($section));
exit;
} else {
$db->rollback();
}
}
/*
* View
*/
$form = new Form($db);
llxHeader('', '', '', '', 0, 0, '', '', '', 'mod-ecm page-file_card');
$object->section_id = $ecmdir->id;
$object->label = $urlfile;
$head = ecm_file_prepare_head($object);
if ($action == 'edit') {
print '
';
}
// Confirm deletion of a file
if ($action == 'deletefile') {
print $form->formconfirm($_SERVER["PHP_SELF"].'?section='.urlencode($section), $langs->trans('DeleteFile'), $langs->trans('ConfirmDeleteFile', $urlfile), 'confirm_deletefile', '', 1, 1);
}
if ($action != 'edit') {
// Actions buttons
print '';
}
// End of page
llxFooter();
$db->close();