Newer
Older
* Copyright 2008-2011 Maarch
*
* Maarch Framework 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.
*
* Maarch Framework 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 Maarch Framework. If not, see <http://www.gnu.org/licenses/>.
* @brief Contains the docservers_controler Object
* (herits of the BaseObject class)

SNA
committed
*
* @file
* @author Luc KEULEYAN - BULL
* @author Laurent Giovannoni
* @date $date$
* @version $Revision$
* @ingroup core
*/
require_once 'core/class/class_request.php';
require_once 'core/class/docservers.php';
require_once 'core/docservers_tools.php';
require_once 'core/core_tables.php';
require_once 'core/class/ObjectControlerAbstract.php';
require_once 'core/class/ObjectControlerIF.php';
require_once 'core/class/class_security.php';
require_once 'core/class/class_resource.php';
require_once 'core/class/class_history.php';
functions::xecho($e->getMessage()) . ' // ';
}
/**
* Class for controling docservers objects from database
*/
class docservers_controler
extends ObjectControler
implements ObjectControlerIF
{

SNA
committed
/**
* Save given object in database:
* - make an update if object already exists,
* - make an insert if new object.
* Return updated object.
* @param docservers $docservers

SNA
committed
*/

SNA
committed
//var_dump($docserver);
$control = array();
'value' => '',
'error' => _DOCSERVER_EMPTY,
);

SNA
committed
return $control;
}
'docserver_id',
'docserver_type_id',
'coll_id',
if ($mode == 'up') {
$control = $this->control($docserver, 'up');
if ($control['status'] == 'ok') {

SNA
committed
//Update existing docserver
$this->createPackageInformation($docserver);

SNA
committed
//history
if ($_SESSION['history']['docserversadd'] == 'true') {

SNA
committed
$history = new history();
_DOCSERVERS_TABLE_NAME,
$docserver->docserver_id,
_DOCSERVER_UPDATED . ' : '
. $docserver->docserver_id,

SNA
committed
}
} else {

SNA
committed
}
return $control;
}
} else {
$control = $this->control($docserver, 'add');
if ($control['status'] == 'ok') {

SNA
committed
//Insert new docserver
$this->createPackageInformation($docserver);

SNA
committed
//history
if ($_SESSION['history']['docserversadd'] == 'true') {

SNA
committed
$history = new history();
_DOCSERVERS_TABLE_NAME,
$docserver->docserver_id,
_DOCSERVER_ADDED . ' : ' . $docserver->docserver_id,
$_SESSION['config']['databasetype']
);

SNA
committed
}
} else {

SNA
committed
}
}
}
return $control;
}
/**
* control the docserver object before action
*
* @param $docserver docserver object
* @return array ok if the object is well formated, ko otherwise
*/

SNA
committed
$f = new functions();

SNA
committed
// Update, so values exist
$docserver->docserver_id,
'nick',
_DOCSERVER_ID . ' ',
} else {

SNA
committed
}
$docserver->docserver_type_id,
'no',
_DOCSERVER_TYPES . ' ',
'yes',
0,
$docserver->device_label,
'no',
_DEVICE_LABEL . ' ',
'yes',
0,
if ($docserver->is_readonly == '') {
$docserver->is_readonly = 'false';

SNA
committed
}
$docserver->is_readonly,
'no',
_IS_READONLY . ' ',
'yes',
0,

SNA
committed
$docserver->is_readonly = false;
} else {
$docserver->is_readonly = true;
}
$docserver->size_limit_number,
'no',
_SIZE_LIMIT . ' ',
'yes',
0,

