2014-10-22 19:33:36 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2016-03-01 19:25:15 +03:00
|
|
|
* @author Lars <winnetou+github@catolic.de>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2015-10-05 21:54:56 +03:00
|
|
|
* @author Martin Mattel <martin.mattel@diemattels.at>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2015-06-25 12:43:55 +03:00
|
|
|
* @author Olivier Paroz <github@oparoz.com>
|
2016-07-21 19:13:36 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-01-12 17:02:16 +03:00
|
|
|
* @author Robin McCorkell <robin@mccorkell.me.uk>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Stefan Weil <sw@weilnetz.de>
|
2015-03-26 13:44:34 +03:00
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* 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 Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-03-26 13:44:34 +03:00
|
|
|
*
|
2014-10-22 19:33:36 +04:00
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2014-10-22 19:33:36 +04:00
|
|
|
namespace OC;
|
|
|
|
|
2015-08-29 19:36:21 +03:00
|
|
|
use OCP\IConfig;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCP\ILogger;
|
2014-10-22 19:33:36 +04:00
|
|
|
use OCP\ITempManager;
|
|
|
|
|
|
|
|
class TempManager implements ITempManager {
|
2015-04-23 15:29:15 +03:00
|
|
|
/** @var string[] Current temporary files and folders, used for cleanup */
|
|
|
|
protected $current = [];
|
|
|
|
/** @var string i.e. /tmp on linux systems */
|
2014-10-22 19:33:36 +04:00
|
|
|
protected $tmpBaseDir;
|
2015-04-23 15:29:15 +03:00
|
|
|
/** @var ILogger */
|
2014-10-22 19:33:36 +04:00
|
|
|
protected $log;
|
2015-08-29 19:36:21 +03:00
|
|
|
/** @var IConfig */
|
|
|
|
protected $config;
|
|
|
|
|
2015-04-23 15:29:15 +03:00
|
|
|
/** Prefix */
|
2020-04-10 17:54:27 +03:00
|
|
|
public const TMP_PREFIX = 'oc_tmp_';
|
2014-10-22 19:33:36 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \OCP\ILogger $logger
|
2015-08-29 19:36:21 +03:00
|
|
|
* @param \OCP\IConfig $config
|
2014-10-22 19:33:36 +04:00
|
|
|
*/
|
2015-08-29 19:36:21 +03:00
|
|
|
public function __construct(ILogger $logger, IConfig $config) {
|
2014-10-22 19:33:36 +04:00
|
|
|
$this->log = $logger;
|
2015-08-29 19:36:21 +03:00
|
|
|
$this->config = $config;
|
|
|
|
$this->tmpBaseDir = $this->getTempBaseDir();
|
2014-10-22 19:33:36 +04:00
|
|
|
}
|
|
|
|
|
2015-03-27 01:14:24 +03:00
|
|
|
/**
|
2015-04-23 15:29:15 +03:00
|
|
|
* Builds the filename with suffix and removes potential dangerous characters
|
|
|
|
* such as directory separators.
|
|
|
|
*
|
|
|
|
* @param string $absolutePath Absolute path to the file / folder
|
|
|
|
* @param string $postFix Postfix appended to the temporary file name, may be user controlled
|
2015-03-27 01:14:24 +03:00
|
|
|
* @return string
|
|
|
|
*/
|
2015-04-23 15:29:15 +03:00
|
|
|
private function buildFileNameWithSuffix($absolutePath, $postFix = '') {
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($postFix !== '') {
|
2015-03-19 17:35:36 +03:00
|
|
|
$postFix = '.' . ltrim($postFix, '.');
|
2015-04-23 15:29:15 +03:00
|
|
|
$postFix = str_replace(['\\', '/'], '', $postFix);
|
|
|
|
$absolutePath .= '-';
|
2015-03-19 17:35:36 +03:00
|
|
|
}
|
2015-04-23 15:29:15 +03:00
|
|
|
|
|
|
|
return $absolutePath . $postFix;
|
2014-10-22 19:33:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a temporary file and return the path
|
|
|
|
*
|
2015-04-23 15:29:15 +03:00
|
|
|
* @param string $postFix Postfix appended to the temporary file name
|
2014-10-22 19:33:36 +04:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getTemporaryFile($postFix = '') {
|
|
|
|
if (is_writable($this->tmpBaseDir)) {
|
2015-04-23 15:29:15 +03:00
|
|
|
// To create an unique file and prevent the risk of race conditions
|
|
|
|
// or duplicated temporary files by other means such as collisions
|
|
|
|
// we need to create the file using `tempnam` and append a possible
|
|
|
|
// postfix to it later
|
|
|
|
$file = tempnam($this->tmpBaseDir, self::TMP_PREFIX);
|
2014-10-22 19:33:36 +04:00
|
|
|
$this->current[] = $file;
|
2015-04-23 15:29:15 +03:00
|
|
|
|
|
|
|
// If a postfix got specified sanitize it and create a postfixed
|
|
|
|
// temporary file
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($postFix !== '') {
|
2015-04-23 15:29:15 +03:00
|
|
|
$fileNameWithPostfix = $this->buildFileNameWithSuffix($file, $postFix);
|
|
|
|
touch($fileNameWithPostfix);
|
|
|
|
chmod($fileNameWithPostfix, 0600);
|
|
|
|
$this->current[] = $fileNameWithPostfix;
|
|
|
|
return $fileNameWithPostfix;
|
|
|
|
}
|
|
|
|
|
2014-10-22 19:33:36 +04:00
|
|
|
return $file;
|
|
|
|
} else {
|
|
|
|
$this->log->warning(
|
|
|
|
'Can not create a temporary file in directory {dir}. Check it exists and has correct permissions',
|
2015-04-23 15:29:15 +03:00
|
|
|
[
|
|
|
|
'dir' => $this->tmpBaseDir,
|
|
|
|
]
|
2014-10-22 19:33:36 +04:00
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a temporary folder and return the path
|
|
|
|
*
|
2015-04-23 15:29:15 +03:00
|
|
|
* @param string $postFix Postfix appended to the temporary folder name
|
2014-10-22 19:33:36 +04:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getTemporaryFolder($postFix = '') {
|
|
|
|
if (is_writable($this->tmpBaseDir)) {
|
2015-04-23 15:29:15 +03:00
|
|
|
// To create an unique directory and prevent the risk of race conditions
|
|
|
|
// or duplicated temporary files by other means such as collisions
|
|
|
|
// we need to create the file using `tempnam` and append a possible
|
|
|
|
// postfix to it later
|
|
|
|
$uniqueFileName = tempnam($this->tmpBaseDir, self::TMP_PREFIX);
|
|
|
|
$this->current[] = $uniqueFileName;
|
|
|
|
|
|
|
|
// Build a name without postfix
|
|
|
|
$path = $this->buildFileNameWithSuffix($uniqueFileName . '-folder', $postFix);
|
|
|
|
mkdir($path, 0700);
|
2014-10-22 19:33:36 +04:00
|
|
|
$this->current[] = $path;
|
2015-04-23 15:29:15 +03:00
|
|
|
|
2014-10-22 19:33:36 +04:00
|
|
|
return $path . '/';
|
|
|
|
} else {
|
|
|
|
$this->log->warning(
|
|
|
|
'Can not create a temporary folder in directory {dir}. Check it exists and has correct permissions',
|
2015-04-23 15:29:15 +03:00
|
|
|
[
|
|
|
|
'dir' => $this->tmpBaseDir,
|
|
|
|
]
|
2014-10-22 19:33:36 +04:00
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the temporary files and folders generated during this request
|
|
|
|
*/
|
|
|
|
public function clean() {
|
|
|
|
$this->cleanFiles($this->current);
|
|
|
|
}
|
|
|
|
|
2015-04-23 15:29:15 +03:00
|
|
|
/**
|
|
|
|
* @param string[] $files
|
|
|
|
*/
|
2014-10-22 19:33:36 +04:00
|
|
|
protected function cleanFiles($files) {
|
|
|
|
foreach ($files as $file) {
|
|
|
|
if (file_exists($file)) {
|
|
|
|
try {
|
|
|
|
\OC_Helper::rmdirr($file);
|
|
|
|
} catch (\UnexpectedValueException $ex) {
|
|
|
|
$this->log->warning(
|
|
|
|
"Error deleting temporary file/folder: {file} - Reason: {error}",
|
2015-04-23 15:29:15 +03:00
|
|
|
[
|
2014-10-22 19:33:36 +04:00
|
|
|
'file' => $file,
|
2015-04-23 15:29:15 +03:00
|
|
|
'error' => $ex->getMessage(),
|
|
|
|
]
|
2014-10-22 19:33:36 +04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove old temporary files and folders that were failed to be cleaned
|
|
|
|
*/
|
|
|
|
public function cleanOld() {
|
|
|
|
$this->cleanFiles($this->getOldFiles());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all temporary files and folders generated by oc older than an hour
|
|
|
|
*
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
protected function getOldFiles() {
|
|
|
|
$cutOfTime = time() - 3600;
|
2015-04-23 15:29:15 +03:00
|
|
|
$files = [];
|
2014-10-22 19:33:36 +04:00
|
|
|
$dh = opendir($this->tmpBaseDir);
|
2015-02-09 14:36:18 +03:00
|
|
|
if ($dh) {
|
|
|
|
while (($file = readdir($dh)) !== false) {
|
2015-04-23 15:29:15 +03:00
|
|
|
if (substr($file, 0, 7) === self::TMP_PREFIX) {
|
2015-02-09 14:36:18 +03:00
|
|
|
$path = $this->tmpBaseDir . '/' . $file;
|
|
|
|
$mtime = filemtime($path);
|
|
|
|
if ($mtime < $cutOfTime) {
|
|
|
|
$files[] = $path;
|
|
|
|
}
|
2014-10-22 19:33:36 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $files;
|
|
|
|
}
|
2015-05-29 22:56:08 +03:00
|
|
|
|
|
|
|
/**
|
2015-08-29 19:36:21 +03:00
|
|
|
* Get the temporary base directory configured on the server
|
|
|
|
*
|
|
|
|
* @return string Path to the temporary directory or null
|
|
|
|
* @throws \UnexpectedValueException
|
2015-05-29 22:56:08 +03:00
|
|
|
*/
|
2015-08-29 19:36:21 +03:00
|
|
|
public function getTempBaseDir() {
|
|
|
|
if ($this->tmpBaseDir) {
|
|
|
|
return $this->tmpBaseDir;
|
|
|
|
}
|
2015-05-29 22:56:08 +03:00
|
|
|
|
2015-08-29 19:36:21 +03:00
|
|
|
$directories = [];
|
|
|
|
if ($temp = $this->config->getSystemValue('tempdirectory', null)) {
|
|
|
|
$directories[] = $temp;
|
|
|
|
}
|
2015-10-29 11:52:13 +03:00
|
|
|
if ($temp = \OC::$server->getIniWrapper()->get('upload_tmp_dir')) {
|
2015-08-29 19:36:21 +03:00
|
|
|
$directories[] = $temp;
|
|
|
|
}
|
|
|
|
if ($temp = getenv('TMP')) {
|
|
|
|
$directories[] = $temp;
|
|
|
|
}
|
|
|
|
if ($temp = getenv('TEMP')) {
|
|
|
|
$directories[] = $temp;
|
|
|
|
}
|
|
|
|
if ($temp = getenv('TMPDIR')) {
|
|
|
|
$directories[] = $temp;
|
|
|
|
}
|
|
|
|
if ($temp = sys_get_temp_dir()) {
|
|
|
|
$directories[] = $temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($directories as $dir) {
|
|
|
|
if ($this->checkTemporaryDirectory($dir)) {
|
|
|
|
return $dir;
|
|
|
|
}
|
|
|
|
}
|
2015-12-24 02:20:30 +03:00
|
|
|
|
|
|
|
$temp = tempnam(dirname(__FILE__), '');
|
|
|
|
if (file_exists($temp)) {
|
|
|
|
unlink($temp);
|
|
|
|
return dirname($temp);
|
|
|
|
}
|
2015-08-29 19:36:21 +03:00
|
|
|
throw new \UnexpectedValueException('Unable to detect system temporary directory');
|
2015-05-29 22:56:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-29 19:36:21 +03:00
|
|
|
* Check if a temporary directory is ready for use
|
|
|
|
*
|
|
|
|
* @param mixed $directory
|
|
|
|
* @return bool
|
2015-05-29 22:56:08 +03:00
|
|
|
*/
|
2015-08-29 19:36:21 +03:00
|
|
|
private function checkTemporaryDirectory($directory) {
|
2016-04-07 20:51:27 +03:00
|
|
|
// suppress any possible errors caused by is_writable
|
|
|
|
// checks missing or invalid path or characters, wrong permissions etc
|
2015-08-29 19:36:21 +03:00
|
|
|
try {
|
2017-07-23 22:03:26 +03:00
|
|
|
if (is_writable($directory)) {
|
2015-08-29 19:36:21 +03:00
|
|
|
return true;
|
2015-05-29 22:56:08 +03:00
|
|
|
}
|
2016-01-10 01:43:12 +03:00
|
|
|
} catch (\Exception $e) {
|
2015-05-29 22:56:08 +03:00
|
|
|
}
|
2015-08-29 19:36:21 +03:00
|
|
|
$this->log->warning('Temporary directory {dir} is not present or writable',
|
|
|
|
['dir' => $directory]
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Override the temporary base directory
|
|
|
|
*
|
|
|
|
* @param string $directory
|
|
|
|
*/
|
|
|
|
public function overrideTempBaseDir($directory) {
|
|
|
|
$this->tmpBaseDir = $directory;
|
2015-05-29 22:56:08 +03:00
|
|
|
}
|
2014-10-22 19:33:36 +04:00
|
|
|
}
|