From 12c11b65baed7504113cfbaf1503909fe7a8e504 Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 26 Oct 2023 13:46:55 +0200 Subject: [PATCH] doggo upload --- web/inc/helpers.php | 28 ++++++++++++++++++ web/models/Dog.model.php | 1 + web/pages/dogs/controller.php | 55 ++++++++++++++++++++++++++++++++++- web/pages/dogs/dog.html | 35 ++++++++++++++++++++++ web/pages/dogs/edit.html | 17 +++++++++-- 5 files changed, 132 insertions(+), 4 deletions(-) create mode 100644 web/pages/dogs/dog.html diff --git a/web/inc/helpers.php b/web/inc/helpers.php index 31b5b3e..baabdc7 100644 --- a/web/inc/helpers.php +++ b/web/inc/helpers.php @@ -209,4 +209,32 @@ function uuid4($data = null) { // Output the 36 character UUID. return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4)); +} + +/* +* @param $path string Path to the file that should be uploaded +* @param $hash string Optional. File name we want on pictshare for the file +*/ +function pictshareUploadImage($path,$hash=false) +{ + if(!file_exists($path)) return false; + $request = curl_init('https://i.haschek.at/api/upload.php'); + + curl_setopt($request, CURLOPT_POST, true); + curl_setopt( + $request, + CURLOPT_POSTFIELDS, + array( + 'file' => curl_file_create($path), + 'hash'=>$hash + )); + + // output the response + curl_setopt($request, CURLOPT_RETURNTRANSFER, true); + $json = json_decode(curl_exec($request).PHP_EOL,true); + + // close the session + curl_close($request); + + return $json; } \ No newline at end of file diff --git a/web/models/Dog.model.php b/web/models/Dog.model.php index 4f12981..9822205 100644 --- a/web/models/Dog.model.php +++ b/web/models/Dog.model.php @@ -12,6 +12,7 @@ class Dog extends Model 'size' => ['type' => 'text'], //in cm 'birthday' => ['type' => 'text'], 'agility_size' => ['type' => 'text'], //S,M,I,L + 'photo' => ['type' => 'text'], 'active' => ['type' => 'int', 'default' => 1] ); diff --git a/web/pages/dogs/controller.php b/web/pages/dogs/controller.php index 4f2476c..a4f8703 100644 --- a/web/pages/dogs/controller.php +++ b/web/pages/dogs/controller.php @@ -77,6 +77,53 @@ class Dogs extends Page { $dog_size = $_REQUEST['dog_size']; $dog_birthday = $_REQUEST['dog_birthday']; $agi_height_category = $_REQUEST['agi_height_category']; + $newphoto = false; + + if($_FILES['photo']) + { + $photo = $_FILES['photo']; + $photo_name = $photo['name']; + $photo_tmp_name = $photo['tmp_name']; + $photo_size = $photo['size']; + $photo_error = $photo['error']; + $photo_type = $photo['type']; + + $allowed = ['jpg','jpeg','png']; + $photo_ext = strtolower(end(explode('.', $photo_name))); + $photo_name = $name.'.'.$photo_ext; + $photo_path = 'uploads/'.$photo_name; + + if(in_array($photo_ext, $allowed)) + { + if($photo_error === 0) + { + if($photo_size < 10000000) + { + $answer = pictshareUploadImage($photo_tmp_name); + if($answer['status']=='ok' && in_array($answer['filetype'],['jpeg','png','gif'])) + $newphoto = $answer['url']; + } + else + { + $this->set('errorMessage', 'Die Datei ist zu groß. Bitte eine kleinere Datei hochladen'); + $this->set('template', '/templates/partials/error.html'); + return; + } + } + else + { + $this->set('errorMessage', 'Beim Upload der Datei ist ein Fehler aufgetreten'); + $this->set('template', '/templates/partials/error.html'); + return; + } + } + else + { + $this->set('errorMessage', 'Dieser Dateityp ist nicht erlaubt. Bitte nur jpg, jpeg oder png Dateien hochladen'); + $this->set('template', '/templates/partials/error.html'); + return; + } + } $error = false; if(!$name || !$dog_birthday ) @@ -101,6 +148,8 @@ class Dogs extends Page { $dog->size = $dog_size; $dog->birthday = $dog_birthday; $dog->agility_size = $agi_height_category; + if($newphoto) + $dog->photo = $newphoto; try { @@ -160,7 +209,11 @@ class Dogs extends Page { if(!$d->isMyDog($dogid)) return 'Not your dog :('; - return 'hier wird der hund angezeigt'; + + $d->load($dogid); + $this->set('dogdata', $d->data); + $this->set('dogid', $dogid); + $this->set('template', 'dog.html'); } function maySeeThisPage() { diff --git a/web/pages/dogs/dog.html b/web/pages/dogs/dog.html new file mode 100644 index 0000000..0bd9317 --- /dev/null +++ b/web/pages/dogs/dog.html @@ -0,0 +1,35 @@ + + +
+
+
+
+ <?= escape($dogdata['name']); ?>'s profile Picture +
+
+

+ +

    +
  • Rasse:
  • +
  • Alter: y ?>
  • + +
  • Zuchtname:
  • +
  • Größe: cm
  • +
  • Agility Größe:
  • +
+ +
+ +
+
+
+
+
+ Second Column +
+ +
+
+ diff --git a/web/pages/dogs/edit.html b/web/pages/dogs/edit.html index 7517db9..b14f4af 100644 --- a/web/pages/dogs/edit.html +++ b/web/pages/dogs/edit.html @@ -1,7 +1,7 @@
-

Hund hinzufügen/bearbeiten

+

Hund

-
+
@@ -32,7 +32,18 @@
+
+ + +
+
-
\ No newline at end of file + + + \ No newline at end of file