You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TempManager.php 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Lars <winnetou+github@catolic.de>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Martin Mattel <martin.mattel@diemattels.at>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Olivier Paroz <github@oparoz.com>
  12. * @author Robin Appelman <robin@icewind.nl>
  13. * @author Robin McCorkell <robin@mccorkell.me.uk>
  14. * @author Roeland Jago Douma <roeland@famdouma.nl>
  15. * @author Stefan Weil <sw@weilnetz.de>
  16. *
  17. * @license AGPL-3.0
  18. *
  19. * This code is free software: you can redistribute it and/or modify
  20. * it under the terms of the GNU Affero General Public License, version 3,
  21. * as published by the Free Software Foundation.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU Affero General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Affero General Public License, version 3,
  29. * along with this program. If not, see <http://www.gnu.org/licenses/>
  30. *
  31. */
  32. namespace OC;
  33. use bantu\IniGetWrapper\IniGetWrapper;
  34. use OCP\IConfig;
  35. use OCP\ITempManager;
  36. use Psr\Log\LoggerInterface;
  37. class TempManager implements ITempManager {
  38. /** @var string[] Current temporary files and folders, used for cleanup */
  39. protected $current = [];
  40. /** @var string i.e. /tmp on linux systems */
  41. protected $tmpBaseDir;
  42. /** @var LoggerInterface */
  43. protected $log;
  44. /** @var IConfig */
  45. protected $config;
  46. /** @var IniGetWrapper */
  47. protected $iniGetWrapper;
  48. /** Prefix */
  49. public const TMP_PREFIX = 'oc_tmp_';
  50. public function __construct(LoggerInterface $logger, IConfig $config, IniGetWrapper $iniGetWrapper) {
  51. $this->log = $logger;
  52. $this->config = $config;
  53. $this->iniGetWrapper = $iniGetWrapper;
  54. $this->tmpBaseDir = $this->getTempBaseDir();
  55. }
  56. /**
  57. * Builds the filename with suffix and removes potential dangerous characters
  58. * such as directory separators.
  59. *
  60. * @param string $absolutePath Absolute path to the file / folder
  61. * @param string $postFix Postfix appended to the temporary file name, may be user controlled
  62. * @return string
  63. */
  64. private function buildFileNameWithSuffix($absolutePath, $postFix = '') {
  65. if ($postFix !== '') {
  66. $postFix = '.' . ltrim($postFix, '.');
  67. $postFix = str_replace(['\\', '/'], '', $postFix);
  68. $absolutePath .= '-';
  69. }
  70. return $absolutePath . $postFix;
  71. }
  72. /**
  73. * Create a temporary file and return the path
  74. *
  75. * @param string $postFix Postfix appended to the temporary file name
  76. * @return string
  77. */
  78. public function getTemporaryFile($postFix = '') {
  79. if (is_writable($this->tmpBaseDir)) {
  80. // To create an unique file and prevent the risk of race conditions
  81. // or duplicated temporary files by other means such as collisions
  82. // we need to create the file using `tempnam` and append a possible
  83. // postfix to it later
  84. $file = tempnam($this->tmpBaseDir, self::TMP_PREFIX);
  85. $this->current[] = $file;
  86. // If a postfix got specified sanitize it and create a postfixed
  87. // temporary file
  88. if ($postFix !== '') {
  89. $fileNameWithPostfix = $this->buildFileNameWithSuffix($file, $postFix);
  90. touch($fileNameWithPostfix);
  91. chmod($fileNameWithPostfix, 0600);
  92. $this->current[] = $fileNameWithPostfix;
  93. return $fileNameWithPostfix;
  94. }
  95. return $file;
  96. } else {
  97. $this->log->warning(
  98. 'Can not create a temporary file in directory {dir}. Check it exists and has correct permissions',
  99. [
  100. 'dir' => $this->tmpBaseDir,
  101. ]
  102. );
  103. return false;
  104. }
  105. }
  106. /**
  107. * Create a temporary folder and return the path
  108. *
  109. * @param string $postFix Postfix appended to the temporary folder name
  110. * @return string
  111. */
  112. public function getTemporaryFolder($postFix = '') {
  113. if (is_writable($this->tmpBaseDir)) {
  114. // To create an unique directory and prevent the risk of race conditions
  115. // or duplicated temporary files by other means such as collisions
  116. // we need to create the file using `tempnam` and append a possible
  117. // postfix to it later
  118. $uniqueFileName = tempnam($this->tmpBaseDir, self::TMP_PREFIX);
  119. $this->current[] = $uniqueFileName;
  120. // Build a name without postfix
  121. $path = $this->buildFileNameWithSuffix($uniqueFileName . '-folder', $postFix);
  122. mkdir($path, 0700);
  123. $this->current[] = $path;
  124. return $path . '/';
  125. } else {
  126. $this->log->warning(
  127. 'Can not create a temporary folder in directory {dir}. Check it exists and has correct permissions',
  128. [
  129. 'dir' => $this->tmpBaseDir,
  130. ]
  131. );
  132. return false;
  133. }
  134. }
  135. /**
  136. * Remove the temporary files and folders generated during this request
  137. */
  138. public function clean() {
  139. $this->cleanFiles($this->current);
  140. }
  141. /**
  142. * @param string[] $files
  143. */
  144. protected function cleanFiles($files) {
  145. foreach ($files as $file) {
  146. if (file_exists($file)) {
  147. try {
  148. \OC_Helper::rmdirr($file);
  149. } catch (\UnexpectedValueException $ex) {
  150. $this->log->warning(
  151. "Error deleting temporary file/folder: {file} - Reason: {error}",
  152. [
  153. 'file' => $file,
  154. 'error' => $ex->getMessage(),
  155. ]
  156. );
  157. }
  158. }
  159. }
  160. }
  161. /**
  162. * Remove old temporary files and folders that were failed to be cleaned
  163. */
  164. public function cleanOld() {
  165. $this->cleanFiles($this->getOldFiles());
  166. }
  167. /**
  168. * Get all temporary files and folders generated by oc older than an hour
  169. *
  170. * @return string[]
  171. */
  172. protected function getOldFiles() {
  173. $cutOfTime = time() - 3600;
  174. $files = [];
  175. $dh = opendir($this->tmpBaseDir);
  176. if ($dh) {
  177. while (($file = readdir($dh)) !== false) {
  178. if (substr($file, 0, 7) === self::TMP_PREFIX) {
  179. $path = $this->tmpBaseDir . '/' . $file;
  180. $mtime = filemtime($path);
  181. if ($mtime < $cutOfTime) {
  182. $files[] = $path;
  183. }
  184. }
  185. }
  186. }
  187. return $files;
  188. }
  189. /**
  190. * Get the temporary base directory configured on the server
  191. *
  192. * @return string Path to the temporary directory or null
  193. * @throws \UnexpectedValueException
  194. */
  195. public function getTempBaseDir() {
  196. if ($this->tmpBaseDir) {
  197. return $this->tmpBaseDir;
  198. }
  199. $directories = [];
  200. if ($temp = $this->config->getSystemValue('tempdirectory', null)) {
  201. $directories[] = $temp;
  202. }
  203. if ($temp = $this->iniGetWrapper->get('upload_tmp_dir')) {
  204. $directories[] = $temp;
  205. }
  206. if ($temp = getenv('TMP')) {
  207. $directories[] = $temp;
  208. }
  209. if ($temp = getenv('TEMP')) {
  210. $directories[] = $temp;
  211. }
  212. if ($temp = getenv('TMPDIR')) {
  213. $directories[] = $temp;
  214. }
  215. if ($temp = sys_get_temp_dir()) {
  216. $directories[] = $temp;
  217. }
  218. foreach ($directories as $dir) {
  219. if ($this->checkTemporaryDirectory($dir)) {
  220. return $dir;
  221. }
  222. }
  223. $temp = tempnam(dirname(__FILE__), '');
  224. if (file_exists($temp)) {
  225. unlink($temp);
  226. return dirname($temp);
  227. }
  228. throw new \UnexpectedValueException('Unable to detect system temporary directory');
  229. }
  230. /**
  231. * Check if a temporary directory is ready for use
  232. *
  233. * @param mixed $directory
  234. * @return bool
  235. */
  236. private function checkTemporaryDirectory($directory) {
  237. // suppress any possible errors caused by is_writable
  238. // checks missing or invalid path or characters, wrong permissions etc
  239. try {
  240. if (is_writable($directory)) {
  241. return true;
  242. }
  243. } catch (\Exception $e) {
  244. }
  245. $this->log->warning('Temporary directory {dir} is not present or writable',
  246. ['dir' => $directory]
  247. );
  248. return false;
  249. }
  250. /**
  251. * Override the temporary base directory
  252. *
  253. * @param string $directory
  254. */
  255. public function overrideTempBaseDir($directory) {
  256. $this->tmpBaseDir = $directory;
  257. }
  258. }