diff options
Diffstat (limited to 'src/controller/ajax.php')
-rw-r--r-- | src/controller/ajax.php | 82 |
1 files changed, 81 insertions, 1 deletions
diff --git a/src/controller/ajax.php b/src/controller/ajax.php index 18f303b..943c027 100644 --- a/src/controller/ajax.php +++ b/src/controller/ajax.php | |||
@@ -7,10 +7,62 @@ use App\Entity\Page; | |||
7 | use App\Entity\Node; | 7 | use App\Entity\Node; |
8 | use App\Entity\Article; | 8 | use App\Entity\Article; |
9 | 9 | ||
10 | |||
11 | // mettre ça ailleurs | ||
12 | function imagickCleanImage(string $image_data, string $local_path): bool // "string" parce que file_get_contents... | ||
13 | { | ||
14 | try{ | ||
15 | $imagick = new Imagick(); | ||
16 | $imagick->readImageBlob($image_data); | ||
17 | $imagick->stripImage(); // nettoyage métadonnées | ||
18 | $imagick->setImageFormat('jpeg'); | ||
19 | $imagick->setImageCompression(Imagick::COMPRESSION_JPEG); | ||
20 | $imagick->setImageCompressionQuality(85); // optionnel | ||
21 | $imagick->writeImage($local_path); // enregistrement | ||
22 | $imagick->clear(); | ||
23 | $imagick->destroy(); | ||
24 | return true; | ||
25 | } | ||
26 | catch(Exception $e){ | ||
27 | return false; | ||
28 | } | ||
29 | } | ||
30 | function curlDownloadImage(string $url, $maxRetries = 3, $timeout = 10): string|false | ||
31 | { | ||
32 | $attempt = 0; | ||
33 | $imageData = false; | ||
34 | |||
35 | while($attempt < $maxRetries){ | ||
36 | $ch = curl_init($url); // instance de CurlHandle | ||
37 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | ||
38 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); | ||
39 | curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); | ||
40 | curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); | ||
41 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); | ||
42 | curl_setopt($ch, CURLOPT_USERAGENT, 'TinyMCE-Image-Downloader'); | ||
43 | |||
44 | $imageData = curl_exec($ch); | ||
45 | $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); | ||
46 | //$curlError = curl_error($ch); | ||
47 | |||
48 | curl_close($ch); | ||
49 | |||
50 | if($imageData !== false && $httpCode >= 200 && $httpCode < 300){ | ||
51 | return $imageData; | ||
52 | } | ||
53 | |||
54 | $attempt++; | ||
55 | sleep(1); | ||
56 | } | ||
57 | |||
58 | return false; // échec après trois tentatives | ||
59 | } | ||
60 | |||
61 | |||
10 | // détection des requêtes d'upload d'image de tinymce | 62 | // détection des requêtes d'upload d'image de tinymce |
11 | if(strpos($_SERVER['CONTENT_TYPE'], 'multipart/form-data') !== false && isset($_GET['action']) && $_GET['action'] === 'upload_image') | 63 | if(strpos($_SERVER['CONTENT_TYPE'], 'multipart/form-data') !== false && isset($_GET['action']) && $_GET['action'] === 'upload_image') |
12 | { | 64 | { |
13 | if (isset($_FILES['file'])) { | 65 | if(isset($_FILES['file'])){ |
14 | $file = $_FILES['file']; | 66 | $file = $_FILES['file']; |
15 | $dest = 'images/'; | 67 | $dest = 'images/'; |
16 | $dest_mini = 'images-mini/'; | 68 | $dest_mini = 'images-mini/'; |
@@ -26,6 +78,7 @@ if(strpos($_SERVER['CONTENT_TYPE'], 'multipart/form-data') !== false && isset($_ | |||
26 | $filePath = $dest . basename($file['name']); | 78 | $filePath = $dest . basename($file['name']); |
27 | 79 | ||
28 | // créer une miniature de l'image | 80 | // créer une miniature de l'image |
81 | // | ||
29 | 82 | ||
30 | if(move_uploaded_file($file['tmp_name'], $filePath)) { | 83 | if(move_uploaded_file($file['tmp_name'], $filePath)) { |
31 | $image_url = str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']); | 84 | $image_url = str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']); |
@@ -42,6 +95,33 @@ if(strpos($_SERVER['CONTENT_TYPE'], 'multipart/form-data') !== false && isset($_ | |||
42 | } | 95 | } |
43 | die; | 96 | die; |
44 | } | 97 | } |
98 | // cas du collage d'un contenu HTML, réception d'une URL, téléchargement par le serveur et renvoie de l'adresse sur le serveur | ||
99 | elseif(isset($_GET['action']) && $_GET['action'] == 'upload_image_url'){ | ||
100 | $json = json_decode(file_get_contents('php://input'), true); | ||
101 | |||
102 | if(isset($json['image_url'])){ | ||
103 | $image_data = curlDownloadImage($json['image_url']); // téléchargement de l’image par le serveur avec cURL au lieu de file_get_contents | ||
104 | |||
105 | if($image_data === false){ | ||
106 | http_response_code(400); | ||
107 | echo json_encode(['message' => "Erreur, le serveur n'a pas réussi à télécharger l'image."]); | ||
108 | die; | ||
109 | } | ||
110 | |||
111 | $local_path = 'images/' . uniqid() . '.jpg'; | ||
112 | if(imagickCleanImage($image_data, $local_path)){ // recréer l’image pour la nettoyer | ||
113 | echo json_encode(['location' => $local_path]); // nouvelle adresse | ||
114 | } | ||
115 | else{ | ||
116 | http_response_code(500); | ||
117 | echo json_encode(['message' => 'Erreur image non valide']); | ||
118 | } | ||
119 | } | ||
120 | else{ | ||
121 | echo json_encode(['message' => 'Erreur 400: Bad Request']); | ||
122 | } | ||
123 | die; | ||
124 | } | ||
45 | 125 | ||
46 | 126 | ||
47 | // détection des requêtes de type XHR, y en a pas à priori | 127 | // détection des requêtes de type XHR, y en a pas à priori |