Skip to content
Snippets Groups Projects
docservers_tools.php 10.8 KiB
Newer Older
Giovannoni Laurent's avatar
Giovannoni Laurent committed
<?php

/*
*   Copyright 2008-2011 Maarch
*
*   This file is part of Maarch Framework.
*
*   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/>.
*/

/**
SNA's avatar
SNA committed
* @brief API to manage docservers
Giovannoni Laurent's avatar
Giovannoni Laurent committed
*
* @file
* @author Laurent Giovannoni
* @date $date$
* @version $Revision$
* @ingroup core
*/

//Loads the required class
try {
SNA's avatar
SNA committed
    require_once 'core/class/docservers.php';
    require_once 'core/class/docservers_controler.php';
    require_once 'core/core_tables.php';
Giovannoni Laurent's avatar
Giovannoni Laurent committed
} catch (Exception $e) {
    functions::xecho($e->getMessage()) . ' // ';
Giovannoni Laurent's avatar
Giovannoni Laurent committed
}

/**
 * copy doc in a docserver.
 * @param   string $sourceFilePath collection resource
SNA's avatar
SNA committed
 * @param   array $infoFileNameInTargetDocserver infos of the doc to store,
Giovannoni Laurent's avatar
Giovannoni Laurent committed
 *          contains : subdirectory path and new filename
 * @param   string $docserverSourceFingerprint
 * @return  array of docserver data for res_x else return error
 */
function Ds_copyOnDocserver(
SNA's avatar
SNA committed
    $sourceFilePath,
    $infoFileNameInTargetDocserver,
Giovannoni Laurent's avatar
Giovannoni Laurent committed
    $docserverSourceFingerprint='NONE'
) {
Alex ORLUC's avatar
Alex ORLUC committed
    error_reporting(0);
Giovannoni Laurent's avatar
Giovannoni Laurent committed
    $destinationDir = $infoFileNameInTargetDocserver['destinationDir'];
    $fileDestinationName =
        $infoFileNameInTargetDocserver['fileDestinationName'];
    $sourceFilePath = str_replace('\\\\', '\\', $sourceFilePath);
    if (file_exists($destinationDir . $fileDestinationName)) {
        $storeInfos = array('error' => _FILE_ALREADY_EXISTS);
        return $storeInfos;
    }
Giovannoni Laurent's avatar
Giovannoni Laurent committed
    if (!is_dir($destinationDir)) {
        mkdir($destinationDir, 0770, true);
        Ds_setRights($destinationDir);
    }
Giovannoni Laurent's avatar
Giovannoni Laurent committed
    $cp = copy($sourceFilePath, $destinationDir . $fileDestinationName);
    Ds_setRights($destinationDir . $fileDestinationName);
Giovannoni Laurent's avatar
Giovannoni Laurent committed
    if ($cp == false) {
        $storeInfos = array('error' => _DOCSERVER_COPY_ERROR);
        return $storeInfos;
    }
    $fingerprintControl = array();
    $fingerprintControl = Ds_controlFingerprint(
SNA's avatar
SNA committed
        $sourceFilePath,
        $destinationDir . $fileDestinationName,
Giovannoni Laurent's avatar
Giovannoni Laurent committed
        $docserverSourceFingerprint
    );
    if ($fingerprintControl['status'] == 'ko') {
        $storeInfos = array('error' => $fingerprintControl['error']);
        return $storeInfos;
    }

Giovannoni Laurent's avatar
Giovannoni Laurent committed
    /*$ofile = fopen($destinationDir.$fileDestinationName, 'r');
    if (Ds_isCompleteFile($ofile)) {
        fclose($ofile);
    } else {
        $storeInfos = array('error' => _COPY_OF_DOC_NOT_COMPLETE);
        return $storeInfos;
    }*/
SNA's avatar
SNA committed
    if (isset($GLOBALS['currentStep'])) {
        $destinationDir = str_replace(
            $GLOBALS['docservers'][$GLOBALS['currentStep']]['docserver']
            ['path_template'],
            '',
            $destinationDir
        );
    }
Giovannoni Laurent's avatar
Giovannoni Laurent committed
    $destinationDir = str_replace(
SNA's avatar
SNA committed
        DIRECTORY_SEPARATOR,
        '#',
Giovannoni Laurent's avatar
Giovannoni Laurent committed
        $destinationDir
    );
    $storeInfos = array(
SNA's avatar
SNA committed
        'destinationDir' => $destinationDir,
        'fileDestinationName' => $fileDestinationName,
Giovannoni Laurent's avatar
Giovannoni Laurent committed
        'fileSize' => filesize($sourceFilePath),
    );
SNA's avatar
SNA committed
    if (isset($GLOBALS['TmpDirectory']) && $GLOBALS['TmpDirectory'] <> '') {
Giovannoni Laurent's avatar
Giovannoni Laurent committed
        Ds_washTmp($GLOBALS['TmpDirectory'], true);
    }
    return $storeInfos;
}

