@@ -28,7 +28,7 @@ discard block |
||
| 28 | 28 | **/ |
| 29 | 29 | |
| 30 | 30 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 31 | - return; |
|
| 31 | + return; |
|
| 32 | 32 | } |
| 33 | 33 | |
| 34 | 34 | /** |
@@ -50,16 +50,16 @@ discard block |
||
| 50 | 50 | * Code PHP si cet argument est présent, sinon null |
| 51 | 51 | **/ |
| 52 | 52 | function interprete_argument_balise(int $n, Champ $p): ?string { |
| 53 | - if (($p->param) && (!$p->param[0][0]) && ((is_countable($p->param[0]) ? count($p->param[0]) : 0) > $n)) { |
|
| 54 | - return calculer_liste( |
|
| 55 | - $p->param[0][$n], |
|
| 56 | - $p->descr, |
|
| 57 | - $p->boucles, |
|
| 58 | - $p->id_boucle |
|
| 59 | - ); |
|
| 60 | - } else { |
|
| 61 | - return null; |
|
| 62 | - } |
|
| 53 | + if (($p->param) && (!$p->param[0][0]) && ((is_countable($p->param[0]) ? count($p->param[0]) : 0) > $n)) { |
|
| 54 | + return calculer_liste( |
|
| 55 | + $p->param[0][$n], |
|
| 56 | + $p->descr, |
|
| 57 | + $p->boucles, |
|
| 58 | + $p->id_boucle |
|
| 59 | + ); |
|
| 60 | + } else { |
|
| 61 | + return null; |
|
| 62 | + } |
|
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | |
@@ -79,10 +79,10 @@ discard block |
||
| 79 | 79 | * Pile complétée par le code à générer |
| 80 | 80 | **/ |
| 81 | 81 | function balise_NOM_SITE_SPIP_dist($p) { |
| 82 | - $p->code = "\$GLOBALS['meta']['nom_site']"; |
|
| 82 | + $p->code = "\$GLOBALS['meta']['nom_site']"; |
|
| 83 | 83 | |
| 84 | - #$p->interdire_scripts = true; |
|
| 85 | - return $p; |
|
| 84 | + #$p->interdire_scripts = true; |
|
| 85 | + return $p; |
|
| 86 | 86 | } |
| 87 | 87 | |
| 88 | 88 | /** |
@@ -98,10 +98,10 @@ discard block |
||
| 98 | 98 | * Pile complétée par le code à générer |
| 99 | 99 | **/ |
| 100 | 100 | function balise_EMAIL_WEBMASTER_dist($p) { |
| 101 | - $p->code = "\$GLOBALS['meta']['email_webmaster']"; |
|
| 101 | + $p->code = "\$GLOBALS['meta']['email_webmaster']"; |
|
| 102 | 102 | |
| 103 | - #$p->interdire_scripts = true; |
|
| 104 | - return $p; |
|
| 103 | + #$p->interdire_scripts = true; |
|
| 104 | + return $p; |
|
| 105 | 105 | } |
| 106 | 106 | |
| 107 | 107 | /** |
@@ -117,10 +117,10 @@ discard block |
||
| 117 | 117 | * Pile complétée par le code à générer |
| 118 | 118 | **/ |
| 119 | 119 | function balise_DESCRIPTIF_SITE_SPIP_dist($p) { |
| 120 | - $p->code = "\$GLOBALS['meta']['descriptif_site']"; |
|
| 120 | + $p->code = "\$GLOBALS['meta']['descriptif_site']"; |
|
| 121 | 121 | |
| 122 | - #$p->interdire_scripts = true; |
|
| 123 | - return $p; |
|
| 122 | + #$p->interdire_scripts = true; |
|
| 123 | + return $p; |
|
| 124 | 124 | } |
| 125 | 125 | |
| 126 | 126 | |
@@ -141,10 +141,10 @@ discard block |
||
| 141 | 141 | * Pile complétée par le code à générer |
| 142 | 142 | **/ |
| 143 | 143 | function balise_CHARSET_dist($p) { |
| 144 | - $p->code = "\$GLOBALS['meta']['charset']"; |
|
| 144 | + $p->code = "\$GLOBALS['meta']['charset']"; |
|
| 145 | 145 | |
| 146 | - #$p->interdire_scripts = true; |
|
| 147 | - return $p; |
|
| 146 | + #$p->interdire_scripts = true; |
|
| 147 | + return $p; |
|
| 148 | 148 | } |
| 149 | 149 | |
| 150 | 150 | /** |
@@ -169,11 +169,11 @@ discard block |
||
| 169 | 169 | * Pile complétée par le code à générer |
| 170 | 170 | **/ |
| 171 | 171 | function balise_LANG_LEFT_dist($p) { |
| 172 | - $_lang = champ_sql('lang', $p); |
|
| 173 | - $p->code = "lang_dir($_lang, 'left','right')"; |
|
| 174 | - $p->interdire_scripts = false; |
|
| 172 | + $_lang = champ_sql('lang', $p); |
|
| 173 | + $p->code = "lang_dir($_lang, 'left','right')"; |
|
| 174 | + $p->interdire_scripts = false; |
|
| 175 | 175 | |
| 176 | - return $p; |
|
| 176 | + return $p; |
|
| 177 | 177 | } |
| 178 | 178 | |
| 179 | 179 | /** |
@@ -193,11 +193,11 @@ discard block |
||
| 193 | 193 | * Pile complétée par le code à générer |
| 194 | 194 | **/ |
| 195 | 195 | function balise_LANG_RIGHT_dist($p) { |
| 196 | - $_lang = champ_sql('lang', $p); |
|
| 197 | - $p->code = "lang_dir($_lang, 'right','left')"; |
|
| 198 | - $p->interdire_scripts = false; |
|
| 196 | + $_lang = champ_sql('lang', $p); |
|
| 197 | + $p->code = "lang_dir($_lang, 'right','left')"; |
|
| 198 | + $p->interdire_scripts = false; |
|
| 199 | 199 | |
| 200 | - return $p; |
|
| 200 | + return $p; |
|
| 201 | 201 | } |
| 202 | 202 | |
| 203 | 203 | /** |
@@ -222,11 +222,11 @@ discard block |
||
| 222 | 222 | * Pile complétée par le code à générer |
| 223 | 223 | **/ |
| 224 | 224 | function balise_LANG_DIR_dist($p) { |
| 225 | - $_lang = champ_sql('lang', $p); |
|
| 226 | - $p->code = "lang_dir($_lang, 'ltr','rtl')"; |
|
| 227 | - $p->interdire_scripts = false; |
|
| 225 | + $_lang = champ_sql('lang', $p); |
|
| 226 | + $p->code = "lang_dir($_lang, 'ltr','rtl')"; |
|
| 227 | + $p->interdire_scripts = false; |
|
| 228 | 228 | |
| 229 | - return $p; |
|
| 229 | + return $p; |
|
| 230 | 230 | } |
| 231 | 231 | |
| 232 | 232 | |
@@ -243,10 +243,10 @@ discard block |
||
| 243 | 243 | * Pile complétée par le code à générer |
| 244 | 244 | **/ |
| 245 | 245 | function balise_PUCE_dist($p) { |
| 246 | - $p->code = 'definir_puce()'; |
|
| 247 | - $p->interdire_scripts = false; |
|
| 246 | + $p->code = 'definir_puce()'; |
|
| 247 | + $p->interdire_scripts = false; |
|
| 248 | 248 | |
| 249 | - return $p; |
|
| 249 | + return $p; |
|
| 250 | 250 | } |
| 251 | 251 | |
| 252 | 252 | |
@@ -270,9 +270,9 @@ discard block |
||
| 270 | 270 | * Pile completée du code PHP d'exécution de la balise |
| 271 | 271 | */ |
| 272 | 272 | function balise_DATE_dist($p) { |
| 273 | - $p->code = champ_sql('date', $p); |
|
| 273 | + $p->code = champ_sql('date', $p); |
|
| 274 | 274 | |
| 275 | - return $p; |
|
| 275 | + return $p; |
|
| 276 | 276 | } |
| 277 | 277 | |
| 278 | 278 | |
@@ -292,10 +292,10 @@ discard block |
||
| 292 | 292 | * Pile completée du code PHP d'exécution de la balise |
| 293 | 293 | */ |
| 294 | 294 | function balise_DATE_REDAC_dist($p) { |
| 295 | - $p->code = champ_sql('date_redac', $p); |
|
| 296 | - $p->interdire_scripts = false; |
|
| 295 | + $p->code = champ_sql('date_redac', $p); |
|
| 296 | + $p->interdire_scripts = false; |
|
| 297 | 297 | |
| 298 | - return $p; |
|
| 298 | + return $p; |
|
| 299 | 299 | } |
| 300 | 300 | |
| 301 | 301 | /** |
@@ -314,10 +314,10 @@ discard block |
||
| 314 | 314 | * Pile completée du code PHP d'exécution de la balise |
| 315 | 315 | */ |
| 316 | 316 | function balise_DATE_MODIF_dist($p) { |
| 317 | - $p->code = champ_sql('date_modif', $p); |
|
| 318 | - $p->interdire_scripts = false; |
|
| 317 | + $p->code = champ_sql('date_modif', $p); |
|
| 318 | + $p->interdire_scripts = false; |
|
| 319 | 319 | |
| 320 | - return $p; |
|
| 320 | + return $p; |
|
| 321 | 321 | } |
| 322 | 322 | |
| 323 | 323 | /** |
@@ -335,13 +335,13 @@ discard block |
||
| 335 | 335 | * Pile completée du code PHP d'exécution de la balise |
| 336 | 336 | */ |
| 337 | 337 | function balise_DATE_NOUVEAUTES_dist($p) { |
| 338 | - $p->code = "((\$GLOBALS['meta']['quoi_de_neuf'] == 'oui' |
|
| 338 | + $p->code = "((\$GLOBALS['meta']['quoi_de_neuf'] == 'oui' |
|
| 339 | 339 | AND isset(\$GLOBALS['meta']['dernier_envoi_neuf'])) ? |
| 340 | 340 | \$GLOBALS['meta']['dernier_envoi_neuf'] : |
| 341 | 341 | \"'0000-00-00'\")"; |
| 342 | - $p->interdire_scripts = false; |
|
| 342 | + $p->interdire_scripts = false; |
|
| 343 | 343 | |
| 344 | - return $p; |
|
| 344 | + return $p; |
|
| 345 | 345 | } |
| 346 | 346 | |
| 347 | 347 | |
@@ -359,11 +359,11 @@ discard block |
||
| 359 | 359 | * Pile completée du code PHP d'exécution de la balise |
| 360 | 360 | */ |
| 361 | 361 | function balise_DOSSIER_SQUELETTE_dist($p) { |
| 362 | - $code = substr(addslashes(dirname($p->descr['sourcefile'])), strlen(_DIR_RACINE)); |
|
| 363 | - $p->code = "_DIR_RACINE . '$code'" . |
|
| 364 | - $p->interdire_scripts = false; |
|
| 362 | + $code = substr(addslashes(dirname($p->descr['sourcefile'])), strlen(_DIR_RACINE)); |
|
| 363 | + $p->code = "_DIR_RACINE . '$code'" . |
|
| 364 | + $p->interdire_scripts = false; |
|
| 365 | 365 | |
| 366 | - return $p; |
|
| 366 | + return $p; |
|
| 367 | 367 | } |
| 368 | 368 | |
| 369 | 369 | /** |
@@ -378,11 +378,11 @@ discard block |
||
| 378 | 378 | * Pile completée du code PHP d'exécution de la balise |
| 379 | 379 | */ |
| 380 | 380 | function balise_SQUELETTE_dist($p) { |
| 381 | - $code = addslashes($p->descr['sourcefile']); |
|
| 382 | - $p->code = "'$code'" . |
|
| 383 | - $p->interdire_scripts = false; |
|
| 381 | + $code = addslashes($p->descr['sourcefile']); |
|
| 382 | + $p->code = "'$code'" . |
|
| 383 | + $p->interdire_scripts = false; |
|
| 384 | 384 | |
| 385 | - return $p; |
|
| 385 | + return $p; |
|
| 386 | 386 | } |
| 387 | 387 | |
| 388 | 388 | /** |
@@ -401,10 +401,10 @@ discard block |
||
| 401 | 401 | * Pile completée du code PHP d'exécution de la balise |
| 402 | 402 | */ |
| 403 | 403 | function balise_SPIP_VERSION_dist($p) { |
| 404 | - $p->code = 'spip_version()'; |
|
| 405 | - $p->interdire_scripts = false; |
|
| 404 | + $p->code = 'spip_version()'; |
|
| 405 | + $p->interdire_scripts = false; |
|
| 406 | 406 | |
| 407 | - return $p; |
|
| 407 | + return $p; |
|
| 408 | 408 | } |
| 409 | 409 | |
| 410 | 410 | |
@@ -430,18 +430,18 @@ discard block |
||
| 430 | 430 | * Pile complétée par le code à générer |
| 431 | 431 | **/ |
| 432 | 432 | function balise_NOM_SITE_dist($p) { |
| 433 | - if (!$p->etoile) { |
|
| 434 | - $p->code = 'supprimer_numero(calculer_url(' . |
|
| 435 | - champ_sql('url_site', $p) . ',' . |
|
| 436 | - champ_sql('nom_site', $p) . |
|
| 437 | - ", 'titre', \$connect, false))"; |
|
| 438 | - } else { |
|
| 439 | - $p->code = champ_sql('nom_site', $p); |
|
| 440 | - } |
|
| 433 | + if (!$p->etoile) { |
|
| 434 | + $p->code = 'supprimer_numero(calculer_url(' . |
|
| 435 | + champ_sql('url_site', $p) . ',' . |
|
| 436 | + champ_sql('nom_site', $p) . |
|
| 437 | + ", 'titre', \$connect, false))"; |
|
| 438 | + } else { |
|
| 439 | + $p->code = champ_sql('nom_site', $p); |
|
| 440 | + } |
|
| 441 | 441 | |
| 442 | - $p->interdire_scripts = true; |
|
| 442 | + $p->interdire_scripts = true; |
|
| 443 | 443 | |
| 444 | - return $p; |
|
| 444 | + return $p; |
|
| 445 | 445 | } |
| 446 | 446 | |
| 447 | 447 | |
@@ -458,11 +458,11 @@ discard block |
||
| 458 | 458 | * Pile complétée par le code à générer |
| 459 | 459 | **/ |
| 460 | 460 | function balise_NOTES_dist($p) { |
| 461 | - // Recuperer les notes |
|
| 462 | - $p->code = 'calculer_notes()'; |
|
| 461 | + // Recuperer les notes |
|
| 462 | + $p->code = 'calculer_notes()'; |
|
| 463 | 463 | |
| 464 | - #$p->interdire_scripts = true; |
|
| 465 | - return $p; |
|
| 464 | + #$p->interdire_scripts = true; |
|
| 465 | + return $p; |
|
| 466 | 466 | } |
| 467 | 467 | |
| 468 | 468 | |
@@ -484,10 +484,10 @@ discard block |
||
| 484 | 484 | * Pile complétée par le code à générer |
| 485 | 485 | **/ |
| 486 | 486 | function balise_RECHERCHE_dist($p) { |
| 487 | - $p->code = 'entites_html(_request("recherche"))'; |
|
| 488 | - $p->interdire_scripts = false; |
|
| 487 | + $p->code = 'entites_html(_request("recherche"))'; |
|
| 488 | + $p->interdire_scripts = false; |
|
| 489 | 489 | |
| 490 | - return $p; |
|
| 490 | + return $p; |
|
| 491 | 491 | } |
| 492 | 492 | |
| 493 | 493 | |
@@ -505,18 +505,18 @@ discard block |
||
| 505 | 505 | * Pile complétée par le code à générer |
| 506 | 506 | **/ |
| 507 | 507 | function balise_COMPTEUR_BOUCLE_dist($p) { |
| 508 | - $b = index_boucle_mere($p); |
|
| 509 | - if ($b === '') { |
|
| 510 | - $msg = ['zbug_champ_hors_boucle', ['champ' => zbug_presenter_champ($p)]]; |
|
| 511 | - erreur_squelette($msg, $p); |
|
| 512 | - return null; |
|
| 513 | - } else { |
|
| 514 | - $p->code = "(\$Numrows['$b']['compteur_boucle'] ?? 0)"; |
|
| 515 | - $p->boucles[$b]->cptrows = true; |
|
| 516 | - $p->interdire_scripts = false; |
|
| 508 | + $b = index_boucle_mere($p); |
|
| 509 | + if ($b === '') { |
|
| 510 | + $msg = ['zbug_champ_hors_boucle', ['champ' => zbug_presenter_champ($p)]]; |
|
| 511 | + erreur_squelette($msg, $p); |
|
| 512 | + return null; |
|
| 513 | + } else { |
|
| 514 | + $p->code = "(\$Numrows['$b']['compteur_boucle'] ?? 0)"; |
|
| 515 | + $p->boucles[$b]->cptrows = true; |
|
| 516 | + $p->interdire_scripts = false; |
|
| 517 | 517 | |
| 518 | - return $p; |
|
| 519 | - } |
|
| 518 | + return $p; |
|
| 519 | + } |
|
| 520 | 520 | } |
| 521 | 521 | |
| 522 | 522 | /** |
@@ -534,17 +534,17 @@ discard block |
||
| 534 | 534 | * Pile complétée par le code à générer |
| 535 | 535 | **/ |
| 536 | 536 | function balise_TOTAL_BOUCLE_dist($p) { |
| 537 | - $b = index_boucle_mere($p); |
|
| 538 | - if ($b === '') { |
|
| 539 | - $msg = ['zbug_champ_hors_boucle', ['champ' => zbug_presenter_champ($p)]]; |
|
| 540 | - erreur_squelette($msg, $p); |
|
| 541 | - } else { |
|
| 542 | - $p->code = "(\$Numrows['$b']['total'] ?? 0)"; |
|
| 543 | - $p->boucles[$b]->numrows = true; |
|
| 544 | - $p->interdire_scripts = false; |
|
| 545 | - } |
|
| 537 | + $b = index_boucle_mere($p); |
|
| 538 | + if ($b === '') { |
|
| 539 | + $msg = ['zbug_champ_hors_boucle', ['champ' => zbug_presenter_champ($p)]]; |
|
| 540 | + erreur_squelette($msg, $p); |
|
| 541 | + } else { |
|
| 542 | + $p->code = "(\$Numrows['$b']['total'] ?? 0)"; |
|
| 543 | + $p->boucles[$b]->numrows = true; |
|
| 544 | + $p->interdire_scripts = false; |
|
| 545 | + } |
|
| 546 | 546 | |
| 547 | - return $p; |
|
| 547 | + return $p; |
|
| 548 | 548 | } |
| 549 | 549 | |
| 550 | 550 | |
@@ -564,7 +564,7 @@ discard block |
||
| 564 | 564 | * Pile complétée par le code à générer |
| 565 | 565 | **/ |
| 566 | 566 | function balise_POINTS_dist($p) { |
| 567 | - return rindex_pile($p, 'points', 'recherche'); |
|
| 567 | + return rindex_pile($p, 'points', 'recherche'); |
|
| 568 | 568 | } |
| 569 | 569 | |
| 570 | 570 | |
@@ -585,12 +585,12 @@ discard block |
||
| 585 | 585 | * Pile complétée par le code à générer |
| 586 | 586 | **/ |
| 587 | 587 | function balise_POPULARITE_ABSOLUE_dist($p) { |
| 588 | - $p->code = 'ceil(' . |
|
| 589 | - champ_sql('popularite', $p) . |
|
| 590 | - ')'; |
|
| 591 | - $p->interdire_scripts = false; |
|
| 588 | + $p->code = 'ceil(' . |
|
| 589 | + champ_sql('popularite', $p) . |
|
| 590 | + ')'; |
|
| 591 | + $p->interdire_scripts = false; |
|
| 592 | 592 | |
| 593 | - return $p; |
|
| 593 | + return $p; |
|
| 594 | 594 | } |
| 595 | 595 | |
| 596 | 596 | /** |
@@ -610,10 +610,10 @@ discard block |
||
| 610 | 610 | * Pile complétée par le code à générer |
| 611 | 611 | **/ |
| 612 | 612 | function balise_POPULARITE_SITE_dist($p) { |
| 613 | - $p->code = 'ceil($GLOBALS["meta"][\'popularite_total\'])'; |
|
| 614 | - $p->interdire_scripts = false; |
|
| 613 | + $p->code = 'ceil($GLOBALS["meta"][\'popularite_total\'])'; |
|
| 614 | + $p->interdire_scripts = false; |
|
| 615 | 615 | |
| 616 | - return $p; |
|
| 616 | + return $p; |
|
| 617 | 617 | } |
| 618 | 618 | |
| 619 | 619 | /** |
@@ -634,10 +634,10 @@ discard block |
||
| 634 | 634 | * Pile complétée par le code à générer |
| 635 | 635 | **/ |
| 636 | 636 | function balise_POPULARITE_MAX_dist($p) { |
| 637 | - $p->code = 'ceil($GLOBALS["meta"][\'popularite_max\'])'; |
|
| 638 | - $p->interdire_scripts = false; |
|
| 637 | + $p->code = 'ceil($GLOBALS["meta"][\'popularite_max\'])'; |
|
| 638 | + $p->interdire_scripts = false; |
|
| 639 | 639 | |
| 640 | - return $p; |
|
| 640 | + return $p; |
|
| 641 | 641 | } |
| 642 | 642 | |
| 643 | 643 | |
@@ -663,15 +663,15 @@ discard block |
||
| 663 | 663 | * Pile complétée par le code à générer |
| 664 | 664 | **/ |
| 665 | 665 | function balise_VALEUR_dist($p) { |
| 666 | - $b = $p->nom_boucle ?: $p->id_boucle; |
|
| 667 | - $p->code = index_pile($p->id_boucle, 'valeur', $p->boucles, $b); |
|
| 666 | + $b = $p->nom_boucle ?: $p->id_boucle; |
|
| 667 | + $p->code = index_pile($p->id_boucle, 'valeur', $p->boucles, $b); |
|
| 668 | 668 | ; |
| 669 | - if (($v = interprete_argument_balise(1, $p)) !== null) { |
|
| 670 | - $p->code = 'table_valeur(' . $p->code . ', ' . $v . ')'; |
|
| 671 | - } |
|
| 672 | - $p->interdire_scripts = true; |
|
| 669 | + if (($v = interprete_argument_balise(1, $p)) !== null) { |
|
| 670 | + $p->code = 'table_valeur(' . $p->code . ', ' . $v . ')'; |
|
| 671 | + } |
|
| 672 | + $p->interdire_scripts = true; |
|
| 673 | 673 | |
| 674 | - return $p; |
|
| 674 | + return $p; |
|
| 675 | 675 | } |
| 676 | 676 | |
| 677 | 677 | /** |
@@ -700,16 +700,16 @@ discard block |
||
| 700 | 700 | * Pile complétée par le code à générer |
| 701 | 701 | **/ |
| 702 | 702 | function balise_EXPOSE_dist($p) { |
| 703 | - $on = "'on'"; |
|
| 704 | - $off = "''"; |
|
| 705 | - if (($v = interprete_argument_balise(1, $p)) !== null) { |
|
| 706 | - $on = $v; |
|
| 707 | - if (($v = interprete_argument_balise(2, $p)) !== null) { |
|
| 708 | - $off = $v; |
|
| 709 | - } |
|
| 710 | - } |
|
| 703 | + $on = "'on'"; |
|
| 704 | + $off = "''"; |
|
| 705 | + if (($v = interprete_argument_balise(1, $p)) !== null) { |
|
| 706 | + $on = $v; |
|
| 707 | + if (($v = interprete_argument_balise(2, $p)) !== null) { |
|
| 708 | + $off = $v; |
|
| 709 | + } |
|
| 710 | + } |
|
| 711 | 711 | |
| 712 | - return calculer_balise_expose($p, $on, $off); |
|
| 712 | + return calculer_balise_expose($p, $on, $off); |
|
| 713 | 713 | } |
| 714 | 714 | |
| 715 | 715 | /** |
@@ -727,35 +727,35 @@ discard block |
||
| 727 | 727 | * Pile complétée par le code à générer |
| 728 | 728 | **/ |
| 729 | 729 | function calculer_balise_expose($p, $on, $off) { |
| 730 | - $b = index_boucle($p); |
|
| 731 | - if (empty($p->boucles[$b]->primary)) { |
|
| 732 | - $msg = ['zbug_champ_hors_boucle', ['champ' => zbug_presenter_champ($p)]]; |
|
| 733 | - erreur_squelette($msg, $p); |
|
| 734 | - } else { |
|
| 735 | - $key = $p->boucles[$b]->primary; |
|
| 736 | - $type = $p->boucles[$p->id_boucle]->primary; |
|
| 737 | - $desc = $p->boucles[$b]->show; |
|
| 738 | - $connect = sql_quote($p->boucles[$b]->sql_serveur); |
|
| 730 | + $b = index_boucle($p); |
|
| 731 | + if (empty($p->boucles[$b]->primary)) { |
|
| 732 | + $msg = ['zbug_champ_hors_boucle', ['champ' => zbug_presenter_champ($p)]]; |
|
| 733 | + erreur_squelette($msg, $p); |
|
| 734 | + } else { |
|
| 735 | + $key = $p->boucles[$b]->primary; |
|
| 736 | + $type = $p->boucles[$p->id_boucle]->primary; |
|
| 737 | + $desc = $p->boucles[$b]->show; |
|
| 738 | + $connect = sql_quote($p->boucles[$b]->sql_serveur); |
|
| 739 | 739 | |
| 740 | - // Ne pas utiliser champ_sql, on jongle avec le nom boucle explicite |
|
| 741 | - $c = index_pile($p->id_boucle, $type, $p->boucles); |
|
| 740 | + // Ne pas utiliser champ_sql, on jongle avec le nom boucle explicite |
|
| 741 | + $c = index_pile($p->id_boucle, $type, $p->boucles); |
|
| 742 | 742 | |
| 743 | - if (isset($desc['field']['id_parent'])) { |
|
| 744 | - $parent = 0; // pour if (!$parent) dans calculer_expose |
|
| 745 | - } elseif (isset($desc['field']['id_rubrique'])) { |
|
| 746 | - $parent = index_pile($p->id_boucle, 'id_rubrique', $p->boucles, $b); |
|
| 747 | - } elseif (isset($desc['field']['id_groupe'])) { |
|
| 748 | - $parent = index_pile($p->id_boucle, 'id_groupe', $p->boucles, $b); |
|
| 749 | - } else { |
|
| 750 | - $parent = "''"; |
|
| 751 | - } |
|
| 743 | + if (isset($desc['field']['id_parent'])) { |
|
| 744 | + $parent = 0; // pour if (!$parent) dans calculer_expose |
|
| 745 | + } elseif (isset($desc['field']['id_rubrique'])) { |
|
| 746 | + $parent = index_pile($p->id_boucle, 'id_rubrique', $p->boucles, $b); |
|
| 747 | + } elseif (isset($desc['field']['id_groupe'])) { |
|
| 748 | + $parent = index_pile($p->id_boucle, 'id_groupe', $p->boucles, $b); |
|
| 749 | + } else { |
|
| 750 | + $parent = "''"; |
|
| 751 | + } |
|
| 752 | 752 | |
| 753 | - $p->code = "(calcul_exposer($c, '$type', \$Pile[0], $parent, '$key', $connect) ? $on : $off)"; |
|
| 754 | - } |
|
| 753 | + $p->code = "(calcul_exposer($c, '$type', \$Pile[0], $parent, '$key', $connect) ? $on : $off)"; |
|
| 754 | + } |
|
| 755 | 755 | |
| 756 | - $p->interdire_scripts = false; |
|
| 756 | + $p->interdire_scripts = false; |
|
| 757 | 757 | |
| 758 | - return $p; |
|
| 758 | + return $p; |
|
| 759 | 759 | } |
| 760 | 760 | |
| 761 | 761 | |
@@ -796,46 +796,46 @@ discard block |
||
| 796 | 796 | **/ |
| 797 | 797 | function balise_INTRODUCTION_dist($p) { |
| 798 | 798 | |
| 799 | - $type_objet = $p->type_requete; |
|
| 800 | - $cle_objet = id_table_objet($type_objet); |
|
| 801 | - $_id_objet = champ_sql($cle_objet, $p); |
|
| 802 | - |
|
| 803 | - // Récupérer les valeurs sql nécessaires : descriptif, texte et chapo |
|
| 804 | - // ainsi que le longueur d'introduction donnée dans la description de l'objet. |
|
| 805 | - $_introduction_longueur = 'null'; |
|
| 806 | - $_ligne = 'array('; |
|
| 807 | - $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 808 | - if ($desc = $trouver_table(table_objet_sql($type_objet))) { |
|
| 809 | - if (isset($desc['field']['descriptif'])) { |
|
| 810 | - $_ligne .= "'descriptif' => " . champ_sql('descriptif', $p) . ','; |
|
| 811 | - } |
|
| 812 | - if (isset($desc['field']['texte'])) { |
|
| 813 | - $_ligne .= "'texte' => " . champ_sql('texte', $p) . ','; |
|
| 814 | - } |
|
| 815 | - if (isset($desc['field']['chapo'])) { |
|
| 816 | - $_ligne .= "'chapo' => " . champ_sql('chapo', $p) . ','; |
|
| 817 | - } |
|
| 818 | - if (isset($desc['introduction_longueur'])) { |
|
| 819 | - $_introduction_longueur = "'" . $desc['introduction_longueur'] . "'"; |
|
| 820 | - } |
|
| 821 | - } |
|
| 822 | - $_ligne .= ')'; |
|
| 823 | - |
|
| 824 | - // Récupérer la longueur et la suite passés en paramètres |
|
| 825 | - $_longueur_ou_suite = 'null'; |
|
| 826 | - if (($v1 = interprete_argument_balise(1, $p)) !== null) { |
|
| 827 | - $_longueur_ou_suite = $v1; |
|
| 828 | - } |
|
| 829 | - $_suite = 'null'; |
|
| 830 | - if (($v2 = interprete_argument_balise(2, $p)) !== null) { |
|
| 831 | - $_suite = $v2; |
|
| 832 | - } |
|
| 833 | - |
|
| 834 | - $p->code = "generer_objet_introduction($_id_objet, '$type_objet', $_ligne, $_introduction_longueur, $_longueur_ou_suite, $_suite, \$connect)"; |
|
| 835 | - |
|
| 836 | - #$p->interdire_scripts = true; |
|
| 837 | - $p->etoile = '*'; // propre est deja fait dans le calcul de l'intro |
|
| 838 | - return $p; |
|
| 799 | + $type_objet = $p->type_requete; |
|
| 800 | + $cle_objet = id_table_objet($type_objet); |
|
| 801 | + $_id_objet = champ_sql($cle_objet, $p); |
|
| 802 | + |
|
| 803 | + // Récupérer les valeurs sql nécessaires : descriptif, texte et chapo |
|
| 804 | + // ainsi que le longueur d'introduction donnée dans la description de l'objet. |
|
| 805 | + $_introduction_longueur = 'null'; |
|
| 806 | + $_ligne = 'array('; |
|
| 807 | + $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 808 | + if ($desc = $trouver_table(table_objet_sql($type_objet))) { |
|
| 809 | + if (isset($desc['field']['descriptif'])) { |
|
| 810 | + $_ligne .= "'descriptif' => " . champ_sql('descriptif', $p) . ','; |
|
| 811 | + } |
|
| 812 | + if (isset($desc['field']['texte'])) { |
|
| 813 | + $_ligne .= "'texte' => " . champ_sql('texte', $p) . ','; |
|
| 814 | + } |
|
| 815 | + if (isset($desc['field']['chapo'])) { |
|
| 816 | + $_ligne .= "'chapo' => " . champ_sql('chapo', $p) . ','; |
|
| 817 | + } |
|
| 818 | + if (isset($desc['introduction_longueur'])) { |
|
| 819 | + $_introduction_longueur = "'" . $desc['introduction_longueur'] . "'"; |
|
| 820 | + } |
|
| 821 | + } |
|
| 822 | + $_ligne .= ')'; |
|
| 823 | + |
|
| 824 | + // Récupérer la longueur et la suite passés en paramètres |
|
| 825 | + $_longueur_ou_suite = 'null'; |
|
| 826 | + if (($v1 = interprete_argument_balise(1, $p)) !== null) { |
|
| 827 | + $_longueur_ou_suite = $v1; |
|
| 828 | + } |
|
| 829 | + $_suite = 'null'; |
|
| 830 | + if (($v2 = interprete_argument_balise(2, $p)) !== null) { |
|
| 831 | + $_suite = $v2; |
|
| 832 | + } |
|
| 833 | + |
|
| 834 | + $p->code = "generer_objet_introduction($_id_objet, '$type_objet', $_ligne, $_introduction_longueur, $_longueur_ou_suite, $_suite, \$connect)"; |
|
| 835 | + |
|
| 836 | + #$p->interdire_scripts = true; |
|
| 837 | + $p->etoile = '*'; // propre est deja fait dans le calcul de l'intro |
|
| 838 | + return $p; |
|
| 839 | 839 | } |
| 840 | 840 | |
| 841 | 841 | |
@@ -855,15 +855,15 @@ discard block |
||
| 855 | 855 | * Pile complétée par le code à générer |
| 856 | 856 | **/ |
| 857 | 857 | function balise_LANG_dist($p) { |
| 858 | - $_lang = champ_sql('lang', $p); |
|
| 859 | - if (!$p->etoile) { |
|
| 860 | - $p->code = "spip_htmlentities($_lang ? $_lang : \$GLOBALS['spip_lang'])"; |
|
| 861 | - } else { |
|
| 862 | - $p->code = "spip_htmlentities($_lang)"; |
|
| 863 | - } |
|
| 864 | - $p->interdire_scripts = false; |
|
| 858 | + $_lang = champ_sql('lang', $p); |
|
| 859 | + if (!$p->etoile) { |
|
| 860 | + $p->code = "spip_htmlentities($_lang ? $_lang : \$GLOBALS['spip_lang'])"; |
|
| 861 | + } else { |
|
| 862 | + $p->code = "spip_htmlentities($_lang)"; |
|
| 863 | + } |
|
| 864 | + $p->interdire_scripts = false; |
|
| 865 | 865 | |
| 866 | - return $p; |
|
| 866 | + return $p; |
|
| 867 | 867 | } |
| 868 | 868 | |
| 869 | 869 | /** |
@@ -885,45 +885,45 @@ discard block |
||
| 885 | 885 | * Pile complétée par le code à générer |
| 886 | 886 | */ |
| 887 | 887 | function balise_LESAUTEURS_dist($p) { |
| 888 | - // Cherche le champ 'lesauteurs' dans la pile |
|
| 889 | - $_lesauteurs = champ_sql('lesauteurs', $p, ''); |
|
| 890 | - |
|
| 891 | - // Si le champ n'existe pas (cas de spip_articles), on applique |
|
| 892 | - // le modele lesauteurs.html en passant id_article dans le contexte; |
|
| 893 | - // dans le cas contraire on prend le champ 'lesauteurs' |
|
| 894 | - // (cf extension sites/) |
|
| 895 | - if ($_lesauteurs) { |
|
| 896 | - $p->code = "safehtml($_lesauteurs)"; |
|
| 897 | - // $p->interdire_scripts = true; |
|
| 898 | - } else { |
|
| 899 | - if (!$p->id_boucle) { |
|
| 900 | - $connect = ''; |
|
| 901 | - $objet = 'article'; |
|
| 902 | - $id_table_objet = 'id_article'; |
|
| 903 | - } else { |
|
| 904 | - $b = $p->nom_boucle ?: $p->id_boucle; |
|
| 905 | - $connect = $p->boucles[$b]->sql_serveur; |
|
| 906 | - $type_boucle = $p->boucles[$b]->type_requete; |
|
| 907 | - $objet = objet_type($type_boucle); |
|
| 908 | - $id_table_objet = id_table_objet($type_boucle); |
|
| 909 | - } |
|
| 910 | - $c = memoriser_contexte_compil($p); |
|
| 911 | - |
|
| 912 | - $p->code = sprintf( |
|
| 913 | - CODE_RECUPERER_FOND, |
|
| 914 | - "'modeles/lesauteurs'", |
|
| 915 | - "array('objet'=>'" . $objet . |
|
| 916 | - "','id_objet' => " . champ_sql($id_table_objet, $p) . |
|
| 917 | - ",'$id_table_objet' => " . champ_sql($id_table_objet, $p) . |
|
| 918 | - ($objet == 'article' ? '' : ",'id_article' => " . champ_sql('id_article', $p)) . |
|
| 919 | - ')', |
|
| 920 | - "'trim'=>true, 'compil'=>array($c)", |
|
| 921 | - _q($connect) |
|
| 922 | - ); |
|
| 923 | - $p->interdire_scripts = false; // securite apposee par recuperer_fond() |
|
| 924 | - } |
|
| 925 | - |
|
| 926 | - return $p; |
|
| 888 | + // Cherche le champ 'lesauteurs' dans la pile |
|
| 889 | + $_lesauteurs = champ_sql('lesauteurs', $p, ''); |
|
| 890 | + |
|
| 891 | + // Si le champ n'existe pas (cas de spip_articles), on applique |
|
| 892 | + // le modele lesauteurs.html en passant id_article dans le contexte; |
|
| 893 | + // dans le cas contraire on prend le champ 'lesauteurs' |
|
| 894 | + // (cf extension sites/) |
|
| 895 | + if ($_lesauteurs) { |
|
| 896 | + $p->code = "safehtml($_lesauteurs)"; |
|
| 897 | + // $p->interdire_scripts = true; |
|
| 898 | + } else { |
|
| 899 | + if (!$p->id_boucle) { |
|
| 900 | + $connect = ''; |
|
| 901 | + $objet = 'article'; |
|
| 902 | + $id_table_objet = 'id_article'; |
|
| 903 | + } else { |
|
| 904 | + $b = $p->nom_boucle ?: $p->id_boucle; |
|
| 905 | + $connect = $p->boucles[$b]->sql_serveur; |
|
| 906 | + $type_boucle = $p->boucles[$b]->type_requete; |
|
| 907 | + $objet = objet_type($type_boucle); |
|
| 908 | + $id_table_objet = id_table_objet($type_boucle); |
|
| 909 | + } |
|
| 910 | + $c = memoriser_contexte_compil($p); |
|
| 911 | + |
|
| 912 | + $p->code = sprintf( |
|
| 913 | + CODE_RECUPERER_FOND, |
|
| 914 | + "'modeles/lesauteurs'", |
|
| 915 | + "array('objet'=>'" . $objet . |
|
| 916 | + "','id_objet' => " . champ_sql($id_table_objet, $p) . |
|
| 917 | + ",'$id_table_objet' => " . champ_sql($id_table_objet, $p) . |
|
| 918 | + ($objet == 'article' ? '' : ",'id_article' => " . champ_sql('id_article', $p)) . |
|
| 919 | + ')', |
|
| 920 | + "'trim'=>true, 'compil'=>array($c)", |
|
| 921 | + _q($connect) |
|
| 922 | + ); |
|
| 923 | + $p->interdire_scripts = false; // securite apposee par recuperer_fond() |
|
| 924 | + } |
|
| 925 | + |
|
| 926 | + return $p; |
|
| 927 | 927 | } |
| 928 | 928 | |
| 929 | 929 | |
@@ -950,76 +950,76 @@ discard block |
||
| 950 | 950 | * Pile complétée par le code à générer |
| 951 | 951 | */ |
| 952 | 952 | function balise_RANG_dist($p) { |
| 953 | - $b = index_boucle($p); |
|
| 954 | - if ($b === '') { |
|
| 955 | - $msg = [ |
|
| 956 | - 'zbug_champ_hors_boucle', |
|
| 957 | - ['champ' => '#RANG'] |
|
| 958 | - ]; |
|
| 959 | - erreur_squelette($msg, $p); |
|
| 960 | - } else { |
|
| 961 | - // chercher d'abord un champ sql rang (mais pas dans le env : defaut '' si on trouve pas de champ sql) |
|
| 962 | - // dans la boucle immediatement englobante uniquement |
|
| 963 | - // sinon on compose le champ calcule |
|
| 964 | - $_rang = champ_sql('rang', $p, '', false); |
|
| 965 | - |
|
| 966 | - // si pas trouve de champ sql rang : |
|
| 967 | - if (!$_rang or $_rang == "''") { |
|
| 968 | - $boucle = &$p->boucles[$b]; |
|
| 969 | - |
|
| 970 | - // on gere le cas ou #RANG est une extraction du numero dans le titre |
|
| 971 | - $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 972 | - $desc = $trouver_table($boucle->id_table); |
|
| 973 | - $_titre = ''; # où extraire le numero ? |
|
| 974 | - |
|
| 975 | - if (isset($desc['titre'])) { |
|
| 976 | - $t = $desc['titre']; |
|
| 977 | - if ( |
|
| 978 | - // Soit on trouve avec la déclaration de la lang AVANT |
|
| 979 | - preg_match(';(?:lang\s*,)\s*(.*?titre)\s*(,|$);', $t, $m) |
|
| 980 | - // Soit on prend depuis le début |
|
| 981 | - or preg_match(';^(.*?titre)\s*(,|$);', $t, $m) |
|
| 982 | - ) { |
|
| 983 | - $m = preg_replace(',as\s+titre$,i', '', $m[1]); |
|
| 984 | - $m = trim($m); |
|
| 985 | - if ($m != "''") { |
|
| 986 | - if (!preg_match(',\W,', $m)) { |
|
| 987 | - $m = $boucle->id_table . ".$m"; |
|
| 988 | - } |
|
| 989 | - |
|
| 990 | - $m .= ' AS titre_rang'; |
|
| 991 | - |
|
| 992 | - $boucle->select[] = $m; |
|
| 993 | - $_titre = '$Pile[$SP][\'titre_rang\']'; |
|
| 994 | - } |
|
| 995 | - } |
|
| 996 | - } |
|
| 997 | - |
|
| 998 | - // si on n'a rien trouvé, on utilise le champ titre classique |
|
| 999 | - if (!$_titre) { |
|
| 1000 | - $_titre = champ_sql('titre', $p); |
|
| 1001 | - } |
|
| 1002 | - |
|
| 1003 | - // et on recupere aussi les infos de liaison si on est en train d'editer les liens justement |
|
| 1004 | - // cas des formulaires xxx_lies utilises par #FORMULAIRE_EDITER_LIENS |
|
| 1005 | - $type_boucle = $boucle->type_requete; |
|
| 1006 | - $objet = objet_type($type_boucle); |
|
| 1007 | - $id_table_objet = id_table_objet($type_boucle); |
|
| 1008 | - $_primary = champ_sql($id_table_objet, $p, '', false); |
|
| 1009 | - $_env = '$Pile[0]'; |
|
| 1010 | - |
|
| 1011 | - if (!$_titre) {$_titre = "''"; |
|
| 1012 | - } |
|
| 1013 | - if (!$_primary) {$_primary = "''"; |
|
| 1014 | - } |
|
| 1015 | - $_rang = "calculer_rang_smart($_titre, '$objet', $_primary, $_env)"; |
|
| 1016 | - } |
|
| 1017 | - |
|
| 1018 | - $p->code = $_rang; |
|
| 1019 | - $p->interdire_scripts = false; |
|
| 1020 | - } |
|
| 1021 | - |
|
| 1022 | - return $p; |
|
| 953 | + $b = index_boucle($p); |
|
| 954 | + if ($b === '') { |
|
| 955 | + $msg = [ |
|
| 956 | + 'zbug_champ_hors_boucle', |
|
| 957 | + ['champ' => '#RANG'] |
|
| 958 | + ]; |
|
| 959 | + erreur_squelette($msg, $p); |
|
| 960 | + } else { |
|
| 961 | + // chercher d'abord un champ sql rang (mais pas dans le env : defaut '' si on trouve pas de champ sql) |
|
| 962 | + // dans la boucle immediatement englobante uniquement |
|
| 963 | + // sinon on compose le champ calcule |
|
| 964 | + $_rang = champ_sql('rang', $p, '', false); |
|
| 965 | + |
|
| 966 | + // si pas trouve de champ sql rang : |
|
| 967 | + if (!$_rang or $_rang == "''") { |
|
| 968 | + $boucle = &$p->boucles[$b]; |
|
| 969 | + |
|
| 970 | + // on gere le cas ou #RANG est une extraction du numero dans le titre |
|
| 971 | + $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 972 | + $desc = $trouver_table($boucle->id_table); |
|
| 973 | + $_titre = ''; # où extraire le numero ? |
|
| 974 | + |
|
| 975 | + if (isset($desc['titre'])) { |
|
| 976 | + $t = $desc['titre']; |
|
| 977 | + if ( |
|
| 978 | + // Soit on trouve avec la déclaration de la lang AVANT |
|
| 979 | + preg_match(';(?:lang\s*,)\s*(.*?titre)\s*(,|$);', $t, $m) |
|
| 980 | + // Soit on prend depuis le début |
|
| 981 | + or preg_match(';^(.*?titre)\s*(,|$);', $t, $m) |
|
| 982 | + ) { |
|
| 983 | + $m = preg_replace(',as\s+titre$,i', '', $m[1]); |
|
| 984 | + $m = trim($m); |
|
| 985 | + if ($m != "''") { |
|
| 986 | + if (!preg_match(',\W,', $m)) { |
|
| 987 | + $m = $boucle->id_table . ".$m"; |
|
| 988 | + } |
|
| 989 | + |
|
| 990 | + $m .= ' AS titre_rang'; |
|
| 991 | + |
|
| 992 | + $boucle->select[] = $m; |
|
| 993 | + $_titre = '$Pile[$SP][\'titre_rang\']'; |
|
| 994 | + } |
|
| 995 | + } |
|
| 996 | + } |
|
| 997 | + |
|
| 998 | + // si on n'a rien trouvé, on utilise le champ titre classique |
|
| 999 | + if (!$_titre) { |
|
| 1000 | + $_titre = champ_sql('titre', $p); |
|
| 1001 | + } |
|
| 1002 | + |
|
| 1003 | + // et on recupere aussi les infos de liaison si on est en train d'editer les liens justement |
|
| 1004 | + // cas des formulaires xxx_lies utilises par #FORMULAIRE_EDITER_LIENS |
|
| 1005 | + $type_boucle = $boucle->type_requete; |
|
| 1006 | + $objet = objet_type($type_boucle); |
|
| 1007 | + $id_table_objet = id_table_objet($type_boucle); |
|
| 1008 | + $_primary = champ_sql($id_table_objet, $p, '', false); |
|
| 1009 | + $_env = '$Pile[0]'; |
|
| 1010 | + |
|
| 1011 | + if (!$_titre) {$_titre = "''"; |
|
| 1012 | + } |
|
| 1013 | + if (!$_primary) {$_primary = "''"; |
|
| 1014 | + } |
|
| 1015 | + $_rang = "calculer_rang_smart($_titre, '$objet', $_primary, $_env)"; |
|
| 1016 | + } |
|
| 1017 | + |
|
| 1018 | + $p->code = $_rang; |
|
| 1019 | + $p->interdire_scripts = false; |
|
| 1020 | + } |
|
| 1021 | + |
|
| 1022 | + return $p; |
|
| 1023 | 1023 | } |
| 1024 | 1024 | |
| 1025 | 1025 | |
@@ -1041,12 +1041,12 @@ discard block |
||
| 1041 | 1041 | * Pile complétée par le code à générer |
| 1042 | 1042 | **/ |
| 1043 | 1043 | function balise_POPULARITE_dist($p) { |
| 1044 | - $_popularite = champ_sql('popularite', $p); |
|
| 1045 | - $p->code = "(ceil(min(100, 100 * $_popularite |
|
| 1044 | + $_popularite = champ_sql('popularite', $p); |
|
| 1045 | + $p->code = "(ceil(min(100, 100 * $_popularite |
|
| 1046 | 1046 | / max(1 , 0 + \$GLOBALS['meta']['popularite_max']))))"; |
| 1047 | - $p->interdire_scripts = false; |
|
| 1047 | + $p->interdire_scripts = false; |
|
| 1048 | 1048 | |
| 1049 | - return $p; |
|
| 1049 | + return $p; |
|
| 1050 | 1050 | } |
| 1051 | 1051 | |
| 1052 | 1052 | /** |
@@ -1057,8 +1057,8 @@ discard block |
||
| 1057 | 1057 | * l'absence peut-être due à une faute de frappe dans le contexte inclus. |
| 1058 | 1058 | */ |
| 1059 | 1059 | define( |
| 1060 | - 'CODE_PAGINATION', |
|
| 1061 | - '%s($Numrows["%s"]["grand_total"], |
|
| 1060 | + 'CODE_PAGINATION', |
|
| 1061 | + '%s($Numrows["%s"]["grand_total"], |
|
| 1062 | 1062 | %s, |
| 1063 | 1063 | isset($Pile[0][%4$s])?$Pile[0][%4$s]:intval(_request(%4$s)), |
| 1064 | 1064 | %5$s, %6$s, %7$s, %8$s, array(%9$s))' |
@@ -1095,75 +1095,75 @@ discard block |
||
| 1095 | 1095 | * Pile complétée par le code à générer |
| 1096 | 1096 | */ |
| 1097 | 1097 | function balise_PAGINATION_dist($p, $liste = 'true') { |
| 1098 | - $b = index_boucle_mere($p); |
|
| 1099 | - |
|
| 1100 | - // s'il n'y a pas de nom de boucle, on ne peut pas paginer |
|
| 1101 | - if ($b === '') { |
|
| 1102 | - $msg = [ |
|
| 1103 | - 'zbug_champ_hors_boucle', |
|
| 1104 | - ['champ' => $liste ? 'PAGINATION' : 'ANCRE_PAGINATION'] |
|
| 1105 | - ]; |
|
| 1106 | - erreur_squelette($msg, $p); |
|
| 1107 | - |
|
| 1108 | - return $p; |
|
| 1109 | - } |
|
| 1110 | - |
|
| 1111 | - // s'il n'y a pas de mode_partie, c'est qu'on se trouve |
|
| 1112 | - // dans un boucle recursive ou qu'on a oublie le critere {pagination} |
|
| 1113 | - if (!$p->boucles[$b]->mode_partie) { |
|
| 1114 | - if (!$p->boucles[$b]->table_optionnelle) { |
|
| 1115 | - $msg = [ |
|
| 1116 | - 'zbug_pagination_sans_critere', |
|
| 1117 | - ['champ' => '#PAGINATION'] |
|
| 1118 | - ]; |
|
| 1119 | - erreur_squelette($msg, $p); |
|
| 1120 | - } |
|
| 1121 | - |
|
| 1122 | - return $p; |
|
| 1123 | - } |
|
| 1124 | - |
|
| 1125 | - // a priori true |
|
| 1126 | - // si false, le compilo va bloquer sur des syntaxes avec un filtre sans argument qui suit la balise |
|
| 1127 | - // si true, les arguments simples (sans truc=chose) vont degager |
|
| 1128 | - $_contexte = argumenter_inclure($p->param, true, $p, $p->boucles, $p->id_boucle, false, false); |
|
| 1129 | - if (is_countable($_contexte) ? count($_contexte) : 0) { |
|
| 1130 | - $key = key($_contexte); |
|
| 1131 | - if (is_numeric($key)) { |
|
| 1132 | - array_shift($_contexte); |
|
| 1133 | - $__modele = interprete_argument_balise(1, $p); |
|
| 1134 | - } |
|
| 1135 | - } |
|
| 1136 | - |
|
| 1137 | - if (is_countable($_contexte) ? count($_contexte) : 0) { |
|
| 1138 | - $code_contexte = implode(',', $_contexte); |
|
| 1139 | - } else { |
|
| 1140 | - $code_contexte = ''; |
|
| 1141 | - } |
|
| 1142 | - |
|
| 1143 | - $connect = $p->boucles[$b]->sql_serveur; |
|
| 1144 | - $pas = $p->boucles[$b]->total_parties; |
|
| 1145 | - $f_pagination = chercher_filtre('pagination'); |
|
| 1146 | - $type = $p->boucles[$b]->modificateur['debut_nom']; |
|
| 1147 | - $modif = ($type[0] !== "'") ? "'debut'.$type" |
|
| 1148 | - : ("'debut" . substr($type, 1)); |
|
| 1149 | - |
|
| 1150 | - $p->code = sprintf( |
|
| 1151 | - CODE_PAGINATION, |
|
| 1152 | - $f_pagination, |
|
| 1153 | - $b, |
|
| 1154 | - $type, |
|
| 1155 | - $modif, |
|
| 1156 | - $pas, |
|
| 1157 | - $liste, |
|
| 1158 | - ((isset($__modele) and $__modele) ? $__modele : "''"), |
|
| 1159 | - _q($connect), |
|
| 1160 | - $code_contexte |
|
| 1161 | - ); |
|
| 1162 | - |
|
| 1163 | - $p->boucles[$b]->numrows = true; |
|
| 1164 | - $p->interdire_scripts = false; |
|
| 1165 | - |
|
| 1166 | - return $p; |
|
| 1098 | + $b = index_boucle_mere($p); |
|
| 1099 | + |
|
| 1100 | + // s'il n'y a pas de nom de boucle, on ne peut pas paginer |
|
| 1101 | + if ($b === '') { |
|
| 1102 | + $msg = [ |
|
| 1103 | + 'zbug_champ_hors_boucle', |
|
| 1104 | + ['champ' => $liste ? 'PAGINATION' : 'ANCRE_PAGINATION'] |
|
| 1105 | + ]; |
|
| 1106 | + erreur_squelette($msg, $p); |
|
| 1107 | + |
|
| 1108 | + return $p; |
|
| 1109 | + } |
|
| 1110 | + |
|
| 1111 | + // s'il n'y a pas de mode_partie, c'est qu'on se trouve |
|
| 1112 | + // dans un boucle recursive ou qu'on a oublie le critere {pagination} |
|
| 1113 | + if (!$p->boucles[$b]->mode_partie) { |
|
| 1114 | + if (!$p->boucles[$b]->table_optionnelle) { |
|
| 1115 | + $msg = [ |
|
| 1116 | + 'zbug_pagination_sans_critere', |
|
| 1117 | + ['champ' => '#PAGINATION'] |
|
| 1118 | + ]; |
|
| 1119 | + erreur_squelette($msg, $p); |
|
| 1120 | + } |
|
| 1121 | + |
|
| 1122 | + return $p; |
|
| 1123 | + } |
|
| 1124 | + |
|
| 1125 | + // a priori true |
|
| 1126 | + // si false, le compilo va bloquer sur des syntaxes avec un filtre sans argument qui suit la balise |
|
| 1127 | + // si true, les arguments simples (sans truc=chose) vont degager |
|
| 1128 | + $_contexte = argumenter_inclure($p->param, true, $p, $p->boucles, $p->id_boucle, false, false); |
|
| 1129 | + if (is_countable($_contexte) ? count($_contexte) : 0) { |
|
| 1130 | + $key = key($_contexte); |
|
| 1131 | + if (is_numeric($key)) { |
|
| 1132 | + array_shift($_contexte); |
|
| 1133 | + $__modele = interprete_argument_balise(1, $p); |
|
| 1134 | + } |
|
| 1135 | + } |
|
| 1136 | + |
|
| 1137 | + if (is_countable($_contexte) ? count($_contexte) : 0) { |
|
| 1138 | + $code_contexte = implode(',', $_contexte); |
|
| 1139 | + } else { |
|
| 1140 | + $code_contexte = ''; |
|
| 1141 | + } |
|
| 1142 | + |
|
| 1143 | + $connect = $p->boucles[$b]->sql_serveur; |
|
| 1144 | + $pas = $p->boucles[$b]->total_parties; |
|
| 1145 | + $f_pagination = chercher_filtre('pagination'); |
|
| 1146 | + $type = $p->boucles[$b]->modificateur['debut_nom']; |
|
| 1147 | + $modif = ($type[0] !== "'") ? "'debut'.$type" |
|
| 1148 | + : ("'debut" . substr($type, 1)); |
|
| 1149 | + |
|
| 1150 | + $p->code = sprintf( |
|
| 1151 | + CODE_PAGINATION, |
|
| 1152 | + $f_pagination, |
|
| 1153 | + $b, |
|
| 1154 | + $type, |
|
| 1155 | + $modif, |
|
| 1156 | + $pas, |
|
| 1157 | + $liste, |
|
| 1158 | + ((isset($__modele) and $__modele) ? $__modele : "''"), |
|
| 1159 | + _q($connect), |
|
| 1160 | + $code_contexte |
|
| 1161 | + ); |
|
| 1162 | + |
|
| 1163 | + $p->boucles[$b]->numrows = true; |
|
| 1164 | + $p->interdire_scripts = false; |
|
| 1165 | + |
|
| 1166 | + return $p; |
|
| 1167 | 1167 | } |
| 1168 | 1168 | |
| 1169 | 1169 | |
@@ -1190,11 +1190,11 @@ discard block |
||
| 1190 | 1190 | * Pile complétée par le code à générer |
| 1191 | 1191 | **/ |
| 1192 | 1192 | function balise_ANCRE_PAGINATION_dist($p) { |
| 1193 | - if ($f = charger_fonction('PAGINATION', 'balise', true)) { |
|
| 1194 | - return $f($p, $liste = 'false'); |
|
| 1195 | - } else { |
|
| 1196 | - return null; |
|
| 1197 | - } // ou une erreur ? |
|
| 1193 | + if ($f = charger_fonction('PAGINATION', 'balise', true)) { |
|
| 1194 | + return $f($p, $liste = 'false'); |
|
| 1195 | + } else { |
|
| 1196 | + return null; |
|
| 1197 | + } // ou une erreur ? |
|
| 1198 | 1198 | } |
| 1199 | 1199 | |
| 1200 | 1200 | |
@@ -1215,17 +1215,17 @@ discard block |
||
| 1215 | 1215 | * Pile complétée par le code à générer |
| 1216 | 1216 | **/ |
| 1217 | 1217 | function balise_GRAND_TOTAL_dist($p) { |
| 1218 | - $b = index_boucle_mere($p); |
|
| 1219 | - if ($b === '') { |
|
| 1220 | - $msg = ['zbug_champ_hors_boucle', ['champ' => zbug_presenter_champ($p)]]; |
|
| 1221 | - erreur_squelette($msg, $p); |
|
| 1222 | - } else { |
|
| 1223 | - $p->code = "(\$Numrows['$b']['grand_total'] ?? \$Numrows['$b']['total'] ?? 0)"; |
|
| 1224 | - $p->boucles[$b]->numrows = true; |
|
| 1225 | - $p->interdire_scripts = false; |
|
| 1226 | - } |
|
| 1218 | + $b = index_boucle_mere($p); |
|
| 1219 | + if ($b === '') { |
|
| 1220 | + $msg = ['zbug_champ_hors_boucle', ['champ' => zbug_presenter_champ($p)]]; |
|
| 1221 | + erreur_squelette($msg, $p); |
|
| 1222 | + } else { |
|
| 1223 | + $p->code = "(\$Numrows['$b']['grand_total'] ?? \$Numrows['$b']['total'] ?? 0)"; |
|
| 1224 | + $p->boucles[$b]->numrows = true; |
|
| 1225 | + $p->interdire_scripts = false; |
|
| 1226 | + } |
|
| 1227 | 1227 | |
| 1228 | - return $p; |
|
| 1228 | + return $p; |
|
| 1229 | 1229 | } |
| 1230 | 1230 | |
| 1231 | 1231 | |
@@ -1253,10 +1253,10 @@ discard block |
||
| 1253 | 1253 | * Pile complétée par le code à générer |
| 1254 | 1254 | **/ |
| 1255 | 1255 | function balise_SELF_dist($p) { |
| 1256 | - $p->code = 'self()'; |
|
| 1257 | - $p->interdire_scripts = false; |
|
| 1256 | + $p->code = 'self()'; |
|
| 1257 | + $p->interdire_scripts = false; |
|
| 1258 | 1258 | |
| 1259 | - return $p; |
|
| 1259 | + return $p; |
|
| 1260 | 1260 | } |
| 1261 | 1261 | |
| 1262 | 1262 | |
@@ -1283,17 +1283,17 @@ discard block |
||
| 1283 | 1283 | * Pile complétée par le code à générer |
| 1284 | 1284 | **/ |
| 1285 | 1285 | function balise_CHEMIN_dist($p) { |
| 1286 | - $arg = interprete_argument_balise(1, $p); |
|
| 1287 | - if (!$arg) { |
|
| 1288 | - $msg = ['zbug_balise_sans_argument', ['balise' => ' CHEMIN']]; |
|
| 1289 | - erreur_squelette($msg, $p); |
|
| 1290 | - } else { |
|
| 1291 | - $p->code = 'find_in_path((string)' . $arg . ')'; |
|
| 1292 | - } |
|
| 1286 | + $arg = interprete_argument_balise(1, $p); |
|
| 1287 | + if (!$arg) { |
|
| 1288 | + $msg = ['zbug_balise_sans_argument', ['balise' => ' CHEMIN']]; |
|
| 1289 | + erreur_squelette($msg, $p); |
|
| 1290 | + } else { |
|
| 1291 | + $p->code = 'find_in_path((string)' . $arg . ')'; |
|
| 1292 | + } |
|
| 1293 | 1293 | |
| 1294 | - $p->interdire_scripts = false; |
|
| 1294 | + $p->interdire_scripts = false; |
|
| 1295 | 1295 | |
| 1296 | - return $p; |
|
| 1296 | + return $p; |
|
| 1297 | 1297 | } |
| 1298 | 1298 | |
| 1299 | 1299 | /** |
@@ -1318,16 +1318,16 @@ discard block |
||
| 1318 | 1318 | * Pile complétée par le code à générer |
| 1319 | 1319 | **/ |
| 1320 | 1320 | function balise_CHEMIN_IMAGE_dist($p) { |
| 1321 | - $arg = interprete_argument_balise(1, $p); |
|
| 1322 | - if (!$arg) { |
|
| 1323 | - $msg = ['zbug_balise_sans_argument', ['balise' => ' CHEMIN_IMAGE']]; |
|
| 1324 | - erreur_squelette($msg, $p); |
|
| 1325 | - } else { |
|
| 1326 | - $p->code = 'chemin_image((string)' . $arg . ')'; |
|
| 1327 | - } |
|
| 1321 | + $arg = interprete_argument_balise(1, $p); |
|
| 1322 | + if (!$arg) { |
|
| 1323 | + $msg = ['zbug_balise_sans_argument', ['balise' => ' CHEMIN_IMAGE']]; |
|
| 1324 | + erreur_squelette($msg, $p); |
|
| 1325 | + } else { |
|
| 1326 | + $p->code = 'chemin_image((string)' . $arg . ')'; |
|
| 1327 | + } |
|
| 1328 | 1328 | |
| 1329 | - $p->interdire_scripts = false; |
|
| 1330 | - return $p; |
|
| 1329 | + $p->interdire_scripts = false; |
|
| 1330 | + return $p; |
|
| 1331 | 1331 | } |
| 1332 | 1332 | |
| 1333 | 1333 | |
@@ -1365,36 +1365,36 @@ discard block |
||
| 1365 | 1365 | **/ |
| 1366 | 1366 | function balise_ENV_dist($p, $src = null) { |
| 1367 | 1367 | |
| 1368 | - // cle du tableau desiree |
|
| 1369 | - $_nom = interprete_argument_balise(1, $p); |
|
| 1370 | - // valeur par defaut |
|
| 1371 | - $_sinon = interprete_argument_balise(2, $p); |
|
| 1368 | + // cle du tableau desiree |
|
| 1369 | + $_nom = interprete_argument_balise(1, $p); |
|
| 1370 | + // valeur par defaut |
|
| 1371 | + $_sinon = interprete_argument_balise(2, $p); |
|
| 1372 | 1372 | |
| 1373 | - // $src est un tableau de donnees sources eventuellement transmis |
|
| 1374 | - // en absence, on utilise l'environnement du squelette $Pile[0] |
|
| 1373 | + // $src est un tableau de donnees sources eventuellement transmis |
|
| 1374 | + // en absence, on utilise l'environnement du squelette $Pile[0] |
|
| 1375 | 1375 | |
| 1376 | - if (!$_nom) { |
|
| 1377 | - // cas de #ENV sans argument : on retourne le serialize() du tableau |
|
| 1378 | - // une belle fonction [(#ENV|affiche_env)] serait pratique |
|
| 1379 | - if ($src) { |
|
| 1380 | - $p->code = '(is_array($a = (' . $src . ')) ? serialize($a) : "")'; |
|
| 1381 | - } else { |
|
| 1382 | - $p->code = 'serialize($Pile[0]??[])'; |
|
| 1383 | - } |
|
| 1384 | - } else { |
|
| 1385 | - if (!$src) { |
|
| 1386 | - $src = '$Pile[0]??[]'; |
|
| 1387 | - } |
|
| 1388 | - if ($_sinon) { |
|
| 1389 | - $p->code = "sinon(table_valeur($src, (string)$_nom, null), $_sinon)"; |
|
| 1390 | - } else { |
|
| 1391 | - $p->code = "table_valeur($src, (string)$_nom, null)"; |
|
| 1392 | - } |
|
| 1393 | - } |
|
| 1376 | + if (!$_nom) { |
|
| 1377 | + // cas de #ENV sans argument : on retourne le serialize() du tableau |
|
| 1378 | + // une belle fonction [(#ENV|affiche_env)] serait pratique |
|
| 1379 | + if ($src) { |
|
| 1380 | + $p->code = '(is_array($a = (' . $src . ')) ? serialize($a) : "")'; |
|
| 1381 | + } else { |
|
| 1382 | + $p->code = 'serialize($Pile[0]??[])'; |
|
| 1383 | + } |
|
| 1384 | + } else { |
|
| 1385 | + if (!$src) { |
|
| 1386 | + $src = '$Pile[0]??[]'; |
|
| 1387 | + } |
|
| 1388 | + if ($_sinon) { |
|
| 1389 | + $p->code = "sinon(table_valeur($src, (string)$_nom, null), $_sinon)"; |
|
| 1390 | + } else { |
|
| 1391 | + $p->code = "table_valeur($src, (string)$_nom, null)"; |
|
| 1392 | + } |
|
| 1393 | + } |
|
| 1394 | 1394 | |
| 1395 | - #$p->interdire_scripts = true; |
|
| 1395 | + #$p->interdire_scripts = true; |
|
| 1396 | 1396 | |
| 1397 | - return $p; |
|
| 1397 | + return $p; |
|
| 1398 | 1398 | } |
| 1399 | 1399 | |
| 1400 | 1400 | /** |
@@ -1424,16 +1424,16 @@ discard block |
||
| 1424 | 1424 | * Pile completée du code PHP d'exécution de la balise |
| 1425 | 1425 | */ |
| 1426 | 1426 | function balise_CONFIG_dist($p) { |
| 1427 | - if (!$arg = interprete_argument_balise(1, $p)) { |
|
| 1428 | - $arg = "''"; |
|
| 1429 | - } |
|
| 1430 | - $_sinon = interprete_argument_balise(2, $p); |
|
| 1431 | - $_unserialize = sinon(interprete_argument_balise(3, $p), 'false'); |
|
| 1427 | + if (!$arg = interprete_argument_balise(1, $p)) { |
|
| 1428 | + $arg = "''"; |
|
| 1429 | + } |
|
| 1430 | + $_sinon = interprete_argument_balise(2, $p); |
|
| 1431 | + $_unserialize = sinon(interprete_argument_balise(3, $p), 'false'); |
|
| 1432 | 1432 | |
| 1433 | - $p->code = '(include_spip(\'inc/config\')?lire_config(' . $arg . ',' . |
|
| 1434 | - ($_sinon && $_sinon != "''" ? $_sinon : 'null') . ',' . $_unserialize . "):'')"; |
|
| 1433 | + $p->code = '(include_spip(\'inc/config\')?lire_config(' . $arg . ',' . |
|
| 1434 | + ($_sinon && $_sinon != "''" ? $_sinon : 'null') . ',' . $_unserialize . "):'')"; |
|
| 1435 | 1435 | |
| 1436 | - return $p; |
|
| 1436 | + return $p; |
|
| 1437 | 1437 | } |
| 1438 | 1438 | |
| 1439 | 1439 | |
@@ -1456,10 +1456,10 @@ discard block |
||
| 1456 | 1456 | * Pile completée du code PHP d'exécution de la balise |
| 1457 | 1457 | */ |
| 1458 | 1458 | function balise_CONNECT_dist($p) { |
| 1459 | - $p->code = '($connect ? $connect : NULL)'; |
|
| 1460 | - $p->interdire_scripts = false; |
|
| 1459 | + $p->code = '($connect ? $connect : NULL)'; |
|
| 1460 | + $p->interdire_scripts = false; |
|
| 1461 | 1461 | |
| 1462 | - return $p; |
|
| 1462 | + return $p; |
|
| 1463 | 1463 | } |
| 1464 | 1464 | |
| 1465 | 1465 | |
@@ -1487,15 +1487,15 @@ discard block |
||
| 1487 | 1487 | * Pile completée du code PHP d'exécution de la balise |
| 1488 | 1488 | **/ |
| 1489 | 1489 | function balise_SESSION_dist($p) { |
| 1490 | - $p->descr['session'] = true; |
|
| 1490 | + $p->descr['session'] = true; |
|
| 1491 | 1491 | |
| 1492 | - $f = function_exists('balise_ENV') |
|
| 1493 | - ? 'balise_ENV' |
|
| 1494 | - : 'balise_ENV_dist'; |
|
| 1492 | + $f = function_exists('balise_ENV') |
|
| 1493 | + ? 'balise_ENV' |
|
| 1494 | + : 'balise_ENV_dist'; |
|
| 1495 | 1495 | |
| 1496 | - $p = $f($p, '$GLOBALS["visiteur_session"]??[]'); |
|
| 1496 | + $p = $f($p, '$GLOBALS["visiteur_session"]??[]'); |
|
| 1497 | 1497 | |
| 1498 | - return $p; |
|
| 1498 | + return $p; |
|
| 1499 | 1499 | } |
| 1500 | 1500 | |
| 1501 | 1501 | |
@@ -1518,18 +1518,18 @@ discard block |
||
| 1518 | 1518 | * Pile completée du code PHP d'exécution de la balise |
| 1519 | 1519 | **/ |
| 1520 | 1520 | function balise_SESSION_SET_dist($p) { |
| 1521 | - $_nom = interprete_argument_balise(1, $p); |
|
| 1522 | - $_val = interprete_argument_balise(2, $p); |
|
| 1523 | - if (!$_nom or !$_val) { |
|
| 1524 | - $err_b_s_a = ['zbug_balise_sans_argument', ['balise' => 'SESSION_SET']]; |
|
| 1525 | - erreur_squelette($err_b_s_a, $p); |
|
| 1526 | - } else { |
|
| 1527 | - $p->code = '(include_spip("inc/session") AND session_set(' . $_nom . ',' . $_val . '))'; |
|
| 1528 | - } |
|
| 1521 | + $_nom = interprete_argument_balise(1, $p); |
|
| 1522 | + $_val = interprete_argument_balise(2, $p); |
|
| 1523 | + if (!$_nom or !$_val) { |
|
| 1524 | + $err_b_s_a = ['zbug_balise_sans_argument', ['balise' => 'SESSION_SET']]; |
|
| 1525 | + erreur_squelette($err_b_s_a, $p); |
|
| 1526 | + } else { |
|
| 1527 | + $p->code = '(include_spip("inc/session") AND session_set(' . $_nom . ',' . $_val . '))'; |
|
| 1528 | + } |
|
| 1529 | 1529 | |
| 1530 | - $p->interdire_scripts = false; |
|
| 1530 | + $p->interdire_scripts = false; |
|
| 1531 | 1531 | |
| 1532 | - return $p; |
|
| 1532 | + return $p; |
|
| 1533 | 1533 | } |
| 1534 | 1534 | |
| 1535 | 1535 | |
@@ -1560,30 +1560,30 @@ discard block |
||
| 1560 | 1560 | * Pile completée du code PHP d'exécution de la balise |
| 1561 | 1561 | **/ |
| 1562 | 1562 | function balise_EVAL_dist($p) { |
| 1563 | - $php = interprete_argument_balise(1, $p); |
|
| 1564 | - if ($php) { |
|
| 1565 | - # optimisation sur les #EVAL{une expression sans #BALISE} |
|
| 1566 | - # attention au commentaire "// x signes" qui precede |
|
| 1567 | - if ( |
|
| 1568 | - preg_match( |
|
| 1569 | - ",^([[:space:]]*//[^\n]*\n)'([^']+)'$,ms", |
|
| 1570 | - $php, |
|
| 1571 | - $r |
|
| 1572 | - ) |
|
| 1573 | - ) { |
|
| 1574 | - $p->code = /* $r[1]. */ |
|
| 1575 | - '(' . $r[2] . ')'; |
|
| 1576 | - } else { |
|
| 1577 | - $p->code = "eval('return '.$php.';')"; |
|
| 1578 | - } |
|
| 1579 | - } else { |
|
| 1580 | - $msg = ['zbug_balise_sans_argument', ['balise' => ' EVAL']]; |
|
| 1581 | - erreur_squelette($msg, $p); |
|
| 1582 | - } |
|
| 1583 | - |
|
| 1584 | - #$p->interdire_scripts = true; |
|
| 1585 | - |
|
| 1586 | - return $p; |
|
| 1563 | + $php = interprete_argument_balise(1, $p); |
|
| 1564 | + if ($php) { |
|
| 1565 | + # optimisation sur les #EVAL{une expression sans #BALISE} |
|
| 1566 | + # attention au commentaire "// x signes" qui precede |
|
| 1567 | + if ( |
|
| 1568 | + preg_match( |
|
| 1569 | + ",^([[:space:]]*//[^\n]*\n)'([^']+)'$,ms", |
|
| 1570 | + $php, |
|
| 1571 | + $r |
|
| 1572 | + ) |
|
| 1573 | + ) { |
|
| 1574 | + $p->code = /* $r[1]. */ |
|
| 1575 | + '(' . $r[2] . ')'; |
|
| 1576 | + } else { |
|
| 1577 | + $p->code = "eval('return '.$php.';')"; |
|
| 1578 | + } |
|
| 1579 | + } else { |
|
| 1580 | + $msg = ['zbug_balise_sans_argument', ['balise' => ' EVAL']]; |
|
| 1581 | + erreur_squelette($msg, $p); |
|
| 1582 | + } |
|
| 1583 | + |
|
| 1584 | + #$p->interdire_scripts = true; |
|
| 1585 | + |
|
| 1586 | + return $p; |
|
| 1587 | 1587 | } |
| 1588 | 1588 | |
| 1589 | 1589 | |
@@ -1613,19 +1613,19 @@ discard block |
||
| 1613 | 1613 | **/ |
| 1614 | 1614 | function balise_CHAMP_SQL_dist($p) { |
| 1615 | 1615 | |
| 1616 | - if ( |
|
| 1617 | - $p->param |
|
| 1618 | - and isset($p->param[0][1][0]) |
|
| 1619 | - and $champ = ($p->param[0][1][0]->texte) |
|
| 1620 | - ) { |
|
| 1621 | - $p->code = champ_sql($champ, $p); |
|
| 1622 | - } else { |
|
| 1623 | - $err_b_s_a = ['zbug_balise_sans_argument', ['balise' => ' CHAMP_SQL']]; |
|
| 1624 | - erreur_squelette($err_b_s_a, $p); |
|
| 1625 | - } |
|
| 1616 | + if ( |
|
| 1617 | + $p->param |
|
| 1618 | + and isset($p->param[0][1][0]) |
|
| 1619 | + and $champ = ($p->param[0][1][0]->texte) |
|
| 1620 | + ) { |
|
| 1621 | + $p->code = champ_sql($champ, $p); |
|
| 1622 | + } else { |
|
| 1623 | + $err_b_s_a = ['zbug_balise_sans_argument', ['balise' => ' CHAMP_SQL']]; |
|
| 1624 | + erreur_squelette($err_b_s_a, $p); |
|
| 1625 | + } |
|
| 1626 | 1626 | |
| 1627 | - #$p->interdire_scripts = true; |
|
| 1628 | - return $p; |
|
| 1627 | + #$p->interdire_scripts = true; |
|
| 1628 | + return $p; |
|
| 1629 | 1629 | } |
| 1630 | 1630 | |
| 1631 | 1631 | /** |
@@ -1651,13 +1651,13 @@ discard block |
||
| 1651 | 1651 | * Pile complétée par le code à générer |
| 1652 | 1652 | **/ |
| 1653 | 1653 | function balise_VAL_dist($p) { |
| 1654 | - $p->code = interprete_argument_balise(1, $p) ?? ''; |
|
| 1655 | - if (!strlen($p->code)) { |
|
| 1656 | - $p->code = "''"; |
|
| 1657 | - } |
|
| 1658 | - $p->interdire_scripts = false; |
|
| 1654 | + $p->code = interprete_argument_balise(1, $p) ?? ''; |
|
| 1655 | + if (!strlen($p->code)) { |
|
| 1656 | + $p->code = "''"; |
|
| 1657 | + } |
|
| 1658 | + $p->interdire_scripts = false; |
|
| 1659 | 1659 | |
| 1660 | - return $p; |
|
| 1660 | + return $p; |
|
| 1661 | 1661 | } |
| 1662 | 1662 | |
| 1663 | 1663 | /** |
@@ -1686,10 +1686,10 @@ discard block |
||
| 1686 | 1686 | * Pile complétée par le code à générer |
| 1687 | 1687 | **/ |
| 1688 | 1688 | function balise_REM_dist($p) { |
| 1689 | - $p->code = "''"; |
|
| 1690 | - $p->interdire_scripts = false; |
|
| 1689 | + $p->code = "''"; |
|
| 1690 | + $p->interdire_scripts = false; |
|
| 1691 | 1691 | |
| 1692 | - return $p; |
|
| 1692 | + return $p; |
|
| 1693 | 1693 | } |
| 1694 | 1694 | |
| 1695 | 1695 | /** |
@@ -1699,10 +1699,10 @@ discard block |
||
| 1699 | 1699 | * @return mixed |
| 1700 | 1700 | */ |
| 1701 | 1701 | function balise_NULL_dist($p) { |
| 1702 | - $p->code = 'null'; |
|
| 1703 | - $p->interdire_scripts = false; |
|
| 1702 | + $p->code = 'null'; |
|
| 1703 | + $p->interdire_scripts = false; |
|
| 1704 | 1704 | |
| 1705 | - return $p; |
|
| 1705 | + return $p; |
|
| 1706 | 1706 | } |
| 1707 | 1707 | |
| 1708 | 1708 | |
@@ -1726,18 +1726,18 @@ discard block |
||
| 1726 | 1726 | **/ |
| 1727 | 1727 | function balise_HTTP_HEADER_dist($p) { |
| 1728 | 1728 | |
| 1729 | - $header = interprete_argument_balise(1, $p); |
|
| 1730 | - if (!$header) { |
|
| 1731 | - $err_b_s_a = ['zbug_balise_sans_argument', ['balise' => 'HTTP_HEADER']]; |
|
| 1732 | - erreur_squelette($err_b_s_a, $p); |
|
| 1733 | - } else { |
|
| 1734 | - $p->code = "'<'.'?php header(' . _q(" |
|
| 1735 | - . $header |
|
| 1736 | - . ") . '); ?'.'>'"; |
|
| 1737 | - } |
|
| 1738 | - $p->interdire_scripts = false; |
|
| 1729 | + $header = interprete_argument_balise(1, $p); |
|
| 1730 | + if (!$header) { |
|
| 1731 | + $err_b_s_a = ['zbug_balise_sans_argument', ['balise' => 'HTTP_HEADER']]; |
|
| 1732 | + erreur_squelette($err_b_s_a, $p); |
|
| 1733 | + } else { |
|
| 1734 | + $p->code = "'<'.'?php header(' . _q(" |
|
| 1735 | + . $header |
|
| 1736 | + . ") . '); ?'.'>'"; |
|
| 1737 | + } |
|
| 1738 | + $p->interdire_scripts = false; |
|
| 1739 | 1739 | |
| 1740 | - return $p; |
|
| 1740 | + return $p; |
|
| 1741 | 1741 | } |
| 1742 | 1742 | |
| 1743 | 1743 | |
@@ -1762,22 +1762,22 @@ discard block |
||
| 1762 | 1762 | * Pile complétée par le code à générer |
| 1763 | 1763 | **/ |
| 1764 | 1764 | function balise_FILTRE_dist($p) { |
| 1765 | - if ($p->param) { |
|
| 1766 | - $args = []; |
|
| 1767 | - foreach ($p->param as $i => $ignore) { |
|
| 1768 | - $args[] = interprete_argument_balise($i + 1, $p); |
|
| 1769 | - } |
|
| 1770 | - $p->code = "'<' . '" |
|
| 1771 | - . '?php header("X-Spip-Filtre: \'.' |
|
| 1772 | - . join('.\'|\'.', $args) |
|
| 1773 | - . " . '\"); ?'.'>'"; |
|
| 1765 | + if ($p->param) { |
|
| 1766 | + $args = []; |
|
| 1767 | + foreach ($p->param as $i => $ignore) { |
|
| 1768 | + $args[] = interprete_argument_balise($i + 1, $p); |
|
| 1769 | + } |
|
| 1770 | + $p->code = "'<' . '" |
|
| 1771 | + . '?php header("X-Spip-Filtre: \'.' |
|
| 1772 | + . join('.\'|\'.', $args) |
|
| 1773 | + . " . '\"); ?'.'>'"; |
|
| 1774 | 1774 | |
| 1775 | - $p->interdire_scripts = false; |
|
| 1775 | + $p->interdire_scripts = false; |
|
| 1776 | 1776 | |
| 1777 | - return $p; |
|
| 1778 | - } |
|
| 1777 | + return $p; |
|
| 1778 | + } |
|
| 1779 | 1779 | |
| 1780 | - return null; |
|
| 1780 | + return null; |
|
| 1781 | 1781 | } |
| 1782 | 1782 | |
| 1783 | 1783 | |
@@ -1813,55 +1813,55 @@ discard block |
||
| 1813 | 1813 | **/ |
| 1814 | 1814 | function balise_CACHE_dist($p) { |
| 1815 | 1815 | |
| 1816 | - if ($p->param) { |
|
| 1817 | - $duree = valeur_numerique($p->param[0][1][0]->texte); |
|
| 1818 | - |
|
| 1819 | - // noter la duree du cache dans un entete proprietaire |
|
| 1820 | - |
|
| 1821 | - $code = "'<'.'" . '?php header("X-Spip-Cache: ' |
|
| 1822 | - . $duree |
|
| 1823 | - . '"); ?' . "'.'>'"; |
|
| 1824 | - |
|
| 1825 | - // Remplir le header Cache-Control |
|
| 1826 | - // cas #CACHE{0} |
|
| 1827 | - if ($duree == 0) { |
|
| 1828 | - $code .= ".'<'.'" |
|
| 1829 | - . '?php header("Cache-Control: no-cache, must-revalidate"); ?' |
|
| 1830 | - . "'.'><'.'" |
|
| 1831 | - . '?php header("Pragma: no-cache"); ?' |
|
| 1832 | - . "'.'>'"; |
|
| 1833 | - } |
|
| 1834 | - |
|
| 1835 | - // recuperer les parametres suivants |
|
| 1836 | - $i = 1; |
|
| 1837 | - while (isset($p->param[0][++$i])) { |
|
| 1838 | - $pa = ($p->param[0][$i][0]->texte); |
|
| 1839 | - |
|
| 1840 | - if ( |
|
| 1841 | - $pa == 'cache-client' |
|
| 1842 | - and $duree > 0 |
|
| 1843 | - ) { |
|
| 1844 | - $code .= ".'<'.'" . '?php header("Cache-Control: max-age=' |
|
| 1845 | - . $duree |
|
| 1846 | - . '"); ?' . "'.'>'"; |
|
| 1847 | - // il semble logique, si on cache-client, de ne pas invalider |
|
| 1848 | - $pa = 'statique'; |
|
| 1849 | - } |
|
| 1850 | - |
|
| 1851 | - if ( |
|
| 1852 | - $pa == 'statique' |
|
| 1853 | - and $duree > 0 |
|
| 1854 | - ) { |
|
| 1855 | - $code .= ".'<'.'" . '?php header("X-Spip-Statique: oui"); ?' . "'.'>'"; |
|
| 1856 | - } |
|
| 1857 | - } |
|
| 1858 | - } else { |
|
| 1859 | - $code = "''"; |
|
| 1860 | - } |
|
| 1861 | - $p->code = $code; |
|
| 1862 | - $p->interdire_scripts = false; |
|
| 1863 | - |
|
| 1864 | - return $p; |
|
| 1816 | + if ($p->param) { |
|
| 1817 | + $duree = valeur_numerique($p->param[0][1][0]->texte); |
|
| 1818 | + |
|
| 1819 | + // noter la duree du cache dans un entete proprietaire |
|
| 1820 | + |
|
| 1821 | + $code = "'<'.'" . '?php header("X-Spip-Cache: ' |
|
| 1822 | + . $duree |
|
| 1823 | + . '"); ?' . "'.'>'"; |
|
| 1824 | + |
|
| 1825 | + // Remplir le header Cache-Control |
|
| 1826 | + // cas #CACHE{0} |
|
| 1827 | + if ($duree == 0) { |
|
| 1828 | + $code .= ".'<'.'" |
|
| 1829 | + . '?php header("Cache-Control: no-cache, must-revalidate"); ?' |
|
| 1830 | + . "'.'><'.'" |
|
| 1831 | + . '?php header("Pragma: no-cache"); ?' |
|
| 1832 | + . "'.'>'"; |
|
| 1833 | + } |
|
| 1834 | + |
|
| 1835 | + // recuperer les parametres suivants |
|
| 1836 | + $i = 1; |
|
| 1837 | + while (isset($p->param[0][++$i])) { |
|
| 1838 | + $pa = ($p->param[0][$i][0]->texte); |
|
| 1839 | + |
|
| 1840 | + if ( |
|
| 1841 | + $pa == 'cache-client' |
|
| 1842 | + and $duree > 0 |
|
| 1843 | + ) { |
|
| 1844 | + $code .= ".'<'.'" . '?php header("Cache-Control: max-age=' |
|
| 1845 | + . $duree |
|
| 1846 | + . '"); ?' . "'.'>'"; |
|
| 1847 | + // il semble logique, si on cache-client, de ne pas invalider |
|
| 1848 | + $pa = 'statique'; |
|
| 1849 | + } |
|
| 1850 | + |
|
| 1851 | + if ( |
|
| 1852 | + $pa == 'statique' |
|
| 1853 | + and $duree > 0 |
|
| 1854 | + ) { |
|
| 1855 | + $code .= ".'<'.'" . '?php header("X-Spip-Statique: oui"); ?' . "'.'>'"; |
|
| 1856 | + } |
|
| 1857 | + } |
|
| 1858 | + } else { |
|
| 1859 | + $code = "''"; |
|
| 1860 | + } |
|
| 1861 | + $p->code = $code; |
|
| 1862 | + $p->interdire_scripts = false; |
|
| 1863 | + |
|
| 1864 | + return $p; |
|
| 1865 | 1865 | } |
| 1866 | 1866 | |
| 1867 | 1867 | |
@@ -1893,13 +1893,13 @@ discard block |
||
| 1893 | 1893 | * Pile complétée par le code à générer |
| 1894 | 1894 | */ |
| 1895 | 1895 | function balise_INSERT_HEAD_dist($p) { |
| 1896 | - $p->code = "'<'.'" |
|
| 1897 | - . '?php header("X-Spip-Filtre: insert_head_css_conditionnel"); ?' |
|
| 1898 | - . "'.'>'"; |
|
| 1899 | - $p->code .= ". pipeline('insert_head','<!-- insert_head -->')"; |
|
| 1900 | - $p->interdire_scripts = false; |
|
| 1896 | + $p->code = "'<'.'" |
|
| 1897 | + . '?php header("X-Spip-Filtre: insert_head_css_conditionnel"); ?' |
|
| 1898 | + . "'.'>'"; |
|
| 1899 | + $p->code .= ". pipeline('insert_head','<!-- insert_head -->')"; |
|
| 1900 | + $p->interdire_scripts = false; |
|
| 1901 | 1901 | |
| 1902 | - return $p; |
|
| 1902 | + return $p; |
|
| 1903 | 1903 | } |
| 1904 | 1904 | |
| 1905 | 1905 | /** |
@@ -1917,10 +1917,10 @@ discard block |
||
| 1917 | 1917 | * Pile complétée par le code à générer |
| 1918 | 1918 | */ |
| 1919 | 1919 | function balise_INSERT_HEAD_CSS_dist($p) { |
| 1920 | - $p->code = "pipeline('insert_head_css','<!-- insert_head_css -->')"; |
|
| 1921 | - $p->interdire_scripts = false; |
|
| 1920 | + $p->code = "pipeline('insert_head_css','<!-- insert_head_css -->')"; |
|
| 1921 | + $p->interdire_scripts = false; |
|
| 1922 | 1922 | |
| 1923 | - return $p; |
|
| 1923 | + return $p; |
|
| 1924 | 1924 | } |
| 1925 | 1925 | |
| 1926 | 1926 | /** |
@@ -1935,11 +1935,11 @@ discard block |
||
| 1935 | 1935 | * Pile complétée par le code à générer |
| 1936 | 1936 | **/ |
| 1937 | 1937 | function balise_INCLUDE_dist($p) { |
| 1938 | - if (function_exists('balise_INCLURE')) { |
|
| 1939 | - return balise_INCLURE($p); |
|
| 1940 | - } else { |
|
| 1941 | - return balise_INCLURE_dist($p); |
|
| 1942 | - } |
|
| 1938 | + if (function_exists('balise_INCLURE')) { |
|
| 1939 | + return balise_INCLURE($p); |
|
| 1940 | + } else { |
|
| 1941 | + return balise_INCLURE_dist($p); |
|
| 1942 | + } |
|
| 1943 | 1943 | } |
| 1944 | 1944 | |
| 1945 | 1945 | /** |
@@ -1973,66 +1973,66 @@ discard block |
||
| 1973 | 1973 | * Pile complétée par le code à générer |
| 1974 | 1974 | **/ |
| 1975 | 1975 | function balise_INCLURE_dist($p) { |
| 1976 | - $id_boucle = $p->id_boucle; |
|
| 1977 | - // la lang n'est pas passe de facon automatique par argumenter |
|
| 1978 | - // mais le sera pas recuperer_fond, sauf si etoile=>true est passe |
|
| 1979 | - // en option |
|
| 1980 | - |
|
| 1981 | - $_contexte = argumenter_inclure($p->param, true, $p, $p->boucles, $id_boucle, false, false); |
|
| 1982 | - |
|
| 1983 | - // erreur de syntaxe = fond absent |
|
| 1984 | - // (2 messages d'erreur SPIP pour le prix d'un, mais pas d'erreur PHP |
|
| 1985 | - if (!$_contexte) { |
|
| 1986 | - $_contexte = []; |
|
| 1987 | - } |
|
| 1988 | - |
|
| 1989 | - if (isset($_contexte['fond'])) { |
|
| 1990 | - $f = $_contexte['fond']; |
|
| 1991 | - // toujours vrai : |
|
| 1992 | - if (preg_match('/^.fond.\s*=>(.*)$/s', $f, $r)) { |
|
| 1993 | - $f = $r[1]; |
|
| 1994 | - unset($_contexte['fond']); |
|
| 1995 | - } else { |
|
| 1996 | - spip_log('compilation de #INCLURE a revoir'); |
|
| 1997 | - } |
|
| 1998 | - |
|
| 1999 | - // #INCLURE{doublons} |
|
| 2000 | - if (isset($_contexte['doublons'])) { |
|
| 2001 | - $_contexte['doublons'] = "'doublons' => \$doublons"; |
|
| 2002 | - } |
|
| 2003 | - |
|
| 2004 | - // Critere d'inclusion {env} (et {self} pour compatibilite ascendante) |
|
| 2005 | - $flag_env = false; |
|
| 2006 | - if (isset($_contexte['env']) or isset($_contexte['self'])) { |
|
| 2007 | - $flag_env = true; |
|
| 2008 | - unset($_contexte['env']); |
|
| 2009 | - } |
|
| 2010 | - |
|
| 2011 | - $_options = []; |
|
| 2012 | - if (isset($_contexte['ajax'])) { |
|
| 2013 | - $_options[] = preg_replace(',=>(.*)$,ims', '=> ($v=(\\1))?$v:true', $_contexte['ajax']); |
|
| 2014 | - unset($_contexte['ajax']); |
|
| 2015 | - } |
|
| 2016 | - if ($p->etoile) { |
|
| 2017 | - $_options[] = "'etoile'=>true"; |
|
| 2018 | - } |
|
| 2019 | - $_options[] = "'compil'=>array(" . memoriser_contexte_compil($p) . ')'; |
|
| 2020 | - |
|
| 2021 | - $_l = 'array(' . join(",\n\t", $_contexte) . ')'; |
|
| 2022 | - if ($flag_env) { |
|
| 2023 | - $_l = "array_merge(\$Pile[0],$_l)"; |
|
| 2024 | - } |
|
| 2025 | - |
|
| 2026 | - $p->code = sprintf(CODE_RECUPERER_FOND, $f, $_l, join(',', $_options), "_request('connect') ?? ''"); |
|
| 2027 | - } elseif (!isset($_contexte[1])) { |
|
| 2028 | - $msg = ['zbug_balise_sans_argument', ['balise' => ' INCLURE']]; |
|
| 2029 | - erreur_squelette($msg, $p); |
|
| 2030 | - } else { |
|
| 2031 | - $p->code = 'charge_scripts(' . $_contexte[1] . ',false)'; |
|
| 2032 | - } |
|
| 2033 | - |
|
| 2034 | - $p->interdire_scripts = false; // la securite est assuree par recuperer_fond |
|
| 2035 | - return $p; |
|
| 1976 | + $id_boucle = $p->id_boucle; |
|
| 1977 | + // la lang n'est pas passe de facon automatique par argumenter |
|
| 1978 | + // mais le sera pas recuperer_fond, sauf si etoile=>true est passe |
|
| 1979 | + // en option |
|
| 1980 | + |
|
| 1981 | + $_contexte = argumenter_inclure($p->param, true, $p, $p->boucles, $id_boucle, false, false); |
|
| 1982 | + |
|
| 1983 | + // erreur de syntaxe = fond absent |
|
| 1984 | + // (2 messages d'erreur SPIP pour le prix d'un, mais pas d'erreur PHP |
|
| 1985 | + if (!$_contexte) { |
|
| 1986 | + $_contexte = []; |
|
| 1987 | + } |
|
| 1988 | + |
|
| 1989 | + if (isset($_contexte['fond'])) { |
|
| 1990 | + $f = $_contexte['fond']; |
|
| 1991 | + // toujours vrai : |
|
| 1992 | + if (preg_match('/^.fond.\s*=>(.*)$/s', $f, $r)) { |
|
| 1993 | + $f = $r[1]; |
|
| 1994 | + unset($_contexte['fond']); |
|
| 1995 | + } else { |
|
| 1996 | + spip_log('compilation de #INCLURE a revoir'); |
|
| 1997 | + } |
|
| 1998 | + |
|
| 1999 | + // #INCLURE{doublons} |
|
| 2000 | + if (isset($_contexte['doublons'])) { |
|
| 2001 | + $_contexte['doublons'] = "'doublons' => \$doublons"; |
|
| 2002 | + } |
|
| 2003 | + |
|
| 2004 | + // Critere d'inclusion {env} (et {self} pour compatibilite ascendante) |
|
| 2005 | + $flag_env = false; |
|
| 2006 | + if (isset($_contexte['env']) or isset($_contexte['self'])) { |
|
| 2007 | + $flag_env = true; |
|
| 2008 | + unset($_contexte['env']); |
|
| 2009 | + } |
|
| 2010 | + |
|
| 2011 | + $_options = []; |
|
| 2012 | + if (isset($_contexte['ajax'])) { |
|
| 2013 | + $_options[] = preg_replace(',=>(.*)$,ims', '=> ($v=(\\1))?$v:true', $_contexte['ajax']); |
|
| 2014 | + unset($_contexte['ajax']); |
|
| 2015 | + } |
|
| 2016 | + if ($p->etoile) { |
|
| 2017 | + $_options[] = "'etoile'=>true"; |
|
| 2018 | + } |
|
| 2019 | + $_options[] = "'compil'=>array(" . memoriser_contexte_compil($p) . ')'; |
|
| 2020 | + |
|
| 2021 | + $_l = 'array(' . join(",\n\t", $_contexte) . ')'; |
|
| 2022 | + if ($flag_env) { |
|
| 2023 | + $_l = "array_merge(\$Pile[0],$_l)"; |
|
| 2024 | + } |
|
| 2025 | + |
|
| 2026 | + $p->code = sprintf(CODE_RECUPERER_FOND, $f, $_l, join(',', $_options), "_request('connect') ?? ''"); |
|
| 2027 | + } elseif (!isset($_contexte[1])) { |
|
| 2028 | + $msg = ['zbug_balise_sans_argument', ['balise' => ' INCLURE']]; |
|
| 2029 | + erreur_squelette($msg, $p); |
|
| 2030 | + } else { |
|
| 2031 | + $p->code = 'charge_scripts(' . $_contexte[1] . ',false)'; |
|
| 2032 | + } |
|
| 2033 | + |
|
| 2034 | + $p->interdire_scripts = false; // la securite est assuree par recuperer_fond |
|
| 2035 | + return $p; |
|
| 2036 | 2036 | } |
| 2037 | 2037 | |
| 2038 | 2038 | |
@@ -2060,69 +2060,69 @@ discard block |
||
| 2060 | 2060 | **/ |
| 2061 | 2061 | function balise_MODELE_dist($p) { |
| 2062 | 2062 | |
| 2063 | - $_contexte = argumenter_inclure($p->param, true, $p, $p->boucles, $p->id_boucle, false); |
|
| 2064 | - |
|
| 2065 | - // erreur de syntaxe = fond absent |
|
| 2066 | - // (2 messages d'erreur SPIP pour le prix d'un, mais pas d'erreur PHP |
|
| 2067 | - if (!$_contexte) { |
|
| 2068 | - $_contexte = []; |
|
| 2069 | - } |
|
| 2070 | - |
|
| 2071 | - if (!isset($_contexte[1])) { |
|
| 2072 | - $msg = ['zbug_balise_sans_argument', ['balise' => ' MODELE']]; |
|
| 2073 | - erreur_squelette($msg, $p); |
|
| 2074 | - } else { |
|
| 2075 | - $nom = $_contexte[1]; |
|
| 2076 | - unset($_contexte[1]); |
|
| 2077 | - |
|
| 2078 | - if (preg_match("/^\s*'[^']*'/s", $nom)) { |
|
| 2079 | - $nom = "'modeles/" . substr($nom, 1); |
|
| 2080 | - } else { |
|
| 2081 | - $nom = "'modeles/' . $nom"; |
|
| 2082 | - } |
|
| 2083 | - |
|
| 2084 | - $flag_env = false; |
|
| 2085 | - if (isset($_contexte['env'])) { |
|
| 2086 | - $flag_env = true; |
|
| 2087 | - unset($_contexte['env']); |
|
| 2088 | - } |
|
| 2089 | - |
|
| 2090 | - // Incoherence dans la syntaxe du contexte. A revoir. |
|
| 2091 | - // Reserver la cle primaire de la boucle courante si elle existe |
|
| 2092 | - if (isset($p->boucles[$p->id_boucle]->primary)) { |
|
| 2093 | - $primary = $p->boucles[$p->id_boucle]->primary; |
|
| 2094 | - if (!strpos($primary, ',')) { |
|
| 2095 | - $id = champ_sql($primary, $p); |
|
| 2096 | - $_contexte[] = "'$primary'=>" . $id; |
|
| 2097 | - $_contexte[] = "'id'=>" . $id; |
|
| 2098 | - } |
|
| 2099 | - } |
|
| 2100 | - $_contexte[] = "'recurs'=>(++\$recurs)"; |
|
| 2101 | - $connect = ''; |
|
| 2102 | - if (isset($p->boucles[$p->id_boucle])) { |
|
| 2103 | - $connect = $p->boucles[$p->id_boucle]->sql_serveur; |
|
| 2104 | - } |
|
| 2105 | - |
|
| 2106 | - $_options = memoriser_contexte_compil($p); |
|
| 2107 | - $_options = "'compil'=>array($_options), 'trim'=>true"; |
|
| 2108 | - if (isset($_contexte['ajax'])) { |
|
| 2109 | - $_options .= ', ' . preg_replace(',=>(.*)$,ims', '=> ($v=(\\1))?$v:true', $_contexte['ajax']); |
|
| 2110 | - unset($_contexte['ajax']); |
|
| 2111 | - } |
|
| 2112 | - |
|
| 2113 | - $_l = 'array(' . join(",\n\t", $_contexte) . ')'; |
|
| 2114 | - if ($flag_env) { |
|
| 2115 | - $_l = "array_merge(\$Pile[0],$_l)"; |
|
| 2116 | - } |
|
| 2117 | - |
|
| 2118 | - $page = sprintf(CODE_RECUPERER_FOND, $nom, $_l, $_options, _q($connect)); |
|
| 2119 | - |
|
| 2120 | - $p->code = "\n\t(((\$recurs=(isset(\$Pile[0]['recurs'])?\$Pile[0]['recurs']:0))>=5)? '' :\n\t$page)\n"; |
|
| 2121 | - |
|
| 2122 | - $p->interdire_scripts = false; // securite assuree par le squelette |
|
| 2123 | - } |
|
| 2124 | - |
|
| 2125 | - return $p; |
|
| 2063 | + $_contexte = argumenter_inclure($p->param, true, $p, $p->boucles, $p->id_boucle, false); |
|
| 2064 | + |
|
| 2065 | + // erreur de syntaxe = fond absent |
|
| 2066 | + // (2 messages d'erreur SPIP pour le prix d'un, mais pas d'erreur PHP |
|
| 2067 | + if (!$_contexte) { |
|
| 2068 | + $_contexte = []; |
|
| 2069 | + } |
|
| 2070 | + |
|
| 2071 | + if (!isset($_contexte[1])) { |
|
| 2072 | + $msg = ['zbug_balise_sans_argument', ['balise' => ' MODELE']]; |
|
| 2073 | + erreur_squelette($msg, $p); |
|
| 2074 | + } else { |
|
| 2075 | + $nom = $_contexte[1]; |
|
| 2076 | + unset($_contexte[1]); |
|
| 2077 | + |
|
| 2078 | + if (preg_match("/^\s*'[^']*'/s", $nom)) { |
|
| 2079 | + $nom = "'modeles/" . substr($nom, 1); |
|
| 2080 | + } else { |
|
| 2081 | + $nom = "'modeles/' . $nom"; |
|
| 2082 | + } |
|
| 2083 | + |
|
| 2084 | + $flag_env = false; |
|
| 2085 | + if (isset($_contexte['env'])) { |
|
| 2086 | + $flag_env = true; |
|
| 2087 | + unset($_contexte['env']); |
|
| 2088 | + } |
|
| 2089 | + |
|
| 2090 | + // Incoherence dans la syntaxe du contexte. A revoir. |
|
| 2091 | + // Reserver la cle primaire de la boucle courante si elle existe |
|
| 2092 | + if (isset($p->boucles[$p->id_boucle]->primary)) { |
|
| 2093 | + $primary = $p->boucles[$p->id_boucle]->primary; |
|
| 2094 | + if (!strpos($primary, ',')) { |
|
| 2095 | + $id = champ_sql($primary, $p); |
|
| 2096 | + $_contexte[] = "'$primary'=>" . $id; |
|
| 2097 | + $_contexte[] = "'id'=>" . $id; |
|
| 2098 | + } |
|
| 2099 | + } |
|
| 2100 | + $_contexte[] = "'recurs'=>(++\$recurs)"; |
|
| 2101 | + $connect = ''; |
|
| 2102 | + if (isset($p->boucles[$p->id_boucle])) { |
|
| 2103 | + $connect = $p->boucles[$p->id_boucle]->sql_serveur; |
|
| 2104 | + } |
|
| 2105 | + |
|
| 2106 | + $_options = memoriser_contexte_compil($p); |
|
| 2107 | + $_options = "'compil'=>array($_options), 'trim'=>true"; |
|
| 2108 | + if (isset($_contexte['ajax'])) { |
|
| 2109 | + $_options .= ', ' . preg_replace(',=>(.*)$,ims', '=> ($v=(\\1))?$v:true', $_contexte['ajax']); |
|
| 2110 | + unset($_contexte['ajax']); |
|
| 2111 | + } |
|
| 2112 | + |
|
| 2113 | + $_l = 'array(' . join(",\n\t", $_contexte) . ')'; |
|
| 2114 | + if ($flag_env) { |
|
| 2115 | + $_l = "array_merge(\$Pile[0],$_l)"; |
|
| 2116 | + } |
|
| 2117 | + |
|
| 2118 | + $page = sprintf(CODE_RECUPERER_FOND, $nom, $_l, $_options, _q($connect)); |
|
| 2119 | + |
|
| 2120 | + $p->code = "\n\t(((\$recurs=(isset(\$Pile[0]['recurs'])?\$Pile[0]['recurs']:0))>=5)? '' :\n\t$page)\n"; |
|
| 2121 | + |
|
| 2122 | + $p->interdire_scripts = false; // securite assuree par le squelette |
|
| 2123 | + } |
|
| 2124 | + |
|
| 2125 | + return $p; |
|
| 2126 | 2126 | } |
| 2127 | 2127 | |
| 2128 | 2128 | |
@@ -2146,21 +2146,21 @@ discard block |
||
| 2146 | 2146 | * Pile complétée par le code à générer |
| 2147 | 2147 | **/ |
| 2148 | 2148 | function balise_SET_dist($p) { |
| 2149 | - $_nom = interprete_argument_balise(1, $p); |
|
| 2150 | - $_val = interprete_argument_balise(2, $p); |
|
| 2149 | + $_nom = interprete_argument_balise(1, $p); |
|
| 2150 | + $_val = interprete_argument_balise(2, $p); |
|
| 2151 | 2151 | |
| 2152 | - if (!$_nom or !$_val) { |
|
| 2153 | - $err_b_s_a = ['zbug_balise_sans_argument', ['balise' => 'SET']]; |
|
| 2154 | - erreur_squelette($err_b_s_a, $p); |
|
| 2155 | - } |
|
| 2156 | - // affectation $_zzz inutile, mais permet de contourner un bug OpCode cache sous PHP 5.5.4 |
|
| 2157 | - // cf https://bugs.php.net/bug.php?id=65845 |
|
| 2158 | - else { |
|
| 2159 | - $p->code = "vide(\$Pile['vars'][\$_zzz=(string)$_nom] = $_val)"; |
|
| 2160 | - } |
|
| 2152 | + if (!$_nom or !$_val) { |
|
| 2153 | + $err_b_s_a = ['zbug_balise_sans_argument', ['balise' => 'SET']]; |
|
| 2154 | + erreur_squelette($err_b_s_a, $p); |
|
| 2155 | + } |
|
| 2156 | + // affectation $_zzz inutile, mais permet de contourner un bug OpCode cache sous PHP 5.5.4 |
|
| 2157 | + // cf https://bugs.php.net/bug.php?id=65845 |
|
| 2158 | + else { |
|
| 2159 | + $p->code = "vide(\$Pile['vars'][\$_zzz=(string)$_nom] = $_val)"; |
|
| 2160 | + } |
|
| 2161 | 2161 | |
| 2162 | - $p->interdire_scripts = false; // la balise ne renvoie rien |
|
| 2163 | - return $p; |
|
| 2162 | + $p->interdire_scripts = false; // la balise ne renvoie rien |
|
| 2163 | + return $p; |
|
| 2164 | 2164 | } |
| 2165 | 2165 | |
| 2166 | 2166 | |
@@ -2190,12 +2190,12 @@ discard block |
||
| 2190 | 2190 | * Pile complétée par le code à générer |
| 2191 | 2191 | **/ |
| 2192 | 2192 | function balise_GET_dist($p) { |
| 2193 | - $p->interdire_scripts = false; // le contenu vient de #SET, donc il est de confiance |
|
| 2194 | - if (function_exists('balise_ENV')) { |
|
| 2195 | - return balise_ENV($p, '$Pile["vars"]??[]'); |
|
| 2196 | - } else { |
|
| 2197 | - return balise_ENV_dist($p, '$Pile["vars"]??[]'); |
|
| 2198 | - } |
|
| 2193 | + $p->interdire_scripts = false; // le contenu vient de #SET, donc il est de confiance |
|
| 2194 | + if (function_exists('balise_ENV')) { |
|
| 2195 | + return balise_ENV($p, '$Pile["vars"]??[]'); |
|
| 2196 | + } else { |
|
| 2197 | + return balise_ENV_dist($p, '$Pile["vars"]??[]'); |
|
| 2198 | + } |
|
| 2199 | 2199 | } |
| 2200 | 2200 | |
| 2201 | 2201 | |
@@ -2218,22 +2218,22 @@ discard block |
||
| 2218 | 2218 | * Pile complétée par le code à générer |
| 2219 | 2219 | **/ |
| 2220 | 2220 | function balise_DOUBLONS_dist($p) { |
| 2221 | - if ($type = interprete_argument_balise(1, $p)) { |
|
| 2222 | - if ($famille = interprete_argument_balise(2, $p)) { |
|
| 2223 | - $type .= '.' . $famille; |
|
| 2224 | - } |
|
| 2225 | - $p->code = '(isset($doublons[' . $type . ']) ? $doublons[' . $type . '] : "")'; |
|
| 2226 | - if (!$p->etoile) { |
|
| 2227 | - $p->code = 'array_filter(array_map("intval",explode(",",' |
|
| 2228 | - . $p->code . ')))'; |
|
| 2229 | - } |
|
| 2230 | - } else { |
|
| 2231 | - $p->code = '$doublons'; |
|
| 2232 | - } |
|
| 2221 | + if ($type = interprete_argument_balise(1, $p)) { |
|
| 2222 | + if ($famille = interprete_argument_balise(2, $p)) { |
|
| 2223 | + $type .= '.' . $famille; |
|
| 2224 | + } |
|
| 2225 | + $p->code = '(isset($doublons[' . $type . ']) ? $doublons[' . $type . '] : "")'; |
|
| 2226 | + if (!$p->etoile) { |
|
| 2227 | + $p->code = 'array_filter(array_map("intval",explode(",",' |
|
| 2228 | + . $p->code . ')))'; |
|
| 2229 | + } |
|
| 2230 | + } else { |
|
| 2231 | + $p->code = '$doublons'; |
|
| 2232 | + } |
|
| 2233 | 2233 | |
| 2234 | - $p->interdire_scripts = false; |
|
| 2234 | + $p->interdire_scripts = false; |
|
| 2235 | 2235 | |
| 2236 | - return $p; |
|
| 2236 | + return $p; |
|
| 2237 | 2237 | } |
| 2238 | 2238 | |
| 2239 | 2239 | |
@@ -2256,18 +2256,18 @@ discard block |
||
| 2256 | 2256 | * Pile complétée par le code à générer |
| 2257 | 2257 | **/ |
| 2258 | 2258 | function balise_PIPELINE_dist($p) { |
| 2259 | - $_pipe = interprete_argument_balise(1, $p); |
|
| 2260 | - if (!$_pipe) { |
|
| 2261 | - $err_b_s_a = ['zbug_balise_sans_argument', ['balise' => 'PIPELINE']]; |
|
| 2262 | - erreur_squelette($err_b_s_a, $p); |
|
| 2263 | - } else { |
|
| 2264 | - $_flux = interprete_argument_balise(2, $p); |
|
| 2265 | - $_flux = $_flux ?: "''"; |
|
| 2266 | - $p->code = "pipeline( $_pipe , $_flux )"; |
|
| 2267 | - $p->interdire_scripts = false; |
|
| 2268 | - } |
|
| 2259 | + $_pipe = interprete_argument_balise(1, $p); |
|
| 2260 | + if (!$_pipe) { |
|
| 2261 | + $err_b_s_a = ['zbug_balise_sans_argument', ['balise' => 'PIPELINE']]; |
|
| 2262 | + erreur_squelette($err_b_s_a, $p); |
|
| 2263 | + } else { |
|
| 2264 | + $_flux = interprete_argument_balise(2, $p); |
|
| 2265 | + $_flux = $_flux ?: "''"; |
|
| 2266 | + $p->code = "pipeline( $_pipe , $_flux )"; |
|
| 2267 | + $p->interdire_scripts = false; |
|
| 2268 | + } |
|
| 2269 | 2269 | |
| 2270 | - return $p; |
|
| 2270 | + return $p; |
|
| 2271 | 2271 | } |
| 2272 | 2272 | |
| 2273 | 2273 | |
@@ -2292,10 +2292,10 @@ discard block |
||
| 2292 | 2292 | * Pile complétée par le code à générer |
| 2293 | 2293 | **/ |
| 2294 | 2294 | function balise_EDIT_dist($p) { |
| 2295 | - $p->code = "''"; |
|
| 2296 | - $p->interdire_scripts = false; |
|
| 2295 | + $p->code = "''"; |
|
| 2296 | + $p->interdire_scripts = false; |
|
| 2297 | 2297 | |
| 2298 | - return $p; |
|
| 2298 | + return $p; |
|
| 2299 | 2299 | } |
| 2300 | 2300 | |
| 2301 | 2301 | |
@@ -2318,11 +2318,11 @@ discard block |
||
| 2318 | 2318 | * Pile complétée par le code à générer |
| 2319 | 2319 | **/ |
| 2320 | 2320 | function balise_TOTAL_UNIQUE_dist($p) { |
| 2321 | - $_famille = interprete_argument_balise(1, $p); |
|
| 2322 | - $_famille = $_famille ?: "''"; |
|
| 2323 | - $p->code = "unique('', $_famille, true)"; |
|
| 2321 | + $_famille = interprete_argument_balise(1, $p); |
|
| 2322 | + $_famille = $_famille ?: "''"; |
|
| 2323 | + $p->code = "unique('', $_famille, true)"; |
|
| 2324 | 2324 | |
| 2325 | - return $p; |
|
| 2325 | + return $p; |
|
| 2326 | 2326 | } |
| 2327 | 2327 | |
| 2328 | 2328 | /** |
@@ -2345,19 +2345,19 @@ discard block |
||
| 2345 | 2345 | * Pile complétée par le code à générer |
| 2346 | 2346 | **/ |
| 2347 | 2347 | function balise_ARRAY_dist($p) { |
| 2348 | - $_code = []; |
|
| 2349 | - $n = 1; |
|
| 2350 | - do { |
|
| 2351 | - $_key = interprete_argument_balise($n++, $p); |
|
| 2352 | - $_val = interprete_argument_balise($n++, $p); |
|
| 2353 | - if ($_key and $_val) { |
|
| 2354 | - $_code[] = "$_key => $_val"; |
|
| 2355 | - } |
|
| 2356 | - } while ($_key && $_val); |
|
| 2357 | - $p->code = 'array(' . join(', ', $_code) . ')'; |
|
| 2358 | - $p->interdire_scripts = false; |
|
| 2348 | + $_code = []; |
|
| 2349 | + $n = 1; |
|
| 2350 | + do { |
|
| 2351 | + $_key = interprete_argument_balise($n++, $p); |
|
| 2352 | + $_val = interprete_argument_balise($n++, $p); |
|
| 2353 | + if ($_key and $_val) { |
|
| 2354 | + $_code[] = "$_key => $_val"; |
|
| 2355 | + } |
|
| 2356 | + } while ($_key && $_val); |
|
| 2357 | + $p->code = 'array(' . join(', ', $_code) . ')'; |
|
| 2358 | + $p->interdire_scripts = false; |
|
| 2359 | 2359 | |
| 2360 | - return $p; |
|
| 2360 | + return $p; |
|
| 2361 | 2361 | } |
| 2362 | 2362 | |
| 2363 | 2363 | /** |
@@ -2376,15 +2376,15 @@ discard block |
||
| 2376 | 2376 | * Pile complétée par le code à générer |
| 2377 | 2377 | */ |
| 2378 | 2378 | function balise_LISTE_dist($p) { |
| 2379 | - $_code = []; |
|
| 2380 | - $n = 1; |
|
| 2381 | - while ($_val = interprete_argument_balise($n++, $p)) { |
|
| 2382 | - $_code[] = $_val; |
|
| 2383 | - } |
|
| 2384 | - $p->code = 'array(' . join(', ', $_code) . ')'; |
|
| 2385 | - $p->interdire_scripts = false; |
|
| 2379 | + $_code = []; |
|
| 2380 | + $n = 1; |
|
| 2381 | + while ($_val = interprete_argument_balise($n++, $p)) { |
|
| 2382 | + $_code[] = $_val; |
|
| 2383 | + } |
|
| 2384 | + $p->code = 'array(' . join(', ', $_code) . ')'; |
|
| 2385 | + $p->interdire_scripts = false; |
|
| 2386 | 2386 | |
| 2387 | - return $p; |
|
| 2387 | + return $p; |
|
| 2388 | 2388 | } |
| 2389 | 2389 | |
| 2390 | 2390 | |
@@ -2418,21 +2418,21 @@ discard block |
||
| 2418 | 2418 | * Pile complétée par le code à générer |
| 2419 | 2419 | **/ |
| 2420 | 2420 | function balise_AUTORISER_dist($p) { |
| 2421 | - $_code = []; |
|
| 2422 | - $p->descr['session'] = true; // faire un cache par session |
|
| 2421 | + $_code = []; |
|
| 2422 | + $p->descr['session'] = true; // faire un cache par session |
|
| 2423 | 2423 | |
| 2424 | - $n = 1; |
|
| 2425 | - while ($_v = interprete_argument_balise($n++, $p)) { |
|
| 2426 | - $_code[] = $_v; |
|
| 2427 | - } |
|
| 2424 | + $n = 1; |
|
| 2425 | + while ($_v = interprete_argument_balise($n++, $p)) { |
|
| 2426 | + $_code[] = $_v; |
|
| 2427 | + } |
|
| 2428 | 2428 | |
| 2429 | - $p->code = '((function_exists("autoriser")||include_spip("inc/autoriser"))&&autoriser(' . join( |
|
| 2430 | - ', ', |
|
| 2431 | - $_code |
|
| 2432 | - ) . ')?" ":"")'; |
|
| 2433 | - $p->interdire_scripts = false; |
|
| 2429 | + $p->code = '((function_exists("autoriser")||include_spip("inc/autoriser"))&&autoriser(' . join( |
|
| 2430 | + ', ', |
|
| 2431 | + $_code |
|
| 2432 | + ) . ')?" ":"")'; |
|
| 2433 | + $p->interdire_scripts = false; |
|
| 2434 | 2434 | |
| 2435 | - return $p; |
|
| 2435 | + return $p; |
|
| 2436 | 2436 | } |
| 2437 | 2437 | |
| 2438 | 2438 | |
@@ -2456,15 +2456,15 @@ discard block |
||
| 2456 | 2456 | * Pile complétée par le code à générer |
| 2457 | 2457 | **/ |
| 2458 | 2458 | function balise_PLUGIN_dist($p) { |
| 2459 | - $plugin = interprete_argument_balise(1, $p); |
|
| 2460 | - $plugin = isset($plugin) ? str_replace('\'', '"', $plugin) : '""'; |
|
| 2461 | - $type_info = interprete_argument_balise(2, $p); |
|
| 2462 | - $type_info = isset($type_info) ? str_replace('\'', '"', $type_info) : '"est_actif"'; |
|
| 2459 | + $plugin = interprete_argument_balise(1, $p); |
|
| 2460 | + $plugin = isset($plugin) ? str_replace('\'', '"', $plugin) : '""'; |
|
| 2461 | + $type_info = interprete_argument_balise(2, $p); |
|
| 2462 | + $type_info = isset($type_info) ? str_replace('\'', '"', $type_info) : '"est_actif"'; |
|
| 2463 | 2463 | |
| 2464 | - $f = chercher_filtre('info_plugin'); |
|
| 2465 | - $p->code = $f . '(' . $plugin . ', ' . $type_info . ')'; |
|
| 2464 | + $f = chercher_filtre('info_plugin'); |
|
| 2465 | + $p->code = $f . '(' . $plugin . ', ' . $type_info . ')'; |
|
| 2466 | 2466 | |
| 2467 | - return $p; |
|
| 2467 | + return $p; |
|
| 2468 | 2468 | } |
| 2469 | 2469 | |
| 2470 | 2470 | /** |
@@ -2485,9 +2485,9 @@ discard block |
||
| 2485 | 2485 | * Pile complétée par le code à générer |
| 2486 | 2486 | **/ |
| 2487 | 2487 | function balise_AIDER_dist($p) { |
| 2488 | - $_motif = interprete_argument_balise(1, $p); |
|
| 2489 | - $p->code = "((\$aider=charger_fonction('aide','inc',true))?\$aider($_motif):'')"; |
|
| 2490 | - return $p; |
|
| 2488 | + $_motif = interprete_argument_balise(1, $p); |
|
| 2489 | + $p->code = "((\$aider=charger_fonction('aide','inc',true))?\$aider($_motif):'')"; |
|
| 2490 | + return $p; |
|
| 2491 | 2491 | } |
| 2492 | 2492 | |
| 2493 | 2493 | /** |
@@ -2513,16 +2513,16 @@ discard block |
||
| 2513 | 2513 | * Pile complétée par le code à générer |
| 2514 | 2514 | **/ |
| 2515 | 2515 | function balise_ACTION_FORMULAIRE($p) { |
| 2516 | - if (!$_url = interprete_argument_balise(1, $p)) { |
|
| 2517 | - $_url = "(\$Pile[0]['action'] ?? '')"; |
|
| 2518 | - } |
|
| 2519 | - if (!$_form = interprete_argument_balise(2, $p)) { |
|
| 2520 | - $_form = "(\$Pile[0]['form'] ?? '')"; |
|
| 2521 | - } |
|
| 2522 | - |
|
| 2523 | - // envoyer le nom du formulaire que l'on traite |
|
| 2524 | - // transmettre les eventuels args de la balise formulaire |
|
| 2525 | - $p->code = " '<span class=\"form-hidden\">' . |
|
| 2516 | + if (!$_url = interprete_argument_balise(1, $p)) { |
|
| 2517 | + $_url = "(\$Pile[0]['action'] ?? '')"; |
|
| 2518 | + } |
|
| 2519 | + if (!$_form = interprete_argument_balise(2, $p)) { |
|
| 2520 | + $_form = "(\$Pile[0]['form'] ?? '')"; |
|
| 2521 | + } |
|
| 2522 | + |
|
| 2523 | + // envoyer le nom du formulaire que l'on traite |
|
| 2524 | + // transmettre les eventuels args de la balise formulaire |
|
| 2525 | + $p->code = " '<span class=\"form-hidden\">' . |
|
| 2526 | 2526 | form_hidden($_url) . |
| 2527 | 2527 | '<input name=\'formulaire_action\' type=\'hidden\' |
| 2528 | 2528 | value=\'' . $_form . '\' />' . |
@@ -2533,9 +2533,9 @@ discard block |
||
| 2533 | 2533 | (\$Pile[0]['_hidden'] ?? '') . |
| 2534 | 2534 | '</span>'"; |
| 2535 | 2535 | |
| 2536 | - $p->interdire_scripts = false; |
|
| 2536 | + $p->interdire_scripts = false; |
|
| 2537 | 2537 | |
| 2538 | - return $p; |
|
| 2538 | + return $p; |
|
| 2539 | 2539 | } |
| 2540 | 2540 | |
| 2541 | 2541 | |
@@ -2576,25 +2576,25 @@ discard block |
||
| 2576 | 2576 | */ |
| 2577 | 2577 | function balise_BOUTON_ACTION_dist($p) { |
| 2578 | 2578 | |
| 2579 | - $args = []; |
|
| 2580 | - for ($k = 1; $k <= 6; $k++) { |
|
| 2581 | - $_a = interprete_argument_balise($k, $p); |
|
| 2582 | - if (!$_a) { |
|
| 2583 | - $_a = "''"; |
|
| 2584 | - } |
|
| 2585 | - $args[] = $_a; |
|
| 2586 | - } |
|
| 2587 | - // supprimer les args vides |
|
| 2588 | - while (end($args) == "''" and count($args) > 2) { |
|
| 2589 | - array_pop($args); |
|
| 2590 | - } |
|
| 2591 | - $args = implode(',', $args); |
|
| 2579 | + $args = []; |
|
| 2580 | + for ($k = 1; $k <= 6; $k++) { |
|
| 2581 | + $_a = interprete_argument_balise($k, $p); |
|
| 2582 | + if (!$_a) { |
|
| 2583 | + $_a = "''"; |
|
| 2584 | + } |
|
| 2585 | + $args[] = $_a; |
|
| 2586 | + } |
|
| 2587 | + // supprimer les args vides |
|
| 2588 | + while (end($args) == "''" and count($args) > 2) { |
|
| 2589 | + array_pop($args); |
|
| 2590 | + } |
|
| 2591 | + $args = implode(',', $args); |
|
| 2592 | 2592 | |
| 2593 | - $bouton_action = chercher_filtre('bouton_action'); |
|
| 2594 | - $p->code = "$bouton_action($args)"; |
|
| 2595 | - $p->interdire_scripts = false; |
|
| 2593 | + $bouton_action = chercher_filtre('bouton_action'); |
|
| 2594 | + $p->code = "$bouton_action($args)"; |
|
| 2595 | + $p->interdire_scripts = false; |
|
| 2596 | 2596 | |
| 2597 | - return $p; |
|
| 2597 | + return $p; |
|
| 2598 | 2598 | } |
| 2599 | 2599 | |
| 2600 | 2600 | |
@@ -2613,10 +2613,10 @@ discard block |
||
| 2613 | 2613 | * Pile complétée par le code à générer |
| 2614 | 2614 | */ |
| 2615 | 2615 | function balise_SLOGAN_SITE_SPIP_dist($p) { |
| 2616 | - $p->code = "\$GLOBALS['meta']['slogan_site']"; |
|
| 2616 | + $p->code = "\$GLOBALS['meta']['slogan_site']"; |
|
| 2617 | 2617 | |
| 2618 | - #$p->interdire_scripts = true; |
|
| 2619 | - return $p; |
|
| 2618 | + #$p->interdire_scripts = true; |
|
| 2619 | + return $p; |
|
| 2620 | 2620 | } |
| 2621 | 2621 | |
| 2622 | 2622 | |
@@ -2640,10 +2640,10 @@ discard block |
||
| 2640 | 2640 | * Pile complétée par le code à générer |
| 2641 | 2641 | */ |
| 2642 | 2642 | function balise_HTML5_dist($p) { |
| 2643 | - $p->code = html5_permis() ? "' '" : "''"; |
|
| 2644 | - $p->interdire_scripts = false; |
|
| 2643 | + $p->code = html5_permis() ? "' '" : "''"; |
|
| 2644 | + $p->interdire_scripts = false; |
|
| 2645 | 2645 | |
| 2646 | - return $p; |
|
| 2646 | + return $p; |
|
| 2647 | 2647 | } |
| 2648 | 2648 | |
| 2649 | 2649 | |
@@ -2669,58 +2669,58 @@ discard block |
||
| 2669 | 2669 | * Pile complétée par le code à générer |
| 2670 | 2670 | */ |
| 2671 | 2671 | function balise_TRI_dist($p, $liste = 'true') { |
| 2672 | - $b = index_boucle_mere($p); |
|
| 2673 | - // s'il n'y a pas de nom de boucle, on ne peut pas trier |
|
| 2674 | - if ($b === '') { |
|
| 2675 | - $msg = ['zbug_champ_hors_boucle', ['champ' => zbug_presenter_champ($p)]]; |
|
| 2676 | - erreur_squelette($msg, $p); |
|
| 2677 | - $p->code = "''"; |
|
| 2672 | + $b = index_boucle_mere($p); |
|
| 2673 | + // s'il n'y a pas de nom de boucle, on ne peut pas trier |
|
| 2674 | + if ($b === '') { |
|
| 2675 | + $msg = ['zbug_champ_hors_boucle', ['champ' => zbug_presenter_champ($p)]]; |
|
| 2676 | + erreur_squelette($msg, $p); |
|
| 2677 | + $p->code = "''"; |
|
| 2678 | 2678 | |
| 2679 | - return $p; |
|
| 2680 | - } |
|
| 2681 | - $boucle = $p->boucles[$b]; |
|
| 2679 | + return $p; |
|
| 2680 | + } |
|
| 2681 | + $boucle = $p->boucles[$b]; |
|
| 2682 | 2682 | |
| 2683 | - // s'il n'y a pas de tri_champ, c'est qu'on se trouve |
|
| 2684 | - // dans un boucle recursive ou qu'on a oublie le critere {tri} |
|
| 2685 | - if (!isset($boucle->modificateur['tri_champ'])) { |
|
| 2686 | - $msg = ['zbug_champ_hors_critere', [ |
|
| 2687 | - 'champ' => zbug_presenter_champ($p), |
|
| 2688 | - 'critere' => 'tri' |
|
| 2689 | - ]]; |
|
| 2690 | - erreur_squelette($msg, $p); |
|
| 2691 | - $p->code = "''"; |
|
| 2683 | + // s'il n'y a pas de tri_champ, c'est qu'on se trouve |
|
| 2684 | + // dans un boucle recursive ou qu'on a oublie le critere {tri} |
|
| 2685 | + if (!isset($boucle->modificateur['tri_champ'])) { |
|
| 2686 | + $msg = ['zbug_champ_hors_critere', [ |
|
| 2687 | + 'champ' => zbug_presenter_champ($p), |
|
| 2688 | + 'critere' => 'tri' |
|
| 2689 | + ]]; |
|
| 2690 | + erreur_squelette($msg, $p); |
|
| 2691 | + $p->code = "''"; |
|
| 2692 | 2692 | |
| 2693 | - return $p; |
|
| 2694 | - } |
|
| 2693 | + return $p; |
|
| 2694 | + } |
|
| 2695 | 2695 | |
| 2696 | - // Différentes infos relatives au tri présentes dans les modificateurs |
|
| 2697 | - $_tri_nom = $boucle->modificateur['tri_nom'] ; // nom du paramètre définissant le tri |
|
| 2698 | - $_tri_champ = $boucle->modificateur['tri_champ']; // champ actuel utilisé le tri |
|
| 2699 | - $_tri_sens = $boucle->modificateur['tri_sens']; // sens de tri actuel |
|
| 2700 | - $_tri_liste_sens_defaut = $boucle->modificateur['tri_liste_sens_defaut']; // sens par défaut pour chaque champ |
|
| 2696 | + // Différentes infos relatives au tri présentes dans les modificateurs |
|
| 2697 | + $_tri_nom = $boucle->modificateur['tri_nom'] ; // nom du paramètre définissant le tri |
|
| 2698 | + $_tri_champ = $boucle->modificateur['tri_champ']; // champ actuel utilisé le tri |
|
| 2699 | + $_tri_sens = $boucle->modificateur['tri_sens']; // sens de tri actuel |
|
| 2700 | + $_tri_liste_sens_defaut = $boucle->modificateur['tri_liste_sens_defaut']; // sens par défaut pour chaque champ |
|
| 2701 | 2701 | |
| 2702 | - $_champ_ou_sens = interprete_argument_balise(1, $p); |
|
| 2703 | - // si pas de champ, renvoyer le critère de tri actuel |
|
| 2704 | - if (!$_champ_ou_sens) { |
|
| 2705 | - $p->code = $_tri_champ; |
|
| 2702 | + $_champ_ou_sens = interprete_argument_balise(1, $p); |
|
| 2703 | + // si pas de champ, renvoyer le critère de tri actuel |
|
| 2704 | + if (!$_champ_ou_sens) { |
|
| 2705 | + $p->code = $_tri_champ; |
|
| 2706 | 2706 | |
| 2707 | - return $p; |
|
| 2708 | - } |
|
| 2709 | - // forcer la jointure si besoin, et si le champ est statique |
|
| 2710 | - if (preg_match(",^'([\w.]+)'$,i", $_champ_ou_sens, $m)) { |
|
| 2711 | - index_pile($b, $m[1], $p->boucles, '', null, true, false); |
|
| 2712 | - } |
|
| 2707 | + return $p; |
|
| 2708 | + } |
|
| 2709 | + // forcer la jointure si besoin, et si le champ est statique |
|
| 2710 | + if (preg_match(",^'([\w.]+)'$,i", $_champ_ou_sens, $m)) { |
|
| 2711 | + index_pile($b, $m[1], $p->boucles, '', null, true, false); |
|
| 2712 | + } |
|
| 2713 | 2713 | |
| 2714 | - $_libelle = interprete_argument_balise(2, $p); |
|
| 2715 | - $_libelle = $_libelle ?: $_champ_ou_sens; |
|
| 2714 | + $_libelle = interprete_argument_balise(2, $p); |
|
| 2715 | + $_libelle = $_libelle ?: $_champ_ou_sens; |
|
| 2716 | 2716 | |
| 2717 | - $_class = interprete_argument_balise(3, $p) ?? "''"; |
|
| 2717 | + $_class = interprete_argument_balise(3, $p) ?? "''"; |
|
| 2718 | 2718 | |
| 2719 | - $p->code = "calculer_balise_tri($_champ_ou_sens, $_libelle, $_class, $_tri_nom, $_tri_champ, $_tri_sens, $_tri_liste_sens_defaut)"; |
|
| 2719 | + $p->code = "calculer_balise_tri($_champ_ou_sens, $_libelle, $_class, $_tri_nom, $_tri_champ, $_tri_sens, $_tri_liste_sens_defaut)"; |
|
| 2720 | 2720 | |
| 2721 | - $p->interdire_scripts = false; |
|
| 2721 | + $p->interdire_scripts = false; |
|
| 2722 | 2722 | |
| 2723 | - return $p; |
|
| 2723 | + return $p; |
|
| 2724 | 2724 | } |
| 2725 | 2725 | |
| 2726 | 2726 | |
@@ -2741,21 +2741,21 @@ discard block |
||
| 2741 | 2741 | * Pile complétée par le code à générer |
| 2742 | 2742 | */ |
| 2743 | 2743 | function balise_SAUTER_dist($p) { |
| 2744 | - $id_boucle = $p->id_boucle; |
|
| 2744 | + $id_boucle = $p->id_boucle; |
|
| 2745 | 2745 | |
| 2746 | - if (empty($p->boucles[$id_boucle])) { |
|
| 2747 | - $msg = ['zbug_champ_hors_boucle', ['champ' => '#SAUTER']]; |
|
| 2748 | - erreur_squelette($msg, $p); |
|
| 2749 | - } else { |
|
| 2750 | - $_saut = interprete_argument_balise(1, $p); |
|
| 2751 | - $_compteur = "\$Numrows['$id_boucle']['compteur_boucle']"; |
|
| 2752 | - $_total = "(\$Numrows['$id_boucle']['total'] ?? null)"; |
|
| 2746 | + if (empty($p->boucles[$id_boucle])) { |
|
| 2747 | + $msg = ['zbug_champ_hors_boucle', ['champ' => '#SAUTER']]; |
|
| 2748 | + erreur_squelette($msg, $p); |
|
| 2749 | + } else { |
|
| 2750 | + $_saut = interprete_argument_balise(1, $p); |
|
| 2751 | + $_compteur = "\$Numrows['$id_boucle']['compteur_boucle']"; |
|
| 2752 | + $_total = "(\$Numrows['$id_boucle']['total'] ?? null)"; |
|
| 2753 | 2753 | |
| 2754 | - $p->code = "vide($_compteur=\$iter->skip($_saut,$_total))"; |
|
| 2755 | - } |
|
| 2756 | - $p->interdire_scripts = false; |
|
| 2754 | + $p->code = "vide($_compteur=\$iter->skip($_saut,$_total))"; |
|
| 2755 | + } |
|
| 2756 | + $p->interdire_scripts = false; |
|
| 2757 | 2757 | |
| 2758 | - return $p; |
|
| 2758 | + return $p; |
|
| 2759 | 2759 | } |
| 2760 | 2760 | |
| 2761 | 2761 | |
@@ -2777,22 +2777,22 @@ discard block |
||
| 2777 | 2777 | * Pile complétée par le code à générer |
| 2778 | 2778 | */ |
| 2779 | 2779 | function balise_PUBLIE_dist($p) { |
| 2780 | - if (!$_type = interprete_argument_balise(1, $p)) { |
|
| 2781 | - $_type = _q($p->type_requete); |
|
| 2782 | - $_id = champ_sql($p->boucles[$p->id_boucle]->primary, $p); |
|
| 2783 | - } else { |
|
| 2784 | - $_id = interprete_argument_balise(2, $p); |
|
| 2785 | - } |
|
| 2780 | + if (!$_type = interprete_argument_balise(1, $p)) { |
|
| 2781 | + $_type = _q($p->type_requete); |
|
| 2782 | + $_id = champ_sql($p->boucles[$p->id_boucle]->primary, $p); |
|
| 2783 | + } else { |
|
| 2784 | + $_id = interprete_argument_balise(2, $p); |
|
| 2785 | + } |
|
| 2786 | 2786 | |
| 2787 | - $connect = ''; |
|
| 2788 | - if (isset($p->boucles[$p->id_boucle])) { |
|
| 2789 | - $connect = $p->boucles[$p->id_boucle]->sql_serveur; |
|
| 2790 | - } |
|
| 2787 | + $connect = ''; |
|
| 2788 | + if (isset($p->boucles[$p->id_boucle])) { |
|
| 2789 | + $connect = $p->boucles[$p->id_boucle]->sql_serveur; |
|
| 2790 | + } |
|
| 2791 | 2791 | |
| 2792 | - $p->code = '(objet_test_si_publie(' . $_type . ',intval(' . $_id . '),' . _q($connect) . ")?' ':'')"; |
|
| 2793 | - $p->interdire_scripts = false; |
|
| 2792 | + $p->code = '(objet_test_si_publie(' . $_type . ',intval(' . $_id . '),' . _q($connect) . ")?' ':'')"; |
|
| 2793 | + $p->interdire_scripts = false; |
|
| 2794 | 2794 | |
| 2795 | - return $p; |
|
| 2795 | + return $p; |
|
| 2796 | 2796 | } |
| 2797 | 2797 | |
| 2798 | 2798 | /** |
@@ -2821,12 +2821,12 @@ discard block |
||
| 2821 | 2821 | * Pile complétée par le code à générer |
| 2822 | 2822 | */ |
| 2823 | 2823 | function balise_PRODUIRE_dist($p) { |
| 2824 | - $balise_inclure = charger_fonction('INCLURE', 'balise'); |
|
| 2825 | - $p = $balise_inclure($p); |
|
| 2824 | + $balise_inclure = charger_fonction('INCLURE', 'balise'); |
|
| 2825 | + $p = $balise_inclure($p); |
|
| 2826 | 2826 | |
| 2827 | - $p->code = str_replace('recuperer_fond(', 'produire_fond_statique(', $p->code); |
|
| 2827 | + $p->code = str_replace('recuperer_fond(', 'produire_fond_statique(', $p->code); |
|
| 2828 | 2828 | |
| 2829 | - return $p; |
|
| 2829 | + return $p; |
|
| 2830 | 2830 | } |
| 2831 | 2831 | |
| 2832 | 2832 | /** |
@@ -2845,13 +2845,13 @@ discard block |
||
| 2845 | 2845 | * Pile complétée par le code à générer |
| 2846 | 2846 | */ |
| 2847 | 2847 | function balise_LARGEUR_ECRAN_dist($p) { |
| 2848 | - $_class = interprete_argument_balise(1, $p); |
|
| 2849 | - if (!$_class) { |
|
| 2850 | - $_class = 'null'; |
|
| 2851 | - } |
|
| 2852 | - $p->code = "(is_string($_class)?vide(\$GLOBALS['largeur_ecran']=$_class):(isset(\$GLOBALS['largeur_ecran'])?\$GLOBALS['largeur_ecran']:''))"; |
|
| 2848 | + $_class = interprete_argument_balise(1, $p); |
|
| 2849 | + if (!$_class) { |
|
| 2850 | + $_class = 'null'; |
|
| 2851 | + } |
|
| 2852 | + $p->code = "(is_string($_class)?vide(\$GLOBALS['largeur_ecran']=$_class):(isset(\$GLOBALS['largeur_ecran'])?\$GLOBALS['largeur_ecran']:''))"; |
|
| 2853 | 2853 | |
| 2854 | - return $p; |
|
| 2854 | + return $p; |
|
| 2855 | 2855 | } |
| 2856 | 2856 | |
| 2857 | 2857 | |
@@ -2867,14 +2867,14 @@ discard block |
||
| 2867 | 2867 | * Pile complétée par le code à générer |
| 2868 | 2868 | **/ |
| 2869 | 2869 | function balise_CONST_dist($p) { |
| 2870 | - $_const = interprete_argument_balise(1, $p); |
|
| 2871 | - if (!strlen($_const ?? '')) { |
|
| 2872 | - $p->code = "''"; |
|
| 2873 | - } |
|
| 2874 | - else { |
|
| 2875 | - $p->code = "(defined($_const)?constant($_const):'')"; |
|
| 2876 | - } |
|
| 2877 | - $p->interdire_scripts = false; |
|
| 2878 | - |
|
| 2879 | - return $p; |
|
| 2870 | + $_const = interprete_argument_balise(1, $p); |
|
| 2871 | + if (!strlen($_const ?? '')) { |
|
| 2872 | + $p->code = "''"; |
|
| 2873 | + } |
|
| 2874 | + else { |
|
| 2875 | + $p->code = "(defined($_const)?constant($_const):'')"; |
|
| 2876 | + } |
|
| 2877 | + $p->interdire_scripts = false; |
|
| 2878 | + |
|
| 2879 | + return $p; |
|
| 2880 | 2880 | } |
@@ -11,7 +11,7 @@ discard block |
||
| 11 | 11 | \***************************************************************************/ |
| 12 | 12 | |
| 13 | 13 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 14 | - return; |
|
| 14 | + return; |
|
| 15 | 15 | } |
| 16 | 16 | |
| 17 | 17 | /** |
@@ -25,9 +25,9 @@ discard block |
||
| 25 | 25 | * @return string |
| 26 | 26 | */ |
| 27 | 27 | function generer_nom_fichier_cache($contexte, $page) { |
| 28 | - $u = md5(var_export([$contexte, $page], true)); |
|
| 28 | + $u = md5(var_export([$contexte, $page], true)); |
|
| 29 | 29 | |
| 30 | - return $u . '.cache'; |
|
| 30 | + return $u . '.cache'; |
|
| 31 | 31 | } |
| 32 | 32 | |
| 33 | 33 | /** |
@@ -43,26 +43,26 @@ discard block |
||
| 43 | 43 | * @return string |
| 44 | 44 | */ |
| 45 | 45 | function cache_chemin_fichier($nom_cache, $ecrire = false) { |
| 46 | - static $l1, $l2; |
|
| 47 | - if (is_null($l1)) { |
|
| 48 | - $length = (defined('_CACHE_PROFONDEUR_STOCKAGE') ? min(8, max(_CACHE_PROFONDEUR_STOCKAGE, 2)) : 4); |
|
| 49 | - $l1 = intval(floor($length / 2)); |
|
| 50 | - $l2 = $length - $l1; |
|
| 51 | - } |
|
| 52 | - $d = substr($nom_cache, 0, $l1); |
|
| 53 | - $u = substr($nom_cache, $l1, $l2); |
|
| 54 | - |
|
| 55 | - if ($ecrire) { |
|
| 56 | - $rep = sous_repertoire(_DIR_CACHE, '', false, true); |
|
| 57 | - $rep = sous_repertoire($rep, 'calcul/', false, true); |
|
| 58 | - $rep = sous_repertoire($rep, $d, false, true); |
|
| 59 | - } |
|
| 60 | - else { |
|
| 61 | - // en lecture on essaye pas de creer les repertoires, on va au plus vite |
|
| 62 | - $rep = _DIR_CACHE . "calcul/$d/"; |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - return $rep . $u . '.cache'; |
|
| 46 | + static $l1, $l2; |
|
| 47 | + if (is_null($l1)) { |
|
| 48 | + $length = (defined('_CACHE_PROFONDEUR_STOCKAGE') ? min(8, max(_CACHE_PROFONDEUR_STOCKAGE, 2)) : 4); |
|
| 49 | + $l1 = intval(floor($length / 2)); |
|
| 50 | + $l2 = $length - $l1; |
|
| 51 | + } |
|
| 52 | + $d = substr($nom_cache, 0, $l1); |
|
| 53 | + $u = substr($nom_cache, $l1, $l2); |
|
| 54 | + |
|
| 55 | + if ($ecrire) { |
|
| 56 | + $rep = sous_repertoire(_DIR_CACHE, '', false, true); |
|
| 57 | + $rep = sous_repertoire($rep, 'calcul/', false, true); |
|
| 58 | + $rep = sous_repertoire($rep, $d, false, true); |
|
| 59 | + } |
|
| 60 | + else { |
|
| 61 | + // en lecture on essaye pas de creer les repertoires, on va au plus vite |
|
| 62 | + $rep = _DIR_CACHE . "calcul/$d/"; |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + return $rep . $u . '.cache'; |
|
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | /** |
@@ -73,7 +73,7 @@ discard block |
||
| 73 | 73 | * @return bool |
| 74 | 74 | */ |
| 75 | 75 | function ecrire_cache($nom_cache, $valeur) { |
| 76 | - return ecrire_fichier(cache_chemin_fichier($nom_cache, true), serialize(['nom_cache' => $nom_cache, 'valeur' => $valeur])); |
|
| 76 | + return ecrire_fichier(cache_chemin_fichier($nom_cache, true), serialize(['nom_cache' => $nom_cache, 'valeur' => $valeur])); |
|
| 77 | 77 | } |
| 78 | 78 | |
| 79 | 79 | /** |
@@ -83,38 +83,38 @@ discard block |
||
| 83 | 83 | * @return mixed |
| 84 | 84 | */ |
| 85 | 85 | function lire_cache($nom_cache) { |
| 86 | - $tmp = []; |
|
| 87 | - if ( |
|
| 88 | - file_exists($f = cache_chemin_fichier($nom_cache)) |
|
| 89 | - and lire_fichier($f, $tmp) |
|
| 90 | - and $tmp = unserialize($tmp) |
|
| 91 | - and $tmp['nom_cache'] == $nom_cache |
|
| 92 | - and isset($tmp['valeur']) |
|
| 93 | - ) { |
|
| 94 | - return $tmp['valeur']; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - return false; |
|
| 86 | + $tmp = []; |
|
| 87 | + if ( |
|
| 88 | + file_exists($f = cache_chemin_fichier($nom_cache)) |
|
| 89 | + and lire_fichier($f, $tmp) |
|
| 90 | + and $tmp = unserialize($tmp) |
|
| 91 | + and $tmp['nom_cache'] == $nom_cache |
|
| 92 | + and isset($tmp['valeur']) |
|
| 93 | + ) { |
|
| 94 | + return $tmp['valeur']; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + return false; |
|
| 98 | 98 | } |
| 99 | 99 | |
| 100 | 100 | // Parano : on signe le cache, afin d'interdire un hack d'injection |
| 101 | 101 | // dans notre memcache |
| 102 | 102 | function cache_signature(&$page) { |
| 103 | - if (!isset($GLOBALS['meta']['cache_signature'])) { |
|
| 104 | - include_spip('inc/acces'); |
|
| 105 | - include_spip('auth/sha256.inc'); |
|
| 106 | - ecrire_meta( |
|
| 107 | - 'cache_signature', |
|
| 108 | - spip_sha256( |
|
| 109 | - $_SERVER['DOCUMENT_ROOT'] |
|
| 110 | - . ($_SERVER['SERVER_SIGNATURE'] ?? '') |
|
| 111 | - . creer_uniqid() |
|
| 112 | - ), |
|
| 113 | - 'non' |
|
| 114 | - ); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - return crc32($GLOBALS['meta']['cache_signature'] . $page['texte']); |
|
| 103 | + if (!isset($GLOBALS['meta']['cache_signature'])) { |
|
| 104 | + include_spip('inc/acces'); |
|
| 105 | + include_spip('auth/sha256.inc'); |
|
| 106 | + ecrire_meta( |
|
| 107 | + 'cache_signature', |
|
| 108 | + spip_sha256( |
|
| 109 | + $_SERVER['DOCUMENT_ROOT'] |
|
| 110 | + . ($_SERVER['SERVER_SIGNATURE'] ?? '') |
|
| 111 | + . creer_uniqid() |
|
| 112 | + ), |
|
| 113 | + 'non' |
|
| 114 | + ); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + return crc32($GLOBALS['meta']['cache_signature'] . $page['texte']); |
|
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | /** |
@@ -127,14 +127,14 @@ discard block |
||
| 127 | 127 | * @return array |
| 128 | 128 | */ |
| 129 | 129 | function gzip_page($page) { |
| 130 | - if (function_exists('gzcompress') and strlen($page['texte']) > 16 * 1024) { |
|
| 131 | - $page['gz'] = true; |
|
| 132 | - $page['texte'] = gzcompress($page['texte']); |
|
| 133 | - } else { |
|
| 134 | - $page['gz'] = false; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - return $page; |
|
| 130 | + if (function_exists('gzcompress') and strlen($page['texte']) > 16 * 1024) { |
|
| 131 | + $page['gz'] = true; |
|
| 132 | + $page['texte'] = gzcompress($page['texte']); |
|
| 133 | + } else { |
|
| 134 | + $page['gz'] = false; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + return $page; |
|
| 138 | 138 | } |
| 139 | 139 | |
| 140 | 140 | /** |
@@ -147,10 +147,10 @@ discard block |
||
| 147 | 147 | * @return void |
| 148 | 148 | */ |
| 149 | 149 | function gunzip_page(&$page) { |
| 150 | - if ($page['gz']) { |
|
| 151 | - $page['texte'] = gzuncompress($page['texte']); |
|
| 152 | - $page['gz'] = false; // ne pas gzuncompress deux fois une meme page |
|
| 153 | - } |
|
| 150 | + if ($page['gz']) { |
|
| 151 | + $page['texte'] = gzuncompress($page['texte']); |
|
| 152 | + $page['gz'] = false; // ne pas gzuncompress deux fois une meme page |
|
| 153 | + } |
|
| 154 | 154 | } |
| 155 | 155 | |
| 156 | 156 | /** |
@@ -165,72 +165,72 @@ discard block |
||
| 165 | 165 | * -1 si il faut calculer sans stocker en cache |
| 166 | 166 | */ |
| 167 | 167 | function cache_valide(&$page, $date) { |
| 168 | - $now = $_SERVER['REQUEST_TIME']; |
|
| 169 | - |
|
| 170 | - // Apparition d'un nouvel article post-date ? |
|
| 171 | - if ( |
|
| 172 | - isset($GLOBALS['meta']['post_dates']) |
|
| 173 | - and $GLOBALS['meta']['post_dates'] == 'non' |
|
| 174 | - and isset($GLOBALS['meta']['date_prochain_postdate']) |
|
| 175 | - and $now > $GLOBALS['meta']['date_prochain_postdate'] |
|
| 176 | - ) { |
|
| 177 | - spip_log('Un article post-date invalide le cache'); |
|
| 178 | - include_spip('inc/rubriques'); |
|
| 179 | - calculer_prochain_postdate(true); |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - if (defined('_VAR_NOCACHE') and _VAR_NOCACHE) { |
|
| 183 | - return -1; |
|
| 184 | - } |
|
| 185 | - if (isset($GLOBALS['meta']['cache_inhib']) and $_SERVER['REQUEST_TIME'] < $GLOBALS['meta']['cache_inhib']) { |
|
| 186 | - return -1; |
|
| 187 | - } |
|
| 188 | - if (defined('_NO_CACHE')) { |
|
| 189 | - return (_NO_CACHE == 0 and !isset($page['texte'])) ? 1 : _NO_CACHE; |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - // pas de cache ? on le met a jour, sauf pour les bots (on leur calcule la page sans mise en cache) |
|
| 193 | - if (!$page or !isset($page['texte']) or !isset($page['entetes']['X-Spip-Cache'])) { |
|
| 194 | - return _IS_BOT ? -1 : 1; |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - // controle de la signature |
|
| 198 | - if ($page['sig'] !== cache_signature($page)) { |
|
| 199 | - return _IS_BOT ? -1 : 1; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - // #CACHE{n,statique} => on n'invalide pas avec derniere_modif |
|
| 203 | - // cf. ecrire/public/balises.php, balise_CACHE_dist() |
|
| 204 | - if (!isset($page['entetes']['X-Spip-Statique']) or $page['entetes']['X-Spip-Statique'] !== 'oui') { |
|
| 205 | - // Cache invalide par la meta 'derniere_modif' |
|
| 206 | - // sauf pour les bots, qui utilisent toujours le cache |
|
| 207 | - if ( |
|
| 208 | - !_IS_BOT |
|
| 209 | - and $GLOBALS['derniere_modif_invalide'] |
|
| 210 | - and isset($GLOBALS['meta']['derniere_modif']) |
|
| 211 | - and $date < $GLOBALS['meta']['derniere_modif'] |
|
| 212 | - ) { |
|
| 213 | - return 1; |
|
| 214 | - } |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - // Sinon comparer l'age du fichier a sa duree de cache |
|
| 218 | - $duree = intval($page['entetes']['X-Spip-Cache']); |
|
| 219 | - $cache_mark = ($GLOBALS['meta']['cache_mark'] ?? 0); |
|
| 220 | - if ($duree == 0) { #CACHE{0} |
|
| 221 | - return -1; |
|
| 222 | - } // sauf pour les bots, qui utilisent toujours le cache |
|
| 223 | - else { |
|
| 224 | - if ( |
|
| 225 | - (!_IS_BOT and $date + $duree < $now) |
|
| 226 | - # le cache est anterieur a la derniere purge : l'ignorer, meme pour les bots |
|
| 227 | - or $date < $cache_mark |
|
| 228 | - ) { |
|
| 229 | - return _IS_BOT ? -1 : 1; |
|
| 230 | - } else { |
|
| 231 | - return 0; |
|
| 232 | - } |
|
| 233 | - } |
|
| 168 | + $now = $_SERVER['REQUEST_TIME']; |
|
| 169 | + |
|
| 170 | + // Apparition d'un nouvel article post-date ? |
|
| 171 | + if ( |
|
| 172 | + isset($GLOBALS['meta']['post_dates']) |
|
| 173 | + and $GLOBALS['meta']['post_dates'] == 'non' |
|
| 174 | + and isset($GLOBALS['meta']['date_prochain_postdate']) |
|
| 175 | + and $now > $GLOBALS['meta']['date_prochain_postdate'] |
|
| 176 | + ) { |
|
| 177 | + spip_log('Un article post-date invalide le cache'); |
|
| 178 | + include_spip('inc/rubriques'); |
|
| 179 | + calculer_prochain_postdate(true); |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + if (defined('_VAR_NOCACHE') and _VAR_NOCACHE) { |
|
| 183 | + return -1; |
|
| 184 | + } |
|
| 185 | + if (isset($GLOBALS['meta']['cache_inhib']) and $_SERVER['REQUEST_TIME'] < $GLOBALS['meta']['cache_inhib']) { |
|
| 186 | + return -1; |
|
| 187 | + } |
|
| 188 | + if (defined('_NO_CACHE')) { |
|
| 189 | + return (_NO_CACHE == 0 and !isset($page['texte'])) ? 1 : _NO_CACHE; |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + // pas de cache ? on le met a jour, sauf pour les bots (on leur calcule la page sans mise en cache) |
|
| 193 | + if (!$page or !isset($page['texte']) or !isset($page['entetes']['X-Spip-Cache'])) { |
|
| 194 | + return _IS_BOT ? -1 : 1; |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + // controle de la signature |
|
| 198 | + if ($page['sig'] !== cache_signature($page)) { |
|
| 199 | + return _IS_BOT ? -1 : 1; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + // #CACHE{n,statique} => on n'invalide pas avec derniere_modif |
|
| 203 | + // cf. ecrire/public/balises.php, balise_CACHE_dist() |
|
| 204 | + if (!isset($page['entetes']['X-Spip-Statique']) or $page['entetes']['X-Spip-Statique'] !== 'oui') { |
|
| 205 | + // Cache invalide par la meta 'derniere_modif' |
|
| 206 | + // sauf pour les bots, qui utilisent toujours le cache |
|
| 207 | + if ( |
|
| 208 | + !_IS_BOT |
|
| 209 | + and $GLOBALS['derniere_modif_invalide'] |
|
| 210 | + and isset($GLOBALS['meta']['derniere_modif']) |
|
| 211 | + and $date < $GLOBALS['meta']['derniere_modif'] |
|
| 212 | + ) { |
|
| 213 | + return 1; |
|
| 214 | + } |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + // Sinon comparer l'age du fichier a sa duree de cache |
|
| 218 | + $duree = intval($page['entetes']['X-Spip-Cache']); |
|
| 219 | + $cache_mark = ($GLOBALS['meta']['cache_mark'] ?? 0); |
|
| 220 | + if ($duree == 0) { #CACHE{0} |
|
| 221 | + return -1; |
|
| 222 | + } // sauf pour les bots, qui utilisent toujours le cache |
|
| 223 | + else { |
|
| 224 | + if ( |
|
| 225 | + (!_IS_BOT and $date + $duree < $now) |
|
| 226 | + # le cache est anterieur a la derniere purge : l'ignorer, meme pour les bots |
|
| 227 | + or $date < $cache_mark |
|
| 228 | + ) { |
|
| 229 | + return _IS_BOT ? -1 : 1; |
|
| 230 | + } else { |
|
| 231 | + return 0; |
|
| 232 | + } |
|
| 233 | + } |
|
| 234 | 234 | } |
| 235 | 235 | |
| 236 | 236 | /** |
@@ -243,59 +243,59 @@ discard block |
||
| 243 | 243 | */ |
| 244 | 244 | function creer_cache(&$page, &$chemin_cache) { |
| 245 | 245 | |
| 246 | - // Ne rien faire si on est en preview, debug, ou si une erreur |
|
| 247 | - // grave s'est presentee (compilation du squelette, MySQL, etc) |
|
| 248 | - // le cas var_nocache ne devrait jamais arriver ici (securite) |
|
| 249 | - // le cas spip_interdire_cache correspond a une ereur SQL grave non anticipable |
|
| 250 | - if ( |
|
| 251 | - (defined('_VAR_NOCACHE') and _VAR_NOCACHE) |
|
| 252 | - or defined('spip_interdire_cache') |
|
| 253 | - ) { |
|
| 254 | - return; |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - // Si la page c1234 a un invalideur de session 'zz', sauver dans |
|
| 258 | - // 'tmp/cache/MD5(chemin_cache)_zz' |
|
| 259 | - if ( |
|
| 260 | - isset($page['invalideurs']) |
|
| 261 | - and isset($page['invalideurs']['session']) |
|
| 262 | - ) { |
|
| 263 | - // on verifie que le contenu du chemin cache indique seulement |
|
| 264 | - // "cache sessionne" ; sa date indique la date de validite |
|
| 265 | - // des caches sessionnes |
|
| 266 | - if (!$tmp = lire_cache($chemin_cache)) { |
|
| 267 | - spip_log('Creation cache sessionne ' . $chemin_cache); |
|
| 268 | - $tmp = [ |
|
| 269 | - 'invalideurs' => ['session' => ''], |
|
| 270 | - 'lastmodified' => $_SERVER['REQUEST_TIME'] |
|
| 271 | - ]; |
|
| 272 | - ecrire_cache($chemin_cache, $tmp); |
|
| 273 | - } |
|
| 274 | - $chemin_cache = generer_nom_fichier_cache( |
|
| 275 | - ['chemin_cache' => $chemin_cache], |
|
| 276 | - ['session' => $page['invalideurs']['session']] |
|
| 277 | - ); |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - // ajouter la date de production dans le cache lui meme |
|
| 281 | - // (qui contient deja sa duree de validite) |
|
| 282 | - $page['lastmodified'] = $_SERVER['REQUEST_TIME']; |
|
| 283 | - |
|
| 284 | - // compresser le contenu si besoin |
|
| 285 | - $pagez = gzip_page($page); |
|
| 286 | - |
|
| 287 | - // signer le contenu |
|
| 288 | - $pagez['sig'] = cache_signature($pagez); |
|
| 289 | - |
|
| 290 | - // l'enregistrer, compresse ou non... |
|
| 291 | - $ok = ecrire_cache($chemin_cache, $pagez); |
|
| 292 | - |
|
| 293 | - spip_log((_IS_BOT ? 'Bot:' : '') . "Creation du cache $chemin_cache pour " |
|
| 294 | - . $page['entetes']['X-Spip-Cache'] . ' secondes' . ($ok ? '' : ' (erreur!)'), _LOG_INFO); |
|
| 295 | - |
|
| 296 | - // Inserer ses invalideurs |
|
| 297 | - include_spip('inc/invalideur'); |
|
| 298 | - maj_invalideurs($chemin_cache, $page); |
|
| 246 | + // Ne rien faire si on est en preview, debug, ou si une erreur |
|
| 247 | + // grave s'est presentee (compilation du squelette, MySQL, etc) |
|
| 248 | + // le cas var_nocache ne devrait jamais arriver ici (securite) |
|
| 249 | + // le cas spip_interdire_cache correspond a une ereur SQL grave non anticipable |
|
| 250 | + if ( |
|
| 251 | + (defined('_VAR_NOCACHE') and _VAR_NOCACHE) |
|
| 252 | + or defined('spip_interdire_cache') |
|
| 253 | + ) { |
|
| 254 | + return; |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + // Si la page c1234 a un invalideur de session 'zz', sauver dans |
|
| 258 | + // 'tmp/cache/MD5(chemin_cache)_zz' |
|
| 259 | + if ( |
|
| 260 | + isset($page['invalideurs']) |
|
| 261 | + and isset($page['invalideurs']['session']) |
|
| 262 | + ) { |
|
| 263 | + // on verifie que le contenu du chemin cache indique seulement |
|
| 264 | + // "cache sessionne" ; sa date indique la date de validite |
|
| 265 | + // des caches sessionnes |
|
| 266 | + if (!$tmp = lire_cache($chemin_cache)) { |
|
| 267 | + spip_log('Creation cache sessionne ' . $chemin_cache); |
|
| 268 | + $tmp = [ |
|
| 269 | + 'invalideurs' => ['session' => ''], |
|
| 270 | + 'lastmodified' => $_SERVER['REQUEST_TIME'] |
|
| 271 | + ]; |
|
| 272 | + ecrire_cache($chemin_cache, $tmp); |
|
| 273 | + } |
|
| 274 | + $chemin_cache = generer_nom_fichier_cache( |
|
| 275 | + ['chemin_cache' => $chemin_cache], |
|
| 276 | + ['session' => $page['invalideurs']['session']] |
|
| 277 | + ); |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + // ajouter la date de production dans le cache lui meme |
|
| 281 | + // (qui contient deja sa duree de validite) |
|
| 282 | + $page['lastmodified'] = $_SERVER['REQUEST_TIME']; |
|
| 283 | + |
|
| 284 | + // compresser le contenu si besoin |
|
| 285 | + $pagez = gzip_page($page); |
|
| 286 | + |
|
| 287 | + // signer le contenu |
|
| 288 | + $pagez['sig'] = cache_signature($pagez); |
|
| 289 | + |
|
| 290 | + // l'enregistrer, compresse ou non... |
|
| 291 | + $ok = ecrire_cache($chemin_cache, $pagez); |
|
| 292 | + |
|
| 293 | + spip_log((_IS_BOT ? 'Bot:' : '') . "Creation du cache $chemin_cache pour " |
|
| 294 | + . $page['entetes']['X-Spip-Cache'] . ' secondes' . ($ok ? '' : ' (erreur!)'), _LOG_INFO); |
|
| 295 | + |
|
| 296 | + // Inserer ses invalideurs |
|
| 297 | + include_spip('inc/invalideur'); |
|
| 298 | + maj_invalideurs($chemin_cache, $page); |
|
| 299 | 299 | } |
| 300 | 300 | |
| 301 | 301 | |
@@ -308,15 +308,15 @@ discard block |
||
| 308 | 308 | * @return void |
| 309 | 309 | */ |
| 310 | 310 | function nettoyer_petit_cache($prefix, $duree = 300) { |
| 311 | - // determiner le repertoire a purger : 'tmp/CACHE/rech/' |
|
| 312 | - $dircache = sous_repertoire(_DIR_CACHE, $prefix); |
|
| 313 | - if (spip_touch($dircache . 'purger_' . $prefix, $duree, true)) { |
|
| 314 | - foreach (preg_files($dircache, '[.]txt$') as $f) { |
|
| 315 | - if ($_SERVER['REQUEST_TIME'] - (@file_exists($f) ? @filemtime($f) : 0) > $duree) { |
|
| 316 | - spip_unlink($f); |
|
| 317 | - } |
|
| 318 | - } |
|
| 319 | - } |
|
| 311 | + // determiner le repertoire a purger : 'tmp/CACHE/rech/' |
|
| 312 | + $dircache = sous_repertoire(_DIR_CACHE, $prefix); |
|
| 313 | + if (spip_touch($dircache . 'purger_' . $prefix, $duree, true)) { |
|
| 314 | + foreach (preg_files($dircache, '[.]txt$') as $f) { |
|
| 315 | + if ($_SERVER['REQUEST_TIME'] - (@file_exists($f) ? @filemtime($f) : 0) > $duree) { |
|
| 316 | + spip_unlink($f); |
|
| 317 | + } |
|
| 318 | + } |
|
| 319 | + } |
|
| 320 | 320 | } |
| 321 | 321 | |
| 322 | 322 | |
@@ -344,134 +344,134 @@ discard block |
||
| 344 | 344 | */ |
| 345 | 345 | function public_cacher_dist($contexte, &$use_cache, &$chemin_cache, &$page, &$lastmodified) { |
| 346 | 346 | |
| 347 | - # fonction de cache minimale : dire "non on ne met rien en cache" |
|
| 348 | - # $use_cache = -1; return; |
|
| 349 | - |
|
| 350 | - // Second appel, destine a l'enregistrement du cache sur le disque |
|
| 351 | - if (isset($chemin_cache)) { |
|
| 352 | - creer_cache($page, $chemin_cache); |
|
| 353 | - return; |
|
| 354 | - } |
|
| 355 | - |
|
| 356 | - // Toute la suite correspond au premier appel |
|
| 357 | - $contexte_implicite = $page['contexte_implicite']; |
|
| 358 | - |
|
| 359 | - // Cas ignorant le cache car completement dynamique |
|
| 360 | - if ( |
|
| 361 | - (!empty($_SERVER['REQUEST_METHOD']) and $_SERVER['REQUEST_METHOD'] === 'POST') |
|
| 362 | - or _request('connect') |
|
| 363 | - ) { |
|
| 364 | - $use_cache = -1; |
|
| 365 | - $lastmodified = 0; |
|
| 366 | - $chemin_cache = ''; |
|
| 367 | - $page = []; |
|
| 368 | - |
|
| 369 | - return; |
|
| 370 | - } |
|
| 371 | - |
|
| 372 | - // Controler l'existence d'un cache nous correspondant |
|
| 373 | - $chemin_cache = generer_nom_fichier_cache($contexte, $page); |
|
| 374 | - $lastmodified = 0; |
|
| 375 | - |
|
| 376 | - // charger le cache s'il existe (et si il a bien le bon hash = anticollision) |
|
| 377 | - if (!$page = lire_cache($chemin_cache)) { |
|
| 378 | - $page = []; |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - // s'il est sessionne, charger celui correspondant a notre session |
|
| 382 | - if ( |
|
| 383 | - isset($page['invalideurs']) |
|
| 384 | - and isset($page['invalideurs']['session']) |
|
| 385 | - ) { |
|
| 386 | - $chemin_cache_session = generer_nom_fichier_cache( |
|
| 387 | - ['chemin_cache' => $chemin_cache], |
|
| 388 | - ['session' => spip_session()] |
|
| 389 | - ); |
|
| 390 | - if ( |
|
| 391 | - $page_session = lire_cache($chemin_cache_session) |
|
| 392 | - and $page_session['lastmodified'] >= $page['lastmodified'] |
|
| 393 | - ) { |
|
| 394 | - $page = $page_session; |
|
| 395 | - } else { |
|
| 396 | - $page = []; |
|
| 397 | - } |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - |
|
| 401 | - // Faut-il effacer des pages invalidees (en particulier ce cache-ci) ? |
|
| 402 | - if (isset($GLOBALS['meta']['invalider'])) { |
|
| 403 | - // ne le faire que si la base est disponible |
|
| 404 | - if (spip_connect()) { |
|
| 405 | - include_spip('inc/invalideur'); |
|
| 406 | - retire_caches($chemin_cache); # API invalideur inutile |
|
| 407 | - supprimer_fichier(_DIR_CACHE . $chemin_cache); |
|
| 408 | - if (isset($chemin_cache_session) and $chemin_cache_session) { |
|
| 409 | - supprimer_fichier(_DIR_CACHE . $chemin_cache_session); |
|
| 410 | - } |
|
| 411 | - } |
|
| 412 | - } |
|
| 413 | - |
|
| 414 | - // Si un calcul, recalcul [ou preview, mais c'est recalcul] est demande, |
|
| 415 | - // on supprime le cache |
|
| 416 | - if ( |
|
| 417 | - defined('_VAR_MODE') && |
|
| 418 | - _VAR_MODE && |
|
| 419 | - (isset($_COOKIE['spip_session']) || |
|
| 420 | - isset($_COOKIE['spip_admin']) || |
|
| 421 | - @file_exists(_ACCESS_FILE_NAME)) |
|
| 422 | - ) { |
|
| 423 | - $page = ['contexte_implicite' => $contexte_implicite]; // ignorer le cache deja lu |
|
| 424 | - include_spip('inc/invalideur'); |
|
| 425 | - retire_caches($chemin_cache); # API invalideur inutile |
|
| 426 | - supprimer_fichier(_DIR_CACHE . $chemin_cache); |
|
| 427 | - if (isset($chemin_cache_session) and $chemin_cache_session) { |
|
| 428 | - supprimer_fichier(_DIR_CACHE . $chemin_cache_session); |
|
| 429 | - } |
|
| 430 | - } |
|
| 431 | - |
|
| 432 | - // $delais par defaut |
|
| 433 | - // pour toutes les pages sans #CACHE{} hors modeles/ et espace privé |
|
| 434 | - // qui sont a cache nul par defaut |
|
| 435 | - if (!isset($GLOBALS['delais'])) { |
|
| 436 | - if (!defined('_DUREE_CACHE_DEFAUT')) { |
|
| 437 | - define('_DUREE_CACHE_DEFAUT', 24 * 3600); |
|
| 438 | - } |
|
| 439 | - $GLOBALS['delais'] = _DUREE_CACHE_DEFAUT; |
|
| 440 | - } |
|
| 441 | - |
|
| 442 | - // determiner la validite de la page |
|
| 443 | - if ($page) { |
|
| 444 | - $use_cache = cache_valide($page, $page['lastmodified'] ?? 0); |
|
| 445 | - // le contexte implicite n'est pas stocke dans le cache, mais il y a equivalence |
|
| 446 | - // par le nom du cache. On le reinjecte donc ici pour utilisation eventuelle au calcul |
|
| 447 | - $page['contexte_implicite'] = $contexte_implicite; |
|
| 448 | - if (!$use_cache) { |
|
| 449 | - // $page est un cache utilisable |
|
| 450 | - gunzip_page($page); |
|
| 451 | - |
|
| 452 | - return; |
|
| 453 | - } |
|
| 454 | - } else { |
|
| 455 | - $page = ['contexte_implicite' => $contexte_implicite]; |
|
| 456 | - $use_cache = cache_valide($page, 0); // fichier cache absent : provoque le calcul |
|
| 457 | - } |
|
| 458 | - |
|
| 459 | - // Si pas valide mais pas de connexion a la base, le garder quand meme |
|
| 460 | - if (!spip_connect()) { |
|
| 461 | - if (isset($page['texte'])) { |
|
| 462 | - gunzip_page($page); |
|
| 463 | - $use_cache = 0; |
|
| 464 | - } else { |
|
| 465 | - spip_log("Erreur base de donnees, impossible utiliser $chemin_cache"); |
|
| 466 | - include_spip('inc/minipres'); |
|
| 467 | - |
|
| 468 | - return minipres(_T('info_travaux_titre'), _T('titre_probleme_technique'), ['status' => 503]); |
|
| 469 | - } |
|
| 470 | - } |
|
| 471 | - |
|
| 472 | - if ($use_cache < 0) { |
|
| 473 | - $chemin_cache = ''; |
|
| 474 | - } |
|
| 475 | - |
|
| 476 | - return; |
|
| 347 | + # fonction de cache minimale : dire "non on ne met rien en cache" |
|
| 348 | + # $use_cache = -1; return; |
|
| 349 | + |
|
| 350 | + // Second appel, destine a l'enregistrement du cache sur le disque |
|
| 351 | + if (isset($chemin_cache)) { |
|
| 352 | + creer_cache($page, $chemin_cache); |
|
| 353 | + return; |
|
| 354 | + } |
|
| 355 | + |
|
| 356 | + // Toute la suite correspond au premier appel |
|
| 357 | + $contexte_implicite = $page['contexte_implicite']; |
|
| 358 | + |
|
| 359 | + // Cas ignorant le cache car completement dynamique |
|
| 360 | + if ( |
|
| 361 | + (!empty($_SERVER['REQUEST_METHOD']) and $_SERVER['REQUEST_METHOD'] === 'POST') |
|
| 362 | + or _request('connect') |
|
| 363 | + ) { |
|
| 364 | + $use_cache = -1; |
|
| 365 | + $lastmodified = 0; |
|
| 366 | + $chemin_cache = ''; |
|
| 367 | + $page = []; |
|
| 368 | + |
|
| 369 | + return; |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + // Controler l'existence d'un cache nous correspondant |
|
| 373 | + $chemin_cache = generer_nom_fichier_cache($contexte, $page); |
|
| 374 | + $lastmodified = 0; |
|
| 375 | + |
|
| 376 | + // charger le cache s'il existe (et si il a bien le bon hash = anticollision) |
|
| 377 | + if (!$page = lire_cache($chemin_cache)) { |
|
| 378 | + $page = []; |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + // s'il est sessionne, charger celui correspondant a notre session |
|
| 382 | + if ( |
|
| 383 | + isset($page['invalideurs']) |
|
| 384 | + and isset($page['invalideurs']['session']) |
|
| 385 | + ) { |
|
| 386 | + $chemin_cache_session = generer_nom_fichier_cache( |
|
| 387 | + ['chemin_cache' => $chemin_cache], |
|
| 388 | + ['session' => spip_session()] |
|
| 389 | + ); |
|
| 390 | + if ( |
|
| 391 | + $page_session = lire_cache($chemin_cache_session) |
|
| 392 | + and $page_session['lastmodified'] >= $page['lastmodified'] |
|
| 393 | + ) { |
|
| 394 | + $page = $page_session; |
|
| 395 | + } else { |
|
| 396 | + $page = []; |
|
| 397 | + } |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + |
|
| 401 | + // Faut-il effacer des pages invalidees (en particulier ce cache-ci) ? |
|
| 402 | + if (isset($GLOBALS['meta']['invalider'])) { |
|
| 403 | + // ne le faire que si la base est disponible |
|
| 404 | + if (spip_connect()) { |
|
| 405 | + include_spip('inc/invalideur'); |
|
| 406 | + retire_caches($chemin_cache); # API invalideur inutile |
|
| 407 | + supprimer_fichier(_DIR_CACHE . $chemin_cache); |
|
| 408 | + if (isset($chemin_cache_session) and $chemin_cache_session) { |
|
| 409 | + supprimer_fichier(_DIR_CACHE . $chemin_cache_session); |
|
| 410 | + } |
|
| 411 | + } |
|
| 412 | + } |
|
| 413 | + |
|
| 414 | + // Si un calcul, recalcul [ou preview, mais c'est recalcul] est demande, |
|
| 415 | + // on supprime le cache |
|
| 416 | + if ( |
|
| 417 | + defined('_VAR_MODE') && |
|
| 418 | + _VAR_MODE && |
|
| 419 | + (isset($_COOKIE['spip_session']) || |
|
| 420 | + isset($_COOKIE['spip_admin']) || |
|
| 421 | + @file_exists(_ACCESS_FILE_NAME)) |
|
| 422 | + ) { |
|
| 423 | + $page = ['contexte_implicite' => $contexte_implicite]; // ignorer le cache deja lu |
|
| 424 | + include_spip('inc/invalideur'); |
|
| 425 | + retire_caches($chemin_cache); # API invalideur inutile |
|
| 426 | + supprimer_fichier(_DIR_CACHE . $chemin_cache); |
|
| 427 | + if (isset($chemin_cache_session) and $chemin_cache_session) { |
|
| 428 | + supprimer_fichier(_DIR_CACHE . $chemin_cache_session); |
|
| 429 | + } |
|
| 430 | + } |
|
| 431 | + |
|
| 432 | + // $delais par defaut |
|
| 433 | + // pour toutes les pages sans #CACHE{} hors modeles/ et espace privé |
|
| 434 | + // qui sont a cache nul par defaut |
|
| 435 | + if (!isset($GLOBALS['delais'])) { |
|
| 436 | + if (!defined('_DUREE_CACHE_DEFAUT')) { |
|
| 437 | + define('_DUREE_CACHE_DEFAUT', 24 * 3600); |
|
| 438 | + } |
|
| 439 | + $GLOBALS['delais'] = _DUREE_CACHE_DEFAUT; |
|
| 440 | + } |
|
| 441 | + |
|
| 442 | + // determiner la validite de la page |
|
| 443 | + if ($page) { |
|
| 444 | + $use_cache = cache_valide($page, $page['lastmodified'] ?? 0); |
|
| 445 | + // le contexte implicite n'est pas stocke dans le cache, mais il y a equivalence |
|
| 446 | + // par le nom du cache. On le reinjecte donc ici pour utilisation eventuelle au calcul |
|
| 447 | + $page['contexte_implicite'] = $contexte_implicite; |
|
| 448 | + if (!$use_cache) { |
|
| 449 | + // $page est un cache utilisable |
|
| 450 | + gunzip_page($page); |
|
| 451 | + |
|
| 452 | + return; |
|
| 453 | + } |
|
| 454 | + } else { |
|
| 455 | + $page = ['contexte_implicite' => $contexte_implicite]; |
|
| 456 | + $use_cache = cache_valide($page, 0); // fichier cache absent : provoque le calcul |
|
| 457 | + } |
|
| 458 | + |
|
| 459 | + // Si pas valide mais pas de connexion a la base, le garder quand meme |
|
| 460 | + if (!spip_connect()) { |
|
| 461 | + if (isset($page['texte'])) { |
|
| 462 | + gunzip_page($page); |
|
| 463 | + $use_cache = 0; |
|
| 464 | + } else { |
|
| 465 | + spip_log("Erreur base de donnees, impossible utiliser $chemin_cache"); |
|
| 466 | + include_spip('inc/minipres'); |
|
| 467 | + |
|
| 468 | + return minipres(_T('info_travaux_titre'), _T('titre_probleme_technique'), ['status' => 503]); |
|
| 469 | + } |
|
| 470 | + } |
|
| 471 | + |
|
| 472 | + if ($use_cache < 0) { |
|
| 473 | + $chemin_cache = ''; |
|
| 474 | + } |
|
| 475 | + |
|
| 476 | + return; |
|
| 477 | 477 | } |
@@ -20,178 +20,178 @@ discard block |
||
| 20 | 20 | **/ |
| 21 | 21 | |
| 22 | 22 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 23 | - return; |
|
| 23 | + return; |
|
| 24 | 24 | } |
| 25 | 25 | |
| 26 | 26 | // En cas de modification, il faut aussi actualiser la regexp de nettoyer_uri_var() dans inc/utils.php |
| 27 | 27 | if (!defined('_CONTEXTE_IGNORE_VARIABLES')) { |
| 28 | - define('_CONTEXTE_IGNORE_VARIABLES', '/(^var_|^PHPSESSID$|^fbclid$|^utm_)/'); |
|
| 28 | + define('_CONTEXTE_IGNORE_VARIABLES', '/(^var_|^PHPSESSID$|^fbclid$|^utm_)/'); |
|
| 29 | 29 | } |
| 30 | 30 | |
| 31 | 31 | function assembler($fond, string $connect = '') { |
| 32 | 32 | |
| 33 | - $chemin_cache = null; |
|
| 34 | - $lastmodified = null; |
|
| 35 | - $res = null; |
|
| 36 | - // flag_preserver est modifie ici, et utilise en globale |
|
| 37 | - // use_cache sert a informer le bouton d'admin pr savoir s'il met un * |
|
| 38 | - // contexte est utilise en globale dans le formulaire d'admin |
|
| 39 | - |
|
| 40 | - $GLOBALS['contexte'] = calculer_contexte(); |
|
| 41 | - $page = ['contexte_implicite' => calculer_contexte_implicite()]; |
|
| 42 | - $page['contexte_implicite']['cache'] = $fond . preg_replace( |
|
| 43 | - ',\.[a-zA-Z0-9]*$,', |
|
| 44 | - '', |
|
| 45 | - preg_replace('/[?].*$/', '', $GLOBALS['REQUEST_URI']) |
|
| 46 | - ); |
|
| 47 | - // Cette fonction est utilisee deux fois |
|
| 48 | - $cacher = charger_fonction('cacher', 'public', true); |
|
| 49 | - // Les quatre derniers parametres sont modifies par la fonction: |
|
| 50 | - // emplacement, validite, et, s'il est valide, contenu & age |
|
| 51 | - if ($cacher) { |
|
| 52 | - $res = $cacher($GLOBALS['contexte'], $GLOBALS['use_cache'], $chemin_cache, $page, $lastmodified); |
|
| 53 | - } else { |
|
| 54 | - $GLOBALS['use_cache'] = -1; |
|
| 55 | - } |
|
| 56 | - // Si un resultat est retourne, c'est un message d'impossibilite |
|
| 57 | - if ($res) { |
|
| 58 | - return ['texte' => $res]; |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - if (!$chemin_cache || !$lastmodified) { |
|
| 62 | - $lastmodified = time(); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - $headers_only = ($_SERVER['REQUEST_METHOD'] == 'HEAD'); |
|
| 66 | - $calculer_page = true; |
|
| 67 | - |
|
| 68 | - // Pour les pages non-dynamiques (indiquees par #CACHE{duree,cache-client}) |
|
| 69 | - // une perennite valide a meme reponse qu'une requete HEAD (par defaut les |
|
| 70 | - // pages sont dynamiques) |
|
| 71 | - if ( |
|
| 72 | - isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) |
|
| 73 | - and (!defined('_VAR_MODE') or !_VAR_MODE) |
|
| 74 | - and $chemin_cache |
|
| 75 | - and isset($page['entetes']) |
|
| 76 | - and isset($page['entetes']['Cache-Control']) |
|
| 77 | - and strstr($page['entetes']['Cache-Control'], 'max-age=') |
|
| 78 | - and !strstr($_SERVER['SERVER_SOFTWARE'], 'IIS/') |
|
| 79 | - ) { |
|
| 80 | - $since = preg_replace( |
|
| 81 | - '/;.*/', |
|
| 82 | - '', |
|
| 83 | - $_SERVER['HTTP_IF_MODIFIED_SINCE'] |
|
| 84 | - ); |
|
| 85 | - $since = str_replace('GMT', '', $since); |
|
| 86 | - if (trim($since) == gmdate('D, d M Y H:i:s', $lastmodified)) { |
|
| 87 | - $page['status'] = 304; |
|
| 88 | - $headers_only = true; |
|
| 89 | - $calculer_page = false; |
|
| 90 | - } |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - // Si requete HEAD ou Last-modified compatible, ignorer le texte |
|
| 94 | - // et pas de content-type (pour contrer le bouton admin de inc-public) |
|
| 95 | - if (!$calculer_page) { |
|
| 96 | - $page['texte'] = ''; |
|
| 97 | - } else { |
|
| 98 | - // si la page est prise dans le cache |
|
| 99 | - if (!$GLOBALS['use_cache']) { |
|
| 100 | - // Informer les boutons d'admin du contexte |
|
| 101 | - // (fourni par urls_decoder_url ci-dessous lors de la mise en cache) |
|
| 102 | - $GLOBALS['contexte'] = $page['contexte']; |
|
| 103 | - |
|
| 104 | - // vider les globales url propres qui ne doivent plus etre utilisees en cas |
|
| 105 | - // d'inversion url => objet |
|
| 106 | - // plus necessaire si on utilise bien la fonction urls_decoder_url |
|
| 107 | - #unset($_SERVER['REDIRECT_url_propre']); |
|
| 108 | - #unset($_ENV['url_propre']); |
|
| 109 | - } else { |
|
| 110 | - // Compat ascendante : |
|
| 111 | - // 1. $contexte est global |
|
| 112 | - // (a evacuer car urls_decoder_url gere ce probleme ?) |
|
| 113 | - // et calculer la page |
|
| 114 | - if (!test_espace_prive()) { |
|
| 115 | - include_spip('inc/urls'); |
|
| 116 | - [$fond, $GLOBALS['contexte'], $url_redirect] = urls_decoder_url( |
|
| 117 | - nettoyer_uri(), |
|
| 118 | - $fond, |
|
| 119 | - $GLOBALS['contexte'], |
|
| 120 | - true |
|
| 121 | - ); |
|
| 122 | - } |
|
| 123 | - // squelette par defaut |
|
| 124 | - if (!strlen($fond ?? '')) { |
|
| 125 | - $fond = 'sommaire'; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - // produire la page : peut mettre a jour $lastmodified |
|
| 129 | - $produire_page = charger_fonction('produire_page', 'public'); |
|
| 130 | - $page = $produire_page( |
|
| 131 | - $fond, |
|
| 132 | - $GLOBALS['contexte'], |
|
| 133 | - $GLOBALS['use_cache'], |
|
| 134 | - $chemin_cache, |
|
| 135 | - null, |
|
| 136 | - $page, |
|
| 137 | - $lastmodified, |
|
| 138 | - $connect |
|
| 139 | - ); |
|
| 140 | - if ($page === '') { |
|
| 141 | - $erreur = _T( |
|
| 142 | - 'info_erreur_squelette2', |
|
| 143 | - ['fichier' => spip_htmlspecialchars($fond) . '.' . _EXTENSION_SQUELETTES] |
|
| 144 | - ); |
|
| 145 | - erreur_squelette($erreur); |
|
| 146 | - // eviter des erreurs strictes ensuite sur $page['cle'] en PHP >= 5.4 |
|
| 147 | - $page = ['texte' => '', 'erreur' => $erreur]; |
|
| 148 | - } |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - if ($page and $chemin_cache) { |
|
| 152 | - $page['cache'] = $chemin_cache; |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - auto_content_type($page); |
|
| 156 | - |
|
| 157 | - $GLOBALS['flag_preserver'] |= headers_sent(); |
|
| 158 | - |
|
| 159 | - // Definir les entetes si ce n'est fait |
|
| 160 | - if (!$GLOBALS['flag_preserver']) { |
|
| 161 | - // Si la page est vide, produire l'erreur 404 ou message d'erreur pour les inclusions |
|
| 162 | - if ( |
|
| 163 | - trim($page['texte']) === '' |
|
| 164 | - and _VAR_MODE !== 'debug' |
|
| 165 | - and !isset($page['entetes']['Location']) // cette page realise une redirection, donc pas d'erreur |
|
| 166 | - ) { |
|
| 167 | - $GLOBALS['contexte']['fond_erreur'] = $fond; |
|
| 168 | - $page = message_page_indisponible($page, $GLOBALS['contexte']); |
|
| 169 | - } |
|
| 170 | - // pas de cache client en mode 'observation' |
|
| 171 | - if (defined('_VAR_MODE') and _VAR_MODE) { |
|
| 172 | - $page['entetes']['Cache-Control'] = 'no-cache,must-revalidate'; |
|
| 173 | - $page['entetes']['Pragma'] = 'no-cache'; |
|
| 174 | - } |
|
| 175 | - } |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - // Entete Last-Modified: |
|
| 179 | - // eviter d'etre incoherent en envoyant un lastmodified identique |
|
| 180 | - // a celui qu'on a refuse d'honorer plus haut (cf. #655) |
|
| 181 | - if ( |
|
| 182 | - $lastmodified |
|
| 183 | - and !isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) |
|
| 184 | - and !isset($page['entetes']['Last-Modified']) |
|
| 185 | - ) { |
|
| 186 | - $page['entetes']['Last-Modified'] = gmdate('D, d M Y H:i:s', $lastmodified) . ' GMT'; |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - // fermer la connexion apres les headers si requete HEAD |
|
| 190 | - if ($headers_only) { |
|
| 191 | - $page['entetes']['Connection'] = 'close'; |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - return $page; |
|
| 33 | + $chemin_cache = null; |
|
| 34 | + $lastmodified = null; |
|
| 35 | + $res = null; |
|
| 36 | + // flag_preserver est modifie ici, et utilise en globale |
|
| 37 | + // use_cache sert a informer le bouton d'admin pr savoir s'il met un * |
|
| 38 | + // contexte est utilise en globale dans le formulaire d'admin |
|
| 39 | + |
|
| 40 | + $GLOBALS['contexte'] = calculer_contexte(); |
|
| 41 | + $page = ['contexte_implicite' => calculer_contexte_implicite()]; |
|
| 42 | + $page['contexte_implicite']['cache'] = $fond . preg_replace( |
|
| 43 | + ',\.[a-zA-Z0-9]*$,', |
|
| 44 | + '', |
|
| 45 | + preg_replace('/[?].*$/', '', $GLOBALS['REQUEST_URI']) |
|
| 46 | + ); |
|
| 47 | + // Cette fonction est utilisee deux fois |
|
| 48 | + $cacher = charger_fonction('cacher', 'public', true); |
|
| 49 | + // Les quatre derniers parametres sont modifies par la fonction: |
|
| 50 | + // emplacement, validite, et, s'il est valide, contenu & age |
|
| 51 | + if ($cacher) { |
|
| 52 | + $res = $cacher($GLOBALS['contexte'], $GLOBALS['use_cache'], $chemin_cache, $page, $lastmodified); |
|
| 53 | + } else { |
|
| 54 | + $GLOBALS['use_cache'] = -1; |
|
| 55 | + } |
|
| 56 | + // Si un resultat est retourne, c'est un message d'impossibilite |
|
| 57 | + if ($res) { |
|
| 58 | + return ['texte' => $res]; |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + if (!$chemin_cache || !$lastmodified) { |
|
| 62 | + $lastmodified = time(); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + $headers_only = ($_SERVER['REQUEST_METHOD'] == 'HEAD'); |
|
| 66 | + $calculer_page = true; |
|
| 67 | + |
|
| 68 | + // Pour les pages non-dynamiques (indiquees par #CACHE{duree,cache-client}) |
|
| 69 | + // une perennite valide a meme reponse qu'une requete HEAD (par defaut les |
|
| 70 | + // pages sont dynamiques) |
|
| 71 | + if ( |
|
| 72 | + isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) |
|
| 73 | + and (!defined('_VAR_MODE') or !_VAR_MODE) |
|
| 74 | + and $chemin_cache |
|
| 75 | + and isset($page['entetes']) |
|
| 76 | + and isset($page['entetes']['Cache-Control']) |
|
| 77 | + and strstr($page['entetes']['Cache-Control'], 'max-age=') |
|
| 78 | + and !strstr($_SERVER['SERVER_SOFTWARE'], 'IIS/') |
|
| 79 | + ) { |
|
| 80 | + $since = preg_replace( |
|
| 81 | + '/;.*/', |
|
| 82 | + '', |
|
| 83 | + $_SERVER['HTTP_IF_MODIFIED_SINCE'] |
|
| 84 | + ); |
|
| 85 | + $since = str_replace('GMT', '', $since); |
|
| 86 | + if (trim($since) == gmdate('D, d M Y H:i:s', $lastmodified)) { |
|
| 87 | + $page['status'] = 304; |
|
| 88 | + $headers_only = true; |
|
| 89 | + $calculer_page = false; |
|
| 90 | + } |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + // Si requete HEAD ou Last-modified compatible, ignorer le texte |
|
| 94 | + // et pas de content-type (pour contrer le bouton admin de inc-public) |
|
| 95 | + if (!$calculer_page) { |
|
| 96 | + $page['texte'] = ''; |
|
| 97 | + } else { |
|
| 98 | + // si la page est prise dans le cache |
|
| 99 | + if (!$GLOBALS['use_cache']) { |
|
| 100 | + // Informer les boutons d'admin du contexte |
|
| 101 | + // (fourni par urls_decoder_url ci-dessous lors de la mise en cache) |
|
| 102 | + $GLOBALS['contexte'] = $page['contexte']; |
|
| 103 | + |
|
| 104 | + // vider les globales url propres qui ne doivent plus etre utilisees en cas |
|
| 105 | + // d'inversion url => objet |
|
| 106 | + // plus necessaire si on utilise bien la fonction urls_decoder_url |
|
| 107 | + #unset($_SERVER['REDIRECT_url_propre']); |
|
| 108 | + #unset($_ENV['url_propre']); |
|
| 109 | + } else { |
|
| 110 | + // Compat ascendante : |
|
| 111 | + // 1. $contexte est global |
|
| 112 | + // (a evacuer car urls_decoder_url gere ce probleme ?) |
|
| 113 | + // et calculer la page |
|
| 114 | + if (!test_espace_prive()) { |
|
| 115 | + include_spip('inc/urls'); |
|
| 116 | + [$fond, $GLOBALS['contexte'], $url_redirect] = urls_decoder_url( |
|
| 117 | + nettoyer_uri(), |
|
| 118 | + $fond, |
|
| 119 | + $GLOBALS['contexte'], |
|
| 120 | + true |
|
| 121 | + ); |
|
| 122 | + } |
|
| 123 | + // squelette par defaut |
|
| 124 | + if (!strlen($fond ?? '')) { |
|
| 125 | + $fond = 'sommaire'; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + // produire la page : peut mettre a jour $lastmodified |
|
| 129 | + $produire_page = charger_fonction('produire_page', 'public'); |
|
| 130 | + $page = $produire_page( |
|
| 131 | + $fond, |
|
| 132 | + $GLOBALS['contexte'], |
|
| 133 | + $GLOBALS['use_cache'], |
|
| 134 | + $chemin_cache, |
|
| 135 | + null, |
|
| 136 | + $page, |
|
| 137 | + $lastmodified, |
|
| 138 | + $connect |
|
| 139 | + ); |
|
| 140 | + if ($page === '') { |
|
| 141 | + $erreur = _T( |
|
| 142 | + 'info_erreur_squelette2', |
|
| 143 | + ['fichier' => spip_htmlspecialchars($fond) . '.' . _EXTENSION_SQUELETTES] |
|
| 144 | + ); |
|
| 145 | + erreur_squelette($erreur); |
|
| 146 | + // eviter des erreurs strictes ensuite sur $page['cle'] en PHP >= 5.4 |
|
| 147 | + $page = ['texte' => '', 'erreur' => $erreur]; |
|
| 148 | + } |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + if ($page and $chemin_cache) { |
|
| 152 | + $page['cache'] = $chemin_cache; |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + auto_content_type($page); |
|
| 156 | + |
|
| 157 | + $GLOBALS['flag_preserver'] |= headers_sent(); |
|
| 158 | + |
|
| 159 | + // Definir les entetes si ce n'est fait |
|
| 160 | + if (!$GLOBALS['flag_preserver']) { |
|
| 161 | + // Si la page est vide, produire l'erreur 404 ou message d'erreur pour les inclusions |
|
| 162 | + if ( |
|
| 163 | + trim($page['texte']) === '' |
|
| 164 | + and _VAR_MODE !== 'debug' |
|
| 165 | + and !isset($page['entetes']['Location']) // cette page realise une redirection, donc pas d'erreur |
|
| 166 | + ) { |
|
| 167 | + $GLOBALS['contexte']['fond_erreur'] = $fond; |
|
| 168 | + $page = message_page_indisponible($page, $GLOBALS['contexte']); |
|
| 169 | + } |
|
| 170 | + // pas de cache client en mode 'observation' |
|
| 171 | + if (defined('_VAR_MODE') and _VAR_MODE) { |
|
| 172 | + $page['entetes']['Cache-Control'] = 'no-cache,must-revalidate'; |
|
| 173 | + $page['entetes']['Pragma'] = 'no-cache'; |
|
| 174 | + } |
|
| 175 | + } |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + // Entete Last-Modified: |
|
| 179 | + // eviter d'etre incoherent en envoyant un lastmodified identique |
|
| 180 | + // a celui qu'on a refuse d'honorer plus haut (cf. #655) |
|
| 181 | + if ( |
|
| 182 | + $lastmodified |
|
| 183 | + and !isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) |
|
| 184 | + and !isset($page['entetes']['Last-Modified']) |
|
| 185 | + ) { |
|
| 186 | + $page['entetes']['Last-Modified'] = gmdate('D, d M Y H:i:s', $lastmodified) . ' GMT'; |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + // fermer la connexion apres les headers si requete HEAD |
|
| 190 | + if ($headers_only) { |
|
| 191 | + $page['entetes']['Connection'] = 'close'; |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + return $page; |
|
| 195 | 195 | } |
| 196 | 196 | |
| 197 | 197 | /** |
@@ -208,19 +208,19 @@ discard block |
||
| 208 | 208 | */ |
| 209 | 209 | function calculer_contexte() { |
| 210 | 210 | |
| 211 | - $contexte = []; |
|
| 212 | - foreach ($_GET as $var => $val) { |
|
| 213 | - if (!preg_match(_CONTEXTE_IGNORE_VARIABLES, $var)) { |
|
| 214 | - $contexte[$var] = $val; |
|
| 215 | - } |
|
| 216 | - } |
|
| 217 | - foreach ($_POST as $var => $val) { |
|
| 218 | - if (!preg_match(_CONTEXTE_IGNORE_VARIABLES, $var)) { |
|
| 219 | - $contexte[$var] = $val; |
|
| 220 | - } |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - return $contexte; |
|
| 211 | + $contexte = []; |
|
| 212 | + foreach ($_GET as $var => $val) { |
|
| 213 | + if (!preg_match(_CONTEXTE_IGNORE_VARIABLES, $var)) { |
|
| 214 | + $contexte[$var] = $val; |
|
| 215 | + } |
|
| 216 | + } |
|
| 217 | + foreach ($_POST as $var => $val) { |
|
| 218 | + if (!preg_match(_CONTEXTE_IGNORE_VARIABLES, $var)) { |
|
| 219 | + $contexte[$var] = $val; |
|
| 220 | + } |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + return $contexte; |
|
| 224 | 224 | } |
| 225 | 225 | |
| 226 | 226 | /** |
@@ -231,25 +231,25 @@ discard block |
||
| 231 | 231 | * @return array |
| 232 | 232 | */ |
| 233 | 233 | function calculer_contexte_implicite() { |
| 234 | - static $notes = null; |
|
| 235 | - if (is_null($notes)) { |
|
| 236 | - $notes = charger_fonction('notes', 'inc', true); |
|
| 237 | - } |
|
| 238 | - $contexte_implicite = [ |
|
| 239 | - 'squelettes' => $GLOBALS['dossier_squelettes'], // devrait etre 'chemin' => $GLOBALS['path_sig'], ? |
|
| 240 | - 'host' => ($_SERVER['HTTP_HOST'] ?? null), |
|
| 241 | - 'https' => ($_SERVER['HTTPS'] ?? ''), |
|
| 242 | - 'espace' => test_espace_prive(), |
|
| 243 | - 'marqueur' => ($GLOBALS['marqueur'] ?? ''), |
|
| 244 | - 'marqueur_skel' => ($GLOBALS['marqueur_skel'] ?? ''), |
|
| 245 | - 'notes' => $notes ? $notes('', 'contexter_cache') : '', |
|
| 246 | - 'spip_version_code' => $GLOBALS['spip_version_code'], |
|
| 247 | - ]; |
|
| 248 | - if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) { |
|
| 249 | - $contexte_implicite['host'] .= '|' . $_SERVER['HTTP_X_FORWARDED_HOST']; |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - return $contexte_implicite; |
|
| 234 | + static $notes = null; |
|
| 235 | + if (is_null($notes)) { |
|
| 236 | + $notes = charger_fonction('notes', 'inc', true); |
|
| 237 | + } |
|
| 238 | + $contexte_implicite = [ |
|
| 239 | + 'squelettes' => $GLOBALS['dossier_squelettes'], // devrait etre 'chemin' => $GLOBALS['path_sig'], ? |
|
| 240 | + 'host' => ($_SERVER['HTTP_HOST'] ?? null), |
|
| 241 | + 'https' => ($_SERVER['HTTPS'] ?? ''), |
|
| 242 | + 'espace' => test_espace_prive(), |
|
| 243 | + 'marqueur' => ($GLOBALS['marqueur'] ?? ''), |
|
| 244 | + 'marqueur_skel' => ($GLOBALS['marqueur_skel'] ?? ''), |
|
| 245 | + 'notes' => $notes ? $notes('', 'contexter_cache') : '', |
|
| 246 | + 'spip_version_code' => $GLOBALS['spip_version_code'], |
|
| 247 | + ]; |
|
| 248 | + if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) { |
|
| 249 | + $contexte_implicite['host'] .= '|' . $_SERVER['HTTP_X_FORWARDED_HOST']; |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + return $contexte_implicite; |
|
| 253 | 253 | } |
| 254 | 254 | |
| 255 | 255 | // |
@@ -258,55 +258,55 @@ discard block |
||
| 258 | 258 | |
| 259 | 259 | function auto_content_type($page) { |
| 260 | 260 | |
| 261 | - if (!isset($GLOBALS['flag_preserver'])) { |
|
| 262 | - $GLOBALS['flag_preserver'] = ($page && preg_match( |
|
| 263 | - '/header\s*\(\s*.content\-type:/isx', |
|
| 264 | - $page['texte'] |
|
| 265 | - ) || (isset($page['entetes']['Content-Type']))); |
|
| 266 | - } |
|
| 261 | + if (!isset($GLOBALS['flag_preserver'])) { |
|
| 262 | + $GLOBALS['flag_preserver'] = ($page && preg_match( |
|
| 263 | + '/header\s*\(\s*.content\-type:/isx', |
|
| 264 | + $page['texte'] |
|
| 265 | + ) || (isset($page['entetes']['Content-Type']))); |
|
| 266 | + } |
|
| 267 | 267 | } |
| 268 | 268 | |
| 269 | 269 | function inclure_page($fond, $contexte, string $connect = '') { |
| 270 | - $use_cache = null; |
|
| 271 | - $chemin_cache = null; |
|
| 272 | - $lastinclude = null; |
|
| 273 | - $res = null; |
|
| 274 | - static $cacher, $produire_page; |
|
| 275 | - |
|
| 276 | - // enlever le fond de contexte inclus car sinon il prend la main |
|
| 277 | - // dans les sous inclusions -> boucle infinie d'inclusion identique |
|
| 278 | - // (cette precaution n'est probablement plus utile) |
|
| 279 | - unset($contexte['fond']); |
|
| 280 | - $page = ['contexte_implicite' => calculer_contexte_implicite()]; |
|
| 281 | - $page['contexte_implicite']['cache'] = $fond; |
|
| 282 | - if (is_null($cacher)) { |
|
| 283 | - $cacher = charger_fonction('cacher', 'public', true); |
|
| 284 | - } |
|
| 285 | - // Les quatre derniers parametres sont modifies par la fonction: |
|
| 286 | - // emplacement, validite, et, s'il est valide, contenu & age |
|
| 287 | - if ($cacher) { |
|
| 288 | - $res = $cacher($contexte, $use_cache, $chemin_cache, $page, $lastinclude); |
|
| 289 | - } else { |
|
| 290 | - $use_cache = -1; |
|
| 291 | - } |
|
| 292 | - // $res = message d'erreur : on sort de la |
|
| 293 | - if ($res) { |
|
| 294 | - return ['texte' => $res]; |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - // Si use_cache ne vaut pas 0, la page doit etre calculee |
|
| 298 | - // produire la page : peut mettre a jour $lastinclude |
|
| 299 | - // le contexte_cache envoye a cacher() a ete conserve et est passe a produire |
|
| 300 | - if ($use_cache) { |
|
| 301 | - if (is_null($produire_page)) { |
|
| 302 | - $produire_page = charger_fonction('produire_page', 'public'); |
|
| 303 | - } |
|
| 304 | - $page = $produire_page($fond, $contexte, $use_cache, $chemin_cache, $contexte, $page, $lastinclude, $connect); |
|
| 305 | - } |
|
| 306 | - // dans tous les cas, mettre a jour $GLOBALS['lastmodified'] |
|
| 307 | - $GLOBALS['lastmodified'] = max(($GLOBALS['lastmodified'] ?? 0), $lastinclude); |
|
| 308 | - |
|
| 309 | - return $page; |
|
| 270 | + $use_cache = null; |
|
| 271 | + $chemin_cache = null; |
|
| 272 | + $lastinclude = null; |
|
| 273 | + $res = null; |
|
| 274 | + static $cacher, $produire_page; |
|
| 275 | + |
|
| 276 | + // enlever le fond de contexte inclus car sinon il prend la main |
|
| 277 | + // dans les sous inclusions -> boucle infinie d'inclusion identique |
|
| 278 | + // (cette precaution n'est probablement plus utile) |
|
| 279 | + unset($contexte['fond']); |
|
| 280 | + $page = ['contexte_implicite' => calculer_contexte_implicite()]; |
|
| 281 | + $page['contexte_implicite']['cache'] = $fond; |
|
| 282 | + if (is_null($cacher)) { |
|
| 283 | + $cacher = charger_fonction('cacher', 'public', true); |
|
| 284 | + } |
|
| 285 | + // Les quatre derniers parametres sont modifies par la fonction: |
|
| 286 | + // emplacement, validite, et, s'il est valide, contenu & age |
|
| 287 | + if ($cacher) { |
|
| 288 | + $res = $cacher($contexte, $use_cache, $chemin_cache, $page, $lastinclude); |
|
| 289 | + } else { |
|
| 290 | + $use_cache = -1; |
|
| 291 | + } |
|
| 292 | + // $res = message d'erreur : on sort de la |
|
| 293 | + if ($res) { |
|
| 294 | + return ['texte' => $res]; |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + // Si use_cache ne vaut pas 0, la page doit etre calculee |
|
| 298 | + // produire la page : peut mettre a jour $lastinclude |
|
| 299 | + // le contexte_cache envoye a cacher() a ete conserve et est passe a produire |
|
| 300 | + if ($use_cache) { |
|
| 301 | + if (is_null($produire_page)) { |
|
| 302 | + $produire_page = charger_fonction('produire_page', 'public'); |
|
| 303 | + } |
|
| 304 | + $page = $produire_page($fond, $contexte, $use_cache, $chemin_cache, $contexte, $page, $lastinclude, $connect); |
|
| 305 | + } |
|
| 306 | + // dans tous les cas, mettre a jour $GLOBALS['lastmodified'] |
|
| 307 | + $GLOBALS['lastmodified'] = max(($GLOBALS['lastmodified'] ?? 0), $lastinclude); |
|
| 308 | + |
|
| 309 | + return $page; |
|
| 310 | 310 | } |
| 311 | 311 | |
| 312 | 312 | /** |
@@ -324,41 +324,41 @@ discard block |
||
| 324 | 324 | * @return array |
| 325 | 325 | */ |
| 326 | 326 | function public_produire_page_dist( |
| 327 | - $fond, |
|
| 328 | - $contexte, |
|
| 329 | - $use_cache, |
|
| 330 | - $chemin_cache, |
|
| 331 | - $contexte_cache, |
|
| 332 | - &$page, |
|
| 333 | - &$lastinclude, |
|
| 334 | - $connect = '' |
|
| 327 | + $fond, |
|
| 328 | + $contexte, |
|
| 329 | + $use_cache, |
|
| 330 | + $chemin_cache, |
|
| 331 | + $contexte_cache, |
|
| 332 | + &$page, |
|
| 333 | + &$lastinclude, |
|
| 334 | + $connect = '' |
|
| 335 | 335 | ) { |
| 336 | - static $parametrer, $cacher; |
|
| 337 | - if (!$parametrer) { |
|
| 338 | - $parametrer = charger_fonction('parametrer', 'public'); |
|
| 339 | - } |
|
| 340 | - $page = $parametrer($fond, $contexte, $chemin_cache, $connect); |
|
| 341 | - // et on l'enregistre sur le disque |
|
| 342 | - if ( |
|
| 343 | - $chemin_cache |
|
| 344 | - and $use_cache > -1 |
|
| 345 | - and is_array($page) |
|
| 346 | - and count($page) |
|
| 347 | - and isset($page['entetes']['X-Spip-Cache']) |
|
| 348 | - and $page['entetes']['X-Spip-Cache'] > 0 |
|
| 349 | - ) { |
|
| 350 | - if (is_null($cacher)) { |
|
| 351 | - $cacher = charger_fonction('cacher', 'public', true); |
|
| 352 | - } |
|
| 353 | - $lastinclude = time(); |
|
| 354 | - if ($cacher) { |
|
| 355 | - $cacher($contexte_cache, $use_cache, $chemin_cache, $page, $lastinclude); |
|
| 356 | - } else { |
|
| 357 | - $use_cache = -1; |
|
| 358 | - } |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - return $page; |
|
| 336 | + static $parametrer, $cacher; |
|
| 337 | + if (!$parametrer) { |
|
| 338 | + $parametrer = charger_fonction('parametrer', 'public'); |
|
| 339 | + } |
|
| 340 | + $page = $parametrer($fond, $contexte, $chemin_cache, $connect); |
|
| 341 | + // et on l'enregistre sur le disque |
|
| 342 | + if ( |
|
| 343 | + $chemin_cache |
|
| 344 | + and $use_cache > -1 |
|
| 345 | + and is_array($page) |
|
| 346 | + and count($page) |
|
| 347 | + and isset($page['entetes']['X-Spip-Cache']) |
|
| 348 | + and $page['entetes']['X-Spip-Cache'] > 0 |
|
| 349 | + ) { |
|
| 350 | + if (is_null($cacher)) { |
|
| 351 | + $cacher = charger_fonction('cacher', 'public', true); |
|
| 352 | + } |
|
| 353 | + $lastinclude = time(); |
|
| 354 | + if ($cacher) { |
|
| 355 | + $cacher($contexte_cache, $use_cache, $chemin_cache, $page, $lastinclude); |
|
| 356 | + } else { |
|
| 357 | + $use_cache = -1; |
|
| 358 | + } |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + return $page; |
|
| 362 | 362 | } |
| 363 | 363 | |
| 364 | 364 | // Fonction inseree par le compilateur dans le code compile. |
@@ -372,14 +372,14 @@ discard block |
||
| 372 | 372 | // 4: langue |
| 373 | 373 | |
| 374 | 374 | function inserer_balise_dynamique($contexte_exec, $contexte_compil) { |
| 375 | - arguments_balise_dyn_depuis_modele(null, 'reset'); |
|
| 376 | - |
|
| 377 | - if (!is_array($contexte_exec)) { |
|
| 378 | - echo $contexte_exec; |
|
| 379 | - } // message d'erreur etc |
|
| 380 | - else { |
|
| 381 | - inclure_balise_dynamique($contexte_exec, true, $contexte_compil); |
|
| 382 | - } |
|
| 375 | + arguments_balise_dyn_depuis_modele(null, 'reset'); |
|
| 376 | + |
|
| 377 | + if (!is_array($contexte_exec)) { |
|
| 378 | + echo $contexte_exec; |
|
| 379 | + } // message d'erreur etc |
|
| 380 | + else { |
|
| 381 | + inclure_balise_dynamique($contexte_exec, true, $contexte_compil); |
|
| 382 | + } |
|
| 383 | 383 | } |
| 384 | 384 | |
| 385 | 385 | /** |
@@ -392,101 +392,101 @@ discard block |
||
| 392 | 392 | * @return string|void |
| 393 | 393 | */ |
| 394 | 394 | function inclure_balise_dynamique($texte, $echo = true, $contexte_compil = []) { |
| 395 | - if (is_array($texte)) { |
|
| 396 | - [$fond, $delainc, $contexte_inclus] = $texte; |
|
| 397 | - |
|
| 398 | - // delais a l'ancienne, c'est pratiquement mort |
|
| 399 | - $d = $GLOBALS['delais'] ?? null; |
|
| 400 | - $GLOBALS['delais'] = $delainc; |
|
| 401 | - |
|
| 402 | - $page = recuperer_fond( |
|
| 403 | - $fond, |
|
| 404 | - $contexte_inclus, |
|
| 405 | - ['trim' => false, 'raw' => true, 'compil' => $contexte_compil] |
|
| 406 | - ); |
|
| 407 | - |
|
| 408 | - $texte = $page['texte']; |
|
| 409 | - |
|
| 410 | - $GLOBALS['delais'] = $d; |
|
| 411 | - // Faire remonter les entetes |
|
| 412 | - if ( |
|
| 413 | - isset($page['entetes']) |
|
| 414 | - and is_array($page['entetes']) |
|
| 415 | - ) { |
|
| 416 | - // mais pas toutes |
|
| 417 | - unset($page['entetes']['X-Spip-Cache']); |
|
| 418 | - unset($page['entetes']['Content-Type']); |
|
| 419 | - if (isset($GLOBALS['page']) and is_array($GLOBALS['page'])) { |
|
| 420 | - if (!is_array($GLOBALS['page']['entetes'])) { |
|
| 421 | - $GLOBALS['page']['entetes'] = []; |
|
| 422 | - } |
|
| 423 | - $GLOBALS['page']['entetes'] = |
|
| 424 | - array_merge($GLOBALS['page']['entetes'], $page['entetes']); |
|
| 425 | - } |
|
| 426 | - } |
|
| 427 | - // _pipelines au pluriel array('nom_pipeline' => $args...) avec une syntaxe permettant plusieurs pipelines |
|
| 428 | - if ( |
|
| 429 | - isset($page['contexte']['_pipelines']) |
|
| 430 | - and is_array($page['contexte']['_pipelines']) |
|
| 431 | - and count($page['contexte']['_pipelines']) |
|
| 432 | - ) { |
|
| 433 | - foreach ($page['contexte']['_pipelines'] as $pipe => $args) { |
|
| 434 | - $args['contexte'] = $page['contexte']; |
|
| 435 | - unset($args['contexte']['_pipelines']); // par precaution, meme si le risque de boucle infinie est a priori nul |
|
| 436 | - $texte = pipeline( |
|
| 437 | - $pipe, |
|
| 438 | - [ |
|
| 439 | - 'data' => $texte, |
|
| 440 | - 'args' => $args |
|
| 441 | - ] |
|
| 442 | - ); |
|
| 443 | - } |
|
| 444 | - } |
|
| 445 | - } |
|
| 446 | - |
|
| 447 | - if (defined('_VAR_MODE') and _VAR_MODE == 'debug') { |
|
| 448 | - // compatibilite : avant on donnait le numero de ligne ou rien. |
|
| 449 | - $ligne = intval($contexte_compil[3] ?? $contexte_compil); |
|
| 450 | - $GLOBALS['debug_objets']['resultat'][$ligne] = $texte; |
|
| 451 | - } |
|
| 452 | - if ($echo) { |
|
| 453 | - echo $texte; |
|
| 454 | - } else { |
|
| 455 | - return $texte; |
|
| 456 | - } |
|
| 395 | + if (is_array($texte)) { |
|
| 396 | + [$fond, $delainc, $contexte_inclus] = $texte; |
|
| 397 | + |
|
| 398 | + // delais a l'ancienne, c'est pratiquement mort |
|
| 399 | + $d = $GLOBALS['delais'] ?? null; |
|
| 400 | + $GLOBALS['delais'] = $delainc; |
|
| 401 | + |
|
| 402 | + $page = recuperer_fond( |
|
| 403 | + $fond, |
|
| 404 | + $contexte_inclus, |
|
| 405 | + ['trim' => false, 'raw' => true, 'compil' => $contexte_compil] |
|
| 406 | + ); |
|
| 407 | + |
|
| 408 | + $texte = $page['texte']; |
|
| 409 | + |
|
| 410 | + $GLOBALS['delais'] = $d; |
|
| 411 | + // Faire remonter les entetes |
|
| 412 | + if ( |
|
| 413 | + isset($page['entetes']) |
|
| 414 | + and is_array($page['entetes']) |
|
| 415 | + ) { |
|
| 416 | + // mais pas toutes |
|
| 417 | + unset($page['entetes']['X-Spip-Cache']); |
|
| 418 | + unset($page['entetes']['Content-Type']); |
|
| 419 | + if (isset($GLOBALS['page']) and is_array($GLOBALS['page'])) { |
|
| 420 | + if (!is_array($GLOBALS['page']['entetes'])) { |
|
| 421 | + $GLOBALS['page']['entetes'] = []; |
|
| 422 | + } |
|
| 423 | + $GLOBALS['page']['entetes'] = |
|
| 424 | + array_merge($GLOBALS['page']['entetes'], $page['entetes']); |
|
| 425 | + } |
|
| 426 | + } |
|
| 427 | + // _pipelines au pluriel array('nom_pipeline' => $args...) avec une syntaxe permettant plusieurs pipelines |
|
| 428 | + if ( |
|
| 429 | + isset($page['contexte']['_pipelines']) |
|
| 430 | + and is_array($page['contexte']['_pipelines']) |
|
| 431 | + and count($page['contexte']['_pipelines']) |
|
| 432 | + ) { |
|
| 433 | + foreach ($page['contexte']['_pipelines'] as $pipe => $args) { |
|
| 434 | + $args['contexte'] = $page['contexte']; |
|
| 435 | + unset($args['contexte']['_pipelines']); // par precaution, meme si le risque de boucle infinie est a priori nul |
|
| 436 | + $texte = pipeline( |
|
| 437 | + $pipe, |
|
| 438 | + [ |
|
| 439 | + 'data' => $texte, |
|
| 440 | + 'args' => $args |
|
| 441 | + ] |
|
| 442 | + ); |
|
| 443 | + } |
|
| 444 | + } |
|
| 445 | + } |
|
| 446 | + |
|
| 447 | + if (defined('_VAR_MODE') and _VAR_MODE == 'debug') { |
|
| 448 | + // compatibilite : avant on donnait le numero de ligne ou rien. |
|
| 449 | + $ligne = intval($contexte_compil[3] ?? $contexte_compil); |
|
| 450 | + $GLOBALS['debug_objets']['resultat'][$ligne] = $texte; |
|
| 451 | + } |
|
| 452 | + if ($echo) { |
|
| 453 | + echo $texte; |
|
| 454 | + } else { |
|
| 455 | + return $texte; |
|
| 456 | + } |
|
| 457 | 457 | } |
| 458 | 458 | |
| 459 | 459 | function message_page_indisponible($page, $contexte) { |
| 460 | - static $deja = false; |
|
| 461 | - if ($deja) { |
|
| 462 | - return 'erreur'; |
|
| 463 | - } |
|
| 464 | - $codes = [ |
|
| 465 | - '404' => '404 Not Found', |
|
| 466 | - '503' => '503 Service Unavailable', |
|
| 467 | - ]; |
|
| 468 | - |
|
| 469 | - $contexte['status'] = ($page !== false) ? '404' : '503'; |
|
| 470 | - $contexte['code'] = $codes[$contexte['status']]; |
|
| 471 | - $contexte['fond'] = '404'; // gere les 2 erreurs |
|
| 472 | - if (!isset($contexte['lang'])) { |
|
| 473 | - include_spip('inc/lang'); |
|
| 474 | - $contexte['lang'] = $GLOBALS['spip_lang']; |
|
| 475 | - } |
|
| 476 | - |
|
| 477 | - $deja = true; |
|
| 478 | - // passer aux plugins qui peuvent decider d'une page d'erreur plus pertinent |
|
| 479 | - // ex restriction d'acces => 401 |
|
| 480 | - $contexte = pipeline('page_indisponible', $contexte); |
|
| 481 | - |
|
| 482 | - // produire la page d'erreur |
|
| 483 | - $page = inclure_page($contexte['fond'], $contexte); |
|
| 484 | - if (!$page) { |
|
| 485 | - $page = inclure_page('404', $contexte); |
|
| 486 | - } |
|
| 487 | - $page['status'] = $contexte['status']; |
|
| 488 | - |
|
| 489 | - return $page; |
|
| 460 | + static $deja = false; |
|
| 461 | + if ($deja) { |
|
| 462 | + return 'erreur'; |
|
| 463 | + } |
|
| 464 | + $codes = [ |
|
| 465 | + '404' => '404 Not Found', |
|
| 466 | + '503' => '503 Service Unavailable', |
|
| 467 | + ]; |
|
| 468 | + |
|
| 469 | + $contexte['status'] = ($page !== false) ? '404' : '503'; |
|
| 470 | + $contexte['code'] = $codes[$contexte['status']]; |
|
| 471 | + $contexte['fond'] = '404'; // gere les 2 erreurs |
|
| 472 | + if (!isset($contexte['lang'])) { |
|
| 473 | + include_spip('inc/lang'); |
|
| 474 | + $contexte['lang'] = $GLOBALS['spip_lang']; |
|
| 475 | + } |
|
| 476 | + |
|
| 477 | + $deja = true; |
|
| 478 | + // passer aux plugins qui peuvent decider d'une page d'erreur plus pertinent |
|
| 479 | + // ex restriction d'acces => 401 |
|
| 480 | + $contexte = pipeline('page_indisponible', $contexte); |
|
| 481 | + |
|
| 482 | + // produire la page d'erreur |
|
| 483 | + $page = inclure_page($contexte['fond'], $contexte); |
|
| 484 | + if (!$page) { |
|
| 485 | + $page = inclure_page('404', $contexte); |
|
| 486 | + } |
|
| 487 | + $page['status'] = $contexte['status']; |
|
| 488 | + |
|
| 489 | + return $page; |
|
| 490 | 490 | } |
| 491 | 491 | |
| 492 | 492 | /** |
@@ -498,44 +498,44 @@ discard block |
||
| 498 | 498 | * @return mixed |
| 499 | 499 | */ |
| 500 | 500 | function arguments_balise_dyn_depuis_modele($arg, $operation = 'set') { |
| 501 | - static $balise_dyn_appellee_par_modele = null; |
|
| 502 | - switch ($operation) { |
|
| 503 | - case 'read': |
|
| 504 | - return $balise_dyn_appellee_par_modele; |
|
| 505 | - case 'reset': |
|
| 506 | - $balise_dyn_appellee_par_modele = null; |
|
| 507 | - return null; |
|
| 508 | - case 'set': |
|
| 509 | - default: |
|
| 510 | - $balise_dyn_appellee_par_modele = $arg; |
|
| 511 | - return $arg; |
|
| 512 | - } |
|
| 501 | + static $balise_dyn_appellee_par_modele = null; |
|
| 502 | + switch ($operation) { |
|
| 503 | + case 'read': |
|
| 504 | + return $balise_dyn_appellee_par_modele; |
|
| 505 | + case 'reset': |
|
| 506 | + $balise_dyn_appellee_par_modele = null; |
|
| 507 | + return null; |
|
| 508 | + case 'set': |
|
| 509 | + default: |
|
| 510 | + $balise_dyn_appellee_par_modele = $arg; |
|
| 511 | + return $arg; |
|
| 512 | + } |
|
| 513 | 513 | } |
| 514 | 514 | |
| 515 | 515 | // temporairement ici : a mettre dans le futur inc/modeles |
| 516 | 516 | // creer_contexte_de_modele('left', 'autostart=true', ...) renvoie un array() |
| 517 | 517 | function creer_contexte_de_modele($args) { |
| 518 | - $contexte = []; |
|
| 519 | - foreach ($args as $var => $val) { |
|
| 520 | - if (is_int($var)) { // argument pas formate |
|
| 521 | - if (in_array($val, ['left', 'right', 'center'])) { |
|
| 522 | - $var = 'align'; |
|
| 523 | - $contexte[$var] = $val; |
|
| 524 | - } else { |
|
| 525 | - $args = explode('=', $val); |
|
| 526 | - if (count($args) >= 2) { // Flashvars=arg1=machin&arg2=truc genere plus de deux args |
|
| 527 | - $contexte[trim($args[0])] = substr($val, strlen($args[0]) + 1); |
|
| 528 | - } else // notation abregee |
|
| 529 | - { |
|
| 530 | - $contexte[trim($val)] = trim($val); |
|
| 531 | - } |
|
| 532 | - } |
|
| 533 | - } else { |
|
| 534 | - $contexte[$var] = $val; |
|
| 535 | - } |
|
| 536 | - } |
|
| 537 | - |
|
| 538 | - return $contexte; |
|
| 518 | + $contexte = []; |
|
| 519 | + foreach ($args as $var => $val) { |
|
| 520 | + if (is_int($var)) { // argument pas formate |
|
| 521 | + if (in_array($val, ['left', 'right', 'center'])) { |
|
| 522 | + $var = 'align'; |
|
| 523 | + $contexte[$var] = $val; |
|
| 524 | + } else { |
|
| 525 | + $args = explode('=', $val); |
|
| 526 | + if (count($args) >= 2) { // Flashvars=arg1=machin&arg2=truc genere plus de deux args |
|
| 527 | + $contexte[trim($args[0])] = substr($val, strlen($args[0]) + 1); |
|
| 528 | + } else // notation abregee |
|
| 529 | + { |
|
| 530 | + $contexte[trim($val)] = trim($val); |
|
| 531 | + } |
|
| 532 | + } |
|
| 533 | + } else { |
|
| 534 | + $contexte[$var] = $val; |
|
| 535 | + } |
|
| 536 | + } |
|
| 537 | + |
|
| 538 | + return $contexte; |
|
| 539 | 539 | } |
| 540 | 540 | |
| 541 | 541 | /** |
@@ -550,43 +550,43 @@ discard block |
||
| 550 | 550 | * @return string |
| 551 | 551 | */ |
| 552 | 552 | function styliser_modele($modele, $id, $contexte = null) { |
| 553 | - static $styliseurs = null; |
|
| 554 | - if (is_null($styliseurs)) { |
|
| 555 | - $tables_objet = lister_tables_objets_sql(); |
|
| 556 | - foreach ($tables_objet as $table => $desc) { |
|
| 557 | - if ( |
|
| 558 | - isset($desc['modeles']) and $desc['modeles'] |
|
| 559 | - and isset($desc['modeles_styliser']) and $desc['modeles_styliser'] |
|
| 560 | - and function_exists($desc['modeles_styliser']) |
|
| 561 | - ) { |
|
| 562 | - $primary = id_table_objet($table); |
|
| 563 | - foreach ($desc['modeles'] as $m) { |
|
| 564 | - $styliseurs[$m] = ['primary' => $primary, 'callback' => $desc['modeles_styliser']]; |
|
| 565 | - } |
|
| 566 | - } |
|
| 567 | - } |
|
| 568 | - } |
|
| 569 | - |
|
| 570 | - if (isset($styliseurs[$modele])) { |
|
| 571 | - $styliseur = $styliseurs[$modele]['callback']; |
|
| 572 | - $primary = $styliseurs[$modele]['primary']; |
|
| 573 | - if (is_null($id) and $contexte) { |
|
| 574 | - if (isset($contexte['id'])) { |
|
| 575 | - $id = $contexte['id']; |
|
| 576 | - } elseif (isset($contexte[$primary])) { |
|
| 577 | - $id = $contexte[$primary]; |
|
| 578 | - } |
|
| 579 | - } |
|
| 580 | - if (is_null($id)) { |
|
| 581 | - $msg = "modeles/$modele : " . _T('zbug_parametres_inclus_incorrects', ['param' => "id/$primary"]); |
|
| 582 | - erreur_squelette($msg); |
|
| 583 | - // on passe id=0 au routeur pour tomber sur le modele par defaut et eviter une seconde erreur sur un modele inexistant |
|
| 584 | - $id = 0; |
|
| 585 | - } |
|
| 586 | - $modele = $styliseur($modele, $id); |
|
| 587 | - } |
|
| 588 | - |
|
| 589 | - return $modele; |
|
| 553 | + static $styliseurs = null; |
|
| 554 | + if (is_null($styliseurs)) { |
|
| 555 | + $tables_objet = lister_tables_objets_sql(); |
|
| 556 | + foreach ($tables_objet as $table => $desc) { |
|
| 557 | + if ( |
|
| 558 | + isset($desc['modeles']) and $desc['modeles'] |
|
| 559 | + and isset($desc['modeles_styliser']) and $desc['modeles_styliser'] |
|
| 560 | + and function_exists($desc['modeles_styliser']) |
|
| 561 | + ) { |
|
| 562 | + $primary = id_table_objet($table); |
|
| 563 | + foreach ($desc['modeles'] as $m) { |
|
| 564 | + $styliseurs[$m] = ['primary' => $primary, 'callback' => $desc['modeles_styliser']]; |
|
| 565 | + } |
|
| 566 | + } |
|
| 567 | + } |
|
| 568 | + } |
|
| 569 | + |
|
| 570 | + if (isset($styliseurs[$modele])) { |
|
| 571 | + $styliseur = $styliseurs[$modele]['callback']; |
|
| 572 | + $primary = $styliseurs[$modele]['primary']; |
|
| 573 | + if (is_null($id) and $contexte) { |
|
| 574 | + if (isset($contexte['id'])) { |
|
| 575 | + $id = $contexte['id']; |
|
| 576 | + } elseif (isset($contexte[$primary])) { |
|
| 577 | + $id = $contexte[$primary]; |
|
| 578 | + } |
|
| 579 | + } |
|
| 580 | + if (is_null($id)) { |
|
| 581 | + $msg = "modeles/$modele : " . _T('zbug_parametres_inclus_incorrects', ['param' => "id/$primary"]); |
|
| 582 | + erreur_squelette($msg); |
|
| 583 | + // on passe id=0 au routeur pour tomber sur le modele par defaut et eviter une seconde erreur sur un modele inexistant |
|
| 584 | + $id = 0; |
|
| 585 | + } |
|
| 586 | + $modele = $styliseur($modele, $id); |
|
| 587 | + } |
|
| 588 | + |
|
| 589 | + return $modele; |
|
| 590 | 590 | } |
| 591 | 591 | |
| 592 | 592 | /** |
@@ -603,102 +603,102 @@ discard block |
||
| 603 | 603 | */ |
| 604 | 604 | function inclure_modele($type, $id, $params, $lien, string $connect = '', $env = []) { |
| 605 | 605 | |
| 606 | - static $compteur; |
|
| 607 | - if (++$compteur > 10) { |
|
| 608 | - return ''; |
|
| 609 | - } # ne pas boucler indefiniment |
|
| 610 | - |
|
| 611 | - $type = strtolower($type); |
|
| 612 | - $type = styliser_modele($type, $id); |
|
| 613 | - |
|
| 614 | - $fond = $class = ''; |
|
| 615 | - |
|
| 616 | - $params = array_filter(explode('|', $params)); |
|
| 617 | - if ($params) { |
|
| 618 | - $soustype = current($params); |
|
| 619 | - $soustype = strtolower(trim($soustype)); |
|
| 620 | - if (in_array($soustype, ['left', 'right', 'center', 'ajax'])) { |
|
| 621 | - $soustype = next($params); |
|
| 622 | - $soustype = strtolower($soustype); |
|
| 623 | - } |
|
| 624 | - |
|
| 625 | - if (preg_match(',^[a-z0-9_]+$,', $soustype)) { |
|
| 626 | - if (!trouve_modele($fond = ($type . '_' . $soustype))) { |
|
| 627 | - $fond = ''; |
|
| 628 | - $class = $soustype; |
|
| 629 | - } |
|
| 630 | - // enlever le sous type des params |
|
| 631 | - $params = array_diff($params, [$soustype]); |
|
| 632 | - } |
|
| 633 | - } |
|
| 634 | - |
|
| 635 | - // Si ca marche pas en precisant le sous-type, prendre le type |
|
| 636 | - if (!$fond and !trouve_modele($fond = $type)) { |
|
| 637 | - spip_log("Modele $type introuvable", _LOG_INFO_IMPORTANTE); |
|
| 638 | - |
|
| 639 | - return false; |
|
| 640 | - } |
|
| 641 | - $fond = 'modeles/' . $fond; |
|
| 642 | - // Creer le contexte |
|
| 643 | - $contexte = $env; |
|
| 644 | - $contexte['dir_racine'] = _DIR_RACINE; # eviter de mixer un cache racine et un cache ecrire (meme si pour l'instant les modeles ne sont pas caches, le resultat etant different il faut que le contexte en tienne compte |
|
| 645 | - |
|
| 646 | - // Le numero du modele est mis dans l'environnement |
|
| 647 | - // d'une part sous l'identifiant "id" |
|
| 648 | - // et d'autre part sous l'identifiant de la cle primaire |
|
| 649 | - // par la fonction id_table_objet, |
|
| 650 | - // (<article1> =>> article =>> id_article =>> id_article=1) |
|
| 651 | - $_id = id_table_objet($type); |
|
| 652 | - $contexte['id'] = $contexte[$_id] = $id; |
|
| 653 | - |
|
| 654 | - if (isset($class)) { |
|
| 655 | - $contexte['class'] = $class; |
|
| 656 | - } |
|
| 657 | - |
|
| 658 | - // Si un lien a ete passe en parametre, ex: [<modele1>->url] ou [<modele1|title_du_lien{hreflang}->url] |
|
| 659 | - if ($lien) { |
|
| 660 | - # un eventuel guillemet (") sera reechappe par #ENV |
|
| 661 | - $contexte['lien'] = str_replace('"', '"', $lien['href']); |
|
| 662 | - $contexte['lien_class'] = $lien['class']; |
|
| 663 | - $contexte['lien_mime'] = $lien['mime']; |
|
| 664 | - $contexte['lien_title'] = $lien['title']; |
|
| 665 | - $contexte['lien_hreflang'] = $lien['hreflang']; |
|
| 666 | - } |
|
| 667 | - |
|
| 668 | - // Traiter les parametres |
|
| 669 | - // par exemple : <img1|center>, <emb12|autostart=true> ou <doc1|lang=en> |
|
| 670 | - $arg_list = creer_contexte_de_modele($params); |
|
| 671 | - $contexte['args'] = $arg_list; // on passe la liste des arguments du modeles dans une variable args |
|
| 672 | - $contexte = array_merge($contexte, $arg_list); |
|
| 673 | - |
|
| 674 | - // Appliquer le modele avec le contexte |
|
| 675 | - $retour = recuperer_fond($fond, $contexte, [], $connect); |
|
| 676 | - |
|
| 677 | - // Regarder si le modele tient compte des liens (il *doit* alors indiquer |
|
| 678 | - // spip_lien_ok dans les classes de son conteneur de premier niveau ; |
|
| 679 | - // sinon, s'il y a un lien, on l'ajoute classiquement |
|
| 680 | - if ( |
|
| 681 | - strstr( |
|
| 682 | - ' ' . ($classes = extraire_attribut($retour, 'class')) . ' ', |
|
| 683 | - 'spip_lien_ok' |
|
| 684 | - ) |
|
| 685 | - ) { |
|
| 686 | - $retour = inserer_attribut( |
|
| 687 | - $retour, |
|
| 688 | - 'class', |
|
| 689 | - trim(str_replace(' spip_lien_ok ', ' ', " $classes ")) |
|
| 690 | - ); |
|
| 691 | - } else { |
|
| 692 | - if ($lien) { |
|
| 693 | - $retour = "<a href=\"" . $lien['href'] . "\" class=\"" . $lien['class'] . "\">" . $retour . '</a>'; |
|
| 694 | - } |
|
| 695 | - } |
|
| 696 | - |
|
| 697 | - $compteur--; |
|
| 698 | - |
|
| 699 | - return (isset($arg_list['ajax']) and $arg_list['ajax'] == 'ajax') |
|
| 700 | - ? encoder_contexte_ajax($contexte, '', $retour) |
|
| 701 | - : $retour; |
|
| 606 | + static $compteur; |
|
| 607 | + if (++$compteur > 10) { |
|
| 608 | + return ''; |
|
| 609 | + } # ne pas boucler indefiniment |
|
| 610 | + |
|
| 611 | + $type = strtolower($type); |
|
| 612 | + $type = styliser_modele($type, $id); |
|
| 613 | + |
|
| 614 | + $fond = $class = ''; |
|
| 615 | + |
|
| 616 | + $params = array_filter(explode('|', $params)); |
|
| 617 | + if ($params) { |
|
| 618 | + $soustype = current($params); |
|
| 619 | + $soustype = strtolower(trim($soustype)); |
|
| 620 | + if (in_array($soustype, ['left', 'right', 'center', 'ajax'])) { |
|
| 621 | + $soustype = next($params); |
|
| 622 | + $soustype = strtolower($soustype); |
|
| 623 | + } |
|
| 624 | + |
|
| 625 | + if (preg_match(',^[a-z0-9_]+$,', $soustype)) { |
|
| 626 | + if (!trouve_modele($fond = ($type . '_' . $soustype))) { |
|
| 627 | + $fond = ''; |
|
| 628 | + $class = $soustype; |
|
| 629 | + } |
|
| 630 | + // enlever le sous type des params |
|
| 631 | + $params = array_diff($params, [$soustype]); |
|
| 632 | + } |
|
| 633 | + } |
|
| 634 | + |
|
| 635 | + // Si ca marche pas en precisant le sous-type, prendre le type |
|
| 636 | + if (!$fond and !trouve_modele($fond = $type)) { |
|
| 637 | + spip_log("Modele $type introuvable", _LOG_INFO_IMPORTANTE); |
|
| 638 | + |
|
| 639 | + return false; |
|
| 640 | + } |
|
| 641 | + $fond = 'modeles/' . $fond; |
|
| 642 | + // Creer le contexte |
|
| 643 | + $contexte = $env; |
|
| 644 | + $contexte['dir_racine'] = _DIR_RACINE; # eviter de mixer un cache racine et un cache ecrire (meme si pour l'instant les modeles ne sont pas caches, le resultat etant different il faut que le contexte en tienne compte |
|
| 645 | + |
|
| 646 | + // Le numero du modele est mis dans l'environnement |
|
| 647 | + // d'une part sous l'identifiant "id" |
|
| 648 | + // et d'autre part sous l'identifiant de la cle primaire |
|
| 649 | + // par la fonction id_table_objet, |
|
| 650 | + // (<article1> =>> article =>> id_article =>> id_article=1) |
|
| 651 | + $_id = id_table_objet($type); |
|
| 652 | + $contexte['id'] = $contexte[$_id] = $id; |
|
| 653 | + |
|
| 654 | + if (isset($class)) { |
|
| 655 | + $contexte['class'] = $class; |
|
| 656 | + } |
|
| 657 | + |
|
| 658 | + // Si un lien a ete passe en parametre, ex: [<modele1>->url] ou [<modele1|title_du_lien{hreflang}->url] |
|
| 659 | + if ($lien) { |
|
| 660 | + # un eventuel guillemet (") sera reechappe par #ENV |
|
| 661 | + $contexte['lien'] = str_replace('"', '"', $lien['href']); |
|
| 662 | + $contexte['lien_class'] = $lien['class']; |
|
| 663 | + $contexte['lien_mime'] = $lien['mime']; |
|
| 664 | + $contexte['lien_title'] = $lien['title']; |
|
| 665 | + $contexte['lien_hreflang'] = $lien['hreflang']; |
|
| 666 | + } |
|
| 667 | + |
|
| 668 | + // Traiter les parametres |
|
| 669 | + // par exemple : <img1|center>, <emb12|autostart=true> ou <doc1|lang=en> |
|
| 670 | + $arg_list = creer_contexte_de_modele($params); |
|
| 671 | + $contexte['args'] = $arg_list; // on passe la liste des arguments du modeles dans une variable args |
|
| 672 | + $contexte = array_merge($contexte, $arg_list); |
|
| 673 | + |
|
| 674 | + // Appliquer le modele avec le contexte |
|
| 675 | + $retour = recuperer_fond($fond, $contexte, [], $connect); |
|
| 676 | + |
|
| 677 | + // Regarder si le modele tient compte des liens (il *doit* alors indiquer |
|
| 678 | + // spip_lien_ok dans les classes de son conteneur de premier niveau ; |
|
| 679 | + // sinon, s'il y a un lien, on l'ajoute classiquement |
|
| 680 | + if ( |
|
| 681 | + strstr( |
|
| 682 | + ' ' . ($classes = extraire_attribut($retour, 'class')) . ' ', |
|
| 683 | + 'spip_lien_ok' |
|
| 684 | + ) |
|
| 685 | + ) { |
|
| 686 | + $retour = inserer_attribut( |
|
| 687 | + $retour, |
|
| 688 | + 'class', |
|
| 689 | + trim(str_replace(' spip_lien_ok ', ' ', " $classes ")) |
|
| 690 | + ); |
|
| 691 | + } else { |
|
| 692 | + if ($lien) { |
|
| 693 | + $retour = "<a href=\"" . $lien['href'] . "\" class=\"" . $lien['class'] . "\">" . $retour . '</a>'; |
|
| 694 | + } |
|
| 695 | + } |
|
| 696 | + |
|
| 697 | + $compteur--; |
|
| 698 | + |
|
| 699 | + return (isset($arg_list['ajax']) and $arg_list['ajax'] == 'ajax') |
|
| 700 | + ? encoder_contexte_ajax($contexte, '', $retour) |
|
| 701 | + : $retour; |
|
| 702 | 702 | } |
| 703 | 703 | |
| 704 | 704 | // Un inclure_page qui marche aussi pour l'espace prive |
@@ -707,105 +707,105 @@ discard block |
||
| 707 | 707 | // recuperer_fond($fond,$contexte,array('raw'=>true)) |
| 708 | 708 | function evaluer_fond($fond, $contexte = [], string $connect = '') { |
| 709 | 709 | |
| 710 | - $page = inclure_page($fond, $contexte, $connect); |
|
| 711 | - |
|
| 712 | - if (!$page) { |
|
| 713 | - return $page; |
|
| 714 | - } |
|
| 715 | - // eval $page et affecte $res |
|
| 716 | - include _ROOT_RESTREINT . 'public/evaluer_page.php'; |
|
| 717 | - |
|
| 718 | - // Lever un drapeau (global) si le fond utilise #SESSION |
|
| 719 | - // a destination de public/parametrer |
|
| 720 | - // pour remonter vers les inclusions appelantes |
|
| 721 | - // il faut bien lever ce drapeau apres avoir evalue le fond |
|
| 722 | - // pour ne pas faire descendre le flag vers les inclusions appelees |
|
| 723 | - if ( |
|
| 724 | - isset($page['invalideurs']) |
|
| 725 | - and isset($page['invalideurs']['session']) |
|
| 726 | - ) { |
|
| 727 | - $GLOBALS['cache_utilise_session'] = $page['invalideurs']['session']; |
|
| 728 | - } |
|
| 729 | - |
|
| 730 | - return $page; |
|
| 710 | + $page = inclure_page($fond, $contexte, $connect); |
|
| 711 | + |
|
| 712 | + if (!$page) { |
|
| 713 | + return $page; |
|
| 714 | + } |
|
| 715 | + // eval $page et affecte $res |
|
| 716 | + include _ROOT_RESTREINT . 'public/evaluer_page.php'; |
|
| 717 | + |
|
| 718 | + // Lever un drapeau (global) si le fond utilise #SESSION |
|
| 719 | + // a destination de public/parametrer |
|
| 720 | + // pour remonter vers les inclusions appelantes |
|
| 721 | + // il faut bien lever ce drapeau apres avoir evalue le fond |
|
| 722 | + // pour ne pas faire descendre le flag vers les inclusions appelees |
|
| 723 | + if ( |
|
| 724 | + isset($page['invalideurs']) |
|
| 725 | + and isset($page['invalideurs']['session']) |
|
| 726 | + ) { |
|
| 727 | + $GLOBALS['cache_utilise_session'] = $page['invalideurs']['session']; |
|
| 728 | + } |
|
| 729 | + |
|
| 730 | + return $page; |
|
| 731 | 731 | } |
| 732 | 732 | |
| 733 | 733 | |
| 734 | 734 | function page_base_href(&$texte) { |
| 735 | - static $set_html_base = null; |
|
| 736 | - if (is_null($set_html_base)) { |
|
| 737 | - if (!defined('_SET_HTML_BASE')) { |
|
| 738 | - // si la profondeur est superieure a 1 |
|
| 739 | - // est que ce n'est pas une url page ni une url action |
|
| 740 | - // activer par defaut |
|
| 741 | - $set_html_base = (( |
|
| 742 | - $GLOBALS['profondeur_url'] >= (_DIR_RESTREINT ? 1 : 2) |
|
| 743 | - and _request(_SPIP_PAGE) !== 'login' |
|
| 744 | - and !_request('action')) ? true : false); |
|
| 745 | - } else { |
|
| 746 | - $set_html_base = _SET_HTML_BASE; |
|
| 747 | - } |
|
| 748 | - } |
|
| 749 | - |
|
| 750 | - if ( |
|
| 751 | - $set_html_base |
|
| 752 | - and isset($GLOBALS['html']) and $GLOBALS['html'] |
|
| 753 | - and $GLOBALS['profondeur_url'] > 0 |
|
| 754 | - and ($poshead = strpos($texte, '</head>')) !== false |
|
| 755 | - ) { |
|
| 756 | - $head = substr($texte, 0, $poshead); |
|
| 757 | - $insert = false; |
|
| 758 | - $href_base = false; |
|
| 759 | - if (strpos($head, '<base') === false) { |
|
| 760 | - $insert = true; |
|
| 761 | - } else { |
|
| 762 | - // si aucun <base ...> n'a de href il faut en inserer un |
|
| 763 | - // sinon juste re-ecrire les ancres si besoin |
|
| 764 | - $insert = true; |
|
| 765 | - include_spip('inc/filtres'); |
|
| 766 | - $bases = extraire_balises($head, 'base'); |
|
| 767 | - foreach ($bases as $base) { |
|
| 768 | - if ($href_base = extraire_attribut($base, 'href')) { |
|
| 769 | - $insert = false; |
|
| 770 | - break; |
|
| 771 | - } |
|
| 772 | - } |
|
| 773 | - } |
|
| 774 | - |
|
| 775 | - if ($insert) { |
|
| 776 | - include_spip('inc/filtres_mini'); |
|
| 777 | - // ajouter un base qui reglera tous les liens relatifs |
|
| 778 | - $href_base = url_absolue('./'); |
|
| 779 | - $base = "\n<base href=\"$href_base\" />"; |
|
| 780 | - if (($pos = strpos($head, '<head>')) !== false) { |
|
| 781 | - $head = substr_replace($head, $base, $pos + 6, 0); |
|
| 782 | - } elseif (preg_match(',<head[^>]*>,i', $head, $r)) { |
|
| 783 | - $head = str_replace($r[0], $r[0] . $base, $head); |
|
| 784 | - } |
|
| 785 | - $texte = $head . substr($texte, $poshead); |
|
| 786 | - } |
|
| 787 | - if ($href_base) { |
|
| 788 | - // gerer les ancres |
|
| 789 | - $base = $_SERVER['REQUEST_URI']; |
|
| 790 | - // pas de guillemets ni < dans l'URL qu'on insere dans le HTML |
|
| 791 | - if (strpos($base, "'") or strpos($base, '"') or strpos($base, '<')) { |
|
| 792 | - $base = str_replace(["'",'"','<'], ['%27','%22','%3C'], $base); |
|
| 793 | - } |
|
| 794 | - if (strpos($texte, "href='#") !== false) { |
|
| 795 | - $texte = str_replace("href='#", "href='$base#", $texte); |
|
| 796 | - } |
|
| 797 | - if (strpos($texte, 'href="#') !== false) { |
|
| 798 | - $texte = str_replace('href="#', "href=\"$base#", $texte); |
|
| 799 | - } |
|
| 800 | - } |
|
| 801 | - } |
|
| 735 | + static $set_html_base = null; |
|
| 736 | + if (is_null($set_html_base)) { |
|
| 737 | + if (!defined('_SET_HTML_BASE')) { |
|
| 738 | + // si la profondeur est superieure a 1 |
|
| 739 | + // est que ce n'est pas une url page ni une url action |
|
| 740 | + // activer par defaut |
|
| 741 | + $set_html_base = (( |
|
| 742 | + $GLOBALS['profondeur_url'] >= (_DIR_RESTREINT ? 1 : 2) |
|
| 743 | + and _request(_SPIP_PAGE) !== 'login' |
|
| 744 | + and !_request('action')) ? true : false); |
|
| 745 | + } else { |
|
| 746 | + $set_html_base = _SET_HTML_BASE; |
|
| 747 | + } |
|
| 748 | + } |
|
| 749 | + |
|
| 750 | + if ( |
|
| 751 | + $set_html_base |
|
| 752 | + and isset($GLOBALS['html']) and $GLOBALS['html'] |
|
| 753 | + and $GLOBALS['profondeur_url'] > 0 |
|
| 754 | + and ($poshead = strpos($texte, '</head>')) !== false |
|
| 755 | + ) { |
|
| 756 | + $head = substr($texte, 0, $poshead); |
|
| 757 | + $insert = false; |
|
| 758 | + $href_base = false; |
|
| 759 | + if (strpos($head, '<base') === false) { |
|
| 760 | + $insert = true; |
|
| 761 | + } else { |
|
| 762 | + // si aucun <base ...> n'a de href il faut en inserer un |
|
| 763 | + // sinon juste re-ecrire les ancres si besoin |
|
| 764 | + $insert = true; |
|
| 765 | + include_spip('inc/filtres'); |
|
| 766 | + $bases = extraire_balises($head, 'base'); |
|
| 767 | + foreach ($bases as $base) { |
|
| 768 | + if ($href_base = extraire_attribut($base, 'href')) { |
|
| 769 | + $insert = false; |
|
| 770 | + break; |
|
| 771 | + } |
|
| 772 | + } |
|
| 773 | + } |
|
| 774 | + |
|
| 775 | + if ($insert) { |
|
| 776 | + include_spip('inc/filtres_mini'); |
|
| 777 | + // ajouter un base qui reglera tous les liens relatifs |
|
| 778 | + $href_base = url_absolue('./'); |
|
| 779 | + $base = "\n<base href=\"$href_base\" />"; |
|
| 780 | + if (($pos = strpos($head, '<head>')) !== false) { |
|
| 781 | + $head = substr_replace($head, $base, $pos + 6, 0); |
|
| 782 | + } elseif (preg_match(',<head[^>]*>,i', $head, $r)) { |
|
| 783 | + $head = str_replace($r[0], $r[0] . $base, $head); |
|
| 784 | + } |
|
| 785 | + $texte = $head . substr($texte, $poshead); |
|
| 786 | + } |
|
| 787 | + if ($href_base) { |
|
| 788 | + // gerer les ancres |
|
| 789 | + $base = $_SERVER['REQUEST_URI']; |
|
| 790 | + // pas de guillemets ni < dans l'URL qu'on insere dans le HTML |
|
| 791 | + if (strpos($base, "'") or strpos($base, '"') or strpos($base, '<')) { |
|
| 792 | + $base = str_replace(["'",'"','<'], ['%27','%22','%3C'], $base); |
|
| 793 | + } |
|
| 794 | + if (strpos($texte, "href='#") !== false) { |
|
| 795 | + $texte = str_replace("href='#", "href='$base#", $texte); |
|
| 796 | + } |
|
| 797 | + if (strpos($texte, 'href="#') !== false) { |
|
| 798 | + $texte = str_replace('href="#', "href=\"$base#", $texte); |
|
| 799 | + } |
|
| 800 | + } |
|
| 801 | + } |
|
| 802 | 802 | } |
| 803 | 803 | |
| 804 | 804 | |
| 805 | 805 | // Envoyer les entetes, en retenant ceux qui sont a usage interne |
| 806 | 806 | // et demarrent par X-Spip-... |
| 807 | 807 | function envoyer_entetes($entetes) { |
| 808 | - foreach ($entetes as $k => $v) { # if (strncmp($k, 'X-Spip-', 7)) |
|
| 809 | - @header(strlen($v) ? "$k: $v" : $k); |
|
| 810 | - } |
|
| 808 | + foreach ($entetes as $k => $v) { # if (strncmp($k, 'X-Spip-', 7)) |
|
| 809 | + @header(strlen($v) ? "$k: $v" : $k); |
|
| 810 | + } |
|
| 811 | 811 | } |
@@ -21,7 +21,7 @@ discard block |
||
| 21 | 21 | **/ |
| 22 | 22 | |
| 23 | 23 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 24 | - return; |
|
| 24 | + return; |
|
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | /** |
@@ -47,12 +47,12 @@ discard block |
||
| 47 | 47 | **/ |
| 48 | 48 | function critere_racine_dist($idb, &$boucles, $crit) { |
| 49 | 49 | |
| 50 | - $not = $crit->not; |
|
| 51 | - $boucle = &$boucles[$idb]; |
|
| 52 | - $id_parent = $GLOBALS['exceptions_des_tables'][$boucle->id_table]['id_parent'] ?? 'id_parent'; |
|
| 50 | + $not = $crit->not; |
|
| 51 | + $boucle = &$boucles[$idb]; |
|
| 52 | + $id_parent = $GLOBALS['exceptions_des_tables'][$boucle->id_table]['id_parent'] ?? 'id_parent'; |
|
| 53 | 53 | |
| 54 | - $c = ["'='", "'$boucle->id_table." . "$id_parent'", 0]; |
|
| 55 | - $boucle->where[] = ($crit->not ? ["'NOT'", $c] : $c); |
|
| 54 | + $c = ["'='", "'$boucle->id_table." . "$id_parent'", 0]; |
|
| 55 | + $boucle->where[] = ($crit->not ? ["'NOT'", $c] : $c); |
|
| 56 | 56 | } |
| 57 | 57 | |
| 58 | 58 | |
@@ -69,15 +69,15 @@ discard block |
||
| 69 | 69 | * @return void|array |
| 70 | 70 | **/ |
| 71 | 71 | function critere_exclus_dist($idb, &$boucles, $crit) { |
| 72 | - $not = $crit->not; |
|
| 73 | - $boucle = &$boucles[$idb]; |
|
| 74 | - $id = $boucle->primary; |
|
| 75 | - |
|
| 76 | - if ($not or !$id) { |
|
| 77 | - return ['zbug_critere_inconnu', ['critere' => $not . $crit->op]]; |
|
| 78 | - } |
|
| 79 | - $arg = kwote(calculer_argument_precedent($idb, $id, $boucles)); |
|
| 80 | - $boucle->where[] = ["'!='", "'$boucle->id_table." . "$id'", $arg]; |
|
| 72 | + $not = $crit->not; |
|
| 73 | + $boucle = &$boucles[$idb]; |
|
| 74 | + $id = $boucle->primary; |
|
| 75 | + |
|
| 76 | + if ($not or !$id) { |
|
| 77 | + return ['zbug_critere_inconnu', ['critere' => $not . $crit->op]]; |
|
| 78 | + } |
|
| 79 | + $arg = kwote(calculer_argument_precedent($idb, $id, $boucles)); |
|
| 80 | + $boucle->where[] = ["'!='", "'$boucle->id_table." . "$id'", $arg]; |
|
| 81 | 81 | } |
| 82 | 82 | |
| 83 | 83 | |
@@ -97,73 +97,73 @@ discard block |
||
| 97 | 97 | * @return void|array |
| 98 | 98 | **/ |
| 99 | 99 | function critere_doublons_dist($idb, &$boucles, $crit) { |
| 100 | - $boucle = &$boucles[$idb]; |
|
| 101 | - $primary = $boucle->primary; |
|
| 102 | - |
|
| 103 | - // la table nécessite une clé primaire, non composée |
|
| 104 | - if (!$primary or strpos($primary, ',')) { |
|
| 105 | - return ['zbug_doublon_sur_table_sans_cle_primaire']; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - $not = ($crit->not ? '' : 'NOT'); |
|
| 109 | - |
|
| 110 | - // le doublon s'applique sur un type de boucle (article) |
|
| 111 | - $nom = "'" . $boucle->type_requete . "'"; |
|
| 112 | - |
|
| 113 | - // compléter le nom avec un nom précisé {doublons nom} |
|
| 114 | - // on obtient $nom = "'article' . 'nom'" |
|
| 115 | - if (isset($crit->param[0])) { |
|
| 116 | - $nom .= '.' . calculer_liste($crit->param[0], $idb, $boucles, $boucles[$idb]->id_parent); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - // code qui déclarera l'index du stockage de nos doublons (pour éviter une notice PHP) |
|
| 120 | - $init_comment = "\n\n\t// Initialise le(s) critère(s) doublons\n"; |
|
| 121 | - $init_code = "\tif (!isset(\$doublons[\$d = $nom])) { \$doublons[\$d] = ''; }\n"; |
|
| 122 | - |
|
| 123 | - // on crée un sql_in avec la clé primaire de la table |
|
| 124 | - // et la collection des doublons déjà emmagasinés dans le tableau |
|
| 125 | - // $doublons et son index, ici $nom |
|
| 126 | - |
|
| 127 | - // debut du code "sql_in('articles.id_article', " |
|
| 128 | - $debut_in = "sql_in('" . $boucle->id_table . '.' . $primary . "', "; |
|
| 129 | - // lecture des données du doublon "$doublons[$doublon_index[] = " |
|
| 130 | - // Attention : boucle->doublons désigne une variable qu'on affecte |
|
| 131 | - $debut_doub = '$doublons[' . (!$not ? '' : ($boucle->doublons . '[]= ')); |
|
| 132 | - |
|
| 133 | - // le debut complet du code des doublons |
|
| 134 | - $debut_doub = $debut_in . $debut_doub; |
|
| 135 | - |
|
| 136 | - // nom du doublon "('article' . 'nom')]" |
|
| 137 | - $fin_doub = "($nom)]"; |
|
| 138 | - |
|
| 139 | - // si on trouve un autre critère doublon, |
|
| 140 | - // on fusionne pour avoir un seul IN, et on s'en va ! |
|
| 141 | - foreach ($boucle->where as $k => $w) { |
|
| 142 | - if (strpos($w[0], $debut_doub) === 0) { |
|
| 143 | - // fusionner le sql_in (du where) |
|
| 144 | - $boucle->where[$k][0] = $debut_doub . $fin_doub . ' . ' . substr($w[0], strlen($debut_in)); |
|
| 145 | - // fusionner l'initialisation (du hash) pour faire plus joli |
|
| 146 | - $x = strpos($boucle->hash, $init_comment); |
|
| 147 | - $len = strlen($init_comment); |
|
| 148 | - $boucle->hash = |
|
| 149 | - substr($boucle->hash, 0, $x + $len) . $init_code . substr($boucle->hash, $x + $len); |
|
| 150 | - |
|
| 151 | - return; |
|
| 152 | - } |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - // mettre l'ensemble dans un tableau pour que ce ne soit pas vu comme une constante |
|
| 156 | - $boucle->where[] = [$debut_doub . $fin_doub . ", '" . $not . "')"]; |
|
| 157 | - |
|
| 158 | - // déclarer le doublon s'il n'existe pas encore |
|
| 159 | - $boucle->hash .= $init_comment . $init_code; |
|
| 160 | - |
|
| 161 | - |
|
| 162 | - # la ligne suivante avait l'intention d'eviter une collecte deja faite |
|
| 163 | - # mais elle fait planter une boucle a 2 critere doublons: |
|
| 164 | - # {!doublons A}{doublons B} |
|
| 165 | - # (de http://article.gmane.org/gmane.comp.web.spip.devel/31034) |
|
| 166 | - # if ($crit->not) $boucle->doublons = ""; |
|
| 100 | + $boucle = &$boucles[$idb]; |
|
| 101 | + $primary = $boucle->primary; |
|
| 102 | + |
|
| 103 | + // la table nécessite une clé primaire, non composée |
|
| 104 | + if (!$primary or strpos($primary, ',')) { |
|
| 105 | + return ['zbug_doublon_sur_table_sans_cle_primaire']; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + $not = ($crit->not ? '' : 'NOT'); |
|
| 109 | + |
|
| 110 | + // le doublon s'applique sur un type de boucle (article) |
|
| 111 | + $nom = "'" . $boucle->type_requete . "'"; |
|
| 112 | + |
|
| 113 | + // compléter le nom avec un nom précisé {doublons nom} |
|
| 114 | + // on obtient $nom = "'article' . 'nom'" |
|
| 115 | + if (isset($crit->param[0])) { |
|
| 116 | + $nom .= '.' . calculer_liste($crit->param[0], $idb, $boucles, $boucles[$idb]->id_parent); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + // code qui déclarera l'index du stockage de nos doublons (pour éviter une notice PHP) |
|
| 120 | + $init_comment = "\n\n\t// Initialise le(s) critère(s) doublons\n"; |
|
| 121 | + $init_code = "\tif (!isset(\$doublons[\$d = $nom])) { \$doublons[\$d] = ''; }\n"; |
|
| 122 | + |
|
| 123 | + // on crée un sql_in avec la clé primaire de la table |
|
| 124 | + // et la collection des doublons déjà emmagasinés dans le tableau |
|
| 125 | + // $doublons et son index, ici $nom |
|
| 126 | + |
|
| 127 | + // debut du code "sql_in('articles.id_article', " |
|
| 128 | + $debut_in = "sql_in('" . $boucle->id_table . '.' . $primary . "', "; |
|
| 129 | + // lecture des données du doublon "$doublons[$doublon_index[] = " |
|
| 130 | + // Attention : boucle->doublons désigne une variable qu'on affecte |
|
| 131 | + $debut_doub = '$doublons[' . (!$not ? '' : ($boucle->doublons . '[]= ')); |
|
| 132 | + |
|
| 133 | + // le debut complet du code des doublons |
|
| 134 | + $debut_doub = $debut_in . $debut_doub; |
|
| 135 | + |
|
| 136 | + // nom du doublon "('article' . 'nom')]" |
|
| 137 | + $fin_doub = "($nom)]"; |
|
| 138 | + |
|
| 139 | + // si on trouve un autre critère doublon, |
|
| 140 | + // on fusionne pour avoir un seul IN, et on s'en va ! |
|
| 141 | + foreach ($boucle->where as $k => $w) { |
|
| 142 | + if (strpos($w[0], $debut_doub) === 0) { |
|
| 143 | + // fusionner le sql_in (du where) |
|
| 144 | + $boucle->where[$k][0] = $debut_doub . $fin_doub . ' . ' . substr($w[0], strlen($debut_in)); |
|
| 145 | + // fusionner l'initialisation (du hash) pour faire plus joli |
|
| 146 | + $x = strpos($boucle->hash, $init_comment); |
|
| 147 | + $len = strlen($init_comment); |
|
| 148 | + $boucle->hash = |
|
| 149 | + substr($boucle->hash, 0, $x + $len) . $init_code . substr($boucle->hash, $x + $len); |
|
| 150 | + |
|
| 151 | + return; |
|
| 152 | + } |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + // mettre l'ensemble dans un tableau pour que ce ne soit pas vu comme une constante |
|
| 156 | + $boucle->where[] = [$debut_doub . $fin_doub . ", '" . $not . "')"]; |
|
| 157 | + |
|
| 158 | + // déclarer le doublon s'il n'existe pas encore |
|
| 159 | + $boucle->hash .= $init_comment . $init_code; |
|
| 160 | + |
|
| 161 | + |
|
| 162 | + # la ligne suivante avait l'intention d'eviter une collecte deja faite |
|
| 163 | + # mais elle fait planter une boucle a 2 critere doublons: |
|
| 164 | + # {!doublons A}{doublons B} |
|
| 165 | + # (de http://article.gmane.org/gmane.comp.web.spip.devel/31034) |
|
| 166 | + # if ($crit->not) $boucle->doublons = ""; |
|
| 167 | 167 | } |
| 168 | 168 | |
| 169 | 169 | |
@@ -184,14 +184,14 @@ discard block |
||
| 184 | 184 | * @return void |
| 185 | 185 | **/ |
| 186 | 186 | function critere_lang_select_dist($idb, &$boucles, $crit) { |
| 187 | - if (!isset($crit->param[1][0]) or !($param = $crit->param[1][0]->texte)) { |
|
| 188 | - $param = 'oui'; |
|
| 189 | - } |
|
| 190 | - if ($crit->not) { |
|
| 191 | - $param = ($param == 'oui') ? 'non' : 'oui'; |
|
| 192 | - } |
|
| 193 | - $boucle = &$boucles[$idb]; |
|
| 194 | - $boucle->lang_select = $param; |
|
| 187 | + if (!isset($crit->param[1][0]) or !($param = $crit->param[1][0]->texte)) { |
|
| 188 | + $param = 'oui'; |
|
| 189 | + } |
|
| 190 | + if ($crit->not) { |
|
| 191 | + $param = ($param == 'oui') ? 'non' : 'oui'; |
|
| 192 | + } |
|
| 193 | + $boucle = &$boucles[$idb]; |
|
| 194 | + $boucle->lang_select = $param; |
|
| 195 | 195 | } |
| 196 | 196 | |
| 197 | 197 | |
@@ -213,15 +213,15 @@ discard block |
||
| 213 | 213 | * @return void |
| 214 | 214 | **/ |
| 215 | 215 | function critere_debut_dist($idb, &$boucles, $crit) { |
| 216 | - [$un, $deux] = $crit->param; |
|
| 217 | - $un = $un[0]->texte; |
|
| 218 | - $deux = $deux[0]->texte; |
|
| 219 | - if ($deux) { |
|
| 220 | - $boucles[$idb]->limit = |
|
| 221 | - 'intval($Pile[0]["debut' . $un . '"]) . ",' . $deux . '"'; |
|
| 222 | - } else { |
|
| 223 | - calculer_critere_DEFAUT_dist($idb, $boucles, $crit); |
|
| 224 | - } |
|
| 216 | + [$un, $deux] = $crit->param; |
|
| 217 | + $un = $un[0]->texte; |
|
| 218 | + $deux = $deux[0]->texte; |
|
| 219 | + if ($deux) { |
|
| 220 | + $boucles[$idb]->limit = |
|
| 221 | + 'intval($Pile[0]["debut' . $un . '"]) . ",' . $deux . '"'; |
|
| 222 | + } else { |
|
| 223 | + calculer_critere_DEFAUT_dist($idb, $boucles, $crit); |
|
| 224 | + } |
|
| 225 | 225 | } |
| 226 | 226 | |
| 227 | 227 | |
@@ -255,58 +255,58 @@ discard block |
||
| 255 | 255 | **/ |
| 256 | 256 | function critere_pagination_dist($idb, &$boucles, $crit) { |
| 257 | 257 | |
| 258 | - $boucle = &$boucles[$idb]; |
|
| 259 | - // definition de la taille de la page |
|
| 260 | - $pas = !isset($crit->param[0][0]) ? "''" |
|
| 261 | - : calculer_liste([$crit->param[0][0]], $idb, $boucles, $boucle->id_parent); |
|
| 262 | - |
|
| 263 | - if (!preg_match(_CODE_QUOTE, $pas, $r)) { |
|
| 264 | - $pas = "((\$a = intval($pas)) ? \$a : 10)"; |
|
| 265 | - } else { |
|
| 266 | - $r = intval($r[2]); |
|
| 267 | - $pas = strval($r ?: 10); |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - // Calcul du nommage de la pagination si il existe. |
|
| 271 | - // La nouvelle syntaxe {pagination 20, nom} est prise en compte et privilégiée mais on reste |
|
| 272 | - // compatible avec l'ancienne car certains cas fonctionnent correctement |
|
| 273 | - $type = "'$idb'"; |
|
| 274 | - // Calcul d'un nommage spécifique de la pagination si précisé. |
|
| 275 | - // Syntaxe {pagination 20, nom} |
|
| 276 | - if (isset($crit->param[0][1])) { |
|
| 277 | - $type = calculer_liste([$crit->param[0][1]], $idb, $boucles, $boucle->id_parent); |
|
| 278 | - } // Ancienne syntaxe {pagination 20 nom} pour compatibilité |
|
| 279 | - elseif (isset($crit->param[1][0])) { |
|
| 280 | - $type = calculer_liste([$crit->param[1][0]], $idb, $boucles, $boucle->id_parent); |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - $debut = ($type[0] !== "'") ? "'debut'.$type" : ("'debut" . substr($type, 1)); |
|
| 284 | - $boucle->modificateur['debut_nom'] = $type; |
|
| 285 | - $partie = |
|
| 286 | - // tester si le numero de page demande est de la forme '@yyy' |
|
| 287 | - 'isset($Pile[0][' . $debut . ']) ? $Pile[0][' . $debut . '] : _request(' . $debut . ");\n" |
|
| 288 | - . "\tif (\$debut_boucle && \$debut_boucle[0] === '@') {\n" |
|
| 289 | - . "\t\t" . '$debut_boucle = $Pile[0][' . $debut . '] = quete_debut_pagination(\'' . $boucle->primary . '\',$Pile[0][\'@' . $boucle->primary . '\'] = substr($debut_boucle,1),' . $pas . ',$iter);' . "\n" |
|
| 290 | - . "\t\t" . '$iter->seek(0);' . "\n" |
|
| 291 | - . "\t}\n" |
|
| 292 | - . "\t" . '$debut_boucle = intval($debut_boucle)'; |
|
| 293 | - |
|
| 294 | - $boucle->hash .= ' |
|
| 258 | + $boucle = &$boucles[$idb]; |
|
| 259 | + // definition de la taille de la page |
|
| 260 | + $pas = !isset($crit->param[0][0]) ? "''" |
|
| 261 | + : calculer_liste([$crit->param[0][0]], $idb, $boucles, $boucle->id_parent); |
|
| 262 | + |
|
| 263 | + if (!preg_match(_CODE_QUOTE, $pas, $r)) { |
|
| 264 | + $pas = "((\$a = intval($pas)) ? \$a : 10)"; |
|
| 265 | + } else { |
|
| 266 | + $r = intval($r[2]); |
|
| 267 | + $pas = strval($r ?: 10); |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + // Calcul du nommage de la pagination si il existe. |
|
| 271 | + // La nouvelle syntaxe {pagination 20, nom} est prise en compte et privilégiée mais on reste |
|
| 272 | + // compatible avec l'ancienne car certains cas fonctionnent correctement |
|
| 273 | + $type = "'$idb'"; |
|
| 274 | + // Calcul d'un nommage spécifique de la pagination si précisé. |
|
| 275 | + // Syntaxe {pagination 20, nom} |
|
| 276 | + if (isset($crit->param[0][1])) { |
|
| 277 | + $type = calculer_liste([$crit->param[0][1]], $idb, $boucles, $boucle->id_parent); |
|
| 278 | + } // Ancienne syntaxe {pagination 20 nom} pour compatibilité |
|
| 279 | + elseif (isset($crit->param[1][0])) { |
|
| 280 | + $type = calculer_liste([$crit->param[1][0]], $idb, $boucles, $boucle->id_parent); |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + $debut = ($type[0] !== "'") ? "'debut'.$type" : ("'debut" . substr($type, 1)); |
|
| 284 | + $boucle->modificateur['debut_nom'] = $type; |
|
| 285 | + $partie = |
|
| 286 | + // tester si le numero de page demande est de la forme '@yyy' |
|
| 287 | + 'isset($Pile[0][' . $debut . ']) ? $Pile[0][' . $debut . '] : _request(' . $debut . ");\n" |
|
| 288 | + . "\tif (\$debut_boucle && \$debut_boucle[0] === '@') {\n" |
|
| 289 | + . "\t\t" . '$debut_boucle = $Pile[0][' . $debut . '] = quete_debut_pagination(\'' . $boucle->primary . '\',$Pile[0][\'@' . $boucle->primary . '\'] = substr($debut_boucle,1),' . $pas . ',$iter);' . "\n" |
|
| 290 | + . "\t\t" . '$iter->seek(0);' . "\n" |
|
| 291 | + . "\t}\n" |
|
| 292 | + . "\t" . '$debut_boucle = intval($debut_boucle)'; |
|
| 293 | + |
|
| 294 | + $boucle->hash .= ' |
|
| 295 | 295 | $command[\'pagination\'] = array((isset($Pile[0][' . $debut . ']) ? $Pile[0][' . $debut . '] : null), ' . $pas . ');'; |
| 296 | 296 | |
| 297 | - $boucle->total_parties = $pas; |
|
| 298 | - calculer_parties($boucles, $idb, $partie, 'p+'); |
|
| 299 | - // ajouter la cle primaire dans le select pour pouvoir gerer la pagination referencee par @id |
|
| 300 | - // sauf si pas de primaire, ou si primaire composee |
|
| 301 | - // dans ce cas, on ne sait pas gerer une pagination indirecte |
|
| 302 | - $t = $boucle->id_table . '.' . $boucle->primary; |
|
| 303 | - if ( |
|
| 304 | - $boucle->primary |
|
| 305 | - and !preg_match('/[,\s]/', $boucle->primary) |
|
| 306 | - and !in_array($t, $boucle->select) |
|
| 307 | - ) { |
|
| 308 | - $boucle->select[] = $t; |
|
| 309 | - } |
|
| 297 | + $boucle->total_parties = $pas; |
|
| 298 | + calculer_parties($boucles, $idb, $partie, 'p+'); |
|
| 299 | + // ajouter la cle primaire dans le select pour pouvoir gerer la pagination referencee par @id |
|
| 300 | + // sauf si pas de primaire, ou si primaire composee |
|
| 301 | + // dans ce cas, on ne sait pas gerer une pagination indirecte |
|
| 302 | + $t = $boucle->id_table . '.' . $boucle->primary; |
|
| 303 | + if ( |
|
| 304 | + $boucle->primary |
|
| 305 | + and !preg_match('/[,\s]/', $boucle->primary) |
|
| 306 | + and !in_array($t, $boucle->select) |
|
| 307 | + ) { |
|
| 308 | + $boucle->select[] = $t; |
|
| 309 | + } |
|
| 310 | 310 | } |
| 311 | 311 | |
| 312 | 312 | |
@@ -328,24 +328,24 @@ discard block |
||
| 328 | 328 | **/ |
| 329 | 329 | function critere_recherche_dist($idb, &$boucles, $crit) { |
| 330 | 330 | |
| 331 | - $boucle = &$boucles[$idb]; |
|
| 331 | + $boucle = &$boucles[$idb]; |
|
| 332 | 332 | |
| 333 | - if (!$boucle->primary or strpos($boucle->primary, ',')) { |
|
| 334 | - erreur_squelette(_T('zbug_critere_sur_table_sans_cle_primaire', ['critere' => 'recherche']), $boucle); |
|
| 333 | + if (!$boucle->primary or strpos($boucle->primary, ',')) { |
|
| 334 | + erreur_squelette(_T('zbug_critere_sur_table_sans_cle_primaire', ['critere' => 'recherche']), $boucle); |
|
| 335 | 335 | |
| 336 | - return; |
|
| 337 | - } |
|
| 336 | + return; |
|
| 337 | + } |
|
| 338 | 338 | |
| 339 | - if (isset($crit->param[0])) { |
|
| 340 | - $quoi = calculer_liste($crit->param[0], $idb, $boucles, $boucles[$idb]->id_parent); |
|
| 341 | - } else { |
|
| 342 | - $quoi = '(isset($Pile[0]["recherche"])?$Pile[0]["recherche"]:(isset($GLOBALS["recherche"])?$GLOBALS["recherche"]:""))'; |
|
| 343 | - } |
|
| 339 | + if (isset($crit->param[0])) { |
|
| 340 | + $quoi = calculer_liste($crit->param[0], $idb, $boucles, $boucles[$idb]->id_parent); |
|
| 341 | + } else { |
|
| 342 | + $quoi = '(isset($Pile[0]["recherche"])?$Pile[0]["recherche"]:(isset($GLOBALS["recherche"])?$GLOBALS["recherche"]:""))'; |
|
| 343 | + } |
|
| 344 | 344 | |
| 345 | - $_modificateur = var_export($boucle->modificateur, true); |
|
| 346 | - $boucle->hash .= ' |
|
| 345 | + $_modificateur = var_export($boucle->modificateur, true); |
|
| 346 | + $boucle->hash .= ' |
|
| 347 | 347 | // RECHERCHE' |
| 348 | - . ($crit->cond ? ' |
|
| 348 | + . ($crit->cond ? ' |
|
| 349 | 349 | if (!strlen(' . $quoi . ')){ |
| 350 | 350 | list($rech_select, $rech_where) = array("0 as points",""); |
| 351 | 351 | } else' : '') . ' |
@@ -356,21 +356,21 @@ discard block |
||
| 356 | 356 | '; |
| 357 | 357 | |
| 358 | 358 | |
| 359 | - $t = $boucle->id_table . '.' . $boucle->primary; |
|
| 360 | - if (!in_array($t, $boucles[$idb]->select)) { |
|
| 361 | - $boucle->select[] = $t; |
|
| 362 | - } # pour postgres, neuneu ici |
|
| 363 | - // jointure uniquement sur le serveur principal |
|
| 364 | - // (on ne peut joindre une table d'un serveur distant avec la table des resultats du serveur principal) |
|
| 365 | - if (!$boucle->sql_serveur) { |
|
| 366 | - $boucle->join['resultats'] = ["'" . $boucle->id_table . "'", "'id'", "'" . $boucle->primary . "'"]; |
|
| 367 | - $boucle->from['resultats'] = 'spip_resultats'; |
|
| 368 | - } |
|
| 369 | - $boucle->select[] = '$rech_select'; |
|
| 370 | - //$boucle->where[]= "\$rech_where?'resultats.id=".$boucle->id_table.".".$boucle->primary."':''"; |
|
| 371 | - |
|
| 372 | - // et la recherche trouve |
|
| 373 | - $boucle->where[] = '$rech_where?$rech_where:\'\''; |
|
| 359 | + $t = $boucle->id_table . '.' . $boucle->primary; |
|
| 360 | + if (!in_array($t, $boucles[$idb]->select)) { |
|
| 361 | + $boucle->select[] = $t; |
|
| 362 | + } # pour postgres, neuneu ici |
|
| 363 | + // jointure uniquement sur le serveur principal |
|
| 364 | + // (on ne peut joindre une table d'un serveur distant avec la table des resultats du serveur principal) |
|
| 365 | + if (!$boucle->sql_serveur) { |
|
| 366 | + $boucle->join['resultats'] = ["'" . $boucle->id_table . "'", "'id'", "'" . $boucle->primary . "'"]; |
|
| 367 | + $boucle->from['resultats'] = 'spip_resultats'; |
|
| 368 | + } |
|
| 369 | + $boucle->select[] = '$rech_select'; |
|
| 370 | + //$boucle->where[]= "\$rech_where?'resultats.id=".$boucle->id_table.".".$boucle->primary."':''"; |
|
| 371 | + |
|
| 372 | + // et la recherche trouve |
|
| 373 | + $boucle->where[] = '$rech_where?$rech_where:\'\''; |
|
| 374 | 374 | } |
| 375 | 375 | |
| 376 | 376 | /** |
@@ -387,25 +387,25 @@ discard block |
||
| 387 | 387 | * @return void |
| 388 | 388 | **/ |
| 389 | 389 | function critere_traduction_dist($idb, &$boucles, $crit) { |
| 390 | - $boucle = &$boucles[$idb]; |
|
| 391 | - $prim = $boucle->primary; |
|
| 392 | - $table = $boucle->id_table; |
|
| 393 | - $arg = kwote(calculer_argument_precedent($idb, 'id_trad', $boucles)); |
|
| 394 | - $dprim = kwote(calculer_argument_precedent($idb, $prim, $boucles)); |
|
| 395 | - $boucle->where[] = |
|
| 396 | - [ |
|
| 397 | - "'OR'", |
|
| 398 | - [ |
|
| 399 | - "'AND'", |
|
| 400 | - ["'='", "'$table.id_trad'", 0], |
|
| 401 | - ["'='", "'$table.$prim'", $dprim] |
|
| 402 | - ], |
|
| 403 | - [ |
|
| 404 | - "'AND'", |
|
| 405 | - ["'>'", "'$table.id_trad'", 0], |
|
| 406 | - ["'='", "'$table.id_trad'", $arg] |
|
| 407 | - ] |
|
| 408 | - ]; |
|
| 390 | + $boucle = &$boucles[$idb]; |
|
| 391 | + $prim = $boucle->primary; |
|
| 392 | + $table = $boucle->id_table; |
|
| 393 | + $arg = kwote(calculer_argument_precedent($idb, 'id_trad', $boucles)); |
|
| 394 | + $dprim = kwote(calculer_argument_precedent($idb, $prim, $boucles)); |
|
| 395 | + $boucle->where[] = |
|
| 396 | + [ |
|
| 397 | + "'OR'", |
|
| 398 | + [ |
|
| 399 | + "'AND'", |
|
| 400 | + ["'='", "'$table.id_trad'", 0], |
|
| 401 | + ["'='", "'$table.$prim'", $dprim] |
|
| 402 | + ], |
|
| 403 | + [ |
|
| 404 | + "'AND'", |
|
| 405 | + ["'>'", "'$table.id_trad'", 0], |
|
| 406 | + ["'='", "'$table.id_trad'", $arg] |
|
| 407 | + ] |
|
| 408 | + ]; |
|
| 409 | 409 | } |
| 410 | 410 | |
| 411 | 411 | |
@@ -423,17 +423,17 @@ discard block |
||
| 423 | 423 | * @return void |
| 424 | 424 | **/ |
| 425 | 425 | function critere_origine_traduction_dist($idb, &$boucles, $crit) { |
| 426 | - $boucle = &$boucles[$idb]; |
|
| 427 | - $prim = $boucle->primary; |
|
| 428 | - $table = $boucle->id_table; |
|
| 429 | - |
|
| 430 | - $c = |
|
| 431 | - [ |
|
| 432 | - "'OR'", |
|
| 433 | - ["'='", "'$table." . "id_trad'", "'$table.$prim'"], |
|
| 434 | - ["'='", "'$table.id_trad'", "'0'"] |
|
| 435 | - ]; |
|
| 436 | - $boucle->where[] = ($crit->not ? ["'NOT'", $c] : $c); |
|
| 426 | + $boucle = &$boucles[$idb]; |
|
| 427 | + $prim = $boucle->primary; |
|
| 428 | + $table = $boucle->id_table; |
|
| 429 | + |
|
| 430 | + $c = |
|
| 431 | + [ |
|
| 432 | + "'OR'", |
|
| 433 | + ["'='", "'$table." . "id_trad'", "'$table.$prim'"], |
|
| 434 | + ["'='", "'$table.id_trad'", "'0'"] |
|
| 435 | + ]; |
|
| 436 | + $boucle->where[] = ($crit->not ? ["'NOT'", $c] : $c); |
|
| 437 | 437 | } |
| 438 | 438 | |
| 439 | 439 | |
@@ -450,17 +450,17 @@ discard block |
||
| 450 | 450 | **/ |
| 451 | 451 | function critere_meme_parent_dist($idb, &$boucles, $crit) { |
| 452 | 452 | |
| 453 | - $boucle = &$boucles[$idb]; |
|
| 454 | - $arg = kwote(calculer_argument_precedent($idb, 'id_parent', $boucles)); |
|
| 455 | - $id_parent = $GLOBALS['exceptions_des_tables'][$boucle->id_table]['id_parent'] ?? 'id_parent'; |
|
| 456 | - $mparent = $boucle->id_table . '.' . $id_parent; |
|
| 457 | - |
|
| 458 | - if ($boucle->type_requete == 'rubriques' or isset($GLOBALS['exceptions_des_tables'][$boucle->id_table]['id_parent'])) { |
|
| 459 | - $boucle->where[] = ["'='", "'$mparent'", $arg]; |
|
| 460 | - } // le cas FORUMS est gere dans le plugin forum, dans la fonction critere_FORUMS_meme_parent_dist() |
|
| 461 | - else { |
|
| 462 | - return ['zbug_critere_inconnu', ['critere' => $crit->op . ' ' . $boucle->type_requete]]; |
|
| 463 | - } |
|
| 453 | + $boucle = &$boucles[$idb]; |
|
| 454 | + $arg = kwote(calculer_argument_precedent($idb, 'id_parent', $boucles)); |
|
| 455 | + $id_parent = $GLOBALS['exceptions_des_tables'][$boucle->id_table]['id_parent'] ?? 'id_parent'; |
|
| 456 | + $mparent = $boucle->id_table . '.' . $id_parent; |
|
| 457 | + |
|
| 458 | + if ($boucle->type_requete == 'rubriques' or isset($GLOBALS['exceptions_des_tables'][$boucle->id_table]['id_parent'])) { |
|
| 459 | + $boucle->where[] = ["'='", "'$mparent'", $arg]; |
|
| 460 | + } // le cas FORUMS est gere dans le plugin forum, dans la fonction critere_FORUMS_meme_parent_dist() |
|
| 461 | + else { |
|
| 462 | + return ['zbug_critere_inconnu', ['critere' => $crit->op . ' ' . $boucle->type_requete]]; |
|
| 463 | + } |
|
| 464 | 464 | } |
| 465 | 465 | |
| 466 | 466 | |
@@ -491,37 +491,37 @@ discard block |
||
| 491 | 491 | **/ |
| 492 | 492 | function critere_branche_dist($idb, &$boucles, $crit) { |
| 493 | 493 | |
| 494 | - $not = $crit->not; |
|
| 495 | - $boucle = &$boucles[$idb]; |
|
| 496 | - // prendre en priorite un identifiant en parametre {branche XX} |
|
| 497 | - if (isset($crit->param[0])) { |
|
| 498 | - $arg = calculer_liste($crit->param[0], $idb, $boucles, $boucles[$idb]->id_parent); |
|
| 499 | - // sinon on le prend chez une boucle parente |
|
| 500 | - } else { |
|
| 501 | - $arg = kwote(calculer_argument_precedent($idb, 'id_rubrique', $boucles), $boucle->sql_serveur, 'int NOT NULL'); |
|
| 502 | - } |
|
| 503 | - |
|
| 504 | - //Trouver une jointure |
|
| 505 | - $champ = 'id_rubrique'; |
|
| 506 | - $desc = $boucle->show; |
|
| 507 | - //Seulement si necessaire |
|
| 508 | - if (!array_key_exists($champ, $desc['field'])) { |
|
| 509 | - $cle = trouver_jointure_champ($champ, $boucle); |
|
| 510 | - $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 511 | - $desc = $trouver_table($boucle->from[$cle]); |
|
| 512 | - if (count(trouver_champs_decomposes($champ, $desc)) > 1) { |
|
| 513 | - $decompose = decompose_champ_id_objet($champ); |
|
| 514 | - $champ = array_shift($decompose); |
|
| 515 | - $boucle->where[] = ["'='", _q($cle . '.' . reset($decompose)), '"' . sql_quote(end($decompose)) . '"']; |
|
| 516 | - } |
|
| 517 | - } else { |
|
| 518 | - $cle = $boucle->id_table; |
|
| 519 | - } |
|
| 520 | - |
|
| 521 | - $c = "sql_in('$cle" . ".$champ', calcul_branche_in($arg)" |
|
| 522 | - . ($not ? ", 'NOT'" : '') . ')'; |
|
| 523 | - $boucle->where[] = !$crit->cond ? $c : |
|
| 524 | - ("($arg ? $c : " . ($not ? "'0=1'" : "'1=1'") . ')'); |
|
| 494 | + $not = $crit->not; |
|
| 495 | + $boucle = &$boucles[$idb]; |
|
| 496 | + // prendre en priorite un identifiant en parametre {branche XX} |
|
| 497 | + if (isset($crit->param[0])) { |
|
| 498 | + $arg = calculer_liste($crit->param[0], $idb, $boucles, $boucles[$idb]->id_parent); |
|
| 499 | + // sinon on le prend chez une boucle parente |
|
| 500 | + } else { |
|
| 501 | + $arg = kwote(calculer_argument_precedent($idb, 'id_rubrique', $boucles), $boucle->sql_serveur, 'int NOT NULL'); |
|
| 502 | + } |
|
| 503 | + |
|
| 504 | + //Trouver une jointure |
|
| 505 | + $champ = 'id_rubrique'; |
|
| 506 | + $desc = $boucle->show; |
|
| 507 | + //Seulement si necessaire |
|
| 508 | + if (!array_key_exists($champ, $desc['field'])) { |
|
| 509 | + $cle = trouver_jointure_champ($champ, $boucle); |
|
| 510 | + $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 511 | + $desc = $trouver_table($boucle->from[$cle]); |
|
| 512 | + if (count(trouver_champs_decomposes($champ, $desc)) > 1) { |
|
| 513 | + $decompose = decompose_champ_id_objet($champ); |
|
| 514 | + $champ = array_shift($decompose); |
|
| 515 | + $boucle->where[] = ["'='", _q($cle . '.' . reset($decompose)), '"' . sql_quote(end($decompose)) . '"']; |
|
| 516 | + } |
|
| 517 | + } else { |
|
| 518 | + $cle = $boucle->id_table; |
|
| 519 | + } |
|
| 520 | + |
|
| 521 | + $c = "sql_in('$cle" . ".$champ', calcul_branche_in($arg)" |
|
| 522 | + . ($not ? ", 'NOT'" : '') . ')'; |
|
| 523 | + $boucle->where[] = !$crit->cond ? $c : |
|
| 524 | + ("($arg ? $c : " . ($not ? "'0=1'" : "'1=1'") . ')'); |
|
| 525 | 525 | } |
| 526 | 526 | |
| 527 | 527 | /** |
@@ -537,15 +537,15 @@ discard block |
||
| 537 | 537 | **/ |
| 538 | 538 | function critere_logo_dist($idb, &$boucles, $crit) { |
| 539 | 539 | |
| 540 | - $boucle = &$boucles[$idb]; |
|
| 541 | - $not = ($crit->not ? 'NOT' : ''); |
|
| 542 | - $serveur = $boucle->sql_serveur; |
|
| 540 | + $boucle = &$boucles[$idb]; |
|
| 541 | + $not = ($crit->not ? 'NOT' : ''); |
|
| 542 | + $serveur = $boucle->sql_serveur; |
|
| 543 | 543 | |
| 544 | - $c = "sql_in('" . |
|
| 545 | - $boucle->id_table . '.' . $boucle->primary |
|
| 546 | - . "', lister_objets_avec_logos('" . $boucle->primary . "'), '$not', '$serveur')"; |
|
| 544 | + $c = "sql_in('" . |
|
| 545 | + $boucle->id_table . '.' . $boucle->primary |
|
| 546 | + . "', lister_objets_avec_logos('" . $boucle->primary . "'), '$not', '$serveur')"; |
|
| 547 | 547 | |
| 548 | - $boucle->where[] = $c; |
|
| 548 | + $boucle->where[] = $c; |
|
| 549 | 549 | } |
| 550 | 550 | |
| 551 | 551 | |
@@ -567,31 +567,31 @@ discard block |
||
| 567 | 567 | * @return void|array |
| 568 | 568 | **/ |
| 569 | 569 | function critere_fusion_dist($idb, &$boucles, $crit) { |
| 570 | - if ($t = isset($crit->param[0])) { |
|
| 571 | - $t = $crit->param[0]; |
|
| 572 | - if ($t[0]->type == 'texte') { |
|
| 573 | - $t = $t[0]->texte; |
|
| 574 | - if (preg_match('/^(.*)\.(.*)$/', $t, $r)) { |
|
| 575 | - $t = table_objet_sql($r[1]); |
|
| 576 | - $t = array_search($t, $boucles[$idb]->from); |
|
| 577 | - if ($t) { |
|
| 578 | - $t .= '.' . $r[2]; |
|
| 579 | - } |
|
| 580 | - } |
|
| 581 | - } else { |
|
| 582 | - $t = '".' |
|
| 583 | - . calculer_critere_arg_dynamique($idb, $boucles, $t) |
|
| 584 | - . '."'; |
|
| 585 | - } |
|
| 586 | - } |
|
| 587 | - if ($t) { |
|
| 588 | - $boucles[$idb]->group[] = $t; |
|
| 589 | - if (!in_array($t, $boucles[$idb]->select)) { |
|
| 590 | - $boucles[$idb]->select[] = $t; |
|
| 591 | - } |
|
| 592 | - } else { |
|
| 593 | - return ['zbug_critere_inconnu', ['critere' => $crit->op . ' ?']]; |
|
| 594 | - } |
|
| 570 | + if ($t = isset($crit->param[0])) { |
|
| 571 | + $t = $crit->param[0]; |
|
| 572 | + if ($t[0]->type == 'texte') { |
|
| 573 | + $t = $t[0]->texte; |
|
| 574 | + if (preg_match('/^(.*)\.(.*)$/', $t, $r)) { |
|
| 575 | + $t = table_objet_sql($r[1]); |
|
| 576 | + $t = array_search($t, $boucles[$idb]->from); |
|
| 577 | + if ($t) { |
|
| 578 | + $t .= '.' . $r[2]; |
|
| 579 | + } |
|
| 580 | + } |
|
| 581 | + } else { |
|
| 582 | + $t = '".' |
|
| 583 | + . calculer_critere_arg_dynamique($idb, $boucles, $t) |
|
| 584 | + . '."'; |
|
| 585 | + } |
|
| 586 | + } |
|
| 587 | + if ($t) { |
|
| 588 | + $boucles[$idb]->group[] = $t; |
|
| 589 | + if (!in_array($t, $boucles[$idb]->select)) { |
|
| 590 | + $boucles[$idb]->select[] = $t; |
|
| 591 | + } |
|
| 592 | + } else { |
|
| 593 | + return ['zbug_critere_inconnu', ['critere' => $crit->op . ' ?']]; |
|
| 594 | + } |
|
| 595 | 595 | } |
| 596 | 596 | |
| 597 | 597 | /** |
@@ -611,7 +611,7 @@ discard block |
||
| 611 | 611 | * @return void |
| 612 | 612 | **/ |
| 613 | 613 | function critere_fusion_supprimer_dist($idb, &$boucles, $crit) { |
| 614 | - $boucles[$idb]->group = []; |
|
| 614 | + $boucles[$idb]->group = []; |
|
| 615 | 615 | } |
| 616 | 616 | |
| 617 | 617 | /** |
@@ -648,44 +648,44 @@ discard block |
||
| 648 | 648 | * @param Critere $crit Paramètres du critère dans cette boucle |
| 649 | 649 | */ |
| 650 | 650 | function critere_collecte_dist($idb, &$boucles, $crit) { |
| 651 | - if (isset($crit->param[0])) { |
|
| 652 | - $_coll = calculer_liste($crit->param[0], $idb, $boucles, $boucles[$idb]->id_parent); |
|
| 653 | - $boucle = $boucles[$idb]; |
|
| 654 | - $boucle->modificateur['collate'] = "($_coll ?' COLLATE '.$_coll:'')"; |
|
| 655 | - $n = is_countable($boucle->order) ? count($boucle->order) : 0; |
|
| 656 | - if ($n && (strpos($boucle->order[$n - 1], 'COLLATE') === false)) { |
|
| 657 | - // l'instruction COLLATE doit être placée avant ASC ou DESC |
|
| 658 | - // notamment lors de l'utilisation `{!par xxx}{collate yyy}` |
|
| 659 | - if ( |
|
| 660 | - (false !== $i = strpos($boucle->order[$n - 1], 'ASC')) |
|
| 661 | - or (false !== $i = strpos($boucle->order[$n - 1], 'DESC')) |
|
| 662 | - ) { |
|
| 663 | - $boucle->order[$n - 1] = substr_replace($boucle->order[$n - 1], "' . " . $boucle->modificateur['collate'] . " . ' ", $i, 0); |
|
| 664 | - } else { |
|
| 665 | - $boucle->order[$n - 1] .= ' . ' . $boucle->modificateur['collate']; |
|
| 666 | - } |
|
| 667 | - } |
|
| 668 | - } else { |
|
| 669 | - return (['zbug_critere_inconnu', ['critere' => $crit->op . ' ' . (is_countable($boucles[$idb]->order) ? count($boucles[$idb]->order) : 0)]]); |
|
| 670 | - } |
|
| 651 | + if (isset($crit->param[0])) { |
|
| 652 | + $_coll = calculer_liste($crit->param[0], $idb, $boucles, $boucles[$idb]->id_parent); |
|
| 653 | + $boucle = $boucles[$idb]; |
|
| 654 | + $boucle->modificateur['collate'] = "($_coll ?' COLLATE '.$_coll:'')"; |
|
| 655 | + $n = is_countable($boucle->order) ? count($boucle->order) : 0; |
|
| 656 | + if ($n && (strpos($boucle->order[$n - 1], 'COLLATE') === false)) { |
|
| 657 | + // l'instruction COLLATE doit être placée avant ASC ou DESC |
|
| 658 | + // notamment lors de l'utilisation `{!par xxx}{collate yyy}` |
|
| 659 | + if ( |
|
| 660 | + (false !== $i = strpos($boucle->order[$n - 1], 'ASC')) |
|
| 661 | + or (false !== $i = strpos($boucle->order[$n - 1], 'DESC')) |
|
| 662 | + ) { |
|
| 663 | + $boucle->order[$n - 1] = substr_replace($boucle->order[$n - 1], "' . " . $boucle->modificateur['collate'] . " . ' ", $i, 0); |
|
| 664 | + } else { |
|
| 665 | + $boucle->order[$n - 1] .= ' . ' . $boucle->modificateur['collate']; |
|
| 666 | + } |
|
| 667 | + } |
|
| 668 | + } else { |
|
| 669 | + return (['zbug_critere_inconnu', ['critere' => $crit->op . ' ' . (is_countable($boucles[$idb]->order) ? count($boucles[$idb]->order) : 0)]]); |
|
| 670 | + } |
|
| 671 | 671 | } |
| 672 | 672 | |
| 673 | 673 | function calculer_critere_arg_dynamique($idb, &$boucles, $crit, $suffix = '') { |
| 674 | - $boucle = $boucles[$idb]; |
|
| 675 | - $alt = "('" . $boucle->id_table . '.\' . $x' . $suffix . ')'; |
|
| 676 | - $var = '$champs_' . $idb; |
|
| 677 | - $desc = (strpos($boucle->in, (string) "static $var =") !== false); |
|
| 678 | - if (!$desc) { |
|
| 679 | - $desc = $boucle->show['field']; |
|
| 680 | - $desc = implode(',', array_map('_q', array_keys($desc))); |
|
| 681 | - $boucles[$idb]->in .= "\n\tstatic $var = array(" . $desc . ');'; |
|
| 682 | - } |
|
| 683 | - if ($desc) { |
|
| 684 | - $alt = "(in_array(\$x, $var) ? $alt :(\$x$suffix))"; |
|
| 685 | - } |
|
| 686 | - $arg = calculer_liste($crit, $idb, $boucles, $boucle->id_parent); |
|
| 687 | - |
|
| 688 | - return "((\$x = preg_replace(\"/\\W/\",'', $arg)) ? $alt : '')"; |
|
| 674 | + $boucle = $boucles[$idb]; |
|
| 675 | + $alt = "('" . $boucle->id_table . '.\' . $x' . $suffix . ')'; |
|
| 676 | + $var = '$champs_' . $idb; |
|
| 677 | + $desc = (strpos($boucle->in, (string) "static $var =") !== false); |
|
| 678 | + if (!$desc) { |
|
| 679 | + $desc = $boucle->show['field']; |
|
| 680 | + $desc = implode(',', array_map('_q', array_keys($desc))); |
|
| 681 | + $boucles[$idb]->in .= "\n\tstatic $var = array(" . $desc . ');'; |
|
| 682 | + } |
|
| 683 | + if ($desc) { |
|
| 684 | + $alt = "(in_array(\$x, $var) ? $alt :(\$x$suffix))"; |
|
| 685 | + } |
|
| 686 | + $arg = calculer_liste($crit, $idb, $boucles, $boucle->id_parent); |
|
| 687 | + |
|
| 688 | + return "((\$x = preg_replace(\"/\\W/\",'', $arg)) ? $alt : '')"; |
|
| 689 | 689 | } |
| 690 | 690 | |
| 691 | 691 | /** |
@@ -724,7 +724,7 @@ discard block |
||
| 724 | 724 | * @param Critere $crit Paramètres du critère dans cette boucle |
| 725 | 725 | */ |
| 726 | 726 | function critere_par_dist($idb, &$boucles, $crit) { |
| 727 | - return critere_parinverse($idb, $boucles, $crit); |
|
| 727 | + return critere_parinverse($idb, $boucles, $crit); |
|
| 728 | 728 | } |
| 729 | 729 | |
| 730 | 730 | /** |
@@ -746,93 +746,93 @@ discard block |
||
| 746 | 746 | * @param Critere $crit Paramètres du critère dans cette boucle |
| 747 | 747 | */ |
| 748 | 748 | function critere_parinverse($idb, &$boucles, $crit) { |
| 749 | - $boucle = &$boucles[$idb]; |
|
| 750 | - |
|
| 751 | - $sens = $collecte = ''; |
|
| 752 | - if ($crit->not) { |
|
| 753 | - $sens = " . ' DESC'"; |
|
| 754 | - } |
|
| 755 | - if (isset($boucle->modificateur['collate'])) { |
|
| 756 | - $collecte = ' . ' . $boucle->modificateur['collate']; |
|
| 757 | - } |
|
| 758 | - |
|
| 759 | - // Pour chaque paramètre du critère |
|
| 760 | - foreach ($crit->param as $tri) { |
|
| 761 | - $order = $fct = ''; |
|
| 762 | - // tris specifiés dynamiquement {par #ENV{tri}} |
|
| 763 | - if ($tri[0]->type != 'texte') { |
|
| 764 | - // calculer le order dynamique qui verifie les champs |
|
| 765 | - $order = calculer_critere_arg_dynamique($idb, $boucles, $tri, $sens); |
|
| 766 | - // ajouter 'hasard' comme possibilité de tri dynamique |
|
| 767 | - calculer_critere_par_hasard($idb, $boucles, $crit); |
|
| 768 | - } |
|
| 769 | - // tris textuels {par titre} |
|
| 770 | - else { |
|
| 771 | - $par = array_shift($tri); |
|
| 772 | - $par = $par->texte; |
|
| 773 | - |
|
| 774 | - // tris de la forme {par expression champ} tel que {par num titre} ou {par multi titre} |
|
| 775 | - if (preg_match(',^(\w+)[\s]+(.*)$,', $par, $m)) { |
|
| 776 | - $expression = trim($m[1]); |
|
| 777 | - $champ = trim($m[2]); |
|
| 778 | - if (function_exists($f = 'calculer_critere_par_expression_' . $expression)) { |
|
| 779 | - $order = $f($idb, $boucles, $crit, $tri, $champ); |
|
| 780 | - } else { |
|
| 781 | - return ['zbug_critere_inconnu', ['critere' => $crit->op . " $par"]]; |
|
| 782 | - } |
|
| 783 | - |
|
| 784 | - // tris de la forme {par champ} ou {par FONCTION(champ)} |
|
| 785 | - } elseif ($boucle->type_requete == 'DATA' or preg_match(',^' . CHAMP_SQL_PLUS_FONC . '$,is', $par, $match)) { |
|
| 786 | - // {par FONCTION(champ)} |
|
| 787 | - if (isset($match) and count($match) > 2) { |
|
| 788 | - $par = substr($match[2], 1, -1); |
|
| 789 | - $fct = $match[1]; |
|
| 790 | - } |
|
| 791 | - // quelques cas spécifiques {par hasard}, {par date} |
|
| 792 | - if ($par == 'hasard') { |
|
| 793 | - $order = calculer_critere_par_hasard($idb, $boucles, $crit); |
|
| 794 | - } elseif ($par == 'date' and !empty($boucle->show['date'])) { |
|
| 795 | - $order = "'" . $boucle->id_table . '.' . $boucle->show['date'] . "'"; |
|
| 796 | - } else { |
|
| 797 | - // cas général {par champ}, {par table.champ}, ... |
|
| 798 | - $order = calculer_critere_par_champ($idb, $boucles, $crit, $par); |
|
| 799 | - } |
|
| 800 | - } |
|
| 801 | - |
|
| 802 | - // on ne sait pas traiter… |
|
| 803 | - else { |
|
| 804 | - return ['zbug_critere_inconnu', ['critere' => $crit->op . " $par"]]; |
|
| 805 | - } |
|
| 806 | - |
|
| 807 | - // En cas d'erreur de squelette retournée par une fonction |
|
| 808 | - if (is_array($order)) { |
|
| 809 | - return $order; |
|
| 810 | - } |
|
| 811 | - } |
|
| 812 | - |
|
| 813 | - if (preg_match('/^\'([^"]*)\'$/', $order, $m)) { |
|
| 814 | - $t = $m[1]; |
|
| 815 | - if (strpos($t, '.') and !in_array($t, $boucle->select)) { |
|
| 816 | - $boucle->select[] = $t; |
|
| 817 | - } |
|
| 818 | - } else { |
|
| 819 | - $sens = ''; |
|
| 820 | - } |
|
| 821 | - |
|
| 822 | - if ($fct) { |
|
| 823 | - if (preg_match("/^\s*'(.*)'\s*$/", $order, $r)) { |
|
| 824 | - $order = "'$fct(" . $r[1] . ")'"; |
|
| 825 | - } else { |
|
| 826 | - $order = "'$fct(' . $order . ')'"; |
|
| 827 | - } |
|
| 828 | - } |
|
| 829 | - $t = $order . $collecte . $sens; |
|
| 830 | - if (preg_match("/^(.*)'\s*\.\s*'([^']*')$/", $t, $r)) { |
|
| 831 | - $t = $r[1] . $r[2]; |
|
| 832 | - } |
|
| 833 | - |
|
| 834 | - $boucle->order[] = $t; |
|
| 835 | - } |
|
| 749 | + $boucle = &$boucles[$idb]; |
|
| 750 | + |
|
| 751 | + $sens = $collecte = ''; |
|
| 752 | + if ($crit->not) { |
|
| 753 | + $sens = " . ' DESC'"; |
|
| 754 | + } |
|
| 755 | + if (isset($boucle->modificateur['collate'])) { |
|
| 756 | + $collecte = ' . ' . $boucle->modificateur['collate']; |
|
| 757 | + } |
|
| 758 | + |
|
| 759 | + // Pour chaque paramètre du critère |
|
| 760 | + foreach ($crit->param as $tri) { |
|
| 761 | + $order = $fct = ''; |
|
| 762 | + // tris specifiés dynamiquement {par #ENV{tri}} |
|
| 763 | + if ($tri[0]->type != 'texte') { |
|
| 764 | + // calculer le order dynamique qui verifie les champs |
|
| 765 | + $order = calculer_critere_arg_dynamique($idb, $boucles, $tri, $sens); |
|
| 766 | + // ajouter 'hasard' comme possibilité de tri dynamique |
|
| 767 | + calculer_critere_par_hasard($idb, $boucles, $crit); |
|
| 768 | + } |
|
| 769 | + // tris textuels {par titre} |
|
| 770 | + else { |
|
| 771 | + $par = array_shift($tri); |
|
| 772 | + $par = $par->texte; |
|
| 773 | + |
|
| 774 | + // tris de la forme {par expression champ} tel que {par num titre} ou {par multi titre} |
|
| 775 | + if (preg_match(',^(\w+)[\s]+(.*)$,', $par, $m)) { |
|
| 776 | + $expression = trim($m[1]); |
|
| 777 | + $champ = trim($m[2]); |
|
| 778 | + if (function_exists($f = 'calculer_critere_par_expression_' . $expression)) { |
|
| 779 | + $order = $f($idb, $boucles, $crit, $tri, $champ); |
|
| 780 | + } else { |
|
| 781 | + return ['zbug_critere_inconnu', ['critere' => $crit->op . " $par"]]; |
|
| 782 | + } |
|
| 783 | + |
|
| 784 | + // tris de la forme {par champ} ou {par FONCTION(champ)} |
|
| 785 | + } elseif ($boucle->type_requete == 'DATA' or preg_match(',^' . CHAMP_SQL_PLUS_FONC . '$,is', $par, $match)) { |
|
| 786 | + // {par FONCTION(champ)} |
|
| 787 | + if (isset($match) and count($match) > 2) { |
|
| 788 | + $par = substr($match[2], 1, -1); |
|
| 789 | + $fct = $match[1]; |
|
| 790 | + } |
|
| 791 | + // quelques cas spécifiques {par hasard}, {par date} |
|
| 792 | + if ($par == 'hasard') { |
|
| 793 | + $order = calculer_critere_par_hasard($idb, $boucles, $crit); |
|
| 794 | + } elseif ($par == 'date' and !empty($boucle->show['date'])) { |
|
| 795 | + $order = "'" . $boucle->id_table . '.' . $boucle->show['date'] . "'"; |
|
| 796 | + } else { |
|
| 797 | + // cas général {par champ}, {par table.champ}, ... |
|
| 798 | + $order = calculer_critere_par_champ($idb, $boucles, $crit, $par); |
|
| 799 | + } |
|
| 800 | + } |
|
| 801 | + |
|
| 802 | + // on ne sait pas traiter… |
|
| 803 | + else { |
|
| 804 | + return ['zbug_critere_inconnu', ['critere' => $crit->op . " $par"]]; |
|
| 805 | + } |
|
| 806 | + |
|
| 807 | + // En cas d'erreur de squelette retournée par une fonction |
|
| 808 | + if (is_array($order)) { |
|
| 809 | + return $order; |
|
| 810 | + } |
|
| 811 | + } |
|
| 812 | + |
|
| 813 | + if (preg_match('/^\'([^"]*)\'$/', $order, $m)) { |
|
| 814 | + $t = $m[1]; |
|
| 815 | + if (strpos($t, '.') and !in_array($t, $boucle->select)) { |
|
| 816 | + $boucle->select[] = $t; |
|
| 817 | + } |
|
| 818 | + } else { |
|
| 819 | + $sens = ''; |
|
| 820 | + } |
|
| 821 | + |
|
| 822 | + if ($fct) { |
|
| 823 | + if (preg_match("/^\s*'(.*)'\s*$/", $order, $r)) { |
|
| 824 | + $order = "'$fct(" . $r[1] . ")'"; |
|
| 825 | + } else { |
|
| 826 | + $order = "'$fct(' . $order . ')'"; |
|
| 827 | + } |
|
| 828 | + } |
|
| 829 | + $t = $order . $collecte . $sens; |
|
| 830 | + if (preg_match("/^(.*)'\s*\.\s*'([^']*')$/", $t, $r)) { |
|
| 831 | + $t = $r[1] . $r[2]; |
|
| 832 | + } |
|
| 833 | + |
|
| 834 | + $boucle->order[] = $t; |
|
| 835 | + } |
|
| 836 | 836 | } |
| 837 | 837 | |
| 838 | 838 | /** |
@@ -846,13 +846,13 @@ discard block |
||
| 846 | 846 | * @return string Clause pour le Order by |
| 847 | 847 | */ |
| 848 | 848 | function calculer_critere_par_hasard($idb, &$boucles, $crit) { |
| 849 | - $boucle = &$boucles[$idb]; |
|
| 850 | - // Si ce n'est fait, ajouter un champ 'hasard' dans le select |
|
| 851 | - $parha = 'rand() AS hasard'; |
|
| 852 | - if (!in_array($parha, $boucle->select)) { |
|
| 853 | - $boucle->select[] = $parha; |
|
| 854 | - } |
|
| 855 | - return "'hasard'"; |
|
| 849 | + $boucle = &$boucles[$idb]; |
|
| 850 | + // Si ce n'est fait, ajouter un champ 'hasard' dans le select |
|
| 851 | + $parha = 'rand() AS hasard'; |
|
| 852 | + if (!in_array($parha, $boucle->select)) { |
|
| 853 | + $boucle->select[] = $parha; |
|
| 854 | + } |
|
| 855 | + return "'hasard'"; |
|
| 856 | 856 | } |
| 857 | 857 | |
| 858 | 858 | /** |
@@ -876,24 +876,24 @@ discard block |
||
| 876 | 876 | * @return string|array Clause pour le Order by (array si erreur) |
| 877 | 877 | */ |
| 878 | 878 | function calculer_critere_par_expression_num($idb, &$boucles, $crit, $tri, $champ) { |
| 879 | - $_champ = calculer_critere_par_champ($idb, $boucles, $crit, $champ, true); |
|
| 880 | - if (is_array($_champ)) { |
|
| 881 | - return ['zbug_critere_inconnu', ['critere' => $crit->op . " num $champ"]]; |
|
| 882 | - } |
|
| 883 | - $boucle = &$boucles[$idb]; |
|
| 884 | - $texte = '0+' . $_champ; |
|
| 885 | - $suite = calculer_liste($tri, $idb, $boucles, $boucle->id_parent); |
|
| 886 | - if ($suite !== "''") { |
|
| 887 | - $texte = "\" . ((\$x = $suite) ? ('$texte' . \$x) : '0')" . ' . "'; |
|
| 888 | - } |
|
| 889 | - $asnum = 'num' . ($boucle->order ? count($boucle->order) : ''); |
|
| 890 | - $boucle->select[] = $texte . " AS $asnum"; |
|
| 891 | - |
|
| 892 | - $orderassinum = calculer_critere_par_expression_sinum($idb, $boucles, $crit, $tri, $champ); |
|
| 893 | - $orderassinum = trim($orderassinum, "'"); |
|
| 894 | - |
|
| 895 | - $order = "'$orderassinum, $asnum'"; |
|
| 896 | - return $order; |
|
| 879 | + $_champ = calculer_critere_par_champ($idb, $boucles, $crit, $champ, true); |
|
| 880 | + if (is_array($_champ)) { |
|
| 881 | + return ['zbug_critere_inconnu', ['critere' => $crit->op . " num $champ"]]; |
|
| 882 | + } |
|
| 883 | + $boucle = &$boucles[$idb]; |
|
| 884 | + $texte = '0+' . $_champ; |
|
| 885 | + $suite = calculer_liste($tri, $idb, $boucles, $boucle->id_parent); |
|
| 886 | + if ($suite !== "''") { |
|
| 887 | + $texte = "\" . ((\$x = $suite) ? ('$texte' . \$x) : '0')" . ' . "'; |
|
| 888 | + } |
|
| 889 | + $asnum = 'num' . ($boucle->order ? count($boucle->order) : ''); |
|
| 890 | + $boucle->select[] = $texte . " AS $asnum"; |
|
| 891 | + |
|
| 892 | + $orderassinum = calculer_critere_par_expression_sinum($idb, $boucles, $crit, $tri, $champ); |
|
| 893 | + $orderassinum = trim($orderassinum, "'"); |
|
| 894 | + |
|
| 895 | + $order = "'$orderassinum, $asnum'"; |
|
| 896 | + return $order; |
|
| 897 | 897 | } |
| 898 | 898 | |
| 899 | 899 | /** |
@@ -914,35 +914,35 @@ discard block |
||
| 914 | 914 | * @return string|array Clause pour le Order by (array si erreur) |
| 915 | 915 | */ |
| 916 | 916 | function calculer_critere_par_expression_sinum($idb, &$boucles, $crit, $tri, $champ) { |
| 917 | - $_champ = calculer_critere_par_champ($idb, $boucles, $crit, $champ, true); |
|
| 918 | - if (is_array($_champ)) { |
|
| 919 | - return ['zbug_critere_inconnu', ['critere' => $crit->op . " sinum $champ"]]; |
|
| 920 | - } |
|
| 921 | - $boucle = &$boucles[$idb]; |
|
| 922 | - $texte = '0+' . $_champ; |
|
| 923 | - $suite = calculer_liste($tri, $idb, $boucles, $boucle->id_parent); |
|
| 924 | - if ($suite !== "''") { |
|
| 925 | - $texte = "\" . ((\$x = $suite) ? ('$texte' . \$x) : '0')" . ' . "'; |
|
| 926 | - } |
|
| 927 | - |
|
| 928 | - $as = false; |
|
| 929 | - $select = "CASE ( $texte ) WHEN 0 THEN 1 ELSE 0 END AS "; |
|
| 930 | - foreach ($boucle->select as $s) { |
|
| 931 | - if (strpos($s, $select) === 0) { |
|
| 932 | - $as = trim(substr($s, strlen($select))); |
|
| 933 | - if (!preg_match(',\W,', $as)) { |
|
| 934 | - break; |
|
| 935 | - } |
|
| 936 | - $as = false; |
|
| 937 | - } |
|
| 938 | - } |
|
| 939 | - |
|
| 940 | - if (!$as) { |
|
| 941 | - $as = 'sinum' . ($boucle->order ? count($boucle->order) : ''); |
|
| 942 | - $boucle->select[] = $select . $as; |
|
| 943 | - } |
|
| 944 | - $order = "'$as'"; |
|
| 945 | - return $order; |
|
| 917 | + $_champ = calculer_critere_par_champ($idb, $boucles, $crit, $champ, true); |
|
| 918 | + if (is_array($_champ)) { |
|
| 919 | + return ['zbug_critere_inconnu', ['critere' => $crit->op . " sinum $champ"]]; |
|
| 920 | + } |
|
| 921 | + $boucle = &$boucles[$idb]; |
|
| 922 | + $texte = '0+' . $_champ; |
|
| 923 | + $suite = calculer_liste($tri, $idb, $boucles, $boucle->id_parent); |
|
| 924 | + if ($suite !== "''") { |
|
| 925 | + $texte = "\" . ((\$x = $suite) ? ('$texte' . \$x) : '0')" . ' . "'; |
|
| 926 | + } |
|
| 927 | + |
|
| 928 | + $as = false; |
|
| 929 | + $select = "CASE ( $texte ) WHEN 0 THEN 1 ELSE 0 END AS "; |
|
| 930 | + foreach ($boucle->select as $s) { |
|
| 931 | + if (strpos($s, $select) === 0) { |
|
| 932 | + $as = trim(substr($s, strlen($select))); |
|
| 933 | + if (!preg_match(',\W,', $as)) { |
|
| 934 | + break; |
|
| 935 | + } |
|
| 936 | + $as = false; |
|
| 937 | + } |
|
| 938 | + } |
|
| 939 | + |
|
| 940 | + if (!$as) { |
|
| 941 | + $as = 'sinum' . ($boucle->order ? count($boucle->order) : ''); |
|
| 942 | + $boucle->select[] = $select . $as; |
|
| 943 | + } |
|
| 944 | + $order = "'$as'"; |
|
| 945 | + return $order; |
|
| 946 | 946 | } |
| 947 | 947 | |
| 948 | 948 | |
@@ -962,14 +962,14 @@ discard block |
||
| 962 | 962 | * @return string|array Clause pour le Order by (array si erreur) |
| 963 | 963 | */ |
| 964 | 964 | function calculer_critere_par_expression_multi($idb, &$boucles, $crit, $tri, $champ) { |
| 965 | - $_champ = calculer_critere_par_champ($idb, $boucles, $crit, $champ, true); |
|
| 966 | - if (is_array($_champ)) { |
|
| 967 | - return ['zbug_critere_inconnu', ['critere' => $crit->op . " multi $champ"]]; |
|
| 968 | - } |
|
| 969 | - $boucle = &$boucles[$idb]; |
|
| 970 | - $boucle->select[] = "\".sql_multi('" . $_champ . "', \$GLOBALS['spip_lang']).\""; |
|
| 971 | - $order = "'multi'"; |
|
| 972 | - return $order; |
|
| 965 | + $_champ = calculer_critere_par_champ($idb, $boucles, $crit, $champ, true); |
|
| 966 | + if (is_array($_champ)) { |
|
| 967 | + return ['zbug_critere_inconnu', ['critere' => $crit->op . " multi $champ"]]; |
|
| 968 | + } |
|
| 969 | + $boucle = &$boucles[$idb]; |
|
| 970 | + $boucle->select[] = "\".sql_multi('" . $_champ . "', \$GLOBALS['spip_lang']).\""; |
|
| 971 | + $order = "'multi'"; |
|
| 972 | + return $order; |
|
| 973 | 973 | } |
| 974 | 974 | |
| 975 | 975 | /** |
@@ -988,56 +988,56 @@ discard block |
||
| 988 | 988 | * @return array|string |
| 989 | 989 | */ |
| 990 | 990 | function calculer_critere_par_champ($idb, &$boucles, $crit, $par, $raw = false) { |
| 991 | - $boucle = &$boucles[$idb]; |
|
| 992 | - $desc = $boucle->show; |
|
| 993 | - |
|
| 994 | - // le champ existe dans la table, pas de souci (le plus commun) |
|
| 995 | - if (isset($desc['field'][$par])) { |
|
| 996 | - $par = $boucle->id_table . '.' . $par; |
|
| 997 | - } |
|
| 998 | - // le champ est peut être une jointure |
|
| 999 | - else { |
|
| 1000 | - $table = $table_alias = false; // toutes les tables de jointure possibles |
|
| 1001 | - $champ = $par; |
|
| 1002 | - |
|
| 1003 | - // le champ demandé est une exception de jointure {par titre_mot} |
|
| 1004 | - if (isset($GLOBALS['exceptions_des_jointures'][$par])) { |
|
| 1005 | - [$table, $champ] = $GLOBALS['exceptions_des_jointures'][$par]; |
|
| 1006 | - } // la table de jointure est explicitement indiquée {par truc.muche} |
|
| 1007 | - elseif (preg_match('/^([^,]*)\.(.*)$/', $par, $r)) { |
|
| 1008 | - [, $table, $champ] = $r; |
|
| 1009 | - $table_alias = $table; // c'est peut-être un alias de table {par L1.titre} |
|
| 1010 | - $table = table_objet_sql($table); |
|
| 1011 | - } |
|
| 1012 | - |
|
| 1013 | - // Si on connait la table d'arrivée, on la demande donc explicitement |
|
| 1014 | - // Sinon on cherche le champ dans les tables possibles de jointures |
|
| 1015 | - // Si la table est déjà dans le from, on la réutilise. |
|
| 1016 | - if ($infos = chercher_champ_dans_tables($champ, $boucle->from, $boucle->sql_serveur, $table)) { |
|
| 1017 | - $par = $infos['alias'] . '.' . $champ; |
|
| 1018 | - } elseif ( |
|
| 1019 | - $boucle->jointures_explicites |
|
| 1020 | - and $alias = trouver_jointure_champ($champ, $boucle, explode(' ', $boucle->jointures_explicites), false, $table) |
|
| 1021 | - ) { |
|
| 1022 | - $par = $alias . '.' . $champ; |
|
| 1023 | - } elseif ($alias = trouver_jointure_champ($champ, $boucle, $boucle->jointures, false, $table)) { |
|
| 1024 | - $par = $alias . '.' . $champ; |
|
| 1025 | - // en spécifiant directement l'alias {par L2.titre} (situation hasardeuse tout de même) |
|
| 1026 | - } elseif ( |
|
| 1027 | - $table_alias |
|
| 1028 | - and isset($boucle->from[$table_alias]) |
|
| 1029 | - and $infos = chercher_champ_dans_tables($champ, $boucle->from, $boucle->sql_serveur, $boucle->from[$table_alias]) |
|
| 1030 | - ) { |
|
| 1031 | - $par = $infos['alias'] . '.' . $champ; |
|
| 1032 | - } elseif ($table) { |
|
| 1033 | - // On avait table + champ, mais on ne les a pas trouvés |
|
| 1034 | - return ['zbug_critere_inconnu', ['critere' => $crit->op . " $par"]]; |
|
| 1035 | - } else { |
|
| 1036 | - // Sinon tant pis, ca doit etre un champ synthetise (cf points) |
|
| 1037 | - } |
|
| 1038 | - } |
|
| 1039 | - |
|
| 1040 | - return $raw ? $par : "'$par'"; |
|
| 991 | + $boucle = &$boucles[$idb]; |
|
| 992 | + $desc = $boucle->show; |
|
| 993 | + |
|
| 994 | + // le champ existe dans la table, pas de souci (le plus commun) |
|
| 995 | + if (isset($desc['field'][$par])) { |
|
| 996 | + $par = $boucle->id_table . '.' . $par; |
|
| 997 | + } |
|
| 998 | + // le champ est peut être une jointure |
|
| 999 | + else { |
|
| 1000 | + $table = $table_alias = false; // toutes les tables de jointure possibles |
|
| 1001 | + $champ = $par; |
|
| 1002 | + |
|
| 1003 | + // le champ demandé est une exception de jointure {par titre_mot} |
|
| 1004 | + if (isset($GLOBALS['exceptions_des_jointures'][$par])) { |
|
| 1005 | + [$table, $champ] = $GLOBALS['exceptions_des_jointures'][$par]; |
|
| 1006 | + } // la table de jointure est explicitement indiquée {par truc.muche} |
|
| 1007 | + elseif (preg_match('/^([^,]*)\.(.*)$/', $par, $r)) { |
|
| 1008 | + [, $table, $champ] = $r; |
|
| 1009 | + $table_alias = $table; // c'est peut-être un alias de table {par L1.titre} |
|
| 1010 | + $table = table_objet_sql($table); |
|
| 1011 | + } |
|
| 1012 | + |
|
| 1013 | + // Si on connait la table d'arrivée, on la demande donc explicitement |
|
| 1014 | + // Sinon on cherche le champ dans les tables possibles de jointures |
|
| 1015 | + // Si la table est déjà dans le from, on la réutilise. |
|
| 1016 | + if ($infos = chercher_champ_dans_tables($champ, $boucle->from, $boucle->sql_serveur, $table)) { |
|
| 1017 | + $par = $infos['alias'] . '.' . $champ; |
|
| 1018 | + } elseif ( |
|
| 1019 | + $boucle->jointures_explicites |
|
| 1020 | + and $alias = trouver_jointure_champ($champ, $boucle, explode(' ', $boucle->jointures_explicites), false, $table) |
|
| 1021 | + ) { |
|
| 1022 | + $par = $alias . '.' . $champ; |
|
| 1023 | + } elseif ($alias = trouver_jointure_champ($champ, $boucle, $boucle->jointures, false, $table)) { |
|
| 1024 | + $par = $alias . '.' . $champ; |
|
| 1025 | + // en spécifiant directement l'alias {par L2.titre} (situation hasardeuse tout de même) |
|
| 1026 | + } elseif ( |
|
| 1027 | + $table_alias |
|
| 1028 | + and isset($boucle->from[$table_alias]) |
|
| 1029 | + and $infos = chercher_champ_dans_tables($champ, $boucle->from, $boucle->sql_serveur, $boucle->from[$table_alias]) |
|
| 1030 | + ) { |
|
| 1031 | + $par = $infos['alias'] . '.' . $champ; |
|
| 1032 | + } elseif ($table) { |
|
| 1033 | + // On avait table + champ, mais on ne les a pas trouvés |
|
| 1034 | + return ['zbug_critere_inconnu', ['critere' => $crit->op . " $par"]]; |
|
| 1035 | + } else { |
|
| 1036 | + // Sinon tant pis, ca doit etre un champ synthetise (cf points) |
|
| 1037 | + } |
|
| 1038 | + } |
|
| 1039 | + |
|
| 1040 | + return $raw ? $par : "'$par'"; |
|
| 1041 | 1041 | } |
| 1042 | 1042 | |
| 1043 | 1043 | /** |
@@ -1051,11 +1051,11 @@ discard block |
||
| 1051 | 1051 | * @return string Champ pour le compilateur si trouvé, tel que "'alias.champ'", sinon vide. |
| 1052 | 1052 | */ |
| 1053 | 1053 | function critere_par_joint($table, $champ, &$boucle) { |
| 1054 | - $t = array_search($table, $boucle->from); |
|
| 1055 | - if (!$t) { |
|
| 1056 | - $t = trouver_jointure_champ($champ, $boucle); |
|
| 1057 | - } |
|
| 1058 | - return !$t ? '' : ("'" . $t . '.' . $champ . "'"); |
|
| 1054 | + $t = array_search($table, $boucle->from); |
|
| 1055 | + if (!$t) { |
|
| 1056 | + $t = trouver_jointure_champ($champ, $boucle); |
|
| 1057 | + } |
|
| 1058 | + return !$t ? '' : ("'" . $t . '.' . $champ . "'"); |
|
| 1059 | 1059 | } |
| 1060 | 1060 | |
| 1061 | 1061 | /** |
@@ -1080,33 +1080,33 @@ discard block |
||
| 1080 | 1080 | */ |
| 1081 | 1081 | function critere_inverse_dist($idb, &$boucles, $crit) { |
| 1082 | 1082 | |
| 1083 | - $boucle = &$boucles[$idb]; |
|
| 1084 | - // Classement par ordre inverse |
|
| 1085 | - if ($crit->not) { |
|
| 1086 | - critere_parinverse($idb, $boucles, $crit); |
|
| 1087 | - } else { |
|
| 1088 | - $order = "' DESC'"; |
|
| 1089 | - // Classement par ordre inverse fonction eventuelle de #ENV{...} |
|
| 1090 | - if (isset($crit->param[0])) { |
|
| 1091 | - $critere = calculer_liste($crit->param[0], $idb, $boucles, $boucles[$idb]->id_parent); |
|
| 1092 | - $order = "(($critere)?' DESC':'')"; |
|
| 1093 | - } |
|
| 1094 | - |
|
| 1095 | - $n = is_countable($boucle->order) ? count($boucle->order) : 0; |
|
| 1096 | - if (!$n) { |
|
| 1097 | - if (isset($boucle->default_order[0])) { |
|
| 1098 | - $boucle->default_order[0] .= ' . " DESC"'; |
|
| 1099 | - } else { |
|
| 1100 | - $boucle->default_order[] = ' DESC'; |
|
| 1101 | - } |
|
| 1102 | - } else { |
|
| 1103 | - $t = $boucle->order[$n - 1] . " . $order"; |
|
| 1104 | - if (preg_match("/^(.*)'\s*\.\s*'([^']*')$/", $t, $r)) { |
|
| 1105 | - $t = $r[1] . $r[2]; |
|
| 1106 | - } |
|
| 1107 | - $boucle->order[$n - 1] = $t; |
|
| 1108 | - } |
|
| 1109 | - } |
|
| 1083 | + $boucle = &$boucles[$idb]; |
|
| 1084 | + // Classement par ordre inverse |
|
| 1085 | + if ($crit->not) { |
|
| 1086 | + critere_parinverse($idb, $boucles, $crit); |
|
| 1087 | + } else { |
|
| 1088 | + $order = "' DESC'"; |
|
| 1089 | + // Classement par ordre inverse fonction eventuelle de #ENV{...} |
|
| 1090 | + if (isset($crit->param[0])) { |
|
| 1091 | + $critere = calculer_liste($crit->param[0], $idb, $boucles, $boucles[$idb]->id_parent); |
|
| 1092 | + $order = "(($critere)?' DESC':'')"; |
|
| 1093 | + } |
|
| 1094 | + |
|
| 1095 | + $n = is_countable($boucle->order) ? count($boucle->order) : 0; |
|
| 1096 | + if (!$n) { |
|
| 1097 | + if (isset($boucle->default_order[0])) { |
|
| 1098 | + $boucle->default_order[0] .= ' . " DESC"'; |
|
| 1099 | + } else { |
|
| 1100 | + $boucle->default_order[] = ' DESC'; |
|
| 1101 | + } |
|
| 1102 | + } else { |
|
| 1103 | + $t = $boucle->order[$n - 1] . " . $order"; |
|
| 1104 | + if (preg_match("/^(.*)'\s*\.\s*'([^']*')$/", $t, $r)) { |
|
| 1105 | + $t = $r[1] . $r[2]; |
|
| 1106 | + } |
|
| 1107 | + $boucle->order[$n - 1] = $t; |
|
| 1108 | + } |
|
| 1109 | + } |
|
| 1110 | 1110 | } |
| 1111 | 1111 | |
| 1112 | 1112 | /** |
@@ -1118,139 +1118,139 @@ discard block |
||
| 1118 | 1118 | * @return void|array |
| 1119 | 1119 | */ |
| 1120 | 1120 | function critere_par_ordre_liste_dist($idb, &$boucles, $crit) { |
| 1121 | - $boucle = &$boucles[$idb]; |
|
| 1121 | + $boucle = &$boucles[$idb]; |
|
| 1122 | 1122 | |
| 1123 | - $sens = $collecte = ''; |
|
| 1124 | - if ($crit->not) { |
|
| 1125 | - $sens = " . ' DESC'"; |
|
| 1126 | - } |
|
| 1123 | + $sens = $collecte = ''; |
|
| 1124 | + if ($crit->not) { |
|
| 1125 | + $sens = " . ' DESC'"; |
|
| 1126 | + } |
|
| 1127 | 1127 | |
| 1128 | - $crit2 = clone $crit; |
|
| 1129 | - $crit2->not = false; |
|
| 1130 | - $crit2->param = [reset($crit->param)]; |
|
| 1131 | - $res = critere_parinverse($idb, $boucles, $crit2); |
|
| 1128 | + $crit2 = clone $crit; |
|
| 1129 | + $crit2->not = false; |
|
| 1130 | + $crit2->param = [reset($crit->param)]; |
|
| 1131 | + $res = critere_parinverse($idb, $boucles, $crit2); |
|
| 1132 | 1132 | |
| 1133 | - // erreur ? |
|
| 1134 | - if (is_array($res)) { |
|
| 1135 | - return $res; |
|
| 1136 | - } |
|
| 1133 | + // erreur ? |
|
| 1134 | + if (is_array($res)) { |
|
| 1135 | + return $res; |
|
| 1136 | + } |
|
| 1137 | 1137 | |
| 1138 | - $_order = array_pop($boucle->order); |
|
| 1138 | + $_order = array_pop($boucle->order); |
|
| 1139 | 1139 | |
| 1140 | - $_liste = calculer_liste($crit->param[1], [], $boucles, $boucles[$idb]->id_parent); |
|
| 1140 | + $_liste = calculer_liste($crit->param[1], [], $boucles, $boucles[$idb]->id_parent); |
|
| 1141 | 1141 | |
| 1142 | - $order = "'-FIELD(' . $_order . ',' . ((\$zl=formate_liste_critere_par_ordre_liste(array_reverse($_liste),'" . $boucle->sql_serveur . "')) ? \$zl : '0').')'$sens"; |
|
| 1143 | - $boucle->order[] = $order; |
|
| 1142 | + $order = "'-FIELD(' . $_order . ',' . ((\$zl=formate_liste_critere_par_ordre_liste(array_reverse($_liste),'" . $boucle->sql_serveur . "')) ? \$zl : '0').')'$sens"; |
|
| 1143 | + $boucle->order[] = $order; |
|
| 1144 | 1144 | } |
| 1145 | 1145 | |
| 1146 | 1146 | |
| 1147 | 1147 | function critere_agenda_dist($idb, &$boucles, $crit) { |
| 1148 | - $params = $crit->param; |
|
| 1149 | - |
|
| 1150 | - if ((is_countable($params) ? count($params) : 0) < 1) { |
|
| 1151 | - return ['zbug_critere_inconnu', ['critere' => $crit->op . ' ?']]; |
|
| 1152 | - } |
|
| 1153 | - |
|
| 1154 | - $boucle = &$boucles[$idb]; |
|
| 1155 | - $parent = $boucle->id_parent; |
|
| 1156 | - $fields = $boucle->show['field']; |
|
| 1157 | - |
|
| 1158 | - $date = array_shift($params); |
|
| 1159 | - $type = array_shift($params); |
|
| 1160 | - |
|
| 1161 | - // la valeur $type doit etre connue a la compilation |
|
| 1162 | - // donc etre forcement reduite a un litteral unique dans le source |
|
| 1163 | - $type = is_object($type[0]) ? $type[0]->texte : null; |
|
| 1164 | - |
|
| 1165 | - // La valeur date doit designer un champ de la table SQL. |
|
| 1166 | - // Si c'est un litteral unique dans le source, verifier a la compil, |
|
| 1167 | - // sinon synthetiser le test de verif pour execution ulterieure |
|
| 1168 | - // On prendra arbitrairement le premier champ si test negatif. |
|
| 1169 | - if (((is_countable($date) ? count($date) : 0) == 1) and ($date[0]->type == 'texte')) { |
|
| 1170 | - $date = $date[0]->texte; |
|
| 1171 | - if (!isset($fields[$date])) { |
|
| 1172 | - return ['zbug_critere_inconnu', ['critere' => $crit->op . ' ' . $date]]; |
|
| 1173 | - } |
|
| 1174 | - } else { |
|
| 1175 | - $a = calculer_liste($date, $idb, $boucles, $parent); |
|
| 1176 | - $noms = array_keys($fields); |
|
| 1177 | - $defaut = $noms[0]; |
|
| 1178 | - $noms = join(' ', $noms); |
|
| 1179 | - # bien laisser 2 espaces avant $nom pour que strpos<>0 |
|
| 1180 | - $cond = "(\$a=strval($a))AND\nstrpos(\" $noms \",\" \$a \")"; |
|
| 1181 | - $date = "'.(($cond)\n?\$a:\"$defaut\").'"; |
|
| 1182 | - } |
|
| 1183 | - $annee = $params ? array_shift($params) : ''; |
|
| 1184 | - $annee = "\n" . 'sprintf("%04d", ($x = ' . |
|
| 1185 | - calculer_liste($annee, $idb, $boucles, $parent) . |
|
| 1186 | - ') ? $x : date("Y"))'; |
|
| 1187 | - |
|
| 1188 | - $mois = $params ? array_shift($params) : ''; |
|
| 1189 | - $mois = "\n" . 'sprintf("%02d", ($x = ' . |
|
| 1190 | - calculer_liste($mois, $idb, $boucles, $parent) . |
|
| 1191 | - ') ? $x : date("m"))'; |
|
| 1192 | - |
|
| 1193 | - $jour = $params ? array_shift($params) : ''; |
|
| 1194 | - $jour = "\n" . 'sprintf("%02d", ($x = ' . |
|
| 1195 | - calculer_liste($jour, $idb, $boucles, $parent) . |
|
| 1196 | - ') ? $x : date("d"))'; |
|
| 1197 | - |
|
| 1198 | - $annee2 = $params ? array_shift($params) : ''; |
|
| 1199 | - $annee2 = "\n" . 'sprintf("%04d", ($x = ' . |
|
| 1200 | - calculer_liste($annee2, $idb, $boucles, $parent) . |
|
| 1201 | - ') ? $x : date("Y"))'; |
|
| 1202 | - |
|
| 1203 | - $mois2 = $params ? array_shift($params) : ''; |
|
| 1204 | - $mois2 = "\n" . 'sprintf("%02d", ($x = ' . |
|
| 1205 | - calculer_liste($mois2, $idb, $boucles, $parent) . |
|
| 1206 | - ') ? $x : date("m"))'; |
|
| 1207 | - |
|
| 1208 | - $jour2 = $params ? array_shift($params) : ''; |
|
| 1209 | - $jour2 = "\n" . 'sprintf("%02d", ($x = ' . |
|
| 1210 | - calculer_liste($jour2, $idb, $boucles, $parent) . |
|
| 1211 | - ') ? $x : date("d"))'; |
|
| 1212 | - |
|
| 1213 | - $date = $boucle->id_table . ".$date"; |
|
| 1214 | - |
|
| 1215 | - $quote_end = ",'" . $boucle->sql_serveur . "','text'"; |
|
| 1216 | - if ($type == 'jour') { |
|
| 1217 | - $boucle->where[] = [ |
|
| 1218 | - "'='", |
|
| 1219 | - "'DATE_FORMAT($date, \'%Y%m%d\')'", |
|
| 1220 | - ("sql_quote($annee . $mois . $jour$quote_end)") |
|
| 1221 | - ]; |
|
| 1222 | - } elseif ($type == 'mois') { |
|
| 1223 | - $boucle->where[] = [ |
|
| 1224 | - "'='", |
|
| 1225 | - "'DATE_FORMAT($date, \'%Y%m\')'", |
|
| 1226 | - ("sql_quote($annee . $mois$quote_end)") |
|
| 1227 | - ]; |
|
| 1228 | - } elseif ($type == 'semaine') { |
|
| 1229 | - $boucle->where[] = [ |
|
| 1230 | - "'AND'", |
|
| 1231 | - [ |
|
| 1232 | - "'>='", |
|
| 1233 | - "'DATE_FORMAT($date, \'%Y%m%d\')'", |
|
| 1234 | - ("date_debut_semaine($annee, $mois, $jour)") |
|
| 1235 | - ], |
|
| 1236 | - [ |
|
| 1237 | - "'<='", |
|
| 1238 | - "'DATE_FORMAT($date, \'%Y%m%d\')'", |
|
| 1239 | - ("date_fin_semaine($annee, $mois, $jour)") |
|
| 1240 | - ] |
|
| 1241 | - ]; |
|
| 1242 | - } elseif ((is_countable($crit->param) ? count($crit->param) : 0) > 2) { |
|
| 1243 | - $boucle->where[] = [ |
|
| 1244 | - "'AND'", |
|
| 1245 | - [ |
|
| 1246 | - "'>='", |
|
| 1247 | - "'DATE_FORMAT($date, \'%Y%m%d\')'", |
|
| 1248 | - ("sql_quote($annee . $mois . $jour$quote_end)") |
|
| 1249 | - ], |
|
| 1250 | - ["'<='", "'DATE_FORMAT($date, \'%Y%m%d\')'", ("sql_quote($annee2 . $mois2 . $jour2$quote_end)")] |
|
| 1251 | - ]; |
|
| 1252 | - } |
|
| 1253 | - // sinon on prend tout |
|
| 1148 | + $params = $crit->param; |
|
| 1149 | + |
|
| 1150 | + if ((is_countable($params) ? count($params) : 0) < 1) { |
|
| 1151 | + return ['zbug_critere_inconnu', ['critere' => $crit->op . ' ?']]; |
|
| 1152 | + } |
|
| 1153 | + |
|
| 1154 | + $boucle = &$boucles[$idb]; |
|
| 1155 | + $parent = $boucle->id_parent; |
|
| 1156 | + $fields = $boucle->show['field']; |
|
| 1157 | + |
|
| 1158 | + $date = array_shift($params); |
|
| 1159 | + $type = array_shift($params); |
|
| 1160 | + |
|
| 1161 | + // la valeur $type doit etre connue a la compilation |
|
| 1162 | + // donc etre forcement reduite a un litteral unique dans le source |
|
| 1163 | + $type = is_object($type[0]) ? $type[0]->texte : null; |
|
| 1164 | + |
|
| 1165 | + // La valeur date doit designer un champ de la table SQL. |
|
| 1166 | + // Si c'est un litteral unique dans le source, verifier a la compil, |
|
| 1167 | + // sinon synthetiser le test de verif pour execution ulterieure |
|
| 1168 | + // On prendra arbitrairement le premier champ si test negatif. |
|
| 1169 | + if (((is_countable($date) ? count($date) : 0) == 1) and ($date[0]->type == 'texte')) { |
|
| 1170 | + $date = $date[0]->texte; |
|
| 1171 | + if (!isset($fields[$date])) { |
|
| 1172 | + return ['zbug_critere_inconnu', ['critere' => $crit->op . ' ' . $date]]; |
|
| 1173 | + } |
|
| 1174 | + } else { |
|
| 1175 | + $a = calculer_liste($date, $idb, $boucles, $parent); |
|
| 1176 | + $noms = array_keys($fields); |
|
| 1177 | + $defaut = $noms[0]; |
|
| 1178 | + $noms = join(' ', $noms); |
|
| 1179 | + # bien laisser 2 espaces avant $nom pour que strpos<>0 |
|
| 1180 | + $cond = "(\$a=strval($a))AND\nstrpos(\" $noms \",\" \$a \")"; |
|
| 1181 | + $date = "'.(($cond)\n?\$a:\"$defaut\").'"; |
|
| 1182 | + } |
|
| 1183 | + $annee = $params ? array_shift($params) : ''; |
|
| 1184 | + $annee = "\n" . 'sprintf("%04d", ($x = ' . |
|
| 1185 | + calculer_liste($annee, $idb, $boucles, $parent) . |
|
| 1186 | + ') ? $x : date("Y"))'; |
|
| 1187 | + |
|
| 1188 | + $mois = $params ? array_shift($params) : ''; |
|
| 1189 | + $mois = "\n" . 'sprintf("%02d", ($x = ' . |
|
| 1190 | + calculer_liste($mois, $idb, $boucles, $parent) . |
|
| 1191 | + ') ? $x : date("m"))'; |
|
| 1192 | + |
|
| 1193 | + $jour = $params ? array_shift($params) : ''; |
|
| 1194 | + $jour = "\n" . 'sprintf("%02d", ($x = ' . |
|
| 1195 | + calculer_liste($jour, $idb, $boucles, $parent) . |
|
| 1196 | + ') ? $x : date("d"))'; |
|
| 1197 | + |
|
| 1198 | + $annee2 = $params ? array_shift($params) : ''; |
|
| 1199 | + $annee2 = "\n" . 'sprintf("%04d", ($x = ' . |
|
| 1200 | + calculer_liste($annee2, $idb, $boucles, $parent) . |
|
| 1201 | + ') ? $x : date("Y"))'; |
|
| 1202 | + |
|
| 1203 | + $mois2 = $params ? array_shift($params) : ''; |
|
| 1204 | + $mois2 = "\n" . 'sprintf("%02d", ($x = ' . |
|
| 1205 | + calculer_liste($mois2, $idb, $boucles, $parent) . |
|
| 1206 | + ') ? $x : date("m"))'; |
|
| 1207 | + |
|
| 1208 | + $jour2 = $params ? array_shift($params) : ''; |
|
| 1209 | + $jour2 = "\n" . 'sprintf("%02d", ($x = ' . |
|
| 1210 | + calculer_liste($jour2, $idb, $boucles, $parent) . |
|
| 1211 | + ') ? $x : date("d"))'; |
|
| 1212 | + |
|
| 1213 | + $date = $boucle->id_table . ".$date"; |
|
| 1214 | + |
|
| 1215 | + $quote_end = ",'" . $boucle->sql_serveur . "','text'"; |
|
| 1216 | + if ($type == 'jour') { |
|
| 1217 | + $boucle->where[] = [ |
|
| 1218 | + "'='", |
|
| 1219 | + "'DATE_FORMAT($date, \'%Y%m%d\')'", |
|
| 1220 | + ("sql_quote($annee . $mois . $jour$quote_end)") |
|
| 1221 | + ]; |
|
| 1222 | + } elseif ($type == 'mois') { |
|
| 1223 | + $boucle->where[] = [ |
|
| 1224 | + "'='", |
|
| 1225 | + "'DATE_FORMAT($date, \'%Y%m\')'", |
|
| 1226 | + ("sql_quote($annee . $mois$quote_end)") |
|
| 1227 | + ]; |
|
| 1228 | + } elseif ($type == 'semaine') { |
|
| 1229 | + $boucle->where[] = [ |
|
| 1230 | + "'AND'", |
|
| 1231 | + [ |
|
| 1232 | + "'>='", |
|
| 1233 | + "'DATE_FORMAT($date, \'%Y%m%d\')'", |
|
| 1234 | + ("date_debut_semaine($annee, $mois, $jour)") |
|
| 1235 | + ], |
|
| 1236 | + [ |
|
| 1237 | + "'<='", |
|
| 1238 | + "'DATE_FORMAT($date, \'%Y%m%d\')'", |
|
| 1239 | + ("date_fin_semaine($annee, $mois, $jour)") |
|
| 1240 | + ] |
|
| 1241 | + ]; |
|
| 1242 | + } elseif ((is_countable($crit->param) ? count($crit->param) : 0) > 2) { |
|
| 1243 | + $boucle->where[] = [ |
|
| 1244 | + "'AND'", |
|
| 1245 | + [ |
|
| 1246 | + "'>='", |
|
| 1247 | + "'DATE_FORMAT($date, \'%Y%m%d\')'", |
|
| 1248 | + ("sql_quote($annee . $mois . $jour$quote_end)") |
|
| 1249 | + ], |
|
| 1250 | + ["'<='", "'DATE_FORMAT($date, \'%Y%m%d\')'", ("sql_quote($annee2 . $mois2 . $jour2$quote_end)")] |
|
| 1251 | + ]; |
|
| 1252 | + } |
|
| 1253 | + // sinon on prend tout |
|
| 1254 | 1254 | } |
| 1255 | 1255 | |
| 1256 | 1256 | |
@@ -1275,33 +1275,33 @@ discard block |
||
| 1275 | 1275 | * @return void |
| 1276 | 1276 | **/ |
| 1277 | 1277 | function calculer_critere_parties($idb, &$boucles, $crit) { |
| 1278 | - $boucle = &$boucles[$idb]; |
|
| 1279 | - $a1 = $crit->param[0]; |
|
| 1280 | - $a2 = $crit->param[1]; |
|
| 1281 | - $op = $crit->op; |
|
| 1282 | - |
|
| 1283 | - [$a11, $a12] = calculer_critere_parties_aux($idb, $boucles, $a1); |
|
| 1284 | - [$a21, $a22] = calculer_critere_parties_aux($idb, $boucles, $a2); |
|
| 1285 | - |
|
| 1286 | - if (($op == ',') && (is_numeric($a11) && (is_numeric($a21)))) { |
|
| 1287 | - $boucle->limit = $a11 . ',' . $a21; |
|
| 1288 | - } else { |
|
| 1289 | - // 3 dans {1/3}, {2,3} ou {1,n-3} |
|
| 1290 | - $boucle->total_parties = ($a21 != 'n') ? $a21 : $a22; |
|
| 1291 | - // 2 dans {2/3}, {2,5}, {n-2,1} |
|
| 1292 | - $partie = ($a11 != 'n') ? $a11 : $a12; |
|
| 1293 | - $mode = (($op == '/') ? '/' : |
|
| 1294 | - (($a11 == 'n') ? '-' : '+') . (($a21 == 'n') ? '-' : '+')); |
|
| 1295 | - // cas simple {0,#ENV{truc}} compilons le en LIMIT : |
|
| 1296 | - if ($a11 !== 'n' and $a21 !== 'n' and $mode == '++' and $op == ',') { |
|
| 1297 | - $boucle->limit = |
|
| 1298 | - (is_numeric($a11) ? "'$a11'" : $a11) |
|
| 1299 | - . ".','." |
|
| 1300 | - . (is_numeric($a21) ? "'$a21'" : $a21); |
|
| 1301 | - } else { |
|
| 1302 | - calculer_parties($boucles, $idb, $partie, $mode); |
|
| 1303 | - } |
|
| 1304 | - } |
|
| 1278 | + $boucle = &$boucles[$idb]; |
|
| 1279 | + $a1 = $crit->param[0]; |
|
| 1280 | + $a2 = $crit->param[1]; |
|
| 1281 | + $op = $crit->op; |
|
| 1282 | + |
|
| 1283 | + [$a11, $a12] = calculer_critere_parties_aux($idb, $boucles, $a1); |
|
| 1284 | + [$a21, $a22] = calculer_critere_parties_aux($idb, $boucles, $a2); |
|
| 1285 | + |
|
| 1286 | + if (($op == ',') && (is_numeric($a11) && (is_numeric($a21)))) { |
|
| 1287 | + $boucle->limit = $a11 . ',' . $a21; |
|
| 1288 | + } else { |
|
| 1289 | + // 3 dans {1/3}, {2,3} ou {1,n-3} |
|
| 1290 | + $boucle->total_parties = ($a21 != 'n') ? $a21 : $a22; |
|
| 1291 | + // 2 dans {2/3}, {2,5}, {n-2,1} |
|
| 1292 | + $partie = ($a11 != 'n') ? $a11 : $a12; |
|
| 1293 | + $mode = (($op == '/') ? '/' : |
|
| 1294 | + (($a11 == 'n') ? '-' : '+') . (($a21 == 'n') ? '-' : '+')); |
|
| 1295 | + // cas simple {0,#ENV{truc}} compilons le en LIMIT : |
|
| 1296 | + if ($a11 !== 'n' and $a21 !== 'n' and $mode == '++' and $op == ',') { |
|
| 1297 | + $boucle->limit = |
|
| 1298 | + (is_numeric($a11) ? "'$a11'" : $a11) |
|
| 1299 | + . ".','." |
|
| 1300 | + . (is_numeric($a21) ? "'$a21'" : $a21); |
|
| 1301 | + } else { |
|
| 1302 | + calculer_parties($boucles, $idb, $partie, $mode); |
|
| 1303 | + } |
|
| 1304 | + } |
|
| 1305 | 1305 | } |
| 1306 | 1306 | |
| 1307 | 1307 | /** |
@@ -1329,63 +1329,63 @@ discard block |
||
| 1329 | 1329 | * @return void |
| 1330 | 1330 | **/ |
| 1331 | 1331 | function calculer_parties(&$boucles, $id_boucle, $debut, $mode) { |
| 1332 | - $total_parties = $boucles[$id_boucle]->total_parties; |
|
| 1333 | - |
|
| 1334 | - preg_match(',([+-/p])([+-/])?,', $mode, $regs); |
|
| 1335 | - [, $op1, $op2] = array_pad($regs, 3, null); |
|
| 1336 | - $nombre_boucle = "\$Numrows['$id_boucle']['total']"; |
|
| 1337 | - // {1/3} |
|
| 1338 | - if ($op1 == '/') { |
|
| 1339 | - $pmoins1 = is_numeric($debut) ? ($debut - 1) : "($debut-1)"; |
|
| 1340 | - $totpos = is_numeric($total_parties) ? ($total_parties) : |
|
| 1341 | - "($total_parties ? $total_parties : 1)"; |
|
| 1342 | - $fin = "ceil(($nombre_boucle * $debut )/$totpos) - 1"; |
|
| 1343 | - $debut = !$pmoins1 ? 0 : "ceil(($nombre_boucle * $pmoins1)/$totpos);"; |
|
| 1344 | - } else { |
|
| 1345 | - // cas {n-1,x} |
|
| 1346 | - if ($op1 == '-') { |
|
| 1347 | - $debut = "$nombre_boucle - $debut;"; |
|
| 1348 | - } |
|
| 1349 | - |
|
| 1350 | - // cas {x,n-1} |
|
| 1351 | - if ($op2 == '-') { |
|
| 1352 | - $fin = '$debut_boucle + ' . $nombre_boucle . ' - ' |
|
| 1353 | - . (is_numeric($total_parties) ? ($total_parties + 1) : |
|
| 1354 | - ($total_parties . ' - 1')); |
|
| 1355 | - } else { |
|
| 1356 | - // {x,1} ou {pagination} |
|
| 1357 | - $fin = '$debut_boucle' |
|
| 1358 | - . (is_numeric($total_parties) ? |
|
| 1359 | - (($total_parties == 1) ? '' : (' + ' . ($total_parties - 1))) : |
|
| 1360 | - ('+' . $total_parties . ' - 1')); |
|
| 1361 | - } |
|
| 1362 | - |
|
| 1363 | - // {pagination}, gerer le debut_xx=-1 pour tout voir |
|
| 1364 | - if ($op1 == 'p') { |
|
| 1365 | - $debut .= ";\n \$debut_boucle = ((\$tout=(\$debut_boucle == -1))?0:(\$debut_boucle))"; |
|
| 1366 | - $debut .= ";\n \$debut_boucle = max(0,min(\$debut_boucle,floor(($nombre_boucle-1)/($total_parties))*($total_parties)))"; |
|
| 1367 | - $fin = "(\$tout ? $nombre_boucle : $fin)"; |
|
| 1368 | - } |
|
| 1369 | - } |
|
| 1370 | - |
|
| 1371 | - // Notes : |
|
| 1372 | - // $debut_boucle et $fin_boucle sont les indices SQL du premier |
|
| 1373 | - // et du dernier demandes dans la boucle : 0 pour le premier, |
|
| 1374 | - // n-1 pour le dernier ; donc total_boucle = 1 + debut - fin |
|
| 1375 | - // Utiliser min pour rabattre $fin_boucle sur total_boucle. |
|
| 1376 | - |
|
| 1377 | - $boucles[$id_boucle]->mode_partie = "\n\t" |
|
| 1378 | - . '$debut_boucle = ' . $debut . ";\n " |
|
| 1379 | - . "\$debut_boucle = intval(\$debut_boucle);\n " |
|
| 1380 | - . '$fin_boucle = min(' . $fin . ", \$Numrows['$id_boucle']['total'] - 1);\n " |
|
| 1381 | - . '$Numrows[\'' . $id_boucle . "']['grand_total'] = \$Numrows['$id_boucle']['total'];\n " |
|
| 1382 | - . '$Numrows[\'' . $id_boucle . '\']["total"] = max(0,$fin_boucle - $debut_boucle + 1);' |
|
| 1383 | - . "\n\tif (\$debut_boucle>0" |
|
| 1384 | - . " AND \$debut_boucle < \$Numrows['$id_boucle']['grand_total']" |
|
| 1385 | - . " AND \$iter->seek(\$debut_boucle,'continue'))" |
|
| 1386 | - . "\n\t\t\$Numrows['$id_boucle']['compteur_boucle'] = \$debut_boucle;\n\t"; |
|
| 1387 | - |
|
| 1388 | - $boucles[$id_boucle]->partie = " |
|
| 1332 | + $total_parties = $boucles[$id_boucle]->total_parties; |
|
| 1333 | + |
|
| 1334 | + preg_match(',([+-/p])([+-/])?,', $mode, $regs); |
|
| 1335 | + [, $op1, $op2] = array_pad($regs, 3, null); |
|
| 1336 | + $nombre_boucle = "\$Numrows['$id_boucle']['total']"; |
|
| 1337 | + // {1/3} |
|
| 1338 | + if ($op1 == '/') { |
|
| 1339 | + $pmoins1 = is_numeric($debut) ? ($debut - 1) : "($debut-1)"; |
|
| 1340 | + $totpos = is_numeric($total_parties) ? ($total_parties) : |
|
| 1341 | + "($total_parties ? $total_parties : 1)"; |
|
| 1342 | + $fin = "ceil(($nombre_boucle * $debut )/$totpos) - 1"; |
|
| 1343 | + $debut = !$pmoins1 ? 0 : "ceil(($nombre_boucle * $pmoins1)/$totpos);"; |
|
| 1344 | + } else { |
|
| 1345 | + // cas {n-1,x} |
|
| 1346 | + if ($op1 == '-') { |
|
| 1347 | + $debut = "$nombre_boucle - $debut;"; |
|
| 1348 | + } |
|
| 1349 | + |
|
| 1350 | + // cas {x,n-1} |
|
| 1351 | + if ($op2 == '-') { |
|
| 1352 | + $fin = '$debut_boucle + ' . $nombre_boucle . ' - ' |
|
| 1353 | + . (is_numeric($total_parties) ? ($total_parties + 1) : |
|
| 1354 | + ($total_parties . ' - 1')); |
|
| 1355 | + } else { |
|
| 1356 | + // {x,1} ou {pagination} |
|
| 1357 | + $fin = '$debut_boucle' |
|
| 1358 | + . (is_numeric($total_parties) ? |
|
| 1359 | + (($total_parties == 1) ? '' : (' + ' . ($total_parties - 1))) : |
|
| 1360 | + ('+' . $total_parties . ' - 1')); |
|
| 1361 | + } |
|
| 1362 | + |
|
| 1363 | + // {pagination}, gerer le debut_xx=-1 pour tout voir |
|
| 1364 | + if ($op1 == 'p') { |
|
| 1365 | + $debut .= ";\n \$debut_boucle = ((\$tout=(\$debut_boucle == -1))?0:(\$debut_boucle))"; |
|
| 1366 | + $debut .= ";\n \$debut_boucle = max(0,min(\$debut_boucle,floor(($nombre_boucle-1)/($total_parties))*($total_parties)))"; |
|
| 1367 | + $fin = "(\$tout ? $nombre_boucle : $fin)"; |
|
| 1368 | + } |
|
| 1369 | + } |
|
| 1370 | + |
|
| 1371 | + // Notes : |
|
| 1372 | + // $debut_boucle et $fin_boucle sont les indices SQL du premier |
|
| 1373 | + // et du dernier demandes dans la boucle : 0 pour le premier, |
|
| 1374 | + // n-1 pour le dernier ; donc total_boucle = 1 + debut - fin |
|
| 1375 | + // Utiliser min pour rabattre $fin_boucle sur total_boucle. |
|
| 1376 | + |
|
| 1377 | + $boucles[$id_boucle]->mode_partie = "\n\t" |
|
| 1378 | + . '$debut_boucle = ' . $debut . ";\n " |
|
| 1379 | + . "\$debut_boucle = intval(\$debut_boucle);\n " |
|
| 1380 | + . '$fin_boucle = min(' . $fin . ", \$Numrows['$id_boucle']['total'] - 1);\n " |
|
| 1381 | + . '$Numrows[\'' . $id_boucle . "']['grand_total'] = \$Numrows['$id_boucle']['total'];\n " |
|
| 1382 | + . '$Numrows[\'' . $id_boucle . '\']["total"] = max(0,$fin_boucle - $debut_boucle + 1);' |
|
| 1383 | + . "\n\tif (\$debut_boucle>0" |
|
| 1384 | + . " AND \$debut_boucle < \$Numrows['$id_boucle']['grand_total']" |
|
| 1385 | + . " AND \$iter->seek(\$debut_boucle,'continue'))" |
|
| 1386 | + . "\n\t\t\$Numrows['$id_boucle']['compteur_boucle'] = \$debut_boucle;\n\t"; |
|
| 1387 | + |
|
| 1388 | + $boucles[$id_boucle]->partie = " |
|
| 1389 | 1389 | if (\$Numrows['$id_boucle']['compteur_boucle'] <= \$debut_boucle) continue; |
| 1390 | 1390 | if (\$Numrows['$id_boucle']['compteur_boucle']-1 > \$fin_boucle) break;"; |
| 1391 | 1391 | } |
@@ -1402,26 +1402,26 @@ discard block |
||
| 1402 | 1402 | * @return array Valeur de l'élément (peut être une expression PHP), Nombre soustrait |
| 1403 | 1403 | **/ |
| 1404 | 1404 | function calculer_critere_parties_aux($idb, &$boucles, $param) { |
| 1405 | - if ($param[0]->type != 'texte') { |
|
| 1406 | - $a1 = calculer_liste([$param[0]], $idb, $boucles, $boucles[$idb]->id_parent); |
|
| 1407 | - if (isset($param[1]->texte)) { |
|
| 1408 | - preg_match(',^\s*(-([0-9]+))?\s*$,', $param[1]->texte, $m); |
|
| 1409 | - |
|
| 1410 | - return ["intval($a1)", ((isset($m[2]) and $m[2]) ? $m[2] : 0)]; |
|
| 1411 | - } else { |
|
| 1412 | - return ["intval($a1)", 0]; |
|
| 1413 | - } |
|
| 1414 | - } else { |
|
| 1415 | - preg_match(',^\s*(([0-9]+)|n)\s*(-\s*([0-9]+)?\s*)?$,', $param[0]->texte, $m); |
|
| 1416 | - $a1 = $m[1]; |
|
| 1417 | - if (empty($m[3])) { |
|
| 1418 | - return [$a1, 0]; |
|
| 1419 | - } elseif (!empty($m[4])) { |
|
| 1420 | - return [$a1, $m[4]]; |
|
| 1421 | - } else { |
|
| 1422 | - return [$a1, calculer_liste([$param[1]], $idb, $boucles, $boucles[$idb]->id_parent)]; |
|
| 1423 | - } |
|
| 1424 | - } |
|
| 1405 | + if ($param[0]->type != 'texte') { |
|
| 1406 | + $a1 = calculer_liste([$param[0]], $idb, $boucles, $boucles[$idb]->id_parent); |
|
| 1407 | + if (isset($param[1]->texte)) { |
|
| 1408 | + preg_match(',^\s*(-([0-9]+))?\s*$,', $param[1]->texte, $m); |
|
| 1409 | + |
|
| 1410 | + return ["intval($a1)", ((isset($m[2]) and $m[2]) ? $m[2] : 0)]; |
|
| 1411 | + } else { |
|
| 1412 | + return ["intval($a1)", 0]; |
|
| 1413 | + } |
|
| 1414 | + } else { |
|
| 1415 | + preg_match(',^\s*(([0-9]+)|n)\s*(-\s*([0-9]+)?\s*)?$,', $param[0]->texte, $m); |
|
| 1416 | + $a1 = $m[1]; |
|
| 1417 | + if (empty($m[3])) { |
|
| 1418 | + return [$a1, 0]; |
|
| 1419 | + } elseif (!empty($m[4])) { |
|
| 1420 | + return [$a1, $m[4]]; |
|
| 1421 | + } else { |
|
| 1422 | + return [$a1, calculer_liste([$param[1]], $idb, $boucles, $boucles[$idb]->id_parent)]; |
|
| 1423 | + } |
|
| 1424 | + } |
|
| 1425 | 1425 | } |
| 1426 | 1426 | |
| 1427 | 1427 | |
@@ -1448,47 +1448,47 @@ discard block |
||
| 1448 | 1448 | * array : Erreur sur un des critères |
| 1449 | 1449 | **/ |
| 1450 | 1450 | function calculer_criteres($idb, &$boucles) { |
| 1451 | - $msg = ''; |
|
| 1452 | - $boucle = $boucles[$idb]; |
|
| 1453 | - $table = strtoupper($boucle->type_requete); |
|
| 1454 | - $serveur = strtolower($boucle->sql_serveur); |
|
| 1455 | - |
|
| 1456 | - $defaut = charger_fonction('DEFAUT', 'calculer_critere'); |
|
| 1457 | - // s'il y avait une erreur de syntaxe, propager cette info |
|
| 1458 | - if (!is_array($boucle->criteres)) { |
|
| 1459 | - return []; |
|
| 1460 | - } |
|
| 1461 | - |
|
| 1462 | - foreach ($boucle->criteres as $crit) { |
|
| 1463 | - $critere = $crit->op; |
|
| 1464 | - // critere personnalise ? |
|
| 1465 | - if ( |
|
| 1466 | - (!$serveur or |
|
| 1467 | - ((!function_exists($f = 'critere_' . $serveur . '_' . $table . '_' . $critere)) |
|
| 1468 | - and (!function_exists($f = $f . '_dist')) |
|
| 1469 | - and (!function_exists($f = 'critere_' . $serveur . '_' . $critere)) |
|
| 1470 | - and (!function_exists($f = $f . '_dist')) |
|
| 1471 | - ) |
|
| 1472 | - ) |
|
| 1473 | - and (!function_exists($f = 'critere_' . $table . '_' . $critere)) |
|
| 1474 | - and (!function_exists($f = $f . '_dist')) |
|
| 1475 | - and (!function_exists($f = 'critere_' . $critere)) |
|
| 1476 | - and (!function_exists($f = $f . '_dist')) |
|
| 1477 | - ) { |
|
| 1478 | - // fonction critere standard |
|
| 1479 | - $f = $defaut; |
|
| 1480 | - } |
|
| 1481 | - // compile le critere |
|
| 1482 | - $res = $f($idb, $boucles, $crit); |
|
| 1483 | - |
|
| 1484 | - // Gestion centralisee des erreurs pour pouvoir propager |
|
| 1485 | - if (is_array($res)) { |
|
| 1486 | - $msg = $res; |
|
| 1487 | - erreur_squelette($msg, $boucle); |
|
| 1488 | - } |
|
| 1489 | - } |
|
| 1490 | - |
|
| 1491 | - return $msg; |
|
| 1451 | + $msg = ''; |
|
| 1452 | + $boucle = $boucles[$idb]; |
|
| 1453 | + $table = strtoupper($boucle->type_requete); |
|
| 1454 | + $serveur = strtolower($boucle->sql_serveur); |
|
| 1455 | + |
|
| 1456 | + $defaut = charger_fonction('DEFAUT', 'calculer_critere'); |
|
| 1457 | + // s'il y avait une erreur de syntaxe, propager cette info |
|
| 1458 | + if (!is_array($boucle->criteres)) { |
|
| 1459 | + return []; |
|
| 1460 | + } |
|
| 1461 | + |
|
| 1462 | + foreach ($boucle->criteres as $crit) { |
|
| 1463 | + $critere = $crit->op; |
|
| 1464 | + // critere personnalise ? |
|
| 1465 | + if ( |
|
| 1466 | + (!$serveur or |
|
| 1467 | + ((!function_exists($f = 'critere_' . $serveur . '_' . $table . '_' . $critere)) |
|
| 1468 | + and (!function_exists($f = $f . '_dist')) |
|
| 1469 | + and (!function_exists($f = 'critere_' . $serveur . '_' . $critere)) |
|
| 1470 | + and (!function_exists($f = $f . '_dist')) |
|
| 1471 | + ) |
|
| 1472 | + ) |
|
| 1473 | + and (!function_exists($f = 'critere_' . $table . '_' . $critere)) |
|
| 1474 | + and (!function_exists($f = $f . '_dist')) |
|
| 1475 | + and (!function_exists($f = 'critere_' . $critere)) |
|
| 1476 | + and (!function_exists($f = $f . '_dist')) |
|
| 1477 | + ) { |
|
| 1478 | + // fonction critere standard |
|
| 1479 | + $f = $defaut; |
|
| 1480 | + } |
|
| 1481 | + // compile le critere |
|
| 1482 | + $res = $f($idb, $boucles, $crit); |
|
| 1483 | + |
|
| 1484 | + // Gestion centralisee des erreurs pour pouvoir propager |
|
| 1485 | + if (is_array($res)) { |
|
| 1486 | + $msg = $res; |
|
| 1487 | + erreur_squelette($msg, $boucle); |
|
| 1488 | + } |
|
| 1489 | + } |
|
| 1490 | + |
|
| 1491 | + return $msg; |
|
| 1492 | 1492 | } |
| 1493 | 1493 | |
| 1494 | 1494 | /** |
@@ -1503,11 +1503,11 @@ discard block |
||
| 1503 | 1503 | * @return string Code compilé rééchappé |
| 1504 | 1504 | */ |
| 1505 | 1505 | function kwote($lisp, $serveur = '', $type = '') { |
| 1506 | - if (preg_match(_CODE_QUOTE, $lisp, $r)) { |
|
| 1507 | - return $r[1] . '"' . sql_quote(str_replace(["\\'", '\\\\'], ["'", '\\'], $r[2]), $serveur, $type) . '"'; |
|
| 1508 | - } else { |
|
| 1509 | - return "sql_quote($lisp, '$serveur', '" . str_replace("'", "\\'", $type) . "')"; |
|
| 1510 | - } |
|
| 1506 | + if (preg_match(_CODE_QUOTE, $lisp, $r)) { |
|
| 1507 | + return $r[1] . '"' . sql_quote(str_replace(["\\'", '\\\\'], ["'", '\\'], $r[2]), $serveur, $type) . '"'; |
|
| 1508 | + } else { |
|
| 1509 | + return "sql_quote($lisp, '$serveur', '" . str_replace("'", "\\'", $type) . "')"; |
|
| 1510 | + } |
|
| 1511 | 1511 | } |
| 1512 | 1512 | |
| 1513 | 1513 | |
@@ -1526,81 +1526,81 @@ discard block |
||
| 1526 | 1526 | * @return void|array |
| 1527 | 1527 | **/ |
| 1528 | 1528 | function critere_IN_dist($idb, &$boucles, $crit) { |
| 1529 | - $r = calculer_critere_infixe($idb, $boucles, $crit); |
|
| 1530 | - if (!$r) { |
|
| 1531 | - return ['zbug_critere_inconnu', ['critere' => $crit->op . ' ?']]; |
|
| 1532 | - } |
|
| 1533 | - [$arg, $op, $val, $col, $where_complement] = $r; |
|
| 1534 | - |
|
| 1535 | - $in = critere_IN_cas($idb, $boucles, $crit->not ? 'NOT' : ($crit->exclus ? 'exclus' : ''), $arg, $op, $val, $col); |
|
| 1536 | - |
|
| 1537 | - // inserer la condition; exemple: {id_mot ?IN (66, 62, 64)} |
|
| 1538 | - $where = $in; |
|
| 1539 | - if ($crit->cond) { |
|
| 1540 | - $pred = calculer_argument_precedent($idb, $col, $boucles); |
|
| 1541 | - $where = ["'?'", $pred, $where, "''"]; |
|
| 1542 | - if ($where_complement) { // condition annexe du type "AND (objet='article')" |
|
| 1543 | - $where_complement = ["'?'", $pred, $where_complement, "''"]; |
|
| 1544 | - } |
|
| 1545 | - } |
|
| 1546 | - if ($crit->exclus) { |
|
| 1547 | - if (!preg_match(',^L[0-9]+[.],', $arg)) { |
|
| 1548 | - $where = ["'NOT'", $where]; |
|
| 1549 | - } else // un not sur un critere de jointure se traduit comme un NOT IN avec une sous requete |
|
| 1550 | - // c'est une sous requete identique a la requete principale sous la forme (SELF,$select,$where) avec $select et $where qui surchargent |
|
| 1551 | - { |
|
| 1552 | - $where = [ |
|
| 1553 | - "'NOT'", |
|
| 1554 | - [ |
|
| 1555 | - "'IN'", |
|
| 1556 | - "'" . $boucles[$idb]->id_table . '.' . $boucles[$idb]->primary . "'", |
|
| 1557 | - ["'SELF'", "'" . $boucles[$idb]->id_table . '.' . $boucles[$idb]->primary . "'", $where] |
|
| 1558 | - ] |
|
| 1559 | - ]; |
|
| 1560 | - } |
|
| 1561 | - } |
|
| 1562 | - |
|
| 1563 | - $boucles[$idb]->where[] = $where; |
|
| 1564 | - if ($where_complement) { // condition annexe du type "AND (objet='article')" |
|
| 1565 | - $boucles[$idb]->where[] = $where_complement; |
|
| 1566 | - } |
|
| 1529 | + $r = calculer_critere_infixe($idb, $boucles, $crit); |
|
| 1530 | + if (!$r) { |
|
| 1531 | + return ['zbug_critere_inconnu', ['critere' => $crit->op . ' ?']]; |
|
| 1532 | + } |
|
| 1533 | + [$arg, $op, $val, $col, $where_complement] = $r; |
|
| 1534 | + |
|
| 1535 | + $in = critere_IN_cas($idb, $boucles, $crit->not ? 'NOT' : ($crit->exclus ? 'exclus' : ''), $arg, $op, $val, $col); |
|
| 1536 | + |
|
| 1537 | + // inserer la condition; exemple: {id_mot ?IN (66, 62, 64)} |
|
| 1538 | + $where = $in; |
|
| 1539 | + if ($crit->cond) { |
|
| 1540 | + $pred = calculer_argument_precedent($idb, $col, $boucles); |
|
| 1541 | + $where = ["'?'", $pred, $where, "''"]; |
|
| 1542 | + if ($where_complement) { // condition annexe du type "AND (objet='article')" |
|
| 1543 | + $where_complement = ["'?'", $pred, $where_complement, "''"]; |
|
| 1544 | + } |
|
| 1545 | + } |
|
| 1546 | + if ($crit->exclus) { |
|
| 1547 | + if (!preg_match(',^L[0-9]+[.],', $arg)) { |
|
| 1548 | + $where = ["'NOT'", $where]; |
|
| 1549 | + } else // un not sur un critere de jointure se traduit comme un NOT IN avec une sous requete |
|
| 1550 | + // c'est une sous requete identique a la requete principale sous la forme (SELF,$select,$where) avec $select et $where qui surchargent |
|
| 1551 | + { |
|
| 1552 | + $where = [ |
|
| 1553 | + "'NOT'", |
|
| 1554 | + [ |
|
| 1555 | + "'IN'", |
|
| 1556 | + "'" . $boucles[$idb]->id_table . '.' . $boucles[$idb]->primary . "'", |
|
| 1557 | + ["'SELF'", "'" . $boucles[$idb]->id_table . '.' . $boucles[$idb]->primary . "'", $where] |
|
| 1558 | + ] |
|
| 1559 | + ]; |
|
| 1560 | + } |
|
| 1561 | + } |
|
| 1562 | + |
|
| 1563 | + $boucles[$idb]->where[] = $where; |
|
| 1564 | + if ($where_complement) { // condition annexe du type "AND (objet='article')" |
|
| 1565 | + $boucles[$idb]->where[] = $where_complement; |
|
| 1566 | + } |
|
| 1567 | 1567 | } |
| 1568 | 1568 | |
| 1569 | 1569 | function critere_IN_cas($idb, &$boucles, $crit2, $arg, $op, $val, $col) { |
| 1570 | - static $num = []; |
|
| 1571 | - $descr = $boucles[$idb]->descr; |
|
| 1572 | - $cpt = &$num[$descr['nom']][$descr['gram']][$idb]; |
|
| 1573 | - |
|
| 1574 | - $var = '$in' . $cpt++; |
|
| 1575 | - $x = "\n\t$var = array();"; |
|
| 1576 | - foreach ($val as $k => $v) { |
|
| 1577 | - if (preg_match(",^(\n//.*\n)?'(.*)'$,", $v, $r)) { |
|
| 1578 | - // optimiser le traitement des constantes |
|
| 1579 | - if (is_numeric($r[2])) { |
|
| 1580 | - $x .= "\n\t$var" . "[]= $r[2];"; |
|
| 1581 | - } else { |
|
| 1582 | - $x .= "\n\t$var" . '[]= ' . sql_quote($r[2]) . ';'; |
|
| 1583 | - } |
|
| 1584 | - } else { |
|
| 1585 | - // Pour permettre de passer des tableaux de valeurs |
|
| 1586 | - // on repere l'utilisation brute de #ENV**{X}, |
|
| 1587 | - // c'est-a-dire sa traduction en ($PILE[0][X]). |
|
| 1588 | - // et on deballe mais en rajoutant l'anti XSS |
|
| 1589 | - $x .= "\n\tif (!(is_array(\$a = ($v))))\n\t\t$var" . "[]= \$a;\n\telse $var = array_merge($var, \$a);"; |
|
| 1590 | - } |
|
| 1591 | - } |
|
| 1592 | - |
|
| 1593 | - $boucles[$idb]->in .= $x; |
|
| 1594 | - |
|
| 1595 | - // inserer le tri par defaut selon les ordres du IN ... |
|
| 1596 | - // avec une ecriture de type FIELD qui degrade les performances (du meme ordre qu'un regexp) |
|
| 1597 | - // et que l'on limite donc strictement aux cas necessaires : |
|
| 1598 | - // si ce n'est pas un !IN, et si il n'y a pas d'autre order dans la boucle |
|
| 1599 | - if (!$crit2) { |
|
| 1600 | - $boucles[$idb]->default_order[] = "((!\$zqv=sql_quote($var) OR \$zqv===\"''\") ? 0 : ('FIELD($arg,' . \$zqv . ')'))"; |
|
| 1601 | - } |
|
| 1602 | - |
|
| 1603 | - return "sql_in('$arg', $var" . ($crit2 == 'NOT' ? ",'NOT'" : '') . ')'; |
|
| 1570 | + static $num = []; |
|
| 1571 | + $descr = $boucles[$idb]->descr; |
|
| 1572 | + $cpt = &$num[$descr['nom']][$descr['gram']][$idb]; |
|
| 1573 | + |
|
| 1574 | + $var = '$in' . $cpt++; |
|
| 1575 | + $x = "\n\t$var = array();"; |
|
| 1576 | + foreach ($val as $k => $v) { |
|
| 1577 | + if (preg_match(",^(\n//.*\n)?'(.*)'$,", $v, $r)) { |
|
| 1578 | + // optimiser le traitement des constantes |
|
| 1579 | + if (is_numeric($r[2])) { |
|
| 1580 | + $x .= "\n\t$var" . "[]= $r[2];"; |
|
| 1581 | + } else { |
|
| 1582 | + $x .= "\n\t$var" . '[]= ' . sql_quote($r[2]) . ';'; |
|
| 1583 | + } |
|
| 1584 | + } else { |
|
| 1585 | + // Pour permettre de passer des tableaux de valeurs |
|
| 1586 | + // on repere l'utilisation brute de #ENV**{X}, |
|
| 1587 | + // c'est-a-dire sa traduction en ($PILE[0][X]). |
|
| 1588 | + // et on deballe mais en rajoutant l'anti XSS |
|
| 1589 | + $x .= "\n\tif (!(is_array(\$a = ($v))))\n\t\t$var" . "[]= \$a;\n\telse $var = array_merge($var, \$a);"; |
|
| 1590 | + } |
|
| 1591 | + } |
|
| 1592 | + |
|
| 1593 | + $boucles[$idb]->in .= $x; |
|
| 1594 | + |
|
| 1595 | + // inserer le tri par defaut selon les ordres du IN ... |
|
| 1596 | + // avec une ecriture de type FIELD qui degrade les performances (du meme ordre qu'un regexp) |
|
| 1597 | + // et que l'on limite donc strictement aux cas necessaires : |
|
| 1598 | + // si ce n'est pas un !IN, et si il n'y a pas d'autre order dans la boucle |
|
| 1599 | + if (!$crit2) { |
|
| 1600 | + $boucles[$idb]->default_order[] = "((!\$zqv=sql_quote($var) OR \$zqv===\"''\") ? 0 : ('FIELD($arg,' . \$zqv . ')'))"; |
|
| 1601 | + } |
|
| 1602 | + |
|
| 1603 | + return "sql_in('$arg', $var" . ($crit2 == 'NOT' ? ",'NOT'" : '') . ')'; |
|
| 1604 | 1604 | } |
| 1605 | 1605 | |
| 1606 | 1606 | /** |
@@ -1616,22 +1616,22 @@ discard block |
||
| 1616 | 1616 | * @return void |
| 1617 | 1617 | */ |
| 1618 | 1618 | function critere_where_dist($idb, &$boucles, $crit) { |
| 1619 | - $boucle = &$boucles[$idb]; |
|
| 1620 | - if (isset($crit->param[0])) { |
|
| 1621 | - $_where = calculer_liste($crit->param[0], $idb, $boucles, $boucle->id_parent); |
|
| 1622 | - } else { |
|
| 1623 | - $_where = 'spip_sanitize_from_request(@$Pile[0]["where"],"where","vide")'; |
|
| 1624 | - } |
|
| 1625 | - |
|
| 1626 | - if ($crit->cond) { |
|
| 1627 | - $_where = "((\$zzw = $_where) ? \$zzw : '')"; |
|
| 1628 | - } |
|
| 1629 | - |
|
| 1630 | - if ($crit->not) { |
|
| 1631 | - $_where = "array('NOT',$_where)"; |
|
| 1632 | - } |
|
| 1633 | - |
|
| 1634 | - $boucle->where[] = $_where; |
|
| 1619 | + $boucle = &$boucles[$idb]; |
|
| 1620 | + if (isset($crit->param[0])) { |
|
| 1621 | + $_where = calculer_liste($crit->param[0], $idb, $boucles, $boucle->id_parent); |
|
| 1622 | + } else { |
|
| 1623 | + $_where = 'spip_sanitize_from_request(@$Pile[0]["where"],"where","vide")'; |
|
| 1624 | + } |
|
| 1625 | + |
|
| 1626 | + if ($crit->cond) { |
|
| 1627 | + $_where = "((\$zzw = $_where) ? \$zzw : '')"; |
|
| 1628 | + } |
|
| 1629 | + |
|
| 1630 | + if ($crit->not) { |
|
| 1631 | + $_where = "array('NOT',$_where)"; |
|
| 1632 | + } |
|
| 1633 | + |
|
| 1634 | + $boucle->where[] = $_where; |
|
| 1635 | 1635 | } |
| 1636 | 1636 | |
| 1637 | 1637 | /** |
@@ -1659,31 +1659,31 @@ discard block |
||
| 1659 | 1659 | * @return void |
| 1660 | 1660 | */ |
| 1661 | 1661 | function critere_id__dist($idb, &$boucles, $crit) { |
| 1662 | - /** @var Boucle $boucle */ |
|
| 1663 | - $boucle = $boucles[$idb]; |
|
| 1664 | - |
|
| 1665 | - $champs = lister_champs_id_conditionnel( |
|
| 1666 | - $boucle->show['table'], |
|
| 1667 | - $boucle->show, |
|
| 1668 | - $boucle->sql_serveur |
|
| 1669 | - ); |
|
| 1670 | - |
|
| 1671 | - // ne pas tenir compte des critères identiques déjà présents. |
|
| 1672 | - if (!empty($boucle->modificateur['criteres'])) { |
|
| 1673 | - $champs = array_diff($champs, array_keys($boucle->modificateur['criteres'])); |
|
| 1674 | - } |
|
| 1675 | - // nous aider en mode debug. |
|
| 1676 | - $boucle->debug[] = 'id_ : ' . implode(', ', $champs); |
|
| 1677 | - $boucle->modificateur['id_'] = $champs; |
|
| 1678 | - |
|
| 1679 | - // créer un critère {id_xxx?} de chaque champ retenu |
|
| 1680 | - foreach ($champs as $champ) { |
|
| 1681 | - $critere_id_table = new Critere(); |
|
| 1682 | - $critere_id_table->op = $champ; |
|
| 1683 | - $critere_id_table->cond = '?'; |
|
| 1684 | - $critere_id_table->ligne = $crit->ligne; |
|
| 1685 | - calculer_critere_DEFAUT_dist($idb, $boucles, $critere_id_table); |
|
| 1686 | - } |
|
| 1662 | + /** @var Boucle $boucle */ |
|
| 1663 | + $boucle = $boucles[$idb]; |
|
| 1664 | + |
|
| 1665 | + $champs = lister_champs_id_conditionnel( |
|
| 1666 | + $boucle->show['table'], |
|
| 1667 | + $boucle->show, |
|
| 1668 | + $boucle->sql_serveur |
|
| 1669 | + ); |
|
| 1670 | + |
|
| 1671 | + // ne pas tenir compte des critères identiques déjà présents. |
|
| 1672 | + if (!empty($boucle->modificateur['criteres'])) { |
|
| 1673 | + $champs = array_diff($champs, array_keys($boucle->modificateur['criteres'])); |
|
| 1674 | + } |
|
| 1675 | + // nous aider en mode debug. |
|
| 1676 | + $boucle->debug[] = 'id_ : ' . implode(', ', $champs); |
|
| 1677 | + $boucle->modificateur['id_'] = $champs; |
|
| 1678 | + |
|
| 1679 | + // créer un critère {id_xxx?} de chaque champ retenu |
|
| 1680 | + foreach ($champs as $champ) { |
|
| 1681 | + $critere_id_table = new Critere(); |
|
| 1682 | + $critere_id_table->op = $champ; |
|
| 1683 | + $critere_id_table->cond = '?'; |
|
| 1684 | + $critere_id_table->ligne = $crit->ligne; |
|
| 1685 | + calculer_critere_DEFAUT_dist($idb, $boucles, $critere_id_table); |
|
| 1686 | + } |
|
| 1687 | 1687 | } |
| 1688 | 1688 | |
| 1689 | 1689 | /** |
@@ -1703,75 +1703,75 @@ discard block |
||
| 1703 | 1703 | * @return array Liste de nom de champs (tel que id_article, id_mot, id_parent ...) |
| 1704 | 1704 | */ |
| 1705 | 1705 | function lister_champs_id_conditionnel($table, $desc = null, $serveur = '') { |
| 1706 | - // calculer la description de la table |
|
| 1707 | - if (!is_array($desc)) { |
|
| 1708 | - $desc = description_table($table, $serveur); |
|
| 1709 | - } |
|
| 1710 | - if (!$desc) { |
|
| 1711 | - return []; |
|
| 1712 | - } |
|
| 1713 | - |
|
| 1714 | - // Les champs id_xx de la table demandée |
|
| 1715 | - $champs = array_filter( |
|
| 1716 | - array_keys($desc['field']), |
|
| 1717 | - fn($champ) => strpos($champ, 'id_') === 0 or (in_array($champ, ['objet'])) |
|
| 1718 | - ); |
|
| 1719 | - |
|
| 1720 | - // Si le champ id_rubrique appartient à la liste et si id_secteur n'est pas inclus on le rajoute. |
|
| 1721 | - if ( |
|
| 1722 | - in_array('id_rubrique', $champs) |
|
| 1723 | - and !in_array('id_secteur', $champs) |
|
| 1724 | - ) { |
|
| 1725 | - $champs[] = 'id_secteur'; |
|
| 1726 | - } |
|
| 1727 | - |
|
| 1728 | - // On ne fera pas mieux pour les tables d’un autre serveur |
|
| 1729 | - if ($serveur) { |
|
| 1730 | - return $champs; |
|
| 1731 | - } |
|
| 1732 | - |
|
| 1733 | - $primary = false; |
|
| 1734 | - $associable = false; |
|
| 1735 | - include_spip('action/editer_liens'); |
|
| 1736 | - |
|
| 1737 | - if (isset($desc['type'])) { |
|
| 1738 | - $primary = id_table_objet($desc['type']); |
|
| 1739 | - $associable = objet_associable($desc['type']); |
|
| 1740 | - } |
|
| 1741 | - if (isset($desc['field']['id_objet']) and isset($desc['field']['objet'])) { |
|
| 1742 | - $associable = true; |
|
| 1743 | - } |
|
| 1744 | - |
|
| 1745 | - // liste de toutes les tables principales, sauf la notre |
|
| 1746 | - $tables = lister_tables_objets_sql(); |
|
| 1747 | - unset($tables[$table]); |
|
| 1748 | - |
|
| 1749 | - foreach ($tables as $_table => $_desc) { |
|
| 1750 | - if ( |
|
| 1751 | - $associable |
|
| 1752 | - or ($primary and in_array($primary, array_keys($_desc['field']))) |
|
| 1753 | - or objet_associable($_desc['type']) |
|
| 1754 | - ) { |
|
| 1755 | - $champs[] = id_table_objet($_table); |
|
| 1756 | - } |
|
| 1757 | - } |
|
| 1758 | - $champs = array_values(array_unique($champs)); |
|
| 1759 | - |
|
| 1760 | - // Exclusions de certains id |
|
| 1761 | - $exclusions = pipeline( |
|
| 1762 | - 'exclure_id_conditionnel', |
|
| 1763 | - [ |
|
| 1764 | - 'args' => [ |
|
| 1765 | - 'table' => $table, |
|
| 1766 | - 'id_table_objet' => $primary, |
|
| 1767 | - 'associable' => $associable, |
|
| 1768 | - ], |
|
| 1769 | - 'data' => [], |
|
| 1770 | - ] |
|
| 1771 | - ); |
|
| 1772 | - $champs = array_diff($champs, $exclusions); |
|
| 1773 | - |
|
| 1774 | - return $champs; |
|
| 1706 | + // calculer la description de la table |
|
| 1707 | + if (!is_array($desc)) { |
|
| 1708 | + $desc = description_table($table, $serveur); |
|
| 1709 | + } |
|
| 1710 | + if (!$desc) { |
|
| 1711 | + return []; |
|
| 1712 | + } |
|
| 1713 | + |
|
| 1714 | + // Les champs id_xx de la table demandée |
|
| 1715 | + $champs = array_filter( |
|
| 1716 | + array_keys($desc['field']), |
|
| 1717 | + fn($champ) => strpos($champ, 'id_') === 0 or (in_array($champ, ['objet'])) |
|
| 1718 | + ); |
|
| 1719 | + |
|
| 1720 | + // Si le champ id_rubrique appartient à la liste et si id_secteur n'est pas inclus on le rajoute. |
|
| 1721 | + if ( |
|
| 1722 | + in_array('id_rubrique', $champs) |
|
| 1723 | + and !in_array('id_secteur', $champs) |
|
| 1724 | + ) { |
|
| 1725 | + $champs[] = 'id_secteur'; |
|
| 1726 | + } |
|
| 1727 | + |
|
| 1728 | + // On ne fera pas mieux pour les tables d’un autre serveur |
|
| 1729 | + if ($serveur) { |
|
| 1730 | + return $champs; |
|
| 1731 | + } |
|
| 1732 | + |
|
| 1733 | + $primary = false; |
|
| 1734 | + $associable = false; |
|
| 1735 | + include_spip('action/editer_liens'); |
|
| 1736 | + |
|
| 1737 | + if (isset($desc['type'])) { |
|
| 1738 | + $primary = id_table_objet($desc['type']); |
|
| 1739 | + $associable = objet_associable($desc['type']); |
|
| 1740 | + } |
|
| 1741 | + if (isset($desc['field']['id_objet']) and isset($desc['field']['objet'])) { |
|
| 1742 | + $associable = true; |
|
| 1743 | + } |
|
| 1744 | + |
|
| 1745 | + // liste de toutes les tables principales, sauf la notre |
|
| 1746 | + $tables = lister_tables_objets_sql(); |
|
| 1747 | + unset($tables[$table]); |
|
| 1748 | + |
|
| 1749 | + foreach ($tables as $_table => $_desc) { |
|
| 1750 | + if ( |
|
| 1751 | + $associable |
|
| 1752 | + or ($primary and in_array($primary, array_keys($_desc['field']))) |
|
| 1753 | + or objet_associable($_desc['type']) |
|
| 1754 | + ) { |
|
| 1755 | + $champs[] = id_table_objet($_table); |
|
| 1756 | + } |
|
| 1757 | + } |
|
| 1758 | + $champs = array_values(array_unique($champs)); |
|
| 1759 | + |
|
| 1760 | + // Exclusions de certains id |
|
| 1761 | + $exclusions = pipeline( |
|
| 1762 | + 'exclure_id_conditionnel', |
|
| 1763 | + [ |
|
| 1764 | + 'args' => [ |
|
| 1765 | + 'table' => $table, |
|
| 1766 | + 'id_table_objet' => $primary, |
|
| 1767 | + 'associable' => $associable, |
|
| 1768 | + ], |
|
| 1769 | + 'data' => [], |
|
| 1770 | + ] |
|
| 1771 | + ); |
|
| 1772 | + $champs = array_diff($champs, $exclusions); |
|
| 1773 | + |
|
| 1774 | + return $champs; |
|
| 1775 | 1775 | } |
| 1776 | 1776 | |
| 1777 | 1777 | /** |
@@ -1826,28 +1826,28 @@ discard block |
||
| 1826 | 1826 | * @return void |
| 1827 | 1827 | */ |
| 1828 | 1828 | function critere_tri_dist($idb, &$boucles, $crit) { |
| 1829 | - $boucle = &$boucles[$idb]; |
|
| 1830 | - |
|
| 1831 | - // definition du champ par defaut |
|
| 1832 | - $_champ_defaut = !isset($crit->param[0][0]) ? "''" |
|
| 1833 | - : calculer_liste([$crit->param[0][0]], $idb, $boucles, $boucle->id_parent); |
|
| 1834 | - $_liste_sens_defaut = !isset($crit->param[1][0]) ? '1' |
|
| 1835 | - : calculer_liste([$crit->param[1][0]], $idb, $boucles, $boucle->id_parent); |
|
| 1836 | - $_variable = !isset($crit->param[2][0]) ? "'$idb'" |
|
| 1837 | - : calculer_liste([$crit->param[2][0]], $idb, $boucles, $boucle->id_parent); |
|
| 1838 | - |
|
| 1839 | - $_tri = "((\$t=(isset(\$Pile[0]['tri'.$_variable]))?\$Pile[0]['tri'.$_variable]:((strncmp($_variable,'session',7)==0 AND session_get('tri'.$_variable))?session_get('tri'.$_variable):$_champ_defaut))?tri_protege_champ(\$t):'')"; |
|
| 1840 | - |
|
| 1841 | - $_sens_defaut = "(is_array(\$s=$_liste_sens_defaut)?(isset(\$s[\$st=$_tri])?\$s[\$st]:reset(\$s)):\$s)"; |
|
| 1842 | - $_sens = "((intval(\$t=(isset(\$Pile[0]['sens'.$_variable]))?\$Pile[0]['sens'.$_variable]:((strncmp($_variable,'session',7)==0 AND session_get('sens'.$_variable))?session_get('sens'.$_variable):$_sens_defaut))==-1 OR \$t=='inverse')?-1:1)"; |
|
| 1843 | - |
|
| 1844 | - $boucle->modificateur['tri_champ'] = $_tri; |
|
| 1845 | - $boucle->modificateur['tri_sens'] = $_sens; |
|
| 1846 | - $boucle->modificateur['tri_liste_sens_defaut'] = $_liste_sens_defaut; |
|
| 1847 | - $boucle->modificateur['tri_nom'] = $_variable; |
|
| 1848 | - // faut il inserer un test sur l'existence de $tri parmi les champs de la table ? |
|
| 1849 | - // evite des erreurs sql, mais peut empecher des tri sur jointure ... |
|
| 1850 | - $boucle->hash .= " |
|
| 1829 | + $boucle = &$boucles[$idb]; |
|
| 1830 | + |
|
| 1831 | + // definition du champ par defaut |
|
| 1832 | + $_champ_defaut = !isset($crit->param[0][0]) ? "''" |
|
| 1833 | + : calculer_liste([$crit->param[0][0]], $idb, $boucles, $boucle->id_parent); |
|
| 1834 | + $_liste_sens_defaut = !isset($crit->param[1][0]) ? '1' |
|
| 1835 | + : calculer_liste([$crit->param[1][0]], $idb, $boucles, $boucle->id_parent); |
|
| 1836 | + $_variable = !isset($crit->param[2][0]) ? "'$idb'" |
|
| 1837 | + : calculer_liste([$crit->param[2][0]], $idb, $boucles, $boucle->id_parent); |
|
| 1838 | + |
|
| 1839 | + $_tri = "((\$t=(isset(\$Pile[0]['tri'.$_variable]))?\$Pile[0]['tri'.$_variable]:((strncmp($_variable,'session',7)==0 AND session_get('tri'.$_variable))?session_get('tri'.$_variable):$_champ_defaut))?tri_protege_champ(\$t):'')"; |
|
| 1840 | + |
|
| 1841 | + $_sens_defaut = "(is_array(\$s=$_liste_sens_defaut)?(isset(\$s[\$st=$_tri])?\$s[\$st]:reset(\$s)):\$s)"; |
|
| 1842 | + $_sens = "((intval(\$t=(isset(\$Pile[0]['sens'.$_variable]))?\$Pile[0]['sens'.$_variable]:((strncmp($_variable,'session',7)==0 AND session_get('sens'.$_variable))?session_get('sens'.$_variable):$_sens_defaut))==-1 OR \$t=='inverse')?-1:1)"; |
|
| 1843 | + |
|
| 1844 | + $boucle->modificateur['tri_champ'] = $_tri; |
|
| 1845 | + $boucle->modificateur['tri_sens'] = $_sens; |
|
| 1846 | + $boucle->modificateur['tri_liste_sens_defaut'] = $_liste_sens_defaut; |
|
| 1847 | + $boucle->modificateur['tri_nom'] = $_variable; |
|
| 1848 | + // faut il inserer un test sur l'existence de $tri parmi les champs de la table ? |
|
| 1849 | + // evite des erreurs sql, mais peut empecher des tri sur jointure ... |
|
| 1850 | + $boucle->hash .= " |
|
| 1851 | 1851 | \$senstri = ''; |
| 1852 | 1852 | \$tri = $_tri; |
| 1853 | 1853 | if (\$tri){ |
@@ -1855,8 +1855,8 @@ discard block |
||
| 1855 | 1855 | \$senstri = (\$senstri<0)?' DESC':''; |
| 1856 | 1856 | }; |
| 1857 | 1857 | "; |
| 1858 | - $boucle->select[] = '".tri_champ_select($tri)."'; |
|
| 1859 | - $boucle->order[] = "tri_champ_order(\$tri,\$command['from'],\$senstri)"; |
|
| 1858 | + $boucle->select[] = '".tri_champ_select($tri)."'; |
|
| 1859 | + $boucle->order[] = "tri_champ_order(\$tri,\$command['from'],\$senstri)"; |
|
| 1860 | 1860 | } |
| 1861 | 1861 | |
| 1862 | 1862 | # criteres de comparaison |
@@ -1873,21 +1873,21 @@ discard block |
||
| 1873 | 1873 | * @return void|array |
| 1874 | 1874 | **/ |
| 1875 | 1875 | function calculer_critere_DEFAUT_dist($idb, &$boucles, $crit) { |
| 1876 | - // double cas particulier {0,1} et {1/2} repere a l'analyse lexicale |
|
| 1877 | - if (($crit->op == ',') or ($crit->op == '/')) { |
|
| 1878 | - calculer_critere_parties($idb, $boucles, $crit); |
|
| 1879 | - return; |
|
| 1880 | - } |
|
| 1881 | - |
|
| 1882 | - $r = calculer_critere_infixe($idb, $boucles, $crit); |
|
| 1883 | - if (!$r) { |
|
| 1884 | - # // on produit une erreur seulement si le critere n'a pas de '?' |
|
| 1885 | - # if (!$crit->cond) { |
|
| 1886 | - return ['zbug_critere_inconnu', ['critere' => $crit->op]]; |
|
| 1887 | - # } |
|
| 1888 | - } else { |
|
| 1889 | - calculer_critere_DEFAUT_args($idb, $boucles, $crit, $r); |
|
| 1890 | - } |
|
| 1876 | + // double cas particulier {0,1} et {1/2} repere a l'analyse lexicale |
|
| 1877 | + if (($crit->op == ',') or ($crit->op == '/')) { |
|
| 1878 | + calculer_critere_parties($idb, $boucles, $crit); |
|
| 1879 | + return; |
|
| 1880 | + } |
|
| 1881 | + |
|
| 1882 | + $r = calculer_critere_infixe($idb, $boucles, $crit); |
|
| 1883 | + if (!$r) { |
|
| 1884 | + # // on produit une erreur seulement si le critere n'a pas de '?' |
|
| 1885 | + # if (!$crit->cond) { |
|
| 1886 | + return ['zbug_critere_inconnu', ['critere' => $crit->op]]; |
|
| 1887 | + # } |
|
| 1888 | + } else { |
|
| 1889 | + calculer_critere_DEFAUT_args($idb, $boucles, $crit, $r); |
|
| 1890 | + } |
|
| 1891 | 1891 | } |
| 1892 | 1892 | |
| 1893 | 1893 | |
@@ -1907,62 +1907,62 @@ discard block |
||
| 1907 | 1907 | * @return void |
| 1908 | 1908 | **/ |
| 1909 | 1909 | function calculer_critere_DEFAUT_args($idb, &$boucles, $crit, $args) { |
| 1910 | - [$arg, $op, $val, $col, $where_complement] = $args; |
|
| 1911 | - |
|
| 1912 | - $where = ["'$op'", "'$arg'", $val[0]]; |
|
| 1913 | - |
|
| 1914 | - // inserer la negation (cf !...) |
|
| 1915 | - |
|
| 1916 | - if ($crit->not) { |
|
| 1917 | - $where = ["'NOT'", $where]; |
|
| 1918 | - } |
|
| 1919 | - if ($crit->exclus) { |
|
| 1920 | - if (!preg_match(',^L[0-9]+[.],', $arg)) { |
|
| 1921 | - $where = ["'NOT'", $where]; |
|
| 1922 | - } else { |
|
| 1923 | - // un not sur un critere de jointure se traduit comme un NOT IN avec une sous requete |
|
| 1924 | - // c'est une sous requete identique a la requete principale sous la forme (SELF,$select,$where) avec $select et $where qui surchargent |
|
| 1925 | - $where = [ |
|
| 1926 | - "'NOT'", |
|
| 1927 | - [ |
|
| 1928 | - "'IN'", |
|
| 1929 | - "'" . $boucles[$idb]->id_table . '.' . $boucles[$idb]->primary . "'", |
|
| 1930 | - ["'SELF'", "'" . $boucles[$idb]->id_table . '.' . $boucles[$idb]->primary . "'", $where] |
|
| 1931 | - ] |
|
| 1932 | - ]; |
|
| 1933 | - } |
|
| 1934 | - } |
|
| 1935 | - |
|
| 1936 | - // inserer la condition (cf {lang?}) |
|
| 1937 | - // traiter a part la date, elle est mise d'office par SPIP, |
|
| 1938 | - if ($crit->cond) { |
|
| 1939 | - $pred = calculer_argument_precedent($idb, $col, $boucles); |
|
| 1940 | - if ($col === 'date' or $col === 'date_redac') { |
|
| 1941 | - if ($pred === "\$Pile[0]['" . $col . "']") { |
|
| 1942 | - $pred = "(\$Pile[0]['{$col}_default']?'':$pred)"; |
|
| 1943 | - } |
|
| 1944 | - } |
|
| 1945 | - |
|
| 1946 | - if ($op === '=' and !$crit->not) { |
|
| 1947 | - $where = [ |
|
| 1948 | - "'?'", |
|
| 1949 | - "(is_array($pred))", |
|
| 1950 | - critere_IN_cas($idb, $boucles, 'COND', $arg, $op, [$pred], $col), |
|
| 1951 | - $where |
|
| 1952 | - ]; |
|
| 1953 | - } |
|
| 1954 | - $where = ["'?'", "!is_whereable($pred)", "''", $where]; |
|
| 1955 | - if ($where_complement) { |
|
| 1956 | - // condition annexe du type "AND (objet='article')" |
|
| 1957 | - $where_complement = ["'?'", "!is_whereable($pred)", "''", $where_complement]; |
|
| 1958 | - } |
|
| 1959 | - } |
|
| 1960 | - |
|
| 1961 | - $boucles[$idb]->where[] = $where; |
|
| 1962 | - if ($where_complement) { |
|
| 1963 | - // condition annexe du type "AND (objet='article')" |
|
| 1964 | - $boucles[$idb]->where[] = $where_complement; |
|
| 1965 | - } |
|
| 1910 | + [$arg, $op, $val, $col, $where_complement] = $args; |
|
| 1911 | + |
|
| 1912 | + $where = ["'$op'", "'$arg'", $val[0]]; |
|
| 1913 | + |
|
| 1914 | + // inserer la negation (cf !...) |
|
| 1915 | + |
|
| 1916 | + if ($crit->not) { |
|
| 1917 | + $where = ["'NOT'", $where]; |
|
| 1918 | + } |
|
| 1919 | + if ($crit->exclus) { |
|
| 1920 | + if (!preg_match(',^L[0-9]+[.],', $arg)) { |
|
| 1921 | + $where = ["'NOT'", $where]; |
|
| 1922 | + } else { |
|
| 1923 | + // un not sur un critere de jointure se traduit comme un NOT IN avec une sous requete |
|
| 1924 | + // c'est une sous requete identique a la requete principale sous la forme (SELF,$select,$where) avec $select et $where qui surchargent |
|
| 1925 | + $where = [ |
|
| 1926 | + "'NOT'", |
|
| 1927 | + [ |
|
| 1928 | + "'IN'", |
|
| 1929 | + "'" . $boucles[$idb]->id_table . '.' . $boucles[$idb]->primary . "'", |
|
| 1930 | + ["'SELF'", "'" . $boucles[$idb]->id_table . '.' . $boucles[$idb]->primary . "'", $where] |
|
| 1931 | + ] |
|
| 1932 | + ]; |
|
| 1933 | + } |
|
| 1934 | + } |
|
| 1935 | + |
|
| 1936 | + // inserer la condition (cf {lang?}) |
|
| 1937 | + // traiter a part la date, elle est mise d'office par SPIP, |
|
| 1938 | + if ($crit->cond) { |
|
| 1939 | + $pred = calculer_argument_precedent($idb, $col, $boucles); |
|
| 1940 | + if ($col === 'date' or $col === 'date_redac') { |
|
| 1941 | + if ($pred === "\$Pile[0]['" . $col . "']") { |
|
| 1942 | + $pred = "(\$Pile[0]['{$col}_default']?'':$pred)"; |
|
| 1943 | + } |
|
| 1944 | + } |
|
| 1945 | + |
|
| 1946 | + if ($op === '=' and !$crit->not) { |
|
| 1947 | + $where = [ |
|
| 1948 | + "'?'", |
|
| 1949 | + "(is_array($pred))", |
|
| 1950 | + critere_IN_cas($idb, $boucles, 'COND', $arg, $op, [$pred], $col), |
|
| 1951 | + $where |
|
| 1952 | + ]; |
|
| 1953 | + } |
|
| 1954 | + $where = ["'?'", "!is_whereable($pred)", "''", $where]; |
|
| 1955 | + if ($where_complement) { |
|
| 1956 | + // condition annexe du type "AND (objet='article')" |
|
| 1957 | + $where_complement = ["'?'", "!is_whereable($pred)", "''", $where_complement]; |
|
| 1958 | + } |
|
| 1959 | + } |
|
| 1960 | + |
|
| 1961 | + $boucles[$idb]->where[] = $where; |
|
| 1962 | + if ($where_complement) { |
|
| 1963 | + // condition annexe du type "AND (objet='article')" |
|
| 1964 | + $boucles[$idb]->where[] = $where_complement; |
|
| 1965 | + } |
|
| 1966 | 1966 | } |
| 1967 | 1967 | |
| 1968 | 1968 | |
@@ -2003,165 +2003,165 @@ discard block |
||
| 2003 | 2003 | **/ |
| 2004 | 2004 | function calculer_critere_infixe($idb, &$boucles, $crit) { |
| 2005 | 2005 | |
| 2006 | - $boucle = &$boucles[$idb]; |
|
| 2007 | - $type = $boucle->type_requete; |
|
| 2008 | - $table = $boucle->id_table; |
|
| 2009 | - $desc = $boucle->show; |
|
| 2010 | - $col_vraie = null; |
|
| 2011 | - |
|
| 2012 | - [$fct, $col, $op, $val, $args_sql] = |
|
| 2013 | - calculer_critere_infixe_ops($idb, $boucles, $crit); |
|
| 2014 | - |
|
| 2015 | - $col_alias = $col; |
|
| 2016 | - $where_complement = false; |
|
| 2017 | - |
|
| 2018 | - // Cas particulier : id_enfant => utiliser la colonne id_objet |
|
| 2019 | - if ($col == 'id_enfant') { |
|
| 2020 | - $col = $boucle->primary; |
|
| 2021 | - } |
|
| 2022 | - |
|
| 2023 | - // Cas particulier : id_parent => verifier les exceptions de tables |
|
| 2024 | - if ( |
|
| 2025 | - (in_array($col, ['id_parent', 'id_secteur']) and isset($GLOBALS['exceptions_des_tables'][$table][$col])) |
|
| 2026 | - or (isset($GLOBALS['exceptions_des_tables'][$table][$col]) and is_string($GLOBALS['exceptions_des_tables'][$table][$col])) |
|
| 2027 | - ) { |
|
| 2028 | - $col = $GLOBALS['exceptions_des_tables'][$table][$col]; |
|
| 2029 | - } // et possibilite de gerer un critere secteur sur des tables de plugins (ie forums) |
|
| 2030 | - else { |
|
| 2031 | - if (($col == 'id_secteur') and ($critere_secteur = charger_fonction("critere_secteur_$type", 'public', true))) { |
|
| 2032 | - $table = $critere_secteur($idb, $boucles, $val, $crit); |
|
| 2033 | - } |
|
| 2034 | - |
|
| 2035 | - // cas id_article=xx qui se mappe en id_objet=xx AND objet=article |
|
| 2036 | - // sauf si exception declaree : sauter cette etape |
|
| 2037 | - else { |
|
| 2038 | - if ( |
|
| 2039 | - !isset($GLOBALS['exceptions_des_jointures'][table_objet_sql($table)][$col]) |
|
| 2040 | - and !isset($GLOBALS['exceptions_des_jointures'][$col]) |
|
| 2041 | - and count(trouver_champs_decomposes($col, $desc)) > 1 |
|
| 2042 | - ) { |
|
| 2043 | - $e = decompose_champ_id_objet($col); |
|
| 2044 | - $col = array_shift($e); |
|
| 2045 | - $where_complement = primary_doublee($e, $table); |
|
| 2046 | - } // Cas particulier : expressions de date |
|
| 2047 | - else { |
|
| 2048 | - if ($c = calculer_critere_infixe_date($idb, $boucles, $col)) { |
|
| 2049 | - [$col, $col_vraie] = $c; |
|
| 2050 | - $table = ''; |
|
| 2051 | - } // table explicitée {mots.titre} |
|
| 2052 | - else { |
|
| 2053 | - if (preg_match('/^(.*)\.(.*)$/', $col, $r)) { |
|
| 2054 | - [, $table, $col] = $r; |
|
| 2055 | - $col_alias = $col; |
|
| 2056 | - |
|
| 2057 | - $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 2058 | - if ( |
|
| 2059 | - $desc = $trouver_table($table, $boucle->sql_serveur) |
|
| 2060 | - and isset($desc['field'][$col]) |
|
| 2061 | - and $cle = array_search($desc['table'], $boucle->from) |
|
| 2062 | - ) { |
|
| 2063 | - $table = $cle; |
|
| 2064 | - } else { |
|
| 2065 | - $table = trouver_jointure_champ($col, $boucle, [$table], ($crit->cond or $op != '=')); |
|
| 2066 | - } |
|
| 2067 | - #$table = calculer_critere_externe_init($boucle, array($table), $col, $desc, ($crit->cond OR $op!='='), true); |
|
| 2068 | - if (!$table) { |
|
| 2069 | - return ''; |
|
| 2070 | - } |
|
| 2071 | - } |
|
| 2072 | - // si le champ n'est pas trouvé dans la table, |
|
| 2073 | - // on cherche si une jointure peut l'obtenir |
|
| 2074 | - elseif (@!array_key_exists($col, $desc['field'])) { |
|
| 2075 | - // Champ joker * des iterateurs DATA qui accepte tout |
|
| 2076 | - if (@array_key_exists('*', $desc['field'])) { |
|
| 2077 | - $desc['field'][$col_vraie ?: $col] = ''; // on veut pas de cast INT par defaut car le type peut etre n'importe quoi dans les boucles DATA |
|
| 2078 | - } |
|
| 2079 | - else { |
|
| 2080 | - $r = calculer_critere_infixe_externe($boucle, $crit, $op, $desc, $col, $col_alias, $table); |
|
| 2081 | - if (!$r) { |
|
| 2082 | - return ''; |
|
| 2083 | - } |
|
| 2084 | - [$col, $col_alias, $table, $where_complement, $desc] = $r; |
|
| 2085 | - } |
|
| 2086 | - } |
|
| 2087 | - } |
|
| 2088 | - } |
|
| 2089 | - } |
|
| 2090 | - } |
|
| 2091 | - |
|
| 2092 | - $col_vraie = ($col_vraie ?: $col); |
|
| 2093 | - // Dans tous les cas, |
|
| 2094 | - // virer les guillemets eventuels autour d'un int (qui sont refuses par certains SQL) |
|
| 2095 | - // et passer dans sql_quote avec le type si connu |
|
| 2096 | - // et int sinon si la valeur est numerique |
|
| 2097 | - // sinon introduire le vrai type du champ si connu dans le sql_quote (ou int NOT NULL sinon) |
|
| 2098 | - // Ne pas utiliser intval, PHP tronquant les Bigint de SQL |
|
| 2099 | - if ($op == '=' or in_array($op, $GLOBALS['table_criteres_infixes'])) { |
|
| 2100 | - $type_cast_quote = ($desc['field'][$col_vraie] ?? 'int NOT NULL'); |
|
| 2101 | - // defaire le quote des int et les passer dans sql_quote avec le bon type de champ si on le connait, int sinon |
|
| 2102 | - // prendre en compte le debug ou la valeur arrive avec un commentaire PHP en debut |
|
| 2103 | - if (preg_match(",^\\A(\s*//.*?$\s*)?\"'(-?\d+)'\"\\z,ms", $val[0], $r)) { |
|
| 2104 | - $val[0] = $r[1] . '"' . sql_quote($r[2], $boucle->sql_serveur, $type_cast_quote) . '"'; |
|
| 2105 | - } |
|
| 2106 | - // sinon expliciter les |
|
| 2107 | - // sql_quote(truc) en sql_quote(truc,'',type) |
|
| 2108 | - // sql_quote(truc,serveur) en sql_quote(truc,serveur,type) |
|
| 2109 | - // sql_quote(truc,serveur,'') en sql_quote(truc,serveur,type) |
|
| 2110 | - // sans toucher aux |
|
| 2111 | - // sql_quote(truc,'','varchar(10) DEFAULT \'oui\' COLLATE NOCASE') |
|
| 2112 | - // sql_quote(truc,'','varchar') |
|
| 2113 | - elseif ( |
|
| 2114 | - preg_match('/\Asql_quote[(](.*?)(,[^)]*?)?(,[^)]*(?:\(\d+\)[^)]*)?)?[)]\s*\z/ms', $val[0], $r) |
|
| 2115 | - // si pas deja un type |
|
| 2116 | - and (!isset($r[3]) or !$r[3] or !trim($r[3], ", '")) |
|
| 2117 | - ) { |
|
| 2118 | - $r = $r[1] |
|
| 2119 | - . ((isset($r[2]) and $r[2]) ? $r[2] : ",''") |
|
| 2120 | - . ",'" . addslashes($type_cast_quote) . "'"; |
|
| 2121 | - $val[0] = "sql_quote($r)"; |
|
| 2122 | - } |
|
| 2123 | - elseif ( |
|
| 2124 | - strpos($val[0], '@@defaultcast@@') !== false |
|
| 2125 | - and preg_match("/'@@defaultcast@@'\s*\)\s*\z/ms", $val[0], $r) |
|
| 2126 | - ) { |
|
| 2127 | - $val[0] = substr($val[0], 0, -strlen($r[0])) . "'" . addslashes($type_cast_quote) . "')"; |
|
| 2128 | - } |
|
| 2129 | - } |
|
| 2130 | - |
|
| 2131 | - if ( |
|
| 2132 | - strpos($val[0], '@@defaultcast@@') !== false |
|
| 2133 | - and preg_match("/'@@defaultcast@@'\s*\)\s*\z/ms", $val[0], $r) |
|
| 2134 | - ) { |
|
| 2135 | - $val[0] = substr($val[0], 0, -strlen($r[0])) . "'char')"; |
|
| 2136 | - } |
|
| 2137 | - |
|
| 2138 | - // Indicateur pour permettre aux fonctionx boucle_X de modifier |
|
| 2139 | - // leurs requetes par defaut, notamment le champ statut |
|
| 2140 | - // Ne pas confondre champs de la table principale et des jointures |
|
| 2141 | - if ($table === $boucle->id_table) { |
|
| 2142 | - $boucles[$idb]->modificateur['criteres'][$col_vraie] = true; |
|
| 2143 | - if ($col_alias != $col_vraie) { |
|
| 2144 | - $boucles[$idb]->modificateur['criteres'][$col_alias] = true; |
|
| 2145 | - } |
|
| 2146 | - } |
|
| 2147 | - |
|
| 2148 | - // inserer le nom de la table SQL devant le nom du champ |
|
| 2149 | - if ($table) { |
|
| 2150 | - if ($col[0] == '`') { |
|
| 2151 | - $arg = "$table." . substr($col, 1, -1); |
|
| 2152 | - } else { |
|
| 2153 | - $arg = "$table.$col"; |
|
| 2154 | - } |
|
| 2155 | - } else { |
|
| 2156 | - $arg = $col; |
|
| 2157 | - } |
|
| 2158 | - |
|
| 2159 | - // inserer la fonction SQL |
|
| 2160 | - if ($fct) { |
|
| 2161 | - $arg = "$fct($arg$args_sql)"; |
|
| 2162 | - } |
|
| 2163 | - |
|
| 2164 | - return [$arg, $op, $val, $col_alias, $where_complement]; |
|
| 2006 | + $boucle = &$boucles[$idb]; |
|
| 2007 | + $type = $boucle->type_requete; |
|
| 2008 | + $table = $boucle->id_table; |
|
| 2009 | + $desc = $boucle->show; |
|
| 2010 | + $col_vraie = null; |
|
| 2011 | + |
|
| 2012 | + [$fct, $col, $op, $val, $args_sql] = |
|
| 2013 | + calculer_critere_infixe_ops($idb, $boucles, $crit); |
|
| 2014 | + |
|
| 2015 | + $col_alias = $col; |
|
| 2016 | + $where_complement = false; |
|
| 2017 | + |
|
| 2018 | + // Cas particulier : id_enfant => utiliser la colonne id_objet |
|
| 2019 | + if ($col == 'id_enfant') { |
|
| 2020 | + $col = $boucle->primary; |
|
| 2021 | + } |
|
| 2022 | + |
|
| 2023 | + // Cas particulier : id_parent => verifier les exceptions de tables |
|
| 2024 | + if ( |
|
| 2025 | + (in_array($col, ['id_parent', 'id_secteur']) and isset($GLOBALS['exceptions_des_tables'][$table][$col])) |
|
| 2026 | + or (isset($GLOBALS['exceptions_des_tables'][$table][$col]) and is_string($GLOBALS['exceptions_des_tables'][$table][$col])) |
|
| 2027 | + ) { |
|
| 2028 | + $col = $GLOBALS['exceptions_des_tables'][$table][$col]; |
|
| 2029 | + } // et possibilite de gerer un critere secteur sur des tables de plugins (ie forums) |
|
| 2030 | + else { |
|
| 2031 | + if (($col == 'id_secteur') and ($critere_secteur = charger_fonction("critere_secteur_$type", 'public', true))) { |
|
| 2032 | + $table = $critere_secteur($idb, $boucles, $val, $crit); |
|
| 2033 | + } |
|
| 2034 | + |
|
| 2035 | + // cas id_article=xx qui se mappe en id_objet=xx AND objet=article |
|
| 2036 | + // sauf si exception declaree : sauter cette etape |
|
| 2037 | + else { |
|
| 2038 | + if ( |
|
| 2039 | + !isset($GLOBALS['exceptions_des_jointures'][table_objet_sql($table)][$col]) |
|
| 2040 | + and !isset($GLOBALS['exceptions_des_jointures'][$col]) |
|
| 2041 | + and count(trouver_champs_decomposes($col, $desc)) > 1 |
|
| 2042 | + ) { |
|
| 2043 | + $e = decompose_champ_id_objet($col); |
|
| 2044 | + $col = array_shift($e); |
|
| 2045 | + $where_complement = primary_doublee($e, $table); |
|
| 2046 | + } // Cas particulier : expressions de date |
|
| 2047 | + else { |
|
| 2048 | + if ($c = calculer_critere_infixe_date($idb, $boucles, $col)) { |
|
| 2049 | + [$col, $col_vraie] = $c; |
|
| 2050 | + $table = ''; |
|
| 2051 | + } // table explicitée {mots.titre} |
|
| 2052 | + else { |
|
| 2053 | + if (preg_match('/^(.*)\.(.*)$/', $col, $r)) { |
|
| 2054 | + [, $table, $col] = $r; |
|
| 2055 | + $col_alias = $col; |
|
| 2056 | + |
|
| 2057 | + $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 2058 | + if ( |
|
| 2059 | + $desc = $trouver_table($table, $boucle->sql_serveur) |
|
| 2060 | + and isset($desc['field'][$col]) |
|
| 2061 | + and $cle = array_search($desc['table'], $boucle->from) |
|
| 2062 | + ) { |
|
| 2063 | + $table = $cle; |
|
| 2064 | + } else { |
|
| 2065 | + $table = trouver_jointure_champ($col, $boucle, [$table], ($crit->cond or $op != '=')); |
|
| 2066 | + } |
|
| 2067 | + #$table = calculer_critere_externe_init($boucle, array($table), $col, $desc, ($crit->cond OR $op!='='), true); |
|
| 2068 | + if (!$table) { |
|
| 2069 | + return ''; |
|
| 2070 | + } |
|
| 2071 | + } |
|
| 2072 | + // si le champ n'est pas trouvé dans la table, |
|
| 2073 | + // on cherche si une jointure peut l'obtenir |
|
| 2074 | + elseif (@!array_key_exists($col, $desc['field'])) { |
|
| 2075 | + // Champ joker * des iterateurs DATA qui accepte tout |
|
| 2076 | + if (@array_key_exists('*', $desc['field'])) { |
|
| 2077 | + $desc['field'][$col_vraie ?: $col] = ''; // on veut pas de cast INT par defaut car le type peut etre n'importe quoi dans les boucles DATA |
|
| 2078 | + } |
|
| 2079 | + else { |
|
| 2080 | + $r = calculer_critere_infixe_externe($boucle, $crit, $op, $desc, $col, $col_alias, $table); |
|
| 2081 | + if (!$r) { |
|
| 2082 | + return ''; |
|
| 2083 | + } |
|
| 2084 | + [$col, $col_alias, $table, $where_complement, $desc] = $r; |
|
| 2085 | + } |
|
| 2086 | + } |
|
| 2087 | + } |
|
| 2088 | + } |
|
| 2089 | + } |
|
| 2090 | + } |
|
| 2091 | + |
|
| 2092 | + $col_vraie = ($col_vraie ?: $col); |
|
| 2093 | + // Dans tous les cas, |
|
| 2094 | + // virer les guillemets eventuels autour d'un int (qui sont refuses par certains SQL) |
|
| 2095 | + // et passer dans sql_quote avec le type si connu |
|
| 2096 | + // et int sinon si la valeur est numerique |
|
| 2097 | + // sinon introduire le vrai type du champ si connu dans le sql_quote (ou int NOT NULL sinon) |
|
| 2098 | + // Ne pas utiliser intval, PHP tronquant les Bigint de SQL |
|
| 2099 | + if ($op == '=' or in_array($op, $GLOBALS['table_criteres_infixes'])) { |
|
| 2100 | + $type_cast_quote = ($desc['field'][$col_vraie] ?? 'int NOT NULL'); |
|
| 2101 | + // defaire le quote des int et les passer dans sql_quote avec le bon type de champ si on le connait, int sinon |
|
| 2102 | + // prendre en compte le debug ou la valeur arrive avec un commentaire PHP en debut |
|
| 2103 | + if (preg_match(",^\\A(\s*//.*?$\s*)?\"'(-?\d+)'\"\\z,ms", $val[0], $r)) { |
|
| 2104 | + $val[0] = $r[1] . '"' . sql_quote($r[2], $boucle->sql_serveur, $type_cast_quote) . '"'; |
|
| 2105 | + } |
|
| 2106 | + // sinon expliciter les |
|
| 2107 | + // sql_quote(truc) en sql_quote(truc,'',type) |
|
| 2108 | + // sql_quote(truc,serveur) en sql_quote(truc,serveur,type) |
|
| 2109 | + // sql_quote(truc,serveur,'') en sql_quote(truc,serveur,type) |
|
| 2110 | + // sans toucher aux |
|
| 2111 | + // sql_quote(truc,'','varchar(10) DEFAULT \'oui\' COLLATE NOCASE') |
|
| 2112 | + // sql_quote(truc,'','varchar') |
|
| 2113 | + elseif ( |
|
| 2114 | + preg_match('/\Asql_quote[(](.*?)(,[^)]*?)?(,[^)]*(?:\(\d+\)[^)]*)?)?[)]\s*\z/ms', $val[0], $r) |
|
| 2115 | + // si pas deja un type |
|
| 2116 | + and (!isset($r[3]) or !$r[3] or !trim($r[3], ", '")) |
|
| 2117 | + ) { |
|
| 2118 | + $r = $r[1] |
|
| 2119 | + . ((isset($r[2]) and $r[2]) ? $r[2] : ",''") |
|
| 2120 | + . ",'" . addslashes($type_cast_quote) . "'"; |
|
| 2121 | + $val[0] = "sql_quote($r)"; |
|
| 2122 | + } |
|
| 2123 | + elseif ( |
|
| 2124 | + strpos($val[0], '@@defaultcast@@') !== false |
|
| 2125 | + and preg_match("/'@@defaultcast@@'\s*\)\s*\z/ms", $val[0], $r) |
|
| 2126 | + ) { |
|
| 2127 | + $val[0] = substr($val[0], 0, -strlen($r[0])) . "'" . addslashes($type_cast_quote) . "')"; |
|
| 2128 | + } |
|
| 2129 | + } |
|
| 2130 | + |
|
| 2131 | + if ( |
|
| 2132 | + strpos($val[0], '@@defaultcast@@') !== false |
|
| 2133 | + and preg_match("/'@@defaultcast@@'\s*\)\s*\z/ms", $val[0], $r) |
|
| 2134 | + ) { |
|
| 2135 | + $val[0] = substr($val[0], 0, -strlen($r[0])) . "'char')"; |
|
| 2136 | + } |
|
| 2137 | + |
|
| 2138 | + // Indicateur pour permettre aux fonctionx boucle_X de modifier |
|
| 2139 | + // leurs requetes par defaut, notamment le champ statut |
|
| 2140 | + // Ne pas confondre champs de la table principale et des jointures |
|
| 2141 | + if ($table === $boucle->id_table) { |
|
| 2142 | + $boucles[$idb]->modificateur['criteres'][$col_vraie] = true; |
|
| 2143 | + if ($col_alias != $col_vraie) { |
|
| 2144 | + $boucles[$idb]->modificateur['criteres'][$col_alias] = true; |
|
| 2145 | + } |
|
| 2146 | + } |
|
| 2147 | + |
|
| 2148 | + // inserer le nom de la table SQL devant le nom du champ |
|
| 2149 | + if ($table) { |
|
| 2150 | + if ($col[0] == '`') { |
|
| 2151 | + $arg = "$table." . substr($col, 1, -1); |
|
| 2152 | + } else { |
|
| 2153 | + $arg = "$table.$col"; |
|
| 2154 | + } |
|
| 2155 | + } else { |
|
| 2156 | + $arg = $col; |
|
| 2157 | + } |
|
| 2158 | + |
|
| 2159 | + // inserer la fonction SQL |
|
| 2160 | + if ($fct) { |
|
| 2161 | + $arg = "$fct($arg$args_sql)"; |
|
| 2162 | + } |
|
| 2163 | + |
|
| 2164 | + return [$arg, $op, $val, $col_alias, $where_complement]; |
|
| 2165 | 2165 | } |
| 2166 | 2166 | |
| 2167 | 2167 | |
@@ -2190,77 +2190,77 @@ discard block |
||
| 2190 | 2190 | **/ |
| 2191 | 2191 | function calculer_critere_infixe_externe($boucle, $crit, $op, $desc, $col, $col_alias, $table) { |
| 2192 | 2192 | |
| 2193 | - $where = ''; |
|
| 2194 | - |
|
| 2195 | - $calculer_critere_externe = 'calculer_critere_externe_init'; |
|
| 2196 | - // gestion par les plugins des jointures tordues |
|
| 2197 | - // pas automatiques mais necessaires |
|
| 2198 | - $table_sql = table_objet_sql($table); |
|
| 2199 | - if ( |
|
| 2200 | - isset($GLOBALS['exceptions_des_jointures'][$table_sql]) |
|
| 2201 | - and is_array($GLOBALS['exceptions_des_jointures'][$table_sql]) |
|
| 2202 | - and |
|
| 2203 | - ( |
|
| 2204 | - isset($GLOBALS['exceptions_des_jointures'][$table_sql][$col]) |
|
| 2205 | - or |
|
| 2206 | - isset($GLOBALS['exceptions_des_jointures'][$table_sql]['']) |
|
| 2207 | - ) |
|
| 2208 | - ) { |
|
| 2209 | - $t = $GLOBALS['exceptions_des_jointures'][$table_sql]; |
|
| 2210 | - $index = $t[$col] ?? $t[''] ?? []; |
|
| 2211 | - |
|
| 2212 | - if ((is_countable($index) ? count($index) : 0) == 3) { |
|
| 2213 | - [$t, $col, $calculer_critere_externe] = $index; |
|
| 2214 | - } elseif ((is_countable($index) ? count($index) : 0) == 2) { |
|
| 2215 | - [$t, $col] = $t[$col]; |
|
| 2216 | - } elseif ((is_countable($index) ? count($index) : 0) == 1) { |
|
| 2217 | - [$calculer_critere_externe] = $index; |
|
| 2218 | - $t = $table; |
|
| 2219 | - } else { |
|
| 2220 | - $t = ''; |
|
| 2221 | - } // jointure non declaree. La trouver. |
|
| 2222 | - } elseif (isset($GLOBALS['exceptions_des_jointures'][$col])) { |
|
| 2223 | - [$t, $col] = $GLOBALS['exceptions_des_jointures'][$col]; |
|
| 2224 | - } else { |
|
| 2225 | - $t = ''; |
|
| 2226 | - } // jointure non declaree. La trouver. |
|
| 2227 | - |
|
| 2228 | - // ici on construit le from pour fournir $col en piochant dans les jointures |
|
| 2229 | - |
|
| 2230 | - // si des jointures explicites sont fournies, on cherche d'abord dans celles ci |
|
| 2231 | - // permet de forcer une table de lien quand il y a ambiguite |
|
| 2232 | - // <BOUCLE_(DOCUMENTS documents_liens){id_mot}> |
|
| 2233 | - // alors que <BOUCLE_(DOCUMENTS){id_mot}> produit la meme chose que <BOUCLE_(DOCUMENTS mots_liens){id_mot}> |
|
| 2234 | - $table = ''; |
|
| 2235 | - if ($boucle->jointures_explicites) { |
|
| 2236 | - $jointures_explicites = explode(' ', $boucle->jointures_explicites); |
|
| 2237 | - $table = $calculer_critere_externe($boucle, $jointures_explicites, $col, $desc, ($crit->cond or $op != '='), $t); |
|
| 2238 | - } |
|
| 2239 | - |
|
| 2240 | - // et sinon on cherche parmi toutes les jointures declarees |
|
| 2241 | - if (!$table) { |
|
| 2242 | - $table = $calculer_critere_externe($boucle, $boucle->jointures, $col, $desc, ($crit->cond or $op != '='), $t); |
|
| 2243 | - } |
|
| 2244 | - |
|
| 2245 | - if (!$table) { |
|
| 2246 | - return ''; |
|
| 2247 | - } |
|
| 2248 | - |
|
| 2249 | - // il ne reste plus qu'a trouver le champ dans les from |
|
| 2250 | - [$nom, $desc, $cle] = trouver_champ_exterieur($col, $boucle->from, $boucle); |
|
| 2251 | - |
|
| 2252 | - if ((is_countable($cle) ? count($cle) : 0) > 1 or reset($cle) !== $col) { |
|
| 2253 | - $col_alias = $col; // id_article devient juste le nom d'origine |
|
| 2254 | - if ((is_countable($cle) ? count($cle) : 0) > 1 and reset($cle) == 'id_objet') { |
|
| 2255 | - $e = decompose_champ_id_objet($col); |
|
| 2256 | - $col = array_shift($e); |
|
| 2257 | - $where = primary_doublee($e, $table); |
|
| 2258 | - } else { |
|
| 2259 | - $col = reset($cle); |
|
| 2260 | - } |
|
| 2261 | - } |
|
| 2262 | - |
|
| 2263 | - return [$col, $col_alias, $table, $where, $desc]; |
|
| 2193 | + $where = ''; |
|
| 2194 | + |
|
| 2195 | + $calculer_critere_externe = 'calculer_critere_externe_init'; |
|
| 2196 | + // gestion par les plugins des jointures tordues |
|
| 2197 | + // pas automatiques mais necessaires |
|
| 2198 | + $table_sql = table_objet_sql($table); |
|
| 2199 | + if ( |
|
| 2200 | + isset($GLOBALS['exceptions_des_jointures'][$table_sql]) |
|
| 2201 | + and is_array($GLOBALS['exceptions_des_jointures'][$table_sql]) |
|
| 2202 | + and |
|
| 2203 | + ( |
|
| 2204 | + isset($GLOBALS['exceptions_des_jointures'][$table_sql][$col]) |
|
| 2205 | + or |
|
| 2206 | + isset($GLOBALS['exceptions_des_jointures'][$table_sql]['']) |
|
| 2207 | + ) |
|
| 2208 | + ) { |
|
| 2209 | + $t = $GLOBALS['exceptions_des_jointures'][$table_sql]; |
|
| 2210 | + $index = $t[$col] ?? $t[''] ?? []; |
|
| 2211 | + |
|
| 2212 | + if ((is_countable($index) ? count($index) : 0) == 3) { |
|
| 2213 | + [$t, $col, $calculer_critere_externe] = $index; |
|
| 2214 | + } elseif ((is_countable($index) ? count($index) : 0) == 2) { |
|
| 2215 | + [$t, $col] = $t[$col]; |
|
| 2216 | + } elseif ((is_countable($index) ? count($index) : 0) == 1) { |
|
| 2217 | + [$calculer_critere_externe] = $index; |
|
| 2218 | + $t = $table; |
|
| 2219 | + } else { |
|
| 2220 | + $t = ''; |
|
| 2221 | + } // jointure non declaree. La trouver. |
|
| 2222 | + } elseif (isset($GLOBALS['exceptions_des_jointures'][$col])) { |
|
| 2223 | + [$t, $col] = $GLOBALS['exceptions_des_jointures'][$col]; |
|
| 2224 | + } else { |
|
| 2225 | + $t = ''; |
|
| 2226 | + } // jointure non declaree. La trouver. |
|
| 2227 | + |
|
| 2228 | + // ici on construit le from pour fournir $col en piochant dans les jointures |
|
| 2229 | + |
|
| 2230 | + // si des jointures explicites sont fournies, on cherche d'abord dans celles ci |
|
| 2231 | + // permet de forcer une table de lien quand il y a ambiguite |
|
| 2232 | + // <BOUCLE_(DOCUMENTS documents_liens){id_mot}> |
|
| 2233 | + // alors que <BOUCLE_(DOCUMENTS){id_mot}> produit la meme chose que <BOUCLE_(DOCUMENTS mots_liens){id_mot}> |
|
| 2234 | + $table = ''; |
|
| 2235 | + if ($boucle->jointures_explicites) { |
|
| 2236 | + $jointures_explicites = explode(' ', $boucle->jointures_explicites); |
|
| 2237 | + $table = $calculer_critere_externe($boucle, $jointures_explicites, $col, $desc, ($crit->cond or $op != '='), $t); |
|
| 2238 | + } |
|
| 2239 | + |
|
| 2240 | + // et sinon on cherche parmi toutes les jointures declarees |
|
| 2241 | + if (!$table) { |
|
| 2242 | + $table = $calculer_critere_externe($boucle, $boucle->jointures, $col, $desc, ($crit->cond or $op != '='), $t); |
|
| 2243 | + } |
|
| 2244 | + |
|
| 2245 | + if (!$table) { |
|
| 2246 | + return ''; |
|
| 2247 | + } |
|
| 2248 | + |
|
| 2249 | + // il ne reste plus qu'a trouver le champ dans les from |
|
| 2250 | + [$nom, $desc, $cle] = trouver_champ_exterieur($col, $boucle->from, $boucle); |
|
| 2251 | + |
|
| 2252 | + if ((is_countable($cle) ? count($cle) : 0) > 1 or reset($cle) !== $col) { |
|
| 2253 | + $col_alias = $col; // id_article devient juste le nom d'origine |
|
| 2254 | + if ((is_countable($cle) ? count($cle) : 0) > 1 and reset($cle) == 'id_objet') { |
|
| 2255 | + $e = decompose_champ_id_objet($col); |
|
| 2256 | + $col = array_shift($e); |
|
| 2257 | + $where = primary_doublee($e, $table); |
|
| 2258 | + } else { |
|
| 2259 | + $col = reset($cle); |
|
| 2260 | + } |
|
| 2261 | + } |
|
| 2262 | + |
|
| 2263 | + return [$col, $col_alias, $table, $where, $desc]; |
|
| 2264 | 2264 | } |
| 2265 | 2265 | |
| 2266 | 2266 | |
@@ -2281,10 +2281,10 @@ discard block |
||
| 2281 | 2281 | * - valeur |
| 2282 | 2282 | **/ |
| 2283 | 2283 | function primary_doublee($decompose, $table) { |
| 2284 | - $e1 = reset($decompose); |
|
| 2285 | - $e2 = "sql_quote('" . end($decompose) . "')"; |
|
| 2284 | + $e1 = reset($decompose); |
|
| 2285 | + $e2 = "sql_quote('" . end($decompose) . "')"; |
|
| 2286 | 2286 | |
| 2287 | - return ["'='", "'$table." . $e1 . "'", $e2]; |
|
| 2287 | + return ["'='", "'$table." . $e1 . "'", $e2]; |
|
| 2288 | 2288 | } |
| 2289 | 2289 | |
| 2290 | 2290 | /** |
@@ -2315,57 +2315,57 @@ discard block |
||
| 2315 | 2315 | * Vide sinon. |
| 2316 | 2316 | */ |
| 2317 | 2317 | function calculer_critere_externe_init(&$boucle, $joints, $col, $desc, $cond, $checkarrivee = false) { |
| 2318 | - // si on demande un truc du genre spip_mots |
|
| 2319 | - // avec aussi spip_mots_liens dans les jointures dispo |
|
| 2320 | - // et qu'on est la |
|
| 2321 | - // il faut privilegier la jointure directe en 2 etapes spip_mots_liens, spip_mots |
|
| 2322 | - if ( |
|
| 2323 | - $checkarrivee |
|
| 2324 | - and is_string($checkarrivee) |
|
| 2325 | - and $a = table_objet($checkarrivee) |
|
| 2326 | - and in_array($a . '_liens', $joints) |
|
| 2327 | - ) { |
|
| 2328 | - if ($res = calculer_lien_externe_init($boucle, $joints, $col, $desc, $cond, $checkarrivee)) { |
|
| 2329 | - return $res; |
|
| 2330 | - } |
|
| 2331 | - } |
|
| 2332 | - foreach ($joints as $joint) { |
|
| 2333 | - if ($arrivee = trouver_champ_exterieur($col, [$joint], $boucle, $checkarrivee)) { |
|
| 2334 | - // alias de table dans le from |
|
| 2335 | - $t = array_search($arrivee[0], $boucle->from); |
|
| 2336 | - // recuperer la cle id_xx eventuellement decomposee en (id_objet,objet) |
|
| 2337 | - $cols = $arrivee[2]; |
|
| 2338 | - // mais on ignore la 3eme cle si presente qui correspond alors au point de depart |
|
| 2339 | - if ((is_countable($cols) ? count($cols) : 0) > 2) { |
|
| 2340 | - array_pop($cols); |
|
| 2341 | - } |
|
| 2342 | - if ($t) { |
|
| 2343 | - // la table est déjà dans le FROM, on vérifie si le champ est utilisé. |
|
| 2344 | - $joindre = false; |
|
| 2345 | - foreach ($cols as $col) { |
|
| 2346 | - $c = '/\b' . $t . ".$col" . '\b/'; |
|
| 2347 | - if (trouver_champ($c, $boucle->where)) { |
|
| 2348 | - $joindre = true; |
|
| 2349 | - } else { |
|
| 2350 | - // mais ca peut etre dans le FIELD pour le Having |
|
| 2351 | - $c = "/FIELD.$t" . ".$col,/"; |
|
| 2352 | - if (trouver_champ($c, $boucle->select)) { |
|
| 2353 | - $joindre = true; |
|
| 2354 | - } |
|
| 2355 | - } |
|
| 2356 | - } |
|
| 2357 | - if (!$joindre) { |
|
| 2358 | - return $t; |
|
| 2359 | - } |
|
| 2360 | - } |
|
| 2361 | - array_pop($arrivee); |
|
| 2362 | - if ($res = calculer_jointure($boucle, [$boucle->id_table, $desc], $arrivee, $cols, $cond, 1)) { |
|
| 2363 | - return $res; |
|
| 2364 | - } |
|
| 2365 | - } |
|
| 2366 | - } |
|
| 2367 | - |
|
| 2368 | - return ''; |
|
| 2318 | + // si on demande un truc du genre spip_mots |
|
| 2319 | + // avec aussi spip_mots_liens dans les jointures dispo |
|
| 2320 | + // et qu'on est la |
|
| 2321 | + // il faut privilegier la jointure directe en 2 etapes spip_mots_liens, spip_mots |
|
| 2322 | + if ( |
|
| 2323 | + $checkarrivee |
|
| 2324 | + and is_string($checkarrivee) |
|
| 2325 | + and $a = table_objet($checkarrivee) |
|
| 2326 | + and in_array($a . '_liens', $joints) |
|
| 2327 | + ) { |
|
| 2328 | + if ($res = calculer_lien_externe_init($boucle, $joints, $col, $desc, $cond, $checkarrivee)) { |
|
| 2329 | + return $res; |
|
| 2330 | + } |
|
| 2331 | + } |
|
| 2332 | + foreach ($joints as $joint) { |
|
| 2333 | + if ($arrivee = trouver_champ_exterieur($col, [$joint], $boucle, $checkarrivee)) { |
|
| 2334 | + // alias de table dans le from |
|
| 2335 | + $t = array_search($arrivee[0], $boucle->from); |
|
| 2336 | + // recuperer la cle id_xx eventuellement decomposee en (id_objet,objet) |
|
| 2337 | + $cols = $arrivee[2]; |
|
| 2338 | + // mais on ignore la 3eme cle si presente qui correspond alors au point de depart |
|
| 2339 | + if ((is_countable($cols) ? count($cols) : 0) > 2) { |
|
| 2340 | + array_pop($cols); |
|
| 2341 | + } |
|
| 2342 | + if ($t) { |
|
| 2343 | + // la table est déjà dans le FROM, on vérifie si le champ est utilisé. |
|
| 2344 | + $joindre = false; |
|
| 2345 | + foreach ($cols as $col) { |
|
| 2346 | + $c = '/\b' . $t . ".$col" . '\b/'; |
|
| 2347 | + if (trouver_champ($c, $boucle->where)) { |
|
| 2348 | + $joindre = true; |
|
| 2349 | + } else { |
|
| 2350 | + // mais ca peut etre dans le FIELD pour le Having |
|
| 2351 | + $c = "/FIELD.$t" . ".$col,/"; |
|
| 2352 | + if (trouver_champ($c, $boucle->select)) { |
|
| 2353 | + $joindre = true; |
|
| 2354 | + } |
|
| 2355 | + } |
|
| 2356 | + } |
|
| 2357 | + if (!$joindre) { |
|
| 2358 | + return $t; |
|
| 2359 | + } |
|
| 2360 | + } |
|
| 2361 | + array_pop($arrivee); |
|
| 2362 | + if ($res = calculer_jointure($boucle, [$boucle->id_table, $desc], $arrivee, $cols, $cond, 1)) { |
|
| 2363 | + return $res; |
|
| 2364 | + } |
|
| 2365 | + } |
|
| 2366 | + } |
|
| 2367 | + |
|
| 2368 | + return ''; |
|
| 2369 | 2369 | } |
| 2370 | 2370 | |
| 2371 | 2371 | /** |
@@ -2391,35 +2391,35 @@ discard block |
||
| 2391 | 2391 | * Alias de la table de jointure (Lx) |
| 2392 | 2392 | */ |
| 2393 | 2393 | function calculer_lien_externe_init(&$boucle, $joints, $col, $desc, $cond, $checkarrivee = false) { |
| 2394 | - $primary_arrivee = id_table_objet($checkarrivee); |
|
| 2395 | - |
|
| 2396 | - // [FIXME] $checkarrivee peut-il arriver avec false ???? |
|
| 2397 | - $intermediaire = trouver_champ_exterieur($primary_arrivee, $joints, $boucle, $checkarrivee . '_liens'); |
|
| 2398 | - $arrivee = trouver_champ_exterieur($col, $joints, $boucle, $checkarrivee); |
|
| 2399 | - |
|
| 2400 | - if (!$intermediaire or !$arrivee) { |
|
| 2401 | - return ''; |
|
| 2402 | - } |
|
| 2403 | - array_pop($intermediaire); // enlever la cle en 3eme argument |
|
| 2404 | - array_pop($arrivee); // enlever la cle en 3eme argument |
|
| 2405 | - |
|
| 2406 | - $res = fabrique_jointures( |
|
| 2407 | - $boucle, |
|
| 2408 | - [ |
|
| 2409 | - [ |
|
| 2410 | - $boucle->id_table, |
|
| 2411 | - $intermediaire, |
|
| 2412 | - [id_table_objet($desc['table_objet']), 'id_objet', 'objet', $desc['type']] |
|
| 2413 | - ], |
|
| 2414 | - [reset($intermediaire), $arrivee, $primary_arrivee] |
|
| 2415 | - ], |
|
| 2416 | - $cond, |
|
| 2417 | - $desc, |
|
| 2418 | - $boucle->id_table, |
|
| 2419 | - [$col] |
|
| 2420 | - ); |
|
| 2421 | - |
|
| 2422 | - return $res; |
|
| 2394 | + $primary_arrivee = id_table_objet($checkarrivee); |
|
| 2395 | + |
|
| 2396 | + // [FIXME] $checkarrivee peut-il arriver avec false ???? |
|
| 2397 | + $intermediaire = trouver_champ_exterieur($primary_arrivee, $joints, $boucle, $checkarrivee . '_liens'); |
|
| 2398 | + $arrivee = trouver_champ_exterieur($col, $joints, $boucle, $checkarrivee); |
|
| 2399 | + |
|
| 2400 | + if (!$intermediaire or !$arrivee) { |
|
| 2401 | + return ''; |
|
| 2402 | + } |
|
| 2403 | + array_pop($intermediaire); // enlever la cle en 3eme argument |
|
| 2404 | + array_pop($arrivee); // enlever la cle en 3eme argument |
|
| 2405 | + |
|
| 2406 | + $res = fabrique_jointures( |
|
| 2407 | + $boucle, |
|
| 2408 | + [ |
|
| 2409 | + [ |
|
| 2410 | + $boucle->id_table, |
|
| 2411 | + $intermediaire, |
|
| 2412 | + [id_table_objet($desc['table_objet']), 'id_objet', 'objet', $desc['type']] |
|
| 2413 | + ], |
|
| 2414 | + [reset($intermediaire), $arrivee, $primary_arrivee] |
|
| 2415 | + ], |
|
| 2416 | + $cond, |
|
| 2417 | + $desc, |
|
| 2418 | + $boucle->id_table, |
|
| 2419 | + [$col] |
|
| 2420 | + ); |
|
| 2421 | + |
|
| 2422 | + return $res; |
|
| 2423 | 2423 | } |
| 2424 | 2424 | |
| 2425 | 2425 | |
@@ -2436,17 +2436,17 @@ discard block |
||
| 2436 | 2436 | * false sinon. |
| 2437 | 2437 | **/ |
| 2438 | 2438 | function trouver_champ($champ, $where) { |
| 2439 | - if (!is_array($where)) { |
|
| 2440 | - return preg_match($champ, $where); |
|
| 2441 | - } else { |
|
| 2442 | - foreach ($where as $clause) { |
|
| 2443 | - if (trouver_champ($champ, $clause)) { |
|
| 2444 | - return true; |
|
| 2445 | - } |
|
| 2446 | - } |
|
| 2447 | - |
|
| 2448 | - return false; |
|
| 2449 | - } |
|
| 2439 | + if (!is_array($where)) { |
|
| 2440 | + return preg_match($champ, $where); |
|
| 2441 | + } else { |
|
| 2442 | + foreach ($where as $clause) { |
|
| 2443 | + if (trouver_champ($champ, $clause)) { |
|
| 2444 | + return true; |
|
| 2445 | + } |
|
| 2446 | + } |
|
| 2447 | + |
|
| 2448 | + return false; |
|
| 2449 | + } |
|
| 2450 | 2450 | } |
| 2451 | 2451 | |
| 2452 | 2452 | |
@@ -2472,129 +2472,129 @@ discard block |
||
| 2472 | 2472 | * - string $args_sql Suite des arguments du critère. ? |
| 2473 | 2473 | **/ |
| 2474 | 2474 | function calculer_critere_infixe_ops($idb, &$boucles, $crit) { |
| 2475 | - // cas d'une valeur comparee a elle-meme ou son referent |
|
| 2476 | - if (count($crit->param) == 0) { |
|
| 2477 | - $op = '='; |
|
| 2478 | - $col = $val = $crit->op; |
|
| 2479 | - if (preg_match('/^(.*)\.(.*)$/', $col, $r)) { |
|
| 2480 | - $val = $r[2]; |
|
| 2481 | - } |
|
| 2482 | - // Cas special {lang} : aller chercher $GLOBALS['spip_lang'] |
|
| 2483 | - if ($val == 'lang') { |
|
| 2484 | - $val = [kwote('$GLOBALS[\'spip_lang\']')]; |
|
| 2485 | - } else { |
|
| 2486 | - $defaut = null; |
|
| 2487 | - if ($val == 'id_parent') { |
|
| 2488 | - // Si id_parent, comparer l'id_parent avec l'id_objet |
|
| 2489 | - // de la boucle superieure.... faudrait verifier qu'il existe |
|
| 2490 | - // pour eviter l'erreur SQL |
|
| 2491 | - $val = $boucles[$idb]->primary; |
|
| 2492 | - // mais si pas de boucle superieure, prendre id_parent dans l'env |
|
| 2493 | - $defaut = "(\$Pile[0]['id_parent'] ?? null)"; |
|
| 2494 | - } elseif ($val == 'id_enfant') { |
|
| 2495 | - // Si id_enfant, comparer l'id_objet avec l'id_parent |
|
| 2496 | - // de la boucle superieure |
|
| 2497 | - $val = 'id_parent'; |
|
| 2498 | - } elseif ($crit->cond and ($col == 'date' or $col == 'date_redac')) { |
|
| 2499 | - // un critere conditionnel sur date est traite a part |
|
| 2500 | - // car la date est mise d'office par SPIP, |
|
| 2501 | - $defaut = "(\$Pile[0]['{$col}_default']?'':\$Pile[0]['" . $col . "'])"; |
|
| 2502 | - } |
|
| 2503 | - |
|
| 2504 | - $val = calculer_argument_precedent($idb, $val, $boucles, $defaut); |
|
| 2505 | - $val = [kwote($val)]; |
|
| 2506 | - } |
|
| 2507 | - } else { |
|
| 2508 | - // comparaison explicite |
|
| 2509 | - // le phraseur impose que le premier param soit du texte |
|
| 2510 | - $params = $crit->param; |
|
| 2511 | - $op = $crit->op; |
|
| 2512 | - if ($op == '==') { |
|
| 2513 | - $op = 'REGEXP'; |
|
| 2514 | - } |
|
| 2515 | - $col = array_shift($params); |
|
| 2516 | - $col = $col[0]->texte; |
|
| 2517 | - |
|
| 2518 | - $val = []; |
|
| 2519 | - $parent = $boucles[$idb]->id_parent; |
|
| 2520 | - |
|
| 2521 | - // Dans le cas {x=='#DATE'} etc, defaire le travail du phraseur, |
|
| 2522 | - // celui ne sachant pas ce qu'est un critere infixe |
|
| 2523 | - // et a fortiori son 2e operande qu'entoure " ou ' |
|
| 2524 | - if ( |
|
| 2525 | - count($params) == 1 |
|
| 2526 | - and (is_countable($params[0]) ? count($params[0]) : 0) == 3 |
|
| 2527 | - and $params[0][0]->type == 'texte' |
|
| 2528 | - and $params[0][2]->type == 'texte' |
|
| 2529 | - and ($p = $params[0][0]->texte) == $params[0][2]->texte |
|
| 2530 | - and (($p == "'") or ($p == '"')) |
|
| 2531 | - and $params[0][1]->type == 'champ' |
|
| 2532 | - ) { |
|
| 2533 | - $val[] = "$p\\$p#" . $params[0][1]->nom_champ . "\\$p$p"; |
|
| 2534 | - } else { |
|
| 2535 | - foreach ((($op != 'IN') ? $params : calculer_vieux_in($params)) as $p) { |
|
| 2536 | - $a = calculer_liste($p, $idb, $boucles, $parent); |
|
| 2537 | - if (strcasecmp($op, 'IN') == 0) { |
|
| 2538 | - $val[] = $a; |
|
| 2539 | - } else { |
|
| 2540 | - $val[] = kwote($a, $boucles[$idb]->sql_serveur, '@@defaultcast@@'); |
|
| 2541 | - } // toujours quoter en char ici |
|
| 2542 | - } |
|
| 2543 | - } |
|
| 2544 | - } |
|
| 2545 | - |
|
| 2546 | - $fct = $args_sql = ''; |
|
| 2547 | - // fonction SQL ? |
|
| 2548 | - // chercher FONCTION(champ) tel que CONCAT(titre,descriptif) |
|
| 2549 | - if (preg_match('/^(.*)' . SQL_ARGS . '$/', $col, $m)) { |
|
| 2550 | - $fct = $m[1]; |
|
| 2551 | - preg_match('/^\(([^,]*)(.*)\)$/', $m[2], $a); |
|
| 2552 | - $col = $a[1]; |
|
| 2553 | - if (preg_match('/^(\S*)(\s+AS\s+.*)$/i', $col, $m)) { |
|
| 2554 | - $col = $m[1]; |
|
| 2555 | - $args_sql = $m[2]; |
|
| 2556 | - } |
|
| 2557 | - $args_sql .= $a[2]; |
|
| 2558 | - } |
|
| 2559 | - |
|
| 2560 | - return [$fct, $col, $op, $val, $args_sql]; |
|
| 2475 | + // cas d'une valeur comparee a elle-meme ou son referent |
|
| 2476 | + if (count($crit->param) == 0) { |
|
| 2477 | + $op = '='; |
|
| 2478 | + $col = $val = $crit->op; |
|
| 2479 | + if (preg_match('/^(.*)\.(.*)$/', $col, $r)) { |
|
| 2480 | + $val = $r[2]; |
|
| 2481 | + } |
|
| 2482 | + // Cas special {lang} : aller chercher $GLOBALS['spip_lang'] |
|
| 2483 | + if ($val == 'lang') { |
|
| 2484 | + $val = [kwote('$GLOBALS[\'spip_lang\']')]; |
|
| 2485 | + } else { |
|
| 2486 | + $defaut = null; |
|
| 2487 | + if ($val == 'id_parent') { |
|
| 2488 | + // Si id_parent, comparer l'id_parent avec l'id_objet |
|
| 2489 | + // de la boucle superieure.... faudrait verifier qu'il existe |
|
| 2490 | + // pour eviter l'erreur SQL |
|
| 2491 | + $val = $boucles[$idb]->primary; |
|
| 2492 | + // mais si pas de boucle superieure, prendre id_parent dans l'env |
|
| 2493 | + $defaut = "(\$Pile[0]['id_parent'] ?? null)"; |
|
| 2494 | + } elseif ($val == 'id_enfant') { |
|
| 2495 | + // Si id_enfant, comparer l'id_objet avec l'id_parent |
|
| 2496 | + // de la boucle superieure |
|
| 2497 | + $val = 'id_parent'; |
|
| 2498 | + } elseif ($crit->cond and ($col == 'date' or $col == 'date_redac')) { |
|
| 2499 | + // un critere conditionnel sur date est traite a part |
|
| 2500 | + // car la date est mise d'office par SPIP, |
|
| 2501 | + $defaut = "(\$Pile[0]['{$col}_default']?'':\$Pile[0]['" . $col . "'])"; |
|
| 2502 | + } |
|
| 2503 | + |
|
| 2504 | + $val = calculer_argument_precedent($idb, $val, $boucles, $defaut); |
|
| 2505 | + $val = [kwote($val)]; |
|
| 2506 | + } |
|
| 2507 | + } else { |
|
| 2508 | + // comparaison explicite |
|
| 2509 | + // le phraseur impose que le premier param soit du texte |
|
| 2510 | + $params = $crit->param; |
|
| 2511 | + $op = $crit->op; |
|
| 2512 | + if ($op == '==') { |
|
| 2513 | + $op = 'REGEXP'; |
|
| 2514 | + } |
|
| 2515 | + $col = array_shift($params); |
|
| 2516 | + $col = $col[0]->texte; |
|
| 2517 | + |
|
| 2518 | + $val = []; |
|
| 2519 | + $parent = $boucles[$idb]->id_parent; |
|
| 2520 | + |
|
| 2521 | + // Dans le cas {x=='#DATE'} etc, defaire le travail du phraseur, |
|
| 2522 | + // celui ne sachant pas ce qu'est un critere infixe |
|
| 2523 | + // et a fortiori son 2e operande qu'entoure " ou ' |
|
| 2524 | + if ( |
|
| 2525 | + count($params) == 1 |
|
| 2526 | + and (is_countable($params[0]) ? count($params[0]) : 0) == 3 |
|
| 2527 | + and $params[0][0]->type == 'texte' |
|
| 2528 | + and $params[0][2]->type == 'texte' |
|
| 2529 | + and ($p = $params[0][0]->texte) == $params[0][2]->texte |
|
| 2530 | + and (($p == "'") or ($p == '"')) |
|
| 2531 | + and $params[0][1]->type == 'champ' |
|
| 2532 | + ) { |
|
| 2533 | + $val[] = "$p\\$p#" . $params[0][1]->nom_champ . "\\$p$p"; |
|
| 2534 | + } else { |
|
| 2535 | + foreach ((($op != 'IN') ? $params : calculer_vieux_in($params)) as $p) { |
|
| 2536 | + $a = calculer_liste($p, $idb, $boucles, $parent); |
|
| 2537 | + if (strcasecmp($op, 'IN') == 0) { |
|
| 2538 | + $val[] = $a; |
|
| 2539 | + } else { |
|
| 2540 | + $val[] = kwote($a, $boucles[$idb]->sql_serveur, '@@defaultcast@@'); |
|
| 2541 | + } // toujours quoter en char ici |
|
| 2542 | + } |
|
| 2543 | + } |
|
| 2544 | + } |
|
| 2545 | + |
|
| 2546 | + $fct = $args_sql = ''; |
|
| 2547 | + // fonction SQL ? |
|
| 2548 | + // chercher FONCTION(champ) tel que CONCAT(titre,descriptif) |
|
| 2549 | + if (preg_match('/^(.*)' . SQL_ARGS . '$/', $col, $m)) { |
|
| 2550 | + $fct = $m[1]; |
|
| 2551 | + preg_match('/^\(([^,]*)(.*)\)$/', $m[2], $a); |
|
| 2552 | + $col = $a[1]; |
|
| 2553 | + if (preg_match('/^(\S*)(\s+AS\s+.*)$/i', $col, $m)) { |
|
| 2554 | + $col = $m[1]; |
|
| 2555 | + $args_sql = $m[2]; |
|
| 2556 | + } |
|
| 2557 | + $args_sql .= $a[2]; |
|
| 2558 | + } |
|
| 2559 | + |
|
| 2560 | + return [$fct, $col, $op, $val, $args_sql]; |
|
| 2561 | 2561 | } |
| 2562 | 2562 | |
| 2563 | 2563 | // compatibilite ancienne version |
| 2564 | 2564 | |
| 2565 | 2565 | function calculer_vieux_in($params) { |
| 2566 | - $deb = $params[0][0]; |
|
| 2567 | - $k = (is_countable($params) ? count($params) : 0) - 1; |
|
| 2568 | - $last = $params[$k]; |
|
| 2569 | - $j = (is_countable($last) ? count($last) : 0) - 1; |
|
| 2570 | - $last = $last[$j]; |
|
| 2571 | - $n = isset($last->texte) ? strlen($last->texte) : 0; |
|
| 2572 | - |
|
| 2573 | - if ( |
|
| 2574 | - !((isset($deb->texte[0]) and $deb->texte[0] == '(') |
|
| 2575 | - && (isset($last->texte[$n - 1]) and $last->texte[$n - 1] == ')')) |
|
| 2576 | - ) { |
|
| 2577 | - return $params; |
|
| 2578 | - } |
|
| 2579 | - $params[0][0]->texte = substr($deb->texte, 1); |
|
| 2580 | - // attention, on peut avoir k=0,j=0 ==> recalculer |
|
| 2581 | - $last = $params[$k][$j]; |
|
| 2582 | - $n = strlen($last->texte); |
|
| 2583 | - $params[$k][$j]->texte = substr($last->texte, 0, $n - 1); |
|
| 2584 | - $newp = []; |
|
| 2585 | - foreach ($params as $v) { |
|
| 2586 | - if ($v[0]->type != 'texte') { |
|
| 2587 | - $newp[] = $v; |
|
| 2588 | - } else { |
|
| 2589 | - foreach (explode(',', $v[0]->texte) as $x) { |
|
| 2590 | - $t = new Texte(); |
|
| 2591 | - $t->texte = $x; |
|
| 2592 | - $newp[] = [$t]; |
|
| 2593 | - } |
|
| 2594 | - } |
|
| 2595 | - } |
|
| 2596 | - |
|
| 2597 | - return $newp; |
|
| 2566 | + $deb = $params[0][0]; |
|
| 2567 | + $k = (is_countable($params) ? count($params) : 0) - 1; |
|
| 2568 | + $last = $params[$k]; |
|
| 2569 | + $j = (is_countable($last) ? count($last) : 0) - 1; |
|
| 2570 | + $last = $last[$j]; |
|
| 2571 | + $n = isset($last->texte) ? strlen($last->texte) : 0; |
|
| 2572 | + |
|
| 2573 | + if ( |
|
| 2574 | + !((isset($deb->texte[0]) and $deb->texte[0] == '(') |
|
| 2575 | + && (isset($last->texte[$n - 1]) and $last->texte[$n - 1] == ')')) |
|
| 2576 | + ) { |
|
| 2577 | + return $params; |
|
| 2578 | + } |
|
| 2579 | + $params[0][0]->texte = substr($deb->texte, 1); |
|
| 2580 | + // attention, on peut avoir k=0,j=0 ==> recalculer |
|
| 2581 | + $last = $params[$k][$j]; |
|
| 2582 | + $n = strlen($last->texte); |
|
| 2583 | + $params[$k][$j]->texte = substr($last->texte, 0, $n - 1); |
|
| 2584 | + $newp = []; |
|
| 2585 | + foreach ($params as $v) { |
|
| 2586 | + if ($v[0]->type != 'texte') { |
|
| 2587 | + $newp[] = $v; |
|
| 2588 | + } else { |
|
| 2589 | + foreach (explode(',', $v[0]->texte) as $x) { |
|
| 2590 | + $t = new Texte(); |
|
| 2591 | + $t->texte = $x; |
|
| 2592 | + $newp[] = [$t]; |
|
| 2593 | + } |
|
| 2594 | + } |
|
| 2595 | + } |
|
| 2596 | + |
|
| 2597 | + return $newp; |
|
| 2598 | 2598 | } |
| 2599 | 2599 | |
| 2600 | 2600 | /** |
@@ -2613,95 +2613,95 @@ discard block |
||
| 2613 | 2613 | * - nom de la colonne de date (si le calcul n'est pas relatif) |
| 2614 | 2614 | **/ |
| 2615 | 2615 | function calculer_critere_infixe_date($idb, &$boucles, $col) { |
| 2616 | - if (!preg_match(',^((age|jour|mois|annee)_relatif|date|mois|annee|jour|heure|age)(_[a-z_]+)?$,', $col, $regs)) { |
|
| 2617 | - return ''; |
|
| 2618 | - } |
|
| 2619 | - |
|
| 2620 | - $boucle = $boucles[$idb]; |
|
| 2621 | - $table = $boucle->show; |
|
| 2622 | - |
|
| 2623 | - // si c'est une colonne de la table, ne rien faire |
|
| 2624 | - if (isset($table['field'][$col])) { |
|
| 2625 | - return ''; |
|
| 2626 | - } |
|
| 2627 | - |
|
| 2628 | - // Le type de critère à prendre en compte |
|
| 2629 | - $col = $regs[1]; |
|
| 2630 | - |
|
| 2631 | - // Si on trouve un nom de champ date précis, on l'utilise, pas besoin de déclaration dans l'API objet |
|
| 2632 | - if (isset($regs[3]) and $suite = $regs[3]) { |
|
| 2633 | - # Recherche de l'existence du champ date_xxxx, |
|
| 2634 | - # si oui choisir ce champ, sinon choisir xxxx |
|
| 2635 | - if (isset($table['field']["date$suite"])) { |
|
| 2636 | - $date_orig = 'date' . $suite; |
|
| 2637 | - } else { |
|
| 2638 | - $date_orig = substr($suite, 1); |
|
| 2639 | - } |
|
| 2640 | - |
|
| 2641 | - $pred = $date_orig; |
|
| 2642 | - } else { // Sinon il FAUT avoir déclaré le champ date officiel dans l'API objet |
|
| 2643 | - // Si aucune déclaration trouvée, on quitte |
|
| 2644 | - if (!$table['date'] && !isset($GLOBALS['table_date'][$table['id_table']])) { |
|
| 2645 | - return ''; |
|
| 2646 | - } |
|
| 2647 | - // Par défaut, on prend le champ date déclaré dans l'API |
|
| 2648 | - $pred = $date_orig = $GLOBALS['table_date'][$table['id_table']] ?? $table['date']; |
|
| 2649 | - |
|
| 2650 | - // Si c'est pour du relatif |
|
| 2651 | - if (isset($regs[2]) and $rel = $regs[2]) { |
|
| 2652 | - $pred = 'date'; |
|
| 2653 | - } |
|
| 2654 | - } |
|
| 2655 | - |
|
| 2656 | - $date_compare = "\"' . normaliser_date(" . |
|
| 2657 | - calculer_argument_precedent($idb, $pred, $boucles) . |
|
| 2658 | - ") . '\""; |
|
| 2659 | - |
|
| 2660 | - $col_vraie = $date_orig; |
|
| 2661 | - $date_orig = $boucle->id_table . '.' . $date_orig; |
|
| 2662 | - |
|
| 2663 | - switch ($col) { |
|
| 2664 | - case 'date': |
|
| 2665 | - $col = $date_orig; |
|
| 2666 | - break; |
|
| 2667 | - case 'jour': |
|
| 2668 | - $col = "DAYOFMONTH($date_orig)"; |
|
| 2669 | - break; |
|
| 2670 | - case 'mois': |
|
| 2671 | - $col = "MONTH($date_orig)"; |
|
| 2672 | - break; |
|
| 2673 | - case 'annee': |
|
| 2674 | - $col = "YEAR($date_orig)"; |
|
| 2675 | - break; |
|
| 2676 | - case 'heure': |
|
| 2677 | - $col = "DATE_FORMAT($date_orig, \\'%H:%i\\')"; |
|
| 2678 | - break; |
|
| 2679 | - case 'age': |
|
| 2680 | - $col = calculer_param_date("\'' . date('Y-m-d H:i:00') . '\'", $date_orig); |
|
| 2681 | - $col_vraie = '';// comparer a un int (par defaut) |
|
| 2682 | - break; |
|
| 2683 | - case 'age_relatif': |
|
| 2684 | - $col = calculer_param_date($date_compare, $date_orig); |
|
| 2685 | - $col_vraie = '';// comparer a un int (par defaut) |
|
| 2686 | - break; |
|
| 2687 | - case 'jour_relatif': |
|
| 2688 | - $col = '(TO_DAYS(' . $date_compare . ')-TO_DAYS(' . $date_orig . '))'; |
|
| 2689 | - $col_vraie = '';// comparer a un int (par defaut) |
|
| 2690 | - break; |
|
| 2691 | - case 'mois_relatif': |
|
| 2692 | - $col = 'MONTH(' . $date_compare . ')-MONTH(' . |
|
| 2693 | - $date_orig . ')+12*(YEAR(' . $date_compare . |
|
| 2694 | - ')-YEAR(' . $date_orig . '))'; |
|
| 2695 | - $col_vraie = '';// comparer a un int (par defaut) |
|
| 2696 | - break; |
|
| 2697 | - case 'annee_relatif': |
|
| 2698 | - $col = 'YEAR(' . $date_compare . ')-YEAR(' . |
|
| 2699 | - $date_orig . ')'; |
|
| 2700 | - $col_vraie = '';// comparer a un int (par defaut) |
|
| 2701 | - break; |
|
| 2702 | - } |
|
| 2703 | - |
|
| 2704 | - return [$col, $col_vraie]; |
|
| 2616 | + if (!preg_match(',^((age|jour|mois|annee)_relatif|date|mois|annee|jour|heure|age)(_[a-z_]+)?$,', $col, $regs)) { |
|
| 2617 | + return ''; |
|
| 2618 | + } |
|
| 2619 | + |
|
| 2620 | + $boucle = $boucles[$idb]; |
|
| 2621 | + $table = $boucle->show; |
|
| 2622 | + |
|
| 2623 | + // si c'est une colonne de la table, ne rien faire |
|
| 2624 | + if (isset($table['field'][$col])) { |
|
| 2625 | + return ''; |
|
| 2626 | + } |
|
| 2627 | + |
|
| 2628 | + // Le type de critère à prendre en compte |
|
| 2629 | + $col = $regs[1]; |
|
| 2630 | + |
|
| 2631 | + // Si on trouve un nom de champ date précis, on l'utilise, pas besoin de déclaration dans l'API objet |
|
| 2632 | + if (isset($regs[3]) and $suite = $regs[3]) { |
|
| 2633 | + # Recherche de l'existence du champ date_xxxx, |
|
| 2634 | + # si oui choisir ce champ, sinon choisir xxxx |
|
| 2635 | + if (isset($table['field']["date$suite"])) { |
|
| 2636 | + $date_orig = 'date' . $suite; |
|
| 2637 | + } else { |
|
| 2638 | + $date_orig = substr($suite, 1); |
|
| 2639 | + } |
|
| 2640 | + |
|
| 2641 | + $pred = $date_orig; |
|
| 2642 | + } else { // Sinon il FAUT avoir déclaré le champ date officiel dans l'API objet |
|
| 2643 | + // Si aucune déclaration trouvée, on quitte |
|
| 2644 | + if (!$table['date'] && !isset($GLOBALS['table_date'][$table['id_table']])) { |
|
| 2645 | + return ''; |
|
| 2646 | + } |
|
| 2647 | + // Par défaut, on prend le champ date déclaré dans l'API |
|
| 2648 | + $pred = $date_orig = $GLOBALS['table_date'][$table['id_table']] ?? $table['date']; |
|
| 2649 | + |
|
| 2650 | + // Si c'est pour du relatif |
|
| 2651 | + if (isset($regs[2]) and $rel = $regs[2]) { |
|
| 2652 | + $pred = 'date'; |
|
| 2653 | + } |
|
| 2654 | + } |
|
| 2655 | + |
|
| 2656 | + $date_compare = "\"' . normaliser_date(" . |
|
| 2657 | + calculer_argument_precedent($idb, $pred, $boucles) . |
|
| 2658 | + ") . '\""; |
|
| 2659 | + |
|
| 2660 | + $col_vraie = $date_orig; |
|
| 2661 | + $date_orig = $boucle->id_table . '.' . $date_orig; |
|
| 2662 | + |
|
| 2663 | + switch ($col) { |
|
| 2664 | + case 'date': |
|
| 2665 | + $col = $date_orig; |
|
| 2666 | + break; |
|
| 2667 | + case 'jour': |
|
| 2668 | + $col = "DAYOFMONTH($date_orig)"; |
|
| 2669 | + break; |
|
| 2670 | + case 'mois': |
|
| 2671 | + $col = "MONTH($date_orig)"; |
|
| 2672 | + break; |
|
| 2673 | + case 'annee': |
|
| 2674 | + $col = "YEAR($date_orig)"; |
|
| 2675 | + break; |
|
| 2676 | + case 'heure': |
|
| 2677 | + $col = "DATE_FORMAT($date_orig, \\'%H:%i\\')"; |
|
| 2678 | + break; |
|
| 2679 | + case 'age': |
|
| 2680 | + $col = calculer_param_date("\'' . date('Y-m-d H:i:00') . '\'", $date_orig); |
|
| 2681 | + $col_vraie = '';// comparer a un int (par defaut) |
|
| 2682 | + break; |
|
| 2683 | + case 'age_relatif': |
|
| 2684 | + $col = calculer_param_date($date_compare, $date_orig); |
|
| 2685 | + $col_vraie = '';// comparer a un int (par defaut) |
|
| 2686 | + break; |
|
| 2687 | + case 'jour_relatif': |
|
| 2688 | + $col = '(TO_DAYS(' . $date_compare . ')-TO_DAYS(' . $date_orig . '))'; |
|
| 2689 | + $col_vraie = '';// comparer a un int (par defaut) |
|
| 2690 | + break; |
|
| 2691 | + case 'mois_relatif': |
|
| 2692 | + $col = 'MONTH(' . $date_compare . ')-MONTH(' . |
|
| 2693 | + $date_orig . ')+12*(YEAR(' . $date_compare . |
|
| 2694 | + ')-YEAR(' . $date_orig . '))'; |
|
| 2695 | + $col_vraie = '';// comparer a un int (par defaut) |
|
| 2696 | + break; |
|
| 2697 | + case 'annee_relatif': |
|
| 2698 | + $col = 'YEAR(' . $date_compare . ')-YEAR(' . |
|
| 2699 | + $date_orig . ')'; |
|
| 2700 | + $col_vraie = '';// comparer a un int (par defaut) |
|
| 2701 | + break; |
|
| 2702 | + } |
|
| 2703 | + |
|
| 2704 | + return [$col, $col_vraie]; |
|
| 2705 | 2705 | } |
| 2706 | 2706 | |
| 2707 | 2707 | /** |
@@ -2720,16 +2720,16 @@ discard block |
||
| 2720 | 2720 | * de colonne SQL et une date. |
| 2721 | 2721 | **/ |
| 2722 | 2722 | function calculer_param_date($date_compare, $date_orig) { |
| 2723 | - if (preg_match(",'\" *\.(.*)\. *\"',", $date_compare, $r)) { |
|
| 2724 | - $init = "'\" . (\$x = $r[1]) . \"'"; |
|
| 2725 | - $date_compare = '\'$x\''; |
|
| 2726 | - } else { |
|
| 2727 | - $init = $date_compare; |
|
| 2728 | - } |
|
| 2729 | - |
|
| 2730 | - return |
|
| 2731 | - // optimisation : mais prevoir le support SQLite avant |
|
| 2732 | - "TIMESTAMPDIFF(HOUR,$date_orig,$init)/24"; |
|
| 2723 | + if (preg_match(",'\" *\.(.*)\. *\"',", $date_compare, $r)) { |
|
| 2724 | + $init = "'\" . (\$x = $r[1]) . \"'"; |
|
| 2725 | + $date_compare = '\'$x\''; |
|
| 2726 | + } else { |
|
| 2727 | + $init = $date_compare; |
|
| 2728 | + } |
|
| 2729 | + |
|
| 2730 | + return |
|
| 2731 | + // optimisation : mais prevoir le support SQLite avant |
|
| 2732 | + "TIMESTAMPDIFF(HOUR,$date_orig,$init)/24"; |
|
| 2733 | 2733 | } |
| 2734 | 2734 | |
| 2735 | 2735 | /** |
@@ -2747,20 +2747,20 @@ discard block |
||
| 2747 | 2747 | * @param Critere $crit Paramètres du critère dans cette boucle |
| 2748 | 2748 | */ |
| 2749 | 2749 | function critere_DATA_source_dist($idb, &$boucles, $crit) { |
| 2750 | - $boucle = &$boucles[$idb]; |
|
| 2751 | - |
|
| 2752 | - $args = []; |
|
| 2753 | - foreach ($crit->param as &$param) { |
|
| 2754 | - array_push( |
|
| 2755 | - $args, |
|
| 2756 | - calculer_liste($param, $idb, $boucles, $boucles[$idb]->id_parent) |
|
| 2757 | - ); |
|
| 2758 | - } |
|
| 2750 | + $boucle = &$boucles[$idb]; |
|
| 2759 | 2751 | |
| 2760 | - $boucle->hash .= ' |
|
| 2752 | + $args = []; |
|
| 2753 | + foreach ($crit->param as &$param) { |
|
| 2754 | + array_push( |
|
| 2755 | + $args, |
|
| 2756 | + calculer_liste($param, $idb, $boucles, $boucles[$idb]->id_parent) |
|
| 2757 | + ); |
|
| 2758 | + } |
|
| 2759 | + |
|
| 2760 | + $boucle->hash .= ' |
|
| 2761 | 2761 | $command[\'sourcemode\'] = ' . array_shift($args) . ";\n"; |
| 2762 | 2762 | |
| 2763 | - $boucle->hash .= ' |
|
| 2763 | + $boucle->hash .= ' |
|
| 2764 | 2764 | $command[\'source\'] = array(' . join(', ', $args) . ");\n"; |
| 2765 | 2765 | } |
| 2766 | 2766 | |
@@ -2778,8 +2778,8 @@ discard block |
||
| 2778 | 2778 | * @param Critere $crit Paramètres du critère dans cette boucle |
| 2779 | 2779 | */ |
| 2780 | 2780 | function critere_DATA_datacache_dist($idb, &$boucles, $crit) { |
| 2781 | - $boucle = &$boucles[$idb]; |
|
| 2782 | - $boucle->hash .= ' |
|
| 2781 | + $boucle = &$boucles[$idb]; |
|
| 2782 | + $boucle->hash .= ' |
|
| 2783 | 2783 | $command[\'datacache\'] = ' . calculer_liste($crit->param[0], $idb, $boucles, $boucles[$idb]->id_parent) . ';'; |
| 2784 | 2784 | } |
| 2785 | 2785 | |
@@ -2795,12 +2795,12 @@ discard block |
||
| 2795 | 2795 | * @param Critere $crit Paramètres du critère dans cette boucle |
| 2796 | 2796 | */ |
| 2797 | 2797 | function critere_php_args_dist($idb, &$boucles, $crit) { |
| 2798 | - $boucle = &$boucles[$idb]; |
|
| 2799 | - $boucle->hash .= '$command[\'args\']=array();'; |
|
| 2800 | - foreach ($crit->param as $param) { |
|
| 2801 | - $boucle->hash .= ' |
|
| 2798 | + $boucle = &$boucles[$idb]; |
|
| 2799 | + $boucle->hash .= '$command[\'args\']=array();'; |
|
| 2800 | + foreach ($crit->param as $param) { |
|
| 2801 | + $boucle->hash .= ' |
|
| 2802 | 2802 | $command[\'args\'][] = ' . calculer_liste($param, $idb, $boucles, $boucles[$idb]->id_parent) . ';'; |
| 2803 | - } |
|
| 2803 | + } |
|
| 2804 | 2804 | } |
| 2805 | 2805 | |
| 2806 | 2806 | /** |
@@ -2817,16 +2817,16 @@ discard block |
||
| 2817 | 2817 | * @param Critere $crit Paramètres du critère dans cette boucle |
| 2818 | 2818 | */ |
| 2819 | 2819 | function critere_DATA_liste_dist($idb, &$boucles, $crit) { |
| 2820 | - $boucle = &$boucles[$idb]; |
|
| 2821 | - $boucle->hash .= "\n\t" . '$command[\'liste\'] = array();' . "\n"; |
|
| 2822 | - foreach ($crit->param as $param) { |
|
| 2823 | - $boucle->hash .= "\t" . '$command[\'liste\'][] = ' . calculer_liste( |
|
| 2824 | - $param, |
|
| 2825 | - $idb, |
|
| 2826 | - $boucles, |
|
| 2827 | - $boucles[$idb]->id_parent |
|
| 2828 | - ) . ";\n"; |
|
| 2829 | - } |
|
| 2820 | + $boucle = &$boucles[$idb]; |
|
| 2821 | + $boucle->hash .= "\n\t" . '$command[\'liste\'] = array();' . "\n"; |
|
| 2822 | + foreach ($crit->param as $param) { |
|
| 2823 | + $boucle->hash .= "\t" . '$command[\'liste\'][] = ' . calculer_liste( |
|
| 2824 | + $param, |
|
| 2825 | + $idb, |
|
| 2826 | + $boucles, |
|
| 2827 | + $boucles[$idb]->id_parent |
|
| 2828 | + ) . ";\n"; |
|
| 2829 | + } |
|
| 2830 | 2830 | } |
| 2831 | 2831 | |
| 2832 | 2832 | /** |
@@ -2851,16 +2851,16 @@ discard block |
||
| 2851 | 2851 | * @param Critere $crit Paramètres du critère dans cette boucle |
| 2852 | 2852 | */ |
| 2853 | 2853 | function critere_DATA_enum_dist($idb, &$boucles, $crit) { |
| 2854 | - $boucle = &$boucles[$idb]; |
|
| 2855 | - $boucle->hash .= "\n\t" . '$command[\'enum\'] = array();' . "\n"; |
|
| 2856 | - foreach ($crit->param as $param) { |
|
| 2857 | - $boucle->hash .= "\t" . '$command[\'enum\'][] = ' . calculer_liste( |
|
| 2858 | - $param, |
|
| 2859 | - $idb, |
|
| 2860 | - $boucles, |
|
| 2861 | - $boucles[$idb]->id_parent |
|
| 2862 | - ) . ";\n"; |
|
| 2863 | - } |
|
| 2854 | + $boucle = &$boucles[$idb]; |
|
| 2855 | + $boucle->hash .= "\n\t" . '$command[\'enum\'] = array();' . "\n"; |
|
| 2856 | + foreach ($crit->param as $param) { |
|
| 2857 | + $boucle->hash .= "\t" . '$command[\'enum\'][] = ' . calculer_liste( |
|
| 2858 | + $param, |
|
| 2859 | + $idb, |
|
| 2860 | + $boucles, |
|
| 2861 | + $boucles[$idb]->id_parent |
|
| 2862 | + ) . ";\n"; |
|
| 2863 | + } |
|
| 2864 | 2864 | } |
| 2865 | 2865 | |
| 2866 | 2866 | /** |
@@ -2875,11 +2875,11 @@ discard block |
||
| 2875 | 2875 | * @param Critere $crit Paramètres du critère dans cette boucle |
| 2876 | 2876 | */ |
| 2877 | 2877 | function critere_DATA_datapath_dist($idb, &$boucles, $crit) { |
| 2878 | - $boucle = &$boucles[$idb]; |
|
| 2879 | - foreach ($crit->param as $param) { |
|
| 2880 | - $boucle->hash .= ' |
|
| 2878 | + $boucle = &$boucles[$idb]; |
|
| 2879 | + foreach ($crit->param as $param) { |
|
| 2880 | + $boucle->hash .= ' |
|
| 2881 | 2881 | $command[\'datapath\'][] = ' . calculer_liste($param, $idb, $boucles, $boucles[$idb]->id_parent) . ';'; |
| 2882 | - } |
|
| 2882 | + } |
|
| 2883 | 2883 | } |
| 2884 | 2884 | |
| 2885 | 2885 | |
@@ -2911,20 +2911,20 @@ discard block |
||
| 2911 | 2911 | * @param Critere $crit Paramètres du critère dans cette boucle |
| 2912 | 2912 | */ |
| 2913 | 2913 | function critere_si_dist($idb, &$boucles, $crit) { |
| 2914 | - $boucle = &$boucles[$idb]; |
|
| 2915 | - // il faut initialiser 1 fois le tableau a chaque appel de la boucle |
|
| 2916 | - // (par exemple lorsque notre boucle est appelee dans une autre boucle) |
|
| 2917 | - // mais ne pas l'initialiser n fois si il y a n criteres {si } dans la boucle ! |
|
| 2918 | - $boucle->hash .= "\n\tif (!isset(\$si_init)) { \$command['si'] = array(); \$si_init = true; }\n"; |
|
| 2919 | - if ($crit->param) { |
|
| 2920 | - foreach ($crit->param as $param) { |
|
| 2921 | - $boucle->hash .= "\t\$command['si'][] = " |
|
| 2922 | - . calculer_liste($param, $idb, $boucles, $boucles[$idb]->id_parent) . ";\n"; |
|
| 2923 | - } |
|
| 2924 | - // interdire {si 0} aussi ! |
|
| 2925 | - } else { |
|
| 2926 | - $boucle->hash .= '$command[\'si\'][] = 0;'; |
|
| 2927 | - } |
|
| 2914 | + $boucle = &$boucles[$idb]; |
|
| 2915 | + // il faut initialiser 1 fois le tableau a chaque appel de la boucle |
|
| 2916 | + // (par exemple lorsque notre boucle est appelee dans une autre boucle) |
|
| 2917 | + // mais ne pas l'initialiser n fois si il y a n criteres {si } dans la boucle ! |
|
| 2918 | + $boucle->hash .= "\n\tif (!isset(\$si_init)) { \$command['si'] = array(); \$si_init = true; }\n"; |
|
| 2919 | + if ($crit->param) { |
|
| 2920 | + foreach ($crit->param as $param) { |
|
| 2921 | + $boucle->hash .= "\t\$command['si'][] = " |
|
| 2922 | + . calculer_liste($param, $idb, $boucles, $boucles[$idb]->id_parent) . ";\n"; |
|
| 2923 | + } |
|
| 2924 | + // interdire {si 0} aussi ! |
|
| 2925 | + } else { |
|
| 2926 | + $boucle->hash .= '$command[\'si\'][] = 0;'; |
|
| 2927 | + } |
|
| 2928 | 2928 | } |
| 2929 | 2929 | |
| 2930 | 2930 | /** |
@@ -2941,8 +2941,8 @@ discard block |
||
| 2941 | 2941 | * @param Critere $crit Paramètres du critère dans cette boucle |
| 2942 | 2942 | */ |
| 2943 | 2943 | function critere_POUR_tableau_dist($idb, &$boucles, $crit) { |
| 2944 | - $boucle = &$boucles[$idb]; |
|
| 2945 | - $boucle->hash .= ' |
|
| 2944 | + $boucle = &$boucles[$idb]; |
|
| 2945 | + $boucle->hash .= ' |
|
| 2946 | 2946 | $command[\'source\'] = array(' . calculer_liste($crit->param[0], $idb, $boucles, $boucles[$idb]->id_parent) . '); |
| 2947 | 2947 | $command[\'sourcemode\'] = \'table\';'; |
| 2948 | 2948 | } |
@@ -2963,27 +2963,27 @@ discard block |
||
| 2963 | 2963 | */ |
| 2964 | 2964 | function critere_noeud_dist($idb, &$boucles, $crit) { |
| 2965 | 2965 | |
| 2966 | - $not = $crit->not; |
|
| 2967 | - $boucle = &$boucles[$idb]; |
|
| 2968 | - $primary = $boucle->primary; |
|
| 2966 | + $not = $crit->not; |
|
| 2967 | + $boucle = &$boucles[$idb]; |
|
| 2968 | + $primary = $boucle->primary; |
|
| 2969 | 2969 | |
| 2970 | - if (!$primary or strpos($primary, ',')) { |
|
| 2971 | - erreur_squelette(_T('zbug_doublon_sur_table_sans_cle_primaire'), $boucle); |
|
| 2970 | + if (!$primary or strpos($primary, ',')) { |
|
| 2971 | + erreur_squelette(_T('zbug_doublon_sur_table_sans_cle_primaire'), $boucle); |
|
| 2972 | 2972 | |
| 2973 | - return; |
|
| 2974 | - } |
|
| 2975 | - $table = $boucle->type_requete; |
|
| 2976 | - $table_sql = table_objet_sql(objet_type($table)); |
|
| 2973 | + return; |
|
| 2974 | + } |
|
| 2975 | + $table = $boucle->type_requete; |
|
| 2976 | + $table_sql = table_objet_sql(objet_type($table)); |
|
| 2977 | 2977 | |
| 2978 | - $id_parent = $GLOBALS['exceptions_des_tables'][$boucle->id_table]['id_parent'] ?? 'id_parent'; |
|
| 2978 | + $id_parent = $GLOBALS['exceptions_des_tables'][$boucle->id_table]['id_parent'] ?? 'id_parent'; |
|
| 2979 | 2979 | |
| 2980 | - $in = 'IN'; |
|
| 2981 | - $where = ["'IN'", "'$boucle->id_table." . "$primary'", "'('.sql_get_select('$id_parent', '$table_sql').')'"]; |
|
| 2982 | - if ($not) { |
|
| 2983 | - $where = ["'NOT'", $where]; |
|
| 2984 | - } |
|
| 2980 | + $in = 'IN'; |
|
| 2981 | + $where = ["'IN'", "'$boucle->id_table." . "$primary'", "'('.sql_get_select('$id_parent', '$table_sql').')'"]; |
|
| 2982 | + if ($not) { |
|
| 2983 | + $where = ["'NOT'", $where]; |
|
| 2984 | + } |
|
| 2985 | 2985 | |
| 2986 | - $boucle->where[] = $where; |
|
| 2986 | + $boucle->where[] = $where; |
|
| 2987 | 2987 | } |
| 2988 | 2988 | |
| 2989 | 2989 | /** |
@@ -2999,8 +2999,8 @@ discard block |
||
| 2999 | 2999 | * @param Critere $crit Paramètres du critère dans cette boucle |
| 3000 | 3000 | */ |
| 3001 | 3001 | function critere_feuille_dist($idb, &$boucles, $crit) { |
| 3002 | - $not = $crit->not; |
|
| 3003 | - $crit->not = $not ? false : true; |
|
| 3004 | - critere_noeud_dist($idb, $boucles, $crit); |
|
| 3005 | - $crit->not = $not; |
|
| 3002 | + $not = $crit->not; |
|
| 3003 | + $crit->not = $not ? false : true; |
|
| 3004 | + critere_noeud_dist($idb, $boucles, $crit); |
|
| 3005 | + $crit->not = $not; |
|
| 3006 | 3006 | } |
@@ -20,7 +20,7 @@ discard block |
||
| 20 | 20 | */ |
| 21 | 21 | |
| 22 | 22 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 23 | - return; |
|
| 23 | + return; |
|
| 24 | 24 | } |
| 25 | 25 | |
| 26 | 26 | // TODO: get/set_caracteres ? |
@@ -44,91 +44,91 @@ discard block |
||
| 44 | 44 | */ |
| 45 | 45 | function req_sqlite_dist($addr, $port, $login, $pass, $db = '', $prefixe = '', $sqlite_version = '') |
| 46 | 46 | { |
| 47 | - static $last_connect = []; |
|
| 48 | - |
|
| 49 | - // si provient de selectdb |
|
| 50 | - // un code pour etre sur que l'on vient de select_db() |
|
| 51 | - if (strpos($db, $code = '@selectdb@') !== false) { |
|
| 52 | - foreach (['addr', 'port', 'login', 'pass', 'prefixe'] as $a) { |
|
| 53 | - $$a = $last_connect[$a]; |
|
| 54 | - } |
|
| 55 | - $db = str_replace($code, '', $db); |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /* |
|
| 47 | + static $last_connect = []; |
|
| 48 | + |
|
| 49 | + // si provient de selectdb |
|
| 50 | + // un code pour etre sur que l'on vient de select_db() |
|
| 51 | + if (strpos($db, $code = '@selectdb@') !== false) { |
|
| 52 | + foreach (['addr', 'port', 'login', 'pass', 'prefixe'] as $a) { |
|
| 53 | + $$a = $last_connect[$a]; |
|
| 54 | + } |
|
| 55 | + $db = str_replace($code, '', $db); |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /* |
|
| 59 | 59 | * En sqlite, seule l'adresse du fichier est importante. |
| 60 | 60 | * Ce sera $db le nom, |
| 61 | 61 | * le path est $addr |
| 62 | 62 | * (_DIR_DB si $addr est vide) |
| 63 | 63 | */ |
| 64 | - _sqlite_init(); |
|
| 65 | - |
|
| 66 | - // determiner le dossier de la base : $addr ou _DIR_DB |
|
| 67 | - $f = _DIR_DB; |
|
| 68 | - if ($addr and str_contains($addr, '/')) { |
|
| 69 | - $f = rtrim($addr, '/') . '/'; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - // un nom de base demande et impossible d'obtenir la base, on s'en va : |
|
| 73 | - // il faut que la base existe ou que le repertoire parent soit writable |
|
| 74 | - if ($db and !is_file($f .= $db . '.sqlite') and !is_writable(dirname($f))) { |
|
| 75 | - spip_log("base $f non trouvee ou droits en ecriture manquants", 'sqlite.' . _LOG_HS); |
|
| 76 | - |
|
| 77 | - return false; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - // charger les modules sqlite au besoin |
|
| 81 | - if (!_sqlite_charger_version($sqlite_version)) { |
|
| 82 | - spip_log("Impossible de trouver/charger le module SQLite ($sqlite_version)!", 'sqlite.' . _LOG_HS); |
|
| 83 | - |
|
| 84 | - return false; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - // chargement des constantes |
|
| 88 | - // il ne faut pas definir les constantes avant d'avoir charge les modules sqlite |
|
| 89 | - $define = 'spip_sqlite' . $sqlite_version . '_constantes'; |
|
| 90 | - $define(); |
|
| 91 | - |
|
| 92 | - $ok = false; |
|
| 93 | - if (!$db) { |
|
| 94 | - // si pas de db -> |
|
| 95 | - // base temporaire tant qu'on ne connait pas son vrai nom |
|
| 96 | - // pour tester la connexion |
|
| 97 | - $db = '_sqlite' . $sqlite_version . '_install'; |
|
| 98 | - $tmp = _DIR_DB . $db . '.sqlite'; |
|
| 99 | - $ok = $link = new \PDO("sqlite:$tmp"); |
|
| 100 | - } else { |
|
| 101 | - // Ouvrir (eventuellement creer la base) |
|
| 102 | - $ok = $link = new \PDO("sqlite:$f"); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - if (!$ok) { |
|
| 106 | - $e = _sqlite_last_error_from_link($link); |
|
| 107 | - spip_log("Impossible d'ouvrir la base SQLite($sqlite_version) $f : $e", 'sqlite.' . _LOG_HS); |
|
| 108 | - |
|
| 109 | - return false; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - if ($link) { |
|
| 113 | - $last_connect = [ |
|
| 114 | - 'addr' => $addr, |
|
| 115 | - 'port' => $port, |
|
| 116 | - 'login' => $login, |
|
| 117 | - 'pass' => $pass, |
|
| 118 | - 'db' => $db, |
|
| 119 | - 'prefixe' => $prefixe, |
|
| 120 | - ]; |
|
| 121 | - // etre sur qu'on definit bien les fonctions a chaque nouvelle connexion |
|
| 122 | - include_spip('req/sqlite_fonctions'); |
|
| 123 | - _sqlite_init_functions($link); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - return [ |
|
| 127 | - 'db' => $db, |
|
| 128 | - 'prefixe' => $prefixe ? $prefixe : $db, |
|
| 129 | - 'link' => $link, |
|
| 130 | - 'total_requetes' => 0, |
|
| 131 | - ]; |
|
| 64 | + _sqlite_init(); |
|
| 65 | + |
|
| 66 | + // determiner le dossier de la base : $addr ou _DIR_DB |
|
| 67 | + $f = _DIR_DB; |
|
| 68 | + if ($addr and str_contains($addr, '/')) { |
|
| 69 | + $f = rtrim($addr, '/') . '/'; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + // un nom de base demande et impossible d'obtenir la base, on s'en va : |
|
| 73 | + // il faut que la base existe ou que le repertoire parent soit writable |
|
| 74 | + if ($db and !is_file($f .= $db . '.sqlite') and !is_writable(dirname($f))) { |
|
| 75 | + spip_log("base $f non trouvee ou droits en ecriture manquants", 'sqlite.' . _LOG_HS); |
|
| 76 | + |
|
| 77 | + return false; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + // charger les modules sqlite au besoin |
|
| 81 | + if (!_sqlite_charger_version($sqlite_version)) { |
|
| 82 | + spip_log("Impossible de trouver/charger le module SQLite ($sqlite_version)!", 'sqlite.' . _LOG_HS); |
|
| 83 | + |
|
| 84 | + return false; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + // chargement des constantes |
|
| 88 | + // il ne faut pas definir les constantes avant d'avoir charge les modules sqlite |
|
| 89 | + $define = 'spip_sqlite' . $sqlite_version . '_constantes'; |
|
| 90 | + $define(); |
|
| 91 | + |
|
| 92 | + $ok = false; |
|
| 93 | + if (!$db) { |
|
| 94 | + // si pas de db -> |
|
| 95 | + // base temporaire tant qu'on ne connait pas son vrai nom |
|
| 96 | + // pour tester la connexion |
|
| 97 | + $db = '_sqlite' . $sqlite_version . '_install'; |
|
| 98 | + $tmp = _DIR_DB . $db . '.sqlite'; |
|
| 99 | + $ok = $link = new \PDO("sqlite:$tmp"); |
|
| 100 | + } else { |
|
| 101 | + // Ouvrir (eventuellement creer la base) |
|
| 102 | + $ok = $link = new \PDO("sqlite:$f"); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + if (!$ok) { |
|
| 106 | + $e = _sqlite_last_error_from_link($link); |
|
| 107 | + spip_log("Impossible d'ouvrir la base SQLite($sqlite_version) $f : $e", 'sqlite.' . _LOG_HS); |
|
| 108 | + |
|
| 109 | + return false; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + if ($link) { |
|
| 113 | + $last_connect = [ |
|
| 114 | + 'addr' => $addr, |
|
| 115 | + 'port' => $port, |
|
| 116 | + 'login' => $login, |
|
| 117 | + 'pass' => $pass, |
|
| 118 | + 'db' => $db, |
|
| 119 | + 'prefixe' => $prefixe, |
|
| 120 | + ]; |
|
| 121 | + // etre sur qu'on definit bien les fonctions a chaque nouvelle connexion |
|
| 122 | + include_spip('req/sqlite_fonctions'); |
|
| 123 | + _sqlite_init_functions($link); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + return [ |
|
| 127 | + 'db' => $db, |
|
| 128 | + 'prefixe' => $prefixe ? $prefixe : $db, |
|
| 129 | + 'link' => $link, |
|
| 130 | + 'total_requetes' => 0, |
|
| 131 | + ]; |
|
| 132 | 132 | } |
| 133 | 133 | |
| 134 | 134 | |
@@ -148,14 +148,14 @@ discard block |
||
| 148 | 148 | */ |
| 149 | 149 | function spip_sqlite_query($query, $serveur = '', $requeter = true) |
| 150 | 150 | { |
| 151 | - #spip_log("spip_sqlite_query() > $query",'sqlite.'._LOG_DEBUG); |
|
| 152 | - #_sqlite_init(); // fait la premiere fois dans spip_sqlite |
|
| 153 | - $query = Sqlite::traduire_requete($query, $serveur); |
|
| 154 | - if (!$requeter) { |
|
| 155 | - return $query; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - return Sqlite::executer_requete($query, $serveur); |
|
| 151 | + #spip_log("spip_sqlite_query() > $query",'sqlite.'._LOG_DEBUG); |
|
| 152 | + #_sqlite_init(); // fait la premiere fois dans spip_sqlite |
|
| 153 | + $query = Sqlite::traduire_requete($query, $serveur); |
|
| 154 | + if (!$requeter) { |
|
| 155 | + return $query; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + return Sqlite::executer_requete($query, $serveur); |
|
| 159 | 159 | } |
| 160 | 160 | |
| 161 | 161 | |
@@ -173,11 +173,11 @@ discard block |
||
| 173 | 173 | function spip_sqlite_alter($query, $serveur = '', $requeter = true) |
| 174 | 174 | { |
| 175 | 175 | |
| 176 | - $query = spip_sqlite_query("ALTER $query", $serveur, false); |
|
| 177 | - // traduire la requete pour recuperer les bons noms de table |
|
| 178 | - $query = Sqlite::traduire_requete($query, $serveur); |
|
| 176 | + $query = spip_sqlite_query("ALTER $query", $serveur, false); |
|
| 177 | + // traduire la requete pour recuperer les bons noms de table |
|
| 178 | + $query = Sqlite::traduire_requete($query, $serveur); |
|
| 179 | 179 | |
| 180 | - /* |
|
| 180 | + /* |
|
| 181 | 181 | * la il faut faire les transformations |
| 182 | 182 | * si ALTER TABLE x (DROP|CHANGE) y |
| 183 | 183 | * |
@@ -186,251 +186,251 @@ discard block |
||
| 186 | 186 | * 3) faire chaque requete independemment |
| 187 | 187 | */ |
| 188 | 188 | |
| 189 | - // 1 |
|
| 190 | - if (preg_match('/\s*(ALTER(\s*IGNORE)?\s*TABLE\s*([^\s]*))\s*(.*)?/is', $query, $regs)) { |
|
| 191 | - $debut = $regs[1]; |
|
| 192 | - $table = $regs[3]; |
|
| 193 | - $suite = $regs[4]; |
|
| 194 | - } else { |
|
| 195 | - spip_log("SQLite : Probleme de ALTER TABLE mal forme dans $query", 'sqlite.' . _LOG_ERREUR); |
|
| 196 | - |
|
| 197 | - return false; |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - // 2 |
|
| 201 | - // il faudrait une regexp pour eviter de spliter ADD PRIMARY KEY (colA, colB) |
|
| 202 | - // tout en cassant "ADD PRIMARY KEY (colA, colB), ADD INDEX (chose)"... en deux |
|
| 203 | - // ou revoir l'api de sql_alter en creant un |
|
| 204 | - // sql_alter_table($table,array($actions)); |
|
| 205 | - $todo = explode(',', $suite); |
|
| 206 | - |
|
| 207 | - // on remet les morceaux dechires ensembles... que c'est laid ! |
|
| 208 | - $todo2 = []; |
|
| 209 | - $i = 0; |
|
| 210 | - $ouverte = false; |
|
| 211 | - while ($do = array_shift($todo)) { |
|
| 212 | - $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . ',' . $do : $do; |
|
| 213 | - $o = (str_contains($do, '(')); |
|
| 214 | - $f = (str_contains($do, ')')); |
|
| 215 | - if ($o and !$f) { |
|
| 216 | - $ouverte = true; |
|
| 217 | - } elseif ($f) { |
|
| 218 | - $ouverte = false; |
|
| 219 | - } |
|
| 220 | - if (!$ouverte) { |
|
| 221 | - $i++; |
|
| 222 | - } |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - // 3 |
|
| 226 | - $resultats = []; |
|
| 227 | - foreach ($todo2 as $do) { |
|
| 228 | - $do = trim($do); |
|
| 229 | - if ( |
|
| 230 | - !preg_match('/(DROP PRIMARY KEY|DROP KEY|DROP INDEX|DROP COLUMN|DROP' |
|
| 231 | - . '|CHANGE COLUMN|CHANGE|MODIFY|RENAME TO|RENAME' |
|
| 232 | - . '|ADD PRIMARY KEY|ADD KEY|ADD INDEX|ADD UNIQUE KEY|ADD UNIQUE' |
|
| 233 | - . '|ADD COLUMN|ADD' |
|
| 234 | - . ')\s*([^\s]*)\s*(.*)?/i', $do, $matches) |
|
| 235 | - ) { |
|
| 236 | - spip_log( |
|
| 237 | - "SQLite : Probleme de ALTER TABLE, utilisation non reconnue dans : $do \n(requete d'origine : $query)", |
|
| 238 | - 'sqlite.' . _LOG_ERREUR |
|
| 239 | - ); |
|
| 240 | - |
|
| 241 | - return false; |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - $cle = strtoupper($matches[1]); |
|
| 245 | - $colonne_origine = $matches[2]; |
|
| 246 | - $colonne_destination = ''; |
|
| 247 | - |
|
| 248 | - $def = $matches[3]; |
|
| 249 | - |
|
| 250 | - // eluder une eventuelle clause before|after|first inutilisable |
|
| 251 | - $defr = rtrim(preg_replace('/(BEFORE|AFTER|FIRST)(.*)$/is', '', $def)); |
|
| 252 | - $defo = $defr; // garder la def d'origine pour certains cas |
|
| 253 | - // remplacer les definitions venant de mysql |
|
| 254 | - $defr = _sqlite_remplacements_definitions_table($defr); |
|
| 255 | - |
|
| 256 | - // reinjecter dans le do |
|
| 257 | - $do = str_replace($def, $defr, $do); |
|
| 258 | - $def = $defr; |
|
| 259 | - |
|
| 260 | - switch ($cle) { |
|
| 261 | - // suppression d'un index |
|
| 262 | - case 'DROP KEY': |
|
| 263 | - case 'DROP INDEX': |
|
| 264 | - $nom_index = $colonne_origine; |
|
| 265 | - spip_sqlite_drop_index($nom_index, $table, $serveur); |
|
| 266 | - break; |
|
| 267 | - |
|
| 268 | - // suppression d'une pk |
|
| 269 | - case 'DROP PRIMARY KEY': |
|
| 270 | - if ( |
|
| 271 | - !_sqlite_modifier_table( |
|
| 272 | - $table, |
|
| 273 | - $colonne_origine, |
|
| 274 | - ['key' => ['PRIMARY KEY' => '']], |
|
| 275 | - $serveur |
|
| 276 | - ) |
|
| 277 | - ) { |
|
| 278 | - return false; |
|
| 279 | - } |
|
| 280 | - break; |
|
| 281 | - // suppression d'une colonne |
|
| 282 | - case 'DROP COLUMN': |
|
| 283 | - case 'DROP': |
|
| 284 | - if ( |
|
| 285 | - !_sqlite_modifier_table( |
|
| 286 | - $table, |
|
| 287 | - [$colonne_origine => ''], |
|
| 288 | - [], |
|
| 289 | - $serveur |
|
| 290 | - ) |
|
| 291 | - ) { |
|
| 292 | - return false; |
|
| 293 | - } |
|
| 294 | - break; |
|
| 295 | - |
|
| 296 | - case 'CHANGE COLUMN': |
|
| 297 | - case 'CHANGE': |
|
| 298 | - // recuperer le nom de la future colonne |
|
| 299 | - // on reprend la def d'origine car _sqlite_modifier_table va refaire la translation |
|
| 300 | - // en tenant compte de la cle primaire (ce qui est mieux) |
|
| 301 | - $def = trim($defo); |
|
| 302 | - $colonne_destination = substr($def, 0, strpos($def, ' ')); |
|
| 303 | - $def = substr($def, strlen($colonne_destination) + 1); |
|
| 304 | - |
|
| 305 | - if ( |
|
| 306 | - !_sqlite_modifier_table( |
|
| 307 | - $table, |
|
| 308 | - [$colonne_origine => $colonne_destination], |
|
| 309 | - ['field' => [$colonne_destination => $def]], |
|
| 310 | - $serveur |
|
| 311 | - ) |
|
| 312 | - ) { |
|
| 313 | - return false; |
|
| 314 | - } |
|
| 315 | - break; |
|
| 316 | - |
|
| 317 | - case 'MODIFY': |
|
| 318 | - // on reprend la def d'origine car _sqlite_modifier_table va refaire la translation |
|
| 319 | - // en tenant compte de la cle primaire (ce qui est mieux) |
|
| 320 | - if ( |
|
| 321 | - !_sqlite_modifier_table( |
|
| 322 | - $table, |
|
| 323 | - $colonne_origine, |
|
| 324 | - ['field' => [$colonne_origine => $defo]], |
|
| 325 | - $serveur |
|
| 326 | - ) |
|
| 327 | - ) { |
|
| 328 | - return false; |
|
| 329 | - } |
|
| 330 | - break; |
|
| 331 | - |
|
| 332 | - // pas geres en sqlite2 |
|
| 333 | - case 'RENAME': |
|
| 334 | - $do = 'RENAME TO' . substr($do, 6); |
|
| 335 | - case 'RENAME TO': |
|
| 336 | - if (!Sqlite::executer_requete("$debut $do", $serveur)) { |
|
| 337 | - spip_log("SQLite : Erreur ALTER TABLE / RENAME : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 338 | - |
|
| 339 | - return false; |
|
| 340 | - } |
|
| 341 | - break; |
|
| 342 | - |
|
| 343 | - // ajout d'une pk |
|
| 344 | - case 'ADD PRIMARY KEY': |
|
| 345 | - $pk = trim(substr($do, 16)); |
|
| 346 | - $pk = ($pk[0] == '(') ? substr($pk, 1, -1) : $pk; |
|
| 347 | - if ( |
|
| 348 | - !_sqlite_modifier_table( |
|
| 349 | - $table, |
|
| 350 | - $colonne_origine, |
|
| 351 | - ['key' => ['PRIMARY KEY' => $pk]], |
|
| 352 | - $serveur |
|
| 353 | - ) |
|
| 354 | - ) { |
|
| 355 | - return false; |
|
| 356 | - } |
|
| 357 | - break; |
|
| 358 | - // ajout d'un index |
|
| 359 | - case 'ADD UNIQUE KEY': |
|
| 360 | - case 'ADD UNIQUE': |
|
| 361 | - $unique = true; |
|
| 362 | - case 'ADD INDEX': |
|
| 363 | - case 'ADD KEY': |
|
| 364 | - if (!isset($unique)) { |
|
| 365 | - $unique = false; |
|
| 366 | - } |
|
| 367 | - // peut etre "(colonne)" ou "nom_index (colonnes)" |
|
| 368 | - // bug potentiel si qqn met "(colonne, colonne)" |
|
| 369 | - // |
|
| 370 | - // nom_index (colonnes) |
|
| 371 | - if ($def) { |
|
| 372 | - $colonnes = substr($def, 1, -1); |
|
| 373 | - $nom_index = $colonne_origine; |
|
| 374 | - } else { |
|
| 375 | - // (colonne) |
|
| 376 | - if ($colonne_origine[0] == '(') { |
|
| 377 | - $colonnes = substr($colonne_origine, 1, -1); |
|
| 378 | - if (str_contains(',', $colonnes)) { |
|
| 379 | - spip_log('SQLite : Erreur, impossible de creer un index sur plusieurs colonnes' |
|
| 380 | - . " sans qu'il ait de nom ($table, ($colonnes))", 'sqlite.' . _LOG_ERREUR); |
|
| 381 | - break; |
|
| 382 | - } else { |
|
| 383 | - $nom_index = $colonnes; |
|
| 384 | - } |
|
| 385 | - } // nom_index |
|
| 386 | - else { |
|
| 387 | - $nom_index = $colonnes = $colonne_origine; |
|
| 388 | - } |
|
| 389 | - } |
|
| 390 | - spip_sqlite_create_index($nom_index, $table, $colonnes, $unique, $serveur); |
|
| 391 | - break; |
|
| 392 | - |
|
| 393 | - // pas geres en sqlite2 |
|
| 394 | - case 'ADD COLUMN': |
|
| 395 | - $do = 'ADD' . substr($do, 10); |
|
| 396 | - case 'ADD': |
|
| 397 | - default: |
|
| 398 | - if (!preg_match(',primary\s+key,i', $do)) { |
|
| 399 | - if (!Sqlite::executer_requete("$debut $do", $serveur)) { |
|
| 400 | - spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 401 | - |
|
| 402 | - return false; |
|
| 403 | - } |
|
| 404 | - break; |
|
| 405 | - } |
|
| 406 | - // ou si la colonne est aussi primary key |
|
| 407 | - // cas du add id_truc int primary key |
|
| 408 | - // ajout d'une colonne qui passe en primary key directe |
|
| 409 | - else { |
|
| 410 | - $def = trim(substr($do, 3)); |
|
| 411 | - $colonne_ajoutee = substr($def, 0, strpos($def, ' ')); |
|
| 412 | - $def = substr($def, strlen($colonne_ajoutee) + 1); |
|
| 413 | - $opts = []; |
|
| 414 | - if (preg_match(',primary\s+key,i', $def)) { |
|
| 415 | - $opts['key'] = ['PRIMARY KEY' => $colonne_ajoutee]; |
|
| 416 | - $def = preg_replace(',primary\s+key,i', '', $def); |
|
| 417 | - } |
|
| 418 | - $opts['field'] = [$colonne_ajoutee => $def]; |
|
| 419 | - if (!_sqlite_modifier_table($table, [$colonne_ajoutee], $opts, $serveur)) { |
|
| 420 | - spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 421 | - |
|
| 422 | - return false; |
|
| 423 | - } |
|
| 424 | - } |
|
| 425 | - break; |
|
| 426 | - } |
|
| 427 | - // tout est bon, ouf ! |
|
| 428 | - spip_log("SQLite ($serveur) : Changements OK : $debut $do", 'sqlite.' . _LOG_INFO); |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - spip_log("SQLite ($serveur) : fin ALTER TABLE OK !", 'sqlite.' . _LOG_INFO); |
|
| 432 | - |
|
| 433 | - return true; |
|
| 189 | + // 1 |
|
| 190 | + if (preg_match('/\s*(ALTER(\s*IGNORE)?\s*TABLE\s*([^\s]*))\s*(.*)?/is', $query, $regs)) { |
|
| 191 | + $debut = $regs[1]; |
|
| 192 | + $table = $regs[3]; |
|
| 193 | + $suite = $regs[4]; |
|
| 194 | + } else { |
|
| 195 | + spip_log("SQLite : Probleme de ALTER TABLE mal forme dans $query", 'sqlite.' . _LOG_ERREUR); |
|
| 196 | + |
|
| 197 | + return false; |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + // 2 |
|
| 201 | + // il faudrait une regexp pour eviter de spliter ADD PRIMARY KEY (colA, colB) |
|
| 202 | + // tout en cassant "ADD PRIMARY KEY (colA, colB), ADD INDEX (chose)"... en deux |
|
| 203 | + // ou revoir l'api de sql_alter en creant un |
|
| 204 | + // sql_alter_table($table,array($actions)); |
|
| 205 | + $todo = explode(',', $suite); |
|
| 206 | + |
|
| 207 | + // on remet les morceaux dechires ensembles... que c'est laid ! |
|
| 208 | + $todo2 = []; |
|
| 209 | + $i = 0; |
|
| 210 | + $ouverte = false; |
|
| 211 | + while ($do = array_shift($todo)) { |
|
| 212 | + $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . ',' . $do : $do; |
|
| 213 | + $o = (str_contains($do, '(')); |
|
| 214 | + $f = (str_contains($do, ')')); |
|
| 215 | + if ($o and !$f) { |
|
| 216 | + $ouverte = true; |
|
| 217 | + } elseif ($f) { |
|
| 218 | + $ouverte = false; |
|
| 219 | + } |
|
| 220 | + if (!$ouverte) { |
|
| 221 | + $i++; |
|
| 222 | + } |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + // 3 |
|
| 226 | + $resultats = []; |
|
| 227 | + foreach ($todo2 as $do) { |
|
| 228 | + $do = trim($do); |
|
| 229 | + if ( |
|
| 230 | + !preg_match('/(DROP PRIMARY KEY|DROP KEY|DROP INDEX|DROP COLUMN|DROP' |
|
| 231 | + . '|CHANGE COLUMN|CHANGE|MODIFY|RENAME TO|RENAME' |
|
| 232 | + . '|ADD PRIMARY KEY|ADD KEY|ADD INDEX|ADD UNIQUE KEY|ADD UNIQUE' |
|
| 233 | + . '|ADD COLUMN|ADD' |
|
| 234 | + . ')\s*([^\s]*)\s*(.*)?/i', $do, $matches) |
|
| 235 | + ) { |
|
| 236 | + spip_log( |
|
| 237 | + "SQLite : Probleme de ALTER TABLE, utilisation non reconnue dans : $do \n(requete d'origine : $query)", |
|
| 238 | + 'sqlite.' . _LOG_ERREUR |
|
| 239 | + ); |
|
| 240 | + |
|
| 241 | + return false; |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + $cle = strtoupper($matches[1]); |
|
| 245 | + $colonne_origine = $matches[2]; |
|
| 246 | + $colonne_destination = ''; |
|
| 247 | + |
|
| 248 | + $def = $matches[3]; |
|
| 249 | + |
|
| 250 | + // eluder une eventuelle clause before|after|first inutilisable |
|
| 251 | + $defr = rtrim(preg_replace('/(BEFORE|AFTER|FIRST)(.*)$/is', '', $def)); |
|
| 252 | + $defo = $defr; // garder la def d'origine pour certains cas |
|
| 253 | + // remplacer les definitions venant de mysql |
|
| 254 | + $defr = _sqlite_remplacements_definitions_table($defr); |
|
| 255 | + |
|
| 256 | + // reinjecter dans le do |
|
| 257 | + $do = str_replace($def, $defr, $do); |
|
| 258 | + $def = $defr; |
|
| 259 | + |
|
| 260 | + switch ($cle) { |
|
| 261 | + // suppression d'un index |
|
| 262 | + case 'DROP KEY': |
|
| 263 | + case 'DROP INDEX': |
|
| 264 | + $nom_index = $colonne_origine; |
|
| 265 | + spip_sqlite_drop_index($nom_index, $table, $serveur); |
|
| 266 | + break; |
|
| 267 | + |
|
| 268 | + // suppression d'une pk |
|
| 269 | + case 'DROP PRIMARY KEY': |
|
| 270 | + if ( |
|
| 271 | + !_sqlite_modifier_table( |
|
| 272 | + $table, |
|
| 273 | + $colonne_origine, |
|
| 274 | + ['key' => ['PRIMARY KEY' => '']], |
|
| 275 | + $serveur |
|
| 276 | + ) |
|
| 277 | + ) { |
|
| 278 | + return false; |
|
| 279 | + } |
|
| 280 | + break; |
|
| 281 | + // suppression d'une colonne |
|
| 282 | + case 'DROP COLUMN': |
|
| 283 | + case 'DROP': |
|
| 284 | + if ( |
|
| 285 | + !_sqlite_modifier_table( |
|
| 286 | + $table, |
|
| 287 | + [$colonne_origine => ''], |
|
| 288 | + [], |
|
| 289 | + $serveur |
|
| 290 | + ) |
|
| 291 | + ) { |
|
| 292 | + return false; |
|
| 293 | + } |
|
| 294 | + break; |
|
| 295 | + |
|
| 296 | + case 'CHANGE COLUMN': |
|
| 297 | + case 'CHANGE': |
|
| 298 | + // recuperer le nom de la future colonne |
|
| 299 | + // on reprend la def d'origine car _sqlite_modifier_table va refaire la translation |
|
| 300 | + // en tenant compte de la cle primaire (ce qui est mieux) |
|
| 301 | + $def = trim($defo); |
|
| 302 | + $colonne_destination = substr($def, 0, strpos($def, ' ')); |
|
| 303 | + $def = substr($def, strlen($colonne_destination) + 1); |
|
| 304 | + |
|
| 305 | + if ( |
|
| 306 | + !_sqlite_modifier_table( |
|
| 307 | + $table, |
|
| 308 | + [$colonne_origine => $colonne_destination], |
|
| 309 | + ['field' => [$colonne_destination => $def]], |
|
| 310 | + $serveur |
|
| 311 | + ) |
|
| 312 | + ) { |
|
| 313 | + return false; |
|
| 314 | + } |
|
| 315 | + break; |
|
| 316 | + |
|
| 317 | + case 'MODIFY': |
|
| 318 | + // on reprend la def d'origine car _sqlite_modifier_table va refaire la translation |
|
| 319 | + // en tenant compte de la cle primaire (ce qui est mieux) |
|
| 320 | + if ( |
|
| 321 | + !_sqlite_modifier_table( |
|
| 322 | + $table, |
|
| 323 | + $colonne_origine, |
|
| 324 | + ['field' => [$colonne_origine => $defo]], |
|
| 325 | + $serveur |
|
| 326 | + ) |
|
| 327 | + ) { |
|
| 328 | + return false; |
|
| 329 | + } |
|
| 330 | + break; |
|
| 331 | + |
|
| 332 | + // pas geres en sqlite2 |
|
| 333 | + case 'RENAME': |
|
| 334 | + $do = 'RENAME TO' . substr($do, 6); |
|
| 335 | + case 'RENAME TO': |
|
| 336 | + if (!Sqlite::executer_requete("$debut $do", $serveur)) { |
|
| 337 | + spip_log("SQLite : Erreur ALTER TABLE / RENAME : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 338 | + |
|
| 339 | + return false; |
|
| 340 | + } |
|
| 341 | + break; |
|
| 342 | + |
|
| 343 | + // ajout d'une pk |
|
| 344 | + case 'ADD PRIMARY KEY': |
|
| 345 | + $pk = trim(substr($do, 16)); |
|
| 346 | + $pk = ($pk[0] == '(') ? substr($pk, 1, -1) : $pk; |
|
| 347 | + if ( |
|
| 348 | + !_sqlite_modifier_table( |
|
| 349 | + $table, |
|
| 350 | + $colonne_origine, |
|
| 351 | + ['key' => ['PRIMARY KEY' => $pk]], |
|
| 352 | + $serveur |
|
| 353 | + ) |
|
| 354 | + ) { |
|
| 355 | + return false; |
|
| 356 | + } |
|
| 357 | + break; |
|
| 358 | + // ajout d'un index |
|
| 359 | + case 'ADD UNIQUE KEY': |
|
| 360 | + case 'ADD UNIQUE': |
|
| 361 | + $unique = true; |
|
| 362 | + case 'ADD INDEX': |
|
| 363 | + case 'ADD KEY': |
|
| 364 | + if (!isset($unique)) { |
|
| 365 | + $unique = false; |
|
| 366 | + } |
|
| 367 | + // peut etre "(colonne)" ou "nom_index (colonnes)" |
|
| 368 | + // bug potentiel si qqn met "(colonne, colonne)" |
|
| 369 | + // |
|
| 370 | + // nom_index (colonnes) |
|
| 371 | + if ($def) { |
|
| 372 | + $colonnes = substr($def, 1, -1); |
|
| 373 | + $nom_index = $colonne_origine; |
|
| 374 | + } else { |
|
| 375 | + // (colonne) |
|
| 376 | + if ($colonne_origine[0] == '(') { |
|
| 377 | + $colonnes = substr($colonne_origine, 1, -1); |
|
| 378 | + if (str_contains(',', $colonnes)) { |
|
| 379 | + spip_log('SQLite : Erreur, impossible de creer un index sur plusieurs colonnes' |
|
| 380 | + . " sans qu'il ait de nom ($table, ($colonnes))", 'sqlite.' . _LOG_ERREUR); |
|
| 381 | + break; |
|
| 382 | + } else { |
|
| 383 | + $nom_index = $colonnes; |
|
| 384 | + } |
|
| 385 | + } // nom_index |
|
| 386 | + else { |
|
| 387 | + $nom_index = $colonnes = $colonne_origine; |
|
| 388 | + } |
|
| 389 | + } |
|
| 390 | + spip_sqlite_create_index($nom_index, $table, $colonnes, $unique, $serveur); |
|
| 391 | + break; |
|
| 392 | + |
|
| 393 | + // pas geres en sqlite2 |
|
| 394 | + case 'ADD COLUMN': |
|
| 395 | + $do = 'ADD' . substr($do, 10); |
|
| 396 | + case 'ADD': |
|
| 397 | + default: |
|
| 398 | + if (!preg_match(',primary\s+key,i', $do)) { |
|
| 399 | + if (!Sqlite::executer_requete("$debut $do", $serveur)) { |
|
| 400 | + spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 401 | + |
|
| 402 | + return false; |
|
| 403 | + } |
|
| 404 | + break; |
|
| 405 | + } |
|
| 406 | + // ou si la colonne est aussi primary key |
|
| 407 | + // cas du add id_truc int primary key |
|
| 408 | + // ajout d'une colonne qui passe en primary key directe |
|
| 409 | + else { |
|
| 410 | + $def = trim(substr($do, 3)); |
|
| 411 | + $colonne_ajoutee = substr($def, 0, strpos($def, ' ')); |
|
| 412 | + $def = substr($def, strlen($colonne_ajoutee) + 1); |
|
| 413 | + $opts = []; |
|
| 414 | + if (preg_match(',primary\s+key,i', $def)) { |
|
| 415 | + $opts['key'] = ['PRIMARY KEY' => $colonne_ajoutee]; |
|
| 416 | + $def = preg_replace(',primary\s+key,i', '', $def); |
|
| 417 | + } |
|
| 418 | + $opts['field'] = [$colonne_ajoutee => $def]; |
|
| 419 | + if (!_sqlite_modifier_table($table, [$colonne_ajoutee], $opts, $serveur)) { |
|
| 420 | + spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 421 | + |
|
| 422 | + return false; |
|
| 423 | + } |
|
| 424 | + } |
|
| 425 | + break; |
|
| 426 | + } |
|
| 427 | + // tout est bon, ouf ! |
|
| 428 | + spip_log("SQLite ($serveur) : Changements OK : $debut $do", 'sqlite.' . _LOG_INFO); |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + spip_log("SQLite ($serveur) : fin ALTER TABLE OK !", 'sqlite.' . _LOG_INFO); |
|
| 432 | + |
|
| 433 | + return true; |
|
| 434 | 434 | } |
| 435 | 435 | |
| 436 | 436 | /** |
@@ -452,38 +452,38 @@ discard block |
||
| 452 | 452 | * - true si la requête réussie, false sinon. |
| 453 | 453 | */ |
| 454 | 454 | function spip_sqlite_create( |
| 455 | - $nom, |
|
| 456 | - $champs, |
|
| 457 | - $cles, |
|
| 458 | - $autoinc = false, |
|
| 459 | - $temporary = false, |
|
| 460 | - $serveur = '', |
|
| 461 | - $requeter = true |
|
| 455 | + $nom, |
|
| 456 | + $champs, |
|
| 457 | + $cles, |
|
| 458 | + $autoinc = false, |
|
| 459 | + $temporary = false, |
|
| 460 | + $serveur = '', |
|
| 461 | + $requeter = true |
|
| 462 | 462 | ) { |
| 463 | - $query = _sqlite_requete_create($nom, $champs, $cles, $autoinc, $temporary, $ifnotexists = true, $serveur, $requeter); |
|
| 464 | - if (!$query) { |
|
| 465 | - return false; |
|
| 466 | - } |
|
| 467 | - $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 468 | - |
|
| 469 | - // SQLite ne cree pas les KEY sur les requetes CREATE TABLE |
|
| 470 | - // il faut donc les faire creer ensuite |
|
| 471 | - if (!$requeter) { |
|
| 472 | - return $res; |
|
| 473 | - } |
|
| 474 | - |
|
| 475 | - $ok = $res ? true : false; |
|
| 476 | - if ($ok) { |
|
| 477 | - foreach ($cles as $k => $v) { |
|
| 478 | - if (preg_match(',^(UNIQUE KEY|KEY|UNIQUE)\s,i', $k, $m)) { |
|
| 479 | - $index = trim(substr($k, strlen($m[1]))); |
|
| 480 | - $unique = (strlen($m[1]) > 3); |
|
| 481 | - $ok &= spip_sqlite_create_index($index, $nom, $v, $unique, $serveur); |
|
| 482 | - } |
|
| 483 | - } |
|
| 484 | - } |
|
| 485 | - |
|
| 486 | - return $ok ? true : false; |
|
| 463 | + $query = _sqlite_requete_create($nom, $champs, $cles, $autoinc, $temporary, $ifnotexists = true, $serveur, $requeter); |
|
| 464 | + if (!$query) { |
|
| 465 | + return false; |
|
| 466 | + } |
|
| 467 | + $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 468 | + |
|
| 469 | + // SQLite ne cree pas les KEY sur les requetes CREATE TABLE |
|
| 470 | + // il faut donc les faire creer ensuite |
|
| 471 | + if (!$requeter) { |
|
| 472 | + return $res; |
|
| 473 | + } |
|
| 474 | + |
|
| 475 | + $ok = $res ? true : false; |
|
| 476 | + if ($ok) { |
|
| 477 | + foreach ($cles as $k => $v) { |
|
| 478 | + if (preg_match(',^(UNIQUE KEY|KEY|UNIQUE)\s,i', $k, $m)) { |
|
| 479 | + $index = trim(substr($k, strlen($m[1]))); |
|
| 480 | + $unique = (strlen($m[1]) > 3); |
|
| 481 | + $ok &= spip_sqlite_create_index($index, $nom, $v, $unique, $serveur); |
|
| 482 | + } |
|
| 483 | + } |
|
| 484 | + } |
|
| 485 | + |
|
| 486 | + return $ok ? true : false; |
|
| 487 | 487 | } |
| 488 | 488 | |
| 489 | 489 | /** |
@@ -497,21 +497,21 @@ discard block |
||
| 497 | 497 | **/ |
| 498 | 498 | function spip_sqlite_create_base($nom, $serveur = '', $option = true) |
| 499 | 499 | { |
| 500 | - $f = $nom . '.sqlite'; |
|
| 501 | - if (strpos($nom, '/') === false) { |
|
| 502 | - $f = _DIR_DB . $f; |
|
| 503 | - } |
|
| 500 | + $f = $nom . '.sqlite'; |
|
| 501 | + if (strpos($nom, '/') === false) { |
|
| 502 | + $f = _DIR_DB . $f; |
|
| 503 | + } |
|
| 504 | 504 | |
| 505 | - $ok = new \PDO("sqlite:$f"); |
|
| 505 | + $ok = new \PDO("sqlite:$f"); |
|
| 506 | 506 | |
| 507 | - if ($ok) { |
|
| 508 | - unset($ok); |
|
| 507 | + if ($ok) { |
|
| 508 | + unset($ok); |
|
| 509 | 509 | |
| 510 | - return true; |
|
| 511 | - } |
|
| 512 | - unset($ok); |
|
| 510 | + return true; |
|
| 511 | + } |
|
| 512 | + unset($ok); |
|
| 513 | 513 | |
| 514 | - return false; |
|
| 514 | + return false; |
|
| 515 | 515 | } |
| 516 | 516 | |
| 517 | 517 | |
@@ -533,22 +533,22 @@ discard block |
||
| 533 | 533 | */ |
| 534 | 534 | function spip_sqlite_create_view($nom, $query_select, $serveur = '', $requeter = true) |
| 535 | 535 | { |
| 536 | - if (!$query_select) { |
|
| 537 | - return false; |
|
| 538 | - } |
|
| 539 | - // vue deja presente |
|
| 540 | - if (sql_showtable($nom, false, $serveur)) { |
|
| 541 | - spip_log( |
|
| 542 | - "Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", |
|
| 543 | - 'sqlite.' . _LOG_ERREUR |
|
| 544 | - ); |
|
| 545 | - |
|
| 546 | - return false; |
|
| 547 | - } |
|
| 548 | - |
|
| 549 | - $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 550 | - |
|
| 551 | - return spip_sqlite_query($query, $serveur, $requeter); |
|
| 536 | + if (!$query_select) { |
|
| 537 | + return false; |
|
| 538 | + } |
|
| 539 | + // vue deja presente |
|
| 540 | + if (sql_showtable($nom, false, $serveur)) { |
|
| 541 | + spip_log( |
|
| 542 | + "Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", |
|
| 543 | + 'sqlite.' . _LOG_ERREUR |
|
| 544 | + ); |
|
| 545 | + |
|
| 546 | + return false; |
|
| 547 | + } |
|
| 548 | + |
|
| 549 | + $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 550 | + |
|
| 551 | + return spip_sqlite_query($query, $serveur, $requeter); |
|
| 552 | 552 | } |
| 553 | 553 | |
| 554 | 554 | /** |
@@ -571,54 +571,54 @@ discard block |
||
| 571 | 571 | */ |
| 572 | 572 | function spip_sqlite_create_index($nom, $table, $champs, $unique = '', $serveur = '', $requeter = true) |
| 573 | 573 | { |
| 574 | - if (!($nom or $table or $champs)) { |
|
| 575 | - spip_log( |
|
| 576 | - "Champ manquant pour creer un index sqlite ($nom, $table, (" . join(',', $champs) . '))', |
|
| 577 | - 'sqlite.' . _LOG_ERREUR |
|
| 578 | - ); |
|
| 579 | - |
|
| 580 | - return false; |
|
| 581 | - } |
|
| 582 | - |
|
| 583 | - // SQLite ne differentie pas noms des index en fonction des tables |
|
| 584 | - // il faut donc creer des noms uniques d'index pour une base sqlite |
|
| 585 | - $nom = $table . '_' . $nom; |
|
| 586 | - // enlever d'eventuelles parentheses deja presentes sur champs |
|
| 587 | - if (!is_array($champs)) { |
|
| 588 | - if ($champs[0] == '(') { |
|
| 589 | - $champs = substr($champs, 1, -1); |
|
| 590 | - } |
|
| 591 | - $champs = [$champs]; |
|
| 592 | - // supprimer l'info de longueur d'index mysql en fin de champ |
|
| 593 | - $champs = preg_replace(',\(\d+\)$,', '', $champs); |
|
| 594 | - } |
|
| 595 | - |
|
| 596 | - $ifnotexists = ''; |
|
| 597 | - $version = spip_sqlite_fetch(spip_sqlite_query('select sqlite_version() AS sqlite_version', $serveur), '', $serveur); |
|
| 598 | - if (!function_exists('spip_version_compare')) { |
|
| 599 | - include_spip('plugins/installer'); |
|
| 600 | - } |
|
| 601 | - |
|
| 602 | - if ($version and spip_version_compare($version['sqlite_version'], '3.3.0', '>=')) { |
|
| 603 | - $ifnotexists = ' IF NOT EXISTS'; |
|
| 604 | - } else { |
|
| 605 | - /* simuler le IF EXISTS - version 2 et sqlite < 3.3a */ |
|
| 606 | - $a = spip_sqlite_showtable($table, $serveur); |
|
| 607 | - if (isset($a['key']['KEY ' . $nom])) { |
|
| 608 | - return true; |
|
| 609 | - } |
|
| 610 | - } |
|
| 611 | - |
|
| 612 | - $query = 'CREATE ' . ($unique ? 'UNIQUE ' : '') . "INDEX$ifnotexists $nom ON $table (" . join(',', $champs) . ')'; |
|
| 613 | - $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 614 | - if (!$requeter) { |
|
| 615 | - return $res; |
|
| 616 | - } |
|
| 617 | - if ($res) { |
|
| 618 | - return true; |
|
| 619 | - } else { |
|
| 620 | - return false; |
|
| 621 | - } |
|
| 574 | + if (!($nom or $table or $champs)) { |
|
| 575 | + spip_log( |
|
| 576 | + "Champ manquant pour creer un index sqlite ($nom, $table, (" . join(',', $champs) . '))', |
|
| 577 | + 'sqlite.' . _LOG_ERREUR |
|
| 578 | + ); |
|
| 579 | + |
|
| 580 | + return false; |
|
| 581 | + } |
|
| 582 | + |
|
| 583 | + // SQLite ne differentie pas noms des index en fonction des tables |
|
| 584 | + // il faut donc creer des noms uniques d'index pour une base sqlite |
|
| 585 | + $nom = $table . '_' . $nom; |
|
| 586 | + // enlever d'eventuelles parentheses deja presentes sur champs |
|
| 587 | + if (!is_array($champs)) { |
|
| 588 | + if ($champs[0] == '(') { |
|
| 589 | + $champs = substr($champs, 1, -1); |
|
| 590 | + } |
|
| 591 | + $champs = [$champs]; |
|
| 592 | + // supprimer l'info de longueur d'index mysql en fin de champ |
|
| 593 | + $champs = preg_replace(',\(\d+\)$,', '', $champs); |
|
| 594 | + } |
|
| 595 | + |
|
| 596 | + $ifnotexists = ''; |
|
| 597 | + $version = spip_sqlite_fetch(spip_sqlite_query('select sqlite_version() AS sqlite_version', $serveur), '', $serveur); |
|
| 598 | + if (!function_exists('spip_version_compare')) { |
|
| 599 | + include_spip('plugins/installer'); |
|
| 600 | + } |
|
| 601 | + |
|
| 602 | + if ($version and spip_version_compare($version['sqlite_version'], '3.3.0', '>=')) { |
|
| 603 | + $ifnotexists = ' IF NOT EXISTS'; |
|
| 604 | + } else { |
|
| 605 | + /* simuler le IF EXISTS - version 2 et sqlite < 3.3a */ |
|
| 606 | + $a = spip_sqlite_showtable($table, $serveur); |
|
| 607 | + if (isset($a['key']['KEY ' . $nom])) { |
|
| 608 | + return true; |
|
| 609 | + } |
|
| 610 | + } |
|
| 611 | + |
|
| 612 | + $query = 'CREATE ' . ($unique ? 'UNIQUE ' : '') . "INDEX$ifnotexists $nom ON $table (" . join(',', $champs) . ')'; |
|
| 613 | + $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 614 | + if (!$requeter) { |
|
| 615 | + return $res; |
|
| 616 | + } |
|
| 617 | + if ($res) { |
|
| 618 | + return true; |
|
| 619 | + } else { |
|
| 620 | + return false; |
|
| 621 | + } |
|
| 622 | 622 | } |
| 623 | 623 | |
| 624 | 624 | /** |
@@ -636,31 +636,31 @@ discard block |
||
| 636 | 636 | */ |
| 637 | 637 | function spip_sqlite_count($r, $serveur = '', $requeter = true) |
| 638 | 638 | { |
| 639 | - if (!$r) { |
|
| 640 | - return 0; |
|
| 641 | - } |
|
| 642 | - |
|
| 643 | - // select ou autre (insert, update,...) ? |
|
| 644 | - // (link,requete) a compter |
|
| 645 | - if (is_array($r->spipSqliteRowCount)) { |
|
| 646 | - list($link, $query) = $r->spipSqliteRowCount; |
|
| 647 | - // amelioration possible a tester intensivement : pas de order by pour compter ! |
|
| 648 | - // $query = preg_replace(",ORDER BY .+(LIMIT\s|HAVING\s|GROUP BY\s|$),Uims","\\1",$query); |
|
| 649 | - $query = "SELECT count(*) as zzzzsqlitecount FROM ($query)"; |
|
| 650 | - $l = $link->query($query); |
|
| 651 | - $i = 0; |
|
| 652 | - if ($l and $z = $l->fetch()) { |
|
| 653 | - $i = $z['zzzzsqlitecount']; |
|
| 654 | - } |
|
| 655 | - $r->spipSqliteRowCount = $i; |
|
| 656 | - } |
|
| 657 | - if (isset($r->spipSqliteRowCount)) { |
|
| 658 | - // Ce compte est faux s'il y a des limit dans la requete :( |
|
| 659 | - // il retourne le nombre d'enregistrements sans le limit |
|
| 660 | - return $r->spipSqliteRowCount; |
|
| 661 | - } else { |
|
| 662 | - return $r->rowCount(); |
|
| 663 | - } |
|
| 639 | + if (!$r) { |
|
| 640 | + return 0; |
|
| 641 | + } |
|
| 642 | + |
|
| 643 | + // select ou autre (insert, update,...) ? |
|
| 644 | + // (link,requete) a compter |
|
| 645 | + if (is_array($r->spipSqliteRowCount)) { |
|
| 646 | + list($link, $query) = $r->spipSqliteRowCount; |
|
| 647 | + // amelioration possible a tester intensivement : pas de order by pour compter ! |
|
| 648 | + // $query = preg_replace(",ORDER BY .+(LIMIT\s|HAVING\s|GROUP BY\s|$),Uims","\\1",$query); |
|
| 649 | + $query = "SELECT count(*) as zzzzsqlitecount FROM ($query)"; |
|
| 650 | + $l = $link->query($query); |
|
| 651 | + $i = 0; |
|
| 652 | + if ($l and $z = $l->fetch()) { |
|
| 653 | + $i = $z['zzzzsqlitecount']; |
|
| 654 | + } |
|
| 655 | + $r->spipSqliteRowCount = $i; |
|
| 656 | + } |
|
| 657 | + if (isset($r->spipSqliteRowCount)) { |
|
| 658 | + // Ce compte est faux s'il y a des limit dans la requete :( |
|
| 659 | + // il retourne le nombre d'enregistrements sans le limit |
|
| 660 | + return $r->spipSqliteRowCount; |
|
| 661 | + } else { |
|
| 662 | + return $r->rowCount(); |
|
| 663 | + } |
|
| 664 | 664 | } |
| 665 | 665 | |
| 666 | 666 | |
@@ -679,30 +679,30 @@ discard block |
||
| 679 | 679 | * - false si la requête a échouée |
| 680 | 680 | **/ |
| 681 | 681 | function spip_sqlite_countsel( |
| 682 | - $from = [], |
|
| 683 | - $where = [], |
|
| 684 | - $groupby = '', |
|
| 685 | - $having = [], |
|
| 686 | - $serveur = '', |
|
| 687 | - $requeter = true |
|
| 682 | + $from = [], |
|
| 683 | + $where = [], |
|
| 684 | + $groupby = '', |
|
| 685 | + $having = [], |
|
| 686 | + $serveur = '', |
|
| 687 | + $requeter = true |
|
| 688 | 688 | ) { |
| 689 | - $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 690 | - $r = spip_sqlite_select( |
|
| 691 | - "COUNT($c)", |
|
| 692 | - $from, |
|
| 693 | - $where, |
|
| 694 | - '', |
|
| 695 | - '', |
|
| 696 | - '', |
|
| 697 | - $having, |
|
| 698 | - $serveur, |
|
| 699 | - $requeter |
|
| 700 | - ); |
|
| 701 | - if ((is_resource($r) or is_object($r)) && $requeter) { // ressource : sqlite2, object : sqlite3 |
|
| 702 | - list($r) = spip_sqlite_fetch($r, SPIP_SQLITE3_NUM, $serveur); |
|
| 703 | - } |
|
| 704 | - |
|
| 705 | - return $r; |
|
| 689 | + $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 690 | + $r = spip_sqlite_select( |
|
| 691 | + "COUNT($c)", |
|
| 692 | + $from, |
|
| 693 | + $where, |
|
| 694 | + '', |
|
| 695 | + '', |
|
| 696 | + '', |
|
| 697 | + $having, |
|
| 698 | + $serveur, |
|
| 699 | + $requeter |
|
| 700 | + ); |
|
| 701 | + if ((is_resource($r) or is_object($r)) && $requeter) { // ressource : sqlite2, object : sqlite3 |
|
| 702 | + list($r) = spip_sqlite_fetch($r, SPIP_SQLITE3_NUM, $serveur); |
|
| 703 | + } |
|
| 704 | + |
|
| 705 | + return $r; |
|
| 706 | 706 | } |
| 707 | 707 | |
| 708 | 708 | |
@@ -720,24 +720,24 @@ discard block |
||
| 720 | 720 | **/ |
| 721 | 721 | function spip_sqlite_delete($table, $where = '', $serveur = '', $requeter = true) |
| 722 | 722 | { |
| 723 | - $res = spip_sqlite_query( |
|
| 724 | - _sqlite_calculer_expression('DELETE FROM', $table, ',') |
|
| 725 | - . _sqlite_calculer_expression('WHERE', $where), |
|
| 726 | - $serveur, |
|
| 727 | - $requeter |
|
| 728 | - ); |
|
| 729 | - |
|
| 730 | - // renvoyer la requete inerte si demandee |
|
| 731 | - if (!$requeter) { |
|
| 732 | - return $res; |
|
| 733 | - } |
|
| 734 | - |
|
| 735 | - if ($res) { |
|
| 736 | - $link = _sqlite_link($serveur); |
|
| 737 | - return $res->rowCount(); |
|
| 738 | - } else { |
|
| 739 | - return false; |
|
| 740 | - } |
|
| 723 | + $res = spip_sqlite_query( |
|
| 724 | + _sqlite_calculer_expression('DELETE FROM', $table, ',') |
|
| 725 | + . _sqlite_calculer_expression('WHERE', $where), |
|
| 726 | + $serveur, |
|
| 727 | + $requeter |
|
| 728 | + ); |
|
| 729 | + |
|
| 730 | + // renvoyer la requete inerte si demandee |
|
| 731 | + if (!$requeter) { |
|
| 732 | + return $res; |
|
| 733 | + } |
|
| 734 | + |
|
| 735 | + if ($res) { |
|
| 736 | + $link = _sqlite_link($serveur); |
|
| 737 | + return $res->rowCount(); |
|
| 738 | + } else { |
|
| 739 | + return false; |
|
| 740 | + } |
|
| 741 | 741 | } |
| 742 | 742 | |
| 743 | 743 | |
@@ -754,15 +754,15 @@ discard block |
||
| 754 | 754 | */ |
| 755 | 755 | function spip_sqlite_drop_table($table, $exist = '', $serveur = '', $requeter = true) |
| 756 | 756 | { |
| 757 | - if ($exist) { |
|
| 758 | - $exist = ' IF EXISTS'; |
|
| 759 | - } |
|
| 760 | - |
|
| 761 | - if (spip_sqlite_query("DROP TABLE$exist $table", $serveur, $requeter)) { |
|
| 762 | - return true; |
|
| 763 | - } else { |
|
| 764 | - return false; |
|
| 765 | - } |
|
| 757 | + if ($exist) { |
|
| 758 | + $exist = ' IF EXISTS'; |
|
| 759 | + } |
|
| 760 | + |
|
| 761 | + if (spip_sqlite_query("DROP TABLE$exist $table", $serveur, $requeter)) { |
|
| 762 | + return true; |
|
| 763 | + } else { |
|
| 764 | + return false; |
|
| 765 | + } |
|
| 766 | 766 | } |
| 767 | 767 | |
| 768 | 768 | |
@@ -779,11 +779,11 @@ discard block |
||
| 779 | 779 | */ |
| 780 | 780 | function spip_sqlite_drop_view($view, $exist = '', $serveur = '', $requeter = true) |
| 781 | 781 | { |
| 782 | - if ($exist) { |
|
| 783 | - $exist = ' IF EXISTS'; |
|
| 784 | - } |
|
| 782 | + if ($exist) { |
|
| 783 | + $exist = ' IF EXISTS'; |
|
| 784 | + } |
|
| 785 | 785 | |
| 786 | - return spip_sqlite_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 786 | + return spip_sqlite_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 787 | 787 | } |
| 788 | 788 | |
| 789 | 789 | /** |
@@ -798,20 +798,20 @@ discard block |
||
| 798 | 798 | */ |
| 799 | 799 | function spip_sqlite_drop_index($nom, $table, $serveur = '', $requeter = true) |
| 800 | 800 | { |
| 801 | - if (!($nom or $table)) { |
|
| 802 | - spip_log("Champ manquant pour supprimer un index sqlite ($nom, $table)", 'sqlite.' . _LOG_ERREUR); |
|
| 801 | + if (!($nom or $table)) { |
|
| 802 | + spip_log("Champ manquant pour supprimer un index sqlite ($nom, $table)", 'sqlite.' . _LOG_ERREUR); |
|
| 803 | 803 | |
| 804 | - return false; |
|
| 805 | - } |
|
| 804 | + return false; |
|
| 805 | + } |
|
| 806 | 806 | |
| 807 | - // SQLite ne differentie pas noms des index en fonction des tables |
|
| 808 | - // il faut donc creer des noms uniques d'index pour une base sqlite |
|
| 809 | - $index = $table . '_' . $nom; |
|
| 810 | - $exist = ' IF EXISTS'; |
|
| 807 | + // SQLite ne differentie pas noms des index en fonction des tables |
|
| 808 | + // il faut donc creer des noms uniques d'index pour une base sqlite |
|
| 809 | + $index = $table . '_' . $nom; |
|
| 810 | + $exist = ' IF EXISTS'; |
|
| 811 | 811 | |
| 812 | - $query = "DROP INDEX$exist $index"; |
|
| 812 | + $query = "DROP INDEX$exist $index"; |
|
| 813 | 813 | |
| 814 | - return spip_sqlite_query($query, $serveur, $requeter); |
|
| 814 | + return spip_sqlite_query($query, $serveur, $requeter); |
|
| 815 | 815 | } |
| 816 | 816 | |
| 817 | 817 | /** |
@@ -828,29 +828,29 @@ discard block |
||
| 828 | 828 | **/ |
| 829 | 829 | function spip_sqlite_error($query = '', $serveur = '') |
| 830 | 830 | { |
| 831 | - $link = _sqlite_link($serveur); |
|
| 832 | - |
|
| 833 | - if ($link) { |
|
| 834 | - $errs = $link->errorInfo(); |
|
| 835 | - $s = _sqlite_last_error_from_link($link); |
|
| 836 | - } else { |
|
| 837 | - $s = ': aucune ressource sqlite (link)'; |
|
| 838 | - } |
|
| 839 | - if ($s) { |
|
| 840 | - $trace = debug_backtrace(); |
|
| 841 | - if ($trace[0]['function'] != 'spip_sqlite_error') { |
|
| 842 | - spip_log("$s - $query - " . sql_error_backtrace(), 'sqlite.' . _LOG_ERREUR); |
|
| 843 | - } |
|
| 844 | - } |
|
| 845 | - |
|
| 846 | - return $s; |
|
| 831 | + $link = _sqlite_link($serveur); |
|
| 832 | + |
|
| 833 | + if ($link) { |
|
| 834 | + $errs = $link->errorInfo(); |
|
| 835 | + $s = _sqlite_last_error_from_link($link); |
|
| 836 | + } else { |
|
| 837 | + $s = ': aucune ressource sqlite (link)'; |
|
| 838 | + } |
|
| 839 | + if ($s) { |
|
| 840 | + $trace = debug_backtrace(); |
|
| 841 | + if ($trace[0]['function'] != 'spip_sqlite_error') { |
|
| 842 | + spip_log("$s - $query - " . sql_error_backtrace(), 'sqlite.' . _LOG_ERREUR); |
|
| 843 | + } |
|
| 844 | + } |
|
| 845 | + |
|
| 846 | + return $s; |
|
| 847 | 847 | } |
| 848 | 848 | |
| 849 | 849 | function _sqlite_last_error_from_link($link) |
| 850 | 850 | { |
| 851 | - if ($link) { |
|
| 852 | - $errs = $link->errorInfo(); |
|
| 853 | - /* |
|
| 851 | + if ($link) { |
|
| 852 | + $errs = $link->errorInfo(); |
|
| 853 | + /* |
|
| 854 | 854 | $errs[0] |
| 855 | 855 | numero SQLState ('HY000' souvent lors d'une erreur) |
| 856 | 856 | http://www.easysoft.com/developer/interfaces/odbc/sqlstate_status_return_codes.html |
@@ -860,11 +860,11 @@ discard block |
||
| 860 | 860 | $errs[2] |
| 861 | 861 | Le texte du message d'erreur |
| 862 | 862 | */ |
| 863 | - if (ltrim($errs[0], '0')) { // 00000 si pas d'erreur |
|
| 864 | - return "$errs[2]"; |
|
| 865 | - } |
|
| 866 | - } |
|
| 867 | - return ''; |
|
| 863 | + if (ltrim($errs[0], '0')) { // 00000 si pas d'erreur |
|
| 864 | + return "$errs[2]"; |
|
| 865 | + } |
|
| 866 | + } |
|
| 867 | + return ''; |
|
| 868 | 868 | } |
| 869 | 869 | |
| 870 | 870 | /** |
@@ -882,23 +882,23 @@ discard block |
||
| 882 | 882 | **/ |
| 883 | 883 | function spip_sqlite_errno($serveur = '') |
| 884 | 884 | { |
| 885 | - $link = _sqlite_link($serveur); |
|
| 886 | - |
|
| 887 | - if ($link) { |
|
| 888 | - $t = $link->errorInfo(); |
|
| 889 | - $s = ltrim($t[0], '0'); // 00000 si pas d'erreur |
|
| 890 | - if ($s) { |
|
| 891 | - $s .= ' / ' . $t[1]; |
|
| 892 | - } // ajoute l'erreur du moteur SQLite |
|
| 893 | - } else { |
|
| 894 | - $s = ': aucune ressource sqlite (link)'; |
|
| 895 | - } |
|
| 896 | - |
|
| 897 | - if ($s) { |
|
| 898 | - spip_log("Erreur sqlite $s", 'sqlite.' . _LOG_ERREUR); |
|
| 899 | - } |
|
| 900 | - |
|
| 901 | - return $s ? $s : 0; |
|
| 885 | + $link = _sqlite_link($serveur); |
|
| 886 | + |
|
| 887 | + if ($link) { |
|
| 888 | + $t = $link->errorInfo(); |
|
| 889 | + $s = ltrim($t[0], '0'); // 00000 si pas d'erreur |
|
| 890 | + if ($s) { |
|
| 891 | + $s .= ' / ' . $t[1]; |
|
| 892 | + } // ajoute l'erreur du moteur SQLite |
|
| 893 | + } else { |
|
| 894 | + $s = ': aucune ressource sqlite (link)'; |
|
| 895 | + } |
|
| 896 | + |
|
| 897 | + if ($s) { |
|
| 898 | + spip_log("Erreur sqlite $s", 'sqlite.' . _LOG_ERREUR); |
|
| 899 | + } |
|
| 900 | + |
|
| 901 | + return $s ? $s : 0; |
|
| 902 | 902 | } |
| 903 | 903 | |
| 904 | 904 | |
@@ -915,19 +915,19 @@ discard block |
||
| 915 | 915 | */ |
| 916 | 916 | function spip_sqlite_explain($query, $serveur = '', $requeter = true) |
| 917 | 917 | { |
| 918 | - if (strpos(ltrim($query), 'SELECT') !== 0) { |
|
| 919 | - return []; |
|
| 920 | - } |
|
| 921 | - |
|
| 922 | - $query = Sqlite::traduire_requete($query, $serveur); |
|
| 923 | - $query = 'EXPLAIN ' . $query; |
|
| 924 | - if (!$requeter) { |
|
| 925 | - return $query; |
|
| 926 | - } |
|
| 927 | - // on ne trace pas ces requetes, sinon on obtient un tracage sans fin... |
|
| 928 | - $r = Sqlite::executer_requete($query, $serveur, false); |
|
| 929 | - |
|
| 930 | - return $r ? spip_sqlite_fetch($r, null, $serveur) : false; // hum ? etrange ca... a verifier |
|
| 918 | + if (strpos(ltrim($query), 'SELECT') !== 0) { |
|
| 919 | + return []; |
|
| 920 | + } |
|
| 921 | + |
|
| 922 | + $query = Sqlite::traduire_requete($query, $serveur); |
|
| 923 | + $query = 'EXPLAIN ' . $query; |
|
| 924 | + if (!$requeter) { |
|
| 925 | + return $query; |
|
| 926 | + } |
|
| 927 | + // on ne trace pas ces requetes, sinon on obtient un tracage sans fin... |
|
| 928 | + $r = Sqlite::executer_requete($query, $serveur, false); |
|
| 929 | + |
|
| 930 | + return $r ? spip_sqlite_fetch($r, null, $serveur) : false; // hum ? etrange ca... a verifier |
|
| 931 | 931 | } |
| 932 | 932 | |
| 933 | 933 | |
@@ -948,35 +948,35 @@ discard block |
||
| 948 | 948 | function spip_sqlite_fetch($r, $t = '', $serveur = '', $requeter = true) |
| 949 | 949 | { |
| 950 | 950 | |
| 951 | - $link = _sqlite_link($serveur); |
|
| 952 | - $t = $t ? $t : SPIP_SQLITE3_ASSOC; |
|
| 953 | - |
|
| 954 | - if (!$r) { |
|
| 955 | - return false; |
|
| 956 | - } |
|
| 957 | - |
|
| 958 | - $retour = $r->fetch($t); |
|
| 959 | - |
|
| 960 | - if (!$retour) { |
|
| 961 | - if ($r->errorCode() === '00000') { |
|
| 962 | - return null; |
|
| 963 | - } |
|
| 964 | - return false; |
|
| 965 | - } |
|
| 966 | - |
|
| 967 | - // Renvoie des 'table.titre' au lieu de 'titre' tout court ! pff ! |
|
| 968 | - // suppression de 'table.' pour toutes les cles (c'est un peu violent !) |
|
| 969 | - // c'est couteux : on ne verifie que la premiere ligne pour voir si on le fait ou non |
|
| 970 | - if (str_contains(implode('', array_keys($retour)), '.')) { |
|
| 971 | - foreach ($retour as $cle => $val) { |
|
| 972 | - if (($pos = strpos($cle, '.')) !== false) { |
|
| 973 | - $retour[substr($cle, $pos + 1)] = &$retour[$cle]; |
|
| 974 | - unset($retour[$cle]); |
|
| 975 | - } |
|
| 976 | - } |
|
| 977 | - } |
|
| 978 | - |
|
| 979 | - return $retour; |
|
| 951 | + $link = _sqlite_link($serveur); |
|
| 952 | + $t = $t ? $t : SPIP_SQLITE3_ASSOC; |
|
| 953 | + |
|
| 954 | + if (!$r) { |
|
| 955 | + return false; |
|
| 956 | + } |
|
| 957 | + |
|
| 958 | + $retour = $r->fetch($t); |
|
| 959 | + |
|
| 960 | + if (!$retour) { |
|
| 961 | + if ($r->errorCode() === '00000') { |
|
| 962 | + return null; |
|
| 963 | + } |
|
| 964 | + return false; |
|
| 965 | + } |
|
| 966 | + |
|
| 967 | + // Renvoie des 'table.titre' au lieu de 'titre' tout court ! pff ! |
|
| 968 | + // suppression de 'table.' pour toutes les cles (c'est un peu violent !) |
|
| 969 | + // c'est couteux : on ne verifie que la premiere ligne pour voir si on le fait ou non |
|
| 970 | + if (str_contains(implode('', array_keys($retour)), '.')) { |
|
| 971 | + foreach ($retour as $cle => $val) { |
|
| 972 | + if (($pos = strpos($cle, '.')) !== false) { |
|
| 973 | + $retour[substr($cle, $pos + 1)] = &$retour[$cle]; |
|
| 974 | + unset($retour[$cle]); |
|
| 975 | + } |
|
| 976 | + } |
|
| 977 | + } |
|
| 978 | + |
|
| 979 | + return $retour; |
|
| 980 | 980 | } |
| 981 | 981 | |
| 982 | 982 | /** |
@@ -990,8 +990,8 @@ discard block |
||
| 990 | 990 | **/ |
| 991 | 991 | function spip_sqlite_seek($r, $row_number, $serveur = '', $requeter = true) |
| 992 | 992 | { |
| 993 | - // encore un truc de bien fichu : PDO ne PEUT PAS faire de seek ou de rewind... |
|
| 994 | - return false; |
|
| 993 | + // encore un truc de bien fichu : PDO ne PEUT PAS faire de seek ou de rewind... |
|
| 994 | + return false; |
|
| 995 | 995 | } |
| 996 | 996 | |
| 997 | 997 | |
@@ -1008,10 +1008,10 @@ discard block |
||
| 1008 | 1008 | */ |
| 1009 | 1009 | function spip_sqlite_free(&$r, $serveur = '', $requeter = true) |
| 1010 | 1010 | { |
| 1011 | - unset($r); |
|
| 1011 | + unset($r); |
|
| 1012 | 1012 | |
| 1013 | - return true; |
|
| 1014 | - //return sqlite_free_result($r); |
|
| 1013 | + return true; |
|
| 1014 | + //return sqlite_free_result($r); |
|
| 1015 | 1015 | } |
| 1016 | 1016 | |
| 1017 | 1017 | |
@@ -1027,8 +1027,8 @@ discard block |
||
| 1027 | 1027 | */ |
| 1028 | 1028 | function spip_sqlite_get_charset($charset = [], $serveur = '', $requeter = true) |
| 1029 | 1029 | { |
| 1030 | - //$c = !$charset ? '' : (" LIKE "._q($charset['charset'])); |
|
| 1031 | - //return spip_sqlite_fetch(sqlite_query(_sqlite_link($serveur), "SHOW CHARACTER SET$c"), NULL, $serveur); |
|
| 1030 | + //$c = !$charset ? '' : (" LIKE "._q($charset['charset'])); |
|
| 1031 | + //return spip_sqlite_fetch(sqlite_query(_sqlite_link($serveur), "SHOW CHARACTER SET$c"), NULL, $serveur); |
|
| 1032 | 1032 | } |
| 1033 | 1033 | |
| 1034 | 1034 | |
@@ -1044,7 +1044,7 @@ discard block |
||
| 1044 | 1044 | **/ |
| 1045 | 1045 | function spip_sqlite_hex($v) |
| 1046 | 1046 | { |
| 1047 | - return hexdec($v); |
|
| 1047 | + return hexdec($v); |
|
| 1048 | 1048 | } |
| 1049 | 1049 | |
| 1050 | 1050 | |
@@ -1067,7 +1067,7 @@ discard block |
||
| 1067 | 1067 | **/ |
| 1068 | 1068 | function spip_sqlite_in($val, $valeurs, $not = '', $serveur = '', $requeter = true) |
| 1069 | 1069 | { |
| 1070 | - return "($val $not IN ($valeurs))"; |
|
| 1070 | + return "($val $not IN ($valeurs))"; |
|
| 1071 | 1071 | } |
| 1072 | 1072 | |
| 1073 | 1073 | |
@@ -1096,20 +1096,20 @@ discard block |
||
| 1096 | 1096 | function spip_sqlite_insert($table, $champs, $valeurs, $desc = [], $serveur = '', $requeter = true) |
| 1097 | 1097 | { |
| 1098 | 1098 | |
| 1099 | - $query = "INSERT INTO $table " . ($champs ? "$champs VALUES $valeurs" : 'DEFAULT VALUES'); |
|
| 1100 | - if ($r = spip_sqlite_query($query, $serveur, $requeter)) { |
|
| 1101 | - if (!$requeter) { |
|
| 1102 | - return $r; |
|
| 1103 | - } |
|
| 1104 | - $nb = Sqlite::last_insert_id($serveur); |
|
| 1105 | - } else { |
|
| 1106 | - $nb = false; |
|
| 1107 | - } |
|
| 1099 | + $query = "INSERT INTO $table " . ($champs ? "$champs VALUES $valeurs" : 'DEFAULT VALUES'); |
|
| 1100 | + if ($r = spip_sqlite_query($query, $serveur, $requeter)) { |
|
| 1101 | + if (!$requeter) { |
|
| 1102 | + return $r; |
|
| 1103 | + } |
|
| 1104 | + $nb = Sqlite::last_insert_id($serveur); |
|
| 1105 | + } else { |
|
| 1106 | + $nb = false; |
|
| 1107 | + } |
|
| 1108 | 1108 | |
| 1109 | - $err = spip_sqlite_error($query, $serveur); |
|
| 1109 | + $err = spip_sqlite_error($query, $serveur); |
|
| 1110 | 1110 | |
| 1111 | - // cas particulier : ne pas substituer la reponse spip_sqlite_query si on est en profilage |
|
| 1112 | - return isset($_GET['var_profile']) ? $r : $nb; |
|
| 1111 | + // cas particulier : ne pas substituer la reponse spip_sqlite_query si on est en profilage |
|
| 1112 | + return isset($_GET['var_profile']) ? $r : $nb; |
|
| 1113 | 1113 | } |
| 1114 | 1114 | |
| 1115 | 1115 | |
@@ -1135,28 +1135,28 @@ discard block |
||
| 1135 | 1135 | **/ |
| 1136 | 1136 | function spip_sqlite_insertq($table, $couples = [], $desc = [], $serveur = '', $requeter = true) |
| 1137 | 1137 | { |
| 1138 | - if (!$desc) { |
|
| 1139 | - $desc = description_table($table, $serveur); |
|
| 1140 | - } |
|
| 1141 | - if (!$desc) { |
|
| 1142 | - die("$table insertion sans description"); |
|
| 1143 | - } |
|
| 1144 | - $fields = isset($desc['field']) ? $desc['field'] : []; |
|
| 1145 | - |
|
| 1146 | - foreach ($couples as $champ => $val) { |
|
| 1147 | - $couples[$champ] = _sqlite_calculer_cite($val, $fields[$champ]); |
|
| 1148 | - } |
|
| 1149 | - |
|
| 1150 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1151 | - $couples = _sqlite_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1152 | - |
|
| 1153 | - $cles = $valeurs = ''; |
|
| 1154 | - if (count($couples)) { |
|
| 1155 | - $cles = '(' . join(',', array_keys($couples)) . ')'; |
|
| 1156 | - $valeurs = '(' . join(',', $couples) . ')'; |
|
| 1157 | - } |
|
| 1158 | - |
|
| 1159 | - return spip_sqlite_insert($table, $cles, $valeurs, $desc, $serveur, $requeter); |
|
| 1138 | + if (!$desc) { |
|
| 1139 | + $desc = description_table($table, $serveur); |
|
| 1140 | + } |
|
| 1141 | + if (!$desc) { |
|
| 1142 | + die("$table insertion sans description"); |
|
| 1143 | + } |
|
| 1144 | + $fields = isset($desc['field']) ? $desc['field'] : []; |
|
| 1145 | + |
|
| 1146 | + foreach ($couples as $champ => $val) { |
|
| 1147 | + $couples[$champ] = _sqlite_calculer_cite($val, $fields[$champ]); |
|
| 1148 | + } |
|
| 1149 | + |
|
| 1150 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1151 | + $couples = _sqlite_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1152 | + |
|
| 1153 | + $cles = $valeurs = ''; |
|
| 1154 | + if (count($couples)) { |
|
| 1155 | + $cles = '(' . join(',', array_keys($couples)) . ')'; |
|
| 1156 | + $valeurs = '(' . join(',', $couples) . ')'; |
|
| 1157 | + } |
|
| 1158 | + |
|
| 1159 | + return spip_sqlite_insert($table, $cles, $valeurs, $desc, $serveur, $requeter); |
|
| 1160 | 1160 | } |
| 1161 | 1161 | |
| 1162 | 1162 | |
@@ -1181,70 +1181,70 @@ discard block |
||
| 1181 | 1181 | **/ |
| 1182 | 1182 | function spip_sqlite_insertq_multi($table, $tab_couples = [], $desc = [], $serveur = '', $requeter = true) |
| 1183 | 1183 | { |
| 1184 | - if (!$desc) { |
|
| 1185 | - $desc = description_table($table, $serveur); |
|
| 1186 | - } |
|
| 1187 | - if (!$desc) { |
|
| 1188 | - die("$table insertion sans description"); |
|
| 1189 | - } |
|
| 1190 | - if (!isset($desc['field'])) { |
|
| 1191 | - $desc['field'] = []; |
|
| 1192 | - } |
|
| 1193 | - |
|
| 1194 | - // recuperer les champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1195 | - $maj = _sqlite_ajouter_champs_timestamp($table, [], $desc, $serveur); |
|
| 1196 | - |
|
| 1197 | - // seul le nom de la table est a traduire ici : |
|
| 1198 | - // le faire une seule fois au debut |
|
| 1199 | - $query_start = "INSERT INTO $table "; |
|
| 1200 | - $query_start = Sqlite::traduire_requete($query_start, $serveur); |
|
| 1201 | - |
|
| 1202 | - // ouvrir une transaction |
|
| 1203 | - if ($requeter) { |
|
| 1204 | - Sqlite::demarrer_transaction($serveur); |
|
| 1205 | - } |
|
| 1206 | - |
|
| 1207 | - while ($couples = array_shift($tab_couples)) { |
|
| 1208 | - foreach ($couples as $champ => $val) { |
|
| 1209 | - $couples[$champ] = _sqlite_calculer_cite($val, $desc['field'][$champ]); |
|
| 1210 | - } |
|
| 1211 | - |
|
| 1212 | - // inserer les champs timestamp par defaut |
|
| 1213 | - $couples = array_merge($maj, $couples); |
|
| 1214 | - |
|
| 1215 | - $champs = $valeurs = ''; |
|
| 1216 | - if (count($couples)) { |
|
| 1217 | - $champs = '(' . join(',', array_keys($couples)) . ')'; |
|
| 1218 | - $valeurs = '(' . join(',', $couples) . ')'; |
|
| 1219 | - $query = $query_start . "$champs VALUES $valeurs"; |
|
| 1220 | - } else { |
|
| 1221 | - $query = $query_start . 'DEFAULT VALUES'; |
|
| 1222 | - } |
|
| 1223 | - |
|
| 1224 | - if ($requeter) { |
|
| 1225 | - $retour = Sqlite::executer_requete($query, $serveur); |
|
| 1226 | - } |
|
| 1227 | - |
|
| 1228 | - // sur le dernier couple uniquement |
|
| 1229 | - if (!count($tab_couples)) { |
|
| 1230 | - $nb = 0; |
|
| 1231 | - if ($requeter) { |
|
| 1232 | - $nb = Sqlite::last_insert_id($serveur); |
|
| 1233 | - } else { |
|
| 1234 | - return $query; |
|
| 1235 | - } |
|
| 1236 | - } |
|
| 1237 | - |
|
| 1238 | - $err = spip_sqlite_error($query, $serveur); |
|
| 1239 | - } |
|
| 1240 | - |
|
| 1241 | - if ($requeter) { |
|
| 1242 | - Sqlite::finir_transaction($serveur); |
|
| 1243 | - } |
|
| 1244 | - |
|
| 1245 | - // renvoie le dernier id d'autoincrement ajoute |
|
| 1246 | - // cas particulier : ne pas substituer la reponse spip_sqlite_query si on est en profilage |
|
| 1247 | - return isset($_GET['var_profile']) ? $retour : $nb; |
|
| 1184 | + if (!$desc) { |
|
| 1185 | + $desc = description_table($table, $serveur); |
|
| 1186 | + } |
|
| 1187 | + if (!$desc) { |
|
| 1188 | + die("$table insertion sans description"); |
|
| 1189 | + } |
|
| 1190 | + if (!isset($desc['field'])) { |
|
| 1191 | + $desc['field'] = []; |
|
| 1192 | + } |
|
| 1193 | + |
|
| 1194 | + // recuperer les champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1195 | + $maj = _sqlite_ajouter_champs_timestamp($table, [], $desc, $serveur); |
|
| 1196 | + |
|
| 1197 | + // seul le nom de la table est a traduire ici : |
|
| 1198 | + // le faire une seule fois au debut |
|
| 1199 | + $query_start = "INSERT INTO $table "; |
|
| 1200 | + $query_start = Sqlite::traduire_requete($query_start, $serveur); |
|
| 1201 | + |
|
| 1202 | + // ouvrir une transaction |
|
| 1203 | + if ($requeter) { |
|
| 1204 | + Sqlite::demarrer_transaction($serveur); |
|
| 1205 | + } |
|
| 1206 | + |
|
| 1207 | + while ($couples = array_shift($tab_couples)) { |
|
| 1208 | + foreach ($couples as $champ => $val) { |
|
| 1209 | + $couples[$champ] = _sqlite_calculer_cite($val, $desc['field'][$champ]); |
|
| 1210 | + } |
|
| 1211 | + |
|
| 1212 | + // inserer les champs timestamp par defaut |
|
| 1213 | + $couples = array_merge($maj, $couples); |
|
| 1214 | + |
|
| 1215 | + $champs = $valeurs = ''; |
|
| 1216 | + if (count($couples)) { |
|
| 1217 | + $champs = '(' . join(',', array_keys($couples)) . ')'; |
|
| 1218 | + $valeurs = '(' . join(',', $couples) . ')'; |
|
| 1219 | + $query = $query_start . "$champs VALUES $valeurs"; |
|
| 1220 | + } else { |
|
| 1221 | + $query = $query_start . 'DEFAULT VALUES'; |
|
| 1222 | + } |
|
| 1223 | + |
|
| 1224 | + if ($requeter) { |
|
| 1225 | + $retour = Sqlite::executer_requete($query, $serveur); |
|
| 1226 | + } |
|
| 1227 | + |
|
| 1228 | + // sur le dernier couple uniquement |
|
| 1229 | + if (!count($tab_couples)) { |
|
| 1230 | + $nb = 0; |
|
| 1231 | + if ($requeter) { |
|
| 1232 | + $nb = Sqlite::last_insert_id($serveur); |
|
| 1233 | + } else { |
|
| 1234 | + return $query; |
|
| 1235 | + } |
|
| 1236 | + } |
|
| 1237 | + |
|
| 1238 | + $err = spip_sqlite_error($query, $serveur); |
|
| 1239 | + } |
|
| 1240 | + |
|
| 1241 | + if ($requeter) { |
|
| 1242 | + Sqlite::finir_transaction($serveur); |
|
| 1243 | + } |
|
| 1244 | + |
|
| 1245 | + // renvoie le dernier id d'autoincrement ajoute |
|
| 1246 | + // cas particulier : ne pas substituer la reponse spip_sqlite_query si on est en profilage |
|
| 1247 | + return isset($_GET['var_profile']) ? $retour : $nb; |
|
| 1248 | 1248 | } |
| 1249 | 1249 | |
| 1250 | 1250 | |
@@ -1260,7 +1260,7 @@ discard block |
||
| 1260 | 1260 | **/ |
| 1261 | 1261 | function spip_sqlite_preferer_transaction($serveur = '', $requeter = true) |
| 1262 | 1262 | { |
| 1263 | - return true; |
|
| 1263 | + return true; |
|
| 1264 | 1264 | } |
| 1265 | 1265 | |
| 1266 | 1266 | /** |
@@ -1278,12 +1278,12 @@ discard block |
||
| 1278 | 1278 | **/ |
| 1279 | 1279 | function spip_sqlite_demarrer_transaction($serveur = '', $requeter = true) |
| 1280 | 1280 | { |
| 1281 | - if (!$requeter) { |
|
| 1282 | - return 'BEGIN TRANSACTION'; |
|
| 1283 | - } |
|
| 1284 | - Sqlite::demarrer_transaction($serveur); |
|
| 1281 | + if (!$requeter) { |
|
| 1282 | + return 'BEGIN TRANSACTION'; |
|
| 1283 | + } |
|
| 1284 | + Sqlite::demarrer_transaction($serveur); |
|
| 1285 | 1285 | |
| 1286 | - return true; |
|
| 1286 | + return true; |
|
| 1287 | 1287 | } |
| 1288 | 1288 | |
| 1289 | 1289 | /** |
@@ -1298,12 +1298,12 @@ discard block |
||
| 1298 | 1298 | **/ |
| 1299 | 1299 | function spip_sqlite_terminer_transaction($serveur = '', $requeter = true) |
| 1300 | 1300 | { |
| 1301 | - if (!$requeter) { |
|
| 1302 | - return 'COMMIT'; |
|
| 1303 | - } |
|
| 1304 | - Sqlite::finir_transaction($serveur); |
|
| 1301 | + if (!$requeter) { |
|
| 1302 | + return 'COMMIT'; |
|
| 1303 | + } |
|
| 1304 | + Sqlite::finir_transaction($serveur); |
|
| 1305 | 1305 | |
| 1306 | - return true; |
|
| 1306 | + return true; |
|
| 1307 | 1307 | } |
| 1308 | 1308 | |
| 1309 | 1309 | |
@@ -1319,27 +1319,27 @@ discard block |
||
| 1319 | 1319 | **/ |
| 1320 | 1320 | function spip_sqlite_listdbs($serveur = '', $requeter = true) |
| 1321 | 1321 | { |
| 1322 | - _sqlite_init(); |
|
| 1323 | - |
|
| 1324 | - if (!is_dir($d = substr(_DIR_DB, 0, -1))) { |
|
| 1325 | - return []; |
|
| 1326 | - } |
|
| 1327 | - |
|
| 1328 | - include_spip('inc/flock'); |
|
| 1329 | - $bases = preg_files($d, $pattern = '(.*)\.sqlite$'); |
|
| 1330 | - $bds = []; |
|
| 1331 | - |
|
| 1332 | - foreach ($bases as $b) { |
|
| 1333 | - // pas de bases commencant pas sqlite |
|
| 1334 | - // (on s'en sert pour l'installation pour simuler la presence d'un serveur) |
|
| 1335 | - // les bases sont de la forme _sqliteX_tmp_spip_install.sqlite |
|
| 1336 | - if (strpos($b, '_sqlite')) { |
|
| 1337 | - continue; |
|
| 1338 | - } |
|
| 1339 | - $bds[] = preg_replace(";.*/$pattern;iS", '$1', $b); |
|
| 1340 | - } |
|
| 1341 | - |
|
| 1342 | - return $bds; |
|
| 1322 | + _sqlite_init(); |
|
| 1323 | + |
|
| 1324 | + if (!is_dir($d = substr(_DIR_DB, 0, -1))) { |
|
| 1325 | + return []; |
|
| 1326 | + } |
|
| 1327 | + |
|
| 1328 | + include_spip('inc/flock'); |
|
| 1329 | + $bases = preg_files($d, $pattern = '(.*)\.sqlite$'); |
|
| 1330 | + $bds = []; |
|
| 1331 | + |
|
| 1332 | + foreach ($bases as $b) { |
|
| 1333 | + // pas de bases commencant pas sqlite |
|
| 1334 | + // (on s'en sert pour l'installation pour simuler la presence d'un serveur) |
|
| 1335 | + // les bases sont de la forme _sqliteX_tmp_spip_install.sqlite |
|
| 1336 | + if (strpos($b, '_sqlite')) { |
|
| 1337 | + continue; |
|
| 1338 | + } |
|
| 1339 | + $bds[] = preg_replace(";.*/$pattern;iS", '$1', $b); |
|
| 1340 | + } |
|
| 1341 | + |
|
| 1342 | + return $bds; |
|
| 1343 | 1343 | } |
| 1344 | 1344 | |
| 1345 | 1345 | |
@@ -1355,9 +1355,9 @@ discard block |
||
| 1355 | 1355 | */ |
| 1356 | 1356 | function spip_sqlite_multi($objet, $lang) |
| 1357 | 1357 | { |
| 1358 | - $r = 'EXTRAIRE_MULTI(' . $objet . ", '" . $lang . "') AS multi"; |
|
| 1358 | + $r = 'EXTRAIRE_MULTI(' . $objet . ", '" . $lang . "') AS multi"; |
|
| 1359 | 1359 | |
| 1360 | - return $r; |
|
| 1360 | + return $r; |
|
| 1361 | 1361 | } |
| 1362 | 1362 | |
| 1363 | 1363 | |
@@ -1375,15 +1375,15 @@ discard block |
||
| 1375 | 1375 | **/ |
| 1376 | 1376 | function spip_sqlite_optimize($table, $serveur = '', $requeter = true) |
| 1377 | 1377 | { |
| 1378 | - static $do = false; |
|
| 1379 | - if ($requeter and $do) { |
|
| 1380 | - return true; |
|
| 1381 | - } |
|
| 1382 | - if ($requeter) { |
|
| 1383 | - $do = true; |
|
| 1384 | - } |
|
| 1385 | - |
|
| 1386 | - return spip_sqlite_query('VACUUM', $serveur, $requeter); |
|
| 1378 | + static $do = false; |
|
| 1379 | + if ($requeter and $do) { |
|
| 1380 | + return true; |
|
| 1381 | + } |
|
| 1382 | + if ($requeter) { |
|
| 1383 | + $do = true; |
|
| 1384 | + } |
|
| 1385 | + |
|
| 1386 | + return spip_sqlite_query('VACUUM', $serveur, $requeter); |
|
| 1387 | 1387 | } |
| 1388 | 1388 | |
| 1389 | 1389 | |
@@ -1401,15 +1401,15 @@ discard block |
||
| 1401 | 1401 | */ |
| 1402 | 1402 | function spip_sqlite_quote($v, $type = '') |
| 1403 | 1403 | { |
| 1404 | - if (!is_array($v)) { |
|
| 1405 | - return _sqlite_calculer_cite($v, $type); |
|
| 1406 | - } |
|
| 1407 | - // si c'est un tableau, le parcourir en propageant le type |
|
| 1408 | - foreach ($v as $k => $r) { |
|
| 1409 | - $v[$k] = spip_sqlite_quote($r, $type); |
|
| 1410 | - } |
|
| 1411 | - |
|
| 1412 | - return join(',', $v); |
|
| 1404 | + if (!is_array($v)) { |
|
| 1405 | + return _sqlite_calculer_cite($v, $type); |
|
| 1406 | + } |
|
| 1407 | + // si c'est un tableau, le parcourir en propageant le type |
|
| 1408 | + foreach ($v as $k => $r) { |
|
| 1409 | + $v[$k] = spip_sqlite_quote($r, $type); |
|
| 1410 | + } |
|
| 1411 | + |
|
| 1412 | + return join(',', $v); |
|
| 1413 | 1413 | } |
| 1414 | 1414 | |
| 1415 | 1415 | |
@@ -1427,9 +1427,9 @@ discard block |
||
| 1427 | 1427 | **/ |
| 1428 | 1428 | function spip_sqlite_date_proche($champ, $interval, $unite) |
| 1429 | 1429 | { |
| 1430 | - $op = (($interval <= 0) ? '>' : '<'); |
|
| 1430 | + $op = (($interval <= 0) ? '>' : '<'); |
|
| 1431 | 1431 | |
| 1432 | - return "($champ $op datetime('" . date('Y-m-d H:i:s') . "', '$interval $unite'))"; |
|
| 1432 | + return "($champ $op datetime('" . date('Y-m-d H:i:s') . "', '$interval $unite'))"; |
|
| 1433 | 1433 | } |
| 1434 | 1434 | |
| 1435 | 1435 | |
@@ -1448,48 +1448,48 @@ discard block |
||
| 1448 | 1448 | */ |
| 1449 | 1449 | function spip_sqlite_repair($table, $serveur = '', $requeter = true) |
| 1450 | 1450 | { |
| 1451 | - if ( |
|
| 1452 | - $desc = spip_sqlite_showtable($table, $serveur) |
|
| 1453 | - and isset($desc['field']) |
|
| 1454 | - and is_array($desc['field']) |
|
| 1455 | - ) { |
|
| 1456 | - foreach ($desc['field'] as $c => $d) { |
|
| 1457 | - if ( |
|
| 1458 | - preg_match(',^(tinytext|mediumtext|text|longtext|varchar|char),i', $d) |
|
| 1459 | - and stripos($d, 'NOT NULL') !== false |
|
| 1460 | - and stripos($d, 'DEFAULT') === false |
|
| 1461 | - /* pas touche aux cles primaires */ |
|
| 1462 | - and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1463 | - ) { |
|
| 1464 | - spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT ''", $serveur); |
|
| 1465 | - spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1466 | - } |
|
| 1467 | - if ( |
|
| 1468 | - preg_match(',^(INTEGER),i', $d) |
|
| 1469 | - and stripos($d, 'NOT NULL') !== false |
|
| 1470 | - and stripos($d, 'DEFAULT') === false |
|
| 1471 | - /* pas touche aux cles primaires */ |
|
| 1472 | - and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1473 | - ) { |
|
| 1474 | - spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT '0'", $serveur); |
|
| 1475 | - spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1476 | - } |
|
| 1477 | - if ( |
|
| 1478 | - preg_match(',^(datetime),i', $d) |
|
| 1479 | - and stripos($d, 'NOT NULL') !== false |
|
| 1480 | - and stripos($d, 'DEFAULT') === false |
|
| 1481 | - /* pas touche aux cles primaires */ |
|
| 1482 | - and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1483 | - ) { |
|
| 1484 | - spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT '0000-00-00 00:00:00'", $serveur); |
|
| 1485 | - spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1486 | - } |
|
| 1487 | - } |
|
| 1488 | - |
|
| 1489 | - return [' OK ']; |
|
| 1490 | - } |
|
| 1491 | - |
|
| 1492 | - return [' ERROR ']; |
|
| 1451 | + if ( |
|
| 1452 | + $desc = spip_sqlite_showtable($table, $serveur) |
|
| 1453 | + and isset($desc['field']) |
|
| 1454 | + and is_array($desc['field']) |
|
| 1455 | + ) { |
|
| 1456 | + foreach ($desc['field'] as $c => $d) { |
|
| 1457 | + if ( |
|
| 1458 | + preg_match(',^(tinytext|mediumtext|text|longtext|varchar|char),i', $d) |
|
| 1459 | + and stripos($d, 'NOT NULL') !== false |
|
| 1460 | + and stripos($d, 'DEFAULT') === false |
|
| 1461 | + /* pas touche aux cles primaires */ |
|
| 1462 | + and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1463 | + ) { |
|
| 1464 | + spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT ''", $serveur); |
|
| 1465 | + spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1466 | + } |
|
| 1467 | + if ( |
|
| 1468 | + preg_match(',^(INTEGER),i', $d) |
|
| 1469 | + and stripos($d, 'NOT NULL') !== false |
|
| 1470 | + and stripos($d, 'DEFAULT') === false |
|
| 1471 | + /* pas touche aux cles primaires */ |
|
| 1472 | + and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1473 | + ) { |
|
| 1474 | + spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT '0'", $serveur); |
|
| 1475 | + spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1476 | + } |
|
| 1477 | + if ( |
|
| 1478 | + preg_match(',^(datetime),i', $d) |
|
| 1479 | + and stripos($d, 'NOT NULL') !== false |
|
| 1480 | + and stripos($d, 'DEFAULT') === false |
|
| 1481 | + /* pas touche aux cles primaires */ |
|
| 1482 | + and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1483 | + ) { |
|
| 1484 | + spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT '0000-00-00 00:00:00'", $serveur); |
|
| 1485 | + spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1486 | + } |
|
| 1487 | + } |
|
| 1488 | + |
|
| 1489 | + return [' OK ']; |
|
| 1490 | + } |
|
| 1491 | + |
|
| 1492 | + return [' ERROR ']; |
|
| 1493 | 1493 | } |
| 1494 | 1494 | |
| 1495 | 1495 | |
@@ -1519,25 +1519,25 @@ discard block |
||
| 1519 | 1519 | **/ |
| 1520 | 1520 | function spip_sqlite_replace($table, $couples, $desc = [], $serveur = '', $requeter = true) |
| 1521 | 1521 | { |
| 1522 | - if (!$desc) { |
|
| 1523 | - $desc = description_table($table, $serveur); |
|
| 1524 | - } |
|
| 1525 | - if (!$desc) { |
|
| 1526 | - die("$table insertion sans description"); |
|
| 1527 | - } |
|
| 1528 | - $fields = isset($desc['field']) ? $desc['field'] : []; |
|
| 1529 | - |
|
| 1530 | - foreach ($couples as $champ => $val) { |
|
| 1531 | - $couples[$champ] = _sqlite_calculer_cite($val, $fields[$champ]); |
|
| 1532 | - } |
|
| 1533 | - |
|
| 1534 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1535 | - $couples = _sqlite_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1536 | - |
|
| 1537 | - return spip_sqlite_query("REPLACE INTO $table (" . join(',', array_keys($couples)) . ') VALUES (' . join( |
|
| 1538 | - ',', |
|
| 1539 | - $couples |
|
| 1540 | - ) . ')', $serveur); |
|
| 1522 | + if (!$desc) { |
|
| 1523 | + $desc = description_table($table, $serveur); |
|
| 1524 | + } |
|
| 1525 | + if (!$desc) { |
|
| 1526 | + die("$table insertion sans description"); |
|
| 1527 | + } |
|
| 1528 | + $fields = isset($desc['field']) ? $desc['field'] : []; |
|
| 1529 | + |
|
| 1530 | + foreach ($couples as $champ => $val) { |
|
| 1531 | + $couples[$champ] = _sqlite_calculer_cite($val, $fields[$champ]); |
|
| 1532 | + } |
|
| 1533 | + |
|
| 1534 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1535 | + $couples = _sqlite_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1536 | + |
|
| 1537 | + return spip_sqlite_query("REPLACE INTO $table (" . join(',', array_keys($couples)) . ') VALUES (' . join( |
|
| 1538 | + ',', |
|
| 1539 | + $couples |
|
| 1540 | + ) . ')', $serveur); |
|
| 1541 | 1541 | } |
| 1542 | 1542 | |
| 1543 | 1543 | |
@@ -1568,13 +1568,13 @@ discard block |
||
| 1568 | 1568 | function spip_sqlite_replace_multi($table, $tab_couples, $desc = [], $serveur = '', $requeter = true) |
| 1569 | 1569 | { |
| 1570 | 1570 | |
| 1571 | - // boucler pour trainter chaque requete independemment |
|
| 1572 | - foreach ($tab_couples as $couples) { |
|
| 1573 | - $retour = spip_sqlite_replace($table, $couples, $desc, $serveur, $requeter); |
|
| 1574 | - } |
|
| 1571 | + // boucler pour trainter chaque requete independemment |
|
| 1572 | + foreach ($tab_couples as $couples) { |
|
| 1573 | + $retour = spip_sqlite_replace($table, $couples, $desc, $serveur, $requeter); |
|
| 1574 | + } |
|
| 1575 | 1575 | |
| 1576 | - // renvoie le dernier id |
|
| 1577 | - return $retour; |
|
| 1576 | + // renvoie le dernier id |
|
| 1577 | + return $retour; |
|
| 1578 | 1578 | } |
| 1579 | 1579 | |
| 1580 | 1580 | |
@@ -1601,44 +1601,44 @@ discard block |
||
| 1601 | 1601 | * - array : Tableau décrivant requête et temps d'exécution si var_profile actif pour tracer. |
| 1602 | 1602 | */ |
| 1603 | 1603 | function spip_sqlite_select( |
| 1604 | - $select, |
|
| 1605 | - $from, |
|
| 1606 | - $where = '', |
|
| 1607 | - $groupby = '', |
|
| 1608 | - $orderby = '', |
|
| 1609 | - $limit = '', |
|
| 1610 | - $having = '', |
|
| 1611 | - $serveur = '', |
|
| 1612 | - $requeter = true |
|
| 1604 | + $select, |
|
| 1605 | + $from, |
|
| 1606 | + $where = '', |
|
| 1607 | + $groupby = '', |
|
| 1608 | + $orderby = '', |
|
| 1609 | + $limit = '', |
|
| 1610 | + $having = '', |
|
| 1611 | + $serveur = '', |
|
| 1612 | + $requeter = true |
|
| 1613 | 1613 | ) { |
| 1614 | 1614 | |
| 1615 | - // version() n'est pas connu de sqlite |
|
| 1616 | - $select = str_replace('version()', 'sqlite_version()', $select); |
|
| 1617 | - |
|
| 1618 | - // recomposer from |
|
| 1619 | - $from = (!is_array($from) ? $from : _sqlite_calculer_select_as($from)); |
|
| 1620 | - |
|
| 1621 | - $query = |
|
| 1622 | - _sqlite_calculer_expression('SELECT', $select, ', ') |
|
| 1623 | - . _sqlite_calculer_expression('FROM', $from, ', ') |
|
| 1624 | - . _sqlite_calculer_expression('WHERE', $where) |
|
| 1625 | - . _sqlite_calculer_expression('GROUP BY', $groupby, ',') |
|
| 1626 | - . _sqlite_calculer_expression('HAVING', $having) |
|
| 1627 | - . ($orderby ? ("\nORDER BY " . _sqlite_calculer_order($orderby)) : '') |
|
| 1628 | - . ($limit ? "\nLIMIT $limit" : ''); |
|
| 1629 | - |
|
| 1630 | - // dans un select, on doit renvoyer la requête en cas d'erreur |
|
| 1631 | - $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1632 | - // texte de la requete demande ? |
|
| 1633 | - if (!$requeter) { |
|
| 1634 | - return $res; |
|
| 1635 | - } |
|
| 1636 | - // erreur survenue ? |
|
| 1637 | - if ($res === false) { |
|
| 1638 | - return Sqlite::traduire_requete($query, $serveur); |
|
| 1639 | - } |
|
| 1640 | - |
|
| 1641 | - return $res; |
|
| 1615 | + // version() n'est pas connu de sqlite |
|
| 1616 | + $select = str_replace('version()', 'sqlite_version()', $select); |
|
| 1617 | + |
|
| 1618 | + // recomposer from |
|
| 1619 | + $from = (!is_array($from) ? $from : _sqlite_calculer_select_as($from)); |
|
| 1620 | + |
|
| 1621 | + $query = |
|
| 1622 | + _sqlite_calculer_expression('SELECT', $select, ', ') |
|
| 1623 | + . _sqlite_calculer_expression('FROM', $from, ', ') |
|
| 1624 | + . _sqlite_calculer_expression('WHERE', $where) |
|
| 1625 | + . _sqlite_calculer_expression('GROUP BY', $groupby, ',') |
|
| 1626 | + . _sqlite_calculer_expression('HAVING', $having) |
|
| 1627 | + . ($orderby ? ("\nORDER BY " . _sqlite_calculer_order($orderby)) : '') |
|
| 1628 | + . ($limit ? "\nLIMIT $limit" : ''); |
|
| 1629 | + |
|
| 1630 | + // dans un select, on doit renvoyer la requête en cas d'erreur |
|
| 1631 | + $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1632 | + // texte de la requete demande ? |
|
| 1633 | + if (!$requeter) { |
|
| 1634 | + return $res; |
|
| 1635 | + } |
|
| 1636 | + // erreur survenue ? |
|
| 1637 | + if ($res === false) { |
|
| 1638 | + return Sqlite::traduire_requete($query, $serveur); |
|
| 1639 | + } |
|
| 1640 | + |
|
| 1641 | + return $res; |
|
| 1642 | 1642 | } |
| 1643 | 1643 | |
| 1644 | 1644 | |
@@ -1658,32 +1658,32 @@ discard block |
||
| 1658 | 1658 | **/ |
| 1659 | 1659 | function spip_sqlite_selectdb($db, $serveur = '', $requeter = true) |
| 1660 | 1660 | { |
| 1661 | - _sqlite_init(); |
|
| 1662 | - |
|
| 1663 | - // interdire la creation d'une nouvelle base, |
|
| 1664 | - // sauf si on est dans l'installation |
|
| 1665 | - if ( |
|
| 1666 | - !is_file($f = _DIR_DB . $db . '.sqlite') |
|
| 1667 | - && (!defined('_ECRIRE_INSTALL') || !_ECRIRE_INSTALL) |
|
| 1668 | - ) { |
|
| 1669 | - spip_log("Il est interdit de creer la base $db", 'sqlite.' . _LOG_HS); |
|
| 1670 | - |
|
| 1671 | - return false; |
|
| 1672 | - } |
|
| 1673 | - |
|
| 1674 | - // se connecter a la base indiquee |
|
| 1675 | - // avec les identifiants connus |
|
| 1676 | - $index = $serveur ? $serveur : 0; |
|
| 1677 | - |
|
| 1678 | - if ($link = spip_connect_db('', '', '', '', '@selectdb@' . $db, $serveur, '', '')) { |
|
| 1679 | - if (($db == $link['db']) && $GLOBALS['connexions'][$index] = $link) { |
|
| 1680 | - return $db; |
|
| 1681 | - } |
|
| 1682 | - } else { |
|
| 1683 | - spip_log("Impossible de selectionner la base $db", 'sqlite.' . _LOG_HS); |
|
| 1684 | - } |
|
| 1685 | - |
|
| 1686 | - return false; |
|
| 1661 | + _sqlite_init(); |
|
| 1662 | + |
|
| 1663 | + // interdire la creation d'une nouvelle base, |
|
| 1664 | + // sauf si on est dans l'installation |
|
| 1665 | + if ( |
|
| 1666 | + !is_file($f = _DIR_DB . $db . '.sqlite') |
|
| 1667 | + && (!defined('_ECRIRE_INSTALL') || !_ECRIRE_INSTALL) |
|
| 1668 | + ) { |
|
| 1669 | + spip_log("Il est interdit de creer la base $db", 'sqlite.' . _LOG_HS); |
|
| 1670 | + |
|
| 1671 | + return false; |
|
| 1672 | + } |
|
| 1673 | + |
|
| 1674 | + // se connecter a la base indiquee |
|
| 1675 | + // avec les identifiants connus |
|
| 1676 | + $index = $serveur ? $serveur : 0; |
|
| 1677 | + |
|
| 1678 | + if ($link = spip_connect_db('', '', '', '', '@selectdb@' . $db, $serveur, '', '')) { |
|
| 1679 | + if (($db == $link['db']) && $GLOBALS['connexions'][$index] = $link) { |
|
| 1680 | + return $db; |
|
| 1681 | + } |
|
| 1682 | + } else { |
|
| 1683 | + spip_log("Impossible de selectionner la base $db", 'sqlite.' . _LOG_HS); |
|
| 1684 | + } |
|
| 1685 | + |
|
| 1686 | + return false; |
|
| 1687 | 1687 | } |
| 1688 | 1688 | |
| 1689 | 1689 | |
@@ -1699,8 +1699,8 @@ discard block |
||
| 1699 | 1699 | */ |
| 1700 | 1700 | function spip_sqlite_set_charset($charset, $serveur = '', $requeter = true) |
| 1701 | 1701 | { |
| 1702 | - # spip_log("Gestion charset sql a ecrire : "."SET NAMES "._q($charset), 'sqlite.'._LOG_ERREUR); |
|
| 1703 | - # return spip_sqlite_query("SET NAMES ". spip_sqlite_quote($charset), $serveur); //<-- Passe pas ! |
|
| 1702 | + # spip_log("Gestion charset sql a ecrire : "."SET NAMES "._q($charset), 'sqlite.'._LOG_ERREUR); |
|
| 1703 | + # return spip_sqlite_query("SET NAMES ". spip_sqlite_quote($charset), $serveur); //<-- Passe pas ! |
|
| 1704 | 1704 | } |
| 1705 | 1705 | |
| 1706 | 1706 | |
@@ -1719,24 +1719,24 @@ discard block |
||
| 1719 | 1719 | **/ |
| 1720 | 1720 | function spip_sqlite_showbase($match, $serveur = '', $requeter = true) |
| 1721 | 1721 | { |
| 1722 | - // type est le type d'entrée : table / index / view |
|
| 1723 | - // on ne retourne que les tables (?) et non les vues... |
|
| 1724 | - # ESCAPE non supporte par les versions sqlite <3 |
|
| 1725 | - # return spip_sqlite_query("SELECT name FROM sqlite_master WHERE type='table' AND tbl_name LIKE "._q($match)." ESCAPE '\'", $serveur, $requeter); |
|
| 1726 | - $match = preg_quote($match); |
|
| 1727 | - $match = str_replace('\\\_', '[[TIRETBAS]]', $match); |
|
| 1728 | - $match = str_replace('\\\%', '[[POURCENT]]', $match); |
|
| 1729 | - $match = str_replace('_', '.', $match); |
|
| 1730 | - $match = str_replace('%', '.*', $match); |
|
| 1731 | - $match = str_replace('[[TIRETBAS]]', '_', $match); |
|
| 1732 | - $match = str_replace('[[POURCENT]]', '%', $match); |
|
| 1733 | - $match = "^$match$"; |
|
| 1734 | - |
|
| 1735 | - return spip_sqlite_query( |
|
| 1736 | - "SELECT name FROM sqlite_master WHERE type='table' AND tbl_name REGEXP " . _q($match), |
|
| 1737 | - $serveur, |
|
| 1738 | - $requeter |
|
| 1739 | - ); |
|
| 1722 | + // type est le type d'entrée : table / index / view |
|
| 1723 | + // on ne retourne que les tables (?) et non les vues... |
|
| 1724 | + # ESCAPE non supporte par les versions sqlite <3 |
|
| 1725 | + # return spip_sqlite_query("SELECT name FROM sqlite_master WHERE type='table' AND tbl_name LIKE "._q($match)." ESCAPE '\'", $serveur, $requeter); |
|
| 1726 | + $match = preg_quote($match); |
|
| 1727 | + $match = str_replace('\\\_', '[[TIRETBAS]]', $match); |
|
| 1728 | + $match = str_replace('\\\%', '[[POURCENT]]', $match); |
|
| 1729 | + $match = str_replace('_', '.', $match); |
|
| 1730 | + $match = str_replace('%', '.*', $match); |
|
| 1731 | + $match = str_replace('[[TIRETBAS]]', '_', $match); |
|
| 1732 | + $match = str_replace('[[POURCENT]]', '%', $match); |
|
| 1733 | + $match = "^$match$"; |
|
| 1734 | + |
|
| 1735 | + return spip_sqlite_query( |
|
| 1736 | + "SELECT name FROM sqlite_master WHERE type='table' AND tbl_name REGEXP " . _q($match), |
|
| 1737 | + $serveur, |
|
| 1738 | + $requeter |
|
| 1739 | + ); |
|
| 1740 | 1740 | } |
| 1741 | 1741 | |
| 1742 | 1742 | /** |
@@ -1755,19 +1755,19 @@ discard block |
||
| 1755 | 1755 | **/ |
| 1756 | 1756 | function spip_sqlite_table_exists(string $table, $serveur = '', $requeter = true) |
| 1757 | 1757 | { |
| 1758 | - $r = spip_sqlite_query( |
|
| 1759 | - 'SELECT name FROM sqlite_master WHERE' |
|
| 1760 | - . ' type=\'table\'' |
|
| 1761 | - . ' AND name=' . spip_sqlite_quote($table, 'string') |
|
| 1762 | - . ' AND name NOT LIKE \'sqlite_%\'', |
|
| 1763 | - $serveur, |
|
| 1764 | - $requeter |
|
| 1765 | - ); |
|
| 1766 | - if (!$requeter) { |
|
| 1767 | - return $r; |
|
| 1768 | - } |
|
| 1769 | - $res = spip_sqlite_fetch($r); |
|
| 1770 | - return (bool) $res; |
|
| 1758 | + $r = spip_sqlite_query( |
|
| 1759 | + 'SELECT name FROM sqlite_master WHERE' |
|
| 1760 | + . ' type=\'table\'' |
|
| 1761 | + . ' AND name=' . spip_sqlite_quote($table, 'string') |
|
| 1762 | + . ' AND name NOT LIKE \'sqlite_%\'', |
|
| 1763 | + $serveur, |
|
| 1764 | + $requeter |
|
| 1765 | + ); |
|
| 1766 | + if (!$requeter) { |
|
| 1767 | + return $r; |
|
| 1768 | + } |
|
| 1769 | + $res = spip_sqlite_fetch($r); |
|
| 1770 | + return (bool) $res; |
|
| 1771 | 1771 | } |
| 1772 | 1772 | |
| 1773 | 1773 | define('_SQLITE_RE_SHOW_TABLE', '/^[^(),]*\(((?:[^()]*\((?:[^()]*\([^()]*\))?[^()]*\)[^()]*)*[^()]*)\)[^()]*$/'); |
@@ -1791,129 +1791,129 @@ discard block |
||
| 1791 | 1791 | */ |
| 1792 | 1792 | function spip_sqlite_showtable($nom_table, $serveur = '', $requeter = true) |
| 1793 | 1793 | { |
| 1794 | - $query = |
|
| 1795 | - 'SELECT sql, type FROM' |
|
| 1796 | - . ' (SELECT * FROM sqlite_master UNION ALL' |
|
| 1797 | - . ' SELECT * FROM sqlite_temp_master)' |
|
| 1798 | - . " WHERE tbl_name LIKE '$nom_table'" |
|
| 1799 | - . " AND type!='meta' AND sql NOT NULL AND name NOT LIKE 'sqlite_%'" |
|
| 1800 | - . ' ORDER BY substr(type,2,1), name'; |
|
| 1801 | - |
|
| 1802 | - $a = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1803 | - if (!$a) { |
|
| 1804 | - return ''; |
|
| 1805 | - } |
|
| 1806 | - if (!$requeter) { |
|
| 1807 | - return $a; |
|
| 1808 | - } |
|
| 1809 | - if (!($a = spip_sqlite_fetch($a, null, $serveur))) { |
|
| 1810 | - return ''; |
|
| 1811 | - } |
|
| 1812 | - $vue = ($a['type'] == 'view'); // table | vue |
|
| 1813 | - |
|
| 1814 | - // c'est une table |
|
| 1815 | - // il faut parser le create |
|
| 1816 | - if (!$vue) { |
|
| 1817 | - if (!preg_match(_SQLITE_RE_SHOW_TABLE, array_shift($a), $r)) { |
|
| 1818 | - return ''; |
|
| 1819 | - } else { |
|
| 1820 | - $desc = $r[1]; |
|
| 1821 | - // extraction d'une KEY éventuelle en prenant garde de ne pas |
|
| 1822 | - // relever un champ dont le nom contient KEY (ex. ID_WHISKEY) |
|
| 1823 | - if (preg_match('/^(.*?),([^,]*\sKEY[ (].*)$/s', $desc, $r)) { |
|
| 1824 | - $namedkeys = $r[2]; |
|
| 1825 | - $desc = $r[1]; |
|
| 1826 | - } else { |
|
| 1827 | - $namedkeys = ''; |
|
| 1828 | - } |
|
| 1829 | - |
|
| 1830 | - $fields = []; |
|
| 1831 | - $keys = []; |
|
| 1832 | - |
|
| 1833 | - // enlever les contenus des valeurs DEFAULT 'xxx' qui pourraient perturber |
|
| 1834 | - // par exemple s'il contiennent une virgule. |
|
| 1835 | - // /!\ cela peut aussi echapper le nom des champs si la table a eu des operations avec SQLite Manager ! |
|
| 1836 | - list($desc, $echaps) = query_echappe_textes($desc); |
|
| 1837 | - |
|
| 1838 | - // separer toutes les descriptions de champs, separes par des virgules |
|
| 1839 | - # /!\ explode peut exploser aussi DECIMAL(10,2) ! |
|
| 1840 | - $k_precedent = null; |
|
| 1841 | - foreach (explode(',', $desc) as $v) { |
|
| 1842 | - preg_match('/^\s*([^\s]+)\s+(.*)/', $v, $r); |
|
| 1843 | - // Les cles de champs peuvent etre entourees |
|
| 1844 | - // de guillements doubles " , simples ', graves ` ou de crochets [ ], ou rien. |
|
| 1845 | - // http://www.sqlite.org/lang_keywords.html |
|
| 1846 | - $k = strtolower(query_reinjecte_textes($r[1], $echaps)); // champ, "champ", [champ]... |
|
| 1847 | - if ($char = strpbrk($k[0], '\'"[`')) { |
|
| 1848 | - $k = trim($k, $char); |
|
| 1849 | - if ($char == '[') { |
|
| 1850 | - $k = rtrim($k, ']'); |
|
| 1851 | - } |
|
| 1852 | - } |
|
| 1853 | - $def = query_reinjecte_textes($r[2], $echaps); // valeur du champ |
|
| 1854 | - |
|
| 1855 | - // rustine pour DECIMAL(10,2) |
|
| 1856 | - // s'il y a une parenthèse fermante dans la clé |
|
| 1857 | - // ou dans la définition sans qu'il n'y ait une ouverture avant |
|
| 1858 | - if (str_contains($k, ')') or preg_match('/^[^\(]*\)/', $def)) { |
|
| 1859 | - $fields[$k_precedent] .= ',' . $k . ' ' . $def; |
|
| 1860 | - continue; |
|
| 1861 | - } |
|
| 1862 | - |
|
| 1863 | - // la primary key peut etre dans une des descriptions de champs |
|
| 1864 | - // et non en fin de table, cas encore decouvert avec Sqlite Manager |
|
| 1865 | - if (stripos($r[2], 'PRIMARY KEY') !== false) { |
|
| 1866 | - $keys['PRIMARY KEY'] = $k; |
|
| 1867 | - } |
|
| 1868 | - |
|
| 1869 | - $fields[$k] = $def; |
|
| 1870 | - $k_precedent = $k; |
|
| 1871 | - } |
|
| 1872 | - // key inclues dans la requete |
|
| 1873 | - foreach (preg_split('/\)\s*(,|$)/', $namedkeys) as $v) { |
|
| 1874 | - if (preg_match('/^\s*([^(]*)\(([^(]*(\(\d+\))?)$/', $v, $r)) { |
|
| 1875 | - $k = str_replace('`', '', trim($r[1])); |
|
| 1876 | - $t = trim(strtolower(str_replace('`', '', $r[2])), '"'); |
|
| 1877 | - if ($k && !isset($keys[$k])) { |
|
| 1878 | - $keys[$k] = $t; |
|
| 1879 | - } else { |
|
| 1880 | - $keys[] = $t; |
|
| 1881 | - } |
|
| 1882 | - } |
|
| 1883 | - } |
|
| 1884 | - // sinon ajouter les key index |
|
| 1885 | - $query = |
|
| 1886 | - 'SELECT name,sql FROM' |
|
| 1887 | - . ' (SELECT * FROM sqlite_master UNION ALL' |
|
| 1888 | - . ' SELECT * FROM sqlite_temp_master)' |
|
| 1889 | - . " WHERE tbl_name LIKE '$nom_table'" |
|
| 1890 | - . " AND type='index' AND name NOT LIKE 'sqlite_%'" |
|
| 1891 | - . 'ORDER BY substr(type,2,1), name'; |
|
| 1892 | - $a = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1893 | - while ($r = spip_sqlite_fetch($a, null, $serveur)) { |
|
| 1894 | - $key = str_replace($nom_table . '_', '', $r['name']); // enlever le nom de la table ajoute a l'index |
|
| 1895 | - $keytype = 'KEY'; |
|
| 1896 | - if (strpos($r['sql'], 'UNIQUE INDEX') !== false) { |
|
| 1897 | - $keytype = 'UNIQUE KEY'; |
|
| 1898 | - } |
|
| 1899 | - $colonnes = preg_replace(',.*\((.*)\).*,', '$1', $r['sql']); |
|
| 1900 | - $keys[$keytype . ' ' . $key] = $colonnes; |
|
| 1901 | - } |
|
| 1902 | - } |
|
| 1903 | - } // c'est une vue, on liste les champs disponibles simplement |
|
| 1904 | - else { |
|
| 1905 | - if ($res = sql_fetsel('*', $nom_table, '', '', '', '1', '', $serveur)) { // limit 1 |
|
| 1906 | - $fields = []; |
|
| 1907 | - foreach ($res as $c => $v) { |
|
| 1908 | - $fields[$c] = ''; |
|
| 1909 | - } |
|
| 1910 | - $keys = []; |
|
| 1911 | - } else { |
|
| 1912 | - return ''; |
|
| 1913 | - } |
|
| 1914 | - } |
|
| 1915 | - |
|
| 1916 | - return ['field' => $fields, 'key' => $keys]; |
|
| 1794 | + $query = |
|
| 1795 | + 'SELECT sql, type FROM' |
|
| 1796 | + . ' (SELECT * FROM sqlite_master UNION ALL' |
|
| 1797 | + . ' SELECT * FROM sqlite_temp_master)' |
|
| 1798 | + . " WHERE tbl_name LIKE '$nom_table'" |
|
| 1799 | + . " AND type!='meta' AND sql NOT NULL AND name NOT LIKE 'sqlite_%'" |
|
| 1800 | + . ' ORDER BY substr(type,2,1), name'; |
|
| 1801 | + |
|
| 1802 | + $a = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1803 | + if (!$a) { |
|
| 1804 | + return ''; |
|
| 1805 | + } |
|
| 1806 | + if (!$requeter) { |
|
| 1807 | + return $a; |
|
| 1808 | + } |
|
| 1809 | + if (!($a = spip_sqlite_fetch($a, null, $serveur))) { |
|
| 1810 | + return ''; |
|
| 1811 | + } |
|
| 1812 | + $vue = ($a['type'] == 'view'); // table | vue |
|
| 1813 | + |
|
| 1814 | + // c'est une table |
|
| 1815 | + // il faut parser le create |
|
| 1816 | + if (!$vue) { |
|
| 1817 | + if (!preg_match(_SQLITE_RE_SHOW_TABLE, array_shift($a), $r)) { |
|
| 1818 | + return ''; |
|
| 1819 | + } else { |
|
| 1820 | + $desc = $r[1]; |
|
| 1821 | + // extraction d'une KEY éventuelle en prenant garde de ne pas |
|
| 1822 | + // relever un champ dont le nom contient KEY (ex. ID_WHISKEY) |
|
| 1823 | + if (preg_match('/^(.*?),([^,]*\sKEY[ (].*)$/s', $desc, $r)) { |
|
| 1824 | + $namedkeys = $r[2]; |
|
| 1825 | + $desc = $r[1]; |
|
| 1826 | + } else { |
|
| 1827 | + $namedkeys = ''; |
|
| 1828 | + } |
|
| 1829 | + |
|
| 1830 | + $fields = []; |
|
| 1831 | + $keys = []; |
|
| 1832 | + |
|
| 1833 | + // enlever les contenus des valeurs DEFAULT 'xxx' qui pourraient perturber |
|
| 1834 | + // par exemple s'il contiennent une virgule. |
|
| 1835 | + // /!\ cela peut aussi echapper le nom des champs si la table a eu des operations avec SQLite Manager ! |
|
| 1836 | + list($desc, $echaps) = query_echappe_textes($desc); |
|
| 1837 | + |
|
| 1838 | + // separer toutes les descriptions de champs, separes par des virgules |
|
| 1839 | + # /!\ explode peut exploser aussi DECIMAL(10,2) ! |
|
| 1840 | + $k_precedent = null; |
|
| 1841 | + foreach (explode(',', $desc) as $v) { |
|
| 1842 | + preg_match('/^\s*([^\s]+)\s+(.*)/', $v, $r); |
|
| 1843 | + // Les cles de champs peuvent etre entourees |
|
| 1844 | + // de guillements doubles " , simples ', graves ` ou de crochets [ ], ou rien. |
|
| 1845 | + // http://www.sqlite.org/lang_keywords.html |
|
| 1846 | + $k = strtolower(query_reinjecte_textes($r[1], $echaps)); // champ, "champ", [champ]... |
|
| 1847 | + if ($char = strpbrk($k[0], '\'"[`')) { |
|
| 1848 | + $k = trim($k, $char); |
|
| 1849 | + if ($char == '[') { |
|
| 1850 | + $k = rtrim($k, ']'); |
|
| 1851 | + } |
|
| 1852 | + } |
|
| 1853 | + $def = query_reinjecte_textes($r[2], $echaps); // valeur du champ |
|
| 1854 | + |
|
| 1855 | + // rustine pour DECIMAL(10,2) |
|
| 1856 | + // s'il y a une parenthèse fermante dans la clé |
|
| 1857 | + // ou dans la définition sans qu'il n'y ait une ouverture avant |
|
| 1858 | + if (str_contains($k, ')') or preg_match('/^[^\(]*\)/', $def)) { |
|
| 1859 | + $fields[$k_precedent] .= ',' . $k . ' ' . $def; |
|
| 1860 | + continue; |
|
| 1861 | + } |
|
| 1862 | + |
|
| 1863 | + // la primary key peut etre dans une des descriptions de champs |
|
| 1864 | + // et non en fin de table, cas encore decouvert avec Sqlite Manager |
|
| 1865 | + if (stripos($r[2], 'PRIMARY KEY') !== false) { |
|
| 1866 | + $keys['PRIMARY KEY'] = $k; |
|
| 1867 | + } |
|
| 1868 | + |
|
| 1869 | + $fields[$k] = $def; |
|
| 1870 | + $k_precedent = $k; |
|
| 1871 | + } |
|
| 1872 | + // key inclues dans la requete |
|
| 1873 | + foreach (preg_split('/\)\s*(,|$)/', $namedkeys) as $v) { |
|
| 1874 | + if (preg_match('/^\s*([^(]*)\(([^(]*(\(\d+\))?)$/', $v, $r)) { |
|
| 1875 | + $k = str_replace('`', '', trim($r[1])); |
|
| 1876 | + $t = trim(strtolower(str_replace('`', '', $r[2])), '"'); |
|
| 1877 | + if ($k && !isset($keys[$k])) { |
|
| 1878 | + $keys[$k] = $t; |
|
| 1879 | + } else { |
|
| 1880 | + $keys[] = $t; |
|
| 1881 | + } |
|
| 1882 | + } |
|
| 1883 | + } |
|
| 1884 | + // sinon ajouter les key index |
|
| 1885 | + $query = |
|
| 1886 | + 'SELECT name,sql FROM' |
|
| 1887 | + . ' (SELECT * FROM sqlite_master UNION ALL' |
|
| 1888 | + . ' SELECT * FROM sqlite_temp_master)' |
|
| 1889 | + . " WHERE tbl_name LIKE '$nom_table'" |
|
| 1890 | + . " AND type='index' AND name NOT LIKE 'sqlite_%'" |
|
| 1891 | + . 'ORDER BY substr(type,2,1), name'; |
|
| 1892 | + $a = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1893 | + while ($r = spip_sqlite_fetch($a, null, $serveur)) { |
|
| 1894 | + $key = str_replace($nom_table . '_', '', $r['name']); // enlever le nom de la table ajoute a l'index |
|
| 1895 | + $keytype = 'KEY'; |
|
| 1896 | + if (strpos($r['sql'], 'UNIQUE INDEX') !== false) { |
|
| 1897 | + $keytype = 'UNIQUE KEY'; |
|
| 1898 | + } |
|
| 1899 | + $colonnes = preg_replace(',.*\((.*)\).*,', '$1', $r['sql']); |
|
| 1900 | + $keys[$keytype . ' ' . $key] = $colonnes; |
|
| 1901 | + } |
|
| 1902 | + } |
|
| 1903 | + } // c'est une vue, on liste les champs disponibles simplement |
|
| 1904 | + else { |
|
| 1905 | + if ($res = sql_fetsel('*', $nom_table, '', '', '', '1', '', $serveur)) { // limit 1 |
|
| 1906 | + $fields = []; |
|
| 1907 | + foreach ($res as $c => $v) { |
|
| 1908 | + $fields[$c] = ''; |
|
| 1909 | + } |
|
| 1910 | + $keys = []; |
|
| 1911 | + } else { |
|
| 1912 | + return ''; |
|
| 1913 | + } |
|
| 1914 | + } |
|
| 1915 | + |
|
| 1916 | + return ['field' => $fields, 'key' => $keys]; |
|
| 1917 | 1917 | } |
| 1918 | 1918 | |
| 1919 | 1919 | |
@@ -1940,24 +1940,24 @@ discard block |
||
| 1940 | 1940 | */ |
| 1941 | 1941 | function spip_sqlite_update($table, $champs, $where = '', $desc = '', $serveur = '', $requeter = true) |
| 1942 | 1942 | { |
| 1943 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1944 | - $champs = _sqlite_ajouter_champs_timestamp($table, $champs, $desc, $serveur); |
|
| 1945 | - |
|
| 1946 | - $set = []; |
|
| 1947 | - foreach ($champs as $champ => $val) { |
|
| 1948 | - $set[] = $champ . "=$val"; |
|
| 1949 | - } |
|
| 1950 | - if (!empty($set)) { |
|
| 1951 | - return spip_sqlite_query( |
|
| 1952 | - _sqlite_calculer_expression('UPDATE', $table, ',') |
|
| 1953 | - . _sqlite_calculer_expression('SET', $set, ',') |
|
| 1954 | - . _sqlite_calculer_expression('WHERE', $where), |
|
| 1955 | - $serveur, |
|
| 1956 | - $requeter |
|
| 1957 | - ); |
|
| 1958 | - } |
|
| 1959 | - |
|
| 1960 | - return false; |
|
| 1943 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1944 | + $champs = _sqlite_ajouter_champs_timestamp($table, $champs, $desc, $serveur); |
|
| 1945 | + |
|
| 1946 | + $set = []; |
|
| 1947 | + foreach ($champs as $champ => $val) { |
|
| 1948 | + $set[] = $champ . "=$val"; |
|
| 1949 | + } |
|
| 1950 | + if (!empty($set)) { |
|
| 1951 | + return spip_sqlite_query( |
|
| 1952 | + _sqlite_calculer_expression('UPDATE', $table, ',') |
|
| 1953 | + . _sqlite_calculer_expression('SET', $set, ',') |
|
| 1954 | + . _sqlite_calculer_expression('WHERE', $where), |
|
| 1955 | + $serveur, |
|
| 1956 | + $requeter |
|
| 1957 | + ); |
|
| 1958 | + } |
|
| 1959 | + |
|
| 1960 | + return false; |
|
| 1961 | 1961 | } |
| 1962 | 1962 | |
| 1963 | 1963 | |
@@ -1988,38 +1988,38 @@ discard block |
||
| 1988 | 1988 | function spip_sqlite_updateq($table, $champs, $where = '', $desc = [], $serveur = '', $requeter = true) |
| 1989 | 1989 | { |
| 1990 | 1990 | |
| 1991 | - if (!$champs) { |
|
| 1992 | - return; |
|
| 1993 | - } |
|
| 1994 | - if (!$desc) { |
|
| 1995 | - $desc = description_table($table, $serveur); |
|
| 1996 | - } |
|
| 1997 | - if (!$desc) { |
|
| 1998 | - die("$table insertion sans description"); |
|
| 1999 | - } |
|
| 2000 | - $fields = $desc['field']; |
|
| 2001 | - |
|
| 2002 | - $set = []; |
|
| 2003 | - foreach ($champs as $champ => $val) { |
|
| 2004 | - $set[$champ] = $champ . '=' . _sqlite_calculer_cite($val, isset($fields[$champ]) ? $fields[$champ] : ''); |
|
| 2005 | - } |
|
| 2006 | - |
|
| 2007 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 2008 | - // attention ils sont deja quotes |
|
| 2009 | - $maj = _sqlite_ajouter_champs_timestamp($table, [], $desc, $serveur); |
|
| 2010 | - foreach ($maj as $champ => $val) { |
|
| 2011 | - if (!isset($set[$champ])) { |
|
| 2012 | - $set[$champ] = $champ . '=' . $val; |
|
| 2013 | - } |
|
| 2014 | - } |
|
| 2015 | - |
|
| 2016 | - return spip_sqlite_query( |
|
| 2017 | - _sqlite_calculer_expression('UPDATE', $table, ',') |
|
| 2018 | - . _sqlite_calculer_expression('SET', $set, ',') |
|
| 2019 | - . _sqlite_calculer_expression('WHERE', $where), |
|
| 2020 | - $serveur, |
|
| 2021 | - $requeter |
|
| 2022 | - ); |
|
| 1991 | + if (!$champs) { |
|
| 1992 | + return; |
|
| 1993 | + } |
|
| 1994 | + if (!$desc) { |
|
| 1995 | + $desc = description_table($table, $serveur); |
|
| 1996 | + } |
|
| 1997 | + if (!$desc) { |
|
| 1998 | + die("$table insertion sans description"); |
|
| 1999 | + } |
|
| 2000 | + $fields = $desc['field']; |
|
| 2001 | + |
|
| 2002 | + $set = []; |
|
| 2003 | + foreach ($champs as $champ => $val) { |
|
| 2004 | + $set[$champ] = $champ . '=' . _sqlite_calculer_cite($val, isset($fields[$champ]) ? $fields[$champ] : ''); |
|
| 2005 | + } |
|
| 2006 | + |
|
| 2007 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 2008 | + // attention ils sont deja quotes |
|
| 2009 | + $maj = _sqlite_ajouter_champs_timestamp($table, [], $desc, $serveur); |
|
| 2010 | + foreach ($maj as $champ => $val) { |
|
| 2011 | + if (!isset($set[$champ])) { |
|
| 2012 | + $set[$champ] = $champ . '=' . $val; |
|
| 2013 | + } |
|
| 2014 | + } |
|
| 2015 | + |
|
| 2016 | + return spip_sqlite_query( |
|
| 2017 | + _sqlite_calculer_expression('UPDATE', $table, ',') |
|
| 2018 | + . _sqlite_calculer_expression('SET', $set, ',') |
|
| 2019 | + . _sqlite_calculer_expression('WHERE', $where), |
|
| 2020 | + $serveur, |
|
| 2021 | + $requeter |
|
| 2022 | + ); |
|
| 2023 | 2023 | } |
| 2024 | 2024 | |
| 2025 | 2025 | |
@@ -2038,17 +2038,17 @@ discard block |
||
| 2038 | 2038 | */ |
| 2039 | 2039 | function _sqlite_init() |
| 2040 | 2040 | { |
| 2041 | - if (!defined('_DIR_DB')) { |
|
| 2042 | - define('_DIR_DB', _DIR_ETC . 'bases/'); |
|
| 2043 | - } |
|
| 2044 | - if (!defined('_SQLITE_CHMOD')) { |
|
| 2045 | - define('_SQLITE_CHMOD', _SPIP_CHMOD); |
|
| 2046 | - } |
|
| 2047 | - |
|
| 2048 | - if (!is_dir($d = _DIR_DB)) { |
|
| 2049 | - include_spip('inc/flock'); |
|
| 2050 | - sous_repertoire($d); |
|
| 2051 | - } |
|
| 2041 | + if (!defined('_DIR_DB')) { |
|
| 2042 | + define('_DIR_DB', _DIR_ETC . 'bases/'); |
|
| 2043 | + } |
|
| 2044 | + if (!defined('_SQLITE_CHMOD')) { |
|
| 2045 | + define('_SQLITE_CHMOD', _SPIP_CHMOD); |
|
| 2046 | + } |
|
| 2047 | + |
|
| 2048 | + if (!is_dir($d = _DIR_DB)) { |
|
| 2049 | + include_spip('inc/flock'); |
|
| 2050 | + sous_repertoire($d); |
|
| 2051 | + } |
|
| 2052 | 2052 | } |
| 2053 | 2053 | |
| 2054 | 2054 | |
@@ -2063,20 +2063,20 @@ discard block |
||
| 2063 | 2063 | */ |
| 2064 | 2064 | function _sqlite_is_version($version = '', $link = '', $serveur = '', $requeter = true) |
| 2065 | 2065 | { |
| 2066 | - if ($link === '') { |
|
| 2067 | - $link = _sqlite_link($serveur); |
|
| 2068 | - } |
|
| 2069 | - if (!$link) { |
|
| 2070 | - return false; |
|
| 2071 | - } |
|
| 2066 | + if ($link === '') { |
|
| 2067 | + $link = _sqlite_link($serveur); |
|
| 2068 | + } |
|
| 2069 | + if (!$link) { |
|
| 2070 | + return false; |
|
| 2071 | + } |
|
| 2072 | 2072 | |
| 2073 | - $v = 3; |
|
| 2073 | + $v = 3; |
|
| 2074 | 2074 | |
| 2075 | - if (!$version) { |
|
| 2076 | - return $v; |
|
| 2077 | - } |
|
| 2075 | + if (!$version) { |
|
| 2076 | + return $v; |
|
| 2077 | + } |
|
| 2078 | 2078 | |
| 2079 | - return ($version == $v); |
|
| 2079 | + return ($version == $v); |
|
| 2080 | 2080 | } |
| 2081 | 2081 | |
| 2082 | 2082 | |
@@ -2088,9 +2088,9 @@ discard block |
||
| 2088 | 2088 | */ |
| 2089 | 2089 | function _sqlite_link($serveur = '') |
| 2090 | 2090 | { |
| 2091 | - $link = &$GLOBALS['connexions'][$serveur ? $serveur : 0]['link']; |
|
| 2091 | + $link = &$GLOBALS['connexions'][$serveur ? $serveur : 0]['link']; |
|
| 2092 | 2092 | |
| 2093 | - return $link; |
|
| 2093 | + return $link; |
|
| 2094 | 2094 | } |
| 2095 | 2095 | |
| 2096 | 2096 | |
@@ -2106,54 +2106,54 @@ discard block |
||
| 2106 | 2106 | */ |
| 2107 | 2107 | function _sqlite_calculer_cite($v, $type) |
| 2108 | 2108 | { |
| 2109 | - if ($type) { |
|
| 2110 | - if ( |
|
| 2111 | - is_null($v) |
|
| 2112 | - and stripos($type, 'NOT NULL') === false |
|
| 2113 | - ) { |
|
| 2114 | - // null php se traduit en NULL SQL |
|
| 2115 | - return 'NULL'; |
|
| 2116 | - } |
|
| 2117 | - |
|
| 2118 | - if (sql_test_date($type) and preg_match('/^\w+\(/', $v)) { |
|
| 2119 | - return $v; |
|
| 2120 | - } |
|
| 2121 | - if (sql_test_int($type)) { |
|
| 2122 | - if (is_numeric($v)) { |
|
| 2123 | - return $v; |
|
| 2124 | - } elseif ($v === null) { |
|
| 2125 | - return 0; |
|
| 2126 | - } elseif (ctype_xdigit(substr($v, 2)) and strncmp($v, '0x', 2) === 0) { |
|
| 2127 | - return hexdec(substr($v, 2)); |
|
| 2128 | - } else { |
|
| 2129 | - return intval($v); |
|
| 2130 | - } |
|
| 2131 | - } |
|
| 2132 | - } else { |
|
| 2133 | - // si on ne connait pas le type on le deduit de $v autant que possible |
|
| 2134 | - if (is_bool($v)) { |
|
| 2135 | - return strval(intval($v)); |
|
| 2136 | - } elseif (is_numeric($v)) { |
|
| 2137 | - return strval($v); |
|
| 2138 | - } |
|
| 2139 | - } |
|
| 2140 | - |
|
| 2141 | - // trouver un link sqlite pour faire l'echappement |
|
| 2142 | - foreach ($GLOBALS['connexions'] as $s) { |
|
| 2143 | - if ( |
|
| 2144 | - $l = $s['link'] |
|
| 2145 | - and is_object($l) |
|
| 2146 | - and $l instanceof \PDO |
|
| 2147 | - and $l->getAttribute(\PDO::ATTR_DRIVER_NAME) === 'sqlite' |
|
| 2148 | - ) { |
|
| 2149 | - return $l->quote($v ?? ''); |
|
| 2150 | - } |
|
| 2151 | - } |
|
| 2152 | - |
|
| 2153 | - // echapper les ' en '' |
|
| 2154 | - spip_log('Pas de methode ->quote pour echapper', 'sqlite.' . _LOG_INFO_IMPORTANTE); |
|
| 2155 | - |
|
| 2156 | - return ("'" . str_replace("'", "''", $v) . "'"); |
|
| 2109 | + if ($type) { |
|
| 2110 | + if ( |
|
| 2111 | + is_null($v) |
|
| 2112 | + and stripos($type, 'NOT NULL') === false |
|
| 2113 | + ) { |
|
| 2114 | + // null php se traduit en NULL SQL |
|
| 2115 | + return 'NULL'; |
|
| 2116 | + } |
|
| 2117 | + |
|
| 2118 | + if (sql_test_date($type) and preg_match('/^\w+\(/', $v)) { |
|
| 2119 | + return $v; |
|
| 2120 | + } |
|
| 2121 | + if (sql_test_int($type)) { |
|
| 2122 | + if (is_numeric($v)) { |
|
| 2123 | + return $v; |
|
| 2124 | + } elseif ($v === null) { |
|
| 2125 | + return 0; |
|
| 2126 | + } elseif (ctype_xdigit(substr($v, 2)) and strncmp($v, '0x', 2) === 0) { |
|
| 2127 | + return hexdec(substr($v, 2)); |
|
| 2128 | + } else { |
|
| 2129 | + return intval($v); |
|
| 2130 | + } |
|
| 2131 | + } |
|
| 2132 | + } else { |
|
| 2133 | + // si on ne connait pas le type on le deduit de $v autant que possible |
|
| 2134 | + if (is_bool($v)) { |
|
| 2135 | + return strval(intval($v)); |
|
| 2136 | + } elseif (is_numeric($v)) { |
|
| 2137 | + return strval($v); |
|
| 2138 | + } |
|
| 2139 | + } |
|
| 2140 | + |
|
| 2141 | + // trouver un link sqlite pour faire l'echappement |
|
| 2142 | + foreach ($GLOBALS['connexions'] as $s) { |
|
| 2143 | + if ( |
|
| 2144 | + $l = $s['link'] |
|
| 2145 | + and is_object($l) |
|
| 2146 | + and $l instanceof \PDO |
|
| 2147 | + and $l->getAttribute(\PDO::ATTR_DRIVER_NAME) === 'sqlite' |
|
| 2148 | + ) { |
|
| 2149 | + return $l->quote($v ?? ''); |
|
| 2150 | + } |
|
| 2151 | + } |
|
| 2152 | + |
|
| 2153 | + // echapper les ' en '' |
|
| 2154 | + spip_log('Pas de methode ->quote pour echapper', 'sqlite.' . _LOG_INFO_IMPORTANTE); |
|
| 2155 | + |
|
| 2156 | + return ("'" . str_replace("'", "''", $v) . "'"); |
|
| 2157 | 2157 | } |
| 2158 | 2158 | |
| 2159 | 2159 | |
@@ -2170,21 +2170,21 @@ discard block |
||
| 2170 | 2170 | */ |
| 2171 | 2171 | function _sqlite_calculer_expression($expression, $v, $join = 'AND') |
| 2172 | 2172 | { |
| 2173 | - if (empty($v)) { |
|
| 2174 | - return ''; |
|
| 2175 | - } |
|
| 2176 | - |
|
| 2177 | - $exp = "\n$expression "; |
|
| 2178 | - |
|
| 2179 | - if (!is_array($v)) { |
|
| 2180 | - return $exp . $v; |
|
| 2181 | - } else { |
|
| 2182 | - if (strtoupper($join) === 'AND') { |
|
| 2183 | - return $exp . join("\n\t$join ", array_map('_sqlite_calculer_where', $v)); |
|
| 2184 | - } else { |
|
| 2185 | - return $exp . join($join, $v); |
|
| 2186 | - } |
|
| 2187 | - } |
|
| 2173 | + if (empty($v)) { |
|
| 2174 | + return ''; |
|
| 2175 | + } |
|
| 2176 | + |
|
| 2177 | + $exp = "\n$expression "; |
|
| 2178 | + |
|
| 2179 | + if (!is_array($v)) { |
|
| 2180 | + return $exp . $v; |
|
| 2181 | + } else { |
|
| 2182 | + if (strtoupper($join) === 'AND') { |
|
| 2183 | + return $exp . join("\n\t$join ", array_map('_sqlite_calculer_where', $v)); |
|
| 2184 | + } else { |
|
| 2185 | + return $exp . join($join, $v); |
|
| 2186 | + } |
|
| 2187 | + } |
|
| 2188 | 2188 | } |
| 2189 | 2189 | |
| 2190 | 2190 | |
@@ -2201,7 +2201,7 @@ discard block |
||
| 2201 | 2201 | */ |
| 2202 | 2202 | function _sqlite_calculer_order($orderby) |
| 2203 | 2203 | { |
| 2204 | - return (is_array($orderby)) ? join(', ', $orderby) : $orderby; |
|
| 2204 | + return (is_array($orderby)) ? join(', ', $orderby) : $orderby; |
|
| 2205 | 2205 | } |
| 2206 | 2206 | |
| 2207 | 2207 | |
@@ -2213,26 +2213,26 @@ discard block |
||
| 2213 | 2213 | */ |
| 2214 | 2214 | function _sqlite_calculer_select_as($args) |
| 2215 | 2215 | { |
| 2216 | - $res = ''; |
|
| 2217 | - foreach ($args as $k => $v) { |
|
| 2218 | - if (substr($k, -1) == '@') { |
|
| 2219 | - // c'est une jointure qui se refere au from precedent |
|
| 2220 | - // pas de virgule |
|
| 2221 | - $res .= ' ' . $v; |
|
| 2222 | - } else { |
|
| 2223 | - if (!is_numeric($k)) { |
|
| 2224 | - $p = strpos($v, ' '); |
|
| 2225 | - if ($p) { |
|
| 2226 | - $v = substr($v, 0, $p) . " AS '$k'" . substr($v, $p); |
|
| 2227 | - } else { |
|
| 2228 | - $v .= " AS '$k'"; |
|
| 2229 | - } |
|
| 2230 | - } |
|
| 2231 | - $res .= ', ' . $v; |
|
| 2232 | - } |
|
| 2233 | - } |
|
| 2234 | - |
|
| 2235 | - return substr($res, 2); |
|
| 2216 | + $res = ''; |
|
| 2217 | + foreach ($args as $k => $v) { |
|
| 2218 | + if (substr($k, -1) == '@') { |
|
| 2219 | + // c'est une jointure qui se refere au from precedent |
|
| 2220 | + // pas de virgule |
|
| 2221 | + $res .= ' ' . $v; |
|
| 2222 | + } else { |
|
| 2223 | + if (!is_numeric($k)) { |
|
| 2224 | + $p = strpos($v, ' '); |
|
| 2225 | + if ($p) { |
|
| 2226 | + $v = substr($v, 0, $p) . " AS '$k'" . substr($v, $p); |
|
| 2227 | + } else { |
|
| 2228 | + $v .= " AS '$k'"; |
|
| 2229 | + } |
|
| 2230 | + } |
|
| 2231 | + $res .= ', ' . $v; |
|
| 2232 | + } |
|
| 2233 | + } |
|
| 2234 | + |
|
| 2235 | + return substr($res, 2); |
|
| 2236 | 2236 | } |
| 2237 | 2237 | |
| 2238 | 2238 | |
@@ -2256,26 +2256,26 @@ discard block |
||
| 2256 | 2256 | */ |
| 2257 | 2257 | function _sqlite_calculer_where($v) |
| 2258 | 2258 | { |
| 2259 | - if (!is_array($v)) { |
|
| 2260 | - return $v; |
|
| 2261 | - } |
|
| 2262 | - |
|
| 2263 | - $op = array_shift($v); |
|
| 2264 | - if (!($n = count($v))) { |
|
| 2265 | - return $op; |
|
| 2266 | - } else { |
|
| 2267 | - $arg = _sqlite_calculer_where(array_shift($v)); |
|
| 2268 | - if ($n == 1) { |
|
| 2269 | - return "$op($arg)"; |
|
| 2270 | - } else { |
|
| 2271 | - $arg2 = _sqlite_calculer_where(array_shift($v)); |
|
| 2272 | - if ($n == 2) { |
|
| 2273 | - return "($arg $op $arg2)"; |
|
| 2274 | - } else { |
|
| 2275 | - return "($arg $op ($arg2) : $v[0])"; |
|
| 2276 | - } |
|
| 2277 | - } |
|
| 2278 | - } |
|
| 2259 | + if (!is_array($v)) { |
|
| 2260 | + return $v; |
|
| 2261 | + } |
|
| 2262 | + |
|
| 2263 | + $op = array_shift($v); |
|
| 2264 | + if (!($n = count($v))) { |
|
| 2265 | + return $op; |
|
| 2266 | + } else { |
|
| 2267 | + $arg = _sqlite_calculer_where(array_shift($v)); |
|
| 2268 | + if ($n == 1) { |
|
| 2269 | + return "$op($arg)"; |
|
| 2270 | + } else { |
|
| 2271 | + $arg2 = _sqlite_calculer_where(array_shift($v)); |
|
| 2272 | + if ($n == 2) { |
|
| 2273 | + return "($arg $op $arg2)"; |
|
| 2274 | + } else { |
|
| 2275 | + return "($arg $op ($arg2) : $v[0])"; |
|
| 2276 | + } |
|
| 2277 | + } |
|
| 2278 | + } |
|
| 2279 | 2279 | } |
| 2280 | 2280 | |
| 2281 | 2281 | |
@@ -2291,19 +2291,19 @@ discard block |
||
| 2291 | 2291 | */ |
| 2292 | 2292 | function _sqlite_charger_version($version = '') |
| 2293 | 2293 | { |
| 2294 | - $versions = []; |
|
| 2295 | - |
|
| 2296 | - // version 3 |
|
| 2297 | - if (!$version || $version == 3) { |
|
| 2298 | - if (extension_loaded('pdo') && extension_loaded('pdo_sqlite')) { |
|
| 2299 | - $versions[] = 3; |
|
| 2300 | - } |
|
| 2301 | - } |
|
| 2302 | - if ($version) { |
|
| 2303 | - return in_array($version, $versions); |
|
| 2304 | - } |
|
| 2305 | - |
|
| 2306 | - return $versions; |
|
| 2294 | + $versions = []; |
|
| 2295 | + |
|
| 2296 | + // version 3 |
|
| 2297 | + if (!$version || $version == 3) { |
|
| 2298 | + if (extension_loaded('pdo') && extension_loaded('pdo_sqlite')) { |
|
| 2299 | + $versions[] = 3; |
|
| 2300 | + } |
|
| 2301 | + } |
|
| 2302 | + if ($version) { |
|
| 2303 | + return in_array($version, $versions); |
|
| 2304 | + } |
|
| 2305 | + |
|
| 2306 | + return $versions; |
|
| 2307 | 2307 | } |
| 2308 | 2308 | |
| 2309 | 2309 | |
@@ -2342,147 +2342,147 @@ discard block |
||
| 2342 | 2342 | function _sqlite_modifier_table($table, $colonne, $opt = [], $serveur = '') |
| 2343 | 2343 | { |
| 2344 | 2344 | |
| 2345 | - if (is_array($table)) { |
|
| 2346 | - $table_destination = reset($table); |
|
| 2347 | - $table_origine = key($table); |
|
| 2348 | - } else { |
|
| 2349 | - $table_origine = $table_destination = $table; |
|
| 2350 | - } |
|
| 2351 | - // ne prend actuellement qu'un changement |
|
| 2352 | - // mais pourra etre adapte pour changer plus qu'une colonne a la fois |
|
| 2353 | - if (is_array($colonne)) { |
|
| 2354 | - $colonne_destination = reset($colonne); |
|
| 2355 | - $colonne_origine = key($colonne); |
|
| 2356 | - } else { |
|
| 2357 | - $colonne_origine = $colonne_destination = $colonne; |
|
| 2358 | - } |
|
| 2359 | - if (!isset($opt['field'])) { |
|
| 2360 | - $opt['field'] = []; |
|
| 2361 | - } |
|
| 2362 | - if (!isset($opt['key'])) { |
|
| 2363 | - $opt['key'] = []; |
|
| 2364 | - } |
|
| 2365 | - |
|
| 2366 | - // si les noms de tables sont differents, pas besoin de table temporaire |
|
| 2367 | - // on prendra directement le nom de la future table |
|
| 2368 | - $meme_table = ($table_origine == $table_destination); |
|
| 2369 | - |
|
| 2370 | - $def_origine = sql_showtable($table_origine, false, $serveur); |
|
| 2371 | - if (!$def_origine or !isset($def_origine['field'])) { |
|
| 2372 | - spip_log("Alter table impossible sur $table_origine : table non trouvee", 'sqlite' . _LOG_ERREUR); |
|
| 2373 | - |
|
| 2374 | - return false; |
|
| 2375 | - } |
|
| 2376 | - |
|
| 2377 | - |
|
| 2378 | - $table_tmp = $table_origine . '_tmp'; |
|
| 2379 | - |
|
| 2380 | - // 1) creer une table temporaire avec les modifications |
|
| 2381 | - // - DROP : suppression de la colonne |
|
| 2382 | - // - CHANGE : modification de la colonne |
|
| 2383 | - // (foreach pour conserver l'ordre des champs) |
|
| 2384 | - |
|
| 2385 | - // field |
|
| 2386 | - $fields = []; |
|
| 2387 | - // pour le INSERT INTO plus loin |
|
| 2388 | - // stocker la correspondance nouvelles->anciennes colonnes |
|
| 2389 | - $fields_correspondances = []; |
|
| 2390 | - foreach ($def_origine['field'] as $c => $d) { |
|
| 2391 | - if ($colonne_origine && ($c == $colonne_origine)) { |
|
| 2392 | - // si pas DROP |
|
| 2393 | - if ($colonne_destination) { |
|
| 2394 | - $fields[$colonne_destination] = $opt['field'][$colonne_destination]; |
|
| 2395 | - $fields_correspondances[$colonne_destination] = $c; |
|
| 2396 | - } |
|
| 2397 | - } else { |
|
| 2398 | - $fields[$c] = $d; |
|
| 2399 | - $fields_correspondances[$c] = $c; |
|
| 2400 | - } |
|
| 2401 | - } |
|
| 2402 | - // cas de ADD sqlite2 (ajout du champ en fin de table): |
|
| 2403 | - if (!$colonne_origine && $colonne_destination) { |
|
| 2404 | - $fields[$colonne_destination] = $opt['field'][$colonne_destination]; |
|
| 2405 | - } |
|
| 2406 | - |
|
| 2407 | - // key... |
|
| 2408 | - $keys = []; |
|
| 2409 | - foreach ($def_origine['key'] as $c => $d) { |
|
| 2410 | - $c = str_replace($colonne_origine, $colonne_destination, $c); |
|
| 2411 | - $d = str_replace($colonne_origine, $colonne_destination, $d); |
|
| 2412 | - // seulement si on ne supprime pas la colonne ! |
|
| 2413 | - if ($d) { |
|
| 2414 | - $keys[$c] = $d; |
|
| 2415 | - } |
|
| 2416 | - } |
|
| 2417 | - |
|
| 2418 | - // autres keys, on merge |
|
| 2419 | - $keys = array_merge($keys, $opt['key']); |
|
| 2420 | - $queries = []; |
|
| 2421 | - |
|
| 2422 | - // copier dans destination (si differente de origine), sinon tmp |
|
| 2423 | - $table_copie = ($meme_table) ? $table_tmp : $table_destination; |
|
| 2424 | - $autoinc = (isset($keys['PRIMARY KEY']) |
|
| 2425 | - and $keys['PRIMARY KEY'] |
|
| 2426 | - and stripos($keys['PRIMARY KEY'], ',') === false |
|
| 2427 | - and stripos($fields[$keys['PRIMARY KEY']], 'default') === false); |
|
| 2428 | - |
|
| 2429 | - if ( |
|
| 2430 | - $q = _sqlite_requete_create( |
|
| 2431 | - $table_copie, |
|
| 2432 | - $fields, |
|
| 2433 | - $keys, |
|
| 2434 | - $autoinc, |
|
| 2435 | - $temporary = false, |
|
| 2436 | - $ifnotexists = true, |
|
| 2437 | - $serveur |
|
| 2438 | - ) |
|
| 2439 | - ) { |
|
| 2440 | - $queries[] = $q; |
|
| 2441 | - } |
|
| 2442 | - |
|
| 2443 | - |
|
| 2444 | - // 2) y copier les champs qui vont bien |
|
| 2445 | - $champs_dest = join(', ', array_keys($fields_correspondances)); |
|
| 2446 | - $champs_ori = join(', ', $fields_correspondances); |
|
| 2447 | - $queries[] = "INSERT INTO $table_copie ($champs_dest) SELECT $champs_ori FROM $table_origine"; |
|
| 2448 | - |
|
| 2449 | - // 3) supprimer la table d'origine |
|
| 2450 | - $queries[] = "DROP TABLE $table_origine"; |
|
| 2451 | - |
|
| 2452 | - // 4) renommer la table temporaire |
|
| 2453 | - // avec le nom de la table destination |
|
| 2454 | - // si necessaire |
|
| 2455 | - if ($meme_table) { |
|
| 2456 | - $queries[] = "ALTER TABLE $table_copie RENAME TO $table_destination"; |
|
| 2457 | - } |
|
| 2458 | - |
|
| 2459 | - // 5) remettre les index ! |
|
| 2460 | - foreach ($keys as $k => $v) { |
|
| 2461 | - if ($k == 'PRIMARY KEY') { |
|
| 2462 | - } else { |
|
| 2463 | - // enlever KEY |
|
| 2464 | - $k = substr($k, 4); |
|
| 2465 | - $queries[] = "CREATE INDEX $table_destination" . "_$k ON $table_destination ($v)"; |
|
| 2466 | - } |
|
| 2467 | - } |
|
| 2468 | - |
|
| 2469 | - |
|
| 2470 | - if (count($queries)) { |
|
| 2471 | - Sqlite::demarrer_transaction($serveur); |
|
| 2472 | - // il faut les faire une par une car $query = join('; ', $queries).";"; ne fonctionne pas |
|
| 2473 | - foreach ($queries as $q) { |
|
| 2474 | - if (!Sqlite::executer_requete($q, $serveur)) { |
|
| 2475 | - spip_log('SQLite : ALTER TABLE table :' |
|
| 2476 | - . " Erreur a l'execution de la requete : $q", 'sqlite.' . _LOG_ERREUR); |
|
| 2477 | - Sqlite::annuler_transaction($serveur); |
|
| 2478 | - |
|
| 2479 | - return false; |
|
| 2480 | - } |
|
| 2481 | - } |
|
| 2482 | - Sqlite::finir_transaction($serveur); |
|
| 2483 | - } |
|
| 2484 | - |
|
| 2485 | - return true; |
|
| 2345 | + if (is_array($table)) { |
|
| 2346 | + $table_destination = reset($table); |
|
| 2347 | + $table_origine = key($table); |
|
| 2348 | + } else { |
|
| 2349 | + $table_origine = $table_destination = $table; |
|
| 2350 | + } |
|
| 2351 | + // ne prend actuellement qu'un changement |
|
| 2352 | + // mais pourra etre adapte pour changer plus qu'une colonne a la fois |
|
| 2353 | + if (is_array($colonne)) { |
|
| 2354 | + $colonne_destination = reset($colonne); |
|
| 2355 | + $colonne_origine = key($colonne); |
|
| 2356 | + } else { |
|
| 2357 | + $colonne_origine = $colonne_destination = $colonne; |
|
| 2358 | + } |
|
| 2359 | + if (!isset($opt['field'])) { |
|
| 2360 | + $opt['field'] = []; |
|
| 2361 | + } |
|
| 2362 | + if (!isset($opt['key'])) { |
|
| 2363 | + $opt['key'] = []; |
|
| 2364 | + } |
|
| 2365 | + |
|
| 2366 | + // si les noms de tables sont differents, pas besoin de table temporaire |
|
| 2367 | + // on prendra directement le nom de la future table |
|
| 2368 | + $meme_table = ($table_origine == $table_destination); |
|
| 2369 | + |
|
| 2370 | + $def_origine = sql_showtable($table_origine, false, $serveur); |
|
| 2371 | + if (!$def_origine or !isset($def_origine['field'])) { |
|
| 2372 | + spip_log("Alter table impossible sur $table_origine : table non trouvee", 'sqlite' . _LOG_ERREUR); |
|
| 2373 | + |
|
| 2374 | + return false; |
|
| 2375 | + } |
|
| 2376 | + |
|
| 2377 | + |
|
| 2378 | + $table_tmp = $table_origine . '_tmp'; |
|
| 2379 | + |
|
| 2380 | + // 1) creer une table temporaire avec les modifications |
|
| 2381 | + // - DROP : suppression de la colonne |
|
| 2382 | + // - CHANGE : modification de la colonne |
|
| 2383 | + // (foreach pour conserver l'ordre des champs) |
|
| 2384 | + |
|
| 2385 | + // field |
|
| 2386 | + $fields = []; |
|
| 2387 | + // pour le INSERT INTO plus loin |
|
| 2388 | + // stocker la correspondance nouvelles->anciennes colonnes |
|
| 2389 | + $fields_correspondances = []; |
|
| 2390 | + foreach ($def_origine['field'] as $c => $d) { |
|
| 2391 | + if ($colonne_origine && ($c == $colonne_origine)) { |
|
| 2392 | + // si pas DROP |
|
| 2393 | + if ($colonne_destination) { |
|
| 2394 | + $fields[$colonne_destination] = $opt['field'][$colonne_destination]; |
|
| 2395 | + $fields_correspondances[$colonne_destination] = $c; |
|
| 2396 | + } |
|
| 2397 | + } else { |
|
| 2398 | + $fields[$c] = $d; |
|
| 2399 | + $fields_correspondances[$c] = $c; |
|
| 2400 | + } |
|
| 2401 | + } |
|
| 2402 | + // cas de ADD sqlite2 (ajout du champ en fin de table): |
|
| 2403 | + if (!$colonne_origine && $colonne_destination) { |
|
| 2404 | + $fields[$colonne_destination] = $opt['field'][$colonne_destination]; |
|
| 2405 | + } |
|
| 2406 | + |
|
| 2407 | + // key... |
|
| 2408 | + $keys = []; |
|
| 2409 | + foreach ($def_origine['key'] as $c => $d) { |
|
| 2410 | + $c = str_replace($colonne_origine, $colonne_destination, $c); |
|
| 2411 | + $d = str_replace($colonne_origine, $colonne_destination, $d); |
|
| 2412 | + // seulement si on ne supprime pas la colonne ! |
|
| 2413 | + if ($d) { |
|
| 2414 | + $keys[$c] = $d; |
|
| 2415 | + } |
|
| 2416 | + } |
|
| 2417 | + |
|
| 2418 | + // autres keys, on merge |
|
| 2419 | + $keys = array_merge($keys, $opt['key']); |
|
| 2420 | + $queries = []; |
|
| 2421 | + |
|
| 2422 | + // copier dans destination (si differente de origine), sinon tmp |
|
| 2423 | + $table_copie = ($meme_table) ? $table_tmp : $table_destination; |
|
| 2424 | + $autoinc = (isset($keys['PRIMARY KEY']) |
|
| 2425 | + and $keys['PRIMARY KEY'] |
|
| 2426 | + and stripos($keys['PRIMARY KEY'], ',') === false |
|
| 2427 | + and stripos($fields[$keys['PRIMARY KEY']], 'default') === false); |
|
| 2428 | + |
|
| 2429 | + if ( |
|
| 2430 | + $q = _sqlite_requete_create( |
|
| 2431 | + $table_copie, |
|
| 2432 | + $fields, |
|
| 2433 | + $keys, |
|
| 2434 | + $autoinc, |
|
| 2435 | + $temporary = false, |
|
| 2436 | + $ifnotexists = true, |
|
| 2437 | + $serveur |
|
| 2438 | + ) |
|
| 2439 | + ) { |
|
| 2440 | + $queries[] = $q; |
|
| 2441 | + } |
|
| 2442 | + |
|
| 2443 | + |
|
| 2444 | + // 2) y copier les champs qui vont bien |
|
| 2445 | + $champs_dest = join(', ', array_keys($fields_correspondances)); |
|
| 2446 | + $champs_ori = join(', ', $fields_correspondances); |
|
| 2447 | + $queries[] = "INSERT INTO $table_copie ($champs_dest) SELECT $champs_ori FROM $table_origine"; |
|
| 2448 | + |
|
| 2449 | + // 3) supprimer la table d'origine |
|
| 2450 | + $queries[] = "DROP TABLE $table_origine"; |
|
| 2451 | + |
|
| 2452 | + // 4) renommer la table temporaire |
|
| 2453 | + // avec le nom de la table destination |
|
| 2454 | + // si necessaire |
|
| 2455 | + if ($meme_table) { |
|
| 2456 | + $queries[] = "ALTER TABLE $table_copie RENAME TO $table_destination"; |
|
| 2457 | + } |
|
| 2458 | + |
|
| 2459 | + // 5) remettre les index ! |
|
| 2460 | + foreach ($keys as $k => $v) { |
|
| 2461 | + if ($k == 'PRIMARY KEY') { |
|
| 2462 | + } else { |
|
| 2463 | + // enlever KEY |
|
| 2464 | + $k = substr($k, 4); |
|
| 2465 | + $queries[] = "CREATE INDEX $table_destination" . "_$k ON $table_destination ($v)"; |
|
| 2466 | + } |
|
| 2467 | + } |
|
| 2468 | + |
|
| 2469 | + |
|
| 2470 | + if (count($queries)) { |
|
| 2471 | + Sqlite::demarrer_transaction($serveur); |
|
| 2472 | + // il faut les faire une par une car $query = join('; ', $queries).";"; ne fonctionne pas |
|
| 2473 | + foreach ($queries as $q) { |
|
| 2474 | + if (!Sqlite::executer_requete($q, $serveur)) { |
|
| 2475 | + spip_log('SQLite : ALTER TABLE table :' |
|
| 2476 | + . " Erreur a l'execution de la requete : $q", 'sqlite.' . _LOG_ERREUR); |
|
| 2477 | + Sqlite::annuler_transaction($serveur); |
|
| 2478 | + |
|
| 2479 | + return false; |
|
| 2480 | + } |
|
| 2481 | + } |
|
| 2482 | + Sqlite::finir_transaction($serveur); |
|
| 2483 | + } |
|
| 2484 | + |
|
| 2485 | + return true; |
|
| 2486 | 2486 | } |
| 2487 | 2487 | |
| 2488 | 2488 | |
@@ -2493,61 +2493,61 @@ discard block |
||
| 2493 | 2493 | */ |
| 2494 | 2494 | function _sqlite_ref_fonctions() |
| 2495 | 2495 | { |
| 2496 | - $fonctions = [ |
|
| 2497 | - 'alter' => 'spip_sqlite_alter', |
|
| 2498 | - 'count' => 'spip_sqlite_count', |
|
| 2499 | - 'countsel' => 'spip_sqlite_countsel', |
|
| 2500 | - 'create' => 'spip_sqlite_create', |
|
| 2501 | - 'create_base' => 'spip_sqlite_create_base', |
|
| 2502 | - 'create_view' => 'spip_sqlite_create_view', |
|
| 2503 | - 'date_proche' => 'spip_sqlite_date_proche', |
|
| 2504 | - 'delete' => 'spip_sqlite_delete', |
|
| 2505 | - 'drop_table' => 'spip_sqlite_drop_table', |
|
| 2506 | - 'drop_view' => 'spip_sqlite_drop_view', |
|
| 2507 | - 'errno' => 'spip_sqlite_errno', |
|
| 2508 | - 'error' => 'spip_sqlite_error', |
|
| 2509 | - 'explain' => 'spip_sqlite_explain', |
|
| 2510 | - 'fetch' => 'spip_sqlite_fetch', |
|
| 2511 | - 'seek' => 'spip_sqlite_seek', |
|
| 2512 | - 'free' => 'spip_sqlite_free', |
|
| 2513 | - 'hex' => 'spip_sqlite_hex', |
|
| 2514 | - 'in' => 'spip_sqlite_in', |
|
| 2515 | - 'insert' => 'spip_sqlite_insert', |
|
| 2516 | - 'insertq' => 'spip_sqlite_insertq', |
|
| 2517 | - 'insertq_multi' => 'spip_sqlite_insertq_multi', |
|
| 2518 | - 'listdbs' => 'spip_sqlite_listdbs', |
|
| 2519 | - 'multi' => 'spip_sqlite_multi', |
|
| 2520 | - 'optimize' => 'spip_sqlite_optimize', |
|
| 2521 | - 'query' => 'spip_sqlite_query', |
|
| 2522 | - 'quote' => 'spip_sqlite_quote', |
|
| 2523 | - 'repair' => 'spip_sqlite_repair', |
|
| 2524 | - 'replace' => 'spip_sqlite_replace', |
|
| 2525 | - 'replace_multi' => 'spip_sqlite_replace_multi', |
|
| 2526 | - 'select' => 'spip_sqlite_select', |
|
| 2527 | - 'selectdb' => 'spip_sqlite_selectdb', |
|
| 2528 | - 'set_charset' => 'spip_sqlite_set_charset', |
|
| 2529 | - 'get_charset' => 'spip_sqlite_get_charset', |
|
| 2530 | - 'showbase' => 'spip_sqlite_showbase', |
|
| 2531 | - 'showtable' => 'spip_sqlite_showtable', |
|
| 2532 | - 'table_exists' => 'spip_sqlite_table_exists', |
|
| 2533 | - 'update' => 'spip_sqlite_update', |
|
| 2534 | - 'updateq' => 'spip_sqlite_updateq', |
|
| 2535 | - 'preferer_transaction' => 'spip_sqlite_preferer_transaction', |
|
| 2536 | - 'demarrer_transaction' => 'spip_sqlite_demarrer_transaction', |
|
| 2537 | - 'terminer_transaction' => 'spip_sqlite_terminer_transaction', |
|
| 2538 | - ]; |
|
| 2539 | - |
|
| 2540 | - // association de chaque nom http d'un charset aux couples sqlite |
|
| 2541 | - // SQLite supporte utf-8 et utf-16 uniquement. |
|
| 2542 | - $charsets = [ |
|
| 2543 | - 'utf-8' => ['charset' => 'utf8', 'collation' => 'utf8_general_ci'], |
|
| 2544 | - //'utf-16be'=>array('charset'=>'utf16be','collation'=>'UTF-16BE'),// aucune idee de quoi il faut remplir dans es champs la |
|
| 2545 | - //'utf-16le'=>array('charset'=>'utf16le','collation'=>'UTF-16LE') |
|
| 2546 | - ]; |
|
| 2547 | - |
|
| 2548 | - $fonctions['charsets'] = $charsets; |
|
| 2549 | - |
|
| 2550 | - return $fonctions; |
|
| 2496 | + $fonctions = [ |
|
| 2497 | + 'alter' => 'spip_sqlite_alter', |
|
| 2498 | + 'count' => 'spip_sqlite_count', |
|
| 2499 | + 'countsel' => 'spip_sqlite_countsel', |
|
| 2500 | + 'create' => 'spip_sqlite_create', |
|
| 2501 | + 'create_base' => 'spip_sqlite_create_base', |
|
| 2502 | + 'create_view' => 'spip_sqlite_create_view', |
|
| 2503 | + 'date_proche' => 'spip_sqlite_date_proche', |
|
| 2504 | + 'delete' => 'spip_sqlite_delete', |
|
| 2505 | + 'drop_table' => 'spip_sqlite_drop_table', |
|
| 2506 | + 'drop_view' => 'spip_sqlite_drop_view', |
|
| 2507 | + 'errno' => 'spip_sqlite_errno', |
|
| 2508 | + 'error' => 'spip_sqlite_error', |
|
| 2509 | + 'explain' => 'spip_sqlite_explain', |
|
| 2510 | + 'fetch' => 'spip_sqlite_fetch', |
|
| 2511 | + 'seek' => 'spip_sqlite_seek', |
|
| 2512 | + 'free' => 'spip_sqlite_free', |
|
| 2513 | + 'hex' => 'spip_sqlite_hex', |
|
| 2514 | + 'in' => 'spip_sqlite_in', |
|
| 2515 | + 'insert' => 'spip_sqlite_insert', |
|
| 2516 | + 'insertq' => 'spip_sqlite_insertq', |
|
| 2517 | + 'insertq_multi' => 'spip_sqlite_insertq_multi', |
|
| 2518 | + 'listdbs' => 'spip_sqlite_listdbs', |
|
| 2519 | + 'multi' => 'spip_sqlite_multi', |
|
| 2520 | + 'optimize' => 'spip_sqlite_optimize', |
|
| 2521 | + 'query' => 'spip_sqlite_query', |
|
| 2522 | + 'quote' => 'spip_sqlite_quote', |
|
| 2523 | + 'repair' => 'spip_sqlite_repair', |
|
| 2524 | + 'replace' => 'spip_sqlite_replace', |
|
| 2525 | + 'replace_multi' => 'spip_sqlite_replace_multi', |
|
| 2526 | + 'select' => 'spip_sqlite_select', |
|
| 2527 | + 'selectdb' => 'spip_sqlite_selectdb', |
|
| 2528 | + 'set_charset' => 'spip_sqlite_set_charset', |
|
| 2529 | + 'get_charset' => 'spip_sqlite_get_charset', |
|
| 2530 | + 'showbase' => 'spip_sqlite_showbase', |
|
| 2531 | + 'showtable' => 'spip_sqlite_showtable', |
|
| 2532 | + 'table_exists' => 'spip_sqlite_table_exists', |
|
| 2533 | + 'update' => 'spip_sqlite_update', |
|
| 2534 | + 'updateq' => 'spip_sqlite_updateq', |
|
| 2535 | + 'preferer_transaction' => 'spip_sqlite_preferer_transaction', |
|
| 2536 | + 'demarrer_transaction' => 'spip_sqlite_demarrer_transaction', |
|
| 2537 | + 'terminer_transaction' => 'spip_sqlite_terminer_transaction', |
|
| 2538 | + ]; |
|
| 2539 | + |
|
| 2540 | + // association de chaque nom http d'un charset aux couples sqlite |
|
| 2541 | + // SQLite supporte utf-8 et utf-16 uniquement. |
|
| 2542 | + $charsets = [ |
|
| 2543 | + 'utf-8' => ['charset' => 'utf8', 'collation' => 'utf8_general_ci'], |
|
| 2544 | + //'utf-16be'=>array('charset'=>'utf16be','collation'=>'UTF-16BE'),// aucune idee de quoi il faut remplir dans es champs la |
|
| 2545 | + //'utf-16le'=>array('charset'=>'utf16le','collation'=>'UTF-16LE') |
|
| 2546 | + ]; |
|
| 2547 | + |
|
| 2548 | + $fonctions['charsets'] = $charsets; |
|
| 2549 | + |
|
| 2550 | + return $fonctions; |
|
| 2551 | 2551 | } |
| 2552 | 2552 | |
| 2553 | 2553 | |
@@ -2560,56 +2560,56 @@ discard block |
||
| 2560 | 2560 | */ |
| 2561 | 2561 | function _sqlite_remplacements_definitions_table($query, $autoinc = false) |
| 2562 | 2562 | { |
| 2563 | - // quelques remplacements |
|
| 2564 | - $num = '(\s*\([0-9]*\))?'; |
|
| 2565 | - $enum = '(\s*\([^\)]*\))?'; |
|
| 2566 | - |
|
| 2567 | - $remplace = [ |
|
| 2568 | - '/enum' . $enum . '/is' => 'VARCHAR(255)', |
|
| 2569 | - '/COLLATE \w+_bin/is' => 'COLLATE BINARY', |
|
| 2570 | - '/COLLATE \w+_ci/is' => 'COLLATE NOCASE', |
|
| 2571 | - '/auto_increment/is' => '', |
|
| 2572 | - '/current_timestamp\(\)/is' => 'CURRENT_TIMESTAMP', // Fix export depuis mariaDB #4374 |
|
| 2573 | - '/(timestamp .* )ON .*$/is' => '\\1', |
|
| 2574 | - '/character set \w+/is' => '', |
|
| 2575 | - '/((big|small|medium|tiny)?int(eger)?)' . $num . '\s*unsigned/is' => '\\1 UNSIGNED', |
|
| 2576 | - '/(text\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
|
| 2577 | - '/((char|varchar)' . $num . '\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
|
| 2578 | - '/(datetime\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00 00:00:00'", |
|
| 2579 | - '/(date\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00'", |
|
| 2580 | - ]; |
|
| 2581 | - |
|
| 2582 | - // pour l'autoincrement, il faut des INTEGER NOT NULL PRIMARY KEY |
|
| 2583 | - $remplace_autocinc = [ |
|
| 2584 | - '/(big|small|medium|tiny)?int(eger)?' . $num . '/is' => 'INTEGER' |
|
| 2585 | - ]; |
|
| 2586 | - // pour les int non autoincrement, il faut un DEFAULT |
|
| 2587 | - $remplace_nonautocinc = [ |
|
| 2588 | - '/((big|small|medium|tiny)?int(eger)?' . $num . '\s+not\s+null)\s*$/is' => "\\1 DEFAULT 0", |
|
| 2589 | - ]; |
|
| 2590 | - |
|
| 2591 | - if (is_string($query)) { |
|
| 2592 | - $query = preg_replace(array_keys($remplace), $remplace, $query); |
|
| 2593 | - if ($autoinc or preg_match(',AUTO_INCREMENT,is', $query)) { |
|
| 2594 | - $query = preg_replace(array_keys($remplace_autocinc), $remplace_autocinc, $query); |
|
| 2595 | - } else { |
|
| 2596 | - $query = preg_replace(array_keys($remplace_nonautocinc), $remplace_nonautocinc, $query); |
|
| 2597 | - $query = _sqlite_collate_ci($query); |
|
| 2598 | - } |
|
| 2599 | - } elseif (is_array($query)) { |
|
| 2600 | - foreach ($query as $k => $q) { |
|
| 2601 | - $ai = ($autoinc ? $k == $autoinc : preg_match(',AUTO_INCREMENT,is', $q)); |
|
| 2602 | - $query[$k] = preg_replace(array_keys($remplace), $remplace, $query[$k]); |
|
| 2603 | - if ($ai) { |
|
| 2604 | - $query[$k] = preg_replace(array_keys($remplace_autocinc), $remplace_autocinc, $query[$k]); |
|
| 2605 | - } else { |
|
| 2606 | - $query[$k] = preg_replace(array_keys($remplace_nonautocinc), $remplace_nonautocinc, $query[$k]); |
|
| 2607 | - $query[$k] = _sqlite_collate_ci($query[$k]); |
|
| 2608 | - } |
|
| 2609 | - } |
|
| 2610 | - } |
|
| 2611 | - |
|
| 2612 | - return $query; |
|
| 2563 | + // quelques remplacements |
|
| 2564 | + $num = '(\s*\([0-9]*\))?'; |
|
| 2565 | + $enum = '(\s*\([^\)]*\))?'; |
|
| 2566 | + |
|
| 2567 | + $remplace = [ |
|
| 2568 | + '/enum' . $enum . '/is' => 'VARCHAR(255)', |
|
| 2569 | + '/COLLATE \w+_bin/is' => 'COLLATE BINARY', |
|
| 2570 | + '/COLLATE \w+_ci/is' => 'COLLATE NOCASE', |
|
| 2571 | + '/auto_increment/is' => '', |
|
| 2572 | + '/current_timestamp\(\)/is' => 'CURRENT_TIMESTAMP', // Fix export depuis mariaDB #4374 |
|
| 2573 | + '/(timestamp .* )ON .*$/is' => '\\1', |
|
| 2574 | + '/character set \w+/is' => '', |
|
| 2575 | + '/((big|small|medium|tiny)?int(eger)?)' . $num . '\s*unsigned/is' => '\\1 UNSIGNED', |
|
| 2576 | + '/(text\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
|
| 2577 | + '/((char|varchar)' . $num . '\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
|
| 2578 | + '/(datetime\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00 00:00:00'", |
|
| 2579 | + '/(date\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00'", |
|
| 2580 | + ]; |
|
| 2581 | + |
|
| 2582 | + // pour l'autoincrement, il faut des INTEGER NOT NULL PRIMARY KEY |
|
| 2583 | + $remplace_autocinc = [ |
|
| 2584 | + '/(big|small|medium|tiny)?int(eger)?' . $num . '/is' => 'INTEGER' |
|
| 2585 | + ]; |
|
| 2586 | + // pour les int non autoincrement, il faut un DEFAULT |
|
| 2587 | + $remplace_nonautocinc = [ |
|
| 2588 | + '/((big|small|medium|tiny)?int(eger)?' . $num . '\s+not\s+null)\s*$/is' => "\\1 DEFAULT 0", |
|
| 2589 | + ]; |
|
| 2590 | + |
|
| 2591 | + if (is_string($query)) { |
|
| 2592 | + $query = preg_replace(array_keys($remplace), $remplace, $query); |
|
| 2593 | + if ($autoinc or preg_match(',AUTO_INCREMENT,is', $query)) { |
|
| 2594 | + $query = preg_replace(array_keys($remplace_autocinc), $remplace_autocinc, $query); |
|
| 2595 | + } else { |
|
| 2596 | + $query = preg_replace(array_keys($remplace_nonautocinc), $remplace_nonautocinc, $query); |
|
| 2597 | + $query = _sqlite_collate_ci($query); |
|
| 2598 | + } |
|
| 2599 | + } elseif (is_array($query)) { |
|
| 2600 | + foreach ($query as $k => $q) { |
|
| 2601 | + $ai = ($autoinc ? $k == $autoinc : preg_match(',AUTO_INCREMENT,is', $q)); |
|
| 2602 | + $query[$k] = preg_replace(array_keys($remplace), $remplace, $query[$k]); |
|
| 2603 | + if ($ai) { |
|
| 2604 | + $query[$k] = preg_replace(array_keys($remplace_autocinc), $remplace_autocinc, $query[$k]); |
|
| 2605 | + } else { |
|
| 2606 | + $query[$k] = preg_replace(array_keys($remplace_nonautocinc), $remplace_nonautocinc, $query[$k]); |
|
| 2607 | + $query[$k] = _sqlite_collate_ci($query[$k]); |
|
| 2608 | + } |
|
| 2609 | + } |
|
| 2610 | + } |
|
| 2611 | + |
|
| 2612 | + return $query; |
|
| 2613 | 2613 | } |
| 2614 | 2614 | |
| 2615 | 2615 | /** |
@@ -2621,17 +2621,17 @@ discard block |
||
| 2621 | 2621 | */ |
| 2622 | 2622 | function _sqlite_collate_ci($champ) |
| 2623 | 2623 | { |
| 2624 | - if (stripos($champ, 'COLLATE') !== false) { |
|
| 2625 | - return $champ; |
|
| 2626 | - } |
|
| 2627 | - if (stripos($champ, 'BINARY') !== false) { |
|
| 2628 | - return str_ireplace('BINARY', 'COLLATE BINARY', $champ); |
|
| 2629 | - } |
|
| 2630 | - if (preg_match(',^(char|varchar|(long|small|medium|tiny)?text),i', $champ)) { |
|
| 2631 | - return $champ . ' COLLATE NOCASE'; |
|
| 2632 | - } |
|
| 2633 | - |
|
| 2634 | - return $champ; |
|
| 2624 | + if (stripos($champ, 'COLLATE') !== false) { |
|
| 2625 | + return $champ; |
|
| 2626 | + } |
|
| 2627 | + if (stripos($champ, 'BINARY') !== false) { |
|
| 2628 | + return str_ireplace('BINARY', 'COLLATE BINARY', $champ); |
|
| 2629 | + } |
|
| 2630 | + if (preg_match(',^(char|varchar|(long|small|medium|tiny)?text),i', $champ)) { |
|
| 2631 | + return $champ . ' COLLATE NOCASE'; |
|
| 2632 | + } |
|
| 2633 | + |
|
| 2634 | + return $champ; |
|
| 2635 | 2635 | } |
| 2636 | 2636 | |
| 2637 | 2637 | |
@@ -2650,84 +2650,84 @@ discard block |
||
| 2650 | 2650 | * @return bool|string |
| 2651 | 2651 | */ |
| 2652 | 2652 | function _sqlite_requete_create( |
| 2653 | - $nom, |
|
| 2654 | - $champs, |
|
| 2655 | - $cles, |
|
| 2656 | - $autoinc = false, |
|
| 2657 | - $temporary = false, |
|
| 2658 | - $_ifnotexists = true, |
|
| 2659 | - $serveur = '', |
|
| 2660 | - $requeter = true |
|
| 2653 | + $nom, |
|
| 2654 | + $champs, |
|
| 2655 | + $cles, |
|
| 2656 | + $autoinc = false, |
|
| 2657 | + $temporary = false, |
|
| 2658 | + $_ifnotexists = true, |
|
| 2659 | + $serveur = '', |
|
| 2660 | + $requeter = true |
|
| 2661 | 2661 | ) { |
| 2662 | - $query = $keys = $s = $p = ''; |
|
| 2663 | - |
|
| 2664 | - // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 2665 | - // sans les renseigner (laisse le compilo recuperer la description) |
|
| 2666 | - if (!is_array($champs) || !is_array($cles)) { |
|
| 2667 | - return; |
|
| 2668 | - } |
|
| 2669 | - |
|
| 2670 | - // sqlite ne gere pas KEY tout court dans une requete CREATE TABLE |
|
| 2671 | - // il faut passer par des create index |
|
| 2672 | - // Il gere par contre primary key ! |
|
| 2673 | - // Soit la PK est definie dans les cles, soit dans un champs |
|
| 2674 | - // soit faussement dans les 2 (et dans ce cas, il faut l’enlever à un des 2 endroits !) |
|
| 2675 | - $pk = 'PRIMARY KEY'; |
|
| 2676 | - // le champ de cle primaire |
|
| 2677 | - $champ_pk = !empty($cles[$pk]) ? $cles[$pk] : ''; |
|
| 2678 | - |
|
| 2679 | - foreach ($champs as $k => $v) { |
|
| 2680 | - if (false !== stripos($v, $pk)) { |
|
| 2681 | - $champ_pk = $k; |
|
| 2682 | - // on n'en a plus besoin dans field, vu que defini dans key |
|
| 2683 | - $champs[$k] = preg_replace("/$pk/is", '', $champs[$k]); |
|
| 2684 | - break; |
|
| 2685 | - } |
|
| 2686 | - } |
|
| 2687 | - |
|
| 2688 | - if ($champ_pk) { |
|
| 2689 | - $keys = "\n\t\t$pk ($champ_pk)"; |
|
| 2690 | - } |
|
| 2691 | - // Pas de DEFAULT 0 sur les cles primaires en auto-increment |
|
| 2692 | - if ( |
|
| 2693 | - isset($champs[$champ_pk]) |
|
| 2694 | - and stripos($champs[$champ_pk], 'default 0') !== false |
|
| 2695 | - ) { |
|
| 2696 | - $champs[$champ_pk] = trim(str_ireplace('default 0', '', $champs[$champ_pk])); |
|
| 2697 | - } |
|
| 2698 | - |
|
| 2699 | - $champs = _sqlite_remplacements_definitions_table($champs, $autoinc ? $champ_pk : false); |
|
| 2700 | - foreach ($champs as $k => $v) { |
|
| 2701 | - $query .= "$s\n\t\t$k $v"; |
|
| 2702 | - $s = ','; |
|
| 2703 | - } |
|
| 2704 | - |
|
| 2705 | - $ifnotexists = ''; |
|
| 2706 | - if ($_ifnotexists) { |
|
| 2707 | - $version = spip_sqlite_fetch( |
|
| 2708 | - spip_sqlite_query('select sqlite_version() AS sqlite_version', $serveur), |
|
| 2709 | - '', |
|
| 2710 | - $serveur |
|
| 2711 | - ); |
|
| 2712 | - if (!function_exists('spip_version_compare')) { |
|
| 2713 | - include_spip('plugins/installer'); |
|
| 2714 | - } |
|
| 2715 | - |
|
| 2716 | - if ($version and spip_version_compare($version['sqlite_version'], '3.3.0', '>=')) { |
|
| 2717 | - $ifnotexists = ' IF NOT EXISTS'; |
|
| 2718 | - } else { |
|
| 2719 | - /* simuler le IF EXISTS - version 2 et sqlite < 3.3a */ |
|
| 2720 | - $a = spip_sqlite_showtable($nom, $serveur); |
|
| 2721 | - if (isset($a['key']['KEY ' . $nom])) { |
|
| 2722 | - return true; |
|
| 2723 | - } |
|
| 2724 | - } |
|
| 2725 | - } |
|
| 2726 | - |
|
| 2727 | - $temporary = $temporary ? ' TEMPORARY' : ''; |
|
| 2728 | - $q = "CREATE$temporary TABLE$ifnotexists $nom ($query" . ($keys ? ",$keys" : '') . ")\n"; |
|
| 2729 | - |
|
| 2730 | - return $q; |
|
| 2662 | + $query = $keys = $s = $p = ''; |
|
| 2663 | + |
|
| 2664 | + // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 2665 | + // sans les renseigner (laisse le compilo recuperer la description) |
|
| 2666 | + if (!is_array($champs) || !is_array($cles)) { |
|
| 2667 | + return; |
|
| 2668 | + } |
|
| 2669 | + |
|
| 2670 | + // sqlite ne gere pas KEY tout court dans une requete CREATE TABLE |
|
| 2671 | + // il faut passer par des create index |
|
| 2672 | + // Il gere par contre primary key ! |
|
| 2673 | + // Soit la PK est definie dans les cles, soit dans un champs |
|
| 2674 | + // soit faussement dans les 2 (et dans ce cas, il faut l’enlever à un des 2 endroits !) |
|
| 2675 | + $pk = 'PRIMARY KEY'; |
|
| 2676 | + // le champ de cle primaire |
|
| 2677 | + $champ_pk = !empty($cles[$pk]) ? $cles[$pk] : ''; |
|
| 2678 | + |
|
| 2679 | + foreach ($champs as $k => $v) { |
|
| 2680 | + if (false !== stripos($v, $pk)) { |
|
| 2681 | + $champ_pk = $k; |
|
| 2682 | + // on n'en a plus besoin dans field, vu que defini dans key |
|
| 2683 | + $champs[$k] = preg_replace("/$pk/is", '', $champs[$k]); |
|
| 2684 | + break; |
|
| 2685 | + } |
|
| 2686 | + } |
|
| 2687 | + |
|
| 2688 | + if ($champ_pk) { |
|
| 2689 | + $keys = "\n\t\t$pk ($champ_pk)"; |
|
| 2690 | + } |
|
| 2691 | + // Pas de DEFAULT 0 sur les cles primaires en auto-increment |
|
| 2692 | + if ( |
|
| 2693 | + isset($champs[$champ_pk]) |
|
| 2694 | + and stripos($champs[$champ_pk], 'default 0') !== false |
|
| 2695 | + ) { |
|
| 2696 | + $champs[$champ_pk] = trim(str_ireplace('default 0', '', $champs[$champ_pk])); |
|
| 2697 | + } |
|
| 2698 | + |
|
| 2699 | + $champs = _sqlite_remplacements_definitions_table($champs, $autoinc ? $champ_pk : false); |
|
| 2700 | + foreach ($champs as $k => $v) { |
|
| 2701 | + $query .= "$s\n\t\t$k $v"; |
|
| 2702 | + $s = ','; |
|
| 2703 | + } |
|
| 2704 | + |
|
| 2705 | + $ifnotexists = ''; |
|
| 2706 | + if ($_ifnotexists) { |
|
| 2707 | + $version = spip_sqlite_fetch( |
|
| 2708 | + spip_sqlite_query('select sqlite_version() AS sqlite_version', $serveur), |
|
| 2709 | + '', |
|
| 2710 | + $serveur |
|
| 2711 | + ); |
|
| 2712 | + if (!function_exists('spip_version_compare')) { |
|
| 2713 | + include_spip('plugins/installer'); |
|
| 2714 | + } |
|
| 2715 | + |
|
| 2716 | + if ($version and spip_version_compare($version['sqlite_version'], '3.3.0', '>=')) { |
|
| 2717 | + $ifnotexists = ' IF NOT EXISTS'; |
|
| 2718 | + } else { |
|
| 2719 | + /* simuler le IF EXISTS - version 2 et sqlite < 3.3a */ |
|
| 2720 | + $a = spip_sqlite_showtable($nom, $serveur); |
|
| 2721 | + if (isset($a['key']['KEY ' . $nom])) { |
|
| 2722 | + return true; |
|
| 2723 | + } |
|
| 2724 | + } |
|
| 2725 | + } |
|
| 2726 | + |
|
| 2727 | + $temporary = $temporary ? ' TEMPORARY' : ''; |
|
| 2728 | + $q = "CREATE$temporary TABLE$ifnotexists $nom ($query" . ($keys ? ",$keys" : '') . ")\n"; |
|
| 2729 | + |
|
| 2730 | + return $q; |
|
| 2731 | 2731 | } |
| 2732 | 2732 | |
| 2733 | 2733 | |
@@ -2747,40 +2747,40 @@ discard block |
||
| 2747 | 2747 | */ |
| 2748 | 2748 | function _sqlite_ajouter_champs_timestamp($table, $couples, $desc = '', $serveur = '') |
| 2749 | 2749 | { |
| 2750 | - static $tables = []; |
|
| 2751 | - |
|
| 2752 | - if (!isset($tables[$table])) { |
|
| 2753 | - if (!$desc) { |
|
| 2754 | - $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 2755 | - $desc = $trouver_table($table, $serveur); |
|
| 2756 | - // si pas de description, on ne fait rien, ou on die() ? |
|
| 2757 | - if (!$desc) { |
|
| 2758 | - return $couples; |
|
| 2759 | - } |
|
| 2760 | - } |
|
| 2761 | - |
|
| 2762 | - // recherche des champs avec simplement 'TIMESTAMP' |
|
| 2763 | - // cependant, il faudra peut etre etendre |
|
| 2764 | - // avec la gestion de DEFAULT et ON UPDATE |
|
| 2765 | - // mais ceux-ci ne sont pas utilises dans le core |
|
| 2766 | - $tables[$table] = ['valeur' => [], 'cite' => [], 'desc' => []]; |
|
| 2767 | - |
|
| 2768 | - $now = _sqlite_func_now(true); |
|
| 2769 | - foreach ($desc['field'] as $k => $v) { |
|
| 2770 | - if (strpos(strtolower(ltrim($v)), 'timestamp') === 0) { |
|
| 2771 | - $tables[$table]['desc'][$k] = $v; |
|
| 2772 | - $tables[$table]['valeur'][$k] = _sqlite_calculer_cite($now, $tables[$table]['desc'][$k]); |
|
| 2773 | - } |
|
| 2774 | - } |
|
| 2775 | - } else { |
|
| 2776 | - $now = _sqlite_func_now(true); |
|
| 2777 | - foreach (array_keys($tables[$table]['desc']) as $k) { |
|
| 2778 | - $tables[$table]['valeur'][$k] = _sqlite_calculer_cite($now, $tables[$table]['desc'][$k]); |
|
| 2779 | - } |
|
| 2780 | - } |
|
| 2781 | - |
|
| 2782 | - // ajout des champs type 'timestamp' absents |
|
| 2783 | - return array_merge($tables[$table]['valeur'], $couples); |
|
| 2750 | + static $tables = []; |
|
| 2751 | + |
|
| 2752 | + if (!isset($tables[$table])) { |
|
| 2753 | + if (!$desc) { |
|
| 2754 | + $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 2755 | + $desc = $trouver_table($table, $serveur); |
|
| 2756 | + // si pas de description, on ne fait rien, ou on die() ? |
|
| 2757 | + if (!$desc) { |
|
| 2758 | + return $couples; |
|
| 2759 | + } |
|
| 2760 | + } |
|
| 2761 | + |
|
| 2762 | + // recherche des champs avec simplement 'TIMESTAMP' |
|
| 2763 | + // cependant, il faudra peut etre etendre |
|
| 2764 | + // avec la gestion de DEFAULT et ON UPDATE |
|
| 2765 | + // mais ceux-ci ne sont pas utilises dans le core |
|
| 2766 | + $tables[$table] = ['valeur' => [], 'cite' => [], 'desc' => []]; |
|
| 2767 | + |
|
| 2768 | + $now = _sqlite_func_now(true); |
|
| 2769 | + foreach ($desc['field'] as $k => $v) { |
|
| 2770 | + if (strpos(strtolower(ltrim($v)), 'timestamp') === 0) { |
|
| 2771 | + $tables[$table]['desc'][$k] = $v; |
|
| 2772 | + $tables[$table]['valeur'][$k] = _sqlite_calculer_cite($now, $tables[$table]['desc'][$k]); |
|
| 2773 | + } |
|
| 2774 | + } |
|
| 2775 | + } else { |
|
| 2776 | + $now = _sqlite_func_now(true); |
|
| 2777 | + foreach (array_keys($tables[$table]['desc']) as $k) { |
|
| 2778 | + $tables[$table]['valeur'][$k] = _sqlite_calculer_cite($now, $tables[$table]['desc'][$k]); |
|
| 2779 | + } |
|
| 2780 | + } |
|
| 2781 | + |
|
| 2782 | + // ajout des champs type 'timestamp' absents |
|
| 2783 | + return array_merge($tables[$table]['valeur'], $couples); |
|
| 2784 | 2784 | } |
| 2785 | 2785 | |
| 2786 | 2786 | |
@@ -2792,5 +2792,5 @@ discard block |
||
| 2792 | 2792 | */ |
| 2793 | 2793 | function spip_versions_sqlite() |
| 2794 | 2794 | { |
| 2795 | - return _sqlite_charger_version(); |
|
| 2795 | + return _sqlite_charger_version(); |
|
| 2796 | 2796 | } |
@@ -66,27 +66,27 @@ discard block |
||
| 66 | 66 | // determiner le dossier de la base : $addr ou _DIR_DB |
| 67 | 67 | $f = _DIR_DB; |
| 68 | 68 | if ($addr and str_contains($addr, '/')) { |
| 69 | - $f = rtrim($addr, '/') . '/'; |
|
| 69 | + $f = rtrim($addr, '/').'/'; |
|
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | // un nom de base demande et impossible d'obtenir la base, on s'en va : |
| 73 | 73 | // il faut que la base existe ou que le repertoire parent soit writable |
| 74 | - if ($db and !is_file($f .= $db . '.sqlite') and !is_writable(dirname($f))) { |
|
| 75 | - spip_log("base $f non trouvee ou droits en ecriture manquants", 'sqlite.' . _LOG_HS); |
|
| 74 | + if ($db and !is_file($f .= $db.'.sqlite') and !is_writable(dirname($f))) { |
|
| 75 | + spip_log("base $f non trouvee ou droits en ecriture manquants", 'sqlite.'._LOG_HS); |
|
| 76 | 76 | |
| 77 | 77 | return false; |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | 80 | // charger les modules sqlite au besoin |
| 81 | 81 | if (!_sqlite_charger_version($sqlite_version)) { |
| 82 | - spip_log("Impossible de trouver/charger le module SQLite ($sqlite_version)!", 'sqlite.' . _LOG_HS); |
|
| 82 | + spip_log("Impossible de trouver/charger le module SQLite ($sqlite_version)!", 'sqlite.'._LOG_HS); |
|
| 83 | 83 | |
| 84 | 84 | return false; |
| 85 | 85 | } |
| 86 | 86 | |
| 87 | 87 | // chargement des constantes |
| 88 | 88 | // il ne faut pas definir les constantes avant d'avoir charge les modules sqlite |
| 89 | - $define = 'spip_sqlite' . $sqlite_version . '_constantes'; |
|
| 89 | + $define = 'spip_sqlite'.$sqlite_version.'_constantes'; |
|
| 90 | 90 | $define(); |
| 91 | 91 | |
| 92 | 92 | $ok = false; |
@@ -94,8 +94,8 @@ discard block |
||
| 94 | 94 | // si pas de db -> |
| 95 | 95 | // base temporaire tant qu'on ne connait pas son vrai nom |
| 96 | 96 | // pour tester la connexion |
| 97 | - $db = '_sqlite' . $sqlite_version . '_install'; |
|
| 98 | - $tmp = _DIR_DB . $db . '.sqlite'; |
|
| 97 | + $db = '_sqlite'.$sqlite_version.'_install'; |
|
| 98 | + $tmp = _DIR_DB.$db.'.sqlite'; |
|
| 99 | 99 | $ok = $link = new \PDO("sqlite:$tmp"); |
| 100 | 100 | } else { |
| 101 | 101 | // Ouvrir (eventuellement creer la base) |
@@ -104,7 +104,7 @@ discard block |
||
| 104 | 104 | |
| 105 | 105 | if (!$ok) { |
| 106 | 106 | $e = _sqlite_last_error_from_link($link); |
| 107 | - spip_log("Impossible d'ouvrir la base SQLite($sqlite_version) $f : $e", 'sqlite.' . _LOG_HS); |
|
| 107 | + spip_log("Impossible d'ouvrir la base SQLite($sqlite_version) $f : $e", 'sqlite.'._LOG_HS); |
|
| 108 | 108 | |
| 109 | 109 | return false; |
| 110 | 110 | } |
@@ -192,7 +192,7 @@ discard block |
||
| 192 | 192 | $table = $regs[3]; |
| 193 | 193 | $suite = $regs[4]; |
| 194 | 194 | } else { |
| 195 | - spip_log("SQLite : Probleme de ALTER TABLE mal forme dans $query", 'sqlite.' . _LOG_ERREUR); |
|
| 195 | + spip_log("SQLite : Probleme de ALTER TABLE mal forme dans $query", 'sqlite.'._LOG_ERREUR); |
|
| 196 | 196 | |
| 197 | 197 | return false; |
| 198 | 198 | } |
@@ -209,7 +209,7 @@ discard block |
||
| 209 | 209 | $i = 0; |
| 210 | 210 | $ouverte = false; |
| 211 | 211 | while ($do = array_shift($todo)) { |
| 212 | - $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . ',' . $do : $do; |
|
| 212 | + $todo2[$i] = isset($todo2[$i]) ? $todo2[$i].','.$do : $do; |
|
| 213 | 213 | $o = (str_contains($do, '(')); |
| 214 | 214 | $f = (str_contains($do, ')')); |
| 215 | 215 | if ($o and !$f) { |
@@ -235,7 +235,7 @@ discard block |
||
| 235 | 235 | ) { |
| 236 | 236 | spip_log( |
| 237 | 237 | "SQLite : Probleme de ALTER TABLE, utilisation non reconnue dans : $do \n(requete d'origine : $query)", |
| 238 | - 'sqlite.' . _LOG_ERREUR |
|
| 238 | + 'sqlite.'._LOG_ERREUR |
|
| 239 | 239 | ); |
| 240 | 240 | |
| 241 | 241 | return false; |
@@ -331,10 +331,10 @@ discard block |
||
| 331 | 331 | |
| 332 | 332 | // pas geres en sqlite2 |
| 333 | 333 | case 'RENAME': |
| 334 | - $do = 'RENAME TO' . substr($do, 6); |
|
| 334 | + $do = 'RENAME TO'.substr($do, 6); |
|
| 335 | 335 | case 'RENAME TO': |
| 336 | 336 | if (!Sqlite::executer_requete("$debut $do", $serveur)) { |
| 337 | - spip_log("SQLite : Erreur ALTER TABLE / RENAME : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 337 | + spip_log("SQLite : Erreur ALTER TABLE / RENAME : $query", 'sqlite.'._LOG_ERREUR); |
|
| 338 | 338 | |
| 339 | 339 | return false; |
| 340 | 340 | } |
@@ -377,7 +377,7 @@ discard block |
||
| 377 | 377 | $colonnes = substr($colonne_origine, 1, -1); |
| 378 | 378 | if (str_contains(',', $colonnes)) { |
| 379 | 379 | spip_log('SQLite : Erreur, impossible de creer un index sur plusieurs colonnes' |
| 380 | - . " sans qu'il ait de nom ($table, ($colonnes))", 'sqlite.' . _LOG_ERREUR); |
|
| 380 | + . " sans qu'il ait de nom ($table, ($colonnes))", 'sqlite.'._LOG_ERREUR); |
|
| 381 | 381 | break; |
| 382 | 382 | } else { |
| 383 | 383 | $nom_index = $colonnes; |
@@ -392,12 +392,12 @@ discard block |
||
| 392 | 392 | |
| 393 | 393 | // pas geres en sqlite2 |
| 394 | 394 | case 'ADD COLUMN': |
| 395 | - $do = 'ADD' . substr($do, 10); |
|
| 395 | + $do = 'ADD'.substr($do, 10); |
|
| 396 | 396 | case 'ADD': |
| 397 | 397 | default: |
| 398 | 398 | if (!preg_match(',primary\s+key,i', $do)) { |
| 399 | 399 | if (!Sqlite::executer_requete("$debut $do", $serveur)) { |
| 400 | - spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 400 | + spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.'._LOG_ERREUR); |
|
| 401 | 401 | |
| 402 | 402 | return false; |
| 403 | 403 | } |
@@ -417,7 +417,7 @@ discard block |
||
| 417 | 417 | } |
| 418 | 418 | $opts['field'] = [$colonne_ajoutee => $def]; |
| 419 | 419 | if (!_sqlite_modifier_table($table, [$colonne_ajoutee], $opts, $serveur)) { |
| 420 | - spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 420 | + spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.'._LOG_ERREUR); |
|
| 421 | 421 | |
| 422 | 422 | return false; |
| 423 | 423 | } |
@@ -425,10 +425,10 @@ discard block |
||
| 425 | 425 | break; |
| 426 | 426 | } |
| 427 | 427 | // tout est bon, ouf ! |
| 428 | - spip_log("SQLite ($serveur) : Changements OK : $debut $do", 'sqlite.' . _LOG_INFO); |
|
| 428 | + spip_log("SQLite ($serveur) : Changements OK : $debut $do", 'sqlite.'._LOG_INFO); |
|
| 429 | 429 | } |
| 430 | 430 | |
| 431 | - spip_log("SQLite ($serveur) : fin ALTER TABLE OK !", 'sqlite.' . _LOG_INFO); |
|
| 431 | + spip_log("SQLite ($serveur) : fin ALTER TABLE OK !", 'sqlite.'._LOG_INFO); |
|
| 432 | 432 | |
| 433 | 433 | return true; |
| 434 | 434 | } |
@@ -497,9 +497,9 @@ discard block |
||
| 497 | 497 | **/ |
| 498 | 498 | function spip_sqlite_create_base($nom, $serveur = '', $option = true) |
| 499 | 499 | { |
| 500 | - $f = $nom . '.sqlite'; |
|
| 500 | + $f = $nom.'.sqlite'; |
|
| 501 | 501 | if (strpos($nom, '/') === false) { |
| 502 | - $f = _DIR_DB . $f; |
|
| 502 | + $f = _DIR_DB.$f; |
|
| 503 | 503 | } |
| 504 | 504 | |
| 505 | 505 | $ok = new \PDO("sqlite:$f"); |
@@ -540,13 +540,13 @@ discard block |
||
| 540 | 540 | if (sql_showtable($nom, false, $serveur)) { |
| 541 | 541 | spip_log( |
| 542 | 542 | "Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", |
| 543 | - 'sqlite.' . _LOG_ERREUR |
|
| 543 | + 'sqlite.'._LOG_ERREUR |
|
| 544 | 544 | ); |
| 545 | 545 | |
| 546 | 546 | return false; |
| 547 | 547 | } |
| 548 | 548 | |
| 549 | - $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 549 | + $query = "CREATE VIEW $nom AS ".$query_select; |
|
| 550 | 550 | |
| 551 | 551 | return spip_sqlite_query($query, $serveur, $requeter); |
| 552 | 552 | } |
@@ -573,8 +573,8 @@ discard block |
||
| 573 | 573 | { |
| 574 | 574 | if (!($nom or $table or $champs)) { |
| 575 | 575 | spip_log( |
| 576 | - "Champ manquant pour creer un index sqlite ($nom, $table, (" . join(',', $champs) . '))', |
|
| 577 | - 'sqlite.' . _LOG_ERREUR |
|
| 576 | + "Champ manquant pour creer un index sqlite ($nom, $table, (".join(',', $champs).'))', |
|
| 577 | + 'sqlite.'._LOG_ERREUR |
|
| 578 | 578 | ); |
| 579 | 579 | |
| 580 | 580 | return false; |
@@ -582,7 +582,7 @@ discard block |
||
| 582 | 582 | |
| 583 | 583 | // SQLite ne differentie pas noms des index en fonction des tables |
| 584 | 584 | // il faut donc creer des noms uniques d'index pour une base sqlite |
| 585 | - $nom = $table . '_' . $nom; |
|
| 585 | + $nom = $table.'_'.$nom; |
|
| 586 | 586 | // enlever d'eventuelles parentheses deja presentes sur champs |
| 587 | 587 | if (!is_array($champs)) { |
| 588 | 588 | if ($champs[0] == '(') { |
@@ -604,12 +604,12 @@ discard block |
||
| 604 | 604 | } else { |
| 605 | 605 | /* simuler le IF EXISTS - version 2 et sqlite < 3.3a */ |
| 606 | 606 | $a = spip_sqlite_showtable($table, $serveur); |
| 607 | - if (isset($a['key']['KEY ' . $nom])) { |
|
| 607 | + if (isset($a['key']['KEY '.$nom])) { |
|
| 608 | 608 | return true; |
| 609 | 609 | } |
| 610 | 610 | } |
| 611 | 611 | |
| 612 | - $query = 'CREATE ' . ($unique ? 'UNIQUE ' : '') . "INDEX$ifnotexists $nom ON $table (" . join(',', $champs) . ')'; |
|
| 612 | + $query = 'CREATE '.($unique ? 'UNIQUE ' : '')."INDEX$ifnotexists $nom ON $table (".join(',', $champs).')'; |
|
| 613 | 613 | $res = spip_sqlite_query($query, $serveur, $requeter); |
| 614 | 614 | if (!$requeter) { |
| 615 | 615 | return $res; |
@@ -686,7 +686,7 @@ discard block |
||
| 686 | 686 | $serveur = '', |
| 687 | 687 | $requeter = true |
| 688 | 688 | ) { |
| 689 | - $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 689 | + $c = !$groupby ? '*' : ('DISTINCT '.(is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 690 | 690 | $r = spip_sqlite_select( |
| 691 | 691 | "COUNT($c)", |
| 692 | 692 | $from, |
@@ -799,14 +799,14 @@ discard block |
||
| 799 | 799 | function spip_sqlite_drop_index($nom, $table, $serveur = '', $requeter = true) |
| 800 | 800 | { |
| 801 | 801 | if (!($nom or $table)) { |
| 802 | - spip_log("Champ manquant pour supprimer un index sqlite ($nom, $table)", 'sqlite.' . _LOG_ERREUR); |
|
| 802 | + spip_log("Champ manquant pour supprimer un index sqlite ($nom, $table)", 'sqlite.'._LOG_ERREUR); |
|
| 803 | 803 | |
| 804 | 804 | return false; |
| 805 | 805 | } |
| 806 | 806 | |
| 807 | 807 | // SQLite ne differentie pas noms des index en fonction des tables |
| 808 | 808 | // il faut donc creer des noms uniques d'index pour une base sqlite |
| 809 | - $index = $table . '_' . $nom; |
|
| 809 | + $index = $table.'_'.$nom; |
|
| 810 | 810 | $exist = ' IF EXISTS'; |
| 811 | 811 | |
| 812 | 812 | $query = "DROP INDEX$exist $index"; |
@@ -839,7 +839,7 @@ discard block |
||
| 839 | 839 | if ($s) { |
| 840 | 840 | $trace = debug_backtrace(); |
| 841 | 841 | if ($trace[0]['function'] != 'spip_sqlite_error') { |
| 842 | - spip_log("$s - $query - " . sql_error_backtrace(), 'sqlite.' . _LOG_ERREUR); |
|
| 842 | + spip_log("$s - $query - ".sql_error_backtrace(), 'sqlite.'._LOG_ERREUR); |
|
| 843 | 843 | } |
| 844 | 844 | } |
| 845 | 845 | |
@@ -888,14 +888,14 @@ discard block |
||
| 888 | 888 | $t = $link->errorInfo(); |
| 889 | 889 | $s = ltrim($t[0], '0'); // 00000 si pas d'erreur |
| 890 | 890 | if ($s) { |
| 891 | - $s .= ' / ' . $t[1]; |
|
| 891 | + $s .= ' / '.$t[1]; |
|
| 892 | 892 | } // ajoute l'erreur du moteur SQLite |
| 893 | 893 | } else { |
| 894 | 894 | $s = ': aucune ressource sqlite (link)'; |
| 895 | 895 | } |
| 896 | 896 | |
| 897 | 897 | if ($s) { |
| 898 | - spip_log("Erreur sqlite $s", 'sqlite.' . _LOG_ERREUR); |
|
| 898 | + spip_log("Erreur sqlite $s", 'sqlite.'._LOG_ERREUR); |
|
| 899 | 899 | } |
| 900 | 900 | |
| 901 | 901 | return $s ? $s : 0; |
@@ -920,7 +920,7 @@ discard block |
||
| 920 | 920 | } |
| 921 | 921 | |
| 922 | 922 | $query = Sqlite::traduire_requete($query, $serveur); |
| 923 | - $query = 'EXPLAIN ' . $query; |
|
| 923 | + $query = 'EXPLAIN '.$query; |
|
| 924 | 924 | if (!$requeter) { |
| 925 | 925 | return $query; |
| 926 | 926 | } |
@@ -1096,7 +1096,7 @@ discard block |
||
| 1096 | 1096 | function spip_sqlite_insert($table, $champs, $valeurs, $desc = [], $serveur = '', $requeter = true) |
| 1097 | 1097 | { |
| 1098 | 1098 | |
| 1099 | - $query = "INSERT INTO $table " . ($champs ? "$champs VALUES $valeurs" : 'DEFAULT VALUES'); |
|
| 1099 | + $query = "INSERT INTO $table ".($champs ? "$champs VALUES $valeurs" : 'DEFAULT VALUES'); |
|
| 1100 | 1100 | if ($r = spip_sqlite_query($query, $serveur, $requeter)) { |
| 1101 | 1101 | if (!$requeter) { |
| 1102 | 1102 | return $r; |
@@ -1152,8 +1152,8 @@ discard block |
||
| 1152 | 1152 | |
| 1153 | 1153 | $cles = $valeurs = ''; |
| 1154 | 1154 | if (count($couples)) { |
| 1155 | - $cles = '(' . join(',', array_keys($couples)) . ')'; |
|
| 1156 | - $valeurs = '(' . join(',', $couples) . ')'; |
|
| 1155 | + $cles = '('.join(',', array_keys($couples)).')'; |
|
| 1156 | + $valeurs = '('.join(',', $couples).')'; |
|
| 1157 | 1157 | } |
| 1158 | 1158 | |
| 1159 | 1159 | return spip_sqlite_insert($table, $cles, $valeurs, $desc, $serveur, $requeter); |
@@ -1214,11 +1214,11 @@ discard block |
||
| 1214 | 1214 | |
| 1215 | 1215 | $champs = $valeurs = ''; |
| 1216 | 1216 | if (count($couples)) { |
| 1217 | - $champs = '(' . join(',', array_keys($couples)) . ')'; |
|
| 1218 | - $valeurs = '(' . join(',', $couples) . ')'; |
|
| 1219 | - $query = $query_start . "$champs VALUES $valeurs"; |
|
| 1217 | + $champs = '('.join(',', array_keys($couples)).')'; |
|
| 1218 | + $valeurs = '('.join(',', $couples).')'; |
|
| 1219 | + $query = $query_start."$champs VALUES $valeurs"; |
|
| 1220 | 1220 | } else { |
| 1221 | - $query = $query_start . 'DEFAULT VALUES'; |
|
| 1221 | + $query = $query_start.'DEFAULT VALUES'; |
|
| 1222 | 1222 | } |
| 1223 | 1223 | |
| 1224 | 1224 | if ($requeter) { |
@@ -1355,7 +1355,7 @@ discard block |
||
| 1355 | 1355 | */ |
| 1356 | 1356 | function spip_sqlite_multi($objet, $lang) |
| 1357 | 1357 | { |
| 1358 | - $r = 'EXTRAIRE_MULTI(' . $objet . ", '" . $lang . "') AS multi"; |
|
| 1358 | + $r = 'EXTRAIRE_MULTI('.$objet.", '".$lang."') AS multi"; |
|
| 1359 | 1359 | |
| 1360 | 1360 | return $r; |
| 1361 | 1361 | } |
@@ -1429,7 +1429,7 @@ discard block |
||
| 1429 | 1429 | { |
| 1430 | 1430 | $op = (($interval <= 0) ? '>' : '<'); |
| 1431 | 1431 | |
| 1432 | - return "($champ $op datetime('" . date('Y-m-d H:i:s') . "', '$interval $unite'))"; |
|
| 1432 | + return "($champ $op datetime('".date('Y-m-d H:i:s')."', '$interval $unite'))"; |
|
| 1433 | 1433 | } |
| 1434 | 1434 | |
| 1435 | 1435 | |
@@ -1462,7 +1462,7 @@ discard block |
||
| 1462 | 1462 | and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
| 1463 | 1463 | ) { |
| 1464 | 1464 | spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT ''", $serveur); |
| 1465 | - spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1465 | + spip_log("ALTER $q", 'repair'._LOG_INFO_IMPORTANTE); |
|
| 1466 | 1466 | } |
| 1467 | 1467 | if ( |
| 1468 | 1468 | preg_match(',^(INTEGER),i', $d) |
@@ -1472,7 +1472,7 @@ discard block |
||
| 1472 | 1472 | and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
| 1473 | 1473 | ) { |
| 1474 | 1474 | spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT '0'", $serveur); |
| 1475 | - spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1475 | + spip_log("ALTER $q", 'repair'._LOG_INFO_IMPORTANTE); |
|
| 1476 | 1476 | } |
| 1477 | 1477 | if ( |
| 1478 | 1478 | preg_match(',^(datetime),i', $d) |
@@ -1482,7 +1482,7 @@ discard block |
||
| 1482 | 1482 | and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
| 1483 | 1483 | ) { |
| 1484 | 1484 | spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT '0000-00-00 00:00:00'", $serveur); |
| 1485 | - spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1485 | + spip_log("ALTER $q", 'repair'._LOG_INFO_IMPORTANTE); |
|
| 1486 | 1486 | } |
| 1487 | 1487 | } |
| 1488 | 1488 | |
@@ -1534,10 +1534,10 @@ discard block |
||
| 1534 | 1534 | // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
| 1535 | 1535 | $couples = _sqlite_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
| 1536 | 1536 | |
| 1537 | - return spip_sqlite_query("REPLACE INTO $table (" . join(',', array_keys($couples)) . ') VALUES (' . join( |
|
| 1537 | + return spip_sqlite_query("REPLACE INTO $table (".join(',', array_keys($couples)).') VALUES ('.join( |
|
| 1538 | 1538 | ',', |
| 1539 | 1539 | $couples |
| 1540 | - ) . ')', $serveur); |
|
| 1540 | + ).')', $serveur); |
|
| 1541 | 1541 | } |
| 1542 | 1542 | |
| 1543 | 1543 | |
@@ -1624,7 +1624,7 @@ discard block |
||
| 1624 | 1624 | . _sqlite_calculer_expression('WHERE', $where) |
| 1625 | 1625 | . _sqlite_calculer_expression('GROUP BY', $groupby, ',') |
| 1626 | 1626 | . _sqlite_calculer_expression('HAVING', $having) |
| 1627 | - . ($orderby ? ("\nORDER BY " . _sqlite_calculer_order($orderby)) : '') |
|
| 1627 | + . ($orderby ? ("\nORDER BY "._sqlite_calculer_order($orderby)) : '') |
|
| 1628 | 1628 | . ($limit ? "\nLIMIT $limit" : ''); |
| 1629 | 1629 | |
| 1630 | 1630 | // dans un select, on doit renvoyer la requête en cas d'erreur |
@@ -1663,10 +1663,10 @@ discard block |
||
| 1663 | 1663 | // interdire la creation d'une nouvelle base, |
| 1664 | 1664 | // sauf si on est dans l'installation |
| 1665 | 1665 | if ( |
| 1666 | - !is_file($f = _DIR_DB . $db . '.sqlite') |
|
| 1666 | + !is_file($f = _DIR_DB.$db.'.sqlite') |
|
| 1667 | 1667 | && (!defined('_ECRIRE_INSTALL') || !_ECRIRE_INSTALL) |
| 1668 | 1668 | ) { |
| 1669 | - spip_log("Il est interdit de creer la base $db", 'sqlite.' . _LOG_HS); |
|
| 1669 | + spip_log("Il est interdit de creer la base $db", 'sqlite.'._LOG_HS); |
|
| 1670 | 1670 | |
| 1671 | 1671 | return false; |
| 1672 | 1672 | } |
@@ -1675,12 +1675,12 @@ discard block |
||
| 1675 | 1675 | // avec les identifiants connus |
| 1676 | 1676 | $index = $serveur ? $serveur : 0; |
| 1677 | 1677 | |
| 1678 | - if ($link = spip_connect_db('', '', '', '', '@selectdb@' . $db, $serveur, '', '')) { |
|
| 1678 | + if ($link = spip_connect_db('', '', '', '', '@selectdb@'.$db, $serveur, '', '')) { |
|
| 1679 | 1679 | if (($db == $link['db']) && $GLOBALS['connexions'][$index] = $link) { |
| 1680 | 1680 | return $db; |
| 1681 | 1681 | } |
| 1682 | 1682 | } else { |
| 1683 | - spip_log("Impossible de selectionner la base $db", 'sqlite.' . _LOG_HS); |
|
| 1683 | + spip_log("Impossible de selectionner la base $db", 'sqlite.'._LOG_HS); |
|
| 1684 | 1684 | } |
| 1685 | 1685 | |
| 1686 | 1686 | return false; |
@@ -1733,7 +1733,7 @@ discard block |
||
| 1733 | 1733 | $match = "^$match$"; |
| 1734 | 1734 | |
| 1735 | 1735 | return spip_sqlite_query( |
| 1736 | - "SELECT name FROM sqlite_master WHERE type='table' AND tbl_name REGEXP " . _q($match), |
|
| 1736 | + "SELECT name FROM sqlite_master WHERE type='table' AND tbl_name REGEXP "._q($match), |
|
| 1737 | 1737 | $serveur, |
| 1738 | 1738 | $requeter |
| 1739 | 1739 | ); |
@@ -1758,7 +1758,7 @@ discard block |
||
| 1758 | 1758 | $r = spip_sqlite_query( |
| 1759 | 1759 | 'SELECT name FROM sqlite_master WHERE' |
| 1760 | 1760 | . ' type=\'table\'' |
| 1761 | - . ' AND name=' . spip_sqlite_quote($table, 'string') |
|
| 1761 | + . ' AND name='.spip_sqlite_quote($table, 'string') |
|
| 1762 | 1762 | . ' AND name NOT LIKE \'sqlite_%\'', |
| 1763 | 1763 | $serveur, |
| 1764 | 1764 | $requeter |
@@ -1856,7 +1856,7 @@ discard block |
||
| 1856 | 1856 | // s'il y a une parenthèse fermante dans la clé |
| 1857 | 1857 | // ou dans la définition sans qu'il n'y ait une ouverture avant |
| 1858 | 1858 | if (str_contains($k, ')') or preg_match('/^[^\(]*\)/', $def)) { |
| 1859 | - $fields[$k_precedent] .= ',' . $k . ' ' . $def; |
|
| 1859 | + $fields[$k_precedent] .= ','.$k.' '.$def; |
|
| 1860 | 1860 | continue; |
| 1861 | 1861 | } |
| 1862 | 1862 | |
@@ -1891,13 +1891,13 @@ discard block |
||
| 1891 | 1891 | . 'ORDER BY substr(type,2,1), name'; |
| 1892 | 1892 | $a = spip_sqlite_query($query, $serveur, $requeter); |
| 1893 | 1893 | while ($r = spip_sqlite_fetch($a, null, $serveur)) { |
| 1894 | - $key = str_replace($nom_table . '_', '', $r['name']); // enlever le nom de la table ajoute a l'index |
|
| 1894 | + $key = str_replace($nom_table.'_', '', $r['name']); // enlever le nom de la table ajoute a l'index |
|
| 1895 | 1895 | $keytype = 'KEY'; |
| 1896 | 1896 | if (strpos($r['sql'], 'UNIQUE INDEX') !== false) { |
| 1897 | 1897 | $keytype = 'UNIQUE KEY'; |
| 1898 | 1898 | } |
| 1899 | 1899 | $colonnes = preg_replace(',.*\((.*)\).*,', '$1', $r['sql']); |
| 1900 | - $keys[$keytype . ' ' . $key] = $colonnes; |
|
| 1900 | + $keys[$keytype.' '.$key] = $colonnes; |
|
| 1901 | 1901 | } |
| 1902 | 1902 | } |
| 1903 | 1903 | } // c'est une vue, on liste les champs disponibles simplement |
@@ -1945,7 +1945,7 @@ discard block |
||
| 1945 | 1945 | |
| 1946 | 1946 | $set = []; |
| 1947 | 1947 | foreach ($champs as $champ => $val) { |
| 1948 | - $set[] = $champ . "=$val"; |
|
| 1948 | + $set[] = $champ."=$val"; |
|
| 1949 | 1949 | } |
| 1950 | 1950 | if (!empty($set)) { |
| 1951 | 1951 | return spip_sqlite_query( |
@@ -2001,7 +2001,7 @@ discard block |
||
| 2001 | 2001 | |
| 2002 | 2002 | $set = []; |
| 2003 | 2003 | foreach ($champs as $champ => $val) { |
| 2004 | - $set[$champ] = $champ . '=' . _sqlite_calculer_cite($val, isset($fields[$champ]) ? $fields[$champ] : ''); |
|
| 2004 | + $set[$champ] = $champ.'='._sqlite_calculer_cite($val, isset($fields[$champ]) ? $fields[$champ] : ''); |
|
| 2005 | 2005 | } |
| 2006 | 2006 | |
| 2007 | 2007 | // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
@@ -2009,7 +2009,7 @@ discard block |
||
| 2009 | 2009 | $maj = _sqlite_ajouter_champs_timestamp($table, [], $desc, $serveur); |
| 2010 | 2010 | foreach ($maj as $champ => $val) { |
| 2011 | 2011 | if (!isset($set[$champ])) { |
| 2012 | - $set[$champ] = $champ . '=' . $val; |
|
| 2012 | + $set[$champ] = $champ.'='.$val; |
|
| 2013 | 2013 | } |
| 2014 | 2014 | } |
| 2015 | 2015 | |
@@ -2039,7 +2039,7 @@ discard block |
||
| 2039 | 2039 | function _sqlite_init() |
| 2040 | 2040 | { |
| 2041 | 2041 | if (!defined('_DIR_DB')) { |
| 2042 | - define('_DIR_DB', _DIR_ETC . 'bases/'); |
|
| 2042 | + define('_DIR_DB', _DIR_ETC.'bases/'); |
|
| 2043 | 2043 | } |
| 2044 | 2044 | if (!defined('_SQLITE_CHMOD')) { |
| 2045 | 2045 | define('_SQLITE_CHMOD', _SPIP_CHMOD); |
@@ -2151,9 +2151,9 @@ discard block |
||
| 2151 | 2151 | } |
| 2152 | 2152 | |
| 2153 | 2153 | // echapper les ' en '' |
| 2154 | - spip_log('Pas de methode ->quote pour echapper', 'sqlite.' . _LOG_INFO_IMPORTANTE); |
|
| 2154 | + spip_log('Pas de methode ->quote pour echapper', 'sqlite.'._LOG_INFO_IMPORTANTE); |
|
| 2155 | 2155 | |
| 2156 | - return ("'" . str_replace("'", "''", $v) . "'"); |
|
| 2156 | + return ("'".str_replace("'", "''", $v)."'"); |
|
| 2157 | 2157 | } |
| 2158 | 2158 | |
| 2159 | 2159 | |
@@ -2177,12 +2177,12 @@ discard block |
||
| 2177 | 2177 | $exp = "\n$expression "; |
| 2178 | 2178 | |
| 2179 | 2179 | if (!is_array($v)) { |
| 2180 | - return $exp . $v; |
|
| 2180 | + return $exp.$v; |
|
| 2181 | 2181 | } else { |
| 2182 | 2182 | if (strtoupper($join) === 'AND') { |
| 2183 | - return $exp . join("\n\t$join ", array_map('_sqlite_calculer_where', $v)); |
|
| 2183 | + return $exp.join("\n\t$join ", array_map('_sqlite_calculer_where', $v)); |
|
| 2184 | 2184 | } else { |
| 2185 | - return $exp . join($join, $v); |
|
| 2185 | + return $exp.join($join, $v); |
|
| 2186 | 2186 | } |
| 2187 | 2187 | } |
| 2188 | 2188 | } |
@@ -2218,17 +2218,17 @@ discard block |
||
| 2218 | 2218 | if (substr($k, -1) == '@') { |
| 2219 | 2219 | // c'est une jointure qui se refere au from precedent |
| 2220 | 2220 | // pas de virgule |
| 2221 | - $res .= ' ' . $v; |
|
| 2221 | + $res .= ' '.$v; |
|
| 2222 | 2222 | } else { |
| 2223 | 2223 | if (!is_numeric($k)) { |
| 2224 | 2224 | $p = strpos($v, ' '); |
| 2225 | 2225 | if ($p) { |
| 2226 | - $v = substr($v, 0, $p) . " AS '$k'" . substr($v, $p); |
|
| 2226 | + $v = substr($v, 0, $p)." AS '$k'".substr($v, $p); |
|
| 2227 | 2227 | } else { |
| 2228 | 2228 | $v .= " AS '$k'"; |
| 2229 | 2229 | } |
| 2230 | 2230 | } |
| 2231 | - $res .= ', ' . $v; |
|
| 2231 | + $res .= ', '.$v; |
|
| 2232 | 2232 | } |
| 2233 | 2233 | } |
| 2234 | 2234 | |
@@ -2369,13 +2369,13 @@ discard block |
||
| 2369 | 2369 | |
| 2370 | 2370 | $def_origine = sql_showtable($table_origine, false, $serveur); |
| 2371 | 2371 | if (!$def_origine or !isset($def_origine['field'])) { |
| 2372 | - spip_log("Alter table impossible sur $table_origine : table non trouvee", 'sqlite' . _LOG_ERREUR); |
|
| 2372 | + spip_log("Alter table impossible sur $table_origine : table non trouvee", 'sqlite'._LOG_ERREUR); |
|
| 2373 | 2373 | |
| 2374 | 2374 | return false; |
| 2375 | 2375 | } |
| 2376 | 2376 | |
| 2377 | 2377 | |
| 2378 | - $table_tmp = $table_origine . '_tmp'; |
|
| 2378 | + $table_tmp = $table_origine.'_tmp'; |
|
| 2379 | 2379 | |
| 2380 | 2380 | // 1) creer une table temporaire avec les modifications |
| 2381 | 2381 | // - DROP : suppression de la colonne |
@@ -2462,7 +2462,7 @@ discard block |
||
| 2462 | 2462 | } else { |
| 2463 | 2463 | // enlever KEY |
| 2464 | 2464 | $k = substr($k, 4); |
| 2465 | - $queries[] = "CREATE INDEX $table_destination" . "_$k ON $table_destination ($v)"; |
|
| 2465 | + $queries[] = "CREATE INDEX $table_destination"."_$k ON $table_destination ($v)"; |
|
| 2466 | 2466 | } |
| 2467 | 2467 | } |
| 2468 | 2468 | |
@@ -2473,7 +2473,7 @@ discard block |
||
| 2473 | 2473 | foreach ($queries as $q) { |
| 2474 | 2474 | if (!Sqlite::executer_requete($q, $serveur)) { |
| 2475 | 2475 | spip_log('SQLite : ALTER TABLE table :' |
| 2476 | - . " Erreur a l'execution de la requete : $q", 'sqlite.' . _LOG_ERREUR); |
|
| 2476 | + . " Erreur a l'execution de la requete : $q", 'sqlite.'._LOG_ERREUR); |
|
| 2477 | 2477 | Sqlite::annuler_transaction($serveur); |
| 2478 | 2478 | |
| 2479 | 2479 | return false; |
@@ -2565,27 +2565,27 @@ discard block |
||
| 2565 | 2565 | $enum = '(\s*\([^\)]*\))?'; |
| 2566 | 2566 | |
| 2567 | 2567 | $remplace = [ |
| 2568 | - '/enum' . $enum . '/is' => 'VARCHAR(255)', |
|
| 2568 | + '/enum'.$enum.'/is' => 'VARCHAR(255)', |
|
| 2569 | 2569 | '/COLLATE \w+_bin/is' => 'COLLATE BINARY', |
| 2570 | 2570 | '/COLLATE \w+_ci/is' => 'COLLATE NOCASE', |
| 2571 | 2571 | '/auto_increment/is' => '', |
| 2572 | 2572 | '/current_timestamp\(\)/is' => 'CURRENT_TIMESTAMP', // Fix export depuis mariaDB #4374 |
| 2573 | 2573 | '/(timestamp .* )ON .*$/is' => '\\1', |
| 2574 | 2574 | '/character set \w+/is' => '', |
| 2575 | - '/((big|small|medium|tiny)?int(eger)?)' . $num . '\s*unsigned/is' => '\\1 UNSIGNED', |
|
| 2575 | + '/((big|small|medium|tiny)?int(eger)?)'.$num.'\s*unsigned/is' => '\\1 UNSIGNED', |
|
| 2576 | 2576 | '/(text\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
| 2577 | - '/((char|varchar)' . $num . '\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
|
| 2577 | + '/((char|varchar)'.$num.'\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
|
| 2578 | 2578 | '/(datetime\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00 00:00:00'", |
| 2579 | 2579 | '/(date\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00'", |
| 2580 | 2580 | ]; |
| 2581 | 2581 | |
| 2582 | 2582 | // pour l'autoincrement, il faut des INTEGER NOT NULL PRIMARY KEY |
| 2583 | 2583 | $remplace_autocinc = [ |
| 2584 | - '/(big|small|medium|tiny)?int(eger)?' . $num . '/is' => 'INTEGER' |
|
| 2584 | + '/(big|small|medium|tiny)?int(eger)?'.$num.'/is' => 'INTEGER' |
|
| 2585 | 2585 | ]; |
| 2586 | 2586 | // pour les int non autoincrement, il faut un DEFAULT |
| 2587 | 2587 | $remplace_nonautocinc = [ |
| 2588 | - '/((big|small|medium|tiny)?int(eger)?' . $num . '\s+not\s+null)\s*$/is' => "\\1 DEFAULT 0", |
|
| 2588 | + '/((big|small|medium|tiny)?int(eger)?'.$num.'\s+not\s+null)\s*$/is' => "\\1 DEFAULT 0", |
|
| 2589 | 2589 | ]; |
| 2590 | 2590 | |
| 2591 | 2591 | if (is_string($query)) { |
@@ -2628,7 +2628,7 @@ discard block |
||
| 2628 | 2628 | return str_ireplace('BINARY', 'COLLATE BINARY', $champ); |
| 2629 | 2629 | } |
| 2630 | 2630 | if (preg_match(',^(char|varchar|(long|small|medium|tiny)?text),i', $champ)) { |
| 2631 | - return $champ . ' COLLATE NOCASE'; |
|
| 2631 | + return $champ.' COLLATE NOCASE'; |
|
| 2632 | 2632 | } |
| 2633 | 2633 | |
| 2634 | 2634 | return $champ; |
@@ -2718,14 +2718,14 @@ discard block |
||
| 2718 | 2718 | } else { |
| 2719 | 2719 | /* simuler le IF EXISTS - version 2 et sqlite < 3.3a */ |
| 2720 | 2720 | $a = spip_sqlite_showtable($nom, $serveur); |
| 2721 | - if (isset($a['key']['KEY ' . $nom])) { |
|
| 2721 | + if (isset($a['key']['KEY '.$nom])) { |
|
| 2722 | 2722 | return true; |
| 2723 | 2723 | } |
| 2724 | 2724 | } |
| 2725 | 2725 | } |
| 2726 | 2726 | |
| 2727 | 2727 | $temporary = $temporary ? ' TEMPORARY' : ''; |
| 2728 | - $q = "CREATE$temporary TABLE$ifnotexists $nom ($query" . ($keys ? ",$keys" : '') . ")\n"; |
|
| 2728 | + $q = "CREATE$temporary TABLE$ifnotexists $nom ($query".($keys ? ",$keys" : '').")\n"; |
|
| 2729 | 2729 | |
| 2730 | 2730 | return $q; |
| 2731 | 2731 | } |
@@ -20,11 +20,11 @@ discard block |
||
| 20 | 20 | */ |
| 21 | 21 | |
| 22 | 22 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 23 | - return; |
|
| 23 | + return; |
|
| 24 | 24 | } |
| 25 | 25 | |
| 26 | 26 | if (!defined('_MYSQL_NOPLANES')) { |
| 27 | - define('_MYSQL_NOPLANES', true); |
|
| 27 | + define('_MYSQL_NOPLANES', true); |
|
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 30 | /** |
@@ -41,112 +41,112 @@ discard block |
||
| 41 | 41 | * - tableau décrivant la connexion sinon |
| 42 | 42 | */ |
| 43 | 43 | function req_mysql_dist($host, $port, $login, $pass, $db = '', $prefixe = '') { |
| 44 | - if (!extension_loaded(\mysqli::class)) { |
|
| 45 | - return false; |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - // si port est fourni mais pas host, c'est un socket -> compat avec vieille syntaxe de mysql_connect() et anciens fichiers connect.php |
|
| 49 | - try { |
|
| 50 | - if ( |
|
| 51 | - $port and !is_numeric($socket = $port) |
|
| 52 | - and (!$host or $host === 'localhost') |
|
| 53 | - ) { |
|
| 54 | - $link = @mysqli_connect($host, $login, $pass, '', null, $socket); |
|
| 55 | - } elseif ($port) { |
|
| 56 | - $link = @mysqli_connect($host, $login, $pass, '', $port); |
|
| 57 | - } else { |
|
| 58 | - $link = @mysqli_connect($host, $login, $pass); |
|
| 59 | - } |
|
| 60 | - } catch (\mysqli_sql_exception $e) { |
|
| 61 | - spip_log('mysqli_sql_exception: ' . $e->getMessage(), 'mysql.' . _LOG_DEBUG); |
|
| 62 | - $link = false; |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - if (!$link) { |
|
| 66 | - spip_log('Echec mysqli_connect. Erreur : ' . mysqli_connect_error(), 'mysql.' . _LOG_HS); |
|
| 67 | - |
|
| 68 | - return false; |
|
| 69 | - } |
|
| 70 | - $last = ''; |
|
| 71 | - if (!$db) { |
|
| 72 | - $ok = $link; |
|
| 73 | - $db = 'spip'; |
|
| 74 | - } else { |
|
| 75 | - $ok = mysqli_select_db($link, $db); |
|
| 76 | - if ( |
|
| 77 | - defined('_MYSQL_SET_SQL_MODE') |
|
| 78 | - or defined('_MYSQL_SQL_MODE_TEXT_NOT_NULL') // compatibilite |
|
| 79 | - ) { |
|
| 80 | - mysqli_query($link, $last = "set sql_mode=''"); |
|
| 81 | - } |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - spip_log( |
|
| 85 | - "Connexion MySQLi vers $host, base $db, prefixe $prefixe " . ($ok ? 'operationnelle' : 'impossible'), |
|
| 86 | - _LOG_DEBUG |
|
| 87 | - ); |
|
| 88 | - |
|
| 89 | - return !$ok ? false : [ |
|
| 90 | - 'db' => $db, |
|
| 91 | - 'last' => $last, |
|
| 92 | - 'prefixe' => $prefixe ?: $db, |
|
| 93 | - 'link' => $link, |
|
| 94 | - 'total_requetes' => 0, |
|
| 95 | - ]; |
|
| 44 | + if (!extension_loaded(\mysqli::class)) { |
|
| 45 | + return false; |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + // si port est fourni mais pas host, c'est un socket -> compat avec vieille syntaxe de mysql_connect() et anciens fichiers connect.php |
|
| 49 | + try { |
|
| 50 | + if ( |
|
| 51 | + $port and !is_numeric($socket = $port) |
|
| 52 | + and (!$host or $host === 'localhost') |
|
| 53 | + ) { |
|
| 54 | + $link = @mysqli_connect($host, $login, $pass, '', null, $socket); |
|
| 55 | + } elseif ($port) { |
|
| 56 | + $link = @mysqli_connect($host, $login, $pass, '', $port); |
|
| 57 | + } else { |
|
| 58 | + $link = @mysqli_connect($host, $login, $pass); |
|
| 59 | + } |
|
| 60 | + } catch (\mysqli_sql_exception $e) { |
|
| 61 | + spip_log('mysqli_sql_exception: ' . $e->getMessage(), 'mysql.' . _LOG_DEBUG); |
|
| 62 | + $link = false; |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + if (!$link) { |
|
| 66 | + spip_log('Echec mysqli_connect. Erreur : ' . mysqli_connect_error(), 'mysql.' . _LOG_HS); |
|
| 67 | + |
|
| 68 | + return false; |
|
| 69 | + } |
|
| 70 | + $last = ''; |
|
| 71 | + if (!$db) { |
|
| 72 | + $ok = $link; |
|
| 73 | + $db = 'spip'; |
|
| 74 | + } else { |
|
| 75 | + $ok = mysqli_select_db($link, $db); |
|
| 76 | + if ( |
|
| 77 | + defined('_MYSQL_SET_SQL_MODE') |
|
| 78 | + or defined('_MYSQL_SQL_MODE_TEXT_NOT_NULL') // compatibilite |
|
| 79 | + ) { |
|
| 80 | + mysqli_query($link, $last = "set sql_mode=''"); |
|
| 81 | + } |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + spip_log( |
|
| 85 | + "Connexion MySQLi vers $host, base $db, prefixe $prefixe " . ($ok ? 'operationnelle' : 'impossible'), |
|
| 86 | + _LOG_DEBUG |
|
| 87 | + ); |
|
| 88 | + |
|
| 89 | + return !$ok ? false : [ |
|
| 90 | + 'db' => $db, |
|
| 91 | + 'last' => $last, |
|
| 92 | + 'prefixe' => $prefixe ?: $db, |
|
| 93 | + 'link' => $link, |
|
| 94 | + 'total_requetes' => 0, |
|
| 95 | + ]; |
|
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | |
| 99 | 99 | $GLOBALS['spip_mysql_functions_1'] = [ |
| 100 | - 'alter' => 'spip_mysql_alter', |
|
| 101 | - 'count' => 'spip_mysql_count', |
|
| 102 | - 'countsel' => 'spip_mysql_countsel', |
|
| 103 | - 'create' => 'spip_mysql_create', |
|
| 104 | - 'create_base' => 'spip_mysql_create_base', |
|
| 105 | - 'create_view' => 'spip_mysql_create_view', |
|
| 106 | - 'date_proche' => 'spip_mysql_date_proche', |
|
| 107 | - 'delete' => 'spip_mysql_delete', |
|
| 108 | - 'drop_table' => 'spip_mysql_drop_table', |
|
| 109 | - 'drop_view' => 'spip_mysql_drop_view', |
|
| 110 | - 'errno' => 'spip_mysql_errno', |
|
| 111 | - 'error' => 'spip_mysql_error', |
|
| 112 | - 'explain' => 'spip_mysql_explain', |
|
| 113 | - 'fetch' => 'spip_mysql_fetch', |
|
| 114 | - 'seek' => 'spip_mysql_seek', |
|
| 115 | - 'free' => 'spip_mysql_free', |
|
| 116 | - 'hex' => 'spip_mysql_hex', |
|
| 117 | - 'in' => 'spip_mysql_in', |
|
| 118 | - 'insert' => 'spip_mysql_insert', |
|
| 119 | - 'insertq' => 'spip_mysql_insertq', |
|
| 120 | - 'insertq_multi' => 'spip_mysql_insertq_multi', |
|
| 121 | - 'listdbs' => 'spip_mysql_listdbs', |
|
| 122 | - 'multi' => 'spip_mysql_multi', |
|
| 123 | - 'optimize' => 'spip_mysql_optimize', |
|
| 124 | - 'query' => 'spip_mysql_query', |
|
| 125 | - 'quote' => 'spip_mysql_quote', |
|
| 126 | - 'replace' => 'spip_mysql_replace', |
|
| 127 | - 'replace_multi' => 'spip_mysql_replace_multi', |
|
| 128 | - 'repair' => 'spip_mysql_repair', |
|
| 129 | - 'select' => 'spip_mysql_select', |
|
| 130 | - 'selectdb' => 'spip_mysql_selectdb', |
|
| 131 | - 'set_charset' => 'spip_mysql_set_charset', |
|
| 132 | - 'get_charset' => 'spip_mysql_get_charset', |
|
| 133 | - 'showbase' => 'spip_mysql_showbase', |
|
| 134 | - 'showtable' => 'spip_mysql_showtable', |
|
| 135 | - 'table_exists' => 'spip_mysql_table_exists', |
|
| 136 | - 'update' => 'spip_mysql_update', |
|
| 137 | - 'updateq' => 'spip_mysql_updateq', |
|
| 138 | - |
|
| 139 | - // association de chaque nom http d'un charset aux couples MySQL |
|
| 140 | - 'charsets' => [ |
|
| 141 | - 'cp1250' => ['charset' => 'cp1250', 'collation' => 'cp1250_general_ci'], |
|
| 142 | - 'cp1251' => ['charset' => 'cp1251', 'collation' => 'cp1251_general_ci'], |
|
| 143 | - 'cp1256' => ['charset' => 'cp1256', 'collation' => 'cp1256_general_ci'], |
|
| 144 | - 'iso-8859-1' => ['charset' => 'latin1', 'collation' => 'latin1_swedish_ci'], |
|
| 100 | + 'alter' => 'spip_mysql_alter', |
|
| 101 | + 'count' => 'spip_mysql_count', |
|
| 102 | + 'countsel' => 'spip_mysql_countsel', |
|
| 103 | + 'create' => 'spip_mysql_create', |
|
| 104 | + 'create_base' => 'spip_mysql_create_base', |
|
| 105 | + 'create_view' => 'spip_mysql_create_view', |
|
| 106 | + 'date_proche' => 'spip_mysql_date_proche', |
|
| 107 | + 'delete' => 'spip_mysql_delete', |
|
| 108 | + 'drop_table' => 'spip_mysql_drop_table', |
|
| 109 | + 'drop_view' => 'spip_mysql_drop_view', |
|
| 110 | + 'errno' => 'spip_mysql_errno', |
|
| 111 | + 'error' => 'spip_mysql_error', |
|
| 112 | + 'explain' => 'spip_mysql_explain', |
|
| 113 | + 'fetch' => 'spip_mysql_fetch', |
|
| 114 | + 'seek' => 'spip_mysql_seek', |
|
| 115 | + 'free' => 'spip_mysql_free', |
|
| 116 | + 'hex' => 'spip_mysql_hex', |
|
| 117 | + 'in' => 'spip_mysql_in', |
|
| 118 | + 'insert' => 'spip_mysql_insert', |
|
| 119 | + 'insertq' => 'spip_mysql_insertq', |
|
| 120 | + 'insertq_multi' => 'spip_mysql_insertq_multi', |
|
| 121 | + 'listdbs' => 'spip_mysql_listdbs', |
|
| 122 | + 'multi' => 'spip_mysql_multi', |
|
| 123 | + 'optimize' => 'spip_mysql_optimize', |
|
| 124 | + 'query' => 'spip_mysql_query', |
|
| 125 | + 'quote' => 'spip_mysql_quote', |
|
| 126 | + 'replace' => 'spip_mysql_replace', |
|
| 127 | + 'replace_multi' => 'spip_mysql_replace_multi', |
|
| 128 | + 'repair' => 'spip_mysql_repair', |
|
| 129 | + 'select' => 'spip_mysql_select', |
|
| 130 | + 'selectdb' => 'spip_mysql_selectdb', |
|
| 131 | + 'set_charset' => 'spip_mysql_set_charset', |
|
| 132 | + 'get_charset' => 'spip_mysql_get_charset', |
|
| 133 | + 'showbase' => 'spip_mysql_showbase', |
|
| 134 | + 'showtable' => 'spip_mysql_showtable', |
|
| 135 | + 'table_exists' => 'spip_mysql_table_exists', |
|
| 136 | + 'update' => 'spip_mysql_update', |
|
| 137 | + 'updateq' => 'spip_mysql_updateq', |
|
| 138 | + |
|
| 139 | + // association de chaque nom http d'un charset aux couples MySQL |
|
| 140 | + 'charsets' => [ |
|
| 141 | + 'cp1250' => ['charset' => 'cp1250', 'collation' => 'cp1250_general_ci'], |
|
| 142 | + 'cp1251' => ['charset' => 'cp1251', 'collation' => 'cp1251_general_ci'], |
|
| 143 | + 'cp1256' => ['charset' => 'cp1256', 'collation' => 'cp1256_general_ci'], |
|
| 144 | + 'iso-8859-1' => ['charset' => 'latin1', 'collation' => 'latin1_swedish_ci'], |
|
| 145 | 145 | //'iso-8859-6'=>array('charset'=>'latin1','collation'=>'latin1_swedish_ci'), |
| 146 | - 'iso-8859-9' => ['charset' => 'latin5', 'collation' => 'latin5_turkish_ci'], |
|
| 146 | + 'iso-8859-9' => ['charset' => 'latin5', 'collation' => 'latin5_turkish_ci'], |
|
| 147 | 147 | //'iso-8859-15'=>array('charset'=>'latin1','collation'=>'latin1_swedish_ci'), |
| 148 | - 'utf-8' => ['charset' => 'utf8', 'collation' => 'utf8_general_ci'] |
|
| 149 | - ] |
|
| 148 | + 'utf-8' => ['charset' => 'utf8', 'collation' => 'utf8_general_ci'] |
|
| 149 | + ] |
|
| 150 | 150 | ]; |
| 151 | 151 | |
| 152 | 152 | |
@@ -157,9 +157,9 @@ discard block |
||
| 157 | 157 | * @return Object Information de connexion pour mysqli |
| 158 | 158 | */ |
| 159 | 159 | function _mysql_link($serveur = '') { |
| 160 | - $link = &$GLOBALS['connexions'][$serveur ?: 0]['link']; |
|
| 160 | + $link = &$GLOBALS['connexions'][$serveur ?: 0]['link']; |
|
| 161 | 161 | |
| 162 | - return $link; |
|
| 162 | + return $link; |
|
| 163 | 163 | } |
| 164 | 164 | |
| 165 | 165 | |
@@ -172,10 +172,10 @@ discard block |
||
| 172 | 172 | * @return mysqli_result|bool Jeu de résultats pour fetch() |
| 173 | 173 | */ |
| 174 | 174 | function spip_mysql_set_charset($charset, $serveur = '', $requeter = true) { |
| 175 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 176 | - spip_log('changement de charset sql : ' . 'SET NAMES ' . _q($charset), _LOG_DEBUG); |
|
| 175 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 176 | + spip_log('changement de charset sql : ' . 'SET NAMES ' . _q($charset), _LOG_DEBUG); |
|
| 177 | 177 | |
| 178 | - return mysqli_query($connexion['link'], $connexion['last'] = 'SET NAMES ' . _q($charset)); |
|
| 178 | + return mysqli_query($connexion['link'], $connexion['last'] = 'SET NAMES ' . _q($charset)); |
|
| 179 | 179 | } |
| 180 | 180 | |
| 181 | 181 | |
@@ -188,11 +188,11 @@ discard block |
||
| 188 | 188 | * @return array Description du charset (son nom est dans 'charset') |
| 189 | 189 | */ |
| 190 | 190 | function spip_mysql_get_charset($charset = [], $serveur = '', $requeter = true) { |
| 191 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 192 | - $connexion['last'] = $c = 'SHOW CHARACTER SET' |
|
| 193 | - . (!$charset ? '' : (' LIKE ' . _q($charset['charset']))); |
|
| 191 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 192 | + $connexion['last'] = $c = 'SHOW CHARACTER SET' |
|
| 193 | + . (!$charset ? '' : (' LIKE ' . _q($charset['charset']))); |
|
| 194 | 194 | |
| 195 | - return spip_mysql_fetch(mysqli_query($connexion['link'], $c), null, $serveur); |
|
| 195 | + return spip_mysql_fetch(mysqli_query($connexion['link'], $c), null, $serveur); |
|
| 196 | 196 | } |
| 197 | 197 | |
| 198 | 198 | /** |
@@ -208,80 +208,80 @@ discard block |
||
| 208 | 208 | */ |
| 209 | 209 | function spip_mysql_query($query, $serveur = '', $requeter = true) { |
| 210 | 210 | |
| 211 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 212 | - $prefixe = $connexion['prefixe']; |
|
| 213 | - $link = $connexion['link']; |
|
| 214 | - $db = $connexion['db']; |
|
| 215 | - |
|
| 216 | - $query = _mysql_traite_query($query, $db, $prefixe); |
|
| 217 | - |
|
| 218 | - // renvoyer la requete inerte si demandee |
|
| 219 | - if (!$requeter) { |
|
| 220 | - return $query; |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - if (isset($_GET['var_profile']) or (defined('_DEBUG_TRACE_QUERIES') and _DEBUG_TRACE_QUERIES)) { |
|
| 224 | - include_spip('public/tracer'); |
|
| 225 | - $t = trace_query_start(); |
|
| 226 | - } else { |
|
| 227 | - $t = 0; |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - $connexion['last'] = $query; |
|
| 231 | - $connexion['total_requetes']++; |
|
| 232 | - |
|
| 233 | - // ajouter un debug utile dans log/mysql-slow.log ? |
|
| 234 | - $debug = ''; |
|
| 235 | - if (defined('_DEBUG_SLOW_QUERIES') and _DEBUG_SLOW_QUERIES) { |
|
| 236 | - if (isset($GLOBALS['debug']['aucasou'])) { |
|
| 237 | - [, $id, , $infos] = $GLOBALS['debug']['aucasou']; |
|
| 238 | - $debug .= "BOUCLE$id @ " . ($infos[0] ?? '') . ' | '; |
|
| 239 | - } |
|
| 240 | - if (isset($_SERVER['REQUEST_URI'])) { |
|
| 241 | - $debug .= $_SERVER['REQUEST_URI']; |
|
| 242 | - } |
|
| 243 | - if (!empty($GLOBALS['ip'])) { |
|
| 244 | - $debug .= ' + ' . $GLOBALS['ip']; |
|
| 245 | - } |
|
| 246 | - $debug = ' /* ' . mysqli_real_escape_string($link, str_replace('*/', '@/', $debug)) . ' */'; |
|
| 247 | - } |
|
| 248 | - try { |
|
| 249 | - $r = mysqli_query($link, $query . $debug); |
|
| 250 | - } catch (\mysqli_sql_exception $e) { |
|
| 251 | - spip_log('mysqli_sql_exception: ' . $e->getMessage(), 'mysql.' . _LOG_DEBUG); |
|
| 252 | - $r = false; |
|
| 253 | - // TODO: utiliser l’exception ensuite plutôt que les appels à spip_mysql_errno() |
|
| 254 | - // mais il faut pour php < 8.1 forcer les exeptions via mysqli_report(). |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - //Eviter de propager le GoneAway sur les autres requetes d'un même processus PHP |
|
| 258 | - if ($e = spip_mysql_errno($serveur)) { // Log d'un Gone Away |
|
| 259 | - if ($e == 2006) { //Si Gone Away on relance une connexion vierge |
|
| 260 | - //Fermer la connexion defaillante |
|
| 261 | - mysqli_close($connexion['link']); |
|
| 262 | - unset($GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]); |
|
| 263 | - //Relancer une connexion vierge |
|
| 264 | - spip_connect($serveur); |
|
| 265 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 266 | - $link = $connexion['link']; |
|
| 267 | - //On retente au cas où |
|
| 268 | - try { |
|
| 269 | - $r = mysqli_query($link, $query . $debug); |
|
| 270 | - } catch (\mysqli_sql_exception $e) { |
|
| 271 | - spip_log('mysqli_sql_exception: ' . $e->getMessage(), 'mysql.' . _LOG_DEBUG); |
|
| 272 | - $r = false; |
|
| 273 | - // TODO: utiliser l’exception ensuite plutôt que les appels à spip_mysql_errno() |
|
| 274 | - // mais il faut pour php < 8.1 forcer les exeptions via mysqli_report(). |
|
| 275 | - } |
|
| 276 | - } |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - // Log de l'erreur eventuelle |
|
| 280 | - if ($e = spip_mysql_errno($serveur)) { |
|
| 281 | - // et du fautif |
|
| 282 | - $e .= spip_mysql_error($query, $serveur); |
|
| 283 | - } |
|
| 284 | - return $t ? trace_query_end($query, $t, $r, $e, $serveur) : $r; |
|
| 211 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 212 | + $prefixe = $connexion['prefixe']; |
|
| 213 | + $link = $connexion['link']; |
|
| 214 | + $db = $connexion['db']; |
|
| 215 | + |
|
| 216 | + $query = _mysql_traite_query($query, $db, $prefixe); |
|
| 217 | + |
|
| 218 | + // renvoyer la requete inerte si demandee |
|
| 219 | + if (!$requeter) { |
|
| 220 | + return $query; |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + if (isset($_GET['var_profile']) or (defined('_DEBUG_TRACE_QUERIES') and _DEBUG_TRACE_QUERIES)) { |
|
| 224 | + include_spip('public/tracer'); |
|
| 225 | + $t = trace_query_start(); |
|
| 226 | + } else { |
|
| 227 | + $t = 0; |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + $connexion['last'] = $query; |
|
| 231 | + $connexion['total_requetes']++; |
|
| 232 | + |
|
| 233 | + // ajouter un debug utile dans log/mysql-slow.log ? |
|
| 234 | + $debug = ''; |
|
| 235 | + if (defined('_DEBUG_SLOW_QUERIES') and _DEBUG_SLOW_QUERIES) { |
|
| 236 | + if (isset($GLOBALS['debug']['aucasou'])) { |
|
| 237 | + [, $id, , $infos] = $GLOBALS['debug']['aucasou']; |
|
| 238 | + $debug .= "BOUCLE$id @ " . ($infos[0] ?? '') . ' | '; |
|
| 239 | + } |
|
| 240 | + if (isset($_SERVER['REQUEST_URI'])) { |
|
| 241 | + $debug .= $_SERVER['REQUEST_URI']; |
|
| 242 | + } |
|
| 243 | + if (!empty($GLOBALS['ip'])) { |
|
| 244 | + $debug .= ' + ' . $GLOBALS['ip']; |
|
| 245 | + } |
|
| 246 | + $debug = ' /* ' . mysqli_real_escape_string($link, str_replace('*/', '@/', $debug)) . ' */'; |
|
| 247 | + } |
|
| 248 | + try { |
|
| 249 | + $r = mysqli_query($link, $query . $debug); |
|
| 250 | + } catch (\mysqli_sql_exception $e) { |
|
| 251 | + spip_log('mysqli_sql_exception: ' . $e->getMessage(), 'mysql.' . _LOG_DEBUG); |
|
| 252 | + $r = false; |
|
| 253 | + // TODO: utiliser l’exception ensuite plutôt que les appels à spip_mysql_errno() |
|
| 254 | + // mais il faut pour php < 8.1 forcer les exeptions via mysqli_report(). |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + //Eviter de propager le GoneAway sur les autres requetes d'un même processus PHP |
|
| 258 | + if ($e = spip_mysql_errno($serveur)) { // Log d'un Gone Away |
|
| 259 | + if ($e == 2006) { //Si Gone Away on relance une connexion vierge |
|
| 260 | + //Fermer la connexion defaillante |
|
| 261 | + mysqli_close($connexion['link']); |
|
| 262 | + unset($GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]); |
|
| 263 | + //Relancer une connexion vierge |
|
| 264 | + spip_connect($serveur); |
|
| 265 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 266 | + $link = $connexion['link']; |
|
| 267 | + //On retente au cas où |
|
| 268 | + try { |
|
| 269 | + $r = mysqli_query($link, $query . $debug); |
|
| 270 | + } catch (\mysqli_sql_exception $e) { |
|
| 271 | + spip_log('mysqli_sql_exception: ' . $e->getMessage(), 'mysql.' . _LOG_DEBUG); |
|
| 272 | + $r = false; |
|
| 273 | + // TODO: utiliser l’exception ensuite plutôt que les appels à spip_mysql_errno() |
|
| 274 | + // mais il faut pour php < 8.1 forcer les exeptions via mysqli_report(). |
|
| 275 | + } |
|
| 276 | + } |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + // Log de l'erreur eventuelle |
|
| 280 | + if ($e = spip_mysql_errno($serveur)) { |
|
| 281 | + // et du fautif |
|
| 282 | + $e .= spip_mysql_error($query, $serveur); |
|
| 283 | + } |
|
| 284 | + return $t ? trace_query_end($query, $t, $r, $e, $serveur) : $r; |
|
| 285 | 285 | } |
| 286 | 286 | |
| 287 | 287 | /** |
@@ -296,12 +296,12 @@ discard block |
||
| 296 | 296 | * - array : Tableau décrivant requête et temps d'exécution si var_profile actif pour tracer. |
| 297 | 297 | */ |
| 298 | 298 | function spip_mysql_alter($query, $serveur = '', $requeter = true) { |
| 299 | - // ici on supprime les ` entourant le nom de table pour permettre |
|
| 300 | - // la transposition du prefixe, compte tenu que les plugins ont la mauvaise habitude |
|
| 301 | - // d'utiliser ceux-ci, copie-colle de phpmyadmin |
|
| 302 | - $query = preg_replace(',^TABLE\s*`([^`]*)`,i', "TABLE \\1", $query); |
|
| 299 | + // ici on supprime les ` entourant le nom de table pour permettre |
|
| 300 | + // la transposition du prefixe, compte tenu que les plugins ont la mauvaise habitude |
|
| 301 | + // d'utiliser ceux-ci, copie-colle de phpmyadmin |
|
| 302 | + $query = preg_replace(',^TABLE\s*`([^`]*)`,i', "TABLE \\1", $query); |
|
| 303 | 303 | |
| 304 | - return spip_mysql_query('ALTER ' . $query, $serveur, $requeter); # i.e. que PG se debrouille |
|
| 304 | + return spip_mysql_query('ALTER ' . $query, $serveur, $requeter); # i.e. que PG se debrouille |
|
| 305 | 305 | } |
| 306 | 306 | |
| 307 | 307 | |
@@ -314,9 +314,9 @@ discard block |
||
| 314 | 314 | * @return bool Toujours true |
| 315 | 315 | */ |
| 316 | 316 | function spip_mysql_optimize($table, $serveur = '', $requeter = true) { |
| 317 | - spip_mysql_query('OPTIMIZE TABLE ' . $table); |
|
| 317 | + spip_mysql_query('OPTIMIZE TABLE ' . $table); |
|
| 318 | 318 | |
| 319 | - return true; |
|
| 319 | + return true; |
|
| 320 | 320 | } |
| 321 | 321 | |
| 322 | 322 | |
@@ -329,18 +329,18 @@ discard block |
||
| 329 | 329 | * @return array Tableau de l'explication |
| 330 | 330 | */ |
| 331 | 331 | function spip_mysql_explain($query, $serveur = '', $requeter = true) { |
| 332 | - if (strpos(ltrim($query), 'SELECT') !== 0) { |
|
| 333 | - return []; |
|
| 334 | - } |
|
| 335 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 336 | - $prefixe = $connexion['prefixe']; |
|
| 337 | - $link = $connexion['link']; |
|
| 338 | - $db = $connexion['db']; |
|
| 339 | - |
|
| 340 | - $query = 'EXPLAIN ' . _mysql_traite_query($query, $db, $prefixe); |
|
| 341 | - $r = mysqli_query($link, $query); |
|
| 342 | - |
|
| 343 | - return spip_mysql_fetch($r, null, $serveur); |
|
| 332 | + if (strpos(ltrim($query), 'SELECT') !== 0) { |
|
| 333 | + return []; |
|
| 334 | + } |
|
| 335 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 336 | + $prefixe = $connexion['prefixe']; |
|
| 337 | + $link = $connexion['link']; |
|
| 338 | + $db = $connexion['db']; |
|
| 339 | + |
|
| 340 | + $query = 'EXPLAIN ' . _mysql_traite_query($query, $db, $prefixe); |
|
| 341 | + $r = mysqli_query($link, $query); |
|
| 342 | + |
|
| 343 | + return spip_mysql_fetch($r, null, $serveur); |
|
| 344 | 344 | } |
| 345 | 345 | |
| 346 | 346 | |
@@ -369,35 +369,35 @@ discard block |
||
| 369 | 369 | * - array : Tableau décrivant requête et temps d'exécution si var_profile actif pour tracer. |
| 370 | 370 | */ |
| 371 | 371 | function spip_mysql_select( |
| 372 | - $select, |
|
| 373 | - $from, |
|
| 374 | - $where = '', |
|
| 375 | - $groupby = '', |
|
| 376 | - $orderby = '', |
|
| 377 | - $limit = '', |
|
| 378 | - $having = '', |
|
| 379 | - $serveur = '', |
|
| 380 | - $requeter = true |
|
| 372 | + $select, |
|
| 373 | + $from, |
|
| 374 | + $where = '', |
|
| 375 | + $groupby = '', |
|
| 376 | + $orderby = '', |
|
| 377 | + $limit = '', |
|
| 378 | + $having = '', |
|
| 379 | + $serveur = '', |
|
| 380 | + $requeter = true |
|
| 381 | 381 | ) { |
| 382 | 382 | |
| 383 | 383 | |
| 384 | - $from = (!is_array($from) ? $from : spip_mysql_select_as($from)); |
|
| 385 | - $query = |
|
| 386 | - calculer_mysql_expression('SELECT', $select, ', ') |
|
| 387 | - . calculer_mysql_expression('FROM', $from, ', ') |
|
| 388 | - . calculer_mysql_expression('WHERE', $where) |
|
| 389 | - . calculer_mysql_expression('GROUP BY', $groupby, ',') |
|
| 390 | - . calculer_mysql_expression('HAVING', $having) |
|
| 391 | - . ($orderby ? ("\nORDER BY " . spip_mysql_order($orderby)) : '') |
|
| 392 | - . ($limit ? "\nLIMIT $limit" : ''); |
|
| 384 | + $from = (!is_array($from) ? $from : spip_mysql_select_as($from)); |
|
| 385 | + $query = |
|
| 386 | + calculer_mysql_expression('SELECT', $select, ', ') |
|
| 387 | + . calculer_mysql_expression('FROM', $from, ', ') |
|
| 388 | + . calculer_mysql_expression('WHERE', $where) |
|
| 389 | + . calculer_mysql_expression('GROUP BY', $groupby, ',') |
|
| 390 | + . calculer_mysql_expression('HAVING', $having) |
|
| 391 | + . ($orderby ? ("\nORDER BY " . spip_mysql_order($orderby)) : '') |
|
| 392 | + . ($limit ? "\nLIMIT $limit" : ''); |
|
| 393 | 393 | |
| 394 | - // renvoyer la requete inerte si demandee |
|
| 395 | - if ($requeter === false) { |
|
| 396 | - return $query; |
|
| 397 | - } |
|
| 398 | - $r = spip_mysql_query($query, $serveur, $requeter); |
|
| 394 | + // renvoyer la requete inerte si demandee |
|
| 395 | + if ($requeter === false) { |
|
| 396 | + return $query; |
|
| 397 | + } |
|
| 398 | + $r = spip_mysql_query($query, $serveur, $requeter); |
|
| 399 | 399 | |
| 400 | - return $r ?: $query; |
|
| 400 | + return $r ?: $query; |
|
| 401 | 401 | } |
| 402 | 402 | |
| 403 | 403 | |
@@ -414,7 +414,7 @@ discard block |
||
| 414 | 414 | * @return string texte du orderby préparé |
| 415 | 415 | */ |
| 416 | 416 | function spip_mysql_order($orderby) { |
| 417 | - return (is_array($orderby)) ? join(', ', $orderby) : $orderby; |
|
| 417 | + return (is_array($orderby)) ? join(', ', $orderby) : $orderby; |
|
| 418 | 418 | } |
| 419 | 419 | |
| 420 | 420 | |
@@ -437,26 +437,26 @@ discard block |
||
| 437 | 437 | * Contrainte pour clause WHERE |
| 438 | 438 | */ |
| 439 | 439 | function calculer_mysql_where($v) { |
| 440 | - if (!is_array($v)) { |
|
| 441 | - return $v; |
|
| 442 | - } |
|
| 443 | - |
|
| 444 | - $op = array_shift($v); |
|
| 445 | - if (!($n = count($v))) { |
|
| 446 | - return $op; |
|
| 447 | - } else { |
|
| 448 | - $arg = calculer_mysql_where(array_shift($v)); |
|
| 449 | - if ($n == 1) { |
|
| 450 | - return "$op($arg)"; |
|
| 451 | - } else { |
|
| 452 | - $arg2 = calculer_mysql_where(array_shift($v)); |
|
| 453 | - if ($n == 2) { |
|
| 454 | - return "($arg $op $arg2)"; |
|
| 455 | - } else { |
|
| 456 | - return "($arg $op ($arg2) : $v[0])"; |
|
| 457 | - } |
|
| 458 | - } |
|
| 459 | - } |
|
| 440 | + if (!is_array($v)) { |
|
| 441 | + return $v; |
|
| 442 | + } |
|
| 443 | + |
|
| 444 | + $op = array_shift($v); |
|
| 445 | + if (!($n = count($v))) { |
|
| 446 | + return $op; |
|
| 447 | + } else { |
|
| 448 | + $arg = calculer_mysql_where(array_shift($v)); |
|
| 449 | + if ($n == 1) { |
|
| 450 | + return "$op($arg)"; |
|
| 451 | + } else { |
|
| 452 | + $arg2 = calculer_mysql_where(array_shift($v)); |
|
| 453 | + if ($n == 2) { |
|
| 454 | + return "($arg $op $arg2)"; |
|
| 455 | + } else { |
|
| 456 | + return "($arg $op ($arg2) : $v[0])"; |
|
| 457 | + } |
|
| 458 | + } |
|
| 459 | + } |
|
| 460 | 460 | } |
| 461 | 461 | |
| 462 | 462 | /** |
@@ -471,21 +471,21 @@ discard block |
||
| 471 | 471 | * @return string texte de l'expression, une partie donc, du texte la requête. |
| 472 | 472 | */ |
| 473 | 473 | function calculer_mysql_expression($expression, $v, $join = 'AND') { |
| 474 | - if (empty($v)) { |
|
| 475 | - return ''; |
|
| 476 | - } |
|
| 477 | - |
|
| 478 | - $exp = "\n$expression "; |
|
| 479 | - |
|
| 480 | - if (!is_array($v)) { |
|
| 481 | - return $exp . $v; |
|
| 482 | - } else { |
|
| 483 | - if (strtoupper($join) === 'AND') { |
|
| 484 | - return $exp . join("\n\t$join ", array_map('calculer_mysql_where', $v)); |
|
| 485 | - } else { |
|
| 486 | - return $exp . join($join, $v); |
|
| 487 | - } |
|
| 488 | - } |
|
| 474 | + if (empty($v)) { |
|
| 475 | + return ''; |
|
| 476 | + } |
|
| 477 | + |
|
| 478 | + $exp = "\n$expression "; |
|
| 479 | + |
|
| 480 | + if (!is_array($v)) { |
|
| 481 | + return $exp . $v; |
|
| 482 | + } else { |
|
| 483 | + if (strtoupper($join) === 'AND') { |
|
| 484 | + return $exp . join("\n\t$join ", array_map('calculer_mysql_where', $v)); |
|
| 485 | + } else { |
|
| 486 | + return $exp . join($join, $v); |
|
| 487 | + } |
|
| 488 | + } |
|
| 489 | 489 | } |
| 490 | 490 | |
| 491 | 491 | |
@@ -496,26 +496,26 @@ discard block |
||
| 496 | 496 | * @return string Sélection de colonnes pour une clause SELECT |
| 497 | 497 | */ |
| 498 | 498 | function spip_mysql_select_as($args) { |
| 499 | - $res = ''; |
|
| 500 | - foreach ($args as $k => $v) { |
|
| 501 | - if (substr($k, -1) == '@') { |
|
| 502 | - // c'est une jointure qui se refere au from precedent |
|
| 503 | - // pas de virgule |
|
| 504 | - $res .= ' ' . $v; |
|
| 505 | - } else { |
|
| 506 | - if (!is_numeric($k)) { |
|
| 507 | - $p = strpos($v, ' '); |
|
| 508 | - if ($p) { |
|
| 509 | - $v = substr($v, 0, $p) . " AS `$k`" . substr($v, $p); |
|
| 510 | - } else { |
|
| 511 | - $v .= " AS `$k`"; |
|
| 512 | - } |
|
| 513 | - } |
|
| 514 | - $res .= ', ' . $v; |
|
| 515 | - } |
|
| 516 | - } |
|
| 517 | - |
|
| 518 | - return substr($res, 2); |
|
| 499 | + $res = ''; |
|
| 500 | + foreach ($args as $k => $v) { |
|
| 501 | + if (substr($k, -1) == '@') { |
|
| 502 | + // c'est une jointure qui se refere au from precedent |
|
| 503 | + // pas de virgule |
|
| 504 | + $res .= ' ' . $v; |
|
| 505 | + } else { |
|
| 506 | + if (!is_numeric($k)) { |
|
| 507 | + $p = strpos($v, ' '); |
|
| 508 | + if ($p) { |
|
| 509 | + $v = substr($v, 0, $p) . " AS `$k`" . substr($v, $p); |
|
| 510 | + } else { |
|
| 511 | + $v .= " AS `$k`"; |
|
| 512 | + } |
|
| 513 | + } |
|
| 514 | + $res .= ', ' . $v; |
|
| 515 | + } |
|
| 516 | + } |
|
| 517 | + |
|
| 518 | + return substr($res, 2); |
|
| 519 | 519 | } |
| 520 | 520 | |
| 521 | 521 | |
@@ -540,58 +540,58 @@ discard block |
||
| 540 | 540 | */ |
| 541 | 541 | function _mysql_traite_query($query, $db = '', $prefixe = '', $echappe_textes = true) { |
| 542 | 542 | |
| 543 | - if ($GLOBALS['mysql_rappel_nom_base'] and $db) { |
|
| 544 | - $pref = '`' . $db . '`.'; |
|
| 545 | - } else { |
|
| 546 | - $pref = ''; |
|
| 547 | - } |
|
| 548 | - |
|
| 549 | - if ($prefixe) { |
|
| 550 | - $pref .= $prefixe . '_'; |
|
| 551 | - } |
|
| 552 | - |
|
| 553 | - if (!preg_match('/\s(SET|VALUES|WHERE|DATABASE)\s/i', $query, $regs)) { |
|
| 554 | - $suite = ''; |
|
| 555 | - } else { |
|
| 556 | - $suite = strstr($query, (string) $regs[0]); |
|
| 557 | - $query = substr($query, 0, -strlen($suite)); |
|
| 558 | - // propager le prefixe en cas de requete imbriquee |
|
| 559 | - // il faut alors echapper les chaine avant de le faire, pour ne pas risquer de |
|
| 560 | - // modifier une requete qui est en fait juste du texte dans un champ |
|
| 561 | - if (stripos($suite, 'SELECT') !== false) { |
|
| 562 | - if ($echappe_textes) { |
|
| 563 | - [$suite_echap, $textes] = query_echappe_textes($suite); |
|
| 564 | - } |
|
| 565 | - else { |
|
| 566 | - $suite_echap = $suite; |
|
| 567 | - } |
|
| 568 | - if (preg_match('/^(.*?)([(]\s*SELECT\b.*)$/si', $suite_echap, $r)) { |
|
| 569 | - $suite_echap = $r[1] . _mysql_traite_query($r[2], $db, $prefixe, false); |
|
| 570 | - if ($echappe_textes) { |
|
| 571 | - $suite = query_reinjecte_textes($suite_echap, $textes); |
|
| 572 | - } |
|
| 573 | - else { |
|
| 574 | - $suite = $suite_echap; |
|
| 575 | - } |
|
| 576 | - } |
|
| 577 | - } |
|
| 578 | - } |
|
| 579 | - $r = preg_replace(_SQL_PREFIXE_TABLE_MYSQL, '\1' . $pref, $query) . $suite; |
|
| 580 | - |
|
| 581 | - // en option, remplacer les emoji (que mysql ne sait pas gérer) en 💩 |
|
| 582 | - // remplacer les emoji (que mysql ne sait pas gérer) en 💩 |
|
| 583 | - if ( |
|
| 584 | - defined('_MYSQL_NOPLANES') |
|
| 585 | - and _MYSQL_NOPLANES |
|
| 586 | - and !empty($GLOBALS['meta']['charset_sql_connexion']) |
|
| 587 | - and $GLOBALS['meta']['charset_sql_connexion'] == 'utf8' |
|
| 588 | - ) { |
|
| 589 | - include_spip('inc/charsets'); |
|
| 590 | - $r = utf8_noplanes($r); |
|
| 591 | - } |
|
| 592 | - |
|
| 593 | - #spip_log("_mysql_traite_query: " . substr($r,0, 50) . ".... $db, $prefixe", _LOG_DEBUG); |
|
| 594 | - return $r; |
|
| 543 | + if ($GLOBALS['mysql_rappel_nom_base'] and $db) { |
|
| 544 | + $pref = '`' . $db . '`.'; |
|
| 545 | + } else { |
|
| 546 | + $pref = ''; |
|
| 547 | + } |
|
| 548 | + |
|
| 549 | + if ($prefixe) { |
|
| 550 | + $pref .= $prefixe . '_'; |
|
| 551 | + } |
|
| 552 | + |
|
| 553 | + if (!preg_match('/\s(SET|VALUES|WHERE|DATABASE)\s/i', $query, $regs)) { |
|
| 554 | + $suite = ''; |
|
| 555 | + } else { |
|
| 556 | + $suite = strstr($query, (string) $regs[0]); |
|
| 557 | + $query = substr($query, 0, -strlen($suite)); |
|
| 558 | + // propager le prefixe en cas de requete imbriquee |
|
| 559 | + // il faut alors echapper les chaine avant de le faire, pour ne pas risquer de |
|
| 560 | + // modifier une requete qui est en fait juste du texte dans un champ |
|
| 561 | + if (stripos($suite, 'SELECT') !== false) { |
|
| 562 | + if ($echappe_textes) { |
|
| 563 | + [$suite_echap, $textes] = query_echappe_textes($suite); |
|
| 564 | + } |
|
| 565 | + else { |
|
| 566 | + $suite_echap = $suite; |
|
| 567 | + } |
|
| 568 | + if (preg_match('/^(.*?)([(]\s*SELECT\b.*)$/si', $suite_echap, $r)) { |
|
| 569 | + $suite_echap = $r[1] . _mysql_traite_query($r[2], $db, $prefixe, false); |
|
| 570 | + if ($echappe_textes) { |
|
| 571 | + $suite = query_reinjecte_textes($suite_echap, $textes); |
|
| 572 | + } |
|
| 573 | + else { |
|
| 574 | + $suite = $suite_echap; |
|
| 575 | + } |
|
| 576 | + } |
|
| 577 | + } |
|
| 578 | + } |
|
| 579 | + $r = preg_replace(_SQL_PREFIXE_TABLE_MYSQL, '\1' . $pref, $query) . $suite; |
|
| 580 | + |
|
| 581 | + // en option, remplacer les emoji (que mysql ne sait pas gérer) en 💩 |
|
| 582 | + // remplacer les emoji (que mysql ne sait pas gérer) en 💩 |
|
| 583 | + if ( |
|
| 584 | + defined('_MYSQL_NOPLANES') |
|
| 585 | + and _MYSQL_NOPLANES |
|
| 586 | + and !empty($GLOBALS['meta']['charset_sql_connexion']) |
|
| 587 | + and $GLOBALS['meta']['charset_sql_connexion'] == 'utf8' |
|
| 588 | + ) { |
|
| 589 | + include_spip('inc/charsets'); |
|
| 590 | + $r = utf8_noplanes($r); |
|
| 591 | + } |
|
| 592 | + |
|
| 593 | + #spip_log("_mysql_traite_query: " . substr($r,0, 50) . ".... $db, $prefixe", _LOG_DEBUG); |
|
| 594 | + return $r; |
|
| 595 | 595 | } |
| 596 | 596 | |
| 597 | 597 | /** |
@@ -609,17 +609,17 @@ discard block |
||
| 609 | 609 | * - False en cas d'erreur. |
| 610 | 610 | **/ |
| 611 | 611 | function spip_mysql_selectdb($db, $serveur = '', $requeter = true) { |
| 612 | - $link = _mysql_link($serveur); |
|
| 613 | - try { |
|
| 614 | - $ok = mysqli_select_db($link, $db); |
|
| 615 | - } catch (\mysqli_sql_exception $e) { |
|
| 616 | - $ok = false; |
|
| 617 | - } |
|
| 618 | - if (!$ok) { |
|
| 619 | - spip_log('Echec mysqli_selectdb. Erreur : ' . mysqli_error($link), 'mysql.' . _LOG_CRITIQUE); |
|
| 620 | - } |
|
| 621 | - |
|
| 622 | - return $ok; |
|
| 612 | + $link = _mysql_link($serveur); |
|
| 613 | + try { |
|
| 614 | + $ok = mysqli_select_db($link, $db); |
|
| 615 | + } catch (\mysqli_sql_exception $e) { |
|
| 616 | + $ok = false; |
|
| 617 | + } |
|
| 618 | + if (!$ok) { |
|
| 619 | + spip_log('Echec mysqli_selectdb. Erreur : ' . mysqli_error($link), 'mysql.' . _LOG_CRITIQUE); |
|
| 620 | + } |
|
| 621 | + |
|
| 622 | + return $ok; |
|
| 623 | 623 | } |
| 624 | 624 | |
| 625 | 625 | |
@@ -640,14 +640,14 @@ discard block |
||
| 640 | 640 | * Liste de noms de bases de données |
| 641 | 641 | **/ |
| 642 | 642 | function spip_mysql_listdbs($serveur = '', $requeter = true) { |
| 643 | - $dbs = []; |
|
| 644 | - if ($res = spip_mysql_query('SHOW DATABASES', $serveur)) { |
|
| 645 | - while ($row = mysqli_fetch_assoc($res)) { |
|
| 646 | - $dbs[] = $row['Database']; |
|
| 647 | - } |
|
| 648 | - } |
|
| 649 | - |
|
| 650 | - return $dbs; |
|
| 643 | + $dbs = []; |
|
| 644 | + if ($res = spip_mysql_query('SHOW DATABASES', $serveur)) { |
|
| 645 | + while ($row = mysqli_fetch_assoc($res)) { |
|
| 646 | + $dbs[] = $row['Database']; |
|
| 647 | + } |
|
| 648 | + } |
|
| 649 | + |
|
| 650 | + return $dbs; |
|
| 651 | 651 | } |
| 652 | 652 | |
| 653 | 653 | |
@@ -670,73 +670,73 @@ discard block |
||
| 670 | 670 | * - true si la requête réussie, false sinon. |
| 671 | 671 | */ |
| 672 | 672 | function spip_mysql_create( |
| 673 | - $nom, |
|
| 674 | - $champs, |
|
| 675 | - $cles, |
|
| 676 | - $autoinc = false, |
|
| 677 | - $temporary = false, |
|
| 678 | - $serveur = '', |
|
| 679 | - $requeter = true |
|
| 673 | + $nom, |
|
| 674 | + $champs, |
|
| 675 | + $cles, |
|
| 676 | + $autoinc = false, |
|
| 677 | + $temporary = false, |
|
| 678 | + $serveur = '', |
|
| 679 | + $requeter = true |
|
| 680 | 680 | ) { |
| 681 | 681 | |
| 682 | - $query = ''; |
|
| 683 | - $keys = ''; |
|
| 684 | - $s = ''; |
|
| 685 | - $p = ''; |
|
| 686 | - |
|
| 687 | - // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 688 | - // sans les renseigner (laisse le compilo recuperer la description) |
|
| 689 | - if (!is_array($champs) || !is_array($cles)) { |
|
| 690 | - return; |
|
| 691 | - } |
|
| 692 | - |
|
| 693 | - $res = spip_mysql_query('SELECT version() as v', $serveur); |
|
| 694 | - if (($row = mysqli_fetch_array($res)) && (version_compare($row['v'], '5.0', '>='))) { |
|
| 695 | - spip_mysql_query("SET sql_mode=''", $serveur); |
|
| 696 | - } |
|
| 697 | - |
|
| 698 | - foreach ($cles as $k => $v) { |
|
| 699 | - $keys .= "$s\n\t\t$k ($v)"; |
|
| 700 | - if ($k == 'PRIMARY KEY') { |
|
| 701 | - $p = $v; |
|
| 702 | - } |
|
| 703 | - $s = ','; |
|
| 704 | - } |
|
| 705 | - $s = ''; |
|
| 706 | - |
|
| 707 | - $character_set = ''; |
|
| 708 | - if (@$GLOBALS['meta']['charset_sql_base']) { |
|
| 709 | - $character_set .= ' CHARACTER SET ' . $GLOBALS['meta']['charset_sql_base']; |
|
| 710 | - } |
|
| 711 | - if (@$GLOBALS['meta']['charset_collation_sql_base']) { |
|
| 712 | - $character_set .= ' COLLATE ' . $GLOBALS['meta']['charset_collation_sql_base']; |
|
| 713 | - } |
|
| 714 | - |
|
| 715 | - foreach ($champs as $k => $v) { |
|
| 716 | - $v = _mysql_remplacements_definitions_table($v); |
|
| 717 | - if (preg_match(',([a-z]*\s*(\(\s*[0-9]*\s*\))?(\s*binary)?),i', $v, $defs)) { |
|
| 718 | - if ( |
|
| 719 | - preg_match(',(char|text),i', $defs[1]) |
|
| 720 | - and !preg_match(',(binary|CHARACTER|COLLATE),i', $v) |
|
| 721 | - ) { |
|
| 722 | - $v = $defs[1] . $character_set . ' ' . substr($v, strlen($defs[1])); |
|
| 723 | - } |
|
| 724 | - } |
|
| 725 | - |
|
| 726 | - $query .= "$s\n\t\t$k $v" |
|
| 727 | - . (($autoinc && ($p == $k) && preg_match(',\b(big|small|medium)?int\b,i', $v)) |
|
| 728 | - ? ' auto_increment' |
|
| 729 | - : '' |
|
| 730 | - ); |
|
| 731 | - $s = ','; |
|
| 732 | - } |
|
| 733 | - $temporary = $temporary ? 'TEMPORARY' : ''; |
|
| 734 | - $q = "CREATE $temporary TABLE IF NOT EXISTS $nom ($query" . ($keys ? ",$keys" : '') . ')' |
|
| 735 | - . (defined('_MYSQL_ENGINE') ? ' ENGINE=' . _MYSQL_ENGINE : '') |
|
| 736 | - . ($character_set ? " DEFAULT $character_set" : '') |
|
| 737 | - . "\n"; |
|
| 738 | - |
|
| 739 | - return spip_mysql_query($q, $serveur); |
|
| 682 | + $query = ''; |
|
| 683 | + $keys = ''; |
|
| 684 | + $s = ''; |
|
| 685 | + $p = ''; |
|
| 686 | + |
|
| 687 | + // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 688 | + // sans les renseigner (laisse le compilo recuperer la description) |
|
| 689 | + if (!is_array($champs) || !is_array($cles)) { |
|
| 690 | + return; |
|
| 691 | + } |
|
| 692 | + |
|
| 693 | + $res = spip_mysql_query('SELECT version() as v', $serveur); |
|
| 694 | + if (($row = mysqli_fetch_array($res)) && (version_compare($row['v'], '5.0', '>='))) { |
|
| 695 | + spip_mysql_query("SET sql_mode=''", $serveur); |
|
| 696 | + } |
|
| 697 | + |
|
| 698 | + foreach ($cles as $k => $v) { |
|
| 699 | + $keys .= "$s\n\t\t$k ($v)"; |
|
| 700 | + if ($k == 'PRIMARY KEY') { |
|
| 701 | + $p = $v; |
|
| 702 | + } |
|
| 703 | + $s = ','; |
|
| 704 | + } |
|
| 705 | + $s = ''; |
|
| 706 | + |
|
| 707 | + $character_set = ''; |
|
| 708 | + if (@$GLOBALS['meta']['charset_sql_base']) { |
|
| 709 | + $character_set .= ' CHARACTER SET ' . $GLOBALS['meta']['charset_sql_base']; |
|
| 710 | + } |
|
| 711 | + if (@$GLOBALS['meta']['charset_collation_sql_base']) { |
|
| 712 | + $character_set .= ' COLLATE ' . $GLOBALS['meta']['charset_collation_sql_base']; |
|
| 713 | + } |
|
| 714 | + |
|
| 715 | + foreach ($champs as $k => $v) { |
|
| 716 | + $v = _mysql_remplacements_definitions_table($v); |
|
| 717 | + if (preg_match(',([a-z]*\s*(\(\s*[0-9]*\s*\))?(\s*binary)?),i', $v, $defs)) { |
|
| 718 | + if ( |
|
| 719 | + preg_match(',(char|text),i', $defs[1]) |
|
| 720 | + and !preg_match(',(binary|CHARACTER|COLLATE),i', $v) |
|
| 721 | + ) { |
|
| 722 | + $v = $defs[1] . $character_set . ' ' . substr($v, strlen($defs[1])); |
|
| 723 | + } |
|
| 724 | + } |
|
| 725 | + |
|
| 726 | + $query .= "$s\n\t\t$k $v" |
|
| 727 | + . (($autoinc && ($p == $k) && preg_match(',\b(big|small|medium)?int\b,i', $v)) |
|
| 728 | + ? ' auto_increment' |
|
| 729 | + : '' |
|
| 730 | + ); |
|
| 731 | + $s = ','; |
|
| 732 | + } |
|
| 733 | + $temporary = $temporary ? 'TEMPORARY' : ''; |
|
| 734 | + $q = "CREATE $temporary TABLE IF NOT EXISTS $nom ($query" . ($keys ? ",$keys" : '') . ')' |
|
| 735 | + . (defined('_MYSQL_ENGINE') ? ' ENGINE=' . _MYSQL_ENGINE : '') |
|
| 736 | + . ($character_set ? " DEFAULT $character_set" : '') |
|
| 737 | + . "\n"; |
|
| 738 | + |
|
| 739 | + return spip_mysql_query($q, $serveur); |
|
| 740 | 740 | } |
| 741 | 741 | |
| 742 | 742 | |
@@ -749,25 +749,25 @@ discard block |
||
| 749 | 749 | * Définition SQL adaptée pour MySQL d'un champ de table |
| 750 | 750 | */ |
| 751 | 751 | function _mysql_remplacements_definitions_table($query) { |
| 752 | - // quelques remplacements |
|
| 753 | - $num = '(\s*\([0-9]*\))?'; |
|
| 754 | - $enum = '(\s*\([^\)]*\))?'; |
|
| 755 | - |
|
| 756 | - $remplace = [ |
|
| 757 | - '/VARCHAR(\s*[^\s\(])/is' => 'VARCHAR(255)\\1', |
|
| 758 | - '/^TIMESTAMP($| NULL DEFAULT NULL)/is' => 'TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP', |
|
| 759 | - ]; |
|
| 760 | - |
|
| 761 | - if (is_string($query)) { |
|
| 762 | - $query = preg_replace(array_keys($remplace), $remplace, $query); |
|
| 763 | - } elseif (is_array($query)) { |
|
| 764 | - $keys = array_keys($remplace); |
|
| 765 | - foreach ($query as $k => $q) { |
|
| 766 | - $query[$k] = preg_replace($keys, $remplace, $q); |
|
| 767 | - } |
|
| 768 | - } |
|
| 769 | - |
|
| 770 | - return $query; |
|
| 752 | + // quelques remplacements |
|
| 753 | + $num = '(\s*\([0-9]*\))?'; |
|
| 754 | + $enum = '(\s*\([^\)]*\))?'; |
|
| 755 | + |
|
| 756 | + $remplace = [ |
|
| 757 | + '/VARCHAR(\s*[^\s\(])/is' => 'VARCHAR(255)\\1', |
|
| 758 | + '/^TIMESTAMP($| NULL DEFAULT NULL)/is' => 'TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP', |
|
| 759 | + ]; |
|
| 760 | + |
|
| 761 | + if (is_string($query)) { |
|
| 762 | + $query = preg_replace(array_keys($remplace), $remplace, $query); |
|
| 763 | + } elseif (is_array($query)) { |
|
| 764 | + $keys = array_keys($remplace); |
|
| 765 | + foreach ($query as $k => $q) { |
|
| 766 | + $query[$k] = preg_replace($keys, $remplace, $q); |
|
| 767 | + } |
|
| 768 | + } |
|
| 769 | + |
|
| 770 | + return $query; |
|
| 771 | 771 | } |
| 772 | 772 | |
| 773 | 773 | |
@@ -780,7 +780,7 @@ discard block |
||
| 780 | 780 | * @return bool true si la base est créee. |
| 781 | 781 | **/ |
| 782 | 782 | function spip_mysql_create_base($nom, $serveur = '', $requeter = true) { |
| 783 | - return spip_mysql_query("CREATE DATABASE `$nom`", $serveur, $requeter); |
|
| 783 | + return spip_mysql_query("CREATE DATABASE `$nom`", $serveur, $requeter); |
|
| 784 | 784 | } |
| 785 | 785 | |
| 786 | 786 | |
@@ -801,19 +801,19 @@ discard block |
||
| 801 | 801 | * - string texte de la requête si $requeter vaut false |
| 802 | 802 | */ |
| 803 | 803 | function spip_mysql_create_view($nom, $query_select, $serveur = '', $requeter = true) { |
| 804 | - if (!$query_select) { |
|
| 805 | - return false; |
|
| 806 | - } |
|
| 807 | - // vue deja presente |
|
| 808 | - if (sql_showtable($nom, false, $serveur)) { |
|
| 809 | - spip_log("Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", _LOG_ERREUR); |
|
| 804 | + if (!$query_select) { |
|
| 805 | + return false; |
|
| 806 | + } |
|
| 807 | + // vue deja presente |
|
| 808 | + if (sql_showtable($nom, false, $serveur)) { |
|
| 809 | + spip_log("Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", _LOG_ERREUR); |
|
| 810 | 810 | |
| 811 | - return false; |
|
| 812 | - } |
|
| 811 | + return false; |
|
| 812 | + } |
|
| 813 | 813 | |
| 814 | - $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 814 | + $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 815 | 815 | |
| 816 | - return spip_mysql_query($query, $serveur, $requeter); |
|
| 816 | + return spip_mysql_query($query, $serveur, $requeter); |
|
| 817 | 817 | } |
| 818 | 818 | |
| 819 | 819 | |
@@ -829,11 +829,11 @@ discard block |
||
| 829 | 829 | * - true si la requête a réussie, false sinon |
| 830 | 830 | */ |
| 831 | 831 | function spip_mysql_drop_table($table, $exist = '', $serveur = '', $requeter = true) { |
| 832 | - if ($exist) { |
|
| 833 | - $exist = ' IF EXISTS'; |
|
| 834 | - } |
|
| 832 | + if ($exist) { |
|
| 833 | + $exist = ' IF EXISTS'; |
|
| 834 | + } |
|
| 835 | 835 | |
| 836 | - return spip_mysql_query("DROP TABLE$exist $table", $serveur, $requeter); |
|
| 836 | + return spip_mysql_query("DROP TABLE$exist $table", $serveur, $requeter); |
|
| 837 | 837 | } |
| 838 | 838 | |
| 839 | 839 | /** |
@@ -848,11 +848,11 @@ discard block |
||
| 848 | 848 | * - true si la requête a réussie, false sinon |
| 849 | 849 | */ |
| 850 | 850 | function spip_mysql_drop_view($view, $exist = '', $serveur = '', $requeter = true) { |
| 851 | - if ($exist) { |
|
| 852 | - $exist = ' IF EXISTS'; |
|
| 853 | - } |
|
| 851 | + if ($exist) { |
|
| 852 | + $exist = ' IF EXISTS'; |
|
| 853 | + } |
|
| 854 | 854 | |
| 855 | - return spip_mysql_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 855 | + return spip_mysql_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 856 | 856 | } |
| 857 | 857 | |
| 858 | 858 | /** |
@@ -869,7 +869,7 @@ discard block |
||
| 869 | 869 | * Ressource à utiliser avec sql_fetch() |
| 870 | 870 | **/ |
| 871 | 871 | function spip_mysql_showbase($match, $serveur = '', $requeter = true) { |
| 872 | - return spip_mysql_query('SHOW TABLES LIKE ' . _q($match), $serveur, $requeter); |
|
| 872 | + return spip_mysql_query('SHOW TABLES LIKE ' . _q($match), $serveur, $requeter); |
|
| 873 | 873 | } |
| 874 | 874 | |
| 875 | 875 | /** |
@@ -885,18 +885,18 @@ discard block |
||
| 885 | 885 | * - true si la requête a réussie, false sinon |
| 886 | 886 | */ |
| 887 | 887 | function spip_mysql_repair($table, $serveur = '', $requeter = true) { |
| 888 | - $table_status = spip_mysql_fetch(spip_mysql_query('SHOW TABLE STATUS WHERE Name = ' . _q($table), $serveur, true)); |
|
| 889 | - $engine = $table_status['Engine']; |
|
| 890 | - if ($engine == 'InnoDB') { |
|
| 891 | - if (spip_mysql_alter("TABLE $table ENGINE = InnoDB", $serveur, $requeter)) { |
|
| 892 | - return [' OK ']; |
|
| 893 | - } |
|
| 894 | - } elseif ($engine == 'MyISAM') { |
|
| 895 | - return spip_mysql_query("REPAIR TABLE `$table`", $serveur, $requeter); |
|
| 896 | - } else { |
|
| 897 | - spip_log("spip_mysql_repair impossible pour la table $table engine $engine", 'mysql.' . _LOG_DEBUG); |
|
| 898 | - } |
|
| 899 | - return false; |
|
| 888 | + $table_status = spip_mysql_fetch(spip_mysql_query('SHOW TABLE STATUS WHERE Name = ' . _q($table), $serveur, true)); |
|
| 889 | + $engine = $table_status['Engine']; |
|
| 890 | + if ($engine == 'InnoDB') { |
|
| 891 | + if (spip_mysql_alter("TABLE $table ENGINE = InnoDB", $serveur, $requeter)) { |
|
| 892 | + return [' OK ']; |
|
| 893 | + } |
|
| 894 | + } elseif ($engine == 'MyISAM') { |
|
| 895 | + return spip_mysql_query("REPAIR TABLE `$table`", $serveur, $requeter); |
|
| 896 | + } else { |
|
| 897 | + spip_log("spip_mysql_repair impossible pour la table $table engine $engine", 'mysql.' . _LOG_DEBUG); |
|
| 898 | + } |
|
| 899 | + return false; |
|
| 900 | 900 | } |
| 901 | 901 | |
| 902 | 902 | /** |
@@ -914,12 +914,12 @@ discard block |
||
| 914 | 914 | * - string : requete sql, si $requeter = true |
| 915 | 915 | **/ |
| 916 | 916 | function spip_mysql_table_exists(string $table, $serveur = '', $requeter = true) { |
| 917 | - $r = spip_mysql_query('SHOW TABLES LIKE ' . _q($table), $serveur, $requeter); |
|
| 918 | - if (!$requeter) { |
|
| 919 | - return $r; |
|
| 920 | - } |
|
| 921 | - $res = spip_mysql_fetch($r); |
|
| 922 | - return (bool) $res; |
|
| 917 | + $r = spip_mysql_query('SHOW TABLES LIKE ' . _q($table), $serveur, $requeter); |
|
| 918 | + if (!$requeter) { |
|
| 919 | + return $r; |
|
| 920 | + } |
|
| 921 | + $res = spip_mysql_fetch($r); |
|
| 922 | + return (bool) $res; |
|
| 923 | 923 | } |
| 924 | 924 | |
| 925 | 925 | define('_MYSQL_RE_SHOW_TABLE', '/^[^(),]*\(((?:[^()]*\((?:[^()]*\([^()]*\))?[^()]*\)[^()]*)*[^()]*)\)[^()]*$/'); |
@@ -942,86 +942,86 @@ discard block |
||
| 942 | 942 | * - array description de la table sinon |
| 943 | 943 | */ |
| 944 | 944 | function spip_mysql_showtable($nom_table, $serveur = '', $requeter = true) { |
| 945 | - $s = spip_mysql_query("SHOW CREATE TABLE `$nom_table`", $serveur, $requeter); |
|
| 946 | - if (!$s) { |
|
| 947 | - return ''; |
|
| 948 | - } |
|
| 949 | - if (!$requeter) { |
|
| 950 | - return $s; |
|
| 951 | - } |
|
| 952 | - |
|
| 953 | - [, $a] = mysqli_fetch_array($s, MYSQLI_NUM); |
|
| 954 | - if (preg_match(_MYSQL_RE_SHOW_TABLE, $a, $r)) { |
|
| 955 | - $desc = $r[1]; |
|
| 956 | - // extraction d'une KEY éventuelle en prenant garde de ne pas |
|
| 957 | - // relever un champ dont le nom contient KEY (ex. ID_WHISKEY) |
|
| 958 | - if (preg_match('/^(.*?),([^,]*\sKEY[ (].*)$/s', $desc, $r)) { |
|
| 959 | - $namedkeys = $r[2]; |
|
| 960 | - $desc = $r[1]; |
|
| 961 | - } else { |
|
| 962 | - $namedkeys = ''; |
|
| 963 | - } |
|
| 964 | - |
|
| 965 | - $fields = []; |
|
| 966 | - foreach (preg_split('/,\s*`/', $desc) as $v) { |
|
| 967 | - preg_match('/^\s*`?([^`]*)`\s*(.*)/', $v, $r); |
|
| 968 | - $fields[strtolower($r[1])] = $r[2]; |
|
| 969 | - } |
|
| 970 | - $keys = []; |
|
| 971 | - |
|
| 972 | - foreach (preg_split('/\)\s*(,|$)/', $namedkeys) as $v) { |
|
| 973 | - if (preg_match('/^\s*([^(]*)\(([^(]*(\(\d+\))?)$/', $v, $r)) { |
|
| 974 | - $k = str_replace('`', '', trim($r[1])); |
|
| 975 | - $t = strtolower(str_replace('`', '', $r[2])); |
|
| 976 | - if ($k && !isset($keys[$k])) { |
|
| 977 | - $keys[$k] = $t; |
|
| 978 | - } else { |
|
| 979 | - $keys[] = $t; |
|
| 980 | - } |
|
| 981 | - } |
|
| 982 | - } |
|
| 983 | - spip_mysql_free($s); |
|
| 984 | - |
|
| 985 | - return ['field' => $fields, 'key' => $keys]; |
|
| 986 | - } |
|
| 987 | - |
|
| 988 | - $res = spip_mysql_query("SHOW COLUMNS FROM `$nom_table`", $serveur); |
|
| 989 | - if ($res) { |
|
| 990 | - $nfields = []; |
|
| 991 | - $nkeys = []; |
|
| 992 | - while ($val = spip_mysql_fetch($res)) { |
|
| 993 | - $nfields[$val['Field']] = $val['Type']; |
|
| 994 | - if ($val['Null'] == 'NO') { |
|
| 995 | - $nfields[$val['Field']] .= ' NOT NULL'; |
|
| 996 | - } |
|
| 997 | - if ($val['Default'] === '0' || $val['Default']) { |
|
| 998 | - if (preg_match('/[A-Z_]/', $val['Default'])) { |
|
| 999 | - $nfields[$val['Field']] .= ' DEFAULT ' . $val['Default']; |
|
| 1000 | - } else { |
|
| 1001 | - $nfields[$val['Field']] .= " DEFAULT '" . $val['Default'] . "'"; |
|
| 1002 | - } |
|
| 1003 | - } |
|
| 1004 | - if ($val['Extra']) { |
|
| 1005 | - $nfields[$val['Field']] .= ' ' . $val['Extra']; |
|
| 1006 | - } |
|
| 1007 | - if ($val['Key'] == 'PRI') { |
|
| 1008 | - $nkeys['PRIMARY KEY'] = $val['Field']; |
|
| 1009 | - } else { |
|
| 1010 | - if ($val['Key'] == 'MUL') { |
|
| 1011 | - $nkeys['KEY ' . $val['Field']] = $val['Field']; |
|
| 1012 | - } else { |
|
| 1013 | - if ($val['Key'] == 'UNI') { |
|
| 1014 | - $nkeys['UNIQUE KEY ' . $val['Field']] = $val['Field']; |
|
| 1015 | - } |
|
| 1016 | - } |
|
| 1017 | - } |
|
| 1018 | - } |
|
| 1019 | - spip_mysql_free($res); |
|
| 1020 | - |
|
| 1021 | - return ['field' => $nfields, 'key' => $nkeys]; |
|
| 1022 | - } |
|
| 1023 | - |
|
| 1024 | - return ''; |
|
| 945 | + $s = spip_mysql_query("SHOW CREATE TABLE `$nom_table`", $serveur, $requeter); |
|
| 946 | + if (!$s) { |
|
| 947 | + return ''; |
|
| 948 | + } |
|
| 949 | + if (!$requeter) { |
|
| 950 | + return $s; |
|
| 951 | + } |
|
| 952 | + |
|
| 953 | + [, $a] = mysqli_fetch_array($s, MYSQLI_NUM); |
|
| 954 | + if (preg_match(_MYSQL_RE_SHOW_TABLE, $a, $r)) { |
|
| 955 | + $desc = $r[1]; |
|
| 956 | + // extraction d'une KEY éventuelle en prenant garde de ne pas |
|
| 957 | + // relever un champ dont le nom contient KEY (ex. ID_WHISKEY) |
|
| 958 | + if (preg_match('/^(.*?),([^,]*\sKEY[ (].*)$/s', $desc, $r)) { |
|
| 959 | + $namedkeys = $r[2]; |
|
| 960 | + $desc = $r[1]; |
|
| 961 | + } else { |
|
| 962 | + $namedkeys = ''; |
|
| 963 | + } |
|
| 964 | + |
|
| 965 | + $fields = []; |
|
| 966 | + foreach (preg_split('/,\s*`/', $desc) as $v) { |
|
| 967 | + preg_match('/^\s*`?([^`]*)`\s*(.*)/', $v, $r); |
|
| 968 | + $fields[strtolower($r[1])] = $r[2]; |
|
| 969 | + } |
|
| 970 | + $keys = []; |
|
| 971 | + |
|
| 972 | + foreach (preg_split('/\)\s*(,|$)/', $namedkeys) as $v) { |
|
| 973 | + if (preg_match('/^\s*([^(]*)\(([^(]*(\(\d+\))?)$/', $v, $r)) { |
|
| 974 | + $k = str_replace('`', '', trim($r[1])); |
|
| 975 | + $t = strtolower(str_replace('`', '', $r[2])); |
|
| 976 | + if ($k && !isset($keys[$k])) { |
|
| 977 | + $keys[$k] = $t; |
|
| 978 | + } else { |
|
| 979 | + $keys[] = $t; |
|
| 980 | + } |
|
| 981 | + } |
|
| 982 | + } |
|
| 983 | + spip_mysql_free($s); |
|
| 984 | + |
|
| 985 | + return ['field' => $fields, 'key' => $keys]; |
|
| 986 | + } |
|
| 987 | + |
|
| 988 | + $res = spip_mysql_query("SHOW COLUMNS FROM `$nom_table`", $serveur); |
|
| 989 | + if ($res) { |
|
| 990 | + $nfields = []; |
|
| 991 | + $nkeys = []; |
|
| 992 | + while ($val = spip_mysql_fetch($res)) { |
|
| 993 | + $nfields[$val['Field']] = $val['Type']; |
|
| 994 | + if ($val['Null'] == 'NO') { |
|
| 995 | + $nfields[$val['Field']] .= ' NOT NULL'; |
|
| 996 | + } |
|
| 997 | + if ($val['Default'] === '0' || $val['Default']) { |
|
| 998 | + if (preg_match('/[A-Z_]/', $val['Default'])) { |
|
| 999 | + $nfields[$val['Field']] .= ' DEFAULT ' . $val['Default']; |
|
| 1000 | + } else { |
|
| 1001 | + $nfields[$val['Field']] .= " DEFAULT '" . $val['Default'] . "'"; |
|
| 1002 | + } |
|
| 1003 | + } |
|
| 1004 | + if ($val['Extra']) { |
|
| 1005 | + $nfields[$val['Field']] .= ' ' . $val['Extra']; |
|
| 1006 | + } |
|
| 1007 | + if ($val['Key'] == 'PRI') { |
|
| 1008 | + $nkeys['PRIMARY KEY'] = $val['Field']; |
|
| 1009 | + } else { |
|
| 1010 | + if ($val['Key'] == 'MUL') { |
|
| 1011 | + $nkeys['KEY ' . $val['Field']] = $val['Field']; |
|
| 1012 | + } else { |
|
| 1013 | + if ($val['Key'] == 'UNI') { |
|
| 1014 | + $nkeys['UNIQUE KEY ' . $val['Field']] = $val['Field']; |
|
| 1015 | + } |
|
| 1016 | + } |
|
| 1017 | + } |
|
| 1018 | + } |
|
| 1019 | + spip_mysql_free($res); |
|
| 1020 | + |
|
| 1021 | + return ['field' => $nfields, 'key' => $nkeys]; |
|
| 1022 | + } |
|
| 1023 | + |
|
| 1024 | + return ''; |
|
| 1025 | 1025 | } |
| 1026 | 1026 | |
| 1027 | 1027 | |
@@ -1040,13 +1040,13 @@ discard block |
||
| 1040 | 1040 | * - false Erreur |
| 1041 | 1041 | */ |
| 1042 | 1042 | function spip_mysql_fetch($r, $t = '', $serveur = '', $requeter = true) { |
| 1043 | - if (!$t) { |
|
| 1044 | - $t = \MYSQLI_ASSOC; |
|
| 1045 | - } |
|
| 1046 | - if ($r) { |
|
| 1047 | - return mysqli_fetch_array($r, $t); |
|
| 1048 | - } |
|
| 1049 | - return false; |
|
| 1043 | + if (!$t) { |
|
| 1044 | + $t = \MYSQLI_ASSOC; |
|
| 1045 | + } |
|
| 1046 | + if ($r) { |
|
| 1047 | + return mysqli_fetch_array($r, $t); |
|
| 1048 | + } |
|
| 1049 | + return false; |
|
| 1050 | 1050 | } |
| 1051 | 1051 | |
| 1052 | 1052 | /** |
@@ -1059,10 +1059,10 @@ discard block |
||
| 1059 | 1059 | * @return bool True si déplacement réussi, false sinon. |
| 1060 | 1060 | **/ |
| 1061 | 1061 | function spip_mysql_seek($r, $row_number, $serveur = '', $requeter = true) { |
| 1062 | - if ($r and mysqli_num_rows($r)) { |
|
| 1063 | - return mysqli_data_seek($r, $row_number); |
|
| 1064 | - } |
|
| 1065 | - return false; |
|
| 1062 | + if ($r and mysqli_num_rows($r)) { |
|
| 1063 | + return mysqli_data_seek($r, $row_number); |
|
| 1064 | + } |
|
| 1065 | + return false; |
|
| 1066 | 1066 | } |
| 1067 | 1067 | |
| 1068 | 1068 | |
@@ -1080,26 +1080,26 @@ discard block |
||
| 1080 | 1080 | * - int Nombre de lignes (0 si la requête n'a pas réussie) |
| 1081 | 1081 | **/ |
| 1082 | 1082 | function spip_mysql_countsel( |
| 1083 | - $from = [], |
|
| 1084 | - $where = [], |
|
| 1085 | - $groupby = '', |
|
| 1086 | - $having = [], |
|
| 1087 | - $serveur = '', |
|
| 1088 | - $requeter = true |
|
| 1083 | + $from = [], |
|
| 1084 | + $where = [], |
|
| 1085 | + $groupby = '', |
|
| 1086 | + $having = [], |
|
| 1087 | + $serveur = '', |
|
| 1088 | + $requeter = true |
|
| 1089 | 1089 | ) { |
| 1090 | - $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 1091 | - |
|
| 1092 | - $r = spip_mysql_select("COUNT($c)", $from, $where, '', '', '', $having, $serveur, $requeter); |
|
| 1093 | - if (!$requeter) { |
|
| 1094 | - return $r; |
|
| 1095 | - } |
|
| 1096 | - if (!$r instanceof mysqli_result) { |
|
| 1097 | - return 0; |
|
| 1098 | - } |
|
| 1099 | - [$c] = mysqli_fetch_array($r, MYSQLI_NUM); |
|
| 1100 | - mysqli_free_result($r); |
|
| 1101 | - |
|
| 1102 | - return intval($c); |
|
| 1090 | + $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 1091 | + |
|
| 1092 | + $r = spip_mysql_select("COUNT($c)", $from, $where, '', '', '', $having, $serveur, $requeter); |
|
| 1093 | + if (!$requeter) { |
|
| 1094 | + return $r; |
|
| 1095 | + } |
|
| 1096 | + if (!$r instanceof mysqli_result) { |
|
| 1097 | + return 0; |
|
| 1098 | + } |
|
| 1099 | + [$c] = mysqli_fetch_array($r, MYSQLI_NUM); |
|
| 1100 | + mysqli_free_result($r); |
|
| 1101 | + |
|
| 1102 | + return intval($c); |
|
| 1103 | 1103 | } |
| 1104 | 1104 | |
| 1105 | 1105 | |
@@ -1122,16 +1122,16 @@ discard block |
||
| 1122 | 1122 | * Erreur eventuelle |
| 1123 | 1123 | **/ |
| 1124 | 1124 | function spip_mysql_error($query = '', $serveur = '', $requeter = true) { |
| 1125 | - $link = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]['link']; |
|
| 1126 | - $s = mysqli_error($link); |
|
| 1127 | - if ($s) { |
|
| 1128 | - $trace = debug_backtrace(); |
|
| 1129 | - if ($trace[0]['function'] != 'spip_mysql_error') { |
|
| 1130 | - spip_log("$s - $query - " . sql_error_backtrace(), 'mysql.' . _LOG_ERREUR); |
|
| 1131 | - } |
|
| 1132 | - } |
|
| 1133 | - |
|
| 1134 | - return $s; |
|
| 1125 | + $link = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]['link']; |
|
| 1126 | + $s = mysqli_error($link); |
|
| 1127 | + if ($s) { |
|
| 1128 | + $trace = debug_backtrace(); |
|
| 1129 | + if ($trace[0]['function'] != 'spip_mysql_error') { |
|
| 1130 | + spip_log("$s - $query - " . sql_error_backtrace(), 'mysql.' . _LOG_ERREUR); |
|
| 1131 | + } |
|
| 1132 | + } |
|
| 1133 | + |
|
| 1134 | + return $s; |
|
| 1135 | 1135 | } |
| 1136 | 1136 | |
| 1137 | 1137 | |
@@ -1146,18 +1146,18 @@ discard block |
||
| 1146 | 1146 | * 0, pas d'erreur. Autre, numéro de l'erreur. |
| 1147 | 1147 | **/ |
| 1148 | 1148 | function spip_mysql_errno($serveur = '', $requeter = true) { |
| 1149 | - $link = $GLOBALS['connexions'][$serveur ?: 0]['link']; |
|
| 1150 | - $s = mysqli_errno($link); |
|
| 1151 | - // 2006 MySQL server has gone away |
|
| 1152 | - // 2013 Lost connection to MySQL server during query |
|
| 1153 | - if (in_array($s, [2006, 2013])) { |
|
| 1154 | - define('spip_interdire_cache', true); |
|
| 1155 | - } |
|
| 1156 | - if ($s) { |
|
| 1157 | - spip_log("Erreur mysql $s", _LOG_ERREUR); |
|
| 1158 | - } |
|
| 1159 | - |
|
| 1160 | - return $s; |
|
| 1149 | + $link = $GLOBALS['connexions'][$serveur ?: 0]['link']; |
|
| 1150 | + $s = mysqli_errno($link); |
|
| 1151 | + // 2006 MySQL server has gone away |
|
| 1152 | + // 2013 Lost connection to MySQL server during query |
|
| 1153 | + if (in_array($s, [2006, 2013])) { |
|
| 1154 | + define('spip_interdire_cache', true); |
|
| 1155 | + } |
|
| 1156 | + if ($s) { |
|
| 1157 | + spip_log("Erreur mysql $s", _LOG_ERREUR); |
|
| 1158 | + } |
|
| 1159 | + |
|
| 1160 | + return $s; |
|
| 1161 | 1161 | } |
| 1162 | 1162 | |
| 1163 | 1163 | |
@@ -1171,9 +1171,9 @@ discard block |
||
| 1171 | 1171 | * @return int Nombre de lignes |
| 1172 | 1172 | */ |
| 1173 | 1173 | function spip_mysql_count($r, $serveur = '', $requeter = true) { |
| 1174 | - if ($r) { |
|
| 1175 | - return mysqli_num_rows($r); |
|
| 1176 | - } |
|
| 1174 | + if ($r) { |
|
| 1175 | + return mysqli_num_rows($r); |
|
| 1176 | + } |
|
| 1177 | 1177 | } |
| 1178 | 1178 | |
| 1179 | 1179 | |
@@ -1189,11 +1189,11 @@ discard block |
||
| 1189 | 1189 | * @return bool True si réussi |
| 1190 | 1190 | */ |
| 1191 | 1191 | function spip_mysql_free($r, $serveur = '', $requeter = true) { |
| 1192 | - if ($r instanceof mysqli_result) { |
|
| 1193 | - mysqli_free_result($r); |
|
| 1194 | - return true; |
|
| 1195 | - } |
|
| 1196 | - return false; |
|
| 1192 | + if ($r instanceof mysqli_result) { |
|
| 1193 | + mysqli_free_result($r); |
|
| 1194 | + return true; |
|
| 1195 | + } |
|
| 1196 | + return false; |
|
| 1197 | 1197 | } |
| 1198 | 1198 | |
| 1199 | 1199 | |
@@ -1221,59 +1221,59 @@ discard block |
||
| 1221 | 1221 | **/ |
| 1222 | 1222 | function spip_mysql_insert($table, $champs, $valeurs, $desc = [], $serveur = '', $requeter = true) { |
| 1223 | 1223 | |
| 1224 | - $e = null; |
|
| 1225 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1226 | - $link = $connexion['link']; |
|
| 1227 | - $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 1228 | - |
|
| 1229 | - // remplacer les emoji (que mysql ne sait pas gérer) en 💩 |
|
| 1230 | - if ( |
|
| 1231 | - defined('_MYSQL_NOPLANES') |
|
| 1232 | - and _MYSQL_NOPLANES |
|
| 1233 | - and !empty($GLOBALS['meta']['charset_sql_connexion']) |
|
| 1234 | - and $GLOBALS['meta']['charset_sql_connexion'] == 'utf8' |
|
| 1235 | - ) { |
|
| 1236 | - include_spip('inc/charsets'); |
|
| 1237 | - $valeurs = utf8_noplanes($valeurs); |
|
| 1238 | - } |
|
| 1239 | - |
|
| 1240 | - $query = "INSERT INTO $table $champs VALUES $valeurs"; |
|
| 1241 | - if (!$requeter) { |
|
| 1242 | - return $query; |
|
| 1243 | - } |
|
| 1244 | - |
|
| 1245 | - if (isset($_GET['var_profile'])) { |
|
| 1246 | - include_spip('public/tracer'); |
|
| 1247 | - $t = trace_query_start(); |
|
| 1248 | - $e = ''; |
|
| 1249 | - } else { |
|
| 1250 | - $t = 0; |
|
| 1251 | - } |
|
| 1252 | - |
|
| 1253 | - $connexion['last'] = $query; |
|
| 1254 | - #spip_log($query, 'mysql.'._LOG_DEBUG); |
|
| 1255 | - $r = false; |
|
| 1256 | - $insert = false; |
|
| 1257 | - try { |
|
| 1258 | - $insert = mysqli_query($link, $query); |
|
| 1259 | - } catch (\mysqli_sql_exception $e) { |
|
| 1260 | - spip_log('mysqli_sql_exception: ' . $e->getMessage(), 'mysql.' . _LOG_DEBUG); |
|
| 1261 | - // TODO: utiliser l’exception ensuite plutôt que les appels à spip_mysql_errno() |
|
| 1262 | - // mais il faut pour php < 8.1 forcer les exeptions via mysqli_report(). |
|
| 1263 | - } |
|
| 1264 | - if ($insert) { |
|
| 1265 | - $r = mysqli_insert_id($link); |
|
| 1266 | - } else { |
|
| 1267 | - // Log de l'erreur eventuelle |
|
| 1268 | - if ($e = spip_mysql_errno($serveur)) { |
|
| 1269 | - // et du fautif |
|
| 1270 | - $e .= spip_mysql_error($query, $serveur); |
|
| 1271 | - } |
|
| 1272 | - } |
|
| 1273 | - |
|
| 1274 | - return $t ? trace_query_end($query, $t, $r, $e, $serveur) : $r; |
|
| 1275 | - |
|
| 1276 | - // return $r ? $r : (($r===0) ? -1 : 0); pb avec le multi-base. |
|
| 1224 | + $e = null; |
|
| 1225 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1226 | + $link = $connexion['link']; |
|
| 1227 | + $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 1228 | + |
|
| 1229 | + // remplacer les emoji (que mysql ne sait pas gérer) en 💩 |
|
| 1230 | + if ( |
|
| 1231 | + defined('_MYSQL_NOPLANES') |
|
| 1232 | + and _MYSQL_NOPLANES |
|
| 1233 | + and !empty($GLOBALS['meta']['charset_sql_connexion']) |
|
| 1234 | + and $GLOBALS['meta']['charset_sql_connexion'] == 'utf8' |
|
| 1235 | + ) { |
|
| 1236 | + include_spip('inc/charsets'); |
|
| 1237 | + $valeurs = utf8_noplanes($valeurs); |
|
| 1238 | + } |
|
| 1239 | + |
|
| 1240 | + $query = "INSERT INTO $table $champs VALUES $valeurs"; |
|
| 1241 | + if (!$requeter) { |
|
| 1242 | + return $query; |
|
| 1243 | + } |
|
| 1244 | + |
|
| 1245 | + if (isset($_GET['var_profile'])) { |
|
| 1246 | + include_spip('public/tracer'); |
|
| 1247 | + $t = trace_query_start(); |
|
| 1248 | + $e = ''; |
|
| 1249 | + } else { |
|
| 1250 | + $t = 0; |
|
| 1251 | + } |
|
| 1252 | + |
|
| 1253 | + $connexion['last'] = $query; |
|
| 1254 | + #spip_log($query, 'mysql.'._LOG_DEBUG); |
|
| 1255 | + $r = false; |
|
| 1256 | + $insert = false; |
|
| 1257 | + try { |
|
| 1258 | + $insert = mysqli_query($link, $query); |
|
| 1259 | + } catch (\mysqli_sql_exception $e) { |
|
| 1260 | + spip_log('mysqli_sql_exception: ' . $e->getMessage(), 'mysql.' . _LOG_DEBUG); |
|
| 1261 | + // TODO: utiliser l’exception ensuite plutôt que les appels à spip_mysql_errno() |
|
| 1262 | + // mais il faut pour php < 8.1 forcer les exeptions via mysqli_report(). |
|
| 1263 | + } |
|
| 1264 | + if ($insert) { |
|
| 1265 | + $r = mysqli_insert_id($link); |
|
| 1266 | + } else { |
|
| 1267 | + // Log de l'erreur eventuelle |
|
| 1268 | + if ($e = spip_mysql_errno($serveur)) { |
|
| 1269 | + // et du fautif |
|
| 1270 | + $e .= spip_mysql_error($query, $serveur); |
|
| 1271 | + } |
|
| 1272 | + } |
|
| 1273 | + |
|
| 1274 | + return $t ? trace_query_end($query, $t, $r, $e, $serveur) : $r; |
|
| 1275 | + |
|
| 1276 | + // return $r ? $r : (($r===0) ? -1 : 0); pb avec le multi-base. |
|
| 1277 | 1277 | } |
| 1278 | 1278 | |
| 1279 | 1279 | /** |
@@ -1298,26 +1298,26 @@ discard block |
||
| 1298 | 1298 | **/ |
| 1299 | 1299 | function spip_mysql_insertq($table, $couples = [], $desc = [], $serveur = '', $requeter = true) { |
| 1300 | 1300 | |
| 1301 | - if (!$desc) { |
|
| 1302 | - $desc = description_table($table, $serveur); |
|
| 1303 | - } |
|
| 1304 | - if (!$desc) { |
|
| 1305 | - $couples = []; |
|
| 1306 | - } |
|
| 1307 | - $fields = $desc['field'] ?? []; |
|
| 1308 | - |
|
| 1309 | - foreach ($couples as $champ => $val) { |
|
| 1310 | - $couples[$champ] = spip_mysql_cite($val, $fields[$champ]); |
|
| 1311 | - } |
|
| 1312 | - |
|
| 1313 | - return spip_mysql_insert( |
|
| 1314 | - $table, |
|
| 1315 | - '(' . join(',', array_keys($couples)) . ')', |
|
| 1316 | - '(' . join(',', $couples) . ')', |
|
| 1317 | - $desc, |
|
| 1318 | - $serveur, |
|
| 1319 | - $requeter |
|
| 1320 | - ); |
|
| 1301 | + if (!$desc) { |
|
| 1302 | + $desc = description_table($table, $serveur); |
|
| 1303 | + } |
|
| 1304 | + if (!$desc) { |
|
| 1305 | + $couples = []; |
|
| 1306 | + } |
|
| 1307 | + $fields = $desc['field'] ?? []; |
|
| 1308 | + |
|
| 1309 | + foreach ($couples as $champ => $val) { |
|
| 1310 | + $couples[$champ] = spip_mysql_cite($val, $fields[$champ]); |
|
| 1311 | + } |
|
| 1312 | + |
|
| 1313 | + return spip_mysql_insert( |
|
| 1314 | + $table, |
|
| 1315 | + '(' . join(',', array_keys($couples)) . ')', |
|
| 1316 | + '(' . join(',', $couples) . ')', |
|
| 1317 | + $desc, |
|
| 1318 | + $serveur, |
|
| 1319 | + $requeter |
|
| 1320 | + ); |
|
| 1321 | 1321 | } |
| 1322 | 1322 | |
| 1323 | 1323 | |
@@ -1342,34 +1342,34 @@ discard block |
||
| 1342 | 1342 | **/ |
| 1343 | 1343 | function spip_mysql_insertq_multi($table, $tab_couples = [], $desc = [], $serveur = '', $requeter = true) { |
| 1344 | 1344 | |
| 1345 | - if (!$desc) { |
|
| 1346 | - $desc = description_table($table, $serveur); |
|
| 1347 | - } |
|
| 1348 | - if (!$desc) { |
|
| 1349 | - $tab_couples = []; |
|
| 1350 | - } |
|
| 1351 | - $fields = $desc['field'] ?? []; |
|
| 1352 | - |
|
| 1353 | - $cles = '(' . join(',', array_keys(reset($tab_couples))) . ')'; |
|
| 1354 | - $valeurs = []; |
|
| 1355 | - $r = false; |
|
| 1356 | - |
|
| 1357 | - // Quoter et Inserer par groupes de 100 max pour eviter un debordement de pile |
|
| 1358 | - foreach ($tab_couples as $couples) { |
|
| 1359 | - foreach ($couples as $champ => $val) { |
|
| 1360 | - $couples[$champ] = spip_mysql_cite($val, $fields[$champ]); |
|
| 1361 | - } |
|
| 1362 | - $valeurs[] = '(' . join(',', $couples) . ')'; |
|
| 1363 | - if (count($valeurs) >= 100) { |
|
| 1364 | - $r = spip_mysql_insert($table, $cles, join(', ', $valeurs), $desc, $serveur, $requeter); |
|
| 1365 | - $valeurs = []; |
|
| 1366 | - } |
|
| 1367 | - } |
|
| 1368 | - if (count($valeurs)) { |
|
| 1369 | - $r = spip_mysql_insert($table, $cles, join(', ', $valeurs), $desc, $serveur, $requeter); |
|
| 1370 | - } |
|
| 1371 | - |
|
| 1372 | - return $r; // dans le cas d'une table auto_increment, le dernier insert_id |
|
| 1345 | + if (!$desc) { |
|
| 1346 | + $desc = description_table($table, $serveur); |
|
| 1347 | + } |
|
| 1348 | + if (!$desc) { |
|
| 1349 | + $tab_couples = []; |
|
| 1350 | + } |
|
| 1351 | + $fields = $desc['field'] ?? []; |
|
| 1352 | + |
|
| 1353 | + $cles = '(' . join(',', array_keys(reset($tab_couples))) . ')'; |
|
| 1354 | + $valeurs = []; |
|
| 1355 | + $r = false; |
|
| 1356 | + |
|
| 1357 | + // Quoter et Inserer par groupes de 100 max pour eviter un debordement de pile |
|
| 1358 | + foreach ($tab_couples as $couples) { |
|
| 1359 | + foreach ($couples as $champ => $val) { |
|
| 1360 | + $couples[$champ] = spip_mysql_cite($val, $fields[$champ]); |
|
| 1361 | + } |
|
| 1362 | + $valeurs[] = '(' . join(',', $couples) . ')'; |
|
| 1363 | + if (count($valeurs) >= 100) { |
|
| 1364 | + $r = spip_mysql_insert($table, $cles, join(', ', $valeurs), $desc, $serveur, $requeter); |
|
| 1365 | + $valeurs = []; |
|
| 1366 | + } |
|
| 1367 | + } |
|
| 1368 | + if (count($valeurs)) { |
|
| 1369 | + $r = spip_mysql_insert($table, $cles, join(', ', $valeurs), $desc, $serveur, $requeter); |
|
| 1370 | + } |
|
| 1371 | + |
|
| 1372 | + return $r; // dans le cas d'une table auto_increment, le dernier insert_id |
|
| 1373 | 1373 | } |
| 1374 | 1374 | |
| 1375 | 1375 | /** |
@@ -1394,20 +1394,20 @@ discard block |
||
| 1394 | 1394 | * - array Tableau décrivant la requête et son temps d'exécution si var_profile est actif |
| 1395 | 1395 | */ |
| 1396 | 1396 | function spip_mysql_update($table, $champs, $where = '', $desc = [], $serveur = '', $requeter = true) { |
| 1397 | - $set = []; |
|
| 1398 | - foreach ($champs as $champ => $val) { |
|
| 1399 | - $set[] = $champ . "=$val"; |
|
| 1400 | - } |
|
| 1401 | - if (!empty($set)) { |
|
| 1402 | - return spip_mysql_query( |
|
| 1403 | - calculer_mysql_expression('UPDATE', $table, ',') |
|
| 1404 | - . calculer_mysql_expression('SET', $set, ',') |
|
| 1405 | - . calculer_mysql_expression('WHERE', $where), |
|
| 1406 | - $serveur, |
|
| 1407 | - $requeter |
|
| 1408 | - ); |
|
| 1409 | - } |
|
| 1410 | - return false; |
|
| 1397 | + $set = []; |
|
| 1398 | + foreach ($champs as $champ => $val) { |
|
| 1399 | + $set[] = $champ . "=$val"; |
|
| 1400 | + } |
|
| 1401 | + if (!empty($set)) { |
|
| 1402 | + return spip_mysql_query( |
|
| 1403 | + calculer_mysql_expression('UPDATE', $table, ',') |
|
| 1404 | + . calculer_mysql_expression('SET', $set, ',') |
|
| 1405 | + . calculer_mysql_expression('WHERE', $where), |
|
| 1406 | + $serveur, |
|
| 1407 | + $requeter |
|
| 1408 | + ); |
|
| 1409 | + } |
|
| 1410 | + return false; |
|
| 1411 | 1411 | } |
| 1412 | 1412 | |
| 1413 | 1413 | /** |
@@ -1440,29 +1440,29 @@ discard block |
||
| 1440 | 1440 | */ |
| 1441 | 1441 | function spip_mysql_updateq($table, $champs, $where = '', $desc = [], $serveur = '', $requeter = true) { |
| 1442 | 1442 | |
| 1443 | - if (!$champs) { |
|
| 1444 | - return; |
|
| 1445 | - } |
|
| 1446 | - if (!$desc) { |
|
| 1447 | - $desc = description_table($table, $serveur); |
|
| 1448 | - } |
|
| 1449 | - if (!$desc) { |
|
| 1450 | - $champs = []; |
|
| 1451 | - } else { |
|
| 1452 | - $fields = $desc['field']; |
|
| 1453 | - } |
|
| 1454 | - $set = []; |
|
| 1455 | - foreach ($champs as $champ => $val) { |
|
| 1456 | - $set[] = $champ . '=' . spip_mysql_cite($val, @$fields[$champ]); |
|
| 1457 | - } |
|
| 1458 | - |
|
| 1459 | - return spip_mysql_query( |
|
| 1460 | - calculer_mysql_expression('UPDATE', $table, ',') |
|
| 1461 | - . calculer_mysql_expression('SET', $set, ',') |
|
| 1462 | - . calculer_mysql_expression('WHERE', $where), |
|
| 1463 | - $serveur, |
|
| 1464 | - $requeter |
|
| 1465 | - ); |
|
| 1443 | + if (!$champs) { |
|
| 1444 | + return; |
|
| 1445 | + } |
|
| 1446 | + if (!$desc) { |
|
| 1447 | + $desc = description_table($table, $serveur); |
|
| 1448 | + } |
|
| 1449 | + if (!$desc) { |
|
| 1450 | + $champs = []; |
|
| 1451 | + } else { |
|
| 1452 | + $fields = $desc['field']; |
|
| 1453 | + } |
|
| 1454 | + $set = []; |
|
| 1455 | + foreach ($champs as $champ => $val) { |
|
| 1456 | + $set[] = $champ . '=' . spip_mysql_cite($val, @$fields[$champ]); |
|
| 1457 | + } |
|
| 1458 | + |
|
| 1459 | + return spip_mysql_query( |
|
| 1460 | + calculer_mysql_expression('UPDATE', $table, ',') |
|
| 1461 | + . calculer_mysql_expression('SET', $set, ',') |
|
| 1462 | + . calculer_mysql_expression('WHERE', $where), |
|
| 1463 | + $serveur, |
|
| 1464 | + $requeter |
|
| 1465 | + ); |
|
| 1466 | 1466 | } |
| 1467 | 1467 | |
| 1468 | 1468 | /** |
@@ -1478,22 +1478,22 @@ discard block |
||
| 1478 | 1478 | * - false en cas d'erreur. |
| 1479 | 1479 | **/ |
| 1480 | 1480 | function spip_mysql_delete($table, $where = '', $serveur = '', $requeter = true) { |
| 1481 | - $res = spip_mysql_query( |
|
| 1482 | - calculer_mysql_expression('DELETE FROM', $table, ',') |
|
| 1483 | - . calculer_mysql_expression('WHERE', $where), |
|
| 1484 | - $serveur, |
|
| 1485 | - $requeter |
|
| 1486 | - ); |
|
| 1487 | - if (!$requeter) { |
|
| 1488 | - return $res; |
|
| 1489 | - } |
|
| 1490 | - if ($res) { |
|
| 1491 | - $link = _mysql_link($serveur); |
|
| 1492 | - |
|
| 1493 | - return mysqli_affected_rows($link); |
|
| 1494 | - } else { |
|
| 1495 | - return false; |
|
| 1496 | - } |
|
| 1481 | + $res = spip_mysql_query( |
|
| 1482 | + calculer_mysql_expression('DELETE FROM', $table, ',') |
|
| 1483 | + . calculer_mysql_expression('WHERE', $where), |
|
| 1484 | + $serveur, |
|
| 1485 | + $requeter |
|
| 1486 | + ); |
|
| 1487 | + if (!$requeter) { |
|
| 1488 | + return $res; |
|
| 1489 | + } |
|
| 1490 | + if ($res) { |
|
| 1491 | + $link = _mysql_link($serveur); |
|
| 1492 | + |
|
| 1493 | + return mysqli_affected_rows($link); |
|
| 1494 | + } else { |
|
| 1495 | + return false; |
|
| 1496 | + } |
|
| 1497 | 1497 | } |
| 1498 | 1498 | |
| 1499 | 1499 | |
@@ -1522,10 +1522,10 @@ discard block |
||
| 1522 | 1522 | * - false en cas d'erreur. |
| 1523 | 1523 | **/ |
| 1524 | 1524 | function spip_mysql_replace($table, $couples, $desc = [], $serveur = '', $requeter = true) { |
| 1525 | - return spip_mysql_query("REPLACE $table (" . join(',', array_keys($couples)) . ') VALUES (' . join( |
|
| 1526 | - ',', |
|
| 1527 | - array_map('_q', $couples) |
|
| 1528 | - ) . ')', $serveur, $requeter); |
|
| 1525 | + return spip_mysql_query("REPLACE $table (" . join(',', array_keys($couples)) . ') VALUES (' . join( |
|
| 1526 | + ',', |
|
| 1527 | + array_map('_q', $couples) |
|
| 1528 | + ) . ')', $serveur, $requeter); |
|
| 1529 | 1529 | } |
| 1530 | 1530 | |
| 1531 | 1531 | |
@@ -1554,14 +1554,14 @@ discard block |
||
| 1554 | 1554 | * - false en cas d'erreur. |
| 1555 | 1555 | **/ |
| 1556 | 1556 | function spip_mysql_replace_multi($table, $tab_couples, $desc = [], $serveur = '', $requeter = true) { |
| 1557 | - $cles = '(' . join(',', array_keys($tab_couples[0])) . ')'; |
|
| 1558 | - $valeurs = []; |
|
| 1559 | - foreach ($tab_couples as $couples) { |
|
| 1560 | - $valeurs[] = '(' . join(',', array_map('_q', $couples)) . ')'; |
|
| 1561 | - } |
|
| 1562 | - $valeurs = implode(', ', $valeurs); |
|
| 1563 | - |
|
| 1564 | - return spip_mysql_query("REPLACE $table $cles VALUES $valeurs", $serveur, $requeter); |
|
| 1557 | + $cles = '(' . join(',', array_keys($tab_couples[0])) . ')'; |
|
| 1558 | + $valeurs = []; |
|
| 1559 | + foreach ($tab_couples as $couples) { |
|
| 1560 | + $valeurs[] = '(' . join(',', array_map('_q', $couples)) . ')'; |
|
| 1561 | + } |
|
| 1562 | + $valeurs = implode(', ', $valeurs); |
|
| 1563 | + |
|
| 1564 | + return spip_mysql_query("REPLACE $table $cles VALUES $valeurs", $serveur, $requeter); |
|
| 1565 | 1565 | } |
| 1566 | 1566 | |
| 1567 | 1567 | |
@@ -1576,32 +1576,32 @@ discard block |
||
| 1576 | 1576 | * @return string texte de sélection pour la requête |
| 1577 | 1577 | */ |
| 1578 | 1578 | function spip_mysql_multi($objet, $lang) { |
| 1579 | - $lengthlang = strlen("[$lang]"); |
|
| 1580 | - $posmulti = 'INSTR(' . $objet . ", '<multi>')"; |
|
| 1581 | - $posfinmulti = 'INSTR(' . $objet . ", '</multi>')"; |
|
| 1582 | - $debutchaine = 'LEFT(' . $objet . ", $posmulti-1)"; |
|
| 1583 | - $finchaine = 'RIGHT(' . $objet . ', CHAR_LENGTH(' . $objet . ") -(7+$posfinmulti))"; |
|
| 1584 | - $chainemulti = 'TRIM(SUBSTRING(' . $objet . ", $posmulti+7, $posfinmulti -(7+$posmulti)))"; |
|
| 1585 | - $poslang = "INSTR($chainemulti,'[" . $lang . "]')"; |
|
| 1586 | - $poslang = "IF($poslang=0,INSTR($chainemulti,']')+1,$poslang+$lengthlang)"; |
|
| 1587 | - $chainelang = 'TRIM(SUBSTRING(' . $objet . ", $posmulti+7+$poslang-1,$posfinmulti -($posmulti+7+$poslang-1) ))"; |
|
| 1588 | - $posfinlang = 'INSTR(' . $chainelang . ", '[')"; |
|
| 1589 | - $chainelang = "IF($posfinlang>0,LEFT($chainelang,$posfinlang-1),$chainelang)"; |
|
| 1590 | - //$chainelang = "LEFT($chainelang,$posfinlang-1)"; |
|
| 1591 | - $retour = "(TRIM(IF($posmulti = 0 , " . |
|
| 1592 | - ' TRIM(' . $objet . '), ' . |
|
| 1593 | - ' CONCAT( ' . |
|
| 1594 | - " $debutchaine, " . |
|
| 1595 | - ' IF( ' . |
|
| 1596 | - " $poslang = 0, " . |
|
| 1597 | - " $chainemulti, " . |
|
| 1598 | - " $chainelang" . |
|
| 1599 | - ' ), ' . |
|
| 1600 | - " $finchaine" . |
|
| 1601 | - ' ) ' . |
|
| 1602 | - '))) AS multi'; |
|
| 1603 | - |
|
| 1604 | - return $retour; |
|
| 1579 | + $lengthlang = strlen("[$lang]"); |
|
| 1580 | + $posmulti = 'INSTR(' . $objet . ", '<multi>')"; |
|
| 1581 | + $posfinmulti = 'INSTR(' . $objet . ", '</multi>')"; |
|
| 1582 | + $debutchaine = 'LEFT(' . $objet . ", $posmulti-1)"; |
|
| 1583 | + $finchaine = 'RIGHT(' . $objet . ', CHAR_LENGTH(' . $objet . ") -(7+$posfinmulti))"; |
|
| 1584 | + $chainemulti = 'TRIM(SUBSTRING(' . $objet . ", $posmulti+7, $posfinmulti -(7+$posmulti)))"; |
|
| 1585 | + $poslang = "INSTR($chainemulti,'[" . $lang . "]')"; |
|
| 1586 | + $poslang = "IF($poslang=0,INSTR($chainemulti,']')+1,$poslang+$lengthlang)"; |
|
| 1587 | + $chainelang = 'TRIM(SUBSTRING(' . $objet . ", $posmulti+7+$poslang-1,$posfinmulti -($posmulti+7+$poslang-1) ))"; |
|
| 1588 | + $posfinlang = 'INSTR(' . $chainelang . ", '[')"; |
|
| 1589 | + $chainelang = "IF($posfinlang>0,LEFT($chainelang,$posfinlang-1),$chainelang)"; |
|
| 1590 | + //$chainelang = "LEFT($chainelang,$posfinlang-1)"; |
|
| 1591 | + $retour = "(TRIM(IF($posmulti = 0 , " . |
|
| 1592 | + ' TRIM(' . $objet . '), ' . |
|
| 1593 | + ' CONCAT( ' . |
|
| 1594 | + " $debutchaine, " . |
|
| 1595 | + ' IF( ' . |
|
| 1596 | + " $poslang = 0, " . |
|
| 1597 | + " $chainemulti, " . |
|
| 1598 | + " $chainelang" . |
|
| 1599 | + ' ), ' . |
|
| 1600 | + " $finchaine" . |
|
| 1601 | + ' ) ' . |
|
| 1602 | + '))) AS multi'; |
|
| 1603 | + |
|
| 1604 | + return $retour; |
|
| 1605 | 1605 | } |
| 1606 | 1606 | |
| 1607 | 1607 | /** |
@@ -1615,7 +1615,7 @@ discard block |
||
| 1615 | 1615 | * Valeur hexadécimale pour MySQL |
| 1616 | 1616 | **/ |
| 1617 | 1617 | function spip_mysql_hex($v) { |
| 1618 | - return '0x' . $v; |
|
| 1618 | + return '0x' . $v; |
|
| 1619 | 1619 | } |
| 1620 | 1620 | |
| 1621 | 1621 | /** |
@@ -1631,15 +1631,15 @@ discard block |
||
| 1631 | 1631 | * Donnée prête à être utilisée par le gestionnaire SQL |
| 1632 | 1632 | */ |
| 1633 | 1633 | function spip_mysql_quote($v, $type = '') { |
| 1634 | - if (!is_array($v)) { |
|
| 1635 | - return spip_mysql_cite($v, $type); |
|
| 1636 | - } |
|
| 1637 | - |
|
| 1638 | - // si c'est un tableau, le parcourir en propageant le type |
|
| 1639 | - foreach ($v as $k => $r) { |
|
| 1640 | - $v[$k] = spip_mysql_quote($r, $type); |
|
| 1641 | - } |
|
| 1642 | - return implode(',', $v); |
|
| 1634 | + if (!is_array($v)) { |
|
| 1635 | + return spip_mysql_cite($v, $type); |
|
| 1636 | + } |
|
| 1637 | + |
|
| 1638 | + // si c'est un tableau, le parcourir en propageant le type |
|
| 1639 | + foreach ($v as $k => $r) { |
|
| 1640 | + $v[$k] = spip_mysql_quote($r, $type); |
|
| 1641 | + } |
|
| 1642 | + return implode(',', $v); |
|
| 1643 | 1643 | } |
| 1644 | 1644 | |
| 1645 | 1645 | /** |
@@ -1655,18 +1655,18 @@ discard block |
||
| 1655 | 1655 | * Expression SQL |
| 1656 | 1656 | **/ |
| 1657 | 1657 | function spip_mysql_date_proche($champ, $interval, $unite) { |
| 1658 | - $use_now = ( ($champ === 'maj' or strpos($champ, '.maj')) ? true : false ); |
|
| 1659 | - return '(' |
|
| 1660 | - . $champ |
|
| 1661 | - . (($interval <= 0) ? '>' : '<') |
|
| 1662 | - . (($interval <= 0) ? 'DATE_SUB' : 'DATE_ADD') |
|
| 1663 | - . '(' |
|
| 1664 | - . ($use_now ? 'NOW()' : sql_quote(date('Y-m-d H:i:s'))) |
|
| 1665 | - . ', INTERVAL ' |
|
| 1666 | - . (($interval > 0) ? $interval : (0 - $interval)) |
|
| 1667 | - . ' ' |
|
| 1668 | - . $unite |
|
| 1669 | - . '))'; |
|
| 1658 | + $use_now = ( ($champ === 'maj' or strpos($champ, '.maj')) ? true : false ); |
|
| 1659 | + return '(' |
|
| 1660 | + . $champ |
|
| 1661 | + . (($interval <= 0) ? '>' : '<') |
|
| 1662 | + . (($interval <= 0) ? 'DATE_SUB' : 'DATE_ADD') |
|
| 1663 | + . '(' |
|
| 1664 | + . ($use_now ? 'NOW()' : sql_quote(date('Y-m-d H:i:s'))) |
|
| 1665 | + . ', INTERVAL ' |
|
| 1666 | + . (($interval > 0) ? $interval : (0 - $interval)) |
|
| 1667 | + . ' ' |
|
| 1668 | + . $unite |
|
| 1669 | + . '))'; |
|
| 1670 | 1670 | } |
| 1671 | 1671 | |
| 1672 | 1672 | |
@@ -1690,7 +1690,7 @@ discard block |
||
| 1690 | 1690 | * Expression de requête SQL |
| 1691 | 1691 | **/ |
| 1692 | 1692 | function spip_mysql_in($val, $valeurs, $not = '', $serveur = '', $requeter = true) { |
| 1693 | - return "($val $not IN ($valeurs))"; |
|
| 1693 | + return "($val $not IN ($valeurs))"; |
|
| 1694 | 1694 | } |
| 1695 | 1695 | |
| 1696 | 1696 | |
@@ -1702,39 +1702,39 @@ discard block |
||
| 1702 | 1702 | * @return string|number texte ou nombre échappé |
| 1703 | 1703 | */ |
| 1704 | 1704 | function spip_mysql_cite($v, $type) { |
| 1705 | - if (!$type) { |
|
| 1706 | - if (is_bool($v)) { |
|
| 1707 | - return strval(intval($v)); |
|
| 1708 | - } elseif (is_numeric($v)) { |
|
| 1709 | - return strval($v); |
|
| 1710 | - } elseif ($v === null) { |
|
| 1711 | - return "''"; |
|
| 1712 | - } |
|
| 1713 | - return "'" . addslashes($v) . "'"; |
|
| 1714 | - } |
|
| 1715 | - |
|
| 1716 | - if ($v === null) { |
|
| 1717 | - if (stripos($type, 'NOT NULL') === false) { |
|
| 1718 | - // null php se traduit en NULL SQL |
|
| 1719 | - return 'NULL'; |
|
| 1720 | - } else { |
|
| 1721 | - return "''"; |
|
| 1722 | - } |
|
| 1723 | - } elseif (sql_test_date($type) and preg_match('/^\w+\(/', $v)) { |
|
| 1724 | - return $v; |
|
| 1725 | - } elseif (sql_test_int($type)) { |
|
| 1726 | - if ( |
|
| 1727 | - is_numeric($v) |
|
| 1728 | - or ($v and ctype_xdigit(substr($v, 2)) and $v[0] === '0' and $v[1] === 'x') |
|
| 1729 | - ) { |
|
| 1730 | - return $v; |
|
| 1731 | - } else { |
|
| 1732 | - // si pas numerique, forcer le intval |
|
| 1733 | - return intval($v); |
|
| 1734 | - } |
|
| 1735 | - } |
|
| 1736 | - |
|
| 1737 | - return ("'" . addslashes($v) . "'"); |
|
| 1705 | + if (!$type) { |
|
| 1706 | + if (is_bool($v)) { |
|
| 1707 | + return strval(intval($v)); |
|
| 1708 | + } elseif (is_numeric($v)) { |
|
| 1709 | + return strval($v); |
|
| 1710 | + } elseif ($v === null) { |
|
| 1711 | + return "''"; |
|
| 1712 | + } |
|
| 1713 | + return "'" . addslashes($v) . "'"; |
|
| 1714 | + } |
|
| 1715 | + |
|
| 1716 | + if ($v === null) { |
|
| 1717 | + if (stripos($type, 'NOT NULL') === false) { |
|
| 1718 | + // null php se traduit en NULL SQL |
|
| 1719 | + return 'NULL'; |
|
| 1720 | + } else { |
|
| 1721 | + return "''"; |
|
| 1722 | + } |
|
| 1723 | + } elseif (sql_test_date($type) and preg_match('/^\w+\(/', $v)) { |
|
| 1724 | + return $v; |
|
| 1725 | + } elseif (sql_test_int($type)) { |
|
| 1726 | + if ( |
|
| 1727 | + is_numeric($v) |
|
| 1728 | + or ($v and ctype_xdigit(substr($v, 2)) and $v[0] === '0' and $v[1] === 'x') |
|
| 1729 | + ) { |
|
| 1730 | + return $v; |
|
| 1731 | + } else { |
|
| 1732 | + // si pas numerique, forcer le intval |
|
| 1733 | + return intval($v); |
|
| 1734 | + } |
|
| 1735 | + } |
|
| 1736 | + |
|
| 1737 | + return ("'" . addslashes($v) . "'"); |
|
| 1738 | 1738 | } |
| 1739 | 1739 | |
| 1740 | 1740 | /** |
@@ -1744,7 +1744,7 @@ discard block |
||
| 1744 | 1744 | * True si on a les fonctions, false sinon |
| 1745 | 1745 | */ |
| 1746 | 1746 | function spip_versions_mysql() { |
| 1747 | - return function_exists('mysqli_query'); |
|
| 1747 | + return function_exists('mysqli_query'); |
|
| 1748 | 1748 | } |
| 1749 | 1749 | |
| 1750 | 1750 | |
@@ -1757,20 +1757,20 @@ discard block |
||
| 1757 | 1757 | * - chaîne : code compilé pour le faire désactiver par SPIP sinon |
| 1758 | 1758 | */ |
| 1759 | 1759 | function test_rappel_nom_base_mysql($server_db) { |
| 1760 | - $GLOBALS['mysql_rappel_nom_base'] = true; |
|
| 1761 | - sql_delete('spip_meta', "nom='mysql_rappel_nom_base'", $server_db); |
|
| 1762 | - $ok = spip_query("INSERT INTO spip_meta (nom,valeur) VALUES ('mysql_rappel_nom_base', 'test')", $server_db); |
|
| 1760 | + $GLOBALS['mysql_rappel_nom_base'] = true; |
|
| 1761 | + sql_delete('spip_meta', "nom='mysql_rappel_nom_base'", $server_db); |
|
| 1762 | + $ok = spip_query("INSERT INTO spip_meta (nom,valeur) VALUES ('mysql_rappel_nom_base', 'test')", $server_db); |
|
| 1763 | 1763 | |
| 1764 | - if ($ok) { |
|
| 1765 | - sql_delete('spip_meta', "nom='mysql_rappel_nom_base'", $server_db); |
|
| 1764 | + if ($ok) { |
|
| 1765 | + sql_delete('spip_meta', "nom='mysql_rappel_nom_base'", $server_db); |
|
| 1766 | 1766 | |
| 1767 | - return ''; |
|
| 1768 | - } else { |
|
| 1769 | - $GLOBALS['mysql_rappel_nom_base'] = false; |
|
| 1767 | + return ''; |
|
| 1768 | + } else { |
|
| 1769 | + $GLOBALS['mysql_rappel_nom_base'] = false; |
|
| 1770 | 1770 | |
| 1771 | - return "\$GLOBALS['mysql_rappel_nom_base'] = false; " . |
|
| 1772 | - "/* echec de test_rappel_nom_base_mysql a l'installation. */\n"; |
|
| 1773 | - } |
|
| 1771 | + return "\$GLOBALS['mysql_rappel_nom_base'] = false; " . |
|
| 1772 | + "/* echec de test_rappel_nom_base_mysql a l'installation. */\n"; |
|
| 1773 | + } |
|
| 1774 | 1774 | } |
| 1775 | 1775 | |
| 1776 | 1776 | /** |
@@ -1784,13 +1784,13 @@ discard block |
||
| 1784 | 1784 | * - chaîne : code compilé pour l'indiquer le résultat du test à SPIP |
| 1785 | 1785 | */ |
| 1786 | 1786 | function test_sql_mode_mysql($server_db) { |
| 1787 | - $res = sql_select('version() as v', '', '', '', '', '', '', $server_db); |
|
| 1788 | - $row = sql_fetch($res, $server_db); |
|
| 1789 | - if (version_compare($row['v'], '5.0.0', '>=')) { |
|
| 1790 | - defined('_MYSQL_SET_SQL_MODE') || define('_MYSQL_SET_SQL_MODE', true); |
|
| 1787 | + $res = sql_select('version() as v', '', '', '', '', '', '', $server_db); |
|
| 1788 | + $row = sql_fetch($res, $server_db); |
|
| 1789 | + if (version_compare($row['v'], '5.0.0', '>=')) { |
|
| 1790 | + defined('_MYSQL_SET_SQL_MODE') || define('_MYSQL_SET_SQL_MODE', true); |
|
| 1791 | 1791 | |
| 1792 | - return "defined('_MYSQL_SET_SQL_MODE') || define('_MYSQL_SET_SQL_MODE',true);\n"; |
|
| 1793 | - } |
|
| 1792 | + return "defined('_MYSQL_SET_SQL_MODE') || define('_MYSQL_SET_SQL_MODE',true);\n"; |
|
| 1793 | + } |
|
| 1794 | 1794 | |
| 1795 | - return ''; |
|
| 1795 | + return ''; |
|
| 1796 | 1796 | } |
@@ -58,12 +58,12 @@ discard block |
||
| 58 | 58 | $link = @mysqli_connect($host, $login, $pass); |
| 59 | 59 | } |
| 60 | 60 | } catch (\mysqli_sql_exception $e) { |
| 61 | - spip_log('mysqli_sql_exception: ' . $e->getMessage(), 'mysql.' . _LOG_DEBUG); |
|
| 61 | + spip_log('mysqli_sql_exception: '.$e->getMessage(), 'mysql.'._LOG_DEBUG); |
|
| 62 | 62 | $link = false; |
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | if (!$link) { |
| 66 | - spip_log('Echec mysqli_connect. Erreur : ' . mysqli_connect_error(), 'mysql.' . _LOG_HS); |
|
| 66 | + spip_log('Echec mysqli_connect. Erreur : '.mysqli_connect_error(), 'mysql.'._LOG_HS); |
|
| 67 | 67 | |
| 68 | 68 | return false; |
| 69 | 69 | } |
@@ -82,7 +82,7 @@ discard block |
||
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | spip_log( |
| 85 | - "Connexion MySQLi vers $host, base $db, prefixe $prefixe " . ($ok ? 'operationnelle' : 'impossible'), |
|
| 85 | + "Connexion MySQLi vers $host, base $db, prefixe $prefixe ".($ok ? 'operationnelle' : 'impossible'), |
|
| 86 | 86 | _LOG_DEBUG |
| 87 | 87 | ); |
| 88 | 88 | |
@@ -173,9 +173,9 @@ discard block |
||
| 173 | 173 | */ |
| 174 | 174 | function spip_mysql_set_charset($charset, $serveur = '', $requeter = true) { |
| 175 | 175 | $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
| 176 | - spip_log('changement de charset sql : ' . 'SET NAMES ' . _q($charset), _LOG_DEBUG); |
|
| 176 | + spip_log('changement de charset sql : '.'SET NAMES '._q($charset), _LOG_DEBUG); |
|
| 177 | 177 | |
| 178 | - return mysqli_query($connexion['link'], $connexion['last'] = 'SET NAMES ' . _q($charset)); |
|
| 178 | + return mysqli_query($connexion['link'], $connexion['last'] = 'SET NAMES '._q($charset)); |
|
| 179 | 179 | } |
| 180 | 180 | |
| 181 | 181 | |
@@ -190,7 +190,7 @@ discard block |
||
| 190 | 190 | function spip_mysql_get_charset($charset = [], $serveur = '', $requeter = true) { |
| 191 | 191 | $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
| 192 | 192 | $connexion['last'] = $c = 'SHOW CHARACTER SET' |
| 193 | - . (!$charset ? '' : (' LIKE ' . _q($charset['charset']))); |
|
| 193 | + . (!$charset ? '' : (' LIKE '._q($charset['charset']))); |
|
| 194 | 194 | |
| 195 | 195 | return spip_mysql_fetch(mysqli_query($connexion['link'], $c), null, $serveur); |
| 196 | 196 | } |
@@ -234,21 +234,21 @@ discard block |
||
| 234 | 234 | $debug = ''; |
| 235 | 235 | if (defined('_DEBUG_SLOW_QUERIES') and _DEBUG_SLOW_QUERIES) { |
| 236 | 236 | if (isset($GLOBALS['debug']['aucasou'])) { |
| 237 | - [, $id, , $infos] = $GLOBALS['debug']['aucasou']; |
|
| 238 | - $debug .= "BOUCLE$id @ " . ($infos[0] ?? '') . ' | '; |
|
| 237 | + [, $id,, $infos] = $GLOBALS['debug']['aucasou']; |
|
| 238 | + $debug .= "BOUCLE$id @ ".($infos[0] ?? '').' | '; |
|
| 239 | 239 | } |
| 240 | 240 | if (isset($_SERVER['REQUEST_URI'])) { |
| 241 | 241 | $debug .= $_SERVER['REQUEST_URI']; |
| 242 | 242 | } |
| 243 | 243 | if (!empty($GLOBALS['ip'])) { |
| 244 | - $debug .= ' + ' . $GLOBALS['ip']; |
|
| 244 | + $debug .= ' + '.$GLOBALS['ip']; |
|
| 245 | 245 | } |
| 246 | - $debug = ' /* ' . mysqli_real_escape_string($link, str_replace('*/', '@/', $debug)) . ' */'; |
|
| 246 | + $debug = ' /* '.mysqli_real_escape_string($link, str_replace('*/', '@/', $debug)).' */'; |
|
| 247 | 247 | } |
| 248 | 248 | try { |
| 249 | - $r = mysqli_query($link, $query . $debug); |
|
| 249 | + $r = mysqli_query($link, $query.$debug); |
|
| 250 | 250 | } catch (\mysqli_sql_exception $e) { |
| 251 | - spip_log('mysqli_sql_exception: ' . $e->getMessage(), 'mysql.' . _LOG_DEBUG); |
|
| 251 | + spip_log('mysqli_sql_exception: '.$e->getMessage(), 'mysql.'._LOG_DEBUG); |
|
| 252 | 252 | $r = false; |
| 253 | 253 | // TODO: utiliser l’exception ensuite plutôt que les appels à spip_mysql_errno() |
| 254 | 254 | // mais il faut pour php < 8.1 forcer les exeptions via mysqli_report(). |
@@ -266,9 +266,9 @@ discard block |
||
| 266 | 266 | $link = $connexion['link']; |
| 267 | 267 | //On retente au cas où |
| 268 | 268 | try { |
| 269 | - $r = mysqli_query($link, $query . $debug); |
|
| 269 | + $r = mysqli_query($link, $query.$debug); |
|
| 270 | 270 | } catch (\mysqli_sql_exception $e) { |
| 271 | - spip_log('mysqli_sql_exception: ' . $e->getMessage(), 'mysql.' . _LOG_DEBUG); |
|
| 271 | + spip_log('mysqli_sql_exception: '.$e->getMessage(), 'mysql.'._LOG_DEBUG); |
|
| 272 | 272 | $r = false; |
| 273 | 273 | // TODO: utiliser l’exception ensuite plutôt que les appels à spip_mysql_errno() |
| 274 | 274 | // mais il faut pour php < 8.1 forcer les exeptions via mysqli_report(). |
@@ -301,7 +301,7 @@ discard block |
||
| 301 | 301 | // d'utiliser ceux-ci, copie-colle de phpmyadmin |
| 302 | 302 | $query = preg_replace(',^TABLE\s*`([^`]*)`,i', "TABLE \\1", $query); |
| 303 | 303 | |
| 304 | - return spip_mysql_query('ALTER ' . $query, $serveur, $requeter); # i.e. que PG se debrouille |
|
| 304 | + return spip_mysql_query('ALTER '.$query, $serveur, $requeter); # i.e. que PG se debrouille |
|
| 305 | 305 | } |
| 306 | 306 | |
| 307 | 307 | |
@@ -314,7 +314,7 @@ discard block |
||
| 314 | 314 | * @return bool Toujours true |
| 315 | 315 | */ |
| 316 | 316 | function spip_mysql_optimize($table, $serveur = '', $requeter = true) { |
| 317 | - spip_mysql_query('OPTIMIZE TABLE ' . $table); |
|
| 317 | + spip_mysql_query('OPTIMIZE TABLE '.$table); |
|
| 318 | 318 | |
| 319 | 319 | return true; |
| 320 | 320 | } |
@@ -337,7 +337,7 @@ discard block |
||
| 337 | 337 | $link = $connexion['link']; |
| 338 | 338 | $db = $connexion['db']; |
| 339 | 339 | |
| 340 | - $query = 'EXPLAIN ' . _mysql_traite_query($query, $db, $prefixe); |
|
| 340 | + $query = 'EXPLAIN '._mysql_traite_query($query, $db, $prefixe); |
|
| 341 | 341 | $r = mysqli_query($link, $query); |
| 342 | 342 | |
| 343 | 343 | return spip_mysql_fetch($r, null, $serveur); |
@@ -388,7 +388,7 @@ discard block |
||
| 388 | 388 | . calculer_mysql_expression('WHERE', $where) |
| 389 | 389 | . calculer_mysql_expression('GROUP BY', $groupby, ',') |
| 390 | 390 | . calculer_mysql_expression('HAVING', $having) |
| 391 | - . ($orderby ? ("\nORDER BY " . spip_mysql_order($orderby)) : '') |
|
| 391 | + . ($orderby ? ("\nORDER BY ".spip_mysql_order($orderby)) : '') |
|
| 392 | 392 | . ($limit ? "\nLIMIT $limit" : ''); |
| 393 | 393 | |
| 394 | 394 | // renvoyer la requete inerte si demandee |
@@ -478,12 +478,12 @@ discard block |
||
| 478 | 478 | $exp = "\n$expression "; |
| 479 | 479 | |
| 480 | 480 | if (!is_array($v)) { |
| 481 | - return $exp . $v; |
|
| 481 | + return $exp.$v; |
|
| 482 | 482 | } else { |
| 483 | 483 | if (strtoupper($join) === 'AND') { |
| 484 | - return $exp . join("\n\t$join ", array_map('calculer_mysql_where', $v)); |
|
| 484 | + return $exp.join("\n\t$join ", array_map('calculer_mysql_where', $v)); |
|
| 485 | 485 | } else { |
| 486 | - return $exp . join($join, $v); |
|
| 486 | + return $exp.join($join, $v); |
|
| 487 | 487 | } |
| 488 | 488 | } |
| 489 | 489 | } |
@@ -501,17 +501,17 @@ discard block |
||
| 501 | 501 | if (substr($k, -1) == '@') { |
| 502 | 502 | // c'est une jointure qui se refere au from precedent |
| 503 | 503 | // pas de virgule |
| 504 | - $res .= ' ' . $v; |
|
| 504 | + $res .= ' '.$v; |
|
| 505 | 505 | } else { |
| 506 | 506 | if (!is_numeric($k)) { |
| 507 | 507 | $p = strpos($v, ' '); |
| 508 | 508 | if ($p) { |
| 509 | - $v = substr($v, 0, $p) . " AS `$k`" . substr($v, $p); |
|
| 509 | + $v = substr($v, 0, $p)." AS `$k`".substr($v, $p); |
|
| 510 | 510 | } else { |
| 511 | 511 | $v .= " AS `$k`"; |
| 512 | 512 | } |
| 513 | 513 | } |
| 514 | - $res .= ', ' . $v; |
|
| 514 | + $res .= ', '.$v; |
|
| 515 | 515 | } |
| 516 | 516 | } |
| 517 | 517 | |
@@ -541,13 +541,13 @@ discard block |
||
| 541 | 541 | function _mysql_traite_query($query, $db = '', $prefixe = '', $echappe_textes = true) { |
| 542 | 542 | |
| 543 | 543 | if ($GLOBALS['mysql_rappel_nom_base'] and $db) { |
| 544 | - $pref = '`' . $db . '`.'; |
|
| 544 | + $pref = '`'.$db.'`.'; |
|
| 545 | 545 | } else { |
| 546 | 546 | $pref = ''; |
| 547 | 547 | } |
| 548 | 548 | |
| 549 | 549 | if ($prefixe) { |
| 550 | - $pref .= $prefixe . '_'; |
|
| 550 | + $pref .= $prefixe.'_'; |
|
| 551 | 551 | } |
| 552 | 552 | |
| 553 | 553 | if (!preg_match('/\s(SET|VALUES|WHERE|DATABASE)\s/i', $query, $regs)) { |
@@ -566,7 +566,7 @@ discard block |
||
| 566 | 566 | $suite_echap = $suite; |
| 567 | 567 | } |
| 568 | 568 | if (preg_match('/^(.*?)([(]\s*SELECT\b.*)$/si', $suite_echap, $r)) { |
| 569 | - $suite_echap = $r[1] . _mysql_traite_query($r[2], $db, $prefixe, false); |
|
| 569 | + $suite_echap = $r[1]._mysql_traite_query($r[2], $db, $prefixe, false); |
|
| 570 | 570 | if ($echappe_textes) { |
| 571 | 571 | $suite = query_reinjecte_textes($suite_echap, $textes); |
| 572 | 572 | } |
@@ -576,7 +576,7 @@ discard block |
||
| 576 | 576 | } |
| 577 | 577 | } |
| 578 | 578 | } |
| 579 | - $r = preg_replace(_SQL_PREFIXE_TABLE_MYSQL, '\1' . $pref, $query) . $suite; |
|
| 579 | + $r = preg_replace(_SQL_PREFIXE_TABLE_MYSQL, '\1'.$pref, $query).$suite; |
|
| 580 | 580 | |
| 581 | 581 | // en option, remplacer les emoji (que mysql ne sait pas gérer) en 💩 |
| 582 | 582 | // remplacer les emoji (que mysql ne sait pas gérer) en 💩 |
@@ -616,7 +616,7 @@ discard block |
||
| 616 | 616 | $ok = false; |
| 617 | 617 | } |
| 618 | 618 | if (!$ok) { |
| 619 | - spip_log('Echec mysqli_selectdb. Erreur : ' . mysqli_error($link), 'mysql.' . _LOG_CRITIQUE); |
|
| 619 | + spip_log('Echec mysqli_selectdb. Erreur : '.mysqli_error($link), 'mysql.'._LOG_CRITIQUE); |
|
| 620 | 620 | } |
| 621 | 621 | |
| 622 | 622 | return $ok; |
@@ -706,10 +706,10 @@ discard block |
||
| 706 | 706 | |
| 707 | 707 | $character_set = ''; |
| 708 | 708 | if (@$GLOBALS['meta']['charset_sql_base']) { |
| 709 | - $character_set .= ' CHARACTER SET ' . $GLOBALS['meta']['charset_sql_base']; |
|
| 709 | + $character_set .= ' CHARACTER SET '.$GLOBALS['meta']['charset_sql_base']; |
|
| 710 | 710 | } |
| 711 | 711 | if (@$GLOBALS['meta']['charset_collation_sql_base']) { |
| 712 | - $character_set .= ' COLLATE ' . $GLOBALS['meta']['charset_collation_sql_base']; |
|
| 712 | + $character_set .= ' COLLATE '.$GLOBALS['meta']['charset_collation_sql_base']; |
|
| 713 | 713 | } |
| 714 | 714 | |
| 715 | 715 | foreach ($champs as $k => $v) { |
@@ -719,7 +719,7 @@ discard block |
||
| 719 | 719 | preg_match(',(char|text),i', $defs[1]) |
| 720 | 720 | and !preg_match(',(binary|CHARACTER|COLLATE),i', $v) |
| 721 | 721 | ) { |
| 722 | - $v = $defs[1] . $character_set . ' ' . substr($v, strlen($defs[1])); |
|
| 722 | + $v = $defs[1].$character_set.' '.substr($v, strlen($defs[1])); |
|
| 723 | 723 | } |
| 724 | 724 | } |
| 725 | 725 | |
@@ -731,8 +731,8 @@ discard block |
||
| 731 | 731 | $s = ','; |
| 732 | 732 | } |
| 733 | 733 | $temporary = $temporary ? 'TEMPORARY' : ''; |
| 734 | - $q = "CREATE $temporary TABLE IF NOT EXISTS $nom ($query" . ($keys ? ",$keys" : '') . ')' |
|
| 735 | - . (defined('_MYSQL_ENGINE') ? ' ENGINE=' . _MYSQL_ENGINE : '') |
|
| 734 | + $q = "CREATE $temporary TABLE IF NOT EXISTS $nom ($query".($keys ? ",$keys" : '').')' |
|
| 735 | + . (defined('_MYSQL_ENGINE') ? ' ENGINE='._MYSQL_ENGINE : '') |
|
| 736 | 736 | . ($character_set ? " DEFAULT $character_set" : '') |
| 737 | 737 | . "\n"; |
| 738 | 738 | |
@@ -811,7 +811,7 @@ discard block |
||
| 811 | 811 | return false; |
| 812 | 812 | } |
| 813 | 813 | |
| 814 | - $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 814 | + $query = "CREATE VIEW $nom AS ".$query_select; |
|
| 815 | 815 | |
| 816 | 816 | return spip_mysql_query($query, $serveur, $requeter); |
| 817 | 817 | } |
@@ -869,7 +869,7 @@ discard block |
||
| 869 | 869 | * Ressource à utiliser avec sql_fetch() |
| 870 | 870 | **/ |
| 871 | 871 | function spip_mysql_showbase($match, $serveur = '', $requeter = true) { |
| 872 | - return spip_mysql_query('SHOW TABLES LIKE ' . _q($match), $serveur, $requeter); |
|
| 872 | + return spip_mysql_query('SHOW TABLES LIKE '._q($match), $serveur, $requeter); |
|
| 873 | 873 | } |
| 874 | 874 | |
| 875 | 875 | /** |
@@ -885,7 +885,7 @@ discard block |
||
| 885 | 885 | * - true si la requête a réussie, false sinon |
| 886 | 886 | */ |
| 887 | 887 | function spip_mysql_repair($table, $serveur = '', $requeter = true) { |
| 888 | - $table_status = spip_mysql_fetch(spip_mysql_query('SHOW TABLE STATUS WHERE Name = ' . _q($table), $serveur, true)); |
|
| 888 | + $table_status = spip_mysql_fetch(spip_mysql_query('SHOW TABLE STATUS WHERE Name = '._q($table), $serveur, true)); |
|
| 889 | 889 | $engine = $table_status['Engine']; |
| 890 | 890 | if ($engine == 'InnoDB') { |
| 891 | 891 | if (spip_mysql_alter("TABLE $table ENGINE = InnoDB", $serveur, $requeter)) { |
@@ -894,7 +894,7 @@ discard block |
||
| 894 | 894 | } elseif ($engine == 'MyISAM') { |
| 895 | 895 | return spip_mysql_query("REPAIR TABLE `$table`", $serveur, $requeter); |
| 896 | 896 | } else { |
| 897 | - spip_log("spip_mysql_repair impossible pour la table $table engine $engine", 'mysql.' . _LOG_DEBUG); |
|
| 897 | + spip_log("spip_mysql_repair impossible pour la table $table engine $engine", 'mysql.'._LOG_DEBUG); |
|
| 898 | 898 | } |
| 899 | 899 | return false; |
| 900 | 900 | } |
@@ -914,7 +914,7 @@ discard block |
||
| 914 | 914 | * - string : requete sql, si $requeter = true |
| 915 | 915 | **/ |
| 916 | 916 | function spip_mysql_table_exists(string $table, $serveur = '', $requeter = true) { |
| 917 | - $r = spip_mysql_query('SHOW TABLES LIKE ' . _q($table), $serveur, $requeter); |
|
| 917 | + $r = spip_mysql_query('SHOW TABLES LIKE '._q($table), $serveur, $requeter); |
|
| 918 | 918 | if (!$requeter) { |
| 919 | 919 | return $r; |
| 920 | 920 | } |
@@ -996,22 +996,22 @@ discard block |
||
| 996 | 996 | } |
| 997 | 997 | if ($val['Default'] === '0' || $val['Default']) { |
| 998 | 998 | if (preg_match('/[A-Z_]/', $val['Default'])) { |
| 999 | - $nfields[$val['Field']] .= ' DEFAULT ' . $val['Default']; |
|
| 999 | + $nfields[$val['Field']] .= ' DEFAULT '.$val['Default']; |
|
| 1000 | 1000 | } else { |
| 1001 | - $nfields[$val['Field']] .= " DEFAULT '" . $val['Default'] . "'"; |
|
| 1001 | + $nfields[$val['Field']] .= " DEFAULT '".$val['Default']."'"; |
|
| 1002 | 1002 | } |
| 1003 | 1003 | } |
| 1004 | 1004 | if ($val['Extra']) { |
| 1005 | - $nfields[$val['Field']] .= ' ' . $val['Extra']; |
|
| 1005 | + $nfields[$val['Field']] .= ' '.$val['Extra']; |
|
| 1006 | 1006 | } |
| 1007 | 1007 | if ($val['Key'] == 'PRI') { |
| 1008 | 1008 | $nkeys['PRIMARY KEY'] = $val['Field']; |
| 1009 | 1009 | } else { |
| 1010 | 1010 | if ($val['Key'] == 'MUL') { |
| 1011 | - $nkeys['KEY ' . $val['Field']] = $val['Field']; |
|
| 1011 | + $nkeys['KEY '.$val['Field']] = $val['Field']; |
|
| 1012 | 1012 | } else { |
| 1013 | 1013 | if ($val['Key'] == 'UNI') { |
| 1014 | - $nkeys['UNIQUE KEY ' . $val['Field']] = $val['Field']; |
|
| 1014 | + $nkeys['UNIQUE KEY '.$val['Field']] = $val['Field']; |
|
| 1015 | 1015 | } |
| 1016 | 1016 | } |
| 1017 | 1017 | } |
@@ -1087,7 +1087,7 @@ discard block |
||
| 1087 | 1087 | $serveur = '', |
| 1088 | 1088 | $requeter = true |
| 1089 | 1089 | ) { |
| 1090 | - $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 1090 | + $c = !$groupby ? '*' : ('DISTINCT '.(is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 1091 | 1091 | |
| 1092 | 1092 | $r = spip_mysql_select("COUNT($c)", $from, $where, '', '', '', $having, $serveur, $requeter); |
| 1093 | 1093 | if (!$requeter) { |
@@ -1127,7 +1127,7 @@ discard block |
||
| 1127 | 1127 | if ($s) { |
| 1128 | 1128 | $trace = debug_backtrace(); |
| 1129 | 1129 | if ($trace[0]['function'] != 'spip_mysql_error') { |
| 1130 | - spip_log("$s - $query - " . sql_error_backtrace(), 'mysql.' . _LOG_ERREUR); |
|
| 1130 | + spip_log("$s - $query - ".sql_error_backtrace(), 'mysql.'._LOG_ERREUR); |
|
| 1131 | 1131 | } |
| 1132 | 1132 | } |
| 1133 | 1133 | |
@@ -1257,7 +1257,7 @@ discard block |
||
| 1257 | 1257 | try { |
| 1258 | 1258 | $insert = mysqli_query($link, $query); |
| 1259 | 1259 | } catch (\mysqli_sql_exception $e) { |
| 1260 | - spip_log('mysqli_sql_exception: ' . $e->getMessage(), 'mysql.' . _LOG_DEBUG); |
|
| 1260 | + spip_log('mysqli_sql_exception: '.$e->getMessage(), 'mysql.'._LOG_DEBUG); |
|
| 1261 | 1261 | // TODO: utiliser l’exception ensuite plutôt que les appels à spip_mysql_errno() |
| 1262 | 1262 | // mais il faut pour php < 8.1 forcer les exeptions via mysqli_report(). |
| 1263 | 1263 | } |
@@ -1312,8 +1312,8 @@ discard block |
||
| 1312 | 1312 | |
| 1313 | 1313 | return spip_mysql_insert( |
| 1314 | 1314 | $table, |
| 1315 | - '(' . join(',', array_keys($couples)) . ')', |
|
| 1316 | - '(' . join(',', $couples) . ')', |
|
| 1315 | + '('.join(',', array_keys($couples)).')', |
|
| 1316 | + '('.join(',', $couples).')', |
|
| 1317 | 1317 | $desc, |
| 1318 | 1318 | $serveur, |
| 1319 | 1319 | $requeter |
@@ -1350,7 +1350,7 @@ discard block |
||
| 1350 | 1350 | } |
| 1351 | 1351 | $fields = $desc['field'] ?? []; |
| 1352 | 1352 | |
| 1353 | - $cles = '(' . join(',', array_keys(reset($tab_couples))) . ')'; |
|
| 1353 | + $cles = '('.join(',', array_keys(reset($tab_couples))).')'; |
|
| 1354 | 1354 | $valeurs = []; |
| 1355 | 1355 | $r = false; |
| 1356 | 1356 | |
@@ -1359,7 +1359,7 @@ discard block |
||
| 1359 | 1359 | foreach ($couples as $champ => $val) { |
| 1360 | 1360 | $couples[$champ] = spip_mysql_cite($val, $fields[$champ]); |
| 1361 | 1361 | } |
| 1362 | - $valeurs[] = '(' . join(',', $couples) . ')'; |
|
| 1362 | + $valeurs[] = '('.join(',', $couples).')'; |
|
| 1363 | 1363 | if (count($valeurs) >= 100) { |
| 1364 | 1364 | $r = spip_mysql_insert($table, $cles, join(', ', $valeurs), $desc, $serveur, $requeter); |
| 1365 | 1365 | $valeurs = []; |
@@ -1396,7 +1396,7 @@ discard block |
||
| 1396 | 1396 | function spip_mysql_update($table, $champs, $where = '', $desc = [], $serveur = '', $requeter = true) { |
| 1397 | 1397 | $set = []; |
| 1398 | 1398 | foreach ($champs as $champ => $val) { |
| 1399 | - $set[] = $champ . "=$val"; |
|
| 1399 | + $set[] = $champ."=$val"; |
|
| 1400 | 1400 | } |
| 1401 | 1401 | if (!empty($set)) { |
| 1402 | 1402 | return spip_mysql_query( |
@@ -1453,7 +1453,7 @@ discard block |
||
| 1453 | 1453 | } |
| 1454 | 1454 | $set = []; |
| 1455 | 1455 | foreach ($champs as $champ => $val) { |
| 1456 | - $set[] = $champ . '=' . spip_mysql_cite($val, @$fields[$champ]); |
|
| 1456 | + $set[] = $champ.'='.spip_mysql_cite($val, @$fields[$champ]); |
|
| 1457 | 1457 | } |
| 1458 | 1458 | |
| 1459 | 1459 | return spip_mysql_query( |
@@ -1522,10 +1522,10 @@ discard block |
||
| 1522 | 1522 | * - false en cas d'erreur. |
| 1523 | 1523 | **/ |
| 1524 | 1524 | function spip_mysql_replace($table, $couples, $desc = [], $serveur = '', $requeter = true) { |
| 1525 | - return spip_mysql_query("REPLACE $table (" . join(',', array_keys($couples)) . ') VALUES (' . join( |
|
| 1525 | + return spip_mysql_query("REPLACE $table (".join(',', array_keys($couples)).') VALUES ('.join( |
|
| 1526 | 1526 | ',', |
| 1527 | 1527 | array_map('_q', $couples) |
| 1528 | - ) . ')', $serveur, $requeter); |
|
| 1528 | + ).')', $serveur, $requeter); |
|
| 1529 | 1529 | } |
| 1530 | 1530 | |
| 1531 | 1531 | |
@@ -1554,10 +1554,10 @@ discard block |
||
| 1554 | 1554 | * - false en cas d'erreur. |
| 1555 | 1555 | **/ |
| 1556 | 1556 | function spip_mysql_replace_multi($table, $tab_couples, $desc = [], $serveur = '', $requeter = true) { |
| 1557 | - $cles = '(' . join(',', array_keys($tab_couples[0])) . ')'; |
|
| 1557 | + $cles = '('.join(',', array_keys($tab_couples[0])).')'; |
|
| 1558 | 1558 | $valeurs = []; |
| 1559 | 1559 | foreach ($tab_couples as $couples) { |
| 1560 | - $valeurs[] = '(' . join(',', array_map('_q', $couples)) . ')'; |
|
| 1560 | + $valeurs[] = '('.join(',', array_map('_q', $couples)).')'; |
|
| 1561 | 1561 | } |
| 1562 | 1562 | $valeurs = implode(', ', $valeurs); |
| 1563 | 1563 | |
@@ -1577,28 +1577,28 @@ discard block |
||
| 1577 | 1577 | */ |
| 1578 | 1578 | function spip_mysql_multi($objet, $lang) { |
| 1579 | 1579 | $lengthlang = strlen("[$lang]"); |
| 1580 | - $posmulti = 'INSTR(' . $objet . ", '<multi>')"; |
|
| 1581 | - $posfinmulti = 'INSTR(' . $objet . ", '</multi>')"; |
|
| 1582 | - $debutchaine = 'LEFT(' . $objet . ", $posmulti-1)"; |
|
| 1583 | - $finchaine = 'RIGHT(' . $objet . ', CHAR_LENGTH(' . $objet . ") -(7+$posfinmulti))"; |
|
| 1584 | - $chainemulti = 'TRIM(SUBSTRING(' . $objet . ", $posmulti+7, $posfinmulti -(7+$posmulti)))"; |
|
| 1585 | - $poslang = "INSTR($chainemulti,'[" . $lang . "]')"; |
|
| 1580 | + $posmulti = 'INSTR('.$objet.", '<multi>')"; |
|
| 1581 | + $posfinmulti = 'INSTR('.$objet.", '</multi>')"; |
|
| 1582 | + $debutchaine = 'LEFT('.$objet.", $posmulti-1)"; |
|
| 1583 | + $finchaine = 'RIGHT('.$objet.', CHAR_LENGTH('.$objet.") -(7+$posfinmulti))"; |
|
| 1584 | + $chainemulti = 'TRIM(SUBSTRING('.$objet.", $posmulti+7, $posfinmulti -(7+$posmulti)))"; |
|
| 1585 | + $poslang = "INSTR($chainemulti,'[".$lang."]')"; |
|
| 1586 | 1586 | $poslang = "IF($poslang=0,INSTR($chainemulti,']')+1,$poslang+$lengthlang)"; |
| 1587 | - $chainelang = 'TRIM(SUBSTRING(' . $objet . ", $posmulti+7+$poslang-1,$posfinmulti -($posmulti+7+$poslang-1) ))"; |
|
| 1588 | - $posfinlang = 'INSTR(' . $chainelang . ", '[')"; |
|
| 1587 | + $chainelang = 'TRIM(SUBSTRING('.$objet.", $posmulti+7+$poslang-1,$posfinmulti -($posmulti+7+$poslang-1) ))"; |
|
| 1588 | + $posfinlang = 'INSTR('.$chainelang.", '[')"; |
|
| 1589 | 1589 | $chainelang = "IF($posfinlang>0,LEFT($chainelang,$posfinlang-1),$chainelang)"; |
| 1590 | 1590 | //$chainelang = "LEFT($chainelang,$posfinlang-1)"; |
| 1591 | - $retour = "(TRIM(IF($posmulti = 0 , " . |
|
| 1592 | - ' TRIM(' . $objet . '), ' . |
|
| 1593 | - ' CONCAT( ' . |
|
| 1594 | - " $debutchaine, " . |
|
| 1595 | - ' IF( ' . |
|
| 1596 | - " $poslang = 0, " . |
|
| 1597 | - " $chainemulti, " . |
|
| 1598 | - " $chainelang" . |
|
| 1599 | - ' ), ' . |
|
| 1600 | - " $finchaine" . |
|
| 1601 | - ' ) ' . |
|
| 1591 | + $retour = "(TRIM(IF($posmulti = 0 , ". |
|
| 1592 | + ' TRIM('.$objet.'), '. |
|
| 1593 | + ' CONCAT( '. |
|
| 1594 | + " $debutchaine, ". |
|
| 1595 | + ' IF( '. |
|
| 1596 | + " $poslang = 0, ". |
|
| 1597 | + " $chainemulti, ". |
|
| 1598 | + " $chainelang". |
|
| 1599 | + ' ), '. |
|
| 1600 | + " $finchaine". |
|
| 1601 | + ' ) '. |
|
| 1602 | 1602 | '))) AS multi'; |
| 1603 | 1603 | |
| 1604 | 1604 | return $retour; |
@@ -1615,7 +1615,7 @@ discard block |
||
| 1615 | 1615 | * Valeur hexadécimale pour MySQL |
| 1616 | 1616 | **/ |
| 1617 | 1617 | function spip_mysql_hex($v) { |
| 1618 | - return '0x' . $v; |
|
| 1618 | + return '0x'.$v; |
|
| 1619 | 1619 | } |
| 1620 | 1620 | |
| 1621 | 1621 | /** |
@@ -1655,7 +1655,7 @@ discard block |
||
| 1655 | 1655 | * Expression SQL |
| 1656 | 1656 | **/ |
| 1657 | 1657 | function spip_mysql_date_proche($champ, $interval, $unite) { |
| 1658 | - $use_now = ( ($champ === 'maj' or strpos($champ, '.maj')) ? true : false ); |
|
| 1658 | + $use_now = (($champ === 'maj' or strpos($champ, '.maj')) ? true : false); |
|
| 1659 | 1659 | return '(' |
| 1660 | 1660 | . $champ |
| 1661 | 1661 | . (($interval <= 0) ? '>' : '<') |
@@ -1710,7 +1710,7 @@ discard block |
||
| 1710 | 1710 | } elseif ($v === null) { |
| 1711 | 1711 | return "''"; |
| 1712 | 1712 | } |
| 1713 | - return "'" . addslashes($v) . "'"; |
|
| 1713 | + return "'".addslashes($v)."'"; |
|
| 1714 | 1714 | } |
| 1715 | 1715 | |
| 1716 | 1716 | if ($v === null) { |
@@ -1734,7 +1734,7 @@ discard block |
||
| 1734 | 1734 | } |
| 1735 | 1735 | } |
| 1736 | 1736 | |
| 1737 | - return ("'" . addslashes($v) . "'"); |
|
| 1737 | + return ("'".addslashes($v)."'"); |
|
| 1738 | 1738 | } |
| 1739 | 1739 | |
| 1740 | 1740 | /** |
@@ -1768,7 +1768,7 @@ discard block |
||
| 1768 | 1768 | } else { |
| 1769 | 1769 | $GLOBALS['mysql_rappel_nom_base'] = false; |
| 1770 | 1770 | |
| 1771 | - return "\$GLOBALS['mysql_rappel_nom_base'] = false; " . |
|
| 1771 | + return "\$GLOBALS['mysql_rappel_nom_base'] = false; ". |
|
| 1772 | 1772 | "/* echec de test_rappel_nom_base_mysql a l'installation. */\n"; |
| 1773 | 1773 | } |
| 1774 | 1774 | } |
@@ -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 | define('_DEFAULT_DB', 'spip'); |
@@ -31,159 +31,159 @@ discard block |
||
| 31 | 31 | // si ca ne marche toujours pas, echec. |
| 32 | 32 | |
| 33 | 33 | function req_pg_dist($addr, $port, $login, $pass, $db = '', $prefixe = '') { |
| 34 | - static $last_connect = []; |
|
| 35 | - if (!extension_loaded('pgsql')) { |
|
| 36 | - return false; |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - // si provient de selectdb |
|
| 40 | - if (empty($addr) && empty($port) && empty($login) && empty($pass)) { |
|
| 41 | - foreach (['addr', 'port', 'login', 'pass', 'prefixe'] as $a) { |
|
| 42 | - ${$a} = $last_connect[$a]; |
|
| 43 | - } |
|
| 44 | - } |
|
| 45 | - [$host, $p] = array_pad(explode(';', $addr), 2, null); |
|
| 46 | - if ($p > 0) { |
|
| 47 | - $port = " port=$p"; |
|
| 48 | - } else { |
|
| 49 | - $port = ''; |
|
| 50 | - } |
|
| 51 | - $erreurs = []; |
|
| 52 | - if ($db) { |
|
| 53 | - @$link = pg_connect("host=$host$port dbname=$db user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW); |
|
| 54 | - } elseif (!@$link = pg_connect("host=$host$port user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW)) { |
|
| 55 | - $erreurs[] = pg_last_error(); |
|
| 56 | - if (@$link = pg_connect("host=$host$port dbname=$login user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW)) { |
|
| 57 | - $db = $login; |
|
| 58 | - } else { |
|
| 59 | - $erreurs[] = pg_last_error(); |
|
| 60 | - $db = _DEFAULT_DB; |
|
| 61 | - $link = pg_connect("host=$host$port dbname=$db user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW); |
|
| 62 | - } |
|
| 63 | - } |
|
| 64 | - if (!$link) { |
|
| 65 | - $erreurs[] = pg_last_error(); |
|
| 66 | - foreach ($erreurs as $e) { |
|
| 67 | - spip_log('Echec pg_connect. Erreur : ' . $e, 'pg.' . _LOG_HS); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - return false; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - if ($link) { |
|
| 74 | - $last_connect = [ |
|
| 75 | - 'addr' => $addr, |
|
| 76 | - 'port' => $port, |
|
| 77 | - 'login' => $login, |
|
| 78 | - 'pass' => $pass, |
|
| 79 | - 'db' => $db, |
|
| 80 | - 'prefixe' => $prefixe, |
|
| 81 | - ]; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - spip_log( |
|
| 85 | - "Connexion vers $host, base $db, prefixe $prefixe " . ($link ? 'operationnelle' : 'impossible'), |
|
| 86 | - 'pg.' . _LOG_DEBUG |
|
| 87 | - ); |
|
| 88 | - |
|
| 89 | - return !$link ? false : [ |
|
| 90 | - 'db' => $db, |
|
| 91 | - 'prefixe' => $prefixe ?: $db, |
|
| 92 | - 'link' => $link, |
|
| 93 | - ]; |
|
| 34 | + static $last_connect = []; |
|
| 35 | + if (!extension_loaded('pgsql')) { |
|
| 36 | + return false; |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + // si provient de selectdb |
|
| 40 | + if (empty($addr) && empty($port) && empty($login) && empty($pass)) { |
|
| 41 | + foreach (['addr', 'port', 'login', 'pass', 'prefixe'] as $a) { |
|
| 42 | + ${$a} = $last_connect[$a]; |
|
| 43 | + } |
|
| 44 | + } |
|
| 45 | + [$host, $p] = array_pad(explode(';', $addr), 2, null); |
|
| 46 | + if ($p > 0) { |
|
| 47 | + $port = " port=$p"; |
|
| 48 | + } else { |
|
| 49 | + $port = ''; |
|
| 50 | + } |
|
| 51 | + $erreurs = []; |
|
| 52 | + if ($db) { |
|
| 53 | + @$link = pg_connect("host=$host$port dbname=$db user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW); |
|
| 54 | + } elseif (!@$link = pg_connect("host=$host$port user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW)) { |
|
| 55 | + $erreurs[] = pg_last_error(); |
|
| 56 | + if (@$link = pg_connect("host=$host$port dbname=$login user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW)) { |
|
| 57 | + $db = $login; |
|
| 58 | + } else { |
|
| 59 | + $erreurs[] = pg_last_error(); |
|
| 60 | + $db = _DEFAULT_DB; |
|
| 61 | + $link = pg_connect("host=$host$port dbname=$db user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW); |
|
| 62 | + } |
|
| 63 | + } |
|
| 64 | + if (!$link) { |
|
| 65 | + $erreurs[] = pg_last_error(); |
|
| 66 | + foreach ($erreurs as $e) { |
|
| 67 | + spip_log('Echec pg_connect. Erreur : ' . $e, 'pg.' . _LOG_HS); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + return false; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + if ($link) { |
|
| 74 | + $last_connect = [ |
|
| 75 | + 'addr' => $addr, |
|
| 76 | + 'port' => $port, |
|
| 77 | + 'login' => $login, |
|
| 78 | + 'pass' => $pass, |
|
| 79 | + 'db' => $db, |
|
| 80 | + 'prefixe' => $prefixe, |
|
| 81 | + ]; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + spip_log( |
|
| 85 | + "Connexion vers $host, base $db, prefixe $prefixe " . ($link ? 'operationnelle' : 'impossible'), |
|
| 86 | + 'pg.' . _LOG_DEBUG |
|
| 87 | + ); |
|
| 88 | + |
|
| 89 | + return !$link ? false : [ |
|
| 90 | + 'db' => $db, |
|
| 91 | + 'prefixe' => $prefixe ?: $db, |
|
| 92 | + 'link' => $link, |
|
| 93 | + ]; |
|
| 94 | 94 | } |
| 95 | 95 | |
| 96 | 96 | $GLOBALS['spip_pg_functions_1'] = [ |
| 97 | - 'alter' => 'spip_pg_alter', |
|
| 98 | - 'count' => 'spip_pg_count', |
|
| 99 | - 'countsel' => 'spip_pg_countsel', |
|
| 100 | - 'create' => 'spip_pg_create', |
|
| 101 | - 'create_base' => 'spip_pg_create_base', |
|
| 102 | - 'create_view' => 'spip_pg_create_view', |
|
| 103 | - 'date_proche' => 'spip_pg_date_proche', |
|
| 104 | - 'delete' => 'spip_pg_delete', |
|
| 105 | - 'drop_table' => 'spip_pg_drop_table', |
|
| 106 | - 'drop_view' => 'spip_pg_drop_view', |
|
| 107 | - 'errno' => 'spip_pg_errno', |
|
| 108 | - 'error' => 'spip_pg_error', |
|
| 109 | - 'explain' => 'spip_pg_explain', |
|
| 110 | - 'fetch' => 'spip_pg_fetch', |
|
| 111 | - 'seek' => 'spip_pg_seek', |
|
| 112 | - 'free' => 'spip_pg_free', |
|
| 113 | - 'hex' => 'spip_pg_hex', |
|
| 114 | - 'in' => 'spip_pg_in', |
|
| 115 | - 'insert' => 'spip_pg_insert', |
|
| 116 | - 'insertq' => 'spip_pg_insertq', |
|
| 117 | - 'insertq_multi' => 'spip_pg_insertq_multi', |
|
| 118 | - 'listdbs' => 'spip_pg_listdbs', |
|
| 119 | - 'multi' => 'spip_pg_multi', |
|
| 120 | - 'optimize' => 'spip_pg_optimize', |
|
| 121 | - 'query' => 'spip_pg_query', |
|
| 122 | - 'quote' => 'spip_pg_quote', |
|
| 123 | - 'replace' => 'spip_pg_replace', |
|
| 124 | - 'replace_multi' => 'spip_pg_replace_multi', |
|
| 125 | - 'select' => 'spip_pg_select', |
|
| 126 | - 'selectdb' => 'spip_pg_selectdb', |
|
| 127 | - 'set_connect_charset' => 'spip_pg_set_connect_charset', |
|
| 128 | - 'showbase' => 'spip_pg_showbase', |
|
| 129 | - 'showtable' => 'spip_pg_showtable', |
|
| 130 | - 'update' => 'spip_pg_update', |
|
| 131 | - 'updateq' => 'spip_pg_updateq', |
|
| 97 | + 'alter' => 'spip_pg_alter', |
|
| 98 | + 'count' => 'spip_pg_count', |
|
| 99 | + 'countsel' => 'spip_pg_countsel', |
|
| 100 | + 'create' => 'spip_pg_create', |
|
| 101 | + 'create_base' => 'spip_pg_create_base', |
|
| 102 | + 'create_view' => 'spip_pg_create_view', |
|
| 103 | + 'date_proche' => 'spip_pg_date_proche', |
|
| 104 | + 'delete' => 'spip_pg_delete', |
|
| 105 | + 'drop_table' => 'spip_pg_drop_table', |
|
| 106 | + 'drop_view' => 'spip_pg_drop_view', |
|
| 107 | + 'errno' => 'spip_pg_errno', |
|
| 108 | + 'error' => 'spip_pg_error', |
|
| 109 | + 'explain' => 'spip_pg_explain', |
|
| 110 | + 'fetch' => 'spip_pg_fetch', |
|
| 111 | + 'seek' => 'spip_pg_seek', |
|
| 112 | + 'free' => 'spip_pg_free', |
|
| 113 | + 'hex' => 'spip_pg_hex', |
|
| 114 | + 'in' => 'spip_pg_in', |
|
| 115 | + 'insert' => 'spip_pg_insert', |
|
| 116 | + 'insertq' => 'spip_pg_insertq', |
|
| 117 | + 'insertq_multi' => 'spip_pg_insertq_multi', |
|
| 118 | + 'listdbs' => 'spip_pg_listdbs', |
|
| 119 | + 'multi' => 'spip_pg_multi', |
|
| 120 | + 'optimize' => 'spip_pg_optimize', |
|
| 121 | + 'query' => 'spip_pg_query', |
|
| 122 | + 'quote' => 'spip_pg_quote', |
|
| 123 | + 'replace' => 'spip_pg_replace', |
|
| 124 | + 'replace_multi' => 'spip_pg_replace_multi', |
|
| 125 | + 'select' => 'spip_pg_select', |
|
| 126 | + 'selectdb' => 'spip_pg_selectdb', |
|
| 127 | + 'set_connect_charset' => 'spip_pg_set_connect_charset', |
|
| 128 | + 'showbase' => 'spip_pg_showbase', |
|
| 129 | + 'showtable' => 'spip_pg_showtable', |
|
| 130 | + 'update' => 'spip_pg_update', |
|
| 131 | + 'updateq' => 'spip_pg_updateq', |
|
| 132 | 132 | ]; |
| 133 | 133 | |
| 134 | 134 | // Par ou ca passe une fois les traductions faites |
| 135 | 135 | function spip_pg_trace_query($query, $serveur = '') { |
| 136 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 137 | - $prefixe = $connexion['prefixe']; |
|
| 138 | - $link = $connexion['link']; |
|
| 139 | - $db = $connexion['db']; |
|
| 140 | - |
|
| 141 | - if (isset($_GET['var_profile'])) { |
|
| 142 | - include_spip('public/tracer'); |
|
| 143 | - $t = trace_query_start(); |
|
| 144 | - $e = ''; |
|
| 145 | - } else { |
|
| 146 | - $t = 0; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - $connexion['last'] = $query; |
|
| 150 | - $r = spip_pg_query_simple($link, $query); |
|
| 151 | - |
|
| 152 | - // Log de l'erreur eventuelle |
|
| 153 | - if ($e = spip_pg_errno($serveur)) { |
|
| 154 | - $e .= spip_pg_error($query, $serveur); |
|
| 155 | - } // et du fautif |
|
| 156 | - return $t ? trace_query_end($query, $t, $r, $e, $serveur) : $r; |
|
| 136 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 137 | + $prefixe = $connexion['prefixe']; |
|
| 138 | + $link = $connexion['link']; |
|
| 139 | + $db = $connexion['db']; |
|
| 140 | + |
|
| 141 | + if (isset($_GET['var_profile'])) { |
|
| 142 | + include_spip('public/tracer'); |
|
| 143 | + $t = trace_query_start(); |
|
| 144 | + $e = ''; |
|
| 145 | + } else { |
|
| 146 | + $t = 0; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + $connexion['last'] = $query; |
|
| 150 | + $r = spip_pg_query_simple($link, $query); |
|
| 151 | + |
|
| 152 | + // Log de l'erreur eventuelle |
|
| 153 | + if ($e = spip_pg_errno($serveur)) { |
|
| 154 | + $e .= spip_pg_error($query, $serveur); |
|
| 155 | + } // et du fautif |
|
| 156 | + return $t ? trace_query_end($query, $t, $r, $e, $serveur) : $r; |
|
| 157 | 157 | } |
| 158 | 158 | |
| 159 | 159 | // Fonction de requete generale quand on est sur que c'est SQL standard. |
| 160 | 160 | // Elle change juste le noms des tables ($table_prefix) dans le FROM etc |
| 161 | 161 | |
| 162 | 162 | function spip_pg_query($query, $serveur = '', $requeter = true) { |
| 163 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 164 | - $prefixe = $connexion['prefixe']; |
|
| 165 | - $link = $connexion['link']; |
|
| 166 | - $db = $connexion['db']; |
|
| 167 | - |
|
| 168 | - if (preg_match('/\s(SET|VALUES|WHERE|DATABASE)\s/i', $query, $regs)) { |
|
| 169 | - $suite = strstr($query, (string) $regs[0]); |
|
| 170 | - $query = substr($query, 0, -strlen($suite)); |
|
| 171 | - } else { |
|
| 172 | - $suite = ''; |
|
| 173 | - } |
|
| 174 | - $query = preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', $query) . $suite; |
|
| 175 | - |
|
| 176 | - // renvoyer la requete inerte si demandee |
|
| 177 | - if (!$requeter) { |
|
| 178 | - return $query; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - return spip_pg_trace_query($query, $serveur); |
|
| 163 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 164 | + $prefixe = $connexion['prefixe']; |
|
| 165 | + $link = $connexion['link']; |
|
| 166 | + $db = $connexion['db']; |
|
| 167 | + |
|
| 168 | + if (preg_match('/\s(SET|VALUES|WHERE|DATABASE)\s/i', $query, $regs)) { |
|
| 169 | + $suite = strstr($query, (string) $regs[0]); |
|
| 170 | + $query = substr($query, 0, -strlen($suite)); |
|
| 171 | + } else { |
|
| 172 | + $suite = ''; |
|
| 173 | + } |
|
| 174 | + $query = preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', $query) . $suite; |
|
| 175 | + |
|
| 176 | + // renvoyer la requete inerte si demandee |
|
| 177 | + if (!$requeter) { |
|
| 178 | + return $query; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + return spip_pg_trace_query($query, $serveur); |
|
| 182 | 182 | } |
| 183 | 183 | |
| 184 | 184 | function spip_pg_query_simple($link, $query) { |
| 185 | - #spip_log(var_export($query,true), 'pg.'._LOG_DEBUG); |
|
| 186 | - return pg_query($link, $query); |
|
| 185 | + #spip_log(var_export($query,true), 'pg.'._LOG_DEBUG); |
|
| 186 | + return pg_query($link, $query); |
|
| 187 | 187 | } |
| 188 | 188 | |
| 189 | 189 | /* |
@@ -195,196 +195,196 @@ discard block |
||
| 195 | 195 | * de requetes showtable intempestives |
| 196 | 196 | */ |
| 197 | 197 | function spip_pg_ajouter_champs_timestamp($table, $couples, $desc = '', $serveur = '') { |
| 198 | - static $tables = []; |
|
| 199 | - |
|
| 200 | - if (!isset($tables[$table])) { |
|
| 201 | - if (!$desc) { |
|
| 202 | - $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 203 | - $desc = $trouver_table($table, $serveur); |
|
| 204 | - // si pas de description, on ne fait rien, ou on die() ? |
|
| 205 | - if (!$desc) { |
|
| 206 | - return $couples; |
|
| 207 | - } |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - // recherche des champs avec simplement 'TIMESTAMP' |
|
| 211 | - // cependant, il faudra peut etre etendre |
|
| 212 | - // avec la gestion de DEFAULT et ON UPDATE |
|
| 213 | - // mais ceux-ci ne sont pas utilises dans le core |
|
| 214 | - $tables[$table] = []; |
|
| 215 | - foreach ($desc['field'] as $k => $v) { |
|
| 216 | - $v = strtolower(ltrim($v)); |
|
| 217 | - // ne pas ajouter de timestamp now() si un default est specifie |
|
| 218 | - if (strpos($v, 'timestamp') === 0 and !str_contains($v, 'default')) { |
|
| 219 | - $tables[$table][] = $k; |
|
| 220 | - } |
|
| 221 | - } |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - // ajout des champs type 'timestamp' absents |
|
| 225 | - foreach ($tables[$table] as $maj) { |
|
| 226 | - if (!array_key_exists($maj, $couples)) { |
|
| 227 | - $couples[$maj] = 'NOW()'; |
|
| 228 | - } |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - return $couples; |
|
| 198 | + static $tables = []; |
|
| 199 | + |
|
| 200 | + if (!isset($tables[$table])) { |
|
| 201 | + if (!$desc) { |
|
| 202 | + $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 203 | + $desc = $trouver_table($table, $serveur); |
|
| 204 | + // si pas de description, on ne fait rien, ou on die() ? |
|
| 205 | + if (!$desc) { |
|
| 206 | + return $couples; |
|
| 207 | + } |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + // recherche des champs avec simplement 'TIMESTAMP' |
|
| 211 | + // cependant, il faudra peut etre etendre |
|
| 212 | + // avec la gestion de DEFAULT et ON UPDATE |
|
| 213 | + // mais ceux-ci ne sont pas utilises dans le core |
|
| 214 | + $tables[$table] = []; |
|
| 215 | + foreach ($desc['field'] as $k => $v) { |
|
| 216 | + $v = strtolower(ltrim($v)); |
|
| 217 | + // ne pas ajouter de timestamp now() si un default est specifie |
|
| 218 | + if (strpos($v, 'timestamp') === 0 and !str_contains($v, 'default')) { |
|
| 219 | + $tables[$table][] = $k; |
|
| 220 | + } |
|
| 221 | + } |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + // ajout des champs type 'timestamp' absents |
|
| 225 | + foreach ($tables[$table] as $maj) { |
|
| 226 | + if (!array_key_exists($maj, $couples)) { |
|
| 227 | + $couples[$maj] = 'NOW()'; |
|
| 228 | + } |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + return $couples; |
|
| 232 | 232 | } |
| 233 | 233 | |
| 234 | 234 | |
| 235 | 235 | // Alter en PG ne traite pas les index |
| 236 | 236 | function spip_pg_alter($query, $serveur = '', $requeter = true) { |
| 237 | - // il faudrait une regexp pour eviter de spliter ADD PRIMARY KEY (colA, colB) |
|
| 238 | - // tout en cassant en deux alter distincts "ADD PRIMARY KEY (colA, colB), ADD INDEX (chose)"... |
|
| 239 | - // ou revoir l'api de sql_alter en creant un |
|
| 240 | - // sql_alter_table($table,array($actions)); |
|
| 241 | - if (!preg_match('/\s*((\s*IGNORE)?\s*TABLE\s*([^\s]*))\s*(.*)?/is', $query, $regs)) { |
|
| 242 | - spip_log("$query mal comprise", 'pg.' . _LOG_ERREUR); |
|
| 243 | - |
|
| 244 | - return false; |
|
| 245 | - } |
|
| 246 | - $debut = $regs[1]; |
|
| 247 | - $table = $regs[3]; |
|
| 248 | - $suite = $regs[4]; |
|
| 249 | - $todo = explode(',', $suite); |
|
| 250 | - // on remet les morceaux dechires ensembles... que c'est laid ! |
|
| 251 | - $todo2 = []; |
|
| 252 | - $i = 0; |
|
| 253 | - $ouverte = false; |
|
| 254 | - while ($do = array_shift($todo)) { |
|
| 255 | - $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . ',' . $do : $do; |
|
| 256 | - $o = (str_contains($do, '(')); |
|
| 257 | - $f = (str_contains($do, ')')); |
|
| 258 | - if ($o and !$f) { |
|
| 259 | - $ouverte = true; |
|
| 260 | - } elseif ($f) { |
|
| 261 | - $ouverte = false; |
|
| 262 | - } |
|
| 263 | - if (!$ouverte) { |
|
| 264 | - $i++; |
|
| 265 | - } |
|
| 266 | - } |
|
| 267 | - $todo = $todo2; |
|
| 268 | - $query = $debut . ' ' . array_shift($todo); |
|
| 269 | - |
|
| 270 | - if (!preg_match('/^\s*(IGNORE\s*)?TABLE\s+(\w+)\s+(ADD|DROP|CHANGE|MODIFY|RENAME)\s*(.*)$/is', $query, $r)) { |
|
| 271 | - spip_log("$query incompris", 'pg.' . _LOG_ERREUR); |
|
| 272 | - } else { |
|
| 273 | - if ($r[1]) { |
|
| 274 | - spip_log("j'ignore IGNORE dans $query", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 275 | - } |
|
| 276 | - $f = 'spip_pg_alter_' . strtolower($r[3]); |
|
| 277 | - if (function_exists($f)) { |
|
| 278 | - $f($r[2], $r[4], $serveur, $requeter); |
|
| 279 | - } else { |
|
| 280 | - spip_log("$query non prevu", 'pg.' . _LOG_ERREUR); |
|
| 281 | - } |
|
| 282 | - } |
|
| 283 | - // Alter a plusieurs args. Faudrait optimiser. |
|
| 284 | - if ($todo) { |
|
| 285 | - spip_pg_alter("TABLE $table " . join(',', $todo)); |
|
| 286 | - } |
|
| 237 | + // il faudrait une regexp pour eviter de spliter ADD PRIMARY KEY (colA, colB) |
|
| 238 | + // tout en cassant en deux alter distincts "ADD PRIMARY KEY (colA, colB), ADD INDEX (chose)"... |
|
| 239 | + // ou revoir l'api de sql_alter en creant un |
|
| 240 | + // sql_alter_table($table,array($actions)); |
|
| 241 | + if (!preg_match('/\s*((\s*IGNORE)?\s*TABLE\s*([^\s]*))\s*(.*)?/is', $query, $regs)) { |
|
| 242 | + spip_log("$query mal comprise", 'pg.' . _LOG_ERREUR); |
|
| 243 | + |
|
| 244 | + return false; |
|
| 245 | + } |
|
| 246 | + $debut = $regs[1]; |
|
| 247 | + $table = $regs[3]; |
|
| 248 | + $suite = $regs[4]; |
|
| 249 | + $todo = explode(',', $suite); |
|
| 250 | + // on remet les morceaux dechires ensembles... que c'est laid ! |
|
| 251 | + $todo2 = []; |
|
| 252 | + $i = 0; |
|
| 253 | + $ouverte = false; |
|
| 254 | + while ($do = array_shift($todo)) { |
|
| 255 | + $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . ',' . $do : $do; |
|
| 256 | + $o = (str_contains($do, '(')); |
|
| 257 | + $f = (str_contains($do, ')')); |
|
| 258 | + if ($o and !$f) { |
|
| 259 | + $ouverte = true; |
|
| 260 | + } elseif ($f) { |
|
| 261 | + $ouverte = false; |
|
| 262 | + } |
|
| 263 | + if (!$ouverte) { |
|
| 264 | + $i++; |
|
| 265 | + } |
|
| 266 | + } |
|
| 267 | + $todo = $todo2; |
|
| 268 | + $query = $debut . ' ' . array_shift($todo); |
|
| 269 | + |
|
| 270 | + if (!preg_match('/^\s*(IGNORE\s*)?TABLE\s+(\w+)\s+(ADD|DROP|CHANGE|MODIFY|RENAME)\s*(.*)$/is', $query, $r)) { |
|
| 271 | + spip_log("$query incompris", 'pg.' . _LOG_ERREUR); |
|
| 272 | + } else { |
|
| 273 | + if ($r[1]) { |
|
| 274 | + spip_log("j'ignore IGNORE dans $query", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 275 | + } |
|
| 276 | + $f = 'spip_pg_alter_' . strtolower($r[3]); |
|
| 277 | + if (function_exists($f)) { |
|
| 278 | + $f($r[2], $r[4], $serveur, $requeter); |
|
| 279 | + } else { |
|
| 280 | + spip_log("$query non prevu", 'pg.' . _LOG_ERREUR); |
|
| 281 | + } |
|
| 282 | + } |
|
| 283 | + // Alter a plusieurs args. Faudrait optimiser. |
|
| 284 | + if ($todo) { |
|
| 285 | + spip_pg_alter("TABLE $table " . join(',', $todo)); |
|
| 286 | + } |
|
| 287 | 287 | } |
| 288 | 288 | |
| 289 | 289 | function spip_pg_alter_change($table, $arg, $serveur = '', $requeter = true) { |
| 290 | - if (!preg_match('/^`?(\w+)`?\s+`?(\w+)`?\s+(.*?)\s*(DEFAULT .*?)?(NOT\s+NULL)?\s*(DEFAULT .*?)?$/i', $arg, $r)) { |
|
| 291 | - spip_log("alter change: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 292 | - } else { |
|
| 293 | - [, $old, $new, $type, $default, $null, $def2] = $r; |
|
| 294 | - $actions = ["ALTER $old TYPE " . mysql2pg_type($type)]; |
|
| 295 | - if ($null) { |
|
| 296 | - $actions[] = "ALTER $old SET NOT NULL"; |
|
| 297 | - } else { |
|
| 298 | - $actions[] = "ALTER $old DROP NOT NULL"; |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - if ($d = ($default ?: $def2)) { |
|
| 302 | - $actions[] = "ALTER $old SET $d"; |
|
| 303 | - } else { |
|
| 304 | - $actions[] = "ALTER $old DROP DEFAULT"; |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - spip_pg_query("ALTER TABLE $table " . join(', ', $actions)); |
|
| 308 | - |
|
| 309 | - if ($old != $new) { |
|
| 310 | - spip_pg_query("ALTER TABLE $table RENAME $old TO $new", $serveur); |
|
| 311 | - } |
|
| 312 | - } |
|
| 290 | + if (!preg_match('/^`?(\w+)`?\s+`?(\w+)`?\s+(.*?)\s*(DEFAULT .*?)?(NOT\s+NULL)?\s*(DEFAULT .*?)?$/i', $arg, $r)) { |
|
| 291 | + spip_log("alter change: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 292 | + } else { |
|
| 293 | + [, $old, $new, $type, $default, $null, $def2] = $r; |
|
| 294 | + $actions = ["ALTER $old TYPE " . mysql2pg_type($type)]; |
|
| 295 | + if ($null) { |
|
| 296 | + $actions[] = "ALTER $old SET NOT NULL"; |
|
| 297 | + } else { |
|
| 298 | + $actions[] = "ALTER $old DROP NOT NULL"; |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + if ($d = ($default ?: $def2)) { |
|
| 302 | + $actions[] = "ALTER $old SET $d"; |
|
| 303 | + } else { |
|
| 304 | + $actions[] = "ALTER $old DROP DEFAULT"; |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + spip_pg_query("ALTER TABLE $table " . join(', ', $actions)); |
|
| 308 | + |
|
| 309 | + if ($old != $new) { |
|
| 310 | + spip_pg_query("ALTER TABLE $table RENAME $old TO $new", $serveur); |
|
| 311 | + } |
|
| 312 | + } |
|
| 313 | 313 | } |
| 314 | 314 | |
| 315 | 315 | function spip_pg_alter_add($table, $arg, $serveur = '', $requeter = true) { |
| 316 | - $nom_index = null; |
|
| 317 | - if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*(.*)$/', $arg, $r)) { |
|
| 318 | - spip_log("alter add $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 319 | - |
|
| 320 | - return null; |
|
| 321 | - } |
|
| 322 | - if (!$r[1] or $r[1] == 'COLUMN') { |
|
| 323 | - preg_match('/`?(\w+)`?(.*)/', $r[2], $m); |
|
| 324 | - if (preg_match('/^(.*)(BEFORE|AFTER|FIRST)(.*)$/is', $m[2], $n)) { |
|
| 325 | - $m[2] = $n[1]; |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - return spip_pg_query("ALTER TABLE $table ADD " . $m[1] . ' ' . mysql2pg_type($m[2]), $serveur, $requeter); |
|
| 329 | - } elseif ($r[1][0] == 'P') { |
|
| 330 | - // la primary peut etre sur plusieurs champs |
|
| 331 | - $r[2] = trim(str_replace('`', '', $r[2])); |
|
| 332 | - $m = ($r[2][0] == '(') ? substr($r[2], 1, -1) : $r[2]; |
|
| 333 | - |
|
| 334 | - return spip_pg_query( |
|
| 335 | - "ALTER TABLE $table ADD CONSTRAINT $table" . '_pkey PRIMARY KEY (' . $m . ')', |
|
| 336 | - $serveur, |
|
| 337 | - $requeter |
|
| 338 | - ); |
|
| 339 | - } else { |
|
| 340 | - preg_match('/([^\s,]*)\s*(.*)?/', $r[2], $m); |
|
| 341 | - // peut etre "(colonne)" ou "nom_index (colonnes)" |
|
| 342 | - // bug potentiel si qqn met "(colonne, colonne)" |
|
| 343 | - // |
|
| 344 | - // nom_index (colonnes) |
|
| 345 | - if ($m[2]) { |
|
| 346 | - $colonnes = substr($m[2], 1, -1); |
|
| 347 | - $nom_index = $m[1]; |
|
| 348 | - } else { |
|
| 349 | - // (colonne) |
|
| 350 | - if ($m[1][0] == '(') { |
|
| 351 | - $colonnes = substr($m[1], 1, -1); |
|
| 352 | - if (str_contains(',', $colonnes)) { |
|
| 353 | - spip_log('PG : Erreur, impossible de creer un index sur plusieurs colonnes' |
|
| 354 | - . " sans qu'il ait de nom ($table, ($colonnes))", 'pg.' . _LOG_ERREUR); |
|
| 355 | - } else { |
|
| 356 | - $nom_index = $colonnes; |
|
| 357 | - } |
|
| 358 | - } // nom_index |
|
| 359 | - else { |
|
| 360 | - $nom_index = $colonnes = $m[1]; |
|
| 361 | - } |
|
| 362 | - } |
|
| 363 | - |
|
| 364 | - return spip_pg_create_index($nom_index, $table, $colonnes, $serveur, $requeter); |
|
| 365 | - } |
|
| 316 | + $nom_index = null; |
|
| 317 | + if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*(.*)$/', $arg, $r)) { |
|
| 318 | + spip_log("alter add $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 319 | + |
|
| 320 | + return null; |
|
| 321 | + } |
|
| 322 | + if (!$r[1] or $r[1] == 'COLUMN') { |
|
| 323 | + preg_match('/`?(\w+)`?(.*)/', $r[2], $m); |
|
| 324 | + if (preg_match('/^(.*)(BEFORE|AFTER|FIRST)(.*)$/is', $m[2], $n)) { |
|
| 325 | + $m[2] = $n[1]; |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + return spip_pg_query("ALTER TABLE $table ADD " . $m[1] . ' ' . mysql2pg_type($m[2]), $serveur, $requeter); |
|
| 329 | + } elseif ($r[1][0] == 'P') { |
|
| 330 | + // la primary peut etre sur plusieurs champs |
|
| 331 | + $r[2] = trim(str_replace('`', '', $r[2])); |
|
| 332 | + $m = ($r[2][0] == '(') ? substr($r[2], 1, -1) : $r[2]; |
|
| 333 | + |
|
| 334 | + return spip_pg_query( |
|
| 335 | + "ALTER TABLE $table ADD CONSTRAINT $table" . '_pkey PRIMARY KEY (' . $m . ')', |
|
| 336 | + $serveur, |
|
| 337 | + $requeter |
|
| 338 | + ); |
|
| 339 | + } else { |
|
| 340 | + preg_match('/([^\s,]*)\s*(.*)?/', $r[2], $m); |
|
| 341 | + // peut etre "(colonne)" ou "nom_index (colonnes)" |
|
| 342 | + // bug potentiel si qqn met "(colonne, colonne)" |
|
| 343 | + // |
|
| 344 | + // nom_index (colonnes) |
|
| 345 | + if ($m[2]) { |
|
| 346 | + $colonnes = substr($m[2], 1, -1); |
|
| 347 | + $nom_index = $m[1]; |
|
| 348 | + } else { |
|
| 349 | + // (colonne) |
|
| 350 | + if ($m[1][0] == '(') { |
|
| 351 | + $colonnes = substr($m[1], 1, -1); |
|
| 352 | + if (str_contains(',', $colonnes)) { |
|
| 353 | + spip_log('PG : Erreur, impossible de creer un index sur plusieurs colonnes' |
|
| 354 | + . " sans qu'il ait de nom ($table, ($colonnes))", 'pg.' . _LOG_ERREUR); |
|
| 355 | + } else { |
|
| 356 | + $nom_index = $colonnes; |
|
| 357 | + } |
|
| 358 | + } // nom_index |
|
| 359 | + else { |
|
| 360 | + $nom_index = $colonnes = $m[1]; |
|
| 361 | + } |
|
| 362 | + } |
|
| 363 | + |
|
| 364 | + return spip_pg_create_index($nom_index, $table, $colonnes, $serveur, $requeter); |
|
| 365 | + } |
|
| 366 | 366 | } |
| 367 | 367 | |
| 368 | 368 | function spip_pg_alter_drop($table, $arg, $serveur = '', $requeter = true) { |
| 369 | - if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*`?(\w*)`?/', $arg, $r)) { |
|
| 370 | - spip_log("alter drop: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 371 | - } else { |
|
| 372 | - if (!$r[1] or $r[1] == 'COLUMN') { |
|
| 373 | - return spip_pg_query("ALTER TABLE $table DROP " . $r[2], $serveur); |
|
| 374 | - } elseif ($r[1][0] == 'P') { |
|
| 375 | - return spip_pg_query("ALTER TABLE $table DROP CONSTRAINT $table" . '_pkey', $serveur); |
|
| 376 | - } else { |
|
| 377 | - return spip_pg_query('DROP INDEX ' . $table . '_' . $r[2], $serveur); |
|
| 378 | - } |
|
| 379 | - } |
|
| 369 | + if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*`?(\w*)`?/', $arg, $r)) { |
|
| 370 | + spip_log("alter drop: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 371 | + } else { |
|
| 372 | + if (!$r[1] or $r[1] == 'COLUMN') { |
|
| 373 | + return spip_pg_query("ALTER TABLE $table DROP " . $r[2], $serveur); |
|
| 374 | + } elseif ($r[1][0] == 'P') { |
|
| 375 | + return spip_pg_query("ALTER TABLE $table DROP CONSTRAINT $table" . '_pkey', $serveur); |
|
| 376 | + } else { |
|
| 377 | + return spip_pg_query('DROP INDEX ' . $table . '_' . $r[2], $serveur); |
|
| 378 | + } |
|
| 379 | + } |
|
| 380 | 380 | } |
| 381 | 381 | |
| 382 | 382 | function spip_pg_alter_modify($table, $arg, $serveur = '', $requeter = true) { |
| 383 | - if (!preg_match('/^`?(\w+)`?\s+(.*)$/', $arg, $r)) { |
|
| 384 | - spip_log("alter modify: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 385 | - } else { |
|
| 386 | - return spip_pg_alter_change($table, $r[1] . ' ' . $arg, $serveur = '', $requeter = true); |
|
| 387 | - } |
|
| 383 | + if (!preg_match('/^`?(\w+)`?\s+(.*)$/', $arg, $r)) { |
|
| 384 | + spip_log("alter modify: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 385 | + } else { |
|
| 386 | + return spip_pg_alter_change($table, $r[1] . ' ' . $arg, $serveur = '', $requeter = true); |
|
| 387 | + } |
|
| 388 | 388 | } |
| 389 | 389 | |
| 390 | 390 | // attention (en pg) : |
@@ -392,17 +392,17 @@ discard block |
||
| 392 | 392 | // - alter table A rename X to Y = changer le nom de la colonne X en Y |
| 393 | 393 | // pour l'instant, traiter simplement RENAME TO X |
| 394 | 394 | function spip_pg_alter_rename($table, $arg, $serveur = '', $requeter = true) { |
| 395 | - $rename = ''; |
|
| 396 | - // si TO, mais pas au debut |
|
| 397 | - if (!stripos($arg, 'TO ')) { |
|
| 398 | - $rename = $arg; |
|
| 399 | - } elseif (preg_match('/^(TO)\s*`?(\w*)`?/', $arg, $r)) { |
|
| 400 | - $rename = $r[2]; |
|
| 401 | - } else { |
|
| 402 | - spip_log("alter rename: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - return $rename ? spip_pg_query("ALTER TABLE $table RENAME TO $rename") : false; |
|
| 395 | + $rename = ''; |
|
| 396 | + // si TO, mais pas au debut |
|
| 397 | + if (!stripos($arg, 'TO ')) { |
|
| 398 | + $rename = $arg; |
|
| 399 | + } elseif (preg_match('/^(TO)\s*`?(\w*)`?/', $arg, $r)) { |
|
| 400 | + $rename = $r[2]; |
|
| 401 | + } else { |
|
| 402 | + spip_log("alter rename: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + return $rename ? spip_pg_query("ALTER TABLE $table RENAME TO $rename") : false; |
|
| 406 | 406 | } |
| 407 | 407 | |
| 408 | 408 | |
@@ -418,59 +418,59 @@ discard block |
||
| 418 | 418 | * @return bool ou requete |
| 419 | 419 | */ |
| 420 | 420 | function spip_pg_create_index($nom, $table, $champs, $serveur = '', $requeter = true) { |
| 421 | - if (!($nom or $table or $champs)) { |
|
| 422 | - spip_log( |
|
| 423 | - "Champ manquant pour creer un index pg ($nom, $table, (" . @join(',', $champs) . '))', |
|
| 424 | - 'pg.' . _LOG_ERREUR |
|
| 425 | - ); |
|
| 426 | - |
|
| 427 | - return false; |
|
| 428 | - } |
|
| 429 | - |
|
| 430 | - $nom = str_replace('`', '', $nom); |
|
| 431 | - $champs = str_replace('`', '', $champs); |
|
| 432 | - |
|
| 433 | - // PG ne differentie pas noms des index en fonction des tables |
|
| 434 | - // il faut donc creer des noms uniques d'index pour une base pg |
|
| 435 | - $nom = $table . '_' . $nom; |
|
| 436 | - // enlever d'eventuelles parentheses deja presentes sur champs |
|
| 437 | - if (!is_array($champs)) { |
|
| 438 | - if ($champs[0] == '(') { |
|
| 439 | - $champs = substr($champs, 1, -1); |
|
| 440 | - } |
|
| 441 | - $champs = [$champs]; |
|
| 442 | - } |
|
| 443 | - $query = "CREATE INDEX $nom ON $table (" . join(',', $champs) . ')'; |
|
| 444 | - if (!$requeter) { |
|
| 445 | - return $query; |
|
| 446 | - } |
|
| 447 | - $res = spip_pg_query($query, $serveur, $requeter); |
|
| 448 | - |
|
| 449 | - return $res; |
|
| 421 | + if (!($nom or $table or $champs)) { |
|
| 422 | + spip_log( |
|
| 423 | + "Champ manquant pour creer un index pg ($nom, $table, (" . @join(',', $champs) . '))', |
|
| 424 | + 'pg.' . _LOG_ERREUR |
|
| 425 | + ); |
|
| 426 | + |
|
| 427 | + return false; |
|
| 428 | + } |
|
| 429 | + |
|
| 430 | + $nom = str_replace('`', '', $nom); |
|
| 431 | + $champs = str_replace('`', '', $champs); |
|
| 432 | + |
|
| 433 | + // PG ne differentie pas noms des index en fonction des tables |
|
| 434 | + // il faut donc creer des noms uniques d'index pour une base pg |
|
| 435 | + $nom = $table . '_' . $nom; |
|
| 436 | + // enlever d'eventuelles parentheses deja presentes sur champs |
|
| 437 | + if (!is_array($champs)) { |
|
| 438 | + if ($champs[0] == '(') { |
|
| 439 | + $champs = substr($champs, 1, -1); |
|
| 440 | + } |
|
| 441 | + $champs = [$champs]; |
|
| 442 | + } |
|
| 443 | + $query = "CREATE INDEX $nom ON $table (" . join(',', $champs) . ')'; |
|
| 444 | + if (!$requeter) { |
|
| 445 | + return $query; |
|
| 446 | + } |
|
| 447 | + $res = spip_pg_query($query, $serveur, $requeter); |
|
| 448 | + |
|
| 449 | + return $res; |
|
| 450 | 450 | } |
| 451 | 451 | |
| 452 | 452 | |
| 453 | 453 | function spip_pg_explain($query, $serveur = '', $requeter = true) { |
| 454 | - if (strpos(ltrim($query), 'SELECT') !== 0) { |
|
| 455 | - return []; |
|
| 456 | - } |
|
| 457 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 458 | - $prefixe = $connexion['prefixe']; |
|
| 459 | - $link = $connexion['link']; |
|
| 460 | - if (preg_match('/\s(SET|VALUES|WHERE)\s/i', $query, $regs)) { |
|
| 461 | - $suite = strstr($query, (string) $regs[0]); |
|
| 462 | - $query = substr($query, 0, -strlen($suite)); |
|
| 463 | - } else { |
|
| 464 | - $suite = ''; |
|
| 465 | - } |
|
| 466 | - $query = 'EXPLAIN ' . preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', $query) . $suite; |
|
| 467 | - |
|
| 468 | - if (!$requeter) { |
|
| 469 | - return $query; |
|
| 470 | - } |
|
| 471 | - $r = spip_pg_query_simple($link, $query); |
|
| 472 | - |
|
| 473 | - return spip_pg_fetch($r, null, $serveur); |
|
| 454 | + if (strpos(ltrim($query), 'SELECT') !== 0) { |
|
| 455 | + return []; |
|
| 456 | + } |
|
| 457 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 458 | + $prefixe = $connexion['prefixe']; |
|
| 459 | + $link = $connexion['link']; |
|
| 460 | + if (preg_match('/\s(SET|VALUES|WHERE)\s/i', $query, $regs)) { |
|
| 461 | + $suite = strstr($query, (string) $regs[0]); |
|
| 462 | + $query = substr($query, 0, -strlen($suite)); |
|
| 463 | + } else { |
|
| 464 | + $suite = ''; |
|
| 465 | + } |
|
| 466 | + $query = 'EXPLAIN ' . preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', $query) . $suite; |
|
| 467 | + |
|
| 468 | + if (!$requeter) { |
|
| 469 | + return $query; |
|
| 470 | + } |
|
| 471 | + $r = spip_pg_query_simple($link, $query); |
|
| 472 | + |
|
| 473 | + return spip_pg_fetch($r, null, $serveur); |
|
| 474 | 474 | } |
| 475 | 475 | |
| 476 | 476 | |
@@ -489,92 +489,92 @@ discard block |
||
| 489 | 489 | * - False en cas d'erreur. |
| 490 | 490 | **/ |
| 491 | 491 | function spip_pg_selectdb($db, $serveur = '', $requeter = true) { |
| 492 | - // se connecter a la base indiquee |
|
| 493 | - // avec les identifiants connus |
|
| 494 | - $index = $serveur ? strtolower($serveur) : 0; |
|
| 492 | + // se connecter a la base indiquee |
|
| 493 | + // avec les identifiants connus |
|
| 494 | + $index = $serveur ? strtolower($serveur) : 0; |
|
| 495 | 495 | |
| 496 | - if ($link = spip_connect_db('', '', '', '', $db, 'pg', '', '')) { |
|
| 497 | - if (($db == $link['db']) && $GLOBALS['connexions'][$index] = $link) { |
|
| 498 | - return $db; |
|
| 499 | - } |
|
| 500 | - } |
|
| 496 | + if ($link = spip_connect_db('', '', '', '', $db, 'pg', '', '')) { |
|
| 497 | + if (($db == $link['db']) && $GLOBALS['connexions'][$index] = $link) { |
|
| 498 | + return $db; |
|
| 499 | + } |
|
| 500 | + } |
|
| 501 | 501 | |
| 502 | - return false; |
|
| 502 | + return false; |
|
| 503 | 503 | } |
| 504 | 504 | |
| 505 | 505 | // Qu'une seule base pour le moment |
| 506 | 506 | |
| 507 | 507 | function spip_pg_listdbs($serveur) { |
| 508 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 509 | - $link = $connexion['link']; |
|
| 510 | - $dbs = []; |
|
| 511 | - $res = spip_pg_query_simple($link, 'select * From pg_database'); |
|
| 512 | - while ($row = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 513 | - $dbs[] = reset($row); |
|
| 514 | - } |
|
| 515 | - |
|
| 516 | - return $dbs; |
|
| 508 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 509 | + $link = $connexion['link']; |
|
| 510 | + $dbs = []; |
|
| 511 | + $res = spip_pg_query_simple($link, 'select * From pg_database'); |
|
| 512 | + while ($row = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 513 | + $dbs[] = reset($row); |
|
| 514 | + } |
|
| 515 | + |
|
| 516 | + return $dbs; |
|
| 517 | 517 | } |
| 518 | 518 | |
| 519 | 519 | function spip_pg_select( |
| 520 | - $select, |
|
| 521 | - $from, |
|
| 522 | - $where = '', |
|
| 523 | - $groupby = [], |
|
| 524 | - $orderby = '', |
|
| 525 | - $limit = '', |
|
| 526 | - $having = '', |
|
| 527 | - $serveur = '', |
|
| 528 | - $requeter = true |
|
| 520 | + $select, |
|
| 521 | + $from, |
|
| 522 | + $where = '', |
|
| 523 | + $groupby = [], |
|
| 524 | + $orderby = '', |
|
| 525 | + $limit = '', |
|
| 526 | + $having = '', |
|
| 527 | + $serveur = '', |
|
| 528 | + $requeter = true |
|
| 529 | 529 | ) { |
| 530 | 530 | |
| 531 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 532 | - $prefixe = $connexion['prefixe']; |
|
| 533 | - $link = $connexion['link']; |
|
| 534 | - $db = $connexion['db']; |
|
| 535 | - |
|
| 536 | - $limit = preg_match('/^\s*(([0-9]+),)?\s*([0-9]+)\s*$/', $limit, $limatch); |
|
| 537 | - if ($limit) { |
|
| 538 | - $offset = $limatch[2]; |
|
| 539 | - $count = $limatch[3]; |
|
| 540 | - } |
|
| 541 | - |
|
| 542 | - $select = spip_pg_frommysql($select); |
|
| 543 | - |
|
| 544 | - // si pas de tri explicitement demande, le GROUP BY ne |
|
| 545 | - // contient que la clef primaire. |
|
| 546 | - // lui ajouter alors le champ de tri par defaut |
|
| 547 | - if (preg_match('/FIELD\(([a-z]+\.[a-z]+),/i', $orderby[0], $groupbyplus)) { |
|
| 548 | - $groupby[] = $groupbyplus[1]; |
|
| 549 | - } |
|
| 550 | - |
|
| 551 | - $orderby = spip_pg_orderby($orderby, $select); |
|
| 552 | - |
|
| 553 | - if ($having) { |
|
| 554 | - if (is_array($having)) { |
|
| 555 | - $having = join("\n\tAND ", array_map('calculer_pg_where', $having)); |
|
| 556 | - } |
|
| 557 | - } |
|
| 558 | - $from = spip_pg_from($from, $prefixe); |
|
| 559 | - $query = 'SELECT ' . $select |
|
| 560 | - . (!$from ? '' : "\nFROM $from") |
|
| 561 | - . (!$where ? '' : ("\nWHERE " . (!is_array($where) ? calculer_pg_where($where) : (join( |
|
| 562 | - "\n\tAND ", |
|
| 563 | - array_map('calculer_pg_where', $where) |
|
| 564 | - ))))) |
|
| 565 | - . spip_pg_groupby($groupby, $from, $select) |
|
| 566 | - . (!$having ? '' : "\nHAVING $having") |
|
| 567 | - . ($orderby ? ("\nORDER BY $orderby") : '') |
|
| 568 | - . (!$limit ? '' : (" LIMIT $count" . (!$offset ? '' : " OFFSET $offset"))); |
|
| 569 | - |
|
| 570 | - // renvoyer la requete inerte si demandee |
|
| 571 | - if ($requeter === false) { |
|
| 572 | - return $query; |
|
| 573 | - } |
|
| 574 | - |
|
| 575 | - $r = spip_pg_trace_query($query, $serveur); |
|
| 576 | - |
|
| 577 | - return $r ?: $query; |
|
| 531 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 532 | + $prefixe = $connexion['prefixe']; |
|
| 533 | + $link = $connexion['link']; |
|
| 534 | + $db = $connexion['db']; |
|
| 535 | + |
|
| 536 | + $limit = preg_match('/^\s*(([0-9]+),)?\s*([0-9]+)\s*$/', $limit, $limatch); |
|
| 537 | + if ($limit) { |
|
| 538 | + $offset = $limatch[2]; |
|
| 539 | + $count = $limatch[3]; |
|
| 540 | + } |
|
| 541 | + |
|
| 542 | + $select = spip_pg_frommysql($select); |
|
| 543 | + |
|
| 544 | + // si pas de tri explicitement demande, le GROUP BY ne |
|
| 545 | + // contient que la clef primaire. |
|
| 546 | + // lui ajouter alors le champ de tri par defaut |
|
| 547 | + if (preg_match('/FIELD\(([a-z]+\.[a-z]+),/i', $orderby[0], $groupbyplus)) { |
|
| 548 | + $groupby[] = $groupbyplus[1]; |
|
| 549 | + } |
|
| 550 | + |
|
| 551 | + $orderby = spip_pg_orderby($orderby, $select); |
|
| 552 | + |
|
| 553 | + if ($having) { |
|
| 554 | + if (is_array($having)) { |
|
| 555 | + $having = join("\n\tAND ", array_map('calculer_pg_where', $having)); |
|
| 556 | + } |
|
| 557 | + } |
|
| 558 | + $from = spip_pg_from($from, $prefixe); |
|
| 559 | + $query = 'SELECT ' . $select |
|
| 560 | + . (!$from ? '' : "\nFROM $from") |
|
| 561 | + . (!$where ? '' : ("\nWHERE " . (!is_array($where) ? calculer_pg_where($where) : (join( |
|
| 562 | + "\n\tAND ", |
|
| 563 | + array_map('calculer_pg_where', $where) |
|
| 564 | + ))))) |
|
| 565 | + . spip_pg_groupby($groupby, $from, $select) |
|
| 566 | + . (!$having ? '' : "\nHAVING $having") |
|
| 567 | + . ($orderby ? ("\nORDER BY $orderby") : '') |
|
| 568 | + . (!$limit ? '' : (" LIMIT $count" . (!$offset ? '' : " OFFSET $offset"))); |
|
| 569 | + |
|
| 570 | + // renvoyer la requete inerte si demandee |
|
| 571 | + if ($requeter === false) { |
|
| 572 | + return $query; |
|
| 573 | + } |
|
| 574 | + |
|
| 575 | + $r = spip_pg_trace_query($query, $serveur); |
|
| 576 | + |
|
| 577 | + return $r ?: $query; |
|
| 578 | 578 | ; |
| 579 | 579 | } |
| 580 | 580 | |
@@ -582,26 +582,26 @@ discard block |
||
| 582 | 582 | // car le reste de la requete utilise les alias (AS) systematiquement |
| 583 | 583 | |
| 584 | 584 | function spip_pg_from($from, $prefixe) { |
| 585 | - if (is_array($from)) { |
|
| 586 | - $from = spip_pg_select_as($from); |
|
| 587 | - } |
|
| 585 | + if (is_array($from)) { |
|
| 586 | + $from = spip_pg_select_as($from); |
|
| 587 | + } |
|
| 588 | 588 | |
| 589 | - return !$prefixe ? $from : preg_replace('/(\b)spip_/', '\1' . $prefixe . '_', $from); |
|
| 589 | + return !$prefixe ? $from : preg_replace('/(\b)spip_/', '\1' . $prefixe . '_', $from); |
|
| 590 | 590 | } |
| 591 | 591 | |
| 592 | 592 | function spip_pg_orderby($order, $select) { |
| 593 | - $res = []; |
|
| 594 | - $arg = (is_array($order) ? $order : preg_split('/\s*,\s*/', $order)); |
|
| 595 | - |
|
| 596 | - foreach ($arg as $v) { |
|
| 597 | - if (preg_match('/(case\s+.*?else\s+0\s+end)\s*AS\s+' . $v . '/', $select, $m)) { |
|
| 598 | - $res[] = $m[1]; |
|
| 599 | - } else { |
|
| 600 | - $res[] = $v; |
|
| 601 | - } |
|
| 602 | - } |
|
| 603 | - |
|
| 604 | - return spip_pg_frommysql(join(',', $res)); |
|
| 593 | + $res = []; |
|
| 594 | + $arg = (is_array($order) ? $order : preg_split('/\s*,\s*/', $order)); |
|
| 595 | + |
|
| 596 | + foreach ($arg as $v) { |
|
| 597 | + if (preg_match('/(case\s+.*?else\s+0\s+end)\s*AS\s+' . $v . '/', $select, $m)) { |
|
| 598 | + $res[] = $m[1]; |
|
| 599 | + } else { |
|
| 600 | + $res[] = $v; |
|
| 601 | + } |
|
| 602 | + } |
|
| 603 | + |
|
| 604 | + return spip_pg_frommysql(join(',', $res)); |
|
| 605 | 605 | } |
| 606 | 606 | |
| 607 | 607 | // Conversion a l'arrach' des jointures MySQL en jointures PG |
@@ -609,56 +609,56 @@ discard block |
||
| 609 | 609 | // et pour enlever les repetitions (sans incidence de perf, mais ca fait sale) |
| 610 | 610 | |
| 611 | 611 | function spip_pg_groupby($groupby, $from, $select) { |
| 612 | - $join = strpos($from, ','); |
|
| 613 | - // ismplifier avant de decouper |
|
| 614 | - if (is_string($select)) { // fct SQL sur colonne et constante apostrophee ==> la colonne |
|
| 615 | - $select = preg_replace('/\w+\(\s*([^(),\']*),\s*\'[^\']*\'[^)]*\)/', '\\1', $select); |
|
| 616 | - } |
|
| 617 | - |
|
| 618 | - if ($join or $groupby) { |
|
| 619 | - $join = is_array($select) ? $select : explode(', ', $select); |
|
| 620 | - } |
|
| 621 | - if ($join) { |
|
| 622 | - // enlever les 0 as points, '', ... |
|
| 623 | - foreach ($join as $k => $v) { |
|
| 624 | - $v = str_replace('DISTINCT ', '', $v); |
|
| 625 | - // fct SQL sur colonne et constante apostrophee ==> la colonne |
|
| 626 | - $v = preg_replace('/\w+\(\s*([^(),\']*),\s*\'[^\']*\'[^)]*\)/', '\\1', $v); |
|
| 627 | - $v = preg_replace('/CAST\(\s*([^(),\' ]*\s+)as\s*\w+\)/', '\\1', $v); |
|
| 628 | - // resultat d'agregat ne sont pas a mettre dans le groupby |
|
| 629 | - $v = preg_replace('/(SUM|COUNT|MAX|MIN|UPPER)\([^)]+\)(\s*AS\s+\w+)\s*,?/i', '', $v); |
|
| 630 | - // idem sans AS (fetch numerique) |
|
| 631 | - $v = preg_replace('/(SUM|COUNT|MAX|MIN|UPPER)\([^)]+\)\s*,?/i', '', $v); |
|
| 632 | - // des AS simples : on garde le cote droit du AS |
|
| 633 | - $v = preg_replace('/^.*\sAS\s+(\w+)\s*$/i', '\\1', $v); |
|
| 634 | - // ne reste plus que les vrais colonnes, ou des constantes a virer |
|
| 635 | - if (preg_match(',^[\'"],', $v) or is_numeric($v)) { |
|
| 636 | - unset($join[$k]); |
|
| 637 | - } else { |
|
| 638 | - $join[$k] = trim($v); |
|
| 639 | - } |
|
| 640 | - } |
|
| 641 | - $join = array_diff($join, ['']); |
|
| 642 | - $join = implode(',', $join); |
|
| 643 | - } |
|
| 644 | - if (is_array($groupby)) { |
|
| 645 | - $groupby = join(',', $groupby); |
|
| 646 | - } |
|
| 647 | - if ($join) { |
|
| 648 | - $groupby = $groupby ? "$groupby, $join" : $join; |
|
| 649 | - } |
|
| 650 | - if (!$groupby) { |
|
| 651 | - return ''; |
|
| 652 | - } |
|
| 653 | - |
|
| 654 | - $groupby = spip_pg_frommysql($groupby); |
|
| 655 | - // Ne pas mettre dans le Group-By des valeurs numeriques |
|
| 656 | - // issue de prepare_recherche |
|
| 657 | - $groupby = preg_replace('/^\s*\d+\s+AS\s+\w+\s*,?\s*/i', '', $groupby); |
|
| 658 | - $groupby = preg_replace('/,\s*\d+\s+AS\s+\w+\s*/i', '', $groupby); |
|
| 659 | - $groupby = preg_replace('/\s+AS\s+\w+\s*/i', '', $groupby); |
|
| 660 | - |
|
| 661 | - return "\nGROUP BY $groupby"; |
|
| 612 | + $join = strpos($from, ','); |
|
| 613 | + // ismplifier avant de decouper |
|
| 614 | + if (is_string($select)) { // fct SQL sur colonne et constante apostrophee ==> la colonne |
|
| 615 | + $select = preg_replace('/\w+\(\s*([^(),\']*),\s*\'[^\']*\'[^)]*\)/', '\\1', $select); |
|
| 616 | + } |
|
| 617 | + |
|
| 618 | + if ($join or $groupby) { |
|
| 619 | + $join = is_array($select) ? $select : explode(', ', $select); |
|
| 620 | + } |
|
| 621 | + if ($join) { |
|
| 622 | + // enlever les 0 as points, '', ... |
|
| 623 | + foreach ($join as $k => $v) { |
|
| 624 | + $v = str_replace('DISTINCT ', '', $v); |
|
| 625 | + // fct SQL sur colonne et constante apostrophee ==> la colonne |
|
| 626 | + $v = preg_replace('/\w+\(\s*([^(),\']*),\s*\'[^\']*\'[^)]*\)/', '\\1', $v); |
|
| 627 | + $v = preg_replace('/CAST\(\s*([^(),\' ]*\s+)as\s*\w+\)/', '\\1', $v); |
|
| 628 | + // resultat d'agregat ne sont pas a mettre dans le groupby |
|
| 629 | + $v = preg_replace('/(SUM|COUNT|MAX|MIN|UPPER)\([^)]+\)(\s*AS\s+\w+)\s*,?/i', '', $v); |
|
| 630 | + // idem sans AS (fetch numerique) |
|
| 631 | + $v = preg_replace('/(SUM|COUNT|MAX|MIN|UPPER)\([^)]+\)\s*,?/i', '', $v); |
|
| 632 | + // des AS simples : on garde le cote droit du AS |
|
| 633 | + $v = preg_replace('/^.*\sAS\s+(\w+)\s*$/i', '\\1', $v); |
|
| 634 | + // ne reste plus que les vrais colonnes, ou des constantes a virer |
|
| 635 | + if (preg_match(',^[\'"],', $v) or is_numeric($v)) { |
|
| 636 | + unset($join[$k]); |
|
| 637 | + } else { |
|
| 638 | + $join[$k] = trim($v); |
|
| 639 | + } |
|
| 640 | + } |
|
| 641 | + $join = array_diff($join, ['']); |
|
| 642 | + $join = implode(',', $join); |
|
| 643 | + } |
|
| 644 | + if (is_array($groupby)) { |
|
| 645 | + $groupby = join(',', $groupby); |
|
| 646 | + } |
|
| 647 | + if ($join) { |
|
| 648 | + $groupby = $groupby ? "$groupby, $join" : $join; |
|
| 649 | + } |
|
| 650 | + if (!$groupby) { |
|
| 651 | + return ''; |
|
| 652 | + } |
|
| 653 | + |
|
| 654 | + $groupby = spip_pg_frommysql($groupby); |
|
| 655 | + // Ne pas mettre dans le Group-By des valeurs numeriques |
|
| 656 | + // issue de prepare_recherche |
|
| 657 | + $groupby = preg_replace('/^\s*\d+\s+AS\s+\w+\s*,?\s*/i', '', $groupby); |
|
| 658 | + $groupby = preg_replace('/,\s*\d+\s+AS\s+\w+\s*/i', '', $groupby); |
|
| 659 | + $groupby = preg_replace('/\s+AS\s+\w+\s*/i', '', $groupby); |
|
| 660 | + |
|
| 661 | + return "\nGROUP BY $groupby"; |
|
| 662 | 662 | } |
| 663 | 663 | |
| 664 | 664 | // Conversion des operateurs MySQL en PG |
@@ -669,492 +669,492 @@ discard block |
||
| 669 | 669 | // A ameliorer. |
| 670 | 670 | |
| 671 | 671 | function spip_pg_frommysql($arg) { |
| 672 | - if (is_array($arg)) { |
|
| 673 | - $arg = join(', ', $arg); |
|
| 674 | - } |
|
| 675 | - |
|
| 676 | - $res = spip_pg_fromfield($arg); |
|
| 677 | - |
|
| 678 | - $res = preg_replace('/\brand[(][)]/i', 'random()', $res); |
|
| 679 | - |
|
| 680 | - $res = preg_replace( |
|
| 681 | - '/\b0\.0[+]([a-zA-Z0-9_.]+)\s*/', |
|
| 682 | - 'CAST(substring(\1, \'^ *[0-9.]+\') as float)', |
|
| 683 | - $res |
|
| 684 | - ); |
|
| 685 | - $res = preg_replace( |
|
| 686 | - '/\b0[+]([a-zA-Z0-9_.]+)\s*/', |
|
| 687 | - 'CAST(substring(\1, \'^ *[0-9]+\') as int)', |
|
| 688 | - $res |
|
| 689 | - ); |
|
| 690 | - $res = preg_replace( |
|
| 691 | - '/\bconv[(]([^,]*)[^)]*[)]/i', |
|
| 692 | - 'CAST(substring(\1, \'^ *[0-9]+\') as int)', |
|
| 693 | - $res |
|
| 694 | - ); |
|
| 695 | - |
|
| 696 | - $res = preg_replace( |
|
| 697 | - '/UNIX_TIMESTAMP\s*[(]\s*[)]/', |
|
| 698 | - ' EXTRACT(epoch FROM NOW())', |
|
| 699 | - $res |
|
| 700 | - ); |
|
| 701 | - |
|
| 702 | - // la fonction md5(integer) n'est pas connu en pg |
|
| 703 | - // il faut donc forcer les types en text (cas de md5(id_article)) |
|
| 704 | - $res = preg_replace( |
|
| 705 | - '/md5\s*[(]([^)]*)[)]/i', |
|
| 706 | - 'MD5(CAST(\1 AS text))', |
|
| 707 | - $res |
|
| 708 | - ); |
|
| 709 | - |
|
| 710 | - $res = preg_replace( |
|
| 711 | - '/UNIX_TIMESTAMP\s*[(]([^)]*)[)]/', |
|
| 712 | - ' EXTRACT(epoch FROM \1)', |
|
| 713 | - $res |
|
| 714 | - ); |
|
| 715 | - |
|
| 716 | - $res = preg_replace( |
|
| 717 | - '/\bDAYOFMONTH\s*[(]([^()]*([(][^()]*[)][^()]*)*[^)]*)[)]/', |
|
| 718 | - ' EXTRACT(day FROM \1)', |
|
| 719 | - $res |
|
| 720 | - ); |
|
| 721 | - |
|
| 722 | - $res = preg_replace( |
|
| 723 | - '/\bMONTH\s*[(]([^()]*([(][^)]*[)][^()]*)*[^)]*)[)]/', |
|
| 724 | - ' EXTRACT(month FROM \1)', |
|
| 725 | - $res |
|
| 726 | - ); |
|
| 727 | - |
|
| 728 | - $res = preg_replace( |
|
| 729 | - '/\bYEAR\s*[(]([^()]*([(][^)]*[)][^()]*)*[^)]*)[)]/', |
|
| 730 | - ' EXTRACT(year FROM \1)', |
|
| 731 | - $res |
|
| 732 | - ); |
|
| 733 | - |
|
| 734 | - $res = preg_replace( |
|
| 735 | - '/TO_DAYS\s*[(]([^()]*([(][^)]*[)][()]*)*)[)]/', |
|
| 736 | - ' EXTRACT(day FROM \1 - \'0001-01-01\')', |
|
| 737 | - $res |
|
| 738 | - ); |
|
| 739 | - |
|
| 740 | - $res = preg_replace('/(EXTRACT[(][^ ]* FROM *)"([^"]*)"/', '\1\'\2\'', $res); |
|
| 741 | - |
|
| 742 | - $res = preg_replace('/DATE_FORMAT\s*[(]([^,]*),\s*\'%Y%m%d\'[)]/', 'to_char(\1, \'YYYYMMDD\')', $res); |
|
| 743 | - |
|
| 744 | - $res = preg_replace('/DATE_FORMAT\s*[(]([^,]*),\s*\'%Y%m\'[)]/', 'to_char(\1, \'YYYYMM\')', $res); |
|
| 745 | - |
|
| 746 | - $res = preg_replace('/DATE_SUB\s*[(]([^,]*),/', '(\1 -', $res); |
|
| 747 | - $res = preg_replace('/DATE_ADD\s*[(]([^,]*),/', '(\1 +', $res); |
|
| 748 | - $res = preg_replace('/INTERVAL\s+(\d+\s+\w+)/', 'INTERVAL \'\1\'', $res); |
|
| 749 | - $res = preg_replace('/([+<>-]=?)\s*(\'\d+-\d+-\d+\s+\d+:\d+(:\d+)\')/', '\1 timestamp \2', $res); |
|
| 750 | - $res = preg_replace('/(\'\d+-\d+-\d+\s+\d+:\d+:\d+\')\s*([+<>-]=?)/', 'timestamp \1 \2', $res); |
|
| 751 | - |
|
| 752 | - $res = preg_replace('/([+<>-]=?)\s*(\'\d+-\d+-\d+\')/', '\1 timestamp \2', $res); |
|
| 753 | - $res = preg_replace('/(\'\d+-\d+-\d+\')\s*([+<>-]=?)/', 'timestamp \1 \2', $res); |
|
| 754 | - |
|
| 755 | - $res = preg_replace('/(timestamp .\d+)-00-/', '\1-01-', $res); |
|
| 756 | - $res = preg_replace('/(timestamp .\d+-\d+)-00/', '\1-01', $res); |
|
| 672 | + if (is_array($arg)) { |
|
| 673 | + $arg = join(', ', $arg); |
|
| 674 | + } |
|
| 675 | + |
|
| 676 | + $res = spip_pg_fromfield($arg); |
|
| 677 | + |
|
| 678 | + $res = preg_replace('/\brand[(][)]/i', 'random()', $res); |
|
| 679 | + |
|
| 680 | + $res = preg_replace( |
|
| 681 | + '/\b0\.0[+]([a-zA-Z0-9_.]+)\s*/', |
|
| 682 | + 'CAST(substring(\1, \'^ *[0-9.]+\') as float)', |
|
| 683 | + $res |
|
| 684 | + ); |
|
| 685 | + $res = preg_replace( |
|
| 686 | + '/\b0[+]([a-zA-Z0-9_.]+)\s*/', |
|
| 687 | + 'CAST(substring(\1, \'^ *[0-9]+\') as int)', |
|
| 688 | + $res |
|
| 689 | + ); |
|
| 690 | + $res = preg_replace( |
|
| 691 | + '/\bconv[(]([^,]*)[^)]*[)]/i', |
|
| 692 | + 'CAST(substring(\1, \'^ *[0-9]+\') as int)', |
|
| 693 | + $res |
|
| 694 | + ); |
|
| 695 | + |
|
| 696 | + $res = preg_replace( |
|
| 697 | + '/UNIX_TIMESTAMP\s*[(]\s*[)]/', |
|
| 698 | + ' EXTRACT(epoch FROM NOW())', |
|
| 699 | + $res |
|
| 700 | + ); |
|
| 701 | + |
|
| 702 | + // la fonction md5(integer) n'est pas connu en pg |
|
| 703 | + // il faut donc forcer les types en text (cas de md5(id_article)) |
|
| 704 | + $res = preg_replace( |
|
| 705 | + '/md5\s*[(]([^)]*)[)]/i', |
|
| 706 | + 'MD5(CAST(\1 AS text))', |
|
| 707 | + $res |
|
| 708 | + ); |
|
| 709 | + |
|
| 710 | + $res = preg_replace( |
|
| 711 | + '/UNIX_TIMESTAMP\s*[(]([^)]*)[)]/', |
|
| 712 | + ' EXTRACT(epoch FROM \1)', |
|
| 713 | + $res |
|
| 714 | + ); |
|
| 715 | + |
|
| 716 | + $res = preg_replace( |
|
| 717 | + '/\bDAYOFMONTH\s*[(]([^()]*([(][^()]*[)][^()]*)*[^)]*)[)]/', |
|
| 718 | + ' EXTRACT(day FROM \1)', |
|
| 719 | + $res |
|
| 720 | + ); |
|
| 721 | + |
|
| 722 | + $res = preg_replace( |
|
| 723 | + '/\bMONTH\s*[(]([^()]*([(][^)]*[)][^()]*)*[^)]*)[)]/', |
|
| 724 | + ' EXTRACT(month FROM \1)', |
|
| 725 | + $res |
|
| 726 | + ); |
|
| 727 | + |
|
| 728 | + $res = preg_replace( |
|
| 729 | + '/\bYEAR\s*[(]([^()]*([(][^)]*[)][^()]*)*[^)]*)[)]/', |
|
| 730 | + ' EXTRACT(year FROM \1)', |
|
| 731 | + $res |
|
| 732 | + ); |
|
| 733 | + |
|
| 734 | + $res = preg_replace( |
|
| 735 | + '/TO_DAYS\s*[(]([^()]*([(][^)]*[)][()]*)*)[)]/', |
|
| 736 | + ' EXTRACT(day FROM \1 - \'0001-01-01\')', |
|
| 737 | + $res |
|
| 738 | + ); |
|
| 739 | + |
|
| 740 | + $res = preg_replace('/(EXTRACT[(][^ ]* FROM *)"([^"]*)"/', '\1\'\2\'', $res); |
|
| 741 | + |
|
| 742 | + $res = preg_replace('/DATE_FORMAT\s*[(]([^,]*),\s*\'%Y%m%d\'[)]/', 'to_char(\1, \'YYYYMMDD\')', $res); |
|
| 743 | + |
|
| 744 | + $res = preg_replace('/DATE_FORMAT\s*[(]([^,]*),\s*\'%Y%m\'[)]/', 'to_char(\1, \'YYYYMM\')', $res); |
|
| 745 | + |
|
| 746 | + $res = preg_replace('/DATE_SUB\s*[(]([^,]*),/', '(\1 -', $res); |
|
| 747 | + $res = preg_replace('/DATE_ADD\s*[(]([^,]*),/', '(\1 +', $res); |
|
| 748 | + $res = preg_replace('/INTERVAL\s+(\d+\s+\w+)/', 'INTERVAL \'\1\'', $res); |
|
| 749 | + $res = preg_replace('/([+<>-]=?)\s*(\'\d+-\d+-\d+\s+\d+:\d+(:\d+)\')/', '\1 timestamp \2', $res); |
|
| 750 | + $res = preg_replace('/(\'\d+-\d+-\d+\s+\d+:\d+:\d+\')\s*([+<>-]=?)/', 'timestamp \1 \2', $res); |
|
| 751 | + |
|
| 752 | + $res = preg_replace('/([+<>-]=?)\s*(\'\d+-\d+-\d+\')/', '\1 timestamp \2', $res); |
|
| 753 | + $res = preg_replace('/(\'\d+-\d+-\d+\')\s*([+<>-]=?)/', 'timestamp \1 \2', $res); |
|
| 754 | + |
|
| 755 | + $res = preg_replace('/(timestamp .\d+)-00-/', '\1-01-', $res); |
|
| 756 | + $res = preg_replace('/(timestamp .\d+-\d+)-00/', '\1-01', $res); |
|
| 757 | 757 | # correct en theorie mais produit des debordements arithmetiques |
| 758 | 758 | # $res = preg_replace("/(EXTRACT[(][^ ]* FROM *)(timestamp *'[^']*' *[+-] *timestamp *'[^']*') *[)]/", '\2', $res); |
| 759 | - $res = preg_replace("/(EXTRACT[(][^ ]* FROM *)('[^']*')/", '\1 timestamp \2', $res); |
|
| 760 | - $res = preg_replace('/\sLIKE\s+/', ' ILIKE ', $res); |
|
| 759 | + $res = preg_replace("/(EXTRACT[(][^ ]* FROM *)('[^']*')/", '\1 timestamp \2', $res); |
|
| 760 | + $res = preg_replace('/\sLIKE\s+/', ' ILIKE ', $res); |
|
| 761 | 761 | |
| 762 | - return str_replace('REGEXP', '~', $res); |
|
| 762 | + return str_replace('REGEXP', '~', $res); |
|
| 763 | 763 | } |
| 764 | 764 | |
| 765 | 765 | function spip_pg_fromfield($arg) { |
| 766 | - while (preg_match('/^(.*?)FIELD\s*\(([^,]*)((,[^,)]*)*)\)/', $arg, $m)) { |
|
| 767 | - preg_match_all('/,([^,]*)/', $m[3], $r, PREG_PATTERN_ORDER); |
|
| 768 | - $res = ''; |
|
| 769 | - $n = 0; |
|
| 770 | - $index = $m[2]; |
|
| 771 | - foreach ($r[1] as $v) { |
|
| 772 | - $n++; |
|
| 773 | - $res .= "\nwhen $index=$v then $n"; |
|
| 774 | - } |
|
| 775 | - $arg = $m[1] . "case $res else 0 end " |
|
| 776 | - . substr($arg, strlen($m[0])); |
|
| 777 | - } |
|
| 778 | - |
|
| 779 | - return $arg; |
|
| 766 | + while (preg_match('/^(.*?)FIELD\s*\(([^,]*)((,[^,)]*)*)\)/', $arg, $m)) { |
|
| 767 | + preg_match_all('/,([^,]*)/', $m[3], $r, PREG_PATTERN_ORDER); |
|
| 768 | + $res = ''; |
|
| 769 | + $n = 0; |
|
| 770 | + $index = $m[2]; |
|
| 771 | + foreach ($r[1] as $v) { |
|
| 772 | + $n++; |
|
| 773 | + $res .= "\nwhen $index=$v then $n"; |
|
| 774 | + } |
|
| 775 | + $arg = $m[1] . "case $res else 0 end " |
|
| 776 | + . substr($arg, strlen($m[0])); |
|
| 777 | + } |
|
| 778 | + |
|
| 779 | + return $arg; |
|
| 780 | 780 | } |
| 781 | 781 | |
| 782 | 782 | function calculer_pg_where($v) { |
| 783 | - if (!is_array($v)) { |
|
| 784 | - return spip_pg_frommysql($v); |
|
| 785 | - } |
|
| 786 | - |
|
| 787 | - $op = str_replace('REGEXP', '~', array_shift($v)); |
|
| 788 | - if (!($n = count($v))) { |
|
| 789 | - return $op; |
|
| 790 | - } else { |
|
| 791 | - $arg = calculer_pg_where(array_shift($v)); |
|
| 792 | - if ($n == 1) { |
|
| 793 | - return "$op($arg)"; |
|
| 794 | - } else { |
|
| 795 | - $arg2 = calculer_pg_where(array_shift($v)); |
|
| 796 | - if ($n == 2) { |
|
| 797 | - return "($arg $op $arg2)"; |
|
| 798 | - } else { |
|
| 799 | - return "($arg $op ($arg2) : $v[0])"; |
|
| 800 | - } |
|
| 801 | - } |
|
| 802 | - } |
|
| 783 | + if (!is_array($v)) { |
|
| 784 | + return spip_pg_frommysql($v); |
|
| 785 | + } |
|
| 786 | + |
|
| 787 | + $op = str_replace('REGEXP', '~', array_shift($v)); |
|
| 788 | + if (!($n = count($v))) { |
|
| 789 | + return $op; |
|
| 790 | + } else { |
|
| 791 | + $arg = calculer_pg_where(array_shift($v)); |
|
| 792 | + if ($n == 1) { |
|
| 793 | + return "$op($arg)"; |
|
| 794 | + } else { |
|
| 795 | + $arg2 = calculer_pg_where(array_shift($v)); |
|
| 796 | + if ($n == 2) { |
|
| 797 | + return "($arg $op $arg2)"; |
|
| 798 | + } else { |
|
| 799 | + return "($arg $op ($arg2) : $v[0])"; |
|
| 800 | + } |
|
| 801 | + } |
|
| 802 | + } |
|
| 803 | 803 | } |
| 804 | 804 | |
| 805 | 805 | |
| 806 | 806 | function calculer_pg_expression($expression, $v, $join = 'AND') { |
| 807 | - if (empty($v)) { |
|
| 808 | - return ''; |
|
| 809 | - } |
|
| 807 | + if (empty($v)) { |
|
| 808 | + return ''; |
|
| 809 | + } |
|
| 810 | 810 | |
| 811 | - $exp = "\n$expression "; |
|
| 811 | + $exp = "\n$expression "; |
|
| 812 | 812 | |
| 813 | - if (!is_array($v)) { |
|
| 814 | - $v = [$v]; |
|
| 815 | - } |
|
| 813 | + if (!is_array($v)) { |
|
| 814 | + $v = [$v]; |
|
| 815 | + } |
|
| 816 | 816 | |
| 817 | - if (strtoupper($join) === 'AND') { |
|
| 818 | - return $exp . join("\n\t$join ", array_map('calculer_pg_where', $v)); |
|
| 819 | - } else { |
|
| 820 | - return $exp . join($join, $v); |
|
| 821 | - } |
|
| 817 | + if (strtoupper($join) === 'AND') { |
|
| 818 | + return $exp . join("\n\t$join ", array_map('calculer_pg_where', $v)); |
|
| 819 | + } else { |
|
| 820 | + return $exp . join($join, $v); |
|
| 821 | + } |
|
| 822 | 822 | } |
| 823 | 823 | |
| 824 | 824 | function spip_pg_select_as($args) { |
| 825 | - $argsas = ''; |
|
| 826 | - foreach ($args as $k => $v) { |
|
| 827 | - if (substr($k, -1) == '@') { |
|
| 828 | - // c'est une jointure qui se refere au from precedent |
|
| 829 | - // pas de virgule |
|
| 830 | - $argsas .= ' ' . $v; |
|
| 831 | - } else { |
|
| 832 | - $as = ''; |
|
| 833 | - // spip_log("$k : $v", _LOG_DEBUG); |
|
| 834 | - if (!is_numeric($k)) { |
|
| 835 | - if (preg_match('/\.(.*)$/', $k, $r)) { |
|
| 836 | - $v = $k; |
|
| 837 | - } elseif ($v != $k) { |
|
| 838 | - $p = strpos($v, ' '); |
|
| 839 | - if ($p) { |
|
| 840 | - $v = substr($v, 0, $p) . " AS $k" . substr($v, $p); |
|
| 841 | - } else { |
|
| 842 | - $as = " AS $k"; |
|
| 843 | - } |
|
| 844 | - } |
|
| 845 | - } |
|
| 846 | - // spip_log("subs $k : $v avec $as", _LOG_DEBUG); |
|
| 847 | - // if (strpos($v, 'JOIN') === false) $argsas .= ', '; |
|
| 848 | - $argsas .= ', ' . $v . $as; |
|
| 849 | - } |
|
| 850 | - } |
|
| 851 | - |
|
| 852 | - return substr($argsas, 2); |
|
| 825 | + $argsas = ''; |
|
| 826 | + foreach ($args as $k => $v) { |
|
| 827 | + if (substr($k, -1) == '@') { |
|
| 828 | + // c'est une jointure qui se refere au from precedent |
|
| 829 | + // pas de virgule |
|
| 830 | + $argsas .= ' ' . $v; |
|
| 831 | + } else { |
|
| 832 | + $as = ''; |
|
| 833 | + // spip_log("$k : $v", _LOG_DEBUG); |
|
| 834 | + if (!is_numeric($k)) { |
|
| 835 | + if (preg_match('/\.(.*)$/', $k, $r)) { |
|
| 836 | + $v = $k; |
|
| 837 | + } elseif ($v != $k) { |
|
| 838 | + $p = strpos($v, ' '); |
|
| 839 | + if ($p) { |
|
| 840 | + $v = substr($v, 0, $p) . " AS $k" . substr($v, $p); |
|
| 841 | + } else { |
|
| 842 | + $as = " AS $k"; |
|
| 843 | + } |
|
| 844 | + } |
|
| 845 | + } |
|
| 846 | + // spip_log("subs $k : $v avec $as", _LOG_DEBUG); |
|
| 847 | + // if (strpos($v, 'JOIN') === false) $argsas .= ', '; |
|
| 848 | + $argsas .= ', ' . $v . $as; |
|
| 849 | + } |
|
| 850 | + } |
|
| 851 | + |
|
| 852 | + return substr($argsas, 2); |
|
| 853 | 853 | } |
| 854 | 854 | |
| 855 | 855 | function spip_pg_fetch($res, $t = '', $serveur = '', $requeter = true) { |
| 856 | 856 | |
| 857 | - if ($res) { |
|
| 858 | - $res = pg_fetch_array($res, null, PGSQL_ASSOC); |
|
| 859 | - } |
|
| 857 | + if ($res) { |
|
| 858 | + $res = pg_fetch_array($res, null, PGSQL_ASSOC); |
|
| 859 | + } |
|
| 860 | 860 | |
| 861 | - return $res; |
|
| 861 | + return $res; |
|
| 862 | 862 | } |
| 863 | 863 | |
| 864 | 864 | function spip_pg_seek($r, $row_number, $serveur = '', $requeter = true) { |
| 865 | - if ($r) { |
|
| 866 | - return pg_result_seek($r, $row_number); |
|
| 867 | - } |
|
| 865 | + if ($r) { |
|
| 866 | + return pg_result_seek($r, $row_number); |
|
| 867 | + } |
|
| 868 | 868 | } |
| 869 | 869 | |
| 870 | 870 | |
| 871 | 871 | function spip_pg_countsel( |
| 872 | - $from = [], |
|
| 873 | - $where = [], |
|
| 874 | - $groupby = [], |
|
| 875 | - $having = [], |
|
| 876 | - $serveur = '', |
|
| 877 | - $requeter = true |
|
| 872 | + $from = [], |
|
| 873 | + $where = [], |
|
| 874 | + $groupby = [], |
|
| 875 | + $having = [], |
|
| 876 | + $serveur = '', |
|
| 877 | + $requeter = true |
|
| 878 | 878 | ) { |
| 879 | - $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 880 | - $r = spip_pg_select("COUNT($c)", $from, $where, '', '', '', $having, $serveur, $requeter); |
|
| 881 | - if (!$requeter) { |
|
| 882 | - return $r; |
|
| 883 | - } |
|
| 884 | - if (!is_resource($r)) { |
|
| 885 | - return 0; |
|
| 886 | - } |
|
| 887 | - [$c] = pg_fetch_array($r, null, PGSQL_NUM); |
|
| 888 | - |
|
| 889 | - return $c; |
|
| 879 | + $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 880 | + $r = spip_pg_select("COUNT($c)", $from, $where, '', '', '', $having, $serveur, $requeter); |
|
| 881 | + if (!$requeter) { |
|
| 882 | + return $r; |
|
| 883 | + } |
|
| 884 | + if (!is_resource($r)) { |
|
| 885 | + return 0; |
|
| 886 | + } |
|
| 887 | + [$c] = pg_fetch_array($r, null, PGSQL_NUM); |
|
| 888 | + |
|
| 889 | + return $c; |
|
| 890 | 890 | } |
| 891 | 891 | |
| 892 | 892 | function spip_pg_count($res, $serveur = '', $requeter = true) { |
| 893 | - return !$res ? 0 : pg_numrows($res); |
|
| 893 | + return !$res ? 0 : pg_numrows($res); |
|
| 894 | 894 | } |
| 895 | 895 | |
| 896 | 896 | function spip_pg_free($res, $serveur = '', $requeter = true) { |
| 897 | - // rien a faire en postgres |
|
| 897 | + // rien a faire en postgres |
|
| 898 | 898 | } |
| 899 | 899 | |
| 900 | 900 | function spip_pg_delete($table, $where = '', $serveur = '', $requeter = true) { |
| 901 | 901 | |
| 902 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 903 | - $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 902 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 903 | + $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 904 | 904 | |
| 905 | - $query = calculer_pg_expression('DELETE FROM', $table, ',') |
|
| 906 | - . calculer_pg_expression('WHERE', $where, 'AND'); |
|
| 905 | + $query = calculer_pg_expression('DELETE FROM', $table, ',') |
|
| 906 | + . calculer_pg_expression('WHERE', $where, 'AND'); |
|
| 907 | 907 | |
| 908 | - // renvoyer la requete inerte si demandee |
|
| 909 | - if (!$requeter) { |
|
| 910 | - return $query; |
|
| 911 | - } |
|
| 908 | + // renvoyer la requete inerte si demandee |
|
| 909 | + if (!$requeter) { |
|
| 910 | + return $query; |
|
| 911 | + } |
|
| 912 | 912 | |
| 913 | - $res = spip_pg_trace_query($query, $serveur); |
|
| 914 | - if ($res) { |
|
| 915 | - return pg_affected_rows($res); |
|
| 916 | - } else { |
|
| 917 | - return false; |
|
| 918 | - } |
|
| 913 | + $res = spip_pg_trace_query($query, $serveur); |
|
| 914 | + if ($res) { |
|
| 915 | + return pg_affected_rows($res); |
|
| 916 | + } else { |
|
| 917 | + return false; |
|
| 918 | + } |
|
| 919 | 919 | } |
| 920 | 920 | |
| 921 | 921 | function spip_pg_insert($table, $champs, $valeurs, $desc = [], $serveur = '', $requeter = true) { |
| 922 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 923 | - $prefixe = $connexion['prefixe']; |
|
| 924 | - $link = $connexion['link']; |
|
| 925 | - |
|
| 926 | - if (!$desc) { |
|
| 927 | - $desc = description_table($table, $serveur); |
|
| 928 | - } |
|
| 929 | - $seq = spip_pg_sequence($table, true); |
|
| 930 | - // si pas de cle primaire dans l'insertion, renvoyer curval |
|
| 931 | - if (!preg_match(",\b$seq\b,", $champs)) { |
|
| 932 | - $seq = spip_pg_sequence($table); |
|
| 933 | - $seq = prefixer_table_spip($seq, $prefixe); |
|
| 934 | - $seq = "currval('$seq')"; |
|
| 935 | - } |
|
| 936 | - |
|
| 937 | - $table = prefixer_table_spip($table, $prefixe); |
|
| 938 | - $ret = !$seq ? '' : (" RETURNING $seq"); |
|
| 939 | - $ins = (strlen($champs) < 3) |
|
| 940 | - ? ' DEFAULT VALUES' |
|
| 941 | - : "$champs VALUES $valeurs"; |
|
| 942 | - $q = "INSERT INTO $table $ins $ret"; |
|
| 943 | - if (!$requeter) { |
|
| 944 | - return $q; |
|
| 945 | - } |
|
| 946 | - $connexion['last'] = $q; |
|
| 947 | - $r = spip_pg_query_simple($link, $q); |
|
| 922 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 923 | + $prefixe = $connexion['prefixe']; |
|
| 924 | + $link = $connexion['link']; |
|
| 925 | + |
|
| 926 | + if (!$desc) { |
|
| 927 | + $desc = description_table($table, $serveur); |
|
| 928 | + } |
|
| 929 | + $seq = spip_pg_sequence($table, true); |
|
| 930 | + // si pas de cle primaire dans l'insertion, renvoyer curval |
|
| 931 | + if (!preg_match(",\b$seq\b,", $champs)) { |
|
| 932 | + $seq = spip_pg_sequence($table); |
|
| 933 | + $seq = prefixer_table_spip($seq, $prefixe); |
|
| 934 | + $seq = "currval('$seq')"; |
|
| 935 | + } |
|
| 936 | + |
|
| 937 | + $table = prefixer_table_spip($table, $prefixe); |
|
| 938 | + $ret = !$seq ? '' : (" RETURNING $seq"); |
|
| 939 | + $ins = (strlen($champs) < 3) |
|
| 940 | + ? ' DEFAULT VALUES' |
|
| 941 | + : "$champs VALUES $valeurs"; |
|
| 942 | + $q = "INSERT INTO $table $ins $ret"; |
|
| 943 | + if (!$requeter) { |
|
| 944 | + return $q; |
|
| 945 | + } |
|
| 946 | + $connexion['last'] = $q; |
|
| 947 | + $r = spip_pg_query_simple($link, $q); |
|
| 948 | 948 | # spip_log($q,'pg.'._LOG_DEBUG); |
| 949 | - if ($r) { |
|
| 950 | - if (!$ret) { |
|
| 951 | - return 0; |
|
| 952 | - } |
|
| 953 | - if ($r2 = pg_fetch_array($r, null, PGSQL_NUM)) { |
|
| 954 | - return $r2[0]; |
|
| 955 | - } |
|
| 956 | - } |
|
| 957 | - |
|
| 958 | - return false; |
|
| 949 | + if ($r) { |
|
| 950 | + if (!$ret) { |
|
| 951 | + return 0; |
|
| 952 | + } |
|
| 953 | + if ($r2 = pg_fetch_array($r, null, PGSQL_NUM)) { |
|
| 954 | + return $r2[0]; |
|
| 955 | + } |
|
| 956 | + } |
|
| 957 | + |
|
| 958 | + return false; |
|
| 959 | 959 | } |
| 960 | 960 | |
| 961 | 961 | function spip_pg_insertq($table, $couples = [], $desc = [], $serveur = '', $requeter = true) { |
| 962 | 962 | |
| 963 | - if (!$desc) { |
|
| 964 | - $desc = description_table($table, $serveur); |
|
| 965 | - } |
|
| 966 | - if (!$desc) { |
|
| 967 | - die("$table insertion sans description"); |
|
| 968 | - } |
|
| 969 | - $fields = $desc['field']; |
|
| 970 | - |
|
| 971 | - foreach ($couples as $champ => $val) { |
|
| 972 | - $couples[$champ] = spip_pg_cite($val, $fields[$champ]); |
|
| 973 | - } |
|
| 974 | - |
|
| 975 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 976 | - $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 977 | - |
|
| 978 | - return spip_pg_insert( |
|
| 979 | - $table, |
|
| 980 | - '(' . join(',', array_keys($couples)) . ')', |
|
| 981 | - '(' . join(',', $couples) . ')', |
|
| 982 | - $desc, |
|
| 983 | - $serveur, |
|
| 984 | - $requeter |
|
| 985 | - ); |
|
| 963 | + if (!$desc) { |
|
| 964 | + $desc = description_table($table, $serveur); |
|
| 965 | + } |
|
| 966 | + if (!$desc) { |
|
| 967 | + die("$table insertion sans description"); |
|
| 968 | + } |
|
| 969 | + $fields = $desc['field']; |
|
| 970 | + |
|
| 971 | + foreach ($couples as $champ => $val) { |
|
| 972 | + $couples[$champ] = spip_pg_cite($val, $fields[$champ]); |
|
| 973 | + } |
|
| 974 | + |
|
| 975 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 976 | + $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 977 | + |
|
| 978 | + return spip_pg_insert( |
|
| 979 | + $table, |
|
| 980 | + '(' . join(',', array_keys($couples)) . ')', |
|
| 981 | + '(' . join(',', $couples) . ')', |
|
| 982 | + $desc, |
|
| 983 | + $serveur, |
|
| 984 | + $requeter |
|
| 985 | + ); |
|
| 986 | 986 | } |
| 987 | 987 | |
| 988 | 988 | |
| 989 | 989 | function spip_pg_insertq_multi($table, $tab_couples = [], $desc = [], $serveur = '', $requeter = true) { |
| 990 | 990 | |
| 991 | - if (!$desc) { |
|
| 992 | - $desc = description_table($table, $serveur); |
|
| 993 | - } |
|
| 994 | - if (!$desc) { |
|
| 995 | - die("$table insertion sans description"); |
|
| 996 | - } |
|
| 997 | - $fields = $desc['field'] ?? []; |
|
| 998 | - |
|
| 999 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1000 | - // une premiere fois pour ajouter maj dans les cles |
|
| 1001 | - $c = $tab_couples[0] ?? []; |
|
| 1002 | - $les_cles = spip_pg_ajouter_champs_timestamp($table, $c, $desc, $serveur); |
|
| 1003 | - |
|
| 1004 | - $cles = '(' . join(',', array_keys($les_cles)) . ')'; |
|
| 1005 | - $valeurs = []; |
|
| 1006 | - foreach ($tab_couples as $couples) { |
|
| 1007 | - foreach ($couples as $champ => $val) { |
|
| 1008 | - $couples[$champ] = spip_pg_cite($val, $fields[$champ]); |
|
| 1009 | - } |
|
| 1010 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1011 | - $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1012 | - |
|
| 1013 | - $valeurs[] = '(' . join(',', $couples) . ')'; |
|
| 1014 | - } |
|
| 1015 | - $valeurs = implode(', ', $valeurs); |
|
| 1016 | - |
|
| 1017 | - return spip_pg_insert($table, $cles, $valeurs, $desc, $serveur, $requeter); |
|
| 991 | + if (!$desc) { |
|
| 992 | + $desc = description_table($table, $serveur); |
|
| 993 | + } |
|
| 994 | + if (!$desc) { |
|
| 995 | + die("$table insertion sans description"); |
|
| 996 | + } |
|
| 997 | + $fields = $desc['field'] ?? []; |
|
| 998 | + |
|
| 999 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1000 | + // une premiere fois pour ajouter maj dans les cles |
|
| 1001 | + $c = $tab_couples[0] ?? []; |
|
| 1002 | + $les_cles = spip_pg_ajouter_champs_timestamp($table, $c, $desc, $serveur); |
|
| 1003 | + |
|
| 1004 | + $cles = '(' . join(',', array_keys($les_cles)) . ')'; |
|
| 1005 | + $valeurs = []; |
|
| 1006 | + foreach ($tab_couples as $couples) { |
|
| 1007 | + foreach ($couples as $champ => $val) { |
|
| 1008 | + $couples[$champ] = spip_pg_cite($val, $fields[$champ]); |
|
| 1009 | + } |
|
| 1010 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1011 | + $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1012 | + |
|
| 1013 | + $valeurs[] = '(' . join(',', $couples) . ')'; |
|
| 1014 | + } |
|
| 1015 | + $valeurs = implode(', ', $valeurs); |
|
| 1016 | + |
|
| 1017 | + return spip_pg_insert($table, $cles, $valeurs, $desc, $serveur, $requeter); |
|
| 1018 | 1018 | } |
| 1019 | 1019 | |
| 1020 | 1020 | |
| 1021 | 1021 | function spip_pg_update($table, $couples, $where = '', $desc = '', $serveur = '', $requeter = true) { |
| 1022 | 1022 | |
| 1023 | - if (!$couples) { |
|
| 1024 | - return; |
|
| 1025 | - } |
|
| 1026 | - $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1027 | - $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 1023 | + if (!$couples) { |
|
| 1024 | + return; |
|
| 1025 | + } |
|
| 1026 | + $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1027 | + $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 1028 | 1028 | |
| 1029 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1030 | - $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1029 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1030 | + $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1031 | 1031 | |
| 1032 | - $set = []; |
|
| 1033 | - foreach ($couples as $champ => $val) { |
|
| 1034 | - $set[] = $champ . '=' . $val; |
|
| 1035 | - } |
|
| 1032 | + $set = []; |
|
| 1033 | + foreach ($couples as $champ => $val) { |
|
| 1034 | + $set[] = $champ . '=' . $val; |
|
| 1035 | + } |
|
| 1036 | 1036 | |
| 1037 | - $query = calculer_pg_expression('UPDATE', $table, ',') |
|
| 1038 | - . calculer_pg_expression('SET', $set, ',') |
|
| 1039 | - . calculer_pg_expression('WHERE', $where, 'AND'); |
|
| 1037 | + $query = calculer_pg_expression('UPDATE', $table, ',') |
|
| 1038 | + . calculer_pg_expression('SET', $set, ',') |
|
| 1039 | + . calculer_pg_expression('WHERE', $where, 'AND'); |
|
| 1040 | 1040 | |
| 1041 | - // renvoyer la requete inerte si demandee |
|
| 1042 | - if (!$requeter) { |
|
| 1043 | - return $query; |
|
| 1044 | - } |
|
| 1041 | + // renvoyer la requete inerte si demandee |
|
| 1042 | + if (!$requeter) { |
|
| 1043 | + return $query; |
|
| 1044 | + } |
|
| 1045 | 1045 | |
| 1046 | - return spip_pg_trace_query($query, $serveur); |
|
| 1046 | + return spip_pg_trace_query($query, $serveur); |
|
| 1047 | 1047 | } |
| 1048 | 1048 | |
| 1049 | 1049 | // idem, mais les valeurs sont des constantes a mettre entre apostrophes |
| 1050 | 1050 | // sauf les expressions de date lorsqu'il s'agit de fonctions SQL (NOW etc) |
| 1051 | 1051 | function spip_pg_updateq($table, $couples, $where = '', $desc = [], $serveur = '', $requeter = true) { |
| 1052 | - if (!$couples) { |
|
| 1053 | - return; |
|
| 1054 | - } |
|
| 1055 | - if (!$desc) { |
|
| 1056 | - $desc = description_table($table, $serveur); |
|
| 1057 | - } |
|
| 1058 | - $fields = $desc['field']; |
|
| 1059 | - foreach ($couples as $k => $val) { |
|
| 1060 | - $couples[$k] = spip_pg_cite($val, $fields[$k]); |
|
| 1061 | - } |
|
| 1062 | - |
|
| 1063 | - return spip_pg_update($table, $couples, $where, $desc, $serveur, $requeter); |
|
| 1052 | + if (!$couples) { |
|
| 1053 | + return; |
|
| 1054 | + } |
|
| 1055 | + if (!$desc) { |
|
| 1056 | + $desc = description_table($table, $serveur); |
|
| 1057 | + } |
|
| 1058 | + $fields = $desc['field']; |
|
| 1059 | + foreach ($couples as $k => $val) { |
|
| 1060 | + $couples[$k] = spip_pg_cite($val, $fields[$k]); |
|
| 1061 | + } |
|
| 1062 | + |
|
| 1063 | + return spip_pg_update($table, $couples, $where, $desc, $serveur, $requeter); |
|
| 1064 | 1064 | } |
| 1065 | 1065 | |
| 1066 | 1066 | |
| 1067 | 1067 | function spip_pg_replace($table, $values, $desc, $serveur = '', $requeter = true) { |
| 1068 | - if (!$values) { |
|
| 1069 | - spip_log("replace vide $table", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1070 | - |
|
| 1071 | - return 0; |
|
| 1072 | - } |
|
| 1073 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1074 | - $prefixe = $connexion['prefixe']; |
|
| 1075 | - $link = $connexion['link']; |
|
| 1076 | - |
|
| 1077 | - if (!$desc) { |
|
| 1078 | - $desc = description_table($table, $serveur); |
|
| 1079 | - } |
|
| 1080 | - if (!$desc) { |
|
| 1081 | - die("$table insertion sans description"); |
|
| 1082 | - } |
|
| 1083 | - $prim = $desc['key']['PRIMARY KEY']; |
|
| 1084 | - $ids = preg_split('/,\s*/', $prim); |
|
| 1085 | - $noprims = $prims = []; |
|
| 1086 | - foreach ($values as $k => $v) { |
|
| 1087 | - $values[$k] = $v = spip_pg_cite($v, $desc['field'][$k]); |
|
| 1088 | - |
|
| 1089 | - if (!in_array($k, $ids)) { |
|
| 1090 | - $noprims[$k] = "$k=$v"; |
|
| 1091 | - } else { |
|
| 1092 | - $prims[$k] = "$k=$v"; |
|
| 1093 | - } |
|
| 1094 | - } |
|
| 1095 | - |
|
| 1096 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1097 | - $values = spip_pg_ajouter_champs_timestamp($table, $values, $desc, $serveur); |
|
| 1098 | - |
|
| 1099 | - $where = join(' AND ', $prims); |
|
| 1100 | - if (!$where) { |
|
| 1101 | - return spip_pg_insert( |
|
| 1102 | - $table, |
|
| 1103 | - '(' . join(',', array_keys($values)) . ')', |
|
| 1104 | - '(' . join(',', $values) . ')', |
|
| 1105 | - $desc, |
|
| 1106 | - $serveur |
|
| 1107 | - ); |
|
| 1108 | - } |
|
| 1109 | - $couples = join(',', $noprims); |
|
| 1110 | - |
|
| 1111 | - $seq = spip_pg_sequence($table); |
|
| 1112 | - $table = prefixer_table_spip($table, $prefixe); |
|
| 1113 | - $seq = prefixer_table_spip($seq, $prefixe); |
|
| 1114 | - |
|
| 1115 | - $connexion['last'] = $q = "UPDATE $table SET $couples WHERE $where"; |
|
| 1116 | - if ($couples) { |
|
| 1117 | - $couples = spip_pg_query_simple($link, $q); |
|
| 1068 | + if (!$values) { |
|
| 1069 | + spip_log("replace vide $table", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1070 | + |
|
| 1071 | + return 0; |
|
| 1072 | + } |
|
| 1073 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1074 | + $prefixe = $connexion['prefixe']; |
|
| 1075 | + $link = $connexion['link']; |
|
| 1076 | + |
|
| 1077 | + if (!$desc) { |
|
| 1078 | + $desc = description_table($table, $serveur); |
|
| 1079 | + } |
|
| 1080 | + if (!$desc) { |
|
| 1081 | + die("$table insertion sans description"); |
|
| 1082 | + } |
|
| 1083 | + $prim = $desc['key']['PRIMARY KEY']; |
|
| 1084 | + $ids = preg_split('/,\s*/', $prim); |
|
| 1085 | + $noprims = $prims = []; |
|
| 1086 | + foreach ($values as $k => $v) { |
|
| 1087 | + $values[$k] = $v = spip_pg_cite($v, $desc['field'][$k]); |
|
| 1088 | + |
|
| 1089 | + if (!in_array($k, $ids)) { |
|
| 1090 | + $noprims[$k] = "$k=$v"; |
|
| 1091 | + } else { |
|
| 1092 | + $prims[$k] = "$k=$v"; |
|
| 1093 | + } |
|
| 1094 | + } |
|
| 1095 | + |
|
| 1096 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1097 | + $values = spip_pg_ajouter_champs_timestamp($table, $values, $desc, $serveur); |
|
| 1098 | + |
|
| 1099 | + $where = join(' AND ', $prims); |
|
| 1100 | + if (!$where) { |
|
| 1101 | + return spip_pg_insert( |
|
| 1102 | + $table, |
|
| 1103 | + '(' . join(',', array_keys($values)) . ')', |
|
| 1104 | + '(' . join(',', $values) . ')', |
|
| 1105 | + $desc, |
|
| 1106 | + $serveur |
|
| 1107 | + ); |
|
| 1108 | + } |
|
| 1109 | + $couples = join(',', $noprims); |
|
| 1110 | + |
|
| 1111 | + $seq = spip_pg_sequence($table); |
|
| 1112 | + $table = prefixer_table_spip($table, $prefixe); |
|
| 1113 | + $seq = prefixer_table_spip($seq, $prefixe); |
|
| 1114 | + |
|
| 1115 | + $connexion['last'] = $q = "UPDATE $table SET $couples WHERE $where"; |
|
| 1116 | + if ($couples) { |
|
| 1117 | + $couples = spip_pg_query_simple($link, $q); |
|
| 1118 | 1118 | # spip_log($q,'pg.'._LOG_DEBUG); |
| 1119 | - if (!$couples) { |
|
| 1120 | - return false; |
|
| 1121 | - } |
|
| 1122 | - $couples = pg_affected_rows($couples); |
|
| 1123 | - } |
|
| 1124 | - if (!$couples) { |
|
| 1125 | - $ret = !$seq ? '' : |
|
| 1126 | - (" RETURNING nextval('$seq') < $prim"); |
|
| 1127 | - $connexion['last'] = $q = "INSERT INTO $table (" . join(',', array_keys($values)) . ') VALUES (' . join( |
|
| 1128 | - ',', |
|
| 1129 | - $values |
|
| 1130 | - ) . ")$ret"; |
|
| 1131 | - $couples = spip_pg_query_simple($link, $q); |
|
| 1132 | - if (!$couples) { |
|
| 1133 | - return false; |
|
| 1134 | - } elseif ($ret) { |
|
| 1135 | - $r = pg_fetch_array($couples, null, PGSQL_NUM); |
|
| 1136 | - if ($r[0]) { |
|
| 1137 | - $connexion['last'] = $q = "SELECT setval('$seq', $prim) from $table"; |
|
| 1138 | - // Le code de SPIP met parfois la sequence a 0 (dans l'import) |
|
| 1139 | - // MySQL n'en dit rien, on fait pareil pour PG |
|
| 1140 | - $r = @pg_query($link, $q); |
|
| 1141 | - } |
|
| 1142 | - } |
|
| 1143 | - } |
|
| 1144 | - |
|
| 1145 | - return $couples; |
|
| 1119 | + if (!$couples) { |
|
| 1120 | + return false; |
|
| 1121 | + } |
|
| 1122 | + $couples = pg_affected_rows($couples); |
|
| 1123 | + } |
|
| 1124 | + if (!$couples) { |
|
| 1125 | + $ret = !$seq ? '' : |
|
| 1126 | + (" RETURNING nextval('$seq') < $prim"); |
|
| 1127 | + $connexion['last'] = $q = "INSERT INTO $table (" . join(',', array_keys($values)) . ') VALUES (' . join( |
|
| 1128 | + ',', |
|
| 1129 | + $values |
|
| 1130 | + ) . ")$ret"; |
|
| 1131 | + $couples = spip_pg_query_simple($link, $q); |
|
| 1132 | + if (!$couples) { |
|
| 1133 | + return false; |
|
| 1134 | + } elseif ($ret) { |
|
| 1135 | + $r = pg_fetch_array($couples, null, PGSQL_NUM); |
|
| 1136 | + if ($r[0]) { |
|
| 1137 | + $connexion['last'] = $q = "SELECT setval('$seq', $prim) from $table"; |
|
| 1138 | + // Le code de SPIP met parfois la sequence a 0 (dans l'import) |
|
| 1139 | + // MySQL n'en dit rien, on fait pareil pour PG |
|
| 1140 | + $r = @pg_query($link, $q); |
|
| 1141 | + } |
|
| 1142 | + } |
|
| 1143 | + } |
|
| 1144 | + |
|
| 1145 | + return $couples; |
|
| 1146 | 1146 | } |
| 1147 | 1147 | |
| 1148 | 1148 | |
| 1149 | 1149 | function spip_pg_replace_multi($table, $tab_couples, $desc = [], $serveur = '', $requeter = true) { |
| 1150 | - $retour = null; |
|
| 1151 | - // boucler pour traiter chaque requete independemment |
|
| 1152 | - foreach ($tab_couples as $couples) { |
|
| 1153 | - $retour = spip_pg_replace($table, $couples, $desc, $serveur, $requeter); |
|
| 1154 | - } |
|
| 1155 | - |
|
| 1156 | - // renvoie le dernier id |
|
| 1157 | - return $retour; |
|
| 1150 | + $retour = null; |
|
| 1151 | + // boucler pour traiter chaque requete independemment |
|
| 1152 | + foreach ($tab_couples as $couples) { |
|
| 1153 | + $retour = spip_pg_replace($table, $couples, $desc, $serveur, $requeter); |
|
| 1154 | + } |
|
| 1155 | + |
|
| 1156 | + // renvoie le dernier id |
|
| 1157 | + return $retour; |
|
| 1158 | 1158 | } |
| 1159 | 1159 | |
| 1160 | 1160 | |
@@ -1163,149 +1163,149 @@ discard block |
||
| 1163 | 1163 | |
| 1164 | 1164 | function spip_pg_sequence($table, $raw = false) { |
| 1165 | 1165 | |
| 1166 | - include_spip('base/serial'); |
|
| 1167 | - if (!isset($GLOBALS['tables_principales'][$table])) { |
|
| 1168 | - return false; |
|
| 1169 | - } |
|
| 1170 | - $desc = $GLOBALS['tables_principales'][$table]; |
|
| 1171 | - $prim = @$desc['key']['PRIMARY KEY']; |
|
| 1172 | - if ( |
|
| 1173 | - !preg_match('/^\w+$/', $prim) |
|
| 1174 | - or !str_contains($desc['field'][$prim], 'int') |
|
| 1175 | - ) { |
|
| 1176 | - return ''; |
|
| 1177 | - } else { |
|
| 1178 | - return $raw ? $prim : $table . '_' . $prim . '_seq'; |
|
| 1179 | - } |
|
| 1166 | + include_spip('base/serial'); |
|
| 1167 | + if (!isset($GLOBALS['tables_principales'][$table])) { |
|
| 1168 | + return false; |
|
| 1169 | + } |
|
| 1170 | + $desc = $GLOBALS['tables_principales'][$table]; |
|
| 1171 | + $prim = @$desc['key']['PRIMARY KEY']; |
|
| 1172 | + if ( |
|
| 1173 | + !preg_match('/^\w+$/', $prim) |
|
| 1174 | + or !str_contains($desc['field'][$prim], 'int') |
|
| 1175 | + ) { |
|
| 1176 | + return ''; |
|
| 1177 | + } else { |
|
| 1178 | + return $raw ? $prim : $table . '_' . $prim . '_seq'; |
|
| 1179 | + } |
|
| 1180 | 1180 | } |
| 1181 | 1181 | |
| 1182 | 1182 | // Explicite les conversions de Mysql d'une valeur $v de type $t |
| 1183 | 1183 | // Dans le cas d'un champ date, pas d'apostrophe, c'est une syntaxe ad hoc |
| 1184 | 1184 | |
| 1185 | 1185 | function spip_pg_cite($v, $t) { |
| 1186 | - if (is_null($v)) { |
|
| 1187 | - return 'NULL'; |
|
| 1188 | - } // null php se traduit en NULL SQL |
|
| 1189 | - |
|
| 1190 | - if (sql_test_date($t)) { |
|
| 1191 | - if ($v and (strpos('0123456789', (string) $v[0]) === false)) { |
|
| 1192 | - return spip_pg_frommysql($v); |
|
| 1193 | - } else { |
|
| 1194 | - if (strncmp($v, '0000', 4) == 0) { |
|
| 1195 | - $v = '0001' . substr($v, 4); |
|
| 1196 | - } |
|
| 1197 | - if (strpos($v, '-00-00') === 4) { |
|
| 1198 | - $v = substr($v, 0, 4) . '-01-01' . substr($v, 10); |
|
| 1199 | - } |
|
| 1200 | - |
|
| 1201 | - return "timestamp '$v'"; |
|
| 1202 | - } |
|
| 1203 | - } elseif (!sql_test_int($t)) { |
|
| 1204 | - return ("'" . pg_escape_string($v) . "'"); |
|
| 1205 | - } elseif (is_numeric($v) or (strpos($v, 'CAST(') === 0)) { |
|
| 1206 | - return $v; |
|
| 1207 | - } elseif ($v[0] == '0' and $v[1] !== 'x' and ctype_xdigit(substr($v, 1))) { |
|
| 1208 | - return substr($v, 1); |
|
| 1209 | - } else { |
|
| 1210 | - spip_log("Warning: '$v' n'est pas de type $t", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1211 | - |
|
| 1212 | - return intval($v); |
|
| 1213 | - } |
|
| 1186 | + if (is_null($v)) { |
|
| 1187 | + return 'NULL'; |
|
| 1188 | + } // null php se traduit en NULL SQL |
|
| 1189 | + |
|
| 1190 | + if (sql_test_date($t)) { |
|
| 1191 | + if ($v and (strpos('0123456789', (string) $v[0]) === false)) { |
|
| 1192 | + return spip_pg_frommysql($v); |
|
| 1193 | + } else { |
|
| 1194 | + if (strncmp($v, '0000', 4) == 0) { |
|
| 1195 | + $v = '0001' . substr($v, 4); |
|
| 1196 | + } |
|
| 1197 | + if (strpos($v, '-00-00') === 4) { |
|
| 1198 | + $v = substr($v, 0, 4) . '-01-01' . substr($v, 10); |
|
| 1199 | + } |
|
| 1200 | + |
|
| 1201 | + return "timestamp '$v'"; |
|
| 1202 | + } |
|
| 1203 | + } elseif (!sql_test_int($t)) { |
|
| 1204 | + return ("'" . pg_escape_string($v) . "'"); |
|
| 1205 | + } elseif (is_numeric($v) or (strpos($v, 'CAST(') === 0)) { |
|
| 1206 | + return $v; |
|
| 1207 | + } elseif ($v[0] == '0' and $v[1] !== 'x' and ctype_xdigit(substr($v, 1))) { |
|
| 1208 | + return substr($v, 1); |
|
| 1209 | + } else { |
|
| 1210 | + spip_log("Warning: '$v' n'est pas de type $t", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1211 | + |
|
| 1212 | + return intval($v); |
|
| 1213 | + } |
|
| 1214 | 1214 | } |
| 1215 | 1215 | |
| 1216 | 1216 | function spip_pg_hex($v) { |
| 1217 | - return "CAST(x'" . $v . "' as bigint)"; |
|
| 1217 | + return "CAST(x'" . $v . "' as bigint)"; |
|
| 1218 | 1218 | } |
| 1219 | 1219 | |
| 1220 | 1220 | function spip_pg_quote($v, $type = '') { |
| 1221 | - if (!is_array($v)) { |
|
| 1222 | - return spip_pg_cite($v, $type); |
|
| 1223 | - } |
|
| 1224 | - // si c'est un tableau, le parcourir en propageant le type |
|
| 1225 | - foreach ($v as $k => $r) { |
|
| 1226 | - $v[$k] = spip_pg_quote($r, $type); |
|
| 1227 | - } |
|
| 1228 | - |
|
| 1229 | - return join(',', $v); |
|
| 1221 | + if (!is_array($v)) { |
|
| 1222 | + return spip_pg_cite($v, $type); |
|
| 1223 | + } |
|
| 1224 | + // si c'est un tableau, le parcourir en propageant le type |
|
| 1225 | + foreach ($v as $k => $r) { |
|
| 1226 | + $v[$k] = spip_pg_quote($r, $type); |
|
| 1227 | + } |
|
| 1228 | + |
|
| 1229 | + return join(',', $v); |
|
| 1230 | 1230 | } |
| 1231 | 1231 | |
| 1232 | 1232 | function spip_pg_date_proche($champ, $interval, $unite) { |
| 1233 | - return '(' |
|
| 1234 | - . $champ |
|
| 1235 | - . (($interval <= 0) ? '>' : '<') |
|
| 1236 | - . (($interval <= 0) ? 'DATE_SUB' : 'DATE_ADD') |
|
| 1237 | - . '(' |
|
| 1238 | - . sql_quote(date('Y-m-d H:i:s')) |
|
| 1239 | - . ', INTERVAL ' |
|
| 1240 | - . (($interval > 0) ? $interval : (0 - $interval)) |
|
| 1241 | - . ' ' |
|
| 1242 | - . $unite |
|
| 1243 | - . '))'; |
|
| 1233 | + return '(' |
|
| 1234 | + . $champ |
|
| 1235 | + . (($interval <= 0) ? '>' : '<') |
|
| 1236 | + . (($interval <= 0) ? 'DATE_SUB' : 'DATE_ADD') |
|
| 1237 | + . '(' |
|
| 1238 | + . sql_quote(date('Y-m-d H:i:s')) |
|
| 1239 | + . ', INTERVAL ' |
|
| 1240 | + . (($interval > 0) ? $interval : (0 - $interval)) |
|
| 1241 | + . ' ' |
|
| 1242 | + . $unite |
|
| 1243 | + . '))'; |
|
| 1244 | 1244 | } |
| 1245 | 1245 | |
| 1246 | 1246 | function spip_pg_in($val, $valeurs, $not = '', $serveur = '') { |
| 1247 | 1247 | // |
| 1248 | 1248 | // IN (...) souvent limite a 255 elements, d'ou cette fonction assistante |
| 1249 | 1249 | // |
| 1250 | - // s'il n'y a pas de valeur, eviter de produire un IN vide: PG rale. |
|
| 1251 | - if (!$valeurs) { |
|
| 1252 | - return $not ? '0=0' : '0=1'; |
|
| 1253 | - } |
|
| 1254 | - if (strpos($valeurs, "CAST(x'") !== false) { |
|
| 1255 | - return "($val=" . join("OR $val=", explode(',', $valeurs)) . ')'; |
|
| 1256 | - } |
|
| 1257 | - $n = $i = 0; |
|
| 1258 | - $in_sql = ''; |
|
| 1259 | - while ($n = strpos($valeurs, ',', $n + 1)) { |
|
| 1260 | - if ((++$i) >= 255) { |
|
| 1261 | - $in_sql .= "($val $not IN (" . |
|
| 1262 | - substr($valeurs, 0, $n) . |
|
| 1263 | - "))\n" . |
|
| 1264 | - ($not ? "AND\t" : "OR\t"); |
|
| 1265 | - $valeurs = substr($valeurs, $n + 1); |
|
| 1266 | - $i = $n = 0; |
|
| 1267 | - } |
|
| 1268 | - } |
|
| 1269 | - $in_sql .= "($val $not IN ($valeurs))"; |
|
| 1270 | - |
|
| 1271 | - return "($in_sql)"; |
|
| 1250 | + // s'il n'y a pas de valeur, eviter de produire un IN vide: PG rale. |
|
| 1251 | + if (!$valeurs) { |
|
| 1252 | + return $not ? '0=0' : '0=1'; |
|
| 1253 | + } |
|
| 1254 | + if (strpos($valeurs, "CAST(x'") !== false) { |
|
| 1255 | + return "($val=" . join("OR $val=", explode(',', $valeurs)) . ')'; |
|
| 1256 | + } |
|
| 1257 | + $n = $i = 0; |
|
| 1258 | + $in_sql = ''; |
|
| 1259 | + while ($n = strpos($valeurs, ',', $n + 1)) { |
|
| 1260 | + if ((++$i) >= 255) { |
|
| 1261 | + $in_sql .= "($val $not IN (" . |
|
| 1262 | + substr($valeurs, 0, $n) . |
|
| 1263 | + "))\n" . |
|
| 1264 | + ($not ? "AND\t" : "OR\t"); |
|
| 1265 | + $valeurs = substr($valeurs, $n + 1); |
|
| 1266 | + $i = $n = 0; |
|
| 1267 | + } |
|
| 1268 | + } |
|
| 1269 | + $in_sql .= "($val $not IN ($valeurs))"; |
|
| 1270 | + |
|
| 1271 | + return "($in_sql)"; |
|
| 1272 | 1272 | } |
| 1273 | 1273 | |
| 1274 | 1274 | function spip_pg_error($query = '', $serveur = '', $requeter = true) { |
| 1275 | - $link = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]['link']; |
|
| 1276 | - $s = $link ? pg_last_error($link) : pg_last_error(); |
|
| 1277 | - if ($s) { |
|
| 1278 | - $s = str_replace('ERROR', 'errcode: 1000 ', $s); |
|
| 1279 | - spip_log("$s - $query", 'pg.' . _LOG_ERREUR); |
|
| 1280 | - } |
|
| 1281 | - |
|
| 1282 | - return $s; |
|
| 1275 | + $link = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]['link']; |
|
| 1276 | + $s = $link ? pg_last_error($link) : pg_last_error(); |
|
| 1277 | + if ($s) { |
|
| 1278 | + $s = str_replace('ERROR', 'errcode: 1000 ', $s); |
|
| 1279 | + spip_log("$s - $query", 'pg.' . _LOG_ERREUR); |
|
| 1280 | + } |
|
| 1281 | + |
|
| 1282 | + return $s; |
|
| 1283 | 1283 | } |
| 1284 | 1284 | |
| 1285 | 1285 | function spip_pg_errno($serveur = '') { |
| 1286 | - // il faudrait avoir la derniere ressource retournee et utiliser |
|
| 1287 | - // http://fr2.php.net/manual/fr/function.pg-result-error.php |
|
| 1288 | - return 0; |
|
| 1286 | + // il faudrait avoir la derniere ressource retournee et utiliser |
|
| 1287 | + // http://fr2.php.net/manual/fr/function.pg-result-error.php |
|
| 1288 | + return 0; |
|
| 1289 | 1289 | } |
| 1290 | 1290 | |
| 1291 | 1291 | function spip_pg_drop_table($table, $exist = '', $serveur = '', $requeter = true) { |
| 1292 | - if ($exist) { |
|
| 1293 | - $exist = ' IF EXISTS'; |
|
| 1294 | - } |
|
| 1295 | - if (spip_pg_query("DROP TABLE$exist $table", $serveur, $requeter)) { |
|
| 1296 | - return true; |
|
| 1297 | - } else { |
|
| 1298 | - return false; |
|
| 1299 | - } |
|
| 1292 | + if ($exist) { |
|
| 1293 | + $exist = ' IF EXISTS'; |
|
| 1294 | + } |
|
| 1295 | + if (spip_pg_query("DROP TABLE$exist $table", $serveur, $requeter)) { |
|
| 1296 | + return true; |
|
| 1297 | + } else { |
|
| 1298 | + return false; |
|
| 1299 | + } |
|
| 1300 | 1300 | } |
| 1301 | 1301 | |
| 1302 | 1302 | // supprime une vue |
| 1303 | 1303 | function spip_pg_drop_view($view, $exist = '', $serveur = '', $requeter = true) { |
| 1304 | - if ($exist) { |
|
| 1305 | - $exist = ' IF EXISTS'; |
|
| 1306 | - } |
|
| 1304 | + if ($exist) { |
|
| 1305 | + $exist = ' IF EXISTS'; |
|
| 1306 | + } |
|
| 1307 | 1307 | |
| 1308 | - return spip_pg_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 1308 | + return spip_pg_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 1309 | 1309 | } |
| 1310 | 1310 | |
| 1311 | 1311 | /** |
@@ -1322,40 +1322,40 @@ discard block |
||
| 1322 | 1322 | * Ressource à utiliser avec sql_fetch() |
| 1323 | 1323 | **/ |
| 1324 | 1324 | function spip_pg_showbase($match, $serveur = '', $requeter = true) { |
| 1325 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1326 | - $link = $connexion['link']; |
|
| 1327 | - $connexion['last'] = $q = 'SELECT tablename FROM pg_tables WHERE tablename ILIKE ' . _q($match); |
|
| 1325 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1326 | + $link = $connexion['link']; |
|
| 1327 | + $connexion['last'] = $q = 'SELECT tablename FROM pg_tables WHERE tablename ILIKE ' . _q($match); |
|
| 1328 | 1328 | |
| 1329 | - return spip_pg_query_simple($link, $q); |
|
| 1329 | + return spip_pg_query_simple($link, $q); |
|
| 1330 | 1330 | } |
| 1331 | 1331 | |
| 1332 | 1332 | function spip_pg_showtable($nom_table, $serveur = '', $requeter = true) { |
| 1333 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1334 | - $link = $connexion['link']; |
|
| 1335 | - $connexion['last'] = $q = 'SELECT column_name, column_default, data_type FROM information_schema.columns WHERE table_name ILIKE ' . _q($nom_table); |
|
| 1336 | - |
|
| 1337 | - $res = spip_pg_query_simple($link, $q); |
|
| 1338 | - if (!$res) { |
|
| 1339 | - return false; |
|
| 1340 | - } |
|
| 1341 | - |
|
| 1342 | - // etrangement, $res peut ne rien contenir, mais arriver ici... |
|
| 1343 | - // il faut en tenir compte dans le return |
|
| 1344 | - $fields = []; |
|
| 1345 | - while ($field = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 1346 | - $fields[$field[0]] = $field[2] . (!$field[1] ? '' : (' DEFAULT ' . $field[1])); |
|
| 1347 | - } |
|
| 1348 | - $connexion['last'] = $q = 'SELECT indexdef FROM pg_indexes WHERE tablename ILIKE ' . _q($nom_table); |
|
| 1349 | - $res = spip_pg_query_simple($link, $q); |
|
| 1350 | - $keys = []; |
|
| 1351 | - while ($index = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 1352 | - if (preg_match('/CREATE\s+(UNIQUE\s+)?INDEX\s([^\s]+).*\((.*)\)$/', $index[0], $r)) { |
|
| 1353 | - $nom = str_replace($nom_table . '_', '', $r[2]); |
|
| 1354 | - $keys[($r[1] ? 'PRIMARY KEY' : ('KEY ' . $nom))] = $r[3]; |
|
| 1355 | - } |
|
| 1356 | - } |
|
| 1357 | - |
|
| 1358 | - return count($fields) ? ['field' => $fields, 'key' => $keys] : false; |
|
| 1333 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1334 | + $link = $connexion['link']; |
|
| 1335 | + $connexion['last'] = $q = 'SELECT column_name, column_default, data_type FROM information_schema.columns WHERE table_name ILIKE ' . _q($nom_table); |
|
| 1336 | + |
|
| 1337 | + $res = spip_pg_query_simple($link, $q); |
|
| 1338 | + if (!$res) { |
|
| 1339 | + return false; |
|
| 1340 | + } |
|
| 1341 | + |
|
| 1342 | + // etrangement, $res peut ne rien contenir, mais arriver ici... |
|
| 1343 | + // il faut en tenir compte dans le return |
|
| 1344 | + $fields = []; |
|
| 1345 | + while ($field = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 1346 | + $fields[$field[0]] = $field[2] . (!$field[1] ? '' : (' DEFAULT ' . $field[1])); |
|
| 1347 | + } |
|
| 1348 | + $connexion['last'] = $q = 'SELECT indexdef FROM pg_indexes WHERE tablename ILIKE ' . _q($nom_table); |
|
| 1349 | + $res = spip_pg_query_simple($link, $q); |
|
| 1350 | + $keys = []; |
|
| 1351 | + while ($index = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 1352 | + if (preg_match('/CREATE\s+(UNIQUE\s+)?INDEX\s([^\s]+).*\((.*)\)$/', $index[0], $r)) { |
|
| 1353 | + $nom = str_replace($nom_table . '_', '', $r[2]); |
|
| 1354 | + $keys[($r[1] ? 'PRIMARY KEY' : ('KEY ' . $nom))] = $r[3]; |
|
| 1355 | + } |
|
| 1356 | + } |
|
| 1357 | + |
|
| 1358 | + return count($fields) ? ['field' => $fields, 'key' => $keys] : false; |
|
| 1359 | 1359 | } |
| 1360 | 1360 | |
| 1361 | 1361 | // Fonction de creation d'une table SQL nommee $nom |
@@ -1366,116 +1366,116 @@ discard block |
||
| 1366 | 1366 | // Le nom des index est prefixe par celui de la table pour eviter les conflits |
| 1367 | 1367 | function spip_pg_create($nom, $champs, $cles, $autoinc = false, $temporary = false, $serveur = '', $requeter = true) { |
| 1368 | 1368 | |
| 1369 | - $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1370 | - $link = $connexion['link']; |
|
| 1371 | - $nom = prefixer_table_spip($nom, $connexion['prefixe']); |
|
| 1372 | - |
|
| 1373 | - $query = $prim = $prim_name = $v = $s = $p = ''; |
|
| 1374 | - $keys = []; |
|
| 1375 | - |
|
| 1376 | - // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 1377 | - // sans les renseigner (laisse le compilo recuperer la description) |
|
| 1378 | - if (!is_array($champs) || !is_array($cles)) { |
|
| 1379 | - return; |
|
| 1380 | - } |
|
| 1381 | - |
|
| 1382 | - foreach ($cles as $k => $v) { |
|
| 1383 | - if (strpos($k, 'KEY ') === 0) { |
|
| 1384 | - $n = str_replace('`', '', $k); |
|
| 1385 | - $v = str_replace('`', '"', $v); |
|
| 1386 | - $i = $nom . preg_replace('/KEY +/', '_', $n); |
|
| 1387 | - if ($k != $n) { |
|
| 1388 | - $i = "\"$i\""; |
|
| 1389 | - } |
|
| 1390 | - $keys[] = "CREATE INDEX $i ON $nom ($v);"; |
|
| 1391 | - } elseif (strpos($k, 'UNIQUE ') === 0) { |
|
| 1392 | - $k = preg_replace('/^UNIQUE +/', '', $k); |
|
| 1393 | - $prim .= "$s\n\t\tCONSTRAINT " . str_replace('`', '"', $k) . " UNIQUE ($v)"; |
|
| 1394 | - } else { |
|
| 1395 | - $prim .= "$s\n\t\t" . str_replace('`', '"', $k) . " ($v)"; |
|
| 1396 | - } |
|
| 1397 | - if ($k == 'PRIMARY KEY') { |
|
| 1398 | - $prim_name = $v; |
|
| 1399 | - } |
|
| 1400 | - $s = ','; |
|
| 1401 | - } |
|
| 1402 | - $s = ''; |
|
| 1403 | - |
|
| 1404 | - $character_set = ''; |
|
| 1405 | - if (@$GLOBALS['meta']['charset_sql_base']) { |
|
| 1406 | - $character_set .= ' CHARACTER SET ' . $GLOBALS['meta']['charset_sql_base']; |
|
| 1407 | - } |
|
| 1408 | - if (@$GLOBALS['meta']['charset_collation_sql_base']) { |
|
| 1409 | - $character_set .= ' COLLATE ' . $GLOBALS['meta']['charset_collation_sql_base']; |
|
| 1410 | - } |
|
| 1411 | - |
|
| 1412 | - foreach ($champs as $k => $v) { |
|
| 1413 | - $k = str_replace('`', '"', $k); |
|
| 1414 | - if (preg_match(',([a-z]*\s*(\(\s*[0-9]*\s*\))?(\s*binary)?),i', $v, $defs)) { |
|
| 1415 | - if (preg_match(',(char|text),i', $defs[1]) and !preg_match(',binary,i', $defs[1])) { |
|
| 1416 | - $v = $defs[1] . $character_set . ' ' . substr($v, strlen($defs[1])); |
|
| 1417 | - } |
|
| 1418 | - } |
|
| 1419 | - |
|
| 1420 | - $query .= "$s\n\t\t$k " |
|
| 1421 | - . (($autoinc && ($prim_name == $k) && preg_match(',\b(big|small|medium|tiny)?int\b,i', $v)) |
|
| 1422 | - ? ' bigserial' |
|
| 1423 | - : mysql2pg_type($v) |
|
| 1424 | - ); |
|
| 1425 | - $s = ','; |
|
| 1426 | - } |
|
| 1427 | - $temporary = $temporary ? 'TEMPORARY' : ''; |
|
| 1428 | - |
|
| 1429 | - // En l'absence de "if not exists" en PG, on neutralise les erreurs |
|
| 1430 | - |
|
| 1431 | - $q = "CREATE $temporary TABLE $nom ($query" . ($prim ? ",$prim" : '') . ')' . |
|
| 1432 | - ($character_set ? " DEFAULT $character_set" : '') |
|
| 1433 | - . "\n"; |
|
| 1434 | - |
|
| 1435 | - if (!$requeter) { |
|
| 1436 | - return $q; |
|
| 1437 | - } |
|
| 1438 | - $connexion['last'] = $q; |
|
| 1439 | - $r = @pg_query($link, $q); |
|
| 1440 | - |
|
| 1441 | - if (!$r) { |
|
| 1442 | - spip_log("Impossible de creer cette table: $q", 'pg.' . _LOG_ERREUR); |
|
| 1443 | - } else { |
|
| 1444 | - foreach ($keys as $index) { |
|
| 1445 | - pg_query($link, $index); |
|
| 1446 | - } |
|
| 1447 | - } |
|
| 1448 | - |
|
| 1449 | - return $r; |
|
| 1369 | + $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1370 | + $link = $connexion['link']; |
|
| 1371 | + $nom = prefixer_table_spip($nom, $connexion['prefixe']); |
|
| 1372 | + |
|
| 1373 | + $query = $prim = $prim_name = $v = $s = $p = ''; |
|
| 1374 | + $keys = []; |
|
| 1375 | + |
|
| 1376 | + // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 1377 | + // sans les renseigner (laisse le compilo recuperer la description) |
|
| 1378 | + if (!is_array($champs) || !is_array($cles)) { |
|
| 1379 | + return; |
|
| 1380 | + } |
|
| 1381 | + |
|
| 1382 | + foreach ($cles as $k => $v) { |
|
| 1383 | + if (strpos($k, 'KEY ') === 0) { |
|
| 1384 | + $n = str_replace('`', '', $k); |
|
| 1385 | + $v = str_replace('`', '"', $v); |
|
| 1386 | + $i = $nom . preg_replace('/KEY +/', '_', $n); |
|
| 1387 | + if ($k != $n) { |
|
| 1388 | + $i = "\"$i\""; |
|
| 1389 | + } |
|
| 1390 | + $keys[] = "CREATE INDEX $i ON $nom ($v);"; |
|
| 1391 | + } elseif (strpos($k, 'UNIQUE ') === 0) { |
|
| 1392 | + $k = preg_replace('/^UNIQUE +/', '', $k); |
|
| 1393 | + $prim .= "$s\n\t\tCONSTRAINT " . str_replace('`', '"', $k) . " UNIQUE ($v)"; |
|
| 1394 | + } else { |
|
| 1395 | + $prim .= "$s\n\t\t" . str_replace('`', '"', $k) . " ($v)"; |
|
| 1396 | + } |
|
| 1397 | + if ($k == 'PRIMARY KEY') { |
|
| 1398 | + $prim_name = $v; |
|
| 1399 | + } |
|
| 1400 | + $s = ','; |
|
| 1401 | + } |
|
| 1402 | + $s = ''; |
|
| 1403 | + |
|
| 1404 | + $character_set = ''; |
|
| 1405 | + if (@$GLOBALS['meta']['charset_sql_base']) { |
|
| 1406 | + $character_set .= ' CHARACTER SET ' . $GLOBALS['meta']['charset_sql_base']; |
|
| 1407 | + } |
|
| 1408 | + if (@$GLOBALS['meta']['charset_collation_sql_base']) { |
|
| 1409 | + $character_set .= ' COLLATE ' . $GLOBALS['meta']['charset_collation_sql_base']; |
|
| 1410 | + } |
|
| 1411 | + |
|
| 1412 | + foreach ($champs as $k => $v) { |
|
| 1413 | + $k = str_replace('`', '"', $k); |
|
| 1414 | + if (preg_match(',([a-z]*\s*(\(\s*[0-9]*\s*\))?(\s*binary)?),i', $v, $defs)) { |
|
| 1415 | + if (preg_match(',(char|text),i', $defs[1]) and !preg_match(',binary,i', $defs[1])) { |
|
| 1416 | + $v = $defs[1] . $character_set . ' ' . substr($v, strlen($defs[1])); |
|
| 1417 | + } |
|
| 1418 | + } |
|
| 1419 | + |
|
| 1420 | + $query .= "$s\n\t\t$k " |
|
| 1421 | + . (($autoinc && ($prim_name == $k) && preg_match(',\b(big|small|medium|tiny)?int\b,i', $v)) |
|
| 1422 | + ? ' bigserial' |
|
| 1423 | + : mysql2pg_type($v) |
|
| 1424 | + ); |
|
| 1425 | + $s = ','; |
|
| 1426 | + } |
|
| 1427 | + $temporary = $temporary ? 'TEMPORARY' : ''; |
|
| 1428 | + |
|
| 1429 | + // En l'absence de "if not exists" en PG, on neutralise les erreurs |
|
| 1430 | + |
|
| 1431 | + $q = "CREATE $temporary TABLE $nom ($query" . ($prim ? ",$prim" : '') . ')' . |
|
| 1432 | + ($character_set ? " DEFAULT $character_set" : '') |
|
| 1433 | + . "\n"; |
|
| 1434 | + |
|
| 1435 | + if (!$requeter) { |
|
| 1436 | + return $q; |
|
| 1437 | + } |
|
| 1438 | + $connexion['last'] = $q; |
|
| 1439 | + $r = @pg_query($link, $q); |
|
| 1440 | + |
|
| 1441 | + if (!$r) { |
|
| 1442 | + spip_log("Impossible de creer cette table: $q", 'pg.' . _LOG_ERREUR); |
|
| 1443 | + } else { |
|
| 1444 | + foreach ($keys as $index) { |
|
| 1445 | + pg_query($link, $index); |
|
| 1446 | + } |
|
| 1447 | + } |
|
| 1448 | + |
|
| 1449 | + return $r; |
|
| 1450 | 1450 | } |
| 1451 | 1451 | |
| 1452 | 1452 | |
| 1453 | 1453 | function spip_pg_create_base($nom, $serveur = '', $requeter = true) { |
| 1454 | - return spip_pg_query("CREATE DATABASE $nom", $serveur, $requeter); |
|
| 1454 | + return spip_pg_query("CREATE DATABASE $nom", $serveur, $requeter); |
|
| 1455 | 1455 | } |
| 1456 | 1456 | |
| 1457 | 1457 | // Fonction de creation d'une vue SQL nommee $nom |
| 1458 | 1458 | function spip_pg_create_view($nom, $query_select, $serveur = '', $requeter = true) { |
| 1459 | - if (!$query_select) { |
|
| 1460 | - return false; |
|
| 1461 | - } |
|
| 1462 | - // vue deja presente |
|
| 1463 | - if (sql_showtable($nom, false, $serveur)) { |
|
| 1464 | - if ($requeter) { |
|
| 1465 | - spip_log("Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", 'pg.' . _LOG_ERREUR); |
|
| 1466 | - } |
|
| 1459 | + if (!$query_select) { |
|
| 1460 | + return false; |
|
| 1461 | + } |
|
| 1462 | + // vue deja presente |
|
| 1463 | + if (sql_showtable($nom, false, $serveur)) { |
|
| 1464 | + if ($requeter) { |
|
| 1465 | + spip_log("Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", 'pg.' . _LOG_ERREUR); |
|
| 1466 | + } |
|
| 1467 | 1467 | |
| 1468 | - return false; |
|
| 1469 | - } |
|
| 1468 | + return false; |
|
| 1469 | + } |
|
| 1470 | 1470 | |
| 1471 | - $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 1471 | + $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 1472 | 1472 | |
| 1473 | - return spip_pg_query($query, $serveur, $requeter); |
|
| 1473 | + return spip_pg_query($query, $serveur, $requeter); |
|
| 1474 | 1474 | } |
| 1475 | 1475 | |
| 1476 | 1476 | |
| 1477 | 1477 | function spip_pg_set_connect_charset($charset, $serveur = '', $requeter = true) { |
| 1478 | - spip_log('changement de charset sql a ecrire en PG', 'pg.' . _LOG_ERREUR); |
|
| 1478 | + spip_log('changement de charset sql a ecrire en PG', 'pg.' . _LOG_ERREUR); |
|
| 1479 | 1479 | } |
| 1480 | 1480 | |
| 1481 | 1481 | |
@@ -1488,50 +1488,50 @@ discard block |
||
| 1488 | 1488 | * @return bool|string true / false / requete |
| 1489 | 1489 | **/ |
| 1490 | 1490 | function spip_pg_optimize($table, $serveur = '', $requeter = true) { |
| 1491 | - return spip_pg_query('VACUUM ' . $table, $serveur, $requeter); |
|
| 1491 | + return spip_pg_query('VACUUM ' . $table, $serveur, $requeter); |
|
| 1492 | 1492 | } |
| 1493 | 1493 | |
| 1494 | 1494 | // Selectionner la sous-chaine dans $objet |
| 1495 | 1495 | // correspondant a $lang. Cf balise Multi de Spip |
| 1496 | 1496 | |
| 1497 | 1497 | function spip_pg_multi($objet, $lang) { |
| 1498 | - $r = 'regexp_replace(' |
|
| 1499 | - . $objet |
|
| 1500 | - . ",'<multi>.*[[]" |
|
| 1501 | - . $lang |
|
| 1502 | - . "[]]([^[]*).*</multi>', E'\\\\1') AS multi"; |
|
| 1498 | + $r = 'regexp_replace(' |
|
| 1499 | + . $objet |
|
| 1500 | + . ",'<multi>.*[[]" |
|
| 1501 | + . $lang |
|
| 1502 | + . "[]]([^[]*).*</multi>', E'\\\\1') AS multi"; |
|
| 1503 | 1503 | |
| 1504 | - return $r; |
|
| 1504 | + return $r; |
|
| 1505 | 1505 | } |
| 1506 | 1506 | |
| 1507 | 1507 | // Palanquee d'idiosyncrasies MySQL dans les creations de table |
| 1508 | 1508 | // A completer par les autres, mais essayer de reduire en amont. |
| 1509 | 1509 | |
| 1510 | 1510 | function mysql2pg_type($v) { |
| 1511 | - $remplace = [ |
|
| 1512 | - '/auto_increment/i' => '', // non reconnu |
|
| 1513 | - '/bigint/i' => 'bigint', |
|
| 1514 | - '/mediumint/i' => 'mediumint', |
|
| 1515 | - '/smallint/i' => 'smallint', |
|
| 1516 | - '/tinyint/i' => 'int', |
|
| 1517 | - '/int\s*[(]\s*\d+\s*[)]/i' => 'int', |
|
| 1518 | - '/longtext/i' => 'text', |
|
| 1519 | - '/mediumtext/i' => 'text', |
|
| 1520 | - '/tinytext/i' => 'text', |
|
| 1521 | - '/longblob/i' => 'text', |
|
| 1522 | - '/0000-00-00/' => '0001-01-01', |
|
| 1523 | - '/datetime/i' => 'timestamp', |
|
| 1524 | - '/unsigned/i' => '', |
|
| 1525 | - '/double/i' => 'double precision', |
|
| 1526 | - '/VARCHAR\((\d+)\)\s+BINARY/i' => 'varchar(\1)', |
|
| 1527 | - '/ENUM *[(][^)]*[)]/i' => 'varchar(255)', |
|
| 1528 | - '/(timestamp .* )ON .*$/is' => '\\1', |
|
| 1529 | - ]; |
|
| 1530 | - |
|
| 1531 | - return preg_replace(array_keys($remplace), array_values($remplace), $v); |
|
| 1511 | + $remplace = [ |
|
| 1512 | + '/auto_increment/i' => '', // non reconnu |
|
| 1513 | + '/bigint/i' => 'bigint', |
|
| 1514 | + '/mediumint/i' => 'mediumint', |
|
| 1515 | + '/smallint/i' => 'smallint', |
|
| 1516 | + '/tinyint/i' => 'int', |
|
| 1517 | + '/int\s*[(]\s*\d+\s*[)]/i' => 'int', |
|
| 1518 | + '/longtext/i' => 'text', |
|
| 1519 | + '/mediumtext/i' => 'text', |
|
| 1520 | + '/tinytext/i' => 'text', |
|
| 1521 | + '/longblob/i' => 'text', |
|
| 1522 | + '/0000-00-00/' => '0001-01-01', |
|
| 1523 | + '/datetime/i' => 'timestamp', |
|
| 1524 | + '/unsigned/i' => '', |
|
| 1525 | + '/double/i' => 'double precision', |
|
| 1526 | + '/VARCHAR\((\d+)\)\s+BINARY/i' => 'varchar(\1)', |
|
| 1527 | + '/ENUM *[(][^)]*[)]/i' => 'varchar(255)', |
|
| 1528 | + '/(timestamp .* )ON .*$/is' => '\\1', |
|
| 1529 | + ]; |
|
| 1530 | + |
|
| 1531 | + return preg_replace(array_keys($remplace), array_values($remplace), $v); |
|
| 1532 | 1532 | } |
| 1533 | 1533 | |
| 1534 | 1534 | // Renvoie false si on n'a pas les fonctions pg (pour l'install) |
| 1535 | 1535 | function spip_versions_pg() { |
| 1536 | - return function_exists('pg_connect'); |
|
| 1536 | + return function_exists('pg_connect'); |
|
| 1537 | 1537 | } |
@@ -19,7 +19,7 @@ discard block |
||
| 19 | 19 | */ |
| 20 | 20 | |
| 21 | 21 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 22 | - return; |
|
| 22 | + return; |
|
| 23 | 23 | } |
| 24 | 24 | |
| 25 | 25 | |
@@ -55,65 +55,65 @@ discard block |
||
| 55 | 55 | * @return void |
| 56 | 56 | **/ |
| 57 | 57 | function calculer_rubriques_if($id_rubrique, $modifs, $infos = [], $postdate = false) { |
| 58 | - $statuts_publies = null; |
|
| 59 | - $neuf = false; |
|
| 60 | - |
|
| 61 | - // Compat avec l'ancienne signature |
|
| 62 | - if (is_string($infos)) { |
|
| 63 | - $infos = ['statut_ancien' => $infos]; |
|
| 64 | - } |
|
| 65 | - if (!isset($infos['statut_ancien'])) { |
|
| 66 | - $infos['statut_ancien'] = ''; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - // On recherche quels statuts tester |
|
| 70 | - if ( |
|
| 71 | - isset($infos['objet']) |
|
| 72 | - and include_spip('inc/filtres') |
|
| 73 | - and $declaration_statut = objet_info($infos['objet'], 'statut') |
|
| 74 | - and is_array($declaration_statut) |
|
| 75 | - ) { |
|
| 76 | - foreach ($declaration_statut as $champ_statut) { |
|
| 77 | - if ($champ_statut['champ'] == 'statut') { |
|
| 78 | - $statuts_publies = array_map('trim', explode(',', $champ_statut['publie'])); |
|
| 79 | - break; // stop on a trouvé le bon champ |
|
| 80 | - } |
|
| 81 | - } |
|
| 82 | - } else { |
|
| 83 | - $statuts_publies = ['publie']; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - if (in_array($infos['statut_ancien'], $statuts_publies)) { |
|
| 87 | - if ( |
|
| 88 | - isset($modifs['statut']) |
|
| 89 | - or isset($modifs['id_rubrique']) |
|
| 90 | - or ($postdate and strtotime($postdate) > time()) |
|
| 91 | - ) { |
|
| 92 | - $neuf |= depublier_branche_rubrique_if($id_rubrique); |
|
| 93 | - } |
|
| 94 | - // ne publier que si c'est pas un postdate, ou si la date n'est pas dans le futur |
|
| 95 | - if ($postdate) { |
|
| 96 | - calculer_prochain_postdate(true); |
|
| 97 | - $neuf |= (strtotime($postdate) <= time()); // par securite |
|
| 98 | - } elseif (isset($modifs['id_rubrique'])) { |
|
| 99 | - $neuf |= publier_branche_rubrique($modifs['id_rubrique']); |
|
| 100 | - } |
|
| 101 | - } elseif (isset($modifs['statut']) and in_array($modifs['statut'], $statuts_publies)) { |
|
| 102 | - if ($postdate) { |
|
| 103 | - calculer_prochain_postdate(true); |
|
| 104 | - $neuf |= (strtotime($postdate) <= time()); // par securite |
|
| 105 | - } else { |
|
| 106 | - $neuf |= publier_branche_rubrique($id_rubrique); |
|
| 107 | - } |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - if ($neuf) { |
|
| 111 | - // Sauver la date de la derniere mise a jour (pour menu_rubriques) |
|
| 112 | - ecrire_meta('date_calcul_rubriques', date('U')); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - $langues = calculer_langues_utilisees(); |
|
| 116 | - ecrire_meta('langues_utilisees', $langues); |
|
| 58 | + $statuts_publies = null; |
|
| 59 | + $neuf = false; |
|
| 60 | + |
|
| 61 | + // Compat avec l'ancienne signature |
|
| 62 | + if (is_string($infos)) { |
|
| 63 | + $infos = ['statut_ancien' => $infos]; |
|
| 64 | + } |
|
| 65 | + if (!isset($infos['statut_ancien'])) { |
|
| 66 | + $infos['statut_ancien'] = ''; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + // On recherche quels statuts tester |
|
| 70 | + if ( |
|
| 71 | + isset($infos['objet']) |
|
| 72 | + and include_spip('inc/filtres') |
|
| 73 | + and $declaration_statut = objet_info($infos['objet'], 'statut') |
|
| 74 | + and is_array($declaration_statut) |
|
| 75 | + ) { |
|
| 76 | + foreach ($declaration_statut as $champ_statut) { |
|
| 77 | + if ($champ_statut['champ'] == 'statut') { |
|
| 78 | + $statuts_publies = array_map('trim', explode(',', $champ_statut['publie'])); |
|
| 79 | + break; // stop on a trouvé le bon champ |
|
| 80 | + } |
|
| 81 | + } |
|
| 82 | + } else { |
|
| 83 | + $statuts_publies = ['publie']; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + if (in_array($infos['statut_ancien'], $statuts_publies)) { |
|
| 87 | + if ( |
|
| 88 | + isset($modifs['statut']) |
|
| 89 | + or isset($modifs['id_rubrique']) |
|
| 90 | + or ($postdate and strtotime($postdate) > time()) |
|
| 91 | + ) { |
|
| 92 | + $neuf |= depublier_branche_rubrique_if($id_rubrique); |
|
| 93 | + } |
|
| 94 | + // ne publier que si c'est pas un postdate, ou si la date n'est pas dans le futur |
|
| 95 | + if ($postdate) { |
|
| 96 | + calculer_prochain_postdate(true); |
|
| 97 | + $neuf |= (strtotime($postdate) <= time()); // par securite |
|
| 98 | + } elseif (isset($modifs['id_rubrique'])) { |
|
| 99 | + $neuf |= publier_branche_rubrique($modifs['id_rubrique']); |
|
| 100 | + } |
|
| 101 | + } elseif (isset($modifs['statut']) and in_array($modifs['statut'], $statuts_publies)) { |
|
| 102 | + if ($postdate) { |
|
| 103 | + calculer_prochain_postdate(true); |
|
| 104 | + $neuf |= (strtotime($postdate) <= time()); // par securite |
|
| 105 | + } else { |
|
| 106 | + $neuf |= publier_branche_rubrique($id_rubrique); |
|
| 107 | + } |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + if ($neuf) { |
|
| 111 | + // Sauver la date de la derniere mise a jour (pour menu_rubriques) |
|
| 112 | + ecrire_meta('date_calcul_rubriques', date('U')); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + $langues = calculer_langues_utilisees(); |
|
| 116 | + ecrire_meta('langues_utilisees', $langues); |
|
| 117 | 117 | } |
| 118 | 118 | |
| 119 | 119 | |
@@ -131,22 +131,22 @@ discard block |
||
| 131 | 131 | * true si le statut change effectivement |
| 132 | 132 | */ |
| 133 | 133 | function publier_branche_rubrique($id_rubrique) { |
| 134 | - $id_pred = $id_rubrique; |
|
| 135 | - while (true) { |
|
| 136 | - sql_updateq( |
|
| 137 | - 'spip_rubriques', |
|
| 138 | - ['statut' => 'publie', 'date' => date('Y-m-d H:i:s')], |
|
| 139 | - 'id_rubrique=' . intval($id_rubrique) |
|
| 140 | - ); |
|
| 141 | - $id_parent = sql_getfetsel('id_parent', 'spip_rubriques AS R', 'R.id_rubrique=' . intval($id_rubrique)); |
|
| 142 | - if (!$id_parent) { |
|
| 143 | - break; |
|
| 144 | - } |
|
| 145 | - $id_rubrique = $id_parent; |
|
| 146 | - } |
|
| 134 | + $id_pred = $id_rubrique; |
|
| 135 | + while (true) { |
|
| 136 | + sql_updateq( |
|
| 137 | + 'spip_rubriques', |
|
| 138 | + ['statut' => 'publie', 'date' => date('Y-m-d H:i:s')], |
|
| 139 | + 'id_rubrique=' . intval($id_rubrique) |
|
| 140 | + ); |
|
| 141 | + $id_parent = sql_getfetsel('id_parent', 'spip_rubriques AS R', 'R.id_rubrique=' . intval($id_rubrique)); |
|
| 142 | + if (!$id_parent) { |
|
| 143 | + break; |
|
| 144 | + } |
|
| 145 | + $id_rubrique = $id_parent; |
|
| 146 | + } |
|
| 147 | 147 | |
| 148 | 148 | # spip_log(" publier_branche_rubrique($id_rubrique $id_pred"); |
| 149 | - return $id_pred != $id_rubrique; |
|
| 149 | + return $id_pred != $id_rubrique; |
|
| 150 | 150 | } |
| 151 | 151 | |
| 152 | 152 | /** |
@@ -164,20 +164,20 @@ discard block |
||
| 164 | 164 | * true si le statut change effectivement |
| 165 | 165 | */ |
| 166 | 166 | function depublier_branche_rubrique_if($id_rubrique) { |
| 167 | - $date = date('Y-m-d H:i:s'); // figer la date |
|
| 168 | - |
|
| 169 | - # spip_log("depublier_branche_rubrique($id_rubrique ?"); |
|
| 170 | - $id_pred = $id_rubrique; |
|
| 171 | - while ($id_pred) { |
|
| 172 | - if (!depublier_rubrique_if($id_pred, $date)) { |
|
| 173 | - return $id_pred != $id_rubrique; |
|
| 174 | - } |
|
| 175 | - // passer au parent si on a depublie |
|
| 176 | - $r = sql_fetsel('id_parent', 'spip_rubriques', 'id_rubrique=' . intval($id_pred)); |
|
| 177 | - $id_pred = $r['id_parent']; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - return $id_pred != $id_rubrique; |
|
| 167 | + $date = date('Y-m-d H:i:s'); // figer la date |
|
| 168 | + |
|
| 169 | + # spip_log("depublier_branche_rubrique($id_rubrique ?"); |
|
| 170 | + $id_pred = $id_rubrique; |
|
| 171 | + while ($id_pred) { |
|
| 172 | + if (!depublier_rubrique_if($id_pred, $date)) { |
|
| 173 | + return $id_pred != $id_rubrique; |
|
| 174 | + } |
|
| 175 | + // passer au parent si on a depublie |
|
| 176 | + $r = sql_fetsel('id_parent', 'spip_rubriques', 'id_rubrique=' . intval($id_pred)); |
|
| 177 | + $id_pred = $r['id_parent']; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + return $id_pred != $id_rubrique; |
|
| 181 | 181 | } |
| 182 | 182 | |
| 183 | 183 | /** |
@@ -194,61 +194,61 @@ discard block |
||
| 194 | 194 | * true si la rubrique a été dépubliée |
| 195 | 195 | */ |
| 196 | 196 | function depublier_rubrique_if($id_rubrique, $date = null) { |
| 197 | - if (is_null($date)) { |
|
| 198 | - $date = date('Y-m-d H:i:s'); |
|
| 199 | - } |
|
| 200 | - $postdates = ($GLOBALS['meta']['post_dates'] == 'non') ? |
|
| 201 | - ' AND date <= ' . sql_quote($date) : ''; |
|
| 202 | - |
|
| 203 | - if (!$id_rubrique = intval($id_rubrique)) { |
|
| 204 | - return false; |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - // verifier qu'elle existe et est bien publiee |
|
| 208 | - $r = sql_fetsel('id_rubrique,statut', 'spip_rubriques', 'id_rubrique=' . intval($id_rubrique)); |
|
| 209 | - if (!$r or $r['statut'] !== 'publie') { |
|
| 210 | - return false; |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - // On met le nombre de chaque type d'enfants dans un tableau |
|
| 214 | - // Le type de l'objet est au pluriel |
|
| 215 | - $compte = [ |
|
| 216 | - 'articles' => sql_countsel( |
|
| 217 | - 'spip_articles', |
|
| 218 | - 'id_rubrique=' . intval($id_rubrique) . " AND statut='publie'$postdates" |
|
| 219 | - ), |
|
| 220 | - 'rubriques' => sql_countsel('spip_rubriques', 'id_parent=' . intval($id_rubrique) . " AND statut='publie'"), |
|
| 221 | - 'documents' => sql_countsel( |
|
| 222 | - 'spip_documents AS D JOIN spip_documents_liens AS L ON D.id_document=L.id_document', |
|
| 223 | - 'L.id_objet=' . intval($id_rubrique) . " AND L.objet='rubrique' and D.mode NOT IN('logoon', 'logooff') " |
|
| 224 | - ) |
|
| 225 | - ]; |
|
| 226 | - |
|
| 227 | - // On passe le tableau des comptes dans un pipeline pour que les plugins puissent ajouter (ou retirer) des enfants |
|
| 228 | - $compte = pipeline( |
|
| 229 | - 'objet_compte_enfants', |
|
| 230 | - [ |
|
| 231 | - 'args' => [ |
|
| 232 | - 'objet' => 'rubrique', |
|
| 233 | - 'id_objet' => $id_rubrique, |
|
| 234 | - 'statut' => 'publie', |
|
| 235 | - 'date' => $date |
|
| 236 | - ], |
|
| 237 | - 'data' => $compte |
|
| 238 | - ] |
|
| 239 | - ); |
|
| 240 | - |
|
| 241 | - // S'il y a au moins un enfant de n'importe quoi, on ne dépublie pas |
|
| 242 | - foreach ($compte as $objet => $n) { |
|
| 243 | - if ($n) { |
|
| 244 | - return false; |
|
| 245 | - } |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - sql_updateq('spip_rubriques', ['statut' => 'prepa'], 'id_rubrique=' . intval($id_rubrique)); |
|
| 197 | + if (is_null($date)) { |
|
| 198 | + $date = date('Y-m-d H:i:s'); |
|
| 199 | + } |
|
| 200 | + $postdates = ($GLOBALS['meta']['post_dates'] == 'non') ? |
|
| 201 | + ' AND date <= ' . sql_quote($date) : ''; |
|
| 202 | + |
|
| 203 | + if (!$id_rubrique = intval($id_rubrique)) { |
|
| 204 | + return false; |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + // verifier qu'elle existe et est bien publiee |
|
| 208 | + $r = sql_fetsel('id_rubrique,statut', 'spip_rubriques', 'id_rubrique=' . intval($id_rubrique)); |
|
| 209 | + if (!$r or $r['statut'] !== 'publie') { |
|
| 210 | + return false; |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + // On met le nombre de chaque type d'enfants dans un tableau |
|
| 214 | + // Le type de l'objet est au pluriel |
|
| 215 | + $compte = [ |
|
| 216 | + 'articles' => sql_countsel( |
|
| 217 | + 'spip_articles', |
|
| 218 | + 'id_rubrique=' . intval($id_rubrique) . " AND statut='publie'$postdates" |
|
| 219 | + ), |
|
| 220 | + 'rubriques' => sql_countsel('spip_rubriques', 'id_parent=' . intval($id_rubrique) . " AND statut='publie'"), |
|
| 221 | + 'documents' => sql_countsel( |
|
| 222 | + 'spip_documents AS D JOIN spip_documents_liens AS L ON D.id_document=L.id_document', |
|
| 223 | + 'L.id_objet=' . intval($id_rubrique) . " AND L.objet='rubrique' and D.mode NOT IN('logoon', 'logooff') " |
|
| 224 | + ) |
|
| 225 | + ]; |
|
| 226 | + |
|
| 227 | + // On passe le tableau des comptes dans un pipeline pour que les plugins puissent ajouter (ou retirer) des enfants |
|
| 228 | + $compte = pipeline( |
|
| 229 | + 'objet_compte_enfants', |
|
| 230 | + [ |
|
| 231 | + 'args' => [ |
|
| 232 | + 'objet' => 'rubrique', |
|
| 233 | + 'id_objet' => $id_rubrique, |
|
| 234 | + 'statut' => 'publie', |
|
| 235 | + 'date' => $date |
|
| 236 | + ], |
|
| 237 | + 'data' => $compte |
|
| 238 | + ] |
|
| 239 | + ); |
|
| 240 | + |
|
| 241 | + // S'il y a au moins un enfant de n'importe quoi, on ne dépublie pas |
|
| 242 | + foreach ($compte as $objet => $n) { |
|
| 243 | + if ($n) { |
|
| 244 | + return false; |
|
| 245 | + } |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + sql_updateq('spip_rubriques', ['statut' => 'prepa'], 'id_rubrique=' . intval($id_rubrique)); |
|
| 249 | 249 | |
| 250 | 250 | # spip_log("depublier_rubrique $id_pred"); |
| 251 | - return true; |
|
| 251 | + return true; |
|
| 252 | 252 | } |
| 253 | 253 | |
| 254 | 254 | |
@@ -271,18 +271,18 @@ discard block |
||
| 271 | 271 | **/ |
| 272 | 272 | function calculer_rubriques() { |
| 273 | 273 | |
| 274 | - calculer_rubriques_publiees(); |
|
| 274 | + calculer_rubriques_publiees(); |
|
| 275 | 275 | |
| 276 | - // Apres chaque (de)publication |
|
| 277 | - // recalculer les langues utilisees sur le site |
|
| 278 | - $langues = calculer_langues_utilisees(); |
|
| 279 | - ecrire_meta('langues_utilisees', $langues); |
|
| 276 | + // Apres chaque (de)publication |
|
| 277 | + // recalculer les langues utilisees sur le site |
|
| 278 | + $langues = calculer_langues_utilisees(); |
|
| 279 | + ecrire_meta('langues_utilisees', $langues); |
|
| 280 | 280 | |
| 281 | - // Sauver la date de la derniere mise a jour (pour menu_rubriques) |
|
| 282 | - ecrire_meta('date_calcul_rubriques', date('U')); |
|
| 281 | + // Sauver la date de la derniere mise a jour (pour menu_rubriques) |
|
| 282 | + ecrire_meta('date_calcul_rubriques', date('U')); |
|
| 283 | 283 | |
| 284 | - // on calcule la date du prochain article post-date |
|
| 285 | - calculer_prochain_postdate(); |
|
| 284 | + // on calcule la date du prochain article post-date |
|
| 285 | + calculer_prochain_postdate(); |
|
| 286 | 286 | } |
| 287 | 287 | |
| 288 | 288 | |
@@ -299,61 +299,61 @@ discard block |
||
| 299 | 299 | **/ |
| 300 | 300 | function calculer_rubriques_publiees() { |
| 301 | 301 | |
| 302 | - // Mettre les compteurs a zero |
|
| 303 | - sql_updateq('spip_rubriques', ['date_tmp' => '0000-00-00 00:00:00', 'statut_tmp' => 'prepa']); |
|
| 304 | - |
|
| 305 | - // |
|
| 306 | - // Publier et dater les rubriques qui ont un article publie |
|
| 307 | - // |
|
| 308 | - |
|
| 309 | - // Afficher les articles post-dates ? |
|
| 310 | - $postdates = ($GLOBALS['meta']['post_dates'] == 'non') ? |
|
| 311 | - 'AND A.date <= ' . sql_quote(date('Y-m-d H:i:s')) : ''; |
|
| 312 | - |
|
| 313 | - $r = sql_select( |
|
| 314 | - 'R.id_rubrique AS id, max(A.date) AS date_h', |
|
| 315 | - 'spip_rubriques AS R JOIN spip_articles AS A ON R.id_rubrique = A.id_rubrique', |
|
| 316 | - "A.date>R.date_tmp AND A.statut='publie' $postdates ", |
|
| 317 | - 'R.id_rubrique' |
|
| 318 | - ); |
|
| 319 | - while ($row = sql_fetch($r)) { |
|
| 320 | - sql_updateq( |
|
| 321 | - 'spip_rubriques', |
|
| 322 | - ['statut_tmp' => 'publie', 'date_tmp' => $row['date_h']], |
|
| 323 | - 'id_rubrique=' . intval($row['id']) |
|
| 324 | - ); |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - // point d'entree pour permettre a des plugins de gerer le statut |
|
| 328 | - // autrement (par ex: toute rubrique est publiee des sa creation) |
|
| 329 | - // Ce pipeline fait ce qu'il veut, mais s'il touche aux statuts/dates |
|
| 330 | - // c'est statut_tmp/date_tmp qu'il doit modifier |
|
| 331 | - // [C'est un trigger... a renommer en trig_calculer_rubriques ?] |
|
| 332 | - pipeline('calculer_rubriques', null); |
|
| 333 | - |
|
| 334 | - |
|
| 335 | - // Les rubriques qui ont une rubrique fille plus recente |
|
| 336 | - // on tourne tant que les donnees remontent vers la racine. |
|
| 337 | - do { |
|
| 338 | - $continuer = false; |
|
| 339 | - $r = sql_select( |
|
| 340 | - 'R.id_rubrique AS id, max(SR.date_tmp) AS date_h', |
|
| 341 | - 'spip_rubriques AS R JOIN spip_rubriques AS SR ON R.id_rubrique = SR.id_parent', |
|
| 342 | - "(SR.date_tmp>R.date_tmp OR R.statut_tmp<>'publie') AND SR.statut_tmp='publie' ", |
|
| 343 | - 'R.id_rubrique' |
|
| 344 | - ); |
|
| 345 | - while ($row = sql_fetch($r)) { |
|
| 346 | - sql_updateq( |
|
| 347 | - 'spip_rubriques', |
|
| 348 | - ['statut_tmp' => 'publie', 'date_tmp' => $row['date_h']], |
|
| 349 | - 'id_rubrique=' . intval($row['id']) |
|
| 350 | - ); |
|
| 351 | - $continuer = true; |
|
| 352 | - } |
|
| 353 | - } while ($continuer); |
|
| 354 | - |
|
| 355 | - // Enregistrement des modifs |
|
| 356 | - sql_update('spip_rubriques', ['date' => 'date_tmp', 'statut' => 'statut_tmp']); |
|
| 302 | + // Mettre les compteurs a zero |
|
| 303 | + sql_updateq('spip_rubriques', ['date_tmp' => '0000-00-00 00:00:00', 'statut_tmp' => 'prepa']); |
|
| 304 | + |
|
| 305 | + // |
|
| 306 | + // Publier et dater les rubriques qui ont un article publie |
|
| 307 | + // |
|
| 308 | + |
|
| 309 | + // Afficher les articles post-dates ? |
|
| 310 | + $postdates = ($GLOBALS['meta']['post_dates'] == 'non') ? |
|
| 311 | + 'AND A.date <= ' . sql_quote(date('Y-m-d H:i:s')) : ''; |
|
| 312 | + |
|
| 313 | + $r = sql_select( |
|
| 314 | + 'R.id_rubrique AS id, max(A.date) AS date_h', |
|
| 315 | + 'spip_rubriques AS R JOIN spip_articles AS A ON R.id_rubrique = A.id_rubrique', |
|
| 316 | + "A.date>R.date_tmp AND A.statut='publie' $postdates ", |
|
| 317 | + 'R.id_rubrique' |
|
| 318 | + ); |
|
| 319 | + while ($row = sql_fetch($r)) { |
|
| 320 | + sql_updateq( |
|
| 321 | + 'spip_rubriques', |
|
| 322 | + ['statut_tmp' => 'publie', 'date_tmp' => $row['date_h']], |
|
| 323 | + 'id_rubrique=' . intval($row['id']) |
|
| 324 | + ); |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + // point d'entree pour permettre a des plugins de gerer le statut |
|
| 328 | + // autrement (par ex: toute rubrique est publiee des sa creation) |
|
| 329 | + // Ce pipeline fait ce qu'il veut, mais s'il touche aux statuts/dates |
|
| 330 | + // c'est statut_tmp/date_tmp qu'il doit modifier |
|
| 331 | + // [C'est un trigger... a renommer en trig_calculer_rubriques ?] |
|
| 332 | + pipeline('calculer_rubriques', null); |
|
| 333 | + |
|
| 334 | + |
|
| 335 | + // Les rubriques qui ont une rubrique fille plus recente |
|
| 336 | + // on tourne tant que les donnees remontent vers la racine. |
|
| 337 | + do { |
|
| 338 | + $continuer = false; |
|
| 339 | + $r = sql_select( |
|
| 340 | + 'R.id_rubrique AS id, max(SR.date_tmp) AS date_h', |
|
| 341 | + 'spip_rubriques AS R JOIN spip_rubriques AS SR ON R.id_rubrique = SR.id_parent', |
|
| 342 | + "(SR.date_tmp>R.date_tmp OR R.statut_tmp<>'publie') AND SR.statut_tmp='publie' ", |
|
| 343 | + 'R.id_rubrique' |
|
| 344 | + ); |
|
| 345 | + while ($row = sql_fetch($r)) { |
|
| 346 | + sql_updateq( |
|
| 347 | + 'spip_rubriques', |
|
| 348 | + ['statut_tmp' => 'publie', 'date_tmp' => $row['date_h']], |
|
| 349 | + 'id_rubrique=' . intval($row['id']) |
|
| 350 | + ); |
|
| 351 | + $continuer = true; |
|
| 352 | + } |
|
| 353 | + } while ($continuer); |
|
| 354 | + |
|
| 355 | + // Enregistrement des modifs |
|
| 356 | + sql_update('spip_rubriques', ['date' => 'date_tmp', 'statut' => 'statut_tmp']); |
|
| 357 | 357 | } |
| 358 | 358 | |
| 359 | 359 | /** |
@@ -368,123 +368,123 @@ discard block |
||
| 368 | 368 | * @return void |
| 369 | 369 | **/ |
| 370 | 370 | function propager_les_secteurs() { |
| 371 | - // Profondeur 0 |
|
| 372 | - // Toutes les rubriques racines sont de profondeur 0 |
|
| 373 | - // et fixer les id_secteur des rubriques racines |
|
| 374 | - sql_update('spip_rubriques', ['id_secteur' => 'id_rubrique', 'profondeur' => 0], 'id_parent=0'); |
|
| 375 | - // Toute rubrique non racine est de profondeur >0 |
|
| 376 | - sql_updateq('spip_rubriques', ['profondeur' => 1], 'id_parent<>0 AND profondeur=0'); |
|
| 377 | - |
|
| 378 | - // securite : pas plus d'iteration que de rubriques dans la base |
|
| 379 | - $maxiter = sql_countsel('spip_rubriques'); |
|
| 380 | - |
|
| 381 | - // reparer les rubriques qui n'ont pas l'id_secteur de leur parent |
|
| 382 | - // on fait profondeur par profondeur |
|
| 383 | - |
|
| 384 | - $prof = 0; |
|
| 385 | - do { |
|
| 386 | - $continuer = false; |
|
| 387 | - |
|
| 388 | - // Par recursivite : si toutes les rubriques de profondeur $prof sont bonnes |
|
| 389 | - // on fixe le profondeur $prof+1 |
|
| 390 | - |
|
| 391 | - // Toutes les rubriques dont le parent est de profondeur $prof ont une profondeur $prof+1 |
|
| 392 | - // on teste A.profondeur > $prof+1 car : |
|
| 393 | - // - toutes les rubriques de profondeur 0 à $prof sont bonnes |
|
| 394 | - // - si A.profondeur = $prof+1 c'est bon |
|
| 395 | - // - cela nous protege de la boucle infinie en cas de reference circulaire dans les rubriques |
|
| 396 | - $maxiter2 = $maxiter; |
|
| 397 | - while ( |
|
| 398 | - $maxiter2-- |
|
| 399 | - and $rows = sql_allfetsel( |
|
| 400 | - 'A.id_rubrique AS id, R.id_secteur AS id_secteur, R.profondeur+1 as profondeur', |
|
| 401 | - 'spip_rubriques AS A JOIN spip_rubriques AS R ON A.id_parent = R.id_rubrique', |
|
| 402 | - 'R.profondeur=' . intval($prof) . ' AND (A.id_secteur <> R.id_secteur OR A.profondeur > R.profondeur+1)', |
|
| 403 | - '', |
|
| 404 | - 'R.id_secteur', |
|
| 405 | - '0,100' |
|
| 406 | - ) |
|
| 407 | - ) { |
|
| 408 | - $id_secteur = null; |
|
| 409 | - $ids = []; |
|
| 410 | - while ($row = array_shift($rows)) { |
|
| 411 | - if ($row['id_secteur'] !== $id_secteur) { |
|
| 412 | - if (count($ids)) { |
|
| 413 | - sql_updateq( |
|
| 414 | - 'spip_rubriques', |
|
| 415 | - ['id_secteur' => $id_secteur, 'profondeur' => $prof + 1], |
|
| 416 | - sql_in('id_rubrique', $ids) |
|
| 417 | - ); |
|
| 418 | - } |
|
| 419 | - $id_secteur = $row['id_secteur']; |
|
| 420 | - $ids = []; |
|
| 421 | - } |
|
| 422 | - $ids[] = $row['id']; |
|
| 423 | - } |
|
| 424 | - if (count($ids)) { |
|
| 425 | - sql_updateq( |
|
| 426 | - 'spip_rubriques', |
|
| 427 | - ['id_secteur' => $id_secteur, 'profondeur' => $prof + 1], |
|
| 428 | - sql_in('id_rubrique', $ids) |
|
| 429 | - ); |
|
| 430 | - } |
|
| 431 | - } |
|
| 432 | - |
|
| 433 | - |
|
| 434 | - // Toutes les rubriques de profondeur $prof+1 qui n'ont pas un parent de profondeur $prof sont decalees |
|
| 435 | - $maxiter2 = $maxiter; |
|
| 436 | - while ( |
|
| 437 | - $maxiter2-- |
|
| 438 | - and $rows = sql_allfetsel( |
|
| 439 | - 'id_rubrique as id', |
|
| 440 | - 'spip_rubriques', |
|
| 441 | - 'profondeur=' . intval($prof + 1) . ' AND id_parent NOT IN (' . sql_get_select( |
|
| 442 | - 'zzz.id_rubrique', |
|
| 443 | - 'spip_rubriques AS zzz', |
|
| 444 | - 'zzz.profondeur=' . intval($prof) |
|
| 445 | - ) . ')', |
|
| 446 | - '', |
|
| 447 | - '', |
|
| 448 | - '0,100' |
|
| 449 | - ) |
|
| 450 | - ) { |
|
| 451 | - $rows = array_column($rows, 'id'); |
|
| 452 | - sql_updateq('spip_rubriques', ['profondeur' => $prof + 2], sql_in('id_rubrique', $rows)); |
|
| 453 | - } |
|
| 454 | - |
|
| 455 | - // ici on a fini de valider $prof+1, toutes les rubriques de prondeur 0 a $prof+1 sont OK |
|
| 456 | - // si pas de rubrique a profondeur $prof+1 pas la peine de continuer |
|
| 457 | - // si il reste des rubriques non vues, c'est une branche morte ou reference circulaire (base foireuse) |
|
| 458 | - // on arrete les frais |
|
| 459 | - if (sql_countsel('spip_rubriques', 'profondeur=' . intval($prof + 1))) { |
|
| 460 | - $prof++; |
|
| 461 | - $continuer = true; |
|
| 462 | - } |
|
| 463 | - } while ($continuer and $maxiter--); |
|
| 464 | - |
|
| 465 | - // loger si la table des rubriques semble foireuse |
|
| 466 | - // et mettre un id_secteur=0 sur ces rubriques pour eviter toute selection par les boucles |
|
| 467 | - if (sql_countsel('spip_rubriques', 'profondeur>' . intval($prof + 1))) { |
|
| 468 | - spip_log( |
|
| 469 | - 'Les rubriques de profondeur>' . ($prof + 1) . ' semblent suspectes (branches morte ou reference circulaire dans les parents)', |
|
| 470 | - _LOG_CRITIQUE |
|
| 471 | - ); |
|
| 472 | - sql_update('spip_rubriques', ['id_secteur' => 0], 'profondeur>' . intval($prof + 1)); |
|
| 473 | - } |
|
| 474 | - |
|
| 475 | - // reparer les articles |
|
| 476 | - $r = sql_select( |
|
| 477 | - 'A.id_article AS id, R.id_secteur AS secteur', |
|
| 478 | - 'spip_articles AS A, spip_rubriques AS R', |
|
| 479 | - 'A.id_rubrique = R.id_rubrique AND A.id_secteur <> R.id_secteur' |
|
| 480 | - ); |
|
| 481 | - |
|
| 482 | - while ($row = sql_fetch($r)) { |
|
| 483 | - sql_update('spip_articles', ['id_secteur' => $row['secteur']], 'id_article=' . intval($row['id'])); |
|
| 484 | - } |
|
| 485 | - |
|
| 486 | - // avertir les plugins qui peuvent faire leur mises a jour egalement |
|
| 487 | - pipeline('trig_propager_les_secteurs', ''); |
|
| 371 | + // Profondeur 0 |
|
| 372 | + // Toutes les rubriques racines sont de profondeur 0 |
|
| 373 | + // et fixer les id_secteur des rubriques racines |
|
| 374 | + sql_update('spip_rubriques', ['id_secteur' => 'id_rubrique', 'profondeur' => 0], 'id_parent=0'); |
|
| 375 | + // Toute rubrique non racine est de profondeur >0 |
|
| 376 | + sql_updateq('spip_rubriques', ['profondeur' => 1], 'id_parent<>0 AND profondeur=0'); |
|
| 377 | + |
|
| 378 | + // securite : pas plus d'iteration que de rubriques dans la base |
|
| 379 | + $maxiter = sql_countsel('spip_rubriques'); |
|
| 380 | + |
|
| 381 | + // reparer les rubriques qui n'ont pas l'id_secteur de leur parent |
|
| 382 | + // on fait profondeur par profondeur |
|
| 383 | + |
|
| 384 | + $prof = 0; |
|
| 385 | + do { |
|
| 386 | + $continuer = false; |
|
| 387 | + |
|
| 388 | + // Par recursivite : si toutes les rubriques de profondeur $prof sont bonnes |
|
| 389 | + // on fixe le profondeur $prof+1 |
|
| 390 | + |
|
| 391 | + // Toutes les rubriques dont le parent est de profondeur $prof ont une profondeur $prof+1 |
|
| 392 | + // on teste A.profondeur > $prof+1 car : |
|
| 393 | + // - toutes les rubriques de profondeur 0 à $prof sont bonnes |
|
| 394 | + // - si A.profondeur = $prof+1 c'est bon |
|
| 395 | + // - cela nous protege de la boucle infinie en cas de reference circulaire dans les rubriques |
|
| 396 | + $maxiter2 = $maxiter; |
|
| 397 | + while ( |
|
| 398 | + $maxiter2-- |
|
| 399 | + and $rows = sql_allfetsel( |
|
| 400 | + 'A.id_rubrique AS id, R.id_secteur AS id_secteur, R.profondeur+1 as profondeur', |
|
| 401 | + 'spip_rubriques AS A JOIN spip_rubriques AS R ON A.id_parent = R.id_rubrique', |
|
| 402 | + 'R.profondeur=' . intval($prof) . ' AND (A.id_secteur <> R.id_secteur OR A.profondeur > R.profondeur+1)', |
|
| 403 | + '', |
|
| 404 | + 'R.id_secteur', |
|
| 405 | + '0,100' |
|
| 406 | + ) |
|
| 407 | + ) { |
|
| 408 | + $id_secteur = null; |
|
| 409 | + $ids = []; |
|
| 410 | + while ($row = array_shift($rows)) { |
|
| 411 | + if ($row['id_secteur'] !== $id_secteur) { |
|
| 412 | + if (count($ids)) { |
|
| 413 | + sql_updateq( |
|
| 414 | + 'spip_rubriques', |
|
| 415 | + ['id_secteur' => $id_secteur, 'profondeur' => $prof + 1], |
|
| 416 | + sql_in('id_rubrique', $ids) |
|
| 417 | + ); |
|
| 418 | + } |
|
| 419 | + $id_secteur = $row['id_secteur']; |
|
| 420 | + $ids = []; |
|
| 421 | + } |
|
| 422 | + $ids[] = $row['id']; |
|
| 423 | + } |
|
| 424 | + if (count($ids)) { |
|
| 425 | + sql_updateq( |
|
| 426 | + 'spip_rubriques', |
|
| 427 | + ['id_secteur' => $id_secteur, 'profondeur' => $prof + 1], |
|
| 428 | + sql_in('id_rubrique', $ids) |
|
| 429 | + ); |
|
| 430 | + } |
|
| 431 | + } |
|
| 432 | + |
|
| 433 | + |
|
| 434 | + // Toutes les rubriques de profondeur $prof+1 qui n'ont pas un parent de profondeur $prof sont decalees |
|
| 435 | + $maxiter2 = $maxiter; |
|
| 436 | + while ( |
|
| 437 | + $maxiter2-- |
|
| 438 | + and $rows = sql_allfetsel( |
|
| 439 | + 'id_rubrique as id', |
|
| 440 | + 'spip_rubriques', |
|
| 441 | + 'profondeur=' . intval($prof + 1) . ' AND id_parent NOT IN (' . sql_get_select( |
|
| 442 | + 'zzz.id_rubrique', |
|
| 443 | + 'spip_rubriques AS zzz', |
|
| 444 | + 'zzz.profondeur=' . intval($prof) |
|
| 445 | + ) . ')', |
|
| 446 | + '', |
|
| 447 | + '', |
|
| 448 | + '0,100' |
|
| 449 | + ) |
|
| 450 | + ) { |
|
| 451 | + $rows = array_column($rows, 'id'); |
|
| 452 | + sql_updateq('spip_rubriques', ['profondeur' => $prof + 2], sql_in('id_rubrique', $rows)); |
|
| 453 | + } |
|
| 454 | + |
|
| 455 | + // ici on a fini de valider $prof+1, toutes les rubriques de prondeur 0 a $prof+1 sont OK |
|
| 456 | + // si pas de rubrique a profondeur $prof+1 pas la peine de continuer |
|
| 457 | + // si il reste des rubriques non vues, c'est une branche morte ou reference circulaire (base foireuse) |
|
| 458 | + // on arrete les frais |
|
| 459 | + if (sql_countsel('spip_rubriques', 'profondeur=' . intval($prof + 1))) { |
|
| 460 | + $prof++; |
|
| 461 | + $continuer = true; |
|
| 462 | + } |
|
| 463 | + } while ($continuer and $maxiter--); |
|
| 464 | + |
|
| 465 | + // loger si la table des rubriques semble foireuse |
|
| 466 | + // et mettre un id_secteur=0 sur ces rubriques pour eviter toute selection par les boucles |
|
| 467 | + if (sql_countsel('spip_rubriques', 'profondeur>' . intval($prof + 1))) { |
|
| 468 | + spip_log( |
|
| 469 | + 'Les rubriques de profondeur>' . ($prof + 1) . ' semblent suspectes (branches morte ou reference circulaire dans les parents)', |
|
| 470 | + _LOG_CRITIQUE |
|
| 471 | + ); |
|
| 472 | + sql_update('spip_rubriques', ['id_secteur' => 0], 'profondeur>' . intval($prof + 1)); |
|
| 473 | + } |
|
| 474 | + |
|
| 475 | + // reparer les articles |
|
| 476 | + $r = sql_select( |
|
| 477 | + 'A.id_article AS id, R.id_secteur AS secteur', |
|
| 478 | + 'spip_articles AS A, spip_rubriques AS R', |
|
| 479 | + 'A.id_rubrique = R.id_rubrique AND A.id_secteur <> R.id_secteur' |
|
| 480 | + ); |
|
| 481 | + |
|
| 482 | + while ($row = sql_fetch($r)) { |
|
| 483 | + sql_update('spip_articles', ['id_secteur' => $row['secteur']], 'id_article=' . intval($row['id'])); |
|
| 484 | + } |
|
| 485 | + |
|
| 486 | + // avertir les plugins qui peuvent faire leur mises a jour egalement |
|
| 487 | + pipeline('trig_propager_les_secteurs', ''); |
|
| 488 | 488 | } |
| 489 | 489 | |
| 490 | 490 | |
@@ -499,23 +499,23 @@ discard block |
||
| 499 | 499 | * true si un changement a eu lieu |
| 500 | 500 | **/ |
| 501 | 501 | function calculer_langues_rubriques_etape() { |
| 502 | - $s = sql_select( |
|
| 503 | - 'A.id_rubrique AS id_rubrique, R.lang AS lang', |
|
| 504 | - 'spip_rubriques AS A, spip_rubriques AS R', |
|
| 505 | - "A.id_parent = R.id_rubrique AND A.langue_choisie != 'oui' AND R.lang<>'' AND R.lang<>A.lang" |
|
| 506 | - ); |
|
| 507 | - |
|
| 508 | - $t = false; |
|
| 509 | - while ($row = sql_fetch($s)) { |
|
| 510 | - $id_rubrique = $row['id_rubrique']; |
|
| 511 | - $t = sql_updateq( |
|
| 512 | - 'spip_rubriques', |
|
| 513 | - ['lang' => $row['lang'], 'langue_choisie' => 'non'], |
|
| 514 | - 'id_rubrique=' . intval($id_rubrique) |
|
| 515 | - ); |
|
| 516 | - } |
|
| 517 | - |
|
| 518 | - return $t; |
|
| 502 | + $s = sql_select( |
|
| 503 | + 'A.id_rubrique AS id_rubrique, R.lang AS lang', |
|
| 504 | + 'spip_rubriques AS A, spip_rubriques AS R', |
|
| 505 | + "A.id_parent = R.id_rubrique AND A.langue_choisie != 'oui' AND R.lang<>'' AND R.lang<>A.lang" |
|
| 506 | + ); |
|
| 507 | + |
|
| 508 | + $t = false; |
|
| 509 | + while ($row = sql_fetch($s)) { |
|
| 510 | + $id_rubrique = $row['id_rubrique']; |
|
| 511 | + $t = sql_updateq( |
|
| 512 | + 'spip_rubriques', |
|
| 513 | + ['lang' => $row['lang'], 'langue_choisie' => 'non'], |
|
| 514 | + 'id_rubrique=' . intval($id_rubrique) |
|
| 515 | + ); |
|
| 516 | + } |
|
| 517 | + |
|
| 518 | + return $t; |
|
| 519 | 519 | } |
| 520 | 520 | |
| 521 | 521 | /** |
@@ -535,38 +535,38 @@ discard block |
||
| 535 | 535 | **/ |
| 536 | 536 | function calculer_langues_rubriques() { |
| 537 | 537 | |
| 538 | - // rubriques (recursivite) |
|
| 539 | - sql_updateq( |
|
| 540 | - 'spip_rubriques', |
|
| 541 | - ['lang' => $GLOBALS['meta']['langue_site'], 'langue_choisie' => 'non'], |
|
| 542 | - "id_parent=0 AND langue_choisie != 'oui'" |
|
| 543 | - ); |
|
| 544 | - while (calculer_langues_rubriques_etape()) { |
|
| 545 | - ; |
|
| 546 | - } |
|
| 547 | - |
|
| 548 | - // articles |
|
| 549 | - $s = sql_select( |
|
| 550 | - 'A.id_article AS id_article, R.lang AS lang', |
|
| 551 | - 'spip_articles AS A, spip_rubriques AS R', |
|
| 552 | - "A.id_rubrique = R.id_rubrique AND A.langue_choisie != 'oui' AND (length(A.lang)=0 OR length(R.lang)>0) AND R.lang<>A.lang" |
|
| 553 | - ); |
|
| 554 | - while ($row = sql_fetch($s)) { |
|
| 555 | - $id_article = $row['id_article']; |
|
| 556 | - sql_updateq( |
|
| 557 | - 'spip_articles', |
|
| 558 | - ['lang' => $row['lang'], 'langue_choisie' => 'non'], |
|
| 559 | - 'id_article=' . intval($id_article) |
|
| 560 | - ); |
|
| 561 | - } |
|
| 562 | - |
|
| 563 | - if ($GLOBALS['meta']['multi_rubriques'] == 'oui') { |
|
| 564 | - $langues = calculer_langues_utilisees(); |
|
| 565 | - ecrire_meta('langues_utilisees', $langues); |
|
| 566 | - } |
|
| 567 | - |
|
| 568 | - // avertir les plugins qui peuvent faire leur mises a jour egalement |
|
| 569 | - pipeline('trig_calculer_langues_rubriques', ''); |
|
| 538 | + // rubriques (recursivite) |
|
| 539 | + sql_updateq( |
|
| 540 | + 'spip_rubriques', |
|
| 541 | + ['lang' => $GLOBALS['meta']['langue_site'], 'langue_choisie' => 'non'], |
|
| 542 | + "id_parent=0 AND langue_choisie != 'oui'" |
|
| 543 | + ); |
|
| 544 | + while (calculer_langues_rubriques_etape()) { |
|
| 545 | + ; |
|
| 546 | + } |
|
| 547 | + |
|
| 548 | + // articles |
|
| 549 | + $s = sql_select( |
|
| 550 | + 'A.id_article AS id_article, R.lang AS lang', |
|
| 551 | + 'spip_articles AS A, spip_rubriques AS R', |
|
| 552 | + "A.id_rubrique = R.id_rubrique AND A.langue_choisie != 'oui' AND (length(A.lang)=0 OR length(R.lang)>0) AND R.lang<>A.lang" |
|
| 553 | + ); |
|
| 554 | + while ($row = sql_fetch($s)) { |
|
| 555 | + $id_article = $row['id_article']; |
|
| 556 | + sql_updateq( |
|
| 557 | + 'spip_articles', |
|
| 558 | + ['lang' => $row['lang'], 'langue_choisie' => 'non'], |
|
| 559 | + 'id_article=' . intval($id_article) |
|
| 560 | + ); |
|
| 561 | + } |
|
| 562 | + |
|
| 563 | + if ($GLOBALS['meta']['multi_rubriques'] == 'oui') { |
|
| 564 | + $langues = calculer_langues_utilisees(); |
|
| 565 | + ecrire_meta('langues_utilisees', $langues); |
|
| 566 | + } |
|
| 567 | + |
|
| 568 | + // avertir les plugins qui peuvent faire leur mises a jour egalement |
|
| 569 | + pipeline('trig_calculer_langues_rubriques', ''); |
|
| 570 | 570 | } |
| 571 | 571 | |
| 572 | 572 | |
@@ -583,80 +583,80 @@ discard block |
||
| 583 | 583 | * Liste des langues utilisées séparées par des virgules |
| 584 | 584 | **/ |
| 585 | 585 | function calculer_langues_utilisees($serveur = '') { |
| 586 | - include_spip('public/interfaces'); |
|
| 587 | - include_spip('public/compiler'); |
|
| 588 | - include_spip('public/composer'); |
|
| 589 | - include_spip('public/phraser_html'); |
|
| 590 | - $langues = []; |
|
| 591 | - |
|
| 592 | - $langues[$GLOBALS['meta']['langue_site']] = 1; |
|
| 593 | - |
|
| 594 | - include_spip('base/objets'); |
|
| 595 | - $tables = lister_tables_objets_sql(); |
|
| 596 | - $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 597 | - |
|
| 598 | - foreach (array_keys($tables) as $t) { |
|
| 599 | - $desc = $trouver_table($t, $serveur); |
|
| 600 | - // c'est une table avec des langues |
|
| 601 | - if ( |
|
| 602 | - $desc['exist'] |
|
| 603 | - and isset($desc['field']['lang']) |
|
| 604 | - and isset($desc['field']['langue_choisie']) |
|
| 605 | - ) { |
|
| 606 | - $boucle = new Boucle(); |
|
| 607 | - $boucle->show = $desc; |
|
| 608 | - $boucle->nom = 'calculer_langues_utilisees'; |
|
| 609 | - $boucle->id_boucle = $desc['table_objet']; |
|
| 610 | - $boucle->id_table = $desc['table_objet']; |
|
| 611 | - $boucle->primary = $desc['key']['PRIMARY KEY'] ?? ''; |
|
| 612 | - $boucle->sql_serveur = $serveur; |
|
| 613 | - $boucle->select[] = 'DISTINCT lang'; |
|
| 614 | - $boucle->from[$desc['table_objet']] = $t; |
|
| 615 | - $boucle->separateur[] = ','; |
|
| 616 | - $boucle->return = '$Pile[$SP][\'lang\']'; |
|
| 617 | - $boucle->iterateur = 'sql'; |
|
| 618 | - |
|
| 619 | - $boucle->descr['nom'] = 'calculer_langues_utilisees'; // eviter notice php |
|
| 620 | - $boucle->descr['sourcefile'] = 'internal'; |
|
| 621 | - $boucle->descr['gram'] = 'html'; |
|
| 622 | - |
|
| 623 | - $boucle = pipeline('pre_boucle', $boucle); |
|
| 624 | - |
|
| 625 | - if ( |
|
| 626 | - isset($desc['statut']) |
|
| 627 | - and $desc['statut'] |
|
| 628 | - ) { |
|
| 629 | - $boucles = [ |
|
| 630 | - 'calculer_langues_utilisees' => $boucle, |
|
| 631 | - ]; |
|
| 632 | - // generer un nom de fonction "anonyme" unique |
|
| 633 | - do { |
|
| 634 | - $functionname = 'f_calculer_langues_utilisees_' . $boucle->id_table . '_' . time() . '_' . random_int(0, mt_getrandmax()); |
|
| 635 | - } while (function_exists($functionname)); |
|
| 636 | - $code = calculer_boucle('calculer_langues_utilisees', $boucles); |
|
| 637 | - $code = '$SP=0; $command=array();$command["connect"] = $connect = "' . $serveur . '"; $Pile=array(0=>array());' . "\n" . $code; |
|
| 638 | - $code = 'function ' . $functionname . '(){' . $code . '};$res = ' . $functionname . '();'; |
|
| 639 | - $res = ''; |
|
| 640 | - eval($code); |
|
| 641 | - $res = explode(',', $res); |
|
| 642 | - foreach ($res as $lang) { |
|
| 643 | - $langues[$lang] = 1; |
|
| 644 | - } |
|
| 645 | - } else { |
|
| 646 | - $res = sql_select(implode(',', $boucle->select), $boucle->from); |
|
| 647 | - while ($row = sql_fetch($res)) { |
|
| 648 | - $langues[$row['lang']] = 1; |
|
| 649 | - } |
|
| 650 | - } |
|
| 651 | - } |
|
| 652 | - } |
|
| 653 | - |
|
| 654 | - $langues = array_filter(array_keys($langues)); |
|
| 655 | - sort($langues); |
|
| 656 | - $langues = join(',', $langues); |
|
| 657 | - spip_log("langues utilisees: $langues"); |
|
| 658 | - |
|
| 659 | - return $langues; |
|
| 586 | + include_spip('public/interfaces'); |
|
| 587 | + include_spip('public/compiler'); |
|
| 588 | + include_spip('public/composer'); |
|
| 589 | + include_spip('public/phraser_html'); |
|
| 590 | + $langues = []; |
|
| 591 | + |
|
| 592 | + $langues[$GLOBALS['meta']['langue_site']] = 1; |
|
| 593 | + |
|
| 594 | + include_spip('base/objets'); |
|
| 595 | + $tables = lister_tables_objets_sql(); |
|
| 596 | + $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 597 | + |
|
| 598 | + foreach (array_keys($tables) as $t) { |
|
| 599 | + $desc = $trouver_table($t, $serveur); |
|
| 600 | + // c'est une table avec des langues |
|
| 601 | + if ( |
|
| 602 | + $desc['exist'] |
|
| 603 | + and isset($desc['field']['lang']) |
|
| 604 | + and isset($desc['field']['langue_choisie']) |
|
| 605 | + ) { |
|
| 606 | + $boucle = new Boucle(); |
|
| 607 | + $boucle->show = $desc; |
|
| 608 | + $boucle->nom = 'calculer_langues_utilisees'; |
|
| 609 | + $boucle->id_boucle = $desc['table_objet']; |
|
| 610 | + $boucle->id_table = $desc['table_objet']; |
|
| 611 | + $boucle->primary = $desc['key']['PRIMARY KEY'] ?? ''; |
|
| 612 | + $boucle->sql_serveur = $serveur; |
|
| 613 | + $boucle->select[] = 'DISTINCT lang'; |
|
| 614 | + $boucle->from[$desc['table_objet']] = $t; |
|
| 615 | + $boucle->separateur[] = ','; |
|
| 616 | + $boucle->return = '$Pile[$SP][\'lang\']'; |
|
| 617 | + $boucle->iterateur = 'sql'; |
|
| 618 | + |
|
| 619 | + $boucle->descr['nom'] = 'calculer_langues_utilisees'; // eviter notice php |
|
| 620 | + $boucle->descr['sourcefile'] = 'internal'; |
|
| 621 | + $boucle->descr['gram'] = 'html'; |
|
| 622 | + |
|
| 623 | + $boucle = pipeline('pre_boucle', $boucle); |
|
| 624 | + |
|
| 625 | + if ( |
|
| 626 | + isset($desc['statut']) |
|
| 627 | + and $desc['statut'] |
|
| 628 | + ) { |
|
| 629 | + $boucles = [ |
|
| 630 | + 'calculer_langues_utilisees' => $boucle, |
|
| 631 | + ]; |
|
| 632 | + // generer un nom de fonction "anonyme" unique |
|
| 633 | + do { |
|
| 634 | + $functionname = 'f_calculer_langues_utilisees_' . $boucle->id_table . '_' . time() . '_' . random_int(0, mt_getrandmax()); |
|
| 635 | + } while (function_exists($functionname)); |
|
| 636 | + $code = calculer_boucle('calculer_langues_utilisees', $boucles); |
|
| 637 | + $code = '$SP=0; $command=array();$command["connect"] = $connect = "' . $serveur . '"; $Pile=array(0=>array());' . "\n" . $code; |
|
| 638 | + $code = 'function ' . $functionname . '(){' . $code . '};$res = ' . $functionname . '();'; |
|
| 639 | + $res = ''; |
|
| 640 | + eval($code); |
|
| 641 | + $res = explode(',', $res); |
|
| 642 | + foreach ($res as $lang) { |
|
| 643 | + $langues[$lang] = 1; |
|
| 644 | + } |
|
| 645 | + } else { |
|
| 646 | + $res = sql_select(implode(',', $boucle->select), $boucle->from); |
|
| 647 | + while ($row = sql_fetch($res)) { |
|
| 648 | + $langues[$row['lang']] = 1; |
|
| 649 | + } |
|
| 650 | + } |
|
| 651 | + } |
|
| 652 | + } |
|
| 653 | + |
|
| 654 | + $langues = array_filter(array_keys($langues)); |
|
| 655 | + sort($langues); |
|
| 656 | + $langues = join(',', $langues); |
|
| 657 | + spip_log("langues utilisees: $langues"); |
|
| 658 | + |
|
| 659 | + return $langues; |
|
| 660 | 660 | } |
| 661 | 661 | |
| 662 | 662 | /** |
@@ -673,9 +673,9 @@ discard block |
||
| 673 | 673 | * incluant les rubriques noeuds et toutes leurs descendances |
| 674 | 674 | */ |
| 675 | 675 | function calcul_branche_in($id) { |
| 676 | - $calcul_branche_in = charger_fonction('calcul_branche_in', 'inc'); |
|
| 676 | + $calcul_branche_in = charger_fonction('calcul_branche_in', 'inc'); |
|
| 677 | 677 | |
| 678 | - return $calcul_branche_in($id); |
|
| 678 | + return $calcul_branche_in($id); |
|
| 679 | 679 | } |
| 680 | 680 | |
| 681 | 681 | /** |
@@ -693,9 +693,9 @@ discard block |
||
| 693 | 693 | * incluant les rubriques transmises et toutes leurs parentées |
| 694 | 694 | */ |
| 695 | 695 | function calcul_hierarchie_in($id, $tout = true) { |
| 696 | - $calcul_hierarchie_in = charger_fonction('calcul_hierarchie_in', 'inc'); |
|
| 696 | + $calcul_hierarchie_in = charger_fonction('calcul_hierarchie_in', 'inc'); |
|
| 697 | 697 | |
| 698 | - return $calcul_hierarchie_in($id, $tout); |
|
| 698 | + return $calcul_hierarchie_in($id, $tout); |
|
| 699 | 699 | } |
| 700 | 700 | |
| 701 | 701 | |
@@ -716,40 +716,40 @@ discard block |
||
| 716 | 716 | * incluant les rubriques noeuds et toutes leurs descendances |
| 717 | 717 | */ |
| 718 | 718 | function inc_calcul_branche_in_dist($id) { |
| 719 | - static $b = []; |
|
| 720 | - |
|
| 721 | - // normaliser $id qui a pu arriver comme un array, comme un entier, ou comme une chaine NN,NN,NN |
|
| 722 | - if (!is_array($id)) { |
|
| 723 | - $id = explode(',', $id); |
|
| 724 | - } |
|
| 725 | - $id = join(',', array_map('intval', $id)); |
|
| 726 | - if (isset($b[$id])) { |
|
| 727 | - return $b[$id]; |
|
| 728 | - } |
|
| 729 | - |
|
| 730 | - // Notre branche commence par la rubrique de depart |
|
| 731 | - $branche = $r = $id; |
|
| 732 | - |
|
| 733 | - // On ajoute une generation (les filles de la generation precedente) |
|
| 734 | - // jusqu'a epuisement, en se protegeant des references circulaires |
|
| 735 | - $maxiter = 10000; |
|
| 736 | - while ( |
|
| 737 | - $maxiter-- and $filles = sql_allfetsel( |
|
| 738 | - 'id_rubrique', |
|
| 739 | - 'spip_rubriques', |
|
| 740 | - sql_in('id_parent', $r) . ' AND ' . sql_in('id_rubrique', $r, 'NOT') |
|
| 741 | - ) |
|
| 742 | - ) { |
|
| 743 | - $r = join(',', array_column($filles, 'id_rubrique')); |
|
| 744 | - $branche .= ',' . $r; |
|
| 745 | - } |
|
| 746 | - |
|
| 747 | - # securite pour ne pas plomber la conso memoire sur les sites prolifiques |
|
| 748 | - if (strlen($branche) < 10000) { |
|
| 749 | - $b[$id] = $branche; |
|
| 750 | - } |
|
| 751 | - |
|
| 752 | - return $branche; |
|
| 719 | + static $b = []; |
|
| 720 | + |
|
| 721 | + // normaliser $id qui a pu arriver comme un array, comme un entier, ou comme une chaine NN,NN,NN |
|
| 722 | + if (!is_array($id)) { |
|
| 723 | + $id = explode(',', $id); |
|
| 724 | + } |
|
| 725 | + $id = join(',', array_map('intval', $id)); |
|
| 726 | + if (isset($b[$id])) { |
|
| 727 | + return $b[$id]; |
|
| 728 | + } |
|
| 729 | + |
|
| 730 | + // Notre branche commence par la rubrique de depart |
|
| 731 | + $branche = $r = $id; |
|
| 732 | + |
|
| 733 | + // On ajoute une generation (les filles de la generation precedente) |
|
| 734 | + // jusqu'a epuisement, en se protegeant des references circulaires |
|
| 735 | + $maxiter = 10000; |
|
| 736 | + while ( |
|
| 737 | + $maxiter-- and $filles = sql_allfetsel( |
|
| 738 | + 'id_rubrique', |
|
| 739 | + 'spip_rubriques', |
|
| 740 | + sql_in('id_parent', $r) . ' AND ' . sql_in('id_rubrique', $r, 'NOT') |
|
| 741 | + ) |
|
| 742 | + ) { |
|
| 743 | + $r = join(',', array_column($filles, 'id_rubrique')); |
|
| 744 | + $branche .= ',' . $r; |
|
| 745 | + } |
|
| 746 | + |
|
| 747 | + # securite pour ne pas plomber la conso memoire sur les sites prolifiques |
|
| 748 | + if (strlen($branche) < 10000) { |
|
| 749 | + $b[$id] = $branche; |
|
| 750 | + } |
|
| 751 | + |
|
| 752 | + return $branche; |
|
| 753 | 753 | } |
| 754 | 754 | |
| 755 | 755 | |
@@ -771,45 +771,45 @@ discard block |
||
| 771 | 771 | * incluant les rubriques transmises et toutes leurs parentées |
| 772 | 772 | */ |
| 773 | 773 | function inc_calcul_hierarchie_in_dist($id, $tout = true) { |
| 774 | - static $b = []; |
|
| 775 | - |
|
| 776 | - // normaliser $id qui a pu arriver comme un array, comme un entier, ou comme une chaine NN,NN,NN |
|
| 777 | - if (!is_array($id)) { |
|
| 778 | - $id = explode(',', $id); |
|
| 779 | - } |
|
| 780 | - $id = join(',', array_map('intval', $id)); |
|
| 781 | - |
|
| 782 | - if (isset($b[$id])) { |
|
| 783 | - // Notre branche commence par la rubrique de depart si $tout=true |
|
| 784 | - return $tout ? (strlen($b[$id]) ? $b[$id] . ",$id" : $id) : $b[$id]; |
|
| 785 | - } |
|
| 786 | - |
|
| 787 | - $hier = ''; |
|
| 788 | - |
|
| 789 | - // On ajoute une generation (les filles de la generation precedente) |
|
| 790 | - // jusqu'a epuisement, en se protegeant des references circulaires |
|
| 791 | - $ids_nouveaux_parents = $id; |
|
| 792 | - $maxiter = 10000; |
|
| 793 | - while ( |
|
| 794 | - $maxiter-- and $parents = sql_allfetsel( |
|
| 795 | - 'id_parent', |
|
| 796 | - 'spip_rubriques', |
|
| 797 | - sql_in('id_rubrique', $ids_nouveaux_parents) . ' AND ' . sql_in('id_parent', $hier, 'NOT') |
|
| 798 | - ) |
|
| 799 | - ) { |
|
| 800 | - $ids_nouveaux_parents = join(',', array_column($parents, 'id_parent')); |
|
| 801 | - $hier = $ids_nouveaux_parents . (strlen($hier) ? ',' . $hier : ''); |
|
| 802 | - } |
|
| 803 | - |
|
| 804 | - # securite pour ne pas plomber la conso memoire sur les sites prolifiques |
|
| 805 | - if (strlen($hier) < 10000) { |
|
| 806 | - $b[$id] = $hier; |
|
| 807 | - } |
|
| 808 | - |
|
| 809 | - // Notre branche commence par la rubrique de depart si $tout=true |
|
| 810 | - $hier = $tout ? (strlen($hier) ? "$hier,$id" : $id) : $hier; |
|
| 811 | - |
|
| 812 | - return $hier; |
|
| 774 | + static $b = []; |
|
| 775 | + |
|
| 776 | + // normaliser $id qui a pu arriver comme un array, comme un entier, ou comme une chaine NN,NN,NN |
|
| 777 | + if (!is_array($id)) { |
|
| 778 | + $id = explode(',', $id); |
|
| 779 | + } |
|
| 780 | + $id = join(',', array_map('intval', $id)); |
|
| 781 | + |
|
| 782 | + if (isset($b[$id])) { |
|
| 783 | + // Notre branche commence par la rubrique de depart si $tout=true |
|
| 784 | + return $tout ? (strlen($b[$id]) ? $b[$id] . ",$id" : $id) : $b[$id]; |
|
| 785 | + } |
|
| 786 | + |
|
| 787 | + $hier = ''; |
|
| 788 | + |
|
| 789 | + // On ajoute une generation (les filles de la generation precedente) |
|
| 790 | + // jusqu'a epuisement, en se protegeant des references circulaires |
|
| 791 | + $ids_nouveaux_parents = $id; |
|
| 792 | + $maxiter = 10000; |
|
| 793 | + while ( |
|
| 794 | + $maxiter-- and $parents = sql_allfetsel( |
|
| 795 | + 'id_parent', |
|
| 796 | + 'spip_rubriques', |
|
| 797 | + sql_in('id_rubrique', $ids_nouveaux_parents) . ' AND ' . sql_in('id_parent', $hier, 'NOT') |
|
| 798 | + ) |
|
| 799 | + ) { |
|
| 800 | + $ids_nouveaux_parents = join(',', array_column($parents, 'id_parent')); |
|
| 801 | + $hier = $ids_nouveaux_parents . (strlen($hier) ? ',' . $hier : ''); |
|
| 802 | + } |
|
| 803 | + |
|
| 804 | + # securite pour ne pas plomber la conso memoire sur les sites prolifiques |
|
| 805 | + if (strlen($hier) < 10000) { |
|
| 806 | + $b[$id] = $hier; |
|
| 807 | + } |
|
| 808 | + |
|
| 809 | + // Notre branche commence par la rubrique de depart si $tout=true |
|
| 810 | + $hier = $tout ? (strlen($hier) ? "$hier,$id" : $id) : $hier; |
|
| 811 | + |
|
| 812 | + return $hier; |
|
| 813 | 813 | } |
| 814 | 814 | |
| 815 | 815 | |
@@ -827,47 +827,47 @@ discard block |
||
| 827 | 827 | * @return void |
| 828 | 828 | **/ |
| 829 | 829 | function calculer_prochain_postdate($check = false) { |
| 830 | - include_spip('base/abstract_sql'); |
|
| 831 | - if ($check) { |
|
| 832 | - $postdates = ($GLOBALS['meta']['post_dates'] == 'non') ? |
|
| 833 | - 'AND A.date <= ' . sql_quote(date('Y-m-d H:i:s')) : ''; |
|
| 834 | - |
|
| 835 | - $r = sql_select( |
|
| 836 | - 'DISTINCT A.id_rubrique AS id', |
|
| 837 | - 'spip_articles AS A LEFT JOIN spip_rubriques AS R ON A.id_rubrique=R.id_rubrique', |
|
| 838 | - "R.statut != 'publie' AND A.statut='publie'$postdates" |
|
| 839 | - ); |
|
| 840 | - while ($row = sql_fetch($r)) { |
|
| 841 | - publier_branche_rubrique($row['id']); |
|
| 842 | - } |
|
| 843 | - |
|
| 844 | - pipeline('trig_calculer_prochain_postdate', ''); |
|
| 845 | - } |
|
| 846 | - |
|
| 847 | - $t = sql_fetsel( |
|
| 848 | - 'date', |
|
| 849 | - 'spip_articles', |
|
| 850 | - "statut='publie' AND date > " . sql_quote(date('Y-m-d H:i:s')), |
|
| 851 | - '', |
|
| 852 | - 'date', |
|
| 853 | - '1' |
|
| 854 | - ); |
|
| 855 | - |
|
| 856 | - if ($t) { |
|
| 857 | - $t = $t['date']; |
|
| 858 | - if ( |
|
| 859 | - !isset($GLOBALS['meta']['date_prochain_postdate']) |
|
| 860 | - or $t <> $GLOBALS['meta']['date_prochain_postdate'] |
|
| 861 | - ) { |
|
| 862 | - ecrire_meta('date_prochain_postdate', strtotime($t)); |
|
| 863 | - ecrire_meta('derniere_modif', time()); |
|
| 864 | - } |
|
| 865 | - } else { |
|
| 866 | - effacer_meta('date_prochain_postdate'); |
|
| 867 | - ecrire_meta('derniere_modif', time()); |
|
| 868 | - } |
|
| 869 | - |
|
| 870 | - spip_log("prochain postdate: $t"); |
|
| 830 | + include_spip('base/abstract_sql'); |
|
| 831 | + if ($check) { |
|
| 832 | + $postdates = ($GLOBALS['meta']['post_dates'] == 'non') ? |
|
| 833 | + 'AND A.date <= ' . sql_quote(date('Y-m-d H:i:s')) : ''; |
|
| 834 | + |
|
| 835 | + $r = sql_select( |
|
| 836 | + 'DISTINCT A.id_rubrique AS id', |
|
| 837 | + 'spip_articles AS A LEFT JOIN spip_rubriques AS R ON A.id_rubrique=R.id_rubrique', |
|
| 838 | + "R.statut != 'publie' AND A.statut='publie'$postdates" |
|
| 839 | + ); |
|
| 840 | + while ($row = sql_fetch($r)) { |
|
| 841 | + publier_branche_rubrique($row['id']); |
|
| 842 | + } |
|
| 843 | + |
|
| 844 | + pipeline('trig_calculer_prochain_postdate', ''); |
|
| 845 | + } |
|
| 846 | + |
|
| 847 | + $t = sql_fetsel( |
|
| 848 | + 'date', |
|
| 849 | + 'spip_articles', |
|
| 850 | + "statut='publie' AND date > " . sql_quote(date('Y-m-d H:i:s')), |
|
| 851 | + '', |
|
| 852 | + 'date', |
|
| 853 | + '1' |
|
| 854 | + ); |
|
| 855 | + |
|
| 856 | + if ($t) { |
|
| 857 | + $t = $t['date']; |
|
| 858 | + if ( |
|
| 859 | + !isset($GLOBALS['meta']['date_prochain_postdate']) |
|
| 860 | + or $t <> $GLOBALS['meta']['date_prochain_postdate'] |
|
| 861 | + ) { |
|
| 862 | + ecrire_meta('date_prochain_postdate', strtotime($t)); |
|
| 863 | + ecrire_meta('derniere_modif', time()); |
|
| 864 | + } |
|
| 865 | + } else { |
|
| 866 | + effacer_meta('date_prochain_postdate'); |
|
| 867 | + ecrire_meta('derniere_modif', time()); |
|
| 868 | + } |
|
| 869 | + |
|
| 870 | + spip_log("prochain postdate: $t"); |
|
| 871 | 871 | } |
| 872 | 872 | |
| 873 | 873 | /** |
@@ -892,62 +892,62 @@ discard block |
||
| 892 | 892 | */ |
| 893 | 893 | function creer_rubrique_nommee($titre, $id_parent = 0, $serveur = '') { |
| 894 | 894 | |
| 895 | - // eclater l'arborescence demandee |
|
| 896 | - // echapper les </multi> et autres balises fermantes html |
|
| 897 | - $titre = preg_replace(',</([a-z][^>]*)>,ims', "<@\\1>", $titre); |
|
| 898 | - $arbo = explode('/', preg_replace(',^/,', '', $titre)); |
|
| 899 | - include_spip('base/abstract_sql'); |
|
| 900 | - foreach ($arbo as $titre) { |
|
| 901 | - // retablir les </multi> et autres balises fermantes html |
|
| 902 | - $titre = preg_replace(',<@([a-z][^>]*)>,ims', "</\\1>", $titre); |
|
| 903 | - $r = sql_getfetsel( |
|
| 904 | - 'id_rubrique', |
|
| 905 | - 'spip_rubriques', |
|
| 906 | - 'titre = ' . sql_quote($titre) . ' AND id_parent=' . intval($id_parent), |
|
| 907 | - $groupby = [], |
|
| 908 | - $orderby = [], |
|
| 909 | - $limit = '', |
|
| 910 | - $having = [], |
|
| 911 | - $serveur |
|
| 912 | - ); |
|
| 913 | - if ($r !== null) { |
|
| 914 | - $id_parent = $r; |
|
| 915 | - } else { |
|
| 916 | - $id_rubrique = sql_insertq('spip_rubriques', [ |
|
| 917 | - 'titre' => $titre, |
|
| 918 | - 'id_parent' => $id_parent, |
|
| 919 | - 'statut' => 'prepa' |
|
| 920 | - ], $desc = [], $serveur); |
|
| 921 | - if ($id_parent > 0) { |
|
| 922 | - $data = sql_fetsel( |
|
| 923 | - 'id_secteur,lang', |
|
| 924 | - 'spip_rubriques', |
|
| 925 | - "id_rubrique=$id_parent", |
|
| 926 | - $groupby = [], |
|
| 927 | - $orderby = [], |
|
| 928 | - $limit = '', |
|
| 929 | - $having = [], |
|
| 930 | - $serveur |
|
| 931 | - ); |
|
| 932 | - $id_secteur = $data['id_secteur']; |
|
| 933 | - $lang = $data['lang']; |
|
| 934 | - } else { |
|
| 935 | - $id_secteur = $id_rubrique; |
|
| 936 | - $lang = $GLOBALS['meta']['langue_site']; |
|
| 937 | - } |
|
| 938 | - |
|
| 939 | - sql_updateq( |
|
| 940 | - 'spip_rubriques', |
|
| 941 | - ['id_secteur' => $id_secteur, 'lang' => $lang], |
|
| 942 | - 'id_rubrique=' . intval($id_rubrique), |
|
| 943 | - [], |
|
| 944 | - $serveur |
|
| 945 | - ); |
|
| 946 | - |
|
| 947 | - // pour la recursion |
|
| 948 | - $id_parent = $id_rubrique; |
|
| 949 | - } |
|
| 950 | - } |
|
| 951 | - |
|
| 952 | - return intval($id_parent); |
|
| 895 | + // eclater l'arborescence demandee |
|
| 896 | + // echapper les </multi> et autres balises fermantes html |
|
| 897 | + $titre = preg_replace(',</([a-z][^>]*)>,ims', "<@\\1>", $titre); |
|
| 898 | + $arbo = explode('/', preg_replace(',^/,', '', $titre)); |
|
| 899 | + include_spip('base/abstract_sql'); |
|
| 900 | + foreach ($arbo as $titre) { |
|
| 901 | + // retablir les </multi> et autres balises fermantes html |
|
| 902 | + $titre = preg_replace(',<@([a-z][^>]*)>,ims', "</\\1>", $titre); |
|
| 903 | + $r = sql_getfetsel( |
|
| 904 | + 'id_rubrique', |
|
| 905 | + 'spip_rubriques', |
|
| 906 | + 'titre = ' . sql_quote($titre) . ' AND id_parent=' . intval($id_parent), |
|
| 907 | + $groupby = [], |
|
| 908 | + $orderby = [], |
|
| 909 | + $limit = '', |
|
| 910 | + $having = [], |
|
| 911 | + $serveur |
|
| 912 | + ); |
|
| 913 | + if ($r !== null) { |
|
| 914 | + $id_parent = $r; |
|
| 915 | + } else { |
|
| 916 | + $id_rubrique = sql_insertq('spip_rubriques', [ |
|
| 917 | + 'titre' => $titre, |
|
| 918 | + 'id_parent' => $id_parent, |
|
| 919 | + 'statut' => 'prepa' |
|
| 920 | + ], $desc = [], $serveur); |
|
| 921 | + if ($id_parent > 0) { |
|
| 922 | + $data = sql_fetsel( |
|
| 923 | + 'id_secteur,lang', |
|
| 924 | + 'spip_rubriques', |
|
| 925 | + "id_rubrique=$id_parent", |
|
| 926 | + $groupby = [], |
|
| 927 | + $orderby = [], |
|
| 928 | + $limit = '', |
|
| 929 | + $having = [], |
|
| 930 | + $serveur |
|
| 931 | + ); |
|
| 932 | + $id_secteur = $data['id_secteur']; |
|
| 933 | + $lang = $data['lang']; |
|
| 934 | + } else { |
|
| 935 | + $id_secteur = $id_rubrique; |
|
| 936 | + $lang = $GLOBALS['meta']['langue_site']; |
|
| 937 | + } |
|
| 938 | + |
|
| 939 | + sql_updateq( |
|
| 940 | + 'spip_rubriques', |
|
| 941 | + ['id_secteur' => $id_secteur, 'lang' => $lang], |
|
| 942 | + 'id_rubrique=' . intval($id_rubrique), |
|
| 943 | + [], |
|
| 944 | + $serveur |
|
| 945 | + ); |
|
| 946 | + |
|
| 947 | + // pour la recursion |
|
| 948 | + $id_parent = $id_rubrique; |
|
| 949 | + } |
|
| 950 | + } |
|
| 951 | + |
|
| 952 | + return intval($id_parent); |
|
| 953 | 953 | } |
@@ -18,32 +18,32 @@ discard block |
||
| 18 | 18 | * @package SPIP\Core\Distant |
| 19 | 19 | **/ |
| 20 | 20 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 21 | - return; |
|
| 21 | + return; |
|
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | if (!defined('_INC_DISTANT_VERSION_HTTP')) { |
| 25 | - define('_INC_DISTANT_VERSION_HTTP', 'HTTP/1.0'); |
|
| 25 | + define('_INC_DISTANT_VERSION_HTTP', 'HTTP/1.0'); |
|
| 26 | 26 | } |
| 27 | 27 | if (!defined('_INC_DISTANT_CONTENT_ENCODING')) { |
| 28 | - define('_INC_DISTANT_CONTENT_ENCODING', 'gzip'); |
|
| 28 | + define('_INC_DISTANT_CONTENT_ENCODING', 'gzip'); |
|
| 29 | 29 | } |
| 30 | 30 | if (!defined('_INC_DISTANT_USER_AGENT')) { |
| 31 | - define('_INC_DISTANT_USER_AGENT', 'SPIP-' . $GLOBALS['spip_version_affichee'] . ' (' . $GLOBALS['home_server'] . ')'); |
|
| 31 | + define('_INC_DISTANT_USER_AGENT', 'SPIP-' . $GLOBALS['spip_version_affichee'] . ' (' . $GLOBALS['home_server'] . ')'); |
|
| 32 | 32 | } |
| 33 | 33 | if (!defined('_INC_DISTANT_MAX_SIZE')) { |
| 34 | - define('_INC_DISTANT_MAX_SIZE', 2_097_152); |
|
| 34 | + define('_INC_DISTANT_MAX_SIZE', 2_097_152); |
|
| 35 | 35 | } |
| 36 | 36 | if (!defined('_INC_DISTANT_CONNECT_TIMEOUT')) { |
| 37 | - define('_INC_DISTANT_CONNECT_TIMEOUT', 10); |
|
| 37 | + define('_INC_DISTANT_CONNECT_TIMEOUT', 10); |
|
| 38 | 38 | } |
| 39 | 39 | |
| 40 | 40 | define('_REGEXP_COPIE_LOCALE', ',' . |
| 41 | - preg_replace( |
|
| 42 | - '@^https?:@', |
|
| 43 | - 'https?:', |
|
| 44 | - ($GLOBALS['meta']['adresse_site'] ?? '') |
|
| 45 | - ) |
|
| 46 | - . '/?spip.php[?]action=acceder_document.*file=(.*)$,'); |
|
| 41 | + preg_replace( |
|
| 42 | + '@^https?:@', |
|
| 43 | + 'https?:', |
|
| 44 | + ($GLOBALS['meta']['adresse_site'] ?? '') |
|
| 45 | + ) |
|
| 46 | + . '/?spip.php[?]action=acceder_document.*file=(.*)$,'); |
|
| 47 | 47 | |
| 48 | 48 | //@define('_COPIE_LOCALE_MAX_SIZE',2097152); // poids (inc/utils l'a fait) |
| 49 | 49 | |
@@ -72,107 +72,107 @@ discard block |
||
| 72 | 72 | */ |
| 73 | 73 | function copie_locale($source, $mode = 'auto', $local = null, $taille_max = null, $callback_valider_url = null) { |
| 74 | 74 | |
| 75 | - // si c'est la protection de soi-meme, retourner le path |
|
| 76 | - if ($mode !== 'force' and preg_match(_REGEXP_COPIE_LOCALE, $source, $match)) { |
|
| 77 | - $source = substr(_DIR_IMG, strlen(_DIR_RACINE)) . urldecode($match[1]); |
|
| 78 | - |
|
| 79 | - return @file_exists($source) ? $source : false; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - if (is_null($local)) { |
|
| 83 | - $local = fichier_copie_locale($source); |
|
| 84 | - } else { |
|
| 85 | - if (_DIR_RACINE and strncmp(_DIR_RACINE, $local, strlen(_DIR_RACINE)) == 0) { |
|
| 86 | - $local = substr($local, strlen(_DIR_RACINE)); |
|
| 87 | - } |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - // si $local = '' c'est un fichier refuse par fichier_copie_locale(), |
|
| 91 | - // par exemple un fichier qui ne figure pas dans nos documents ; |
|
| 92 | - // dans ce cas on n'essaie pas de le telecharger pour ensuite echouer |
|
| 93 | - if (!$local) { |
|
| 94 | - return false; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - $localrac = _DIR_RACINE . $local; |
|
| 98 | - $t = ($mode === 'force') ? false : @file_exists($localrac); |
|
| 99 | - |
|
| 100 | - // test d'existence du fichier |
|
| 101 | - if ($mode === 'test') { |
|
| 102 | - return $t ? $local : ''; |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - // sinon voir si on doit/peut le telecharger |
|
| 106 | - if ($local === $source or !tester_url_absolue($source)) { |
|
| 107 | - return $t ? $local : ''; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - if ($mode === 'modif' or !$t) { |
|
| 111 | - // passer par un fichier temporaire unique pour gerer les echecs en cours de recuperation |
|
| 112 | - // et des eventuelles recuperations concurantes |
|
| 113 | - include_spip('inc/acces'); |
|
| 114 | - if (!$taille_max) { |
|
| 115 | - $taille_max = _COPIE_LOCALE_MAX_SIZE; |
|
| 116 | - } |
|
| 117 | - $localrac_tmp = $localrac . '.tmp'; |
|
| 118 | - $res = recuperer_url( |
|
| 119 | - $source, |
|
| 120 | - ['file' => $localrac_tmp, 'taille_max' => $taille_max, 'if_modified_since' => $t ? filemtime($localrac) : ''] |
|
| 121 | - ); |
|
| 122 | - |
|
| 123 | - if (!$res or (!$res['length'] and $res['status'] != 304)) { |
|
| 124 | - spip_log("copie_locale : Echec recuperation $source sur $localrac_tmp status : " . ($res ? $res['status'] : '-'), 'distant' . _LOG_INFO_IMPORTANTE); |
|
| 125 | - @unlink($localrac_tmp); |
|
| 126 | - } |
|
| 127 | - else { |
|
| 128 | - spip_log("copie_locale : recuperation $source sur $localrac_tmp OK | taille " . $res['length'] . ' status ' . $res['status'], 'distant'); |
|
| 129 | - } |
|
| 130 | - if (!$res or !$res['length']) { |
|
| 131 | - // si $t c'est sans doute juste un not-modified-since |
|
| 132 | - return $t ? $local : false; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - // si option valider url, verifions que l'URL finale est acceptable |
|
| 136 | - if ( |
|
| 137 | - $callback_valider_url |
|
| 138 | - and is_callable($callback_valider_url) |
|
| 139 | - and !$callback_valider_url($res['url']) |
|
| 140 | - ) { |
|
| 141 | - spip_log('copie_locale : url finale ' . $res['url'] . " non valide, on refuse le fichier $localrac_tmp", 'distant' . _LOG_INFO_IMPORTANTE); |
|
| 142 | - @unlink($localrac_tmp); |
|
| 143 | - return $t ? $local : false; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - // on peut renommer le fichier tmp |
|
| 147 | - @rename($localrac_tmp, $localrac); |
|
| 148 | - |
|
| 149 | - // si on retrouve l'extension |
|
| 150 | - if ( |
|
| 151 | - !empty($res['headers']) |
|
| 152 | - and $extension = distant_trouver_extension_selon_headers($source, $res['headers']) |
|
| 153 | - ) { |
|
| 154 | - if ($sanitizer = charger_fonction($extension, 'sanitizer', true)) { |
|
| 155 | - $sanitizer($localrac); |
|
| 156 | - } |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - // pour une eventuelle indexation |
|
| 160 | - pipeline( |
|
| 161 | - 'post_edition', |
|
| 162 | - [ |
|
| 163 | - 'args' => [ |
|
| 164 | - 'operation' => 'copie_locale', |
|
| 165 | - 'source' => $source, |
|
| 166 | - 'fichier' => $local, |
|
| 167 | - 'http_res' => $res['length'], |
|
| 168 | - 'url' => $res['url'], |
|
| 169 | - ], |
|
| 170 | - 'data' => null |
|
| 171 | - ] |
|
| 172 | - ); |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - return $local; |
|
| 75 | + // si c'est la protection de soi-meme, retourner le path |
|
| 76 | + if ($mode !== 'force' and preg_match(_REGEXP_COPIE_LOCALE, $source, $match)) { |
|
| 77 | + $source = substr(_DIR_IMG, strlen(_DIR_RACINE)) . urldecode($match[1]); |
|
| 78 | + |
|
| 79 | + return @file_exists($source) ? $source : false; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + if (is_null($local)) { |
|
| 83 | + $local = fichier_copie_locale($source); |
|
| 84 | + } else { |
|
| 85 | + if (_DIR_RACINE and strncmp(_DIR_RACINE, $local, strlen(_DIR_RACINE)) == 0) { |
|
| 86 | + $local = substr($local, strlen(_DIR_RACINE)); |
|
| 87 | + } |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + // si $local = '' c'est un fichier refuse par fichier_copie_locale(), |
|
| 91 | + // par exemple un fichier qui ne figure pas dans nos documents ; |
|
| 92 | + // dans ce cas on n'essaie pas de le telecharger pour ensuite echouer |
|
| 93 | + if (!$local) { |
|
| 94 | + return false; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + $localrac = _DIR_RACINE . $local; |
|
| 98 | + $t = ($mode === 'force') ? false : @file_exists($localrac); |
|
| 99 | + |
|
| 100 | + // test d'existence du fichier |
|
| 101 | + if ($mode === 'test') { |
|
| 102 | + return $t ? $local : ''; |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + // sinon voir si on doit/peut le telecharger |
|
| 106 | + if ($local === $source or !tester_url_absolue($source)) { |
|
| 107 | + return $t ? $local : ''; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + if ($mode === 'modif' or !$t) { |
|
| 111 | + // passer par un fichier temporaire unique pour gerer les echecs en cours de recuperation |
|
| 112 | + // et des eventuelles recuperations concurantes |
|
| 113 | + include_spip('inc/acces'); |
|
| 114 | + if (!$taille_max) { |
|
| 115 | + $taille_max = _COPIE_LOCALE_MAX_SIZE; |
|
| 116 | + } |
|
| 117 | + $localrac_tmp = $localrac . '.tmp'; |
|
| 118 | + $res = recuperer_url( |
|
| 119 | + $source, |
|
| 120 | + ['file' => $localrac_tmp, 'taille_max' => $taille_max, 'if_modified_since' => $t ? filemtime($localrac) : ''] |
|
| 121 | + ); |
|
| 122 | + |
|
| 123 | + if (!$res or (!$res['length'] and $res['status'] != 304)) { |
|
| 124 | + spip_log("copie_locale : Echec recuperation $source sur $localrac_tmp status : " . ($res ? $res['status'] : '-'), 'distant' . _LOG_INFO_IMPORTANTE); |
|
| 125 | + @unlink($localrac_tmp); |
|
| 126 | + } |
|
| 127 | + else { |
|
| 128 | + spip_log("copie_locale : recuperation $source sur $localrac_tmp OK | taille " . $res['length'] . ' status ' . $res['status'], 'distant'); |
|
| 129 | + } |
|
| 130 | + if (!$res or !$res['length']) { |
|
| 131 | + // si $t c'est sans doute juste un not-modified-since |
|
| 132 | + return $t ? $local : false; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + // si option valider url, verifions que l'URL finale est acceptable |
|
| 136 | + if ( |
|
| 137 | + $callback_valider_url |
|
| 138 | + and is_callable($callback_valider_url) |
|
| 139 | + and !$callback_valider_url($res['url']) |
|
| 140 | + ) { |
|
| 141 | + spip_log('copie_locale : url finale ' . $res['url'] . " non valide, on refuse le fichier $localrac_tmp", 'distant' . _LOG_INFO_IMPORTANTE); |
|
| 142 | + @unlink($localrac_tmp); |
|
| 143 | + return $t ? $local : false; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + // on peut renommer le fichier tmp |
|
| 147 | + @rename($localrac_tmp, $localrac); |
|
| 148 | + |
|
| 149 | + // si on retrouve l'extension |
|
| 150 | + if ( |
|
| 151 | + !empty($res['headers']) |
|
| 152 | + and $extension = distant_trouver_extension_selon_headers($source, $res['headers']) |
|
| 153 | + ) { |
|
| 154 | + if ($sanitizer = charger_fonction($extension, 'sanitizer', true)) { |
|
| 155 | + $sanitizer($localrac); |
|
| 156 | + } |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + // pour une eventuelle indexation |
|
| 160 | + pipeline( |
|
| 161 | + 'post_edition', |
|
| 162 | + [ |
|
| 163 | + 'args' => [ |
|
| 164 | + 'operation' => 'copie_locale', |
|
| 165 | + 'source' => $source, |
|
| 166 | + 'fichier' => $local, |
|
| 167 | + 'http_res' => $res['length'], |
|
| 168 | + 'url' => $res['url'], |
|
| 169 | + ], |
|
| 170 | + 'data' => null |
|
| 171 | + ] |
|
| 172 | + ); |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + return $local; |
|
| 176 | 176 | } |
| 177 | 177 | |
| 178 | 178 | /** |
@@ -187,99 +187,99 @@ discard block |
||
| 187 | 187 | * url ou false en cas d'echec |
| 188 | 188 | */ |
| 189 | 189 | function valider_url_distante($url, $known_hosts = []) { |
| 190 | - if (!function_exists('protocole_verifier')) { |
|
| 191 | - include_spip('inc/filtres_mini'); |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - if (!protocole_verifier($url, ['http', 'https'])) { |
|
| 195 | - return false; |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - $parsed_url = parse_url($url); |
|
| 199 | - if (!$parsed_url or empty($parsed_url['host'])) { |
|
| 200 | - return false; |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - if (isset($parsed_url['user']) or isset($parsed_url['pass'])) { |
|
| 204 | - return false; |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - if (false !== strpbrk($parsed_url['host'], ':#?[]')) { |
|
| 208 | - return false; |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - if (!is_array($known_hosts)) { |
|
| 212 | - $known_hosts = [$known_hosts]; |
|
| 213 | - } |
|
| 214 | - $known_hosts[] = $GLOBALS['meta']['adresse_site']; |
|
| 215 | - $known_hosts[] = url_de_base(); |
|
| 216 | - $known_hosts = pipeline('declarer_hosts_distants', $known_hosts); |
|
| 217 | - |
|
| 218 | - $is_known_host = false; |
|
| 219 | - foreach ($known_hosts as $known_host) { |
|
| 220 | - $parse_known = parse_url($known_host); |
|
| 221 | - if ( |
|
| 222 | - $parse_known |
|
| 223 | - and strtolower($parse_known['host']) === strtolower($parsed_url['host']) |
|
| 224 | - ) { |
|
| 225 | - $is_known_host = true; |
|
| 226 | - break; |
|
| 227 | - } |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - if (!$is_known_host) { |
|
| 231 | - $host = trim($parsed_url['host'], '.'); |
|
| 232 | - if (! $ip = filter_var($host, FILTER_VALIDATE_IP)) { |
|
| 233 | - $ip = gethostbyname($host); |
|
| 234 | - if ($ip === $host) { |
|
| 235 | - // Error condition for gethostbyname() |
|
| 236 | - $ip = false; |
|
| 237 | - } |
|
| 238 | - if ($records = dns_get_record($host)) { |
|
| 239 | - foreach ($records as $record) { |
|
| 240 | - // il faut que le TTL soit suffisant afin d'etre certain que le copie_locale eventuel qui suit |
|
| 241 | - // se fasse sur la meme IP |
|
| 242 | - if ($record['ttl'] < 10) { |
|
| 243 | - $ip = false; |
|
| 244 | - break; |
|
| 245 | - } |
|
| 246 | - } |
|
| 247 | - } |
|
| 248 | - else { |
|
| 249 | - $ip = false; |
|
| 250 | - } |
|
| 251 | - } |
|
| 252 | - if ($ip) { |
|
| 253 | - if (! filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) { |
|
| 254 | - return false; |
|
| 255 | - } |
|
| 256 | - } |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - if (empty($parsed_url['port'])) { |
|
| 260 | - return $url; |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - $port = $parsed_url['port']; |
|
| 264 | - if ($port === 80 or $port === 443 or $port === 8080) { |
|
| 265 | - return $url; |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - if ($is_known_host) { |
|
| 269 | - foreach ($known_hosts as $known_host) { |
|
| 270 | - $parse_known = parse_url($known_host); |
|
| 271 | - if ( |
|
| 272 | - $parse_known |
|
| 273 | - and !empty($parse_known['port']) |
|
| 274 | - and strtolower($parse_known['host']) === strtolower($parsed_url['host']) |
|
| 275 | - and $parse_known['port'] == $port |
|
| 276 | - ) { |
|
| 277 | - return $url; |
|
| 278 | - } |
|
| 279 | - } |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - return false; |
|
| 190 | + if (!function_exists('protocole_verifier')) { |
|
| 191 | + include_spip('inc/filtres_mini'); |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + if (!protocole_verifier($url, ['http', 'https'])) { |
|
| 195 | + return false; |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + $parsed_url = parse_url($url); |
|
| 199 | + if (!$parsed_url or empty($parsed_url['host'])) { |
|
| 200 | + return false; |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + if (isset($parsed_url['user']) or isset($parsed_url['pass'])) { |
|
| 204 | + return false; |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + if (false !== strpbrk($parsed_url['host'], ':#?[]')) { |
|
| 208 | + return false; |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + if (!is_array($known_hosts)) { |
|
| 212 | + $known_hosts = [$known_hosts]; |
|
| 213 | + } |
|
| 214 | + $known_hosts[] = $GLOBALS['meta']['adresse_site']; |
|
| 215 | + $known_hosts[] = url_de_base(); |
|
| 216 | + $known_hosts = pipeline('declarer_hosts_distants', $known_hosts); |
|
| 217 | + |
|
| 218 | + $is_known_host = false; |
|
| 219 | + foreach ($known_hosts as $known_host) { |
|
| 220 | + $parse_known = parse_url($known_host); |
|
| 221 | + if ( |
|
| 222 | + $parse_known |
|
| 223 | + and strtolower($parse_known['host']) === strtolower($parsed_url['host']) |
|
| 224 | + ) { |
|
| 225 | + $is_known_host = true; |
|
| 226 | + break; |
|
| 227 | + } |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + if (!$is_known_host) { |
|
| 231 | + $host = trim($parsed_url['host'], '.'); |
|
| 232 | + if (! $ip = filter_var($host, FILTER_VALIDATE_IP)) { |
|
| 233 | + $ip = gethostbyname($host); |
|
| 234 | + if ($ip === $host) { |
|
| 235 | + // Error condition for gethostbyname() |
|
| 236 | + $ip = false; |
|
| 237 | + } |
|
| 238 | + if ($records = dns_get_record($host)) { |
|
| 239 | + foreach ($records as $record) { |
|
| 240 | + // il faut que le TTL soit suffisant afin d'etre certain que le copie_locale eventuel qui suit |
|
| 241 | + // se fasse sur la meme IP |
|
| 242 | + if ($record['ttl'] < 10) { |
|
| 243 | + $ip = false; |
|
| 244 | + break; |
|
| 245 | + } |
|
| 246 | + } |
|
| 247 | + } |
|
| 248 | + else { |
|
| 249 | + $ip = false; |
|
| 250 | + } |
|
| 251 | + } |
|
| 252 | + if ($ip) { |
|
| 253 | + if (! filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) { |
|
| 254 | + return false; |
|
| 255 | + } |
|
| 256 | + } |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + if (empty($parsed_url['port'])) { |
|
| 260 | + return $url; |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + $port = $parsed_url['port']; |
|
| 264 | + if ($port === 80 or $port === 443 or $port === 8080) { |
|
| 265 | + return $url; |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + if ($is_known_host) { |
|
| 269 | + foreach ($known_hosts as $known_host) { |
|
| 270 | + $parse_known = parse_url($known_host); |
|
| 271 | + if ( |
|
| 272 | + $parse_known |
|
| 273 | + and !empty($parse_known['port']) |
|
| 274 | + and strtolower($parse_known['host']) === strtolower($parsed_url['host']) |
|
| 275 | + and $parse_known['port'] == $port |
|
| 276 | + ) { |
|
| 277 | + return $url; |
|
| 278 | + } |
|
| 279 | + } |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + return false; |
|
| 283 | 283 | } |
| 284 | 284 | |
| 285 | 285 | /** |
@@ -299,86 +299,86 @@ discard block |
||
| 299 | 299 | */ |
| 300 | 300 | function prepare_donnees_post($donnees, $boundary = '') { |
| 301 | 301 | |
| 302 | - // permettre a la fonction qui a demande le post de formater elle meme ses donnees |
|
| 303 | - // pour un appel soap par exemple |
|
| 304 | - // l'entete est separe des donnees par un double retour a la ligne |
|
| 305 | - // on s'occupe ici de passer tous les retours lignes (\r\n, \r ou \n) en \r\n |
|
| 306 | - if (is_string($donnees) && strlen($donnees)) { |
|
| 307 | - $entete = ''; |
|
| 308 | - // on repasse tous les \r\n et \r en simples \n |
|
| 309 | - $donnees = str_replace("\r\n", "\n", $donnees); |
|
| 310 | - $donnees = str_replace("\r", "\n", $donnees); |
|
| 311 | - // un double retour a la ligne signifie la fin de l'entete et le debut des donnees |
|
| 312 | - $p = strpos($donnees, "\n\n"); |
|
| 313 | - if ($p !== false) { |
|
| 314 | - $entete = str_replace("\n", "\r\n", substr($donnees, 0, $p + 1)); |
|
| 315 | - $donnees = substr($donnees, $p + 2); |
|
| 316 | - } |
|
| 317 | - $chaine = str_replace("\n", "\r\n", $donnees); |
|
| 318 | - } else { |
|
| 319 | - /* boundary automatique */ |
|
| 320 | - // Si on a plus de 500 octects de donnees, on "boundarise" |
|
| 321 | - if ($boundary === '') { |
|
| 322 | - $taille = 0; |
|
| 323 | - foreach ($donnees as $cle => $valeur) { |
|
| 324 | - if (is_array($valeur)) { |
|
| 325 | - foreach ($valeur as $val2) { |
|
| 326 | - $taille += strlen($val2); |
|
| 327 | - } |
|
| 328 | - } else { |
|
| 329 | - // faut-il utiliser spip_strlen() dans inc/charsets ? |
|
| 330 | - $taille += strlen($valeur); |
|
| 331 | - } |
|
| 332 | - } |
|
| 333 | - if ($taille > 500) { |
|
| 334 | - $boundary = substr(md5(random_int(0, mt_getrandmax()) . 'spip'), 0, 8); |
|
| 335 | - } |
|
| 336 | - } |
|
| 337 | - |
|
| 338 | - if (is_string($boundary) and strlen($boundary)) { |
|
| 339 | - // fabrique une chaine HTTP pour un POST avec boundary |
|
| 340 | - $entete = "Content-Type: multipart/form-data; boundary=$boundary\r\n"; |
|
| 341 | - $chaine = ''; |
|
| 342 | - if (is_array($donnees)) { |
|
| 343 | - foreach ($donnees as $cle => $valeur) { |
|
| 344 | - if (is_array($valeur)) { |
|
| 345 | - foreach ($valeur as $val2) { |
|
| 346 | - $chaine .= "\r\n--$boundary\r\n"; |
|
| 347 | - $chaine .= "Content-Disposition: form-data; name=\"{$cle}[]\"\r\n"; |
|
| 348 | - $chaine .= "\r\n"; |
|
| 349 | - $chaine .= $val2; |
|
| 350 | - } |
|
| 351 | - } else { |
|
| 352 | - $chaine .= "\r\n--$boundary\r\n"; |
|
| 353 | - $chaine .= "Content-Disposition: form-data; name=\"$cle\"\r\n"; |
|
| 354 | - $chaine .= "\r\n"; |
|
| 355 | - $chaine .= $valeur; |
|
| 356 | - } |
|
| 357 | - } |
|
| 358 | - $chaine .= "\r\n--$boundary\r\n"; |
|
| 359 | - } |
|
| 360 | - } else { |
|
| 361 | - // fabrique une chaine HTTP simple pour un POST |
|
| 362 | - $entete = 'Content-Type: application/x-www-form-urlencoded' . "\r\n"; |
|
| 363 | - $chaine = []; |
|
| 364 | - if (is_array($donnees)) { |
|
| 365 | - foreach ($donnees as $cle => $valeur) { |
|
| 366 | - if (is_array($valeur)) { |
|
| 367 | - foreach ($valeur as $val2) { |
|
| 368 | - $chaine[] = rawurlencode($cle) . '[]=' . rawurlencode($val2); |
|
| 369 | - } |
|
| 370 | - } else { |
|
| 371 | - $chaine[] = rawurlencode($cle) . '=' . rawurlencode($valeur); |
|
| 372 | - } |
|
| 373 | - } |
|
| 374 | - $chaine = implode('&', $chaine); |
|
| 375 | - } else { |
|
| 376 | - $chaine = $donnees; |
|
| 377 | - } |
|
| 378 | - } |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - return [$entete, $chaine]; |
|
| 302 | + // permettre a la fonction qui a demande le post de formater elle meme ses donnees |
|
| 303 | + // pour un appel soap par exemple |
|
| 304 | + // l'entete est separe des donnees par un double retour a la ligne |
|
| 305 | + // on s'occupe ici de passer tous les retours lignes (\r\n, \r ou \n) en \r\n |
|
| 306 | + if (is_string($donnees) && strlen($donnees)) { |
|
| 307 | + $entete = ''; |
|
| 308 | + // on repasse tous les \r\n et \r en simples \n |
|
| 309 | + $donnees = str_replace("\r\n", "\n", $donnees); |
|
| 310 | + $donnees = str_replace("\r", "\n", $donnees); |
|
| 311 | + // un double retour a la ligne signifie la fin de l'entete et le debut des donnees |
|
| 312 | + $p = strpos($donnees, "\n\n"); |
|
| 313 | + if ($p !== false) { |
|
| 314 | + $entete = str_replace("\n", "\r\n", substr($donnees, 0, $p + 1)); |
|
| 315 | + $donnees = substr($donnees, $p + 2); |
|
| 316 | + } |
|
| 317 | + $chaine = str_replace("\n", "\r\n", $donnees); |
|
| 318 | + } else { |
|
| 319 | + /* boundary automatique */ |
|
| 320 | + // Si on a plus de 500 octects de donnees, on "boundarise" |
|
| 321 | + if ($boundary === '') { |
|
| 322 | + $taille = 0; |
|
| 323 | + foreach ($donnees as $cle => $valeur) { |
|
| 324 | + if (is_array($valeur)) { |
|
| 325 | + foreach ($valeur as $val2) { |
|
| 326 | + $taille += strlen($val2); |
|
| 327 | + } |
|
| 328 | + } else { |
|
| 329 | + // faut-il utiliser spip_strlen() dans inc/charsets ? |
|
| 330 | + $taille += strlen($valeur); |
|
| 331 | + } |
|
| 332 | + } |
|
| 333 | + if ($taille > 500) { |
|
| 334 | + $boundary = substr(md5(random_int(0, mt_getrandmax()) . 'spip'), 0, 8); |
|
| 335 | + } |
|
| 336 | + } |
|
| 337 | + |
|
| 338 | + if (is_string($boundary) and strlen($boundary)) { |
|
| 339 | + // fabrique une chaine HTTP pour un POST avec boundary |
|
| 340 | + $entete = "Content-Type: multipart/form-data; boundary=$boundary\r\n"; |
|
| 341 | + $chaine = ''; |
|
| 342 | + if (is_array($donnees)) { |
|
| 343 | + foreach ($donnees as $cle => $valeur) { |
|
| 344 | + if (is_array($valeur)) { |
|
| 345 | + foreach ($valeur as $val2) { |
|
| 346 | + $chaine .= "\r\n--$boundary\r\n"; |
|
| 347 | + $chaine .= "Content-Disposition: form-data; name=\"{$cle}[]\"\r\n"; |
|
| 348 | + $chaine .= "\r\n"; |
|
| 349 | + $chaine .= $val2; |
|
| 350 | + } |
|
| 351 | + } else { |
|
| 352 | + $chaine .= "\r\n--$boundary\r\n"; |
|
| 353 | + $chaine .= "Content-Disposition: form-data; name=\"$cle\"\r\n"; |
|
| 354 | + $chaine .= "\r\n"; |
|
| 355 | + $chaine .= $valeur; |
|
| 356 | + } |
|
| 357 | + } |
|
| 358 | + $chaine .= "\r\n--$boundary\r\n"; |
|
| 359 | + } |
|
| 360 | + } else { |
|
| 361 | + // fabrique une chaine HTTP simple pour un POST |
|
| 362 | + $entete = 'Content-Type: application/x-www-form-urlencoded' . "\r\n"; |
|
| 363 | + $chaine = []; |
|
| 364 | + if (is_array($donnees)) { |
|
| 365 | + foreach ($donnees as $cle => $valeur) { |
|
| 366 | + if (is_array($valeur)) { |
|
| 367 | + foreach ($valeur as $val2) { |
|
| 368 | + $chaine[] = rawurlencode($cle) . '[]=' . rawurlencode($val2); |
|
| 369 | + } |
|
| 370 | + } else { |
|
| 371 | + $chaine[] = rawurlencode($cle) . '=' . rawurlencode($valeur); |
|
| 372 | + } |
|
| 373 | + } |
|
| 374 | + $chaine = implode('&', $chaine); |
|
| 375 | + } else { |
|
| 376 | + $chaine = $donnees; |
|
| 377 | + } |
|
| 378 | + } |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + return [$entete, $chaine]; |
|
| 382 | 382 | } |
| 383 | 383 | |
| 384 | 384 | /** |
@@ -389,19 +389,19 @@ discard block |
||
| 389 | 389 | */ |
| 390 | 390 | function url_to_ascii($url_idn) { |
| 391 | 391 | |
| 392 | - if ($parts = parse_url($url_idn)) { |
|
| 393 | - $host = $parts['host']; |
|
| 394 | - if (!preg_match(',^[a-z0-9_\.\-]+$,i', $host)) { |
|
| 395 | - $converter = new ToIdn(); |
|
| 396 | - $host_ascii = $converter->convert($host); |
|
| 397 | - $url_idn = explode($host, $url_idn, 2); |
|
| 398 | - $url_idn = implode($host_ascii, $url_idn); |
|
| 399 | - } |
|
| 400 | - // et on urlencode les char utf si besoin dans le path |
|
| 401 | - $url_idn = preg_replace_callback('/[^\x20-\x7f]/', fn($match) => urlencode($match[0]), $url_idn); |
|
| 402 | - } |
|
| 403 | - |
|
| 404 | - return $url_idn; |
|
| 392 | + if ($parts = parse_url($url_idn)) { |
|
| 393 | + $host = $parts['host']; |
|
| 394 | + if (!preg_match(',^[a-z0-9_\.\-]+$,i', $host)) { |
|
| 395 | + $converter = new ToIdn(); |
|
| 396 | + $host_ascii = $converter->convert($host); |
|
| 397 | + $url_idn = explode($host, $url_idn, 2); |
|
| 398 | + $url_idn = implode($host_ascii, $url_idn); |
|
| 399 | + } |
|
| 400 | + // et on urlencode les char utf si besoin dans le path |
|
| 401 | + $url_idn = preg_replace_callback('/[^\x20-\x7f]/', fn($match) => urlencode($match[0]), $url_idn); |
|
| 402 | + } |
|
| 403 | + |
|
| 404 | + return $url_idn; |
|
| 405 | 405 | } |
| 406 | 406 | |
| 407 | 407 | /** |
@@ -442,209 +442,209 @@ discard block |
||
| 442 | 442 | * string file : nom du fichier si enregistre dans un fichier |
| 443 | 443 | */ |
| 444 | 444 | function recuperer_url($url, $options = []) { |
| 445 | - // Conserve la mémoire de la méthode fournit éventuellement |
|
| 446 | - $methode_demandee = $options['methode'] ?? ''; |
|
| 447 | - $default = [ |
|
| 448 | - 'transcoder' => false, |
|
| 449 | - 'methode' => 'GET', |
|
| 450 | - 'taille_max' => null, |
|
| 451 | - 'headers' => [], |
|
| 452 | - 'datas' => '', |
|
| 453 | - 'boundary' => '', |
|
| 454 | - 'refuser_gz' => false, |
|
| 455 | - 'if_modified_since' => '', |
|
| 456 | - 'uri_referer' => '', |
|
| 457 | - 'file' => '', |
|
| 458 | - 'follow_location' => 10, |
|
| 459 | - 'version_http' => _INC_DISTANT_VERSION_HTTP, |
|
| 460 | - ]; |
|
| 461 | - $options = array_merge($default, $options); |
|
| 462 | - // copier directement dans un fichier ? |
|
| 463 | - $copy = $options['file']; |
|
| 464 | - |
|
| 465 | - if ($options['methode'] == 'HEAD') { |
|
| 466 | - $options['taille_max'] = 0; |
|
| 467 | - } |
|
| 468 | - if (is_null($options['taille_max'])) { |
|
| 469 | - $options['taille_max'] = $copy ? _COPIE_LOCALE_MAX_SIZE : _INC_DISTANT_MAX_SIZE; |
|
| 470 | - } |
|
| 471 | - |
|
| 472 | - spip_log('recuperer_url ' . $options['methode'] . " sur $url", 'distant' . _LOG_DEBUG); |
|
| 473 | - |
|
| 474 | - // Ajout des en-têtes spécifiques si besoin |
|
| 475 | - $formatted_data = ''; |
|
| 476 | - if (!empty($options['headers'])) { |
|
| 477 | - foreach ($options['headers'] as $champ => $valeur) { |
|
| 478 | - $formatted_data .= $champ . ': ' . $valeur . "\r\n"; |
|
| 479 | - } |
|
| 480 | - } |
|
| 481 | - |
|
| 482 | - if (!empty($options['datas'])) { |
|
| 483 | - [$head, $postdata] = prepare_donnees_post($options['datas'], $options['boundary']); |
|
| 484 | - $head .= $formatted_data; |
|
| 485 | - if (stripos($head, 'Content-Length:') === false) { |
|
| 486 | - $head .= 'Content-Length: ' . strlen($postdata) . "\r\n"; |
|
| 487 | - } |
|
| 488 | - $formatted_data = $head . "\r\n" . $postdata; |
|
| 489 | - if ( |
|
| 490 | - strlen($postdata) |
|
| 491 | - and !$methode_demandee |
|
| 492 | - ) { |
|
| 493 | - $options['methode'] = 'POST'; |
|
| 494 | - } |
|
| 495 | - } elseif ($formatted_data) { |
|
| 496 | - $formatted_data .= "\r\n"; |
|
| 497 | - } |
|
| 498 | - |
|
| 499 | - // Accepter les URLs au format feed:// ou qui ont oublie le http:// ou les urls relatives au protocole |
|
| 500 | - $url = preg_replace(',^feed://,i', 'http://', $url); |
|
| 501 | - if (!tester_url_absolue($url)) { |
|
| 502 | - $url = 'http://' . $url; |
|
| 503 | - } elseif (strncmp($url, '//', 2) == 0) { |
|
| 504 | - $url = 'http:' . $url; |
|
| 505 | - } |
|
| 506 | - |
|
| 507 | - $url = url_to_ascii($url); |
|
| 508 | - |
|
| 509 | - $result = [ |
|
| 510 | - 'status' => 0, |
|
| 511 | - 'headers' => '', |
|
| 512 | - 'page' => '', |
|
| 513 | - 'length' => 0, |
|
| 514 | - 'last_modified' => '', |
|
| 515 | - 'location' => '', |
|
| 516 | - 'url' => $url |
|
| 517 | - ]; |
|
| 518 | - |
|
| 519 | - // si on ecrit directement dans un fichier, pour ne pas manipuler en memoire refuser gz |
|
| 520 | - $refuser_gz = (($options['refuser_gz'] or $copy) ? true : false); |
|
| 521 | - |
|
| 522 | - // ouvrir la connexion et envoyer la requete et ses en-tetes |
|
| 523 | - [$handle, $fopen] = init_http( |
|
| 524 | - $options['methode'], |
|
| 525 | - $url, |
|
| 526 | - $refuser_gz, |
|
| 527 | - $options['uri_referer'], |
|
| 528 | - $formatted_data, |
|
| 529 | - $options['version_http'], |
|
| 530 | - $options['if_modified_since'] |
|
| 531 | - ); |
|
| 532 | - if (!$handle) { |
|
| 533 | - spip_log("ECHEC init_http $url", 'distant' . _LOG_ERREUR); |
|
| 534 | - |
|
| 535 | - return false; |
|
| 536 | - } |
|
| 537 | - |
|
| 538 | - // Sauf en fopen, envoyer le flux d'entree |
|
| 539 | - // et recuperer les en-tetes de reponses |
|
| 540 | - if (!$fopen) { |
|
| 541 | - $res = recuperer_entetes_complets($handle, $options['if_modified_since']); |
|
| 542 | - if (!$res) { |
|
| 543 | - fclose($handle); |
|
| 544 | - $t = @parse_url($url); |
|
| 545 | - $host = $t['host']; |
|
| 546 | - // Chinoisierie inexplicable pour contrer |
|
| 547 | - // les actions liberticides de l'empire du milieu |
|
| 548 | - if ( |
|
| 549 | - !need_proxy($host) |
|
| 550 | - and $res = @file_get_contents($url) |
|
| 551 | - ) { |
|
| 552 | - $result['length'] = strlen($res); |
|
| 553 | - if ($copy) { |
|
| 554 | - ecrire_fichier($copy, $res); |
|
| 555 | - $result['file'] = $copy; |
|
| 556 | - } else { |
|
| 557 | - $result['page'] = $res; |
|
| 558 | - } |
|
| 559 | - $res = [ |
|
| 560 | - 'status' => 200, |
|
| 561 | - ]; |
|
| 562 | - } else { |
|
| 563 | - spip_log("ECHEC chinoiserie $url", 'distant' . _LOG_ERREUR); |
|
| 564 | - return false; |
|
| 565 | - } |
|
| 566 | - } elseif ($res['location'] and $options['follow_location']) { |
|
| 567 | - $options['follow_location']--; |
|
| 568 | - fclose($handle); |
|
| 569 | - include_spip('inc/filtres'); |
|
| 570 | - $url = suivre_lien($url, $res['location']); |
|
| 571 | - |
|
| 572 | - // une redirection doit se faire en GET, sauf status explicite 307 ou 308 qui indique de garder la meme methode |
|
| 573 | - if ($options['methode'] !== 'GET') { |
|
| 574 | - if (empty($res['status']) or !in_array($res['status'], [307, 308])) { |
|
| 575 | - $options['methode'] = 'GET'; |
|
| 576 | - $options['datas'] = ''; |
|
| 577 | - } |
|
| 578 | - } |
|
| 579 | - spip_log('recuperer_url recommence ' . $options['methode'] . " sur $url", 'distant' . _LOG_DEBUG); |
|
| 580 | - |
|
| 581 | - return recuperer_url($url, $options); |
|
| 582 | - } elseif ($res['status'] !== 200) { |
|
| 583 | - spip_log('HTTP status ' . $res['status'] . " pour $url", 'distant'); |
|
| 584 | - } |
|
| 585 | - $result['status'] = $res['status']; |
|
| 586 | - if (isset($res['headers'])) { |
|
| 587 | - $result['headers'] = $res['headers']; |
|
| 588 | - } |
|
| 589 | - if (isset($res['last_modified'])) { |
|
| 590 | - $result['last_modified'] = $res['last_modified']; |
|
| 591 | - } |
|
| 592 | - if (isset($res['location'])) { |
|
| 593 | - $result['location'] = $res['location']; |
|
| 594 | - } |
|
| 595 | - } |
|
| 596 | - |
|
| 597 | - // on ne veut que les entetes |
|
| 598 | - if (!$options['taille_max'] or $options['methode'] == 'HEAD' or $result['status'] == '304') { |
|
| 599 | - spip_log('RESULTAT recuperer_url ' . $options['methode'] . " sur $url : " . json_encode($result), 'distant' . _LOG_DEBUG); |
|
| 600 | - return $result; |
|
| 601 | - } |
|
| 602 | - |
|
| 603 | - |
|
| 604 | - // s'il faut deballer, le faire via un fichier temporaire |
|
| 605 | - // sinon la memoire explose pour les gros flux |
|
| 606 | - |
|
| 607 | - $gz = false; |
|
| 608 | - if (preg_match(",\bContent-Encoding: .*gzip,is", $result['headers'])) { |
|
| 609 | - $gz = (_DIR_TMP . md5(uniqid(random_int(0, mt_getrandmax()))) . '.tmp.gz'); |
|
| 610 | - } |
|
| 611 | - |
|
| 612 | - // si on a pas deja recuperer le contenu par une methode detournee |
|
| 613 | - if (!$result['length']) { |
|
| 614 | - $res = recuperer_body($handle, $options['taille_max'], $gz ?: $copy); |
|
| 615 | - fclose($handle); |
|
| 616 | - if ($copy) { |
|
| 617 | - $result['length'] = $res; |
|
| 618 | - $result['file'] = $copy; |
|
| 619 | - } elseif ($res) { |
|
| 620 | - $result['page'] = &$res; |
|
| 621 | - $result['length'] = strlen($result['page']); |
|
| 622 | - } |
|
| 623 | - if (!$result['status']) { |
|
| 624 | - $result['status'] = 200; // on a reussi, donc ! |
|
| 625 | - } |
|
| 626 | - } |
|
| 627 | - if (!$result['page']) { |
|
| 628 | - return $result; |
|
| 629 | - } |
|
| 630 | - |
|
| 631 | - // Decompresser au besoin |
|
| 632 | - if ($gz) { |
|
| 633 | - $result['page'] = implode('', gzfile($gz)); |
|
| 634 | - supprimer_fichier($gz); |
|
| 635 | - } |
|
| 636 | - |
|
| 637 | - // Faut-il l'importer dans notre charset local ? |
|
| 638 | - if ($options['transcoder']) { |
|
| 639 | - include_spip('inc/charsets'); |
|
| 640 | - $result['page'] = transcoder_page($result['page'], $result['headers']); |
|
| 641 | - } |
|
| 642 | - |
|
| 643 | - $trace = json_decode(json_encode($result), true); |
|
| 644 | - $trace['page'] = '...'; |
|
| 645 | - spip_log('RESULTAT recuperer_url ' . $options['methode'] . " sur $url : " . json_encode($trace), 'distant' . _LOG_DEBUG); |
|
| 646 | - |
|
| 647 | - return $result; |
|
| 445 | + // Conserve la mémoire de la méthode fournit éventuellement |
|
| 446 | + $methode_demandee = $options['methode'] ?? ''; |
|
| 447 | + $default = [ |
|
| 448 | + 'transcoder' => false, |
|
| 449 | + 'methode' => 'GET', |
|
| 450 | + 'taille_max' => null, |
|
| 451 | + 'headers' => [], |
|
| 452 | + 'datas' => '', |
|
| 453 | + 'boundary' => '', |
|
| 454 | + 'refuser_gz' => false, |
|
| 455 | + 'if_modified_since' => '', |
|
| 456 | + 'uri_referer' => '', |
|
| 457 | + 'file' => '', |
|
| 458 | + 'follow_location' => 10, |
|
| 459 | + 'version_http' => _INC_DISTANT_VERSION_HTTP, |
|
| 460 | + ]; |
|
| 461 | + $options = array_merge($default, $options); |
|
| 462 | + // copier directement dans un fichier ? |
|
| 463 | + $copy = $options['file']; |
|
| 464 | + |
|
| 465 | + if ($options['methode'] == 'HEAD') { |
|
| 466 | + $options['taille_max'] = 0; |
|
| 467 | + } |
|
| 468 | + if (is_null($options['taille_max'])) { |
|
| 469 | + $options['taille_max'] = $copy ? _COPIE_LOCALE_MAX_SIZE : _INC_DISTANT_MAX_SIZE; |
|
| 470 | + } |
|
| 471 | + |
|
| 472 | + spip_log('recuperer_url ' . $options['methode'] . " sur $url", 'distant' . _LOG_DEBUG); |
|
| 473 | + |
|
| 474 | + // Ajout des en-têtes spécifiques si besoin |
|
| 475 | + $formatted_data = ''; |
|
| 476 | + if (!empty($options['headers'])) { |
|
| 477 | + foreach ($options['headers'] as $champ => $valeur) { |
|
| 478 | + $formatted_data .= $champ . ': ' . $valeur . "\r\n"; |
|
| 479 | + } |
|
| 480 | + } |
|
| 481 | + |
|
| 482 | + if (!empty($options['datas'])) { |
|
| 483 | + [$head, $postdata] = prepare_donnees_post($options['datas'], $options['boundary']); |
|
| 484 | + $head .= $formatted_data; |
|
| 485 | + if (stripos($head, 'Content-Length:') === false) { |
|
| 486 | + $head .= 'Content-Length: ' . strlen($postdata) . "\r\n"; |
|
| 487 | + } |
|
| 488 | + $formatted_data = $head . "\r\n" . $postdata; |
|
| 489 | + if ( |
|
| 490 | + strlen($postdata) |
|
| 491 | + and !$methode_demandee |
|
| 492 | + ) { |
|
| 493 | + $options['methode'] = 'POST'; |
|
| 494 | + } |
|
| 495 | + } elseif ($formatted_data) { |
|
| 496 | + $formatted_data .= "\r\n"; |
|
| 497 | + } |
|
| 498 | + |
|
| 499 | + // Accepter les URLs au format feed:// ou qui ont oublie le http:// ou les urls relatives au protocole |
|
| 500 | + $url = preg_replace(',^feed://,i', 'http://', $url); |
|
| 501 | + if (!tester_url_absolue($url)) { |
|
| 502 | + $url = 'http://' . $url; |
|
| 503 | + } elseif (strncmp($url, '//', 2) == 0) { |
|
| 504 | + $url = 'http:' . $url; |
|
| 505 | + } |
|
| 506 | + |
|
| 507 | + $url = url_to_ascii($url); |
|
| 508 | + |
|
| 509 | + $result = [ |
|
| 510 | + 'status' => 0, |
|
| 511 | + 'headers' => '', |
|
| 512 | + 'page' => '', |
|
| 513 | + 'length' => 0, |
|
| 514 | + 'last_modified' => '', |
|
| 515 | + 'location' => '', |
|
| 516 | + 'url' => $url |
|
| 517 | + ]; |
|
| 518 | + |
|
| 519 | + // si on ecrit directement dans un fichier, pour ne pas manipuler en memoire refuser gz |
|
| 520 | + $refuser_gz = (($options['refuser_gz'] or $copy) ? true : false); |
|
| 521 | + |
|
| 522 | + // ouvrir la connexion et envoyer la requete et ses en-tetes |
|
| 523 | + [$handle, $fopen] = init_http( |
|
| 524 | + $options['methode'], |
|
| 525 | + $url, |
|
| 526 | + $refuser_gz, |
|
| 527 | + $options['uri_referer'], |
|
| 528 | + $formatted_data, |
|
| 529 | + $options['version_http'], |
|
| 530 | + $options['if_modified_since'] |
|
| 531 | + ); |
|
| 532 | + if (!$handle) { |
|
| 533 | + spip_log("ECHEC init_http $url", 'distant' . _LOG_ERREUR); |
|
| 534 | + |
|
| 535 | + return false; |
|
| 536 | + } |
|
| 537 | + |
|
| 538 | + // Sauf en fopen, envoyer le flux d'entree |
|
| 539 | + // et recuperer les en-tetes de reponses |
|
| 540 | + if (!$fopen) { |
|
| 541 | + $res = recuperer_entetes_complets($handle, $options['if_modified_since']); |
|
| 542 | + if (!$res) { |
|
| 543 | + fclose($handle); |
|
| 544 | + $t = @parse_url($url); |
|
| 545 | + $host = $t['host']; |
|
| 546 | + // Chinoisierie inexplicable pour contrer |
|
| 547 | + // les actions liberticides de l'empire du milieu |
|
| 548 | + if ( |
|
| 549 | + !need_proxy($host) |
|
| 550 | + and $res = @file_get_contents($url) |
|
| 551 | + ) { |
|
| 552 | + $result['length'] = strlen($res); |
|
| 553 | + if ($copy) { |
|
| 554 | + ecrire_fichier($copy, $res); |
|
| 555 | + $result['file'] = $copy; |
|
| 556 | + } else { |
|
| 557 | + $result['page'] = $res; |
|
| 558 | + } |
|
| 559 | + $res = [ |
|
| 560 | + 'status' => 200, |
|
| 561 | + ]; |
|
| 562 | + } else { |
|
| 563 | + spip_log("ECHEC chinoiserie $url", 'distant' . _LOG_ERREUR); |
|
| 564 | + return false; |
|
| 565 | + } |
|
| 566 | + } elseif ($res['location'] and $options['follow_location']) { |
|
| 567 | + $options['follow_location']--; |
|
| 568 | + fclose($handle); |
|
| 569 | + include_spip('inc/filtres'); |
|
| 570 | + $url = suivre_lien($url, $res['location']); |
|
| 571 | + |
|
| 572 | + // une redirection doit se faire en GET, sauf status explicite 307 ou 308 qui indique de garder la meme methode |
|
| 573 | + if ($options['methode'] !== 'GET') { |
|
| 574 | + if (empty($res['status']) or !in_array($res['status'], [307, 308])) { |
|
| 575 | + $options['methode'] = 'GET'; |
|
| 576 | + $options['datas'] = ''; |
|
| 577 | + } |
|
| 578 | + } |
|
| 579 | + spip_log('recuperer_url recommence ' . $options['methode'] . " sur $url", 'distant' . _LOG_DEBUG); |
|
| 580 | + |
|
| 581 | + return recuperer_url($url, $options); |
|
| 582 | + } elseif ($res['status'] !== 200) { |
|
| 583 | + spip_log('HTTP status ' . $res['status'] . " pour $url", 'distant'); |
|
| 584 | + } |
|
| 585 | + $result['status'] = $res['status']; |
|
| 586 | + if (isset($res['headers'])) { |
|
| 587 | + $result['headers'] = $res['headers']; |
|
| 588 | + } |
|
| 589 | + if (isset($res['last_modified'])) { |
|
| 590 | + $result['last_modified'] = $res['last_modified']; |
|
| 591 | + } |
|
| 592 | + if (isset($res['location'])) { |
|
| 593 | + $result['location'] = $res['location']; |
|
| 594 | + } |
|
| 595 | + } |
|
| 596 | + |
|
| 597 | + // on ne veut que les entetes |
|
| 598 | + if (!$options['taille_max'] or $options['methode'] == 'HEAD' or $result['status'] == '304') { |
|
| 599 | + spip_log('RESULTAT recuperer_url ' . $options['methode'] . " sur $url : " . json_encode($result), 'distant' . _LOG_DEBUG); |
|
| 600 | + return $result; |
|
| 601 | + } |
|
| 602 | + |
|
| 603 | + |
|
| 604 | + // s'il faut deballer, le faire via un fichier temporaire |
|
| 605 | + // sinon la memoire explose pour les gros flux |
|
| 606 | + |
|
| 607 | + $gz = false; |
|
| 608 | + if (preg_match(",\bContent-Encoding: .*gzip,is", $result['headers'])) { |
|
| 609 | + $gz = (_DIR_TMP . md5(uniqid(random_int(0, mt_getrandmax()))) . '.tmp.gz'); |
|
| 610 | + } |
|
| 611 | + |
|
| 612 | + // si on a pas deja recuperer le contenu par une methode detournee |
|
| 613 | + if (!$result['length']) { |
|
| 614 | + $res = recuperer_body($handle, $options['taille_max'], $gz ?: $copy); |
|
| 615 | + fclose($handle); |
|
| 616 | + if ($copy) { |
|
| 617 | + $result['length'] = $res; |
|
| 618 | + $result['file'] = $copy; |
|
| 619 | + } elseif ($res) { |
|
| 620 | + $result['page'] = &$res; |
|
| 621 | + $result['length'] = strlen($result['page']); |
|
| 622 | + } |
|
| 623 | + if (!$result['status']) { |
|
| 624 | + $result['status'] = 200; // on a reussi, donc ! |
|
| 625 | + } |
|
| 626 | + } |
|
| 627 | + if (!$result['page']) { |
|
| 628 | + return $result; |
|
| 629 | + } |
|
| 630 | + |
|
| 631 | + // Decompresser au besoin |
|
| 632 | + if ($gz) { |
|
| 633 | + $result['page'] = implode('', gzfile($gz)); |
|
| 634 | + supprimer_fichier($gz); |
|
| 635 | + } |
|
| 636 | + |
|
| 637 | + // Faut-il l'importer dans notre charset local ? |
|
| 638 | + if ($options['transcoder']) { |
|
| 639 | + include_spip('inc/charsets'); |
|
| 640 | + $result['page'] = transcoder_page($result['page'], $result['headers']); |
|
| 641 | + } |
|
| 642 | + |
|
| 643 | + $trace = json_decode(json_encode($result), true); |
|
| 644 | + $trace['page'] = '...'; |
|
| 645 | + spip_log('RESULTAT recuperer_url ' . $options['methode'] . " sur $url : " . json_encode($trace), 'distant' . _LOG_DEBUG); |
|
| 646 | + |
|
| 647 | + return $result; |
|
| 648 | 648 | } |
| 649 | 649 | |
| 650 | 650 | /** |
@@ -660,73 +660,73 @@ discard block |
||
| 660 | 660 | * @return array|bool|mixed |
| 661 | 661 | */ |
| 662 | 662 | function recuperer_url_cache($url, $options = []) { |
| 663 | - if (!defined('_DELAI_RECUPERER_URL_CACHE')) { |
|
| 664 | - define('_DELAI_RECUPERER_URL_CACHE', 3600); |
|
| 665 | - } |
|
| 666 | - $default = [ |
|
| 667 | - 'transcoder' => false, |
|
| 668 | - 'methode' => 'GET', |
|
| 669 | - 'taille_max' => null, |
|
| 670 | - 'datas' => '', |
|
| 671 | - 'boundary' => '', |
|
| 672 | - 'refuser_gz' => false, |
|
| 673 | - 'if_modified_since' => '', |
|
| 674 | - 'uri_referer' => '', |
|
| 675 | - 'file' => '', |
|
| 676 | - 'follow_location' => 10, |
|
| 677 | - 'version_http' => _INC_DISTANT_VERSION_HTTP, |
|
| 678 | - 'delai_cache' => in_array(_VAR_MODE, ['preview', 'recalcul']) ? 0 : _DELAI_RECUPERER_URL_CACHE, |
|
| 679 | - ]; |
|
| 680 | - $options = array_merge($default, $options); |
|
| 681 | - |
|
| 682 | - // cas ou il n'est pas possible de cacher |
|
| 683 | - if (!empty($options['data']) or $options['methode'] == 'POST') { |
|
| 684 | - return recuperer_url($url, $options); |
|
| 685 | - } |
|
| 686 | - |
|
| 687 | - // ne pas tenter plusieurs fois la meme url en erreur (non cachee donc) |
|
| 688 | - static $errors = []; |
|
| 689 | - if (isset($errors[$url])) { |
|
| 690 | - return $errors[$url]; |
|
| 691 | - } |
|
| 692 | - |
|
| 693 | - $sig = $options; |
|
| 694 | - unset($sig['if_modified_since']); |
|
| 695 | - unset($sig['delai_cache']); |
|
| 696 | - $sig['url'] = $url; |
|
| 697 | - |
|
| 698 | - $dir = sous_repertoire(_DIR_CACHE, 'curl'); |
|
| 699 | - $cache = md5(serialize($sig)) . '-' . substr(preg_replace(',\W+,', '_', $url), 0, 80); |
|
| 700 | - $sub = sous_repertoire($dir, substr($cache, 0, 2)); |
|
| 701 | - $cache = "$sub$cache"; |
|
| 702 | - |
|
| 703 | - $res = false; |
|
| 704 | - $is_cached = file_exists($cache); |
|
| 705 | - if ( |
|
| 706 | - $is_cached |
|
| 707 | - and (filemtime($cache) > $_SERVER['REQUEST_TIME'] - $options['delai_cache']) |
|
| 708 | - ) { |
|
| 709 | - lire_fichier($cache, $res); |
|
| 710 | - if ($res = unserialize($res)) { |
|
| 711 | - // mettre le last_modified et le status=304 ? |
|
| 712 | - } |
|
| 713 | - } |
|
| 714 | - if (!$res) { |
|
| 715 | - $res = recuperer_url($url, $options); |
|
| 716 | - // ne pas recharger cette url non cachee dans le meme hit puisque non disponible |
|
| 717 | - if (!$res) { |
|
| 718 | - if ($is_cached) { |
|
| 719 | - // on a pas reussi a recuperer mais on avait un cache : l'utiliser |
|
| 720 | - lire_fichier($cache, $res); |
|
| 721 | - $res = unserialize($res); |
|
| 722 | - } |
|
| 723 | - |
|
| 724 | - return $errors[$url] = $res; |
|
| 725 | - } |
|
| 726 | - ecrire_fichier($cache, serialize($res)); |
|
| 727 | - } |
|
| 728 | - |
|
| 729 | - return $res; |
|
| 663 | + if (!defined('_DELAI_RECUPERER_URL_CACHE')) { |
|
| 664 | + define('_DELAI_RECUPERER_URL_CACHE', 3600); |
|
| 665 | + } |
|
| 666 | + $default = [ |
|
| 667 | + 'transcoder' => false, |
|
| 668 | + 'methode' => 'GET', |
|
| 669 | + 'taille_max' => null, |
|
| 670 | + 'datas' => '', |
|
| 671 | + 'boundary' => '', |
|
| 672 | + 'refuser_gz' => false, |
|
| 673 | + 'if_modified_since' => '', |
|
| 674 | + 'uri_referer' => '', |
|
| 675 | + 'file' => '', |
|
| 676 | + 'follow_location' => 10, |
|
| 677 | + 'version_http' => _INC_DISTANT_VERSION_HTTP, |
|
| 678 | + 'delai_cache' => in_array(_VAR_MODE, ['preview', 'recalcul']) ? 0 : _DELAI_RECUPERER_URL_CACHE, |
|
| 679 | + ]; |
|
| 680 | + $options = array_merge($default, $options); |
|
| 681 | + |
|
| 682 | + // cas ou il n'est pas possible de cacher |
|
| 683 | + if (!empty($options['data']) or $options['methode'] == 'POST') { |
|
| 684 | + return recuperer_url($url, $options); |
|
| 685 | + } |
|
| 686 | + |
|
| 687 | + // ne pas tenter plusieurs fois la meme url en erreur (non cachee donc) |
|
| 688 | + static $errors = []; |
|
| 689 | + if (isset($errors[$url])) { |
|
| 690 | + return $errors[$url]; |
|
| 691 | + } |
|
| 692 | + |
|
| 693 | + $sig = $options; |
|
| 694 | + unset($sig['if_modified_since']); |
|
| 695 | + unset($sig['delai_cache']); |
|
| 696 | + $sig['url'] = $url; |
|
| 697 | + |
|
| 698 | + $dir = sous_repertoire(_DIR_CACHE, 'curl'); |
|
| 699 | + $cache = md5(serialize($sig)) . '-' . substr(preg_replace(',\W+,', '_', $url), 0, 80); |
|
| 700 | + $sub = sous_repertoire($dir, substr($cache, 0, 2)); |
|
| 701 | + $cache = "$sub$cache"; |
|
| 702 | + |
|
| 703 | + $res = false; |
|
| 704 | + $is_cached = file_exists($cache); |
|
| 705 | + if ( |
|
| 706 | + $is_cached |
|
| 707 | + and (filemtime($cache) > $_SERVER['REQUEST_TIME'] - $options['delai_cache']) |
|
| 708 | + ) { |
|
| 709 | + lire_fichier($cache, $res); |
|
| 710 | + if ($res = unserialize($res)) { |
|
| 711 | + // mettre le last_modified et le status=304 ? |
|
| 712 | + } |
|
| 713 | + } |
|
| 714 | + if (!$res) { |
|
| 715 | + $res = recuperer_url($url, $options); |
|
| 716 | + // ne pas recharger cette url non cachee dans le meme hit puisque non disponible |
|
| 717 | + if (!$res) { |
|
| 718 | + if ($is_cached) { |
|
| 719 | + // on a pas reussi a recuperer mais on avait un cache : l'utiliser |
|
| 720 | + lire_fichier($cache, $res); |
|
| 721 | + $res = unserialize($res); |
|
| 722 | + } |
|
| 723 | + |
|
| 724 | + return $errors[$url] = $res; |
|
| 725 | + } |
|
| 726 | + ecrire_fichier($cache, serialize($res)); |
|
| 727 | + } |
|
| 728 | + |
|
| 729 | + return $res; |
|
| 730 | 730 | } |
| 731 | 731 | |
| 732 | 732 | /** |
@@ -744,42 +744,42 @@ discard block |
||
| 744 | 744 | * string contenu de la resource |
| 745 | 745 | */ |
| 746 | 746 | function recuperer_body($handle, $taille_max = _INC_DISTANT_MAX_SIZE, $fichier = '') { |
| 747 | - $tmpfile = null; |
|
| 748 | - $taille = 0; |
|
| 749 | - $result = ''; |
|
| 750 | - $fp = false; |
|
| 751 | - if ($fichier) { |
|
| 752 | - include_spip('inc/acces'); |
|
| 753 | - $tmpfile = "$fichier." . creer_uniqid() . '.tmp'; |
|
| 754 | - $fp = spip_fopen_lock($tmpfile, 'w', LOCK_EX); |
|
| 755 | - if (!$fp and file_exists($fichier)) { |
|
| 756 | - return filesize($fichier); |
|
| 757 | - } |
|
| 758 | - if (!$fp) { |
|
| 759 | - return false; |
|
| 760 | - } |
|
| 761 | - $result = 0; // on renvoie la taille du fichier |
|
| 762 | - } |
|
| 763 | - while (!feof($handle) and $taille < $taille_max) { |
|
| 764 | - $res = fread($handle, 16384); |
|
| 765 | - $taille += strlen($res); |
|
| 766 | - if ($fp) { |
|
| 767 | - fwrite($fp, $res); |
|
| 768 | - $result = $taille; |
|
| 769 | - } else { |
|
| 770 | - $result .= $res; |
|
| 771 | - } |
|
| 772 | - } |
|
| 773 | - if ($fp) { |
|
| 774 | - spip_fclose_unlock($fp); |
|
| 775 | - spip_unlink($fichier); |
|
| 776 | - @rename($tmpfile, $fichier); |
|
| 777 | - if (!file_exists($fichier)) { |
|
| 778 | - return false; |
|
| 779 | - } |
|
| 780 | - } |
|
| 781 | - |
|
| 782 | - return $result; |
|
| 747 | + $tmpfile = null; |
|
| 748 | + $taille = 0; |
|
| 749 | + $result = ''; |
|
| 750 | + $fp = false; |
|
| 751 | + if ($fichier) { |
|
| 752 | + include_spip('inc/acces'); |
|
| 753 | + $tmpfile = "$fichier." . creer_uniqid() . '.tmp'; |
|
| 754 | + $fp = spip_fopen_lock($tmpfile, 'w', LOCK_EX); |
|
| 755 | + if (!$fp and file_exists($fichier)) { |
|
| 756 | + return filesize($fichier); |
|
| 757 | + } |
|
| 758 | + if (!$fp) { |
|
| 759 | + return false; |
|
| 760 | + } |
|
| 761 | + $result = 0; // on renvoie la taille du fichier |
|
| 762 | + } |
|
| 763 | + while (!feof($handle) and $taille < $taille_max) { |
|
| 764 | + $res = fread($handle, 16384); |
|
| 765 | + $taille += strlen($res); |
|
| 766 | + if ($fp) { |
|
| 767 | + fwrite($fp, $res); |
|
| 768 | + $result = $taille; |
|
| 769 | + } else { |
|
| 770 | + $result .= $res; |
|
| 771 | + } |
|
| 772 | + } |
|
| 773 | + if ($fp) { |
|
| 774 | + spip_fclose_unlock($fp); |
|
| 775 | + spip_unlink($fichier); |
|
| 776 | + @rename($tmpfile, $fichier); |
|
| 777 | + if (!file_exists($fichier)) { |
|
| 778 | + return false; |
|
| 779 | + } |
|
| 780 | + } |
|
| 781 | + |
|
| 782 | + return $result; |
|
| 783 | 783 | } |
| 784 | 784 | |
| 785 | 785 | /** |
@@ -801,35 +801,35 @@ discard block |
||
| 801 | 801 | * string location |
| 802 | 802 | */ |
| 803 | 803 | function recuperer_entetes_complets($handle, $if_modified_since = false) { |
| 804 | - $result = ['status' => 0, 'headers' => [], 'last_modified' => 0, 'location' => '']; |
|
| 805 | - |
|
| 806 | - $s = @trim(fgets($handle, 16384)); |
|
| 807 | - if (!preg_match(',^HTTP/[0-9]+\.[0-9]+ ([0-9]+),', $s, $r)) { |
|
| 808 | - return false; |
|
| 809 | - } |
|
| 810 | - $result['status'] = intval($r[1]); |
|
| 811 | - while ($s = trim(fgets($handle, 16384))) { |
|
| 812 | - $result['headers'][] = $s . "\n"; |
|
| 813 | - preg_match(',^([^:]*): *(.*)$,i', $s, $r); |
|
| 814 | - [, $d, $v] = $r; |
|
| 815 | - if (strtolower(trim($d)) == 'location' and $result['status'] >= 300 and $result['status'] < 400) { |
|
| 816 | - $result['location'] = $v; |
|
| 817 | - } elseif ($d == 'Last-Modified') { |
|
| 818 | - $result['last_modified'] = strtotime($v); |
|
| 819 | - } |
|
| 820 | - } |
|
| 821 | - if ( |
|
| 822 | - $if_modified_since |
|
| 823 | - and $result['last_modified'] |
|
| 824 | - and $if_modified_since > $result['last_modified'] |
|
| 825 | - and $result['status'] == 200 |
|
| 826 | - ) { |
|
| 827 | - $result['status'] = 304; |
|
| 828 | - } |
|
| 829 | - |
|
| 830 | - $result['headers'] = implode('', $result['headers']); |
|
| 831 | - |
|
| 832 | - return $result; |
|
| 804 | + $result = ['status' => 0, 'headers' => [], 'last_modified' => 0, 'location' => '']; |
|
| 805 | + |
|
| 806 | + $s = @trim(fgets($handle, 16384)); |
|
| 807 | + if (!preg_match(',^HTTP/[0-9]+\.[0-9]+ ([0-9]+),', $s, $r)) { |
|
| 808 | + return false; |
|
| 809 | + } |
|
| 810 | + $result['status'] = intval($r[1]); |
|
| 811 | + while ($s = trim(fgets($handle, 16384))) { |
|
| 812 | + $result['headers'][] = $s . "\n"; |
|
| 813 | + preg_match(',^([^:]*): *(.*)$,i', $s, $r); |
|
| 814 | + [, $d, $v] = $r; |
|
| 815 | + if (strtolower(trim($d)) == 'location' and $result['status'] >= 300 and $result['status'] < 400) { |
|
| 816 | + $result['location'] = $v; |
|
| 817 | + } elseif ($d == 'Last-Modified') { |
|
| 818 | + $result['last_modified'] = strtotime($v); |
|
| 819 | + } |
|
| 820 | + } |
|
| 821 | + if ( |
|
| 822 | + $if_modified_since |
|
| 823 | + and $result['last_modified'] |
|
| 824 | + and $if_modified_since > $result['last_modified'] |
|
| 825 | + and $result['status'] == 200 |
|
| 826 | + ) { |
|
| 827 | + $result['status'] = 304; |
|
| 828 | + } |
|
| 829 | + |
|
| 830 | + $result['headers'] = implode('', $result['headers']); |
|
| 831 | + |
|
| 832 | + return $result; |
|
| 833 | 833 | } |
| 834 | 834 | |
| 835 | 835 | /** |
@@ -851,22 +851,22 @@ discard block |
||
| 851 | 851 | * Nom du fichier pour copie locale |
| 852 | 852 | **/ |
| 853 | 853 | function nom_fichier_copie_locale($source, $extension) { |
| 854 | - include_spip('inc/documents'); |
|
| 854 | + include_spip('inc/documents'); |
|
| 855 | 855 | |
| 856 | - $d = creer_repertoire_documents('distant'); # IMG/distant/ |
|
| 857 | - $d = sous_repertoire($d, $extension); # IMG/distant/pdf/ |
|
| 856 | + $d = creer_repertoire_documents('distant'); # IMG/distant/ |
|
| 857 | + $d = sous_repertoire($d, $extension); # IMG/distant/pdf/ |
|
| 858 | 858 | |
| 859 | - // on se place tout le temps comme si on etait a la racine |
|
| 860 | - if (_DIR_RACINE) { |
|
| 861 | - $d = preg_replace(',^' . preg_quote(_DIR_RACINE) . ',', '', $d); |
|
| 862 | - } |
|
| 859 | + // on se place tout le temps comme si on etait a la racine |
|
| 860 | + if (_DIR_RACINE) { |
|
| 861 | + $d = preg_replace(',^' . preg_quote(_DIR_RACINE) . ',', '', $d); |
|
| 862 | + } |
|
| 863 | 863 | |
| 864 | - $m = md5($source); |
|
| 864 | + $m = md5($source); |
|
| 865 | 865 | |
| 866 | - return $d |
|
| 867 | - . substr(preg_replace(',[^\w-],', '', basename($source)) . '-' . $m, 0, 12) |
|
| 868 | - . substr($m, 0, 4) |
|
| 869 | - . ".$extension"; |
|
| 866 | + return $d |
|
| 867 | + . substr(preg_replace(',[^\w-],', '', basename($source)) . '-' . $m, 0, 12) |
|
| 868 | + . substr($m, 0, 4) |
|
| 869 | + . ".$extension"; |
|
| 870 | 870 | } |
| 871 | 871 | |
| 872 | 872 | /** |
@@ -885,72 +885,72 @@ discard block |
||
| 885 | 885 | * - null: Copie locale impossible |
| 886 | 886 | **/ |
| 887 | 887 | function fichier_copie_locale($source) { |
| 888 | - // Si c'est deja local pas de souci |
|
| 889 | - if (!tester_url_absolue($source)) { |
|
| 890 | - if (_DIR_RACINE) { |
|
| 891 | - $source = preg_replace(',^' . preg_quote(_DIR_RACINE) . ',', '', $source); |
|
| 892 | - } |
|
| 893 | - |
|
| 894 | - return $source; |
|
| 895 | - } |
|
| 896 | - |
|
| 897 | - // optimisation : on regarde si on peut deviner l'extension dans l'url et si le fichier |
|
| 898 | - // a deja ete copie en local avec cette extension |
|
| 899 | - // dans ce cas elle est fiable, pas la peine de requeter en base |
|
| 900 | - $path_parts = pathinfo($source); |
|
| 901 | - if (!isset($path_parts['extension'])) { |
|
| 902 | - $path_parts['extension'] = ''; |
|
| 903 | - } |
|
| 904 | - $ext = $path_parts ? $path_parts['extension'] : ''; |
|
| 905 | - if ( |
|
| 906 | - $ext |
|
| 907 | - and preg_match(',^\w+$,', $ext) // pas de php?truc=1&... |
|
| 908 | - and $f = nom_fichier_copie_locale($source, $ext) |
|
| 909 | - and file_exists(_DIR_RACINE . $f) |
|
| 910 | - ) { |
|
| 911 | - return $f; |
|
| 912 | - } |
|
| 913 | - |
|
| 914 | - |
|
| 915 | - // Si c'est deja dans la table des documents, |
|
| 916 | - // ramener le nom de sa copie potentielle |
|
| 917 | - $ext = sql_getfetsel('extension', 'spip_documents', 'fichier=' . sql_quote($source) . " AND distant='oui' AND extension <> ''"); |
|
| 918 | - |
|
| 919 | - if ($ext) { |
|
| 920 | - return nom_fichier_copie_locale($source, $ext); |
|
| 921 | - } |
|
| 922 | - |
|
| 923 | - // voir si l'extension indiquee dans le nom du fichier est ok |
|
| 924 | - // et si il n'aurait pas deja ete rapatrie |
|
| 925 | - |
|
| 926 | - $ext = $path_parts ? $path_parts['extension'] : ''; |
|
| 927 | - |
|
| 928 | - if ($ext and sql_getfetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($ext))) { |
|
| 929 | - $f = nom_fichier_copie_locale($source, $ext); |
|
| 930 | - if (file_exists(_DIR_RACINE . $f)) { |
|
| 931 | - return $f; |
|
| 932 | - } |
|
| 933 | - } |
|
| 934 | - |
|
| 935 | - // Ping pour voir si son extension est connue et autorisee |
|
| 936 | - // avec mise en cache du resultat du ping |
|
| 937 | - |
|
| 938 | - $cache = sous_repertoire(_DIR_CACHE, 'rid') . md5($source); |
|
| 939 | - if ( |
|
| 940 | - !@file_exists($cache) |
|
| 941 | - or !$path_parts = @unserialize(spip_file_get_contents($cache)) |
|
| 942 | - or _request('var_mode') === 'recalcul' |
|
| 943 | - ) { |
|
| 944 | - $path_parts = recuperer_infos_distantes($source, ['charger_si_petite_image' => false]); |
|
| 945 | - ecrire_fichier($cache, serialize($path_parts)); |
|
| 946 | - } |
|
| 947 | - $ext = !empty($path_parts['extension']) ? $path_parts['extension'] : ''; |
|
| 948 | - if ($ext and sql_getfetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($ext))) { |
|
| 949 | - return nom_fichier_copie_locale($source, $ext); |
|
| 950 | - } |
|
| 951 | - |
|
| 952 | - spip_log("pas de copie locale pour $source", 'distant' . _LOG_ERREUR); |
|
| 953 | - return null; |
|
| 888 | + // Si c'est deja local pas de souci |
|
| 889 | + if (!tester_url_absolue($source)) { |
|
| 890 | + if (_DIR_RACINE) { |
|
| 891 | + $source = preg_replace(',^' . preg_quote(_DIR_RACINE) . ',', '', $source); |
|
| 892 | + } |
|
| 893 | + |
|
| 894 | + return $source; |
|
| 895 | + } |
|
| 896 | + |
|
| 897 | + // optimisation : on regarde si on peut deviner l'extension dans l'url et si le fichier |
|
| 898 | + // a deja ete copie en local avec cette extension |
|
| 899 | + // dans ce cas elle est fiable, pas la peine de requeter en base |
|
| 900 | + $path_parts = pathinfo($source); |
|
| 901 | + if (!isset($path_parts['extension'])) { |
|
| 902 | + $path_parts['extension'] = ''; |
|
| 903 | + } |
|
| 904 | + $ext = $path_parts ? $path_parts['extension'] : ''; |
|
| 905 | + if ( |
|
| 906 | + $ext |
|
| 907 | + and preg_match(',^\w+$,', $ext) // pas de php?truc=1&... |
|
| 908 | + and $f = nom_fichier_copie_locale($source, $ext) |
|
| 909 | + and file_exists(_DIR_RACINE . $f) |
|
| 910 | + ) { |
|
| 911 | + return $f; |
|
| 912 | + } |
|
| 913 | + |
|
| 914 | + |
|
| 915 | + // Si c'est deja dans la table des documents, |
|
| 916 | + // ramener le nom de sa copie potentielle |
|
| 917 | + $ext = sql_getfetsel('extension', 'spip_documents', 'fichier=' . sql_quote($source) . " AND distant='oui' AND extension <> ''"); |
|
| 918 | + |
|
| 919 | + if ($ext) { |
|
| 920 | + return nom_fichier_copie_locale($source, $ext); |
|
| 921 | + } |
|
| 922 | + |
|
| 923 | + // voir si l'extension indiquee dans le nom du fichier est ok |
|
| 924 | + // et si il n'aurait pas deja ete rapatrie |
|
| 925 | + |
|
| 926 | + $ext = $path_parts ? $path_parts['extension'] : ''; |
|
| 927 | + |
|
| 928 | + if ($ext and sql_getfetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($ext))) { |
|
| 929 | + $f = nom_fichier_copie_locale($source, $ext); |
|
| 930 | + if (file_exists(_DIR_RACINE . $f)) { |
|
| 931 | + return $f; |
|
| 932 | + } |
|
| 933 | + } |
|
| 934 | + |
|
| 935 | + // Ping pour voir si son extension est connue et autorisee |
|
| 936 | + // avec mise en cache du resultat du ping |
|
| 937 | + |
|
| 938 | + $cache = sous_repertoire(_DIR_CACHE, 'rid') . md5($source); |
|
| 939 | + if ( |
|
| 940 | + !@file_exists($cache) |
|
| 941 | + or !$path_parts = @unserialize(spip_file_get_contents($cache)) |
|
| 942 | + or _request('var_mode') === 'recalcul' |
|
| 943 | + ) { |
|
| 944 | + $path_parts = recuperer_infos_distantes($source, ['charger_si_petite_image' => false]); |
|
| 945 | + ecrire_fichier($cache, serialize($path_parts)); |
|
| 946 | + } |
|
| 947 | + $ext = !empty($path_parts['extension']) ? $path_parts['extension'] : ''; |
|
| 948 | + if ($ext and sql_getfetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($ext))) { |
|
| 949 | + return nom_fichier_copie_locale($source, $ext); |
|
| 950 | + } |
|
| 951 | + |
|
| 952 | + spip_log("pas de copie locale pour $source", 'distant' . _LOG_ERREUR); |
|
| 953 | + return null; |
|
| 954 | 954 | } |
| 955 | 955 | |
| 956 | 956 | |
@@ -979,110 +979,110 @@ discard block |
||
| 979 | 979 | **/ |
| 980 | 980 | function recuperer_infos_distantes($source, $options = []) { |
| 981 | 981 | |
| 982 | - // pas la peine de perdre son temps |
|
| 983 | - if (!tester_url_absolue($source)) { |
|
| 984 | - return false; |
|
| 985 | - } |
|
| 986 | - |
|
| 987 | - $taille_max = $options['taille_max'] ?? 0; |
|
| 988 | - $charger_si_petite_image = !!($options['charger_si_petite_image'] ?? true); |
|
| 989 | - $callback_valider_url = $options['callback_valider_url'] ?? null; |
|
| 990 | - |
|
| 991 | - # charger les alias des types mime |
|
| 992 | - include_spip('base/typedoc'); |
|
| 993 | - |
|
| 994 | - $a = []; |
|
| 995 | - $mime_type = ''; |
|
| 996 | - // On va directement charger le debut des images et des fichiers html, |
|
| 997 | - // de maniere a attrapper le maximum d'infos (titre, taille, etc). Si |
|
| 998 | - // ca echoue l'utilisateur devra les entrer... |
|
| 999 | - $reponse = recuperer_url($source, ['taille_max' => $taille_max, 'refuser_gz' => true]); |
|
| 1000 | - if ( |
|
| 1001 | - $callback_valider_url |
|
| 1002 | - and is_callable($callback_valider_url) |
|
| 1003 | - and !$callback_valider_url($reponse['url']) |
|
| 1004 | - ) { |
|
| 1005 | - return false; |
|
| 1006 | - } |
|
| 1007 | - $headers = $reponse['headers'] ?? ''; |
|
| 1008 | - $a['body'] = $reponse['page'] ?? ''; |
|
| 1009 | - if ($headers) { |
|
| 1010 | - if (!$extension = distant_trouver_extension_selon_headers($source, $headers)) { |
|
| 1011 | - return false; |
|
| 1012 | - } |
|
| 1013 | - |
|
| 1014 | - $a['extension'] = $extension; |
|
| 1015 | - |
|
| 1016 | - if (preg_match(",\nContent-Length: *([^[:space:]]*),i", "\n$headers", $regs)) { |
|
| 1017 | - $a['taille'] = intval($regs[1]); |
|
| 1018 | - } |
|
| 1019 | - } |
|
| 1020 | - |
|
| 1021 | - // Echec avec HEAD, on tente avec GET |
|
| 1022 | - if (!$a and !$taille_max) { |
|
| 1023 | - spip_log("tenter GET $source", 'distant'); |
|
| 1024 | - $options['taille_max'] = _INC_DISTANT_MAX_SIZE; |
|
| 1025 | - $a = recuperer_infos_distantes($source, $options); |
|
| 1026 | - } |
|
| 1027 | - |
|
| 1028 | - // si on a rien trouve pas la peine d'insister |
|
| 1029 | - if (!$a) { |
|
| 1030 | - return false; |
|
| 1031 | - } |
|
| 1032 | - |
|
| 1033 | - // S'il s'agit d'une image pas trop grosse ou d'un fichier html, on va aller |
|
| 1034 | - // recharger le document en GET et recuperer des donnees supplementaires... |
|
| 1035 | - include_spip('inc/filtres_images_lib_mini'); |
|
| 1036 | - if ( |
|
| 1037 | - strpos($mime_type, 'image/') === 0 |
|
| 1038 | - and $extension = _image_trouver_extension_depuis_mime($mime_type) |
|
| 1039 | - ) { |
|
| 1040 | - if ( |
|
| 1041 | - $taille_max == 0 |
|
| 1042 | - and (empty($a['taille']) or $a['taille'] < _INC_DISTANT_MAX_SIZE) |
|
| 1043 | - and in_array($extension, formats_image_acceptables()) |
|
| 1044 | - and $charger_si_petite_image |
|
| 1045 | - ) { |
|
| 1046 | - $options['taille_max'] = _INC_DISTANT_MAX_SIZE; |
|
| 1047 | - $a = recuperer_infos_distantes($source, $options); |
|
| 1048 | - } else { |
|
| 1049 | - if ($a['body']) { |
|
| 1050 | - $a['extension'] = $extension; |
|
| 1051 | - $a['fichier'] = _DIR_RACINE . nom_fichier_copie_locale($source, $extension); |
|
| 1052 | - ecrire_fichier($a['fichier'], $a['body']); |
|
| 1053 | - $size_image = @spip_getimagesize($a['fichier']); |
|
| 1054 | - $a['largeur'] = intval($size_image[0]); |
|
| 1055 | - $a['hauteur'] = intval($size_image[1]); |
|
| 1056 | - $a['type_image'] = true; |
|
| 1057 | - } |
|
| 1058 | - } |
|
| 1059 | - } |
|
| 1060 | - |
|
| 1061 | - // Fichier swf, si on n'a pas la taille, on va mettre 425x350 par defaut |
|
| 1062 | - // ce sera mieux que 0x0 |
|
| 1063 | - // Flash is dead! |
|
| 1064 | - if ( |
|
| 1065 | - $a and isset($a['extension']) and $a['extension'] == 'swf' |
|
| 1066 | - and empty($a['largeur']) |
|
| 1067 | - ) { |
|
| 1068 | - $a['largeur'] = 425; |
|
| 1069 | - $a['hauteur'] = 350; |
|
| 1070 | - } |
|
| 1071 | - |
|
| 1072 | - if ($mime_type == 'text/html') { |
|
| 1073 | - include_spip('inc/filtres'); |
|
| 1074 | - $page = recuperer_url($source, ['transcoder' => true, 'taille_max' => _INC_DISTANT_MAX_SIZE]); |
|
| 1075 | - $page = $page['page'] ?? ''; |
|
| 1076 | - if (preg_match(',<title>(.*?)</title>,ims', $page, $regs)) { |
|
| 1077 | - $a['titre'] = corriger_caracteres(trim($regs[1])); |
|
| 1078 | - } |
|
| 1079 | - if (!isset($a['taille']) or !$a['taille']) { |
|
| 1080 | - $a['taille'] = strlen($page); # a peu pres |
|
| 1081 | - } |
|
| 1082 | - } |
|
| 1083 | - $a['mime_type'] = $mime_type; |
|
| 1084 | - |
|
| 1085 | - return $a; |
|
| 982 | + // pas la peine de perdre son temps |
|
| 983 | + if (!tester_url_absolue($source)) { |
|
| 984 | + return false; |
|
| 985 | + } |
|
| 986 | + |
|
| 987 | + $taille_max = $options['taille_max'] ?? 0; |
|
| 988 | + $charger_si_petite_image = !!($options['charger_si_petite_image'] ?? true); |
|
| 989 | + $callback_valider_url = $options['callback_valider_url'] ?? null; |
|
| 990 | + |
|
| 991 | + # charger les alias des types mime |
|
| 992 | + include_spip('base/typedoc'); |
|
| 993 | + |
|
| 994 | + $a = []; |
|
| 995 | + $mime_type = ''; |
|
| 996 | + // On va directement charger le debut des images et des fichiers html, |
|
| 997 | + // de maniere a attrapper le maximum d'infos (titre, taille, etc). Si |
|
| 998 | + // ca echoue l'utilisateur devra les entrer... |
|
| 999 | + $reponse = recuperer_url($source, ['taille_max' => $taille_max, 'refuser_gz' => true]); |
|
| 1000 | + if ( |
|
| 1001 | + $callback_valider_url |
|
| 1002 | + and is_callable($callback_valider_url) |
|
| 1003 | + and !$callback_valider_url($reponse['url']) |
|
| 1004 | + ) { |
|
| 1005 | + return false; |
|
| 1006 | + } |
|
| 1007 | + $headers = $reponse['headers'] ?? ''; |
|
| 1008 | + $a['body'] = $reponse['page'] ?? ''; |
|
| 1009 | + if ($headers) { |
|
| 1010 | + if (!$extension = distant_trouver_extension_selon_headers($source, $headers)) { |
|
| 1011 | + return false; |
|
| 1012 | + } |
|
| 1013 | + |
|
| 1014 | + $a['extension'] = $extension; |
|
| 1015 | + |
|
| 1016 | + if (preg_match(",\nContent-Length: *([^[:space:]]*),i", "\n$headers", $regs)) { |
|
| 1017 | + $a['taille'] = intval($regs[1]); |
|
| 1018 | + } |
|
| 1019 | + } |
|
| 1020 | + |
|
| 1021 | + // Echec avec HEAD, on tente avec GET |
|
| 1022 | + if (!$a and !$taille_max) { |
|
| 1023 | + spip_log("tenter GET $source", 'distant'); |
|
| 1024 | + $options['taille_max'] = _INC_DISTANT_MAX_SIZE; |
|
| 1025 | + $a = recuperer_infos_distantes($source, $options); |
|
| 1026 | + } |
|
| 1027 | + |
|
| 1028 | + // si on a rien trouve pas la peine d'insister |
|
| 1029 | + if (!$a) { |
|
| 1030 | + return false; |
|
| 1031 | + } |
|
| 1032 | + |
|
| 1033 | + // S'il s'agit d'une image pas trop grosse ou d'un fichier html, on va aller |
|
| 1034 | + // recharger le document en GET et recuperer des donnees supplementaires... |
|
| 1035 | + include_spip('inc/filtres_images_lib_mini'); |
|
| 1036 | + if ( |
|
| 1037 | + strpos($mime_type, 'image/') === 0 |
|
| 1038 | + and $extension = _image_trouver_extension_depuis_mime($mime_type) |
|
| 1039 | + ) { |
|
| 1040 | + if ( |
|
| 1041 | + $taille_max == 0 |
|
| 1042 | + and (empty($a['taille']) or $a['taille'] < _INC_DISTANT_MAX_SIZE) |
|
| 1043 | + and in_array($extension, formats_image_acceptables()) |
|
| 1044 | + and $charger_si_petite_image |
|
| 1045 | + ) { |
|
| 1046 | + $options['taille_max'] = _INC_DISTANT_MAX_SIZE; |
|
| 1047 | + $a = recuperer_infos_distantes($source, $options); |
|
| 1048 | + } else { |
|
| 1049 | + if ($a['body']) { |
|
| 1050 | + $a['extension'] = $extension; |
|
| 1051 | + $a['fichier'] = _DIR_RACINE . nom_fichier_copie_locale($source, $extension); |
|
| 1052 | + ecrire_fichier($a['fichier'], $a['body']); |
|
| 1053 | + $size_image = @spip_getimagesize($a['fichier']); |
|
| 1054 | + $a['largeur'] = intval($size_image[0]); |
|
| 1055 | + $a['hauteur'] = intval($size_image[1]); |
|
| 1056 | + $a['type_image'] = true; |
|
| 1057 | + } |
|
| 1058 | + } |
|
| 1059 | + } |
|
| 1060 | + |
|
| 1061 | + // Fichier swf, si on n'a pas la taille, on va mettre 425x350 par defaut |
|
| 1062 | + // ce sera mieux que 0x0 |
|
| 1063 | + // Flash is dead! |
|
| 1064 | + if ( |
|
| 1065 | + $a and isset($a['extension']) and $a['extension'] == 'swf' |
|
| 1066 | + and empty($a['largeur']) |
|
| 1067 | + ) { |
|
| 1068 | + $a['largeur'] = 425; |
|
| 1069 | + $a['hauteur'] = 350; |
|
| 1070 | + } |
|
| 1071 | + |
|
| 1072 | + if ($mime_type == 'text/html') { |
|
| 1073 | + include_spip('inc/filtres'); |
|
| 1074 | + $page = recuperer_url($source, ['transcoder' => true, 'taille_max' => _INC_DISTANT_MAX_SIZE]); |
|
| 1075 | + $page = $page['page'] ?? ''; |
|
| 1076 | + if (preg_match(',<title>(.*?)</title>,ims', $page, $regs)) { |
|
| 1077 | + $a['titre'] = corriger_caracteres(trim($regs[1])); |
|
| 1078 | + } |
|
| 1079 | + if (!isset($a['taille']) or !$a['taille']) { |
|
| 1080 | + $a['taille'] = strlen($page); # a peu pres |
|
| 1081 | + } |
|
| 1082 | + } |
|
| 1083 | + $a['mime_type'] = $mime_type; |
|
| 1084 | + |
|
| 1085 | + return $a; |
|
| 1086 | 1086 | } |
| 1087 | 1087 | |
| 1088 | 1088 | /** |
@@ -1091,70 +1091,70 @@ discard block |
||
| 1091 | 1091 | * @return false|mixed |
| 1092 | 1092 | */ |
| 1093 | 1093 | function distant_trouver_extension_selon_headers($source, $headers) { |
| 1094 | - if (preg_match(",\nContent-Type: *([^[:space:];]*),i", "\n$headers", $regs)) { |
|
| 1095 | - $mime_type = (trim($regs[1])); |
|
| 1096 | - } else { |
|
| 1097 | - $mime_type = ''; |
|
| 1098 | - } // inconnu |
|
| 1099 | - |
|
| 1100 | - // Appliquer les alias |
|
| 1101 | - while (isset($GLOBALS['mime_alias'][$mime_type])) { |
|
| 1102 | - $mime_type = $GLOBALS['mime_alias'][$mime_type]; |
|
| 1103 | - } |
|
| 1104 | - |
|
| 1105 | - // pour corriger_extension() |
|
| 1106 | - include_spip('inc/documents'); |
|
| 1107 | - |
|
| 1108 | - // Si on a un mime-type insignifiant |
|
| 1109 | - // text/plain,application/octet-stream ou vide |
|
| 1110 | - // c'est peut-etre que le serveur ne sait pas |
|
| 1111 | - // ce qu'il sert ; on va tenter de detecter via l'extension de l'url |
|
| 1112 | - // ou le Content-Disposition: attachment; filename=... |
|
| 1113 | - $t = null; |
|
| 1114 | - if (in_array($mime_type, ['text/plain', '', 'application/octet-stream'])) { |
|
| 1115 | - if ( |
|
| 1116 | - !$t |
|
| 1117 | - and preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $source, $rext) |
|
| 1118 | - ) { |
|
| 1119 | - $t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote(corriger_extension($rext[1]), '', 'text')); |
|
| 1120 | - } |
|
| 1121 | - if ( |
|
| 1122 | - !$t |
|
| 1123 | - and preg_match(',^Content-Disposition:\s*attachment;\s*filename=(.*)$,Uims', $headers, $m) |
|
| 1124 | - and preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $m[1], $rext) |
|
| 1125 | - ) { |
|
| 1126 | - $t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote(corriger_extension($rext[1]), '', 'text')); |
|
| 1127 | - } |
|
| 1128 | - } |
|
| 1129 | - |
|
| 1130 | - // Autre mime/type (ou text/plain avec fichier d'extension inconnue) |
|
| 1131 | - if (!$t) { |
|
| 1132 | - $t = sql_fetsel('extension', 'spip_types_documents', 'mime_type=' . sql_quote($mime_type)); |
|
| 1133 | - } |
|
| 1134 | - |
|
| 1135 | - // Toujours rien ? (ex: audio/x-ogg au lieu de application/ogg) |
|
| 1136 | - // On essaie de nouveau avec l'extension |
|
| 1137 | - if ( |
|
| 1138 | - !$t |
|
| 1139 | - and $mime_type != 'text/plain' |
|
| 1140 | - and preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $source, $rext) |
|
| 1141 | - ) { |
|
| 1142 | - # eviter xxx.3 => 3gp (> SPIP 3) |
|
| 1143 | - $t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote(corriger_extension($rext[1]), '', 'text')); |
|
| 1144 | - } |
|
| 1145 | - |
|
| 1146 | - if ($t) { |
|
| 1147 | - spip_log("mime-type $mime_type ok, extension " . $t['extension'], 'distant'); |
|
| 1148 | - return $t['extension']; |
|
| 1149 | - } else { |
|
| 1150 | - # par defaut on retombe sur '.bin' si c'est autorise |
|
| 1151 | - spip_log("mime-type $mime_type inconnu", 'distant'); |
|
| 1152 | - $t = sql_fetsel('extension', 'spip_types_documents', "extension='bin'"); |
|
| 1153 | - if (!$t) { |
|
| 1154 | - return false; |
|
| 1155 | - } |
|
| 1156 | - return $t['extension']; |
|
| 1157 | - } |
|
| 1094 | + if (preg_match(",\nContent-Type: *([^[:space:];]*),i", "\n$headers", $regs)) { |
|
| 1095 | + $mime_type = (trim($regs[1])); |
|
| 1096 | + } else { |
|
| 1097 | + $mime_type = ''; |
|
| 1098 | + } // inconnu |
|
| 1099 | + |
|
| 1100 | + // Appliquer les alias |
|
| 1101 | + while (isset($GLOBALS['mime_alias'][$mime_type])) { |
|
| 1102 | + $mime_type = $GLOBALS['mime_alias'][$mime_type]; |
|
| 1103 | + } |
|
| 1104 | + |
|
| 1105 | + // pour corriger_extension() |
|
| 1106 | + include_spip('inc/documents'); |
|
| 1107 | + |
|
| 1108 | + // Si on a un mime-type insignifiant |
|
| 1109 | + // text/plain,application/octet-stream ou vide |
|
| 1110 | + // c'est peut-etre que le serveur ne sait pas |
|
| 1111 | + // ce qu'il sert ; on va tenter de detecter via l'extension de l'url |
|
| 1112 | + // ou le Content-Disposition: attachment; filename=... |
|
| 1113 | + $t = null; |
|
| 1114 | + if (in_array($mime_type, ['text/plain', '', 'application/octet-stream'])) { |
|
| 1115 | + if ( |
|
| 1116 | + !$t |
|
| 1117 | + and preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $source, $rext) |
|
| 1118 | + ) { |
|
| 1119 | + $t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote(corriger_extension($rext[1]), '', 'text')); |
|
| 1120 | + } |
|
| 1121 | + if ( |
|
| 1122 | + !$t |
|
| 1123 | + and preg_match(',^Content-Disposition:\s*attachment;\s*filename=(.*)$,Uims', $headers, $m) |
|
| 1124 | + and preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $m[1], $rext) |
|
| 1125 | + ) { |
|
| 1126 | + $t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote(corriger_extension($rext[1]), '', 'text')); |
|
| 1127 | + } |
|
| 1128 | + } |
|
| 1129 | + |
|
| 1130 | + // Autre mime/type (ou text/plain avec fichier d'extension inconnue) |
|
| 1131 | + if (!$t) { |
|
| 1132 | + $t = sql_fetsel('extension', 'spip_types_documents', 'mime_type=' . sql_quote($mime_type)); |
|
| 1133 | + } |
|
| 1134 | + |
|
| 1135 | + // Toujours rien ? (ex: audio/x-ogg au lieu de application/ogg) |
|
| 1136 | + // On essaie de nouveau avec l'extension |
|
| 1137 | + if ( |
|
| 1138 | + !$t |
|
| 1139 | + and $mime_type != 'text/plain' |
|
| 1140 | + and preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $source, $rext) |
|
| 1141 | + ) { |
|
| 1142 | + # eviter xxx.3 => 3gp (> SPIP 3) |
|
| 1143 | + $t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote(corriger_extension($rext[1]), '', 'text')); |
|
| 1144 | + } |
|
| 1145 | + |
|
| 1146 | + if ($t) { |
|
| 1147 | + spip_log("mime-type $mime_type ok, extension " . $t['extension'], 'distant'); |
|
| 1148 | + return $t['extension']; |
|
| 1149 | + } else { |
|
| 1150 | + # par defaut on retombe sur '.bin' si c'est autorise |
|
| 1151 | + spip_log("mime-type $mime_type inconnu", 'distant'); |
|
| 1152 | + $t = sql_fetsel('extension', 'spip_types_documents', "extension='bin'"); |
|
| 1153 | + if (!$t) { |
|
| 1154 | + return false; |
|
| 1155 | + } |
|
| 1156 | + return $t['extension']; |
|
| 1157 | + } |
|
| 1158 | 1158 | } |
| 1159 | 1159 | |
| 1160 | 1160 | /** |
@@ -1170,45 +1170,45 @@ discard block |
||
| 1170 | 1170 | */ |
| 1171 | 1171 | function need_proxy($host, $http_proxy = null, $http_noproxy = null) { |
| 1172 | 1172 | |
| 1173 | - $http_proxy ??= $GLOBALS['meta']['http_proxy'] ?? null; |
|
| 1174 | - |
|
| 1175 | - // rien a faire si pas de proxy :) |
|
| 1176 | - if (is_null($http_proxy) or !$http_proxy = trim($http_proxy)) { |
|
| 1177 | - return ''; |
|
| 1178 | - } |
|
| 1179 | - |
|
| 1180 | - if (is_null($http_noproxy)) { |
|
| 1181 | - $http_noproxy = $GLOBALS['meta']['http_noproxy'] ?? null; |
|
| 1182 | - } |
|
| 1183 | - // si pas d'exception, on retourne le proxy |
|
| 1184 | - if (is_null($http_noproxy) or !$http_noproxy = trim($http_noproxy)) { |
|
| 1185 | - return $http_proxy; |
|
| 1186 | - } |
|
| 1187 | - |
|
| 1188 | - // si le host ou l'un des domaines parents est dans $http_noproxy on fait exception |
|
| 1189 | - // $http_noproxy peut contenir plusieurs domaines separes par des espaces ou retour ligne |
|
| 1190 | - $http_noproxy = str_replace("\n", ' ', $http_noproxy); |
|
| 1191 | - $http_noproxy = str_replace("\r", ' ', $http_noproxy); |
|
| 1192 | - $http_noproxy = " $http_noproxy "; |
|
| 1193 | - $domain = $host; |
|
| 1194 | - // si le domaine exact www.example.org est dans les exceptions |
|
| 1195 | - if (strpos($http_noproxy, (string) " $domain ") !== false) { |
|
| 1196 | - return ''; |
|
| 1197 | - } |
|
| 1198 | - |
|
| 1199 | - while (strpos($domain, '.') !== false) { |
|
| 1200 | - $domain = explode('.', $domain); |
|
| 1201 | - array_shift($domain); |
|
| 1202 | - $domain = implode('.', $domain); |
|
| 1203 | - |
|
| 1204 | - // ou si un domaine parent commencant par un . est dans les exceptions (indiquant qu'il couvre tous les sous-domaines) |
|
| 1205 | - if (strpos($http_noproxy, (string) " .$domain ") !== false) { |
|
| 1206 | - return ''; |
|
| 1207 | - } |
|
| 1208 | - } |
|
| 1209 | - |
|
| 1210 | - // ok c'est pas une exception |
|
| 1211 | - return $http_proxy; |
|
| 1173 | + $http_proxy ??= $GLOBALS['meta']['http_proxy'] ?? null; |
|
| 1174 | + |
|
| 1175 | + // rien a faire si pas de proxy :) |
|
| 1176 | + if (is_null($http_proxy) or !$http_proxy = trim($http_proxy)) { |
|
| 1177 | + return ''; |
|
| 1178 | + } |
|
| 1179 | + |
|
| 1180 | + if (is_null($http_noproxy)) { |
|
| 1181 | + $http_noproxy = $GLOBALS['meta']['http_noproxy'] ?? null; |
|
| 1182 | + } |
|
| 1183 | + // si pas d'exception, on retourne le proxy |
|
| 1184 | + if (is_null($http_noproxy) or !$http_noproxy = trim($http_noproxy)) { |
|
| 1185 | + return $http_proxy; |
|
| 1186 | + } |
|
| 1187 | + |
|
| 1188 | + // si le host ou l'un des domaines parents est dans $http_noproxy on fait exception |
|
| 1189 | + // $http_noproxy peut contenir plusieurs domaines separes par des espaces ou retour ligne |
|
| 1190 | + $http_noproxy = str_replace("\n", ' ', $http_noproxy); |
|
| 1191 | + $http_noproxy = str_replace("\r", ' ', $http_noproxy); |
|
| 1192 | + $http_noproxy = " $http_noproxy "; |
|
| 1193 | + $domain = $host; |
|
| 1194 | + // si le domaine exact www.example.org est dans les exceptions |
|
| 1195 | + if (strpos($http_noproxy, (string) " $domain ") !== false) { |
|
| 1196 | + return ''; |
|
| 1197 | + } |
|
| 1198 | + |
|
| 1199 | + while (strpos($domain, '.') !== false) { |
|
| 1200 | + $domain = explode('.', $domain); |
|
| 1201 | + array_shift($domain); |
|
| 1202 | + $domain = implode('.', $domain); |
|
| 1203 | + |
|
| 1204 | + // ou si un domaine parent commencant par un . est dans les exceptions (indiquant qu'il couvre tous les sous-domaines) |
|
| 1205 | + if (strpos($http_noproxy, (string) " .$domain ") !== false) { |
|
| 1206 | + return ''; |
|
| 1207 | + } |
|
| 1208 | + } |
|
| 1209 | + |
|
| 1210 | + // ok c'est pas une exception |
|
| 1211 | + return $http_proxy; |
|
| 1212 | 1212 | } |
| 1213 | 1213 | |
| 1214 | 1214 | |
@@ -1231,59 +1231,59 @@ discard block |
||
| 1231 | 1231 | * @return array |
| 1232 | 1232 | */ |
| 1233 | 1233 | function init_http($method, $url, $refuse_gz = false, $referer = '', $datas = '', $vers = 'HTTP/1.0', $date = '') { |
| 1234 | - $user = $via_proxy = $proxy_user = ''; |
|
| 1235 | - $fopen = false; |
|
| 1236 | - |
|
| 1237 | - $t = @parse_url($url); |
|
| 1238 | - $host = $t['host']; |
|
| 1239 | - if ($t['scheme'] == 'http') { |
|
| 1240 | - $scheme = 'http'; |
|
| 1241 | - $noproxy = ''; |
|
| 1242 | - } elseif ($t['scheme'] == 'https') { |
|
| 1243 | - $scheme = 'ssl'; |
|
| 1244 | - $noproxy = 'ssl://'; |
|
| 1245 | - if (!isset($t['port']) || !($port = $t['port'])) { |
|
| 1246 | - $t['port'] = 443; |
|
| 1247 | - } |
|
| 1248 | - } else { |
|
| 1249 | - $scheme = $t['scheme']; |
|
| 1250 | - $noproxy = $scheme . '://'; |
|
| 1251 | - } |
|
| 1252 | - if (isset($t['user'])) { |
|
| 1253 | - $user = [$t['user'], $t['pass']]; |
|
| 1254 | - } |
|
| 1255 | - |
|
| 1256 | - if (!isset($t['port']) || !($port = $t['port'])) { |
|
| 1257 | - $port = 80; |
|
| 1258 | - } |
|
| 1259 | - if (!isset($t['path']) || !($path = $t['path'])) { |
|
| 1260 | - $path = '/'; |
|
| 1261 | - } |
|
| 1262 | - |
|
| 1263 | - if (!empty($t['query'])) { |
|
| 1264 | - $path .= '?' . $t['query']; |
|
| 1265 | - } |
|
| 1266 | - |
|
| 1267 | - $f = lance_requete($method, $scheme, $user, $host, $path, $port, $noproxy, $refuse_gz, $referer, $datas, $vers, $date); |
|
| 1268 | - if (!$f or !is_resource($f)) { |
|
| 1269 | - // fallback : fopen si on a pas fait timeout dans lance_requete |
|
| 1270 | - // ce qui correspond a $f===110 |
|
| 1271 | - if ( |
|
| 1272 | - $f !== 110 |
|
| 1273 | - and !need_proxy($host) |
|
| 1274 | - and !_request('tester_proxy') |
|
| 1275 | - and (!isset($GLOBALS['inc_distant_allow_fopen']) or $GLOBALS['inc_distant_allow_fopen']) |
|
| 1276 | - ) { |
|
| 1277 | - $f = @fopen($url, 'rb'); |
|
| 1278 | - spip_log("connexion vers $url par simple fopen", 'distant'); |
|
| 1279 | - $fopen = true; |
|
| 1280 | - } else { |
|
| 1281 | - // echec total |
|
| 1282 | - $f = false; |
|
| 1283 | - } |
|
| 1284 | - } |
|
| 1285 | - |
|
| 1286 | - return [$f, $fopen]; |
|
| 1234 | + $user = $via_proxy = $proxy_user = ''; |
|
| 1235 | + $fopen = false; |
|
| 1236 | + |
|
| 1237 | + $t = @parse_url($url); |
|
| 1238 | + $host = $t['host']; |
|
| 1239 | + if ($t['scheme'] == 'http') { |
|
| 1240 | + $scheme = 'http'; |
|
| 1241 | + $noproxy = ''; |
|
| 1242 | + } elseif ($t['scheme'] == 'https') { |
|
| 1243 | + $scheme = 'ssl'; |
|
| 1244 | + $noproxy = 'ssl://'; |
|
| 1245 | + if (!isset($t['port']) || !($port = $t['port'])) { |
|
| 1246 | + $t['port'] = 443; |
|
| 1247 | + } |
|
| 1248 | + } else { |
|
| 1249 | + $scheme = $t['scheme']; |
|
| 1250 | + $noproxy = $scheme . '://'; |
|
| 1251 | + } |
|
| 1252 | + if (isset($t['user'])) { |
|
| 1253 | + $user = [$t['user'], $t['pass']]; |
|
| 1254 | + } |
|
| 1255 | + |
|
| 1256 | + if (!isset($t['port']) || !($port = $t['port'])) { |
|
| 1257 | + $port = 80; |
|
| 1258 | + } |
|
| 1259 | + if (!isset($t['path']) || !($path = $t['path'])) { |
|
| 1260 | + $path = '/'; |
|
| 1261 | + } |
|
| 1262 | + |
|
| 1263 | + if (!empty($t['query'])) { |
|
| 1264 | + $path .= '?' . $t['query']; |
|
| 1265 | + } |
|
| 1266 | + |
|
| 1267 | + $f = lance_requete($method, $scheme, $user, $host, $path, $port, $noproxy, $refuse_gz, $referer, $datas, $vers, $date); |
|
| 1268 | + if (!$f or !is_resource($f)) { |
|
| 1269 | + // fallback : fopen si on a pas fait timeout dans lance_requete |
|
| 1270 | + // ce qui correspond a $f===110 |
|
| 1271 | + if ( |
|
| 1272 | + $f !== 110 |
|
| 1273 | + and !need_proxy($host) |
|
| 1274 | + and !_request('tester_proxy') |
|
| 1275 | + and (!isset($GLOBALS['inc_distant_allow_fopen']) or $GLOBALS['inc_distant_allow_fopen']) |
|
| 1276 | + ) { |
|
| 1277 | + $f = @fopen($url, 'rb'); |
|
| 1278 | + spip_log("connexion vers $url par simple fopen", 'distant'); |
|
| 1279 | + $fopen = true; |
|
| 1280 | + } else { |
|
| 1281 | + // echec total |
|
| 1282 | + $f = false; |
|
| 1283 | + } |
|
| 1284 | + } |
|
| 1285 | + |
|
| 1286 | + return [$f, $fopen]; |
|
| 1287 | 1287 | } |
| 1288 | 1288 | |
| 1289 | 1289 | /** |
@@ -1318,123 +1318,123 @@ discard block |
||
| 1318 | 1318 | * resource socket vers l'url demandee |
| 1319 | 1319 | */ |
| 1320 | 1320 | function lance_requete( |
| 1321 | - $method, |
|
| 1322 | - $scheme, |
|
| 1323 | - $user, |
|
| 1324 | - $host, |
|
| 1325 | - $path, |
|
| 1326 | - $port, |
|
| 1327 | - $noproxy, |
|
| 1328 | - $refuse_gz = false, |
|
| 1329 | - $referer = '', |
|
| 1330 | - $datas = '', |
|
| 1331 | - $vers = 'HTTP/1.0', |
|
| 1332 | - $date = '' |
|
| 1321 | + $method, |
|
| 1322 | + $scheme, |
|
| 1323 | + $user, |
|
| 1324 | + $host, |
|
| 1325 | + $path, |
|
| 1326 | + $port, |
|
| 1327 | + $noproxy, |
|
| 1328 | + $refuse_gz = false, |
|
| 1329 | + $referer = '', |
|
| 1330 | + $datas = '', |
|
| 1331 | + $vers = 'HTTP/1.0', |
|
| 1332 | + $date = '' |
|
| 1333 | 1333 | ) { |
| 1334 | 1334 | |
| 1335 | - $proxy_user = ''; |
|
| 1336 | - $http_proxy = need_proxy($host); |
|
| 1337 | - if ($user) { |
|
| 1338 | - $user = urlencode($user[0]) . ':' . urlencode($user[1]); |
|
| 1339 | - } |
|
| 1340 | - |
|
| 1341 | - $connect = ''; |
|
| 1342 | - if ($http_proxy) { |
|
| 1343 | - if (!defined('_PROXY_HTTPS_NOT_VIA_CONNECT') and in_array($scheme, ['tls','ssl'])) { |
|
| 1344 | - $path_host = (!$user ? '' : "$user@") . $host . (($port != 80) ? ":$port" : ''); |
|
| 1345 | - $connect = 'CONNECT ' . $path_host . " $vers\r\n" |
|
| 1346 | - . "Host: $path_host\r\n" |
|
| 1347 | - . "Proxy-Connection: Keep-Alive\r\n"; |
|
| 1348 | - } else { |
|
| 1349 | - $path = (in_array($scheme, ['tls','ssl']) ? 'https://' : "$scheme://") |
|
| 1350 | - . (!$user ? '' : "$user@") |
|
| 1351 | - . "$host" . (($port != 80) ? ":$port" : '') . $path; |
|
| 1352 | - } |
|
| 1353 | - $t2 = @parse_url($http_proxy); |
|
| 1354 | - $first_host = $t2['host']; |
|
| 1355 | - $port = ($t2['port'] ?? null) ?: 80; |
|
| 1356 | - if ($t2['user'] ?? null) { |
|
| 1357 | - $proxy_user = base64_encode($t2['user'] . ':' . $t2['pass']); |
|
| 1358 | - } |
|
| 1359 | - } else { |
|
| 1360 | - $first_host = $noproxy . $host; |
|
| 1361 | - } |
|
| 1362 | - |
|
| 1363 | - if ($connect) { |
|
| 1364 | - $streamContext = stream_context_create([ |
|
| 1365 | - 'ssl' => [ |
|
| 1366 | - 'verify_peer' => false, |
|
| 1367 | - 'allow_self_signed' => true, |
|
| 1368 | - 'SNI_enabled' => true, |
|
| 1369 | - 'peer_name' => $host, |
|
| 1370 | - ] |
|
| 1371 | - ]); |
|
| 1372 | - $f = @stream_socket_client( |
|
| 1373 | - "tcp://$first_host:$port", |
|
| 1374 | - $errno, |
|
| 1375 | - $errstr, |
|
| 1376 | - _INC_DISTANT_CONNECT_TIMEOUT, |
|
| 1377 | - STREAM_CLIENT_CONNECT, |
|
| 1378 | - $streamContext |
|
| 1379 | - ); |
|
| 1380 | - spip_log("Recuperer $path sur $first_host:$port par $f (via CONNECT)", 'connect'); |
|
| 1381 | - if (!$f) { |
|
| 1382 | - spip_log("Erreur connexion $errno $errstr", 'distant' . _LOG_ERREUR); |
|
| 1383 | - return $errno; |
|
| 1384 | - } |
|
| 1385 | - stream_set_timeout($f, _INC_DISTANT_CONNECT_TIMEOUT); |
|
| 1386 | - |
|
| 1387 | - fputs($f, $connect); |
|
| 1388 | - fputs($f, "\r\n"); |
|
| 1389 | - $res = fread($f, 1024); |
|
| 1390 | - if ( |
|
| 1391 | - !$res |
|
| 1392 | - or !count($res = explode(' ', $res)) |
|
| 1393 | - or $res[1] !== '200' |
|
| 1394 | - ) { |
|
| 1395 | - spip_log("Echec CONNECT sur $first_host:$port", 'connect' . _LOG_INFO_IMPORTANTE); |
|
| 1396 | - fclose($f); |
|
| 1397 | - |
|
| 1398 | - return false; |
|
| 1399 | - } |
|
| 1400 | - // important, car sinon on lit trop vite et les donnees ne sont pas encore dispo |
|
| 1401 | - stream_set_blocking($f, true); |
|
| 1402 | - // envoyer le handshake |
|
| 1403 | - stream_socket_enable_crypto($f, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT); |
|
| 1404 | - spip_log("OK CONNECT sur $first_host:$port", 'connect'); |
|
| 1405 | - } else { |
|
| 1406 | - $ntry = 3; |
|
| 1407 | - do { |
|
| 1408 | - $f = @fsockopen($first_host, $port, $errno, $errstr, _INC_DISTANT_CONNECT_TIMEOUT); |
|
| 1409 | - } while (!$f and $ntry-- and $errno !== 110 and sleep(1)); |
|
| 1410 | - spip_log("Recuperer $path sur $first_host:$port par $f"); |
|
| 1411 | - if (!$f) { |
|
| 1412 | - spip_log("Erreur connexion $errno $errstr", 'distant' . _LOG_ERREUR); |
|
| 1413 | - |
|
| 1414 | - return $errno; |
|
| 1415 | - } |
|
| 1416 | - stream_set_timeout($f, _INC_DISTANT_CONNECT_TIMEOUT); |
|
| 1417 | - } |
|
| 1418 | - |
|
| 1419 | - $site = $GLOBALS['meta']['adresse_site'] ?? ''; |
|
| 1420 | - |
|
| 1421 | - $host_port = $host; |
|
| 1422 | - if ($port != (in_array($scheme, ['tls','ssl']) ? 443 : 80)) { |
|
| 1423 | - $host_port .= ":$port"; |
|
| 1424 | - } |
|
| 1425 | - $req = "$method $path $vers\r\n" |
|
| 1426 | - . "Host: $host_port\r\n" |
|
| 1427 | - . 'User-Agent: ' . _INC_DISTANT_USER_AGENT . "\r\n" |
|
| 1428 | - . ($refuse_gz ? '' : ('Accept-Encoding: ' . _INC_DISTANT_CONTENT_ENCODING . "\r\n")) |
|
| 1429 | - . (!$site ? '' : "Referer: $site/$referer\r\n") |
|
| 1430 | - . (!$date ? '' : 'If-Modified-Since: ' . (gmdate('D, d M Y H:i:s', $date) . " GMT\r\n")) |
|
| 1431 | - . (!$user ? '' : ('Authorization: Basic ' . base64_encode($user) . "\r\n")) |
|
| 1432 | - . (!$proxy_user ? '' : "Proxy-Authorization: Basic $proxy_user\r\n") |
|
| 1433 | - . (!strpos($vers, '1.1') ? '' : "Keep-Alive: 300\r\nConnection: keep-alive\r\n"); |
|
| 1335 | + $proxy_user = ''; |
|
| 1336 | + $http_proxy = need_proxy($host); |
|
| 1337 | + if ($user) { |
|
| 1338 | + $user = urlencode($user[0]) . ':' . urlencode($user[1]); |
|
| 1339 | + } |
|
| 1340 | + |
|
| 1341 | + $connect = ''; |
|
| 1342 | + if ($http_proxy) { |
|
| 1343 | + if (!defined('_PROXY_HTTPS_NOT_VIA_CONNECT') and in_array($scheme, ['tls','ssl'])) { |
|
| 1344 | + $path_host = (!$user ? '' : "$user@") . $host . (($port != 80) ? ":$port" : ''); |
|
| 1345 | + $connect = 'CONNECT ' . $path_host . " $vers\r\n" |
|
| 1346 | + . "Host: $path_host\r\n" |
|
| 1347 | + . "Proxy-Connection: Keep-Alive\r\n"; |
|
| 1348 | + } else { |
|
| 1349 | + $path = (in_array($scheme, ['tls','ssl']) ? 'https://' : "$scheme://") |
|
| 1350 | + . (!$user ? '' : "$user@") |
|
| 1351 | + . "$host" . (($port != 80) ? ":$port" : '') . $path; |
|
| 1352 | + } |
|
| 1353 | + $t2 = @parse_url($http_proxy); |
|
| 1354 | + $first_host = $t2['host']; |
|
| 1355 | + $port = ($t2['port'] ?? null) ?: 80; |
|
| 1356 | + if ($t2['user'] ?? null) { |
|
| 1357 | + $proxy_user = base64_encode($t2['user'] . ':' . $t2['pass']); |
|
| 1358 | + } |
|
| 1359 | + } else { |
|
| 1360 | + $first_host = $noproxy . $host; |
|
| 1361 | + } |
|
| 1362 | + |
|
| 1363 | + if ($connect) { |
|
| 1364 | + $streamContext = stream_context_create([ |
|
| 1365 | + 'ssl' => [ |
|
| 1366 | + 'verify_peer' => false, |
|
| 1367 | + 'allow_self_signed' => true, |
|
| 1368 | + 'SNI_enabled' => true, |
|
| 1369 | + 'peer_name' => $host, |
|
| 1370 | + ] |
|
| 1371 | + ]); |
|
| 1372 | + $f = @stream_socket_client( |
|
| 1373 | + "tcp://$first_host:$port", |
|
| 1374 | + $errno, |
|
| 1375 | + $errstr, |
|
| 1376 | + _INC_DISTANT_CONNECT_TIMEOUT, |
|
| 1377 | + STREAM_CLIENT_CONNECT, |
|
| 1378 | + $streamContext |
|
| 1379 | + ); |
|
| 1380 | + spip_log("Recuperer $path sur $first_host:$port par $f (via CONNECT)", 'connect'); |
|
| 1381 | + if (!$f) { |
|
| 1382 | + spip_log("Erreur connexion $errno $errstr", 'distant' . _LOG_ERREUR); |
|
| 1383 | + return $errno; |
|
| 1384 | + } |
|
| 1385 | + stream_set_timeout($f, _INC_DISTANT_CONNECT_TIMEOUT); |
|
| 1386 | + |
|
| 1387 | + fputs($f, $connect); |
|
| 1388 | + fputs($f, "\r\n"); |
|
| 1389 | + $res = fread($f, 1024); |
|
| 1390 | + if ( |
|
| 1391 | + !$res |
|
| 1392 | + or !count($res = explode(' ', $res)) |
|
| 1393 | + or $res[1] !== '200' |
|
| 1394 | + ) { |
|
| 1395 | + spip_log("Echec CONNECT sur $first_host:$port", 'connect' . _LOG_INFO_IMPORTANTE); |
|
| 1396 | + fclose($f); |
|
| 1397 | + |
|
| 1398 | + return false; |
|
| 1399 | + } |
|
| 1400 | + // important, car sinon on lit trop vite et les donnees ne sont pas encore dispo |
|
| 1401 | + stream_set_blocking($f, true); |
|
| 1402 | + // envoyer le handshake |
|
| 1403 | + stream_socket_enable_crypto($f, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT); |
|
| 1404 | + spip_log("OK CONNECT sur $first_host:$port", 'connect'); |
|
| 1405 | + } else { |
|
| 1406 | + $ntry = 3; |
|
| 1407 | + do { |
|
| 1408 | + $f = @fsockopen($first_host, $port, $errno, $errstr, _INC_DISTANT_CONNECT_TIMEOUT); |
|
| 1409 | + } while (!$f and $ntry-- and $errno !== 110 and sleep(1)); |
|
| 1410 | + spip_log("Recuperer $path sur $first_host:$port par $f"); |
|
| 1411 | + if (!$f) { |
|
| 1412 | + spip_log("Erreur connexion $errno $errstr", 'distant' . _LOG_ERREUR); |
|
| 1413 | + |
|
| 1414 | + return $errno; |
|
| 1415 | + } |
|
| 1416 | + stream_set_timeout($f, _INC_DISTANT_CONNECT_TIMEOUT); |
|
| 1417 | + } |
|
| 1418 | + |
|
| 1419 | + $site = $GLOBALS['meta']['adresse_site'] ?? ''; |
|
| 1420 | + |
|
| 1421 | + $host_port = $host; |
|
| 1422 | + if ($port != (in_array($scheme, ['tls','ssl']) ? 443 : 80)) { |
|
| 1423 | + $host_port .= ":$port"; |
|
| 1424 | + } |
|
| 1425 | + $req = "$method $path $vers\r\n" |
|
| 1426 | + . "Host: $host_port\r\n" |
|
| 1427 | + . 'User-Agent: ' . _INC_DISTANT_USER_AGENT . "\r\n" |
|
| 1428 | + . ($refuse_gz ? '' : ('Accept-Encoding: ' . _INC_DISTANT_CONTENT_ENCODING . "\r\n")) |
|
| 1429 | + . (!$site ? '' : "Referer: $site/$referer\r\n") |
|
| 1430 | + . (!$date ? '' : 'If-Modified-Since: ' . (gmdate('D, d M Y H:i:s', $date) . " GMT\r\n")) |
|
| 1431 | + . (!$user ? '' : ('Authorization: Basic ' . base64_encode($user) . "\r\n")) |
|
| 1432 | + . (!$proxy_user ? '' : "Proxy-Authorization: Basic $proxy_user\r\n") |
|
| 1433 | + . (!strpos($vers, '1.1') ? '' : "Keep-Alive: 300\r\nConnection: keep-alive\r\n"); |
|
| 1434 | 1434 | |
| 1435 | 1435 | # spip_log("Requete\n$req", 'distant'); |
| 1436 | - fputs($f, $req); |
|
| 1437 | - fputs($f, $datas ?: "\r\n"); |
|
| 1436 | + fputs($f, $req); |
|
| 1437 | + fputs($f, $datas ?: "\r\n"); |
|
| 1438 | 1438 | |
| 1439 | - return $f; |
|
| 1439 | + return $f; |
|
| 1440 | 1440 | } |
@@ -28,7 +28,7 @@ discard block |
||
| 28 | 28 | define('_INC_DISTANT_CONTENT_ENCODING', 'gzip'); |
| 29 | 29 | } |
| 30 | 30 | if (!defined('_INC_DISTANT_USER_AGENT')) { |
| 31 | - define('_INC_DISTANT_USER_AGENT', 'SPIP-' . $GLOBALS['spip_version_affichee'] . ' (' . $GLOBALS['home_server'] . ')'); |
|
| 31 | + define('_INC_DISTANT_USER_AGENT', 'SPIP-'.$GLOBALS['spip_version_affichee'].' ('.$GLOBALS['home_server'].')'); |
|
| 32 | 32 | } |
| 33 | 33 | if (!defined('_INC_DISTANT_MAX_SIZE')) { |
| 34 | 34 | define('_INC_DISTANT_MAX_SIZE', 2_097_152); |
@@ -37,7 +37,7 @@ discard block |
||
| 37 | 37 | define('_INC_DISTANT_CONNECT_TIMEOUT', 10); |
| 38 | 38 | } |
| 39 | 39 | |
| 40 | -define('_REGEXP_COPIE_LOCALE', ',' . |
|
| 40 | +define('_REGEXP_COPIE_LOCALE', ','. |
|
| 41 | 41 | preg_replace( |
| 42 | 42 | '@^https?:@', |
| 43 | 43 | 'https?:', |
@@ -74,7 +74,7 @@ discard block |
||
| 74 | 74 | |
| 75 | 75 | // si c'est la protection de soi-meme, retourner le path |
| 76 | 76 | if ($mode !== 'force' and preg_match(_REGEXP_COPIE_LOCALE, $source, $match)) { |
| 77 | - $source = substr(_DIR_IMG, strlen(_DIR_RACINE)) . urldecode($match[1]); |
|
| 77 | + $source = substr(_DIR_IMG, strlen(_DIR_RACINE)).urldecode($match[1]); |
|
| 78 | 78 | |
| 79 | 79 | return @file_exists($source) ? $source : false; |
| 80 | 80 | } |
@@ -94,7 +94,7 @@ discard block |
||
| 94 | 94 | return false; |
| 95 | 95 | } |
| 96 | 96 | |
| 97 | - $localrac = _DIR_RACINE . $local; |
|
| 97 | + $localrac = _DIR_RACINE.$local; |
|
| 98 | 98 | $t = ($mode === 'force') ? false : @file_exists($localrac); |
| 99 | 99 | |
| 100 | 100 | // test d'existence du fichier |
@@ -114,18 +114,18 @@ discard block |
||
| 114 | 114 | if (!$taille_max) { |
| 115 | 115 | $taille_max = _COPIE_LOCALE_MAX_SIZE; |
| 116 | 116 | } |
| 117 | - $localrac_tmp = $localrac . '.tmp'; |
|
| 117 | + $localrac_tmp = $localrac.'.tmp'; |
|
| 118 | 118 | $res = recuperer_url( |
| 119 | 119 | $source, |
| 120 | 120 | ['file' => $localrac_tmp, 'taille_max' => $taille_max, 'if_modified_since' => $t ? filemtime($localrac) : ''] |
| 121 | 121 | ); |
| 122 | 122 | |
| 123 | 123 | if (!$res or (!$res['length'] and $res['status'] != 304)) { |
| 124 | - spip_log("copie_locale : Echec recuperation $source sur $localrac_tmp status : " . ($res ? $res['status'] : '-'), 'distant' . _LOG_INFO_IMPORTANTE); |
|
| 124 | + spip_log("copie_locale : Echec recuperation $source sur $localrac_tmp status : ".($res ? $res['status'] : '-'), 'distant'._LOG_INFO_IMPORTANTE); |
|
| 125 | 125 | @unlink($localrac_tmp); |
| 126 | 126 | } |
| 127 | 127 | else { |
| 128 | - spip_log("copie_locale : recuperation $source sur $localrac_tmp OK | taille " . $res['length'] . ' status ' . $res['status'], 'distant'); |
|
| 128 | + spip_log("copie_locale : recuperation $source sur $localrac_tmp OK | taille ".$res['length'].' status '.$res['status'], 'distant'); |
|
| 129 | 129 | } |
| 130 | 130 | if (!$res or !$res['length']) { |
| 131 | 131 | // si $t c'est sans doute juste un not-modified-since |
@@ -138,7 +138,7 @@ discard block |
||
| 138 | 138 | and is_callable($callback_valider_url) |
| 139 | 139 | and !$callback_valider_url($res['url']) |
| 140 | 140 | ) { |
| 141 | - spip_log('copie_locale : url finale ' . $res['url'] . " non valide, on refuse le fichier $localrac_tmp", 'distant' . _LOG_INFO_IMPORTANTE); |
|
| 141 | + spip_log('copie_locale : url finale '.$res['url']." non valide, on refuse le fichier $localrac_tmp", 'distant'._LOG_INFO_IMPORTANTE); |
|
| 142 | 142 | @unlink($localrac_tmp); |
| 143 | 143 | return $t ? $local : false; |
| 144 | 144 | } |
@@ -229,7 +229,7 @@ discard block |
||
| 229 | 229 | |
| 230 | 230 | if (!$is_known_host) { |
| 231 | 231 | $host = trim($parsed_url['host'], '.'); |
| 232 | - if (! $ip = filter_var($host, FILTER_VALIDATE_IP)) { |
|
| 232 | + if (!$ip = filter_var($host, FILTER_VALIDATE_IP)) { |
|
| 233 | 233 | $ip = gethostbyname($host); |
| 234 | 234 | if ($ip === $host) { |
| 235 | 235 | // Error condition for gethostbyname() |
@@ -250,7 +250,7 @@ discard block |
||
| 250 | 250 | } |
| 251 | 251 | } |
| 252 | 252 | if ($ip) { |
| 253 | - if (! filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) { |
|
| 253 | + if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) { |
|
| 254 | 254 | return false; |
| 255 | 255 | } |
| 256 | 256 | } |
@@ -261,7 +261,7 @@ discard block |
||
| 261 | 261 | } |
| 262 | 262 | |
| 263 | 263 | $port = $parsed_url['port']; |
| 264 | - if ($port === 80 or $port === 443 or $port === 8080) { |
|
| 264 | + if ($port === 80 or $port === 443 or $port === 8080) { |
|
| 265 | 265 | return $url; |
| 266 | 266 | } |
| 267 | 267 | |
@@ -331,7 +331,7 @@ discard block |
||
| 331 | 331 | } |
| 332 | 332 | } |
| 333 | 333 | if ($taille > 500) { |
| 334 | - $boundary = substr(md5(random_int(0, mt_getrandmax()) . 'spip'), 0, 8); |
|
| 334 | + $boundary = substr(md5(random_int(0, mt_getrandmax()).'spip'), 0, 8); |
|
| 335 | 335 | } |
| 336 | 336 | } |
| 337 | 337 | |
@@ -359,16 +359,16 @@ discard block |
||
| 359 | 359 | } |
| 360 | 360 | } else { |
| 361 | 361 | // fabrique une chaine HTTP simple pour un POST |
| 362 | - $entete = 'Content-Type: application/x-www-form-urlencoded' . "\r\n"; |
|
| 362 | + $entete = 'Content-Type: application/x-www-form-urlencoded'."\r\n"; |
|
| 363 | 363 | $chaine = []; |
| 364 | 364 | if (is_array($donnees)) { |
| 365 | 365 | foreach ($donnees as $cle => $valeur) { |
| 366 | 366 | if (is_array($valeur)) { |
| 367 | 367 | foreach ($valeur as $val2) { |
| 368 | - $chaine[] = rawurlencode($cle) . '[]=' . rawurlencode($val2); |
|
| 368 | + $chaine[] = rawurlencode($cle).'[]='.rawurlencode($val2); |
|
| 369 | 369 | } |
| 370 | 370 | } else { |
| 371 | - $chaine[] = rawurlencode($cle) . '=' . rawurlencode($valeur); |
|
| 371 | + $chaine[] = rawurlencode($cle).'='.rawurlencode($valeur); |
|
| 372 | 372 | } |
| 373 | 373 | } |
| 374 | 374 | $chaine = implode('&', $chaine); |
@@ -469,13 +469,13 @@ discard block |
||
| 469 | 469 | $options['taille_max'] = $copy ? _COPIE_LOCALE_MAX_SIZE : _INC_DISTANT_MAX_SIZE; |
| 470 | 470 | } |
| 471 | 471 | |
| 472 | - spip_log('recuperer_url ' . $options['methode'] . " sur $url", 'distant' . _LOG_DEBUG); |
|
| 472 | + spip_log('recuperer_url '.$options['methode']." sur $url", 'distant'._LOG_DEBUG); |
|
| 473 | 473 | |
| 474 | 474 | // Ajout des en-têtes spécifiques si besoin |
| 475 | 475 | $formatted_data = ''; |
| 476 | 476 | if (!empty($options['headers'])) { |
| 477 | 477 | foreach ($options['headers'] as $champ => $valeur) { |
| 478 | - $formatted_data .= $champ . ': ' . $valeur . "\r\n"; |
|
| 478 | + $formatted_data .= $champ.': '.$valeur."\r\n"; |
|
| 479 | 479 | } |
| 480 | 480 | } |
| 481 | 481 | |
@@ -483,9 +483,9 @@ discard block |
||
| 483 | 483 | [$head, $postdata] = prepare_donnees_post($options['datas'], $options['boundary']); |
| 484 | 484 | $head .= $formatted_data; |
| 485 | 485 | if (stripos($head, 'Content-Length:') === false) { |
| 486 | - $head .= 'Content-Length: ' . strlen($postdata) . "\r\n"; |
|
| 486 | + $head .= 'Content-Length: '.strlen($postdata)."\r\n"; |
|
| 487 | 487 | } |
| 488 | - $formatted_data = $head . "\r\n" . $postdata; |
|
| 488 | + $formatted_data = $head."\r\n".$postdata; |
|
| 489 | 489 | if ( |
| 490 | 490 | strlen($postdata) |
| 491 | 491 | and !$methode_demandee |
@@ -499,9 +499,9 @@ discard block |
||
| 499 | 499 | // Accepter les URLs au format feed:// ou qui ont oublie le http:// ou les urls relatives au protocole |
| 500 | 500 | $url = preg_replace(',^feed://,i', 'http://', $url); |
| 501 | 501 | if (!tester_url_absolue($url)) { |
| 502 | - $url = 'http://' . $url; |
|
| 502 | + $url = 'http://'.$url; |
|
| 503 | 503 | } elseif (strncmp($url, '//', 2) == 0) { |
| 504 | - $url = 'http:' . $url; |
|
| 504 | + $url = 'http:'.$url; |
|
| 505 | 505 | } |
| 506 | 506 | |
| 507 | 507 | $url = url_to_ascii($url); |
@@ -530,7 +530,7 @@ discard block |
||
| 530 | 530 | $options['if_modified_since'] |
| 531 | 531 | ); |
| 532 | 532 | if (!$handle) { |
| 533 | - spip_log("ECHEC init_http $url", 'distant' . _LOG_ERREUR); |
|
| 533 | + spip_log("ECHEC init_http $url", 'distant'._LOG_ERREUR); |
|
| 534 | 534 | |
| 535 | 535 | return false; |
| 536 | 536 | } |
@@ -560,7 +560,7 @@ discard block |
||
| 560 | 560 | 'status' => 200, |
| 561 | 561 | ]; |
| 562 | 562 | } else { |
| 563 | - spip_log("ECHEC chinoiserie $url", 'distant' . _LOG_ERREUR); |
|
| 563 | + spip_log("ECHEC chinoiserie $url", 'distant'._LOG_ERREUR); |
|
| 564 | 564 | return false; |
| 565 | 565 | } |
| 566 | 566 | } elseif ($res['location'] and $options['follow_location']) { |
@@ -576,11 +576,11 @@ discard block |
||
| 576 | 576 | $options['datas'] = ''; |
| 577 | 577 | } |
| 578 | 578 | } |
| 579 | - spip_log('recuperer_url recommence ' . $options['methode'] . " sur $url", 'distant' . _LOG_DEBUG); |
|
| 579 | + spip_log('recuperer_url recommence '.$options['methode']." sur $url", 'distant'._LOG_DEBUG); |
|
| 580 | 580 | |
| 581 | 581 | return recuperer_url($url, $options); |
| 582 | 582 | } elseif ($res['status'] !== 200) { |
| 583 | - spip_log('HTTP status ' . $res['status'] . " pour $url", 'distant'); |
|
| 583 | + spip_log('HTTP status '.$res['status']." pour $url", 'distant'); |
|
| 584 | 584 | } |
| 585 | 585 | $result['status'] = $res['status']; |
| 586 | 586 | if (isset($res['headers'])) { |
@@ -596,7 +596,7 @@ discard block |
||
| 596 | 596 | |
| 597 | 597 | // on ne veut que les entetes |
| 598 | 598 | if (!$options['taille_max'] or $options['methode'] == 'HEAD' or $result['status'] == '304') { |
| 599 | - spip_log('RESULTAT recuperer_url ' . $options['methode'] . " sur $url : " . json_encode($result), 'distant' . _LOG_DEBUG); |
|
| 599 | + spip_log('RESULTAT recuperer_url '.$options['methode']." sur $url : ".json_encode($result), 'distant'._LOG_DEBUG); |
|
| 600 | 600 | return $result; |
| 601 | 601 | } |
| 602 | 602 | |
@@ -606,7 +606,7 @@ discard block |
||
| 606 | 606 | |
| 607 | 607 | $gz = false; |
| 608 | 608 | if (preg_match(",\bContent-Encoding: .*gzip,is", $result['headers'])) { |
| 609 | - $gz = (_DIR_TMP . md5(uniqid(random_int(0, mt_getrandmax()))) . '.tmp.gz'); |
|
| 609 | + $gz = (_DIR_TMP.md5(uniqid(random_int(0, mt_getrandmax()))).'.tmp.gz'); |
|
| 610 | 610 | } |
| 611 | 611 | |
| 612 | 612 | // si on a pas deja recuperer le contenu par une methode detournee |
@@ -642,7 +642,7 @@ discard block |
||
| 642 | 642 | |
| 643 | 643 | $trace = json_decode(json_encode($result), true); |
| 644 | 644 | $trace['page'] = '...'; |
| 645 | - spip_log('RESULTAT recuperer_url ' . $options['methode'] . " sur $url : " . json_encode($trace), 'distant' . _LOG_DEBUG); |
|
| 645 | + spip_log('RESULTAT recuperer_url '.$options['methode']." sur $url : ".json_encode($trace), 'distant'._LOG_DEBUG); |
|
| 646 | 646 | |
| 647 | 647 | return $result; |
| 648 | 648 | } |
@@ -696,7 +696,7 @@ discard block |
||
| 696 | 696 | $sig['url'] = $url; |
| 697 | 697 | |
| 698 | 698 | $dir = sous_repertoire(_DIR_CACHE, 'curl'); |
| 699 | - $cache = md5(serialize($sig)) . '-' . substr(preg_replace(',\W+,', '_', $url), 0, 80); |
|
| 699 | + $cache = md5(serialize($sig)).'-'.substr(preg_replace(',\W+,', '_', $url), 0, 80); |
|
| 700 | 700 | $sub = sous_repertoire($dir, substr($cache, 0, 2)); |
| 701 | 701 | $cache = "$sub$cache"; |
| 702 | 702 | |
@@ -750,7 +750,7 @@ discard block |
||
| 750 | 750 | $fp = false; |
| 751 | 751 | if ($fichier) { |
| 752 | 752 | include_spip('inc/acces'); |
| 753 | - $tmpfile = "$fichier." . creer_uniqid() . '.tmp'; |
|
| 753 | + $tmpfile = "$fichier.".creer_uniqid().'.tmp'; |
|
| 754 | 754 | $fp = spip_fopen_lock($tmpfile, 'w', LOCK_EX); |
| 755 | 755 | if (!$fp and file_exists($fichier)) { |
| 756 | 756 | return filesize($fichier); |
@@ -809,7 +809,7 @@ discard block |
||
| 809 | 809 | } |
| 810 | 810 | $result['status'] = intval($r[1]); |
| 811 | 811 | while ($s = trim(fgets($handle, 16384))) { |
| 812 | - $result['headers'][] = $s . "\n"; |
|
| 812 | + $result['headers'][] = $s."\n"; |
|
| 813 | 813 | preg_match(',^([^:]*): *(.*)$,i', $s, $r); |
| 814 | 814 | [, $d, $v] = $r; |
| 815 | 815 | if (strtolower(trim($d)) == 'location' and $result['status'] >= 300 and $result['status'] < 400) { |
@@ -858,13 +858,13 @@ discard block |
||
| 858 | 858 | |
| 859 | 859 | // on se place tout le temps comme si on etait a la racine |
| 860 | 860 | if (_DIR_RACINE) { |
| 861 | - $d = preg_replace(',^' . preg_quote(_DIR_RACINE) . ',', '', $d); |
|
| 861 | + $d = preg_replace(',^'.preg_quote(_DIR_RACINE).',', '', $d); |
|
| 862 | 862 | } |
| 863 | 863 | |
| 864 | 864 | $m = md5($source); |
| 865 | 865 | |
| 866 | 866 | return $d |
| 867 | - . substr(preg_replace(',[^\w-],', '', basename($source)) . '-' . $m, 0, 12) |
|
| 867 | + . substr(preg_replace(',[^\w-],', '', basename($source)).'-'.$m, 0, 12) |
|
| 868 | 868 | . substr($m, 0, 4) |
| 869 | 869 | . ".$extension"; |
| 870 | 870 | } |
@@ -888,7 +888,7 @@ discard block |
||
| 888 | 888 | // Si c'est deja local pas de souci |
| 889 | 889 | if (!tester_url_absolue($source)) { |
| 890 | 890 | if (_DIR_RACINE) { |
| 891 | - $source = preg_replace(',^' . preg_quote(_DIR_RACINE) . ',', '', $source); |
|
| 891 | + $source = preg_replace(',^'.preg_quote(_DIR_RACINE).',', '', $source); |
|
| 892 | 892 | } |
| 893 | 893 | |
| 894 | 894 | return $source; |
@@ -906,7 +906,7 @@ discard block |
||
| 906 | 906 | $ext |
| 907 | 907 | and preg_match(',^\w+$,', $ext) // pas de php?truc=1&... |
| 908 | 908 | and $f = nom_fichier_copie_locale($source, $ext) |
| 909 | - and file_exists(_DIR_RACINE . $f) |
|
| 909 | + and file_exists(_DIR_RACINE.$f) |
|
| 910 | 910 | ) { |
| 911 | 911 | return $f; |
| 912 | 912 | } |
@@ -914,7 +914,7 @@ discard block |
||
| 914 | 914 | |
| 915 | 915 | // Si c'est deja dans la table des documents, |
| 916 | 916 | // ramener le nom de sa copie potentielle |
| 917 | - $ext = sql_getfetsel('extension', 'spip_documents', 'fichier=' . sql_quote($source) . " AND distant='oui' AND extension <> ''"); |
|
| 917 | + $ext = sql_getfetsel('extension', 'spip_documents', 'fichier='.sql_quote($source)." AND distant='oui' AND extension <> ''"); |
|
| 918 | 918 | |
| 919 | 919 | if ($ext) { |
| 920 | 920 | return nom_fichier_copie_locale($source, $ext); |
@@ -925,9 +925,9 @@ discard block |
||
| 925 | 925 | |
| 926 | 926 | $ext = $path_parts ? $path_parts['extension'] : ''; |
| 927 | 927 | |
| 928 | - if ($ext and sql_getfetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($ext))) { |
|
| 928 | + if ($ext and sql_getfetsel('extension', 'spip_types_documents', 'extension='.sql_quote($ext))) { |
|
| 929 | 929 | $f = nom_fichier_copie_locale($source, $ext); |
| 930 | - if (file_exists(_DIR_RACINE . $f)) { |
|
| 930 | + if (file_exists(_DIR_RACINE.$f)) { |
|
| 931 | 931 | return $f; |
| 932 | 932 | } |
| 933 | 933 | } |
@@ -935,7 +935,7 @@ discard block |
||
| 935 | 935 | // Ping pour voir si son extension est connue et autorisee |
| 936 | 936 | // avec mise en cache du resultat du ping |
| 937 | 937 | |
| 938 | - $cache = sous_repertoire(_DIR_CACHE, 'rid') . md5($source); |
|
| 938 | + $cache = sous_repertoire(_DIR_CACHE, 'rid').md5($source); |
|
| 939 | 939 | if ( |
| 940 | 940 | !@file_exists($cache) |
| 941 | 941 | or !$path_parts = @unserialize(spip_file_get_contents($cache)) |
@@ -945,11 +945,11 @@ discard block |
||
| 945 | 945 | ecrire_fichier($cache, serialize($path_parts)); |
| 946 | 946 | } |
| 947 | 947 | $ext = !empty($path_parts['extension']) ? $path_parts['extension'] : ''; |
| 948 | - if ($ext and sql_getfetsel('extension', 'spip_types_documents', 'extension=' . sql_quote($ext))) { |
|
| 948 | + if ($ext and sql_getfetsel('extension', 'spip_types_documents', 'extension='.sql_quote($ext))) { |
|
| 949 | 949 | return nom_fichier_copie_locale($source, $ext); |
| 950 | 950 | } |
| 951 | 951 | |
| 952 | - spip_log("pas de copie locale pour $source", 'distant' . _LOG_ERREUR); |
|
| 952 | + spip_log("pas de copie locale pour $source", 'distant'._LOG_ERREUR); |
|
| 953 | 953 | return null; |
| 954 | 954 | } |
| 955 | 955 | |
@@ -1048,7 +1048,7 @@ discard block |
||
| 1048 | 1048 | } else { |
| 1049 | 1049 | if ($a['body']) { |
| 1050 | 1050 | $a['extension'] = $extension; |
| 1051 | - $a['fichier'] = _DIR_RACINE . nom_fichier_copie_locale($source, $extension); |
|
| 1051 | + $a['fichier'] = _DIR_RACINE.nom_fichier_copie_locale($source, $extension); |
|
| 1052 | 1052 | ecrire_fichier($a['fichier'], $a['body']); |
| 1053 | 1053 | $size_image = @spip_getimagesize($a['fichier']); |
| 1054 | 1054 | $a['largeur'] = intval($size_image[0]); |
@@ -1116,20 +1116,20 @@ discard block |
||
| 1116 | 1116 | !$t |
| 1117 | 1117 | and preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $source, $rext) |
| 1118 | 1118 | ) { |
| 1119 | - $t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote(corriger_extension($rext[1]), '', 'text')); |
|
| 1119 | + $t = sql_fetsel('extension', 'spip_types_documents', 'extension='.sql_quote(corriger_extension($rext[1]), '', 'text')); |
|
| 1120 | 1120 | } |
| 1121 | 1121 | if ( |
| 1122 | 1122 | !$t |
| 1123 | 1123 | and preg_match(',^Content-Disposition:\s*attachment;\s*filename=(.*)$,Uims', $headers, $m) |
| 1124 | 1124 | and preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $m[1], $rext) |
| 1125 | 1125 | ) { |
| 1126 | - $t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote(corriger_extension($rext[1]), '', 'text')); |
|
| 1126 | + $t = sql_fetsel('extension', 'spip_types_documents', 'extension='.sql_quote(corriger_extension($rext[1]), '', 'text')); |
|
| 1127 | 1127 | } |
| 1128 | 1128 | } |
| 1129 | 1129 | |
| 1130 | 1130 | // Autre mime/type (ou text/plain avec fichier d'extension inconnue) |
| 1131 | 1131 | if (!$t) { |
| 1132 | - $t = sql_fetsel('extension', 'spip_types_documents', 'mime_type=' . sql_quote($mime_type)); |
|
| 1132 | + $t = sql_fetsel('extension', 'spip_types_documents', 'mime_type='.sql_quote($mime_type)); |
|
| 1133 | 1133 | } |
| 1134 | 1134 | |
| 1135 | 1135 | // Toujours rien ? (ex: audio/x-ogg au lieu de application/ogg) |
@@ -1140,11 +1140,11 @@ discard block |
||
| 1140 | 1140 | and preg_match(',\.([a-z0-9]+)(\?.*)?$,i', $source, $rext) |
| 1141 | 1141 | ) { |
| 1142 | 1142 | # eviter xxx.3 => 3gp (> SPIP 3) |
| 1143 | - $t = sql_fetsel('extension', 'spip_types_documents', 'extension=' . sql_quote(corriger_extension($rext[1]), '', 'text')); |
|
| 1143 | + $t = sql_fetsel('extension', 'spip_types_documents', 'extension='.sql_quote(corriger_extension($rext[1]), '', 'text')); |
|
| 1144 | 1144 | } |
| 1145 | 1145 | |
| 1146 | 1146 | if ($t) { |
| 1147 | - spip_log("mime-type $mime_type ok, extension " . $t['extension'], 'distant'); |
|
| 1147 | + spip_log("mime-type $mime_type ok, extension ".$t['extension'], 'distant'); |
|
| 1148 | 1148 | return $t['extension']; |
| 1149 | 1149 | } else { |
| 1150 | 1150 | # par defaut on retombe sur '.bin' si c'est autorise |
@@ -1247,7 +1247,7 @@ discard block |
||
| 1247 | 1247 | } |
| 1248 | 1248 | } else { |
| 1249 | 1249 | $scheme = $t['scheme']; |
| 1250 | - $noproxy = $scheme . '://'; |
|
| 1250 | + $noproxy = $scheme.'://'; |
|
| 1251 | 1251 | } |
| 1252 | 1252 | if (isset($t['user'])) { |
| 1253 | 1253 | $user = [$t['user'], $t['pass']]; |
@@ -1261,7 +1261,7 @@ discard block |
||
| 1261 | 1261 | } |
| 1262 | 1262 | |
| 1263 | 1263 | if (!empty($t['query'])) { |
| 1264 | - $path .= '?' . $t['query']; |
|
| 1264 | + $path .= '?'.$t['query']; |
|
| 1265 | 1265 | } |
| 1266 | 1266 | |
| 1267 | 1267 | $f = lance_requete($method, $scheme, $user, $host, $path, $port, $noproxy, $refuse_gz, $referer, $datas, $vers, $date); |
@@ -1335,29 +1335,29 @@ discard block |
||
| 1335 | 1335 | $proxy_user = ''; |
| 1336 | 1336 | $http_proxy = need_proxy($host); |
| 1337 | 1337 | if ($user) { |
| 1338 | - $user = urlencode($user[0]) . ':' . urlencode($user[1]); |
|
| 1338 | + $user = urlencode($user[0]).':'.urlencode($user[1]); |
|
| 1339 | 1339 | } |
| 1340 | 1340 | |
| 1341 | 1341 | $connect = ''; |
| 1342 | 1342 | if ($http_proxy) { |
| 1343 | - if (!defined('_PROXY_HTTPS_NOT_VIA_CONNECT') and in_array($scheme, ['tls','ssl'])) { |
|
| 1344 | - $path_host = (!$user ? '' : "$user@") . $host . (($port != 80) ? ":$port" : ''); |
|
| 1345 | - $connect = 'CONNECT ' . $path_host . " $vers\r\n" |
|
| 1343 | + if (!defined('_PROXY_HTTPS_NOT_VIA_CONNECT') and in_array($scheme, ['tls', 'ssl'])) { |
|
| 1344 | + $path_host = (!$user ? '' : "$user@").$host.(($port != 80) ? ":$port" : ''); |
|
| 1345 | + $connect = 'CONNECT '.$path_host." $vers\r\n" |
|
| 1346 | 1346 | . "Host: $path_host\r\n" |
| 1347 | 1347 | . "Proxy-Connection: Keep-Alive\r\n"; |
| 1348 | 1348 | } else { |
| 1349 | - $path = (in_array($scheme, ['tls','ssl']) ? 'https://' : "$scheme://") |
|
| 1349 | + $path = (in_array($scheme, ['tls', 'ssl']) ? 'https://' : "$scheme://") |
|
| 1350 | 1350 | . (!$user ? '' : "$user@") |
| 1351 | - . "$host" . (($port != 80) ? ":$port" : '') . $path; |
|
| 1351 | + . "$host".(($port != 80) ? ":$port" : '').$path; |
|
| 1352 | 1352 | } |
| 1353 | 1353 | $t2 = @parse_url($http_proxy); |
| 1354 | 1354 | $first_host = $t2['host']; |
| 1355 | 1355 | $port = ($t2['port'] ?? null) ?: 80; |
| 1356 | 1356 | if ($t2['user'] ?? null) { |
| 1357 | - $proxy_user = base64_encode($t2['user'] . ':' . $t2['pass']); |
|
| 1357 | + $proxy_user = base64_encode($t2['user'].':'.$t2['pass']); |
|
| 1358 | 1358 | } |
| 1359 | 1359 | } else { |
| 1360 | - $first_host = $noproxy . $host; |
|
| 1360 | + $first_host = $noproxy.$host; |
|
| 1361 | 1361 | } |
| 1362 | 1362 | |
| 1363 | 1363 | if ($connect) { |
@@ -1379,7 +1379,7 @@ discard block |
||
| 1379 | 1379 | ); |
| 1380 | 1380 | spip_log("Recuperer $path sur $first_host:$port par $f (via CONNECT)", 'connect'); |
| 1381 | 1381 | if (!$f) { |
| 1382 | - spip_log("Erreur connexion $errno $errstr", 'distant' . _LOG_ERREUR); |
|
| 1382 | + spip_log("Erreur connexion $errno $errstr", 'distant'._LOG_ERREUR); |
|
| 1383 | 1383 | return $errno; |
| 1384 | 1384 | } |
| 1385 | 1385 | stream_set_timeout($f, _INC_DISTANT_CONNECT_TIMEOUT); |
@@ -1392,7 +1392,7 @@ discard block |
||
| 1392 | 1392 | or !count($res = explode(' ', $res)) |
| 1393 | 1393 | or $res[1] !== '200' |
| 1394 | 1394 | ) { |
| 1395 | - spip_log("Echec CONNECT sur $first_host:$port", 'connect' . _LOG_INFO_IMPORTANTE); |
|
| 1395 | + spip_log("Echec CONNECT sur $first_host:$port", 'connect'._LOG_INFO_IMPORTANTE); |
|
| 1396 | 1396 | fclose($f); |
| 1397 | 1397 | |
| 1398 | 1398 | return false; |
@@ -1409,7 +1409,7 @@ discard block |
||
| 1409 | 1409 | } while (!$f and $ntry-- and $errno !== 110 and sleep(1)); |
| 1410 | 1410 | spip_log("Recuperer $path sur $first_host:$port par $f"); |
| 1411 | 1411 | if (!$f) { |
| 1412 | - spip_log("Erreur connexion $errno $errstr", 'distant' . _LOG_ERREUR); |
|
| 1412 | + spip_log("Erreur connexion $errno $errstr", 'distant'._LOG_ERREUR); |
|
| 1413 | 1413 | |
| 1414 | 1414 | return $errno; |
| 1415 | 1415 | } |
@@ -1419,16 +1419,16 @@ discard block |
||
| 1419 | 1419 | $site = $GLOBALS['meta']['adresse_site'] ?? ''; |
| 1420 | 1420 | |
| 1421 | 1421 | $host_port = $host; |
| 1422 | - if ($port != (in_array($scheme, ['tls','ssl']) ? 443 : 80)) { |
|
| 1422 | + if ($port != (in_array($scheme, ['tls', 'ssl']) ? 443 : 80)) { |
|
| 1423 | 1423 | $host_port .= ":$port"; |
| 1424 | 1424 | } |
| 1425 | 1425 | $req = "$method $path $vers\r\n" |
| 1426 | 1426 | . "Host: $host_port\r\n" |
| 1427 | - . 'User-Agent: ' . _INC_DISTANT_USER_AGENT . "\r\n" |
|
| 1428 | - . ($refuse_gz ? '' : ('Accept-Encoding: ' . _INC_DISTANT_CONTENT_ENCODING . "\r\n")) |
|
| 1427 | + . 'User-Agent: '._INC_DISTANT_USER_AGENT."\r\n" |
|
| 1428 | + . ($refuse_gz ? '' : ('Accept-Encoding: '._INC_DISTANT_CONTENT_ENCODING."\r\n")) |
|
| 1429 | 1429 | . (!$site ? '' : "Referer: $site/$referer\r\n") |
| 1430 | - . (!$date ? '' : 'If-Modified-Since: ' . (gmdate('D, d M Y H:i:s', $date) . " GMT\r\n")) |
|
| 1431 | - . (!$user ? '' : ('Authorization: Basic ' . base64_encode($user) . "\r\n")) |
|
| 1430 | + . (!$date ? '' : 'If-Modified-Since: '.(gmdate('D, d M Y H:i:s', $date)." GMT\r\n")) |
|
| 1431 | + . (!$user ? '' : ('Authorization: Basic '.base64_encode($user)."\r\n")) |
|
| 1432 | 1432 | . (!$proxy_user ? '' : "Proxy-Authorization: Basic $proxy_user\r\n") |
| 1433 | 1433 | . (!strpos($vers, '1.1') ? '' : "Keep-Alive: 300\r\nConnection: keep-alive\r\n"); |
| 1434 | 1434 | |