@@ -20,11 +20,11 @@ discard block |
||
| 20 | 20 | */ |
| 21 | 21 | |
| 22 | 22 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 23 | - return; |
|
| 23 | + return; |
|
| 24 | 24 | } |
| 25 | 25 | |
| 26 | 26 | if (!defined('_MYSQL_NOPLANES')) { |
| 27 | - define('_MYSQL_NOPLANES', true); |
|
| 27 | + define('_MYSQL_NOPLANES', true); |
|
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 30 | /** |
@@ -41,108 +41,108 @@ discard block |
||
| 41 | 41 | * - tableau décrivant la connexion sinon |
| 42 | 42 | */ |
| 43 | 43 | function req_mysql_dist($host, $port, $login, $pass, $db = '', $prefixe = '') { |
| 44 | - if (!extension_loaded('mysqli')) { |
|
| 45 | - return false; |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - // si port est fourni mais pas host, c'est un socket -> compat avec vieille syntaxe de mysql_connect() et anciens fichiers connect.php |
|
| 49 | - 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 | - } |
|
| 55 | - elseif ($port) { |
|
| 56 | - $link = @mysqli_connect($host, $login, $pass, '', $port); |
|
| 57 | - } |
|
| 58 | - else { |
|
| 59 | - $link = @mysqli_connect($host, $login, $pass); |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - if (!$link) { |
|
| 63 | - spip_log('Echec mysqli_connect. Erreur : ' . mysqli_connect_error(), 'mysql.' . _LOG_HS); |
|
| 64 | - |
|
| 65 | - return false; |
|
| 66 | - } |
|
| 67 | - $last = ''; |
|
| 68 | - if (!$db) { |
|
| 69 | - $ok = $link; |
|
| 70 | - $db = 'spip'; |
|
| 71 | - } else { |
|
| 72 | - $ok = mysqli_select_db($link, $db); |
|
| 73 | - if ( |
|
| 74 | - defined('_MYSQL_SET_SQL_MODE') |
|
| 75 | - or defined('_MYSQL_SQL_MODE_TEXT_NOT_NULL') // compatibilite |
|
| 76 | - ) { |
|
| 77 | - mysqli_query($link, $last = "set sql_mode=''"); |
|
| 78 | - } |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - spip_log( |
|
| 82 | - "Connexion MySQLi vers $host, base $db, prefixe $prefixe " . ($ok ? 'operationnelle' : 'impossible'), |
|
| 83 | - _LOG_DEBUG |
|
| 84 | - ); |
|
| 85 | - |
|
| 86 | - return !$ok ? false : [ |
|
| 87 | - 'db' => $db, |
|
| 88 | - 'last' => $last, |
|
| 89 | - 'prefixe' => $prefixe ? $prefixe : $db, |
|
| 90 | - 'link' => $link, |
|
| 91 | - 'total_requetes' => 0, |
|
| 92 | - ]; |
|
| 44 | + if (!extension_loaded('mysqli')) { |
|
| 45 | + return false; |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + // si port est fourni mais pas host, c'est un socket -> compat avec vieille syntaxe de mysql_connect() et anciens fichiers connect.php |
|
| 49 | + 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 | + } |
|
| 55 | + elseif ($port) { |
|
| 56 | + $link = @mysqli_connect($host, $login, $pass, '', $port); |
|
| 57 | + } |
|
| 58 | + else { |
|
| 59 | + $link = @mysqli_connect($host, $login, $pass); |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + if (!$link) { |
|
| 63 | + spip_log('Echec mysqli_connect. Erreur : ' . mysqli_connect_error(), 'mysql.' . _LOG_HS); |
|
| 64 | + |
|
| 65 | + return false; |
|
| 66 | + } |
|
| 67 | + $last = ''; |
|
| 68 | + if (!$db) { |
|
| 69 | + $ok = $link; |
|
| 70 | + $db = 'spip'; |
|
| 71 | + } else { |
|
| 72 | + $ok = mysqli_select_db($link, $db); |
|
| 73 | + if ( |
|
| 74 | + defined('_MYSQL_SET_SQL_MODE') |
|
| 75 | + or defined('_MYSQL_SQL_MODE_TEXT_NOT_NULL') // compatibilite |
|
| 76 | + ) { |
|
| 77 | + mysqli_query($link, $last = "set sql_mode=''"); |
|
| 78 | + } |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + spip_log( |
|
| 82 | + "Connexion MySQLi vers $host, base $db, prefixe $prefixe " . ($ok ? 'operationnelle' : 'impossible'), |
|
| 83 | + _LOG_DEBUG |
|
| 84 | + ); |
|
| 85 | + |
|
| 86 | + return !$ok ? false : [ |
|
| 87 | + 'db' => $db, |
|
| 88 | + 'last' => $last, |
|
| 89 | + 'prefixe' => $prefixe ? $prefixe : $db, |
|
| 90 | + 'link' => $link, |
|
| 91 | + 'total_requetes' => 0, |
|
| 92 | + ]; |
|
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | |
| 96 | 96 | $GLOBALS['spip_mysql_functions_1'] = [ |
| 97 | - 'alter' => 'spip_mysql_alter', |
|
| 98 | - 'count' => 'spip_mysql_count', |
|
| 99 | - 'countsel' => 'spip_mysql_countsel', |
|
| 100 | - 'create' => 'spip_mysql_create', |
|
| 101 | - 'create_base' => 'spip_mysql_create_base', |
|
| 102 | - 'create_view' => 'spip_mysql_create_view', |
|
| 103 | - 'date_proche' => 'spip_mysql_date_proche', |
|
| 104 | - 'delete' => 'spip_mysql_delete', |
|
| 105 | - 'drop_table' => 'spip_mysql_drop_table', |
|
| 106 | - 'drop_view' => 'spip_mysql_drop_view', |
|
| 107 | - 'errno' => 'spip_mysql_errno', |
|
| 108 | - 'error' => 'spip_mysql_error', |
|
| 109 | - 'explain' => 'spip_mysql_explain', |
|
| 110 | - 'fetch' => 'spip_mysql_fetch', |
|
| 111 | - 'seek' => 'spip_mysql_seek', |
|
| 112 | - 'free' => 'spip_mysql_free', |
|
| 113 | - 'hex' => 'spip_mysql_hex', |
|
| 114 | - 'in' => 'spip_mysql_in', |
|
| 115 | - 'insert' => 'spip_mysql_insert', |
|
| 116 | - 'insertq' => 'spip_mysql_insertq', |
|
| 117 | - 'insertq_multi' => 'spip_mysql_insertq_multi', |
|
| 118 | - 'listdbs' => 'spip_mysql_listdbs', |
|
| 119 | - 'multi' => 'spip_mysql_multi', |
|
| 120 | - 'optimize' => 'spip_mysql_optimize', |
|
| 121 | - 'query' => 'spip_mysql_query', |
|
| 122 | - 'quote' => 'spip_mysql_quote', |
|
| 123 | - 'replace' => 'spip_mysql_replace', |
|
| 124 | - 'replace_multi' => 'spip_mysql_replace_multi', |
|
| 125 | - 'repair' => 'spip_mysql_repair', |
|
| 126 | - 'select' => 'spip_mysql_select', |
|
| 127 | - 'selectdb' => 'spip_mysql_selectdb', |
|
| 128 | - 'set_charset' => 'spip_mysql_set_charset', |
|
| 129 | - 'get_charset' => 'spip_mysql_get_charset', |
|
| 130 | - 'showbase' => 'spip_mysql_showbase', |
|
| 131 | - 'showtable' => 'spip_mysql_showtable', |
|
| 132 | - 'update' => 'spip_mysql_update', |
|
| 133 | - 'updateq' => 'spip_mysql_updateq', |
|
| 134 | - |
|
| 135 | - // association de chaque nom http d'un charset aux couples MySQL |
|
| 136 | - 'charsets' => [ |
|
| 137 | - 'cp1250' => ['charset' => 'cp1250', 'collation' => 'cp1250_general_ci'], |
|
| 138 | - 'cp1251' => ['charset' => 'cp1251', 'collation' => 'cp1251_general_ci'], |
|
| 139 | - 'cp1256' => ['charset' => 'cp1256', 'collation' => 'cp1256_general_ci'], |
|
| 140 | - 'iso-8859-1' => ['charset' => 'latin1', 'collation' => 'latin1_swedish_ci'], |
|
| 97 | + 'alter' => 'spip_mysql_alter', |
|
| 98 | + 'count' => 'spip_mysql_count', |
|
| 99 | + 'countsel' => 'spip_mysql_countsel', |
|
| 100 | + 'create' => 'spip_mysql_create', |
|
| 101 | + 'create_base' => 'spip_mysql_create_base', |
|
| 102 | + 'create_view' => 'spip_mysql_create_view', |
|
| 103 | + 'date_proche' => 'spip_mysql_date_proche', |
|
| 104 | + 'delete' => 'spip_mysql_delete', |
|
| 105 | + 'drop_table' => 'spip_mysql_drop_table', |
|
| 106 | + 'drop_view' => 'spip_mysql_drop_view', |
|
| 107 | + 'errno' => 'spip_mysql_errno', |
|
| 108 | + 'error' => 'spip_mysql_error', |
|
| 109 | + 'explain' => 'spip_mysql_explain', |
|
| 110 | + 'fetch' => 'spip_mysql_fetch', |
|
| 111 | + 'seek' => 'spip_mysql_seek', |
|
| 112 | + 'free' => 'spip_mysql_free', |
|
| 113 | + 'hex' => 'spip_mysql_hex', |
|
| 114 | + 'in' => 'spip_mysql_in', |
|
| 115 | + 'insert' => 'spip_mysql_insert', |
|
| 116 | + 'insertq' => 'spip_mysql_insertq', |
|
| 117 | + 'insertq_multi' => 'spip_mysql_insertq_multi', |
|
| 118 | + 'listdbs' => 'spip_mysql_listdbs', |
|
| 119 | + 'multi' => 'spip_mysql_multi', |
|
| 120 | + 'optimize' => 'spip_mysql_optimize', |
|
| 121 | + 'query' => 'spip_mysql_query', |
|
| 122 | + 'quote' => 'spip_mysql_quote', |
|
| 123 | + 'replace' => 'spip_mysql_replace', |
|
| 124 | + 'replace_multi' => 'spip_mysql_replace_multi', |
|
| 125 | + 'repair' => 'spip_mysql_repair', |
|
| 126 | + 'select' => 'spip_mysql_select', |
|
| 127 | + 'selectdb' => 'spip_mysql_selectdb', |
|
| 128 | + 'set_charset' => 'spip_mysql_set_charset', |
|
| 129 | + 'get_charset' => 'spip_mysql_get_charset', |
|
| 130 | + 'showbase' => 'spip_mysql_showbase', |
|
| 131 | + 'showtable' => 'spip_mysql_showtable', |
|
| 132 | + 'update' => 'spip_mysql_update', |
|
| 133 | + 'updateq' => 'spip_mysql_updateq', |
|
| 134 | + |
|
| 135 | + // association de chaque nom http d'un charset aux couples MySQL |
|
| 136 | + 'charsets' => [ |
|
| 137 | + 'cp1250' => ['charset' => 'cp1250', 'collation' => 'cp1250_general_ci'], |
|
| 138 | + 'cp1251' => ['charset' => 'cp1251', 'collation' => 'cp1251_general_ci'], |
|
| 139 | + 'cp1256' => ['charset' => 'cp1256', 'collation' => 'cp1256_general_ci'], |
|
| 140 | + 'iso-8859-1' => ['charset' => 'latin1', 'collation' => 'latin1_swedish_ci'], |
|
| 141 | 141 | //'iso-8859-6'=>array('charset'=>'latin1','collation'=>'latin1_swedish_ci'), |
| 142 | - 'iso-8859-9' => ['charset' => 'latin5', 'collation' => 'latin5_turkish_ci'], |
|
| 142 | + 'iso-8859-9' => ['charset' => 'latin5', 'collation' => 'latin5_turkish_ci'], |
|
| 143 | 143 | //'iso-8859-15'=>array('charset'=>'latin1','collation'=>'latin1_swedish_ci'), |
| 144 | - 'utf-8' => ['charset' => 'utf8', 'collation' => 'utf8_general_ci'] |
|
| 145 | - ] |
|
| 144 | + 'utf-8' => ['charset' => 'utf8', 'collation' => 'utf8_general_ci'] |
|
| 145 | + ] |
|
| 146 | 146 | ]; |
| 147 | 147 | |
| 148 | 148 | |
@@ -153,9 +153,9 @@ discard block |
||
| 153 | 153 | * @return Object Information de connexion pour mysqli |
| 154 | 154 | */ |
| 155 | 155 | function _mysql_link($serveur = '') { |
| 156 | - $link = &$GLOBALS['connexions'][$serveur ? $serveur : 0]['link']; |
|
| 156 | + $link = &$GLOBALS['connexions'][$serveur ? $serveur : 0]['link']; |
|
| 157 | 157 | |
| 158 | - return $link; |
|
| 158 | + return $link; |
|
| 159 | 159 | } |
| 160 | 160 | |
| 161 | 161 | |
@@ -168,10 +168,10 @@ discard block |
||
| 168 | 168 | * @return resource Ressource de résultats pour fetch() |
| 169 | 169 | */ |
| 170 | 170 | function spip_mysql_set_charset($charset, $serveur = '', $requeter = true) { |
| 171 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 172 | - spip_log('changement de charset sql : ' . 'SET NAMES ' . _q($charset), _LOG_DEBUG); |
|
| 171 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 172 | + spip_log('changement de charset sql : ' . 'SET NAMES ' . _q($charset), _LOG_DEBUG); |
|
| 173 | 173 | |
| 174 | - return mysqli_query($connexion['link'], $connexion['last'] = 'SET NAMES ' . _q($charset)); |
|
| 174 | + return mysqli_query($connexion['link'], $connexion['last'] = 'SET NAMES ' . _q($charset)); |
|
| 175 | 175 | } |
| 176 | 176 | |
| 177 | 177 | |
@@ -184,11 +184,11 @@ discard block |
||
| 184 | 184 | * @return array Description du charset (son nom est dans 'charset') |
| 185 | 185 | */ |
| 186 | 186 | function spip_mysql_get_charset($charset = [], $serveur = '', $requeter = true) { |
| 187 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 188 | - $connexion['last'] = $c = 'SHOW CHARACTER SET' |
|
| 189 | - . (!$charset ? '' : (' LIKE ' . _q($charset['charset']))); |
|
| 187 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 188 | + $connexion['last'] = $c = 'SHOW CHARACTER SET' |
|
| 189 | + . (!$charset ? '' : (' LIKE ' . _q($charset['charset']))); |
|
| 190 | 190 | |
| 191 | - return spip_mysql_fetch(mysqli_query($connexion['link'], $c), null, $serveur); |
|
| 191 | + return spip_mysql_fetch(mysqli_query($connexion['link'], $c), null, $serveur); |
|
| 192 | 192 | } |
| 193 | 193 | |
| 194 | 194 | /** |
@@ -204,66 +204,66 @@ discard block |
||
| 204 | 204 | */ |
| 205 | 205 | function spip_mysql_query($query, $serveur = '', $requeter = true) { |
| 206 | 206 | |
| 207 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 208 | - $prefixe = $connexion['prefixe']; |
|
| 209 | - $link = $connexion['link']; |
|
| 210 | - $db = $connexion['db']; |
|
| 211 | - |
|
| 212 | - $query = _mysql_traite_query($query, $db, $prefixe); |
|
| 213 | - |
|
| 214 | - // renvoyer la requete inerte si demandee |
|
| 215 | - if (!$requeter) { |
|
| 216 | - return $query; |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - if (isset($_GET['var_profile']) or (defined('_DEBUG_TRACE_QUERIES') and _DEBUG_TRACE_QUERIES)) { |
|
| 220 | - include_spip('public/tracer'); |
|
| 221 | - $t = trace_query_start(); |
|
| 222 | - } else { |
|
| 223 | - $t = 0; |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - $connexion['last'] = $query; |
|
| 227 | - $connexion['total_requetes']++; |
|
| 228 | - |
|
| 229 | - // ajouter un debug utile dans log/mysql-slow.log ? |
|
| 230 | - $debug = ''; |
|
| 231 | - if (defined('_DEBUG_SLOW_QUERIES') and _DEBUG_SLOW_QUERIES) { |
|
| 232 | - if (isset($GLOBALS['debug']['aucasou'])) { |
|
| 233 | - list(, $id, , $infos) = $GLOBALS['debug']['aucasou']; |
|
| 234 | - $debug .= "BOUCLE$id @ " . (isset($infos[0]) ? $infos[0] : '') . ' | '; |
|
| 235 | - } |
|
| 236 | - if (isset($_SERVER['REQUEST_URI'])) { |
|
| 237 | - $debug .= $_SERVER['REQUEST_URI']; |
|
| 238 | - } |
|
| 239 | - if (!empty($GLOBALS['ip'])) { |
|
| 240 | - $debug .= ' + ' . $GLOBALS['ip']; |
|
| 241 | - } |
|
| 242 | - $debug = ' /* ' . mysqli_real_escape_string($link, str_replace('*/', '@/', $debug)) . ' */'; |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - $r = mysqli_query($link, $query . $debug); |
|
| 246 | - |
|
| 247 | - //Eviter de propager le GoneAway sur les autres requetes d'un même processus PHP |
|
| 248 | - if ($e = spip_mysql_errno($serveur)) { // Log d'un Gone Away |
|
| 249 | - if ($e == 2006) { //Si Gone Away on relance une connexion vierge |
|
| 250 | - //Fermer la connexion defaillante |
|
| 251 | - mysqli_close($connexion['link']); |
|
| 252 | - unset($GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]); |
|
| 253 | - //Relancer une connexion vierge |
|
| 254 | - spip_connect($serveur); |
|
| 255 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 256 | - $link = $connexion['link']; |
|
| 257 | - //On retente au cas où |
|
| 258 | - $r = mysqli_query($link, $query . $debug); |
|
| 259 | - } |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - // Log de l'erreur eventuelle |
|
| 263 | - if ($e = spip_mysql_errno($serveur)) { |
|
| 264 | - $e .= spip_mysql_error($query, $serveur); |
|
| 265 | - } // et du fautif |
|
| 266 | - return $t ? trace_query_end($query, $t, $r, $e, $serveur) : $r; |
|
| 207 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 208 | + $prefixe = $connexion['prefixe']; |
|
| 209 | + $link = $connexion['link']; |
|
| 210 | + $db = $connexion['db']; |
|
| 211 | + |
|
| 212 | + $query = _mysql_traite_query($query, $db, $prefixe); |
|
| 213 | + |
|
| 214 | + // renvoyer la requete inerte si demandee |
|
| 215 | + if (!$requeter) { |
|
| 216 | + return $query; |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + if (isset($_GET['var_profile']) or (defined('_DEBUG_TRACE_QUERIES') and _DEBUG_TRACE_QUERIES)) { |
|
| 220 | + include_spip('public/tracer'); |
|
| 221 | + $t = trace_query_start(); |
|
| 222 | + } else { |
|
| 223 | + $t = 0; |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + $connexion['last'] = $query; |
|
| 227 | + $connexion['total_requetes']++; |
|
| 228 | + |
|
| 229 | + // ajouter un debug utile dans log/mysql-slow.log ? |
|
| 230 | + $debug = ''; |
|
| 231 | + if (defined('_DEBUG_SLOW_QUERIES') and _DEBUG_SLOW_QUERIES) { |
|
| 232 | + if (isset($GLOBALS['debug']['aucasou'])) { |
|
| 233 | + list(, $id, , $infos) = $GLOBALS['debug']['aucasou']; |
|
| 234 | + $debug .= "BOUCLE$id @ " . (isset($infos[0]) ? $infos[0] : '') . ' | '; |
|
| 235 | + } |
|
| 236 | + if (isset($_SERVER['REQUEST_URI'])) { |
|
| 237 | + $debug .= $_SERVER['REQUEST_URI']; |
|
| 238 | + } |
|
| 239 | + if (!empty($GLOBALS['ip'])) { |
|
| 240 | + $debug .= ' + ' . $GLOBALS['ip']; |
|
| 241 | + } |
|
| 242 | + $debug = ' /* ' . mysqli_real_escape_string($link, str_replace('*/', '@/', $debug)) . ' */'; |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + $r = mysqli_query($link, $query . $debug); |
|
| 246 | + |
|
| 247 | + //Eviter de propager le GoneAway sur les autres requetes d'un même processus PHP |
|
| 248 | + if ($e = spip_mysql_errno($serveur)) { // Log d'un Gone Away |
|
| 249 | + if ($e == 2006) { //Si Gone Away on relance une connexion vierge |
|
| 250 | + //Fermer la connexion defaillante |
|
| 251 | + mysqli_close($connexion['link']); |
|
| 252 | + unset($GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]); |
|
| 253 | + //Relancer une connexion vierge |
|
| 254 | + spip_connect($serveur); |
|
| 255 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 256 | + $link = $connexion['link']; |
|
| 257 | + //On retente au cas où |
|
| 258 | + $r = mysqli_query($link, $query . $debug); |
|
| 259 | + } |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + // Log de l'erreur eventuelle |
|
| 263 | + if ($e = spip_mysql_errno($serveur)) { |
|
| 264 | + $e .= spip_mysql_error($query, $serveur); |
|
| 265 | + } // et du fautif |
|
| 266 | + return $t ? trace_query_end($query, $t, $r, $e, $serveur) : $r; |
|
| 267 | 267 | } |
| 268 | 268 | |
| 269 | 269 | /** |
@@ -278,12 +278,12 @@ discard block |
||
| 278 | 278 | * - array : Tableau décrivant requête et temps d'exécution si var_profile actif pour tracer. |
| 279 | 279 | */ |
| 280 | 280 | function spip_mysql_alter($query, $serveur = '', $requeter = true) { |
| 281 | - // ici on supprime les ` entourant le nom de table pour permettre |
|
| 282 | - // la transposition du prefixe, compte tenu que les plugins ont la mauvaise habitude |
|
| 283 | - // d'utiliser ceux-ci, copie-colle de phpmyadmin |
|
| 284 | - $query = preg_replace(',^TABLE\s*`([^`]*)`,i', "TABLE \\1", $query); |
|
| 281 | + // ici on supprime les ` entourant le nom de table pour permettre |
|
| 282 | + // la transposition du prefixe, compte tenu que les plugins ont la mauvaise habitude |
|
| 283 | + // d'utiliser ceux-ci, copie-colle de phpmyadmin |
|
| 284 | + $query = preg_replace(',^TABLE\s*`([^`]*)`,i', "TABLE \\1", $query); |
|
| 285 | 285 | |
| 286 | - return spip_mysql_query('ALTER ' . $query, $serveur, $requeter); # i.e. que PG se debrouille |
|
| 286 | + return spip_mysql_query('ALTER ' . $query, $serveur, $requeter); # i.e. que PG se debrouille |
|
| 287 | 287 | } |
| 288 | 288 | |
| 289 | 289 | |
@@ -296,9 +296,9 @@ discard block |
||
| 296 | 296 | * @return bool Toujours true |
| 297 | 297 | */ |
| 298 | 298 | function spip_mysql_optimize($table, $serveur = '', $requeter = true) { |
| 299 | - spip_mysql_query('OPTIMIZE TABLE ' . $table); |
|
| 299 | + spip_mysql_query('OPTIMIZE TABLE ' . $table); |
|
| 300 | 300 | |
| 301 | - return true; |
|
| 301 | + return true; |
|
| 302 | 302 | } |
| 303 | 303 | |
| 304 | 304 | |
@@ -311,18 +311,18 @@ discard block |
||
| 311 | 311 | * @return array Tableau de l'explication |
| 312 | 312 | */ |
| 313 | 313 | function spip_mysql_explain($query, $serveur = '', $requeter = true) { |
| 314 | - if (strpos(ltrim($query), 'SELECT') !== 0) { |
|
| 315 | - return []; |
|
| 316 | - } |
|
| 317 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 318 | - $prefixe = $connexion['prefixe']; |
|
| 319 | - $link = $connexion['link']; |
|
| 320 | - $db = $connexion['db']; |
|
| 321 | - |
|
| 322 | - $query = 'EXPLAIN ' . _mysql_traite_query($query, $db, $prefixe); |
|
| 323 | - $r = mysqli_query($link, $query); |
|
| 324 | - |
|
| 325 | - return spip_mysql_fetch($r, null, $serveur); |
|
| 314 | + if (strpos(ltrim($query), 'SELECT') !== 0) { |
|
| 315 | + return []; |
|
| 316 | + } |
|
| 317 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 318 | + $prefixe = $connexion['prefixe']; |
|
| 319 | + $link = $connexion['link']; |
|
| 320 | + $db = $connexion['db']; |
|
| 321 | + |
|
| 322 | + $query = 'EXPLAIN ' . _mysql_traite_query($query, $db, $prefixe); |
|
| 323 | + $r = mysqli_query($link, $query); |
|
| 324 | + |
|
| 325 | + return spip_mysql_fetch($r, null, $serveur); |
|
| 326 | 326 | } |
| 327 | 327 | |
| 328 | 328 | |
@@ -351,35 +351,35 @@ discard block |
||
| 351 | 351 | * - array : Tableau décrivant requête et temps d'exécution si var_profile actif pour tracer. |
| 352 | 352 | */ |
| 353 | 353 | function spip_mysql_select( |
| 354 | - $select, |
|
| 355 | - $from, |
|
| 356 | - $where = '', |
|
| 357 | - $groupby = '', |
|
| 358 | - $orderby = '', |
|
| 359 | - $limit = '', |
|
| 360 | - $having = '', |
|
| 361 | - $serveur = '', |
|
| 362 | - $requeter = true |
|
| 354 | + $select, |
|
| 355 | + $from, |
|
| 356 | + $where = '', |
|
| 357 | + $groupby = '', |
|
| 358 | + $orderby = '', |
|
| 359 | + $limit = '', |
|
| 360 | + $having = '', |
|
| 361 | + $serveur = '', |
|
| 362 | + $requeter = true |
|
| 363 | 363 | ) { |
| 364 | 364 | |
| 365 | 365 | |
| 366 | - $from = (!is_array($from) ? $from : spip_mysql_select_as($from)); |
|
| 367 | - $query = |
|
| 368 | - calculer_mysql_expression('SELECT', $select, ', ') |
|
| 369 | - . calculer_mysql_expression('FROM', $from, ', ') |
|
| 370 | - . calculer_mysql_expression('WHERE', $where) |
|
| 371 | - . calculer_mysql_expression('GROUP BY', $groupby, ',') |
|
| 372 | - . calculer_mysql_expression('HAVING', $having) |
|
| 373 | - . ($orderby ? ("\nORDER BY " . spip_mysql_order($orderby)) : '') |
|
| 374 | - . ($limit ? "\nLIMIT $limit" : ''); |
|
| 366 | + $from = (!is_array($from) ? $from : spip_mysql_select_as($from)); |
|
| 367 | + $query = |
|
| 368 | + calculer_mysql_expression('SELECT', $select, ', ') |
|
| 369 | + . calculer_mysql_expression('FROM', $from, ', ') |
|
| 370 | + . calculer_mysql_expression('WHERE', $where) |
|
| 371 | + . calculer_mysql_expression('GROUP BY', $groupby, ',') |
|
| 372 | + . calculer_mysql_expression('HAVING', $having) |
|
| 373 | + . ($orderby ? ("\nORDER BY " . spip_mysql_order($orderby)) : '') |
|
| 374 | + . ($limit ? "\nLIMIT $limit" : ''); |
|
| 375 | 375 | |
| 376 | - // renvoyer la requete inerte si demandee |
|
| 377 | - if ($requeter === false) { |
|
| 378 | - return $query; |
|
| 379 | - } |
|
| 380 | - $r = spip_mysql_query($query, $serveur, $requeter); |
|
| 376 | + // renvoyer la requete inerte si demandee |
|
| 377 | + if ($requeter === false) { |
|
| 378 | + return $query; |
|
| 379 | + } |
|
| 380 | + $r = spip_mysql_query($query, $serveur, $requeter); |
|
| 381 | 381 | |
| 382 | - return $r ? $r : $query; |
|
| 382 | + return $r ? $r : $query; |
|
| 383 | 383 | } |
| 384 | 384 | |
| 385 | 385 | |
@@ -396,7 +396,7 @@ discard block |
||
| 396 | 396 | * @return string Texte du orderby préparé |
| 397 | 397 | */ |
| 398 | 398 | function spip_mysql_order($orderby) { |
| 399 | - return (is_array($orderby)) ? join(', ', $orderby) : $orderby; |
|
| 399 | + return (is_array($orderby)) ? join(', ', $orderby) : $orderby; |
|
| 400 | 400 | } |
| 401 | 401 | |
| 402 | 402 | |
@@ -419,26 +419,26 @@ discard block |
||
| 419 | 419 | * Contrainte pour clause WHERE |
| 420 | 420 | */ |
| 421 | 421 | function calculer_mysql_where($v) { |
| 422 | - if (!is_array($v)) { |
|
| 423 | - return $v; |
|
| 424 | - } |
|
| 425 | - |
|
| 426 | - $op = array_shift($v); |
|
| 427 | - if (!($n = count($v))) { |
|
| 428 | - return $op; |
|
| 429 | - } else { |
|
| 430 | - $arg = calculer_mysql_where(array_shift($v)); |
|
| 431 | - if ($n == 1) { |
|
| 432 | - return "$op($arg)"; |
|
| 433 | - } else { |
|
| 434 | - $arg2 = calculer_mysql_where(array_shift($v)); |
|
| 435 | - if ($n == 2) { |
|
| 436 | - return "($arg $op $arg2)"; |
|
| 437 | - } else { |
|
| 438 | - return "($arg $op ($arg2) : $v[0])"; |
|
| 439 | - } |
|
| 440 | - } |
|
| 441 | - } |
|
| 422 | + if (!is_array($v)) { |
|
| 423 | + return $v; |
|
| 424 | + } |
|
| 425 | + |
|
| 426 | + $op = array_shift($v); |
|
| 427 | + if (!($n = count($v))) { |
|
| 428 | + return $op; |
|
| 429 | + } else { |
|
| 430 | + $arg = calculer_mysql_where(array_shift($v)); |
|
| 431 | + if ($n == 1) { |
|
| 432 | + return "$op($arg)"; |
|
| 433 | + } else { |
|
| 434 | + $arg2 = calculer_mysql_where(array_shift($v)); |
|
| 435 | + if ($n == 2) { |
|
| 436 | + return "($arg $op $arg2)"; |
|
| 437 | + } else { |
|
| 438 | + return "($arg $op ($arg2) : $v[0])"; |
|
| 439 | + } |
|
| 440 | + } |
|
| 441 | + } |
|
| 442 | 442 | } |
| 443 | 443 | |
| 444 | 444 | /** |
@@ -453,21 +453,21 @@ discard block |
||
| 453 | 453 | * @return string Texte de l'expression, une partie donc, du texte la requête. |
| 454 | 454 | */ |
| 455 | 455 | function calculer_mysql_expression($expression, $v, $join = 'AND') { |
| 456 | - if (empty($v)) { |
|
| 457 | - return ''; |
|
| 458 | - } |
|
| 459 | - |
|
| 460 | - $exp = "\n$expression "; |
|
| 461 | - |
|
| 462 | - if (!is_array($v)) { |
|
| 463 | - return $exp . $v; |
|
| 464 | - } else { |
|
| 465 | - if (strtoupper($join) === 'AND') { |
|
| 466 | - return $exp . join("\n\t$join ", array_map('calculer_mysql_where', $v)); |
|
| 467 | - } else { |
|
| 468 | - return $exp . join($join, $v); |
|
| 469 | - } |
|
| 470 | - } |
|
| 456 | + if (empty($v)) { |
|
| 457 | + return ''; |
|
| 458 | + } |
|
| 459 | + |
|
| 460 | + $exp = "\n$expression "; |
|
| 461 | + |
|
| 462 | + if (!is_array($v)) { |
|
| 463 | + return $exp . $v; |
|
| 464 | + } else { |
|
| 465 | + if (strtoupper($join) === 'AND') { |
|
| 466 | + return $exp . join("\n\t$join ", array_map('calculer_mysql_where', $v)); |
|
| 467 | + } else { |
|
| 468 | + return $exp . join($join, $v); |
|
| 469 | + } |
|
| 470 | + } |
|
| 471 | 471 | } |
| 472 | 472 | |
| 473 | 473 | |
@@ -478,26 +478,26 @@ discard block |
||
| 478 | 478 | * @return string Sélection de colonnes pour une clause SELECT |
| 479 | 479 | */ |
| 480 | 480 | function spip_mysql_select_as($args) { |
| 481 | - $res = ''; |
|
| 482 | - foreach ($args as $k => $v) { |
|
| 483 | - if (substr($k, -1) == '@') { |
|
| 484 | - // c'est une jointure qui se refere au from precedent |
|
| 485 | - // pas de virgule |
|
| 486 | - $res .= ' ' . $v; |
|
| 487 | - } else { |
|
| 488 | - if (!is_numeric($k)) { |
|
| 489 | - $p = strpos($v, ' '); |
|
| 490 | - if ($p) { |
|
| 491 | - $v = substr($v, 0, $p) . " AS `$k`" . substr($v, $p); |
|
| 492 | - } else { |
|
| 493 | - $v .= " AS `$k`"; |
|
| 494 | - } |
|
| 495 | - } |
|
| 496 | - $res .= ', ' . $v; |
|
| 497 | - } |
|
| 498 | - } |
|
| 499 | - |
|
| 500 | - return substr($res, 2); |
|
| 481 | + $res = ''; |
|
| 482 | + foreach ($args as $k => $v) { |
|
| 483 | + if (substr($k, -1) == '@') { |
|
| 484 | + // c'est une jointure qui se refere au from precedent |
|
| 485 | + // pas de virgule |
|
| 486 | + $res .= ' ' . $v; |
|
| 487 | + } else { |
|
| 488 | + if (!is_numeric($k)) { |
|
| 489 | + $p = strpos($v, ' '); |
|
| 490 | + if ($p) { |
|
| 491 | + $v = substr($v, 0, $p) . " AS `$k`" . substr($v, $p); |
|
| 492 | + } else { |
|
| 493 | + $v .= " AS `$k`"; |
|
| 494 | + } |
|
| 495 | + } |
|
| 496 | + $res .= ', ' . $v; |
|
| 497 | + } |
|
| 498 | + } |
|
| 499 | + |
|
| 500 | + return substr($res, 2); |
|
| 501 | 501 | } |
| 502 | 502 | |
| 503 | 503 | |
@@ -521,48 +521,48 @@ discard block |
||
| 521 | 521 | */ |
| 522 | 522 | function _mysql_traite_query($query, $db = '', $prefixe = '') { |
| 523 | 523 | |
| 524 | - if ($GLOBALS['mysql_rappel_nom_base'] and $db) { |
|
| 525 | - $pref = '`' . $db . '`.'; |
|
| 526 | - } else { |
|
| 527 | - $pref = ''; |
|
| 528 | - } |
|
| 529 | - |
|
| 530 | - if ($prefixe) { |
|
| 531 | - $pref .= $prefixe . '_'; |
|
| 532 | - } |
|
| 533 | - |
|
| 534 | - if (!preg_match('/\s(SET|VALUES|WHERE|DATABASE)\s/i', $query, $regs)) { |
|
| 535 | - $suite = ''; |
|
| 536 | - } else { |
|
| 537 | - $suite = strstr($query, $regs[0]); |
|
| 538 | - $query = substr($query, 0, -strlen($suite)); |
|
| 539 | - // propager le prefixe en cas de requete imbriquee |
|
| 540 | - // il faut alors echapper les chaine avant de le faire, pour ne pas risquer de |
|
| 541 | - // modifier une requete qui est en fait juste du texte dans un champ |
|
| 542 | - if (stripos($suite, 'SELECT') !== false) { |
|
| 543 | - list($suite_echap, $textes) = query_echappe_textes($suite); |
|
| 544 | - if (preg_match('/^(.*?)([(]\s*SELECT\b.*)$/si', $suite_echap, $r)) { |
|
| 545 | - $suite_echap = $r[1] . _mysql_traite_query($r[2], $db, $prefixe); |
|
| 546 | - $suite = query_reinjecte_textes($suite_echap, $textes); |
|
| 547 | - } |
|
| 548 | - } |
|
| 549 | - } |
|
| 550 | - $r = preg_replace(_SQL_PREFIXE_TABLE_MYSQL, '\1' . $pref, $query) . $suite; |
|
| 551 | - |
|
| 552 | - // en option, remplacer les emoji (que mysql ne sait pas gérer) en 💩 |
|
| 553 | - // remplacer les emoji (que mysql ne sait pas gérer) en 💩 |
|
| 554 | - if ( |
|
| 555 | - defined('_MYSQL_NOPLANES') |
|
| 556 | - and _MYSQL_NOPLANES |
|
| 557 | - and !empty($GLOBALS['meta']['charset_sql_connexion']) |
|
| 558 | - and $GLOBALS['meta']['charset_sql_connexion'] == 'utf8' |
|
| 559 | - ) { |
|
| 560 | - include_spip('inc/charsets'); |
|
| 561 | - $r = utf8_noplanes($r); |
|
| 562 | - } |
|
| 563 | - |
|
| 564 | - #spip_log("_mysql_traite_query: " . substr($r,0, 50) . ".... $db, $prefixe", _LOG_DEBUG); |
|
| 565 | - return $r; |
|
| 524 | + if ($GLOBALS['mysql_rappel_nom_base'] and $db) { |
|
| 525 | + $pref = '`' . $db . '`.'; |
|
| 526 | + } else { |
|
| 527 | + $pref = ''; |
|
| 528 | + } |
|
| 529 | + |
|
| 530 | + if ($prefixe) { |
|
| 531 | + $pref .= $prefixe . '_'; |
|
| 532 | + } |
|
| 533 | + |
|
| 534 | + if (!preg_match('/\s(SET|VALUES|WHERE|DATABASE)\s/i', $query, $regs)) { |
|
| 535 | + $suite = ''; |
|
| 536 | + } else { |
|
| 537 | + $suite = strstr($query, $regs[0]); |
|
| 538 | + $query = substr($query, 0, -strlen($suite)); |
|
| 539 | + // propager le prefixe en cas de requete imbriquee |
|
| 540 | + // il faut alors echapper les chaine avant de le faire, pour ne pas risquer de |
|
| 541 | + // modifier une requete qui est en fait juste du texte dans un champ |
|
| 542 | + if (stripos($suite, 'SELECT') !== false) { |
|
| 543 | + list($suite_echap, $textes) = query_echappe_textes($suite); |
|
| 544 | + if (preg_match('/^(.*?)([(]\s*SELECT\b.*)$/si', $suite_echap, $r)) { |
|
| 545 | + $suite_echap = $r[1] . _mysql_traite_query($r[2], $db, $prefixe); |
|
| 546 | + $suite = query_reinjecte_textes($suite_echap, $textes); |
|
| 547 | + } |
|
| 548 | + } |
|
| 549 | + } |
|
| 550 | + $r = preg_replace(_SQL_PREFIXE_TABLE_MYSQL, '\1' . $pref, $query) . $suite; |
|
| 551 | + |
|
| 552 | + // en option, remplacer les emoji (que mysql ne sait pas gérer) en 💩 |
|
| 553 | + // remplacer les emoji (que mysql ne sait pas gérer) en 💩 |
|
| 554 | + if ( |
|
| 555 | + defined('_MYSQL_NOPLANES') |
|
| 556 | + and _MYSQL_NOPLANES |
|
| 557 | + and !empty($GLOBALS['meta']['charset_sql_connexion']) |
|
| 558 | + and $GLOBALS['meta']['charset_sql_connexion'] == 'utf8' |
|
| 559 | + ) { |
|
| 560 | + include_spip('inc/charsets'); |
|
| 561 | + $r = utf8_noplanes($r); |
|
| 562 | + } |
|
| 563 | + |
|
| 564 | + #spip_log("_mysql_traite_query: " . substr($r,0, 50) . ".... $db, $prefixe", _LOG_DEBUG); |
|
| 565 | + return $r; |
|
| 566 | 566 | } |
| 567 | 567 | |
| 568 | 568 | /** |
@@ -580,13 +580,13 @@ discard block |
||
| 580 | 580 | * - False en cas d'erreur. |
| 581 | 581 | **/ |
| 582 | 582 | function spip_mysql_selectdb($db, $serveur = '', $requeter = true) { |
| 583 | - $link = _mysql_link($serveur); |
|
| 584 | - $ok = mysqli_select_db($link, $db); |
|
| 585 | - if (!$ok) { |
|
| 586 | - spip_log('Echec mysqli_selectdb. Erreur : ' . mysqli_error($link), 'mysql.' . _LOG_CRITIQUE); |
|
| 587 | - } |
|
| 583 | + $link = _mysql_link($serveur); |
|
| 584 | + $ok = mysqli_select_db($link, $db); |
|
| 585 | + if (!$ok) { |
|
| 586 | + spip_log('Echec mysqli_selectdb. Erreur : ' . mysqli_error($link), 'mysql.' . _LOG_CRITIQUE); |
|
| 587 | + } |
|
| 588 | 588 | |
| 589 | - return $ok; |
|
| 589 | + return $ok; |
|
| 590 | 590 | } |
| 591 | 591 | |
| 592 | 592 | |
@@ -607,14 +607,14 @@ discard block |
||
| 607 | 607 | * Liste de noms de bases de données |
| 608 | 608 | **/ |
| 609 | 609 | function spip_mysql_listdbs($serveur = '', $requeter = true) { |
| 610 | - $dbs = []; |
|
| 611 | - if ($res = spip_mysql_query('SHOW DATABASES', $serveur)) { |
|
| 612 | - while ($row = mysqli_fetch_assoc($res)) { |
|
| 613 | - $dbs[] = $row['Database']; |
|
| 614 | - } |
|
| 615 | - } |
|
| 616 | - |
|
| 617 | - return $dbs; |
|
| 610 | + $dbs = []; |
|
| 611 | + if ($res = spip_mysql_query('SHOW DATABASES', $serveur)) { |
|
| 612 | + while ($row = mysqli_fetch_assoc($res)) { |
|
| 613 | + $dbs[] = $row['Database']; |
|
| 614 | + } |
|
| 615 | + } |
|
| 616 | + |
|
| 617 | + return $dbs; |
|
| 618 | 618 | } |
| 619 | 619 | |
| 620 | 620 | |
@@ -637,73 +637,73 @@ discard block |
||
| 637 | 637 | * - true si la requête réussie, false sinon. |
| 638 | 638 | */ |
| 639 | 639 | function spip_mysql_create( |
| 640 | - $nom, |
|
| 641 | - $champs, |
|
| 642 | - $cles, |
|
| 643 | - $autoinc = false, |
|
| 644 | - $temporary = false, |
|
| 645 | - $serveur = '', |
|
| 646 | - $requeter = true |
|
| 640 | + $nom, |
|
| 641 | + $champs, |
|
| 642 | + $cles, |
|
| 643 | + $autoinc = false, |
|
| 644 | + $temporary = false, |
|
| 645 | + $serveur = '', |
|
| 646 | + $requeter = true |
|
| 647 | 647 | ) { |
| 648 | 648 | |
| 649 | - $query = ''; |
|
| 650 | - $keys = ''; |
|
| 651 | - $s = ''; |
|
| 652 | - $p = ''; |
|
| 653 | - |
|
| 654 | - // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 655 | - // sans les renseigner (laisse le compilo recuperer la description) |
|
| 656 | - if (!is_array($champs) || !is_array($cles)) { |
|
| 657 | - return; |
|
| 658 | - } |
|
| 659 | - |
|
| 660 | - $res = spip_mysql_query('SELECT version() as v', $serveur); |
|
| 661 | - if (($row = mysqli_fetch_array($res)) && (version_compare($row['v'], '5.0', '>='))) { |
|
| 662 | - spip_mysql_query("SET sql_mode=''", $serveur); |
|
| 663 | - } |
|
| 664 | - |
|
| 665 | - foreach ($cles as $k => $v) { |
|
| 666 | - $keys .= "$s\n\t\t$k ($v)"; |
|
| 667 | - if ($k == 'PRIMARY KEY') { |
|
| 668 | - $p = $v; |
|
| 669 | - } |
|
| 670 | - $s = ','; |
|
| 671 | - } |
|
| 672 | - $s = ''; |
|
| 673 | - |
|
| 674 | - $character_set = ''; |
|
| 675 | - if (@$GLOBALS['meta']['charset_sql_base']) { |
|
| 676 | - $character_set .= ' CHARACTER SET ' . $GLOBALS['meta']['charset_sql_base']; |
|
| 677 | - } |
|
| 678 | - if (@$GLOBALS['meta']['charset_collation_sql_base']) { |
|
| 679 | - $character_set .= ' COLLATE ' . $GLOBALS['meta']['charset_collation_sql_base']; |
|
| 680 | - } |
|
| 681 | - |
|
| 682 | - foreach ($champs as $k => $v) { |
|
| 683 | - $v = _mysql_remplacements_definitions_table($v); |
|
| 684 | - if (preg_match(',([a-z]*\s*(\(\s*[0-9]*\s*\))?(\s*binary)?),i', $v, $defs)) { |
|
| 685 | - if ( |
|
| 686 | - preg_match(',(char|text),i', $defs[1]) |
|
| 687 | - and !preg_match(',(binary|CHARACTER|COLLATE),i', $v) |
|
| 688 | - ) { |
|
| 689 | - $v = $defs[1] . $character_set . ' ' . substr($v, strlen($defs[1])); |
|
| 690 | - } |
|
| 691 | - } |
|
| 692 | - |
|
| 693 | - $query .= "$s\n\t\t$k $v" |
|
| 694 | - . (($autoinc && ($p == $k) && preg_match(',\b(big|small|medium)?int\b,i', $v)) |
|
| 695 | - ? ' auto_increment' |
|
| 696 | - : '' |
|
| 697 | - ); |
|
| 698 | - $s = ','; |
|
| 699 | - } |
|
| 700 | - $temporary = $temporary ? 'TEMPORARY' : ''; |
|
| 701 | - $q = "CREATE $temporary TABLE IF NOT EXISTS $nom ($query" . ($keys ? ",$keys" : '') . ')' |
|
| 702 | - . ' ENGINE=MyISAM' |
|
| 703 | - . ($character_set ? " DEFAULT $character_set" : '') |
|
| 704 | - . "\n"; |
|
| 705 | - |
|
| 706 | - return spip_mysql_query($q, $serveur); |
|
| 649 | + $query = ''; |
|
| 650 | + $keys = ''; |
|
| 651 | + $s = ''; |
|
| 652 | + $p = ''; |
|
| 653 | + |
|
| 654 | + // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 655 | + // sans les renseigner (laisse le compilo recuperer la description) |
|
| 656 | + if (!is_array($champs) || !is_array($cles)) { |
|
| 657 | + return; |
|
| 658 | + } |
|
| 659 | + |
|
| 660 | + $res = spip_mysql_query('SELECT version() as v', $serveur); |
|
| 661 | + if (($row = mysqli_fetch_array($res)) && (version_compare($row['v'], '5.0', '>='))) { |
|
| 662 | + spip_mysql_query("SET sql_mode=''", $serveur); |
|
| 663 | + } |
|
| 664 | + |
|
| 665 | + foreach ($cles as $k => $v) { |
|
| 666 | + $keys .= "$s\n\t\t$k ($v)"; |
|
| 667 | + if ($k == 'PRIMARY KEY') { |
|
| 668 | + $p = $v; |
|
| 669 | + } |
|
| 670 | + $s = ','; |
|
| 671 | + } |
|
| 672 | + $s = ''; |
|
| 673 | + |
|
| 674 | + $character_set = ''; |
|
| 675 | + if (@$GLOBALS['meta']['charset_sql_base']) { |
|
| 676 | + $character_set .= ' CHARACTER SET ' . $GLOBALS['meta']['charset_sql_base']; |
|
| 677 | + } |
|
| 678 | + if (@$GLOBALS['meta']['charset_collation_sql_base']) { |
|
| 679 | + $character_set .= ' COLLATE ' . $GLOBALS['meta']['charset_collation_sql_base']; |
|
| 680 | + } |
|
| 681 | + |
|
| 682 | + foreach ($champs as $k => $v) { |
|
| 683 | + $v = _mysql_remplacements_definitions_table($v); |
|
| 684 | + if (preg_match(',([a-z]*\s*(\(\s*[0-9]*\s*\))?(\s*binary)?),i', $v, $defs)) { |
|
| 685 | + if ( |
|
| 686 | + preg_match(',(char|text),i', $defs[1]) |
|
| 687 | + and !preg_match(',(binary|CHARACTER|COLLATE),i', $v) |
|
| 688 | + ) { |
|
| 689 | + $v = $defs[1] . $character_set . ' ' . substr($v, strlen($defs[1])); |
|
| 690 | + } |
|
| 691 | + } |
|
| 692 | + |
|
| 693 | + $query .= "$s\n\t\t$k $v" |
|
| 694 | + . (($autoinc && ($p == $k) && preg_match(',\b(big|small|medium)?int\b,i', $v)) |
|
| 695 | + ? ' auto_increment' |
|
| 696 | + : '' |
|
| 697 | + ); |
|
| 698 | + $s = ','; |
|
| 699 | + } |
|
| 700 | + $temporary = $temporary ? 'TEMPORARY' : ''; |
|
| 701 | + $q = "CREATE $temporary TABLE IF NOT EXISTS $nom ($query" . ($keys ? ",$keys" : '') . ')' |
|
| 702 | + . ' ENGINE=MyISAM' |
|
| 703 | + . ($character_set ? " DEFAULT $character_set" : '') |
|
| 704 | + . "\n"; |
|
| 705 | + |
|
| 706 | + return spip_mysql_query($q, $serveur); |
|
| 707 | 707 | } |
| 708 | 708 | |
| 709 | 709 | |
@@ -716,25 +716,25 @@ discard block |
||
| 716 | 716 | * Définition SQL adaptée pour MySQL d'un champ de table |
| 717 | 717 | */ |
| 718 | 718 | function _mysql_remplacements_definitions_table($query) { |
| 719 | - // quelques remplacements |
|
| 720 | - $num = '(\s*\([0-9]*\))?'; |
|
| 721 | - $enum = '(\s*\([^\)]*\))?'; |
|
| 722 | - |
|
| 723 | - $remplace = [ |
|
| 724 | - '/VARCHAR(\s*[^\s\(])/is' => 'VARCHAR(255)\\1', |
|
| 725 | - '/^TIMESTAMP($| NULL DEFAULT NULL)/is' => 'TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP', |
|
| 726 | - ]; |
|
| 727 | - |
|
| 728 | - if (is_string($query)) { |
|
| 729 | - $query = preg_replace(array_keys($remplace), $remplace, $query); |
|
| 730 | - } elseif (is_array($query)) { |
|
| 731 | - $keys = array_keys($remplace); |
|
| 732 | - foreach ($query as $k => $q) { |
|
| 733 | - $query[$k] = preg_replace($keys, $remplace, $q); |
|
| 734 | - } |
|
| 735 | - } |
|
| 736 | - |
|
| 737 | - return $query; |
|
| 719 | + // quelques remplacements |
|
| 720 | + $num = '(\s*\([0-9]*\))?'; |
|
| 721 | + $enum = '(\s*\([^\)]*\))?'; |
|
| 722 | + |
|
| 723 | + $remplace = [ |
|
| 724 | + '/VARCHAR(\s*[^\s\(])/is' => 'VARCHAR(255)\\1', |
|
| 725 | + '/^TIMESTAMP($| NULL DEFAULT NULL)/is' => 'TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP', |
|
| 726 | + ]; |
|
| 727 | + |
|
| 728 | + if (is_string($query)) { |
|
| 729 | + $query = preg_replace(array_keys($remplace), $remplace, $query); |
|
| 730 | + } elseif (is_array($query)) { |
|
| 731 | + $keys = array_keys($remplace); |
|
| 732 | + foreach ($query as $k => $q) { |
|
| 733 | + $query[$k] = preg_replace($keys, $remplace, $q); |
|
| 734 | + } |
|
| 735 | + } |
|
| 736 | + |
|
| 737 | + return $query; |
|
| 738 | 738 | } |
| 739 | 739 | |
| 740 | 740 | |
@@ -747,7 +747,7 @@ discard block |
||
| 747 | 747 | * @return bool true si la base est créee. |
| 748 | 748 | **/ |
| 749 | 749 | function spip_mysql_create_base($nom, $serveur = '', $requeter = true) { |
| 750 | - return spip_mysql_query("CREATE DATABASE `$nom`", $serveur, $requeter); |
|
| 750 | + return spip_mysql_query("CREATE DATABASE `$nom`", $serveur, $requeter); |
|
| 751 | 751 | } |
| 752 | 752 | |
| 753 | 753 | |
@@ -768,19 +768,19 @@ discard block |
||
| 768 | 768 | * - string texte de la requête si $requeter vaut false |
| 769 | 769 | */ |
| 770 | 770 | function spip_mysql_create_view($nom, $query_select, $serveur = '', $requeter = true) { |
| 771 | - if (!$query_select) { |
|
| 772 | - return false; |
|
| 773 | - } |
|
| 774 | - // vue deja presente |
|
| 775 | - if (sql_showtable($nom, false, $serveur)) { |
|
| 776 | - spip_log("Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", _LOG_ERREUR); |
|
| 771 | + if (!$query_select) { |
|
| 772 | + return false; |
|
| 773 | + } |
|
| 774 | + // vue deja presente |
|
| 775 | + if (sql_showtable($nom, false, $serveur)) { |
|
| 776 | + spip_log("Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", _LOG_ERREUR); |
|
| 777 | 777 | |
| 778 | - return false; |
|
| 779 | - } |
|
| 778 | + return false; |
|
| 779 | + } |
|
| 780 | 780 | |
| 781 | - $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 781 | + $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 782 | 782 | |
| 783 | - return spip_mysql_query($query, $serveur, $requeter); |
|
| 783 | + return spip_mysql_query($query, $serveur, $requeter); |
|
| 784 | 784 | } |
| 785 | 785 | |
| 786 | 786 | |
@@ -796,11 +796,11 @@ discard block |
||
| 796 | 796 | * - true si la requête a réussie, false sinon |
| 797 | 797 | */ |
| 798 | 798 | function spip_mysql_drop_table($table, $exist = '', $serveur = '', $requeter = true) { |
| 799 | - if ($exist) { |
|
| 800 | - $exist = ' IF EXISTS'; |
|
| 801 | - } |
|
| 799 | + if ($exist) { |
|
| 800 | + $exist = ' IF EXISTS'; |
|
| 801 | + } |
|
| 802 | 802 | |
| 803 | - return spip_mysql_query("DROP TABLE$exist $table", $serveur, $requeter); |
|
| 803 | + return spip_mysql_query("DROP TABLE$exist $table", $serveur, $requeter); |
|
| 804 | 804 | } |
| 805 | 805 | |
| 806 | 806 | /** |
@@ -815,11 +815,11 @@ discard block |
||
| 815 | 815 | * - true si la requête a réussie, false sinon |
| 816 | 816 | */ |
| 817 | 817 | function spip_mysql_drop_view($view, $exist = '', $serveur = '', $requeter = true) { |
| 818 | - if ($exist) { |
|
| 819 | - $exist = ' IF EXISTS'; |
|
| 820 | - } |
|
| 818 | + if ($exist) { |
|
| 819 | + $exist = ' IF EXISTS'; |
|
| 820 | + } |
|
| 821 | 821 | |
| 822 | - return spip_mysql_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 822 | + return spip_mysql_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 823 | 823 | } |
| 824 | 824 | |
| 825 | 825 | /** |
@@ -836,7 +836,7 @@ discard block |
||
| 836 | 836 | * Ressource à utiliser avec sql_fetch() |
| 837 | 837 | **/ |
| 838 | 838 | function spip_mysql_showbase($match, $serveur = '', $requeter = true) { |
| 839 | - return spip_mysql_query('SHOW TABLES LIKE ' . _q($match), $serveur, $requeter); |
|
| 839 | + return spip_mysql_query('SHOW TABLES LIKE ' . _q($match), $serveur, $requeter); |
|
| 840 | 840 | } |
| 841 | 841 | |
| 842 | 842 | /** |
@@ -852,7 +852,7 @@ discard block |
||
| 852 | 852 | * - true si la requête a réussie, false sinon |
| 853 | 853 | */ |
| 854 | 854 | function spip_mysql_repair($table, $serveur = '', $requeter = true) { |
| 855 | - return spip_mysql_query("REPAIR TABLE `$table`", $serveur, $requeter); |
|
| 855 | + return spip_mysql_query("REPAIR TABLE `$table`", $serveur, $requeter); |
|
| 856 | 856 | } |
| 857 | 857 | |
| 858 | 858 | |
@@ -876,86 +876,86 @@ discard block |
||
| 876 | 876 | * - array description de la table sinon |
| 877 | 877 | */ |
| 878 | 878 | function spip_mysql_showtable($nom_table, $serveur = '', $requeter = true) { |
| 879 | - $s = spip_mysql_query("SHOW CREATE TABLE `$nom_table`", $serveur, $requeter); |
|
| 880 | - if (!$s) { |
|
| 881 | - return ''; |
|
| 882 | - } |
|
| 883 | - if (!$requeter) { |
|
| 884 | - return $s; |
|
| 885 | - } |
|
| 886 | - |
|
| 887 | - list(, $a) = mysqli_fetch_array($s, MYSQLI_NUM); |
|
| 888 | - if (preg_match(_MYSQL_RE_SHOW_TABLE, $a, $r)) { |
|
| 889 | - $desc = $r[1]; |
|
| 890 | - // extraction d'une KEY éventuelle en prenant garde de ne pas |
|
| 891 | - // relever un champ dont le nom contient KEY (ex. ID_WHISKEY) |
|
| 892 | - if (preg_match('/^(.*?),([^,]*\sKEY[ (].*)$/s', $desc, $r)) { |
|
| 893 | - $namedkeys = $r[2]; |
|
| 894 | - $desc = $r[1]; |
|
| 895 | - } else { |
|
| 896 | - $namedkeys = ''; |
|
| 897 | - } |
|
| 898 | - |
|
| 899 | - $fields = []; |
|
| 900 | - foreach (preg_split('/,\s*`/', $desc) as $v) { |
|
| 901 | - preg_match('/^\s*`?([^`]*)`\s*(.*)/', $v, $r); |
|
| 902 | - $fields[strtolower($r[1])] = $r[2]; |
|
| 903 | - } |
|
| 904 | - $keys = []; |
|
| 905 | - |
|
| 906 | - foreach (preg_split('/\)\s*(,|$)/', $namedkeys) as $v) { |
|
| 907 | - if (preg_match('/^\s*([^(]*)\(([^(]*(\(\d+\))?)$/', $v, $r)) { |
|
| 908 | - $k = str_replace('`', '', trim($r[1])); |
|
| 909 | - $t = strtolower(str_replace('`', '', $r[2])); |
|
| 910 | - if ($k && !isset($keys[$k])) { |
|
| 911 | - $keys[$k] = $t; |
|
| 912 | - } else { |
|
| 913 | - $keys[] = $t; |
|
| 914 | - } |
|
| 915 | - } |
|
| 916 | - } |
|
| 917 | - spip_mysql_free($s); |
|
| 918 | - |
|
| 919 | - return ['field' => $fields, 'key' => $keys]; |
|
| 920 | - } |
|
| 921 | - |
|
| 922 | - $res = spip_mysql_query("SHOW COLUMNS FROM `$nom_table`", $serveur); |
|
| 923 | - if ($res) { |
|
| 924 | - $nfields = []; |
|
| 925 | - $nkeys = []; |
|
| 926 | - while ($val = spip_mysql_fetch($res)) { |
|
| 927 | - $nfields[$val['Field']] = $val['Type']; |
|
| 928 | - if ($val['Null'] == 'NO') { |
|
| 929 | - $nfields[$val['Field']] .= ' NOT NULL'; |
|
| 930 | - } |
|
| 931 | - if ($val['Default'] === '0' || $val['Default']) { |
|
| 932 | - if (preg_match('/[A-Z_]/', $val['Default'])) { |
|
| 933 | - $nfields[$val['Field']] .= ' DEFAULT ' . $val['Default']; |
|
| 934 | - } else { |
|
| 935 | - $nfields[$val['Field']] .= " DEFAULT '" . $val['Default'] . "'"; |
|
| 936 | - } |
|
| 937 | - } |
|
| 938 | - if ($val['Extra']) { |
|
| 939 | - $nfields[$val['Field']] .= ' ' . $val['Extra']; |
|
| 940 | - } |
|
| 941 | - if ($val['Key'] == 'PRI') { |
|
| 942 | - $nkeys['PRIMARY KEY'] = $val['Field']; |
|
| 943 | - } else { |
|
| 944 | - if ($val['Key'] == 'MUL') { |
|
| 945 | - $nkeys['KEY ' . $val['Field']] = $val['Field']; |
|
| 946 | - } else { |
|
| 947 | - if ($val['Key'] == 'UNI') { |
|
| 948 | - $nkeys['UNIQUE KEY ' . $val['Field']] = $val['Field']; |
|
| 949 | - } |
|
| 950 | - } |
|
| 951 | - } |
|
| 952 | - } |
|
| 953 | - spip_mysql_free($res); |
|
| 954 | - |
|
| 955 | - return ['field' => $nfields, 'key' => $nkeys]; |
|
| 956 | - } |
|
| 957 | - |
|
| 958 | - return ''; |
|
| 879 | + $s = spip_mysql_query("SHOW CREATE TABLE `$nom_table`", $serveur, $requeter); |
|
| 880 | + if (!$s) { |
|
| 881 | + return ''; |
|
| 882 | + } |
|
| 883 | + if (!$requeter) { |
|
| 884 | + return $s; |
|
| 885 | + } |
|
| 886 | + |
|
| 887 | + list(, $a) = mysqli_fetch_array($s, MYSQLI_NUM); |
|
| 888 | + if (preg_match(_MYSQL_RE_SHOW_TABLE, $a, $r)) { |
|
| 889 | + $desc = $r[1]; |
|
| 890 | + // extraction d'une KEY éventuelle en prenant garde de ne pas |
|
| 891 | + // relever un champ dont le nom contient KEY (ex. ID_WHISKEY) |
|
| 892 | + if (preg_match('/^(.*?),([^,]*\sKEY[ (].*)$/s', $desc, $r)) { |
|
| 893 | + $namedkeys = $r[2]; |
|
| 894 | + $desc = $r[1]; |
|
| 895 | + } else { |
|
| 896 | + $namedkeys = ''; |
|
| 897 | + } |
|
| 898 | + |
|
| 899 | + $fields = []; |
|
| 900 | + foreach (preg_split('/,\s*`/', $desc) as $v) { |
|
| 901 | + preg_match('/^\s*`?([^`]*)`\s*(.*)/', $v, $r); |
|
| 902 | + $fields[strtolower($r[1])] = $r[2]; |
|
| 903 | + } |
|
| 904 | + $keys = []; |
|
| 905 | + |
|
| 906 | + foreach (preg_split('/\)\s*(,|$)/', $namedkeys) as $v) { |
|
| 907 | + if (preg_match('/^\s*([^(]*)\(([^(]*(\(\d+\))?)$/', $v, $r)) { |
|
| 908 | + $k = str_replace('`', '', trim($r[1])); |
|
| 909 | + $t = strtolower(str_replace('`', '', $r[2])); |
|
| 910 | + if ($k && !isset($keys[$k])) { |
|
| 911 | + $keys[$k] = $t; |
|
| 912 | + } else { |
|
| 913 | + $keys[] = $t; |
|
| 914 | + } |
|
| 915 | + } |
|
| 916 | + } |
|
| 917 | + spip_mysql_free($s); |
|
| 918 | + |
|
| 919 | + return ['field' => $fields, 'key' => $keys]; |
|
| 920 | + } |
|
| 921 | + |
|
| 922 | + $res = spip_mysql_query("SHOW COLUMNS FROM `$nom_table`", $serveur); |
|
| 923 | + if ($res) { |
|
| 924 | + $nfields = []; |
|
| 925 | + $nkeys = []; |
|
| 926 | + while ($val = spip_mysql_fetch($res)) { |
|
| 927 | + $nfields[$val['Field']] = $val['Type']; |
|
| 928 | + if ($val['Null'] == 'NO') { |
|
| 929 | + $nfields[$val['Field']] .= ' NOT NULL'; |
|
| 930 | + } |
|
| 931 | + if ($val['Default'] === '0' || $val['Default']) { |
|
| 932 | + if (preg_match('/[A-Z_]/', $val['Default'])) { |
|
| 933 | + $nfields[$val['Field']] .= ' DEFAULT ' . $val['Default']; |
|
| 934 | + } else { |
|
| 935 | + $nfields[$val['Field']] .= " DEFAULT '" . $val['Default'] . "'"; |
|
| 936 | + } |
|
| 937 | + } |
|
| 938 | + if ($val['Extra']) { |
|
| 939 | + $nfields[$val['Field']] .= ' ' . $val['Extra']; |
|
| 940 | + } |
|
| 941 | + if ($val['Key'] == 'PRI') { |
|
| 942 | + $nkeys['PRIMARY KEY'] = $val['Field']; |
|
| 943 | + } else { |
|
| 944 | + if ($val['Key'] == 'MUL') { |
|
| 945 | + $nkeys['KEY ' . $val['Field']] = $val['Field']; |
|
| 946 | + } else { |
|
| 947 | + if ($val['Key'] == 'UNI') { |
|
| 948 | + $nkeys['UNIQUE KEY ' . $val['Field']] = $val['Field']; |
|
| 949 | + } |
|
| 950 | + } |
|
| 951 | + } |
|
| 952 | + } |
|
| 953 | + spip_mysql_free($res); |
|
| 954 | + |
|
| 955 | + return ['field' => $nfields, 'key' => $nkeys]; |
|
| 956 | + } |
|
| 957 | + |
|
| 958 | + return ''; |
|
| 959 | 959 | } |
| 960 | 960 | |
| 961 | 961 | |
@@ -971,12 +971,12 @@ discard block |
||
| 971 | 971 | * @return array Ligne de résultat |
| 972 | 972 | */ |
| 973 | 973 | function spip_mysql_fetch($r, $t = '', $serveur = '', $requeter = true) { |
| 974 | - if (!$t) { |
|
| 975 | - $t = MYSQLI_ASSOC; |
|
| 976 | - } |
|
| 977 | - if ($r) { |
|
| 978 | - return mysqli_fetch_array($r, $t); |
|
| 979 | - } |
|
| 974 | + if (!$t) { |
|
| 975 | + $t = MYSQLI_ASSOC; |
|
| 976 | + } |
|
| 977 | + if ($r) { |
|
| 978 | + return mysqli_fetch_array($r, $t); |
|
| 979 | + } |
|
| 980 | 980 | } |
| 981 | 981 | |
| 982 | 982 | /** |
@@ -989,9 +989,9 @@ discard block |
||
| 989 | 989 | * @return bool True si déplacement réussi, false sinon. |
| 990 | 990 | **/ |
| 991 | 991 | function spip_mysql_seek($r, $row_number, $serveur = '', $requeter = true) { |
| 992 | - if ($r and mysqli_num_rows($r)) { |
|
| 993 | - return mysqli_data_seek($r, $row_number); |
|
| 994 | - } |
|
| 992 | + if ($r and mysqli_num_rows($r)) { |
|
| 993 | + return mysqli_data_seek($r, $row_number); |
|
| 994 | + } |
|
| 995 | 995 | } |
| 996 | 996 | |
| 997 | 997 | |
@@ -1009,26 +1009,26 @@ discard block |
||
| 1009 | 1009 | * - int Nombre de lignes (0 si la requête n'a pas réussie) |
| 1010 | 1010 | **/ |
| 1011 | 1011 | function spip_mysql_countsel( |
| 1012 | - $from = [], |
|
| 1013 | - $where = [], |
|
| 1014 | - $groupby = '', |
|
| 1015 | - $having = [], |
|
| 1016 | - $serveur = '', |
|
| 1017 | - $requeter = true |
|
| 1012 | + $from = [], |
|
| 1013 | + $where = [], |
|
| 1014 | + $groupby = '', |
|
| 1015 | + $having = [], |
|
| 1016 | + $serveur = '', |
|
| 1017 | + $requeter = true |
|
| 1018 | 1018 | ) { |
| 1019 | - $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 1020 | - |
|
| 1021 | - $r = spip_mysql_select("COUNT($c)", $from, $where, '', '', '', $having, $serveur, $requeter); |
|
| 1022 | - if (!$requeter) { |
|
| 1023 | - return $r; |
|
| 1024 | - } |
|
| 1025 | - if (!$r instanceof mysqli_result) { |
|
| 1026 | - return 0; |
|
| 1027 | - } |
|
| 1028 | - list($c) = mysqli_fetch_array($r, MYSQLI_NUM); |
|
| 1029 | - mysqli_free_result($r); |
|
| 1030 | - |
|
| 1031 | - return $c; |
|
| 1019 | + $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 1020 | + |
|
| 1021 | + $r = spip_mysql_select("COUNT($c)", $from, $where, '', '', '', $having, $serveur, $requeter); |
|
| 1022 | + if (!$requeter) { |
|
| 1023 | + return $r; |
|
| 1024 | + } |
|
| 1025 | + if (!$r instanceof mysqli_result) { |
|
| 1026 | + return 0; |
|
| 1027 | + } |
|
| 1028 | + list($c) = mysqli_fetch_array($r, MYSQLI_NUM); |
|
| 1029 | + mysqli_free_result($r); |
|
| 1030 | + |
|
| 1031 | + return $c; |
|
| 1032 | 1032 | } |
| 1033 | 1033 | |
| 1034 | 1034 | |
@@ -1051,16 +1051,16 @@ discard block |
||
| 1051 | 1051 | * Erreur eventuelle |
| 1052 | 1052 | **/ |
| 1053 | 1053 | function spip_mysql_error($query = '', $serveur = '', $requeter = true) { |
| 1054 | - $link = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]['link']; |
|
| 1055 | - $s = mysqli_error($link); |
|
| 1056 | - if ($s) { |
|
| 1057 | - $trace = debug_backtrace(); |
|
| 1058 | - if ($trace[0]['function'] != 'spip_mysql_error') { |
|
| 1059 | - spip_log("$s - $query - " . sql_error_backtrace(), 'mysql.' . _LOG_ERREUR); |
|
| 1060 | - } |
|
| 1061 | - } |
|
| 1062 | - |
|
| 1063 | - return $s; |
|
| 1054 | + $link = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]['link']; |
|
| 1055 | + $s = mysqli_error($link); |
|
| 1056 | + if ($s) { |
|
| 1057 | + $trace = debug_backtrace(); |
|
| 1058 | + if ($trace[0]['function'] != 'spip_mysql_error') { |
|
| 1059 | + spip_log("$s - $query - " . sql_error_backtrace(), 'mysql.' . _LOG_ERREUR); |
|
| 1060 | + } |
|
| 1061 | + } |
|
| 1062 | + |
|
| 1063 | + return $s; |
|
| 1064 | 1064 | } |
| 1065 | 1065 | |
| 1066 | 1066 | |
@@ -1075,18 +1075,18 @@ discard block |
||
| 1075 | 1075 | * 0, pas d'erreur. Autre, numéro de l'erreur. |
| 1076 | 1076 | **/ |
| 1077 | 1077 | function spip_mysql_errno($serveur = '', $requeter = true) { |
| 1078 | - $link = $GLOBALS['connexions'][$serveur ? $serveur : 0]['link']; |
|
| 1079 | - $s = mysqli_errno($link); |
|
| 1080 | - // 2006 MySQL server has gone away |
|
| 1081 | - // 2013 Lost connection to MySQL server during query |
|
| 1082 | - if (in_array($s, [2006, 2013])) { |
|
| 1083 | - define('spip_interdire_cache', true); |
|
| 1084 | - } |
|
| 1085 | - if ($s) { |
|
| 1086 | - spip_log("Erreur mysql $s", _LOG_ERREUR); |
|
| 1087 | - } |
|
| 1088 | - |
|
| 1089 | - return $s; |
|
| 1078 | + $link = $GLOBALS['connexions'][$serveur ? $serveur : 0]['link']; |
|
| 1079 | + $s = mysqli_errno($link); |
|
| 1080 | + // 2006 MySQL server has gone away |
|
| 1081 | + // 2013 Lost connection to MySQL server during query |
|
| 1082 | + if (in_array($s, [2006, 2013])) { |
|
| 1083 | + define('spip_interdire_cache', true); |
|
| 1084 | + } |
|
| 1085 | + if ($s) { |
|
| 1086 | + spip_log("Erreur mysql $s", _LOG_ERREUR); |
|
| 1087 | + } |
|
| 1088 | + |
|
| 1089 | + return $s; |
|
| 1090 | 1090 | } |
| 1091 | 1091 | |
| 1092 | 1092 | |
@@ -1100,9 +1100,9 @@ discard block |
||
| 1100 | 1100 | * @return int Nombre de lignes |
| 1101 | 1101 | */ |
| 1102 | 1102 | function spip_mysql_count($r, $serveur = '', $requeter = true) { |
| 1103 | - if ($r) { |
|
| 1104 | - return mysqli_num_rows($r); |
|
| 1105 | - } |
|
| 1103 | + if ($r) { |
|
| 1104 | + return mysqli_num_rows($r); |
|
| 1105 | + } |
|
| 1106 | 1106 | } |
| 1107 | 1107 | |
| 1108 | 1108 | |
@@ -1118,7 +1118,7 @@ discard block |
||
| 1118 | 1118 | * @return bool True si réussi |
| 1119 | 1119 | */ |
| 1120 | 1120 | function spip_mysql_free($r, $serveur = '', $requeter = true) { |
| 1121 | - return (($r instanceof mysqli_result) ? mysqli_free_result($r) : false); |
|
| 1121 | + return (($r instanceof mysqli_result) ? mysqli_free_result($r) : false); |
|
| 1122 | 1122 | } |
| 1123 | 1123 | |
| 1124 | 1124 | |
@@ -1146,49 +1146,49 @@ discard block |
||
| 1146 | 1146 | **/ |
| 1147 | 1147 | function spip_mysql_insert($table, $champs, $valeurs, $desc = [], $serveur = '', $requeter = true) { |
| 1148 | 1148 | |
| 1149 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1150 | - $link = $connexion['link']; |
|
| 1151 | - $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 1152 | - |
|
| 1153 | - // remplacer les emoji (que mysql ne sait pas gérer) en 💩 |
|
| 1154 | - if ( |
|
| 1155 | - defined('_MYSQL_NOPLANES') |
|
| 1156 | - and _MYSQL_NOPLANES |
|
| 1157 | - and !empty($GLOBALS['meta']['charset_sql_connexion']) |
|
| 1158 | - and $GLOBALS['meta']['charset_sql_connexion'] == 'utf8' |
|
| 1159 | - ) { |
|
| 1160 | - include_spip('inc/charsets'); |
|
| 1161 | - $valeurs = utf8_noplanes($valeurs); |
|
| 1162 | - } |
|
| 1163 | - |
|
| 1164 | - $query = "INSERT INTO $table $champs VALUES $valeurs"; |
|
| 1165 | - if (!$requeter) { |
|
| 1166 | - return $query; |
|
| 1167 | - } |
|
| 1168 | - |
|
| 1169 | - if (isset($_GET['var_profile'])) { |
|
| 1170 | - include_spip('public/tracer'); |
|
| 1171 | - $t = trace_query_start(); |
|
| 1172 | - $e = ''; |
|
| 1173 | - } else { |
|
| 1174 | - $t = 0; |
|
| 1175 | - } |
|
| 1176 | - |
|
| 1177 | - $connexion['last'] = $query; |
|
| 1178 | - #spip_log($query, 'mysql.'._LOG_DEBUG); |
|
| 1179 | - $r = false; |
|
| 1180 | - if (mysqli_query($link, $query)) { |
|
| 1181 | - $r = mysqli_insert_id($link); |
|
| 1182 | - } else { |
|
| 1183 | - // Log de l'erreur eventuelle |
|
| 1184 | - if ($e = spip_mysql_errno($serveur)) { |
|
| 1185 | - $e .= spip_mysql_error($query, $serveur); |
|
| 1186 | - } // et du fautif |
|
| 1187 | - } |
|
| 1188 | - |
|
| 1189 | - return $t ? trace_query_end($query, $t, $r, $e, $serveur) : $r; |
|
| 1190 | - |
|
| 1191 | - // return $r ? $r : (($r===0) ? -1 : 0); pb avec le multi-base. |
|
| 1149 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1150 | + $link = $connexion['link']; |
|
| 1151 | + $table = prefixer_table_spip($table, $connexion['prefixe']); |
|
| 1152 | + |
|
| 1153 | + // remplacer les emoji (que mysql ne sait pas gérer) en 💩 |
|
| 1154 | + if ( |
|
| 1155 | + defined('_MYSQL_NOPLANES') |
|
| 1156 | + and _MYSQL_NOPLANES |
|
| 1157 | + and !empty($GLOBALS['meta']['charset_sql_connexion']) |
|
| 1158 | + and $GLOBALS['meta']['charset_sql_connexion'] == 'utf8' |
|
| 1159 | + ) { |
|
| 1160 | + include_spip('inc/charsets'); |
|
| 1161 | + $valeurs = utf8_noplanes($valeurs); |
|
| 1162 | + } |
|
| 1163 | + |
|
| 1164 | + $query = "INSERT INTO $table $champs VALUES $valeurs"; |
|
| 1165 | + if (!$requeter) { |
|
| 1166 | + return $query; |
|
| 1167 | + } |
|
| 1168 | + |
|
| 1169 | + if (isset($_GET['var_profile'])) { |
|
| 1170 | + include_spip('public/tracer'); |
|
| 1171 | + $t = trace_query_start(); |
|
| 1172 | + $e = ''; |
|
| 1173 | + } else { |
|
| 1174 | + $t = 0; |
|
| 1175 | + } |
|
| 1176 | + |
|
| 1177 | + $connexion['last'] = $query; |
|
| 1178 | + #spip_log($query, 'mysql.'._LOG_DEBUG); |
|
| 1179 | + $r = false; |
|
| 1180 | + if (mysqli_query($link, $query)) { |
|
| 1181 | + $r = mysqli_insert_id($link); |
|
| 1182 | + } else { |
|
| 1183 | + // Log de l'erreur eventuelle |
|
| 1184 | + if ($e = spip_mysql_errno($serveur)) { |
|
| 1185 | + $e .= spip_mysql_error($query, $serveur); |
|
| 1186 | + } // et du fautif |
|
| 1187 | + } |
|
| 1188 | + |
|
| 1189 | + return $t ? trace_query_end($query, $t, $r, $e, $serveur) : $r; |
|
| 1190 | + |
|
| 1191 | + // return $r ? $r : (($r===0) ? -1 : 0); pb avec le multi-base. |
|
| 1192 | 1192 | } |
| 1193 | 1193 | |
| 1194 | 1194 | /** |
@@ -1213,26 +1213,26 @@ discard block |
||
| 1213 | 1213 | **/ |
| 1214 | 1214 | function spip_mysql_insertq($table, $couples = [], $desc = [], $serveur = '', $requeter = true) { |
| 1215 | 1215 | |
| 1216 | - if (!$desc) { |
|
| 1217 | - $desc = description_table($table, $serveur); |
|
| 1218 | - } |
|
| 1219 | - if (!$desc) { |
|
| 1220 | - $couples = []; |
|
| 1221 | - } |
|
| 1222 | - $fields = isset($desc['field']) ? $desc['field'] : []; |
|
| 1223 | - |
|
| 1224 | - foreach ($couples as $champ => $val) { |
|
| 1225 | - $couples[$champ] = spip_mysql_cite($val, $fields[$champ]); |
|
| 1226 | - } |
|
| 1227 | - |
|
| 1228 | - return spip_mysql_insert( |
|
| 1229 | - $table, |
|
| 1230 | - '(' . join(',', array_keys($couples)) . ')', |
|
| 1231 | - '(' . join(',', $couples) . ')', |
|
| 1232 | - $desc, |
|
| 1233 | - $serveur, |
|
| 1234 | - $requeter |
|
| 1235 | - ); |
|
| 1216 | + if (!$desc) { |
|
| 1217 | + $desc = description_table($table, $serveur); |
|
| 1218 | + } |
|
| 1219 | + if (!$desc) { |
|
| 1220 | + $couples = []; |
|
| 1221 | + } |
|
| 1222 | + $fields = isset($desc['field']) ? $desc['field'] : []; |
|
| 1223 | + |
|
| 1224 | + foreach ($couples as $champ => $val) { |
|
| 1225 | + $couples[$champ] = spip_mysql_cite($val, $fields[$champ]); |
|
| 1226 | + } |
|
| 1227 | + |
|
| 1228 | + return spip_mysql_insert( |
|
| 1229 | + $table, |
|
| 1230 | + '(' . join(',', array_keys($couples)) . ')', |
|
| 1231 | + '(' . join(',', $couples) . ')', |
|
| 1232 | + $desc, |
|
| 1233 | + $serveur, |
|
| 1234 | + $requeter |
|
| 1235 | + ); |
|
| 1236 | 1236 | } |
| 1237 | 1237 | |
| 1238 | 1238 | |
@@ -1257,34 +1257,34 @@ discard block |
||
| 1257 | 1257 | **/ |
| 1258 | 1258 | function spip_mysql_insertq_multi($table, $tab_couples = [], $desc = [], $serveur = '', $requeter = true) { |
| 1259 | 1259 | |
| 1260 | - if (!$desc) { |
|
| 1261 | - $desc = description_table($table, $serveur); |
|
| 1262 | - } |
|
| 1263 | - if (!$desc) { |
|
| 1264 | - $tab_couples = []; |
|
| 1265 | - } |
|
| 1266 | - $fields = isset($desc['field']) ? $desc['field'] : []; |
|
| 1267 | - |
|
| 1268 | - $cles = '(' . join(',', array_keys(reset($tab_couples))) . ')'; |
|
| 1269 | - $valeurs = []; |
|
| 1270 | - $r = false; |
|
| 1271 | - |
|
| 1272 | - // Quoter et Inserer par groupes de 100 max pour eviter un debordement de pile |
|
| 1273 | - foreach ($tab_couples as $couples) { |
|
| 1274 | - foreach ($couples as $champ => $val) { |
|
| 1275 | - $couples[$champ] = spip_mysql_cite($val, $fields[$champ]); |
|
| 1276 | - } |
|
| 1277 | - $valeurs[] = '(' . join(',', $couples) . ')'; |
|
| 1278 | - if (count($valeurs) >= 100) { |
|
| 1279 | - $r = spip_mysql_insert($table, $cles, join(', ', $valeurs), $desc, $serveur, $requeter); |
|
| 1280 | - $valeurs = []; |
|
| 1281 | - } |
|
| 1282 | - } |
|
| 1283 | - if (count($valeurs)) { |
|
| 1284 | - $r = spip_mysql_insert($table, $cles, join(', ', $valeurs), $desc, $serveur, $requeter); |
|
| 1285 | - } |
|
| 1286 | - |
|
| 1287 | - return $r; // dans le cas d'une table auto_increment, le dernier insert_id |
|
| 1260 | + if (!$desc) { |
|
| 1261 | + $desc = description_table($table, $serveur); |
|
| 1262 | + } |
|
| 1263 | + if (!$desc) { |
|
| 1264 | + $tab_couples = []; |
|
| 1265 | + } |
|
| 1266 | + $fields = isset($desc['field']) ? $desc['field'] : []; |
|
| 1267 | + |
|
| 1268 | + $cles = '(' . join(',', array_keys(reset($tab_couples))) . ')'; |
|
| 1269 | + $valeurs = []; |
|
| 1270 | + $r = false; |
|
| 1271 | + |
|
| 1272 | + // Quoter et Inserer par groupes de 100 max pour eviter un debordement de pile |
|
| 1273 | + foreach ($tab_couples as $couples) { |
|
| 1274 | + foreach ($couples as $champ => $val) { |
|
| 1275 | + $couples[$champ] = spip_mysql_cite($val, $fields[$champ]); |
|
| 1276 | + } |
|
| 1277 | + $valeurs[] = '(' . join(',', $couples) . ')'; |
|
| 1278 | + if (count($valeurs) >= 100) { |
|
| 1279 | + $r = spip_mysql_insert($table, $cles, join(', ', $valeurs), $desc, $serveur, $requeter); |
|
| 1280 | + $valeurs = []; |
|
| 1281 | + } |
|
| 1282 | + } |
|
| 1283 | + if (count($valeurs)) { |
|
| 1284 | + $r = spip_mysql_insert($table, $cles, join(', ', $valeurs), $desc, $serveur, $requeter); |
|
| 1285 | + } |
|
| 1286 | + |
|
| 1287 | + return $r; // dans le cas d'une table auto_increment, le dernier insert_id |
|
| 1288 | 1288 | } |
| 1289 | 1289 | |
| 1290 | 1290 | /** |
@@ -1309,19 +1309,19 @@ discard block |
||
| 1309 | 1309 | * - array Tableau décrivant la requête et son temps d'exécution si var_profile est actif |
| 1310 | 1310 | */ |
| 1311 | 1311 | function spip_mysql_update($table, $champs, $where = '', $desc = [], $serveur = '', $requeter = true) { |
| 1312 | - $set = []; |
|
| 1313 | - foreach ($champs as $champ => $val) { |
|
| 1314 | - $set[] = $champ . "=$val"; |
|
| 1315 | - } |
|
| 1316 | - if (!empty($set)) { |
|
| 1317 | - return spip_mysql_query( |
|
| 1318 | - calculer_mysql_expression('UPDATE', $table, ',') |
|
| 1319 | - . calculer_mysql_expression('SET', $set, ',') |
|
| 1320 | - . calculer_mysql_expression('WHERE', $where), |
|
| 1321 | - $serveur, |
|
| 1322 | - $requeter |
|
| 1323 | - ); |
|
| 1324 | - } |
|
| 1312 | + $set = []; |
|
| 1313 | + foreach ($champs as $champ => $val) { |
|
| 1314 | + $set[] = $champ . "=$val"; |
|
| 1315 | + } |
|
| 1316 | + if (!empty($set)) { |
|
| 1317 | + return spip_mysql_query( |
|
| 1318 | + calculer_mysql_expression('UPDATE', $table, ',') |
|
| 1319 | + . calculer_mysql_expression('SET', $set, ',') |
|
| 1320 | + . calculer_mysql_expression('WHERE', $where), |
|
| 1321 | + $serveur, |
|
| 1322 | + $requeter |
|
| 1323 | + ); |
|
| 1324 | + } |
|
| 1325 | 1325 | } |
| 1326 | 1326 | |
| 1327 | 1327 | /** |
@@ -1354,29 +1354,29 @@ discard block |
||
| 1354 | 1354 | */ |
| 1355 | 1355 | function spip_mysql_updateq($table, $champs, $where = '', $desc = [], $serveur = '', $requeter = true) { |
| 1356 | 1356 | |
| 1357 | - if (!$champs) { |
|
| 1358 | - return; |
|
| 1359 | - } |
|
| 1360 | - if (!$desc) { |
|
| 1361 | - $desc = description_table($table, $serveur); |
|
| 1362 | - } |
|
| 1363 | - if (!$desc) { |
|
| 1364 | - $champs = []; |
|
| 1365 | - } else { |
|
| 1366 | - $fields = $desc['field']; |
|
| 1367 | - } |
|
| 1368 | - $set = []; |
|
| 1369 | - foreach ($champs as $champ => $val) { |
|
| 1370 | - $set[] = $champ . '=' . spip_mysql_cite($val, @$fields[$champ]); |
|
| 1371 | - } |
|
| 1372 | - |
|
| 1373 | - return spip_mysql_query( |
|
| 1374 | - calculer_mysql_expression('UPDATE', $table, ',') |
|
| 1375 | - . calculer_mysql_expression('SET', $set, ',') |
|
| 1376 | - . calculer_mysql_expression('WHERE', $where), |
|
| 1377 | - $serveur, |
|
| 1378 | - $requeter |
|
| 1379 | - ); |
|
| 1357 | + if (!$champs) { |
|
| 1358 | + return; |
|
| 1359 | + } |
|
| 1360 | + if (!$desc) { |
|
| 1361 | + $desc = description_table($table, $serveur); |
|
| 1362 | + } |
|
| 1363 | + if (!$desc) { |
|
| 1364 | + $champs = []; |
|
| 1365 | + } else { |
|
| 1366 | + $fields = $desc['field']; |
|
| 1367 | + } |
|
| 1368 | + $set = []; |
|
| 1369 | + foreach ($champs as $champ => $val) { |
|
| 1370 | + $set[] = $champ . '=' . spip_mysql_cite($val, @$fields[$champ]); |
|
| 1371 | + } |
|
| 1372 | + |
|
| 1373 | + return spip_mysql_query( |
|
| 1374 | + calculer_mysql_expression('UPDATE', $table, ',') |
|
| 1375 | + . calculer_mysql_expression('SET', $set, ',') |
|
| 1376 | + . calculer_mysql_expression('WHERE', $where), |
|
| 1377 | + $serveur, |
|
| 1378 | + $requeter |
|
| 1379 | + ); |
|
| 1380 | 1380 | } |
| 1381 | 1381 | |
| 1382 | 1382 | /** |
@@ -1392,22 +1392,22 @@ discard block |
||
| 1392 | 1392 | * - False en cas d'erreur. |
| 1393 | 1393 | **/ |
| 1394 | 1394 | function spip_mysql_delete($table, $where = '', $serveur = '', $requeter = true) { |
| 1395 | - $res = spip_mysql_query( |
|
| 1396 | - calculer_mysql_expression('DELETE FROM', $table, ',') |
|
| 1397 | - . calculer_mysql_expression('WHERE', $where), |
|
| 1398 | - $serveur, |
|
| 1399 | - $requeter |
|
| 1400 | - ); |
|
| 1401 | - if (!$requeter) { |
|
| 1402 | - return $res; |
|
| 1403 | - } |
|
| 1404 | - if ($res) { |
|
| 1405 | - $link = _mysql_link($serveur); |
|
| 1406 | - |
|
| 1407 | - return mysqli_affected_rows($link); |
|
| 1408 | - } else { |
|
| 1409 | - return false; |
|
| 1410 | - } |
|
| 1395 | + $res = spip_mysql_query( |
|
| 1396 | + calculer_mysql_expression('DELETE FROM', $table, ',') |
|
| 1397 | + . calculer_mysql_expression('WHERE', $where), |
|
| 1398 | + $serveur, |
|
| 1399 | + $requeter |
|
| 1400 | + ); |
|
| 1401 | + if (!$requeter) { |
|
| 1402 | + return $res; |
|
| 1403 | + } |
|
| 1404 | + if ($res) { |
|
| 1405 | + $link = _mysql_link($serveur); |
|
| 1406 | + |
|
| 1407 | + return mysqli_affected_rows($link); |
|
| 1408 | + } else { |
|
| 1409 | + return false; |
|
| 1410 | + } |
|
| 1411 | 1411 | } |
| 1412 | 1412 | |
| 1413 | 1413 | |
@@ -1436,10 +1436,10 @@ discard block |
||
| 1436 | 1436 | * - False en cas d'erreur. |
| 1437 | 1437 | **/ |
| 1438 | 1438 | function spip_mysql_replace($table, $couples, $desc = [], $serveur = '', $requeter = true) { |
| 1439 | - return spip_mysql_query("REPLACE $table (" . join(',', array_keys($couples)) . ') VALUES (' . join( |
|
| 1440 | - ',', |
|
| 1441 | - array_map('_q', $couples) |
|
| 1442 | - ) . ')', $serveur, $requeter); |
|
| 1439 | + return spip_mysql_query("REPLACE $table (" . join(',', array_keys($couples)) . ') VALUES (' . join( |
|
| 1440 | + ',', |
|
| 1441 | + array_map('_q', $couples) |
|
| 1442 | + ) . ')', $serveur, $requeter); |
|
| 1443 | 1443 | } |
| 1444 | 1444 | |
| 1445 | 1445 | |
@@ -1468,14 +1468,14 @@ discard block |
||
| 1468 | 1468 | * - False en cas d'erreur. |
| 1469 | 1469 | **/ |
| 1470 | 1470 | function spip_mysql_replace_multi($table, $tab_couples, $desc = [], $serveur = '', $requeter = true) { |
| 1471 | - $cles = '(' . join(',', array_keys($tab_couples[0])) . ')'; |
|
| 1472 | - $valeurs = []; |
|
| 1473 | - foreach ($tab_couples as $couples) { |
|
| 1474 | - $valeurs[] = '(' . join(',', array_map('_q', $couples)) . ')'; |
|
| 1475 | - } |
|
| 1476 | - $valeurs = implode(', ', $valeurs); |
|
| 1477 | - |
|
| 1478 | - return spip_mysql_query("REPLACE $table $cles VALUES $valeurs", $serveur, $requeter); |
|
| 1471 | + $cles = '(' . join(',', array_keys($tab_couples[0])) . ')'; |
|
| 1472 | + $valeurs = []; |
|
| 1473 | + foreach ($tab_couples as $couples) { |
|
| 1474 | + $valeurs[] = '(' . join(',', array_map('_q', $couples)) . ')'; |
|
| 1475 | + } |
|
| 1476 | + $valeurs = implode(', ', $valeurs); |
|
| 1477 | + |
|
| 1478 | + return spip_mysql_query("REPLACE $table $cles VALUES $valeurs", $serveur, $requeter); |
|
| 1479 | 1479 | } |
| 1480 | 1480 | |
| 1481 | 1481 | |
@@ -1490,32 +1490,32 @@ discard block |
||
| 1490 | 1490 | * @return string Texte de sélection pour la requête |
| 1491 | 1491 | */ |
| 1492 | 1492 | function spip_mysql_multi($objet, $lang) { |
| 1493 | - $lengthlang = strlen("[$lang]"); |
|
| 1494 | - $posmulti = 'INSTR(' . $objet . ", '<multi>')"; |
|
| 1495 | - $posfinmulti = 'INSTR(' . $objet . ", '</multi>')"; |
|
| 1496 | - $debutchaine = 'LEFT(' . $objet . ", $posmulti-1)"; |
|
| 1497 | - $finchaine = 'RIGHT(' . $objet . ', CHAR_LENGTH(' . $objet . ") -(7+$posfinmulti))"; |
|
| 1498 | - $chainemulti = 'TRIM(SUBSTRING(' . $objet . ", $posmulti+7, $posfinmulti -(7+$posmulti)))"; |
|
| 1499 | - $poslang = "INSTR($chainemulti,'[" . $lang . "]')"; |
|
| 1500 | - $poslang = "IF($poslang=0,INSTR($chainemulti,']')+1,$poslang+$lengthlang)"; |
|
| 1501 | - $chainelang = 'TRIM(SUBSTRING(' . $objet . ", $posmulti+7+$poslang-1,$posfinmulti -($posmulti+7+$poslang-1) ))"; |
|
| 1502 | - $posfinlang = 'INSTR(' . $chainelang . ", '[')"; |
|
| 1503 | - $chainelang = "IF($posfinlang>0,LEFT($chainelang,$posfinlang-1),$chainelang)"; |
|
| 1504 | - //$chainelang = "LEFT($chainelang,$posfinlang-1)"; |
|
| 1505 | - $retour = "(TRIM(IF($posmulti = 0 , " . |
|
| 1506 | - ' TRIM(' . $objet . '), ' . |
|
| 1507 | - ' CONCAT( ' . |
|
| 1508 | - " $debutchaine, " . |
|
| 1509 | - ' IF( ' . |
|
| 1510 | - " $poslang = 0, " . |
|
| 1511 | - " $chainemulti, " . |
|
| 1512 | - " $chainelang" . |
|
| 1513 | - ' ), ' . |
|
| 1514 | - " $finchaine" . |
|
| 1515 | - ' ) ' . |
|
| 1516 | - '))) AS multi'; |
|
| 1517 | - |
|
| 1518 | - return $retour; |
|
| 1493 | + $lengthlang = strlen("[$lang]"); |
|
| 1494 | + $posmulti = 'INSTR(' . $objet . ", '<multi>')"; |
|
| 1495 | + $posfinmulti = 'INSTR(' . $objet . ", '</multi>')"; |
|
| 1496 | + $debutchaine = 'LEFT(' . $objet . ", $posmulti-1)"; |
|
| 1497 | + $finchaine = 'RIGHT(' . $objet . ', CHAR_LENGTH(' . $objet . ") -(7+$posfinmulti))"; |
|
| 1498 | + $chainemulti = 'TRIM(SUBSTRING(' . $objet . ", $posmulti+7, $posfinmulti -(7+$posmulti)))"; |
|
| 1499 | + $poslang = "INSTR($chainemulti,'[" . $lang . "]')"; |
|
| 1500 | + $poslang = "IF($poslang=0,INSTR($chainemulti,']')+1,$poslang+$lengthlang)"; |
|
| 1501 | + $chainelang = 'TRIM(SUBSTRING(' . $objet . ", $posmulti+7+$poslang-1,$posfinmulti -($posmulti+7+$poslang-1) ))"; |
|
| 1502 | + $posfinlang = 'INSTR(' . $chainelang . ", '[')"; |
|
| 1503 | + $chainelang = "IF($posfinlang>0,LEFT($chainelang,$posfinlang-1),$chainelang)"; |
|
| 1504 | + //$chainelang = "LEFT($chainelang,$posfinlang-1)"; |
|
| 1505 | + $retour = "(TRIM(IF($posmulti = 0 , " . |
|
| 1506 | + ' TRIM(' . $objet . '), ' . |
|
| 1507 | + ' CONCAT( ' . |
|
| 1508 | + " $debutchaine, " . |
|
| 1509 | + ' IF( ' . |
|
| 1510 | + " $poslang = 0, " . |
|
| 1511 | + " $chainemulti, " . |
|
| 1512 | + " $chainelang" . |
|
| 1513 | + ' ), ' . |
|
| 1514 | + " $finchaine" . |
|
| 1515 | + ' ) ' . |
|
| 1516 | + '))) AS multi'; |
|
| 1517 | + |
|
| 1518 | + return $retour; |
|
| 1519 | 1519 | } |
| 1520 | 1520 | |
| 1521 | 1521 | /** |
@@ -1529,7 +1529,7 @@ discard block |
||
| 1529 | 1529 | * Valeur hexadécimale pour MySQL |
| 1530 | 1530 | **/ |
| 1531 | 1531 | function spip_mysql_hex($v) { |
| 1532 | - return '0x' . $v; |
|
| 1532 | + return '0x' . $v; |
|
| 1533 | 1533 | } |
| 1534 | 1534 | |
| 1535 | 1535 | /** |
@@ -1545,15 +1545,15 @@ discard block |
||
| 1545 | 1545 | * Donnée prête à être utilisée par le gestionnaire SQL |
| 1546 | 1546 | */ |
| 1547 | 1547 | function spip_mysql_quote($v, $type = '') { |
| 1548 | - if (!is_array($v)) { |
|
| 1549 | - return spip_mysql_cite($v, $type); |
|
| 1550 | - } |
|
| 1551 | - |
|
| 1552 | - // si c'est un tableau, le parcourir en propageant le type |
|
| 1553 | - foreach ($v as $k => $r) { |
|
| 1554 | - $v[$k] = spip_mysql_quote($r, $type); |
|
| 1555 | - } |
|
| 1556 | - return implode(',', $v); |
|
| 1548 | + if (!is_array($v)) { |
|
| 1549 | + return spip_mysql_cite($v, $type); |
|
| 1550 | + } |
|
| 1551 | + |
|
| 1552 | + // si c'est un tableau, le parcourir en propageant le type |
|
| 1553 | + foreach ($v as $k => $r) { |
|
| 1554 | + $v[$k] = spip_mysql_quote($r, $type); |
|
| 1555 | + } |
|
| 1556 | + return implode(',', $v); |
|
| 1557 | 1557 | } |
| 1558 | 1558 | |
| 1559 | 1559 | /** |
@@ -1569,18 +1569,18 @@ discard block |
||
| 1569 | 1569 | * Expression SQL |
| 1570 | 1570 | **/ |
| 1571 | 1571 | function spip_mysql_date_proche($champ, $interval, $unite) { |
| 1572 | - $use_now = ( ($champ === 'maj' or strpos($champ, '.maj')) ? true : false ); |
|
| 1573 | - return '(' |
|
| 1574 | - . $champ |
|
| 1575 | - . (($interval <= 0) ? '>' : '<') |
|
| 1576 | - . (($interval <= 0) ? 'DATE_SUB' : 'DATE_ADD') |
|
| 1577 | - . '(' |
|
| 1578 | - . ($use_now ? 'NOW()' : sql_quote(date('Y-m-d H:i:s'))) |
|
| 1579 | - . ', INTERVAL ' |
|
| 1580 | - . (($interval > 0) ? $interval : (0 - $interval)) |
|
| 1581 | - . ' ' |
|
| 1582 | - . $unite |
|
| 1583 | - . '))'; |
|
| 1572 | + $use_now = ( ($champ === 'maj' or strpos($champ, '.maj')) ? true : false ); |
|
| 1573 | + return '(' |
|
| 1574 | + . $champ |
|
| 1575 | + . (($interval <= 0) ? '>' : '<') |
|
| 1576 | + . (($interval <= 0) ? 'DATE_SUB' : 'DATE_ADD') |
|
| 1577 | + . '(' |
|
| 1578 | + . ($use_now ? 'NOW()' : sql_quote(date('Y-m-d H:i:s'))) |
|
| 1579 | + . ', INTERVAL ' |
|
| 1580 | + . (($interval > 0) ? $interval : (0 - $interval)) |
|
| 1581 | + . ' ' |
|
| 1582 | + . $unite |
|
| 1583 | + . '))'; |
|
| 1584 | 1584 | } |
| 1585 | 1585 | |
| 1586 | 1586 | |
@@ -1604,7 +1604,7 @@ discard block |
||
| 1604 | 1604 | * Expression de requête SQL |
| 1605 | 1605 | **/ |
| 1606 | 1606 | function spip_mysql_in($val, $valeurs, $not = '', $serveur = '', $requeter = true) { |
| 1607 | - return "($val $not IN ($valeurs))"; |
|
| 1607 | + return "($val $not IN ($valeurs))"; |
|
| 1608 | 1608 | } |
| 1609 | 1609 | |
| 1610 | 1610 | |
@@ -1616,38 +1616,38 @@ discard block |
||
| 1616 | 1616 | * @return string|number Texte ou nombre échappé |
| 1617 | 1617 | */ |
| 1618 | 1618 | function spip_mysql_cite($v, $type) { |
| 1619 | - if (!$type) { |
|
| 1620 | - if (is_bool($v)) { |
|
| 1621 | - return strval(intval($v)); |
|
| 1622 | - } |
|
| 1623 | - elseif (is_numeric($v)) { |
|
| 1624 | - return strval($v); |
|
| 1625 | - } |
|
| 1626 | - return "'" . addslashes($v) . "'"; |
|
| 1627 | - } |
|
| 1628 | - |
|
| 1629 | - if ( |
|
| 1630 | - is_null($v) |
|
| 1631 | - and stripos($type, 'NOT NULL') === false |
|
| 1632 | - ) { |
|
| 1633 | - return 'NULL'; |
|
| 1634 | - } // null php se traduit en NULL SQL |
|
| 1635 | - if (sql_test_date($type) and preg_match('/^\w+\(/', $v)) { |
|
| 1636 | - return $v; |
|
| 1637 | - } |
|
| 1638 | - if (sql_test_int($type)) { |
|
| 1639 | - if ( |
|
| 1640 | - is_numeric($v) or (ctype_xdigit(substr($v, 2)) |
|
| 1641 | - and $v[0] == '0' and $v[1] == 'x') |
|
| 1642 | - ) { |
|
| 1643 | - return $v; |
|
| 1644 | - } // si pas numerique, forcer le intval |
|
| 1645 | - else { |
|
| 1646 | - return intval($v); |
|
| 1647 | - } |
|
| 1648 | - } |
|
| 1649 | - |
|
| 1650 | - return ("'" . addslashes($v) . "'"); |
|
| 1619 | + if (!$type) { |
|
| 1620 | + if (is_bool($v)) { |
|
| 1621 | + return strval(intval($v)); |
|
| 1622 | + } |
|
| 1623 | + elseif (is_numeric($v)) { |
|
| 1624 | + return strval($v); |
|
| 1625 | + } |
|
| 1626 | + return "'" . addslashes($v) . "'"; |
|
| 1627 | + } |
|
| 1628 | + |
|
| 1629 | + if ( |
|
| 1630 | + is_null($v) |
|
| 1631 | + and stripos($type, 'NOT NULL') === false |
|
| 1632 | + ) { |
|
| 1633 | + return 'NULL'; |
|
| 1634 | + } // null php se traduit en NULL SQL |
|
| 1635 | + if (sql_test_date($type) and preg_match('/^\w+\(/', $v)) { |
|
| 1636 | + return $v; |
|
| 1637 | + } |
|
| 1638 | + if (sql_test_int($type)) { |
|
| 1639 | + if ( |
|
| 1640 | + is_numeric($v) or (ctype_xdigit(substr($v, 2)) |
|
| 1641 | + and $v[0] == '0' and $v[1] == 'x') |
|
| 1642 | + ) { |
|
| 1643 | + return $v; |
|
| 1644 | + } // si pas numerique, forcer le intval |
|
| 1645 | + else { |
|
| 1646 | + return intval($v); |
|
| 1647 | + } |
|
| 1648 | + } |
|
| 1649 | + |
|
| 1650 | + return ("'" . addslashes($v) . "'"); |
|
| 1651 | 1651 | } |
| 1652 | 1652 | |
| 1653 | 1653 | /** |
@@ -1657,7 +1657,7 @@ discard block |
||
| 1657 | 1657 | * True si on a les fonctions, false sinon |
| 1658 | 1658 | */ |
| 1659 | 1659 | function spip_versions_mysql() { |
| 1660 | - return function_exists('mysqli_query'); |
|
| 1660 | + return function_exists('mysqli_query'); |
|
| 1661 | 1661 | } |
| 1662 | 1662 | |
| 1663 | 1663 | |
@@ -1670,20 +1670,20 @@ discard block |
||
| 1670 | 1670 | * - chaîne : code compilé pour le faire désactiver par SPIP sinon |
| 1671 | 1671 | */ |
| 1672 | 1672 | function test_rappel_nom_base_mysql($server_db) { |
| 1673 | - $GLOBALS['mysql_rappel_nom_base'] = true; |
|
| 1674 | - sql_delete('spip_meta', "nom='mysql_rappel_nom_base'", $server_db); |
|
| 1675 | - $ok = spip_query("INSERT INTO spip_meta (nom,valeur) VALUES ('mysql_rappel_nom_base', 'test')", $server_db); |
|
| 1673 | + $GLOBALS['mysql_rappel_nom_base'] = true; |
|
| 1674 | + sql_delete('spip_meta', "nom='mysql_rappel_nom_base'", $server_db); |
|
| 1675 | + $ok = spip_query("INSERT INTO spip_meta (nom,valeur) VALUES ('mysql_rappel_nom_base', 'test')", $server_db); |
|
| 1676 | 1676 | |
| 1677 | - if ($ok) { |
|
| 1678 | - sql_delete('spip_meta', "nom='mysql_rappel_nom_base'", $server_db); |
|
| 1677 | + if ($ok) { |
|
| 1678 | + sql_delete('spip_meta', "nom='mysql_rappel_nom_base'", $server_db); |
|
| 1679 | 1679 | |
| 1680 | - return ''; |
|
| 1681 | - } else { |
|
| 1682 | - $GLOBALS['mysql_rappel_nom_base'] = false; |
|
| 1680 | + return ''; |
|
| 1681 | + } else { |
|
| 1682 | + $GLOBALS['mysql_rappel_nom_base'] = false; |
|
| 1683 | 1683 | |
| 1684 | - return "\$GLOBALS['mysql_rappel_nom_base'] = false; " . |
|
| 1685 | - "/* echec de test_rappel_nom_base_mysql a l'installation. */\n"; |
|
| 1686 | - } |
|
| 1684 | + return "\$GLOBALS['mysql_rappel_nom_base'] = false; " . |
|
| 1685 | + "/* echec de test_rappel_nom_base_mysql a l'installation. */\n"; |
|
| 1686 | + } |
|
| 1687 | 1687 | } |
| 1688 | 1688 | |
| 1689 | 1689 | /** |
@@ -1697,13 +1697,13 @@ discard block |
||
| 1697 | 1697 | * - chaîne : code compilé pour l'indiquer le résultat du test à SPIP |
| 1698 | 1698 | */ |
| 1699 | 1699 | function test_sql_mode_mysql($server_db) { |
| 1700 | - $res = sql_select('version() as v', '', '', '', '', '', '', $server_db); |
|
| 1701 | - $row = sql_fetch($res, $server_db); |
|
| 1702 | - if (version_compare($row['v'], '5.0.0', '>=')) { |
|
| 1703 | - defined('_MYSQL_SET_SQL_MODE') || define('_MYSQL_SET_SQL_MODE', true); |
|
| 1700 | + $res = sql_select('version() as v', '', '', '', '', '', '', $server_db); |
|
| 1701 | + $row = sql_fetch($res, $server_db); |
|
| 1702 | + if (version_compare($row['v'], '5.0.0', '>=')) { |
|
| 1703 | + defined('_MYSQL_SET_SQL_MODE') || define('_MYSQL_SET_SQL_MODE', true); |
|
| 1704 | 1704 | |
| 1705 | - return "defined('_MYSQL_SET_SQL_MODE') || define('_MYSQL_SET_SQL_MODE',true);\n"; |
|
| 1706 | - } |
|
| 1705 | + return "defined('_MYSQL_SET_SQL_MODE') || define('_MYSQL_SET_SQL_MODE',true);\n"; |
|
| 1706 | + } |
|
| 1707 | 1707 | |
| 1708 | - return ''; |
|
| 1708 | + return ''; |
|
| 1709 | 1709 | } |
@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | * @package SPIP\Core\SQL |
| 17 | 17 | **/ |
| 18 | 18 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 19 | - return; |
|
| 19 | + return; |
|
| 20 | 20 | } |
| 21 | 21 | require_once _ROOT_RESTREINT . 'base/objets.php'; |
| 22 | 22 | |
@@ -42,119 +42,119 @@ discard block |
||
| 42 | 42 | **/ |
| 43 | 43 | function spip_connect($serveur = '', $version = '') { |
| 44 | 44 | |
| 45 | - $serveur = !is_string($serveur) ? '' : strtolower($serveur); |
|
| 46 | - $index = $serveur ? $serveur : 0; |
|
| 47 | - if (!$version) { |
|
| 48 | - $version = $GLOBALS['spip_sql_version']; |
|
| 49 | - } |
|
| 50 | - if (isset($GLOBALS['connexions'][$index][$version])) { |
|
| 51 | - return $GLOBALS['connexions'][$index]; |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - include_spip('base/abstract_sql'); |
|
| 55 | - $install = (_request('exec') == 'install'); |
|
| 56 | - |
|
| 57 | - // Premiere connexion ? |
|
| 58 | - if (!($old = isset($GLOBALS['connexions'][$index]))) { |
|
| 59 | - $f = (!preg_match('/^[\w\.]*$/', $serveur)) |
|
| 60 | - ? '' // nom de serveur mal ecrit |
|
| 61 | - : ($serveur ? |
|
| 62 | - (_DIR_CONNECT . $serveur . '.php') // serveur externe |
|
| 63 | - : (_FILE_CONNECT ? _FILE_CONNECT // serveur principal ok |
|
| 64 | - : ($install ? _FILE_CONNECT_TMP // init du serveur principal |
|
| 65 | - : ''))); // installation pas faite |
|
| 66 | - |
|
| 67 | - unset($GLOBALS['db_ok']); |
|
| 68 | - unset($GLOBALS['spip_connect_version']); |
|
| 69 | - if ($f) { |
|
| 70 | - if (is_readable($f)) { |
|
| 71 | - include($f); |
|
| 72 | - } elseif ($serveur and !$install) { |
|
| 73 | - // chercher une declaration de serveur dans le path |
|
| 74 | - // qui pourra un jour servir a declarer des bases sqlite |
|
| 75 | - // par des plugins. Et sert aussi aux boucles POUR. |
|
| 76 | - find_in_path("$serveur.php", 'connect/', true); |
|
| 77 | - } |
|
| 78 | - } |
|
| 79 | - if (!isset($GLOBALS['db_ok'])) { |
|
| 80 | - // fera mieux la prochaine fois |
|
| 81 | - if ($install) { |
|
| 82 | - return false; |
|
| 83 | - } |
|
| 84 | - if ($f and is_readable($f)) { |
|
| 85 | - spip_log("spip_connect: fichier de connexion '$f' OK.", _LOG_INFO_IMPORTANTE); |
|
| 86 | - } else { |
|
| 87 | - spip_log("spip_connect: fichier de connexion '$f' non trouve", _LOG_INFO_IMPORTANTE); |
|
| 88 | - } |
|
| 89 | - spip_log("spip_connect: echec connexion ou serveur $index mal defini dans '$f'.", _LOG_HS); |
|
| 90 | - |
|
| 91 | - // ne plus reessayer si ce n'est pas l'install |
|
| 92 | - return $GLOBALS['connexions'][$index] = false; |
|
| 93 | - } |
|
| 94 | - $GLOBALS['connexions'][$index] = $GLOBALS['db_ok']; |
|
| 95 | - } |
|
| 96 | - // si la connexion a deja ete tentee mais a echoue, le dire! |
|
| 97 | - if (!$GLOBALS['connexions'][$index]) { |
|
| 98 | - return false; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - // la connexion a reussi ou etait deja faite. |
|
| 102 | - // chargement de la version du jeu de fonctions |
|
| 103 | - // si pas dans le fichier par defaut |
|
| 104 | - $type = $GLOBALS['db_ok']['type']; |
|
| 105 | - $jeu = 'spip_' . $type . '_functions_' . $version; |
|
| 106 | - if (!isset($GLOBALS[$jeu])) { |
|
| 107 | - if (!find_in_path($type . '_' . $version . '.php', 'req/', true)) { |
|
| 108 | - spip_log("spip_connect: serveur $index version '$version' non defini pour '$type'", _LOG_HS); |
|
| 109 | - |
|
| 110 | - // ne plus reessayer |
|
| 111 | - return $GLOBALS['connexions'][$index][$version] = []; |
|
| 112 | - } |
|
| 113 | - } |
|
| 114 | - $GLOBALS['connexions'][$index][$version] = $GLOBALS[$jeu]; |
|
| 115 | - if ($old) { |
|
| 116 | - return $GLOBALS['connexions'][$index]; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - $GLOBALS['connexions'][$index]['spip_connect_version'] = isset($GLOBALS['spip_connect_version']) ? $GLOBALS['spip_connect_version'] : 0; |
|
| 120 | - |
|
| 121 | - // initialisation de l'alphabet utilise dans les connexions SQL |
|
| 122 | - // si l'installation l'a determine. |
|
| 123 | - // Celui du serveur principal l'impose aux serveurs secondaires |
|
| 124 | - // s'ils le connaissent |
|
| 125 | - |
|
| 126 | - if (!$serveur) { |
|
| 127 | - $charset = spip_connect_main($GLOBALS[$jeu], $GLOBALS['db_ok']['charset']); |
|
| 128 | - if (!$charset) { |
|
| 129 | - unset($GLOBALS['connexions'][$index]); |
|
| 130 | - spip_log('spip_connect: absence de charset', _LOG_AVERTISSEMENT); |
|
| 131 | - |
|
| 132 | - return false; |
|
| 133 | - } |
|
| 134 | - } else { |
|
| 135 | - if ($GLOBALS['db_ok']['charset']) { |
|
| 136 | - $charset = $GLOBALS['db_ok']['charset']; |
|
| 137 | - } |
|
| 138 | - // spip_meta n'existe pas toujours dans la base |
|
| 139 | - // C'est le cas d'un dump sqlite par exemple |
|
| 140 | - elseif ( |
|
| 141 | - $GLOBALS['connexions'][$index]['spip_connect_version'] |
|
| 142 | - and sql_showtable('spip_meta', true, $serveur) |
|
| 143 | - and $r = sql_getfetsel('valeur', 'spip_meta', "nom='charset_sql_connexion'", '', '', '', '', $serveur) |
|
| 144 | - ) { |
|
| 145 | - $charset = $r; |
|
| 146 | - } else { |
|
| 147 | - $charset = -1; |
|
| 148 | - } |
|
| 149 | - } |
|
| 150 | - if ($charset != -1) { |
|
| 151 | - $f = $GLOBALS[$jeu]['set_charset']; |
|
| 152 | - if (function_exists($f)) { |
|
| 153 | - $f($charset, $serveur); |
|
| 154 | - } |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - return $GLOBALS['connexions'][$index]; |
|
| 45 | + $serveur = !is_string($serveur) ? '' : strtolower($serveur); |
|
| 46 | + $index = $serveur ? $serveur : 0; |
|
| 47 | + if (!$version) { |
|
| 48 | + $version = $GLOBALS['spip_sql_version']; |
|
| 49 | + } |
|
| 50 | + if (isset($GLOBALS['connexions'][$index][$version])) { |
|
| 51 | + return $GLOBALS['connexions'][$index]; |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + include_spip('base/abstract_sql'); |
|
| 55 | + $install = (_request('exec') == 'install'); |
|
| 56 | + |
|
| 57 | + // Premiere connexion ? |
|
| 58 | + if (!($old = isset($GLOBALS['connexions'][$index]))) { |
|
| 59 | + $f = (!preg_match('/^[\w\.]*$/', $serveur)) |
|
| 60 | + ? '' // nom de serveur mal ecrit |
|
| 61 | + : ($serveur ? |
|
| 62 | + (_DIR_CONNECT . $serveur . '.php') // serveur externe |
|
| 63 | + : (_FILE_CONNECT ? _FILE_CONNECT // serveur principal ok |
|
| 64 | + : ($install ? _FILE_CONNECT_TMP // init du serveur principal |
|
| 65 | + : ''))); // installation pas faite |
|
| 66 | + |
|
| 67 | + unset($GLOBALS['db_ok']); |
|
| 68 | + unset($GLOBALS['spip_connect_version']); |
|
| 69 | + if ($f) { |
|
| 70 | + if (is_readable($f)) { |
|
| 71 | + include($f); |
|
| 72 | + } elseif ($serveur and !$install) { |
|
| 73 | + // chercher une declaration de serveur dans le path |
|
| 74 | + // qui pourra un jour servir a declarer des bases sqlite |
|
| 75 | + // par des plugins. Et sert aussi aux boucles POUR. |
|
| 76 | + find_in_path("$serveur.php", 'connect/', true); |
|
| 77 | + } |
|
| 78 | + } |
|
| 79 | + if (!isset($GLOBALS['db_ok'])) { |
|
| 80 | + // fera mieux la prochaine fois |
|
| 81 | + if ($install) { |
|
| 82 | + return false; |
|
| 83 | + } |
|
| 84 | + if ($f and is_readable($f)) { |
|
| 85 | + spip_log("spip_connect: fichier de connexion '$f' OK.", _LOG_INFO_IMPORTANTE); |
|
| 86 | + } else { |
|
| 87 | + spip_log("spip_connect: fichier de connexion '$f' non trouve", _LOG_INFO_IMPORTANTE); |
|
| 88 | + } |
|
| 89 | + spip_log("spip_connect: echec connexion ou serveur $index mal defini dans '$f'.", _LOG_HS); |
|
| 90 | + |
|
| 91 | + // ne plus reessayer si ce n'est pas l'install |
|
| 92 | + return $GLOBALS['connexions'][$index] = false; |
|
| 93 | + } |
|
| 94 | + $GLOBALS['connexions'][$index] = $GLOBALS['db_ok']; |
|
| 95 | + } |
|
| 96 | + // si la connexion a deja ete tentee mais a echoue, le dire! |
|
| 97 | + if (!$GLOBALS['connexions'][$index]) { |
|
| 98 | + return false; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + // la connexion a reussi ou etait deja faite. |
|
| 102 | + // chargement de la version du jeu de fonctions |
|
| 103 | + // si pas dans le fichier par defaut |
|
| 104 | + $type = $GLOBALS['db_ok']['type']; |
|
| 105 | + $jeu = 'spip_' . $type . '_functions_' . $version; |
|
| 106 | + if (!isset($GLOBALS[$jeu])) { |
|
| 107 | + if (!find_in_path($type . '_' . $version . '.php', 'req/', true)) { |
|
| 108 | + spip_log("spip_connect: serveur $index version '$version' non defini pour '$type'", _LOG_HS); |
|
| 109 | + |
|
| 110 | + // ne plus reessayer |
|
| 111 | + return $GLOBALS['connexions'][$index][$version] = []; |
|
| 112 | + } |
|
| 113 | + } |
|
| 114 | + $GLOBALS['connexions'][$index][$version] = $GLOBALS[$jeu]; |
|
| 115 | + if ($old) { |
|
| 116 | + return $GLOBALS['connexions'][$index]; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + $GLOBALS['connexions'][$index]['spip_connect_version'] = isset($GLOBALS['spip_connect_version']) ? $GLOBALS['spip_connect_version'] : 0; |
|
| 120 | + |
|
| 121 | + // initialisation de l'alphabet utilise dans les connexions SQL |
|
| 122 | + // si l'installation l'a determine. |
|
| 123 | + // Celui du serveur principal l'impose aux serveurs secondaires |
|
| 124 | + // s'ils le connaissent |
|
| 125 | + |
|
| 126 | + if (!$serveur) { |
|
| 127 | + $charset = spip_connect_main($GLOBALS[$jeu], $GLOBALS['db_ok']['charset']); |
|
| 128 | + if (!$charset) { |
|
| 129 | + unset($GLOBALS['connexions'][$index]); |
|
| 130 | + spip_log('spip_connect: absence de charset', _LOG_AVERTISSEMENT); |
|
| 131 | + |
|
| 132 | + return false; |
|
| 133 | + } |
|
| 134 | + } else { |
|
| 135 | + if ($GLOBALS['db_ok']['charset']) { |
|
| 136 | + $charset = $GLOBALS['db_ok']['charset']; |
|
| 137 | + } |
|
| 138 | + // spip_meta n'existe pas toujours dans la base |
|
| 139 | + // C'est le cas d'un dump sqlite par exemple |
|
| 140 | + elseif ( |
|
| 141 | + $GLOBALS['connexions'][$index]['spip_connect_version'] |
|
| 142 | + and sql_showtable('spip_meta', true, $serveur) |
|
| 143 | + and $r = sql_getfetsel('valeur', 'spip_meta', "nom='charset_sql_connexion'", '', '', '', '', $serveur) |
|
| 144 | + ) { |
|
| 145 | + $charset = $r; |
|
| 146 | + } else { |
|
| 147 | + $charset = -1; |
|
| 148 | + } |
|
| 149 | + } |
|
| 150 | + if ($charset != -1) { |
|
| 151 | + $f = $GLOBALS[$jeu]['set_charset']; |
|
| 152 | + if (function_exists($f)) { |
|
| 153 | + $f($charset, $serveur); |
|
| 154 | + } |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + return $GLOBALS['connexions'][$index]; |
|
| 158 | 158 | } |
| 159 | 159 | |
| 160 | 160 | /** |
@@ -163,12 +163,12 @@ discard block |
||
| 163 | 163 | * @param string $serveur Nom du connecteur de bdd utilisé |
| 164 | 164 | **/ |
| 165 | 165 | function spip_sql_erreur($serveur = '') { |
| 166 | - $connexion = spip_connect($serveur); |
|
| 167 | - $e = sql_errno($serveur); |
|
| 168 | - $t = (isset($connexion['type']) ? $connexion['type'] : 'sql'); |
|
| 169 | - $m = "Erreur $e de $t: " . sql_error($serveur) . "\nin " . sql_error_backtrace() . "\n" . trim($connexion['last']); |
|
| 170 | - $f = $t . $serveur; |
|
| 171 | - spip_log($m, $f . '.' . _LOG_ERREUR); |
|
| 166 | + $connexion = spip_connect($serveur); |
|
| 167 | + $e = sql_errno($serveur); |
|
| 168 | + $t = (isset($connexion['type']) ? $connexion['type'] : 'sql'); |
|
| 169 | + $m = "Erreur $e de $t: " . sql_error($serveur) . "\nin " . sql_error_backtrace() . "\n" . trim($connexion['last']); |
|
| 170 | + $f = $t . $serveur; |
|
| 171 | + spip_log($m, $f . '.' . _LOG_ERREUR); |
|
| 172 | 172 | } |
| 173 | 173 | |
| 174 | 174 | /** |
@@ -190,19 +190,19 @@ discard block |
||
| 190 | 190 | * - array : description de la connexion, si l'instruction sql est indisponible pour cette connexion |
| 191 | 191 | **/ |
| 192 | 192 | function spip_connect_sql($version, $ins = '', $serveur = '', $continue = false) { |
| 193 | - $desc = spip_connect($serveur, $version); |
|
| 194 | - if (function_exists($f = @$desc[$version][$ins])) { |
|
| 195 | - return $f; |
|
| 196 | - } |
|
| 197 | - if ($continue) { |
|
| 198 | - return $desc; |
|
| 199 | - } |
|
| 200 | - if ($ins) { |
|
| 201 | - spip_log("Le serveur '$serveur' version $version n'a pas '$ins'", _LOG_ERREUR); |
|
| 202 | - } |
|
| 203 | - include_spip('inc/minipres'); |
|
| 204 | - echo minipres(_T('info_travaux_titre'), _T('titre_probleme_technique'), ['status' => 503]); |
|
| 205 | - exit; |
|
| 193 | + $desc = spip_connect($serveur, $version); |
|
| 194 | + if (function_exists($f = @$desc[$version][$ins])) { |
|
| 195 | + return $f; |
|
| 196 | + } |
|
| 197 | + if ($continue) { |
|
| 198 | + return $desc; |
|
| 199 | + } |
|
| 200 | + if ($ins) { |
|
| 201 | + spip_log("Le serveur '$serveur' version $version n'a pas '$ins'", _LOG_ERREUR); |
|
| 202 | + } |
|
| 203 | + include_spip('inc/minipres'); |
|
| 204 | + echo minipres(_T('info_travaux_titre'), _T('titre_probleme_technique'), ['status' => 503]); |
|
| 205 | + exit; |
|
| 206 | 206 | } |
| 207 | 207 | |
| 208 | 208 | /** |
@@ -228,70 +228,70 @@ discard block |
||
| 228 | 228 | * @return array Description de la connexion |
| 229 | 229 | */ |
| 230 | 230 | function spip_connect_db( |
| 231 | - $host, |
|
| 232 | - $port, |
|
| 233 | - $login, |
|
| 234 | - $pass, |
|
| 235 | - $db = '', |
|
| 236 | - $type = 'mysql', |
|
| 237 | - $prefixe = '', |
|
| 238 | - $auth = '', |
|
| 239 | - $charset = '' |
|
| 231 | + $host, |
|
| 232 | + $port, |
|
| 233 | + $login, |
|
| 234 | + $pass, |
|
| 235 | + $db = '', |
|
| 236 | + $type = 'mysql', |
|
| 237 | + $prefixe = '', |
|
| 238 | + $auth = '', |
|
| 239 | + $charset = '' |
|
| 240 | 240 | ) { |
| 241 | - // temps avant nouvelle tentative de connexion |
|
| 242 | - // suite a une connection echouee |
|
| 243 | - if (!defined('_CONNECT_RETRY_DELAY')) { |
|
| 244 | - define('_CONNECT_RETRY_DELAY', 30); |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - $f = ''; |
|
| 248 | - // un fichier de identifiant par combinaison (type,host,port,db) |
|
| 249 | - // pour ne pas declarer tout indisponible d'un coup |
|
| 250 | - // si en cours d'installation ou si db=@test@ on ne pose rien |
|
| 251 | - // car c'est un test de connexion |
|
| 252 | - if (!defined('_ECRIRE_INSTALL') and $db !== '@test@') { |
|
| 253 | - $f = _DIR_TMP . $type . '.' . substr(md5($host . $port . $db), 0, 8) . '.out'; |
|
| 254 | - } elseif ($db == '@test@') { |
|
| 255 | - $db = ''; |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - if ( |
|
| 259 | - $f |
|
| 260 | - and @file_exists($f) |
|
| 261 | - and (time() - @filemtime($f) < _CONNECT_RETRY_DELAY) |
|
| 262 | - ) { |
|
| 263 | - spip_log("Echec : $f recent. Pas de tentative de connexion", _LOG_HS); |
|
| 264 | - |
|
| 265 | - return; |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - if (!$prefixe) { |
|
| 269 | - $prefixe = isset($GLOBALS['table_prefix']) |
|
| 270 | - ? $GLOBALS['table_prefix'] : $db; |
|
| 271 | - } |
|
| 272 | - $h = charger_fonction($type, 'req', true); |
|
| 273 | - if (!$h) { |
|
| 274 | - spip_log("les requetes $type ne sont pas fournies", _LOG_HS); |
|
| 275 | - |
|
| 276 | - return; |
|
| 277 | - } |
|
| 278 | - if ($g = $h($host, $port, $login, $pass, $db, $prefixe)) { |
|
| 279 | - if (!is_array($auth)) { |
|
| 280 | - // compatibilite version 0.7 initiale |
|
| 281 | - $g['ldap'] = $auth; |
|
| 282 | - $auth = ['ldap' => $auth]; |
|
| 283 | - } |
|
| 284 | - $g['authentification'] = $auth; |
|
| 285 | - $g['type'] = $type; |
|
| 286 | - $g['charset'] = $charset; |
|
| 287 | - |
|
| 288 | - return $GLOBALS['db_ok'] = $g; |
|
| 289 | - } |
|
| 290 | - // En cas d'indisponibilite du serveur, eviter de le bombarder |
|
| 291 | - if ($f) { |
|
| 292 | - @touch($f); |
|
| 293 | - spip_log("Echec connexion serveur $type : host[$host] port[$port] login[$login] base[$db]", $type . '.' . _LOG_HS); |
|
| 294 | - } |
|
| 241 | + // temps avant nouvelle tentative de connexion |
|
| 242 | + // suite a une connection echouee |
|
| 243 | + if (!defined('_CONNECT_RETRY_DELAY')) { |
|
| 244 | + define('_CONNECT_RETRY_DELAY', 30); |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + $f = ''; |
|
| 248 | + // un fichier de identifiant par combinaison (type,host,port,db) |
|
| 249 | + // pour ne pas declarer tout indisponible d'un coup |
|
| 250 | + // si en cours d'installation ou si db=@test@ on ne pose rien |
|
| 251 | + // car c'est un test de connexion |
|
| 252 | + if (!defined('_ECRIRE_INSTALL') and $db !== '@test@') { |
|
| 253 | + $f = _DIR_TMP . $type . '.' . substr(md5($host . $port . $db), 0, 8) . '.out'; |
|
| 254 | + } elseif ($db == '@test@') { |
|
| 255 | + $db = ''; |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + if ( |
|
| 259 | + $f |
|
| 260 | + and @file_exists($f) |
|
| 261 | + and (time() - @filemtime($f) < _CONNECT_RETRY_DELAY) |
|
| 262 | + ) { |
|
| 263 | + spip_log("Echec : $f recent. Pas de tentative de connexion", _LOG_HS); |
|
| 264 | + |
|
| 265 | + return; |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + if (!$prefixe) { |
|
| 269 | + $prefixe = isset($GLOBALS['table_prefix']) |
|
| 270 | + ? $GLOBALS['table_prefix'] : $db; |
|
| 271 | + } |
|
| 272 | + $h = charger_fonction($type, 'req', true); |
|
| 273 | + if (!$h) { |
|
| 274 | + spip_log("les requetes $type ne sont pas fournies", _LOG_HS); |
|
| 275 | + |
|
| 276 | + return; |
|
| 277 | + } |
|
| 278 | + if ($g = $h($host, $port, $login, $pass, $db, $prefixe)) { |
|
| 279 | + if (!is_array($auth)) { |
|
| 280 | + // compatibilite version 0.7 initiale |
|
| 281 | + $g['ldap'] = $auth; |
|
| 282 | + $auth = ['ldap' => $auth]; |
|
| 283 | + } |
|
| 284 | + $g['authentification'] = $auth; |
|
| 285 | + $g['type'] = $type; |
|
| 286 | + $g['charset'] = $charset; |
|
| 287 | + |
|
| 288 | + return $GLOBALS['db_ok'] = $g; |
|
| 289 | + } |
|
| 290 | + // En cas d'indisponibilite du serveur, eviter de le bombarder |
|
| 291 | + if ($f) { |
|
| 292 | + @touch($f); |
|
| 293 | + spip_log("Echec connexion serveur $type : host[$host] port[$port] login[$login] base[$db]", $type . '.' . _LOG_HS); |
|
| 294 | + } |
|
| 295 | 295 | } |
| 296 | 296 | |
| 297 | 297 | |
@@ -323,32 +323,32 @@ discard block |
||
| 323 | 323 | * - nom du charset sinon |
| 324 | 324 | **/ |
| 325 | 325 | function spip_connect_main($connexion, $charset_sql_connexion = '') { |
| 326 | - if ($GLOBALS['spip_connect_version'] < 0.1 and _DIR_RESTREINT) { |
|
| 327 | - include_spip('inc/headers'); |
|
| 328 | - redirige_url_ecrire('upgrade', 'reinstall=oui'); |
|
| 329 | - } |
|
| 330 | - |
|
| 331 | - if (!($f = $connexion['select'])) { |
|
| 332 | - return false; |
|
| 333 | - } |
|
| 334 | - // si le charset est fourni, l'utiliser |
|
| 335 | - if ($charset_sql_connexion) { |
|
| 336 | - return $charset_sql_connexion; |
|
| 337 | - } |
|
| 338 | - // sinon on regarde la table spip_meta |
|
| 339 | - // en cas d'erreur select retourne la requette (is_string=true donc) |
|
| 340 | - if ( |
|
| 341 | - !$r = $f('valeur', 'spip_meta', "nom='charset_sql_connexion'") |
|
| 342 | - or is_string($r) |
|
| 343 | - ) { |
|
| 344 | - return false; |
|
| 345 | - } |
|
| 346 | - if (!($f = $connexion['fetch'])) { |
|
| 347 | - return false; |
|
| 348 | - } |
|
| 349 | - $r = $f($r); |
|
| 350 | - |
|
| 351 | - return (isset($r['valeur']) && $r['valeur']) ? $r['valeur'] : -1; |
|
| 326 | + if ($GLOBALS['spip_connect_version'] < 0.1 and _DIR_RESTREINT) { |
|
| 327 | + include_spip('inc/headers'); |
|
| 328 | + redirige_url_ecrire('upgrade', 'reinstall=oui'); |
|
| 329 | + } |
|
| 330 | + |
|
| 331 | + if (!($f = $connexion['select'])) { |
|
| 332 | + return false; |
|
| 333 | + } |
|
| 334 | + // si le charset est fourni, l'utiliser |
|
| 335 | + if ($charset_sql_connexion) { |
|
| 336 | + return $charset_sql_connexion; |
|
| 337 | + } |
|
| 338 | + // sinon on regarde la table spip_meta |
|
| 339 | + // en cas d'erreur select retourne la requette (is_string=true donc) |
|
| 340 | + if ( |
|
| 341 | + !$r = $f('valeur', 'spip_meta', "nom='charset_sql_connexion'") |
|
| 342 | + or is_string($r) |
|
| 343 | + ) { |
|
| 344 | + return false; |
|
| 345 | + } |
|
| 346 | + if (!($f = $connexion['fetch'])) { |
|
| 347 | + return false; |
|
| 348 | + } |
|
| 349 | + $r = $f($r); |
|
| 350 | + |
|
| 351 | + return (isset($r['valeur']) && $r['valeur']) ? $r['valeur'] : -1; |
|
| 352 | 352 | } |
| 353 | 353 | |
| 354 | 354 | /** |
@@ -364,9 +364,9 @@ discard block |
||
| 364 | 364 | * @return array |
| 365 | 365 | */ |
| 366 | 366 | function spip_connect_ldap($serveur = '') { |
| 367 | - include_spip('auth/ldap'); |
|
| 367 | + include_spip('auth/ldap'); |
|
| 368 | 368 | |
| 369 | - return auth_ldap_connect($serveur); |
|
| 369 | + return auth_ldap_connect($serveur); |
|
| 370 | 370 | } |
| 371 | 371 | |
| 372 | 372 | /** |
@@ -382,9 +382,9 @@ discard block |
||
| 382 | 382 | * @return string Valeur échappée. |
| 383 | 383 | **/ |
| 384 | 384 | function _q($a) { |
| 385 | - return (is_numeric($a)) ? strval($a) : |
|
| 386 | - (!is_array($a) ? ("'" . addslashes($a) . "'") |
|
| 387 | - : join(',', array_map('_q', $a))); |
|
| 385 | + return (is_numeric($a)) ? strval($a) : |
|
| 386 | + (!is_array($a) ? ("'" . addslashes($a) . "'") |
|
| 387 | + : join(',', array_map('_q', $a))); |
|
| 388 | 388 | } |
| 389 | 389 | |
| 390 | 390 | /** |
@@ -400,66 +400,66 @@ discard block |
||
| 400 | 400 | * @return array |
| 401 | 401 | */ |
| 402 | 402 | function query_echappe_textes($query, $uniqid=null) { |
| 403 | - static $codeEchappements = null; |
|
| 404 | - if (is_null($codeEchappements) or $uniqid) { |
|
| 405 | - if (is_null($uniqid)) { |
|
| 406 | - $uniqid = uniqid(); |
|
| 407 | - } |
|
| 408 | - $uniqid = substr(md5($uniqid), 0, 4); |
|
| 409 | - $codeEchappements = ["\\\\" => "\x1@#{$uniqid}#@\x1", "\\'" => "\x2@#{$uniqid}#@\x2", '\\"' => "\x3@#{$uniqid}#@\x3"]; |
|
| 410 | - } |
|
| 411 | - if ($query === null) { |
|
| 412 | - return $codeEchappements; |
|
| 413 | - } |
|
| 414 | - |
|
| 415 | - // si la query contient deja des codes d'echappement on va s'emmeler les pinceaux et donc on ne touche a rien |
|
| 416 | - // ce n'est pas un cas legitime |
|
| 417 | - foreach ($codeEchappements as $codeEchappement) { |
|
| 418 | - if (strpos($query, $codeEchappement) !== false) { |
|
| 419 | - return [$query, []]; |
|
| 420 | - } |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - $query_echappees = str_replace(array_keys($codeEchappements), array_values($codeEchappements), $query); |
|
| 424 | - if (preg_match_all("/('[^']*')|(\"[^\"]*\")/S", $query_echappees, $textes)) { |
|
| 425 | - $textes = reset($textes); // indice 0 du match |
|
| 426 | - switch (count($textes)) { |
|
| 427 | - case 0: |
|
| 428 | - $replace = []; |
|
| 429 | - break; |
|
| 430 | - case 1: |
|
| 431 | - $replace = ['%1$s']; |
|
| 432 | - break; |
|
| 433 | - case 2: |
|
| 434 | - $replace = ['%1$s', '%2$s']; |
|
| 435 | - break; |
|
| 436 | - case 3: |
|
| 437 | - $replace = ['%1$s', '%2$s', '%3$s']; |
|
| 438 | - break; |
|
| 439 | - case 4: |
|
| 440 | - $replace = ['%1$s', '%2$s', '%3$s', '%4$s']; |
|
| 441 | - break; |
|
| 442 | - case 5: |
|
| 443 | - $replace = ['%1$s', '%2$s', '%3$s', '%4$s', '%5$s']; |
|
| 444 | - break; |
|
| 445 | - default: |
|
| 446 | - $replace = range(1, count($textes)); |
|
| 447 | - $replace = '%' . implode('$s,%', $replace) . '$s'; |
|
| 448 | - $replace = explode(',', $replace); |
|
| 449 | - break; |
|
| 450 | - } |
|
| 451 | - $query_echappees = str_replace($textes, $replace, $query_echappees); |
|
| 452 | - } else { |
|
| 453 | - $textes = []; |
|
| 454 | - } |
|
| 455 | - |
|
| 456 | - // si il reste des quotes simples ou doubles, c'est qu'on s'est emmelles les pinceaux |
|
| 457 | - // dans le doute on ne touche a rien |
|
| 458 | - if (strpbrk($query_echappees, "'\"") !== false) { |
|
| 459 | - return [$query, []]; |
|
| 460 | - } |
|
| 461 | - |
|
| 462 | - return [$query_echappees, $textes]; |
|
| 403 | + static $codeEchappements = null; |
|
| 404 | + if (is_null($codeEchappements) or $uniqid) { |
|
| 405 | + if (is_null($uniqid)) { |
|
| 406 | + $uniqid = uniqid(); |
|
| 407 | + } |
|
| 408 | + $uniqid = substr(md5($uniqid), 0, 4); |
|
| 409 | + $codeEchappements = ["\\\\" => "\x1@#{$uniqid}#@\x1", "\\'" => "\x2@#{$uniqid}#@\x2", '\\"' => "\x3@#{$uniqid}#@\x3"]; |
|
| 410 | + } |
|
| 411 | + if ($query === null) { |
|
| 412 | + return $codeEchappements; |
|
| 413 | + } |
|
| 414 | + |
|
| 415 | + // si la query contient deja des codes d'echappement on va s'emmeler les pinceaux et donc on ne touche a rien |
|
| 416 | + // ce n'est pas un cas legitime |
|
| 417 | + foreach ($codeEchappements as $codeEchappement) { |
|
| 418 | + if (strpos($query, $codeEchappement) !== false) { |
|
| 419 | + return [$query, []]; |
|
| 420 | + } |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + $query_echappees = str_replace(array_keys($codeEchappements), array_values($codeEchappements), $query); |
|
| 424 | + if (preg_match_all("/('[^']*')|(\"[^\"]*\")/S", $query_echappees, $textes)) { |
|
| 425 | + $textes = reset($textes); // indice 0 du match |
|
| 426 | + switch (count($textes)) { |
|
| 427 | + case 0: |
|
| 428 | + $replace = []; |
|
| 429 | + break; |
|
| 430 | + case 1: |
|
| 431 | + $replace = ['%1$s']; |
|
| 432 | + break; |
|
| 433 | + case 2: |
|
| 434 | + $replace = ['%1$s', '%2$s']; |
|
| 435 | + break; |
|
| 436 | + case 3: |
|
| 437 | + $replace = ['%1$s', '%2$s', '%3$s']; |
|
| 438 | + break; |
|
| 439 | + case 4: |
|
| 440 | + $replace = ['%1$s', '%2$s', '%3$s', '%4$s']; |
|
| 441 | + break; |
|
| 442 | + case 5: |
|
| 443 | + $replace = ['%1$s', '%2$s', '%3$s', '%4$s', '%5$s']; |
|
| 444 | + break; |
|
| 445 | + default: |
|
| 446 | + $replace = range(1, count($textes)); |
|
| 447 | + $replace = '%' . implode('$s,%', $replace) . '$s'; |
|
| 448 | + $replace = explode(',', $replace); |
|
| 449 | + break; |
|
| 450 | + } |
|
| 451 | + $query_echappees = str_replace($textes, $replace, $query_echappees); |
|
| 452 | + } else { |
|
| 453 | + $textes = []; |
|
| 454 | + } |
|
| 455 | + |
|
| 456 | + // si il reste des quotes simples ou doubles, c'est qu'on s'est emmelles les pinceaux |
|
| 457 | + // dans le doute on ne touche a rien |
|
| 458 | + if (strpbrk($query_echappees, "'\"") !== false) { |
|
| 459 | + return [$query, []]; |
|
| 460 | + } |
|
| 461 | + |
|
| 462 | + return [$query_echappees, $textes]; |
|
| 463 | 463 | } |
| 464 | 464 | |
| 465 | 465 | /** |
@@ -473,14 +473,14 @@ discard block |
||
| 473 | 473 | * @return string |
| 474 | 474 | */ |
| 475 | 475 | function query_reinjecte_textes($query, $textes) { |
| 476 | - // recuperer les codes echappements |
|
| 477 | - $codeEchappements = query_echappe_textes(null); |
|
| 476 | + // recuperer les codes echappements |
|
| 477 | + $codeEchappements = query_echappe_textes(null); |
|
| 478 | 478 | |
| 479 | - $query = sprintf($query, ...$textes); |
|
| 479 | + $query = sprintf($query, ...$textes); |
|
| 480 | 480 | |
| 481 | - $query = str_replace(array_values($codeEchappements), array_keys($codeEchappements), $query); |
|
| 481 | + $query = str_replace(array_values($codeEchappements), array_keys($codeEchappements), $query); |
|
| 482 | 482 | |
| 483 | - return $query; |
|
| 483 | + return $query; |
|
| 484 | 484 | } |
| 485 | 485 | |
| 486 | 486 | |
@@ -499,7 +499,7 @@ discard block |
||
| 499 | 499 | **/ |
| 500 | 500 | function spip_query($query, $serveur = '') { |
| 501 | 501 | |
| 502 | - $f = spip_connect_sql($GLOBALS['spip_sql_version'], 'query', $serveur, true); |
|
| 502 | + $f = spip_connect_sql($GLOBALS['spip_sql_version'], 'query', $serveur, true); |
|
| 503 | 503 | |
| 504 | - return function_exists($f) ? $f($query, $serveur) : false; |
|
| 504 | + return function_exists($f) ? $f($query, $serveur) : false; |
|
| 505 | 505 | } |