SNA
committed
}
if ($this->actualSizeNumberControl($docserver)) {
$error .= _SIZE_LIMIT_LESS_THAN_ACTUAL_SIZE . '#';

SNA
committed
}
$docserver->path_template,
'no',
_PATH_TEMPLATE . ' ',
'yes',
0,
if (!is_dir($docserver->path_template)) {
$error .= _PATH_OF_DOCSERVER_UNAPPROACHABLE . '#';

SNA
committed
} else {
// $Fnm = $docserver->path_template . 'test_docserver.txt';
|| !is_readable($docserver->path_template)
) {
$error .= _THE_DOCSERVER_DOES_NOT_HAVE_THE_ADEQUATE_RIGHTS;
}
}
$docserver->coll_id,
'no',
_COLLECTION . ' ',
'yes',
0,
$docserver->docserver_location_id,
'no',
_DOCSERVER_LOCATIONS . ' ',
'yes',
0,
$docserver->adr_priority_number,
'num',
_ADR_PRIORITY . ' ',
'yes',
0,
&& $this->docserversExists($docserver->docserver_id)
) {
$error .= $docserver->docserver_id . ' ' . _ALREADY_EXISTS . '#';

SNA
committed
}
if (!$this->adrPriorityNumberControl($docserver)) {
$error .= _PRIORITY . ' ' . $docserver->adr_priority_number . ' '
. _ALREADY_EXISTS_FOR_THIS_TYPE_OF_DOCSERVER . '#';

SNA
committed
}
if (!$this->priorityNumberControl($docserver)) {
$error .= _ADR_PRIORITY . $docserver->priority_number . ' '
. _ALREADY_EXISTS_FOR_THIS_TYPE_OF_DOCSERVER . '#';

SNA
committed
}
$error .= $_SESSION['error'];
//TODO:rewrite wash to return errors without html

SNA
committed
$return = array();
'status' => 'ko',
'value' => $docserver->docserver_id,

SNA
committed
} else {

SNA
committed
}
return $return;
}
* method to create package information file on the root of the docserver
* @param $docserver docserver object
*/
private function createPackageInformation($docserver)
&& is_readable($docserver->path_template)
) {
require_once('core' . DIRECTORY_SEPARATOR . 'class'
. DIRECTORY_SEPARATOR . 'docserver_types_controler.php');
$docserverTypeControler = new docserver_types_controler();
$docserverTypeObject = $docserverTypeControler->get(
$docserver->docserver_type_id
);
$Fnm = $docserver->path_template . DIRECTORY_SEPARATOR
if (file_exists($Fnm)) {
unlink($Fnm);
}
. $docserverTypeObject->docserver_type_id . '\r\n'
);
fwrite(
. $docserverTypeObject->docserver_type_label . '\r\n'
);
fwrite(
$inF,
_IS_CONTAINER . ' : ' . $docserverTypeObject->is_container
. $docserverTypeObject->container_max_number . '\r\n'
);
fwrite(
$inF,
_IS_COMPRESSED . ' : ' . $docserverTypeObject->is_compressed
. $docserverTypeObject->compression_mode . '\r\n'
);
fwrite(
_IS_META . ' : ' . $docserverTypeObject->is_meta . '\r\n'
);
fwrite(
$inF,
_META_TEMPLATE . ' : ' . $docserverTypeObject->meta_template
_IS_LOGGED . ' : ' . $docserverTypeObject->is_logged . '\r\n'
);
fwrite(
$inF,
_LOG_TEMPLATE . ' : ' . $docserverTypeObject->log_template
_IS_SIGNED . ' : ' . $docserverTypeObject->is_signed . '\r\n'
);
fwrite(
. $docserverTypeObject->fingerprint_mode . '\r\n'
);
fclose($inF);
}
}

SNA
committed
/**
* Inserts in the database (docservers table) a docserver object
*
* @param $docserver docserver object
* @return bool true if the insertion is complete, false otherwise
*/

SNA
committed
//Giving automatised values

SNA
committed
//Inserting object

SNA
committed
return $result;
}
/**
* Updates in the database (docserver table) a docservers object
*
* @param $docserver docserver object
* @return bool true if the update is complete, false otherwise
*/

SNA
committed
}
/**
* Get docservers with given id.
* Can return null if no corresponding object.
* @param $id Id of docservers to get
* @return docservers
*/

SNA
committed
//var_dump($docserver_id);
$this->set_foolish_ids(array('docserver_id'));
$this->set_specific_id('docserver_id');
$docserver = $this->advanced_get($docserver_id, _DOCSERVERS_TABLE_NAME);

SNA
committed
//var_dump($docserver);

SNA
committed
return null;
} else {
//var_dump($docserver);
return $docserver;
}
}
/**
* get docservers with given id for a ws.
* Can return null if no corresponding object.
* @param $docserver_id of docservers to send
* @return docservers
*/
$this->set_foolish_ids(array('docserver_id'));
$this->set_specific_id('docserver_id');
$docserver = $this->advanced_get($docserver_id, _DOCSERVERS_TABLE_NAME);

