@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | **/ |
| 17 | 17 | |
| 18 | 18 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 19 | - return; |
|
| 19 | + return; |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | /** |
@@ -30,46 +30,46 @@ discard block |
||
| 30 | 30 | * Mot de passe |
| 31 | 31 | **/ |
| 32 | 32 | function creer_pass_aleatoire($longueur = 16, $sel = '') { |
| 33 | - $seed = (int)round(((float)microtime() + 1) * time()); |
|
| 34 | - |
|
| 35 | - mt_srand($seed); |
|
| 36 | - $s = ''; |
|
| 37 | - $pass = ''; |
|
| 38 | - for ($i = 0; $i < $longueur; $i++) { |
|
| 39 | - if (!$s) { |
|
| 40 | - $s = random_int(0, mt_getrandmax()); |
|
| 41 | - if (!$s) { |
|
| 42 | - $s = random_int(0, mt_getrandmax()); |
|
| 43 | - } |
|
| 44 | - $s = substr(md5(uniqid($s) . $sel), 0, 16); |
|
| 45 | - } |
|
| 46 | - $r = unpack('Cr', pack('H2', $s . $s)); |
|
| 47 | - $x = $r['r'] & 63; |
|
| 48 | - if ($x < 10) { |
|
| 49 | - $x = chr($x + 48); |
|
| 50 | - } else { |
|
| 51 | - if ($x < 36) { |
|
| 52 | - $x = chr($x + 55); |
|
| 53 | - } else { |
|
| 54 | - if ($x < 62) { |
|
| 55 | - $x = chr($x + 61); |
|
| 56 | - } else { |
|
| 57 | - if ($x == 63) { |
|
| 58 | - $x = '/'; |
|
| 59 | - } else { |
|
| 60 | - $x = '.'; |
|
| 61 | - } |
|
| 62 | - } |
|
| 63 | - } |
|
| 64 | - } |
|
| 65 | - $pass .= $x; |
|
| 66 | - $s = substr($s, 2); |
|
| 67 | - } |
|
| 68 | - $pass = preg_replace('@[./]@', 'a', $pass); |
|
| 69 | - $pass = preg_replace('@[I1l]@', 'L', $pass); |
|
| 70 | - $pass = preg_replace('@[0O]@', 'o', $pass); |
|
| 71 | - |
|
| 72 | - return $pass; |
|
| 33 | + $seed = (int)round(((float)microtime() + 1) * time()); |
|
| 34 | + |
|
| 35 | + mt_srand($seed); |
|
| 36 | + $s = ''; |
|
| 37 | + $pass = ''; |
|
| 38 | + for ($i = 0; $i < $longueur; $i++) { |
|
| 39 | + if (!$s) { |
|
| 40 | + $s = random_int(0, mt_getrandmax()); |
|
| 41 | + if (!$s) { |
|
| 42 | + $s = random_int(0, mt_getrandmax()); |
|
| 43 | + } |
|
| 44 | + $s = substr(md5(uniqid($s) . $sel), 0, 16); |
|
| 45 | + } |
|
| 46 | + $r = unpack('Cr', pack('H2', $s . $s)); |
|
| 47 | + $x = $r['r'] & 63; |
|
| 48 | + if ($x < 10) { |
|
| 49 | + $x = chr($x + 48); |
|
| 50 | + } else { |
|
| 51 | + if ($x < 36) { |
|
| 52 | + $x = chr($x + 55); |
|
| 53 | + } else { |
|
| 54 | + if ($x < 62) { |
|
| 55 | + $x = chr($x + 61); |
|
| 56 | + } else { |
|
| 57 | + if ($x == 63) { |
|
| 58 | + $x = '/'; |
|
| 59 | + } else { |
|
| 60 | + $x = '.'; |
|
| 61 | + } |
|
| 62 | + } |
|
| 63 | + } |
|
| 64 | + } |
|
| 65 | + $pass .= $x; |
|
| 66 | + $s = substr($s, 2); |
|
| 67 | + } |
|
| 68 | + $pass = preg_replace('@[./]@', 'a', $pass); |
|
| 69 | + $pass = preg_replace('@[I1l]@', 'L', $pass); |
|
| 70 | + $pass = preg_replace('@[0O]@', 'o', $pass); |
|
| 71 | + |
|
| 72 | + return $pass; |
|
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | /** |
@@ -78,20 +78,20 @@ discard block |
||
| 78 | 78 | * @return string Identifiant |
| 79 | 79 | */ |
| 80 | 80 | function creer_uniqid() { |
| 81 | - static $seeded; |
|
| 81 | + static $seeded; |
|
| 82 | 82 | |
| 83 | - if (!$seeded) { |
|
| 84 | - $seed = (int)round(((float)microtime() + 1) * time()); |
|
| 85 | - mt_srand($seed); |
|
| 86 | - $seeded = true; |
|
| 87 | - } |
|
| 83 | + if (!$seeded) { |
|
| 84 | + $seed = (int)round(((float)microtime() + 1) * time()); |
|
| 85 | + mt_srand($seed); |
|
| 86 | + $seeded = true; |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - $s = random_int(0, mt_getrandmax()); |
|
| 90 | - if (!$s) { |
|
| 91 | - $s = random_int(0, mt_getrandmax()); |
|
| 92 | - } |
|
| 89 | + $s = random_int(0, mt_getrandmax()); |
|
| 90 | + if (!$s) { |
|
| 91 | + $s = random_int(0, mt_getrandmax()); |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - return uniqid($s, 1); |
|
| 94 | + return uniqid($s, 1); |
|
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | /** |
@@ -105,42 +105,42 @@ discard block |
||
| 105 | 105 | * @return string Retourne l'alea éphemère actuel au passage |
| 106 | 106 | */ |
| 107 | 107 | function charger_aleas() { |
| 108 | - if (!isset($GLOBALS['meta']['alea_ephemere'])) { |
|
| 109 | - include_spip('base/abstract_sql'); |
|
| 110 | - $aleas = sql_allfetsel( |
|
| 111 | - ['nom', 'valeur'], |
|
| 112 | - 'spip_meta', |
|
| 113 | - sql_in('nom', ['alea_ephemere', 'alea_ephemere_ancien']), |
|
| 114 | - '', |
|
| 115 | - '', |
|
| 116 | - '', |
|
| 117 | - '', |
|
| 118 | - '', |
|
| 119 | - 'continue' |
|
| 120 | - ); |
|
| 121 | - if ($aleas) { |
|
| 122 | - foreach ($aleas as $a) { |
|
| 123 | - $GLOBALS['meta'][$a['nom']] = $a['valeur']; |
|
| 124 | - } |
|
| 125 | - return $GLOBALS['meta']['alea_ephemere']; |
|
| 126 | - } else { |
|
| 127 | - spip_log('aleas indisponibles', 'session'); |
|
| 128 | - return ''; |
|
| 129 | - } |
|
| 130 | - } |
|
| 131 | - return $GLOBALS['meta']['alea_ephemere']; |
|
| 108 | + if (!isset($GLOBALS['meta']['alea_ephemere'])) { |
|
| 109 | + include_spip('base/abstract_sql'); |
|
| 110 | + $aleas = sql_allfetsel( |
|
| 111 | + ['nom', 'valeur'], |
|
| 112 | + 'spip_meta', |
|
| 113 | + sql_in('nom', ['alea_ephemere', 'alea_ephemere_ancien']), |
|
| 114 | + '', |
|
| 115 | + '', |
|
| 116 | + '', |
|
| 117 | + '', |
|
| 118 | + '', |
|
| 119 | + 'continue' |
|
| 120 | + ); |
|
| 121 | + if ($aleas) { |
|
| 122 | + foreach ($aleas as $a) { |
|
| 123 | + $GLOBALS['meta'][$a['nom']] = $a['valeur']; |
|
| 124 | + } |
|
| 125 | + return $GLOBALS['meta']['alea_ephemere']; |
|
| 126 | + } else { |
|
| 127 | + spip_log('aleas indisponibles', 'session'); |
|
| 128 | + return ''; |
|
| 129 | + } |
|
| 130 | + } |
|
| 131 | + return $GLOBALS['meta']['alea_ephemere']; |
|
| 132 | 132 | } |
| 133 | 133 | |
| 134 | 134 | /** |
| 135 | 135 | * Renouveller l'alea (utilisé pour sécuriser les scripts du répertoire `action/`) |
| 136 | 136 | **/ |
| 137 | 137 | function renouvelle_alea() { |
| 138 | - charger_aleas(); |
|
| 139 | - ecrire_meta('alea_ephemere_ancien', @$GLOBALS['meta']['alea_ephemere'], 'non'); |
|
| 140 | - $GLOBALS['meta']['alea_ephemere'] = md5(creer_uniqid()); |
|
| 141 | - ecrire_meta('alea_ephemere', $GLOBALS['meta']['alea_ephemere'], 'non'); |
|
| 142 | - ecrire_meta('alea_ephemere_date', time(), 'non'); |
|
| 143 | - spip_log("renouvellement de l'alea_ephemere"); |
|
| 138 | + charger_aleas(); |
|
| 139 | + ecrire_meta('alea_ephemere_ancien', @$GLOBALS['meta']['alea_ephemere'], 'non'); |
|
| 140 | + $GLOBALS['meta']['alea_ephemere'] = md5(creer_uniqid()); |
|
| 141 | + ecrire_meta('alea_ephemere', $GLOBALS['meta']['alea_ephemere'], 'non'); |
|
| 142 | + ecrire_meta('alea_ephemere_date', time(), 'non'); |
|
| 143 | + spip_log("renouvellement de l'alea_ephemere"); |
|
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | |
@@ -160,21 +160,21 @@ discard block |
||
| 160 | 160 | * Clé de sécurité. |
| 161 | 161 | **/ |
| 162 | 162 | function low_sec($id_auteur) { |
| 163 | - // Pas d'id_auteur : low_sec |
|
| 164 | - if (!$id_auteur = intval($id_auteur)) { |
|
| 165 | - include_spip('inc/config'); |
|
| 166 | - if (!$low_sec = lire_config('low_sec')) { |
|
| 167 | - ecrire_meta('low_sec', $low_sec = creer_pass_aleatoire()); |
|
| 168 | - } |
|
| 169 | - } else { |
|
| 170 | - $low_sec = sql_getfetsel('low_sec', 'spip_auteurs', 'id_auteur = ' . intval($id_auteur)); |
|
| 171 | - if (!$low_sec) { |
|
| 172 | - $low_sec = creer_pass_aleatoire(); |
|
| 173 | - sql_updateq('spip_auteurs', ['low_sec' => $low_sec], 'id_auteur = ' . intval($id_auteur)); |
|
| 174 | - } |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - return $low_sec; |
|
| 163 | + // Pas d'id_auteur : low_sec |
|
| 164 | + if (!$id_auteur = intval($id_auteur)) { |
|
| 165 | + include_spip('inc/config'); |
|
| 166 | + if (!$low_sec = lire_config('low_sec')) { |
|
| 167 | + ecrire_meta('low_sec', $low_sec = creer_pass_aleatoire()); |
|
| 168 | + } |
|
| 169 | + } else { |
|
| 170 | + $low_sec = sql_getfetsel('low_sec', 'spip_auteurs', 'id_auteur = ' . intval($id_auteur)); |
|
| 171 | + if (!$low_sec) { |
|
| 172 | + $low_sec = creer_pass_aleatoire(); |
|
| 173 | + sql_updateq('spip_auteurs', ['low_sec' => $low_sec], 'id_auteur = ' . intval($id_auteur)); |
|
| 174 | + } |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + return $low_sec; |
|
| 178 | 178 | } |
| 179 | 179 | |
| 180 | 180 | |
@@ -207,11 +207,11 @@ discard block |
||
| 207 | 207 | * @filtre |
| 208 | 208 | */ |
| 209 | 209 | function securiser_acces_low_sec($id_auteur, #[\SensitiveParameter] $cle, $dir, $op = '', $args = '') { |
| 210 | - if ($op) { |
|
| 211 | - $dir .= " $op $args"; |
|
| 212 | - } |
|
| 210 | + if ($op) { |
|
| 211 | + $dir .= " $op $args"; |
|
| 212 | + } |
|
| 213 | 213 | |
| 214 | - return verifier_low_sec($id_auteur, $cle, $dir); |
|
| 214 | + return verifier_low_sec($id_auteur, $cle, $dir); |
|
| 215 | 215 | } |
| 216 | 216 | |
| 217 | 217 | /** |
@@ -226,11 +226,11 @@ discard block |
||
| 226 | 226 | * @return string |
| 227 | 227 | */ |
| 228 | 228 | function generer_url_api_low_sec(string $script, string $format, string $fond, string $path, string $args, bool $no_entities = false, ?bool $public = null) { |
| 229 | - $id_auteur = $GLOBALS['visiteur_session']['id_auteur'] ?? 0; |
|
| 230 | - $cle = afficher_low_sec($id_auteur, "$script/$format $fond $args"); |
|
| 231 | - $path = "$id_auteur/$cle/$format/$fond" . ($path ? "/$path" : ''); |
|
| 229 | + $id_auteur = $GLOBALS['visiteur_session']['id_auteur'] ?? 0; |
|
| 230 | + $cle = afficher_low_sec($id_auteur, "$script/$format $fond $args"); |
|
| 231 | + $path = "$id_auteur/$cle/$format/$fond" . ($path ? "/$path" : ''); |
|
| 232 | 232 | |
| 233 | - return generer_url_api($script, $path, $args, $no_entities = false, $public); |
|
| 233 | + return generer_url_api($script, $path, $args, $no_entities = false, $public); |
|
| 234 | 234 | } |
| 235 | 235 | |
| 236 | 236 | |
@@ -248,27 +248,27 @@ discard block |
||
| 248 | 248 | * @deprecated 4.1 |
| 249 | 249 | */ |
| 250 | 250 | function param_low_sec($op, $args = [], $lang = '', $mime = 'rss') { |
| 251 | - $a = $b = ''; |
|
| 252 | - foreach ($args as $val => $var) { |
|
| 253 | - if ($var) { |
|
| 254 | - if ($val <> 'statut') { |
|
| 255 | - $a .= ':' . $val . '-' . $var; |
|
| 256 | - } |
|
| 257 | - $b .= $val . '=' . $var . '&'; |
|
| 258 | - } |
|
| 259 | - } |
|
| 260 | - $a = substr($a, 1); |
|
| 261 | - $id = intval(@$GLOBALS['connect_id_auteur']); |
|
| 262 | - |
|
| 263 | - return $b |
|
| 264 | - . 'op=' |
|
| 265 | - . $op |
|
| 266 | - . '&id=' |
|
| 267 | - . $id |
|
| 268 | - . '&cle=' |
|
| 269 | - . afficher_low_sec($id, "$mime $op $a") |
|
| 270 | - . (!$a ? '' : "&args=$a") |
|
| 271 | - . (!$lang ? '' : "&lang=$lang"); |
|
| 251 | + $a = $b = ''; |
|
| 252 | + foreach ($args as $val => $var) { |
|
| 253 | + if ($var) { |
|
| 254 | + if ($val <> 'statut') { |
|
| 255 | + $a .= ':' . $val . '-' . $var; |
|
| 256 | + } |
|
| 257 | + $b .= $val . '=' . $var . '&'; |
|
| 258 | + } |
|
| 259 | + } |
|
| 260 | + $a = substr($a, 1); |
|
| 261 | + $id = intval(@$GLOBALS['connect_id_auteur']); |
|
| 262 | + |
|
| 263 | + return $b |
|
| 264 | + . 'op=' |
|
| 265 | + . $op |
|
| 266 | + . '&id=' |
|
| 267 | + . $id |
|
| 268 | + . '&cle=' |
|
| 269 | + . afficher_low_sec($id, "$mime $op $a") |
|
| 270 | + . (!$a ? '' : "&args=$a") |
|
| 271 | + . (!$lang ? '' : "&lang=$lang"); |
|
| 272 | 272 | } |
| 273 | 273 | |
| 274 | 274 | /** |
@@ -284,7 +284,7 @@ discard block |
||
| 284 | 284 | * Clé |
| 285 | 285 | **/ |
| 286 | 286 | function afficher_low_sec($id_auteur, $action = '') { |
| 287 | - return substr(md5($action . low_sec($id_auteur)), 0, 8); |
|
| 287 | + return substr(md5($action . low_sec($id_auteur)), 0, 8); |
|
| 288 | 288 | } |
| 289 | 289 | |
| 290 | 290 | /** |
@@ -302,7 +302,7 @@ discard block |
||
| 302 | 302 | * true si les clés corresponde, false sinon |
| 303 | 303 | **/ |
| 304 | 304 | function verifier_low_sec($id_auteur, #[\SensitiveParameter] $cle, $action = '') { |
| 305 | - return ($cle == afficher_low_sec($id_auteur, $action)); |
|
| 305 | + return ($cle == afficher_low_sec($id_auteur, $action)); |
|
| 306 | 306 | } |
| 307 | 307 | |
| 308 | 308 | /** |
@@ -312,10 +312,10 @@ discard block |
||
| 312 | 312 | * Identifiant de l'auteur |
| 313 | 313 | **/ |
| 314 | 314 | function effacer_low_sec($id_auteur) { |
| 315 | - if (!$id_auteur = intval($id_auteur)) { |
|
| 316 | - return; |
|
| 317 | - } // jamais trop prudent ;) |
|
| 318 | - sql_updateq('spip_auteurs', ['low_sec' => ''], 'id_auteur = ' . intval($id_auteur)); |
|
| 315 | + if (!$id_auteur = intval($id_auteur)) { |
|
| 316 | + return; |
|
| 317 | + } // jamais trop prudent ;) |
|
| 318 | + sql_updateq('spip_auteurs', ['low_sec' => ''], 'id_auteur = ' . intval($id_auteur)); |
|
| 319 | 319 | } |
| 320 | 320 | |
| 321 | 321 | |
@@ -333,31 +333,31 @@ discard block |
||
| 333 | 333 | * - void sinon. |
| 334 | 334 | **/ |
| 335 | 335 | function ecrire_acces() { |
| 336 | - $htaccess = _DIR_RESTREINT . _ACCESS_FILE_NAME; |
|
| 337 | - $htpasswd = _DIR_TMP . _AUTH_USER_FILE; |
|
| 338 | - |
|
| 339 | - // Cette variable de configuration peut etre posee par un plugin |
|
| 340 | - // par exemple acces_restreint ; |
|
| 341 | - // si .htaccess existe, outrepasser spip_meta |
|
| 342 | - if ( |
|
| 343 | - (!isset($GLOBALS['meta']['creer_htpasswd']) |
|
| 344 | - or ($GLOBALS['meta']['creer_htpasswd'] != 'oui')) |
|
| 345 | - and !@file_exists($htaccess) |
|
| 346 | - ) { |
|
| 347 | - spip_unlink($htpasswd); |
|
| 348 | - spip_unlink($htpasswd . '-admin'); |
|
| 349 | - return; |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - # remarque : ici on laisse passer les "nouveau" de maniere a leur permettre |
|
| 353 | - # de devenir redacteur le cas echeant (auth http)... a nettoyer |
|
| 354 | - // attention, il faut au prealable se connecter a la base (necessaire car utilise par install) |
|
| 355 | - // TODO: factoriser avec auth/spip qui fait deja ce job et generaliser le test auth_ldap_connect() |
|
| 356 | - if (include_spip('auth/ldap') and auth_ldap_connect()) { |
|
| 357 | - return; |
|
| 358 | - } |
|
| 359 | - |
|
| 360 | - generer_htpasswd_files($htpasswd, "$htpasswd-admin"); |
|
| 336 | + $htaccess = _DIR_RESTREINT . _ACCESS_FILE_NAME; |
|
| 337 | + $htpasswd = _DIR_TMP . _AUTH_USER_FILE; |
|
| 338 | + |
|
| 339 | + // Cette variable de configuration peut etre posee par un plugin |
|
| 340 | + // par exemple acces_restreint ; |
|
| 341 | + // si .htaccess existe, outrepasser spip_meta |
|
| 342 | + if ( |
|
| 343 | + (!isset($GLOBALS['meta']['creer_htpasswd']) |
|
| 344 | + or ($GLOBALS['meta']['creer_htpasswd'] != 'oui')) |
|
| 345 | + and !@file_exists($htaccess) |
|
| 346 | + ) { |
|
| 347 | + spip_unlink($htpasswd); |
|
| 348 | + spip_unlink($htpasswd . '-admin'); |
|
| 349 | + return; |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + # remarque : ici on laisse passer les "nouveau" de maniere a leur permettre |
|
| 353 | + # de devenir redacteur le cas echeant (auth http)... a nettoyer |
|
| 354 | + // attention, il faut au prealable se connecter a la base (necessaire car utilise par install) |
|
| 355 | + // TODO: factoriser avec auth/spip qui fait deja ce job et generaliser le test auth_ldap_connect() |
|
| 356 | + if (include_spip('auth/ldap') and auth_ldap_connect()) { |
|
| 357 | + return; |
|
| 358 | + } |
|
| 359 | + |
|
| 360 | + generer_htpasswd_files($htpasswd, "$htpasswd-admin"); |
|
| 361 | 361 | } |
| 362 | 362 | |
| 363 | 363 | /** |
@@ -366,29 +366,29 @@ discard block |
||
| 366 | 366 | * @param $htpasswd_admin |
| 367 | 367 | */ |
| 368 | 368 | function generer_htpasswd_files($htpasswd, $htpasswd_admin) { |
| 369 | - if ($generer_htpasswd = charger_fonction('generer_htpasswd_files', 'inc', true)) { |
|
| 370 | - $generer_htpasswd($htpasswd, $htpasswd_admin); |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - $pwd_all = ''; // login:htpass pour tous |
|
| 374 | - $pwd_admin = ''; // login:htpass pour les admins |
|
| 375 | - |
|
| 376 | - $res = sql_select('login, htpass, statut', 'spip_auteurs', "htpass!='' AND login!='' AND " . sql_in('statut', ['1comite', '0minirezo', 'nouveau'])); |
|
| 377 | - while ($row = sql_fetch($res)) { |
|
| 378 | - if (strlen($row['login']) and strlen($row['htpass'])) { |
|
| 379 | - $ligne = $row['login'] . ':' . $row['htpass'] . "\n"; |
|
| 380 | - $pwd_all .= $ligne; |
|
| 381 | - if ($row['statut'] == '0minirezo') { |
|
| 382 | - $pwd_admin .= $ligne; |
|
| 383 | - } |
|
| 384 | - } |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - if ($pwd_all) { |
|
| 388 | - ecrire_fichier($htpasswd, $pwd_all); |
|
| 389 | - ecrire_fichier($htpasswd_admin, $pwd_admin); |
|
| 390 | - spip_log("Ecriture de $htpasswd et $htpasswd_admin", 'htpass'); |
|
| 391 | - } |
|
| 369 | + if ($generer_htpasswd = charger_fonction('generer_htpasswd_files', 'inc', true)) { |
|
| 370 | + $generer_htpasswd($htpasswd, $htpasswd_admin); |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + $pwd_all = ''; // login:htpass pour tous |
|
| 374 | + $pwd_admin = ''; // login:htpass pour les admins |
|
| 375 | + |
|
| 376 | + $res = sql_select('login, htpass, statut', 'spip_auteurs', "htpass!='' AND login!='' AND " . sql_in('statut', ['1comite', '0minirezo', 'nouveau'])); |
|
| 377 | + while ($row = sql_fetch($res)) { |
|
| 378 | + if (strlen($row['login']) and strlen($row['htpass'])) { |
|
| 379 | + $ligne = $row['login'] . ':' . $row['htpass'] . "\n"; |
|
| 380 | + $pwd_all .= $ligne; |
|
| 381 | + if ($row['statut'] == '0minirezo') { |
|
| 382 | + $pwd_admin .= $ligne; |
|
| 383 | + } |
|
| 384 | + } |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + if ($pwd_all) { |
|
| 388 | + ecrire_fichier($htpasswd, $pwd_all); |
|
| 389 | + ecrire_fichier($htpasswd_admin, $pwd_admin); |
|
| 390 | + spip_log("Ecriture de $htpasswd et $htpasswd_admin", 'htpass'); |
|
| 391 | + } |
|
| 392 | 392 | } |
| 393 | 393 | |
| 394 | 394 | /** |
@@ -402,11 +402,11 @@ discard block |
||
| 402 | 402 | * La chaîne hachée si fonction crypt présente, rien sinon. |
| 403 | 403 | */ |
| 404 | 404 | function generer_htpass(#[\SensitiveParameter] $pass) { |
| 405 | - if ($generer_htpass = charger_fonction('generer_htpass', 'inc', true)) { |
|
| 406 | - return $generer_htpass($pass); |
|
| 407 | - } |
|
| 405 | + if ($generer_htpass = charger_fonction('generer_htpass', 'inc', true)) { |
|
| 406 | + return $generer_htpass($pass); |
|
| 407 | + } |
|
| 408 | 408 | |
| 409 | - return ''; |
|
| 409 | + return ''; |
|
| 410 | 410 | } |
| 411 | 411 | |
| 412 | 412 | /** |
@@ -419,14 +419,14 @@ discard block |
||
| 419 | 419 | * @return boolean |
| 420 | 420 | */ |
| 421 | 421 | function verifier_htaccess($rep, $force = false) { |
| 422 | - $htaccess = rtrim($rep, '/') . '/' . _ACCESS_FILE_NAME; |
|
| 423 | - if (((@file_exists($htaccess)) or defined('_TEST_DIRS')) and !$force) { |
|
| 424 | - return true; |
|
| 425 | - } |
|
| 426 | - |
|
| 427 | - // directive deny compatible Apache 2.0+ |
|
| 428 | - $deny = |
|
| 429 | - '# Deny all requests from Apache 2.4+. |
|
| 422 | + $htaccess = rtrim($rep, '/') . '/' . _ACCESS_FILE_NAME; |
|
| 423 | + if (((@file_exists($htaccess)) or defined('_TEST_DIRS')) and !$force) { |
|
| 424 | + return true; |
|
| 425 | + } |
|
| 426 | + |
|
| 427 | + // directive deny compatible Apache 2.0+ |
|
| 428 | + $deny = |
|
| 429 | + '# Deny all requests from Apache 2.4+. |
|
| 430 | 430 | <IfModule mod_authz_core.c> |
| 431 | 431 | Require all denied |
| 432 | 432 | </IfModule> |
@@ -435,32 +435,32 @@ discard block |
||
| 435 | 435 | Deny from all |
| 436 | 436 | </IfModule> |
| 437 | 437 | '; |
| 438 | - // support des vieilles versions Apache 1.x mais uniquement si elles l'annoncent (pas en mode PROD) |
|
| 439 | - if ( |
|
| 440 | - function_exists('apache_get_version') |
|
| 441 | - and $v = apache_get_version() |
|
| 442 | - and strncmp($v, 'Apache/1.', 9) == 0 |
|
| 443 | - ) { |
|
| 444 | - $deny = "deny from all\n"; |
|
| 445 | - } |
|
| 446 | - |
|
| 447 | - if ($ht = @fopen($htaccess, 'w')) { |
|
| 448 | - fputs($ht, $deny); |
|
| 449 | - fclose($ht); |
|
| 450 | - @chmod($htaccess, _SPIP_CHMOD & 0666); |
|
| 451 | - $t = rtrim($rep, '/') . '/.ok'; |
|
| 452 | - if ($ht = @fopen($t, 'w')) { |
|
| 453 | - @fclose($ht); |
|
| 454 | - include_spip('inc/distant'); |
|
| 455 | - $t = substr($t, strlen(_DIR_RACINE)); |
|
| 456 | - $t = url_de_base() . $t; |
|
| 457 | - $ht = recuperer_url($t, ['methode' => 'HEAD', 'taille_max' => 0, 'follow_location' => false]); |
|
| 458 | - $ht = ($ht['status'] ?? null) === 403; |
|
| 459 | - } |
|
| 460 | - } |
|
| 461 | - spip_log("Creation de $htaccess " . ($ht ? ' reussie' : ' manquee')); |
|
| 462 | - |
|
| 463 | - return $ht; |
|
| 438 | + // support des vieilles versions Apache 1.x mais uniquement si elles l'annoncent (pas en mode PROD) |
|
| 439 | + if ( |
|
| 440 | + function_exists('apache_get_version') |
|
| 441 | + and $v = apache_get_version() |
|
| 442 | + and strncmp($v, 'Apache/1.', 9) == 0 |
|
| 443 | + ) { |
|
| 444 | + $deny = "deny from all\n"; |
|
| 445 | + } |
|
| 446 | + |
|
| 447 | + if ($ht = @fopen($htaccess, 'w')) { |
|
| 448 | + fputs($ht, $deny); |
|
| 449 | + fclose($ht); |
|
| 450 | + @chmod($htaccess, _SPIP_CHMOD & 0666); |
|
| 451 | + $t = rtrim($rep, '/') . '/.ok'; |
|
| 452 | + if ($ht = @fopen($t, 'w')) { |
|
| 453 | + @fclose($ht); |
|
| 454 | + include_spip('inc/distant'); |
|
| 455 | + $t = substr($t, strlen(_DIR_RACINE)); |
|
| 456 | + $t = url_de_base() . $t; |
|
| 457 | + $ht = recuperer_url($t, ['methode' => 'HEAD', 'taille_max' => 0, 'follow_location' => false]); |
|
| 458 | + $ht = ($ht['status'] ?? null) === 403; |
|
| 459 | + } |
|
| 460 | + } |
|
| 461 | + spip_log("Creation de $htaccess " . ($ht ? ' reussie' : ' manquee')); |
|
| 462 | + |
|
| 463 | + return $ht; |
|
| 464 | 464 | } |
| 465 | 465 | |
| 466 | 466 | /** |
@@ -478,20 +478,20 @@ discard block |
||
| 478 | 478 | * Valeur de la configuration `creer_htaccess` |
| 479 | 479 | */ |
| 480 | 480 | function gerer_htaccess() { |
| 481 | - // Cette variable de configuration peut etre posee par un plugin |
|
| 482 | - // par exemple acces_restreint |
|
| 483 | - $f = (isset($GLOBALS['meta']['creer_htaccess']) and ($GLOBALS['meta']['creer_htaccess'] === 'oui')); |
|
| 484 | - $dirs = sql_allfetsel('extension', 'spip_types_documents'); |
|
| 485 | - $dirs[] = ['extension' => 'distant']; |
|
| 486 | - foreach ($dirs as $e) { |
|
| 487 | - if (is_dir($dir = _DIR_IMG . $e['extension'])) { |
|
| 488 | - if ($f) { |
|
| 489 | - verifier_htaccess($dir); |
|
| 490 | - } else { |
|
| 491 | - spip_unlink($dir . '/' . _ACCESS_FILE_NAME); |
|
| 492 | - } |
|
| 493 | - } |
|
| 494 | - } |
|
| 495 | - |
|
| 496 | - return $GLOBALS['meta']['creer_htaccess'] ?? ''; |
|
| 481 | + // Cette variable de configuration peut etre posee par un plugin |
|
| 482 | + // par exemple acces_restreint |
|
| 483 | + $f = (isset($GLOBALS['meta']['creer_htaccess']) and ($GLOBALS['meta']['creer_htaccess'] === 'oui')); |
|
| 484 | + $dirs = sql_allfetsel('extension', 'spip_types_documents'); |
|
| 485 | + $dirs[] = ['extension' => 'distant']; |
|
| 486 | + foreach ($dirs as $e) { |
|
| 487 | + if (is_dir($dir = _DIR_IMG . $e['extension'])) { |
|
| 488 | + if ($f) { |
|
| 489 | + verifier_htaccess($dir); |
|
| 490 | + } else { |
|
| 491 | + spip_unlink($dir . '/' . _ACCESS_FILE_NAME); |
|
| 492 | + } |
|
| 493 | + } |
|
| 494 | + } |
|
| 495 | + |
|
| 496 | + return $GLOBALS['meta']['creer_htaccess'] ?? ''; |
|
| 497 | 497 | } |
@@ -15,10 +15,10 @@ discard block |
||
| 15 | 15 | * @package SPIP\Core\Actions |
| 16 | 16 | **/ |
| 17 | 17 | |
| 18 | - use Spip\Chiffrer\SpipCles; |
|
| 18 | + use Spip\Chiffrer\SpipCles; |
|
| 19 | 19 | |
| 20 | 20 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 21 | - return; |
|
| 21 | + return; |
|
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | /** |
@@ -51,19 +51,19 @@ discard block |
||
| 51 | 51 | * @return array|string |
| 52 | 52 | */ |
| 53 | 53 | function inc_securiser_action_dist($action = '', $arg = '', $redirect = '', $mode = false, $att = '', $public = false) { |
| 54 | - if ($action) { |
|
| 55 | - return securiser_action_auteur($action, $arg, $redirect, $mode, $att, $public); |
|
| 56 | - } else { |
|
| 57 | - $arg = _request('arg'); |
|
| 58 | - $hash = _request('hash'); |
|
| 59 | - $action = _request('action') ?: _request('formulaire_action'); |
|
| 60 | - if ($a = verifier_action_auteur("$action-$arg", $hash)) { |
|
| 61 | - return $arg; |
|
| 62 | - } |
|
| 63 | - include_spip('inc/minipres'); |
|
| 64 | - echo minipres(); |
|
| 65 | - exit; |
|
| 66 | - } |
|
| 54 | + if ($action) { |
|
| 55 | + return securiser_action_auteur($action, $arg, $redirect, $mode, $att, $public); |
|
| 56 | + } else { |
|
| 57 | + $arg = _request('arg'); |
|
| 58 | + $hash = _request('hash'); |
|
| 59 | + $action = _request('action') ?: _request('formulaire_action'); |
|
| 60 | + if ($a = verifier_action_auteur("$action-$arg", $hash)) { |
|
| 61 | + return $arg; |
|
| 62 | + } |
|
| 63 | + include_spip('inc/minipres'); |
|
| 64 | + echo minipres(); |
|
| 65 | + exit; |
|
| 66 | + } |
|
| 67 | 67 | } |
| 68 | 68 | |
| 69 | 69 | /** |
@@ -82,29 +82,29 @@ discard block |
||
| 82 | 82 | */ |
| 83 | 83 | function demander_confirmation_avant_action($titre, $titre_bouton, $url_action = null) { |
| 84 | 84 | |
| 85 | - if (!$url_action) { |
|
| 86 | - $url_action = self(); |
|
| 87 | - $action = _request('action'); |
|
| 88 | - $url_action = parametre_url($url_action, 'action', $action, '&'); |
|
| 89 | - } |
|
| 90 | - else { |
|
| 91 | - $action = parametre_url($url_action, 'action'); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - $arg = parametre_url($url_action, 'arg'); |
|
| 95 | - $confirm = md5("$action:$arg:" . realpath(__FILE__)); |
|
| 96 | - if (_request('confirm_action') === $confirm) { |
|
| 97 | - return true; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - $url_confirm = parametre_url($url_action, 'confirm_action', $confirm, '&'); |
|
| 101 | - include_spip('inc/filtres'); |
|
| 102 | - $bouton_action = bouton_action($titre_bouton, $url_confirm); |
|
| 103 | - $corps = "<div style='text-align:center;'>$bouton_action</div>"; |
|
| 104 | - |
|
| 105 | - include_spip('inc/minipres'); |
|
| 106 | - echo minipres($titre, $corps); |
|
| 107 | - exit; |
|
| 85 | + if (!$url_action) { |
|
| 86 | + $url_action = self(); |
|
| 87 | + $action = _request('action'); |
|
| 88 | + $url_action = parametre_url($url_action, 'action', $action, '&'); |
|
| 89 | + } |
|
| 90 | + else { |
|
| 91 | + $action = parametre_url($url_action, 'action'); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + $arg = parametre_url($url_action, 'arg'); |
|
| 95 | + $confirm = md5("$action:$arg:" . realpath(__FILE__)); |
|
| 96 | + if (_request('confirm_action') === $confirm) { |
|
| 97 | + return true; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + $url_confirm = parametre_url($url_action, 'confirm_action', $confirm, '&'); |
|
| 101 | + include_spip('inc/filtres'); |
|
| 102 | + $bouton_action = bouton_action($titre_bouton, $url_confirm); |
|
| 103 | + $corps = "<div style='text-align:center;'>$bouton_action</div>"; |
|
| 104 | + |
|
| 105 | + include_spip('inc/minipres'); |
|
| 106 | + echo minipres($titre, $corps); |
|
| 107 | + exit; |
|
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | /** |
@@ -135,34 +135,34 @@ discard block |
||
| 135 | 135 | */ |
| 136 | 136 | function securiser_action_auteur($action, $arg, $redirect = '', $mode = false, $att = '', $public = false) { |
| 137 | 137 | |
| 138 | - // mode URL ou array |
|
| 139 | - if (!is_string($mode)) { |
|
| 140 | - $hash = calculer_action_auteur("$action-$arg", is_numeric($att) ? $att : null); |
|
| 141 | - |
|
| 142 | - $r = rawurlencode($redirect); |
|
| 143 | - if ($mode === -1) { |
|
| 144 | - return ['action' => $action, 'arg' => $arg, 'hash' => $hash]; |
|
| 145 | - } else { |
|
| 146 | - return generer_url_action( |
|
| 147 | - $action, |
|
| 148 | - 'arg=' . rawurlencode($arg) . "&hash=$hash" . (!$r ? '' : "&redirect=$r"), |
|
| 149 | - $mode, |
|
| 150 | - $public |
|
| 151 | - ); |
|
| 152 | - } |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - // mode formulaire |
|
| 156 | - $hash = calculer_action_auteur("$action-$arg"); |
|
| 157 | - $att .= " style='margin: 0px; border: 0px'"; |
|
| 158 | - if ($redirect) { |
|
| 159 | - $redirect = "\n\t\t<input name='redirect' type='hidden' value='" . str_replace("'", ''', $redirect) . "' />"; |
|
| 160 | - } |
|
| 161 | - $mode .= $redirect . " |
|
| 138 | + // mode URL ou array |
|
| 139 | + if (!is_string($mode)) { |
|
| 140 | + $hash = calculer_action_auteur("$action-$arg", is_numeric($att) ? $att : null); |
|
| 141 | + |
|
| 142 | + $r = rawurlencode($redirect); |
|
| 143 | + if ($mode === -1) { |
|
| 144 | + return ['action' => $action, 'arg' => $arg, 'hash' => $hash]; |
|
| 145 | + } else { |
|
| 146 | + return generer_url_action( |
|
| 147 | + $action, |
|
| 148 | + 'arg=' . rawurlencode($arg) . "&hash=$hash" . (!$r ? '' : "&redirect=$r"), |
|
| 149 | + $mode, |
|
| 150 | + $public |
|
| 151 | + ); |
|
| 152 | + } |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + // mode formulaire |
|
| 156 | + $hash = calculer_action_auteur("$action-$arg"); |
|
| 157 | + $att .= " style='margin: 0px; border: 0px'"; |
|
| 158 | + if ($redirect) { |
|
| 159 | + $redirect = "\n\t\t<input name='redirect' type='hidden' value='" . str_replace("'", ''', $redirect) . "' />"; |
|
| 160 | + } |
|
| 161 | + $mode .= $redirect . " |
|
| 162 | 162 | <input name='hash' type='hidden' value='$hash' /> |
| 163 | 163 | <input name='arg' type='hidden' value='$arg' />"; |
| 164 | 164 | |
| 165 | - return generer_form_action($action, $mode, $att, $public); |
|
| 165 | + return generer_form_action($action, $mode, $att, $public); |
|
| 166 | 166 | } |
| 167 | 167 | |
| 168 | 168 | /** |
@@ -172,48 +172,48 @@ discard block |
||
| 172 | 172 | * @return array |
| 173 | 173 | */ |
| 174 | 174 | function caracteriser_auteur($id_auteur = null) { |
| 175 | - static $caracterisation = []; |
|
| 176 | - |
|
| 177 | - if (is_null($id_auteur) and !isset($GLOBALS['visiteur_session']['id_auteur'])) { |
|
| 178 | - // si l'auteur courant n'est pas connu alors qu'il peut demander une action |
|
| 179 | - // c'est une connexion par php_auth ou 1 instal, on se rabat sur le cookie. |
|
| 180 | - // S'il n'avait pas le droit de realiser cette action, le hash sera faux. |
|
| 181 | - if ( |
|
| 182 | - isset($_COOKIE['spip_session']) |
|
| 183 | - and (preg_match('/^(\d+)/', $_COOKIE['spip_session'], $r)) |
|
| 184 | - ) { |
|
| 185 | - return [$r[1], '']; |
|
| 186 | - // Necessaire aux forums anonymes. |
|
| 187 | - // Pour le reste, ca echouera. |
|
| 188 | - } else { |
|
| 189 | - return ['0', '']; |
|
| 190 | - } |
|
| 191 | - } |
|
| 192 | - // Eviter l'acces SQL si le pass est connu de PHP |
|
| 193 | - if (is_null($id_auteur)) { |
|
| 194 | - $id_auteur = $GLOBALS['visiteur_session']['id_auteur'] ?? 0; |
|
| 195 | - if (isset($GLOBALS['visiteur_session']['pass']) and $GLOBALS['visiteur_session']['pass']) { |
|
| 196 | - return $caracterisation[$id_auteur] = [$id_auteur, $GLOBALS['visiteur_session']['pass']]; |
|
| 197 | - } |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - if (isset($caracterisation[$id_auteur])) { |
|
| 201 | - return $caracterisation[$id_auteur]; |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - if ($id_auteur) { |
|
| 205 | - include_spip('base/abstract_sql'); |
|
| 206 | - $t = sql_fetsel('id_auteur, pass', 'spip_auteurs', "id_auteur=$id_auteur"); |
|
| 207 | - if ($t) { |
|
| 208 | - return $caracterisation[$id_auteur] = [$t['id_auteur'], $t['pass']]; |
|
| 209 | - } |
|
| 210 | - include_spip('inc/minipres'); |
|
| 211 | - echo minipres(); |
|
| 212 | - exit; |
|
| 213 | - } // Visiteur anonyme, pour ls forums par exemple |
|
| 214 | - else { |
|
| 215 | - return ['0', '']; |
|
| 216 | - } |
|
| 175 | + static $caracterisation = []; |
|
| 176 | + |
|
| 177 | + if (is_null($id_auteur) and !isset($GLOBALS['visiteur_session']['id_auteur'])) { |
|
| 178 | + // si l'auteur courant n'est pas connu alors qu'il peut demander une action |
|
| 179 | + // c'est une connexion par php_auth ou 1 instal, on se rabat sur le cookie. |
|
| 180 | + // S'il n'avait pas le droit de realiser cette action, le hash sera faux. |
|
| 181 | + if ( |
|
| 182 | + isset($_COOKIE['spip_session']) |
|
| 183 | + and (preg_match('/^(\d+)/', $_COOKIE['spip_session'], $r)) |
|
| 184 | + ) { |
|
| 185 | + return [$r[1], '']; |
|
| 186 | + // Necessaire aux forums anonymes. |
|
| 187 | + // Pour le reste, ca echouera. |
|
| 188 | + } else { |
|
| 189 | + return ['0', '']; |
|
| 190 | + } |
|
| 191 | + } |
|
| 192 | + // Eviter l'acces SQL si le pass est connu de PHP |
|
| 193 | + if (is_null($id_auteur)) { |
|
| 194 | + $id_auteur = $GLOBALS['visiteur_session']['id_auteur'] ?? 0; |
|
| 195 | + if (isset($GLOBALS['visiteur_session']['pass']) and $GLOBALS['visiteur_session']['pass']) { |
|
| 196 | + return $caracterisation[$id_auteur] = [$id_auteur, $GLOBALS['visiteur_session']['pass']]; |
|
| 197 | + } |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + if (isset($caracterisation[$id_auteur])) { |
|
| 201 | + return $caracterisation[$id_auteur]; |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + if ($id_auteur) { |
|
| 205 | + include_spip('base/abstract_sql'); |
|
| 206 | + $t = sql_fetsel('id_auteur, pass', 'spip_auteurs', "id_auteur=$id_auteur"); |
|
| 207 | + if ($t) { |
|
| 208 | + return $caracterisation[$id_auteur] = [$t['id_auteur'], $t['pass']]; |
|
| 209 | + } |
|
| 210 | + include_spip('inc/minipres'); |
|
| 211 | + echo minipres(); |
|
| 212 | + exit; |
|
| 213 | + } // Visiteur anonyme, pour ls forums par exemple |
|
| 214 | + else { |
|
| 215 | + return ['0', '']; |
|
| 216 | + } |
|
| 217 | 217 | } |
| 218 | 218 | |
| 219 | 219 | /** |
@@ -228,30 +228,30 @@ discard block |
||
| 228 | 228 | * @return string |
| 229 | 229 | */ |
| 230 | 230 | function _action_auteur(string $action, int $id_auteur, #[\SensitiveParameter] ?string $pass, string $alea): string { |
| 231 | - static $sha = []; |
|
| 232 | - $pass = $pass ?? ''; |
|
| 233 | - $entry = "$action:$id_auteur:$pass:$alea"; |
|
| 234 | - if (!isset($sha[$entry])) { |
|
| 235 | - $sha[$entry] = hash_hmac('sha256', "$action::$id_auteur", "$pass::" . _action_get_alea($alea)); |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - return $sha[$entry]; |
|
| 231 | + static $sha = []; |
|
| 232 | + $pass = $pass ?? ''; |
|
| 233 | + $entry = "$action:$id_auteur:$pass:$alea"; |
|
| 234 | + if (!isset($sha[$entry])) { |
|
| 235 | + $sha[$entry] = hash_hmac('sha256', "$action::$id_auteur", "$pass::" . _action_get_alea($alea)); |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + return $sha[$entry]; |
|
| 239 | 239 | } |
| 240 | 240 | |
| 241 | 241 | function _action_get_alea(string $alea): string { |
| 242 | - if (!isset($GLOBALS['meta'][$alea])) { |
|
| 243 | - if (!$exec = _request('exec') or !autoriser_sans_cookie($exec)) { |
|
| 244 | - include_spip('inc/acces'); |
|
| 245 | - charger_aleas(); |
|
| 246 | - if (empty($GLOBALS['meta'][$alea])) { |
|
| 247 | - include_spip('inc/minipres'); |
|
| 248 | - echo minipres(); |
|
| 249 | - spip_log("$alea indisponible"); |
|
| 250 | - exit; |
|
| 251 | - } |
|
| 252 | - } |
|
| 253 | - } |
|
| 254 | - return $GLOBALS['meta'][$alea] ?? ''; |
|
| 242 | + if (!isset($GLOBALS['meta'][$alea])) { |
|
| 243 | + if (!$exec = _request('exec') or !autoriser_sans_cookie($exec)) { |
|
| 244 | + include_spip('inc/acces'); |
|
| 245 | + charger_aleas(); |
|
| 246 | + if (empty($GLOBALS['meta'][$alea])) { |
|
| 247 | + include_spip('inc/minipres'); |
|
| 248 | + echo minipres(); |
|
| 249 | + spip_log("$alea indisponible"); |
|
| 250 | + exit; |
|
| 251 | + } |
|
| 252 | + } |
|
| 253 | + } |
|
| 254 | + return $GLOBALS['meta'][$alea] ?? ''; |
|
| 255 | 255 | } |
| 256 | 256 | |
| 257 | 257 | /** |
@@ -262,9 +262,9 @@ discard block |
||
| 262 | 262 | * @return string |
| 263 | 263 | */ |
| 264 | 264 | function calculer_action_auteur($action, $id_auteur = null) { |
| 265 | - [$id_auteur, $pass] = caracteriser_auteur($id_auteur); |
|
| 265 | + [$id_auteur, $pass] = caracteriser_auteur($id_auteur); |
|
| 266 | 266 | |
| 267 | - return _action_auteur($action, $id_auteur, $pass, 'alea_ephemere'); |
|
| 267 | + return _action_auteur($action, $id_auteur, $pass, 'alea_ephemere'); |
|
| 268 | 268 | } |
| 269 | 269 | |
| 270 | 270 | |
@@ -277,15 +277,15 @@ discard block |
||
| 277 | 277 | * @return bool |
| 278 | 278 | */ |
| 279 | 279 | function verifier_action_auteur($action, $hash) { |
| 280 | - [$id_auteur, $pass] = caracteriser_auteur(); |
|
| 281 | - if ( |
|
| 282 | - hash_equals($hash, _action_auteur($action, $id_auteur, $pass, 'alea_ephemere')) |
|
| 283 | - or hash_equals($hash, _action_auteur($action, $id_auteur, $pass, 'alea_ephemere_ancien')) |
|
| 284 | - ) { |
|
| 285 | - return true; |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - return false; |
|
| 280 | + [$id_auteur, $pass] = caracteriser_auteur(); |
|
| 281 | + if ( |
|
| 282 | + hash_equals($hash, _action_auteur($action, $id_auteur, $pass, 'alea_ephemere')) |
|
| 283 | + or hash_equals($hash, _action_auteur($action, $id_auteur, $pass, 'alea_ephemere_ancien')) |
|
| 284 | + ) { |
|
| 285 | + return true; |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + return false; |
|
| 289 | 289 | } |
| 290 | 290 | |
| 291 | 291 | // |
@@ -300,7 +300,7 @@ discard block |
||
| 300 | 300 | * @return string |
| 301 | 301 | */ |
| 302 | 302 | function secret_du_site() { |
| 303 | - return SpipCles::secret_du_site(); |
|
| 303 | + return SpipCles::secret_du_site(); |
|
| 304 | 304 | } |
| 305 | 305 | |
| 306 | 306 | /** |
@@ -310,7 +310,7 @@ discard block |
||
| 310 | 310 | * @return string |
| 311 | 311 | */ |
| 312 | 312 | function calculer_cle_action($action) { |
| 313 | - return hash_hmac('sha256', $action, secret_du_site()); |
|
| 313 | + return hash_hmac('sha256', $action, secret_du_site()); |
|
| 314 | 314 | } |
| 315 | 315 | |
| 316 | 316 | /** |
@@ -321,7 +321,7 @@ discard block |
||
| 321 | 321 | * @return bool |
| 322 | 322 | */ |
| 323 | 323 | function verifier_cle_action($action, #[\SensitiveParameter] $cle) { |
| 324 | - return hash_equals($cle, calculer_cle_action($action)); |
|
| 324 | + return hash_equals($cle, calculer_cle_action($action)); |
|
| 325 | 325 | } |
| 326 | 326 | |
| 327 | 327 | |
@@ -338,19 +338,19 @@ discard block |
||
| 338 | 338 | * @return string Token, de la forme "{id}*{hash}" |
| 339 | 339 | */ |
| 340 | 340 | function calculer_token_previsu($url, $id_auteur = null, $alea = 'alea_ephemere') { |
| 341 | - if (is_null($id_auteur)) { |
|
| 342 | - if (!empty($GLOBALS['visiteur_session']['id_auteur'])) { |
|
| 343 | - $id_auteur = $GLOBALS['visiteur_session']['id_auteur']; |
|
| 344 | - } |
|
| 345 | - } |
|
| 346 | - if (!$id_auteur = intval($id_auteur)) { |
|
| 347 | - return ''; |
|
| 348 | - } |
|
| 349 | - // On nettoie l’URL de tous les var_. |
|
| 350 | - $url = nettoyer_uri_var($url); |
|
| 351 | - |
|
| 352 | - $token = _action_auteur('previsualiser-' . $url, $id_auteur, secret_du_site(), $alea); |
|
| 353 | - return "$id_auteur-$token"; |
|
| 341 | + if (is_null($id_auteur)) { |
|
| 342 | + if (!empty($GLOBALS['visiteur_session']['id_auteur'])) { |
|
| 343 | + $id_auteur = $GLOBALS['visiteur_session']['id_auteur']; |
|
| 344 | + } |
|
| 345 | + } |
|
| 346 | + if (!$id_auteur = intval($id_auteur)) { |
|
| 347 | + return ''; |
|
| 348 | + } |
|
| 349 | + // On nettoie l’URL de tous les var_. |
|
| 350 | + $url = nettoyer_uri_var($url); |
|
| 351 | + |
|
| 352 | + $token = _action_auteur('previsualiser-' . $url, $id_auteur, secret_du_site(), $alea); |
|
| 353 | + return "$id_auteur-$token"; |
|
| 354 | 354 | } |
| 355 | 355 | |
| 356 | 356 | |
@@ -368,31 +368,31 @@ discard block |
||
| 368 | 368 | * + Tableau (id auteur, type d’objet, id_objet) sinon. |
| 369 | 369 | */ |
| 370 | 370 | function verifier_token_previsu(#[\SensitiveParameter] $token) { |
| 371 | - // retrouver auteur / hash |
|
| 372 | - $e = explode('-', $token, 2); |
|
| 373 | - if (count($e) == 2 and is_numeric(reset($e))) { |
|
| 374 | - $id_auteur = intval(reset($e)); |
|
| 375 | - } else { |
|
| 376 | - return false; |
|
| 377 | - } |
|
| 378 | - |
|
| 379 | - // calculer le type et id de l’url actuelle |
|
| 380 | - include_spip('inc/urls'); |
|
| 381 | - include_spip('inc/filtres_mini'); |
|
| 382 | - $url = url_absolue(self()); |
|
| 383 | - |
|
| 384 | - // verifier le token |
|
| 385 | - $_token = calculer_token_previsu($url, $id_auteur, 'alea_ephemere'); |
|
| 386 | - if (!$_token or !hash_equals($token, $_token)) { |
|
| 387 | - $_token = calculer_token_previsu($url, $id_auteur, 'alea_ephemere_ancien'); |
|
| 388 | - if (!$_token or !hash_equals($token, $_token)) { |
|
| 389 | - return false; |
|
| 390 | - } |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - return [ |
|
| 394 | - 'id_auteur' => $id_auteur, |
|
| 395 | - ]; |
|
| 371 | + // retrouver auteur / hash |
|
| 372 | + $e = explode('-', $token, 2); |
|
| 373 | + if (count($e) == 2 and is_numeric(reset($e))) { |
|
| 374 | + $id_auteur = intval(reset($e)); |
|
| 375 | + } else { |
|
| 376 | + return false; |
|
| 377 | + } |
|
| 378 | + |
|
| 379 | + // calculer le type et id de l’url actuelle |
|
| 380 | + include_spip('inc/urls'); |
|
| 381 | + include_spip('inc/filtres_mini'); |
|
| 382 | + $url = url_absolue(self()); |
|
| 383 | + |
|
| 384 | + // verifier le token |
|
| 385 | + $_token = calculer_token_previsu($url, $id_auteur, 'alea_ephemere'); |
|
| 386 | + if (!$_token or !hash_equals($token, $_token)) { |
|
| 387 | + $_token = calculer_token_previsu($url, $id_auteur, 'alea_ephemere_ancien'); |
|
| 388 | + if (!$_token or !hash_equals($token, $_token)) { |
|
| 389 | + return false; |
|
| 390 | + } |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + return [ |
|
| 394 | + 'id_auteur' => $id_auteur, |
|
| 395 | + ]; |
|
| 396 | 396 | } |
| 397 | 397 | |
| 398 | 398 | /** |
@@ -401,13 +401,13 @@ discard block |
||
| 401 | 401 | * @return bool|array |
| 402 | 402 | */ |
| 403 | 403 | function decrire_token_previsu() { |
| 404 | - static $desc = null; |
|
| 405 | - if (is_null($desc)) { |
|
| 406 | - if ($token = _request('var_previewtoken')) { |
|
| 407 | - $desc = verifier_token_previsu($token); |
|
| 408 | - } else { |
|
| 409 | - $desc = false; |
|
| 410 | - } |
|
| 411 | - } |
|
| 412 | - return $desc; |
|
| 404 | + static $desc = null; |
|
| 405 | + if (is_null($desc)) { |
|
| 406 | + if ($token = _request('var_previewtoken')) { |
|
| 407 | + $desc = verifier_token_previsu($token); |
|
| 408 | + } else { |
|
| 409 | + $desc = false; |
|
| 410 | + } |
|
| 411 | + } |
|
| 412 | + return $desc; |
|
| 413 | 413 | } |
@@ -18,7 +18,7 @@ discard block |
||
| 18 | 18 | use Spip\Chiffrer\SpipCles; |
| 19 | 19 | |
| 20 | 20 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 21 | - return; |
|
| 21 | + return; |
|
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | /** |
@@ -33,169 +33,169 @@ discard block |
||
| 33 | 33 | */ |
| 34 | 34 | function auth_spip_dist($login, #[\SensitiveParameter] $pass, $serveur = '', $phpauth = false) { |
| 35 | 35 | |
| 36 | - // retrouver le login |
|
| 37 | - $login = auth_spip_retrouver_login($login); |
|
| 38 | - // login inconnu, n'allons pas plus loin |
|
| 39 | - if (!$login) { |
|
| 40 | - return []; |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - $md5pass = ''; |
|
| 44 | - $shapass = $shanext = ''; |
|
| 45 | - $auteur_peut_sauver_cles = false; |
|
| 46 | - |
|
| 47 | - if ($pass) { |
|
| 48 | - $row = sql_fetsel( |
|
| 49 | - '*', |
|
| 50 | - 'spip_auteurs', |
|
| 51 | - 'login=' . sql_quote($login, $serveur, 'text') . " AND statut<>'5poubelle'", |
|
| 52 | - '', |
|
| 53 | - '', |
|
| 54 | - '', |
|
| 55 | - '', |
|
| 56 | - $serveur |
|
| 57 | - ); |
|
| 58 | - |
|
| 59 | - // lever un flag si cet auteur peut sauver les cles |
|
| 60 | - if ($row['statut'] === '0minirezo' and $row['webmestre'] === 'oui' and isset($row['backup_cles'])) { |
|
| 61 | - $auteur_peut_sauver_cles = true; |
|
| 62 | - } |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - // login inexistant ou mot de passe vide |
|
| 66 | - if (!$pass or !$row) { |
|
| 67 | - return []; |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - $cles = SpipCles::instance(); |
|
| 71 | - $secret = $cles->getSecretAuth(); |
|
| 72 | - |
|
| 73 | - $hash = null; |
|
| 74 | - switch (strlen($row['pass'])) { |
|
| 75 | - // legacy = md5 ou sha256 |
|
| 76 | - case 32: |
|
| 77 | - // tres anciens mots de passe encodes en md5(alea.pass) |
|
| 78 | - $hash = md5($row['alea_actuel'] . $pass); |
|
| 79 | - $methode = 'md5'; |
|
| 80 | - case 64: |
|
| 81 | - if (empty($hash)) { |
|
| 82 | - // anciens mots de passe encodes en sha256(alea.pass) |
|
| 83 | - include_spip('auth/sha256.inc'); |
|
| 84 | - $hash = spip_sha256($row['alea_actuel'] . $pass); |
|
| 85 | - $methode = 'sha256'; |
|
| 86 | - } |
|
| 87 | - if ($row['pass'] === $hash) { |
|
| 88 | - spip_log("validation du mot de passe pour l'auteur #" . $row['id_auteur'] . " $login via $methode", 'auth' . _LOG_DEBUG); |
|
| 89 | - // ce n'est pas cense arriver, mais si jamais c'est un backup inutilisable, il faut le nettoyer pour ne pas bloquer la creation d'une nouvelle cle d'auth |
|
| 90 | - if (!empty($row['backup_cles'])) { |
|
| 91 | - sql_updateq('spip_auteurs', ['backup_cles' => ''], 'id_auteur=' . intval($row['id_auteur'])); |
|
| 92 | - } |
|
| 93 | - break; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - // on teste la methode par defaut, au cas ou ce serait un pass moderne qui a la malchance d'etre en 64char de long |
|
| 97 | - |
|
| 98 | - case 60: |
|
| 99 | - case 98: |
|
| 100 | - default: |
|
| 101 | - // doit-on restaurer un backup des cles ? |
|
| 102 | - // si on a le bon pass on peut decoder le backup, retrouver la cle, et du coup valider le pass |
|
| 103 | - if ( |
|
| 104 | - !$secret |
|
| 105 | - and $auteur_peut_sauver_cles |
|
| 106 | - and !empty($row['backup_cles']) |
|
| 107 | - ) { |
|
| 108 | - if ($cles->restore($row['backup_cles'], $pass, $row['pass'], $row['id_auteur'])) { |
|
| 109 | - spip_log('Les cles secretes ont ete restaurées avec le backup du webmestre #' . $row['id_auteur'], 'auth' . _LOG_INFO_IMPORTANTE); |
|
| 110 | - if ($cles->save()) { |
|
| 111 | - $secret = $cles->getSecretAuth(); |
|
| 112 | - } |
|
| 113 | - else { |
|
| 114 | - spip_log("Echec restauration des cles : verifier les droits d'ecriture ?", 'auth' . _LOG_ERREUR); |
|
| 115 | - // et on echoue car on ne veut pas que la situation reste telle quelle |
|
| 116 | - raler_fichier(_DIR_ETC . 'cles.php'); |
|
| 117 | - } |
|
| 118 | - } |
|
| 119 | - else { |
|
| 120 | - spip_log('Pas de cle secrete disponible (fichier config/cle.php absent ?) mais le backup du webmestre #' . $row['id_auteur'] . " n'est pas valide", 'auth' . _LOG_ERREUR); |
|
| 121 | - sql_updateq('spip_auteurs', ['backup_cles' => ''], 'id_auteur=' . intval($row['id_auteur'])); |
|
| 122 | - } |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - if (!$secret or !Password::verifier($pass, $row['pass'], $secret)) { |
|
| 126 | - unset($row); |
|
| 127 | - } |
|
| 128 | - else { |
|
| 129 | - spip_log("validation du mot de passe pour l'auteur #" . $row['id_auteur'] . " $login via Password::verifier", 'auth' . _LOG_DEBUG); |
|
| 130 | - } |
|
| 131 | - break; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - // Migration depuis ancienne version : si on a pas encore de cle |
|
| 135 | - // ET si c'est le login d'un auteur qui peut sauver la cle |
|
| 136 | - // créer la clé (en s'assurant bien que personne n'a de backup d'un precedent fichier cle.php) |
|
| 137 | - // si c'est un auteur normal, on ne fait rien, il garde son ancien pass hashé en sha256 en attendant le login d'un webmestre |
|
| 138 | - if (!$secret and $auteur_peut_sauver_cles) { |
|
| 139 | - if (auth_spip_initialiser_secret()) { |
|
| 140 | - $secret = $cles->getSecretAuth(); |
|
| 141 | - } |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - // login/mot de passe incorrect |
|
| 145 | - if (empty($row)) { |
|
| 146 | - return []; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - // fait tourner le codage du pass dans la base |
|
| 150 | - // sauf si phpauth : cela reviendrait a changer l'alea a chaque hit, et aucune action verifiable par securiser_action() |
|
| 151 | - if (!$phpauth and $secret) { |
|
| 152 | - include_spip('inc/acces'); // pour creer_uniqid et verifier_htaccess |
|
| 153 | - $pass_hash_next = Password::hacher($pass, $secret); |
|
| 154 | - if ($pass_hash_next) { |
|
| 155 | - $set = [ |
|
| 156 | - 'alea_actuel' => 'alea_futur', // @deprecated 4.1 |
|
| 157 | - 'alea_futur' => sql_quote(creer_uniqid(), $serveur, 'text'), // @deprecated 4.1 |
|
| 158 | - 'pass' => sql_quote($pass_hash_next, $serveur, 'text'), |
|
| 159 | - ]; |
|
| 160 | - |
|
| 161 | - // regenerer un htpass si on a active/desactive le plugin htpasswd |
|
| 162 | - // et/ou que l'algo a change - pour etre certain de toujours utiliser le bon algo |
|
| 163 | - $htpass = generer_htpass($pass); |
|
| 164 | - if (strlen($htpass) !== strlen($row['htpass'])) { |
|
| 165 | - $set['htpass'] = sql_quote($htpass, $serveur, 'text'); |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - // a chaque login de webmestre : sauvegarde chiffree des clés du site (avec les pass du webmestre) |
|
| 169 | - if ($auteur_peut_sauver_cles) { |
|
| 170 | - $set['backup_cles'] = sql_quote($cles->backup($pass), $serveur, 'text'); |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - @sql_update( |
|
| 174 | - 'spip_auteurs', |
|
| 175 | - $set, |
|
| 176 | - 'id_auteur=' . intval($row['id_auteur']) . ' AND pass=' . sql_quote( |
|
| 177 | - $row['pass'], |
|
| 178 | - $serveur, |
|
| 179 | - 'text' |
|
| 180 | - ), |
|
| 181 | - [], |
|
| 182 | - $serveur |
|
| 183 | - ); |
|
| 184 | - |
|
| 185 | - // si on a change le htpass car changement d'algo, regenerer les fichiers htpasswd |
|
| 186 | - if (isset($set['htpass'])) { |
|
| 187 | - ecrire_acces(); |
|
| 188 | - } |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - // En profiter pour verifier la securite de tmp/ |
|
| 192 | - // Si elle ne fonctionne pas a l'installation, prevenir |
|
| 193 | - if (!verifier_htaccess(_DIR_TMP) and defined('_ECRIRE_INSTALL')) { |
|
| 194 | - return false; |
|
| 195 | - } |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - return $row; |
|
| 36 | + // retrouver le login |
|
| 37 | + $login = auth_spip_retrouver_login($login); |
|
| 38 | + // login inconnu, n'allons pas plus loin |
|
| 39 | + if (!$login) { |
|
| 40 | + return []; |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + $md5pass = ''; |
|
| 44 | + $shapass = $shanext = ''; |
|
| 45 | + $auteur_peut_sauver_cles = false; |
|
| 46 | + |
|
| 47 | + if ($pass) { |
|
| 48 | + $row = sql_fetsel( |
|
| 49 | + '*', |
|
| 50 | + 'spip_auteurs', |
|
| 51 | + 'login=' . sql_quote($login, $serveur, 'text') . " AND statut<>'5poubelle'", |
|
| 52 | + '', |
|
| 53 | + '', |
|
| 54 | + '', |
|
| 55 | + '', |
|
| 56 | + $serveur |
|
| 57 | + ); |
|
| 58 | + |
|
| 59 | + // lever un flag si cet auteur peut sauver les cles |
|
| 60 | + if ($row['statut'] === '0minirezo' and $row['webmestre'] === 'oui' and isset($row['backup_cles'])) { |
|
| 61 | + $auteur_peut_sauver_cles = true; |
|
| 62 | + } |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + // login inexistant ou mot de passe vide |
|
| 66 | + if (!$pass or !$row) { |
|
| 67 | + return []; |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + $cles = SpipCles::instance(); |
|
| 71 | + $secret = $cles->getSecretAuth(); |
|
| 72 | + |
|
| 73 | + $hash = null; |
|
| 74 | + switch (strlen($row['pass'])) { |
|
| 75 | + // legacy = md5 ou sha256 |
|
| 76 | + case 32: |
|
| 77 | + // tres anciens mots de passe encodes en md5(alea.pass) |
|
| 78 | + $hash = md5($row['alea_actuel'] . $pass); |
|
| 79 | + $methode = 'md5'; |
|
| 80 | + case 64: |
|
| 81 | + if (empty($hash)) { |
|
| 82 | + // anciens mots de passe encodes en sha256(alea.pass) |
|
| 83 | + include_spip('auth/sha256.inc'); |
|
| 84 | + $hash = spip_sha256($row['alea_actuel'] . $pass); |
|
| 85 | + $methode = 'sha256'; |
|
| 86 | + } |
|
| 87 | + if ($row['pass'] === $hash) { |
|
| 88 | + spip_log("validation du mot de passe pour l'auteur #" . $row['id_auteur'] . " $login via $methode", 'auth' . _LOG_DEBUG); |
|
| 89 | + // ce n'est pas cense arriver, mais si jamais c'est un backup inutilisable, il faut le nettoyer pour ne pas bloquer la creation d'une nouvelle cle d'auth |
|
| 90 | + if (!empty($row['backup_cles'])) { |
|
| 91 | + sql_updateq('spip_auteurs', ['backup_cles' => ''], 'id_auteur=' . intval($row['id_auteur'])); |
|
| 92 | + } |
|
| 93 | + break; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + // on teste la methode par defaut, au cas ou ce serait un pass moderne qui a la malchance d'etre en 64char de long |
|
| 97 | + |
|
| 98 | + case 60: |
|
| 99 | + case 98: |
|
| 100 | + default: |
|
| 101 | + // doit-on restaurer un backup des cles ? |
|
| 102 | + // si on a le bon pass on peut decoder le backup, retrouver la cle, et du coup valider le pass |
|
| 103 | + if ( |
|
| 104 | + !$secret |
|
| 105 | + and $auteur_peut_sauver_cles |
|
| 106 | + and !empty($row['backup_cles']) |
|
| 107 | + ) { |
|
| 108 | + if ($cles->restore($row['backup_cles'], $pass, $row['pass'], $row['id_auteur'])) { |
|
| 109 | + spip_log('Les cles secretes ont ete restaurées avec le backup du webmestre #' . $row['id_auteur'], 'auth' . _LOG_INFO_IMPORTANTE); |
|
| 110 | + if ($cles->save()) { |
|
| 111 | + $secret = $cles->getSecretAuth(); |
|
| 112 | + } |
|
| 113 | + else { |
|
| 114 | + spip_log("Echec restauration des cles : verifier les droits d'ecriture ?", 'auth' . _LOG_ERREUR); |
|
| 115 | + // et on echoue car on ne veut pas que la situation reste telle quelle |
|
| 116 | + raler_fichier(_DIR_ETC . 'cles.php'); |
|
| 117 | + } |
|
| 118 | + } |
|
| 119 | + else { |
|
| 120 | + spip_log('Pas de cle secrete disponible (fichier config/cle.php absent ?) mais le backup du webmestre #' . $row['id_auteur'] . " n'est pas valide", 'auth' . _LOG_ERREUR); |
|
| 121 | + sql_updateq('spip_auteurs', ['backup_cles' => ''], 'id_auteur=' . intval($row['id_auteur'])); |
|
| 122 | + } |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + if (!$secret or !Password::verifier($pass, $row['pass'], $secret)) { |
|
| 126 | + unset($row); |
|
| 127 | + } |
|
| 128 | + else { |
|
| 129 | + spip_log("validation du mot de passe pour l'auteur #" . $row['id_auteur'] . " $login via Password::verifier", 'auth' . _LOG_DEBUG); |
|
| 130 | + } |
|
| 131 | + break; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + // Migration depuis ancienne version : si on a pas encore de cle |
|
| 135 | + // ET si c'est le login d'un auteur qui peut sauver la cle |
|
| 136 | + // créer la clé (en s'assurant bien que personne n'a de backup d'un precedent fichier cle.php) |
|
| 137 | + // si c'est un auteur normal, on ne fait rien, il garde son ancien pass hashé en sha256 en attendant le login d'un webmestre |
|
| 138 | + if (!$secret and $auteur_peut_sauver_cles) { |
|
| 139 | + if (auth_spip_initialiser_secret()) { |
|
| 140 | + $secret = $cles->getSecretAuth(); |
|
| 141 | + } |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + // login/mot de passe incorrect |
|
| 145 | + if (empty($row)) { |
|
| 146 | + return []; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + // fait tourner le codage du pass dans la base |
|
| 150 | + // sauf si phpauth : cela reviendrait a changer l'alea a chaque hit, et aucune action verifiable par securiser_action() |
|
| 151 | + if (!$phpauth and $secret) { |
|
| 152 | + include_spip('inc/acces'); // pour creer_uniqid et verifier_htaccess |
|
| 153 | + $pass_hash_next = Password::hacher($pass, $secret); |
|
| 154 | + if ($pass_hash_next) { |
|
| 155 | + $set = [ |
|
| 156 | + 'alea_actuel' => 'alea_futur', // @deprecated 4.1 |
|
| 157 | + 'alea_futur' => sql_quote(creer_uniqid(), $serveur, 'text'), // @deprecated 4.1 |
|
| 158 | + 'pass' => sql_quote($pass_hash_next, $serveur, 'text'), |
|
| 159 | + ]; |
|
| 160 | + |
|
| 161 | + // regenerer un htpass si on a active/desactive le plugin htpasswd |
|
| 162 | + // et/ou que l'algo a change - pour etre certain de toujours utiliser le bon algo |
|
| 163 | + $htpass = generer_htpass($pass); |
|
| 164 | + if (strlen($htpass) !== strlen($row['htpass'])) { |
|
| 165 | + $set['htpass'] = sql_quote($htpass, $serveur, 'text'); |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + // a chaque login de webmestre : sauvegarde chiffree des clés du site (avec les pass du webmestre) |
|
| 169 | + if ($auteur_peut_sauver_cles) { |
|
| 170 | + $set['backup_cles'] = sql_quote($cles->backup($pass), $serveur, 'text'); |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + @sql_update( |
|
| 174 | + 'spip_auteurs', |
|
| 175 | + $set, |
|
| 176 | + 'id_auteur=' . intval($row['id_auteur']) . ' AND pass=' . sql_quote( |
|
| 177 | + $row['pass'], |
|
| 178 | + $serveur, |
|
| 179 | + 'text' |
|
| 180 | + ), |
|
| 181 | + [], |
|
| 182 | + $serveur |
|
| 183 | + ); |
|
| 184 | + |
|
| 185 | + // si on a change le htpass car changement d'algo, regenerer les fichiers htpasswd |
|
| 186 | + if (isset($set['htpass'])) { |
|
| 187 | + ecrire_acces(); |
|
| 188 | + } |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + // En profiter pour verifier la securite de tmp/ |
|
| 192 | + // Si elle ne fonctionne pas a l'installation, prevenir |
|
| 193 | + if (!verifier_htaccess(_DIR_TMP) and defined('_ECRIRE_INSTALL')) { |
|
| 194 | + return false; |
|
| 195 | + } |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + return $row; |
|
| 199 | 199 | } |
| 200 | 200 | |
| 201 | 201 | /** |
@@ -210,36 +210,36 @@ discard block |
||
| 210 | 210 | * @return bool |
| 211 | 211 | */ |
| 212 | 212 | function auth_spip_initialiser_secret(bool $force = false): bool { |
| 213 | - $cles = SpipCles::instance(); |
|
| 214 | - $secret = $cles->getSecretAuth(); |
|
| 215 | - |
|
| 216 | - // on ne fait rien si on a un secret dispo |
|
| 217 | - if ($secret) { |
|
| 218 | - return false; |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - // si force, on ne verifie pas la presence d'un backup chez un webmestre |
|
| 222 | - if ($force) { |
|
| 223 | - spip_log('Pas de cle secrete disponible, on regenere une nouvelle cle forcee - tous les mots de passe sont invalides', 'auth' . _LOG_INFO_IMPORTANTE); |
|
| 224 | - $secret = $cles->getSecretAuth(true); |
|
| 225 | - return true; |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - $has_backup = sql_allfetsel('id_auteur', 'spip_auteurs', 'statut=' . sql_quote('0minirezo') . ' AND webmestre=' . sql_quote('oui') . " AND backup_cles!=''"); |
|
| 229 | - $has_backup = array_column($has_backup, 'id_auteur'); |
|
| 230 | - if (empty($has_backup)) { |
|
| 231 | - spip_log("Pas de cle secrete disponible, et aucun webmestre n'a de backup, on regenere une nouvelle cle - tous les mots de passe sont invalides", 'auth' . _LOG_INFO_IMPORTANTE); |
|
| 232 | - if ($secret = $cles->getSecretAuth(true)) { |
|
| 233 | - return true; |
|
| 234 | - } |
|
| 235 | - spip_log("Echec generation d'une nouvelle cle : verifier les droits d'ecriture ?", 'auth' . _LOG_ERREUR); |
|
| 236 | - // et on echoue car on ne veut pas que la situation reste telle quelle |
|
| 237 | - raler_fichier(_DIR_ETC . 'cles.php'); |
|
| 238 | - } |
|
| 239 | - else { |
|
| 240 | - spip_log('Pas de cle secrete disponible (fichier config/cle.php absent ?) un des webmestres #' . implode(', #', $has_backup) . ' doit se connecter pour restaurer son backup des cles', 'auth' . _LOG_ERREUR); |
|
| 241 | - } |
|
| 242 | - return false; |
|
| 213 | + $cles = SpipCles::instance(); |
|
| 214 | + $secret = $cles->getSecretAuth(); |
|
| 215 | + |
|
| 216 | + // on ne fait rien si on a un secret dispo |
|
| 217 | + if ($secret) { |
|
| 218 | + return false; |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + // si force, on ne verifie pas la presence d'un backup chez un webmestre |
|
| 222 | + if ($force) { |
|
| 223 | + spip_log('Pas de cle secrete disponible, on regenere une nouvelle cle forcee - tous les mots de passe sont invalides', 'auth' . _LOG_INFO_IMPORTANTE); |
|
| 224 | + $secret = $cles->getSecretAuth(true); |
|
| 225 | + return true; |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + $has_backup = sql_allfetsel('id_auteur', 'spip_auteurs', 'statut=' . sql_quote('0minirezo') . ' AND webmestre=' . sql_quote('oui') . " AND backup_cles!=''"); |
|
| 229 | + $has_backup = array_column($has_backup, 'id_auteur'); |
|
| 230 | + if (empty($has_backup)) { |
|
| 231 | + spip_log("Pas de cle secrete disponible, et aucun webmestre n'a de backup, on regenere une nouvelle cle - tous les mots de passe sont invalides", 'auth' . _LOG_INFO_IMPORTANTE); |
|
| 232 | + if ($secret = $cles->getSecretAuth(true)) { |
|
| 233 | + return true; |
|
| 234 | + } |
|
| 235 | + spip_log("Echec generation d'une nouvelle cle : verifier les droits d'ecriture ?", 'auth' . _LOG_ERREUR); |
|
| 236 | + // et on echoue car on ne veut pas que la situation reste telle quelle |
|
| 237 | + raler_fichier(_DIR_ETC . 'cles.php'); |
|
| 238 | + } |
|
| 239 | + else { |
|
| 240 | + spip_log('Pas de cle secrete disponible (fichier config/cle.php absent ?) un des webmestres #' . implode(', #', $has_backup) . ' doit se connecter pour restaurer son backup des cles', 'auth' . _LOG_ERREUR); |
|
| 241 | + } |
|
| 242 | + return false; |
|
| 243 | 243 | } |
| 244 | 244 | |
| 245 | 245 | /** |
@@ -249,19 +249,19 @@ discard block |
||
| 249 | 249 | * @return array |
| 250 | 250 | */ |
| 251 | 251 | function auth_spip_formulaire_login($flux) { |
| 252 | - // javascript qui gere la securite du login en evitant de faire circuler le pass en clair |
|
| 253 | - $js = file_get_contents(find_in_path('prive/javascript/login.js')); |
|
| 254 | - $flux['data'] .= |
|
| 255 | - '<script type="text/javascript">/*<![CDATA[*/' |
|
| 256 | - . "$js\n" |
|
| 257 | - . "var login_info={'login':'" . $flux['args']['contexte']['var_login'] . "'," |
|
| 258 | - . "'page_auteur': '" . generer_url_public('informer_auteur') . "'," |
|
| 259 | - . "'informe_auteur_en_cours':false," |
|
| 260 | - . "'attente_informe':0};" |
|
| 261 | - . "jQuery(function(){jQuery('#var_login').change(actualise_auteur);});" |
|
| 262 | - . '/*]]>*/</script>'; |
|
| 263 | - |
|
| 264 | - return $flux; |
|
| 252 | + // javascript qui gere la securite du login en evitant de faire circuler le pass en clair |
|
| 253 | + $js = file_get_contents(find_in_path('prive/javascript/login.js')); |
|
| 254 | + $flux['data'] .= |
|
| 255 | + '<script type="text/javascript">/*<![CDATA[*/' |
|
| 256 | + . "$js\n" |
|
| 257 | + . "var login_info={'login':'" . $flux['args']['contexte']['var_login'] . "'," |
|
| 258 | + . "'page_auteur': '" . generer_url_public('informer_auteur') . "'," |
|
| 259 | + . "'informe_auteur_en_cours':false," |
|
| 260 | + . "'attente_informe':0};" |
|
| 261 | + . "jQuery(function(){jQuery('#var_login').change(actualise_auteur);});" |
|
| 262 | + . '/*]]>*/</script>'; |
|
| 263 | + |
|
| 264 | + return $flux; |
|
| 265 | 265 | } |
| 266 | 266 | |
| 267 | 267 | |
@@ -273,11 +273,11 @@ discard block |
||
| 273 | 273 | * toujours true pour un auteur cree dans SPIP |
| 274 | 274 | */ |
| 275 | 275 | function auth_spip_autoriser_modifier_login(string $serveur = ''): bool { |
| 276 | - // les fonctions d'ecriture sur base distante sont encore incompletes |
|
| 277 | - if (strlen($serveur)) { |
|
| 278 | - return false; |
|
| 279 | - } |
|
| 280 | - return true; |
|
| 276 | + // les fonctions d'ecriture sur base distante sont encore incompletes |
|
| 277 | + if (strlen($serveur)) { |
|
| 278 | + return false; |
|
| 279 | + } |
|
| 280 | + return true; |
|
| 281 | 281 | } |
| 282 | 282 | |
| 283 | 283 | /** |
@@ -291,25 +291,25 @@ discard block |
||
| 291 | 291 | * message d'erreur si login non valide, chaine vide sinon |
| 292 | 292 | */ |
| 293 | 293 | function auth_spip_verifier_login($new_login, $id_auteur = 0, $serveur = '') { |
| 294 | - // login et mot de passe |
|
| 295 | - if (strlen($new_login)) { |
|
| 296 | - if (strlen($new_login) < _LOGIN_TROP_COURT) { |
|
| 297 | - return _T('info_login_trop_court_car_pluriel', ['nb' => _LOGIN_TROP_COURT]); |
|
| 298 | - } else { |
|
| 299 | - $n = sql_countsel( |
|
| 300 | - 'spip_auteurs', |
|
| 301 | - 'login=' . sql_quote($new_login) . ' AND id_auteur!=' . intval($id_auteur) . " AND statut!='5poubelle'", |
|
| 302 | - '', |
|
| 303 | - '', |
|
| 304 | - $serveur |
|
| 305 | - ); |
|
| 306 | - if ($n) { |
|
| 307 | - return _T('info_login_existant'); |
|
| 308 | - } |
|
| 309 | - } |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - return ''; |
|
| 294 | + // login et mot de passe |
|
| 295 | + if (strlen($new_login)) { |
|
| 296 | + if (strlen($new_login) < _LOGIN_TROP_COURT) { |
|
| 297 | + return _T('info_login_trop_court_car_pluriel', ['nb' => _LOGIN_TROP_COURT]); |
|
| 298 | + } else { |
|
| 299 | + $n = sql_countsel( |
|
| 300 | + 'spip_auteurs', |
|
| 301 | + 'login=' . sql_quote($new_login) . ' AND id_auteur!=' . intval($id_auteur) . " AND statut!='5poubelle'", |
|
| 302 | + '', |
|
| 303 | + '', |
|
| 304 | + $serveur |
|
| 305 | + ); |
|
| 306 | + if ($n) { |
|
| 307 | + return _T('info_login_existant'); |
|
| 308 | + } |
|
| 309 | + } |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + return ''; |
|
| 313 | 313 | } |
| 314 | 314 | |
| 315 | 315 | /** |
@@ -321,41 +321,41 @@ discard block |
||
| 321 | 321 | * @return bool |
| 322 | 322 | */ |
| 323 | 323 | function auth_spip_modifier_login($new_login, $id_auteur, $serveur = '') { |
| 324 | - if (is_null($new_login) or auth_spip_verifier_login($new_login, $id_auteur, $serveur) != '') { |
|
| 325 | - return false; |
|
| 326 | - } |
|
| 327 | - if ( |
|
| 328 | - !$id_auteur = intval($id_auteur) |
|
| 329 | - or !$auteur = sql_fetsel('login', 'spip_auteurs', 'id_auteur=' . intval($id_auteur), '', '', '', '', $serveur) |
|
| 330 | - ) { |
|
| 331 | - return false; |
|
| 332 | - } |
|
| 333 | - if ($new_login == $auteur['login']) { |
|
| 334 | - return true; |
|
| 335 | - } // on a rien fait mais c'est bon ! |
|
| 336 | - |
|
| 337 | - include_spip('action/editer_auteur'); |
|
| 338 | - |
|
| 339 | - // vider le login des auteurs a la poubelle qui avaient ce meme login |
|
| 340 | - if (strlen($new_login)) { |
|
| 341 | - $anciens = sql_allfetsel( |
|
| 342 | - 'id_auteur', |
|
| 343 | - 'spip_auteurs', |
|
| 344 | - 'login=' . sql_quote($new_login, $serveur, 'text') . " AND statut='5poubelle'", |
|
| 345 | - '', |
|
| 346 | - '', |
|
| 347 | - '', |
|
| 348 | - '', |
|
| 349 | - $serveur |
|
| 350 | - ); |
|
| 351 | - while ($row = array_pop($anciens)) { |
|
| 352 | - auteur_modifier($row['id_auteur'], ['login' => ''], true); // manque la gestion de $serveur |
|
| 353 | - } |
|
| 354 | - } |
|
| 355 | - |
|
| 356 | - auteur_modifier($id_auteur, ['login' => $new_login], true); // manque la gestion de $serveur |
|
| 357 | - |
|
| 358 | - return true; |
|
| 324 | + if (is_null($new_login) or auth_spip_verifier_login($new_login, $id_auteur, $serveur) != '') { |
|
| 325 | + return false; |
|
| 326 | + } |
|
| 327 | + if ( |
|
| 328 | + !$id_auteur = intval($id_auteur) |
|
| 329 | + or !$auteur = sql_fetsel('login', 'spip_auteurs', 'id_auteur=' . intval($id_auteur), '', '', '', '', $serveur) |
|
| 330 | + ) { |
|
| 331 | + return false; |
|
| 332 | + } |
|
| 333 | + if ($new_login == $auteur['login']) { |
|
| 334 | + return true; |
|
| 335 | + } // on a rien fait mais c'est bon ! |
|
| 336 | + |
|
| 337 | + include_spip('action/editer_auteur'); |
|
| 338 | + |
|
| 339 | + // vider le login des auteurs a la poubelle qui avaient ce meme login |
|
| 340 | + if (strlen($new_login)) { |
|
| 341 | + $anciens = sql_allfetsel( |
|
| 342 | + 'id_auteur', |
|
| 343 | + 'spip_auteurs', |
|
| 344 | + 'login=' . sql_quote($new_login, $serveur, 'text') . " AND statut='5poubelle'", |
|
| 345 | + '', |
|
| 346 | + '', |
|
| 347 | + '', |
|
| 348 | + '', |
|
| 349 | + $serveur |
|
| 350 | + ); |
|
| 351 | + while ($row = array_pop($anciens)) { |
|
| 352 | + auteur_modifier($row['id_auteur'], ['login' => ''], true); // manque la gestion de $serveur |
|
| 353 | + } |
|
| 354 | + } |
|
| 355 | + |
|
| 356 | + auteur_modifier($id_auteur, ['login' => $new_login], true); // manque la gestion de $serveur |
|
| 357 | + |
|
| 358 | + return true; |
|
| 359 | 359 | } |
| 360 | 360 | |
| 361 | 361 | /** |
@@ -367,44 +367,44 @@ discard block |
||
| 367 | 367 | * @return string |
| 368 | 368 | */ |
| 369 | 369 | function auth_spip_retrouver_login($login, $serveur = '') { |
| 370 | - if (!strlen($login)) { |
|
| 371 | - return null; |
|
| 372 | - } // pas la peine de requeter |
|
| 373 | - $l = sql_quote($login, $serveur, 'text'); |
|
| 374 | - if ( |
|
| 375 | - $r = sql_getfetsel( |
|
| 376 | - 'login', |
|
| 377 | - 'spip_auteurs', |
|
| 378 | - "statut<>'5poubelle'" . |
|
| 379 | - ' AND (length(pass)>0)' . |
|
| 380 | - " AND (login=$l)", |
|
| 381 | - '', |
|
| 382 | - '', |
|
| 383 | - '', |
|
| 384 | - '', |
|
| 385 | - $serveur |
|
| 386 | - ) |
|
| 387 | - ) { |
|
| 388 | - return $r; |
|
| 389 | - } |
|
| 390 | - // Si pas d'auteur avec ce login |
|
| 391 | - // regarder s'il a saisi son nom ou son mail. |
|
| 392 | - // Ne pas fusionner avec la requete precedente |
|
| 393 | - // car un nom peut etre homonyme d'un autre login |
|
| 394 | - else { |
|
| 395 | - return sql_getfetsel( |
|
| 396 | - 'login', |
|
| 397 | - 'spip_auteurs', |
|
| 398 | - "statut<>'5poubelle'" . |
|
| 399 | - ' AND (length(pass)>0)' . |
|
| 400 | - " AND (login<>'' AND (nom=$l OR email=$l))", |
|
| 401 | - '', |
|
| 402 | - '', |
|
| 403 | - '', |
|
| 404 | - '', |
|
| 405 | - $serveur |
|
| 406 | - ); |
|
| 407 | - } |
|
| 370 | + if (!strlen($login)) { |
|
| 371 | + return null; |
|
| 372 | + } // pas la peine de requeter |
|
| 373 | + $l = sql_quote($login, $serveur, 'text'); |
|
| 374 | + if ( |
|
| 375 | + $r = sql_getfetsel( |
|
| 376 | + 'login', |
|
| 377 | + 'spip_auteurs', |
|
| 378 | + "statut<>'5poubelle'" . |
|
| 379 | + ' AND (length(pass)>0)' . |
|
| 380 | + " AND (login=$l)", |
|
| 381 | + '', |
|
| 382 | + '', |
|
| 383 | + '', |
|
| 384 | + '', |
|
| 385 | + $serveur |
|
| 386 | + ) |
|
| 387 | + ) { |
|
| 388 | + return $r; |
|
| 389 | + } |
|
| 390 | + // Si pas d'auteur avec ce login |
|
| 391 | + // regarder s'il a saisi son nom ou son mail. |
|
| 392 | + // Ne pas fusionner avec la requete precedente |
|
| 393 | + // car un nom peut etre homonyme d'un autre login |
|
| 394 | + else { |
|
| 395 | + return sql_getfetsel( |
|
| 396 | + 'login', |
|
| 397 | + 'spip_auteurs', |
|
| 398 | + "statut<>'5poubelle'" . |
|
| 399 | + ' AND (length(pass)>0)' . |
|
| 400 | + " AND (login<>'' AND (nom=$l OR email=$l))", |
|
| 401 | + '', |
|
| 402 | + '', |
|
| 403 | + '', |
|
| 404 | + '', |
|
| 405 | + $serveur |
|
| 406 | + ); |
|
| 407 | + } |
|
| 408 | 408 | } |
| 409 | 409 | |
| 410 | 410 | /** |
@@ -415,11 +415,11 @@ discard block |
||
| 415 | 415 | * toujours true pour un auteur cree dans SPIP |
| 416 | 416 | */ |
| 417 | 417 | function auth_spip_autoriser_modifier_pass(string $serveur = ''): bool { |
| 418 | - // les fonctions d'ecriture sur base distante sont encore incompletes |
|
| 419 | - if (strlen($serveur)) { |
|
| 420 | - return false; |
|
| 421 | - } |
|
| 422 | - return true; |
|
| 418 | + // les fonctions d'ecriture sur base distante sont encore incompletes |
|
| 419 | + if (strlen($serveur)) { |
|
| 420 | + return false; |
|
| 421 | + } |
|
| 422 | + return true; |
|
| 423 | 423 | } |
| 424 | 424 | |
| 425 | 425 | |
@@ -440,12 +440,12 @@ discard block |
||
| 440 | 440 | * message d'erreur si login non valide, chaine vide sinon |
| 441 | 441 | */ |
| 442 | 442 | function auth_spip_verifier_pass($login, #[\SensitiveParameter] $new_pass, $id_auteur = 0, $serveur = '') { |
| 443 | - // login et mot de passe |
|
| 444 | - if (strlen($new_pass) < _PASS_LONGUEUR_MINI) { |
|
| 445 | - return _T('info_passe_trop_court_car_pluriel', ['nb' => _PASS_LONGUEUR_MINI]); |
|
| 446 | - } |
|
| 443 | + // login et mot de passe |
|
| 444 | + if (strlen($new_pass) < _PASS_LONGUEUR_MINI) { |
|
| 445 | + return _T('info_passe_trop_court_car_pluriel', ['nb' => _PASS_LONGUEUR_MINI]); |
|
| 446 | + } |
|
| 447 | 447 | |
| 448 | - return ''; |
|
| 448 | + return ''; |
|
| 449 | 449 | } |
| 450 | 450 | |
| 451 | 451 | /** |
@@ -459,47 +459,47 @@ discard block |
||
| 459 | 459 | * @return bool |
| 460 | 460 | */ |
| 461 | 461 | function auth_spip_modifier_pass($login, #[\SensitiveParameter] $new_pass, $id_auteur, $serveur = '') { |
| 462 | - if (is_null($new_pass) or auth_spip_verifier_pass($login, $new_pass, $id_auteur, $serveur) != '') { |
|
| 463 | - return false; |
|
| 464 | - } |
|
| 465 | - |
|
| 466 | - if ( |
|
| 467 | - !$id_auteur = intval($id_auteur) |
|
| 468 | - or !$auteur = sql_fetsel('login, statut, webmestre', 'spip_auteurs', 'id_auteur=' . intval($id_auteur), '', '', '', '', $serveur) |
|
| 469 | - ) { |
|
| 470 | - return false; |
|
| 471 | - } |
|
| 472 | - |
|
| 473 | - $cles = SpipCles::instance(); |
|
| 474 | - $secret = $cles->getSecretAuth(); |
|
| 475 | - if (!$secret) { |
|
| 476 | - if (auth_spip_initialiser_secret()) { |
|
| 477 | - $secret = $cles->getSecretAuth(); |
|
| 478 | - } |
|
| 479 | - else { |
|
| 480 | - return false; |
|
| 481 | - } |
|
| 482 | - } |
|
| 483 | - |
|
| 484 | - |
|
| 485 | - include_spip('inc/acces'); |
|
| 486 | - $set = [ |
|
| 487 | - 'pass' => Password::hacher($new_pass, $secret), |
|
| 488 | - 'htpass' => generer_htpass($new_pass), |
|
| 489 | - 'alea_actuel' => creer_uniqid(), // @deprecated 4.1 |
|
| 490 | - 'alea_futur' => creer_uniqid(), // @deprecated 4.1 |
|
| 491 | - 'low_sec' => '', |
|
| 492 | - ]; |
|
| 493 | - |
|
| 494 | - // si c'est un webmestre, on met a jour son backup des cles |
|
| 495 | - if ($auteur['statut'] === '0minirezo' and $auteur['webmestre'] === 'oui') { |
|
| 496 | - $set['backup_cles'] = $cles->backup($new_pass); |
|
| 497 | - } |
|
| 498 | - |
|
| 499 | - include_spip('action/editer_auteur'); |
|
| 500 | - auteur_modifier($id_auteur, $set, true); // manque la gestion de $serveur |
|
| 501 | - |
|
| 502 | - return true; // on a bien modifie le pass |
|
| 462 | + if (is_null($new_pass) or auth_spip_verifier_pass($login, $new_pass, $id_auteur, $serveur) != '') { |
|
| 463 | + return false; |
|
| 464 | + } |
|
| 465 | + |
|
| 466 | + if ( |
|
| 467 | + !$id_auteur = intval($id_auteur) |
|
| 468 | + or !$auteur = sql_fetsel('login, statut, webmestre', 'spip_auteurs', 'id_auteur=' . intval($id_auteur), '', '', '', '', $serveur) |
|
| 469 | + ) { |
|
| 470 | + return false; |
|
| 471 | + } |
|
| 472 | + |
|
| 473 | + $cles = SpipCles::instance(); |
|
| 474 | + $secret = $cles->getSecretAuth(); |
|
| 475 | + if (!$secret) { |
|
| 476 | + if (auth_spip_initialiser_secret()) { |
|
| 477 | + $secret = $cles->getSecretAuth(); |
|
| 478 | + } |
|
| 479 | + else { |
|
| 480 | + return false; |
|
| 481 | + } |
|
| 482 | + } |
|
| 483 | + |
|
| 484 | + |
|
| 485 | + include_spip('inc/acces'); |
|
| 486 | + $set = [ |
|
| 487 | + 'pass' => Password::hacher($new_pass, $secret), |
|
| 488 | + 'htpass' => generer_htpass($new_pass), |
|
| 489 | + 'alea_actuel' => creer_uniqid(), // @deprecated 4.1 |
|
| 490 | + 'alea_futur' => creer_uniqid(), // @deprecated 4.1 |
|
| 491 | + 'low_sec' => '', |
|
| 492 | + ]; |
|
| 493 | + |
|
| 494 | + // si c'est un webmestre, on met a jour son backup des cles |
|
| 495 | + if ($auteur['statut'] === '0minirezo' and $auteur['webmestre'] === 'oui') { |
|
| 496 | + $set['backup_cles'] = $cles->backup($new_pass); |
|
| 497 | + } |
|
| 498 | + |
|
| 499 | + include_spip('action/editer_auteur'); |
|
| 500 | + auteur_modifier($id_auteur, $set, true); // manque la gestion de $serveur |
|
| 501 | + |
|
| 502 | + return true; // on a bien modifie le pass |
|
| 503 | 503 | } |
| 504 | 504 | |
| 505 | 505 | /** |
@@ -513,58 +513,58 @@ discard block |
||
| 513 | 513 | * @return void |
| 514 | 514 | */ |
| 515 | 515 | function auth_spip_synchroniser_distant($id_auteur, $champs, $options = [], string $serveur = ''): void { |
| 516 | - // ne rien faire pour une base distante : on ne sait pas regenerer les htaccess |
|
| 517 | - if (strlen($serveur)) { |
|
| 518 | - return; |
|
| 519 | - } |
|
| 520 | - // si un login, pass ou statut a ete modifie |
|
| 521 | - // regenerer les fichier htpass |
|
| 522 | - if ( |
|
| 523 | - isset($champs['login']) |
|
| 524 | - or isset($champs['pass']) |
|
| 525 | - or isset($champs['statut']) |
|
| 526 | - or (isset($options['all']) and $options['all']) |
|
| 527 | - ) { |
|
| 528 | - $htaccess = _DIR_RESTREINT . _ACCESS_FILE_NAME; |
|
| 529 | - $htpasswd = _DIR_TMP . _AUTH_USER_FILE; |
|
| 530 | - |
|
| 531 | - // Cette variable de configuration peut etre posee par un plugin |
|
| 532 | - // par exemple acces_restreint ; |
|
| 533 | - // si .htaccess existe, outrepasser spip_meta |
|
| 534 | - if ( |
|
| 535 | - (!isset($GLOBALS['meta']['creer_htpasswd']) or ($GLOBALS['meta']['creer_htpasswd'] != 'oui')) |
|
| 536 | - and !@file_exists($htaccess) |
|
| 537 | - ) { |
|
| 538 | - spip_unlink($htpasswd); |
|
| 539 | - spip_unlink($htpasswd . '-admin'); |
|
| 540 | - |
|
| 541 | - return; |
|
| 542 | - } |
|
| 543 | - |
|
| 544 | - # remarque : ici on laisse passer les "nouveau" de maniere a leur permettre |
|
| 545 | - # de devenir redacteur le cas echeant (auth http)... a nettoyer |
|
| 546 | - // attention, il faut au prealable se connecter a la base (necessaire car utilise par install) |
|
| 547 | - |
|
| 548 | - $p1 = ''; // login:htpass pour tous |
|
| 549 | - $p2 = ''; // login:htpass pour les admins |
|
| 550 | - $s = sql_select( |
|
| 551 | - 'login, htpass, statut', |
|
| 552 | - 'spip_auteurs', |
|
| 553 | - sql_in('statut', ['1comite', '0minirezo', 'nouveau']) |
|
| 554 | - ); |
|
| 555 | - while ($t = sql_fetch($s)) { |
|
| 556 | - if (strlen($t['login']) and strlen($t['htpass'])) { |
|
| 557 | - $p1 .= $t['login'] . ':' . $t['htpass'] . "\n"; |
|
| 558 | - if ($t['statut'] == '0minirezo') { |
|
| 559 | - $p2 .= $t['login'] . ':' . $t['htpass'] . "\n"; |
|
| 560 | - } |
|
| 561 | - } |
|
| 562 | - } |
|
| 563 | - sql_free($s); |
|
| 564 | - if ($p1) { |
|
| 565 | - ecrire_fichier($htpasswd, $p1); |
|
| 566 | - ecrire_fichier($htpasswd . '-admin', $p2); |
|
| 567 | - spip_log("Ecriture de $htpasswd et $htpasswd-admin"); |
|
| 568 | - } |
|
| 569 | - } |
|
| 516 | + // ne rien faire pour une base distante : on ne sait pas regenerer les htaccess |
|
| 517 | + if (strlen($serveur)) { |
|
| 518 | + return; |
|
| 519 | + } |
|
| 520 | + // si un login, pass ou statut a ete modifie |
|
| 521 | + // regenerer les fichier htpass |
|
| 522 | + if ( |
|
| 523 | + isset($champs['login']) |
|
| 524 | + or isset($champs['pass']) |
|
| 525 | + or isset($champs['statut']) |
|
| 526 | + or (isset($options['all']) and $options['all']) |
|
| 527 | + ) { |
|
| 528 | + $htaccess = _DIR_RESTREINT . _ACCESS_FILE_NAME; |
|
| 529 | + $htpasswd = _DIR_TMP . _AUTH_USER_FILE; |
|
| 530 | + |
|
| 531 | + // Cette variable de configuration peut etre posee par un plugin |
|
| 532 | + // par exemple acces_restreint ; |
|
| 533 | + // si .htaccess existe, outrepasser spip_meta |
|
| 534 | + if ( |
|
| 535 | + (!isset($GLOBALS['meta']['creer_htpasswd']) or ($GLOBALS['meta']['creer_htpasswd'] != 'oui')) |
|
| 536 | + and !@file_exists($htaccess) |
|
| 537 | + ) { |
|
| 538 | + spip_unlink($htpasswd); |
|
| 539 | + spip_unlink($htpasswd . '-admin'); |
|
| 540 | + |
|
| 541 | + return; |
|
| 542 | + } |
|
| 543 | + |
|
| 544 | + # remarque : ici on laisse passer les "nouveau" de maniere a leur permettre |
|
| 545 | + # de devenir redacteur le cas echeant (auth http)... a nettoyer |
|
| 546 | + // attention, il faut au prealable se connecter a la base (necessaire car utilise par install) |
|
| 547 | + |
|
| 548 | + $p1 = ''; // login:htpass pour tous |
|
| 549 | + $p2 = ''; // login:htpass pour les admins |
|
| 550 | + $s = sql_select( |
|
| 551 | + 'login, htpass, statut', |
|
| 552 | + 'spip_auteurs', |
|
| 553 | + sql_in('statut', ['1comite', '0minirezo', 'nouveau']) |
|
| 554 | + ); |
|
| 555 | + while ($t = sql_fetch($s)) { |
|
| 556 | + if (strlen($t['login']) and strlen($t['htpass'])) { |
|
| 557 | + $p1 .= $t['login'] . ':' . $t['htpass'] . "\n"; |
|
| 558 | + if ($t['statut'] == '0minirezo') { |
|
| 559 | + $p2 .= $t['login'] . ':' . $t['htpass'] . "\n"; |
|
| 560 | + } |
|
| 561 | + } |
|
| 562 | + } |
|
| 563 | + sql_free($s); |
|
| 564 | + if ($p1) { |
|
| 565 | + ecrire_fichier($htpasswd, $p1); |
|
| 566 | + ecrire_fichier($htpasswd . '-admin', $p2); |
|
| 567 | + spip_log("Ecriture de $htpasswd et $htpasswd-admin"); |
|
| 568 | + } |
|
| 569 | + } |
|
| 570 | 570 | } |
@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | **/ |
| 17 | 17 | |
| 18 | 18 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 19 | - return; |
|
| 19 | + return; |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | // Authentifie via LDAP et retourne la ligne SQL decrivant l'utilisateur si ok |
@@ -24,12 +24,12 @@ discard block |
||
| 24 | 24 | // Attributs LDAP correspondants a ceux de SPIP, notamment pour le login |
| 25 | 25 | // ne pas ecraser une definition perso dans mes_options |
| 26 | 26 | if (!isset($GLOBALS['ldap_attributes']) or !is_array($GLOBALS['ldap_attributes'])) { |
| 27 | - $GLOBALS['ldap_attributes'] = [ |
|
| 28 | - 'login' => ['sAMAccountName', 'uid', 'login', 'userid', 'cn', 'sn'], |
|
| 29 | - 'nom' => 'cn', |
|
| 30 | - 'email' => 'mail', |
|
| 31 | - 'bio' => 'description' |
|
| 32 | - ]; |
|
| 27 | + $GLOBALS['ldap_attributes'] = [ |
|
| 28 | + 'login' => ['sAMAccountName', 'uid', 'login', 'userid', 'cn', 'sn'], |
|
| 29 | + 'nom' => 'cn', |
|
| 30 | + 'email' => 'mail', |
|
| 31 | + 'bio' => 'description' |
|
| 32 | + ]; |
|
| 33 | 33 | } |
| 34 | 34 | |
| 35 | 35 | /** |
@@ -55,50 +55,50 @@ discard block |
||
| 55 | 55 | */ |
| 56 | 56 | function auth_ldap_dist($login, #[\SensitiveParameter] $pass, $serveur = '', $phpauth = false) { |
| 57 | 57 | |
| 58 | - #spip_log("ldap $login " . ($pass ? "mdp fourni" : "mdp absent")); |
|
| 59 | - |
|
| 60 | - // Utilisateur connu ? |
|
| 61 | - // si http auth, inutile de reauthentifier: cela |
|
| 62 | - // ne marchera pas avec auth http autre que basic. |
|
| 63 | - $checkpass = isset($_SERVER['REMOTE_USER']) ? false : true; |
|
| 64 | - if (!($dn = auth_ldap_search($login, $pass, $checkpass, $serveur))) { |
|
| 65 | - return []; |
|
| 66 | - } |
|
| 67 | - $credentials_ldap = ['ldap_dn' => $dn, 'ldap_password' => $pass]; |
|
| 68 | - |
|
| 69 | - // Si l'utilisateur figure deja dans la base, y recuperer les infos |
|
| 70 | - $r = sql_fetsel('*', 'spip_auteurs', 'login=' . sql_quote($login) . " AND source='ldap'", '', '', '', '', $serveur); |
|
| 71 | - |
|
| 72 | - if ($r) { |
|
| 73 | - return array_merge($r, $credentials_ldap); |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - // sinon importer les infos depuis LDAP, |
|
| 77 | - |
|
| 78 | - if ( |
|
| 79 | - $GLOBALS['meta']['ldap_statut_import'] |
|
| 80 | - and $desc = auth_ldap_retrouver($dn, [], $serveur) |
|
| 81 | - ) { |
|
| 82 | - // rajouter le statut indique a l'install |
|
| 83 | - $desc['statut'] = $GLOBALS['meta']['ldap_statut_import']; |
|
| 84 | - $desc['login'] = $login; |
|
| 85 | - $desc['source'] = 'ldap'; |
|
| 86 | - $desc['pass'] = ''; |
|
| 87 | - |
|
| 88 | - $r = sql_insertq('spip_auteurs', $desc, [], $serveur); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - if ($r) { |
|
| 92 | - return array_merge( |
|
| 93 | - $credentials_ldap, |
|
| 94 | - sql_fetsel('*', 'spip_auteurs', 'id_auteur=' . intval($r), '', '', '', '', $serveur) |
|
| 95 | - ); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - // sinon echec |
|
| 99 | - spip_log("Creation de l'auteur '$login' impossible"); |
|
| 100 | - |
|
| 101 | - return []; |
|
| 58 | + #spip_log("ldap $login " . ($pass ? "mdp fourni" : "mdp absent")); |
|
| 59 | + |
|
| 60 | + // Utilisateur connu ? |
|
| 61 | + // si http auth, inutile de reauthentifier: cela |
|
| 62 | + // ne marchera pas avec auth http autre que basic. |
|
| 63 | + $checkpass = isset($_SERVER['REMOTE_USER']) ? false : true; |
|
| 64 | + if (!($dn = auth_ldap_search($login, $pass, $checkpass, $serveur))) { |
|
| 65 | + return []; |
|
| 66 | + } |
|
| 67 | + $credentials_ldap = ['ldap_dn' => $dn, 'ldap_password' => $pass]; |
|
| 68 | + |
|
| 69 | + // Si l'utilisateur figure deja dans la base, y recuperer les infos |
|
| 70 | + $r = sql_fetsel('*', 'spip_auteurs', 'login=' . sql_quote($login) . " AND source='ldap'", '', '', '', '', $serveur); |
|
| 71 | + |
|
| 72 | + if ($r) { |
|
| 73 | + return array_merge($r, $credentials_ldap); |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + // sinon importer les infos depuis LDAP, |
|
| 77 | + |
|
| 78 | + if ( |
|
| 79 | + $GLOBALS['meta']['ldap_statut_import'] |
|
| 80 | + and $desc = auth_ldap_retrouver($dn, [], $serveur) |
|
| 81 | + ) { |
|
| 82 | + // rajouter le statut indique a l'install |
|
| 83 | + $desc['statut'] = $GLOBALS['meta']['ldap_statut_import']; |
|
| 84 | + $desc['login'] = $login; |
|
| 85 | + $desc['source'] = 'ldap'; |
|
| 86 | + $desc['pass'] = ''; |
|
| 87 | + |
|
| 88 | + $r = sql_insertq('spip_auteurs', $desc, [], $serveur); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + if ($r) { |
|
| 92 | + return array_merge( |
|
| 93 | + $credentials_ldap, |
|
| 94 | + sql_fetsel('*', 'spip_auteurs', 'id_auteur=' . intval($r), '', '', '', '', $serveur) |
|
| 95 | + ); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + // sinon echec |
|
| 99 | + spip_log("Creation de l'auteur '$login' impossible"); |
|
| 100 | + |
|
| 101 | + return []; |
|
| 102 | 102 | } |
| 103 | 103 | |
| 104 | 104 | /** |
@@ -112,36 +112,36 @@ discard block |
||
| 112 | 112 | * @return array |
| 113 | 113 | */ |
| 114 | 114 | function auth_ldap_connect($serveur = '') { |
| 115 | - include_spip('base/connect_sql'); |
|
| 116 | - static $connexions_ldap = []; |
|
| 117 | - if (isset($connexions_ldap[$serveur])) { |
|
| 118 | - return $connexions_ldap[$serveur]; |
|
| 119 | - } |
|
| 120 | - $connexion = spip_connect($serveur); |
|
| 121 | - if (!is_array($connexion['ldap'])) { |
|
| 122 | - if ($connexion['authentification']['ldap']) { |
|
| 123 | - $f = _DIR_CONNECT . $connexion['authentification']['ldap']; |
|
| 124 | - unset($GLOBALS['ldap_link']); |
|
| 125 | - if (is_readable($f)) { |
|
| 126 | - include_once($f); |
|
| 127 | - }; |
|
| 128 | - if (isset($GLOBALS['ldap_link'])) { |
|
| 129 | - $connexion['ldap'] = [ |
|
| 130 | - 'link' => $GLOBALS['ldap_link'], |
|
| 131 | - 'base' => $GLOBALS['ldap_base'] |
|
| 132 | - ]; |
|
| 133 | - } else { |
|
| 134 | - spip_log("connection LDAP $serveur mal definie dans $f"); |
|
| 135 | - } |
|
| 136 | - if (isset($GLOBALS['ldap_champs'])) { |
|
| 137 | - $connexion['ldap']['attributes'] = $GLOBALS['ldap_champs']; |
|
| 138 | - } |
|
| 139 | - } else { |
|
| 140 | - spip_log("connection LDAP $serveur inconnue"); |
|
| 141 | - } |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - return $connexions_ldap[$serveur] = $connexion['ldap']; |
|
| 115 | + include_spip('base/connect_sql'); |
|
| 116 | + static $connexions_ldap = []; |
|
| 117 | + if (isset($connexions_ldap[$serveur])) { |
|
| 118 | + return $connexions_ldap[$serveur]; |
|
| 119 | + } |
|
| 120 | + $connexion = spip_connect($serveur); |
|
| 121 | + if (!is_array($connexion['ldap'])) { |
|
| 122 | + if ($connexion['authentification']['ldap']) { |
|
| 123 | + $f = _DIR_CONNECT . $connexion['authentification']['ldap']; |
|
| 124 | + unset($GLOBALS['ldap_link']); |
|
| 125 | + if (is_readable($f)) { |
|
| 126 | + include_once($f); |
|
| 127 | + }; |
|
| 128 | + if (isset($GLOBALS['ldap_link'])) { |
|
| 129 | + $connexion['ldap'] = [ |
|
| 130 | + 'link' => $GLOBALS['ldap_link'], |
|
| 131 | + 'base' => $GLOBALS['ldap_base'] |
|
| 132 | + ]; |
|
| 133 | + } else { |
|
| 134 | + spip_log("connection LDAP $serveur mal definie dans $f"); |
|
| 135 | + } |
|
| 136 | + if (isset($GLOBALS['ldap_champs'])) { |
|
| 137 | + $connexion['ldap']['attributes'] = $GLOBALS['ldap_champs']; |
|
| 138 | + } |
|
| 139 | + } else { |
|
| 140 | + spip_log("connection LDAP $serveur inconnue"); |
|
| 141 | + } |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + return $connexions_ldap[$serveur] = $connexion['ldap']; |
|
| 145 | 145 | } |
| 146 | 146 | |
| 147 | 147 | /** |
@@ -155,52 +155,52 @@ discard block |
||
| 155 | 155 | * Le login trouvé ou chaine vide si non trouvé |
| 156 | 156 | */ |
| 157 | 157 | function auth_ldap_search($login, #[\SensitiveParameter] $pass, $checkpass = true, $serveur = '') { |
| 158 | - // Securite anti-injection et contre un serveur LDAP laxiste |
|
| 159 | - $login_search = preg_replace('/[^-@._\s\d\w]/', '', $login); |
|
| 160 | - if (!strlen($login_search) or ($checkpass and !strlen($pass))) { |
|
| 161 | - return ''; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - // verifier la connexion |
|
| 165 | - if (!$ldap = auth_ldap_connect($serveur)) { |
|
| 166 | - return ''; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - $ldap_link = $ldap['link'] ?? null; |
|
| 170 | - $ldap_base = $ldap['base'] ?? null; |
|
| 171 | - $desc = !empty($ldap['attributes']) ? $ldap['attributes'] : $GLOBALS['ldap_attributes']; |
|
| 172 | - |
|
| 173 | - $logins = is_array($desc['login']) ? $desc['login'] : [$desc['login']]; |
|
| 174 | - |
|
| 175 | - // Tenter une recherche pour essayer de retrouver le DN |
|
| 176 | - foreach ($logins as $att) { |
|
| 177 | - $result = @ldap_search($ldap_link, $ldap_base, "$att=$login_search", ['dn']); |
|
| 178 | - $info = @ldap_get_entries($ldap_link, $result); |
|
| 179 | - // Ne pas accepter les resultats si plus d'une entree |
|
| 180 | - // (on veut un attribut unique) |
|
| 181 | - |
|
| 182 | - if (is_array($info) and $info['count'] == 1) { |
|
| 183 | - $dn = $info[0]['dn']; |
|
| 184 | - if (!$checkpass) { |
|
| 185 | - return $dn; |
|
| 186 | - } |
|
| 187 | - if (@ldap_bind($ldap_link, $dn, $pass)) { |
|
| 188 | - return $dn; |
|
| 189 | - } |
|
| 190 | - } |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - if ($checkpass and !isset($dn)) { |
|
| 194 | - // Si echec, essayer de deviner le DN |
|
| 195 | - foreach ($logins as $att) { |
|
| 196 | - $dn = "$att=$login_search, $ldap_base"; |
|
| 197 | - if (@ldap_bind($ldap_link, $dn, $pass)) { |
|
| 198 | - return "$att=$login_search, $ldap_base"; |
|
| 199 | - } |
|
| 200 | - } |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - return ''; |
|
| 158 | + // Securite anti-injection et contre un serveur LDAP laxiste |
|
| 159 | + $login_search = preg_replace('/[^-@._\s\d\w]/', '', $login); |
|
| 160 | + if (!strlen($login_search) or ($checkpass and !strlen($pass))) { |
|
| 161 | + return ''; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + // verifier la connexion |
|
| 165 | + if (!$ldap = auth_ldap_connect($serveur)) { |
|
| 166 | + return ''; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + $ldap_link = $ldap['link'] ?? null; |
|
| 170 | + $ldap_base = $ldap['base'] ?? null; |
|
| 171 | + $desc = !empty($ldap['attributes']) ? $ldap['attributes'] : $GLOBALS['ldap_attributes']; |
|
| 172 | + |
|
| 173 | + $logins = is_array($desc['login']) ? $desc['login'] : [$desc['login']]; |
|
| 174 | + |
|
| 175 | + // Tenter une recherche pour essayer de retrouver le DN |
|
| 176 | + foreach ($logins as $att) { |
|
| 177 | + $result = @ldap_search($ldap_link, $ldap_base, "$att=$login_search", ['dn']); |
|
| 178 | + $info = @ldap_get_entries($ldap_link, $result); |
|
| 179 | + // Ne pas accepter les resultats si plus d'une entree |
|
| 180 | + // (on veut un attribut unique) |
|
| 181 | + |
|
| 182 | + if (is_array($info) and $info['count'] == 1) { |
|
| 183 | + $dn = $info[0]['dn']; |
|
| 184 | + if (!$checkpass) { |
|
| 185 | + return $dn; |
|
| 186 | + } |
|
| 187 | + if (@ldap_bind($ldap_link, $dn, $pass)) { |
|
| 188 | + return $dn; |
|
| 189 | + } |
|
| 190 | + } |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + if ($checkpass and !isset($dn)) { |
|
| 194 | + // Si echec, essayer de deviner le DN |
|
| 195 | + foreach ($logins as $att) { |
|
| 196 | + $dn = "$att=$login_search, $ldap_base"; |
|
| 197 | + if (@ldap_bind($ldap_link, $dn, $pass)) { |
|
| 198 | + return "$att=$login_search, $ldap_base"; |
|
| 199 | + } |
|
| 200 | + } |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + return ''; |
|
| 204 | 204 | } |
| 205 | 205 | |
| 206 | 206 | /** |
@@ -212,40 +212,40 @@ discard block |
||
| 212 | 212 | * @return array |
| 213 | 213 | */ |
| 214 | 214 | function auth_ldap_retrouver($dn, $desc = [], $serveur = '') { |
| 215 | - // Lire les infos sur l'utilisateur a partir de son DN depuis LDAP |
|
| 215 | + // Lire les infos sur l'utilisateur a partir de son DN depuis LDAP |
|
| 216 | 216 | |
| 217 | - if (!$ldap = auth_ldap_connect($serveur)) { |
|
| 218 | - spip_log("ldap $serveur injoignable"); |
|
| 217 | + if (!$ldap = auth_ldap_connect($serveur)) { |
|
| 218 | + spip_log("ldap $serveur injoignable"); |
|
| 219 | 219 | |
| 220 | - return []; |
|
| 221 | - } |
|
| 220 | + return []; |
|
| 221 | + } |
|
| 222 | 222 | |
| 223 | - $ldap_link = $ldap['link']; |
|
| 224 | - if (!$desc) { |
|
| 225 | - $desc = $ldap['attributes'] ?: $GLOBALS['ldap_attributes']; |
|
| 226 | - unset($desc['login']); |
|
| 227 | - } |
|
| 228 | - $result = @ldap_read($ldap_link, $dn, 'objectClass=*', array_values($desc)); |
|
| 223 | + $ldap_link = $ldap['link']; |
|
| 224 | + if (!$desc) { |
|
| 225 | + $desc = $ldap['attributes'] ?: $GLOBALS['ldap_attributes']; |
|
| 226 | + unset($desc['login']); |
|
| 227 | + } |
|
| 228 | + $result = @ldap_read($ldap_link, $dn, 'objectClass=*', array_values($desc)); |
|
| 229 | 229 | |
| 230 | - if (!$result) { |
|
| 231 | - return []; |
|
| 232 | - } |
|
| 230 | + if (!$result) { |
|
| 231 | + return []; |
|
| 232 | + } |
|
| 233 | 233 | |
| 234 | - // Recuperer les donnees du premier (unique?) compte de l'auteur |
|
| 235 | - $val = @ldap_get_entries($ldap_link, $result); |
|
| 236 | - if (!is_array($val) or !is_array($val[0])) { |
|
| 237 | - return []; |
|
| 238 | - } |
|
| 239 | - $val = $val[0]; |
|
| 234 | + // Recuperer les donnees du premier (unique?) compte de l'auteur |
|
| 235 | + $val = @ldap_get_entries($ldap_link, $result); |
|
| 236 | + if (!is_array($val) or !is_array($val[0])) { |
|
| 237 | + return []; |
|
| 238 | + } |
|
| 239 | + $val = $val[0]; |
|
| 240 | 240 | |
| 241 | - // Convertir depuis UTF-8 (jeu de caracteres par defaut) |
|
| 242 | - include_spip('inc/charsets'); |
|
| 241 | + // Convertir depuis UTF-8 (jeu de caracteres par defaut) |
|
| 242 | + include_spip('inc/charsets'); |
|
| 243 | 243 | |
| 244 | - foreach ($desc as $k => $v) { |
|
| 245 | - $desc[$k] = importer_charset($val[strtolower($v)][0], 'utf-8'); |
|
| 246 | - } |
|
| 244 | + foreach ($desc as $k => $v) { |
|
| 245 | + $desc[$k] = importer_charset($val[strtolower($v)][0], 'utf-8'); |
|
| 246 | + } |
|
| 247 | 247 | |
| 248 | - return $desc; |
|
| 248 | + return $desc; |
|
| 249 | 249 | } |
| 250 | 250 | |
| 251 | 251 | |
@@ -257,7 +257,7 @@ discard block |
||
| 257 | 257 | * @return string |
| 258 | 258 | */ |
| 259 | 259 | function auth_ldap_retrouver_login($login, $serveur = '') { |
| 260 | - return auth_ldap_search($login, '', false, $serveur) ? $login : ''; |
|
| 260 | + return auth_ldap_search($login, '', false, $serveur) ? $login : ''; |
|
| 261 | 261 | } |
| 262 | 262 | |
| 263 | 263 | /** |
@@ -277,9 +277,9 @@ discard block |
||
| 277 | 277 | * Message d'erreur si login non valide, chaîne vide sinon |
| 278 | 278 | */ |
| 279 | 279 | function auth_ldap_verifier_pass($login, #[\SensitiveParameter] $new_pass, $id_auteur = 0, $serveur = '') { |
| 280 | - include_spip('auth/spip'); |
|
| 280 | + include_spip('auth/spip'); |
|
| 281 | 281 | |
| 282 | - return auth_spip_verifier_pass($login, $new_pass, $id_auteur, $serveur); |
|
| 282 | + return auth_spip_verifier_pass($login, $new_pass, $id_auteur, $serveur); |
|
| 283 | 283 | } |
| 284 | 284 | |
| 285 | 285 | /** |
@@ -300,7 +300,7 @@ discard block |
||
| 300 | 300 | * ``` |
| 301 | 301 | */ |
| 302 | 302 | function auth_ldap_autoriser_modifier_pass($serveur = '') { |
| 303 | - return true; |
|
| 303 | + return true; |
|
| 304 | 304 | } |
| 305 | 305 | |
| 306 | 306 | /** |
@@ -318,23 +318,23 @@ discard block |
||
| 318 | 318 | * Informe du succès ou de l'echec du changement du mot de passe |
| 319 | 319 | */ |
| 320 | 320 | function auth_ldap_modifier_pass($login, #[\SensitiveParameter] $new_pass, $id_auteur, $serveur = '') { |
| 321 | - if (is_null($new_pass) or auth_ldap_verifier_pass($login, $new_pass, $id_auteur, $serveur) != '') { |
|
| 322 | - return false; |
|
| 323 | - } |
|
| 324 | - if (!$ldap = auth_ldap_connect($serveur)) { |
|
| 325 | - return false; |
|
| 326 | - } |
|
| 327 | - $link = $ldap['link']; |
|
| 328 | - include_spip('inc/session'); |
|
| 329 | - $dn = session_get('ldap_dn'); |
|
| 330 | - if ('' == $dn) { |
|
| 331 | - return false; |
|
| 332 | - } |
|
| 333 | - if (!ldap_bind($link, $dn, session_get('ldap_password'))) { |
|
| 334 | - return false; |
|
| 335 | - } |
|
| 336 | - $encoded_pass = '{MD5}' . base64_encode(pack('H*', md5($new_pass))); |
|
| 337 | - $success = ldap_mod_replace($link, $dn, ['userPassword' => $encoded_pass]); |
|
| 338 | - |
|
| 339 | - return $success; |
|
| 321 | + if (is_null($new_pass) or auth_ldap_verifier_pass($login, $new_pass, $id_auteur, $serveur) != '') { |
|
| 322 | + return false; |
|
| 323 | + } |
|
| 324 | + if (!$ldap = auth_ldap_connect($serveur)) { |
|
| 325 | + return false; |
|
| 326 | + } |
|
| 327 | + $link = $ldap['link']; |
|
| 328 | + include_spip('inc/session'); |
|
| 329 | + $dn = session_get('ldap_dn'); |
|
| 330 | + if ('' == $dn) { |
|
| 331 | + return false; |
|
| 332 | + } |
|
| 333 | + if (!ldap_bind($link, $dn, session_get('ldap_password'))) { |
|
| 334 | + return false; |
|
| 335 | + } |
|
| 336 | + $encoded_pass = '{MD5}' . base64_encode(pack('H*', md5($new_pass))); |
|
| 337 | + $success = ldap_mod_replace($link, $dn, ['userPassword' => $encoded_pass]); |
|
| 338 | + |
|
| 339 | + return $success; |
|
| 340 | 340 | } |
@@ -15,7 +15,7 @@ discard block |
||
| 15 | 15 | * @package SPIP\Core\SQL |
| 16 | 16 | **/ |
| 17 | 17 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 18 | - return; |
|
| 18 | + return; |
|
| 19 | 19 | } |
| 20 | 20 | require_once _ROOT_RESTREINT . 'base/objets.php'; |
| 21 | 21 | |
@@ -41,128 +41,128 @@ discard block |
||
| 41 | 41 | **/ |
| 42 | 42 | function spip_connect($serveur = '', $version = '') { |
| 43 | 43 | |
| 44 | - $serveur = !is_string($serveur) ? '' : strtolower($serveur); |
|
| 45 | - $index = $serveur ?: 0; |
|
| 46 | - if (!$version) { |
|
| 47 | - $version = $GLOBALS['spip_sql_version']; |
|
| 48 | - } |
|
| 49 | - if (isset($GLOBALS['connexions'][$index][$version])) { |
|
| 50 | - return $GLOBALS['connexions'][$index]; |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - include_spip('base/abstract_sql'); |
|
| 54 | - $install = (_request('exec') == 'install'); |
|
| 55 | - |
|
| 56 | - // Premiere connexion ? |
|
| 57 | - if (!($old = isset($GLOBALS['connexions'][$index]))) { |
|
| 58 | - $f = ''; |
|
| 59 | - if ($serveur) { |
|
| 60 | - // serveur externe et nom de serveur bien ecrit ? |
|
| 61 | - if (defined('_DIR_CONNECT') |
|
| 62 | - and preg_match('/^[\w\.]*$/', $serveur)) { |
|
| 63 | - $f = _DIR_CONNECT . $serveur . '.php'; |
|
| 64 | - if (!is_readable($f) and !$install) { |
|
| 65 | - // chercher une declaration de serveur dans le path |
|
| 66 | - // qui peut servir à des plugins à declarer des connexions à une base sqlite |
|
| 67 | - // Ex: sert aux boucles POUR et au plugin-dist dump pour se connecter sur le sqlite du dump |
|
| 68 | - $f = find_in_path("$serveur.php", 'connect/'); |
|
| 69 | - } |
|
| 70 | - } |
|
| 71 | - } |
|
| 72 | - else { |
|
| 73 | - if (defined('_FILE_CONNECT') and _FILE_CONNECT) { |
|
| 74 | - // init du serveur principal |
|
| 75 | - $f = _FILE_CONNECT; |
|
| 76 | - } |
|
| 77 | - elseif ($install and defined('_FILE_CONNECT_TMP')) { |
|
| 78 | - // installation en cours |
|
| 79 | - $f = _FILE_CONNECT_TMP; |
|
| 80 | - } |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - unset($GLOBALS['db_ok']); |
|
| 84 | - unset($GLOBALS['spip_connect_version']); |
|
| 85 | - if ($f and is_readable($f)) { |
|
| 86 | - include($f); |
|
| 87 | - if (!isset($GLOBALS['db_ok'])) { |
|
| 88 | - spip_log("spip_connect: fichier de connexion '$f' OK mais echec connexion au serveur", _LOG_HS); |
|
| 89 | - } |
|
| 90 | - } |
|
| 91 | - else { |
|
| 92 | - spip_log("spip_connect: fichier de connexion '$f' non trouve, pas de connexion serveur", _LOG_HS); |
|
| 93 | - } |
|
| 94 | - if (!isset($GLOBALS['db_ok'])) { |
|
| 95 | - // fera mieux la prochaine fois |
|
| 96 | - if ($install) { |
|
| 97 | - return false; |
|
| 98 | - } |
|
| 99 | - // ne plus reessayer si ce n'est pas l'install |
|
| 100 | - return $GLOBALS['connexions'][$index] = false; |
|
| 101 | - } |
|
| 102 | - $GLOBALS['connexions'][$index] = $GLOBALS['db_ok']; |
|
| 103 | - } |
|
| 104 | - // si la connexion a deja ete tentee mais a echoue, le dire! |
|
| 105 | - if (!$GLOBALS['connexions'][$index]) { |
|
| 106 | - return false; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - // la connexion a reussi ou etait deja faite. |
|
| 110 | - // chargement de la version du jeu de fonctions |
|
| 111 | - // si pas dans le fichier par defaut |
|
| 112 | - $type = $GLOBALS['db_ok']['type']; |
|
| 113 | - $jeu = 'spip_' . $type . '_functions_' . $version; |
|
| 114 | - if (!isset($GLOBALS[$jeu])) { |
|
| 115 | - if (!find_in_path($type . '_' . $version . '.php', 'req/', true)) { |
|
| 116 | - spip_log("spip_connect: serveur $index version '$version' non defini pour '$type'", _LOG_HS); |
|
| 117 | - |
|
| 118 | - // ne plus reessayer |
|
| 119 | - return $GLOBALS['connexions'][$index][$version] = []; |
|
| 120 | - } |
|
| 121 | - } |
|
| 122 | - $GLOBALS['connexions'][$index][$version] = $GLOBALS[$jeu]; |
|
| 123 | - if ($old) { |
|
| 124 | - return $GLOBALS['connexions'][$index]; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - $GLOBALS['connexions'][$index]['spip_connect_version'] = $GLOBALS['spip_connect_version'] ?? 0; |
|
| 128 | - |
|
| 129 | - // initialisation de l'alphabet utilise dans les connexions SQL |
|
| 130 | - // si l'installation l'a determine. |
|
| 131 | - // Celui du serveur principal l'impose aux serveurs secondaires |
|
| 132 | - // s'ils le connaissent |
|
| 133 | - |
|
| 134 | - if (!$serveur) { |
|
| 135 | - $charset = spip_connect_main($GLOBALS[$jeu], $GLOBALS['db_ok']['charset']); |
|
| 136 | - if (!$charset) { |
|
| 137 | - unset($GLOBALS['connexions'][$index]); |
|
| 138 | - spip_log('spip_connect: absence de charset', _LOG_AVERTISSEMENT); |
|
| 139 | - |
|
| 140 | - return false; |
|
| 141 | - } |
|
| 142 | - } else { |
|
| 143 | - if ($GLOBALS['db_ok']['charset']) { |
|
| 144 | - $charset = $GLOBALS['db_ok']['charset']; |
|
| 145 | - } |
|
| 146 | - // spip_meta n'existe pas toujours dans la base |
|
| 147 | - // C'est le cas d'un dump sqlite par exemple |
|
| 148 | - elseif ( |
|
| 149 | - $GLOBALS['connexions'][$index]['spip_connect_version'] |
|
| 150 | - and sql_showtable('spip_meta', true, $serveur) |
|
| 151 | - and $r = sql_getfetsel('valeur', 'spip_meta', "nom='charset_sql_connexion'", '', '', '', '', $serveur) |
|
| 152 | - ) { |
|
| 153 | - $charset = $r; |
|
| 154 | - } else { |
|
| 155 | - $charset = -1; |
|
| 156 | - } |
|
| 157 | - } |
|
| 158 | - if ($charset != -1) { |
|
| 159 | - $f = $GLOBALS[$jeu]['set_charset']; |
|
| 160 | - if (function_exists($f)) { |
|
| 161 | - $f($charset, $serveur); |
|
| 162 | - } |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - return $GLOBALS['connexions'][$index]; |
|
| 44 | + $serveur = !is_string($serveur) ? '' : strtolower($serveur); |
|
| 45 | + $index = $serveur ?: 0; |
|
| 46 | + if (!$version) { |
|
| 47 | + $version = $GLOBALS['spip_sql_version']; |
|
| 48 | + } |
|
| 49 | + if (isset($GLOBALS['connexions'][$index][$version])) { |
|
| 50 | + return $GLOBALS['connexions'][$index]; |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + include_spip('base/abstract_sql'); |
|
| 54 | + $install = (_request('exec') == 'install'); |
|
| 55 | + |
|
| 56 | + // Premiere connexion ? |
|
| 57 | + if (!($old = isset($GLOBALS['connexions'][$index]))) { |
|
| 58 | + $f = ''; |
|
| 59 | + if ($serveur) { |
|
| 60 | + // serveur externe et nom de serveur bien ecrit ? |
|
| 61 | + if (defined('_DIR_CONNECT') |
|
| 62 | + and preg_match('/^[\w\.]*$/', $serveur)) { |
|
| 63 | + $f = _DIR_CONNECT . $serveur . '.php'; |
|
| 64 | + if (!is_readable($f) and !$install) { |
|
| 65 | + // chercher une declaration de serveur dans le path |
|
| 66 | + // qui peut servir à des plugins à declarer des connexions à une base sqlite |
|
| 67 | + // Ex: sert aux boucles POUR et au plugin-dist dump pour se connecter sur le sqlite du dump |
|
| 68 | + $f = find_in_path("$serveur.php", 'connect/'); |
|
| 69 | + } |
|
| 70 | + } |
|
| 71 | + } |
|
| 72 | + else { |
|
| 73 | + if (defined('_FILE_CONNECT') and _FILE_CONNECT) { |
|
| 74 | + // init du serveur principal |
|
| 75 | + $f = _FILE_CONNECT; |
|
| 76 | + } |
|
| 77 | + elseif ($install and defined('_FILE_CONNECT_TMP')) { |
|
| 78 | + // installation en cours |
|
| 79 | + $f = _FILE_CONNECT_TMP; |
|
| 80 | + } |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + unset($GLOBALS['db_ok']); |
|
| 84 | + unset($GLOBALS['spip_connect_version']); |
|
| 85 | + if ($f and is_readable($f)) { |
|
| 86 | + include($f); |
|
| 87 | + if (!isset($GLOBALS['db_ok'])) { |
|
| 88 | + spip_log("spip_connect: fichier de connexion '$f' OK mais echec connexion au serveur", _LOG_HS); |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | + else { |
|
| 92 | + spip_log("spip_connect: fichier de connexion '$f' non trouve, pas de connexion serveur", _LOG_HS); |
|
| 93 | + } |
|
| 94 | + if (!isset($GLOBALS['db_ok'])) { |
|
| 95 | + // fera mieux la prochaine fois |
|
| 96 | + if ($install) { |
|
| 97 | + return false; |
|
| 98 | + } |
|
| 99 | + // ne plus reessayer si ce n'est pas l'install |
|
| 100 | + return $GLOBALS['connexions'][$index] = false; |
|
| 101 | + } |
|
| 102 | + $GLOBALS['connexions'][$index] = $GLOBALS['db_ok']; |
|
| 103 | + } |
|
| 104 | + // si la connexion a deja ete tentee mais a echoue, le dire! |
|
| 105 | + if (!$GLOBALS['connexions'][$index]) { |
|
| 106 | + return false; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + // la connexion a reussi ou etait deja faite. |
|
| 110 | + // chargement de la version du jeu de fonctions |
|
| 111 | + // si pas dans le fichier par defaut |
|
| 112 | + $type = $GLOBALS['db_ok']['type']; |
|
| 113 | + $jeu = 'spip_' . $type . '_functions_' . $version; |
|
| 114 | + if (!isset($GLOBALS[$jeu])) { |
|
| 115 | + if (!find_in_path($type . '_' . $version . '.php', 'req/', true)) { |
|
| 116 | + spip_log("spip_connect: serveur $index version '$version' non defini pour '$type'", _LOG_HS); |
|
| 117 | + |
|
| 118 | + // ne plus reessayer |
|
| 119 | + return $GLOBALS['connexions'][$index][$version] = []; |
|
| 120 | + } |
|
| 121 | + } |
|
| 122 | + $GLOBALS['connexions'][$index][$version] = $GLOBALS[$jeu]; |
|
| 123 | + if ($old) { |
|
| 124 | + return $GLOBALS['connexions'][$index]; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + $GLOBALS['connexions'][$index]['spip_connect_version'] = $GLOBALS['spip_connect_version'] ?? 0; |
|
| 128 | + |
|
| 129 | + // initialisation de l'alphabet utilise dans les connexions SQL |
|
| 130 | + // si l'installation l'a determine. |
|
| 131 | + // Celui du serveur principal l'impose aux serveurs secondaires |
|
| 132 | + // s'ils le connaissent |
|
| 133 | + |
|
| 134 | + if (!$serveur) { |
|
| 135 | + $charset = spip_connect_main($GLOBALS[$jeu], $GLOBALS['db_ok']['charset']); |
|
| 136 | + if (!$charset) { |
|
| 137 | + unset($GLOBALS['connexions'][$index]); |
|
| 138 | + spip_log('spip_connect: absence de charset', _LOG_AVERTISSEMENT); |
|
| 139 | + |
|
| 140 | + return false; |
|
| 141 | + } |
|
| 142 | + } else { |
|
| 143 | + if ($GLOBALS['db_ok']['charset']) { |
|
| 144 | + $charset = $GLOBALS['db_ok']['charset']; |
|
| 145 | + } |
|
| 146 | + // spip_meta n'existe pas toujours dans la base |
|
| 147 | + // C'est le cas d'un dump sqlite par exemple |
|
| 148 | + elseif ( |
|
| 149 | + $GLOBALS['connexions'][$index]['spip_connect_version'] |
|
| 150 | + and sql_showtable('spip_meta', true, $serveur) |
|
| 151 | + and $r = sql_getfetsel('valeur', 'spip_meta', "nom='charset_sql_connexion'", '', '', '', '', $serveur) |
|
| 152 | + ) { |
|
| 153 | + $charset = $r; |
|
| 154 | + } else { |
|
| 155 | + $charset = -1; |
|
| 156 | + } |
|
| 157 | + } |
|
| 158 | + if ($charset != -1) { |
|
| 159 | + $f = $GLOBALS[$jeu]['set_charset']; |
|
| 160 | + if (function_exists($f)) { |
|
| 161 | + $f($charset, $serveur); |
|
| 162 | + } |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + return $GLOBALS['connexions'][$index]; |
|
| 166 | 166 | } |
| 167 | 167 | |
| 168 | 168 | /** |
@@ -171,12 +171,12 @@ discard block |
||
| 171 | 171 | * @param string $serveur Nom du connecteur de bdd utilisé |
| 172 | 172 | **/ |
| 173 | 173 | function spip_sql_erreur($serveur = '') { |
| 174 | - $connexion = spip_connect($serveur); |
|
| 175 | - $e = sql_errno($serveur); |
|
| 176 | - $t = ($connexion['type'] ?? 'sql'); |
|
| 177 | - $m = "Erreur $e de $t: " . sql_error($serveur) . "\nin " . sql_error_backtrace() . "\n" . trim($connexion['last']); |
|
| 178 | - $f = $t . $serveur; |
|
| 179 | - spip_log($m, $f . '.' . _LOG_ERREUR); |
|
| 174 | + $connexion = spip_connect($serveur); |
|
| 175 | + $e = sql_errno($serveur); |
|
| 176 | + $t = ($connexion['type'] ?? 'sql'); |
|
| 177 | + $m = "Erreur $e de $t: " . sql_error($serveur) . "\nin " . sql_error_backtrace() . "\n" . trim($connexion['last']); |
|
| 178 | + $f = $t . $serveur; |
|
| 179 | + spip_log($m, $f . '.' . _LOG_ERREUR); |
|
| 180 | 180 | } |
| 181 | 181 | |
| 182 | 182 | /** |
@@ -198,23 +198,23 @@ discard block |
||
| 198 | 198 | * - array : description de la connexion, si l'instruction sql est indisponible pour cette connexion |
| 199 | 199 | **/ |
| 200 | 200 | function spip_connect_sql($version, $ins = '', $serveur = '', $continue = false) { |
| 201 | - $desc = spip_connect($serveur, $version); |
|
| 202 | - if ( |
|
| 203 | - $desc |
|
| 204 | - and $f = ($desc[$version][$ins] ?? '') |
|
| 205 | - and function_exists($f) |
|
| 206 | - ) { |
|
| 207 | - return $f; |
|
| 208 | - } |
|
| 209 | - if ($continue) { |
|
| 210 | - return $desc; |
|
| 211 | - } |
|
| 212 | - if ($ins) { |
|
| 213 | - spip_log("Le serveur '$serveur' version $version n'a pas '$ins'", _LOG_ERREUR); |
|
| 214 | - } |
|
| 215 | - include_spip('inc/minipres'); |
|
| 216 | - echo minipres(_T('info_travaux_titre'), _T('titre_probleme_technique'), ['status' => 503]); |
|
| 217 | - exit; |
|
| 201 | + $desc = spip_connect($serveur, $version); |
|
| 202 | + if ( |
|
| 203 | + $desc |
|
| 204 | + and $f = ($desc[$version][$ins] ?? '') |
|
| 205 | + and function_exists($f) |
|
| 206 | + ) { |
|
| 207 | + return $f; |
|
| 208 | + } |
|
| 209 | + if ($continue) { |
|
| 210 | + return $desc; |
|
| 211 | + } |
|
| 212 | + if ($ins) { |
|
| 213 | + spip_log("Le serveur '$serveur' version $version n'a pas '$ins'", _LOG_ERREUR); |
|
| 214 | + } |
|
| 215 | + include_spip('inc/minipres'); |
|
| 216 | + echo minipres(_T('info_travaux_titre'), _T('titre_probleme_technique'), ['status' => 503]); |
|
| 217 | + exit; |
|
| 218 | 218 | } |
| 219 | 219 | |
| 220 | 220 | /** |
@@ -240,70 +240,70 @@ discard block |
||
| 240 | 240 | * @return array|null Description de la connexion |
| 241 | 241 | */ |
| 242 | 242 | function spip_connect_db( |
| 243 | - $host, |
|
| 244 | - $port, |
|
| 245 | - $login, |
|
| 246 | - #[\SensitiveParameter] $pass, |
|
| 247 | - $db = '', |
|
| 248 | - $type = 'mysql', |
|
| 249 | - $prefixe = '', |
|
| 250 | - $auth = '', |
|
| 251 | - $charset = '' |
|
| 243 | + $host, |
|
| 244 | + $port, |
|
| 245 | + $login, |
|
| 246 | + #[\SensitiveParameter] $pass, |
|
| 247 | + $db = '', |
|
| 248 | + $type = 'mysql', |
|
| 249 | + $prefixe = '', |
|
| 250 | + $auth = '', |
|
| 251 | + $charset = '' |
|
| 252 | 252 | ) { |
| 253 | - // temps avant nouvelle tentative de connexion |
|
| 254 | - // suite a une connection echouee |
|
| 255 | - if (!defined('_CONNECT_RETRY_DELAY')) { |
|
| 256 | - define('_CONNECT_RETRY_DELAY', 30); |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - $f = ''; |
|
| 260 | - // un fichier de identifiant par combinaison (type,host,port,db) |
|
| 261 | - // pour ne pas declarer tout indisponible d'un coup |
|
| 262 | - // si en cours d'installation ou si db=@test@ on ne pose rien |
|
| 263 | - // car c'est un test de connexion |
|
| 264 | - if (!defined('_ECRIRE_INSTALL') and $db !== '@test@') { |
|
| 265 | - $f = _DIR_TMP . $type . '.' . substr(md5($host . $port . $db), 0, 8) . '.out'; |
|
| 266 | - } elseif ($db == '@test@') { |
|
| 267 | - $db = ''; |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - if ( |
|
| 271 | - $f |
|
| 272 | - and @file_exists($f) |
|
| 273 | - and (time() - @filemtime($f) < _CONNECT_RETRY_DELAY) |
|
| 274 | - ) { |
|
| 275 | - spip_log("Echec : $f recent. Pas de tentative de connexion", _LOG_HS); |
|
| 276 | - |
|
| 277 | - return null; |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - if (!$prefixe) { |
|
| 281 | - $prefixe = $GLOBALS['table_prefix'] ?? $db; |
|
| 282 | - } |
|
| 283 | - $h = charger_fonction($type, 'req', true); |
|
| 284 | - if (!$h) { |
|
| 285 | - spip_log("les requetes $type ne sont pas fournies", _LOG_HS); |
|
| 286 | - |
|
| 287 | - return null; |
|
| 288 | - } |
|
| 289 | - if ($g = $h($host, $port, $login, $pass, $db, $prefixe)) { |
|
| 290 | - if (!is_array($auth)) { |
|
| 291 | - // compatibilite version 0.7 initiale |
|
| 292 | - $g['ldap'] = $auth; |
|
| 293 | - $auth = ['ldap' => $auth]; |
|
| 294 | - } |
|
| 295 | - $g['authentification'] = $auth; |
|
| 296 | - $g['type'] = $type; |
|
| 297 | - $g['charset'] = $charset; |
|
| 298 | - |
|
| 299 | - return $GLOBALS['db_ok'] = $g; |
|
| 300 | - } |
|
| 301 | - // En cas d'indisponibilite du serveur, eviter de le bombarder |
|
| 302 | - if ($f) { |
|
| 303 | - @touch($f); |
|
| 304 | - spip_log("Echec connexion serveur $type : host[$host] port[$port] login[$login] base[$db]", $type . '.' . _LOG_HS); |
|
| 305 | - } |
|
| 306 | - return null; |
|
| 253 | + // temps avant nouvelle tentative de connexion |
|
| 254 | + // suite a une connection echouee |
|
| 255 | + if (!defined('_CONNECT_RETRY_DELAY')) { |
|
| 256 | + define('_CONNECT_RETRY_DELAY', 30); |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + $f = ''; |
|
| 260 | + // un fichier de identifiant par combinaison (type,host,port,db) |
|
| 261 | + // pour ne pas declarer tout indisponible d'un coup |
|
| 262 | + // si en cours d'installation ou si db=@test@ on ne pose rien |
|
| 263 | + // car c'est un test de connexion |
|
| 264 | + if (!defined('_ECRIRE_INSTALL') and $db !== '@test@') { |
|
| 265 | + $f = _DIR_TMP . $type . '.' . substr(md5($host . $port . $db), 0, 8) . '.out'; |
|
| 266 | + } elseif ($db == '@test@') { |
|
| 267 | + $db = ''; |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + if ( |
|
| 271 | + $f |
|
| 272 | + and @file_exists($f) |
|
| 273 | + and (time() - @filemtime($f) < _CONNECT_RETRY_DELAY) |
|
| 274 | + ) { |
|
| 275 | + spip_log("Echec : $f recent. Pas de tentative de connexion", _LOG_HS); |
|
| 276 | + |
|
| 277 | + return null; |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + if (!$prefixe) { |
|
| 281 | + $prefixe = $GLOBALS['table_prefix'] ?? $db; |
|
| 282 | + } |
|
| 283 | + $h = charger_fonction($type, 'req', true); |
|
| 284 | + if (!$h) { |
|
| 285 | + spip_log("les requetes $type ne sont pas fournies", _LOG_HS); |
|
| 286 | + |
|
| 287 | + return null; |
|
| 288 | + } |
|
| 289 | + if ($g = $h($host, $port, $login, $pass, $db, $prefixe)) { |
|
| 290 | + if (!is_array($auth)) { |
|
| 291 | + // compatibilite version 0.7 initiale |
|
| 292 | + $g['ldap'] = $auth; |
|
| 293 | + $auth = ['ldap' => $auth]; |
|
| 294 | + } |
|
| 295 | + $g['authentification'] = $auth; |
|
| 296 | + $g['type'] = $type; |
|
| 297 | + $g['charset'] = $charset; |
|
| 298 | + |
|
| 299 | + return $GLOBALS['db_ok'] = $g; |
|
| 300 | + } |
|
| 301 | + // En cas d'indisponibilite du serveur, eviter de le bombarder |
|
| 302 | + if ($f) { |
|
| 303 | + @touch($f); |
|
| 304 | + spip_log("Echec connexion serveur $type : host[$host] port[$port] login[$login] base[$db]", $type . '.' . _LOG_HS); |
|
| 305 | + } |
|
| 306 | + return null; |
|
| 307 | 307 | } |
| 308 | 308 | |
| 309 | 309 | |
@@ -335,32 +335,32 @@ discard block |
||
| 335 | 335 | * - nom du charset sinon |
| 336 | 336 | **/ |
| 337 | 337 | function spip_connect_main($connexion, $charset_sql_connexion = '') { |
| 338 | - if ($GLOBALS['spip_connect_version'] < 0.1 and _DIR_RESTREINT) { |
|
| 339 | - include_spip('inc/headers'); |
|
| 340 | - redirige_url_ecrire('upgrade', 'reinstall=oui'); |
|
| 341 | - } |
|
| 342 | - |
|
| 343 | - if (!($f = $connexion['select'])) { |
|
| 344 | - return false; |
|
| 345 | - } |
|
| 346 | - // si le charset est fourni, l'utiliser |
|
| 347 | - if ($charset_sql_connexion) { |
|
| 348 | - return $charset_sql_connexion; |
|
| 349 | - } |
|
| 350 | - // sinon on regarde la table spip_meta |
|
| 351 | - // en cas d'erreur select retourne la requette (is_string=true donc) |
|
| 352 | - if ( |
|
| 353 | - !$r = $f('valeur', 'spip_meta', "nom='charset_sql_connexion'") |
|
| 354 | - or is_string($r) |
|
| 355 | - ) { |
|
| 356 | - return false; |
|
| 357 | - } |
|
| 358 | - if (!($f = $connexion['fetch'])) { |
|
| 359 | - return false; |
|
| 360 | - } |
|
| 361 | - $r = $f($r); |
|
| 362 | - |
|
| 363 | - return (isset($r['valeur']) && $r['valeur']) ? $r['valeur'] : -1; |
|
| 338 | + if ($GLOBALS['spip_connect_version'] < 0.1 and _DIR_RESTREINT) { |
|
| 339 | + include_spip('inc/headers'); |
|
| 340 | + redirige_url_ecrire('upgrade', 'reinstall=oui'); |
|
| 341 | + } |
|
| 342 | + |
|
| 343 | + if (!($f = $connexion['select'])) { |
|
| 344 | + return false; |
|
| 345 | + } |
|
| 346 | + // si le charset est fourni, l'utiliser |
|
| 347 | + if ($charset_sql_connexion) { |
|
| 348 | + return $charset_sql_connexion; |
|
| 349 | + } |
|
| 350 | + // sinon on regarde la table spip_meta |
|
| 351 | + // en cas d'erreur select retourne la requette (is_string=true donc) |
|
| 352 | + if ( |
|
| 353 | + !$r = $f('valeur', 'spip_meta', "nom='charset_sql_connexion'") |
|
| 354 | + or is_string($r) |
|
| 355 | + ) { |
|
| 356 | + return false; |
|
| 357 | + } |
|
| 358 | + if (!($f = $connexion['fetch'])) { |
|
| 359 | + return false; |
|
| 360 | + } |
|
| 361 | + $r = $f($r); |
|
| 362 | + |
|
| 363 | + return (isset($r['valeur']) && $r['valeur']) ? $r['valeur'] : -1; |
|
| 364 | 364 | } |
| 365 | 365 | |
| 366 | 366 | /** |
@@ -376,8 +376,8 @@ discard block |
||
| 376 | 376 | * @return array |
| 377 | 377 | */ |
| 378 | 378 | function spip_connect_ldap($serveur = '') { |
| 379 | - include_spip('auth/ldap'); |
|
| 380 | - return auth_ldap_connect($serveur); |
|
| 379 | + include_spip('auth/ldap'); |
|
| 380 | + return auth_ldap_connect($serveur); |
|
| 381 | 381 | } |
| 382 | 382 | |
| 383 | 383 | /** |
@@ -393,16 +393,16 @@ discard block |
||
| 393 | 393 | * @return string Valeur échappée. |
| 394 | 394 | **/ |
| 395 | 395 | function _q($a): string { |
| 396 | - if (is_numeric($a)) { |
|
| 397 | - return strval($a); |
|
| 398 | - } elseif (is_array($a)) { |
|
| 399 | - return join(',', array_map('_q', $a)); |
|
| 400 | - } elseif (is_scalar($a)) { |
|
| 401 | - return ("'" . addslashes($a) . "'"); |
|
| 402 | - } elseif ($a === null) { |
|
| 403 | - return "''"; |
|
| 404 | - } |
|
| 405 | - throw new \RuntimeException('Can’t use _q with ' . gettype($a)); |
|
| 396 | + if (is_numeric($a)) { |
|
| 397 | + return strval($a); |
|
| 398 | + } elseif (is_array($a)) { |
|
| 399 | + return join(',', array_map('_q', $a)); |
|
| 400 | + } elseif (is_scalar($a)) { |
|
| 401 | + return ("'" . addslashes($a) . "'"); |
|
| 402 | + } elseif ($a === null) { |
|
| 403 | + return "''"; |
|
| 404 | + } |
|
| 405 | + throw new \RuntimeException('Can’t use _q with ' . gettype($a)); |
|
| 406 | 406 | } |
| 407 | 407 | |
| 408 | 408 | /** |
@@ -418,75 +418,75 @@ discard block |
||
| 418 | 418 | * @return array |
| 419 | 419 | */ |
| 420 | 420 | function query_echappe_textes($query, $uniqid = null) { |
| 421 | - static $codeEchappements = null; |
|
| 422 | - if (is_null($codeEchappements) or $uniqid) { |
|
| 423 | - if (is_null($uniqid)) { |
|
| 424 | - $uniqid = uniqid(); |
|
| 425 | - } |
|
| 426 | - $uniqid = substr(md5($uniqid), 0, 4); |
|
| 427 | - $codeEchappements = ['\\\\' => "\x1@#{$uniqid}#@\x1", "\\'" => "\x2@#{$uniqid}#@\x2", '\\"' => "\x3@#{$uniqid}#@\x3", '%' => "\x4@#{$uniqid}#@\x4"]; |
|
| 428 | - } |
|
| 429 | - if ($query === null) { |
|
| 430 | - return $codeEchappements; |
|
| 431 | - } |
|
| 432 | - |
|
| 433 | - // si la query contient deja des codes d'echappement on va s'emmeler les pinceaux et donc on ne touche a rien |
|
| 434 | - // ce n'est pas un cas legitime |
|
| 435 | - foreach ($codeEchappements as $codeEchappement) { |
|
| 436 | - if (strpos($query, (string) $codeEchappement) !== false) { |
|
| 437 | - return [$query, []]; |
|
| 438 | - } |
|
| 439 | - } |
|
| 440 | - |
|
| 441 | - $query_echappees = str_replace(array_keys($codeEchappements), array_values($codeEchappements), $query); |
|
| 442 | - if (preg_match_all("/('[^']*')|(\"[^\"]*\")/S", $query_echappees, $textes)) { |
|
| 443 | - $textes = reset($textes); |
|
| 444 | - |
|
| 445 | - $parts = []; |
|
| 446 | - $currentpos = 0; |
|
| 447 | - $k = 0; |
|
| 448 | - while (count($textes)) { |
|
| 449 | - $part = array_shift($textes); |
|
| 450 | - $nextpos = strpos($query_echappees, $part, $currentpos); |
|
| 451 | - // si besoin recoller ensemble les doubles '' de sqlite (echappement des ') |
|
| 452 | - while (count($textes) and substr($part, -1) === "'") { |
|
| 453 | - $next = reset($textes); |
|
| 454 | - if ( |
|
| 455 | - strpos($next, "'") === 0 |
|
| 456 | - and strpos($query_echappees, $part . $next, $currentpos) === $nextpos |
|
| 457 | - ) { |
|
| 458 | - $part .= array_shift($textes); |
|
| 459 | - } |
|
| 460 | - else { |
|
| 461 | - break; |
|
| 462 | - } |
|
| 463 | - } |
|
| 464 | - $k++; |
|
| 465 | - $parts[$k] = [ |
|
| 466 | - 'texte' => $part, |
|
| 467 | - 'position' => $nextpos, |
|
| 468 | - 'placeholder' => '%' . $k . '$s', |
|
| 469 | - ]; |
|
| 470 | - $currentpos = $nextpos + strlen($part); |
|
| 471 | - } |
|
| 472 | - |
|
| 473 | - // et on replace les parts une par une en commencant par la fin |
|
| 474 | - while ($k > 0) { |
|
| 475 | - $query_echappees = substr_replace($query_echappees, $parts[$k]['placeholder'], $parts[$k]['position'], strlen($parts[$k]['texte'])); |
|
| 476 | - $k--; |
|
| 477 | - } |
|
| 478 | - $textes = array_column($parts, 'texte'); |
|
| 479 | - } else { |
|
| 480 | - $textes = []; |
|
| 481 | - } |
|
| 482 | - |
|
| 483 | - // si il reste des quotes simples ou doubles, c'est qu'on s'est emmelle les pinceaux |
|
| 484 | - // dans le doute on ne touche a rien |
|
| 485 | - if (strpbrk($query_echappees, "'\"") !== false) { |
|
| 486 | - return [$query, []]; |
|
| 487 | - } |
|
| 488 | - |
|
| 489 | - return [$query_echappees, $textes]; |
|
| 421 | + static $codeEchappements = null; |
|
| 422 | + if (is_null($codeEchappements) or $uniqid) { |
|
| 423 | + if (is_null($uniqid)) { |
|
| 424 | + $uniqid = uniqid(); |
|
| 425 | + } |
|
| 426 | + $uniqid = substr(md5($uniqid), 0, 4); |
|
| 427 | + $codeEchappements = ['\\\\' => "\x1@#{$uniqid}#@\x1", "\\'" => "\x2@#{$uniqid}#@\x2", '\\"' => "\x3@#{$uniqid}#@\x3", '%' => "\x4@#{$uniqid}#@\x4"]; |
|
| 428 | + } |
|
| 429 | + if ($query === null) { |
|
| 430 | + return $codeEchappements; |
|
| 431 | + } |
|
| 432 | + |
|
| 433 | + // si la query contient deja des codes d'echappement on va s'emmeler les pinceaux et donc on ne touche a rien |
|
| 434 | + // ce n'est pas un cas legitime |
|
| 435 | + foreach ($codeEchappements as $codeEchappement) { |
|
| 436 | + if (strpos($query, (string) $codeEchappement) !== false) { |
|
| 437 | + return [$query, []]; |
|
| 438 | + } |
|
| 439 | + } |
|
| 440 | + |
|
| 441 | + $query_echappees = str_replace(array_keys($codeEchappements), array_values($codeEchappements), $query); |
|
| 442 | + if (preg_match_all("/('[^']*')|(\"[^\"]*\")/S", $query_echappees, $textes)) { |
|
| 443 | + $textes = reset($textes); |
|
| 444 | + |
|
| 445 | + $parts = []; |
|
| 446 | + $currentpos = 0; |
|
| 447 | + $k = 0; |
|
| 448 | + while (count($textes)) { |
|
| 449 | + $part = array_shift($textes); |
|
| 450 | + $nextpos = strpos($query_echappees, $part, $currentpos); |
|
| 451 | + // si besoin recoller ensemble les doubles '' de sqlite (echappement des ') |
|
| 452 | + while (count($textes) and substr($part, -1) === "'") { |
|
| 453 | + $next = reset($textes); |
|
| 454 | + if ( |
|
| 455 | + strpos($next, "'") === 0 |
|
| 456 | + and strpos($query_echappees, $part . $next, $currentpos) === $nextpos |
|
| 457 | + ) { |
|
| 458 | + $part .= array_shift($textes); |
|
| 459 | + } |
|
| 460 | + else { |
|
| 461 | + break; |
|
| 462 | + } |
|
| 463 | + } |
|
| 464 | + $k++; |
|
| 465 | + $parts[$k] = [ |
|
| 466 | + 'texte' => $part, |
|
| 467 | + 'position' => $nextpos, |
|
| 468 | + 'placeholder' => '%' . $k . '$s', |
|
| 469 | + ]; |
|
| 470 | + $currentpos = $nextpos + strlen($part); |
|
| 471 | + } |
|
| 472 | + |
|
| 473 | + // et on replace les parts une par une en commencant par la fin |
|
| 474 | + while ($k > 0) { |
|
| 475 | + $query_echappees = substr_replace($query_echappees, $parts[$k]['placeholder'], $parts[$k]['position'], strlen($parts[$k]['texte'])); |
|
| 476 | + $k--; |
|
| 477 | + } |
|
| 478 | + $textes = array_column($parts, 'texte'); |
|
| 479 | + } else { |
|
| 480 | + $textes = []; |
|
| 481 | + } |
|
| 482 | + |
|
| 483 | + // si il reste des quotes simples ou doubles, c'est qu'on s'est emmelle les pinceaux |
|
| 484 | + // dans le doute on ne touche a rien |
|
| 485 | + if (strpbrk($query_echappees, "'\"") !== false) { |
|
| 486 | + return [$query, []]; |
|
| 487 | + } |
|
| 488 | + |
|
| 489 | + return [$query_echappees, $textes]; |
|
| 490 | 490 | } |
| 491 | 491 | |
| 492 | 492 | /** |
@@ -500,16 +500,16 @@ discard block |
||
| 500 | 500 | * @return string |
| 501 | 501 | */ |
| 502 | 502 | function query_reinjecte_textes($query, $textes) { |
| 503 | - // recuperer les codes echappements |
|
| 504 | - $codeEchappements = query_echappe_textes(null); |
|
| 503 | + // recuperer les codes echappements |
|
| 504 | + $codeEchappements = query_echappe_textes(null); |
|
| 505 | 505 | |
| 506 | - if (!empty($textes)) { |
|
| 507 | - $query = sprintf($query, ...$textes); |
|
| 508 | - } |
|
| 506 | + if (!empty($textes)) { |
|
| 507 | + $query = sprintf($query, ...$textes); |
|
| 508 | + } |
|
| 509 | 509 | |
| 510 | - $query = str_replace(array_values($codeEchappements), array_keys($codeEchappements), $query); |
|
| 510 | + $query = str_replace(array_values($codeEchappements), array_keys($codeEchappements), $query); |
|
| 511 | 511 | |
| 512 | - return $query; |
|
| 512 | + return $query; |
|
| 513 | 513 | } |
| 514 | 514 | |
| 515 | 515 | |
@@ -528,7 +528,7 @@ discard block |
||
| 528 | 528 | **/ |
| 529 | 529 | function spip_query($query, $serveur = '') { |
| 530 | 530 | |
| 531 | - $f = spip_connect_sql($GLOBALS['spip_sql_version'], 'query', $serveur, true); |
|
| 531 | + $f = spip_connect_sql($GLOBALS['spip_sql_version'], 'query', $serveur, true); |
|
| 532 | 532 | |
| 533 | - return function_exists($f) ? $f($query, $serveur) : false; |
|
| 533 | + return function_exists($f) ? $f($query, $serveur) : false; |
|
| 534 | 534 | } |
@@ -10,7 +10,7 @@ discard block |
||
| 10 | 10 | \***************************************************************************/ |
| 11 | 11 | |
| 12 | 12 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 13 | - return; |
|
| 13 | + return; |
|
| 14 | 14 | } |
| 15 | 15 | |
| 16 | 16 | |
@@ -20,18 +20,18 @@ discard block |
||
| 20 | 20 | |
| 21 | 21 | |
| 22 | 22 | function req_sqlite3_dist($addr, $port, $login, #[\SensitiveParameter] $pass, $db = '', $prefixe = '') { |
| 23 | - return req_sqlite_dist($addr, $port, $login, $pass, $db, $prefixe, $sqlite_version = 3); |
|
| 23 | + return req_sqlite_dist($addr, $port, $login, $pass, $db, $prefixe, $sqlite_version = 3); |
|
| 24 | 24 | } |
| 25 | 25 | |
| 26 | 26 | |
| 27 | 27 | function spip_sqlite3_constantes() { |
| 28 | - if (!defined('SPIP_SQLITE3_ASSOC')) { |
|
| 29 | - define('SPIP_SQLITE3_ASSOC', PDO::FETCH_ASSOC); |
|
| 30 | - define('SPIP_SQLITE3_NUM', PDO::FETCH_NUM); |
|
| 31 | - define('SPIP_SQLITE3_BOTH', PDO::FETCH_BOTH); |
|
| 32 | - } |
|
| 28 | + if (!defined('SPIP_SQLITE3_ASSOC')) { |
|
| 29 | + define('SPIP_SQLITE3_ASSOC', PDO::FETCH_ASSOC); |
|
| 30 | + define('SPIP_SQLITE3_NUM', PDO::FETCH_NUM); |
|
| 31 | + define('SPIP_SQLITE3_BOTH', PDO::FETCH_BOTH); |
|
| 32 | + } |
|
| 33 | 33 | } |
| 34 | 34 | |
| 35 | 35 | function spip_versions_sqlite3() { |
| 36 | - return _sqlite_charger_version(3) ? 3 : false; |
|
| 36 | + return _sqlite_charger_version(3) ? 3 : false; |
|
| 37 | 37 | } |
@@ -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,88 +44,88 @@ discard block |
||
| 44 | 44 | */ |
| 45 | 45 | function req_sqlite_dist($addr, $port, $login, #[\SensitiveParameter] $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 | - if (!$db) { |
|
| 93 | - // si pas de db -> |
|
| 94 | - // base temporaire tant qu'on ne connait pas son vrai nom |
|
| 95 | - // pour tester la connexion |
|
| 96 | - $db = '_sqlite' . $sqlite_version . '_install'; |
|
| 97 | - $tmp = _DIR_DB . $db . '.sqlite'; |
|
| 98 | - $link = spip_sqlite_open($tmp); |
|
| 99 | - } else { |
|
| 100 | - // Ouvrir (eventuellement creer la base) |
|
| 101 | - $link = spip_sqlite_open($f); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - if (!$link) { |
|
| 105 | - spip_log("Impossible d'ouvrir la base SQLite($sqlite_version) $f", 'sqlite.' . _LOG_HS); |
|
| 106 | - |
|
| 107 | - return false; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - $last_connect = [ |
|
| 111 | - 'addr' => $addr, |
|
| 112 | - 'port' => $port, |
|
| 113 | - 'login' => $login, |
|
| 114 | - 'pass' => $pass, |
|
| 115 | - 'db' => $db, |
|
| 116 | - 'prefixe' => $prefixe, |
|
| 117 | - ]; |
|
| 118 | - |
|
| 119 | - // etre sur qu'on definit bien les fonctions a chaque nouvelle connexion |
|
| 120 | - include_spip('req/sqlite_fonctions'); |
|
| 121 | - _sqlite_init_functions($link); |
|
| 122 | - |
|
| 123 | - return [ |
|
| 124 | - 'db' => $db, |
|
| 125 | - 'prefixe' => $prefixe ? $prefixe : $db, |
|
| 126 | - 'link' => $link, |
|
| 127 | - 'total_requetes' => 0, |
|
| 128 | - ]; |
|
| 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 | + if (!$db) { |
|
| 93 | + // si pas de db -> |
|
| 94 | + // base temporaire tant qu'on ne connait pas son vrai nom |
|
| 95 | + // pour tester la connexion |
|
| 96 | + $db = '_sqlite' . $sqlite_version . '_install'; |
|
| 97 | + $tmp = _DIR_DB . $db . '.sqlite'; |
|
| 98 | + $link = spip_sqlite_open($tmp); |
|
| 99 | + } else { |
|
| 100 | + // Ouvrir (eventuellement creer la base) |
|
| 101 | + $link = spip_sqlite_open($f); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + if (!$link) { |
|
| 105 | + spip_log("Impossible d'ouvrir la base SQLite($sqlite_version) $f", 'sqlite.' . _LOG_HS); |
|
| 106 | + |
|
| 107 | + return false; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + $last_connect = [ |
|
| 111 | + 'addr' => $addr, |
|
| 112 | + 'port' => $port, |
|
| 113 | + 'login' => $login, |
|
| 114 | + 'pass' => $pass, |
|
| 115 | + 'db' => $db, |
|
| 116 | + 'prefixe' => $prefixe, |
|
| 117 | + ]; |
|
| 118 | + |
|
| 119 | + // etre sur qu'on definit bien les fonctions a chaque nouvelle connexion |
|
| 120 | + include_spip('req/sqlite_fonctions'); |
|
| 121 | + _sqlite_init_functions($link); |
|
| 122 | + |
|
| 123 | + return [ |
|
| 124 | + 'db' => $db, |
|
| 125 | + 'prefixe' => $prefixe ? $prefixe : $db, |
|
| 126 | + 'link' => $link, |
|
| 127 | + 'total_requetes' => 0, |
|
| 128 | + ]; |
|
| 129 | 129 | } |
| 130 | 130 | |
| 131 | 131 | /** |
@@ -136,9 +136,9 @@ discard block |
||
| 136 | 136 | * @return PDO |
| 137 | 137 | */ |
| 138 | 138 | function spip_sqlite_open(string $file): \PDO { |
| 139 | - $PDO = new \PDO("sqlite:$file"); |
|
| 140 | - $PDO->setAttribute(\PDO::ATTR_STATEMENT_CLASS , [\Spip\Sql\Sqlite\PDOStatement::class, [&$PDO]]); |
|
| 141 | - return $PDO; |
|
| 139 | + $PDO = new \PDO("sqlite:$file"); |
|
| 140 | + $PDO->setAttribute(\PDO::ATTR_STATEMENT_CLASS , [\Spip\Sql\Sqlite\PDOStatement::class, [&$PDO]]); |
|
| 141 | + return $PDO; |
|
| 142 | 142 | } |
| 143 | 143 | |
| 144 | 144 | |
@@ -158,14 +158,14 @@ discard block |
||
| 158 | 158 | */ |
| 159 | 159 | function spip_sqlite_query($query, $serveur = '', $requeter = true) |
| 160 | 160 | { |
| 161 | - #spip_log("spip_sqlite_query() > $query",'sqlite.'._LOG_DEBUG); |
|
| 162 | - #_sqlite_init(); // fait la premiere fois dans spip_sqlite |
|
| 163 | - $query = Sqlite::traduire_requete($query, $serveur); |
|
| 164 | - if (!$requeter) { |
|
| 165 | - return $query; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - return Sqlite::executer_requete($query, $serveur); |
|
| 161 | + #spip_log("spip_sqlite_query() > $query",'sqlite.'._LOG_DEBUG); |
|
| 162 | + #_sqlite_init(); // fait la premiere fois dans spip_sqlite |
|
| 163 | + $query = Sqlite::traduire_requete($query, $serveur); |
|
| 164 | + if (!$requeter) { |
|
| 165 | + return $query; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + return Sqlite::executer_requete($query, $serveur); |
|
| 169 | 169 | } |
| 170 | 170 | |
| 171 | 171 | |
@@ -183,11 +183,11 @@ discard block |
||
| 183 | 183 | function spip_sqlite_alter($query, $serveur = '', $requeter = true) |
| 184 | 184 | { |
| 185 | 185 | |
| 186 | - $query = spip_sqlite_query("ALTER $query", $serveur, false); |
|
| 187 | - // traduire la requete pour recuperer les bons noms de table |
|
| 188 | - $query = Sqlite::traduire_requete($query, $serveur); |
|
| 186 | + $query = spip_sqlite_query("ALTER $query", $serveur, false); |
|
| 187 | + // traduire la requete pour recuperer les bons noms de table |
|
| 188 | + $query = Sqlite::traduire_requete($query, $serveur); |
|
| 189 | 189 | |
| 190 | - /* |
|
| 190 | + /* |
|
| 191 | 191 | * la il faut faire les transformations |
| 192 | 192 | * si ALTER TABLE x (DROP|CHANGE) y |
| 193 | 193 | * |
@@ -196,251 +196,251 @@ discard block |
||
| 196 | 196 | * 3) faire chaque requete independemment |
| 197 | 197 | */ |
| 198 | 198 | |
| 199 | - // 1 |
|
| 200 | - if (preg_match('/\s*(ALTER(\s*IGNORE)?\s*TABLE\s*([^\s]*))\s*(.*)?/is', $query, $regs)) { |
|
| 201 | - $debut = $regs[1]; |
|
| 202 | - $table = $regs[3]; |
|
| 203 | - $suite = $regs[4]; |
|
| 204 | - } else { |
|
| 205 | - spip_log("SQLite : Probleme de ALTER TABLE mal forme dans $query", 'sqlite.' . _LOG_ERREUR); |
|
| 206 | - |
|
| 207 | - return false; |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - // 2 |
|
| 211 | - // il faudrait une regexp pour eviter de spliter ADD PRIMARY KEY (colA, colB) |
|
| 212 | - // tout en cassant "ADD PRIMARY KEY (colA, colB), ADD INDEX (chose)"... en deux |
|
| 213 | - // ou revoir l'api de sql_alter en creant un |
|
| 214 | - // sql_alter_table($table,array($actions)); |
|
| 215 | - $todo = explode(',', $suite); |
|
| 216 | - |
|
| 217 | - // on remet les morceaux dechires ensembles... que c'est laid ! |
|
| 218 | - $todo2 = []; |
|
| 219 | - $i = 0; |
|
| 220 | - $ouverte = false; |
|
| 221 | - while ($do = array_shift($todo)) { |
|
| 222 | - $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . ',' . $do : $do; |
|
| 223 | - $o = (str_contains($do, '(')); |
|
| 224 | - $f = (str_contains($do, ')')); |
|
| 225 | - if ($o and !$f) { |
|
| 226 | - $ouverte = true; |
|
| 227 | - } elseif ($f) { |
|
| 228 | - $ouverte = false; |
|
| 229 | - } |
|
| 230 | - if (!$ouverte) { |
|
| 231 | - $i++; |
|
| 232 | - } |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - // 3 |
|
| 236 | - $resultats = []; |
|
| 237 | - foreach ($todo2 as $do) { |
|
| 238 | - $do = trim($do); |
|
| 239 | - if ( |
|
| 240 | - !preg_match('/(DROP PRIMARY KEY|DROP KEY|DROP INDEX|DROP COLUMN|DROP' |
|
| 241 | - . '|CHANGE COLUMN|CHANGE|MODIFY|RENAME TO|RENAME' |
|
| 242 | - . '|ADD PRIMARY KEY|ADD KEY|ADD INDEX|ADD UNIQUE KEY|ADD UNIQUE' |
|
| 243 | - . '|ADD COLUMN|ADD' |
|
| 244 | - . ')\s*([^\s]*)\s*(.*)?/i', $do, $matches) |
|
| 245 | - ) { |
|
| 246 | - spip_log( |
|
| 247 | - "SQLite : Probleme de ALTER TABLE, utilisation non reconnue dans : $do \n(requete d'origine : $query)", |
|
| 248 | - 'sqlite.' . _LOG_ERREUR |
|
| 249 | - ); |
|
| 250 | - |
|
| 251 | - return false; |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - $cle = strtoupper($matches[1]); |
|
| 255 | - $colonne_origine = $matches[2]; |
|
| 256 | - $colonne_destination = ''; |
|
| 257 | - |
|
| 258 | - $def = $matches[3]; |
|
| 259 | - |
|
| 260 | - // eluder une eventuelle clause before|after|first inutilisable |
|
| 261 | - $defr = rtrim(preg_replace('/(BEFORE|AFTER|FIRST)(.*)$/is', '', $def)); |
|
| 262 | - $defo = $defr; // garder la def d'origine pour certains cas |
|
| 263 | - // remplacer les definitions venant de mysql |
|
| 264 | - $defr = _sqlite_remplacements_definitions_table($defr); |
|
| 265 | - |
|
| 266 | - // reinjecter dans le do |
|
| 267 | - $do = str_replace($def, $defr, $do); |
|
| 268 | - $def = $defr; |
|
| 269 | - |
|
| 270 | - switch ($cle) { |
|
| 271 | - // suppression d'un index |
|
| 272 | - case 'DROP KEY': |
|
| 273 | - case 'DROP INDEX': |
|
| 274 | - $nom_index = $colonne_origine; |
|
| 275 | - spip_sqlite_drop_index($nom_index, $table, $serveur); |
|
| 276 | - break; |
|
| 277 | - |
|
| 278 | - // suppression d'une pk |
|
| 279 | - case 'DROP PRIMARY KEY': |
|
| 280 | - if ( |
|
| 281 | - !_sqlite_modifier_table( |
|
| 282 | - $table, |
|
| 283 | - $colonne_origine, |
|
| 284 | - ['key' => ['PRIMARY KEY' => '']], |
|
| 285 | - $serveur |
|
| 286 | - ) |
|
| 287 | - ) { |
|
| 288 | - return false; |
|
| 289 | - } |
|
| 290 | - break; |
|
| 291 | - // suppression d'une colonne |
|
| 292 | - case 'DROP COLUMN': |
|
| 293 | - case 'DROP': |
|
| 294 | - if ( |
|
| 295 | - !_sqlite_modifier_table( |
|
| 296 | - $table, |
|
| 297 | - [$colonne_origine => ''], |
|
| 298 | - [], |
|
| 299 | - $serveur |
|
| 300 | - ) |
|
| 301 | - ) { |
|
| 302 | - return false; |
|
| 303 | - } |
|
| 304 | - break; |
|
| 305 | - |
|
| 306 | - case 'CHANGE COLUMN': |
|
| 307 | - case 'CHANGE': |
|
| 308 | - // recuperer le nom de la future colonne |
|
| 309 | - // on reprend la def d'origine car _sqlite_modifier_table va refaire la translation |
|
| 310 | - // en tenant compte de la cle primaire (ce qui est mieux) |
|
| 311 | - $def = trim($defo); |
|
| 312 | - $colonne_destination = substr($def, 0, strpos($def, ' ')); |
|
| 313 | - $def = substr($def, strlen($colonne_destination) + 1); |
|
| 314 | - |
|
| 315 | - if ( |
|
| 316 | - !_sqlite_modifier_table( |
|
| 317 | - $table, |
|
| 318 | - [$colonne_origine => $colonne_destination], |
|
| 319 | - ['field' => [$colonne_destination => $def]], |
|
| 320 | - $serveur |
|
| 321 | - ) |
|
| 322 | - ) { |
|
| 323 | - return false; |
|
| 324 | - } |
|
| 325 | - break; |
|
| 326 | - |
|
| 327 | - case 'MODIFY': |
|
| 328 | - // on reprend la def d'origine car _sqlite_modifier_table va refaire la translation |
|
| 329 | - // en tenant compte de la cle primaire (ce qui est mieux) |
|
| 330 | - if ( |
|
| 331 | - !_sqlite_modifier_table( |
|
| 332 | - $table, |
|
| 333 | - $colonne_origine, |
|
| 334 | - ['field' => [$colonne_origine => $defo]], |
|
| 335 | - $serveur |
|
| 336 | - ) |
|
| 337 | - ) { |
|
| 338 | - return false; |
|
| 339 | - } |
|
| 340 | - break; |
|
| 341 | - |
|
| 342 | - // pas geres en sqlite2 |
|
| 343 | - case 'RENAME': |
|
| 344 | - $do = 'RENAME TO' . substr($do, 6); |
|
| 345 | - case 'RENAME TO': |
|
| 346 | - if (!Sqlite::executer_requete("$debut $do", $serveur)) { |
|
| 347 | - spip_log("SQLite : Erreur ALTER TABLE / RENAME : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 348 | - |
|
| 349 | - return false; |
|
| 350 | - } |
|
| 351 | - break; |
|
| 352 | - |
|
| 353 | - // ajout d'une pk |
|
| 354 | - case 'ADD PRIMARY KEY': |
|
| 355 | - $pk = trim(substr($do, 16)); |
|
| 356 | - $pk = ($pk[0] == '(') ? substr($pk, 1, -1) : $pk; |
|
| 357 | - if ( |
|
| 358 | - !_sqlite_modifier_table( |
|
| 359 | - $table, |
|
| 360 | - $colonne_origine, |
|
| 361 | - ['key' => ['PRIMARY KEY' => $pk]], |
|
| 362 | - $serveur |
|
| 363 | - ) |
|
| 364 | - ) { |
|
| 365 | - return false; |
|
| 366 | - } |
|
| 367 | - break; |
|
| 368 | - // ajout d'un index |
|
| 369 | - case 'ADD UNIQUE KEY': |
|
| 370 | - case 'ADD UNIQUE': |
|
| 371 | - $unique = true; |
|
| 372 | - case 'ADD INDEX': |
|
| 373 | - case 'ADD KEY': |
|
| 374 | - if (!isset($unique)) { |
|
| 375 | - $unique = false; |
|
| 376 | - } |
|
| 377 | - // peut etre "(colonne)" ou "nom_index (colonnes)" |
|
| 378 | - // bug potentiel si qqn met "(colonne, colonne)" |
|
| 379 | - // |
|
| 380 | - // nom_index (colonnes) |
|
| 381 | - if ($def) { |
|
| 382 | - $colonnes = substr($def, 1, -1); |
|
| 383 | - $nom_index = $colonne_origine; |
|
| 384 | - } else { |
|
| 385 | - // (colonne) |
|
| 386 | - if ($colonne_origine[0] == '(') { |
|
| 387 | - $colonnes = substr($colonne_origine, 1, -1); |
|
| 388 | - if (str_contains(',', $colonnes)) { |
|
| 389 | - spip_log('SQLite : Erreur, impossible de creer un index sur plusieurs colonnes' |
|
| 390 | - . " sans qu'il ait de nom ($table, ($colonnes))", 'sqlite.' . _LOG_ERREUR); |
|
| 391 | - break; |
|
| 392 | - } else { |
|
| 393 | - $nom_index = $colonnes; |
|
| 394 | - } |
|
| 395 | - } // nom_index |
|
| 396 | - else { |
|
| 397 | - $nom_index = $colonnes = $colonne_origine; |
|
| 398 | - } |
|
| 399 | - } |
|
| 400 | - spip_sqlite_create_index($nom_index, $table, $colonnes, $unique, $serveur); |
|
| 401 | - break; |
|
| 402 | - |
|
| 403 | - // pas geres en sqlite2 |
|
| 404 | - case 'ADD COLUMN': |
|
| 405 | - $do = 'ADD' . substr($do, 10); |
|
| 406 | - case 'ADD': |
|
| 407 | - default: |
|
| 408 | - if (!preg_match(',primary\s+key,i', $do)) { |
|
| 409 | - if (!Sqlite::executer_requete("$debut $do", $serveur)) { |
|
| 410 | - spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 411 | - |
|
| 412 | - return false; |
|
| 413 | - } |
|
| 414 | - break; |
|
| 415 | - } |
|
| 416 | - // ou si la colonne est aussi primary key |
|
| 417 | - // cas du add id_truc int primary key |
|
| 418 | - // ajout d'une colonne qui passe en primary key directe |
|
| 419 | - else { |
|
| 420 | - $def = trim(substr($do, 3)); |
|
| 421 | - $colonne_ajoutee = substr($def, 0, strpos($def, ' ')); |
|
| 422 | - $def = substr($def, strlen($colonne_ajoutee) + 1); |
|
| 423 | - $opts = []; |
|
| 424 | - if (preg_match(',primary\s+key,i', $def)) { |
|
| 425 | - $opts['key'] = ['PRIMARY KEY' => $colonne_ajoutee]; |
|
| 426 | - $def = preg_replace(',primary\s+key,i', '', $def); |
|
| 427 | - } |
|
| 428 | - $opts['field'] = [$colonne_ajoutee => $def]; |
|
| 429 | - if (!_sqlite_modifier_table($table, [$colonne_ajoutee], $opts, $serveur)) { |
|
| 430 | - spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 431 | - |
|
| 432 | - return false; |
|
| 433 | - } |
|
| 434 | - } |
|
| 435 | - break; |
|
| 436 | - } |
|
| 437 | - // tout est bon, ouf ! |
|
| 438 | - spip_log("SQLite ($serveur) : Changements OK : $debut $do", 'sqlite.' . _LOG_INFO); |
|
| 439 | - } |
|
| 440 | - |
|
| 441 | - spip_log("SQLite ($serveur) : fin ALTER TABLE OK !", 'sqlite.' . _LOG_INFO); |
|
| 442 | - |
|
| 443 | - return true; |
|
| 199 | + // 1 |
|
| 200 | + if (preg_match('/\s*(ALTER(\s*IGNORE)?\s*TABLE\s*([^\s]*))\s*(.*)?/is', $query, $regs)) { |
|
| 201 | + $debut = $regs[1]; |
|
| 202 | + $table = $regs[3]; |
|
| 203 | + $suite = $regs[4]; |
|
| 204 | + } else { |
|
| 205 | + spip_log("SQLite : Probleme de ALTER TABLE mal forme dans $query", 'sqlite.' . _LOG_ERREUR); |
|
| 206 | + |
|
| 207 | + return false; |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + // 2 |
|
| 211 | + // il faudrait une regexp pour eviter de spliter ADD PRIMARY KEY (colA, colB) |
|
| 212 | + // tout en cassant "ADD PRIMARY KEY (colA, colB), ADD INDEX (chose)"... en deux |
|
| 213 | + // ou revoir l'api de sql_alter en creant un |
|
| 214 | + // sql_alter_table($table,array($actions)); |
|
| 215 | + $todo = explode(',', $suite); |
|
| 216 | + |
|
| 217 | + // on remet les morceaux dechires ensembles... que c'est laid ! |
|
| 218 | + $todo2 = []; |
|
| 219 | + $i = 0; |
|
| 220 | + $ouverte = false; |
|
| 221 | + while ($do = array_shift($todo)) { |
|
| 222 | + $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . ',' . $do : $do; |
|
| 223 | + $o = (str_contains($do, '(')); |
|
| 224 | + $f = (str_contains($do, ')')); |
|
| 225 | + if ($o and !$f) { |
|
| 226 | + $ouverte = true; |
|
| 227 | + } elseif ($f) { |
|
| 228 | + $ouverte = false; |
|
| 229 | + } |
|
| 230 | + if (!$ouverte) { |
|
| 231 | + $i++; |
|
| 232 | + } |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + // 3 |
|
| 236 | + $resultats = []; |
|
| 237 | + foreach ($todo2 as $do) { |
|
| 238 | + $do = trim($do); |
|
| 239 | + if ( |
|
| 240 | + !preg_match('/(DROP PRIMARY KEY|DROP KEY|DROP INDEX|DROP COLUMN|DROP' |
|
| 241 | + . '|CHANGE COLUMN|CHANGE|MODIFY|RENAME TO|RENAME' |
|
| 242 | + . '|ADD PRIMARY KEY|ADD KEY|ADD INDEX|ADD UNIQUE KEY|ADD UNIQUE' |
|
| 243 | + . '|ADD COLUMN|ADD' |
|
| 244 | + . ')\s*([^\s]*)\s*(.*)?/i', $do, $matches) |
|
| 245 | + ) { |
|
| 246 | + spip_log( |
|
| 247 | + "SQLite : Probleme de ALTER TABLE, utilisation non reconnue dans : $do \n(requete d'origine : $query)", |
|
| 248 | + 'sqlite.' . _LOG_ERREUR |
|
| 249 | + ); |
|
| 250 | + |
|
| 251 | + return false; |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + $cle = strtoupper($matches[1]); |
|
| 255 | + $colonne_origine = $matches[2]; |
|
| 256 | + $colonne_destination = ''; |
|
| 257 | + |
|
| 258 | + $def = $matches[3]; |
|
| 259 | + |
|
| 260 | + // eluder une eventuelle clause before|after|first inutilisable |
|
| 261 | + $defr = rtrim(preg_replace('/(BEFORE|AFTER|FIRST)(.*)$/is', '', $def)); |
|
| 262 | + $defo = $defr; // garder la def d'origine pour certains cas |
|
| 263 | + // remplacer les definitions venant de mysql |
|
| 264 | + $defr = _sqlite_remplacements_definitions_table($defr); |
|
| 265 | + |
|
| 266 | + // reinjecter dans le do |
|
| 267 | + $do = str_replace($def, $defr, $do); |
|
| 268 | + $def = $defr; |
|
| 269 | + |
|
| 270 | + switch ($cle) { |
|
| 271 | + // suppression d'un index |
|
| 272 | + case 'DROP KEY': |
|
| 273 | + case 'DROP INDEX': |
|
| 274 | + $nom_index = $colonne_origine; |
|
| 275 | + spip_sqlite_drop_index($nom_index, $table, $serveur); |
|
| 276 | + break; |
|
| 277 | + |
|
| 278 | + // suppression d'une pk |
|
| 279 | + case 'DROP PRIMARY KEY': |
|
| 280 | + if ( |
|
| 281 | + !_sqlite_modifier_table( |
|
| 282 | + $table, |
|
| 283 | + $colonne_origine, |
|
| 284 | + ['key' => ['PRIMARY KEY' => '']], |
|
| 285 | + $serveur |
|
| 286 | + ) |
|
| 287 | + ) { |
|
| 288 | + return false; |
|
| 289 | + } |
|
| 290 | + break; |
|
| 291 | + // suppression d'une colonne |
|
| 292 | + case 'DROP COLUMN': |
|
| 293 | + case 'DROP': |
|
| 294 | + if ( |
|
| 295 | + !_sqlite_modifier_table( |
|
| 296 | + $table, |
|
| 297 | + [$colonne_origine => ''], |
|
| 298 | + [], |
|
| 299 | + $serveur |
|
| 300 | + ) |
|
| 301 | + ) { |
|
| 302 | + return false; |
|
| 303 | + } |
|
| 304 | + break; |
|
| 305 | + |
|
| 306 | + case 'CHANGE COLUMN': |
|
| 307 | + case 'CHANGE': |
|
| 308 | + // recuperer le nom de la future colonne |
|
| 309 | + // on reprend la def d'origine car _sqlite_modifier_table va refaire la translation |
|
| 310 | + // en tenant compte de la cle primaire (ce qui est mieux) |
|
| 311 | + $def = trim($defo); |
|
| 312 | + $colonne_destination = substr($def, 0, strpos($def, ' ')); |
|
| 313 | + $def = substr($def, strlen($colonne_destination) + 1); |
|
| 314 | + |
|
| 315 | + if ( |
|
| 316 | + !_sqlite_modifier_table( |
|
| 317 | + $table, |
|
| 318 | + [$colonne_origine => $colonne_destination], |
|
| 319 | + ['field' => [$colonne_destination => $def]], |
|
| 320 | + $serveur |
|
| 321 | + ) |
|
| 322 | + ) { |
|
| 323 | + return false; |
|
| 324 | + } |
|
| 325 | + break; |
|
| 326 | + |
|
| 327 | + case 'MODIFY': |
|
| 328 | + // on reprend la def d'origine car _sqlite_modifier_table va refaire la translation |
|
| 329 | + // en tenant compte de la cle primaire (ce qui est mieux) |
|
| 330 | + if ( |
|
| 331 | + !_sqlite_modifier_table( |
|
| 332 | + $table, |
|
| 333 | + $colonne_origine, |
|
| 334 | + ['field' => [$colonne_origine => $defo]], |
|
| 335 | + $serveur |
|
| 336 | + ) |
|
| 337 | + ) { |
|
| 338 | + return false; |
|
| 339 | + } |
|
| 340 | + break; |
|
| 341 | + |
|
| 342 | + // pas geres en sqlite2 |
|
| 343 | + case 'RENAME': |
|
| 344 | + $do = 'RENAME TO' . substr($do, 6); |
|
| 345 | + case 'RENAME TO': |
|
| 346 | + if (!Sqlite::executer_requete("$debut $do", $serveur)) { |
|
| 347 | + spip_log("SQLite : Erreur ALTER TABLE / RENAME : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 348 | + |
|
| 349 | + return false; |
|
| 350 | + } |
|
| 351 | + break; |
|
| 352 | + |
|
| 353 | + // ajout d'une pk |
|
| 354 | + case 'ADD PRIMARY KEY': |
|
| 355 | + $pk = trim(substr($do, 16)); |
|
| 356 | + $pk = ($pk[0] == '(') ? substr($pk, 1, -1) : $pk; |
|
| 357 | + if ( |
|
| 358 | + !_sqlite_modifier_table( |
|
| 359 | + $table, |
|
| 360 | + $colonne_origine, |
|
| 361 | + ['key' => ['PRIMARY KEY' => $pk]], |
|
| 362 | + $serveur |
|
| 363 | + ) |
|
| 364 | + ) { |
|
| 365 | + return false; |
|
| 366 | + } |
|
| 367 | + break; |
|
| 368 | + // ajout d'un index |
|
| 369 | + case 'ADD UNIQUE KEY': |
|
| 370 | + case 'ADD UNIQUE': |
|
| 371 | + $unique = true; |
|
| 372 | + case 'ADD INDEX': |
|
| 373 | + case 'ADD KEY': |
|
| 374 | + if (!isset($unique)) { |
|
| 375 | + $unique = false; |
|
| 376 | + } |
|
| 377 | + // peut etre "(colonne)" ou "nom_index (colonnes)" |
|
| 378 | + // bug potentiel si qqn met "(colonne, colonne)" |
|
| 379 | + // |
|
| 380 | + // nom_index (colonnes) |
|
| 381 | + if ($def) { |
|
| 382 | + $colonnes = substr($def, 1, -1); |
|
| 383 | + $nom_index = $colonne_origine; |
|
| 384 | + } else { |
|
| 385 | + // (colonne) |
|
| 386 | + if ($colonne_origine[0] == '(') { |
|
| 387 | + $colonnes = substr($colonne_origine, 1, -1); |
|
| 388 | + if (str_contains(',', $colonnes)) { |
|
| 389 | + spip_log('SQLite : Erreur, impossible de creer un index sur plusieurs colonnes' |
|
| 390 | + . " sans qu'il ait de nom ($table, ($colonnes))", 'sqlite.' . _LOG_ERREUR); |
|
| 391 | + break; |
|
| 392 | + } else { |
|
| 393 | + $nom_index = $colonnes; |
|
| 394 | + } |
|
| 395 | + } // nom_index |
|
| 396 | + else { |
|
| 397 | + $nom_index = $colonnes = $colonne_origine; |
|
| 398 | + } |
|
| 399 | + } |
|
| 400 | + spip_sqlite_create_index($nom_index, $table, $colonnes, $unique, $serveur); |
|
| 401 | + break; |
|
| 402 | + |
|
| 403 | + // pas geres en sqlite2 |
|
| 404 | + case 'ADD COLUMN': |
|
| 405 | + $do = 'ADD' . substr($do, 10); |
|
| 406 | + case 'ADD': |
|
| 407 | + default: |
|
| 408 | + if (!preg_match(',primary\s+key,i', $do)) { |
|
| 409 | + if (!Sqlite::executer_requete("$debut $do", $serveur)) { |
|
| 410 | + spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 411 | + |
|
| 412 | + return false; |
|
| 413 | + } |
|
| 414 | + break; |
|
| 415 | + } |
|
| 416 | + // ou si la colonne est aussi primary key |
|
| 417 | + // cas du add id_truc int primary key |
|
| 418 | + // ajout d'une colonne qui passe en primary key directe |
|
| 419 | + else { |
|
| 420 | + $def = trim(substr($do, 3)); |
|
| 421 | + $colonne_ajoutee = substr($def, 0, strpos($def, ' ')); |
|
| 422 | + $def = substr($def, strlen($colonne_ajoutee) + 1); |
|
| 423 | + $opts = []; |
|
| 424 | + if (preg_match(',primary\s+key,i', $def)) { |
|
| 425 | + $opts['key'] = ['PRIMARY KEY' => $colonne_ajoutee]; |
|
| 426 | + $def = preg_replace(',primary\s+key,i', '', $def); |
|
| 427 | + } |
|
| 428 | + $opts['field'] = [$colonne_ajoutee => $def]; |
|
| 429 | + if (!_sqlite_modifier_table($table, [$colonne_ajoutee], $opts, $serveur)) { |
|
| 430 | + spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 431 | + |
|
| 432 | + return false; |
|
| 433 | + } |
|
| 434 | + } |
|
| 435 | + break; |
|
| 436 | + } |
|
| 437 | + // tout est bon, ouf ! |
|
| 438 | + spip_log("SQLite ($serveur) : Changements OK : $debut $do", 'sqlite.' . _LOG_INFO); |
|
| 439 | + } |
|
| 440 | + |
|
| 441 | + spip_log("SQLite ($serveur) : fin ALTER TABLE OK !", 'sqlite.' . _LOG_INFO); |
|
| 442 | + |
|
| 443 | + return true; |
|
| 444 | 444 | } |
| 445 | 445 | |
| 446 | 446 | /** |
@@ -462,38 +462,38 @@ discard block |
||
| 462 | 462 | * - true si la requête réussie, false sinon. |
| 463 | 463 | */ |
| 464 | 464 | function spip_sqlite_create( |
| 465 | - $nom, |
|
| 466 | - $champs, |
|
| 467 | - $cles, |
|
| 468 | - $autoinc = false, |
|
| 469 | - $temporary = false, |
|
| 470 | - $serveur = '', |
|
| 471 | - $requeter = true |
|
| 465 | + $nom, |
|
| 466 | + $champs, |
|
| 467 | + $cles, |
|
| 468 | + $autoinc = false, |
|
| 469 | + $temporary = false, |
|
| 470 | + $serveur = '', |
|
| 471 | + $requeter = true |
|
| 472 | 472 | ) { |
| 473 | - $query = _sqlite_requete_create($nom, $champs, $cles, $autoinc, $temporary, $ifnotexists = true, $serveur, $requeter); |
|
| 474 | - if (!$query) { |
|
| 475 | - return false; |
|
| 476 | - } |
|
| 477 | - $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 478 | - |
|
| 479 | - // SQLite ne cree pas les KEY sur les requetes CREATE TABLE |
|
| 480 | - // il faut donc les faire creer ensuite |
|
| 481 | - if (!$requeter) { |
|
| 482 | - return $res; |
|
| 483 | - } |
|
| 484 | - |
|
| 485 | - $ok = $res ? true : false; |
|
| 486 | - if ($ok) { |
|
| 487 | - foreach ($cles as $k => $v) { |
|
| 488 | - if (preg_match(',^(UNIQUE KEY|KEY|UNIQUE)\s,i', $k, $m)) { |
|
| 489 | - $index = trim(substr($k, strlen($m[1]))); |
|
| 490 | - $unique = (strlen($m[1]) > 3); |
|
| 491 | - $ok &= spip_sqlite_create_index($index, $nom, $v, $unique, $serveur); |
|
| 492 | - } |
|
| 493 | - } |
|
| 494 | - } |
|
| 495 | - |
|
| 496 | - return $ok ? true : false; |
|
| 473 | + $query = _sqlite_requete_create($nom, $champs, $cles, $autoinc, $temporary, $ifnotexists = true, $serveur, $requeter); |
|
| 474 | + if (!$query) { |
|
| 475 | + return false; |
|
| 476 | + } |
|
| 477 | + $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 478 | + |
|
| 479 | + // SQLite ne cree pas les KEY sur les requetes CREATE TABLE |
|
| 480 | + // il faut donc les faire creer ensuite |
|
| 481 | + if (!$requeter) { |
|
| 482 | + return $res; |
|
| 483 | + } |
|
| 484 | + |
|
| 485 | + $ok = $res ? true : false; |
|
| 486 | + if ($ok) { |
|
| 487 | + foreach ($cles as $k => $v) { |
|
| 488 | + if (preg_match(',^(UNIQUE KEY|KEY|UNIQUE)\s,i', $k, $m)) { |
|
| 489 | + $index = trim(substr($k, strlen($m[1]))); |
|
| 490 | + $unique = (strlen($m[1]) > 3); |
|
| 491 | + $ok &= spip_sqlite_create_index($index, $nom, $v, $unique, $serveur); |
|
| 492 | + } |
|
| 493 | + } |
|
| 494 | + } |
|
| 495 | + |
|
| 496 | + return $ok ? true : false; |
|
| 497 | 497 | } |
| 498 | 498 | |
| 499 | 499 | /** |
@@ -507,21 +507,21 @@ discard block |
||
| 507 | 507 | **/ |
| 508 | 508 | function spip_sqlite_create_base($nom, $serveur = '', $option = true) |
| 509 | 509 | { |
| 510 | - $f = $nom . '.sqlite'; |
|
| 511 | - if (strpos($nom, '/') === false) { |
|
| 512 | - $f = _DIR_DB . $f; |
|
| 513 | - } |
|
| 510 | + $f = $nom . '.sqlite'; |
|
| 511 | + if (strpos($nom, '/') === false) { |
|
| 512 | + $f = _DIR_DB . $f; |
|
| 513 | + } |
|
| 514 | 514 | |
| 515 | - $ok = new \PDO("sqlite:$f"); |
|
| 515 | + $ok = new \PDO("sqlite:$f"); |
|
| 516 | 516 | |
| 517 | - if ($ok) { |
|
| 518 | - unset($ok); |
|
| 517 | + if ($ok) { |
|
| 518 | + unset($ok); |
|
| 519 | 519 | |
| 520 | - return true; |
|
| 521 | - } |
|
| 522 | - unset($ok); |
|
| 520 | + return true; |
|
| 521 | + } |
|
| 522 | + unset($ok); |
|
| 523 | 523 | |
| 524 | - return false; |
|
| 524 | + return false; |
|
| 525 | 525 | } |
| 526 | 526 | |
| 527 | 527 | |
@@ -543,22 +543,22 @@ discard block |
||
| 543 | 543 | */ |
| 544 | 544 | function spip_sqlite_create_view($nom, $query_select, $serveur = '', $requeter = true) |
| 545 | 545 | { |
| 546 | - if (!$query_select) { |
|
| 547 | - return false; |
|
| 548 | - } |
|
| 549 | - // vue deja presente |
|
| 550 | - if (sql_showtable($nom, false, $serveur)) { |
|
| 551 | - spip_log( |
|
| 552 | - "Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", |
|
| 553 | - 'sqlite.' . _LOG_ERREUR |
|
| 554 | - ); |
|
| 555 | - |
|
| 556 | - return false; |
|
| 557 | - } |
|
| 558 | - |
|
| 559 | - $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 560 | - |
|
| 561 | - return spip_sqlite_query($query, $serveur, $requeter); |
|
| 546 | + if (!$query_select) { |
|
| 547 | + return false; |
|
| 548 | + } |
|
| 549 | + // vue deja presente |
|
| 550 | + if (sql_showtable($nom, false, $serveur)) { |
|
| 551 | + spip_log( |
|
| 552 | + "Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", |
|
| 553 | + 'sqlite.' . _LOG_ERREUR |
|
| 554 | + ); |
|
| 555 | + |
|
| 556 | + return false; |
|
| 557 | + } |
|
| 558 | + |
|
| 559 | + $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 560 | + |
|
| 561 | + return spip_sqlite_query($query, $serveur, $requeter); |
|
| 562 | 562 | } |
| 563 | 563 | |
| 564 | 564 | /** |
@@ -581,54 +581,54 @@ discard block |
||
| 581 | 581 | */ |
| 582 | 582 | function spip_sqlite_create_index($nom, $table, $champs, $unique = '', $serveur = '', $requeter = true) |
| 583 | 583 | { |
| 584 | - if (!($nom or $table or $champs)) { |
|
| 585 | - spip_log( |
|
| 586 | - "Champ manquant pour creer un index sqlite ($nom, $table, (" . join(',', $champs) . '))', |
|
| 587 | - 'sqlite.' . _LOG_ERREUR |
|
| 588 | - ); |
|
| 589 | - |
|
| 590 | - return false; |
|
| 591 | - } |
|
| 592 | - |
|
| 593 | - // SQLite ne differentie pas noms des index en fonction des tables |
|
| 594 | - // il faut donc creer des noms uniques d'index pour une base sqlite |
|
| 595 | - $nom = $table . '_' . $nom; |
|
| 596 | - // enlever d'eventuelles parentheses deja presentes sur champs |
|
| 597 | - if (!is_array($champs)) { |
|
| 598 | - if ($champs[0] == '(') { |
|
| 599 | - $champs = substr($champs, 1, -1); |
|
| 600 | - } |
|
| 601 | - $champs = [$champs]; |
|
| 602 | - // supprimer l'info de longueur d'index mysql en fin de champ |
|
| 603 | - $champs = preg_replace(',\(\d+\)$,', '', $champs); |
|
| 604 | - } |
|
| 605 | - |
|
| 606 | - $ifnotexists = ''; |
|
| 607 | - $version = spip_sqlite_fetch(spip_sqlite_query('select sqlite_version() AS sqlite_version', $serveur), '', $serveur); |
|
| 608 | - if (!function_exists('spip_version_compare')) { |
|
| 609 | - include_spip('plugins/installer'); |
|
| 610 | - } |
|
| 611 | - |
|
| 612 | - if ($version and spip_version_compare($version['sqlite_version'], '3.3.0', '>=')) { |
|
| 613 | - $ifnotexists = ' IF NOT EXISTS'; |
|
| 614 | - } else { |
|
| 615 | - /* simuler le IF EXISTS - version 2 et sqlite < 3.3a */ |
|
| 616 | - $a = spip_sqlite_showtable($table, $serveur); |
|
| 617 | - if (isset($a['key']['KEY ' . $nom])) { |
|
| 618 | - return true; |
|
| 619 | - } |
|
| 620 | - } |
|
| 621 | - |
|
| 622 | - $query = 'CREATE ' . ($unique ? 'UNIQUE ' : '') . "INDEX$ifnotexists $nom ON $table (" . join(',', $champs) . ')'; |
|
| 623 | - $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 624 | - if (!$requeter) { |
|
| 625 | - return $res; |
|
| 626 | - } |
|
| 627 | - if ($res) { |
|
| 628 | - return true; |
|
| 629 | - } else { |
|
| 630 | - return false; |
|
| 631 | - } |
|
| 584 | + if (!($nom or $table or $champs)) { |
|
| 585 | + spip_log( |
|
| 586 | + "Champ manquant pour creer un index sqlite ($nom, $table, (" . join(',', $champs) . '))', |
|
| 587 | + 'sqlite.' . _LOG_ERREUR |
|
| 588 | + ); |
|
| 589 | + |
|
| 590 | + return false; |
|
| 591 | + } |
|
| 592 | + |
|
| 593 | + // SQLite ne differentie pas noms des index en fonction des tables |
|
| 594 | + // il faut donc creer des noms uniques d'index pour une base sqlite |
|
| 595 | + $nom = $table . '_' . $nom; |
|
| 596 | + // enlever d'eventuelles parentheses deja presentes sur champs |
|
| 597 | + if (!is_array($champs)) { |
|
| 598 | + if ($champs[0] == '(') { |
|
| 599 | + $champs = substr($champs, 1, -1); |
|
| 600 | + } |
|
| 601 | + $champs = [$champs]; |
|
| 602 | + // supprimer l'info de longueur d'index mysql en fin de champ |
|
| 603 | + $champs = preg_replace(',\(\d+\)$,', '', $champs); |
|
| 604 | + } |
|
| 605 | + |
|
| 606 | + $ifnotexists = ''; |
|
| 607 | + $version = spip_sqlite_fetch(spip_sqlite_query('select sqlite_version() AS sqlite_version', $serveur), '', $serveur); |
|
| 608 | + if (!function_exists('spip_version_compare')) { |
|
| 609 | + include_spip('plugins/installer'); |
|
| 610 | + } |
|
| 611 | + |
|
| 612 | + if ($version and spip_version_compare($version['sqlite_version'], '3.3.0', '>=')) { |
|
| 613 | + $ifnotexists = ' IF NOT EXISTS'; |
|
| 614 | + } else { |
|
| 615 | + /* simuler le IF EXISTS - version 2 et sqlite < 3.3a */ |
|
| 616 | + $a = spip_sqlite_showtable($table, $serveur); |
|
| 617 | + if (isset($a['key']['KEY ' . $nom])) { |
|
| 618 | + return true; |
|
| 619 | + } |
|
| 620 | + } |
|
| 621 | + |
|
| 622 | + $query = 'CREATE ' . ($unique ? 'UNIQUE ' : '') . "INDEX$ifnotexists $nom ON $table (" . join(',', $champs) . ')'; |
|
| 623 | + $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 624 | + if (!$requeter) { |
|
| 625 | + return $res; |
|
| 626 | + } |
|
| 627 | + if ($res) { |
|
| 628 | + return true; |
|
| 629 | + } else { |
|
| 630 | + return false; |
|
| 631 | + } |
|
| 632 | 632 | } |
| 633 | 633 | |
| 634 | 634 | /** |
@@ -645,24 +645,24 @@ discard block |
||
| 645 | 645 | */ |
| 646 | 646 | function spip_sqlite_count($r, $serveur = '', $requeter = true) |
| 647 | 647 | { |
| 648 | - if (!$r) { |
|
| 649 | - return 0; |
|
| 650 | - } |
|
| 651 | - |
|
| 652 | - // select ou autre (insert, update,...) ? |
|
| 653 | - // (link,requete) a compter |
|
| 654 | - if (strtoupper(substr(ltrim($r->queryString), 0, 6)) === 'SELECT') { |
|
| 655 | - $link = $r->getPDO(); |
|
| 656 | - $query = "SELECT count(*) as zzzzsqlitecount FROM ({$r->queryString})"; |
|
| 657 | - $l = $link->query($query); |
|
| 658 | - $i = 0; |
|
| 659 | - if ($l and $z = $l->fetch()) { |
|
| 660 | - $i = (int) $z['zzzzsqlitecount']; |
|
| 661 | - } |
|
| 662 | - return $i; |
|
| 663 | - } |
|
| 664 | - |
|
| 665 | - return $r->rowCount(); |
|
| 648 | + if (!$r) { |
|
| 649 | + return 0; |
|
| 650 | + } |
|
| 651 | + |
|
| 652 | + // select ou autre (insert, update,...) ? |
|
| 653 | + // (link,requete) a compter |
|
| 654 | + if (strtoupper(substr(ltrim($r->queryString), 0, 6)) === 'SELECT') { |
|
| 655 | + $link = $r->getPDO(); |
|
| 656 | + $query = "SELECT count(*) as zzzzsqlitecount FROM ({$r->queryString})"; |
|
| 657 | + $l = $link->query($query); |
|
| 658 | + $i = 0; |
|
| 659 | + if ($l and $z = $l->fetch()) { |
|
| 660 | + $i = (int) $z['zzzzsqlitecount']; |
|
| 661 | + } |
|
| 662 | + return $i; |
|
| 663 | + } |
|
| 664 | + |
|
| 665 | + return $r->rowCount(); |
|
| 666 | 666 | } |
| 667 | 667 | |
| 668 | 668 | |
@@ -681,31 +681,31 @@ discard block |
||
| 681 | 681 | * - false si la requête a échouée |
| 682 | 682 | **/ |
| 683 | 683 | function spip_sqlite_countsel( |
| 684 | - $from = [], |
|
| 685 | - $where = [], |
|
| 686 | - $groupby = '', |
|
| 687 | - $having = [], |
|
| 688 | - $serveur = '', |
|
| 689 | - $requeter = true |
|
| 684 | + $from = [], |
|
| 685 | + $where = [], |
|
| 686 | + $groupby = '', |
|
| 687 | + $having = [], |
|
| 688 | + $serveur = '', |
|
| 689 | + $requeter = true |
|
| 690 | 690 | ) { |
| 691 | - $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 692 | - $r = spip_sqlite_select( |
|
| 693 | - "COUNT($c)", |
|
| 694 | - $from, |
|
| 695 | - $where, |
|
| 696 | - '', |
|
| 697 | - '', |
|
| 698 | - '', |
|
| 699 | - $having, |
|
| 700 | - $serveur, |
|
| 701 | - $requeter |
|
| 702 | - ); |
|
| 703 | - if ((is_resource($r) or is_object($r)) && $requeter) { // ressource : sqlite2, object : sqlite3 |
|
| 704 | - [$r] = spip_sqlite_fetch($r, SPIP_SQLITE3_NUM, $serveur); |
|
| 705 | - $r = (int) $r; |
|
| 706 | - } |
|
| 707 | - |
|
| 708 | - return $r; |
|
| 691 | + $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 692 | + $r = spip_sqlite_select( |
|
| 693 | + "COUNT($c)", |
|
| 694 | + $from, |
|
| 695 | + $where, |
|
| 696 | + '', |
|
| 697 | + '', |
|
| 698 | + '', |
|
| 699 | + $having, |
|
| 700 | + $serveur, |
|
| 701 | + $requeter |
|
| 702 | + ); |
|
| 703 | + if ((is_resource($r) or is_object($r)) && $requeter) { // ressource : sqlite2, object : sqlite3 |
|
| 704 | + [$r] = spip_sqlite_fetch($r, SPIP_SQLITE3_NUM, $serveur); |
|
| 705 | + $r = (int) $r; |
|
| 706 | + } |
|
| 707 | + |
|
| 708 | + return $r; |
|
| 709 | 709 | } |
| 710 | 710 | |
| 711 | 711 | |
@@ -723,24 +723,24 @@ discard block |
||
| 723 | 723 | **/ |
| 724 | 724 | function spip_sqlite_delete($table, $where = '', $serveur = '', $requeter = true) |
| 725 | 725 | { |
| 726 | - $res = spip_sqlite_query( |
|
| 727 | - _sqlite_calculer_expression('DELETE FROM', $table, ',') |
|
| 728 | - . _sqlite_calculer_expression('WHERE', $where), |
|
| 729 | - $serveur, |
|
| 730 | - $requeter |
|
| 731 | - ); |
|
| 732 | - |
|
| 733 | - // renvoyer la requete inerte si demandee |
|
| 734 | - if (!$requeter) { |
|
| 735 | - return $res; |
|
| 736 | - } |
|
| 737 | - |
|
| 738 | - if ($res) { |
|
| 739 | - $link = _sqlite_link($serveur); |
|
| 740 | - return $res->rowCount(); |
|
| 741 | - } else { |
|
| 742 | - return false; |
|
| 743 | - } |
|
| 726 | + $res = spip_sqlite_query( |
|
| 727 | + _sqlite_calculer_expression('DELETE FROM', $table, ',') |
|
| 728 | + . _sqlite_calculer_expression('WHERE', $where), |
|
| 729 | + $serveur, |
|
| 730 | + $requeter |
|
| 731 | + ); |
|
| 732 | + |
|
| 733 | + // renvoyer la requete inerte si demandee |
|
| 734 | + if (!$requeter) { |
|
| 735 | + return $res; |
|
| 736 | + } |
|
| 737 | + |
|
| 738 | + if ($res) { |
|
| 739 | + $link = _sqlite_link($serveur); |
|
| 740 | + return $res->rowCount(); |
|
| 741 | + } else { |
|
| 742 | + return false; |
|
| 743 | + } |
|
| 744 | 744 | } |
| 745 | 745 | |
| 746 | 746 | |
@@ -757,15 +757,15 @@ discard block |
||
| 757 | 757 | */ |
| 758 | 758 | function spip_sqlite_drop_table($table, $exist = '', $serveur = '', $requeter = true) |
| 759 | 759 | { |
| 760 | - if ($exist) { |
|
| 761 | - $exist = ' IF EXISTS'; |
|
| 762 | - } |
|
| 763 | - |
|
| 764 | - if (spip_sqlite_query("DROP TABLE$exist $table", $serveur, $requeter)) { |
|
| 765 | - return true; |
|
| 766 | - } else { |
|
| 767 | - return false; |
|
| 768 | - } |
|
| 760 | + if ($exist) { |
|
| 761 | + $exist = ' IF EXISTS'; |
|
| 762 | + } |
|
| 763 | + |
|
| 764 | + if (spip_sqlite_query("DROP TABLE$exist $table", $serveur, $requeter)) { |
|
| 765 | + return true; |
|
| 766 | + } else { |
|
| 767 | + return false; |
|
| 768 | + } |
|
| 769 | 769 | } |
| 770 | 770 | |
| 771 | 771 | |
@@ -782,11 +782,11 @@ discard block |
||
| 782 | 782 | */ |
| 783 | 783 | function spip_sqlite_drop_view($view, $exist = '', $serveur = '', $requeter = true) |
| 784 | 784 | { |
| 785 | - if ($exist) { |
|
| 786 | - $exist = ' IF EXISTS'; |
|
| 787 | - } |
|
| 785 | + if ($exist) { |
|
| 786 | + $exist = ' IF EXISTS'; |
|
| 787 | + } |
|
| 788 | 788 | |
| 789 | - return spip_sqlite_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 789 | + return spip_sqlite_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 790 | 790 | } |
| 791 | 791 | |
| 792 | 792 | /** |
@@ -801,20 +801,20 @@ discard block |
||
| 801 | 801 | */ |
| 802 | 802 | function spip_sqlite_drop_index($nom, $table, $serveur = '', $requeter = true) |
| 803 | 803 | { |
| 804 | - if (!($nom or $table)) { |
|
| 805 | - spip_log("Champ manquant pour supprimer un index sqlite ($nom, $table)", 'sqlite.' . _LOG_ERREUR); |
|
| 804 | + if (!($nom or $table)) { |
|
| 805 | + spip_log("Champ manquant pour supprimer un index sqlite ($nom, $table)", 'sqlite.' . _LOG_ERREUR); |
|
| 806 | 806 | |
| 807 | - return false; |
|
| 808 | - } |
|
| 807 | + return false; |
|
| 808 | + } |
|
| 809 | 809 | |
| 810 | - // SQLite ne differentie pas noms des index en fonction des tables |
|
| 811 | - // il faut donc creer des noms uniques d'index pour une base sqlite |
|
| 812 | - $index = $table . '_' . $nom; |
|
| 813 | - $exist = ' IF EXISTS'; |
|
| 810 | + // SQLite ne differentie pas noms des index en fonction des tables |
|
| 811 | + // il faut donc creer des noms uniques d'index pour une base sqlite |
|
| 812 | + $index = $table . '_' . $nom; |
|
| 813 | + $exist = ' IF EXISTS'; |
|
| 814 | 814 | |
| 815 | - $query = "DROP INDEX$exist $index"; |
|
| 815 | + $query = "DROP INDEX$exist $index"; |
|
| 816 | 816 | |
| 817 | - return spip_sqlite_query($query, $serveur, $requeter); |
|
| 817 | + return spip_sqlite_query($query, $serveur, $requeter); |
|
| 818 | 818 | } |
| 819 | 819 | |
| 820 | 820 | /** |
@@ -831,29 +831,29 @@ discard block |
||
| 831 | 831 | **/ |
| 832 | 832 | function spip_sqlite_error($query = '', $serveur = '') |
| 833 | 833 | { |
| 834 | - $link = _sqlite_link($serveur); |
|
| 835 | - |
|
| 836 | - if ($link) { |
|
| 837 | - $errs = $link->errorInfo(); |
|
| 838 | - $s = _sqlite_last_error_from_link($link); |
|
| 839 | - } else { |
|
| 840 | - $s = ': aucune ressource sqlite (link)'; |
|
| 841 | - } |
|
| 842 | - if ($s) { |
|
| 843 | - $trace = debug_backtrace(); |
|
| 844 | - if ($trace[0]['function'] != 'spip_sqlite_error') { |
|
| 845 | - spip_log("$s - $query - " . sql_error_backtrace(), 'sqlite.' . _LOG_ERREUR); |
|
| 846 | - } |
|
| 847 | - } |
|
| 848 | - |
|
| 849 | - return $s; |
|
| 834 | + $link = _sqlite_link($serveur); |
|
| 835 | + |
|
| 836 | + if ($link) { |
|
| 837 | + $errs = $link->errorInfo(); |
|
| 838 | + $s = _sqlite_last_error_from_link($link); |
|
| 839 | + } else { |
|
| 840 | + $s = ': aucune ressource sqlite (link)'; |
|
| 841 | + } |
|
| 842 | + if ($s) { |
|
| 843 | + $trace = debug_backtrace(); |
|
| 844 | + if ($trace[0]['function'] != 'spip_sqlite_error') { |
|
| 845 | + spip_log("$s - $query - " . sql_error_backtrace(), 'sqlite.' . _LOG_ERREUR); |
|
| 846 | + } |
|
| 847 | + } |
|
| 848 | + |
|
| 849 | + return $s; |
|
| 850 | 850 | } |
| 851 | 851 | |
| 852 | 852 | function _sqlite_last_error_from_link($link) |
| 853 | 853 | { |
| 854 | - if ($link) { |
|
| 855 | - $errs = $link->errorInfo(); |
|
| 856 | - /* |
|
| 854 | + if ($link) { |
|
| 855 | + $errs = $link->errorInfo(); |
|
| 856 | + /* |
|
| 857 | 857 | $errs[0] |
| 858 | 858 | numero SQLState ('HY000' souvent lors d'une erreur) |
| 859 | 859 | http://www.easysoft.com/developer/interfaces/odbc/sqlstate_status_return_codes.html |
@@ -863,11 +863,11 @@ discard block |
||
| 863 | 863 | $errs[2] |
| 864 | 864 | Le texte du message d'erreur |
| 865 | 865 | */ |
| 866 | - if (ltrim($errs[0], '0')) { // 00000 si pas d'erreur |
|
| 867 | - return "$errs[2]"; |
|
| 868 | - } |
|
| 869 | - } |
|
| 870 | - return ''; |
|
| 866 | + if (ltrim($errs[0], '0')) { // 00000 si pas d'erreur |
|
| 867 | + return "$errs[2]"; |
|
| 868 | + } |
|
| 869 | + } |
|
| 870 | + return ''; |
|
| 871 | 871 | } |
| 872 | 872 | |
| 873 | 873 | /** |
@@ -885,23 +885,23 @@ discard block |
||
| 885 | 885 | **/ |
| 886 | 886 | function spip_sqlite_errno($serveur = '') |
| 887 | 887 | { |
| 888 | - $link = _sqlite_link($serveur); |
|
| 889 | - |
|
| 890 | - if ($link) { |
|
| 891 | - $t = $link->errorInfo(); |
|
| 892 | - $s = ltrim($t[0], '0'); // 00000 si pas d'erreur |
|
| 893 | - if ($s) { |
|
| 894 | - $s .= ' / ' . $t[1]; |
|
| 895 | - } // ajoute l'erreur du moteur SQLite |
|
| 896 | - } else { |
|
| 897 | - $s = ': aucune ressource sqlite (link)'; |
|
| 898 | - } |
|
| 899 | - |
|
| 900 | - if ($s) { |
|
| 901 | - spip_log("Erreur sqlite $s", 'sqlite.' . _LOG_ERREUR); |
|
| 902 | - } |
|
| 903 | - |
|
| 904 | - return $s ? $s : 0; |
|
| 888 | + $link = _sqlite_link($serveur); |
|
| 889 | + |
|
| 890 | + if ($link) { |
|
| 891 | + $t = $link->errorInfo(); |
|
| 892 | + $s = ltrim($t[0], '0'); // 00000 si pas d'erreur |
|
| 893 | + if ($s) { |
|
| 894 | + $s .= ' / ' . $t[1]; |
|
| 895 | + } // ajoute l'erreur du moteur SQLite |
|
| 896 | + } else { |
|
| 897 | + $s = ': aucune ressource sqlite (link)'; |
|
| 898 | + } |
|
| 899 | + |
|
| 900 | + if ($s) { |
|
| 901 | + spip_log("Erreur sqlite $s", 'sqlite.' . _LOG_ERREUR); |
|
| 902 | + } |
|
| 903 | + |
|
| 904 | + return $s ? $s : 0; |
|
| 905 | 905 | } |
| 906 | 906 | |
| 907 | 907 | |
@@ -918,19 +918,19 @@ discard block |
||
| 918 | 918 | */ |
| 919 | 919 | function spip_sqlite_explain($query, $serveur = '', $requeter = true) |
| 920 | 920 | { |
| 921 | - if (strpos(ltrim($query), 'SELECT') !== 0) { |
|
| 922 | - return []; |
|
| 923 | - } |
|
| 924 | - |
|
| 925 | - $query = Sqlite::traduire_requete($query, $serveur); |
|
| 926 | - $query = 'EXPLAIN ' . $query; |
|
| 927 | - if (!$requeter) { |
|
| 928 | - return $query; |
|
| 929 | - } |
|
| 930 | - // on ne trace pas ces requetes, sinon on obtient un tracage sans fin... |
|
| 931 | - $r = Sqlite::executer_requete($query, $serveur, false); |
|
| 932 | - |
|
| 933 | - return $r ? spip_sqlite_fetch($r, null, $serveur) : false; // hum ? etrange ca... a verifier |
|
| 921 | + if (strpos(ltrim($query), 'SELECT') !== 0) { |
|
| 922 | + return []; |
|
| 923 | + } |
|
| 924 | + |
|
| 925 | + $query = Sqlite::traduire_requete($query, $serveur); |
|
| 926 | + $query = 'EXPLAIN ' . $query; |
|
| 927 | + if (!$requeter) { |
|
| 928 | + return $query; |
|
| 929 | + } |
|
| 930 | + // on ne trace pas ces requetes, sinon on obtient un tracage sans fin... |
|
| 931 | + $r = Sqlite::executer_requete($query, $serveur, false); |
|
| 932 | + |
|
| 933 | + return $r ? spip_sqlite_fetch($r, null, $serveur) : false; // hum ? etrange ca... a verifier |
|
| 934 | 934 | } |
| 935 | 935 | |
| 936 | 936 | |
@@ -951,35 +951,35 @@ discard block |
||
| 951 | 951 | function spip_sqlite_fetch($r, $t = '', $serveur = '', $requeter = true) |
| 952 | 952 | { |
| 953 | 953 | |
| 954 | - $link = _sqlite_link($serveur); |
|
| 955 | - $t = $t ? $t : SPIP_SQLITE3_ASSOC; |
|
| 956 | - |
|
| 957 | - if (!$r) { |
|
| 958 | - return false; |
|
| 959 | - } |
|
| 960 | - |
|
| 961 | - $retour = $r->fetch($t); |
|
| 962 | - |
|
| 963 | - if (!$retour) { |
|
| 964 | - if ($r->errorCode() === '00000') { |
|
| 965 | - return null; |
|
| 966 | - } |
|
| 967 | - return false; |
|
| 968 | - } |
|
| 969 | - |
|
| 970 | - // Renvoie des 'table.titre' au lieu de 'titre' tout court ! pff ! |
|
| 971 | - // suppression de 'table.' pour toutes les cles (c'est un peu violent !) |
|
| 972 | - // c'est couteux : on ne verifie que la premiere ligne pour voir si on le fait ou non |
|
| 973 | - if (str_contains(implode('', array_keys($retour)), '.')) { |
|
| 974 | - foreach ($retour as $cle => $val) { |
|
| 975 | - if (($pos = strpos($cle, '.')) !== false) { |
|
| 976 | - $retour[substr($cle, $pos + 1)] = &$retour[$cle]; |
|
| 977 | - unset($retour[$cle]); |
|
| 978 | - } |
|
| 979 | - } |
|
| 980 | - } |
|
| 981 | - |
|
| 982 | - return $retour; |
|
| 954 | + $link = _sqlite_link($serveur); |
|
| 955 | + $t = $t ? $t : SPIP_SQLITE3_ASSOC; |
|
| 956 | + |
|
| 957 | + if (!$r) { |
|
| 958 | + return false; |
|
| 959 | + } |
|
| 960 | + |
|
| 961 | + $retour = $r->fetch($t); |
|
| 962 | + |
|
| 963 | + if (!$retour) { |
|
| 964 | + if ($r->errorCode() === '00000') { |
|
| 965 | + return null; |
|
| 966 | + } |
|
| 967 | + return false; |
|
| 968 | + } |
|
| 969 | + |
|
| 970 | + // Renvoie des 'table.titre' au lieu de 'titre' tout court ! pff ! |
|
| 971 | + // suppression de 'table.' pour toutes les cles (c'est un peu violent !) |
|
| 972 | + // c'est couteux : on ne verifie que la premiere ligne pour voir si on le fait ou non |
|
| 973 | + if (str_contains(implode('', array_keys($retour)), '.')) { |
|
| 974 | + foreach ($retour as $cle => $val) { |
|
| 975 | + if (($pos = strpos($cle, '.')) !== false) { |
|
| 976 | + $retour[substr($cle, $pos + 1)] = &$retour[$cle]; |
|
| 977 | + unset($retour[$cle]); |
|
| 978 | + } |
|
| 979 | + } |
|
| 980 | + } |
|
| 981 | + |
|
| 982 | + return $retour; |
|
| 983 | 983 | } |
| 984 | 984 | |
| 985 | 985 | /** |
@@ -993,8 +993,8 @@ discard block |
||
| 993 | 993 | **/ |
| 994 | 994 | function spip_sqlite_seek($r, $row_number, $serveur = '', $requeter = true) |
| 995 | 995 | { |
| 996 | - // encore un truc de bien fichu : PDO ne PEUT PAS faire de seek ou de rewind... |
|
| 997 | - return false; |
|
| 996 | + // encore un truc de bien fichu : PDO ne PEUT PAS faire de seek ou de rewind... |
|
| 997 | + return false; |
|
| 998 | 998 | } |
| 999 | 999 | |
| 1000 | 1000 | |
@@ -1011,10 +1011,10 @@ discard block |
||
| 1011 | 1011 | */ |
| 1012 | 1012 | function spip_sqlite_free(&$r, $serveur = '', $requeter = true) |
| 1013 | 1013 | { |
| 1014 | - unset($r); |
|
| 1014 | + unset($r); |
|
| 1015 | 1015 | |
| 1016 | - return true; |
|
| 1017 | - //return sqlite_free_result($r); |
|
| 1016 | + return true; |
|
| 1017 | + //return sqlite_free_result($r); |
|
| 1018 | 1018 | } |
| 1019 | 1019 | |
| 1020 | 1020 | |
@@ -1030,8 +1030,8 @@ discard block |
||
| 1030 | 1030 | */ |
| 1031 | 1031 | function spip_sqlite_get_charset($charset = [], $serveur = '', $requeter = true) |
| 1032 | 1032 | { |
| 1033 | - //$c = !$charset ? '' : (" LIKE "._q($charset['charset'])); |
|
| 1034 | - //return spip_sqlite_fetch(sqlite_query(_sqlite_link($serveur), "SHOW CHARACTER SET$c"), NULL, $serveur); |
|
| 1033 | + //$c = !$charset ? '' : (" LIKE "._q($charset['charset'])); |
|
| 1034 | + //return spip_sqlite_fetch(sqlite_query(_sqlite_link($serveur), "SHOW CHARACTER SET$c"), NULL, $serveur); |
|
| 1035 | 1035 | } |
| 1036 | 1036 | |
| 1037 | 1037 | |
@@ -1047,7 +1047,7 @@ discard block |
||
| 1047 | 1047 | **/ |
| 1048 | 1048 | function spip_sqlite_hex($v) |
| 1049 | 1049 | { |
| 1050 | - return hexdec($v); |
|
| 1050 | + return hexdec($v); |
|
| 1051 | 1051 | } |
| 1052 | 1052 | |
| 1053 | 1053 | |
@@ -1070,7 +1070,7 @@ discard block |
||
| 1070 | 1070 | **/ |
| 1071 | 1071 | function spip_sqlite_in($val, $valeurs, $not = '', $serveur = '', $requeter = true) |
| 1072 | 1072 | { |
| 1073 | - return "($val $not IN ($valeurs))"; |
|
| 1073 | + return "($val $not IN ($valeurs))"; |
|
| 1074 | 1074 | } |
| 1075 | 1075 | |
| 1076 | 1076 | |
@@ -1099,20 +1099,20 @@ discard block |
||
| 1099 | 1099 | function spip_sqlite_insert($table, $champs, $valeurs, $desc = [], $serveur = '', $requeter = true) |
| 1100 | 1100 | { |
| 1101 | 1101 | |
| 1102 | - $query = "INSERT INTO $table " . ($champs ? "$champs VALUES $valeurs" : 'DEFAULT VALUES'); |
|
| 1103 | - if ($r = spip_sqlite_query($query, $serveur, $requeter)) { |
|
| 1104 | - if (!$requeter) { |
|
| 1105 | - return $r; |
|
| 1106 | - } |
|
| 1107 | - $nb = Sqlite::last_insert_id($serveur); |
|
| 1108 | - } else { |
|
| 1109 | - $nb = false; |
|
| 1110 | - } |
|
| 1102 | + $query = "INSERT INTO $table " . ($champs ? "$champs VALUES $valeurs" : 'DEFAULT VALUES'); |
|
| 1103 | + if ($r = spip_sqlite_query($query, $serveur, $requeter)) { |
|
| 1104 | + if (!$requeter) { |
|
| 1105 | + return $r; |
|
| 1106 | + } |
|
| 1107 | + $nb = Sqlite::last_insert_id($serveur); |
|
| 1108 | + } else { |
|
| 1109 | + $nb = false; |
|
| 1110 | + } |
|
| 1111 | 1111 | |
| 1112 | - $err = spip_sqlite_error($query, $serveur); |
|
| 1112 | + $err = spip_sqlite_error($query, $serveur); |
|
| 1113 | 1113 | |
| 1114 | - // cas particulier : ne pas substituer la reponse spip_sqlite_query si on est en profilage |
|
| 1115 | - return isset($_GET['var_profile']) ? $r : $nb; |
|
| 1114 | + // cas particulier : ne pas substituer la reponse spip_sqlite_query si on est en profilage |
|
| 1115 | + return isset($_GET['var_profile']) ? $r : $nb; |
|
| 1116 | 1116 | } |
| 1117 | 1117 | |
| 1118 | 1118 | |
@@ -1138,28 +1138,28 @@ discard block |
||
| 1138 | 1138 | **/ |
| 1139 | 1139 | function spip_sqlite_insertq($table, $couples = [], $desc = [], $serveur = '', $requeter = true) |
| 1140 | 1140 | { |
| 1141 | - if (!$desc) { |
|
| 1142 | - $desc = description_table($table, $serveur); |
|
| 1143 | - } |
|
| 1144 | - if (!$desc) { |
|
| 1145 | - die("$table insertion sans description"); |
|
| 1146 | - } |
|
| 1147 | - $fields = isset($desc['field']) ? $desc['field'] : []; |
|
| 1148 | - |
|
| 1149 | - foreach ($couples as $champ => $val) { |
|
| 1150 | - $couples[$champ] = _sqlite_calculer_cite($val, $fields[$champ]); |
|
| 1151 | - } |
|
| 1152 | - |
|
| 1153 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1154 | - $couples = _sqlite_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1155 | - |
|
| 1156 | - $cles = $valeurs = ''; |
|
| 1157 | - if (count($couples)) { |
|
| 1158 | - $cles = '(' . join(',', array_keys($couples)) . ')'; |
|
| 1159 | - $valeurs = '(' . join(',', $couples) . ')'; |
|
| 1160 | - } |
|
| 1161 | - |
|
| 1162 | - return spip_sqlite_insert($table, $cles, $valeurs, $desc, $serveur, $requeter); |
|
| 1141 | + if (!$desc) { |
|
| 1142 | + $desc = description_table($table, $serveur); |
|
| 1143 | + } |
|
| 1144 | + if (!$desc) { |
|
| 1145 | + die("$table insertion sans description"); |
|
| 1146 | + } |
|
| 1147 | + $fields = isset($desc['field']) ? $desc['field'] : []; |
|
| 1148 | + |
|
| 1149 | + foreach ($couples as $champ => $val) { |
|
| 1150 | + $couples[$champ] = _sqlite_calculer_cite($val, $fields[$champ]); |
|
| 1151 | + } |
|
| 1152 | + |
|
| 1153 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1154 | + $couples = _sqlite_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1155 | + |
|
| 1156 | + $cles = $valeurs = ''; |
|
| 1157 | + if (count($couples)) { |
|
| 1158 | + $cles = '(' . join(',', array_keys($couples)) . ')'; |
|
| 1159 | + $valeurs = '(' . join(',', $couples) . ')'; |
|
| 1160 | + } |
|
| 1161 | + |
|
| 1162 | + return spip_sqlite_insert($table, $cles, $valeurs, $desc, $serveur, $requeter); |
|
| 1163 | 1163 | } |
| 1164 | 1164 | |
| 1165 | 1165 | |
@@ -1184,70 +1184,70 @@ discard block |
||
| 1184 | 1184 | **/ |
| 1185 | 1185 | function spip_sqlite_insertq_multi($table, $tab_couples = [], $desc = [], $serveur = '', $requeter = true) |
| 1186 | 1186 | { |
| 1187 | - if (!$desc) { |
|
| 1188 | - $desc = description_table($table, $serveur); |
|
| 1189 | - } |
|
| 1190 | - if (!$desc) { |
|
| 1191 | - die("$table insertion sans description"); |
|
| 1192 | - } |
|
| 1193 | - if (!isset($desc['field'])) { |
|
| 1194 | - $desc['field'] = []; |
|
| 1195 | - } |
|
| 1196 | - |
|
| 1197 | - // recuperer les champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1198 | - $maj = _sqlite_ajouter_champs_timestamp($table, [], $desc, $serveur); |
|
| 1199 | - |
|
| 1200 | - // seul le nom de la table est a traduire ici : |
|
| 1201 | - // le faire une seule fois au debut |
|
| 1202 | - $query_start = "INSERT INTO $table "; |
|
| 1203 | - $query_start = Sqlite::traduire_requete($query_start, $serveur); |
|
| 1204 | - |
|
| 1205 | - // ouvrir une transaction |
|
| 1206 | - if ($requeter) { |
|
| 1207 | - Sqlite::demarrer_transaction($serveur); |
|
| 1208 | - } |
|
| 1209 | - |
|
| 1210 | - while ($couples = array_shift($tab_couples)) { |
|
| 1211 | - foreach ($couples as $champ => $val) { |
|
| 1212 | - $couples[$champ] = _sqlite_calculer_cite($val, $desc['field'][$champ]); |
|
| 1213 | - } |
|
| 1214 | - |
|
| 1215 | - // inserer les champs timestamp par defaut |
|
| 1216 | - $couples = array_merge($maj, $couples); |
|
| 1217 | - |
|
| 1218 | - $champs = $valeurs = ''; |
|
| 1219 | - if (count($couples)) { |
|
| 1220 | - $champs = '(' . join(',', array_keys($couples)) . ')'; |
|
| 1221 | - $valeurs = '(' . join(',', $couples) . ')'; |
|
| 1222 | - $query = $query_start . "$champs VALUES $valeurs"; |
|
| 1223 | - } else { |
|
| 1224 | - $query = $query_start . 'DEFAULT VALUES'; |
|
| 1225 | - } |
|
| 1226 | - |
|
| 1227 | - if ($requeter) { |
|
| 1228 | - $retour = Sqlite::executer_requete($query, $serveur); |
|
| 1229 | - } |
|
| 1230 | - |
|
| 1231 | - // sur le dernier couple uniquement |
|
| 1232 | - if (!count($tab_couples)) { |
|
| 1233 | - $nb = 0; |
|
| 1234 | - if ($requeter) { |
|
| 1235 | - $nb = Sqlite::last_insert_id($serveur); |
|
| 1236 | - } else { |
|
| 1237 | - return $query; |
|
| 1238 | - } |
|
| 1239 | - } |
|
| 1240 | - |
|
| 1241 | - $err = spip_sqlite_error($query, $serveur); |
|
| 1242 | - } |
|
| 1243 | - |
|
| 1244 | - if ($requeter) { |
|
| 1245 | - Sqlite::finir_transaction($serveur); |
|
| 1246 | - } |
|
| 1247 | - |
|
| 1248 | - // renvoie le dernier id d'autoincrement ajoute |
|
| 1249 | - // cas particulier : ne pas substituer la reponse spip_sqlite_query si on est en profilage |
|
| 1250 | - return isset($_GET['var_profile']) ? $retour : $nb; |
|
| 1187 | + if (!$desc) { |
|
| 1188 | + $desc = description_table($table, $serveur); |
|
| 1189 | + } |
|
| 1190 | + if (!$desc) { |
|
| 1191 | + die("$table insertion sans description"); |
|
| 1192 | + } |
|
| 1193 | + if (!isset($desc['field'])) { |
|
| 1194 | + $desc['field'] = []; |
|
| 1195 | + } |
|
| 1196 | + |
|
| 1197 | + // recuperer les champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1198 | + $maj = _sqlite_ajouter_champs_timestamp($table, [], $desc, $serveur); |
|
| 1199 | + |
|
| 1200 | + // seul le nom de la table est a traduire ici : |
|
| 1201 | + // le faire une seule fois au debut |
|
| 1202 | + $query_start = "INSERT INTO $table "; |
|
| 1203 | + $query_start = Sqlite::traduire_requete($query_start, $serveur); |
|
| 1204 | + |
|
| 1205 | + // ouvrir une transaction |
|
| 1206 | + if ($requeter) { |
|
| 1207 | + Sqlite::demarrer_transaction($serveur); |
|
| 1208 | + } |
|
| 1209 | + |
|
| 1210 | + while ($couples = array_shift($tab_couples)) { |
|
| 1211 | + foreach ($couples as $champ => $val) { |
|
| 1212 | + $couples[$champ] = _sqlite_calculer_cite($val, $desc['field'][$champ]); |
|
| 1213 | + } |
|
| 1214 | + |
|
| 1215 | + // inserer les champs timestamp par defaut |
|
| 1216 | + $couples = array_merge($maj, $couples); |
|
| 1217 | + |
|
| 1218 | + $champs = $valeurs = ''; |
|
| 1219 | + if (count($couples)) { |
|
| 1220 | + $champs = '(' . join(',', array_keys($couples)) . ')'; |
|
| 1221 | + $valeurs = '(' . join(',', $couples) . ')'; |
|
| 1222 | + $query = $query_start . "$champs VALUES $valeurs"; |
|
| 1223 | + } else { |
|
| 1224 | + $query = $query_start . 'DEFAULT VALUES'; |
|
| 1225 | + } |
|
| 1226 | + |
|
| 1227 | + if ($requeter) { |
|
| 1228 | + $retour = Sqlite::executer_requete($query, $serveur); |
|
| 1229 | + } |
|
| 1230 | + |
|
| 1231 | + // sur le dernier couple uniquement |
|
| 1232 | + if (!count($tab_couples)) { |
|
| 1233 | + $nb = 0; |
|
| 1234 | + if ($requeter) { |
|
| 1235 | + $nb = Sqlite::last_insert_id($serveur); |
|
| 1236 | + } else { |
|
| 1237 | + return $query; |
|
| 1238 | + } |
|
| 1239 | + } |
|
| 1240 | + |
|
| 1241 | + $err = spip_sqlite_error($query, $serveur); |
|
| 1242 | + } |
|
| 1243 | + |
|
| 1244 | + if ($requeter) { |
|
| 1245 | + Sqlite::finir_transaction($serveur); |
|
| 1246 | + } |
|
| 1247 | + |
|
| 1248 | + // renvoie le dernier id d'autoincrement ajoute |
|
| 1249 | + // cas particulier : ne pas substituer la reponse spip_sqlite_query si on est en profilage |
|
| 1250 | + return isset($_GET['var_profile']) ? $retour : $nb; |
|
| 1251 | 1251 | } |
| 1252 | 1252 | |
| 1253 | 1253 | |
@@ -1263,7 +1263,7 @@ discard block |
||
| 1263 | 1263 | **/ |
| 1264 | 1264 | function spip_sqlite_preferer_transaction($serveur = '', $requeter = true) |
| 1265 | 1265 | { |
| 1266 | - return true; |
|
| 1266 | + return true; |
|
| 1267 | 1267 | } |
| 1268 | 1268 | |
| 1269 | 1269 | /** |
@@ -1281,12 +1281,12 @@ discard block |
||
| 1281 | 1281 | **/ |
| 1282 | 1282 | function spip_sqlite_demarrer_transaction($serveur = '', $requeter = true) |
| 1283 | 1283 | { |
| 1284 | - if (!$requeter) { |
|
| 1285 | - return 'BEGIN TRANSACTION'; |
|
| 1286 | - } |
|
| 1287 | - Sqlite::demarrer_transaction($serveur); |
|
| 1284 | + if (!$requeter) { |
|
| 1285 | + return 'BEGIN TRANSACTION'; |
|
| 1286 | + } |
|
| 1287 | + Sqlite::demarrer_transaction($serveur); |
|
| 1288 | 1288 | |
| 1289 | - return true; |
|
| 1289 | + return true; |
|
| 1290 | 1290 | } |
| 1291 | 1291 | |
| 1292 | 1292 | /** |
@@ -1301,12 +1301,12 @@ discard block |
||
| 1301 | 1301 | **/ |
| 1302 | 1302 | function spip_sqlite_terminer_transaction($serveur = '', $requeter = true) |
| 1303 | 1303 | { |
| 1304 | - if (!$requeter) { |
|
| 1305 | - return 'COMMIT'; |
|
| 1306 | - } |
|
| 1307 | - Sqlite::finir_transaction($serveur); |
|
| 1304 | + if (!$requeter) { |
|
| 1305 | + return 'COMMIT'; |
|
| 1306 | + } |
|
| 1307 | + Sqlite::finir_transaction($serveur); |
|
| 1308 | 1308 | |
| 1309 | - return true; |
|
| 1309 | + return true; |
|
| 1310 | 1310 | } |
| 1311 | 1311 | |
| 1312 | 1312 | |
@@ -1322,27 +1322,27 @@ discard block |
||
| 1322 | 1322 | **/ |
| 1323 | 1323 | function spip_sqlite_listdbs($serveur = '', $requeter = true) |
| 1324 | 1324 | { |
| 1325 | - _sqlite_init(); |
|
| 1326 | - |
|
| 1327 | - if (!is_dir($d = substr(_DIR_DB, 0, -1))) { |
|
| 1328 | - return []; |
|
| 1329 | - } |
|
| 1330 | - |
|
| 1331 | - include_spip('inc/flock'); |
|
| 1332 | - $bases = preg_files($d, $pattern = '(.*)\.sqlite$'); |
|
| 1333 | - $bds = []; |
|
| 1334 | - |
|
| 1335 | - foreach ($bases as $b) { |
|
| 1336 | - // pas de bases commencant pas sqlite |
|
| 1337 | - // (on s'en sert pour l'installation pour simuler la presence d'un serveur) |
|
| 1338 | - // les bases sont de la forme _sqliteX_tmp_spip_install.sqlite |
|
| 1339 | - if (strpos($b, '_sqlite')) { |
|
| 1340 | - continue; |
|
| 1341 | - } |
|
| 1342 | - $bds[] = preg_replace(";.*/$pattern;iS", '$1', $b); |
|
| 1343 | - } |
|
| 1344 | - |
|
| 1345 | - return $bds; |
|
| 1325 | + _sqlite_init(); |
|
| 1326 | + |
|
| 1327 | + if (!is_dir($d = substr(_DIR_DB, 0, -1))) { |
|
| 1328 | + return []; |
|
| 1329 | + } |
|
| 1330 | + |
|
| 1331 | + include_spip('inc/flock'); |
|
| 1332 | + $bases = preg_files($d, $pattern = '(.*)\.sqlite$'); |
|
| 1333 | + $bds = []; |
|
| 1334 | + |
|
| 1335 | + foreach ($bases as $b) { |
|
| 1336 | + // pas de bases commencant pas sqlite |
|
| 1337 | + // (on s'en sert pour l'installation pour simuler la presence d'un serveur) |
|
| 1338 | + // les bases sont de la forme _sqliteX_tmp_spip_install.sqlite |
|
| 1339 | + if (strpos($b, '_sqlite')) { |
|
| 1340 | + continue; |
|
| 1341 | + } |
|
| 1342 | + $bds[] = preg_replace(";.*/$pattern;iS", '$1', $b); |
|
| 1343 | + } |
|
| 1344 | + |
|
| 1345 | + return $bds; |
|
| 1346 | 1346 | } |
| 1347 | 1347 | |
| 1348 | 1348 | |
@@ -1358,9 +1358,9 @@ discard block |
||
| 1358 | 1358 | */ |
| 1359 | 1359 | function spip_sqlite_multi($objet, $lang) |
| 1360 | 1360 | { |
| 1361 | - $r = 'EXTRAIRE_MULTI(' . $objet . ", '" . $lang . "') AS multi"; |
|
| 1361 | + $r = 'EXTRAIRE_MULTI(' . $objet . ", '" . $lang . "') AS multi"; |
|
| 1362 | 1362 | |
| 1363 | - return $r; |
|
| 1363 | + return $r; |
|
| 1364 | 1364 | } |
| 1365 | 1365 | |
| 1366 | 1366 | |
@@ -1378,15 +1378,15 @@ discard block |
||
| 1378 | 1378 | **/ |
| 1379 | 1379 | function spip_sqlite_optimize($table, $serveur = '', $requeter = true) |
| 1380 | 1380 | { |
| 1381 | - static $do = false; |
|
| 1382 | - if ($requeter and $do) { |
|
| 1383 | - return true; |
|
| 1384 | - } |
|
| 1385 | - if ($requeter) { |
|
| 1386 | - $do = true; |
|
| 1387 | - } |
|
| 1388 | - |
|
| 1389 | - return spip_sqlite_query('VACUUM', $serveur, $requeter); |
|
| 1381 | + static $do = false; |
|
| 1382 | + if ($requeter and $do) { |
|
| 1383 | + return true; |
|
| 1384 | + } |
|
| 1385 | + if ($requeter) { |
|
| 1386 | + $do = true; |
|
| 1387 | + } |
|
| 1388 | + |
|
| 1389 | + return spip_sqlite_query('VACUUM', $serveur, $requeter); |
|
| 1390 | 1390 | } |
| 1391 | 1391 | |
| 1392 | 1392 | |
@@ -1404,15 +1404,15 @@ discard block |
||
| 1404 | 1404 | */ |
| 1405 | 1405 | function spip_sqlite_quote($v, $type = '') |
| 1406 | 1406 | { |
| 1407 | - if (!is_array($v)) { |
|
| 1408 | - return _sqlite_calculer_cite($v, $type); |
|
| 1409 | - } |
|
| 1410 | - // si c'est un tableau, le parcourir en propageant le type |
|
| 1411 | - foreach ($v as $k => $r) { |
|
| 1412 | - $v[$k] = spip_sqlite_quote($r, $type); |
|
| 1413 | - } |
|
| 1414 | - |
|
| 1415 | - return join(',', $v); |
|
| 1407 | + if (!is_array($v)) { |
|
| 1408 | + return _sqlite_calculer_cite($v, $type); |
|
| 1409 | + } |
|
| 1410 | + // si c'est un tableau, le parcourir en propageant le type |
|
| 1411 | + foreach ($v as $k => $r) { |
|
| 1412 | + $v[$k] = spip_sqlite_quote($r, $type); |
|
| 1413 | + } |
|
| 1414 | + |
|
| 1415 | + return join(',', $v); |
|
| 1416 | 1416 | } |
| 1417 | 1417 | |
| 1418 | 1418 | |
@@ -1430,9 +1430,9 @@ discard block |
||
| 1430 | 1430 | **/ |
| 1431 | 1431 | function spip_sqlite_date_proche($champ, $interval, $unite) |
| 1432 | 1432 | { |
| 1433 | - $op = (($interval <= 0) ? '>' : '<'); |
|
| 1433 | + $op = (($interval <= 0) ? '>' : '<'); |
|
| 1434 | 1434 | |
| 1435 | - return "($champ $op datetime('" . date('Y-m-d H:i:s') . "', '$interval $unite'))"; |
|
| 1435 | + return "($champ $op datetime('" . date('Y-m-d H:i:s') . "', '$interval $unite'))"; |
|
| 1436 | 1436 | } |
| 1437 | 1437 | |
| 1438 | 1438 | |
@@ -1451,48 +1451,48 @@ discard block |
||
| 1451 | 1451 | */ |
| 1452 | 1452 | function spip_sqlite_repair($table, $serveur = '', $requeter = true) |
| 1453 | 1453 | { |
| 1454 | - if ( |
|
| 1455 | - $desc = spip_sqlite_showtable($table, $serveur) |
|
| 1456 | - and isset($desc['field']) |
|
| 1457 | - and is_array($desc['field']) |
|
| 1458 | - ) { |
|
| 1459 | - foreach ($desc['field'] as $c => $d) { |
|
| 1460 | - if ( |
|
| 1461 | - preg_match(',^(tinytext|mediumtext|text|longtext|varchar|char),i', $d) |
|
| 1462 | - and stripos($d, 'NOT NULL') !== false |
|
| 1463 | - and stripos($d, 'DEFAULT') === false |
|
| 1464 | - /* pas touche aux cles primaires */ |
|
| 1465 | - and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1466 | - ) { |
|
| 1467 | - spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT ''", $serveur); |
|
| 1468 | - spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1469 | - } |
|
| 1470 | - if ( |
|
| 1471 | - preg_match(',^(INTEGER),i', $d) |
|
| 1472 | - and stripos($d, 'NOT NULL') !== false |
|
| 1473 | - and stripos($d, 'DEFAULT') === false |
|
| 1474 | - /* pas touche aux cles primaires */ |
|
| 1475 | - and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1476 | - ) { |
|
| 1477 | - spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT '0'", $serveur); |
|
| 1478 | - spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1479 | - } |
|
| 1480 | - if ( |
|
| 1481 | - preg_match(',^(datetime),i', $d) |
|
| 1482 | - and stripos($d, 'NOT NULL') !== false |
|
| 1483 | - and stripos($d, 'DEFAULT') === false |
|
| 1484 | - /* pas touche aux cles primaires */ |
|
| 1485 | - and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1486 | - ) { |
|
| 1487 | - spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT '0000-00-00 00:00:00'", $serveur); |
|
| 1488 | - spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1489 | - } |
|
| 1490 | - } |
|
| 1491 | - |
|
| 1492 | - return [' OK ']; |
|
| 1493 | - } |
|
| 1494 | - |
|
| 1495 | - return [' ERROR ']; |
|
| 1454 | + if ( |
|
| 1455 | + $desc = spip_sqlite_showtable($table, $serveur) |
|
| 1456 | + and isset($desc['field']) |
|
| 1457 | + and is_array($desc['field']) |
|
| 1458 | + ) { |
|
| 1459 | + foreach ($desc['field'] as $c => $d) { |
|
| 1460 | + if ( |
|
| 1461 | + preg_match(',^(tinytext|mediumtext|text|longtext|varchar|char),i', $d) |
|
| 1462 | + and stripos($d, 'NOT NULL') !== false |
|
| 1463 | + and stripos($d, 'DEFAULT') === false |
|
| 1464 | + /* pas touche aux cles primaires */ |
|
| 1465 | + and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1466 | + ) { |
|
| 1467 | + spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT ''", $serveur); |
|
| 1468 | + spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1469 | + } |
|
| 1470 | + if ( |
|
| 1471 | + preg_match(',^(INTEGER),i', $d) |
|
| 1472 | + and stripos($d, 'NOT NULL') !== false |
|
| 1473 | + and stripos($d, 'DEFAULT') === false |
|
| 1474 | + /* pas touche aux cles primaires */ |
|
| 1475 | + and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1476 | + ) { |
|
| 1477 | + spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT '0'", $serveur); |
|
| 1478 | + spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1479 | + } |
|
| 1480 | + if ( |
|
| 1481 | + preg_match(',^(datetime),i', $d) |
|
| 1482 | + and stripos($d, 'NOT NULL') !== false |
|
| 1483 | + and stripos($d, 'DEFAULT') === false |
|
| 1484 | + /* pas touche aux cles primaires */ |
|
| 1485 | + and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1486 | + ) { |
|
| 1487 | + spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT '0000-00-00 00:00:00'", $serveur); |
|
| 1488 | + spip_log("ALTER $q", 'repair' . _LOG_INFO_IMPORTANTE); |
|
| 1489 | + } |
|
| 1490 | + } |
|
| 1491 | + |
|
| 1492 | + return [' OK ']; |
|
| 1493 | + } |
|
| 1494 | + |
|
| 1495 | + return [' ERROR ']; |
|
| 1496 | 1496 | } |
| 1497 | 1497 | |
| 1498 | 1498 | |
@@ -1522,25 +1522,25 @@ discard block |
||
| 1522 | 1522 | **/ |
| 1523 | 1523 | function spip_sqlite_replace($table, $couples, $desc = [], $serveur = '', $requeter = true) |
| 1524 | 1524 | { |
| 1525 | - if (!$desc) { |
|
| 1526 | - $desc = description_table($table, $serveur); |
|
| 1527 | - } |
|
| 1528 | - if (!$desc) { |
|
| 1529 | - die("$table insertion sans description"); |
|
| 1530 | - } |
|
| 1531 | - $fields = isset($desc['field']) ? $desc['field'] : []; |
|
| 1532 | - |
|
| 1533 | - foreach ($couples as $champ => $val) { |
|
| 1534 | - $couples[$champ] = _sqlite_calculer_cite($val, $fields[$champ]); |
|
| 1535 | - } |
|
| 1536 | - |
|
| 1537 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1538 | - $couples = _sqlite_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1539 | - |
|
| 1540 | - return spip_sqlite_query("REPLACE INTO $table (" . join(',', array_keys($couples)) . ') VALUES (' . join( |
|
| 1541 | - ',', |
|
| 1542 | - $couples |
|
| 1543 | - ) . ')', $serveur); |
|
| 1525 | + if (!$desc) { |
|
| 1526 | + $desc = description_table($table, $serveur); |
|
| 1527 | + } |
|
| 1528 | + if (!$desc) { |
|
| 1529 | + die("$table insertion sans description"); |
|
| 1530 | + } |
|
| 1531 | + $fields = isset($desc['field']) ? $desc['field'] : []; |
|
| 1532 | + |
|
| 1533 | + foreach ($couples as $champ => $val) { |
|
| 1534 | + $couples[$champ] = _sqlite_calculer_cite($val, $fields[$champ]); |
|
| 1535 | + } |
|
| 1536 | + |
|
| 1537 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1538 | + $couples = _sqlite_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1539 | + |
|
| 1540 | + return spip_sqlite_query("REPLACE INTO $table (" . join(',', array_keys($couples)) . ') VALUES (' . join( |
|
| 1541 | + ',', |
|
| 1542 | + $couples |
|
| 1543 | + ) . ')', $serveur); |
|
| 1544 | 1544 | } |
| 1545 | 1545 | |
| 1546 | 1546 | |
@@ -1571,13 +1571,13 @@ discard block |
||
| 1571 | 1571 | function spip_sqlite_replace_multi($table, $tab_couples, $desc = [], $serveur = '', $requeter = true) |
| 1572 | 1572 | { |
| 1573 | 1573 | |
| 1574 | - // boucler pour trainter chaque requete independemment |
|
| 1575 | - foreach ($tab_couples as $couples) { |
|
| 1576 | - $retour = spip_sqlite_replace($table, $couples, $desc, $serveur, $requeter); |
|
| 1577 | - } |
|
| 1574 | + // boucler pour trainter chaque requete independemment |
|
| 1575 | + foreach ($tab_couples as $couples) { |
|
| 1576 | + $retour = spip_sqlite_replace($table, $couples, $desc, $serveur, $requeter); |
|
| 1577 | + } |
|
| 1578 | 1578 | |
| 1579 | - // renvoie le dernier id |
|
| 1580 | - return $retour; |
|
| 1579 | + // renvoie le dernier id |
|
| 1580 | + return $retour; |
|
| 1581 | 1581 | } |
| 1582 | 1582 | |
| 1583 | 1583 | |
@@ -1604,44 +1604,44 @@ discard block |
||
| 1604 | 1604 | * - array : Tableau décrivant requête et temps d'exécution si var_profile actif pour tracer. |
| 1605 | 1605 | */ |
| 1606 | 1606 | function spip_sqlite_select( |
| 1607 | - $select, |
|
| 1608 | - $from, |
|
| 1609 | - $where = '', |
|
| 1610 | - $groupby = '', |
|
| 1611 | - $orderby = '', |
|
| 1612 | - $limit = '', |
|
| 1613 | - $having = '', |
|
| 1614 | - $serveur = '', |
|
| 1615 | - $requeter = true |
|
| 1607 | + $select, |
|
| 1608 | + $from, |
|
| 1609 | + $where = '', |
|
| 1610 | + $groupby = '', |
|
| 1611 | + $orderby = '', |
|
| 1612 | + $limit = '', |
|
| 1613 | + $having = '', |
|
| 1614 | + $serveur = '', |
|
| 1615 | + $requeter = true |
|
| 1616 | 1616 | ) { |
| 1617 | 1617 | |
| 1618 | - // version() n'est pas connu de sqlite |
|
| 1619 | - $select = str_replace('version()', 'sqlite_version()', $select); |
|
| 1620 | - |
|
| 1621 | - // recomposer from |
|
| 1622 | - $from = (!is_array($from) ? $from : _sqlite_calculer_select_as($from)); |
|
| 1623 | - |
|
| 1624 | - $query = |
|
| 1625 | - _sqlite_calculer_expression('SELECT', $select, ', ') |
|
| 1626 | - . _sqlite_calculer_expression('FROM', $from, ', ') |
|
| 1627 | - . _sqlite_calculer_expression('WHERE', $where) |
|
| 1628 | - . _sqlite_calculer_expression('GROUP BY', $groupby, ',') |
|
| 1629 | - . _sqlite_calculer_expression('HAVING', $having) |
|
| 1630 | - . ($orderby ? ("\nORDER BY " . _sqlite_calculer_order($orderby)) : '') |
|
| 1631 | - . ($limit ? "\nLIMIT $limit" : ''); |
|
| 1632 | - |
|
| 1633 | - // dans un select, on doit renvoyer la requête en cas d'erreur |
|
| 1634 | - $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1635 | - // texte de la requete demande ? |
|
| 1636 | - if (!$requeter) { |
|
| 1637 | - return $res; |
|
| 1638 | - } |
|
| 1639 | - // erreur survenue ? |
|
| 1640 | - if ($res === false) { |
|
| 1641 | - return Sqlite::traduire_requete($query, $serveur); |
|
| 1642 | - } |
|
| 1643 | - |
|
| 1644 | - return $res; |
|
| 1618 | + // version() n'est pas connu de sqlite |
|
| 1619 | + $select = str_replace('version()', 'sqlite_version()', $select); |
|
| 1620 | + |
|
| 1621 | + // recomposer from |
|
| 1622 | + $from = (!is_array($from) ? $from : _sqlite_calculer_select_as($from)); |
|
| 1623 | + |
|
| 1624 | + $query = |
|
| 1625 | + _sqlite_calculer_expression('SELECT', $select, ', ') |
|
| 1626 | + . _sqlite_calculer_expression('FROM', $from, ', ') |
|
| 1627 | + . _sqlite_calculer_expression('WHERE', $where) |
|
| 1628 | + . _sqlite_calculer_expression('GROUP BY', $groupby, ',') |
|
| 1629 | + . _sqlite_calculer_expression('HAVING', $having) |
|
| 1630 | + . ($orderby ? ("\nORDER BY " . _sqlite_calculer_order($orderby)) : '') |
|
| 1631 | + . ($limit ? "\nLIMIT $limit" : ''); |
|
| 1632 | + |
|
| 1633 | + // dans un select, on doit renvoyer la requête en cas d'erreur |
|
| 1634 | + $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1635 | + // texte de la requete demande ? |
|
| 1636 | + if (!$requeter) { |
|
| 1637 | + return $res; |
|
| 1638 | + } |
|
| 1639 | + // erreur survenue ? |
|
| 1640 | + if ($res === false) { |
|
| 1641 | + return Sqlite::traduire_requete($query, $serveur); |
|
| 1642 | + } |
|
| 1643 | + |
|
| 1644 | + return $res; |
|
| 1645 | 1645 | } |
| 1646 | 1646 | |
| 1647 | 1647 | |
@@ -1661,32 +1661,32 @@ discard block |
||
| 1661 | 1661 | **/ |
| 1662 | 1662 | function spip_sqlite_selectdb($db, $serveur = '', $requeter = true) |
| 1663 | 1663 | { |
| 1664 | - _sqlite_init(); |
|
| 1665 | - |
|
| 1666 | - // interdire la creation d'une nouvelle base, |
|
| 1667 | - // sauf si on est dans l'installation |
|
| 1668 | - if ( |
|
| 1669 | - !is_file($f = _DIR_DB . $db . '.sqlite') |
|
| 1670 | - && (!defined('_ECRIRE_INSTALL') || !_ECRIRE_INSTALL) |
|
| 1671 | - ) { |
|
| 1672 | - spip_log("Il est interdit de creer la base $db", 'sqlite.' . _LOG_HS); |
|
| 1673 | - |
|
| 1674 | - return false; |
|
| 1675 | - } |
|
| 1676 | - |
|
| 1677 | - // se connecter a la base indiquee |
|
| 1678 | - // avec les identifiants connus |
|
| 1679 | - $index = $serveur ? $serveur : 0; |
|
| 1680 | - |
|
| 1681 | - if ($link = spip_connect_db('', '', '', '', '@selectdb@' . $db, $serveur, '', '')) { |
|
| 1682 | - if (($db == $link['db']) && $GLOBALS['connexions'][$index] = $link) { |
|
| 1683 | - return $db; |
|
| 1684 | - } |
|
| 1685 | - } else { |
|
| 1686 | - spip_log("Impossible de selectionner la base $db", 'sqlite.' . _LOG_HS); |
|
| 1687 | - } |
|
| 1688 | - |
|
| 1689 | - return false; |
|
| 1664 | + _sqlite_init(); |
|
| 1665 | + |
|
| 1666 | + // interdire la creation d'une nouvelle base, |
|
| 1667 | + // sauf si on est dans l'installation |
|
| 1668 | + if ( |
|
| 1669 | + !is_file($f = _DIR_DB . $db . '.sqlite') |
|
| 1670 | + && (!defined('_ECRIRE_INSTALL') || !_ECRIRE_INSTALL) |
|
| 1671 | + ) { |
|
| 1672 | + spip_log("Il est interdit de creer la base $db", 'sqlite.' . _LOG_HS); |
|
| 1673 | + |
|
| 1674 | + return false; |
|
| 1675 | + } |
|
| 1676 | + |
|
| 1677 | + // se connecter a la base indiquee |
|
| 1678 | + // avec les identifiants connus |
|
| 1679 | + $index = $serveur ? $serveur : 0; |
|
| 1680 | + |
|
| 1681 | + if ($link = spip_connect_db('', '', '', '', '@selectdb@' . $db, $serveur, '', '')) { |
|
| 1682 | + if (($db == $link['db']) && $GLOBALS['connexions'][$index] = $link) { |
|
| 1683 | + return $db; |
|
| 1684 | + } |
|
| 1685 | + } else { |
|
| 1686 | + spip_log("Impossible de selectionner la base $db", 'sqlite.' . _LOG_HS); |
|
| 1687 | + } |
|
| 1688 | + |
|
| 1689 | + return false; |
|
| 1690 | 1690 | } |
| 1691 | 1691 | |
| 1692 | 1692 | |
@@ -1702,8 +1702,8 @@ discard block |
||
| 1702 | 1702 | */ |
| 1703 | 1703 | function spip_sqlite_set_charset($charset, $serveur = '', $requeter = true) |
| 1704 | 1704 | { |
| 1705 | - # spip_log("Gestion charset sql a ecrire : "."SET NAMES "._q($charset), 'sqlite.'._LOG_ERREUR); |
|
| 1706 | - # return spip_sqlite_query("SET NAMES ". spip_sqlite_quote($charset), $serveur); //<-- Passe pas ! |
|
| 1705 | + # spip_log("Gestion charset sql a ecrire : "."SET NAMES "._q($charset), 'sqlite.'._LOG_ERREUR); |
|
| 1706 | + # return spip_sqlite_query("SET NAMES ". spip_sqlite_quote($charset), $serveur); //<-- Passe pas ! |
|
| 1707 | 1707 | } |
| 1708 | 1708 | |
| 1709 | 1709 | |
@@ -1722,24 +1722,24 @@ discard block |
||
| 1722 | 1722 | **/ |
| 1723 | 1723 | function spip_sqlite_showbase($match, $serveur = '', $requeter = true) |
| 1724 | 1724 | { |
| 1725 | - // type est le type d'entrée : table / index / view |
|
| 1726 | - // on ne retourne que les tables (?) et non les vues... |
|
| 1727 | - # ESCAPE non supporte par les versions sqlite <3 |
|
| 1728 | - # return spip_sqlite_query("SELECT name FROM sqlite_master WHERE type='table' AND tbl_name LIKE "._q($match)." ESCAPE '\'", $serveur, $requeter); |
|
| 1729 | - $match = preg_quote($match); |
|
| 1730 | - $match = str_replace('\\\_', '[[TIRETBAS]]', $match); |
|
| 1731 | - $match = str_replace('\\\%', '[[POURCENT]]', $match); |
|
| 1732 | - $match = str_replace('_', '.', $match); |
|
| 1733 | - $match = str_replace('%', '.*', $match); |
|
| 1734 | - $match = str_replace('[[TIRETBAS]]', '_', $match); |
|
| 1735 | - $match = str_replace('[[POURCENT]]', '%', $match); |
|
| 1736 | - $match = "^$match$"; |
|
| 1737 | - |
|
| 1738 | - return spip_sqlite_query( |
|
| 1739 | - "SELECT name FROM sqlite_master WHERE type='table' AND tbl_name REGEXP " . _q($match), |
|
| 1740 | - $serveur, |
|
| 1741 | - $requeter |
|
| 1742 | - ); |
|
| 1725 | + // type est le type d'entrée : table / index / view |
|
| 1726 | + // on ne retourne que les tables (?) et non les vues... |
|
| 1727 | + # ESCAPE non supporte par les versions sqlite <3 |
|
| 1728 | + # return spip_sqlite_query("SELECT name FROM sqlite_master WHERE type='table' AND tbl_name LIKE "._q($match)." ESCAPE '\'", $serveur, $requeter); |
|
| 1729 | + $match = preg_quote($match); |
|
| 1730 | + $match = str_replace('\\\_', '[[TIRETBAS]]', $match); |
|
| 1731 | + $match = str_replace('\\\%', '[[POURCENT]]', $match); |
|
| 1732 | + $match = str_replace('_', '.', $match); |
|
| 1733 | + $match = str_replace('%', '.*', $match); |
|
| 1734 | + $match = str_replace('[[TIRETBAS]]', '_', $match); |
|
| 1735 | + $match = str_replace('[[POURCENT]]', '%', $match); |
|
| 1736 | + $match = "^$match$"; |
|
| 1737 | + |
|
| 1738 | + return spip_sqlite_query( |
|
| 1739 | + "SELECT name FROM sqlite_master WHERE type='table' AND tbl_name REGEXP " . _q($match), |
|
| 1740 | + $serveur, |
|
| 1741 | + $requeter |
|
| 1742 | + ); |
|
| 1743 | 1743 | } |
| 1744 | 1744 | |
| 1745 | 1745 | /** |
@@ -1758,19 +1758,19 @@ discard block |
||
| 1758 | 1758 | **/ |
| 1759 | 1759 | function spip_sqlite_table_exists(string $table, $serveur = '', $requeter = true) |
| 1760 | 1760 | { |
| 1761 | - $r = spip_sqlite_query( |
|
| 1762 | - 'SELECT name FROM sqlite_master WHERE' |
|
| 1763 | - . ' type=\'table\'' |
|
| 1764 | - . ' AND name=' . spip_sqlite_quote($table, 'string') |
|
| 1765 | - . ' AND name NOT LIKE \'sqlite_%\'', |
|
| 1766 | - $serveur, |
|
| 1767 | - $requeter |
|
| 1768 | - ); |
|
| 1769 | - if (!$requeter) { |
|
| 1770 | - return $r; |
|
| 1771 | - } |
|
| 1772 | - $res = spip_sqlite_fetch($r, '', $serveur); |
|
| 1773 | - return (bool) $res; |
|
| 1761 | + $r = spip_sqlite_query( |
|
| 1762 | + 'SELECT name FROM sqlite_master WHERE' |
|
| 1763 | + . ' type=\'table\'' |
|
| 1764 | + . ' AND name=' . spip_sqlite_quote($table, 'string') |
|
| 1765 | + . ' AND name NOT LIKE \'sqlite_%\'', |
|
| 1766 | + $serveur, |
|
| 1767 | + $requeter |
|
| 1768 | + ); |
|
| 1769 | + if (!$requeter) { |
|
| 1770 | + return $r; |
|
| 1771 | + } |
|
| 1772 | + $res = spip_sqlite_fetch($r, '', $serveur); |
|
| 1773 | + return (bool) $res; |
|
| 1774 | 1774 | } |
| 1775 | 1775 | |
| 1776 | 1776 | define('_SQLITE_RE_SHOW_TABLE', '/^[^(),]*\(((?:[^()]*\((?:[^()]*\([^()]*\))?[^()]*\)[^()]*)*[^()]*)\)[^()]*$/'); |
@@ -1794,129 +1794,129 @@ discard block |
||
| 1794 | 1794 | */ |
| 1795 | 1795 | function spip_sqlite_showtable($nom_table, $serveur = '', $requeter = true) |
| 1796 | 1796 | { |
| 1797 | - $query = |
|
| 1798 | - 'SELECT sql, type FROM' |
|
| 1799 | - . ' (SELECT * FROM sqlite_master UNION ALL' |
|
| 1800 | - . ' SELECT * FROM sqlite_temp_master)' |
|
| 1801 | - . " WHERE tbl_name LIKE '$nom_table'" |
|
| 1802 | - . " AND type!='meta' AND sql NOT NULL AND name NOT LIKE 'sqlite_%'" |
|
| 1803 | - . ' ORDER BY substr(type,2,1), name'; |
|
| 1804 | - |
|
| 1805 | - $a = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1806 | - if (!$a) { |
|
| 1807 | - return ''; |
|
| 1808 | - } |
|
| 1809 | - if (!$requeter) { |
|
| 1810 | - return $a; |
|
| 1811 | - } |
|
| 1812 | - if (!($a = spip_sqlite_fetch($a, null, $serveur))) { |
|
| 1813 | - return ''; |
|
| 1814 | - } |
|
| 1815 | - $vue = ($a['type'] == 'view'); // table | vue |
|
| 1816 | - |
|
| 1817 | - // c'est une table |
|
| 1818 | - // il faut parser le create |
|
| 1819 | - if (!$vue) { |
|
| 1820 | - if (!preg_match(_SQLITE_RE_SHOW_TABLE, array_shift($a), $r)) { |
|
| 1821 | - return ''; |
|
| 1822 | - } else { |
|
| 1823 | - $desc = $r[1]; |
|
| 1824 | - // extraction d'une KEY éventuelle en prenant garde de ne pas |
|
| 1825 | - // relever un champ dont le nom contient KEY (ex. ID_WHISKEY) |
|
| 1826 | - if (preg_match('/^(.*?),([^,]*\sKEY[ (].*)$/s', $desc, $r)) { |
|
| 1827 | - $namedkeys = $r[2]; |
|
| 1828 | - $desc = $r[1]; |
|
| 1829 | - } else { |
|
| 1830 | - $namedkeys = ''; |
|
| 1831 | - } |
|
| 1832 | - |
|
| 1833 | - $fields = []; |
|
| 1834 | - $keys = []; |
|
| 1835 | - |
|
| 1836 | - // enlever les contenus des valeurs DEFAULT 'xxx' qui pourraient perturber |
|
| 1837 | - // par exemple s'il contiennent une virgule. |
|
| 1838 | - // /!\ cela peut aussi echapper le nom des champs si la table a eu des operations avec SQLite Manager ! |
|
| 1839 | - list($desc, $echaps) = query_echappe_textes($desc); |
|
| 1840 | - |
|
| 1841 | - // separer toutes les descriptions de champs, separes par des virgules |
|
| 1842 | - # /!\ explode peut exploser aussi DECIMAL(10,2) ! |
|
| 1843 | - $k_precedent = null; |
|
| 1844 | - foreach (explode(',', $desc) as $v) { |
|
| 1845 | - preg_match('/^\s*([^\s]+)\s+(.*)/', $v, $r); |
|
| 1846 | - // Les cles de champs peuvent etre entourees |
|
| 1847 | - // de guillements doubles " , simples ', graves ` ou de crochets [ ], ou rien. |
|
| 1848 | - // http://www.sqlite.org/lang_keywords.html |
|
| 1849 | - $k = strtolower(query_reinjecte_textes($r[1], $echaps)); // champ, "champ", [champ]... |
|
| 1850 | - if ($char = strpbrk($k[0], '\'"[`')) { |
|
| 1851 | - $k = trim($k, $char); |
|
| 1852 | - if ($char == '[') { |
|
| 1853 | - $k = rtrim($k, ']'); |
|
| 1854 | - } |
|
| 1855 | - } |
|
| 1856 | - $def = query_reinjecte_textes($r[2], $echaps); // valeur du champ |
|
| 1857 | - |
|
| 1858 | - // rustine pour DECIMAL(10,2) |
|
| 1859 | - // s'il y a une parenthèse fermante dans la clé |
|
| 1860 | - // ou dans la définition sans qu'il n'y ait une ouverture avant |
|
| 1861 | - if (str_contains($k, ')') or preg_match('/^[^\(]*\)/', $def)) { |
|
| 1862 | - $fields[$k_precedent] .= ',' . $k . ' ' . $def; |
|
| 1863 | - continue; |
|
| 1864 | - } |
|
| 1865 | - |
|
| 1866 | - // la primary key peut etre dans une des descriptions de champs |
|
| 1867 | - // et non en fin de table, cas encore decouvert avec Sqlite Manager |
|
| 1868 | - if (stripos($r[2], 'PRIMARY KEY') !== false) { |
|
| 1869 | - $keys['PRIMARY KEY'] = $k; |
|
| 1870 | - } |
|
| 1871 | - |
|
| 1872 | - $fields[$k] = $def; |
|
| 1873 | - $k_precedent = $k; |
|
| 1874 | - } |
|
| 1875 | - // key inclues dans la requete |
|
| 1876 | - foreach (preg_split('/\)\s*(,|$)/', $namedkeys) as $v) { |
|
| 1877 | - if (preg_match('/^\s*([^(]*)\(([^(]*(\(\d+\))?)$/', $v, $r)) { |
|
| 1878 | - $k = str_replace('`', '', trim($r[1])); |
|
| 1879 | - $t = trim(strtolower(str_replace('`', '', $r[2])), '"'); |
|
| 1880 | - if ($k && !isset($keys[$k])) { |
|
| 1881 | - $keys[$k] = $t; |
|
| 1882 | - } else { |
|
| 1883 | - $keys[] = $t; |
|
| 1884 | - } |
|
| 1885 | - } |
|
| 1886 | - } |
|
| 1887 | - // sinon ajouter les key index |
|
| 1888 | - $query = |
|
| 1889 | - 'SELECT name,sql FROM' |
|
| 1890 | - . ' (SELECT * FROM sqlite_master UNION ALL' |
|
| 1891 | - . ' SELECT * FROM sqlite_temp_master)' |
|
| 1892 | - . " WHERE tbl_name LIKE '$nom_table'" |
|
| 1893 | - . " AND type='index' AND name NOT LIKE 'sqlite_%'" |
|
| 1894 | - . 'ORDER BY substr(type,2,1), name'; |
|
| 1895 | - $a = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1896 | - while ($r = spip_sqlite_fetch($a, null, $serveur)) { |
|
| 1897 | - $key = str_replace($nom_table . '_', '', $r['name']); // enlever le nom de la table ajoute a l'index |
|
| 1898 | - $keytype = 'KEY'; |
|
| 1899 | - if (strpos($r['sql'], 'UNIQUE INDEX') !== false) { |
|
| 1900 | - $keytype = 'UNIQUE KEY'; |
|
| 1901 | - } |
|
| 1902 | - $colonnes = preg_replace(',.*\((.*)\).*,', '$1', $r['sql']); |
|
| 1903 | - $keys[$keytype . ' ' . $key] = $colonnes; |
|
| 1904 | - } |
|
| 1905 | - } |
|
| 1906 | - } // c'est une vue, on liste les champs disponibles simplement |
|
| 1907 | - else { |
|
| 1908 | - if ($res = sql_fetsel('*', $nom_table, '', '', '', '1', '', $serveur)) { // limit 1 |
|
| 1909 | - $fields = []; |
|
| 1910 | - foreach ($res as $c => $v) { |
|
| 1911 | - $fields[$c] = ''; |
|
| 1912 | - } |
|
| 1913 | - $keys = []; |
|
| 1914 | - } else { |
|
| 1915 | - return ''; |
|
| 1916 | - } |
|
| 1917 | - } |
|
| 1918 | - |
|
| 1919 | - return ['field' => $fields, 'key' => $keys]; |
|
| 1797 | + $query = |
|
| 1798 | + 'SELECT sql, type FROM' |
|
| 1799 | + . ' (SELECT * FROM sqlite_master UNION ALL' |
|
| 1800 | + . ' SELECT * FROM sqlite_temp_master)' |
|
| 1801 | + . " WHERE tbl_name LIKE '$nom_table'" |
|
| 1802 | + . " AND type!='meta' AND sql NOT NULL AND name NOT LIKE 'sqlite_%'" |
|
| 1803 | + . ' ORDER BY substr(type,2,1), name'; |
|
| 1804 | + |
|
| 1805 | + $a = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1806 | + if (!$a) { |
|
| 1807 | + return ''; |
|
| 1808 | + } |
|
| 1809 | + if (!$requeter) { |
|
| 1810 | + return $a; |
|
| 1811 | + } |
|
| 1812 | + if (!($a = spip_sqlite_fetch($a, null, $serveur))) { |
|
| 1813 | + return ''; |
|
| 1814 | + } |
|
| 1815 | + $vue = ($a['type'] == 'view'); // table | vue |
|
| 1816 | + |
|
| 1817 | + // c'est une table |
|
| 1818 | + // il faut parser le create |
|
| 1819 | + if (!$vue) { |
|
| 1820 | + if (!preg_match(_SQLITE_RE_SHOW_TABLE, array_shift($a), $r)) { |
|
| 1821 | + return ''; |
|
| 1822 | + } else { |
|
| 1823 | + $desc = $r[1]; |
|
| 1824 | + // extraction d'une KEY éventuelle en prenant garde de ne pas |
|
| 1825 | + // relever un champ dont le nom contient KEY (ex. ID_WHISKEY) |
|
| 1826 | + if (preg_match('/^(.*?),([^,]*\sKEY[ (].*)$/s', $desc, $r)) { |
|
| 1827 | + $namedkeys = $r[2]; |
|
| 1828 | + $desc = $r[1]; |
|
| 1829 | + } else { |
|
| 1830 | + $namedkeys = ''; |
|
| 1831 | + } |
|
| 1832 | + |
|
| 1833 | + $fields = []; |
|
| 1834 | + $keys = []; |
|
| 1835 | + |
|
| 1836 | + // enlever les contenus des valeurs DEFAULT 'xxx' qui pourraient perturber |
|
| 1837 | + // par exemple s'il contiennent une virgule. |
|
| 1838 | + // /!\ cela peut aussi echapper le nom des champs si la table a eu des operations avec SQLite Manager ! |
|
| 1839 | + list($desc, $echaps) = query_echappe_textes($desc); |
|
| 1840 | + |
|
| 1841 | + // separer toutes les descriptions de champs, separes par des virgules |
|
| 1842 | + # /!\ explode peut exploser aussi DECIMAL(10,2) ! |
|
| 1843 | + $k_precedent = null; |
|
| 1844 | + foreach (explode(',', $desc) as $v) { |
|
| 1845 | + preg_match('/^\s*([^\s]+)\s+(.*)/', $v, $r); |
|
| 1846 | + // Les cles de champs peuvent etre entourees |
|
| 1847 | + // de guillements doubles " , simples ', graves ` ou de crochets [ ], ou rien. |
|
| 1848 | + // http://www.sqlite.org/lang_keywords.html |
|
| 1849 | + $k = strtolower(query_reinjecte_textes($r[1], $echaps)); // champ, "champ", [champ]... |
|
| 1850 | + if ($char = strpbrk($k[0], '\'"[`')) { |
|
| 1851 | + $k = trim($k, $char); |
|
| 1852 | + if ($char == '[') { |
|
| 1853 | + $k = rtrim($k, ']'); |
|
| 1854 | + } |
|
| 1855 | + } |
|
| 1856 | + $def = query_reinjecte_textes($r[2], $echaps); // valeur du champ |
|
| 1857 | + |
|
| 1858 | + // rustine pour DECIMAL(10,2) |
|
| 1859 | + // s'il y a une parenthèse fermante dans la clé |
|
| 1860 | + // ou dans la définition sans qu'il n'y ait une ouverture avant |
|
| 1861 | + if (str_contains($k, ')') or preg_match('/^[^\(]*\)/', $def)) { |
|
| 1862 | + $fields[$k_precedent] .= ',' . $k . ' ' . $def; |
|
| 1863 | + continue; |
|
| 1864 | + } |
|
| 1865 | + |
|
| 1866 | + // la primary key peut etre dans une des descriptions de champs |
|
| 1867 | + // et non en fin de table, cas encore decouvert avec Sqlite Manager |
|
| 1868 | + if (stripos($r[2], 'PRIMARY KEY') !== false) { |
|
| 1869 | + $keys['PRIMARY KEY'] = $k; |
|
| 1870 | + } |
|
| 1871 | + |
|
| 1872 | + $fields[$k] = $def; |
|
| 1873 | + $k_precedent = $k; |
|
| 1874 | + } |
|
| 1875 | + // key inclues dans la requete |
|
| 1876 | + foreach (preg_split('/\)\s*(,|$)/', $namedkeys) as $v) { |
|
| 1877 | + if (preg_match('/^\s*([^(]*)\(([^(]*(\(\d+\))?)$/', $v, $r)) { |
|
| 1878 | + $k = str_replace('`', '', trim($r[1])); |
|
| 1879 | + $t = trim(strtolower(str_replace('`', '', $r[2])), '"'); |
|
| 1880 | + if ($k && !isset($keys[$k])) { |
|
| 1881 | + $keys[$k] = $t; |
|
| 1882 | + } else { |
|
| 1883 | + $keys[] = $t; |
|
| 1884 | + } |
|
| 1885 | + } |
|
| 1886 | + } |
|
| 1887 | + // sinon ajouter les key index |
|
| 1888 | + $query = |
|
| 1889 | + 'SELECT name,sql FROM' |
|
| 1890 | + . ' (SELECT * FROM sqlite_master UNION ALL' |
|
| 1891 | + . ' SELECT * FROM sqlite_temp_master)' |
|
| 1892 | + . " WHERE tbl_name LIKE '$nom_table'" |
|
| 1893 | + . " AND type='index' AND name NOT LIKE 'sqlite_%'" |
|
| 1894 | + . 'ORDER BY substr(type,2,1), name'; |
|
| 1895 | + $a = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1896 | + while ($r = spip_sqlite_fetch($a, null, $serveur)) { |
|
| 1897 | + $key = str_replace($nom_table . '_', '', $r['name']); // enlever le nom de la table ajoute a l'index |
|
| 1898 | + $keytype = 'KEY'; |
|
| 1899 | + if (strpos($r['sql'], 'UNIQUE INDEX') !== false) { |
|
| 1900 | + $keytype = 'UNIQUE KEY'; |
|
| 1901 | + } |
|
| 1902 | + $colonnes = preg_replace(',.*\((.*)\).*,', '$1', $r['sql']); |
|
| 1903 | + $keys[$keytype . ' ' . $key] = $colonnes; |
|
| 1904 | + } |
|
| 1905 | + } |
|
| 1906 | + } // c'est une vue, on liste les champs disponibles simplement |
|
| 1907 | + else { |
|
| 1908 | + if ($res = sql_fetsel('*', $nom_table, '', '', '', '1', '', $serveur)) { // limit 1 |
|
| 1909 | + $fields = []; |
|
| 1910 | + foreach ($res as $c => $v) { |
|
| 1911 | + $fields[$c] = ''; |
|
| 1912 | + } |
|
| 1913 | + $keys = []; |
|
| 1914 | + } else { |
|
| 1915 | + return ''; |
|
| 1916 | + } |
|
| 1917 | + } |
|
| 1918 | + |
|
| 1919 | + return ['field' => $fields, 'key' => $keys]; |
|
| 1920 | 1920 | } |
| 1921 | 1921 | |
| 1922 | 1922 | |
@@ -1943,24 +1943,24 @@ discard block |
||
| 1943 | 1943 | */ |
| 1944 | 1944 | function spip_sqlite_update($table, $champs, $where = '', $desc = '', $serveur = '', $requeter = true) |
| 1945 | 1945 | { |
| 1946 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1947 | - $champs = _sqlite_ajouter_champs_timestamp($table, $champs, $desc, $serveur); |
|
| 1948 | - |
|
| 1949 | - $set = []; |
|
| 1950 | - foreach ($champs as $champ => $val) { |
|
| 1951 | - $set[] = $champ . "=$val"; |
|
| 1952 | - } |
|
| 1953 | - if (!empty($set)) { |
|
| 1954 | - return spip_sqlite_query( |
|
| 1955 | - _sqlite_calculer_expression('UPDATE', $table, ',') |
|
| 1956 | - . _sqlite_calculer_expression('SET', $set, ',') |
|
| 1957 | - . _sqlite_calculer_expression('WHERE', $where), |
|
| 1958 | - $serveur, |
|
| 1959 | - $requeter |
|
| 1960 | - ); |
|
| 1961 | - } |
|
| 1962 | - |
|
| 1963 | - return false; |
|
| 1946 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1947 | + $champs = _sqlite_ajouter_champs_timestamp($table, $champs, $desc, $serveur); |
|
| 1948 | + |
|
| 1949 | + $set = []; |
|
| 1950 | + foreach ($champs as $champ => $val) { |
|
| 1951 | + $set[] = $champ . "=$val"; |
|
| 1952 | + } |
|
| 1953 | + if (!empty($set)) { |
|
| 1954 | + return spip_sqlite_query( |
|
| 1955 | + _sqlite_calculer_expression('UPDATE', $table, ',') |
|
| 1956 | + . _sqlite_calculer_expression('SET', $set, ',') |
|
| 1957 | + . _sqlite_calculer_expression('WHERE', $where), |
|
| 1958 | + $serveur, |
|
| 1959 | + $requeter |
|
| 1960 | + ); |
|
| 1961 | + } |
|
| 1962 | + |
|
| 1963 | + return false; |
|
| 1964 | 1964 | } |
| 1965 | 1965 | |
| 1966 | 1966 | |
@@ -1991,38 +1991,38 @@ discard block |
||
| 1991 | 1991 | function spip_sqlite_updateq($table, $champs, $where = '', $desc = [], $serveur = '', $requeter = true) |
| 1992 | 1992 | { |
| 1993 | 1993 | |
| 1994 | - if (!$champs) { |
|
| 1995 | - return; |
|
| 1996 | - } |
|
| 1997 | - if (!$desc) { |
|
| 1998 | - $desc = description_table($table, $serveur); |
|
| 1999 | - } |
|
| 2000 | - if (!$desc) { |
|
| 2001 | - die("$table insertion sans description"); |
|
| 2002 | - } |
|
| 2003 | - $fields = $desc['field']; |
|
| 2004 | - |
|
| 2005 | - $set = []; |
|
| 2006 | - foreach ($champs as $champ => $val) { |
|
| 2007 | - $set[$champ] = $champ . '=' . _sqlite_calculer_cite($val, isset($fields[$champ]) ? $fields[$champ] : ''); |
|
| 2008 | - } |
|
| 2009 | - |
|
| 2010 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 2011 | - // attention ils sont deja quotes |
|
| 2012 | - $maj = _sqlite_ajouter_champs_timestamp($table, [], $desc, $serveur); |
|
| 2013 | - foreach ($maj as $champ => $val) { |
|
| 2014 | - if (!isset($set[$champ])) { |
|
| 2015 | - $set[$champ] = $champ . '=' . $val; |
|
| 2016 | - } |
|
| 2017 | - } |
|
| 2018 | - |
|
| 2019 | - return spip_sqlite_query( |
|
| 2020 | - _sqlite_calculer_expression('UPDATE', $table, ',') |
|
| 2021 | - . _sqlite_calculer_expression('SET', $set, ',') |
|
| 2022 | - . _sqlite_calculer_expression('WHERE', $where), |
|
| 2023 | - $serveur, |
|
| 2024 | - $requeter |
|
| 2025 | - ); |
|
| 1994 | + if (!$champs) { |
|
| 1995 | + return; |
|
| 1996 | + } |
|
| 1997 | + if (!$desc) { |
|
| 1998 | + $desc = description_table($table, $serveur); |
|
| 1999 | + } |
|
| 2000 | + if (!$desc) { |
|
| 2001 | + die("$table insertion sans description"); |
|
| 2002 | + } |
|
| 2003 | + $fields = $desc['field']; |
|
| 2004 | + |
|
| 2005 | + $set = []; |
|
| 2006 | + foreach ($champs as $champ => $val) { |
|
| 2007 | + $set[$champ] = $champ . '=' . _sqlite_calculer_cite($val, isset($fields[$champ]) ? $fields[$champ] : ''); |
|
| 2008 | + } |
|
| 2009 | + |
|
| 2010 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 2011 | + // attention ils sont deja quotes |
|
| 2012 | + $maj = _sqlite_ajouter_champs_timestamp($table, [], $desc, $serveur); |
|
| 2013 | + foreach ($maj as $champ => $val) { |
|
| 2014 | + if (!isset($set[$champ])) { |
|
| 2015 | + $set[$champ] = $champ . '=' . $val; |
|
| 2016 | + } |
|
| 2017 | + } |
|
| 2018 | + |
|
| 2019 | + return spip_sqlite_query( |
|
| 2020 | + _sqlite_calculer_expression('UPDATE', $table, ',') |
|
| 2021 | + . _sqlite_calculer_expression('SET', $set, ',') |
|
| 2022 | + . _sqlite_calculer_expression('WHERE', $where), |
|
| 2023 | + $serveur, |
|
| 2024 | + $requeter |
|
| 2025 | + ); |
|
| 2026 | 2026 | } |
| 2027 | 2027 | |
| 2028 | 2028 | |
@@ -2041,17 +2041,17 @@ discard block |
||
| 2041 | 2041 | */ |
| 2042 | 2042 | function _sqlite_init() |
| 2043 | 2043 | { |
| 2044 | - if (!defined('_DIR_DB')) { |
|
| 2045 | - define('_DIR_DB', _DIR_ETC . 'bases/'); |
|
| 2046 | - } |
|
| 2047 | - if (!defined('_SQLITE_CHMOD')) { |
|
| 2048 | - define('_SQLITE_CHMOD', _SPIP_CHMOD); |
|
| 2049 | - } |
|
| 2050 | - |
|
| 2051 | - if (!is_dir($d = _DIR_DB)) { |
|
| 2052 | - include_spip('inc/flock'); |
|
| 2053 | - sous_repertoire($d); |
|
| 2054 | - } |
|
| 2044 | + if (!defined('_DIR_DB')) { |
|
| 2045 | + define('_DIR_DB', _DIR_ETC . 'bases/'); |
|
| 2046 | + } |
|
| 2047 | + if (!defined('_SQLITE_CHMOD')) { |
|
| 2048 | + define('_SQLITE_CHMOD', _SPIP_CHMOD); |
|
| 2049 | + } |
|
| 2050 | + |
|
| 2051 | + if (!is_dir($d = _DIR_DB)) { |
|
| 2052 | + include_spip('inc/flock'); |
|
| 2053 | + sous_repertoire($d); |
|
| 2054 | + } |
|
| 2055 | 2055 | } |
| 2056 | 2056 | |
| 2057 | 2057 | |
@@ -2066,20 +2066,20 @@ discard block |
||
| 2066 | 2066 | */ |
| 2067 | 2067 | function _sqlite_is_version($version = '', $link = '', $serveur = '', $requeter = true) |
| 2068 | 2068 | { |
| 2069 | - if ($link === '') { |
|
| 2070 | - $link = _sqlite_link($serveur); |
|
| 2071 | - } |
|
| 2072 | - if (!$link) { |
|
| 2073 | - return false; |
|
| 2074 | - } |
|
| 2069 | + if ($link === '') { |
|
| 2070 | + $link = _sqlite_link($serveur); |
|
| 2071 | + } |
|
| 2072 | + if (!$link) { |
|
| 2073 | + return false; |
|
| 2074 | + } |
|
| 2075 | 2075 | |
| 2076 | - $v = 3; |
|
| 2076 | + $v = 3; |
|
| 2077 | 2077 | |
| 2078 | - if (!$version) { |
|
| 2079 | - return $v; |
|
| 2080 | - } |
|
| 2078 | + if (!$version) { |
|
| 2079 | + return $v; |
|
| 2080 | + } |
|
| 2081 | 2081 | |
| 2082 | - return ($version == $v); |
|
| 2082 | + return ($version == $v); |
|
| 2083 | 2083 | } |
| 2084 | 2084 | |
| 2085 | 2085 | |
@@ -2091,9 +2091,9 @@ discard block |
||
| 2091 | 2091 | */ |
| 2092 | 2092 | function _sqlite_link($serveur = ''): ?\PDO |
| 2093 | 2093 | { |
| 2094 | - $link = &$GLOBALS['connexions'][$serveur ? $serveur : 0]['link']; |
|
| 2094 | + $link = &$GLOBALS['connexions'][$serveur ? $serveur : 0]['link']; |
|
| 2095 | 2095 | |
| 2096 | - return $link; |
|
| 2096 | + return $link; |
|
| 2097 | 2097 | } |
| 2098 | 2098 | |
| 2099 | 2099 | |
@@ -2109,54 +2109,54 @@ discard block |
||
| 2109 | 2109 | */ |
| 2110 | 2110 | function _sqlite_calculer_cite($v, $type) |
| 2111 | 2111 | { |
| 2112 | - if ($type) { |
|
| 2113 | - if ( |
|
| 2114 | - is_null($v) |
|
| 2115 | - and stripos($type, 'NOT NULL') === false |
|
| 2116 | - ) { |
|
| 2117 | - // null php se traduit en NULL SQL |
|
| 2118 | - return 'NULL'; |
|
| 2119 | - } |
|
| 2120 | - |
|
| 2121 | - if (sql_test_date($type) and preg_match('/^\w+\(/', $v)) { |
|
| 2122 | - return $v; |
|
| 2123 | - } |
|
| 2124 | - if (sql_test_int($type)) { |
|
| 2125 | - if (is_numeric($v)) { |
|
| 2126 | - return $v; |
|
| 2127 | - } elseif ($v === null) { |
|
| 2128 | - return 0; |
|
| 2129 | - } elseif (ctype_xdigit(substr($v, 2)) and strncmp($v, '0x', 2) === 0) { |
|
| 2130 | - return hexdec(substr($v, 2)); |
|
| 2131 | - } else { |
|
| 2132 | - return intval($v); |
|
| 2133 | - } |
|
| 2134 | - } |
|
| 2135 | - } else { |
|
| 2136 | - // si on ne connait pas le type on le deduit de $v autant que possible |
|
| 2137 | - if (is_bool($v)) { |
|
| 2138 | - return strval(intval($v)); |
|
| 2139 | - } elseif (is_numeric($v)) { |
|
| 2140 | - return strval($v); |
|
| 2141 | - } |
|
| 2142 | - } |
|
| 2143 | - |
|
| 2144 | - // trouver un link sqlite pour faire l'echappement |
|
| 2145 | - foreach ($GLOBALS['connexions'] as $s) { |
|
| 2146 | - if ( |
|
| 2147 | - $l = $s['link'] |
|
| 2148 | - and is_object($l) |
|
| 2149 | - and $l instanceof \PDO |
|
| 2150 | - and $l->getAttribute(\PDO::ATTR_DRIVER_NAME) === 'sqlite' |
|
| 2151 | - ) { |
|
| 2152 | - return $l->quote($v ?? ''); |
|
| 2153 | - } |
|
| 2154 | - } |
|
| 2155 | - |
|
| 2156 | - // echapper les ' en '' |
|
| 2157 | - spip_log('Pas de methode ->quote pour echapper', 'sqlite.' . _LOG_INFO_IMPORTANTE); |
|
| 2158 | - |
|
| 2159 | - return ("'" . str_replace("'", "''", $v) . "'"); |
|
| 2112 | + if ($type) { |
|
| 2113 | + if ( |
|
| 2114 | + is_null($v) |
|
| 2115 | + and stripos($type, 'NOT NULL') === false |
|
| 2116 | + ) { |
|
| 2117 | + // null php se traduit en NULL SQL |
|
| 2118 | + return 'NULL'; |
|
| 2119 | + } |
|
| 2120 | + |
|
| 2121 | + if (sql_test_date($type) and preg_match('/^\w+\(/', $v)) { |
|
| 2122 | + return $v; |
|
| 2123 | + } |
|
| 2124 | + if (sql_test_int($type)) { |
|
| 2125 | + if (is_numeric($v)) { |
|
| 2126 | + return $v; |
|
| 2127 | + } elseif ($v === null) { |
|
| 2128 | + return 0; |
|
| 2129 | + } elseif (ctype_xdigit(substr($v, 2)) and strncmp($v, '0x', 2) === 0) { |
|
| 2130 | + return hexdec(substr($v, 2)); |
|
| 2131 | + } else { |
|
| 2132 | + return intval($v); |
|
| 2133 | + } |
|
| 2134 | + } |
|
| 2135 | + } else { |
|
| 2136 | + // si on ne connait pas le type on le deduit de $v autant que possible |
|
| 2137 | + if (is_bool($v)) { |
|
| 2138 | + return strval(intval($v)); |
|
| 2139 | + } elseif (is_numeric($v)) { |
|
| 2140 | + return strval($v); |
|
| 2141 | + } |
|
| 2142 | + } |
|
| 2143 | + |
|
| 2144 | + // trouver un link sqlite pour faire l'echappement |
|
| 2145 | + foreach ($GLOBALS['connexions'] as $s) { |
|
| 2146 | + if ( |
|
| 2147 | + $l = $s['link'] |
|
| 2148 | + and is_object($l) |
|
| 2149 | + and $l instanceof \PDO |
|
| 2150 | + and $l->getAttribute(\PDO::ATTR_DRIVER_NAME) === 'sqlite' |
|
| 2151 | + ) { |
|
| 2152 | + return $l->quote($v ?? ''); |
|
| 2153 | + } |
|
| 2154 | + } |
|
| 2155 | + |
|
| 2156 | + // echapper les ' en '' |
|
| 2157 | + spip_log('Pas de methode ->quote pour echapper', 'sqlite.' . _LOG_INFO_IMPORTANTE); |
|
| 2158 | + |
|
| 2159 | + return ("'" . str_replace("'", "''", $v) . "'"); |
|
| 2160 | 2160 | } |
| 2161 | 2161 | |
| 2162 | 2162 | |
@@ -2173,21 +2173,21 @@ discard block |
||
| 2173 | 2173 | */ |
| 2174 | 2174 | function _sqlite_calculer_expression($expression, $v, $join = 'AND') |
| 2175 | 2175 | { |
| 2176 | - if (empty($v)) { |
|
| 2177 | - return ''; |
|
| 2178 | - } |
|
| 2179 | - |
|
| 2180 | - $exp = "\n$expression "; |
|
| 2181 | - |
|
| 2182 | - if (!is_array($v)) { |
|
| 2183 | - return $exp . $v; |
|
| 2184 | - } else { |
|
| 2185 | - if (strtoupper($join) === 'AND') { |
|
| 2186 | - return $exp . join("\n\t$join ", array_map('_sqlite_calculer_where', $v)); |
|
| 2187 | - } else { |
|
| 2188 | - return $exp . join($join, $v); |
|
| 2189 | - } |
|
| 2190 | - } |
|
| 2176 | + if (empty($v)) { |
|
| 2177 | + return ''; |
|
| 2178 | + } |
|
| 2179 | + |
|
| 2180 | + $exp = "\n$expression "; |
|
| 2181 | + |
|
| 2182 | + if (!is_array($v)) { |
|
| 2183 | + return $exp . $v; |
|
| 2184 | + } else { |
|
| 2185 | + if (strtoupper($join) === 'AND') { |
|
| 2186 | + return $exp . join("\n\t$join ", array_map('_sqlite_calculer_where', $v)); |
|
| 2187 | + } else { |
|
| 2188 | + return $exp . join($join, $v); |
|
| 2189 | + } |
|
| 2190 | + } |
|
| 2191 | 2191 | } |
| 2192 | 2192 | |
| 2193 | 2193 | |
@@ -2204,7 +2204,7 @@ discard block |
||
| 2204 | 2204 | */ |
| 2205 | 2205 | function _sqlite_calculer_order($orderby) |
| 2206 | 2206 | { |
| 2207 | - return (is_array($orderby)) ? join(', ', $orderby) : $orderby; |
|
| 2207 | + return (is_array($orderby)) ? join(', ', $orderby) : $orderby; |
|
| 2208 | 2208 | } |
| 2209 | 2209 | |
| 2210 | 2210 | |
@@ -2216,26 +2216,26 @@ discard block |
||
| 2216 | 2216 | */ |
| 2217 | 2217 | function _sqlite_calculer_select_as($args) |
| 2218 | 2218 | { |
| 2219 | - $res = ''; |
|
| 2220 | - foreach ($args as $k => $v) { |
|
| 2221 | - if (substr($k, -1) == '@') { |
|
| 2222 | - // c'est une jointure qui se refere au from precedent |
|
| 2223 | - // pas de virgule |
|
| 2224 | - $res .= ' ' . $v; |
|
| 2225 | - } else { |
|
| 2226 | - if (!is_numeric($k)) { |
|
| 2227 | - $p = strpos($v, ' '); |
|
| 2228 | - if ($p) { |
|
| 2229 | - $v = substr($v, 0, $p) . " AS '$k'" . substr($v, $p); |
|
| 2230 | - } else { |
|
| 2231 | - $v .= " AS '$k'"; |
|
| 2232 | - } |
|
| 2233 | - } |
|
| 2234 | - $res .= ', ' . $v; |
|
| 2235 | - } |
|
| 2236 | - } |
|
| 2237 | - |
|
| 2238 | - return substr($res, 2); |
|
| 2219 | + $res = ''; |
|
| 2220 | + foreach ($args as $k => $v) { |
|
| 2221 | + if (substr($k, -1) == '@') { |
|
| 2222 | + // c'est une jointure qui se refere au from precedent |
|
| 2223 | + // pas de virgule |
|
| 2224 | + $res .= ' ' . $v; |
|
| 2225 | + } else { |
|
| 2226 | + if (!is_numeric($k)) { |
|
| 2227 | + $p = strpos($v, ' '); |
|
| 2228 | + if ($p) { |
|
| 2229 | + $v = substr($v, 0, $p) . " AS '$k'" . substr($v, $p); |
|
| 2230 | + } else { |
|
| 2231 | + $v .= " AS '$k'"; |
|
| 2232 | + } |
|
| 2233 | + } |
|
| 2234 | + $res .= ', ' . $v; |
|
| 2235 | + } |
|
| 2236 | + } |
|
| 2237 | + |
|
| 2238 | + return substr($res, 2); |
|
| 2239 | 2239 | } |
| 2240 | 2240 | |
| 2241 | 2241 | |
@@ -2259,26 +2259,26 @@ discard block |
||
| 2259 | 2259 | */ |
| 2260 | 2260 | function _sqlite_calculer_where($v) |
| 2261 | 2261 | { |
| 2262 | - if (!is_array($v)) { |
|
| 2263 | - return $v; |
|
| 2264 | - } |
|
| 2265 | - |
|
| 2266 | - $op = array_shift($v); |
|
| 2267 | - if (!($n = count($v))) { |
|
| 2268 | - return $op; |
|
| 2269 | - } else { |
|
| 2270 | - $arg = _sqlite_calculer_where(array_shift($v)); |
|
| 2271 | - if ($n == 1) { |
|
| 2272 | - return "$op($arg)"; |
|
| 2273 | - } else { |
|
| 2274 | - $arg2 = _sqlite_calculer_where(array_shift($v)); |
|
| 2275 | - if ($n == 2) { |
|
| 2276 | - return "($arg $op $arg2)"; |
|
| 2277 | - } else { |
|
| 2278 | - return "($arg $op ($arg2) : $v[0])"; |
|
| 2279 | - } |
|
| 2280 | - } |
|
| 2281 | - } |
|
| 2262 | + if (!is_array($v)) { |
|
| 2263 | + return $v; |
|
| 2264 | + } |
|
| 2265 | + |
|
| 2266 | + $op = array_shift($v); |
|
| 2267 | + if (!($n = count($v))) { |
|
| 2268 | + return $op; |
|
| 2269 | + } else { |
|
| 2270 | + $arg = _sqlite_calculer_where(array_shift($v)); |
|
| 2271 | + if ($n == 1) { |
|
| 2272 | + return "$op($arg)"; |
|
| 2273 | + } else { |
|
| 2274 | + $arg2 = _sqlite_calculer_where(array_shift($v)); |
|
| 2275 | + if ($n == 2) { |
|
| 2276 | + return "($arg $op $arg2)"; |
|
| 2277 | + } else { |
|
| 2278 | + return "($arg $op ($arg2) : $v[0])"; |
|
| 2279 | + } |
|
| 2280 | + } |
|
| 2281 | + } |
|
| 2282 | 2282 | } |
| 2283 | 2283 | |
| 2284 | 2284 | |
@@ -2294,19 +2294,19 @@ discard block |
||
| 2294 | 2294 | */ |
| 2295 | 2295 | function _sqlite_charger_version($version = '') |
| 2296 | 2296 | { |
| 2297 | - $versions = []; |
|
| 2298 | - |
|
| 2299 | - // version 3 |
|
| 2300 | - if (!$version || $version == 3) { |
|
| 2301 | - if (extension_loaded('pdo') && extension_loaded('pdo_sqlite')) { |
|
| 2302 | - $versions[] = 3; |
|
| 2303 | - } |
|
| 2304 | - } |
|
| 2305 | - if ($version) { |
|
| 2306 | - return in_array($version, $versions); |
|
| 2307 | - } |
|
| 2308 | - |
|
| 2309 | - return $versions; |
|
| 2297 | + $versions = []; |
|
| 2298 | + |
|
| 2299 | + // version 3 |
|
| 2300 | + if (!$version || $version == 3) { |
|
| 2301 | + if (extension_loaded('pdo') && extension_loaded('pdo_sqlite')) { |
|
| 2302 | + $versions[] = 3; |
|
| 2303 | + } |
|
| 2304 | + } |
|
| 2305 | + if ($version) { |
|
| 2306 | + return in_array($version, $versions); |
|
| 2307 | + } |
|
| 2308 | + |
|
| 2309 | + return $versions; |
|
| 2310 | 2310 | } |
| 2311 | 2311 | |
| 2312 | 2312 | |
@@ -2345,147 +2345,147 @@ discard block |
||
| 2345 | 2345 | function _sqlite_modifier_table($table, $colonne, $opt = [], $serveur = '') |
| 2346 | 2346 | { |
| 2347 | 2347 | |
| 2348 | - if (is_array($table)) { |
|
| 2349 | - $table_destination = reset($table); |
|
| 2350 | - $table_origine = key($table); |
|
| 2351 | - } else { |
|
| 2352 | - $table_origine = $table_destination = $table; |
|
| 2353 | - } |
|
| 2354 | - // ne prend actuellement qu'un changement |
|
| 2355 | - // mais pourra etre adapte pour changer plus qu'une colonne a la fois |
|
| 2356 | - if (is_array($colonne)) { |
|
| 2357 | - $colonne_destination = reset($colonne); |
|
| 2358 | - $colonne_origine = key($colonne); |
|
| 2359 | - } else { |
|
| 2360 | - $colonne_origine = $colonne_destination = $colonne; |
|
| 2361 | - } |
|
| 2362 | - if (!isset($opt['field'])) { |
|
| 2363 | - $opt['field'] = []; |
|
| 2364 | - } |
|
| 2365 | - if (!isset($opt['key'])) { |
|
| 2366 | - $opt['key'] = []; |
|
| 2367 | - } |
|
| 2368 | - |
|
| 2369 | - // si les noms de tables sont differents, pas besoin de table temporaire |
|
| 2370 | - // on prendra directement le nom de la future table |
|
| 2371 | - $meme_table = ($table_origine == $table_destination); |
|
| 2372 | - |
|
| 2373 | - $def_origine = sql_showtable($table_origine, false, $serveur); |
|
| 2374 | - if (!$def_origine or !isset($def_origine['field'])) { |
|
| 2375 | - spip_log("Alter table impossible sur $table_origine : table non trouvee", 'sqlite' . _LOG_ERREUR); |
|
| 2376 | - |
|
| 2377 | - return false; |
|
| 2378 | - } |
|
| 2379 | - |
|
| 2380 | - |
|
| 2381 | - $table_tmp = $table_origine . '_tmp'; |
|
| 2382 | - |
|
| 2383 | - // 1) creer une table temporaire avec les modifications |
|
| 2384 | - // - DROP : suppression de la colonne |
|
| 2385 | - // - CHANGE : modification de la colonne |
|
| 2386 | - // (foreach pour conserver l'ordre des champs) |
|
| 2387 | - |
|
| 2388 | - // field |
|
| 2389 | - $fields = []; |
|
| 2390 | - // pour le INSERT INTO plus loin |
|
| 2391 | - // stocker la correspondance nouvelles->anciennes colonnes |
|
| 2392 | - $fields_correspondances = []; |
|
| 2393 | - foreach ($def_origine['field'] as $c => $d) { |
|
| 2394 | - if ($colonne_origine && ($c == $colonne_origine)) { |
|
| 2395 | - // si pas DROP |
|
| 2396 | - if ($colonne_destination) { |
|
| 2397 | - $fields[$colonne_destination] = $opt['field'][$colonne_destination]; |
|
| 2398 | - $fields_correspondances[$colonne_destination] = $c; |
|
| 2399 | - } |
|
| 2400 | - } else { |
|
| 2401 | - $fields[$c] = $d; |
|
| 2402 | - $fields_correspondances[$c] = $c; |
|
| 2403 | - } |
|
| 2404 | - } |
|
| 2405 | - // cas de ADD sqlite2 (ajout du champ en fin de table): |
|
| 2406 | - if (!$colonne_origine && $colonne_destination) { |
|
| 2407 | - $fields[$colonne_destination] = $opt['field'][$colonne_destination]; |
|
| 2408 | - } |
|
| 2409 | - |
|
| 2410 | - // key... |
|
| 2411 | - $keys = []; |
|
| 2412 | - foreach ($def_origine['key'] as $c => $d) { |
|
| 2413 | - $c = str_replace($colonne_origine, $colonne_destination, $c); |
|
| 2414 | - $d = str_replace($colonne_origine, $colonne_destination, $d); |
|
| 2415 | - // seulement si on ne supprime pas la colonne ! |
|
| 2416 | - if ($d) { |
|
| 2417 | - $keys[$c] = $d; |
|
| 2418 | - } |
|
| 2419 | - } |
|
| 2420 | - |
|
| 2421 | - // autres keys, on merge |
|
| 2422 | - $keys = array_merge($keys, $opt['key']); |
|
| 2423 | - $queries = []; |
|
| 2424 | - |
|
| 2425 | - // copier dans destination (si differente de origine), sinon tmp |
|
| 2426 | - $table_copie = ($meme_table) ? $table_tmp : $table_destination; |
|
| 2427 | - $autoinc = (isset($keys['PRIMARY KEY']) |
|
| 2428 | - and $keys['PRIMARY KEY'] |
|
| 2429 | - and stripos($keys['PRIMARY KEY'], ',') === false |
|
| 2430 | - and stripos($fields[$keys['PRIMARY KEY']], 'default') === false); |
|
| 2431 | - |
|
| 2432 | - if ( |
|
| 2433 | - $q = _sqlite_requete_create( |
|
| 2434 | - $table_copie, |
|
| 2435 | - $fields, |
|
| 2436 | - $keys, |
|
| 2437 | - $autoinc, |
|
| 2438 | - $temporary = false, |
|
| 2439 | - $ifnotexists = true, |
|
| 2440 | - $serveur |
|
| 2441 | - ) |
|
| 2442 | - ) { |
|
| 2443 | - $queries[] = $q; |
|
| 2444 | - } |
|
| 2445 | - |
|
| 2446 | - |
|
| 2447 | - // 2) y copier les champs qui vont bien |
|
| 2448 | - $champs_dest = join(', ', array_keys($fields_correspondances)); |
|
| 2449 | - $champs_ori = join(', ', $fields_correspondances); |
|
| 2450 | - $queries[] = "INSERT INTO $table_copie ($champs_dest) SELECT $champs_ori FROM $table_origine"; |
|
| 2451 | - |
|
| 2452 | - // 3) supprimer la table d'origine |
|
| 2453 | - $queries[] = "DROP TABLE $table_origine"; |
|
| 2454 | - |
|
| 2455 | - // 4) renommer la table temporaire |
|
| 2456 | - // avec le nom de la table destination |
|
| 2457 | - // si necessaire |
|
| 2458 | - if ($meme_table) { |
|
| 2459 | - $queries[] = "ALTER TABLE $table_copie RENAME TO $table_destination"; |
|
| 2460 | - } |
|
| 2461 | - |
|
| 2462 | - // 5) remettre les index ! |
|
| 2463 | - foreach ($keys as $k => $v) { |
|
| 2464 | - if ($k == 'PRIMARY KEY') { |
|
| 2465 | - } else { |
|
| 2466 | - // enlever KEY |
|
| 2467 | - $k = substr($k, 4); |
|
| 2468 | - $queries[] = "CREATE INDEX $table_destination" . "_$k ON $table_destination ($v)"; |
|
| 2469 | - } |
|
| 2470 | - } |
|
| 2471 | - |
|
| 2472 | - |
|
| 2473 | - if (count($queries)) { |
|
| 2474 | - Sqlite::demarrer_transaction($serveur); |
|
| 2475 | - // il faut les faire une par une car $query = join('; ', $queries).";"; ne fonctionne pas |
|
| 2476 | - foreach ($queries as $q) { |
|
| 2477 | - if (!Sqlite::executer_requete($q, $serveur)) { |
|
| 2478 | - spip_log('SQLite : ALTER TABLE table :' |
|
| 2479 | - . " Erreur a l'execution de la requete : $q", 'sqlite.' . _LOG_ERREUR); |
|
| 2480 | - Sqlite::annuler_transaction($serveur); |
|
| 2481 | - |
|
| 2482 | - return false; |
|
| 2483 | - } |
|
| 2484 | - } |
|
| 2485 | - Sqlite::finir_transaction($serveur); |
|
| 2486 | - } |
|
| 2487 | - |
|
| 2488 | - return true; |
|
| 2348 | + if (is_array($table)) { |
|
| 2349 | + $table_destination = reset($table); |
|
| 2350 | + $table_origine = key($table); |
|
| 2351 | + } else { |
|
| 2352 | + $table_origine = $table_destination = $table; |
|
| 2353 | + } |
|
| 2354 | + // ne prend actuellement qu'un changement |
|
| 2355 | + // mais pourra etre adapte pour changer plus qu'une colonne a la fois |
|
| 2356 | + if (is_array($colonne)) { |
|
| 2357 | + $colonne_destination = reset($colonne); |
|
| 2358 | + $colonne_origine = key($colonne); |
|
| 2359 | + } else { |
|
| 2360 | + $colonne_origine = $colonne_destination = $colonne; |
|
| 2361 | + } |
|
| 2362 | + if (!isset($opt['field'])) { |
|
| 2363 | + $opt['field'] = []; |
|
| 2364 | + } |
|
| 2365 | + if (!isset($opt['key'])) { |
|
| 2366 | + $opt['key'] = []; |
|
| 2367 | + } |
|
| 2368 | + |
|
| 2369 | + // si les noms de tables sont differents, pas besoin de table temporaire |
|
| 2370 | + // on prendra directement le nom de la future table |
|
| 2371 | + $meme_table = ($table_origine == $table_destination); |
|
| 2372 | + |
|
| 2373 | + $def_origine = sql_showtable($table_origine, false, $serveur); |
|
| 2374 | + if (!$def_origine or !isset($def_origine['field'])) { |
|
| 2375 | + spip_log("Alter table impossible sur $table_origine : table non trouvee", 'sqlite' . _LOG_ERREUR); |
|
| 2376 | + |
|
| 2377 | + return false; |
|
| 2378 | + } |
|
| 2379 | + |
|
| 2380 | + |
|
| 2381 | + $table_tmp = $table_origine . '_tmp'; |
|
| 2382 | + |
|
| 2383 | + // 1) creer une table temporaire avec les modifications |
|
| 2384 | + // - DROP : suppression de la colonne |
|
| 2385 | + // - CHANGE : modification de la colonne |
|
| 2386 | + // (foreach pour conserver l'ordre des champs) |
|
| 2387 | + |
|
| 2388 | + // field |
|
| 2389 | + $fields = []; |
|
| 2390 | + // pour le INSERT INTO plus loin |
|
| 2391 | + // stocker la correspondance nouvelles->anciennes colonnes |
|
| 2392 | + $fields_correspondances = []; |
|
| 2393 | + foreach ($def_origine['field'] as $c => $d) { |
|
| 2394 | + if ($colonne_origine && ($c == $colonne_origine)) { |
|
| 2395 | + // si pas DROP |
|
| 2396 | + if ($colonne_destination) { |
|
| 2397 | + $fields[$colonne_destination] = $opt['field'][$colonne_destination]; |
|
| 2398 | + $fields_correspondances[$colonne_destination] = $c; |
|
| 2399 | + } |
|
| 2400 | + } else { |
|
| 2401 | + $fields[$c] = $d; |
|
| 2402 | + $fields_correspondances[$c] = $c; |
|
| 2403 | + } |
|
| 2404 | + } |
|
| 2405 | + // cas de ADD sqlite2 (ajout du champ en fin de table): |
|
| 2406 | + if (!$colonne_origine && $colonne_destination) { |
|
| 2407 | + $fields[$colonne_destination] = $opt['field'][$colonne_destination]; |
|
| 2408 | + } |
|
| 2409 | + |
|
| 2410 | + // key... |
|
| 2411 | + $keys = []; |
|
| 2412 | + foreach ($def_origine['key'] as $c => $d) { |
|
| 2413 | + $c = str_replace($colonne_origine, $colonne_destination, $c); |
|
| 2414 | + $d = str_replace($colonne_origine, $colonne_destination, $d); |
|
| 2415 | + // seulement si on ne supprime pas la colonne ! |
|
| 2416 | + if ($d) { |
|
| 2417 | + $keys[$c] = $d; |
|
| 2418 | + } |
|
| 2419 | + } |
|
| 2420 | + |
|
| 2421 | + // autres keys, on merge |
|
| 2422 | + $keys = array_merge($keys, $opt['key']); |
|
| 2423 | + $queries = []; |
|
| 2424 | + |
|
| 2425 | + // copier dans destination (si differente de origine), sinon tmp |
|
| 2426 | + $table_copie = ($meme_table) ? $table_tmp : $table_destination; |
|
| 2427 | + $autoinc = (isset($keys['PRIMARY KEY']) |
|
| 2428 | + and $keys['PRIMARY KEY'] |
|
| 2429 | + and stripos($keys['PRIMARY KEY'], ',') === false |
|
| 2430 | + and stripos($fields[$keys['PRIMARY KEY']], 'default') === false); |
|
| 2431 | + |
|
| 2432 | + if ( |
|
| 2433 | + $q = _sqlite_requete_create( |
|
| 2434 | + $table_copie, |
|
| 2435 | + $fields, |
|
| 2436 | + $keys, |
|
| 2437 | + $autoinc, |
|
| 2438 | + $temporary = false, |
|
| 2439 | + $ifnotexists = true, |
|
| 2440 | + $serveur |
|
| 2441 | + ) |
|
| 2442 | + ) { |
|
| 2443 | + $queries[] = $q; |
|
| 2444 | + } |
|
| 2445 | + |
|
| 2446 | + |
|
| 2447 | + // 2) y copier les champs qui vont bien |
|
| 2448 | + $champs_dest = join(', ', array_keys($fields_correspondances)); |
|
| 2449 | + $champs_ori = join(', ', $fields_correspondances); |
|
| 2450 | + $queries[] = "INSERT INTO $table_copie ($champs_dest) SELECT $champs_ori FROM $table_origine"; |
|
| 2451 | + |
|
| 2452 | + // 3) supprimer la table d'origine |
|
| 2453 | + $queries[] = "DROP TABLE $table_origine"; |
|
| 2454 | + |
|
| 2455 | + // 4) renommer la table temporaire |
|
| 2456 | + // avec le nom de la table destination |
|
| 2457 | + // si necessaire |
|
| 2458 | + if ($meme_table) { |
|
| 2459 | + $queries[] = "ALTER TABLE $table_copie RENAME TO $table_destination"; |
|
| 2460 | + } |
|
| 2461 | + |
|
| 2462 | + // 5) remettre les index ! |
|
| 2463 | + foreach ($keys as $k => $v) { |
|
| 2464 | + if ($k == 'PRIMARY KEY') { |
|
| 2465 | + } else { |
|
| 2466 | + // enlever KEY |
|
| 2467 | + $k = substr($k, 4); |
|
| 2468 | + $queries[] = "CREATE INDEX $table_destination" . "_$k ON $table_destination ($v)"; |
|
| 2469 | + } |
|
| 2470 | + } |
|
| 2471 | + |
|
| 2472 | + |
|
| 2473 | + if (count($queries)) { |
|
| 2474 | + Sqlite::demarrer_transaction($serveur); |
|
| 2475 | + // il faut les faire une par une car $query = join('; ', $queries).";"; ne fonctionne pas |
|
| 2476 | + foreach ($queries as $q) { |
|
| 2477 | + if (!Sqlite::executer_requete($q, $serveur)) { |
|
| 2478 | + spip_log('SQLite : ALTER TABLE table :' |
|
| 2479 | + . " Erreur a l'execution de la requete : $q", 'sqlite.' . _LOG_ERREUR); |
|
| 2480 | + Sqlite::annuler_transaction($serveur); |
|
| 2481 | + |
|
| 2482 | + return false; |
|
| 2483 | + } |
|
| 2484 | + } |
|
| 2485 | + Sqlite::finir_transaction($serveur); |
|
| 2486 | + } |
|
| 2487 | + |
|
| 2488 | + return true; |
|
| 2489 | 2489 | } |
| 2490 | 2490 | |
| 2491 | 2491 | |
@@ -2496,61 +2496,61 @@ discard block |
||
| 2496 | 2496 | */ |
| 2497 | 2497 | function _sqlite_ref_fonctions() |
| 2498 | 2498 | { |
| 2499 | - $fonctions = [ |
|
| 2500 | - 'alter' => 'spip_sqlite_alter', |
|
| 2501 | - 'count' => 'spip_sqlite_count', |
|
| 2502 | - 'countsel' => 'spip_sqlite_countsel', |
|
| 2503 | - 'create' => 'spip_sqlite_create', |
|
| 2504 | - 'create_base' => 'spip_sqlite_create_base', |
|
| 2505 | - 'create_view' => 'spip_sqlite_create_view', |
|
| 2506 | - 'date_proche' => 'spip_sqlite_date_proche', |
|
| 2507 | - 'delete' => 'spip_sqlite_delete', |
|
| 2508 | - 'drop_table' => 'spip_sqlite_drop_table', |
|
| 2509 | - 'drop_view' => 'spip_sqlite_drop_view', |
|
| 2510 | - 'errno' => 'spip_sqlite_errno', |
|
| 2511 | - 'error' => 'spip_sqlite_error', |
|
| 2512 | - 'explain' => 'spip_sqlite_explain', |
|
| 2513 | - 'fetch' => 'spip_sqlite_fetch', |
|
| 2514 | - 'seek' => 'spip_sqlite_seek', |
|
| 2515 | - 'free' => 'spip_sqlite_free', |
|
| 2516 | - 'hex' => 'spip_sqlite_hex', |
|
| 2517 | - 'in' => 'spip_sqlite_in', |
|
| 2518 | - 'insert' => 'spip_sqlite_insert', |
|
| 2519 | - 'insertq' => 'spip_sqlite_insertq', |
|
| 2520 | - 'insertq_multi' => 'spip_sqlite_insertq_multi', |
|
| 2521 | - 'listdbs' => 'spip_sqlite_listdbs', |
|
| 2522 | - 'multi' => 'spip_sqlite_multi', |
|
| 2523 | - 'optimize' => 'spip_sqlite_optimize', |
|
| 2524 | - 'query' => 'spip_sqlite_query', |
|
| 2525 | - 'quote' => 'spip_sqlite_quote', |
|
| 2526 | - 'repair' => 'spip_sqlite_repair', |
|
| 2527 | - 'replace' => 'spip_sqlite_replace', |
|
| 2528 | - 'replace_multi' => 'spip_sqlite_replace_multi', |
|
| 2529 | - 'select' => 'spip_sqlite_select', |
|
| 2530 | - 'selectdb' => 'spip_sqlite_selectdb', |
|
| 2531 | - 'set_charset' => 'spip_sqlite_set_charset', |
|
| 2532 | - 'get_charset' => 'spip_sqlite_get_charset', |
|
| 2533 | - 'showbase' => 'spip_sqlite_showbase', |
|
| 2534 | - 'showtable' => 'spip_sqlite_showtable', |
|
| 2535 | - 'table_exists' => 'spip_sqlite_table_exists', |
|
| 2536 | - 'update' => 'spip_sqlite_update', |
|
| 2537 | - 'updateq' => 'spip_sqlite_updateq', |
|
| 2538 | - 'preferer_transaction' => 'spip_sqlite_preferer_transaction', |
|
| 2539 | - 'demarrer_transaction' => 'spip_sqlite_demarrer_transaction', |
|
| 2540 | - 'terminer_transaction' => 'spip_sqlite_terminer_transaction', |
|
| 2541 | - ]; |
|
| 2542 | - |
|
| 2543 | - // association de chaque nom http d'un charset aux couples sqlite |
|
| 2544 | - // SQLite supporte utf-8 et utf-16 uniquement. |
|
| 2545 | - $charsets = [ |
|
| 2546 | - 'utf-8' => ['charset' => 'utf8', 'collation' => 'utf8_general_ci'], |
|
| 2547 | - //'utf-16be'=>array('charset'=>'utf16be','collation'=>'UTF-16BE'),// aucune idee de quoi il faut remplir dans es champs la |
|
| 2548 | - //'utf-16le'=>array('charset'=>'utf16le','collation'=>'UTF-16LE') |
|
| 2549 | - ]; |
|
| 2550 | - |
|
| 2551 | - $fonctions['charsets'] = $charsets; |
|
| 2552 | - |
|
| 2553 | - return $fonctions; |
|
| 2499 | + $fonctions = [ |
|
| 2500 | + 'alter' => 'spip_sqlite_alter', |
|
| 2501 | + 'count' => 'spip_sqlite_count', |
|
| 2502 | + 'countsel' => 'spip_sqlite_countsel', |
|
| 2503 | + 'create' => 'spip_sqlite_create', |
|
| 2504 | + 'create_base' => 'spip_sqlite_create_base', |
|
| 2505 | + 'create_view' => 'spip_sqlite_create_view', |
|
| 2506 | + 'date_proche' => 'spip_sqlite_date_proche', |
|
| 2507 | + 'delete' => 'spip_sqlite_delete', |
|
| 2508 | + 'drop_table' => 'spip_sqlite_drop_table', |
|
| 2509 | + 'drop_view' => 'spip_sqlite_drop_view', |
|
| 2510 | + 'errno' => 'spip_sqlite_errno', |
|
| 2511 | + 'error' => 'spip_sqlite_error', |
|
| 2512 | + 'explain' => 'spip_sqlite_explain', |
|
| 2513 | + 'fetch' => 'spip_sqlite_fetch', |
|
| 2514 | + 'seek' => 'spip_sqlite_seek', |
|
| 2515 | + 'free' => 'spip_sqlite_free', |
|
| 2516 | + 'hex' => 'spip_sqlite_hex', |
|
| 2517 | + 'in' => 'spip_sqlite_in', |
|
| 2518 | + 'insert' => 'spip_sqlite_insert', |
|
| 2519 | + 'insertq' => 'spip_sqlite_insertq', |
|
| 2520 | + 'insertq_multi' => 'spip_sqlite_insertq_multi', |
|
| 2521 | + 'listdbs' => 'spip_sqlite_listdbs', |
|
| 2522 | + 'multi' => 'spip_sqlite_multi', |
|
| 2523 | + 'optimize' => 'spip_sqlite_optimize', |
|
| 2524 | + 'query' => 'spip_sqlite_query', |
|
| 2525 | + 'quote' => 'spip_sqlite_quote', |
|
| 2526 | + 'repair' => 'spip_sqlite_repair', |
|
| 2527 | + 'replace' => 'spip_sqlite_replace', |
|
| 2528 | + 'replace_multi' => 'spip_sqlite_replace_multi', |
|
| 2529 | + 'select' => 'spip_sqlite_select', |
|
| 2530 | + 'selectdb' => 'spip_sqlite_selectdb', |
|
| 2531 | + 'set_charset' => 'spip_sqlite_set_charset', |
|
| 2532 | + 'get_charset' => 'spip_sqlite_get_charset', |
|
| 2533 | + 'showbase' => 'spip_sqlite_showbase', |
|
| 2534 | + 'showtable' => 'spip_sqlite_showtable', |
|
| 2535 | + 'table_exists' => 'spip_sqlite_table_exists', |
|
| 2536 | + 'update' => 'spip_sqlite_update', |
|
| 2537 | + 'updateq' => 'spip_sqlite_updateq', |
|
| 2538 | + 'preferer_transaction' => 'spip_sqlite_preferer_transaction', |
|
| 2539 | + 'demarrer_transaction' => 'spip_sqlite_demarrer_transaction', |
|
| 2540 | + 'terminer_transaction' => 'spip_sqlite_terminer_transaction', |
|
| 2541 | + ]; |
|
| 2542 | + |
|
| 2543 | + // association de chaque nom http d'un charset aux couples sqlite |
|
| 2544 | + // SQLite supporte utf-8 et utf-16 uniquement. |
|
| 2545 | + $charsets = [ |
|
| 2546 | + 'utf-8' => ['charset' => 'utf8', 'collation' => 'utf8_general_ci'], |
|
| 2547 | + //'utf-16be'=>array('charset'=>'utf16be','collation'=>'UTF-16BE'),// aucune idee de quoi il faut remplir dans es champs la |
|
| 2548 | + //'utf-16le'=>array('charset'=>'utf16le','collation'=>'UTF-16LE') |
|
| 2549 | + ]; |
|
| 2550 | + |
|
| 2551 | + $fonctions['charsets'] = $charsets; |
|
| 2552 | + |
|
| 2553 | + return $fonctions; |
|
| 2554 | 2554 | } |
| 2555 | 2555 | |
| 2556 | 2556 | |
@@ -2563,56 +2563,56 @@ discard block |
||
| 2563 | 2563 | */ |
| 2564 | 2564 | function _sqlite_remplacements_definitions_table($query, $autoinc = false) |
| 2565 | 2565 | { |
| 2566 | - // quelques remplacements |
|
| 2567 | - $num = '(\s*\([0-9]*\))?'; |
|
| 2568 | - $enum = '(\s*\([^\)]*\))?'; |
|
| 2569 | - |
|
| 2570 | - $remplace = [ |
|
| 2571 | - '/enum' . $enum . '/is' => 'VARCHAR(255)', |
|
| 2572 | - '/COLLATE \w+_bin/is' => 'COLLATE BINARY', |
|
| 2573 | - '/COLLATE \w+_ci/is' => 'COLLATE NOCASE', |
|
| 2574 | - '/auto_increment/is' => '', |
|
| 2575 | - '/current_timestamp\(\)/is' => 'CURRENT_TIMESTAMP', // Fix export depuis mariaDB #4374 |
|
| 2576 | - '/(timestamp .* )ON .*$/is' => '\\1', |
|
| 2577 | - '/character set \w+/is' => '', |
|
| 2578 | - '/((big|small|medium|tiny)?int(eger)?)' . $num . '\s*unsigned/is' => '\\1 UNSIGNED', |
|
| 2579 | - '/(text\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
|
| 2580 | - '/((char|varchar)' . $num . '\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
|
| 2581 | - '/(datetime\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00 00:00:00'", |
|
| 2582 | - '/(date\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00'", |
|
| 2583 | - ]; |
|
| 2584 | - |
|
| 2585 | - // pour l'autoincrement, il faut des INTEGER NOT NULL PRIMARY KEY |
|
| 2586 | - $remplace_autocinc = [ |
|
| 2587 | - '/(big|small|medium|tiny)?int(eger)?' . $num . '/is' => 'INTEGER' |
|
| 2588 | - ]; |
|
| 2589 | - // pour les int non autoincrement, il faut un DEFAULT |
|
| 2590 | - $remplace_nonautocinc = [ |
|
| 2591 | - '/((big|small|medium|tiny)?int(eger)?' . $num . '\s+not\s+null)\s*$/is' => "\\1 DEFAULT 0", |
|
| 2592 | - ]; |
|
| 2593 | - |
|
| 2594 | - if (is_string($query)) { |
|
| 2595 | - $query = preg_replace(array_keys($remplace), $remplace, $query); |
|
| 2596 | - if ($autoinc or preg_match(',AUTO_INCREMENT,is', $query)) { |
|
| 2597 | - $query = preg_replace(array_keys($remplace_autocinc), $remplace_autocinc, $query); |
|
| 2598 | - } else { |
|
| 2599 | - $query = preg_replace(array_keys($remplace_nonautocinc), $remplace_nonautocinc, $query); |
|
| 2600 | - $query = _sqlite_collate_ci($query); |
|
| 2601 | - } |
|
| 2602 | - } elseif (is_array($query)) { |
|
| 2603 | - foreach ($query as $k => $q) { |
|
| 2604 | - $ai = ($autoinc ? $k == $autoinc : preg_match(',AUTO_INCREMENT,is', $q)); |
|
| 2605 | - $query[$k] = preg_replace(array_keys($remplace), $remplace, $query[$k]); |
|
| 2606 | - if ($ai) { |
|
| 2607 | - $query[$k] = preg_replace(array_keys($remplace_autocinc), $remplace_autocinc, $query[$k]); |
|
| 2608 | - } else { |
|
| 2609 | - $query[$k] = preg_replace(array_keys($remplace_nonautocinc), $remplace_nonautocinc, $query[$k]); |
|
| 2610 | - $query[$k] = _sqlite_collate_ci($query[$k]); |
|
| 2611 | - } |
|
| 2612 | - } |
|
| 2613 | - } |
|
| 2614 | - |
|
| 2615 | - return $query; |
|
| 2566 | + // quelques remplacements |
|
| 2567 | + $num = '(\s*\([0-9]*\))?'; |
|
| 2568 | + $enum = '(\s*\([^\)]*\))?'; |
|
| 2569 | + |
|
| 2570 | + $remplace = [ |
|
| 2571 | + '/enum' . $enum . '/is' => 'VARCHAR(255)', |
|
| 2572 | + '/COLLATE \w+_bin/is' => 'COLLATE BINARY', |
|
| 2573 | + '/COLLATE \w+_ci/is' => 'COLLATE NOCASE', |
|
| 2574 | + '/auto_increment/is' => '', |
|
| 2575 | + '/current_timestamp\(\)/is' => 'CURRENT_TIMESTAMP', // Fix export depuis mariaDB #4374 |
|
| 2576 | + '/(timestamp .* )ON .*$/is' => '\\1', |
|
| 2577 | + '/character set \w+/is' => '', |
|
| 2578 | + '/((big|small|medium|tiny)?int(eger)?)' . $num . '\s*unsigned/is' => '\\1 UNSIGNED', |
|
| 2579 | + '/(text\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
|
| 2580 | + '/((char|varchar)' . $num . '\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
|
| 2581 | + '/(datetime\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00 00:00:00'", |
|
| 2582 | + '/(date\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00'", |
|
| 2583 | + ]; |
|
| 2584 | + |
|
| 2585 | + // pour l'autoincrement, il faut des INTEGER NOT NULL PRIMARY KEY |
|
| 2586 | + $remplace_autocinc = [ |
|
| 2587 | + '/(big|small|medium|tiny)?int(eger)?' . $num . '/is' => 'INTEGER' |
|
| 2588 | + ]; |
|
| 2589 | + // pour les int non autoincrement, il faut un DEFAULT |
|
| 2590 | + $remplace_nonautocinc = [ |
|
| 2591 | + '/((big|small|medium|tiny)?int(eger)?' . $num . '\s+not\s+null)\s*$/is' => "\\1 DEFAULT 0", |
|
| 2592 | + ]; |
|
| 2593 | + |
|
| 2594 | + if (is_string($query)) { |
|
| 2595 | + $query = preg_replace(array_keys($remplace), $remplace, $query); |
|
| 2596 | + if ($autoinc or preg_match(',AUTO_INCREMENT,is', $query)) { |
|
| 2597 | + $query = preg_replace(array_keys($remplace_autocinc), $remplace_autocinc, $query); |
|
| 2598 | + } else { |
|
| 2599 | + $query = preg_replace(array_keys($remplace_nonautocinc), $remplace_nonautocinc, $query); |
|
| 2600 | + $query = _sqlite_collate_ci($query); |
|
| 2601 | + } |
|
| 2602 | + } elseif (is_array($query)) { |
|
| 2603 | + foreach ($query as $k => $q) { |
|
| 2604 | + $ai = ($autoinc ? $k == $autoinc : preg_match(',AUTO_INCREMENT,is', $q)); |
|
| 2605 | + $query[$k] = preg_replace(array_keys($remplace), $remplace, $query[$k]); |
|
| 2606 | + if ($ai) { |
|
| 2607 | + $query[$k] = preg_replace(array_keys($remplace_autocinc), $remplace_autocinc, $query[$k]); |
|
| 2608 | + } else { |
|
| 2609 | + $query[$k] = preg_replace(array_keys($remplace_nonautocinc), $remplace_nonautocinc, $query[$k]); |
|
| 2610 | + $query[$k] = _sqlite_collate_ci($query[$k]); |
|
| 2611 | + } |
|
| 2612 | + } |
|
| 2613 | + } |
|
| 2614 | + |
|
| 2615 | + return $query; |
|
| 2616 | 2616 | } |
| 2617 | 2617 | |
| 2618 | 2618 | /** |
@@ -2624,17 +2624,17 @@ discard block |
||
| 2624 | 2624 | */ |
| 2625 | 2625 | function _sqlite_collate_ci($champ) |
| 2626 | 2626 | { |
| 2627 | - if (stripos($champ, 'COLLATE') !== false) { |
|
| 2628 | - return $champ; |
|
| 2629 | - } |
|
| 2630 | - if (stripos($champ, 'BINARY') !== false) { |
|
| 2631 | - return str_ireplace('BINARY', 'COLLATE BINARY', $champ); |
|
| 2632 | - } |
|
| 2633 | - if (preg_match(',^(char|varchar|(long|small|medium|tiny)?text),i', $champ)) { |
|
| 2634 | - return $champ . ' COLLATE NOCASE'; |
|
| 2635 | - } |
|
| 2636 | - |
|
| 2637 | - return $champ; |
|
| 2627 | + if (stripos($champ, 'COLLATE') !== false) { |
|
| 2628 | + return $champ; |
|
| 2629 | + } |
|
| 2630 | + if (stripos($champ, 'BINARY') !== false) { |
|
| 2631 | + return str_ireplace('BINARY', 'COLLATE BINARY', $champ); |
|
| 2632 | + } |
|
| 2633 | + if (preg_match(',^(char|varchar|(long|small|medium|tiny)?text),i', $champ)) { |
|
| 2634 | + return $champ . ' COLLATE NOCASE'; |
|
| 2635 | + } |
|
| 2636 | + |
|
| 2637 | + return $champ; |
|
| 2638 | 2638 | } |
| 2639 | 2639 | |
| 2640 | 2640 | |
@@ -2653,84 +2653,84 @@ discard block |
||
| 2653 | 2653 | * @return bool|string |
| 2654 | 2654 | */ |
| 2655 | 2655 | function _sqlite_requete_create( |
| 2656 | - $nom, |
|
| 2657 | - $champs, |
|
| 2658 | - $cles, |
|
| 2659 | - $autoinc = false, |
|
| 2660 | - $temporary = false, |
|
| 2661 | - $_ifnotexists = true, |
|
| 2662 | - $serveur = '', |
|
| 2663 | - $requeter = true |
|
| 2656 | + $nom, |
|
| 2657 | + $champs, |
|
| 2658 | + $cles, |
|
| 2659 | + $autoinc = false, |
|
| 2660 | + $temporary = false, |
|
| 2661 | + $_ifnotexists = true, |
|
| 2662 | + $serveur = '', |
|
| 2663 | + $requeter = true |
|
| 2664 | 2664 | ) { |
| 2665 | - $query = $keys = $s = $p = ''; |
|
| 2666 | - |
|
| 2667 | - // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 2668 | - // sans les renseigner (laisse le compilo recuperer la description) |
|
| 2669 | - if (!is_array($champs) || !is_array($cles)) { |
|
| 2670 | - return; |
|
| 2671 | - } |
|
| 2672 | - |
|
| 2673 | - // sqlite ne gere pas KEY tout court dans une requete CREATE TABLE |
|
| 2674 | - // il faut passer par des create index |
|
| 2675 | - // Il gere par contre primary key ! |
|
| 2676 | - // Soit la PK est definie dans les cles, soit dans un champs |
|
| 2677 | - // soit faussement dans les 2 (et dans ce cas, il faut l’enlever à un des 2 endroits !) |
|
| 2678 | - $pk = 'PRIMARY KEY'; |
|
| 2679 | - // le champ de cle primaire |
|
| 2680 | - $champ_pk = !empty($cles[$pk]) ? $cles[$pk] : ''; |
|
| 2681 | - |
|
| 2682 | - foreach ($champs as $k => $v) { |
|
| 2683 | - if (false !== stripos($v, $pk)) { |
|
| 2684 | - $champ_pk = $k; |
|
| 2685 | - // on n'en a plus besoin dans field, vu que defini dans key |
|
| 2686 | - $champs[$k] = preg_replace("/$pk/is", '', $champs[$k]); |
|
| 2687 | - break; |
|
| 2688 | - } |
|
| 2689 | - } |
|
| 2690 | - |
|
| 2691 | - if ($champ_pk) { |
|
| 2692 | - $keys = "\n\t\t$pk ($champ_pk)"; |
|
| 2693 | - } |
|
| 2694 | - // Pas de DEFAULT 0 sur les cles primaires en auto-increment |
|
| 2695 | - if ( |
|
| 2696 | - isset($champs[$champ_pk]) |
|
| 2697 | - and stripos($champs[$champ_pk], 'default 0') !== false |
|
| 2698 | - ) { |
|
| 2699 | - $champs[$champ_pk] = trim(str_ireplace('default 0', '', $champs[$champ_pk])); |
|
| 2700 | - } |
|
| 2701 | - |
|
| 2702 | - $champs = _sqlite_remplacements_definitions_table($champs, $autoinc ? $champ_pk : false); |
|
| 2703 | - foreach ($champs as $k => $v) { |
|
| 2704 | - $query .= "$s\n\t\t$k $v"; |
|
| 2705 | - $s = ','; |
|
| 2706 | - } |
|
| 2707 | - |
|
| 2708 | - $ifnotexists = ''; |
|
| 2709 | - if ($_ifnotexists) { |
|
| 2710 | - $version = spip_sqlite_fetch( |
|
| 2711 | - spip_sqlite_query('select sqlite_version() AS sqlite_version', $serveur), |
|
| 2712 | - '', |
|
| 2713 | - $serveur |
|
| 2714 | - ); |
|
| 2715 | - if (!function_exists('spip_version_compare')) { |
|
| 2716 | - include_spip('plugins/installer'); |
|
| 2717 | - } |
|
| 2718 | - |
|
| 2719 | - if ($version and spip_version_compare($version['sqlite_version'], '3.3.0', '>=')) { |
|
| 2720 | - $ifnotexists = ' IF NOT EXISTS'; |
|
| 2721 | - } else { |
|
| 2722 | - /* simuler le IF EXISTS - version 2 et sqlite < 3.3a */ |
|
| 2723 | - $a = spip_sqlite_showtable($nom, $serveur); |
|
| 2724 | - if (isset($a['key']['KEY ' . $nom])) { |
|
| 2725 | - return true; |
|
| 2726 | - } |
|
| 2727 | - } |
|
| 2728 | - } |
|
| 2729 | - |
|
| 2730 | - $temporary = $temporary ? ' TEMPORARY' : ''; |
|
| 2731 | - $q = "CREATE$temporary TABLE$ifnotexists $nom ($query" . ($keys ? ",$keys" : '') . ")\n"; |
|
| 2732 | - |
|
| 2733 | - return $q; |
|
| 2665 | + $query = $keys = $s = $p = ''; |
|
| 2666 | + |
|
| 2667 | + // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 2668 | + // sans les renseigner (laisse le compilo recuperer la description) |
|
| 2669 | + if (!is_array($champs) || !is_array($cles)) { |
|
| 2670 | + return; |
|
| 2671 | + } |
|
| 2672 | + |
|
| 2673 | + // sqlite ne gere pas KEY tout court dans une requete CREATE TABLE |
|
| 2674 | + // il faut passer par des create index |
|
| 2675 | + // Il gere par contre primary key ! |
|
| 2676 | + // Soit la PK est definie dans les cles, soit dans un champs |
|
| 2677 | + // soit faussement dans les 2 (et dans ce cas, il faut l’enlever à un des 2 endroits !) |
|
| 2678 | + $pk = 'PRIMARY KEY'; |
|
| 2679 | + // le champ de cle primaire |
|
| 2680 | + $champ_pk = !empty($cles[$pk]) ? $cles[$pk] : ''; |
|
| 2681 | + |
|
| 2682 | + foreach ($champs as $k => $v) { |
|
| 2683 | + if (false !== stripos($v, $pk)) { |
|
| 2684 | + $champ_pk = $k; |
|
| 2685 | + // on n'en a plus besoin dans field, vu que defini dans key |
|
| 2686 | + $champs[$k] = preg_replace("/$pk/is", '', $champs[$k]); |
|
| 2687 | + break; |
|
| 2688 | + } |
|
| 2689 | + } |
|
| 2690 | + |
|
| 2691 | + if ($champ_pk) { |
|
| 2692 | + $keys = "\n\t\t$pk ($champ_pk)"; |
|
| 2693 | + } |
|
| 2694 | + // Pas de DEFAULT 0 sur les cles primaires en auto-increment |
|
| 2695 | + if ( |
|
| 2696 | + isset($champs[$champ_pk]) |
|
| 2697 | + and stripos($champs[$champ_pk], 'default 0') !== false |
|
| 2698 | + ) { |
|
| 2699 | + $champs[$champ_pk] = trim(str_ireplace('default 0', '', $champs[$champ_pk])); |
|
| 2700 | + } |
|
| 2701 | + |
|
| 2702 | + $champs = _sqlite_remplacements_definitions_table($champs, $autoinc ? $champ_pk : false); |
|
| 2703 | + foreach ($champs as $k => $v) { |
|
| 2704 | + $query .= "$s\n\t\t$k $v"; |
|
| 2705 | + $s = ','; |
|
| 2706 | + } |
|
| 2707 | + |
|
| 2708 | + $ifnotexists = ''; |
|
| 2709 | + if ($_ifnotexists) { |
|
| 2710 | + $version = spip_sqlite_fetch( |
|
| 2711 | + spip_sqlite_query('select sqlite_version() AS sqlite_version', $serveur), |
|
| 2712 | + '', |
|
| 2713 | + $serveur |
|
| 2714 | + ); |
|
| 2715 | + if (!function_exists('spip_version_compare')) { |
|
| 2716 | + include_spip('plugins/installer'); |
|
| 2717 | + } |
|
| 2718 | + |
|
| 2719 | + if ($version and spip_version_compare($version['sqlite_version'], '3.3.0', '>=')) { |
|
| 2720 | + $ifnotexists = ' IF NOT EXISTS'; |
|
| 2721 | + } else { |
|
| 2722 | + /* simuler le IF EXISTS - version 2 et sqlite < 3.3a */ |
|
| 2723 | + $a = spip_sqlite_showtable($nom, $serveur); |
|
| 2724 | + if (isset($a['key']['KEY ' . $nom])) { |
|
| 2725 | + return true; |
|
| 2726 | + } |
|
| 2727 | + } |
|
| 2728 | + } |
|
| 2729 | + |
|
| 2730 | + $temporary = $temporary ? ' TEMPORARY' : ''; |
|
| 2731 | + $q = "CREATE$temporary TABLE$ifnotexists $nom ($query" . ($keys ? ",$keys" : '') . ")\n"; |
|
| 2732 | + |
|
| 2733 | + return $q; |
|
| 2734 | 2734 | } |
| 2735 | 2735 | |
| 2736 | 2736 | |
@@ -2750,40 +2750,40 @@ discard block |
||
| 2750 | 2750 | */ |
| 2751 | 2751 | function _sqlite_ajouter_champs_timestamp($table, $couples, $desc = '', $serveur = '') |
| 2752 | 2752 | { |
| 2753 | - static $tables = []; |
|
| 2754 | - |
|
| 2755 | - if (!isset($tables[$table])) { |
|
| 2756 | - if (!$desc) { |
|
| 2757 | - $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 2758 | - $desc = $trouver_table($table, $serveur); |
|
| 2759 | - // si pas de description, on ne fait rien, ou on die() ? |
|
| 2760 | - if (!$desc) { |
|
| 2761 | - return $couples; |
|
| 2762 | - } |
|
| 2763 | - } |
|
| 2764 | - |
|
| 2765 | - // recherche des champs avec simplement 'TIMESTAMP' |
|
| 2766 | - // cependant, il faudra peut etre etendre |
|
| 2767 | - // avec la gestion de DEFAULT et ON UPDATE |
|
| 2768 | - // mais ceux-ci ne sont pas utilises dans le core |
|
| 2769 | - $tables[$table] = ['valeur' => [], 'cite' => [], 'desc' => []]; |
|
| 2770 | - |
|
| 2771 | - $now = _sqlite_func_now(true); |
|
| 2772 | - foreach ($desc['field'] as $k => $v) { |
|
| 2773 | - if (strpos(strtolower(ltrim($v)), 'timestamp') === 0) { |
|
| 2774 | - $tables[$table]['desc'][$k] = $v; |
|
| 2775 | - $tables[$table]['valeur'][$k] = _sqlite_calculer_cite($now, $tables[$table]['desc'][$k]); |
|
| 2776 | - } |
|
| 2777 | - } |
|
| 2778 | - } else { |
|
| 2779 | - $now = _sqlite_func_now(true); |
|
| 2780 | - foreach (array_keys($tables[$table]['desc']) as $k) { |
|
| 2781 | - $tables[$table]['valeur'][$k] = _sqlite_calculer_cite($now, $tables[$table]['desc'][$k]); |
|
| 2782 | - } |
|
| 2783 | - } |
|
| 2784 | - |
|
| 2785 | - // ajout des champs type 'timestamp' absents |
|
| 2786 | - return array_merge($tables[$table]['valeur'], $couples); |
|
| 2753 | + static $tables = []; |
|
| 2754 | + |
|
| 2755 | + if (!isset($tables[$table])) { |
|
| 2756 | + if (!$desc) { |
|
| 2757 | + $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 2758 | + $desc = $trouver_table($table, $serveur); |
|
| 2759 | + // si pas de description, on ne fait rien, ou on die() ? |
|
| 2760 | + if (!$desc) { |
|
| 2761 | + return $couples; |
|
| 2762 | + } |
|
| 2763 | + } |
|
| 2764 | + |
|
| 2765 | + // recherche des champs avec simplement 'TIMESTAMP' |
|
| 2766 | + // cependant, il faudra peut etre etendre |
|
| 2767 | + // avec la gestion de DEFAULT et ON UPDATE |
|
| 2768 | + // mais ceux-ci ne sont pas utilises dans le core |
|
| 2769 | + $tables[$table] = ['valeur' => [], 'cite' => [], 'desc' => []]; |
|
| 2770 | + |
|
| 2771 | + $now = _sqlite_func_now(true); |
|
| 2772 | + foreach ($desc['field'] as $k => $v) { |
|
| 2773 | + if (strpos(strtolower(ltrim($v)), 'timestamp') === 0) { |
|
| 2774 | + $tables[$table]['desc'][$k] = $v; |
|
| 2775 | + $tables[$table]['valeur'][$k] = _sqlite_calculer_cite($now, $tables[$table]['desc'][$k]); |
|
| 2776 | + } |
|
| 2777 | + } |
|
| 2778 | + } else { |
|
| 2779 | + $now = _sqlite_func_now(true); |
|
| 2780 | + foreach (array_keys($tables[$table]['desc']) as $k) { |
|
| 2781 | + $tables[$table]['valeur'][$k] = _sqlite_calculer_cite($now, $tables[$table]['desc'][$k]); |
|
| 2782 | + } |
|
| 2783 | + } |
|
| 2784 | + |
|
| 2785 | + // ajout des champs type 'timestamp' absents |
|
| 2786 | + return array_merge($tables[$table]['valeur'], $couples); |
|
| 2787 | 2787 | } |
| 2788 | 2788 | |
| 2789 | 2789 | |
@@ -2795,5 +2795,5 @@ discard block |
||
| 2795 | 2795 | */ |
| 2796 | 2796 | function spip_versions_sqlite() |
| 2797 | 2797 | { |
| 2798 | - return _sqlite_charger_version(); |
|
| 2798 | + return _sqlite_charger_version(); |
|
| 2799 | 2799 | } |
@@ -19,11 +19,11 @@ discard block |
||
| 19 | 19 | */ |
| 20 | 20 | |
| 21 | 21 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 22 | - return; |
|
| 22 | + return; |
|
| 23 | 23 | } |
| 24 | 24 | |
| 25 | 25 | if (!defined('_MYSQL_NOPLANES')) { |
| 26 | - define('_MYSQL_NOPLANES', true); |
|
| 26 | + define('_MYSQL_NOPLANES', true); |
|
| 27 | 27 | } |
| 28 | 28 | |
| 29 | 29 | /** |
@@ -40,112 +40,112 @@ discard block |
||
| 40 | 40 | * - tableau décrivant la connexion sinon |
| 41 | 41 | */ |
| 42 | 42 | function req_mysql_dist($host, $port, $login, #[\SensitiveParameter] $pass, $db = '', $prefixe = '') { |
| 43 | - if (!extension_loaded(\mysqli::class)) { |
|
| 44 | - return false; |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - // si port est fourni mais pas host, c'est un socket -> compat avec vieille syntaxe de mysql_connect() et anciens fichiers connect.php |
|
| 48 | - try { |
|
| 49 | - if ( |
|
| 50 | - $port and !is_numeric($socket = $port) |
|
| 51 | - and (!$host or $host === 'localhost') |
|
| 52 | - ) { |
|
| 53 | - $link = @mysqli_connect($host, $login, $pass, '', null, $socket); |
|
| 54 | - } elseif ($port) { |
|
| 55 | - $link = @mysqli_connect($host, $login, $pass, '', $port); |
|
| 56 | - } else { |
|
| 57 | - $link = @mysqli_connect($host, $login, $pass); |
|
| 58 | - } |
|
| 59 | - } catch (\mysqli_sql_exception $e) { |
|
| 60 | - spip_log('mysqli_sql_exception: ' . $e->getMessage(), 'mysql.' . _LOG_DEBUG); |
|
| 61 | - $link = false; |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - if (!$link) { |
|
| 65 | - spip_log('Echec mysqli_connect. Erreur : ' . mysqli_connect_error(), 'mysql.' . _LOG_HS); |
|
| 66 | - |
|
| 67 | - return false; |
|
| 68 | - } |
|
| 69 | - $last = ''; |
|
| 70 | - if (!$db) { |
|
| 71 | - $ok = $link; |
|
| 72 | - $db = 'spip'; |
|
| 73 | - } else { |
|
| 74 | - $ok = mysqli_select_db($link, $db); |
|
| 75 | - if ( |
|
| 76 | - defined('_MYSQL_SET_SQL_MODE') |
|
| 77 | - or defined('_MYSQL_SQL_MODE_TEXT_NOT_NULL') // compatibilite |
|
| 78 | - ) { |
|
| 79 | - mysqli_query($link, $last = "set sql_mode=''"); |
|
| 80 | - } |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - spip_log( |
|
| 84 | - "Connexion MySQLi vers $host, base $db, prefixe $prefixe " . ($ok ? 'operationnelle' : 'impossible'), |
|
| 85 | - _LOG_DEBUG |
|
| 86 | - ); |
|
| 87 | - |
|
| 88 | - return !$ok ? false : [ |
|
| 89 | - 'db' => $db, |
|
| 90 | - 'last' => $last, |
|
| 91 | - 'prefixe' => $prefixe ?: $db, |
|
| 92 | - 'link' => $link, |
|
| 93 | - 'total_requetes' => 0, |
|
| 94 | - ]; |
|
| 43 | + if (!extension_loaded(\mysqli::class)) { |
|
| 44 | + return false; |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + // si port est fourni mais pas host, c'est un socket -> compat avec vieille syntaxe de mysql_connect() et anciens fichiers connect.php |
|
| 48 | + try { |
|
| 49 | + if ( |
|
| 50 | + $port and !is_numeric($socket = $port) |
|
| 51 | + and (!$host or $host === 'localhost') |
|
| 52 | + ) { |
|
| 53 | + $link = @mysqli_connect($host, $login, $pass, '', null, $socket); |
|
| 54 | + } elseif ($port) { |
|
| 55 | + $link = @mysqli_connect($host, $login, $pass, '', $port); |
|
| 56 | + } else { |
|
| 57 | + $link = @mysqli_connect($host, $login, $pass); |
|
| 58 | + } |
|
| 59 | + } catch (\mysqli_sql_exception $e) { |
|
| 60 | + spip_log('mysqli_sql_exception: ' . $e->getMessage(), 'mysql.' . _LOG_DEBUG); |
|
| 61 | + $link = false; |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + if (!$link) { |
|
| 65 | + spip_log('Echec mysqli_connect. Erreur : ' . mysqli_connect_error(), 'mysql.' . _LOG_HS); |
|
| 66 | + |
|
| 67 | + return false; |
|
| 68 | + } |
|
| 69 | + $last = ''; |
|
| 70 | + if (!$db) { |
|
| 71 | + $ok = $link; |
|
| 72 | + $db = 'spip'; |
|
| 73 | + } else { |
|
| 74 | + $ok = mysqli_select_db($link, $db); |
|
| 75 | + if ( |
|
| 76 | + defined('_MYSQL_SET_SQL_MODE') |
|
| 77 | + or defined('_MYSQL_SQL_MODE_TEXT_NOT_NULL') // compatibilite |
|
| 78 | + ) { |
|
| 79 | + mysqli_query($link, $last = "set sql_mode=''"); |
|
| 80 | + } |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + spip_log( |
|
| 84 | + "Connexion MySQLi vers $host, base $db, prefixe $prefixe " . ($ok ? 'operationnelle' : 'impossible'), |
|
| 85 | + _LOG_DEBUG |
|
| 86 | + ); |
|
| 87 | + |
|
| 88 | + return !$ok ? false : [ |
|
| 89 | + 'db' => $db, |
|
| 90 | + 'last' => $last, |
|
| 91 | + 'prefixe' => $prefixe ?: $db, |
|
| 92 | + 'link' => $link, |
|
| 93 | + 'total_requetes' => 0, |
|
| 94 | + ]; |
|
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | |
| 98 | 98 | $GLOBALS['spip_mysql_functions_1'] = [ |
| 99 | - 'alter' => 'spip_mysql_alter', |
|
| 100 | - 'count' => 'spip_mysql_count', |
|
| 101 | - 'countsel' => 'spip_mysql_countsel', |
|
| 102 | - 'create' => 'spip_mysql_create', |
|
| 103 | - 'create_base' => 'spip_mysql_create_base', |
|
| 104 | - 'create_view' => 'spip_mysql_create_view', |
|
| 105 | - 'date_proche' => 'spip_mysql_date_proche', |
|
| 106 | - 'delete' => 'spip_mysql_delete', |
|
| 107 | - 'drop_table' => 'spip_mysql_drop_table', |
|
| 108 | - 'drop_view' => 'spip_mysql_drop_view', |
|
| 109 | - 'errno' => 'spip_mysql_errno', |
|
| 110 | - 'error' => 'spip_mysql_error', |
|
| 111 | - 'explain' => 'spip_mysql_explain', |
|
| 112 | - 'fetch' => 'spip_mysql_fetch', |
|
| 113 | - 'seek' => 'spip_mysql_seek', |
|
| 114 | - 'free' => 'spip_mysql_free', |
|
| 115 | - 'hex' => 'spip_mysql_hex', |
|
| 116 | - 'in' => 'spip_mysql_in', |
|
| 117 | - 'insert' => 'spip_mysql_insert', |
|
| 118 | - 'insertq' => 'spip_mysql_insertq', |
|
| 119 | - 'insertq_multi' => 'spip_mysql_insertq_multi', |
|
| 120 | - 'listdbs' => 'spip_mysql_listdbs', |
|
| 121 | - 'multi' => 'spip_mysql_multi', |
|
| 122 | - 'optimize' => 'spip_mysql_optimize', |
|
| 123 | - 'query' => 'spip_mysql_query', |
|
| 124 | - 'quote' => 'spip_mysql_quote', |
|
| 125 | - 'replace' => 'spip_mysql_replace', |
|
| 126 | - 'replace_multi' => 'spip_mysql_replace_multi', |
|
| 127 | - 'repair' => 'spip_mysql_repair', |
|
| 128 | - 'select' => 'spip_mysql_select', |
|
| 129 | - 'selectdb' => 'spip_mysql_selectdb', |
|
| 130 | - 'set_charset' => 'spip_mysql_set_charset', |
|
| 131 | - 'get_charset' => 'spip_mysql_get_charset', |
|
| 132 | - 'showbase' => 'spip_mysql_showbase', |
|
| 133 | - 'showtable' => 'spip_mysql_showtable', |
|
| 134 | - 'table_exists' => 'spip_mysql_table_exists', |
|
| 135 | - 'update' => 'spip_mysql_update', |
|
| 136 | - 'updateq' => 'spip_mysql_updateq', |
|
| 137 | - |
|
| 138 | - // association de chaque nom http d'un charset aux couples MySQL |
|
| 139 | - 'charsets' => [ |
|
| 140 | - 'cp1250' => ['charset' => 'cp1250', 'collation' => 'cp1250_general_ci'], |
|
| 141 | - 'cp1251' => ['charset' => 'cp1251', 'collation' => 'cp1251_general_ci'], |
|
| 142 | - 'cp1256' => ['charset' => 'cp1256', 'collation' => 'cp1256_general_ci'], |
|
| 143 | - 'iso-8859-1' => ['charset' => 'latin1', 'collation' => 'latin1_swedish_ci'], |
|
| 99 | + 'alter' => 'spip_mysql_alter', |
|
| 100 | + 'count' => 'spip_mysql_count', |
|
| 101 | + 'countsel' => 'spip_mysql_countsel', |
|
| 102 | + 'create' => 'spip_mysql_create', |
|
| 103 | + 'create_base' => 'spip_mysql_create_base', |
|
| 104 | + 'create_view' => 'spip_mysql_create_view', |
|
| 105 | + 'date_proche' => 'spip_mysql_date_proche', |
|
| 106 | + 'delete' => 'spip_mysql_delete', |
|
| 107 | + 'drop_table' => 'spip_mysql_drop_table', |
|
| 108 | + 'drop_view' => 'spip_mysql_drop_view', |
|
| 109 | + 'errno' => 'spip_mysql_errno', |
|
| 110 | + 'error' => 'spip_mysql_error', |
|
| 111 | + 'explain' => 'spip_mysql_explain', |
|
| 112 | + 'fetch' => 'spip_mysql_fetch', |
|
| 113 | + 'seek' => 'spip_mysql_seek', |
|
| 114 | + 'free' => 'spip_mysql_free', |
|
| 115 | + 'hex' => 'spip_mysql_hex', |
|
| 116 | + 'in' => 'spip_mysql_in', |
|
| 117 | + 'insert' => 'spip_mysql_insert', |
|
| 118 | + 'insertq' => 'spip_mysql_insertq', |
|
| 119 | + 'insertq_multi' => 'spip_mysql_insertq_multi', |
|
| 120 | + 'listdbs' => 'spip_mysql_listdbs', |
|
| 121 | + 'multi' => 'spip_mysql_multi', |
|
| 122 | + 'optimize' => 'spip_mysql_optimize', |
|
| 123 | + 'query' => 'spip_mysql_query', |
|
| 124 | + 'quote' => 'spip_mysql_quote', |
|
| 125 | + 'replace' => 'spip_mysql_replace', |
|
| 126 | + 'replace_multi' => 'spip_mysql_replace_multi', |
|
| 127 | + 'repair' => 'spip_mysql_repair', |
|
| 128 | + 'select' => 'spip_mysql_select', |
|
| 129 | + 'selectdb' => 'spip_mysql_selectdb', |
|
| 130 | + 'set_charset' => 'spip_mysql_set_charset', |
|
| 131 | + 'get_charset' => 'spip_mysql_get_charset', |
|
| 132 | + 'showbase' => 'spip_mysql_showbase', |
|
| 133 | + 'showtable' => 'spip_mysql_showtable', |
|
| 134 | + 'table_exists' => 'spip_mysql_table_exists', |
|
| 135 | + 'update' => 'spip_mysql_update', |
|
| 136 | + 'updateq' => 'spip_mysql_updateq', |
|
| 137 | + |
|
| 138 | + // association de chaque nom http d'un charset aux couples MySQL |
|
| 139 | + 'charsets' => [ |
|
| 140 | + 'cp1250' => ['charset' => 'cp1250', 'collation' => 'cp1250_general_ci'], |
|
| 141 | + 'cp1251' => ['charset' => 'cp1251', 'collation' => 'cp1251_general_ci'], |
|
| 142 | + 'cp1256' => ['charset' => 'cp1256', 'collation' => 'cp1256_general_ci'], |
|
| 143 | + 'iso-8859-1' => ['charset' => 'latin1', 'collation' => 'latin1_swedish_ci'], |
|
| 144 | 144 | //'iso-8859-6'=>array('charset'=>'latin1','collation'=>'latin1_swedish_ci'), |
| 145 | - 'iso-8859-9' => ['charset' => 'latin5', 'collation' => 'latin5_turkish_ci'], |
|
| 145 | + 'iso-8859-9' => ['charset' => 'latin5', 'collation' => 'latin5_turkish_ci'], |
|
| 146 | 146 | //'iso-8859-15'=>array('charset'=>'latin1','collation'=>'latin1_swedish_ci'), |
| 147 | - 'utf-8' => ['charset' => 'utf8', 'collation' => 'utf8_general_ci'] |
|
| 148 | - ] |
|
| 147 | + 'utf-8' => ['charset' => 'utf8', 'collation' => 'utf8_general_ci'] |
|
| 148 | + ] |
|
| 149 | 149 | ]; |
| 150 | 150 | |
| 151 | 151 | |
@@ -156,9 +156,9 @@ discard block |
||
| 156 | 156 | * @return Object Information de connexion pour mysqli |
| 157 | 157 | */ |
| 158 | 158 | function _mysql_link($serveur = '') { |
| 159 | - $link = &$GLOBALS['connexions'][$serveur ?: 0]['link']; |
|
| 159 | + $link = &$GLOBALS['connexions'][$serveur ?: 0]['link']; |
|
| 160 | 160 | |
| 161 | - return $link; |
|
| 161 | + return $link; |
|
| 162 | 162 | } |
| 163 | 163 | |
| 164 | 164 | |
@@ -171,10 +171,10 @@ discard block |
||
| 171 | 171 | * @return mysqli_result|bool Jeu de résultats pour fetch() |
| 172 | 172 | */ |
| 173 | 173 | function spip_mysql_set_charset($charset, $serveur = '', $requeter = true) { |
| 174 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 175 | - spip_log('changement de charset sql : ' . 'SET NAMES ' . _q($charset), _LOG_DEBUG); |
|
| 174 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 175 | + spip_log('changement de charset sql : ' . 'SET NAMES ' . _q($charset), _LOG_DEBUG); |
|
| 176 | 176 | |
| 177 | - return mysqli_query($connexion['link'], $connexion['last'] = 'SET NAMES ' . _q($charset)); |
|
| 177 | + return mysqli_query($connexion['link'], $connexion['last'] = 'SET NAMES ' . _q($charset)); |
|
| 178 | 178 | } |
| 179 | 179 | |
| 180 | 180 | |
@@ -187,11 +187,11 @@ discard block |
||
| 187 | 187 | * @return array Description du charset (son nom est dans 'charset') |
| 188 | 188 | */ |
| 189 | 189 | function spip_mysql_get_charset($charset = [], $serveur = '', $requeter = true) { |
| 190 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 191 | - $connexion['last'] = $c = 'SHOW CHARACTER SET' |
|
| 192 | - . (!$charset ? '' : (' LIKE ' . _q($charset['charset']))); |
|
| 190 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 191 | + $connexion['last'] = $c = 'SHOW CHARACTER SET' |
|
| 192 | + . (!$charset ? '' : (' LIKE ' . _q($charset['charset']))); |
|
| 193 | 193 | |
| 194 | - return spip_mysql_fetch(mysqli_query($connexion['link'], $c), null, $serveur); |
|
| 194 | + return spip_mysql_fetch(mysqli_query($connexion['link'], $c), null, $serveur); |
|
| 195 | 195 | } |
| 196 | 196 | |
| 197 | 197 | /** |
@@ -207,80 +207,80 @@ discard block |
||
| 207 | 207 | */ |
| 208 | 208 | function spip_mysql_query($query, $serveur = '', $requeter = true) { |
| 209 | 209 | |
| 210 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 211 | - $prefixe = $connexion['prefixe']; |
|
| 212 | - $link = $connexion['link']; |
|
| 213 | - $db = $connexion['db']; |
|
| 214 | - |
|
| 215 | - $query = _mysql_traite_query($query, $db, $prefixe); |
|
| 216 | - |
|
| 217 | - // renvoyer la requete inerte si demandee |
|
| 218 | - if (!$requeter) { |
|
| 219 | - return $query; |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - if (isset($_GET['var_profile']) or (defined('_DEBUG_TRACE_QUERIES') and _DEBUG_TRACE_QUERIES)) { |
|
| 223 | - include_spip('public/tracer'); |
|
| 224 | - $t = trace_query_start(); |
|
| 225 | - } else { |
|
| 226 | - $t = 0; |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - $connexion['last'] = $query; |
|
| 230 | - $connexion['total_requetes']++; |
|
| 231 | - |
|
| 232 | - // ajouter un debug utile dans log/mysql-slow.log ? |
|
| 233 | - $debug = ''; |
|
| 234 | - if (defined('_DEBUG_SLOW_QUERIES') and _DEBUG_SLOW_QUERIES) { |
|
| 235 | - if (isset($GLOBALS['debug']['aucasou'])) { |
|
| 236 | - [, $id, , $infos] = $GLOBALS['debug']['aucasou']; |
|
| 237 | - $debug .= "BOUCLE$id @ " . ($infos[0] ?? '') . ' | '; |
|
| 238 | - } |
|
| 239 | - if (isset($_SERVER['REQUEST_URI'])) { |
|
| 240 | - $debug .= $_SERVER['REQUEST_URI']; |
|
| 241 | - } |
|
| 242 | - if (!empty($GLOBALS['ip'])) { |
|
| 243 | - $debug .= ' + ' . $GLOBALS['ip']; |
|
| 244 | - } |
|
| 245 | - $debug = ' /* ' . mysqli_real_escape_string($link, str_replace('*/', '@/', $debug)) . ' */'; |
|
| 246 | - } |
|
| 247 | - try { |
|
| 248 | - $r = mysqli_query($link, $query . $debug); |
|
| 249 | - } catch (\mysqli_sql_exception $e) { |
|
| 250 | - spip_log('mysqli_sql_exception: ' . $e->getMessage(), 'mysql.' . _LOG_DEBUG); |
|
| 251 | - $r = false; |
|
| 252 | - // TODO: utiliser l’exception ensuite plutôt que les appels à spip_mysql_errno() |
|
| 253 | - // mais il faut pour php < 8.1 forcer les exeptions via mysqli_report(). |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - //Eviter de propager le GoneAway sur les autres requetes d'un même processus PHP |
|
| 257 | - if ($e = spip_mysql_errno($serveur)) { // Log d'un Gone Away |
|
| 258 | - if ($e == 2006) { //Si Gone Away on relance une connexion vierge |
|
| 259 | - //Fermer la connexion defaillante |
|
| 260 | - mysqli_close($connexion['link']); |
|
| 261 | - unset($GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]); |
|
| 262 | - //Relancer une connexion vierge |
|
| 263 | - spip_connect($serveur); |
|
| 264 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 265 | - $link = $connexion['link']; |
|
| 266 | - //On retente au cas où |
|
| 267 | - try { |
|
| 268 | - $r = mysqli_query($link, $query . $debug); |
|
| 269 | - } catch (\mysqli_sql_exception $e) { |
|
| 270 | - spip_log('mysqli_sql_exception: ' . $e->getMessage(), 'mysql.' . _LOG_DEBUG); |
|
| 271 | - $r = false; |
|
| 272 | - // TODO: utiliser l’exception ensuite plutôt que les appels à spip_mysql_errno() |
|
| 273 | - // mais il faut pour php < 8.1 forcer les exeptions via mysqli_report(). |
|
| 274 | - } |
|
| 275 | - } |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - // Log de l'erreur eventuelle |
|
| 279 | - if ($e = spip_mysql_errno($serveur)) { |
|
| 280 | - // et du fautif |
|
| 281 | - $e .= spip_mysql_error($query, $serveur); |
|
| 282 | - } |
|
| 283 | - return $t ? trace_query_end($query, $t, $r, $e, $serveur) : $r; |
|
| 210 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 211 | + $prefixe = $connexion['prefixe']; |
|
| 212 | + $link = $connexion['link']; |
|
| 213 | + $db = $connexion['db']; |
|
| 214 | + |
|
| 215 | + $query = _mysql_traite_query($query, $db, $prefixe); |
|
| 216 | + |
|
| 217 | + // renvoyer la requete inerte si demandee |
|
| 218 | + if (!$requeter) { |
|
| 219 | + return $query; |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + if (isset($_GET['var_profile']) or (defined('_DEBUG_TRACE_QUERIES') and _DEBUG_TRACE_QUERIES)) { |
|
| 223 | + include_spip('public/tracer'); |
|
| 224 | + $t = trace_query_start(); |
|
| 225 | + } else { |
|
| 226 | + $t = 0; |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + $connexion['last'] = $query; |
|
| 230 | + $connexion['total_requetes']++; |
|
| 231 | + |
|
| 232 | + // ajouter un debug utile dans log/mysql-slow.log ? |
|
| 233 | + $debug = ''; |
|
| 234 | + if (defined('_DEBUG_SLOW_QUERIES') and _DEBUG_SLOW_QUERIES) { |
|
| 235 | + if (isset($GLOBALS['debug']['aucasou'])) { |
|
| 236 | + [, $id, , $infos] = $GLOBALS['debug']['aucasou']; |
|
| 237 | + $debug .= "BOUCLE$id @ " . ($infos[0] ?? '') . ' | '; |
|
| 238 | + } |
|
| 239 | + if (isset($_SERVER['REQUEST_URI'])) { |
|
| 240 | + $debug .= $_SERVER['REQUEST_URI']; |
|
| 241 | + } |
|
| 242 | + if (!empty($GLOBALS['ip'])) { |
|
| 243 | + $debug .= ' + ' . $GLOBALS['ip']; |
|
| 244 | + } |
|
| 245 | + $debug = ' /* ' . mysqli_real_escape_string($link, str_replace('*/', '@/', $debug)) . ' */'; |
|
| 246 | + } |
|
| 247 | + try { |
|
| 248 | + $r = mysqli_query($link, $query . $debug); |
|
| 249 | + } catch (\mysqli_sql_exception $e) { |
|
| 250 | + spip_log('mysqli_sql_exception: ' . $e->getMessage(), 'mysql.' . _LOG_DEBUG); |
|
| 251 | + $r = false; |
|
| 252 | + // TODO: utiliser l’exception ensuite plutôt que les appels à spip_mysql_errno() |
|
| 253 | + // mais il faut pour php < 8.1 forcer les exeptions via mysqli_report(). |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + //Eviter de propager le GoneAway sur les autres requetes d'un même processus PHP |
|
| 257 | + if ($e = spip_mysql_errno($serveur)) { // Log d'un Gone Away |
|
| 258 | + if ($e == 2006) { //Si Gone Away on relance une connexion vierge |
|
| 259 | + //Fermer la connexion defaillante |
|
| 260 | + mysqli_close($connexion['link']); |
|
| 261 | + unset($GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]); |
|
| 262 | + //Relancer une connexion vierge |
|
| 263 | + spip_connect($serveur); |
|
| 264 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 265 | + $link = $connexion['link']; |
|
| 266 | + //On retente au cas où |
|
| 267 | + try { |
|
| 268 | + $r = mysqli_query($link, $query . $debug); |
|
| 269 | + } catch (\mysqli_sql_exception $e) { |
|
| 270 | + spip_log('mysqli_sql_exception: ' . $e->getMessage(), 'mysql.' . _LOG_DEBUG); |
|
| 271 | + $r = false; |
|
| 272 | + // TODO: utiliser l’exception ensuite plutôt que les appels à spip_mysql_errno() |
|
| 273 | + // mais il faut pour php < 8.1 forcer les exeptions via mysqli_report(). |
|
| 274 | + } |
|
| 275 | + } |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + // Log de l'erreur eventuelle |
|
| 279 | + if ($e = spip_mysql_errno($serveur)) { |
|
| 280 | + // et du fautif |
|
| 281 | + $e .= spip_mysql_error($query, $serveur); |
|
| 282 | + } |
|
| 283 | + return $t ? trace_query_end($query, $t, $r, $e, $serveur) : $r; |
|
| 284 | 284 | } |
| 285 | 285 | |
| 286 | 286 | /** |
@@ -295,12 +295,12 @@ discard block |
||
| 295 | 295 | * - array : Tableau décrivant requête et temps d'exécution si var_profile actif pour tracer. |
| 296 | 296 | */ |
| 297 | 297 | function spip_mysql_alter($query, $serveur = '', $requeter = true) { |
| 298 | - // ici on supprime les ` entourant le nom de table pour permettre |
|
| 299 | - // la transposition du prefixe, compte tenu que les plugins ont la mauvaise habitude |
|
| 300 | - // d'utiliser ceux-ci, copie-colle de phpmyadmin |
|
| 301 | - $query = preg_replace(',^TABLE\s*`([^`]*)`,i', "TABLE \\1", $query); |
|
| 298 | + // ici on supprime les ` entourant le nom de table pour permettre |
|
| 299 | + // la transposition du prefixe, compte tenu que les plugins ont la mauvaise habitude |
|
| 300 | + // d'utiliser ceux-ci, copie-colle de phpmyadmin |
|
| 301 | + $query = preg_replace(',^TABLE\s*`([^`]*)`,i', "TABLE \\1", $query); |
|
| 302 | 302 | |
| 303 | - return spip_mysql_query('ALTER ' . $query, $serveur, $requeter); # i.e. que PG se debrouille |
|
| 303 | + return spip_mysql_query('ALTER ' . $query, $serveur, $requeter); # i.e. que PG se debrouille |
|
| 304 | 304 | } |
| 305 | 305 | |
| 306 | 306 | |
@@ -313,9 +313,9 @@ discard block |
||
| 313 | 313 | * @return bool Toujours true |
| 314 | 314 | */ |
| 315 | 315 | function spip_mysql_optimize($table, $serveur = '', $requeter = true) { |
| 316 | - spip_mysql_query('OPTIMIZE TABLE ' . $table); |
|
| 316 | + spip_mysql_query('OPTIMIZE TABLE ' . $table); |
|
| 317 | 317 | |
| 318 | - return true; |
|
| 318 | + return true; |
|
| 319 | 319 | } |
| 320 | 320 | |
| 321 | 321 | |
@@ -328,18 +328,18 @@ discard block |
||
| 328 | 328 | * @return array Tableau de l'explication |
| 329 | 329 | */ |
| 330 | 330 | function spip_mysql_explain($query, $serveur = '', $requeter = true) { |
| 331 | - if (strpos(ltrim($query), 'SELECT') !== 0) { |
|
| 332 | - return []; |
|
| 333 | - } |
|
| 334 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 335 | - $prefixe = $connexion['prefixe']; |
|
| 336 | - $link = $connexion['link']; |
|
| 337 | - $db = $connexion['db']; |
|
| 338 | - |
|
| 339 | - $query = 'EXPLAIN ' . _mysql_traite_query($query, $db, $prefixe); |
|
| 340 | - $r = mysqli_query($link, $query); |
|
| 341 | - |
|
| 342 | - return spip_mysql_fetch($r, null, $serveur); |
|
| 331 | + if (strpos(ltrim($query), 'SELECT') !== 0) { |
|
| 332 | + return []; |
|
| 333 | + } |
|
| 334 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 335 | + $prefixe = $connexion['prefixe']; |
|
| 336 | + $link = $connexion['link']; |
|
| 337 | + $db = $connexion['db']; |
|
| 338 | + |
|
| 339 | + $query = 'EXPLAIN ' . _mysql_traite_query($query, $db, $prefixe); |
|
| 340 | + $r = mysqli_query($link, $query); |
|
| 341 | + |
|
| 342 | + return spip_mysql_fetch($r, null, $serveur); |
|
| 343 | 343 | } |
| 344 | 344 | |
| 345 | 345 | |
@@ -368,35 +368,35 @@ discard block |
||
| 368 | 368 | * - array : Tableau décrivant requête et temps d'exécution si var_profile actif pour tracer. |
| 369 | 369 | */ |
| 370 | 370 | function spip_mysql_select( |
| 371 | - $select, |
|
| 372 | - $from, |
|
| 373 | - $where = '', |
|
| 374 | - $groupby = '', |
|
| 375 | - $orderby = '', |
|
| 376 | - $limit = '', |
|
| 377 | - $having = '', |
|
| 378 | - $serveur = '', |
|
| 379 | - $requeter = true |
|
| 371 | + $select, |
|
| 372 | + $from, |
|
| 373 | + $where = '', |
|
| 374 | + $groupby = '', |
|
| 375 | + $orderby = '', |
|
| 376 | + $limit = '', |
|
| 377 | + $having = '', |
|
| 378 | + $serveur = '', |
|
| 379 | + $requeter = true |
|
| 380 | 380 | ) { |
| 381 | 381 | |
| 382 | 382 | |
| 383 | - $from = (!is_array($from) ? $from : spip_mysql_select_as($from)); |
|
| 384 | - $query = |
|
| 385 | - calculer_mysql_expression('SELECT', $select, ', ') |
|
| 386 | - . calculer_mysql_expression('FROM', $from, ', ') |
|
| 387 | - . calculer_mysql_expression('WHERE', $where) |
|
| 388 | - . calculer_mysql_expression('GROUP BY', $groupby, ',') |
|
| 389 | - . calculer_mysql_expression('HAVING', $having) |
|
| 390 | - . ($orderby ? ("\nORDER BY " . spip_mysql_order($orderby)) : '') |
|
| 391 | - . ($limit ? "\nLIMIT $limit" : ''); |
|
| 383 | + $from = (!is_array($from) ? $from : spip_mysql_select_as($from)); |
|
| 384 | + $query = |
|
| 385 | + calculer_mysql_expression('SELECT', $select, ', ') |
|
| 386 | + . calculer_mysql_expression('FROM', $from, ', ') |
|
| 387 | + . calculer_mysql_expression('WHERE', $where) |
|
| 388 | + . calculer_mysql_expression('GROUP BY', $groupby, ',') |
|
| 389 | + . calculer_mysql_expression('HAVING', $having) |
|
| 390 | + . ($orderby ? ("\nORDER BY " . spip_mysql_order($orderby)) : '') |
|
| 391 | + . ($limit ? "\nLIMIT $limit" : ''); |
|
| 392 | 392 | |
| 393 | - // renvoyer la requete inerte si demandee |
|
| 394 | - if ($requeter === false) { |
|
| 395 | - return $query; |
|
| 396 | - } |
|
| 397 | - $r = spip_mysql_query($query, $serveur, $requeter); |
|
| 393 | + // renvoyer la requete inerte si demandee |
|
| 394 | + if ($requeter === false) { |
|
| 395 | + return $query; |
|
| 396 | + } |
|
| 397 | + $r = spip_mysql_query($query, $serveur, $requeter); |
|
| 398 | 398 | |
| 399 | - return $r ?: $query; |
|
| 399 | + return $r ?: $query; |
|
| 400 | 400 | } |
| 401 | 401 | |
| 402 | 402 | |
@@ -413,7 +413,7 @@ discard block |
||
| 413 | 413 | * @return string texte du orderby préparé |
| 414 | 414 | */ |
| 415 | 415 | function spip_mysql_order($orderby) { |
| 416 | - return (is_array($orderby)) ? join(', ', $orderby) : $orderby; |
|
| 416 | + return (is_array($orderby)) ? join(', ', $orderby) : $orderby; |
|
| 417 | 417 | } |
| 418 | 418 | |
| 419 | 419 | |
@@ -436,26 +436,26 @@ discard block |
||
| 436 | 436 | * Contrainte pour clause WHERE |
| 437 | 437 | */ |
| 438 | 438 | function calculer_mysql_where($v) { |
| 439 | - if (!is_array($v)) { |
|
| 440 | - return $v; |
|
| 441 | - } |
|
| 442 | - |
|
| 443 | - $op = array_shift($v); |
|
| 444 | - if (!($n = count($v))) { |
|
| 445 | - return $op; |
|
| 446 | - } else { |
|
| 447 | - $arg = calculer_mysql_where(array_shift($v)); |
|
| 448 | - if ($n == 1) { |
|
| 449 | - return "$op($arg)"; |
|
| 450 | - } else { |
|
| 451 | - $arg2 = calculer_mysql_where(array_shift($v)); |
|
| 452 | - if ($n == 2) { |
|
| 453 | - return "($arg $op $arg2)"; |
|
| 454 | - } else { |
|
| 455 | - return "($arg $op ($arg2) : $v[0])"; |
|
| 456 | - } |
|
| 457 | - } |
|
| 458 | - } |
|
| 439 | + if (!is_array($v)) { |
|
| 440 | + return $v; |
|
| 441 | + } |
|
| 442 | + |
|
| 443 | + $op = array_shift($v); |
|
| 444 | + if (!($n = count($v))) { |
|
| 445 | + return $op; |
|
| 446 | + } else { |
|
| 447 | + $arg = calculer_mysql_where(array_shift($v)); |
|
| 448 | + if ($n == 1) { |
|
| 449 | + return "$op($arg)"; |
|
| 450 | + } else { |
|
| 451 | + $arg2 = calculer_mysql_where(array_shift($v)); |
|
| 452 | + if ($n == 2) { |
|
| 453 | + return "($arg $op $arg2)"; |
|
| 454 | + } else { |
|
| 455 | + return "($arg $op ($arg2) : $v[0])"; |
|
| 456 | + } |
|
| 457 | + } |
|
| 458 | + } |
|
| 459 | 459 | } |
| 460 | 460 | |
| 461 | 461 | /** |
@@ -470,21 +470,21 @@ discard block |
||
| 470 | 470 | * @return string texte de l'expression, une partie donc, du texte la requête. |
| 471 | 471 | */ |
| 472 | 472 | function calculer_mysql_expression($expression, $v, $join = 'AND') { |
| 473 | - if (empty($v)) { |
|
| 474 | - return ''; |
|
| 475 | - } |
|
| 476 | - |
|
| 477 | - $exp = "\n$expression "; |
|
| 478 | - |
|
| 479 | - if (!is_array($v)) { |
|
| 480 | - return $exp . $v; |
|
| 481 | - } else { |
|
| 482 | - if (strtoupper($join) === 'AND') { |
|
| 483 | - return $exp . join("\n\t$join ", array_map('calculer_mysql_where', $v)); |
|
| 484 | - } else { |
|
| 485 | - return $exp . join($join, $v); |
|
| 486 | - } |
|
| 487 | - } |
|
| 473 | + if (empty($v)) { |
|
| 474 | + return ''; |
|
| 475 | + } |
|
| 476 | + |
|
| 477 | + $exp = "\n$expression "; |
|
| 478 | + |
|
| 479 | + if (!is_array($v)) { |
|
| 480 | + return $exp . $v; |
|
| 481 | + } else { |
|
| 482 | + if (strtoupper($join) === 'AND') { |
|
| 483 | + return $exp . join("\n\t$join ", array_map('calculer_mysql_where', $v)); |
|
| 484 | + } else { |
|
| 485 | + return $exp . join($join, $v); |
|
| 486 | + } |
|
| 487 | + } |
|
| 488 | 488 | } |
| 489 | 489 | |
| 490 | 490 | |
@@ -495,26 +495,26 @@ discard block |
||
| 495 | 495 | * @return string Sélection de colonnes pour une clause SELECT |
| 496 | 496 | */ |
| 497 | 497 | function spip_mysql_select_as($args) { |
| 498 | - $res = ''; |
|
| 499 | - foreach ($args as $k => $v) { |
|
| 500 | - if (substr($k, -1) == '@') { |
|
| 501 | - // c'est une jointure qui se refere au from precedent |
|
| 502 | - // pas de virgule |
|
| 503 | - $res .= ' ' . $v; |
|
| 504 | - } else { |
|
| 505 | - if (!is_numeric($k)) { |
|
| 506 | - $p = strpos($v, ' '); |
|
| 507 | - if ($p) { |
|
| 508 | - $v = substr($v, 0, $p) . " AS `$k`" . substr($v, $p); |
|
| 509 | - } else { |
|
| 510 | - $v .= " AS `$k`"; |
|
| 511 | - } |
|
| 512 | - } |
|
| 513 | - $res .= ', ' . $v; |
|
| 514 | - } |
|
| 515 | - } |
|
| 516 | - |
|
| 517 | - return substr($res, 2); |
|
| 498 | + $res = ''; |
|
| 499 | + foreach ($args as $k => $v) { |
|
| 500 | + if (substr($k, -1) == '@') { |
|
| 501 | + // c'est une jointure qui se refere au from precedent |
|
| 502 | + // pas de virgule |
|
| 503 | + $res .= ' ' . $v; |
|
| 504 | + } else { |
|
| 505 | + if (!is_numeric($k)) { |
|
| 506 | + $p = strpos($v, ' '); |
|
| 507 | + if ($p) { |
|
| 508 | + $v = substr($v, 0, $p) . " AS `$k`" . substr($v, $p); |
|
| 509 | + } else { |
|
| 510 | + $v .= " AS `$k`"; |
|
| 511 | + } |
|
| 512 | + } |
|
| 513 | + $res .= ', ' . $v; |
|
| 514 | + } |
|
| 515 | + } |
|
| 516 | + |
|
| 517 | + return substr($res, 2); |
|
| 518 | 518 | } |
| 519 | 519 | |
| 520 | 520 | |
@@ -539,58 +539,58 @@ discard block |
||
| 539 | 539 | */ |
| 540 | 540 | function _mysql_traite_query($query, $db = '', $prefixe = '', $echappe_textes = true) { |
| 541 | 541 | |
| 542 | - if ($GLOBALS['mysql_rappel_nom_base'] and $db) { |
|
| 543 | - $pref = '`' . $db . '`.'; |
|
| 544 | - } else { |
|
| 545 | - $pref = ''; |
|
| 546 | - } |
|
| 547 | - |
|
| 548 | - if ($prefixe) { |
|
| 549 | - $pref .= $prefixe . '_'; |
|
| 550 | - } |
|
| 551 | - |
|
| 552 | - if (!preg_match('/\s(SET|VALUES|WHERE|DATABASE)\s/i', $query, $regs)) { |
|
| 553 | - $suite = ''; |
|
| 554 | - } else { |
|
| 555 | - $suite = strstr($query, (string) $regs[0]); |
|
| 556 | - $query = substr($query, 0, -strlen($suite)); |
|
| 557 | - // propager le prefixe en cas de requete imbriquee |
|
| 558 | - // il faut alors echapper les chaine avant de le faire, pour ne pas risquer de |
|
| 559 | - // modifier une requete qui est en fait juste du texte dans un champ |
|
| 560 | - if (stripos($suite, 'SELECT') !== false) { |
|
| 561 | - if ($echappe_textes) { |
|
| 562 | - [$suite_echap, $textes] = query_echappe_textes($suite); |
|
| 563 | - } |
|
| 564 | - else { |
|
| 565 | - $suite_echap = $suite; |
|
| 566 | - } |
|
| 567 | - if (preg_match('/^(.*?)([(]\s*SELECT\b.*)$/si', $suite_echap, $r)) { |
|
| 568 | - $suite_echap = $r[1] . _mysql_traite_query($r[2], $db, $prefixe, false); |
|
| 569 | - if ($echappe_textes) { |
|
| 570 | - $suite = query_reinjecte_textes($suite_echap, $textes); |
|
| 571 | - } |
|
| 572 | - else { |
|
| 573 | - $suite = $suite_echap; |
|
| 574 | - } |
|
| 575 | - } |
|
| 576 | - } |
|
| 577 | - } |
|
| 578 | - $r = preg_replace(_SQL_PREFIXE_TABLE_MYSQL, '\1' . $pref, $query) . $suite; |
|
| 579 | - |
|
| 580 | - // en option, remplacer les emoji (que mysql ne sait pas gérer) en 💩 |
|
| 581 | - // remplacer les emoji (que mysql ne sait pas gérer) en 💩 |
|
| 582 | - if ( |
|
| 583 | - defined('_MYSQL_NOPLANES') |
|
| 584 | - and _MYSQL_NOPLANES |
|
| 585 | - and !empty($GLOBALS['meta']['charset_sql_connexion']) |
|
| 586 | - and $GLOBALS['meta']['charset_sql_connexion'] == 'utf8' |
|
| 587 | - ) { |
|
| 588 | - include_spip('inc/charsets'); |
|
| 589 | - $r = utf8_noplanes($r); |
|
| 590 | - } |
|
| 591 | - |
|
| 592 | - #spip_log("_mysql_traite_query: " . substr($r,0, 50) . ".... $db, $prefixe", _LOG_DEBUG); |
|
| 593 | - return $r; |
|
| 542 | + if ($GLOBALS['mysql_rappel_nom_base'] and $db) { |
|
| 543 | + $pref = '`' . $db . '`.'; |
|
| 544 | + } else { |
|
| 545 | + $pref = ''; |
|
| 546 | + } |
|
| 547 | + |
|
| 548 | + if ($prefixe) { |
|
| 549 | + $pref .= $prefixe . '_'; |
|
| 550 | + } |
|
| 551 | + |
|
| 552 | + if (!preg_match('/\s(SET|VALUES|WHERE|DATABASE)\s/i', $query, $regs)) { |
|
| 553 | + $suite = ''; |
|
| 554 | + } else { |
|
| 555 | + $suite = strstr($query, (string) $regs[0]); |
|
| 556 | + $query = substr($query, 0, -strlen($suite)); |
|
| 557 | + // propager le prefixe en cas de requete imbriquee |
|
| 558 | + // il faut alors echapper les chaine avant de le faire, pour ne pas risquer de |
|
| 559 | + // modifier une requete qui est en fait juste du texte dans un champ |
|
| 560 | + if (stripos($suite, 'SELECT') !== false) { |
|
| 561 | + if ($echappe_textes) { |
|
| 562 | + [$suite_echap, $textes] = query_echappe_textes($suite); |
|
| 563 | + } |
|
| 564 | + else { |
|
| 565 | + $suite_echap = $suite; |
|
| 566 | + } |
|
| 567 | + if (preg_match('/^(.*?)([(]\s*SELECT\b.*)$/si', $suite_echap, $r)) { |
|
| 568 | + $suite_echap = $r[1] . _mysql_traite_query($r[2], $db, $prefixe, false); |
|
| 569 | + if ($echappe_textes) { |
|
| 570 | + $suite = query_reinjecte_textes($suite_echap, $textes); |
|
| 571 | + } |
|
| 572 | + else { |
|
| 573 | + $suite = $suite_echap; |
|
| 574 | + } |
|
| 575 | + } |
|
| 576 | + } |
|
| 577 | + } |
|
| 578 | + $r = preg_replace(_SQL_PREFIXE_TABLE_MYSQL, '\1' . $pref, $query) . $suite; |
|
| 579 | + |
|
| 580 | + // en option, remplacer les emoji (que mysql ne sait pas gérer) en 💩 |
|
| 581 | + // remplacer les emoji (que mysql ne sait pas gérer) en 💩 |
|
| 582 | + if ( |
|
| 583 | + defined('_MYSQL_NOPLANES') |
|
| 584 | + and _MYSQL_NOPLANES |
|
| 585 | + and !empty($GLOBALS['meta']['charset_sql_connexion']) |
|
| 586 | + and $GLOBALS['meta']['charset_sql_connexion'] == 'utf8' |
|
| 587 | + ) { |
|
| 588 | + include_spip('inc/charsets'); |
|
| 589 | + $r = utf8_noplanes($r); |
|
| 590 | + } |
|
| 591 | + |
|
| 592 | + #spip_log("_mysql_traite_query: " . substr($r,0, 50) . ".... $db, $prefixe", _LOG_DEBUG); |
|
| 593 | + return $r; |
|
| 594 | 594 | } |
| 595 | 595 | |
| 596 | 596 | /** |
@@ -608,17 +608,17 @@ discard block |
||
| 608 | 608 | * - False en cas d'erreur. |
| 609 | 609 | **/ |
| 610 | 610 | function spip_mysql_selectdb($db, $serveur = '', $requeter = true) { |
| 611 | - $link = _mysql_link($serveur); |
|
| 612 | - try { |
|
| 613 | - $ok = mysqli_select_db($link, $db); |
|
| 614 | - } catch (\mysqli_sql_exception $e) { |
|
| 615 | - $ok = false; |
|
| 616 | - } |
|
| 617 | - if (!$ok) { |
|
| 618 | - spip_log('Echec mysqli_selectdb. Erreur : ' . mysqli_error($link), 'mysql.' . _LOG_CRITIQUE); |
|
| 619 | - } |
|
| 620 | - |
|
| 621 | - return $ok; |
|
| 611 | + $link = _mysql_link($serveur); |
|
| 612 | + try { |
|
| 613 | + $ok = mysqli_select_db($link, $db); |
|
| 614 | + } catch (\mysqli_sql_exception $e) { |
|
| 615 | + $ok = false; |
|
| 616 | + } |
|
| 617 | + if (!$ok) { |
|
| 618 | + spip_log('Echec mysqli_selectdb. Erreur : ' . mysqli_error($link), 'mysql.' . _LOG_CRITIQUE); |
|
| 619 | + } |
|
| 620 | + |
|
| 621 | + return $ok; |
|
| 622 | 622 | } |
| 623 | 623 | |
| 624 | 624 | |
@@ -639,14 +639,14 @@ discard block |
||
| 639 | 639 | * Liste de noms de bases de données |
| 640 | 640 | **/ |
| 641 | 641 | function spip_mysql_listdbs($serveur = '', $requeter = true) { |
| 642 | - $dbs = []; |
|
| 643 | - if ($res = spip_mysql_query('SHOW DATABASES', $serveur)) { |
|
| 644 | - while ($row = mysqli_fetch_assoc($res)) { |
|
| 645 | - $dbs[] = $row['Database']; |
|
| 646 | - } |
|
| 647 | - } |
|
| 648 | - |
|
| 649 | - return $dbs; |
|
| 642 | + $dbs = []; |
|
| 643 | + if ($res = spip_mysql_query('SHOW DATABASES', $serveur)) { |
|
| 644 | + while ($row = mysqli_fetch_assoc($res)) { |
|
| 645 | + $dbs[] = $row['Database']; |
|
| 646 | + } |
|
| 647 | + } |
|
| 648 | + |
|
| 649 | + return $dbs; |
|
| 650 | 650 | } |
| 651 | 651 | |
| 652 | 652 | |
@@ -669,73 +669,73 @@ discard block |
||
| 669 | 669 | * - true si la requête réussie, false sinon. |
| 670 | 670 | */ |
| 671 | 671 | function spip_mysql_create( |
| 672 | - $nom, |
|
| 673 | - $champs, |
|
| 674 | - $cles, |
|
| 675 | - $autoinc = false, |
|
| 676 | - $temporary = false, |
|
| 677 | - $serveur = '', |
|
| 678 | - $requeter = true |
|
| 672 | + $nom, |
|
| 673 | + $champs, |
|
| 674 | + $cles, |
|
| 675 | + $autoinc = false, |
|
| 676 | + $temporary = false, |
|
| 677 | + $serveur = '', |
|
| 678 | + $requeter = true |
|
| 679 | 679 | ) { |
| 680 | 680 | |
| 681 | - $query = ''; |
|
| 682 | - $keys = ''; |
|
| 683 | - $s = ''; |
|
| 684 | - $p = ''; |
|
| 685 | - |
|
| 686 | - // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 687 | - // sans les renseigner (laisse le compilo recuperer la description) |
|
| 688 | - if (!is_array($champs) || !is_array($cles)) { |
|
| 689 | - return; |
|
| 690 | - } |
|
| 691 | - |
|
| 692 | - $res = spip_mysql_query('SELECT version() as v', $serveur); |
|
| 693 | - if (($row = mysqli_fetch_array($res)) && (version_compare($row['v'], '5.0', '>='))) { |
|
| 694 | - spip_mysql_query("SET sql_mode=''", $serveur); |
|
| 695 | - } |
|
| 696 | - |
|
| 697 | - foreach ($cles as $k => $v) { |
|
| 698 | - $keys .= "$s\n\t\t$k ($v)"; |
|
| 699 | - if ($k == 'PRIMARY KEY') { |
|
| 700 | - $p = $v; |
|
| 701 | - } |
|
| 702 | - $s = ','; |
|
| 703 | - } |
|
| 704 | - $s = ''; |
|
| 705 | - |
|
| 706 | - $character_set = ''; |
|
| 707 | - if (@$GLOBALS['meta']['charset_sql_base']) { |
|
| 708 | - $character_set .= ' CHARACTER SET ' . $GLOBALS['meta']['charset_sql_base']; |
|
| 709 | - } |
|
| 710 | - if (@$GLOBALS['meta']['charset_collation_sql_base']) { |
|
| 711 | - $character_set .= ' COLLATE ' . $GLOBALS['meta']['charset_collation_sql_base']; |
|
| 712 | - } |
|
| 713 | - |
|
| 714 | - foreach ($champs as $k => $v) { |
|
| 715 | - $v = _mysql_remplacements_definitions_table($v); |
|
| 716 | - if (preg_match(',([a-z]*\s*(\(\s*[0-9]*\s*\))?(\s*binary)?),i', $v, $defs)) { |
|
| 717 | - if ( |
|
| 718 | - preg_match(',(char|text),i', $defs[1]) |
|
| 719 | - and !preg_match(',(binary|CHARACTER|COLLATE),i', $v) |
|
| 720 | - ) { |
|
| 721 | - $v = $defs[1] . $character_set . ' ' . substr($v, strlen($defs[1])); |
|
| 722 | - } |
|
| 723 | - } |
|
| 724 | - |
|
| 725 | - $query .= "$s\n\t\t$k $v" |
|
| 726 | - . (($autoinc && ($p == $k) && preg_match(',\b(big|small|medium)?int\b,i', $v)) |
|
| 727 | - ? ' auto_increment' |
|
| 728 | - : '' |
|
| 729 | - ); |
|
| 730 | - $s = ','; |
|
| 731 | - } |
|
| 732 | - $temporary = $temporary ? 'TEMPORARY' : ''; |
|
| 733 | - $q = "CREATE $temporary TABLE IF NOT EXISTS $nom ($query" . ($keys ? ",$keys" : '') . ')' |
|
| 734 | - . (defined('_MYSQL_ENGINE') ? ' ENGINE=' . _MYSQL_ENGINE : '') |
|
| 735 | - . ($character_set ? " DEFAULT $character_set" : '') |
|
| 736 | - . "\n"; |
|
| 737 | - |
|
| 738 | - return spip_mysql_query($q, $serveur); |
|
| 681 | + $query = ''; |
|
| 682 | + $keys = ''; |
|
| 683 | + $s = ''; |
|
| 684 | + $p = ''; |
|
| 685 | + |
|
| 686 | + // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 687 | + // sans les renseigner (laisse le compilo recuperer la description) |
|
| 688 | + if (!is_array($champs) || !is_array($cles)) { |
|
| 689 | + return; |
|
| 690 | + } |
|
| 691 | + |
|
| 692 | + $res = spip_mysql_query('SELECT version() as v', $serveur); |
|
| 693 | + if (($row = mysqli_fetch_array($res)) && (version_compare($row['v'], '5.0', '>='))) { |
|
| 694 | + spip_mysql_query("SET sql_mode=''", $serveur); |
|
| 695 | + } |
|
| 696 | + |
|
| 697 | + foreach ($cles as $k => $v) { |
|
| 698 | + $keys .= "$s\n\t\t$k ($v)"; |
|
| 699 | + if ($k == 'PRIMARY KEY') { |
|
| 700 | + $p = $v; |
|
| 701 | + } |
|
| 702 | + $s = ','; |
|
| 703 | + } |
|
| 704 | + $s = ''; |
|
| 705 | + |
|
| 706 | + $character_set = ''; |
|
| 707 | + if (@$GLOBALS['meta']['charset_sql_base']) { |
|
| 708 | + $character_set .= ' CHARACTER SET ' . $GLOBALS['meta']['charset_sql_base']; |
|
| 709 | + } |
|
| 710 | + if (@$GLOBALS['meta']['charset_collation_sql_base']) { |
|
| 711 | + $character_set .= ' COLLATE ' . $GLOBALS['meta']['charset_collation_sql_base']; |
|
| 712 | + } |
|
| 713 | + |
|
| 714 | + foreach ($champs as $k => $v) { |
|
| 715 | + $v = _mysql_remplacements_definitions_table($v); |
|
| 716 | + if (preg_match(',([a-z]*\s*(\(\s*[0-9]*\s*\))?(\s*binary)?),i', $v, $defs)) { |
|
| 717 | + if ( |
|
| 718 | + preg_match(',(char|text),i', $defs[1]) |
|
| 719 | + and !preg_match(',(binary|CHARACTER|COLLATE),i', $v) |
|
| 720 | + ) { |
|
| 721 | + $v = $defs[1] . $character_set . ' ' . substr($v, strlen($defs[1])); |
|
| 722 | + } |
|
| 723 | + } |
|
| 724 | + |
|
| 725 | + $query .= "$s\n\t\t$k $v" |
|
| 726 | + . (($autoinc && ($p == $k) && preg_match(',\b(big|small|medium)?int\b,i', $v)) |
|
| 727 | + ? ' auto_increment' |
|
| 728 | + : '' |
|
| 729 | + ); |
|
| 730 | + $s = ','; |
|
| 731 | + } |
|
| 732 | + $temporary = $temporary ? 'TEMPORARY' : ''; |
|
| 733 | + $q = "CREATE $temporary TABLE IF NOT EXISTS $nom ($query" . ($keys ? ",$keys" : '') . ')' |
|
| 734 | + . (defined('_MYSQL_ENGINE') ? ' ENGINE=' . _MYSQL_ENGINE : '') |
|
| 735 | + . ($character_set ? " DEFAULT $character_set" : '') |
|
| 736 | + . "\n"; |
|
| 737 | + |
|
| 738 | + return spip_mysql_query($q, $serveur); |
|
| 739 | 739 | } |
| 740 | 740 | |
| 741 | 741 | |
@@ -748,25 +748,25 @@ discard block |
||
| 748 | 748 | * Définition SQL adaptée pour MySQL d'un champ de table |
| 749 | 749 | */ |
| 750 | 750 | function _mysql_remplacements_definitions_table($query) { |
| 751 | - // quelques remplacements |
|
| 752 | - $num = '(\s*\([0-9]*\))?'; |
|
| 753 | - $enum = '(\s*\([^\)]*\))?'; |
|
| 754 | - |
|
| 755 | - $remplace = [ |
|
| 756 | - '/VARCHAR(\s*[^\s\(])/is' => 'VARCHAR(255)\\1', |
|
| 757 | - '/^TIMESTAMP($| NULL DEFAULT NULL)/is' => 'TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP', |
|
| 758 | - ]; |
|
| 759 | - |
|
| 760 | - if (is_string($query)) { |
|
| 761 | - $query = preg_replace(array_keys($remplace), $remplace, $query); |
|
| 762 | - } elseif (is_array($query)) { |
|
| 763 | - $keys = array_keys($remplace); |
|
| 764 | - foreach ($query as $k => $q) { |
|
| 765 | - $query[$k] = preg_replace($keys, $remplace, $q); |
|
| 766 | - } |
|
| 767 | - } |
|
| 768 | - |
|
| 769 | - return $query; |
|
| 751 | + // quelques remplacements |
|
| 752 | + $num = '(\s*\([0-9]*\))?'; |
|
| 753 | + $enum = '(\s*\([^\)]*\))?'; |
|
| 754 | + |
|
| 755 | + $remplace = [ |
|
| 756 | + '/VARCHAR(\s*[^\s\(])/is' => 'VARCHAR(255)\\1', |
|
| 757 | + '/^TIMESTAMP($| NULL DEFAULT NULL)/is' => 'TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP', |
|
| 758 | + ]; |
|
| 759 | + |
|
| 760 | + if (is_string($query)) { |
|
| 761 | + $query = preg_replace(array_keys($remplace), $remplace, $query); |
|
| 762 | + } elseif (is_array($query)) { |
|
| 763 | + $keys = array_keys($remplace); |
|
| 764 | + foreach ($query as $k => $q) { |
|
| 765 | + $query[$k] = preg_replace($keys, $remplace, $q); |
|
| 766 | + } |
|
| 767 | + } |
|
| 768 | + |
|
| 769 | + return $query; |
|
| 770 | 770 | } |
| 771 | 771 | |
| 772 | 772 | |
@@ -779,7 +779,7 @@ discard block |
||
| 779 | 779 | * @return bool true si la base est créee. |
| 780 | 780 | **/ |
| 781 | 781 | function spip_mysql_create_base($nom, $serveur = '', $requeter = true) { |
| 782 | - return spip_mysql_query("CREATE DATABASE `$nom`", $serveur, $requeter); |
|
| 782 | + return spip_mysql_query("CREATE DATABASE `$nom`", $serveur, $requeter); |
|
| 783 | 783 | } |
| 784 | 784 | |
| 785 | 785 | |
@@ -800,19 +800,19 @@ discard block |
||
| 800 | 800 | * - string texte de la requête si $requeter vaut false |
| 801 | 801 | */ |
| 802 | 802 | function spip_mysql_create_view($nom, $query_select, $serveur = '', $requeter = true) { |
| 803 | - if (!$query_select) { |
|
| 804 | - return false; |
|
| 805 | - } |
|
| 806 | - // vue deja presente |
|
| 807 | - if (sql_showtable($nom, false, $serveur)) { |
|
| 808 | - spip_log("Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", _LOG_ERREUR); |
|
| 803 | + if (!$query_select) { |
|
| 804 | + return false; |
|
| 805 | + } |
|
| 806 | + // vue deja presente |
|
| 807 | + if (sql_showtable($nom, false, $serveur)) { |
|
| 808 | + spip_log("Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", _LOG_ERREUR); |
|
| 809 | 809 | |
| 810 | - return false; |
|
| 811 | - } |
|
| 810 | + return false; |
|
| 811 | + } |
|
| 812 | 812 | |
| 813 | - $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 813 | + $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 814 | 814 | |
| 815 | - return spip_mysql_query($query, $serveur, $requeter); |
|
| 815 | + return spip_mysql_query($query, $serveur, $requeter); |
|
| 816 | 816 | } |
| 817 | 817 | |
| 818 | 818 | |
@@ -828,11 +828,11 @@ discard block |
||
| 828 | 828 | * - true si la requête a réussie, false sinon |
| 829 | 829 | */ |
| 830 | 830 | function spip_mysql_drop_table($table, $exist = '', $serveur = '', $requeter = true) { |
| 831 | - if ($exist) { |
|
| 832 | - $exist = ' IF EXISTS'; |
|
| 833 | - } |
|
| 831 | + if ($exist) { |
|
| 832 | + $exist = ' IF EXISTS'; |
|
| 833 | + } |
|
| 834 | 834 | |
| 835 | - return spip_mysql_query("DROP TABLE$exist $table", $serveur, $requeter); |
|
| 835 | + return spip_mysql_query("DROP TABLE$exist $table", $serveur, $requeter); |
|
| 836 | 836 | } |
| 837 | 837 | |
| 838 | 838 | /** |
@@ -847,11 +847,11 @@ discard block |
||
| 847 | 847 | * - true si la requête a réussie, false sinon |
| 848 | 848 | */ |
| 849 | 849 | function spip_mysql_drop_view($view, $exist = '', $serveur = '', $requeter = true) { |
| 850 | - if ($exist) { |
|
| 851 | - $exist = ' IF EXISTS'; |
|
| 852 | - } |
|
| 850 | + if ($exist) { |
|
| 851 | + $exist = ' IF EXISTS'; |
|
| 852 | + } |
|
| 853 | 853 | |
| 854 | - return spip_mysql_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 854 | + return spip_mysql_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 855 | 855 | } |
| 856 | 856 | |
| 857 | 857 | /** |
@@ -868,7 +868,7 @@ discard block |
||
| 868 | 868 | * Ressource à utiliser avec sql_fetch() |
| 869 | 869 | **/ |
| 870 | 870 | function spip_mysql_showbase($match, $serveur = '', $requeter = true) { |
| 871 | - return spip_mysql_query('SHOW TABLES LIKE ' . _q($match), $serveur, $requeter); |
|
| 871 | + return spip_mysql_query('SHOW TABLES LIKE ' . _q($match), $serveur, $requeter); |
|
| 872 | 872 | } |
| 873 | 873 | |
| 874 | 874 | /** |
@@ -884,18 +884,18 @@ discard block |
||
| 884 | 884 | * - true si la requête a réussie, false sinon |
| 885 | 885 | */ |
| 886 | 886 | function spip_mysql_repair($table, $serveur = '', $requeter = true) { |
| 887 | - $table_status = spip_mysql_fetch(spip_mysql_query('SHOW TABLE STATUS WHERE Name = ' . _q($table), $serveur, true)); |
|
| 888 | - $engine = $table_status['Engine']; |
|
| 889 | - if ($engine == 'InnoDB') { |
|
| 890 | - if (spip_mysql_alter("TABLE $table ENGINE = InnoDB", $serveur, $requeter)) { |
|
| 891 | - return [' OK ']; |
|
| 892 | - } |
|
| 893 | - } elseif ($engine == 'MyISAM') { |
|
| 894 | - return spip_mysql_query("REPAIR TABLE `$table`", $serveur, $requeter); |
|
| 895 | - } else { |
|
| 896 | - spip_log("spip_mysql_repair impossible pour la table $table engine $engine", 'mysql.' . _LOG_DEBUG); |
|
| 897 | - } |
|
| 898 | - return false; |
|
| 887 | + $table_status = spip_mysql_fetch(spip_mysql_query('SHOW TABLE STATUS WHERE Name = ' . _q($table), $serveur, true)); |
|
| 888 | + $engine = $table_status['Engine']; |
|
| 889 | + if ($engine == 'InnoDB') { |
|
| 890 | + if (spip_mysql_alter("TABLE $table ENGINE = InnoDB", $serveur, $requeter)) { |
|
| 891 | + return [' OK ']; |
|
| 892 | + } |
|
| 893 | + } elseif ($engine == 'MyISAM') { |
|
| 894 | + return spip_mysql_query("REPAIR TABLE `$table`", $serveur, $requeter); |
|
| 895 | + } else { |
|
| 896 | + spip_log("spip_mysql_repair impossible pour la table $table engine $engine", 'mysql.' . _LOG_DEBUG); |
|
| 897 | + } |
|
| 898 | + return false; |
|
| 899 | 899 | } |
| 900 | 900 | |
| 901 | 901 | /** |
@@ -913,12 +913,12 @@ discard block |
||
| 913 | 913 | * - string : requete sql, si $requeter = true |
| 914 | 914 | **/ |
| 915 | 915 | function spip_mysql_table_exists(string $table, $serveur = '', $requeter = true) { |
| 916 | - $r = spip_mysql_query('SHOW TABLES LIKE ' . _q($table), $serveur, $requeter); |
|
| 917 | - if (!$requeter) { |
|
| 918 | - return $r; |
|
| 919 | - } |
|
| 920 | - $res = spip_mysql_fetch($r); |
|
| 921 | - return (bool) $res; |
|
| 916 | + $r = spip_mysql_query('SHOW TABLES LIKE ' . _q($table), $serveur, $requeter); |
|
| 917 | + if (!$requeter) { |
|
| 918 | + return $r; |
|
| 919 | + } |
|
| 920 | + $res = spip_mysql_fetch($r); |
|
| 921 | + return (bool) $res; |
|
| 922 | 922 | } |
| 923 | 923 | |
| 924 | 924 | define('_MYSQL_RE_SHOW_TABLE', '/^[^(),]*\(((?:[^()]*\((?:[^()]*\([^()]*\))?[^()]*\)[^()]*)*[^()]*)\)[^()]*$/'); |
@@ -941,86 +941,86 @@ discard block |
||
| 941 | 941 | * - array description de la table sinon |
| 942 | 942 | */ |
| 943 | 943 | function spip_mysql_showtable($nom_table, $serveur = '', $requeter = true) { |
| 944 | - $s = spip_mysql_query("SHOW CREATE TABLE `$nom_table`", $serveur, $requeter); |
|
| 945 | - if (!$s) { |
|
| 946 | - return ''; |
|
| 947 | - } |
|
| 948 | - if (!$requeter) { |
|
| 949 | - return $s; |
|
| 950 | - } |
|
| 951 | - |
|
| 952 | - [, $a] = mysqli_fetch_array($s, MYSQLI_NUM); |
|
| 953 | - if (preg_match(_MYSQL_RE_SHOW_TABLE, $a, $r)) { |
|
| 954 | - $desc = $r[1]; |
|
| 955 | - // extraction d'une KEY éventuelle en prenant garde de ne pas |
|
| 956 | - // relever un champ dont le nom contient KEY (ex. ID_WHISKEY) |
|
| 957 | - if (preg_match('/^(.*?),([^,]*\sKEY[ (].*)$/s', $desc, $r)) { |
|
| 958 | - $namedkeys = $r[2]; |
|
| 959 | - $desc = $r[1]; |
|
| 960 | - } else { |
|
| 961 | - $namedkeys = ''; |
|
| 962 | - } |
|
| 963 | - |
|
| 964 | - $fields = []; |
|
| 965 | - foreach (preg_split('/,\s*`/', $desc) as $v) { |
|
| 966 | - preg_match('/^\s*`?([^`]*)`\s*(.*)/', $v, $r); |
|
| 967 | - $fields[strtolower($r[1])] = $r[2]; |
|
| 968 | - } |
|
| 969 | - $keys = []; |
|
| 970 | - |
|
| 971 | - foreach (preg_split('/\)\s*(,|$)/', $namedkeys) as $v) { |
|
| 972 | - if (preg_match('/^\s*([^(]*)\(([^(]*(\(\d+\))?)$/', $v, $r)) { |
|
| 973 | - $k = str_replace('`', '', trim($r[1])); |
|
| 974 | - $t = strtolower(str_replace('`', '', $r[2])); |
|
| 975 | - if ($k && !isset($keys[$k])) { |
|
| 976 | - $keys[$k] = $t; |
|
| 977 | - } else { |
|
| 978 | - $keys[] = $t; |
|
| 979 | - } |
|
| 980 | - } |
|
| 981 | - } |
|
| 982 | - spip_mysql_free($s); |
|
| 983 | - |
|
| 984 | - return ['field' => $fields, 'key' => $keys]; |
|
| 985 | - } |
|
| 986 | - |
|
| 987 | - $res = spip_mysql_query("SHOW COLUMNS FROM `$nom_table`", $serveur); |
|
| 988 | - if ($res) { |
|
| 989 | - $nfields = []; |
|
| 990 | - $nkeys = []; |
|
| 991 | - while ($val = spip_mysql_fetch($res)) { |
|
| 992 | - $nfields[$val['Field']] = $val['Type']; |
|
| 993 | - if ($val['Null'] == 'NO') { |
|
| 994 | - $nfields[$val['Field']] .= ' NOT NULL'; |
|
| 995 | - } |
|
| 996 | - if ($val['Default'] === '0' || $val['Default']) { |
|
| 997 | - if (preg_match('/[A-Z_]/', $val['Default'])) { |
|
| 998 | - $nfields[$val['Field']] .= ' DEFAULT ' . $val['Default']; |
|
| 999 | - } else { |
|
| 1000 | - $nfields[$val['Field']] .= " DEFAULT '" . $val['Default'] . "'"; |
|
| 1001 | - } |
|
| 1002 | - } |
|
| 1003 | - if ($val['Extra']) { |
|
| 1004 | - $nfields[$val['Field']] .= ' ' . $val['Extra']; |
|
| 1005 | - } |
|
| 1006 | - if ($val['Key'] == 'PRI') { |
|
| 1007 | - $nkeys['PRIMARY KEY'] = $val['Field']; |
|
| 1008 | - } else { |
|
| 1009 | - if ($val['Key'] == 'MUL') { |
|
| 1010 | - $nkeys['KEY ' . $val['Field']] = $val['Field']; |
|
| 1011 | - } else { |
|
| 1012 | - if ($val['Key'] == 'UNI') { |
|
| 1013 | - $nkeys['UNIQUE KEY ' . $val['Field']] = $val['Field']; |
|
| 1014 | - } |
|
| 1015 | - } |
|
| 1016 | - } |
|
| 1017 | - } |
|
| 1018 | - spip_mysql_free($res); |
|
| 1019 | - |
|
| 1020 | - return ['field' => $nfields, 'key' => $nkeys]; |
|
| 1021 | - } |
|
| 1022 | - |
|
| 1023 | - return ''; |
|
| 944 | + $s = spip_mysql_query("SHOW CREATE TABLE `$nom_table`", $serveur, $requeter); |
|
| 945 | + if (!$s) { |
|
| 946 | + return ''; |
|
| 947 | + } |
|
| 948 | + if (!$requeter) { |
|
| 949 | + return $s; |
|
| 950 | + } |
|
| 951 | + |
|
| 952 | + [, $a] = mysqli_fetch_array($s, MYSQLI_NUM); |
|
| 953 | + if (preg_match(_MYSQL_RE_SHOW_TABLE, $a, $r)) { |
|
| 954 | + $desc = $r[1]; |
|
| 955 | + // extraction d'une KEY éventuelle en prenant garde de ne pas |
|
| 956 | + // relever un champ dont le nom contient KEY (ex. ID_WHISKEY) |
|
| 957 | + if (preg_match('/^(.*?),([^,]*\sKEY[ (].*)$/s', $desc, $r)) { |
|
| 958 | + $namedkeys = $r[2]; |
|
| 959 | + $desc = $r[1]; |
|
| 960 | + } else { |
|
| 961 | + $namedkeys = ''; |
|
| 962 | + } |
|
| 963 | + |
|
| 964 | + $fields = []; |
|
| 965 | + foreach (preg_split('/,\s*`/', $desc) as $v) { |
|
| 966 | + preg_match('/^\s*`?([^`]*)`\s*(.*)/', $v, $r); |
|
| 967 | + $fields[strtolower($r[1])] = $r[2]; |
|
| 968 | + } |
|
| 969 | + $keys = []; |
|
| 970 | + |
|
| 971 | + foreach (preg_split('/\)\s*(,|$)/', $namedkeys) as $v) { |
|
| 972 | + if (preg_match('/^\s*([^(]*)\(([^(]*(\(\d+\))?)$/', $v, $r)) { |
|
| 973 | + $k = str_replace('`', '', trim($r[1])); |
|
| 974 | + $t = strtolower(str_replace('`', '', $r[2])); |
|
| 975 | + if ($k && !isset($keys[$k])) { |
|
| 976 | + $keys[$k] = $t; |
|
| 977 | + } else { |
|
| 978 | + $keys[] = $t; |
|
| 979 | + } |
|
| 980 | + } |
|
| 981 | + } |
|
| 982 | + spip_mysql_free($s); |
|
| 983 | + |
|
| 984 | + return ['field' => $fields, 'key' => $keys]; |
|
| 985 | + } |
|
| 986 | + |
|
| 987 | + $res = spip_mysql_query("SHOW COLUMNS FROM `$nom_table`", $serveur); |
|
| 988 | + if ($res) { |
|
| 989 | + $nfields = []; |
|
| 990 | + $nkeys = []; |
|
| 991 | + while ($val = spip_mysql_fetch($res)) { |
|
| 992 | + $nfields[$val['Field']] = $val['Type']; |
|
| 993 | + if ($val['Null'] == 'NO') { |
|
| 994 | + $nfields[$val['Field']] .= ' NOT NULL'; |
|
| 995 | + } |
|
| 996 | + if ($val['Default'] === '0' || $val['Default']) { |
|
| 997 | + if (preg_match('/[A-Z_]/', $val['Default'])) { |
|
| 998 | + $nfields[$val['Field']] .= ' DEFAULT ' . $val['Default']; |
|
| 999 | + } else { |
|
| 1000 | + $nfields[$val['Field']] .= " DEFAULT '" . $val['Default'] . "'"; |
|
| 1001 | + } |
|
| 1002 | + } |
|
| 1003 | + if ($val['Extra']) { |
|
| 1004 | + $nfields[$val['Field']] .= ' ' . $val['Extra']; |
|
| 1005 | + } |
|
| 1006 | + if ($val['Key'] == 'PRI') { |
|
| 1007 | + $nkeys['PRIMARY KEY'] = $val['Field']; |
|
| 1008 | + } else { |
|
| 1009 | + if ($val['Key'] == 'MUL') { |
|
| 1010 | + $nkeys['KEY ' . $val['Field']] = $val['Field']; |
|
| 1011 | + } else { |
|
| 1012 | + if ($val['Key'] == 'UNI') { |
|
| 1013 | + $nkeys['UNIQUE KEY ' . $val['Field']] = $val['Field']; |
|
| 1014 | + } |
|
| 1015 | + } |
|
| 1016 | + } |
|
| 1017 | + } |
|
| 1018 | + spip_mysql_free($res); |
|
| 1019 | + |
|
| 1020 | + return ['field' => $nfields, 'key' => $nkeys]; |
|
| 1021 | + } |
|
| 1022 | + |
|
| 1023 | + return ''; |
|
| 1024 | 1024 | } |
| 1025 | 1025 | |
| 1026 | 1026 | |
@@ -1039,13 +1039,13 @@ discard block |
||
| 1039 | 1039 | * - false Erreur |
| 1040 | 1040 | */ |
| 1041 | 1041 | function spip_mysql_fetch($r, $t = '', $serveur = '', $requeter = true) { |
| 1042 | - if (!$t) { |
|
| 1043 | - $t = \MYSQLI_ASSOC; |
|
| 1044 | - } |
|
| 1045 | - if ($r) { |
|
| 1046 | - return mysqli_fetch_array($r, $t); |
|
| 1047 | - } |
|
| 1048 | - return false; |
|
| 1042 | + if (!$t) { |
|
| 1043 | + $t = \MYSQLI_ASSOC; |
|
| 1044 | + } |
|
| 1045 | + if ($r) { |
|
| 1046 | + return mysqli_fetch_array($r, $t); |
|
| 1047 | + } |
|
| 1048 | + return false; |
|
| 1049 | 1049 | } |
| 1050 | 1050 | |
| 1051 | 1051 | /** |
@@ -1058,10 +1058,10 @@ discard block |
||
| 1058 | 1058 | * @return bool True si déplacement réussi, false sinon. |
| 1059 | 1059 | **/ |
| 1060 | 1060 | function spip_mysql_seek($r, $row_number, $serveur = '', $requeter = true) { |
| 1061 | - if ($r and mysqli_num_rows($r)) { |
|
| 1062 | - return mysqli_data_seek($r, $row_number); |
|
| 1063 | - } |
|
| 1064 | - return false; |
|
| 1061 | + if ($r and mysqli_num_rows($r)) { |
|
| 1062 | + return mysqli_data_seek($r, $row_number); |
|
| 1063 | + } |
|
| 1064 | + return false; |
|
| 1065 | 1065 | } |
| 1066 | 1066 | |
| 1067 | 1067 | |
@@ -1079,26 +1079,26 @@ discard block |
||
| 1079 | 1079 | * - int Nombre de lignes (0 si la requête n'a pas réussie) |
| 1080 | 1080 | **/ |
| 1081 | 1081 | function spip_mysql_countsel( |
| 1082 | - $from = [], |
|
| 1083 | - $where = [], |
|
| 1084 | - $groupby = '', |
|
| 1085 | - $having = [], |
|
| 1086 | - $serveur = '', |
|
| 1087 | - $requeter = true |
|
| 1082 | + $from = [], |
|
| 1083 | + $where = [], |
|
| 1084 | + $groupby = '', |
|
| 1085 | + $having = [], |
|
| 1086 | + $serveur = '', |
|
| 1087 | + $requeter = true |
|
| 1088 | 1088 | ) { |
| 1089 | - $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 1090 | - |
|
| 1091 | - $r = spip_mysql_select("COUNT($c)", $from, $where, '', '', '', $having, $serveur, $requeter); |
|
| 1092 | - if (!$requeter) { |
|
| 1093 | - return $r; |
|
| 1094 | - } |
|
| 1095 | - if (!$r instanceof mysqli_result) { |
|
| 1096 | - return 0; |
|
| 1097 | - } |
|
| 1098 | - [$c] = mysqli_fetch_array($r, MYSQLI_NUM); |
|
| 1099 | - mysqli_free_result($r); |
|
| 1100 | - |
|
| 1101 | - return intval($c); |
|
| 1089 | + $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 1090 | + |
|
| 1091 | + $r = spip_mysql_select("COUNT($c)", $from, $where, '', '', '', $having, $serveur, $requeter); |
|
| 1092 | + if (!$requeter) { |
|
| 1093 | + return $r; |
|
| 1094 | + } |
|
| 1095 | + if (!$r instanceof mysqli_result) { |
|
| 1096 | + return 0; |
|
| 1097 | + } |
|
| 1098 | + [$c] = mysqli_fetch_array($r, MYSQLI_NUM); |
|
| 1099 | + mysqli_free_result($r); |
|
| 1100 | + |
|
| 1101 | + return intval($c); |
|
| 1102 | 1102 | } |
| 1103 | 1103 | |
| 1104 | 1104 | |
@@ -1121,16 +1121,16 @@ discard block |
||
| 1121 | 1121 | * Erreur eventuelle |
| 1122 | 1122 | **/ |
| 1123 | 1123 | function spip_mysql_error($query = '', $serveur = '', $requeter = true) { |
| 1124 | - $link = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]['link']; |
|
| 1125 | - $s = mysqli_error($link); |
|
| 1126 | - if ($s) { |
|
| 1127 | - $trace = debug_backtrace(); |
|
| 1128 | - if ($trace[0]['function'] != 'spip_mysql_error') { |
|
| 1129 | - spip_log("$s - $query - " . sql_error_backtrace(), 'mysql.' . _LOG_ERREUR); |
|
| 1130 | - } |
|
| 1131 | - } |
|
| 1132 | - |
|
| 1133 | - return $s; |
|
| 1124 | + $link = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]['link']; |
|
| 1125 | + $s = mysqli_error($link); |
|
| 1126 | + if ($s) { |
|
| 1127 | + $trace = debug_backtrace(); |
|
| 1128 | + if ($trace[0]['function'] != 'spip_mysql_error') { |
|
| 1129 | + spip_log("$s - $query - " . sql_error_backtrace(), 'mysql.' . _LOG_ERREUR); |
|
| 1130 | + } |
|
| 1131 | + } |
|
| 1132 | + |
|
| 1133 | + return $s; |
|
| 1134 | 1134 | } |
| 1135 | 1135 | |
| 1136 | 1136 | |
@@ -1145,18 +1145,18 @@ discard block |
||
| 1145 | 1145 | * 0, pas d'erreur. Autre, numéro de l'erreur. |
| 1146 | 1146 | **/ |
| 1147 | 1147 | function spip_mysql_errno($serveur = '', $requeter = true) { |
| 1148 | - $link = $GLOBALS['connexions'][$serveur ?: 0]['link']; |
|
| 1149 | - $s = mysqli_errno($link); |
|
| 1150 | - // 2006 MySQL server has gone away |
|
| 1151 | - // 2013 Lost connection to MySQL server during query |
|
| 1152 | - if (in_array($s, [2006, 2013])) { |
|
| 1153 | - define('spip_interdire_cache', true); |
|
| 1154 | - } |
|
| 1155 | - if ($s) { |
|
| 1156 | - spip_log("Erreur mysql $s", _LOG_ERREUR); |
|
| 1157 | - } |
|
| 1158 | - |
|
| 1159 | - return $s; |
|
| 1148 | + $link = $GLOBALS['connexions'][$serveur ?: 0]['link']; |
|
| 1149 | + $s = mysqli_errno($link); |
|
| 1150 | + // 2006 MySQL server has gone away |
|
| 1151 | + // 2013 Lost connection to MySQL server during query |
|
| 1152 | + if (in_array($s, [2006, 2013])) { |
|
| 1153 | + define('spip_interdire_cache', true); |
|
| 1154 | + } |
|
| 1155 | + if ($s) { |
|
| 1156 | + spip_log("Erreur mysql $s", _LOG_ERREUR); |
|
| 1157 | + } |
|
| 1158 | + |
|
| 1159 | + return $s; |
|
| 1160 | 1160 | } |
| 1161 | 1161 | |
| 1162 | 1162 | |
@@ -1170,9 +1170,9 @@ discard block |
||
| 1170 | 1170 | * @return int Nombre de lignes |
| 1171 | 1171 | */ |
| 1172 | 1172 | function spip_mysql_count($r, $serveur = '', $requeter = true) { |
| 1173 | - if ($r) { |
|
| 1174 | - return mysqli_num_rows($r); |
|
| 1175 | - } |
|
| 1173 | + if ($r) { |
|
| 1174 | + return mysqli_num_rows($r); |
|
| 1175 | + } |
|
| 1176 | 1176 | } |
| 1177 | 1177 | |
| 1178 | 1178 | |
@@ -1188,11 +1188,11 @@ discard block |
||
| 1188 | 1188 | * @return bool True si réussi |
| 1189 | 1189 | */ |
| 1190 | 1190 | function spip_mysql_free($r, $serveur = '', $requeter = true) { |
| 1191 | - if ($r instanceof mysqli_result) { |
|
| 1192 | - mysqli_free_result($r); |
|
| 1193 | - return true; |
|
| 1194 | - } |
|
| 1195 | - return false; |
|
| 1191 | + if ($r instanceof mysqli_result) { |
|
| 1192 | + mysqli_free_result($r); |
|
| 1193 | + return true; |
|
| 1194 | + } |
|
| 1195 | + return false; |
|
| 1196 | 1196 | } |
| 1197 | 1197 | |
| 1198 | 1198 | |
@@ -1220,59 +1220,59 @@ discard block |
||
| 1220 | 1220 | **/ |
| 1221 | 1221 | function spip_mysql_insert($table, $champs, $valeurs, $desc = [], $serveur = '', $requeter = true) { |
| 1222 | 1222 | |
| 1223 | - $e = null; |
|
| 1224 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1225 | - $link = $connexion['link']; |
|
| 1226 | - $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 1227 | - |
|
| 1228 | - // remplacer les emoji (que mysql ne sait pas gérer) en 💩 |
|
| 1229 | - if ( |
|
| 1230 | - defined('_MYSQL_NOPLANES') |
|
| 1231 | - and _MYSQL_NOPLANES |
|
| 1232 | - and !empty($GLOBALS['meta']['charset_sql_connexion']) |
|
| 1233 | - and $GLOBALS['meta']['charset_sql_connexion'] == 'utf8' |
|
| 1234 | - ) { |
|
| 1235 | - include_spip('inc/charsets'); |
|
| 1236 | - $valeurs = utf8_noplanes($valeurs); |
|
| 1237 | - } |
|
| 1238 | - |
|
| 1239 | - $query = "INSERT INTO $table $champs VALUES $valeurs"; |
|
| 1240 | - if (!$requeter) { |
|
| 1241 | - return $query; |
|
| 1242 | - } |
|
| 1243 | - |
|
| 1244 | - if (isset($_GET['var_profile'])) { |
|
| 1245 | - include_spip('public/tracer'); |
|
| 1246 | - $t = trace_query_start(); |
|
| 1247 | - $e = ''; |
|
| 1248 | - } else { |
|
| 1249 | - $t = 0; |
|
| 1250 | - } |
|
| 1251 | - |
|
| 1252 | - $connexion['last'] = $query; |
|
| 1253 | - #spip_log($query, 'mysql.'._LOG_DEBUG); |
|
| 1254 | - $r = false; |
|
| 1255 | - $insert = false; |
|
| 1256 | - try { |
|
| 1257 | - $insert = mysqli_query($link, $query); |
|
| 1258 | - } catch (\mysqli_sql_exception $e) { |
|
| 1259 | - spip_log('mysqli_sql_exception: ' . $e->getMessage(), 'mysql.' . _LOG_DEBUG); |
|
| 1260 | - // TODO: utiliser l’exception ensuite plutôt que les appels à spip_mysql_errno() |
|
| 1261 | - // mais il faut pour php < 8.1 forcer les exeptions via mysqli_report(). |
|
| 1262 | - } |
|
| 1263 | - if ($insert) { |
|
| 1264 | - $r = mysqli_insert_id($link); |
|
| 1265 | - } else { |
|
| 1266 | - // Log de l'erreur eventuelle |
|
| 1267 | - if ($e = spip_mysql_errno($serveur)) { |
|
| 1268 | - // et du fautif |
|
| 1269 | - $e .= spip_mysql_error($query, $serveur); |
|
| 1270 | - } |
|
| 1271 | - } |
|
| 1272 | - |
|
| 1273 | - return $t ? trace_query_end($query, $t, $r, $e, $serveur) : $r; |
|
| 1274 | - |
|
| 1275 | - // return $r ? $r : (($r===0) ? -1 : 0); pb avec le multi-base. |
|
| 1223 | + $e = null; |
|
| 1224 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1225 | + $link = $connexion['link']; |
|
| 1226 | + $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 1227 | + |
|
| 1228 | + // remplacer les emoji (que mysql ne sait pas gérer) en 💩 |
|
| 1229 | + if ( |
|
| 1230 | + defined('_MYSQL_NOPLANES') |
|
| 1231 | + and _MYSQL_NOPLANES |
|
| 1232 | + and !empty($GLOBALS['meta']['charset_sql_connexion']) |
|
| 1233 | + and $GLOBALS['meta']['charset_sql_connexion'] == 'utf8' |
|
| 1234 | + ) { |
|
| 1235 | + include_spip('inc/charsets'); |
|
| 1236 | + $valeurs = utf8_noplanes($valeurs); |
|
| 1237 | + } |
|
| 1238 | + |
|
| 1239 | + $query = "INSERT INTO $table $champs VALUES $valeurs"; |
|
| 1240 | + if (!$requeter) { |
|
| 1241 | + return $query; |
|
| 1242 | + } |
|
| 1243 | + |
|
| 1244 | + if (isset($_GET['var_profile'])) { |
|
| 1245 | + include_spip('public/tracer'); |
|
| 1246 | + $t = trace_query_start(); |
|
| 1247 | + $e = ''; |
|
| 1248 | + } else { |
|
| 1249 | + $t = 0; |
|
| 1250 | + } |
|
| 1251 | + |
|
| 1252 | + $connexion['last'] = $query; |
|
| 1253 | + #spip_log($query, 'mysql.'._LOG_DEBUG); |
|
| 1254 | + $r = false; |
|
| 1255 | + $insert = false; |
|
| 1256 | + try { |
|
| 1257 | + $insert = mysqli_query($link, $query); |
|
| 1258 | + } catch (\mysqli_sql_exception $e) { |
|
| 1259 | + spip_log('mysqli_sql_exception: ' . $e->getMessage(), 'mysql.' . _LOG_DEBUG); |
|
| 1260 | + // TODO: utiliser l’exception ensuite plutôt que les appels à spip_mysql_errno() |
|
| 1261 | + // mais il faut pour php < 8.1 forcer les exeptions via mysqli_report(). |
|
| 1262 | + } |
|
| 1263 | + if ($insert) { |
|
| 1264 | + $r = mysqli_insert_id($link); |
|
| 1265 | + } else { |
|
| 1266 | + // Log de l'erreur eventuelle |
|
| 1267 | + if ($e = spip_mysql_errno($serveur)) { |
|
| 1268 | + // et du fautif |
|
| 1269 | + $e .= spip_mysql_error($query, $serveur); |
|
| 1270 | + } |
|
| 1271 | + } |
|
| 1272 | + |
|
| 1273 | + return $t ? trace_query_end($query, $t, $r, $e, $serveur) : $r; |
|
| 1274 | + |
|
| 1275 | + // return $r ? $r : (($r===0) ? -1 : 0); pb avec le multi-base. |
|
| 1276 | 1276 | } |
| 1277 | 1277 | |
| 1278 | 1278 | /** |
@@ -1297,26 +1297,26 @@ discard block |
||
| 1297 | 1297 | **/ |
| 1298 | 1298 | function spip_mysql_insertq($table, $couples = [], $desc = [], $serveur = '', $requeter = true) { |
| 1299 | 1299 | |
| 1300 | - if (!$desc) { |
|
| 1301 | - $desc = description_table($table, $serveur); |
|
| 1302 | - } |
|
| 1303 | - if (!$desc) { |
|
| 1304 | - $couples = []; |
|
| 1305 | - } |
|
| 1306 | - $fields = $desc['field'] ?? []; |
|
| 1307 | - |
|
| 1308 | - foreach ($couples as $champ => $val) { |
|
| 1309 | - $couples[$champ] = spip_mysql_cite($val, $fields[$champ]); |
|
| 1310 | - } |
|
| 1311 | - |
|
| 1312 | - return spip_mysql_insert( |
|
| 1313 | - $table, |
|
| 1314 | - '(' . join(',', array_keys($couples)) . ')', |
|
| 1315 | - '(' . join(',', $couples) . ')', |
|
| 1316 | - $desc, |
|
| 1317 | - $serveur, |
|
| 1318 | - $requeter |
|
| 1319 | - ); |
|
| 1300 | + if (!$desc) { |
|
| 1301 | + $desc = description_table($table, $serveur); |
|
| 1302 | + } |
|
| 1303 | + if (!$desc) { |
|
| 1304 | + $couples = []; |
|
| 1305 | + } |
|
| 1306 | + $fields = $desc['field'] ?? []; |
|
| 1307 | + |
|
| 1308 | + foreach ($couples as $champ => $val) { |
|
| 1309 | + $couples[$champ] = spip_mysql_cite($val, $fields[$champ]); |
|
| 1310 | + } |
|
| 1311 | + |
|
| 1312 | + return spip_mysql_insert( |
|
| 1313 | + $table, |
|
| 1314 | + '(' . join(',', array_keys($couples)) . ')', |
|
| 1315 | + '(' . join(',', $couples) . ')', |
|
| 1316 | + $desc, |
|
| 1317 | + $serveur, |
|
| 1318 | + $requeter |
|
| 1319 | + ); |
|
| 1320 | 1320 | } |
| 1321 | 1321 | |
| 1322 | 1322 | |
@@ -1341,34 +1341,34 @@ discard block |
||
| 1341 | 1341 | **/ |
| 1342 | 1342 | function spip_mysql_insertq_multi($table, $tab_couples = [], $desc = [], $serveur = '', $requeter = true) { |
| 1343 | 1343 | |
| 1344 | - if (!$desc) { |
|
| 1345 | - $desc = description_table($table, $serveur); |
|
| 1346 | - } |
|
| 1347 | - if (!$desc) { |
|
| 1348 | - $tab_couples = []; |
|
| 1349 | - } |
|
| 1350 | - $fields = $desc['field'] ?? []; |
|
| 1351 | - |
|
| 1352 | - $cles = '(' . join(',', array_keys(reset($tab_couples))) . ')'; |
|
| 1353 | - $valeurs = []; |
|
| 1354 | - $r = false; |
|
| 1355 | - |
|
| 1356 | - // Quoter et Inserer par groupes de 100 max pour eviter un debordement de pile |
|
| 1357 | - foreach ($tab_couples as $couples) { |
|
| 1358 | - foreach ($couples as $champ => $val) { |
|
| 1359 | - $couples[$champ] = spip_mysql_cite($val, $fields[$champ]); |
|
| 1360 | - } |
|
| 1361 | - $valeurs[] = '(' . join(',', $couples) . ')'; |
|
| 1362 | - if (count($valeurs) >= 100) { |
|
| 1363 | - $r = spip_mysql_insert($table, $cles, join(', ', $valeurs), $desc, $serveur, $requeter); |
|
| 1364 | - $valeurs = []; |
|
| 1365 | - } |
|
| 1366 | - } |
|
| 1367 | - if (count($valeurs)) { |
|
| 1368 | - $r = spip_mysql_insert($table, $cles, join(', ', $valeurs), $desc, $serveur, $requeter); |
|
| 1369 | - } |
|
| 1370 | - |
|
| 1371 | - return $r; // dans le cas d'une table auto_increment, le dernier insert_id |
|
| 1344 | + if (!$desc) { |
|
| 1345 | + $desc = description_table($table, $serveur); |
|
| 1346 | + } |
|
| 1347 | + if (!$desc) { |
|
| 1348 | + $tab_couples = []; |
|
| 1349 | + } |
|
| 1350 | + $fields = $desc['field'] ?? []; |
|
| 1351 | + |
|
| 1352 | + $cles = '(' . join(',', array_keys(reset($tab_couples))) . ')'; |
|
| 1353 | + $valeurs = []; |
|
| 1354 | + $r = false; |
|
| 1355 | + |
|
| 1356 | + // Quoter et Inserer par groupes de 100 max pour eviter un debordement de pile |
|
| 1357 | + foreach ($tab_couples as $couples) { |
|
| 1358 | + foreach ($couples as $champ => $val) { |
|
| 1359 | + $couples[$champ] = spip_mysql_cite($val, $fields[$champ]); |
|
| 1360 | + } |
|
| 1361 | + $valeurs[] = '(' . join(',', $couples) . ')'; |
|
| 1362 | + if (count($valeurs) >= 100) { |
|
| 1363 | + $r = spip_mysql_insert($table, $cles, join(', ', $valeurs), $desc, $serveur, $requeter); |
|
| 1364 | + $valeurs = []; |
|
| 1365 | + } |
|
| 1366 | + } |
|
| 1367 | + if (count($valeurs)) { |
|
| 1368 | + $r = spip_mysql_insert($table, $cles, join(', ', $valeurs), $desc, $serveur, $requeter); |
|
| 1369 | + } |
|
| 1370 | + |
|
| 1371 | + return $r; // dans le cas d'une table auto_increment, le dernier insert_id |
|
| 1372 | 1372 | } |
| 1373 | 1373 | |
| 1374 | 1374 | /** |
@@ -1393,20 +1393,20 @@ discard block |
||
| 1393 | 1393 | * - array Tableau décrivant la requête et son temps d'exécution si var_profile est actif |
| 1394 | 1394 | */ |
| 1395 | 1395 | function spip_mysql_update($table, $champs, $where = '', $desc = [], $serveur = '', $requeter = true) { |
| 1396 | - $set = []; |
|
| 1397 | - foreach ($champs as $champ => $val) { |
|
| 1398 | - $set[] = $champ . "=$val"; |
|
| 1399 | - } |
|
| 1400 | - if (!empty($set)) { |
|
| 1401 | - return spip_mysql_query( |
|
| 1402 | - calculer_mysql_expression('UPDATE', $table, ',') |
|
| 1403 | - . calculer_mysql_expression('SET', $set, ',') |
|
| 1404 | - . calculer_mysql_expression('WHERE', $where), |
|
| 1405 | - $serveur, |
|
| 1406 | - $requeter |
|
| 1407 | - ); |
|
| 1408 | - } |
|
| 1409 | - return false; |
|
| 1396 | + $set = []; |
|
| 1397 | + foreach ($champs as $champ => $val) { |
|
| 1398 | + $set[] = $champ . "=$val"; |
|
| 1399 | + } |
|
| 1400 | + if (!empty($set)) { |
|
| 1401 | + return spip_mysql_query( |
|
| 1402 | + calculer_mysql_expression('UPDATE', $table, ',') |
|
| 1403 | + . calculer_mysql_expression('SET', $set, ',') |
|
| 1404 | + . calculer_mysql_expression('WHERE', $where), |
|
| 1405 | + $serveur, |
|
| 1406 | + $requeter |
|
| 1407 | + ); |
|
| 1408 | + } |
|
| 1409 | + return false; |
|
| 1410 | 1410 | } |
| 1411 | 1411 | |
| 1412 | 1412 | /** |
@@ -1439,29 +1439,29 @@ discard block |
||
| 1439 | 1439 | */ |
| 1440 | 1440 | function spip_mysql_updateq($table, $champs, $where = '', $desc = [], $serveur = '', $requeter = true) { |
| 1441 | 1441 | |
| 1442 | - if (!$champs) { |
|
| 1443 | - return; |
|
| 1444 | - } |
|
| 1445 | - if (!$desc) { |
|
| 1446 | - $desc = description_table($table, $serveur); |
|
| 1447 | - } |
|
| 1448 | - if (!$desc) { |
|
| 1449 | - $champs = []; |
|
| 1450 | - } else { |
|
| 1451 | - $fields = $desc['field']; |
|
| 1452 | - } |
|
| 1453 | - $set = []; |
|
| 1454 | - foreach ($champs as $champ => $val) { |
|
| 1455 | - $set[] = $champ . '=' . spip_mysql_cite($val, @$fields[$champ]); |
|
| 1456 | - } |
|
| 1457 | - |
|
| 1458 | - return spip_mysql_query( |
|
| 1459 | - calculer_mysql_expression('UPDATE', $table, ',') |
|
| 1460 | - . calculer_mysql_expression('SET', $set, ',') |
|
| 1461 | - . calculer_mysql_expression('WHERE', $where), |
|
| 1462 | - $serveur, |
|
| 1463 | - $requeter |
|
| 1464 | - ); |
|
| 1442 | + if (!$champs) { |
|
| 1443 | + return; |
|
| 1444 | + } |
|
| 1445 | + if (!$desc) { |
|
| 1446 | + $desc = description_table($table, $serveur); |
|
| 1447 | + } |
|
| 1448 | + if (!$desc) { |
|
| 1449 | + $champs = []; |
|
| 1450 | + } else { |
|
| 1451 | + $fields = $desc['field']; |
|
| 1452 | + } |
|
| 1453 | + $set = []; |
|
| 1454 | + foreach ($champs as $champ => $val) { |
|
| 1455 | + $set[] = $champ . '=' . spip_mysql_cite($val, @$fields[$champ]); |
|
| 1456 | + } |
|
| 1457 | + |
|
| 1458 | + return spip_mysql_query( |
|
| 1459 | + calculer_mysql_expression('UPDATE', $table, ',') |
|
| 1460 | + . calculer_mysql_expression('SET', $set, ',') |
|
| 1461 | + . calculer_mysql_expression('WHERE', $where), |
|
| 1462 | + $serveur, |
|
| 1463 | + $requeter |
|
| 1464 | + ); |
|
| 1465 | 1465 | } |
| 1466 | 1466 | |
| 1467 | 1467 | /** |
@@ -1477,22 +1477,22 @@ discard block |
||
| 1477 | 1477 | * - false en cas d'erreur. |
| 1478 | 1478 | **/ |
| 1479 | 1479 | function spip_mysql_delete($table, $where = '', $serveur = '', $requeter = true) { |
| 1480 | - $res = spip_mysql_query( |
|
| 1481 | - calculer_mysql_expression('DELETE FROM', $table, ',') |
|
| 1482 | - . calculer_mysql_expression('WHERE', $where), |
|
| 1483 | - $serveur, |
|
| 1484 | - $requeter |
|
| 1485 | - ); |
|
| 1486 | - if (!$requeter) { |
|
| 1487 | - return $res; |
|
| 1488 | - } |
|
| 1489 | - if ($res) { |
|
| 1490 | - $link = _mysql_link($serveur); |
|
| 1491 | - |
|
| 1492 | - return mysqli_affected_rows($link); |
|
| 1493 | - } else { |
|
| 1494 | - return false; |
|
| 1495 | - } |
|
| 1480 | + $res = spip_mysql_query( |
|
| 1481 | + calculer_mysql_expression('DELETE FROM', $table, ',') |
|
| 1482 | + . calculer_mysql_expression('WHERE', $where), |
|
| 1483 | + $serveur, |
|
| 1484 | + $requeter |
|
| 1485 | + ); |
|
| 1486 | + if (!$requeter) { |
|
| 1487 | + return $res; |
|
| 1488 | + } |
|
| 1489 | + if ($res) { |
|
| 1490 | + $link = _mysql_link($serveur); |
|
| 1491 | + |
|
| 1492 | + return mysqli_affected_rows($link); |
|
| 1493 | + } else { |
|
| 1494 | + return false; |
|
| 1495 | + } |
|
| 1496 | 1496 | } |
| 1497 | 1497 | |
| 1498 | 1498 | |
@@ -1521,10 +1521,10 @@ discard block |
||
| 1521 | 1521 | * - false en cas d'erreur. |
| 1522 | 1522 | **/ |
| 1523 | 1523 | function spip_mysql_replace($table, $couples, $desc = [], $serveur = '', $requeter = true) { |
| 1524 | - return spip_mysql_query("REPLACE $table (" . join(',', array_keys($couples)) . ') VALUES (' . join( |
|
| 1525 | - ',', |
|
| 1526 | - array_map('_q', $couples) |
|
| 1527 | - ) . ')', $serveur, $requeter); |
|
| 1524 | + return spip_mysql_query("REPLACE $table (" . join(',', array_keys($couples)) . ') VALUES (' . join( |
|
| 1525 | + ',', |
|
| 1526 | + array_map('_q', $couples) |
|
| 1527 | + ) . ')', $serveur, $requeter); |
|
| 1528 | 1528 | } |
| 1529 | 1529 | |
| 1530 | 1530 | |
@@ -1553,14 +1553,14 @@ discard block |
||
| 1553 | 1553 | * - false en cas d'erreur. |
| 1554 | 1554 | **/ |
| 1555 | 1555 | function spip_mysql_replace_multi($table, $tab_couples, $desc = [], $serveur = '', $requeter = true) { |
| 1556 | - $cles = '(' . join(',', array_keys($tab_couples[0])) . ')'; |
|
| 1557 | - $valeurs = []; |
|
| 1558 | - foreach ($tab_couples as $couples) { |
|
| 1559 | - $valeurs[] = '(' . join(',', array_map('_q', $couples)) . ')'; |
|
| 1560 | - } |
|
| 1561 | - $valeurs = implode(', ', $valeurs); |
|
| 1562 | - |
|
| 1563 | - return spip_mysql_query("REPLACE $table $cles VALUES $valeurs", $serveur, $requeter); |
|
| 1556 | + $cles = '(' . join(',', array_keys($tab_couples[0])) . ')'; |
|
| 1557 | + $valeurs = []; |
|
| 1558 | + foreach ($tab_couples as $couples) { |
|
| 1559 | + $valeurs[] = '(' . join(',', array_map('_q', $couples)) . ')'; |
|
| 1560 | + } |
|
| 1561 | + $valeurs = implode(', ', $valeurs); |
|
| 1562 | + |
|
| 1563 | + return spip_mysql_query("REPLACE $table $cles VALUES $valeurs", $serveur, $requeter); |
|
| 1564 | 1564 | } |
| 1565 | 1565 | |
| 1566 | 1566 | |
@@ -1575,32 +1575,32 @@ discard block |
||
| 1575 | 1575 | * @return string texte de sélection pour la requête |
| 1576 | 1576 | */ |
| 1577 | 1577 | function spip_mysql_multi($objet, $lang) { |
| 1578 | - $lengthlang = strlen("[$lang]"); |
|
| 1579 | - $posmulti = 'INSTR(' . $objet . ", '<multi>')"; |
|
| 1580 | - $posfinmulti = 'INSTR(' . $objet . ", '</multi>')"; |
|
| 1581 | - $debutchaine = 'LEFT(' . $objet . ", $posmulti-1)"; |
|
| 1582 | - $finchaine = 'RIGHT(' . $objet . ', CHAR_LENGTH(' . $objet . ") -(7+$posfinmulti))"; |
|
| 1583 | - $chainemulti = 'TRIM(SUBSTRING(' . $objet . ", $posmulti+7, $posfinmulti -(7+$posmulti)))"; |
|
| 1584 | - $poslang = "INSTR($chainemulti,'[" . $lang . "]')"; |
|
| 1585 | - $poslang = "IF($poslang=0,INSTR($chainemulti,']')+1,$poslang+$lengthlang)"; |
|
| 1586 | - $chainelang = 'TRIM(SUBSTRING(' . $objet . ", $posmulti+7+$poslang-1,$posfinmulti -($posmulti+7+$poslang-1) ))"; |
|
| 1587 | - $posfinlang = 'INSTR(' . $chainelang . ", '[')"; |
|
| 1588 | - $chainelang = "IF($posfinlang>0,LEFT($chainelang,$posfinlang-1),$chainelang)"; |
|
| 1589 | - //$chainelang = "LEFT($chainelang,$posfinlang-1)"; |
|
| 1590 | - $retour = "(TRIM(IF($posmulti = 0 , " . |
|
| 1591 | - ' TRIM(' . $objet . '), ' . |
|
| 1592 | - ' CONCAT( ' . |
|
| 1593 | - " $debutchaine, " . |
|
| 1594 | - ' IF( ' . |
|
| 1595 | - " $poslang = 0, " . |
|
| 1596 | - " $chainemulti, " . |
|
| 1597 | - " $chainelang" . |
|
| 1598 | - ' ), ' . |
|
| 1599 | - " $finchaine" . |
|
| 1600 | - ' ) ' . |
|
| 1601 | - '))) AS multi'; |
|
| 1602 | - |
|
| 1603 | - return $retour; |
|
| 1578 | + $lengthlang = strlen("[$lang]"); |
|
| 1579 | + $posmulti = 'INSTR(' . $objet . ", '<multi>')"; |
|
| 1580 | + $posfinmulti = 'INSTR(' . $objet . ", '</multi>')"; |
|
| 1581 | + $debutchaine = 'LEFT(' . $objet . ", $posmulti-1)"; |
|
| 1582 | + $finchaine = 'RIGHT(' . $objet . ', CHAR_LENGTH(' . $objet . ") -(7+$posfinmulti))"; |
|
| 1583 | + $chainemulti = 'TRIM(SUBSTRING(' . $objet . ", $posmulti+7, $posfinmulti -(7+$posmulti)))"; |
|
| 1584 | + $poslang = "INSTR($chainemulti,'[" . $lang . "]')"; |
|
| 1585 | + $poslang = "IF($poslang=0,INSTR($chainemulti,']')+1,$poslang+$lengthlang)"; |
|
| 1586 | + $chainelang = 'TRIM(SUBSTRING(' . $objet . ", $posmulti+7+$poslang-1,$posfinmulti -($posmulti+7+$poslang-1) ))"; |
|
| 1587 | + $posfinlang = 'INSTR(' . $chainelang . ", '[')"; |
|
| 1588 | + $chainelang = "IF($posfinlang>0,LEFT($chainelang,$posfinlang-1),$chainelang)"; |
|
| 1589 | + //$chainelang = "LEFT($chainelang,$posfinlang-1)"; |
|
| 1590 | + $retour = "(TRIM(IF($posmulti = 0 , " . |
|
| 1591 | + ' TRIM(' . $objet . '), ' . |
|
| 1592 | + ' CONCAT( ' . |
|
| 1593 | + " $debutchaine, " . |
|
| 1594 | + ' IF( ' . |
|
| 1595 | + " $poslang = 0, " . |
|
| 1596 | + " $chainemulti, " . |
|
| 1597 | + " $chainelang" . |
|
| 1598 | + ' ), ' . |
|
| 1599 | + " $finchaine" . |
|
| 1600 | + ' ) ' . |
|
| 1601 | + '))) AS multi'; |
|
| 1602 | + |
|
| 1603 | + return $retour; |
|
| 1604 | 1604 | } |
| 1605 | 1605 | |
| 1606 | 1606 | /** |
@@ -1614,7 +1614,7 @@ discard block |
||
| 1614 | 1614 | * Valeur hexadécimale pour MySQL |
| 1615 | 1615 | **/ |
| 1616 | 1616 | function spip_mysql_hex($v) { |
| 1617 | - return '0x' . $v; |
|
| 1617 | + return '0x' . $v; |
|
| 1618 | 1618 | } |
| 1619 | 1619 | |
| 1620 | 1620 | /** |
@@ -1630,15 +1630,15 @@ discard block |
||
| 1630 | 1630 | * Donnée prête à être utilisée par le gestionnaire SQL |
| 1631 | 1631 | */ |
| 1632 | 1632 | function spip_mysql_quote($v, $type = '') { |
| 1633 | - if (!is_array($v)) { |
|
| 1634 | - return spip_mysql_cite($v, $type); |
|
| 1635 | - } |
|
| 1636 | - |
|
| 1637 | - // si c'est un tableau, le parcourir en propageant le type |
|
| 1638 | - foreach ($v as $k => $r) { |
|
| 1639 | - $v[$k] = spip_mysql_quote($r, $type); |
|
| 1640 | - } |
|
| 1641 | - return implode(',', $v); |
|
| 1633 | + if (!is_array($v)) { |
|
| 1634 | + return spip_mysql_cite($v, $type); |
|
| 1635 | + } |
|
| 1636 | + |
|
| 1637 | + // si c'est un tableau, le parcourir en propageant le type |
|
| 1638 | + foreach ($v as $k => $r) { |
|
| 1639 | + $v[$k] = spip_mysql_quote($r, $type); |
|
| 1640 | + } |
|
| 1641 | + return implode(',', $v); |
|
| 1642 | 1642 | } |
| 1643 | 1643 | |
| 1644 | 1644 | /** |
@@ -1654,18 +1654,18 @@ discard block |
||
| 1654 | 1654 | * Expression SQL |
| 1655 | 1655 | **/ |
| 1656 | 1656 | function spip_mysql_date_proche($champ, $interval, $unite) { |
| 1657 | - $use_now = ( ($champ === 'maj' or strpos($champ, '.maj')) ? true : false ); |
|
| 1658 | - return '(' |
|
| 1659 | - . $champ |
|
| 1660 | - . (($interval <= 0) ? '>' : '<') |
|
| 1661 | - . (($interval <= 0) ? 'DATE_SUB' : 'DATE_ADD') |
|
| 1662 | - . '(' |
|
| 1663 | - . ($use_now ? 'NOW()' : sql_quote(date('Y-m-d H:i:s'))) |
|
| 1664 | - . ', INTERVAL ' |
|
| 1665 | - . (($interval > 0) ? $interval : (0 - $interval)) |
|
| 1666 | - . ' ' |
|
| 1667 | - . $unite |
|
| 1668 | - . '))'; |
|
| 1657 | + $use_now = ( ($champ === 'maj' or strpos($champ, '.maj')) ? true : false ); |
|
| 1658 | + return '(' |
|
| 1659 | + . $champ |
|
| 1660 | + . (($interval <= 0) ? '>' : '<') |
|
| 1661 | + . (($interval <= 0) ? 'DATE_SUB' : 'DATE_ADD') |
|
| 1662 | + . '(' |
|
| 1663 | + . ($use_now ? 'NOW()' : sql_quote(date('Y-m-d H:i:s'))) |
|
| 1664 | + . ', INTERVAL ' |
|
| 1665 | + . (($interval > 0) ? $interval : (0 - $interval)) |
|
| 1666 | + . ' ' |
|
| 1667 | + . $unite |
|
| 1668 | + . '))'; |
|
| 1669 | 1669 | } |
| 1670 | 1670 | |
| 1671 | 1671 | |
@@ -1689,7 +1689,7 @@ discard block |
||
| 1689 | 1689 | * Expression de requête SQL |
| 1690 | 1690 | **/ |
| 1691 | 1691 | function spip_mysql_in($val, $valeurs, $not = '', $serveur = '', $requeter = true) { |
| 1692 | - return "($val $not IN ($valeurs))"; |
|
| 1692 | + return "($val $not IN ($valeurs))"; |
|
| 1693 | 1693 | } |
| 1694 | 1694 | |
| 1695 | 1695 | |
@@ -1701,39 +1701,39 @@ discard block |
||
| 1701 | 1701 | * @return string|number texte ou nombre échappé |
| 1702 | 1702 | */ |
| 1703 | 1703 | function spip_mysql_cite($v, $type) { |
| 1704 | - if (!$type) { |
|
| 1705 | - if (is_bool($v)) { |
|
| 1706 | - return strval(intval($v)); |
|
| 1707 | - } elseif (is_numeric($v)) { |
|
| 1708 | - return strval($v); |
|
| 1709 | - } elseif ($v === null) { |
|
| 1710 | - return "''"; |
|
| 1711 | - } |
|
| 1712 | - return "'" . addslashes($v) . "'"; |
|
| 1713 | - } |
|
| 1714 | - |
|
| 1715 | - if ($v === null) { |
|
| 1716 | - if (stripos($type, 'NOT NULL') === false) { |
|
| 1717 | - // null php se traduit en NULL SQL |
|
| 1718 | - return 'NULL'; |
|
| 1719 | - } else { |
|
| 1720 | - return "''"; |
|
| 1721 | - } |
|
| 1722 | - } elseif (sql_test_date($type) and preg_match('/^\w+\(/', $v)) { |
|
| 1723 | - return $v; |
|
| 1724 | - } elseif (sql_test_int($type)) { |
|
| 1725 | - if ( |
|
| 1726 | - is_numeric($v) |
|
| 1727 | - or ($v and ctype_xdigit(substr($v, 2)) and $v[0] === '0' and $v[1] === 'x') |
|
| 1728 | - ) { |
|
| 1729 | - return $v; |
|
| 1730 | - } else { |
|
| 1731 | - // si pas numerique, forcer le intval |
|
| 1732 | - return intval($v); |
|
| 1733 | - } |
|
| 1734 | - } |
|
| 1735 | - |
|
| 1736 | - return ("'" . addslashes($v) . "'"); |
|
| 1704 | + if (!$type) { |
|
| 1705 | + if (is_bool($v)) { |
|
| 1706 | + return strval(intval($v)); |
|
| 1707 | + } elseif (is_numeric($v)) { |
|
| 1708 | + return strval($v); |
|
| 1709 | + } elseif ($v === null) { |
|
| 1710 | + return "''"; |
|
| 1711 | + } |
|
| 1712 | + return "'" . addslashes($v) . "'"; |
|
| 1713 | + } |
|
| 1714 | + |
|
| 1715 | + if ($v === null) { |
|
| 1716 | + if (stripos($type, 'NOT NULL') === false) { |
|
| 1717 | + // null php se traduit en NULL SQL |
|
| 1718 | + return 'NULL'; |
|
| 1719 | + } else { |
|
| 1720 | + return "''"; |
|
| 1721 | + } |
|
| 1722 | + } elseif (sql_test_date($type) and preg_match('/^\w+\(/', $v)) { |
|
| 1723 | + return $v; |
|
| 1724 | + } elseif (sql_test_int($type)) { |
|
| 1725 | + if ( |
|
| 1726 | + is_numeric($v) |
|
| 1727 | + or ($v and ctype_xdigit(substr($v, 2)) and $v[0] === '0' and $v[1] === 'x') |
|
| 1728 | + ) { |
|
| 1729 | + return $v; |
|
| 1730 | + } else { |
|
| 1731 | + // si pas numerique, forcer le intval |
|
| 1732 | + return intval($v); |
|
| 1733 | + } |
|
| 1734 | + } |
|
| 1735 | + |
|
| 1736 | + return ("'" . addslashes($v) . "'"); |
|
| 1737 | 1737 | } |
| 1738 | 1738 | |
| 1739 | 1739 | /** |
@@ -1743,7 +1743,7 @@ discard block |
||
| 1743 | 1743 | * True si on a les fonctions, false sinon |
| 1744 | 1744 | */ |
| 1745 | 1745 | function spip_versions_mysql() { |
| 1746 | - return function_exists('mysqli_query'); |
|
| 1746 | + return function_exists('mysqli_query'); |
|
| 1747 | 1747 | } |
| 1748 | 1748 | |
| 1749 | 1749 | |
@@ -1756,20 +1756,20 @@ discard block |
||
| 1756 | 1756 | * - chaîne : code compilé pour le faire désactiver par SPIP sinon |
| 1757 | 1757 | */ |
| 1758 | 1758 | function test_rappel_nom_base_mysql($server_db) { |
| 1759 | - $GLOBALS['mysql_rappel_nom_base'] = true; |
|
| 1760 | - sql_delete('spip_meta', "nom='mysql_rappel_nom_base'", $server_db); |
|
| 1761 | - $ok = spip_query("INSERT INTO spip_meta (nom,valeur) VALUES ('mysql_rappel_nom_base', 'test')", $server_db); |
|
| 1759 | + $GLOBALS['mysql_rappel_nom_base'] = true; |
|
| 1760 | + sql_delete('spip_meta', "nom='mysql_rappel_nom_base'", $server_db); |
|
| 1761 | + $ok = spip_query("INSERT INTO spip_meta (nom,valeur) VALUES ('mysql_rappel_nom_base', 'test')", $server_db); |
|
| 1762 | 1762 | |
| 1763 | - if ($ok) { |
|
| 1764 | - sql_delete('spip_meta', "nom='mysql_rappel_nom_base'", $server_db); |
|
| 1763 | + if ($ok) { |
|
| 1764 | + sql_delete('spip_meta', "nom='mysql_rappel_nom_base'", $server_db); |
|
| 1765 | 1765 | |
| 1766 | - return ''; |
|
| 1767 | - } else { |
|
| 1768 | - $GLOBALS['mysql_rappel_nom_base'] = false; |
|
| 1766 | + return ''; |
|
| 1767 | + } else { |
|
| 1768 | + $GLOBALS['mysql_rappel_nom_base'] = false; |
|
| 1769 | 1769 | |
| 1770 | - return "\$GLOBALS['mysql_rappel_nom_base'] = false; " . |
|
| 1771 | - "/* echec de test_rappel_nom_base_mysql a l'installation. */\n"; |
|
| 1772 | - } |
|
| 1770 | + return "\$GLOBALS['mysql_rappel_nom_base'] = false; " . |
|
| 1771 | + "/* echec de test_rappel_nom_base_mysql a l'installation. */\n"; |
|
| 1772 | + } |
|
| 1773 | 1773 | } |
| 1774 | 1774 | |
| 1775 | 1775 | /** |
@@ -1783,13 +1783,13 @@ discard block |
||
| 1783 | 1783 | * - chaîne : code compilé pour l'indiquer le résultat du test à SPIP |
| 1784 | 1784 | */ |
| 1785 | 1785 | function test_sql_mode_mysql($server_db) { |
| 1786 | - $res = sql_select('version() as v', '', '', '', '', '', '', $server_db); |
|
| 1787 | - $row = sql_fetch($res, $server_db); |
|
| 1788 | - if (version_compare($row['v'], '5.0.0', '>=')) { |
|
| 1789 | - defined('_MYSQL_SET_SQL_MODE') || define('_MYSQL_SET_SQL_MODE', true); |
|
| 1786 | + $res = sql_select('version() as v', '', '', '', '', '', '', $server_db); |
|
| 1787 | + $row = sql_fetch($res, $server_db); |
|
| 1788 | + if (version_compare($row['v'], '5.0.0', '>=')) { |
|
| 1789 | + defined('_MYSQL_SET_SQL_MODE') || define('_MYSQL_SET_SQL_MODE', true); |
|
| 1790 | 1790 | |
| 1791 | - return "defined('_MYSQL_SET_SQL_MODE') || define('_MYSQL_SET_SQL_MODE',true);\n"; |
|
| 1792 | - } |
|
| 1791 | + return "defined('_MYSQL_SET_SQL_MODE') || define('_MYSQL_SET_SQL_MODE',true);\n"; |
|
| 1792 | + } |
|
| 1793 | 1793 | |
| 1794 | - return ''; |
|
| 1794 | + return ''; |
|
| 1795 | 1795 | } |
@@ -17,7 +17,7 @@ discard block |
||
| 17 | 17 | */ |
| 18 | 18 | |
| 19 | 19 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 20 | - return; |
|
| 20 | + return; |
|
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | define('_DEFAULT_DB', 'spip'); |
@@ -30,159 +30,159 @@ discard block |
||
| 30 | 30 | // si ca ne marche toujours pas, echec. |
| 31 | 31 | |
| 32 | 32 | function req_pg_dist($addr, $port, $login, #[\SensitiveParameter] $pass, $db = '', $prefixe = '') { |
| 33 | - static $last_connect = []; |
|
| 34 | - if (!extension_loaded('pgsql')) { |
|
| 35 | - return false; |
|
| 36 | - } |
|
| 37 | - |
|
| 38 | - // si provient de selectdb |
|
| 39 | - if (empty($addr) && empty($port) && empty($login) && empty($pass)) { |
|
| 40 | - foreach (['addr', 'port', 'login', 'pass', 'prefixe'] as $a) { |
|
| 41 | - ${$a} = $last_connect[$a]; |
|
| 42 | - } |
|
| 43 | - } |
|
| 44 | - [$host, $p] = array_pad(explode(';', $addr), 2, null); |
|
| 45 | - if ($p > 0) { |
|
| 46 | - $port = " port=$p"; |
|
| 47 | - } else { |
|
| 48 | - $port = ''; |
|
| 49 | - } |
|
| 50 | - $erreurs = []; |
|
| 51 | - if ($db) { |
|
| 52 | - @$link = pg_connect("host=$host$port dbname=$db user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW); |
|
| 53 | - } elseif (!@$link = pg_connect("host=$host$port user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW)) { |
|
| 54 | - $erreurs[] = pg_last_error(); |
|
| 55 | - if (@$link = pg_connect("host=$host$port dbname=$login user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW)) { |
|
| 56 | - $db = $login; |
|
| 57 | - } else { |
|
| 58 | - $erreurs[] = pg_last_error(); |
|
| 59 | - $db = _DEFAULT_DB; |
|
| 60 | - $link = pg_connect("host=$host$port dbname=$db user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW); |
|
| 61 | - } |
|
| 62 | - } |
|
| 63 | - if (!$link) { |
|
| 64 | - $erreurs[] = pg_last_error(); |
|
| 65 | - foreach ($erreurs as $e) { |
|
| 66 | - spip_log('Echec pg_connect. Erreur : ' . $e, 'pg.' . _LOG_HS); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - return false; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - if ($link) { |
|
| 73 | - $last_connect = [ |
|
| 74 | - 'addr' => $addr, |
|
| 75 | - 'port' => $port, |
|
| 76 | - 'login' => $login, |
|
| 77 | - 'pass' => $pass, |
|
| 78 | - 'db' => $db, |
|
| 79 | - 'prefixe' => $prefixe, |
|
| 80 | - ]; |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - spip_log( |
|
| 84 | - "Connexion vers $host, base $db, prefixe $prefixe " . ($link ? 'operationnelle' : 'impossible'), |
|
| 85 | - 'pg.' . _LOG_DEBUG |
|
| 86 | - ); |
|
| 87 | - |
|
| 88 | - return !$link ? false : [ |
|
| 89 | - 'db' => $db, |
|
| 90 | - 'prefixe' => $prefixe ?: $db, |
|
| 91 | - 'link' => $link, |
|
| 92 | - ]; |
|
| 33 | + static $last_connect = []; |
|
| 34 | + if (!extension_loaded('pgsql')) { |
|
| 35 | + return false; |
|
| 36 | + } |
|
| 37 | + |
|
| 38 | + // si provient de selectdb |
|
| 39 | + if (empty($addr) && empty($port) && empty($login) && empty($pass)) { |
|
| 40 | + foreach (['addr', 'port', 'login', 'pass', 'prefixe'] as $a) { |
|
| 41 | + ${$a} = $last_connect[$a]; |
|
| 42 | + } |
|
| 43 | + } |
|
| 44 | + [$host, $p] = array_pad(explode(';', $addr), 2, null); |
|
| 45 | + if ($p > 0) { |
|
| 46 | + $port = " port=$p"; |
|
| 47 | + } else { |
|
| 48 | + $port = ''; |
|
| 49 | + } |
|
| 50 | + $erreurs = []; |
|
| 51 | + if ($db) { |
|
| 52 | + @$link = pg_connect("host=$host$port dbname=$db user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW); |
|
| 53 | + } elseif (!@$link = pg_connect("host=$host$port user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW)) { |
|
| 54 | + $erreurs[] = pg_last_error(); |
|
| 55 | + if (@$link = pg_connect("host=$host$port dbname=$login user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW)) { |
|
| 56 | + $db = $login; |
|
| 57 | + } else { |
|
| 58 | + $erreurs[] = pg_last_error(); |
|
| 59 | + $db = _DEFAULT_DB; |
|
| 60 | + $link = pg_connect("host=$host$port dbname=$db user=$login password='$pass'", PGSQL_CONNECT_FORCE_NEW); |
|
| 61 | + } |
|
| 62 | + } |
|
| 63 | + if (!$link) { |
|
| 64 | + $erreurs[] = pg_last_error(); |
|
| 65 | + foreach ($erreurs as $e) { |
|
| 66 | + spip_log('Echec pg_connect. Erreur : ' . $e, 'pg.' . _LOG_HS); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + return false; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + if ($link) { |
|
| 73 | + $last_connect = [ |
|
| 74 | + 'addr' => $addr, |
|
| 75 | + 'port' => $port, |
|
| 76 | + 'login' => $login, |
|
| 77 | + 'pass' => $pass, |
|
| 78 | + 'db' => $db, |
|
| 79 | + 'prefixe' => $prefixe, |
|
| 80 | + ]; |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + spip_log( |
|
| 84 | + "Connexion vers $host, base $db, prefixe $prefixe " . ($link ? 'operationnelle' : 'impossible'), |
|
| 85 | + 'pg.' . _LOG_DEBUG |
|
| 86 | + ); |
|
| 87 | + |
|
| 88 | + return !$link ? false : [ |
|
| 89 | + 'db' => $db, |
|
| 90 | + 'prefixe' => $prefixe ?: $db, |
|
| 91 | + 'link' => $link, |
|
| 92 | + ]; |
|
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | $GLOBALS['spip_pg_functions_1'] = [ |
| 96 | - 'alter' => 'spip_pg_alter', |
|
| 97 | - 'count' => 'spip_pg_count', |
|
| 98 | - 'countsel' => 'spip_pg_countsel', |
|
| 99 | - 'create' => 'spip_pg_create', |
|
| 100 | - 'create_base' => 'spip_pg_create_base', |
|
| 101 | - 'create_view' => 'spip_pg_create_view', |
|
| 102 | - 'date_proche' => 'spip_pg_date_proche', |
|
| 103 | - 'delete' => 'spip_pg_delete', |
|
| 104 | - 'drop_table' => 'spip_pg_drop_table', |
|
| 105 | - 'drop_view' => 'spip_pg_drop_view', |
|
| 106 | - 'errno' => 'spip_pg_errno', |
|
| 107 | - 'error' => 'spip_pg_error', |
|
| 108 | - 'explain' => 'spip_pg_explain', |
|
| 109 | - 'fetch' => 'spip_pg_fetch', |
|
| 110 | - 'seek' => 'spip_pg_seek', |
|
| 111 | - 'free' => 'spip_pg_free', |
|
| 112 | - 'hex' => 'spip_pg_hex', |
|
| 113 | - 'in' => 'spip_pg_in', |
|
| 114 | - 'insert' => 'spip_pg_insert', |
|
| 115 | - 'insertq' => 'spip_pg_insertq', |
|
| 116 | - 'insertq_multi' => 'spip_pg_insertq_multi', |
|
| 117 | - 'listdbs' => 'spip_pg_listdbs', |
|
| 118 | - 'multi' => 'spip_pg_multi', |
|
| 119 | - 'optimize' => 'spip_pg_optimize', |
|
| 120 | - 'query' => 'spip_pg_query', |
|
| 121 | - 'quote' => 'spip_pg_quote', |
|
| 122 | - 'replace' => 'spip_pg_replace', |
|
| 123 | - 'replace_multi' => 'spip_pg_replace_multi', |
|
| 124 | - 'select' => 'spip_pg_select', |
|
| 125 | - 'selectdb' => 'spip_pg_selectdb', |
|
| 126 | - 'set_connect_charset' => 'spip_pg_set_connect_charset', |
|
| 127 | - 'showbase' => 'spip_pg_showbase', |
|
| 128 | - 'showtable' => 'spip_pg_showtable', |
|
| 129 | - 'update' => 'spip_pg_update', |
|
| 130 | - 'updateq' => 'spip_pg_updateq', |
|
| 96 | + 'alter' => 'spip_pg_alter', |
|
| 97 | + 'count' => 'spip_pg_count', |
|
| 98 | + 'countsel' => 'spip_pg_countsel', |
|
| 99 | + 'create' => 'spip_pg_create', |
|
| 100 | + 'create_base' => 'spip_pg_create_base', |
|
| 101 | + 'create_view' => 'spip_pg_create_view', |
|
| 102 | + 'date_proche' => 'spip_pg_date_proche', |
|
| 103 | + 'delete' => 'spip_pg_delete', |
|
| 104 | + 'drop_table' => 'spip_pg_drop_table', |
|
| 105 | + 'drop_view' => 'spip_pg_drop_view', |
|
| 106 | + 'errno' => 'spip_pg_errno', |
|
| 107 | + 'error' => 'spip_pg_error', |
|
| 108 | + 'explain' => 'spip_pg_explain', |
|
| 109 | + 'fetch' => 'spip_pg_fetch', |
|
| 110 | + 'seek' => 'spip_pg_seek', |
|
| 111 | + 'free' => 'spip_pg_free', |
|
| 112 | + 'hex' => 'spip_pg_hex', |
|
| 113 | + 'in' => 'spip_pg_in', |
|
| 114 | + 'insert' => 'spip_pg_insert', |
|
| 115 | + 'insertq' => 'spip_pg_insertq', |
|
| 116 | + 'insertq_multi' => 'spip_pg_insertq_multi', |
|
| 117 | + 'listdbs' => 'spip_pg_listdbs', |
|
| 118 | + 'multi' => 'spip_pg_multi', |
|
| 119 | + 'optimize' => 'spip_pg_optimize', |
|
| 120 | + 'query' => 'spip_pg_query', |
|
| 121 | + 'quote' => 'spip_pg_quote', |
|
| 122 | + 'replace' => 'spip_pg_replace', |
|
| 123 | + 'replace_multi' => 'spip_pg_replace_multi', |
|
| 124 | + 'select' => 'spip_pg_select', |
|
| 125 | + 'selectdb' => 'spip_pg_selectdb', |
|
| 126 | + 'set_connect_charset' => 'spip_pg_set_connect_charset', |
|
| 127 | + 'showbase' => 'spip_pg_showbase', |
|
| 128 | + 'showtable' => 'spip_pg_showtable', |
|
| 129 | + 'update' => 'spip_pg_update', |
|
| 130 | + 'updateq' => 'spip_pg_updateq', |
|
| 131 | 131 | ]; |
| 132 | 132 | |
| 133 | 133 | // Par ou ca passe une fois les traductions faites |
| 134 | 134 | function spip_pg_trace_query($query, $serveur = '') { |
| 135 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 136 | - $prefixe = $connexion['prefixe']; |
|
| 137 | - $link = $connexion['link']; |
|
| 138 | - $db = $connexion['db']; |
|
| 139 | - |
|
| 140 | - if (isset($_GET['var_profile'])) { |
|
| 141 | - include_spip('public/tracer'); |
|
| 142 | - $t = trace_query_start(); |
|
| 143 | - $e = ''; |
|
| 144 | - } else { |
|
| 145 | - $t = 0; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - $connexion['last'] = $query; |
|
| 149 | - $r = spip_pg_query_simple($link, $query); |
|
| 150 | - |
|
| 151 | - // Log de l'erreur eventuelle |
|
| 152 | - if ($e = spip_pg_errno($serveur)) { |
|
| 153 | - $e .= spip_pg_error($query, $serveur); |
|
| 154 | - } // et du fautif |
|
| 155 | - return $t ? trace_query_end($query, $t, $r, $e, $serveur) : $r; |
|
| 135 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 136 | + $prefixe = $connexion['prefixe']; |
|
| 137 | + $link = $connexion['link']; |
|
| 138 | + $db = $connexion['db']; |
|
| 139 | + |
|
| 140 | + if (isset($_GET['var_profile'])) { |
|
| 141 | + include_spip('public/tracer'); |
|
| 142 | + $t = trace_query_start(); |
|
| 143 | + $e = ''; |
|
| 144 | + } else { |
|
| 145 | + $t = 0; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + $connexion['last'] = $query; |
|
| 149 | + $r = spip_pg_query_simple($link, $query); |
|
| 150 | + |
|
| 151 | + // Log de l'erreur eventuelle |
|
| 152 | + if ($e = spip_pg_errno($serveur)) { |
|
| 153 | + $e .= spip_pg_error($query, $serveur); |
|
| 154 | + } // et du fautif |
|
| 155 | + return $t ? trace_query_end($query, $t, $r, $e, $serveur) : $r; |
|
| 156 | 156 | } |
| 157 | 157 | |
| 158 | 158 | // Fonction de requete generale quand on est sur que c'est SQL standard. |
| 159 | 159 | // Elle change juste le noms des tables ($table_prefix) dans le FROM etc |
| 160 | 160 | |
| 161 | 161 | function spip_pg_query($query, $serveur = '', $requeter = true) { |
| 162 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 163 | - $prefixe = $connexion['prefixe']; |
|
| 164 | - $link = $connexion['link']; |
|
| 165 | - $db = $connexion['db']; |
|
| 166 | - |
|
| 167 | - if (preg_match('/\s(SET|VALUES|WHERE|DATABASE)\s/i', $query, $regs)) { |
|
| 168 | - $suite = strstr($query, (string) $regs[0]); |
|
| 169 | - $query = substr($query, 0, -strlen($suite)); |
|
| 170 | - } else { |
|
| 171 | - $suite = ''; |
|
| 172 | - } |
|
| 173 | - $query = preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', $query) . $suite; |
|
| 174 | - |
|
| 175 | - // renvoyer la requete inerte si demandee |
|
| 176 | - if (!$requeter) { |
|
| 177 | - return $query; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - return spip_pg_trace_query($query, $serveur); |
|
| 162 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 163 | + $prefixe = $connexion['prefixe']; |
|
| 164 | + $link = $connexion['link']; |
|
| 165 | + $db = $connexion['db']; |
|
| 166 | + |
|
| 167 | + if (preg_match('/\s(SET|VALUES|WHERE|DATABASE)\s/i', $query, $regs)) { |
|
| 168 | + $suite = strstr($query, (string) $regs[0]); |
|
| 169 | + $query = substr($query, 0, -strlen($suite)); |
|
| 170 | + } else { |
|
| 171 | + $suite = ''; |
|
| 172 | + } |
|
| 173 | + $query = preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', $query) . $suite; |
|
| 174 | + |
|
| 175 | + // renvoyer la requete inerte si demandee |
|
| 176 | + if (!$requeter) { |
|
| 177 | + return $query; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + return spip_pg_trace_query($query, $serveur); |
|
| 181 | 181 | } |
| 182 | 182 | |
| 183 | 183 | function spip_pg_query_simple($link, $query) { |
| 184 | - #spip_log(var_export($query,true), 'pg.'._LOG_DEBUG); |
|
| 185 | - return pg_query($link, $query); |
|
| 184 | + #spip_log(var_export($query,true), 'pg.'._LOG_DEBUG); |
|
| 185 | + return pg_query($link, $query); |
|
| 186 | 186 | } |
| 187 | 187 | |
| 188 | 188 | /* |
@@ -194,196 +194,196 @@ discard block |
||
| 194 | 194 | * de requetes showtable intempestives |
| 195 | 195 | */ |
| 196 | 196 | function spip_pg_ajouter_champs_timestamp($table, $couples, $desc = '', $serveur = '') { |
| 197 | - static $tables = []; |
|
| 198 | - |
|
| 199 | - if (!isset($tables[$table])) { |
|
| 200 | - if (!$desc) { |
|
| 201 | - $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 202 | - $desc = $trouver_table($table, $serveur); |
|
| 203 | - // si pas de description, on ne fait rien, ou on die() ? |
|
| 204 | - if (!$desc) { |
|
| 205 | - return $couples; |
|
| 206 | - } |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - // recherche des champs avec simplement 'TIMESTAMP' |
|
| 210 | - // cependant, il faudra peut etre etendre |
|
| 211 | - // avec la gestion de DEFAULT et ON UPDATE |
|
| 212 | - // mais ceux-ci ne sont pas utilises dans le core |
|
| 213 | - $tables[$table] = []; |
|
| 214 | - foreach ($desc['field'] as $k => $v) { |
|
| 215 | - $v = strtolower(ltrim($v)); |
|
| 216 | - // ne pas ajouter de timestamp now() si un default est specifie |
|
| 217 | - if (strpos($v, 'timestamp') === 0 and !str_contains($v, 'default')) { |
|
| 218 | - $tables[$table][] = $k; |
|
| 219 | - } |
|
| 220 | - } |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - // ajout des champs type 'timestamp' absents |
|
| 224 | - foreach ($tables[$table] as $maj) { |
|
| 225 | - if (!array_key_exists($maj, $couples)) { |
|
| 226 | - $couples[$maj] = 'NOW()'; |
|
| 227 | - } |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - return $couples; |
|
| 197 | + static $tables = []; |
|
| 198 | + |
|
| 199 | + if (!isset($tables[$table])) { |
|
| 200 | + if (!$desc) { |
|
| 201 | + $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 202 | + $desc = $trouver_table($table, $serveur); |
|
| 203 | + // si pas de description, on ne fait rien, ou on die() ? |
|
| 204 | + if (!$desc) { |
|
| 205 | + return $couples; |
|
| 206 | + } |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + // recherche des champs avec simplement 'TIMESTAMP' |
|
| 210 | + // cependant, il faudra peut etre etendre |
|
| 211 | + // avec la gestion de DEFAULT et ON UPDATE |
|
| 212 | + // mais ceux-ci ne sont pas utilises dans le core |
|
| 213 | + $tables[$table] = []; |
|
| 214 | + foreach ($desc['field'] as $k => $v) { |
|
| 215 | + $v = strtolower(ltrim($v)); |
|
| 216 | + // ne pas ajouter de timestamp now() si un default est specifie |
|
| 217 | + if (strpos($v, 'timestamp') === 0 and !str_contains($v, 'default')) { |
|
| 218 | + $tables[$table][] = $k; |
|
| 219 | + } |
|
| 220 | + } |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + // ajout des champs type 'timestamp' absents |
|
| 224 | + foreach ($tables[$table] as $maj) { |
|
| 225 | + if (!array_key_exists($maj, $couples)) { |
|
| 226 | + $couples[$maj] = 'NOW()'; |
|
| 227 | + } |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + return $couples; |
|
| 231 | 231 | } |
| 232 | 232 | |
| 233 | 233 | |
| 234 | 234 | // Alter en PG ne traite pas les index |
| 235 | 235 | function spip_pg_alter($query, $serveur = '', $requeter = true) { |
| 236 | - // il faudrait une regexp pour eviter de spliter ADD PRIMARY KEY (colA, colB) |
|
| 237 | - // tout en cassant en deux alter distincts "ADD PRIMARY KEY (colA, colB), ADD INDEX (chose)"... |
|
| 238 | - // ou revoir l'api de sql_alter en creant un |
|
| 239 | - // sql_alter_table($table,array($actions)); |
|
| 240 | - if (!preg_match('/\s*((\s*IGNORE)?\s*TABLE\s*([^\s]*))\s*(.*)?/is', $query, $regs)) { |
|
| 241 | - spip_log("$query mal comprise", 'pg.' . _LOG_ERREUR); |
|
| 242 | - |
|
| 243 | - return false; |
|
| 244 | - } |
|
| 245 | - $debut = $regs[1]; |
|
| 246 | - $table = $regs[3]; |
|
| 247 | - $suite = $regs[4]; |
|
| 248 | - $todo = explode(',', $suite); |
|
| 249 | - // on remet les morceaux dechires ensembles... que c'est laid ! |
|
| 250 | - $todo2 = []; |
|
| 251 | - $i = 0; |
|
| 252 | - $ouverte = false; |
|
| 253 | - while ($do = array_shift($todo)) { |
|
| 254 | - $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . ',' . $do : $do; |
|
| 255 | - $o = (str_contains($do, '(')); |
|
| 256 | - $f = (str_contains($do, ')')); |
|
| 257 | - if ($o and !$f) { |
|
| 258 | - $ouverte = true; |
|
| 259 | - } elseif ($f) { |
|
| 260 | - $ouverte = false; |
|
| 261 | - } |
|
| 262 | - if (!$ouverte) { |
|
| 263 | - $i++; |
|
| 264 | - } |
|
| 265 | - } |
|
| 266 | - $todo = $todo2; |
|
| 267 | - $query = $debut . ' ' . array_shift($todo); |
|
| 268 | - |
|
| 269 | - if (!preg_match('/^\s*(IGNORE\s*)?TABLE\s+(\w+)\s+(ADD|DROP|CHANGE|MODIFY|RENAME)\s*(.*)$/is', $query, $r)) { |
|
| 270 | - spip_log("$query incompris", 'pg.' . _LOG_ERREUR); |
|
| 271 | - } else { |
|
| 272 | - if ($r[1]) { |
|
| 273 | - spip_log("j'ignore IGNORE dans $query", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 274 | - } |
|
| 275 | - $f = 'spip_pg_alter_' . strtolower($r[3]); |
|
| 276 | - if (function_exists($f)) { |
|
| 277 | - $f($r[2], $r[4], $serveur, $requeter); |
|
| 278 | - } else { |
|
| 279 | - spip_log("$query non prevu", 'pg.' . _LOG_ERREUR); |
|
| 280 | - } |
|
| 281 | - } |
|
| 282 | - // Alter a plusieurs args. Faudrait optimiser. |
|
| 283 | - if ($todo) { |
|
| 284 | - spip_pg_alter("TABLE $table " . join(',', $todo)); |
|
| 285 | - } |
|
| 236 | + // il faudrait une regexp pour eviter de spliter ADD PRIMARY KEY (colA, colB) |
|
| 237 | + // tout en cassant en deux alter distincts "ADD PRIMARY KEY (colA, colB), ADD INDEX (chose)"... |
|
| 238 | + // ou revoir l'api de sql_alter en creant un |
|
| 239 | + // sql_alter_table($table,array($actions)); |
|
| 240 | + if (!preg_match('/\s*((\s*IGNORE)?\s*TABLE\s*([^\s]*))\s*(.*)?/is', $query, $regs)) { |
|
| 241 | + spip_log("$query mal comprise", 'pg.' . _LOG_ERREUR); |
|
| 242 | + |
|
| 243 | + return false; |
|
| 244 | + } |
|
| 245 | + $debut = $regs[1]; |
|
| 246 | + $table = $regs[3]; |
|
| 247 | + $suite = $regs[4]; |
|
| 248 | + $todo = explode(',', $suite); |
|
| 249 | + // on remet les morceaux dechires ensembles... que c'est laid ! |
|
| 250 | + $todo2 = []; |
|
| 251 | + $i = 0; |
|
| 252 | + $ouverte = false; |
|
| 253 | + while ($do = array_shift($todo)) { |
|
| 254 | + $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . ',' . $do : $do; |
|
| 255 | + $o = (str_contains($do, '(')); |
|
| 256 | + $f = (str_contains($do, ')')); |
|
| 257 | + if ($o and !$f) { |
|
| 258 | + $ouverte = true; |
|
| 259 | + } elseif ($f) { |
|
| 260 | + $ouverte = false; |
|
| 261 | + } |
|
| 262 | + if (!$ouverte) { |
|
| 263 | + $i++; |
|
| 264 | + } |
|
| 265 | + } |
|
| 266 | + $todo = $todo2; |
|
| 267 | + $query = $debut . ' ' . array_shift($todo); |
|
| 268 | + |
|
| 269 | + if (!preg_match('/^\s*(IGNORE\s*)?TABLE\s+(\w+)\s+(ADD|DROP|CHANGE|MODIFY|RENAME)\s*(.*)$/is', $query, $r)) { |
|
| 270 | + spip_log("$query incompris", 'pg.' . _LOG_ERREUR); |
|
| 271 | + } else { |
|
| 272 | + if ($r[1]) { |
|
| 273 | + spip_log("j'ignore IGNORE dans $query", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 274 | + } |
|
| 275 | + $f = 'spip_pg_alter_' . strtolower($r[3]); |
|
| 276 | + if (function_exists($f)) { |
|
| 277 | + $f($r[2], $r[4], $serveur, $requeter); |
|
| 278 | + } else { |
|
| 279 | + spip_log("$query non prevu", 'pg.' . _LOG_ERREUR); |
|
| 280 | + } |
|
| 281 | + } |
|
| 282 | + // Alter a plusieurs args. Faudrait optimiser. |
|
| 283 | + if ($todo) { |
|
| 284 | + spip_pg_alter("TABLE $table " . join(',', $todo)); |
|
| 285 | + } |
|
| 286 | 286 | } |
| 287 | 287 | |
| 288 | 288 | function spip_pg_alter_change($table, $arg, $serveur = '', $requeter = true) { |
| 289 | - if (!preg_match('/^`?(\w+)`?\s+`?(\w+)`?\s+(.*?)\s*(DEFAULT .*?)?(NOT\s+NULL)?\s*(DEFAULT .*?)?$/i', $arg, $r)) { |
|
| 290 | - spip_log("alter change: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 291 | - } else { |
|
| 292 | - [, $old, $new, $type, $default, $null, $def2] = $r; |
|
| 293 | - $actions = ["ALTER $old TYPE " . mysql2pg_type($type)]; |
|
| 294 | - if ($null) { |
|
| 295 | - $actions[] = "ALTER $old SET NOT NULL"; |
|
| 296 | - } else { |
|
| 297 | - $actions[] = "ALTER $old DROP NOT NULL"; |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - if ($d = ($default ?: $def2)) { |
|
| 301 | - $actions[] = "ALTER $old SET $d"; |
|
| 302 | - } else { |
|
| 303 | - $actions[] = "ALTER $old DROP DEFAULT"; |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - spip_pg_query("ALTER TABLE $table " . join(', ', $actions)); |
|
| 307 | - |
|
| 308 | - if ($old != $new) { |
|
| 309 | - spip_pg_query("ALTER TABLE $table RENAME $old TO $new", $serveur); |
|
| 310 | - } |
|
| 311 | - } |
|
| 289 | + if (!preg_match('/^`?(\w+)`?\s+`?(\w+)`?\s+(.*?)\s*(DEFAULT .*?)?(NOT\s+NULL)?\s*(DEFAULT .*?)?$/i', $arg, $r)) { |
|
| 290 | + spip_log("alter change: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 291 | + } else { |
|
| 292 | + [, $old, $new, $type, $default, $null, $def2] = $r; |
|
| 293 | + $actions = ["ALTER $old TYPE " . mysql2pg_type($type)]; |
|
| 294 | + if ($null) { |
|
| 295 | + $actions[] = "ALTER $old SET NOT NULL"; |
|
| 296 | + } else { |
|
| 297 | + $actions[] = "ALTER $old DROP NOT NULL"; |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + if ($d = ($default ?: $def2)) { |
|
| 301 | + $actions[] = "ALTER $old SET $d"; |
|
| 302 | + } else { |
|
| 303 | + $actions[] = "ALTER $old DROP DEFAULT"; |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + spip_pg_query("ALTER TABLE $table " . join(', ', $actions)); |
|
| 307 | + |
|
| 308 | + if ($old != $new) { |
|
| 309 | + spip_pg_query("ALTER TABLE $table RENAME $old TO $new", $serveur); |
|
| 310 | + } |
|
| 311 | + } |
|
| 312 | 312 | } |
| 313 | 313 | |
| 314 | 314 | function spip_pg_alter_add($table, $arg, $serveur = '', $requeter = true) { |
| 315 | - $nom_index = null; |
|
| 316 | - if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*(.*)$/', $arg, $r)) { |
|
| 317 | - spip_log("alter add $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 318 | - |
|
| 319 | - return null; |
|
| 320 | - } |
|
| 321 | - if (!$r[1] or $r[1] == 'COLUMN') { |
|
| 322 | - preg_match('/`?(\w+)`?(.*)/', $r[2], $m); |
|
| 323 | - if (preg_match('/^(.*)(BEFORE|AFTER|FIRST)(.*)$/is', $m[2], $n)) { |
|
| 324 | - $m[2] = $n[1]; |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - return spip_pg_query("ALTER TABLE $table ADD " . $m[1] . ' ' . mysql2pg_type($m[2]), $serveur, $requeter); |
|
| 328 | - } elseif ($r[1][0] == 'P') { |
|
| 329 | - // la primary peut etre sur plusieurs champs |
|
| 330 | - $r[2] = trim(str_replace('`', '', $r[2])); |
|
| 331 | - $m = ($r[2][0] == '(') ? substr($r[2], 1, -1) : $r[2]; |
|
| 332 | - |
|
| 333 | - return spip_pg_query( |
|
| 334 | - "ALTER TABLE $table ADD CONSTRAINT $table" . '_pkey PRIMARY KEY (' . $m . ')', |
|
| 335 | - $serveur, |
|
| 336 | - $requeter |
|
| 337 | - ); |
|
| 338 | - } else { |
|
| 339 | - preg_match('/([^\s,]*)\s*(.*)?/', $r[2], $m); |
|
| 340 | - // peut etre "(colonne)" ou "nom_index (colonnes)" |
|
| 341 | - // bug potentiel si qqn met "(colonne, colonne)" |
|
| 342 | - // |
|
| 343 | - // nom_index (colonnes) |
|
| 344 | - if ($m[2]) { |
|
| 345 | - $colonnes = substr($m[2], 1, -1); |
|
| 346 | - $nom_index = $m[1]; |
|
| 347 | - } else { |
|
| 348 | - // (colonne) |
|
| 349 | - if ($m[1][0] == '(') { |
|
| 350 | - $colonnes = substr($m[1], 1, -1); |
|
| 351 | - if (str_contains(',', $colonnes)) { |
|
| 352 | - spip_log('PG : Erreur, impossible de creer un index sur plusieurs colonnes' |
|
| 353 | - . " sans qu'il ait de nom ($table, ($colonnes))", 'pg.' . _LOG_ERREUR); |
|
| 354 | - } else { |
|
| 355 | - $nom_index = $colonnes; |
|
| 356 | - } |
|
| 357 | - } // nom_index |
|
| 358 | - else { |
|
| 359 | - $nom_index = $colonnes = $m[1]; |
|
| 360 | - } |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - return spip_pg_create_index($nom_index, $table, $colonnes, $serveur, $requeter); |
|
| 364 | - } |
|
| 315 | + $nom_index = null; |
|
| 316 | + if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*(.*)$/', $arg, $r)) { |
|
| 317 | + spip_log("alter add $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 318 | + |
|
| 319 | + return null; |
|
| 320 | + } |
|
| 321 | + if (!$r[1] or $r[1] == 'COLUMN') { |
|
| 322 | + preg_match('/`?(\w+)`?(.*)/', $r[2], $m); |
|
| 323 | + if (preg_match('/^(.*)(BEFORE|AFTER|FIRST)(.*)$/is', $m[2], $n)) { |
|
| 324 | + $m[2] = $n[1]; |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + return spip_pg_query("ALTER TABLE $table ADD " . $m[1] . ' ' . mysql2pg_type($m[2]), $serveur, $requeter); |
|
| 328 | + } elseif ($r[1][0] == 'P') { |
|
| 329 | + // la primary peut etre sur plusieurs champs |
|
| 330 | + $r[2] = trim(str_replace('`', '', $r[2])); |
|
| 331 | + $m = ($r[2][0] == '(') ? substr($r[2], 1, -1) : $r[2]; |
|
| 332 | + |
|
| 333 | + return spip_pg_query( |
|
| 334 | + "ALTER TABLE $table ADD CONSTRAINT $table" . '_pkey PRIMARY KEY (' . $m . ')', |
|
| 335 | + $serveur, |
|
| 336 | + $requeter |
|
| 337 | + ); |
|
| 338 | + } else { |
|
| 339 | + preg_match('/([^\s,]*)\s*(.*)?/', $r[2], $m); |
|
| 340 | + // peut etre "(colonne)" ou "nom_index (colonnes)" |
|
| 341 | + // bug potentiel si qqn met "(colonne, colonne)" |
|
| 342 | + // |
|
| 343 | + // nom_index (colonnes) |
|
| 344 | + if ($m[2]) { |
|
| 345 | + $colonnes = substr($m[2], 1, -1); |
|
| 346 | + $nom_index = $m[1]; |
|
| 347 | + } else { |
|
| 348 | + // (colonne) |
|
| 349 | + if ($m[1][0] == '(') { |
|
| 350 | + $colonnes = substr($m[1], 1, -1); |
|
| 351 | + if (str_contains(',', $colonnes)) { |
|
| 352 | + spip_log('PG : Erreur, impossible de creer un index sur plusieurs colonnes' |
|
| 353 | + . " sans qu'il ait de nom ($table, ($colonnes))", 'pg.' . _LOG_ERREUR); |
|
| 354 | + } else { |
|
| 355 | + $nom_index = $colonnes; |
|
| 356 | + } |
|
| 357 | + } // nom_index |
|
| 358 | + else { |
|
| 359 | + $nom_index = $colonnes = $m[1]; |
|
| 360 | + } |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + return spip_pg_create_index($nom_index, $table, $colonnes, $serveur, $requeter); |
|
| 364 | + } |
|
| 365 | 365 | } |
| 366 | 366 | |
| 367 | 367 | function spip_pg_alter_drop($table, $arg, $serveur = '', $requeter = true) { |
| 368 | - if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*`?(\w*)`?/', $arg, $r)) { |
|
| 369 | - spip_log("alter drop: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 370 | - } else { |
|
| 371 | - if (!$r[1] or $r[1] == 'COLUMN') { |
|
| 372 | - return spip_pg_query("ALTER TABLE $table DROP " . $r[2], $serveur); |
|
| 373 | - } elseif ($r[1][0] == 'P') { |
|
| 374 | - return spip_pg_query("ALTER TABLE $table DROP CONSTRAINT $table" . '_pkey', $serveur); |
|
| 375 | - } else { |
|
| 376 | - return spip_pg_query('DROP INDEX ' . $table . '_' . $r[2], $serveur); |
|
| 377 | - } |
|
| 378 | - } |
|
| 368 | + if (!preg_match('/^(COLUMN|INDEX|KEY|PRIMARY\s+KEY|)\s*`?(\w*)`?/', $arg, $r)) { |
|
| 369 | + spip_log("alter drop: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 370 | + } else { |
|
| 371 | + if (!$r[1] or $r[1] == 'COLUMN') { |
|
| 372 | + return spip_pg_query("ALTER TABLE $table DROP " . $r[2], $serveur); |
|
| 373 | + } elseif ($r[1][0] == 'P') { |
|
| 374 | + return spip_pg_query("ALTER TABLE $table DROP CONSTRAINT $table" . '_pkey', $serveur); |
|
| 375 | + } else { |
|
| 376 | + return spip_pg_query('DROP INDEX ' . $table . '_' . $r[2], $serveur); |
|
| 377 | + } |
|
| 378 | + } |
|
| 379 | 379 | } |
| 380 | 380 | |
| 381 | 381 | function spip_pg_alter_modify($table, $arg, $serveur = '', $requeter = true) { |
| 382 | - if (!preg_match('/^`?(\w+)`?\s+(.*)$/', $arg, $r)) { |
|
| 383 | - spip_log("alter modify: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 384 | - } else { |
|
| 385 | - return spip_pg_alter_change($table, $r[1] . ' ' . $arg, $serveur = '', $requeter = true); |
|
| 386 | - } |
|
| 382 | + if (!preg_match('/^`?(\w+)`?\s+(.*)$/', $arg, $r)) { |
|
| 383 | + spip_log("alter modify: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 384 | + } else { |
|
| 385 | + return spip_pg_alter_change($table, $r[1] . ' ' . $arg, $serveur = '', $requeter = true); |
|
| 386 | + } |
|
| 387 | 387 | } |
| 388 | 388 | |
| 389 | 389 | // attention (en pg) : |
@@ -391,17 +391,17 @@ discard block |
||
| 391 | 391 | // - alter table A rename X to Y = changer le nom de la colonne X en Y |
| 392 | 392 | // pour l'instant, traiter simplement RENAME TO X |
| 393 | 393 | function spip_pg_alter_rename($table, $arg, $serveur = '', $requeter = true) { |
| 394 | - $rename = ''; |
|
| 395 | - // si TO, mais pas au debut |
|
| 396 | - if (!stripos($arg, 'TO ')) { |
|
| 397 | - $rename = $arg; |
|
| 398 | - } elseif (preg_match('/^(TO)\s*`?(\w*)`?/', $arg, $r)) { |
|
| 399 | - $rename = $r[2]; |
|
| 400 | - } else { |
|
| 401 | - spip_log("alter rename: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 402 | - } |
|
| 403 | - |
|
| 404 | - return $rename ? spip_pg_query("ALTER TABLE $table RENAME TO $rename") : false; |
|
| 394 | + $rename = ''; |
|
| 395 | + // si TO, mais pas au debut |
|
| 396 | + if (!stripos($arg, 'TO ')) { |
|
| 397 | + $rename = $arg; |
|
| 398 | + } elseif (preg_match('/^(TO)\s*`?(\w*)`?/', $arg, $r)) { |
|
| 399 | + $rename = $r[2]; |
|
| 400 | + } else { |
|
| 401 | + spip_log("alter rename: $arg incompris", 'pg.' . _LOG_ERREUR); |
|
| 402 | + } |
|
| 403 | + |
|
| 404 | + return $rename ? spip_pg_query("ALTER TABLE $table RENAME TO $rename") : false; |
|
| 405 | 405 | } |
| 406 | 406 | |
| 407 | 407 | |
@@ -417,59 +417,59 @@ discard block |
||
| 417 | 417 | * @return bool ou requete |
| 418 | 418 | */ |
| 419 | 419 | function spip_pg_create_index($nom, $table, $champs, $serveur = '', $requeter = true) { |
| 420 | - if (!($nom or $table or $champs)) { |
|
| 421 | - spip_log( |
|
| 422 | - "Champ manquant pour creer un index pg ($nom, $table, (" . @join(',', $champs) . '))', |
|
| 423 | - 'pg.' . _LOG_ERREUR |
|
| 424 | - ); |
|
| 425 | - |
|
| 426 | - return false; |
|
| 427 | - } |
|
| 428 | - |
|
| 429 | - $nom = str_replace('`', '', $nom); |
|
| 430 | - $champs = str_replace('`', '', $champs); |
|
| 431 | - |
|
| 432 | - // PG ne differentie pas noms des index en fonction des tables |
|
| 433 | - // il faut donc creer des noms uniques d'index pour une base pg |
|
| 434 | - $nom = $table . '_' . $nom; |
|
| 435 | - // enlever d'eventuelles parentheses deja presentes sur champs |
|
| 436 | - if (!is_array($champs)) { |
|
| 437 | - if ($champs[0] == '(') { |
|
| 438 | - $champs = substr($champs, 1, -1); |
|
| 439 | - } |
|
| 440 | - $champs = [$champs]; |
|
| 441 | - } |
|
| 442 | - $query = "CREATE INDEX $nom ON $table (" . join(',', $champs) . ')'; |
|
| 443 | - if (!$requeter) { |
|
| 444 | - return $query; |
|
| 445 | - } |
|
| 446 | - $res = spip_pg_query($query, $serveur, $requeter); |
|
| 447 | - |
|
| 448 | - return $res; |
|
| 420 | + if (!($nom or $table or $champs)) { |
|
| 421 | + spip_log( |
|
| 422 | + "Champ manquant pour creer un index pg ($nom, $table, (" . @join(',', $champs) . '))', |
|
| 423 | + 'pg.' . _LOG_ERREUR |
|
| 424 | + ); |
|
| 425 | + |
|
| 426 | + return false; |
|
| 427 | + } |
|
| 428 | + |
|
| 429 | + $nom = str_replace('`', '', $nom); |
|
| 430 | + $champs = str_replace('`', '', $champs); |
|
| 431 | + |
|
| 432 | + // PG ne differentie pas noms des index en fonction des tables |
|
| 433 | + // il faut donc creer des noms uniques d'index pour une base pg |
|
| 434 | + $nom = $table . '_' . $nom; |
|
| 435 | + // enlever d'eventuelles parentheses deja presentes sur champs |
|
| 436 | + if (!is_array($champs)) { |
|
| 437 | + if ($champs[0] == '(') { |
|
| 438 | + $champs = substr($champs, 1, -1); |
|
| 439 | + } |
|
| 440 | + $champs = [$champs]; |
|
| 441 | + } |
|
| 442 | + $query = "CREATE INDEX $nom ON $table (" . join(',', $champs) . ')'; |
|
| 443 | + if (!$requeter) { |
|
| 444 | + return $query; |
|
| 445 | + } |
|
| 446 | + $res = spip_pg_query($query, $serveur, $requeter); |
|
| 447 | + |
|
| 448 | + return $res; |
|
| 449 | 449 | } |
| 450 | 450 | |
| 451 | 451 | |
| 452 | 452 | function spip_pg_explain($query, $serveur = '', $requeter = true) { |
| 453 | - if (strpos(ltrim($query), 'SELECT') !== 0) { |
|
| 454 | - return []; |
|
| 455 | - } |
|
| 456 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 457 | - $prefixe = $connexion['prefixe']; |
|
| 458 | - $link = $connexion['link']; |
|
| 459 | - if (preg_match('/\s(SET|VALUES|WHERE)\s/i', $query, $regs)) { |
|
| 460 | - $suite = strstr($query, (string) $regs[0]); |
|
| 461 | - $query = substr($query, 0, -strlen($suite)); |
|
| 462 | - } else { |
|
| 463 | - $suite = ''; |
|
| 464 | - } |
|
| 465 | - $query = 'EXPLAIN ' . preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', $query) . $suite; |
|
| 466 | - |
|
| 467 | - if (!$requeter) { |
|
| 468 | - return $query; |
|
| 469 | - } |
|
| 470 | - $r = spip_pg_query_simple($link, $query); |
|
| 471 | - |
|
| 472 | - return spip_pg_fetch($r, null, $serveur); |
|
| 453 | + if (strpos(ltrim($query), 'SELECT') !== 0) { |
|
| 454 | + return []; |
|
| 455 | + } |
|
| 456 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 457 | + $prefixe = $connexion['prefixe']; |
|
| 458 | + $link = $connexion['link']; |
|
| 459 | + if (preg_match('/\s(SET|VALUES|WHERE)\s/i', $query, $regs)) { |
|
| 460 | + $suite = strstr($query, (string) $regs[0]); |
|
| 461 | + $query = substr($query, 0, -strlen($suite)); |
|
| 462 | + } else { |
|
| 463 | + $suite = ''; |
|
| 464 | + } |
|
| 465 | + $query = 'EXPLAIN ' . preg_replace('/([,\s])spip_/', '\1' . $prefixe . '_', $query) . $suite; |
|
| 466 | + |
|
| 467 | + if (!$requeter) { |
|
| 468 | + return $query; |
|
| 469 | + } |
|
| 470 | + $r = spip_pg_query_simple($link, $query); |
|
| 471 | + |
|
| 472 | + return spip_pg_fetch($r, null, $serveur); |
|
| 473 | 473 | } |
| 474 | 474 | |
| 475 | 475 | |
@@ -488,92 +488,92 @@ discard block |
||
| 488 | 488 | * - False en cas d'erreur. |
| 489 | 489 | **/ |
| 490 | 490 | function spip_pg_selectdb($db, $serveur = '', $requeter = true) { |
| 491 | - // se connecter a la base indiquee |
|
| 492 | - // avec les identifiants connus |
|
| 493 | - $index = $serveur ? strtolower($serveur) : 0; |
|
| 491 | + // se connecter a la base indiquee |
|
| 492 | + // avec les identifiants connus |
|
| 493 | + $index = $serveur ? strtolower($serveur) : 0; |
|
| 494 | 494 | |
| 495 | - if ($link = spip_connect_db('', '', '', '', $db, 'pg', '', '')) { |
|
| 496 | - if (($db == $link['db']) && $GLOBALS['connexions'][$index] = $link) { |
|
| 497 | - return $db; |
|
| 498 | - } |
|
| 499 | - } |
|
| 495 | + if ($link = spip_connect_db('', '', '', '', $db, 'pg', '', '')) { |
|
| 496 | + if (($db == $link['db']) && $GLOBALS['connexions'][$index] = $link) { |
|
| 497 | + return $db; |
|
| 498 | + } |
|
| 499 | + } |
|
| 500 | 500 | |
| 501 | - return false; |
|
| 501 | + return false; |
|
| 502 | 502 | } |
| 503 | 503 | |
| 504 | 504 | // Qu'une seule base pour le moment |
| 505 | 505 | |
| 506 | 506 | function spip_pg_listdbs($serveur) { |
| 507 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 508 | - $link = $connexion['link']; |
|
| 509 | - $dbs = []; |
|
| 510 | - $res = spip_pg_query_simple($link, 'select * From pg_database'); |
|
| 511 | - while ($row = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 512 | - $dbs[] = reset($row); |
|
| 513 | - } |
|
| 514 | - |
|
| 515 | - return $dbs; |
|
| 507 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 508 | + $link = $connexion['link']; |
|
| 509 | + $dbs = []; |
|
| 510 | + $res = spip_pg_query_simple($link, 'select * From pg_database'); |
|
| 511 | + while ($row = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 512 | + $dbs[] = reset($row); |
|
| 513 | + } |
|
| 514 | + |
|
| 515 | + return $dbs; |
|
| 516 | 516 | } |
| 517 | 517 | |
| 518 | 518 | function spip_pg_select( |
| 519 | - $select, |
|
| 520 | - $from, |
|
| 521 | - $where = '', |
|
| 522 | - $groupby = [], |
|
| 523 | - $orderby = '', |
|
| 524 | - $limit = '', |
|
| 525 | - $having = '', |
|
| 526 | - $serveur = '', |
|
| 527 | - $requeter = true |
|
| 519 | + $select, |
|
| 520 | + $from, |
|
| 521 | + $where = '', |
|
| 522 | + $groupby = [], |
|
| 523 | + $orderby = '', |
|
| 524 | + $limit = '', |
|
| 525 | + $having = '', |
|
| 526 | + $serveur = '', |
|
| 527 | + $requeter = true |
|
| 528 | 528 | ) { |
| 529 | 529 | |
| 530 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 531 | - $prefixe = $connexion['prefixe']; |
|
| 532 | - $link = $connexion['link']; |
|
| 533 | - $db = $connexion['db']; |
|
| 534 | - |
|
| 535 | - $limit = preg_match('/^\s*(([0-9]+),)?\s*([0-9]+)\s*$/', $limit, $limatch); |
|
| 536 | - if ($limit) { |
|
| 537 | - $offset = $limatch[2]; |
|
| 538 | - $count = $limatch[3]; |
|
| 539 | - } |
|
| 540 | - |
|
| 541 | - $select = spip_pg_frommysql($select); |
|
| 542 | - |
|
| 543 | - // si pas de tri explicitement demande, le GROUP BY ne |
|
| 544 | - // contient que la clef primaire. |
|
| 545 | - // lui ajouter alors le champ de tri par defaut |
|
| 546 | - if (preg_match('/FIELD\(([a-z]+\.[a-z]+),/i', $orderby[0], $groupbyplus)) { |
|
| 547 | - $groupby[] = $groupbyplus[1]; |
|
| 548 | - } |
|
| 549 | - |
|
| 550 | - $orderby = spip_pg_orderby($orderby, $select); |
|
| 551 | - |
|
| 552 | - if ($having) { |
|
| 553 | - if (is_array($having)) { |
|
| 554 | - $having = join("\n\tAND ", array_map('calculer_pg_where', $having)); |
|
| 555 | - } |
|
| 556 | - } |
|
| 557 | - $from = spip_pg_from($from, $prefixe); |
|
| 558 | - $query = 'SELECT ' . $select |
|
| 559 | - . (!$from ? '' : "\nFROM $from") |
|
| 560 | - . (!$where ? '' : ("\nWHERE " . (!is_array($where) ? calculer_pg_where($where) : (join( |
|
| 561 | - "\n\tAND ", |
|
| 562 | - array_map('calculer_pg_where', $where) |
|
| 563 | - ))))) |
|
| 564 | - . spip_pg_groupby($groupby, $from, $select) |
|
| 565 | - . (!$having ? '' : "\nHAVING $having") |
|
| 566 | - . ($orderby ? ("\nORDER BY $orderby") : '') |
|
| 567 | - . (!$limit ? '' : (" LIMIT $count" . (!$offset ? '' : " OFFSET $offset"))); |
|
| 568 | - |
|
| 569 | - // renvoyer la requete inerte si demandee |
|
| 570 | - if ($requeter === false) { |
|
| 571 | - return $query; |
|
| 572 | - } |
|
| 573 | - |
|
| 574 | - $r = spip_pg_trace_query($query, $serveur); |
|
| 575 | - |
|
| 576 | - return $r ?: $query; |
|
| 530 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 531 | + $prefixe = $connexion['prefixe']; |
|
| 532 | + $link = $connexion['link']; |
|
| 533 | + $db = $connexion['db']; |
|
| 534 | + |
|
| 535 | + $limit = preg_match('/^\s*(([0-9]+),)?\s*([0-9]+)\s*$/', $limit, $limatch); |
|
| 536 | + if ($limit) { |
|
| 537 | + $offset = $limatch[2]; |
|
| 538 | + $count = $limatch[3]; |
|
| 539 | + } |
|
| 540 | + |
|
| 541 | + $select = spip_pg_frommysql($select); |
|
| 542 | + |
|
| 543 | + // si pas de tri explicitement demande, le GROUP BY ne |
|
| 544 | + // contient que la clef primaire. |
|
| 545 | + // lui ajouter alors le champ de tri par defaut |
|
| 546 | + if (preg_match('/FIELD\(([a-z]+\.[a-z]+),/i', $orderby[0], $groupbyplus)) { |
|
| 547 | + $groupby[] = $groupbyplus[1]; |
|
| 548 | + } |
|
| 549 | + |
|
| 550 | + $orderby = spip_pg_orderby($orderby, $select); |
|
| 551 | + |
|
| 552 | + if ($having) { |
|
| 553 | + if (is_array($having)) { |
|
| 554 | + $having = join("\n\tAND ", array_map('calculer_pg_where', $having)); |
|
| 555 | + } |
|
| 556 | + } |
|
| 557 | + $from = spip_pg_from($from, $prefixe); |
|
| 558 | + $query = 'SELECT ' . $select |
|
| 559 | + . (!$from ? '' : "\nFROM $from") |
|
| 560 | + . (!$where ? '' : ("\nWHERE " . (!is_array($where) ? calculer_pg_where($where) : (join( |
|
| 561 | + "\n\tAND ", |
|
| 562 | + array_map('calculer_pg_where', $where) |
|
| 563 | + ))))) |
|
| 564 | + . spip_pg_groupby($groupby, $from, $select) |
|
| 565 | + . (!$having ? '' : "\nHAVING $having") |
|
| 566 | + . ($orderby ? ("\nORDER BY $orderby") : '') |
|
| 567 | + . (!$limit ? '' : (" LIMIT $count" . (!$offset ? '' : " OFFSET $offset"))); |
|
| 568 | + |
|
| 569 | + // renvoyer la requete inerte si demandee |
|
| 570 | + if ($requeter === false) { |
|
| 571 | + return $query; |
|
| 572 | + } |
|
| 573 | + |
|
| 574 | + $r = spip_pg_trace_query($query, $serveur); |
|
| 575 | + |
|
| 576 | + return $r ?: $query; |
|
| 577 | 577 | ; |
| 578 | 578 | } |
| 579 | 579 | |
@@ -581,26 +581,26 @@ discard block |
||
| 581 | 581 | // car le reste de la requete utilise les alias (AS) systematiquement |
| 582 | 582 | |
| 583 | 583 | function spip_pg_from($from, $prefixe) { |
| 584 | - if (is_array($from)) { |
|
| 585 | - $from = spip_pg_select_as($from); |
|
| 586 | - } |
|
| 584 | + if (is_array($from)) { |
|
| 585 | + $from = spip_pg_select_as($from); |
|
| 586 | + } |
|
| 587 | 587 | |
| 588 | - return !$prefixe ? $from : preg_replace('/(\b)spip_/', '\1' . $prefixe . '_', $from); |
|
| 588 | + return !$prefixe ? $from : preg_replace('/(\b)spip_/', '\1' . $prefixe . '_', $from); |
|
| 589 | 589 | } |
| 590 | 590 | |
| 591 | 591 | function spip_pg_orderby($order, $select) { |
| 592 | - $res = []; |
|
| 593 | - $arg = (is_array($order) ? $order : preg_split('/\s*,\s*/', $order)); |
|
| 594 | - |
|
| 595 | - foreach ($arg as $v) { |
|
| 596 | - if (preg_match('/(case\s+.*?else\s+0\s+end)\s*AS\s+' . $v . '/', $select, $m)) { |
|
| 597 | - $res[] = $m[1]; |
|
| 598 | - } else { |
|
| 599 | - $res[] = $v; |
|
| 600 | - } |
|
| 601 | - } |
|
| 602 | - |
|
| 603 | - return spip_pg_frommysql(join(',', $res)); |
|
| 592 | + $res = []; |
|
| 593 | + $arg = (is_array($order) ? $order : preg_split('/\s*,\s*/', $order)); |
|
| 594 | + |
|
| 595 | + foreach ($arg as $v) { |
|
| 596 | + if (preg_match('/(case\s+.*?else\s+0\s+end)\s*AS\s+' . $v . '/', $select, $m)) { |
|
| 597 | + $res[] = $m[1]; |
|
| 598 | + } else { |
|
| 599 | + $res[] = $v; |
|
| 600 | + } |
|
| 601 | + } |
|
| 602 | + |
|
| 603 | + return spip_pg_frommysql(join(',', $res)); |
|
| 604 | 604 | } |
| 605 | 605 | |
| 606 | 606 | // Conversion a l'arrach' des jointures MySQL en jointures PG |
@@ -608,56 +608,56 @@ discard block |
||
| 608 | 608 | // et pour enlever les repetitions (sans incidence de perf, mais ca fait sale) |
| 609 | 609 | |
| 610 | 610 | function spip_pg_groupby($groupby, $from, $select) { |
| 611 | - $join = strpos($from, ','); |
|
| 612 | - // ismplifier avant de decouper |
|
| 613 | - if (is_string($select)) { // fct SQL sur colonne et constante apostrophee ==> la colonne |
|
| 614 | - $select = preg_replace('/\w+\(\s*([^(),\']*),\s*\'[^\']*\'[^)]*\)/', '\\1', $select); |
|
| 615 | - } |
|
| 616 | - |
|
| 617 | - if ($join or $groupby) { |
|
| 618 | - $join = is_array($select) ? $select : explode(', ', $select); |
|
| 619 | - } |
|
| 620 | - if ($join) { |
|
| 621 | - // enlever les 0 as points, '', ... |
|
| 622 | - foreach ($join as $k => $v) { |
|
| 623 | - $v = str_replace('DISTINCT ', '', $v); |
|
| 624 | - // fct SQL sur colonne et constante apostrophee ==> la colonne |
|
| 625 | - $v = preg_replace('/\w+\(\s*([^(),\']*),\s*\'[^\']*\'[^)]*\)/', '\\1', $v); |
|
| 626 | - $v = preg_replace('/CAST\(\s*([^(),\' ]*\s+)as\s*\w+\)/', '\\1', $v); |
|
| 627 | - // resultat d'agregat ne sont pas a mettre dans le groupby |
|
| 628 | - $v = preg_replace('/(SUM|COUNT|MAX|MIN|UPPER)\([^)]+\)(\s*AS\s+\w+)\s*,?/i', '', $v); |
|
| 629 | - // idem sans AS (fetch numerique) |
|
| 630 | - $v = preg_replace('/(SUM|COUNT|MAX|MIN|UPPER)\([^)]+\)\s*,?/i', '', $v); |
|
| 631 | - // des AS simples : on garde le cote droit du AS |
|
| 632 | - $v = preg_replace('/^.*\sAS\s+(\w+)\s*$/i', '\\1', $v); |
|
| 633 | - // ne reste plus que les vrais colonnes, ou des constantes a virer |
|
| 634 | - if (preg_match(',^[\'"],', $v) or is_numeric($v)) { |
|
| 635 | - unset($join[$k]); |
|
| 636 | - } else { |
|
| 637 | - $join[$k] = trim($v); |
|
| 638 | - } |
|
| 639 | - } |
|
| 640 | - $join = array_diff($join, ['']); |
|
| 641 | - $join = implode(',', $join); |
|
| 642 | - } |
|
| 643 | - if (is_array($groupby)) { |
|
| 644 | - $groupby = join(',', $groupby); |
|
| 645 | - } |
|
| 646 | - if ($join) { |
|
| 647 | - $groupby = $groupby ? "$groupby, $join" : $join; |
|
| 648 | - } |
|
| 649 | - if (!$groupby) { |
|
| 650 | - return ''; |
|
| 651 | - } |
|
| 652 | - |
|
| 653 | - $groupby = spip_pg_frommysql($groupby); |
|
| 654 | - // Ne pas mettre dans le Group-By des valeurs numeriques |
|
| 655 | - // issue de prepare_recherche |
|
| 656 | - $groupby = preg_replace('/^\s*\d+\s+AS\s+\w+\s*,?\s*/i', '', $groupby); |
|
| 657 | - $groupby = preg_replace('/,\s*\d+\s+AS\s+\w+\s*/i', '', $groupby); |
|
| 658 | - $groupby = preg_replace('/\s+AS\s+\w+\s*/i', '', $groupby); |
|
| 659 | - |
|
| 660 | - return "\nGROUP BY $groupby"; |
|
| 611 | + $join = strpos($from, ','); |
|
| 612 | + // ismplifier avant de decouper |
|
| 613 | + if (is_string($select)) { // fct SQL sur colonne et constante apostrophee ==> la colonne |
|
| 614 | + $select = preg_replace('/\w+\(\s*([^(),\']*),\s*\'[^\']*\'[^)]*\)/', '\\1', $select); |
|
| 615 | + } |
|
| 616 | + |
|
| 617 | + if ($join or $groupby) { |
|
| 618 | + $join = is_array($select) ? $select : explode(', ', $select); |
|
| 619 | + } |
|
| 620 | + if ($join) { |
|
| 621 | + // enlever les 0 as points, '', ... |
|
| 622 | + foreach ($join as $k => $v) { |
|
| 623 | + $v = str_replace('DISTINCT ', '', $v); |
|
| 624 | + // fct SQL sur colonne et constante apostrophee ==> la colonne |
|
| 625 | + $v = preg_replace('/\w+\(\s*([^(),\']*),\s*\'[^\']*\'[^)]*\)/', '\\1', $v); |
|
| 626 | + $v = preg_replace('/CAST\(\s*([^(),\' ]*\s+)as\s*\w+\)/', '\\1', $v); |
|
| 627 | + // resultat d'agregat ne sont pas a mettre dans le groupby |
|
| 628 | + $v = preg_replace('/(SUM|COUNT|MAX|MIN|UPPER)\([^)]+\)(\s*AS\s+\w+)\s*,?/i', '', $v); |
|
| 629 | + // idem sans AS (fetch numerique) |
|
| 630 | + $v = preg_replace('/(SUM|COUNT|MAX|MIN|UPPER)\([^)]+\)\s*,?/i', '', $v); |
|
| 631 | + // des AS simples : on garde le cote droit du AS |
|
| 632 | + $v = preg_replace('/^.*\sAS\s+(\w+)\s*$/i', '\\1', $v); |
|
| 633 | + // ne reste plus que les vrais colonnes, ou des constantes a virer |
|
| 634 | + if (preg_match(',^[\'"],', $v) or is_numeric($v)) { |
|
| 635 | + unset($join[$k]); |
|
| 636 | + } else { |
|
| 637 | + $join[$k] = trim($v); |
|
| 638 | + } |
|
| 639 | + } |
|
| 640 | + $join = array_diff($join, ['']); |
|
| 641 | + $join = implode(',', $join); |
|
| 642 | + } |
|
| 643 | + if (is_array($groupby)) { |
|
| 644 | + $groupby = join(',', $groupby); |
|
| 645 | + } |
|
| 646 | + if ($join) { |
|
| 647 | + $groupby = $groupby ? "$groupby, $join" : $join; |
|
| 648 | + } |
|
| 649 | + if (!$groupby) { |
|
| 650 | + return ''; |
|
| 651 | + } |
|
| 652 | + |
|
| 653 | + $groupby = spip_pg_frommysql($groupby); |
|
| 654 | + // Ne pas mettre dans le Group-By des valeurs numeriques |
|
| 655 | + // issue de prepare_recherche |
|
| 656 | + $groupby = preg_replace('/^\s*\d+\s+AS\s+\w+\s*,?\s*/i', '', $groupby); |
|
| 657 | + $groupby = preg_replace('/,\s*\d+\s+AS\s+\w+\s*/i', '', $groupby); |
|
| 658 | + $groupby = preg_replace('/\s+AS\s+\w+\s*/i', '', $groupby); |
|
| 659 | + |
|
| 660 | + return "\nGROUP BY $groupby"; |
|
| 661 | 661 | } |
| 662 | 662 | |
| 663 | 663 | // Conversion des operateurs MySQL en PG |
@@ -668,492 +668,492 @@ discard block |
||
| 668 | 668 | // A ameliorer. |
| 669 | 669 | |
| 670 | 670 | function spip_pg_frommysql($arg) { |
| 671 | - if (is_array($arg)) { |
|
| 672 | - $arg = join(', ', $arg); |
|
| 673 | - } |
|
| 674 | - |
|
| 675 | - $res = spip_pg_fromfield($arg); |
|
| 676 | - |
|
| 677 | - $res = preg_replace('/\brand[(][)]/i', 'random()', $res); |
|
| 678 | - |
|
| 679 | - $res = preg_replace( |
|
| 680 | - '/\b0\.0[+]([a-zA-Z0-9_.]+)\s*/', |
|
| 681 | - 'CAST(substring(\1, \'^ *[0-9.]+\') as float)', |
|
| 682 | - $res |
|
| 683 | - ); |
|
| 684 | - $res = preg_replace( |
|
| 685 | - '/\b0[+]([a-zA-Z0-9_.]+)\s*/', |
|
| 686 | - 'CAST(substring(\1, \'^ *[0-9]+\') as int)', |
|
| 687 | - $res |
|
| 688 | - ); |
|
| 689 | - $res = preg_replace( |
|
| 690 | - '/\bconv[(]([^,]*)[^)]*[)]/i', |
|
| 691 | - 'CAST(substring(\1, \'^ *[0-9]+\') as int)', |
|
| 692 | - $res |
|
| 693 | - ); |
|
| 694 | - |
|
| 695 | - $res = preg_replace( |
|
| 696 | - '/UNIX_TIMESTAMP\s*[(]\s*[)]/', |
|
| 697 | - ' EXTRACT(epoch FROM NOW())', |
|
| 698 | - $res |
|
| 699 | - ); |
|
| 700 | - |
|
| 701 | - // la fonction md5(integer) n'est pas connu en pg |
|
| 702 | - // il faut donc forcer les types en text (cas de md5(id_article)) |
|
| 703 | - $res = preg_replace( |
|
| 704 | - '/md5\s*[(]([^)]*)[)]/i', |
|
| 705 | - 'MD5(CAST(\1 AS text))', |
|
| 706 | - $res |
|
| 707 | - ); |
|
| 708 | - |
|
| 709 | - $res = preg_replace( |
|
| 710 | - '/UNIX_TIMESTAMP\s*[(]([^)]*)[)]/', |
|
| 711 | - ' EXTRACT(epoch FROM \1)', |
|
| 712 | - $res |
|
| 713 | - ); |
|
| 714 | - |
|
| 715 | - $res = preg_replace( |
|
| 716 | - '/\bDAYOFMONTH\s*[(]([^()]*([(][^()]*[)][^()]*)*[^)]*)[)]/', |
|
| 717 | - ' EXTRACT(day FROM \1)', |
|
| 718 | - $res |
|
| 719 | - ); |
|
| 720 | - |
|
| 721 | - $res = preg_replace( |
|
| 722 | - '/\bMONTH\s*[(]([^()]*([(][^)]*[)][^()]*)*[^)]*)[)]/', |
|
| 723 | - ' EXTRACT(month FROM \1)', |
|
| 724 | - $res |
|
| 725 | - ); |
|
| 726 | - |
|
| 727 | - $res = preg_replace( |
|
| 728 | - '/\bYEAR\s*[(]([^()]*([(][^)]*[)][^()]*)*[^)]*)[)]/', |
|
| 729 | - ' EXTRACT(year FROM \1)', |
|
| 730 | - $res |
|
| 731 | - ); |
|
| 732 | - |
|
| 733 | - $res = preg_replace( |
|
| 734 | - '/TO_DAYS\s*[(]([^()]*([(][^)]*[)][()]*)*)[)]/', |
|
| 735 | - ' EXTRACT(day FROM \1 - \'0001-01-01\')', |
|
| 736 | - $res |
|
| 737 | - ); |
|
| 738 | - |
|
| 739 | - $res = preg_replace('/(EXTRACT[(][^ ]* FROM *)"([^"]*)"/', '\1\'\2\'', $res); |
|
| 740 | - |
|
| 741 | - $res = preg_replace('/DATE_FORMAT\s*[(]([^,]*),\s*\'%Y%m%d\'[)]/', 'to_char(\1, \'YYYYMMDD\')', $res); |
|
| 742 | - |
|
| 743 | - $res = preg_replace('/DATE_FORMAT\s*[(]([^,]*),\s*\'%Y%m\'[)]/', 'to_char(\1, \'YYYYMM\')', $res); |
|
| 744 | - |
|
| 745 | - $res = preg_replace('/DATE_SUB\s*[(]([^,]*),/', '(\1 -', $res); |
|
| 746 | - $res = preg_replace('/DATE_ADD\s*[(]([^,]*),/', '(\1 +', $res); |
|
| 747 | - $res = preg_replace('/INTERVAL\s+(\d+\s+\w+)/', 'INTERVAL \'\1\'', $res); |
|
| 748 | - $res = preg_replace('/([+<>-]=?)\s*(\'\d+-\d+-\d+\s+\d+:\d+(:\d+)\')/', '\1 timestamp \2', $res); |
|
| 749 | - $res = preg_replace('/(\'\d+-\d+-\d+\s+\d+:\d+:\d+\')\s*([+<>-]=?)/', 'timestamp \1 \2', $res); |
|
| 750 | - |
|
| 751 | - $res = preg_replace('/([+<>-]=?)\s*(\'\d+-\d+-\d+\')/', '\1 timestamp \2', $res); |
|
| 752 | - $res = preg_replace('/(\'\d+-\d+-\d+\')\s*([+<>-]=?)/', 'timestamp \1 \2', $res); |
|
| 753 | - |
|
| 754 | - $res = preg_replace('/(timestamp .\d+)-00-/', '\1-01-', $res); |
|
| 755 | - $res = preg_replace('/(timestamp .\d+-\d+)-00/', '\1-01', $res); |
|
| 671 | + if (is_array($arg)) { |
|
| 672 | + $arg = join(', ', $arg); |
|
| 673 | + } |
|
| 674 | + |
|
| 675 | + $res = spip_pg_fromfield($arg); |
|
| 676 | + |
|
| 677 | + $res = preg_replace('/\brand[(][)]/i', 'random()', $res); |
|
| 678 | + |
|
| 679 | + $res = preg_replace( |
|
| 680 | + '/\b0\.0[+]([a-zA-Z0-9_.]+)\s*/', |
|
| 681 | + 'CAST(substring(\1, \'^ *[0-9.]+\') as float)', |
|
| 682 | + $res |
|
| 683 | + ); |
|
| 684 | + $res = preg_replace( |
|
| 685 | + '/\b0[+]([a-zA-Z0-9_.]+)\s*/', |
|
| 686 | + 'CAST(substring(\1, \'^ *[0-9]+\') as int)', |
|
| 687 | + $res |
|
| 688 | + ); |
|
| 689 | + $res = preg_replace( |
|
| 690 | + '/\bconv[(]([^,]*)[^)]*[)]/i', |
|
| 691 | + 'CAST(substring(\1, \'^ *[0-9]+\') as int)', |
|
| 692 | + $res |
|
| 693 | + ); |
|
| 694 | + |
|
| 695 | + $res = preg_replace( |
|
| 696 | + '/UNIX_TIMESTAMP\s*[(]\s*[)]/', |
|
| 697 | + ' EXTRACT(epoch FROM NOW())', |
|
| 698 | + $res |
|
| 699 | + ); |
|
| 700 | + |
|
| 701 | + // la fonction md5(integer) n'est pas connu en pg |
|
| 702 | + // il faut donc forcer les types en text (cas de md5(id_article)) |
|
| 703 | + $res = preg_replace( |
|
| 704 | + '/md5\s*[(]([^)]*)[)]/i', |
|
| 705 | + 'MD5(CAST(\1 AS text))', |
|
| 706 | + $res |
|
| 707 | + ); |
|
| 708 | + |
|
| 709 | + $res = preg_replace( |
|
| 710 | + '/UNIX_TIMESTAMP\s*[(]([^)]*)[)]/', |
|
| 711 | + ' EXTRACT(epoch FROM \1)', |
|
| 712 | + $res |
|
| 713 | + ); |
|
| 714 | + |
|
| 715 | + $res = preg_replace( |
|
| 716 | + '/\bDAYOFMONTH\s*[(]([^()]*([(][^()]*[)][^()]*)*[^)]*)[)]/', |
|
| 717 | + ' EXTRACT(day FROM \1)', |
|
| 718 | + $res |
|
| 719 | + ); |
|
| 720 | + |
|
| 721 | + $res = preg_replace( |
|
| 722 | + '/\bMONTH\s*[(]([^()]*([(][^)]*[)][^()]*)*[^)]*)[)]/', |
|
| 723 | + ' EXTRACT(month FROM \1)', |
|
| 724 | + $res |
|
| 725 | + ); |
|
| 726 | + |
|
| 727 | + $res = preg_replace( |
|
| 728 | + '/\bYEAR\s*[(]([^()]*([(][^)]*[)][^()]*)*[^)]*)[)]/', |
|
| 729 | + ' EXTRACT(year FROM \1)', |
|
| 730 | + $res |
|
| 731 | + ); |
|
| 732 | + |
|
| 733 | + $res = preg_replace( |
|
| 734 | + '/TO_DAYS\s*[(]([^()]*([(][^)]*[)][()]*)*)[)]/', |
|
| 735 | + ' EXTRACT(day FROM \1 - \'0001-01-01\')', |
|
| 736 | + $res |
|
| 737 | + ); |
|
| 738 | + |
|
| 739 | + $res = preg_replace('/(EXTRACT[(][^ ]* FROM *)"([^"]*)"/', '\1\'\2\'', $res); |
|
| 740 | + |
|
| 741 | + $res = preg_replace('/DATE_FORMAT\s*[(]([^,]*),\s*\'%Y%m%d\'[)]/', 'to_char(\1, \'YYYYMMDD\')', $res); |
|
| 742 | + |
|
| 743 | + $res = preg_replace('/DATE_FORMAT\s*[(]([^,]*),\s*\'%Y%m\'[)]/', 'to_char(\1, \'YYYYMM\')', $res); |
|
| 744 | + |
|
| 745 | + $res = preg_replace('/DATE_SUB\s*[(]([^,]*),/', '(\1 -', $res); |
|
| 746 | + $res = preg_replace('/DATE_ADD\s*[(]([^,]*),/', '(\1 +', $res); |
|
| 747 | + $res = preg_replace('/INTERVAL\s+(\d+\s+\w+)/', 'INTERVAL \'\1\'', $res); |
|
| 748 | + $res = preg_replace('/([+<>-]=?)\s*(\'\d+-\d+-\d+\s+\d+:\d+(:\d+)\')/', '\1 timestamp \2', $res); |
|
| 749 | + $res = preg_replace('/(\'\d+-\d+-\d+\s+\d+:\d+:\d+\')\s*([+<>-]=?)/', 'timestamp \1 \2', $res); |
|
| 750 | + |
|
| 751 | + $res = preg_replace('/([+<>-]=?)\s*(\'\d+-\d+-\d+\')/', '\1 timestamp \2', $res); |
|
| 752 | + $res = preg_replace('/(\'\d+-\d+-\d+\')\s*([+<>-]=?)/', 'timestamp \1 \2', $res); |
|
| 753 | + |
|
| 754 | + $res = preg_replace('/(timestamp .\d+)-00-/', '\1-01-', $res); |
|
| 755 | + $res = preg_replace('/(timestamp .\d+-\d+)-00/', '\1-01', $res); |
|
| 756 | 756 | # correct en theorie mais produit des debordements arithmetiques |
| 757 | 757 | # $res = preg_replace("/(EXTRACT[(][^ ]* FROM *)(timestamp *'[^']*' *[+-] *timestamp *'[^']*') *[)]/", '\2', $res); |
| 758 | - $res = preg_replace("/(EXTRACT[(][^ ]* FROM *)('[^']*')/", '\1 timestamp \2', $res); |
|
| 759 | - $res = preg_replace('/\sLIKE\s+/', ' ILIKE ', $res); |
|
| 758 | + $res = preg_replace("/(EXTRACT[(][^ ]* FROM *)('[^']*')/", '\1 timestamp \2', $res); |
|
| 759 | + $res = preg_replace('/\sLIKE\s+/', ' ILIKE ', $res); |
|
| 760 | 760 | |
| 761 | - return str_replace('REGEXP', '~', $res); |
|
| 761 | + return str_replace('REGEXP', '~', $res); |
|
| 762 | 762 | } |
| 763 | 763 | |
| 764 | 764 | function spip_pg_fromfield($arg) { |
| 765 | - while (preg_match('/^(.*?)FIELD\s*\(([^,]*)((,[^,)]*)*)\)/', $arg, $m)) { |
|
| 766 | - preg_match_all('/,([^,]*)/', $m[3], $r, PREG_PATTERN_ORDER); |
|
| 767 | - $res = ''; |
|
| 768 | - $n = 0; |
|
| 769 | - $index = $m[2]; |
|
| 770 | - foreach ($r[1] as $v) { |
|
| 771 | - $n++; |
|
| 772 | - $res .= "\nwhen $index=$v then $n"; |
|
| 773 | - } |
|
| 774 | - $arg = $m[1] . "case $res else 0 end " |
|
| 775 | - . substr($arg, strlen($m[0])); |
|
| 776 | - } |
|
| 777 | - |
|
| 778 | - return $arg; |
|
| 765 | + while (preg_match('/^(.*?)FIELD\s*\(([^,]*)((,[^,)]*)*)\)/', $arg, $m)) { |
|
| 766 | + preg_match_all('/,([^,]*)/', $m[3], $r, PREG_PATTERN_ORDER); |
|
| 767 | + $res = ''; |
|
| 768 | + $n = 0; |
|
| 769 | + $index = $m[2]; |
|
| 770 | + foreach ($r[1] as $v) { |
|
| 771 | + $n++; |
|
| 772 | + $res .= "\nwhen $index=$v then $n"; |
|
| 773 | + } |
|
| 774 | + $arg = $m[1] . "case $res else 0 end " |
|
| 775 | + . substr($arg, strlen($m[0])); |
|
| 776 | + } |
|
| 777 | + |
|
| 778 | + return $arg; |
|
| 779 | 779 | } |
| 780 | 780 | |
| 781 | 781 | function calculer_pg_where($v) { |
| 782 | - if (!is_array($v)) { |
|
| 783 | - return spip_pg_frommysql($v); |
|
| 784 | - } |
|
| 785 | - |
|
| 786 | - $op = str_replace('REGEXP', '~', array_shift($v)); |
|
| 787 | - if (!($n = count($v))) { |
|
| 788 | - return $op; |
|
| 789 | - } else { |
|
| 790 | - $arg = calculer_pg_where(array_shift($v)); |
|
| 791 | - if ($n == 1) { |
|
| 792 | - return "$op($arg)"; |
|
| 793 | - } else { |
|
| 794 | - $arg2 = calculer_pg_where(array_shift($v)); |
|
| 795 | - if ($n == 2) { |
|
| 796 | - return "($arg $op $arg2)"; |
|
| 797 | - } else { |
|
| 798 | - return "($arg $op ($arg2) : $v[0])"; |
|
| 799 | - } |
|
| 800 | - } |
|
| 801 | - } |
|
| 782 | + if (!is_array($v)) { |
|
| 783 | + return spip_pg_frommysql($v); |
|
| 784 | + } |
|
| 785 | + |
|
| 786 | + $op = str_replace('REGEXP', '~', array_shift($v)); |
|
| 787 | + if (!($n = count($v))) { |
|
| 788 | + return $op; |
|
| 789 | + } else { |
|
| 790 | + $arg = calculer_pg_where(array_shift($v)); |
|
| 791 | + if ($n == 1) { |
|
| 792 | + return "$op($arg)"; |
|
| 793 | + } else { |
|
| 794 | + $arg2 = calculer_pg_where(array_shift($v)); |
|
| 795 | + if ($n == 2) { |
|
| 796 | + return "($arg $op $arg2)"; |
|
| 797 | + } else { |
|
| 798 | + return "($arg $op ($arg2) : $v[0])"; |
|
| 799 | + } |
|
| 800 | + } |
|
| 801 | + } |
|
| 802 | 802 | } |
| 803 | 803 | |
| 804 | 804 | |
| 805 | 805 | function calculer_pg_expression($expression, $v, $join = 'AND') { |
| 806 | - if (empty($v)) { |
|
| 807 | - return ''; |
|
| 808 | - } |
|
| 806 | + if (empty($v)) { |
|
| 807 | + return ''; |
|
| 808 | + } |
|
| 809 | 809 | |
| 810 | - $exp = "\n$expression "; |
|
| 810 | + $exp = "\n$expression "; |
|
| 811 | 811 | |
| 812 | - if (!is_array($v)) { |
|
| 813 | - $v = [$v]; |
|
| 814 | - } |
|
| 812 | + if (!is_array($v)) { |
|
| 813 | + $v = [$v]; |
|
| 814 | + } |
|
| 815 | 815 | |
| 816 | - if (strtoupper($join) === 'AND') { |
|
| 817 | - return $exp . join("\n\t$join ", array_map('calculer_pg_where', $v)); |
|
| 818 | - } else { |
|
| 819 | - return $exp . join($join, $v); |
|
| 820 | - } |
|
| 816 | + if (strtoupper($join) === 'AND') { |
|
| 817 | + return $exp . join("\n\t$join ", array_map('calculer_pg_where', $v)); |
|
| 818 | + } else { |
|
| 819 | + return $exp . join($join, $v); |
|
| 820 | + } |
|
| 821 | 821 | } |
| 822 | 822 | |
| 823 | 823 | function spip_pg_select_as($args) { |
| 824 | - $argsas = ''; |
|
| 825 | - foreach ($args as $k => $v) { |
|
| 826 | - if (substr($k, -1) == '@') { |
|
| 827 | - // c'est une jointure qui se refere au from precedent |
|
| 828 | - // pas de virgule |
|
| 829 | - $argsas .= ' ' . $v; |
|
| 830 | - } else { |
|
| 831 | - $as = ''; |
|
| 832 | - // spip_log("$k : $v", _LOG_DEBUG); |
|
| 833 | - if (!is_numeric($k)) { |
|
| 834 | - if (preg_match('/\.(.*)$/', $k, $r)) { |
|
| 835 | - $v = $k; |
|
| 836 | - } elseif ($v != $k) { |
|
| 837 | - $p = strpos($v, ' '); |
|
| 838 | - if ($p) { |
|
| 839 | - $v = substr($v, 0, $p) . " AS $k" . substr($v, $p); |
|
| 840 | - } else { |
|
| 841 | - $as = " AS $k"; |
|
| 842 | - } |
|
| 843 | - } |
|
| 844 | - } |
|
| 845 | - // spip_log("subs $k : $v avec $as", _LOG_DEBUG); |
|
| 846 | - // if (strpos($v, 'JOIN') === false) $argsas .= ', '; |
|
| 847 | - $argsas .= ', ' . $v . $as; |
|
| 848 | - } |
|
| 849 | - } |
|
| 850 | - |
|
| 851 | - return substr($argsas, 2); |
|
| 824 | + $argsas = ''; |
|
| 825 | + foreach ($args as $k => $v) { |
|
| 826 | + if (substr($k, -1) == '@') { |
|
| 827 | + // c'est une jointure qui se refere au from precedent |
|
| 828 | + // pas de virgule |
|
| 829 | + $argsas .= ' ' . $v; |
|
| 830 | + } else { |
|
| 831 | + $as = ''; |
|
| 832 | + // spip_log("$k : $v", _LOG_DEBUG); |
|
| 833 | + if (!is_numeric($k)) { |
|
| 834 | + if (preg_match('/\.(.*)$/', $k, $r)) { |
|
| 835 | + $v = $k; |
|
| 836 | + } elseif ($v != $k) { |
|
| 837 | + $p = strpos($v, ' '); |
|
| 838 | + if ($p) { |
|
| 839 | + $v = substr($v, 0, $p) . " AS $k" . substr($v, $p); |
|
| 840 | + } else { |
|
| 841 | + $as = " AS $k"; |
|
| 842 | + } |
|
| 843 | + } |
|
| 844 | + } |
|
| 845 | + // spip_log("subs $k : $v avec $as", _LOG_DEBUG); |
|
| 846 | + // if (strpos($v, 'JOIN') === false) $argsas .= ', '; |
|
| 847 | + $argsas .= ', ' . $v . $as; |
|
| 848 | + } |
|
| 849 | + } |
|
| 850 | + |
|
| 851 | + return substr($argsas, 2); |
|
| 852 | 852 | } |
| 853 | 853 | |
| 854 | 854 | function spip_pg_fetch($res, $t = '', $serveur = '', $requeter = true) { |
| 855 | 855 | |
| 856 | - if ($res) { |
|
| 857 | - $res = pg_fetch_array($res, null, PGSQL_ASSOC); |
|
| 858 | - } |
|
| 856 | + if ($res) { |
|
| 857 | + $res = pg_fetch_array($res, null, PGSQL_ASSOC); |
|
| 858 | + } |
|
| 859 | 859 | |
| 860 | - return $res; |
|
| 860 | + return $res; |
|
| 861 | 861 | } |
| 862 | 862 | |
| 863 | 863 | function spip_pg_seek($r, $row_number, $serveur = '', $requeter = true) { |
| 864 | - if ($r) { |
|
| 865 | - return pg_result_seek($r, $row_number); |
|
| 866 | - } |
|
| 864 | + if ($r) { |
|
| 865 | + return pg_result_seek($r, $row_number); |
|
| 866 | + } |
|
| 867 | 867 | } |
| 868 | 868 | |
| 869 | 869 | |
| 870 | 870 | function spip_pg_countsel( |
| 871 | - $from = [], |
|
| 872 | - $where = [], |
|
| 873 | - $groupby = [], |
|
| 874 | - $having = [], |
|
| 875 | - $serveur = '', |
|
| 876 | - $requeter = true |
|
| 871 | + $from = [], |
|
| 872 | + $where = [], |
|
| 873 | + $groupby = [], |
|
| 874 | + $having = [], |
|
| 875 | + $serveur = '', |
|
| 876 | + $requeter = true |
|
| 877 | 877 | ) { |
| 878 | - $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 879 | - $r = spip_pg_select("COUNT($c)", $from, $where, '', '', '', $having, $serveur, $requeter); |
|
| 880 | - if (!$requeter) { |
|
| 881 | - return $r; |
|
| 882 | - } |
|
| 883 | - if (!is_resource($r)) { |
|
| 884 | - return 0; |
|
| 885 | - } |
|
| 886 | - [$c] = pg_fetch_array($r, null, PGSQL_NUM); |
|
| 887 | - |
|
| 888 | - return $c; |
|
| 878 | + $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 879 | + $r = spip_pg_select("COUNT($c)", $from, $where, '', '', '', $having, $serveur, $requeter); |
|
| 880 | + if (!$requeter) { |
|
| 881 | + return $r; |
|
| 882 | + } |
|
| 883 | + if (!is_resource($r)) { |
|
| 884 | + return 0; |
|
| 885 | + } |
|
| 886 | + [$c] = pg_fetch_array($r, null, PGSQL_NUM); |
|
| 887 | + |
|
| 888 | + return $c; |
|
| 889 | 889 | } |
| 890 | 890 | |
| 891 | 891 | function spip_pg_count($res, $serveur = '', $requeter = true) { |
| 892 | - return !$res ? 0 : pg_numrows($res); |
|
| 892 | + return !$res ? 0 : pg_numrows($res); |
|
| 893 | 893 | } |
| 894 | 894 | |
| 895 | 895 | function spip_pg_free($res, $serveur = '', $requeter = true) { |
| 896 | - // rien a faire en postgres |
|
| 896 | + // rien a faire en postgres |
|
| 897 | 897 | } |
| 898 | 898 | |
| 899 | 899 | function spip_pg_delete($table, $where = '', $serveur = '', $requeter = true) { |
| 900 | 900 | |
| 901 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 902 | - $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 901 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 902 | + $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 903 | 903 | |
| 904 | - $query = calculer_pg_expression('DELETE FROM', $table, ',') |
|
| 905 | - . calculer_pg_expression('WHERE', $where, 'AND'); |
|
| 904 | + $query = calculer_pg_expression('DELETE FROM', $table, ',') |
|
| 905 | + . calculer_pg_expression('WHERE', $where, 'AND'); |
|
| 906 | 906 | |
| 907 | - // renvoyer la requete inerte si demandee |
|
| 908 | - if (!$requeter) { |
|
| 909 | - return $query; |
|
| 910 | - } |
|
| 907 | + // renvoyer la requete inerte si demandee |
|
| 908 | + if (!$requeter) { |
|
| 909 | + return $query; |
|
| 910 | + } |
|
| 911 | 911 | |
| 912 | - $res = spip_pg_trace_query($query, $serveur); |
|
| 913 | - if ($res) { |
|
| 914 | - return pg_affected_rows($res); |
|
| 915 | - } else { |
|
| 916 | - return false; |
|
| 917 | - } |
|
| 912 | + $res = spip_pg_trace_query($query, $serveur); |
|
| 913 | + if ($res) { |
|
| 914 | + return pg_affected_rows($res); |
|
| 915 | + } else { |
|
| 916 | + return false; |
|
| 917 | + } |
|
| 918 | 918 | } |
| 919 | 919 | |
| 920 | 920 | function spip_pg_insert($table, $champs, $valeurs, $desc = [], $serveur = '', $requeter = true) { |
| 921 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 922 | - $prefixe = $connexion['prefixe']; |
|
| 923 | - $link = $connexion['link']; |
|
| 924 | - |
|
| 925 | - if (!$desc) { |
|
| 926 | - $desc = description_table($table, $serveur); |
|
| 927 | - } |
|
| 928 | - $seq = spip_pg_sequence($table, true); |
|
| 929 | - // si pas de cle primaire dans l'insertion, renvoyer curval |
|
| 930 | - if (!preg_match(",\b$seq\b,", $champs)) { |
|
| 931 | - $seq = spip_pg_sequence($table); |
|
| 932 | - $seq = prefixer_table_spip($seq, $prefixe); |
|
| 933 | - $seq = "currval('$seq')"; |
|
| 934 | - } |
|
| 935 | - |
|
| 936 | - $table = prefixer_table_spip($table, $prefixe); |
|
| 937 | - $ret = !$seq ? '' : (" RETURNING $seq"); |
|
| 938 | - $ins = (strlen($champs) < 3) |
|
| 939 | - ? ' DEFAULT VALUES' |
|
| 940 | - : "$champs VALUES $valeurs"; |
|
| 941 | - $q = "INSERT INTO $table $ins $ret"; |
|
| 942 | - if (!$requeter) { |
|
| 943 | - return $q; |
|
| 944 | - } |
|
| 945 | - $connexion['last'] = $q; |
|
| 946 | - $r = spip_pg_query_simple($link, $q); |
|
| 921 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 922 | + $prefixe = $connexion['prefixe']; |
|
| 923 | + $link = $connexion['link']; |
|
| 924 | + |
|
| 925 | + if (!$desc) { |
|
| 926 | + $desc = description_table($table, $serveur); |
|
| 927 | + } |
|
| 928 | + $seq = spip_pg_sequence($table, true); |
|
| 929 | + // si pas de cle primaire dans l'insertion, renvoyer curval |
|
| 930 | + if (!preg_match(",\b$seq\b,", $champs)) { |
|
| 931 | + $seq = spip_pg_sequence($table); |
|
| 932 | + $seq = prefixer_table_spip($seq, $prefixe); |
|
| 933 | + $seq = "currval('$seq')"; |
|
| 934 | + } |
|
| 935 | + |
|
| 936 | + $table = prefixer_table_spip($table, $prefixe); |
|
| 937 | + $ret = !$seq ? '' : (" RETURNING $seq"); |
|
| 938 | + $ins = (strlen($champs) < 3) |
|
| 939 | + ? ' DEFAULT VALUES' |
|
| 940 | + : "$champs VALUES $valeurs"; |
|
| 941 | + $q = "INSERT INTO $table $ins $ret"; |
|
| 942 | + if (!$requeter) { |
|
| 943 | + return $q; |
|
| 944 | + } |
|
| 945 | + $connexion['last'] = $q; |
|
| 946 | + $r = spip_pg_query_simple($link, $q); |
|
| 947 | 947 | # spip_log($q,'pg.'._LOG_DEBUG); |
| 948 | - if ($r) { |
|
| 949 | - if (!$ret) { |
|
| 950 | - return 0; |
|
| 951 | - } |
|
| 952 | - if ($r2 = pg_fetch_array($r, null, PGSQL_NUM)) { |
|
| 953 | - return $r2[0]; |
|
| 954 | - } |
|
| 955 | - } |
|
| 956 | - |
|
| 957 | - return false; |
|
| 948 | + if ($r) { |
|
| 949 | + if (!$ret) { |
|
| 950 | + return 0; |
|
| 951 | + } |
|
| 952 | + if ($r2 = pg_fetch_array($r, null, PGSQL_NUM)) { |
|
| 953 | + return $r2[0]; |
|
| 954 | + } |
|
| 955 | + } |
|
| 956 | + |
|
| 957 | + return false; |
|
| 958 | 958 | } |
| 959 | 959 | |
| 960 | 960 | function spip_pg_insertq($table, $couples = [], $desc = [], $serveur = '', $requeter = true) { |
| 961 | 961 | |
| 962 | - if (!$desc) { |
|
| 963 | - $desc = description_table($table, $serveur); |
|
| 964 | - } |
|
| 965 | - if (!$desc) { |
|
| 966 | - die("$table insertion sans description"); |
|
| 967 | - } |
|
| 968 | - $fields = $desc['field']; |
|
| 969 | - |
|
| 970 | - foreach ($couples as $champ => $val) { |
|
| 971 | - $couples[$champ] = spip_pg_cite($val, $fields[$champ]); |
|
| 972 | - } |
|
| 973 | - |
|
| 974 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 975 | - $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 976 | - |
|
| 977 | - return spip_pg_insert( |
|
| 978 | - $table, |
|
| 979 | - '(' . join(',', array_keys($couples)) . ')', |
|
| 980 | - '(' . join(',', $couples) . ')', |
|
| 981 | - $desc, |
|
| 982 | - $serveur, |
|
| 983 | - $requeter |
|
| 984 | - ); |
|
| 962 | + if (!$desc) { |
|
| 963 | + $desc = description_table($table, $serveur); |
|
| 964 | + } |
|
| 965 | + if (!$desc) { |
|
| 966 | + die("$table insertion sans description"); |
|
| 967 | + } |
|
| 968 | + $fields = $desc['field']; |
|
| 969 | + |
|
| 970 | + foreach ($couples as $champ => $val) { |
|
| 971 | + $couples[$champ] = spip_pg_cite($val, $fields[$champ]); |
|
| 972 | + } |
|
| 973 | + |
|
| 974 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 975 | + $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 976 | + |
|
| 977 | + return spip_pg_insert( |
|
| 978 | + $table, |
|
| 979 | + '(' . join(',', array_keys($couples)) . ')', |
|
| 980 | + '(' . join(',', $couples) . ')', |
|
| 981 | + $desc, |
|
| 982 | + $serveur, |
|
| 983 | + $requeter |
|
| 984 | + ); |
|
| 985 | 985 | } |
| 986 | 986 | |
| 987 | 987 | |
| 988 | 988 | function spip_pg_insertq_multi($table, $tab_couples = [], $desc = [], $serveur = '', $requeter = true) { |
| 989 | 989 | |
| 990 | - if (!$desc) { |
|
| 991 | - $desc = description_table($table, $serveur); |
|
| 992 | - } |
|
| 993 | - if (!$desc) { |
|
| 994 | - die("$table insertion sans description"); |
|
| 995 | - } |
|
| 996 | - $fields = $desc['field'] ?? []; |
|
| 997 | - |
|
| 998 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 999 | - // une premiere fois pour ajouter maj dans les cles |
|
| 1000 | - $c = $tab_couples[0] ?? []; |
|
| 1001 | - $les_cles = spip_pg_ajouter_champs_timestamp($table, $c, $desc, $serveur); |
|
| 1002 | - |
|
| 1003 | - $cles = '(' . join(',', array_keys($les_cles)) . ')'; |
|
| 1004 | - $valeurs = []; |
|
| 1005 | - foreach ($tab_couples as $couples) { |
|
| 1006 | - foreach ($couples as $champ => $val) { |
|
| 1007 | - $couples[$champ] = spip_pg_cite($val, $fields[$champ]); |
|
| 1008 | - } |
|
| 1009 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1010 | - $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1011 | - |
|
| 1012 | - $valeurs[] = '(' . join(',', $couples) . ')'; |
|
| 1013 | - } |
|
| 1014 | - $valeurs = implode(', ', $valeurs); |
|
| 1015 | - |
|
| 1016 | - return spip_pg_insert($table, $cles, $valeurs, $desc, $serveur, $requeter); |
|
| 990 | + if (!$desc) { |
|
| 991 | + $desc = description_table($table, $serveur); |
|
| 992 | + } |
|
| 993 | + if (!$desc) { |
|
| 994 | + die("$table insertion sans description"); |
|
| 995 | + } |
|
| 996 | + $fields = $desc['field'] ?? []; |
|
| 997 | + |
|
| 998 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 999 | + // une premiere fois pour ajouter maj dans les cles |
|
| 1000 | + $c = $tab_couples[0] ?? []; |
|
| 1001 | + $les_cles = spip_pg_ajouter_champs_timestamp($table, $c, $desc, $serveur); |
|
| 1002 | + |
|
| 1003 | + $cles = '(' . join(',', array_keys($les_cles)) . ')'; |
|
| 1004 | + $valeurs = []; |
|
| 1005 | + foreach ($tab_couples as $couples) { |
|
| 1006 | + foreach ($couples as $champ => $val) { |
|
| 1007 | + $couples[$champ] = spip_pg_cite($val, $fields[$champ]); |
|
| 1008 | + } |
|
| 1009 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1010 | + $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1011 | + |
|
| 1012 | + $valeurs[] = '(' . join(',', $couples) . ')'; |
|
| 1013 | + } |
|
| 1014 | + $valeurs = implode(', ', $valeurs); |
|
| 1015 | + |
|
| 1016 | + return spip_pg_insert($table, $cles, $valeurs, $desc, $serveur, $requeter); |
|
| 1017 | 1017 | } |
| 1018 | 1018 | |
| 1019 | 1019 | |
| 1020 | 1020 | function spip_pg_update($table, $couples, $where = '', $desc = '', $serveur = '', $requeter = true) { |
| 1021 | 1021 | |
| 1022 | - if (!$couples) { |
|
| 1023 | - return; |
|
| 1024 | - } |
|
| 1025 | - $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1026 | - $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 1022 | + if (!$couples) { |
|
| 1023 | + return; |
|
| 1024 | + } |
|
| 1025 | + $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1026 | + $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 1027 | 1027 | |
| 1028 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1029 | - $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1028 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1029 | + $couples = spip_pg_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1030 | 1030 | |
| 1031 | - $set = []; |
|
| 1032 | - foreach ($couples as $champ => $val) { |
|
| 1033 | - $set[] = $champ . '=' . $val; |
|
| 1034 | - } |
|
| 1031 | + $set = []; |
|
| 1032 | + foreach ($couples as $champ => $val) { |
|
| 1033 | + $set[] = $champ . '=' . $val; |
|
| 1034 | + } |
|
| 1035 | 1035 | |
| 1036 | - $query = calculer_pg_expression('UPDATE', $table, ',') |
|
| 1037 | - . calculer_pg_expression('SET', $set, ',') |
|
| 1038 | - . calculer_pg_expression('WHERE', $where, 'AND'); |
|
| 1036 | + $query = calculer_pg_expression('UPDATE', $table, ',') |
|
| 1037 | + . calculer_pg_expression('SET', $set, ',') |
|
| 1038 | + . calculer_pg_expression('WHERE', $where, 'AND'); |
|
| 1039 | 1039 | |
| 1040 | - // renvoyer la requete inerte si demandee |
|
| 1041 | - if (!$requeter) { |
|
| 1042 | - return $query; |
|
| 1043 | - } |
|
| 1040 | + // renvoyer la requete inerte si demandee |
|
| 1041 | + if (!$requeter) { |
|
| 1042 | + return $query; |
|
| 1043 | + } |
|
| 1044 | 1044 | |
| 1045 | - return spip_pg_trace_query($query, $serveur); |
|
| 1045 | + return spip_pg_trace_query($query, $serveur); |
|
| 1046 | 1046 | } |
| 1047 | 1047 | |
| 1048 | 1048 | // idem, mais les valeurs sont des constantes a mettre entre apostrophes |
| 1049 | 1049 | // sauf les expressions de date lorsqu'il s'agit de fonctions SQL (NOW etc) |
| 1050 | 1050 | function spip_pg_updateq($table, $couples, $where = '', $desc = [], $serveur = '', $requeter = true) { |
| 1051 | - if (!$couples) { |
|
| 1052 | - return; |
|
| 1053 | - } |
|
| 1054 | - if (!$desc) { |
|
| 1055 | - $desc = description_table($table, $serveur); |
|
| 1056 | - } |
|
| 1057 | - $fields = $desc['field']; |
|
| 1058 | - foreach ($couples as $k => $val) { |
|
| 1059 | - $couples[$k] = spip_pg_cite($val, $fields[$k]); |
|
| 1060 | - } |
|
| 1061 | - |
|
| 1062 | - return spip_pg_update($table, $couples, $where, $desc, $serveur, $requeter); |
|
| 1051 | + if (!$couples) { |
|
| 1052 | + return; |
|
| 1053 | + } |
|
| 1054 | + if (!$desc) { |
|
| 1055 | + $desc = description_table($table, $serveur); |
|
| 1056 | + } |
|
| 1057 | + $fields = $desc['field']; |
|
| 1058 | + foreach ($couples as $k => $val) { |
|
| 1059 | + $couples[$k] = spip_pg_cite($val, $fields[$k]); |
|
| 1060 | + } |
|
| 1061 | + |
|
| 1062 | + return spip_pg_update($table, $couples, $where, $desc, $serveur, $requeter); |
|
| 1063 | 1063 | } |
| 1064 | 1064 | |
| 1065 | 1065 | |
| 1066 | 1066 | function spip_pg_replace($table, $values, $desc, $serveur = '', $requeter = true) { |
| 1067 | - if (!$values) { |
|
| 1068 | - spip_log("replace vide $table", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1069 | - |
|
| 1070 | - return 0; |
|
| 1071 | - } |
|
| 1072 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1073 | - $prefixe = $connexion['prefixe']; |
|
| 1074 | - $link = $connexion['link']; |
|
| 1075 | - |
|
| 1076 | - if (!$desc) { |
|
| 1077 | - $desc = description_table($table, $serveur); |
|
| 1078 | - } |
|
| 1079 | - if (!$desc) { |
|
| 1080 | - die("$table insertion sans description"); |
|
| 1081 | - } |
|
| 1082 | - $prim = $desc['key']['PRIMARY KEY']; |
|
| 1083 | - $ids = preg_split('/,\s*/', $prim); |
|
| 1084 | - $noprims = $prims = []; |
|
| 1085 | - foreach ($values as $k => $v) { |
|
| 1086 | - $values[$k] = $v = spip_pg_cite($v, $desc['field'][$k]); |
|
| 1087 | - |
|
| 1088 | - if (!in_array($k, $ids)) { |
|
| 1089 | - $noprims[$k] = "$k=$v"; |
|
| 1090 | - } else { |
|
| 1091 | - $prims[$k] = "$k=$v"; |
|
| 1092 | - } |
|
| 1093 | - } |
|
| 1094 | - |
|
| 1095 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1096 | - $values = spip_pg_ajouter_champs_timestamp($table, $values, $desc, $serveur); |
|
| 1097 | - |
|
| 1098 | - $where = join(' AND ', $prims); |
|
| 1099 | - if (!$where) { |
|
| 1100 | - return spip_pg_insert( |
|
| 1101 | - $table, |
|
| 1102 | - '(' . join(',', array_keys($values)) . ')', |
|
| 1103 | - '(' . join(',', $values) . ')', |
|
| 1104 | - $desc, |
|
| 1105 | - $serveur |
|
| 1106 | - ); |
|
| 1107 | - } |
|
| 1108 | - $couples = join(',', $noprims); |
|
| 1109 | - |
|
| 1110 | - $seq = spip_pg_sequence($table); |
|
| 1111 | - $table = prefixer_table_spip($table, $prefixe); |
|
| 1112 | - $seq = prefixer_table_spip($seq, $prefixe); |
|
| 1113 | - |
|
| 1114 | - $connexion['last'] = $q = "UPDATE $table SET $couples WHERE $where"; |
|
| 1115 | - if ($couples) { |
|
| 1116 | - $couples = spip_pg_query_simple($link, $q); |
|
| 1067 | + if (!$values) { |
|
| 1068 | + spip_log("replace vide $table", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1069 | + |
|
| 1070 | + return 0; |
|
| 1071 | + } |
|
| 1072 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1073 | + $prefixe = $connexion['prefixe']; |
|
| 1074 | + $link = $connexion['link']; |
|
| 1075 | + |
|
| 1076 | + if (!$desc) { |
|
| 1077 | + $desc = description_table($table, $serveur); |
|
| 1078 | + } |
|
| 1079 | + if (!$desc) { |
|
| 1080 | + die("$table insertion sans description"); |
|
| 1081 | + } |
|
| 1082 | + $prim = $desc['key']['PRIMARY KEY']; |
|
| 1083 | + $ids = preg_split('/,\s*/', $prim); |
|
| 1084 | + $noprims = $prims = []; |
|
| 1085 | + foreach ($values as $k => $v) { |
|
| 1086 | + $values[$k] = $v = spip_pg_cite($v, $desc['field'][$k]); |
|
| 1087 | + |
|
| 1088 | + if (!in_array($k, $ids)) { |
|
| 1089 | + $noprims[$k] = "$k=$v"; |
|
| 1090 | + } else { |
|
| 1091 | + $prims[$k] = "$k=$v"; |
|
| 1092 | + } |
|
| 1093 | + } |
|
| 1094 | + |
|
| 1095 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1096 | + $values = spip_pg_ajouter_champs_timestamp($table, $values, $desc, $serveur); |
|
| 1097 | + |
|
| 1098 | + $where = join(' AND ', $prims); |
|
| 1099 | + if (!$where) { |
|
| 1100 | + return spip_pg_insert( |
|
| 1101 | + $table, |
|
| 1102 | + '(' . join(',', array_keys($values)) . ')', |
|
| 1103 | + '(' . join(',', $values) . ')', |
|
| 1104 | + $desc, |
|
| 1105 | + $serveur |
|
| 1106 | + ); |
|
| 1107 | + } |
|
| 1108 | + $couples = join(',', $noprims); |
|
| 1109 | + |
|
| 1110 | + $seq = spip_pg_sequence($table); |
|
| 1111 | + $table = prefixer_table_spip($table, $prefixe); |
|
| 1112 | + $seq = prefixer_table_spip($seq, $prefixe); |
|
| 1113 | + |
|
| 1114 | + $connexion['last'] = $q = "UPDATE $table SET $couples WHERE $where"; |
|
| 1115 | + if ($couples) { |
|
| 1116 | + $couples = spip_pg_query_simple($link, $q); |
|
| 1117 | 1117 | # spip_log($q,'pg.'._LOG_DEBUG); |
| 1118 | - if (!$couples) { |
|
| 1119 | - return false; |
|
| 1120 | - } |
|
| 1121 | - $couples = pg_affected_rows($couples); |
|
| 1122 | - } |
|
| 1123 | - if (!$couples) { |
|
| 1124 | - $ret = !$seq ? '' : |
|
| 1125 | - (" RETURNING nextval('$seq') < $prim"); |
|
| 1126 | - $connexion['last'] = $q = "INSERT INTO $table (" . join(',', array_keys($values)) . ') VALUES (' . join( |
|
| 1127 | - ',', |
|
| 1128 | - $values |
|
| 1129 | - ) . ")$ret"; |
|
| 1130 | - $couples = spip_pg_query_simple($link, $q); |
|
| 1131 | - if (!$couples) { |
|
| 1132 | - return false; |
|
| 1133 | - } elseif ($ret) { |
|
| 1134 | - $r = pg_fetch_array($couples, null, PGSQL_NUM); |
|
| 1135 | - if ($r[0]) { |
|
| 1136 | - $connexion['last'] = $q = "SELECT setval('$seq', $prim) from $table"; |
|
| 1137 | - // Le code de SPIP met parfois la sequence a 0 (dans l'import) |
|
| 1138 | - // MySQL n'en dit rien, on fait pareil pour PG |
|
| 1139 | - $r = @pg_query($link, $q); |
|
| 1140 | - } |
|
| 1141 | - } |
|
| 1142 | - } |
|
| 1143 | - |
|
| 1144 | - return $couples; |
|
| 1118 | + if (!$couples) { |
|
| 1119 | + return false; |
|
| 1120 | + } |
|
| 1121 | + $couples = pg_affected_rows($couples); |
|
| 1122 | + } |
|
| 1123 | + if (!$couples) { |
|
| 1124 | + $ret = !$seq ? '' : |
|
| 1125 | + (" RETURNING nextval('$seq') < $prim"); |
|
| 1126 | + $connexion['last'] = $q = "INSERT INTO $table (" . join(',', array_keys($values)) . ') VALUES (' . join( |
|
| 1127 | + ',', |
|
| 1128 | + $values |
|
| 1129 | + ) . ")$ret"; |
|
| 1130 | + $couples = spip_pg_query_simple($link, $q); |
|
| 1131 | + if (!$couples) { |
|
| 1132 | + return false; |
|
| 1133 | + } elseif ($ret) { |
|
| 1134 | + $r = pg_fetch_array($couples, null, PGSQL_NUM); |
|
| 1135 | + if ($r[0]) { |
|
| 1136 | + $connexion['last'] = $q = "SELECT setval('$seq', $prim) from $table"; |
|
| 1137 | + // Le code de SPIP met parfois la sequence a 0 (dans l'import) |
|
| 1138 | + // MySQL n'en dit rien, on fait pareil pour PG |
|
| 1139 | + $r = @pg_query($link, $q); |
|
| 1140 | + } |
|
| 1141 | + } |
|
| 1142 | + } |
|
| 1143 | + |
|
| 1144 | + return $couples; |
|
| 1145 | 1145 | } |
| 1146 | 1146 | |
| 1147 | 1147 | |
| 1148 | 1148 | function spip_pg_replace_multi($table, $tab_couples, $desc = [], $serveur = '', $requeter = true) { |
| 1149 | - $retour = null; |
|
| 1150 | - // boucler pour traiter chaque requete independemment |
|
| 1151 | - foreach ($tab_couples as $couples) { |
|
| 1152 | - $retour = spip_pg_replace($table, $couples, $desc, $serveur, $requeter); |
|
| 1153 | - } |
|
| 1154 | - |
|
| 1155 | - // renvoie le dernier id |
|
| 1156 | - return $retour; |
|
| 1149 | + $retour = null; |
|
| 1150 | + // boucler pour traiter chaque requete independemment |
|
| 1151 | + foreach ($tab_couples as $couples) { |
|
| 1152 | + $retour = spip_pg_replace($table, $couples, $desc, $serveur, $requeter); |
|
| 1153 | + } |
|
| 1154 | + |
|
| 1155 | + // renvoie le dernier id |
|
| 1156 | + return $retour; |
|
| 1157 | 1157 | } |
| 1158 | 1158 | |
| 1159 | 1159 | |
@@ -1162,149 +1162,149 @@ discard block |
||
| 1162 | 1162 | |
| 1163 | 1163 | function spip_pg_sequence($table, $raw = false) { |
| 1164 | 1164 | |
| 1165 | - include_spip('base/serial'); |
|
| 1166 | - if (!isset($GLOBALS['tables_principales'][$table])) { |
|
| 1167 | - return false; |
|
| 1168 | - } |
|
| 1169 | - $desc = $GLOBALS['tables_principales'][$table]; |
|
| 1170 | - $prim = @$desc['key']['PRIMARY KEY']; |
|
| 1171 | - if ( |
|
| 1172 | - !preg_match('/^\w+$/', $prim) |
|
| 1173 | - or !str_contains($desc['field'][$prim], 'int') |
|
| 1174 | - ) { |
|
| 1175 | - return ''; |
|
| 1176 | - } else { |
|
| 1177 | - return $raw ? $prim : $table . '_' . $prim . '_seq'; |
|
| 1178 | - } |
|
| 1165 | + include_spip('base/serial'); |
|
| 1166 | + if (!isset($GLOBALS['tables_principales'][$table])) { |
|
| 1167 | + return false; |
|
| 1168 | + } |
|
| 1169 | + $desc = $GLOBALS['tables_principales'][$table]; |
|
| 1170 | + $prim = @$desc['key']['PRIMARY KEY']; |
|
| 1171 | + if ( |
|
| 1172 | + !preg_match('/^\w+$/', $prim) |
|
| 1173 | + or !str_contains($desc['field'][$prim], 'int') |
|
| 1174 | + ) { |
|
| 1175 | + return ''; |
|
| 1176 | + } else { |
|
| 1177 | + return $raw ? $prim : $table . '_' . $prim . '_seq'; |
|
| 1178 | + } |
|
| 1179 | 1179 | } |
| 1180 | 1180 | |
| 1181 | 1181 | // Explicite les conversions de Mysql d'une valeur $v de type $t |
| 1182 | 1182 | // Dans le cas d'un champ date, pas d'apostrophe, c'est une syntaxe ad hoc |
| 1183 | 1183 | |
| 1184 | 1184 | function spip_pg_cite($v, $t) { |
| 1185 | - if (is_null($v)) { |
|
| 1186 | - return 'NULL'; |
|
| 1187 | - } // null php se traduit en NULL SQL |
|
| 1188 | - |
|
| 1189 | - if (sql_test_date($t)) { |
|
| 1190 | - if ($v and (strpos('0123456789', (string) $v[0]) === false)) { |
|
| 1191 | - return spip_pg_frommysql($v); |
|
| 1192 | - } else { |
|
| 1193 | - if (strncmp($v, '0000', 4) == 0) { |
|
| 1194 | - $v = '0001' . substr($v, 4); |
|
| 1195 | - } |
|
| 1196 | - if (strpos($v, '-00-00') === 4) { |
|
| 1197 | - $v = substr($v, 0, 4) . '-01-01' . substr($v, 10); |
|
| 1198 | - } |
|
| 1199 | - |
|
| 1200 | - return "timestamp '$v'"; |
|
| 1201 | - } |
|
| 1202 | - } elseif (!sql_test_int($t)) { |
|
| 1203 | - return ("'" . pg_escape_string($v) . "'"); |
|
| 1204 | - } elseif (is_numeric($v) or (strpos($v, 'CAST(') === 0)) { |
|
| 1205 | - return $v; |
|
| 1206 | - } elseif ($v[0] == '0' and $v[1] !== 'x' and ctype_xdigit(substr($v, 1))) { |
|
| 1207 | - return substr($v, 1); |
|
| 1208 | - } else { |
|
| 1209 | - spip_log("Warning: '$v' n'est pas de type $t", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1210 | - |
|
| 1211 | - return intval($v); |
|
| 1212 | - } |
|
| 1185 | + if (is_null($v)) { |
|
| 1186 | + return 'NULL'; |
|
| 1187 | + } // null php se traduit en NULL SQL |
|
| 1188 | + |
|
| 1189 | + if (sql_test_date($t)) { |
|
| 1190 | + if ($v and (strpos('0123456789', (string) $v[0]) === false)) { |
|
| 1191 | + return spip_pg_frommysql($v); |
|
| 1192 | + } else { |
|
| 1193 | + if (strncmp($v, '0000', 4) == 0) { |
|
| 1194 | + $v = '0001' . substr($v, 4); |
|
| 1195 | + } |
|
| 1196 | + if (strpos($v, '-00-00') === 4) { |
|
| 1197 | + $v = substr($v, 0, 4) . '-01-01' . substr($v, 10); |
|
| 1198 | + } |
|
| 1199 | + |
|
| 1200 | + return "timestamp '$v'"; |
|
| 1201 | + } |
|
| 1202 | + } elseif (!sql_test_int($t)) { |
|
| 1203 | + return ("'" . pg_escape_string($v) . "'"); |
|
| 1204 | + } elseif (is_numeric($v) or (strpos($v, 'CAST(') === 0)) { |
|
| 1205 | + return $v; |
|
| 1206 | + } elseif ($v[0] == '0' and $v[1] !== 'x' and ctype_xdigit(substr($v, 1))) { |
|
| 1207 | + return substr($v, 1); |
|
| 1208 | + } else { |
|
| 1209 | + spip_log("Warning: '$v' n'est pas de type $t", 'pg.' . _LOG_AVERTISSEMENT); |
|
| 1210 | + |
|
| 1211 | + return intval($v); |
|
| 1212 | + } |
|
| 1213 | 1213 | } |
| 1214 | 1214 | |
| 1215 | 1215 | function spip_pg_hex($v) { |
| 1216 | - return "CAST(x'" . $v . "' as bigint)"; |
|
| 1216 | + return "CAST(x'" . $v . "' as bigint)"; |
|
| 1217 | 1217 | } |
| 1218 | 1218 | |
| 1219 | 1219 | function spip_pg_quote($v, $type = '') { |
| 1220 | - if (!is_array($v)) { |
|
| 1221 | - return spip_pg_cite($v, $type); |
|
| 1222 | - } |
|
| 1223 | - // si c'est un tableau, le parcourir en propageant le type |
|
| 1224 | - foreach ($v as $k => $r) { |
|
| 1225 | - $v[$k] = spip_pg_quote($r, $type); |
|
| 1226 | - } |
|
| 1227 | - |
|
| 1228 | - return join(',', $v); |
|
| 1220 | + if (!is_array($v)) { |
|
| 1221 | + return spip_pg_cite($v, $type); |
|
| 1222 | + } |
|
| 1223 | + // si c'est un tableau, le parcourir en propageant le type |
|
| 1224 | + foreach ($v as $k => $r) { |
|
| 1225 | + $v[$k] = spip_pg_quote($r, $type); |
|
| 1226 | + } |
|
| 1227 | + |
|
| 1228 | + return join(',', $v); |
|
| 1229 | 1229 | } |
| 1230 | 1230 | |
| 1231 | 1231 | function spip_pg_date_proche($champ, $interval, $unite) { |
| 1232 | - return '(' |
|
| 1233 | - . $champ |
|
| 1234 | - . (($interval <= 0) ? '>' : '<') |
|
| 1235 | - . (($interval <= 0) ? 'DATE_SUB' : 'DATE_ADD') |
|
| 1236 | - . '(' |
|
| 1237 | - . sql_quote(date('Y-m-d H:i:s')) |
|
| 1238 | - . ', INTERVAL ' |
|
| 1239 | - . (($interval > 0) ? $interval : (0 - $interval)) |
|
| 1240 | - . ' ' |
|
| 1241 | - . $unite |
|
| 1242 | - . '))'; |
|
| 1232 | + return '(' |
|
| 1233 | + . $champ |
|
| 1234 | + . (($interval <= 0) ? '>' : '<') |
|
| 1235 | + . (($interval <= 0) ? 'DATE_SUB' : 'DATE_ADD') |
|
| 1236 | + . '(' |
|
| 1237 | + . sql_quote(date('Y-m-d H:i:s')) |
|
| 1238 | + . ', INTERVAL ' |
|
| 1239 | + . (($interval > 0) ? $interval : (0 - $interval)) |
|
| 1240 | + . ' ' |
|
| 1241 | + . $unite |
|
| 1242 | + . '))'; |
|
| 1243 | 1243 | } |
| 1244 | 1244 | |
| 1245 | 1245 | function spip_pg_in($val, $valeurs, $not = '', $serveur = '') { |
| 1246 | 1246 | // |
| 1247 | 1247 | // IN (...) souvent limite a 255 elements, d'ou cette fonction assistante |
| 1248 | 1248 | // |
| 1249 | - // s'il n'y a pas de valeur, eviter de produire un IN vide: PG rale. |
|
| 1250 | - if (!$valeurs) { |
|
| 1251 | - return $not ? '0=0' : '0=1'; |
|
| 1252 | - } |
|
| 1253 | - if (strpos($valeurs, "CAST(x'") !== false) { |
|
| 1254 | - return "($val=" . join("OR $val=", explode(',', $valeurs)) . ')'; |
|
| 1255 | - } |
|
| 1256 | - $n = $i = 0; |
|
| 1257 | - $in_sql = ''; |
|
| 1258 | - while ($n = strpos($valeurs, ',', $n + 1)) { |
|
| 1259 | - if ((++$i) >= 255) { |
|
| 1260 | - $in_sql .= "($val $not IN (" . |
|
| 1261 | - substr($valeurs, 0, $n) . |
|
| 1262 | - "))\n" . |
|
| 1263 | - ($not ? "AND\t" : "OR\t"); |
|
| 1264 | - $valeurs = substr($valeurs, $n + 1); |
|
| 1265 | - $i = $n = 0; |
|
| 1266 | - } |
|
| 1267 | - } |
|
| 1268 | - $in_sql .= "($val $not IN ($valeurs))"; |
|
| 1269 | - |
|
| 1270 | - return "($in_sql)"; |
|
| 1249 | + // s'il n'y a pas de valeur, eviter de produire un IN vide: PG rale. |
|
| 1250 | + if (!$valeurs) { |
|
| 1251 | + return $not ? '0=0' : '0=1'; |
|
| 1252 | + } |
|
| 1253 | + if (strpos($valeurs, "CAST(x'") !== false) { |
|
| 1254 | + return "($val=" . join("OR $val=", explode(',', $valeurs)) . ')'; |
|
| 1255 | + } |
|
| 1256 | + $n = $i = 0; |
|
| 1257 | + $in_sql = ''; |
|
| 1258 | + while ($n = strpos($valeurs, ',', $n + 1)) { |
|
| 1259 | + if ((++$i) >= 255) { |
|
| 1260 | + $in_sql .= "($val $not IN (" . |
|
| 1261 | + substr($valeurs, 0, $n) . |
|
| 1262 | + "))\n" . |
|
| 1263 | + ($not ? "AND\t" : "OR\t"); |
|
| 1264 | + $valeurs = substr($valeurs, $n + 1); |
|
| 1265 | + $i = $n = 0; |
|
| 1266 | + } |
|
| 1267 | + } |
|
| 1268 | + $in_sql .= "($val $not IN ($valeurs))"; |
|
| 1269 | + |
|
| 1270 | + return "($in_sql)"; |
|
| 1271 | 1271 | } |
| 1272 | 1272 | |
| 1273 | 1273 | function spip_pg_error($query = '', $serveur = '', $requeter = true) { |
| 1274 | - $link = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]['link']; |
|
| 1275 | - $s = $link ? pg_last_error($link) : pg_last_error(); |
|
| 1276 | - if ($s) { |
|
| 1277 | - $s = str_replace('ERROR', 'errcode: 1000 ', $s); |
|
| 1278 | - spip_log("$s - $query", 'pg.' . _LOG_ERREUR); |
|
| 1279 | - } |
|
| 1280 | - |
|
| 1281 | - return $s; |
|
| 1274 | + $link = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]['link']; |
|
| 1275 | + $s = $link ? pg_last_error($link) : pg_last_error(); |
|
| 1276 | + if ($s) { |
|
| 1277 | + $s = str_replace('ERROR', 'errcode: 1000 ', $s); |
|
| 1278 | + spip_log("$s - $query", 'pg.' . _LOG_ERREUR); |
|
| 1279 | + } |
|
| 1280 | + |
|
| 1281 | + return $s; |
|
| 1282 | 1282 | } |
| 1283 | 1283 | |
| 1284 | 1284 | function spip_pg_errno($serveur = '') { |
| 1285 | - // il faudrait avoir la derniere ressource retournee et utiliser |
|
| 1286 | - // http://fr2.php.net/manual/fr/function.pg-result-error.php |
|
| 1287 | - return 0; |
|
| 1285 | + // il faudrait avoir la derniere ressource retournee et utiliser |
|
| 1286 | + // http://fr2.php.net/manual/fr/function.pg-result-error.php |
|
| 1287 | + return 0; |
|
| 1288 | 1288 | } |
| 1289 | 1289 | |
| 1290 | 1290 | function spip_pg_drop_table($table, $exist = '', $serveur = '', $requeter = true) { |
| 1291 | - if ($exist) { |
|
| 1292 | - $exist = ' IF EXISTS'; |
|
| 1293 | - } |
|
| 1294 | - if (spip_pg_query("DROP TABLE$exist $table", $serveur, $requeter)) { |
|
| 1295 | - return true; |
|
| 1296 | - } else { |
|
| 1297 | - return false; |
|
| 1298 | - } |
|
| 1291 | + if ($exist) { |
|
| 1292 | + $exist = ' IF EXISTS'; |
|
| 1293 | + } |
|
| 1294 | + if (spip_pg_query("DROP TABLE$exist $table", $serveur, $requeter)) { |
|
| 1295 | + return true; |
|
| 1296 | + } else { |
|
| 1297 | + return false; |
|
| 1298 | + } |
|
| 1299 | 1299 | } |
| 1300 | 1300 | |
| 1301 | 1301 | // supprime une vue |
| 1302 | 1302 | function spip_pg_drop_view($view, $exist = '', $serveur = '', $requeter = true) { |
| 1303 | - if ($exist) { |
|
| 1304 | - $exist = ' IF EXISTS'; |
|
| 1305 | - } |
|
| 1303 | + if ($exist) { |
|
| 1304 | + $exist = ' IF EXISTS'; |
|
| 1305 | + } |
|
| 1306 | 1306 | |
| 1307 | - return spip_pg_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 1307 | + return spip_pg_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 1308 | 1308 | } |
| 1309 | 1309 | |
| 1310 | 1310 | /** |
@@ -1321,40 +1321,40 @@ discard block |
||
| 1321 | 1321 | * Ressource à utiliser avec sql_fetch() |
| 1322 | 1322 | **/ |
| 1323 | 1323 | function spip_pg_showbase($match, $serveur = '', $requeter = true) { |
| 1324 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1325 | - $link = $connexion['link']; |
|
| 1326 | - $connexion['last'] = $q = 'SELECT tablename FROM pg_tables WHERE tablename ILIKE ' . _q($match); |
|
| 1324 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1325 | + $link = $connexion['link']; |
|
| 1326 | + $connexion['last'] = $q = 'SELECT tablename FROM pg_tables WHERE tablename ILIKE ' . _q($match); |
|
| 1327 | 1327 | |
| 1328 | - return spip_pg_query_simple($link, $q); |
|
| 1328 | + return spip_pg_query_simple($link, $q); |
|
| 1329 | 1329 | } |
| 1330 | 1330 | |
| 1331 | 1331 | function spip_pg_showtable($nom_table, $serveur = '', $requeter = true) { |
| 1332 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1333 | - $link = $connexion['link']; |
|
| 1334 | - $connexion['last'] = $q = 'SELECT column_name, column_default, data_type FROM information_schema.columns WHERE table_name ILIKE ' . _q($nom_table); |
|
| 1335 | - |
|
| 1336 | - $res = spip_pg_query_simple($link, $q); |
|
| 1337 | - if (!$res) { |
|
| 1338 | - return false; |
|
| 1339 | - } |
|
| 1340 | - |
|
| 1341 | - // etrangement, $res peut ne rien contenir, mais arriver ici... |
|
| 1342 | - // il faut en tenir compte dans le return |
|
| 1343 | - $fields = []; |
|
| 1344 | - while ($field = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 1345 | - $fields[$field[0]] = $field[2] . (!$field[1] ? '' : (' DEFAULT ' . $field[1])); |
|
| 1346 | - } |
|
| 1347 | - $connexion['last'] = $q = 'SELECT indexdef FROM pg_indexes WHERE tablename ILIKE ' . _q($nom_table); |
|
| 1348 | - $res = spip_pg_query_simple($link, $q); |
|
| 1349 | - $keys = []; |
|
| 1350 | - while ($index = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 1351 | - if (preg_match('/CREATE\s+(UNIQUE\s+)?INDEX\s([^\s]+).*\((.*)\)$/', $index[0], $r)) { |
|
| 1352 | - $nom = str_replace($nom_table . '_', '', $r[2]); |
|
| 1353 | - $keys[($r[1] ? 'PRIMARY KEY' : ('KEY ' . $nom))] = $r[3]; |
|
| 1354 | - } |
|
| 1355 | - } |
|
| 1356 | - |
|
| 1357 | - return count($fields) ? ['field' => $fields, 'key' => $keys] : false; |
|
| 1332 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1333 | + $link = $connexion['link']; |
|
| 1334 | + $connexion['last'] = $q = 'SELECT column_name, column_default, data_type FROM information_schema.columns WHERE table_name ILIKE ' . _q($nom_table); |
|
| 1335 | + |
|
| 1336 | + $res = spip_pg_query_simple($link, $q); |
|
| 1337 | + if (!$res) { |
|
| 1338 | + return false; |
|
| 1339 | + } |
|
| 1340 | + |
|
| 1341 | + // etrangement, $res peut ne rien contenir, mais arriver ici... |
|
| 1342 | + // il faut en tenir compte dans le return |
|
| 1343 | + $fields = []; |
|
| 1344 | + while ($field = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 1345 | + $fields[$field[0]] = $field[2] . (!$field[1] ? '' : (' DEFAULT ' . $field[1])); |
|
| 1346 | + } |
|
| 1347 | + $connexion['last'] = $q = 'SELECT indexdef FROM pg_indexes WHERE tablename ILIKE ' . _q($nom_table); |
|
| 1348 | + $res = spip_pg_query_simple($link, $q); |
|
| 1349 | + $keys = []; |
|
| 1350 | + while ($index = pg_fetch_array($res, null, PGSQL_NUM)) { |
|
| 1351 | + if (preg_match('/CREATE\s+(UNIQUE\s+)?INDEX\s([^\s]+).*\((.*)\)$/', $index[0], $r)) { |
|
| 1352 | + $nom = str_replace($nom_table . '_', '', $r[2]); |
|
| 1353 | + $keys[($r[1] ? 'PRIMARY KEY' : ('KEY ' . $nom))] = $r[3]; |
|
| 1354 | + } |
|
| 1355 | + } |
|
| 1356 | + |
|
| 1357 | + return count($fields) ? ['field' => $fields, 'key' => $keys] : false; |
|
| 1358 | 1358 | } |
| 1359 | 1359 | |
| 1360 | 1360 | // Fonction de creation d'une table SQL nommee $nom |
@@ -1365,116 +1365,116 @@ discard block |
||
| 1365 | 1365 | // Le nom des index est prefixe par celui de la table pour eviter les conflits |
| 1366 | 1366 | function spip_pg_create($nom, $champs, $cles, $autoinc = false, $temporary = false, $serveur = '', $requeter = true) { |
| 1367 | 1367 | |
| 1368 | - $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1369 | - $link = $connexion['link']; |
|
| 1370 | - $nom = prefixer_table_spip($nom, $connexion['prefixe']); |
|
| 1371 | - |
|
| 1372 | - $query = $prim = $prim_name = $v = $s = $p = ''; |
|
| 1373 | - $keys = []; |
|
| 1374 | - |
|
| 1375 | - // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 1376 | - // sans les renseigner (laisse le compilo recuperer la description) |
|
| 1377 | - if (!is_array($champs) || !is_array($cles)) { |
|
| 1378 | - return; |
|
| 1379 | - } |
|
| 1380 | - |
|
| 1381 | - foreach ($cles as $k => $v) { |
|
| 1382 | - if (strpos($k, 'KEY ') === 0) { |
|
| 1383 | - $n = str_replace('`', '', $k); |
|
| 1384 | - $v = str_replace('`', '"', $v); |
|
| 1385 | - $i = $nom . preg_replace('/KEY +/', '_', $n); |
|
| 1386 | - if ($k != $n) { |
|
| 1387 | - $i = "\"$i\""; |
|
| 1388 | - } |
|
| 1389 | - $keys[] = "CREATE INDEX $i ON $nom ($v);"; |
|
| 1390 | - } elseif (strpos($k, 'UNIQUE ') === 0) { |
|
| 1391 | - $k = preg_replace('/^UNIQUE +/', '', $k); |
|
| 1392 | - $prim .= "$s\n\t\tCONSTRAINT " . str_replace('`', '"', $k) . " UNIQUE ($v)"; |
|
| 1393 | - } else { |
|
| 1394 | - $prim .= "$s\n\t\t" . str_replace('`', '"', $k) . " ($v)"; |
|
| 1395 | - } |
|
| 1396 | - if ($k == 'PRIMARY KEY') { |
|
| 1397 | - $prim_name = $v; |
|
| 1398 | - } |
|
| 1399 | - $s = ','; |
|
| 1400 | - } |
|
| 1401 | - $s = ''; |
|
| 1402 | - |
|
| 1403 | - $character_set = ''; |
|
| 1404 | - if (@$GLOBALS['meta']['charset_sql_base']) { |
|
| 1405 | - $character_set .= ' CHARACTER SET ' . $GLOBALS['meta']['charset_sql_base']; |
|
| 1406 | - } |
|
| 1407 | - if (@$GLOBALS['meta']['charset_collation_sql_base']) { |
|
| 1408 | - $character_set .= ' COLLATE ' . $GLOBALS['meta']['charset_collation_sql_base']; |
|
| 1409 | - } |
|
| 1410 | - |
|
| 1411 | - foreach ($champs as $k => $v) { |
|
| 1412 | - $k = str_replace('`', '"', $k); |
|
| 1413 | - if (preg_match(',([a-z]*\s*(\(\s*[0-9]*\s*\))?(\s*binary)?),i', $v, $defs)) { |
|
| 1414 | - if (preg_match(',(char|text),i', $defs[1]) and !preg_match(',binary,i', $defs[1])) { |
|
| 1415 | - $v = $defs[1] . $character_set . ' ' . substr($v, strlen($defs[1])); |
|
| 1416 | - } |
|
| 1417 | - } |
|
| 1418 | - |
|
| 1419 | - $query .= "$s\n\t\t$k " |
|
| 1420 | - . (($autoinc && ($prim_name == $k) && preg_match(',\b(big|small|medium|tiny)?int\b,i', $v)) |
|
| 1421 | - ? ' bigserial' |
|
| 1422 | - : mysql2pg_type($v) |
|
| 1423 | - ); |
|
| 1424 | - $s = ','; |
|
| 1425 | - } |
|
| 1426 | - $temporary = $temporary ? 'TEMPORARY' : ''; |
|
| 1427 | - |
|
| 1428 | - // En l'absence de "if not exists" en PG, on neutralise les erreurs |
|
| 1429 | - |
|
| 1430 | - $q = "CREATE $temporary TABLE $nom ($query" . ($prim ? ",$prim" : '') . ')' . |
|
| 1431 | - ($character_set ? " DEFAULT $character_set" : '') |
|
| 1432 | - . "\n"; |
|
| 1433 | - |
|
| 1434 | - if (!$requeter) { |
|
| 1435 | - return $q; |
|
| 1436 | - } |
|
| 1437 | - $connexion['last'] = $q; |
|
| 1438 | - $r = @pg_query($link, $q); |
|
| 1439 | - |
|
| 1440 | - if (!$r) { |
|
| 1441 | - spip_log("Impossible de creer cette table: $q", 'pg.' . _LOG_ERREUR); |
|
| 1442 | - } else { |
|
| 1443 | - foreach ($keys as $index) { |
|
| 1444 | - pg_query($link, $index); |
|
| 1445 | - } |
|
| 1446 | - } |
|
| 1447 | - |
|
| 1448 | - return $r; |
|
| 1368 | + $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1369 | + $link = $connexion['link']; |
|
| 1370 | + $nom = prefixer_table_spip($nom, $connexion['prefixe']); |
|
| 1371 | + |
|
| 1372 | + $query = $prim = $prim_name = $v = $s = $p = ''; |
|
| 1373 | + $keys = []; |
|
| 1374 | + |
|
| 1375 | + // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 1376 | + // sans les renseigner (laisse le compilo recuperer la description) |
|
| 1377 | + if (!is_array($champs) || !is_array($cles)) { |
|
| 1378 | + return; |
|
| 1379 | + } |
|
| 1380 | + |
|
| 1381 | + foreach ($cles as $k => $v) { |
|
| 1382 | + if (strpos($k, 'KEY ') === 0) { |
|
| 1383 | + $n = str_replace('`', '', $k); |
|
| 1384 | + $v = str_replace('`', '"', $v); |
|
| 1385 | + $i = $nom . preg_replace('/KEY +/', '_', $n); |
|
| 1386 | + if ($k != $n) { |
|
| 1387 | + $i = "\"$i\""; |
|
| 1388 | + } |
|
| 1389 | + $keys[] = "CREATE INDEX $i ON $nom ($v);"; |
|
| 1390 | + } elseif (strpos($k, 'UNIQUE ') === 0) { |
|
| 1391 | + $k = preg_replace('/^UNIQUE +/', '', $k); |
|
| 1392 | + $prim .= "$s\n\t\tCONSTRAINT " . str_replace('`', '"', $k) . " UNIQUE ($v)"; |
|
| 1393 | + } else { |
|
| 1394 | + $prim .= "$s\n\t\t" . str_replace('`', '"', $k) . " ($v)"; |
|
| 1395 | + } |
|
| 1396 | + if ($k == 'PRIMARY KEY') { |
|
| 1397 | + $prim_name = $v; |
|
| 1398 | + } |
|
| 1399 | + $s = ','; |
|
| 1400 | + } |
|
| 1401 | + $s = ''; |
|
| 1402 | + |
|
| 1403 | + $character_set = ''; |
|
| 1404 | + if (@$GLOBALS['meta']['charset_sql_base']) { |
|
| 1405 | + $character_set .= ' CHARACTER SET ' . $GLOBALS['meta']['charset_sql_base']; |
|
| 1406 | + } |
|
| 1407 | + if (@$GLOBALS['meta']['charset_collation_sql_base']) { |
|
| 1408 | + $character_set .= ' COLLATE ' . $GLOBALS['meta']['charset_collation_sql_base']; |
|
| 1409 | + } |
|
| 1410 | + |
|
| 1411 | + foreach ($champs as $k => $v) { |
|
| 1412 | + $k = str_replace('`', '"', $k); |
|
| 1413 | + if (preg_match(',([a-z]*\s*(\(\s*[0-9]*\s*\))?(\s*binary)?),i', $v, $defs)) { |
|
| 1414 | + if (preg_match(',(char|text),i', $defs[1]) and !preg_match(',binary,i', $defs[1])) { |
|
| 1415 | + $v = $defs[1] . $character_set . ' ' . substr($v, strlen($defs[1])); |
|
| 1416 | + } |
|
| 1417 | + } |
|
| 1418 | + |
|
| 1419 | + $query .= "$s\n\t\t$k " |
|
| 1420 | + . (($autoinc && ($prim_name == $k) && preg_match(',\b(big|small|medium|tiny)?int\b,i', $v)) |
|
| 1421 | + ? ' bigserial' |
|
| 1422 | + : mysql2pg_type($v) |
|
| 1423 | + ); |
|
| 1424 | + $s = ','; |
|
| 1425 | + } |
|
| 1426 | + $temporary = $temporary ? 'TEMPORARY' : ''; |
|
| 1427 | + |
|
| 1428 | + // En l'absence de "if not exists" en PG, on neutralise les erreurs |
|
| 1429 | + |
|
| 1430 | + $q = "CREATE $temporary TABLE $nom ($query" . ($prim ? ",$prim" : '') . ')' . |
|
| 1431 | + ($character_set ? " DEFAULT $character_set" : '') |
|
| 1432 | + . "\n"; |
|
| 1433 | + |
|
| 1434 | + if (!$requeter) { |
|
| 1435 | + return $q; |
|
| 1436 | + } |
|
| 1437 | + $connexion['last'] = $q; |
|
| 1438 | + $r = @pg_query($link, $q); |
|
| 1439 | + |
|
| 1440 | + if (!$r) { |
|
| 1441 | + spip_log("Impossible de creer cette table: $q", 'pg.' . _LOG_ERREUR); |
|
| 1442 | + } else { |
|
| 1443 | + foreach ($keys as $index) { |
|
| 1444 | + pg_query($link, $index); |
|
| 1445 | + } |
|
| 1446 | + } |
|
| 1447 | + |
|
| 1448 | + return $r; |
|
| 1449 | 1449 | } |
| 1450 | 1450 | |
| 1451 | 1451 | |
| 1452 | 1452 | function spip_pg_create_base($nom, $serveur = '', $requeter = true) { |
| 1453 | - return spip_pg_query("CREATE DATABASE $nom", $serveur, $requeter); |
|
| 1453 | + return spip_pg_query("CREATE DATABASE $nom", $serveur, $requeter); |
|
| 1454 | 1454 | } |
| 1455 | 1455 | |
| 1456 | 1456 | // Fonction de creation d'une vue SQL nommee $nom |
| 1457 | 1457 | function spip_pg_create_view($nom, $query_select, $serveur = '', $requeter = true) { |
| 1458 | - if (!$query_select) { |
|
| 1459 | - return false; |
|
| 1460 | - } |
|
| 1461 | - // vue deja presente |
|
| 1462 | - if (sql_showtable($nom, false, $serveur)) { |
|
| 1463 | - if ($requeter) { |
|
| 1464 | - spip_log("Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", 'pg.' . _LOG_ERREUR); |
|
| 1465 | - } |
|
| 1458 | + if (!$query_select) { |
|
| 1459 | + return false; |
|
| 1460 | + } |
|
| 1461 | + // vue deja presente |
|
| 1462 | + if (sql_showtable($nom, false, $serveur)) { |
|
| 1463 | + if ($requeter) { |
|
| 1464 | + spip_log("Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", 'pg.' . _LOG_ERREUR); |
|
| 1465 | + } |
|
| 1466 | 1466 | |
| 1467 | - return false; |
|
| 1468 | - } |
|
| 1467 | + return false; |
|
| 1468 | + } |
|
| 1469 | 1469 | |
| 1470 | - $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 1470 | + $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 1471 | 1471 | |
| 1472 | - return spip_pg_query($query, $serveur, $requeter); |
|
| 1472 | + return spip_pg_query($query, $serveur, $requeter); |
|
| 1473 | 1473 | } |
| 1474 | 1474 | |
| 1475 | 1475 | |
| 1476 | 1476 | function spip_pg_set_connect_charset($charset, $serveur = '', $requeter = true) { |
| 1477 | - spip_log('changement de charset sql a ecrire en PG', 'pg.' . _LOG_ERREUR); |
|
| 1477 | + spip_log('changement de charset sql a ecrire en PG', 'pg.' . _LOG_ERREUR); |
|
| 1478 | 1478 | } |
| 1479 | 1479 | |
| 1480 | 1480 | |
@@ -1487,50 +1487,50 @@ discard block |
||
| 1487 | 1487 | * @return bool|string true / false / requete |
| 1488 | 1488 | **/ |
| 1489 | 1489 | function spip_pg_optimize($table, $serveur = '', $requeter = true) { |
| 1490 | - return spip_pg_query('VACUUM ' . $table, $serveur, $requeter); |
|
| 1490 | + return spip_pg_query('VACUUM ' . $table, $serveur, $requeter); |
|
| 1491 | 1491 | } |
| 1492 | 1492 | |
| 1493 | 1493 | // Selectionner la sous-chaine dans $objet |
| 1494 | 1494 | // correspondant a $lang. Cf balise Multi de Spip |
| 1495 | 1495 | |
| 1496 | 1496 | function spip_pg_multi($objet, $lang) { |
| 1497 | - $r = 'regexp_replace(' |
|
| 1498 | - . $objet |
|
| 1499 | - . ",'<multi>.*[[]" |
|
| 1500 | - . $lang |
|
| 1501 | - . "[]]([^[]*).*</multi>', E'\\\\1') AS multi"; |
|
| 1497 | + $r = 'regexp_replace(' |
|
| 1498 | + . $objet |
|
| 1499 | + . ",'<multi>.*[[]" |
|
| 1500 | + . $lang |
|
| 1501 | + . "[]]([^[]*).*</multi>', E'\\\\1') AS multi"; |
|
| 1502 | 1502 | |
| 1503 | - return $r; |
|
| 1503 | + return $r; |
|
| 1504 | 1504 | } |
| 1505 | 1505 | |
| 1506 | 1506 | // Palanquee d'idiosyncrasies MySQL dans les creations de table |
| 1507 | 1507 | // A completer par les autres, mais essayer de reduire en amont. |
| 1508 | 1508 | |
| 1509 | 1509 | function mysql2pg_type($v) { |
| 1510 | - $remplace = [ |
|
| 1511 | - '/auto_increment/i' => '', // non reconnu |
|
| 1512 | - '/bigint/i' => 'bigint', |
|
| 1513 | - '/mediumint/i' => 'mediumint', |
|
| 1514 | - '/smallint/i' => 'smallint', |
|
| 1515 | - '/tinyint/i' => 'int', |
|
| 1516 | - '/int\s*[(]\s*\d+\s*[)]/i' => 'int', |
|
| 1517 | - '/longtext/i' => 'text', |
|
| 1518 | - '/mediumtext/i' => 'text', |
|
| 1519 | - '/tinytext/i' => 'text', |
|
| 1520 | - '/longblob/i' => 'text', |
|
| 1521 | - '/0000-00-00/' => '0001-01-01', |
|
| 1522 | - '/datetime/i' => 'timestamp', |
|
| 1523 | - '/unsigned/i' => '', |
|
| 1524 | - '/double/i' => 'double precision', |
|
| 1525 | - '/VARCHAR\((\d+)\)\s+BINARY/i' => 'varchar(\1)', |
|
| 1526 | - '/ENUM *[(][^)]*[)]/i' => 'varchar(255)', |
|
| 1527 | - '/(timestamp .* )ON .*$/is' => '\\1', |
|
| 1528 | - ]; |
|
| 1529 | - |
|
| 1530 | - return preg_replace(array_keys($remplace), array_values($remplace), $v); |
|
| 1510 | + $remplace = [ |
|
| 1511 | + '/auto_increment/i' => '', // non reconnu |
|
| 1512 | + '/bigint/i' => 'bigint', |
|
| 1513 | + '/mediumint/i' => 'mediumint', |
|
| 1514 | + '/smallint/i' => 'smallint', |
|
| 1515 | + '/tinyint/i' => 'int', |
|
| 1516 | + '/int\s*[(]\s*\d+\s*[)]/i' => 'int', |
|
| 1517 | + '/longtext/i' => 'text', |
|
| 1518 | + '/mediumtext/i' => 'text', |
|
| 1519 | + '/tinytext/i' => 'text', |
|
| 1520 | + '/longblob/i' => 'text', |
|
| 1521 | + '/0000-00-00/' => '0001-01-01', |
|
| 1522 | + '/datetime/i' => 'timestamp', |
|
| 1523 | + '/unsigned/i' => '', |
|
| 1524 | + '/double/i' => 'double precision', |
|
| 1525 | + '/VARCHAR\((\d+)\)\s+BINARY/i' => 'varchar(\1)', |
|
| 1526 | + '/ENUM *[(][^)]*[)]/i' => 'varchar(255)', |
|
| 1527 | + '/(timestamp .* )ON .*$/is' => '\\1', |
|
| 1528 | + ]; |
|
| 1529 | + |
|
| 1530 | + return preg_replace(array_keys($remplace), array_values($remplace), $v); |
|
| 1531 | 1531 | } |
| 1532 | 1532 | |
| 1533 | 1533 | // Renvoie false si on n'a pas les fonctions pg (pour l'install) |
| 1534 | 1534 | function spip_versions_pg() { |
| 1535 | - return function_exists('pg_connect'); |
|
| 1535 | + return function_exists('pg_connect'); |
|
| 1536 | 1536 | } |