Skip to content
Snippets Groups Projects
docservers_tools.php 21.4 KiB
Newer Older
  • Learn to ignore specific revisions
  • 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 @return array Contains 2 items : subdirectory path and error
     */
    
    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' => '',
        );
    }
    
    /**
     * Extract a file from an archive
     * @param   $fileInfos infos of the doc to store, contains :
     *          tmpDir : path to tmp directory
     *          path_to_file : path to the file in the docserver
     *          filename : name of the file
     *          offset_doc : offset of the doc in the container
     *          $fingerprintMode
     * @return  array with path of the extracted doc
     */
    
    SNA's avatar
    SNA committed
    function Ds_extractArchive($fileInfos, $fingerprintMode)
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
    {
        //var_dump($fileInfos);
    
        if (!isset($fileInfos['tmpDir']) || $fileInfos['tmpDir'] == '') {
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
            $tmp = $_SESSION['config']['tmppath'];
        } else {
            $tmp = $fileInfos['tmpDir'];
        }
    
    SNA's avatar
    SNA committed
        $fileNameOnTmp = $tmp . rand() . '_'
                       . md5_file($fileInfos['path_to_file'])
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                       . '_' . $fileInfos['filename'];
        $cp = copy($fileInfos['path_to_file'], $fileNameOnTmp);
    
        Ds_setRights($fileNameOnTmp);
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
        if ($cp == false) {
            $result = array(
    
    SNA's avatar
    SNA committed
                'status' => 'ko',
                'path' => '',
                'mime_type' => '',
                'format' => '',
                'tmpArchive' => '',
                'fingerprint' => '',
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                'error' => _TMP_COPY_ERROR,
            );
            return $result;
        } else {
            $execError = '';
            $tmpArchive = uniqid(rand());
            if (mkdir($tmp . $tmpArchive)) {
                //try to extract the offset if it's possible
                if (DIRECTORY_SEPARATOR == '/') {
    
    SNA's avatar
    SNA committed
                    $command = '7z x -y -o'
                             . escapeshellarg($tmp . $tmpArchive) . ' '
                             . escapeshellarg($fileNameOnTmp) . ' '
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                             . escapeshellarg($fileNameOnTmp);
                } else {
    
    SNA's avatar
    SNA committed
                    $command = '"'
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                        . str_replace(
    
    SNA's avatar
    SNA committed
                            '\\',
                            '\\\\',
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                            $_SESSION['docserversFeatures']['DOCSERVERS']
                            ['PATHTOCOMPRESSTOOL']
    
    SNA's avatar
    SNA committed
                        )
                        . '" x -y -o' . escapeshellarg($tmp . $tmpArchive)
                        . ' ' . escapeshellarg($fileNameOnTmp) . ' '
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                        . escapeshellarg($fileNameOnTmp);
                }
                $tmpCmd = '';
                exec($command, $tmpCmd, $execError);
                if ($execError > 0) {
                    if (DIRECTORY_SEPARATOR == '/') {
                        //else try to extract only the first container
    
    SNA's avatar
    SNA committed
                        $command = '7z x -y -o'
                                 . escapeshellarg($tmp . $tmpArchive) . ' '
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                                 . escapeshellarg($fileNameOnTmp);
                    } else {
    
    SNA's avatar
    SNA committed
                        $command = '"'
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                            . str_replace(
    
    SNA's avatar
    SNA committed
                                '\\',
                                '\\\\',
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                                $_SESSION['docserversFeatures']['DOCSERVERS']
                                ['PATHTOCOMPRESSTOOL']
    
    SNA's avatar
    SNA committed
                            )
                            . '" x -y -o'
                            . escapeshellarg($tmp . $tmpArchive) . ' '
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                            . escapeshellarg($fileNameOnTmp);
                    }
                    $tmpCmd = '';
                    exec($command, $tmpCmd, $execError);
                    if ($execError > 0) {
                        $result = array(
    
    SNA's avatar
    SNA committed
                            'status' => 'ko',
                            'path' => '',
                            'mime_type' => '',
                            'format' => '',
                            'tmpArchive' => '',
                            'fingerprint' => '',
                            'error' => _PB_WITH_EXTRACTION_OF_CONTAINER . '#'
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                            . $execError,
                        );
                        return $result;
                    }
                }
            } else {
                $result = array(
    
    SNA's avatar
    SNA committed
                    'status' => 'ko',
                    'path' => '',
                    'mime_type' => '',
                    'format' => '',
                    'tmpArchive' => '',
                    'fingerprint' => '',
                    'error' => _PB_WITH_EXTRACTION_OF_CONTAINER . '#' . $tmp
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                    . $tmpArchive,
                );
                return $result;
            }
            $format = substr(
    
    SNA's avatar
    SNA committed
                $fileInfos['offset_doc'],
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                strrpos($fileInfos['offset_doc'], '.') + 1
            );
            if (!file_exists(
    
    SNA's avatar
    SNA committed
                $tmp . $tmpArchive . DIRECTORY_SEPARATOR
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                . $fileInfos['offset_doc']
            )
            ) {
                $classScan = dir($tmp . $tmpArchive);
                while (($fileScan = $classScan->read()) != false) {
                    if ($fileScan == '.' || $fileScan == '..') {
                        continue;
                    } else {
                        preg_match("'CI|.tar'", $fileScan, $out);
    
                        if (isset($out[0]) && count($out[0]) == 1) {
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                            $execError = '';
                            $tmpArchiveBis = uniqid(rand());
                            if (mkdir(
    
    SNA's avatar
    SNA committed
                                $tmp . $tmpArchive . DIRECTORY_SEPARATOR
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                                . $tmpArchiveBis
                            )
                            ) {
                                if (DIRECTORY_SEPARATOR == '/') {
    
    SNA's avatar
    SNA committed
                                    $commandBis = '7z x -y -o'
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                                                . escapeshellarg(
    
    SNA's avatar
    SNA committed
                                                    $tmp . $tmpArchive
                                                    . DIRECTORY_SEPARATOR
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                                                    . $tmpArchiveBis
                                                )
    
    SNA's avatar
    SNA committed
                                                . ' '
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                                                . escapeshellarg(
    
    SNA's avatar
    SNA committed
                                                    $tmp . $tmpArchive
                                                    . DIRECTORY_SEPARATOR
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                                                    . $fileScan
                                                )
                                                . ' ' .$fileInfos['offset_doc'];
                                } else {
    
    SNA's avatar
    SNA committed
                                    $commandBis = '"'
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                                                . str_replace(
    
    SNA's avatar
    SNA committed
                                                    '\\',
                                                    '\\\\',
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                                                    $_SESSION
                                                    ['docserversFeatures']
                                                    ['DOCSERVERS']
                                                    ['PATHTOCOMPRESSTOOL']
                                                )
    
    SNA's avatar
    SNA committed
                                                . '" x -y -o'
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                                                . escapeshellarg(
    
    SNA's avatar
    SNA committed
                                                    $tmp . $tmpArchive
                                                    . DIRECTORY_SEPARATOR
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                                                    . $tmpArchiveBis
                                                )
    
    SNA's avatar
    SNA committed
                                                . ' '
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                                                . escapeshellarg(
    
    SNA's avatar
    SNA committed
                                                    $tmp . $tmpArchive
                                                    . DIRECTORY_SEPARATOR
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                                                    . $fileScan
                                                )
                                                . ' ' .$fileInfos['offset_doc'];
                                }
                                $tmpCmd = '';
                                exec($commandBis, $tmpCmd, $execError);
                                if ($execError > 0) {
                                    $result = array(
    
    SNA's avatar
    SNA committed
                                        'status' => 'ko',
                                        'path' => '',
                                        'mime_type' => '',
                                        'format' => '',
                                        'tmpArchive' => '',
                                        'fingerprint' => '',
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                                        'error' =>
    
    SNA's avatar
    SNA committed
                                        _PB_WITH_EXTRACTION_OF_CONTAINER . '#'
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                                        . $execError,
                                    );
                                }
                            } else {
                                $result = array(
    
    SNA's avatar
    SNA committed
                                    'status' => 'ko',
                                    'path' => '',
                                    'mime_type' => '',
                                    'format' => '',
                                    'tmpArchive' => '',
                                    'fingerprint' => '',
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                                    'error' => _PB_WITH_EXTRACTION_OF_CONTAINER
    
    SNA's avatar
    SNA committed
                                    . '#' . $tmp . $tmpArchive
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                                    . DIRECTORY_SEPARATOR . $tmpArchiveBis,
                                );
                                return $result;
                            }
                            $path = str_replace(
    
    SNA's avatar
    SNA committed
                                $fileScan,
                                '',
                                $tmp . $tmpArchive . DIRECTORY_SEPARATOR
                                . $tmpArchiveBis . DIRECTORY_SEPARATOR
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                                . $fileInfos['offset_doc']
                            );
                            $path = str_replace(
    
    SNA's avatar
    SNA committed
                                '#',
                                DIRECTORY_SEPARATOR,
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                                $path
                            );
                            $result = array(
    
    SNA's avatar
    SNA committed
                                'status' => 'ok',
                                'path' => $path,
                                'mime_type' => Ds_getMimeType($path),
                                'format' => $format,
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                                'fingerprint' =>
    
    SNA's avatar
    SNA committed
                                Ds_doFingerprint($path, $fingerprintMode),
                                'tmpArchive' => $tmp . $tmpArchive,
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                                'error' => '',
                            );
                            unlink(
    
    SNA's avatar
    SNA committed
                                $tmp . $tmpArchive . DIRECTORY_SEPARATOR
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                                . $fileScan
                            );
                            break;
                        }
                    }
                }
            } else {
                $result = array(
    
    SNA's avatar
    SNA committed
                    'status' => 'ok',
                    'path' => $tmp . $tmpArchive . DIRECTORY_SEPARATOR
                    . $fileInfos['offset_doc'],
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                    'mime_type' =>
                    Ds_getMimeType(
    
    SNA's avatar
    SNA committed
                        $tmp . $tmpArchive . DIRECTORY_SEPARATOR
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                        . $fileInfos['offset_doc']
                    )
    
    SNA's avatar
    SNA committed
                    ,
                    'format' => $format,
                    'tmpArchive' => $tmp . $tmpArchive,
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                    'fingerprint' =>
                    Ds_doFingerprint(
    
    SNA's avatar
    SNA committed
                        $tmp . $tmpArchive . DIRECTORY_SEPARATOR
                        . $fileInfos['offset_doc'],
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                        $fingerprintMode
                    )
    
    SNA's avatar
    SNA committed
                    ,
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                    'error' => '',
                );
            }
            unlink($fileNameOnTmp);
            return $result;
        }
    }
    /**
     * 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;
    }