SNA
committed
return null;
} else {
$docserver = $docserver->getArray();
return $docserver;
}
}
/**
* Delete given docserver from database.
* @param docservers $docservers
*/
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
if ($docserver->docserver_id <> 'TEMPLATES') {
$func = new functions();
$control = array();
if (!isset($docserver) || empty($docserver)) {
$control = array(
'status' => 'ko',
'value' => '',
'error' => _DOCSERVER_EMPTY,
);
return $control;
}
$docserver = $this->isADocserver($docserver);
if (!$this->docserversExists($docserver->docserver_id)) {
$control = array(
'status' => 'ko',
'value' => '',
'error' => _DOCSERVER_NOT_EXISTS,
);
return $control;
}
if ($this->adrxLinkExists(
$docserver->docserver_id,
$docserver->coll_id
)
) {
$control = array('status' => 'ko', 'value' => '',
'error' => _DOCSERVER_ATTACHED_TO_ADR_X);
return $control;
}
if ($this->resxLinkExists(
$docserver->docserver_id,
$docserver->coll_id
) {
$control = array(
'status' => 'ko',
'value' => '',
'error' => _DOCSERVER_ATTACHED_TO_RES_X,
);
return $control;
}
$query = "delete from " . _DOCSERVERS_TABLE_NAME
$stmt = $db->query($query, array($docserver->docserver_id));
} catch (Exception $e) {
$control = array(
'status' => 'ko',
'value' => '',
'error' => _CANNOT_DELETE_DOCSERVER_ID
. ' ' . $docserver->docserver_id,
);
}
'status' => 'ok',
'value' => $docserver->docserver_id,
if ($_SESSION['history']['docserversdel'] == 'true') {
$history = new history();
$history->add(
_DOCSERVERS_TABLE_NAME,
$docserver->docserver_id,
'DEL','docserversdel',
_DOCSERVER_DELETED . ' : ' . $docserver->docserver_id,
$_SESSION['config']['databasetype']
);
}
} else {
'status' => 'ko',
'value' => '',
'error' => _CANNOT_DELETE_DOCSERVER_ID

SNA
committed
}
return $control;
}
/**
* Disables a given docservers
*
* @param $docserver docservers object
* @return bool true if the disabling is complete, false otherwise
*/
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
if ($docserver->docserver_id <> 'TEMPLATES') {
$control = array();
if (!isset($docserver) || empty($docserver)) {
$control = array(
'status' => 'ko',
'value' => '',
'error' => _DOCSERVER_EMPTY,
);
return $control;
}
$docserver = $this->isADocserver($docserver);
$this->set_foolish_ids(array('docserver_id'));
$this->set_specific_id('docserver_id');
if ($this->advanced_disable($docserver)) {
$control = array(
'status' => 'ok',
'value' => $docserver->docserver_id,
);
if ($_SESSION['history']['docserversban'] == 'true') {
$history = new history();
$history->add(
_DOCSERVERS_TABLE_NAME,
$docserver->docserver_id,
'BAN','docserversban',
_DOCSERVER_DISABLED . ' : ' . $docserver->docserver_id,
$_SESSION['config']['databasetype']
);
}
} else {
$control = array(
'status' => 'ko',
'value' => '',
'error' => _PB_WITH_DOCSERVER,

SNA
committed
}
} else {
'error' => _CANNOT_SUSPEND_DOCSERVER . ' ' . $docserver->docserver_id,

SNA
committed
}
return $control;
}
/**
* Enables a given docserver
*
* @param $docserver docservers object
* @return bool true if the enabling is complete, false otherwise
*/

SNA
committed
$control = array();

SNA
committed
return $control;
}
$docserver = $this->isADocserver($docserver);
$this->set_foolish_ids(array('docserver_id'));
$this->set_specific_id('docserver_id');
if ($this->advanced_enable($docserver)) {
'value' => $docserver->docserver_id,
);
if ($_SESSION['history']['docserversallow'] == 'true') {

SNA
committed
$history = new history();
_DOCSERVERS_TABLE_NAME,
$docserver->docserver_id,
_DOCSERVER_ENABLED . ' : ' . $docserver->docserver_id,

SNA
committed
}
} else {

SNA
committed
}
return $control;
}
/**
* Fill a docserver object with an object if it's not a docserver
*
* @param $object ws docserver object
* @return object docservers
*/

