@@ -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 | - $pass, |
|
| 247 | - $db = '', |
|
| 248 | - $type = 'mysql', |
|
| 249 | - $prefixe = '', |
|
| 250 | - $auth = '', |
|
| 251 | - $charset = '' |
|
| 243 | + $host, |
|
| 244 | + $port, |
|
| 245 | + $login, |
|
| 246 | + $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 | } |