tempfile.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * @package GNUsocial
  18. * @author Alexei Sorokin <sor.alexei@meowr.ru>
  19. * @copyright 2020 Free Software Foundation, Inc http://www.fsf.org
  20. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  21. */
  22. defined('GNUSOCIAL') || die();
  23. /**
  24. * Exception wrapper for TemporaryFile errors
  25. *
  26. * @package GNUsocial
  27. * @author Alexei Sorokin <sor.alexei@meowr.ru>
  28. * @copyright 2020 Free Software Foundation, Inc http://www.fsf.org
  29. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  30. */
  31. class TemporaryFileException extends Exception
  32. {
  33. }
  34. /**
  35. * Class oriented at providing automatic temporary file handling.
  36. *
  37. * @package GNUsocial
  38. * @author Alexei Sorokin <sor.alexei@meowr.ru>
  39. * @copyright 2020 Free Software Foundation, Inc http://www.fsf.org
  40. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  41. */
  42. class TemporaryFile extends SplFileInfo
  43. {
  44. protected $resource = null;
  45. /**
  46. * @param string|null $prefix The file name will begin with that prefix
  47. * ("php" by default)
  48. * @param string|null $mode File open mode ("w+b" by default)
  49. */
  50. public function __construct(
  51. ?string $prefix = null,
  52. ?string $mode = null
  53. ) {
  54. $filename = tempnam(sys_get_temp_dir(), $prefix ?? 'gs-php');
  55. if ($filename === false) {
  56. throw new TemporaryFileException('Could not create file: ' . $filename);
  57. }
  58. parent::__construct($filename);
  59. if (($this->resource = fopen($filename, $mode ?? 'w+b')) === false) {
  60. $this->cleanup();
  61. throw new TemporaryFileException('Could not open file: ' . $filename);
  62. }
  63. }
  64. public function __destruct()
  65. {
  66. $this->close();
  67. $this->cleanup();
  68. }
  69. /**
  70. * Closes the file descriptor if opened.
  71. *
  72. * @return bool Whether successful
  73. */
  74. protected function close(): bool
  75. {
  76. $ret = true;
  77. if (!is_null($this->resource)) {
  78. $ret = fclose($this->resource);
  79. }
  80. if ($ret) {
  81. $this->resource = null;
  82. }
  83. return $ret;
  84. }
  85. /**
  86. * Closes the file descriptor and removes the temporary file.
  87. *
  88. * @return void
  89. */
  90. protected function cleanup(): void
  91. {
  92. $path = $this->getRealPath();
  93. $this->close();
  94. if (file_exists($path)) {
  95. unlink($path);
  96. }
  97. }
  98. /**
  99. * Get the file resource.
  100. *
  101. * @return resource
  102. */
  103. public function getResource()
  104. {
  105. return $this->resource;
  106. }
  107. /**
  108. * Release the hold on the temporary file and move it to the desired
  109. * location, setting file permissions in the process.
  110. *
  111. * @param string File destination
  112. * @param int New file permissions (in octal mode)
  113. * @return void
  114. * @throws TemporaryFileException
  115. */
  116. public function commit(string $destpath, int $umode = 0644): void
  117. {
  118. $temppath = $this->getRealPath();
  119. // Might be attempted, and won't end well
  120. if ($destpath === $temppath) {
  121. throw new TemporaryFileException('Cannot use self as destination');
  122. }
  123. // Memorise if the file was there and see if there is access
  124. $exists = file_exists($destpath);
  125. if (!touch($destpath)) {
  126. throw new TemporaryFileException(
  127. 'Insufficient permissions for destination: "' . $destpath . '"'
  128. );
  129. } elseif (!$exists) {
  130. // If the file wasn't there, clean it up in case of a later failure
  131. unlink($destpath);
  132. }
  133. if (!$this->close()) {
  134. throw new TemporaryFileException('Could not close the resource');
  135. }
  136. rename($temppath, $destpath);
  137. chmod($destpath, $umode);
  138. }
  139. }