diff options
| author | polo <ordipolo@gmx.fr> | 2025-10-23 14:15:01 +0200 |
|---|---|---|
| committer | polo <ordipolo@gmx.fr> | 2025-10-23 14:15:01 +0200 |
| commit | f9a9bf1ae4fac9317fd71f0bcb207544b000e6f1 (patch) | |
| tree | a0ca5cdc0374529b80951d7d126f1c49442bae68 /src/controller | |
| parent | db5521f7b194190013dd0ad606a018ab68464562 (diff) | |
| download | cms-f9a9bf1ae4fac9317fd71f0bcb207544b000e6f1.zip | |
téléchargement d'assets dans head, header et footer
Diffstat (limited to 'src/controller')
| -rw-r--r-- | src/controller/HeadFootController.php | 60 | ||||
| -rw-r--r-- | src/controller/ImageUploadController.php | 22 |
2 files changed, 65 insertions, 17 deletions
diff --git a/src/controller/HeadFootController.php b/src/controller/HeadFootController.php index 8d59d10..0429aac 100644 --- a/src/controller/HeadFootController.php +++ b/src/controller/HeadFootController.php | |||
| @@ -5,23 +5,24 @@ declare(strict_types=1); | |||
| 5 | 5 | ||
| 6 | //use App\Entity\Node; | 6 | //use App\Entity\Node; |
| 7 | //use App\Entity\NodeData; | 7 | //use App\Entity\NodeData; |
| 8 | //use App\Entity\Image; | 8 | use App\Entity\Asset; |
| 9 | //use Doctrine\Common\Collections\ArrayCollection; | 9 | use Doctrine\Common\Collections\ArrayCollection; |
| 10 | use Doctrine\ORM\EntityManager; | 10 | use Doctrine\ORM\EntityManager; |
| 11 | 11 | ||
| 12 | class HeadFootController | 12 | class HeadFootController |
| 13 | { | 13 | { |
| 14 | static public function setTextData(EntityManager $entityManager, array $request_params, array $json): void | 14 | static public function setTextData(EntityManager $entityManager, string $request_params, array $json): void |
| 15 | { | 15 | { |
| 16 | if(count($request_params) !== 2){ | 16 | $params_array = explode('_', $request_params); // header_title, header_description, footer_name, footer_address, footer_email |
| 17 | if(count($params_array) !== 2){ | ||
| 17 | echo json_encode(['success' => false]); | 18 | echo json_encode(['success' => false]); |
| 18 | die; | 19 | die; |
| 19 | } | 20 | } |
| 20 | 21 | ||
| 21 | $model = new Model($entityManager); | 22 | $model = new Model($entityManager); |
| 22 | if($model->findWhateverNode('name_node', $request_params[0])){ | 23 | if($model->findWhateverNode('name_node', $params_array[0])){ |
| 23 | $node_data = $model->getNode()->getNodeData(); | 24 | $node_data = $model->getNode()->getNodeData(); |
| 24 | $node_data->updateData($request_params[1], $json['new_text']); // $request_params[1] n'est pas contrôlé | 25 | $node_data->updateData($params_array[1], $json['new_text']); // $params_array[1] n'est pas contrôlé |
| 25 | $entityManager->flush(); | 26 | $entityManager->flush(); |
| 26 | echo json_encode(['success' => true]); | 27 | echo json_encode(['success' => true]); |
| 27 | } | 28 | } |
| @@ -30,4 +31,51 @@ class HeadFootController | |||
| 30 | } | 31 | } |
| 31 | die; | 32 | die; |
| 32 | } | 33 | } |
| 34 | static public function uploadAsset(EntityManager $entityManager, string $request_params): void | ||
| 35 | { | ||
| 36 | if(empty($_FILES)){ | ||
| 37 | http_response_code(400); | ||
| 38 | echo json_encode(['success' => false]); | ||
| 39 | } | ||
| 40 | else{ | ||
| 41 | $file = $_FILES['file']; | ||
| 42 | |||
| 43 | if(!is_dir(Asset::USER_PATH)){ | ||
| 44 | mkdir(Asset::USER_PATH, 0700, true); | ||
| 45 | } | ||
| 46 | |||
| 47 | $allowed_extensions = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'tiff', 'tif', 'ico', 'bmp']; // pas de SVG | ||
| 48 | $name = Security::secureFileName(pathinfo($file['name'], PATHINFO_FILENAME)); | ||
| 49 | $extension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION)); | ||
| 50 | if(!in_array($extension, $allowed_extensions) || $extension === 'jpg'){ | ||
| 51 | $extension = 'jpeg'; | ||
| 52 | } | ||
| 53 | $file_path = uniqid($name . '_') . '.' . $extension; | ||
| 54 | |||
| 55 | if(ImageUploadController::imagickCleanImage(file_get_contents($file['tmp_name']), Asset::USER_PATH . $file_path, $extension)){ // recréer l’image pour la nettoyer | ||
| 56 | $params_array = explode('_', $request_params); // favicon, header_logo, header_background, footer_logo | ||
| 57 | |||
| 58 | $model = new Model($entityManager); | ||
| 59 | if($model->findWhateverNode('name_node', $params_array[0])){ | ||
| 60 | $node_data = $model->getNode()->getNodeData(); | ||
| 61 | $image = new Asset($name, $file_path, mime_content_type($file['tmp_name']), $request_params); | ||
| 62 | $node_data->addAsset($image); | ||
| 63 | |||
| 64 | $entityManager->persist($image); | ||
| 65 | $entityManager->flush(); | ||
| 66 | echo json_encode(['success' => true, 'location' => Asset::USER_PATH . $file_path]); | ||
| 67 | } | ||
| 68 | else{ | ||
| 69 | echo json_encode(['success' => false, 'message' => 'Erreur noeud non trouvé.']); | ||
| 70 | } | ||
| 71 | } | ||
| 72 | else{ | ||
| 73 | http_response_code(500); | ||
| 74 | echo json_encode(['success' => false, 'message' => 'Erreur image non valide.']); | ||
| 75 | } | ||
| 76 | } | ||
| 77 | die; | ||
| 78 | } | ||
| 79 | |||
| 80 | //static public function uploadImage(EntityManager $entityManager, array $request_params): void | ||
| 33 | } \ No newline at end of file | 81 | } \ No newline at end of file |
diff --git a/src/controller/ImageUploadController.php b/src/controller/ImageUploadController.php index 29b8059..5e80ba5 100644 --- a/src/controller/ImageUploadController.php +++ b/src/controller/ImageUploadController.php | |||
| @@ -61,14 +61,14 @@ class ImageUploadController | |||
| 61 | { | 61 | { |
| 62 | if(isset($_FILES['file'])){ | 62 | if(isset($_FILES['file'])){ |
| 63 | $file = $_FILES['file']; | 63 | $file = $_FILES['file']; |
| 64 | $dest = 'images/'; | 64 | $dest = 'user_data/images/'; |
| 65 | $dest_mini = 'images-mini/'; | 65 | $dest_mini = 'user_data/images-mini/'; |
| 66 | 66 | ||
| 67 | // Vérifier si les répertoires existent, sinon les créer | 67 | // Vérifier si les répertoires existent, sinon les créer |
| 68 | if(!is_dir($dest)) { | 68 | if(!is_dir($dest)){ |
| 69 | mkdir($dest, 0700, true); | 69 | mkdir($dest, 0700, true); |
| 70 | } | 70 | } |
| 71 | if(!is_dir($dest_mini)) { | 71 | if(!is_dir($dest_mini)){ |
| 72 | mkdir($dest_mini, 0700, true); | 72 | mkdir($dest_mini, 0700, true); |
| 73 | } | 73 | } |
| 74 | 74 | ||
| @@ -78,7 +78,7 @@ class ImageUploadController | |||
| 78 | if(!in_array($extension, $allowed_extensions) || $extension === 'jpg'){ | 78 | if(!in_array($extension, $allowed_extensions) || $extension === 'jpg'){ |
| 79 | $extension = 'jpeg'; | 79 | $extension = 'jpeg'; |
| 80 | } | 80 | } |
| 81 | $file_path = $dest . $name . '_' . uniqid() . '.' . $extension; | 81 | $file_path = uniqid($dest . $name . '_') . '.' . $extension; |
| 82 | 82 | ||
| 83 | // créer une miniature de l'image | 83 | // créer une miniature de l'image |
| 84 | // | 84 | // |
| @@ -105,7 +105,7 @@ class ImageUploadController | |||
| 105 | 105 | ||
| 106 | if(isset($json['image_url'])){ | 106 | if(isset($json['image_url'])){ |
| 107 | $image_data = self::curlDownloadImage($json['image_url']); // téléchargement de l’image par le serveur avec cURL au lieu de file_get_contents | 107 | $image_data = self::curlDownloadImage($json['image_url']); // téléchargement de l’image par le serveur avec cURL au lieu de file_get_contents |
| 108 | $dest = 'images/'; | 108 | $dest = 'user_data/images/'; |
| 109 | 109 | ||
| 110 | if(!is_dir($dest)) { // Vérifier si le répertoire existe, sinon le créer | 110 | if(!is_dir($dest)) { // Vérifier si le répertoire existe, sinon le créer |
| 111 | mkdir($dest, 0777, true); | 111 | mkdir($dest, 0777, true); |
| @@ -124,7 +124,7 @@ class ImageUploadController | |||
| 124 | if(!in_array($extension, $allowed_extensions) || $extension === 'jpg'){ | 124 | if(!in_array($extension, $allowed_extensions) || $extension === 'jpg'){ |
| 125 | $extension = 'jpeg'; | 125 | $extension = 'jpeg'; |
| 126 | } | 126 | } |
| 127 | $local_path = $dest . $name . '_' . uniqid() . '.' . $extension; | 127 | $local_path = uniqid($dest . $name . '_') . '.' . $extension; |
| 128 | 128 | ||
| 129 | if(self::imagickCleanImage($image_data, $local_path, $extension)){ // recréer l’image pour la nettoyer | 129 | if(self::imagickCleanImage($image_data, $local_path, $extension)){ // recréer l’image pour la nettoyer |
| 130 | echo json_encode(['location' => $local_path]); // nouvelle adresse | 130 | echo json_encode(['location' => $local_path]); // nouvelle adresse |
| @@ -144,10 +144,10 @@ class ImageUploadController | |||
| 144 | static public function uploadImageBase64(): void | 144 | static public function uploadImageBase64(): void |
| 145 | { | 145 | { |
| 146 | $json = json_decode(file_get_contents('php://input'), true); | 146 | $json = json_decode(file_get_contents('php://input'), true); |
| 147 | $dest = 'images/'; | 147 | $dest = 'user_data/images/'; |
| 148 | 148 | ||
| 149 | if(!is_dir('images')){ | 149 | if(!is_dir($dest)){ |
| 150 | mkdir('images', 0777, true); | 150 | mkdir($dest, 0777, true); |
| 151 | } | 151 | } |
| 152 | 152 | ||
| 153 | // détection de data:image/ et de ;base64, et capture du format dans $type | 153 | // détection de data:image/ et de ;base64, et capture du format dans $type |
| @@ -170,7 +170,7 @@ class ImageUploadController | |||
| 170 | die; | 170 | die; |
| 171 | } | 171 | } |
| 172 | 172 | ||
| 173 | $local_path = $dest . 'pasted_image_' . uniqid() . '.' . $extension; | 173 | $local_path = uniqid($dest . 'pasted_image_') . '.' . $extension; |
| 174 | 174 | ||
| 175 | if(self::imagickCleanImage($image_data, $local_path)){ | 175 | if(self::imagickCleanImage($image_data, $local_path)){ |
| 176 | echo json_encode(['location' => $local_path]); | 176 | echo json_encode(['location' => $local_path]); |
