123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- <?php
- /**
- * @author Lukas Reschke <lukas@owncloud.com>
- * @author Morris Jobke <hey@morrisjobke.de>
- * @author Olivier Paroz <github@oparoz.com>
- * @author Robin Appelman <icewind@owncloud.com>
- *
- * @copyright Copyright (c) 2015, ownCloud, Inc.
- * @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,
- * along with this program. If not, see <http://www.gnu.org/licenses/>
- *
- */
- namespace OC;
- use OCP\ILogger;
- use OCP\ITempManager;
- class TempManager implements ITempManager {
- /** @var string[] Current temporary files and folders, used for cleanup */
- protected $current = [];
- /** @var string i.e. /tmp on linux systems */
- protected $tmpBaseDir;
- /** @var ILogger */
- protected $log;
- /** Prefix */
- const TMP_PREFIX = 'oc_tmp_';
- /**
- * @param string $baseDir
- * @param \OCP\ILogger $logger
- */
- public function __construct($baseDir, ILogger $logger) {
- $this->tmpBaseDir = $baseDir;
- $this->log = $logger;
- }
- /**
- * 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
- * @return string
- */
- private function buildFileNameWithSuffix($absolutePath, $postFix = '') {
- if($postFix !== '') {
- $postFix = '.' . ltrim($postFix, '.');
- $postFix = str_replace(['\\', '/'], '', $postFix);
- $absolutePath .= '-';
- }
- return $absolutePath . $postFix;
- }
- /**
- * Create a temporary file and return the path
- *
- * @param string $postFix Postfix appended to the temporary file name
- * @return string
- */
- public function getTemporaryFile($postFix = '') {
- if (is_writable($this->tmpBaseDir)) {
- // 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);
- $this->current[] = $file;
- // If a postfix got specified sanitize it and create a postfixed
- // temporary file
- if($postFix !== '') {
- $fileNameWithPostfix = $this->buildFileNameWithSuffix($file, $postFix);
- touch($fileNameWithPostfix);
- chmod($fileNameWithPostfix, 0600);
- $this->current[] = $fileNameWithPostfix;
- return $fileNameWithPostfix;
- }
- return $file;
- } else {
- $this->log->warning(
- 'Can not create a temporary file in directory {dir}. Check it exists and has correct permissions',
- [
- 'dir' => $this->tmpBaseDir,
- ]
- );
- return false;
- }
- }
- /**
- * Create a temporary folder and return the path
- *
- * @param string $postFix Postfix appended to the temporary folder name
- * @return string
- */
- public function getTemporaryFolder($postFix = '') {
- if (is_writable($this->tmpBaseDir)) {
- // 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);
- $this->current[] = $path;
- return $path . '/';
- } else {
- $this->log->warning(
- 'Can not create a temporary folder in directory {dir}. Check it exists and has correct permissions',
- [
- 'dir' => $this->tmpBaseDir,
- ]
- );
- return false;
- }
- }
- /**
- * Remove the temporary files and folders generated during this request
- */
- public function clean() {
- $this->cleanFiles($this->current);
- }
- /**
- * @param string[] $files
- */
- 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}",
- [
- 'file' => $file,
- 'error' => $ex->getMessage(),
- ]
- );
- }
- }
- }
- }
- /**
- * 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;
- $files = [];
- $dh = opendir($this->tmpBaseDir);
- if ($dh) {
- while (($file = readdir($dh)) !== false) {
- if (substr($file, 0, 7) === self::TMP_PREFIX) {
- $path = $this->tmpBaseDir . '/' . $file;
- $mtime = filemtime($path);
- if ($mtime < $cutOfTime) {
- $files[] = $path;
- }
- }
- }
- }
- return $files;
- }
- }
|