summaryrefslogtreecommitdiff
path: root/public/index.php
diff options
context:
space:
mode:
Diffstat (limited to 'public/index.php')
-rw-r--r--public/index.php60
1 files changed, 48 insertions, 12 deletions
diff --git a/public/index.php b/public/index.php
index d6179b1..c423162 100644
--- a/public/index.php
+++ b/public/index.php
@@ -18,6 +18,37 @@ function imagickCleanImage(string $image_data, string $local_path): bool // "str
18 } 18 }
19} 19}
20 20
21function curlDownloadImage(string $url, $maxRetries = 3, $timeout = 10): string|false
22{
23 $attempt = 0;
24 $imageData = false;
25
26 while($attempt < $maxRetries) {
27 $ch = curl_init($url); // instance de CurlHandle
28 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
29 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
30 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
31 curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
32 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
33 curl_setopt($ch, CURLOPT_USERAGENT, 'TinyMCE-Image-Downloader');
34
35 $imageData = curl_exec($ch);
36 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
37 $curlError = curl_error($ch);
38
39 curl_close($ch);
40
41 if($imageData !== false && $httpCode >= 200 && $httpCode < 300){
42 return $imageData;
43 }
44
45 $attempt++;
46 sleep(1);
47 }
48
49 return false; // échec après trois tentatives
50}
51
21if(isset($_GET['action']) && $_GET['action'] == 'editor_submit'){ 52if(isset($_GET['action']) && $_GET['action'] == 'editor_submit'){
22 // récupération des données 53 // récupération des données
23 $data = file_get_contents('php://input'); 54 $data = file_get_contents('php://input');
@@ -84,7 +115,7 @@ elseif(isset($_GET['action']) && $_GET['action'] == 'upload_image_url'){
84 $json = json_decode(file_get_contents('php://input'), true); 115 $json = json_decode(file_get_contents('php://input'), true);
85 116
86 if(isset($json['image_url'])){ 117 if(isset($json['image_url'])){
87 $image_data = file_get_contents($json['image_url']); // téléchargement de l’image par le serveur 118 $image_data = curlDownloadImage($json['image_url']); // téléchargement de l’image par le serveur avec cURL au lieu de file_get_contents
88 119
89 if($image_data === false){ 120 if($image_data === false){
90 http_response_code(400); 121 http_response_code(400);
@@ -154,25 +185,29 @@ elseif(isset($_GET['action']) && $_GET['action'] == 'upload_image_url'){
154 document.querySelector(`#submit-${articleId}`).classList.remove('hidden'); 185 document.querySelector(`#submit-${articleId}`).classList.remove('hidden');
155 }); 186 });
156 editor.on('PastePreProcess', function (e){ // déclenchement au collage AVANT insertion dans l'éditeur 187 editor.on('PastePreProcess', function (e){ // déclenchement au collage AVANT insertion dans l'éditeur
157 //const images = e.node.querySelectorAll('img');
158 let parser = new DOMParser(); 188 let parser = new DOMParser();
159 let doc = parser.parseFromString(e.content, 'text/html'); 189 let doc = parser.parseFromString(e.content, 'text/html');
160 let images = doc.querySelectorAll('img'); 190 let images = doc.querySelectorAll('img');
161 191
162 let downloads_in_progress = []; 192 let downloads_in_progress = [];
163 193
164 images.forEach(image => { 194 images.forEach(img => {
165 if(image.src.startsWith('http')){ 195 if(img.src.startsWith('file://')){ // détection d'images non insérables
166 // le fait d'avoir lancer un téléchargement 196 console.warn('Image locale non insérable dans tinymce :', img.src);
167 let promise = fetch('index.php?action=upload_image_url', { 197 img.outerHTML = '<div style="border:1px solid red; padding:10px; margin:5px 0; background-color:#ffe6e6; color:#a94442; font-size:14px;">' +
198 "Image locale non insérée. Pour insérer une image depuis LibreOffice, copiez l'image seule et recoller." +
199 '</div>';
200 }
201 else if(img.src.startsWith('http')){ // détection d'images web
202 let promise = fetch('index.php?action=upload_image_url', { // promesse d'un fichier téléchargeable sur le serveur
168 method: 'POST', 203 method: 'POST',
169 headers: { 'Content-Type': 'application/json' }, 204 headers: { 'Content-Type': 'application/json' },
170 body: JSON.stringify({ image_url: image.src }) 205 body: JSON.stringify({ image_url: img.src })
171 }) 206 })
172 .then(response => response.json()) 207 .then(response => response.json())
173 .then(data => { 208 .then(data => {
174 if(data.location){ 209 if(data.location){
175 image.src = data.location; // remplacer l'image par celle du serveur 210 img.src = data.location; // remplacer l'image par celle du serveur
176 } 211 }
177 }) 212 })
178 .catch(error => { 213 .catch(error => {
@@ -183,17 +218,18 @@ elseif(isset($_GET['action']) && $_GET['action'] == 'upload_image_url'){
183 } 218 }
184 }); 219 });
185 220
186 console.log(downloads_in_progress); 221 // une image web ou plus: différer l'insertion dans l'éditeur le temps que le serveur télécharge les images
187
188 if(downloads_in_progress.length > 0){ 222 if(downloads_in_progress.length > 0){
189 // faire attendre tinymce que le serveur récupère les images avant insertion
190 e.preventDefault(); 223 e.preventDefault();
191 224
192 Promise.all(downloads_in_progress).then(() => { 225 Promise.all(downloads_in_progress).then(() => {
193 e.content = doc.body.innerHTML; 226 e.content = doc.body.innerHTML; // remplacement du HTML dans l'éditeur par la copie modifiée (doc)
194 editor.insertContent(e.content); 227 editor.insertContent(e.content);
195 }); 228 });
196 } 229 }
230 else{
231 e.content = doc.body.innerHTML; // remplacement du HTML dans l'éditeur par la copie modifiée (doc)
232 }
197 }); 233 });
198 }, 234 },
199 // upload d'image 235 // upload d'image