SNA
committed
$func = new functions();
$docserverObject = new docservers();
$array = array();
$array = $func->object2array($object);
$docserverObject->{$key} = $array[$key];

SNA
committed
}
return $docserverObject;
} else {
return $object;
}
}
/**
* Test if a given docserver exists
*
* @param $docserver docservers object
* @return bool true if exists, false otherwise
*/
public function docserversExists($docserver_id)
{
if (!isset($docserver_id) || empty($docserver_id)) {

SNA
committed
return false;
$query = "select docserver_id from " . _DOCSERVERS_TABLE_NAME

SNA
committed
try{
echo _UNKNOWN . _DOCSERVER . ' ' . functions::xssafe($docserver_id) . ' // ';

SNA
committed
}

SNA
committed
return true;
}
return false;
}
/**
*Check if the docserver is linked to a ressource
*@param docserver_id docservers
*@return bool true if it's linked
*/
public function resxLinkExists($docserver_id, $coll_id)
if ($coll_id == 'templates') {
return false;
}
$tableName = $security->retrieve_table_from_coll($coll_id);
if (!isset($tableName) || empty($tableName)) {
return false;
}
$query = "select docserver_id from " . $tableName
. " where docserver_id = ?";
$stmt = $db->query($query, array($docserver_id));
if ($stmt->rowCount() > 0) {

SNA
committed
return true;
}

SNA
committed
}
/**
*Check if the docserver is linked to a ressource address
*@param docserver_id docservers
*@return bool true if it's linked
*/
public function adrxLinkExists($docserver_id, $coll_id)
$adrName = $security->retrieveAdrFromColl($coll_id);
if (!isset($adrName) || empty($adrName)) {
return false;
}
$query = "select docserver_id from " . $adrName
. " where docserver_id = ?";
$stmt = $db->query($query, array($docserver_id));
if ($stmt->rowCount() > 0) {

SNA
committed
return true;
}
}
/**
* Check if two docservers have the same priorities
*
* @param $docserver docservers object
* @return bool true if the control is ok
*/
private function adrPriorityNumberControl($docserver)
$func = new functions();
if (!isset($docserver)
|| empty($docserver)
|| empty($docserver->adr_priority_number)
) {
return false;
}
$query = "select adr_priority_number from " . _DOCSERVERS_TABLE_NAME
. " where adr_priority_number = ? AND docserver_type_id = ?"
. " AND docserver_id <> ?";
$stmt = $db->query(
$query,
array(
$docserver->adr_priority_number,
$docserver->docserver_type_id,
$docserver->docserver_id
)
);
if ($stmt->rowCount() > 0) {

SNA
committed
return false;
}
return true;
}
/**
* Check if two docservers have the same priorities
*
* @param $docserver docservers object
* @return bool true if the control is ok
*/
private function priorityNumberControl($docserver)
$func = new functions();
if (!isset($docserver)
|| empty($docserver)
|| empty($docserver->priority_number)
) {
return false;
}
$query = "select priority_number from " . _DOCSERVERS_TABLE_NAME
. " where priority_number = ? AND docserver_type_id = ?"
. " AND docserver_id <> ?";
$stmt = $db->query(
$query,
array(
$docserver->priority_number,
$docserver->docserver_type_id,
$docserver->docserver_id
)
);
if ($stmt->rowCount() > 0) {

SNA
committed
return false;
}
return true;
}
/**
* Check if the docserver actual size is less than the size limit
*
* @param $docserver docservers object
* @return bool true if the control is ok
*/
public function actualSizeNumberControl($docserver)
{
if (!isset($docserver) || empty($docserver)) {
return false;
}

SNA
committed
$size_limit_number = floatval($docserver->size_limit_number);
$size_limit_number = $size_limit_number * 1000 * 1000 * 1000;
$query = "select actual_size_number from " . _DOCSERVERS_TABLE_NAME
. " where docserver_id = ?";
$stmt = $db->query($query, array($docserver->docserver_id));
$queryResult = $stmt->fetchObject();
if (isset($queryResult->actual_size_number)) {
$actual_size_number = floatval($queryResult->actual_size_number);
} else {
$actual_size_number = 0;
}

SNA
committed
return true;
} else {
return false;
}
}
/**
* Check if the docserver size has not reached the limit
*
* @param $docserver docservers object
* @return bool true if the control is ok
*/
private function sizeLimitControl($docserver)

