- commit
- 24aad1744fb9f24e28ab8767e2e36ca0383f8364
- parent
- 1084f7c3c8ad6ab28e067faa7c9269a9d2a35ec9
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2019-01-04 10:02
catch write errors
Diffstat
| M | api.php | 2 | +- |
| M | index.php | 2 | ++ |
| M | utils.php | 40 | ++++++++++++++++++++++++++++++++++------ |
3 files changed, 37 insertions, 7 deletions
diff --git a/api.php b/api.php
@@ -134,7 +134,7 @@ class Pupupu 134 134 { 135 135 $p = $this->targetDir . '/files' . $path . '/' . $file['name']; 136 136 mkdirp(dirname($p));137 -1 move_uploaded_file($file['tmp_name'], $p);-1 137 _move_uploaded_file($file['tmp_name'], $p); 138 138 } 139 139 140 140 public function createFileFolder($path, $name)
diff --git a/index.php b/index.php
@@ -44,6 +44,8 @@ if (isset($_SERVER['REQUEST_METHOD'])) {
44 44 } else {
45 45 pageView($pupupu, $twig);
46 46 }
-1 47 } catch (WriteException $e) {
-1 48 errorView($pupupu, $twig, new HttpException('unable to write: ' . $e->getMessage(), 500));
47 49 } catch (Twig_Error_Loader $e) {
48 50 errorView($pupupu, $twig, new HttpException($e->getMessage(), 500));
49 51 } catch (HttpException $e) {
diff --git a/utils.php b/utils.php
@@ -1,5 +1,7 @@ 1 1 <?php declare(strict_types=1); 2 2 -1 3 class WriteException extends Exception {} -1 4 3 5 function trans($s) 4 6 { 5 7 return $s; @@ -8,7 +10,10 @@ function trans($s) 8 10 function rmdirs($path) 9 11 { 10 12 if ($path !== '.' && is_dir($path)) {11 -1 rmdir($path);-1 13 $success = rmdir($path); -1 14 if ($success === false) { -1 15 throw new WriteException($path); -1 16 } 12 17 rmdirs(dirname($path)); 13 18 } 14 19 } @@ -16,7 +21,10 @@ function rmdirs($path) 16 21 function rmfile($path) 17 22 { 18 23 if (file_exists($path)) {19 -1 unlink($path);-1 24 $success = unlink($path); -1 25 if ($success === false) { -1 26 throw new WriteException($path); -1 27 } 20 28 } 21 29 rmdirs(dirname($path)); 22 30 } @@ -30,9 +38,15 @@ function rmr($path) 30 38 rmr("$path/$name"); 31 39 } 32 40 }33 -1 rmdir($path);-1 41 $success = rmdir($path); -1 42 if ($success === false) { -1 43 throw new WriteException($path); -1 44 } 34 45 } else {35 -1 unlink($path);-1 46 $success = unlink($path); -1 47 if ($success === false) { -1 48 throw new WriteException($path); -1 49 } 36 50 } 37 51 } 38 52 } @@ -40,14 +54,28 @@ function rmr($path) 40 54 function mkdirp($path) 41 55 { 42 56 if (!file_exists($path)) {43 -1 mkdir($path, 0777, true);-1 57 $success = mkdir($path, 0777, true); -1 58 if ($success === false) { -1 59 throw new WriteException($path); -1 60 } 44 61 } 45 62 } 46 63 47 64 function _file_put_contents($path, $content) 48 65 { 49 66 $normalized = preg_replace("/\r\n/", "\n", $content);50 -1 file_put_contents($path, $normalized);-1 67 $success = file_put_contents($path, $normalized); -1 68 if ($success === false) { -1 69 throw new WriteException($path); -1 70 } -1 71 } -1 72 -1 73 function _move_uploaded_file($tmp_name, $path) -1 74 { -1 75 $success = move_uploaded_file($tmp_name, $path); -1 76 if ($success === false) { -1 77 throw new WriteException($path); -1 78 } 51 79 } 52 80 53 81 function pathDirname($path)