2016-11-07 07:52:46 +00:00
|
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Implementation of preview base
|
|
|
|
|
*
|
|
|
|
|
* @category DMS
|
|
|
|
|
* @package SeedDMS_Preview
|
|
|
|
|
* @license GPL 2
|
|
|
|
|
* @version @version@
|
|
|
|
|
* @author Uwe Steinmann <uwe@steinmann.cx>
|
|
|
|
|
* @copyright Copyright (C) 2010, Uwe Steinmann
|
|
|
|
|
* @version Release: @package_version@
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class for managing creation of preview images for documents.
|
|
|
|
|
*
|
|
|
|
|
* @category DMS
|
|
|
|
|
* @package SeedDMS_Preview
|
|
|
|
|
* @version @version@
|
|
|
|
|
* @author Uwe Steinmann <uwe@steinmann.cx>
|
|
|
|
|
* @copyright Copyright (C) 2011, Uwe Steinmann
|
|
|
|
|
* @version Release: @package_version@
|
|
|
|
|
*/
|
|
|
|
|
class SeedDMS_Preview_Base {
|
|
|
|
|
/**
|
|
|
|
|
* @var string $cacheDir location in the file system where all the
|
|
|
|
|
* cached data like thumbnails are located. This should be an
|
|
|
|
|
* absolute path.
|
|
|
|
|
* @access public
|
|
|
|
|
*/
|
|
|
|
|
public $previewDir;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var array $converters list of mimetypes and commands for converting
|
|
|
|
|
* file into preview image
|
|
|
|
|
* @access protected
|
|
|
|
|
*/
|
|
|
|
|
protected $converters;
|
|
|
|
|
|
2021-10-06 13:03:03 +00:00
|
|
|
|
/**
|
|
|
|
|
* @var object $conversionmgr
|
|
|
|
|
* @access protected
|
|
|
|
|
*/
|
|
|
|
|
protected $conversionmgr;
|
|
|
|
|
|
2016-11-07 07:52:46 +00:00
|
|
|
|
/**
|
|
|
|
|
* @var integer $timeout maximum time for execution of external commands
|
|
|
|
|
* @access protected
|
|
|
|
|
*/
|
|
|
|
|
protected $timeout;
|
|
|
|
|
|
2019-01-18 12:05:10 +00:00
|
|
|
|
/**
|
|
|
|
|
* @var boolean $xsendfile set to true if mod_xѕendfile is to be used
|
|
|
|
|
* @access protected
|
|
|
|
|
*/
|
|
|
|
|
protected $xsendfile;
|
|
|
|
|
|
2020-01-21 07:11:03 +00:00
|
|
|
|
/**
|
|
|
|
|
* @var string $lastpreviewfile will be set to the file name of the last preview
|
|
|
|
|
* @access protected
|
|
|
|
|
*/
|
|
|
|
|
protected $lastpreviewfile;
|
|
|
|
|
|
2019-01-18 12:05:10 +00:00
|
|
|
|
function __construct($previewDir, $timeout=5, $xsendfile=true) { /* {{{ */
|
2016-11-07 07:52:46 +00:00
|
|
|
|
if(!is_dir($previewDir)) {
|
|
|
|
|
if (!SeedDMS_Core_File::makeDir($previewDir)) {
|
|
|
|
|
$this->previewDir = '';
|
|
|
|
|
} else {
|
|
|
|
|
$this->previewDir = $previewDir;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$this->previewDir = $previewDir;
|
|
|
|
|
}
|
|
|
|
|
$this->timeout = intval($timeout);
|
2019-01-18 12:05:10 +00:00
|
|
|
|
$this->xsendfile = $xsendfile;
|
2021-10-06 13:03:03 +00:00
|
|
|
|
$this->conversionmgr = null;
|
2016-11-07 07:52:46 +00:00
|
|
|
|
} /* }}} */
|
|
|
|
|
|
2021-05-10 06:54:18 +00:00
|
|
|
|
/**
|
|
|
|
|
* Run a shell command
|
|
|
|
|
*
|
|
|
|
|
* @param $cmd
|
|
|
|
|
* @param int $timeout
|
|
|
|
|
* @return array
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2016-11-07 07:52:46 +00:00
|
|
|
|
static function execWithTimeout($cmd, $timeout=5) { /* {{{ */
|
|
|
|
|
$descriptorspec = array(
|
|
|
|
|
0 => array("pipe", "r"),
|
|
|
|
|
1 => array("pipe", "w"),
|
|
|
|
|
2 => array("pipe", "w")
|
|
|
|
|
);
|
|
|
|
|
$pipes = array();
|
2017-09-18 08:23:26 +00:00
|
|
|
|
|
2016-11-07 07:52:46 +00:00
|
|
|
|
$timeout += time();
|
2021-03-25 14:42:28 +00:00
|
|
|
|
// Putting an 'exec' before the command will not fork the command
|
|
|
|
|
// and therefore not create any child process. proc_terminate will
|
|
|
|
|
// then reliably terminate the cmd and not just shell. See notes of
|
|
|
|
|
// https://www.php.net/manual/de/function.proc-terminate.php
|
|
|
|
|
$process = proc_open('exec '.$cmd, $descriptorspec, $pipes);
|
2016-11-07 07:52:46 +00:00
|
|
|
|
if (!is_resource($process)) {
|
|
|
|
|
throw new Exception("proc_open failed on: " . $cmd);
|
|
|
|
|
}
|
2018-01-30 05:26:54 +00:00
|
|
|
|
stream_set_blocking($pipes[1], 0);
|
|
|
|
|
stream_set_blocking($pipes[2], 0);
|
2017-09-18 08:23:26 +00:00
|
|
|
|
|
2018-01-30 05:26:54 +00:00
|
|
|
|
$output = $error = '';
|
2016-11-07 07:52:46 +00:00
|
|
|
|
$timeleft = $timeout - time();
|
2018-01-30 05:26:54 +00:00
|
|
|
|
$read = array($pipes[1], $pipes[2]);
|
2016-11-07 07:52:46 +00:00
|
|
|
|
$write = NULL;
|
|
|
|
|
$exeptions = NULL;
|
|
|
|
|
do {
|
2018-01-30 05:26:54 +00:00
|
|
|
|
$num_changed_streams = stream_select($read, $write, $exeptions, $timeleft, 200000);
|
2017-09-18 08:23:26 +00:00
|
|
|
|
|
2018-01-30 05:26:54 +00:00
|
|
|
|
if ($num_changed_streams === false) {
|
|
|
|
|
proc_terminate($process);
|
|
|
|
|
throw new Exception("stream select failed on: " . $cmd);
|
|
|
|
|
} elseif ($num_changed_streams > 0) {
|
2016-11-07 07:52:46 +00:00
|
|
|
|
$output .= fread($pipes[1], 8192);
|
2018-01-30 05:26:54 +00:00
|
|
|
|
$error .= fread($pipes[2], 8192);
|
|
|
|
|
}
|
2016-11-07 07:52:46 +00:00
|
|
|
|
$timeleft = $timeout - time();
|
|
|
|
|
} while (!feof($pipes[1]) && $timeleft > 0);
|
2017-09-18 08:23:26 +00:00
|
|
|
|
|
2021-05-10 06:54:18 +00:00
|
|
|
|
fclose($pipes[0]);
|
|
|
|
|
fclose($pipes[1]);
|
|
|
|
|
fclose($pipes[2]);
|
2016-11-07 07:52:46 +00:00
|
|
|
|
if ($timeleft <= 0) {
|
|
|
|
|
proc_terminate($process);
|
|
|
|
|
throw new Exception("command timeout on: " . $cmd);
|
|
|
|
|
} else {
|
2021-05-10 06:54:18 +00:00
|
|
|
|
$return_value = proc_close($process);
|
|
|
|
|
return array('stdout'=>$output, 'stderr'=>$error, 'return'=>$return_value);
|
2016-11-07 07:52:46 +00:00
|
|
|
|
}
|
|
|
|
|
} /* }}} */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set a list of converters
|
|
|
|
|
*
|
|
|
|
|
* Merges the list of passed converters with the already existing ones.
|
|
|
|
|
* Existing converters will be overwritten.
|
|
|
|
|
*
|
|
|
|
|
* @param array list of converters. The key of the array contains the mimetype
|
|
|
|
|
* and the value is the command to be called for creating the preview
|
|
|
|
|
*/
|
|
|
|
|
function setConverters($arr) { /* {{{ */
|
2017-12-04 09:59:07 +00:00
|
|
|
|
$this->converters = $arr;
|
|
|
|
|
} /* }}} */
|
|
|
|
|
|
2021-10-06 13:03:03 +00:00
|
|
|
|
/**
|
|
|
|
|
* Set conversion service manager
|
|
|
|
|
*
|
|
|
|
|
* A conversion manager is a service for converting files from one format
|
|
|
|
|
* to another format.
|
|
|
|
|
*
|
|
|
|
|
* @param object $conversionmgr
|
|
|
|
|
*/
|
|
|
|
|
function setConversionMgr($conversionmgr) { /* {{{ */
|
|
|
|
|
$this->conversionmgr = $conversionmgr;
|
|
|
|
|
} /* }}} */
|
|
|
|
|
|
2019-01-18 12:05:10 +00:00
|
|
|
|
/**
|
|
|
|
|
* Enable/Disable xsendfile extension
|
|
|
|
|
*
|
|
|
|
|
* Merges the list of passed converters with the already existing ones.
|
|
|
|
|
* Existing converters will be overwritten.
|
|
|
|
|
*
|
|
|
|
|
* @param boolean $xsendfile turn on/off use of xsendfile module in apache
|
|
|
|
|
*/
|
|
|
|
|
function setXsendfile($xsendfile) { /* {{{ */
|
|
|
|
|
$this->xsendfile = $xsendfile;
|
|
|
|
|
} /* }}} */
|
|
|
|
|
|
2017-12-04 09:59:07 +00:00
|
|
|
|
/**
|
|
|
|
|
* Add a list of converters
|
|
|
|
|
*
|
|
|
|
|
* Merges the list of passed converters with the already existing ones.
|
|
|
|
|
* Existing converters will be overwritten.
|
|
|
|
|
*
|
|
|
|
|
* @param array list of converters. The key of the array contains the mimetype
|
|
|
|
|
* and the value is the command to be called for creating the preview
|
|
|
|
|
*/
|
|
|
|
|
function addConverters($arr) { /* {{{ */
|
2016-11-15 20:08:03 +00:00
|
|
|
|
$this->converters = array_merge($this->converters, $arr);
|
2016-11-07 07:52:46 +00:00
|
|
|
|
} /* }}} */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if converter for a given mimetype is set
|
|
|
|
|
*
|
2021-10-06 13:03:03 +00:00
|
|
|
|
* @param string $from from mimetype
|
|
|
|
|
* @param string $to to mimetype
|
|
|
|
|
*
|
2016-11-07 07:52:46 +00:00
|
|
|
|
* @return boolean true if converter exists, otherwise false
|
|
|
|
|
*/
|
2021-10-06 13:03:03 +00:00
|
|
|
|
function hasConverter($from, $to='') { /* {{{ */
|
|
|
|
|
if($this->conversionmgr)
|
|
|
|
|
return $this->conversionmgr->hasService($from, $to);
|
|
|
|
|
else
|
|
|
|
|
return array_key_exists($from, $this->converters) && $this->converters[$from];
|
2016-11-07 07:52:46 +00:00
|
|
|
|
} /* }}} */
|
|
|
|
|
|
2020-01-21 07:11:03 +00:00
|
|
|
|
/**
|
|
|
|
|
* Send a file from disk to the browser
|
|
|
|
|
*
|
|
|
|
|
* This function uses either readfile() or the xѕendfile apache module if
|
|
|
|
|
* it is installed.
|
|
|
|
|
*
|
|
|
|
|
* @param string $filename
|
|
|
|
|
*/
|
2018-01-18 07:45:34 +00:00
|
|
|
|
protected function sendFile($filename) { /* {{{ */
|
2019-01-18 12:05:10 +00:00
|
|
|
|
if($this->xsendfile && function_exists('apache_get_modules') && in_array('mod_xsendfile',apache_get_modules())) {
|
2018-01-18 07:45:34 +00:00
|
|
|
|
header("X-Sendfile: ".$filename);
|
|
|
|
|
} else {
|
2020-12-23 05:19:52 +00:00
|
|
|
|
$size = filesize($filename);
|
|
|
|
|
header("Content-Length: " . $size);
|
2018-01-18 07:45:34 +00:00
|
|
|
|
/* Make sure output buffering is off */
|
|
|
|
|
if (ob_get_level()) {
|
|
|
|
|
ob_end_clean();
|
|
|
|
|
}
|
|
|
|
|
readfile($filename);
|
|
|
|
|
}
|
|
|
|
|
} /* }}} */
|
2020-01-21 07:11:03 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return path of last created preview file
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getPreviewFile() { /* {{{ */
|
|
|
|
|
return $this->lastpreviewfile;
|
|
|
|
|
} /* }}} */
|
2016-11-07 07:52:46 +00:00
|
|
|
|
}
|
|
|
|
|
|