@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | |
| 18 | 18 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 19 | - return; |
|
| 19 | + return; |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | /** |
@@ -29,12 +29,12 @@ discard block |
||
| 29 | 29 | * @return string |
| 30 | 30 | */ |
| 31 | 31 | function set_spip_doc(?string $fichier): string { |
| 32 | - if ($fichier && str_starts_with($fichier, (string) _DIR_IMG)) { |
|
| 33 | - return substr($fichier, strlen((string) _DIR_IMG)); |
|
| 34 | - } else { |
|
| 35 | - // ex: fichier distant |
|
| 36 | - return $fichier ?? ''; |
|
| 37 | - } |
|
| 32 | + if ($fichier && str_starts_with($fichier, (string) _DIR_IMG)) { |
|
| 33 | + return substr($fichier, strlen((string) _DIR_IMG)); |
|
| 34 | + } else { |
|
| 35 | + // ex: fichier distant |
|
| 36 | + return $fichier ?? ''; |
|
| 37 | + } |
|
| 38 | 38 | } |
| 39 | 39 | |
| 40 | 40 | /** |
@@ -46,26 +46,26 @@ discard block |
||
| 46 | 46 | * @return bool|string |
| 47 | 47 | */ |
| 48 | 48 | function get_spip_doc(?string $fichier) { |
| 49 | - if ($fichier === null) { |
|
| 50 | - return false; |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - // fichier distant |
|
| 54 | - if (tester_url_absolue($fichier)) { |
|
| 55 | - return $fichier; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - // gestion d'erreurs, fichier='' |
|
| 59 | - if (!strlen($fichier)) { |
|
| 60 | - return false; |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - if (!str_starts_with($fichier, (string) _DIR_IMG)) { |
|
| 64 | - $fichier = _DIR_IMG . $fichier; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - // fichier normal |
|
| 68 | - return $fichier; |
|
| 49 | + if ($fichier === null) { |
|
| 50 | + return false; |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + // fichier distant |
|
| 54 | + if (tester_url_absolue($fichier)) { |
|
| 55 | + return $fichier; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + // gestion d'erreurs, fichier='' |
|
| 59 | + if (!strlen($fichier)) { |
|
| 60 | + return false; |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + if (!str_starts_with($fichier, (string) _DIR_IMG)) { |
|
| 64 | + $fichier = _DIR_IMG . $fichier; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + // fichier normal |
|
| 68 | + return $fichier; |
|
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | /** |
@@ -79,26 +79,26 @@ discard block |
||
| 79 | 79 | * @return string |
| 80 | 80 | */ |
| 81 | 81 | function creer_repertoire_documents($ext) { |
| 82 | - $rep = sous_repertoire(_DIR_IMG, $ext); |
|
| 83 | - |
|
| 84 | - if (!$ext || !$rep) { |
|
| 85 | - spip_logger()->info("creer_repertoire_documents '$rep' interdit"); |
|
| 86 | - exit; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - // Cette variable de configuration peut etre posee par un plugin |
|
| 90 | - // par exemple acces_restreint |
|
| 91 | - // sauf pour logo/ utilise pour stocker les logoon et logooff |
|
| 92 | - if ( |
|
| 93 | - isset($GLOBALS['meta']['creer_htaccess']) |
|
| 94 | - && $GLOBALS['meta']['creer_htaccess'] == 'oui' |
|
| 95 | - && $ext !== 'logo' |
|
| 96 | - ) { |
|
| 97 | - include_spip('inc/acces'); |
|
| 98 | - verifier_htaccess($rep); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - return $rep; |
|
| 82 | + $rep = sous_repertoire(_DIR_IMG, $ext); |
|
| 83 | + |
|
| 84 | + if (!$ext || !$rep) { |
|
| 85 | + spip_logger()->info("creer_repertoire_documents '$rep' interdit"); |
|
| 86 | + exit; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + // Cette variable de configuration peut etre posee par un plugin |
|
| 90 | + // par exemple acces_restreint |
|
| 91 | + // sauf pour logo/ utilise pour stocker les logoon et logooff |
|
| 92 | + if ( |
|
| 93 | + isset($GLOBALS['meta']['creer_htaccess']) |
|
| 94 | + && $GLOBALS['meta']['creer_htaccess'] == 'oui' |
|
| 95 | + && $ext !== 'logo' |
|
| 96 | + ) { |
|
| 97 | + include_spip('inc/acces'); |
|
| 98 | + verifier_htaccess($rep); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + return $rep; |
|
| 102 | 102 | } |
| 103 | 103 | |
| 104 | 104 | /** |
@@ -107,19 +107,19 @@ discard block |
||
| 107 | 107 | * @param string $nom |
| 108 | 108 | */ |
| 109 | 109 | function effacer_repertoire_temporaire($nom) { |
| 110 | - if ($d = opendir($nom)) { |
|
| 111 | - while (($f = readdir($d)) !== false) { |
|
| 112 | - if (is_file("$nom/$f")) { |
|
| 113 | - spip_unlink("$nom/$f"); |
|
| 114 | - } else { |
|
| 115 | - if ($f != '.' && $f != '..' && is_dir("$nom/$f")) { |
|
| 116 | - effacer_repertoire_temporaire("$nom/$f"); |
|
| 117 | - } |
|
| 118 | - } |
|
| 119 | - } |
|
| 120 | - } |
|
| 121 | - closedir($d); |
|
| 122 | - @rmdir($nom); |
|
| 110 | + if ($d = opendir($nom)) { |
|
| 111 | + while (($f = readdir($d)) !== false) { |
|
| 112 | + if (is_file("$nom/$f")) { |
|
| 113 | + spip_unlink("$nom/$f"); |
|
| 114 | + } else { |
|
| 115 | + if ($f != '.' && $f != '..' && is_dir("$nom/$f")) { |
|
| 116 | + effacer_repertoire_temporaire("$nom/$f"); |
|
| 117 | + } |
|
| 118 | + } |
|
| 119 | + } |
|
| 120 | + } |
|
| 121 | + closedir($d); |
|
| 122 | + @rmdir($nom); |
|
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | // |
@@ -136,44 +136,44 @@ discard block |
||
| 136 | 136 | */ |
| 137 | 137 | function copier_document($ext, $orig, $source, $subdir = null) { |
| 138 | 138 | |
| 139 | - $orig = preg_replace(',\.\.+,', '.', $orig); // pas de .. dans le nom du doc |
|
| 140 | - $dir = creer_repertoire_documents($subdir ?: $ext); |
|
| 141 | - |
|
| 142 | - $dest = preg_replace('/<[^>]*>/', '', basename($orig)); |
|
| 143 | - $dest = preg_replace('/\.([^.]+)$/', '', $dest); |
|
| 144 | - $dest = translitteration($dest); |
|
| 145 | - $dest = preg_replace('/[^.=\w-]+/', '_', (string) $dest); |
|
| 146 | - |
|
| 147 | - // ne pas accepter de noms de la forme -r90.jpg qui sont reserves |
|
| 148 | - // pour les images transformees par rotation (action/documenter) |
|
| 149 | - $dest = preg_replace(',-r(90|180|270)$,', '', $dest); |
|
| 150 | - |
|
| 151 | - while (preg_match(',\.(\w+)$,', $dest, $m)) { |
|
| 152 | - if ( |
|
| 153 | - !function_exists('verifier_upload_autorise') |
|
| 154 | - || !($r = verifier_upload_autorise($dest)) |
|
| 155 | - || !empty($r['autozip']) |
|
| 156 | - ) { |
|
| 157 | - $dest = substr($dest, 0, -strlen($m[0])) . '_' . $m[1]; |
|
| 158 | - break; |
|
| 159 | - } else { |
|
| 160 | - $dest = substr($dest, 0, -strlen($m[0])); |
|
| 161 | - $ext = $m[1] . '.' . $ext; |
|
| 162 | - } |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - // Si le document "source" est deja au bon endroit, ne rien faire |
|
| 166 | - if ($source == ($dir . $dest . '.' . $ext)) { |
|
| 167 | - return $source; |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - // sinon tourner jusqu'a trouver un numero correct |
|
| 171 | - $n = 0; |
|
| 172 | - while (@file_exists($newFile = $dir . $dest . ($n++ ? ('-' . $n) : '') . '.' . $ext)) { |
|
| 173 | - ; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - return deplacer_fichier_upload($source, $newFile); |
|
| 139 | + $orig = preg_replace(',\.\.+,', '.', $orig); // pas de .. dans le nom du doc |
|
| 140 | + $dir = creer_repertoire_documents($subdir ?: $ext); |
|
| 141 | + |
|
| 142 | + $dest = preg_replace('/<[^>]*>/', '', basename($orig)); |
|
| 143 | + $dest = preg_replace('/\.([^.]+)$/', '', $dest); |
|
| 144 | + $dest = translitteration($dest); |
|
| 145 | + $dest = preg_replace('/[^.=\w-]+/', '_', (string) $dest); |
|
| 146 | + |
|
| 147 | + // ne pas accepter de noms de la forme -r90.jpg qui sont reserves |
|
| 148 | + // pour les images transformees par rotation (action/documenter) |
|
| 149 | + $dest = preg_replace(',-r(90|180|270)$,', '', $dest); |
|
| 150 | + |
|
| 151 | + while (preg_match(',\.(\w+)$,', $dest, $m)) { |
|
| 152 | + if ( |
|
| 153 | + !function_exists('verifier_upload_autorise') |
|
| 154 | + || !($r = verifier_upload_autorise($dest)) |
|
| 155 | + || !empty($r['autozip']) |
|
| 156 | + ) { |
|
| 157 | + $dest = substr($dest, 0, -strlen($m[0])) . '_' . $m[1]; |
|
| 158 | + break; |
|
| 159 | + } else { |
|
| 160 | + $dest = substr($dest, 0, -strlen($m[0])); |
|
| 161 | + $ext = $m[1] . '.' . $ext; |
|
| 162 | + } |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + // Si le document "source" est deja au bon endroit, ne rien faire |
|
| 166 | + if ($source == ($dir . $dest . '.' . $ext)) { |
|
| 167 | + return $source; |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + // sinon tourner jusqu'a trouver un numero correct |
|
| 171 | + $n = 0; |
|
| 172 | + while (@file_exists($newFile = $dir . $dest . ($n++ ? ('-' . $n) : '') . '.' . $ext)) { |
|
| 173 | + ; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + return deplacer_fichier_upload($source, $newFile); |
|
| 177 | 177 | } |
| 178 | 178 | |
| 179 | 179 | /** |
@@ -188,28 +188,28 @@ discard block |
||
| 188 | 188 | * @return bool|string |
| 189 | 189 | */ |
| 190 | 190 | function determine_upload($type = '') { |
| 191 | - if (!function_exists('autoriser')) { |
|
| 192 | - include_spip('inc/autoriser'); |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - if ( |
|
| 196 | - !autoriser('chargerftp') |
|
| 197 | - || $type == 'logos' |
|
| 198 | - ) { # on ne le permet pas pour les logos |
|
| 199 | - return false; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - $repertoire = _DIR_TRANSFERT; |
|
| 203 | - if (!@is_dir($repertoire)) { |
|
| 204 | - $repertoire = str_replace(_DIR_TMP, '', (string) $repertoire); |
|
| 205 | - $repertoire = sous_repertoire(_DIR_TMP, $repertoire); |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - if (!$GLOBALS['visiteur_session']['restreint']) { |
|
| 209 | - return $repertoire; |
|
| 210 | - } else { |
|
| 211 | - return sous_repertoire($repertoire, $GLOBALS['visiteur_session']['login']); |
|
| 212 | - } |
|
| 191 | + if (!function_exists('autoriser')) { |
|
| 192 | + include_spip('inc/autoriser'); |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + if ( |
|
| 196 | + !autoriser('chargerftp') |
|
| 197 | + || $type == 'logos' |
|
| 198 | + ) { # on ne le permet pas pour les logos |
|
| 199 | + return false; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + $repertoire = _DIR_TRANSFERT; |
|
| 203 | + if (!@is_dir($repertoire)) { |
|
| 204 | + $repertoire = str_replace(_DIR_TMP, '', (string) $repertoire); |
|
| 205 | + $repertoire = sous_repertoire(_DIR_TMP, $repertoire); |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + if (!$GLOBALS['visiteur_session']['restreint']) { |
|
| 209 | + return $repertoire; |
|
| 210 | + } else { |
|
| 211 | + return sous_repertoire($repertoire, $GLOBALS['visiteur_session']['login']); |
|
| 212 | + } |
|
| 213 | 213 | } |
| 214 | 214 | |
| 215 | 215 | /** |
@@ -228,31 +228,31 @@ discard block |
||
| 228 | 228 | * @return bool|mixed|string |
| 229 | 229 | */ |
| 230 | 230 | function deplacer_fichier_upload($source, $dest, $move = false) { |
| 231 | - // Securite |
|
| 232 | - if (str_starts_with($dest, (string) _DIR_RACINE)) { |
|
| 233 | - $dest = _DIR_RACINE . preg_replace(',\.\.+,', '.', substr($dest, strlen((string) _DIR_RACINE))); |
|
| 234 | - } else { |
|
| 235 | - $dest = preg_replace(',\.\.+,', '.', $dest); |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - $ok = $move ? @rename($source, $dest) : @copy($source, $dest); |
|
| 239 | - if (!$ok) { |
|
| 240 | - $ok = @move_uploaded_file($source, $dest); |
|
| 241 | - } |
|
| 242 | - if ($ok) { |
|
| 243 | - @chmod($dest, _SPIP_CHMOD & ~0111); |
|
| 244 | - } else { |
|
| 245 | - $f = @fopen($dest, 'w'); |
|
| 246 | - if ($f) { |
|
| 247 | - fclose($f); |
|
| 248 | - } else { |
|
| 249 | - include_spip('inc/flock'); |
|
| 250 | - raler_fichier($dest); |
|
| 251 | - } |
|
| 252 | - spip_unlink($dest); |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - return $ok ? $dest : false; |
|
| 231 | + // Securite |
|
| 232 | + if (str_starts_with($dest, (string) _DIR_RACINE)) { |
|
| 233 | + $dest = _DIR_RACINE . preg_replace(',\.\.+,', '.', substr($dest, strlen((string) _DIR_RACINE))); |
|
| 234 | + } else { |
|
| 235 | + $dest = preg_replace(',\.\.+,', '.', $dest); |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + $ok = $move ? @rename($source, $dest) : @copy($source, $dest); |
|
| 239 | + if (!$ok) { |
|
| 240 | + $ok = @move_uploaded_file($source, $dest); |
|
| 241 | + } |
|
| 242 | + if ($ok) { |
|
| 243 | + @chmod($dest, _SPIP_CHMOD & ~0111); |
|
| 244 | + } else { |
|
| 245 | + $f = @fopen($dest, 'w'); |
|
| 246 | + if ($f) { |
|
| 247 | + fclose($f); |
|
| 248 | + } else { |
|
| 249 | + include_spip('inc/flock'); |
|
| 250 | + raler_fichier($dest); |
|
| 251 | + } |
|
| 252 | + spip_unlink($dest); |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + return $ok ? $dest : false; |
|
| 256 | 256 | } |
| 257 | 257 | |
| 258 | 258 | |
@@ -276,60 +276,60 @@ discard block |
||
| 276 | 276 | */ |
| 277 | 277 | function check_upload_error($error, $msg = '', $return = false) { |
| 278 | 278 | |
| 279 | - if (!$error) { |
|
| 280 | - return false; |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - spip_logger()->info("Erreur upload $error -- cf. http://php.net/manual/fr/features.file-upload.errors.php"); |
|
| 284 | - |
|
| 285 | - switch ($error) { |
|
| 286 | - case 4: /* UPLOAD_ERR_NO_FILE */ |
|
| 287 | - return true; |
|
| 288 | - |
|
| 289 | - # on peut affiner les differents messages d'erreur |
|
| 290 | - case 1: /* UPLOAD_ERR_INI_SIZE */ |
|
| 291 | - $msg = _T( |
|
| 292 | - 'upload_limit', |
|
| 293 | - ['max' => ini_get('upload_max_filesize')] |
|
| 294 | - ); |
|
| 295 | - break; |
|
| 296 | - case 2: /* UPLOAD_ERR_FORM_SIZE */ |
|
| 297 | - $msg = _T( |
|
| 298 | - 'upload_limit', |
|
| 299 | - ['max' => ini_get('upload_max_filesize')] |
|
| 300 | - ); |
|
| 301 | - break; |
|
| 302 | - case 3: /* UPLOAD_ERR_PARTIAL */ |
|
| 303 | - $msg = _T( |
|
| 304 | - 'upload_limit', |
|
| 305 | - ['max' => ini_get('upload_max_filesize')] |
|
| 306 | - ); |
|
| 307 | - break; |
|
| 308 | - |
|
| 309 | - default: /* autre */ |
|
| 310 | - if (!$msg) { |
|
| 311 | - $msg = _T('pass_erreur') . ' ' . $error |
|
| 312 | - . '<br />' . propre('[->http://php.net/manual/fr/features.file-upload.errors.php]'); |
|
| 313 | - } |
|
| 314 | - break; |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - spip_logger()->info("erreur upload $error"); |
|
| 318 | - if ($return) { |
|
| 319 | - return $msg; |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - if (_request('iframe') == 'iframe') { |
|
| 323 | - echo "<div class='upload_answer upload_error'>$msg</div>"; |
|
| 324 | - exit; |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - include_spip('inc/minipres'); |
|
| 328 | - echo minipres( |
|
| 329 | - $msg, |
|
| 330 | - "<div style='text-align: " . $GLOBALS['spip_lang_right'] . "'><a href='" . rawurldecode((string) $GLOBALS['redirect']) . "'><button type='button'>" . _T('ecrire:bouton_suivant') . '</button></a></div>' |
|
| 331 | - ); |
|
| 332 | - exit; |
|
| 279 | + if (!$error) { |
|
| 280 | + return false; |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + spip_logger()->info("Erreur upload $error -- cf. http://php.net/manual/fr/features.file-upload.errors.php"); |
|
| 284 | + |
|
| 285 | + switch ($error) { |
|
| 286 | + case 4: /* UPLOAD_ERR_NO_FILE */ |
|
| 287 | + return true; |
|
| 288 | + |
|
| 289 | + # on peut affiner les differents messages d'erreur |
|
| 290 | + case 1: /* UPLOAD_ERR_INI_SIZE */ |
|
| 291 | + $msg = _T( |
|
| 292 | + 'upload_limit', |
|
| 293 | + ['max' => ini_get('upload_max_filesize')] |
|
| 294 | + ); |
|
| 295 | + break; |
|
| 296 | + case 2: /* UPLOAD_ERR_FORM_SIZE */ |
|
| 297 | + $msg = _T( |
|
| 298 | + 'upload_limit', |
|
| 299 | + ['max' => ini_get('upload_max_filesize')] |
|
| 300 | + ); |
|
| 301 | + break; |
|
| 302 | + case 3: /* UPLOAD_ERR_PARTIAL */ |
|
| 303 | + $msg = _T( |
|
| 304 | + 'upload_limit', |
|
| 305 | + ['max' => ini_get('upload_max_filesize')] |
|
| 306 | + ); |
|
| 307 | + break; |
|
| 308 | + |
|
| 309 | + default: /* autre */ |
|
| 310 | + if (!$msg) { |
|
| 311 | + $msg = _T('pass_erreur') . ' ' . $error |
|
| 312 | + . '<br />' . propre('[->http://php.net/manual/fr/features.file-upload.errors.php]'); |
|
| 313 | + } |
|
| 314 | + break; |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + spip_logger()->info("erreur upload $error"); |
|
| 318 | + if ($return) { |
|
| 319 | + return $msg; |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + if (_request('iframe') == 'iframe') { |
|
| 323 | + echo "<div class='upload_answer upload_error'>$msg</div>"; |
|
| 324 | + exit; |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + include_spip('inc/minipres'); |
|
| 328 | + echo minipres( |
|
| 329 | + $msg, |
|
| 330 | + "<div style='text-align: " . $GLOBALS['spip_lang_right'] . "'><a href='" . rawurldecode((string) $GLOBALS['redirect']) . "'><button type='button'>" . _T('ecrire:bouton_suivant') . '</button></a></div>' |
|
| 331 | + ); |
|
| 332 | + exit; |
|
| 333 | 333 | } |
| 334 | 334 | |
| 335 | 335 | /** |
@@ -346,14 +346,14 @@ discard block |
||
| 346 | 346 | * @return string |
| 347 | 347 | */ |
| 348 | 348 | function corriger_extension($ext) { |
| 349 | - $ext = preg_replace(',[^a-z0-9],i', '', $ext); |
|
| 350 | - |
|
| 351 | - return match ($ext) { |
|
| 352 | - 'htm' => 'html', |
|
| 353 | - 'jpeg' => 'jpg', |
|
| 354 | - 'tiff' => 'tif', |
|
| 355 | - 'aif' => 'aiff', |
|
| 356 | - 'mpeg' => 'mpg', |
|
| 357 | - default => $ext, |
|
| 358 | - }; |
|
| 349 | + $ext = preg_replace(',[^a-z0-9],i', '', $ext); |
|
| 350 | + |
|
| 351 | + return match ($ext) { |
|
| 352 | + 'htm' => 'html', |
|
| 353 | + 'jpeg' => 'jpg', |
|
| 354 | + 'tiff' => 'tif', |
|
| 355 | + 'aif' => 'aiff', |
|
| 356 | + 'mpeg' => 'mpg', |
|
| 357 | + default => $ext, |
|
| 358 | + }; |
|
| 359 | 359 | } |
@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | **/ |
| 17 | 17 | |
| 18 | 18 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 19 | - return; |
|
| 19 | + return; |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | // Les parametres generaux du site sont dans une table SQL; |
@@ -26,51 +26,51 @@ discard block |
||
| 26 | 26 | define('_META_CACHE_TIME', 1 << 24); |
| 27 | 27 | |
| 28 | 28 | function inc_meta_dist($table = 'meta') { |
| 29 | - $new = null; |
|
| 30 | - // Lire les meta, en cache si present, valide et lisible |
|
| 31 | - // en cas d'install ne pas faire confiance au meta_cache eventuel |
|
| 32 | - $cache = cache_meta($table); |
|
| 29 | + $new = null; |
|
| 30 | + // Lire les meta, en cache si present, valide et lisible |
|
| 31 | + // en cas d'install ne pas faire confiance au meta_cache eventuel |
|
| 32 | + $cache = cache_meta($table); |
|
| 33 | 33 | |
| 34 | - if ( |
|
| 35 | - (!($exec = _request('exec')) || !autoriser_sans_cookie($exec)) |
|
| 36 | - && ($new = jeune_fichier($cache, _META_CACHE_TIME)) |
|
| 37 | - && lire_fichier_securise($cache, $meta) |
|
| 38 | - && ($meta = @unserialize($meta)) |
|
| 39 | - ) { |
|
| 40 | - $GLOBALS[$table] = $meta; |
|
| 41 | - } |
|
| 34 | + if ( |
|
| 35 | + (!($exec = _request('exec')) || !autoriser_sans_cookie($exec)) |
|
| 36 | + && ($new = jeune_fichier($cache, _META_CACHE_TIME)) |
|
| 37 | + && lire_fichier_securise($cache, $meta) |
|
| 38 | + && ($meta = @unserialize($meta)) |
|
| 39 | + ) { |
|
| 40 | + $GLOBALS[$table] = $meta; |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - if ( |
|
| 44 | - isset($GLOBALS[$table]['touch']) |
|
| 45 | - && $GLOBALS[$table]['touch'] < time() - _META_CACHE_TIME |
|
| 46 | - ) { |
|
| 47 | - $GLOBALS[$table] = []; |
|
| 48 | - } |
|
| 49 | - // sinon lire en base |
|
| 50 | - if (!$GLOBALS[$table]) { |
|
| 51 | - $new = !lire_metas($table); |
|
| 52 | - } |
|
| 43 | + if ( |
|
| 44 | + isset($GLOBALS[$table]['touch']) |
|
| 45 | + && $GLOBALS[$table]['touch'] < time() - _META_CACHE_TIME |
|
| 46 | + ) { |
|
| 47 | + $GLOBALS[$table] = []; |
|
| 48 | + } |
|
| 49 | + // sinon lire en base |
|
| 50 | + if (!$GLOBALS[$table]) { |
|
| 51 | + $new = !lire_metas($table); |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - // renouveller l'alea general si trop vieux ou sur demande explicite |
|
| 55 | - if ( |
|
| 56 | - (test_espace_prive() || isset($_GET['renouvelle_alea'])) |
|
| 57 | - && $GLOBALS[$table] |
|
| 58 | - && time() > _RENOUVELLE_ALEA + ($GLOBALS['meta']['alea_ephemere_date'] ?? 0) |
|
| 59 | - ) { |
|
| 60 | - // si on n'a pas l'acces en ecriture sur le cache, |
|
| 61 | - // ne pas renouveller l'alea sinon le cache devient faux |
|
| 62 | - if (supprimer_fichier($cache)) { |
|
| 63 | - include_spip('inc/acces'); |
|
| 64 | - renouvelle_alea(); |
|
| 65 | - $new = false; |
|
| 66 | - } else { |
|
| 67 | - spip_logger()->info("impossible d'ecrire dans " . $cache); |
|
| 68 | - } |
|
| 69 | - } |
|
| 70 | - // et refaire le cache si on a du lire en base |
|
| 71 | - if (!$new) { |
|
| 72 | - touch_meta(false, $table); |
|
| 73 | - } |
|
| 54 | + // renouveller l'alea general si trop vieux ou sur demande explicite |
|
| 55 | + if ( |
|
| 56 | + (test_espace_prive() || isset($_GET['renouvelle_alea'])) |
|
| 57 | + && $GLOBALS[$table] |
|
| 58 | + && time() > _RENOUVELLE_ALEA + ($GLOBALS['meta']['alea_ephemere_date'] ?? 0) |
|
| 59 | + ) { |
|
| 60 | + // si on n'a pas l'acces en ecriture sur le cache, |
|
| 61 | + // ne pas renouveller l'alea sinon le cache devient faux |
|
| 62 | + if (supprimer_fichier($cache)) { |
|
| 63 | + include_spip('inc/acces'); |
|
| 64 | + renouvelle_alea(); |
|
| 65 | + $new = false; |
|
| 66 | + } else { |
|
| 67 | + spip_logger()->info("impossible d'ecrire dans " . $cache); |
|
| 68 | + } |
|
| 69 | + } |
|
| 70 | + // et refaire le cache si on a du lire en base |
|
| 71 | + if (!$new) { |
|
| 72 | + touch_meta(false, $table); |
|
| 73 | + } |
|
| 74 | 74 | } |
| 75 | 75 | |
| 76 | 76 | // fonctions aussi appelees a l'install ==> spip_query en premiere requete |
@@ -78,39 +78,39 @@ discard block |
||
| 78 | 78 | |
| 79 | 79 | function lire_metas($table = 'meta') { |
| 80 | 80 | |
| 81 | - if ($result = spip_query("SELECT nom,valeur FROM spip_$table")) { |
|
| 82 | - include_spip('base/abstract_sql'); |
|
| 83 | - $GLOBALS[$table] = []; |
|
| 84 | - while ($row = sql_fetch($result)) { |
|
| 85 | - $GLOBALS[$table][$row['nom']] = $row['valeur']; |
|
| 86 | - } |
|
| 87 | - sql_free($result); |
|
| 81 | + if ($result = spip_query("SELECT nom,valeur FROM spip_$table")) { |
|
| 82 | + include_spip('base/abstract_sql'); |
|
| 83 | + $GLOBALS[$table] = []; |
|
| 84 | + while ($row = sql_fetch($result)) { |
|
| 85 | + $GLOBALS[$table][$row['nom']] = $row['valeur']; |
|
| 86 | + } |
|
| 87 | + sql_free($result); |
|
| 88 | 88 | |
| 89 | - if ( |
|
| 90 | - !isset($GLOBALS[$table]['charset']) |
|
| 91 | - || !$GLOBALS[$table]['charset'] |
|
| 92 | - || $GLOBALS[$table]['charset'] == '_DEFAULT_CHARSET' // hum, correction d'un bug ayant abime quelques install |
|
| 93 | - ) { |
|
| 94 | - ecrire_meta('charset', _DEFAULT_CHARSET, null, $table); |
|
| 95 | - } |
|
| 89 | + if ( |
|
| 90 | + !isset($GLOBALS[$table]['charset']) |
|
| 91 | + || !$GLOBALS[$table]['charset'] |
|
| 92 | + || $GLOBALS[$table]['charset'] == '_DEFAULT_CHARSET' // hum, correction d'un bug ayant abime quelques install |
|
| 93 | + ) { |
|
| 94 | + ecrire_meta('charset', _DEFAULT_CHARSET, null, $table); |
|
| 95 | + } |
|
| 96 | 96 | |
| 97 | - // noter cette table de configuration dans les meta de SPIP |
|
| 98 | - if ($table !== 'meta') { |
|
| 99 | - $liste = []; |
|
| 100 | - if (isset($GLOBALS['meta']['tables_config'])) { |
|
| 101 | - $liste = unserialize($GLOBALS['meta']['tables_config']); |
|
| 102 | - } |
|
| 103 | - if (!$liste) { |
|
| 104 | - $liste = []; |
|
| 105 | - } |
|
| 106 | - if (!in_array($table, $liste)) { |
|
| 107 | - $liste[] = $table; |
|
| 108 | - ecrire_meta('tables_config', serialize($liste)); |
|
| 109 | - } |
|
| 110 | - } |
|
| 111 | - } |
|
| 97 | + // noter cette table de configuration dans les meta de SPIP |
|
| 98 | + if ($table !== 'meta') { |
|
| 99 | + $liste = []; |
|
| 100 | + if (isset($GLOBALS['meta']['tables_config'])) { |
|
| 101 | + $liste = unserialize($GLOBALS['meta']['tables_config']); |
|
| 102 | + } |
|
| 103 | + if (!$liste) { |
|
| 104 | + $liste = []; |
|
| 105 | + } |
|
| 106 | + if (!in_array($table, $liste)) { |
|
| 107 | + $liste[] = $table; |
|
| 108 | + ecrire_meta('tables_config', serialize($liste)); |
|
| 109 | + } |
|
| 110 | + } |
|
| 111 | + } |
|
| 112 | 112 | |
| 113 | - return $GLOBALS[$table] ?? null; |
|
| 113 | + return $GLOBALS[$table] ?? null; |
|
| 114 | 114 | } |
| 115 | 115 | |
| 116 | 116 | |
@@ -124,22 +124,22 @@ discard block |
||
| 124 | 124 | * Table SQL d'enregistrement des meta. |
| 125 | 125 | **/ |
| 126 | 126 | function touch_meta($antidate = false, $table = 'meta') { |
| 127 | - $file = cache_meta($table); |
|
| 128 | - if (!$antidate || !@touch($file, $antidate)) { |
|
| 129 | - $r = $GLOBALS[$table] ?? []; |
|
| 130 | - if ($table == 'meta') { |
|
| 131 | - unset($r['alea_ephemere']); |
|
| 132 | - unset($r['alea_ephemere_ancien']); |
|
| 133 | - // le secret du site est utilise pour encoder les contextes ajax que l'on considere fiables |
|
| 134 | - // mais le sortir deu cache meta implique une requete sql des qu'on a un form dynamique |
|
| 135 | - // meme si son squelette est en cache |
|
| 136 | - //unset($r['secret_du_site']); |
|
| 137 | - if ($antidate) { |
|
| 138 | - $r['touch'] = $antidate; |
|
| 139 | - } |
|
| 140 | - } |
|
| 141 | - ecrire_fichier_securise($file, serialize($r)); |
|
| 142 | - } |
|
| 127 | + $file = cache_meta($table); |
|
| 128 | + if (!$antidate || !@touch($file, $antidate)) { |
|
| 129 | + $r = $GLOBALS[$table] ?? []; |
|
| 130 | + if ($table == 'meta') { |
|
| 131 | + unset($r['alea_ephemere']); |
|
| 132 | + unset($r['alea_ephemere_ancien']); |
|
| 133 | + // le secret du site est utilise pour encoder les contextes ajax que l'on considere fiables |
|
| 134 | + // mais le sortir deu cache meta implique une requete sql des qu'on a un form dynamique |
|
| 135 | + // meme si son squelette est en cache |
|
| 136 | + //unset($r['secret_du_site']); |
|
| 137 | + if ($antidate) { |
|
| 138 | + $r['touch'] = $antidate; |
|
| 139 | + } |
|
| 140 | + } |
|
| 141 | + ecrire_fichier_securise($file, serialize($r)); |
|
| 142 | + } |
|
| 143 | 143 | } |
| 144 | 144 | |
| 145 | 145 | /** |
@@ -155,21 +155,21 @@ discard block |
||
| 155 | 155 | * Table SQL d'enregistrement de la meta. |
| 156 | 156 | **/ |
| 157 | 157 | function effacer_meta($nom, $table = 'meta') { |
| 158 | - // section critique sur le cache: |
|
| 159 | - // l'invalider avant et apres la MAJ de la BD |
|
| 160 | - // c'est un peu moins bien qu'un vrai verrou mais ca suffira |
|
| 161 | - // et utiliser une statique pour eviter des acces disques a repetition |
|
| 162 | - static $touch = []; |
|
| 163 | - $antidate = time() - (_META_CACHE_TIME << 4); |
|
| 164 | - if (!isset($touch[$table])) { |
|
| 165 | - touch_meta($antidate, $table); |
|
| 166 | - } |
|
| 167 | - sql_delete('spip_' . $table, "nom='$nom'", '', 'continue'); |
|
| 168 | - unset($GLOBALS[$table][$nom]); |
|
| 169 | - if (!isset($touch[$table])) { |
|
| 170 | - touch_meta($antidate, $table); |
|
| 171 | - $touch[$table] = false; |
|
| 172 | - } |
|
| 158 | + // section critique sur le cache: |
|
| 159 | + // l'invalider avant et apres la MAJ de la BD |
|
| 160 | + // c'est un peu moins bien qu'un vrai verrou mais ca suffira |
|
| 161 | + // et utiliser une statique pour eviter des acces disques a repetition |
|
| 162 | + static $touch = []; |
|
| 163 | + $antidate = time() - (_META_CACHE_TIME << 4); |
|
| 164 | + if (!isset($touch[$table])) { |
|
| 165 | + touch_meta($antidate, $table); |
|
| 166 | + } |
|
| 167 | + sql_delete('spip_' . $table, "nom='$nom'", '', 'continue'); |
|
| 168 | + unset($GLOBALS[$table][$nom]); |
|
| 169 | + if (!isset($touch[$table])) { |
|
| 170 | + touch_meta($antidate, $table); |
|
| 171 | + $touch[$table] = false; |
|
| 172 | + } |
|
| 173 | 173 | } |
| 174 | 174 | |
| 175 | 175 | /** |
@@ -191,54 +191,54 @@ discard block |
||
| 191 | 191 | **/ |
| 192 | 192 | function ecrire_meta($nom, $valeur, $importable = null, $table = 'meta') { |
| 193 | 193 | |
| 194 | - static $touch = []; |
|
| 195 | - if (!$nom) { |
|
| 196 | - return; |
|
| 197 | - } |
|
| 198 | - include_spip('base/abstract_sql'); |
|
| 199 | - $res = sql_select('*', 'spip_' . $table, 'nom=' . sql_quote($nom), '', '', '', '', '', 'continue'); |
|
| 200 | - // table pas encore installee, travailler en php seulement |
|
| 201 | - if (!$res) { |
|
| 202 | - $GLOBALS[$table][$nom] = $valeur; |
|
| 194 | + static $touch = []; |
|
| 195 | + if (!$nom) { |
|
| 196 | + return; |
|
| 197 | + } |
|
| 198 | + include_spip('base/abstract_sql'); |
|
| 199 | + $res = sql_select('*', 'spip_' . $table, 'nom=' . sql_quote($nom), '', '', '', '', '', 'continue'); |
|
| 200 | + // table pas encore installee, travailler en php seulement |
|
| 201 | + if (!$res) { |
|
| 202 | + $GLOBALS[$table][$nom] = $valeur; |
|
| 203 | 203 | |
| 204 | - return; |
|
| 205 | - } |
|
| 206 | - $row = sql_fetch($res); |
|
| 207 | - sql_free($res); |
|
| 204 | + return; |
|
| 205 | + } |
|
| 206 | + $row = sql_fetch($res); |
|
| 207 | + sql_free($res); |
|
| 208 | 208 | |
| 209 | - // ne pas invalider le cache si affectation a l'identique |
|
| 210 | - // (tant pis si impt aurait du changer) |
|
| 211 | - if ( |
|
| 212 | - $row |
|
| 213 | - && $valeur == $row['valeur'] |
|
| 214 | - && isset($GLOBALS[$table][$nom]) |
|
| 215 | - && $GLOBALS[$table][$nom] == $valeur |
|
| 216 | - ) { |
|
| 217 | - return; |
|
| 218 | - } |
|
| 209 | + // ne pas invalider le cache si affectation a l'identique |
|
| 210 | + // (tant pis si impt aurait du changer) |
|
| 211 | + if ( |
|
| 212 | + $row |
|
| 213 | + && $valeur == $row['valeur'] |
|
| 214 | + && isset($GLOBALS[$table][$nom]) |
|
| 215 | + && $GLOBALS[$table][$nom] == $valeur |
|
| 216 | + ) { |
|
| 217 | + return; |
|
| 218 | + } |
|
| 219 | 219 | |
| 220 | - $GLOBALS[$table][$nom] = $valeur; |
|
| 221 | - // cf effacer pour comprendre le double touch |
|
| 222 | - $antidate = time() - (_META_CACHE_TIME << 1); |
|
| 223 | - if (!isset($touch[$table])) { |
|
| 224 | - touch_meta($antidate, $table); |
|
| 225 | - } |
|
| 226 | - $r = ['nom' => sql_quote($nom, '', 'text'), 'valeur' => sql_quote($valeur, '', 'text')]; |
|
| 227 | - // Gaffe aux tables sans impt (vieilles versions de SPIP notamment) |
|
| 228 | - // ici on utilise pas sql_updateq et sql_insertq pour ne pas provoquer trop tot |
|
| 229 | - // de lecture des descriptions des tables |
|
| 230 | - if ($importable && isset($row['impt'])) { |
|
| 231 | - $r['impt'] = sql_quote($importable, '', 'text'); |
|
| 232 | - } |
|
| 233 | - if ($row) { |
|
| 234 | - sql_update('spip_' . $table, $r, 'nom=' . sql_quote($nom)); |
|
| 235 | - } else { |
|
| 236 | - sql_insert('spip_' . $table, '(' . implode(',', array_keys($r)) . ')', '(' . implode(',', array_values($r)) . ')'); |
|
| 237 | - } |
|
| 238 | - if (!isset($touch[$table])) { |
|
| 239 | - touch_meta($antidate, $table); |
|
| 240 | - $touch[$table] = false; |
|
| 241 | - } |
|
| 220 | + $GLOBALS[$table][$nom] = $valeur; |
|
| 221 | + // cf effacer pour comprendre le double touch |
|
| 222 | + $antidate = time() - (_META_CACHE_TIME << 1); |
|
| 223 | + if (!isset($touch[$table])) { |
|
| 224 | + touch_meta($antidate, $table); |
|
| 225 | + } |
|
| 226 | + $r = ['nom' => sql_quote($nom, '', 'text'), 'valeur' => sql_quote($valeur, '', 'text')]; |
|
| 227 | + // Gaffe aux tables sans impt (vieilles versions de SPIP notamment) |
|
| 228 | + // ici on utilise pas sql_updateq et sql_insertq pour ne pas provoquer trop tot |
|
| 229 | + // de lecture des descriptions des tables |
|
| 230 | + if ($importable && isset($row['impt'])) { |
|
| 231 | + $r['impt'] = sql_quote($importable, '', 'text'); |
|
| 232 | + } |
|
| 233 | + if ($row) { |
|
| 234 | + sql_update('spip_' . $table, $r, 'nom=' . sql_quote($nom)); |
|
| 235 | + } else { |
|
| 236 | + sql_insert('spip_' . $table, '(' . implode(',', array_keys($r)) . ')', '(' . implode(',', array_values($r)) . ')'); |
|
| 237 | + } |
|
| 238 | + if (!isset($touch[$table])) { |
|
| 239 | + touch_meta($antidate, $table); |
|
| 240 | + $touch[$table] = false; |
|
| 241 | + } |
|
| 242 | 242 | } |
| 243 | 243 | |
| 244 | 244 | /** |
@@ -250,7 +250,7 @@ discard block |
||
| 250 | 250 | * Nom du fichier cache |
| 251 | 251 | **/ |
| 252 | 252 | function cache_meta($table = 'meta') { |
| 253 | - return ($table == 'meta') ? _FILE_META : (_DIR_CACHE . $table . '.php'); |
|
| 253 | + return ($table == 'meta') ? _FILE_META : (_DIR_CACHE . $table . '.php'); |
|
| 254 | 254 | } |
| 255 | 255 | |
| 256 | 256 | /** |
@@ -259,14 +259,14 @@ discard block |
||
| 259 | 259 | * @param string $table |
| 260 | 260 | */ |
| 261 | 261 | function installer_table_meta($table) { |
| 262 | - $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 263 | - if (!$trouver_table("spip_$table")) { |
|
| 264 | - include_spip('base/auxiliaires'); |
|
| 265 | - include_spip('base/create'); |
|
| 266 | - creer_ou_upgrader_table("spip_$table", $GLOBALS['tables_auxiliaires']['spip_meta'], false, false); |
|
| 267 | - $trouver_table(''); |
|
| 268 | - } |
|
| 269 | - lire_metas($table); |
|
| 262 | + $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 263 | + if (!$trouver_table("spip_$table")) { |
|
| 264 | + include_spip('base/auxiliaires'); |
|
| 265 | + include_spip('base/create'); |
|
| 266 | + creer_ou_upgrader_table("spip_$table", $GLOBALS['tables_auxiliaires']['spip_meta'], false, false); |
|
| 267 | + $trouver_table(''); |
|
| 268 | + } |
|
| 269 | + lire_metas($table); |
|
| 270 | 270 | } |
| 271 | 271 | |
| 272 | 272 | /** |
@@ -278,44 +278,44 @@ discard block |
||
| 278 | 278 | * @param bool $force |
| 279 | 279 | */ |
| 280 | 280 | function supprimer_table_meta($table, $force = false) { |
| 281 | - if ($table !== 'meta') { |
|
| 282 | - // Vérifier le contenu restant de la table |
|
| 283 | - $nb_variables = sql_countsel("spip_$table"); |
|
| 281 | + if ($table !== 'meta') { |
|
| 282 | + // Vérifier le contenu restant de la table |
|
| 283 | + $nb_variables = sql_countsel("spip_$table"); |
|
| 284 | 284 | |
| 285 | - // Supprimer si : |
|
| 286 | - // - la table est vide |
|
| 287 | - // - ou limitée à la variable charset |
|
| 288 | - // - ou qu'on force la suppression |
|
| 289 | - if ( |
|
| 290 | - $force |
|
| 291 | - || !$nb_variables |
|
| 292 | - || $nb_variables == 1 && isset($GLOBALS[$table]['charset']) |
|
| 293 | - ) { |
|
| 294 | - // Supprimer la table des globaleset de la base |
|
| 295 | - unset($GLOBALS[$table]); |
|
| 296 | - sql_drop_table("spip_$table"); |
|
| 297 | - // Supprimer le fichier cache |
|
| 298 | - include_spip('inc/flock'); |
|
| 299 | - $cache = cache_meta($table); |
|
| 300 | - supprimer_fichier($cache); |
|
| 285 | + // Supprimer si : |
|
| 286 | + // - la table est vide |
|
| 287 | + // - ou limitée à la variable charset |
|
| 288 | + // - ou qu'on force la suppression |
|
| 289 | + if ( |
|
| 290 | + $force |
|
| 291 | + || !$nb_variables |
|
| 292 | + || $nb_variables == 1 && isset($GLOBALS[$table]['charset']) |
|
| 293 | + ) { |
|
| 294 | + // Supprimer la table des globaleset de la base |
|
| 295 | + unset($GLOBALS[$table]); |
|
| 296 | + sql_drop_table("spip_$table"); |
|
| 297 | + // Supprimer le fichier cache |
|
| 298 | + include_spip('inc/flock'); |
|
| 299 | + $cache = cache_meta($table); |
|
| 300 | + supprimer_fichier($cache); |
|
| 301 | 301 | |
| 302 | - // vider le cache des tables |
|
| 303 | - $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 304 | - $trouver_table(''); |
|
| 302 | + // vider le cache des tables |
|
| 303 | + $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 304 | + $trouver_table(''); |
|
| 305 | 305 | |
| 306 | - // Supprimer la table de la liste des tables de configuration autres que spip_meta |
|
| 307 | - if (isset($GLOBALS['meta']['tables_config'])) { |
|
| 308 | - $liste = unserialize($GLOBALS['meta']['tables_config']); |
|
| 309 | - $cle = array_search($table, $liste); |
|
| 310 | - if ($cle !== false) { |
|
| 311 | - unset($liste[$cle]); |
|
| 312 | - if ($liste) { |
|
| 313 | - ecrire_meta('tables_config', serialize($liste)); |
|
| 314 | - } else { |
|
| 315 | - effacer_meta('tables_config'); |
|
| 316 | - } |
|
| 317 | - } |
|
| 318 | - } |
|
| 319 | - } |
|
| 320 | - } |
|
| 306 | + // Supprimer la table de la liste des tables de configuration autres que spip_meta |
|
| 307 | + if (isset($GLOBALS['meta']['tables_config'])) { |
|
| 308 | + $liste = unserialize($GLOBALS['meta']['tables_config']); |
|
| 309 | + $cle = array_search($table, $liste); |
|
| 310 | + if ($cle !== false) { |
|
| 311 | + unset($liste[$cle]); |
|
| 312 | + if ($liste) { |
|
| 313 | + ecrire_meta('tables_config', serialize($liste)); |
|
| 314 | + } else { |
|
| 315 | + effacer_meta('tables_config'); |
|
| 316 | + } |
|
| 317 | + } |
|
| 318 | + } |
|
| 319 | + } |
|
| 320 | + } |
|
| 321 | 321 | } |
@@ -64,7 +64,7 @@ discard block |
||
| 64 | 64 | renouvelle_alea(); |
| 65 | 65 | $new = false; |
| 66 | 66 | } else { |
| 67 | - spip_logger()->info("impossible d'ecrire dans " . $cache); |
|
| 67 | + spip_logger()->info("impossible d'ecrire dans ".$cache); |
|
| 68 | 68 | } |
| 69 | 69 | } |
| 70 | 70 | // et refaire le cache si on a du lire en base |
@@ -164,7 +164,7 @@ discard block |
||
| 164 | 164 | if (!isset($touch[$table])) { |
| 165 | 165 | touch_meta($antidate, $table); |
| 166 | 166 | } |
| 167 | - sql_delete('spip_' . $table, "nom='$nom'", '', 'continue'); |
|
| 167 | + sql_delete('spip_'.$table, "nom='$nom'", '', 'continue'); |
|
| 168 | 168 | unset($GLOBALS[$table][$nom]); |
| 169 | 169 | if (!isset($touch[$table])) { |
| 170 | 170 | touch_meta($antidate, $table); |
@@ -196,7 +196,7 @@ discard block |
||
| 196 | 196 | return; |
| 197 | 197 | } |
| 198 | 198 | include_spip('base/abstract_sql'); |
| 199 | - $res = sql_select('*', 'spip_' . $table, 'nom=' . sql_quote($nom), '', '', '', '', '', 'continue'); |
|
| 199 | + $res = sql_select('*', 'spip_'.$table, 'nom='.sql_quote($nom), '', '', '', '', '', 'continue'); |
|
| 200 | 200 | // table pas encore installee, travailler en php seulement |
| 201 | 201 | if (!$res) { |
| 202 | 202 | $GLOBALS[$table][$nom] = $valeur; |
@@ -231,9 +231,9 @@ discard block |
||
| 231 | 231 | $r['impt'] = sql_quote($importable, '', 'text'); |
| 232 | 232 | } |
| 233 | 233 | if ($row) { |
| 234 | - sql_update('spip_' . $table, $r, 'nom=' . sql_quote($nom)); |
|
| 234 | + sql_update('spip_'.$table, $r, 'nom='.sql_quote($nom)); |
|
| 235 | 235 | } else { |
| 236 | - sql_insert('spip_' . $table, '(' . implode(',', array_keys($r)) . ')', '(' . implode(',', array_values($r)) . ')'); |
|
| 236 | + sql_insert('spip_'.$table, '('.implode(',', array_keys($r)).')', '('.implode(',', array_values($r)).')'); |
|
| 237 | 237 | } |
| 238 | 238 | if (!isset($touch[$table])) { |
| 239 | 239 | touch_meta($antidate, $table); |
@@ -250,7 +250,7 @@ discard block |
||
| 250 | 250 | * Nom du fichier cache |
| 251 | 251 | **/ |
| 252 | 252 | function cache_meta($table = 'meta') { |
| 253 | - return ($table == 'meta') ? _FILE_META : (_DIR_CACHE . $table . '.php'); |
|
| 253 | + return ($table == 'meta') ? _FILE_META : (_DIR_CACHE.$table.'.php'); |
|
| 254 | 254 | } |
| 255 | 255 | |
| 256 | 256 | /** |
@@ -17,7 +17,7 @@ discard block |
||
| 17 | 17 | */ |
| 18 | 18 | |
| 19 | 19 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 20 | - return; |
|
| 20 | + return; |
|
| 21 | 21 | } |
| 22 | 22 | include_spip('inc/filtres'); // par precaution |
| 23 | 23 | include_spip('inc/filtres_images_mini'); // par precaution |
@@ -37,21 +37,21 @@ discard block |
||
| 37 | 37 | * Le code de la couleur en hexadécimal. |
| 38 | 38 | */ |
| 39 | 39 | function _couleur_dec_to_hex($red, $green, $blue) { |
| 40 | - $red = dechex($red); |
|
| 41 | - $green = dechex($green); |
|
| 42 | - $blue = dechex($blue); |
|
| 43 | - |
|
| 44 | - if (strlen($red) == 1) { |
|
| 45 | - $red = '0' . $red; |
|
| 46 | - } |
|
| 47 | - if (strlen($green) == 1) { |
|
| 48 | - $green = '0' . $green; |
|
| 49 | - } |
|
| 50 | - if (strlen($blue) == 1) { |
|
| 51 | - $blue = '0' . $blue; |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - return "$red$green$blue"; |
|
| 40 | + $red = dechex($red); |
|
| 41 | + $green = dechex($green); |
|
| 42 | + $blue = dechex($blue); |
|
| 43 | + |
|
| 44 | + if (strlen($red) == 1) { |
|
| 45 | + $red = '0' . $red; |
|
| 46 | + } |
|
| 47 | + if (strlen($green) == 1) { |
|
| 48 | + $green = '0' . $green; |
|
| 49 | + } |
|
| 50 | + if (strlen($blue) == 1) { |
|
| 51 | + $blue = '0' . $blue; |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + return "$red$green$blue"; |
|
| 55 | 55 | } |
| 56 | 56 | |
| 57 | 57 | /** |
@@ -63,18 +63,18 @@ discard block |
||
| 63 | 63 | * Un tableau des 3 éléments : rouge, vert, bleu. |
| 64 | 64 | */ |
| 65 | 65 | function _couleur_hex_to_dec($couleur) { |
| 66 | - $couleur ??= ''; |
|
| 67 | - $couleur = couleur_html_to_hex($couleur); |
|
| 68 | - $couleur = ltrim($couleur, '#'); |
|
| 69 | - if (strlen($couleur) === 3) { |
|
| 70 | - $couleur = $couleur[0] . $couleur[0] . $couleur[1] . $couleur[1] . $couleur[2] . $couleur[2]; |
|
| 71 | - } |
|
| 72 | - $retour = []; |
|
| 73 | - $retour['red'] = hexdec(substr($couleur, 0, 2)); |
|
| 74 | - $retour['green'] = hexdec(substr($couleur, 2, 2)); |
|
| 75 | - $retour['blue'] = hexdec(substr($couleur, 4, 2)); |
|
| 76 | - |
|
| 77 | - return $retour; |
|
| 66 | + $couleur ??= ''; |
|
| 67 | + $couleur = couleur_html_to_hex($couleur); |
|
| 68 | + $couleur = ltrim($couleur, '#'); |
|
| 69 | + if (strlen($couleur) === 3) { |
|
| 70 | + $couleur = $couleur[0] . $couleur[0] . $couleur[1] . $couleur[1] . $couleur[2] . $couleur[2]; |
|
| 71 | + } |
|
| 72 | + $retour = []; |
|
| 73 | + $retour['red'] = hexdec(substr($couleur, 0, 2)); |
|
| 74 | + $retour['green'] = hexdec(substr($couleur, 2, 2)); |
|
| 75 | + $retour['blue'] = hexdec(substr($couleur, 4, 2)); |
|
| 76 | + |
|
| 77 | + return $retour; |
|
| 78 | 78 | } |
| 79 | 79 | |
| 80 | 80 | |
@@ -91,8 +91,8 @@ discard block |
||
| 91 | 91 | * Le code de la couleur en hexadécimal. |
| 92 | 92 | */ |
| 93 | 93 | function _couleur_hsl_to_hex($hue, $saturation, $lightness) { |
| 94 | - $rgb = _couleur_hsl_to_rgb($hue, $saturation, $lightness); |
|
| 95 | - return _couleur_dec_to_hex($rgb['r'], $rgb['g'], $rgb['b']); |
|
| 94 | + $rgb = _couleur_hsl_to_rgb($hue, $saturation, $lightness); |
|
| 95 | + return _couleur_dec_to_hex($rgb['r'], $rgb['g'], $rgb['b']); |
|
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | /** |
@@ -104,8 +104,8 @@ discard block |
||
| 104 | 104 | * Un tableau des 3 éléments : teinte, saturation, luminosité. |
| 105 | 105 | */ |
| 106 | 106 | function _couleur_hex_to_hsl($couleur) { |
| 107 | - $rgb = _couleur_hex_to_dec($couleur); |
|
| 108 | - return _couleur_rgb_to_hsl($rgb['red'], $rgb['green'], $rgb['blue']); |
|
| 107 | + $rgb = _couleur_hex_to_dec($couleur); |
|
| 108 | + return _couleur_rgb_to_hsl($rgb['red'], $rgb['green'], $rgb['blue']); |
|
| 109 | 109 | } |
| 110 | 110 | |
| 111 | 111 | /** |
@@ -120,55 +120,55 @@ discard block |
||
| 120 | 120 | * @return array |
| 121 | 121 | */ |
| 122 | 122 | function _couleur_rgb_to_hsl($R, $G, $B) { |
| 123 | - $H = null; |
|
| 124 | - $var_R = ($R / 255); // Where RGB values = 0 ÷ 255 |
|
| 125 | - $var_G = ($G / 255); |
|
| 126 | - $var_B = ($B / 255); |
|
| 127 | - |
|
| 128 | - $var_Min = min($var_R, $var_G, $var_B); //Min. value of RGB |
|
| 129 | - $var_Max = max($var_R, $var_G, $var_B); //Max. value of RGB |
|
| 130 | - $del_Max = $var_Max - $var_Min; //Delta RGB value |
|
| 131 | - |
|
| 132 | - $L = ($var_Max + $var_Min) / 2; |
|
| 133 | - |
|
| 134 | - if ($del_Max == 0) { |
|
| 135 | - //This is a gray, no chroma... |
|
| 136 | - $H = 0; //HSL results = 0 ÷ 1 |
|
| 137 | - $S = 0; |
|
| 138 | - } else { |
|
| 139 | - // Chromatic data... |
|
| 140 | - $S = $L < 0.5 ? $del_Max / ($var_Max + $var_Min) : $del_Max / (2 - $var_Max - $var_Min); |
|
| 141 | - |
|
| 142 | - $del_R = ((($var_Max - $var_R) / 6) + ($del_Max / 2)) / $del_Max; |
|
| 143 | - $del_G = ((($var_Max - $var_G) / 6) + ($del_Max / 2)) / $del_Max; |
|
| 144 | - $del_B = ((($var_Max - $var_B) / 6) + ($del_Max / 2)) / $del_Max; |
|
| 145 | - |
|
| 146 | - if ($var_R === $var_Max) { |
|
| 147 | - $H = $del_B - $del_G; |
|
| 148 | - } else { |
|
| 149 | - if ($var_G === $var_Max) { |
|
| 150 | - $H = (1 / 3) + $del_R - $del_B; |
|
| 151 | - } else { |
|
| 152 | - if ($var_B === $var_Max) { |
|
| 153 | - $H = (2 / 3) + $del_G - $del_R; |
|
| 154 | - } |
|
| 155 | - } |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - if ($H < 0) { |
|
| 159 | - $H += 1; |
|
| 160 | - } |
|
| 161 | - if ($H > 1) { |
|
| 162 | - $H -= 1; |
|
| 163 | - } |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - $ret = []; |
|
| 167 | - $ret['h'] = $H; |
|
| 168 | - $ret['s'] = $S; |
|
| 169 | - $ret['l'] = $L; |
|
| 170 | - |
|
| 171 | - return $ret; |
|
| 123 | + $H = null; |
|
| 124 | + $var_R = ($R / 255); // Where RGB values = 0 ÷ 255 |
|
| 125 | + $var_G = ($G / 255); |
|
| 126 | + $var_B = ($B / 255); |
|
| 127 | + |
|
| 128 | + $var_Min = min($var_R, $var_G, $var_B); //Min. value of RGB |
|
| 129 | + $var_Max = max($var_R, $var_G, $var_B); //Max. value of RGB |
|
| 130 | + $del_Max = $var_Max - $var_Min; //Delta RGB value |
|
| 131 | + |
|
| 132 | + $L = ($var_Max + $var_Min) / 2; |
|
| 133 | + |
|
| 134 | + if ($del_Max == 0) { |
|
| 135 | + //This is a gray, no chroma... |
|
| 136 | + $H = 0; //HSL results = 0 ÷ 1 |
|
| 137 | + $S = 0; |
|
| 138 | + } else { |
|
| 139 | + // Chromatic data... |
|
| 140 | + $S = $L < 0.5 ? $del_Max / ($var_Max + $var_Min) : $del_Max / (2 - $var_Max - $var_Min); |
|
| 141 | + |
|
| 142 | + $del_R = ((($var_Max - $var_R) / 6) + ($del_Max / 2)) / $del_Max; |
|
| 143 | + $del_G = ((($var_Max - $var_G) / 6) + ($del_Max / 2)) / $del_Max; |
|
| 144 | + $del_B = ((($var_Max - $var_B) / 6) + ($del_Max / 2)) / $del_Max; |
|
| 145 | + |
|
| 146 | + if ($var_R === $var_Max) { |
|
| 147 | + $H = $del_B - $del_G; |
|
| 148 | + } else { |
|
| 149 | + if ($var_G === $var_Max) { |
|
| 150 | + $H = (1 / 3) + $del_R - $del_B; |
|
| 151 | + } else { |
|
| 152 | + if ($var_B === $var_Max) { |
|
| 153 | + $H = (2 / 3) + $del_G - $del_R; |
|
| 154 | + } |
|
| 155 | + } |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + if ($H < 0) { |
|
| 159 | + $H += 1; |
|
| 160 | + } |
|
| 161 | + if ($H > 1) { |
|
| 162 | + $H -= 1; |
|
| 163 | + } |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + $ret = []; |
|
| 167 | + $ret['h'] = $H; |
|
| 168 | + $ret['s'] = $S; |
|
| 169 | + $ret['l'] = $L; |
|
| 170 | + |
|
| 171 | + return $ret; |
|
| 172 | 172 | } |
| 173 | 173 | |
| 174 | 174 | |
@@ -184,48 +184,48 @@ discard block |
||
| 184 | 184 | * @return array |
| 185 | 185 | */ |
| 186 | 186 | function _couleur_hsl_to_rgb($H, $S, $L) { |
| 187 | - // helper |
|
| 188 | - $hue_2_rgb = function ($v1, $v2, $vH) { |
|
| 189 | - if ($vH < 0) { |
|
| 190 | - $vH += 1; |
|
| 191 | - } |
|
| 192 | - if ($vH > 1) { |
|
| 193 | - $vH -= 1; |
|
| 194 | - } |
|
| 195 | - if ((6 * $vH) < 1) { |
|
| 196 | - return ($v1 + ($v2 - $v1) * 6 * $vH); |
|
| 197 | - } |
|
| 198 | - if ((2 * $vH) < 1) { |
|
| 199 | - return ($v2); |
|
| 200 | - } |
|
| 201 | - if ((3 * $vH) < 2) { |
|
| 202 | - return ($v1 + ($v2 - $v1) * ((2 / 3) - $vH) * 6); |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - return ($v1); |
|
| 206 | - }; |
|
| 207 | - |
|
| 208 | - if ($S == 0) { |
|
| 209 | - // HSV values = 0 -> 1 |
|
| 210 | - $R = $L * 255; |
|
| 211 | - $G = $L * 255; |
|
| 212 | - $B = $L * 255; |
|
| 213 | - } else { |
|
| 214 | - $var_2 = $L < 0.5 ? $L * (1 + $S) : ($L + $S) - ($S * $L); |
|
| 215 | - |
|
| 216 | - $var_1 = 2 * $L - $var_2; |
|
| 217 | - |
|
| 218 | - $R = 255 * $hue_2_rgb($var_1, $var_2, $H + (1 / 3)); |
|
| 219 | - $G = 255 * $hue_2_rgb($var_1, $var_2, $H); |
|
| 220 | - $B = 255 * $hue_2_rgb($var_1, $var_2, $H - (1 / 3)); |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - $ret = []; |
|
| 224 | - $ret['r'] = floor($R); |
|
| 225 | - $ret['g'] = floor($G); |
|
| 226 | - $ret['b'] = floor($B); |
|
| 227 | - |
|
| 228 | - return $ret; |
|
| 187 | + // helper |
|
| 188 | + $hue_2_rgb = function ($v1, $v2, $vH) { |
|
| 189 | + if ($vH < 0) { |
|
| 190 | + $vH += 1; |
|
| 191 | + } |
|
| 192 | + if ($vH > 1) { |
|
| 193 | + $vH -= 1; |
|
| 194 | + } |
|
| 195 | + if ((6 * $vH) < 1) { |
|
| 196 | + return ($v1 + ($v2 - $v1) * 6 * $vH); |
|
| 197 | + } |
|
| 198 | + if ((2 * $vH) < 1) { |
|
| 199 | + return ($v2); |
|
| 200 | + } |
|
| 201 | + if ((3 * $vH) < 2) { |
|
| 202 | + return ($v1 + ($v2 - $v1) * ((2 / 3) - $vH) * 6); |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + return ($v1); |
|
| 206 | + }; |
|
| 207 | + |
|
| 208 | + if ($S == 0) { |
|
| 209 | + // HSV values = 0 -> 1 |
|
| 210 | + $R = $L * 255; |
|
| 211 | + $G = $L * 255; |
|
| 212 | + $B = $L * 255; |
|
| 213 | + } else { |
|
| 214 | + $var_2 = $L < 0.5 ? $L * (1 + $S) : ($L + $S) - ($S * $L); |
|
| 215 | + |
|
| 216 | + $var_1 = 2 * $L - $var_2; |
|
| 217 | + |
|
| 218 | + $R = 255 * $hue_2_rgb($var_1, $var_2, $H + (1 / 3)); |
|
| 219 | + $G = 255 * $hue_2_rgb($var_1, $var_2, $H); |
|
| 220 | + $B = 255 * $hue_2_rgb($var_1, $var_2, $H - (1 / 3)); |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + $ret = []; |
|
| 224 | + $ret['r'] = floor($R); |
|
| 225 | + $ret['g'] = floor($G); |
|
| 226 | + $ret['b'] = floor($B); |
|
| 227 | + |
|
| 228 | + return $ret; |
|
| 229 | 229 | } |
| 230 | 230 | |
| 231 | 231 | /** |
@@ -243,11 +243,11 @@ discard block |
||
| 243 | 243 | * true si il faut supprimer le fichier temporaire ; false sinon. |
| 244 | 244 | */ |
| 245 | 245 | function statut_effacer_images_temporaires($stat) { |
| 246 | - static $statut = false; // par defaut on grave toute les images |
|
| 247 | - if ($stat === 'get') { |
|
| 248 | - return $statut; |
|
| 249 | - } |
|
| 250 | - $statut = (bool) $stat; |
|
| 246 | + static $statut = false; // par defaut on grave toute les images |
|
| 247 | + if ($stat === 'get') { |
|
| 248 | + return $statut; |
|
| 249 | + } |
|
| 250 | + $statut = (bool) $stat; |
|
| 251 | 251 | } |
| 252 | 252 | |
| 253 | 253 | |
@@ -300,250 +300,250 @@ discard block |
||
| 300 | 300 | * - array : tableau décrivant de l'image |
| 301 | 301 | */ |
| 302 | 302 | function _image_valeurs_trans($img, $effet, $forcer_format = false, $fonction_creation = null, $find_in_path = false, $support_svg = false) { |
| 303 | - $valeurs = []; |
|
| 304 | - $ret = []; |
|
| 305 | - $f = null; |
|
| 306 | - static $images_recalcul = []; |
|
| 307 | - if (strlen($img) == 0) { |
|
| 308 | - return false; |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - $source = trim(extraire_attribut($img, 'src') ?? ''); |
|
| 312 | - if (strlen($source) < 1) { |
|
| 313 | - if (!str_starts_with($img, '<img ')) { |
|
| 314 | - $source = $img; |
|
| 315 | - $img = "<img src='$source' />"; |
|
| 316 | - } else { |
|
| 317 | - // pas d'attribut src sur cette balise <img../> |
|
| 318 | - return false; |
|
| 319 | - } |
|
| 320 | - } elseif ( |
|
| 321 | - preg_match('@^data:image/([^;]*);base64,(.*)$@isS', $source, $regs) |
|
| 322 | - && ($extension = _image_trouver_extension_depuis_mime('image/' . $regs[1])) |
|
| 323 | - && in_array($extension, _image_extensions_acceptees_en_entree()) |
|
| 324 | - ) { |
|
| 325 | - # gerer img src="data:....base64" |
|
| 326 | - $local = sous_repertoire(_DIR_VAR, 'image-data') . md5($regs[2]) . '.' . _image_extension_normalisee($extension); |
|
| 327 | - if (!file_exists($local)) { |
|
| 328 | - ecrire_fichier($local, base64_decode($regs[2])); |
|
| 329 | - } |
|
| 330 | - if ($sanitizer = charger_fonction($extension, 'sanitizer', true)) { |
|
| 331 | - $sanitizer($local); |
|
| 332 | - } |
|
| 333 | - $source = $local; |
|
| 334 | - $img = inserer_attribut($img, 'src', $source); |
|
| 335 | - # eviter les mauvaises surprises lors de conversions de format |
|
| 336 | - $img = inserer_attribut($img, 'width', ''); |
|
| 337 | - $img = inserer_attribut($img, 'height', ''); |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - // les protocoles web prennent au moins 3 lettres |
|
| 341 | - if (tester_url_absolue($source)) { |
|
| 342 | - include_spip('inc/distant'); |
|
| 343 | - $fichier = _DIR_RACINE . copie_locale($source); |
|
| 344 | - if (!$fichier) { |
|
| 345 | - return ''; |
|
| 346 | - } |
|
| 347 | - if ( |
|
| 348 | - ($extension = _image_trouver_extension($fichier)) |
|
| 349 | - && ($sanitizer = charger_fonction($extension, 'sanitizer', true)) |
|
| 350 | - ) { |
|
| 351 | - $sanitizer($fichier); |
|
| 352 | - } |
|
| 353 | - } else { |
|
| 354 | - // enlever le timestamp eventuel |
|
| 355 | - if (str_contains($source, '?')) { |
|
| 356 | - $source = preg_replace(',[?]\d+$,', '', $source); |
|
| 357 | - } |
|
| 358 | - if ( |
|
| 359 | - str_contains($source, '?') |
|
| 360 | - && str_starts_with($source, (string) _DIR_IMG) |
|
| 361 | - && file_exists($f = preg_replace(',[?].*$,', '', $source)) |
|
| 362 | - ) { |
|
| 363 | - $source = $f; |
|
| 364 | - } |
|
| 365 | - $fichier = $source; |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - $terminaison_dest = ''; |
|
| 369 | - if ($terminaison = _image_trouver_extension($fichier)) { |
|
| 370 | - $terminaison_dest = ($terminaison == 'gif') ? 'png' : $terminaison; |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - if ( |
|
| 374 | - $forcer_format !== false |
|
| 375 | - && ($terminaison_dest !== 'svg' || $support_svg || !in_array($forcer_format, _image_extensions_acceptees_en_sortie())) |
|
| 376 | - ) { |
|
| 377 | - $terminaison_dest = $forcer_format; |
|
| 378 | - } |
|
| 379 | - |
|
| 380 | - if (!$terminaison_dest) { |
|
| 381 | - return false; |
|
| 382 | - } |
|
| 383 | - |
|
| 384 | - $nom_fichier = substr($fichier, 0, strlen($fichier) - (strlen($terminaison) + 1)); |
|
| 385 | - $fichier_dest = $nom_fichier; |
|
| 386 | - if ( |
|
| 387 | - $find_in_path && ($f = find_in_path($fichier)) && ($fichier = $f) |
|
| 388 | - || @file_exists($f = $fichier) |
|
| 389 | - ) { |
|
| 390 | - // on ne passe jamais la balise img qui est peut-être en x2 et à laquelle on ne peut pas faire confiance |
|
| 391 | - // on lit directement les infos du fichier |
|
| 392 | - [$ret['hauteur'], $ret['largeur']] = taille_image($f); |
|
| 393 | - $date_src = @filemtime($f); |
|
| 394 | - } elseif ( |
|
| 395 | - @file_exists($f = "$fichier.src") |
|
| 396 | - && lire_fichier($f, $valeurs) |
|
| 397 | - && ($valeurs = unserialize($valeurs)) |
|
| 398 | - && isset($valeurs['hauteur_dest']) |
|
| 399 | - && isset($valeurs['largeur_dest']) |
|
| 400 | - ) { |
|
| 401 | - $ret['hauteur'] = $valeurs['hauteur_dest']; |
|
| 402 | - $ret['largeur'] = $valeurs['largeur_dest']; |
|
| 403 | - $date_src = $valeurs['date']; |
|
| 404 | - } // pas de fichier source par la |
|
| 405 | - else { |
|
| 406 | - return false; |
|
| 407 | - } |
|
| 408 | - |
|
| 409 | - // pas de taille mesurable ? |
|
| 410 | - if ( |
|
| 411 | - !$ret['hauteur'] |
|
| 412 | - || !($ret['hauteur'] = (int) round($ret['hauteur'])) |
|
| 413 | - || !$ret['largeur'] |
|
| 414 | - || !($ret['largeur'] = (int) round($ret['largeur'])) |
|
| 415 | - ) { |
|
| 416 | - return false; |
|
| 417 | - } |
|
| 418 | - |
|
| 419 | - // les images calculees dependent du chemin du fichier source |
|
| 420 | - // pour une meme image source et un meme filtre on aboutira a 2 fichiers selon si l'appel est dans le public ou dans le prive |
|
| 421 | - // ce n'est pas totalement optimal en terme de stockage, mais chaque image est associee a un fichier .src |
|
| 422 | - // qui contient la methode de reconstrucion (le filtre + les arguments d'appel) et les arguments different entre prive et public |
|
| 423 | - // la mise en commun du fichier image cree donc un bug et des problemes qui necessiteraient beaucoup de complexite de code |
|
| 424 | - // alors que ca concerne peu de site au final |
|
| 425 | - // la release de r23632+r23633+r23634 a provoque peu de remontee de bug attestant du peu de sites impactes |
|
| 426 | - $identifiant = $fichier; |
|
| 427 | - |
|
| 428 | - // cas general : |
|
| 429 | - // on a un dossier cache commun et un nom de fichier qui varie avec l'effet |
|
| 430 | - // cas particulier de reduire : |
|
| 431 | - // un cache par dimension, et le nom de fichier est conserve, suffixe par la dimension aussi |
|
| 432 | - $cache = 'cache-gd2'; |
|
| 433 | - if (str_starts_with($effet, 'reduire')) { |
|
| 434 | - [, $maxWidth, $maxHeight] = explode('-', $effet); |
|
| 435 | - [$destWidth, $destHeight] = _image_ratio($ret['largeur'], $ret['hauteur'], $maxWidth, $maxHeight); |
|
| 436 | - $ret['largeur_dest'] = $destWidth; |
|
| 437 | - $ret['hauteur_dest'] = $destHeight; |
|
| 438 | - $effet = "L{$destWidth}xH$destHeight"; |
|
| 439 | - $cache = 'cache-vignettes'; |
|
| 440 | - $fichier_dest = basename($fichier_dest); |
|
| 441 | - if (($ret['largeur'] <= $maxWidth) && ($ret['hauteur'] <= $maxHeight)) { |
|
| 442 | - // on garde la terminaison initiale car image simplement copiee |
|
| 443 | - // et on postfixe son nom avec un md5 du path |
|
| 444 | - $terminaison_dest = $terminaison; |
|
| 445 | - $fichier_dest .= '-' . substr(md5("$identifiant"), 0, 5); |
|
| 446 | - } else { |
|
| 447 | - $fichier_dest .= '-' . substr(md5("$identifiant-$effet"), 0, 5); |
|
| 448 | - } |
|
| 449 | - $cache = sous_repertoire(_DIR_VAR, $cache); |
|
| 450 | - $cache = sous_repertoire($cache, $effet); |
|
| 451 | - } else { |
|
| 452 | - $fichier_dest = md5("$identifiant-$effet"); |
|
| 453 | - $cache = sous_repertoire(_DIR_VAR, $cache); |
|
| 454 | - $cache = sous_repertoire($cache, substr($fichier_dest, 0, 2)); |
|
| 455 | - $fichier_dest = substr($fichier_dest, 2); |
|
| 456 | - } |
|
| 457 | - |
|
| 458 | - $fichier_dest = $cache . $fichier_dest . '.' . $terminaison_dest; |
|
| 459 | - |
|
| 460 | - $GLOBALS['images_calculees'][] = $fichier_dest; |
|
| 461 | - |
|
| 462 | - $creer = true; |
|
| 463 | - // si recalcul des images demande, recalculer chaque image une fois |
|
| 464 | - if (defined('_VAR_IMAGES') && _VAR_IMAGES && !isset($images_recalcul[$fichier_dest])) { |
|
| 465 | - $images_recalcul[$fichier_dest] = true; |
|
| 466 | - } else { |
|
| 467 | - if (@file_exists($f = $fichier_dest)) { |
|
| 468 | - if (filemtime($f) >= $date_src) { |
|
| 469 | - $creer = false; |
|
| 470 | - } |
|
| 471 | - } else { |
|
| 472 | - if ( |
|
| 473 | - @file_exists($f = "$fichier_dest.src") |
|
| 474 | - && lire_fichier($f, $valeurs) |
|
| 475 | - && ($valeurs = unserialize($valeurs)) |
|
| 476 | - && $valeurs['date'] >= $date_src |
|
| 477 | - ) { |
|
| 478 | - $creer = false; |
|
| 479 | - } |
|
| 480 | - } |
|
| 481 | - } |
|
| 482 | - if ($creer && !@file_exists($fichier)) { |
|
| 483 | - if (!@file_exists("$fichier.src")) { |
|
| 484 | - spip_logger('images')->error("Image absente : $fichier"); |
|
| 485 | - |
|
| 486 | - return false; |
|
| 487 | - } |
|
| 488 | - # on reconstruit l'image source absente a partir de la chaine des .src |
|
| 489 | - reconstruire_image_intermediaire($fichier); |
|
| 490 | - } |
|
| 491 | - |
|
| 492 | - if ($creer) { |
|
| 493 | - spip_logger('images')->error( |
|
| 494 | - 'filtre image ' . ($fonction_creation ? reset($fonction_creation) : '') . "[$effet] sur $fichier", |
|
| 495 | - ); |
|
| 496 | - } |
|
| 497 | - |
|
| 498 | - $term_fonction = _image_trouver_extension_pertinente($fichier); |
|
| 499 | - $ret['fonction_imagecreatefrom'] = '_imagecreatefrom' . $term_fonction; |
|
| 500 | - $ret['fichier'] = $fichier; |
|
| 501 | - $ret['fonction_image'] = '_image_image' . $terminaison_dest; |
|
| 502 | - $ret['fichier_dest'] = $fichier_dest; |
|
| 503 | - $ret['format_source'] = _image_extension_normalisee($terminaison); |
|
| 504 | - $ret['format_dest'] = $terminaison_dest; |
|
| 505 | - $ret['date_src'] = $date_src; |
|
| 506 | - $ret['creer'] = $creer; |
|
| 507 | - $ret['class'] = extraire_attribut($img, 'class'); |
|
| 508 | - $ret['alt'] = extraire_attribut($img, 'alt'); |
|
| 509 | - $ret['style'] = extraire_attribut($img, 'style'); |
|
| 510 | - $ret['tag'] = $img; |
|
| 511 | - if ($fonction_creation) { |
|
| 512 | - $ret['reconstruction'] = $fonction_creation; |
|
| 513 | - # ecrire ici comment creer le fichier, car il est pas sur qu'on l'ecrira reelement |
|
| 514 | - # cas de image_reduire qui finalement ne reduit pas l'image source |
|
| 515 | - # ca evite d'essayer de le creer au prochain hit si il n'est pas la |
|
| 516 | - #ecrire_fichier($ret['fichier_dest'].'.src',serialize($ret),true); |
|
| 517 | - } |
|
| 518 | - |
|
| 519 | - $ret = pipeline('image_preparer_filtre', [ |
|
| 520 | - 'args' => [ |
|
| 521 | - 'img' => $img, |
|
| 522 | - 'effet' => $effet, |
|
| 523 | - 'forcer_format' => $forcer_format, |
|
| 524 | - 'fonction_creation' => $fonction_creation, |
|
| 525 | - 'find_in_path' => $find_in_path, |
|
| 526 | - ], |
|
| 527 | - 'data' => $ret |
|
| 528 | - ]); |
|
| 529 | - |
|
| 530 | - // une globale pour le debug en cas de crash memoire |
|
| 531 | - $GLOBALS['derniere_image_calculee'] = $ret; |
|
| 532 | - |
|
| 533 | - // traiter le cas particulier des SVG : si le filtre n'a pas annonce explicitement qu'il savait faire, on delegue |
|
| 534 | - if ($term_fonction === 'svg') { |
|
| 535 | - if ($creer && !$support_svg) { |
|
| 536 | - process_image_svg_identite($ret); |
|
| 537 | - $ret['creer'] = false; |
|
| 538 | - } |
|
| 539 | - } |
|
| 540 | - else { |
|
| 541 | - if (!function_exists($ret['fonction_imagecreatefrom'])) { |
|
| 542 | - return false; |
|
| 543 | - } |
|
| 544 | - } |
|
| 545 | - |
|
| 546 | - return $ret; |
|
| 303 | + $valeurs = []; |
|
| 304 | + $ret = []; |
|
| 305 | + $f = null; |
|
| 306 | + static $images_recalcul = []; |
|
| 307 | + if (strlen($img) == 0) { |
|
| 308 | + return false; |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + $source = trim(extraire_attribut($img, 'src') ?? ''); |
|
| 312 | + if (strlen($source) < 1) { |
|
| 313 | + if (!str_starts_with($img, '<img ')) { |
|
| 314 | + $source = $img; |
|
| 315 | + $img = "<img src='$source' />"; |
|
| 316 | + } else { |
|
| 317 | + // pas d'attribut src sur cette balise <img../> |
|
| 318 | + return false; |
|
| 319 | + } |
|
| 320 | + } elseif ( |
|
| 321 | + preg_match('@^data:image/([^;]*);base64,(.*)$@isS', $source, $regs) |
|
| 322 | + && ($extension = _image_trouver_extension_depuis_mime('image/' . $regs[1])) |
|
| 323 | + && in_array($extension, _image_extensions_acceptees_en_entree()) |
|
| 324 | + ) { |
|
| 325 | + # gerer img src="data:....base64" |
|
| 326 | + $local = sous_repertoire(_DIR_VAR, 'image-data') . md5($regs[2]) . '.' . _image_extension_normalisee($extension); |
|
| 327 | + if (!file_exists($local)) { |
|
| 328 | + ecrire_fichier($local, base64_decode($regs[2])); |
|
| 329 | + } |
|
| 330 | + if ($sanitizer = charger_fonction($extension, 'sanitizer', true)) { |
|
| 331 | + $sanitizer($local); |
|
| 332 | + } |
|
| 333 | + $source = $local; |
|
| 334 | + $img = inserer_attribut($img, 'src', $source); |
|
| 335 | + # eviter les mauvaises surprises lors de conversions de format |
|
| 336 | + $img = inserer_attribut($img, 'width', ''); |
|
| 337 | + $img = inserer_attribut($img, 'height', ''); |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + // les protocoles web prennent au moins 3 lettres |
|
| 341 | + if (tester_url_absolue($source)) { |
|
| 342 | + include_spip('inc/distant'); |
|
| 343 | + $fichier = _DIR_RACINE . copie_locale($source); |
|
| 344 | + if (!$fichier) { |
|
| 345 | + return ''; |
|
| 346 | + } |
|
| 347 | + if ( |
|
| 348 | + ($extension = _image_trouver_extension($fichier)) |
|
| 349 | + && ($sanitizer = charger_fonction($extension, 'sanitizer', true)) |
|
| 350 | + ) { |
|
| 351 | + $sanitizer($fichier); |
|
| 352 | + } |
|
| 353 | + } else { |
|
| 354 | + // enlever le timestamp eventuel |
|
| 355 | + if (str_contains($source, '?')) { |
|
| 356 | + $source = preg_replace(',[?]\d+$,', '', $source); |
|
| 357 | + } |
|
| 358 | + if ( |
|
| 359 | + str_contains($source, '?') |
|
| 360 | + && str_starts_with($source, (string) _DIR_IMG) |
|
| 361 | + && file_exists($f = preg_replace(',[?].*$,', '', $source)) |
|
| 362 | + ) { |
|
| 363 | + $source = $f; |
|
| 364 | + } |
|
| 365 | + $fichier = $source; |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + $terminaison_dest = ''; |
|
| 369 | + if ($terminaison = _image_trouver_extension($fichier)) { |
|
| 370 | + $terminaison_dest = ($terminaison == 'gif') ? 'png' : $terminaison; |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + if ( |
|
| 374 | + $forcer_format !== false |
|
| 375 | + && ($terminaison_dest !== 'svg' || $support_svg || !in_array($forcer_format, _image_extensions_acceptees_en_sortie())) |
|
| 376 | + ) { |
|
| 377 | + $terminaison_dest = $forcer_format; |
|
| 378 | + } |
|
| 379 | + |
|
| 380 | + if (!$terminaison_dest) { |
|
| 381 | + return false; |
|
| 382 | + } |
|
| 383 | + |
|
| 384 | + $nom_fichier = substr($fichier, 0, strlen($fichier) - (strlen($terminaison) + 1)); |
|
| 385 | + $fichier_dest = $nom_fichier; |
|
| 386 | + if ( |
|
| 387 | + $find_in_path && ($f = find_in_path($fichier)) && ($fichier = $f) |
|
| 388 | + || @file_exists($f = $fichier) |
|
| 389 | + ) { |
|
| 390 | + // on ne passe jamais la balise img qui est peut-être en x2 et à laquelle on ne peut pas faire confiance |
|
| 391 | + // on lit directement les infos du fichier |
|
| 392 | + [$ret['hauteur'], $ret['largeur']] = taille_image($f); |
|
| 393 | + $date_src = @filemtime($f); |
|
| 394 | + } elseif ( |
|
| 395 | + @file_exists($f = "$fichier.src") |
|
| 396 | + && lire_fichier($f, $valeurs) |
|
| 397 | + && ($valeurs = unserialize($valeurs)) |
|
| 398 | + && isset($valeurs['hauteur_dest']) |
|
| 399 | + && isset($valeurs['largeur_dest']) |
|
| 400 | + ) { |
|
| 401 | + $ret['hauteur'] = $valeurs['hauteur_dest']; |
|
| 402 | + $ret['largeur'] = $valeurs['largeur_dest']; |
|
| 403 | + $date_src = $valeurs['date']; |
|
| 404 | + } // pas de fichier source par la |
|
| 405 | + else { |
|
| 406 | + return false; |
|
| 407 | + } |
|
| 408 | + |
|
| 409 | + // pas de taille mesurable ? |
|
| 410 | + if ( |
|
| 411 | + !$ret['hauteur'] |
|
| 412 | + || !($ret['hauteur'] = (int) round($ret['hauteur'])) |
|
| 413 | + || !$ret['largeur'] |
|
| 414 | + || !($ret['largeur'] = (int) round($ret['largeur'])) |
|
| 415 | + ) { |
|
| 416 | + return false; |
|
| 417 | + } |
|
| 418 | + |
|
| 419 | + // les images calculees dependent du chemin du fichier source |
|
| 420 | + // pour une meme image source et un meme filtre on aboutira a 2 fichiers selon si l'appel est dans le public ou dans le prive |
|
| 421 | + // ce n'est pas totalement optimal en terme de stockage, mais chaque image est associee a un fichier .src |
|
| 422 | + // qui contient la methode de reconstrucion (le filtre + les arguments d'appel) et les arguments different entre prive et public |
|
| 423 | + // la mise en commun du fichier image cree donc un bug et des problemes qui necessiteraient beaucoup de complexite de code |
|
| 424 | + // alors que ca concerne peu de site au final |
|
| 425 | + // la release de r23632+r23633+r23634 a provoque peu de remontee de bug attestant du peu de sites impactes |
|
| 426 | + $identifiant = $fichier; |
|
| 427 | + |
|
| 428 | + // cas general : |
|
| 429 | + // on a un dossier cache commun et un nom de fichier qui varie avec l'effet |
|
| 430 | + // cas particulier de reduire : |
|
| 431 | + // un cache par dimension, et le nom de fichier est conserve, suffixe par la dimension aussi |
|
| 432 | + $cache = 'cache-gd2'; |
|
| 433 | + if (str_starts_with($effet, 'reduire')) { |
|
| 434 | + [, $maxWidth, $maxHeight] = explode('-', $effet); |
|
| 435 | + [$destWidth, $destHeight] = _image_ratio($ret['largeur'], $ret['hauteur'], $maxWidth, $maxHeight); |
|
| 436 | + $ret['largeur_dest'] = $destWidth; |
|
| 437 | + $ret['hauteur_dest'] = $destHeight; |
|
| 438 | + $effet = "L{$destWidth}xH$destHeight"; |
|
| 439 | + $cache = 'cache-vignettes'; |
|
| 440 | + $fichier_dest = basename($fichier_dest); |
|
| 441 | + if (($ret['largeur'] <= $maxWidth) && ($ret['hauteur'] <= $maxHeight)) { |
|
| 442 | + // on garde la terminaison initiale car image simplement copiee |
|
| 443 | + // et on postfixe son nom avec un md5 du path |
|
| 444 | + $terminaison_dest = $terminaison; |
|
| 445 | + $fichier_dest .= '-' . substr(md5("$identifiant"), 0, 5); |
|
| 446 | + } else { |
|
| 447 | + $fichier_dest .= '-' . substr(md5("$identifiant-$effet"), 0, 5); |
|
| 448 | + } |
|
| 449 | + $cache = sous_repertoire(_DIR_VAR, $cache); |
|
| 450 | + $cache = sous_repertoire($cache, $effet); |
|
| 451 | + } else { |
|
| 452 | + $fichier_dest = md5("$identifiant-$effet"); |
|
| 453 | + $cache = sous_repertoire(_DIR_VAR, $cache); |
|
| 454 | + $cache = sous_repertoire($cache, substr($fichier_dest, 0, 2)); |
|
| 455 | + $fichier_dest = substr($fichier_dest, 2); |
|
| 456 | + } |
|
| 457 | + |
|
| 458 | + $fichier_dest = $cache . $fichier_dest . '.' . $terminaison_dest; |
|
| 459 | + |
|
| 460 | + $GLOBALS['images_calculees'][] = $fichier_dest; |
|
| 461 | + |
|
| 462 | + $creer = true; |
|
| 463 | + // si recalcul des images demande, recalculer chaque image une fois |
|
| 464 | + if (defined('_VAR_IMAGES') && _VAR_IMAGES && !isset($images_recalcul[$fichier_dest])) { |
|
| 465 | + $images_recalcul[$fichier_dest] = true; |
|
| 466 | + } else { |
|
| 467 | + if (@file_exists($f = $fichier_dest)) { |
|
| 468 | + if (filemtime($f) >= $date_src) { |
|
| 469 | + $creer = false; |
|
| 470 | + } |
|
| 471 | + } else { |
|
| 472 | + if ( |
|
| 473 | + @file_exists($f = "$fichier_dest.src") |
|
| 474 | + && lire_fichier($f, $valeurs) |
|
| 475 | + && ($valeurs = unserialize($valeurs)) |
|
| 476 | + && $valeurs['date'] >= $date_src |
|
| 477 | + ) { |
|
| 478 | + $creer = false; |
|
| 479 | + } |
|
| 480 | + } |
|
| 481 | + } |
|
| 482 | + if ($creer && !@file_exists($fichier)) { |
|
| 483 | + if (!@file_exists("$fichier.src")) { |
|
| 484 | + spip_logger('images')->error("Image absente : $fichier"); |
|
| 485 | + |
|
| 486 | + return false; |
|
| 487 | + } |
|
| 488 | + # on reconstruit l'image source absente a partir de la chaine des .src |
|
| 489 | + reconstruire_image_intermediaire($fichier); |
|
| 490 | + } |
|
| 491 | + |
|
| 492 | + if ($creer) { |
|
| 493 | + spip_logger('images')->error( |
|
| 494 | + 'filtre image ' . ($fonction_creation ? reset($fonction_creation) : '') . "[$effet] sur $fichier", |
|
| 495 | + ); |
|
| 496 | + } |
|
| 497 | + |
|
| 498 | + $term_fonction = _image_trouver_extension_pertinente($fichier); |
|
| 499 | + $ret['fonction_imagecreatefrom'] = '_imagecreatefrom' . $term_fonction; |
|
| 500 | + $ret['fichier'] = $fichier; |
|
| 501 | + $ret['fonction_image'] = '_image_image' . $terminaison_dest; |
|
| 502 | + $ret['fichier_dest'] = $fichier_dest; |
|
| 503 | + $ret['format_source'] = _image_extension_normalisee($terminaison); |
|
| 504 | + $ret['format_dest'] = $terminaison_dest; |
|
| 505 | + $ret['date_src'] = $date_src; |
|
| 506 | + $ret['creer'] = $creer; |
|
| 507 | + $ret['class'] = extraire_attribut($img, 'class'); |
|
| 508 | + $ret['alt'] = extraire_attribut($img, 'alt'); |
|
| 509 | + $ret['style'] = extraire_attribut($img, 'style'); |
|
| 510 | + $ret['tag'] = $img; |
|
| 511 | + if ($fonction_creation) { |
|
| 512 | + $ret['reconstruction'] = $fonction_creation; |
|
| 513 | + # ecrire ici comment creer le fichier, car il est pas sur qu'on l'ecrira reelement |
|
| 514 | + # cas de image_reduire qui finalement ne reduit pas l'image source |
|
| 515 | + # ca evite d'essayer de le creer au prochain hit si il n'est pas la |
|
| 516 | + #ecrire_fichier($ret['fichier_dest'].'.src',serialize($ret),true); |
|
| 517 | + } |
|
| 518 | + |
|
| 519 | + $ret = pipeline('image_preparer_filtre', [ |
|
| 520 | + 'args' => [ |
|
| 521 | + 'img' => $img, |
|
| 522 | + 'effet' => $effet, |
|
| 523 | + 'forcer_format' => $forcer_format, |
|
| 524 | + 'fonction_creation' => $fonction_creation, |
|
| 525 | + 'find_in_path' => $find_in_path, |
|
| 526 | + ], |
|
| 527 | + 'data' => $ret |
|
| 528 | + ]); |
|
| 529 | + |
|
| 530 | + // une globale pour le debug en cas de crash memoire |
|
| 531 | + $GLOBALS['derniere_image_calculee'] = $ret; |
|
| 532 | + |
|
| 533 | + // traiter le cas particulier des SVG : si le filtre n'a pas annonce explicitement qu'il savait faire, on delegue |
|
| 534 | + if ($term_fonction === 'svg') { |
|
| 535 | + if ($creer && !$support_svg) { |
|
| 536 | + process_image_svg_identite($ret); |
|
| 537 | + $ret['creer'] = false; |
|
| 538 | + } |
|
| 539 | + } |
|
| 540 | + else { |
|
| 541 | + if (!function_exists($ret['fonction_imagecreatefrom'])) { |
|
| 542 | + return false; |
|
| 543 | + } |
|
| 544 | + } |
|
| 545 | + |
|
| 546 | + return $ret; |
|
| 547 | 547 | } |
| 548 | 548 | |
| 549 | 549 | |
@@ -551,54 +551,54 @@ discard block |
||
| 551 | 551 | * Extensions d’images acceptées en entrée |
| 552 | 552 | */ |
| 553 | 553 | function _image_extensions_acceptees_en_entree(): array { |
| 554 | - static $extensions = null; |
|
| 555 | - if (empty($extensions)) { |
|
| 556 | - $extensions = ['png', 'gif', 'jpg', 'jpeg']; |
|
| 557 | - if (!empty($GLOBALS['meta']['gd_formats'])) { |
|
| 558 | - // action=tester renseigne gd_formats et detecte le support de webp |
|
| 559 | - $extensions = array_merge(explode(',', (string) $GLOBALS['meta']['gd_formats'])); |
|
| 560 | - $extensions = array_map('trim', $extensions); |
|
| 561 | - $extensions = array_filter($extensions); |
|
| 562 | - if (in_array('jpg', $extensions)) { |
|
| 563 | - $extensions[] = 'jpeg'; |
|
| 564 | - } |
|
| 565 | - $extensions = array_unique($extensions); |
|
| 566 | - } |
|
| 567 | - $extensions[] = 'svg'; // on le supporte toujours avec des fonctions specifiques |
|
| 568 | - } |
|
| 569 | - |
|
| 570 | - return $extensions; |
|
| 554 | + static $extensions = null; |
|
| 555 | + if (empty($extensions)) { |
|
| 556 | + $extensions = ['png', 'gif', 'jpg', 'jpeg']; |
|
| 557 | + if (!empty($GLOBALS['meta']['gd_formats'])) { |
|
| 558 | + // action=tester renseigne gd_formats et detecte le support de webp |
|
| 559 | + $extensions = array_merge(explode(',', (string) $GLOBALS['meta']['gd_formats'])); |
|
| 560 | + $extensions = array_map('trim', $extensions); |
|
| 561 | + $extensions = array_filter($extensions); |
|
| 562 | + if (in_array('jpg', $extensions)) { |
|
| 563 | + $extensions[] = 'jpeg'; |
|
| 564 | + } |
|
| 565 | + $extensions = array_unique($extensions); |
|
| 566 | + } |
|
| 567 | + $extensions[] = 'svg'; // on le supporte toujours avec des fonctions specifiques |
|
| 568 | + } |
|
| 569 | + |
|
| 570 | + return $extensions; |
|
| 571 | 571 | } |
| 572 | 572 | |
| 573 | 573 | /** |
| 574 | 574 | * Extensions d’images acceptées en sortie |
| 575 | 575 | */ |
| 576 | 576 | function _image_extensions_acceptees_en_sortie(): array { |
| 577 | - static $extensions = null; |
|
| 578 | - if (empty($extensions)) { |
|
| 579 | - $extensions = _image_extensions_acceptees_en_entree(); |
|
| 580 | - $extensions = array_diff($extensions, ['jpeg']); |
|
| 581 | - if (in_array('gif', $extensions) && !function_exists('imagegif')) { |
|
| 582 | - $extensions = array_diff($extensions, ['gif']); |
|
| 583 | - } |
|
| 584 | - if (in_array('webp', $extensions) && !function_exists('imagewebp')) { |
|
| 585 | - $extensions = array_diff($extensions, ['webp']); |
|
| 586 | - } |
|
| 587 | - } |
|
| 588 | - |
|
| 589 | - return $extensions; |
|
| 577 | + static $extensions = null; |
|
| 578 | + if (empty($extensions)) { |
|
| 579 | + $extensions = _image_extensions_acceptees_en_entree(); |
|
| 580 | + $extensions = array_diff($extensions, ['jpeg']); |
|
| 581 | + if (in_array('gif', $extensions) && !function_exists('imagegif')) { |
|
| 582 | + $extensions = array_diff($extensions, ['gif']); |
|
| 583 | + } |
|
| 584 | + if (in_array('webp', $extensions) && !function_exists('imagewebp')) { |
|
| 585 | + $extensions = array_diff($extensions, ['webp']); |
|
| 586 | + } |
|
| 587 | + } |
|
| 588 | + |
|
| 589 | + return $extensions; |
|
| 590 | 590 | } |
| 591 | 591 | |
| 592 | 592 | function _image_extension_normalisee($extension) { |
| 593 | - $extension = strtolower((string) $extension); |
|
| 594 | - if ($extension === 'jpeg') { |
|
| 595 | - $extension = 'jpg'; |
|
| 596 | - } |
|
| 597 | - return $extension; |
|
| 593 | + $extension = strtolower((string) $extension); |
|
| 594 | + if ($extension === 'jpeg') { |
|
| 595 | + $extension = 'jpg'; |
|
| 596 | + } |
|
| 597 | + return $extension; |
|
| 598 | 598 | } |
| 599 | 599 | |
| 600 | 600 | function _image_extensions_conservent_transparence() { |
| 601 | - return ['png', 'webp']; |
|
| 601 | + return ['png', 'webp']; |
|
| 602 | 602 | } |
| 603 | 603 | |
| 604 | 604 | |
@@ -608,11 +608,11 @@ discard block |
||
| 608 | 608 | * @return string |
| 609 | 609 | */ |
| 610 | 610 | function _image_trouver_extension($path) { |
| 611 | - $preg_extensions = implode('|', _image_extensions_acceptees_en_entree()); |
|
| 612 | - if (preg_match(",\.($preg_extensions)($|[?]),i", $path, $regs)) { |
|
| 613 | - return strtolower($regs[1]); |
|
| 614 | - } |
|
| 615 | - return ''; |
|
| 611 | + $preg_extensions = implode('|', _image_extensions_acceptees_en_entree()); |
|
| 612 | + if (preg_match(",\.($preg_extensions)($|[?]),i", $path, $regs)) { |
|
| 613 | + return strtolower($regs[1]); |
|
| 614 | + } |
|
| 615 | + return ''; |
|
| 616 | 616 | } |
| 617 | 617 | |
| 618 | 618 | /** |
@@ -623,42 +623,42 @@ discard block |
||
| 623 | 623 | * @return string Extension, dans le format attendu par les fonctions 'gd' ('jpeg' pour les .jpg par exemple) |
| 624 | 624 | */ |
| 625 | 625 | function _image_trouver_extension_pertinente($path) { |
| 626 | - $path = supprimer_timestamp($path); |
|
| 627 | - $terminaison = _image_trouver_extension($path); |
|
| 628 | - if ($terminaison == 'jpg') { |
|
| 629 | - $terminaison = 'jpeg'; |
|
| 630 | - } |
|
| 631 | - |
|
| 632 | - if (!file_exists($path)) { |
|
| 633 | - return $terminaison; |
|
| 634 | - } |
|
| 635 | - |
|
| 636 | - if (!$info = @spip_getimagesize($path)) { |
|
| 637 | - return $terminaison; |
|
| 638 | - } |
|
| 639 | - |
|
| 640 | - $mime = $info['mime'] ?? image_type_to_mime_type($info[2]); |
|
| 641 | - |
|
| 642 | - $_terminaison = _image_trouver_extension_depuis_mime($mime); |
|
| 643 | - if ($_terminaison && $_terminaison !== $terminaison) { |
|
| 644 | - spip_logger('images')->notice("Mauvaise extension du fichier : $path . Son type mime est : $mime"); |
|
| 645 | - $terminaison = $_terminaison; |
|
| 646 | - } |
|
| 647 | - return $terminaison; |
|
| 626 | + $path = supprimer_timestamp($path); |
|
| 627 | + $terminaison = _image_trouver_extension($path); |
|
| 628 | + if ($terminaison == 'jpg') { |
|
| 629 | + $terminaison = 'jpeg'; |
|
| 630 | + } |
|
| 631 | + |
|
| 632 | + if (!file_exists($path)) { |
|
| 633 | + return $terminaison; |
|
| 634 | + } |
|
| 635 | + |
|
| 636 | + if (!$info = @spip_getimagesize($path)) { |
|
| 637 | + return $terminaison; |
|
| 638 | + } |
|
| 639 | + |
|
| 640 | + $mime = $info['mime'] ?? image_type_to_mime_type($info[2]); |
|
| 641 | + |
|
| 642 | + $_terminaison = _image_trouver_extension_depuis_mime($mime); |
|
| 643 | + if ($_terminaison && $_terminaison !== $terminaison) { |
|
| 644 | + spip_logger('images')->notice("Mauvaise extension du fichier : $path . Son type mime est : $mime"); |
|
| 645 | + $terminaison = $_terminaison; |
|
| 646 | + } |
|
| 647 | + return $terminaison; |
|
| 648 | 648 | } |
| 649 | 649 | |
| 650 | 650 | /** |
| 651 | 651 | * Retourne une extension d’image depuis un mime-type |
| 652 | 652 | */ |
| 653 | 653 | function _image_trouver_extension_depuis_mime(string $mime): string { |
| 654 | - return match (strtolower($mime)) { |
|
| 655 | - 'image/png', 'image/x-png' => 'png', |
|
| 656 | - 'image/jpg', 'image/jpeg', 'image/pjpeg' => 'jpeg', |
|
| 657 | - 'image/gif' => 'gif', |
|
| 658 | - 'image/webp', 'image/x-webp' => 'webp', |
|
| 659 | - 'image/svg+xml' => 'svg', |
|
| 660 | - default => '', |
|
| 661 | - }; |
|
| 654 | + return match (strtolower($mime)) { |
|
| 655 | + 'image/png', 'image/x-png' => 'png', |
|
| 656 | + 'image/jpg', 'image/jpeg', 'image/pjpeg' => 'jpeg', |
|
| 657 | + 'image/gif' => 'gif', |
|
| 658 | + 'image/webp', 'image/x-webp' => 'webp', |
|
| 659 | + 'image/svg+xml' => 'svg', |
|
| 660 | + default => '', |
|
| 661 | + }; |
|
| 662 | 662 | } |
| 663 | 663 | |
| 664 | 664 | |
@@ -678,18 +678,18 @@ discard block |
||
| 678 | 678 | * Une ressource de type Image GD. |
| 679 | 679 | */ |
| 680 | 680 | function _imagecreatefrom_func(string $func, string $filename) { |
| 681 | - if (!function_exists($func)) { |
|
| 682 | - spip_logger('images')->critical("GD indisponible : $func inexistante. Traitement $filename impossible."); |
|
| 683 | - erreur_squelette("GD indisponible : $func inexistante. Traitement $filename impossible."); |
|
| 684 | - return null; |
|
| 685 | - } |
|
| 686 | - $img = @$func($filename); |
|
| 687 | - if (!$img) { |
|
| 688 | - spip_logger('images')->critical("Erreur lecture $func $filename"); |
|
| 689 | - erreur_squelette("Erreur lecture $func $filename"); |
|
| 690 | - $img = imagecreate(10, 10); |
|
| 691 | - } |
|
| 692 | - return $img; |
|
| 681 | + if (!function_exists($func)) { |
|
| 682 | + spip_logger('images')->critical("GD indisponible : $func inexistante. Traitement $filename impossible."); |
|
| 683 | + erreur_squelette("GD indisponible : $func inexistante. Traitement $filename impossible."); |
|
| 684 | + return null; |
|
| 685 | + } |
|
| 686 | + $img = @$func($filename); |
|
| 687 | + if (!$img) { |
|
| 688 | + spip_logger('images')->critical("Erreur lecture $func $filename"); |
|
| 689 | + erreur_squelette("Erreur lecture $func $filename"); |
|
| 690 | + $img = imagecreate(10, 10); |
|
| 691 | + } |
|
| 692 | + return $img; |
|
| 693 | 693 | } |
| 694 | 694 | |
| 695 | 695 | /** |
@@ -705,7 +705,7 @@ discard block |
||
| 705 | 705 | * Une ressource de type Image GD. |
| 706 | 706 | */ |
| 707 | 707 | function _imagecreatefromjpeg($filename) { |
| 708 | - return _imagecreatefrom_func('imagecreatefromjpeg', $filename); |
|
| 708 | + return _imagecreatefrom_func('imagecreatefromjpeg', $filename); |
|
| 709 | 709 | } |
| 710 | 710 | |
| 711 | 711 | /** |
@@ -721,7 +721,7 @@ discard block |
||
| 721 | 721 | * Une ressource de type Image GD. |
| 722 | 722 | */ |
| 723 | 723 | function _imagecreatefrompng($filename) { |
| 724 | - return _imagecreatefrom_func('imagecreatefrompng', $filename); |
|
| 724 | + return _imagecreatefrom_func('imagecreatefrompng', $filename); |
|
| 725 | 725 | } |
| 726 | 726 | |
| 727 | 727 | /** |
@@ -737,7 +737,7 @@ discard block |
||
| 737 | 737 | * Une ressource de type Image GD. |
| 738 | 738 | */ |
| 739 | 739 | function _imagecreatefromgif($filename) { |
| 740 | - return _imagecreatefrom_func('imagecreatefromgif', $filename); |
|
| 740 | + return _imagecreatefrom_func('imagecreatefromgif', $filename); |
|
| 741 | 741 | } |
| 742 | 742 | |
| 743 | 743 | |
@@ -754,7 +754,7 @@ discard block |
||
| 754 | 754 | * Une ressource de type Image GD. |
| 755 | 755 | */ |
| 756 | 756 | function _imagecreatefromwebp($filename) { |
| 757 | - return _imagecreatefrom_func('imagecreatefromwebp', $filename); |
|
| 757 | + return _imagecreatefrom_func('imagecreatefromwebp', $filename); |
|
| 758 | 758 | } |
| 759 | 759 | |
| 760 | 760 | /** |
@@ -772,24 +772,24 @@ discard block |
||
| 772 | 772 | * - true si une image est bien retournée. |
| 773 | 773 | */ |
| 774 | 774 | function _image_imagepng($img, $fichier) { |
| 775 | - if (!function_exists('imagepng')) { |
|
| 776 | - return false; |
|
| 777 | - } |
|
| 778 | - $tmp = $fichier . '.tmp'; |
|
| 779 | - $ret = imagepng($img, $tmp); |
|
| 780 | - if (file_exists($tmp)) { |
|
| 781 | - $taille_test = @getimagesize($tmp); |
|
| 782 | - if (empty($taille_test[0])) { |
|
| 783 | - return false; |
|
| 784 | - } |
|
| 785 | - |
|
| 786 | - spip_unlink($fichier); // le fichier peut deja exister |
|
| 787 | - @rename($tmp, $fichier); |
|
| 788 | - |
|
| 789 | - return $ret; |
|
| 790 | - } |
|
| 791 | - |
|
| 792 | - return false; |
|
| 775 | + if (!function_exists('imagepng')) { |
|
| 776 | + return false; |
|
| 777 | + } |
|
| 778 | + $tmp = $fichier . '.tmp'; |
|
| 779 | + $ret = imagepng($img, $tmp); |
|
| 780 | + if (file_exists($tmp)) { |
|
| 781 | + $taille_test = @getimagesize($tmp); |
|
| 782 | + if (empty($taille_test[0])) { |
|
| 783 | + return false; |
|
| 784 | + } |
|
| 785 | + |
|
| 786 | + spip_unlink($fichier); // le fichier peut deja exister |
|
| 787 | + @rename($tmp, $fichier); |
|
| 788 | + |
|
| 789 | + return $ret; |
|
| 790 | + } |
|
| 791 | + |
|
| 792 | + return false; |
|
| 793 | 793 | } |
| 794 | 794 | |
| 795 | 795 | /** |
@@ -807,24 +807,24 @@ discard block |
||
| 807 | 807 | * - true si une image est bien retournée. |
| 808 | 808 | */ |
| 809 | 809 | function _image_imagegif($img, $fichier) { |
| 810 | - if (!function_exists('imagegif')) { |
|
| 811 | - return false; |
|
| 812 | - } |
|
| 813 | - $tmp = $fichier . '.tmp'; |
|
| 814 | - $ret = imagegif($img, $tmp); |
|
| 815 | - if (file_exists($tmp)) { |
|
| 816 | - $taille_test = @getimagesize($tmp); |
|
| 817 | - if (empty($taille_test[0])) { |
|
| 818 | - return false; |
|
| 819 | - } |
|
| 820 | - |
|
| 821 | - spip_unlink($fichier); // le fichier peut deja exister |
|
| 822 | - @rename($tmp, $fichier); |
|
| 823 | - |
|
| 824 | - return $ret; |
|
| 825 | - } |
|
| 826 | - |
|
| 827 | - return false; |
|
| 810 | + if (!function_exists('imagegif')) { |
|
| 811 | + return false; |
|
| 812 | + } |
|
| 813 | + $tmp = $fichier . '.tmp'; |
|
| 814 | + $ret = imagegif($img, $tmp); |
|
| 815 | + if (file_exists($tmp)) { |
|
| 816 | + $taille_test = @getimagesize($tmp); |
|
| 817 | + if (empty($taille_test[0])) { |
|
| 818 | + return false; |
|
| 819 | + } |
|
| 820 | + |
|
| 821 | + spip_unlink($fichier); // le fichier peut deja exister |
|
| 822 | + @rename($tmp, $fichier); |
|
| 823 | + |
|
| 824 | + return $ret; |
|
| 825 | + } |
|
| 826 | + |
|
| 827 | + return false; |
|
| 828 | 828 | } |
| 829 | 829 | |
| 830 | 830 | /** |
@@ -847,29 +847,29 @@ discard block |
||
| 847 | 847 | * - true si une image est bien retournée. |
| 848 | 848 | */ |
| 849 | 849 | function _image_imagejpg($img, $fichier, $qualite = _IMG_GD_QUALITE) { |
| 850 | - if (!function_exists('imagejpeg')) { |
|
| 851 | - return false; |
|
| 852 | - } |
|
| 853 | - $tmp = $fichier . '.tmp'; |
|
| 850 | + if (!function_exists('imagejpeg')) { |
|
| 851 | + return false; |
|
| 852 | + } |
|
| 853 | + $tmp = $fichier . '.tmp'; |
|
| 854 | 854 | |
| 855 | - // Enable interlancing |
|
| 856 | - imageinterlace($img, true); |
|
| 855 | + // Enable interlancing |
|
| 856 | + imageinterlace($img, true); |
|
| 857 | 857 | |
| 858 | - $ret = imagejpeg($img, $tmp, $qualite); |
|
| 858 | + $ret = imagejpeg($img, $tmp, $qualite); |
|
| 859 | 859 | |
| 860 | - if (file_exists($tmp)) { |
|
| 861 | - $taille_test = @getimagesize($tmp); |
|
| 862 | - if (empty($taille_test[0])) { |
|
| 863 | - return false; |
|
| 864 | - } |
|
| 860 | + if (file_exists($tmp)) { |
|
| 861 | + $taille_test = @getimagesize($tmp); |
|
| 862 | + if (empty($taille_test[0])) { |
|
| 863 | + return false; |
|
| 864 | + } |
|
| 865 | 865 | |
| 866 | - spip_unlink($fichier); // le fichier peut deja exister |
|
| 867 | - @rename($tmp, $fichier); |
|
| 866 | + spip_unlink($fichier); // le fichier peut deja exister |
|
| 867 | + @rename($tmp, $fichier); |
|
| 868 | 868 | |
| 869 | - return $ret; |
|
| 870 | - } |
|
| 869 | + return $ret; |
|
| 870 | + } |
|
| 871 | 871 | |
| 872 | - return false; |
|
| 872 | + return false; |
|
| 873 | 873 | } |
| 874 | 874 | |
| 875 | 875 | /** |
@@ -887,9 +887,9 @@ discard block |
||
| 887 | 887 | * true si le fichier a bien été créé ; false sinon. |
| 888 | 888 | */ |
| 889 | 889 | function _image_imageico($img, $fichier) { |
| 890 | - $gd_image_array = [$img]; |
|
| 890 | + $gd_image_array = [$img]; |
|
| 891 | 891 | |
| 892 | - return ecrire_fichier($fichier, phpthumb_functions::GD2ICOstring($gd_image_array)); |
|
| 892 | + return ecrire_fichier($fichier, phpthumb_functions::GD2ICOstring($gd_image_array)); |
|
| 893 | 893 | } |
| 894 | 894 | |
| 895 | 895 | |
@@ -908,24 +908,24 @@ discard block |
||
| 908 | 908 | * - true si une image est bien retournée. |
| 909 | 909 | */ |
| 910 | 910 | function _image_imagewebp($img, $fichier, $qualite = _IMG_GD_QUALITE) { |
| 911 | - if (!function_exists('imagewebp')) { |
|
| 912 | - return false; |
|
| 913 | - } |
|
| 914 | - $tmp = $fichier . '.tmp'; |
|
| 915 | - $ret = imagewebp($img, $tmp, $qualite); |
|
| 916 | - if (file_exists($tmp)) { |
|
| 917 | - $taille_test = @getimagesize($tmp); |
|
| 918 | - if (empty($taille_test[0])) { |
|
| 919 | - return false; |
|
| 920 | - } |
|
| 921 | - |
|
| 922 | - spip_unlink($fichier); // le fichier peut deja exister |
|
| 923 | - @rename($tmp, $fichier); |
|
| 924 | - |
|
| 925 | - return $ret; |
|
| 926 | - } |
|
| 927 | - |
|
| 928 | - return false; |
|
| 911 | + if (!function_exists('imagewebp')) { |
|
| 912 | + return false; |
|
| 913 | + } |
|
| 914 | + $tmp = $fichier . '.tmp'; |
|
| 915 | + $ret = imagewebp($img, $tmp, $qualite); |
|
| 916 | + if (file_exists($tmp)) { |
|
| 917 | + $taille_test = @getimagesize($tmp); |
|
| 918 | + if (empty($taille_test[0])) { |
|
| 919 | + return false; |
|
| 920 | + } |
|
| 921 | + |
|
| 922 | + spip_unlink($fichier); // le fichier peut deja exister |
|
| 923 | + @rename($tmp, $fichier); |
|
| 924 | + |
|
| 925 | + return $ret; |
|
| 926 | + } |
|
| 927 | + |
|
| 928 | + return false; |
|
| 929 | 929 | } |
| 930 | 930 | |
| 931 | 931 | /** |
@@ -945,35 +945,35 @@ discard block |
||
| 945 | 945 | */ |
| 946 | 946 | function _image_imagesvg($img, $fichier) { |
| 947 | 947 | |
| 948 | - $tmp = $fichier . '.tmp'; |
|
| 949 | - if (!str_contains($img, '<')) { |
|
| 950 | - $img = supprimer_timestamp($img); |
|
| 951 | - if (!file_exists($img)) { |
|
| 952 | - return false; |
|
| 953 | - } |
|
| 954 | - @copy($img, $tmp); |
|
| 955 | - if (filesize($tmp) === filesize($img)) { |
|
| 956 | - spip_unlink($fichier); // le fichier peut deja exister |
|
| 957 | - @rename($tmp, $fichier); |
|
| 958 | - return true; |
|
| 959 | - } |
|
| 960 | - return false; |
|
| 961 | - } |
|
| 962 | - |
|
| 963 | - file_put_contents($tmp, $img); |
|
| 964 | - if (file_exists($tmp)) { |
|
| 965 | - $taille_test = spip_getimagesize($tmp); |
|
| 966 | - if (empty($taille_test[0])) { |
|
| 967 | - return false; |
|
| 968 | - } |
|
| 969 | - |
|
| 970 | - spip_unlink($fichier); // le fichier peut deja exister |
|
| 971 | - @rename($tmp, $fichier); |
|
| 972 | - |
|
| 973 | - return true; |
|
| 974 | - } |
|
| 975 | - |
|
| 976 | - return false; |
|
| 948 | + $tmp = $fichier . '.tmp'; |
|
| 949 | + if (!str_contains($img, '<')) { |
|
| 950 | + $img = supprimer_timestamp($img); |
|
| 951 | + if (!file_exists($img)) { |
|
| 952 | + return false; |
|
| 953 | + } |
|
| 954 | + @copy($img, $tmp); |
|
| 955 | + if (filesize($tmp) === filesize($img)) { |
|
| 956 | + spip_unlink($fichier); // le fichier peut deja exister |
|
| 957 | + @rename($tmp, $fichier); |
|
| 958 | + return true; |
|
| 959 | + } |
|
| 960 | + return false; |
|
| 961 | + } |
|
| 962 | + |
|
| 963 | + file_put_contents($tmp, $img); |
|
| 964 | + if (file_exists($tmp)) { |
|
| 965 | + $taille_test = spip_getimagesize($tmp); |
|
| 966 | + if (empty($taille_test[0])) { |
|
| 967 | + return false; |
|
| 968 | + } |
|
| 969 | + |
|
| 970 | + spip_unlink($fichier); // le fichier peut deja exister |
|
| 971 | + @rename($tmp, $fichier); |
|
| 972 | + |
|
| 973 | + return true; |
|
| 974 | + } |
|
| 975 | + |
|
| 976 | + return false; |
|
| 977 | 977 | } |
| 978 | 978 | |
| 979 | 979 | |
@@ -1001,30 +1001,30 @@ discard block |
||
| 1001 | 1001 | * - false sinon. |
| 1002 | 1002 | */ |
| 1003 | 1003 | function _image_gd_output($img, $valeurs, $qualite = _IMG_GD_QUALITE, $fonction = null) { |
| 1004 | - if (is_null($fonction)) { |
|
| 1005 | - $fonction = '_image_image' . $valeurs['format_dest']; |
|
| 1006 | - } |
|
| 1007 | - $ret = false; |
|
| 1008 | - #un flag pour reperer les images gravees |
|
| 1009 | - $lock = ( |
|
| 1010 | - !statut_effacer_images_temporaires('get') |
|
| 1011 | - || @file_exists($valeurs['fichier_dest']) |
|
| 1012 | - && !@file_exists($valeurs['fichier_dest'] . '.src') |
|
| 1013 | - ); |
|
| 1014 | - if ( |
|
| 1015 | - function_exists($fonction) |
|
| 1016 | - && ($ret = $fonction($img, $valeurs['fichier_dest'], $qualite)) # on a reussi a creer l'image |
|
| 1017 | - && isset($valeurs['reconstruction']) # et on sait comment la resonctruire le cas echeant |
|
| 1018 | - && !$lock && @file_exists($valeurs['fichier_dest']) |
|
| 1019 | - ) { |
|
| 1020 | - // dans tous les cas mettre a jour la taille de l'image finale |
|
| 1021 | - [$valeurs['hauteur_dest'], $valeurs['largeur_dest']] = taille_image($valeurs['fichier_dest']); |
|
| 1022 | - $valeurs['date'] = @filemtime($valeurs['fichier_dest']); |
|
| 1023 | - // pour la retrouver apres disparition |
|
| 1024 | - ecrire_fichier($valeurs['fichier_dest'] . '.src', serialize($valeurs), true); |
|
| 1025 | - } |
|
| 1026 | - |
|
| 1027 | - return $ret; |
|
| 1004 | + if (is_null($fonction)) { |
|
| 1005 | + $fonction = '_image_image' . $valeurs['format_dest']; |
|
| 1006 | + } |
|
| 1007 | + $ret = false; |
|
| 1008 | + #un flag pour reperer les images gravees |
|
| 1009 | + $lock = ( |
|
| 1010 | + !statut_effacer_images_temporaires('get') |
|
| 1011 | + || @file_exists($valeurs['fichier_dest']) |
|
| 1012 | + && !@file_exists($valeurs['fichier_dest'] . '.src') |
|
| 1013 | + ); |
|
| 1014 | + if ( |
|
| 1015 | + function_exists($fonction) |
|
| 1016 | + && ($ret = $fonction($img, $valeurs['fichier_dest'], $qualite)) # on a reussi a creer l'image |
|
| 1017 | + && isset($valeurs['reconstruction']) # et on sait comment la resonctruire le cas echeant |
|
| 1018 | + && !$lock && @file_exists($valeurs['fichier_dest']) |
|
| 1019 | + ) { |
|
| 1020 | + // dans tous les cas mettre a jour la taille de l'image finale |
|
| 1021 | + [$valeurs['hauteur_dest'], $valeurs['largeur_dest']] = taille_image($valeurs['fichier_dest']); |
|
| 1022 | + $valeurs['date'] = @filemtime($valeurs['fichier_dest']); |
|
| 1023 | + // pour la retrouver apres disparition |
|
| 1024 | + ecrire_fichier($valeurs['fichier_dest'] . '.src', serialize($valeurs), true); |
|
| 1025 | + } |
|
| 1026 | + |
|
| 1027 | + return $ret; |
|
| 1028 | 1028 | } |
| 1029 | 1029 | |
| 1030 | 1030 | /** |
@@ -1037,28 +1037,28 @@ discard block |
||
| 1037 | 1037 | * Chemin vers le fichier manquant |
| 1038 | 1038 | **/ |
| 1039 | 1039 | function reconstruire_image_intermediaire($fichier_manquant) { |
| 1040 | - $source = null; |
|
| 1041 | - $reconstruire = []; |
|
| 1042 | - $fichier = $fichier_manquant; |
|
| 1043 | - while ( |
|
| 1044 | - !str_contains((string) $fichier, '://') |
|
| 1045 | - && !@file_exists($fichier) |
|
| 1046 | - && lire_fichier($src = "$fichier.src", $source) |
|
| 1047 | - && ($valeurs = unserialize($source)) |
|
| 1048 | - && ($fichier = $valeurs['fichier']) # l'origine est connue (on ne verifie pas son existence, qu'importe ...) |
|
| 1049 | - ) { |
|
| 1050 | - spip_unlink($src); // si jamais on a un timeout pendant la reconstruction, elle se fera naturellement au hit suivant |
|
| 1051 | - $reconstruire[] = $valeurs['reconstruction']; |
|
| 1052 | - } |
|
| 1053 | - while (count($reconstruire)) { |
|
| 1054 | - $r = array_pop($reconstruire); |
|
| 1055 | - $fonction = $r[0]; |
|
| 1056 | - $args = $r[1]; |
|
| 1057 | - $fonction(...$args); |
|
| 1058 | - } |
|
| 1059 | - // cette image intermediaire est commune a plusieurs series de filtre, il faut la conserver |
|
| 1060 | - // mais l'on peut nettoyer les miettes de sa creation |
|
| 1061 | - ramasse_miettes($fichier_manquant); |
|
| 1040 | + $source = null; |
|
| 1041 | + $reconstruire = []; |
|
| 1042 | + $fichier = $fichier_manquant; |
|
| 1043 | + while ( |
|
| 1044 | + !str_contains((string) $fichier, '://') |
|
| 1045 | + && !@file_exists($fichier) |
|
| 1046 | + && lire_fichier($src = "$fichier.src", $source) |
|
| 1047 | + && ($valeurs = unserialize($source)) |
|
| 1048 | + && ($fichier = $valeurs['fichier']) # l'origine est connue (on ne verifie pas son existence, qu'importe ...) |
|
| 1049 | + ) { |
|
| 1050 | + spip_unlink($src); // si jamais on a un timeout pendant la reconstruction, elle se fera naturellement au hit suivant |
|
| 1051 | + $reconstruire[] = $valeurs['reconstruction']; |
|
| 1052 | + } |
|
| 1053 | + while (count($reconstruire)) { |
|
| 1054 | + $r = array_pop($reconstruire); |
|
| 1055 | + $fonction = $r[0]; |
|
| 1056 | + $args = $r[1]; |
|
| 1057 | + $fonction(...$args); |
|
| 1058 | + } |
|
| 1059 | + // cette image intermediaire est commune a plusieurs series de filtre, il faut la conserver |
|
| 1060 | + // mais l'on peut nettoyer les miettes de sa creation |
|
| 1061 | + ramasse_miettes($fichier_manquant); |
|
| 1062 | 1062 | } |
| 1063 | 1063 | |
| 1064 | 1064 | /** |
@@ -1078,26 +1078,26 @@ discard block |
||
| 1078 | 1078 | * Chemin du fichier d'image calculé |
| 1079 | 1079 | **/ |
| 1080 | 1080 | function ramasse_miettes($fichier) { |
| 1081 | - $source = null; |
|
| 1082 | - if ( |
|
| 1083 | - str_contains($fichier, '://') |
|
| 1084 | - || !lire_fichier($src = "$fichier.src", $source) |
|
| 1085 | - || !$valeurs = unserialize($source) |
|
| 1086 | - ) { |
|
| 1087 | - return; |
|
| 1088 | - } |
|
| 1089 | - spip_unlink($src); # on supprime la reference a sa source pour marquer cette image comme non intermediaire |
|
| 1090 | - while ( |
|
| 1091 | - ($fichier = $valeurs['fichier']) |
|
| 1092 | - && str_starts_with((string) $fichier, (string) _DIR_VAR) |
|
| 1093 | - && lire_fichier($src = "$fichier.src", $source) |
|
| 1094 | - && ($valeurs = unserialize($source)) # et valide |
|
| 1095 | - ) { |
|
| 1096 | - # on efface le fichier |
|
| 1097 | - spip_unlink($fichier); |
|
| 1098 | - # mais laisse le .src qui permet de savoir comment reconstruire l'image si besoin |
|
| 1099 | - #spip_unlink($src); |
|
| 1100 | - } |
|
| 1081 | + $source = null; |
|
| 1082 | + if ( |
|
| 1083 | + str_contains($fichier, '://') |
|
| 1084 | + || !lire_fichier($src = "$fichier.src", $source) |
|
| 1085 | + || !$valeurs = unserialize($source) |
|
| 1086 | + ) { |
|
| 1087 | + return; |
|
| 1088 | + } |
|
| 1089 | + spip_unlink($src); # on supprime la reference a sa source pour marquer cette image comme non intermediaire |
|
| 1090 | + while ( |
|
| 1091 | + ($fichier = $valeurs['fichier']) |
|
| 1092 | + && str_starts_with((string) $fichier, (string) _DIR_VAR) |
|
| 1093 | + && lire_fichier($src = "$fichier.src", $source) |
|
| 1094 | + && ($valeurs = unserialize($source)) # et valide |
|
| 1095 | + ) { |
|
| 1096 | + # on efface le fichier |
|
| 1097 | + spip_unlink($fichier); |
|
| 1098 | + # mais laisse le .src qui permet de savoir comment reconstruire l'image si besoin |
|
| 1099 | + #spip_unlink($src); |
|
| 1100 | + } |
|
| 1101 | 1101 | } |
| 1102 | 1102 | |
| 1103 | 1103 | |
@@ -1122,33 +1122,33 @@ discard block |
||
| 1122 | 1122 | * Code HTML de l'image |
| 1123 | 1123 | **/ |
| 1124 | 1124 | function image_graver($img) { |
| 1125 | - // appeler le filtre post_image_filtrer qui permet de faire |
|
| 1126 | - // des traitements auto a la fin d'une serie de filtres |
|
| 1127 | - $img = pipeline('post_image_filtrer', $img); |
|
| 1128 | - |
|
| 1129 | - $fichier_ori = $fichier = (extraire_attribut($img, 'src') ?? ''); |
|
| 1130 | - if (($p = strpos($fichier, '?')) !== false) { |
|
| 1131 | - $fichier = substr($fichier, 0, $p); |
|
| 1132 | - } |
|
| 1133 | - if (strlen($fichier) < 1 && !str_starts_with((string) $img, '<img ')) { |
|
| 1134 | - $fichier = $img; |
|
| 1135 | - } |
|
| 1136 | - if (strlen((string) $fichier)) { |
|
| 1137 | - # si jamais le fichier final n'a pas ete calcule car suppose temporaire |
|
| 1138 | - # et qu'il ne s'agit pas d'une URL |
|
| 1139 | - if (!str_contains((string) $fichier, '://') && !@file_exists($fichier)) { |
|
| 1140 | - reconstruire_image_intermediaire($fichier); |
|
| 1141 | - } |
|
| 1142 | - ramasse_miettes($fichier); |
|
| 1143 | - |
|
| 1144 | - // ajouter le timestamp si besoin |
|
| 1145 | - if (!str_contains($fichier_ori, '?')) { |
|
| 1146 | - // on utilise str_replace pour attraper le onmouseover des logo si besoin |
|
| 1147 | - $img = str_replace($fichier_ori, timestamp($fichier_ori), (string) $img); |
|
| 1148 | - } |
|
| 1149 | - } |
|
| 1150 | - |
|
| 1151 | - return $img; |
|
| 1125 | + // appeler le filtre post_image_filtrer qui permet de faire |
|
| 1126 | + // des traitements auto a la fin d'une serie de filtres |
|
| 1127 | + $img = pipeline('post_image_filtrer', $img); |
|
| 1128 | + |
|
| 1129 | + $fichier_ori = $fichier = (extraire_attribut($img, 'src') ?? ''); |
|
| 1130 | + if (($p = strpos($fichier, '?')) !== false) { |
|
| 1131 | + $fichier = substr($fichier, 0, $p); |
|
| 1132 | + } |
|
| 1133 | + if (strlen($fichier) < 1 && !str_starts_with((string) $img, '<img ')) { |
|
| 1134 | + $fichier = $img; |
|
| 1135 | + } |
|
| 1136 | + if (strlen((string) $fichier)) { |
|
| 1137 | + # si jamais le fichier final n'a pas ete calcule car suppose temporaire |
|
| 1138 | + # et qu'il ne s'agit pas d'une URL |
|
| 1139 | + if (!str_contains((string) $fichier, '://') && !@file_exists($fichier)) { |
|
| 1140 | + reconstruire_image_intermediaire($fichier); |
|
| 1141 | + } |
|
| 1142 | + ramasse_miettes($fichier); |
|
| 1143 | + |
|
| 1144 | + // ajouter le timestamp si besoin |
|
| 1145 | + if (!str_contains($fichier_ori, '?')) { |
|
| 1146 | + // on utilise str_replace pour attraper le onmouseover des logo si besoin |
|
| 1147 | + $img = str_replace($fichier_ori, timestamp($fichier_ori), (string) $img); |
|
| 1148 | + } |
|
| 1149 | + } |
|
| 1150 | + |
|
| 1151 | + return $img; |
|
| 1152 | 1152 | } |
| 1153 | 1153 | |
| 1154 | 1154 | /** |
@@ -1175,32 +1175,32 @@ discard block |
||
| 1175 | 1175 | * Code html modifié de la balise. |
| 1176 | 1176 | **/ |
| 1177 | 1177 | function _image_tag_changer_taille($tag, $width, $height, $style = false) { |
| 1178 | - if ($style === false) { |
|
| 1179 | - $style = extraire_attribut($tag, 'style'); |
|
| 1180 | - } |
|
| 1181 | - |
|
| 1182 | - // enlever le width et height du style |
|
| 1183 | - if ($style) { |
|
| 1184 | - $style = preg_replace(',(^|;)\s*(width|height)\s*:\s*[^;]+,ims', '', $style); |
|
| 1185 | - } |
|
| 1186 | - if ($style && $style[0] === ';') { |
|
| 1187 | - $style = substr($style, 1); |
|
| 1188 | - } |
|
| 1189 | - |
|
| 1190 | - // mettre des attributs de width et height sur les images, |
|
| 1191 | - // ca accelere le rendu du navigateur |
|
| 1192 | - // ca permet aux navigateurs de reserver la bonne taille |
|
| 1193 | - // quand on a desactive l'affichage des images. |
|
| 1194 | - $tag = inserer_attribut($tag, 'width', round($width)); |
|
| 1195 | - $tag = inserer_attribut($tag, 'height', round($height)); |
|
| 1196 | - |
|
| 1197 | - // attributs deprecies. Transformer en CSS |
|
| 1198 | - if ($espace = extraire_attribut($tag, 'hspace')) { |
|
| 1199 | - $style = "margin:{$espace}px;" . $style; |
|
| 1200 | - $tag = inserer_attribut($tag, 'hspace', ''); |
|
| 1201 | - } |
|
| 1202 | - |
|
| 1203 | - return inserer_attribut($tag, 'style', (string) $style, true, !(bool) $style); |
|
| 1178 | + if ($style === false) { |
|
| 1179 | + $style = extraire_attribut($tag, 'style'); |
|
| 1180 | + } |
|
| 1181 | + |
|
| 1182 | + // enlever le width et height du style |
|
| 1183 | + if ($style) { |
|
| 1184 | + $style = preg_replace(',(^|;)\s*(width|height)\s*:\s*[^;]+,ims', '', $style); |
|
| 1185 | + } |
|
| 1186 | + if ($style && $style[0] === ';') { |
|
| 1187 | + $style = substr($style, 1); |
|
| 1188 | + } |
|
| 1189 | + |
|
| 1190 | + // mettre des attributs de width et height sur les images, |
|
| 1191 | + // ca accelere le rendu du navigateur |
|
| 1192 | + // ca permet aux navigateurs de reserver la bonne taille |
|
| 1193 | + // quand on a desactive l'affichage des images. |
|
| 1194 | + $tag = inserer_attribut($tag, 'width', round($width)); |
|
| 1195 | + $tag = inserer_attribut($tag, 'height', round($height)); |
|
| 1196 | + |
|
| 1197 | + // attributs deprecies. Transformer en CSS |
|
| 1198 | + if ($espace = extraire_attribut($tag, 'hspace')) { |
|
| 1199 | + $style = "margin:{$espace}px;" . $style; |
|
| 1200 | + $tag = inserer_attribut($tag, 'hspace', ''); |
|
| 1201 | + } |
|
| 1202 | + |
|
| 1203 | + return inserer_attribut($tag, 'style', (string) $style, true, !(bool) $style); |
|
| 1204 | 1204 | } |
| 1205 | 1205 | |
| 1206 | 1206 | |
@@ -1226,70 +1226,70 @@ discard block |
||
| 1226 | 1226 | * Retourne le code HTML de l'image |
| 1227 | 1227 | **/ |
| 1228 | 1228 | function _image_ecrire_tag($valeurs, $surcharge = []) { |
| 1229 | - $valeurs = pipeline('image_ecrire_tag_preparer', $valeurs); |
|
| 1230 | - |
|
| 1231 | - // fermer les tags img pas bien fermes; |
|
| 1232 | - $tag = str_replace('>', '/>', str_replace('/>', '>', (string) $valeurs['tag'])); |
|
| 1233 | - |
|
| 1234 | - // le style |
|
| 1235 | - $style = $valeurs['style']; |
|
| 1236 | - if (isset($surcharge['style'])) { |
|
| 1237 | - $style = $surcharge['style']; |
|
| 1238 | - unset($surcharge['style']); |
|
| 1239 | - } |
|
| 1240 | - |
|
| 1241 | - // traiter specifiquement la largeur et la hauteur |
|
| 1242 | - $width = $valeurs['largeur']; |
|
| 1243 | - if (isset($surcharge['width'])) { |
|
| 1244 | - $width = $surcharge['width']; |
|
| 1245 | - unset($surcharge['width']); |
|
| 1246 | - } |
|
| 1247 | - $height = $valeurs['hauteur']; |
|
| 1248 | - if (isset($surcharge['height'])) { |
|
| 1249 | - $height = $surcharge['height']; |
|
| 1250 | - unset($surcharge['height']); |
|
| 1251 | - } |
|
| 1252 | - |
|
| 1253 | - $tag = _image_tag_changer_taille($tag, $width, $height, $style); |
|
| 1254 | - // traiter specifiquement le src qui peut etre repris dans un onmouseout |
|
| 1255 | - // on remplace toute les ref a src dans le tag |
|
| 1256 | - $src = extraire_attribut($tag, 'src'); |
|
| 1257 | - if (isset($surcharge['src'])) { |
|
| 1258 | - $tag = str_replace($src, $surcharge['src'], $tag); |
|
| 1259 | - // si il y a des & dans src, alors ils peuvent provenir d'un & |
|
| 1260 | - // pas garanti comme methode, mais mieux que rien |
|
| 1261 | - if (str_contains($src, '&')) { |
|
| 1262 | - $tag = str_replace(str_replace('&', '&', $src), $surcharge['src'], $tag); |
|
| 1263 | - } |
|
| 1264 | - $src = $surcharge['src']; |
|
| 1265 | - unset($surcharge['src']); |
|
| 1266 | - } |
|
| 1267 | - |
|
| 1268 | - $class = $valeurs['class']; |
|
| 1269 | - if (isset($surcharge['class'])) { |
|
| 1270 | - $class = $surcharge['class']; |
|
| 1271 | - unset($surcharge['class']); |
|
| 1272 | - } |
|
| 1273 | - if (is_scalar($class) && strlen($class)) { |
|
| 1274 | - $tag = inserer_attribut($tag, 'class', $class); |
|
| 1275 | - } |
|
| 1276 | - |
|
| 1277 | - if ($surcharge !== []) { |
|
| 1278 | - foreach ($surcharge as $attribut => $valeur) { |
|
| 1279 | - $tag = inserer_attribut($tag, $attribut, $valeur); |
|
| 1280 | - } |
|
| 1281 | - } |
|
| 1282 | - |
|
| 1283 | - return pipeline( |
|
| 1284 | - 'image_ecrire_tag_finir', |
|
| 1285 | - [ |
|
| 1286 | - 'args' => [ |
|
| 1287 | - 'valeurs' => $valeurs, |
|
| 1288 | - 'surcharge' => $surcharge, |
|
| 1289 | - ], |
|
| 1290 | - 'data' => $tag |
|
| 1291 | - ] |
|
| 1292 | - ); |
|
| 1229 | + $valeurs = pipeline('image_ecrire_tag_preparer', $valeurs); |
|
| 1230 | + |
|
| 1231 | + // fermer les tags img pas bien fermes; |
|
| 1232 | + $tag = str_replace('>', '/>', str_replace('/>', '>', (string) $valeurs['tag'])); |
|
| 1233 | + |
|
| 1234 | + // le style |
|
| 1235 | + $style = $valeurs['style']; |
|
| 1236 | + if (isset($surcharge['style'])) { |
|
| 1237 | + $style = $surcharge['style']; |
|
| 1238 | + unset($surcharge['style']); |
|
| 1239 | + } |
|
| 1240 | + |
|
| 1241 | + // traiter specifiquement la largeur et la hauteur |
|
| 1242 | + $width = $valeurs['largeur']; |
|
| 1243 | + if (isset($surcharge['width'])) { |
|
| 1244 | + $width = $surcharge['width']; |
|
| 1245 | + unset($surcharge['width']); |
|
| 1246 | + } |
|
| 1247 | + $height = $valeurs['hauteur']; |
|
| 1248 | + if (isset($surcharge['height'])) { |
|
| 1249 | + $height = $surcharge['height']; |
|
| 1250 | + unset($surcharge['height']); |
|
| 1251 | + } |
|
| 1252 | + |
|
| 1253 | + $tag = _image_tag_changer_taille($tag, $width, $height, $style); |
|
| 1254 | + // traiter specifiquement le src qui peut etre repris dans un onmouseout |
|
| 1255 | + // on remplace toute les ref a src dans le tag |
|
| 1256 | + $src = extraire_attribut($tag, 'src'); |
|
| 1257 | + if (isset($surcharge['src'])) { |
|
| 1258 | + $tag = str_replace($src, $surcharge['src'], $tag); |
|
| 1259 | + // si il y a des & dans src, alors ils peuvent provenir d'un & |
|
| 1260 | + // pas garanti comme methode, mais mieux que rien |
|
| 1261 | + if (str_contains($src, '&')) { |
|
| 1262 | + $tag = str_replace(str_replace('&', '&', $src), $surcharge['src'], $tag); |
|
| 1263 | + } |
|
| 1264 | + $src = $surcharge['src']; |
|
| 1265 | + unset($surcharge['src']); |
|
| 1266 | + } |
|
| 1267 | + |
|
| 1268 | + $class = $valeurs['class']; |
|
| 1269 | + if (isset($surcharge['class'])) { |
|
| 1270 | + $class = $surcharge['class']; |
|
| 1271 | + unset($surcharge['class']); |
|
| 1272 | + } |
|
| 1273 | + if (is_scalar($class) && strlen($class)) { |
|
| 1274 | + $tag = inserer_attribut($tag, 'class', $class); |
|
| 1275 | + } |
|
| 1276 | + |
|
| 1277 | + if ($surcharge !== []) { |
|
| 1278 | + foreach ($surcharge as $attribut => $valeur) { |
|
| 1279 | + $tag = inserer_attribut($tag, $attribut, $valeur); |
|
| 1280 | + } |
|
| 1281 | + } |
|
| 1282 | + |
|
| 1283 | + return pipeline( |
|
| 1284 | + 'image_ecrire_tag_finir', |
|
| 1285 | + [ |
|
| 1286 | + 'args' => [ |
|
| 1287 | + 'valeurs' => $valeurs, |
|
| 1288 | + 'surcharge' => $surcharge, |
|
| 1289 | + ], |
|
| 1290 | + 'data' => $tag |
|
| 1291 | + ] |
|
| 1292 | + ); |
|
| 1293 | 1293 | } |
| 1294 | 1294 | |
| 1295 | 1295 | /** |
@@ -1312,268 +1312,268 @@ discard block |
||
| 1312 | 1312 | * Description de l'image, sinon null. |
| 1313 | 1313 | **/ |
| 1314 | 1314 | function _image_creer_vignette($valeurs, $maxWidth, $maxHeight, $process = 'AUTO', $force = false) { |
| 1315 | - $srcHeight = null; |
|
| 1316 | - $retour = []; |
|
| 1317 | - // ordre de preference des formats graphiques pour creer les vignettes |
|
| 1318 | - // le premier format disponible, selon la methode demandee, est utilise |
|
| 1319 | - $image = $valeurs['fichier']; |
|
| 1320 | - $format = $valeurs['format_source']; |
|
| 1321 | - $destdir = dirname((string) $valeurs['fichier_dest']); |
|
| 1322 | - $destfile = basename((string) $valeurs['fichier_dest'], '.' . $valeurs['format_dest']); |
|
| 1323 | - |
|
| 1324 | - $format_sortie = $valeurs['format_dest']; |
|
| 1325 | - |
|
| 1326 | - if ($process == 'AUTO' && isset($GLOBALS['meta']['image_process'])) { |
|
| 1327 | - $process = $GLOBALS['meta']['image_process']; |
|
| 1328 | - } |
|
| 1329 | - |
|
| 1330 | - // si le doc n'est pas une image dans un format accetpable, refuser |
|
| 1331 | - if (!$force && !in_array($format, formats_image_acceptables($process === 'gd2'))) { |
|
| 1332 | - return; |
|
| 1333 | - } |
|
| 1334 | - $destination = "$destdir/$destfile"; |
|
| 1335 | - |
|
| 1336 | - // calculer la taille |
|
| 1337 | - if (($srcWidth = $valeurs['largeur']) && ($srcHeight = $valeurs['hauteur'])) { |
|
| 1338 | - if (!($destWidth = $valeurs['largeur_dest']) || !($destHeight = $valeurs['hauteur_dest'])) { |
|
| 1339 | - [$destWidth, $destHeight] = _image_ratio($srcWidth, $srcHeight, $maxWidth, $maxHeight); |
|
| 1340 | - } |
|
| 1341 | - } elseif ($process == 'convert' || $process == 'imagick') { |
|
| 1342 | - $destWidth = $maxWidth; |
|
| 1343 | - $destHeight = $maxHeight; |
|
| 1344 | - } else { |
|
| 1345 | - spip_logger('images')->info("echec $process sur $image"); |
|
| 1346 | - |
|
| 1347 | - return; |
|
| 1348 | - } |
|
| 1349 | - |
|
| 1350 | - $vignette = ''; |
|
| 1351 | - |
|
| 1352 | - // Si l'image est de la taille demandee (ou plus petite), simplement la retourner |
|
| 1353 | - if ($srcWidth && $srcWidth <= $maxWidth && $srcHeight <= $maxHeight) { |
|
| 1354 | - $vignette = $destination . '.' . $format; |
|
| 1355 | - @copy($image, $vignette); |
|
| 1356 | - } |
|
| 1357 | - |
|
| 1358 | - elseif ($valeurs['format_source'] === 'svg') { |
|
| 1359 | - include_spip('inc/svg'); |
|
| 1360 | - if ($svg = svg_redimensionner($valeurs['fichier'], $destWidth, $destHeight)) { |
|
| 1361 | - $format_sortie = 'svg'; |
|
| 1362 | - $vignette = $destination . '.' . $format_sortie; |
|
| 1363 | - $valeurs['fichier_dest'] = $vignette; |
|
| 1364 | - _image_gd_output($svg, $valeurs); |
|
| 1365 | - } |
|
| 1366 | - } |
|
| 1367 | - |
|
| 1368 | - // imagemagick en ligne de commande |
|
| 1369 | - elseif ($process == 'convert') { |
|
| 1370 | - if (!defined('_CONVERT_COMMAND')) { |
|
| 1371 | - define('_CONVERT_COMMAND', 'convert'); |
|
| 1372 | - } // Securite : mes_options.php peut preciser le chemin absolu |
|
| 1373 | - if (!defined('_RESIZE_COMMAND')) { |
|
| 1374 | - define('_RESIZE_COMMAND', _CONVERT_COMMAND . ' -quality ' . _IMG_CONVERT_QUALITE . ' -orient Undefined -resize %xx%y! %src %dest'); |
|
| 1375 | - } |
|
| 1376 | - $vignette = $destination . '.' . $format_sortie; |
|
| 1377 | - $commande = str_replace( |
|
| 1378 | - ['%x', '%y', '%src', '%dest'], |
|
| 1379 | - [ |
|
| 1380 | - $destWidth, |
|
| 1381 | - $destHeight, |
|
| 1382 | - escapeshellcmd($image), |
|
| 1383 | - escapeshellcmd($vignette) |
|
| 1384 | - ], |
|
| 1385 | - (string) _RESIZE_COMMAND |
|
| 1386 | - ); |
|
| 1387 | - spip_logger('images')->info($commande); |
|
| 1388 | - exec($commande); |
|
| 1389 | - if (!@file_exists($vignette)) { |
|
| 1390 | - spip_logger('images')->info("echec convert sur $vignette"); |
|
| 1391 | - |
|
| 1392 | - return; // echec commande |
|
| 1393 | - } |
|
| 1394 | - } |
|
| 1395 | - |
|
| 1396 | - // php5 imagemagick |
|
| 1397 | - elseif ($process == 'imagick') { |
|
| 1398 | - if (!class_exists(\Imagick::class)) { |
|
| 1399 | - spip_logger('images')->error('Classe Imagick absente !'); |
|
| 1400 | - |
|
| 1401 | - return; |
|
| 1402 | - } |
|
| 1403 | - |
|
| 1404 | - // chemin compatible Windows |
|
| 1405 | - $output = realpath(dirname($destination)); |
|
| 1406 | - if (!$output) { |
|
| 1407 | - return; |
|
| 1408 | - } |
|
| 1409 | - $vignette = $output . DIRECTORY_SEPARATOR . basename($destination) . '.' . $format_sortie; |
|
| 1410 | - |
|
| 1411 | - $imagick = new Imagick(); |
|
| 1412 | - $imagick->readImage(realpath($image)); |
|
| 1413 | - $imagick->resizeImage( |
|
| 1414 | - $destWidth, |
|
| 1415 | - $destHeight, |
|
| 1416 | - Imagick::FILTER_LANCZOS, |
|
| 1417 | - 1 |
|
| 1418 | - );//, IMAGICK_FILTER_LANCZOS, _IMG_IMAGICK_QUALITE / 100); |
|
| 1419 | - $imagick->writeImage($vignette); |
|
| 1420 | - |
|
| 1421 | - if (!@file_exists($vignette)) { |
|
| 1422 | - spip_logger('images')->info("echec imagick sur $vignette"); |
|
| 1423 | - |
|
| 1424 | - return; |
|
| 1425 | - } |
|
| 1426 | - // remettre le chemin relatif car c'est ce qu'attend SPIP pour la suite (en particlier action/tester) |
|
| 1427 | - $vignette = $destination . '.' . $format_sortie; |
|
| 1428 | - } |
|
| 1429 | - |
|
| 1430 | - // netpbm |
|
| 1431 | - elseif ($process == 'netpbm') { |
|
| 1432 | - if (!defined('_PNMSCALE_COMMAND')) { |
|
| 1433 | - define('_PNMSCALE_COMMAND', 'pnmscale'); |
|
| 1434 | - } // Securite : mes_options.php peut preciser le chemin absolu |
|
| 1435 | - if (_PNMSCALE_COMMAND == '') { |
|
| 1436 | - return; |
|
| 1437 | - } |
|
| 1438 | - $vignette = $destination . '.' . $format_sortie; |
|
| 1439 | - $pnmtojpeg_command = str_replace('pnmscale', 'pnmtojpeg', (string) _PNMSCALE_COMMAND); |
|
| 1440 | - if ($format == 'jpg') { |
|
| 1441 | - $jpegtopnm_command = str_replace('pnmscale', 'jpegtopnm', (string) _PNMSCALE_COMMAND); |
|
| 1442 | - exec("$jpegtopnm_command $image | " . _PNMSCALE_COMMAND . " -width $destWidth | $pnmtojpeg_command > $vignette"); |
|
| 1443 | - if (!($s = @filesize($vignette))) { |
|
| 1444 | - spip_unlink($vignette); |
|
| 1445 | - } |
|
| 1446 | - if (!@file_exists($vignette)) { |
|
| 1447 | - spip_logger('images')->info("echec netpbm-jpg sur $vignette"); |
|
| 1448 | - |
|
| 1449 | - return; |
|
| 1450 | - } |
|
| 1451 | - } else { |
|
| 1452 | - if ($format == 'gif') { |
|
| 1453 | - $giftopnm_command = str_replace('pnmscale', 'giftopnm', (string) _PNMSCALE_COMMAND); |
|
| 1454 | - exec("$giftopnm_command $image | " . _PNMSCALE_COMMAND . " -width $destWidth | $pnmtojpeg_command > $vignette"); |
|
| 1455 | - if (!($s = @filesize($vignette))) { |
|
| 1456 | - spip_unlink($vignette); |
|
| 1457 | - } |
|
| 1458 | - if (!@file_exists($vignette)) { |
|
| 1459 | - spip_logger('images')->info("echec netpbm-gif sur $vignette"); |
|
| 1460 | - |
|
| 1461 | - return; |
|
| 1462 | - } |
|
| 1463 | - } else { |
|
| 1464 | - if ($format == 'png') { |
|
| 1465 | - $pngtopnm_command = str_replace('pnmscale', 'pngtopnm', (string) _PNMSCALE_COMMAND); |
|
| 1466 | - exec("$pngtopnm_command $image | " . _PNMSCALE_COMMAND . " -width $destWidth | $pnmtojpeg_command > $vignette"); |
|
| 1467 | - if (!($s = @filesize($vignette))) { |
|
| 1468 | - spip_unlink($vignette); |
|
| 1469 | - } |
|
| 1470 | - if (!@file_exists($vignette)) { |
|
| 1471 | - spip_logger('images')->info("echec netpbm-png sur $vignette"); |
|
| 1472 | - |
|
| 1473 | - return; |
|
| 1474 | - } |
|
| 1475 | - } |
|
| 1476 | - } |
|
| 1477 | - } |
|
| 1478 | - } |
|
| 1479 | - |
|
| 1480 | - // gd ou gd2 |
|
| 1481 | - elseif ($process === 'gd2') { |
|
| 1482 | - if (!function_exists('gd_info')) { |
|
| 1483 | - spip_logger('images')->error('Librairie GD absente !'); |
|
| 1484 | - |
|
| 1485 | - return; |
|
| 1486 | - } |
|
| 1487 | - if (_IMG_GD_MAX_PIXELS && $srcWidth * $srcHeight > _IMG_GD_MAX_PIXELS) { |
|
| 1488 | - spip_logger('images')->info('vignette gd2 impossible : ' . $srcWidth * $srcHeight . 'pixels'); |
|
| 1489 | - |
|
| 1490 | - return; |
|
| 1491 | - } |
|
| 1492 | - $destFormat = $format_sortie; |
|
| 1493 | - if (!$destFormat) { |
|
| 1494 | - spip_logger('images')->info("pas de format pour $image"); |
|
| 1495 | - |
|
| 1496 | - return; |
|
| 1497 | - } |
|
| 1498 | - |
|
| 1499 | - $fonction_imagecreatefrom = $valeurs['fonction_imagecreatefrom']; |
|
| 1500 | - if (!function_exists($fonction_imagecreatefrom)) { |
|
| 1501 | - return; |
|
| 1502 | - } |
|
| 1503 | - $srcImage = @$fonction_imagecreatefrom($image); |
|
| 1504 | - if (!$srcImage) { |
|
| 1505 | - spip_logger('images')->info('echec gd2'); |
|
| 1506 | - |
|
| 1507 | - return; |
|
| 1508 | - } |
|
| 1509 | - |
|
| 1510 | - // Initialisation de l'image destination |
|
| 1511 | - $destImage = null; |
|
| 1512 | - if ($process == 'gd2' && $destFormat != 'gif') { |
|
| 1513 | - $destImage = ImageCreateTrueColor($destWidth, $destHeight); |
|
| 1514 | - } |
|
| 1515 | - if (!$destImage) { |
|
| 1516 | - $destImage = ImageCreate($destWidth, $destHeight); |
|
| 1517 | - } |
|
| 1518 | - |
|
| 1519 | - // Recopie de l'image d'origine avec adaptation de la taille |
|
| 1520 | - $ok = false; |
|
| 1521 | - if ($process == 'gd2' && function_exists('ImageCopyResampled')) { |
|
| 1522 | - if ($format == 'gif') { |
|
| 1523 | - // Si un GIF est transparent, |
|
| 1524 | - // fabriquer un PNG transparent |
|
| 1525 | - $transp = imagecolortransparent($srcImage); |
|
| 1526 | - if ($transp > 0) { |
|
| 1527 | - $destFormat = 'png'; |
|
| 1528 | - } |
|
| 1529 | - } |
|
| 1530 | - if (in_array($destFormat, _image_extensions_conservent_transparence())) { |
|
| 1531 | - // Conserver la transparence |
|
| 1532 | - if (function_exists('imageAntiAlias')) { |
|
| 1533 | - imageAntiAlias($destImage, true); |
|
| 1534 | - } |
|
| 1535 | - @imagealphablending($destImage, false); |
|
| 1536 | - @imagesavealpha($destImage, true); |
|
| 1537 | - } |
|
| 1538 | - $ok = @ImageCopyResampled($destImage, $srcImage, 0, 0, 0, 0, $destWidth, $destHeight, $srcWidth, $srcHeight); |
|
| 1539 | - } |
|
| 1540 | - if (!$ok) { |
|
| 1541 | - $ok = ImageCopyResized($destImage, $srcImage, 0, 0, 0, 0, $destWidth, $destHeight, $srcWidth, $srcHeight); |
|
| 1542 | - } |
|
| 1543 | - |
|
| 1544 | - // Sauvegarde de l'image destination |
|
| 1545 | - $valeurs['fichier_dest'] = $vignette = "$destination.$destFormat"; |
|
| 1546 | - $valeurs['format_dest'] = $format = $destFormat; |
|
| 1547 | - _image_gd_output($destImage, $valeurs); |
|
| 1548 | - |
|
| 1549 | - if ($srcImage) { |
|
| 1550 | - ImageDestroy($srcImage); |
|
| 1551 | - } |
|
| 1552 | - ImageDestroy($destImage); |
|
| 1553 | - } |
|
| 1554 | - |
|
| 1555 | - if (!$vignette || !$size = @spip_getimagesize($vignette)) { |
|
| 1556 | - $size = [$destWidth, $destHeight]; |
|
| 1557 | - } |
|
| 1558 | - |
|
| 1559 | - // Gaffe: en safe mode, pas d'acces a la vignette, |
|
| 1560 | - // donc risque de balancer "width='0'", ce qui masque l'image sous MSIE |
|
| 1561 | - if ($size[0] < 1) { |
|
| 1562 | - $size[0] = $destWidth; |
|
| 1563 | - } |
|
| 1564 | - if ($size[1] < 1) { |
|
| 1565 | - $size[1] = $destHeight; |
|
| 1566 | - } |
|
| 1567 | - |
|
| 1568 | - $retour['width'] = $largeur = $size[0]; |
|
| 1569 | - $retour['height'] = $hauteur = $size[1]; |
|
| 1570 | - |
|
| 1571 | - $retour['fichier'] = $vignette; |
|
| 1572 | - $retour['format'] = $format; |
|
| 1573 | - $retour['date'] = @filemtime($vignette); |
|
| 1574 | - |
|
| 1575 | - // renvoyer l'image |
|
| 1576 | - return $retour; |
|
| 1315 | + $srcHeight = null; |
|
| 1316 | + $retour = []; |
|
| 1317 | + // ordre de preference des formats graphiques pour creer les vignettes |
|
| 1318 | + // le premier format disponible, selon la methode demandee, est utilise |
|
| 1319 | + $image = $valeurs['fichier']; |
|
| 1320 | + $format = $valeurs['format_source']; |
|
| 1321 | + $destdir = dirname((string) $valeurs['fichier_dest']); |
|
| 1322 | + $destfile = basename((string) $valeurs['fichier_dest'], '.' . $valeurs['format_dest']); |
|
| 1323 | + |
|
| 1324 | + $format_sortie = $valeurs['format_dest']; |
|
| 1325 | + |
|
| 1326 | + if ($process == 'AUTO' && isset($GLOBALS['meta']['image_process'])) { |
|
| 1327 | + $process = $GLOBALS['meta']['image_process']; |
|
| 1328 | + } |
|
| 1329 | + |
|
| 1330 | + // si le doc n'est pas une image dans un format accetpable, refuser |
|
| 1331 | + if (!$force && !in_array($format, formats_image_acceptables($process === 'gd2'))) { |
|
| 1332 | + return; |
|
| 1333 | + } |
|
| 1334 | + $destination = "$destdir/$destfile"; |
|
| 1335 | + |
|
| 1336 | + // calculer la taille |
|
| 1337 | + if (($srcWidth = $valeurs['largeur']) && ($srcHeight = $valeurs['hauteur'])) { |
|
| 1338 | + if (!($destWidth = $valeurs['largeur_dest']) || !($destHeight = $valeurs['hauteur_dest'])) { |
|
| 1339 | + [$destWidth, $destHeight] = _image_ratio($srcWidth, $srcHeight, $maxWidth, $maxHeight); |
|
| 1340 | + } |
|
| 1341 | + } elseif ($process == 'convert' || $process == 'imagick') { |
|
| 1342 | + $destWidth = $maxWidth; |
|
| 1343 | + $destHeight = $maxHeight; |
|
| 1344 | + } else { |
|
| 1345 | + spip_logger('images')->info("echec $process sur $image"); |
|
| 1346 | + |
|
| 1347 | + return; |
|
| 1348 | + } |
|
| 1349 | + |
|
| 1350 | + $vignette = ''; |
|
| 1351 | + |
|
| 1352 | + // Si l'image est de la taille demandee (ou plus petite), simplement la retourner |
|
| 1353 | + if ($srcWidth && $srcWidth <= $maxWidth && $srcHeight <= $maxHeight) { |
|
| 1354 | + $vignette = $destination . '.' . $format; |
|
| 1355 | + @copy($image, $vignette); |
|
| 1356 | + } |
|
| 1357 | + |
|
| 1358 | + elseif ($valeurs['format_source'] === 'svg') { |
|
| 1359 | + include_spip('inc/svg'); |
|
| 1360 | + if ($svg = svg_redimensionner($valeurs['fichier'], $destWidth, $destHeight)) { |
|
| 1361 | + $format_sortie = 'svg'; |
|
| 1362 | + $vignette = $destination . '.' . $format_sortie; |
|
| 1363 | + $valeurs['fichier_dest'] = $vignette; |
|
| 1364 | + _image_gd_output($svg, $valeurs); |
|
| 1365 | + } |
|
| 1366 | + } |
|
| 1367 | + |
|
| 1368 | + // imagemagick en ligne de commande |
|
| 1369 | + elseif ($process == 'convert') { |
|
| 1370 | + if (!defined('_CONVERT_COMMAND')) { |
|
| 1371 | + define('_CONVERT_COMMAND', 'convert'); |
|
| 1372 | + } // Securite : mes_options.php peut preciser le chemin absolu |
|
| 1373 | + if (!defined('_RESIZE_COMMAND')) { |
|
| 1374 | + define('_RESIZE_COMMAND', _CONVERT_COMMAND . ' -quality ' . _IMG_CONVERT_QUALITE . ' -orient Undefined -resize %xx%y! %src %dest'); |
|
| 1375 | + } |
|
| 1376 | + $vignette = $destination . '.' . $format_sortie; |
|
| 1377 | + $commande = str_replace( |
|
| 1378 | + ['%x', '%y', '%src', '%dest'], |
|
| 1379 | + [ |
|
| 1380 | + $destWidth, |
|
| 1381 | + $destHeight, |
|
| 1382 | + escapeshellcmd($image), |
|
| 1383 | + escapeshellcmd($vignette) |
|
| 1384 | + ], |
|
| 1385 | + (string) _RESIZE_COMMAND |
|
| 1386 | + ); |
|
| 1387 | + spip_logger('images')->info($commande); |
|
| 1388 | + exec($commande); |
|
| 1389 | + if (!@file_exists($vignette)) { |
|
| 1390 | + spip_logger('images')->info("echec convert sur $vignette"); |
|
| 1391 | + |
|
| 1392 | + return; // echec commande |
|
| 1393 | + } |
|
| 1394 | + } |
|
| 1395 | + |
|
| 1396 | + // php5 imagemagick |
|
| 1397 | + elseif ($process == 'imagick') { |
|
| 1398 | + if (!class_exists(\Imagick::class)) { |
|
| 1399 | + spip_logger('images')->error('Classe Imagick absente !'); |
|
| 1400 | + |
|
| 1401 | + return; |
|
| 1402 | + } |
|
| 1403 | + |
|
| 1404 | + // chemin compatible Windows |
|
| 1405 | + $output = realpath(dirname($destination)); |
|
| 1406 | + if (!$output) { |
|
| 1407 | + return; |
|
| 1408 | + } |
|
| 1409 | + $vignette = $output . DIRECTORY_SEPARATOR . basename($destination) . '.' . $format_sortie; |
|
| 1410 | + |
|
| 1411 | + $imagick = new Imagick(); |
|
| 1412 | + $imagick->readImage(realpath($image)); |
|
| 1413 | + $imagick->resizeImage( |
|
| 1414 | + $destWidth, |
|
| 1415 | + $destHeight, |
|
| 1416 | + Imagick::FILTER_LANCZOS, |
|
| 1417 | + 1 |
|
| 1418 | + );//, IMAGICK_FILTER_LANCZOS, _IMG_IMAGICK_QUALITE / 100); |
|
| 1419 | + $imagick->writeImage($vignette); |
|
| 1420 | + |
|
| 1421 | + if (!@file_exists($vignette)) { |
|
| 1422 | + spip_logger('images')->info("echec imagick sur $vignette"); |
|
| 1423 | + |
|
| 1424 | + return; |
|
| 1425 | + } |
|
| 1426 | + // remettre le chemin relatif car c'est ce qu'attend SPIP pour la suite (en particlier action/tester) |
|
| 1427 | + $vignette = $destination . '.' . $format_sortie; |
|
| 1428 | + } |
|
| 1429 | + |
|
| 1430 | + // netpbm |
|
| 1431 | + elseif ($process == 'netpbm') { |
|
| 1432 | + if (!defined('_PNMSCALE_COMMAND')) { |
|
| 1433 | + define('_PNMSCALE_COMMAND', 'pnmscale'); |
|
| 1434 | + } // Securite : mes_options.php peut preciser le chemin absolu |
|
| 1435 | + if (_PNMSCALE_COMMAND == '') { |
|
| 1436 | + return; |
|
| 1437 | + } |
|
| 1438 | + $vignette = $destination . '.' . $format_sortie; |
|
| 1439 | + $pnmtojpeg_command = str_replace('pnmscale', 'pnmtojpeg', (string) _PNMSCALE_COMMAND); |
|
| 1440 | + if ($format == 'jpg') { |
|
| 1441 | + $jpegtopnm_command = str_replace('pnmscale', 'jpegtopnm', (string) _PNMSCALE_COMMAND); |
|
| 1442 | + exec("$jpegtopnm_command $image | " . _PNMSCALE_COMMAND . " -width $destWidth | $pnmtojpeg_command > $vignette"); |
|
| 1443 | + if (!($s = @filesize($vignette))) { |
|
| 1444 | + spip_unlink($vignette); |
|
| 1445 | + } |
|
| 1446 | + if (!@file_exists($vignette)) { |
|
| 1447 | + spip_logger('images')->info("echec netpbm-jpg sur $vignette"); |
|
| 1448 | + |
|
| 1449 | + return; |
|
| 1450 | + } |
|
| 1451 | + } else { |
|
| 1452 | + if ($format == 'gif') { |
|
| 1453 | + $giftopnm_command = str_replace('pnmscale', 'giftopnm', (string) _PNMSCALE_COMMAND); |
|
| 1454 | + exec("$giftopnm_command $image | " . _PNMSCALE_COMMAND . " -width $destWidth | $pnmtojpeg_command > $vignette"); |
|
| 1455 | + if (!($s = @filesize($vignette))) { |
|
| 1456 | + spip_unlink($vignette); |
|
| 1457 | + } |
|
| 1458 | + if (!@file_exists($vignette)) { |
|
| 1459 | + spip_logger('images')->info("echec netpbm-gif sur $vignette"); |
|
| 1460 | + |
|
| 1461 | + return; |
|
| 1462 | + } |
|
| 1463 | + } else { |
|
| 1464 | + if ($format == 'png') { |
|
| 1465 | + $pngtopnm_command = str_replace('pnmscale', 'pngtopnm', (string) _PNMSCALE_COMMAND); |
|
| 1466 | + exec("$pngtopnm_command $image | " . _PNMSCALE_COMMAND . " -width $destWidth | $pnmtojpeg_command > $vignette"); |
|
| 1467 | + if (!($s = @filesize($vignette))) { |
|
| 1468 | + spip_unlink($vignette); |
|
| 1469 | + } |
|
| 1470 | + if (!@file_exists($vignette)) { |
|
| 1471 | + spip_logger('images')->info("echec netpbm-png sur $vignette"); |
|
| 1472 | + |
|
| 1473 | + return; |
|
| 1474 | + } |
|
| 1475 | + } |
|
| 1476 | + } |
|
| 1477 | + } |
|
| 1478 | + } |
|
| 1479 | + |
|
| 1480 | + // gd ou gd2 |
|
| 1481 | + elseif ($process === 'gd2') { |
|
| 1482 | + if (!function_exists('gd_info')) { |
|
| 1483 | + spip_logger('images')->error('Librairie GD absente !'); |
|
| 1484 | + |
|
| 1485 | + return; |
|
| 1486 | + } |
|
| 1487 | + if (_IMG_GD_MAX_PIXELS && $srcWidth * $srcHeight > _IMG_GD_MAX_PIXELS) { |
|
| 1488 | + spip_logger('images')->info('vignette gd2 impossible : ' . $srcWidth * $srcHeight . 'pixels'); |
|
| 1489 | + |
|
| 1490 | + return; |
|
| 1491 | + } |
|
| 1492 | + $destFormat = $format_sortie; |
|
| 1493 | + if (!$destFormat) { |
|
| 1494 | + spip_logger('images')->info("pas de format pour $image"); |
|
| 1495 | + |
|
| 1496 | + return; |
|
| 1497 | + } |
|
| 1498 | + |
|
| 1499 | + $fonction_imagecreatefrom = $valeurs['fonction_imagecreatefrom']; |
|
| 1500 | + if (!function_exists($fonction_imagecreatefrom)) { |
|
| 1501 | + return; |
|
| 1502 | + } |
|
| 1503 | + $srcImage = @$fonction_imagecreatefrom($image); |
|
| 1504 | + if (!$srcImage) { |
|
| 1505 | + spip_logger('images')->info('echec gd2'); |
|
| 1506 | + |
|
| 1507 | + return; |
|
| 1508 | + } |
|
| 1509 | + |
|
| 1510 | + // Initialisation de l'image destination |
|
| 1511 | + $destImage = null; |
|
| 1512 | + if ($process == 'gd2' && $destFormat != 'gif') { |
|
| 1513 | + $destImage = ImageCreateTrueColor($destWidth, $destHeight); |
|
| 1514 | + } |
|
| 1515 | + if (!$destImage) { |
|
| 1516 | + $destImage = ImageCreate($destWidth, $destHeight); |
|
| 1517 | + } |
|
| 1518 | + |
|
| 1519 | + // Recopie de l'image d'origine avec adaptation de la taille |
|
| 1520 | + $ok = false; |
|
| 1521 | + if ($process == 'gd2' && function_exists('ImageCopyResampled')) { |
|
| 1522 | + if ($format == 'gif') { |
|
| 1523 | + // Si un GIF est transparent, |
|
| 1524 | + // fabriquer un PNG transparent |
|
| 1525 | + $transp = imagecolortransparent($srcImage); |
|
| 1526 | + if ($transp > 0) { |
|
| 1527 | + $destFormat = 'png'; |
|
| 1528 | + } |
|
| 1529 | + } |
|
| 1530 | + if (in_array($destFormat, _image_extensions_conservent_transparence())) { |
|
| 1531 | + // Conserver la transparence |
|
| 1532 | + if (function_exists('imageAntiAlias')) { |
|
| 1533 | + imageAntiAlias($destImage, true); |
|
| 1534 | + } |
|
| 1535 | + @imagealphablending($destImage, false); |
|
| 1536 | + @imagesavealpha($destImage, true); |
|
| 1537 | + } |
|
| 1538 | + $ok = @ImageCopyResampled($destImage, $srcImage, 0, 0, 0, 0, $destWidth, $destHeight, $srcWidth, $srcHeight); |
|
| 1539 | + } |
|
| 1540 | + if (!$ok) { |
|
| 1541 | + $ok = ImageCopyResized($destImage, $srcImage, 0, 0, 0, 0, $destWidth, $destHeight, $srcWidth, $srcHeight); |
|
| 1542 | + } |
|
| 1543 | + |
|
| 1544 | + // Sauvegarde de l'image destination |
|
| 1545 | + $valeurs['fichier_dest'] = $vignette = "$destination.$destFormat"; |
|
| 1546 | + $valeurs['format_dest'] = $format = $destFormat; |
|
| 1547 | + _image_gd_output($destImage, $valeurs); |
|
| 1548 | + |
|
| 1549 | + if ($srcImage) { |
|
| 1550 | + ImageDestroy($srcImage); |
|
| 1551 | + } |
|
| 1552 | + ImageDestroy($destImage); |
|
| 1553 | + } |
|
| 1554 | + |
|
| 1555 | + if (!$vignette || !$size = @spip_getimagesize($vignette)) { |
|
| 1556 | + $size = [$destWidth, $destHeight]; |
|
| 1557 | + } |
|
| 1558 | + |
|
| 1559 | + // Gaffe: en safe mode, pas d'acces a la vignette, |
|
| 1560 | + // donc risque de balancer "width='0'", ce qui masque l'image sous MSIE |
|
| 1561 | + if ($size[0] < 1) { |
|
| 1562 | + $size[0] = $destWidth; |
|
| 1563 | + } |
|
| 1564 | + if ($size[1] < 1) { |
|
| 1565 | + $size[1] = $destHeight; |
|
| 1566 | + } |
|
| 1567 | + |
|
| 1568 | + $retour['width'] = $largeur = $size[0]; |
|
| 1569 | + $retour['height'] = $hauteur = $size[1]; |
|
| 1570 | + |
|
| 1571 | + $retour['fichier'] = $vignette; |
|
| 1572 | + $retour['format'] = $format; |
|
| 1573 | + $retour['date'] = @filemtime($vignette); |
|
| 1574 | + |
|
| 1575 | + // renvoyer l'image |
|
| 1576 | + return $retour; |
|
| 1577 | 1577 | } |
| 1578 | 1578 | |
| 1579 | 1579 | /** |
@@ -1593,25 +1593,25 @@ discard block |
||
| 1593 | 1593 | * @return array Liste [ largeur, hauteur, ratio de réduction ] |
| 1594 | 1594 | **/ |
| 1595 | 1595 | function _image_ratio(int $srcWidth, int $srcHeight, int $maxWidth, int $maxHeight): array { |
| 1596 | - $ratioWidth = $srcWidth / $maxWidth; |
|
| 1597 | - $ratioHeight = $srcHeight / $maxHeight; |
|
| 1598 | - |
|
| 1599 | - if ($srcWidth <= $maxWidth && $srcHeight <= $maxHeight) { |
|
| 1600 | - $destWidth = $srcWidth; |
|
| 1601 | - $destHeight = $srcHeight; |
|
| 1602 | - } elseif ($ratioWidth < $ratioHeight) { |
|
| 1603 | - $destWidth = $srcWidth / $ratioHeight; |
|
| 1604 | - $destHeight = $maxHeight; |
|
| 1605 | - } else { |
|
| 1606 | - $destWidth = $maxWidth; |
|
| 1607 | - $destHeight = $srcHeight / $ratioWidth; |
|
| 1608 | - } |
|
| 1609 | - |
|
| 1610 | - return [ |
|
| 1611 | - (int) round($destWidth), |
|
| 1612 | - (int) round($destHeight), |
|
| 1613 | - max($ratioWidth, $ratioHeight) |
|
| 1614 | - ]; |
|
| 1596 | + $ratioWidth = $srcWidth / $maxWidth; |
|
| 1597 | + $ratioHeight = $srcHeight / $maxHeight; |
|
| 1598 | + |
|
| 1599 | + if ($srcWidth <= $maxWidth && $srcHeight <= $maxHeight) { |
|
| 1600 | + $destWidth = $srcWidth; |
|
| 1601 | + $destHeight = $srcHeight; |
|
| 1602 | + } elseif ($ratioWidth < $ratioHeight) { |
|
| 1603 | + $destWidth = $srcWidth / $ratioHeight; |
|
| 1604 | + $destHeight = $maxHeight; |
|
| 1605 | + } else { |
|
| 1606 | + $destWidth = $maxWidth; |
|
| 1607 | + $destHeight = $srcHeight / $ratioWidth; |
|
| 1608 | + } |
|
| 1609 | + |
|
| 1610 | + return [ |
|
| 1611 | + (int) round($destWidth), |
|
| 1612 | + (int) round($destHeight), |
|
| 1613 | + max($ratioWidth, $ratioHeight) |
|
| 1614 | + ]; |
|
| 1615 | 1615 | } |
| 1616 | 1616 | |
| 1617 | 1617 | /** |
@@ -1631,25 +1631,25 @@ discard block |
||
| 1631 | 1631 | * @return array Liste [ largeur, hauteur, ratio de réduction ] |
| 1632 | 1632 | **/ |
| 1633 | 1633 | function ratio_passe_partout(int $srcWidth, int $srcHeight, int $maxWidth, int $maxHeight): array { |
| 1634 | - $ratioWidth = $srcWidth / $maxWidth; |
|
| 1635 | - $ratioHeight = $srcHeight / $maxHeight; |
|
| 1636 | - |
|
| 1637 | - if ($srcWidth <= $maxWidth && $srcHeight <= $maxHeight) { |
|
| 1638 | - $destWidth = $srcWidth; |
|
| 1639 | - $destHeight = $srcHeight; |
|
| 1640 | - } elseif ($ratioWidth > $ratioHeight) { |
|
| 1641 | - $destWidth = $srcWidth / $ratioHeight; |
|
| 1642 | - $destHeight = $maxHeight; |
|
| 1643 | - } else { |
|
| 1644 | - $destWidth = $maxWidth; |
|
| 1645 | - $destHeight = $srcHeight / $ratioWidth; |
|
| 1646 | - } |
|
| 1647 | - |
|
| 1648 | - return [ |
|
| 1649 | - (int) round($destWidth), |
|
| 1650 | - (int) round($destHeight), |
|
| 1651 | - min($ratioWidth, $ratioHeight) |
|
| 1652 | - ]; |
|
| 1634 | + $ratioWidth = $srcWidth / $maxWidth; |
|
| 1635 | + $ratioHeight = $srcHeight / $maxHeight; |
|
| 1636 | + |
|
| 1637 | + if ($srcWidth <= $maxWidth && $srcHeight <= $maxHeight) { |
|
| 1638 | + $destWidth = $srcWidth; |
|
| 1639 | + $destHeight = $srcHeight; |
|
| 1640 | + } elseif ($ratioWidth > $ratioHeight) { |
|
| 1641 | + $destWidth = $srcWidth / $ratioHeight; |
|
| 1642 | + $destHeight = $maxHeight; |
|
| 1643 | + } else { |
|
| 1644 | + $destWidth = $maxWidth; |
|
| 1645 | + $destHeight = $srcHeight / $ratioWidth; |
|
| 1646 | + } |
|
| 1647 | + |
|
| 1648 | + return [ |
|
| 1649 | + (int) round($destWidth), |
|
| 1650 | + (int) round($destHeight), |
|
| 1651 | + min($ratioWidth, $ratioHeight) |
|
| 1652 | + ]; |
|
| 1653 | 1653 | } |
| 1654 | 1654 | |
| 1655 | 1655 | |
@@ -1662,12 +1662,12 @@ discard block |
||
| 1662 | 1662 | * @return string |
| 1663 | 1663 | */ |
| 1664 | 1664 | function process_image_svg_identite($image) { |
| 1665 | - if ($image['creer']) { |
|
| 1666 | - $source = $image['fichier']; |
|
| 1667 | - _image_gd_output($source, $image); |
|
| 1668 | - } |
|
| 1665 | + if ($image['creer']) { |
|
| 1666 | + $source = $image['fichier']; |
|
| 1667 | + _image_gd_output($source, $image); |
|
| 1668 | + } |
|
| 1669 | 1669 | |
| 1670 | - return _image_ecrire_tag($image, ['src' => $image['fichier_dest']]); |
|
| 1670 | + return _image_ecrire_tag($image, ['src' => $image['fichier_dest']]); |
|
| 1671 | 1671 | } |
| 1672 | 1672 | |
| 1673 | 1673 | |
@@ -1700,102 +1700,102 @@ discard block |
||
| 1700 | 1700 | * Code HTML de la balise img produite |
| 1701 | 1701 | **/ |
| 1702 | 1702 | function process_image_reduire($fonction, $img, $taille, $taille_y, $force, $process = 'AUTO') { |
| 1703 | - $image = false; |
|
| 1704 | - if ($process == 'AUTO' && isset($GLOBALS['meta']['image_process'])) { |
|
| 1705 | - $process = $GLOBALS['meta']['image_process']; |
|
| 1706 | - } |
|
| 1707 | - # determiner le format de sortie |
|
| 1708 | - $format_sortie = false; // le choix par defaut sera bon |
|
| 1709 | - if ($process === 'netpbm') { |
|
| 1710 | - $format_sortie = 'jpg'; |
|
| 1711 | - } elseif ($process === 'gd2') { |
|
| 1712 | - $image = _image_valeurs_trans($img, "reduire-{$taille}-{$taille_y}", $format_sortie, $fonction, false, _SVG_SUPPORTED); |
|
| 1713 | - // on verifie que l'extension choisie est bonne (en principe oui) |
|
| 1714 | - $gd_formats = formats_image_acceptables(true); |
|
| 1715 | - if ( |
|
| 1716 | - is_array($image) |
|
| 1717 | - && (!in_array($image['format_dest'], $gd_formats) || !in_array($image['format_dest'], _image_extensions_acceptees_en_sortie())) |
|
| 1718 | - ) { |
|
| 1719 | - $formats_sortie = $image['format_source'] == 'jpg' ? ['jpg', 'png', 'gif'] : ['png', 'jpg', 'gif']; |
|
| 1720 | - // Choisir le format destination |
|
| 1721 | - // - on sauve de preference en JPEG (meilleure compression) |
|
| 1722 | - // - pour le GIF : les GD recentes peuvent le lire mais pas l'ecrire |
|
| 1723 | - # bug : gd_formats contient la liste des fichiers qu'on sait *lire*, |
|
| 1724 | - # pas *ecrire* |
|
| 1725 | - $format_sortie = ''; |
|
| 1726 | - foreach ($formats_sortie as $fmt) { |
|
| 1727 | - if (in_array($fmt, $gd_formats) && in_array($fmt, _image_extensions_acceptees_en_sortie())) { |
|
| 1728 | - $format_sortie = $fmt; |
|
| 1729 | - break; |
|
| 1730 | - } |
|
| 1731 | - } |
|
| 1732 | - $image = false; |
|
| 1733 | - } |
|
| 1734 | - } |
|
| 1735 | - |
|
| 1736 | - if (!is_array($image)) { |
|
| 1737 | - $image = _image_valeurs_trans($img, "reduire-{$taille}-{$taille_y}", $format_sortie, $fonction, false, _SVG_SUPPORTED); |
|
| 1738 | - } |
|
| 1739 | - |
|
| 1740 | - if (!is_array($image) || !$image['largeur'] || !$image['hauteur']) { |
|
| 1741 | - spip_logger('images')->info("image_reduire_src:pas de version locale de $img ou extension non prise en charge"); |
|
| 1742 | - // on peut resizer en mode html si on dispose des elements |
|
| 1743 | - [$srcw, $srch] = taille_image($img); |
|
| 1744 | - if ($srcw && $srch) { |
|
| 1745 | - [$w, $h] = _image_ratio($srcw, $srch, $taille, $taille_y); |
|
| 1746 | - |
|
| 1747 | - return _image_tag_changer_taille($img, $w, $h); |
|
| 1748 | - } |
|
| 1749 | - // la on n'a pas d'infos sur l'image source... on refile le truc a css |
|
| 1750 | - // sous la forme style='max-width: NNpx;' |
|
| 1751 | - return inserer_attribut( |
|
| 1752 | - $img, |
|
| 1753 | - 'style', |
|
| 1754 | - "max-width: {$taille}px;max-width: min(100%,{$taille}px); max-height: {$taille_y}px" |
|
| 1755 | - ); |
|
| 1756 | - } |
|
| 1757 | - |
|
| 1758 | - // si l'image est plus petite que la cible retourner une copie cachee de l'image |
|
| 1759 | - if (($image['largeur'] <= $taille) && ($image['hauteur'] <= $taille_y)) { |
|
| 1760 | - if ($image['creer']) { |
|
| 1761 | - @copy($image['fichier'], $image['fichier_dest']); |
|
| 1762 | - } |
|
| 1763 | - |
|
| 1764 | - return _image_ecrire_tag($image, ['src' => $image['fichier_dest']]); |
|
| 1765 | - } |
|
| 1766 | - |
|
| 1767 | - if ($image['creer'] == false && !$force) { |
|
| 1768 | - return _image_ecrire_tag( |
|
| 1769 | - $image, |
|
| 1770 | - ['src' => $image['fichier_dest'], 'width' => $image['largeur_dest'], 'height' => $image['hauteur_dest']] |
|
| 1771 | - ); |
|
| 1772 | - } |
|
| 1773 | - |
|
| 1774 | - if (in_array($image['format_source'], _image_extensions_acceptees_en_entree())) { |
|
| 1775 | - $destWidth = $image['largeur_dest']; |
|
| 1776 | - $destHeight = $image['hauteur_dest']; |
|
| 1777 | - $logo = $image['fichier']; |
|
| 1778 | - $date = $image['date_src']; |
|
| 1779 | - $preview = _image_creer_vignette($image, $taille, $taille_y, $process, $force); |
|
| 1780 | - |
|
| 1781 | - if ($preview && $preview['fichier']) { |
|
| 1782 | - $logo = $preview['fichier']; |
|
| 1783 | - $destWidth = $preview['width']; |
|
| 1784 | - $destHeight = $preview['height']; |
|
| 1785 | - $date = $preview['date']; |
|
| 1786 | - } |
|
| 1787 | - // dans l'espace prive mettre un timestamp sur l'adresse |
|
| 1788 | - // de l'image, de facon a tromper le cache du navigateur |
|
| 1789 | - // quand on fait supprimer/reuploader un logo |
|
| 1790 | - // (pas de filemtime si SAFE MODE) |
|
| 1791 | - $date = test_espace_prive() ? ('?' . $date) : ''; |
|
| 1792 | - |
|
| 1793 | - return _image_ecrire_tag($image, ['src' => "$logo$date", 'width' => $destWidth, 'height' => $destHeight]); |
|
| 1794 | - } |
|
| 1795 | - else { |
|
| 1796 | - # BMP, tiff ... les redacteurs osent tout! |
|
| 1797 | - return $img; |
|
| 1798 | - } |
|
| 1703 | + $image = false; |
|
| 1704 | + if ($process == 'AUTO' && isset($GLOBALS['meta']['image_process'])) { |
|
| 1705 | + $process = $GLOBALS['meta']['image_process']; |
|
| 1706 | + } |
|
| 1707 | + # determiner le format de sortie |
|
| 1708 | + $format_sortie = false; // le choix par defaut sera bon |
|
| 1709 | + if ($process === 'netpbm') { |
|
| 1710 | + $format_sortie = 'jpg'; |
|
| 1711 | + } elseif ($process === 'gd2') { |
|
| 1712 | + $image = _image_valeurs_trans($img, "reduire-{$taille}-{$taille_y}", $format_sortie, $fonction, false, _SVG_SUPPORTED); |
|
| 1713 | + // on verifie que l'extension choisie est bonne (en principe oui) |
|
| 1714 | + $gd_formats = formats_image_acceptables(true); |
|
| 1715 | + if ( |
|
| 1716 | + is_array($image) |
|
| 1717 | + && (!in_array($image['format_dest'], $gd_formats) || !in_array($image['format_dest'], _image_extensions_acceptees_en_sortie())) |
|
| 1718 | + ) { |
|
| 1719 | + $formats_sortie = $image['format_source'] == 'jpg' ? ['jpg', 'png', 'gif'] : ['png', 'jpg', 'gif']; |
|
| 1720 | + // Choisir le format destination |
|
| 1721 | + // - on sauve de preference en JPEG (meilleure compression) |
|
| 1722 | + // - pour le GIF : les GD recentes peuvent le lire mais pas l'ecrire |
|
| 1723 | + # bug : gd_formats contient la liste des fichiers qu'on sait *lire*, |
|
| 1724 | + # pas *ecrire* |
|
| 1725 | + $format_sortie = ''; |
|
| 1726 | + foreach ($formats_sortie as $fmt) { |
|
| 1727 | + if (in_array($fmt, $gd_formats) && in_array($fmt, _image_extensions_acceptees_en_sortie())) { |
|
| 1728 | + $format_sortie = $fmt; |
|
| 1729 | + break; |
|
| 1730 | + } |
|
| 1731 | + } |
|
| 1732 | + $image = false; |
|
| 1733 | + } |
|
| 1734 | + } |
|
| 1735 | + |
|
| 1736 | + if (!is_array($image)) { |
|
| 1737 | + $image = _image_valeurs_trans($img, "reduire-{$taille}-{$taille_y}", $format_sortie, $fonction, false, _SVG_SUPPORTED); |
|
| 1738 | + } |
|
| 1739 | + |
|
| 1740 | + if (!is_array($image) || !$image['largeur'] || !$image['hauteur']) { |
|
| 1741 | + spip_logger('images')->info("image_reduire_src:pas de version locale de $img ou extension non prise en charge"); |
|
| 1742 | + // on peut resizer en mode html si on dispose des elements |
|
| 1743 | + [$srcw, $srch] = taille_image($img); |
|
| 1744 | + if ($srcw && $srch) { |
|
| 1745 | + [$w, $h] = _image_ratio($srcw, $srch, $taille, $taille_y); |
|
| 1746 | + |
|
| 1747 | + return _image_tag_changer_taille($img, $w, $h); |
|
| 1748 | + } |
|
| 1749 | + // la on n'a pas d'infos sur l'image source... on refile le truc a css |
|
| 1750 | + // sous la forme style='max-width: NNpx;' |
|
| 1751 | + return inserer_attribut( |
|
| 1752 | + $img, |
|
| 1753 | + 'style', |
|
| 1754 | + "max-width: {$taille}px;max-width: min(100%,{$taille}px); max-height: {$taille_y}px" |
|
| 1755 | + ); |
|
| 1756 | + } |
|
| 1757 | + |
|
| 1758 | + // si l'image est plus petite que la cible retourner une copie cachee de l'image |
|
| 1759 | + if (($image['largeur'] <= $taille) && ($image['hauteur'] <= $taille_y)) { |
|
| 1760 | + if ($image['creer']) { |
|
| 1761 | + @copy($image['fichier'], $image['fichier_dest']); |
|
| 1762 | + } |
|
| 1763 | + |
|
| 1764 | + return _image_ecrire_tag($image, ['src' => $image['fichier_dest']]); |
|
| 1765 | + } |
|
| 1766 | + |
|
| 1767 | + if ($image['creer'] == false && !$force) { |
|
| 1768 | + return _image_ecrire_tag( |
|
| 1769 | + $image, |
|
| 1770 | + ['src' => $image['fichier_dest'], 'width' => $image['largeur_dest'], 'height' => $image['hauteur_dest']] |
|
| 1771 | + ); |
|
| 1772 | + } |
|
| 1773 | + |
|
| 1774 | + if (in_array($image['format_source'], _image_extensions_acceptees_en_entree())) { |
|
| 1775 | + $destWidth = $image['largeur_dest']; |
|
| 1776 | + $destHeight = $image['hauteur_dest']; |
|
| 1777 | + $logo = $image['fichier']; |
|
| 1778 | + $date = $image['date_src']; |
|
| 1779 | + $preview = _image_creer_vignette($image, $taille, $taille_y, $process, $force); |
|
| 1780 | + |
|
| 1781 | + if ($preview && $preview['fichier']) { |
|
| 1782 | + $logo = $preview['fichier']; |
|
| 1783 | + $destWidth = $preview['width']; |
|
| 1784 | + $destHeight = $preview['height']; |
|
| 1785 | + $date = $preview['date']; |
|
| 1786 | + } |
|
| 1787 | + // dans l'espace prive mettre un timestamp sur l'adresse |
|
| 1788 | + // de l'image, de facon a tromper le cache du navigateur |
|
| 1789 | + // quand on fait supprimer/reuploader un logo |
|
| 1790 | + // (pas de filemtime si SAFE MODE) |
|
| 1791 | + $date = test_espace_prive() ? ('?' . $date) : ''; |
|
| 1792 | + |
|
| 1793 | + return _image_ecrire_tag($image, ['src' => "$logo$date", 'width' => $destWidth, 'height' => $destHeight]); |
|
| 1794 | + } |
|
| 1795 | + else { |
|
| 1796 | + # BMP, tiff ... les redacteurs osent tout! |
|
| 1797 | + return $img; |
|
| 1798 | + } |
|
| 1799 | 1799 | } |
| 1800 | 1800 | |
| 1801 | 1801 | /** |
@@ -1809,145 +1809,145 @@ discard block |
||
| 1809 | 1809 | * Class phpthumb_functions |
| 1810 | 1810 | */ |
| 1811 | 1811 | class phpthumb_functions { |
| 1812 | - /** |
|
| 1813 | - * Retourne la couleur d'un pixel dans une image |
|
| 1814 | - * |
|
| 1815 | - * @param GdImage $img |
|
| 1816 | - * @param int $x |
|
| 1817 | - * @param int $y |
|
| 1818 | - * @return array|bool |
|
| 1819 | - */ |
|
| 1820 | - public static function GetPixelColor(&$img, $x, $y) { |
|
| 1821 | - if ($img instanceof \GdImage) { |
|
| 1822 | - return @ImageColorsForIndex($img, @ImageColorAt($img, $x, $y)); |
|
| 1823 | - } |
|
| 1824 | - return false; |
|
| 1825 | - } |
|
| 1826 | - |
|
| 1827 | - /** |
|
| 1828 | - * Retourne un nombre dans une représentation en Little Endian |
|
| 1829 | - * |
|
| 1830 | - * @param int $number |
|
| 1831 | - * @param int $minbytes |
|
| 1832 | - * @return string |
|
| 1833 | - */ |
|
| 1834 | - public static function LittleEndian2String($number, $minbytes = 1) { |
|
| 1835 | - $intstring = ''; |
|
| 1836 | - while ($number > 0) { |
|
| 1837 | - $intstring .= chr($number & 255); |
|
| 1838 | - $number >>= 8; |
|
| 1839 | - } |
|
| 1840 | - |
|
| 1841 | - return str_pad($intstring, $minbytes, "\x00", STR_PAD_RIGHT); |
|
| 1842 | - } |
|
| 1843 | - |
|
| 1844 | - /** |
|
| 1845 | - * Transforme une ressource GD en image au format ICO |
|
| 1846 | - * |
|
| 1847 | - * @param array $gd_image_array |
|
| 1848 | - * Tableau de ressources d'images GD |
|
| 1849 | - * @return string |
|
| 1850 | - * Image au format ICO |
|
| 1851 | - */ |
|
| 1852 | - public static function GD2ICOstring(&$gd_image_array) { |
|
| 1853 | - foreach ($gd_image_array as $key => $gd_image) { |
|
| 1854 | - $ImageWidths[$key] = ImageSX($gd_image); |
|
| 1855 | - $ImageHeights[$key] = ImageSY($gd_image); |
|
| 1856 | - $bpp[$key] = ImageIsTrueColor($gd_image) ? 32 : 24; |
|
| 1857 | - $totalcolors[$key] = ImageColorsTotal($gd_image); |
|
| 1858 | - |
|
| 1859 | - $icXOR[$key] = ''; |
|
| 1860 | - for ($y = $ImageHeights[$key] - 1; $y >= 0; $y--) { |
|
| 1861 | - for ($x = 0; $x < $ImageWidths[$key]; $x++) { |
|
| 1862 | - $argb = phpthumb_functions::GetPixelColor($gd_image, $x, $y); |
|
| 1863 | - $a = round(255 * ((127 - $argb['alpha']) / 127)); |
|
| 1864 | - $r = $argb['red']; |
|
| 1865 | - $g = $argb['green']; |
|
| 1866 | - $b = $argb['blue']; |
|
| 1867 | - |
|
| 1868 | - if ($bpp[$key] == 32) { |
|
| 1869 | - $icXOR[$key] .= chr($b) . chr($g) . chr($r) . chr($a); |
|
| 1870 | - } elseif ($bpp[$key] === 24) { |
|
| 1871 | - $icXOR[$key] .= chr($b) . chr($g) . chr($r); |
|
| 1872 | - } |
|
| 1873 | - |
|
| 1874 | - if ($a < 128) { |
|
| 1875 | - @$icANDmask[$key][$y] .= '1'; |
|
| 1876 | - } else { |
|
| 1877 | - @$icANDmask[$key][$y] .= '0'; |
|
| 1878 | - } |
|
| 1879 | - } |
|
| 1880 | - // mask bits are 32-bit aligned per scanline |
|
| 1881 | - while (strlen($icANDmask[$key][$y]) % 32) { |
|
| 1882 | - $icANDmask[$key][$y] .= '0'; |
|
| 1883 | - } |
|
| 1884 | - } |
|
| 1885 | - $icAND[$key] = ''; |
|
| 1886 | - foreach ($icANDmask[$key] as $y => $scanlinemaskbits) { |
|
| 1887 | - for ($i = 0; $i < strlen($scanlinemaskbits); $i += 8) { |
|
| 1888 | - $icAND[$key] .= chr(bindec(str_pad(substr($scanlinemaskbits, $i, 8), 8, '0', STR_PAD_LEFT))); |
|
| 1889 | - } |
|
| 1890 | - } |
|
| 1891 | - } |
|
| 1892 | - |
|
| 1893 | - foreach (array_keys($gd_image_array) as $key) { |
|
| 1894 | - $biSizeImage = $ImageWidths[$key] * $ImageHeights[$key] * ($bpp[$key] / 8); |
|
| 1895 | - |
|
| 1896 | - // BITMAPINFOHEADER - 40 bytes |
|
| 1897 | - $BitmapInfoHeader[$key] = ''; |
|
| 1898 | - $BitmapInfoHeader[$key] .= "\x28\x00\x00\x00"; // DWORD biSize; |
|
| 1899 | - $BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($ImageWidths[$key], 4); // LONG biWidth; |
|
| 1900 | - // The biHeight member specifies the combined |
|
| 1901 | - // height of the XOR and AND masks. |
|
| 1902 | - $BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($ImageHeights[$key] * 2, 4); // LONG biHeight; |
|
| 1903 | - $BitmapInfoHeader[$key] .= "\x01\x00"; // WORD biPlanes; |
|
| 1904 | - $BitmapInfoHeader[$key] .= chr($bpp[$key]) . "\x00"; // wBitCount; |
|
| 1905 | - $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00"; // DWORD biCompression; |
|
| 1906 | - $BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($biSizeImage, 4); // DWORD biSizeImage; |
|
| 1907 | - $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00"; // LONG biXPelsPerMeter; |
|
| 1908 | - $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00"; // LONG biYPelsPerMeter; |
|
| 1909 | - $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00"; // DWORD biClrUsed; |
|
| 1910 | - $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00"; // DWORD biClrImportant; |
|
| 1911 | - } |
|
| 1912 | - |
|
| 1913 | - |
|
| 1914 | - $icondata = "\x00\x00"; // idReserved; // Reserved (must be 0) |
|
| 1915 | - $icondata .= "\x01\x00"; // idType; // Resource Type (1 for icons) |
|
| 1916 | - $icondata .= phpthumb_functions::LittleEndian2String(count($gd_image_array), 2); // idCount; // How many images? |
|
| 1917 | - |
|
| 1918 | - $dwImageOffset = 6 + (count($gd_image_array) * 16); |
|
| 1919 | - foreach (array_keys($gd_image_array) as $key) { |
|
| 1920 | - // ICONDIRENTRY idEntries[1]; // An entry for each image (idCount of 'em) |
|
| 1921 | - |
|
| 1922 | - $icondata .= chr($ImageWidths[$key]); // bWidth; // Width, in pixels, of the image |
|
| 1923 | - $icondata .= chr($ImageHeights[$key]); // bHeight; // Height, in pixels, of the image |
|
| 1924 | - $icondata .= chr($totalcolors[$key]); // bColorCount; // Number of colors in image (0 if >=8bpp) |
|
| 1925 | - $icondata .= "\x00"; // bReserved; // Reserved ( must be 0) |
|
| 1926 | - |
|
| 1927 | - $icondata .= "\x01\x00"; // wPlanes; // Color Planes |
|
| 1928 | - $icondata .= chr($bpp[$key]) . "\x00"; // wBitCount; // Bits per pixel |
|
| 1929 | - |
|
| 1930 | - $dwBytesInRes = 40 + strlen($icXOR[$key]) + strlen($icAND[$key]); |
|
| 1931 | - $icondata .= phpthumb_functions::LittleEndian2String( |
|
| 1932 | - $dwBytesInRes, |
|
| 1933 | - 4 |
|
| 1934 | - ); // dwBytesInRes; // How many bytes in this resource? |
|
| 1935 | - |
|
| 1936 | - $icondata .= phpthumb_functions::LittleEndian2String( |
|
| 1937 | - $dwImageOffset, |
|
| 1938 | - 4 |
|
| 1939 | - ); // dwImageOffset; // Where in the file is this image? |
|
| 1940 | - $dwImageOffset += strlen($BitmapInfoHeader[$key]); |
|
| 1941 | - $dwImageOffset += strlen($icXOR[$key]); |
|
| 1942 | - $dwImageOffset += strlen($icAND[$key]); |
|
| 1943 | - } |
|
| 1944 | - |
|
| 1945 | - foreach (array_keys($gd_image_array) as $key) { |
|
| 1946 | - $icondata .= $BitmapInfoHeader[$key]; |
|
| 1947 | - $icondata .= $icXOR[$key]; |
|
| 1948 | - $icondata .= $icAND[$key]; |
|
| 1949 | - } |
|
| 1950 | - |
|
| 1951 | - return $icondata; |
|
| 1952 | - } |
|
| 1812 | + /** |
|
| 1813 | + * Retourne la couleur d'un pixel dans une image |
|
| 1814 | + * |
|
| 1815 | + * @param GdImage $img |
|
| 1816 | + * @param int $x |
|
| 1817 | + * @param int $y |
|
| 1818 | + * @return array|bool |
|
| 1819 | + */ |
|
| 1820 | + public static function GetPixelColor(&$img, $x, $y) { |
|
| 1821 | + if ($img instanceof \GdImage) { |
|
| 1822 | + return @ImageColorsForIndex($img, @ImageColorAt($img, $x, $y)); |
|
| 1823 | + } |
|
| 1824 | + return false; |
|
| 1825 | + } |
|
| 1826 | + |
|
| 1827 | + /** |
|
| 1828 | + * Retourne un nombre dans une représentation en Little Endian |
|
| 1829 | + * |
|
| 1830 | + * @param int $number |
|
| 1831 | + * @param int $minbytes |
|
| 1832 | + * @return string |
|
| 1833 | + */ |
|
| 1834 | + public static function LittleEndian2String($number, $minbytes = 1) { |
|
| 1835 | + $intstring = ''; |
|
| 1836 | + while ($number > 0) { |
|
| 1837 | + $intstring .= chr($number & 255); |
|
| 1838 | + $number >>= 8; |
|
| 1839 | + } |
|
| 1840 | + |
|
| 1841 | + return str_pad($intstring, $minbytes, "\x00", STR_PAD_RIGHT); |
|
| 1842 | + } |
|
| 1843 | + |
|
| 1844 | + /** |
|
| 1845 | + * Transforme une ressource GD en image au format ICO |
|
| 1846 | + * |
|
| 1847 | + * @param array $gd_image_array |
|
| 1848 | + * Tableau de ressources d'images GD |
|
| 1849 | + * @return string |
|
| 1850 | + * Image au format ICO |
|
| 1851 | + */ |
|
| 1852 | + public static function GD2ICOstring(&$gd_image_array) { |
|
| 1853 | + foreach ($gd_image_array as $key => $gd_image) { |
|
| 1854 | + $ImageWidths[$key] = ImageSX($gd_image); |
|
| 1855 | + $ImageHeights[$key] = ImageSY($gd_image); |
|
| 1856 | + $bpp[$key] = ImageIsTrueColor($gd_image) ? 32 : 24; |
|
| 1857 | + $totalcolors[$key] = ImageColorsTotal($gd_image); |
|
| 1858 | + |
|
| 1859 | + $icXOR[$key] = ''; |
|
| 1860 | + for ($y = $ImageHeights[$key] - 1; $y >= 0; $y--) { |
|
| 1861 | + for ($x = 0; $x < $ImageWidths[$key]; $x++) { |
|
| 1862 | + $argb = phpthumb_functions::GetPixelColor($gd_image, $x, $y); |
|
| 1863 | + $a = round(255 * ((127 - $argb['alpha']) / 127)); |
|
| 1864 | + $r = $argb['red']; |
|
| 1865 | + $g = $argb['green']; |
|
| 1866 | + $b = $argb['blue']; |
|
| 1867 | + |
|
| 1868 | + if ($bpp[$key] == 32) { |
|
| 1869 | + $icXOR[$key] .= chr($b) . chr($g) . chr($r) . chr($a); |
|
| 1870 | + } elseif ($bpp[$key] === 24) { |
|
| 1871 | + $icXOR[$key] .= chr($b) . chr($g) . chr($r); |
|
| 1872 | + } |
|
| 1873 | + |
|
| 1874 | + if ($a < 128) { |
|
| 1875 | + @$icANDmask[$key][$y] .= '1'; |
|
| 1876 | + } else { |
|
| 1877 | + @$icANDmask[$key][$y] .= '0'; |
|
| 1878 | + } |
|
| 1879 | + } |
|
| 1880 | + // mask bits are 32-bit aligned per scanline |
|
| 1881 | + while (strlen($icANDmask[$key][$y]) % 32) { |
|
| 1882 | + $icANDmask[$key][$y] .= '0'; |
|
| 1883 | + } |
|
| 1884 | + } |
|
| 1885 | + $icAND[$key] = ''; |
|
| 1886 | + foreach ($icANDmask[$key] as $y => $scanlinemaskbits) { |
|
| 1887 | + for ($i = 0; $i < strlen($scanlinemaskbits); $i += 8) { |
|
| 1888 | + $icAND[$key] .= chr(bindec(str_pad(substr($scanlinemaskbits, $i, 8), 8, '0', STR_PAD_LEFT))); |
|
| 1889 | + } |
|
| 1890 | + } |
|
| 1891 | + } |
|
| 1892 | + |
|
| 1893 | + foreach (array_keys($gd_image_array) as $key) { |
|
| 1894 | + $biSizeImage = $ImageWidths[$key] * $ImageHeights[$key] * ($bpp[$key] / 8); |
|
| 1895 | + |
|
| 1896 | + // BITMAPINFOHEADER - 40 bytes |
|
| 1897 | + $BitmapInfoHeader[$key] = ''; |
|
| 1898 | + $BitmapInfoHeader[$key] .= "\x28\x00\x00\x00"; // DWORD biSize; |
|
| 1899 | + $BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($ImageWidths[$key], 4); // LONG biWidth; |
|
| 1900 | + // The biHeight member specifies the combined |
|
| 1901 | + // height of the XOR and AND masks. |
|
| 1902 | + $BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($ImageHeights[$key] * 2, 4); // LONG biHeight; |
|
| 1903 | + $BitmapInfoHeader[$key] .= "\x01\x00"; // WORD biPlanes; |
|
| 1904 | + $BitmapInfoHeader[$key] .= chr($bpp[$key]) . "\x00"; // wBitCount; |
|
| 1905 | + $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00"; // DWORD biCompression; |
|
| 1906 | + $BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($biSizeImage, 4); // DWORD biSizeImage; |
|
| 1907 | + $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00"; // LONG biXPelsPerMeter; |
|
| 1908 | + $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00"; // LONG biYPelsPerMeter; |
|
| 1909 | + $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00"; // DWORD biClrUsed; |
|
| 1910 | + $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00"; // DWORD biClrImportant; |
|
| 1911 | + } |
|
| 1912 | + |
|
| 1913 | + |
|
| 1914 | + $icondata = "\x00\x00"; // idReserved; // Reserved (must be 0) |
|
| 1915 | + $icondata .= "\x01\x00"; // idType; // Resource Type (1 for icons) |
|
| 1916 | + $icondata .= phpthumb_functions::LittleEndian2String(count($gd_image_array), 2); // idCount; // How many images? |
|
| 1917 | + |
|
| 1918 | + $dwImageOffset = 6 + (count($gd_image_array) * 16); |
|
| 1919 | + foreach (array_keys($gd_image_array) as $key) { |
|
| 1920 | + // ICONDIRENTRY idEntries[1]; // An entry for each image (idCount of 'em) |
|
| 1921 | + |
|
| 1922 | + $icondata .= chr($ImageWidths[$key]); // bWidth; // Width, in pixels, of the image |
|
| 1923 | + $icondata .= chr($ImageHeights[$key]); // bHeight; // Height, in pixels, of the image |
|
| 1924 | + $icondata .= chr($totalcolors[$key]); // bColorCount; // Number of colors in image (0 if >=8bpp) |
|
| 1925 | + $icondata .= "\x00"; // bReserved; // Reserved ( must be 0) |
|
| 1926 | + |
|
| 1927 | + $icondata .= "\x01\x00"; // wPlanes; // Color Planes |
|
| 1928 | + $icondata .= chr($bpp[$key]) . "\x00"; // wBitCount; // Bits per pixel |
|
| 1929 | + |
|
| 1930 | + $dwBytesInRes = 40 + strlen($icXOR[$key]) + strlen($icAND[$key]); |
|
| 1931 | + $icondata .= phpthumb_functions::LittleEndian2String( |
|
| 1932 | + $dwBytesInRes, |
|
| 1933 | + 4 |
|
| 1934 | + ); // dwBytesInRes; // How many bytes in this resource? |
|
| 1935 | + |
|
| 1936 | + $icondata .= phpthumb_functions::LittleEndian2String( |
|
| 1937 | + $dwImageOffset, |
|
| 1938 | + 4 |
|
| 1939 | + ); // dwImageOffset; // Where in the file is this image? |
|
| 1940 | + $dwImageOffset += strlen($BitmapInfoHeader[$key]); |
|
| 1941 | + $dwImageOffset += strlen($icXOR[$key]); |
|
| 1942 | + $dwImageOffset += strlen($icAND[$key]); |
|
| 1943 | + } |
|
| 1944 | + |
|
| 1945 | + foreach (array_keys($gd_image_array) as $key) { |
|
| 1946 | + $icondata .= $BitmapInfoHeader[$key]; |
|
| 1947 | + $icondata .= $icXOR[$key]; |
|
| 1948 | + $icondata .= $icAND[$key]; |
|
| 1949 | + } |
|
| 1950 | + |
|
| 1951 | + return $icondata; |
|
| 1952 | + } |
|
| 1953 | 1953 | } |
@@ -42,13 +42,13 @@ discard block |
||
| 42 | 42 | $blue = dechex($blue); |
| 43 | 43 | |
| 44 | 44 | if (strlen($red) == 1) { |
| 45 | - $red = '0' . $red; |
|
| 45 | + $red = '0'.$red; |
|
| 46 | 46 | } |
| 47 | 47 | if (strlen($green) == 1) { |
| 48 | - $green = '0' . $green; |
|
| 48 | + $green = '0'.$green; |
|
| 49 | 49 | } |
| 50 | 50 | if (strlen($blue) == 1) { |
| 51 | - $blue = '0' . $blue; |
|
| 51 | + $blue = '0'.$blue; |
|
| 52 | 52 | } |
| 53 | 53 | |
| 54 | 54 | return "$red$green$blue"; |
@@ -67,7 +67,7 @@ discard block |
||
| 67 | 67 | $couleur = couleur_html_to_hex($couleur); |
| 68 | 68 | $couleur = ltrim($couleur, '#'); |
| 69 | 69 | if (strlen($couleur) === 3) { |
| 70 | - $couleur = $couleur[0] . $couleur[0] . $couleur[1] . $couleur[1] . $couleur[2] . $couleur[2]; |
|
| 70 | + $couleur = $couleur[0].$couleur[0].$couleur[1].$couleur[1].$couleur[2].$couleur[2]; |
|
| 71 | 71 | } |
| 72 | 72 | $retour = []; |
| 73 | 73 | $retour['red'] = hexdec(substr($couleur, 0, 2)); |
@@ -125,9 +125,9 @@ discard block |
||
| 125 | 125 | $var_G = ($G / 255); |
| 126 | 126 | $var_B = ($B / 255); |
| 127 | 127 | |
| 128 | - $var_Min = min($var_R, $var_G, $var_B); //Min. value of RGB |
|
| 129 | - $var_Max = max($var_R, $var_G, $var_B); //Max. value of RGB |
|
| 130 | - $del_Max = $var_Max - $var_Min; //Delta RGB value |
|
| 128 | + $var_Min = min($var_R, $var_G, $var_B); //Min. value of RGB |
|
| 129 | + $var_Max = max($var_R, $var_G, $var_B); //Max. value of RGB |
|
| 130 | + $del_Max = $var_Max - $var_Min; //Delta RGB value |
|
| 131 | 131 | |
| 132 | 132 | $L = ($var_Max + $var_Min) / 2; |
| 133 | 133 | |
@@ -185,7 +185,7 @@ discard block |
||
| 185 | 185 | */ |
| 186 | 186 | function _couleur_hsl_to_rgb($H, $S, $L) { |
| 187 | 187 | // helper |
| 188 | - $hue_2_rgb = function ($v1, $v2, $vH) { |
|
| 188 | + $hue_2_rgb = function($v1, $v2, $vH) { |
|
| 189 | 189 | if ($vH < 0) { |
| 190 | 190 | $vH += 1; |
| 191 | 191 | } |
@@ -319,11 +319,11 @@ discard block |
||
| 319 | 319 | } |
| 320 | 320 | } elseif ( |
| 321 | 321 | preg_match('@^data:image/([^;]*);base64,(.*)$@isS', $source, $regs) |
| 322 | - && ($extension = _image_trouver_extension_depuis_mime('image/' . $regs[1])) |
|
| 322 | + && ($extension = _image_trouver_extension_depuis_mime('image/'.$regs[1])) |
|
| 323 | 323 | && in_array($extension, _image_extensions_acceptees_en_entree()) |
| 324 | 324 | ) { |
| 325 | 325 | # gerer img src="data:....base64" |
| 326 | - $local = sous_repertoire(_DIR_VAR, 'image-data') . md5($regs[2]) . '.' . _image_extension_normalisee($extension); |
|
| 326 | + $local = sous_repertoire(_DIR_VAR, 'image-data').md5($regs[2]).'.'._image_extension_normalisee($extension); |
|
| 327 | 327 | if (!file_exists($local)) { |
| 328 | 328 | ecrire_fichier($local, base64_decode($regs[2])); |
| 329 | 329 | } |
@@ -340,7 +340,7 @@ discard block |
||
| 340 | 340 | // les protocoles web prennent au moins 3 lettres |
| 341 | 341 | if (tester_url_absolue($source)) { |
| 342 | 342 | include_spip('inc/distant'); |
| 343 | - $fichier = _DIR_RACINE . copie_locale($source); |
|
| 343 | + $fichier = _DIR_RACINE.copie_locale($source); |
|
| 344 | 344 | if (!$fichier) { |
| 345 | 345 | return ''; |
| 346 | 346 | } |
@@ -442,9 +442,9 @@ discard block |
||
| 442 | 442 | // on garde la terminaison initiale car image simplement copiee |
| 443 | 443 | // et on postfixe son nom avec un md5 du path |
| 444 | 444 | $terminaison_dest = $terminaison; |
| 445 | - $fichier_dest .= '-' . substr(md5("$identifiant"), 0, 5); |
|
| 445 | + $fichier_dest .= '-'.substr(md5("$identifiant"), 0, 5); |
|
| 446 | 446 | } else { |
| 447 | - $fichier_dest .= '-' . substr(md5("$identifiant-$effet"), 0, 5); |
|
| 447 | + $fichier_dest .= '-'.substr(md5("$identifiant-$effet"), 0, 5); |
|
| 448 | 448 | } |
| 449 | 449 | $cache = sous_repertoire(_DIR_VAR, $cache); |
| 450 | 450 | $cache = sous_repertoire($cache, $effet); |
@@ -455,7 +455,7 @@ discard block |
||
| 455 | 455 | $fichier_dest = substr($fichier_dest, 2); |
| 456 | 456 | } |
| 457 | 457 | |
| 458 | - $fichier_dest = $cache . $fichier_dest . '.' . $terminaison_dest; |
|
| 458 | + $fichier_dest = $cache.$fichier_dest.'.'.$terminaison_dest; |
|
| 459 | 459 | |
| 460 | 460 | $GLOBALS['images_calculees'][] = $fichier_dest; |
| 461 | 461 | |
@@ -491,14 +491,14 @@ discard block |
||
| 491 | 491 | |
| 492 | 492 | if ($creer) { |
| 493 | 493 | spip_logger('images')->error( |
| 494 | - 'filtre image ' . ($fonction_creation ? reset($fonction_creation) : '') . "[$effet] sur $fichier", |
|
| 494 | + 'filtre image '.($fonction_creation ? reset($fonction_creation) : '')."[$effet] sur $fichier", |
|
| 495 | 495 | ); |
| 496 | 496 | } |
| 497 | 497 | |
| 498 | 498 | $term_fonction = _image_trouver_extension_pertinente($fichier); |
| 499 | - $ret['fonction_imagecreatefrom'] = '_imagecreatefrom' . $term_fonction; |
|
| 499 | + $ret['fonction_imagecreatefrom'] = '_imagecreatefrom'.$term_fonction; |
|
| 500 | 500 | $ret['fichier'] = $fichier; |
| 501 | - $ret['fonction_image'] = '_image_image' . $terminaison_dest; |
|
| 501 | + $ret['fonction_image'] = '_image_image'.$terminaison_dest; |
|
| 502 | 502 | $ret['fichier_dest'] = $fichier_dest; |
| 503 | 503 | $ret['format_source'] = _image_extension_normalisee($terminaison); |
| 504 | 504 | $ret['format_dest'] = $terminaison_dest; |
@@ -775,7 +775,7 @@ discard block |
||
| 775 | 775 | if (!function_exists('imagepng')) { |
| 776 | 776 | return false; |
| 777 | 777 | } |
| 778 | - $tmp = $fichier . '.tmp'; |
|
| 778 | + $tmp = $fichier.'.tmp'; |
|
| 779 | 779 | $ret = imagepng($img, $tmp); |
| 780 | 780 | if (file_exists($tmp)) { |
| 781 | 781 | $taille_test = @getimagesize($tmp); |
@@ -810,7 +810,7 @@ discard block |
||
| 810 | 810 | if (!function_exists('imagegif')) { |
| 811 | 811 | return false; |
| 812 | 812 | } |
| 813 | - $tmp = $fichier . '.tmp'; |
|
| 813 | + $tmp = $fichier.'.tmp'; |
|
| 814 | 814 | $ret = imagegif($img, $tmp); |
| 815 | 815 | if (file_exists($tmp)) { |
| 816 | 816 | $taille_test = @getimagesize($tmp); |
@@ -850,7 +850,7 @@ discard block |
||
| 850 | 850 | if (!function_exists('imagejpeg')) { |
| 851 | 851 | return false; |
| 852 | 852 | } |
| 853 | - $tmp = $fichier . '.tmp'; |
|
| 853 | + $tmp = $fichier.'.tmp'; |
|
| 854 | 854 | |
| 855 | 855 | // Enable interlancing |
| 856 | 856 | imageinterlace($img, true); |
@@ -911,7 +911,7 @@ discard block |
||
| 911 | 911 | if (!function_exists('imagewebp')) { |
| 912 | 912 | return false; |
| 913 | 913 | } |
| 914 | - $tmp = $fichier . '.tmp'; |
|
| 914 | + $tmp = $fichier.'.tmp'; |
|
| 915 | 915 | $ret = imagewebp($img, $tmp, $qualite); |
| 916 | 916 | if (file_exists($tmp)) { |
| 917 | 917 | $taille_test = @getimagesize($tmp); |
@@ -945,7 +945,7 @@ discard block |
||
| 945 | 945 | */ |
| 946 | 946 | function _image_imagesvg($img, $fichier) { |
| 947 | 947 | |
| 948 | - $tmp = $fichier . '.tmp'; |
|
| 948 | + $tmp = $fichier.'.tmp'; |
|
| 949 | 949 | if (!str_contains($img, '<')) { |
| 950 | 950 | $img = supprimer_timestamp($img); |
| 951 | 951 | if (!file_exists($img)) { |
@@ -1002,14 +1002,14 @@ discard block |
||
| 1002 | 1002 | */ |
| 1003 | 1003 | function _image_gd_output($img, $valeurs, $qualite = _IMG_GD_QUALITE, $fonction = null) { |
| 1004 | 1004 | if (is_null($fonction)) { |
| 1005 | - $fonction = '_image_image' . $valeurs['format_dest']; |
|
| 1005 | + $fonction = '_image_image'.$valeurs['format_dest']; |
|
| 1006 | 1006 | } |
| 1007 | 1007 | $ret = false; |
| 1008 | 1008 | #un flag pour reperer les images gravees |
| 1009 | 1009 | $lock = ( |
| 1010 | 1010 | !statut_effacer_images_temporaires('get') |
| 1011 | 1011 | || @file_exists($valeurs['fichier_dest']) |
| 1012 | - && !@file_exists($valeurs['fichier_dest'] . '.src') |
|
| 1012 | + && !@file_exists($valeurs['fichier_dest'].'.src') |
|
| 1013 | 1013 | ); |
| 1014 | 1014 | if ( |
| 1015 | 1015 | function_exists($fonction) |
@@ -1021,7 +1021,7 @@ discard block |
||
| 1021 | 1021 | [$valeurs['hauteur_dest'], $valeurs['largeur_dest']] = taille_image($valeurs['fichier_dest']); |
| 1022 | 1022 | $valeurs['date'] = @filemtime($valeurs['fichier_dest']); |
| 1023 | 1023 | // pour la retrouver apres disparition |
| 1024 | - ecrire_fichier($valeurs['fichier_dest'] . '.src', serialize($valeurs), true); |
|
| 1024 | + ecrire_fichier($valeurs['fichier_dest'].'.src', serialize($valeurs), true); |
|
| 1025 | 1025 | } |
| 1026 | 1026 | |
| 1027 | 1027 | return $ret; |
@@ -1196,7 +1196,7 @@ discard block |
||
| 1196 | 1196 | |
| 1197 | 1197 | // attributs deprecies. Transformer en CSS |
| 1198 | 1198 | if ($espace = extraire_attribut($tag, 'hspace')) { |
| 1199 | - $style = "margin:{$espace}px;" . $style; |
|
| 1199 | + $style = "margin:{$espace}px;".$style; |
|
| 1200 | 1200 | $tag = inserer_attribut($tag, 'hspace', ''); |
| 1201 | 1201 | } |
| 1202 | 1202 | |
@@ -1319,7 +1319,7 @@ discard block |
||
| 1319 | 1319 | $image = $valeurs['fichier']; |
| 1320 | 1320 | $format = $valeurs['format_source']; |
| 1321 | 1321 | $destdir = dirname((string) $valeurs['fichier_dest']); |
| 1322 | - $destfile = basename((string) $valeurs['fichier_dest'], '.' . $valeurs['format_dest']); |
|
| 1322 | + $destfile = basename((string) $valeurs['fichier_dest'], '.'.$valeurs['format_dest']); |
|
| 1323 | 1323 | |
| 1324 | 1324 | $format_sortie = $valeurs['format_dest']; |
| 1325 | 1325 | |
@@ -1351,7 +1351,7 @@ discard block |
||
| 1351 | 1351 | |
| 1352 | 1352 | // Si l'image est de la taille demandee (ou plus petite), simplement la retourner |
| 1353 | 1353 | if ($srcWidth && $srcWidth <= $maxWidth && $srcHeight <= $maxHeight) { |
| 1354 | - $vignette = $destination . '.' . $format; |
|
| 1354 | + $vignette = $destination.'.'.$format; |
|
| 1355 | 1355 | @copy($image, $vignette); |
| 1356 | 1356 | } |
| 1357 | 1357 | |
@@ -1359,7 +1359,7 @@ discard block |
||
| 1359 | 1359 | include_spip('inc/svg'); |
| 1360 | 1360 | if ($svg = svg_redimensionner($valeurs['fichier'], $destWidth, $destHeight)) { |
| 1361 | 1361 | $format_sortie = 'svg'; |
| 1362 | - $vignette = $destination . '.' . $format_sortie; |
|
| 1362 | + $vignette = $destination.'.'.$format_sortie; |
|
| 1363 | 1363 | $valeurs['fichier_dest'] = $vignette; |
| 1364 | 1364 | _image_gd_output($svg, $valeurs); |
| 1365 | 1365 | } |
@@ -1371,9 +1371,9 @@ discard block |
||
| 1371 | 1371 | define('_CONVERT_COMMAND', 'convert'); |
| 1372 | 1372 | } // Securite : mes_options.php peut preciser le chemin absolu |
| 1373 | 1373 | if (!defined('_RESIZE_COMMAND')) { |
| 1374 | - define('_RESIZE_COMMAND', _CONVERT_COMMAND . ' -quality ' . _IMG_CONVERT_QUALITE . ' -orient Undefined -resize %xx%y! %src %dest'); |
|
| 1374 | + define('_RESIZE_COMMAND', _CONVERT_COMMAND.' -quality '._IMG_CONVERT_QUALITE.' -orient Undefined -resize %xx%y! %src %dest'); |
|
| 1375 | 1375 | } |
| 1376 | - $vignette = $destination . '.' . $format_sortie; |
|
| 1376 | + $vignette = $destination.'.'.$format_sortie; |
|
| 1377 | 1377 | $commande = str_replace( |
| 1378 | 1378 | ['%x', '%y', '%src', '%dest'], |
| 1379 | 1379 | [ |
@@ -1389,7 +1389,7 @@ discard block |
||
| 1389 | 1389 | if (!@file_exists($vignette)) { |
| 1390 | 1390 | spip_logger('images')->info("echec convert sur $vignette"); |
| 1391 | 1391 | |
| 1392 | - return; // echec commande |
|
| 1392 | + return; // echec commande |
|
| 1393 | 1393 | } |
| 1394 | 1394 | } |
| 1395 | 1395 | |
@@ -1406,7 +1406,7 @@ discard block |
||
| 1406 | 1406 | if (!$output) { |
| 1407 | 1407 | return; |
| 1408 | 1408 | } |
| 1409 | - $vignette = $output . DIRECTORY_SEPARATOR . basename($destination) . '.' . $format_sortie; |
|
| 1409 | + $vignette = $output.DIRECTORY_SEPARATOR.basename($destination).'.'.$format_sortie; |
|
| 1410 | 1410 | |
| 1411 | 1411 | $imagick = new Imagick(); |
| 1412 | 1412 | $imagick->readImage(realpath($image)); |
@@ -1415,7 +1415,7 @@ discard block |
||
| 1415 | 1415 | $destHeight, |
| 1416 | 1416 | Imagick::FILTER_LANCZOS, |
| 1417 | 1417 | 1 |
| 1418 | - );//, IMAGICK_FILTER_LANCZOS, _IMG_IMAGICK_QUALITE / 100); |
|
| 1418 | + ); //, IMAGICK_FILTER_LANCZOS, _IMG_IMAGICK_QUALITE / 100); |
|
| 1419 | 1419 | $imagick->writeImage($vignette); |
| 1420 | 1420 | |
| 1421 | 1421 | if (!@file_exists($vignette)) { |
@@ -1424,7 +1424,7 @@ discard block |
||
| 1424 | 1424 | return; |
| 1425 | 1425 | } |
| 1426 | 1426 | // remettre le chemin relatif car c'est ce qu'attend SPIP pour la suite (en particlier action/tester) |
| 1427 | - $vignette = $destination . '.' . $format_sortie; |
|
| 1427 | + $vignette = $destination.'.'.$format_sortie; |
|
| 1428 | 1428 | } |
| 1429 | 1429 | |
| 1430 | 1430 | // netpbm |
@@ -1435,11 +1435,11 @@ discard block |
||
| 1435 | 1435 | if (_PNMSCALE_COMMAND == '') { |
| 1436 | 1436 | return; |
| 1437 | 1437 | } |
| 1438 | - $vignette = $destination . '.' . $format_sortie; |
|
| 1438 | + $vignette = $destination.'.'.$format_sortie; |
|
| 1439 | 1439 | $pnmtojpeg_command = str_replace('pnmscale', 'pnmtojpeg', (string) _PNMSCALE_COMMAND); |
| 1440 | 1440 | if ($format == 'jpg') { |
| 1441 | 1441 | $jpegtopnm_command = str_replace('pnmscale', 'jpegtopnm', (string) _PNMSCALE_COMMAND); |
| 1442 | - exec("$jpegtopnm_command $image | " . _PNMSCALE_COMMAND . " -width $destWidth | $pnmtojpeg_command > $vignette"); |
|
| 1442 | + exec("$jpegtopnm_command $image | "._PNMSCALE_COMMAND." -width $destWidth | $pnmtojpeg_command > $vignette"); |
|
| 1443 | 1443 | if (!($s = @filesize($vignette))) { |
| 1444 | 1444 | spip_unlink($vignette); |
| 1445 | 1445 | } |
@@ -1451,7 +1451,7 @@ discard block |
||
| 1451 | 1451 | } else { |
| 1452 | 1452 | if ($format == 'gif') { |
| 1453 | 1453 | $giftopnm_command = str_replace('pnmscale', 'giftopnm', (string) _PNMSCALE_COMMAND); |
| 1454 | - exec("$giftopnm_command $image | " . _PNMSCALE_COMMAND . " -width $destWidth | $pnmtojpeg_command > $vignette"); |
|
| 1454 | + exec("$giftopnm_command $image | "._PNMSCALE_COMMAND." -width $destWidth | $pnmtojpeg_command > $vignette"); |
|
| 1455 | 1455 | if (!($s = @filesize($vignette))) { |
| 1456 | 1456 | spip_unlink($vignette); |
| 1457 | 1457 | } |
@@ -1463,7 +1463,7 @@ discard block |
||
| 1463 | 1463 | } else { |
| 1464 | 1464 | if ($format == 'png') { |
| 1465 | 1465 | $pngtopnm_command = str_replace('pnmscale', 'pngtopnm', (string) _PNMSCALE_COMMAND); |
| 1466 | - exec("$pngtopnm_command $image | " . _PNMSCALE_COMMAND . " -width $destWidth | $pnmtojpeg_command > $vignette"); |
|
| 1466 | + exec("$pngtopnm_command $image | "._PNMSCALE_COMMAND." -width $destWidth | $pnmtojpeg_command > $vignette"); |
|
| 1467 | 1467 | if (!($s = @filesize($vignette))) { |
| 1468 | 1468 | spip_unlink($vignette); |
| 1469 | 1469 | } |
@@ -1485,7 +1485,7 @@ discard block |
||
| 1485 | 1485 | return; |
| 1486 | 1486 | } |
| 1487 | 1487 | if (_IMG_GD_MAX_PIXELS && $srcWidth * $srcHeight > _IMG_GD_MAX_PIXELS) { |
| 1488 | - spip_logger('images')->info('vignette gd2 impossible : ' . $srcWidth * $srcHeight . 'pixels'); |
|
| 1488 | + spip_logger('images')->info('vignette gd2 impossible : '.$srcWidth * $srcHeight.'pixels'); |
|
| 1489 | 1489 | |
| 1490 | 1490 | return; |
| 1491 | 1491 | } |
@@ -1788,7 +1788,7 @@ discard block |
||
| 1788 | 1788 | // de l'image, de facon a tromper le cache du navigateur |
| 1789 | 1789 | // quand on fait supprimer/reuploader un logo |
| 1790 | 1790 | // (pas de filemtime si SAFE MODE) |
| 1791 | - $date = test_espace_prive() ? ('?' . $date) : ''; |
|
| 1791 | + $date = test_espace_prive() ? ('?'.$date) : ''; |
|
| 1792 | 1792 | |
| 1793 | 1793 | return _image_ecrire_tag($image, ['src' => "$logo$date", 'width' => $destWidth, 'height' => $destHeight]); |
| 1794 | 1794 | } |
@@ -1866,9 +1866,9 @@ discard block |
||
| 1866 | 1866 | $b = $argb['blue']; |
| 1867 | 1867 | |
| 1868 | 1868 | if ($bpp[$key] == 32) { |
| 1869 | - $icXOR[$key] .= chr($b) . chr($g) . chr($r) . chr($a); |
|
| 1869 | + $icXOR[$key] .= chr($b).chr($g).chr($r).chr($a); |
|
| 1870 | 1870 | } elseif ($bpp[$key] === 24) { |
| 1871 | - $icXOR[$key] .= chr($b) . chr($g) . chr($r); |
|
| 1871 | + $icXOR[$key] .= chr($b).chr($g).chr($r); |
|
| 1872 | 1872 | } |
| 1873 | 1873 | |
| 1874 | 1874 | if ($a < 128) { |
@@ -1895,48 +1895,48 @@ discard block |
||
| 1895 | 1895 | |
| 1896 | 1896 | // BITMAPINFOHEADER - 40 bytes |
| 1897 | 1897 | $BitmapInfoHeader[$key] = ''; |
| 1898 | - $BitmapInfoHeader[$key] .= "\x28\x00\x00\x00"; // DWORD biSize; |
|
| 1899 | - $BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($ImageWidths[$key], 4); // LONG biWidth; |
|
| 1898 | + $BitmapInfoHeader[$key] .= "\x28\x00\x00\x00"; // DWORD biSize; |
|
| 1899 | + $BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($ImageWidths[$key], 4); // LONG biWidth; |
|
| 1900 | 1900 | // The biHeight member specifies the combined |
| 1901 | 1901 | // height of the XOR and AND masks. |
| 1902 | 1902 | $BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($ImageHeights[$key] * 2, 4); // LONG biHeight; |
| 1903 | - $BitmapInfoHeader[$key] .= "\x01\x00"; // WORD biPlanes; |
|
| 1904 | - $BitmapInfoHeader[$key] .= chr($bpp[$key]) . "\x00"; // wBitCount; |
|
| 1905 | - $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00"; // DWORD biCompression; |
|
| 1906 | - $BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($biSizeImage, 4); // DWORD biSizeImage; |
|
| 1907 | - $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00"; // LONG biXPelsPerMeter; |
|
| 1908 | - $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00"; // LONG biYPelsPerMeter; |
|
| 1909 | - $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00"; // DWORD biClrUsed; |
|
| 1910 | - $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00"; // DWORD biClrImportant; |
|
| 1903 | + $BitmapInfoHeader[$key] .= "\x01\x00"; // WORD biPlanes; |
|
| 1904 | + $BitmapInfoHeader[$key] .= chr($bpp[$key])."\x00"; // wBitCount; |
|
| 1905 | + $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00"; // DWORD biCompression; |
|
| 1906 | + $BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($biSizeImage, 4); // DWORD biSizeImage; |
|
| 1907 | + $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00"; // LONG biXPelsPerMeter; |
|
| 1908 | + $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00"; // LONG biYPelsPerMeter; |
|
| 1909 | + $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00"; // DWORD biClrUsed; |
|
| 1910 | + $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00"; // DWORD biClrImportant; |
|
| 1911 | 1911 | } |
| 1912 | 1912 | |
| 1913 | 1913 | |
| 1914 | - $icondata = "\x00\x00"; // idReserved; // Reserved (must be 0) |
|
| 1915 | - $icondata .= "\x01\x00"; // idType; // Resource Type (1 for icons) |
|
| 1916 | - $icondata .= phpthumb_functions::LittleEndian2String(count($gd_image_array), 2); // idCount; // How many images? |
|
| 1914 | + $icondata = "\x00\x00"; // idReserved; // Reserved (must be 0) |
|
| 1915 | + $icondata .= "\x01\x00"; // idType; // Resource Type (1 for icons) |
|
| 1916 | + $icondata .= phpthumb_functions::LittleEndian2String(count($gd_image_array), 2); // idCount; // How many images? |
|
| 1917 | 1917 | |
| 1918 | 1918 | $dwImageOffset = 6 + (count($gd_image_array) * 16); |
| 1919 | 1919 | foreach (array_keys($gd_image_array) as $key) { |
| 1920 | 1920 | // ICONDIRENTRY idEntries[1]; // An entry for each image (idCount of 'em) |
| 1921 | 1921 | |
| 1922 | - $icondata .= chr($ImageWidths[$key]); // bWidth; // Width, in pixels, of the image |
|
| 1923 | - $icondata .= chr($ImageHeights[$key]); // bHeight; // Height, in pixels, of the image |
|
| 1924 | - $icondata .= chr($totalcolors[$key]); // bColorCount; // Number of colors in image (0 if >=8bpp) |
|
| 1925 | - $icondata .= "\x00"; // bReserved; // Reserved ( must be 0) |
|
| 1922 | + $icondata .= chr($ImageWidths[$key]); // bWidth; // Width, in pixels, of the image |
|
| 1923 | + $icondata .= chr($ImageHeights[$key]); // bHeight; // Height, in pixels, of the image |
|
| 1924 | + $icondata .= chr($totalcolors[$key]); // bColorCount; // Number of colors in image (0 if >=8bpp) |
|
| 1925 | + $icondata .= "\x00"; // bReserved; // Reserved ( must be 0) |
|
| 1926 | 1926 | |
| 1927 | - $icondata .= "\x01\x00"; // wPlanes; // Color Planes |
|
| 1928 | - $icondata .= chr($bpp[$key]) . "\x00"; // wBitCount; // Bits per pixel |
|
| 1927 | + $icondata .= "\x01\x00"; // wPlanes; // Color Planes |
|
| 1928 | + $icondata .= chr($bpp[$key])."\x00"; // wBitCount; // Bits per pixel |
|
| 1929 | 1929 | |
| 1930 | 1930 | $dwBytesInRes = 40 + strlen($icXOR[$key]) + strlen($icAND[$key]); |
| 1931 | 1931 | $icondata .= phpthumb_functions::LittleEndian2String( |
| 1932 | 1932 | $dwBytesInRes, |
| 1933 | 1933 | 4 |
| 1934 | - ); // dwBytesInRes; // How many bytes in this resource? |
|
| 1934 | + ); // dwBytesInRes; // How many bytes in this resource? |
|
| 1935 | 1935 | |
| 1936 | 1936 | $icondata .= phpthumb_functions::LittleEndian2String( |
| 1937 | 1937 | $dwImageOffset, |
| 1938 | 1938 | 4 |
| 1939 | - ); // dwImageOffset; // Where in the file is this image? |
|
| 1939 | + ); // dwImageOffset; // Where in the file is this image? |
|
| 1940 | 1940 | $dwImageOffset += strlen($BitmapInfoHeader[$key]); |
| 1941 | 1941 | $dwImageOffset += strlen($icXOR[$key]); |
| 1942 | 1942 | $dwImageOffset += strlen($icAND[$key]); |
@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | **/ |
| 17 | 17 | |
| 18 | 18 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 19 | - return; |
|
| 19 | + return; |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | /** |
@@ -44,29 +44,29 @@ discard block |
||
| 44 | 44 | * sinon code HTML de la page après le traitement effectué. |
| 45 | 45 | **/ |
| 46 | 46 | function inc_admin_dist($script, $titre, $comment = '', $anonymous = false) { |
| 47 | - $reprise = true; |
|
| 48 | - if ( |
|
| 49 | - !isset($GLOBALS['meta'][$script]) |
|
| 50 | - || !isset($GLOBALS['meta']['admin']) |
|
| 51 | - ) { |
|
| 52 | - $reprise = false; |
|
| 53 | - $res = debut_admin($script, $titre, $comment); |
|
| 54 | - if ($res) { |
|
| 55 | - return $res; |
|
| 56 | - } |
|
| 57 | - spip_logger()->info("meta: $script " . print_r($_POST, true)); |
|
| 58 | - ecrire_meta($script, serialize($_POST)); |
|
| 59 | - } |
|
| 47 | + $reprise = true; |
|
| 48 | + if ( |
|
| 49 | + !isset($GLOBALS['meta'][$script]) |
|
| 50 | + || !isset($GLOBALS['meta']['admin']) |
|
| 51 | + ) { |
|
| 52 | + $reprise = false; |
|
| 53 | + $res = debut_admin($script, $titre, $comment); |
|
| 54 | + if ($res) { |
|
| 55 | + return $res; |
|
| 56 | + } |
|
| 57 | + spip_logger()->info("meta: $script " . print_r($_POST, true)); |
|
| 58 | + ecrire_meta($script, serialize($_POST)); |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - $res = admin_verifie_session($script, $anonymous); |
|
| 62 | - if ($res) { |
|
| 63 | - return $res; |
|
| 64 | - } |
|
| 65 | - $base = charger_fonction($script, 'base'); |
|
| 66 | - $base($titre, $reprise); |
|
| 67 | - fin_admin($script); |
|
| 61 | + $res = admin_verifie_session($script, $anonymous); |
|
| 62 | + if ($res) { |
|
| 63 | + return $res; |
|
| 64 | + } |
|
| 65 | + $base = charger_fonction($script, 'base'); |
|
| 66 | + $base($titre, $reprise); |
|
| 67 | + fin_admin($script); |
|
| 68 | 68 | |
| 69 | - return ''; |
|
| 69 | + return ''; |
|
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | /** |
@@ -97,36 +97,36 @@ discard block |
||
| 97 | 97 | * Code HTML si message d'erreur, '' sinon; |
| 98 | 98 | */ |
| 99 | 99 | function admin_verifie_session($script, $anonymous = false) { |
| 100 | - include_spip('base/abstract_sql'); |
|
| 101 | - $pref = sprintf('_%d_', $GLOBALS['visiteur_session']['id_auteur']); |
|
| 102 | - $signal = fichier_admin($script, "$script$pref"); |
|
| 103 | - $valeur = sql_getfetsel('valeur', 'spip_meta', "nom='admin'"); |
|
| 104 | - if ($valeur === null) { |
|
| 105 | - ecrire_meta('admin', $signal, 'non'); |
|
| 106 | - } else { |
|
| 107 | - if ( |
|
| 108 | - !$anonymous |
|
| 109 | - && $valeur != $signal |
|
| 110 | - && ( |
|
| 111 | - !preg_match('/^(.*)_(\d+)_/', (string) $GLOBALS['meta']['admin'], $l) |
|
| 112 | - || (int) $l[2] != $GLOBALS['visiteur_session']['id_auteur'] |
|
| 113 | - ) |
|
| 114 | - ) { |
|
| 115 | - include_spip('inc/minipres'); |
|
| 116 | - spip_logger()->info("refus de lancer $script, priorite a $valeur"); |
|
| 117 | - return minipres(_T('info_travaux_texte'), '', ['status' => 503]); |
|
| 118 | - } |
|
| 119 | - } |
|
| 120 | - $journal = 'spip'; |
|
| 121 | - if (autoriser('configurer')) { |
|
| 122 | - // c'est une action webmestre, soit par ftp soit par statut webmestre |
|
| 123 | - $journal = 'webmestre'; |
|
| 124 | - } |
|
| 125 | - // on pourrait statuer automatiquement les webmestres a l'init d'une action auth par ftp ... ? |
|
| 100 | + include_spip('base/abstract_sql'); |
|
| 101 | + $pref = sprintf('_%d_', $GLOBALS['visiteur_session']['id_auteur']); |
|
| 102 | + $signal = fichier_admin($script, "$script$pref"); |
|
| 103 | + $valeur = sql_getfetsel('valeur', 'spip_meta', "nom='admin'"); |
|
| 104 | + if ($valeur === null) { |
|
| 105 | + ecrire_meta('admin', $signal, 'non'); |
|
| 106 | + } else { |
|
| 107 | + if ( |
|
| 108 | + !$anonymous |
|
| 109 | + && $valeur != $signal |
|
| 110 | + && ( |
|
| 111 | + !preg_match('/^(.*)_(\d+)_/', (string) $GLOBALS['meta']['admin'], $l) |
|
| 112 | + || (int) $l[2] != $GLOBALS['visiteur_session']['id_auteur'] |
|
| 113 | + ) |
|
| 114 | + ) { |
|
| 115 | + include_spip('inc/minipres'); |
|
| 116 | + spip_logger()->info("refus de lancer $script, priorite a $valeur"); |
|
| 117 | + return minipres(_T('info_travaux_texte'), '', ['status' => 503]); |
|
| 118 | + } |
|
| 119 | + } |
|
| 120 | + $journal = 'spip'; |
|
| 121 | + if (autoriser('configurer')) { |
|
| 122 | + // c'est une action webmestre, soit par ftp soit par statut webmestre |
|
| 123 | + $journal = 'webmestre'; |
|
| 124 | + } |
|
| 125 | + // on pourrait statuer automatiquement les webmestres a l'init d'une action auth par ftp ... ? |
|
| 126 | 126 | |
| 127 | - spip_logger($journal)->info("admin $pref" . ($valeur ? ' (reprise)' : ' (init)')); |
|
| 127 | + spip_logger($journal)->info("admin $pref" . ($valeur ? ' (reprise)' : ' (init)')); |
|
| 128 | 128 | |
| 129 | - return ''; |
|
| 129 | + return ''; |
|
| 130 | 130 | } |
| 131 | 131 | |
| 132 | 132 | /** |
@@ -139,11 +139,11 @@ discard block |
||
| 139 | 139 | * Chemin du répertoire. |
| 140 | 140 | **/ |
| 141 | 141 | function dir_admin() { |
| 142 | - if (autoriser('configurer')) { |
|
| 143 | - return _DIR_TMP; |
|
| 144 | - } else { |
|
| 145 | - return _DIR_TRANSFERT . $GLOBALS['visiteur_session']['login'] . '/'; |
|
| 146 | - } |
|
| 142 | + if (autoriser('configurer')) { |
|
| 143 | + return _DIR_TMP; |
|
| 144 | + } else { |
|
| 145 | + return _DIR_TRANSFERT . $GLOBALS['visiteur_session']['login'] . '/'; |
|
| 146 | + } |
|
| 147 | 147 | } |
| 148 | 148 | |
| 149 | 149 | /** |
@@ -160,8 +160,8 @@ discard block |
||
| 160 | 160 | * Nom du fichier |
| 161 | 161 | **/ |
| 162 | 162 | function fichier_admin($action, $pref = 'admin_') { |
| 163 | - return $pref . |
|
| 164 | - substr(md5($action . (time() & ~2047) . $GLOBALS['visiteur_session']['login']), 0, 10); |
|
| 163 | + return $pref . |
|
| 164 | + substr(md5($action . (time() & ~2047) . $GLOBALS['visiteur_session']['login']), 0, 10); |
|
| 165 | 165 | } |
| 166 | 166 | |
| 167 | 167 | /** |
@@ -187,76 +187,76 @@ discard block |
||
| 187 | 187 | * sinon chaîne vide si déjà fait. |
| 188 | 188 | **/ |
| 189 | 189 | function debut_admin($script, $action = '', $corps = '') { |
| 190 | - if ( |
|
| 191 | - !$action |
|
| 192 | - || !autoriser('webmestre') && !autoriser('chargerftp') |
|
| 193 | - ) { |
|
| 194 | - include_spip('inc/minipres'); |
|
| 190 | + if ( |
|
| 191 | + !$action |
|
| 192 | + || !autoriser('webmestre') && !autoriser('chargerftp') |
|
| 193 | + ) { |
|
| 194 | + include_spip('inc/minipres'); |
|
| 195 | 195 | |
| 196 | - return minipres(); |
|
| 197 | - } else { |
|
| 198 | - $dir = dir_admin(); |
|
| 199 | - $signal = fichier_admin($script); |
|
| 200 | - if (@file_exists($dir . $signal)) { |
|
| 201 | - spip_logger()->info("Action admin: $action"); |
|
| 196 | + return minipres(); |
|
| 197 | + } else { |
|
| 198 | + $dir = dir_admin(); |
|
| 199 | + $signal = fichier_admin($script); |
|
| 200 | + if (@file_exists($dir . $signal)) { |
|
| 201 | + spip_logger()->info("Action admin: $action"); |
|
| 202 | 202 | |
| 203 | - return ''; |
|
| 204 | - } |
|
| 205 | - include_spip('inc/minipres'); |
|
| 203 | + return ''; |
|
| 204 | + } |
|
| 205 | + include_spip('inc/minipres'); |
|
| 206 | 206 | |
| 207 | - // Si on est un super-admin, un bouton de validation suffit |
|
| 208 | - // sauf dans les cas destroy |
|
| 209 | - if ( |
|
| 210 | - (autoriser('webmestre') || $script === 'repair') |
|
| 211 | - && $script != 'delete_all' |
|
| 212 | - ) { |
|
| 213 | - if (_request('validation_admin') == $signal) { |
|
| 214 | - spip_logger()->info("Action super-admin: $action"); |
|
| 207 | + // Si on est un super-admin, un bouton de validation suffit |
|
| 208 | + // sauf dans les cas destroy |
|
| 209 | + if ( |
|
| 210 | + (autoriser('webmestre') || $script === 'repair') |
|
| 211 | + && $script != 'delete_all' |
|
| 212 | + ) { |
|
| 213 | + if (_request('validation_admin') == $signal) { |
|
| 214 | + spip_logger()->info("Action super-admin: $action"); |
|
| 215 | 215 | |
| 216 | - return ''; |
|
| 217 | - } |
|
| 218 | - $corps .= '<input type="hidden" name="validation_admin" value="' . $signal . '" />'; |
|
| 219 | - $suivant = _T('bouton_valider'); |
|
| 220 | - $js = ''; |
|
| 221 | - } else { |
|
| 222 | - // cet appel permet d'assurer un copier-coller du nom du repertoire a creer dans tmp (esj) |
|
| 223 | - // l'insertion du script a cet endroit n'est pas xhtml licite |
|
| 224 | - // mais evite de l'embarquer dans toutes les pages minipres |
|
| 225 | - $corps .= http_script('', 'spip_barre.js'); |
|
| 216 | + return ''; |
|
| 217 | + } |
|
| 218 | + $corps .= '<input type="hidden" name="validation_admin" value="' . $signal . '" />'; |
|
| 219 | + $suivant = _T('bouton_valider'); |
|
| 220 | + $js = ''; |
|
| 221 | + } else { |
|
| 222 | + // cet appel permet d'assurer un copier-coller du nom du repertoire a creer dans tmp (esj) |
|
| 223 | + // l'insertion du script a cet endroit n'est pas xhtml licite |
|
| 224 | + // mais evite de l'embarquer dans toutes les pages minipres |
|
| 225 | + $corps .= http_script('', 'spip_barre.js'); |
|
| 226 | 226 | |
| 227 | - $corps .= '<fieldset><legend>' |
|
| 228 | - . _T('info_authentification_ftp') |
|
| 229 | - . aider('ftp_auth') |
|
| 230 | - . "</legend>\n<label for='fichier'>" |
|
| 231 | - . _T('info_creer_repertoire') |
|
| 232 | - . "</label>\n" |
|
| 233 | - . "<span id='signal' class='formo'>" . $signal . '</span>' |
|
| 234 | - . "<input type='hidden' id='fichier' name='fichier' value='" |
|
| 235 | - . $signal |
|
| 236 | - . "' />" |
|
| 237 | - . _T('info_creer_repertoire_2', ['repertoire' => joli_repertoire($dir)]) |
|
| 238 | - . '</fieldset>'; |
|
| 227 | + $corps .= '<fieldset><legend>' |
|
| 228 | + . _T('info_authentification_ftp') |
|
| 229 | + . aider('ftp_auth') |
|
| 230 | + . "</legend>\n<label for='fichier'>" |
|
| 231 | + . _T('info_creer_repertoire') |
|
| 232 | + . "</label>\n" |
|
| 233 | + . "<span id='signal' class='formo'>" . $signal . '</span>' |
|
| 234 | + . "<input type='hidden' id='fichier' name='fichier' value='" |
|
| 235 | + . $signal |
|
| 236 | + . "' />" |
|
| 237 | + . _T('info_creer_repertoire_2', ['repertoire' => joli_repertoire($dir)]) |
|
| 238 | + . '</fieldset>'; |
|
| 239 | 239 | |
| 240 | - $suivant = _T('bouton_recharger_page'); |
|
| 240 | + $suivant = _T('bouton_recharger_page'); |
|
| 241 | 241 | |
| 242 | - // code volontairement tordu: |
|
| 243 | - // provoquer la copie dans le presse papier du nom du repertoire |
|
| 244 | - // en remettant a vide le champ pour que ca marche aussi en cas |
|
| 245 | - // de JavaScript inactif. |
|
| 246 | - $js = " onload='var range=document.createRange(); var signal = document.getElementById(\"signal\"); var userSelection = window.getSelection(); range.setStart(signal,0); range.setEnd(signal,1); userSelection.addRange(range);'"; |
|
| 247 | - } |
|
| 242 | + // code volontairement tordu: |
|
| 243 | + // provoquer la copie dans le presse papier du nom du repertoire |
|
| 244 | + // en remettant a vide le champ pour que ca marche aussi en cas |
|
| 245 | + // de JavaScript inactif. |
|
| 246 | + $js = " onload='var range=document.createRange(); var signal = document.getElementById(\"signal\"); var userSelection = window.getSelection(); range.setStart(signal,0); range.setEnd(signal,1); userSelection.addRange(range);'"; |
|
| 247 | + } |
|
| 248 | 248 | |
| 249 | - // admin/xxx correspond |
|
| 250 | - // a exec/base_xxx de preference |
|
| 251 | - // et exec/xxx sinon (compat) |
|
| 252 | - if (tester_url_ecrire("base_$script")) { |
|
| 253 | - $script = "base_$script"; |
|
| 254 | - } |
|
| 255 | - $form = copy_request($script, $corps, $suivant); |
|
| 256 | - $info_action = _T('info_action', ['action' => "$action"]); |
|
| 249 | + // admin/xxx correspond |
|
| 250 | + // a exec/base_xxx de preference |
|
| 251 | + // et exec/xxx sinon (compat) |
|
| 252 | + if (tester_url_ecrire("base_$script")) { |
|
| 253 | + $script = "base_$script"; |
|
| 254 | + } |
|
| 255 | + $form = copy_request($script, $corps, $suivant); |
|
| 256 | + $info_action = _T('info_action', ['action' => "$action"]); |
|
| 257 | 257 | |
| 258 | - return minipres($info_action, $form, ['onload' => $js]); |
|
| 259 | - } |
|
| 258 | + return minipres($info_action, $form, ['onload' => $js]); |
|
| 259 | + } |
|
| 260 | 260 | } |
| 261 | 261 | |
| 262 | 262 | /** |
@@ -267,13 +267,13 @@ discard block |
||
| 267 | 267 | * Nom de l'action (en base) qui a été exécutée |
| 268 | 268 | **/ |
| 269 | 269 | function fin_admin($action) { |
| 270 | - $signal = dir_admin() . fichier_admin($action); |
|
| 271 | - spip_unlink($signal); |
|
| 272 | - if ($action != 'delete_all') { |
|
| 273 | - effacer_meta($action); |
|
| 274 | - effacer_meta('admin'); |
|
| 275 | - spip_logger()->info("efface les meta admin et $action"); |
|
| 276 | - } |
|
| 270 | + $signal = dir_admin() . fichier_admin($action); |
|
| 271 | + spip_unlink($signal); |
|
| 272 | + if ($action != 'delete_all') { |
|
| 273 | + effacer_meta($action); |
|
| 274 | + effacer_meta('admin'); |
|
| 275 | + spip_logger()->info("efface les meta admin et $action"); |
|
| 276 | + } |
|
| 277 | 277 | } |
| 278 | 278 | |
| 279 | 279 | /** |
@@ -292,14 +292,14 @@ discard block |
||
| 292 | 292 | * Code HTML du formulaire |
| 293 | 293 | **/ |
| 294 | 294 | function copy_request($script, $suite, $submit = '') { |
| 295 | - include_spip('inc/filtres'); |
|
| 296 | - foreach ([...$_POST, ...$_GET] as $n => $c) { |
|
| 297 | - if (!in_array($n, ['fichier', 'exec', 'validation_admin']) && !is_array($c)) { |
|
| 298 | - $suite .= "\n<input type='hidden' name='" . spip_htmlspecialchars($n) . "' value='" . |
|
| 299 | - entites_html($c) . |
|
| 300 | - "' />"; |
|
| 301 | - } |
|
| 302 | - } |
|
| 295 | + include_spip('inc/filtres'); |
|
| 296 | + foreach ([...$_POST, ...$_GET] as $n => $c) { |
|
| 297 | + if (!in_array($n, ['fichier', 'exec', 'validation_admin']) && !is_array($c)) { |
|
| 298 | + $suite .= "\n<input type='hidden' name='" . spip_htmlspecialchars($n) . "' value='" . |
|
| 299 | + entites_html($c) . |
|
| 300 | + "' />"; |
|
| 301 | + } |
|
| 302 | + } |
|
| 303 | 303 | |
| 304 | - return generer_form_ecrire($script, $suite, '', $submit); |
|
| 304 | + return generer_form_ecrire($script, $suite, '', $submit); |
|
| 305 | 305 | } |
@@ -54,7 +54,7 @@ discard block |
||
| 54 | 54 | if ($res) { |
| 55 | 55 | return $res; |
| 56 | 56 | } |
| 57 | - spip_logger()->info("meta: $script " . print_r($_POST, true)); |
|
| 57 | + spip_logger()->info("meta: $script ".print_r($_POST, true)); |
|
| 58 | 58 | ecrire_meta($script, serialize($_POST)); |
| 59 | 59 | } |
| 60 | 60 | |
@@ -124,7 +124,7 @@ discard block |
||
| 124 | 124 | } |
| 125 | 125 | // on pourrait statuer automatiquement les webmestres a l'init d'une action auth par ftp ... ? |
| 126 | 126 | |
| 127 | - spip_logger($journal)->info("admin $pref" . ($valeur ? ' (reprise)' : ' (init)')); |
|
| 127 | + spip_logger($journal)->info("admin $pref".($valeur ? ' (reprise)' : ' (init)')); |
|
| 128 | 128 | |
| 129 | 129 | return ''; |
| 130 | 130 | } |
@@ -142,7 +142,7 @@ discard block |
||
| 142 | 142 | if (autoriser('configurer')) { |
| 143 | 143 | return _DIR_TMP; |
| 144 | 144 | } else { |
| 145 | - return _DIR_TRANSFERT . $GLOBALS['visiteur_session']['login'] . '/'; |
|
| 145 | + return _DIR_TRANSFERT.$GLOBALS['visiteur_session']['login'].'/'; |
|
| 146 | 146 | } |
| 147 | 147 | } |
| 148 | 148 | |
@@ -160,8 +160,8 @@ discard block |
||
| 160 | 160 | * Nom du fichier |
| 161 | 161 | **/ |
| 162 | 162 | function fichier_admin($action, $pref = 'admin_') { |
| 163 | - return $pref . |
|
| 164 | - substr(md5($action . (time() & ~2047) . $GLOBALS['visiteur_session']['login']), 0, 10); |
|
| 163 | + return $pref. |
|
| 164 | + substr(md5($action.(time() & ~2047).$GLOBALS['visiteur_session']['login']), 0, 10); |
|
| 165 | 165 | } |
| 166 | 166 | |
| 167 | 167 | /** |
@@ -197,7 +197,7 @@ discard block |
||
| 197 | 197 | } else { |
| 198 | 198 | $dir = dir_admin(); |
| 199 | 199 | $signal = fichier_admin($script); |
| 200 | - if (@file_exists($dir . $signal)) { |
|
| 200 | + if (@file_exists($dir.$signal)) { |
|
| 201 | 201 | spip_logger()->info("Action admin: $action"); |
| 202 | 202 | |
| 203 | 203 | return ''; |
@@ -215,7 +215,7 @@ discard block |
||
| 215 | 215 | |
| 216 | 216 | return ''; |
| 217 | 217 | } |
| 218 | - $corps .= '<input type="hidden" name="validation_admin" value="' . $signal . '" />'; |
|
| 218 | + $corps .= '<input type="hidden" name="validation_admin" value="'.$signal.'" />'; |
|
| 219 | 219 | $suivant = _T('bouton_valider'); |
| 220 | 220 | $js = ''; |
| 221 | 221 | } else { |
@@ -230,7 +230,7 @@ discard block |
||
| 230 | 230 | . "</legend>\n<label for='fichier'>" |
| 231 | 231 | . _T('info_creer_repertoire') |
| 232 | 232 | . "</label>\n" |
| 233 | - . "<span id='signal' class='formo'>" . $signal . '</span>' |
|
| 233 | + . "<span id='signal' class='formo'>".$signal.'</span>' |
|
| 234 | 234 | . "<input type='hidden' id='fichier' name='fichier' value='" |
| 235 | 235 | . $signal |
| 236 | 236 | . "' />" |
@@ -267,7 +267,7 @@ discard block |
||
| 267 | 267 | * Nom de l'action (en base) qui a été exécutée |
| 268 | 268 | **/ |
| 269 | 269 | function fin_admin($action) { |
| 270 | - $signal = dir_admin() . fichier_admin($action); |
|
| 270 | + $signal = dir_admin().fichier_admin($action); |
|
| 271 | 271 | spip_unlink($signal); |
| 272 | 272 | if ($action != 'delete_all') { |
| 273 | 273 | effacer_meta($action); |
@@ -295,8 +295,8 @@ discard block |
||
| 295 | 295 | include_spip('inc/filtres'); |
| 296 | 296 | foreach ([...$_POST, ...$_GET] as $n => $c) { |
| 297 | 297 | if (!in_array($n, ['fichier', 'exec', 'validation_admin']) && !is_array($c)) { |
| 298 | - $suite .= "\n<input type='hidden' name='" . spip_htmlspecialchars($n) . "' value='" . |
|
| 299 | - entites_html($c) . |
|
| 298 | + $suite .= "\n<input type='hidden' name='".spip_htmlspecialchars($n)."' value='". |
|
| 299 | + entites_html($c). |
|
| 300 | 300 | "' />"; |
| 301 | 301 | } |
| 302 | 302 | } |
@@ -15,7 +15,7 @@ discard block |
||
| 15 | 15 | * @package SPIP\Core\Queue |
| 16 | 16 | **/ |
| 17 | 17 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 18 | - return; |
|
| 18 | + return; |
|
| 19 | 19 | } |
| 20 | 20 | |
| 21 | 21 | define('_JQ_SCHEDULED', 1); |
@@ -49,101 +49,101 @@ discard block |
||
| 49 | 49 | * id of job |
| 50 | 50 | */ |
| 51 | 51 | function queue_add_job( |
| 52 | - $function, |
|
| 53 | - $description, |
|
| 54 | - $arguments = [], |
|
| 55 | - $file = '', |
|
| 56 | - $no_duplicate = false, |
|
| 57 | - $time = 0, |
|
| 58 | - $priority = 0 |
|
| 52 | + $function, |
|
| 53 | + $description, |
|
| 54 | + $arguments = [], |
|
| 55 | + $file = '', |
|
| 56 | + $no_duplicate = false, |
|
| 57 | + $time = 0, |
|
| 58 | + $priority = 0 |
|
| 59 | 59 | ) { |
| 60 | - include_spip('base/abstract_sql'); |
|
| 61 | - |
|
| 62 | - // cas pourri de ecrire/action/editer_site avec l'option reload=oui |
|
| 63 | - if (defined('_GENIE_SYNDIC_NOW')) { |
|
| 64 | - $arguments['id_syndic'] = _GENIE_SYNDIC_NOW; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - // serialiser les arguments |
|
| 68 | - $arguments = serialize($arguments); |
|
| 69 | - $md5args = md5($arguments); |
|
| 70 | - |
|
| 71 | - // si pas de date programee, des que possible |
|
| 72 | - $duplicate_where = 'status=' . intval(_JQ_SCHEDULED) . ' AND '; |
|
| 73 | - if (!$time) { |
|
| 74 | - $time = time(); |
|
| 75 | - $duplicate_where = ''; // ne pas dupliquer si deja le meme job en cours d'execution |
|
| 76 | - } |
|
| 77 | - $date = date('Y-m-d H:i:s', $time); |
|
| 78 | - |
|
| 79 | - $set_job = [ |
|
| 80 | - 'fonction' => $function, |
|
| 81 | - 'descriptif' => $description, |
|
| 82 | - 'args' => $arguments, |
|
| 83 | - 'md5args' => $md5args, |
|
| 84 | - 'inclure' => $file, |
|
| 85 | - 'priorite' => max(-10, min(10, intval($priority))), |
|
| 86 | - 'date' => $date, |
|
| 87 | - 'status' => _JQ_SCHEDULED, |
|
| 88 | - ]; |
|
| 89 | - // si option ne pas dupliquer, regarder si la fonction existe deja |
|
| 90 | - // avec les memes args et file |
|
| 91 | - if ( |
|
| 92 | - $no_duplicate |
|
| 93 | - && ($id_job = sql_getfetsel( |
|
| 94 | - 'id_job', |
|
| 95 | - 'spip_jobs', |
|
| 96 | - $duplicate_where = |
|
| 97 | - $duplicate_where . 'fonction=' . sql_quote($function) |
|
| 98 | - . (($no_duplicate === 'function_only') ? '' : |
|
| 99 | - ' AND md5args=' . sql_quote($md5args) . ' AND inclure=' . sql_quote($file)) |
|
| 100 | - )) |
|
| 101 | - ) { |
|
| 102 | - return $id_job; |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - $id_job = sql_insertq('spip_jobs', $set_job); |
|
| 106 | - // en cas de concurrence, deux process peuvent arriver jusqu'ici en parallele |
|
| 107 | - // avec le meme job unique a inserer. Dans ce cas, celui qui a eu l'id le plus grand |
|
| 108 | - // doit s'effacer |
|
| 109 | - if ( |
|
| 110 | - $no_duplicate |
|
| 111 | - && ($id_prev = sql_getfetsel('id_job', 'spip_jobs', 'id_job<' . intval($id_job) . " AND $duplicate_where")) |
|
| 112 | - ) { |
|
| 113 | - sql_delete('spip_jobs', 'id_job=' . intval($id_job)); |
|
| 114 | - |
|
| 115 | - return $id_prev; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - // verifier la non duplication qui peut etre problematique en cas de concurence |
|
| 119 | - // il faut dans ce cas que seul le dernier ajoute se supprime ! |
|
| 120 | - |
|
| 121 | - // une option de debug pour verifier que les arguments en base sont bons |
|
| 122 | - // ie cas d'un char non acceptables sur certains type de champs |
|
| 123 | - // qui coupe la valeur |
|
| 124 | - if (defined('_JQ_INSERT_CHECK_ARGS') && $id_job) { |
|
| 125 | - $args = sql_getfetsel('args', 'spip_jobs', 'id_job=' . intval($id_job)); |
|
| 126 | - if ($args !== $arguments) { |
|
| 127 | - spip_logger('queue')->info( |
|
| 128 | - 'arguments job errones / longueur ' . strlen($args) . ' vs ' . strlen($arguments) . ' / valeur : ' |
|
| 129 | - . var_export($arguments, true) |
|
| 130 | - ); |
|
| 131 | - } |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - if ($id_job) { |
|
| 135 | - queue_update_next_job_time($time); |
|
| 136 | - } |
|
| 137 | - // si la mise en file d'attente du job echoue, |
|
| 138 | - // il ne faut pas perdre l'execution de la fonction |
|
| 139 | - // on la lance immediatement, c'est un fallback |
|
| 140 | - // sauf en cas d'upgrade necessaire (table spip_jobs inexistante) |
|
| 141 | - elseif ($GLOBALS['meta']['version_installee'] == $GLOBALS['spip_version_base']) { |
|
| 142 | - $set_job['id_job'] = 0; |
|
| 143 | - queue_start_job($set_job); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - return $id_job; |
|
| 60 | + include_spip('base/abstract_sql'); |
|
| 61 | + |
|
| 62 | + // cas pourri de ecrire/action/editer_site avec l'option reload=oui |
|
| 63 | + if (defined('_GENIE_SYNDIC_NOW')) { |
|
| 64 | + $arguments['id_syndic'] = _GENIE_SYNDIC_NOW; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + // serialiser les arguments |
|
| 68 | + $arguments = serialize($arguments); |
|
| 69 | + $md5args = md5($arguments); |
|
| 70 | + |
|
| 71 | + // si pas de date programee, des que possible |
|
| 72 | + $duplicate_where = 'status=' . intval(_JQ_SCHEDULED) . ' AND '; |
|
| 73 | + if (!$time) { |
|
| 74 | + $time = time(); |
|
| 75 | + $duplicate_where = ''; // ne pas dupliquer si deja le meme job en cours d'execution |
|
| 76 | + } |
|
| 77 | + $date = date('Y-m-d H:i:s', $time); |
|
| 78 | + |
|
| 79 | + $set_job = [ |
|
| 80 | + 'fonction' => $function, |
|
| 81 | + 'descriptif' => $description, |
|
| 82 | + 'args' => $arguments, |
|
| 83 | + 'md5args' => $md5args, |
|
| 84 | + 'inclure' => $file, |
|
| 85 | + 'priorite' => max(-10, min(10, intval($priority))), |
|
| 86 | + 'date' => $date, |
|
| 87 | + 'status' => _JQ_SCHEDULED, |
|
| 88 | + ]; |
|
| 89 | + // si option ne pas dupliquer, regarder si la fonction existe deja |
|
| 90 | + // avec les memes args et file |
|
| 91 | + if ( |
|
| 92 | + $no_duplicate |
|
| 93 | + && ($id_job = sql_getfetsel( |
|
| 94 | + 'id_job', |
|
| 95 | + 'spip_jobs', |
|
| 96 | + $duplicate_where = |
|
| 97 | + $duplicate_where . 'fonction=' . sql_quote($function) |
|
| 98 | + . (($no_duplicate === 'function_only') ? '' : |
|
| 99 | + ' AND md5args=' . sql_quote($md5args) . ' AND inclure=' . sql_quote($file)) |
|
| 100 | + )) |
|
| 101 | + ) { |
|
| 102 | + return $id_job; |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + $id_job = sql_insertq('spip_jobs', $set_job); |
|
| 106 | + // en cas de concurrence, deux process peuvent arriver jusqu'ici en parallele |
|
| 107 | + // avec le meme job unique a inserer. Dans ce cas, celui qui a eu l'id le plus grand |
|
| 108 | + // doit s'effacer |
|
| 109 | + if ( |
|
| 110 | + $no_duplicate |
|
| 111 | + && ($id_prev = sql_getfetsel('id_job', 'spip_jobs', 'id_job<' . intval($id_job) . " AND $duplicate_where")) |
|
| 112 | + ) { |
|
| 113 | + sql_delete('spip_jobs', 'id_job=' . intval($id_job)); |
|
| 114 | + |
|
| 115 | + return $id_prev; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + // verifier la non duplication qui peut etre problematique en cas de concurence |
|
| 119 | + // il faut dans ce cas que seul le dernier ajoute se supprime ! |
|
| 120 | + |
|
| 121 | + // une option de debug pour verifier que les arguments en base sont bons |
|
| 122 | + // ie cas d'un char non acceptables sur certains type de champs |
|
| 123 | + // qui coupe la valeur |
|
| 124 | + if (defined('_JQ_INSERT_CHECK_ARGS') && $id_job) { |
|
| 125 | + $args = sql_getfetsel('args', 'spip_jobs', 'id_job=' . intval($id_job)); |
|
| 126 | + if ($args !== $arguments) { |
|
| 127 | + spip_logger('queue')->info( |
|
| 128 | + 'arguments job errones / longueur ' . strlen($args) . ' vs ' . strlen($arguments) . ' / valeur : ' |
|
| 129 | + . var_export($arguments, true) |
|
| 130 | + ); |
|
| 131 | + } |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + if ($id_job) { |
|
| 135 | + queue_update_next_job_time($time); |
|
| 136 | + } |
|
| 137 | + // si la mise en file d'attente du job echoue, |
|
| 138 | + // il ne faut pas perdre l'execution de la fonction |
|
| 139 | + // on la lance immediatement, c'est un fallback |
|
| 140 | + // sauf en cas d'upgrade necessaire (table spip_jobs inexistante) |
|
| 141 | + elseif ($GLOBALS['meta']['version_installee'] == $GLOBALS['spip_version_base']) { |
|
| 142 | + $set_job['id_job'] = 0; |
|
| 143 | + queue_start_job($set_job); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + return $id_job; |
|
| 147 | 147 | } |
| 148 | 148 | |
| 149 | 149 | /** |
@@ -152,11 +152,11 @@ discard block |
||
| 152 | 152 | * @return void |
| 153 | 153 | */ |
| 154 | 154 | function queue_purger() { |
| 155 | - include_spip('base/abstract_sql'); |
|
| 156 | - sql_delete('spip_jobs'); |
|
| 157 | - sql_delete('spip_jobs_liens', 'id_job NOT IN (' . sql_get_select('id_job', 'spip_jobs') . ')'); |
|
| 158 | - include_spip('inc/genie'); |
|
| 159 | - genie_queue_watch_dist(); |
|
| 155 | + include_spip('base/abstract_sql'); |
|
| 156 | + sql_delete('spip_jobs'); |
|
| 157 | + sql_delete('spip_jobs_liens', 'id_job NOT IN (' . sql_get_select('id_job', 'spip_jobs') . ')'); |
|
| 158 | + include_spip('inc/genie'); |
|
| 159 | + genie_queue_watch_dist(); |
|
| 160 | 160 | } |
| 161 | 161 | |
| 162 | 162 | /** |
@@ -167,25 +167,25 @@ discard block |
||
| 167 | 167 | * @return int|bool |
| 168 | 168 | */ |
| 169 | 169 | function queue_remove_job($id_job) { |
| 170 | - include_spip('base/abstract_sql'); |
|
| 171 | - |
|
| 172 | - if ( |
|
| 173 | - ($row = sql_fetsel('fonction,inclure,date', 'spip_jobs', 'id_job=' . intval($id_job))) |
|
| 174 | - && ($res = sql_delete('spip_jobs', 'id_job=' . intval($id_job))) |
|
| 175 | - ) { |
|
| 176 | - queue_unlink_job($id_job); |
|
| 177 | - // est-ce une tache cron qu'il faut relancer ? |
|
| 178 | - if ($periode = queue_is_cron_job($row['fonction'], $row['inclure'])) { |
|
| 179 | - // relancer avec les nouveaux arguments de temps |
|
| 180 | - include_spip('inc/genie'); |
|
| 181 | - // relancer avec la periode prevue |
|
| 182 | - queue_genie_replan_job($row['fonction'], $periode, strtotime($row['date'])); |
|
| 183 | - } |
|
| 184 | - queue_update_next_job_time(); |
|
| 185 | - return $res; |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - return false; |
|
| 170 | + include_spip('base/abstract_sql'); |
|
| 171 | + |
|
| 172 | + if ( |
|
| 173 | + ($row = sql_fetsel('fonction,inclure,date', 'spip_jobs', 'id_job=' . intval($id_job))) |
|
| 174 | + && ($res = sql_delete('spip_jobs', 'id_job=' . intval($id_job))) |
|
| 175 | + ) { |
|
| 176 | + queue_unlink_job($id_job); |
|
| 177 | + // est-ce une tache cron qu'il faut relancer ? |
|
| 178 | + if ($periode = queue_is_cron_job($row['fonction'], $row['inclure'])) { |
|
| 179 | + // relancer avec les nouveaux arguments de temps |
|
| 180 | + include_spip('inc/genie'); |
|
| 181 | + // relancer avec la periode prevue |
|
| 182 | + queue_genie_replan_job($row['fonction'], $periode, strtotime($row['date'])); |
|
| 183 | + } |
|
| 184 | + queue_update_next_job_time(); |
|
| 185 | + return $res; |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + return false; |
|
| 189 | 189 | } |
| 190 | 190 | |
| 191 | 191 | /** |
@@ -198,18 +198,18 @@ discard block |
||
| 198 | 198 | * ou un tableau composé de tableaux simples pour lieur plusieurs objets en une fois |
| 199 | 199 | */ |
| 200 | 200 | function queue_link_job($id_job, $objets) { |
| 201 | - include_spip('base/abstract_sql'); |
|
| 202 | - |
|
| 203 | - if (is_array($objets) && count($objets)) { |
|
| 204 | - if (is_array(reset($objets))) { |
|
| 205 | - foreach ($objets as $k => $o) { |
|
| 206 | - $objets[$k]['id_job'] = $id_job; |
|
| 207 | - } |
|
| 208 | - sql_insertq_multi('spip_jobs_liens', $objets); |
|
| 209 | - } else { |
|
| 210 | - sql_insertq('spip_jobs_liens', array_merge(['id_job' => $id_job], $objets)); |
|
| 211 | - } |
|
| 212 | - } |
|
| 201 | + include_spip('base/abstract_sql'); |
|
| 202 | + |
|
| 203 | + if (is_array($objets) && count($objets)) { |
|
| 204 | + if (is_array(reset($objets))) { |
|
| 205 | + foreach ($objets as $k => $o) { |
|
| 206 | + $objets[$k]['id_job'] = $id_job; |
|
| 207 | + } |
|
| 208 | + sql_insertq_multi('spip_jobs_liens', $objets); |
|
| 209 | + } else { |
|
| 210 | + sql_insertq('spip_jobs_liens', array_merge(['id_job' => $id_job], $objets)); |
|
| 211 | + } |
|
| 212 | + } |
|
| 213 | 213 | } |
| 214 | 214 | |
| 215 | 215 | /** |
@@ -221,7 +221,7 @@ discard block |
||
| 221 | 221 | * resultat du sql_delete |
| 222 | 222 | */ |
| 223 | 223 | function queue_unlink_job($id_job) { |
| 224 | - return sql_delete('spip_jobs_liens', 'id_job=' . intval($id_job)); |
|
| 224 | + return sql_delete('spip_jobs_liens', 'id_job=' . intval($id_job)); |
|
| 225 | 225 | } |
| 226 | 226 | |
| 227 | 227 | /** |
@@ -234,38 +234,38 @@ discard block |
||
| 234 | 234 | */ |
| 235 | 235 | function queue_start_job($row) { |
| 236 | 236 | |
| 237 | - $logger = spip_logger('queue'); |
|
| 238 | - |
|
| 239 | - // deserialiser les arguments |
|
| 240 | - $args = unserialize($row['args']); |
|
| 241 | - if (!is_array($args)) { |
|
| 242 | - $logger->info('arguments job errones ' . var_export($row, true)); |
|
| 243 | - return false; |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - $fonction = $row['fonction']; |
|
| 247 | - if (strlen($inclure = trim($row['inclure']))) { |
|
| 248 | - if (str_ends_with($inclure, '/')) { // c'est un chemin pour charger_fonction |
|
| 249 | - $f = charger_fonction($fonction, rtrim($inclure, '/'), false); |
|
| 250 | - if ($f) { |
|
| 251 | - $fonction = $f; |
|
| 252 | - } |
|
| 253 | - } else { |
|
| 254 | - include_spip($inclure); |
|
| 255 | - } |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - if (!function_exists($fonction)) { |
|
| 259 | - $logger->info("fonction $fonction ($inclure) inexistante " . var_export($row, true)); |
|
| 260 | - |
|
| 261 | - return false; |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - $logger->info('queue [' . $row['id_job'] . "]: $fonction() start"); |
|
| 265 | - $res = $fonction(...$args); |
|
| 266 | - $logger->info('queue [' . $row['id_job'] . "]: $fonction() end"); |
|
| 267 | - |
|
| 268 | - return $res; |
|
| 237 | + $logger = spip_logger('queue'); |
|
| 238 | + |
|
| 239 | + // deserialiser les arguments |
|
| 240 | + $args = unserialize($row['args']); |
|
| 241 | + if (!is_array($args)) { |
|
| 242 | + $logger->info('arguments job errones ' . var_export($row, true)); |
|
| 243 | + return false; |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + $fonction = $row['fonction']; |
|
| 247 | + if (strlen($inclure = trim($row['inclure']))) { |
|
| 248 | + if (str_ends_with($inclure, '/')) { // c'est un chemin pour charger_fonction |
|
| 249 | + $f = charger_fonction($fonction, rtrim($inclure, '/'), false); |
|
| 250 | + if ($f) { |
|
| 251 | + $fonction = $f; |
|
| 252 | + } |
|
| 253 | + } else { |
|
| 254 | + include_spip($inclure); |
|
| 255 | + } |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + if (!function_exists($fonction)) { |
|
| 259 | + $logger->info("fonction $fonction ($inclure) inexistante " . var_export($row, true)); |
|
| 260 | + |
|
| 261 | + return false; |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + $logger->info('queue [' . $row['id_job'] . "]: $fonction() start"); |
|
| 265 | + $res = $fonction(...$args); |
|
| 266 | + $logger->info('queue [' . $row['id_job'] . "]: $fonction() end"); |
|
| 267 | + |
|
| 268 | + return $res; |
|
| 269 | 269 | } |
| 270 | 270 | |
| 271 | 271 | /** |
@@ -292,90 +292,90 @@ discard block |
||
| 292 | 292 | * - true : une planification a été faite. |
| 293 | 293 | */ |
| 294 | 294 | function queue_schedule($force_jobs = null) { |
| 295 | - $time = time(); |
|
| 296 | - $logger = spip_logger('jq'); |
|
| 297 | - if (defined('_DEBUG_BLOCK_QUEUE')) { |
|
| 298 | - $logger->debug('_DEBUG_BLOCK_QUEUE : schedule stop'); |
|
| 299 | - |
|
| 300 | - return; |
|
| 301 | - } |
|
| 302 | - |
|
| 303 | - // rien a faire si le prochain job est encore dans le futur |
|
| 304 | - if (queue_sleep_time_to_next_job() > 0 && (!$force_jobs || !count($force_jobs))) { |
|
| 305 | - $logger->debug('queue_sleep_time_to_next_job'); |
|
| 306 | - |
|
| 307 | - return; |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - include_spip('base/abstract_sql'); |
|
| 311 | - // on ne peut rien faire si pas de connexion SQL |
|
| 312 | - if (!spip_connect()) { |
|
| 313 | - return false; |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - if (!defined('_JQ_MAX_JOBS_TIME_TO_EXECUTE')) { |
|
| 317 | - $max_time = ini_get('max_execution_time') / 2; |
|
| 318 | - // valeur conservatrice si on a pas reussi a lire le max_execution_time |
|
| 319 | - if (!$max_time) { |
|
| 320 | - $max_time = 5; |
|
| 321 | - } |
|
| 322 | - define('_JQ_MAX_JOBS_TIME_TO_EXECUTE', min($max_time, 15)); // une valeur maxi en temps. |
|
| 323 | - } |
|
| 324 | - $end_time = $time + _JQ_MAX_JOBS_TIME_TO_EXECUTE; |
|
| 325 | - |
|
| 326 | - $logger->debug("JQ schedule $time / $end_time"); |
|
| 327 | - |
|
| 328 | - if (!defined('_JQ_MAX_JOBS_EXECUTE')) { |
|
| 329 | - define('_JQ_MAX_JOBS_EXECUTE', 200); |
|
| 330 | - } |
|
| 331 | - $nbj = 0; |
|
| 332 | - // attraper les jobs |
|
| 333 | - // dont la date est passee (echus en attente), |
|
| 334 | - // par ordre : |
|
| 335 | - // - de priorite |
|
| 336 | - // - de date |
|
| 337 | - // lorsqu'un job cron n'a pas fini, sa priorite est descendue |
|
| 338 | - // pour qu'il ne bloque pas les autres jobs en attente |
|
| 339 | - if (is_array($force_jobs) && count($force_jobs)) { |
|
| 340 | - $cond = 'status=' . intval(_JQ_SCHEDULED) . ' AND ' . sql_in('id_job', $force_jobs); |
|
| 341 | - } else { |
|
| 342 | - $now = date('Y-m-d H:i:s', $time); |
|
| 343 | - $cond = 'status=' . intval(_JQ_SCHEDULED) . ' AND date<=' . sql_quote($now); |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - register_shutdown_function('queue_error_handler'); // recuperer les erreurs auant que possible |
|
| 347 | - $res = sql_allfetsel('*', 'spip_jobs', $cond, '', 'priorite DESC,date', '0,' . (_JQ_MAX_JOBS_EXECUTE + 1)); |
|
| 348 | - do { |
|
| 349 | - if ($row = array_shift($res)) { |
|
| 350 | - $nbj++; |
|
| 351 | - // il faut un verrou, a base de sql_delete |
|
| 352 | - if (sql_delete('spip_jobs', 'id_job=' . intval($row['id_job']) . ' AND status=' . intval(_JQ_SCHEDULED))) { |
|
| 353 | - #$logger->info("JQ schedule job ".$nbj." OK"); |
|
| 354 | - // on reinsert dans la base aussitot avec un status=_JQ_PENDING |
|
| 355 | - $row['status'] = _JQ_PENDING; |
|
| 356 | - $row['date'] = date('Y-m-d H:i:s', $time); |
|
| 357 | - sql_insertq('spip_jobs', $row); |
|
| 358 | - |
|
| 359 | - // on a la main sur le job : |
|
| 360 | - // l'executer |
|
| 361 | - $result = queue_start_job($row); |
|
| 362 | - |
|
| 363 | - $time = time(); |
|
| 364 | - queue_close_job($row, $time, $result); |
|
| 365 | - } |
|
| 366 | - } |
|
| 367 | - $logger->debug('JQ schedule job end time ' . $time); |
|
| 368 | - } while ($nbj < _JQ_MAX_JOBS_EXECUTE && $row && $time < $end_time); |
|
| 369 | - $logger->debug('JQ schedule end time ' . time()); |
|
| 370 | - |
|
| 371 | - if ($row = array_shift($res)) { |
|
| 372 | - queue_update_next_job_time(0); // on sait qu'il y a encore des jobs a lancer ASAP |
|
| 373 | - $logger->debug('JQ encore !'); |
|
| 374 | - } else { |
|
| 375 | - queue_update_next_job_time(); |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - return true; |
|
| 295 | + $time = time(); |
|
| 296 | + $logger = spip_logger('jq'); |
|
| 297 | + if (defined('_DEBUG_BLOCK_QUEUE')) { |
|
| 298 | + $logger->debug('_DEBUG_BLOCK_QUEUE : schedule stop'); |
|
| 299 | + |
|
| 300 | + return; |
|
| 301 | + } |
|
| 302 | + |
|
| 303 | + // rien a faire si le prochain job est encore dans le futur |
|
| 304 | + if (queue_sleep_time_to_next_job() > 0 && (!$force_jobs || !count($force_jobs))) { |
|
| 305 | + $logger->debug('queue_sleep_time_to_next_job'); |
|
| 306 | + |
|
| 307 | + return; |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + include_spip('base/abstract_sql'); |
|
| 311 | + // on ne peut rien faire si pas de connexion SQL |
|
| 312 | + if (!spip_connect()) { |
|
| 313 | + return false; |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + if (!defined('_JQ_MAX_JOBS_TIME_TO_EXECUTE')) { |
|
| 317 | + $max_time = ini_get('max_execution_time') / 2; |
|
| 318 | + // valeur conservatrice si on a pas reussi a lire le max_execution_time |
|
| 319 | + if (!$max_time) { |
|
| 320 | + $max_time = 5; |
|
| 321 | + } |
|
| 322 | + define('_JQ_MAX_JOBS_TIME_TO_EXECUTE', min($max_time, 15)); // une valeur maxi en temps. |
|
| 323 | + } |
|
| 324 | + $end_time = $time + _JQ_MAX_JOBS_TIME_TO_EXECUTE; |
|
| 325 | + |
|
| 326 | + $logger->debug("JQ schedule $time / $end_time"); |
|
| 327 | + |
|
| 328 | + if (!defined('_JQ_MAX_JOBS_EXECUTE')) { |
|
| 329 | + define('_JQ_MAX_JOBS_EXECUTE', 200); |
|
| 330 | + } |
|
| 331 | + $nbj = 0; |
|
| 332 | + // attraper les jobs |
|
| 333 | + // dont la date est passee (echus en attente), |
|
| 334 | + // par ordre : |
|
| 335 | + // - de priorite |
|
| 336 | + // - de date |
|
| 337 | + // lorsqu'un job cron n'a pas fini, sa priorite est descendue |
|
| 338 | + // pour qu'il ne bloque pas les autres jobs en attente |
|
| 339 | + if (is_array($force_jobs) && count($force_jobs)) { |
|
| 340 | + $cond = 'status=' . intval(_JQ_SCHEDULED) . ' AND ' . sql_in('id_job', $force_jobs); |
|
| 341 | + } else { |
|
| 342 | + $now = date('Y-m-d H:i:s', $time); |
|
| 343 | + $cond = 'status=' . intval(_JQ_SCHEDULED) . ' AND date<=' . sql_quote($now); |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + register_shutdown_function('queue_error_handler'); // recuperer les erreurs auant que possible |
|
| 347 | + $res = sql_allfetsel('*', 'spip_jobs', $cond, '', 'priorite DESC,date', '0,' . (_JQ_MAX_JOBS_EXECUTE + 1)); |
|
| 348 | + do { |
|
| 349 | + if ($row = array_shift($res)) { |
|
| 350 | + $nbj++; |
|
| 351 | + // il faut un verrou, a base de sql_delete |
|
| 352 | + if (sql_delete('spip_jobs', 'id_job=' . intval($row['id_job']) . ' AND status=' . intval(_JQ_SCHEDULED))) { |
|
| 353 | + #$logger->info("JQ schedule job ".$nbj." OK"); |
|
| 354 | + // on reinsert dans la base aussitot avec un status=_JQ_PENDING |
|
| 355 | + $row['status'] = _JQ_PENDING; |
|
| 356 | + $row['date'] = date('Y-m-d H:i:s', $time); |
|
| 357 | + sql_insertq('spip_jobs', $row); |
|
| 358 | + |
|
| 359 | + // on a la main sur le job : |
|
| 360 | + // l'executer |
|
| 361 | + $result = queue_start_job($row); |
|
| 362 | + |
|
| 363 | + $time = time(); |
|
| 364 | + queue_close_job($row, $time, $result); |
|
| 365 | + } |
|
| 366 | + } |
|
| 367 | + $logger->debug('JQ schedule job end time ' . $time); |
|
| 368 | + } while ($nbj < _JQ_MAX_JOBS_EXECUTE && $row && $time < $end_time); |
|
| 369 | + $logger->debug('JQ schedule end time ' . time()); |
|
| 370 | + |
|
| 371 | + if ($row = array_shift($res)) { |
|
| 372 | + queue_update_next_job_time(0); // on sait qu'il y a encore des jobs a lancer ASAP |
|
| 373 | + $logger->debug('JQ encore !'); |
|
| 374 | + } else { |
|
| 375 | + queue_update_next_job_time(); |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + return true; |
|
| 379 | 379 | } |
| 380 | 380 | |
| 381 | 381 | /** |
@@ -393,21 +393,21 @@ discard block |
||
| 393 | 393 | * @param int $result |
| 394 | 394 | */ |
| 395 | 395 | function queue_close_job(&$row, $time, $result = 0) { |
| 396 | - // est-ce une tache cron qu'il faut relancer ? |
|
| 397 | - if ($periode = queue_is_cron_job($row['fonction'], $row['inclure'])) { |
|
| 398 | - // relancer avec les nouveaux arguments de temps |
|
| 399 | - include_spip('inc/genie'); |
|
| 400 | - if ($result < 0) { // relancer tout de suite, mais en baissant la priorite |
|
| 401 | - queue_genie_replan_job($row['fonction'], $periode, 0 - $result, null, $row['priorite'] - 1); |
|
| 402 | - } else // relancer avec la periode prevue |
|
| 403 | - { |
|
| 404 | - queue_genie_replan_job($row['fonction'], $periode, $time); |
|
| 405 | - } |
|
| 406 | - } |
|
| 407 | - // purger ses liens eventuels avec des objets |
|
| 408 | - sql_delete('spip_jobs_liens', 'id_job=' . intval($row['id_job'])); |
|
| 409 | - // supprimer le job fini |
|
| 410 | - sql_delete('spip_jobs', 'id_job=' . intval($row['id_job'])); |
|
| 396 | + // est-ce une tache cron qu'il faut relancer ? |
|
| 397 | + if ($periode = queue_is_cron_job($row['fonction'], $row['inclure'])) { |
|
| 398 | + // relancer avec les nouveaux arguments de temps |
|
| 399 | + include_spip('inc/genie'); |
|
| 400 | + if ($result < 0) { // relancer tout de suite, mais en baissant la priorite |
|
| 401 | + queue_genie_replan_job($row['fonction'], $periode, 0 - $result, null, $row['priorite'] - 1); |
|
| 402 | + } else // relancer avec la periode prevue |
|
| 403 | + { |
|
| 404 | + queue_genie_replan_job($row['fonction'], $periode, $time); |
|
| 405 | + } |
|
| 406 | + } |
|
| 407 | + // purger ses liens eventuels avec des objets |
|
| 408 | + sql_delete('spip_jobs_liens', 'id_job=' . intval($row['id_job'])); |
|
| 409 | + // supprimer le job fini |
|
| 410 | + sql_delete('spip_jobs', 'id_job=' . intval($row['id_job'])); |
|
| 411 | 411 | } |
| 412 | 412 | |
| 413 | 413 | /** |
@@ -417,10 +417,10 @@ discard block |
||
| 417 | 417 | * @uses queue_update_next_job_time() |
| 418 | 418 | */ |
| 419 | 419 | function queue_error_handler() { |
| 420 | - // se remettre dans le bon dossier, car Apache le change parfois (toujours?) |
|
| 421 | - chdir(_ROOT_CWD); |
|
| 420 | + // se remettre dans le bon dossier, car Apache le change parfois (toujours?) |
|
| 421 | + chdir(_ROOT_CWD); |
|
| 422 | 422 | |
| 423 | - queue_update_next_job_time(); |
|
| 423 | + queue_update_next_job_time(); |
|
| 424 | 424 | } |
| 425 | 425 | |
| 426 | 426 | |
@@ -437,18 +437,18 @@ discard block |
||
| 437 | 437 | * Périodicité de la tâche en secondes, si tâche périodique, sinon false. |
| 438 | 438 | */ |
| 439 | 439 | function queue_is_cron_job($function, $inclure) { |
| 440 | - static $taches = null; |
|
| 441 | - if (str_starts_with($inclure, 'genie/')) { |
|
| 442 | - if (is_null($taches)) { |
|
| 443 | - include_spip('inc/genie'); |
|
| 444 | - $taches = taches_generales(); |
|
| 445 | - } |
|
| 446 | - if (isset($taches[$function])) { |
|
| 447 | - return $taches[$function]; |
|
| 448 | - } |
|
| 449 | - } |
|
| 450 | - |
|
| 451 | - return false; |
|
| 440 | + static $taches = null; |
|
| 441 | + if (str_starts_with($inclure, 'genie/')) { |
|
| 442 | + if (is_null($taches)) { |
|
| 443 | + include_spip('inc/genie'); |
|
| 444 | + $taches = taches_generales(); |
|
| 445 | + } |
|
| 446 | + if (isset($taches[$function])) { |
|
| 447 | + return $taches[$function]; |
|
| 448 | + } |
|
| 449 | + } |
|
| 450 | + |
|
| 451 | + return false; |
|
| 452 | 452 | } |
| 453 | 453 | |
| 454 | 454 | /** |
@@ -462,62 +462,62 @@ discard block |
||
| 462 | 462 | * temps de la tache ajoutee ou 0 pour ASAP |
| 463 | 463 | */ |
| 464 | 464 | function queue_update_next_job_time($next_time = null) { |
| 465 | - static $nb_jobs_scheduled = null; |
|
| 466 | - static $deja_la = false; |
|
| 467 | - // prendre le min des $next_time que l'on voit passer ici, en cas de reentrance |
|
| 468 | - static $next = null; |
|
| 469 | - // queue_close_job peut etre reentrant ici |
|
| 470 | - if ($deja_la) { |
|
| 471 | - return; |
|
| 472 | - } |
|
| 473 | - $deja_la = true; |
|
| 474 | - |
|
| 475 | - include_spip('base/abstract_sql'); |
|
| 476 | - $time = time(); |
|
| 477 | - |
|
| 478 | - // traiter les jobs morts au combat (_JQ_PENDING depuis plus de 180s) |
|
| 479 | - // pour cause de timeout ou autre erreur fatale |
|
| 480 | - $res = sql_allfetsel( |
|
| 481 | - '*', |
|
| 482 | - 'spip_jobs', |
|
| 483 | - 'status=' . intval(_JQ_PENDING) . ' AND date<' . sql_quote(date('Y-m-d H:i:s', $time - 180)) |
|
| 484 | - ); |
|
| 485 | - if (is_array($res)) { |
|
| 486 | - foreach ($res as $row) { |
|
| 487 | - queue_close_job($row, $time); |
|
| 488 | - spip_logger('job_mort')->error('queue_close_job car _JQ_PENDING depuis +180s : ' . print_r($row, true)); |
|
| 489 | - } |
|
| 490 | - } |
|
| 491 | - |
|
| 492 | - // chercher la date du prochain job si pas connu |
|
| 493 | - if (is_null($next) || is_null(queue_sleep_time_to_next_job())) { |
|
| 494 | - $date = sql_getfetsel('date', 'spip_jobs', 'status=' . intval(_JQ_SCHEDULED), '', 'date', '0,1'); |
|
| 495 | - $next = strtotime($date); |
|
| 496 | - } |
|
| 497 | - if (!is_null($next_time)) { |
|
| 498 | - if (is_null($next) || $next > $next_time) { |
|
| 499 | - $next = $next_time; |
|
| 500 | - } |
|
| 501 | - } |
|
| 502 | - |
|
| 503 | - if ($next) { |
|
| 504 | - if (is_null($nb_jobs_scheduled)) { |
|
| 505 | - $nb_jobs_scheduled = sql_countsel( |
|
| 506 | - 'spip_jobs', |
|
| 507 | - 'status=' . intval(_JQ_SCHEDULED) . ' AND date<' . sql_quote(date('Y-m-d H:i:s', $time)) |
|
| 508 | - ); |
|
| 509 | - } elseif ($next <= $time) { |
|
| 510 | - $nb_jobs_scheduled++; |
|
| 511 | - } |
|
| 512 | - // si trop de jobs en attente, on force la purge en fin de hit |
|
| 513 | - // pour assurer le coup |
|
| 514 | - if ($nb_jobs_scheduled > (defined('_JQ_NB_JOBS_OVERFLOW') ? _JQ_NB_JOBS_OVERFLOW : 10000)) { |
|
| 515 | - define('_DIRECT_CRON_FORCE', true); |
|
| 516 | - } |
|
| 517 | - } |
|
| 518 | - |
|
| 519 | - queue_set_next_job_time($next); |
|
| 520 | - $deja_la = false; |
|
| 465 | + static $nb_jobs_scheduled = null; |
|
| 466 | + static $deja_la = false; |
|
| 467 | + // prendre le min des $next_time que l'on voit passer ici, en cas de reentrance |
|
| 468 | + static $next = null; |
|
| 469 | + // queue_close_job peut etre reentrant ici |
|
| 470 | + if ($deja_la) { |
|
| 471 | + return; |
|
| 472 | + } |
|
| 473 | + $deja_la = true; |
|
| 474 | + |
|
| 475 | + include_spip('base/abstract_sql'); |
|
| 476 | + $time = time(); |
|
| 477 | + |
|
| 478 | + // traiter les jobs morts au combat (_JQ_PENDING depuis plus de 180s) |
|
| 479 | + // pour cause de timeout ou autre erreur fatale |
|
| 480 | + $res = sql_allfetsel( |
|
| 481 | + '*', |
|
| 482 | + 'spip_jobs', |
|
| 483 | + 'status=' . intval(_JQ_PENDING) . ' AND date<' . sql_quote(date('Y-m-d H:i:s', $time - 180)) |
|
| 484 | + ); |
|
| 485 | + if (is_array($res)) { |
|
| 486 | + foreach ($res as $row) { |
|
| 487 | + queue_close_job($row, $time); |
|
| 488 | + spip_logger('job_mort')->error('queue_close_job car _JQ_PENDING depuis +180s : ' . print_r($row, true)); |
|
| 489 | + } |
|
| 490 | + } |
|
| 491 | + |
|
| 492 | + // chercher la date du prochain job si pas connu |
|
| 493 | + if (is_null($next) || is_null(queue_sleep_time_to_next_job())) { |
|
| 494 | + $date = sql_getfetsel('date', 'spip_jobs', 'status=' . intval(_JQ_SCHEDULED), '', 'date', '0,1'); |
|
| 495 | + $next = strtotime($date); |
|
| 496 | + } |
|
| 497 | + if (!is_null($next_time)) { |
|
| 498 | + if (is_null($next) || $next > $next_time) { |
|
| 499 | + $next = $next_time; |
|
| 500 | + } |
|
| 501 | + } |
|
| 502 | + |
|
| 503 | + if ($next) { |
|
| 504 | + if (is_null($nb_jobs_scheduled)) { |
|
| 505 | + $nb_jobs_scheduled = sql_countsel( |
|
| 506 | + 'spip_jobs', |
|
| 507 | + 'status=' . intval(_JQ_SCHEDULED) . ' AND date<' . sql_quote(date('Y-m-d H:i:s', $time)) |
|
| 508 | + ); |
|
| 509 | + } elseif ($next <= $time) { |
|
| 510 | + $nb_jobs_scheduled++; |
|
| 511 | + } |
|
| 512 | + // si trop de jobs en attente, on force la purge en fin de hit |
|
| 513 | + // pour assurer le coup |
|
| 514 | + if ($nb_jobs_scheduled > (defined('_JQ_NB_JOBS_OVERFLOW') ? _JQ_NB_JOBS_OVERFLOW : 10000)) { |
|
| 515 | + define('_DIRECT_CRON_FORCE', true); |
|
| 516 | + } |
|
| 517 | + } |
|
| 518 | + |
|
| 519 | + queue_set_next_job_time($next); |
|
| 520 | + $deja_la = false; |
|
| 521 | 521 | } |
| 522 | 522 | |
| 523 | 523 | |
@@ -528,26 +528,26 @@ discard block |
||
| 528 | 528 | */ |
| 529 | 529 | function queue_set_next_job_time($next) { |
| 530 | 530 | |
| 531 | - // utiliser le temps courant reel plutot que temps de la requete ici |
|
| 532 | - $time = time(); |
|
| 533 | - |
|
| 534 | - // toujours relire la valeur pour comparer, pour tenir compte des maj concourrantes |
|
| 535 | - // et ne mettre a jour que si il y a un interet a le faire |
|
| 536 | - // permet ausis d'initialiser le nom de fichier a coup sur |
|
| 537 | - $curr_next = $_SERVER['REQUEST_TIME'] + max(0, queue_sleep_time_to_next_job(true)); |
|
| 538 | - if ( |
|
| 539 | - $curr_next <= $time && $next > $time // le prochain job est dans le futur mais pas la date planifiee actuelle |
|
| 540 | - || $curr_next > $next // le prochain job est plus tot que la date planifiee actuelle |
|
| 541 | - ) { |
|
| 542 | - if (function_exists('cache_set') && defined('_MEMOIZE_MEMORY') && _MEMOIZE_MEMORY) { |
|
| 543 | - cache_set(_JQ_NEXT_JOB_TIME_FILENAME, intval($next)); |
|
| 544 | - } else { |
|
| 545 | - ecrire_fichier(_JQ_NEXT_JOB_TIME_FILENAME, intval($next)); |
|
| 546 | - } |
|
| 547 | - queue_sleep_time_to_next_job($next); |
|
| 548 | - } |
|
| 549 | - |
|
| 550 | - return queue_sleep_time_to_next_job(); |
|
| 531 | + // utiliser le temps courant reel plutot que temps de la requete ici |
|
| 532 | + $time = time(); |
|
| 533 | + |
|
| 534 | + // toujours relire la valeur pour comparer, pour tenir compte des maj concourrantes |
|
| 535 | + // et ne mettre a jour que si il y a un interet a le faire |
|
| 536 | + // permet ausis d'initialiser le nom de fichier a coup sur |
|
| 537 | + $curr_next = $_SERVER['REQUEST_TIME'] + max(0, queue_sleep_time_to_next_job(true)); |
|
| 538 | + if ( |
|
| 539 | + $curr_next <= $time && $next > $time // le prochain job est dans le futur mais pas la date planifiee actuelle |
|
| 540 | + || $curr_next > $next // le prochain job est plus tot que la date planifiee actuelle |
|
| 541 | + ) { |
|
| 542 | + if (function_exists('cache_set') && defined('_MEMOIZE_MEMORY') && _MEMOIZE_MEMORY) { |
|
| 543 | + cache_set(_JQ_NEXT_JOB_TIME_FILENAME, intval($next)); |
|
| 544 | + } else { |
|
| 545 | + ecrire_fichier(_JQ_NEXT_JOB_TIME_FILENAME, intval($next)); |
|
| 546 | + } |
|
| 547 | + queue_sleep_time_to_next_job($next); |
|
| 548 | + } |
|
| 549 | + |
|
| 550 | + return queue_sleep_time_to_next_job(); |
|
| 551 | 551 | } |
| 552 | 552 | |
| 553 | 553 | /** |
@@ -564,60 +564,60 @@ discard block |
||
| 564 | 564 | * @return string |
| 565 | 565 | */ |
| 566 | 566 | function queue_affichage_cron() { |
| 567 | - $texte = ''; |
|
| 568 | - |
|
| 569 | - $time_to_next = queue_sleep_time_to_next_job(); |
|
| 570 | - // rien a faire si le prochain job est encore dans le futur |
|
| 571 | - if ($time_to_next > 0 || defined('_DEBUG_BLOCK_QUEUE')) { |
|
| 572 | - return $texte; |
|
| 573 | - } |
|
| 574 | - |
|
| 575 | - // ne pas relancer si on vient de lancer dans la meme seconde par un hit concurent |
|
| 576 | - if (file_exists($lock = _DIR_TMP . 'cron.lock') && !(@filemtime($lock) < $_SERVER['REQUEST_TIME'])) { |
|
| 577 | - return $texte; |
|
| 578 | - } |
|
| 579 | - |
|
| 580 | - @touch($lock); |
|
| 581 | - |
|
| 582 | - // il y a des taches en attentes |
|
| 583 | - // si depuis plus de 5min, on essaye de lancer le cron par tous les moyens pour rattraper le coup |
|
| 584 | - // on est sans doute sur un site qui n'autorise pas http sortant ou avec peu de trafic |
|
| 585 | - $urgent = false; |
|
| 586 | - if ($time_to_next < -300) { |
|
| 587 | - $urgent = true; |
|
| 588 | - } |
|
| 589 | - |
|
| 590 | - $url_cron = generer_url_action('cron', '', false, true); |
|
| 591 | - |
|
| 592 | - if (!defined('_HTML_BG_CRON_FORCE') || !_HTML_BG_CRON_FORCE) { |
|
| 593 | - if (queue_lancer_url_http_async($url_cron) && !$urgent) { |
|
| 594 | - return $texte; |
|
| 595 | - } |
|
| 596 | - } |
|
| 597 | - |
|
| 598 | - // si deja force, on retourne sans rien |
|
| 599 | - if (defined('_DIRECT_CRON_FORCE')) { |
|
| 600 | - return $texte; |
|
| 601 | - } |
|
| 602 | - |
|
| 603 | - // si c'est un bot |
|
| 604 | - // inutile de faire un appel par image background, |
|
| 605 | - // on force un appel direct en fin de hit |
|
| 606 | - if ((defined('_IS_BOT') && _IS_BOT)) { |
|
| 607 | - define('_DIRECT_CRON_FORCE', true); |
|
| 608 | - |
|
| 609 | - return $texte; |
|
| 610 | - } |
|
| 611 | - |
|
| 612 | - if (!defined('_HTML_BG_CRON_INHIB') || !_HTML_BG_CRON_INHIB) { |
|
| 613 | - // en derniere solution, on insere un appel xhr non bloquant ou une image background dans la page si pas de JS |
|
| 614 | - $url_cron = generer_url_action('cron'); |
|
| 615 | - $texte = '<!-- SPIP-CRON -->' |
|
| 616 | - . "<script>setTimeout(function(){var xo = new XMLHttpRequest();xo.open('GET', '$url_cron', true);xo.send('');},100);</script>" |
|
| 617 | - . "<noscript><div style=\"background-image: url('$url_cron');\"></div></noscript>"; |
|
| 618 | - } |
|
| 619 | - |
|
| 620 | - return $texte; |
|
| 567 | + $texte = ''; |
|
| 568 | + |
|
| 569 | + $time_to_next = queue_sleep_time_to_next_job(); |
|
| 570 | + // rien a faire si le prochain job est encore dans le futur |
|
| 571 | + if ($time_to_next > 0 || defined('_DEBUG_BLOCK_QUEUE')) { |
|
| 572 | + return $texte; |
|
| 573 | + } |
|
| 574 | + |
|
| 575 | + // ne pas relancer si on vient de lancer dans la meme seconde par un hit concurent |
|
| 576 | + if (file_exists($lock = _DIR_TMP . 'cron.lock') && !(@filemtime($lock) < $_SERVER['REQUEST_TIME'])) { |
|
| 577 | + return $texte; |
|
| 578 | + } |
|
| 579 | + |
|
| 580 | + @touch($lock); |
|
| 581 | + |
|
| 582 | + // il y a des taches en attentes |
|
| 583 | + // si depuis plus de 5min, on essaye de lancer le cron par tous les moyens pour rattraper le coup |
|
| 584 | + // on est sans doute sur un site qui n'autorise pas http sortant ou avec peu de trafic |
|
| 585 | + $urgent = false; |
|
| 586 | + if ($time_to_next < -300) { |
|
| 587 | + $urgent = true; |
|
| 588 | + } |
|
| 589 | + |
|
| 590 | + $url_cron = generer_url_action('cron', '', false, true); |
|
| 591 | + |
|
| 592 | + if (!defined('_HTML_BG_CRON_FORCE') || !_HTML_BG_CRON_FORCE) { |
|
| 593 | + if (queue_lancer_url_http_async($url_cron) && !$urgent) { |
|
| 594 | + return $texte; |
|
| 595 | + } |
|
| 596 | + } |
|
| 597 | + |
|
| 598 | + // si deja force, on retourne sans rien |
|
| 599 | + if (defined('_DIRECT_CRON_FORCE')) { |
|
| 600 | + return $texte; |
|
| 601 | + } |
|
| 602 | + |
|
| 603 | + // si c'est un bot |
|
| 604 | + // inutile de faire un appel par image background, |
|
| 605 | + // on force un appel direct en fin de hit |
|
| 606 | + if ((defined('_IS_BOT') && _IS_BOT)) { |
|
| 607 | + define('_DIRECT_CRON_FORCE', true); |
|
| 608 | + |
|
| 609 | + return $texte; |
|
| 610 | + } |
|
| 611 | + |
|
| 612 | + if (!defined('_HTML_BG_CRON_INHIB') || !_HTML_BG_CRON_INHIB) { |
|
| 613 | + // en derniere solution, on insere un appel xhr non bloquant ou une image background dans la page si pas de JS |
|
| 614 | + $url_cron = generer_url_action('cron'); |
|
| 615 | + $texte = '<!-- SPIP-CRON -->' |
|
| 616 | + . "<script>setTimeout(function(){var xo = new XMLHttpRequest();xo.open('GET', '$url_cron', true);xo.send('');},100);</script>" |
|
| 617 | + . "<noscript><div style=\"background-image: url('$url_cron');\"></div></noscript>"; |
|
| 618 | + } |
|
| 619 | + |
|
| 620 | + return $texte; |
|
| 621 | 621 | } |
| 622 | 622 | |
| 623 | 623 | /** |
@@ -626,73 +626,73 @@ discard block |
||
| 626 | 626 | * @return bool : true si l'url a pu être appelée en asynchrone, false sinon |
| 627 | 627 | */ |
| 628 | 628 | function queue_lancer_url_http_async($url_cron) { |
| 629 | - // methode la plus rapide : |
|
| 630 | - // Si fsockopen est possible, on lance le cron via un socket en asynchrone |
|
| 631 | - // si fsockopen echoue (disponibilite serveur, firewall) on essaye pas cURL |
|
| 632 | - // car on a toutes les chances d'echouer pareil mais sans moyen de le savoir |
|
| 633 | - // mais on renvoie false direct |
|
| 634 | - if (function_exists('fsockopen')) { |
|
| 635 | - $parts = parse_url($url_cron); |
|
| 636 | - |
|
| 637 | - switch ($parts['scheme']) { |
|
| 638 | - case 'https': |
|
| 639 | - $scheme = 'ssl://'; |
|
| 640 | - $port = 443; |
|
| 641 | - break; |
|
| 642 | - case 'http': |
|
| 643 | - default: |
|
| 644 | - $scheme = ''; |
|
| 645 | - $port = 80; |
|
| 646 | - } |
|
| 647 | - $fp = @fsockopen( |
|
| 648 | - $scheme . $parts['host'], |
|
| 649 | - $parts['port'] ?? $port, |
|
| 650 | - $errno, |
|
| 651 | - $errstr, |
|
| 652 | - 1 |
|
| 653 | - ); |
|
| 654 | - |
|
| 655 | - if ($fp) { |
|
| 656 | - $host_sent = $parts['host']; |
|
| 657 | - if (isset($parts['port']) && $parts['port'] !== $port) { |
|
| 658 | - $host_sent .= ':' . $parts['port']; |
|
| 659 | - } |
|
| 660 | - $timeout = 200; // ms |
|
| 661 | - stream_set_timeout($fp, 0, $timeout * 1000); |
|
| 662 | - $query = $parts['path'] . ($parts['query'] ? '?' . $parts['query'] : ''); |
|
| 663 | - $out = 'GET ' . $query . " HTTP/1.1\r\n"; |
|
| 664 | - $out .= 'Host: ' . $host_sent . "\r\n"; |
|
| 665 | - $out .= "Connection: Close\r\n\r\n"; |
|
| 666 | - fwrite($fp, $out); |
|
| 667 | - spip_timer('read'); |
|
| 668 | - $t = 0; |
|
| 669 | - // on lit la reponse si possible pour fermer proprement la connexion |
|
| 670 | - // avec un timeout total de 200ms pour ne pas se bloquer |
|
| 671 | - while (!feof($fp) && $t < $timeout) { |
|
| 672 | - @fgets($fp, 1024); |
|
| 673 | - $t += spip_timer('read', true); |
|
| 674 | - spip_timer('read'); |
|
| 675 | - } |
|
| 676 | - fclose($fp); |
|
| 677 | - return true; |
|
| 678 | - } |
|
| 679 | - } |
|
| 680 | - // si fsockopen n'est pas dispo on essaye cURL : |
|
| 681 | - // lancer le cron par un cURL asynchrone si cURL est present |
|
| 682 | - elseif (function_exists('curl_init')) { |
|
| 683 | - //setting the curl parameters. |
|
| 684 | - $ch = curl_init($url_cron); |
|
| 685 | - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
| 686 | - // cf bug : http://www.php.net/manual/en/function.curl-setopt.php#104597 |
|
| 687 | - curl_setopt($ch, CURLOPT_NOSIGNAL, 1); |
|
| 688 | - // valeur mini pour que la requete soit lancee |
|
| 689 | - curl_setopt($ch, CURLOPT_TIMEOUT_MS, 200); |
|
| 690 | - // lancer |
|
| 691 | - curl_exec($ch); |
|
| 692 | - // fermer |
|
| 693 | - curl_close($ch); |
|
| 694 | - return true; |
|
| 695 | - } |
|
| 696 | - |
|
| 697 | - return false; |
|
| 629 | + // methode la plus rapide : |
|
| 630 | + // Si fsockopen est possible, on lance le cron via un socket en asynchrone |
|
| 631 | + // si fsockopen echoue (disponibilite serveur, firewall) on essaye pas cURL |
|
| 632 | + // car on a toutes les chances d'echouer pareil mais sans moyen de le savoir |
|
| 633 | + // mais on renvoie false direct |
|
| 634 | + if (function_exists('fsockopen')) { |
|
| 635 | + $parts = parse_url($url_cron); |
|
| 636 | + |
|
| 637 | + switch ($parts['scheme']) { |
|
| 638 | + case 'https': |
|
| 639 | + $scheme = 'ssl://'; |
|
| 640 | + $port = 443; |
|
| 641 | + break; |
|
| 642 | + case 'http': |
|
| 643 | + default: |
|
| 644 | + $scheme = ''; |
|
| 645 | + $port = 80; |
|
| 646 | + } |
|
| 647 | + $fp = @fsockopen( |
|
| 648 | + $scheme . $parts['host'], |
|
| 649 | + $parts['port'] ?? $port, |
|
| 650 | + $errno, |
|
| 651 | + $errstr, |
|
| 652 | + 1 |
|
| 653 | + ); |
|
| 654 | + |
|
| 655 | + if ($fp) { |
|
| 656 | + $host_sent = $parts['host']; |
|
| 657 | + if (isset($parts['port']) && $parts['port'] !== $port) { |
|
| 658 | + $host_sent .= ':' . $parts['port']; |
|
| 659 | + } |
|
| 660 | + $timeout = 200; // ms |
|
| 661 | + stream_set_timeout($fp, 0, $timeout * 1000); |
|
| 662 | + $query = $parts['path'] . ($parts['query'] ? '?' . $parts['query'] : ''); |
|
| 663 | + $out = 'GET ' . $query . " HTTP/1.1\r\n"; |
|
| 664 | + $out .= 'Host: ' . $host_sent . "\r\n"; |
|
| 665 | + $out .= "Connection: Close\r\n\r\n"; |
|
| 666 | + fwrite($fp, $out); |
|
| 667 | + spip_timer('read'); |
|
| 668 | + $t = 0; |
|
| 669 | + // on lit la reponse si possible pour fermer proprement la connexion |
|
| 670 | + // avec un timeout total de 200ms pour ne pas se bloquer |
|
| 671 | + while (!feof($fp) && $t < $timeout) { |
|
| 672 | + @fgets($fp, 1024); |
|
| 673 | + $t += spip_timer('read', true); |
|
| 674 | + spip_timer('read'); |
|
| 675 | + } |
|
| 676 | + fclose($fp); |
|
| 677 | + return true; |
|
| 678 | + } |
|
| 679 | + } |
|
| 680 | + // si fsockopen n'est pas dispo on essaye cURL : |
|
| 681 | + // lancer le cron par un cURL asynchrone si cURL est present |
|
| 682 | + elseif (function_exists('curl_init')) { |
|
| 683 | + //setting the curl parameters. |
|
| 684 | + $ch = curl_init($url_cron); |
|
| 685 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
| 686 | + // cf bug : http://www.php.net/manual/en/function.curl-setopt.php#104597 |
|
| 687 | + curl_setopt($ch, CURLOPT_NOSIGNAL, 1); |
|
| 688 | + // valeur mini pour que la requete soit lancee |
|
| 689 | + curl_setopt($ch, CURLOPT_TIMEOUT_MS, 200); |
|
| 690 | + // lancer |
|
| 691 | + curl_exec($ch); |
|
| 692 | + // fermer |
|
| 693 | + curl_close($ch); |
|
| 694 | + return true; |
|
| 695 | + } |
|
| 696 | + |
|
| 697 | + return false; |
|
| 698 | 698 | } |
@@ -69,7 +69,7 @@ discard block |
||
| 69 | 69 | $md5args = md5($arguments); |
| 70 | 70 | |
| 71 | 71 | // si pas de date programee, des que possible |
| 72 | - $duplicate_where = 'status=' . intval(_JQ_SCHEDULED) . ' AND '; |
|
| 72 | + $duplicate_where = 'status='.intval(_JQ_SCHEDULED).' AND '; |
|
| 73 | 73 | if (!$time) { |
| 74 | 74 | $time = time(); |
| 75 | 75 | $duplicate_where = ''; // ne pas dupliquer si deja le meme job en cours d'execution |
@@ -94,9 +94,8 @@ discard block |
||
| 94 | 94 | 'id_job', |
| 95 | 95 | 'spip_jobs', |
| 96 | 96 | $duplicate_where = |
| 97 | - $duplicate_where . 'fonction=' . sql_quote($function) |
|
| 98 | - . (($no_duplicate === 'function_only') ? '' : |
|
| 99 | - ' AND md5args=' . sql_quote($md5args) . ' AND inclure=' . sql_quote($file)) |
|
| 97 | + $duplicate_where.'fonction='.sql_quote($function) |
|
| 98 | + . (($no_duplicate === 'function_only') ? '' : ' AND md5args='.sql_quote($md5args).' AND inclure='.sql_quote($file)) |
|
| 100 | 99 | )) |
| 101 | 100 | ) { |
| 102 | 101 | return $id_job; |
@@ -108,9 +107,9 @@ discard block |
||
| 108 | 107 | // doit s'effacer |
| 109 | 108 | if ( |
| 110 | 109 | $no_duplicate |
| 111 | - && ($id_prev = sql_getfetsel('id_job', 'spip_jobs', 'id_job<' . intval($id_job) . " AND $duplicate_where")) |
|
| 110 | + && ($id_prev = sql_getfetsel('id_job', 'spip_jobs', 'id_job<'.intval($id_job)." AND $duplicate_where")) |
|
| 112 | 111 | ) { |
| 113 | - sql_delete('spip_jobs', 'id_job=' . intval($id_job)); |
|
| 112 | + sql_delete('spip_jobs', 'id_job='.intval($id_job)); |
|
| 114 | 113 | |
| 115 | 114 | return $id_prev; |
| 116 | 115 | } |
@@ -122,10 +121,10 @@ discard block |
||
| 122 | 121 | // ie cas d'un char non acceptables sur certains type de champs |
| 123 | 122 | // qui coupe la valeur |
| 124 | 123 | if (defined('_JQ_INSERT_CHECK_ARGS') && $id_job) { |
| 125 | - $args = sql_getfetsel('args', 'spip_jobs', 'id_job=' . intval($id_job)); |
|
| 124 | + $args = sql_getfetsel('args', 'spip_jobs', 'id_job='.intval($id_job)); |
|
| 126 | 125 | if ($args !== $arguments) { |
| 127 | 126 | spip_logger('queue')->info( |
| 128 | - 'arguments job errones / longueur ' . strlen($args) . ' vs ' . strlen($arguments) . ' / valeur : ' |
|
| 127 | + 'arguments job errones / longueur '.strlen($args).' vs '.strlen($arguments).' / valeur : ' |
|
| 129 | 128 | . var_export($arguments, true) |
| 130 | 129 | ); |
| 131 | 130 | } |
@@ -154,7 +153,7 @@ discard block |
||
| 154 | 153 | function queue_purger() { |
| 155 | 154 | include_spip('base/abstract_sql'); |
| 156 | 155 | sql_delete('spip_jobs'); |
| 157 | - sql_delete('spip_jobs_liens', 'id_job NOT IN (' . sql_get_select('id_job', 'spip_jobs') . ')'); |
|
| 156 | + sql_delete('spip_jobs_liens', 'id_job NOT IN ('.sql_get_select('id_job', 'spip_jobs').')'); |
|
| 158 | 157 | include_spip('inc/genie'); |
| 159 | 158 | genie_queue_watch_dist(); |
| 160 | 159 | } |
@@ -170,8 +169,8 @@ discard block |
||
| 170 | 169 | include_spip('base/abstract_sql'); |
| 171 | 170 | |
| 172 | 171 | if ( |
| 173 | - ($row = sql_fetsel('fonction,inclure,date', 'spip_jobs', 'id_job=' . intval($id_job))) |
|
| 174 | - && ($res = sql_delete('spip_jobs', 'id_job=' . intval($id_job))) |
|
| 172 | + ($row = sql_fetsel('fonction,inclure,date', 'spip_jobs', 'id_job='.intval($id_job))) |
|
| 173 | + && ($res = sql_delete('spip_jobs', 'id_job='.intval($id_job))) |
|
| 175 | 174 | ) { |
| 176 | 175 | queue_unlink_job($id_job); |
| 177 | 176 | // est-ce une tache cron qu'il faut relancer ? |
@@ -221,7 +220,7 @@ discard block |
||
| 221 | 220 | * resultat du sql_delete |
| 222 | 221 | */ |
| 223 | 222 | function queue_unlink_job($id_job) { |
| 224 | - return sql_delete('spip_jobs_liens', 'id_job=' . intval($id_job)); |
|
| 223 | + return sql_delete('spip_jobs_liens', 'id_job='.intval($id_job)); |
|
| 225 | 224 | } |
| 226 | 225 | |
| 227 | 226 | /** |
@@ -239,7 +238,7 @@ discard block |
||
| 239 | 238 | // deserialiser les arguments |
| 240 | 239 | $args = unserialize($row['args']); |
| 241 | 240 | if (!is_array($args)) { |
| 242 | - $logger->info('arguments job errones ' . var_export($row, true)); |
|
| 241 | + $logger->info('arguments job errones '.var_export($row, true)); |
|
| 243 | 242 | return false; |
| 244 | 243 | } |
| 245 | 244 | |
@@ -256,14 +255,14 @@ discard block |
||
| 256 | 255 | } |
| 257 | 256 | |
| 258 | 257 | if (!function_exists($fonction)) { |
| 259 | - $logger->info("fonction $fonction ($inclure) inexistante " . var_export($row, true)); |
|
| 258 | + $logger->info("fonction $fonction ($inclure) inexistante ".var_export($row, true)); |
|
| 260 | 259 | |
| 261 | 260 | return false; |
| 262 | 261 | } |
| 263 | 262 | |
| 264 | - $logger->info('queue [' . $row['id_job'] . "]: $fonction() start"); |
|
| 263 | + $logger->info('queue ['.$row['id_job']."]: $fonction() start"); |
|
| 265 | 264 | $res = $fonction(...$args); |
| 266 | - $logger->info('queue [' . $row['id_job'] . "]: $fonction() end"); |
|
| 265 | + $logger->info('queue ['.$row['id_job']."]: $fonction() end"); |
|
| 267 | 266 | |
| 268 | 267 | return $res; |
| 269 | 268 | } |
@@ -337,19 +336,19 @@ discard block |
||
| 337 | 336 | // lorsqu'un job cron n'a pas fini, sa priorite est descendue |
| 338 | 337 | // pour qu'il ne bloque pas les autres jobs en attente |
| 339 | 338 | if (is_array($force_jobs) && count($force_jobs)) { |
| 340 | - $cond = 'status=' . intval(_JQ_SCHEDULED) . ' AND ' . sql_in('id_job', $force_jobs); |
|
| 339 | + $cond = 'status='.intval(_JQ_SCHEDULED).' AND '.sql_in('id_job', $force_jobs); |
|
| 341 | 340 | } else { |
| 342 | 341 | $now = date('Y-m-d H:i:s', $time); |
| 343 | - $cond = 'status=' . intval(_JQ_SCHEDULED) . ' AND date<=' . sql_quote($now); |
|
| 342 | + $cond = 'status='.intval(_JQ_SCHEDULED).' AND date<='.sql_quote($now); |
|
| 344 | 343 | } |
| 345 | 344 | |
| 346 | 345 | register_shutdown_function('queue_error_handler'); // recuperer les erreurs auant que possible |
| 347 | - $res = sql_allfetsel('*', 'spip_jobs', $cond, '', 'priorite DESC,date', '0,' . (_JQ_MAX_JOBS_EXECUTE + 1)); |
|
| 346 | + $res = sql_allfetsel('*', 'spip_jobs', $cond, '', 'priorite DESC,date', '0,'.(_JQ_MAX_JOBS_EXECUTE + 1)); |
|
| 348 | 347 | do { |
| 349 | 348 | if ($row = array_shift($res)) { |
| 350 | 349 | $nbj++; |
| 351 | 350 | // il faut un verrou, a base de sql_delete |
| 352 | - if (sql_delete('spip_jobs', 'id_job=' . intval($row['id_job']) . ' AND status=' . intval(_JQ_SCHEDULED))) { |
|
| 351 | + if (sql_delete('spip_jobs', 'id_job='.intval($row['id_job']).' AND status='.intval(_JQ_SCHEDULED))) { |
|
| 353 | 352 | #$logger->info("JQ schedule job ".$nbj." OK"); |
| 354 | 353 | // on reinsert dans la base aussitot avec un status=_JQ_PENDING |
| 355 | 354 | $row['status'] = _JQ_PENDING; |
@@ -364,9 +363,9 @@ discard block |
||
| 364 | 363 | queue_close_job($row, $time, $result); |
| 365 | 364 | } |
| 366 | 365 | } |
| 367 | - $logger->debug('JQ schedule job end time ' . $time); |
|
| 366 | + $logger->debug('JQ schedule job end time '.$time); |
|
| 368 | 367 | } while ($nbj < _JQ_MAX_JOBS_EXECUTE && $row && $time < $end_time); |
| 369 | - $logger->debug('JQ schedule end time ' . time()); |
|
| 368 | + $logger->debug('JQ schedule end time '.time()); |
|
| 370 | 369 | |
| 371 | 370 | if ($row = array_shift($res)) { |
| 372 | 371 | queue_update_next_job_time(0); // on sait qu'il y a encore des jobs a lancer ASAP |
@@ -405,9 +404,9 @@ discard block |
||
| 405 | 404 | } |
| 406 | 405 | } |
| 407 | 406 | // purger ses liens eventuels avec des objets |
| 408 | - sql_delete('spip_jobs_liens', 'id_job=' . intval($row['id_job'])); |
|
| 407 | + sql_delete('spip_jobs_liens', 'id_job='.intval($row['id_job'])); |
|
| 409 | 408 | // supprimer le job fini |
| 410 | - sql_delete('spip_jobs', 'id_job=' . intval($row['id_job'])); |
|
| 409 | + sql_delete('spip_jobs', 'id_job='.intval($row['id_job'])); |
|
| 411 | 410 | } |
| 412 | 411 | |
| 413 | 412 | /** |
@@ -480,18 +479,18 @@ discard block |
||
| 480 | 479 | $res = sql_allfetsel( |
| 481 | 480 | '*', |
| 482 | 481 | 'spip_jobs', |
| 483 | - 'status=' . intval(_JQ_PENDING) . ' AND date<' . sql_quote(date('Y-m-d H:i:s', $time - 180)) |
|
| 482 | + 'status='.intval(_JQ_PENDING).' AND date<'.sql_quote(date('Y-m-d H:i:s', $time - 180)) |
|
| 484 | 483 | ); |
| 485 | 484 | if (is_array($res)) { |
| 486 | 485 | foreach ($res as $row) { |
| 487 | 486 | queue_close_job($row, $time); |
| 488 | - spip_logger('job_mort')->error('queue_close_job car _JQ_PENDING depuis +180s : ' . print_r($row, true)); |
|
| 487 | + spip_logger('job_mort')->error('queue_close_job car _JQ_PENDING depuis +180s : '.print_r($row, true)); |
|
| 489 | 488 | } |
| 490 | 489 | } |
| 491 | 490 | |
| 492 | 491 | // chercher la date du prochain job si pas connu |
| 493 | 492 | if (is_null($next) || is_null(queue_sleep_time_to_next_job())) { |
| 494 | - $date = sql_getfetsel('date', 'spip_jobs', 'status=' . intval(_JQ_SCHEDULED), '', 'date', '0,1'); |
|
| 493 | + $date = sql_getfetsel('date', 'spip_jobs', 'status='.intval(_JQ_SCHEDULED), '', 'date', '0,1'); |
|
| 495 | 494 | $next = strtotime($date); |
| 496 | 495 | } |
| 497 | 496 | if (!is_null($next_time)) { |
@@ -504,7 +503,7 @@ discard block |
||
| 504 | 503 | if (is_null($nb_jobs_scheduled)) { |
| 505 | 504 | $nb_jobs_scheduled = sql_countsel( |
| 506 | 505 | 'spip_jobs', |
| 507 | - 'status=' . intval(_JQ_SCHEDULED) . ' AND date<' . sql_quote(date('Y-m-d H:i:s', $time)) |
|
| 506 | + 'status='.intval(_JQ_SCHEDULED).' AND date<'.sql_quote(date('Y-m-d H:i:s', $time)) |
|
| 508 | 507 | ); |
| 509 | 508 | } elseif ($next <= $time) { |
| 510 | 509 | $nb_jobs_scheduled++; |
@@ -573,7 +572,7 @@ discard block |
||
| 573 | 572 | } |
| 574 | 573 | |
| 575 | 574 | // ne pas relancer si on vient de lancer dans la meme seconde par un hit concurent |
| 576 | - if (file_exists($lock = _DIR_TMP . 'cron.lock') && !(@filemtime($lock) < $_SERVER['REQUEST_TIME'])) { |
|
| 575 | + if (file_exists($lock = _DIR_TMP.'cron.lock') && !(@filemtime($lock) < $_SERVER['REQUEST_TIME'])) { |
|
| 577 | 576 | return $texte; |
| 578 | 577 | } |
| 579 | 578 | |
@@ -645,7 +644,7 @@ discard block |
||
| 645 | 644 | $port = 80; |
| 646 | 645 | } |
| 647 | 646 | $fp = @fsockopen( |
| 648 | - $scheme . $parts['host'], |
|
| 647 | + $scheme.$parts['host'], |
|
| 649 | 648 | $parts['port'] ?? $port, |
| 650 | 649 | $errno, |
| 651 | 650 | $errstr, |
@@ -655,13 +654,13 @@ discard block |
||
| 655 | 654 | if ($fp) { |
| 656 | 655 | $host_sent = $parts['host']; |
| 657 | 656 | if (isset($parts['port']) && $parts['port'] !== $port) { |
| 658 | - $host_sent .= ':' . $parts['port']; |
|
| 657 | + $host_sent .= ':'.$parts['port']; |
|
| 659 | 658 | } |
| 660 | 659 | $timeout = 200; // ms |
| 661 | 660 | stream_set_timeout($fp, 0, $timeout * 1000); |
| 662 | - $query = $parts['path'] . ($parts['query'] ? '?' . $parts['query'] : ''); |
|
| 663 | - $out = 'GET ' . $query . " HTTP/1.1\r\n"; |
|
| 664 | - $out .= 'Host: ' . $host_sent . "\r\n"; |
|
| 661 | + $query = $parts['path'].($parts['query'] ? '?'.$parts['query'] : ''); |
|
| 662 | + $out = 'GET '.$query." HTTP/1.1\r\n"; |
|
| 663 | + $out .= 'Host: '.$host_sent."\r\n"; |
|
| 665 | 664 | $out .= "Connection: Close\r\n\r\n"; |
| 666 | 665 | fwrite($fp, $out); |
| 667 | 666 | spip_timer('read'); |
@@ -16,12 +16,12 @@ discard block |
||
| 16 | 16 | **/ |
| 17 | 17 | |
| 18 | 18 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 19 | - return; |
|
| 19 | + return; |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | /** l'adresse du repertoire de telechargement et de decompactage des plugins */ |
| 23 | 23 | if (!defined('_DIR_PLUGINS_AUTO')) { |
| 24 | - define('_DIR_PLUGINS_AUTO', _DIR_PLUGINS . 'auto/'); |
|
| 24 | + define('_DIR_PLUGINS_AUTO', _DIR_PLUGINS . 'auto/'); |
|
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | #include_spip('inc/texte'); // ????? Appelle public/parametrer trop tot avant la reconstruction du chemin des plugins. |
@@ -46,27 +46,27 @@ discard block |
||
| 46 | 46 | * @return array |
| 47 | 47 | **/ |
| 48 | 48 | function liste_plugin_files($dir_plugins = null) { |
| 49 | - static $plugin_files = []; |
|
| 50 | - if (is_null($dir_plugins)) { |
|
| 51 | - $dir_plugins = _DIR_PLUGINS; |
|
| 52 | - } |
|
| 53 | - if ( |
|
| 54 | - !isset($plugin_files[$dir_plugins]) |
|
| 55 | - || (is_countable($plugin_files[$dir_plugins]) ? count($plugin_files[$dir_plugins]) : 0) == 0 |
|
| 56 | - ) { |
|
| 57 | - $plugin_files[$dir_plugins] = []; |
|
| 58 | - foreach (fast_find_plugin_dirs($dir_plugins) as $plugin) { |
|
| 59 | - $plugin_files[$dir_plugins][] = substr($plugin, strlen($dir_plugins)); |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - sort($plugin_files[$dir_plugins]); |
|
| 63 | - // et on lit le XML de tous les plugins pour le mettre en cache |
|
| 64 | - // et en profiter pour nettoyer ceux qui n'existent plus du cache |
|
| 65 | - $get_infos = charger_fonction('get_infos', 'plugins'); |
|
| 66 | - $get_infos($plugin_files[$dir_plugins], false, $dir_plugins, true); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - return $plugin_files[$dir_plugins]; |
|
| 49 | + static $plugin_files = []; |
|
| 50 | + if (is_null($dir_plugins)) { |
|
| 51 | + $dir_plugins = _DIR_PLUGINS; |
|
| 52 | + } |
|
| 53 | + if ( |
|
| 54 | + !isset($plugin_files[$dir_plugins]) |
|
| 55 | + || (is_countable($plugin_files[$dir_plugins]) ? count($plugin_files[$dir_plugins]) : 0) == 0 |
|
| 56 | + ) { |
|
| 57 | + $plugin_files[$dir_plugins] = []; |
|
| 58 | + foreach (fast_find_plugin_dirs($dir_plugins) as $plugin) { |
|
| 59 | + $plugin_files[$dir_plugins][] = substr($plugin, strlen($dir_plugins)); |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + sort($plugin_files[$dir_plugins]); |
|
| 63 | + // et on lit le XML de tous les plugins pour le mettre en cache |
|
| 64 | + // et en profiter pour nettoyer ceux qui n'existent plus du cache |
|
| 65 | + $get_infos = charger_fonction('get_infos', 'plugins'); |
|
| 66 | + $get_infos($plugin_files[$dir_plugins], false, $dir_plugins, true); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + return $plugin_files[$dir_plugins]; |
|
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | /** |
@@ -82,44 +82,44 @@ discard block |
||
| 82 | 82 | * Liste complète des répeertoires |
| 83 | 83 | **/ |
| 84 | 84 | function fast_find_plugin_dirs($dir, $max_prof = 100) { |
| 85 | - $fichiers = []; |
|
| 86 | - // revenir au repertoire racine si on a recu dossier/truc |
|
| 87 | - // pour regarder dossier/truc/ ne pas oublier le / final |
|
| 88 | - $dir = preg_replace(',/[^/]*$,', '', $dir); |
|
| 89 | - if ($dir == '') { |
|
| 90 | - $dir = '.'; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - if (!is_dir($dir)) { |
|
| 94 | - return $fichiers; |
|
| 95 | - } |
|
| 96 | - if (is_plugin_dir($dir, '')) { |
|
| 97 | - $fichiers[] = $dir; |
|
| 98 | - |
|
| 99 | - return $fichiers; |
|
| 100 | - } |
|
| 101 | - if ($max_prof <= 0) { |
|
| 102 | - return $fichiers; |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - $subdirs = []; |
|
| 106 | - if (@is_dir($dir) && is_readable($dir) && ($d = opendir($dir))) { |
|
| 107 | - while (($f = readdir($d)) !== false) { |
|
| 108 | - if ( |
|
| 109 | - $f[0] != '.' |
|
| 110 | - && is_dir($f = "$dir/$f") |
|
| 111 | - ) { |
|
| 112 | - $subdirs[] = $f; |
|
| 113 | - } |
|
| 114 | - } |
|
| 115 | - closedir($d); |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - foreach ($subdirs as $d) { |
|
| 119 | - $fichiers = array_merge($fichiers, fast_find_plugin_dirs("$d/", $max_prof - 1)); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - return $fichiers; |
|
| 85 | + $fichiers = []; |
|
| 86 | + // revenir au repertoire racine si on a recu dossier/truc |
|
| 87 | + // pour regarder dossier/truc/ ne pas oublier le / final |
|
| 88 | + $dir = preg_replace(',/[^/]*$,', '', $dir); |
|
| 89 | + if ($dir == '') { |
|
| 90 | + $dir = '.'; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + if (!is_dir($dir)) { |
|
| 94 | + return $fichiers; |
|
| 95 | + } |
|
| 96 | + if (is_plugin_dir($dir, '')) { |
|
| 97 | + $fichiers[] = $dir; |
|
| 98 | + |
|
| 99 | + return $fichiers; |
|
| 100 | + } |
|
| 101 | + if ($max_prof <= 0) { |
|
| 102 | + return $fichiers; |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + $subdirs = []; |
|
| 106 | + if (@is_dir($dir) && is_readable($dir) && ($d = opendir($dir))) { |
|
| 107 | + while (($f = readdir($d)) !== false) { |
|
| 108 | + if ( |
|
| 109 | + $f[0] != '.' |
|
| 110 | + && is_dir($f = "$dir/$f") |
|
| 111 | + ) { |
|
| 112 | + $subdirs[] = $f; |
|
| 113 | + } |
|
| 114 | + } |
|
| 115 | + closedir($d); |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + foreach ($subdirs as $d) { |
|
| 119 | + $fichiers = array_merge($fichiers, fast_find_plugin_dirs("$d/", $max_prof - 1)); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + return $fichiers; |
|
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | /** |
@@ -140,27 +140,27 @@ discard block |
||
| 140 | 140 | **/ |
| 141 | 141 | function is_plugin_dir($dir, $dir_plugins = null) { |
| 142 | 142 | |
| 143 | - if (is_array($dir)) { |
|
| 144 | - foreach ($dir as $k => $d) { |
|
| 145 | - if (!is_plugin_dir($d, $dir_plugins)) { |
|
| 146 | - unset($dir[$k]); |
|
| 147 | - } |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - return $dir; |
|
| 151 | - } |
|
| 152 | - if (is_null($dir_plugins)) { |
|
| 153 | - $dir_plugins = _DIR_PLUGINS; |
|
| 154 | - } |
|
| 155 | - $search = ["$dir_plugins$dir/paquet.xml"]; |
|
| 156 | - |
|
| 157 | - foreach ($search as $s) { |
|
| 158 | - if (file_exists($s)) { |
|
| 159 | - return $dir; |
|
| 160 | - } |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - return ''; |
|
| 143 | + if (is_array($dir)) { |
|
| 144 | + foreach ($dir as $k => $d) { |
|
| 145 | + if (!is_plugin_dir($d, $dir_plugins)) { |
|
| 146 | + unset($dir[$k]); |
|
| 147 | + } |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + return $dir; |
|
| 151 | + } |
|
| 152 | + if (is_null($dir_plugins)) { |
|
| 153 | + $dir_plugins = _DIR_PLUGINS; |
|
| 154 | + } |
|
| 155 | + $search = ["$dir_plugins$dir/paquet.xml"]; |
|
| 156 | + |
|
| 157 | + foreach ($search as $s) { |
|
| 158 | + if (file_exists($s)) { |
|
| 159 | + return $dir; |
|
| 160 | + } |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + return ''; |
|
| 164 | 164 | } |
| 165 | 165 | |
| 166 | 166 | /** Regexp d'extraction des informations d'un intervalle de compatibilité */ |
@@ -187,51 +187,51 @@ discard block |
||
| 187 | 187 | **/ |
| 188 | 188 | function plugin_version_compatible($intervalle, $version, $avec_quoi = '') { |
| 189 | 189 | |
| 190 | - if (!strlen($intervalle)) { |
|
| 191 | - return true; |
|
| 192 | - } |
|
| 193 | - if (!preg_match(_EXTRAIRE_INTERVALLE, $intervalle, $regs)) { |
|
| 194 | - return false; |
|
| 195 | - } |
|
| 196 | - // Extraction des bornes et traitement de * pour la borne sup : |
|
| 197 | - // -- on autorise uniquement les ecritures 3.0.*, 3.* |
|
| 198 | - $minimum = $regs[1]; |
|
| 199 | - $maximum = $regs[2]; |
|
| 200 | - |
|
| 201 | - // si une version SPIP de compatibilité a été définie (dans |
|
| 202 | - // mes_options.php, sous la forme : define('_DEV_VERSION_SPIP_COMPAT', '3.1.0'); |
|
| 203 | - // on l'utilise (phase de dev, de test...) mais *que* en cas de comparaison |
|
| 204 | - // avec la version de SPIP (ne nuit donc pas aux tests de necessite |
|
| 205 | - // entre plugins) |
|
| 206 | - if (defined('_DEV_VERSION_SPIP_COMPAT') && $avec_quoi == 'spip' && $version !== _DEV_VERSION_SPIP_COMPAT) { |
|
| 207 | - if (plugin_version_compatible($intervalle, _DEV_VERSION_SPIP_COMPAT, $avec_quoi)) { |
|
| 208 | - return true; |
|
| 209 | - } |
|
| 210 | - // si pas de compatibilite avec _DEV_VERSION_SPIP_COMPAT, on essaye quand meme avec la vrai version |
|
| 211 | - // cas du plugin qui n'est compatible qu'avec cette nouvelle version |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - $minimum_inc = $intervalle[0] == '['; |
|
| 215 | - $maximum_inc = str_ends_with($intervalle, ']'); |
|
| 216 | - |
|
| 217 | - if (strlen($minimum)) { |
|
| 218 | - if ($minimum_inc && spip_version_compare($version, $minimum, '<')) { |
|
| 219 | - return false; |
|
| 220 | - } |
|
| 221 | - if (!$minimum_inc && spip_version_compare($version, $minimum, '<=')) { |
|
| 222 | - return false; |
|
| 223 | - } |
|
| 224 | - } |
|
| 225 | - if (strlen($maximum)) { |
|
| 226 | - if ($maximum_inc && spip_version_compare($version, $maximum, '>')) { |
|
| 227 | - return false; |
|
| 228 | - } |
|
| 229 | - if (!$maximum_inc && spip_version_compare($version, $maximum, '>=')) { |
|
| 230 | - return false; |
|
| 231 | - } |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - return true; |
|
| 190 | + if (!strlen($intervalle)) { |
|
| 191 | + return true; |
|
| 192 | + } |
|
| 193 | + if (!preg_match(_EXTRAIRE_INTERVALLE, $intervalle, $regs)) { |
|
| 194 | + return false; |
|
| 195 | + } |
|
| 196 | + // Extraction des bornes et traitement de * pour la borne sup : |
|
| 197 | + // -- on autorise uniquement les ecritures 3.0.*, 3.* |
|
| 198 | + $minimum = $regs[1]; |
|
| 199 | + $maximum = $regs[2]; |
|
| 200 | + |
|
| 201 | + // si une version SPIP de compatibilité a été définie (dans |
|
| 202 | + // mes_options.php, sous la forme : define('_DEV_VERSION_SPIP_COMPAT', '3.1.0'); |
|
| 203 | + // on l'utilise (phase de dev, de test...) mais *que* en cas de comparaison |
|
| 204 | + // avec la version de SPIP (ne nuit donc pas aux tests de necessite |
|
| 205 | + // entre plugins) |
|
| 206 | + if (defined('_DEV_VERSION_SPIP_COMPAT') && $avec_quoi == 'spip' && $version !== _DEV_VERSION_SPIP_COMPAT) { |
|
| 207 | + if (plugin_version_compatible($intervalle, _DEV_VERSION_SPIP_COMPAT, $avec_quoi)) { |
|
| 208 | + return true; |
|
| 209 | + } |
|
| 210 | + // si pas de compatibilite avec _DEV_VERSION_SPIP_COMPAT, on essaye quand meme avec la vrai version |
|
| 211 | + // cas du plugin qui n'est compatible qu'avec cette nouvelle version |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + $minimum_inc = $intervalle[0] == '['; |
|
| 215 | + $maximum_inc = str_ends_with($intervalle, ']'); |
|
| 216 | + |
|
| 217 | + if (strlen($minimum)) { |
|
| 218 | + if ($minimum_inc && spip_version_compare($version, $minimum, '<')) { |
|
| 219 | + return false; |
|
| 220 | + } |
|
| 221 | + if (!$minimum_inc && spip_version_compare($version, $minimum, '<=')) { |
|
| 222 | + return false; |
|
| 223 | + } |
|
| 224 | + } |
|
| 225 | + if (strlen($maximum)) { |
|
| 226 | + if ($maximum_inc && spip_version_compare($version, $maximum, '>')) { |
|
| 227 | + return false; |
|
| 228 | + } |
|
| 229 | + if (!$maximum_inc && spip_version_compare($version, $maximum, '>=')) { |
|
| 230 | + return false; |
|
| 231 | + } |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + return true; |
|
| 235 | 235 | } |
| 236 | 236 | |
| 237 | 237 | /** |
@@ -248,62 +248,62 @@ discard block |
||
| 248 | 248 | * @return array |
| 249 | 249 | */ |
| 250 | 250 | function liste_plugin_valides($liste_plug, $force = false) { |
| 251 | - $liste_ext = liste_plugin_files(_DIR_PLUGINS_DIST); |
|
| 252 | - $get_infos = charger_fonction('get_infos', 'plugins'); |
|
| 253 | - $infos = [ |
|
| 254 | - // lister les extensions qui sont automatiquement actives |
|
| 255 | - '_DIR_PLUGINS_DIST' => $get_infos($liste_ext, $force, _DIR_PLUGINS_DIST), |
|
| 256 | - '_DIR_PLUGINS' => $get_infos($liste_plug, $force, _DIR_PLUGINS) |
|
| 257 | - ]; |
|
| 258 | - |
|
| 259 | - // creer une premiere liste non ordonnee mais qui ne retient |
|
| 260 | - // que les plugins valides, et dans leur derniere version en cas de doublon |
|
| 261 | - $infos['_DIR_RESTREINT'][''] = $get_infos('./', $force, _DIR_RESTREINT); |
|
| 262 | - $infos['_DIR_RESTREINT']['SPIP']['version'] = $GLOBALS['spip_version_branche']; |
|
| 263 | - $infos['_DIR_RESTREINT']['SPIP']['chemin'] = []; |
|
| 264 | - $liste_non_classee = [ |
|
| 265 | - 'SPIP' => [ |
|
| 266 | - 'nom' => 'SPIP', |
|
| 267 | - 'etat' => 'stable', |
|
| 268 | - 'version' => $GLOBALS['spip_version_branche'], |
|
| 269 | - 'dir_type' => '_DIR_RESTREINT', |
|
| 270 | - 'dir' => '', |
|
| 271 | - ] |
|
| 272 | - ]; |
|
| 273 | - |
|
| 274 | - $invalides = []; |
|
| 275 | - foreach ($liste_ext as $plug) { |
|
| 276 | - if (isset($infos['_DIR_PLUGINS_DIST'][$plug])) { |
|
| 277 | - plugin_valide_resume($liste_non_classee, $plug, $infos, '_DIR_PLUGINS_DIST'); |
|
| 278 | - } |
|
| 279 | - } |
|
| 280 | - foreach ($liste_plug as $plug) { |
|
| 281 | - if (isset($infos['_DIR_PLUGINS'][$plug])) { |
|
| 282 | - $r = plugin_valide_resume($liste_non_classee, $plug, $infos, '_DIR_PLUGINS'); |
|
| 283 | - if (is_array($r)) { |
|
| 284 | - $invalides = array_merge($invalides, $r); |
|
| 285 | - } |
|
| 286 | - } |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - if (defined('_DIR_PLUGINS_SUPPL') && _DIR_PLUGINS_SUPPL) { |
|
| 290 | - $infos['_DIR_PLUGINS_SUPPL'] = $get_infos($liste_plug, false, _DIR_PLUGINS_SUPPL); |
|
| 291 | - foreach ($liste_plug as $plug) { |
|
| 292 | - if (isset($infos['_DIR_PLUGINS_SUPPL'][$plug])) { |
|
| 293 | - $r = plugin_valide_resume($liste_non_classee, $plug, $infos, '_DIR_PLUGINS_SUPPL'); |
|
| 294 | - if (is_array($r)) { |
|
| 295 | - $invalides = array_merge($invalides, $r); |
|
| 296 | - } |
|
| 297 | - } |
|
| 298 | - } |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - plugin_fixer_procure($liste_non_classee, $infos); |
|
| 302 | - |
|
| 303 | - // les plugins qui sont dans $liste_non_classee ne sont pas invalides (on a trouve un autre version valide) |
|
| 304 | - $invalides = array_diff_key($invalides, $liste_non_classee); |
|
| 305 | - |
|
| 306 | - return [$infos, $liste_non_classee, $invalides]; |
|
| 251 | + $liste_ext = liste_plugin_files(_DIR_PLUGINS_DIST); |
|
| 252 | + $get_infos = charger_fonction('get_infos', 'plugins'); |
|
| 253 | + $infos = [ |
|
| 254 | + // lister les extensions qui sont automatiquement actives |
|
| 255 | + '_DIR_PLUGINS_DIST' => $get_infos($liste_ext, $force, _DIR_PLUGINS_DIST), |
|
| 256 | + '_DIR_PLUGINS' => $get_infos($liste_plug, $force, _DIR_PLUGINS) |
|
| 257 | + ]; |
|
| 258 | + |
|
| 259 | + // creer une premiere liste non ordonnee mais qui ne retient |
|
| 260 | + // que les plugins valides, et dans leur derniere version en cas de doublon |
|
| 261 | + $infos['_DIR_RESTREINT'][''] = $get_infos('./', $force, _DIR_RESTREINT); |
|
| 262 | + $infos['_DIR_RESTREINT']['SPIP']['version'] = $GLOBALS['spip_version_branche']; |
|
| 263 | + $infos['_DIR_RESTREINT']['SPIP']['chemin'] = []; |
|
| 264 | + $liste_non_classee = [ |
|
| 265 | + 'SPIP' => [ |
|
| 266 | + 'nom' => 'SPIP', |
|
| 267 | + 'etat' => 'stable', |
|
| 268 | + 'version' => $GLOBALS['spip_version_branche'], |
|
| 269 | + 'dir_type' => '_DIR_RESTREINT', |
|
| 270 | + 'dir' => '', |
|
| 271 | + ] |
|
| 272 | + ]; |
|
| 273 | + |
|
| 274 | + $invalides = []; |
|
| 275 | + foreach ($liste_ext as $plug) { |
|
| 276 | + if (isset($infos['_DIR_PLUGINS_DIST'][$plug])) { |
|
| 277 | + plugin_valide_resume($liste_non_classee, $plug, $infos, '_DIR_PLUGINS_DIST'); |
|
| 278 | + } |
|
| 279 | + } |
|
| 280 | + foreach ($liste_plug as $plug) { |
|
| 281 | + if (isset($infos['_DIR_PLUGINS'][$plug])) { |
|
| 282 | + $r = plugin_valide_resume($liste_non_classee, $plug, $infos, '_DIR_PLUGINS'); |
|
| 283 | + if (is_array($r)) { |
|
| 284 | + $invalides = array_merge($invalides, $r); |
|
| 285 | + } |
|
| 286 | + } |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + if (defined('_DIR_PLUGINS_SUPPL') && _DIR_PLUGINS_SUPPL) { |
|
| 290 | + $infos['_DIR_PLUGINS_SUPPL'] = $get_infos($liste_plug, false, _DIR_PLUGINS_SUPPL); |
|
| 291 | + foreach ($liste_plug as $plug) { |
|
| 292 | + if (isset($infos['_DIR_PLUGINS_SUPPL'][$plug])) { |
|
| 293 | + $r = plugin_valide_resume($liste_non_classee, $plug, $infos, '_DIR_PLUGINS_SUPPL'); |
|
| 294 | + if (is_array($r)) { |
|
| 295 | + $invalides = array_merge($invalides, $r); |
|
| 296 | + } |
|
| 297 | + } |
|
| 298 | + } |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + plugin_fixer_procure($liste_non_classee, $infos); |
|
| 302 | + |
|
| 303 | + // les plugins qui sont dans $liste_non_classee ne sont pas invalides (on a trouve un autre version valide) |
|
| 304 | + $invalides = array_diff_key($invalides, $liste_non_classee); |
|
| 305 | + |
|
| 306 | + return [$infos, $liste_non_classee, $invalides]; |
|
| 307 | 307 | } |
| 308 | 308 | |
| 309 | 309 | /** |
@@ -323,37 +323,37 @@ discard block |
||
| 323 | 323 | * array description short si on ne le retient pas (pour memorisation dans une table des erreurs) |
| 324 | 324 | */ |
| 325 | 325 | function plugin_valide_resume(&$liste, $plug, $infos, $dir_type) { |
| 326 | - $i = $infos[$dir_type][$plug]; |
|
| 327 | - // minimum syndical pour afficher si le xml avait des erreurs éventuelles |
|
| 328 | - $short_desc = [ |
|
| 329 | - 'dir' => $plug, |
|
| 330 | - 'dir_type' => $dir_type |
|
| 331 | - ]; |
|
| 332 | - if (empty($i['prefix'])) { |
|
| 333 | - // erreur xml ? mais sans connaissance du prefix, on retourne le chemin… |
|
| 334 | - $short_desc['erreur'] = $i['erreur'] ?? ['?']; |
|
| 335 | - return [$plug => $short_desc]; |
|
| 336 | - } |
|
| 337 | - |
|
| 338 | - $p = strtoupper($i['prefix']); |
|
| 339 | - $short_desc['nom'] = $i['nom']; |
|
| 340 | - $short_desc['etat'] = $i['etat']; |
|
| 341 | - $short_desc['version'] = $i['version']; |
|
| 342 | - |
|
| 343 | - if (isset($i['erreur']) && $i['erreur']) { |
|
| 344 | - $short_desc['erreur'] = $i['erreur']; |
|
| 345 | - return [$p => $short_desc]; |
|
| 346 | - } |
|
| 347 | - if (!plugin_version_compatible($i['compatibilite'], $GLOBALS['spip_version_branche'], 'spip')) { |
|
| 348 | - return [$p => $short_desc]; |
|
| 349 | - } |
|
| 350 | - if ( |
|
| 351 | - !isset($liste[$p]) || spip_version_compare($i['version'], $liste[$p]['version'], '>') |
|
| 352 | - ) { |
|
| 353 | - $liste[$p] = $short_desc; |
|
| 354 | - } |
|
| 355 | - // ok le plugin etait deja dans la liste ou on a choisi une version plus recente |
|
| 356 | - return $p; |
|
| 326 | + $i = $infos[$dir_type][$plug]; |
|
| 327 | + // minimum syndical pour afficher si le xml avait des erreurs éventuelles |
|
| 328 | + $short_desc = [ |
|
| 329 | + 'dir' => $plug, |
|
| 330 | + 'dir_type' => $dir_type |
|
| 331 | + ]; |
|
| 332 | + if (empty($i['prefix'])) { |
|
| 333 | + // erreur xml ? mais sans connaissance du prefix, on retourne le chemin… |
|
| 334 | + $short_desc['erreur'] = $i['erreur'] ?? ['?']; |
|
| 335 | + return [$plug => $short_desc]; |
|
| 336 | + } |
|
| 337 | + |
|
| 338 | + $p = strtoupper($i['prefix']); |
|
| 339 | + $short_desc['nom'] = $i['nom']; |
|
| 340 | + $short_desc['etat'] = $i['etat']; |
|
| 341 | + $short_desc['version'] = $i['version']; |
|
| 342 | + |
|
| 343 | + if (isset($i['erreur']) && $i['erreur']) { |
|
| 344 | + $short_desc['erreur'] = $i['erreur']; |
|
| 345 | + return [$p => $short_desc]; |
|
| 346 | + } |
|
| 347 | + if (!plugin_version_compatible($i['compatibilite'], $GLOBALS['spip_version_branche'], 'spip')) { |
|
| 348 | + return [$p => $short_desc]; |
|
| 349 | + } |
|
| 350 | + if ( |
|
| 351 | + !isset($liste[$p]) || spip_version_compare($i['version'], $liste[$p]['version'], '>') |
|
| 352 | + ) { |
|
| 353 | + $liste[$p] = $short_desc; |
|
| 354 | + } |
|
| 355 | + // ok le plugin etait deja dans la liste ou on a choisi une version plus recente |
|
| 356 | + return $p; |
|
| 357 | 357 | } |
| 358 | 358 | |
| 359 | 359 | /** |
@@ -369,47 +369,47 @@ discard block |
||
| 369 | 369 | * @param array $infos |
| 370 | 370 | */ |
| 371 | 371 | function plugin_fixer_procure(&$liste, &$infos) { |
| 372 | - foreach ($liste as $p => $resume) { |
|
| 373 | - $i = $infos[$resume['dir_type']][$resume['dir']]; |
|
| 374 | - if (isset($i['procure']) && $i['procure']) { |
|
| 375 | - foreach ($i['procure'] as $procure) { |
|
| 376 | - $p = strtoupper($procure['nom']); |
|
| 377 | - $dir = $resume['dir']; |
|
| 378 | - if ($dir) { |
|
| 379 | - $dir .= '/'; |
|
| 380 | - } |
|
| 381 | - $dir .= 'procure:' . $procure['nom']; |
|
| 382 | - |
|
| 383 | - $procure['etat'] = '?'; |
|
| 384 | - $procure['dir_type'] = $resume['dir_type']; |
|
| 385 | - $procure['dir'] = $dir; |
|
| 386 | - |
|
| 387 | - // si ce plugin n'est pas deja procure, ou dans une version plus ancienne |
|
| 388 | - // on ajoute cette version a la liste |
|
| 389 | - if ( |
|
| 390 | - !isset($liste[$p]) |
|
| 391 | - || spip_version_compare($procure['version'], $liste[$p]['version'], '>') |
|
| 392 | - ) { |
|
| 393 | - $liste[$p] = $procure; |
|
| 394 | - |
|
| 395 | - // on fournit une information minimale pour ne pas perturber la compilation |
|
| 396 | - $infos[$resume['dir_type']][$dir] = [ |
|
| 397 | - 'prefix' => $procure['nom'], |
|
| 398 | - 'nom' => $procure['nom'], |
|
| 399 | - 'etat' => $procure['etat'], |
|
| 400 | - 'version' => $procure['version'], |
|
| 401 | - 'chemin' => [], |
|
| 402 | - 'necessite' => [], |
|
| 403 | - 'utilise' => [], |
|
| 404 | - 'lib' => [], |
|
| 405 | - 'menu' => [], |
|
| 406 | - 'onglet' => [], |
|
| 407 | - 'procure' => [], |
|
| 408 | - ]; |
|
| 409 | - } |
|
| 410 | - } |
|
| 411 | - } |
|
| 412 | - } |
|
| 372 | + foreach ($liste as $p => $resume) { |
|
| 373 | + $i = $infos[$resume['dir_type']][$resume['dir']]; |
|
| 374 | + if (isset($i['procure']) && $i['procure']) { |
|
| 375 | + foreach ($i['procure'] as $procure) { |
|
| 376 | + $p = strtoupper($procure['nom']); |
|
| 377 | + $dir = $resume['dir']; |
|
| 378 | + if ($dir) { |
|
| 379 | + $dir .= '/'; |
|
| 380 | + } |
|
| 381 | + $dir .= 'procure:' . $procure['nom']; |
|
| 382 | + |
|
| 383 | + $procure['etat'] = '?'; |
|
| 384 | + $procure['dir_type'] = $resume['dir_type']; |
|
| 385 | + $procure['dir'] = $dir; |
|
| 386 | + |
|
| 387 | + // si ce plugin n'est pas deja procure, ou dans une version plus ancienne |
|
| 388 | + // on ajoute cette version a la liste |
|
| 389 | + if ( |
|
| 390 | + !isset($liste[$p]) |
|
| 391 | + || spip_version_compare($procure['version'], $liste[$p]['version'], '>') |
|
| 392 | + ) { |
|
| 393 | + $liste[$p] = $procure; |
|
| 394 | + |
|
| 395 | + // on fournit une information minimale pour ne pas perturber la compilation |
|
| 396 | + $infos[$resume['dir_type']][$dir] = [ |
|
| 397 | + 'prefix' => $procure['nom'], |
|
| 398 | + 'nom' => $procure['nom'], |
|
| 399 | + 'etat' => $procure['etat'], |
|
| 400 | + 'version' => $procure['version'], |
|
| 401 | + 'chemin' => [], |
|
| 402 | + 'necessite' => [], |
|
| 403 | + 'utilise' => [], |
|
| 404 | + 'lib' => [], |
|
| 405 | + 'menu' => [], |
|
| 406 | + 'onglet' => [], |
|
| 407 | + 'procure' => [], |
|
| 408 | + ]; |
|
| 409 | + } |
|
| 410 | + } |
|
| 411 | + } |
|
| 412 | + } |
|
| 413 | 413 | } |
| 414 | 414 | |
| 415 | 415 | /** |
@@ -423,17 +423,17 @@ discard block |
||
| 423 | 423 | * @return array |
| 424 | 424 | */ |
| 425 | 425 | function liste_chemin_plugin($liste, $dir_plugins = _DIR_PLUGINS) { |
| 426 | - foreach ($liste as $prefix => $infos) { |
|
| 427 | - if ( |
|
| 428 | - !$dir_plugins || defined($infos['dir_type']) && constant($infos['dir_type']) == $dir_plugins |
|
| 429 | - ) { |
|
| 430 | - $liste[$prefix] = $infos['dir']; |
|
| 431 | - } else { |
|
| 432 | - unset($liste[$prefix]); |
|
| 433 | - } |
|
| 434 | - } |
|
| 435 | - |
|
| 436 | - return $liste; |
|
| 426 | + foreach ($liste as $prefix => $infos) { |
|
| 427 | + if ( |
|
| 428 | + !$dir_plugins || defined($infos['dir_type']) && constant($infos['dir_type']) == $dir_plugins |
|
| 429 | + ) { |
|
| 430 | + $liste[$prefix] = $infos['dir']; |
|
| 431 | + } else { |
|
| 432 | + unset($liste[$prefix]); |
|
| 433 | + } |
|
| 434 | + } |
|
| 435 | + |
|
| 436 | + return $liste; |
|
| 437 | 437 | } |
| 438 | 438 | |
| 439 | 439 | /** |
@@ -448,9 +448,9 @@ discard block |
||
| 448 | 448 | * @return array |
| 449 | 449 | */ |
| 450 | 450 | function liste_chemin_plugin_actifs($dir_plugins = _DIR_PLUGINS) { |
| 451 | - include_spip('plugins/installer'); |
|
| 451 | + include_spip('plugins/installer'); |
|
| 452 | 452 | |
| 453 | - return liste_chemin_plugin(liste_plugin_actifs(), $dir_plugins); |
|
| 453 | + return liste_chemin_plugin(liste_plugin_actifs(), $dir_plugins); |
|
| 454 | 454 | } |
| 455 | 455 | |
| 456 | 456 | /** |
@@ -481,53 +481,53 @@ discard block |
||
| 481 | 481 | * qui n'ont pas satisfait leurs dépendances |
| 482 | 482 | **/ |
| 483 | 483 | function plugin_trier($infos, $liste_non_classee) { |
| 484 | - $toute_la_liste = $liste_non_classee; |
|
| 485 | - $liste = $ordre = []; |
|
| 486 | - $count = 0; |
|
| 487 | - |
|
| 488 | - while (($c = count($liste_non_classee)) && $c != $count) { // tant qu'il reste des plugins a classer, et qu'on ne stagne pas |
|
| 489 | - #echo "tour::";var_dump($liste_non_classee); |
|
| 490 | - $count = $c; |
|
| 491 | - foreach ($liste_non_classee as $p => $resume) { |
|
| 492 | - $plug = $resume['dir']; |
|
| 493 | - $dir_type = $resume['dir_type']; |
|
| 494 | - $info1 = $infos[$dir_type][$plug]; |
|
| 495 | - // si des plugins sont necessaires, |
|
| 496 | - // on ne peut inserer qu'apres eux |
|
| 497 | - foreach ($info1['necessite'] as $need) { |
|
| 498 | - $nom = strtoupper($need['nom']); |
|
| 499 | - $compat = $need['compatibilite'] ?? ''; |
|
| 500 | - if (!isset($liste[$nom]) || !plugin_version_compatible($compat, $liste[$nom]['version'])) { |
|
| 501 | - $info1 = false; |
|
| 502 | - break; |
|
| 503 | - } |
|
| 504 | - } |
|
| 505 | - if (!$info1) { |
|
| 506 | - continue; |
|
| 507 | - } |
|
| 508 | - // idem si des plugins sont utiles, |
|
| 509 | - // sauf si ils sont de toute facon absents de la liste |
|
| 510 | - foreach ($info1['utilise'] as $need) { |
|
| 511 | - $nom = strtoupper($need['nom']); |
|
| 512 | - $compat = $need['compatibilite'] ?? ''; |
|
| 513 | - if (isset($toute_la_liste[$nom])) { |
|
| 514 | - if ( |
|
| 515 | - !isset($liste[$nom]) || !plugin_version_compatible($compat, $liste[$nom]['version']) |
|
| 516 | - ) { |
|
| 517 | - $info1 = false; |
|
| 518 | - break; |
|
| 519 | - } |
|
| 520 | - } |
|
| 521 | - } |
|
| 522 | - if ($info1) { |
|
| 523 | - $ordre[$p] = $info1; |
|
| 524 | - $liste[$p] = $liste_non_classee[$p]; |
|
| 525 | - unset($liste_non_classee[$p]); |
|
| 526 | - } |
|
| 527 | - } |
|
| 528 | - } |
|
| 529 | - |
|
| 530 | - return [$liste, $ordre, $liste_non_classee]; |
|
| 484 | + $toute_la_liste = $liste_non_classee; |
|
| 485 | + $liste = $ordre = []; |
|
| 486 | + $count = 0; |
|
| 487 | + |
|
| 488 | + while (($c = count($liste_non_classee)) && $c != $count) { // tant qu'il reste des plugins a classer, et qu'on ne stagne pas |
|
| 489 | + #echo "tour::";var_dump($liste_non_classee); |
|
| 490 | + $count = $c; |
|
| 491 | + foreach ($liste_non_classee as $p => $resume) { |
|
| 492 | + $plug = $resume['dir']; |
|
| 493 | + $dir_type = $resume['dir_type']; |
|
| 494 | + $info1 = $infos[$dir_type][$plug]; |
|
| 495 | + // si des plugins sont necessaires, |
|
| 496 | + // on ne peut inserer qu'apres eux |
|
| 497 | + foreach ($info1['necessite'] as $need) { |
|
| 498 | + $nom = strtoupper($need['nom']); |
|
| 499 | + $compat = $need['compatibilite'] ?? ''; |
|
| 500 | + if (!isset($liste[$nom]) || !plugin_version_compatible($compat, $liste[$nom]['version'])) { |
|
| 501 | + $info1 = false; |
|
| 502 | + break; |
|
| 503 | + } |
|
| 504 | + } |
|
| 505 | + if (!$info1) { |
|
| 506 | + continue; |
|
| 507 | + } |
|
| 508 | + // idem si des plugins sont utiles, |
|
| 509 | + // sauf si ils sont de toute facon absents de la liste |
|
| 510 | + foreach ($info1['utilise'] as $need) { |
|
| 511 | + $nom = strtoupper($need['nom']); |
|
| 512 | + $compat = $need['compatibilite'] ?? ''; |
|
| 513 | + if (isset($toute_la_liste[$nom])) { |
|
| 514 | + if ( |
|
| 515 | + !isset($liste[$nom]) || !plugin_version_compatible($compat, $liste[$nom]['version']) |
|
| 516 | + ) { |
|
| 517 | + $info1 = false; |
|
| 518 | + break; |
|
| 519 | + } |
|
| 520 | + } |
|
| 521 | + } |
|
| 522 | + if ($info1) { |
|
| 523 | + $ordre[$p] = $info1; |
|
| 524 | + $liste[$p] = $liste_non_classee[$p]; |
|
| 525 | + unset($liste_non_classee[$p]); |
|
| 526 | + } |
|
| 527 | + } |
|
| 528 | + } |
|
| 529 | + |
|
| 530 | + return [$liste, $ordre, $liste_non_classee]; |
|
| 531 | 531 | } |
| 532 | 532 | |
| 533 | 533 | /** |
@@ -544,40 +544,40 @@ discard block |
||
| 544 | 544 | * Répertoire (plugins, plugins-dist, ...) => Couples (prefixes => infos completes) des plugins qu'ils contiennent |
| 545 | 545 | **/ |
| 546 | 546 | function plugins_erreurs($liste_non_classee, $liste, $infos, $msg = []) { |
| 547 | - static $erreurs = []; |
|
| 548 | - |
|
| 549 | - if (!is_array($liste)) { |
|
| 550 | - $liste = []; |
|
| 551 | - } |
|
| 552 | - |
|
| 553 | - // les plugins en erreur ne sont pas actifs ; ils ne doivent pas être dans la liste |
|
| 554 | - $liste = array_diff_key($liste, $liste_non_classee); |
|
| 555 | - |
|
| 556 | - foreach ($liste_non_classee as $p => $resume) { |
|
| 557 | - $dir_type = $resume['dir_type']; |
|
| 558 | - $plug = $resume['dir']; |
|
| 559 | - $k = $infos[$dir_type][$plug]; |
|
| 560 | - |
|
| 561 | - $plug = constant($dir_type) . $plug; |
|
| 562 | - if (!isset($msg[$p])) { |
|
| 563 | - if (isset($resume['erreur']) && $resume['erreur']) { |
|
| 564 | - $msg[$p] = [$resume['erreur']]; |
|
| 565 | - } |
|
| 566 | - elseif (!plugin_version_compatible($k['compatibilite'], $GLOBALS['spip_version_branche'], 'spip')) { |
|
| 567 | - $msg[$p] = [plugin_message_incompatibilite($k['compatibilite'], $GLOBALS['spip_version_branche'], 'SPIP', 'necessite')]; |
|
| 568 | - } |
|
| 569 | - elseif (!$msg[$p] = plugin_necessite($k['necessite'], $liste, 'necessite')) { |
|
| 570 | - $msg[$p] = plugin_necessite($k['utilise'], $liste, 'utilise'); |
|
| 571 | - } |
|
| 572 | - } else { |
|
| 573 | - foreach ($msg[$p] as $c => $l) { |
|
| 574 | - $msg[$p][$c] = plugin_controler_lib($l['nom'], $l['lien']); |
|
| 575 | - } |
|
| 576 | - } |
|
| 577 | - $erreurs[$plug] = $msg[$p]; |
|
| 578 | - } |
|
| 579 | - |
|
| 580 | - ecrire_meta('plugin_erreur_activation', serialize($erreurs)); |
|
| 547 | + static $erreurs = []; |
|
| 548 | + |
|
| 549 | + if (!is_array($liste)) { |
|
| 550 | + $liste = []; |
|
| 551 | + } |
|
| 552 | + |
|
| 553 | + // les plugins en erreur ne sont pas actifs ; ils ne doivent pas être dans la liste |
|
| 554 | + $liste = array_diff_key($liste, $liste_non_classee); |
|
| 555 | + |
|
| 556 | + foreach ($liste_non_classee as $p => $resume) { |
|
| 557 | + $dir_type = $resume['dir_type']; |
|
| 558 | + $plug = $resume['dir']; |
|
| 559 | + $k = $infos[$dir_type][$plug]; |
|
| 560 | + |
|
| 561 | + $plug = constant($dir_type) . $plug; |
|
| 562 | + if (!isset($msg[$p])) { |
|
| 563 | + if (isset($resume['erreur']) && $resume['erreur']) { |
|
| 564 | + $msg[$p] = [$resume['erreur']]; |
|
| 565 | + } |
|
| 566 | + elseif (!plugin_version_compatible($k['compatibilite'], $GLOBALS['spip_version_branche'], 'spip')) { |
|
| 567 | + $msg[$p] = [plugin_message_incompatibilite($k['compatibilite'], $GLOBALS['spip_version_branche'], 'SPIP', 'necessite')]; |
|
| 568 | + } |
|
| 569 | + elseif (!$msg[$p] = plugin_necessite($k['necessite'], $liste, 'necessite')) { |
|
| 570 | + $msg[$p] = plugin_necessite($k['utilise'], $liste, 'utilise'); |
|
| 571 | + } |
|
| 572 | + } else { |
|
| 573 | + foreach ($msg[$p] as $c => $l) { |
|
| 574 | + $msg[$p][$c] = plugin_controler_lib($l['nom'], $l['lien']); |
|
| 575 | + } |
|
| 576 | + } |
|
| 577 | + $erreurs[$plug] = $msg[$p]; |
|
| 578 | + } |
|
| 579 | + |
|
| 580 | + ecrire_meta('plugin_erreur_activation', serialize($erreurs)); |
|
| 581 | 581 | } |
| 582 | 582 | |
| 583 | 583 | /** |
@@ -592,25 +592,25 @@ discard block |
||
| 592 | 592 | * - Liste des erreurs ou code HTML des erreurs |
| 593 | 593 | **/ |
| 594 | 594 | function plugin_donne_erreurs($raw = false, $raz = true) { |
| 595 | - if (!isset($GLOBALS['meta']['plugin_erreur_activation'])) { |
|
| 596 | - return $raw ? [] : ''; |
|
| 597 | - } |
|
| 598 | - $list = @unserialize($GLOBALS['meta']['plugin_erreur_activation']); |
|
| 599 | - // Compat ancienne version |
|
| 600 | - if (!$list) { |
|
| 601 | - $list = $raw ? [] : $GLOBALS['meta']['plugin_erreur_activation']; |
|
| 602 | - } elseif (!$raw) { |
|
| 603 | - foreach ($list as $plug => $msg) { |
|
| 604 | - $list[$plug] = '<li>' . _T('plugin_impossible_activer', ['plugin' => $plug]) |
|
| 605 | - . '<ul><li>' . implode('</li><li>', $msg) . '</li></ul></li>'; |
|
| 606 | - } |
|
| 607 | - $list = '<ul>' . join("\n", $list) . '</ul>'; |
|
| 608 | - } |
|
| 609 | - if ($raz) { |
|
| 610 | - effacer_meta('plugin_erreur_activation'); |
|
| 611 | - } |
|
| 612 | - |
|
| 613 | - return $list; |
|
| 595 | + if (!isset($GLOBALS['meta']['plugin_erreur_activation'])) { |
|
| 596 | + return $raw ? [] : ''; |
|
| 597 | + } |
|
| 598 | + $list = @unserialize($GLOBALS['meta']['plugin_erreur_activation']); |
|
| 599 | + // Compat ancienne version |
|
| 600 | + if (!$list) { |
|
| 601 | + $list = $raw ? [] : $GLOBALS['meta']['plugin_erreur_activation']; |
|
| 602 | + } elseif (!$raw) { |
|
| 603 | + foreach ($list as $plug => $msg) { |
|
| 604 | + $list[$plug] = '<li>' . _T('plugin_impossible_activer', ['plugin' => $plug]) |
|
| 605 | + . '<ul><li>' . implode('</li><li>', $msg) . '</li></ul></li>'; |
|
| 606 | + } |
|
| 607 | + $list = '<ul>' . join("\n", $list) . '</ul>'; |
|
| 608 | + } |
|
| 609 | + if ($raz) { |
|
| 610 | + effacer_meta('plugin_erreur_activation'); |
|
| 611 | + } |
|
| 612 | + |
|
| 613 | + return $list; |
|
| 614 | 614 | } |
| 615 | 615 | |
| 616 | 616 | /** |
@@ -630,21 +630,21 @@ discard block |
||
| 630 | 630 | * |
| 631 | 631 | **/ |
| 632 | 632 | function plugin_necessite($n, $liste, $balise = 'necessite') { |
| 633 | - $msg = []; |
|
| 634 | - foreach ($n as $need) { |
|
| 635 | - $id = strtoupper($need['nom']); |
|
| 636 | - $r = plugin_controler_necessite( |
|
| 637 | - $liste, |
|
| 638 | - $id, |
|
| 639 | - $need['compatibilite'] ?? '', |
|
| 640 | - $balise |
|
| 641 | - ); |
|
| 642 | - if ($r) { |
|
| 643 | - $msg[] = $r; |
|
| 644 | - } |
|
| 645 | - } |
|
| 646 | - |
|
| 647 | - return $msg; |
|
| 633 | + $msg = []; |
|
| 634 | + foreach ($n as $need) { |
|
| 635 | + $id = strtoupper($need['nom']); |
|
| 636 | + $r = plugin_controler_necessite( |
|
| 637 | + $liste, |
|
| 638 | + $id, |
|
| 639 | + $need['compatibilite'] ?? '', |
|
| 640 | + $balise |
|
| 641 | + ); |
|
| 642 | + if ($r) { |
|
| 643 | + $msg[] = $r; |
|
| 644 | + } |
|
| 645 | + } |
|
| 646 | + |
|
| 647 | + return $msg; |
|
| 648 | 648 | } |
| 649 | 649 | |
| 650 | 650 | /** |
@@ -666,19 +666,19 @@ discard block |
||
| 666 | 666 | * Message d'erreur lorsque la dépendance est absente. |
| 667 | 667 | **/ |
| 668 | 668 | function plugin_controler_necessite($liste, $nom, $intervalle, $balise) { |
| 669 | - if (isset($liste[$nom]) && plugin_version_compatible($intervalle, $liste[$nom]['version'])) { |
|
| 670 | - return ''; |
|
| 671 | - } |
|
| 672 | - // Si l'on a un <utilise="plugin non actif" />, ne pas renvoyer d'erreur |
|
| 673 | - if ($balise === 'utilise' && !isset($liste[$nom])) { |
|
| 674 | - return ''; |
|
| 675 | - } |
|
| 676 | - return plugin_message_incompatibilite( |
|
| 677 | - $intervalle, |
|
| 678 | - (isset($liste[$nom]) ? $liste[$nom]['version'] : ''), |
|
| 679 | - $nom, |
|
| 680 | - $balise |
|
| 681 | - ); |
|
| 669 | + if (isset($liste[$nom]) && plugin_version_compatible($intervalle, $liste[$nom]['version'])) { |
|
| 670 | + return ''; |
|
| 671 | + } |
|
| 672 | + // Si l'on a un <utilise="plugin non actif" />, ne pas renvoyer d'erreur |
|
| 673 | + if ($balise === 'utilise' && !isset($liste[$nom])) { |
|
| 674 | + return ''; |
|
| 675 | + } |
|
| 676 | + return plugin_message_incompatibilite( |
|
| 677 | + $intervalle, |
|
| 678 | + (isset($liste[$nom]) ? $liste[$nom]['version'] : ''), |
|
| 679 | + $nom, |
|
| 680 | + $balise |
|
| 681 | + ); |
|
| 682 | 682 | } |
| 683 | 683 | |
| 684 | 684 | /** |
@@ -695,70 +695,70 @@ discard block |
||
| 695 | 695 | */ |
| 696 | 696 | function plugin_message_incompatibilite($intervalle, $version, $nom, $balise) { |
| 697 | 697 | |
| 698 | - // prendre en compte les erreurs de dépendances à PHP |
|
| 699 | - // ou à une extension PHP avec des messages d'erreurs dédiés. |
|
| 700 | - $type = 'plugin'; |
|
| 701 | - if ($nom === 'SPIP') { |
|
| 702 | - $type = 'spip'; |
|
| 703 | - } elseif ($nom === 'PHP') { |
|
| 704 | - $type = 'php'; |
|
| 705 | - } elseif (str_starts_with($nom, 'PHP:')) { |
|
| 706 | - $type = 'extension_php'; |
|
| 707 | - [, $nom] = explode(':', $nom, 2); |
|
| 708 | - } |
|
| 709 | - |
|
| 710 | - if (preg_match(_EXTRAIRE_INTERVALLE, $intervalle, $regs)) { |
|
| 711 | - $minimum = $regs[1]; |
|
| 712 | - $maximum = $regs[2]; |
|
| 713 | - |
|
| 714 | - $minimum_inclus = $intervalle[0] == '['; |
|
| 715 | - $maximum_inclus = str_ends_with($intervalle, ']'); |
|
| 716 | - |
|
| 717 | - if (strlen($minimum)) { |
|
| 718 | - if ($minimum_inclus && spip_version_compare($version, $minimum, '<')) { |
|
| 719 | - return _T("plugin_{$balise}_{$type}", [ |
|
| 720 | - 'plugin' => $nom, |
|
| 721 | - 'version' => ' ≥ ' . $minimum |
|
| 722 | - ]); |
|
| 723 | - } |
|
| 724 | - if (!$minimum_inclus && spip_version_compare($version, $minimum, '<=')) { |
|
| 725 | - return _T("plugin_{$balise}_{$type}", [ |
|
| 726 | - 'plugin' => $nom, |
|
| 727 | - 'version' => ' > ' . $minimum |
|
| 728 | - ]); |
|
| 729 | - } |
|
| 730 | - } |
|
| 731 | - |
|
| 732 | - if (strlen($maximum)) { |
|
| 733 | - if ($maximum_inclus && spip_version_compare($version, $maximum, '>')) { |
|
| 734 | - return _T("plugin_{$balise}_{$type}", [ |
|
| 735 | - 'plugin' => $nom, |
|
| 736 | - 'version' => ' ≤ ' . $maximum |
|
| 737 | - ]); |
|
| 738 | - } |
|
| 739 | - if (!$maximum_inclus && spip_version_compare($version, $maximum, '>=')) { |
|
| 740 | - return _T("plugin_{$balise}_plugin", [ |
|
| 741 | - 'plugin' => $nom, |
|
| 742 | - 'version' => ' < ' . $maximum |
|
| 743 | - ]); |
|
| 744 | - } |
|
| 745 | - } |
|
| 746 | - } |
|
| 747 | - |
|
| 748 | - // note : il ne peut pas y avoir d'erreur sur |
|
| 749 | - // - un 'utilise' sans version. |
|
| 750 | - // - un 'php' sans version. |
|
| 751 | - return _T("plugin_necessite_{$type}_sans_version", ['plugin' => $nom]); |
|
| 698 | + // prendre en compte les erreurs de dépendances à PHP |
|
| 699 | + // ou à une extension PHP avec des messages d'erreurs dédiés. |
|
| 700 | + $type = 'plugin'; |
|
| 701 | + if ($nom === 'SPIP') { |
|
| 702 | + $type = 'spip'; |
|
| 703 | + } elseif ($nom === 'PHP') { |
|
| 704 | + $type = 'php'; |
|
| 705 | + } elseif (str_starts_with($nom, 'PHP:')) { |
|
| 706 | + $type = 'extension_php'; |
|
| 707 | + [, $nom] = explode(':', $nom, 2); |
|
| 708 | + } |
|
| 709 | + |
|
| 710 | + if (preg_match(_EXTRAIRE_INTERVALLE, $intervalle, $regs)) { |
|
| 711 | + $minimum = $regs[1]; |
|
| 712 | + $maximum = $regs[2]; |
|
| 713 | + |
|
| 714 | + $minimum_inclus = $intervalle[0] == '['; |
|
| 715 | + $maximum_inclus = str_ends_with($intervalle, ']'); |
|
| 716 | + |
|
| 717 | + if (strlen($minimum)) { |
|
| 718 | + if ($minimum_inclus && spip_version_compare($version, $minimum, '<')) { |
|
| 719 | + return _T("plugin_{$balise}_{$type}", [ |
|
| 720 | + 'plugin' => $nom, |
|
| 721 | + 'version' => ' ≥ ' . $minimum |
|
| 722 | + ]); |
|
| 723 | + } |
|
| 724 | + if (!$minimum_inclus && spip_version_compare($version, $minimum, '<=')) { |
|
| 725 | + return _T("plugin_{$balise}_{$type}", [ |
|
| 726 | + 'plugin' => $nom, |
|
| 727 | + 'version' => ' > ' . $minimum |
|
| 728 | + ]); |
|
| 729 | + } |
|
| 730 | + } |
|
| 731 | + |
|
| 732 | + if (strlen($maximum)) { |
|
| 733 | + if ($maximum_inclus && spip_version_compare($version, $maximum, '>')) { |
|
| 734 | + return _T("plugin_{$balise}_{$type}", [ |
|
| 735 | + 'plugin' => $nom, |
|
| 736 | + 'version' => ' ≤ ' . $maximum |
|
| 737 | + ]); |
|
| 738 | + } |
|
| 739 | + if (!$maximum_inclus && spip_version_compare($version, $maximum, '>=')) { |
|
| 740 | + return _T("plugin_{$balise}_plugin", [ |
|
| 741 | + 'plugin' => $nom, |
|
| 742 | + 'version' => ' < ' . $maximum |
|
| 743 | + ]); |
|
| 744 | + } |
|
| 745 | + } |
|
| 746 | + } |
|
| 747 | + |
|
| 748 | + // note : il ne peut pas y avoir d'erreur sur |
|
| 749 | + // - un 'utilise' sans version. |
|
| 750 | + // - un 'php' sans version. |
|
| 751 | + return _T("plugin_necessite_{$type}_sans_version", ['plugin' => $nom]); |
|
| 752 | 752 | } |
| 753 | 753 | |
| 754 | 754 | |
| 755 | 755 | function plugin_controler_lib($lib, $url) { |
| 756 | - /* Feature sortie du core, voir STP |
|
| 756 | + /* Feature sortie du core, voir STP |
|
| 757 | 757 | * if ($url) { |
| 758 | 758 | include_spip('inc/charger_plugin'); |
| 759 | 759 | $url = '<br />' . bouton_telechargement_plugin($url, 'lib'); |
| 760 | 760 | }*/ |
| 761 | - return _T('plugin_necessite_lib', ['lib' => $lib]) . " <a href='$url'>$url</a>"; |
|
| 761 | + return _T('plugin_necessite_lib', ['lib' => $lib]) . " <a href='$url'>$url</a>"; |
|
| 762 | 762 | } |
| 763 | 763 | |
| 764 | 764 | |
@@ -773,7 +773,7 @@ discard block |
||
| 773 | 773 | * true si il y a eu des modifications sur la liste des plugins actifs, false sinon |
| 774 | 774 | **/ |
| 775 | 775 | function actualise_plugins_actifs($pipe_recherche = false) { |
| 776 | - return ecrire_plugin_actifs('', $pipe_recherche, 'force'); |
|
| 776 | + return ecrire_plugin_actifs('', $pipe_recherche, 'force'); |
|
| 777 | 777 | } |
| 778 | 778 | |
| 779 | 779 | |
@@ -800,115 +800,115 @@ discard block |
||
| 800 | 800 | **/ |
| 801 | 801 | function ecrire_plugin_actifs($plugin, $pipe_recherche = false, $operation = 'raz') { |
| 802 | 802 | |
| 803 | - // creer le repertoire cache/ si necessaire ! (installation notamment) |
|
| 804 | - $cache = sous_repertoire(_DIR_CACHE, '', false, true); |
|
| 805 | - |
|
| 806 | - // Si on n'a ni cache accessible, ni connexion SQL, on ne peut pas faire grand chose encore. |
|
| 807 | - if (!$cache && !spip_connect()) { |
|
| 808 | - return false; |
|
| 809 | - } |
|
| 810 | - |
|
| 811 | - if ($operation != 'raz') { |
|
| 812 | - $plugin_valides = liste_chemin_plugin_actifs(); |
|
| 813 | - $plugin_valides = is_plugin_dir($plugin_valides); |
|
| 814 | - if (defined('_DIR_PLUGINS_SUPPL') && _DIR_PLUGINS_SUPPL) { |
|
| 815 | - $plugin_valides_supp = liste_chemin_plugin_actifs(_DIR_PLUGINS_SUPPL); |
|
| 816 | - $plugin_valides_supp = is_plugin_dir($plugin_valides_supp, _DIR_PLUGINS_SUPPL); |
|
| 817 | - $plugin_valides = array_merge($plugin_valides, $plugin_valides_supp); |
|
| 818 | - } |
|
| 819 | - // si des plugins sont en attentes (coches mais impossible a activer) |
|
| 820 | - // on les reinjecte ici |
|
| 821 | - if ( |
|
| 822 | - isset($GLOBALS['meta']['plugin_attente']) && ($a = unserialize($GLOBALS['meta']['plugin_attente'])) |
|
| 823 | - ) { |
|
| 824 | - $plugin_valides = $plugin_valides + liste_chemin_plugin($a); |
|
| 825 | - } |
|
| 826 | - |
|
| 827 | - if ($operation == 'ajoute') { |
|
| 828 | - $plugin = array_merge($plugin_valides, $plugin); |
|
| 829 | - } elseif ($operation == 'enleve') { |
|
| 830 | - $plugin = array_diff($plugin_valides, $plugin); |
|
| 831 | - } else { |
|
| 832 | - $plugin = $plugin_valides; |
|
| 833 | - } |
|
| 834 | - } |
|
| 835 | - $actifs_avant = $GLOBALS['meta']['plugin'] ?? ''; |
|
| 836 | - |
|
| 837 | - // si une fonction de gestion de dependances existe, l'appeler ici |
|
| 838 | - if ($ajouter_dependances = charger_fonction('ajouter_dependances', 'plugins', true)) { |
|
| 839 | - $plugin = $ajouter_dependances($plugin); |
|
| 840 | - } |
|
| 841 | - |
|
| 842 | - // recharger le xml des plugins a activer |
|
| 843 | - // on force le reload ici, meme si le fichier xml n'a pas change |
|
| 844 | - // pour ne pas rater l'ajout ou la suppression d'un fichier fonctions/options/administrations |
|
| 845 | - // pourra etre evite quand on ne supportera plus les plugin.xml |
|
| 846 | - // en deplacant la detection de ces fichiers dans la compilation ci dessous |
|
| 847 | - [$infos, $liste, $invalides] = liste_plugin_valides($plugin, true); |
|
| 848 | - // trouver l'ordre d'activation |
|
| 849 | - [$plugin_valides, $ordre, $reste] = plugin_trier($infos, $liste); |
|
| 850 | - if ($invalides || $reste) { |
|
| 851 | - plugins_erreurs(array_merge($invalides, $reste), $liste, $infos); |
|
| 852 | - } |
|
| 853 | - |
|
| 854 | - // Ignorer les plugins necessitant une lib absente |
|
| 855 | - // et preparer la meta d'entete Http |
|
| 856 | - $err = $msg = $header = []; |
|
| 857 | - foreach ($plugin_valides as $p => $resume) { |
|
| 858 | - // Les headers ne doivent pas indiquer les versions des extensions PHP, ni la version PHP |
|
| 859 | - if (!str_starts_with($p, 'PHP:') && $p !== 'PHP') { |
|
| 860 | - $header[] = $p . ($resume['version'] ? '(' . $resume['version'] . ')' : ''); |
|
| 861 | - } |
|
| 862 | - if ($resume['dir']) { |
|
| 863 | - foreach ($infos[$resume['dir_type']][$resume['dir']]['lib'] as $l) { |
|
| 864 | - if (!find_in_path($l['nom'], 'lib/')) { |
|
| 865 | - $err[$p] = $resume; |
|
| 866 | - $msg[$p][] = $l; |
|
| 867 | - unset($plugin_valides[$p]); |
|
| 868 | - } |
|
| 869 | - } |
|
| 870 | - } |
|
| 871 | - } |
|
| 872 | - if ($err) { |
|
| 873 | - plugins_erreurs($err, '', $infos, $msg); |
|
| 874 | - } |
|
| 875 | - |
|
| 876 | - if (isset($GLOBALS['meta']['message_crash_plugins'])) { |
|
| 877 | - effacer_meta('message_crash_plugins'); |
|
| 878 | - } |
|
| 879 | - ecrire_meta('plugin', serialize($plugin_valides)); |
|
| 880 | - $liste = array_diff_key($liste, $plugin_valides); |
|
| 881 | - ecrire_meta('plugin_attente', serialize($liste)); |
|
| 882 | - $header = strtolower(implode(',', $header)); |
|
| 883 | - if (!isset($GLOBALS['spip_header_silencieux']) || !$GLOBALS['spip_header_silencieux']) { |
|
| 884 | - ecrire_fichier( |
|
| 885 | - _DIR_VAR . 'config.txt', |
|
| 886 | - (defined('_HEADER_COMPOSED_BY') ? _HEADER_COMPOSED_BY : 'Composed-By: SPIP') . ' ' . $GLOBALS['spip_version_affichee'] . ' @ www.spip.net + ' . $header |
|
| 887 | - ); |
|
| 888 | - } else { |
|
| 889 | - @unlink(_DIR_VAR . 'config.txt'); |
|
| 890 | - } |
|
| 891 | - // generer charger_plugins_chemin.php |
|
| 892 | - plugins_precompile_chemin($plugin_valides, $ordre); |
|
| 893 | - // generer les fichiers |
|
| 894 | - // - charger_plugins_options.php |
|
| 895 | - // - charger_plugins_fonctions.php |
|
| 896 | - plugins_precompile_xxxtions($plugin_valides, $ordre); |
|
| 897 | - // charger les chemins des plugins et les fichiers d'options |
|
| 898 | - // (qui peuvent déclarer / utiliser des pipelines, ajouter d'autres chemins) |
|
| 899 | - plugins_amorcer_plugins_actifs(); |
|
| 900 | - // mise a jour de la matrice des pipelines |
|
| 901 | - $prepend_code = pipeline_matrice_precompile($plugin_valides, $ordre, $pipe_recherche); |
|
| 902 | - // generer le fichier _CACHE_PIPELINE |
|
| 903 | - pipeline_precompile($prepend_code); |
|
| 904 | - |
|
| 905 | - if (spip_connect()) { |
|
| 906 | - // lancer et initialiser les nouveaux crons ! |
|
| 907 | - include_spip('inc/genie'); |
|
| 908 | - genie_queue_watch_dist(); |
|
| 909 | - } |
|
| 910 | - |
|
| 911 | - return ($GLOBALS['meta']['plugin'] != $actifs_avant); |
|
| 803 | + // creer le repertoire cache/ si necessaire ! (installation notamment) |
|
| 804 | + $cache = sous_repertoire(_DIR_CACHE, '', false, true); |
|
| 805 | + |
|
| 806 | + // Si on n'a ni cache accessible, ni connexion SQL, on ne peut pas faire grand chose encore. |
|
| 807 | + if (!$cache && !spip_connect()) { |
|
| 808 | + return false; |
|
| 809 | + } |
|
| 810 | + |
|
| 811 | + if ($operation != 'raz') { |
|
| 812 | + $plugin_valides = liste_chemin_plugin_actifs(); |
|
| 813 | + $plugin_valides = is_plugin_dir($plugin_valides); |
|
| 814 | + if (defined('_DIR_PLUGINS_SUPPL') && _DIR_PLUGINS_SUPPL) { |
|
| 815 | + $plugin_valides_supp = liste_chemin_plugin_actifs(_DIR_PLUGINS_SUPPL); |
|
| 816 | + $plugin_valides_supp = is_plugin_dir($plugin_valides_supp, _DIR_PLUGINS_SUPPL); |
|
| 817 | + $plugin_valides = array_merge($plugin_valides, $plugin_valides_supp); |
|
| 818 | + } |
|
| 819 | + // si des plugins sont en attentes (coches mais impossible a activer) |
|
| 820 | + // on les reinjecte ici |
|
| 821 | + if ( |
|
| 822 | + isset($GLOBALS['meta']['plugin_attente']) && ($a = unserialize($GLOBALS['meta']['plugin_attente'])) |
|
| 823 | + ) { |
|
| 824 | + $plugin_valides = $plugin_valides + liste_chemin_plugin($a); |
|
| 825 | + } |
|
| 826 | + |
|
| 827 | + if ($operation == 'ajoute') { |
|
| 828 | + $plugin = array_merge($plugin_valides, $plugin); |
|
| 829 | + } elseif ($operation == 'enleve') { |
|
| 830 | + $plugin = array_diff($plugin_valides, $plugin); |
|
| 831 | + } else { |
|
| 832 | + $plugin = $plugin_valides; |
|
| 833 | + } |
|
| 834 | + } |
|
| 835 | + $actifs_avant = $GLOBALS['meta']['plugin'] ?? ''; |
|
| 836 | + |
|
| 837 | + // si une fonction de gestion de dependances existe, l'appeler ici |
|
| 838 | + if ($ajouter_dependances = charger_fonction('ajouter_dependances', 'plugins', true)) { |
|
| 839 | + $plugin = $ajouter_dependances($plugin); |
|
| 840 | + } |
|
| 841 | + |
|
| 842 | + // recharger le xml des plugins a activer |
|
| 843 | + // on force le reload ici, meme si le fichier xml n'a pas change |
|
| 844 | + // pour ne pas rater l'ajout ou la suppression d'un fichier fonctions/options/administrations |
|
| 845 | + // pourra etre evite quand on ne supportera plus les plugin.xml |
|
| 846 | + // en deplacant la detection de ces fichiers dans la compilation ci dessous |
|
| 847 | + [$infos, $liste, $invalides] = liste_plugin_valides($plugin, true); |
|
| 848 | + // trouver l'ordre d'activation |
|
| 849 | + [$plugin_valides, $ordre, $reste] = plugin_trier($infos, $liste); |
|
| 850 | + if ($invalides || $reste) { |
|
| 851 | + plugins_erreurs(array_merge($invalides, $reste), $liste, $infos); |
|
| 852 | + } |
|
| 853 | + |
|
| 854 | + // Ignorer les plugins necessitant une lib absente |
|
| 855 | + // et preparer la meta d'entete Http |
|
| 856 | + $err = $msg = $header = []; |
|
| 857 | + foreach ($plugin_valides as $p => $resume) { |
|
| 858 | + // Les headers ne doivent pas indiquer les versions des extensions PHP, ni la version PHP |
|
| 859 | + if (!str_starts_with($p, 'PHP:') && $p !== 'PHP') { |
|
| 860 | + $header[] = $p . ($resume['version'] ? '(' . $resume['version'] . ')' : ''); |
|
| 861 | + } |
|
| 862 | + if ($resume['dir']) { |
|
| 863 | + foreach ($infos[$resume['dir_type']][$resume['dir']]['lib'] as $l) { |
|
| 864 | + if (!find_in_path($l['nom'], 'lib/')) { |
|
| 865 | + $err[$p] = $resume; |
|
| 866 | + $msg[$p][] = $l; |
|
| 867 | + unset($plugin_valides[$p]); |
|
| 868 | + } |
|
| 869 | + } |
|
| 870 | + } |
|
| 871 | + } |
|
| 872 | + if ($err) { |
|
| 873 | + plugins_erreurs($err, '', $infos, $msg); |
|
| 874 | + } |
|
| 875 | + |
|
| 876 | + if (isset($GLOBALS['meta']['message_crash_plugins'])) { |
|
| 877 | + effacer_meta('message_crash_plugins'); |
|
| 878 | + } |
|
| 879 | + ecrire_meta('plugin', serialize($plugin_valides)); |
|
| 880 | + $liste = array_diff_key($liste, $plugin_valides); |
|
| 881 | + ecrire_meta('plugin_attente', serialize($liste)); |
|
| 882 | + $header = strtolower(implode(',', $header)); |
|
| 883 | + if (!isset($GLOBALS['spip_header_silencieux']) || !$GLOBALS['spip_header_silencieux']) { |
|
| 884 | + ecrire_fichier( |
|
| 885 | + _DIR_VAR . 'config.txt', |
|
| 886 | + (defined('_HEADER_COMPOSED_BY') ? _HEADER_COMPOSED_BY : 'Composed-By: SPIP') . ' ' . $GLOBALS['spip_version_affichee'] . ' @ www.spip.net + ' . $header |
|
| 887 | + ); |
|
| 888 | + } else { |
|
| 889 | + @unlink(_DIR_VAR . 'config.txt'); |
|
| 890 | + } |
|
| 891 | + // generer charger_plugins_chemin.php |
|
| 892 | + plugins_precompile_chemin($plugin_valides, $ordre); |
|
| 893 | + // generer les fichiers |
|
| 894 | + // - charger_plugins_options.php |
|
| 895 | + // - charger_plugins_fonctions.php |
|
| 896 | + plugins_precompile_xxxtions($plugin_valides, $ordre); |
|
| 897 | + // charger les chemins des plugins et les fichiers d'options |
|
| 898 | + // (qui peuvent déclarer / utiliser des pipelines, ajouter d'autres chemins) |
|
| 899 | + plugins_amorcer_plugins_actifs(); |
|
| 900 | + // mise a jour de la matrice des pipelines |
|
| 901 | + $prepend_code = pipeline_matrice_precompile($plugin_valides, $ordre, $pipe_recherche); |
|
| 902 | + // generer le fichier _CACHE_PIPELINE |
|
| 903 | + pipeline_precompile($prepend_code); |
|
| 904 | + |
|
| 905 | + if (spip_connect()) { |
|
| 906 | + // lancer et initialiser les nouveaux crons ! |
|
| 907 | + include_spip('inc/genie'); |
|
| 908 | + genie_queue_watch_dist(); |
|
| 909 | + } |
|
| 910 | + |
|
| 911 | + return ($GLOBALS['meta']['plugin'] != $actifs_avant); |
|
| 912 | 912 | } |
| 913 | 913 | |
| 914 | 914 | /** |
@@ -927,75 +927,75 @@ discard block |
||
| 927 | 927 | * Couples (prefixe => infos complètes) des plugins qui seront actifs, dans l'ordre de leurs dépendances |
| 928 | 928 | **/ |
| 929 | 929 | function plugins_precompile_chemin($plugin_valides, $ordre) { |
| 930 | - $chemins = [ |
|
| 931 | - 'public' => [], |
|
| 932 | - 'prive' => [] |
|
| 933 | - ]; |
|
| 934 | - $contenu = ''; |
|
| 935 | - foreach ($ordre as $p => $info) { |
|
| 936 | - // $ordre peur contenir des plugins en attente et non valides pour ce hit |
|
| 937 | - if (isset($plugin_valides[$p])) { |
|
| 938 | - $dir_type = $plugin_valides[$p]['dir_type']; |
|
| 939 | - $plug = $plugin_valides[$p]['dir']; |
|
| 940 | - // definir le plugin, donc le path avant l'include du fichier options |
|
| 941 | - // permet de faire des include_spip pour attraper un inc_ du plugin |
|
| 942 | - |
|
| 943 | - $dir = $dir_type . ".'" . $plug . "/'"; |
|
| 944 | - |
|
| 945 | - $prefix = strtoupper(preg_replace(',\W,', '_', $info['prefix'])); |
|
| 946 | - if ( |
|
| 947 | - $prefix !== 'SPIP' |
|
| 948 | - && !str_contains($dir, ':') // exclure le cas des procure: |
|
| 949 | - ) { |
|
| 950 | - $contenu .= "define('_DIR_PLUGIN_$prefix',$dir);\n"; |
|
| 951 | - if (!$info['chemin']) { |
|
| 952 | - $chemins['public'][] = "_DIR_PLUGIN_$prefix"; |
|
| 953 | - $chemins['prive'][] = "_DIR_PLUGIN_$prefix"; |
|
| 954 | - if (is_dir(constant($dir_type) . $plug . '/squelettes/')) { |
|
| 955 | - $chemins['public'][] = "_DIR_PLUGIN_{$prefix}.'squelettes/'"; |
|
| 956 | - } |
|
| 957 | - } |
|
| 958 | - else { |
|
| 959 | - foreach ($info['chemin'] as $chemin) { |
|
| 960 | - if ( |
|
| 961 | - !isset($chemin['version']) |
|
| 962 | - || plugin_version_compatible( |
|
| 963 | - $chemin['version'], |
|
| 964 | - $GLOBALS['spip_version_branche'], |
|
| 965 | - 'spip' |
|
| 966 | - ) |
|
| 967 | - ) { |
|
| 968 | - $dir = $chemin['path']; |
|
| 969 | - if (strlen($dir) && $dir[0] == '/') { |
|
| 970 | - $dir = substr($dir, 1); |
|
| 971 | - } |
|
| 972 | - if (strlen($dir) && $dir == './') { |
|
| 973 | - $dir = ''; |
|
| 974 | - } |
|
| 975 | - if (strlen($dir)) { |
|
| 976 | - $dir = rtrim($dir, '/') . '/'; |
|
| 977 | - } |
|
| 978 | - if (!isset($chemin['type']) || $chemin['type'] == 'public') { |
|
| 979 | - $chemins['public'][] = "_DIR_PLUGIN_$prefix" . (strlen($dir) ? ".'$dir'" : ''); |
|
| 980 | - } |
|
| 981 | - if (!isset($chemin['type']) || $chemin['type'] == 'prive') { |
|
| 982 | - $chemins['prive'][] = "_DIR_PLUGIN_$prefix" . (strlen($dir) ? ".'$dir'" : ''); |
|
| 983 | - } |
|
| 984 | - } |
|
| 985 | - } |
|
| 986 | - } |
|
| 987 | - } |
|
| 988 | - } |
|
| 989 | - } |
|
| 990 | - if (count($chemins['public']) || count($chemins['prive'])) { |
|
| 991 | - $contenu .= 'if (_DIR_RESTREINT) _chemin([' . implode( |
|
| 992 | - ',', |
|
| 993 | - array_reverse($chemins['public']) |
|
| 994 | - ) . "]);\n" |
|
| 995 | - . 'else _chemin([' . implode(',', array_reverse($chemins['prive'])) . "]);\n"; |
|
| 996 | - } |
|
| 997 | - |
|
| 998 | - ecrire_fichier_php(_CACHE_PLUGINS_PATH, $contenu); |
|
| 930 | + $chemins = [ |
|
| 931 | + 'public' => [], |
|
| 932 | + 'prive' => [] |
|
| 933 | + ]; |
|
| 934 | + $contenu = ''; |
|
| 935 | + foreach ($ordre as $p => $info) { |
|
| 936 | + // $ordre peur contenir des plugins en attente et non valides pour ce hit |
|
| 937 | + if (isset($plugin_valides[$p])) { |
|
| 938 | + $dir_type = $plugin_valides[$p]['dir_type']; |
|
| 939 | + $plug = $plugin_valides[$p]['dir']; |
|
| 940 | + // definir le plugin, donc le path avant l'include du fichier options |
|
| 941 | + // permet de faire des include_spip pour attraper un inc_ du plugin |
|
| 942 | + |
|
| 943 | + $dir = $dir_type . ".'" . $plug . "/'"; |
|
| 944 | + |
|
| 945 | + $prefix = strtoupper(preg_replace(',\W,', '_', $info['prefix'])); |
|
| 946 | + if ( |
|
| 947 | + $prefix !== 'SPIP' |
|
| 948 | + && !str_contains($dir, ':') // exclure le cas des procure: |
|
| 949 | + ) { |
|
| 950 | + $contenu .= "define('_DIR_PLUGIN_$prefix',$dir);\n"; |
|
| 951 | + if (!$info['chemin']) { |
|
| 952 | + $chemins['public'][] = "_DIR_PLUGIN_$prefix"; |
|
| 953 | + $chemins['prive'][] = "_DIR_PLUGIN_$prefix"; |
|
| 954 | + if (is_dir(constant($dir_type) . $plug . '/squelettes/')) { |
|
| 955 | + $chemins['public'][] = "_DIR_PLUGIN_{$prefix}.'squelettes/'"; |
|
| 956 | + } |
|
| 957 | + } |
|
| 958 | + else { |
|
| 959 | + foreach ($info['chemin'] as $chemin) { |
|
| 960 | + if ( |
|
| 961 | + !isset($chemin['version']) |
|
| 962 | + || plugin_version_compatible( |
|
| 963 | + $chemin['version'], |
|
| 964 | + $GLOBALS['spip_version_branche'], |
|
| 965 | + 'spip' |
|
| 966 | + ) |
|
| 967 | + ) { |
|
| 968 | + $dir = $chemin['path']; |
|
| 969 | + if (strlen($dir) && $dir[0] == '/') { |
|
| 970 | + $dir = substr($dir, 1); |
|
| 971 | + } |
|
| 972 | + if (strlen($dir) && $dir == './') { |
|
| 973 | + $dir = ''; |
|
| 974 | + } |
|
| 975 | + if (strlen($dir)) { |
|
| 976 | + $dir = rtrim($dir, '/') . '/'; |
|
| 977 | + } |
|
| 978 | + if (!isset($chemin['type']) || $chemin['type'] == 'public') { |
|
| 979 | + $chemins['public'][] = "_DIR_PLUGIN_$prefix" . (strlen($dir) ? ".'$dir'" : ''); |
|
| 980 | + } |
|
| 981 | + if (!isset($chemin['type']) || $chemin['type'] == 'prive') { |
|
| 982 | + $chemins['prive'][] = "_DIR_PLUGIN_$prefix" . (strlen($dir) ? ".'$dir'" : ''); |
|
| 983 | + } |
|
| 984 | + } |
|
| 985 | + } |
|
| 986 | + } |
|
| 987 | + } |
|
| 988 | + } |
|
| 989 | + } |
|
| 990 | + if (count($chemins['public']) || count($chemins['prive'])) { |
|
| 991 | + $contenu .= 'if (_DIR_RESTREINT) _chemin([' . implode( |
|
| 992 | + ',', |
|
| 993 | + array_reverse($chemins['public']) |
|
| 994 | + ) . "]);\n" |
|
| 995 | + . 'else _chemin([' . implode(',', array_reverse($chemins['prive'])) . "]);\n"; |
|
| 996 | + } |
|
| 997 | + |
|
| 998 | + ecrire_fichier_php(_CACHE_PLUGINS_PATH, $contenu); |
|
| 999 | 999 | } |
| 1000 | 1000 | |
| 1001 | 1001 | /** |
@@ -1013,65 +1013,65 @@ discard block |
||
| 1013 | 1013 | * Couples (prefixe => infos complètes) des plugins qui seront actifs, dans l'ordre de leurs dépendances |
| 1014 | 1014 | **/ |
| 1015 | 1015 | function plugins_precompile_xxxtions($plugin_valides, $ordre) { |
| 1016 | - $contenu = ['options' => '', 'fonctions' => '']; |
|
| 1017 | - $boutons = []; |
|
| 1018 | - $onglets = []; |
|
| 1019 | - $sign = ''; |
|
| 1020 | - |
|
| 1021 | - foreach ($ordre as $p => $info) { |
|
| 1022 | - // $ordre peur contenir des plugins en attente et non valides pour ce hit |
|
| 1023 | - if (isset($plugin_valides[$p])) { |
|
| 1024 | - $dir_type = $plugin_valides[$p]['dir_type']; |
|
| 1025 | - $plug = $plugin_valides[$p]['dir']; |
|
| 1026 | - $dir = constant($dir_type); |
|
| 1027 | - $root_dir_type = str_replace('_DIR_', '_ROOT_', $dir_type); |
|
| 1028 | - if ($info['menu']) { |
|
| 1029 | - $boutons = array_merge($boutons, $info['menu']); |
|
| 1030 | - } |
|
| 1031 | - if ($info['onglet']) { |
|
| 1032 | - $onglets = array_merge($onglets, $info['onglet']); |
|
| 1033 | - } |
|
| 1034 | - foreach ($contenu as $charge => $v) { |
|
| 1035 | - // si pas declare/detecte a la lecture du paquet.xml, |
|
| 1036 | - // detecer a nouveau ici puisque son ajout ne provoque pas une modif du paquet.xml |
|
| 1037 | - // donc ni sa relecture, ni sa detection |
|
| 1038 | - if ( |
|
| 1039 | - !isset($info[$charge]) |
|
| 1040 | - && $dir |
|
| 1041 | - && !str_contains($dir, ':') |
|
| 1042 | - && file_exists("$dir$plug/paquet.xml") // uniquement pour les paquet.xml |
|
| 1043 | - ) { |
|
| 1044 | - if (is_readable("$dir$plug/" . ($file = $info['prefix'] . '_' . $charge . '.php'))) { |
|
| 1045 | - $info[$charge] = [$file]; |
|
| 1046 | - } |
|
| 1047 | - } |
|
| 1048 | - if (isset($info[$charge])) { |
|
| 1049 | - $files = $info[$charge]; |
|
| 1050 | - foreach ($files as $k => $file) { |
|
| 1051 | - // on genere un if file_exists devant chaque include |
|
| 1052 | - // pour pouvoir garder le meme niveau d'erreur general |
|
| 1053 | - $file = trim($file); |
|
| 1054 | - if ( |
|
| 1055 | - !is_readable("$dir$plug/$file") && file_exists("$dir$plug/paquet.xml") |
|
| 1056 | - ) { |
|
| 1057 | - unset($info[$charge][$k]); |
|
| 1058 | - } else { |
|
| 1059 | - $_file = $root_dir_type . ".'$plug/$file'"; |
|
| 1060 | - $contenu[$charge] .= "include_once_check($_file);\n"; |
|
| 1061 | - } |
|
| 1062 | - } |
|
| 1063 | - } |
|
| 1064 | - } |
|
| 1065 | - $sign .= md5(serialize($info)); |
|
| 1066 | - } |
|
| 1067 | - } |
|
| 1068 | - |
|
| 1069 | - $contenu['options'] = "define('_PLUGINS_HASH','" . md5($sign) . "');\n" . $contenu['options']; |
|
| 1070 | - $contenu['fonctions'] .= plugin_ongletbouton('boutons_plugins', $boutons) |
|
| 1071 | - . plugin_ongletbouton('onglets_plugins', $onglets); |
|
| 1072 | - |
|
| 1073 | - ecrire_fichier_php(_CACHE_PLUGINS_OPT, $contenu['options']); |
|
| 1074 | - ecrire_fichier_php(_CACHE_PLUGINS_FCT, $contenu['fonctions']); |
|
| 1016 | + $contenu = ['options' => '', 'fonctions' => '']; |
|
| 1017 | + $boutons = []; |
|
| 1018 | + $onglets = []; |
|
| 1019 | + $sign = ''; |
|
| 1020 | + |
|
| 1021 | + foreach ($ordre as $p => $info) { |
|
| 1022 | + // $ordre peur contenir des plugins en attente et non valides pour ce hit |
|
| 1023 | + if (isset($plugin_valides[$p])) { |
|
| 1024 | + $dir_type = $plugin_valides[$p]['dir_type']; |
|
| 1025 | + $plug = $plugin_valides[$p]['dir']; |
|
| 1026 | + $dir = constant($dir_type); |
|
| 1027 | + $root_dir_type = str_replace('_DIR_', '_ROOT_', $dir_type); |
|
| 1028 | + if ($info['menu']) { |
|
| 1029 | + $boutons = array_merge($boutons, $info['menu']); |
|
| 1030 | + } |
|
| 1031 | + if ($info['onglet']) { |
|
| 1032 | + $onglets = array_merge($onglets, $info['onglet']); |
|
| 1033 | + } |
|
| 1034 | + foreach ($contenu as $charge => $v) { |
|
| 1035 | + // si pas declare/detecte a la lecture du paquet.xml, |
|
| 1036 | + // detecer a nouveau ici puisque son ajout ne provoque pas une modif du paquet.xml |
|
| 1037 | + // donc ni sa relecture, ni sa detection |
|
| 1038 | + if ( |
|
| 1039 | + !isset($info[$charge]) |
|
| 1040 | + && $dir |
|
| 1041 | + && !str_contains($dir, ':') |
|
| 1042 | + && file_exists("$dir$plug/paquet.xml") // uniquement pour les paquet.xml |
|
| 1043 | + ) { |
|
| 1044 | + if (is_readable("$dir$plug/" . ($file = $info['prefix'] . '_' . $charge . '.php'))) { |
|
| 1045 | + $info[$charge] = [$file]; |
|
| 1046 | + } |
|
| 1047 | + } |
|
| 1048 | + if (isset($info[$charge])) { |
|
| 1049 | + $files = $info[$charge]; |
|
| 1050 | + foreach ($files as $k => $file) { |
|
| 1051 | + // on genere un if file_exists devant chaque include |
|
| 1052 | + // pour pouvoir garder le meme niveau d'erreur general |
|
| 1053 | + $file = trim($file); |
|
| 1054 | + if ( |
|
| 1055 | + !is_readable("$dir$plug/$file") && file_exists("$dir$plug/paquet.xml") |
|
| 1056 | + ) { |
|
| 1057 | + unset($info[$charge][$k]); |
|
| 1058 | + } else { |
|
| 1059 | + $_file = $root_dir_type . ".'$plug/$file'"; |
|
| 1060 | + $contenu[$charge] .= "include_once_check($_file);\n"; |
|
| 1061 | + } |
|
| 1062 | + } |
|
| 1063 | + } |
|
| 1064 | + } |
|
| 1065 | + $sign .= md5(serialize($info)); |
|
| 1066 | + } |
|
| 1067 | + } |
|
| 1068 | + |
|
| 1069 | + $contenu['options'] = "define('_PLUGINS_HASH','" . md5($sign) . "');\n" . $contenu['options']; |
|
| 1070 | + $contenu['fonctions'] .= plugin_ongletbouton('boutons_plugins', $boutons) |
|
| 1071 | + . plugin_ongletbouton('onglets_plugins', $onglets); |
|
| 1072 | + |
|
| 1073 | + ecrire_fichier_php(_CACHE_PLUGINS_OPT, $contenu['options']); |
|
| 1074 | + ecrire_fichier_php(_CACHE_PLUGINS_FCT, $contenu['fonctions']); |
|
| 1075 | 1075 | } |
| 1076 | 1076 | |
| 1077 | 1077 | /** |
@@ -1090,24 +1090,24 @@ discard block |
||
| 1090 | 1090 | * @return string Code php |
| 1091 | 1091 | */ |
| 1092 | 1092 | function plugin_ongletbouton($nom, $val) { |
| 1093 | - if (!$val) { |
|
| 1094 | - $val = []; |
|
| 1095 | - } |
|
| 1096 | - |
|
| 1097 | - $val = serialize($val); |
|
| 1098 | - $md5 = md5($val); |
|
| 1099 | - |
|
| 1100 | - if (!defined("_UPDATED_$nom")) { |
|
| 1101 | - define("_UPDATED_$nom", $val); |
|
| 1102 | - define("_UPDATED_md5_$nom", $md5); |
|
| 1103 | - } |
|
| 1104 | - $val = "unserialize('" . str_replace("'", "\'", $val) . "')"; |
|
| 1105 | - |
|
| 1106 | - return |
|
| 1107 | - "if (!function_exists('$nom')) {\n" |
|
| 1108 | - . "function $nom(){return defined('_UPDATED_$nom')?unserialize(_UPDATED_$nom):$val;}\n" |
|
| 1109 | - . "function md5_$nom(){return defined('_UPDATED_md5_$nom')?_UPDATED_md5_$nom:'" . $md5 . "';}\n" |
|
| 1110 | - . "}\n"; |
|
| 1093 | + if (!$val) { |
|
| 1094 | + $val = []; |
|
| 1095 | + } |
|
| 1096 | + |
|
| 1097 | + $val = serialize($val); |
|
| 1098 | + $md5 = md5($val); |
|
| 1099 | + |
|
| 1100 | + if (!defined("_UPDATED_$nom")) { |
|
| 1101 | + define("_UPDATED_$nom", $val); |
|
| 1102 | + define("_UPDATED_md5_$nom", $md5); |
|
| 1103 | + } |
|
| 1104 | + $val = "unserialize('" . str_replace("'", "\'", $val) . "')"; |
|
| 1105 | + |
|
| 1106 | + return |
|
| 1107 | + "if (!function_exists('$nom')) {\n" |
|
| 1108 | + . "function $nom(){return defined('_UPDATED_$nom')?unserialize(_UPDATED_$nom):$val;}\n" |
|
| 1109 | + . "function md5_$nom(){return defined('_UPDATED_md5_$nom')?_UPDATED_md5_$nom:'" . $md5 . "';}\n" |
|
| 1110 | + . "}\n"; |
|
| 1111 | 1111 | } |
| 1112 | 1112 | |
| 1113 | 1113 | /** |
@@ -1122,15 +1122,15 @@ discard block |
||
| 1122 | 1122 | **/ |
| 1123 | 1123 | function plugins_amorcer_plugins_actifs() { |
| 1124 | 1124 | |
| 1125 | - if (@is_readable(_CACHE_PLUGINS_PATH)) { |
|
| 1126 | - include_once(_CACHE_PLUGINS_PATH); |
|
| 1127 | - } |
|
| 1125 | + if (@is_readable(_CACHE_PLUGINS_PATH)) { |
|
| 1126 | + include_once(_CACHE_PLUGINS_PATH); |
|
| 1127 | + } |
|
| 1128 | 1128 | |
| 1129 | - if (@is_readable(_CACHE_PLUGINS_OPT)) { |
|
| 1130 | - include_once(_CACHE_PLUGINS_OPT); |
|
| 1131 | - } else { |
|
| 1132 | - spip_logger()->info('pipelines desactives: impossible de produire ' . _CACHE_PLUGINS_OPT); |
|
| 1133 | - } |
|
| 1129 | + if (@is_readable(_CACHE_PLUGINS_OPT)) { |
|
| 1130 | + include_once(_CACHE_PLUGINS_OPT); |
|
| 1131 | + } else { |
|
| 1132 | + spip_logger()->info('pipelines desactives: impossible de produire ' . _CACHE_PLUGINS_OPT); |
|
| 1133 | + } |
|
| 1134 | 1134 | } |
| 1135 | 1135 | |
| 1136 | 1136 | /** |
@@ -1153,140 +1153,140 @@ discard block |
||
| 1153 | 1153 | * Couples (nom du pipeline => Code PHP à insérer au début du pipeline) |
| 1154 | 1154 | **/ |
| 1155 | 1155 | function pipeline_matrice_precompile($plugin_valides, $ordre, $pipe_recherche) { |
| 1156 | - static $liste_pipe_manquants = []; |
|
| 1157 | - if (($pipe_recherche) && (!in_array($pipe_recherche, $liste_pipe_manquants))) { |
|
| 1158 | - $liste_pipe_manquants[] = $pipe_recherche; |
|
| 1159 | - } |
|
| 1160 | - |
|
| 1161 | - $prepend_code = []; |
|
| 1162 | - |
|
| 1163 | - foreach ($ordre as $p => $info) { |
|
| 1164 | - // $ordre peur contenir des plugins en attente et non valides pour ce hit |
|
| 1165 | - if (isset($plugin_valides[$p])) { |
|
| 1166 | - $dir_type = $plugin_valides[$p]['dir_type']; |
|
| 1167 | - $root_dir_type = str_replace('_DIR_', '_ROOT_', $dir_type); |
|
| 1168 | - $plug = $plugin_valides[$p]['dir']; |
|
| 1169 | - $prefix = (($info['prefix'] == 'spip') ? '' : $info['prefix'] . '_'); |
|
| 1170 | - if (isset($info['pipeline']) && is_array($info['pipeline'])) { |
|
| 1171 | - foreach ($info['pipeline'] as $pipe) { |
|
| 1172 | - $nom = $pipe['nom']; |
|
| 1173 | - if (isset($pipe['action'])) { |
|
| 1174 | - $action = $pipe['action']; |
|
| 1175 | - } else { |
|
| 1176 | - $action = $nom; |
|
| 1177 | - } |
|
| 1178 | - $nomlower = strtolower($nom); |
|
| 1179 | - if ( |
|
| 1180 | - $nomlower != $nom |
|
| 1181 | - && isset($GLOBALS['spip_pipeline'][$nom]) |
|
| 1182 | - && !isset($GLOBALS['spip_pipeline'][$nomlower]) |
|
| 1183 | - ) { |
|
| 1184 | - $GLOBALS['spip_pipeline'][$nomlower] = $GLOBALS['spip_pipeline'][$nom]; |
|
| 1185 | - unset($GLOBALS['spip_pipeline'][$nom]); |
|
| 1186 | - } |
|
| 1187 | - $nom = $nomlower; |
|
| 1188 | - // une action vide est une declaration qui ne doit pas etre compilee ! |
|
| 1189 | - if (!isset($GLOBALS['spip_pipeline'][$nom])) { // creer le pipeline eventuel |
|
| 1190 | - $GLOBALS['spip_pipeline'][$nom] = ''; |
|
| 1191 | - } |
|
| 1192 | - if ($action) { |
|
| 1193 | - if (!str_contains($GLOBALS['spip_pipeline'][$nom], (string) "|$prefix$action")) { |
|
| 1194 | - $GLOBALS['spip_pipeline'][$nom] = preg_replace( |
|
| 1195 | - ',(\|\||$),', |
|
| 1196 | - "|$prefix$action\\1", |
|
| 1197 | - $GLOBALS['spip_pipeline'][$nom], |
|
| 1198 | - 1 |
|
| 1199 | - ); |
|
| 1200 | - } |
|
| 1201 | - if (isset($pipe['inclure'])) { |
|
| 1202 | - $GLOBALS['spip_matrice']["$prefix$action"] = |
|
| 1203 | - "$root_dir_type:$plug/" . $pipe['inclure']; |
|
| 1204 | - } |
|
| 1205 | - } |
|
| 1206 | - } |
|
| 1207 | - } |
|
| 1208 | - if (isset($info['genie']) && (is_countable($info['genie']) ? count($info['genie']) : 0)) { |
|
| 1209 | - if (!isset($prepend_code['taches_generales_cron'])) { |
|
| 1210 | - $prepend_code['taches_generales_cron'] = ''; |
|
| 1211 | - } |
|
| 1212 | - foreach ($info['genie'] as $genie) { |
|
| 1213 | - $nom = $prefix . $genie['nom']; |
|
| 1214 | - $periode = max(60, intval($genie['periode'])); |
|
| 1215 | - if (charger_fonction($nom, 'genie', true)) { |
|
| 1216 | - $prepend_code['taches_generales_cron'] .= "\$val['$nom'] = $periode;\n"; |
|
| 1217 | - } else { |
|
| 1218 | - spip_logger()->error("Fonction genie_$nom introuvable"); |
|
| 1219 | - } |
|
| 1220 | - } |
|
| 1221 | - } |
|
| 1222 | - if (isset($info['style']) && (is_countable($info['style']) ? count($info['style']) : 0)) { |
|
| 1223 | - if (!isset($prepend_code['insert_head_css'])) { |
|
| 1224 | - $prepend_code['insert_head_css'] = ''; |
|
| 1225 | - } |
|
| 1226 | - if (!isset($prepend_code['header_prive_css'])) { |
|
| 1227 | - $prepend_code['header_prive_css'] = ''; |
|
| 1228 | - } |
|
| 1229 | - foreach ($info['style'] as $style) { |
|
| 1230 | - if (isset($style['path']) && $style['path']) { |
|
| 1231 | - $code = "if (\$f=timestamp(direction_css(find_in_path('" . addslashes($style['path']) . "')))) "; |
|
| 1232 | - } else { |
|
| 1233 | - $code = "if (\$f='" . addslashes($style['url']) . "') "; |
|
| 1234 | - } |
|
| 1235 | - $code .= "\$val .= '<link rel=\"stylesheet\" href=\"'.\$f.'\" type=\"text/css\""; |
|
| 1236 | - if (isset($style['media']) && strlen($style['media'])) { |
|
| 1237 | - $code .= ' media="' . addslashes($style['media']) . '"'; |
|
| 1238 | - } |
|
| 1239 | - $code .= "/>';\n"; |
|
| 1240 | - if ($style['type'] != 'prive') { |
|
| 1241 | - $prepend_code['insert_head_css'] .= $code; |
|
| 1242 | - } |
|
| 1243 | - if ($style['type'] != 'public') { |
|
| 1244 | - $prepend_code['header_prive_css'] .= $code; |
|
| 1245 | - } |
|
| 1246 | - } |
|
| 1247 | - } |
|
| 1248 | - if (!isset($prepend_code['insert_head'])) { |
|
| 1249 | - $prepend_code['insert_head'] = ''; |
|
| 1250 | - } |
|
| 1251 | - if (!isset($prepend_code['header_prive'])) { |
|
| 1252 | - $prepend_code['header_prive'] = ''; |
|
| 1253 | - } |
|
| 1254 | - if (isset($info['script']) && (is_countable($info['script']) ? count($info['script']) : 0)) { |
|
| 1255 | - foreach ($info['script'] as $script) { |
|
| 1256 | - if (isset($script['path']) && $script['path']) { |
|
| 1257 | - $code = "if (\$f=timestamp(find_in_path('" . addslashes($script['path']) . "'))) "; |
|
| 1258 | - } else { |
|
| 1259 | - $code = "if (\$f='" . addslashes($script['url']) . "') "; |
|
| 1260 | - } |
|
| 1261 | - $code .= "\$val .= '<script src=\"'.\$f.'\" type=\"text/javascript\"></script>';\n"; |
|
| 1262 | - if ($script['type'] != 'prive') { |
|
| 1263 | - $prepend_code['insert_head'] .= $code; |
|
| 1264 | - } |
|
| 1265 | - if ($script['type'] != 'public') { |
|
| 1266 | - $prepend_code['header_prive'] .= $code; |
|
| 1267 | - } |
|
| 1268 | - } |
|
| 1269 | - } |
|
| 1270 | - } |
|
| 1271 | - } |
|
| 1272 | - |
|
| 1273 | - $prepend_code['insert_head'] = |
|
| 1274 | - "include_once_check(_DIR_RESTREINT . 'inc/pipelines.php');\n" |
|
| 1275 | - . "\$val = minipipe('f_jQuery', \$val);\n" |
|
| 1276 | - . $prepend_code['insert_head']; |
|
| 1277 | - $prepend_code['header_prive'] = |
|
| 1278 | - "include_once_check(_DIR_RESTREINT . 'inc/pipelines_ecrire.php');\n" |
|
| 1279 | - . "\$val = minipipe('f_jQuery_prive', \$val);\n" |
|
| 1280 | - . $prepend_code['header_prive']; |
|
| 1281 | - |
|
| 1282 | - // on ajoute les pipe qui ont ete recenses manquants |
|
| 1283 | - foreach ($liste_pipe_manquants as $add_pipe) { |
|
| 1284 | - if (!isset($GLOBALS['spip_pipeline'][$add_pipe])) { |
|
| 1285 | - $GLOBALS['spip_pipeline'][$add_pipe] = ''; |
|
| 1286 | - } |
|
| 1287 | - } |
|
| 1288 | - |
|
| 1289 | - return $prepend_code; |
|
| 1156 | + static $liste_pipe_manquants = []; |
|
| 1157 | + if (($pipe_recherche) && (!in_array($pipe_recherche, $liste_pipe_manquants))) { |
|
| 1158 | + $liste_pipe_manquants[] = $pipe_recherche; |
|
| 1159 | + } |
|
| 1160 | + |
|
| 1161 | + $prepend_code = []; |
|
| 1162 | + |
|
| 1163 | + foreach ($ordre as $p => $info) { |
|
| 1164 | + // $ordre peur contenir des plugins en attente et non valides pour ce hit |
|
| 1165 | + if (isset($plugin_valides[$p])) { |
|
| 1166 | + $dir_type = $plugin_valides[$p]['dir_type']; |
|
| 1167 | + $root_dir_type = str_replace('_DIR_', '_ROOT_', $dir_type); |
|
| 1168 | + $plug = $plugin_valides[$p]['dir']; |
|
| 1169 | + $prefix = (($info['prefix'] == 'spip') ? '' : $info['prefix'] . '_'); |
|
| 1170 | + if (isset($info['pipeline']) && is_array($info['pipeline'])) { |
|
| 1171 | + foreach ($info['pipeline'] as $pipe) { |
|
| 1172 | + $nom = $pipe['nom']; |
|
| 1173 | + if (isset($pipe['action'])) { |
|
| 1174 | + $action = $pipe['action']; |
|
| 1175 | + } else { |
|
| 1176 | + $action = $nom; |
|
| 1177 | + } |
|
| 1178 | + $nomlower = strtolower($nom); |
|
| 1179 | + if ( |
|
| 1180 | + $nomlower != $nom |
|
| 1181 | + && isset($GLOBALS['spip_pipeline'][$nom]) |
|
| 1182 | + && !isset($GLOBALS['spip_pipeline'][$nomlower]) |
|
| 1183 | + ) { |
|
| 1184 | + $GLOBALS['spip_pipeline'][$nomlower] = $GLOBALS['spip_pipeline'][$nom]; |
|
| 1185 | + unset($GLOBALS['spip_pipeline'][$nom]); |
|
| 1186 | + } |
|
| 1187 | + $nom = $nomlower; |
|
| 1188 | + // une action vide est une declaration qui ne doit pas etre compilee ! |
|
| 1189 | + if (!isset($GLOBALS['spip_pipeline'][$nom])) { // creer le pipeline eventuel |
|
| 1190 | + $GLOBALS['spip_pipeline'][$nom] = ''; |
|
| 1191 | + } |
|
| 1192 | + if ($action) { |
|
| 1193 | + if (!str_contains($GLOBALS['spip_pipeline'][$nom], (string) "|$prefix$action")) { |
|
| 1194 | + $GLOBALS['spip_pipeline'][$nom] = preg_replace( |
|
| 1195 | + ',(\|\||$),', |
|
| 1196 | + "|$prefix$action\\1", |
|
| 1197 | + $GLOBALS['spip_pipeline'][$nom], |
|
| 1198 | + 1 |
|
| 1199 | + ); |
|
| 1200 | + } |
|
| 1201 | + if (isset($pipe['inclure'])) { |
|
| 1202 | + $GLOBALS['spip_matrice']["$prefix$action"] = |
|
| 1203 | + "$root_dir_type:$plug/" . $pipe['inclure']; |
|
| 1204 | + } |
|
| 1205 | + } |
|
| 1206 | + } |
|
| 1207 | + } |
|
| 1208 | + if (isset($info['genie']) && (is_countable($info['genie']) ? count($info['genie']) : 0)) { |
|
| 1209 | + if (!isset($prepend_code['taches_generales_cron'])) { |
|
| 1210 | + $prepend_code['taches_generales_cron'] = ''; |
|
| 1211 | + } |
|
| 1212 | + foreach ($info['genie'] as $genie) { |
|
| 1213 | + $nom = $prefix . $genie['nom']; |
|
| 1214 | + $periode = max(60, intval($genie['periode'])); |
|
| 1215 | + if (charger_fonction($nom, 'genie', true)) { |
|
| 1216 | + $prepend_code['taches_generales_cron'] .= "\$val['$nom'] = $periode;\n"; |
|
| 1217 | + } else { |
|
| 1218 | + spip_logger()->error("Fonction genie_$nom introuvable"); |
|
| 1219 | + } |
|
| 1220 | + } |
|
| 1221 | + } |
|
| 1222 | + if (isset($info['style']) && (is_countable($info['style']) ? count($info['style']) : 0)) { |
|
| 1223 | + if (!isset($prepend_code['insert_head_css'])) { |
|
| 1224 | + $prepend_code['insert_head_css'] = ''; |
|
| 1225 | + } |
|
| 1226 | + if (!isset($prepend_code['header_prive_css'])) { |
|
| 1227 | + $prepend_code['header_prive_css'] = ''; |
|
| 1228 | + } |
|
| 1229 | + foreach ($info['style'] as $style) { |
|
| 1230 | + if (isset($style['path']) && $style['path']) { |
|
| 1231 | + $code = "if (\$f=timestamp(direction_css(find_in_path('" . addslashes($style['path']) . "')))) "; |
|
| 1232 | + } else { |
|
| 1233 | + $code = "if (\$f='" . addslashes($style['url']) . "') "; |
|
| 1234 | + } |
|
| 1235 | + $code .= "\$val .= '<link rel=\"stylesheet\" href=\"'.\$f.'\" type=\"text/css\""; |
|
| 1236 | + if (isset($style['media']) && strlen($style['media'])) { |
|
| 1237 | + $code .= ' media="' . addslashes($style['media']) . '"'; |
|
| 1238 | + } |
|
| 1239 | + $code .= "/>';\n"; |
|
| 1240 | + if ($style['type'] != 'prive') { |
|
| 1241 | + $prepend_code['insert_head_css'] .= $code; |
|
| 1242 | + } |
|
| 1243 | + if ($style['type'] != 'public') { |
|
| 1244 | + $prepend_code['header_prive_css'] .= $code; |
|
| 1245 | + } |
|
| 1246 | + } |
|
| 1247 | + } |
|
| 1248 | + if (!isset($prepend_code['insert_head'])) { |
|
| 1249 | + $prepend_code['insert_head'] = ''; |
|
| 1250 | + } |
|
| 1251 | + if (!isset($prepend_code['header_prive'])) { |
|
| 1252 | + $prepend_code['header_prive'] = ''; |
|
| 1253 | + } |
|
| 1254 | + if (isset($info['script']) && (is_countable($info['script']) ? count($info['script']) : 0)) { |
|
| 1255 | + foreach ($info['script'] as $script) { |
|
| 1256 | + if (isset($script['path']) && $script['path']) { |
|
| 1257 | + $code = "if (\$f=timestamp(find_in_path('" . addslashes($script['path']) . "'))) "; |
|
| 1258 | + } else { |
|
| 1259 | + $code = "if (\$f='" . addslashes($script['url']) . "') "; |
|
| 1260 | + } |
|
| 1261 | + $code .= "\$val .= '<script src=\"'.\$f.'\" type=\"text/javascript\"></script>';\n"; |
|
| 1262 | + if ($script['type'] != 'prive') { |
|
| 1263 | + $prepend_code['insert_head'] .= $code; |
|
| 1264 | + } |
|
| 1265 | + if ($script['type'] != 'public') { |
|
| 1266 | + $prepend_code['header_prive'] .= $code; |
|
| 1267 | + } |
|
| 1268 | + } |
|
| 1269 | + } |
|
| 1270 | + } |
|
| 1271 | + } |
|
| 1272 | + |
|
| 1273 | + $prepend_code['insert_head'] = |
|
| 1274 | + "include_once_check(_DIR_RESTREINT . 'inc/pipelines.php');\n" |
|
| 1275 | + . "\$val = minipipe('f_jQuery', \$val);\n" |
|
| 1276 | + . $prepend_code['insert_head']; |
|
| 1277 | + $prepend_code['header_prive'] = |
|
| 1278 | + "include_once_check(_DIR_RESTREINT . 'inc/pipelines_ecrire.php');\n" |
|
| 1279 | + . "\$val = minipipe('f_jQuery_prive', \$val);\n" |
|
| 1280 | + . $prepend_code['header_prive']; |
|
| 1281 | + |
|
| 1282 | + // on ajoute les pipe qui ont ete recenses manquants |
|
| 1283 | + foreach ($liste_pipe_manquants as $add_pipe) { |
|
| 1284 | + if (!isset($GLOBALS['spip_pipeline'][$add_pipe])) { |
|
| 1285 | + $GLOBALS['spip_pipeline'][$add_pipe] = ''; |
|
| 1286 | + } |
|
| 1287 | + } |
|
| 1288 | + |
|
| 1289 | + return $prepend_code; |
|
| 1290 | 1290 | } |
| 1291 | 1291 | |
| 1292 | 1292 | /** |
@@ -1313,62 +1313,62 @@ discard block |
||
| 1313 | 1313 | **/ |
| 1314 | 1314 | function pipeline_precompile($prepend_code = []) { |
| 1315 | 1315 | |
| 1316 | - $all_pipes = $all_pipes_end = ''; |
|
| 1317 | - if (!empty($GLOBALS['spip_pipeline']['all'])) { |
|
| 1318 | - $a = explode('||', $GLOBALS['spip_pipeline']['all'], 2); |
|
| 1319 | - unset($GLOBALS['spip_pipeline']['all']); |
|
| 1320 | - $all_pipes = trim(array_shift($a)); |
|
| 1321 | - if ($all_pipes) { |
|
| 1322 | - $all_pipes = '|' . ltrim($all_pipes, '|'); |
|
| 1323 | - } |
|
| 1324 | - if (count($a)) { |
|
| 1325 | - $all_pipes_end = '||' . array_shift($a); |
|
| 1326 | - } |
|
| 1327 | - } |
|
| 1328 | - $content = ''; |
|
| 1329 | - foreach ($GLOBALS['spip_pipeline'] as $action => $pipeline) { |
|
| 1330 | - $s_inc = ''; |
|
| 1331 | - $s_call = ''; |
|
| 1332 | - if ($all_pipes) { |
|
| 1333 | - $pipeline = preg_replace(',(\|\||$),', "$all_pipes\\1", $pipeline, 1); |
|
| 1334 | - } |
|
| 1335 | - if ($all_pipes_end) { |
|
| 1336 | - $pipeline .= $all_pipes_end; |
|
| 1337 | - } |
|
| 1338 | - $pipe = array_filter(explode('|', $pipeline)); |
|
| 1339 | - // Eclater le pipeline en filtres et appliquer chaque filtre |
|
| 1340 | - foreach ($pipe as $fonc) { |
|
| 1341 | - $fonc = trim($fonc); |
|
| 1342 | - $s_call .= '$val = minipipe(\'' . $fonc . '\', $val);' . "\n"; |
|
| 1343 | - if (isset($GLOBALS['spip_matrice'][$fonc])) { |
|
| 1344 | - $file = $GLOBALS['spip_matrice'][$fonc]; |
|
| 1345 | - $file = "'$file'"; |
|
| 1346 | - // si un _DIR_XXX: est dans la chaine, on extrait la constante |
|
| 1347 | - if (preg_match(',(_(DIR|ROOT)_[A-Z_]+):,Ums', $file, $regs)) { |
|
| 1348 | - $dir = $regs[1]; |
|
| 1349 | - $root_dir = str_replace('_DIR_', '_ROOT_', $dir); |
|
| 1350 | - if (defined($root_dir)) { |
|
| 1351 | - $dir = $root_dir; |
|
| 1352 | - } |
|
| 1353 | - $file = str_replace($regs[0], "'." . $dir . ".'", $file); |
|
| 1354 | - $file = str_replace("''.", '', $file); |
|
| 1355 | - $file = str_replace(constant($dir), '', $file); |
|
| 1356 | - } |
|
| 1357 | - $s_inc .= "include_once_check($file);\n"; |
|
| 1358 | - } |
|
| 1359 | - } |
|
| 1360 | - if (strlen($s_inc)) { |
|
| 1361 | - $s_inc = "static \$inc=null;\nif (!\$inc){\n$s_inc\$inc=true;\n}\n"; |
|
| 1362 | - } |
|
| 1363 | - $content .= "// Pipeline $action \n" |
|
| 1364 | - . "function execute_pipeline_$action(&\$val){\n" |
|
| 1365 | - . $s_inc |
|
| 1366 | - . ((isset($prepend_code[$action]) && strlen($prepend_code[$action])) ? trim($prepend_code[$action]) . "\n" : '') |
|
| 1367 | - . $s_call |
|
| 1368 | - . "return \$val;\n}\n"; |
|
| 1369 | - } |
|
| 1370 | - ecrire_fichier_php(_CACHE_PIPELINES, $content); |
|
| 1371 | - clear_path_cache(); |
|
| 1316 | + $all_pipes = $all_pipes_end = ''; |
|
| 1317 | + if (!empty($GLOBALS['spip_pipeline']['all'])) { |
|
| 1318 | + $a = explode('||', $GLOBALS['spip_pipeline']['all'], 2); |
|
| 1319 | + unset($GLOBALS['spip_pipeline']['all']); |
|
| 1320 | + $all_pipes = trim(array_shift($a)); |
|
| 1321 | + if ($all_pipes) { |
|
| 1322 | + $all_pipes = '|' . ltrim($all_pipes, '|'); |
|
| 1323 | + } |
|
| 1324 | + if (count($a)) { |
|
| 1325 | + $all_pipes_end = '||' . array_shift($a); |
|
| 1326 | + } |
|
| 1327 | + } |
|
| 1328 | + $content = ''; |
|
| 1329 | + foreach ($GLOBALS['spip_pipeline'] as $action => $pipeline) { |
|
| 1330 | + $s_inc = ''; |
|
| 1331 | + $s_call = ''; |
|
| 1332 | + if ($all_pipes) { |
|
| 1333 | + $pipeline = preg_replace(',(\|\||$),', "$all_pipes\\1", $pipeline, 1); |
|
| 1334 | + } |
|
| 1335 | + if ($all_pipes_end) { |
|
| 1336 | + $pipeline .= $all_pipes_end; |
|
| 1337 | + } |
|
| 1338 | + $pipe = array_filter(explode('|', $pipeline)); |
|
| 1339 | + // Eclater le pipeline en filtres et appliquer chaque filtre |
|
| 1340 | + foreach ($pipe as $fonc) { |
|
| 1341 | + $fonc = trim($fonc); |
|
| 1342 | + $s_call .= '$val = minipipe(\'' . $fonc . '\', $val);' . "\n"; |
|
| 1343 | + if (isset($GLOBALS['spip_matrice'][$fonc])) { |
|
| 1344 | + $file = $GLOBALS['spip_matrice'][$fonc]; |
|
| 1345 | + $file = "'$file'"; |
|
| 1346 | + // si un _DIR_XXX: est dans la chaine, on extrait la constante |
|
| 1347 | + if (preg_match(',(_(DIR|ROOT)_[A-Z_]+):,Ums', $file, $regs)) { |
|
| 1348 | + $dir = $regs[1]; |
|
| 1349 | + $root_dir = str_replace('_DIR_', '_ROOT_', $dir); |
|
| 1350 | + if (defined($root_dir)) { |
|
| 1351 | + $dir = $root_dir; |
|
| 1352 | + } |
|
| 1353 | + $file = str_replace($regs[0], "'." . $dir . ".'", $file); |
|
| 1354 | + $file = str_replace("''.", '', $file); |
|
| 1355 | + $file = str_replace(constant($dir), '', $file); |
|
| 1356 | + } |
|
| 1357 | + $s_inc .= "include_once_check($file);\n"; |
|
| 1358 | + } |
|
| 1359 | + } |
|
| 1360 | + if (strlen($s_inc)) { |
|
| 1361 | + $s_inc = "static \$inc=null;\nif (!\$inc){\n$s_inc\$inc=true;\n}\n"; |
|
| 1362 | + } |
|
| 1363 | + $content .= "// Pipeline $action \n" |
|
| 1364 | + . "function execute_pipeline_$action(&\$val){\n" |
|
| 1365 | + . $s_inc |
|
| 1366 | + . ((isset($prepend_code[$action]) && strlen($prepend_code[$action])) ? trim($prepend_code[$action]) . "\n" : '') |
|
| 1367 | + . $s_call |
|
| 1368 | + . "return \$val;\n}\n"; |
|
| 1369 | + } |
|
| 1370 | + ecrire_fichier_php(_CACHE_PIPELINES, $content); |
|
| 1371 | + clear_path_cache(); |
|
| 1372 | 1372 | } |
| 1373 | 1373 | |
| 1374 | 1374 | |
@@ -1381,12 +1381,12 @@ discard block |
||
| 1381 | 1381 | * true si le plugin est actif, false sinon |
| 1382 | 1382 | **/ |
| 1383 | 1383 | function plugin_est_installe($plug_path) { |
| 1384 | - $plugin_installes = isset($GLOBALS['meta']['plugin_installes']) ? unserialize($GLOBALS['meta']['plugin_installes']) : []; |
|
| 1385 | - if (!$plugin_installes) { |
|
| 1386 | - return false; |
|
| 1387 | - } |
|
| 1384 | + $plugin_installes = isset($GLOBALS['meta']['plugin_installes']) ? unserialize($GLOBALS['meta']['plugin_installes']) : []; |
|
| 1385 | + if (!$plugin_installes) { |
|
| 1386 | + return false; |
|
| 1387 | + } |
|
| 1388 | 1388 | |
| 1389 | - return in_array($plug_path, $plugin_installes); |
|
| 1389 | + return in_array($plug_path, $plugin_installes); |
|
| 1390 | 1390 | } |
| 1391 | 1391 | |
| 1392 | 1392 | |
@@ -1399,46 +1399,46 @@ discard block |
||
| 1399 | 1399 | * @uses plugins_installer_dist() |
| 1400 | 1400 | **/ |
| 1401 | 1401 | function plugin_installes_meta() { |
| 1402 | - if (isset($GLOBALS['fichier_php_compile_recent'])) { |
|
| 1403 | - // attendre eventuellement l'invalidation du cache opcode |
|
| 1404 | - spip_attend_invalidation_opcode_cache($GLOBALS['fichier_php_compile_recent']); |
|
| 1405 | - } |
|
| 1406 | - |
|
| 1407 | - $installer_plugins = charger_fonction('installer', 'plugins'); |
|
| 1408 | - $meta_plug_installes = []; |
|
| 1409 | - foreach (unserialize($GLOBALS['meta']['plugin']) as $prefix => $resume) { |
|
| 1410 | - if ($plug = $resume['dir']) { |
|
| 1411 | - $infos = $installer_plugins($plug, 'install', $resume['dir_type']); |
|
| 1412 | - if ($infos) { |
|
| 1413 | - if (!is_array($infos) || $infos['install_test'][0]) { |
|
| 1414 | - $meta_plug_installes[] = $plug; |
|
| 1415 | - } |
|
| 1416 | - if (is_array($infos)) { |
|
| 1417 | - [$ok, $trace] = $infos['install_test']; |
|
| 1418 | - $titre = _T('plugin_titre_installation', ['plugin' => typo($infos['nom'])]); |
|
| 1419 | - $result = ($ok ? ((isset($infos['upgrade']) && $infos['upgrade']) ? _T('plugin_info_upgrade_ok') : _T('plugin_info_install_ok')) : _T('avis_operation_echec')); |
|
| 1420 | - if (_IS_CLI) { |
|
| 1421 | - include_spip('inc/filtres'); |
|
| 1422 | - $trace = ltrim(textebrut($trace) . "\n" . $result); |
|
| 1423 | - $trace = ' ' . str_replace("\n", "\n ", $trace); |
|
| 1424 | - echo "\n" . ($ok ? 'OK ' : '/!\ ') . textebrut($titre) . "\n", |
|
| 1425 | - $trace, |
|
| 1426 | - "\n"; |
|
| 1427 | - } |
|
| 1428 | - else { |
|
| 1429 | - include_spip('inc/filtres_boites'); |
|
| 1430 | - echo "<div class='install-plugins svp_retour'>" |
|
| 1431 | - . boite_ouvrir($titre, ($ok ? 'success' : 'error')) |
|
| 1432 | - . $trace |
|
| 1433 | - . "<div class='result'>$result</div>" |
|
| 1434 | - . boite_fermer() |
|
| 1435 | - . '</div>'; |
|
| 1436 | - } |
|
| 1437 | - } |
|
| 1438 | - } |
|
| 1439 | - } |
|
| 1440 | - } |
|
| 1441 | - ecrire_meta('plugin_installes', serialize($meta_plug_installes), 'non'); |
|
| 1402 | + if (isset($GLOBALS['fichier_php_compile_recent'])) { |
|
| 1403 | + // attendre eventuellement l'invalidation du cache opcode |
|
| 1404 | + spip_attend_invalidation_opcode_cache($GLOBALS['fichier_php_compile_recent']); |
|
| 1405 | + } |
|
| 1406 | + |
|
| 1407 | + $installer_plugins = charger_fonction('installer', 'plugins'); |
|
| 1408 | + $meta_plug_installes = []; |
|
| 1409 | + foreach (unserialize($GLOBALS['meta']['plugin']) as $prefix => $resume) { |
|
| 1410 | + if ($plug = $resume['dir']) { |
|
| 1411 | + $infos = $installer_plugins($plug, 'install', $resume['dir_type']); |
|
| 1412 | + if ($infos) { |
|
| 1413 | + if (!is_array($infos) || $infos['install_test'][0]) { |
|
| 1414 | + $meta_plug_installes[] = $plug; |
|
| 1415 | + } |
|
| 1416 | + if (is_array($infos)) { |
|
| 1417 | + [$ok, $trace] = $infos['install_test']; |
|
| 1418 | + $titre = _T('plugin_titre_installation', ['plugin' => typo($infos['nom'])]); |
|
| 1419 | + $result = ($ok ? ((isset($infos['upgrade']) && $infos['upgrade']) ? _T('plugin_info_upgrade_ok') : _T('plugin_info_install_ok')) : _T('avis_operation_echec')); |
|
| 1420 | + if (_IS_CLI) { |
|
| 1421 | + include_spip('inc/filtres'); |
|
| 1422 | + $trace = ltrim(textebrut($trace) . "\n" . $result); |
|
| 1423 | + $trace = ' ' . str_replace("\n", "\n ", $trace); |
|
| 1424 | + echo "\n" . ($ok ? 'OK ' : '/!\ ') . textebrut($titre) . "\n", |
|
| 1425 | + $trace, |
|
| 1426 | + "\n"; |
|
| 1427 | + } |
|
| 1428 | + else { |
|
| 1429 | + include_spip('inc/filtres_boites'); |
|
| 1430 | + echo "<div class='install-plugins svp_retour'>" |
|
| 1431 | + . boite_ouvrir($titre, ($ok ? 'success' : 'error')) |
|
| 1432 | + . $trace |
|
| 1433 | + . "<div class='result'>$result</div>" |
|
| 1434 | + . boite_fermer() |
|
| 1435 | + . '</div>'; |
|
| 1436 | + } |
|
| 1437 | + } |
|
| 1438 | + } |
|
| 1439 | + } |
|
| 1440 | + } |
|
| 1441 | + ecrire_meta('plugin_installes', serialize($meta_plug_installes), 'non'); |
|
| 1442 | 1442 | } |
| 1443 | 1443 | |
| 1444 | 1444 | /** |
@@ -1452,29 +1452,29 @@ discard block |
||
| 1452 | 1452 | * Commentaire : code écrit en tout début de fichier, après la balise PHP ouvrante |
| 1453 | 1453 | **/ |
| 1454 | 1454 | function ecrire_fichier_php($nom, $contenu, $comment = '') { |
| 1455 | - if (!isset($GLOBALS['fichier_php_compile_recent'])) { |
|
| 1456 | - $GLOBALS['fichier_php_compile_recent'] = 0; |
|
| 1457 | - } |
|
| 1458 | - |
|
| 1459 | - $contenu = '<' . '?php' . "\n" . $comment . "\nif (defined('_ECRIRE_INC_VERSION')) {\n" . $contenu . "}\n?" . '>'; |
|
| 1460 | - // si un fichier existe deja on verifie que son contenu change avant de l'ecraser |
|
| 1461 | - // si pas de modif on ne touche pas au fichier initial |
|
| 1462 | - if (file_exists($nom)) { |
|
| 1463 | - if (str_ends_with($nom, '.php')) { |
|
| 1464 | - $fichier_tmp = substr($nom, 0, -4) . '.tmp.php'; |
|
| 1465 | - } |
|
| 1466 | - else { |
|
| 1467 | - $fichier_tmp = $nom . '.tmp'; |
|
| 1468 | - } |
|
| 1469 | - file_put_contents($fichier_tmp, $contenu); |
|
| 1470 | - if (md5_file($nom) == md5_file($fichier_tmp)) { |
|
| 1471 | - $GLOBALS['fichier_php_compile_recent'] = max($GLOBALS['fichier_php_compile_recent'], filemtime($nom)); |
|
| 1472 | - @unlink($fichier_tmp); |
|
| 1473 | - return; |
|
| 1474 | - } |
|
| 1475 | - @unlink($fichier_tmp); |
|
| 1476 | - } |
|
| 1477 | - ecrire_fichier($nom, $contenu); |
|
| 1478 | - $GLOBALS['fichier_php_compile_recent'] = max($GLOBALS['fichier_php_compile_recent'], filemtime($nom)); |
|
| 1479 | - spip_clear_opcode_cache(realpath($nom)); |
|
| 1455 | + if (!isset($GLOBALS['fichier_php_compile_recent'])) { |
|
| 1456 | + $GLOBALS['fichier_php_compile_recent'] = 0; |
|
| 1457 | + } |
|
| 1458 | + |
|
| 1459 | + $contenu = '<' . '?php' . "\n" . $comment . "\nif (defined('_ECRIRE_INC_VERSION')) {\n" . $contenu . "}\n?" . '>'; |
|
| 1460 | + // si un fichier existe deja on verifie que son contenu change avant de l'ecraser |
|
| 1461 | + // si pas de modif on ne touche pas au fichier initial |
|
| 1462 | + if (file_exists($nom)) { |
|
| 1463 | + if (str_ends_with($nom, '.php')) { |
|
| 1464 | + $fichier_tmp = substr($nom, 0, -4) . '.tmp.php'; |
|
| 1465 | + } |
|
| 1466 | + else { |
|
| 1467 | + $fichier_tmp = $nom . '.tmp'; |
|
| 1468 | + } |
|
| 1469 | + file_put_contents($fichier_tmp, $contenu); |
|
| 1470 | + if (md5_file($nom) == md5_file($fichier_tmp)) { |
|
| 1471 | + $GLOBALS['fichier_php_compile_recent'] = max($GLOBALS['fichier_php_compile_recent'], filemtime($nom)); |
|
| 1472 | + @unlink($fichier_tmp); |
|
| 1473 | + return; |
|
| 1474 | + } |
|
| 1475 | + @unlink($fichier_tmp); |
|
| 1476 | + } |
|
| 1477 | + ecrire_fichier($nom, $contenu); |
|
| 1478 | + $GLOBALS['fichier_php_compile_recent'] = max($GLOBALS['fichier_php_compile_recent'], filemtime($nom)); |
|
| 1479 | + spip_clear_opcode_cache(realpath($nom)); |
|
| 1480 | 1480 | } |
@@ -21,7 +21,7 @@ discard block |
||
| 21 | 21 | |
| 22 | 22 | /** l'adresse du repertoire de telechargement et de decompactage des plugins */ |
| 23 | 23 | if (!defined('_DIR_PLUGINS_AUTO')) { |
| 24 | - define('_DIR_PLUGINS_AUTO', _DIR_PLUGINS . 'auto/'); |
|
| 24 | + define('_DIR_PLUGINS_AUTO', _DIR_PLUGINS.'auto/'); |
|
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | #include_spip('inc/texte'); // ????? Appelle public/parametrer trop tot avant la reconstruction du chemin des plugins. |
@@ -378,7 +378,7 @@ discard block |
||
| 378 | 378 | if ($dir) { |
| 379 | 379 | $dir .= '/'; |
| 380 | 380 | } |
| 381 | - $dir .= 'procure:' . $procure['nom']; |
|
| 381 | + $dir .= 'procure:'.$procure['nom']; |
|
| 382 | 382 | |
| 383 | 383 | $procure['etat'] = '?'; |
| 384 | 384 | $procure['dir_type'] = $resume['dir_type']; |
@@ -558,7 +558,7 @@ discard block |
||
| 558 | 558 | $plug = $resume['dir']; |
| 559 | 559 | $k = $infos[$dir_type][$plug]; |
| 560 | 560 | |
| 561 | - $plug = constant($dir_type) . $plug; |
|
| 561 | + $plug = constant($dir_type).$plug; |
|
| 562 | 562 | if (!isset($msg[$p])) { |
| 563 | 563 | if (isset($resume['erreur']) && $resume['erreur']) { |
| 564 | 564 | $msg[$p] = [$resume['erreur']]; |
@@ -601,10 +601,10 @@ discard block |
||
| 601 | 601 | $list = $raw ? [] : $GLOBALS['meta']['plugin_erreur_activation']; |
| 602 | 602 | } elseif (!$raw) { |
| 603 | 603 | foreach ($list as $plug => $msg) { |
| 604 | - $list[$plug] = '<li>' . _T('plugin_impossible_activer', ['plugin' => $plug]) |
|
| 605 | - . '<ul><li>' . implode('</li><li>', $msg) . '</li></ul></li>'; |
|
| 604 | + $list[$plug] = '<li>'._T('plugin_impossible_activer', ['plugin' => $plug]) |
|
| 605 | + . '<ul><li>'.implode('</li><li>', $msg).'</li></ul></li>'; |
|
| 606 | 606 | } |
| 607 | - $list = '<ul>' . join("\n", $list) . '</ul>'; |
|
| 607 | + $list = '<ul>'.join("\n", $list).'</ul>'; |
|
| 608 | 608 | } |
| 609 | 609 | if ($raz) { |
| 610 | 610 | effacer_meta('plugin_erreur_activation'); |
@@ -718,13 +718,13 @@ discard block |
||
| 718 | 718 | if ($minimum_inclus && spip_version_compare($version, $minimum, '<')) { |
| 719 | 719 | return _T("plugin_{$balise}_{$type}", [ |
| 720 | 720 | 'plugin' => $nom, |
| 721 | - 'version' => ' ≥ ' . $minimum |
|
| 721 | + 'version' => ' ≥ '.$minimum |
|
| 722 | 722 | ]); |
| 723 | 723 | } |
| 724 | 724 | if (!$minimum_inclus && spip_version_compare($version, $minimum, '<=')) { |
| 725 | 725 | return _T("plugin_{$balise}_{$type}", [ |
| 726 | 726 | 'plugin' => $nom, |
| 727 | - 'version' => ' > ' . $minimum |
|
| 727 | + 'version' => ' > '.$minimum |
|
| 728 | 728 | ]); |
| 729 | 729 | } |
| 730 | 730 | } |
@@ -733,13 +733,13 @@ discard block |
||
| 733 | 733 | if ($maximum_inclus && spip_version_compare($version, $maximum, '>')) { |
| 734 | 734 | return _T("plugin_{$balise}_{$type}", [ |
| 735 | 735 | 'plugin' => $nom, |
| 736 | - 'version' => ' ≤ ' . $maximum |
|
| 736 | + 'version' => ' ≤ '.$maximum |
|
| 737 | 737 | ]); |
| 738 | 738 | } |
| 739 | 739 | if (!$maximum_inclus && spip_version_compare($version, $maximum, '>=')) { |
| 740 | 740 | return _T("plugin_{$balise}_plugin", [ |
| 741 | 741 | 'plugin' => $nom, |
| 742 | - 'version' => ' < ' . $maximum |
|
| 742 | + 'version' => ' < '.$maximum |
|
| 743 | 743 | ]); |
| 744 | 744 | } |
| 745 | 745 | } |
@@ -758,7 +758,7 @@ discard block |
||
| 758 | 758 | include_spip('inc/charger_plugin'); |
| 759 | 759 | $url = '<br />' . bouton_telechargement_plugin($url, 'lib'); |
| 760 | 760 | }*/ |
| 761 | - return _T('plugin_necessite_lib', ['lib' => $lib]) . " <a href='$url'>$url</a>"; |
|
| 761 | + return _T('plugin_necessite_lib', ['lib' => $lib])." <a href='$url'>$url</a>"; |
|
| 762 | 762 | } |
| 763 | 763 | |
| 764 | 764 | |
@@ -857,7 +857,7 @@ discard block |
||
| 857 | 857 | foreach ($plugin_valides as $p => $resume) { |
| 858 | 858 | // Les headers ne doivent pas indiquer les versions des extensions PHP, ni la version PHP |
| 859 | 859 | if (!str_starts_with($p, 'PHP:') && $p !== 'PHP') { |
| 860 | - $header[] = $p . ($resume['version'] ? '(' . $resume['version'] . ')' : ''); |
|
| 860 | + $header[] = $p.($resume['version'] ? '('.$resume['version'].')' : ''); |
|
| 861 | 861 | } |
| 862 | 862 | if ($resume['dir']) { |
| 863 | 863 | foreach ($infos[$resume['dir_type']][$resume['dir']]['lib'] as $l) { |
@@ -882,11 +882,11 @@ discard block |
||
| 882 | 882 | $header = strtolower(implode(',', $header)); |
| 883 | 883 | if (!isset($GLOBALS['spip_header_silencieux']) || !$GLOBALS['spip_header_silencieux']) { |
| 884 | 884 | ecrire_fichier( |
| 885 | - _DIR_VAR . 'config.txt', |
|
| 886 | - (defined('_HEADER_COMPOSED_BY') ? _HEADER_COMPOSED_BY : 'Composed-By: SPIP') . ' ' . $GLOBALS['spip_version_affichee'] . ' @ www.spip.net + ' . $header |
|
| 885 | + _DIR_VAR.'config.txt', |
|
| 886 | + (defined('_HEADER_COMPOSED_BY') ? _HEADER_COMPOSED_BY : 'Composed-By: SPIP').' '.$GLOBALS['spip_version_affichee'].' @ www.spip.net + '.$header |
|
| 887 | 887 | ); |
| 888 | 888 | } else { |
| 889 | - @unlink(_DIR_VAR . 'config.txt'); |
|
| 889 | + @unlink(_DIR_VAR.'config.txt'); |
|
| 890 | 890 | } |
| 891 | 891 | // generer charger_plugins_chemin.php |
| 892 | 892 | plugins_precompile_chemin($plugin_valides, $ordre); |
@@ -940,7 +940,7 @@ discard block |
||
| 940 | 940 | // definir le plugin, donc le path avant l'include du fichier options |
| 941 | 941 | // permet de faire des include_spip pour attraper un inc_ du plugin |
| 942 | 942 | |
| 943 | - $dir = $dir_type . ".'" . $plug . "/'"; |
|
| 943 | + $dir = $dir_type.".'".$plug."/'"; |
|
| 944 | 944 | |
| 945 | 945 | $prefix = strtoupper(preg_replace(',\W,', '_', $info['prefix'])); |
| 946 | 946 | if ( |
@@ -951,7 +951,7 @@ discard block |
||
| 951 | 951 | if (!$info['chemin']) { |
| 952 | 952 | $chemins['public'][] = "_DIR_PLUGIN_$prefix"; |
| 953 | 953 | $chemins['prive'][] = "_DIR_PLUGIN_$prefix"; |
| 954 | - if (is_dir(constant($dir_type) . $plug . '/squelettes/')) { |
|
| 954 | + if (is_dir(constant($dir_type).$plug.'/squelettes/')) { |
|
| 955 | 955 | $chemins['public'][] = "_DIR_PLUGIN_{$prefix}.'squelettes/'"; |
| 956 | 956 | } |
| 957 | 957 | } |
@@ -973,13 +973,13 @@ discard block |
||
| 973 | 973 | $dir = ''; |
| 974 | 974 | } |
| 975 | 975 | if (strlen($dir)) { |
| 976 | - $dir = rtrim($dir, '/') . '/'; |
|
| 976 | + $dir = rtrim($dir, '/').'/'; |
|
| 977 | 977 | } |
| 978 | 978 | if (!isset($chemin['type']) || $chemin['type'] == 'public') { |
| 979 | - $chemins['public'][] = "_DIR_PLUGIN_$prefix" . (strlen($dir) ? ".'$dir'" : ''); |
|
| 979 | + $chemins['public'][] = "_DIR_PLUGIN_$prefix".(strlen($dir) ? ".'$dir'" : ''); |
|
| 980 | 980 | } |
| 981 | 981 | if (!isset($chemin['type']) || $chemin['type'] == 'prive') { |
| 982 | - $chemins['prive'][] = "_DIR_PLUGIN_$prefix" . (strlen($dir) ? ".'$dir'" : ''); |
|
| 982 | + $chemins['prive'][] = "_DIR_PLUGIN_$prefix".(strlen($dir) ? ".'$dir'" : ''); |
|
| 983 | 983 | } |
| 984 | 984 | } |
| 985 | 985 | } |
@@ -988,11 +988,11 @@ discard block |
||
| 988 | 988 | } |
| 989 | 989 | } |
| 990 | 990 | if (count($chemins['public']) || count($chemins['prive'])) { |
| 991 | - $contenu .= 'if (_DIR_RESTREINT) _chemin([' . implode( |
|
| 991 | + $contenu .= 'if (_DIR_RESTREINT) _chemin(['.implode( |
|
| 992 | 992 | ',', |
| 993 | 993 | array_reverse($chemins['public']) |
| 994 | - ) . "]);\n" |
|
| 995 | - . 'else _chemin([' . implode(',', array_reverse($chemins['prive'])) . "]);\n"; |
|
| 994 | + )."]);\n" |
|
| 995 | + . 'else _chemin(['.implode(',', array_reverse($chemins['prive']))."]);\n"; |
|
| 996 | 996 | } |
| 997 | 997 | |
| 998 | 998 | ecrire_fichier_php(_CACHE_PLUGINS_PATH, $contenu); |
@@ -1041,7 +1041,7 @@ discard block |
||
| 1041 | 1041 | && !str_contains($dir, ':') |
| 1042 | 1042 | && file_exists("$dir$plug/paquet.xml") // uniquement pour les paquet.xml |
| 1043 | 1043 | ) { |
| 1044 | - if (is_readable("$dir$plug/" . ($file = $info['prefix'] . '_' . $charge . '.php'))) { |
|
| 1044 | + if (is_readable("$dir$plug/".($file = $info['prefix'].'_'.$charge.'.php'))) { |
|
| 1045 | 1045 | $info[$charge] = [$file]; |
| 1046 | 1046 | } |
| 1047 | 1047 | } |
@@ -1056,7 +1056,7 @@ discard block |
||
| 1056 | 1056 | ) { |
| 1057 | 1057 | unset($info[$charge][$k]); |
| 1058 | 1058 | } else { |
| 1059 | - $_file = $root_dir_type . ".'$plug/$file'"; |
|
| 1059 | + $_file = $root_dir_type.".'$plug/$file'"; |
|
| 1060 | 1060 | $contenu[$charge] .= "include_once_check($_file);\n"; |
| 1061 | 1061 | } |
| 1062 | 1062 | } |
@@ -1066,7 +1066,7 @@ discard block |
||
| 1066 | 1066 | } |
| 1067 | 1067 | } |
| 1068 | 1068 | |
| 1069 | - $contenu['options'] = "define('_PLUGINS_HASH','" . md5($sign) . "');\n" . $contenu['options']; |
|
| 1069 | + $contenu['options'] = "define('_PLUGINS_HASH','".md5($sign)."');\n".$contenu['options']; |
|
| 1070 | 1070 | $contenu['fonctions'] .= plugin_ongletbouton('boutons_plugins', $boutons) |
| 1071 | 1071 | . plugin_ongletbouton('onglets_plugins', $onglets); |
| 1072 | 1072 | |
@@ -1101,12 +1101,12 @@ discard block |
||
| 1101 | 1101 | define("_UPDATED_$nom", $val); |
| 1102 | 1102 | define("_UPDATED_md5_$nom", $md5); |
| 1103 | 1103 | } |
| 1104 | - $val = "unserialize('" . str_replace("'", "\'", $val) . "')"; |
|
| 1104 | + $val = "unserialize('".str_replace("'", "\'", $val)."')"; |
|
| 1105 | 1105 | |
| 1106 | 1106 | return |
| 1107 | 1107 | "if (!function_exists('$nom')) {\n" |
| 1108 | 1108 | . "function $nom(){return defined('_UPDATED_$nom')?unserialize(_UPDATED_$nom):$val;}\n" |
| 1109 | - . "function md5_$nom(){return defined('_UPDATED_md5_$nom')?_UPDATED_md5_$nom:'" . $md5 . "';}\n" |
|
| 1109 | + . "function md5_$nom(){return defined('_UPDATED_md5_$nom')?_UPDATED_md5_$nom:'".$md5."';}\n" |
|
| 1110 | 1110 | . "}\n"; |
| 1111 | 1111 | } |
| 1112 | 1112 | |
@@ -1129,7 +1129,7 @@ discard block |
||
| 1129 | 1129 | if (@is_readable(_CACHE_PLUGINS_OPT)) { |
| 1130 | 1130 | include_once(_CACHE_PLUGINS_OPT); |
| 1131 | 1131 | } else { |
| 1132 | - spip_logger()->info('pipelines desactives: impossible de produire ' . _CACHE_PLUGINS_OPT); |
|
| 1132 | + spip_logger()->info('pipelines desactives: impossible de produire '._CACHE_PLUGINS_OPT); |
|
| 1133 | 1133 | } |
| 1134 | 1134 | } |
| 1135 | 1135 | |
@@ -1166,7 +1166,7 @@ discard block |
||
| 1166 | 1166 | $dir_type = $plugin_valides[$p]['dir_type']; |
| 1167 | 1167 | $root_dir_type = str_replace('_DIR_', '_ROOT_', $dir_type); |
| 1168 | 1168 | $plug = $plugin_valides[$p]['dir']; |
| 1169 | - $prefix = (($info['prefix'] == 'spip') ? '' : $info['prefix'] . '_'); |
|
| 1169 | + $prefix = (($info['prefix'] == 'spip') ? '' : $info['prefix'].'_'); |
|
| 1170 | 1170 | if (isset($info['pipeline']) && is_array($info['pipeline'])) { |
| 1171 | 1171 | foreach ($info['pipeline'] as $pipe) { |
| 1172 | 1172 | $nom = $pipe['nom']; |
@@ -1200,7 +1200,7 @@ discard block |
||
| 1200 | 1200 | } |
| 1201 | 1201 | if (isset($pipe['inclure'])) { |
| 1202 | 1202 | $GLOBALS['spip_matrice']["$prefix$action"] = |
| 1203 | - "$root_dir_type:$plug/" . $pipe['inclure']; |
|
| 1203 | + "$root_dir_type:$plug/".$pipe['inclure']; |
|
| 1204 | 1204 | } |
| 1205 | 1205 | } |
| 1206 | 1206 | } |
@@ -1210,7 +1210,7 @@ discard block |
||
| 1210 | 1210 | $prepend_code['taches_generales_cron'] = ''; |
| 1211 | 1211 | } |
| 1212 | 1212 | foreach ($info['genie'] as $genie) { |
| 1213 | - $nom = $prefix . $genie['nom']; |
|
| 1213 | + $nom = $prefix.$genie['nom']; |
|
| 1214 | 1214 | $periode = max(60, intval($genie['periode'])); |
| 1215 | 1215 | if (charger_fonction($nom, 'genie', true)) { |
| 1216 | 1216 | $prepend_code['taches_generales_cron'] .= "\$val['$nom'] = $periode;\n"; |
@@ -1228,13 +1228,13 @@ discard block |
||
| 1228 | 1228 | } |
| 1229 | 1229 | foreach ($info['style'] as $style) { |
| 1230 | 1230 | if (isset($style['path']) && $style['path']) { |
| 1231 | - $code = "if (\$f=timestamp(direction_css(find_in_path('" . addslashes($style['path']) . "')))) "; |
|
| 1231 | + $code = "if (\$f=timestamp(direction_css(find_in_path('".addslashes($style['path'])."')))) "; |
|
| 1232 | 1232 | } else { |
| 1233 | - $code = "if (\$f='" . addslashes($style['url']) . "') "; |
|
| 1233 | + $code = "if (\$f='".addslashes($style['url'])."') "; |
|
| 1234 | 1234 | } |
| 1235 | 1235 | $code .= "\$val .= '<link rel=\"stylesheet\" href=\"'.\$f.'\" type=\"text/css\""; |
| 1236 | 1236 | if (isset($style['media']) && strlen($style['media'])) { |
| 1237 | - $code .= ' media="' . addslashes($style['media']) . '"'; |
|
| 1237 | + $code .= ' media="'.addslashes($style['media']).'"'; |
|
| 1238 | 1238 | } |
| 1239 | 1239 | $code .= "/>';\n"; |
| 1240 | 1240 | if ($style['type'] != 'prive') { |
@@ -1254,9 +1254,9 @@ discard block |
||
| 1254 | 1254 | if (isset($info['script']) && (is_countable($info['script']) ? count($info['script']) : 0)) { |
| 1255 | 1255 | foreach ($info['script'] as $script) { |
| 1256 | 1256 | if (isset($script['path']) && $script['path']) { |
| 1257 | - $code = "if (\$f=timestamp(find_in_path('" . addslashes($script['path']) . "'))) "; |
|
| 1257 | + $code = "if (\$f=timestamp(find_in_path('".addslashes($script['path'])."'))) "; |
|
| 1258 | 1258 | } else { |
| 1259 | - $code = "if (\$f='" . addslashes($script['url']) . "') "; |
|
| 1259 | + $code = "if (\$f='".addslashes($script['url'])."') "; |
|
| 1260 | 1260 | } |
| 1261 | 1261 | $code .= "\$val .= '<script src=\"'.\$f.'\" type=\"text/javascript\"></script>';\n"; |
| 1262 | 1262 | if ($script['type'] != 'prive') { |
@@ -1319,10 +1319,10 @@ discard block |
||
| 1319 | 1319 | unset($GLOBALS['spip_pipeline']['all']); |
| 1320 | 1320 | $all_pipes = trim(array_shift($a)); |
| 1321 | 1321 | if ($all_pipes) { |
| 1322 | - $all_pipes = '|' . ltrim($all_pipes, '|'); |
|
| 1322 | + $all_pipes = '|'.ltrim($all_pipes, '|'); |
|
| 1323 | 1323 | } |
| 1324 | 1324 | if (count($a)) { |
| 1325 | - $all_pipes_end = '||' . array_shift($a); |
|
| 1325 | + $all_pipes_end = '||'.array_shift($a); |
|
| 1326 | 1326 | } |
| 1327 | 1327 | } |
| 1328 | 1328 | $content = ''; |
@@ -1339,7 +1339,7 @@ discard block |
||
| 1339 | 1339 | // Eclater le pipeline en filtres et appliquer chaque filtre |
| 1340 | 1340 | foreach ($pipe as $fonc) { |
| 1341 | 1341 | $fonc = trim($fonc); |
| 1342 | - $s_call .= '$val = minipipe(\'' . $fonc . '\', $val);' . "\n"; |
|
| 1342 | + $s_call .= '$val = minipipe(\''.$fonc.'\', $val);'."\n"; |
|
| 1343 | 1343 | if (isset($GLOBALS['spip_matrice'][$fonc])) { |
| 1344 | 1344 | $file = $GLOBALS['spip_matrice'][$fonc]; |
| 1345 | 1345 | $file = "'$file'"; |
@@ -1350,7 +1350,7 @@ discard block |
||
| 1350 | 1350 | if (defined($root_dir)) { |
| 1351 | 1351 | $dir = $root_dir; |
| 1352 | 1352 | } |
| 1353 | - $file = str_replace($regs[0], "'." . $dir . ".'", $file); |
|
| 1353 | + $file = str_replace($regs[0], "'.".$dir.".'", $file); |
|
| 1354 | 1354 | $file = str_replace("''.", '', $file); |
| 1355 | 1355 | $file = str_replace(constant($dir), '', $file); |
| 1356 | 1356 | } |
@@ -1363,7 +1363,7 @@ discard block |
||
| 1363 | 1363 | $content .= "// Pipeline $action \n" |
| 1364 | 1364 | . "function execute_pipeline_$action(&\$val){\n" |
| 1365 | 1365 | . $s_inc |
| 1366 | - . ((isset($prepend_code[$action]) && strlen($prepend_code[$action])) ? trim($prepend_code[$action]) . "\n" : '') |
|
| 1366 | + . ((isset($prepend_code[$action]) && strlen($prepend_code[$action])) ? trim($prepend_code[$action])."\n" : '') |
|
| 1367 | 1367 | . $s_call |
| 1368 | 1368 | . "return \$val;\n}\n"; |
| 1369 | 1369 | } |
@@ -1419,9 +1419,9 @@ discard block |
||
| 1419 | 1419 | $result = ($ok ? ((isset($infos['upgrade']) && $infos['upgrade']) ? _T('plugin_info_upgrade_ok') : _T('plugin_info_install_ok')) : _T('avis_operation_echec')); |
| 1420 | 1420 | if (_IS_CLI) { |
| 1421 | 1421 | include_spip('inc/filtres'); |
| 1422 | - $trace = ltrim(textebrut($trace) . "\n" . $result); |
|
| 1423 | - $trace = ' ' . str_replace("\n", "\n ", $trace); |
|
| 1424 | - echo "\n" . ($ok ? 'OK ' : '/!\ ') . textebrut($titre) . "\n", |
|
| 1422 | + $trace = ltrim(textebrut($trace)."\n".$result); |
|
| 1423 | + $trace = ' '.str_replace("\n", "\n ", $trace); |
|
| 1424 | + echo "\n".($ok ? 'OK ' : '/!\ ').textebrut($titre)."\n", |
|
| 1425 | 1425 | $trace, |
| 1426 | 1426 | "\n"; |
| 1427 | 1427 | } |
@@ -1456,15 +1456,15 @@ discard block |
||
| 1456 | 1456 | $GLOBALS['fichier_php_compile_recent'] = 0; |
| 1457 | 1457 | } |
| 1458 | 1458 | |
| 1459 | - $contenu = '<' . '?php' . "\n" . $comment . "\nif (defined('_ECRIRE_INC_VERSION')) {\n" . $contenu . "}\n?" . '>'; |
|
| 1459 | + $contenu = '<'.'?php'."\n".$comment."\nif (defined('_ECRIRE_INC_VERSION')) {\n".$contenu."}\n?".'>'; |
|
| 1460 | 1460 | // si un fichier existe deja on verifie que son contenu change avant de l'ecraser |
| 1461 | 1461 | // si pas de modif on ne touche pas au fichier initial |
| 1462 | 1462 | if (file_exists($nom)) { |
| 1463 | 1463 | if (str_ends_with($nom, '.php')) { |
| 1464 | - $fichier_tmp = substr($nom, 0, -4) . '.tmp.php'; |
|
| 1464 | + $fichier_tmp = substr($nom, 0, -4).'.tmp.php'; |
|
| 1465 | 1465 | } |
| 1466 | 1466 | else { |
| 1467 | - $fichier_tmp = $nom . '.tmp'; |
|
| 1467 | + $fichier_tmp = $nom.'.tmp'; |
|
| 1468 | 1468 | } |
| 1469 | 1469 | file_put_contents($fichier_tmp, $contenu); |
| 1470 | 1470 | if (md5_file($nom) == md5_file($fichier_tmp)) { |
@@ -17,32 +17,32 @@ discard block |
||
| 17 | 17 | * @package SPIP\Core\Distant |
| 18 | 18 | **/ |
| 19 | 19 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 20 | - return; |
|
| 20 | + return; |
|
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | if (!defined('_INC_DISTANT_VERSION_HTTP')) { |
| 24 | - define('_INC_DISTANT_VERSION_HTTP', 'HTTP/1.0'); |
|
| 24 | + define('_INC_DISTANT_VERSION_HTTP', 'HTTP/1.0'); |
|
| 25 | 25 | } |
| 26 | 26 | if (!defined('_INC_DISTANT_CONTENT_ENCODING')) { |
| 27 | - define('_INC_DISTANT_CONTENT_ENCODING', 'gzip'); |
|
| 27 | + define('_INC_DISTANT_CONTENT_ENCODING', 'gzip'); |
|
| 28 | 28 | } |
| 29 | 29 | if (!defined('_INC_DISTANT_USER_AGENT')) { |
| 30 | - define('_INC_DISTANT_USER_AGENT', 'SPIP-' . $GLOBALS['spip_version_affichee'] . ' (' . $GLOBALS['home_server'] . ')'); |
|
| 30 | + define('_INC_DISTANT_USER_AGENT', 'SPIP-' . $GLOBALS['spip_version_affichee'] . ' (' . $GLOBALS['home_server'] . ')'); |
|
| 31 | 31 | } |
| 32 | 32 | if (!defined('_INC_DISTANT_MAX_SIZE')) { |
| 33 | - define('_INC_DISTANT_MAX_SIZE', 2_097_152); |
|
| 33 | + define('_INC_DISTANT_MAX_SIZE', 2_097_152); |
|
| 34 | 34 | } |
| 35 | 35 | if (!defined('_INC_DISTANT_CONNECT_TIMEOUT')) { |
| 36 | - define('_INC_DISTANT_CONNECT_TIMEOUT', 10); |
|
| 36 | + define('_INC_DISTANT_CONNECT_TIMEOUT', 10); |
|
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 39 | define('_REGEXP_COPIE_LOCALE', ',' . |
| 40 | - preg_replace( |
|
| 41 | - '@^https?:@', |
|
| 42 | - 'https?:', |
|
| 43 | - ($GLOBALS['meta']['adresse_site'] ?? '') |
|
| 44 | - ) |
|
| 45 | - . '/?spip.php[?]action=acceder_document.*file=(.*)$,'); |
|
| 40 | + preg_replace( |
|
| 41 | + '@^https?:@', |
|
| 42 | + 'https?:', |
|
| 43 | + ($GLOBALS['meta']['adresse_site'] ?? '') |
|
| 44 | + ) |
|
| 45 | + . '/?spip.php[?]action=acceder_document.*file=(.*)$,'); |
|
| 46 | 46 | |
| 47 | 47 | //@define('_COPIE_LOCALE_MAX_SIZE',2097152); // poids (inc/utils l'a fait) |
| 48 | 48 | |
@@ -71,106 +71,106 @@ discard block |
||
| 71 | 71 | */ |
| 72 | 72 | function copie_locale($source, $mode = 'auto', $local = null, $taille_max = null, $callback_valider_url = null) { |
| 73 | 73 | |
| 74 | - // si c'est la protection de soi-meme, retourner le path |
|
| 75 | - if ($mode !== 'force' && preg_match(_REGEXP_COPIE_LOCALE, $source, $match)) { |
|
| 76 | - $source = substr((string) _DIR_IMG, strlen((string) _DIR_RACINE)) . urldecode($match[1]); |
|
| 77 | - |
|
| 78 | - return @file_exists($source) ? $source : false; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - if (is_null($local)) { |
|
| 82 | - $local = fichier_copie_locale($source); |
|
| 83 | - } else { |
|
| 84 | - if (_DIR_RACINE && strncmp((string) _DIR_RACINE, $local, strlen((string) _DIR_RACINE)) == 0) { |
|
| 85 | - $local = substr($local, strlen((string) _DIR_RACINE)); |
|
| 86 | - } |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - // si $local = '' c'est un fichier refuse par fichier_copie_locale(), |
|
| 90 | - // par exemple un fichier qui ne figure pas dans nos documents ; |
|
| 91 | - // dans ce cas on n'essaie pas de le telecharger pour ensuite echouer |
|
| 92 | - if (!$local) { |
|
| 93 | - return false; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - $localrac = _DIR_RACINE . $local; |
|
| 97 | - $t = ($mode === 'force') ? false : @file_exists($localrac); |
|
| 98 | - |
|
| 99 | - // test d'existence du fichier |
|
| 100 | - if ($mode === 'test') { |
|
| 101 | - return $t ? $local : ''; |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - // sinon voir si on doit/peut le telecharger |
|
| 105 | - if ($local === $source || !tester_url_absolue($source)) { |
|
| 106 | - return $t ? $local : ''; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - if ($mode === 'modif' || !$t) { |
|
| 110 | - // passer par un fichier temporaire unique pour gerer les echecs en cours de recuperation |
|
| 111 | - // et des eventuelles recuperations concurantes |
|
| 112 | - include_spip('inc/acces'); |
|
| 113 | - if (!$taille_max) { |
|
| 114 | - $taille_max = _COPIE_LOCALE_MAX_SIZE; |
|
| 115 | - } |
|
| 116 | - $localrac_tmp = $localrac . '.tmp'; |
|
| 117 | - $res = recuperer_url( |
|
| 118 | - $source, |
|
| 119 | - ['file' => $localrac_tmp, 'taille_max' => $taille_max, 'if_modified_since' => $t ? filemtime($localrac) : ''] |
|
| 120 | - ); |
|
| 121 | - |
|
| 122 | - $logger = spip_logger('distant'); |
|
| 123 | - if (!$res || !$res['length'] && $res['status'] != 304) { |
|
| 124 | - $logger->notice("copie_locale : Echec recuperation $source sur $localrac_tmp status : " . ($res ? $res['status'] : '-')); |
|
| 125 | - @unlink($localrac_tmp); |
|
| 126 | - } else { |
|
| 127 | - $logger->info("copie_locale : recuperation $source sur $localrac_tmp OK | taille " . $res['length'] . ' status ' . $res['status']); |
|
| 128 | - } |
|
| 129 | - if (!$res || !$res['length']) { |
|
| 130 | - // si $t c'est sans doute juste un not-modified-since |
|
| 131 | - return $t ? $local : false; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - // si option valider url, verifions que l'URL finale est acceptable |
|
| 135 | - if ( |
|
| 136 | - $callback_valider_url |
|
| 137 | - && is_callable($callback_valider_url) |
|
| 138 | - && !$callback_valider_url($res['url']) |
|
| 139 | - ) { |
|
| 140 | - $logger->notice('copie_locale : url finale ' . $res['url'] . " non valide, on refuse le fichier $localrac_tmp"); |
|
| 141 | - @unlink($localrac_tmp); |
|
| 142 | - return $t ? $local : false; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - // on peut renommer le fichier tmp |
|
| 146 | - @rename($localrac_tmp, $localrac); |
|
| 147 | - |
|
| 148 | - // si on retrouve l'extension |
|
| 149 | - if ( |
|
| 150 | - !empty($res['headers']) |
|
| 151 | - && ($extension = distant_trouver_extension_selon_headers($source, $res['headers'])) |
|
| 152 | - && ($sanitizer = charger_fonction($extension, 'sanitizer', true)) |
|
| 153 | - ) { |
|
| 154 | - $sanitizer($localrac); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - // pour une eventuelle indexation |
|
| 158 | - pipeline( |
|
| 159 | - 'post_edition', |
|
| 160 | - [ |
|
| 161 | - 'args' => [ |
|
| 162 | - 'operation' => 'copie_locale', |
|
| 163 | - 'source' => $source, |
|
| 164 | - 'fichier' => $local, |
|
| 165 | - 'http_res' => $res['length'], |
|
| 166 | - 'url' => $res['url'], |
|
| 167 | - ], |
|
| 168 | - 'data' => null |
|
| 169 | - ] |
|
| 170 | - ); |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - return $local; |
|
| 74 | + // si c'est la protection de soi-meme, retourner le path |
|
| 75 | + if ($mode !== 'force' && preg_match(_REGEXP_COPIE_LOCALE, $source, $match)) { |
|
| 76 | + $source = substr((string) _DIR_IMG, strlen((string) _DIR_RACINE)) . urldecode($match[1]); |
|
| 77 | + |
|
| 78 | + return @file_exists($source) ? $source : false; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + if (is_null($local)) { |
|
| 82 | + $local = fichier_copie_locale($source); |
|
| 83 | + } else { |
|
| 84 | + if (_DIR_RACINE && strncmp((string) _DIR_RACINE, $local, strlen((string) _DIR_RACINE)) == 0) { |
|
| 85 | + $local = substr($local, strlen((string) _DIR_RACINE)); |
|
| 86 | + } |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + // si $local = '' c'est un fichier refuse par fichier_copie_locale(), |
|
| 90 | + // par exemple un fichier qui ne figure pas dans nos documents ; |
|
| 91 | + // dans ce cas on n'essaie pas de le telecharger pour ensuite echouer |
|
| 92 | + if (!$local) { |
|
| 93 | + return false; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + $localrac = _DIR_RACINE . $local; |
|
| 97 | + $t = ($mode === 'force') ? false : @file_exists($localrac); |
|
| 98 | + |
|
| 99 | + // test d'existence du fichier |
|
| 100 | + if ($mode === 'test') { |
|
| 101 | + return $t ? $local : ''; |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + // sinon voir si on doit/peut le telecharger |
|
| 105 | + if ($local === $source || !tester_url_absolue($source)) { |
|
| 106 | + return $t ? $local : ''; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + if ($mode === 'modif' || !$t) { |
|
| 110 | + // passer par un fichier temporaire unique pour gerer les echecs en cours de recuperation |
|
| 111 | + // et des eventuelles recuperations concurantes |
|
| 112 | + include_spip('inc/acces'); |
|
| 113 | + if (!$taille_max) { |
|
| 114 | + $taille_max = _COPIE_LOCALE_MAX_SIZE; |
|
| 115 | + } |
|
| 116 | + $localrac_tmp = $localrac . '.tmp'; |
|
| 117 | + $res = recuperer_url( |
|
| 118 | + $source, |
|
| 119 | + ['file' => $localrac_tmp, 'taille_max' => $taille_max, 'if_modified_since' => $t ? filemtime($localrac) : ''] |
|
| 120 | + ); |
|
| 121 | + |
|
| 122 | + $logger = spip_logger('distant'); |
|
| 123 | + if (!$res || !$res['length'] && $res['status'] != 304) { |
|
| 124 | + $logger->notice("copie_locale : Echec recuperation $source sur $localrac_tmp status : " . ($res ? $res['status'] : '-')); |
|
| 125 | + @unlink($localrac_tmp); |
|
| 126 | + } else { |
|
| 127 | + $logger->info("copie_locale : recuperation $source sur $localrac_tmp OK | taille " . $res['length'] . ' status ' . $res['status']); |
|
| 128 | + } |
|
| 129 | + if (!$res || !$res['length']) { |
|
| 130 | + // si $t c'est sans doute juste un not-modified-since |
|
| 131 | + return $t ? $local : false; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + // si option valider url, verifions que l'URL finale est acceptable |
|
| 135 | + if ( |
|
| 136 | + $callback_valider_url |
|
| 137 | + && is_callable($callback_valider_url) |
|
| 138 | + && !$callback_valider_url($res['url']) |
|
| 139 | + ) { |
|
| 140 | + $logger->notice('copie_locale : url finale ' . $res['url'] . " non valide, on refuse le fichier $localrac_tmp"); |
|
| 141 | + @unlink($localrac_tmp); |
|
| 142 | + return $t ? $local : false; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + // on peut renommer le fichier tmp |
|
| 146 | + @rename($localrac_tmp, $localrac); |
|
| 147 | + |
|
| 148 | + // si on retrouve l'extension |
|
| 149 | + if ( |
|
| 150 | + !empty($res['headers']) |
|
| 151 | + && ($extension = distant_trouver_extension_selon_headers($source, $res['headers'])) |
|
| 152 | + && ($sanitizer = charger_fonction($extension, 'sanitizer', true)) |
|
| 153 | + ) { |
|
| 154 | + $sanitizer($localrac); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + // pour une eventuelle indexation |
|
| 158 | + pipeline( |
|
| 159 | + 'post_edition', |
|
| 160 | + [ |
|
| 161 | + 'args' => [ |
|
| 162 | + 'operation' => 'copie_locale', |
|
| 163 | + 'source' => $source, |
|
| 164 | + 'fichier' => $local, |
|
| 165 | + 'http_res' => $res['length'], |
|
| 166 | + 'url' => $res['url'], |
|
| 167 | + ], |
|
| 168 | + 'data' => null |
|
| 169 | + ] |
|
| 170 | + ); |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + return $local; |
|
| 174 | 174 | } |
| 175 | 175 | |
| 176 | 176 | /** |
@@ -185,97 +185,97 @@ discard block |
||
| 185 | 185 | * url ou false en cas d'echec |
| 186 | 186 | */ |
| 187 | 187 | function valider_url_distante($url, $known_hosts = []) { |
| 188 | - if (!function_exists('protocole_verifier')) { |
|
| 189 | - include_spip('inc/filtres_mini'); |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - if (!protocole_verifier($url, ['http', 'https'])) { |
|
| 193 | - return false; |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - $parsed_url = parse_url($url); |
|
| 197 | - if (!$parsed_url || empty($parsed_url['host'])) { |
|
| 198 | - return false; |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - if (isset($parsed_url['user']) || isset($parsed_url['pass'])) { |
|
| 202 | - return false; |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - if (false !== strpbrk($parsed_url['host'], ':#?[]')) { |
|
| 206 | - return false; |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - if (!is_array($known_hosts)) { |
|
| 210 | - $known_hosts = [$known_hosts]; |
|
| 211 | - } |
|
| 212 | - $known_hosts[] = $GLOBALS['meta']['adresse_site']; |
|
| 213 | - $known_hosts[] = url_de_base(); |
|
| 214 | - $known_hosts = pipeline('declarer_hosts_distants', $known_hosts); |
|
| 215 | - |
|
| 216 | - $is_known_host = false; |
|
| 217 | - foreach ($known_hosts as $known_host) { |
|
| 218 | - $parse_known = parse_url((string) $known_host); |
|
| 219 | - if ( |
|
| 220 | - $parse_known |
|
| 221 | - && strtolower($parse_known['host']) === strtolower($parsed_url['host']) |
|
| 222 | - ) { |
|
| 223 | - $is_known_host = true; |
|
| 224 | - break; |
|
| 225 | - } |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - if (!$is_known_host) { |
|
| 229 | - $host = trim($parsed_url['host'], '.'); |
|
| 230 | - if (! $ip = filter_var($host, FILTER_VALIDATE_IP)) { |
|
| 231 | - $ip = gethostbyname($host); |
|
| 232 | - if ($ip === $host) { |
|
| 233 | - // Error condition for gethostbyname() |
|
| 234 | - $ip = false; |
|
| 235 | - } |
|
| 236 | - if ($records = dns_get_record($host)) { |
|
| 237 | - foreach ($records as $record) { |
|
| 238 | - // il faut que le TTL soit suffisant afin d'etre certain que le copie_locale eventuel qui suit |
|
| 239 | - // se fasse sur la meme IP |
|
| 240 | - if ($record['ttl'] < 10) { |
|
| 241 | - $ip = false; |
|
| 242 | - break; |
|
| 243 | - } |
|
| 244 | - } |
|
| 245 | - } |
|
| 246 | - else { |
|
| 247 | - $ip = false; |
|
| 248 | - } |
|
| 249 | - } |
|
| 250 | - if ($ip && ! filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) { |
|
| 251 | - return false; |
|
| 252 | - } |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - if (empty($parsed_url['port'])) { |
|
| 256 | - return $url; |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - $port = $parsed_url['port']; |
|
| 260 | - if ($port === 80 || $port === 443 || $port === 8080) { |
|
| 261 | - return $url; |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - if ($is_known_host) { |
|
| 265 | - foreach ($known_hosts as $known_host) { |
|
| 266 | - $parse_known = parse_url((string) $known_host); |
|
| 267 | - if ( |
|
| 268 | - $parse_known |
|
| 269 | - && !empty($parse_known['port']) |
|
| 270 | - && strtolower($parse_known['host']) === strtolower($parsed_url['host']) |
|
| 271 | - && $parse_known['port'] == $port |
|
| 272 | - ) { |
|
| 273 | - return $url; |
|
| 274 | - } |
|
| 275 | - } |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - return false; |
|
| 188 | + if (!function_exists('protocole_verifier')) { |
|
| 189 | + include_spip('inc/filtres_mini'); |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + if (!protocole_verifier($url, ['http', 'https'])) { |
|
| 193 | + return false; |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + $parsed_url = parse_url($url); |
|
| 197 | + if (!$parsed_url || empty($parsed_url['host'])) { |
|
| 198 | + return false; |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + if (isset($parsed_url['user']) || isset($parsed_url['pass'])) { |
|
| 202 | + return false; |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + if (false !== strpbrk($parsed_url['host'], ':#?[]')) { |
|
| 206 | + return false; |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + if (!is_array($known_hosts)) { |
|
| 210 | + $known_hosts = [$known_hosts]; |
|
| 211 | + } |
|
| 212 | + $known_hosts[] = $GLOBALS['meta']['adresse_site']; |
|
| 213 | + $known_hosts[] = url_de_base(); |
|
| 214 | + $known_hosts = pipeline('declarer_hosts_distants', $known_hosts); |
|
| 215 | + |
|
| 216 | + $is_known_host = false; |
|
| 217 | + foreach ($known_hosts as $known_host) { |
|
| 218 | + $parse_known = parse_url((string) $known_host); |
|
| 219 | + if ( |
|
| 220 | + $parse_known |
|
| 221 | + && strtolower($parse_known['host']) === strtolower($parsed_url['host']) |
|
| 222 | + ) { |
|
| 223 | + $is_known_host = true; |
|
| 224 | + break; |
|
| 225 | + } |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + if (!$is_known_host) { |
|
| 229 | + $host = trim($parsed_url['host'], '.'); |
|
| 230 | + if (! $ip = filter_var($host, FILTER_VALIDATE_IP)) { |
|
| 231 | + $ip = gethostbyname($host); |
|
| 232 | + if ($ip === $host) { |
|
| 233 | + // Error condition for gethostbyname() |
|
| 234 | + $ip = false; |
|
| 235 | + } |
|
| 236 | + if ($records = dns_get_record($host)) { |
|
| 237 | + foreach ($records as $record) { |
|
| 238 | + // il faut que le TTL soit suffisant afin d'etre certain que le copie_locale eventuel qui suit |
|
| 239 | + // se fasse sur la meme IP |
|
| 240 | + if ($record['ttl'] < 10) { |
|
| 241 | + $ip = false; |
|
| 242 | + break; |
|
| 243 | + } |
|
| 244 | + } |
|
| 245 | + } |
|
| 246 | + else { |
|
| 247 | + $ip = false; |
|
| 248 | + } |
|
| 249 | + } |
|
| 250 | + if ($ip && ! filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) { |
|
| 251 | + return false; |
|
| 252 | + } |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + if (empty($parsed_url['port'])) { |
|
| 256 | + return $url; |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + $port = $parsed_url['port']; |
|
| 260 | + if ($port === 80 || $port === 443 || $port === 8080) { |
|
| 261 | + return $url; |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + if ($is_known_host) { |
|
| 265 | + foreach ($known_hosts as $known_host) { |
|
| 266 | + $parse_known = parse_url((string) $known_host); |
|
| 267 | + if ( |
|
| 268 | + $parse_known |
|
| 269 | + && !empty($parse_known['port']) |
|
| 270 | + && strtolower($parse_known['host']) === strtolower($parsed_url['host']) |
|
| 271 | + && $parse_known['port'] == $port |
|
| 272 | + ) { |
|
| 273 | + return $url; |
|
| 274 | + } |
|
| 275 | + } |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + return false; |
|
| 279 | 279 | } |
| 280 | 280 | |
| 281 | 281 | /** |
@@ -295,87 +295,87 @@ discard block |
||
| 295 | 295 | */ |
| 296 | 296 | function prepare_donnees_post($donnees, $boundary = '') { |
| 297 | 297 | |
| 298 | - // permettre a la fonction qui a demande le post de formater elle meme ses donnees |
|
| 299 | - // pour un appel soap par exemple |
|
| 300 | - // l'entete est separe des donnees par un double retour a la ligne |
|
| 301 | - // on s'occupe ici de passer tous les retours lignes (\r\n, \r ou \n) en \r\n |
|
| 302 | - $chaine = ''; |
|
| 303 | - if (is_string($donnees) && strlen($donnees)) { |
|
| 304 | - $entete = ''; |
|
| 305 | - // on repasse tous les \r\n et \r en simples \n |
|
| 306 | - $donnees = str_replace("\r\n", "\n", $donnees); |
|
| 307 | - $donnees = str_replace("\r", "\n", $donnees); |
|
| 308 | - // un double retour a la ligne signifie la fin de l'entete et le debut des donnees |
|
| 309 | - $p = strpos($donnees, "\n\n"); |
|
| 310 | - if ($p !== false) { |
|
| 311 | - $entete = str_replace("\n", "\r\n", substr($donnees, 0, $p + 1)); |
|
| 312 | - $donnees = substr($donnees, $p + 2); |
|
| 313 | - } |
|
| 314 | - $chaine = str_replace("\n", "\r\n", $donnees); |
|
| 315 | - } else { |
|
| 316 | - /* boundary automatique */ |
|
| 317 | - // Si on a plus de 500 octects de donnees, on "boundarise" |
|
| 318 | - if ($boundary === '') { |
|
| 319 | - $taille = 0; |
|
| 320 | - foreach ($donnees as $cle => $valeur) { |
|
| 321 | - if (is_array($valeur)) { |
|
| 322 | - foreach ($valeur as $val2) { |
|
| 323 | - $taille += strlen((string) $val2); |
|
| 324 | - } |
|
| 325 | - } else { |
|
| 326 | - // faut-il utiliser spip_strlen() dans inc/charsets ? |
|
| 327 | - $taille += strlen((string) $valeur); |
|
| 328 | - } |
|
| 329 | - } |
|
| 330 | - if ($taille > 500) { |
|
| 331 | - $boundary = substr(md5(random_int(0, mt_getrandmax()) . 'spip'), 0, 8); |
|
| 332 | - } |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - if (is_string($boundary) && strlen($boundary)) { |
|
| 336 | - // fabrique une chaine HTTP pour un POST avec boundary |
|
| 337 | - $entete = "Content-Type: multipart/form-data; boundary=$boundary\r\n"; |
|
| 338 | - if (is_array($donnees)) { |
|
| 339 | - foreach ($donnees as $cle => $valeur) { |
|
| 340 | - if (is_array($valeur)) { |
|
| 341 | - foreach ($valeur as $val2) { |
|
| 342 | - $chaine .= "\r\n--$boundary\r\n"; |
|
| 343 | - $chaine .= "Content-Disposition: form-data; name=\"{$cle}[]\"\r\n"; |
|
| 344 | - $chaine .= "\r\n"; |
|
| 345 | - $chaine .= $val2; |
|
| 346 | - } |
|
| 347 | - } else { |
|
| 348 | - $chaine .= "\r\n--$boundary\r\n"; |
|
| 349 | - $chaine .= "Content-Disposition: form-data; name=\"$cle\"\r\n"; |
|
| 350 | - $chaine .= "\r\n"; |
|
| 351 | - $chaine .= $valeur; |
|
| 352 | - } |
|
| 353 | - } |
|
| 354 | - $chaine .= "\r\n--$boundary\r\n"; |
|
| 355 | - } |
|
| 356 | - } else { |
|
| 357 | - // fabrique une chaine HTTP simple pour un POST |
|
| 358 | - $entete = "Content-Type: application/x-www-form-urlencoded\r\n"; |
|
| 359 | - if (is_array($donnees)) { |
|
| 360 | - $chaines = []; |
|
| 361 | - foreach ($donnees as $cle => $valeur) { |
|
| 362 | - if (is_array($valeur)) { |
|
| 363 | - foreach ($valeur as $val2) { |
|
| 364 | - $chaines[] = rawurlencode($cle) . '[]=' . rawurlencode((string) $val2); |
|
| 365 | - } |
|
| 366 | - } else { |
|
| 367 | - $chaines[] = rawurlencode($cle) . '=' . rawurlencode((string) $valeur); |
|
| 368 | - } |
|
| 369 | - } |
|
| 370 | - $chaine = implode('&', $chaines); |
|
| 371 | - unset($chaines); |
|
| 372 | - } else { |
|
| 373 | - $chaine = $donnees; |
|
| 374 | - } |
|
| 375 | - } |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - return [$entete, $chaine]; |
|
| 298 | + // permettre a la fonction qui a demande le post de formater elle meme ses donnees |
|
| 299 | + // pour un appel soap par exemple |
|
| 300 | + // l'entete est separe des donnees par un double retour a la ligne |
|
| 301 | + // on s'occupe ici de passer tous les retours lignes (\r\n, \r ou \n) en \r\n |
|
| 302 | + $chaine = ''; |
|
| 303 | + if (is_string($donnees) && strlen($donnees)) { |
|
| 304 | + $entete = ''; |
|
| 305 | + // on repasse tous les \r\n et \r en simples \n |
|
| 306 | + $donnees = str_replace("\r\n", "\n", $donnees); |
|
| 307 | + $donnees = str_replace("\r", "\n", $donnees); |
|
| 308 | + // un double retour a la ligne signifie la fin de l'entete et le debut des donnees |
|
| 309 | + $p = strpos($donnees, "\n\n"); |
|
| 310 | + if ($p !== false) { |
|
| 311 | + $entete = str_replace("\n", "\r\n", substr($donnees, 0, $p + 1)); |
|
| 312 | + $donnees = substr($donnees, $p + 2); |
|
| 313 | + } |
|
| 314 | + $chaine = str_replace("\n", "\r\n", $donnees); |
|
| 315 | + } else { |
|
| 316 | + /* boundary automatique */ |
|
| 317 | + // Si on a plus de 500 octects de donnees, on "boundarise" |
|
| 318 | + if ($boundary === '') { |
|
| 319 | + $taille = 0; |
|
| 320 | + foreach ($donnees as $cle => $valeur) { |
|
| 321 | + if (is_array($valeur)) { |
|
| 322 | + foreach ($valeur as $val2) { |
|
| 323 | + $taille += strlen((string) $val2); |
|
| 324 | + } |
|
| 325 | + } else { |
|
| 326 | + // faut-il utiliser spip_strlen() dans inc/charsets ? |
|
| 327 | + $taille += strlen((string) $valeur); |
|
| 328 | + } |
|
| 329 | + } |
|
| 330 | + if ($taille > 500) { |
|
| 331 | + $boundary = substr(md5(random_int(0, mt_getrandmax()) . 'spip'), 0, 8); |
|
| 332 | + } |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + if (is_string($boundary) && strlen($boundary)) { |
|
| 336 | + // fabrique une chaine HTTP pour un POST avec boundary |
|
| 337 | + $entete = "Content-Type: multipart/form-data; boundary=$boundary\r\n"; |
|
| 338 | + if (is_array($donnees)) { |
|
| 339 | + foreach ($donnees as $cle => $valeur) { |
|
| 340 | + if (is_array($valeur)) { |
|
| 341 | + foreach ($valeur as $val2) { |
|
| 342 | + $chaine .= "\r\n--$boundary\r\n"; |
|
| 343 | + $chaine .= "Content-Disposition: form-data; name=\"{$cle}[]\"\r\n"; |
|
| 344 | + $chaine .= "\r\n"; |
|
| 345 | + $chaine .= $val2; |
|
| 346 | + } |
|
| 347 | + } else { |
|
| 348 | + $chaine .= "\r\n--$boundary\r\n"; |
|
| 349 | + $chaine .= "Content-Disposition: form-data; name=\"$cle\"\r\n"; |
|
| 350 | + $chaine .= "\r\n"; |
|
| 351 | + $chaine .= $valeur; |
|
| 352 | + } |
|
| 353 | + } |
|
| 354 | + $chaine .= "\r\n--$boundary\r\n"; |
|
| 355 | + } |
|
| 356 | + } else { |
|
| 357 | + // fabrique une chaine HTTP simple pour un POST |
|
| 358 | + $entete = "Content-Type: application/x-www-form-urlencoded\r\n"; |
|
| 359 | + if (is_array($donnees)) { |
|
| 360 | + $chaines = []; |
|
| 361 | + foreach ($donnees as $cle => $valeur) { |
|
| 362 | + if (is_array($valeur)) { |
|
| 363 | + foreach ($valeur as $val2) { |
|
| 364 | + $chaines[] = rawurlencode($cle) . '[]=' . rawurlencode((string) $val2); |
|
| 365 | + } |
|
| 366 | + } else { |
|
| 367 | + $chaines[] = rawurlencode($cle) . '=' . rawurlencode((string) $valeur); |
|
| 368 | + } |
|
| 369 | + } |
|
| 370 | + $chaine = implode('&', $chaines); |
|
| 371 | + unset($chaines); |
|
| 372 | + } else { |
|
| 373 | + $chaine = $donnees; |
|
| 374 | + } |
|
| 375 | + } |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + return [$entete, $chaine]; |
|
| 379 | 379 | } |
| 380 | 380 | |
| 381 | 381 | /** |
@@ -386,19 +386,19 @@ discard block |
||
| 386 | 386 | */ |
| 387 | 387 | function url_to_ascii($url_idn) { |
| 388 | 388 | |
| 389 | - if ($parts = parse_url($url_idn)) { |
|
| 390 | - $host = $parts['host']; |
|
| 391 | - if (!preg_match(',^[a-z0-9_\.\-]+$,i', $host)) { |
|
| 392 | - $converter = new ToIdn(); |
|
| 393 | - $host_ascii = $converter->convert($host); |
|
| 394 | - $url_idn = explode($host, $url_idn, 2); |
|
| 395 | - $url_idn = implode($host_ascii, $url_idn); |
|
| 396 | - } |
|
| 397 | - // et on urlencode les char utf si besoin dans le path |
|
| 398 | - $url_idn = preg_replace_callback('/[^\x20-\x7f]/', fn($match) => urlencode((string) $match[0]), $url_idn); |
|
| 399 | - } |
|
| 400 | - |
|
| 401 | - return $url_idn; |
|
| 389 | + if ($parts = parse_url($url_idn)) { |
|
| 390 | + $host = $parts['host']; |
|
| 391 | + if (!preg_match(',^[a-z0-9_\.\-]+$,i', $host)) { |
|
| 392 | + $converter = new ToIdn(); |
|
| 393 | + $host_ascii = $converter->convert($host); |
|
| 394 | + $url_idn = explode($host, $url_idn, 2); |
|
| 395 | + $url_idn = implode($host_ascii, $url_idn); |
|
| 396 | + } |
|
| 397 | + // et on urlencode les char utf si besoin dans le path |
|
| 398 | + $url_idn = preg_replace_callback('/[^\x20-\x7f]/', fn($match) => urlencode((string) $match[0]), $url_idn); |
|
| 399 | + } |
|
| 400 | + |
|
| 401 | + return $url_idn; |
|
| 402 | 402 | } |
| 403 | 403 | |
| 404 | 404 | /** |
@@ -440,229 +440,229 @@ discard block |
||
| 440 | 440 | * string file : nom du fichier si enregistre dans un fichier |
| 441 | 441 | */ |
| 442 | 442 | function recuperer_url($url, $options = []) { |
| 443 | - // Conserve la mémoire de la méthode fournit éventuellement |
|
| 444 | - $methode_demandee = $options['methode'] ?? ''; |
|
| 445 | - $default = [ |
|
| 446 | - 'transcoder' => false, |
|
| 447 | - 'methode' => 'GET', |
|
| 448 | - 'taille_max' => null, |
|
| 449 | - 'headers' => [], |
|
| 450 | - 'datas' => '', |
|
| 451 | - 'boundary' => '', |
|
| 452 | - 'refuser_gz' => false, |
|
| 453 | - 'if_modified_since' => '', |
|
| 454 | - 'uri_referer' => '', |
|
| 455 | - 'file' => '', |
|
| 456 | - 'follow_location' => 10, |
|
| 457 | - 'version_http' => _INC_DISTANT_VERSION_HTTP, |
|
| 458 | - ]; |
|
| 459 | - $options = array_merge($default, $options); |
|
| 460 | - // copier directement dans un fichier ? |
|
| 461 | - $copy = $options['file']; |
|
| 462 | - |
|
| 463 | - if ($options['methode'] == 'HEAD') { |
|
| 464 | - $options['taille_max'] = 0; |
|
| 465 | - } |
|
| 466 | - if (is_null($options['taille_max'])) { |
|
| 467 | - $options['taille_max'] = $copy ? _COPIE_LOCALE_MAX_SIZE : _INC_DISTANT_MAX_SIZE; |
|
| 468 | - } |
|
| 469 | - |
|
| 470 | - $logger = spip_logger('distant'); |
|
| 471 | - $logger->debug('recuperer_url ' . $options['methode'] . " sur $url"); |
|
| 472 | - |
|
| 473 | - // Ajout des en-têtes spécifiques si besoin |
|
| 474 | - $formatted_data = ''; |
|
| 475 | - if (!empty($options['headers'])) { |
|
| 476 | - foreach ($options['headers'] as $champ => $valeur) { |
|
| 477 | - $formatted_data .= $champ . ': ' . $valeur . "\r\n"; |
|
| 478 | - } |
|
| 479 | - } |
|
| 480 | - |
|
| 481 | - if (!empty($options['datas'])) { |
|
| 482 | - [$head, $postdata] = prepare_donnees_post($options['datas'], $options['boundary']); |
|
| 483 | - $head .= $formatted_data; |
|
| 484 | - if (stripos($head, 'Content-Length:') === false) { |
|
| 485 | - $head .= 'Content-Length: ' . strlen((string) $postdata) . "\r\n"; |
|
| 486 | - } |
|
| 487 | - $formatted_data = $head . "\r\n" . $postdata; |
|
| 488 | - if ( |
|
| 489 | - strlen((string) $postdata) && !$methode_demandee |
|
| 490 | - ) { |
|
| 491 | - $options['methode'] = 'POST'; |
|
| 492 | - } |
|
| 493 | - } elseif ($formatted_data) { |
|
| 494 | - $formatted_data .= "\r\n"; |
|
| 495 | - } |
|
| 496 | - |
|
| 497 | - // Accepter les URLs au format feed:// ou qui ont oublie le http:// ou les urls relatives au protocole |
|
| 498 | - $url = preg_replace(',^feed://,i', 'http://', $url); |
|
| 499 | - if (!tester_url_absolue($url)) { |
|
| 500 | - $url = 'http://' . $url; |
|
| 501 | - } elseif (str_starts_with($url, '//')) { |
|
| 502 | - $url = 'http:' . $url; |
|
| 503 | - } |
|
| 504 | - |
|
| 505 | - $url = url_to_ascii($url); |
|
| 506 | - |
|
| 507 | - $result = [ |
|
| 508 | - 'status' => 0, |
|
| 509 | - 'headers' => '', |
|
| 510 | - 'page' => '', |
|
| 511 | - 'length' => 0, |
|
| 512 | - 'last_modified' => '', |
|
| 513 | - 'location' => '', |
|
| 514 | - 'url' => $url |
|
| 515 | - ]; |
|
| 516 | - |
|
| 517 | - // si on ecrit directement dans un fichier, pour ne pas manipuler en memoire refuser gz |
|
| 518 | - $refuser_gz = ($options['refuser_gz'] || $copy); |
|
| 519 | - |
|
| 520 | - // ouvrir la connexion et envoyer la requete et ses en-tetes |
|
| 521 | - [$handle, $fopen] = init_http( |
|
| 522 | - $options['methode'], |
|
| 523 | - $url, |
|
| 524 | - $refuser_gz, |
|
| 525 | - $options['uri_referer'], |
|
| 526 | - $formatted_data, |
|
| 527 | - $options['version_http'], |
|
| 528 | - $options['if_modified_since'] |
|
| 529 | - ); |
|
| 530 | - if (!$handle) { |
|
| 531 | - $logger->error("ECHEC init_http $url"); |
|
| 532 | - |
|
| 533 | - return false; |
|
| 534 | - } |
|
| 535 | - |
|
| 536 | - // Sauf en fopen, envoyer le flux d'entree |
|
| 537 | - // et recuperer les en-tetes de reponses |
|
| 538 | - if (!$fopen) { |
|
| 539 | - $res = recuperer_entetes_complets($handle, $options['if_modified_since']); |
|
| 540 | - if (!$res) { |
|
| 541 | - fclose($handle); |
|
| 542 | - $t = @parse_url($url); |
|
| 543 | - $host = $t['host']; |
|
| 544 | - // Chinoisierie inexplicable pour contrer |
|
| 545 | - // les actions liberticides de l'empire du milieu |
|
| 546 | - if ( |
|
| 547 | - !need_proxy($host) |
|
| 548 | - && ($res = @file_get_contents($url)) |
|
| 549 | - ) { |
|
| 550 | - $result['length'] = strlen($res); |
|
| 551 | - if ($copy) { |
|
| 552 | - ecrire_fichier($copy, $res); |
|
| 553 | - $result['file'] = $copy; |
|
| 554 | - } else { |
|
| 555 | - $result['page'] = $res; |
|
| 556 | - } |
|
| 557 | - $res = [ |
|
| 558 | - 'status' => 200, |
|
| 559 | - ]; |
|
| 560 | - } else { |
|
| 561 | - $logger->error("ECHEC chinoiserie $url"); |
|
| 562 | - return false; |
|
| 563 | - } |
|
| 564 | - } elseif ($res['location'] && $options['follow_location']) { |
|
| 565 | - $options['follow_location']--; |
|
| 566 | - fclose($handle); |
|
| 567 | - include_spip('inc/filtres'); |
|
| 568 | - $url = suivre_lien($url, $res['location']); |
|
| 569 | - |
|
| 570 | - // une redirection doit se faire en GET, sauf status explicite 307 ou 308 qui indique de garder la meme methode |
|
| 571 | - if ( |
|
| 572 | - $options['methode'] !== 'GET' |
|
| 573 | - && (empty($res['status']) || !in_array($res['status'], [307, 308])) |
|
| 574 | - ) { |
|
| 575 | - $options['methode'] = 'GET'; |
|
| 576 | - $options['datas'] = ''; |
|
| 577 | - } |
|
| 578 | - $logger->debug('recuperer_url recommence ' . $options['methode'] . " sur $url"); |
|
| 579 | - |
|
| 580 | - return recuperer_url($url, $options); |
|
| 581 | - } elseif ($res['status'] !== 200) { |
|
| 582 | - $logger->info('HTTP status ' . $res['status'] . " pour $url"); |
|
| 583 | - } |
|
| 584 | - $result['status'] = $res['status']; |
|
| 585 | - if (isset($res['headers'])) { |
|
| 586 | - $result['headers'] = $res['headers']; |
|
| 587 | - } |
|
| 588 | - if (isset($res['last_modified'])) { |
|
| 589 | - $result['last_modified'] = $res['last_modified']; |
|
| 590 | - } |
|
| 591 | - if (isset($res['location'])) { |
|
| 592 | - $result['location'] = $res['location']; |
|
| 593 | - } |
|
| 594 | - if (isset($res['content_length'])) { |
|
| 595 | - $result['content_length'] = $res['content_length']; |
|
| 596 | - } |
|
| 597 | - } |
|
| 598 | - |
|
| 599 | - // on ne veut que les entetes |
|
| 600 | - if (!$options['taille_max'] || $options['methode'] == 'HEAD' || $result['status'] == '304') { |
|
| 601 | - $logger->debug('RESULTAT recuperer_url ' . $options['methode'] . " sur $url : " . json_encode($result, JSON_THROW_ON_ERROR)); |
|
| 602 | - return $result; |
|
| 603 | - } |
|
| 604 | - |
|
| 605 | - |
|
| 606 | - // s'il faut deballer, le faire via un fichier temporaire |
|
| 607 | - // sinon la memoire explose pour les gros flux |
|
| 608 | - |
|
| 609 | - $gz = false; |
|
| 610 | - if (preg_match(",\bContent-Encoding: .*gzip,is", (string) $result['headers'])) { |
|
| 611 | - $gz = (_DIR_TMP . md5(uniqid(random_int(0, mt_getrandmax()))) . '.tmp.gz'); |
|
| 612 | - } |
|
| 613 | - |
|
| 614 | - // si on n'a pas deja récupéré le contenu par une methode detournée |
|
| 615 | - if (!$result['length']) { |
|
| 616 | - $taille_max = $options['taille_max']; |
|
| 617 | - if (isset($result['content_length']) |
|
| 618 | - && !empty($result['content_length']) |
|
| 619 | - && ($result['content_length'] < $taille_max)) { |
|
| 620 | - $taille_max = $result['content_length']; |
|
| 621 | - } |
|
| 622 | - $res = recuperer_body($handle, $taille_max, $gz ?: $copy); |
|
| 623 | - fclose($handle); |
|
| 624 | - if ($copy) { |
|
| 625 | - $result['length'] = $res; |
|
| 626 | - $result['file'] = $copy; |
|
| 627 | - } elseif ($res) { |
|
| 628 | - $result['page'] = &$res; |
|
| 629 | - $result['length'] = strlen($result['page']); |
|
| 630 | - } |
|
| 631 | - if (!$result['status']) { |
|
| 632 | - $result['status'] = 200; // on a reussi, donc ! |
|
| 633 | - } |
|
| 634 | - } |
|
| 635 | - if (!$result['page']) { |
|
| 636 | - return $result; |
|
| 637 | - } |
|
| 638 | - |
|
| 639 | - // Decompresser au besoin |
|
| 640 | - if ($gz) { |
|
| 641 | - $result['page'] = implode('', gzfile($gz)); |
|
| 642 | - $result['length'] = strlen($result['page']); |
|
| 643 | - // et annuler le content-length qui correspond à la version gzip |
|
| 644 | - if (isset($result['content_length'])) { |
|
| 645 | - unset($result['content_length']); |
|
| 646 | - } |
|
| 647 | - supprimer_fichier($gz); |
|
| 648 | - } |
|
| 649 | - |
|
| 650 | - // Faut-il l'importer dans notre charset local ? |
|
| 651 | - if ($options['transcoder']) { |
|
| 652 | - include_spip('inc/charsets'); |
|
| 653 | - $result['page'] = transcoder_page($result['page'], $result['headers']); |
|
| 654 | - } |
|
| 655 | - |
|
| 656 | - try { |
|
| 657 | - $trace = json_decode(json_encode($result, JSON_THROW_ON_ERROR), true, 512, JSON_THROW_ON_ERROR); |
|
| 658 | - } catch (JsonException $e) { |
|
| 659 | - $trace = []; |
|
| 660 | - $logger->error('Failed to parse Json data : ' . $e->getMessage()); |
|
| 661 | - } |
|
| 662 | - $trace['page'] = '...'; |
|
| 663 | - $logger->debug('RESULTAT recuperer_url ' . $options['methode'] . " sur $url : " . json_encode($trace, JSON_THROW_ON_ERROR)); |
|
| 664 | - |
|
| 665 | - return $result; |
|
| 443 | + // Conserve la mémoire de la méthode fournit éventuellement |
|
| 444 | + $methode_demandee = $options['methode'] ?? ''; |
|
| 445 | + $default = [ |
|
| 446 | + 'transcoder' => false, |
|
| 447 | + 'methode' => 'GET', |
|
| 448 | + 'taille_max' => null, |
|
| 449 | + 'headers' => [], |
|
| 450 | + 'datas' => '', |
|
| 451 | + 'boundary' => '', |
|
| 452 | + 'refuser_gz' => false, |
|
| 453 | + 'if_modified_since' => '', |
|
| 454 | + 'uri_referer' => '', |
|
| 455 | + 'file' => '', |
|
| 456 | + 'follow_location' => 10, |
|
| 457 | + 'version_http' => _INC_DISTANT_VERSION_HTTP, |
|
| 458 | + ]; |
|
| 459 | + $options = array_merge($default, $options); |
|
| 460 | + // copier directement dans un fichier ? |
|
| 461 | + $copy = $options['file']; |
|
| 462 | + |
|
| 463 | + if ($options['methode'] == 'HEAD') { |
|
| 464 | + $options['taille_max'] = 0; |
|
| 465 | + } |
|
| 466 | + if (is_null($options['taille_max'])) { |
|
| 467 | + $options['taille_max'] = $copy ? _COPIE_LOCALE_MAX_SIZE : _INC_DISTANT_MAX_SIZE; |
|
| 468 | + } |
|
| 469 | + |
|
| 470 | + $logger = spip_logger('distant'); |
|
| 471 | + $logger->debug('recuperer_url ' . $options['methode'] . " sur $url"); |
|
| 472 | + |
|
| 473 | + // Ajout des en-têtes spécifiques si besoin |
|
| 474 | + $formatted_data = ''; |
|
| 475 | + if (!empty($options['headers'])) { |
|
| 476 | + foreach ($options['headers'] as $champ => $valeur) { |
|
| 477 | + $formatted_data .= $champ . ': ' . $valeur . "\r\n"; |
|
| 478 | + } |
|
| 479 | + } |
|
| 480 | + |
|
| 481 | + if (!empty($options['datas'])) { |
|
| 482 | + [$head, $postdata] = prepare_donnees_post($options['datas'], $options['boundary']); |
|
| 483 | + $head .= $formatted_data; |
|
| 484 | + if (stripos($head, 'Content-Length:') === false) { |
|
| 485 | + $head .= 'Content-Length: ' . strlen((string) $postdata) . "\r\n"; |
|
| 486 | + } |
|
| 487 | + $formatted_data = $head . "\r\n" . $postdata; |
|
| 488 | + if ( |
|
| 489 | + strlen((string) $postdata) && !$methode_demandee |
|
| 490 | + ) { |
|
| 491 | + $options['methode'] = 'POST'; |
|
| 492 | + } |
|
| 493 | + } elseif ($formatted_data) { |
|
| 494 | + $formatted_data .= "\r\n"; |
|
| 495 | + } |
|
| 496 | + |
|
| 497 | + // Accepter les URLs au format feed:// ou qui ont oublie le http:// ou les urls relatives au protocole |
|
| 498 | + $url = preg_replace(',^feed://,i', 'http://', $url); |
|
| 499 | + if (!tester_url_absolue($url)) { |
|
| 500 | + $url = 'http://' . $url; |
|
| 501 | + } elseif (str_starts_with($url, '//')) { |
|
| 502 | + $url = 'http:' . $url; |
|
| 503 | + } |
|
| 504 | + |
|
| 505 | + $url = url_to_ascii($url); |
|
| 506 | + |
|
| 507 | + $result = [ |
|
| 508 | + 'status' => 0, |
|
| 509 | + 'headers' => '', |
|
| 510 | + 'page' => '', |
|
| 511 | + 'length' => 0, |
|
| 512 | + 'last_modified' => '', |
|
| 513 | + 'location' => '', |
|
| 514 | + 'url' => $url |
|
| 515 | + ]; |
|
| 516 | + |
|
| 517 | + // si on ecrit directement dans un fichier, pour ne pas manipuler en memoire refuser gz |
|
| 518 | + $refuser_gz = ($options['refuser_gz'] || $copy); |
|
| 519 | + |
|
| 520 | + // ouvrir la connexion et envoyer la requete et ses en-tetes |
|
| 521 | + [$handle, $fopen] = init_http( |
|
| 522 | + $options['methode'], |
|
| 523 | + $url, |
|
| 524 | + $refuser_gz, |
|
| 525 | + $options['uri_referer'], |
|
| 526 | + $formatted_data, |
|
| 527 | + $options['version_http'], |
|
| 528 | + $options['if_modified_since'] |
|
| 529 | + ); |
|
| 530 | + if (!$handle) { |
|
| 531 | + $logger->error("ECHEC init_http $url"); |
|
| 532 | + |
|
| 533 | + return false; |
|
| 534 | + } |
|
| 535 | + |
|
| 536 | + // Sauf en fopen, envoyer le flux d'entree |
|
| 537 | + // et recuperer les en-tetes de reponses |
|
| 538 | + if (!$fopen) { |
|
| 539 | + $res = recuperer_entetes_complets($handle, $options['if_modified_since']); |
|
| 540 | + if (!$res) { |
|
| 541 | + fclose($handle); |
|
| 542 | + $t = @parse_url($url); |
|
| 543 | + $host = $t['host']; |
|
| 544 | + // Chinoisierie inexplicable pour contrer |
|
| 545 | + // les actions liberticides de l'empire du milieu |
|
| 546 | + if ( |
|
| 547 | + !need_proxy($host) |
|
| 548 | + && ($res = @file_get_contents($url)) |
|
| 549 | + ) { |
|
| 550 | + $result['length'] = strlen($res); |
|
| 551 | + if ($copy) { |
|
| 552 | + ecrire_fichier($copy, $res); |
|
| 553 | + $result['file'] = $copy; |
|
| 554 | + } else { |
|
| 555 | + $result['page'] = $res; |
|
| 556 | + } |
|
| 557 | + $res = [ |
|
| 558 | + 'status' => 200, |
|
| 559 | + ]; |
|
| 560 | + } else { |
|
| 561 | + $logger->error("ECHEC chinoiserie $url"); |
|
| 562 | + return false; |
|
| 563 | + } |
|
| 564 | + } elseif ($res['location'] && $options['follow_location']) { |
|
| 565 | + $options['follow_location']--; |
|
| 566 | + fclose($handle); |
|
| 567 | + include_spip('inc/filtres'); |
|
| 568 | + $url = suivre_lien($url, $res['location']); |
|
| 569 | + |
|
| 570 | + // une redirection doit se faire en GET, sauf status explicite 307 ou 308 qui indique de garder la meme methode |
|
| 571 | + if ( |
|
| 572 | + $options['methode'] !== 'GET' |
|
| 573 | + && (empty($res['status']) || !in_array($res['status'], [307, 308])) |
|
| 574 | + ) { |
|
| 575 | + $options['methode'] = 'GET'; |
|
| 576 | + $options['datas'] = ''; |
|
| 577 | + } |
|
| 578 | + $logger->debug('recuperer_url recommence ' . $options['methode'] . " sur $url"); |
|
| 579 | + |
|
| 580 | + return recuperer_url($url, $options); |
|
| 581 | + } elseif ($res['status'] !== 200) { |
|
| 582 | + $logger->info('HTTP status ' . $res['status'] . " pour $url"); |
|
| 583 | + } |
|
| 584 | + $result['status'] = $res['status']; |
|
| 585 | + if (isset($res['headers'])) { |
|
| 586 | + $result['headers'] = $res['headers']; |
|
| 587 | + } |
|
| 588 | + if (isset($res['last_modified'])) { |
|
| 589 | + $result['last_modified'] = $res['last_modified']; |
|
| 590 | + } |
|
| 591 | + if (isset($res['location'])) { |
|
| 592 | + $result['location'] = $res['location']; |
|
| 593 | + } |
|
| 594 | + if (isset($res['content_length'])) { |
|
| 595 | + $result['content_length'] = $res['content_length']; |
|
| 596 | + } |
|
| 597 | + } |
|
| 598 | + |
|
| 599 | + // on ne veut que les entetes |
|
| 600 | + if (!$options['taille_max'] || $options['methode'] == 'HEAD' || $result['status'] == '304') { |
|
| 601 | + $logger->debug('RESULTAT recuperer_url ' . $options['methode'] . " sur $url : " . json_encode($result, JSON_THROW_ON_ERROR)); |
|
| 602 | + return $result; |
|
| 603 | + } |
|
| 604 | + |
|
| 605 | + |
|
| 606 | + // s'il faut deballer, le faire via un fichier temporaire |
|
| 607 | + // sinon la memoire explose pour les gros flux |
|
| 608 | + |
|
| 609 | + $gz = false; |
|
| 610 | + if (preg_match(",\bContent-Encoding: .*gzip,is", (string) $result['headers'])) { |
|
| 611 | + $gz = (_DIR_TMP . md5(uniqid(random_int(0, mt_getrandmax()))) . '.tmp.gz'); |
|
| 612 | + } |
|
| 613 | + |
|
| 614 | + // si on n'a pas deja récupéré le contenu par une methode detournée |
|
| 615 | + if (!$result['length']) { |
|
| 616 | + $taille_max = $options['taille_max']; |
|
| 617 | + if (isset($result['content_length']) |
|
| 618 | + && !empty($result['content_length']) |
|
| 619 | + && ($result['content_length'] < $taille_max)) { |
|
| 620 | + $taille_max = $result['content_length']; |
|
| 621 | + } |
|
| 622 | + $res = recuperer_body($handle, $taille_max, $gz ?: $copy); |
|
| 623 | + fclose($handle); |
|
| 624 | + if ($copy) { |
|
| 625 | + $result['length'] = $res; |
|
| 626 | + $result['file'] = $copy; |
|
| 627 | + } elseif ($res) { |
|
| 628 | + $result['page'] = &$res; |
|
| 629 | + $result['length'] = strlen($result['page']); |
|
| 630 | + } |
|
| 631 | + if (!$result['status']) { |
|
| 632 | + $result['status'] = 200; // on a reussi, donc ! |
|
| 633 | + } |
|
| 634 | + } |
|
| 635 | + if (!$result['page']) { |
|
| 636 | + return $result; |
|
| 637 | + } |
|
| 638 | + |
|
| 639 | + // Decompresser au besoin |
|
| 640 | + if ($gz) { |
|
| 641 | + $result['page'] = implode('', gzfile($gz)); |
|
| 642 | + $result['length'] = strlen($result['page']); |
|
| 643 | + // et annuler le content-length qui correspond à la version gzip |
|
| 644 | + if (isset($result['content_length'])) { |
|
| 645 | + unset($result['content_length']); |
|
| 646 | + } |
|
| 647 | + supprimer_fichier($gz); |
|
| 648 | + } |
|
| 649 | + |
|
| 650 | + // Faut-il l'importer dans notre charset local ? |
|
| 651 | + if ($options['transcoder']) { |
|
| 652 | + include_spip('inc/charsets'); |
|
| 653 | + $result['page'] = transcoder_page($result['page'], $result['headers']); |
|
| 654 | + } |
|
| 655 | + |
|
| 656 | + try { |
|
| 657 | + $trace = json_decode(json_encode($result, JSON_THROW_ON_ERROR), true, 512, JSON_THROW_ON_ERROR); |
|
| 658 | + } catch (JsonException $e) { |
|
| 659 | + $trace = []; |
|
| 660 | + $logger->error('Failed to parse Json data : ' . $e->getMessage()); |
|
| 661 | + } |
|
| 662 | + $trace['page'] = '...'; |
|
| 663 | + $logger->debug('RESULTAT recuperer_url ' . $options['methode'] . " sur $url : " . json_encode($trace, JSON_THROW_ON_ERROR)); |
|
| 664 | + |
|
| 665 | + return $result; |
|
| 666 | 666 | } |
| 667 | 667 | |
| 668 | 668 | /** |
@@ -678,73 +678,73 @@ discard block |
||
| 678 | 678 | * @return array|bool|mixed |
| 679 | 679 | */ |
| 680 | 680 | function recuperer_url_cache($url, $options = []) { |
| 681 | - if (!defined('_DELAI_RECUPERER_URL_CACHE')) { |
|
| 682 | - define('_DELAI_RECUPERER_URL_CACHE', 3600); |
|
| 683 | - } |
|
| 684 | - $default = [ |
|
| 685 | - 'transcoder' => false, |
|
| 686 | - 'methode' => 'GET', |
|
| 687 | - 'taille_max' => null, |
|
| 688 | - 'datas' => '', |
|
| 689 | - 'boundary' => '', |
|
| 690 | - 'refuser_gz' => false, |
|
| 691 | - 'if_modified_since' => '', |
|
| 692 | - 'uri_referer' => '', |
|
| 693 | - 'file' => '', |
|
| 694 | - 'follow_location' => 10, |
|
| 695 | - 'version_http' => _INC_DISTANT_VERSION_HTTP, |
|
| 696 | - 'delai_cache' => in_array(_VAR_MODE, ['preview', 'recalcul']) ? 0 : _DELAI_RECUPERER_URL_CACHE, |
|
| 697 | - ]; |
|
| 698 | - $options = array_merge($default, $options); |
|
| 699 | - |
|
| 700 | - // cas ou il n'est pas possible de cacher |
|
| 701 | - if (!empty($options['data']) || $options['methode'] == 'POST') { |
|
| 702 | - return recuperer_url($url, $options); |
|
| 703 | - } |
|
| 704 | - |
|
| 705 | - // ne pas tenter plusieurs fois la meme url en erreur (non cachee donc) |
|
| 706 | - static $errors = []; |
|
| 707 | - if (isset($errors[$url])) { |
|
| 708 | - return $errors[$url]; |
|
| 709 | - } |
|
| 710 | - |
|
| 711 | - $sig = $options; |
|
| 712 | - unset($sig['if_modified_since']); |
|
| 713 | - unset($sig['delai_cache']); |
|
| 714 | - $sig['url'] = $url; |
|
| 715 | - |
|
| 716 | - $dir = sous_repertoire(_DIR_CACHE, 'curl'); |
|
| 717 | - $cache = md5(serialize($sig)) . '-' . substr(preg_replace(',\W+,', '_', $url), 0, 80); |
|
| 718 | - $sub = sous_repertoire($dir, substr($cache, 0, 2)); |
|
| 719 | - $cache = "$sub$cache"; |
|
| 720 | - |
|
| 721 | - $res = false; |
|
| 722 | - $is_cached = file_exists($cache); |
|
| 723 | - if ( |
|
| 724 | - $is_cached |
|
| 725 | - && filemtime($cache) > $_SERVER['REQUEST_TIME'] - $options['delai_cache'] |
|
| 726 | - ) { |
|
| 727 | - lire_fichier($cache, $res); |
|
| 728 | - if ($res = unserialize($res)) { |
|
| 729 | - // mettre le last_modified et le status=304 ? |
|
| 730 | - } |
|
| 731 | - } |
|
| 732 | - if (!$res) { |
|
| 733 | - $res = recuperer_url($url, $options); |
|
| 734 | - // ne pas recharger cette url non cachee dans le meme hit puisque non disponible |
|
| 735 | - if (!$res) { |
|
| 736 | - if ($is_cached) { |
|
| 737 | - // on a pas reussi a recuperer mais on avait un cache : l'utiliser |
|
| 738 | - lire_fichier($cache, $res); |
|
| 739 | - $res = unserialize($res); |
|
| 740 | - } |
|
| 741 | - |
|
| 742 | - return $errors[$url] = $res; |
|
| 743 | - } |
|
| 744 | - ecrire_fichier($cache, serialize($res)); |
|
| 745 | - } |
|
| 746 | - |
|
| 747 | - return $res; |
|
| 681 | + if (!defined('_DELAI_RECUPERER_URL_CACHE')) { |
|
| 682 | + define('_DELAI_RECUPERER_URL_CACHE', 3600); |
|
| 683 | + } |
|
| 684 | + $default = [ |
|
| 685 | + 'transcoder' => false, |
|
| 686 | + 'methode' => 'GET', |
|
| 687 | + 'taille_max' => null, |
|
| 688 | + 'datas' => '', |
|
| 689 | + 'boundary' => '', |
|
| 690 | + 'refuser_gz' => false, |
|
| 691 | + 'if_modified_since' => '', |
|
| 692 | + 'uri_referer' => '', |
|
| 693 | + 'file' => '', |
|
| 694 | + 'follow_location' => 10, |
|
| 695 | + 'version_http' => _INC_DISTANT_VERSION_HTTP, |
|
| 696 | + 'delai_cache' => in_array(_VAR_MODE, ['preview', 'recalcul']) ? 0 : _DELAI_RECUPERER_URL_CACHE, |
|
| 697 | + ]; |
|
| 698 | + $options = array_merge($default, $options); |
|
| 699 | + |
|
| 700 | + // cas ou il n'est pas possible de cacher |
|
| 701 | + if (!empty($options['data']) || $options['methode'] == 'POST') { |
|
| 702 | + return recuperer_url($url, $options); |
|
| 703 | + } |
|
| 704 | + |
|
| 705 | + // ne pas tenter plusieurs fois la meme url en erreur (non cachee donc) |
|
| 706 | + static $errors = []; |
|
| 707 | + if (isset($errors[$url])) { |
|
| 708 | + return $errors[$url]; |
|
| 709 | + } |
|
| 710 | + |
|
| 711 | + $sig = $options; |
|
| 712 | + unset($sig['if_modified_since']); |
|
| 713 | + unset($sig['delai_cache']); |
|
| 714 | + $sig['url'] = $url; |
|
| 715 | + |
|
| 716 | + $dir = sous_repertoire(_DIR_CACHE, 'curl'); |
|
| 717 | + $cache = md5(serialize($sig)) . '-' . substr(preg_replace(',\W+,', '_', $url), 0, 80); |
|
| 718 | + $sub = sous_repertoire($dir, substr($cache, 0, 2)); |
|
| 719 | + $cache = "$sub$cache"; |
|
| 720 | + |
|
| 721 | + $res = false; |
|
| 722 | + $is_cached = file_exists($cache); |
|
| 723 | + if ( |
|
| 724 | + $is_cached |
|
| 725 | + && filemtime($cache) > $_SERVER['REQUEST_TIME'] - $options['delai_cache'] |
|
| 726 | + ) { |
|
| 727 | + lire_fichier($cache, $res); |
|
| 728 | + if ($res = unserialize($res)) { |
|
| 729 | + // mettre le last_modified et le status=304 ? |
|
| 730 | + } |
|
| 731 | + } |
|
| 732 | + if (!$res) { |
|
| 733 | + $res = recuperer_url($url, $options); |
|
| 734 | + // ne pas recharger cette url non cachee dans le meme hit puisque non disponible |
|
| 735 | + if (!$res) { |
|
| 736 | + if ($is_cached) { |
|
| 737 | + // on a pas reussi a recuperer mais on avait un cache : l'utiliser |
|
| 738 | + lire_fichier($cache, $res); |
|
| 739 | + $res = unserialize($res); |
|
| 740 | + } |
|
| 741 | + |
|
| 742 | + return $errors[$url] = $res; |
|
| 743 | + } |
|
| 744 | + ecrire_fichier($cache, serialize($res)); |
|
| 745 | + } |
|
| 746 | + |
|
| 747 | + return $res; |
|
| 748 | 748 | } |
| 749 | 749 | |
| 750 | 750 | /** |
@@ -762,56 +762,56 @@ discard block |
||
| 762 | 762 | * string contenu de la resource |
| 763 | 763 | */ |
| 764 | 764 | function recuperer_body($handle, $taille_max = _INC_DISTANT_MAX_SIZE, $fichier = '') { |
| 765 | - $tmpfile = null; |
|
| 766 | - $taille = 0; |
|
| 767 | - $result = ''; |
|
| 768 | - $fp = false; |
|
| 769 | - if ($fichier) { |
|
| 770 | - include_spip('inc/acces'); |
|
| 771 | - $tmpfile = "$fichier." . creer_uniqid() . '.tmp'; |
|
| 772 | - $fp = spip_fopen_lock($tmpfile, 'w', LOCK_EX); |
|
| 773 | - if (!$fp && file_exists($fichier)) { |
|
| 774 | - return filesize($fichier); |
|
| 775 | - } |
|
| 776 | - if (!$fp) { |
|
| 777 | - return false; |
|
| 778 | - } |
|
| 779 | - $result = 0; // on renvoie la taille du fichier |
|
| 780 | - } |
|
| 781 | - |
|
| 782 | - $max_longueur_morceaux = 8192; |
|
| 783 | - while (!feof($handle) && $taille < $taille_max) { |
|
| 784 | - // ne pas lire plus que ce qu'on a besoin (ou que la longueur annoncée du document) |
|
| 785 | - $max_longueur_morceaux = min($max_longueur_morceaux, $taille_max - $taille); |
|
| 786 | - $res = fread($handle, $max_longueur_morceaux); |
|
| 787 | - |
|
| 788 | - // si feof ne trig pas mais on est à la fin, fread retourne false |
|
| 789 | - if ($res === false) { |
|
| 790 | - break; |
|
| 791 | - } |
|
| 792 | - |
|
| 793 | - $taille_morceau = strlen($res); |
|
| 794 | - $taille += $taille_morceau; |
|
| 795 | - |
|
| 796 | - if ($fp) { |
|
| 797 | - fwrite($fp, $res); |
|
| 798 | - $result = $taille; |
|
| 799 | - } else { |
|
| 800 | - $result .= $res; |
|
| 801 | - } |
|
| 802 | - |
|
| 803 | - } |
|
| 804 | - |
|
| 805 | - if ($fp) { |
|
| 806 | - spip_fclose_unlock($fp); |
|
| 807 | - spip_unlink($fichier); |
|
| 808 | - @rename($tmpfile, $fichier); |
|
| 809 | - if (!file_exists($fichier)) { |
|
| 810 | - return false; |
|
| 811 | - } |
|
| 812 | - } |
|
| 813 | - |
|
| 814 | - return $result; |
|
| 765 | + $tmpfile = null; |
|
| 766 | + $taille = 0; |
|
| 767 | + $result = ''; |
|
| 768 | + $fp = false; |
|
| 769 | + if ($fichier) { |
|
| 770 | + include_spip('inc/acces'); |
|
| 771 | + $tmpfile = "$fichier." . creer_uniqid() . '.tmp'; |
|
| 772 | + $fp = spip_fopen_lock($tmpfile, 'w', LOCK_EX); |
|
| 773 | + if (!$fp && file_exists($fichier)) { |
|
| 774 | + return filesize($fichier); |
|
| 775 | + } |
|
| 776 | + if (!$fp) { |
|
| 777 | + return false; |
|
| 778 | + } |
|
| 779 | + $result = 0; // on renvoie la taille du fichier |
|
| 780 | + } |
|
| 781 | + |
|
| 782 | + $max_longueur_morceaux = 8192; |
|
| 783 | + while (!feof($handle) && $taille < $taille_max) { |
|
| 784 | + // ne pas lire plus que ce qu'on a besoin (ou que la longueur annoncée du document) |
|
| 785 | + $max_longueur_morceaux = min($max_longueur_morceaux, $taille_max - $taille); |
|
| 786 | + $res = fread($handle, $max_longueur_morceaux); |
|
| 787 | + |
|
| 788 | + // si feof ne trig pas mais on est à la fin, fread retourne false |
|
| 789 | + if ($res === false) { |
|
| 790 | + break; |
|
| 791 | + } |
|
| 792 | + |
|
| 793 | + $taille_morceau = strlen($res); |
|
| 794 | + $taille += $taille_morceau; |
|
| 795 | + |
|
| 796 | + if ($fp) { |
|
| 797 | + fwrite($fp, $res); |
|
| 798 | + $result = $taille; |
|
| 799 | + } else { |
|
| 800 | + $result .= $res; |
|
| 801 | + } |
|
| 802 | + |
|
| 803 | + } |
|
| 804 | + |
|
| 805 | + if ($fp) { |
|
| 806 | + spip_fclose_unlock($fp); |
|
| 807 | + spip_unlink($fichier); |
|
| 808 | + @rename($tmpfile, $fichier); |
|
| 809 | + if (!file_exists($fichier)) { |
|
| 810 | + return false; |
|
| 811 | + } |
|
| 812 | + } |
|
| 813 | + |
|
| 814 | + return $result; |
|
| 815 | 815 | } |
| 816 | 816 | |
| 817 | 817 | /** |
@@ -833,38 +833,38 @@ discard block |
||
| 833 | 833 | * string location |
| 834 | 834 | */ |
| 835 | 835 | function recuperer_entetes_complets($handle, $if_modified_since = false) { |
| 836 | - $result = ['status' => 0, 'headers' => [], 'last_modified' => 0, 'location' => '']; |
|
| 837 | - |
|
| 838 | - $s = @trim(fgets($handle, 16384)); |
|
| 839 | - if (!preg_match(',^HTTP/\d+\.\d+ (\d+),', $s, $r)) { |
|
| 840 | - return false; |
|
| 841 | - } |
|
| 842 | - $result['status'] = (int) $r[1]; |
|
| 843 | - while ($s = trim(fgets($handle, 16384))) { |
|
| 844 | - $result['headers'][] = $s . "\n"; |
|
| 845 | - preg_match(',^([^:]*): *(.*)$,i', $s, $r); |
|
| 846 | - [, $d, $v] = $r; |
|
| 847 | - $d = strtolower(trim($d)); |
|
| 848 | - if ( $d === 'location' && $result['status'] >= 300 && $result['status'] < 400) { |
|
| 849 | - $result['location'] = $v; |
|
| 850 | - } elseif ($d === 'last-modified') { |
|
| 851 | - $result['last_modified'] = strtotime($v); |
|
| 852 | - } elseif ($d === 'content-length' and strlen(trim($v))) { |
|
| 853 | - $result['content_length'] = intval($v); |
|
| 854 | - } |
|
| 855 | - } |
|
| 856 | - if ( |
|
| 857 | - $if_modified_since |
|
| 858 | - && $result['last_modified'] |
|
| 859 | - && $if_modified_since > $result['last_modified'] |
|
| 860 | - && $result['status'] == 200 |
|
| 861 | - ) { |
|
| 862 | - $result['status'] = 304; |
|
| 863 | - } |
|
| 864 | - |
|
| 865 | - $result['headers'] = implode('', $result['headers']); |
|
| 866 | - |
|
| 867 | - return $result; |
|
| 836 | + $result = ['status' => 0, 'headers' => [], 'last_modified' => 0, 'location' => '']; |
|
| 837 | + |
|
| 838 | + $s = @trim(fgets($handle, 16384)); |
|
| 839 | + if (!preg_match(',^HTTP/\d+\.\d+ (\d+),', $s, $r)) { |
|
| 840 | + return false; |
|
| 841 | + } |
|
| 842 | + $result['status'] = (int) $r[1]; |
|
| 843 | + while ($s = trim(fgets($handle, 16384))) { |
|
| 844 | + $result['headers'][] = $s . "\n"; |
|
| 845 | + preg_match(',^([^:]*): *(.*)$,i', $s, $r); |
|
| 846 | + [, $d, $v] = $r; |
|
| 847 | + $d = strtolower(trim($d)); |
|
| 848 | + if ( $d === 'location' && $result['status'] >= 300 && $result['status'] < 400) { |
|
| 849 | + $result['location'] = $v; |
|
| 850 | + } elseif ($d === 'last-modified') { |
|
| 851 | + $result['last_modified'] = strtotime($v); |
|
| 852 | + } elseif ($d === 'content-length' and strlen(trim($v))) { |
|
| 853 | + $result['content_length'] = intval($v); |
|
| 854 | + } |
|
| 855 | + } |
|
| 856 | + if ( |
|
| 857 | + $if_modified_since |
|
| 858 | + && $result['last_modified'] |
|
| 859 | + && $if_modified_since > $result['last_modified'] |
|
| 860 | + && $result['status'] == 200 |
|
| 861 | + ) { |
|
| 862 | + $result['status'] = 304; |
|
| 863 | + } |
|
| 864 | + |
|
| 865 | + $result['headers'] = implode('', $result['headers']); |
|
| 866 | + |
|
| 867 | + return $result; |
|
| 868 | 868 | } |
| 869 | 869 | |
| 870 | 870 | /** |
@@ -886,36 +886,36 @@ discard block |
||
| 886 | 886 | * Nom du fichier pour copie locale |
| 887 | 887 | **/ |
| 888 | 888 | function nom_fichier_copie_locale($source, $extension) { |
| 889 | - include_spip('inc/documents'); |
|
| 889 | + include_spip('inc/documents'); |
|
| 890 | 890 | |
| 891 | - $d = creer_repertoire_documents('distant'); # IMG/distant/ |
|
| 892 | - $d = sous_repertoire($d, $extension); # IMG/distant/pdf/ |
|
| 891 | + $d = creer_repertoire_documents('distant'); # IMG/distant/ |
|
| 892 | + $d = sous_repertoire($d, $extension); # IMG/distant/pdf/ |
|
| 893 | 893 | |
| 894 | - // on se place tout le temps comme si on était a la racine |
|
| 895 | - if (_DIR_RACINE) { |
|
| 896 | - $d = preg_replace(',^' . preg_quote((string) _DIR_RACINE, ',') . ',', '', (string) $d); |
|
| 897 | - } |
|
| 894 | + // on se place tout le temps comme si on était a la racine |
|
| 895 | + if (_DIR_RACINE) { |
|
| 896 | + $d = preg_replace(',^' . preg_quote((string) _DIR_RACINE, ',') . ',', '', (string) $d); |
|
| 897 | + } |
|
| 898 | 898 | |
| 899 | - $m = md5($source); |
|
| 899 | + $m = md5($source); |
|
| 900 | 900 | |
| 901 | - $filename = |
|
| 902 | - $d |
|
| 903 | - . substr(preg_replace(',[^\w-],', '', basename($source, $extension)), 0, 16) |
|
| 904 | - . '-' . substr($m, 0, 8) |
|
| 905 | - . ".$extension"; |
|
| 901 | + $filename = |
|
| 902 | + $d |
|
| 903 | + . substr(preg_replace(',[^\w-],', '', basename($source, $extension)), 0, 16) |
|
| 904 | + . '-' . substr($m, 0, 8) |
|
| 905 | + . ".$extension"; |
|
| 906 | 906 | |
| 907 | - // ancien nommage des fichiers distants : renommer le fichier a la volee si besoin pour eviter de dupliquer les caches |
|
| 908 | - $legacy_filename = |
|
| 909 | - $d |
|
| 910 | - . substr(preg_replace(',[^\w-],', '', basename($source)) . '-' . $m, 0, 12) |
|
| 911 | - . substr($m, 0, 4) |
|
| 912 | - . ".$extension"; |
|
| 907 | + // ancien nommage des fichiers distants : renommer le fichier a la volee si besoin pour eviter de dupliquer les caches |
|
| 908 | + $legacy_filename = |
|
| 909 | + $d |
|
| 910 | + . substr(preg_replace(',[^\w-],', '', basename($source)) . '-' . $m, 0, 12) |
|
| 911 | + . substr($m, 0, 4) |
|
| 912 | + . ".$extension"; |
|
| 913 | 913 | |
| 914 | - if (file_exists(_DIR_RACINE . $legacy_filename)) { |
|
| 915 | - @rename(_DIR_RACINE . $legacy_filename, $filename); |
|
| 916 | - } |
|
| 914 | + if (file_exists(_DIR_RACINE . $legacy_filename)) { |
|
| 915 | + @rename(_DIR_RACINE . $legacy_filename, $filename); |
|
| 916 | + } |
|
| 917 | 917 | |
| 918 | - return $filename; |
|
| 918 | + return $filename; |
|
| 919 | 919 | } |
| 920 | 920 | |
| 921 | 921 | /** |
@@ -934,72 +934,72 @@ discard block |
||
| 934 | 934 | * - null: Copie locale impossible |
| 935 | 935 | **/ |
| 936 | 936 | function fichier_copie_locale($source) { |
| 937 | - // Si c'est deja local pas de souci |
|
| 938 | - if (!tester_url_absolue($source)) { |
|
| 939 | - if (_DIR_RACINE) { |
|
| 940 | - $source = preg_replace(',^' . preg_quote((string) _DIR_RACINE, ',') . ',', '', $source); |
|
| 941 | - } |
|
| 942 | - |
|
| 943 | - return $source; |
|
| 944 | - } |
|
| 945 | - |
|
| 946 | - // optimisation : on regarde si on peut deviner l'extension dans l'url et si le fichier |
|
| 947 | - // a deja ete copie en local avec cette extension |
|
| 948 | - // dans ce cas elle est fiable, pas la peine de requeter en base |
|
| 949 | - $path_parts = pathinfo($source); |
|
| 950 | - if (!isset($path_parts['extension'])) { |
|
| 951 | - $path_parts['extension'] = ''; |
|
| 952 | - } |
|
| 953 | - $ext = $path_parts ? $path_parts['extension'] : ''; |
|
| 954 | - if ( |
|
| 955 | - $ext |
|
| 956 | - && preg_match(',^\w+$,', $ext) |
|
| 957 | - && ($f = nom_fichier_copie_locale($source, $ext)) |
|
| 958 | - && file_exists(_DIR_RACINE . $f) |
|
| 959 | - ) { |
|
| 960 | - return $f; |
|
| 961 | - } |
|
| 962 | - |
|
| 963 | - |
|
| 964 | - // Si c'est deja dans la table des documents, |
|
| 965 | - // ramener le nom de sa copie potentielle |
|
| 966 | - $ext = sql_getfetsel('extension', 'spip_documents', 'fichier=' . sql_quote($source) . " AND distant='oui' AND extension <> ''"); |
|
| 967 | - |
|
| 968 | - if ($ext) { |
|
| 969 | - return nom_fichier_copie_locale($source, $ext); |
|
| 970 | - } |
|
| 971 | - |
|
| 972 | - // voir si l'extension indiquee dans le nom du fichier est ok |
|
| 973 | - // et si il n'aurait pas deja ete rapatrie |
|
| 974 | - |
|
| 975 | - $ext = $path_parts ? $path_parts['extension'] : ''; |
|
| 976 | - |
|
| 977 | - if ($ext && sql_getfetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($ext))) { |
|
| 978 | - $f = nom_fichier_copie_locale($source, $ext); |
|
| 979 | - if (file_exists(_DIR_RACINE . $f)) { |
|
| 980 | - return $f; |
|
| 981 | - } |
|
| 982 | - } |
|
| 983 | - |
|
| 984 | - // Ping pour voir si son extension est connue et autorisee |
|
| 985 | - // avec mise en cache du resultat du ping |
|
| 986 | - |
|
| 987 | - $cache = sous_repertoire(_DIR_CACHE, 'rid') . md5($source); |
|
| 988 | - if ( |
|
| 989 | - !@file_exists($cache) |
|
| 990 | - || !($path_parts = @unserialize(spip_file_get_contents($cache))) |
|
| 991 | - || _request('var_mode') === 'recalcul' |
|
| 992 | - ) { |
|
| 993 | - $path_parts = recuperer_infos_distantes($source, ['charger_si_petite_image' => false]); |
|
| 994 | - ecrire_fichier($cache, serialize($path_parts)); |
|
| 995 | - } |
|
| 996 | - $ext = empty($path_parts['extension']) ? '' : $path_parts['extension']; |
|
| 997 | - if ($ext && sql_getfetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($ext))) { |
|
| 998 | - return nom_fichier_copie_locale($source, $ext); |
|
| 999 | - } |
|
| 1000 | - |
|
| 1001 | - spip_logger('distant')->error("pas de copie locale pour $source"); |
|
| 1002 | - return null; |
|
| 937 | + // Si c'est deja local pas de souci |
|
| 938 | + if (!tester_url_absolue($source)) { |
|
| 939 | + if (_DIR_RACINE) { |
|
| 940 | + $source = preg_replace(',^' . preg_quote((string) _DIR_RACINE, ',') . ',', '', $source); |
|
| 941 | + } |
|
| 942 | + |
|
| 943 | + return $source; |
|
| 944 | + } |
|
| 945 | + |
|
| 946 | + // optimisation : on regarde si on peut deviner l'extension dans l'url et si le fichier |
|
| 947 | + // a deja ete copie en local avec cette extension |
|
| 948 | + // dans ce cas elle est fiable, pas la peine de requeter en base |
|
| 949 | + $path_parts = pathinfo($source); |
|
| 950 | + if (!isset($path_parts['extension'])) { |
|
| 951 | + $path_parts['extension'] = ''; |
|
| 952 | + } |
|
| 953 | + $ext = $path_parts ? $path_parts['extension'] : ''; |
|
| 954 | + if ( |
|
| 955 | + $ext |
|
| 956 | + && preg_match(',^\w+$,', $ext) |
|
| 957 | + && ($f = nom_fichier_copie_locale($source, $ext)) |
|
| 958 | + && file_exists(_DIR_RACINE . $f) |
|
| 959 | + ) { |
|
| 960 | + return $f; |
|
| 961 | + } |
|
| 962 | + |
|
| 963 | + |
|
| 964 | + // Si c'est deja dans la table des documents, |
|
| 965 | + // ramener le nom de sa copie potentielle |
|
| 966 | + $ext = sql_getfetsel('extension', 'spip_documents', 'fichier=' . sql_quote($source) . " AND distant='oui' AND extension <> ''"); |
|
| 967 | + |
|
| 968 | + if ($ext) { |
|
| 969 | + return nom_fichier_copie_locale($source, $ext); |
|
| 970 | + } |
|
| 971 | + |
|
| 972 | + // voir si l'extension indiquee dans le nom du fichier est ok |
|
| 973 | + // et si il n'aurait pas deja ete rapatrie |
|
| 974 | + |
|
| 975 | + $ext = $path_parts ? $path_parts['extension'] : ''; |
|
| 976 | + |
|
| 977 | + if ($ext && sql_getfetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($ext))) { |
|
| 978 | + $f = nom_fichier_copie_locale($source, $ext); |
|
| 979 | + if (file_exists(_DIR_RACINE . $f)) { |
|
| 980 | + return $f; |
|
| 981 | + } |
|
| 982 | + } |
|
| 983 | + |
|
| 984 | + // Ping pour voir si son extension est connue et autorisee |
|
| 985 | + // avec mise en cache du resultat du ping |
|
| 986 | + |
|
| 987 | + $cache = sous_repertoire(_DIR_CACHE, 'rid') . md5($source); |
|
| 988 | + if ( |
|
| 989 | + !@file_exists($cache) |
|
| 990 | + || !($path_parts = @unserialize(spip_file_get_contents($cache))) |
|
| 991 | + || _request('var_mode') === 'recalcul' |
|
| 992 | + ) { |
|
| 993 | + $path_parts = recuperer_infos_distantes($source, ['charger_si_petite_image' => false]); |
|
| 994 | + ecrire_fichier($cache, serialize($path_parts)); |
|
| 995 | + } |
|
| 996 | + $ext = empty($path_parts['extension']) ? '' : $path_parts['extension']; |
|
| 997 | + if ($ext && sql_getfetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($ext))) { |
|
| 998 | + return nom_fichier_copie_locale($source, $ext); |
|
| 999 | + } |
|
| 1000 | + |
|
| 1001 | + spip_logger('distant')->error("pas de copie locale pour $source"); |
|
| 1002 | + return null; |
|
| 1003 | 1003 | } |
| 1004 | 1004 | |
| 1005 | 1005 | |
@@ -1028,129 +1028,129 @@ discard block |
||
| 1028 | 1028 | **/ |
| 1029 | 1029 | function recuperer_infos_distantes($source, $options = []) { |
| 1030 | 1030 | |
| 1031 | - // pas la peine de perdre son temps |
|
| 1032 | - if (!tester_url_absolue($source)) { |
|
| 1033 | - return false; |
|
| 1034 | - } |
|
| 1035 | - |
|
| 1036 | - $taille_max = $options['taille_max'] ?? 0; |
|
| 1037 | - $charger_si_petite_image = (bool) ($options['charger_si_petite_image'] ?? true); |
|
| 1038 | - $callback_valider_url = $options['callback_valider_url'] ?? null; |
|
| 1039 | - |
|
| 1040 | - # charger les alias des types mime |
|
| 1041 | - include_spip('base/typedoc'); |
|
| 1042 | - |
|
| 1043 | - $a = []; |
|
| 1044 | - $mime_type = ''; |
|
| 1045 | - // On va directement charger le debut des images et des fichiers html, |
|
| 1046 | - // de maniere a attrapper le maximum d'infos (titre, taille, etc). Si |
|
| 1047 | - // ca echoue l'utilisateur devra les entrer... |
|
| 1048 | - $reponse = recuperer_url($source, ['taille_max' => $taille_max, 'refuser_gz' => true]); |
|
| 1049 | - if ( |
|
| 1050 | - $callback_valider_url |
|
| 1051 | - && is_callable($callback_valider_url) |
|
| 1052 | - && !$callback_valider_url($reponse['url']) |
|
| 1053 | - ) { |
|
| 1054 | - return false; |
|
| 1055 | - } |
|
| 1056 | - $headers = $reponse['headers'] ?? ''; |
|
| 1057 | - $a['body'] = $reponse['page'] ?? ''; |
|
| 1058 | - if ($headers) { |
|
| 1059 | - $mime_type = distant_trouver_mime_type_selon_headers($source, $headers); |
|
| 1060 | - |
|
| 1061 | - if (!$extension = distant_trouver_extension_selon_headers($source, $headers)) { |
|
| 1062 | - return false; |
|
| 1063 | - } |
|
| 1064 | - |
|
| 1065 | - $a['extension'] = $extension; |
|
| 1066 | - |
|
| 1067 | - if (preg_match(",\nContent-Length: *([^[:space:]]*),i", "\n$headers", $regs)) { |
|
| 1068 | - $a['taille'] = (int) $regs[1]; |
|
| 1069 | - } |
|
| 1070 | - } |
|
| 1071 | - |
|
| 1072 | - // Echec avec HEAD, on tente avec GET |
|
| 1073 | - if (!$a && !$taille_max) { |
|
| 1074 | - spip_logger('distant')->info("tenter GET $source"); |
|
| 1075 | - $options['taille_max'] = _INC_DISTANT_MAX_SIZE; |
|
| 1076 | - $a = recuperer_infos_distantes($source, $options); |
|
| 1077 | - } |
|
| 1078 | - |
|
| 1079 | - // si on a rien trouve pas la peine d'insister |
|
| 1080 | - if (!$a) { |
|
| 1081 | - return false; |
|
| 1082 | - } |
|
| 1083 | - |
|
| 1084 | - // S'il s'agit d'une image pas trop grosse ou d'un fichier html, on va aller |
|
| 1085 | - // recharger le document en GET et recuperer des donnees supplementaires... |
|
| 1086 | - include_spip('inc/filtres_images_lib_mini'); |
|
| 1087 | - include_spip('inc/documents'); |
|
| 1088 | - if ( |
|
| 1089 | - str_starts_with($mime_type, 'image/') |
|
| 1090 | - && ($extension = _image_trouver_extension_depuis_mime($mime_type)) |
|
| 1091 | - ) { |
|
| 1092 | - if ( |
|
| 1093 | - $taille_max == 0 |
|
| 1094 | - && (empty($a['taille']) || $a['taille'] < _INC_DISTANT_MAX_SIZE) |
|
| 1095 | - && in_array($extension, formats_image_acceptables()) |
|
| 1096 | - && $charger_si_petite_image |
|
| 1097 | - ) { |
|
| 1098 | - $options['taille_max'] = _INC_DISTANT_MAX_SIZE; |
|
| 1099 | - $a = recuperer_infos_distantes($source, $options); |
|
| 1100 | - } else { |
|
| 1101 | - if ($a['body']) { |
|
| 1102 | - $a['extension'] = corriger_extension($extension); |
|
| 1103 | - $a['fichier'] = _DIR_RACINE . nom_fichier_copie_locale($source, $extension); |
|
| 1104 | - ecrire_fichier($a['fichier'], $a['body']); |
|
| 1105 | - $size_image = @spip_getimagesize($a['fichier']); |
|
| 1106 | - $a['largeur'] = (int) $size_image[0]; |
|
| 1107 | - $a['hauteur'] = (int) $size_image[1]; |
|
| 1108 | - $a['type_image'] = true; |
|
| 1109 | - } |
|
| 1110 | - } |
|
| 1111 | - } |
|
| 1112 | - |
|
| 1113 | - // Fichier swf, si on n'a pas la taille, on va mettre 425x350 par defaut |
|
| 1114 | - // ce sera mieux que 0x0 |
|
| 1115 | - // Flash is dead! |
|
| 1116 | - if ( |
|
| 1117 | - $a |
|
| 1118 | - && isset($a['extension']) |
|
| 1119 | - && $a['extension'] == 'swf' |
|
| 1120 | - && empty($a['largeur']) |
|
| 1121 | - ) { |
|
| 1122 | - $a['largeur'] = 425; |
|
| 1123 | - $a['hauteur'] = 350; |
|
| 1124 | - } |
|
| 1125 | - |
|
| 1126 | - if ($mime_type == 'text/html') { |
|
| 1127 | - include_spip('inc/filtres'); |
|
| 1128 | - $page = recuperer_url($source, ['transcoder' => true, 'taille_max' => _INC_DISTANT_MAX_SIZE]); |
|
| 1129 | - $page = $page['page'] ?? ''; |
|
| 1130 | - if (preg_match(',<title>(.*?)</title>,ims', (string) $page, $regs)) { |
|
| 1131 | - $a['titre'] = corriger_caracteres(trim($regs[1])); |
|
| 1132 | - } |
|
| 1133 | - if (!isset($a['taille']) || !$a['taille']) { |
|
| 1134 | - $a['taille'] = strlen((string) $page); # a peu pres |
|
| 1135 | - } |
|
| 1136 | - } |
|
| 1137 | - $a['mime_type'] = $mime_type; |
|
| 1138 | - |
|
| 1139 | - return $a; |
|
| 1031 | + // pas la peine de perdre son temps |
|
| 1032 | + if (!tester_url_absolue($source)) { |
|
| 1033 | + return false; |
|
| 1034 | + } |
|
| 1035 | + |
|
| 1036 | + $taille_max = $options['taille_max'] ?? 0; |
|
| 1037 | + $charger_si_petite_image = (bool) ($options['charger_si_petite_image'] ?? true); |
|
| 1038 | + $callback_valider_url = $options['callback_valider_url'] ?? null; |
|
| 1039 | + |
|
| 1040 | + # charger les alias des types mime |
|
| 1041 | + include_spip('base/typedoc'); |
|
| 1042 | + |
|
| 1043 | + $a = []; |
|
| 1044 | + $mime_type = ''; |
|
| 1045 | + // On va directement charger le debut des images et des fichiers html, |
|
| 1046 | + // de maniere a attrapper le maximum d'infos (titre, taille, etc). Si |
|
| 1047 | + // ca echoue l'utilisateur devra les entrer... |
|
| 1048 | + $reponse = recuperer_url($source, ['taille_max' => $taille_max, 'refuser_gz' => true]); |
|
| 1049 | + if ( |
|
| 1050 | + $callback_valider_url |
|
| 1051 | + && is_callable($callback_valider_url) |
|
| 1052 | + && !$callback_valider_url($reponse['url']) |
|
| 1053 | + ) { |
|
| 1054 | + return false; |
|
| 1055 | + } |
|
| 1056 | + $headers = $reponse['headers'] ?? ''; |
|
| 1057 | + $a['body'] = $reponse['page'] ?? ''; |
|
| 1058 | + if ($headers) { |
|
| 1059 | + $mime_type = distant_trouver_mime_type_selon_headers($source, $headers); |
|
| 1060 | + |
|
| 1061 | + if (!$extension = distant_trouver_extension_selon_headers($source, $headers)) { |
|
| 1062 | + return false; |
|
| 1063 | + } |
|
| 1064 | + |
|
| 1065 | + $a['extension'] = $extension; |
|
| 1066 | + |
|
| 1067 | + if (preg_match(",\nContent-Length: *([^[:space:]]*),i", "\n$headers", $regs)) { |
|
| 1068 | + $a['taille'] = (int) $regs[1]; |
|
| 1069 | + } |
|
| 1070 | + } |
|
| 1071 | + |
|
| 1072 | + // Echec avec HEAD, on tente avec GET |
|
| 1073 | + if (!$a && !$taille_max) { |
|
| 1074 | + spip_logger('distant')->info("tenter GET $source"); |
|
| 1075 | + $options['taille_max'] = _INC_DISTANT_MAX_SIZE; |
|
| 1076 | + $a = recuperer_infos_distantes($source, $options); |
|
| 1077 | + } |
|
| 1078 | + |
|
| 1079 | + // si on a rien trouve pas la peine d'insister |
|
| 1080 | + if (!$a) { |
|
| 1081 | + return false; |
|
| 1082 | + } |
|
| 1083 | + |
|
| 1084 | + // S'il s'agit d'une image pas trop grosse ou d'un fichier html, on va aller |
|
| 1085 | + // recharger le document en GET et recuperer des donnees supplementaires... |
|
| 1086 | + include_spip('inc/filtres_images_lib_mini'); |
|
| 1087 | + include_spip('inc/documents'); |
|
| 1088 | + if ( |
|
| 1089 | + str_starts_with($mime_type, 'image/') |
|
| 1090 | + && ($extension = _image_trouver_extension_depuis_mime($mime_type)) |
|
| 1091 | + ) { |
|
| 1092 | + if ( |
|
| 1093 | + $taille_max == 0 |
|
| 1094 | + && (empty($a['taille']) || $a['taille'] < _INC_DISTANT_MAX_SIZE) |
|
| 1095 | + && in_array($extension, formats_image_acceptables()) |
|
| 1096 | + && $charger_si_petite_image |
|
| 1097 | + ) { |
|
| 1098 | + $options['taille_max'] = _INC_DISTANT_MAX_SIZE; |
|
| 1099 | + $a = recuperer_infos_distantes($source, $options); |
|
| 1100 | + } else { |
|
| 1101 | + if ($a['body']) { |
|
| 1102 | + $a['extension'] = corriger_extension($extension); |
|
| 1103 | + $a['fichier'] = _DIR_RACINE . nom_fichier_copie_locale($source, $extension); |
|
| 1104 | + ecrire_fichier($a['fichier'], $a['body']); |
|
| 1105 | + $size_image = @spip_getimagesize($a['fichier']); |
|
| 1106 | + $a['largeur'] = (int) $size_image[0]; |
|
| 1107 | + $a['hauteur'] = (int) $size_image[1]; |
|
| 1108 | + $a['type_image'] = true; |
|
| 1109 | + } |
|
| 1110 | + } |
|
| 1111 | + } |
|
| 1112 | + |
|
| 1113 | + // Fichier swf, si on n'a pas la taille, on va mettre 425x350 par defaut |
|
| 1114 | + // ce sera mieux que 0x0 |
|
| 1115 | + // Flash is dead! |
|
| 1116 | + if ( |
|
| 1117 | + $a |
|
| 1118 | + && isset($a['extension']) |
|
| 1119 | + && $a['extension'] == 'swf' |
|
| 1120 | + && empty($a['largeur']) |
|
| 1121 | + ) { |
|
| 1122 | + $a['largeur'] = 425; |
|
| 1123 | + $a['hauteur'] = 350; |
|
| 1124 | + } |
|
| 1125 | + |
|
| 1126 | + if ($mime_type == 'text/html') { |
|
| 1127 | + include_spip('inc/filtres'); |
|
| 1128 | + $page = recuperer_url($source, ['transcoder' => true, 'taille_max' => _INC_DISTANT_MAX_SIZE]); |
|
| 1129 | + $page = $page['page'] ?? ''; |
|
| 1130 | + if (preg_match(',<title>(.*?)</title>,ims', (string) $page, $regs)) { |
|
| 1131 | + $a['titre'] = corriger_caracteres(trim($regs[1])); |
|
| 1132 | + } |
|
| 1133 | + if (!isset($a['taille']) || !$a['taille']) { |
|
| 1134 | + $a['taille'] = strlen((string) $page); # a peu pres |
|
| 1135 | + } |
|
| 1136 | + } |
|
| 1137 | + $a['mime_type'] = $mime_type; |
|
| 1138 | + |
|
| 1139 | + return $a; |
|
| 1140 | 1140 | } |
| 1141 | 1141 | |
| 1142 | 1142 | /** |
| 1143 | 1143 | * Retrouver un mime type depuis les headers |
| 1144 | 1144 | */ |
| 1145 | 1145 | function distant_trouver_mime_type_selon_headers(string $source, string $headers): string { |
| 1146 | - $mime_type = preg_match(",\nContent-Type: *([^[:space:];]*),i", "\n$headers", $regs) ? trim($regs[1]) : ''; // inconnu |
|
| 1146 | + $mime_type = preg_match(",\nContent-Type: *([^[:space:];]*),i", "\n$headers", $regs) ? trim($regs[1]) : ''; // inconnu |
|
| 1147 | 1147 | |
| 1148 | - // Appliquer les alias |
|
| 1149 | - while (isset($GLOBALS['mime_alias'][$mime_type])) { |
|
| 1150 | - $mime_type = $GLOBALS['mime_alias'][$mime_type]; |
|
| 1151 | - } |
|
| 1148 | + // Appliquer les alias |
|
| 1149 | + while (isset($GLOBALS['mime_alias'][$mime_type])) { |
|
| 1150 | + $mime_type = $GLOBALS['mime_alias'][$mime_type]; |
|
| 1151 | + } |
|
| 1152 | 1152 | |
| 1153 | - return $mime_type; |
|
| 1153 | + return $mime_type; |
|
| 1154 | 1154 | } |
| 1155 | 1155 | |
| 1156 | 1156 | /** |
@@ -1159,58 +1159,58 @@ discard block |
||
| 1159 | 1159 | * @return false|string |
| 1160 | 1160 | */ |
| 1161 | 1161 | function distant_trouver_extension_selon_headers(string $source, string $headers) { |
| 1162 | - $mime_type = distant_trouver_mime_type_selon_headers($source, $headers); |
|
| 1163 | - |
|
| 1164 | - // pour corriger_extension() |
|
| 1165 | - include_spip('inc/documents'); |
|
| 1166 | - |
|
| 1167 | - // Si on a un mime-type insignifiant |
|
| 1168 | - // text/plain,application/octet-stream ou vide |
|
| 1169 | - // c'est peut-etre que le serveur ne sait pas |
|
| 1170 | - // ce qu'il sert ; on va tenter de detecter via l'extension de l'url |
|
| 1171 | - // ou le Content-Disposition: attachment; filename=... |
|
| 1172 | - $t = null; |
|
| 1173 | - if (in_array($mime_type, ['text/plain', '', 'application/octet-stream'])) { |
|
| 1174 | - if (!$t && preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $source, $rext)) { |
|
| 1175 | - $t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote(corriger_extension($rext[1]), '', 'text')); |
|
| 1176 | - } |
|
| 1177 | - if ( |
|
| 1178 | - !$t |
|
| 1179 | - && preg_match(',^Content-Disposition:\s*attachment;\s*filename=(.*)$,Uims', $headers, $m) |
|
| 1180 | - && preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $m[1], $rext) |
|
| 1181 | - ) { |
|
| 1182 | - $t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote(corriger_extension($rext[1]), '', 'text')); |
|
| 1183 | - } |
|
| 1184 | - } |
|
| 1185 | - |
|
| 1186 | - // Autre mime/type (ou text/plain avec fichier d'extension inconnue) |
|
| 1187 | - if (!$t) { |
|
| 1188 | - $t = sql_fetsel('extension', 'spip_types_documents', 'mime_type=' . sql_quote($mime_type)); |
|
| 1189 | - } |
|
| 1190 | - |
|
| 1191 | - // Toujours rien ? (ex: audio/x-ogg au lieu de application/ogg) |
|
| 1192 | - // On essaie de nouveau avec l'extension |
|
| 1193 | - if ( |
|
| 1194 | - !$t |
|
| 1195 | - && $mime_type != 'text/plain' |
|
| 1196 | - && preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $source, $rext) |
|
| 1197 | - ) { |
|
| 1198 | - # eviter xxx.3 => 3gp (> SPIP 3) |
|
| 1199 | - $t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote(corriger_extension($rext[1]), '', 'text')); |
|
| 1200 | - } |
|
| 1201 | - |
|
| 1202 | - if ($t) { |
|
| 1203 | - spip_logger('distant')->info("mime-type $mime_type ok, extension " . $t['extension']); |
|
| 1204 | - return $t['extension']; |
|
| 1205 | - } else { |
|
| 1206 | - # par defaut on retombe sur '.bin' si c'est autorise |
|
| 1207 | - spip_logger('distant')->info("mime-type $mime_type inconnu"); |
|
| 1208 | - $t = sql_fetsel('extension', 'spip_types_documents', "extension='bin'"); |
|
| 1209 | - if (!$t) { |
|
| 1210 | - return false; |
|
| 1211 | - } |
|
| 1212 | - return $t['extension']; |
|
| 1213 | - } |
|
| 1162 | + $mime_type = distant_trouver_mime_type_selon_headers($source, $headers); |
|
| 1163 | + |
|
| 1164 | + // pour corriger_extension() |
|
| 1165 | + include_spip('inc/documents'); |
|
| 1166 | + |
|
| 1167 | + // Si on a un mime-type insignifiant |
|
| 1168 | + // text/plain,application/octet-stream ou vide |
|
| 1169 | + // c'est peut-etre que le serveur ne sait pas |
|
| 1170 | + // ce qu'il sert ; on va tenter de detecter via l'extension de l'url |
|
| 1171 | + // ou le Content-Disposition: attachment; filename=... |
|
| 1172 | + $t = null; |
|
| 1173 | + if (in_array($mime_type, ['text/plain', '', 'application/octet-stream'])) { |
|
| 1174 | + if (!$t && preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $source, $rext)) { |
|
| 1175 | + $t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote(corriger_extension($rext[1]), '', 'text')); |
|
| 1176 | + } |
|
| 1177 | + if ( |
|
| 1178 | + !$t |
|
| 1179 | + && preg_match(',^Content-Disposition:\s*attachment;\s*filename=(.*)$,Uims', $headers, $m) |
|
| 1180 | + && preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $m[1], $rext) |
|
| 1181 | + ) { |
|
| 1182 | + $t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote(corriger_extension($rext[1]), '', 'text')); |
|
| 1183 | + } |
|
| 1184 | + } |
|
| 1185 | + |
|
| 1186 | + // Autre mime/type (ou text/plain avec fichier d'extension inconnue) |
|
| 1187 | + if (!$t) { |
|
| 1188 | + $t = sql_fetsel('extension', 'spip_types_documents', 'mime_type=' . sql_quote($mime_type)); |
|
| 1189 | + } |
|
| 1190 | + |
|
| 1191 | + // Toujours rien ? (ex: audio/x-ogg au lieu de application/ogg) |
|
| 1192 | + // On essaie de nouveau avec l'extension |
|
| 1193 | + if ( |
|
| 1194 | + !$t |
|
| 1195 | + && $mime_type != 'text/plain' |
|
| 1196 | + && preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $source, $rext) |
|
| 1197 | + ) { |
|
| 1198 | + # eviter xxx.3 => 3gp (> SPIP 3) |
|
| 1199 | + $t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote(corriger_extension($rext[1]), '', 'text')); |
|
| 1200 | + } |
|
| 1201 | + |
|
| 1202 | + if ($t) { |
|
| 1203 | + spip_logger('distant')->info("mime-type $mime_type ok, extension " . $t['extension']); |
|
| 1204 | + return $t['extension']; |
|
| 1205 | + } else { |
|
| 1206 | + # par defaut on retombe sur '.bin' si c'est autorise |
|
| 1207 | + spip_logger('distant')->info("mime-type $mime_type inconnu"); |
|
| 1208 | + $t = sql_fetsel('extension', 'spip_types_documents', "extension='bin'"); |
|
| 1209 | + if (!$t) { |
|
| 1210 | + return false; |
|
| 1211 | + } |
|
| 1212 | + return $t['extension']; |
|
| 1213 | + } |
|
| 1214 | 1214 | } |
| 1215 | 1215 | |
| 1216 | 1216 | /** |
@@ -1226,45 +1226,45 @@ discard block |
||
| 1226 | 1226 | */ |
| 1227 | 1227 | function need_proxy($host, $http_proxy = null, $http_noproxy = null) { |
| 1228 | 1228 | |
| 1229 | - $http_proxy ??= $GLOBALS['meta']['http_proxy'] ?? null; |
|
| 1230 | - |
|
| 1231 | - // rien a faire si pas de proxy :) |
|
| 1232 | - if (is_null($http_proxy) || !$http_proxy = trim((string) $http_proxy)) { |
|
| 1233 | - return ''; |
|
| 1234 | - } |
|
| 1235 | - |
|
| 1236 | - if (is_null($http_noproxy)) { |
|
| 1237 | - $http_noproxy = $GLOBALS['meta']['http_noproxy'] ?? null; |
|
| 1238 | - } |
|
| 1239 | - // si pas d'exception, on retourne le proxy |
|
| 1240 | - if (is_null($http_noproxy) || !$http_noproxy = trim((string) $http_noproxy)) { |
|
| 1241 | - return $http_proxy; |
|
| 1242 | - } |
|
| 1243 | - |
|
| 1244 | - // si le host ou l'un des domaines parents est dans $http_noproxy on fait exception |
|
| 1245 | - // $http_noproxy peut contenir plusieurs domaines separes par des espaces ou retour ligne |
|
| 1246 | - $http_noproxy = str_replace("\n", ' ', $http_noproxy); |
|
| 1247 | - $http_noproxy = str_replace("\r", ' ', $http_noproxy); |
|
| 1248 | - $http_noproxy = " $http_noproxy "; |
|
| 1249 | - $domain = $host; |
|
| 1250 | - // si le domaine exact www.example.org est dans les exceptions |
|
| 1251 | - if (str_contains($http_noproxy, (string) " $domain ")) { |
|
| 1252 | - return ''; |
|
| 1253 | - } |
|
| 1254 | - |
|
| 1255 | - while (str_contains($domain, '.')) { |
|
| 1256 | - $domain = explode('.', $domain); |
|
| 1257 | - array_shift($domain); |
|
| 1258 | - $domain = implode('.', $domain); |
|
| 1259 | - |
|
| 1260 | - // ou si un domaine parent commencant par un . est dans les exceptions (indiquant qu'il couvre tous les sous-domaines) |
|
| 1261 | - if (str_contains($http_noproxy, (string) " .$domain ")) { |
|
| 1262 | - return ''; |
|
| 1263 | - } |
|
| 1264 | - } |
|
| 1265 | - |
|
| 1266 | - // ok c'est pas une exception |
|
| 1267 | - return $http_proxy; |
|
| 1229 | + $http_proxy ??= $GLOBALS['meta']['http_proxy'] ?? null; |
|
| 1230 | + |
|
| 1231 | + // rien a faire si pas de proxy :) |
|
| 1232 | + if (is_null($http_proxy) || !$http_proxy = trim((string) $http_proxy)) { |
|
| 1233 | + return ''; |
|
| 1234 | + } |
|
| 1235 | + |
|
| 1236 | + if (is_null($http_noproxy)) { |
|
| 1237 | + $http_noproxy = $GLOBALS['meta']['http_noproxy'] ?? null; |
|
| 1238 | + } |
|
| 1239 | + // si pas d'exception, on retourne le proxy |
|
| 1240 | + if (is_null($http_noproxy) || !$http_noproxy = trim((string) $http_noproxy)) { |
|
| 1241 | + return $http_proxy; |
|
| 1242 | + } |
|
| 1243 | + |
|
| 1244 | + // si le host ou l'un des domaines parents est dans $http_noproxy on fait exception |
|
| 1245 | + // $http_noproxy peut contenir plusieurs domaines separes par des espaces ou retour ligne |
|
| 1246 | + $http_noproxy = str_replace("\n", ' ', $http_noproxy); |
|
| 1247 | + $http_noproxy = str_replace("\r", ' ', $http_noproxy); |
|
| 1248 | + $http_noproxy = " $http_noproxy "; |
|
| 1249 | + $domain = $host; |
|
| 1250 | + // si le domaine exact www.example.org est dans les exceptions |
|
| 1251 | + if (str_contains($http_noproxy, (string) " $domain ")) { |
|
| 1252 | + return ''; |
|
| 1253 | + } |
|
| 1254 | + |
|
| 1255 | + while (str_contains($domain, '.')) { |
|
| 1256 | + $domain = explode('.', $domain); |
|
| 1257 | + array_shift($domain); |
|
| 1258 | + $domain = implode('.', $domain); |
|
| 1259 | + |
|
| 1260 | + // ou si un domaine parent commencant par un . est dans les exceptions (indiquant qu'il couvre tous les sous-domaines) |
|
| 1261 | + if (str_contains($http_noproxy, (string) " .$domain ")) { |
|
| 1262 | + return ''; |
|
| 1263 | + } |
|
| 1264 | + } |
|
| 1265 | + |
|
| 1266 | + // ok c'est pas une exception |
|
| 1267 | + return $http_proxy; |
|
| 1268 | 1268 | } |
| 1269 | 1269 | |
| 1270 | 1270 | |
@@ -1287,60 +1287,60 @@ discard block |
||
| 1287 | 1287 | * @return array |
| 1288 | 1288 | */ |
| 1289 | 1289 | function init_http($method, $url, $refuse_gz = false, $referer = '', $datas = '', $vers = 'HTTP/1.0', $date = '') { |
| 1290 | - $user = $via_proxy = $proxy_user = ''; |
|
| 1291 | - $fopen = false; |
|
| 1292 | - |
|
| 1293 | - $t = @parse_url($url); |
|
| 1294 | - $host = $t['host']; |
|
| 1295 | - if ($t['scheme'] == 'http') { |
|
| 1296 | - $scheme = 'http'; |
|
| 1297 | - $noproxy = ''; |
|
| 1298 | - } elseif ($t['scheme'] == 'https') { |
|
| 1299 | - $scheme = 'ssl'; |
|
| 1300 | - $noproxy = 'ssl://'; |
|
| 1301 | - if (!isset($t['port']) || !($port = $t['port'])) { |
|
| 1302 | - $t['port'] = 443; |
|
| 1303 | - } |
|
| 1304 | - } else { |
|
| 1305 | - $scheme = $t['scheme']; |
|
| 1306 | - $noproxy = $scheme . '://'; |
|
| 1307 | - } |
|
| 1308 | - if (isset($t['user'])) { |
|
| 1309 | - // user et pass doivent être passés en urlencodé dans l'URL, on redecode ici |
|
| 1310 | - $user = [urldecode($t['user']), urldecode($t['pass'])]; |
|
| 1311 | - } |
|
| 1312 | - |
|
| 1313 | - if (!isset($t['port']) || !($port = $t['port'])) { |
|
| 1314 | - $port = 80; |
|
| 1315 | - } |
|
| 1316 | - if (!isset($t['path']) || !($path = $t['path'])) { |
|
| 1317 | - $path = '/'; |
|
| 1318 | - } |
|
| 1319 | - |
|
| 1320 | - if (!empty($t['query'])) { |
|
| 1321 | - $path .= '?' . $t['query']; |
|
| 1322 | - } |
|
| 1323 | - |
|
| 1324 | - $f = lance_requete($method, $scheme, $user, $host, $path, $port, $noproxy, $refuse_gz, $referer, $datas, $vers, $date); |
|
| 1325 | - if (!$f || !is_resource($f)) { |
|
| 1326 | - // fallback : fopen si on a pas fait timeout dans lance_requete |
|
| 1327 | - // ce qui correspond a $f===110 |
|
| 1328 | - if ( |
|
| 1329 | - $f !== 110 |
|
| 1330 | - && !need_proxy($host) |
|
| 1331 | - && !_request('tester_proxy') |
|
| 1332 | - && (!isset($GLOBALS['inc_distant_allow_fopen']) || $GLOBALS['inc_distant_allow_fopen']) |
|
| 1333 | - ) { |
|
| 1334 | - $f = @fopen($url, 'rb'); |
|
| 1335 | - spip_logger('distant')->info("connexion vers $url par simple fopen"); |
|
| 1336 | - $fopen = true; |
|
| 1337 | - } else { |
|
| 1338 | - // echec total |
|
| 1339 | - $f = false; |
|
| 1340 | - } |
|
| 1341 | - } |
|
| 1342 | - |
|
| 1343 | - return [$f, $fopen]; |
|
| 1290 | + $user = $via_proxy = $proxy_user = ''; |
|
| 1291 | + $fopen = false; |
|
| 1292 | + |
|
| 1293 | + $t = @parse_url($url); |
|
| 1294 | + $host = $t['host']; |
|
| 1295 | + if ($t['scheme'] == 'http') { |
|
| 1296 | + $scheme = 'http'; |
|
| 1297 | + $noproxy = ''; |
|
| 1298 | + } elseif ($t['scheme'] == 'https') { |
|
| 1299 | + $scheme = 'ssl'; |
|
| 1300 | + $noproxy = 'ssl://'; |
|
| 1301 | + if (!isset($t['port']) || !($port = $t['port'])) { |
|
| 1302 | + $t['port'] = 443; |
|
| 1303 | + } |
|
| 1304 | + } else { |
|
| 1305 | + $scheme = $t['scheme']; |
|
| 1306 | + $noproxy = $scheme . '://'; |
|
| 1307 | + } |
|
| 1308 | + if (isset($t['user'])) { |
|
| 1309 | + // user et pass doivent être passés en urlencodé dans l'URL, on redecode ici |
|
| 1310 | + $user = [urldecode($t['user']), urldecode($t['pass'])]; |
|
| 1311 | + } |
|
| 1312 | + |
|
| 1313 | + if (!isset($t['port']) || !($port = $t['port'])) { |
|
| 1314 | + $port = 80; |
|
| 1315 | + } |
|
| 1316 | + if (!isset($t['path']) || !($path = $t['path'])) { |
|
| 1317 | + $path = '/'; |
|
| 1318 | + } |
|
| 1319 | + |
|
| 1320 | + if (!empty($t['query'])) { |
|
| 1321 | + $path .= '?' . $t['query']; |
|
| 1322 | + } |
|
| 1323 | + |
|
| 1324 | + $f = lance_requete($method, $scheme, $user, $host, $path, $port, $noproxy, $refuse_gz, $referer, $datas, $vers, $date); |
|
| 1325 | + if (!$f || !is_resource($f)) { |
|
| 1326 | + // fallback : fopen si on a pas fait timeout dans lance_requete |
|
| 1327 | + // ce qui correspond a $f===110 |
|
| 1328 | + if ( |
|
| 1329 | + $f !== 110 |
|
| 1330 | + && !need_proxy($host) |
|
| 1331 | + && !_request('tester_proxy') |
|
| 1332 | + && (!isset($GLOBALS['inc_distant_allow_fopen']) || $GLOBALS['inc_distant_allow_fopen']) |
|
| 1333 | + ) { |
|
| 1334 | + $f = @fopen($url, 'rb'); |
|
| 1335 | + spip_logger('distant')->info("connexion vers $url par simple fopen"); |
|
| 1336 | + $fopen = true; |
|
| 1337 | + } else { |
|
| 1338 | + // echec total |
|
| 1339 | + $f = false; |
|
| 1340 | + } |
|
| 1341 | + } |
|
| 1342 | + |
|
| 1343 | + return [$f, $fopen]; |
|
| 1344 | 1344 | } |
| 1345 | 1345 | |
| 1346 | 1346 | /** |
@@ -1375,126 +1375,126 @@ discard block |
||
| 1375 | 1375 | * resource socket vers l'url demandee |
| 1376 | 1376 | */ |
| 1377 | 1377 | function lance_requete( |
| 1378 | - $method, |
|
| 1379 | - $scheme, |
|
| 1380 | - $user, |
|
| 1381 | - $host, |
|
| 1382 | - $path, |
|
| 1383 | - $port, |
|
| 1384 | - $noproxy, |
|
| 1385 | - $refuse_gz = false, |
|
| 1386 | - $referer = '', |
|
| 1387 | - $datas = '', |
|
| 1388 | - $vers = 'HTTP/1.0', |
|
| 1389 | - $date = '' |
|
| 1378 | + $method, |
|
| 1379 | + $scheme, |
|
| 1380 | + $user, |
|
| 1381 | + $host, |
|
| 1382 | + $path, |
|
| 1383 | + $port, |
|
| 1384 | + $noproxy, |
|
| 1385 | + $refuse_gz = false, |
|
| 1386 | + $referer = '', |
|
| 1387 | + $datas = '', |
|
| 1388 | + $vers = 'HTTP/1.0', |
|
| 1389 | + $date = '' |
|
| 1390 | 1390 | ) { |
| 1391 | 1391 | |
| 1392 | - $proxy_user = ''; |
|
| 1393 | - $http_proxy = need_proxy($host); |
|
| 1394 | - if ($user) { |
|
| 1395 | - $user = urlencode((string) $user[0]) . ':' . urlencode((string) $user[1]); |
|
| 1396 | - } |
|
| 1397 | - |
|
| 1398 | - $connect = ''; |
|
| 1399 | - if ($http_proxy) { |
|
| 1400 | - if (!defined('_PROXY_HTTPS_NOT_VIA_CONNECT') && in_array($scheme, ['tls','ssl'])) { |
|
| 1401 | - $path_host = ($user ? "$user@" : '') . $host . (($port != 80) ? ":$port" : ''); |
|
| 1402 | - $connect = 'CONNECT ' . $path_host . " $vers\r\n" |
|
| 1403 | - . "Host: $path_host\r\n" |
|
| 1404 | - . "Proxy-Connection: Keep-Alive\r\n"; |
|
| 1405 | - } else { |
|
| 1406 | - $path = (in_array($scheme, ['tls','ssl']) ? 'https://' : "$scheme://") |
|
| 1407 | - . ($user ? "$user@" : '') |
|
| 1408 | - . "$host" . (($port != 80) ? ":$port" : '') . $path; |
|
| 1409 | - } |
|
| 1410 | - $t2 = @parse_url($http_proxy); |
|
| 1411 | - $first_host = $t2['host']; |
|
| 1412 | - $first_port = ($t2['port'] ?? null) ?: 80; |
|
| 1413 | - if ($t2['user'] ?? null) { |
|
| 1414 | - $proxy_user = base64_encode($t2['user'] . ':' . $t2['pass']); |
|
| 1415 | - } |
|
| 1416 | - } else { |
|
| 1417 | - $first_host = $noproxy . $host; |
|
| 1418 | - $first_port = $port; |
|
| 1419 | - } |
|
| 1420 | - |
|
| 1421 | - $logger = spip_logger('distant'); |
|
| 1422 | - $logger_connect = spip_logger('connect'); |
|
| 1423 | - if ($connect) { |
|
| 1424 | - $streamContext = stream_context_create([ |
|
| 1425 | - 'ssl' => [ |
|
| 1426 | - 'verify_peer' => false, |
|
| 1427 | - 'allow_self_signed' => true, |
|
| 1428 | - 'SNI_enabled' => true, |
|
| 1429 | - 'peer_name' => $host, |
|
| 1430 | - ] |
|
| 1431 | - ]); |
|
| 1432 | - $f = @stream_socket_client( |
|
| 1433 | - "tcp://$first_host:$first_port", |
|
| 1434 | - $errno, |
|
| 1435 | - $errstr, |
|
| 1436 | - _INC_DISTANT_CONNECT_TIMEOUT, |
|
| 1437 | - STREAM_CLIENT_CONNECT, |
|
| 1438 | - $streamContext |
|
| 1439 | - ); |
|
| 1440 | - $logger_connect->info("Recuperer $path sur $first_host:$first_port par $f (via CONNECT)"); |
|
| 1441 | - if (!$f) { |
|
| 1442 | - $logger->error("Erreur connexion $errno $errstr"); |
|
| 1443 | - return $errno; |
|
| 1444 | - } |
|
| 1445 | - stream_set_timeout($f, _INC_DISTANT_CONNECT_TIMEOUT); |
|
| 1446 | - |
|
| 1447 | - fwrite($f, $connect); |
|
| 1448 | - fwrite($f, "\r\n"); |
|
| 1449 | - $res = fread($f, 1024); |
|
| 1450 | - if ( |
|
| 1451 | - !$res |
|
| 1452 | - || ($res = explode(' ', $res)) === [] |
|
| 1453 | - || $res[1] !== '200' |
|
| 1454 | - ) { |
|
| 1455 | - $logger_connect->notice("Echec CONNECT sur $first_host:$first_port"); |
|
| 1456 | - fclose($f); |
|
| 1457 | - |
|
| 1458 | - return false; |
|
| 1459 | - } |
|
| 1460 | - // important, car sinon on lit trop vite et les donnees ne sont pas encore dispo |
|
| 1461 | - stream_set_blocking($f, true); |
|
| 1462 | - // envoyer le handshake |
|
| 1463 | - stream_socket_enable_crypto($f, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT); |
|
| 1464 | - $logger_connect->info("OK CONNECT sur $first_host:$first_port"); |
|
| 1465 | - } else { |
|
| 1466 | - $ntry = 3; |
|
| 1467 | - do { |
|
| 1468 | - $f = @fsockopen($first_host, $first_port, $errno, $errstr, _INC_DISTANT_CONNECT_TIMEOUT); |
|
| 1469 | - } while (!$f && $ntry-- && $errno !== 110 && sleep(1)); |
|
| 1470 | - $logger->info("Recuperer $path sur $first_host:$first_port par $f"); |
|
| 1471 | - if (!$f) { |
|
| 1472 | - $logger->error("Erreur connexion $errno $errstr"); |
|
| 1473 | - |
|
| 1474 | - return $errno; |
|
| 1475 | - } |
|
| 1476 | - stream_set_timeout($f, _INC_DISTANT_CONNECT_TIMEOUT); |
|
| 1477 | - } |
|
| 1478 | - |
|
| 1479 | - $site = $GLOBALS['meta']['adresse_site'] ?? ''; |
|
| 1480 | - |
|
| 1481 | - $host_port = $host; |
|
| 1482 | - if ($port != (in_array($scheme, ['tls','ssl']) ? 443 : 80)) { |
|
| 1483 | - $host_port .= ":$port"; |
|
| 1484 | - } |
|
| 1485 | - $req = "$method $path $vers\r\n" |
|
| 1486 | - . "Host: $host_port\r\n" |
|
| 1487 | - . 'User-Agent: ' . _INC_DISTANT_USER_AGENT . "\r\n" |
|
| 1488 | - . ($refuse_gz ? '' : ('Accept-Encoding: ' . _INC_DISTANT_CONTENT_ENCODING . "\r\n")) |
|
| 1489 | - . ($site ? "Referer: $site/$referer\r\n" : '') |
|
| 1490 | - . ($date ? 'If-Modified-Since: ' . (gmdate('D, d M Y H:i:s', $date) . " GMT\r\n") : '') |
|
| 1491 | - . ($user ? 'Authorization: Basic ' . base64_encode(urldecode($user)) . "\r\n" : '') |
|
| 1492 | - . ($proxy_user ? "Proxy-Authorization: Basic $proxy_user\r\n" : '') |
|
| 1493 | - . (strpos($vers, '1.1') ? "Keep-Alive: 300\r\nConnection: keep-alive\r\n" : ''); |
|
| 1392 | + $proxy_user = ''; |
|
| 1393 | + $http_proxy = need_proxy($host); |
|
| 1394 | + if ($user) { |
|
| 1395 | + $user = urlencode((string) $user[0]) . ':' . urlencode((string) $user[1]); |
|
| 1396 | + } |
|
| 1397 | + |
|
| 1398 | + $connect = ''; |
|
| 1399 | + if ($http_proxy) { |
|
| 1400 | + if (!defined('_PROXY_HTTPS_NOT_VIA_CONNECT') && in_array($scheme, ['tls','ssl'])) { |
|
| 1401 | + $path_host = ($user ? "$user@" : '') . $host . (($port != 80) ? ":$port" : ''); |
|
| 1402 | + $connect = 'CONNECT ' . $path_host . " $vers\r\n" |
|
| 1403 | + . "Host: $path_host\r\n" |
|
| 1404 | + . "Proxy-Connection: Keep-Alive\r\n"; |
|
| 1405 | + } else { |
|
| 1406 | + $path = (in_array($scheme, ['tls','ssl']) ? 'https://' : "$scheme://") |
|
| 1407 | + . ($user ? "$user@" : '') |
|
| 1408 | + . "$host" . (($port != 80) ? ":$port" : '') . $path; |
|
| 1409 | + } |
|
| 1410 | + $t2 = @parse_url($http_proxy); |
|
| 1411 | + $first_host = $t2['host']; |
|
| 1412 | + $first_port = ($t2['port'] ?? null) ?: 80; |
|
| 1413 | + if ($t2['user'] ?? null) { |
|
| 1414 | + $proxy_user = base64_encode($t2['user'] . ':' . $t2['pass']); |
|
| 1415 | + } |
|
| 1416 | + } else { |
|
| 1417 | + $first_host = $noproxy . $host; |
|
| 1418 | + $first_port = $port; |
|
| 1419 | + } |
|
| 1420 | + |
|
| 1421 | + $logger = spip_logger('distant'); |
|
| 1422 | + $logger_connect = spip_logger('connect'); |
|
| 1423 | + if ($connect) { |
|
| 1424 | + $streamContext = stream_context_create([ |
|
| 1425 | + 'ssl' => [ |
|
| 1426 | + 'verify_peer' => false, |
|
| 1427 | + 'allow_self_signed' => true, |
|
| 1428 | + 'SNI_enabled' => true, |
|
| 1429 | + 'peer_name' => $host, |
|
| 1430 | + ] |
|
| 1431 | + ]); |
|
| 1432 | + $f = @stream_socket_client( |
|
| 1433 | + "tcp://$first_host:$first_port", |
|
| 1434 | + $errno, |
|
| 1435 | + $errstr, |
|
| 1436 | + _INC_DISTANT_CONNECT_TIMEOUT, |
|
| 1437 | + STREAM_CLIENT_CONNECT, |
|
| 1438 | + $streamContext |
|
| 1439 | + ); |
|
| 1440 | + $logger_connect->info("Recuperer $path sur $first_host:$first_port par $f (via CONNECT)"); |
|
| 1441 | + if (!$f) { |
|
| 1442 | + $logger->error("Erreur connexion $errno $errstr"); |
|
| 1443 | + return $errno; |
|
| 1444 | + } |
|
| 1445 | + stream_set_timeout($f, _INC_DISTANT_CONNECT_TIMEOUT); |
|
| 1446 | + |
|
| 1447 | + fwrite($f, $connect); |
|
| 1448 | + fwrite($f, "\r\n"); |
|
| 1449 | + $res = fread($f, 1024); |
|
| 1450 | + if ( |
|
| 1451 | + !$res |
|
| 1452 | + || ($res = explode(' ', $res)) === [] |
|
| 1453 | + || $res[1] !== '200' |
|
| 1454 | + ) { |
|
| 1455 | + $logger_connect->notice("Echec CONNECT sur $first_host:$first_port"); |
|
| 1456 | + fclose($f); |
|
| 1457 | + |
|
| 1458 | + return false; |
|
| 1459 | + } |
|
| 1460 | + // important, car sinon on lit trop vite et les donnees ne sont pas encore dispo |
|
| 1461 | + stream_set_blocking($f, true); |
|
| 1462 | + // envoyer le handshake |
|
| 1463 | + stream_socket_enable_crypto($f, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT); |
|
| 1464 | + $logger_connect->info("OK CONNECT sur $first_host:$first_port"); |
|
| 1465 | + } else { |
|
| 1466 | + $ntry = 3; |
|
| 1467 | + do { |
|
| 1468 | + $f = @fsockopen($first_host, $first_port, $errno, $errstr, _INC_DISTANT_CONNECT_TIMEOUT); |
|
| 1469 | + } while (!$f && $ntry-- && $errno !== 110 && sleep(1)); |
|
| 1470 | + $logger->info("Recuperer $path sur $first_host:$first_port par $f"); |
|
| 1471 | + if (!$f) { |
|
| 1472 | + $logger->error("Erreur connexion $errno $errstr"); |
|
| 1473 | + |
|
| 1474 | + return $errno; |
|
| 1475 | + } |
|
| 1476 | + stream_set_timeout($f, _INC_DISTANT_CONNECT_TIMEOUT); |
|
| 1477 | + } |
|
| 1478 | + |
|
| 1479 | + $site = $GLOBALS['meta']['adresse_site'] ?? ''; |
|
| 1480 | + |
|
| 1481 | + $host_port = $host; |
|
| 1482 | + if ($port != (in_array($scheme, ['tls','ssl']) ? 443 : 80)) { |
|
| 1483 | + $host_port .= ":$port"; |
|
| 1484 | + } |
|
| 1485 | + $req = "$method $path $vers\r\n" |
|
| 1486 | + . "Host: $host_port\r\n" |
|
| 1487 | + . 'User-Agent: ' . _INC_DISTANT_USER_AGENT . "\r\n" |
|
| 1488 | + . ($refuse_gz ? '' : ('Accept-Encoding: ' . _INC_DISTANT_CONTENT_ENCODING . "\r\n")) |
|
| 1489 | + . ($site ? "Referer: $site/$referer\r\n" : '') |
|
| 1490 | + . ($date ? 'If-Modified-Since: ' . (gmdate('D, d M Y H:i:s', $date) . " GMT\r\n") : '') |
|
| 1491 | + . ($user ? 'Authorization: Basic ' . base64_encode(urldecode($user)) . "\r\n" : '') |
|
| 1492 | + . ($proxy_user ? "Proxy-Authorization: Basic $proxy_user\r\n" : '') |
|
| 1493 | + . (strpos($vers, '1.1') ? "Keep-Alive: 300\r\nConnection: keep-alive\r\n" : ''); |
|
| 1494 | 1494 | |
| 1495 | 1495 | # $logger->info("Requete\n$req"); |
| 1496 | - fwrite($f, $req); |
|
| 1497 | - fwrite($f, $datas ?: "\r\n"); |
|
| 1496 | + fwrite($f, $req); |
|
| 1497 | + fwrite($f, $datas ?: "\r\n"); |
|
| 1498 | 1498 | |
| 1499 | - return $f; |
|
| 1499 | + return $f; |
|
| 1500 | 1500 | } |
@@ -27,7 +27,7 @@ discard block |
||
| 27 | 27 | define('_INC_DISTANT_CONTENT_ENCODING', 'gzip'); |
| 28 | 28 | } |
| 29 | 29 | if (!defined('_INC_DISTANT_USER_AGENT')) { |
| 30 | - define('_INC_DISTANT_USER_AGENT', 'SPIP-' . $GLOBALS['spip_version_affichee'] . ' (' . $GLOBALS['home_server'] . ')'); |
|
| 30 | + define('_INC_DISTANT_USER_AGENT', 'SPIP-'.$GLOBALS['spip_version_affichee'].' ('.$GLOBALS['home_server'].')'); |
|
| 31 | 31 | } |
| 32 | 32 | if (!defined('_INC_DISTANT_MAX_SIZE')) { |
| 33 | 33 | define('_INC_DISTANT_MAX_SIZE', 2_097_152); |
@@ -36,7 +36,7 @@ discard block |
||
| 36 | 36 | define('_INC_DISTANT_CONNECT_TIMEOUT', 10); |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | -define('_REGEXP_COPIE_LOCALE', ',' . |
|
| 39 | +define('_REGEXP_COPIE_LOCALE', ','. |
|
| 40 | 40 | preg_replace( |
| 41 | 41 | '@^https?:@', |
| 42 | 42 | 'https?:', |
@@ -73,7 +73,7 @@ discard block |
||
| 73 | 73 | |
| 74 | 74 | // si c'est la protection de soi-meme, retourner le path |
| 75 | 75 | if ($mode !== 'force' && preg_match(_REGEXP_COPIE_LOCALE, $source, $match)) { |
| 76 | - $source = substr((string) _DIR_IMG, strlen((string) _DIR_RACINE)) . urldecode($match[1]); |
|
| 76 | + $source = substr((string) _DIR_IMG, strlen((string) _DIR_RACINE)).urldecode($match[1]); |
|
| 77 | 77 | |
| 78 | 78 | return @file_exists($source) ? $source : false; |
| 79 | 79 | } |
@@ -93,7 +93,7 @@ discard block |
||
| 93 | 93 | return false; |
| 94 | 94 | } |
| 95 | 95 | |
| 96 | - $localrac = _DIR_RACINE . $local; |
|
| 96 | + $localrac = _DIR_RACINE.$local; |
|
| 97 | 97 | $t = ($mode === 'force') ? false : @file_exists($localrac); |
| 98 | 98 | |
| 99 | 99 | // test d'existence du fichier |
@@ -113,7 +113,7 @@ discard block |
||
| 113 | 113 | if (!$taille_max) { |
| 114 | 114 | $taille_max = _COPIE_LOCALE_MAX_SIZE; |
| 115 | 115 | } |
| 116 | - $localrac_tmp = $localrac . '.tmp'; |
|
| 116 | + $localrac_tmp = $localrac.'.tmp'; |
|
| 117 | 117 | $res = recuperer_url( |
| 118 | 118 | $source, |
| 119 | 119 | ['file' => $localrac_tmp, 'taille_max' => $taille_max, 'if_modified_since' => $t ? filemtime($localrac) : ''] |
@@ -121,10 +121,10 @@ discard block |
||
| 121 | 121 | |
| 122 | 122 | $logger = spip_logger('distant'); |
| 123 | 123 | if (!$res || !$res['length'] && $res['status'] != 304) { |
| 124 | - $logger->notice("copie_locale : Echec recuperation $source sur $localrac_tmp status : " . ($res ? $res['status'] : '-')); |
|
| 124 | + $logger->notice("copie_locale : Echec recuperation $source sur $localrac_tmp status : ".($res ? $res['status'] : '-')); |
|
| 125 | 125 | @unlink($localrac_tmp); |
| 126 | 126 | } else { |
| 127 | - $logger->info("copie_locale : recuperation $source sur $localrac_tmp OK | taille " . $res['length'] . ' status ' . $res['status']); |
|
| 127 | + $logger->info("copie_locale : recuperation $source sur $localrac_tmp OK | taille ".$res['length'].' status '.$res['status']); |
|
| 128 | 128 | } |
| 129 | 129 | if (!$res || !$res['length']) { |
| 130 | 130 | // si $t c'est sans doute juste un not-modified-since |
@@ -137,7 +137,7 @@ discard block |
||
| 137 | 137 | && is_callable($callback_valider_url) |
| 138 | 138 | && !$callback_valider_url($res['url']) |
| 139 | 139 | ) { |
| 140 | - $logger->notice('copie_locale : url finale ' . $res['url'] . " non valide, on refuse le fichier $localrac_tmp"); |
|
| 140 | + $logger->notice('copie_locale : url finale '.$res['url']." non valide, on refuse le fichier $localrac_tmp"); |
|
| 141 | 141 | @unlink($localrac_tmp); |
| 142 | 142 | return $t ? $local : false; |
| 143 | 143 | } |
@@ -227,7 +227,7 @@ discard block |
||
| 227 | 227 | |
| 228 | 228 | if (!$is_known_host) { |
| 229 | 229 | $host = trim($parsed_url['host'], '.'); |
| 230 | - if (! $ip = filter_var($host, FILTER_VALIDATE_IP)) { |
|
| 230 | + if (!$ip = filter_var($host, FILTER_VALIDATE_IP)) { |
|
| 231 | 231 | $ip = gethostbyname($host); |
| 232 | 232 | if ($ip === $host) { |
| 233 | 233 | // Error condition for gethostbyname() |
@@ -247,7 +247,7 @@ discard block |
||
| 247 | 247 | $ip = false; |
| 248 | 248 | } |
| 249 | 249 | } |
| 250 | - if ($ip && ! filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) { |
|
| 250 | + if ($ip && !filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) { |
|
| 251 | 251 | return false; |
| 252 | 252 | } |
| 253 | 253 | } |
@@ -328,7 +328,7 @@ discard block |
||
| 328 | 328 | } |
| 329 | 329 | } |
| 330 | 330 | if ($taille > 500) { |
| 331 | - $boundary = substr(md5(random_int(0, mt_getrandmax()) . 'spip'), 0, 8); |
|
| 331 | + $boundary = substr(md5(random_int(0, mt_getrandmax()).'spip'), 0, 8); |
|
| 332 | 332 | } |
| 333 | 333 | } |
| 334 | 334 | |
@@ -361,10 +361,10 @@ discard block |
||
| 361 | 361 | foreach ($donnees as $cle => $valeur) { |
| 362 | 362 | if (is_array($valeur)) { |
| 363 | 363 | foreach ($valeur as $val2) { |
| 364 | - $chaines[] = rawurlencode($cle) . '[]=' . rawurlencode((string) $val2); |
|
| 364 | + $chaines[] = rawurlencode($cle).'[]='.rawurlencode((string) $val2); |
|
| 365 | 365 | } |
| 366 | 366 | } else { |
| 367 | - $chaines[] = rawurlencode($cle) . '=' . rawurlencode((string) $valeur); |
|
| 367 | + $chaines[] = rawurlencode($cle).'='.rawurlencode((string) $valeur); |
|
| 368 | 368 | } |
| 369 | 369 | } |
| 370 | 370 | $chaine = implode('&', $chaines); |
@@ -468,13 +468,13 @@ discard block |
||
| 468 | 468 | } |
| 469 | 469 | |
| 470 | 470 | $logger = spip_logger('distant'); |
| 471 | - $logger->debug('recuperer_url ' . $options['methode'] . " sur $url"); |
|
| 471 | + $logger->debug('recuperer_url '.$options['methode']." sur $url"); |
|
| 472 | 472 | |
| 473 | 473 | // Ajout des en-têtes spécifiques si besoin |
| 474 | 474 | $formatted_data = ''; |
| 475 | 475 | if (!empty($options['headers'])) { |
| 476 | 476 | foreach ($options['headers'] as $champ => $valeur) { |
| 477 | - $formatted_data .= $champ . ': ' . $valeur . "\r\n"; |
|
| 477 | + $formatted_data .= $champ.': '.$valeur."\r\n"; |
|
| 478 | 478 | } |
| 479 | 479 | } |
| 480 | 480 | |
@@ -482,9 +482,9 @@ discard block |
||
| 482 | 482 | [$head, $postdata] = prepare_donnees_post($options['datas'], $options['boundary']); |
| 483 | 483 | $head .= $formatted_data; |
| 484 | 484 | if (stripos($head, 'Content-Length:') === false) { |
| 485 | - $head .= 'Content-Length: ' . strlen((string) $postdata) . "\r\n"; |
|
| 485 | + $head .= 'Content-Length: '.strlen((string) $postdata)."\r\n"; |
|
| 486 | 486 | } |
| 487 | - $formatted_data = $head . "\r\n" . $postdata; |
|
| 487 | + $formatted_data = $head."\r\n".$postdata; |
|
| 488 | 488 | if ( |
| 489 | 489 | strlen((string) $postdata) && !$methode_demandee |
| 490 | 490 | ) { |
@@ -497,9 +497,9 @@ discard block |
||
| 497 | 497 | // Accepter les URLs au format feed:// ou qui ont oublie le http:// ou les urls relatives au protocole |
| 498 | 498 | $url = preg_replace(',^feed://,i', 'http://', $url); |
| 499 | 499 | if (!tester_url_absolue($url)) { |
| 500 | - $url = 'http://' . $url; |
|
| 500 | + $url = 'http://'.$url; |
|
| 501 | 501 | } elseif (str_starts_with($url, '//')) { |
| 502 | - $url = 'http:' . $url; |
|
| 502 | + $url = 'http:'.$url; |
|
| 503 | 503 | } |
| 504 | 504 | |
| 505 | 505 | $url = url_to_ascii($url); |
@@ -575,11 +575,11 @@ discard block |
||
| 575 | 575 | $options['methode'] = 'GET'; |
| 576 | 576 | $options['datas'] = ''; |
| 577 | 577 | } |
| 578 | - $logger->debug('recuperer_url recommence ' . $options['methode'] . " sur $url"); |
|
| 578 | + $logger->debug('recuperer_url recommence '.$options['methode']." sur $url"); |
|
| 579 | 579 | |
| 580 | 580 | return recuperer_url($url, $options); |
| 581 | 581 | } elseif ($res['status'] !== 200) { |
| 582 | - $logger->info('HTTP status ' . $res['status'] . " pour $url"); |
|
| 582 | + $logger->info('HTTP status '.$res['status']." pour $url"); |
|
| 583 | 583 | } |
| 584 | 584 | $result['status'] = $res['status']; |
| 585 | 585 | if (isset($res['headers'])) { |
@@ -598,7 +598,7 @@ discard block |
||
| 598 | 598 | |
| 599 | 599 | // on ne veut que les entetes |
| 600 | 600 | if (!$options['taille_max'] || $options['methode'] == 'HEAD' || $result['status'] == '304') { |
| 601 | - $logger->debug('RESULTAT recuperer_url ' . $options['methode'] . " sur $url : " . json_encode($result, JSON_THROW_ON_ERROR)); |
|
| 601 | + $logger->debug('RESULTAT recuperer_url '.$options['methode']." sur $url : ".json_encode($result, JSON_THROW_ON_ERROR)); |
|
| 602 | 602 | return $result; |
| 603 | 603 | } |
| 604 | 604 | |
@@ -608,7 +608,7 @@ discard block |
||
| 608 | 608 | |
| 609 | 609 | $gz = false; |
| 610 | 610 | if (preg_match(",\bContent-Encoding: .*gzip,is", (string) $result['headers'])) { |
| 611 | - $gz = (_DIR_TMP . md5(uniqid(random_int(0, mt_getrandmax()))) . '.tmp.gz'); |
|
| 611 | + $gz = (_DIR_TMP.md5(uniqid(random_int(0, mt_getrandmax()))).'.tmp.gz'); |
|
| 612 | 612 | } |
| 613 | 613 | |
| 614 | 614 | // si on n'a pas deja récupéré le contenu par une methode detournée |
@@ -657,10 +657,10 @@ discard block |
||
| 657 | 657 | $trace = json_decode(json_encode($result, JSON_THROW_ON_ERROR), true, 512, JSON_THROW_ON_ERROR); |
| 658 | 658 | } catch (JsonException $e) { |
| 659 | 659 | $trace = []; |
| 660 | - $logger->error('Failed to parse Json data : ' . $e->getMessage()); |
|
| 660 | + $logger->error('Failed to parse Json data : '.$e->getMessage()); |
|
| 661 | 661 | } |
| 662 | 662 | $trace['page'] = '...'; |
| 663 | - $logger->debug('RESULTAT recuperer_url ' . $options['methode'] . " sur $url : " . json_encode($trace, JSON_THROW_ON_ERROR)); |
|
| 663 | + $logger->debug('RESULTAT recuperer_url '.$options['methode']." sur $url : ".json_encode($trace, JSON_THROW_ON_ERROR)); |
|
| 664 | 664 | |
| 665 | 665 | return $result; |
| 666 | 666 | } |
@@ -714,7 +714,7 @@ discard block |
||
| 714 | 714 | $sig['url'] = $url; |
| 715 | 715 | |
| 716 | 716 | $dir = sous_repertoire(_DIR_CACHE, 'curl'); |
| 717 | - $cache = md5(serialize($sig)) . '-' . substr(preg_replace(',\W+,', '_', $url), 0, 80); |
|
| 717 | + $cache = md5(serialize($sig)).'-'.substr(preg_replace(',\W+,', '_', $url), 0, 80); |
|
| 718 | 718 | $sub = sous_repertoire($dir, substr($cache, 0, 2)); |
| 719 | 719 | $cache = "$sub$cache"; |
| 720 | 720 | |
@@ -768,7 +768,7 @@ discard block |
||
| 768 | 768 | $fp = false; |
| 769 | 769 | if ($fichier) { |
| 770 | 770 | include_spip('inc/acces'); |
| 771 | - $tmpfile = "$fichier." . creer_uniqid() . '.tmp'; |
|
| 771 | + $tmpfile = "$fichier.".creer_uniqid().'.tmp'; |
|
| 772 | 772 | $fp = spip_fopen_lock($tmpfile, 'w', LOCK_EX); |
| 773 | 773 | if (!$fp && file_exists($fichier)) { |
| 774 | 774 | return filesize($fichier); |
@@ -841,11 +841,11 @@ discard block |
||
| 841 | 841 | } |
| 842 | 842 | $result['status'] = (int) $r[1]; |
| 843 | 843 | while ($s = trim(fgets($handle, 16384))) { |
| 844 | - $result['headers'][] = $s . "\n"; |
|
| 844 | + $result['headers'][] = $s."\n"; |
|
| 845 | 845 | preg_match(',^([^:]*): *(.*)$,i', $s, $r); |
| 846 | 846 | [, $d, $v] = $r; |
| 847 | 847 | $d = strtolower(trim($d)); |
| 848 | - if ( $d === 'location' && $result['status'] >= 300 && $result['status'] < 400) { |
|
| 848 | + if ($d === 'location' && $result['status'] >= 300 && $result['status'] < 400) { |
|
| 849 | 849 | $result['location'] = $v; |
| 850 | 850 | } elseif ($d === 'last-modified') { |
| 851 | 851 | $result['last_modified'] = strtotime($v); |
@@ -893,7 +893,7 @@ discard block |
||
| 893 | 893 | |
| 894 | 894 | // on se place tout le temps comme si on était a la racine |
| 895 | 895 | if (_DIR_RACINE) { |
| 896 | - $d = preg_replace(',^' . preg_quote((string) _DIR_RACINE, ',') . ',', '', (string) $d); |
|
| 896 | + $d = preg_replace(',^'.preg_quote((string) _DIR_RACINE, ',').',', '', (string) $d); |
|
| 897 | 897 | } |
| 898 | 898 | |
| 899 | 899 | $m = md5($source); |
@@ -901,18 +901,18 @@ discard block |
||
| 901 | 901 | $filename = |
| 902 | 902 | $d |
| 903 | 903 | . substr(preg_replace(',[^\w-],', '', basename($source, $extension)), 0, 16) |
| 904 | - . '-' . substr($m, 0, 8) |
|
| 904 | + . '-'.substr($m, 0, 8) |
|
| 905 | 905 | . ".$extension"; |
| 906 | 906 | |
| 907 | 907 | // ancien nommage des fichiers distants : renommer le fichier a la volee si besoin pour eviter de dupliquer les caches |
| 908 | 908 | $legacy_filename = |
| 909 | 909 | $d |
| 910 | - . substr(preg_replace(',[^\w-],', '', basename($source)) . '-' . $m, 0, 12) |
|
| 910 | + . substr(preg_replace(',[^\w-],', '', basename($source)).'-'.$m, 0, 12) |
|
| 911 | 911 | . substr($m, 0, 4) |
| 912 | 912 | . ".$extension"; |
| 913 | 913 | |
| 914 | - if (file_exists(_DIR_RACINE . $legacy_filename)) { |
|
| 915 | - @rename(_DIR_RACINE . $legacy_filename, $filename); |
|
| 914 | + if (file_exists(_DIR_RACINE.$legacy_filename)) { |
|
| 915 | + @rename(_DIR_RACINE.$legacy_filename, $filename); |
|
| 916 | 916 | } |
| 917 | 917 | |
| 918 | 918 | return $filename; |
@@ -937,7 +937,7 @@ discard block |
||
| 937 | 937 | // Si c'est deja local pas de souci |
| 938 | 938 | if (!tester_url_absolue($source)) { |
| 939 | 939 | if (_DIR_RACINE) { |
| 940 | - $source = preg_replace(',^' . preg_quote((string) _DIR_RACINE, ',') . ',', '', $source); |
|
| 940 | + $source = preg_replace(',^'.preg_quote((string) _DIR_RACINE, ',').',', '', $source); |
|
| 941 | 941 | } |
| 942 | 942 | |
| 943 | 943 | return $source; |
@@ -955,7 +955,7 @@ discard block |
||
| 955 | 955 | $ext |
| 956 | 956 | && preg_match(',^\w+$,', $ext) |
| 957 | 957 | && ($f = nom_fichier_copie_locale($source, $ext)) |
| 958 | - && file_exists(_DIR_RACINE . $f) |
|
| 958 | + && file_exists(_DIR_RACINE.$f) |
|
| 959 | 959 | ) { |
| 960 | 960 | return $f; |
| 961 | 961 | } |
@@ -963,7 +963,7 @@ discard block |
||
| 963 | 963 | |
| 964 | 964 | // Si c'est deja dans la table des documents, |
| 965 | 965 | // ramener le nom de sa copie potentielle |
| 966 | - $ext = sql_getfetsel('extension', 'spip_documents', 'fichier=' . sql_quote($source) . " AND distant='oui' AND extension <> ''"); |
|
| 966 | + $ext = sql_getfetsel('extension', 'spip_documents', 'fichier='.sql_quote($source)." AND distant='oui' AND extension <> ''"); |
|
| 967 | 967 | |
| 968 | 968 | if ($ext) { |
| 969 | 969 | return nom_fichier_copie_locale($source, $ext); |
@@ -974,9 +974,9 @@ discard block |
||
| 974 | 974 | |
| 975 | 975 | $ext = $path_parts ? $path_parts['extension'] : ''; |
| 976 | 976 | |
| 977 | - if ($ext && sql_getfetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($ext))) { |
|
| 977 | + if ($ext && sql_getfetsel('extension', 'spip_types_documents', 'extension='.sql_quote($ext))) { |
|
| 978 | 978 | $f = nom_fichier_copie_locale($source, $ext); |
| 979 | - if (file_exists(_DIR_RACINE . $f)) { |
|
| 979 | + if (file_exists(_DIR_RACINE.$f)) { |
|
| 980 | 980 | return $f; |
| 981 | 981 | } |
| 982 | 982 | } |
@@ -984,7 +984,7 @@ discard block |
||
| 984 | 984 | // Ping pour voir si son extension est connue et autorisee |
| 985 | 985 | // avec mise en cache du resultat du ping |
| 986 | 986 | |
| 987 | - $cache = sous_repertoire(_DIR_CACHE, 'rid') . md5($source); |
|
| 987 | + $cache = sous_repertoire(_DIR_CACHE, 'rid').md5($source); |
|
| 988 | 988 | if ( |
| 989 | 989 | !@file_exists($cache) |
| 990 | 990 | || !($path_parts = @unserialize(spip_file_get_contents($cache))) |
@@ -994,7 +994,7 @@ discard block |
||
| 994 | 994 | ecrire_fichier($cache, serialize($path_parts)); |
| 995 | 995 | } |
| 996 | 996 | $ext = empty($path_parts['extension']) ? '' : $path_parts['extension']; |
| 997 | - if ($ext && sql_getfetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($ext))) { |
|
| 997 | + if ($ext && sql_getfetsel('extension', 'spip_types_documents', 'extension='.sql_quote($ext))) { |
|
| 998 | 998 | return nom_fichier_copie_locale($source, $ext); |
| 999 | 999 | } |
| 1000 | 1000 | |
@@ -1100,7 +1100,7 @@ discard block |
||
| 1100 | 1100 | } else { |
| 1101 | 1101 | if ($a['body']) { |
| 1102 | 1102 | $a['extension'] = corriger_extension($extension); |
| 1103 | - $a['fichier'] = _DIR_RACINE . nom_fichier_copie_locale($source, $extension); |
|
| 1103 | + $a['fichier'] = _DIR_RACINE.nom_fichier_copie_locale($source, $extension); |
|
| 1104 | 1104 | ecrire_fichier($a['fichier'], $a['body']); |
| 1105 | 1105 | $size_image = @spip_getimagesize($a['fichier']); |
| 1106 | 1106 | $a['largeur'] = (int) $size_image[0]; |
@@ -1172,20 +1172,20 @@ discard block |
||
| 1172 | 1172 | $t = null; |
| 1173 | 1173 | if (in_array($mime_type, ['text/plain', '', 'application/octet-stream'])) { |
| 1174 | 1174 | if (!$t && preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $source, $rext)) { |
| 1175 | - $t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote(corriger_extension($rext[1]), '', 'text')); |
|
| 1175 | + $t = sql_fetsel('extension', 'spip_types_documents', 'extension='.sql_quote(corriger_extension($rext[1]), '', 'text')); |
|
| 1176 | 1176 | } |
| 1177 | 1177 | if ( |
| 1178 | 1178 | !$t |
| 1179 | 1179 | && preg_match(',^Content-Disposition:\s*attachment;\s*filename=(.*)$,Uims', $headers, $m) |
| 1180 | 1180 | && preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $m[1], $rext) |
| 1181 | 1181 | ) { |
| 1182 | - $t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote(corriger_extension($rext[1]), '', 'text')); |
|
| 1182 | + $t = sql_fetsel('extension', 'spip_types_documents', 'extension='.sql_quote(corriger_extension($rext[1]), '', 'text')); |
|
| 1183 | 1183 | } |
| 1184 | 1184 | } |
| 1185 | 1185 | |
| 1186 | 1186 | // Autre mime/type (ou text/plain avec fichier d'extension inconnue) |
| 1187 | 1187 | if (!$t) { |
| 1188 | - $t = sql_fetsel('extension', 'spip_types_documents', 'mime_type=' . sql_quote($mime_type)); |
|
| 1188 | + $t = sql_fetsel('extension', 'spip_types_documents', 'mime_type='.sql_quote($mime_type)); |
|
| 1189 | 1189 | } |
| 1190 | 1190 | |
| 1191 | 1191 | // Toujours rien ? (ex: audio/x-ogg au lieu de application/ogg) |
@@ -1196,11 +1196,11 @@ discard block |
||
| 1196 | 1196 | && preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $source, $rext) |
| 1197 | 1197 | ) { |
| 1198 | 1198 | # eviter xxx.3 => 3gp (> SPIP 3) |
| 1199 | - $t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote(corriger_extension($rext[1]), '', 'text')); |
|
| 1199 | + $t = sql_fetsel('extension', 'spip_types_documents', 'extension='.sql_quote(corriger_extension($rext[1]), '', 'text')); |
|
| 1200 | 1200 | } |
| 1201 | 1201 | |
| 1202 | 1202 | if ($t) { |
| 1203 | - spip_logger('distant')->info("mime-type $mime_type ok, extension " . $t['extension']); |
|
| 1203 | + spip_logger('distant')->info("mime-type $mime_type ok, extension ".$t['extension']); |
|
| 1204 | 1204 | return $t['extension']; |
| 1205 | 1205 | } else { |
| 1206 | 1206 | # par defaut on retombe sur '.bin' si c'est autorise |
@@ -1303,7 +1303,7 @@ discard block |
||
| 1303 | 1303 | } |
| 1304 | 1304 | } else { |
| 1305 | 1305 | $scheme = $t['scheme']; |
| 1306 | - $noproxy = $scheme . '://'; |
|
| 1306 | + $noproxy = $scheme.'://'; |
|
| 1307 | 1307 | } |
| 1308 | 1308 | if (isset($t['user'])) { |
| 1309 | 1309 | // user et pass doivent être passés en urlencodé dans l'URL, on redecode ici |
@@ -1318,7 +1318,7 @@ discard block |
||
| 1318 | 1318 | } |
| 1319 | 1319 | |
| 1320 | 1320 | if (!empty($t['query'])) { |
| 1321 | - $path .= '?' . $t['query']; |
|
| 1321 | + $path .= '?'.$t['query']; |
|
| 1322 | 1322 | } |
| 1323 | 1323 | |
| 1324 | 1324 | $f = lance_requete($method, $scheme, $user, $host, $path, $port, $noproxy, $refuse_gz, $referer, $datas, $vers, $date); |
@@ -1392,29 +1392,29 @@ discard block |
||
| 1392 | 1392 | $proxy_user = ''; |
| 1393 | 1393 | $http_proxy = need_proxy($host); |
| 1394 | 1394 | if ($user) { |
| 1395 | - $user = urlencode((string) $user[0]) . ':' . urlencode((string) $user[1]); |
|
| 1395 | + $user = urlencode((string) $user[0]).':'.urlencode((string) $user[1]); |
|
| 1396 | 1396 | } |
| 1397 | 1397 | |
| 1398 | 1398 | $connect = ''; |
| 1399 | 1399 | if ($http_proxy) { |
| 1400 | - if (!defined('_PROXY_HTTPS_NOT_VIA_CONNECT') && in_array($scheme, ['tls','ssl'])) { |
|
| 1401 | - $path_host = ($user ? "$user@" : '') . $host . (($port != 80) ? ":$port" : ''); |
|
| 1402 | - $connect = 'CONNECT ' . $path_host . " $vers\r\n" |
|
| 1400 | + if (!defined('_PROXY_HTTPS_NOT_VIA_CONNECT') && in_array($scheme, ['tls', 'ssl'])) { |
|
| 1401 | + $path_host = ($user ? "$user@" : '').$host.(($port != 80) ? ":$port" : ''); |
|
| 1402 | + $connect = 'CONNECT '.$path_host." $vers\r\n" |
|
| 1403 | 1403 | . "Host: $path_host\r\n" |
| 1404 | 1404 | . "Proxy-Connection: Keep-Alive\r\n"; |
| 1405 | 1405 | } else { |
| 1406 | - $path = (in_array($scheme, ['tls','ssl']) ? 'https://' : "$scheme://") |
|
| 1406 | + $path = (in_array($scheme, ['tls', 'ssl']) ? 'https://' : "$scheme://") |
|
| 1407 | 1407 | . ($user ? "$user@" : '') |
| 1408 | - . "$host" . (($port != 80) ? ":$port" : '') . $path; |
|
| 1408 | + . "$host".(($port != 80) ? ":$port" : '').$path; |
|
| 1409 | 1409 | } |
| 1410 | 1410 | $t2 = @parse_url($http_proxy); |
| 1411 | 1411 | $first_host = $t2['host']; |
| 1412 | 1412 | $first_port = ($t2['port'] ?? null) ?: 80; |
| 1413 | 1413 | if ($t2['user'] ?? null) { |
| 1414 | - $proxy_user = base64_encode($t2['user'] . ':' . $t2['pass']); |
|
| 1414 | + $proxy_user = base64_encode($t2['user'].':'.$t2['pass']); |
|
| 1415 | 1415 | } |
| 1416 | 1416 | } else { |
| 1417 | - $first_host = $noproxy . $host; |
|
| 1417 | + $first_host = $noproxy.$host; |
|
| 1418 | 1418 | $first_port = $port; |
| 1419 | 1419 | } |
| 1420 | 1420 | |
@@ -1479,16 +1479,16 @@ discard block |
||
| 1479 | 1479 | $site = $GLOBALS['meta']['adresse_site'] ?? ''; |
| 1480 | 1480 | |
| 1481 | 1481 | $host_port = $host; |
| 1482 | - if ($port != (in_array($scheme, ['tls','ssl']) ? 443 : 80)) { |
|
| 1482 | + if ($port != (in_array($scheme, ['tls', 'ssl']) ? 443 : 80)) { |
|
| 1483 | 1483 | $host_port .= ":$port"; |
| 1484 | 1484 | } |
| 1485 | 1485 | $req = "$method $path $vers\r\n" |
| 1486 | 1486 | . "Host: $host_port\r\n" |
| 1487 | - . 'User-Agent: ' . _INC_DISTANT_USER_AGENT . "\r\n" |
|
| 1488 | - . ($refuse_gz ? '' : ('Accept-Encoding: ' . _INC_DISTANT_CONTENT_ENCODING . "\r\n")) |
|
| 1487 | + . 'User-Agent: '._INC_DISTANT_USER_AGENT."\r\n" |
|
| 1488 | + . ($refuse_gz ? '' : ('Accept-Encoding: '._INC_DISTANT_CONTENT_ENCODING."\r\n")) |
|
| 1489 | 1489 | . ($site ? "Referer: $site/$referer\r\n" : '') |
| 1490 | - . ($date ? 'If-Modified-Since: ' . (gmdate('D, d M Y H:i:s', $date) . " GMT\r\n") : '') |
|
| 1491 | - . ($user ? 'Authorization: Basic ' . base64_encode(urldecode($user)) . "\r\n" : '') |
|
| 1490 | + . ($date ? 'If-Modified-Since: '.(gmdate('D, d M Y H:i:s', $date)." GMT\r\n") : '') |
|
| 1491 | + . ($user ? 'Authorization: Basic '.base64_encode(urldecode($user))."\r\n" : '') |
|
| 1492 | 1492 | . ($proxy_user ? "Proxy-Authorization: Basic $proxy_user\r\n" : '') |
| 1493 | 1493 | . (strpos($vers, '1.1') ? "Keep-Alive: 300\r\nConnection: keep-alive\r\n" : ''); |
| 1494 | 1494 | |
@@ -11,35 +11,35 @@ |
||
| 11 | 11 | \***************************************************************************/ |
| 12 | 12 | |
| 13 | 13 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 14 | - return; |
|
| 14 | + return; |
|
| 15 | 15 | } |
| 16 | 16 | |
| 17 | 17 | /** |
| 18 | 18 | * @deprecated 5.0 Utiliser spip_logger() |
| 19 | 19 | */ |
| 20 | 20 | function inc_log_dist($message, $logname = null, $logdir = null, $logsuf = null) { |
| 21 | - trigger_deprecation( |
|
| 22 | - 'spip', |
|
| 23 | - '5.0', |
|
| 24 | - 'La fonction inc_log_dist() est déprécée.' . |
|
| 25 | - ' Utilisez spip_logger() à la place.' |
|
| 26 | - ); |
|
| 27 | - static $pre = [ |
|
| 28 | - 'HS:' => LogLevel::EMERGENCY, |
|
| 29 | - 'ALERTE:' => LogLevel::ALERT, |
|
| 30 | - 'CRITIQUE:' => LogLevel::CRITICAL, |
|
| 31 | - 'ERREUR:' => LogLevel::ERROR, |
|
| 32 | - 'WARNING:' => LogLevel::WARNING, |
|
| 33 | - '!INFO:' => LogLevel::NOTICE, |
|
| 34 | - 'info:' => LogLevel::INFO, |
|
| 35 | - 'debug:' => LogLevel::DEBUG, |
|
| 36 | - ]; |
|
| 37 | - foreach ($pre as $start => $level) { |
|
| 38 | - if (str_starts_with($message, $start)) { |
|
| 39 | - spip_logger($logname)->log($level, ltrim(substr($message, strlen($start)))); |
|
| 40 | - return; |
|
| 41 | - } |
|
| 42 | - } |
|
| 43 | - spip_logger($logname)->info($message); |
|
| 44 | - return; |
|
| 21 | + trigger_deprecation( |
|
| 22 | + 'spip', |
|
| 23 | + '5.0', |
|
| 24 | + 'La fonction inc_log_dist() est déprécée.' . |
|
| 25 | + ' Utilisez spip_logger() à la place.' |
|
| 26 | + ); |
|
| 27 | + static $pre = [ |
|
| 28 | + 'HS:' => LogLevel::EMERGENCY, |
|
| 29 | + 'ALERTE:' => LogLevel::ALERT, |
|
| 30 | + 'CRITIQUE:' => LogLevel::CRITICAL, |
|
| 31 | + 'ERREUR:' => LogLevel::ERROR, |
|
| 32 | + 'WARNING:' => LogLevel::WARNING, |
|
| 33 | + '!INFO:' => LogLevel::NOTICE, |
|
| 34 | + 'info:' => LogLevel::INFO, |
|
| 35 | + 'debug:' => LogLevel::DEBUG, |
|
| 36 | + ]; |
|
| 37 | + foreach ($pre as $start => $level) { |
|
| 38 | + if (str_starts_with($message, $start)) { |
|
| 39 | + spip_logger($logname)->log($level, ltrim(substr($message, strlen($start)))); |
|
| 40 | + return; |
|
| 41 | + } |
|
| 42 | + } |
|
| 43 | + spip_logger($logname)->info($message); |
|
| 44 | + return; |
|
| 45 | 45 | } |
@@ -21,7 +21,7 @@ |
||
| 21 | 21 | trigger_deprecation( |
| 22 | 22 | 'spip', |
| 23 | 23 | '5.0', |
| 24 | - 'La fonction inc_log_dist() est déprécée.' . |
|
| 24 | + 'La fonction inc_log_dist() est déprécée.'. |
|
| 25 | 25 | ' Utilisez spip_logger() à la place.' |
| 26 | 26 | ); |
| 27 | 27 | static $pre = [ |
@@ -18,7 +18,7 @@ discard block |
||
| 18 | 18 | */ |
| 19 | 19 | |
| 20 | 20 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 21 | - return; |
|
| 21 | + return; |
|
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | |
@@ -39,15 +39,15 @@ discard block |
||
| 39 | 39 | * @return bool|null|void |
| 40 | 40 | */ |
| 41 | 41 | function inc_session_dist($auteur = false) { |
| 42 | - if (is_numeric($auteur)) { |
|
| 43 | - return supprimer_sessions($auteur, $auteur > 0); |
|
| 44 | - } else { |
|
| 45 | - if (is_array($auteur)) { |
|
| 46 | - return ajouter_session($auteur); |
|
| 47 | - } else { |
|
| 48 | - return verifier_session($auteur); |
|
| 49 | - } |
|
| 50 | - } |
|
| 42 | + if (is_numeric($auteur)) { |
|
| 43 | + return supprimer_sessions($auteur, $auteur > 0); |
|
| 44 | + } else { |
|
| 45 | + if (is_array($auteur)) { |
|
| 46 | + return ajouter_session($auteur); |
|
| 47 | + } else { |
|
| 48 | + return verifier_session($auteur); |
|
| 49 | + } |
|
| 50 | + } |
|
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | |
@@ -76,37 +76,37 @@ discard block |
||
| 76 | 76 | */ |
| 77 | 77 | function supprimer_sessions($id_auteur, $toutes = true, $actives = true) { |
| 78 | 78 | |
| 79 | - $nb_files = 0; |
|
| 80 | - $nb_max_files = (defined('_MAX_NB_SESSIONS_OUVERTES') ? _MAX_NB_SESSIONS_OUVERTES : 1000); |
|
| 81 | - spip_logger('session')->info("supprimer sessions auteur $id_auteur"); |
|
| 82 | - if ($toutes || $id_auteur !== $GLOBALS['visiteur_session']['id_auteur']) { |
|
| 83 | - if ($dir = opendir(_DIR_SESSIONS)) { |
|
| 84 | - $t = $_SERVER['REQUEST_TIME'] - (4 * _RENOUVELLE_ALEA); // 48h par defaut |
|
| 85 | - $t_short = $_SERVER['REQUEST_TIME'] - max(_RENOUVELLE_ALEA / 4, 3 * 3600); // 3h par defaut |
|
| 86 | - while (($f = readdir($dir)) !== false) { |
|
| 87 | - $nb_files++; |
|
| 88 | - if (preg_match(',^[^\d-]*(-?\d+)_\w{32}\.php[3]?$,', $f, $regs)) { |
|
| 89 | - $f = _DIR_SESSIONS . $f; |
|
| 90 | - if ($actives && $regs[1] == $id_auteur || $t > filemtime($f)) { |
|
| 91 | - spip_unlink($f); |
|
| 92 | - } |
|
| 93 | - // si il y a trop de sessions ouvertes, on purge les sessions anonymes de plus de 3H |
|
| 94 | - // cf http://core.spip.org/issues/3276 |
|
| 95 | - elseif ($nb_files > $nb_max_files && !intval($regs[1]) && $t_short > filemtime($f)) { |
|
| 96 | - spip_unlink($f); |
|
| 97 | - } |
|
| 98 | - } |
|
| 99 | - } |
|
| 100 | - } |
|
| 101 | - } else { |
|
| 102 | - verifier_session(); |
|
| 103 | - if ($cookie = lire_cookie_session()) { |
|
| 104 | - spip_unlink(chemin_fichier_session('alea_ephemere', $cookie, true)); |
|
| 105 | - } |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - // forcer le recalcul de la session courante |
|
| 109 | - spip_session(true); |
|
| 79 | + $nb_files = 0; |
|
| 80 | + $nb_max_files = (defined('_MAX_NB_SESSIONS_OUVERTES') ? _MAX_NB_SESSIONS_OUVERTES : 1000); |
|
| 81 | + spip_logger('session')->info("supprimer sessions auteur $id_auteur"); |
|
| 82 | + if ($toutes || $id_auteur !== $GLOBALS['visiteur_session']['id_auteur']) { |
|
| 83 | + if ($dir = opendir(_DIR_SESSIONS)) { |
|
| 84 | + $t = $_SERVER['REQUEST_TIME'] - (4 * _RENOUVELLE_ALEA); // 48h par defaut |
|
| 85 | + $t_short = $_SERVER['REQUEST_TIME'] - max(_RENOUVELLE_ALEA / 4, 3 * 3600); // 3h par defaut |
|
| 86 | + while (($f = readdir($dir)) !== false) { |
|
| 87 | + $nb_files++; |
|
| 88 | + if (preg_match(',^[^\d-]*(-?\d+)_\w{32}\.php[3]?$,', $f, $regs)) { |
|
| 89 | + $f = _DIR_SESSIONS . $f; |
|
| 90 | + if ($actives && $regs[1] == $id_auteur || $t > filemtime($f)) { |
|
| 91 | + spip_unlink($f); |
|
| 92 | + } |
|
| 93 | + // si il y a trop de sessions ouvertes, on purge les sessions anonymes de plus de 3H |
|
| 94 | + // cf http://core.spip.org/issues/3276 |
|
| 95 | + elseif ($nb_files > $nb_max_files && !intval($regs[1]) && $t_short > filemtime($f)) { |
|
| 96 | + spip_unlink($f); |
|
| 97 | + } |
|
| 98 | + } |
|
| 99 | + } |
|
| 100 | + } |
|
| 101 | + } else { |
|
| 102 | + verifier_session(); |
|
| 103 | + if ($cookie = lire_cookie_session()) { |
|
| 104 | + spip_unlink(chemin_fichier_session('alea_ephemere', $cookie, true)); |
|
| 105 | + } |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + // forcer le recalcul de la session courante |
|
| 109 | + spip_session(true); |
|
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | /** |
@@ -124,124 +124,124 @@ discard block |
||
| 124 | 124 | * @return bool|string |
| 125 | 125 | */ |
| 126 | 126 | function ajouter_session($auteur) { |
| 127 | - // Si le client a deja une session valide pour son id_auteur |
|
| 128 | - // on conserve le meme fichier |
|
| 129 | - |
|
| 130 | - // Attention un visiteur peut avoir une session et un id=0, |
|
| 131 | - // => ne pas melanger les sessions des differents visiteurs |
|
| 132 | - $id_auteur = isset($auteur['id_auteur']) ? intval($auteur['id_auteur']) : 0; |
|
| 133 | - |
|
| 134 | - // Si ce n'est pas un inscrit (les inscrits ont toujours des choses en session) |
|
| 135 | - // on va vérifier s'il y a vraiment des choses à écrire |
|
| 136 | - if (!$id_auteur) { |
|
| 137 | - // On supprime les données de base pour voir le contenu réel de la session |
|
| 138 | - $auteur_verif = $auteur; |
|
| 139 | - if (isset($auteur_verif['id_auteur'])) { |
|
| 140 | - unset($auteur_verif['id_auteur']); |
|
| 141 | - } |
|
| 142 | - if (isset($auteur_verif['hash_env'])) { |
|
| 143 | - unset($auteur_verif['hash_env']); |
|
| 144 | - } |
|
| 145 | - if (isset($auteur_verif['ip_change'])) { |
|
| 146 | - unset($auteur_verif['ip_change']); |
|
| 147 | - } |
|
| 148 | - if (isset($auteur_verif['date_session'])) { |
|
| 149 | - unset($auteur_verif['date_session']); |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - // Les variables vraiment nulle ne sont pas à prendre en compte non plus |
|
| 153 | - foreach ($auteur_verif as $variable => $valeur) { |
|
| 154 | - if ($valeur === null) { |
|
| 155 | - unset($auteur_verif[$variable]); |
|
| 156 | - } |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - // Si après ça la session est vide alors on supprime l'éventuel fichier et on arrête là |
|
| 160 | - if (!$auteur_verif) { |
|
| 161 | - if ($cookie = lire_cookie_session()) { |
|
| 162 | - if (isset($_SESSION[$cookie])) { |
|
| 163 | - unset($_SESSION[$cookie]); |
|
| 164 | - } |
|
| 165 | - unset($_COOKIE['spip_session']); |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - return false; |
|
| 169 | - } |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - if ( |
|
| 173 | - (!$cookie = lire_cookie_session()) |
|
| 174 | - || intval($cookie) !== $id_auteur |
|
| 175 | - ) { |
|
| 176 | - $cookie = $id_auteur . '_' . md5(uniqid(random_int(0, mt_getrandmax()), true)); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - // Maintenant on sait qu'on a des choses à écrire |
|
| 180 | - // On s'assure d'avoir au moins ces valeurs |
|
| 181 | - $auteur['id_auteur'] = $id_auteur; |
|
| 182 | - if (!isset($auteur['hash_env'])) { |
|
| 183 | - $auteur['hash_env'] = hash_env(); |
|
| 184 | - } |
|
| 185 | - if (!isset($auteur['ip_change'])) { |
|
| 186 | - $auteur['ip_change'] = false; |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - if (!isset($auteur['date_session'])) { |
|
| 190 | - $auteur['date_session'] = time(); |
|
| 191 | - } |
|
| 192 | - if ( |
|
| 193 | - isset($auteur['prefs']) |
|
| 194 | - && is_string($auteur['prefs']) |
|
| 195 | - && ($prefs = @unserialize($auteur['prefs'])) |
|
| 196 | - ) { |
|
| 197 | - $auteur['prefs'] = $prefs; |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - $fichier_session = ''; |
|
| 201 | - |
|
| 202 | - // les sessions anonymes sont stockees dans $_SESSION |
|
| 203 | - if (!$id_auteur) { |
|
| 204 | - spip_php_session_start(); |
|
| 205 | - $_SESSION[$cookie] = preparer_ecriture_session($auteur); |
|
| 206 | - } else { |
|
| 207 | - $fichier_session = chemin_fichier_session('alea_ephemere', $cookie); |
|
| 208 | - if (!ecrire_fichier_session($fichier_session, $auteur)) { |
|
| 209 | - spip_logger('session')->emergency('Echec ecriture fichier session ' . $fichier_session); |
|
| 210 | - include_spip('inc/minipres'); |
|
| 211 | - echo minipres(); |
|
| 212 | - exit; |
|
| 213 | - } |
|
| 214 | - // verifier et limiter le nombre maxi de sessions |
|
| 215 | - // https://core.spip.net/issues/3807 |
|
| 216 | - lister_sessions_auteur($id_auteur); |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - // poser le cookie de session SPIP |
|
| 220 | - include_spip('inc/cookie'); |
|
| 221 | - $duree = definir_duree_cookie_session($auteur); |
|
| 222 | - $cookie = set_cookie_session($cookie, time() + $duree); |
|
| 223 | - spip_logger('session')->info("ajoute session $fichier_session cookie $duree"); |
|
| 224 | - |
|
| 225 | - // Si on est admin, poser le cookie de correspondance |
|
| 226 | - if (!function_exists('autoriser')) { |
|
| 227 | - include_spip('inc/autoriser'); |
|
| 228 | - } |
|
| 229 | - if (autoriser('ecrire', '', '', $auteur) && _DUREE_COOKIE_ADMIN) { |
|
| 230 | - spip_setcookie( |
|
| 231 | - 'spip_admin', |
|
| 232 | - '@' . ($auteur['email'] ?: $auteur['login']), |
|
| 233 | - time() + max(_DUREE_COOKIE_ADMIN, $duree), |
|
| 234 | - httponly: true |
|
| 235 | - ); |
|
| 236 | - } else { |
|
| 237 | - // sinon le supprimer ... |
|
| 238 | - spip_setcookie('spip_admin', '', 1, httponly: true); |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - # on en profite pour purger les vieilles sessions anonymes abandonnees |
|
| 242 | - # supprimer_sessions(0, true, false); |
|
| 243 | - |
|
| 244 | - return $cookie; |
|
| 127 | + // Si le client a deja une session valide pour son id_auteur |
|
| 128 | + // on conserve le meme fichier |
|
| 129 | + |
|
| 130 | + // Attention un visiteur peut avoir une session et un id=0, |
|
| 131 | + // => ne pas melanger les sessions des differents visiteurs |
|
| 132 | + $id_auteur = isset($auteur['id_auteur']) ? intval($auteur['id_auteur']) : 0; |
|
| 133 | + |
|
| 134 | + // Si ce n'est pas un inscrit (les inscrits ont toujours des choses en session) |
|
| 135 | + // on va vérifier s'il y a vraiment des choses à écrire |
|
| 136 | + if (!$id_auteur) { |
|
| 137 | + // On supprime les données de base pour voir le contenu réel de la session |
|
| 138 | + $auteur_verif = $auteur; |
|
| 139 | + if (isset($auteur_verif['id_auteur'])) { |
|
| 140 | + unset($auteur_verif['id_auteur']); |
|
| 141 | + } |
|
| 142 | + if (isset($auteur_verif['hash_env'])) { |
|
| 143 | + unset($auteur_verif['hash_env']); |
|
| 144 | + } |
|
| 145 | + if (isset($auteur_verif['ip_change'])) { |
|
| 146 | + unset($auteur_verif['ip_change']); |
|
| 147 | + } |
|
| 148 | + if (isset($auteur_verif['date_session'])) { |
|
| 149 | + unset($auteur_verif['date_session']); |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + // Les variables vraiment nulle ne sont pas à prendre en compte non plus |
|
| 153 | + foreach ($auteur_verif as $variable => $valeur) { |
|
| 154 | + if ($valeur === null) { |
|
| 155 | + unset($auteur_verif[$variable]); |
|
| 156 | + } |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + // Si après ça la session est vide alors on supprime l'éventuel fichier et on arrête là |
|
| 160 | + if (!$auteur_verif) { |
|
| 161 | + if ($cookie = lire_cookie_session()) { |
|
| 162 | + if (isset($_SESSION[$cookie])) { |
|
| 163 | + unset($_SESSION[$cookie]); |
|
| 164 | + } |
|
| 165 | + unset($_COOKIE['spip_session']); |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + return false; |
|
| 169 | + } |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + if ( |
|
| 173 | + (!$cookie = lire_cookie_session()) |
|
| 174 | + || intval($cookie) !== $id_auteur |
|
| 175 | + ) { |
|
| 176 | + $cookie = $id_auteur . '_' . md5(uniqid(random_int(0, mt_getrandmax()), true)); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + // Maintenant on sait qu'on a des choses à écrire |
|
| 180 | + // On s'assure d'avoir au moins ces valeurs |
|
| 181 | + $auteur['id_auteur'] = $id_auteur; |
|
| 182 | + if (!isset($auteur['hash_env'])) { |
|
| 183 | + $auteur['hash_env'] = hash_env(); |
|
| 184 | + } |
|
| 185 | + if (!isset($auteur['ip_change'])) { |
|
| 186 | + $auteur['ip_change'] = false; |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + if (!isset($auteur['date_session'])) { |
|
| 190 | + $auteur['date_session'] = time(); |
|
| 191 | + } |
|
| 192 | + if ( |
|
| 193 | + isset($auteur['prefs']) |
|
| 194 | + && is_string($auteur['prefs']) |
|
| 195 | + && ($prefs = @unserialize($auteur['prefs'])) |
|
| 196 | + ) { |
|
| 197 | + $auteur['prefs'] = $prefs; |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + $fichier_session = ''; |
|
| 201 | + |
|
| 202 | + // les sessions anonymes sont stockees dans $_SESSION |
|
| 203 | + if (!$id_auteur) { |
|
| 204 | + spip_php_session_start(); |
|
| 205 | + $_SESSION[$cookie] = preparer_ecriture_session($auteur); |
|
| 206 | + } else { |
|
| 207 | + $fichier_session = chemin_fichier_session('alea_ephemere', $cookie); |
|
| 208 | + if (!ecrire_fichier_session($fichier_session, $auteur)) { |
|
| 209 | + spip_logger('session')->emergency('Echec ecriture fichier session ' . $fichier_session); |
|
| 210 | + include_spip('inc/minipres'); |
|
| 211 | + echo minipres(); |
|
| 212 | + exit; |
|
| 213 | + } |
|
| 214 | + // verifier et limiter le nombre maxi de sessions |
|
| 215 | + // https://core.spip.net/issues/3807 |
|
| 216 | + lister_sessions_auteur($id_auteur); |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + // poser le cookie de session SPIP |
|
| 220 | + include_spip('inc/cookie'); |
|
| 221 | + $duree = definir_duree_cookie_session($auteur); |
|
| 222 | + $cookie = set_cookie_session($cookie, time() + $duree); |
|
| 223 | + spip_logger('session')->info("ajoute session $fichier_session cookie $duree"); |
|
| 224 | + |
|
| 225 | + // Si on est admin, poser le cookie de correspondance |
|
| 226 | + if (!function_exists('autoriser')) { |
|
| 227 | + include_spip('inc/autoriser'); |
|
| 228 | + } |
|
| 229 | + if (autoriser('ecrire', '', '', $auteur) && _DUREE_COOKIE_ADMIN) { |
|
| 230 | + spip_setcookie( |
|
| 231 | + 'spip_admin', |
|
| 232 | + '@' . ($auteur['email'] ?: $auteur['login']), |
|
| 233 | + time() + max(_DUREE_COOKIE_ADMIN, $duree), |
|
| 234 | + httponly: true |
|
| 235 | + ); |
|
| 236 | + } else { |
|
| 237 | + // sinon le supprimer ... |
|
| 238 | + spip_setcookie('spip_admin', '', 1, httponly: true); |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + # on en profite pour purger les vieilles sessions anonymes abandonnees |
|
| 242 | + # supprimer_sessions(0, true, false); |
|
| 243 | + |
|
| 244 | + return $cookie; |
|
| 245 | 245 | } |
| 246 | 246 | |
| 247 | 247 | /** |
@@ -261,54 +261,54 @@ discard block |
||
| 261 | 261 | * Durée en secondes |
| 262 | 262 | **/ |
| 263 | 263 | function definir_duree_cookie_session($auteur) { |
| 264 | - $coef = 2; |
|
| 265 | - if (isset($auteur['cookie'])) { |
|
| 266 | - if (is_numeric($auteur['cookie'])) { |
|
| 267 | - $coef = $auteur['cookie']; |
|
| 268 | - } else { |
|
| 269 | - $coef = 20; |
|
| 270 | - } |
|
| 271 | - } |
|
| 272 | - return (int)(_RENOUVELLE_ALEA * $coef); |
|
| 264 | + $coef = 2; |
|
| 265 | + if (isset($auteur['cookie'])) { |
|
| 266 | + if (is_numeric($auteur['cookie'])) { |
|
| 267 | + $coef = $auteur['cookie']; |
|
| 268 | + } else { |
|
| 269 | + $coef = 20; |
|
| 270 | + } |
|
| 271 | + } |
|
| 272 | + return (int)(_RENOUVELLE_ALEA * $coef); |
|
| 273 | 273 | } |
| 274 | 274 | |
| 275 | 275 | /** |
| 276 | 276 | * Lire le cookie de session et le valider de façon centralisée |
| 277 | 277 | */ |
| 278 | 278 | function lire_cookie_session(bool $accepter_test = false): ?string { |
| 279 | - static $cookie_valide = []; |
|
| 280 | - // pas de cookie ? |
|
| 281 | - if (!isset($_COOKIE['spip_session'])) { |
|
| 282 | - return null; |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - if (array_key_exists($_COOKIE['spip_session'], $cookie_valide)) { |
|
| 286 | - return $cookie_valide[$_COOKIE['spip_session']]; |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - if ($accepter_test && $_COOKIE['spip_session'] === 'test_echec_cookie') { |
|
| 290 | - return 'test_echec_cookie'; |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - if (!preg_match(",^\d+_[0-9a-f]{32}$,", $_COOKIE['spip_session'])) { |
|
| 294 | - // cookie invalide ? |
|
| 295 | - effacer_cookie_session(); |
|
| 296 | - return null; |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - // ok |
|
| 300 | - $cookie_valide[$_COOKIE['spip_session']] = $_COOKIE['spip_session']; |
|
| 301 | - |
|
| 302 | - return $_COOKIE['spip_session']; |
|
| 279 | + static $cookie_valide = []; |
|
| 280 | + // pas de cookie ? |
|
| 281 | + if (!isset($_COOKIE['spip_session'])) { |
|
| 282 | + return null; |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + if (array_key_exists($_COOKIE['spip_session'], $cookie_valide)) { |
|
| 286 | + return $cookie_valide[$_COOKIE['spip_session']]; |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + if ($accepter_test && $_COOKIE['spip_session'] === 'test_echec_cookie') { |
|
| 290 | + return 'test_echec_cookie'; |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + if (!preg_match(",^\d+_[0-9a-f]{32}$,", $_COOKIE['spip_session'])) { |
|
| 294 | + // cookie invalide ? |
|
| 295 | + effacer_cookie_session(); |
|
| 296 | + return null; |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + // ok |
|
| 300 | + $cookie_valide[$_COOKIE['spip_session']] = $_COOKIE['spip_session']; |
|
| 301 | + |
|
| 302 | + return $_COOKIE['spip_session']; |
|
| 303 | 303 | } |
| 304 | 304 | |
| 305 | 305 | /** Annuler le cookie de session */ |
| 306 | 306 | function effacer_cookie_session(): void { |
| 307 | - // supprimer le cookie |
|
| 308 | - if (isset($_COOKIE['spip_session'])) { |
|
| 309 | - spip_setcookie('spip_session', '', time() - 24 * 3600, httponly: true); |
|
| 310 | - unset($_COOKIE['spip_session']); |
|
| 311 | - } |
|
| 307 | + // supprimer le cookie |
|
| 308 | + if (isset($_COOKIE['spip_session'])) { |
|
| 309 | + spip_setcookie('spip_session', '', time() - 24 * 3600, httponly: true); |
|
| 310 | + unset($_COOKIE['spip_session']); |
|
| 311 | + } |
|
| 312 | 312 | } |
| 313 | 313 | |
| 314 | 314 | /** |
@@ -320,20 +320,20 @@ discard block |
||
| 320 | 320 | * timestamp d'expiration |
| 321 | 321 | */ |
| 322 | 322 | function set_cookie_session(?string $valeur_cookie = null, int $expires = 0): ?string { |
| 323 | - if ($valeur_cookie !== null) { |
|
| 324 | - // vérifié par lire_cookie_session() |
|
| 325 | - $_COOKIE['spip_session'] = $valeur_cookie; |
|
| 326 | - } |
|
| 323 | + if ($valeur_cookie !== null) { |
|
| 324 | + // vérifié par lire_cookie_session() |
|
| 325 | + $_COOKIE['spip_session'] = $valeur_cookie; |
|
| 326 | + } |
|
| 327 | 327 | |
| 328 | - $valeur_cookie = lire_cookie_session(); |
|
| 328 | + $valeur_cookie = lire_cookie_session(); |
|
| 329 | 329 | |
| 330 | - if ($valeur_cookie === null) { |
|
| 331 | - effacer_cookie_session(); |
|
| 332 | - } else { |
|
| 333 | - spip_setcookie('spip_session', $valeur_cookie, $expires, httponly: true); |
|
| 334 | - } |
|
| 330 | + if ($valeur_cookie === null) { |
|
| 331 | + effacer_cookie_session(); |
|
| 332 | + } else { |
|
| 333 | + spip_setcookie('spip_session', $valeur_cookie, $expires, httponly: true); |
|
| 334 | + } |
|
| 335 | 335 | |
| 336 | - return $valeur_cookie; |
|
| 336 | + return $valeur_cookie; |
|
| 337 | 337 | } |
| 338 | 338 | |
| 339 | 339 | /** |
@@ -353,91 +353,91 @@ discard block |
||
| 353 | 353 | * @return bool|int|null |
| 354 | 354 | */ |
| 355 | 355 | function verifier_session($change = false) { |
| 356 | - // si pas de cookie, c'est fichu |
|
| 357 | - if (!$cookie = lire_cookie_session()) { |
|
| 358 | - return false; |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - $fichier_session = ''; |
|
| 362 | - |
|
| 363 | - // est-ce une session anonyme ? |
|
| 364 | - if (!intval($cookie)) { |
|
| 365 | - spip_php_session_start(); |
|
| 366 | - if (!isset($_SESSION[$cookie]) || !is_array($_SESSION[$cookie])) { |
|
| 367 | - return false; |
|
| 368 | - } |
|
| 369 | - $GLOBALS['visiteur_session'] = $_SESSION[$cookie]; |
|
| 370 | - } else { |
|
| 371 | - // Tester avec alea courant |
|
| 372 | - $fichier_session = chemin_fichier_session('alea_ephemere', $cookie, true); |
|
| 373 | - if ($fichier_session && @file_exists($fichier_session)) { |
|
| 374 | - include($fichier_session); |
|
| 375 | - } else { |
|
| 376 | - // Sinon, tester avec alea precedent |
|
| 377 | - $fichier_session = chemin_fichier_session('alea_ephemere_ancien', $cookie, true); |
|
| 378 | - if (!$fichier_session || !@file_exists($fichier_session)) { |
|
| 379 | - return false; |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - // Renouveler la session avec l'alea courant |
|
| 383 | - include($fichier_session); |
|
| 384 | - spip_logger('session')->info('renouvelle session ' . $GLOBALS['visiteur_session']['id_auteur']); |
|
| 385 | - spip_unlink($fichier_session); |
|
| 386 | - ajouter_session($GLOBALS['visiteur_session']); |
|
| 387 | - } |
|
| 388 | - } |
|
| 389 | - |
|
| 390 | - // Compatibilite ascendante : auteur_session est visiteur_session si |
|
| 391 | - // c'est un auteur SPIP authentifie (tandis qu'un visiteur_session peut |
|
| 392 | - // n'etre qu'identifie, sans aucune authentification). |
|
| 393 | - |
|
| 394 | - if (isset($GLOBALS['visiteur_session']['id_auteur']) && $GLOBALS['visiteur_session']['id_auteur']) { |
|
| 395 | - $GLOBALS['auteur_session'] = &$GLOBALS['visiteur_session']; |
|
| 396 | - } |
|
| 397 | - |
|
| 398 | - |
|
| 399 | - // Si l'adresse IP change, inc/presentation mettra une balise image |
|
| 400 | - // avec un URL de rappel demandant a changer le nom de la session. |
|
| 401 | - // Seul celui qui a l'IP d'origine est rejoue |
|
| 402 | - // ainsi un eventuel voleur de cookie ne pourrait pas deconnecter |
|
| 403 | - // sa victime, mais se ferait deconnecter par elle. |
|
| 404 | - if (hash_env() != $GLOBALS['visiteur_session']['hash_env']) { |
|
| 405 | - if (!$GLOBALS['visiteur_session']['ip_change']) { |
|
| 406 | - define('_SESSION_REJOUER', true); |
|
| 407 | - $GLOBALS['visiteur_session']['ip_change'] = true; |
|
| 408 | - ajouter_session($GLOBALS['visiteur_session']); |
|
| 409 | - } else { |
|
| 410 | - if ($change) { |
|
| 411 | - spip_logger('session')->info('session non rejouee, vol de cookie ?'); |
|
| 412 | - } |
|
| 413 | - } |
|
| 414 | - } else { |
|
| 415 | - if ($change) { |
|
| 416 | - spip_logger('session')->info("rejoue session $fichier_session $cookie"); |
|
| 417 | - if ($fichier_session) { |
|
| 418 | - spip_unlink($fichier_session); |
|
| 419 | - } |
|
| 420 | - $GLOBALS['visiteur_session']['ip_change'] = false; |
|
| 421 | - unset($_COOKIE['spip_session']); |
|
| 422 | - ajouter_session($GLOBALS['visiteur_session']); |
|
| 423 | - } |
|
| 424 | - } |
|
| 425 | - |
|
| 426 | - // Si la session a ete initiee il y a trop longtemps, elle est annulee |
|
| 427 | - if ( |
|
| 428 | - isset($GLOBALS['visiteur_session']) |
|
| 429 | - && defined('_AGE_SESSION_MAX') |
|
| 430 | - && _AGE_SESSION_MAX > 0 |
|
| 431 | - && time() - @$GLOBALS['visiteur_session']['date_session'] > _AGE_SESSION_MAX |
|
| 432 | - ) { |
|
| 433 | - unset($GLOBALS['visiteur_session']); |
|
| 434 | - |
|
| 435 | - return false; |
|
| 436 | - } |
|
| 437 | - |
|
| 438 | - return is_numeric($GLOBALS['visiteur_session']['id_auteur']) |
|
| 439 | - ? $GLOBALS['visiteur_session']['id_auteur'] |
|
| 440 | - : null; |
|
| 356 | + // si pas de cookie, c'est fichu |
|
| 357 | + if (!$cookie = lire_cookie_session()) { |
|
| 358 | + return false; |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + $fichier_session = ''; |
|
| 362 | + |
|
| 363 | + // est-ce une session anonyme ? |
|
| 364 | + if (!intval($cookie)) { |
|
| 365 | + spip_php_session_start(); |
|
| 366 | + if (!isset($_SESSION[$cookie]) || !is_array($_SESSION[$cookie])) { |
|
| 367 | + return false; |
|
| 368 | + } |
|
| 369 | + $GLOBALS['visiteur_session'] = $_SESSION[$cookie]; |
|
| 370 | + } else { |
|
| 371 | + // Tester avec alea courant |
|
| 372 | + $fichier_session = chemin_fichier_session('alea_ephemere', $cookie, true); |
|
| 373 | + if ($fichier_session && @file_exists($fichier_session)) { |
|
| 374 | + include($fichier_session); |
|
| 375 | + } else { |
|
| 376 | + // Sinon, tester avec alea precedent |
|
| 377 | + $fichier_session = chemin_fichier_session('alea_ephemere_ancien', $cookie, true); |
|
| 378 | + if (!$fichier_session || !@file_exists($fichier_session)) { |
|
| 379 | + return false; |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + // Renouveler la session avec l'alea courant |
|
| 383 | + include($fichier_session); |
|
| 384 | + spip_logger('session')->info('renouvelle session ' . $GLOBALS['visiteur_session']['id_auteur']); |
|
| 385 | + spip_unlink($fichier_session); |
|
| 386 | + ajouter_session($GLOBALS['visiteur_session']); |
|
| 387 | + } |
|
| 388 | + } |
|
| 389 | + |
|
| 390 | + // Compatibilite ascendante : auteur_session est visiteur_session si |
|
| 391 | + // c'est un auteur SPIP authentifie (tandis qu'un visiteur_session peut |
|
| 392 | + // n'etre qu'identifie, sans aucune authentification). |
|
| 393 | + |
|
| 394 | + if (isset($GLOBALS['visiteur_session']['id_auteur']) && $GLOBALS['visiteur_session']['id_auteur']) { |
|
| 395 | + $GLOBALS['auteur_session'] = &$GLOBALS['visiteur_session']; |
|
| 396 | + } |
|
| 397 | + |
|
| 398 | + |
|
| 399 | + // Si l'adresse IP change, inc/presentation mettra une balise image |
|
| 400 | + // avec un URL de rappel demandant a changer le nom de la session. |
|
| 401 | + // Seul celui qui a l'IP d'origine est rejoue |
|
| 402 | + // ainsi un eventuel voleur de cookie ne pourrait pas deconnecter |
|
| 403 | + // sa victime, mais se ferait deconnecter par elle. |
|
| 404 | + if (hash_env() != $GLOBALS['visiteur_session']['hash_env']) { |
|
| 405 | + if (!$GLOBALS['visiteur_session']['ip_change']) { |
|
| 406 | + define('_SESSION_REJOUER', true); |
|
| 407 | + $GLOBALS['visiteur_session']['ip_change'] = true; |
|
| 408 | + ajouter_session($GLOBALS['visiteur_session']); |
|
| 409 | + } else { |
|
| 410 | + if ($change) { |
|
| 411 | + spip_logger('session')->info('session non rejouee, vol de cookie ?'); |
|
| 412 | + } |
|
| 413 | + } |
|
| 414 | + } else { |
|
| 415 | + if ($change) { |
|
| 416 | + spip_logger('session')->info("rejoue session $fichier_session $cookie"); |
|
| 417 | + if ($fichier_session) { |
|
| 418 | + spip_unlink($fichier_session); |
|
| 419 | + } |
|
| 420 | + $GLOBALS['visiteur_session']['ip_change'] = false; |
|
| 421 | + unset($_COOKIE['spip_session']); |
|
| 422 | + ajouter_session($GLOBALS['visiteur_session']); |
|
| 423 | + } |
|
| 424 | + } |
|
| 425 | + |
|
| 426 | + // Si la session a ete initiee il y a trop longtemps, elle est annulee |
|
| 427 | + if ( |
|
| 428 | + isset($GLOBALS['visiteur_session']) |
|
| 429 | + && defined('_AGE_SESSION_MAX') |
|
| 430 | + && _AGE_SESSION_MAX > 0 |
|
| 431 | + && time() - @$GLOBALS['visiteur_session']['date_session'] > _AGE_SESSION_MAX |
|
| 432 | + ) { |
|
| 433 | + unset($GLOBALS['visiteur_session']); |
|
| 434 | + |
|
| 435 | + return false; |
|
| 436 | + } |
|
| 437 | + |
|
| 438 | + return is_numeric($GLOBALS['visiteur_session']['id_auteur']) |
|
| 439 | + ? $GLOBALS['visiteur_session']['id_auteur'] |
|
| 440 | + : null; |
|
| 441 | 441 | } |
| 442 | 442 | |
| 443 | 443 | /** |
@@ -452,7 +452,7 @@ discard block |
||
| 452 | 452 | * Valeur, si trouvée, `null` sinon. |
| 453 | 453 | */ |
| 454 | 454 | function session_get($nom) { |
| 455 | - return $GLOBALS['visiteur_session'][$nom] ?? null; |
|
| 455 | + return $GLOBALS['visiteur_session'][$nom] ?? null; |
|
| 456 | 456 | } |
| 457 | 457 | |
| 458 | 458 | |
@@ -468,32 +468,32 @@ discard block |
||
| 468 | 468 | * @return void|array |
| 469 | 469 | */ |
| 470 | 470 | function session_set($nom, $val = null) { |
| 471 | - static $remove = []; |
|
| 472 | - static $actualiser_sessions = false; |
|
| 473 | - if ($nom === false) { |
|
| 474 | - return $remove; |
|
| 475 | - } |
|
| 476 | - if (is_null($val)) { |
|
| 477 | - // rien a faire |
|
| 478 | - if (!isset($GLOBALS['visiteur_session'][$nom])) { |
|
| 479 | - return; |
|
| 480 | - } |
|
| 481 | - unset($GLOBALS['visiteur_session'][$nom]); |
|
| 482 | - $remove[] = $nom; |
|
| 483 | - } else { |
|
| 484 | - // On ajoute la valeur dans la globale |
|
| 485 | - $GLOBALS['visiteur_session'][$nom] = $val; |
|
| 486 | - if ($remove) { |
|
| 487 | - $remove = array_diff($remove, [$nom]); |
|
| 488 | - } |
|
| 489 | - } |
|
| 490 | - if (!$actualiser_sessions) { |
|
| 491 | - // il faut creer la session si on en a pas, la premiere fois |
|
| 492 | - ajouter_session($GLOBALS['visiteur_session']); |
|
| 493 | - // in register la fonction qui mettra a jour toutes les sessions en fin de hit |
|
| 494 | - register_shutdown_function('terminer_actualiser_sessions'); |
|
| 495 | - $actualiser_sessions = true; |
|
| 496 | - } |
|
| 471 | + static $remove = []; |
|
| 472 | + static $actualiser_sessions = false; |
|
| 473 | + if ($nom === false) { |
|
| 474 | + return $remove; |
|
| 475 | + } |
|
| 476 | + if (is_null($val)) { |
|
| 477 | + // rien a faire |
|
| 478 | + if (!isset($GLOBALS['visiteur_session'][$nom])) { |
|
| 479 | + return; |
|
| 480 | + } |
|
| 481 | + unset($GLOBALS['visiteur_session'][$nom]); |
|
| 482 | + $remove[] = $nom; |
|
| 483 | + } else { |
|
| 484 | + // On ajoute la valeur dans la globale |
|
| 485 | + $GLOBALS['visiteur_session'][$nom] = $val; |
|
| 486 | + if ($remove) { |
|
| 487 | + $remove = array_diff($remove, [$nom]); |
|
| 488 | + } |
|
| 489 | + } |
|
| 490 | + if (!$actualiser_sessions) { |
|
| 491 | + // il faut creer la session si on en a pas, la premiere fois |
|
| 492 | + ajouter_session($GLOBALS['visiteur_session']); |
|
| 493 | + // in register la fonction qui mettra a jour toutes les sessions en fin de hit |
|
| 494 | + register_shutdown_function('terminer_actualiser_sessions'); |
|
| 495 | + $actualiser_sessions = true; |
|
| 496 | + } |
|
| 497 | 497 | } |
| 498 | 498 | |
| 499 | 499 | /** |
@@ -502,12 +502,12 @@ discard block |
||
| 502 | 502 | * @uses actualiser_sessions() |
| 503 | 503 | */ |
| 504 | 504 | function terminer_actualiser_sessions() { |
| 505 | - // se remettre dans le dossier de travail au cas ou Apache a change |
|
| 506 | - chdir(_ROOT_CWD); |
|
| 507 | - // recuperer les variables a effacer |
|
| 508 | - $remove = session_set(false); |
|
| 509 | - // mettre a jour toutes les sessions |
|
| 510 | - actualiser_sessions($GLOBALS['visiteur_session'], $remove); |
|
| 505 | + // se remettre dans le dossier de travail au cas ou Apache a change |
|
| 506 | + chdir(_ROOT_CWD); |
|
| 507 | + // recuperer les variables a effacer |
|
| 508 | + $remove = session_set(false); |
|
| 509 | + // mettre a jour toutes les sessions |
|
| 510 | + actualiser_sessions($GLOBALS['visiteur_session'], $remove); |
|
| 511 | 511 | } |
| 512 | 512 | |
| 513 | 513 | |
@@ -532,83 +532,83 @@ discard block |
||
| 532 | 532 | */ |
| 533 | 533 | function actualiser_sessions($auteur, $supprimer_cles = []) { |
| 534 | 534 | |
| 535 | - $id_auteur = isset($auteur['id_auteur']) ? intval($auteur['id_auteur']) : 0; |
|
| 536 | - $id_auteur_courant = isset($GLOBALS['visiteur_session']['id_auteur']) ? intval($GLOBALS['visiteur_session']['id_auteur']) : 0; |
|
| 537 | - |
|
| 538 | - // si l'auteur est celui de la session courante, verifier/creer la session si besoin |
|
| 539 | - $fichier_session_courante = ''; |
|
| 540 | - if ($id_auteur == $id_auteur_courant) { |
|
| 541 | - $auteur = array_merge($GLOBALS['visiteur_session'], $auteur); |
|
| 542 | - ajouter_session($auteur); |
|
| 543 | - if ($id_auteur && ($cookie = lire_cookie_session())) { |
|
| 544 | - $fichier_session_courante = chemin_fichier_session('alea_ephemere', $cookie); |
|
| 545 | - } |
|
| 546 | - } |
|
| 547 | - |
|
| 548 | - // si session anonyme on ne fait rien d'autre ici : les sessions anonymes sont non partagees |
|
| 549 | - if (!$id_auteur) { |
|
| 550 | - return; |
|
| 551 | - } |
|
| 552 | - |
|
| 553 | - // les préférences sont désérialisées, toujours. |
|
| 554 | - // [fixme] Le champ 'prefs' sert aussi à l’inscription |
|
| 555 | - if (isset($auteur['prefs']) && is_string($auteur['prefs'])) { |
|
| 556 | - $auteur['prefs'] = @unserialize($auteur['prefs']); |
|
| 557 | - if (!is_array($auteur['prefs'])) { |
|
| 558 | - $auteur['prefs'] = []; |
|
| 559 | - } |
|
| 560 | - } |
|
| 561 | - |
|
| 562 | - // memoriser l'auteur courant (celui qui modifie la fiche) |
|
| 563 | - $sauve = $GLOBALS['visiteur_session']; |
|
| 564 | - |
|
| 565 | - // .. mettre a jour les sessions de l'auteur cible |
|
| 566 | - // attention au $ final pour ne pas risquer d'embarquer un .php.jeton temporaire |
|
| 567 | - // cree par une ecriture concurente d'une session (fichier atomique temporaire) |
|
| 568 | - $sessions = lister_sessions_auteur($id_auteur); |
|
| 569 | - |
|
| 570 | - // 1ere passe : lire et fusionner les sessions |
|
| 571 | - foreach ($sessions as $session) { |
|
| 572 | - $GLOBALS['visiteur_session'] = []; |
|
| 573 | - // a pu etre supprime entre le preg initial et le moment ou l'on arrive la (concurrence) |
|
| 574 | - if ( |
|
| 575 | - $session !== $fichier_session_courante |
|
| 576 | - && @file_exists($session) |
|
| 577 | - ) { |
|
| 578 | - include $session; # $GLOBALS['visiteur_session'] est alors l'auteur cible |
|
| 579 | - |
|
| 580 | - $auteur = array_merge($GLOBALS['visiteur_session'], $auteur); |
|
| 581 | - } |
|
| 582 | - } |
|
| 583 | - |
|
| 584 | - // supprimer les eventuelles cles dont on ne veut plus |
|
| 585 | - foreach ($supprimer_cles as $cle) { |
|
| 586 | - unset($auteur[$cle]); |
|
| 587 | - } |
|
| 588 | - |
|
| 589 | - $auteur_session = preparer_ecriture_session($auteur); |
|
| 590 | - |
|
| 591 | - // seconde passe : ecrire les sessions qui ne sont pas a jour |
|
| 592 | - foreach ($sessions as $session) { |
|
| 593 | - $GLOBALS['visiteur_session'] = []; |
|
| 594 | - // a pu etre supprime entre le preg initial et le moment ou l'on arrive la (concurrence) |
|
| 595 | - if (@file_exists($session)) { |
|
| 596 | - include $session; # $GLOBALS['visiteur_session'] est alors l'auteur cible |
|
| 597 | - |
|
| 598 | - // est-ce que cette session est a mettre a jour ? |
|
| 599 | - if ($auteur_session != $GLOBALS['visiteur_session']) { |
|
| 600 | - ecrire_fichier_session($session, $auteur); |
|
| 601 | - } |
|
| 602 | - } |
|
| 603 | - } |
|
| 604 | - |
|
| 605 | - if ($id_auteur == $id_auteur_courant) { |
|
| 606 | - $GLOBALS['visiteur_session'] = $auteur; |
|
| 607 | - $GLOBALS['auteur_session'] = &$GLOBALS['visiteur_session']; |
|
| 608 | - } else { |
|
| 609 | - // restaurer l'auteur courant |
|
| 610 | - $GLOBALS['visiteur_session'] = $sauve; |
|
| 611 | - } |
|
| 535 | + $id_auteur = isset($auteur['id_auteur']) ? intval($auteur['id_auteur']) : 0; |
|
| 536 | + $id_auteur_courant = isset($GLOBALS['visiteur_session']['id_auteur']) ? intval($GLOBALS['visiteur_session']['id_auteur']) : 0; |
|
| 537 | + |
|
| 538 | + // si l'auteur est celui de la session courante, verifier/creer la session si besoin |
|
| 539 | + $fichier_session_courante = ''; |
|
| 540 | + if ($id_auteur == $id_auteur_courant) { |
|
| 541 | + $auteur = array_merge($GLOBALS['visiteur_session'], $auteur); |
|
| 542 | + ajouter_session($auteur); |
|
| 543 | + if ($id_auteur && ($cookie = lire_cookie_session())) { |
|
| 544 | + $fichier_session_courante = chemin_fichier_session('alea_ephemere', $cookie); |
|
| 545 | + } |
|
| 546 | + } |
|
| 547 | + |
|
| 548 | + // si session anonyme on ne fait rien d'autre ici : les sessions anonymes sont non partagees |
|
| 549 | + if (!$id_auteur) { |
|
| 550 | + return; |
|
| 551 | + } |
|
| 552 | + |
|
| 553 | + // les préférences sont désérialisées, toujours. |
|
| 554 | + // [fixme] Le champ 'prefs' sert aussi à l’inscription |
|
| 555 | + if (isset($auteur['prefs']) && is_string($auteur['prefs'])) { |
|
| 556 | + $auteur['prefs'] = @unserialize($auteur['prefs']); |
|
| 557 | + if (!is_array($auteur['prefs'])) { |
|
| 558 | + $auteur['prefs'] = []; |
|
| 559 | + } |
|
| 560 | + } |
|
| 561 | + |
|
| 562 | + // memoriser l'auteur courant (celui qui modifie la fiche) |
|
| 563 | + $sauve = $GLOBALS['visiteur_session']; |
|
| 564 | + |
|
| 565 | + // .. mettre a jour les sessions de l'auteur cible |
|
| 566 | + // attention au $ final pour ne pas risquer d'embarquer un .php.jeton temporaire |
|
| 567 | + // cree par une ecriture concurente d'une session (fichier atomique temporaire) |
|
| 568 | + $sessions = lister_sessions_auteur($id_auteur); |
|
| 569 | + |
|
| 570 | + // 1ere passe : lire et fusionner les sessions |
|
| 571 | + foreach ($sessions as $session) { |
|
| 572 | + $GLOBALS['visiteur_session'] = []; |
|
| 573 | + // a pu etre supprime entre le preg initial et le moment ou l'on arrive la (concurrence) |
|
| 574 | + if ( |
|
| 575 | + $session !== $fichier_session_courante |
|
| 576 | + && @file_exists($session) |
|
| 577 | + ) { |
|
| 578 | + include $session; # $GLOBALS['visiteur_session'] est alors l'auteur cible |
|
| 579 | + |
|
| 580 | + $auteur = array_merge($GLOBALS['visiteur_session'], $auteur); |
|
| 581 | + } |
|
| 582 | + } |
|
| 583 | + |
|
| 584 | + // supprimer les eventuelles cles dont on ne veut plus |
|
| 585 | + foreach ($supprimer_cles as $cle) { |
|
| 586 | + unset($auteur[$cle]); |
|
| 587 | + } |
|
| 588 | + |
|
| 589 | + $auteur_session = preparer_ecriture_session($auteur); |
|
| 590 | + |
|
| 591 | + // seconde passe : ecrire les sessions qui ne sont pas a jour |
|
| 592 | + foreach ($sessions as $session) { |
|
| 593 | + $GLOBALS['visiteur_session'] = []; |
|
| 594 | + // a pu etre supprime entre le preg initial et le moment ou l'on arrive la (concurrence) |
|
| 595 | + if (@file_exists($session)) { |
|
| 596 | + include $session; # $GLOBALS['visiteur_session'] est alors l'auteur cible |
|
| 597 | + |
|
| 598 | + // est-ce que cette session est a mettre a jour ? |
|
| 599 | + if ($auteur_session != $GLOBALS['visiteur_session']) { |
|
| 600 | + ecrire_fichier_session($session, $auteur); |
|
| 601 | + } |
|
| 602 | + } |
|
| 603 | + } |
|
| 604 | + |
|
| 605 | + if ($id_auteur == $id_auteur_courant) { |
|
| 606 | + $GLOBALS['visiteur_session'] = $auteur; |
|
| 607 | + $GLOBALS['auteur_session'] = &$GLOBALS['visiteur_session']; |
|
| 608 | + } else { |
|
| 609 | + // restaurer l'auteur courant |
|
| 610 | + $GLOBALS['visiteur_session'] = $sauve; |
|
| 611 | + } |
|
| 612 | 612 | } |
| 613 | 613 | |
| 614 | 614 | /** |
@@ -622,48 +622,48 @@ discard block |
||
| 622 | 622 | */ |
| 623 | 623 | function lister_sessions_auteur($id_auteur, $nb_max = null) { |
| 624 | 624 | |
| 625 | - if (is_null($nb_max)) { |
|
| 626 | - if (!defined('_NB_SESSIONS_MAX')) { |
|
| 627 | - define('_NB_SESSIONS_MAX', 100); |
|
| 628 | - } |
|
| 629 | - $nb_max = _NB_SESSIONS_MAX; |
|
| 630 | - } |
|
| 631 | - |
|
| 632 | - // liste des sessions |
|
| 633 | - $sessions = preg_files(_DIR_SESSIONS, '/' . $id_auteur . '_.*\.php$'); |
|
| 634 | - |
|
| 635 | - // si on en a plus que la limite, supprimer les plus vieilles |
|
| 636 | - // si ce ne sont pas des sessions anonymes car elles sont alors chacune differentes |
|
| 637 | - if ( |
|
| 638 | - $id_auteur |
|
| 639 | - && count($sessions) > $nb_max |
|
| 640 | - ) { |
|
| 641 | - // limiter le nombre de sessions ouvertes par un auteur |
|
| 642 | - // filemtime sur les sessions |
|
| 643 | - $sessions = array_flip($sessions); |
|
| 644 | - |
|
| 645 | - // 1ere passe : lire les filemtime |
|
| 646 | - foreach ($sessions as $session => $z) { |
|
| 647 | - if ( |
|
| 648 | - $d = @filemtime($session) |
|
| 649 | - ) { |
|
| 650 | - $sessions[$session] = $d; |
|
| 651 | - } else { |
|
| 652 | - $sessions[$session] = 0; |
|
| 653 | - } |
|
| 654 | - } |
|
| 655 | - |
|
| 656 | - // les plus anciennes en premier |
|
| 657 | - asort($sessions); |
|
| 658 | - |
|
| 659 | - $sessions = array_keys($sessions); |
|
| 660 | - while (count($sessions) > $nb_max) { |
|
| 661 | - $session = array_shift($sessions); |
|
| 662 | - @unlink($session); |
|
| 663 | - } |
|
| 664 | - } |
|
| 665 | - |
|
| 666 | - return $sessions; |
|
| 625 | + if (is_null($nb_max)) { |
|
| 626 | + if (!defined('_NB_SESSIONS_MAX')) { |
|
| 627 | + define('_NB_SESSIONS_MAX', 100); |
|
| 628 | + } |
|
| 629 | + $nb_max = _NB_SESSIONS_MAX; |
|
| 630 | + } |
|
| 631 | + |
|
| 632 | + // liste des sessions |
|
| 633 | + $sessions = preg_files(_DIR_SESSIONS, '/' . $id_auteur . '_.*\.php$'); |
|
| 634 | + |
|
| 635 | + // si on en a plus que la limite, supprimer les plus vieilles |
|
| 636 | + // si ce ne sont pas des sessions anonymes car elles sont alors chacune differentes |
|
| 637 | + if ( |
|
| 638 | + $id_auteur |
|
| 639 | + && count($sessions) > $nb_max |
|
| 640 | + ) { |
|
| 641 | + // limiter le nombre de sessions ouvertes par un auteur |
|
| 642 | + // filemtime sur les sessions |
|
| 643 | + $sessions = array_flip($sessions); |
|
| 644 | + |
|
| 645 | + // 1ere passe : lire les filemtime |
|
| 646 | + foreach ($sessions as $session => $z) { |
|
| 647 | + if ( |
|
| 648 | + $d = @filemtime($session) |
|
| 649 | + ) { |
|
| 650 | + $sessions[$session] = $d; |
|
| 651 | + } else { |
|
| 652 | + $sessions[$session] = 0; |
|
| 653 | + } |
|
| 654 | + } |
|
| 655 | + |
|
| 656 | + // les plus anciennes en premier |
|
| 657 | + asort($sessions); |
|
| 658 | + |
|
| 659 | + $sessions = array_keys($sessions); |
|
| 660 | + while (count($sessions) > $nb_max) { |
|
| 661 | + $session = array_shift($sessions); |
|
| 662 | + @unlink($session); |
|
| 663 | + } |
|
| 664 | + } |
|
| 665 | + |
|
| 666 | + return $sessions; |
|
| 667 | 667 | } |
| 668 | 668 | |
| 669 | 669 | |
@@ -678,22 +678,22 @@ discard block |
||
| 678 | 678 | */ |
| 679 | 679 | function preparer_ecriture_session(array $auteur): array { |
| 680 | 680 | |
| 681 | - $row = $auteur; |
|
| 681 | + $row = $auteur; |
|
| 682 | 682 | |
| 683 | - // ne pas enregistrer ces elements de securite dans le fichier de session |
|
| 684 | - include_spip('inc/auth'); |
|
| 685 | - $auteur = auth_desensibiliser_session($auteur); |
|
| 683 | + // ne pas enregistrer ces elements de securite dans le fichier de session |
|
| 684 | + include_spip('inc/auth'); |
|
| 685 | + $auteur = auth_desensibiliser_session($auteur); |
|
| 686 | 686 | |
| 687 | - $auteur = pipeline('preparer_fichier_session', ['args' => ['row' => $row], 'data' => $auteur]); |
|
| 687 | + $auteur = pipeline('preparer_fichier_session', ['args' => ['row' => $row], 'data' => $auteur]); |
|
| 688 | 688 | |
| 689 | - // ne pas enregistrer les valeurs vraiment nulle dans le fichier |
|
| 690 | - foreach ($auteur as $variable => $valeur) { |
|
| 691 | - if ($valeur === null) { |
|
| 692 | - unset($auteur[$variable]); |
|
| 693 | - } |
|
| 694 | - } |
|
| 689 | + // ne pas enregistrer les valeurs vraiment nulle dans le fichier |
|
| 690 | + foreach ($auteur as $variable => $valeur) { |
|
| 691 | + if ($valeur === null) { |
|
| 692 | + unset($auteur[$variable]); |
|
| 693 | + } |
|
| 694 | + } |
|
| 695 | 695 | |
| 696 | - return $auteur; |
|
| 696 | + return $auteur; |
|
| 697 | 697 | } |
| 698 | 698 | |
| 699 | 699 | /** |
@@ -705,39 +705,39 @@ discard block |
||
| 705 | 705 | */ |
| 706 | 706 | function ecrire_fichier_session($fichier, $auteur) { |
| 707 | 707 | |
| 708 | - $auteur = preparer_ecriture_session($auteur); |
|
| 708 | + $auteur = preparer_ecriture_session($auteur); |
|
| 709 | 709 | |
| 710 | - // enregistrer les autres donnees du visiteur |
|
| 711 | - $texte = '<' . "?php\n"; |
|
| 712 | - foreach ($auteur as $var => $val) { |
|
| 713 | - $texte .= '$GLOBALS[\'visiteur_session\'][' . var_export($var, true) . '] = ' |
|
| 714 | - . var_export($val, true) . ";\n"; |
|
| 715 | - } |
|
| 716 | - $texte .= '?' . ">\n"; |
|
| 710 | + // enregistrer les autres donnees du visiteur |
|
| 711 | + $texte = '<' . "?php\n"; |
|
| 712 | + foreach ($auteur as $var => $val) { |
|
| 713 | + $texte .= '$GLOBALS[\'visiteur_session\'][' . var_export($var, true) . '] = ' |
|
| 714 | + . var_export($val, true) . ";\n"; |
|
| 715 | + } |
|
| 716 | + $texte .= '?' . ">\n"; |
|
| 717 | 717 | |
| 718 | - return ecrire_fichier($fichier, $texte); |
|
| 718 | + return ecrire_fichier($fichier, $texte); |
|
| 719 | 719 | } |
| 720 | 720 | |
| 721 | 721 | /** |
| 722 | 722 | * Calculer le chemin vers le fichier de session |
| 723 | 723 | */ |
| 724 | 724 | function chemin_fichier_session(string $alea, string $cookie_session, bool $tantpis = false): string { |
| 725 | - include_spip('inc/acces'); |
|
| 726 | - charger_aleas(); |
|
| 727 | - |
|
| 728 | - if (empty($GLOBALS['meta'][$alea])) { |
|
| 729 | - if (!$tantpis) { |
|
| 730 | - spip_logger('session')->info("fichier session ($tantpis): $alea indisponible"); |
|
| 731 | - include_spip('inc/minipres'); |
|
| 732 | - echo minipres(); |
|
| 733 | - } |
|
| 734 | - |
|
| 735 | - return ''; // echec mais $tanpis |
|
| 736 | - } |
|
| 737 | - |
|
| 738 | - $repertoire = sous_repertoire(_DIR_SESSIONS, '', false, $tantpis); |
|
| 739 | - $id_auteur = intval($cookie_session); |
|
| 740 | - return $repertoire . $id_auteur . '_' . md5($cookie_session . ' ' . $GLOBALS['meta'][$alea]) . '.php'; |
|
| 725 | + include_spip('inc/acces'); |
|
| 726 | + charger_aleas(); |
|
| 727 | + |
|
| 728 | + if (empty($GLOBALS['meta'][$alea])) { |
|
| 729 | + if (!$tantpis) { |
|
| 730 | + spip_logger('session')->info("fichier session ($tantpis): $alea indisponible"); |
|
| 731 | + include_spip('inc/minipres'); |
|
| 732 | + echo minipres(); |
|
| 733 | + } |
|
| 734 | + |
|
| 735 | + return ''; // echec mais $tanpis |
|
| 736 | + } |
|
| 737 | + |
|
| 738 | + $repertoire = sous_repertoire(_DIR_SESSIONS, '', false, $tantpis); |
|
| 739 | + $id_auteur = intval($cookie_session); |
|
| 740 | + return $repertoire . $id_auteur . '_' . md5($cookie_session . ' ' . $GLOBALS['meta'][$alea]) . '.php'; |
|
| 741 | 741 | } |
| 742 | 742 | |
| 743 | 743 | /** |
@@ -748,7 +748,7 @@ discard block |
||
| 748 | 748 | * @param bool $tantpis |
| 749 | 749 | */ |
| 750 | 750 | function fichier_session($alea, $tantpis = false): string { |
| 751 | - return chemin_fichier_session((string) $alea, lire_cookie_session(), (bool) $tantpis); |
|
| 751 | + return chemin_fichier_session((string) $alea, lire_cookie_session(), (bool) $tantpis); |
|
| 752 | 752 | } |
| 753 | 753 | |
| 754 | 754 | |
@@ -765,7 +765,7 @@ discard block |
||
| 765 | 765 | * @return string |
| 766 | 766 | */ |
| 767 | 767 | function rejouer_session() { |
| 768 | - return '<img src="' . generer_url_action('cookie', 'change_session=oui', true) . '" width="0" height="0" alt="" />'; |
|
| 768 | + return '<img src="' . generer_url_action('cookie', 'change_session=oui', true) . '" width="0" height="0" alt="" />'; |
|
| 769 | 769 | } |
| 770 | 770 | |
| 771 | 771 | |
@@ -775,12 +775,12 @@ discard block |
||
| 775 | 775 | * @return string |
| 776 | 776 | */ |
| 777 | 777 | function hash_env() { |
| 778 | - static $res = ''; |
|
| 779 | - if ($res) { |
|
| 780 | - return $res; |
|
| 781 | - } |
|
| 778 | + static $res = ''; |
|
| 779 | + if ($res) { |
|
| 780 | + return $res; |
|
| 781 | + } |
|
| 782 | 782 | |
| 783 | - return $res = md5($GLOBALS['ip'] . ($_SERVER['HTTP_USER_AGENT'] ?? '')); |
|
| 783 | + return $res = md5($GLOBALS['ip'] . ($_SERVER['HTTP_USER_AGENT'] ?? '')); |
|
| 784 | 784 | } |
| 785 | 785 | |
| 786 | 786 | |
@@ -792,11 +792,11 @@ discard block |
||
| 792 | 792 | * @return bool True si une session PHP est ouverte. |
| 793 | 793 | **/ |
| 794 | 794 | function spip_php_session_start() { |
| 795 | - if (!is_php_session_started()) { |
|
| 796 | - return session_start(); |
|
| 797 | - } |
|
| 795 | + if (!is_php_session_started()) { |
|
| 796 | + return session_start(); |
|
| 797 | + } |
|
| 798 | 798 | |
| 799 | - return true; |
|
| 799 | + return true; |
|
| 800 | 800 | } |
| 801 | 801 | |
| 802 | 802 | /** |
@@ -806,9 +806,9 @@ discard block |
||
| 806 | 806 | * @return bool true si une session PHP est active |
| 807 | 807 | **/ |
| 808 | 808 | function is_php_session_started() { |
| 809 | - if (php_sapi_name() !== 'cli') { |
|
| 810 | - return session_status() === PHP_SESSION_ACTIVE ? true : false; |
|
| 811 | - } |
|
| 809 | + if (php_sapi_name() !== 'cli') { |
|
| 810 | + return session_status() === PHP_SESSION_ACTIVE ? true : false; |
|
| 811 | + } |
|
| 812 | 812 | |
| 813 | - return false; |
|
| 813 | + return false; |
|
| 814 | 814 | } |
@@ -81,12 +81,12 @@ discard block |
||
| 81 | 81 | spip_logger('session')->info("supprimer sessions auteur $id_auteur"); |
| 82 | 82 | if ($toutes || $id_auteur !== $GLOBALS['visiteur_session']['id_auteur']) { |
| 83 | 83 | if ($dir = opendir(_DIR_SESSIONS)) { |
| 84 | - $t = $_SERVER['REQUEST_TIME'] - (4 * _RENOUVELLE_ALEA); // 48h par defaut |
|
| 85 | - $t_short = $_SERVER['REQUEST_TIME'] - max(_RENOUVELLE_ALEA / 4, 3 * 3600); // 3h par defaut |
|
| 84 | + $t = $_SERVER['REQUEST_TIME'] - (4 * _RENOUVELLE_ALEA); // 48h par defaut |
|
| 85 | + $t_short = $_SERVER['REQUEST_TIME'] - max(_RENOUVELLE_ALEA / 4, 3 * 3600); // 3h par defaut |
|
| 86 | 86 | while (($f = readdir($dir)) !== false) { |
| 87 | 87 | $nb_files++; |
| 88 | 88 | if (preg_match(',^[^\d-]*(-?\d+)_\w{32}\.php[3]?$,', $f, $regs)) { |
| 89 | - $f = _DIR_SESSIONS . $f; |
|
| 89 | + $f = _DIR_SESSIONS.$f; |
|
| 90 | 90 | if ($actives && $regs[1] == $id_auteur || $t > filemtime($f)) { |
| 91 | 91 | spip_unlink($f); |
| 92 | 92 | } |
@@ -173,7 +173,7 @@ discard block |
||
| 173 | 173 | (!$cookie = lire_cookie_session()) |
| 174 | 174 | || intval($cookie) !== $id_auteur |
| 175 | 175 | ) { |
| 176 | - $cookie = $id_auteur . '_' . md5(uniqid(random_int(0, mt_getrandmax()), true)); |
|
| 176 | + $cookie = $id_auteur.'_'.md5(uniqid(random_int(0, mt_getrandmax()), true)); |
|
| 177 | 177 | } |
| 178 | 178 | |
| 179 | 179 | // Maintenant on sait qu'on a des choses à écrire |
@@ -206,7 +206,7 @@ discard block |
||
| 206 | 206 | } else { |
| 207 | 207 | $fichier_session = chemin_fichier_session('alea_ephemere', $cookie); |
| 208 | 208 | if (!ecrire_fichier_session($fichier_session, $auteur)) { |
| 209 | - spip_logger('session')->emergency('Echec ecriture fichier session ' . $fichier_session); |
|
| 209 | + spip_logger('session')->emergency('Echec ecriture fichier session '.$fichier_session); |
|
| 210 | 210 | include_spip('inc/minipres'); |
| 211 | 211 | echo minipres(); |
| 212 | 212 | exit; |
@@ -229,9 +229,9 @@ discard block |
||
| 229 | 229 | if (autoriser('ecrire', '', '', $auteur) && _DUREE_COOKIE_ADMIN) { |
| 230 | 230 | spip_setcookie( |
| 231 | 231 | 'spip_admin', |
| 232 | - '@' . ($auteur['email'] ?: $auteur['login']), |
|
| 232 | + '@'.($auteur['email'] ?: $auteur['login']), |
|
| 233 | 233 | time() + max(_DUREE_COOKIE_ADMIN, $duree), |
| 234 | - httponly: true |
|
| 234 | + httponly : true |
|
| 235 | 235 | ); |
| 236 | 236 | } else { |
| 237 | 237 | // sinon le supprimer ... |
@@ -269,7 +269,7 @@ discard block |
||
| 269 | 269 | $coef = 20; |
| 270 | 270 | } |
| 271 | 271 | } |
| 272 | - return (int)(_RENOUVELLE_ALEA * $coef); |
|
| 272 | + return (int) (_RENOUVELLE_ALEA * $coef); |
|
| 273 | 273 | } |
| 274 | 274 | |
| 275 | 275 | /** |
@@ -381,7 +381,7 @@ discard block |
||
| 381 | 381 | |
| 382 | 382 | // Renouveler la session avec l'alea courant |
| 383 | 383 | include($fichier_session); |
| 384 | - spip_logger('session')->info('renouvelle session ' . $GLOBALS['visiteur_session']['id_auteur']); |
|
| 384 | + spip_logger('session')->info('renouvelle session '.$GLOBALS['visiteur_session']['id_auteur']); |
|
| 385 | 385 | spip_unlink($fichier_session); |
| 386 | 386 | ajouter_session($GLOBALS['visiteur_session']); |
| 387 | 387 | } |
@@ -630,7 +630,7 @@ discard block |
||
| 630 | 630 | } |
| 631 | 631 | |
| 632 | 632 | // liste des sessions |
| 633 | - $sessions = preg_files(_DIR_SESSIONS, '/' . $id_auteur . '_.*\.php$'); |
|
| 633 | + $sessions = preg_files(_DIR_SESSIONS, '/'.$id_auteur.'_.*\.php$'); |
|
| 634 | 634 | |
| 635 | 635 | // si on en a plus que la limite, supprimer les plus vieilles |
| 636 | 636 | // si ce ne sont pas des sessions anonymes car elles sont alors chacune differentes |
@@ -708,12 +708,12 @@ discard block |
||
| 708 | 708 | $auteur = preparer_ecriture_session($auteur); |
| 709 | 709 | |
| 710 | 710 | // enregistrer les autres donnees du visiteur |
| 711 | - $texte = '<' . "?php\n"; |
|
| 711 | + $texte = '<'."?php\n"; |
|
| 712 | 712 | foreach ($auteur as $var => $val) { |
| 713 | - $texte .= '$GLOBALS[\'visiteur_session\'][' . var_export($var, true) . '] = ' |
|
| 714 | - . var_export($val, true) . ";\n"; |
|
| 713 | + $texte .= '$GLOBALS[\'visiteur_session\']['.var_export($var, true).'] = ' |
|
| 714 | + . var_export($val, true).";\n"; |
|
| 715 | 715 | } |
| 716 | - $texte .= '?' . ">\n"; |
|
| 716 | + $texte .= '?'.">\n"; |
|
| 717 | 717 | |
| 718 | 718 | return ecrire_fichier($fichier, $texte); |
| 719 | 719 | } |
@@ -737,7 +737,7 @@ discard block |
||
| 737 | 737 | |
| 738 | 738 | $repertoire = sous_repertoire(_DIR_SESSIONS, '', false, $tantpis); |
| 739 | 739 | $id_auteur = intval($cookie_session); |
| 740 | - return $repertoire . $id_auteur . '_' . md5($cookie_session . ' ' . $GLOBALS['meta'][$alea]) . '.php'; |
|
| 740 | + return $repertoire.$id_auteur.'_'.md5($cookie_session.' '.$GLOBALS['meta'][$alea]).'.php'; |
|
| 741 | 741 | } |
| 742 | 742 | |
| 743 | 743 | /** |
@@ -765,7 +765,7 @@ discard block |
||
| 765 | 765 | * @return string |
| 766 | 766 | */ |
| 767 | 767 | function rejouer_session() { |
| 768 | - return '<img src="' . generer_url_action('cookie', 'change_session=oui', true) . '" width="0" height="0" alt="" />'; |
|
| 768 | + return '<img src="'.generer_url_action('cookie', 'change_session=oui', true).'" width="0" height="0" alt="" />'; |
|
| 769 | 769 | } |
| 770 | 770 | |
| 771 | 771 | |
@@ -780,7 +780,7 @@ discard block |
||
| 780 | 780 | return $res; |
| 781 | 781 | } |
| 782 | 782 | |
| 783 | - return $res = md5($GLOBALS['ip'] . ($_SERVER['HTTP_USER_AGENT'] ?? '')); |
|
| 783 | + return $res = md5($GLOBALS['ip'].($_SERVER['HTTP_USER_AGENT'] ?? '')); |
|
| 784 | 784 | } |
| 785 | 785 | |
| 786 | 786 | |