/**
 * Compute the path in the docserver for a batch
 * @param $docServer docservers path
 * @return array Contains 2 items : subdirectory path and error
Giovannoni Laurent's avatar
Giovannoni Laurent committed
 */
SNA's avatar
SNA committed
function Ds_createPathOnDocServer($docServer)
Giovannoni Laurent's avatar
Giovannoni Laurent committed
{
Alex ORLUC's avatar
Alex ORLUC committed
    error_reporting(0);
Giovannoni Laurent's avatar
Giovannoni Laurent committed
    umask(0022);
Giovannoni Laurent's avatar
Giovannoni Laurent committed
    if (!is_dir($docServer . date('Y') . DIRECTORY_SEPARATOR)) {
        mkdir($docServer . date('Y') . DIRECTORY_SEPARATOR, 0770);
        Ds_setRights($docServer . date('Y') . DIRECTORY_SEPARATOR);
Giovannoni Laurent's avatar
Giovannoni Laurent committed
    }
    if (!is_dir(
SNA's avatar
SNA committed
        $docServer . date('Y') . DIRECTORY_SEPARATOR.date('m')
Giovannoni Laurent's avatar
Giovannoni Laurent committed
        . DIRECTORY_SEPARATOR
    )
    ) {
        mkdir(
SNA's avatar
SNA committed
            $docServer . date('Y') . DIRECTORY_SEPARATOR.date('m')
            . DIRECTORY_SEPARATOR,
Giovannoni Laurent's avatar
Giovannoni Laurent committed
        );
        Ds_setRights(
SNA's avatar
SNA committed
            $docServer . date('Y') . DIRECTORY_SEPARATOR.date('m')
            . DIRECTORY_SEPARATOR
SNA's avatar
SNA committed
    if (isset($GLOBALS['wb']) && $GLOBALS['wb'] <> '') {
        $path = $docServer . date('Y') . DIRECTORY_SEPARATOR.date('m')
Giovannoni Laurent's avatar
Giovannoni Laurent committed
              . DIRECTORY_SEPARATOR . 'BATCH' . DIRECTORY_SEPARATOR 
              . $GLOBALS['wb'] . DIRECTORY_SEPARATOR;
Giovannoni Laurent's avatar
Giovannoni Laurent committed
        if (!is_dir($path)) {
Giovannoni Laurent's avatar
Giovannoni Laurent committed
            mkdir($path, 0770, true);
            Ds_setRights($path);
Giovannoni Laurent's avatar
Giovannoni Laurent committed
        } else {
            return array(
Giovannoni Laurent's avatar
Giovannoni Laurent committed
                'destinationDir' => $path,
SNA's avatar
SNA committed
                'error' => 'Folder alreay exists, workbatch already exist:'
Giovannoni Laurent's avatar
Giovannoni Laurent committed
                . $path,
            );
        }
    } else {
SNA's avatar
SNA committed
        $path = $docServer . date('Y') . DIRECTORY_SEPARATOR.date('m')
Giovannoni Laurent's avatar
Giovannoni Laurent committed
              . DIRECTORY_SEPARATOR;
    }
    return array(
SNA's avatar
SNA committed
        'destinationDir' => $path,
Giovannoni Laurent's avatar
Giovannoni Laurent committed
        'error' => '',
    );
}

/**
 * Compute the fingerprint of a resource
 * @param   string $path path of the resource
 * @param   string $fingerprintMode (md5, sha512, ...)
 * @return  string the fingerprint
 */
SNA's avatar
SNA committed
function Ds_doFingerprint($path, $fingerprintMode)
Giovannoni Laurent's avatar
Giovannoni Laurent committed
{
    if ($fingerprintMode == 'NONE' || $fingerprintMode == '') {
        return '0';
    } else {
        return hash_file(strtolower($fingerprintMode), $path);
    }
}

 /**
 * Control fingerprint between two resources
 * @param   string $pathInit path of the resource 1
 * @param   string $pathTarget path of the resource 2
 * @param   string $fingerprintMode (md5, sha512, ...)
 * @return  array ok or ko with error
 */
function Ds_controlFingerprint(
SNA's avatar
SNA committed
    $pathInit,
    $pathTarget,
Giovannoni Laurent's avatar
Giovannoni Laurent committed
    $fingerprintMode='NONE'
) {
    $result = array();
    if (Ds_doFingerprint(
SNA's avatar
SNA committed
        $pathInit,
Giovannoni Laurent's avatar
Giovannoni Laurent committed
        $fingerprintMode
    ) <> Ds_doFingerprint($pathTarget, $fingerprintMode)
    ) {
        $result = array(
SNA's avatar
SNA committed
            'status' => 'ko',
            'error' => _PB_WITH_FINGERPRINT_OF_DOCUMENT . ' ' . $pathInit
Giovannoni Laurent's avatar
Giovannoni Laurent committed
            . ' '. _AND . ' ' . $pathTarget,
        );
    } else {
        $result = array(
SNA's avatar
SNA committed
            'status' => 'ok',
Giovannoni Laurent's avatar
Giovannoni Laurent committed
            'error' => '',
        );
    }
    return $result;
}

 /**
 * Set Rights on resources
 * @param   string $dest path of the resource
Giovannoni Laurent's avatar
Giovannoni Laurent committed
 * @return  nothing
 */
function Ds_setRights($dest)
Giovannoni Laurent's avatar
Giovannoni Laurent committed
{
        DIRECTORY_SEPARATOR == '/'
        && (isset($GLOBALS['apacheUserAndGroup'])
        && $GLOBALS['apacheUserAndGroup'] <> '')
    ) {
        exec('chown ' 
            . escapeshellarg($GLOBALS['apacheUserAndGroup']) . ' ' 
            . escapeshellarg($dest)
        );
Giovannoni Laurent's avatar
Giovannoni Laurent committed
    }
    umask(0022);
    chmod($dest, 0770);
Giovannoni Laurent's avatar
Giovannoni Laurent committed
}
Giovannoni Laurent's avatar
Giovannoni Laurent committed
/**
Giovannoni Laurent's avatar
Giovannoni Laurent committed
* get the mime type of a file with a path
Giovannoni Laurent's avatar
Giovannoni Laurent committed
* @param $filePath path of the file
* @return string of the mime type
*/
SNA's avatar
SNA committed
function Ds_getMimeType($filePath)
Giovannoni Laurent's avatar
Giovannoni Laurent committed
{
Giovannoni Laurent's avatar
Giovannoni Laurent committed
    //require_once 'MIME/Type.php';
    //return MIME_Type::autoDetect($filePath);
    return mime_content_type($filePath);
Giovannoni Laurent's avatar
Giovannoni Laurent committed
/**
* get the mime type of a file with a buffer
* @param $fileBuffer buffer of the file
* @return string of the mime type
*/
function Ds_getMimeTypeWithBuffer($fileBuffer)
{
    $finfo = new finfo(FILEINFO_MIME);
    return $finfo->buffer($fileBuffer);
}

Giovannoni Laurent's avatar
Giovannoni Laurent committed
/**
 * del tmp files
 * @param   $dir dir to wash
 * @param   $contentOnly boolean true if only the content
 * @return  boolean
 */
SNA's avatar
SNA committed
function Ds_washTmp($dir, $contentOnly=false)
Giovannoni Laurent's avatar
Giovannoni Laurent committed
{
    if (is_dir($dir)) {
        $objects = scandir($dir);
        foreach ($objects as $object) {
            if ($object != '.' && $object != '..') {
                if (
                    filetype($dir . DIRECTORY_SEPARATOR . $object) == 'dir'
                ) {
SNA's avatar
SNA committed
                    Ds_washTmp($dir . DIRECTORY_SEPARATOR . $object);
Giovannoni Laurent's avatar
Giovannoni Laurent committed
                } else {
                    unlink($dir . DIRECTORY_SEPARATOR . $object);
                }
            }
        }
        reset($objects);
        if (!$contentOnly) {
            rmdir($dir);
        }
    }
}

/**
* Return true when the file is completed
* @param  $file
* @param  $delay
* @param  $pointer position in the file
*/
SNA's avatar
SNA committed
function Ds_isCompleteFile($file, $delay=500, $pointer=0)
Giovannoni Laurent's avatar
Giovannoni Laurent committed
{
    if ($file == null) {
        return false;
    }
    fseek($file, $pointer);
    $currentLine = fgets($file);
    while (!feof($file)) {
        $currentLine = fgets($file);
    }
    $currentPos = ftell($file);
    //Wait $delay ms
    usleep($delay * 1000);
    if ($currentPos == $pointer) {
        return true;
    } else {
        return Ds_isCompleteFile($file, $delay, $currentPos);
    }
}

/**
 * Check the mime type of a file with the extension config file
* Return array with the status of the check and the mime type of the file
* @param  string $filePath
* @param  array
*/
function Ds_isFileTypeAllowed($filePath, $extDefault = '')
    $mimeType = Ds_getMimeType(
        $filePath
    );
    $ext = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
    if ($ext == '' || $ext == 'tmp') {
        $ext = $extDefault;
    }
    if ($ext == 'html' && $mimeType == "text/plain") {
        $arrayReturn = array(
            'status' => true,
            'mime_type' => "text/html",
        );
        return $arrayReturn;
    }
    if (file_exists($_SESSION['config']['corepath'] . 'custom'
        . DIRECTORY_SEPARATOR.$_SESSION['custom_override_id']
        . DIRECTORY_SEPARATOR
        . 'apps' . DIRECTORY_SEPARATOR . $_SESSION['config']['app_id']
        . DIRECTORY_SEPARATOR . 'xml' . DIRECTORY_SEPARATOR
        . 'extensions.xml')
    ) {
        $path = $_SESSION['config']['corepath'] . 'custom'
        . DIRECTORY_SEPARATOR . $_SESSION['custom_override_id']
        . DIRECTORY_SEPARATOR . 'apps' . DIRECTORY_SEPARATOR
        . $_SESSION['config']['app_id'] . DIRECTORY_SEPARATOR . 'xml'
        . DIRECTORY_SEPARATOR . 'extensions.xml';
    } else {
        $path = $_SESSION['config']['corepath'] . 'apps' 
		. DIRECTORY_SEPARATOR . $_SESSION['config']['app_id']
        . DIRECTORY_SEPARATOR . 'xml' . DIRECTORY_SEPARATOR . 'extensions.xml';
    }
    $xmlconfig = simplexml_load_file($path);
    $ext_list = array();
    $i = 0;
    foreach ($xmlconfig->FORMAT as $FORMAT) {
        $ext_list[$i] = array(
            'name' => (string) $FORMAT->name,
            'mime' => (string) $FORMAT->mime
        );
        $i++;
    }
    $type_state = false;
    for ($i=0;$i<count($ext_list);$i++) {
        if (
            $ext_list[$i]['mime'] == $mimeType 
            && strtolower($ext_list[$i]['name']) == $ext
        ) {

            $type_state = true;
            break;
        }
    }
    $arrayReturn = array(
        'status' => $type_state,
        'mime_type' => $mimeType,
    );
    return $arrayReturn;
}