123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- <?php
- namespace OC;
- use bantu\IniGetWrapper\IniGetWrapper;
- use OCP\IConfig;
- use OCP\ITempManager;
- use Psr\Log\LoggerInterface;
- class TempManager implements ITempManager {
-
- protected $current = [];
-
- protected $tmpBaseDir;
-
- protected $log;
-
- protected $config;
-
- protected $iniGetWrapper;
-
- public const TMP_PREFIX = 'oc_tmp_';
- public function __construct(LoggerInterface $logger, IConfig $config, IniGetWrapper $iniGetWrapper) {
- $this->log = $logger;
- $this->config = $config;
- $this->iniGetWrapper = $iniGetWrapper;
- $this->tmpBaseDir = $this->getTempBaseDir();
- }
-
- private function buildFileNameWithSuffix($absolutePath, $postFix = '') {
- if ($postFix !== '') {
- $postFix = '.' . ltrim($postFix, '.');
- $postFix = str_replace(['\\', '/'], '', $postFix);
- $absolutePath .= '-';
- }
- return $absolutePath . $postFix;
- }
-
- public function getTemporaryFile($postFix = '') {
- if (is_writable($this->tmpBaseDir)) {
-
-
-
-
- $file = tempnam($this->tmpBaseDir, self::TMP_PREFIX);
- $this->current[] = $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;
- }
- }
-
- public function getTemporaryFolder($postFix = '') {
- if (is_writable($this->tmpBaseDir)) {
-
-
-
-
- $uniqueFileName = tempnam($this->tmpBaseDir, self::TMP_PREFIX);
- $this->current[] = $uniqueFileName;
-
- $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;
- }
- }
-
- public function clean() {
- $this->cleanFiles($this->current);
- }
-
- 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(),
- ]
- );
- }
- }
- }
- }
-
- public function cleanOld() {
- $this->cleanFiles($this->getOldFiles());
- }
-
- 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;
- }
-
- public function getTempBaseDir() {
- if ($this->tmpBaseDir) {
- return $this->tmpBaseDir;
- }
- $directories = [];
- if ($temp = $this->config->getSystemValue('tempdirectory', null)) {
- $directories[] = $temp;
- }
- if ($temp = $this->iniGetWrapper->get('upload_tmp_dir')) {
- $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;
- }
- }
- $temp = tempnam(dirname(__FILE__), '');
- if (file_exists($temp)) {
- unlink($temp);
- return dirname($temp);
- }
- throw new \UnexpectedValueException('Unable to detect system temporary directory');
- }
-
- private function checkTemporaryDirectory($directory) {
-
-
- try {
- if (is_writable($directory)) {
- return true;
- }
- } catch (\Exception $e) {
- }
- $this->log->warning('Temporary directory {dir} is not present or writable',
- ['dir' => $directory]
- );
- return false;
- }
-
- public function overrideTempBaseDir($directory) {
- $this->tmpBaseDir = $directory;
- }
- }
|