SNA
committed
$docserver->size_limit_number = floatval($docserver->size_limit_number);
$maxsizelimit = floatval(
$_SESSION['docserversFeatures']['DOCSERVERS']['MAX_SIZE_LIMIT']
) * 1000 * 1000 * 1000;
if (!isset($docserver) || empty($docserver)) {

SNA
committed
return false;
if ($docserver->size_limit_number < $maxsizelimit) {

SNA
committed
return false;
} else {
return true;
}
}
/**
* Get docservers to insert a new doc.
* Can return null if no corresponding object.
* @param $coll_id string Collection identifier
* @return docservers
*/
public function getDocserverToInsert($collId)
$query = "select priority_number, docserver_id from "
. _DOCSERVERS_TABLE_NAME . " where is_readonly = 'N' and "
. " enabled = 'Y' and coll_id = ? order by priority_number";
$stmt = $db->query($query, array($collId));
$queryResult = $stmt->fetchObject();
$docserver = $this->get($queryResult->docserver_id);

SNA
committed
return $docserver;

SNA
committed
return null;

SNA
committed
} else {
return null;
}
}
/**
* Store a new doc in a docserver.
* @param $collId string collection resource
* @param $fileInfos array , contains :

SNA
committed
* tmpDir : path to tmp directory
* size : size of the doc
* format : format of the doc
* tmpFileName : file name of the doc in Maarch tmp directory
* @return array of docserver data for res_x else return error
*/
public function storeResourceOnDocserver($collId, $fileInfos)
$docserver = $this->getDocserverToInsert($collId);
. _NO_AVAILABLE_DOCSERVER . ' . ' . _MORE_INFOS . '.',
);

SNA
committed
return $storeInfos;
}
$newSize = $this->checkSize($docserver, $fileInfos['size']);
. _NOT_ENOUGH_DISK_SPACE . ' . ' . _MORE_INFOS . '.',
);

SNA
committed
return $storeInfos;
}

SNA
committed
$tmp = $_SESSION['config']['tmppath'];
} else {
$tmp = $fileInfos['tmpDir'];
}
$d = dir($tmp);
$pathTmp = $d->path;

SNA
committed
$theFile = $entry;
break;
}
}
$d->close();
$pathOnDocserver = array();
$pathOnDocserver = Ds_createPathOnDocServer(
$docserver->path_template
);
$docinfo = $this->getNextFileNameInDocserver(
$pathOnDocserver['destinationDir']
);
if ($docinfo['error'] <> '') {
$_SESSION['error'] = _FILE_SEND_ERROR . '. '._TRY_AGAIN . '. '
. _MORE_INFOS . ' : <a href=\'mailto:'
. $_SESSION['config']['adminmail'] . '\'>'
require_once('core' . DIRECTORY_SEPARATOR . 'class'
. DIRECTORY_SEPARATOR . 'docserver_types_controler.php');
$docserverTypeControler = new docserver_types_controler();
$docserverTypeObject = $docserverTypeControler->get(
$docserver->docserver_type_id
);
if (isset($copyResultArray['error']) && $copyResultArray['error'] <> '') {
//second chance
$docinfo = array();
$copyResultArray = array();
$docinfo = $this->getNextFileNameInDocserver(
$pathOnDocserver['destinationDir']
);
if ($docinfo['error'] <> '') {
$_SESSION['error'] = _FILE_SEND_ERROR . '. '._TRY_AGAIN . '. '
. _MORE_INFOS . ' : <a href=\'mailto:'
. $_SESSION['config']['adminmail'] . '\'>'
. $_SESSION['config']['adminname'] . '</a>';
}
$docinfo['fileDestinationName'] .= '.'
. strtolower($func->extractFileExt($tmpSourceCopy));
$copyResultArray = Ds_copyOnDocserver(
$tmpSourceCopy,
$docinfo,
$docserverTypeObject->fingerprint_mode
);
if (isset($copyResultArray['error']) && $copyResultArray['error'] <> '') {
$storeInfos = array('error' => $copyResultArray['error']);
return $storeInfos;
}
}
$destinationDir = $copyResultArray['destinationDir'];
$fileDestinationName = $copyResultArray['fileDestinationName'];
strlen($docserver->path_template)
) . DIRECTORY_SEPARATOR;
$destinationDir = str_replace(