|
| 1 | +<?php |
| 2 | + namespace Leaf\Core; |
| 3 | + use Leaf\Core\Http\Response; |
| 4 | + /** |
| 5 | + * Leaf PHP base controller |
| 6 | + * -------------------------- |
| 7 | + * Loads the model and views |
| 8 | + */ |
| 9 | + class Controller { |
| 10 | + public $response; |
| 11 | + public function __construct() { |
| 12 | + $this->$response = new Response; |
| 13 | + } |
| 14 | + |
| 15 | + /** |
| 16 | + * Submit JSON encoded data to user |
| 17 | + * |
| 18 | + * @param array $data: An array of data to be displayed to the user |
| 19 | + * |
| 20 | + * @return void |
| 21 | + */ |
| 22 | + public function respond($data) { |
| 23 | + $this->response->respond($data); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Submit JSON encoded data to user with an http code |
| 28 | + * |
| 29 | + * @param array $data: An array of data to be displayed to the user |
| 30 | + * @param integer $code: An array of data to be displayed to the user |
| 31 | + * |
| 32 | + * @return void |
| 33 | + */ |
| 34 | + public function respondWithCode($data, $code) { |
| 35 | + $this->response->respondWithCode($data, $code); |
| 36 | + } |
| 37 | + |
| 38 | + public function file_upload($path, $file, $file_category = "image"): Array { |
| 39 | + // get file details |
| 40 | + $name = strtolower(strtotime(date("Y-m-d H:i:s")).'_'.str_replace(" ", "",$file["name"])); |
| 41 | + $temp = $file["tmp_name"]; |
| 42 | + $size = $file["size"]; |
| 43 | + |
| 44 | + $target_dir = $path; // destination path |
| 45 | + $target_file = $target_dir . basename($name); // destination file |
| 46 | + $upload_ok = true; // upload checker |
| 47 | + $file_type = strtolower(pathinfo($target_file, PATHINFO_EXTENSION)); // file type |
| 48 | + |
| 49 | + |
| 50 | + if (!file_exists($path)): |
| 51 | + mkdir($path, 0777, true); |
| 52 | + endif; |
| 53 | + |
| 54 | + // Check if file already exists |
| 55 | + if (file_exists($target_file)) { |
| 56 | + return [true, $name]; |
| 57 | + } |
| 58 | + // Check file size |
| 59 | + if ($size > 2000000) { |
| 60 | + return [false, "file too big"]; |
| 61 | + } |
| 62 | + // Allow certain file formats |
| 63 | + switch ($file_category) { |
| 64 | + case 'image': |
| 65 | + $extensions = ['jpg', 'jpeg', 'png', 'gif']; |
| 66 | + break; |
| 67 | + |
| 68 | + case 'audio': |
| 69 | + $extensions = ['wav', 'mp3', 'ogg', 'wav', 'm4a']; |
| 70 | + break; |
| 71 | + } |
| 72 | + |
| 73 | + if (!in_array($file_type, $extensions)) { |
| 74 | + return [false, $file['name']." format not acceptable"]; |
| 75 | + } |
| 76 | + // Check if $upload_ok is set to 0 by an error |
| 77 | + if (move_uploaded_file($temp, $target_file)) { |
| 78 | + return [true, $name]; |
| 79 | + } else { |
| 80 | + return [false, "Wasn't able to upload {$file_category}"]; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + function startsWith($haystack, $needle): Bool { |
| 85 | + $length = strlen($needle); |
| 86 | + return (substr($haystack, 0, $length) === $needle); |
| 87 | + } |
| 88 | + |
| 89 | + public function objectify($array): Object { |
| 90 | + return \json_decode(\json_encode($array)); |
| 91 | + } |
| 92 | + |
| 93 | + public function permit($array, $permitables): Array { |
| 94 | + $permits = []; |
| 95 | + |
| 96 | + foreach ($permitables as $key => $permitable) { |
| 97 | + $permits[$permitable] = $array[$permitable]; |
| 98 | + } |
| 99 | + |
| 100 | + return $permits; |
| 101 | + } |
| 102 | + } |
| 103 | + |
0 commit comments