login/edit: move upload handling code into include
authorMischa POSLAWSKY <perl@shiar.org>
Wed, 11 Jul 2018 19:09:21 +0000 (21:09 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Sat, 14 Jul 2018 11:47:55 +0000 (13:47 +0200)
login/edit.php
upload.inc.php [new file with mode: 0644]

index 15b0a2a7dbebd8ded892b98a882bd9d5ee1cef8a..101244afbb42aab0e5d55a47072f2d879078e57c 100644 (file)
@@ -123,27 +123,19 @@ if ($_POST) {
                if (!isset($cols[$col]) and @$cols[$col]['type'] == 'file') {
                        continue; # unknown
                }
-               switch ($val['error']) {
-               case UPLOAD_ERR_OK:
-                       break;
-               case UPLOAD_ERR_NO_FILE:
-                       continue 2; # current
-               default:
-                       $colwarn[$col] = "Afbeelding niet goed ontvangen.";
-                       continue 2;
-               }
                if (empty($cols[$col]['target'])) {
                        $colwarn[$col] = "Kan niet worden aangepast.";
                        continue;
                }
-               if (!@move_uploaded_file($val['tmp_name'], $cols[$col]['target'])) {
-                       $colwarn[$col] = "Fout bij opslaan.";
+               try {
+                       require_once('upload.inc.php');
+                       $target = userupload($val, $cols[$col]['target']);
+                       if (!$target) continue;
+                       $cols[$col]['value'] = '';
                }
-               foreach (@glob('thumb/*/') as $thumbres) {
-                       # attempt to remove old derivations
-                       @unlink($thumbres.'/'.$cols[$col]['target']);
+               catch (Exception $e) {
+                       $colwarn[$col] = $e->getMessage();
                }
-               $cols[$col]['value'] = '';
        }
 
        if (!empty($_POST['newpass'])) {
diff --git a/upload.inc.php b/upload.inc.php
new file mode 100644 (file)
index 0000000..aa24558
--- /dev/null
@@ -0,0 +1,21 @@
+<?php
+function userupload($input, $target)
+{
+       switch ($input['error']) {
+       case UPLOAD_ERR_OK:
+               break;
+       case UPLOAD_ERR_NO_FILE:
+               return; # current
+       default:
+               throw new Exception("Afbeelding niet goed ontvangen.");
+       }
+       if (!@move_uploaded_file($input['tmp_name'], $target)) {
+               throw new Exception("Fout bij opslaan.");
+       }
+
+       foreach (@glob('thumb/*/') as $thumbres) {
+               # attempt to remove old derivations
+               @unlink($thumbres . '/' . $target);
+       }
+       return $target;
+}