@@ -15,7 +15,7 @@ discard block |
||
| 15 | 15 | * Test utilisateur |
| 16 | 16 | */ |
| 17 | 17 | if (isset($_GET['test_ecran_securite'])) |
| 18 | - $ecran_securite_raison = 'test '._ECRAN_SECURITE; |
|
| 18 | + $ecran_securite_raison = 'test '._ECRAN_SECURITE; |
|
| 19 | 19 | |
| 20 | 20 | /* |
| 21 | 21 | * Monitoring |
@@ -24,215 +24,215 @@ discard block |
||
| 24 | 24 | * le load depasse ECRAN_SECURITE_LOAD) |
| 25 | 25 | */ |
| 26 | 26 | if (!defined('_IS_BOT') and isset($_GET['var_isbot'])){ |
| 27 | - define('_IS_BOT', $_GET['var_isbot'] ? true : false); |
|
| 27 | + define('_IS_BOT', $_GET['var_isbot'] ? true : false); |
|
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 30 | /* |
| 31 | 31 | * Détecteur de robot d'indexation |
| 32 | 32 | */ |
| 33 | 33 | if (!defined('_IS_BOT')){ |
| 34 | - define('_IS_BOT', |
|
| 35 | - isset($_SERVER['HTTP_USER_AGENT']) |
|
| 36 | - and preg_match(',' |
|
| 37 | - . implode ('|', array( |
|
| 38 | - // mots generiques |
|
| 39 | - 'bot', |
|
| 40 | - 'slurp', |
|
| 41 | - 'crawler', |
|
| 42 | - 'crwlr', |
|
| 43 | - 'java', |
|
| 44 | - 'monitoring', |
|
| 45 | - 'spider', |
|
| 46 | - 'webvac', |
|
| 47 | - 'yandex', |
|
| 48 | - 'MSIE 6\.0', // botnet 99,9% du temps |
|
| 49 | - // UA plus cibles |
|
| 50 | - '200please', |
|
| 51 | - '80legs', |
|
| 52 | - 'a6-indexer', |
|
| 53 | - 'aboundex', |
|
| 54 | - 'accoona', |
|
| 55 | - 'acrylicapps', |
|
| 56 | - 'addthis', |
|
| 57 | - 'adressendeutschland', |
|
| 58 | - 'alexa', |
|
| 59 | - 'altavista', |
|
| 60 | - 'analyticsseo', |
|
| 61 | - 'antennapod', |
|
| 62 | - 'arachnys', |
|
| 63 | - 'archive', |
|
| 64 | - 'argclrint', |
|
| 65 | - 'aspseek', |
|
| 66 | - 'baidu', |
|
| 67 | - 'begunadvertising', |
|
| 68 | - 'bing', |
|
| 69 | - 'bloglines', |
|
| 70 | - 'buck', |
|
| 71 | - 'browsershots', |
|
| 72 | - 'bubing', |
|
| 73 | - 'butterfly', |
|
| 74 | - 'changedetection', |
|
| 75 | - 'charlotte', |
|
| 76 | - 'chilkat', |
|
| 77 | - 'china', |
|
| 78 | - 'coccoc', |
|
| 79 | - 'crowsnest', |
|
| 80 | - 'dataminr', |
|
| 81 | - 'daumoa', |
|
| 82 | - 'dlvr\.it', |
|
| 83 | - 'dlweb', |
|
| 84 | - 'drupal', |
|
| 85 | - 'ec2linkfinder', |
|
| 86 | - 'eset\.com', |
|
| 87 | - 'estyle', |
|
| 88 | - 'exalead', |
|
| 89 | - 'ezooms', |
|
| 90 | - 'facebookexternalhit', |
|
| 91 | - 'facebookplatform', |
|
| 92 | - 'fairshare', |
|
| 93 | - 'feedfetcher', |
|
| 94 | - 'feedfetcher-google', |
|
| 95 | - 'feedly', |
|
| 96 | - 'fetch', |
|
| 97 | - 'flipboardproxy', |
|
| 98 | - 'genieo', |
|
| 99 | - 'google', |
|
| 100 | - 'go-http-client', |
|
| 101 | - 'grapeshot', |
|
| 102 | - 'hatena-useragent', |
|
| 103 | - 'head', |
|
| 104 | - 'hosttracker', |
|
| 105 | - 'hubspot', |
|
| 106 | - 'ia_archiver', |
|
| 107 | - 'ichiro', |
|
| 108 | - 'iltrovatore-setaccio', |
|
| 109 | - 'immediatenet', |
|
| 110 | - 'ina', |
|
| 111 | - 'inoreader', |
|
| 112 | - 'infegyatlas', |
|
| 113 | - 'infohelfer', |
|
| 114 | - 'instapaper', |
|
| 115 | - 'jabse', |
|
| 116 | - 'james', |
|
| 117 | - 'jersey', |
|
| 118 | - 'kumkie', |
|
| 119 | - 'linkdex', |
|
| 120 | - 'linkfluence', |
|
| 121 | - 'linkwalker', |
|
| 122 | - 'litefinder', |
|
| 123 | - 'loadimpactpageanalyzer', |
|
| 124 | - 'ltx71', |
|
| 125 | - 'luminate', |
|
| 126 | - 'lycos', |
|
| 127 | - 'lycosa', |
|
| 128 | - 'mediapartners-google', |
|
| 129 | - 'msai', |
|
| 130 | - 'myapp', |
|
| 131 | - 'nativehost', |
|
| 132 | - 'najdi', |
|
| 133 | - 'netcraftsurveyagent', |
|
| 134 | - 'netestate', |
|
| 135 | - 'netseer', |
|
| 136 | - 'netnewswire', |
|
| 137 | - 'newspaper', |
|
| 138 | - 'newsblur', |
|
| 139 | - 'nuhk', |
|
| 140 | - 'nuzzel', |
|
| 141 | - 'okhttp', |
|
| 142 | - 'otmedia', |
|
| 143 | - 'owlin', |
|
| 144 | - 'owncloud', |
|
| 145 | - 'panscient', |
|
| 146 | - 'paper\.li', |
|
| 147 | - 'parsijoo', |
|
| 148 | - 'protopage', |
|
| 149 | - 'plukkie', |
|
| 150 | - 'proximic', |
|
| 151 | - 'pubsub', |
|
| 152 | - 'python', |
|
| 153 | - 'qirina', |
|
| 154 | - 'qoshe', |
|
| 155 | - 'qualidator', |
|
| 156 | - 'qwantify', |
|
| 157 | - 'rambler', |
|
| 158 | - 'readability', |
|
| 159 | - 'ruby', |
|
| 160 | - 'sbsearch', |
|
| 161 | - 'scoop\.it', |
|
| 162 | - 'scooter', |
|
| 163 | - 'scoutjet', |
|
| 164 | - 'scrapy', |
|
| 165 | - 'scrubby', |
|
| 166 | - 'scrubbybloglines', |
|
| 167 | - 'shareaholic', |
|
| 168 | - 'shopwiki', |
|
| 169 | - 'simplepie', |
|
| 170 | - 'sistrix', |
|
| 171 | - 'sitechecker', |
|
| 172 | - 'siteexplorer', |
|
| 173 | - 'snapshot', |
|
| 174 | - 'sogou', |
|
| 175 | - 'special_archiver', |
|
| 176 | - 'speedy', |
|
| 177 | - 'spinn3r', |
|
| 178 | - 'spreadtrum', |
|
| 179 | - 'steeler', |
|
| 180 | - 'subscriber', |
|
| 181 | - 'suma', |
|
| 182 | - 'superdownloads', |
|
| 183 | - 'svenska-webbsido', |
|
| 184 | - 'teoma', |
|
| 185 | - 'the knowledge AI', |
|
| 186 | - 'thumbshots', |
|
| 187 | - 'tineye', |
|
| 188 | - 'traackr', |
|
| 189 | - 'trendiction', |
|
| 190 | - 'trendsmap', |
|
| 191 | - 'tweetedtimes', |
|
| 192 | - 'tweetmeme', |
|
| 193 | - 'universalfeedparser', |
|
| 194 | - 'uaslinkchecker', |
|
| 195 | - 'undrip', |
|
| 196 | - 'unwindfetchor', |
|
| 197 | - 'upday', |
|
| 198 | - 'vedma', |
|
| 199 | - 'vkshare', |
|
| 200 | - 'vm', |
|
| 201 | - 'wch', |
|
| 202 | - 'webalta', |
|
| 203 | - 'webcookies', |
|
| 204 | - 'webparser', |
|
| 205 | - 'webthumbnail', |
|
| 206 | - 'wesee', |
|
| 207 | - 'wise-guys', |
|
| 208 | - 'woko', |
|
| 209 | - 'wordpress', |
|
| 210 | - 'wotbox', |
|
| 211 | - 'y!j-bri', |
|
| 212 | - 'y!j-bro', |
|
| 213 | - 'y!j-brw', |
|
| 214 | - 'y!j-bsc', |
|
| 215 | - 'yahoo', |
|
| 216 | - 'yahoo!', |
|
| 217 | - 'yahooysmcm', |
|
| 218 | - 'ymobactus', |
|
| 219 | - 'yats', |
|
| 220 | - 'yeti', |
|
| 221 | - 'zeerch' |
|
| 222 | - )) . ',i', |
|
| 223 | - (string)$_SERVER['HTTP_USER_AGENT']) |
|
| 224 | - ); |
|
| 34 | + define('_IS_BOT', |
|
| 35 | + isset($_SERVER['HTTP_USER_AGENT']) |
|
| 36 | + and preg_match(',' |
|
| 37 | + . implode ('|', array( |
|
| 38 | + // mots generiques |
|
| 39 | + 'bot', |
|
| 40 | + 'slurp', |
|
| 41 | + 'crawler', |
|
| 42 | + 'crwlr', |
|
| 43 | + 'java', |
|
| 44 | + 'monitoring', |
|
| 45 | + 'spider', |
|
| 46 | + 'webvac', |
|
| 47 | + 'yandex', |
|
| 48 | + 'MSIE 6\.0', // botnet 99,9% du temps |
|
| 49 | + // UA plus cibles |
|
| 50 | + '200please', |
|
| 51 | + '80legs', |
|
| 52 | + 'a6-indexer', |
|
| 53 | + 'aboundex', |
|
| 54 | + 'accoona', |
|
| 55 | + 'acrylicapps', |
|
| 56 | + 'addthis', |
|
| 57 | + 'adressendeutschland', |
|
| 58 | + 'alexa', |
|
| 59 | + 'altavista', |
|
| 60 | + 'analyticsseo', |
|
| 61 | + 'antennapod', |
|
| 62 | + 'arachnys', |
|
| 63 | + 'archive', |
|
| 64 | + 'argclrint', |
|
| 65 | + 'aspseek', |
|
| 66 | + 'baidu', |
|
| 67 | + 'begunadvertising', |
|
| 68 | + 'bing', |
|
| 69 | + 'bloglines', |
|
| 70 | + 'buck', |
|
| 71 | + 'browsershots', |
|
| 72 | + 'bubing', |
|
| 73 | + 'butterfly', |
|
| 74 | + 'changedetection', |
|
| 75 | + 'charlotte', |
|
| 76 | + 'chilkat', |
|
| 77 | + 'china', |
|
| 78 | + 'coccoc', |
|
| 79 | + 'crowsnest', |
|
| 80 | + 'dataminr', |
|
| 81 | + 'daumoa', |
|
| 82 | + 'dlvr\.it', |
|
| 83 | + 'dlweb', |
|
| 84 | + 'drupal', |
|
| 85 | + 'ec2linkfinder', |
|
| 86 | + 'eset\.com', |
|
| 87 | + 'estyle', |
|
| 88 | + 'exalead', |
|
| 89 | + 'ezooms', |
|
| 90 | + 'facebookexternalhit', |
|
| 91 | + 'facebookplatform', |
|
| 92 | + 'fairshare', |
|
| 93 | + 'feedfetcher', |
|
| 94 | + 'feedfetcher-google', |
|
| 95 | + 'feedly', |
|
| 96 | + 'fetch', |
|
| 97 | + 'flipboardproxy', |
|
| 98 | + 'genieo', |
|
| 99 | + 'google', |
|
| 100 | + 'go-http-client', |
|
| 101 | + 'grapeshot', |
|
| 102 | + 'hatena-useragent', |
|
| 103 | + 'head', |
|
| 104 | + 'hosttracker', |
|
| 105 | + 'hubspot', |
|
| 106 | + 'ia_archiver', |
|
| 107 | + 'ichiro', |
|
| 108 | + 'iltrovatore-setaccio', |
|
| 109 | + 'immediatenet', |
|
| 110 | + 'ina', |
|
| 111 | + 'inoreader', |
|
| 112 | + 'infegyatlas', |
|
| 113 | + 'infohelfer', |
|
| 114 | + 'instapaper', |
|
| 115 | + 'jabse', |
|
| 116 | + 'james', |
|
| 117 | + 'jersey', |
|
| 118 | + 'kumkie', |
|
| 119 | + 'linkdex', |
|
| 120 | + 'linkfluence', |
|
| 121 | + 'linkwalker', |
|
| 122 | + 'litefinder', |
|
| 123 | + 'loadimpactpageanalyzer', |
|
| 124 | + 'ltx71', |
|
| 125 | + 'luminate', |
|
| 126 | + 'lycos', |
|
| 127 | + 'lycosa', |
|
| 128 | + 'mediapartners-google', |
|
| 129 | + 'msai', |
|
| 130 | + 'myapp', |
|
| 131 | + 'nativehost', |
|
| 132 | + 'najdi', |
|
| 133 | + 'netcraftsurveyagent', |
|
| 134 | + 'netestate', |
|
| 135 | + 'netseer', |
|
| 136 | + 'netnewswire', |
|
| 137 | + 'newspaper', |
|
| 138 | + 'newsblur', |
|
| 139 | + 'nuhk', |
|
| 140 | + 'nuzzel', |
|
| 141 | + 'okhttp', |
|
| 142 | + 'otmedia', |
|
| 143 | + 'owlin', |
|
| 144 | + 'owncloud', |
|
| 145 | + 'panscient', |
|
| 146 | + 'paper\.li', |
|
| 147 | + 'parsijoo', |
|
| 148 | + 'protopage', |
|
| 149 | + 'plukkie', |
|
| 150 | + 'proximic', |
|
| 151 | + 'pubsub', |
|
| 152 | + 'python', |
|
| 153 | + 'qirina', |
|
| 154 | + 'qoshe', |
|
| 155 | + 'qualidator', |
|
| 156 | + 'qwantify', |
|
| 157 | + 'rambler', |
|
| 158 | + 'readability', |
|
| 159 | + 'ruby', |
|
| 160 | + 'sbsearch', |
|
| 161 | + 'scoop\.it', |
|
| 162 | + 'scooter', |
|
| 163 | + 'scoutjet', |
|
| 164 | + 'scrapy', |
|
| 165 | + 'scrubby', |
|
| 166 | + 'scrubbybloglines', |
|
| 167 | + 'shareaholic', |
|
| 168 | + 'shopwiki', |
|
| 169 | + 'simplepie', |
|
| 170 | + 'sistrix', |
|
| 171 | + 'sitechecker', |
|
| 172 | + 'siteexplorer', |
|
| 173 | + 'snapshot', |
|
| 174 | + 'sogou', |
|
| 175 | + 'special_archiver', |
|
| 176 | + 'speedy', |
|
| 177 | + 'spinn3r', |
|
| 178 | + 'spreadtrum', |
|
| 179 | + 'steeler', |
|
| 180 | + 'subscriber', |
|
| 181 | + 'suma', |
|
| 182 | + 'superdownloads', |
|
| 183 | + 'svenska-webbsido', |
|
| 184 | + 'teoma', |
|
| 185 | + 'the knowledge AI', |
|
| 186 | + 'thumbshots', |
|
| 187 | + 'tineye', |
|
| 188 | + 'traackr', |
|
| 189 | + 'trendiction', |
|
| 190 | + 'trendsmap', |
|
| 191 | + 'tweetedtimes', |
|
| 192 | + 'tweetmeme', |
|
| 193 | + 'universalfeedparser', |
|
| 194 | + 'uaslinkchecker', |
|
| 195 | + 'undrip', |
|
| 196 | + 'unwindfetchor', |
|
| 197 | + 'upday', |
|
| 198 | + 'vedma', |
|
| 199 | + 'vkshare', |
|
| 200 | + 'vm', |
|
| 201 | + 'wch', |
|
| 202 | + 'webalta', |
|
| 203 | + 'webcookies', |
|
| 204 | + 'webparser', |
|
| 205 | + 'webthumbnail', |
|
| 206 | + 'wesee', |
|
| 207 | + 'wise-guys', |
|
| 208 | + 'woko', |
|
| 209 | + 'wordpress', |
|
| 210 | + 'wotbox', |
|
| 211 | + 'y!j-bri', |
|
| 212 | + 'y!j-bro', |
|
| 213 | + 'y!j-brw', |
|
| 214 | + 'y!j-bsc', |
|
| 215 | + 'yahoo', |
|
| 216 | + 'yahoo!', |
|
| 217 | + 'yahooysmcm', |
|
| 218 | + 'ymobactus', |
|
| 219 | + 'yats', |
|
| 220 | + 'yeti', |
|
| 221 | + 'zeerch' |
|
| 222 | + )) . ',i', |
|
| 223 | + (string)$_SERVER['HTTP_USER_AGENT']) |
|
| 224 | + ); |
|
| 225 | 225 | } |
| 226 | 226 | if (!defined('_IS_BOT_FRIEND')){ |
| 227 | - define('_IS_BOT_FRIEND', |
|
| 228 | - isset($_SERVER['HTTP_USER_AGENT']) |
|
| 229 | - and preg_match(',' . implode ('|', array( |
|
| 230 | - 'facebookexternalhit', |
|
| 231 | - 'flipboardproxy', |
|
| 232 | - 'wordpress' |
|
| 233 | - )) . ',i', |
|
| 234 | - (string)$_SERVER['HTTP_USER_AGENT']) |
|
| 235 | - ); |
|
| 227 | + define('_IS_BOT_FRIEND', |
|
| 228 | + isset($_SERVER['HTTP_USER_AGENT']) |
|
| 229 | + and preg_match(',' . implode ('|', array( |
|
| 230 | + 'facebookexternalhit', |
|
| 231 | + 'flipboardproxy', |
|
| 232 | + 'wordpress' |
|
| 233 | + )) . ',i', |
|
| 234 | + (string)$_SERVER['HTTP_USER_AGENT']) |
|
| 235 | + ); |
|
| 236 | 236 | } |
| 237 | 237 | |
| 238 | 238 | /* |
@@ -244,17 +244,17 @@ discard block |
||
| 244 | 244 | */ |
| 245 | 245 | $_exceptions = array('id_table','id_base','id_parent','id_article_pdf'); |
| 246 | 246 | foreach ($_GET as $var => $val) |
| 247 | - if ($_GET[$var] and strncmp($var, "id_", 3) == 0 |
|
| 248 | - and !in_array($var, $_exceptions)) |
|
| 249 | - $_GET[$var] = is_array($_GET[$var])?@array_map('intval', $_GET[$var]):intval($_GET[$var]); |
|
| 247 | + if ($_GET[$var] and strncmp($var, "id_", 3) == 0 |
|
| 248 | + and !in_array($var, $_exceptions)) |
|
| 249 | + $_GET[$var] = is_array($_GET[$var])?@array_map('intval', $_GET[$var]):intval($_GET[$var]); |
|
| 250 | 250 | foreach ($_POST as $var => $val) |
| 251 | - if ($_POST[$var] and strncmp($var, "id_", 3) == 0 |
|
| 252 | - and !in_array($var, $_exceptions)) |
|
| 253 | - $_POST[$var] = is_array($_POST[$var])?@array_map('intval', $_POST[$var]):intval($_POST[$var]); |
|
| 251 | + if ($_POST[$var] and strncmp($var, "id_", 3) == 0 |
|
| 252 | + and !in_array($var, $_exceptions)) |
|
| 253 | + $_POST[$var] = is_array($_POST[$var])?@array_map('intval', $_POST[$var]):intval($_POST[$var]); |
|
| 254 | 254 | foreach ($GLOBALS as $var => $val) |
| 255 | - if ($GLOBALS[$var] and strncmp($var, "id_", 3) == 0 |
|
| 256 | - and !in_array($var, $_exceptions)) |
|
| 257 | - $GLOBALS[$var] = is_array($GLOBALS[$var])?@array_map('intval', $GLOBALS[$var]):intval($GLOBALS[$var]); |
|
| 255 | + if ($GLOBALS[$var] and strncmp($var, "id_", 3) == 0 |
|
| 256 | + and !in_array($var, $_exceptions)) |
|
| 257 | + $GLOBALS[$var] = is_array($GLOBALS[$var])?@array_map('intval', $GLOBALS[$var]):intval($GLOBALS[$var]); |
|
| 258 | 258 | |
| 259 | 259 | /* |
| 260 | 260 | * Interdit la variable $cjpeg_command, qui était utilisée sans |
@@ -266,17 +266,17 @@ discard block |
||
| 266 | 266 | * Contrôle de quelques variables (XSS) |
| 267 | 267 | */ |
| 268 | 268 | foreach(array('lang', 'var_recherche', 'aide', 'var_lang_r', 'lang_r', 'var_ajax_ancre', 'nom_fichier') as $var) { |
| 269 | - if (isset($_GET[$var])) |
|
| 270 | - $_REQUEST[$var] = $GLOBALS[$var] = $_GET[$var] = preg_replace(',[^\w\,/#&;-]+,', ' ', (string)$_GET[$var]); |
|
| 271 | - if (isset($_POST[$var])) |
|
| 272 | - $_REQUEST[$var] = $GLOBALS[$var] = $_POST[$var] = preg_replace(',[^\w\,/#&;-]+,', ' ', (string)$_POST[$var]); |
|
| 269 | + if (isset($_GET[$var])) |
|
| 270 | + $_REQUEST[$var] = $GLOBALS[$var] = $_GET[$var] = preg_replace(',[^\w\,/#&;-]+,', ' ', (string)$_GET[$var]); |
|
| 271 | + if (isset($_POST[$var])) |
|
| 272 | + $_REQUEST[$var] = $GLOBALS[$var] = $_POST[$var] = preg_replace(',[^\w\,/#&;-]+,', ' ', (string)$_POST[$var]); |
|
| 273 | 273 | } |
| 274 | 274 | |
| 275 | 275 | /* |
| 276 | 276 | * Filtre l'accès à spip_acces_doc (injection SQL en 1.8.2x) |
| 277 | 277 | */ |
| 278 | 278 | if (preg_match(',^(.*/)?spip_acces_doc\.,', (string)$_SERVER['REQUEST_URI'])) { |
| 279 | - $file = addslashes((string)$_GET['file']); |
|
| 279 | + $file = addslashes((string)$_GET['file']); |
|
| 280 | 280 | } |
| 281 | 281 | |
| 282 | 282 | /* |
@@ -285,61 +285,61 @@ discard block |
||
| 285 | 285 | if (isset($_REQUEST['mode']) and isset($_REQUEST['page']) |
| 286 | 286 | and !in_array($_REQUEST['mode'], array("6forum", "1comite")) |
| 287 | 287 | and $_REQUEST['page'] == "identifiants") |
| 288 | - $ecran_securite_raison = "identifiants"; |
|
| 288 | + $ecran_securite_raison = "identifiants"; |
|
| 289 | 289 | |
| 290 | 290 | /* |
| 291 | 291 | * Agenda joue à l'injection php |
| 292 | 292 | */ |
| 293 | 293 | if (isset($_REQUEST['partie_cal']) |
| 294 | 294 | and $_REQUEST['partie_cal'] !== htmlentities((string)$_REQUEST['partie_cal'])) |
| 295 | - $ecran_securite_raison = "partie_cal"; |
|
| 295 | + $ecran_securite_raison = "partie_cal"; |
|
| 296 | 296 | if (isset($_REQUEST['echelle']) |
| 297 | 297 | and $_REQUEST['echelle'] !== htmlentities((string)$_REQUEST['echelle'])) |
| 298 | - $ecran_securite_raison = "echelle"; |
|
| 298 | + $ecran_securite_raison = "echelle"; |
|
| 299 | 299 | |
| 300 | 300 | /* |
| 301 | 301 | * Espace privé |
| 302 | 302 | */ |
| 303 | 303 | if (isset($_REQUEST['exec']) |
| 304 | 304 | and !preg_match(',^[\w-]+$,', (string)$_REQUEST['exec'])) |
| 305 | - $ecran_securite_raison = "exec"; |
|
| 305 | + $ecran_securite_raison = "exec"; |
|
| 306 | 306 | if (isset($_REQUEST['cherche_auteur']) |
| 307 | 307 | and preg_match(',[<],', (string)$_REQUEST['cherche_auteur'])) |
| 308 | - $ecran_securite_raison = "cherche_auteur"; |
|
| 308 | + $ecran_securite_raison = "cherche_auteur"; |
|
| 309 | 309 | if (isset($_REQUEST['exec']) |
| 310 | 310 | and $_REQUEST['exec'] == 'auteurs' |
| 311 | 311 | and preg_match(',[<],', (string)$_REQUEST['recherche'])) |
| 312 | - $ecran_securite_raison = "recherche"; |
|
| 312 | + $ecran_securite_raison = "recherche"; |
|
| 313 | 313 | if (isset($_REQUEST['exec']) |
| 314 | 314 | and $_REQUEST['exec'] == 'info_plugin' |
| 315 | 315 | and preg_match(',[<],', (string)$_REQUEST['plugin'])) |
| 316 | - $ecran_securite_raison = "plugin"; |
|
| 316 | + $ecran_securite_raison = "plugin"; |
|
| 317 | 317 | if (isset($_REQUEST['exec']) |
| 318 | 318 | and $_REQUEST['exec'] == 'puce_statut' |
| 319 | 319 | and isset($_REQUEST['id']) |
| 320 | 320 | and !intval($_REQUEST['id'])) |
| 321 | - $ecran_securite_raison = "puce_statut"; |
|
| 321 | + $ecran_securite_raison = "puce_statut"; |
|
| 322 | 322 | if (isset($_REQUEST['action']) |
| 323 | 323 | and $_REQUEST['action'] == 'configurer') { |
| 324 | - if (@file_exists('inc_version.php') |
|
| 325 | - or @file_exists('ecrire/inc_version.php')) { |
|
| 326 | - function action_configurer() { |
|
| 327 | - include_spip('inc/autoriser'); |
|
| 328 | - if(!autoriser('configurer', _request('configuration'))) { |
|
| 329 | - include_spip('inc/minipres'); |
|
| 330 | - echo minipres(_T('info_acces_interdit')); |
|
| 331 | - exit; |
|
| 332 | - } |
|
| 333 | - require _DIR_RESTREINT.'action/configurer.php'; |
|
| 334 | - action_configurer_dist(); |
|
| 335 | - } |
|
| 336 | - } |
|
| 324 | + if (@file_exists('inc_version.php') |
|
| 325 | + or @file_exists('ecrire/inc_version.php')) { |
|
| 326 | + function action_configurer() { |
|
| 327 | + include_spip('inc/autoriser'); |
|
| 328 | + if(!autoriser('configurer', _request('configuration'))) { |
|
| 329 | + include_spip('inc/minipres'); |
|
| 330 | + echo minipres(_T('info_acces_interdit')); |
|
| 331 | + exit; |
|
| 332 | + } |
|
| 333 | + require _DIR_RESTREINT.'action/configurer.php'; |
|
| 334 | + action_configurer_dist(); |
|
| 335 | + } |
|
| 336 | + } |
|
| 337 | 337 | } |
| 338 | 338 | if (isset($_REQUEST['action']) |
| 339 | 339 | and $_REQUEST['action'] == 'ordonner_liens_documents' |
| 340 | 340 | and isset($_REQUEST['ordre']) |
| 341 | 341 | and is_string($_REQUEST['ordre'])){ |
| 342 | - $ecran_securite_raison = "ordre a la chaine"; |
|
| 342 | + $ecran_securite_raison = "ordre a la chaine"; |
|
| 343 | 343 | } |
| 344 | 344 | |
| 345 | 345 | |
@@ -347,24 +347,24 @@ discard block |
||
| 347 | 347 | * Bloque les requêtes contenant %00 (manipulation d'include) |
| 348 | 348 | */ |
| 349 | 349 | if (strpos( |
| 350 | - @get_magic_quotes_gpc() ? |
|
| 351 | - stripslashes(serialize($_REQUEST)) : serialize($_REQUEST), |
|
| 352 | - chr(0) |
|
| 350 | + @get_magic_quotes_gpc() ? |
|
| 351 | + stripslashes(serialize($_REQUEST)) : serialize($_REQUEST), |
|
| 352 | + chr(0) |
|
| 353 | 353 | ) !== false) |
| 354 | - $ecran_securite_raison = "%00"; |
|
| 354 | + $ecran_securite_raison = "%00"; |
|
| 355 | 355 | |
| 356 | 356 | /* |
| 357 | 357 | * Bloque les requêtes fond=formulaire_ |
| 358 | 358 | */ |
| 359 | 359 | if (isset($_REQUEST['fond']) |
| 360 | 360 | and preg_match(',^formulaire_,i', $_REQUEST['fond'])) |
| 361 | - $ecran_securite_raison = "fond=formulaire_"; |
|
| 361 | + $ecran_securite_raison = "fond=formulaire_"; |
|
| 362 | 362 | |
| 363 | 363 | /* |
| 364 | 364 | * Bloque les requêtes du type ?GLOBALS[type_urls]=toto (bug vieux php) |
| 365 | 365 | */ |
| 366 | 366 | if (isset($_REQUEST['GLOBALS'])) |
| 367 | - $ecran_securite_raison = "GLOBALS[GLOBALS]"; |
|
| 367 | + $ecran_securite_raison = "GLOBALS[GLOBALS]"; |
|
| 368 | 368 | |
| 369 | 369 | /* |
| 370 | 370 | * Bloque les requêtes des bots sur: |
@@ -372,26 +372,26 @@ discard block |
||
| 372 | 372 | * les paginations entremélées |
| 373 | 373 | */ |
| 374 | 374 | if (_IS_BOT and ( |
| 375 | - (isset($_REQUEST['echelle']) and isset($_REQUEST['partie_cal']) and isset($_REQUEST['type'])) |
|
| 376 | - or (strpos((string)$_SERVER['REQUEST_URI'], 'debut_') and preg_match(',[?&]debut_.*&debut_,', (string)$_SERVER['REQUEST_URI'])) |
|
| 377 | - or (isset($_REQUEST['calendrier_annee']) and strpos((string)$_SERVER['REQUEST_URI'], 'debut_') ) |
|
| 378 | - or (isset($_REQUEST['calendrier_annee']) and preg_match(',[?&]calendrier_annee=.*&calendrier_annee=,', (string)$_SERVER['REQUEST_URI'])) |
|
| 375 | + (isset($_REQUEST['echelle']) and isset($_REQUEST['partie_cal']) and isset($_REQUEST['type'])) |
|
| 376 | + or (strpos((string)$_SERVER['REQUEST_URI'], 'debut_') and preg_match(',[?&]debut_.*&debut_,', (string)$_SERVER['REQUEST_URI'])) |
|
| 377 | + or (isset($_REQUEST['calendrier_annee']) and strpos((string)$_SERVER['REQUEST_URI'], 'debut_') ) |
|
| 378 | + or (isset($_REQUEST['calendrier_annee']) and preg_match(',[?&]calendrier_annee=.*&calendrier_annee=,', (string)$_SERVER['REQUEST_URI'])) |
|
| 379 | 379 | ) |
| 380 | 380 | ) |
| 381 | - $ecran_securite_raison = "robot agenda/double pagination"; |
|
| 381 | + $ecran_securite_raison = "robot agenda/double pagination"; |
|
| 382 | 382 | |
| 383 | 383 | /* |
| 384 | 384 | * Bloque une vieille page de tests de CFG (<1.11) |
| 385 | 385 | * Bloque un XSS sur une page inexistante |
| 386 | 386 | */ |
| 387 | 387 | if (isset($_REQUEST['page'])) { |
| 388 | - if ($_REQUEST['page'] == 'test_cfg') |
|
| 389 | - $ecran_securite_raison = "test_cfg"; |
|
| 390 | - if ($_REQUEST['page'] !== htmlspecialchars((string)$_REQUEST['page'])) |
|
| 391 | - $ecran_securite_raison = "xsspage"; |
|
| 392 | - if ($_REQUEST['page'] == '404' |
|
| 393 | - and isset($_REQUEST['erreur'])) |
|
| 394 | - $ecran_securite_raison = "xss404"; |
|
| 388 | + if ($_REQUEST['page'] == 'test_cfg') |
|
| 389 | + $ecran_securite_raison = "test_cfg"; |
|
| 390 | + if ($_REQUEST['page'] !== htmlspecialchars((string)$_REQUEST['page'])) |
|
| 391 | + $ecran_securite_raison = "xsspage"; |
|
| 392 | + if ($_REQUEST['page'] == '404' |
|
| 393 | + and isset($_REQUEST['erreur'])) |
|
| 394 | + $ecran_securite_raison = "xss404"; |
|
| 395 | 395 | } |
| 396 | 396 | |
| 397 | 397 | /* |
@@ -399,40 +399,40 @@ discard block |
||
| 399 | 399 | */ |
| 400 | 400 | foreach (array('var_login') as $var) |
| 401 | 401 | if (isset($_REQUEST[$var]) and is_array($_REQUEST[$var])) |
| 402 | - $ecran_securite_raison = "xss ".$var; |
|
| 402 | + $ecran_securite_raison = "xss ".$var; |
|
| 403 | 403 | |
| 404 | 404 | /* |
| 405 | 405 | * Parade antivirale contre un cheval de troie |
| 406 | 406 | */ |
| 407 | 407 | if (!function_exists('tmp_lkojfghx')) { |
| 408 | - function tmp_lkojfghx() {} |
|
| 409 | - function tmp_lkojfghx2($a = 0, $b = 0, $c = 0, $d = 0) { |
|
| 410 | - // si jamais on est arrivé ici sur une erreur php |
|
| 411 | - // et qu'un autre gestionnaire d'erreur est défini, l'appeller |
|
| 412 | - if ($b && $GLOBALS['tmp_xhgfjokl']) |
|
| 413 | - call_user_func($GLOBALS['tmp_xhgfjokl'], $a, $b, $c, $d); |
|
| 414 | - } |
|
| 408 | + function tmp_lkojfghx() {} |
|
| 409 | + function tmp_lkojfghx2($a = 0, $b = 0, $c = 0, $d = 0) { |
|
| 410 | + // si jamais on est arrivé ici sur une erreur php |
|
| 411 | + // et qu'un autre gestionnaire d'erreur est défini, l'appeller |
|
| 412 | + if ($b && $GLOBALS['tmp_xhgfjokl']) |
|
| 413 | + call_user_func($GLOBALS['tmp_xhgfjokl'], $a, $b, $c, $d); |
|
| 414 | + } |
|
| 415 | 415 | } |
| 416 | 416 | if (isset($_POST['tmp_lkojfghx3'])) |
| 417 | - $ecran_securite_raison = "gumblar"; |
|
| 417 | + $ecran_securite_raison = "gumblar"; |
|
| 418 | 418 | |
| 419 | 419 | /* |
| 420 | 420 | * Outils XML mal sécurisés < 2.0.9 |
| 421 | 421 | */ |
| 422 | 422 | if (isset($_REQUEST['transformer_xml'])) |
| 423 | - $ecran_securite_raison = "transformer_xml"; |
|
| 423 | + $ecran_securite_raison = "transformer_xml"; |
|
| 424 | 424 | |
| 425 | 425 | /* |
| 426 | 426 | * Outils XML mal sécurisés again |
| 427 | 427 | */ |
| 428 | 428 | if (isset($_REQUEST['var_url']) and $_REQUEST['var_url'] and isset($_REQUEST['exec']) and $_REQUEST['exec']=='valider_xml'){ |
| 429 | - $url = trim($_REQUEST['var_url']); |
|
| 430 | - if (strncmp($url,'/',1)==0 |
|
| 431 | - or (($p=strpos($url,'..'))!==false AND strpos($url,'..',$p+3)!==false) |
|
| 432 | - or (($p=strpos($url,'..'))!==false AND strpos($url,'IMG',$p+3)!==false) |
|
| 433 | - or (strpos($url,'://')!==false or strpos($url,':\\')!==false)) { |
|
| 434 | - $ecran_securite_raison = 'URL interdite pour var_url'; |
|
| 435 | - } |
|
| 429 | + $url = trim($_REQUEST['var_url']); |
|
| 430 | + if (strncmp($url,'/',1)==0 |
|
| 431 | + or (($p=strpos($url,'..'))!==false AND strpos($url,'..',$p+3)!==false) |
|
| 432 | + or (($p=strpos($url,'..'))!==false AND strpos($url,'IMG',$p+3)!==false) |
|
| 433 | + or (strpos($url,'://')!==false or strpos($url,':\\')!==false)) { |
|
| 434 | + $ecran_securite_raison = 'URL interdite pour var_url'; |
|
| 435 | + } |
|
| 436 | 436 | } |
| 437 | 437 | |
| 438 | 438 | /* |
@@ -440,10 +440,10 @@ discard block |
||
| 440 | 440 | */ |
| 441 | 441 | if (isset($_REQUEST['nom_sauvegarde']) |
| 442 | 442 | and strstr((string)$_REQUEST['nom_sauvegarde'], '/')) |
| 443 | - $ecran_securite_raison = 'nom_sauvegarde manipulee'; |
|
| 443 | + $ecran_securite_raison = 'nom_sauvegarde manipulee'; |
|
| 444 | 444 | if (isset($_REQUEST['znom_sauvegarde']) |
| 445 | 445 | and strstr((string)$_REQUEST['znom_sauvegarde'], '/')) |
| 446 | - $ecran_securite_raison = 'znom_sauvegarde manipulee'; |
|
| 446 | + $ecran_securite_raison = 'znom_sauvegarde manipulee'; |
|
| 447 | 447 | |
| 448 | 448 | |
| 449 | 449 | /* |
@@ -452,26 +452,26 @@ discard block |
||
| 452 | 452 | */ |
| 453 | 453 | if (isset($_REQUEST['op']) and isset($_REQUEST['page']) |
| 454 | 454 | and $_REQUEST['op'] !== preg_replace('/[^\-\w]/', '', $_REQUEST['op'])) |
| 455 | - $ecran_securite_raison = 'op'; |
|
| 455 | + $ecran_securite_raison = 'op'; |
|
| 456 | 456 | |
| 457 | 457 | /* |
| 458 | 458 | * Forms & Table ne se méfiait pas assez des uploads de fichiers |
| 459 | 459 | */ |
| 460 | 460 | if (count($_FILES)){ |
| 461 | - foreach($_FILES as $k => $v){ |
|
| 462 | - if (preg_match(',^fichier_\d+$,', $k) |
|
| 463 | - and preg_match(',\.php,i', $v['name'])) |
|
| 464 | - unset($_FILES[$k]); |
|
| 465 | - } |
|
| 461 | + foreach($_FILES as $k => $v){ |
|
| 462 | + if (preg_match(',^fichier_\d+$,', $k) |
|
| 463 | + and preg_match(',\.php,i', $v['name'])) |
|
| 464 | + unset($_FILES[$k]); |
|
| 465 | + } |
|
| 466 | 466 | } |
| 467 | 467 | /* |
| 468 | 468 | * et Contact trop laxiste avec une variable externe |
| 469 | 469 | * on bloque pas le post pour eviter de perdre des donnees mais on unset la variable et c'est tout |
| 470 | 470 | */ |
| 471 | 471 | if (isset($_REQUEST['pj_enregistrees_nom']) and $_REQUEST['pj_enregistrees_nom']){ |
| 472 | - unset($_REQUEST['pj_enregistrees_nom']); |
|
| 473 | - unset($_GET['pj_enregistrees_nom']); |
|
| 474 | - unset($_POST['pj_enregistrees_nom']); |
|
| 472 | + unset($_REQUEST['pj_enregistrees_nom']); |
|
| 473 | + unset($_GET['pj_enregistrees_nom']); |
|
| 474 | + unset($_POST['pj_enregistrees_nom']); |
|
| 475 | 475 | } |
| 476 | 476 | |
| 477 | 477 | /* |
@@ -479,35 +479,35 @@ discard block |
||
| 479 | 479 | */ |
| 480 | 480 | if (isset($_REQUEST['reinstall']) |
| 481 | 481 | and $_REQUEST['reinstall'] == 'oui') |
| 482 | - $ecran_securite_raison = 'reinstall=oui'; |
|
| 482 | + $ecran_securite_raison = 'reinstall=oui'; |
|
| 483 | 483 | |
| 484 | 484 | /* |
| 485 | 485 | * Pas d'action pendant l'install |
| 486 | 486 | */ |
| 487 | 487 | if (isset($_REQUEST['exec']) and $_REQUEST['exec'] === 'install' and isset($_REQUEST['action'])) { |
| 488 | - $ecran_securite_raison = 'install&action impossibles'; |
|
| 488 | + $ecran_securite_raison = 'install&action impossibles'; |
|
| 489 | 489 | } |
| 490 | 490 | |
| 491 | 491 | /* |
| 492 | 492 | * Échappement xss referer |
| 493 | 493 | */ |
| 494 | 494 | if (isset($_SERVER['HTTP_REFERER'])) |
| 495 | - $_SERVER['HTTP_REFERER'] = strtr($_SERVER['HTTP_REFERER'], '<>"\'', '[]##'); |
|
| 495 | + $_SERVER['HTTP_REFERER'] = strtr($_SERVER['HTTP_REFERER'], '<>"\'', '[]##'); |
|
| 496 | 496 | |
| 497 | 497 | |
| 498 | 498 | /* |
| 499 | 499 | * Echappement HTTP_X_FORWARDED_HOST |
| 500 | 500 | */ |
| 501 | 501 | if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) |
| 502 | - $_SERVER['HTTP_X_FORWARDED_HOST'] = strtr($_SERVER['HTTP_X_FORWARDED_HOST'], "<>?\"\{\}\$'` \r\n", '____________'); |
|
| 502 | + $_SERVER['HTTP_X_FORWARDED_HOST'] = strtr($_SERVER['HTTP_X_FORWARDED_HOST'], "<>?\"\{\}\$'` \r\n", '____________'); |
|
| 503 | 503 | |
| 504 | 504 | |
| 505 | 505 | /* |
| 506 | 506 | * Pas d'erreur dans l'erreur |
| 507 | 507 | */ |
| 508 | 508 | if (isset($_REQUEST['var_erreur']) and isset($_REQUEST['page']) and $_REQUEST['page'] === 'login') { |
| 509 | - if (strlen($_REQUEST['var_erreur']) !== strcspn($_REQUEST['var_erreur'], '<>')) |
|
| 510 | - $ecran_securite_raison = 'var_erreur incorrecte'; |
|
| 509 | + if (strlen($_REQUEST['var_erreur']) !== strcspn($_REQUEST['var_erreur'], '<>')) |
|
| 510 | + $ecran_securite_raison = 'var_erreur incorrecte'; |
|
| 511 | 511 | } |
| 512 | 512 | |
| 513 | 513 | |
@@ -515,46 +515,46 @@ discard block |
||
| 515 | 515 | * Réinjection des clés en html dans l'admin r19561 |
| 516 | 516 | */ |
| 517 | 517 | if (strpos($_SERVER['REQUEST_URI'], "ecrire/") !== false or isset($_REQUEST['var_memotri'])){ |
| 518 | - $zzzz = implode("", array_keys($_REQUEST)); |
|
| 519 | - if (strlen($zzzz) != strcspn($zzzz, '<>"\'')) |
|
| 520 | - $ecran_securite_raison = 'Cle incorrecte en $_REQUEST'; |
|
| 518 | + $zzzz = implode("", array_keys($_REQUEST)); |
|
| 519 | + if (strlen($zzzz) != strcspn($zzzz, '<>"\'')) |
|
| 520 | + $ecran_securite_raison = 'Cle incorrecte en $_REQUEST'; |
|
| 521 | 521 | } |
| 522 | 522 | |
| 523 | 523 | /* |
| 524 | 524 | * Injection par connect |
| 525 | 525 | */ |
| 526 | 526 | if (isset($_REQUEST['connect']) |
| 527 | - and |
|
| 528 | - // cas qui permettent de sortir d'un commentaire PHP |
|
| 529 | - (strpos($_REQUEST['connect'], "?") !== false |
|
| 530 | - or strpos($_REQUEST['connect'], "<") !== false |
|
| 531 | - or strpos($_REQUEST['connect'], ">") !== false |
|
| 532 | - or strpos($_REQUEST['connect'], "\n") !== false |
|
| 533 | - or strpos($_REQUEST['connect'], "\r") !== false) |
|
| 534 | - ) { |
|
| 535 | - $ecran_securite_raison = "malformed connect argument"; |
|
| 527 | + and |
|
| 528 | + // cas qui permettent de sortir d'un commentaire PHP |
|
| 529 | + (strpos($_REQUEST['connect'], "?") !== false |
|
| 530 | + or strpos($_REQUEST['connect'], "<") !== false |
|
| 531 | + or strpos($_REQUEST['connect'], ">") !== false |
|
| 532 | + or strpos($_REQUEST['connect'], "\n") !== false |
|
| 533 | + or strpos($_REQUEST['connect'], "\r") !== false) |
|
| 534 | + ) { |
|
| 535 | + $ecran_securite_raison = "malformed connect argument"; |
|
| 536 | 536 | } |
| 537 | 537 | |
| 538 | 538 | /* |
| 539 | 539 | * S'il y a une raison de mourir, mourons |
| 540 | 540 | */ |
| 541 | 541 | if (isset($ecran_securite_raison)) { |
| 542 | - header("HTTP/1.0 403 Forbidden"); |
|
| 543 | - header("Expires: Wed, 11 Jan 1984 05:00:00 GMT"); |
|
| 544 | - header("Cache-Control: no-cache, must-revalidate"); |
|
| 545 | - header("Pragma: no-cache"); |
|
| 546 | - header("Content-Type: text/html"); |
|
| 547 | - die("<html><title>Error 403: Forbidden</title><body><h1>Error 403</h1><p>You are not authorized to view this page ($ecran_securite_raison)</p></body></html>"); |
|
| 542 | + header("HTTP/1.0 403 Forbidden"); |
|
| 543 | + header("Expires: Wed, 11 Jan 1984 05:00:00 GMT"); |
|
| 544 | + header("Cache-Control: no-cache, must-revalidate"); |
|
| 545 | + header("Pragma: no-cache"); |
|
| 546 | + header("Content-Type: text/html"); |
|
| 547 | + die("<html><title>Error 403: Forbidden</title><body><h1>Error 403</h1><p>You are not authorized to view this page ($ecran_securite_raison)</p></body></html>"); |
|
| 548 | 548 | } |
| 549 | 549 | |
| 550 | 550 | /* |
| 551 | 551 | * Un filtre filtrer_entites securise |
| 552 | 552 | */ |
| 553 | 553 | if (!function_exists('filtre_filtrer_entites_dist')) { |
| 554 | - function filtre_filtrer_entites_dist($t) { |
|
| 555 | - include_spip('inc/texte'); |
|
| 556 | - return interdire_scripts(filtrer_entites($t)); |
|
| 557 | - } |
|
| 554 | + function filtre_filtrer_entites_dist($t) { |
|
| 555 | + include_spip('inc/texte'); |
|
| 556 | + return interdire_scripts(filtrer_entites($t)); |
|
| 557 | + } |
|
| 558 | 558 | } |
| 559 | 559 | |
| 560 | 560 | |
@@ -568,35 +568,35 @@ discard block |
||
| 568 | 568 | * Bloque les bots quand le load déborde |
| 569 | 569 | */ |
| 570 | 570 | if (!defined('_ECRAN_SECURITE_LOAD')) |
| 571 | - define('_ECRAN_SECURITE_LOAD', 4); |
|
| 571 | + define('_ECRAN_SECURITE_LOAD', 4); |
|
| 572 | 572 | |
| 573 | 573 | if ( |
| 574 | - defined('_ECRAN_SECURITE_LOAD') |
|
| 575 | - and _ECRAN_SECURITE_LOAD > 0 |
|
| 576 | - and _IS_BOT |
|
| 577 | - and !_IS_BOT_FRIEND |
|
| 578 | - and $_SERVER['REQUEST_METHOD'] === 'GET' |
|
| 579 | - and ( |
|
| 580 | - (function_exists('sys_getloadavg') |
|
| 581 | - and $load = sys_getloadavg() |
|
| 582 | - and is_array($load) |
|
| 583 | - and $load = array_shift($load) |
|
| 584 | - ) |
|
| 585 | - or |
|
| 586 | - (@is_readable('/proc/loadavg') |
|
| 587 | - and $load = file_get_contents('/proc/loadavg') |
|
| 588 | - and $load = floatval($load) |
|
| 589 | - ) |
|
| 590 | - ) |
|
| 591 | - and $load > _ECRAN_SECURITE_LOAD // eviter l'evaluation suivante si de toute facon le load est inferieur a la limite |
|
| 592 | - and rand(0, $load * $load) > _ECRAN_SECURITE_LOAD * _ECRAN_SECURITE_LOAD |
|
| 574 | + defined('_ECRAN_SECURITE_LOAD') |
|
| 575 | + and _ECRAN_SECURITE_LOAD > 0 |
|
| 576 | + and _IS_BOT |
|
| 577 | + and !_IS_BOT_FRIEND |
|
| 578 | + and $_SERVER['REQUEST_METHOD'] === 'GET' |
|
| 579 | + and ( |
|
| 580 | + (function_exists('sys_getloadavg') |
|
| 581 | + and $load = sys_getloadavg() |
|
| 582 | + and is_array($load) |
|
| 583 | + and $load = array_shift($load) |
|
| 584 | + ) |
|
| 585 | + or |
|
| 586 | + (@is_readable('/proc/loadavg') |
|
| 587 | + and $load = file_get_contents('/proc/loadavg') |
|
| 588 | + and $load = floatval($load) |
|
| 589 | + ) |
|
| 590 | + ) |
|
| 591 | + and $load > _ECRAN_SECURITE_LOAD // eviter l'evaluation suivante si de toute facon le load est inferieur a la limite |
|
| 592 | + and rand(0, $load * $load) > _ECRAN_SECURITE_LOAD * _ECRAN_SECURITE_LOAD |
|
| 593 | 593 | ) { |
| 594 | - //https://webmasters.stackexchange.com/questions/65674/should-i-return-a-429-or-503-status-code-to-a-bot |
|
| 595 | - header("HTTP/1.0 429 Too Many Requests"); |
|
| 596 | - header("Retry-After: 300"); |
|
| 597 | - header("Expires: Wed, 11 Jan 1984 05:00:00 GMT"); |
|
| 598 | - header("Cache-Control: no-cache, must-revalidate"); |
|
| 599 | - header("Pragma: no-cache"); |
|
| 600 | - header("Content-Type: text/html"); |
|
| 601 | - die("<html><title>Status 429: Too Many Requests</title><body><h1>Status 429</h1><p>Too Many Requests (try again soon)</p></body></html>"); |
|
| 594 | + //https://webmasters.stackexchange.com/questions/65674/should-i-return-a-429-or-503-status-code-to-a-bot |
|
| 595 | + header("HTTP/1.0 429 Too Many Requests"); |
|
| 596 | + header("Retry-After: 300"); |
|
| 597 | + header("Expires: Wed, 11 Jan 1984 05:00:00 GMT"); |
|
| 598 | + header("Cache-Control: no-cache, must-revalidate"); |
|
| 599 | + header("Pragma: no-cache"); |
|
| 600 | + header("Content-Type: text/html"); |
|
| 601 | + die("<html><title>Status 429: Too Many Requests</title><body><h1>Status 429</h1><p>Too Many Requests (try again soon)</p></body></html>"); |
|
| 602 | 602 | } |
@@ -23,18 +23,18 @@ discard block |
||
| 23 | 23 | * var_isbot=1 peut etre utilise pour monitorer la disponibilite pour les bots (sujets a 503 de delestage si |
| 24 | 24 | * le load depasse ECRAN_SECURITE_LOAD) |
| 25 | 25 | */ |
| 26 | -if (!defined('_IS_BOT') and isset($_GET['var_isbot'])){ |
|
| 26 | +if (!defined('_IS_BOT') and isset($_GET['var_isbot'])) { |
|
| 27 | 27 | define('_IS_BOT', $_GET['var_isbot'] ? true : false); |
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 30 | /* |
| 31 | 31 | * Détecteur de robot d'indexation |
| 32 | 32 | */ |
| 33 | -if (!defined('_IS_BOT')){ |
|
| 33 | +if (!defined('_IS_BOT')) { |
|
| 34 | 34 | define('_IS_BOT', |
| 35 | 35 | isset($_SERVER['HTTP_USER_AGENT']) |
| 36 | 36 | and preg_match(',' |
| 37 | - . implode ('|', array( |
|
| 37 | + . implode('|', array( |
|
| 38 | 38 | // mots generiques |
| 39 | 39 | 'bot', |
| 40 | 40 | 'slurp', |
@@ -219,19 +219,19 @@ discard block |
||
| 219 | 219 | 'yats', |
| 220 | 220 | 'yeti', |
| 221 | 221 | 'zeerch' |
| 222 | - )) . ',i', |
|
| 223 | - (string)$_SERVER['HTTP_USER_AGENT']) |
|
| 222 | + )).',i', |
|
| 223 | + (string) $_SERVER['HTTP_USER_AGENT']) |
|
| 224 | 224 | ); |
| 225 | 225 | } |
| 226 | -if (!defined('_IS_BOT_FRIEND')){ |
|
| 226 | +if (!defined('_IS_BOT_FRIEND')) { |
|
| 227 | 227 | define('_IS_BOT_FRIEND', |
| 228 | 228 | isset($_SERVER['HTTP_USER_AGENT']) |
| 229 | - and preg_match(',' . implode ('|', array( |
|
| 229 | + and preg_match(','.implode('|', array( |
|
| 230 | 230 | 'facebookexternalhit', |
| 231 | 231 | 'flipboardproxy', |
| 232 | 232 | 'wordpress' |
| 233 | - )) . ',i', |
|
| 234 | - (string)$_SERVER['HTTP_USER_AGENT']) |
|
| 233 | + )).',i', |
|
| 234 | + (string) $_SERVER['HTTP_USER_AGENT']) |
|
| 235 | 235 | ); |
| 236 | 236 | } |
| 237 | 237 | |
@@ -242,19 +242,19 @@ discard block |
||
| 242 | 242 | * (sauf pour id_table, qui n'est pas numérique jusqu'à [5743]) |
| 243 | 243 | * (id_base est une variable de la config des widgets de WordPress) |
| 244 | 244 | */ |
| 245 | -$_exceptions = array('id_table','id_base','id_parent','id_article_pdf'); |
|
| 245 | +$_exceptions = array('id_table', 'id_base', 'id_parent', 'id_article_pdf'); |
|
| 246 | 246 | foreach ($_GET as $var => $val) |
| 247 | 247 | if ($_GET[$var] and strncmp($var, "id_", 3) == 0 |
| 248 | 248 | and !in_array($var, $_exceptions)) |
| 249 | - $_GET[$var] = is_array($_GET[$var])?@array_map('intval', $_GET[$var]):intval($_GET[$var]); |
|
| 249 | + $_GET[$var] = is_array($_GET[$var]) ? @array_map('intval', $_GET[$var]) : intval($_GET[$var]); |
|
| 250 | 250 | foreach ($_POST as $var => $val) |
| 251 | 251 | if ($_POST[$var] and strncmp($var, "id_", 3) == 0 |
| 252 | 252 | and !in_array($var, $_exceptions)) |
| 253 | - $_POST[$var] = is_array($_POST[$var])?@array_map('intval', $_POST[$var]):intval($_POST[$var]); |
|
| 253 | + $_POST[$var] = is_array($_POST[$var]) ? @array_map('intval', $_POST[$var]) : intval($_POST[$var]); |
|
| 254 | 254 | foreach ($GLOBALS as $var => $val) |
| 255 | 255 | if ($GLOBALS[$var] and strncmp($var, "id_", 3) == 0 |
| 256 | 256 | and !in_array($var, $_exceptions)) |
| 257 | - $GLOBALS[$var] = is_array($GLOBALS[$var])?@array_map('intval', $GLOBALS[$var]):intval($GLOBALS[$var]); |
|
| 257 | + $GLOBALS[$var] = is_array($GLOBALS[$var]) ? @array_map('intval', $GLOBALS[$var]) : intval($GLOBALS[$var]); |
|
| 258 | 258 | |
| 259 | 259 | /* |
| 260 | 260 | * Interdit la variable $cjpeg_command, qui était utilisée sans |
@@ -265,18 +265,18 @@ discard block |
||
| 265 | 265 | /* |
| 266 | 266 | * Contrôle de quelques variables (XSS) |
| 267 | 267 | */ |
| 268 | -foreach(array('lang', 'var_recherche', 'aide', 'var_lang_r', 'lang_r', 'var_ajax_ancre', 'nom_fichier') as $var) { |
|
| 268 | +foreach (array('lang', 'var_recherche', 'aide', 'var_lang_r', 'lang_r', 'var_ajax_ancre', 'nom_fichier') as $var) { |
|
| 269 | 269 | if (isset($_GET[$var])) |
| 270 | - $_REQUEST[$var] = $GLOBALS[$var] = $_GET[$var] = preg_replace(',[^\w\,/#&;-]+,', ' ', (string)$_GET[$var]); |
|
| 270 | + $_REQUEST[$var] = $GLOBALS[$var] = $_GET[$var] = preg_replace(',[^\w\,/#&;-]+,', ' ', (string) $_GET[$var]); |
|
| 271 | 271 | if (isset($_POST[$var])) |
| 272 | - $_REQUEST[$var] = $GLOBALS[$var] = $_POST[$var] = preg_replace(',[^\w\,/#&;-]+,', ' ', (string)$_POST[$var]); |
|
| 272 | + $_REQUEST[$var] = $GLOBALS[$var] = $_POST[$var] = preg_replace(',[^\w\,/#&;-]+,', ' ', (string) $_POST[$var]); |
|
| 273 | 273 | } |
| 274 | 274 | |
| 275 | 275 | /* |
| 276 | 276 | * Filtre l'accès à spip_acces_doc (injection SQL en 1.8.2x) |
| 277 | 277 | */ |
| 278 | -if (preg_match(',^(.*/)?spip_acces_doc\.,', (string)$_SERVER['REQUEST_URI'])) { |
|
| 279 | - $file = addslashes((string)$_GET['file']); |
|
| 278 | +if (preg_match(',^(.*/)?spip_acces_doc\.,', (string) $_SERVER['REQUEST_URI'])) { |
|
| 279 | + $file = addslashes((string) $_GET['file']); |
|
| 280 | 280 | } |
| 281 | 281 | |
| 282 | 282 | /* |
@@ -291,28 +291,28 @@ discard block |
||
| 291 | 291 | * Agenda joue à l'injection php |
| 292 | 292 | */ |
| 293 | 293 | if (isset($_REQUEST['partie_cal']) |
| 294 | -and $_REQUEST['partie_cal'] !== htmlentities((string)$_REQUEST['partie_cal'])) |
|
| 294 | +and $_REQUEST['partie_cal'] !== htmlentities((string) $_REQUEST['partie_cal'])) |
|
| 295 | 295 | $ecran_securite_raison = "partie_cal"; |
| 296 | 296 | if (isset($_REQUEST['echelle']) |
| 297 | -and $_REQUEST['echelle'] !== htmlentities((string)$_REQUEST['echelle'])) |
|
| 297 | +and $_REQUEST['echelle'] !== htmlentities((string) $_REQUEST['echelle'])) |
|
| 298 | 298 | $ecran_securite_raison = "echelle"; |
| 299 | 299 | |
| 300 | 300 | /* |
| 301 | 301 | * Espace privé |
| 302 | 302 | */ |
| 303 | 303 | if (isset($_REQUEST['exec']) |
| 304 | -and !preg_match(',^[\w-]+$,', (string)$_REQUEST['exec'])) |
|
| 304 | +and !preg_match(',^[\w-]+$,', (string) $_REQUEST['exec'])) |
|
| 305 | 305 | $ecran_securite_raison = "exec"; |
| 306 | 306 | if (isset($_REQUEST['cherche_auteur']) |
| 307 | -and preg_match(',[<],', (string)$_REQUEST['cherche_auteur'])) |
|
| 307 | +and preg_match(',[<],', (string) $_REQUEST['cherche_auteur'])) |
|
| 308 | 308 | $ecran_securite_raison = "cherche_auteur"; |
| 309 | 309 | if (isset($_REQUEST['exec']) |
| 310 | 310 | and $_REQUEST['exec'] == 'auteurs' |
| 311 | -and preg_match(',[<],', (string)$_REQUEST['recherche'])) |
|
| 311 | +and preg_match(',[<],', (string) $_REQUEST['recherche'])) |
|
| 312 | 312 | $ecran_securite_raison = "recherche"; |
| 313 | 313 | if (isset($_REQUEST['exec']) |
| 314 | 314 | and $_REQUEST['exec'] == 'info_plugin' |
| 315 | -and preg_match(',[<],', (string)$_REQUEST['plugin'])) |
|
| 315 | +and preg_match(',[<],', (string) $_REQUEST['plugin'])) |
|
| 316 | 316 | $ecran_securite_raison = "plugin"; |
| 317 | 317 | if (isset($_REQUEST['exec']) |
| 318 | 318 | and $_REQUEST['exec'] == 'puce_statut' |
@@ -325,7 +325,7 @@ discard block |
||
| 325 | 325 | or @file_exists('ecrire/inc_version.php')) { |
| 326 | 326 | function action_configurer() { |
| 327 | 327 | include_spip('inc/autoriser'); |
| 328 | - if(!autoriser('configurer', _request('configuration'))) { |
|
| 328 | + if (!autoriser('configurer', _request('configuration'))) { |
|
| 329 | 329 | include_spip('inc/minipres'); |
| 330 | 330 | echo minipres(_T('info_acces_interdit')); |
| 331 | 331 | exit; |
@@ -338,7 +338,7 @@ discard block |
||
| 338 | 338 | if (isset($_REQUEST['action']) |
| 339 | 339 | and $_REQUEST['action'] == 'ordonner_liens_documents' |
| 340 | 340 | and isset($_REQUEST['ordre']) |
| 341 | -and is_string($_REQUEST['ordre'])){ |
|
| 341 | +and is_string($_REQUEST['ordre'])) { |
|
| 342 | 342 | $ecran_securite_raison = "ordre a la chaine"; |
| 343 | 343 | } |
| 344 | 344 | |
@@ -373,9 +373,9 @@ discard block |
||
| 373 | 373 | */ |
| 374 | 374 | if (_IS_BOT and ( |
| 375 | 375 | (isset($_REQUEST['echelle']) and isset($_REQUEST['partie_cal']) and isset($_REQUEST['type'])) |
| 376 | - or (strpos((string)$_SERVER['REQUEST_URI'], 'debut_') and preg_match(',[?&]debut_.*&debut_,', (string)$_SERVER['REQUEST_URI'])) |
|
| 377 | - or (isset($_REQUEST['calendrier_annee']) and strpos((string)$_SERVER['REQUEST_URI'], 'debut_') ) |
|
| 378 | - or (isset($_REQUEST['calendrier_annee']) and preg_match(',[?&]calendrier_annee=.*&calendrier_annee=,', (string)$_SERVER['REQUEST_URI'])) |
|
| 376 | + or (strpos((string) $_SERVER['REQUEST_URI'], 'debut_') and preg_match(',[?&]debut_.*&debut_,', (string) $_SERVER['REQUEST_URI'])) |
|
| 377 | + or (isset($_REQUEST['calendrier_annee']) and strpos((string) $_SERVER['REQUEST_URI'], 'debut_')) |
|
| 378 | + or (isset($_REQUEST['calendrier_annee']) and preg_match(',[?&]calendrier_annee=.*&calendrier_annee=,', (string) $_SERVER['REQUEST_URI'])) |
|
| 379 | 379 | ) |
| 380 | 380 | ) |
| 381 | 381 | $ecran_securite_raison = "robot agenda/double pagination"; |
@@ -387,7 +387,7 @@ discard block |
||
| 387 | 387 | if (isset($_REQUEST['page'])) { |
| 388 | 388 | if ($_REQUEST['page'] == 'test_cfg') |
| 389 | 389 | $ecran_securite_raison = "test_cfg"; |
| 390 | - if ($_REQUEST['page'] !== htmlspecialchars((string)$_REQUEST['page'])) |
|
| 390 | + if ($_REQUEST['page'] !== htmlspecialchars((string) $_REQUEST['page'])) |
|
| 391 | 391 | $ecran_securite_raison = "xsspage"; |
| 392 | 392 | if ($_REQUEST['page'] == '404' |
| 393 | 393 | and isset($_REQUEST['erreur'])) |
@@ -425,12 +425,12 @@ discard block |
||
| 425 | 425 | /* |
| 426 | 426 | * Outils XML mal sécurisés again |
| 427 | 427 | */ |
| 428 | -if (isset($_REQUEST['var_url']) and $_REQUEST['var_url'] and isset($_REQUEST['exec']) and $_REQUEST['exec']=='valider_xml'){ |
|
| 428 | +if (isset($_REQUEST['var_url']) and $_REQUEST['var_url'] and isset($_REQUEST['exec']) and $_REQUEST['exec'] == 'valider_xml') { |
|
| 429 | 429 | $url = trim($_REQUEST['var_url']); |
| 430 | - if (strncmp($url,'/',1)==0 |
|
| 431 | - or (($p=strpos($url,'..'))!==false AND strpos($url,'..',$p+3)!==false) |
|
| 432 | - or (($p=strpos($url,'..'))!==false AND strpos($url,'IMG',$p+3)!==false) |
|
| 433 | - or (strpos($url,'://')!==false or strpos($url,':\\')!==false)) { |
|
| 430 | + if (strncmp($url, '/', 1) == 0 |
|
| 431 | + or (($p = strpos($url, '..')) !== false AND strpos($url, '..', $p + 3) !== false) |
|
| 432 | + or (($p = strpos($url, '..')) !== false AND strpos($url, 'IMG', $p + 3) !== false) |
|
| 433 | + or (strpos($url, '://') !== false or strpos($url, ':\\') !== false)) { |
|
| 434 | 434 | $ecran_securite_raison = 'URL interdite pour var_url'; |
| 435 | 435 | } |
| 436 | 436 | } |
@@ -439,10 +439,10 @@ discard block |
||
| 439 | 439 | * Sauvegarde mal securisée < 2.0.9 |
| 440 | 440 | */ |
| 441 | 441 | if (isset($_REQUEST['nom_sauvegarde']) |
| 442 | -and strstr((string)$_REQUEST['nom_sauvegarde'], '/')) |
|
| 442 | +and strstr((string) $_REQUEST['nom_sauvegarde'], '/')) |
|
| 443 | 443 | $ecran_securite_raison = 'nom_sauvegarde manipulee'; |
| 444 | 444 | if (isset($_REQUEST['znom_sauvegarde']) |
| 445 | -and strstr((string)$_REQUEST['znom_sauvegarde'], '/')) |
|
| 445 | +and strstr((string) $_REQUEST['znom_sauvegarde'], '/')) |
|
| 446 | 446 | $ecran_securite_raison = 'znom_sauvegarde manipulee'; |
| 447 | 447 | |
| 448 | 448 | |
@@ -457,8 +457,8 @@ discard block |
||
| 457 | 457 | /* |
| 458 | 458 | * Forms & Table ne se méfiait pas assez des uploads de fichiers |
| 459 | 459 | */ |
| 460 | -if (count($_FILES)){ |
|
| 461 | - foreach($_FILES as $k => $v){ |
|
| 460 | +if (count($_FILES)) { |
|
| 461 | + foreach ($_FILES as $k => $v) { |
|
| 462 | 462 | if (preg_match(',^fichier_\d+$,', $k) |
| 463 | 463 | and preg_match(',\.php,i', $v['name'])) |
| 464 | 464 | unset($_FILES[$k]); |
@@ -468,7 +468,7 @@ discard block |
||
| 468 | 468 | * et Contact trop laxiste avec une variable externe |
| 469 | 469 | * on bloque pas le post pour eviter de perdre des donnees mais on unset la variable et c'est tout |
| 470 | 470 | */ |
| 471 | -if (isset($_REQUEST['pj_enregistrees_nom']) and $_REQUEST['pj_enregistrees_nom']){ |
|
| 471 | +if (isset($_REQUEST['pj_enregistrees_nom']) and $_REQUEST['pj_enregistrees_nom']) { |
|
| 472 | 472 | unset($_REQUEST['pj_enregistrees_nom']); |
| 473 | 473 | unset($_GET['pj_enregistrees_nom']); |
| 474 | 474 | unset($_POST['pj_enregistrees_nom']); |
@@ -514,7 +514,7 @@ discard block |
||
| 514 | 514 | /* |
| 515 | 515 | * Réinjection des clés en html dans l'admin r19561 |
| 516 | 516 | */ |
| 517 | -if (strpos($_SERVER['REQUEST_URI'], "ecrire/") !== false or isset($_REQUEST['var_memotri'])){ |
|
| 517 | +if (strpos($_SERVER['REQUEST_URI'], "ecrire/") !== false or isset($_REQUEST['var_memotri'])) { |
|
| 518 | 518 | $zzzz = implode("", array_keys($_REQUEST)); |
| 519 | 519 | if (strlen($zzzz) != strcspn($zzzz, '<>"\'')) |
| 520 | 520 | $ecran_securite_raison = 'Cle incorrecte en $_REQUEST'; |
@@ -17,7 +17,7 @@ discard block |
||
| 17 | 17 | **/ |
| 18 | 18 | |
| 19 | 19 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 20 | - return; |
|
| 20 | + return; |
|
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | /** |
@@ -43,14 +43,14 @@ discard block |
||
| 43 | 43 | **/ |
| 44 | 44 | function critere_racine_dist($idb, &$boucles, $crit) { |
| 45 | 45 | |
| 46 | - $not = $crit->not; |
|
| 47 | - $boucle = &$boucles[$idb]; |
|
| 48 | - $id_parent = isset($GLOBALS['exceptions_des_tables'][$boucle->id_table]['id_parent']) ? |
|
| 49 | - $GLOBALS['exceptions_des_tables'][$boucle->id_table]['id_parent'] : |
|
| 50 | - 'id_parent'; |
|
| 46 | + $not = $crit->not; |
|
| 47 | + $boucle = &$boucles[$idb]; |
|
| 48 | + $id_parent = isset($GLOBALS['exceptions_des_tables'][$boucle->id_table]['id_parent']) ? |
|
| 49 | + $GLOBALS['exceptions_des_tables'][$boucle->id_table]['id_parent'] : |
|
| 50 | + 'id_parent'; |
|
| 51 | 51 | |
| 52 | - $c = array("'='", "'$boucle->id_table." . "$id_parent'", 0); |
|
| 53 | - $boucle->where[] = ($crit->not ? array("'NOT'", $c) : $c); |
|
| 52 | + $c = array("'='", "'$boucle->id_table." . "$id_parent'", 0); |
|
| 53 | + $boucle->where[] = ($crit->not ? array("'NOT'", $c) : $c); |
|
| 54 | 54 | } |
| 55 | 55 | |
| 56 | 56 | |
@@ -67,15 +67,15 @@ discard block |
||
| 67 | 67 | * @return void |
| 68 | 68 | **/ |
| 69 | 69 | function critere_exclus_dist($idb, &$boucles, $crit) { |
| 70 | - $not = $crit->not; |
|
| 71 | - $boucle = &$boucles[$idb]; |
|
| 72 | - $id = $boucle->primary; |
|
| 73 | - |
|
| 74 | - if ($not or !$id) { |
|
| 75 | - return (array('zbug_critere_inconnu', array('critere' => $not . $crit->op))); |
|
| 76 | - } |
|
| 77 | - $arg = kwote(calculer_argument_precedent($idb, $id, $boucles)); |
|
| 78 | - $boucle->where[] = array("'!='", "'$boucle->id_table." . "$id'", $arg); |
|
| 70 | + $not = $crit->not; |
|
| 71 | + $boucle = &$boucles[$idb]; |
|
| 72 | + $id = $boucle->primary; |
|
| 73 | + |
|
| 74 | + if ($not or !$id) { |
|
| 75 | + return (array('zbug_critere_inconnu', array('critere' => $not . $crit->op))); |
|
| 76 | + } |
|
| 77 | + $arg = kwote(calculer_argument_precedent($idb, $id, $boucles)); |
|
| 78 | + $boucle->where[] = array("'!='", "'$boucle->id_table." . "$id'", $arg); |
|
| 79 | 79 | } |
| 80 | 80 | |
| 81 | 81 | |
@@ -95,73 +95,73 @@ discard block |
||
| 95 | 95 | * @return void |
| 96 | 96 | **/ |
| 97 | 97 | function critere_doublons_dist($idb, &$boucles, $crit) { |
| 98 | - $boucle = &$boucles[$idb]; |
|
| 99 | - $primary = $boucle->primary; |
|
| 100 | - |
|
| 101 | - // la table nécessite une clé primaire, non composée |
|
| 102 | - if (!$primary or strpos($primary, ',')) { |
|
| 103 | - return (array('zbug_doublon_sur_table_sans_cle_primaire')); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - $not = ($crit->not ? '' : 'NOT'); |
|
| 107 | - |
|
| 108 | - // le doublon s'applique sur un type de boucle (article) |
|
| 109 | - $nom = "'" . $boucle->type_requete . "'"; |
|
| 110 | - |
|
| 111 | - // compléter le nom avec un nom précisé {doublons nom} |
|
| 112 | - // on obtient $nom = "'article' . 'nom'" |
|
| 113 | - if (isset($crit->param[0])) { |
|
| 114 | - $nom .= "." . calculer_liste($crit->param[0], array(), $boucles, $boucles[$idb]->id_parent); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - // code qui déclarera l'index du stockage de nos doublons (pour éviter une notice PHP) |
|
| 118 | - $init_comment = "\n\n\t// Initialise le(s) critère(s) doublons\n"; |
|
| 119 | - $init_code = "\tif (!isset(\$doublons[\$d = $nom])) { \$doublons[\$d] = ''; }\n"; |
|
| 120 | - |
|
| 121 | - // on crée un sql_in avec la clé primaire de la table |
|
| 122 | - // et la collection des doublons déjà emmagasinés dans le tableau |
|
| 123 | - // $doublons et son index, ici $nom |
|
| 124 | - |
|
| 125 | - // debut du code "sql_in('articles.id_article', " |
|
| 126 | - $debut_in = "sql_in('" . $boucle->id_table . '.' . $primary . "', "; |
|
| 127 | - // lecture des données du doublon "$doublons[$doublon_index[] = " |
|
| 128 | - // Attention : boucle->doublons désigne une variable qu'on affecte |
|
| 129 | - $debut_doub = '$doublons[' . (!$not ? '' : ($boucle->doublons . "[]= ")); |
|
| 130 | - |
|
| 131 | - // le debut complet du code des doublons |
|
| 132 | - $debut_doub = $debut_in . $debut_doub; |
|
| 133 | - |
|
| 134 | - // nom du doublon "('article' . 'nom')]" |
|
| 135 | - $fin_doub = "($nom)]"; |
|
| 136 | - |
|
| 137 | - // si on trouve un autre critère doublon, |
|
| 138 | - // on fusionne pour avoir un seul IN, et on s'en va ! |
|
| 139 | - foreach ($boucle->where as $k => $w) { |
|
| 140 | - if (strpos($w[0], $debut_doub) === 0) { |
|
| 141 | - // fusionner le sql_in (du where) |
|
| 142 | - $boucle->where[$k][0] = $debut_doub . $fin_doub . ' . ' . substr($w[0], strlen($debut_in)); |
|
| 143 | - // fusionner l'initialisation (du hash) pour faire plus joli |
|
| 144 | - $x = strpos($boucle->hash, $init_comment); |
|
| 145 | - $len = strlen($init_comment); |
|
| 146 | - $boucle->hash = |
|
| 147 | - substr($boucle->hash, 0, $x + $len) . $init_code . substr($boucle->hash, $x + $len); |
|
| 148 | - |
|
| 149 | - return; |
|
| 150 | - } |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - // mettre l'ensemble dans un tableau pour que ce ne soit pas vu comme une constante |
|
| 154 | - $boucle->where[] = array($debut_doub . $fin_doub . ", '" . $not . "')"); |
|
| 155 | - |
|
| 156 | - // déclarer le doublon s'il n'existe pas encore |
|
| 157 | - $boucle->hash .= $init_comment . $init_code; |
|
| 158 | - |
|
| 159 | - |
|
| 160 | - # la ligne suivante avait l'intention d'eviter une collecte deja faite |
|
| 161 | - # mais elle fait planter une boucle a 2 critere doublons: |
|
| 162 | - # {!doublons A}{doublons B} |
|
| 163 | - # (de http://article.gmane.org/gmane.comp.web.spip.devel/31034) |
|
| 164 | - # if ($crit->not) $boucle->doublons = ""; |
|
| 98 | + $boucle = &$boucles[$idb]; |
|
| 99 | + $primary = $boucle->primary; |
|
| 100 | + |
|
| 101 | + // la table nécessite une clé primaire, non composée |
|
| 102 | + if (!$primary or strpos($primary, ',')) { |
|
| 103 | + return (array('zbug_doublon_sur_table_sans_cle_primaire')); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + $not = ($crit->not ? '' : 'NOT'); |
|
| 107 | + |
|
| 108 | + // le doublon s'applique sur un type de boucle (article) |
|
| 109 | + $nom = "'" . $boucle->type_requete . "'"; |
|
| 110 | + |
|
| 111 | + // compléter le nom avec un nom précisé {doublons nom} |
|
| 112 | + // on obtient $nom = "'article' . 'nom'" |
|
| 113 | + if (isset($crit->param[0])) { |
|
| 114 | + $nom .= "." . calculer_liste($crit->param[0], array(), $boucles, $boucles[$idb]->id_parent); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + // code qui déclarera l'index du stockage de nos doublons (pour éviter une notice PHP) |
|
| 118 | + $init_comment = "\n\n\t// Initialise le(s) critère(s) doublons\n"; |
|
| 119 | + $init_code = "\tif (!isset(\$doublons[\$d = $nom])) { \$doublons[\$d] = ''; }\n"; |
|
| 120 | + |
|
| 121 | + // on crée un sql_in avec la clé primaire de la table |
|
| 122 | + // et la collection des doublons déjà emmagasinés dans le tableau |
|
| 123 | + // $doublons et son index, ici $nom |
|
| 124 | + |
|
| 125 | + // debut du code "sql_in('articles.id_article', " |
|
| 126 | + $debut_in = "sql_in('" . $boucle->id_table . '.' . $primary . "', "; |
|
| 127 | + // lecture des données du doublon "$doublons[$doublon_index[] = " |
|
| 128 | + // Attention : boucle->doublons désigne une variable qu'on affecte |
|
| 129 | + $debut_doub = '$doublons[' . (!$not ? '' : ($boucle->doublons . "[]= ")); |
|
| 130 | + |
|
| 131 | + // le debut complet du code des doublons |
|
| 132 | + $debut_doub = $debut_in . $debut_doub; |
|
| 133 | + |
|
| 134 | + // nom du doublon "('article' . 'nom')]" |
|
| 135 | + $fin_doub = "($nom)]"; |
|
| 136 | + |
|
| 137 | + // si on trouve un autre critère doublon, |
|
| 138 | + // on fusionne pour avoir un seul IN, et on s'en va ! |
|
| 139 | + foreach ($boucle->where as $k => $w) { |
|
| 140 | + if (strpos($w[0], $debut_doub) === 0) { |
|
| 141 | + // fusionner le sql_in (du where) |
|
| 142 | + $boucle->where[$k][0] = $debut_doub . $fin_doub . ' . ' . substr($w[0], strlen($debut_in)); |
|
| 143 | + // fusionner l'initialisation (du hash) pour faire plus joli |
|
| 144 | + $x = strpos($boucle->hash, $init_comment); |
|
| 145 | + $len = strlen($init_comment); |
|
| 146 | + $boucle->hash = |
|
| 147 | + substr($boucle->hash, 0, $x + $len) . $init_code . substr($boucle->hash, $x + $len); |
|
| 148 | + |
|
| 149 | + return; |
|
| 150 | + } |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + // mettre l'ensemble dans un tableau pour que ce ne soit pas vu comme une constante |
|
| 154 | + $boucle->where[] = array($debut_doub . $fin_doub . ", '" . $not . "')"); |
|
| 155 | + |
|
| 156 | + // déclarer le doublon s'il n'existe pas encore |
|
| 157 | + $boucle->hash .= $init_comment . $init_code; |
|
| 158 | + |
|
| 159 | + |
|
| 160 | + # la ligne suivante avait l'intention d'eviter une collecte deja faite |
|
| 161 | + # mais elle fait planter une boucle a 2 critere doublons: |
|
| 162 | + # {!doublons A}{doublons B} |
|
| 163 | + # (de http://article.gmane.org/gmane.comp.web.spip.devel/31034) |
|
| 164 | + # if ($crit->not) $boucle->doublons = ""; |
|
| 165 | 165 | } |
| 166 | 166 | |
| 167 | 167 | |
@@ -182,14 +182,14 @@ discard block |
||
| 182 | 182 | * @return void |
| 183 | 183 | **/ |
| 184 | 184 | function critere_lang_select_dist($idb, &$boucles, $crit) { |
| 185 | - if (!isset($crit->param[1][0]) or !($param = $crit->param[1][0]->texte)) { |
|
| 186 | - $param = 'oui'; |
|
| 187 | - } |
|
| 188 | - if ($crit->not) { |
|
| 189 | - $param = ($param == 'oui') ? 'non' : 'oui'; |
|
| 190 | - } |
|
| 191 | - $boucle = &$boucles[$idb]; |
|
| 192 | - $boucle->lang_select = $param; |
|
| 185 | + if (!isset($crit->param[1][0]) or !($param = $crit->param[1][0]->texte)) { |
|
| 186 | + $param = 'oui'; |
|
| 187 | + } |
|
| 188 | + if ($crit->not) { |
|
| 189 | + $param = ($param == 'oui') ? 'non' : 'oui'; |
|
| 190 | + } |
|
| 191 | + $boucle = &$boucles[$idb]; |
|
| 192 | + $boucle->lang_select = $param; |
|
| 193 | 193 | } |
| 194 | 194 | |
| 195 | 195 | |
@@ -211,18 +211,18 @@ discard block |
||
| 211 | 211 | * @return void |
| 212 | 212 | **/ |
| 213 | 213 | function critere_debut_dist($idb, &$boucles, $crit) { |
| 214 | - list($un, $deux) = $crit->param; |
|
| 215 | - $un = $un[0]->texte; |
|
| 216 | - $deux = $deux[0]->texte; |
|
| 217 | - if ($deux) { |
|
| 218 | - $boucles[$idb]->limit = 'intval($Pile[0]["debut' . |
|
| 219 | - $un . |
|
| 220 | - '"]) . ",' . |
|
| 221 | - $deux . |
|
| 222 | - '"'; |
|
| 223 | - } else { |
|
| 224 | - calculer_critere_DEFAUT_dist($idb, $boucles, $crit); |
|
| 225 | - } |
|
| 214 | + list($un, $deux) = $crit->param; |
|
| 215 | + $un = $un[0]->texte; |
|
| 216 | + $deux = $deux[0]->texte; |
|
| 217 | + if ($deux) { |
|
| 218 | + $boucles[$idb]->limit = 'intval($Pile[0]["debut' . |
|
| 219 | + $un . |
|
| 220 | + '"]) . ",' . |
|
| 221 | + $deux . |
|
| 222 | + '"'; |
|
| 223 | + } else { |
|
| 224 | + calculer_critere_DEFAUT_dist($idb, $boucles, $crit); |
|
| 225 | + } |
|
| 226 | 226 | } |
| 227 | 227 | |
| 228 | 228 | |
@@ -256,57 +256,57 @@ discard block |
||
| 256 | 256 | **/ |
| 257 | 257 | function critere_pagination_dist($idb, &$boucles, $crit) { |
| 258 | 258 | |
| 259 | - $boucle = &$boucles[$idb]; |
|
| 260 | - // definition de la taille de la page |
|
| 261 | - $pas = !isset($crit->param[0][0]) ? "''" |
|
| 262 | - : calculer_liste(array($crit->param[0][0]), array(), $boucles, $boucle->id_parent); |
|
| 263 | - |
|
| 264 | - if (!preg_match(_CODE_QUOTE, $pas, $r)) { |
|
| 265 | - $pas = "((\$a = intval($pas)) ? \$a : 10)"; |
|
| 266 | - } else { |
|
| 267 | - $r = intval($r[2]); |
|
| 268 | - $pas = strval($r ? $r : 10); |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - // Calcul du nommage de la pagination si il existe. |
|
| 272 | - // La nouvelle syntaxe {pagination 20, nom} est prise en compte et privilégiée mais on reste |
|
| 273 | - // compatible avec l'ancienne car certains cas fonctionnent correctement |
|
| 274 | - $type = "'$idb'"; |
|
| 275 | - // Calcul d'un nommage spécifique de la pagination si précisé. |
|
| 276 | - // Syntaxe {pagination 20, nom} |
|
| 277 | - if (isset($crit->param[0][1])) { |
|
| 278 | - $type = calculer_liste(array($crit->param[0][1]), array(), $boucles, $boucle->id_parent); |
|
| 279 | - } // Ancienne syntaxe {pagination 20 nom} pour compatibilité |
|
| 280 | - elseif (isset($crit->param[1][0])) { |
|
| 281 | - $type = calculer_liste(array($crit->param[1][0]), array(), $boucles, $boucle->id_parent); |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - $debut = ($type[0] !== "'") ? "'debut'.$type" : ("'debut" . substr($type, 1)); |
|
| 285 | - $boucle->modificateur['debut_nom'] = $type; |
|
| 286 | - $partie = |
|
| 287 | - // tester si le numero de page demande est de la forme '@yyy' |
|
| 288 | - 'isset($Pile[0][' . $debut . ']) ? $Pile[0][' . $debut . '] : _request(' . $debut . ");\n" |
|
| 289 | - . "\tif(substr(\$debut_boucle,0,1)=='@'){\n" |
|
| 290 | - . "\t\t" . '$debut_boucle = $Pile[0][' . $debut . '] = quete_debut_pagination(\'' . $boucle->primary . '\',$Pile[0][\'@' . $boucle->primary . '\'] = substr($debut_boucle,1),' . $pas . ',$iter);' . "\n" |
|
| 291 | - . "\t\t" . '$iter->seek(0);' . "\n" |
|
| 292 | - . "\t}\n" |
|
| 293 | - . "\t" . '$debut_boucle = intval($debut_boucle)'; |
|
| 294 | - |
|
| 295 | - $boucle->hash .= ' |
|
| 259 | + $boucle = &$boucles[$idb]; |
|
| 260 | + // definition de la taille de la page |
|
| 261 | + $pas = !isset($crit->param[0][0]) ? "''" |
|
| 262 | + : calculer_liste(array($crit->param[0][0]), array(), $boucles, $boucle->id_parent); |
|
| 263 | + |
|
| 264 | + if (!preg_match(_CODE_QUOTE, $pas, $r)) { |
|
| 265 | + $pas = "((\$a = intval($pas)) ? \$a : 10)"; |
|
| 266 | + } else { |
|
| 267 | + $r = intval($r[2]); |
|
| 268 | + $pas = strval($r ? $r : 10); |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + // Calcul du nommage de la pagination si il existe. |
|
| 272 | + // La nouvelle syntaxe {pagination 20, nom} est prise en compte et privilégiée mais on reste |
|
| 273 | + // compatible avec l'ancienne car certains cas fonctionnent correctement |
|
| 274 | + $type = "'$idb'"; |
|
| 275 | + // Calcul d'un nommage spécifique de la pagination si précisé. |
|
| 276 | + // Syntaxe {pagination 20, nom} |
|
| 277 | + if (isset($crit->param[0][1])) { |
|
| 278 | + $type = calculer_liste(array($crit->param[0][1]), array(), $boucles, $boucle->id_parent); |
|
| 279 | + } // Ancienne syntaxe {pagination 20 nom} pour compatibilité |
|
| 280 | + elseif (isset($crit->param[1][0])) { |
|
| 281 | + $type = calculer_liste(array($crit->param[1][0]), array(), $boucles, $boucle->id_parent); |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + $debut = ($type[0] !== "'") ? "'debut'.$type" : ("'debut" . substr($type, 1)); |
|
| 285 | + $boucle->modificateur['debut_nom'] = $type; |
|
| 286 | + $partie = |
|
| 287 | + // tester si le numero de page demande est de la forme '@yyy' |
|
| 288 | + 'isset($Pile[0][' . $debut . ']) ? $Pile[0][' . $debut . '] : _request(' . $debut . ");\n" |
|
| 289 | + . "\tif(substr(\$debut_boucle,0,1)=='@'){\n" |
|
| 290 | + . "\t\t" . '$debut_boucle = $Pile[0][' . $debut . '] = quete_debut_pagination(\'' . $boucle->primary . '\',$Pile[0][\'@' . $boucle->primary . '\'] = substr($debut_boucle,1),' . $pas . ',$iter);' . "\n" |
|
| 291 | + . "\t\t" . '$iter->seek(0);' . "\n" |
|
| 292 | + . "\t}\n" |
|
| 293 | + . "\t" . '$debut_boucle = intval($debut_boucle)'; |
|
| 294 | + |
|
| 295 | + $boucle->hash .= ' |
|
| 296 | 296 | $command[\'pagination\'] = array((isset($Pile[0][' . $debut . ']) ? $Pile[0][' . $debut . '] : null), ' . $pas . ');'; |
| 297 | 297 | |
| 298 | - $boucle->total_parties = $pas; |
|
| 299 | - calculer_parties($boucles, $idb, $partie, 'p+'); |
|
| 300 | - // ajouter la cle primaire dans le select pour pouvoir gerer la pagination referencee par @id |
|
| 301 | - // sauf si pas de primaire, ou si primaire composee |
|
| 302 | - // dans ce cas, on ne sait pas gerer une pagination indirecte |
|
| 303 | - $t = $boucle->id_table . '.' . $boucle->primary; |
|
| 304 | - if ($boucle->primary |
|
| 305 | - and !preg_match('/[,\s]/', $boucle->primary) |
|
| 306 | - and !in_array($t, $boucle->select) |
|
| 307 | - ) { |
|
| 308 | - $boucle->select[] = $t; |
|
| 309 | - } |
|
| 298 | + $boucle->total_parties = $pas; |
|
| 299 | + calculer_parties($boucles, $idb, $partie, 'p+'); |
|
| 300 | + // ajouter la cle primaire dans le select pour pouvoir gerer la pagination referencee par @id |
|
| 301 | + // sauf si pas de primaire, ou si primaire composee |
|
| 302 | + // dans ce cas, on ne sait pas gerer une pagination indirecte |
|
| 303 | + $t = $boucle->id_table . '.' . $boucle->primary; |
|
| 304 | + if ($boucle->primary |
|
| 305 | + and !preg_match('/[,\s]/', $boucle->primary) |
|
| 306 | + and !in_array($t, $boucle->select) |
|
| 307 | + ) { |
|
| 308 | + $boucle->select[] = $t; |
|
| 309 | + } |
|
| 310 | 310 | } |
| 311 | 311 | |
| 312 | 312 | |
@@ -328,24 +328,24 @@ discard block |
||
| 328 | 328 | **/ |
| 329 | 329 | function critere_recherche_dist($idb, &$boucles, $crit) { |
| 330 | 330 | |
| 331 | - $boucle = &$boucles[$idb]; |
|
| 331 | + $boucle = &$boucles[$idb]; |
|
| 332 | 332 | |
| 333 | - if (!$boucle->primary or strpos($boucle->primary, ',')) { |
|
| 334 | - erreur_squelette(_T('zbug_critere_sur_table_sans_cle_primaire', array('critere' => 'recherche')), $boucle); |
|
| 333 | + if (!$boucle->primary or strpos($boucle->primary, ',')) { |
|
| 334 | + erreur_squelette(_T('zbug_critere_sur_table_sans_cle_primaire', array('critere' => 'recherche')), $boucle); |
|
| 335 | 335 | |
| 336 | - return; |
|
| 337 | - } |
|
| 336 | + return; |
|
| 337 | + } |
|
| 338 | 338 | |
| 339 | - if (isset($crit->param[0])) { |
|
| 340 | - $quoi = calculer_liste($crit->param[0], array(), $boucles, $boucles[$idb]->id_parent); |
|
| 341 | - } else { |
|
| 342 | - $quoi = '(isset($Pile[0]["recherche"])?$Pile[0]["recherche"]:(isset($GLOBALS["recherche"])?$GLOBALS["recherche"]:""))'; |
|
| 343 | - } |
|
| 339 | + if (isset($crit->param[0])) { |
|
| 340 | + $quoi = calculer_liste($crit->param[0], array(), $boucles, $boucles[$idb]->id_parent); |
|
| 341 | + } else { |
|
| 342 | + $quoi = '(isset($Pile[0]["recherche"])?$Pile[0]["recherche"]:(isset($GLOBALS["recherche"])?$GLOBALS["recherche"]:""))'; |
|
| 343 | + } |
|
| 344 | 344 | |
| 345 | - $_modificateur = var_export($boucle->modificateur, true); |
|
| 346 | - $boucle->hash .= ' |
|
| 345 | + $_modificateur = var_export($boucle->modificateur, true); |
|
| 346 | + $boucle->hash .= ' |
|
| 347 | 347 | // RECHERCHE' |
| 348 | - . ($crit->cond ? ' |
|
| 348 | + . ($crit->cond ? ' |
|
| 349 | 349 | if (!strlen(' . $quoi . ')){ |
| 350 | 350 | list($rech_select, $rech_where) = array("0 as points",""); |
| 351 | 351 | } else' : '') . ' |
@@ -356,21 +356,21 @@ discard block |
||
| 356 | 356 | '; |
| 357 | 357 | |
| 358 | 358 | |
| 359 | - $t = $boucle->id_table . '.' . $boucle->primary; |
|
| 360 | - if (!in_array($t, $boucles[$idb]->select)) { |
|
| 361 | - $boucle->select[] = $t; |
|
| 362 | - } # pour postgres, neuneu ici |
|
| 363 | - // jointure uniquement sur le serveur principal |
|
| 364 | - // (on ne peut joindre une table d'un serveur distant avec la table des resultats du serveur principal) |
|
| 365 | - if (!$boucle->sql_serveur) { |
|
| 366 | - $boucle->join['resultats'] = array("'" . $boucle->id_table . "'", "'id'", "'" . $boucle->primary . "'"); |
|
| 367 | - $boucle->from['resultats'] = 'spip_resultats'; |
|
| 368 | - } |
|
| 369 | - $boucle->select[] = '$rech_select'; |
|
| 370 | - //$boucle->where[]= "\$rech_where?'resultats.id=".$boucle->id_table.".".$boucle->primary."':''"; |
|
| 371 | - |
|
| 372 | - // et la recherche trouve |
|
| 373 | - $boucle->where[] = '$rech_where?$rech_where:\'\''; |
|
| 359 | + $t = $boucle->id_table . '.' . $boucle->primary; |
|
| 360 | + if (!in_array($t, $boucles[$idb]->select)) { |
|
| 361 | + $boucle->select[] = $t; |
|
| 362 | + } # pour postgres, neuneu ici |
|
| 363 | + // jointure uniquement sur le serveur principal |
|
| 364 | + // (on ne peut joindre une table d'un serveur distant avec la table des resultats du serveur principal) |
|
| 365 | + if (!$boucle->sql_serveur) { |
|
| 366 | + $boucle->join['resultats'] = array("'" . $boucle->id_table . "'", "'id'", "'" . $boucle->primary . "'"); |
|
| 367 | + $boucle->from['resultats'] = 'spip_resultats'; |
|
| 368 | + } |
|
| 369 | + $boucle->select[] = '$rech_select'; |
|
| 370 | + //$boucle->where[]= "\$rech_where?'resultats.id=".$boucle->id_table.".".$boucle->primary."':''"; |
|
| 371 | + |
|
| 372 | + // et la recherche trouve |
|
| 373 | + $boucle->where[] = '$rech_where?$rech_where:\'\''; |
|
| 374 | 374 | } |
| 375 | 375 | |
| 376 | 376 | /** |
@@ -387,25 +387,25 @@ discard block |
||
| 387 | 387 | * @return void |
| 388 | 388 | **/ |
| 389 | 389 | function critere_traduction_dist($idb, &$boucles, $crit) { |
| 390 | - $boucle = &$boucles[$idb]; |
|
| 391 | - $prim = $boucle->primary; |
|
| 392 | - $table = $boucle->id_table; |
|
| 393 | - $arg = kwote(calculer_argument_precedent($idb, 'id_trad', $boucles)); |
|
| 394 | - $dprim = kwote(calculer_argument_precedent($idb, $prim, $boucles)); |
|
| 395 | - $boucle->where[] = |
|
| 396 | - array( |
|
| 397 | - "'OR'", |
|
| 398 | - array( |
|
| 399 | - "'AND'", |
|
| 400 | - array("'='", "'$table.id_trad'", 0), |
|
| 401 | - array("'='", "'$table.$prim'", $dprim) |
|
| 402 | - ), |
|
| 403 | - array( |
|
| 404 | - "'AND'", |
|
| 405 | - array("'>'", "'$table.id_trad'", 0), |
|
| 406 | - array("'='", "'$table.id_trad'", $arg) |
|
| 407 | - ) |
|
| 408 | - ); |
|
| 390 | + $boucle = &$boucles[$idb]; |
|
| 391 | + $prim = $boucle->primary; |
|
| 392 | + $table = $boucle->id_table; |
|
| 393 | + $arg = kwote(calculer_argument_precedent($idb, 'id_trad', $boucles)); |
|
| 394 | + $dprim = kwote(calculer_argument_precedent($idb, $prim, $boucles)); |
|
| 395 | + $boucle->where[] = |
|
| 396 | + array( |
|
| 397 | + "'OR'", |
|
| 398 | + array( |
|
| 399 | + "'AND'", |
|
| 400 | + array("'='", "'$table.id_trad'", 0), |
|
| 401 | + array("'='", "'$table.$prim'", $dprim) |
|
| 402 | + ), |
|
| 403 | + array( |
|
| 404 | + "'AND'", |
|
| 405 | + array("'>'", "'$table.id_trad'", 0), |
|
| 406 | + array("'='", "'$table.id_trad'", $arg) |
|
| 407 | + ) |
|
| 408 | + ); |
|
| 409 | 409 | } |
| 410 | 410 | |
| 411 | 411 | |
@@ -423,17 +423,17 @@ discard block |
||
| 423 | 423 | * @return void |
| 424 | 424 | **/ |
| 425 | 425 | function critere_origine_traduction_dist($idb, &$boucles, $crit) { |
| 426 | - $boucle = &$boucles[$idb]; |
|
| 427 | - $prim = $boucle->primary; |
|
| 428 | - $table = $boucle->id_table; |
|
| 429 | - |
|
| 430 | - $c = |
|
| 431 | - array( |
|
| 432 | - "'OR'", |
|
| 433 | - array("'='", "'$table." . "id_trad'", "'$table.$prim'"), |
|
| 434 | - array("'='", "'$table.id_trad'", "'0'") |
|
| 435 | - ); |
|
| 436 | - $boucle->where[] = ($crit->not ? array("'NOT'", $c) : $c); |
|
| 426 | + $boucle = &$boucles[$idb]; |
|
| 427 | + $prim = $boucle->primary; |
|
| 428 | + $table = $boucle->id_table; |
|
| 429 | + |
|
| 430 | + $c = |
|
| 431 | + array( |
|
| 432 | + "'OR'", |
|
| 433 | + array("'='", "'$table." . "id_trad'", "'$table.$prim'"), |
|
| 434 | + array("'='", "'$table.id_trad'", "'0'") |
|
| 435 | + ); |
|
| 436 | + $boucle->where[] = ($crit->not ? array("'NOT'", $c) : $c); |
|
| 437 | 437 | } |
| 438 | 438 | |
| 439 | 439 | |
@@ -450,20 +450,20 @@ discard block |
||
| 450 | 450 | **/ |
| 451 | 451 | function critere_meme_parent_dist($idb, &$boucles, $crit) { |
| 452 | 452 | |
| 453 | - $boucle = &$boucles[$idb]; |
|
| 454 | - $arg = kwote(calculer_argument_precedent($idb, 'id_parent', $boucles)); |
|
| 455 | - $id_parent = isset($GLOBALS['exceptions_des_tables'][$boucle->id_table]['id_parent']) ? |
|
| 456 | - $GLOBALS['exceptions_des_tables'][$boucle->id_table]['id_parent'] : |
|
| 457 | - 'id_parent'; |
|
| 458 | - $mparent = $boucle->id_table . '.' . $id_parent; |
|
| 453 | + $boucle = &$boucles[$idb]; |
|
| 454 | + $arg = kwote(calculer_argument_precedent($idb, 'id_parent', $boucles)); |
|
| 455 | + $id_parent = isset($GLOBALS['exceptions_des_tables'][$boucle->id_table]['id_parent']) ? |
|
| 456 | + $GLOBALS['exceptions_des_tables'][$boucle->id_table]['id_parent'] : |
|
| 457 | + 'id_parent'; |
|
| 458 | + $mparent = $boucle->id_table . '.' . $id_parent; |
|
| 459 | 459 | |
| 460 | - if ($boucle->type_requete == 'rubriques' or isset($GLOBALS['exceptions_des_tables'][$boucle->id_table]['id_parent'])) { |
|
| 461 | - $boucle->where[] = array("'='", "'$mparent'", $arg); |
|
| 460 | + if ($boucle->type_requete == 'rubriques' or isset($GLOBALS['exceptions_des_tables'][$boucle->id_table]['id_parent'])) { |
|
| 461 | + $boucle->where[] = array("'='", "'$mparent'", $arg); |
|
| 462 | 462 | |
| 463 | - } // le cas FORUMS est gere dans le plugin forum, dans la fonction critere_FORUMS_meme_parent_dist() |
|
| 464 | - else { |
|
| 465 | - return (array('zbug_critere_inconnu', array('critere' => $crit->op . ' ' . $boucle->type_requete))); |
|
| 466 | - } |
|
| 463 | + } // le cas FORUMS est gere dans le plugin forum, dans la fonction critere_FORUMS_meme_parent_dist() |
|
| 464 | + else { |
|
| 465 | + return (array('zbug_critere_inconnu', array('critere' => $crit->op . ' ' . $boucle->type_requete))); |
|
| 466 | + } |
|
| 467 | 467 | } |
| 468 | 468 | |
| 469 | 469 | |
@@ -494,37 +494,37 @@ discard block |
||
| 494 | 494 | **/ |
| 495 | 495 | function critere_branche_dist($idb, &$boucles, $crit) { |
| 496 | 496 | |
| 497 | - $not = $crit->not; |
|
| 498 | - $boucle = &$boucles[$idb]; |
|
| 499 | - // prendre en priorite un identifiant en parametre {branche XX} |
|
| 500 | - if (isset($crit->param[0])) { |
|
| 501 | - $arg = calculer_liste($crit->param[0], array(), $boucles, $boucles[$idb]->id_parent); |
|
| 502 | - // sinon on le prend chez une boucle parente |
|
| 503 | - } else { |
|
| 504 | - $arg = kwote(calculer_argument_precedent($idb, 'id_rubrique', $boucles), $boucle->sql_serveur, 'int NOT NULL'); |
|
| 505 | - } |
|
| 506 | - |
|
| 507 | - //Trouver une jointure |
|
| 508 | - $champ = "id_rubrique"; |
|
| 509 | - $desc = $boucle->show; |
|
| 510 | - //Seulement si necessaire |
|
| 511 | - if (!array_key_exists($champ, $desc['field'])) { |
|
| 512 | - $cle = trouver_jointure_champ($champ, $boucle); |
|
| 513 | - $trouver_table = charger_fonction("trouver_table", "base"); |
|
| 514 | - $desc = $trouver_table($boucle->from[$cle]); |
|
| 515 | - if (count(trouver_champs_decomposes($champ, $desc)) > 1) { |
|
| 516 | - $decompose = decompose_champ_id_objet($champ); |
|
| 517 | - $champ = array_shift($decompose); |
|
| 518 | - $boucle->where[] = array("'='", _q($cle . "." . reset($decompose)), '"' . sql_quote(end($decompose)) . '"'); |
|
| 519 | - } |
|
| 520 | - } else { |
|
| 521 | - $cle = $boucle->id_table; |
|
| 522 | - } |
|
| 523 | - |
|
| 524 | - $c = "sql_in('$cle" . ".$champ', calcul_branche_in($arg)" |
|
| 525 | - . ($not ? ", 'NOT'" : '') . ")"; |
|
| 526 | - $boucle->where[] = !$crit->cond ? $c : |
|
| 527 | - ("($arg ? $c : " . ($not ? "'0=1'" : "'1=1'") . ')'); |
|
| 497 | + $not = $crit->not; |
|
| 498 | + $boucle = &$boucles[$idb]; |
|
| 499 | + // prendre en priorite un identifiant en parametre {branche XX} |
|
| 500 | + if (isset($crit->param[0])) { |
|
| 501 | + $arg = calculer_liste($crit->param[0], array(), $boucles, $boucles[$idb]->id_parent); |
|
| 502 | + // sinon on le prend chez une boucle parente |
|
| 503 | + } else { |
|
| 504 | + $arg = kwote(calculer_argument_precedent($idb, 'id_rubrique', $boucles), $boucle->sql_serveur, 'int NOT NULL'); |
|
| 505 | + } |
|
| 506 | + |
|
| 507 | + //Trouver une jointure |
|
| 508 | + $champ = "id_rubrique"; |
|
| 509 | + $desc = $boucle->show; |
|
| 510 | + //Seulement si necessaire |
|
| 511 | + if (!array_key_exists($champ, $desc['field'])) { |
|
| 512 | + $cle = trouver_jointure_champ($champ, $boucle); |
|
| 513 | + $trouver_table = charger_fonction("trouver_table", "base"); |
|
| 514 | + $desc = $trouver_table($boucle->from[$cle]); |
|
| 515 | + if (count(trouver_champs_decomposes($champ, $desc)) > 1) { |
|
| 516 | + $decompose = decompose_champ_id_objet($champ); |
|
| 517 | + $champ = array_shift($decompose); |
|
| 518 | + $boucle->where[] = array("'='", _q($cle . "." . reset($decompose)), '"' . sql_quote(end($decompose)) . '"'); |
|
| 519 | + } |
|
| 520 | + } else { |
|
| 521 | + $cle = $boucle->id_table; |
|
| 522 | + } |
|
| 523 | + |
|
| 524 | + $c = "sql_in('$cle" . ".$champ', calcul_branche_in($arg)" |
|
| 525 | + . ($not ? ", 'NOT'" : '') . ")"; |
|
| 526 | + $boucle->where[] = !$crit->cond ? $c : |
|
| 527 | + ("($arg ? $c : " . ($not ? "'0=1'" : "'1=1'") . ')'); |
|
| 528 | 528 | } |
| 529 | 529 | |
| 530 | 530 | /** |
@@ -540,15 +540,15 @@ discard block |
||
| 540 | 540 | **/ |
| 541 | 541 | function critere_logo_dist($idb, &$boucles, $crit) { |
| 542 | 542 | |
| 543 | - $boucle = &$boucles[$idb]; |
|
| 544 | - $not = ($crit->not ? 'NOT' : ''); |
|
| 545 | - $serveur = $boucle->sql_serveur; |
|
| 543 | + $boucle = &$boucles[$idb]; |
|
| 544 | + $not = ($crit->not ? 'NOT' : ''); |
|
| 545 | + $serveur = $boucle->sql_serveur; |
|
| 546 | 546 | |
| 547 | - $c = "sql_in('" . |
|
| 548 | - $boucle->id_table . '.' . $boucle->primary |
|
| 549 | - . "', lister_objets_avec_logos('" . $boucle->primary . "'), '$not', '$serveur')"; |
|
| 547 | + $c = "sql_in('" . |
|
| 548 | + $boucle->id_table . '.' . $boucle->primary |
|
| 549 | + . "', lister_objets_avec_logos('" . $boucle->primary . "'), '$not', '$serveur')"; |
|
| 550 | 550 | |
| 551 | - $boucle->where[] = $c; |
|
| 551 | + $boucle->where[] = $c; |
|
| 552 | 552 | } |
| 553 | 553 | |
| 554 | 554 | |
@@ -570,31 +570,31 @@ discard block |
||
| 570 | 570 | * @return void |
| 571 | 571 | **/ |
| 572 | 572 | function critere_fusion_dist($idb, &$boucles, $crit) { |
| 573 | - if ($t = isset($crit->param[0])) { |
|
| 574 | - $t = $crit->param[0]; |
|
| 575 | - if ($t[0]->type == 'texte') { |
|
| 576 | - $t = $t[0]->texte; |
|
| 577 | - if (preg_match("/^(.*)\.(.*)$/", $t, $r)) { |
|
| 578 | - $t = table_objet_sql($r[1]); |
|
| 579 | - $t = array_search($t, $boucles[$idb]->from); |
|
| 580 | - if ($t) { |
|
| 581 | - $t .= '.' . $r[2]; |
|
| 582 | - } |
|
| 583 | - } |
|
| 584 | - } else { |
|
| 585 | - $t = '".' |
|
| 586 | - . calculer_critere_arg_dynamique($idb, $boucles, $t) |
|
| 587 | - . '."'; |
|
| 588 | - } |
|
| 589 | - } |
|
| 590 | - if ($t) { |
|
| 591 | - $boucles[$idb]->group[] = $t; |
|
| 592 | - if (!in_array($t, $boucles[$idb]->select)) { |
|
| 593 | - $boucles[$idb]->select[] = $t; |
|
| 594 | - } |
|
| 595 | - } else { |
|
| 596 | - return (array('zbug_critere_inconnu', array('critere' => $crit->op . ' ?'))); |
|
| 597 | - } |
|
| 573 | + if ($t = isset($crit->param[0])) { |
|
| 574 | + $t = $crit->param[0]; |
|
| 575 | + if ($t[0]->type == 'texte') { |
|
| 576 | + $t = $t[0]->texte; |
|
| 577 | + if (preg_match("/^(.*)\.(.*)$/", $t, $r)) { |
|
| 578 | + $t = table_objet_sql($r[1]); |
|
| 579 | + $t = array_search($t, $boucles[$idb]->from); |
|
| 580 | + if ($t) { |
|
| 581 | + $t .= '.' . $r[2]; |
|
| 582 | + } |
|
| 583 | + } |
|
| 584 | + } else { |
|
| 585 | + $t = '".' |
|
| 586 | + . calculer_critere_arg_dynamique($idb, $boucles, $t) |
|
| 587 | + . '."'; |
|
| 588 | + } |
|
| 589 | + } |
|
| 590 | + if ($t) { |
|
| 591 | + $boucles[$idb]->group[] = $t; |
|
| 592 | + if (!in_array($t, $boucles[$idb]->select)) { |
|
| 593 | + $boucles[$idb]->select[] = $t; |
|
| 594 | + } |
|
| 595 | + } else { |
|
| 596 | + return (array('zbug_critere_inconnu', array('critere' => $crit->op . ' ?'))); |
|
| 597 | + } |
|
| 598 | 598 | } |
| 599 | 599 | |
| 600 | 600 | /** |
@@ -631,45 +631,45 @@ discard block |
||
| 631 | 631 | * @param Critere $crit Paramètres du critère dans cette boucle |
| 632 | 632 | */ |
| 633 | 633 | function critere_collecte_dist($idb, &$boucles, $crit) { |
| 634 | - if (isset($crit->param[0])) { |
|
| 635 | - $_coll = calculer_liste($crit->param[0], array(), $boucles, $boucles[$idb]->id_parent); |
|
| 636 | - $boucle = $boucles[$idb]; |
|
| 637 | - $boucle->modificateur['collate'] = "($_coll ?' COLLATE '.$_coll:'')"; |
|
| 638 | - $n = count($boucle->order); |
|
| 639 | - if ($n && (strpos($boucle->order[$n - 1], 'COLLATE') === false)) { |
|
| 640 | - // l'instruction COLLATE doit être placée avant ASC ou DESC |
|
| 641 | - // notamment lors de l'utilisation `{!par xxx}{collate yyy}` |
|
| 642 | - if ( |
|
| 643 | - (false !== $i = strpos($boucle->order[$n - 1], 'ASC')) |
|
| 644 | - OR (false !== $i = strpos($boucle->order[$n - 1], 'DESC')) |
|
| 645 | - ) { |
|
| 646 | - $boucle->order[$n - 1] = substr_replace($boucle->order[$n - 1], "' . " . $boucle->modificateur['collate'] . " . ' ", $i, 0); |
|
| 647 | - } else { |
|
| 648 | - $boucle->order[$n - 1] .= " . " . $boucle->modificateur['collate']; |
|
| 649 | - } |
|
| 650 | - } |
|
| 651 | - } else { |
|
| 652 | - return (array('zbug_critere_inconnu', array('critere' => $crit->op . " " . count($boucles[$idb]->order)))); |
|
| 653 | - } |
|
| 634 | + if (isset($crit->param[0])) { |
|
| 635 | + $_coll = calculer_liste($crit->param[0], array(), $boucles, $boucles[$idb]->id_parent); |
|
| 636 | + $boucle = $boucles[$idb]; |
|
| 637 | + $boucle->modificateur['collate'] = "($_coll ?' COLLATE '.$_coll:'')"; |
|
| 638 | + $n = count($boucle->order); |
|
| 639 | + if ($n && (strpos($boucle->order[$n - 1], 'COLLATE') === false)) { |
|
| 640 | + // l'instruction COLLATE doit être placée avant ASC ou DESC |
|
| 641 | + // notamment lors de l'utilisation `{!par xxx}{collate yyy}` |
|
| 642 | + if ( |
|
| 643 | + (false !== $i = strpos($boucle->order[$n - 1], 'ASC')) |
|
| 644 | + OR (false !== $i = strpos($boucle->order[$n - 1], 'DESC')) |
|
| 645 | + ) { |
|
| 646 | + $boucle->order[$n - 1] = substr_replace($boucle->order[$n - 1], "' . " . $boucle->modificateur['collate'] . " . ' ", $i, 0); |
|
| 647 | + } else { |
|
| 648 | + $boucle->order[$n - 1] .= " . " . $boucle->modificateur['collate']; |
|
| 649 | + } |
|
| 650 | + } |
|
| 651 | + } else { |
|
| 652 | + return (array('zbug_critere_inconnu', array('critere' => $crit->op . " " . count($boucles[$idb]->order)))); |
|
| 653 | + } |
|
| 654 | 654 | } |
| 655 | 655 | |
| 656 | 656 | // http://code.spip.net/@calculer_critere_arg_dynamique |
| 657 | 657 | function calculer_critere_arg_dynamique($idb, &$boucles, $crit, $suffix = '') { |
| 658 | - $boucle = $boucles[$idb]; |
|
| 659 | - $alt = "('" . $boucle->id_table . '.\' . $x' . $suffix . ')'; |
|
| 660 | - $var = '$champs_' . $idb; |
|
| 661 | - $desc = (strpos($boucle->in, "static $var =") !== false); |
|
| 662 | - if (!$desc) { |
|
| 663 | - $desc = $boucle->show['field']; |
|
| 664 | - $desc = implode(',', array_map('_q', array_keys($desc))); |
|
| 665 | - $boucles[$idb]->in .= "\n\tstatic $var = array(" . $desc . ");"; |
|
| 666 | - } |
|
| 667 | - if ($desc) { |
|
| 668 | - $alt = "(in_array(\$x, $var) ? $alt :(\$x$suffix))"; |
|
| 669 | - } |
|
| 670 | - $arg = calculer_liste($crit, array(), $boucles, $boucle->id_parent); |
|
| 671 | - |
|
| 672 | - return "((\$x = preg_replace(\"/\\W/\",'', $arg)) ? $alt : '')"; |
|
| 658 | + $boucle = $boucles[$idb]; |
|
| 659 | + $alt = "('" . $boucle->id_table . '.\' . $x' . $suffix . ')'; |
|
| 660 | + $var = '$champs_' . $idb; |
|
| 661 | + $desc = (strpos($boucle->in, "static $var =") !== false); |
|
| 662 | + if (!$desc) { |
|
| 663 | + $desc = $boucle->show['field']; |
|
| 664 | + $desc = implode(',', array_map('_q', array_keys($desc))); |
|
| 665 | + $boucles[$idb]->in .= "\n\tstatic $var = array(" . $desc . ");"; |
|
| 666 | + } |
|
| 667 | + if ($desc) { |
|
| 668 | + $alt = "(in_array(\$x, $var) ? $alt :(\$x$suffix))"; |
|
| 669 | + } |
|
| 670 | + $arg = calculer_liste($crit, array(), $boucles, $boucle->id_parent); |
|
| 671 | + |
|
| 672 | + return "((\$x = preg_replace(\"/\\W/\",'', $arg)) ? $alt : '')"; |
|
| 673 | 673 | } |
| 674 | 674 | |
| 675 | 675 | /** |
@@ -708,7 +708,7 @@ discard block |
||
| 708 | 708 | * @param Critere $crit Paramètres du critère dans cette boucle |
| 709 | 709 | */ |
| 710 | 710 | function critere_par_dist($idb, &$boucles, $crit) { |
| 711 | - return critere_parinverse($idb, $boucles, $crit); |
|
| 711 | + return critere_parinverse($idb, $boucles, $crit); |
|
| 712 | 712 | } |
| 713 | 713 | |
| 714 | 714 | /** |
@@ -730,93 +730,93 @@ discard block |
||
| 730 | 730 | * @param Critere $crit Paramètres du critère dans cette boucle |
| 731 | 731 | */ |
| 732 | 732 | function critere_parinverse($idb, &$boucles, $crit) { |
| 733 | - $boucle = &$boucles[$idb]; |
|
| 734 | - |
|
| 735 | - $sens = $collecte = ''; |
|
| 736 | - if ($crit->not) { |
|
| 737 | - $sens = " . ' DESC'"; |
|
| 738 | - } |
|
| 739 | - if (isset($boucle->modificateur['collate'])) { |
|
| 740 | - $collecte = ' . ' . $boucle->modificateur['collate']; |
|
| 741 | - } |
|
| 742 | - |
|
| 743 | - // Pour chaque paramètre du critère |
|
| 744 | - foreach ($crit->param as $tri) { |
|
| 745 | - $order = $fct = ''; |
|
| 746 | - // tris specifiés dynamiquement {par #ENV{tri}} |
|
| 747 | - if ($tri[0]->type != 'texte') { |
|
| 748 | - // calculer le order dynamique qui verifie les champs |
|
| 749 | - $order = calculer_critere_arg_dynamique($idb, $boucles, $tri, $sens); |
|
| 750 | - // ajouter 'hasard' comme possibilité de tri dynamique |
|
| 751 | - calculer_critere_par_hasard($idb, $boucles, $crit); |
|
| 752 | - } |
|
| 753 | - // tris textuels {par titre} |
|
| 754 | - else { |
|
| 755 | - $par = array_shift($tri); |
|
| 756 | - $par = $par->texte; |
|
| 757 | - |
|
| 758 | - // tris de la forme {par expression champ} tel que {par num titre} ou {par multi titre} |
|
| 759 | - if (preg_match(",^(\w+)[\s]+(.*)$,", $par, $m)) { |
|
| 760 | - $expression = trim($m[1]); |
|
| 761 | - $champ = trim($m[2]); |
|
| 762 | - if (function_exists($f = 'calculer_critere_par_expression_' . $expression)) { |
|
| 763 | - $order = $f($idb, $boucles, $crit, $tri, $champ); |
|
| 764 | - } else { |
|
| 765 | - return array('zbug_critere_inconnu', array('critere' => $crit->op . " $par")); |
|
| 766 | - } |
|
| 767 | - |
|
| 768 | - // tris de la forme {par champ} ou {par FONCTION(champ)} |
|
| 769 | - } elseif (preg_match(",^" . CHAMP_SQL_PLUS_FONC . '$,is', $par, $match)) { |
|
| 770 | - // {par FONCTION(champ)} |
|
| 771 | - if (count($match) > 2) { |
|
| 772 | - $par = substr($match[2], 1, -1); |
|
| 773 | - $fct = $match[1]; |
|
| 774 | - } |
|
| 775 | - // quelques cas spécifiques {par hasard}, {par date} |
|
| 776 | - if ($par == 'hasard') { |
|
| 777 | - $order = calculer_critere_par_hasard($idb, $boucles, $crit); |
|
| 778 | - } elseif ($par == 'date' and !empty($boucle->show['date'])) { |
|
| 779 | - $order = "'" . $boucle->id_table . "." . $boucle->show['date'] . "'"; |
|
| 780 | - } else { |
|
| 781 | - // cas général {par champ}, {par table.champ}, ... |
|
| 782 | - $order = calculer_critere_par_champ($idb, $boucles, $crit, $par); |
|
| 783 | - } |
|
| 784 | - } |
|
| 785 | - |
|
| 786 | - // on ne sait pas traiter… |
|
| 787 | - else { |
|
| 788 | - return array('zbug_critere_inconnu', array('critere' => $crit->op . " $par")); |
|
| 789 | - } |
|
| 790 | - |
|
| 791 | - // En cas d'erreur de squelette retournée par une fonction |
|
| 792 | - if (is_array($order)) { |
|
| 793 | - return $order; |
|
| 794 | - } |
|
| 795 | - } |
|
| 796 | - |
|
| 797 | - if (preg_match('/^\'([^"]*)\'$/', $order, $m)) { |
|
| 798 | - $t = $m[1]; |
|
| 799 | - if (strpos($t, '.') and !in_array($t, $boucle->select)) { |
|
| 800 | - $boucle->select[] = $t; |
|
| 801 | - } |
|
| 802 | - } else { |
|
| 803 | - $sens = ''; |
|
| 804 | - } |
|
| 805 | - |
|
| 806 | - if ($fct) { |
|
| 807 | - if (preg_match("/^\s*'(.*)'\s*$/", $order, $r)) { |
|
| 808 | - $order = "'$fct(" . $r[1] . ")'"; |
|
| 809 | - } else { |
|
| 810 | - $order = "'$fct(' . $order . ')'"; |
|
| 811 | - } |
|
| 812 | - } |
|
| 813 | - $t = $order . $collecte . $sens; |
|
| 814 | - if (preg_match("/^(.*)'\s*\.\s*'([^']*')$/", $t, $r)) { |
|
| 815 | - $t = $r[1] . $r[2]; |
|
| 816 | - } |
|
| 817 | - |
|
| 818 | - $boucle->order[] = $t; |
|
| 819 | - } |
|
| 733 | + $boucle = &$boucles[$idb]; |
|
| 734 | + |
|
| 735 | + $sens = $collecte = ''; |
|
| 736 | + if ($crit->not) { |
|
| 737 | + $sens = " . ' DESC'"; |
|
| 738 | + } |
|
| 739 | + if (isset($boucle->modificateur['collate'])) { |
|
| 740 | + $collecte = ' . ' . $boucle->modificateur['collate']; |
|
| 741 | + } |
|
| 742 | + |
|
| 743 | + // Pour chaque paramètre du critère |
|
| 744 | + foreach ($crit->param as $tri) { |
|
| 745 | + $order = $fct = ''; |
|
| 746 | + // tris specifiés dynamiquement {par #ENV{tri}} |
|
| 747 | + if ($tri[0]->type != 'texte') { |
|
| 748 | + // calculer le order dynamique qui verifie les champs |
|
| 749 | + $order = calculer_critere_arg_dynamique($idb, $boucles, $tri, $sens); |
|
| 750 | + // ajouter 'hasard' comme possibilité de tri dynamique |
|
| 751 | + calculer_critere_par_hasard($idb, $boucles, $crit); |
|
| 752 | + } |
|
| 753 | + // tris textuels {par titre} |
|
| 754 | + else { |
|
| 755 | + $par = array_shift($tri); |
|
| 756 | + $par = $par->texte; |
|
| 757 | + |
|
| 758 | + // tris de la forme {par expression champ} tel que {par num titre} ou {par multi titre} |
|
| 759 | + if (preg_match(",^(\w+)[\s]+(.*)$,", $par, $m)) { |
|
| 760 | + $expression = trim($m[1]); |
|
| 761 | + $champ = trim($m[2]); |
|
| 762 | + if (function_exists($f = 'calculer_critere_par_expression_' . $expression)) { |
|
| 763 | + $order = $f($idb, $boucles, $crit, $tri, $champ); |
|
| 764 | + } else { |
|
| 765 | + return array('zbug_critere_inconnu', array('critere' => $crit->op . " $par")); |
|
| 766 | + } |
|
| 767 | + |
|
| 768 | + // tris de la forme {par champ} ou {par FONCTION(champ)} |
|
| 769 | + } elseif (preg_match(",^" . CHAMP_SQL_PLUS_FONC . '$,is', $par, $match)) { |
|
| 770 | + // {par FONCTION(champ)} |
|
| 771 | + if (count($match) > 2) { |
|
| 772 | + $par = substr($match[2], 1, -1); |
|
| 773 | + $fct = $match[1]; |
|
| 774 | + } |
|
| 775 | + // quelques cas spécifiques {par hasard}, {par date} |
|
| 776 | + if ($par == 'hasard') { |
|
| 777 | + $order = calculer_critere_par_hasard($idb, $boucles, $crit); |
|
| 778 | + } elseif ($par == 'date' and !empty($boucle->show['date'])) { |
|
| 779 | + $order = "'" . $boucle->id_table . "." . $boucle->show['date'] . "'"; |
|
| 780 | + } else { |
|
| 781 | + // cas général {par champ}, {par table.champ}, ... |
|
| 782 | + $order = calculer_critere_par_champ($idb, $boucles, $crit, $par); |
|
| 783 | + } |
|
| 784 | + } |
|
| 785 | + |
|
| 786 | + // on ne sait pas traiter… |
|
| 787 | + else { |
|
| 788 | + return array('zbug_critere_inconnu', array('critere' => $crit->op . " $par")); |
|
| 789 | + } |
|
| 790 | + |
|
| 791 | + // En cas d'erreur de squelette retournée par une fonction |
|
| 792 | + if (is_array($order)) { |
|
| 793 | + return $order; |
|
| 794 | + } |
|
| 795 | + } |
|
| 796 | + |
|
| 797 | + if (preg_match('/^\'([^"]*)\'$/', $order, $m)) { |
|
| 798 | + $t = $m[1]; |
|
| 799 | + if (strpos($t, '.') and !in_array($t, $boucle->select)) { |
|
| 800 | + $boucle->select[] = $t; |
|
| 801 | + } |
|
| 802 | + } else { |
|
| 803 | + $sens = ''; |
|
| 804 | + } |
|
| 805 | + |
|
| 806 | + if ($fct) { |
|
| 807 | + if (preg_match("/^\s*'(.*)'\s*$/", $order, $r)) { |
|
| 808 | + $order = "'$fct(" . $r[1] . ")'"; |
|
| 809 | + } else { |
|
| 810 | + $order = "'$fct(' . $order . ')'"; |
|
| 811 | + } |
|
| 812 | + } |
|
| 813 | + $t = $order . $collecte . $sens; |
|
| 814 | + if (preg_match("/^(.*)'\s*\.\s*'([^']*')$/", $t, $r)) { |
|
| 815 | + $t = $r[1] . $r[2]; |
|
| 816 | + } |
|
| 817 | + |
|
| 818 | + $boucle->order[] = $t; |
|
| 819 | + } |
|
| 820 | 820 | } |
| 821 | 821 | |
| 822 | 822 | /** |
@@ -830,13 +830,13 @@ discard block |
||
| 830 | 830 | * @return string Clause pour le Order by |
| 831 | 831 | */ |
| 832 | 832 | function calculer_critere_par_hasard($idb, &$boucles, $crit) { |
| 833 | - $boucle = &$boucles[$idb]; |
|
| 834 | - // Si ce n'est fait, ajouter un champ 'hasard' dans le select |
|
| 835 | - $parha = "rand() AS hasard"; |
|
| 836 | - if (!in_array($parha, $boucle->select)) { |
|
| 837 | - $boucle->select[] = $parha; |
|
| 838 | - } |
|
| 839 | - return "'hasard'"; |
|
| 833 | + $boucle = &$boucles[$idb]; |
|
| 834 | + // Si ce n'est fait, ajouter un champ 'hasard' dans le select |
|
| 835 | + $parha = "rand() AS hasard"; |
|
| 836 | + if (!in_array($parha, $boucle->select)) { |
|
| 837 | + $boucle->select[] = $parha; |
|
| 838 | + } |
|
| 839 | + return "'hasard'"; |
|
| 840 | 840 | } |
| 841 | 841 | |
| 842 | 842 | /** |
@@ -860,20 +860,20 @@ discard block |
||
| 860 | 860 | * @return string Clause pour le Order by |
| 861 | 861 | */ |
| 862 | 862 | function calculer_critere_par_expression_num($idb, &$boucles, $crit, $tri, $champ) { |
| 863 | - $_champ = calculer_critere_par_champ($idb, $boucles, $crit, $champ, true); |
|
| 864 | - if (is_array($_champ)) { |
|
| 865 | - return array('zbug_critere_inconnu', array('critere' => $crit->op . " num $champ")); |
|
| 866 | - } |
|
| 867 | - $boucle = &$boucles[$idb]; |
|
| 868 | - $texte = '0+' . $_champ; |
|
| 869 | - $suite = calculer_liste($tri, array(), $boucles, $boucle->id_parent); |
|
| 870 | - if ($suite !== "''") { |
|
| 871 | - $texte = "\" . ((\$x = $suite) ? ('$texte' . \$x) : '0')" . " . \""; |
|
| 872 | - } |
|
| 873 | - $as = 'num' . ($boucle->order ? count($boucle->order) : ""); |
|
| 874 | - $boucle->select[] = $texte . " AS $as"; |
|
| 875 | - $order = "'$as'"; |
|
| 876 | - return $order; |
|
| 863 | + $_champ = calculer_critere_par_champ($idb, $boucles, $crit, $champ, true); |
|
| 864 | + if (is_array($_champ)) { |
|
| 865 | + return array('zbug_critere_inconnu', array('critere' => $crit->op . " num $champ")); |
|
| 866 | + } |
|
| 867 | + $boucle = &$boucles[$idb]; |
|
| 868 | + $texte = '0+' . $_champ; |
|
| 869 | + $suite = calculer_liste($tri, array(), $boucles, $boucle->id_parent); |
|
| 870 | + if ($suite !== "''") { |
|
| 871 | + $texte = "\" . ((\$x = $suite) ? ('$texte' . \$x) : '0')" . " . \""; |
|
| 872 | + } |
|
| 873 | + $as = 'num' . ($boucle->order ? count($boucle->order) : ""); |
|
| 874 | + $boucle->select[] = $texte . " AS $as"; |
|
| 875 | + $order = "'$as'"; |
|
| 876 | + return $order; |
|
| 877 | 877 | } |
| 878 | 878 | |
| 879 | 879 | /** |
@@ -894,20 +894,20 @@ discard block |
||
| 894 | 894 | * @return string Clause pour le Order by |
| 895 | 895 | */ |
| 896 | 896 | function calculer_critere_par_expression_sinum($idb, &$boucles, $crit, $tri, $champ) { |
| 897 | - $_champ = calculer_critere_par_champ($idb, $boucles, $crit, $champ, true); |
|
| 898 | - if (is_array($_champ)) { |
|
| 899 | - return array('zbug_critere_inconnu', array('critere' => $crit->op . " sinum $champ")); |
|
| 900 | - } |
|
| 901 | - $boucle = &$boucles[$idb]; |
|
| 902 | - $texte = '0+' . $_champ; |
|
| 903 | - $suite = calculer_liste($tri, array(), $boucles, $boucle->id_parent); |
|
| 904 | - if ($suite !== "''") { |
|
| 905 | - $texte = "\" . ((\$x = $suite) ? ('$texte' . \$x) : '0')" . " . \""; |
|
| 906 | - } |
|
| 907 | - $as = 'sinum' . ($boucle->order ? count($boucle->order) : ""); |
|
| 908 | - $boucle->select[] = 'CASE (' . $texte . ') WHEN 0 THEN 1 ELSE 0 END AS ' . $as; |
|
| 909 | - $order = "'$as'"; |
|
| 910 | - return $order; |
|
| 897 | + $_champ = calculer_critere_par_champ($idb, $boucles, $crit, $champ, true); |
|
| 898 | + if (is_array($_champ)) { |
|
| 899 | + return array('zbug_critere_inconnu', array('critere' => $crit->op . " sinum $champ")); |
|
| 900 | + } |
|
| 901 | + $boucle = &$boucles[$idb]; |
|
| 902 | + $texte = '0+' . $_champ; |
|
| 903 | + $suite = calculer_liste($tri, array(), $boucles, $boucle->id_parent); |
|
| 904 | + if ($suite !== "''") { |
|
| 905 | + $texte = "\" . ((\$x = $suite) ? ('$texte' . \$x) : '0')" . " . \""; |
|
| 906 | + } |
|
| 907 | + $as = 'sinum' . ($boucle->order ? count($boucle->order) : ""); |
|
| 908 | + $boucle->select[] = 'CASE (' . $texte . ') WHEN 0 THEN 1 ELSE 0 END AS ' . $as; |
|
| 909 | + $order = "'$as'"; |
|
| 910 | + return $order; |
|
| 911 | 911 | } |
| 912 | 912 | |
| 913 | 913 | |
@@ -927,14 +927,14 @@ discard block |
||
| 927 | 927 | * @return string Clause pour le Order by |
| 928 | 928 | */ |
| 929 | 929 | function calculer_critere_par_expression_multi($idb, &$boucles, $crit, $tri, $champ) { |
| 930 | - $_champ = calculer_critere_par_champ($idb, $boucles, $crit, $champ, true); |
|
| 931 | - if (is_array($_champ)) { |
|
| 932 | - return array('zbug_critere_inconnu', array('critere' => $crit->op . " multi $champ")); |
|
| 933 | - } |
|
| 934 | - $boucle = &$boucles[$idb]; |
|
| 935 | - $boucle->select[] = "\".sql_multi('" . $_champ . "', \$GLOBALS['spip_lang']).\""; |
|
| 936 | - $order = "'multi'"; |
|
| 937 | - return $order; |
|
| 930 | + $_champ = calculer_critere_par_champ($idb, $boucles, $crit, $champ, true); |
|
| 931 | + if (is_array($_champ)) { |
|
| 932 | + return array('zbug_critere_inconnu', array('critere' => $crit->op . " multi $champ")); |
|
| 933 | + } |
|
| 934 | + $boucle = &$boucles[$idb]; |
|
| 935 | + $boucle->select[] = "\".sql_multi('" . $_champ . "', \$GLOBALS['spip_lang']).\""; |
|
| 936 | + $order = "'multi'"; |
|
| 937 | + return $order; |
|
| 938 | 938 | } |
| 939 | 939 | |
| 940 | 940 | /** |
@@ -953,56 +953,56 @@ discard block |
||
| 953 | 953 | * @return array|string |
| 954 | 954 | */ |
| 955 | 955 | function calculer_critere_par_champ($idb, &$boucles, $crit, $par, $raw = false) { |
| 956 | - $boucle = &$boucles[$idb]; |
|
| 957 | - $desc = $boucle->show; |
|
| 958 | - |
|
| 959 | - // le champ existe dans la table, pas de souci (le plus commun) |
|
| 960 | - if (isset($desc['field'][$par])) { |
|
| 961 | - $par = $boucle->id_table . "." . $par; |
|
| 962 | - } |
|
| 963 | - // le champ est peut être une jointure |
|
| 964 | - else { |
|
| 965 | - $table = $table_alias = false; // toutes les tables de jointure possibles |
|
| 966 | - $champ = $par; |
|
| 967 | - |
|
| 968 | - // le champ demandé est une exception de jointure {par titre_mot} |
|
| 969 | - if (isset($GLOBALS['exceptions_des_jointures'][$par])) { |
|
| 970 | - list($table, $champ) = $GLOBALS['exceptions_des_jointures'][$par]; |
|
| 971 | - } // la table de jointure est explicitement indiquée {par truc.muche} |
|
| 972 | - elseif (preg_match("/^([^,]*)\.(.*)$/", $par, $r)) { |
|
| 973 | - list(, $table, $champ) = $r; |
|
| 974 | - $table_alias = $table; // c'est peut-être un alias de table {par L1.titre} |
|
| 975 | - $table = table_objet_sql($table); |
|
| 976 | - } |
|
| 977 | - |
|
| 978 | - // Si on connait la table d'arrivée, on la demande donc explicitement |
|
| 979 | - // Sinon on cherche le champ dans les tables possibles de jointures |
|
| 980 | - // Si la table est déjà dans le from, on la réutilise. |
|
| 981 | - if ($infos = chercher_champ_dans_tables($champ, $boucle->from, $boucle->sql_serveur, $table)) { |
|
| 982 | - $par = $infos['alias'] . "." . $champ; |
|
| 983 | - } elseif ( |
|
| 984 | - $boucle->jointures_explicites |
|
| 985 | - and $alias = trouver_jointure_champ($champ, $boucle, explode(' ', $boucle->jointures_explicites), false, $table) |
|
| 986 | - ) { |
|
| 987 | - $par = $alias . "." . $champ; |
|
| 988 | - } elseif ($alias = trouver_jointure_champ($champ, $boucle, $boucle->jointures, false, $table)) { |
|
| 989 | - $par = $alias . "." . $champ; |
|
| 990 | - // en spécifiant directement l'alias {par L2.titre} (situation hasardeuse tout de même) |
|
| 991 | - } elseif ( |
|
| 992 | - $table_alias |
|
| 993 | - and isset($boucle->from[$table_alias]) |
|
| 994 | - and $infos = chercher_champ_dans_tables($champ, $boucle->from, $boucle->sql_serveur, $boucle->from[$table_alias]) |
|
| 995 | - ) { |
|
| 996 | - $par = $infos['alias'] . "." . $champ; |
|
| 997 | - } elseif ($table) { |
|
| 998 | - // On avait table + champ, mais on ne les a pas trouvés |
|
| 999 | - return array('zbug_critere_inconnu', array('critere' => $crit->op . " $par")); |
|
| 1000 | - } else { |
|
| 1001 | - // Sinon tant pis, ca doit etre un champ synthetise (cf points) |
|
| 1002 | - } |
|
| 1003 | - } |
|
| 1004 | - |
|
| 1005 | - return $raw ? $par : "'$par'"; |
|
| 956 | + $boucle = &$boucles[$idb]; |
|
| 957 | + $desc = $boucle->show; |
|
| 958 | + |
|
| 959 | + // le champ existe dans la table, pas de souci (le plus commun) |
|
| 960 | + if (isset($desc['field'][$par])) { |
|
| 961 | + $par = $boucle->id_table . "." . $par; |
|
| 962 | + } |
|
| 963 | + // le champ est peut être une jointure |
|
| 964 | + else { |
|
| 965 | + $table = $table_alias = false; // toutes les tables de jointure possibles |
|
| 966 | + $champ = $par; |
|
| 967 | + |
|
| 968 | + // le champ demandé est une exception de jointure {par titre_mot} |
|
| 969 | + if (isset($GLOBALS['exceptions_des_jointures'][$par])) { |
|
| 970 | + list($table, $champ) = $GLOBALS['exceptions_des_jointures'][$par]; |
|
| 971 | + } // la table de jointure est explicitement indiquée {par truc.muche} |
|
| 972 | + elseif (preg_match("/^([^,]*)\.(.*)$/", $par, $r)) { |
|
| 973 | + list(, $table, $champ) = $r; |
|
| 974 | + $table_alias = $table; // c'est peut-être un alias de table {par L1.titre} |
|
| 975 | + $table = table_objet_sql($table); |
|
| 976 | + } |
|
| 977 | + |
|
| 978 | + // Si on connait la table d'arrivée, on la demande donc explicitement |
|
| 979 | + // Sinon on cherche le champ dans les tables possibles de jointures |
|
| 980 | + // Si la table est déjà dans le from, on la réutilise. |
|
| 981 | + if ($infos = chercher_champ_dans_tables($champ, $boucle->from, $boucle->sql_serveur, $table)) { |
|
| 982 | + $par = $infos['alias'] . "." . $champ; |
|
| 983 | + } elseif ( |
|
| 984 | + $boucle->jointures_explicites |
|
| 985 | + and $alias = trouver_jointure_champ($champ, $boucle, explode(' ', $boucle->jointures_explicites), false, $table) |
|
| 986 | + ) { |
|
| 987 | + $par = $alias . "." . $champ; |
|
| 988 | + } elseif ($alias = trouver_jointure_champ($champ, $boucle, $boucle->jointures, false, $table)) { |
|
| 989 | + $par = $alias . "." . $champ; |
|
| 990 | + // en spécifiant directement l'alias {par L2.titre} (situation hasardeuse tout de même) |
|
| 991 | + } elseif ( |
|
| 992 | + $table_alias |
|
| 993 | + and isset($boucle->from[$table_alias]) |
|
| 994 | + and $infos = chercher_champ_dans_tables($champ, $boucle->from, $boucle->sql_serveur, $boucle->from[$table_alias]) |
|
| 995 | + ) { |
|
| 996 | + $par = $infos['alias'] . "." . $champ; |
|
| 997 | + } elseif ($table) { |
|
| 998 | + // On avait table + champ, mais on ne les a pas trouvés |
|
| 999 | + return array('zbug_critere_inconnu', array('critere' => $crit->op . " $par")); |
|
| 1000 | + } else { |
|
| 1001 | + // Sinon tant pis, ca doit etre un champ synthetise (cf points) |
|
| 1002 | + } |
|
| 1003 | + } |
|
| 1004 | + |
|
| 1005 | + return $raw ? $par : "'$par'"; |
|
| 1006 | 1006 | } |
| 1007 | 1007 | |
| 1008 | 1008 | /** |
@@ -1016,11 +1016,11 @@ discard block |
||
| 1016 | 1016 | * @return string Champ pour le compilateur si trouvé, tel que "'alias.champ'", sinon vide. |
| 1017 | 1017 | */ |
| 1018 | 1018 | function critere_par_joint($table, $champ, &$boucle) { |
| 1019 | - $t = array_search($table, $boucle->from); |
|
| 1020 | - if (!$t) { |
|
| 1021 | - $t = trouver_jointure_champ($champ, $boucle); |
|
| 1022 | - } |
|
| 1023 | - return !$t ? '' : ("'" . $t . '.' . $champ . "'"); |
|
| 1019 | + $t = array_search($table, $boucle->from); |
|
| 1020 | + if (!$t) { |
|
| 1021 | + $t = trouver_jointure_champ($champ, $boucle); |
|
| 1022 | + } |
|
| 1023 | + return !$t ? '' : ("'" . $t . '.' . $champ . "'"); |
|
| 1024 | 1024 | } |
| 1025 | 1025 | |
| 1026 | 1026 | /** |
@@ -1045,143 +1045,143 @@ discard block |
||
| 1045 | 1045 | */ |
| 1046 | 1046 | function critere_inverse_dist($idb, &$boucles, $crit) { |
| 1047 | 1047 | |
| 1048 | - $boucle = &$boucles[$idb]; |
|
| 1049 | - // Classement par ordre inverse |
|
| 1050 | - if ($crit->not) { |
|
| 1051 | - critere_parinverse($idb, $boucles, $crit); |
|
| 1052 | - } else { |
|
| 1053 | - $order = "' DESC'"; |
|
| 1054 | - // Classement par ordre inverse fonction eventuelle de #ENV{...} |
|
| 1055 | - if (isset($crit->param[0])) { |
|
| 1056 | - $critere = calculer_liste($crit->param[0], array(), $boucles, $boucles[$idb]->id_parent); |
|
| 1057 | - $order = "(($critere)?' DESC':'')"; |
|
| 1058 | - } |
|
| 1059 | - |
|
| 1060 | - $n = count($boucle->order); |
|
| 1061 | - if (!$n) { |
|
| 1062 | - if (isset($boucle->default_order[0])) { |
|
| 1063 | - $boucle->default_order[0] .= ' . " DESC"'; |
|
| 1064 | - } else { |
|
| 1065 | - $boucle->default_order[] = ' DESC'; |
|
| 1066 | - } |
|
| 1067 | - } else { |
|
| 1068 | - $t = $boucle->order[$n - 1] . " . $order"; |
|
| 1069 | - if (preg_match("/^(.*)'\s*\.\s*'([^']*')$/", $t, $r)) { |
|
| 1070 | - $t = $r[1] . $r[2]; |
|
| 1071 | - } |
|
| 1072 | - $boucle->order[$n - 1] = $t; |
|
| 1073 | - } |
|
| 1074 | - } |
|
| 1048 | + $boucle = &$boucles[$idb]; |
|
| 1049 | + // Classement par ordre inverse |
|
| 1050 | + if ($crit->not) { |
|
| 1051 | + critere_parinverse($idb, $boucles, $crit); |
|
| 1052 | + } else { |
|
| 1053 | + $order = "' DESC'"; |
|
| 1054 | + // Classement par ordre inverse fonction eventuelle de #ENV{...} |
|
| 1055 | + if (isset($crit->param[0])) { |
|
| 1056 | + $critere = calculer_liste($crit->param[0], array(), $boucles, $boucles[$idb]->id_parent); |
|
| 1057 | + $order = "(($critere)?' DESC':'')"; |
|
| 1058 | + } |
|
| 1059 | + |
|
| 1060 | + $n = count($boucle->order); |
|
| 1061 | + if (!$n) { |
|
| 1062 | + if (isset($boucle->default_order[0])) { |
|
| 1063 | + $boucle->default_order[0] .= ' . " DESC"'; |
|
| 1064 | + } else { |
|
| 1065 | + $boucle->default_order[] = ' DESC'; |
|
| 1066 | + } |
|
| 1067 | + } else { |
|
| 1068 | + $t = $boucle->order[$n - 1] . " . $order"; |
|
| 1069 | + if (preg_match("/^(.*)'\s*\.\s*'([^']*')$/", $t, $r)) { |
|
| 1070 | + $t = $r[1] . $r[2]; |
|
| 1071 | + } |
|
| 1072 | + $boucle->order[$n - 1] = $t; |
|
| 1073 | + } |
|
| 1074 | + } |
|
| 1075 | 1075 | } |
| 1076 | 1076 | |
| 1077 | 1077 | // http://code.spip.net/@critere_agenda_dist |
| 1078 | 1078 | function critere_agenda_dist($idb, &$boucles, $crit) { |
| 1079 | - $params = $crit->param; |
|
| 1080 | - |
|
| 1081 | - if (count($params) < 1) { |
|
| 1082 | - return array('zbug_critere_inconnu', array('critere' => $crit->op . " ?")); |
|
| 1083 | - } |
|
| 1084 | - |
|
| 1085 | - $boucle = &$boucles[$idb]; |
|
| 1086 | - $parent = $boucle->id_parent; |
|
| 1087 | - $fields = $boucle->show['field']; |
|
| 1088 | - |
|
| 1089 | - $date = array_shift($params); |
|
| 1090 | - $type = array_shift($params); |
|
| 1091 | - |
|
| 1092 | - // la valeur $type doit etre connue a la compilation |
|
| 1093 | - // donc etre forcement reduite a un litteral unique dans le source |
|
| 1094 | - $type = is_object($type[0]) ? $type[0]->texte : null; |
|
| 1095 | - |
|
| 1096 | - // La valeur date doit designer un champ de la table SQL. |
|
| 1097 | - // Si c'est un litteral unique dans le source, verifier a la compil, |
|
| 1098 | - // sinon synthetiser le test de verif pour execution ulterieure |
|
| 1099 | - // On prendra arbitrairement le premier champ si test negatif. |
|
| 1100 | - if ((count($date) == 1) and ($date[0]->type == 'texte')) { |
|
| 1101 | - $date = $date[0]->texte; |
|
| 1102 | - if (!isset($fields[$date])) { |
|
| 1103 | - return array('zbug_critere_inconnu', array('critere' => $crit->op . " " . $date)); |
|
| 1104 | - } |
|
| 1105 | - } else { |
|
| 1106 | - $a = calculer_liste($date, array(), $boucles, $parent); |
|
| 1107 | - $noms = array_keys($fields); |
|
| 1108 | - $defaut = $noms[0]; |
|
| 1109 | - $noms = join(" ", $noms); |
|
| 1110 | - # bien laisser 2 espaces avant $nom pour que strpos<>0 |
|
| 1111 | - $cond = "(\$a=strval($a))AND\nstrpos(\" $noms \",\" \$a \")"; |
|
| 1112 | - $date = "'.(($cond)\n?\$a:\"$defaut\").'"; |
|
| 1113 | - } |
|
| 1114 | - $annee = $params ? array_shift($params) : ""; |
|
| 1115 | - $annee = "\n" . 'sprintf("%04d", ($x = ' . |
|
| 1116 | - calculer_liste($annee, array(), $boucles, $parent) . |
|
| 1117 | - ') ? $x : date("Y"))'; |
|
| 1118 | - |
|
| 1119 | - $mois = $params ? array_shift($params) : ""; |
|
| 1120 | - $mois = "\n" . 'sprintf("%02d", ($x = ' . |
|
| 1121 | - calculer_liste($mois, array(), $boucles, $parent) . |
|
| 1122 | - ') ? $x : date("m"))'; |
|
| 1123 | - |
|
| 1124 | - $jour = $params ? array_shift($params) : ""; |
|
| 1125 | - $jour = "\n" . 'sprintf("%02d", ($x = ' . |
|
| 1126 | - calculer_liste($jour, array(), $boucles, $parent) . |
|
| 1127 | - ') ? $x : date("d"))'; |
|
| 1128 | - |
|
| 1129 | - $annee2 = $params ? array_shift($params) : ""; |
|
| 1130 | - $annee2 = "\n" . 'sprintf("%04d", ($x = ' . |
|
| 1131 | - calculer_liste($annee2, array(), $boucles, $parent) . |
|
| 1132 | - ') ? $x : date("Y"))'; |
|
| 1133 | - |
|
| 1134 | - $mois2 = $params ? array_shift($params) : ""; |
|
| 1135 | - $mois2 = "\n" . 'sprintf("%02d", ($x = ' . |
|
| 1136 | - calculer_liste($mois2, array(), $boucles, $parent) . |
|
| 1137 | - ') ? $x : date("m"))'; |
|
| 1138 | - |
|
| 1139 | - $jour2 = $params ? array_shift($params) : ""; |
|
| 1140 | - $jour2 = "\n" . 'sprintf("%02d", ($x = ' . |
|
| 1141 | - calculer_liste($jour2, array(), $boucles, $parent) . |
|
| 1142 | - ') ? $x : date("d"))'; |
|
| 1143 | - |
|
| 1144 | - $date = $boucle->id_table . ".$date"; |
|
| 1145 | - |
|
| 1146 | - $quote_end = ",'" . $boucle->sql_serveur . "','text'"; |
|
| 1147 | - if ($type == 'jour') { |
|
| 1148 | - $boucle->where[] = array( |
|
| 1149 | - "'='", |
|
| 1150 | - "'DATE_FORMAT($date, \'%Y%m%d\')'", |
|
| 1151 | - ("sql_quote($annee . $mois . $jour$quote_end)") |
|
| 1152 | - ); |
|
| 1153 | - } elseif ($type == 'mois') { |
|
| 1154 | - $boucle->where[] = array( |
|
| 1155 | - "'='", |
|
| 1156 | - "'DATE_FORMAT($date, \'%Y%m\')'", |
|
| 1157 | - ("sql_quote($annee . $mois$quote_end)") |
|
| 1158 | - ); |
|
| 1159 | - } elseif ($type == 'semaine') { |
|
| 1160 | - $boucle->where[] = array( |
|
| 1161 | - "'AND'", |
|
| 1162 | - array( |
|
| 1163 | - "'>='", |
|
| 1164 | - "'DATE_FORMAT($date, \'%Y%m%d\')'", |
|
| 1165 | - ("date_debut_semaine($annee, $mois, $jour)") |
|
| 1166 | - ), |
|
| 1167 | - array( |
|
| 1168 | - "'<='", |
|
| 1169 | - "'DATE_FORMAT($date, \'%Y%m%d\')'", |
|
| 1170 | - ("date_fin_semaine($annee, $mois, $jour)") |
|
| 1171 | - ) |
|
| 1172 | - ); |
|
| 1173 | - } elseif (count($crit->param) > 2) { |
|
| 1174 | - $boucle->where[] = array( |
|
| 1175 | - "'AND'", |
|
| 1176 | - array( |
|
| 1177 | - "'>='", |
|
| 1178 | - "'DATE_FORMAT($date, \'%Y%m%d\')'", |
|
| 1179 | - ("sql_quote($annee . $mois . $jour$quote_end)") |
|
| 1180 | - ), |
|
| 1181 | - array("'<='", "'DATE_FORMAT($date, \'%Y%m%d\')'", ("sql_quote($annee2 . $mois2 . $jour2$quote_end)")) |
|
| 1182 | - ); |
|
| 1183 | - } |
|
| 1184 | - // sinon on prend tout |
|
| 1079 | + $params = $crit->param; |
|
| 1080 | + |
|
| 1081 | + if (count($params) < 1) { |
|
| 1082 | + return array('zbug_critere_inconnu', array('critere' => $crit->op . " ?")); |
|
| 1083 | + } |
|
| 1084 | + |
|
| 1085 | + $boucle = &$boucles[$idb]; |
|
| 1086 | + $parent = $boucle->id_parent; |
|
| 1087 | + $fields = $boucle->show['field']; |
|
| 1088 | + |
|
| 1089 | + $date = array_shift($params); |
|
| 1090 | + $type = array_shift($params); |
|
| 1091 | + |
|
| 1092 | + // la valeur $type doit etre connue a la compilation |
|
| 1093 | + // donc etre forcement reduite a un litteral unique dans le source |
|
| 1094 | + $type = is_object($type[0]) ? $type[0]->texte : null; |
|
| 1095 | + |
|
| 1096 | + // La valeur date doit designer un champ de la table SQL. |
|
| 1097 | + // Si c'est un litteral unique dans le source, verifier a la compil, |
|
| 1098 | + // sinon synthetiser le test de verif pour execution ulterieure |
|
| 1099 | + // On prendra arbitrairement le premier champ si test negatif. |
|
| 1100 | + if ((count($date) == 1) and ($date[0]->type == 'texte')) { |
|
| 1101 | + $date = $date[0]->texte; |
|
| 1102 | + if (!isset($fields[$date])) { |
|
| 1103 | + return array('zbug_critere_inconnu', array('critere' => $crit->op . " " . $date)); |
|
| 1104 | + } |
|
| 1105 | + } else { |
|
| 1106 | + $a = calculer_liste($date, array(), $boucles, $parent); |
|
| 1107 | + $noms = array_keys($fields); |
|
| 1108 | + $defaut = $noms[0]; |
|
| 1109 | + $noms = join(" ", $noms); |
|
| 1110 | + # bien laisser 2 espaces avant $nom pour que strpos<>0 |
|
| 1111 | + $cond = "(\$a=strval($a))AND\nstrpos(\" $noms \",\" \$a \")"; |
|
| 1112 | + $date = "'.(($cond)\n?\$a:\"$defaut\").'"; |
|
| 1113 | + } |
|
| 1114 | + $annee = $params ? array_shift($params) : ""; |
|
| 1115 | + $annee = "\n" . 'sprintf("%04d", ($x = ' . |
|
| 1116 | + calculer_liste($annee, array(), $boucles, $parent) . |
|
| 1117 | + ') ? $x : date("Y"))'; |
|
| 1118 | + |
|
| 1119 | + $mois = $params ? array_shift($params) : ""; |
|
| 1120 | + $mois = "\n" . 'sprintf("%02d", ($x = ' . |
|
| 1121 | + calculer_liste($mois, array(), $boucles, $parent) . |
|
| 1122 | + ') ? $x : date("m"))'; |
|
| 1123 | + |
|
| 1124 | + $jour = $params ? array_shift($params) : ""; |
|
| 1125 | + $jour = "\n" . 'sprintf("%02d", ($x = ' . |
|
| 1126 | + calculer_liste($jour, array(), $boucles, $parent) . |
|
| 1127 | + ') ? $x : date("d"))'; |
|
| 1128 | + |
|
| 1129 | + $annee2 = $params ? array_shift($params) : ""; |
|
| 1130 | + $annee2 = "\n" . 'sprintf("%04d", ($x = ' . |
|
| 1131 | + calculer_liste($annee2, array(), $boucles, $parent) . |
|
| 1132 | + ') ? $x : date("Y"))'; |
|
| 1133 | + |
|
| 1134 | + $mois2 = $params ? array_shift($params) : ""; |
|
| 1135 | + $mois2 = "\n" . 'sprintf("%02d", ($x = ' . |
|
| 1136 | + calculer_liste($mois2, array(), $boucles, $parent) . |
|
| 1137 | + ') ? $x : date("m"))'; |
|
| 1138 | + |
|
| 1139 | + $jour2 = $params ? array_shift($params) : ""; |
|
| 1140 | + $jour2 = "\n" . 'sprintf("%02d", ($x = ' . |
|
| 1141 | + calculer_liste($jour2, array(), $boucles, $parent) . |
|
| 1142 | + ') ? $x : date("d"))'; |
|
| 1143 | + |
|
| 1144 | + $date = $boucle->id_table . ".$date"; |
|
| 1145 | + |
|
| 1146 | + $quote_end = ",'" . $boucle->sql_serveur . "','text'"; |
|
| 1147 | + if ($type == 'jour') { |
|
| 1148 | + $boucle->where[] = array( |
|
| 1149 | + "'='", |
|
| 1150 | + "'DATE_FORMAT($date, \'%Y%m%d\')'", |
|
| 1151 | + ("sql_quote($annee . $mois . $jour$quote_end)") |
|
| 1152 | + ); |
|
| 1153 | + } elseif ($type == 'mois') { |
|
| 1154 | + $boucle->where[] = array( |
|
| 1155 | + "'='", |
|
| 1156 | + "'DATE_FORMAT($date, \'%Y%m\')'", |
|
| 1157 | + ("sql_quote($annee . $mois$quote_end)") |
|
| 1158 | + ); |
|
| 1159 | + } elseif ($type == 'semaine') { |
|
| 1160 | + $boucle->where[] = array( |
|
| 1161 | + "'AND'", |
|
| 1162 | + array( |
|
| 1163 | + "'>='", |
|
| 1164 | + "'DATE_FORMAT($date, \'%Y%m%d\')'", |
|
| 1165 | + ("date_debut_semaine($annee, $mois, $jour)") |
|
| 1166 | + ), |
|
| 1167 | + array( |
|
| 1168 | + "'<='", |
|
| 1169 | + "'DATE_FORMAT($date, \'%Y%m%d\')'", |
|
| 1170 | + ("date_fin_semaine($annee, $mois, $jour)") |
|
| 1171 | + ) |
|
| 1172 | + ); |
|
| 1173 | + } elseif (count($crit->param) > 2) { |
|
| 1174 | + $boucle->where[] = array( |
|
| 1175 | + "'AND'", |
|
| 1176 | + array( |
|
| 1177 | + "'>='", |
|
| 1178 | + "'DATE_FORMAT($date, \'%Y%m%d\')'", |
|
| 1179 | + ("sql_quote($annee . $mois . $jour$quote_end)") |
|
| 1180 | + ), |
|
| 1181 | + array("'<='", "'DATE_FORMAT($date, \'%Y%m%d\')'", ("sql_quote($annee2 . $mois2 . $jour2$quote_end)")) |
|
| 1182 | + ); |
|
| 1183 | + } |
|
| 1184 | + // sinon on prend tout |
|
| 1185 | 1185 | } |
| 1186 | 1186 | |
| 1187 | 1187 | |
@@ -1206,33 +1206,33 @@ discard block |
||
| 1206 | 1206 | * @return void |
| 1207 | 1207 | **/ |
| 1208 | 1208 | function calculer_critere_parties($idb, &$boucles, $crit) { |
| 1209 | - $boucle = &$boucles[$idb]; |
|
| 1210 | - $a1 = $crit->param[0]; |
|
| 1211 | - $a2 = $crit->param[1]; |
|
| 1212 | - $op = $crit->op; |
|
| 1213 | - |
|
| 1214 | - list($a11, $a12) = calculer_critere_parties_aux($idb, $boucles, $a1); |
|
| 1215 | - list($a21, $a22) = calculer_critere_parties_aux($idb, $boucles, $a2); |
|
| 1216 | - |
|
| 1217 | - if (($op == ',') && (is_numeric($a11) && (is_numeric($a21)))) { |
|
| 1218 | - $boucle->limit = $a11 . ',' . $a21; |
|
| 1219 | - } else { |
|
| 1220 | - // 3 dans {1/3}, {2,3} ou {1,n-3} |
|
| 1221 | - $boucle->total_parties = ($a21 != 'n') ? $a21 : $a22; |
|
| 1222 | - // 2 dans {2/3}, {2,5}, {n-2,1} |
|
| 1223 | - $partie = ($a11 != 'n') ? $a11 : $a12; |
|
| 1224 | - $mode = (($op == '/') ? '/' : |
|
| 1225 | - (($a11 == 'n') ? '-' : '+') . (($a21 == 'n') ? '-' : '+')); |
|
| 1226 | - // cas simple {0,#ENV{truc}} compilons le en LIMIT : |
|
| 1227 | - if ($a11 !== 'n' and $a21 !== 'n' and $mode == "++" and $op == ',') { |
|
| 1228 | - $boucle->limit = |
|
| 1229 | - (is_numeric($a11) ? "'$a11'" : $a11) |
|
| 1230 | - . ".','." |
|
| 1231 | - . (is_numeric($a21) ? "'$a21'" : $a21); |
|
| 1232 | - } else { |
|
| 1233 | - calculer_parties($boucles, $idb, $partie, $mode); |
|
| 1234 | - } |
|
| 1235 | - } |
|
| 1209 | + $boucle = &$boucles[$idb]; |
|
| 1210 | + $a1 = $crit->param[0]; |
|
| 1211 | + $a2 = $crit->param[1]; |
|
| 1212 | + $op = $crit->op; |
|
| 1213 | + |
|
| 1214 | + list($a11, $a12) = calculer_critere_parties_aux($idb, $boucles, $a1); |
|
| 1215 | + list($a21, $a22) = calculer_critere_parties_aux($idb, $boucles, $a2); |
|
| 1216 | + |
|
| 1217 | + if (($op == ',') && (is_numeric($a11) && (is_numeric($a21)))) { |
|
| 1218 | + $boucle->limit = $a11 . ',' . $a21; |
|
| 1219 | + } else { |
|
| 1220 | + // 3 dans {1/3}, {2,3} ou {1,n-3} |
|
| 1221 | + $boucle->total_parties = ($a21 != 'n') ? $a21 : $a22; |
|
| 1222 | + // 2 dans {2/3}, {2,5}, {n-2,1} |
|
| 1223 | + $partie = ($a11 != 'n') ? $a11 : $a12; |
|
| 1224 | + $mode = (($op == '/') ? '/' : |
|
| 1225 | + (($a11 == 'n') ? '-' : '+') . (($a21 == 'n') ? '-' : '+')); |
|
| 1226 | + // cas simple {0,#ENV{truc}} compilons le en LIMIT : |
|
| 1227 | + if ($a11 !== 'n' and $a21 !== 'n' and $mode == "++" and $op == ',') { |
|
| 1228 | + $boucle->limit = |
|
| 1229 | + (is_numeric($a11) ? "'$a11'" : $a11) |
|
| 1230 | + . ".','." |
|
| 1231 | + . (is_numeric($a21) ? "'$a21'" : $a21); |
|
| 1232 | + } else { |
|
| 1233 | + calculer_parties($boucles, $idb, $partie, $mode); |
|
| 1234 | + } |
|
| 1235 | + } |
|
| 1236 | 1236 | } |
| 1237 | 1237 | |
| 1238 | 1238 | /** |
@@ -1260,63 +1260,63 @@ discard block |
||
| 1260 | 1260 | * @return void |
| 1261 | 1261 | **/ |
| 1262 | 1262 | function calculer_parties(&$boucles, $id_boucle, $debut, $mode) { |
| 1263 | - $total_parties = $boucles[$id_boucle]->total_parties; |
|
| 1264 | - |
|
| 1265 | - preg_match(",([+-/p])([+-/])?,", $mode, $regs); |
|
| 1266 | - list(, $op1, $op2) = array_pad($regs, 3, null); |
|
| 1267 | - $nombre_boucle = "\$Numrows['$id_boucle']['total']"; |
|
| 1268 | - // {1/3} |
|
| 1269 | - if ($op1 == '/') { |
|
| 1270 | - $pmoins1 = is_numeric($debut) ? ($debut - 1) : "($debut-1)"; |
|
| 1271 | - $totpos = is_numeric($total_parties) ? ($total_parties) : |
|
| 1272 | - "($total_parties ? $total_parties : 1)"; |
|
| 1273 | - $fin = "ceil(($nombre_boucle * $debut )/$totpos) - 1"; |
|
| 1274 | - $debut = !$pmoins1 ? 0 : "ceil(($nombre_boucle * $pmoins1)/$totpos);"; |
|
| 1275 | - } else { |
|
| 1276 | - // cas {n-1,x} |
|
| 1277 | - if ($op1 == '-') { |
|
| 1278 | - $debut = "$nombre_boucle - $debut;"; |
|
| 1279 | - } |
|
| 1280 | - |
|
| 1281 | - // cas {x,n-1} |
|
| 1282 | - if ($op2 == '-') { |
|
| 1283 | - $fin = '$debut_boucle + ' . $nombre_boucle . ' - ' |
|
| 1284 | - . (is_numeric($total_parties) ? ($total_parties + 1) : |
|
| 1285 | - ($total_parties . ' - 1')); |
|
| 1286 | - } else { |
|
| 1287 | - // {x,1} ou {pagination} |
|
| 1288 | - $fin = '$debut_boucle' |
|
| 1289 | - . (is_numeric($total_parties) ? |
|
| 1290 | - (($total_parties == 1) ? "" : (' + ' . ($total_parties - 1))) : |
|
| 1291 | - ('+' . $total_parties . ' - 1')); |
|
| 1292 | - } |
|
| 1293 | - |
|
| 1294 | - // {pagination}, gerer le debut_xx=-1 pour tout voir |
|
| 1295 | - if ($op1 == 'p') { |
|
| 1296 | - $debut .= ";\n \$debut_boucle = ((\$tout=(\$debut_boucle == -1))?0:(\$debut_boucle))"; |
|
| 1297 | - $debut .= ";\n \$debut_boucle = max(0,min(\$debut_boucle,floor(($nombre_boucle-1)/($total_parties))*($total_parties)))"; |
|
| 1298 | - $fin = "(\$tout ? $nombre_boucle : $fin)"; |
|
| 1299 | - } |
|
| 1300 | - } |
|
| 1301 | - |
|
| 1302 | - // Notes : |
|
| 1303 | - // $debut_boucle et $fin_boucle sont les indices SQL du premier |
|
| 1304 | - // et du dernier demandes dans la boucle : 0 pour le premier, |
|
| 1305 | - // n-1 pour le dernier ; donc total_boucle = 1 + debut - fin |
|
| 1306 | - // Utiliser min pour rabattre $fin_boucle sur total_boucle. |
|
| 1307 | - |
|
| 1308 | - $boucles[$id_boucle]->mode_partie = "\n\t" |
|
| 1309 | - . '$debut_boucle = ' . $debut . ";\n " |
|
| 1310 | - . "\$debut_boucle = intval(\$debut_boucle);\n " |
|
| 1311 | - . '$fin_boucle = min(' . $fin . ", \$Numrows['$id_boucle']['total'] - 1);\n " |
|
| 1312 | - . '$Numrows[\'' . $id_boucle . "']['grand_total'] = \$Numrows['$id_boucle']['total'];\n " |
|
| 1313 | - . '$Numrows[\'' . $id_boucle . '\']["total"] = max(0,$fin_boucle - $debut_boucle + 1);' |
|
| 1314 | - . "\n\tif (\$debut_boucle>0" |
|
| 1315 | - . " AND \$debut_boucle < \$Numrows['$id_boucle']['grand_total']" |
|
| 1316 | - . " AND \$iter->seek(\$debut_boucle,'continue'))" |
|
| 1317 | - . "\n\t\t\$Numrows['$id_boucle']['compteur_boucle'] = \$debut_boucle;\n\t"; |
|
| 1318 | - |
|
| 1319 | - $boucles[$id_boucle]->partie = " |
|
| 1263 | + $total_parties = $boucles[$id_boucle]->total_parties; |
|
| 1264 | + |
|
| 1265 | + preg_match(",([+-/p])([+-/])?,", $mode, $regs); |
|
| 1266 | + list(, $op1, $op2) = array_pad($regs, 3, null); |
|
| 1267 | + $nombre_boucle = "\$Numrows['$id_boucle']['total']"; |
|
| 1268 | + // {1/3} |
|
| 1269 | + if ($op1 == '/') { |
|
| 1270 | + $pmoins1 = is_numeric($debut) ? ($debut - 1) : "($debut-1)"; |
|
| 1271 | + $totpos = is_numeric($total_parties) ? ($total_parties) : |
|
| 1272 | + "($total_parties ? $total_parties : 1)"; |
|
| 1273 | + $fin = "ceil(($nombre_boucle * $debut )/$totpos) - 1"; |
|
| 1274 | + $debut = !$pmoins1 ? 0 : "ceil(($nombre_boucle * $pmoins1)/$totpos);"; |
|
| 1275 | + } else { |
|
| 1276 | + // cas {n-1,x} |
|
| 1277 | + if ($op1 == '-') { |
|
| 1278 | + $debut = "$nombre_boucle - $debut;"; |
|
| 1279 | + } |
|
| 1280 | + |
|
| 1281 | + // cas {x,n-1} |
|
| 1282 | + if ($op2 == '-') { |
|
| 1283 | + $fin = '$debut_boucle + ' . $nombre_boucle . ' - ' |
|
| 1284 | + . (is_numeric($total_parties) ? ($total_parties + 1) : |
|
| 1285 | + ($total_parties . ' - 1')); |
|
| 1286 | + } else { |
|
| 1287 | + // {x,1} ou {pagination} |
|
| 1288 | + $fin = '$debut_boucle' |
|
| 1289 | + . (is_numeric($total_parties) ? |
|
| 1290 | + (($total_parties == 1) ? "" : (' + ' . ($total_parties - 1))) : |
|
| 1291 | + ('+' . $total_parties . ' - 1')); |
|
| 1292 | + } |
|
| 1293 | + |
|
| 1294 | + // {pagination}, gerer le debut_xx=-1 pour tout voir |
|
| 1295 | + if ($op1 == 'p') { |
|
| 1296 | + $debut .= ";\n \$debut_boucle = ((\$tout=(\$debut_boucle == -1))?0:(\$debut_boucle))"; |
|
| 1297 | + $debut .= ";\n \$debut_boucle = max(0,min(\$debut_boucle,floor(($nombre_boucle-1)/($total_parties))*($total_parties)))"; |
|
| 1298 | + $fin = "(\$tout ? $nombre_boucle : $fin)"; |
|
| 1299 | + } |
|
| 1300 | + } |
|
| 1301 | + |
|
| 1302 | + // Notes : |
|
| 1303 | + // $debut_boucle et $fin_boucle sont les indices SQL du premier |
|
| 1304 | + // et du dernier demandes dans la boucle : 0 pour le premier, |
|
| 1305 | + // n-1 pour le dernier ; donc total_boucle = 1 + debut - fin |
|
| 1306 | + // Utiliser min pour rabattre $fin_boucle sur total_boucle. |
|
| 1307 | + |
|
| 1308 | + $boucles[$id_boucle]->mode_partie = "\n\t" |
|
| 1309 | + . '$debut_boucle = ' . $debut . ";\n " |
|
| 1310 | + . "\$debut_boucle = intval(\$debut_boucle);\n " |
|
| 1311 | + . '$fin_boucle = min(' . $fin . ", \$Numrows['$id_boucle']['total'] - 1);\n " |
|
| 1312 | + . '$Numrows[\'' . $id_boucle . "']['grand_total'] = \$Numrows['$id_boucle']['total'];\n " |
|
| 1313 | + . '$Numrows[\'' . $id_boucle . '\']["total"] = max(0,$fin_boucle - $debut_boucle + 1);' |
|
| 1314 | + . "\n\tif (\$debut_boucle>0" |
|
| 1315 | + . " AND \$debut_boucle < \$Numrows['$id_boucle']['grand_total']" |
|
| 1316 | + . " AND \$iter->seek(\$debut_boucle,'continue'))" |
|
| 1317 | + . "\n\t\t\$Numrows['$id_boucle']['compteur_boucle'] = \$debut_boucle;\n\t"; |
|
| 1318 | + |
|
| 1319 | + $boucles[$id_boucle]->partie = " |
|
| 1320 | 1320 | if (\$Numrows['$id_boucle']['compteur_boucle'] <= \$debut_boucle) continue; |
| 1321 | 1321 | if (\$Numrows['$id_boucle']['compteur_boucle']-1 > \$fin_boucle) break;"; |
| 1322 | 1322 | } |
@@ -1333,26 +1333,26 @@ discard block |
||
| 1333 | 1333 | * @return array Valeur de l'élément (peut être une expression PHP), Nombre soustrait |
| 1334 | 1334 | **/ |
| 1335 | 1335 | function calculer_critere_parties_aux($idb, &$boucles, $param) { |
| 1336 | - if ($param[0]->type != 'texte') { |
|
| 1337 | - $a1 = calculer_liste(array($param[0]), array('id_mere' => $idb), $boucles, $boucles[$idb]->id_parent); |
|
| 1338 | - if (isset($param[1]->texte)) { |
|
| 1339 | - preg_match(',^ *(-([0-9]+))? *$,', $param[1]->texte, $m); |
|
| 1340 | - |
|
| 1341 | - return array("intval($a1)", ((isset($m[2]) and $m[2]) ? $m[2] : 0)); |
|
| 1342 | - } else { |
|
| 1343 | - return array("intval($a1)", 0); |
|
| 1344 | - } |
|
| 1345 | - } else { |
|
| 1346 | - preg_match(',^ *(([0-9]+)|n) *(- *([0-9]+)? *)?$,', $param[0]->texte, $m); |
|
| 1347 | - $a1 = $m[1]; |
|
| 1348 | - if (empty($m[3])) { |
|
| 1349 | - return array($a1, 0); |
|
| 1350 | - } elseif (!empty($m[4])) { |
|
| 1351 | - return array($a1, $m[4]); |
|
| 1352 | - } else { |
|
| 1353 | - return array($a1, calculer_liste(array($param[1]), array(), $boucles, $boucles[$idb]->id_parent)); |
|
| 1354 | - } |
|
| 1355 | - } |
|
| 1336 | + if ($param[0]->type != 'texte') { |
|
| 1337 | + $a1 = calculer_liste(array($param[0]), array('id_mere' => $idb), $boucles, $boucles[$idb]->id_parent); |
|
| 1338 | + if (isset($param[1]->texte)) { |
|
| 1339 | + preg_match(',^ *(-([0-9]+))? *$,', $param[1]->texte, $m); |
|
| 1340 | + |
|
| 1341 | + return array("intval($a1)", ((isset($m[2]) and $m[2]) ? $m[2] : 0)); |
|
| 1342 | + } else { |
|
| 1343 | + return array("intval($a1)", 0); |
|
| 1344 | + } |
|
| 1345 | + } else { |
|
| 1346 | + preg_match(',^ *(([0-9]+)|n) *(- *([0-9]+)? *)?$,', $param[0]->texte, $m); |
|
| 1347 | + $a1 = $m[1]; |
|
| 1348 | + if (empty($m[3])) { |
|
| 1349 | + return array($a1, 0); |
|
| 1350 | + } elseif (!empty($m[4])) { |
|
| 1351 | + return array($a1, $m[4]); |
|
| 1352 | + } else { |
|
| 1353 | + return array($a1, calculer_liste(array($param[1]), array(), $boucles, $boucles[$idb]->id_parent)); |
|
| 1354 | + } |
|
| 1355 | + } |
|
| 1356 | 1356 | } |
| 1357 | 1357 | |
| 1358 | 1358 | |
@@ -1379,47 +1379,47 @@ discard block |
||
| 1379 | 1379 | * array : Erreur sur un des critères |
| 1380 | 1380 | **/ |
| 1381 | 1381 | function calculer_criteres($idb, &$boucles) { |
| 1382 | - $msg = ''; |
|
| 1383 | - $boucle = $boucles[$idb]; |
|
| 1384 | - $table = strtoupper($boucle->type_requete); |
|
| 1385 | - $serveur = strtolower($boucle->sql_serveur); |
|
| 1386 | - |
|
| 1387 | - $defaut = charger_fonction('DEFAUT', 'calculer_critere'); |
|
| 1388 | - // s'il y avait une erreur de syntaxe, propager cette info |
|
| 1389 | - if (!is_array($boucle->criteres)) { |
|
| 1390 | - return array(); |
|
| 1391 | - } |
|
| 1392 | - |
|
| 1393 | - foreach ($boucle->criteres as $crit) { |
|
| 1394 | - $critere = $crit->op; |
|
| 1395 | - // critere personnalise ? |
|
| 1396 | - if ( |
|
| 1397 | - (!$serveur or |
|
| 1398 | - ((!function_exists($f = "critere_" . $serveur . "_" . $table . "_" . $critere)) |
|
| 1399 | - and (!function_exists($f = $f . "_dist")) |
|
| 1400 | - and (!function_exists($f = "critere_" . $serveur . "_" . $critere)) |
|
| 1401 | - and (!function_exists($f = $f . "_dist")) |
|
| 1402 | - ) |
|
| 1403 | - ) |
|
| 1404 | - and (!function_exists($f = "critere_" . $table . "_" . $critere)) |
|
| 1405 | - and (!function_exists($f = $f . "_dist")) |
|
| 1406 | - and (!function_exists($f = "critere_" . $critere)) |
|
| 1407 | - and (!function_exists($f = $f . "_dist")) |
|
| 1408 | - ) { |
|
| 1409 | - // fonction critere standard |
|
| 1410 | - $f = $defaut; |
|
| 1411 | - } |
|
| 1412 | - // compile le critere |
|
| 1413 | - $res = $f($idb, $boucles, $crit); |
|
| 1414 | - |
|
| 1415 | - // Gestion centralisee des erreurs pour pouvoir propager |
|
| 1416 | - if (is_array($res)) { |
|
| 1417 | - $msg = $res; |
|
| 1418 | - erreur_squelette($msg, $boucle); |
|
| 1419 | - } |
|
| 1420 | - } |
|
| 1421 | - |
|
| 1422 | - return $msg; |
|
| 1382 | + $msg = ''; |
|
| 1383 | + $boucle = $boucles[$idb]; |
|
| 1384 | + $table = strtoupper($boucle->type_requete); |
|
| 1385 | + $serveur = strtolower($boucle->sql_serveur); |
|
| 1386 | + |
|
| 1387 | + $defaut = charger_fonction('DEFAUT', 'calculer_critere'); |
|
| 1388 | + // s'il y avait une erreur de syntaxe, propager cette info |
|
| 1389 | + if (!is_array($boucle->criteres)) { |
|
| 1390 | + return array(); |
|
| 1391 | + } |
|
| 1392 | + |
|
| 1393 | + foreach ($boucle->criteres as $crit) { |
|
| 1394 | + $critere = $crit->op; |
|
| 1395 | + // critere personnalise ? |
|
| 1396 | + if ( |
|
| 1397 | + (!$serveur or |
|
| 1398 | + ((!function_exists($f = "critere_" . $serveur . "_" . $table . "_" . $critere)) |
|
| 1399 | + and (!function_exists($f = $f . "_dist")) |
|
| 1400 | + and (!function_exists($f = "critere_" . $serveur . "_" . $critere)) |
|
| 1401 | + and (!function_exists($f = $f . "_dist")) |
|
| 1402 | + ) |
|
| 1403 | + ) |
|
| 1404 | + and (!function_exists($f = "critere_" . $table . "_" . $critere)) |
|
| 1405 | + and (!function_exists($f = $f . "_dist")) |
|
| 1406 | + and (!function_exists($f = "critere_" . $critere)) |
|
| 1407 | + and (!function_exists($f = $f . "_dist")) |
|
| 1408 | + ) { |
|
| 1409 | + // fonction critere standard |
|
| 1410 | + $f = $defaut; |
|
| 1411 | + } |
|
| 1412 | + // compile le critere |
|
| 1413 | + $res = $f($idb, $boucles, $crit); |
|
| 1414 | + |
|
| 1415 | + // Gestion centralisee des erreurs pour pouvoir propager |
|
| 1416 | + if (is_array($res)) { |
|
| 1417 | + $msg = $res; |
|
| 1418 | + erreur_squelette($msg, $boucle); |
|
| 1419 | + } |
|
| 1420 | + } |
|
| 1421 | + |
|
| 1422 | + return $msg; |
|
| 1423 | 1423 | } |
| 1424 | 1424 | |
| 1425 | 1425 | /** |
@@ -1436,11 +1436,11 @@ discard block |
||
| 1436 | 1436 | * @return string Code compilé rééchappé |
| 1437 | 1437 | */ |
| 1438 | 1438 | function kwote($lisp, $serveur = '', $type = '') { |
| 1439 | - if (preg_match(_CODE_QUOTE, $lisp, $r)) { |
|
| 1440 | - return $r[1] . "\"" . sql_quote(str_replace(array("\\'", "\\\\"), array("'", "\\"), $r[2]), $serveur, $type) . "\""; |
|
| 1441 | - } else { |
|
| 1442 | - return "sql_quote($lisp, '$serveur', '" . str_replace("'", "\\'", $type) . "')"; |
|
| 1443 | - } |
|
| 1439 | + if (preg_match(_CODE_QUOTE, $lisp, $r)) { |
|
| 1440 | + return $r[1] . "\"" . sql_quote(str_replace(array("\\'", "\\\\"), array("'", "\\"), $r[2]), $serveur, $type) . "\""; |
|
| 1441 | + } else { |
|
| 1442 | + return "sql_quote($lisp, '$serveur', '" . str_replace("'", "\\'", $type) . "')"; |
|
| 1443 | + } |
|
| 1444 | 1444 | } |
| 1445 | 1445 | |
| 1446 | 1446 | |
@@ -1459,85 +1459,85 @@ discard block |
||
| 1459 | 1459 | * @return void |
| 1460 | 1460 | **/ |
| 1461 | 1461 | function critere_IN_dist($idb, &$boucles, $crit) { |
| 1462 | - $r = calculer_critere_infixe($idb, $boucles, $crit); |
|
| 1463 | - if (!$r) { |
|
| 1464 | - return (array('zbug_critere_inconnu', array('critere' => $crit->op . " ?"))); |
|
| 1465 | - } |
|
| 1466 | - list($arg, $op, $val, $col, $where_complement) = $r; |
|
| 1467 | - |
|
| 1468 | - $in = critere_IN_cas($idb, $boucles, $crit->not ? 'NOT' : ($crit->exclus ? 'exclus' : ''), $arg, $op, $val, $col); |
|
| 1469 | - |
|
| 1470 | - // inserer la condition; exemple: {id_mot ?IN (66, 62, 64)} |
|
| 1471 | - $where = $in; |
|
| 1472 | - if ($crit->cond) { |
|
| 1473 | - $pred = calculer_argument_precedent($idb, $col, $boucles); |
|
| 1474 | - $where = array("'?'", $pred, $where, "''"); |
|
| 1475 | - if ($where_complement) // condition annexe du type "AND (objet='article')" |
|
| 1476 | - { |
|
| 1477 | - $where_complement = array("'?'", $pred, $where_complement, "''"); |
|
| 1478 | - } |
|
| 1479 | - } |
|
| 1480 | - if ($crit->exclus) { |
|
| 1481 | - if (!preg_match(",^L[0-9]+[.],", $arg)) { |
|
| 1482 | - $where = array("'NOT'", $where); |
|
| 1483 | - } else |
|
| 1484 | - // un not sur un critere de jointure se traduit comme un NOT IN avec une sous requete |
|
| 1485 | - // c'est une sous requete identique a la requete principale sous la forme (SELF,$select,$where) avec $select et $where qui surchargent |
|
| 1486 | - { |
|
| 1487 | - $where = array( |
|
| 1488 | - "'NOT'", |
|
| 1489 | - array( |
|
| 1490 | - "'IN'", |
|
| 1491 | - "'" . $boucles[$idb]->id_table . "." . $boucles[$idb]->primary . "'", |
|
| 1492 | - array("'SELF'", "'" . $boucles[$idb]->id_table . "." . $boucles[$idb]->primary . "'", $where) |
|
| 1493 | - ) |
|
| 1494 | - ); |
|
| 1495 | - } |
|
| 1496 | - } |
|
| 1497 | - |
|
| 1498 | - $boucles[$idb]->where[] = $where; |
|
| 1499 | - if ($where_complement) // condition annexe du type "AND (objet='article')" |
|
| 1500 | - { |
|
| 1501 | - $boucles[$idb]->where[] = $where_complement; |
|
| 1502 | - } |
|
| 1462 | + $r = calculer_critere_infixe($idb, $boucles, $crit); |
|
| 1463 | + if (!$r) { |
|
| 1464 | + return (array('zbug_critere_inconnu', array('critere' => $crit->op . " ?"))); |
|
| 1465 | + } |
|
| 1466 | + list($arg, $op, $val, $col, $where_complement) = $r; |
|
| 1467 | + |
|
| 1468 | + $in = critere_IN_cas($idb, $boucles, $crit->not ? 'NOT' : ($crit->exclus ? 'exclus' : ''), $arg, $op, $val, $col); |
|
| 1469 | + |
|
| 1470 | + // inserer la condition; exemple: {id_mot ?IN (66, 62, 64)} |
|
| 1471 | + $where = $in; |
|
| 1472 | + if ($crit->cond) { |
|
| 1473 | + $pred = calculer_argument_precedent($idb, $col, $boucles); |
|
| 1474 | + $where = array("'?'", $pred, $where, "''"); |
|
| 1475 | + if ($where_complement) // condition annexe du type "AND (objet='article')" |
|
| 1476 | + { |
|
| 1477 | + $where_complement = array("'?'", $pred, $where_complement, "''"); |
|
| 1478 | + } |
|
| 1479 | + } |
|
| 1480 | + if ($crit->exclus) { |
|
| 1481 | + if (!preg_match(",^L[0-9]+[.],", $arg)) { |
|
| 1482 | + $where = array("'NOT'", $where); |
|
| 1483 | + } else |
|
| 1484 | + // un not sur un critere de jointure se traduit comme un NOT IN avec une sous requete |
|
| 1485 | + // c'est une sous requete identique a la requete principale sous la forme (SELF,$select,$where) avec $select et $where qui surchargent |
|
| 1486 | + { |
|
| 1487 | + $where = array( |
|
| 1488 | + "'NOT'", |
|
| 1489 | + array( |
|
| 1490 | + "'IN'", |
|
| 1491 | + "'" . $boucles[$idb]->id_table . "." . $boucles[$idb]->primary . "'", |
|
| 1492 | + array("'SELF'", "'" . $boucles[$idb]->id_table . "." . $boucles[$idb]->primary . "'", $where) |
|
| 1493 | + ) |
|
| 1494 | + ); |
|
| 1495 | + } |
|
| 1496 | + } |
|
| 1497 | + |
|
| 1498 | + $boucles[$idb]->where[] = $where; |
|
| 1499 | + if ($where_complement) // condition annexe du type "AND (objet='article')" |
|
| 1500 | + { |
|
| 1501 | + $boucles[$idb]->where[] = $where_complement; |
|
| 1502 | + } |
|
| 1503 | 1503 | } |
| 1504 | 1504 | |
| 1505 | 1505 | // http://code.spip.net/@critere_IN_cas |
| 1506 | 1506 | function critere_IN_cas($idb, &$boucles, $crit2, $arg, $op, $val, $col) { |
| 1507 | - static $num = array(); |
|
| 1508 | - $descr = $boucles[$idb]->descr; |
|
| 1509 | - $cpt = &$num[$descr['nom']][$descr['gram']][$idb]; |
|
| 1510 | - |
|
| 1511 | - $var = '$in' . $cpt++; |
|
| 1512 | - $x = "\n\t$var = array();"; |
|
| 1513 | - foreach ($val as $k => $v) { |
|
| 1514 | - if (preg_match(",^(\n//.*\n)?'(.*)'$,", $v, $r)) { |
|
| 1515 | - // optimiser le traitement des constantes |
|
| 1516 | - if (is_numeric($r[2])) { |
|
| 1517 | - $x .= "\n\t$var" . "[]= $r[2];"; |
|
| 1518 | - } else { |
|
| 1519 | - $x .= "\n\t$var" . "[]= " . sql_quote($r[2]) . ";"; |
|
| 1520 | - } |
|
| 1521 | - } else { |
|
| 1522 | - // Pour permettre de passer des tableaux de valeurs |
|
| 1523 | - // on repere l'utilisation brute de #ENV**{X}, |
|
| 1524 | - // c'est-a-dire sa traduction en ($PILE[0][X]). |
|
| 1525 | - // et on deballe mais en rajoutant l'anti XSS |
|
| 1526 | - $x .= "\n\tif (!(is_array(\$a = ($v))))\n\t\t$var" . "[]= \$a;\n\telse $var = array_merge($var, \$a);"; |
|
| 1527 | - } |
|
| 1528 | - } |
|
| 1529 | - |
|
| 1530 | - $boucles[$idb]->in .= $x; |
|
| 1531 | - |
|
| 1532 | - // inserer le tri par defaut selon les ordres du IN ... |
|
| 1533 | - // avec une ecriture de type FIELD qui degrade les performances (du meme ordre qu'un regexp) |
|
| 1534 | - // et que l'on limite donc strictement aux cas necessaires : |
|
| 1535 | - // si ce n'est pas un !IN, et si il n'y a pas d'autre order dans la boucle |
|
| 1536 | - if (!$crit2) { |
|
| 1537 | - $boucles[$idb]->default_order[] = "((!\$zqv=sql_quote($var) OR \$zqv===\"''\") ? 0 : ('FIELD($arg,' . \$zqv . ')'))"; |
|
| 1538 | - } |
|
| 1539 | - |
|
| 1540 | - return "sql_in('$arg', $var" . ($crit2 == 'NOT' ? ",'NOT'" : "") . ")"; |
|
| 1507 | + static $num = array(); |
|
| 1508 | + $descr = $boucles[$idb]->descr; |
|
| 1509 | + $cpt = &$num[$descr['nom']][$descr['gram']][$idb]; |
|
| 1510 | + |
|
| 1511 | + $var = '$in' . $cpt++; |
|
| 1512 | + $x = "\n\t$var = array();"; |
|
| 1513 | + foreach ($val as $k => $v) { |
|
| 1514 | + if (preg_match(",^(\n//.*\n)?'(.*)'$,", $v, $r)) { |
|
| 1515 | + // optimiser le traitement des constantes |
|
| 1516 | + if (is_numeric($r[2])) { |
|
| 1517 | + $x .= "\n\t$var" . "[]= $r[2];"; |
|
| 1518 | + } else { |
|
| 1519 | + $x .= "\n\t$var" . "[]= " . sql_quote($r[2]) . ";"; |
|
| 1520 | + } |
|
| 1521 | + } else { |
|
| 1522 | + // Pour permettre de passer des tableaux de valeurs |
|
| 1523 | + // on repere l'utilisation brute de #ENV**{X}, |
|
| 1524 | + // c'est-a-dire sa traduction en ($PILE[0][X]). |
|
| 1525 | + // et on deballe mais en rajoutant l'anti XSS |
|
| 1526 | + $x .= "\n\tif (!(is_array(\$a = ($v))))\n\t\t$var" . "[]= \$a;\n\telse $var = array_merge($var, \$a);"; |
|
| 1527 | + } |
|
| 1528 | + } |
|
| 1529 | + |
|
| 1530 | + $boucles[$idb]->in .= $x; |
|
| 1531 | + |
|
| 1532 | + // inserer le tri par defaut selon les ordres du IN ... |
|
| 1533 | + // avec une ecriture de type FIELD qui degrade les performances (du meme ordre qu'un regexp) |
|
| 1534 | + // et que l'on limite donc strictement aux cas necessaires : |
|
| 1535 | + // si ce n'est pas un !IN, et si il n'y a pas d'autre order dans la boucle |
|
| 1536 | + if (!$crit2) { |
|
| 1537 | + $boucles[$idb]->default_order[] = "((!\$zqv=sql_quote($var) OR \$zqv===\"''\") ? 0 : ('FIELD($arg,' . \$zqv . ')'))"; |
|
| 1538 | + } |
|
| 1539 | + |
|
| 1540 | + return "sql_in('$arg', $var" . ($crit2 == 'NOT' ? ",'NOT'" : "") . ")"; |
|
| 1541 | 1541 | } |
| 1542 | 1542 | |
| 1543 | 1543 | /** |
@@ -1553,22 +1553,22 @@ discard block |
||
| 1553 | 1553 | * @return void |
| 1554 | 1554 | */ |
| 1555 | 1555 | function critere_where_dist($idb, &$boucles, $crit) { |
| 1556 | - $boucle = &$boucles[$idb]; |
|
| 1557 | - if (isset($crit->param[0])) { |
|
| 1558 | - $_where = calculer_liste($crit->param[0], array(), $boucles, $boucle->id_parent); |
|
| 1559 | - } else { |
|
| 1560 | - $_where = '@$Pile[0]["where"]'; |
|
| 1561 | - } |
|
| 1562 | - |
|
| 1563 | - if ($crit->cond) { |
|
| 1564 | - $_where = "(($_where) ? ($_where) : '')"; |
|
| 1565 | - } |
|
| 1566 | - |
|
| 1567 | - if ($crit->not) { |
|
| 1568 | - $_where = "array('NOT',$_where)"; |
|
| 1569 | - } |
|
| 1570 | - |
|
| 1571 | - $boucle->where[] = $_where; |
|
| 1556 | + $boucle = &$boucles[$idb]; |
|
| 1557 | + if (isset($crit->param[0])) { |
|
| 1558 | + $_where = calculer_liste($crit->param[0], array(), $boucles, $boucle->id_parent); |
|
| 1559 | + } else { |
|
| 1560 | + $_where = '@$Pile[0]["where"]'; |
|
| 1561 | + } |
|
| 1562 | + |
|
| 1563 | + if ($crit->cond) { |
|
| 1564 | + $_where = "(($_where) ? ($_where) : '')"; |
|
| 1565 | + } |
|
| 1566 | + |
|
| 1567 | + if ($crit->not) { |
|
| 1568 | + $_where = "array('NOT',$_where)"; |
|
| 1569 | + } |
|
| 1570 | + |
|
| 1571 | + $boucle->where[] = $_where; |
|
| 1572 | 1572 | } |
| 1573 | 1573 | |
| 1574 | 1574 | /** |
@@ -1596,31 +1596,31 @@ discard block |
||
| 1596 | 1596 | * @return void |
| 1597 | 1597 | */ |
| 1598 | 1598 | function critere_id__dist($idb, &$boucles, $crit) { |
| 1599 | - /** @var Boucle $boucle */ |
|
| 1600 | - $boucle = $boucles[$idb]; |
|
| 1601 | - |
|
| 1602 | - $champs = lister_champs_selection_conditionnelle( |
|
| 1603 | - $boucle->show['table'], |
|
| 1604 | - $boucle->show, |
|
| 1605 | - $boucle->sql_serveur |
|
| 1606 | - ); |
|
| 1607 | - |
|
| 1608 | - // ne pas tenir compte des critères identiques déjà présents. |
|
| 1609 | - if (!empty($boucle->modificateur['criteres'])) { |
|
| 1610 | - $champs = array_diff($champs, array_keys($boucle->modificateur['criteres'])); |
|
| 1611 | - } |
|
| 1612 | - // nous aider en mode debug. |
|
| 1613 | - $boucle->debug[] = "id_ : " . implode(', ', $champs); |
|
| 1614 | - $boucle->modificateur['id_'] = $champs; |
|
| 1615 | - |
|
| 1616 | - // créer un critère {id_xxx?} de chaque champ retenu |
|
| 1617 | - foreach ($champs as $champ) { |
|
| 1618 | - $critere_id_table = new Critere; |
|
| 1619 | - $critere_id_table->op = $champ; |
|
| 1620 | - $critere_id_table->cond = '?'; |
|
| 1621 | - $critere_id_table->ligne = $crit->ligne; |
|
| 1622 | - calculer_critere_DEFAUT_dist($idb, $boucles, $critere_id_table); |
|
| 1623 | - } |
|
| 1599 | + /** @var Boucle $boucle */ |
|
| 1600 | + $boucle = $boucles[$idb]; |
|
| 1601 | + |
|
| 1602 | + $champs = lister_champs_selection_conditionnelle( |
|
| 1603 | + $boucle->show['table'], |
|
| 1604 | + $boucle->show, |
|
| 1605 | + $boucle->sql_serveur |
|
| 1606 | + ); |
|
| 1607 | + |
|
| 1608 | + // ne pas tenir compte des critères identiques déjà présents. |
|
| 1609 | + if (!empty($boucle->modificateur['criteres'])) { |
|
| 1610 | + $champs = array_diff($champs, array_keys($boucle->modificateur['criteres'])); |
|
| 1611 | + } |
|
| 1612 | + // nous aider en mode debug. |
|
| 1613 | + $boucle->debug[] = "id_ : " . implode(', ', $champs); |
|
| 1614 | + $boucle->modificateur['id_'] = $champs; |
|
| 1615 | + |
|
| 1616 | + // créer un critère {id_xxx?} de chaque champ retenu |
|
| 1617 | + foreach ($champs as $champ) { |
|
| 1618 | + $critere_id_table = new Critere; |
|
| 1619 | + $critere_id_table->op = $champ; |
|
| 1620 | + $critere_id_table->cond = '?'; |
|
| 1621 | + $critere_id_table->ligne = $crit->ligne; |
|
| 1622 | + calculer_critere_DEFAUT_dist($idb, $boucles, $critere_id_table); |
|
| 1623 | + } |
|
| 1624 | 1624 | } |
| 1625 | 1625 | |
| 1626 | 1626 | /** |
@@ -1640,67 +1640,67 @@ discard block |
||
| 1640 | 1640 | * @return array Liste de nom de champs (tel que id_article, id_mot, id_parent ...) |
| 1641 | 1641 | */ |
| 1642 | 1642 | function lister_champs_selection_conditionnelle($table, $desc = null, $serveur = '') { |
| 1643 | - // calculer la description de la table |
|
| 1644 | - if (!is_array($desc)) { |
|
| 1645 | - $desc = description_table($table, $serveur); |
|
| 1646 | - } |
|
| 1647 | - if (!$desc) { |
|
| 1648 | - return []; |
|
| 1649 | - } |
|
| 1650 | - |
|
| 1651 | - // Les champs id_xx de la table demandée |
|
| 1652 | - $champs = array_filter( |
|
| 1653 | - array_keys($desc['field']), |
|
| 1654 | - function($champ){ |
|
| 1655 | - return |
|
| 1656 | - strpos($champ, 'id_') === 0 |
|
| 1657 | - or (in_array($champ, array('objet'))); |
|
| 1658 | - } |
|
| 1659 | - ); |
|
| 1660 | - |
|
| 1661 | - // On ne fera pas mieux pour les tables d’un autre serveur |
|
| 1662 | - if ($serveur) { |
|
| 1663 | - return $champs; |
|
| 1664 | - } |
|
| 1665 | - |
|
| 1666 | - $primary = false; |
|
| 1667 | - $associable = false; |
|
| 1668 | - include_spip('action/editer_liens'); |
|
| 1669 | - |
|
| 1670 | - if (isset($desc['type'])) { |
|
| 1671 | - $primary = id_table_objet($desc['type']); |
|
| 1672 | - $associable = objet_associable($desc['type']); |
|
| 1673 | - } |
|
| 1674 | - if (isset($desc['field']['id_objet']) and isset($desc['field']['objet'])) { |
|
| 1675 | - $associable = true; |
|
| 1676 | - } |
|
| 1677 | - |
|
| 1678 | - // liste de toutes les tables principales, sauf la notre |
|
| 1679 | - $tables = lister_tables_objets_sql(); |
|
| 1680 | - unset($tables[$table]); |
|
| 1681 | - |
|
| 1682 | - foreach ($tables as $_table => $_desc) { |
|
| 1683 | - if ( |
|
| 1684 | - $associable |
|
| 1685 | - or ($primary and in_array($primary, array_keys($_desc['field']))) |
|
| 1686 | - or objet_associable($_desc['type']) |
|
| 1687 | - ) { |
|
| 1688 | - $champs[] = id_table_objet($_table); |
|
| 1689 | - } |
|
| 1690 | - } |
|
| 1691 | - $champs = array_values(array_unique($champs)); |
|
| 1692 | - $champs = pipeline( |
|
| 1693 | - 'lister_champs_selection_conditionnelle', |
|
| 1694 | - array( |
|
| 1695 | - 'args' => array( |
|
| 1696 | - 'table' => $table, |
|
| 1697 | - 'id_table_objet' => $primary, |
|
| 1698 | - 'associable' => $associable, |
|
| 1699 | - ), |
|
| 1700 | - 'data' => $champs, |
|
| 1701 | - ) |
|
| 1702 | - ); |
|
| 1703 | - return $champs; |
|
| 1643 | + // calculer la description de la table |
|
| 1644 | + if (!is_array($desc)) { |
|
| 1645 | + $desc = description_table($table, $serveur); |
|
| 1646 | + } |
|
| 1647 | + if (!$desc) { |
|
| 1648 | + return []; |
|
| 1649 | + } |
|
| 1650 | + |
|
| 1651 | + // Les champs id_xx de la table demandée |
|
| 1652 | + $champs = array_filter( |
|
| 1653 | + array_keys($desc['field']), |
|
| 1654 | + function($champ){ |
|
| 1655 | + return |
|
| 1656 | + strpos($champ, 'id_') === 0 |
|
| 1657 | + or (in_array($champ, array('objet'))); |
|
| 1658 | + } |
|
| 1659 | + ); |
|
| 1660 | + |
|
| 1661 | + // On ne fera pas mieux pour les tables d’un autre serveur |
|
| 1662 | + if ($serveur) { |
|
| 1663 | + return $champs; |
|
| 1664 | + } |
|
| 1665 | + |
|
| 1666 | + $primary = false; |
|
| 1667 | + $associable = false; |
|
| 1668 | + include_spip('action/editer_liens'); |
|
| 1669 | + |
|
| 1670 | + if (isset($desc['type'])) { |
|
| 1671 | + $primary = id_table_objet($desc['type']); |
|
| 1672 | + $associable = objet_associable($desc['type']); |
|
| 1673 | + } |
|
| 1674 | + if (isset($desc['field']['id_objet']) and isset($desc['field']['objet'])) { |
|
| 1675 | + $associable = true; |
|
| 1676 | + } |
|
| 1677 | + |
|
| 1678 | + // liste de toutes les tables principales, sauf la notre |
|
| 1679 | + $tables = lister_tables_objets_sql(); |
|
| 1680 | + unset($tables[$table]); |
|
| 1681 | + |
|
| 1682 | + foreach ($tables as $_table => $_desc) { |
|
| 1683 | + if ( |
|
| 1684 | + $associable |
|
| 1685 | + or ($primary and in_array($primary, array_keys($_desc['field']))) |
|
| 1686 | + or objet_associable($_desc['type']) |
|
| 1687 | + ) { |
|
| 1688 | + $champs[] = id_table_objet($_table); |
|
| 1689 | + } |
|
| 1690 | + } |
|
| 1691 | + $champs = array_values(array_unique($champs)); |
|
| 1692 | + $champs = pipeline( |
|
| 1693 | + 'lister_champs_selection_conditionnelle', |
|
| 1694 | + array( |
|
| 1695 | + 'args' => array( |
|
| 1696 | + 'table' => $table, |
|
| 1697 | + 'id_table_objet' => $primary, |
|
| 1698 | + 'associable' => $associable, |
|
| 1699 | + ), |
|
| 1700 | + 'data' => $champs, |
|
| 1701 | + ) |
|
| 1702 | + ); |
|
| 1703 | + return $champs; |
|
| 1704 | 1704 | } |
| 1705 | 1705 | |
| 1706 | 1706 | /** |
@@ -1755,27 +1755,27 @@ discard block |
||
| 1755 | 1755 | * @return void |
| 1756 | 1756 | */ |
| 1757 | 1757 | function critere_tri_dist($idb, &$boucles, $crit) { |
| 1758 | - $boucle = &$boucles[$idb]; |
|
| 1759 | - |
|
| 1760 | - // definition du champ par defaut |
|
| 1761 | - $_champ_defaut = !isset($crit->param[0][0]) ? "''" |
|
| 1762 | - : calculer_liste(array($crit->param[0][0]), array(), $boucles, $boucle->id_parent); |
|
| 1763 | - $_sens_defaut = !isset($crit->param[1][0]) ? "1" |
|
| 1764 | - : calculer_liste(array($crit->param[1][0]), array(), $boucles, $boucle->id_parent); |
|
| 1765 | - $_variable = !isset($crit->param[2][0]) ? "'$idb'" |
|
| 1766 | - : calculer_liste(array($crit->param[2][0]), array(), $boucles, $boucle->id_parent); |
|
| 1767 | - |
|
| 1768 | - $_tri = "((\$t=(isset(\$Pile[0]['tri'.$_variable]))?\$Pile[0]['tri'.$_variable]:((strncmp($_variable,'session',7)==0 AND session_get('tri'.$_variable))?session_get('tri'.$_variable):$_champ_defaut))?tri_protege_champ(\$t):'')"; |
|
| 1769 | - |
|
| 1770 | - $_sens_defaut = "(is_array(\$s=$_sens_defaut)?(isset(\$s[\$st=$_tri])?\$s[\$st]:reset(\$s)):\$s)"; |
|
| 1771 | - $_sens = "((intval(\$t=(isset(\$Pile[0]['sens'.$_variable]))?\$Pile[0]['sens'.$_variable]:((strncmp($_variable,'session',7)==0 AND session_get('sens'.$_variable))?session_get('sens'.$_variable):$_sens_defaut))==-1 OR \$t=='inverse')?-1:1)"; |
|
| 1772 | - |
|
| 1773 | - $boucle->modificateur['tri_champ'] = $_tri; |
|
| 1774 | - $boucle->modificateur['tri_sens'] = $_sens; |
|
| 1775 | - $boucle->modificateur['tri_nom'] = $_variable; |
|
| 1776 | - // faut il inserer un test sur l'existence de $tri parmi les champs de la table ? |
|
| 1777 | - // evite des erreurs sql, mais peut empecher des tri sur jointure ... |
|
| 1778 | - $boucle->hash .= " |
|
| 1758 | + $boucle = &$boucles[$idb]; |
|
| 1759 | + |
|
| 1760 | + // definition du champ par defaut |
|
| 1761 | + $_champ_defaut = !isset($crit->param[0][0]) ? "''" |
|
| 1762 | + : calculer_liste(array($crit->param[0][0]), array(), $boucles, $boucle->id_parent); |
|
| 1763 | + $_sens_defaut = !isset($crit->param[1][0]) ? "1" |
|
| 1764 | + : calculer_liste(array($crit->param[1][0]), array(), $boucles, $boucle->id_parent); |
|
| 1765 | + $_variable = !isset($crit->param[2][0]) ? "'$idb'" |
|
| 1766 | + : calculer_liste(array($crit->param[2][0]), array(), $boucles, $boucle->id_parent); |
|
| 1767 | + |
|
| 1768 | + $_tri = "((\$t=(isset(\$Pile[0]['tri'.$_variable]))?\$Pile[0]['tri'.$_variable]:((strncmp($_variable,'session',7)==0 AND session_get('tri'.$_variable))?session_get('tri'.$_variable):$_champ_defaut))?tri_protege_champ(\$t):'')"; |
|
| 1769 | + |
|
| 1770 | + $_sens_defaut = "(is_array(\$s=$_sens_defaut)?(isset(\$s[\$st=$_tri])?\$s[\$st]:reset(\$s)):\$s)"; |
|
| 1771 | + $_sens = "((intval(\$t=(isset(\$Pile[0]['sens'.$_variable]))?\$Pile[0]['sens'.$_variable]:((strncmp($_variable,'session',7)==0 AND session_get('sens'.$_variable))?session_get('sens'.$_variable):$_sens_defaut))==-1 OR \$t=='inverse')?-1:1)"; |
|
| 1772 | + |
|
| 1773 | + $boucle->modificateur['tri_champ'] = $_tri; |
|
| 1774 | + $boucle->modificateur['tri_sens'] = $_sens; |
|
| 1775 | + $boucle->modificateur['tri_nom'] = $_variable; |
|
| 1776 | + // faut il inserer un test sur l'existence de $tri parmi les champs de la table ? |
|
| 1777 | + // evite des erreurs sql, mais peut empecher des tri sur jointure ... |
|
| 1778 | + $boucle->hash .= " |
|
| 1779 | 1779 | \$senstri = ''; |
| 1780 | 1780 | \$tri = $_tri; |
| 1781 | 1781 | if (\$tri){ |
@@ -1783,8 +1783,8 @@ discard block |
||
| 1783 | 1783 | \$senstri = (\$senstri<0)?' DESC':''; |
| 1784 | 1784 | }; |
| 1785 | 1785 | "; |
| 1786 | - $boucle->select[] = "\".tri_champ_select(\$tri).\""; |
|
| 1787 | - $boucle->order[] = "tri_champ_order(\$tri,\$command['from']).\$senstri"; |
|
| 1786 | + $boucle->select[] = "\".tri_champ_select(\$tri).\""; |
|
| 1787 | + $boucle->order[] = "tri_champ_order(\$tri,\$command['from']).\$senstri"; |
|
| 1788 | 1788 | } |
| 1789 | 1789 | |
| 1790 | 1790 | # Criteres de comparaison |
@@ -1801,20 +1801,20 @@ discard block |
||
| 1801 | 1801 | * @return void |
| 1802 | 1802 | **/ |
| 1803 | 1803 | function calculer_critere_DEFAUT_dist($idb, &$boucles, $crit) { |
| 1804 | - // double cas particulier {0,1} et {1/2} repere a l'analyse lexicale |
|
| 1805 | - if (($crit->op == ",") or ($crit->op == '/')) { |
|
| 1806 | - return calculer_critere_parties($idb, $boucles, $crit); |
|
| 1807 | - } |
|
| 1808 | - |
|
| 1809 | - $r = calculer_critere_infixe($idb, $boucles, $crit); |
|
| 1810 | - if (!$r) { |
|
| 1811 | - # // on produit une erreur seulement si le critere n'a pas de '?' |
|
| 1812 | - # if (!$crit->cond) { |
|
| 1813 | - return (array('zbug_critere_inconnu', array('critere' => $crit->op))); |
|
| 1814 | - # } |
|
| 1815 | - } else { |
|
| 1816 | - calculer_critere_DEFAUT_args($idb, $boucles, $crit, $r); |
|
| 1817 | - } |
|
| 1804 | + // double cas particulier {0,1} et {1/2} repere a l'analyse lexicale |
|
| 1805 | + if (($crit->op == ",") or ($crit->op == '/')) { |
|
| 1806 | + return calculer_critere_parties($idb, $boucles, $crit); |
|
| 1807 | + } |
|
| 1808 | + |
|
| 1809 | + $r = calculer_critere_infixe($idb, $boucles, $crit); |
|
| 1810 | + if (!$r) { |
|
| 1811 | + # // on produit une erreur seulement si le critere n'a pas de '?' |
|
| 1812 | + # if (!$crit->cond) { |
|
| 1813 | + return (array('zbug_critere_inconnu', array('critere' => $crit->op))); |
|
| 1814 | + # } |
|
| 1815 | + } else { |
|
| 1816 | + calculer_critere_DEFAUT_args($idb, $boucles, $crit, $r); |
|
| 1817 | + } |
|
| 1818 | 1818 | } |
| 1819 | 1819 | |
| 1820 | 1820 | |
@@ -1834,63 +1834,63 @@ discard block |
||
| 1834 | 1834 | * @return void |
| 1835 | 1835 | **/ |
| 1836 | 1836 | function calculer_critere_DEFAUT_args($idb, &$boucles, $crit, $args) { |
| 1837 | - list($arg, $op, $val, $col, $where_complement) = $args; |
|
| 1838 | - |
|
| 1839 | - $where = array("'$op'", "'$arg'", $val[0]); |
|
| 1840 | - |
|
| 1841 | - // inserer la negation (cf !...) |
|
| 1842 | - |
|
| 1843 | - if ($crit->not) { |
|
| 1844 | - $where = array("'NOT'", $where); |
|
| 1845 | - } |
|
| 1846 | - if ($crit->exclus) { |
|
| 1847 | - if (!preg_match(",^L[0-9]+[.],", $arg)) { |
|
| 1848 | - $where = array("'NOT'", $where); |
|
| 1849 | - } else |
|
| 1850 | - // un not sur un critere de jointure se traduit comme un NOT IN avec une sous requete |
|
| 1851 | - // c'est une sous requete identique a la requete principale sous la forme (SELF,$select,$where) avec $select et $where qui surchargent |
|
| 1852 | - { |
|
| 1853 | - $where = array( |
|
| 1854 | - "'NOT'", |
|
| 1855 | - array( |
|
| 1856 | - "'IN'", |
|
| 1857 | - "'" . $boucles[$idb]->id_table . "." . $boucles[$idb]->primary . "'", |
|
| 1858 | - array("'SELF'", "'" . $boucles[$idb]->id_table . "." . $boucles[$idb]->primary . "'", $where) |
|
| 1859 | - ) |
|
| 1860 | - ); |
|
| 1861 | - } |
|
| 1862 | - } |
|
| 1863 | - |
|
| 1864 | - // inserer la condition (cf {lang?}) |
|
| 1865 | - // traiter a part la date, elle est mise d'office par SPIP, |
|
| 1866 | - if ($crit->cond) { |
|
| 1867 | - $pred = calculer_argument_precedent($idb, $col, $boucles); |
|
| 1868 | - if ($col == "date" or $col == "date_redac") { |
|
| 1869 | - if ($pred == "\$Pile[0]['" . $col . "']") { |
|
| 1870 | - $pred = "(\$Pile[0]['{$col}_default']?'':$pred)"; |
|
| 1871 | - } |
|
| 1872 | - } |
|
| 1873 | - |
|
| 1874 | - if ($op == '=' and !$crit->not) { |
|
| 1875 | - $where = array( |
|
| 1876 | - "'?'", |
|
| 1877 | - "(is_array($pred))", |
|
| 1878 | - critere_IN_cas($idb, $boucles, 'COND', $arg, $op, array($pred), $col), |
|
| 1879 | - $where |
|
| 1880 | - ); |
|
| 1881 | - } |
|
| 1882 | - $where = array("'?'", "!(is_array($pred)?count($pred):strlen($pred))", "''", $where); |
|
| 1883 | - if ($where_complement) // condition annexe du type "AND (objet='article')" |
|
| 1884 | - { |
|
| 1885 | - $where_complement = array("'?'", "!(is_array($pred)?count($pred):strlen($pred))", "''", $where_complement); |
|
| 1886 | - } |
|
| 1887 | - } |
|
| 1888 | - |
|
| 1889 | - $boucles[$idb]->where[] = $where; |
|
| 1890 | - if ($where_complement) // condition annexe du type "AND (objet='article')" |
|
| 1891 | - { |
|
| 1892 | - $boucles[$idb]->where[] = $where_complement; |
|
| 1893 | - } |
|
| 1837 | + list($arg, $op, $val, $col, $where_complement) = $args; |
|
| 1838 | + |
|
| 1839 | + $where = array("'$op'", "'$arg'", $val[0]); |
|
| 1840 | + |
|
| 1841 | + // inserer la negation (cf !...) |
|
| 1842 | + |
|
| 1843 | + if ($crit->not) { |
|
| 1844 | + $where = array("'NOT'", $where); |
|
| 1845 | + } |
|
| 1846 | + if ($crit->exclus) { |
|
| 1847 | + if (!preg_match(",^L[0-9]+[.],", $arg)) { |
|
| 1848 | + $where = array("'NOT'", $where); |
|
| 1849 | + } else |
|
| 1850 | + // un not sur un critere de jointure se traduit comme un NOT IN avec une sous requete |
|
| 1851 | + // c'est une sous requete identique a la requete principale sous la forme (SELF,$select,$where) avec $select et $where qui surchargent |
|
| 1852 | + { |
|
| 1853 | + $where = array( |
|
| 1854 | + "'NOT'", |
|
| 1855 | + array( |
|
| 1856 | + "'IN'", |
|
| 1857 | + "'" . $boucles[$idb]->id_table . "." . $boucles[$idb]->primary . "'", |
|
| 1858 | + array("'SELF'", "'" . $boucles[$idb]->id_table . "." . $boucles[$idb]->primary . "'", $where) |
|
| 1859 | + ) |
|
| 1860 | + ); |
|
| 1861 | + } |
|
| 1862 | + } |
|
| 1863 | + |
|
| 1864 | + // inserer la condition (cf {lang?}) |
|
| 1865 | + // traiter a part la date, elle est mise d'office par SPIP, |
|
| 1866 | + if ($crit->cond) { |
|
| 1867 | + $pred = calculer_argument_precedent($idb, $col, $boucles); |
|
| 1868 | + if ($col == "date" or $col == "date_redac") { |
|
| 1869 | + if ($pred == "\$Pile[0]['" . $col . "']") { |
|
| 1870 | + $pred = "(\$Pile[0]['{$col}_default']?'':$pred)"; |
|
| 1871 | + } |
|
| 1872 | + } |
|
| 1873 | + |
|
| 1874 | + if ($op == '=' and !$crit->not) { |
|
| 1875 | + $where = array( |
|
| 1876 | + "'?'", |
|
| 1877 | + "(is_array($pred))", |
|
| 1878 | + critere_IN_cas($idb, $boucles, 'COND', $arg, $op, array($pred), $col), |
|
| 1879 | + $where |
|
| 1880 | + ); |
|
| 1881 | + } |
|
| 1882 | + $where = array("'?'", "!(is_array($pred)?count($pred):strlen($pred))", "''", $where); |
|
| 1883 | + if ($where_complement) // condition annexe du type "AND (objet='article')" |
|
| 1884 | + { |
|
| 1885 | + $where_complement = array("'?'", "!(is_array($pred)?count($pred):strlen($pred))", "''", $where_complement); |
|
| 1886 | + } |
|
| 1887 | + } |
|
| 1888 | + |
|
| 1889 | + $boucles[$idb]->where[] = $where; |
|
| 1890 | + if ($where_complement) // condition annexe du type "AND (objet='article')" |
|
| 1891 | + { |
|
| 1892 | + $boucles[$idb]->where[] = $where_complement; |
|
| 1893 | + } |
|
| 1894 | 1894 | } |
| 1895 | 1895 | |
| 1896 | 1896 | |
@@ -1931,167 +1931,167 @@ discard block |
||
| 1931 | 1931 | **/ |
| 1932 | 1932 | function calculer_critere_infixe($idb, &$boucles, $crit) { |
| 1933 | 1933 | |
| 1934 | - $boucle = &$boucles[$idb]; |
|
| 1935 | - $type = $boucle->type_requete; |
|
| 1936 | - $table = $boucle->id_table; |
|
| 1937 | - $desc = $boucle->show; |
|
| 1938 | - $col_vraie = null; |
|
| 1939 | - |
|
| 1940 | - list($fct, $col, $op, $val, $args_sql) = |
|
| 1941 | - calculer_critere_infixe_ops($idb, $boucles, $crit); |
|
| 1942 | - |
|
| 1943 | - $col_alias = $col; |
|
| 1944 | - $where_complement = false; |
|
| 1945 | - |
|
| 1946 | - // Cas particulier : id_enfant => utiliser la colonne id_objet |
|
| 1947 | - if ($col == 'id_enfant') { |
|
| 1948 | - $col = $boucle->primary; |
|
| 1949 | - } |
|
| 1950 | - |
|
| 1951 | - // Cas particulier : id_parent => verifier les exceptions de tables |
|
| 1952 | - if ((in_array($col, array('id_parent', 'id_secteur')) and isset($GLOBALS['exceptions_des_tables'][$table][$col])) |
|
| 1953 | - or (isset($GLOBALS['exceptions_des_tables'][$table][$col]) and is_string($GLOBALS['exceptions_des_tables'][$table][$col])) |
|
| 1954 | - ) { |
|
| 1955 | - $col = $GLOBALS['exceptions_des_tables'][$table][$col]; |
|
| 1956 | - } // et possibilite de gerer un critere secteur sur des tables de plugins (ie forums) |
|
| 1957 | - else { |
|
| 1958 | - if (($col == 'id_secteur') and ($critere_secteur = charger_fonction("critere_secteur_$type", "public", true))) { |
|
| 1959 | - $table = $critere_secteur($idb, $boucles, $val, $crit); |
|
| 1960 | - } |
|
| 1961 | - |
|
| 1962 | - // cas id_article=xx qui se mappe en id_objet=xx AND objet=article |
|
| 1963 | - // sauf si exception declaree : sauter cette etape |
|
| 1964 | - else { |
|
| 1965 | - if ( |
|
| 1966 | - !isset($GLOBALS['exceptions_des_jointures'][table_objet_sql($table)][$col]) |
|
| 1967 | - and !isset($GLOBALS['exceptions_des_jointures'][$col]) |
|
| 1968 | - and count(trouver_champs_decomposes($col, $desc)) > 1 |
|
| 1969 | - ) { |
|
| 1970 | - $e = decompose_champ_id_objet($col); |
|
| 1971 | - $col = array_shift($e); |
|
| 1972 | - $where_complement = primary_doublee($e, $table); |
|
| 1973 | - } // Cas particulier : expressions de date |
|
| 1974 | - else { |
|
| 1975 | - if ($c = calculer_critere_infixe_date($idb, $boucles, $col)) { |
|
| 1976 | - list($col, $col_vraie) = $c; |
|
| 1977 | - $table = ''; |
|
| 1978 | - } // table explicitée {mots.titre} |
|
| 1979 | - else { |
|
| 1980 | - if (preg_match('/^(.*)\.(.*)$/', $col, $r)) { |
|
| 1981 | - list(, $table, $col) = $r; |
|
| 1982 | - $col_alias = $col; |
|
| 1983 | - |
|
| 1984 | - $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 1985 | - if ($desc = $trouver_table($table, $boucle->sql_serveur) |
|
| 1986 | - and isset($desc['field'][$col]) |
|
| 1987 | - and $cle = array_search($desc['table'], $boucle->from) |
|
| 1988 | - ) { |
|
| 1989 | - $table = $cle; |
|
| 1990 | - } else { |
|
| 1991 | - $table = trouver_jointure_champ($col, $boucle, array($table), ($crit->cond or $op != '=')); |
|
| 1992 | - } |
|
| 1993 | - #$table = calculer_critere_externe_init($boucle, array($table), $col, $desc, ($crit->cond OR $op!='='), true); |
|
| 1994 | - if (!$table) { |
|
| 1995 | - return ''; |
|
| 1996 | - } |
|
| 1997 | - } |
|
| 1998 | - // si le champ n'est pas trouvé dans la table, |
|
| 1999 | - // on cherche si une jointure peut l'obtenir |
|
| 2000 | - elseif (@!array_key_exists($col, $desc['field'])) { |
|
| 2001 | - // Champ joker * des iterateurs DATA qui accepte tout |
|
| 2002 | - if (@array_key_exists('*', $desc['field'])) { |
|
| 2003 | - $desc['field'][$col_vraie ? $col_vraie : $col] = ''; // on veut pas de cast INT par defaut car le type peut etre n'importe quoi dans les boucles DATA |
|
| 2004 | - } |
|
| 2005 | - else { |
|
| 2006 | - $r = calculer_critere_infixe_externe($boucle, $crit, $op, $desc, $col, $col_alias, $table); |
|
| 2007 | - if (!$r) { |
|
| 2008 | - return ''; |
|
| 2009 | - } |
|
| 2010 | - list($col, $col_alias, $table, $where_complement, $desc) = $r; |
|
| 2011 | - } |
|
| 2012 | - } |
|
| 2013 | - } |
|
| 2014 | - } |
|
| 2015 | - } |
|
| 2016 | - } |
|
| 2017 | - |
|
| 2018 | - $col_vraie = ($col_vraie ? $col_vraie : $col); |
|
| 2019 | - // Dans tous les cas, |
|
| 2020 | - // virer les guillemets eventuels autour d'un int (qui sont refuses par certains SQL) |
|
| 2021 | - // et passer dans sql_quote avec le type si connu |
|
| 2022 | - // et int sinon si la valeur est numerique |
|
| 2023 | - // sinon introduire le vrai type du champ si connu dans le sql_quote (ou int NOT NULL sinon) |
|
| 2024 | - // Ne pas utiliser intval, PHP tronquant les Bigint de SQL |
|
| 2025 | - if ($op == '=' or in_array($op, $GLOBALS['table_criteres_infixes'])) { |
|
| 2026 | - $type_cast_quote = (isset($desc['field'][$col_vraie]) ? $desc['field'][$col_vraie] : 'int NOT NULL'); |
|
| 2027 | - // defaire le quote des int et les passer dans sql_quote avec le bon type de champ si on le connait, int sinon |
|
| 2028 | - // prendre en compte le debug ou la valeur arrive avec un commentaire PHP en debut |
|
| 2029 | - if (preg_match(",^\\A(\s*//.*?$\s*)?\"'(-?\d+)'\"\\z,ms", $val[0], $r)) { |
|
| 2030 | - $val[0] = $r[1] . '"' . sql_quote($r[2], $boucle->sql_serveur, $type_cast_quote) . '"'; |
|
| 2031 | - } |
|
| 2032 | - // sinon expliciter les |
|
| 2033 | - // sql_quote(truc) en sql_quote(truc,'',type) |
|
| 2034 | - // sql_quote(truc,serveur) en sql_quote(truc,serveur,type) |
|
| 2035 | - // sql_quote(truc,serveur,'') en sql_quote(truc,serveur,type) |
|
| 2036 | - // sans toucher aux |
|
| 2037 | - // sql_quote(truc,'','varchar(10) DEFAULT \'oui\' COLLATE NOCASE') |
|
| 2038 | - // sql_quote(truc,'','varchar') |
|
| 2039 | - elseif (preg_match('/\Asql_quote[(](.*?)(,[^)]*?)?(,[^)]*(?:\(\d+\)[^)]*)?)?[)]\s*\z/ms', $val[0], $r) |
|
| 2040 | - // si pas deja un type |
|
| 2041 | - and (!isset($r[3]) or !$r[3] or !trim($r[3],", '")) |
|
| 2042 | - ) { |
|
| 2043 | - $r = $r[1] |
|
| 2044 | - . ((isset($r[2]) and $r[2]) ? $r[2] : ",''") |
|
| 2045 | - . ",'" . addslashes($type_cast_quote) . "'"; |
|
| 2046 | - $val[0] = "sql_quote($r)"; |
|
| 2047 | - } |
|
| 2048 | - elseif(strpos($val[0], '@@defaultcast@@') !== false |
|
| 2049 | - and preg_match("/'@@defaultcast@@'\s*\)\s*\z/ms", $val[0], $r)) { |
|
| 2050 | - $val[0] = substr($val[0], 0, -strlen($r[0])) . "'" . addslashes($type_cast_quote) . "')"; |
|
| 2051 | - } |
|
| 2052 | - } |
|
| 2053 | - |
|
| 2054 | - if(strpos($val[0], '@@defaultcast@@') !== false |
|
| 2055 | - and preg_match("/'@@defaultcast@@'\s*\)\s*\z/ms", $val[0], $r)) { |
|
| 2056 | - $val[0] = substr($val[0], 0, -strlen($r[0])) . "'char')"; |
|
| 2057 | - } |
|
| 2058 | - |
|
| 2059 | - // Indicateur pour permettre aux fonctionx boucle_X de modifier |
|
| 2060 | - // leurs requetes par defaut, notamment le champ statut |
|
| 2061 | - // Ne pas confondre champs de la table principale et des jointures |
|
| 2062 | - if ($table === $boucle->id_table) { |
|
| 2063 | - $boucles[$idb]->modificateur['criteres'][$col_vraie] = true; |
|
| 2064 | - if ($col_alias != $col_vraie) { |
|
| 2065 | - $boucles[$idb]->modificateur['criteres'][$col_alias] = true; |
|
| 2066 | - } |
|
| 2067 | - } |
|
| 2068 | - |
|
| 2069 | - // ajout pour le cas special d'une condition sur le champ statut: |
|
| 2070 | - // il faut alors interdire a la fonction de boucle |
|
| 2071 | - // de mettre ses propres criteres de statut |
|
| 2072 | - // http://www.spip.net/@statut (a documenter) |
|
| 2073 | - // garde pour compatibilite avec code des plugins anterieurs, mais redondant avec la ligne precedente |
|
| 2074 | - if ($col == 'statut') { |
|
| 2075 | - $boucles[$idb]->statut = true; |
|
| 2076 | - } |
|
| 2077 | - |
|
| 2078 | - // inserer le nom de la table SQL devant le nom du champ |
|
| 2079 | - if ($table) { |
|
| 2080 | - if ($col[0] == "`") { |
|
| 2081 | - $arg = "$table." . substr($col, 1, -1); |
|
| 2082 | - } else { |
|
| 2083 | - $arg = "$table.$col"; |
|
| 2084 | - } |
|
| 2085 | - } else { |
|
| 2086 | - $arg = $col; |
|
| 2087 | - } |
|
| 2088 | - |
|
| 2089 | - // inserer la fonction SQL |
|
| 2090 | - if ($fct) { |
|
| 2091 | - $arg = "$fct($arg$args_sql)"; |
|
| 2092 | - } |
|
| 2093 | - |
|
| 2094 | - return array($arg, $op, $val, $col_alias, $where_complement); |
|
| 1934 | + $boucle = &$boucles[$idb]; |
|
| 1935 | + $type = $boucle->type_requete; |
|
| 1936 | + $table = $boucle->id_table; |
|
| 1937 | + $desc = $boucle->show; |
|
| 1938 | + $col_vraie = null; |
|
| 1939 | + |
|
| 1940 | + list($fct, $col, $op, $val, $args_sql) = |
|
| 1941 | + calculer_critere_infixe_ops($idb, $boucles, $crit); |
|
| 1942 | + |
|
| 1943 | + $col_alias = $col; |
|
| 1944 | + $where_complement = false; |
|
| 1945 | + |
|
| 1946 | + // Cas particulier : id_enfant => utiliser la colonne id_objet |
|
| 1947 | + if ($col == 'id_enfant') { |
|
| 1948 | + $col = $boucle->primary; |
|
| 1949 | + } |
|
| 1950 | + |
|
| 1951 | + // Cas particulier : id_parent => verifier les exceptions de tables |
|
| 1952 | + if ((in_array($col, array('id_parent', 'id_secteur')) and isset($GLOBALS['exceptions_des_tables'][$table][$col])) |
|
| 1953 | + or (isset($GLOBALS['exceptions_des_tables'][$table][$col]) and is_string($GLOBALS['exceptions_des_tables'][$table][$col])) |
|
| 1954 | + ) { |
|
| 1955 | + $col = $GLOBALS['exceptions_des_tables'][$table][$col]; |
|
| 1956 | + } // et possibilite de gerer un critere secteur sur des tables de plugins (ie forums) |
|
| 1957 | + else { |
|
| 1958 | + if (($col == 'id_secteur') and ($critere_secteur = charger_fonction("critere_secteur_$type", "public", true))) { |
|
| 1959 | + $table = $critere_secteur($idb, $boucles, $val, $crit); |
|
| 1960 | + } |
|
| 1961 | + |
|
| 1962 | + // cas id_article=xx qui se mappe en id_objet=xx AND objet=article |
|
| 1963 | + // sauf si exception declaree : sauter cette etape |
|
| 1964 | + else { |
|
| 1965 | + if ( |
|
| 1966 | + !isset($GLOBALS['exceptions_des_jointures'][table_objet_sql($table)][$col]) |
|
| 1967 | + and !isset($GLOBALS['exceptions_des_jointures'][$col]) |
|
| 1968 | + and count(trouver_champs_decomposes($col, $desc)) > 1 |
|
| 1969 | + ) { |
|
| 1970 | + $e = decompose_champ_id_objet($col); |
|
| 1971 | + $col = array_shift($e); |
|
| 1972 | + $where_complement = primary_doublee($e, $table); |
|
| 1973 | + } // Cas particulier : expressions de date |
|
| 1974 | + else { |
|
| 1975 | + if ($c = calculer_critere_infixe_date($idb, $boucles, $col)) { |
|
| 1976 | + list($col, $col_vraie) = $c; |
|
| 1977 | + $table = ''; |
|
| 1978 | + } // table explicitée {mots.titre} |
|
| 1979 | + else { |
|
| 1980 | + if (preg_match('/^(.*)\.(.*)$/', $col, $r)) { |
|
| 1981 | + list(, $table, $col) = $r; |
|
| 1982 | + $col_alias = $col; |
|
| 1983 | + |
|
| 1984 | + $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 1985 | + if ($desc = $trouver_table($table, $boucle->sql_serveur) |
|
| 1986 | + and isset($desc['field'][$col]) |
|
| 1987 | + and $cle = array_search($desc['table'], $boucle->from) |
|
| 1988 | + ) { |
|
| 1989 | + $table = $cle; |
|
| 1990 | + } else { |
|
| 1991 | + $table = trouver_jointure_champ($col, $boucle, array($table), ($crit->cond or $op != '=')); |
|
| 1992 | + } |
|
| 1993 | + #$table = calculer_critere_externe_init($boucle, array($table), $col, $desc, ($crit->cond OR $op!='='), true); |
|
| 1994 | + if (!$table) { |
|
| 1995 | + return ''; |
|
| 1996 | + } |
|
| 1997 | + } |
|
| 1998 | + // si le champ n'est pas trouvé dans la table, |
|
| 1999 | + // on cherche si une jointure peut l'obtenir |
|
| 2000 | + elseif (@!array_key_exists($col, $desc['field'])) { |
|
| 2001 | + // Champ joker * des iterateurs DATA qui accepte tout |
|
| 2002 | + if (@array_key_exists('*', $desc['field'])) { |
|
| 2003 | + $desc['field'][$col_vraie ? $col_vraie : $col] = ''; // on veut pas de cast INT par defaut car le type peut etre n'importe quoi dans les boucles DATA |
|
| 2004 | + } |
|
| 2005 | + else { |
|
| 2006 | + $r = calculer_critere_infixe_externe($boucle, $crit, $op, $desc, $col, $col_alias, $table); |
|
| 2007 | + if (!$r) { |
|
| 2008 | + return ''; |
|
| 2009 | + } |
|
| 2010 | + list($col, $col_alias, $table, $where_complement, $desc) = $r; |
|
| 2011 | + } |
|
| 2012 | + } |
|
| 2013 | + } |
|
| 2014 | + } |
|
| 2015 | + } |
|
| 2016 | + } |
|
| 2017 | + |
|
| 2018 | + $col_vraie = ($col_vraie ? $col_vraie : $col); |
|
| 2019 | + // Dans tous les cas, |
|
| 2020 | + // virer les guillemets eventuels autour d'un int (qui sont refuses par certains SQL) |
|
| 2021 | + // et passer dans sql_quote avec le type si connu |
|
| 2022 | + // et int sinon si la valeur est numerique |
|
| 2023 | + // sinon introduire le vrai type du champ si connu dans le sql_quote (ou int NOT NULL sinon) |
|
| 2024 | + // Ne pas utiliser intval, PHP tronquant les Bigint de SQL |
|
| 2025 | + if ($op == '=' or in_array($op, $GLOBALS['table_criteres_infixes'])) { |
|
| 2026 | + $type_cast_quote = (isset($desc['field'][$col_vraie]) ? $desc['field'][$col_vraie] : 'int NOT NULL'); |
|
| 2027 | + // defaire le quote des int et les passer dans sql_quote avec le bon type de champ si on le connait, int sinon |
|
| 2028 | + // prendre en compte le debug ou la valeur arrive avec un commentaire PHP en debut |
|
| 2029 | + if (preg_match(",^\\A(\s*//.*?$\s*)?\"'(-?\d+)'\"\\z,ms", $val[0], $r)) { |
|
| 2030 | + $val[0] = $r[1] . '"' . sql_quote($r[2], $boucle->sql_serveur, $type_cast_quote) . '"'; |
|
| 2031 | + } |
|
| 2032 | + // sinon expliciter les |
|
| 2033 | + // sql_quote(truc) en sql_quote(truc,'',type) |
|
| 2034 | + // sql_quote(truc,serveur) en sql_quote(truc,serveur,type) |
|
| 2035 | + // sql_quote(truc,serveur,'') en sql_quote(truc,serveur,type) |
|
| 2036 | + // sans toucher aux |
|
| 2037 | + // sql_quote(truc,'','varchar(10) DEFAULT \'oui\' COLLATE NOCASE') |
|
| 2038 | + // sql_quote(truc,'','varchar') |
|
| 2039 | + elseif (preg_match('/\Asql_quote[(](.*?)(,[^)]*?)?(,[^)]*(?:\(\d+\)[^)]*)?)?[)]\s*\z/ms', $val[0], $r) |
|
| 2040 | + // si pas deja un type |
|
| 2041 | + and (!isset($r[3]) or !$r[3] or !trim($r[3],", '")) |
|
| 2042 | + ) { |
|
| 2043 | + $r = $r[1] |
|
| 2044 | + . ((isset($r[2]) and $r[2]) ? $r[2] : ",''") |
|
| 2045 | + . ",'" . addslashes($type_cast_quote) . "'"; |
|
| 2046 | + $val[0] = "sql_quote($r)"; |
|
| 2047 | + } |
|
| 2048 | + elseif(strpos($val[0], '@@defaultcast@@') !== false |
|
| 2049 | + and preg_match("/'@@defaultcast@@'\s*\)\s*\z/ms", $val[0], $r)) { |
|
| 2050 | + $val[0] = substr($val[0], 0, -strlen($r[0])) . "'" . addslashes($type_cast_quote) . "')"; |
|
| 2051 | + } |
|
| 2052 | + } |
|
| 2053 | + |
|
| 2054 | + if(strpos($val[0], '@@defaultcast@@') !== false |
|
| 2055 | + and preg_match("/'@@defaultcast@@'\s*\)\s*\z/ms", $val[0], $r)) { |
|
| 2056 | + $val[0] = substr($val[0], 0, -strlen($r[0])) . "'char')"; |
|
| 2057 | + } |
|
| 2058 | + |
|
| 2059 | + // Indicateur pour permettre aux fonctionx boucle_X de modifier |
|
| 2060 | + // leurs requetes par defaut, notamment le champ statut |
|
| 2061 | + // Ne pas confondre champs de la table principale et des jointures |
|
| 2062 | + if ($table === $boucle->id_table) { |
|
| 2063 | + $boucles[$idb]->modificateur['criteres'][$col_vraie] = true; |
|
| 2064 | + if ($col_alias != $col_vraie) { |
|
| 2065 | + $boucles[$idb]->modificateur['criteres'][$col_alias] = true; |
|
| 2066 | + } |
|
| 2067 | + } |
|
| 2068 | + |
|
| 2069 | + // ajout pour le cas special d'une condition sur le champ statut: |
|
| 2070 | + // il faut alors interdire a la fonction de boucle |
|
| 2071 | + // de mettre ses propres criteres de statut |
|
| 2072 | + // http://www.spip.net/@statut (a documenter) |
|
| 2073 | + // garde pour compatibilite avec code des plugins anterieurs, mais redondant avec la ligne precedente |
|
| 2074 | + if ($col == 'statut') { |
|
| 2075 | + $boucles[$idb]->statut = true; |
|
| 2076 | + } |
|
| 2077 | + |
|
| 2078 | + // inserer le nom de la table SQL devant le nom du champ |
|
| 2079 | + if ($table) { |
|
| 2080 | + if ($col[0] == "`") { |
|
| 2081 | + $arg = "$table." . substr($col, 1, -1); |
|
| 2082 | + } else { |
|
| 2083 | + $arg = "$table.$col"; |
|
| 2084 | + } |
|
| 2085 | + } else { |
|
| 2086 | + $arg = $col; |
|
| 2087 | + } |
|
| 2088 | + |
|
| 2089 | + // inserer la fonction SQL |
|
| 2090 | + if ($fct) { |
|
| 2091 | + $arg = "$fct($arg$args_sql)"; |
|
| 2092 | + } |
|
| 2093 | + |
|
| 2094 | + return array($arg, $op, $val, $col_alias, $where_complement); |
|
| 2095 | 2095 | } |
| 2096 | 2096 | |
| 2097 | 2097 | |
@@ -2120,77 +2120,77 @@ discard block |
||
| 2120 | 2120 | **/ |
| 2121 | 2121 | function calculer_critere_infixe_externe($boucle, $crit, $op, $desc, $col, $col_alias, $table) { |
| 2122 | 2122 | |
| 2123 | - $where = ''; |
|
| 2124 | - |
|
| 2125 | - $calculer_critere_externe = 'calculer_critere_externe_init'; |
|
| 2126 | - // gestion par les plugins des jointures tordues |
|
| 2127 | - // pas automatiques mais necessaires |
|
| 2128 | - $table_sql = table_objet_sql($table); |
|
| 2129 | - if (isset($GLOBALS['exceptions_des_jointures'][$table_sql]) |
|
| 2130 | - and is_array($GLOBALS['exceptions_des_jointures'][$table_sql]) |
|
| 2131 | - and |
|
| 2132 | - ( |
|
| 2133 | - isset($GLOBALS['exceptions_des_jointures'][$table_sql][$col]) |
|
| 2134 | - or |
|
| 2135 | - isset($GLOBALS['exceptions_des_jointures'][$table_sql]['']) |
|
| 2136 | - ) |
|
| 2137 | - ) { |
|
| 2138 | - $t = $GLOBALS['exceptions_des_jointures'][$table_sql]; |
|
| 2139 | - $index = isset($t[$col]) |
|
| 2140 | - ? $t[$col] : (isset($t['']) ? $t[''] : array()); |
|
| 2141 | - |
|
| 2142 | - if (count($index) == 3) { |
|
| 2143 | - list($t, $col, $calculer_critere_externe) = $index; |
|
| 2144 | - } elseif (count($index) == 2) { |
|
| 2145 | - list($t, $col) = $t[$col]; |
|
| 2146 | - } elseif (count($index) == 1) { |
|
| 2147 | - list($calculer_critere_externe) = $index; |
|
| 2148 | - $t = $table; |
|
| 2149 | - } else { |
|
| 2150 | - $t = ''; |
|
| 2151 | - } // jointure non declaree. La trouver. |
|
| 2152 | - } elseif (isset($GLOBALS['exceptions_des_jointures'][$col])) { |
|
| 2153 | - list($t, $col) = $GLOBALS['exceptions_des_jointures'][$col]; |
|
| 2154 | - } else { |
|
| 2155 | - $t = ''; |
|
| 2156 | - } // jointure non declaree. La trouver. |
|
| 2157 | - |
|
| 2158 | - // ici on construit le from pour fournir $col en piochant dans les jointures |
|
| 2159 | - |
|
| 2160 | - // si des jointures explicites sont fournies, on cherche d'abord dans celles ci |
|
| 2161 | - // permet de forcer une table de lien quand il y a ambiguite |
|
| 2162 | - // <BOUCLE_(DOCUMENTS documents_liens){id_mot}> |
|
| 2163 | - // alors que <BOUCLE_(DOCUMENTS){id_mot}> produit la meme chose que <BOUCLE_(DOCUMENTS mots_liens){id_mot}> |
|
| 2164 | - $table = ""; |
|
| 2165 | - if ($boucle->jointures_explicites) { |
|
| 2166 | - $jointures_explicites = explode(' ', $boucle->jointures_explicites); |
|
| 2167 | - $table = $calculer_critere_externe($boucle, $jointures_explicites, $col, $desc, ($crit->cond or $op != '='), $t); |
|
| 2168 | - } |
|
| 2169 | - |
|
| 2170 | - // et sinon on cherche parmi toutes les jointures declarees |
|
| 2171 | - if (!$table) { |
|
| 2172 | - $table = $calculer_critere_externe($boucle, $boucle->jointures, $col, $desc, ($crit->cond or $op != '='), $t); |
|
| 2173 | - } |
|
| 2174 | - |
|
| 2175 | - if (!$table) { |
|
| 2176 | - return ''; |
|
| 2177 | - } |
|
| 2178 | - |
|
| 2179 | - // il ne reste plus qu'a trouver le champ dans les from |
|
| 2180 | - list($nom, $desc, $cle) = trouver_champ_exterieur($col, $boucle->from, $boucle); |
|
| 2181 | - |
|
| 2182 | - if (count($cle) > 1 or reset($cle) !== $col) { |
|
| 2183 | - $col_alias = $col; // id_article devient juste le nom d'origine |
|
| 2184 | - if (count($cle) > 1 and reset($cle) == 'id_objet') { |
|
| 2185 | - $e = decompose_champ_id_objet($col); |
|
| 2186 | - $col = array_shift($e); |
|
| 2187 | - $where = primary_doublee($e, $table); |
|
| 2188 | - } else { |
|
| 2189 | - $col = reset($cle); |
|
| 2190 | - } |
|
| 2191 | - } |
|
| 2192 | - |
|
| 2193 | - return array($col, $col_alias, $table, $where, $desc); |
|
| 2123 | + $where = ''; |
|
| 2124 | + |
|
| 2125 | + $calculer_critere_externe = 'calculer_critere_externe_init'; |
|
| 2126 | + // gestion par les plugins des jointures tordues |
|
| 2127 | + // pas automatiques mais necessaires |
|
| 2128 | + $table_sql = table_objet_sql($table); |
|
| 2129 | + if (isset($GLOBALS['exceptions_des_jointures'][$table_sql]) |
|
| 2130 | + and is_array($GLOBALS['exceptions_des_jointures'][$table_sql]) |
|
| 2131 | + and |
|
| 2132 | + ( |
|
| 2133 | + isset($GLOBALS['exceptions_des_jointures'][$table_sql][$col]) |
|
| 2134 | + or |
|
| 2135 | + isset($GLOBALS['exceptions_des_jointures'][$table_sql]['']) |
|
| 2136 | + ) |
|
| 2137 | + ) { |
|
| 2138 | + $t = $GLOBALS['exceptions_des_jointures'][$table_sql]; |
|
| 2139 | + $index = isset($t[$col]) |
|
| 2140 | + ? $t[$col] : (isset($t['']) ? $t[''] : array()); |
|
| 2141 | + |
|
| 2142 | + if (count($index) == 3) { |
|
| 2143 | + list($t, $col, $calculer_critere_externe) = $index; |
|
| 2144 | + } elseif (count($index) == 2) { |
|
| 2145 | + list($t, $col) = $t[$col]; |
|
| 2146 | + } elseif (count($index) == 1) { |
|
| 2147 | + list($calculer_critere_externe) = $index; |
|
| 2148 | + $t = $table; |
|
| 2149 | + } else { |
|
| 2150 | + $t = ''; |
|
| 2151 | + } // jointure non declaree. La trouver. |
|
| 2152 | + } elseif (isset($GLOBALS['exceptions_des_jointures'][$col])) { |
|
| 2153 | + list($t, $col) = $GLOBALS['exceptions_des_jointures'][$col]; |
|
| 2154 | + } else { |
|
| 2155 | + $t = ''; |
|
| 2156 | + } // jointure non declaree. La trouver. |
|
| 2157 | + |
|
| 2158 | + // ici on construit le from pour fournir $col en piochant dans les jointures |
|
| 2159 | + |
|
| 2160 | + // si des jointures explicites sont fournies, on cherche d'abord dans celles ci |
|
| 2161 | + // permet de forcer une table de lien quand il y a ambiguite |
|
| 2162 | + // <BOUCLE_(DOCUMENTS documents_liens){id_mot}> |
|
| 2163 | + // alors que <BOUCLE_(DOCUMENTS){id_mot}> produit la meme chose que <BOUCLE_(DOCUMENTS mots_liens){id_mot}> |
|
| 2164 | + $table = ""; |
|
| 2165 | + if ($boucle->jointures_explicites) { |
|
| 2166 | + $jointures_explicites = explode(' ', $boucle->jointures_explicites); |
|
| 2167 | + $table = $calculer_critere_externe($boucle, $jointures_explicites, $col, $desc, ($crit->cond or $op != '='), $t); |
|
| 2168 | + } |
|
| 2169 | + |
|
| 2170 | + // et sinon on cherche parmi toutes les jointures declarees |
|
| 2171 | + if (!$table) { |
|
| 2172 | + $table = $calculer_critere_externe($boucle, $boucle->jointures, $col, $desc, ($crit->cond or $op != '='), $t); |
|
| 2173 | + } |
|
| 2174 | + |
|
| 2175 | + if (!$table) { |
|
| 2176 | + return ''; |
|
| 2177 | + } |
|
| 2178 | + |
|
| 2179 | + // il ne reste plus qu'a trouver le champ dans les from |
|
| 2180 | + list($nom, $desc, $cle) = trouver_champ_exterieur($col, $boucle->from, $boucle); |
|
| 2181 | + |
|
| 2182 | + if (count($cle) > 1 or reset($cle) !== $col) { |
|
| 2183 | + $col_alias = $col; // id_article devient juste le nom d'origine |
|
| 2184 | + if (count($cle) > 1 and reset($cle) == 'id_objet') { |
|
| 2185 | + $e = decompose_champ_id_objet($col); |
|
| 2186 | + $col = array_shift($e); |
|
| 2187 | + $where = primary_doublee($e, $table); |
|
| 2188 | + } else { |
|
| 2189 | + $col = reset($cle); |
|
| 2190 | + } |
|
| 2191 | + } |
|
| 2192 | + |
|
| 2193 | + return array($col, $col_alias, $table, $where, $desc); |
|
| 2194 | 2194 | } |
| 2195 | 2195 | |
| 2196 | 2196 | |
@@ -2211,10 +2211,10 @@ discard block |
||
| 2211 | 2211 | * - valeur |
| 2212 | 2212 | **/ |
| 2213 | 2213 | function primary_doublee($decompose, $table) { |
| 2214 | - $e1 = reset($decompose); |
|
| 2215 | - $e2 = "sql_quote('" . end($decompose) . "')"; |
|
| 2214 | + $e1 = reset($decompose); |
|
| 2215 | + $e2 = "sql_quote('" . end($decompose) . "')"; |
|
| 2216 | 2216 | |
| 2217 | - return array("'='", "'$table." . $e1 . "'", $e2); |
|
| 2217 | + return array("'='", "'$table." . $e1 . "'", $e2); |
|
| 2218 | 2218 | } |
| 2219 | 2219 | |
| 2220 | 2220 | /** |
@@ -2245,56 +2245,56 @@ discard block |
||
| 2245 | 2245 | * Vide sinon. |
| 2246 | 2246 | */ |
| 2247 | 2247 | function calculer_critere_externe_init(&$boucle, $joints, $col, $desc, $cond, $checkarrivee = false) { |
| 2248 | - // si on demande un truc du genre spip_mots |
|
| 2249 | - // avec aussi spip_mots_liens dans les jointures dispo |
|
| 2250 | - // et qu'on est la |
|
| 2251 | - // il faut privilegier la jointure directe en 2 etapes spip_mots_liens, spip_mots |
|
| 2252 | - if ($checkarrivee |
|
| 2253 | - and is_string($checkarrivee) |
|
| 2254 | - and $a = table_objet($checkarrivee) |
|
| 2255 | - and in_array($a . '_liens', $joints) |
|
| 2256 | - ) { |
|
| 2257 | - if ($res = calculer_lien_externe_init($boucle, $joints, $col, $desc, $cond, $checkarrivee)) { |
|
| 2258 | - return $res; |
|
| 2259 | - } |
|
| 2260 | - } |
|
| 2261 | - foreach ($joints as $joint) { |
|
| 2262 | - if ($arrivee = trouver_champ_exterieur($col, array($joint), $boucle, $checkarrivee)) { |
|
| 2263 | - // alias de table dans le from |
|
| 2264 | - $t = array_search($arrivee[0], $boucle->from); |
|
| 2265 | - // recuperer la cle id_xx eventuellement decomposee en (id_objet,objet) |
|
| 2266 | - $cols = $arrivee[2]; |
|
| 2267 | - // mais on ignore la 3eme cle si presente qui correspond alors au point de depart |
|
| 2268 | - if (count($cols) > 2) { |
|
| 2269 | - array_pop($cols); |
|
| 2270 | - } |
|
| 2271 | - if ($t) { |
|
| 2272 | - // la table est déjà dans le FROM, on vérifie si le champ est utilisé. |
|
| 2273 | - $joindre = false; |
|
| 2274 | - foreach ($cols as $col) { |
|
| 2275 | - $c = '/\b' . $t . ".$col" . '\b/'; |
|
| 2276 | - if (trouver_champ($c, $boucle->where)) { |
|
| 2277 | - $joindre = true; |
|
| 2278 | - } else { |
|
| 2279 | - // mais ca peut etre dans le FIELD pour le Having |
|
| 2280 | - $c = "/FIELD.$t" . ".$col,/"; |
|
| 2281 | - if (trouver_champ($c, $boucle->select)) { |
|
| 2282 | - $joindre = true; |
|
| 2283 | - } |
|
| 2284 | - } |
|
| 2285 | - } |
|
| 2286 | - if (!$joindre) { |
|
| 2287 | - return $t; |
|
| 2288 | - } |
|
| 2289 | - } |
|
| 2290 | - array_pop($arrivee); |
|
| 2291 | - if ($res = calculer_jointure($boucle, array($boucle->id_table, $desc), $arrivee, $cols, $cond, 1)) { |
|
| 2292 | - return $res; |
|
| 2293 | - } |
|
| 2294 | - } |
|
| 2295 | - } |
|
| 2296 | - |
|
| 2297 | - return ''; |
|
| 2248 | + // si on demande un truc du genre spip_mots |
|
| 2249 | + // avec aussi spip_mots_liens dans les jointures dispo |
|
| 2250 | + // et qu'on est la |
|
| 2251 | + // il faut privilegier la jointure directe en 2 etapes spip_mots_liens, spip_mots |
|
| 2252 | + if ($checkarrivee |
|
| 2253 | + and is_string($checkarrivee) |
|
| 2254 | + and $a = table_objet($checkarrivee) |
|
| 2255 | + and in_array($a . '_liens', $joints) |
|
| 2256 | + ) { |
|
| 2257 | + if ($res = calculer_lien_externe_init($boucle, $joints, $col, $desc, $cond, $checkarrivee)) { |
|
| 2258 | + return $res; |
|
| 2259 | + } |
|
| 2260 | + } |
|
| 2261 | + foreach ($joints as $joint) { |
|
| 2262 | + if ($arrivee = trouver_champ_exterieur($col, array($joint), $boucle, $checkarrivee)) { |
|
| 2263 | + // alias de table dans le from |
|
| 2264 | + $t = array_search($arrivee[0], $boucle->from); |
|
| 2265 | + // recuperer la cle id_xx eventuellement decomposee en (id_objet,objet) |
|
| 2266 | + $cols = $arrivee[2]; |
|
| 2267 | + // mais on ignore la 3eme cle si presente qui correspond alors au point de depart |
|
| 2268 | + if (count($cols) > 2) { |
|
| 2269 | + array_pop($cols); |
|
| 2270 | + } |
|
| 2271 | + if ($t) { |
|
| 2272 | + // la table est déjà dans le FROM, on vérifie si le champ est utilisé. |
|
| 2273 | + $joindre = false; |
|
| 2274 | + foreach ($cols as $col) { |
|
| 2275 | + $c = '/\b' . $t . ".$col" . '\b/'; |
|
| 2276 | + if (trouver_champ($c, $boucle->where)) { |
|
| 2277 | + $joindre = true; |
|
| 2278 | + } else { |
|
| 2279 | + // mais ca peut etre dans le FIELD pour le Having |
|
| 2280 | + $c = "/FIELD.$t" . ".$col,/"; |
|
| 2281 | + if (trouver_champ($c, $boucle->select)) { |
|
| 2282 | + $joindre = true; |
|
| 2283 | + } |
|
| 2284 | + } |
|
| 2285 | + } |
|
| 2286 | + if (!$joindre) { |
|
| 2287 | + return $t; |
|
| 2288 | + } |
|
| 2289 | + } |
|
| 2290 | + array_pop($arrivee); |
|
| 2291 | + if ($res = calculer_jointure($boucle, array($boucle->id_table, $desc), $arrivee, $cols, $cond, 1)) { |
|
| 2292 | + return $res; |
|
| 2293 | + } |
|
| 2294 | + } |
|
| 2295 | + } |
|
| 2296 | + |
|
| 2297 | + return ''; |
|
| 2298 | 2298 | |
| 2299 | 2299 | } |
| 2300 | 2300 | |
@@ -2321,29 +2321,29 @@ discard block |
||
| 2321 | 2321 | * Alias de la table de jointure (Lx) |
| 2322 | 2322 | */ |
| 2323 | 2323 | function calculer_lien_externe_init(&$boucle, $joints, $col, $desc, $cond, $checkarrivee = false) { |
| 2324 | - $primary_arrivee = id_table_objet($checkarrivee); |
|
| 2325 | - |
|
| 2326 | - // [FIXME] $checkarrivee peut-il arriver avec false ???? |
|
| 2327 | - $intermediaire = trouver_champ_exterieur($primary_arrivee, $joints, $boucle, $checkarrivee . "_liens"); |
|
| 2328 | - $arrivee = trouver_champ_exterieur($col, $joints, $boucle, $checkarrivee); |
|
| 2329 | - |
|
| 2330 | - if (!$intermediaire or !$arrivee) { |
|
| 2331 | - return ''; |
|
| 2332 | - } |
|
| 2333 | - array_pop($intermediaire); // enlever la cle en 3eme argument |
|
| 2334 | - array_pop($arrivee); // enlever la cle en 3eme argument |
|
| 2335 | - |
|
| 2336 | - $res = fabrique_jointures($boucle, |
|
| 2337 | - array( |
|
| 2338 | - array( |
|
| 2339 | - $boucle->id_table, |
|
| 2340 | - $intermediaire, |
|
| 2341 | - array(id_table_objet($desc['table_objet']), 'id_objet', 'objet', $desc['type']) |
|
| 2342 | - ), |
|
| 2343 | - array(reset($intermediaire), $arrivee, $primary_arrivee) |
|
| 2344 | - ), $cond, $desc, $boucle->id_table, array($col)); |
|
| 2345 | - |
|
| 2346 | - return $res; |
|
| 2324 | + $primary_arrivee = id_table_objet($checkarrivee); |
|
| 2325 | + |
|
| 2326 | + // [FIXME] $checkarrivee peut-il arriver avec false ???? |
|
| 2327 | + $intermediaire = trouver_champ_exterieur($primary_arrivee, $joints, $boucle, $checkarrivee . "_liens"); |
|
| 2328 | + $arrivee = trouver_champ_exterieur($col, $joints, $boucle, $checkarrivee); |
|
| 2329 | + |
|
| 2330 | + if (!$intermediaire or !$arrivee) { |
|
| 2331 | + return ''; |
|
| 2332 | + } |
|
| 2333 | + array_pop($intermediaire); // enlever la cle en 3eme argument |
|
| 2334 | + array_pop($arrivee); // enlever la cle en 3eme argument |
|
| 2335 | + |
|
| 2336 | + $res = fabrique_jointures($boucle, |
|
| 2337 | + array( |
|
| 2338 | + array( |
|
| 2339 | + $boucle->id_table, |
|
| 2340 | + $intermediaire, |
|
| 2341 | + array(id_table_objet($desc['table_objet']), 'id_objet', 'objet', $desc['type']) |
|
| 2342 | + ), |
|
| 2343 | + array(reset($intermediaire), $arrivee, $primary_arrivee) |
|
| 2344 | + ), $cond, $desc, $boucle->id_table, array($col)); |
|
| 2345 | + |
|
| 2346 | + return $res; |
|
| 2347 | 2347 | } |
| 2348 | 2348 | |
| 2349 | 2349 | |
@@ -2360,17 +2360,17 @@ discard block |
||
| 2360 | 2360 | * false sinon. |
| 2361 | 2361 | **/ |
| 2362 | 2362 | function trouver_champ($champ, $where) { |
| 2363 | - if (!is_array($where)) { |
|
| 2364 | - return preg_match($champ, $where); |
|
| 2365 | - } else { |
|
| 2366 | - foreach ($where as $clause) { |
|
| 2367 | - if (trouver_champ($champ, $clause)) { |
|
| 2368 | - return true; |
|
| 2369 | - } |
|
| 2370 | - } |
|
| 2371 | - |
|
| 2372 | - return false; |
|
| 2373 | - } |
|
| 2363 | + if (!is_array($where)) { |
|
| 2364 | + return preg_match($champ, $where); |
|
| 2365 | + } else { |
|
| 2366 | + foreach ($where as $clause) { |
|
| 2367 | + if (trouver_champ($champ, $clause)) { |
|
| 2368 | + return true; |
|
| 2369 | + } |
|
| 2370 | + } |
|
| 2371 | + |
|
| 2372 | + return false; |
|
| 2373 | + } |
|
| 2374 | 2374 | } |
| 2375 | 2375 | |
| 2376 | 2376 | |
@@ -2396,129 +2396,129 @@ discard block |
||
| 2396 | 2396 | * - string $args_sql Suite des arguments du critère. ? |
| 2397 | 2397 | **/ |
| 2398 | 2398 | function calculer_critere_infixe_ops($idb, &$boucles, $crit) { |
| 2399 | - // cas d'une valeur comparee a elle-meme ou son referent |
|
| 2400 | - if (count($crit->param) == 0) { |
|
| 2401 | - $op = '='; |
|
| 2402 | - $col = $val = $crit->op; |
|
| 2403 | - if (preg_match('/^(.*)\.(.*)$/', $col, $r)) { |
|
| 2404 | - $val = $r[2]; |
|
| 2405 | - } |
|
| 2406 | - // Cas special {lang} : aller chercher $GLOBALS['spip_lang'] |
|
| 2407 | - if ($val == 'lang') { |
|
| 2408 | - $val = array(kwote('$GLOBALS[\'spip_lang\']')); |
|
| 2409 | - } else { |
|
| 2410 | - $defaut = null; |
|
| 2411 | - if ($val == 'id_parent') { |
|
| 2412 | - // Si id_parent, comparer l'id_parent avec l'id_objet |
|
| 2413 | - // de la boucle superieure.... faudrait verifier qu'il existe |
|
| 2414 | - // pour eviter l'erreur SQL |
|
| 2415 | - $val = $boucles[$idb]->primary; |
|
| 2416 | - // mais si pas de boucle superieure, prendre id_parent dans l'env |
|
| 2417 | - $defaut = "@\$Pile[0]['id_parent']"; |
|
| 2418 | - } elseif ($val == 'id_enfant') { |
|
| 2419 | - // Si id_enfant, comparer l'id_objet avec l'id_parent |
|
| 2420 | - // de la boucle superieure |
|
| 2421 | - $val = 'id_parent'; |
|
| 2422 | - } elseif ($crit->cond and ($col == "date" or $col == "date_redac")) { |
|
| 2423 | - // un critere conditionnel sur date est traite a part |
|
| 2424 | - // car la date est mise d'office par SPIP, |
|
| 2425 | - $defaut = "(\$Pile[0]['{$col}_default']?'':\$Pile[0]['" . $col . "'])"; |
|
| 2426 | - } |
|
| 2427 | - |
|
| 2428 | - $val = calculer_argument_precedent($idb, $val, $boucles, $defaut); |
|
| 2429 | - $val = array(kwote($val)); |
|
| 2430 | - } |
|
| 2431 | - } else { |
|
| 2432 | - // comparaison explicite |
|
| 2433 | - // le phraseur impose que le premier param soit du texte |
|
| 2434 | - $params = $crit->param; |
|
| 2435 | - $op = $crit->op; |
|
| 2436 | - if ($op == '==') { |
|
| 2437 | - $op = 'REGEXP'; |
|
| 2438 | - } |
|
| 2439 | - $col = array_shift($params); |
|
| 2440 | - $col = $col[0]->texte; |
|
| 2441 | - |
|
| 2442 | - $val = array(); |
|
| 2443 | - $desc = array('id_mere' => $idb); |
|
| 2444 | - $parent = $boucles[$idb]->id_parent; |
|
| 2445 | - |
|
| 2446 | - // Dans le cas {x=='#DATE'} etc, defaire le travail du phraseur, |
|
| 2447 | - // celui ne sachant pas ce qu'est un critere infixe |
|
| 2448 | - // et a fortiori son 2e operande qu'entoure " ou ' |
|
| 2449 | - if (count($params) == 1 |
|
| 2450 | - and count($params[0]) == 3 |
|
| 2451 | - and $params[0][0]->type == 'texte' |
|
| 2452 | - and $params[0][2]->type == 'texte' |
|
| 2453 | - and ($p = $params[0][0]->texte) == $params[0][2]->texte |
|
| 2454 | - and (($p == "'") or ($p == '"')) |
|
| 2455 | - and $params[0][1]->type == 'champ' |
|
| 2456 | - ) { |
|
| 2457 | - $val[] = "$p\\$p#" . $params[0][1]->nom_champ . "\\$p$p"; |
|
| 2458 | - } else { |
|
| 2459 | - foreach ((($op != 'IN') ? $params : calculer_vieux_in($params)) as $p) { |
|
| 2460 | - $a = calculer_liste($p, $desc, $boucles, $parent); |
|
| 2461 | - if (strcasecmp($op, 'IN') == 0) { |
|
| 2462 | - $val[] = $a; |
|
| 2463 | - } else { |
|
| 2464 | - $val[] = kwote($a, $boucles[$idb]->sql_serveur, '@@defaultcast@@'); |
|
| 2465 | - } // toujours quoter en char ici |
|
| 2466 | - } |
|
| 2467 | - } |
|
| 2468 | - } |
|
| 2469 | - |
|
| 2470 | - $fct = $args_sql = ''; |
|
| 2471 | - // fonction SQL ? |
|
| 2472 | - // chercher FONCTION(champ) tel que CONCAT(titre,descriptif) |
|
| 2473 | - if (preg_match('/^(.*)' . SQL_ARGS . '$/', $col, $m)) { |
|
| 2474 | - $fct = $m[1]; |
|
| 2475 | - preg_match('/^\(([^,]*)(.*)\)$/', $m[2], $a); |
|
| 2476 | - $col = $a[1]; |
|
| 2477 | - if (preg_match('/^(\S*)(\s+AS\s+.*)$/i', $col, $m)) { |
|
| 2478 | - $col = $m[1]; |
|
| 2479 | - $args_sql = $m[2]; |
|
| 2480 | - } |
|
| 2481 | - $args_sql .= $a[2]; |
|
| 2482 | - } |
|
| 2483 | - |
|
| 2484 | - return array($fct, $col, $op, $val, $args_sql); |
|
| 2399 | + // cas d'une valeur comparee a elle-meme ou son referent |
|
| 2400 | + if (count($crit->param) == 0) { |
|
| 2401 | + $op = '='; |
|
| 2402 | + $col = $val = $crit->op; |
|
| 2403 | + if (preg_match('/^(.*)\.(.*)$/', $col, $r)) { |
|
| 2404 | + $val = $r[2]; |
|
| 2405 | + } |
|
| 2406 | + // Cas special {lang} : aller chercher $GLOBALS['spip_lang'] |
|
| 2407 | + if ($val == 'lang') { |
|
| 2408 | + $val = array(kwote('$GLOBALS[\'spip_lang\']')); |
|
| 2409 | + } else { |
|
| 2410 | + $defaut = null; |
|
| 2411 | + if ($val == 'id_parent') { |
|
| 2412 | + // Si id_parent, comparer l'id_parent avec l'id_objet |
|
| 2413 | + // de la boucle superieure.... faudrait verifier qu'il existe |
|
| 2414 | + // pour eviter l'erreur SQL |
|
| 2415 | + $val = $boucles[$idb]->primary; |
|
| 2416 | + // mais si pas de boucle superieure, prendre id_parent dans l'env |
|
| 2417 | + $defaut = "@\$Pile[0]['id_parent']"; |
|
| 2418 | + } elseif ($val == 'id_enfant') { |
|
| 2419 | + // Si id_enfant, comparer l'id_objet avec l'id_parent |
|
| 2420 | + // de la boucle superieure |
|
| 2421 | + $val = 'id_parent'; |
|
| 2422 | + } elseif ($crit->cond and ($col == "date" or $col == "date_redac")) { |
|
| 2423 | + // un critere conditionnel sur date est traite a part |
|
| 2424 | + // car la date est mise d'office par SPIP, |
|
| 2425 | + $defaut = "(\$Pile[0]['{$col}_default']?'':\$Pile[0]['" . $col . "'])"; |
|
| 2426 | + } |
|
| 2427 | + |
|
| 2428 | + $val = calculer_argument_precedent($idb, $val, $boucles, $defaut); |
|
| 2429 | + $val = array(kwote($val)); |
|
| 2430 | + } |
|
| 2431 | + } else { |
|
| 2432 | + // comparaison explicite |
|
| 2433 | + // le phraseur impose que le premier param soit du texte |
|
| 2434 | + $params = $crit->param; |
|
| 2435 | + $op = $crit->op; |
|
| 2436 | + if ($op == '==') { |
|
| 2437 | + $op = 'REGEXP'; |
|
| 2438 | + } |
|
| 2439 | + $col = array_shift($params); |
|
| 2440 | + $col = $col[0]->texte; |
|
| 2441 | + |
|
| 2442 | + $val = array(); |
|
| 2443 | + $desc = array('id_mere' => $idb); |
|
| 2444 | + $parent = $boucles[$idb]->id_parent; |
|
| 2445 | + |
|
| 2446 | + // Dans le cas {x=='#DATE'} etc, defaire le travail du phraseur, |
|
| 2447 | + // celui ne sachant pas ce qu'est un critere infixe |
|
| 2448 | + // et a fortiori son 2e operande qu'entoure " ou ' |
|
| 2449 | + if (count($params) == 1 |
|
| 2450 | + and count($params[0]) == 3 |
|
| 2451 | + and $params[0][0]->type == 'texte' |
|
| 2452 | + and $params[0][2]->type == 'texte' |
|
| 2453 | + and ($p = $params[0][0]->texte) == $params[0][2]->texte |
|
| 2454 | + and (($p == "'") or ($p == '"')) |
|
| 2455 | + and $params[0][1]->type == 'champ' |
|
| 2456 | + ) { |
|
| 2457 | + $val[] = "$p\\$p#" . $params[0][1]->nom_champ . "\\$p$p"; |
|
| 2458 | + } else { |
|
| 2459 | + foreach ((($op != 'IN') ? $params : calculer_vieux_in($params)) as $p) { |
|
| 2460 | + $a = calculer_liste($p, $desc, $boucles, $parent); |
|
| 2461 | + if (strcasecmp($op, 'IN') == 0) { |
|
| 2462 | + $val[] = $a; |
|
| 2463 | + } else { |
|
| 2464 | + $val[] = kwote($a, $boucles[$idb]->sql_serveur, '@@defaultcast@@'); |
|
| 2465 | + } // toujours quoter en char ici |
|
| 2466 | + } |
|
| 2467 | + } |
|
| 2468 | + } |
|
| 2469 | + |
|
| 2470 | + $fct = $args_sql = ''; |
|
| 2471 | + // fonction SQL ? |
|
| 2472 | + // chercher FONCTION(champ) tel que CONCAT(titre,descriptif) |
|
| 2473 | + if (preg_match('/^(.*)' . SQL_ARGS . '$/', $col, $m)) { |
|
| 2474 | + $fct = $m[1]; |
|
| 2475 | + preg_match('/^\(([^,]*)(.*)\)$/', $m[2], $a); |
|
| 2476 | + $col = $a[1]; |
|
| 2477 | + if (preg_match('/^(\S*)(\s+AS\s+.*)$/i', $col, $m)) { |
|
| 2478 | + $col = $m[1]; |
|
| 2479 | + $args_sql = $m[2]; |
|
| 2480 | + } |
|
| 2481 | + $args_sql .= $a[2]; |
|
| 2482 | + } |
|
| 2483 | + |
|
| 2484 | + return array($fct, $col, $op, $val, $args_sql); |
|
| 2485 | 2485 | } |
| 2486 | 2486 | |
| 2487 | 2487 | // compatibilite ancienne version |
| 2488 | 2488 | |
| 2489 | 2489 | // http://code.spip.net/@calculer_vieux_in |
| 2490 | 2490 | function calculer_vieux_in($params) { |
| 2491 | - $deb = $params[0][0]; |
|
| 2492 | - $k = count($params) - 1; |
|
| 2493 | - $last = $params[$k]; |
|
| 2494 | - $j = count($last) - 1; |
|
| 2495 | - $last = $last[$j]; |
|
| 2496 | - $n = isset($last->texte) ? strlen($last->texte) : 0; |
|
| 2497 | - |
|
| 2498 | - if (!((isset($deb->texte[0]) and $deb->texte[0] == '(') |
|
| 2499 | - && (isset($last->texte[$n - 1]) and $last->texte[$n - 1] == ')')) |
|
| 2500 | - ) { |
|
| 2501 | - return $params; |
|
| 2502 | - } |
|
| 2503 | - $params[0][0]->texte = substr($deb->texte, 1); |
|
| 2504 | - // attention, on peut avoir k=0,j=0 ==> recalculer |
|
| 2505 | - $last = $params[$k][$j]; |
|
| 2506 | - $n = strlen($last->texte); |
|
| 2507 | - $params[$k][$j]->texte = substr($last->texte, 0, $n - 1); |
|
| 2508 | - $newp = array(); |
|
| 2509 | - foreach ($params as $v) { |
|
| 2510 | - if ($v[0]->type != 'texte') { |
|
| 2511 | - $newp[] = $v; |
|
| 2512 | - } else { |
|
| 2513 | - foreach (explode(',', $v[0]->texte) as $x) { |
|
| 2514 | - $t = new Texte; |
|
| 2515 | - $t->texte = $x; |
|
| 2516 | - $newp[] = array($t); |
|
| 2517 | - } |
|
| 2518 | - } |
|
| 2519 | - } |
|
| 2520 | - |
|
| 2521 | - return $newp; |
|
| 2491 | + $deb = $params[0][0]; |
|
| 2492 | + $k = count($params) - 1; |
|
| 2493 | + $last = $params[$k]; |
|
| 2494 | + $j = count($last) - 1; |
|
| 2495 | + $last = $last[$j]; |
|
| 2496 | + $n = isset($last->texte) ? strlen($last->texte) : 0; |
|
| 2497 | + |
|
| 2498 | + if (!((isset($deb->texte[0]) and $deb->texte[0] == '(') |
|
| 2499 | + && (isset($last->texte[$n - 1]) and $last->texte[$n - 1] == ')')) |
|
| 2500 | + ) { |
|
| 2501 | + return $params; |
|
| 2502 | + } |
|
| 2503 | + $params[0][0]->texte = substr($deb->texte, 1); |
|
| 2504 | + // attention, on peut avoir k=0,j=0 ==> recalculer |
|
| 2505 | + $last = $params[$k][$j]; |
|
| 2506 | + $n = strlen($last->texte); |
|
| 2507 | + $params[$k][$j]->texte = substr($last->texte, 0, $n - 1); |
|
| 2508 | + $newp = array(); |
|
| 2509 | + foreach ($params as $v) { |
|
| 2510 | + if ($v[0]->type != 'texte') { |
|
| 2511 | + $newp[] = $v; |
|
| 2512 | + } else { |
|
| 2513 | + foreach (explode(',', $v[0]->texte) as $x) { |
|
| 2514 | + $t = new Texte; |
|
| 2515 | + $t->texte = $x; |
|
| 2516 | + $newp[] = array($t); |
|
| 2517 | + } |
|
| 2518 | + } |
|
| 2519 | + } |
|
| 2520 | + |
|
| 2521 | + return $newp; |
|
| 2522 | 2522 | } |
| 2523 | 2523 | |
| 2524 | 2524 | /** |
@@ -2537,89 +2537,89 @@ discard block |
||
| 2537 | 2537 | * - nom de la colonne de date (si le calcul n'est pas relatif) |
| 2538 | 2538 | **/ |
| 2539 | 2539 | function calculer_critere_infixe_date($idb, &$boucles, $col) { |
| 2540 | - if (!preg_match(",^((age|jour|mois|annee)_relatif|date|mois|annee|jour|heure|age)(_[a-z]+)?$,", $col, $regs)) { |
|
| 2541 | - return ''; |
|
| 2542 | - } |
|
| 2543 | - |
|
| 2544 | - $boucle = $boucles[$idb]; |
|
| 2545 | - $table = $boucle->show; |
|
| 2546 | - |
|
| 2547 | - // si c'est une colonne de la table, ne rien faire |
|
| 2548 | - if (isset($table['field'][$col])) { |
|
| 2549 | - return ''; |
|
| 2550 | - } |
|
| 2551 | - |
|
| 2552 | - if (!$table['date'] && !isset($GLOBALS['table_date'][$table['id_table']])) { |
|
| 2553 | - return ''; |
|
| 2554 | - } |
|
| 2555 | - $pred = $date_orig = isset($GLOBALS['table_date'][$table['id_table']]) ? $GLOBALS['table_date'][$table['id_table']] : $table['date']; |
|
| 2556 | - |
|
| 2557 | - $col = $regs[1]; |
|
| 2558 | - if (isset($regs[3]) and $suite = $regs[3]) { |
|
| 2559 | - # Recherche de l'existence du champ date_xxxx, |
|
| 2560 | - # si oui choisir ce champ, sinon choisir xxxx |
|
| 2561 | - |
|
| 2562 | - if (isset($table['field']["date$suite"])) { |
|
| 2563 | - $date_orig = 'date' . $suite; |
|
| 2564 | - } else { |
|
| 2565 | - $date_orig = substr($suite, 1); |
|
| 2566 | - } |
|
| 2567 | - $pred = $date_orig; |
|
| 2568 | - } else { |
|
| 2569 | - if (isset($regs[2]) and $rel = $regs[2]) { |
|
| 2570 | - $pred = 'date'; |
|
| 2571 | - } |
|
| 2572 | - } |
|
| 2573 | - |
|
| 2574 | - $date_compare = "\"' . normaliser_date(" . |
|
| 2575 | - calculer_argument_precedent($idb, $pred, $boucles) . |
|
| 2576 | - ") . '\""; |
|
| 2577 | - |
|
| 2578 | - $col_vraie = $date_orig; |
|
| 2579 | - $date_orig = $boucle->id_table . '.' . $date_orig; |
|
| 2580 | - |
|
| 2581 | - switch ($col) { |
|
| 2582 | - case 'date': |
|
| 2583 | - $col = $date_orig; |
|
| 2584 | - break; |
|
| 2585 | - case 'jour': |
|
| 2586 | - $col = "DAYOFMONTH($date_orig)"; |
|
| 2587 | - break; |
|
| 2588 | - case 'mois': |
|
| 2589 | - $col = "MONTH($date_orig)"; |
|
| 2590 | - break; |
|
| 2591 | - case 'annee': |
|
| 2592 | - $col = "YEAR($date_orig)"; |
|
| 2593 | - break; |
|
| 2594 | - case 'heure': |
|
| 2595 | - $col = "DATE_FORMAT($date_orig, \\'%H:%i\\')"; |
|
| 2596 | - break; |
|
| 2597 | - case 'age': |
|
| 2598 | - $col = calculer_param_date("NOW()", $date_orig); |
|
| 2599 | - $col_vraie = "";// comparer a un int (par defaut) |
|
| 2600 | - break; |
|
| 2601 | - case 'age_relatif': |
|
| 2602 | - $col = calculer_param_date($date_compare, $date_orig); |
|
| 2603 | - $col_vraie = "";// comparer a un int (par defaut) |
|
| 2604 | - break; |
|
| 2605 | - case 'jour_relatif': |
|
| 2606 | - $col = "(TO_DAYS(" . $date_compare . ")-TO_DAYS(" . $date_orig . "))"; |
|
| 2607 | - $col_vraie = "";// comparer a un int (par defaut) |
|
| 2608 | - break; |
|
| 2609 | - case 'mois_relatif': |
|
| 2610 | - $col = "MONTH(" . $date_compare . ")-MONTH(" . |
|
| 2611 | - $date_orig . ")+12*(YEAR(" . $date_compare . |
|
| 2612 | - ")-YEAR(" . $date_orig . "))"; |
|
| 2613 | - $col_vraie = "";// comparer a un int (par defaut) |
|
| 2614 | - break; |
|
| 2615 | - case 'annee_relatif': |
|
| 2616 | - $col = "YEAR(" . $date_compare . ")-YEAR(" . |
|
| 2617 | - $date_orig . ")"; |
|
| 2618 | - $col_vraie = "";// comparer a un int (par defaut) |
|
| 2619 | - break; |
|
| 2620 | - } |
|
| 2621 | - |
|
| 2622 | - return array($col, $col_vraie); |
|
| 2540 | + if (!preg_match(",^((age|jour|mois|annee)_relatif|date|mois|annee|jour|heure|age)(_[a-z]+)?$,", $col, $regs)) { |
|
| 2541 | + return ''; |
|
| 2542 | + } |
|
| 2543 | + |
|
| 2544 | + $boucle = $boucles[$idb]; |
|
| 2545 | + $table = $boucle->show; |
|
| 2546 | + |
|
| 2547 | + // si c'est une colonne de la table, ne rien faire |
|
| 2548 | + if (isset($table['field'][$col])) { |
|
| 2549 | + return ''; |
|
| 2550 | + } |
|
| 2551 | + |
|
| 2552 | + if (!$table['date'] && !isset($GLOBALS['table_date'][$table['id_table']])) { |
|
| 2553 | + return ''; |
|
| 2554 | + } |
|
| 2555 | + $pred = $date_orig = isset($GLOBALS['table_date'][$table['id_table']]) ? $GLOBALS['table_date'][$table['id_table']] : $table['date']; |
|
| 2556 | + |
|
| 2557 | + $col = $regs[1]; |
|
| 2558 | + if (isset($regs[3]) and $suite = $regs[3]) { |
|
| 2559 | + # Recherche de l'existence du champ date_xxxx, |
|
| 2560 | + # si oui choisir ce champ, sinon choisir xxxx |
|
| 2561 | + |
|
| 2562 | + if (isset($table['field']["date$suite"])) { |
|
| 2563 | + $date_orig = 'date' . $suite; |
|
| 2564 | + } else { |
|
| 2565 | + $date_orig = substr($suite, 1); |
|
| 2566 | + } |
|
| 2567 | + $pred = $date_orig; |
|
| 2568 | + } else { |
|
| 2569 | + if (isset($regs[2]) and $rel = $regs[2]) { |
|
| 2570 | + $pred = 'date'; |
|
| 2571 | + } |
|
| 2572 | + } |
|
| 2573 | + |
|
| 2574 | + $date_compare = "\"' . normaliser_date(" . |
|
| 2575 | + calculer_argument_precedent($idb, $pred, $boucles) . |
|
| 2576 | + ") . '\""; |
|
| 2577 | + |
|
| 2578 | + $col_vraie = $date_orig; |
|
| 2579 | + $date_orig = $boucle->id_table . '.' . $date_orig; |
|
| 2580 | + |
|
| 2581 | + switch ($col) { |
|
| 2582 | + case 'date': |
|
| 2583 | + $col = $date_orig; |
|
| 2584 | + break; |
|
| 2585 | + case 'jour': |
|
| 2586 | + $col = "DAYOFMONTH($date_orig)"; |
|
| 2587 | + break; |
|
| 2588 | + case 'mois': |
|
| 2589 | + $col = "MONTH($date_orig)"; |
|
| 2590 | + break; |
|
| 2591 | + case 'annee': |
|
| 2592 | + $col = "YEAR($date_orig)"; |
|
| 2593 | + break; |
|
| 2594 | + case 'heure': |
|
| 2595 | + $col = "DATE_FORMAT($date_orig, \\'%H:%i\\')"; |
|
| 2596 | + break; |
|
| 2597 | + case 'age': |
|
| 2598 | + $col = calculer_param_date("NOW()", $date_orig); |
|
| 2599 | + $col_vraie = "";// comparer a un int (par defaut) |
|
| 2600 | + break; |
|
| 2601 | + case 'age_relatif': |
|
| 2602 | + $col = calculer_param_date($date_compare, $date_orig); |
|
| 2603 | + $col_vraie = "";// comparer a un int (par defaut) |
|
| 2604 | + break; |
|
| 2605 | + case 'jour_relatif': |
|
| 2606 | + $col = "(TO_DAYS(" . $date_compare . ")-TO_DAYS(" . $date_orig . "))"; |
|
| 2607 | + $col_vraie = "";// comparer a un int (par defaut) |
|
| 2608 | + break; |
|
| 2609 | + case 'mois_relatif': |
|
| 2610 | + $col = "MONTH(" . $date_compare . ")-MONTH(" . |
|
| 2611 | + $date_orig . ")+12*(YEAR(" . $date_compare . |
|
| 2612 | + ")-YEAR(" . $date_orig . "))"; |
|
| 2613 | + $col_vraie = "";// comparer a un int (par defaut) |
|
| 2614 | + break; |
|
| 2615 | + case 'annee_relatif': |
|
| 2616 | + $col = "YEAR(" . $date_compare . ")-YEAR(" . |
|
| 2617 | + $date_orig . ")"; |
|
| 2618 | + $col_vraie = "";// comparer a un int (par defaut) |
|
| 2619 | + break; |
|
| 2620 | + } |
|
| 2621 | + |
|
| 2622 | + return array($col, $col_vraie); |
|
| 2623 | 2623 | } |
| 2624 | 2624 | |
| 2625 | 2625 | /** |
@@ -2638,16 +2638,16 @@ discard block |
||
| 2638 | 2638 | * de colonne SQL et une date. |
| 2639 | 2639 | **/ |
| 2640 | 2640 | function calculer_param_date($date_compare, $date_orig) { |
| 2641 | - if (preg_match(",'\" *\.(.*)\. *\"',", $date_compare, $r)) { |
|
| 2642 | - $init = "'\" . (\$x = $r[1]) . \"'"; |
|
| 2643 | - $date_compare = '\'$x\''; |
|
| 2644 | - } else { |
|
| 2645 | - $init = $date_compare; |
|
| 2646 | - } |
|
| 2647 | - |
|
| 2648 | - return |
|
| 2649 | - // optimisation : mais prevoir le support SQLite avant |
|
| 2650 | - "TIMESTAMPDIFF(HOUR,$date_orig,$init)/24"; |
|
| 2641 | + if (preg_match(",'\" *\.(.*)\. *\"',", $date_compare, $r)) { |
|
| 2642 | + $init = "'\" . (\$x = $r[1]) . \"'"; |
|
| 2643 | + $date_compare = '\'$x\''; |
|
| 2644 | + } else { |
|
| 2645 | + $init = $date_compare; |
|
| 2646 | + } |
|
| 2647 | + |
|
| 2648 | + return |
|
| 2649 | + // optimisation : mais prevoir le support SQLite avant |
|
| 2650 | + "TIMESTAMPDIFF(HOUR,$date_orig,$init)/24"; |
|
| 2651 | 2651 | } |
| 2652 | 2652 | |
| 2653 | 2653 | /** |
@@ -2665,18 +2665,18 @@ discard block |
||
| 2665 | 2665 | * @param Critere $crit Paramètres du critère dans cette boucle |
| 2666 | 2666 | */ |
| 2667 | 2667 | function critere_DATA_source_dist($idb, &$boucles, $crit) { |
| 2668 | - $boucle = &$boucles[$idb]; |
|
| 2668 | + $boucle = &$boucles[$idb]; |
|
| 2669 | 2669 | |
| 2670 | - $args = array(); |
|
| 2671 | - foreach ($crit->param as &$param) { |
|
| 2672 | - array_push($args, |
|
| 2673 | - calculer_liste($param, array(), $boucles, $boucles[$idb]->id_parent)); |
|
| 2674 | - } |
|
| 2670 | + $args = array(); |
|
| 2671 | + foreach ($crit->param as &$param) { |
|
| 2672 | + array_push($args, |
|
| 2673 | + calculer_liste($param, array(), $boucles, $boucles[$idb]->id_parent)); |
|
| 2674 | + } |
|
| 2675 | 2675 | |
| 2676 | - $boucle->hash .= ' |
|
| 2676 | + $boucle->hash .= ' |
|
| 2677 | 2677 | $command[\'sourcemode\'] = ' . array_shift($args) . ";\n"; |
| 2678 | 2678 | |
| 2679 | - $boucle->hash .= ' |
|
| 2679 | + $boucle->hash .= ' |
|
| 2680 | 2680 | $command[\'source\'] = array(' . join(', ', $args) . ");\n"; |
| 2681 | 2681 | } |
| 2682 | 2682 | |
@@ -2693,8 +2693,8 @@ discard block |
||
| 2693 | 2693 | * @param Critere $crit Paramètres du critère dans cette boucle |
| 2694 | 2694 | */ |
| 2695 | 2695 | function critere_DATA_datasource_dist($idb, &$boucles, $crit) { |
| 2696 | - $boucle = &$boucles[$idb]; |
|
| 2697 | - $boucle->hash .= ' |
|
| 2696 | + $boucle = &$boucles[$idb]; |
|
| 2697 | + $boucle->hash .= ' |
|
| 2698 | 2698 | $command[\'source\'] = array(' . calculer_liste($crit->param[0], array(), $boucles, $boucles[$idb]->id_parent) . '); |
| 2699 | 2699 | $command[\'sourcemode\'] = ' . calculer_liste($crit->param[1], array(), $boucles, $boucles[$idb]->id_parent) . ';'; |
| 2700 | 2700 | } |
@@ -2714,8 +2714,8 @@ discard block |
||
| 2714 | 2714 | * @param Critere $crit Paramètres du critère dans cette boucle |
| 2715 | 2715 | */ |
| 2716 | 2716 | function critere_DATA_datacache_dist($idb, &$boucles, $crit) { |
| 2717 | - $boucle = &$boucles[$idb]; |
|
| 2718 | - $boucle->hash .= ' |
|
| 2717 | + $boucle = &$boucles[$idb]; |
|
| 2718 | + $boucle->hash .= ' |
|
| 2719 | 2719 | $command[\'datacache\'] = ' . calculer_liste($crit->param[0], array(), $boucles, $boucles[$idb]->id_parent) . ';'; |
| 2720 | 2720 | } |
| 2721 | 2721 | |
@@ -2731,12 +2731,12 @@ discard block |
||
| 2731 | 2731 | * @param Critere $crit Paramètres du critère dans cette boucle |
| 2732 | 2732 | */ |
| 2733 | 2733 | function critere_php_args_dist($idb, &$boucles, $crit) { |
| 2734 | - $boucle = &$boucles[$idb]; |
|
| 2735 | - $boucle->hash .= '$command[\'args\']=array();'; |
|
| 2736 | - foreach ($crit->param as $param) { |
|
| 2737 | - $boucle->hash .= ' |
|
| 2734 | + $boucle = &$boucles[$idb]; |
|
| 2735 | + $boucle->hash .= '$command[\'args\']=array();'; |
|
| 2736 | + foreach ($crit->param as $param) { |
|
| 2737 | + $boucle->hash .= ' |
|
| 2738 | 2738 | $command[\'args\'][] = ' . calculer_liste($param, array(), $boucles, $boucles[$idb]->id_parent) . ';'; |
| 2739 | - } |
|
| 2739 | + } |
|
| 2740 | 2740 | } |
| 2741 | 2741 | |
| 2742 | 2742 | /** |
@@ -2753,12 +2753,12 @@ discard block |
||
| 2753 | 2753 | * @param Critere $crit Paramètres du critère dans cette boucle |
| 2754 | 2754 | */ |
| 2755 | 2755 | function critere_DATA_liste_dist($idb, &$boucles, $crit) { |
| 2756 | - $boucle = &$boucles[$idb]; |
|
| 2757 | - $boucle->hash .= "\n\t" . '$command[\'liste\'] = array();' . "\n"; |
|
| 2758 | - foreach ($crit->param as $param) { |
|
| 2759 | - $boucle->hash .= "\t" . '$command[\'liste\'][] = ' . calculer_liste($param, array(), $boucles, |
|
| 2760 | - $boucles[$idb]->id_parent) . ";\n"; |
|
| 2761 | - } |
|
| 2756 | + $boucle = &$boucles[$idb]; |
|
| 2757 | + $boucle->hash .= "\n\t" . '$command[\'liste\'] = array();' . "\n"; |
|
| 2758 | + foreach ($crit->param as $param) { |
|
| 2759 | + $boucle->hash .= "\t" . '$command[\'liste\'][] = ' . calculer_liste($param, array(), $boucles, |
|
| 2760 | + $boucles[$idb]->id_parent) . ";\n"; |
|
| 2761 | + } |
|
| 2762 | 2762 | } |
| 2763 | 2763 | |
| 2764 | 2764 | /** |
@@ -2783,12 +2783,12 @@ discard block |
||
| 2783 | 2783 | * @param Critere $crit Paramètres du critère dans cette boucle |
| 2784 | 2784 | */ |
| 2785 | 2785 | function critere_DATA_enum_dist($idb, &$boucles, $crit) { |
| 2786 | - $boucle = &$boucles[$idb]; |
|
| 2787 | - $boucle->hash .= "\n\t" . '$command[\'enum\'] = array();' . "\n"; |
|
| 2788 | - foreach ($crit->param as $param) { |
|
| 2789 | - $boucle->hash .= "\t" . '$command[\'enum\'][] = ' . calculer_liste($param, array(), $boucles, |
|
| 2790 | - $boucles[$idb]->id_parent) . ";\n"; |
|
| 2791 | - } |
|
| 2786 | + $boucle = &$boucles[$idb]; |
|
| 2787 | + $boucle->hash .= "\n\t" . '$command[\'enum\'] = array();' . "\n"; |
|
| 2788 | + foreach ($crit->param as $param) { |
|
| 2789 | + $boucle->hash .= "\t" . '$command[\'enum\'][] = ' . calculer_liste($param, array(), $boucles, |
|
| 2790 | + $boucles[$idb]->id_parent) . ";\n"; |
|
| 2791 | + } |
|
| 2792 | 2792 | } |
| 2793 | 2793 | |
| 2794 | 2794 | /** |
@@ -2803,11 +2803,11 @@ discard block |
||
| 2803 | 2803 | * @param Critere $crit Paramètres du critère dans cette boucle |
| 2804 | 2804 | */ |
| 2805 | 2805 | function critere_DATA_datapath_dist($idb, &$boucles, $crit) { |
| 2806 | - $boucle = &$boucles[$idb]; |
|
| 2807 | - foreach ($crit->param as $param) { |
|
| 2808 | - $boucle->hash .= ' |
|
| 2806 | + $boucle = &$boucles[$idb]; |
|
| 2807 | + foreach ($crit->param as $param) { |
|
| 2808 | + $boucle->hash .= ' |
|
| 2809 | 2809 | $command[\'datapath\'][] = ' . calculer_liste($param, array(), $boucles, $boucles[$idb]->id_parent) . ';'; |
| 2810 | - } |
|
| 2810 | + } |
|
| 2811 | 2811 | } |
| 2812 | 2812 | |
| 2813 | 2813 | |
@@ -2839,20 +2839,20 @@ discard block |
||
| 2839 | 2839 | * @param Critere $crit Paramètres du critère dans cette boucle |
| 2840 | 2840 | */ |
| 2841 | 2841 | function critere_si_dist($idb, &$boucles, $crit) { |
| 2842 | - $boucle = &$boucles[$idb]; |
|
| 2843 | - // il faut initialiser 1 fois le tableau a chaque appel de la boucle |
|
| 2844 | - // (par exemple lorsque notre boucle est appelee dans une autre boucle) |
|
| 2845 | - // mais ne pas l'initialiser n fois si il y a n criteres {si } dans la boucle ! |
|
| 2846 | - $boucle->hash .= "\n\tif (!isset(\$si_init)) { \$command['si'] = array(); \$si_init = true; }\n"; |
|
| 2847 | - if ($crit->param) { |
|
| 2848 | - foreach ($crit->param as $param) { |
|
| 2849 | - $boucle->hash .= "\t\$command['si'][] = " |
|
| 2850 | - . calculer_liste($param, array(), $boucles, $boucles[$idb]->id_parent) . ";\n"; |
|
| 2851 | - } |
|
| 2852 | - // interdire {si 0} aussi ! |
|
| 2853 | - } else { |
|
| 2854 | - $boucle->hash .= '$command[\'si\'][] = 0;'; |
|
| 2855 | - } |
|
| 2842 | + $boucle = &$boucles[$idb]; |
|
| 2843 | + // il faut initialiser 1 fois le tableau a chaque appel de la boucle |
|
| 2844 | + // (par exemple lorsque notre boucle est appelee dans une autre boucle) |
|
| 2845 | + // mais ne pas l'initialiser n fois si il y a n criteres {si } dans la boucle ! |
|
| 2846 | + $boucle->hash .= "\n\tif (!isset(\$si_init)) { \$command['si'] = array(); \$si_init = true; }\n"; |
|
| 2847 | + if ($crit->param) { |
|
| 2848 | + foreach ($crit->param as $param) { |
|
| 2849 | + $boucle->hash .= "\t\$command['si'][] = " |
|
| 2850 | + . calculer_liste($param, array(), $boucles, $boucles[$idb]->id_parent) . ";\n"; |
|
| 2851 | + } |
|
| 2852 | + // interdire {si 0} aussi ! |
|
| 2853 | + } else { |
|
| 2854 | + $boucle->hash .= '$command[\'si\'][] = 0;'; |
|
| 2855 | + } |
|
| 2856 | 2856 | } |
| 2857 | 2857 | |
| 2858 | 2858 | /** |
@@ -2868,8 +2868,8 @@ discard block |
||
| 2868 | 2868 | * @param Critere $crit Paramètres du critère dans cette boucle |
| 2869 | 2869 | */ |
| 2870 | 2870 | function critere_POUR_tableau_dist($idb, &$boucles, $crit) { |
| 2871 | - $boucle = &$boucles[$idb]; |
|
| 2872 | - $boucle->hash .= ' |
|
| 2871 | + $boucle = &$boucles[$idb]; |
|
| 2872 | + $boucle->hash .= ' |
|
| 2873 | 2873 | $command[\'source\'] = array(' . calculer_liste($crit->param[0], array(), $boucles, $boucles[$idb]->id_parent) . '); |
| 2874 | 2874 | $command[\'sourcemode\'] = \'table\';'; |
| 2875 | 2875 | } |
@@ -2890,29 +2890,29 @@ discard block |
||
| 2890 | 2890 | */ |
| 2891 | 2891 | function critere_noeud_dist($idb, &$boucles, $crit) { |
| 2892 | 2892 | |
| 2893 | - $not = $crit->not; |
|
| 2894 | - $boucle = &$boucles[$idb]; |
|
| 2895 | - $primary = $boucle->primary; |
|
| 2893 | + $not = $crit->not; |
|
| 2894 | + $boucle = &$boucles[$idb]; |
|
| 2895 | + $primary = $boucle->primary; |
|
| 2896 | 2896 | |
| 2897 | - if (!$primary or strpos($primary, ',')) { |
|
| 2898 | - erreur_squelette(_T('zbug_doublon_sur_table_sans_cle_primaire'), $boucle); |
|
| 2897 | + if (!$primary or strpos($primary, ',')) { |
|
| 2898 | + erreur_squelette(_T('zbug_doublon_sur_table_sans_cle_primaire'), $boucle); |
|
| 2899 | 2899 | |
| 2900 | - return; |
|
| 2901 | - } |
|
| 2902 | - $table = $boucle->type_requete; |
|
| 2903 | - $table_sql = table_objet_sql(objet_type($table)); |
|
| 2900 | + return; |
|
| 2901 | + } |
|
| 2902 | + $table = $boucle->type_requete; |
|
| 2903 | + $table_sql = table_objet_sql(objet_type($table)); |
|
| 2904 | 2904 | |
| 2905 | - $id_parent = isset($GLOBALS['exceptions_des_tables'][$boucle->id_table]['id_parent']) ? |
|
| 2906 | - $GLOBALS['exceptions_des_tables'][$boucle->id_table]['id_parent'] : |
|
| 2907 | - 'id_parent'; |
|
| 2905 | + $id_parent = isset($GLOBALS['exceptions_des_tables'][$boucle->id_table]['id_parent']) ? |
|
| 2906 | + $GLOBALS['exceptions_des_tables'][$boucle->id_table]['id_parent'] : |
|
| 2907 | + 'id_parent'; |
|
| 2908 | 2908 | |
| 2909 | - $in = "IN"; |
|
| 2910 | - $where = array("'IN'", "'$boucle->id_table." . "$primary'", "'('.sql_get_select('$id_parent', '$table_sql').')'"); |
|
| 2911 | - if ($not) { |
|
| 2912 | - $where = array("'NOT'", $where); |
|
| 2913 | - } |
|
| 2909 | + $in = "IN"; |
|
| 2910 | + $where = array("'IN'", "'$boucle->id_table." . "$primary'", "'('.sql_get_select('$id_parent', '$table_sql').')'"); |
|
| 2911 | + if ($not) { |
|
| 2912 | + $where = array("'NOT'", $where); |
|
| 2913 | + } |
|
| 2914 | 2914 | |
| 2915 | - $boucle->where[] = $where; |
|
| 2915 | + $boucle->where[] = $where; |
|
| 2916 | 2916 | } |
| 2917 | 2917 | |
| 2918 | 2918 | /** |
@@ -2928,8 +2928,8 @@ discard block |
||
| 2928 | 2928 | * @param Critere $crit Paramètres du critère dans cette boucle |
| 2929 | 2929 | */ |
| 2930 | 2930 | function critere_feuille_dist($idb, &$boucles, $crit) { |
| 2931 | - $not = $crit->not; |
|
| 2932 | - $crit->not = $not ? false : true; |
|
| 2933 | - critere_noeud_dist($idb, $boucles, $crit); |
|
| 2934 | - $crit->not = $not; |
|
| 2931 | + $not = $crit->not; |
|
| 2932 | + $crit->not = $not ? false : true; |
|
| 2933 | + critere_noeud_dist($idb, $boucles, $crit); |
|
| 2934 | + $crit->not = $not; |
|
| 2935 | 2935 | } |
@@ -46,10 +46,9 @@ discard block |
||
| 46 | 46 | $not = $crit->not; |
| 47 | 47 | $boucle = &$boucles[$idb]; |
| 48 | 48 | $id_parent = isset($GLOBALS['exceptions_des_tables'][$boucle->id_table]['id_parent']) ? |
| 49 | - $GLOBALS['exceptions_des_tables'][$boucle->id_table]['id_parent'] : |
|
| 50 | - 'id_parent'; |
|
| 49 | + $GLOBALS['exceptions_des_tables'][$boucle->id_table]['id_parent'] : 'id_parent'; |
|
| 51 | 50 | |
| 52 | - $c = array("'='", "'$boucle->id_table." . "$id_parent'", 0); |
|
| 51 | + $c = array("'='", "'$boucle->id_table."."$id_parent'", 0); |
|
| 53 | 52 | $boucle->where[] = ($crit->not ? array("'NOT'", $c) : $c); |
| 54 | 53 | } |
| 55 | 54 | |
@@ -72,10 +71,10 @@ discard block |
||
| 72 | 71 | $id = $boucle->primary; |
| 73 | 72 | |
| 74 | 73 | if ($not or !$id) { |
| 75 | - return (array('zbug_critere_inconnu', array('critere' => $not . $crit->op))); |
|
| 74 | + return (array('zbug_critere_inconnu', array('critere' => $not.$crit->op))); |
|
| 76 | 75 | } |
| 77 | 76 | $arg = kwote(calculer_argument_precedent($idb, $id, $boucles)); |
| 78 | - $boucle->where[] = array("'!='", "'$boucle->id_table." . "$id'", $arg); |
|
| 77 | + $boucle->where[] = array("'!='", "'$boucle->id_table."."$id'", $arg); |
|
| 79 | 78 | } |
| 80 | 79 | |
| 81 | 80 | |
@@ -106,12 +105,12 @@ discard block |
||
| 106 | 105 | $not = ($crit->not ? '' : 'NOT'); |
| 107 | 106 | |
| 108 | 107 | // le doublon s'applique sur un type de boucle (article) |
| 109 | - $nom = "'" . $boucle->type_requete . "'"; |
|
| 108 | + $nom = "'".$boucle->type_requete."'"; |
|
| 110 | 109 | |
| 111 | 110 | // compléter le nom avec un nom précisé {doublons nom} |
| 112 | 111 | // on obtient $nom = "'article' . 'nom'" |
| 113 | 112 | if (isset($crit->param[0])) { |
| 114 | - $nom .= "." . calculer_liste($crit->param[0], array(), $boucles, $boucles[$idb]->id_parent); |
|
| 113 | + $nom .= ".".calculer_liste($crit->param[0], array(), $boucles, $boucles[$idb]->id_parent); |
|
| 115 | 114 | } |
| 116 | 115 | |
| 117 | 116 | // code qui déclarera l'index du stockage de nos doublons (pour éviter une notice PHP) |
@@ -123,13 +122,13 @@ discard block |
||
| 123 | 122 | // $doublons et son index, ici $nom |
| 124 | 123 | |
| 125 | 124 | // debut du code "sql_in('articles.id_article', " |
| 126 | - $debut_in = "sql_in('" . $boucle->id_table . '.' . $primary . "', "; |
|
| 125 | + $debut_in = "sql_in('".$boucle->id_table.'.'.$primary."', "; |
|
| 127 | 126 | // lecture des données du doublon "$doublons[$doublon_index[] = " |
| 128 | 127 | // Attention : boucle->doublons désigne une variable qu'on affecte |
| 129 | - $debut_doub = '$doublons[' . (!$not ? '' : ($boucle->doublons . "[]= ")); |
|
| 128 | + $debut_doub = '$doublons['.(!$not ? '' : ($boucle->doublons."[]= ")); |
|
| 130 | 129 | |
| 131 | 130 | // le debut complet du code des doublons |
| 132 | - $debut_doub = $debut_in . $debut_doub; |
|
| 131 | + $debut_doub = $debut_in.$debut_doub; |
|
| 133 | 132 | |
| 134 | 133 | // nom du doublon "('article' . 'nom')]" |
| 135 | 134 | $fin_doub = "($nom)]"; |
@@ -139,22 +138,22 @@ discard block |
||
| 139 | 138 | foreach ($boucle->where as $k => $w) { |
| 140 | 139 | if (strpos($w[0], $debut_doub) === 0) { |
| 141 | 140 | // fusionner le sql_in (du where) |
| 142 | - $boucle->where[$k][0] = $debut_doub . $fin_doub . ' . ' . substr($w[0], strlen($debut_in)); |
|
| 141 | + $boucle->where[$k][0] = $debut_doub.$fin_doub.' . '.substr($w[0], strlen($debut_in)); |
|
| 143 | 142 | // fusionner l'initialisation (du hash) pour faire plus joli |
| 144 | 143 | $x = strpos($boucle->hash, $init_comment); |
| 145 | 144 | $len = strlen($init_comment); |
| 146 | 145 | $boucle->hash = |
| 147 | - substr($boucle->hash, 0, $x + $len) . $init_code . substr($boucle->hash, $x + $len); |
|
| 146 | + substr($boucle->hash, 0, $x + $len).$init_code.substr($boucle->hash, $x + $len); |
|
| 148 | 147 | |
| 149 | 148 | return; |
| 150 | 149 | } |
| 151 | 150 | } |
| 152 | 151 | |
| 153 | 152 | // mettre l'ensemble dans un tableau pour que ce ne soit pas vu comme une constante |
| 154 | - $boucle->where[] = array($debut_doub . $fin_doub . ", '" . $not . "')"); |
|
| 153 | + $boucle->where[] = array($debut_doub.$fin_doub.", '".$not."')"); |
|
| 155 | 154 | |
| 156 | 155 | // déclarer le doublon s'il n'existe pas encore |
| 157 | - $boucle->hash .= $init_comment . $init_code; |
|
| 156 | + $boucle->hash .= $init_comment.$init_code; |
|
| 158 | 157 | |
| 159 | 158 | |
| 160 | 159 | # la ligne suivante avait l'intention d'eviter une collecte deja faite |
@@ -215,10 +214,10 @@ discard block |
||
| 215 | 214 | $un = $un[0]->texte; |
| 216 | 215 | $deux = $deux[0]->texte; |
| 217 | 216 | if ($deux) { |
| 218 | - $boucles[$idb]->limit = 'intval($Pile[0]["debut' . |
|
| 219 | - $un . |
|
| 220 | - '"]) . ",' . |
|
| 221 | - $deux . |
|
| 217 | + $boucles[$idb]->limit = 'intval($Pile[0]["debut'. |
|
| 218 | + $un. |
|
| 219 | + '"]) . ",'. |
|
| 220 | + $deux. |
|
| 222 | 221 | '"'; |
| 223 | 222 | } else { |
| 224 | 223 | calculer_critere_DEFAUT_dist($idb, $boucles, $crit); |
@@ -281,26 +280,26 @@ discard block |
||
| 281 | 280 | $type = calculer_liste(array($crit->param[1][0]), array(), $boucles, $boucle->id_parent); |
| 282 | 281 | } |
| 283 | 282 | |
| 284 | - $debut = ($type[0] !== "'") ? "'debut'.$type" : ("'debut" . substr($type, 1)); |
|
| 283 | + $debut = ($type[0] !== "'") ? "'debut'.$type" : ("'debut".substr($type, 1)); |
|
| 285 | 284 | $boucle->modificateur['debut_nom'] = $type; |
| 286 | 285 | $partie = |
| 287 | 286 | // tester si le numero de page demande est de la forme '@yyy' |
| 288 | - 'isset($Pile[0][' . $debut . ']) ? $Pile[0][' . $debut . '] : _request(' . $debut . ");\n" |
|
| 287 | + 'isset($Pile[0]['.$debut.']) ? $Pile[0]['.$debut.'] : _request('.$debut.");\n" |
|
| 289 | 288 | . "\tif(substr(\$debut_boucle,0,1)=='@'){\n" |
| 290 | - . "\t\t" . '$debut_boucle = $Pile[0][' . $debut . '] = quete_debut_pagination(\'' . $boucle->primary . '\',$Pile[0][\'@' . $boucle->primary . '\'] = substr($debut_boucle,1),' . $pas . ',$iter);' . "\n" |
|
| 291 | - . "\t\t" . '$iter->seek(0);' . "\n" |
|
| 289 | + . "\t\t".'$debut_boucle = $Pile[0]['.$debut.'] = quete_debut_pagination(\''.$boucle->primary.'\',$Pile[0][\'@'.$boucle->primary.'\'] = substr($debut_boucle,1),'.$pas.',$iter);'."\n" |
|
| 290 | + . "\t\t".'$iter->seek(0);'."\n" |
|
| 292 | 291 | . "\t}\n" |
| 293 | - . "\t" . '$debut_boucle = intval($debut_boucle)'; |
|
| 292 | + . "\t".'$debut_boucle = intval($debut_boucle)'; |
|
| 294 | 293 | |
| 295 | 294 | $boucle->hash .= ' |
| 296 | - $command[\'pagination\'] = array((isset($Pile[0][' . $debut . ']) ? $Pile[0][' . $debut . '] : null), ' . $pas . ');'; |
|
| 295 | + $command[\'pagination\'] = array((isset($Pile[0][' . $debut.']) ? $Pile[0]['.$debut.'] : null), '.$pas.');'; |
|
| 297 | 296 | |
| 298 | 297 | $boucle->total_parties = $pas; |
| 299 | 298 | calculer_parties($boucles, $idb, $partie, 'p+'); |
| 300 | 299 | // ajouter la cle primaire dans le select pour pouvoir gerer la pagination referencee par @id |
| 301 | 300 | // sauf si pas de primaire, ou si primaire composee |
| 302 | 301 | // dans ce cas, on ne sait pas gerer une pagination indirecte |
| 303 | - $t = $boucle->id_table . '.' . $boucle->primary; |
|
| 302 | + $t = $boucle->id_table.'.'.$boucle->primary; |
|
| 304 | 303 | if ($boucle->primary |
| 305 | 304 | and !preg_match('/[,\s]/', $boucle->primary) |
| 306 | 305 | and !in_array($t, $boucle->select) |
@@ -346,24 +345,24 @@ discard block |
||
| 346 | 345 | $boucle->hash .= ' |
| 347 | 346 | // RECHERCHE' |
| 348 | 347 | . ($crit->cond ? ' |
| 349 | - if (!strlen(' . $quoi . ')){ |
|
| 348 | + if (!strlen(' . $quoi.')){ |
|
| 350 | 349 | list($rech_select, $rech_where) = array("0 as points",""); |
| 351 | - } else' : '') . ' |
|
| 350 | + } else' : '').' |
|
| 352 | 351 | { |
| 353 | 352 | $prepare_recherche = charger_fonction(\'prepare_recherche\', \'inc\'); |
| 354 | - list($rech_select, $rech_where) = $prepare_recherche(' . $quoi . ', "' . $boucle->id_table . '", "' . $crit->cond . '","' . $boucle->sql_serveur . '",' . $_modificateur . ',"' . $boucle->primary . '"); |
|
| 353 | + list($rech_select, $rech_where) = $prepare_recherche(' . $quoi.', "'.$boucle->id_table.'", "'.$crit->cond.'","'.$boucle->sql_serveur.'",'.$_modificateur.',"'.$boucle->primary.'"); |
|
| 355 | 354 | } |
| 356 | 355 | '; |
| 357 | 356 | |
| 358 | 357 | |
| 359 | - $t = $boucle->id_table . '.' . $boucle->primary; |
|
| 358 | + $t = $boucle->id_table.'.'.$boucle->primary; |
|
| 360 | 359 | if (!in_array($t, $boucles[$idb]->select)) { |
| 361 | 360 | $boucle->select[] = $t; |
| 362 | 361 | } # pour postgres, neuneu ici |
| 363 | 362 | // jointure uniquement sur le serveur principal |
| 364 | 363 | // (on ne peut joindre une table d'un serveur distant avec la table des resultats du serveur principal) |
| 365 | 364 | if (!$boucle->sql_serveur) { |
| 366 | - $boucle->join['resultats'] = array("'" . $boucle->id_table . "'", "'id'", "'" . $boucle->primary . "'"); |
|
| 365 | + $boucle->join['resultats'] = array("'".$boucle->id_table."'", "'id'", "'".$boucle->primary."'"); |
|
| 367 | 366 | $boucle->from['resultats'] = 'spip_resultats'; |
| 368 | 367 | } |
| 369 | 368 | $boucle->select[] = '$rech_select'; |
@@ -430,7 +429,7 @@ discard block |
||
| 430 | 429 | $c = |
| 431 | 430 | array( |
| 432 | 431 | "'OR'", |
| 433 | - array("'='", "'$table." . "id_trad'", "'$table.$prim'"), |
|
| 432 | + array("'='", "'$table."."id_trad'", "'$table.$prim'"), |
|
| 434 | 433 | array("'='", "'$table.id_trad'", "'0'") |
| 435 | 434 | ); |
| 436 | 435 | $boucle->where[] = ($crit->not ? array("'NOT'", $c) : $c); |
@@ -453,16 +452,15 @@ discard block |
||
| 453 | 452 | $boucle = &$boucles[$idb]; |
| 454 | 453 | $arg = kwote(calculer_argument_precedent($idb, 'id_parent', $boucles)); |
| 455 | 454 | $id_parent = isset($GLOBALS['exceptions_des_tables'][$boucle->id_table]['id_parent']) ? |
| 456 | - $GLOBALS['exceptions_des_tables'][$boucle->id_table]['id_parent'] : |
|
| 457 | - 'id_parent'; |
|
| 458 | - $mparent = $boucle->id_table . '.' . $id_parent; |
|
| 455 | + $GLOBALS['exceptions_des_tables'][$boucle->id_table]['id_parent'] : 'id_parent'; |
|
| 456 | + $mparent = $boucle->id_table.'.'.$id_parent; |
|
| 459 | 457 | |
| 460 | 458 | if ($boucle->type_requete == 'rubriques' or isset($GLOBALS['exceptions_des_tables'][$boucle->id_table]['id_parent'])) { |
| 461 | 459 | $boucle->where[] = array("'='", "'$mparent'", $arg); |
| 462 | 460 | |
| 463 | 461 | } // le cas FORUMS est gere dans le plugin forum, dans la fonction critere_FORUMS_meme_parent_dist() |
| 464 | 462 | else { |
| 465 | - return (array('zbug_critere_inconnu', array('critere' => $crit->op . ' ' . $boucle->type_requete))); |
|
| 463 | + return (array('zbug_critere_inconnu', array('critere' => $crit->op.' '.$boucle->type_requete))); |
|
| 466 | 464 | } |
| 467 | 465 | } |
| 468 | 466 | |
@@ -515,16 +513,15 @@ discard block |
||
| 515 | 513 | if (count(trouver_champs_decomposes($champ, $desc)) > 1) { |
| 516 | 514 | $decompose = decompose_champ_id_objet($champ); |
| 517 | 515 | $champ = array_shift($decompose); |
| 518 | - $boucle->where[] = array("'='", _q($cle . "." . reset($decompose)), '"' . sql_quote(end($decompose)) . '"'); |
|
| 516 | + $boucle->where[] = array("'='", _q($cle.".".reset($decompose)), '"'.sql_quote(end($decompose)).'"'); |
|
| 519 | 517 | } |
| 520 | 518 | } else { |
| 521 | 519 | $cle = $boucle->id_table; |
| 522 | 520 | } |
| 523 | 521 | |
| 524 | - $c = "sql_in('$cle" . ".$champ', calcul_branche_in($arg)" |
|
| 525 | - . ($not ? ", 'NOT'" : '') . ")"; |
|
| 526 | - $boucle->where[] = !$crit->cond ? $c : |
|
| 527 | - ("($arg ? $c : " . ($not ? "'0=1'" : "'1=1'") . ')'); |
|
| 522 | + $c = "sql_in('$cle".".$champ', calcul_branche_in($arg)" |
|
| 523 | + . ($not ? ", 'NOT'" : '').")"; |
|
| 524 | + $boucle->where[] = !$crit->cond ? $c : ("($arg ? $c : ".($not ? "'0=1'" : "'1=1'").')'); |
|
| 528 | 525 | } |
| 529 | 526 | |
| 530 | 527 | /** |
@@ -544,9 +541,9 @@ discard block |
||
| 544 | 541 | $not = ($crit->not ? 'NOT' : ''); |
| 545 | 542 | $serveur = $boucle->sql_serveur; |
| 546 | 543 | |
| 547 | - $c = "sql_in('" . |
|
| 548 | - $boucle->id_table . '.' . $boucle->primary |
|
| 549 | - . "', lister_objets_avec_logos('" . $boucle->primary . "'), '$not', '$serveur')"; |
|
| 544 | + $c = "sql_in('". |
|
| 545 | + $boucle->id_table.'.'.$boucle->primary |
|
| 546 | + . "', lister_objets_avec_logos('".$boucle->primary."'), '$not', '$serveur')"; |
|
| 550 | 547 | |
| 551 | 548 | $boucle->where[] = $c; |
| 552 | 549 | } |
@@ -578,7 +575,7 @@ discard block |
||
| 578 | 575 | $t = table_objet_sql($r[1]); |
| 579 | 576 | $t = array_search($t, $boucles[$idb]->from); |
| 580 | 577 | if ($t) { |
| 581 | - $t .= '.' . $r[2]; |
|
| 578 | + $t .= '.'.$r[2]; |
|
| 582 | 579 | } |
| 583 | 580 | } |
| 584 | 581 | } else { |
@@ -593,7 +590,7 @@ discard block |
||
| 593 | 590 | $boucles[$idb]->select[] = $t; |
| 594 | 591 | } |
| 595 | 592 | } else { |
| 596 | - return (array('zbug_critere_inconnu', array('critere' => $crit->op . ' ?'))); |
|
| 593 | + return (array('zbug_critere_inconnu', array('critere' => $crit->op.' ?'))); |
|
| 597 | 594 | } |
| 598 | 595 | } |
| 599 | 596 | |
@@ -643,26 +640,26 @@ discard block |
||
| 643 | 640 | (false !== $i = strpos($boucle->order[$n - 1], 'ASC')) |
| 644 | 641 | OR (false !== $i = strpos($boucle->order[$n - 1], 'DESC')) |
| 645 | 642 | ) { |
| 646 | - $boucle->order[$n - 1] = substr_replace($boucle->order[$n - 1], "' . " . $boucle->modificateur['collate'] . " . ' ", $i, 0); |
|
| 643 | + $boucle->order[$n - 1] = substr_replace($boucle->order[$n - 1], "' . ".$boucle->modificateur['collate']." . ' ", $i, 0); |
|
| 647 | 644 | } else { |
| 648 | - $boucle->order[$n - 1] .= " . " . $boucle->modificateur['collate']; |
|
| 645 | + $boucle->order[$n - 1] .= " . ".$boucle->modificateur['collate']; |
|
| 649 | 646 | } |
| 650 | 647 | } |
| 651 | 648 | } else { |
| 652 | - return (array('zbug_critere_inconnu', array('critere' => $crit->op . " " . count($boucles[$idb]->order)))); |
|
| 649 | + return (array('zbug_critere_inconnu', array('critere' => $crit->op." ".count($boucles[$idb]->order)))); |
|
| 653 | 650 | } |
| 654 | 651 | } |
| 655 | 652 | |
| 656 | 653 | // http://code.spip.net/@calculer_critere_arg_dynamique |
| 657 | 654 | function calculer_critere_arg_dynamique($idb, &$boucles, $crit, $suffix = '') { |
| 658 | 655 | $boucle = $boucles[$idb]; |
| 659 | - $alt = "('" . $boucle->id_table . '.\' . $x' . $suffix . ')'; |
|
| 660 | - $var = '$champs_' . $idb; |
|
| 656 | + $alt = "('".$boucle->id_table.'.\' . $x'.$suffix.')'; |
|
| 657 | + $var = '$champs_'.$idb; |
|
| 661 | 658 | $desc = (strpos($boucle->in, "static $var =") !== false); |
| 662 | 659 | if (!$desc) { |
| 663 | 660 | $desc = $boucle->show['field']; |
| 664 | 661 | $desc = implode(',', array_map('_q', array_keys($desc))); |
| 665 | - $boucles[$idb]->in .= "\n\tstatic $var = array(" . $desc . ");"; |
|
| 662 | + $boucles[$idb]->in .= "\n\tstatic $var = array(".$desc.");"; |
|
| 666 | 663 | } |
| 667 | 664 | if ($desc) { |
| 668 | 665 | $alt = "(in_array(\$x, $var) ? $alt :(\$x$suffix))"; |
@@ -737,7 +734,7 @@ discard block |
||
| 737 | 734 | $sens = " . ' DESC'"; |
| 738 | 735 | } |
| 739 | 736 | if (isset($boucle->modificateur['collate'])) { |
| 740 | - $collecte = ' . ' . $boucle->modificateur['collate']; |
|
| 737 | + $collecte = ' . '.$boucle->modificateur['collate']; |
|
| 741 | 738 | } |
| 742 | 739 | |
| 743 | 740 | // Pour chaque paramètre du critère |
@@ -759,14 +756,14 @@ discard block |
||
| 759 | 756 | if (preg_match(",^(\w+)[\s]+(.*)$,", $par, $m)) { |
| 760 | 757 | $expression = trim($m[1]); |
| 761 | 758 | $champ = trim($m[2]); |
| 762 | - if (function_exists($f = 'calculer_critere_par_expression_' . $expression)) { |
|
| 759 | + if (function_exists($f = 'calculer_critere_par_expression_'.$expression)) { |
|
| 763 | 760 | $order = $f($idb, $boucles, $crit, $tri, $champ); |
| 764 | 761 | } else { |
| 765 | - return array('zbug_critere_inconnu', array('critere' => $crit->op . " $par")); |
|
| 762 | + return array('zbug_critere_inconnu', array('critere' => $crit->op." $par")); |
|
| 766 | 763 | } |
| 767 | 764 | |
| 768 | 765 | // tris de la forme {par champ} ou {par FONCTION(champ)} |
| 769 | - } elseif (preg_match(",^" . CHAMP_SQL_PLUS_FONC . '$,is', $par, $match)) { |
|
| 766 | + } elseif (preg_match(",^".CHAMP_SQL_PLUS_FONC.'$,is', $par, $match)) { |
|
| 770 | 767 | // {par FONCTION(champ)} |
| 771 | 768 | if (count($match) > 2) { |
| 772 | 769 | $par = substr($match[2], 1, -1); |
@@ -776,7 +773,7 @@ discard block |
||
| 776 | 773 | if ($par == 'hasard') { |
| 777 | 774 | $order = calculer_critere_par_hasard($idb, $boucles, $crit); |
| 778 | 775 | } elseif ($par == 'date' and !empty($boucle->show['date'])) { |
| 779 | - $order = "'" . $boucle->id_table . "." . $boucle->show['date'] . "'"; |
|
| 776 | + $order = "'".$boucle->id_table.".".$boucle->show['date']."'"; |
|
| 780 | 777 | } else { |
| 781 | 778 | // cas général {par champ}, {par table.champ}, ... |
| 782 | 779 | $order = calculer_critere_par_champ($idb, $boucles, $crit, $par); |
@@ -785,7 +782,7 @@ discard block |
||
| 785 | 782 | |
| 786 | 783 | // on ne sait pas traiter… |
| 787 | 784 | else { |
| 788 | - return array('zbug_critere_inconnu', array('critere' => $crit->op . " $par")); |
|
| 785 | + return array('zbug_critere_inconnu', array('critere' => $crit->op." $par")); |
|
| 789 | 786 | } |
| 790 | 787 | |
| 791 | 788 | // En cas d'erreur de squelette retournée par une fonction |
@@ -805,14 +802,14 @@ discard block |
||
| 805 | 802 | |
| 806 | 803 | if ($fct) { |
| 807 | 804 | if (preg_match("/^\s*'(.*)'\s*$/", $order, $r)) { |
| 808 | - $order = "'$fct(" . $r[1] . ")'"; |
|
| 805 | + $order = "'$fct(".$r[1].")'"; |
|
| 809 | 806 | } else { |
| 810 | 807 | $order = "'$fct(' . $order . ')'"; |
| 811 | 808 | } |
| 812 | 809 | } |
| 813 | - $t = $order . $collecte . $sens; |
|
| 810 | + $t = $order.$collecte.$sens; |
|
| 814 | 811 | if (preg_match("/^(.*)'\s*\.\s*'([^']*')$/", $t, $r)) { |
| 815 | - $t = $r[1] . $r[2]; |
|
| 812 | + $t = $r[1].$r[2]; |
|
| 816 | 813 | } |
| 817 | 814 | |
| 818 | 815 | $boucle->order[] = $t; |
@@ -862,16 +859,16 @@ discard block |
||
| 862 | 859 | function calculer_critere_par_expression_num($idb, &$boucles, $crit, $tri, $champ) { |
| 863 | 860 | $_champ = calculer_critere_par_champ($idb, $boucles, $crit, $champ, true); |
| 864 | 861 | if (is_array($_champ)) { |
| 865 | - return array('zbug_critere_inconnu', array('critere' => $crit->op . " num $champ")); |
|
| 862 | + return array('zbug_critere_inconnu', array('critere' => $crit->op." num $champ")); |
|
| 866 | 863 | } |
| 867 | 864 | $boucle = &$boucles[$idb]; |
| 868 | - $texte = '0+' . $_champ; |
|
| 865 | + $texte = '0+'.$_champ; |
|
| 869 | 866 | $suite = calculer_liste($tri, array(), $boucles, $boucle->id_parent); |
| 870 | 867 | if ($suite !== "''") { |
| 871 | - $texte = "\" . ((\$x = $suite) ? ('$texte' . \$x) : '0')" . " . \""; |
|
| 868 | + $texte = "\" . ((\$x = $suite) ? ('$texte' . \$x) : '0')"." . \""; |
|
| 872 | 869 | } |
| 873 | - $as = 'num' . ($boucle->order ? count($boucle->order) : ""); |
|
| 874 | - $boucle->select[] = $texte . " AS $as"; |
|
| 870 | + $as = 'num'.($boucle->order ? count($boucle->order) : ""); |
|
| 871 | + $boucle->select[] = $texte." AS $as"; |
|
| 875 | 872 | $order = "'$as'"; |
| 876 | 873 | return $order; |
| 877 | 874 | } |
@@ -896,16 +893,16 @@ discard block |
||
| 896 | 893 | function calculer_critere_par_expression_sinum($idb, &$boucles, $crit, $tri, $champ) { |
| 897 | 894 | $_champ = calculer_critere_par_champ($idb, $boucles, $crit, $champ, true); |
| 898 | 895 | if (is_array($_champ)) { |
| 899 | - return array('zbug_critere_inconnu', array('critere' => $crit->op . " sinum $champ")); |
|
| 896 | + return array('zbug_critere_inconnu', array('critere' => $crit->op." sinum $champ")); |
|
| 900 | 897 | } |
| 901 | 898 | $boucle = &$boucles[$idb]; |
| 902 | - $texte = '0+' . $_champ; |
|
| 899 | + $texte = '0+'.$_champ; |
|
| 903 | 900 | $suite = calculer_liste($tri, array(), $boucles, $boucle->id_parent); |
| 904 | 901 | if ($suite !== "''") { |
| 905 | - $texte = "\" . ((\$x = $suite) ? ('$texte' . \$x) : '0')" . " . \""; |
|
| 902 | + $texte = "\" . ((\$x = $suite) ? ('$texte' . \$x) : '0')"." . \""; |
|
| 906 | 903 | } |
| 907 | - $as = 'sinum' . ($boucle->order ? count($boucle->order) : ""); |
|
| 908 | - $boucle->select[] = 'CASE (' . $texte . ') WHEN 0 THEN 1 ELSE 0 END AS ' . $as; |
|
| 904 | + $as = 'sinum'.($boucle->order ? count($boucle->order) : ""); |
|
| 905 | + $boucle->select[] = 'CASE ('.$texte.') WHEN 0 THEN 1 ELSE 0 END AS '.$as; |
|
| 909 | 906 | $order = "'$as'"; |
| 910 | 907 | return $order; |
| 911 | 908 | } |
@@ -929,10 +926,10 @@ discard block |
||
| 929 | 926 | function calculer_critere_par_expression_multi($idb, &$boucles, $crit, $tri, $champ) { |
| 930 | 927 | $_champ = calculer_critere_par_champ($idb, $boucles, $crit, $champ, true); |
| 931 | 928 | if (is_array($_champ)) { |
| 932 | - return array('zbug_critere_inconnu', array('critere' => $crit->op . " multi $champ")); |
|
| 929 | + return array('zbug_critere_inconnu', array('critere' => $crit->op." multi $champ")); |
|
| 933 | 930 | } |
| 934 | 931 | $boucle = &$boucles[$idb]; |
| 935 | - $boucle->select[] = "\".sql_multi('" . $_champ . "', \$GLOBALS['spip_lang']).\""; |
|
| 932 | + $boucle->select[] = "\".sql_multi('".$_champ."', \$GLOBALS['spip_lang']).\""; |
|
| 936 | 933 | $order = "'multi'"; |
| 937 | 934 | return $order; |
| 938 | 935 | } |
@@ -952,13 +949,13 @@ discard block |
||
| 952 | 949 | * @param bool $raw Retourne le champ pour le compilateur ("'alias.champ'") ou brut ('alias.champ') |
| 953 | 950 | * @return array|string |
| 954 | 951 | */ |
| 955 | -function calculer_critere_par_champ($idb, &$boucles, $crit, $par, $raw = false) { |
|
| 952 | +function calculer_critere_par_champ($idb, &$boucles, $crit, $par, $raw = false) { |
|
| 956 | 953 | $boucle = &$boucles[$idb]; |
| 957 | 954 | $desc = $boucle->show; |
| 958 | 955 | |
| 959 | 956 | // le champ existe dans la table, pas de souci (le plus commun) |
| 960 | 957 | if (isset($desc['field'][$par])) { |
| 961 | - $par = $boucle->id_table . "." . $par; |
|
| 958 | + $par = $boucle->id_table.".".$par; |
|
| 962 | 959 | } |
| 963 | 960 | // le champ est peut être une jointure |
| 964 | 961 | else { |
@@ -979,24 +976,24 @@ discard block |
||
| 979 | 976 | // Sinon on cherche le champ dans les tables possibles de jointures |
| 980 | 977 | // Si la table est déjà dans le from, on la réutilise. |
| 981 | 978 | if ($infos = chercher_champ_dans_tables($champ, $boucle->from, $boucle->sql_serveur, $table)) { |
| 982 | - $par = $infos['alias'] . "." . $champ; |
|
| 979 | + $par = $infos['alias'].".".$champ; |
|
| 983 | 980 | } elseif ( |
| 984 | 981 | $boucle->jointures_explicites |
| 985 | 982 | and $alias = trouver_jointure_champ($champ, $boucle, explode(' ', $boucle->jointures_explicites), false, $table) |
| 986 | 983 | ) { |
| 987 | - $par = $alias . "." . $champ; |
|
| 984 | + $par = $alias.".".$champ; |
|
| 988 | 985 | } elseif ($alias = trouver_jointure_champ($champ, $boucle, $boucle->jointures, false, $table)) { |
| 989 | - $par = $alias . "." . $champ; |
|
| 986 | + $par = $alias.".".$champ; |
|
| 990 | 987 | // en spécifiant directement l'alias {par L2.titre} (situation hasardeuse tout de même) |
| 991 | 988 | } elseif ( |
| 992 | 989 | $table_alias |
| 993 | 990 | and isset($boucle->from[$table_alias]) |
| 994 | 991 | and $infos = chercher_champ_dans_tables($champ, $boucle->from, $boucle->sql_serveur, $boucle->from[$table_alias]) |
| 995 | 992 | ) { |
| 996 | - $par = $infos['alias'] . "." . $champ; |
|
| 993 | + $par = $infos['alias'].".".$champ; |
|
| 997 | 994 | } elseif ($table) { |
| 998 | 995 | // On avait table + champ, mais on ne les a pas trouvés |
| 999 | - return array('zbug_critere_inconnu', array('critere' => $crit->op . " $par")); |
|
| 996 | + return array('zbug_critere_inconnu', array('critere' => $crit->op." $par")); |
|
| 1000 | 997 | } else { |
| 1001 | 998 | // Sinon tant pis, ca doit etre un champ synthetise (cf points) |
| 1002 | 999 | } |
@@ -1020,7 +1017,7 @@ discard block |
||
| 1020 | 1017 | if (!$t) { |
| 1021 | 1018 | $t = trouver_jointure_champ($champ, $boucle); |
| 1022 | 1019 | } |
| 1023 | - return !$t ? '' : ("'" . $t . '.' . $champ . "'"); |
|
| 1020 | + return !$t ? '' : ("'".$t.'.'.$champ."'"); |
|
| 1024 | 1021 | } |
| 1025 | 1022 | |
| 1026 | 1023 | /** |
@@ -1065,9 +1062,9 @@ discard block |
||
| 1065 | 1062 | $boucle->default_order[] = ' DESC'; |
| 1066 | 1063 | } |
| 1067 | 1064 | } else { |
| 1068 | - $t = $boucle->order[$n - 1] . " . $order"; |
|
| 1065 | + $t = $boucle->order[$n - 1]." . $order"; |
|
| 1069 | 1066 | if (preg_match("/^(.*)'\s*\.\s*'([^']*')$/", $t, $r)) { |
| 1070 | - $t = $r[1] . $r[2]; |
|
| 1067 | + $t = $r[1].$r[2]; |
|
| 1071 | 1068 | } |
| 1072 | 1069 | $boucle->order[$n - 1] = $t; |
| 1073 | 1070 | } |
@@ -1079,7 +1076,7 @@ discard block |
||
| 1079 | 1076 | $params = $crit->param; |
| 1080 | 1077 | |
| 1081 | 1078 | if (count($params) < 1) { |
| 1082 | - return array('zbug_critere_inconnu', array('critere' => $crit->op . " ?")); |
|
| 1079 | + return array('zbug_critere_inconnu', array('critere' => $crit->op." ?")); |
|
| 1083 | 1080 | } |
| 1084 | 1081 | |
| 1085 | 1082 | $boucle = &$boucles[$idb]; |
@@ -1100,7 +1097,7 @@ discard block |
||
| 1100 | 1097 | if ((count($date) == 1) and ($date[0]->type == 'texte')) { |
| 1101 | 1098 | $date = $date[0]->texte; |
| 1102 | 1099 | if (!isset($fields[$date])) { |
| 1103 | - return array('zbug_critere_inconnu', array('critere' => $crit->op . " " . $date)); |
|
| 1100 | + return array('zbug_critere_inconnu', array('critere' => $crit->op." ".$date)); |
|
| 1104 | 1101 | } |
| 1105 | 1102 | } else { |
| 1106 | 1103 | $a = calculer_liste($date, array(), $boucles, $parent); |
@@ -1112,38 +1109,38 @@ discard block |
||
| 1112 | 1109 | $date = "'.(($cond)\n?\$a:\"$defaut\").'"; |
| 1113 | 1110 | } |
| 1114 | 1111 | $annee = $params ? array_shift($params) : ""; |
| 1115 | - $annee = "\n" . 'sprintf("%04d", ($x = ' . |
|
| 1116 | - calculer_liste($annee, array(), $boucles, $parent) . |
|
| 1112 | + $annee = "\n".'sprintf("%04d", ($x = '. |
|
| 1113 | + calculer_liste($annee, array(), $boucles, $parent). |
|
| 1117 | 1114 | ') ? $x : date("Y"))'; |
| 1118 | 1115 | |
| 1119 | 1116 | $mois = $params ? array_shift($params) : ""; |
| 1120 | - $mois = "\n" . 'sprintf("%02d", ($x = ' . |
|
| 1121 | - calculer_liste($mois, array(), $boucles, $parent) . |
|
| 1117 | + $mois = "\n".'sprintf("%02d", ($x = '. |
|
| 1118 | + calculer_liste($mois, array(), $boucles, $parent). |
|
| 1122 | 1119 | ') ? $x : date("m"))'; |
| 1123 | 1120 | |
| 1124 | 1121 | $jour = $params ? array_shift($params) : ""; |
| 1125 | - $jour = "\n" . 'sprintf("%02d", ($x = ' . |
|
| 1126 | - calculer_liste($jour, array(), $boucles, $parent) . |
|
| 1122 | + $jour = "\n".'sprintf("%02d", ($x = '. |
|
| 1123 | + calculer_liste($jour, array(), $boucles, $parent). |
|
| 1127 | 1124 | ') ? $x : date("d"))'; |
| 1128 | 1125 | |
| 1129 | 1126 | $annee2 = $params ? array_shift($params) : ""; |
| 1130 | - $annee2 = "\n" . 'sprintf("%04d", ($x = ' . |
|
| 1131 | - calculer_liste($annee2, array(), $boucles, $parent) . |
|
| 1127 | + $annee2 = "\n".'sprintf("%04d", ($x = '. |
|
| 1128 | + calculer_liste($annee2, array(), $boucles, $parent). |
|
| 1132 | 1129 | ') ? $x : date("Y"))'; |
| 1133 | 1130 | |
| 1134 | 1131 | $mois2 = $params ? array_shift($params) : ""; |
| 1135 | - $mois2 = "\n" . 'sprintf("%02d", ($x = ' . |
|
| 1136 | - calculer_liste($mois2, array(), $boucles, $parent) . |
|
| 1132 | + $mois2 = "\n".'sprintf("%02d", ($x = '. |
|
| 1133 | + calculer_liste($mois2, array(), $boucles, $parent). |
|
| 1137 | 1134 | ') ? $x : date("m"))'; |
| 1138 | 1135 | |
| 1139 | 1136 | $jour2 = $params ? array_shift($params) : ""; |
| 1140 | - $jour2 = "\n" . 'sprintf("%02d", ($x = ' . |
|
| 1141 | - calculer_liste($jour2, array(), $boucles, $parent) . |
|
| 1137 | + $jour2 = "\n".'sprintf("%02d", ($x = '. |
|
| 1138 | + calculer_liste($jour2, array(), $boucles, $parent). |
|
| 1142 | 1139 | ') ? $x : date("d"))'; |
| 1143 | 1140 | |
| 1144 | - $date = $boucle->id_table . ".$date"; |
|
| 1141 | + $date = $boucle->id_table.".$date"; |
|
| 1145 | 1142 | |
| 1146 | - $quote_end = ",'" . $boucle->sql_serveur . "','text'"; |
|
| 1143 | + $quote_end = ",'".$boucle->sql_serveur."','text'"; |
|
| 1147 | 1144 | if ($type == 'jour') { |
| 1148 | 1145 | $boucle->where[] = array( |
| 1149 | 1146 | "'='", |
@@ -1215,14 +1212,13 @@ discard block |
||
| 1215 | 1212 | list($a21, $a22) = calculer_critere_parties_aux($idb, $boucles, $a2); |
| 1216 | 1213 | |
| 1217 | 1214 | if (($op == ',') && (is_numeric($a11) && (is_numeric($a21)))) { |
| 1218 | - $boucle->limit = $a11 . ',' . $a21; |
|
| 1215 | + $boucle->limit = $a11.','.$a21; |
|
| 1219 | 1216 | } else { |
| 1220 | 1217 | // 3 dans {1/3}, {2,3} ou {1,n-3} |
| 1221 | 1218 | $boucle->total_parties = ($a21 != 'n') ? $a21 : $a22; |
| 1222 | 1219 | // 2 dans {2/3}, {2,5}, {n-2,1} |
| 1223 | 1220 | $partie = ($a11 != 'n') ? $a11 : $a12; |
| 1224 | - $mode = (($op == '/') ? '/' : |
|
| 1225 | - (($a11 == 'n') ? '-' : '+') . (($a21 == 'n') ? '-' : '+')); |
|
| 1221 | + $mode = (($op == '/') ? '/' : (($a11 == 'n') ? '-' : '+').(($a21 == 'n') ? '-' : '+')); |
|
| 1226 | 1222 | // cas simple {0,#ENV{truc}} compilons le en LIMIT : |
| 1227 | 1223 | if ($a11 !== 'n' and $a21 !== 'n' and $mode == "++" and $op == ',') { |
| 1228 | 1224 | $boucle->limit = |
@@ -1268,8 +1264,7 @@ discard block |
||
| 1268 | 1264 | // {1/3} |
| 1269 | 1265 | if ($op1 == '/') { |
| 1270 | 1266 | $pmoins1 = is_numeric($debut) ? ($debut - 1) : "($debut-1)"; |
| 1271 | - $totpos = is_numeric($total_parties) ? ($total_parties) : |
|
| 1272 | - "($total_parties ? $total_parties : 1)"; |
|
| 1267 | + $totpos = is_numeric($total_parties) ? ($total_parties) : "($total_parties ? $total_parties : 1)"; |
|
| 1273 | 1268 | $fin = "ceil(($nombre_boucle * $debut )/$totpos) - 1"; |
| 1274 | 1269 | $debut = !$pmoins1 ? 0 : "ceil(($nombre_boucle * $pmoins1)/$totpos);"; |
| 1275 | 1270 | } else { |
@@ -1280,15 +1275,13 @@ discard block |
||
| 1280 | 1275 | |
| 1281 | 1276 | // cas {x,n-1} |
| 1282 | 1277 | if ($op2 == '-') { |
| 1283 | - $fin = '$debut_boucle + ' . $nombre_boucle . ' - ' |
|
| 1284 | - . (is_numeric($total_parties) ? ($total_parties + 1) : |
|
| 1285 | - ($total_parties . ' - 1')); |
|
| 1278 | + $fin = '$debut_boucle + '.$nombre_boucle.' - ' |
|
| 1279 | + . (is_numeric($total_parties) ? ($total_parties + 1) : ($total_parties.' - 1')); |
|
| 1286 | 1280 | } else { |
| 1287 | 1281 | // {x,1} ou {pagination} |
| 1288 | 1282 | $fin = '$debut_boucle' |
| 1289 | 1283 | . (is_numeric($total_parties) ? |
| 1290 | - (($total_parties == 1) ? "" : (' + ' . ($total_parties - 1))) : |
|
| 1291 | - ('+' . $total_parties . ' - 1')); |
|
| 1284 | + (($total_parties == 1) ? "" : (' + '.($total_parties - 1))) : ('+'.$total_parties.' - 1')); |
|
| 1292 | 1285 | } |
| 1293 | 1286 | |
| 1294 | 1287 | // {pagination}, gerer le debut_xx=-1 pour tout voir |
@@ -1306,11 +1299,11 @@ discard block |
||
| 1306 | 1299 | // Utiliser min pour rabattre $fin_boucle sur total_boucle. |
| 1307 | 1300 | |
| 1308 | 1301 | $boucles[$id_boucle]->mode_partie = "\n\t" |
| 1309 | - . '$debut_boucle = ' . $debut . ";\n " |
|
| 1302 | + . '$debut_boucle = '.$debut.";\n " |
|
| 1310 | 1303 | . "\$debut_boucle = intval(\$debut_boucle);\n " |
| 1311 | - . '$fin_boucle = min(' . $fin . ", \$Numrows['$id_boucle']['total'] - 1);\n " |
|
| 1312 | - . '$Numrows[\'' . $id_boucle . "']['grand_total'] = \$Numrows['$id_boucle']['total'];\n " |
|
| 1313 | - . '$Numrows[\'' . $id_boucle . '\']["total"] = max(0,$fin_boucle - $debut_boucle + 1);' |
|
| 1304 | + . '$fin_boucle = min('.$fin.", \$Numrows['$id_boucle']['total'] - 1);\n " |
|
| 1305 | + . '$Numrows[\''.$id_boucle."']['grand_total'] = \$Numrows['$id_boucle']['total'];\n " |
|
| 1306 | + . '$Numrows[\''.$id_boucle.'\']["total"] = max(0,$fin_boucle - $debut_boucle + 1);' |
|
| 1314 | 1307 | . "\n\tif (\$debut_boucle>0" |
| 1315 | 1308 | . " AND \$debut_boucle < \$Numrows['$id_boucle']['grand_total']" |
| 1316 | 1309 | . " AND \$iter->seek(\$debut_boucle,'continue'))" |
@@ -1395,16 +1388,16 @@ discard block |
||
| 1395 | 1388 | // critere personnalise ? |
| 1396 | 1389 | if ( |
| 1397 | 1390 | (!$serveur or |
| 1398 | - ((!function_exists($f = "critere_" . $serveur . "_" . $table . "_" . $critere)) |
|
| 1399 | - and (!function_exists($f = $f . "_dist")) |
|
| 1400 | - and (!function_exists($f = "critere_" . $serveur . "_" . $critere)) |
|
| 1401 | - and (!function_exists($f = $f . "_dist")) |
|
| 1391 | + ((!function_exists($f = "critere_".$serveur."_".$table."_".$critere)) |
|
| 1392 | + and (!function_exists($f = $f."_dist")) |
|
| 1393 | + and (!function_exists($f = "critere_".$serveur."_".$critere)) |
|
| 1394 | + and (!function_exists($f = $f."_dist")) |
|
| 1402 | 1395 | ) |
| 1403 | 1396 | ) |
| 1404 | - and (!function_exists($f = "critere_" . $table . "_" . $critere)) |
|
| 1405 | - and (!function_exists($f = $f . "_dist")) |
|
| 1406 | - and (!function_exists($f = "critere_" . $critere)) |
|
| 1407 | - and (!function_exists($f = $f . "_dist")) |
|
| 1397 | + and (!function_exists($f = "critere_".$table."_".$critere)) |
|
| 1398 | + and (!function_exists($f = $f."_dist")) |
|
| 1399 | + and (!function_exists($f = "critere_".$critere)) |
|
| 1400 | + and (!function_exists($f = $f."_dist")) |
|
| 1408 | 1401 | ) { |
| 1409 | 1402 | // fonction critere standard |
| 1410 | 1403 | $f = $defaut; |
@@ -1437,9 +1430,9 @@ discard block |
||
| 1437 | 1430 | */ |
| 1438 | 1431 | function kwote($lisp, $serveur = '', $type = '') { |
| 1439 | 1432 | if (preg_match(_CODE_QUOTE, $lisp, $r)) { |
| 1440 | - return $r[1] . "\"" . sql_quote(str_replace(array("\\'", "\\\\"), array("'", "\\"), $r[2]), $serveur, $type) . "\""; |
|
| 1433 | + return $r[1]."\"".sql_quote(str_replace(array("\\'", "\\\\"), array("'", "\\"), $r[2]), $serveur, $type)."\""; |
|
| 1441 | 1434 | } else { |
| 1442 | - return "sql_quote($lisp, '$serveur', '" . str_replace("'", "\\'", $type) . "')"; |
|
| 1435 | + return "sql_quote($lisp, '$serveur', '".str_replace("'", "\\'", $type)."')"; |
|
| 1443 | 1436 | } |
| 1444 | 1437 | } |
| 1445 | 1438 | |
@@ -1461,7 +1454,7 @@ discard block |
||
| 1461 | 1454 | function critere_IN_dist($idb, &$boucles, $crit) { |
| 1462 | 1455 | $r = calculer_critere_infixe($idb, $boucles, $crit); |
| 1463 | 1456 | if (!$r) { |
| 1464 | - return (array('zbug_critere_inconnu', array('critere' => $crit->op . " ?"))); |
|
| 1457 | + return (array('zbug_critere_inconnu', array('critere' => $crit->op." ?"))); |
|
| 1465 | 1458 | } |
| 1466 | 1459 | list($arg, $op, $val, $col, $where_complement) = $r; |
| 1467 | 1460 | |
@@ -1488,8 +1481,8 @@ discard block |
||
| 1488 | 1481 | "'NOT'", |
| 1489 | 1482 | array( |
| 1490 | 1483 | "'IN'", |
| 1491 | - "'" . $boucles[$idb]->id_table . "." . $boucles[$idb]->primary . "'", |
|
| 1492 | - array("'SELF'", "'" . $boucles[$idb]->id_table . "." . $boucles[$idb]->primary . "'", $where) |
|
| 1484 | + "'".$boucles[$idb]->id_table.".".$boucles[$idb]->primary."'", |
|
| 1485 | + array("'SELF'", "'".$boucles[$idb]->id_table.".".$boucles[$idb]->primary."'", $where) |
|
| 1493 | 1486 | ) |
| 1494 | 1487 | ); |
| 1495 | 1488 | } |
@@ -1508,22 +1501,22 @@ discard block |
||
| 1508 | 1501 | $descr = $boucles[$idb]->descr; |
| 1509 | 1502 | $cpt = &$num[$descr['nom']][$descr['gram']][$idb]; |
| 1510 | 1503 | |
| 1511 | - $var = '$in' . $cpt++; |
|
| 1504 | + $var = '$in'.$cpt++; |
|
| 1512 | 1505 | $x = "\n\t$var = array();"; |
| 1513 | 1506 | foreach ($val as $k => $v) { |
| 1514 | 1507 | if (preg_match(",^(\n//.*\n)?'(.*)'$,", $v, $r)) { |
| 1515 | 1508 | // optimiser le traitement des constantes |
| 1516 | 1509 | if (is_numeric($r[2])) { |
| 1517 | - $x .= "\n\t$var" . "[]= $r[2];"; |
|
| 1510 | + $x .= "\n\t$var"."[]= $r[2];"; |
|
| 1518 | 1511 | } else { |
| 1519 | - $x .= "\n\t$var" . "[]= " . sql_quote($r[2]) . ";"; |
|
| 1512 | + $x .= "\n\t$var"."[]= ".sql_quote($r[2]).";"; |
|
| 1520 | 1513 | } |
| 1521 | 1514 | } else { |
| 1522 | 1515 | // Pour permettre de passer des tableaux de valeurs |
| 1523 | 1516 | // on repere l'utilisation brute de #ENV**{X}, |
| 1524 | 1517 | // c'est-a-dire sa traduction en ($PILE[0][X]). |
| 1525 | 1518 | // et on deballe mais en rajoutant l'anti XSS |
| 1526 | - $x .= "\n\tif (!(is_array(\$a = ($v))))\n\t\t$var" . "[]= \$a;\n\telse $var = array_merge($var, \$a);"; |
|
| 1519 | + $x .= "\n\tif (!(is_array(\$a = ($v))))\n\t\t$var"."[]= \$a;\n\telse $var = array_merge($var, \$a);"; |
|
| 1527 | 1520 | } |
| 1528 | 1521 | } |
| 1529 | 1522 | |
@@ -1537,7 +1530,7 @@ discard block |
||
| 1537 | 1530 | $boucles[$idb]->default_order[] = "((!\$zqv=sql_quote($var) OR \$zqv===\"''\") ? 0 : ('FIELD($arg,' . \$zqv . ')'))"; |
| 1538 | 1531 | } |
| 1539 | 1532 | |
| 1540 | - return "sql_in('$arg', $var" . ($crit2 == 'NOT' ? ",'NOT'" : "") . ")"; |
|
| 1533 | + return "sql_in('$arg', $var".($crit2 == 'NOT' ? ",'NOT'" : "").")"; |
|
| 1541 | 1534 | } |
| 1542 | 1535 | |
| 1543 | 1536 | /** |
@@ -1610,7 +1603,7 @@ discard block |
||
| 1610 | 1603 | $champs = array_diff($champs, array_keys($boucle->modificateur['criteres'])); |
| 1611 | 1604 | } |
| 1612 | 1605 | // nous aider en mode debug. |
| 1613 | - $boucle->debug[] = "id_ : " . implode(', ', $champs); |
|
| 1606 | + $boucle->debug[] = "id_ : ".implode(', ', $champs); |
|
| 1614 | 1607 | $boucle->modificateur['id_'] = $champs; |
| 1615 | 1608 | |
| 1616 | 1609 | // créer un critère {id_xxx?} de chaque champ retenu |
@@ -1651,7 +1644,7 @@ discard block |
||
| 1651 | 1644 | // Les champs id_xx de la table demandée |
| 1652 | 1645 | $champs = array_filter( |
| 1653 | 1646 | array_keys($desc['field']), |
| 1654 | - function($champ){ |
|
| 1647 | + function($champ) { |
|
| 1655 | 1648 | return |
| 1656 | 1649 | strpos($champ, 'id_') === 0 |
| 1657 | 1650 | or (in_array($champ, array('objet'))); |
@@ -1854,8 +1847,8 @@ discard block |
||
| 1854 | 1847 | "'NOT'", |
| 1855 | 1848 | array( |
| 1856 | 1849 | "'IN'", |
| 1857 | - "'" . $boucles[$idb]->id_table . "." . $boucles[$idb]->primary . "'", |
|
| 1858 | - array("'SELF'", "'" . $boucles[$idb]->id_table . "." . $boucles[$idb]->primary . "'", $where) |
|
| 1850 | + "'".$boucles[$idb]->id_table.".".$boucles[$idb]->primary."'", |
|
| 1851 | + array("'SELF'", "'".$boucles[$idb]->id_table.".".$boucles[$idb]->primary."'", $where) |
|
| 1859 | 1852 | ) |
| 1860 | 1853 | ); |
| 1861 | 1854 | } |
@@ -1866,7 +1859,7 @@ discard block |
||
| 1866 | 1859 | if ($crit->cond) { |
| 1867 | 1860 | $pred = calculer_argument_precedent($idb, $col, $boucles); |
| 1868 | 1861 | if ($col == "date" or $col == "date_redac") { |
| 1869 | - if ($pred == "\$Pile[0]['" . $col . "']") { |
|
| 1862 | + if ($pred == "\$Pile[0]['".$col."']") { |
|
| 1870 | 1863 | $pred = "(\$Pile[0]['{$col}_default']?'':$pred)"; |
| 1871 | 1864 | } |
| 1872 | 1865 | } |
@@ -2027,7 +2020,7 @@ discard block |
||
| 2027 | 2020 | // defaire le quote des int et les passer dans sql_quote avec le bon type de champ si on le connait, int sinon |
| 2028 | 2021 | // prendre en compte le debug ou la valeur arrive avec un commentaire PHP en debut |
| 2029 | 2022 | if (preg_match(",^\\A(\s*//.*?$\s*)?\"'(-?\d+)'\"\\z,ms", $val[0], $r)) { |
| 2030 | - $val[0] = $r[1] . '"' . sql_quote($r[2], $boucle->sql_serveur, $type_cast_quote) . '"'; |
|
| 2023 | + $val[0] = $r[1].'"'.sql_quote($r[2], $boucle->sql_serveur, $type_cast_quote).'"'; |
|
| 2031 | 2024 | } |
| 2032 | 2025 | // sinon expliciter les |
| 2033 | 2026 | // sql_quote(truc) en sql_quote(truc,'',type) |
@@ -2038,22 +2031,22 @@ discard block |
||
| 2038 | 2031 | // sql_quote(truc,'','varchar') |
| 2039 | 2032 | elseif (preg_match('/\Asql_quote[(](.*?)(,[^)]*?)?(,[^)]*(?:\(\d+\)[^)]*)?)?[)]\s*\z/ms', $val[0], $r) |
| 2040 | 2033 | // si pas deja un type |
| 2041 | - and (!isset($r[3]) or !$r[3] or !trim($r[3],", '")) |
|
| 2034 | + and (!isset($r[3]) or !$r[3] or !trim($r[3], ", '")) |
|
| 2042 | 2035 | ) { |
| 2043 | 2036 | $r = $r[1] |
| 2044 | 2037 | . ((isset($r[2]) and $r[2]) ? $r[2] : ",''") |
| 2045 | - . ",'" . addslashes($type_cast_quote) . "'"; |
|
| 2038 | + . ",'".addslashes($type_cast_quote)."'"; |
|
| 2046 | 2039 | $val[0] = "sql_quote($r)"; |
| 2047 | 2040 | } |
| 2048 | - elseif(strpos($val[0], '@@defaultcast@@') !== false |
|
| 2041 | + elseif (strpos($val[0], '@@defaultcast@@') !== false |
|
| 2049 | 2042 | and preg_match("/'@@defaultcast@@'\s*\)\s*\z/ms", $val[0], $r)) { |
| 2050 | - $val[0] = substr($val[0], 0, -strlen($r[0])) . "'" . addslashes($type_cast_quote) . "')"; |
|
| 2043 | + $val[0] = substr($val[0], 0, -strlen($r[0]))."'".addslashes($type_cast_quote)."')"; |
|
| 2051 | 2044 | } |
| 2052 | 2045 | } |
| 2053 | 2046 | |
| 2054 | - if(strpos($val[0], '@@defaultcast@@') !== false |
|
| 2047 | + if (strpos($val[0], '@@defaultcast@@') !== false |
|
| 2055 | 2048 | and preg_match("/'@@defaultcast@@'\s*\)\s*\z/ms", $val[0], $r)) { |
| 2056 | - $val[0] = substr($val[0], 0, -strlen($r[0])) . "'char')"; |
|
| 2049 | + $val[0] = substr($val[0], 0, -strlen($r[0]))."'char')"; |
|
| 2057 | 2050 | } |
| 2058 | 2051 | |
| 2059 | 2052 | // Indicateur pour permettre aux fonctionx boucle_X de modifier |
@@ -2078,7 +2071,7 @@ discard block |
||
| 2078 | 2071 | // inserer le nom de la table SQL devant le nom du champ |
| 2079 | 2072 | if ($table) { |
| 2080 | 2073 | if ($col[0] == "`") { |
| 2081 | - $arg = "$table." . substr($col, 1, -1); |
|
| 2074 | + $arg = "$table.".substr($col, 1, -1); |
|
| 2082 | 2075 | } else { |
| 2083 | 2076 | $arg = "$table.$col"; |
| 2084 | 2077 | } |
@@ -2212,9 +2205,9 @@ discard block |
||
| 2212 | 2205 | **/ |
| 2213 | 2206 | function primary_doublee($decompose, $table) { |
| 2214 | 2207 | $e1 = reset($decompose); |
| 2215 | - $e2 = "sql_quote('" . end($decompose) . "')"; |
|
| 2208 | + $e2 = "sql_quote('".end($decompose)."')"; |
|
| 2216 | 2209 | |
| 2217 | - return array("'='", "'$table." . $e1 . "'", $e2); |
|
| 2210 | + return array("'='", "'$table.".$e1."'", $e2); |
|
| 2218 | 2211 | } |
| 2219 | 2212 | |
| 2220 | 2213 | /** |
@@ -2252,7 +2245,7 @@ discard block |
||
| 2252 | 2245 | if ($checkarrivee |
| 2253 | 2246 | and is_string($checkarrivee) |
| 2254 | 2247 | and $a = table_objet($checkarrivee) |
| 2255 | - and in_array($a . '_liens', $joints) |
|
| 2248 | + and in_array($a.'_liens', $joints) |
|
| 2256 | 2249 | ) { |
| 2257 | 2250 | if ($res = calculer_lien_externe_init($boucle, $joints, $col, $desc, $cond, $checkarrivee)) { |
| 2258 | 2251 | return $res; |
@@ -2272,12 +2265,12 @@ discard block |
||
| 2272 | 2265 | // la table est déjà dans le FROM, on vérifie si le champ est utilisé. |
| 2273 | 2266 | $joindre = false; |
| 2274 | 2267 | foreach ($cols as $col) { |
| 2275 | - $c = '/\b' . $t . ".$col" . '\b/'; |
|
| 2268 | + $c = '/\b'.$t.".$col".'\b/'; |
|
| 2276 | 2269 | if (trouver_champ($c, $boucle->where)) { |
| 2277 | 2270 | $joindre = true; |
| 2278 | 2271 | } else { |
| 2279 | 2272 | // mais ca peut etre dans le FIELD pour le Having |
| 2280 | - $c = "/FIELD.$t" . ".$col,/"; |
|
| 2273 | + $c = "/FIELD.$t".".$col,/"; |
|
| 2281 | 2274 | if (trouver_champ($c, $boucle->select)) { |
| 2282 | 2275 | $joindre = true; |
| 2283 | 2276 | } |
@@ -2324,7 +2317,7 @@ discard block |
||
| 2324 | 2317 | $primary_arrivee = id_table_objet($checkarrivee); |
| 2325 | 2318 | |
| 2326 | 2319 | // [FIXME] $checkarrivee peut-il arriver avec false ???? |
| 2327 | - $intermediaire = trouver_champ_exterieur($primary_arrivee, $joints, $boucle, $checkarrivee . "_liens"); |
|
| 2320 | + $intermediaire = trouver_champ_exterieur($primary_arrivee, $joints, $boucle, $checkarrivee."_liens"); |
|
| 2328 | 2321 | $arrivee = trouver_champ_exterieur($col, $joints, $boucle, $checkarrivee); |
| 2329 | 2322 | |
| 2330 | 2323 | if (!$intermediaire or !$arrivee) { |
@@ -2422,7 +2415,7 @@ discard block |
||
| 2422 | 2415 | } elseif ($crit->cond and ($col == "date" or $col == "date_redac")) { |
| 2423 | 2416 | // un critere conditionnel sur date est traite a part |
| 2424 | 2417 | // car la date est mise d'office par SPIP, |
| 2425 | - $defaut = "(\$Pile[0]['{$col}_default']?'':\$Pile[0]['" . $col . "'])"; |
|
| 2418 | + $defaut = "(\$Pile[0]['{$col}_default']?'':\$Pile[0]['".$col."'])"; |
|
| 2426 | 2419 | } |
| 2427 | 2420 | |
| 2428 | 2421 | $val = calculer_argument_precedent($idb, $val, $boucles, $defaut); |
@@ -2454,7 +2447,7 @@ discard block |
||
| 2454 | 2447 | and (($p == "'") or ($p == '"')) |
| 2455 | 2448 | and $params[0][1]->type == 'champ' |
| 2456 | 2449 | ) { |
| 2457 | - $val[] = "$p\\$p#" . $params[0][1]->nom_champ . "\\$p$p"; |
|
| 2450 | + $val[] = "$p\\$p#".$params[0][1]->nom_champ."\\$p$p"; |
|
| 2458 | 2451 | } else { |
| 2459 | 2452 | foreach ((($op != 'IN') ? $params : calculer_vieux_in($params)) as $p) { |
| 2460 | 2453 | $a = calculer_liste($p, $desc, $boucles, $parent); |
@@ -2470,7 +2463,7 @@ discard block |
||
| 2470 | 2463 | $fct = $args_sql = ''; |
| 2471 | 2464 | // fonction SQL ? |
| 2472 | 2465 | // chercher FONCTION(champ) tel que CONCAT(titre,descriptif) |
| 2473 | - if (preg_match('/^(.*)' . SQL_ARGS . '$/', $col, $m)) { |
|
| 2466 | + if (preg_match('/^(.*)'.SQL_ARGS.'$/', $col, $m)) { |
|
| 2474 | 2467 | $fct = $m[1]; |
| 2475 | 2468 | preg_match('/^\(([^,]*)(.*)\)$/', $m[2], $a); |
| 2476 | 2469 | $col = $a[1]; |
@@ -2560,7 +2553,7 @@ discard block |
||
| 2560 | 2553 | # si oui choisir ce champ, sinon choisir xxxx |
| 2561 | 2554 | |
| 2562 | 2555 | if (isset($table['field']["date$suite"])) { |
| 2563 | - $date_orig = 'date' . $suite; |
|
| 2556 | + $date_orig = 'date'.$suite; |
|
| 2564 | 2557 | } else { |
| 2565 | 2558 | $date_orig = substr($suite, 1); |
| 2566 | 2559 | } |
@@ -2571,12 +2564,12 @@ discard block |
||
| 2571 | 2564 | } |
| 2572 | 2565 | } |
| 2573 | 2566 | |
| 2574 | - $date_compare = "\"' . normaliser_date(" . |
|
| 2575 | - calculer_argument_precedent($idb, $pred, $boucles) . |
|
| 2567 | + $date_compare = "\"' . normaliser_date(". |
|
| 2568 | + calculer_argument_precedent($idb, $pred, $boucles). |
|
| 2576 | 2569 | ") . '\""; |
| 2577 | 2570 | |
| 2578 | 2571 | $col_vraie = $date_orig; |
| 2579 | - $date_orig = $boucle->id_table . '.' . $date_orig; |
|
| 2572 | + $date_orig = $boucle->id_table.'.'.$date_orig; |
|
| 2580 | 2573 | |
| 2581 | 2574 | switch ($col) { |
| 2582 | 2575 | case 'date': |
@@ -2596,26 +2589,26 @@ discard block |
||
| 2596 | 2589 | break; |
| 2597 | 2590 | case 'age': |
| 2598 | 2591 | $col = calculer_param_date("NOW()", $date_orig); |
| 2599 | - $col_vraie = "";// comparer a un int (par defaut) |
|
| 2592 | + $col_vraie = ""; // comparer a un int (par defaut) |
|
| 2600 | 2593 | break; |
| 2601 | 2594 | case 'age_relatif': |
| 2602 | 2595 | $col = calculer_param_date($date_compare, $date_orig); |
| 2603 | - $col_vraie = "";// comparer a un int (par defaut) |
|
| 2596 | + $col_vraie = ""; // comparer a un int (par defaut) |
|
| 2604 | 2597 | break; |
| 2605 | 2598 | case 'jour_relatif': |
| 2606 | - $col = "(TO_DAYS(" . $date_compare . ")-TO_DAYS(" . $date_orig . "))"; |
|
| 2607 | - $col_vraie = "";// comparer a un int (par defaut) |
|
| 2599 | + $col = "(TO_DAYS(".$date_compare.")-TO_DAYS(".$date_orig."))"; |
|
| 2600 | + $col_vraie = ""; // comparer a un int (par defaut) |
|
| 2608 | 2601 | break; |
| 2609 | 2602 | case 'mois_relatif': |
| 2610 | - $col = "MONTH(" . $date_compare . ")-MONTH(" . |
|
| 2611 | - $date_orig . ")+12*(YEAR(" . $date_compare . |
|
| 2612 | - ")-YEAR(" . $date_orig . "))"; |
|
| 2613 | - $col_vraie = "";// comparer a un int (par defaut) |
|
| 2603 | + $col = "MONTH(".$date_compare.")-MONTH(". |
|
| 2604 | + $date_orig.")+12*(YEAR(".$date_compare. |
|
| 2605 | + ")-YEAR(".$date_orig."))"; |
|
| 2606 | + $col_vraie = ""; // comparer a un int (par defaut) |
|
| 2614 | 2607 | break; |
| 2615 | 2608 | case 'annee_relatif': |
| 2616 | - $col = "YEAR(" . $date_compare . ")-YEAR(" . |
|
| 2617 | - $date_orig . ")"; |
|
| 2618 | - $col_vraie = "";// comparer a un int (par defaut) |
|
| 2609 | + $col = "YEAR(".$date_compare.")-YEAR(". |
|
| 2610 | + $date_orig.")"; |
|
| 2611 | + $col_vraie = ""; // comparer a un int (par defaut) |
|
| 2619 | 2612 | break; |
| 2620 | 2613 | } |
| 2621 | 2614 | |
@@ -2674,10 +2667,10 @@ discard block |
||
| 2674 | 2667 | } |
| 2675 | 2668 | |
| 2676 | 2669 | $boucle->hash .= ' |
| 2677 | - $command[\'sourcemode\'] = ' . array_shift($args) . ";\n"; |
|
| 2670 | + $command[\'sourcemode\'] = ' . array_shift($args).";\n"; |
|
| 2678 | 2671 | |
| 2679 | 2672 | $boucle->hash .= ' |
| 2680 | - $command[\'source\'] = array(' . join(', ', $args) . ");\n"; |
|
| 2673 | + $command[\'source\'] = array(' . join(', ', $args).");\n"; |
|
| 2681 | 2674 | } |
| 2682 | 2675 | |
| 2683 | 2676 | |
@@ -2695,8 +2688,8 @@ discard block |
||
| 2695 | 2688 | function critere_DATA_datasource_dist($idb, &$boucles, $crit) { |
| 2696 | 2689 | $boucle = &$boucles[$idb]; |
| 2697 | 2690 | $boucle->hash .= ' |
| 2698 | - $command[\'source\'] = array(' . calculer_liste($crit->param[0], array(), $boucles, $boucles[$idb]->id_parent) . '); |
|
| 2699 | - $command[\'sourcemode\'] = ' . calculer_liste($crit->param[1], array(), $boucles, $boucles[$idb]->id_parent) . ';'; |
|
| 2691 | + $command[\'source\'] = array(' . calculer_liste($crit->param[0], array(), $boucles, $boucles[$idb]->id_parent).'); |
|
| 2692 | + $command[\'sourcemode\'] = ' . calculer_liste($crit->param[1], array(), $boucles, $boucles[$idb]->id_parent).';'; |
|
| 2700 | 2693 | } |
| 2701 | 2694 | |
| 2702 | 2695 | |
@@ -2716,7 +2709,7 @@ discard block |
||
| 2716 | 2709 | function critere_DATA_datacache_dist($idb, &$boucles, $crit) { |
| 2717 | 2710 | $boucle = &$boucles[$idb]; |
| 2718 | 2711 | $boucle->hash .= ' |
| 2719 | - $command[\'datacache\'] = ' . calculer_liste($crit->param[0], array(), $boucles, $boucles[$idb]->id_parent) . ';'; |
|
| 2712 | + $command[\'datacache\'] = ' . calculer_liste($crit->param[0], array(), $boucles, $boucles[$idb]->id_parent).';'; |
|
| 2720 | 2713 | } |
| 2721 | 2714 | |
| 2722 | 2715 | |
@@ -2735,7 +2728,7 @@ discard block |
||
| 2735 | 2728 | $boucle->hash .= '$command[\'args\']=array();'; |
| 2736 | 2729 | foreach ($crit->param as $param) { |
| 2737 | 2730 | $boucle->hash .= ' |
| 2738 | - $command[\'args\'][] = ' . calculer_liste($param, array(), $boucles, $boucles[$idb]->id_parent) . ';'; |
|
| 2731 | + $command[\'args\'][] = ' . calculer_liste($param, array(), $boucles, $boucles[$idb]->id_parent).';'; |
|
| 2739 | 2732 | } |
| 2740 | 2733 | } |
| 2741 | 2734 | |
@@ -2754,10 +2747,10 @@ discard block |
||
| 2754 | 2747 | */ |
| 2755 | 2748 | function critere_DATA_liste_dist($idb, &$boucles, $crit) { |
| 2756 | 2749 | $boucle = &$boucles[$idb]; |
| 2757 | - $boucle->hash .= "\n\t" . '$command[\'liste\'] = array();' . "\n"; |
|
| 2750 | + $boucle->hash .= "\n\t".'$command[\'liste\'] = array();'."\n"; |
|
| 2758 | 2751 | foreach ($crit->param as $param) { |
| 2759 | - $boucle->hash .= "\t" . '$command[\'liste\'][] = ' . calculer_liste($param, array(), $boucles, |
|
| 2760 | - $boucles[$idb]->id_parent) . ";\n"; |
|
| 2752 | + $boucle->hash .= "\t".'$command[\'liste\'][] = '.calculer_liste($param, array(), $boucles, |
|
| 2753 | + $boucles[$idb]->id_parent).";\n"; |
|
| 2761 | 2754 | } |
| 2762 | 2755 | } |
| 2763 | 2756 | |
@@ -2784,10 +2777,10 @@ discard block |
||
| 2784 | 2777 | */ |
| 2785 | 2778 | function critere_DATA_enum_dist($idb, &$boucles, $crit) { |
| 2786 | 2779 | $boucle = &$boucles[$idb]; |
| 2787 | - $boucle->hash .= "\n\t" . '$command[\'enum\'] = array();' . "\n"; |
|
| 2780 | + $boucle->hash .= "\n\t".'$command[\'enum\'] = array();'."\n"; |
|
| 2788 | 2781 | foreach ($crit->param as $param) { |
| 2789 | - $boucle->hash .= "\t" . '$command[\'enum\'][] = ' . calculer_liste($param, array(), $boucles, |
|
| 2790 | - $boucles[$idb]->id_parent) . ";\n"; |
|
| 2782 | + $boucle->hash .= "\t".'$command[\'enum\'][] = '.calculer_liste($param, array(), $boucles, |
|
| 2783 | + $boucles[$idb]->id_parent).";\n"; |
|
| 2791 | 2784 | } |
| 2792 | 2785 | } |
| 2793 | 2786 | |
@@ -2806,7 +2799,7 @@ discard block |
||
| 2806 | 2799 | $boucle = &$boucles[$idb]; |
| 2807 | 2800 | foreach ($crit->param as $param) { |
| 2808 | 2801 | $boucle->hash .= ' |
| 2809 | - $command[\'datapath\'][] = ' . calculer_liste($param, array(), $boucles, $boucles[$idb]->id_parent) . ';'; |
|
| 2802 | + $command[\'datapath\'][] = ' . calculer_liste($param, array(), $boucles, $boucles[$idb]->id_parent).';'; |
|
| 2810 | 2803 | } |
| 2811 | 2804 | } |
| 2812 | 2805 | |
@@ -2847,7 +2840,7 @@ discard block |
||
| 2847 | 2840 | if ($crit->param) { |
| 2848 | 2841 | foreach ($crit->param as $param) { |
| 2849 | 2842 | $boucle->hash .= "\t\$command['si'][] = " |
| 2850 | - . calculer_liste($param, array(), $boucles, $boucles[$idb]->id_parent) . ";\n"; |
|
| 2843 | + . calculer_liste($param, array(), $boucles, $boucles[$idb]->id_parent).";\n"; |
|
| 2851 | 2844 | } |
| 2852 | 2845 | // interdire {si 0} aussi ! |
| 2853 | 2846 | } else { |
@@ -2870,7 +2863,7 @@ discard block |
||
| 2870 | 2863 | function critere_POUR_tableau_dist($idb, &$boucles, $crit) { |
| 2871 | 2864 | $boucle = &$boucles[$idb]; |
| 2872 | 2865 | $boucle->hash .= ' |
| 2873 | - $command[\'source\'] = array(' . calculer_liste($crit->param[0], array(), $boucles, $boucles[$idb]->id_parent) . '); |
|
| 2866 | + $command[\'source\'] = array(' . calculer_liste($crit->param[0], array(), $boucles, $boucles[$idb]->id_parent).'); |
|
| 2874 | 2867 | $command[\'sourcemode\'] = \'table\';'; |
| 2875 | 2868 | } |
| 2876 | 2869 | |
@@ -2903,11 +2896,10 @@ discard block |
||
| 2903 | 2896 | $table_sql = table_objet_sql(objet_type($table)); |
| 2904 | 2897 | |
| 2905 | 2898 | $id_parent = isset($GLOBALS['exceptions_des_tables'][$boucle->id_table]['id_parent']) ? |
| 2906 | - $GLOBALS['exceptions_des_tables'][$boucle->id_table]['id_parent'] : |
|
| 2907 | - 'id_parent'; |
|
| 2899 | + $GLOBALS['exceptions_des_tables'][$boucle->id_table]['id_parent'] : 'id_parent'; |
|
| 2908 | 2900 | |
| 2909 | 2901 | $in = "IN"; |
| 2910 | - $where = array("'IN'", "'$boucle->id_table." . "$primary'", "'('.sql_get_select('$id_parent', '$table_sql').')'"); |
|
| 2902 | + $where = array("'IN'", "'$boucle->id_table."."$primary'", "'('.sql_get_select('$id_parent', '$table_sql').')'"); |
|
| 2911 | 2903 | if ($not) { |
| 2912 | 2904 | $where = array("'NOT'", $where); |
| 2913 | 2905 | } |
@@ -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,104 +41,104 @@ 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 | - $link = @mysqli_connect($host, $login, $pass, '', null, $socket); |
|
| 53 | - } |
|
| 54 | - elseif ($port) { |
|
| 55 | - $link = @mysqli_connect($host, $login, $pass, '', $port); |
|
| 56 | - } |
|
| 57 | - else { |
|
| 58 | - $link = @mysqli_connect($host, $login, $pass); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - if (!$link) { |
|
| 62 | - spip_log('Echec mysqli_connect. Erreur : ' . mysqli_connect_error(), 'mysql.' . _LOG_HS); |
|
| 63 | - |
|
| 64 | - return false; |
|
| 65 | - } |
|
| 66 | - $last = ''; |
|
| 67 | - if (!$db) { |
|
| 68 | - $ok = $link; |
|
| 69 | - $db = 'spip'; |
|
| 70 | - } else { |
|
| 71 | - $ok = mysqli_select_db($link, $db); |
|
| 72 | - if (defined('_MYSQL_SET_SQL_MODE') |
|
| 73 | - or defined('_MYSQL_SQL_MODE_TEXT_NOT_NULL') // compatibilite |
|
| 74 | - ) { |
|
| 75 | - mysqli_query($link, $last = "set sql_mode=''"); |
|
| 76 | - } |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - spip_log("Connexion MySQLi vers $host, base $db, prefixe $prefixe " . ($ok ? "operationnelle" : 'impossible'), |
|
| 80 | - _LOG_DEBUG); |
|
| 81 | - |
|
| 82 | - return !$ok ? false : array( |
|
| 83 | - 'db' => $db, |
|
| 84 | - 'last' => $last, |
|
| 85 | - 'prefixe' => $prefixe ? $prefixe : $db, |
|
| 86 | - 'link' => $link, |
|
| 87 | - 'total_requetes' => 0, |
|
| 88 | - ); |
|
| 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 | + $link = @mysqli_connect($host, $login, $pass, '', null, $socket); |
|
| 53 | + } |
|
| 54 | + elseif ($port) { |
|
| 55 | + $link = @mysqli_connect($host, $login, $pass, '', $port); |
|
| 56 | + } |
|
| 57 | + else { |
|
| 58 | + $link = @mysqli_connect($host, $login, $pass); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + if (!$link) { |
|
| 62 | + spip_log('Echec mysqli_connect. Erreur : ' . mysqli_connect_error(), 'mysql.' . _LOG_HS); |
|
| 63 | + |
|
| 64 | + return false; |
|
| 65 | + } |
|
| 66 | + $last = ''; |
|
| 67 | + if (!$db) { |
|
| 68 | + $ok = $link; |
|
| 69 | + $db = 'spip'; |
|
| 70 | + } else { |
|
| 71 | + $ok = mysqli_select_db($link, $db); |
|
| 72 | + if (defined('_MYSQL_SET_SQL_MODE') |
|
| 73 | + or defined('_MYSQL_SQL_MODE_TEXT_NOT_NULL') // compatibilite |
|
| 74 | + ) { |
|
| 75 | + mysqli_query($link, $last = "set sql_mode=''"); |
|
| 76 | + } |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + spip_log("Connexion MySQLi vers $host, base $db, prefixe $prefixe " . ($ok ? "operationnelle" : 'impossible'), |
|
| 80 | + _LOG_DEBUG); |
|
| 81 | + |
|
| 82 | + return !$ok ? false : array( |
|
| 83 | + 'db' => $db, |
|
| 84 | + 'last' => $last, |
|
| 85 | + 'prefixe' => $prefixe ? $prefixe : $db, |
|
| 86 | + 'link' => $link, |
|
| 87 | + 'total_requetes' => 0, |
|
| 88 | + ); |
|
| 89 | 89 | } |
| 90 | 90 | |
| 91 | 91 | |
| 92 | 92 | $GLOBALS['spip_mysql_functions_1'] = array( |
| 93 | - 'alter' => 'spip_mysql_alter', |
|
| 94 | - 'count' => 'spip_mysql_count', |
|
| 95 | - 'countsel' => 'spip_mysql_countsel', |
|
| 96 | - 'create' => 'spip_mysql_create', |
|
| 97 | - 'create_base' => 'spip_mysql_create_base', |
|
| 98 | - 'create_view' => 'spip_mysql_create_view', |
|
| 99 | - 'date_proche' => 'spip_mysql_date_proche', |
|
| 100 | - 'delete' => 'spip_mysql_delete', |
|
| 101 | - 'drop_table' => 'spip_mysql_drop_table', |
|
| 102 | - 'drop_view' => 'spip_mysql_drop_view', |
|
| 103 | - 'errno' => 'spip_mysql_errno', |
|
| 104 | - 'error' => 'spip_mysql_error', |
|
| 105 | - 'explain' => 'spip_mysql_explain', |
|
| 106 | - 'fetch' => 'spip_mysql_fetch', |
|
| 107 | - 'seek' => 'spip_mysql_seek', |
|
| 108 | - 'free' => 'spip_mysql_free', |
|
| 109 | - 'hex' => 'spip_mysql_hex', |
|
| 110 | - 'in' => 'spip_mysql_in', |
|
| 111 | - 'insert' => 'spip_mysql_insert', |
|
| 112 | - 'insertq' => 'spip_mysql_insertq', |
|
| 113 | - 'insertq_multi' => 'spip_mysql_insertq_multi', |
|
| 114 | - 'listdbs' => 'spip_mysql_listdbs', |
|
| 115 | - 'multi' => 'spip_mysql_multi', |
|
| 116 | - 'optimize' => 'spip_mysql_optimize', |
|
| 117 | - 'query' => 'spip_mysql_query', |
|
| 118 | - 'quote' => 'spip_mysql_quote', |
|
| 119 | - 'replace' => 'spip_mysql_replace', |
|
| 120 | - 'replace_multi' => 'spip_mysql_replace_multi', |
|
| 121 | - 'repair' => 'spip_mysql_repair', |
|
| 122 | - 'select' => 'spip_mysql_select', |
|
| 123 | - 'selectdb' => 'spip_mysql_selectdb', |
|
| 124 | - 'set_charset' => 'spip_mysql_set_charset', |
|
| 125 | - 'get_charset' => 'spip_mysql_get_charset', |
|
| 126 | - 'showbase' => 'spip_mysql_showbase', |
|
| 127 | - 'showtable' => 'spip_mysql_showtable', |
|
| 128 | - 'update' => 'spip_mysql_update', |
|
| 129 | - 'updateq' => 'spip_mysql_updateq', |
|
| 130 | - |
|
| 131 | - // association de chaque nom http d'un charset aux couples MySQL |
|
| 132 | - 'charsets' => array( |
|
| 133 | - 'cp1250' => array('charset' => 'cp1250', 'collation' => 'cp1250_general_ci'), |
|
| 134 | - 'cp1251' => array('charset' => 'cp1251', 'collation' => 'cp1251_general_ci'), |
|
| 135 | - 'cp1256' => array('charset' => 'cp1256', 'collation' => 'cp1256_general_ci'), |
|
| 136 | - 'iso-8859-1' => array('charset' => 'latin1', 'collation' => 'latin1_swedish_ci'), |
|
| 93 | + 'alter' => 'spip_mysql_alter', |
|
| 94 | + 'count' => 'spip_mysql_count', |
|
| 95 | + 'countsel' => 'spip_mysql_countsel', |
|
| 96 | + 'create' => 'spip_mysql_create', |
|
| 97 | + 'create_base' => 'spip_mysql_create_base', |
|
| 98 | + 'create_view' => 'spip_mysql_create_view', |
|
| 99 | + 'date_proche' => 'spip_mysql_date_proche', |
|
| 100 | + 'delete' => 'spip_mysql_delete', |
|
| 101 | + 'drop_table' => 'spip_mysql_drop_table', |
|
| 102 | + 'drop_view' => 'spip_mysql_drop_view', |
|
| 103 | + 'errno' => 'spip_mysql_errno', |
|
| 104 | + 'error' => 'spip_mysql_error', |
|
| 105 | + 'explain' => 'spip_mysql_explain', |
|
| 106 | + 'fetch' => 'spip_mysql_fetch', |
|
| 107 | + 'seek' => 'spip_mysql_seek', |
|
| 108 | + 'free' => 'spip_mysql_free', |
|
| 109 | + 'hex' => 'spip_mysql_hex', |
|
| 110 | + 'in' => 'spip_mysql_in', |
|
| 111 | + 'insert' => 'spip_mysql_insert', |
|
| 112 | + 'insertq' => 'spip_mysql_insertq', |
|
| 113 | + 'insertq_multi' => 'spip_mysql_insertq_multi', |
|
| 114 | + 'listdbs' => 'spip_mysql_listdbs', |
|
| 115 | + 'multi' => 'spip_mysql_multi', |
|
| 116 | + 'optimize' => 'spip_mysql_optimize', |
|
| 117 | + 'query' => 'spip_mysql_query', |
|
| 118 | + 'quote' => 'spip_mysql_quote', |
|
| 119 | + 'replace' => 'spip_mysql_replace', |
|
| 120 | + 'replace_multi' => 'spip_mysql_replace_multi', |
|
| 121 | + 'repair' => 'spip_mysql_repair', |
|
| 122 | + 'select' => 'spip_mysql_select', |
|
| 123 | + 'selectdb' => 'spip_mysql_selectdb', |
|
| 124 | + 'set_charset' => 'spip_mysql_set_charset', |
|
| 125 | + 'get_charset' => 'spip_mysql_get_charset', |
|
| 126 | + 'showbase' => 'spip_mysql_showbase', |
|
| 127 | + 'showtable' => 'spip_mysql_showtable', |
|
| 128 | + 'update' => 'spip_mysql_update', |
|
| 129 | + 'updateq' => 'spip_mysql_updateq', |
|
| 130 | + |
|
| 131 | + // association de chaque nom http d'un charset aux couples MySQL |
|
| 132 | + 'charsets' => array( |
|
| 133 | + 'cp1250' => array('charset' => 'cp1250', 'collation' => 'cp1250_general_ci'), |
|
| 134 | + 'cp1251' => array('charset' => 'cp1251', 'collation' => 'cp1251_general_ci'), |
|
| 135 | + 'cp1256' => array('charset' => 'cp1256', 'collation' => 'cp1256_general_ci'), |
|
| 136 | + 'iso-8859-1' => array('charset' => 'latin1', 'collation' => 'latin1_swedish_ci'), |
|
| 137 | 137 | //'iso-8859-6'=>array('charset'=>'latin1','collation'=>'latin1_swedish_ci'), |
| 138 | - 'iso-8859-9' => array('charset' => 'latin5', 'collation' => 'latin5_turkish_ci'), |
|
| 138 | + 'iso-8859-9' => array('charset' => 'latin5', 'collation' => 'latin5_turkish_ci'), |
|
| 139 | 139 | //'iso-8859-15'=>array('charset'=>'latin1','collation'=>'latin1_swedish_ci'), |
| 140 | - 'utf-8' => array('charset' => 'utf8', 'collation' => 'utf8_general_ci') |
|
| 141 | - ) |
|
| 140 | + 'utf-8' => array('charset' => 'utf8', 'collation' => 'utf8_general_ci') |
|
| 141 | + ) |
|
| 142 | 142 | ); |
| 143 | 143 | |
| 144 | 144 | |
@@ -149,9 +149,9 @@ discard block |
||
| 149 | 149 | * @return Object Information de connexion pour mysqli |
| 150 | 150 | */ |
| 151 | 151 | function _mysql_link($serveur = '') { |
| 152 | - $link = &$GLOBALS['connexions'][$serveur ? $serveur : 0]['link']; |
|
| 152 | + $link = &$GLOBALS['connexions'][$serveur ? $serveur : 0]['link']; |
|
| 153 | 153 | |
| 154 | - return $link; |
|
| 154 | + return $link; |
|
| 155 | 155 | } |
| 156 | 156 | |
| 157 | 157 | |
@@ -164,10 +164,10 @@ discard block |
||
| 164 | 164 | * @return resource Ressource de résultats pour fetch() |
| 165 | 165 | */ |
| 166 | 166 | function spip_mysql_set_charset($charset, $serveur = '', $requeter = true) { |
| 167 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 168 | - spip_log("changement de charset sql : " . "SET NAMES " . _q($charset), _LOG_DEBUG); |
|
| 167 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 168 | + spip_log("changement de charset sql : " . "SET NAMES " . _q($charset), _LOG_DEBUG); |
|
| 169 | 169 | |
| 170 | - return mysqli_query($connexion['link'], $connexion['last'] = "SET NAMES " . _q($charset)); |
|
| 170 | + return mysqli_query($connexion['link'], $connexion['last'] = "SET NAMES " . _q($charset)); |
|
| 171 | 171 | } |
| 172 | 172 | |
| 173 | 173 | |
@@ -180,11 +180,11 @@ discard block |
||
| 180 | 180 | * @return array Description du charset (son nom est dans 'charset') |
| 181 | 181 | */ |
| 182 | 182 | function spip_mysql_get_charset($charset = array(), $serveur = '', $requeter = true) { |
| 183 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 184 | - $connexion['last'] = $c = "SHOW CHARACTER SET" |
|
| 185 | - . (!$charset ? '' : (" LIKE " . _q($charset['charset']))); |
|
| 183 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 184 | + $connexion['last'] = $c = "SHOW CHARACTER SET" |
|
| 185 | + . (!$charset ? '' : (" LIKE " . _q($charset['charset']))); |
|
| 186 | 186 | |
| 187 | - return spip_mysql_fetch(mysqli_query($connexion['link'], $c), null, $serveur); |
|
| 187 | + return spip_mysql_fetch(mysqli_query($connexion['link'], $c), null, $serveur); |
|
| 188 | 188 | } |
| 189 | 189 | |
| 190 | 190 | |
@@ -199,7 +199,7 @@ discard block |
||
| 199 | 199 | * @return Resource Ressource pour fetch() |
| 200 | 200 | **/ |
| 201 | 201 | function spip_query_db($query, $serveur = '', $requeter = true) { |
| 202 | - return spip_mysql_query($query, $serveur, $requeter); |
|
| 202 | + return spip_mysql_query($query, $serveur, $requeter); |
|
| 203 | 203 | } |
| 204 | 204 | |
| 205 | 205 | |
@@ -216,61 +216,61 @@ discard block |
||
| 216 | 216 | */ |
| 217 | 217 | function spip_mysql_query($query, $serveur = '', $requeter = true) { |
| 218 | 218 | |
| 219 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 220 | - $prefixe = $connexion['prefixe']; |
|
| 221 | - $link = $connexion['link']; |
|
| 222 | - $db = $connexion['db']; |
|
| 223 | - |
|
| 224 | - $query = _mysql_traite_query($query, $db, $prefixe); |
|
| 225 | - |
|
| 226 | - // renvoyer la requete inerte si demandee |
|
| 227 | - if (!$requeter) { |
|
| 228 | - return $query; |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - if (isset($_GET['var_profile'])) { |
|
| 232 | - include_spip('public/tracer'); |
|
| 233 | - $t = trace_query_start(); |
|
| 234 | - } else { |
|
| 235 | - $t = 0; |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - $connexion['last'] = $query; |
|
| 239 | - $connexion['total_requetes']++; |
|
| 240 | - |
|
| 241 | - // ajouter un debug utile dans log/mysql-slow.log ? |
|
| 242 | - $debug = ''; |
|
| 243 | - if (defined('_DEBUG_SLOW_QUERIES') and _DEBUG_SLOW_QUERIES) { |
|
| 244 | - if (isset($GLOBALS['debug']['aucasou'])) { |
|
| 245 | - list(, $id, , $infos) = $GLOBALS['debug']['aucasou']; |
|
| 246 | - $debug .= "BOUCLE$id @ " . (isset($infos[0]) ? $infos[0] : '') . " | "; |
|
| 247 | - } |
|
| 248 | - $debug .= $_SERVER['REQUEST_URI'] . ' + ' . $GLOBALS['ip']; |
|
| 249 | - $debug = ' /* ' . mysqli_real_escape_string($link, str_replace('*/', '@/', $debug)) . ' */'; |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - $r = mysqli_query($link, $query . $debug); |
|
| 253 | - |
|
| 254 | - //Eviter de propager le GoneAway sur les autres requetes d'un même processus PHP |
|
| 255 | - if ($e = spip_mysql_errno($serveur)) { // Log d'un Gone Away |
|
| 256 | - if ($e == 2006) { //Si Gone Away on relance une connexion vierge |
|
| 257 | - //Fermer la connexion defaillante |
|
| 258 | - mysqli_close($connexion['link']); |
|
| 259 | - unset($GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]); |
|
| 260 | - //Relancer une connexion vierge |
|
| 261 | - spip_connect($serveur); |
|
| 262 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 263 | - $link = $connexion['link']; |
|
| 264 | - //On retente au cas où |
|
| 265 | - $r = mysqli_query($link, $query . $debug); |
|
| 266 | - } |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - // Log de l'erreur eventuelle |
|
| 270 | - if ($e = spip_mysql_errno($serveur)) { |
|
| 271 | - $e .= spip_mysql_error($query, $serveur); |
|
| 272 | - } // et du fautif |
|
| 273 | - return $t ? trace_query_end($query, $t, $r, $e, $serveur) : $r; |
|
| 219 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 220 | + $prefixe = $connexion['prefixe']; |
|
| 221 | + $link = $connexion['link']; |
|
| 222 | + $db = $connexion['db']; |
|
| 223 | + |
|
| 224 | + $query = _mysql_traite_query($query, $db, $prefixe); |
|
| 225 | + |
|
| 226 | + // renvoyer la requete inerte si demandee |
|
| 227 | + if (!$requeter) { |
|
| 228 | + return $query; |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + if (isset($_GET['var_profile'])) { |
|
| 232 | + include_spip('public/tracer'); |
|
| 233 | + $t = trace_query_start(); |
|
| 234 | + } else { |
|
| 235 | + $t = 0; |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + $connexion['last'] = $query; |
|
| 239 | + $connexion['total_requetes']++; |
|
| 240 | + |
|
| 241 | + // ajouter un debug utile dans log/mysql-slow.log ? |
|
| 242 | + $debug = ''; |
|
| 243 | + if (defined('_DEBUG_SLOW_QUERIES') and _DEBUG_SLOW_QUERIES) { |
|
| 244 | + if (isset($GLOBALS['debug']['aucasou'])) { |
|
| 245 | + list(, $id, , $infos) = $GLOBALS['debug']['aucasou']; |
|
| 246 | + $debug .= "BOUCLE$id @ " . (isset($infos[0]) ? $infos[0] : '') . " | "; |
|
| 247 | + } |
|
| 248 | + $debug .= $_SERVER['REQUEST_URI'] . ' + ' . $GLOBALS['ip']; |
|
| 249 | + $debug = ' /* ' . mysqli_real_escape_string($link, str_replace('*/', '@/', $debug)) . ' */'; |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + $r = mysqli_query($link, $query . $debug); |
|
| 253 | + |
|
| 254 | + //Eviter de propager le GoneAway sur les autres requetes d'un même processus PHP |
|
| 255 | + if ($e = spip_mysql_errno($serveur)) { // Log d'un Gone Away |
|
| 256 | + if ($e == 2006) { //Si Gone Away on relance une connexion vierge |
|
| 257 | + //Fermer la connexion defaillante |
|
| 258 | + mysqli_close($connexion['link']); |
|
| 259 | + unset($GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]); |
|
| 260 | + //Relancer une connexion vierge |
|
| 261 | + spip_connect($serveur); |
|
| 262 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 263 | + $link = $connexion['link']; |
|
| 264 | + //On retente au cas où |
|
| 265 | + $r = mysqli_query($link, $query . $debug); |
|
| 266 | + } |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + // Log de l'erreur eventuelle |
|
| 270 | + if ($e = spip_mysql_errno($serveur)) { |
|
| 271 | + $e .= spip_mysql_error($query, $serveur); |
|
| 272 | + } // et du fautif |
|
| 273 | + return $t ? trace_query_end($query, $t, $r, $e, $serveur) : $r; |
|
| 274 | 274 | } |
| 275 | 275 | |
| 276 | 276 | /** |
@@ -285,12 +285,12 @@ discard block |
||
| 285 | 285 | * - array : Tableau décrivant requête et temps d'exécution si var_profile actif pour tracer. |
| 286 | 286 | */ |
| 287 | 287 | function spip_mysql_alter($query, $serveur = '', $requeter = true) { |
| 288 | - // ici on supprime les ` entourant le nom de table pour permettre |
|
| 289 | - // la transposition du prefixe, compte tenu que les plugins ont la mauvaise habitude |
|
| 290 | - // d'utiliser ceux-ci, copie-colle de phpmyadmin |
|
| 291 | - $query = preg_replace(",^TABLE\s*`([^`]*)`,i", "TABLE \\1", $query); |
|
| 288 | + // ici on supprime les ` entourant le nom de table pour permettre |
|
| 289 | + // la transposition du prefixe, compte tenu que les plugins ont la mauvaise habitude |
|
| 290 | + // d'utiliser ceux-ci, copie-colle de phpmyadmin |
|
| 291 | + $query = preg_replace(",^TABLE\s*`([^`]*)`,i", "TABLE \\1", $query); |
|
| 292 | 292 | |
| 293 | - return spip_mysql_query("ALTER " . $query, $serveur, $requeter); # i.e. que PG se debrouille |
|
| 293 | + return spip_mysql_query("ALTER " . $query, $serveur, $requeter); # i.e. que PG se debrouille |
|
| 294 | 294 | } |
| 295 | 295 | |
| 296 | 296 | |
@@ -303,9 +303,9 @@ discard block |
||
| 303 | 303 | * @return bool Toujours true |
| 304 | 304 | */ |
| 305 | 305 | function spip_mysql_optimize($table, $serveur = '', $requeter = true) { |
| 306 | - spip_mysql_query("OPTIMIZE TABLE " . $table); |
|
| 306 | + spip_mysql_query("OPTIMIZE TABLE " . $table); |
|
| 307 | 307 | |
| 308 | - return true; |
|
| 308 | + return true; |
|
| 309 | 309 | } |
| 310 | 310 | |
| 311 | 311 | |
@@ -318,18 +318,18 @@ discard block |
||
| 318 | 318 | * @return array Tableau de l'explication |
| 319 | 319 | */ |
| 320 | 320 | function spip_mysql_explain($query, $serveur = '', $requeter = true) { |
| 321 | - if (strpos(ltrim($query), 'SELECT') !== 0) { |
|
| 322 | - return array(); |
|
| 323 | - } |
|
| 324 | - $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 325 | - $prefixe = $connexion['prefixe']; |
|
| 326 | - $link = $connexion['link']; |
|
| 327 | - $db = $connexion['db']; |
|
| 328 | - |
|
| 329 | - $query = 'EXPLAIN ' . _mysql_traite_query($query, $db, $prefixe); |
|
| 330 | - $r = mysqli_query($link, $query); |
|
| 331 | - |
|
| 332 | - return spip_mysql_fetch($r, null, $serveur); |
|
| 321 | + if (strpos(ltrim($query), 'SELECT') !== 0) { |
|
| 322 | + return array(); |
|
| 323 | + } |
|
| 324 | + $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 325 | + $prefixe = $connexion['prefixe']; |
|
| 326 | + $link = $connexion['link']; |
|
| 327 | + $db = $connexion['db']; |
|
| 328 | + |
|
| 329 | + $query = 'EXPLAIN ' . _mysql_traite_query($query, $db, $prefixe); |
|
| 330 | + $r = mysqli_query($link, $query); |
|
| 331 | + |
|
| 332 | + return spip_mysql_fetch($r, null, $serveur); |
|
| 333 | 333 | } |
| 334 | 334 | |
| 335 | 335 | |
@@ -358,35 +358,35 @@ discard block |
||
| 358 | 358 | * - array : Tableau décrivant requête et temps d'exécution si var_profile actif pour tracer. |
| 359 | 359 | */ |
| 360 | 360 | function spip_mysql_select( |
| 361 | - $select, |
|
| 362 | - $from, |
|
| 363 | - $where = '', |
|
| 364 | - $groupby = '', |
|
| 365 | - $orderby = '', |
|
| 366 | - $limit = '', |
|
| 367 | - $having = '', |
|
| 368 | - $serveur = '', |
|
| 369 | - $requeter = true |
|
| 361 | + $select, |
|
| 362 | + $from, |
|
| 363 | + $where = '', |
|
| 364 | + $groupby = '', |
|
| 365 | + $orderby = '', |
|
| 366 | + $limit = '', |
|
| 367 | + $having = '', |
|
| 368 | + $serveur = '', |
|
| 369 | + $requeter = true |
|
| 370 | 370 | ) { |
| 371 | 371 | |
| 372 | 372 | |
| 373 | - $from = (!is_array($from) ? $from : spip_mysql_select_as($from)); |
|
| 374 | - $query = |
|
| 375 | - calculer_mysql_expression('SELECT', $select, ', ') |
|
| 376 | - . calculer_mysql_expression('FROM', $from, ', ') |
|
| 377 | - . calculer_mysql_expression('WHERE', $where) |
|
| 378 | - . calculer_mysql_expression('GROUP BY', $groupby, ',') |
|
| 379 | - . calculer_mysql_expression('HAVING', $having) |
|
| 380 | - . ($orderby ? ("\nORDER BY " . spip_mysql_order($orderby)) : '') |
|
| 381 | - . ($limit ? "\nLIMIT $limit" : ''); |
|
| 373 | + $from = (!is_array($from) ? $from : spip_mysql_select_as($from)); |
|
| 374 | + $query = |
|
| 375 | + calculer_mysql_expression('SELECT', $select, ', ') |
|
| 376 | + . calculer_mysql_expression('FROM', $from, ', ') |
|
| 377 | + . calculer_mysql_expression('WHERE', $where) |
|
| 378 | + . calculer_mysql_expression('GROUP BY', $groupby, ',') |
|
| 379 | + . calculer_mysql_expression('HAVING', $having) |
|
| 380 | + . ($orderby ? ("\nORDER BY " . spip_mysql_order($orderby)) : '') |
|
| 381 | + . ($limit ? "\nLIMIT $limit" : ''); |
|
| 382 | 382 | |
| 383 | - // renvoyer la requete inerte si demandee |
|
| 384 | - if ($requeter === false) { |
|
| 385 | - return $query; |
|
| 386 | - } |
|
| 387 | - $r = spip_mysql_query($query, $serveur, $requeter); |
|
| 383 | + // renvoyer la requete inerte si demandee |
|
| 384 | + if ($requeter === false) { |
|
| 385 | + return $query; |
|
| 386 | + } |
|
| 387 | + $r = spip_mysql_query($query, $serveur, $requeter); |
|
| 388 | 388 | |
| 389 | - return $r ? $r : $query; |
|
| 389 | + return $r ? $r : $query; |
|
| 390 | 390 | } |
| 391 | 391 | |
| 392 | 392 | |
@@ -403,7 +403,7 @@ discard block |
||
| 403 | 403 | * @return string Texte du orderby préparé |
| 404 | 404 | */ |
| 405 | 405 | function spip_mysql_order($orderby) { |
| 406 | - return (is_array($orderby)) ? join(", ", $orderby) : $orderby; |
|
| 406 | + return (is_array($orderby)) ? join(", ", $orderby) : $orderby; |
|
| 407 | 407 | } |
| 408 | 408 | |
| 409 | 409 | |
@@ -426,26 +426,26 @@ discard block |
||
| 426 | 426 | * Contrainte pour clause WHERE |
| 427 | 427 | */ |
| 428 | 428 | function calculer_mysql_where($v) { |
| 429 | - if (!is_array($v)) { |
|
| 430 | - return $v; |
|
| 431 | - } |
|
| 432 | - |
|
| 433 | - $op = array_shift($v); |
|
| 434 | - if (!($n = count($v))) { |
|
| 435 | - return $op; |
|
| 436 | - } else { |
|
| 437 | - $arg = calculer_mysql_where(array_shift($v)); |
|
| 438 | - if ($n == 1) { |
|
| 439 | - return "$op($arg)"; |
|
| 440 | - } else { |
|
| 441 | - $arg2 = calculer_mysql_where(array_shift($v)); |
|
| 442 | - if ($n == 2) { |
|
| 443 | - return "($arg $op $arg2)"; |
|
| 444 | - } else { |
|
| 445 | - return "($arg $op ($arg2) : $v[0])"; |
|
| 446 | - } |
|
| 447 | - } |
|
| 448 | - } |
|
| 429 | + if (!is_array($v)) { |
|
| 430 | + return $v; |
|
| 431 | + } |
|
| 432 | + |
|
| 433 | + $op = array_shift($v); |
|
| 434 | + if (!($n = count($v))) { |
|
| 435 | + return $op; |
|
| 436 | + } else { |
|
| 437 | + $arg = calculer_mysql_where(array_shift($v)); |
|
| 438 | + if ($n == 1) { |
|
| 439 | + return "$op($arg)"; |
|
| 440 | + } else { |
|
| 441 | + $arg2 = calculer_mysql_where(array_shift($v)); |
|
| 442 | + if ($n == 2) { |
|
| 443 | + return "($arg $op $arg2)"; |
|
| 444 | + } else { |
|
| 445 | + return "($arg $op ($arg2) : $v[0])"; |
|
| 446 | + } |
|
| 447 | + } |
|
| 448 | + } |
|
| 449 | 449 | } |
| 450 | 450 | |
| 451 | 451 | /** |
@@ -460,21 +460,21 @@ discard block |
||
| 460 | 460 | * @return string Texte de l'expression, une partie donc, du texte la requête. |
| 461 | 461 | */ |
| 462 | 462 | function calculer_mysql_expression($expression, $v, $join = 'AND') { |
| 463 | - if (empty($v)) { |
|
| 464 | - return ''; |
|
| 465 | - } |
|
| 466 | - |
|
| 467 | - $exp = "\n$expression "; |
|
| 468 | - |
|
| 469 | - if (!is_array($v)) { |
|
| 470 | - return $exp . $v; |
|
| 471 | - } else { |
|
| 472 | - if (strtoupper($join) === 'AND') { |
|
| 473 | - return $exp . join("\n\t$join ", array_map('calculer_mysql_where', $v)); |
|
| 474 | - } else { |
|
| 475 | - return $exp . join($join, $v); |
|
| 476 | - } |
|
| 477 | - } |
|
| 463 | + if (empty($v)) { |
|
| 464 | + return ''; |
|
| 465 | + } |
|
| 466 | + |
|
| 467 | + $exp = "\n$expression "; |
|
| 468 | + |
|
| 469 | + if (!is_array($v)) { |
|
| 470 | + return $exp . $v; |
|
| 471 | + } else { |
|
| 472 | + if (strtoupper($join) === 'AND') { |
|
| 473 | + return $exp . join("\n\t$join ", array_map('calculer_mysql_where', $v)); |
|
| 474 | + } else { |
|
| 475 | + return $exp . join($join, $v); |
|
| 476 | + } |
|
| 477 | + } |
|
| 478 | 478 | } |
| 479 | 479 | |
| 480 | 480 | |
@@ -485,26 +485,26 @@ discard block |
||
| 485 | 485 | * @return string Sélection de colonnes pour une clause SELECT |
| 486 | 486 | */ |
| 487 | 487 | function spip_mysql_select_as($args) { |
| 488 | - $res = ''; |
|
| 489 | - foreach ($args as $k => $v) { |
|
| 490 | - if (substr($k, -1) == '@') { |
|
| 491 | - // c'est une jointure qui se refere au from precedent |
|
| 492 | - // pas de virgule |
|
| 493 | - $res .= ' ' . $v; |
|
| 494 | - } else { |
|
| 495 | - if (!is_numeric($k)) { |
|
| 496 | - $p = strpos($v, " "); |
|
| 497 | - if ($p) { |
|
| 498 | - $v = substr($v, 0, $p) . " AS `$k`" . substr($v, $p); |
|
| 499 | - } else { |
|
| 500 | - $v .= " AS `$k`"; |
|
| 501 | - } |
|
| 502 | - } |
|
| 503 | - $res .= ', ' . $v; |
|
| 504 | - } |
|
| 505 | - } |
|
| 506 | - |
|
| 507 | - return substr($res, 2); |
|
| 488 | + $res = ''; |
|
| 489 | + foreach ($args as $k => $v) { |
|
| 490 | + if (substr($k, -1) == '@') { |
|
| 491 | + // c'est une jointure qui se refere au from precedent |
|
| 492 | + // pas de virgule |
|
| 493 | + $res .= ' ' . $v; |
|
| 494 | + } else { |
|
| 495 | + if (!is_numeric($k)) { |
|
| 496 | + $p = strpos($v, " "); |
|
| 497 | + if ($p) { |
|
| 498 | + $v = substr($v, 0, $p) . " AS `$k`" . substr($v, $p); |
|
| 499 | + } else { |
|
| 500 | + $v .= " AS `$k`"; |
|
| 501 | + } |
|
| 502 | + } |
|
| 503 | + $res .= ', ' . $v; |
|
| 504 | + } |
|
| 505 | + } |
|
| 506 | + |
|
| 507 | + return substr($res, 2); |
|
| 508 | 508 | } |
| 509 | 509 | |
| 510 | 510 | |
@@ -528,42 +528,42 @@ discard block |
||
| 528 | 528 | */ |
| 529 | 529 | function _mysql_traite_query($query, $db = '', $prefixe = '') { |
| 530 | 530 | |
| 531 | - if ($GLOBALS['mysql_rappel_nom_base'] and $db) { |
|
| 532 | - $pref = '`' . $db . '`.'; |
|
| 533 | - } else { |
|
| 534 | - $pref = ''; |
|
| 535 | - } |
|
| 536 | - |
|
| 537 | - if ($prefixe) { |
|
| 538 | - $pref .= $prefixe . "_"; |
|
| 539 | - } |
|
| 540 | - |
|
| 541 | - if (!preg_match('/\s(SET|VALUES|WHERE|DATABASE)\s/i', $query, $regs)) { |
|
| 542 | - $suite = ''; |
|
| 543 | - } else { |
|
| 544 | - $suite = strstr($query, $regs[0]); |
|
| 545 | - $query = substr($query, 0, -strlen($suite)); |
|
| 546 | - // propager le prefixe en cas de requete imbriquee |
|
| 547 | - // il faut alors echapper les chaine avant de le faire, pour ne pas risquer de |
|
| 548 | - // modifier une requete qui est en fait juste du texte dans un champ |
|
| 549 | - if (stripos($suite, "SELECT") !== false) { |
|
| 550 | - list($suite, $textes) = query_echappe_textes($suite); |
|
| 551 | - if (preg_match('/^(.*?)([(]\s*SELECT\b.*)$/si', $suite, $r)) { |
|
| 552 | - $suite = $r[1] . _mysql_traite_query($r[2], $db, $prefixe); |
|
| 553 | - } |
|
| 554 | - $suite = query_reinjecte_textes($suite, $textes); |
|
| 555 | - } |
|
| 556 | - } |
|
| 557 | - $r = preg_replace(_SQL_PREFIXE_TABLE_MYSQL, '\1' . $pref, $query) . $suite; |
|
| 558 | - |
|
| 559 | - // en option, remplacer les emoji (que mysql ne sait pas gérer) en 💩 |
|
| 560 | - if (defined('_MYSQL_NOPLANES') and _MYSQL_NOPLANES and lire_meta('charset_sql_connexion') == 'utf8') { |
|
| 561 | - include_spip('inc/charsets'); |
|
| 562 | - $r = utf8_noplanes($r); |
|
| 563 | - } |
|
| 564 | - |
|
| 565 | - #spip_log("_mysql_traite_query: " . substr($r,0, 50) . ".... $db, $prefixe", _LOG_DEBUG); |
|
| 566 | - return $r; |
|
| 531 | + if ($GLOBALS['mysql_rappel_nom_base'] and $db) { |
|
| 532 | + $pref = '`' . $db . '`.'; |
|
| 533 | + } else { |
|
| 534 | + $pref = ''; |
|
| 535 | + } |
|
| 536 | + |
|
| 537 | + if ($prefixe) { |
|
| 538 | + $pref .= $prefixe . "_"; |
|
| 539 | + } |
|
| 540 | + |
|
| 541 | + if (!preg_match('/\s(SET|VALUES|WHERE|DATABASE)\s/i', $query, $regs)) { |
|
| 542 | + $suite = ''; |
|
| 543 | + } else { |
|
| 544 | + $suite = strstr($query, $regs[0]); |
|
| 545 | + $query = substr($query, 0, -strlen($suite)); |
|
| 546 | + // propager le prefixe en cas de requete imbriquee |
|
| 547 | + // il faut alors echapper les chaine avant de le faire, pour ne pas risquer de |
|
| 548 | + // modifier une requete qui est en fait juste du texte dans un champ |
|
| 549 | + if (stripos($suite, "SELECT") !== false) { |
|
| 550 | + list($suite, $textes) = query_echappe_textes($suite); |
|
| 551 | + if (preg_match('/^(.*?)([(]\s*SELECT\b.*)$/si', $suite, $r)) { |
|
| 552 | + $suite = $r[1] . _mysql_traite_query($r[2], $db, $prefixe); |
|
| 553 | + } |
|
| 554 | + $suite = query_reinjecte_textes($suite, $textes); |
|
| 555 | + } |
|
| 556 | + } |
|
| 557 | + $r = preg_replace(_SQL_PREFIXE_TABLE_MYSQL, '\1' . $pref, $query) . $suite; |
|
| 558 | + |
|
| 559 | + // en option, remplacer les emoji (que mysql ne sait pas gérer) en 💩 |
|
| 560 | + if (defined('_MYSQL_NOPLANES') and _MYSQL_NOPLANES and lire_meta('charset_sql_connexion') == 'utf8') { |
|
| 561 | + include_spip('inc/charsets'); |
|
| 562 | + $r = utf8_noplanes($r); |
|
| 563 | + } |
|
| 564 | + |
|
| 565 | + #spip_log("_mysql_traite_query: " . substr($r,0, 50) . ".... $db, $prefixe", _LOG_DEBUG); |
|
| 566 | + return $r; |
|
| 567 | 567 | } |
| 568 | 568 | |
| 569 | 569 | /** |
@@ -581,13 +581,13 @@ discard block |
||
| 581 | 581 | * - False en cas d'erreur. |
| 582 | 582 | **/ |
| 583 | 583 | function spip_mysql_selectdb($db, $serveur = '', $requeter = true) { |
| 584 | - $link = _mysql_link($serveur); |
|
| 585 | - $ok = mysqli_select_db($link, $db); |
|
| 586 | - if (!$ok) { |
|
| 587 | - spip_log('Echec mysqli_selectdb. Erreur : ' . mysqli_error($link), 'mysql.' . _LOG_CRITIQUE); |
|
| 588 | - } |
|
| 584 | + $link = _mysql_link($serveur); |
|
| 585 | + $ok = mysqli_select_db($link, $db); |
|
| 586 | + if (!$ok) { |
|
| 587 | + spip_log('Echec mysqli_selectdb. Erreur : ' . mysqli_error($link), 'mysql.' . _LOG_CRITIQUE); |
|
| 588 | + } |
|
| 589 | 589 | |
| 590 | - return $ok; |
|
| 590 | + return $ok; |
|
| 591 | 591 | } |
| 592 | 592 | |
| 593 | 593 | |
@@ -608,14 +608,14 @@ discard block |
||
| 608 | 608 | * Liste de noms de bases de données |
| 609 | 609 | **/ |
| 610 | 610 | function spip_mysql_listdbs($serveur = '', $requeter = true) { |
| 611 | - $dbs = array(); |
|
| 612 | - if ($res = spip_mysql_query("SHOW DATABASES", $serveur)) { |
|
| 613 | - while ($row = mysqli_fetch_assoc($res)) { |
|
| 614 | - $dbs[] = $row['Database']; |
|
| 615 | - } |
|
| 616 | - } |
|
| 617 | - |
|
| 618 | - return $dbs; |
|
| 611 | + $dbs = array(); |
|
| 612 | + if ($res = spip_mysql_query("SHOW DATABASES", $serveur)) { |
|
| 613 | + while ($row = mysqli_fetch_assoc($res)) { |
|
| 614 | + $dbs[] = $row['Database']; |
|
| 615 | + } |
|
| 616 | + } |
|
| 617 | + |
|
| 618 | + return $dbs; |
|
| 619 | 619 | } |
| 620 | 620 | |
| 621 | 621 | |
@@ -638,72 +638,72 @@ discard block |
||
| 638 | 638 | * - true si la requête réussie, false sinon. |
| 639 | 639 | */ |
| 640 | 640 | function spip_mysql_create( |
| 641 | - $nom, |
|
| 642 | - $champs, |
|
| 643 | - $cles, |
|
| 644 | - $autoinc = false, |
|
| 645 | - $temporary = false, |
|
| 646 | - $serveur = '', |
|
| 647 | - $requeter = true |
|
| 641 | + $nom, |
|
| 642 | + $champs, |
|
| 643 | + $cles, |
|
| 644 | + $autoinc = false, |
|
| 645 | + $temporary = false, |
|
| 646 | + $serveur = '', |
|
| 647 | + $requeter = true |
|
| 648 | 648 | ) { |
| 649 | 649 | |
| 650 | - $query = ''; |
|
| 651 | - $keys = ''; |
|
| 652 | - $s = ''; |
|
| 653 | - $p = ''; |
|
| 654 | - |
|
| 655 | - // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 656 | - // sans les renseigner (laisse le compilo recuperer la description) |
|
| 657 | - if (!is_array($champs) || !is_array($cles)) { |
|
| 658 | - return; |
|
| 659 | - } |
|
| 660 | - |
|
| 661 | - $res = spip_mysql_query("SELECT version() as v", $serveur); |
|
| 662 | - if (($row = mysqli_fetch_array($res)) && (version_compare($row['v'], '5.0', '>='))) { |
|
| 663 | - spip_mysql_query("SET sql_mode=''", $serveur); |
|
| 664 | - } |
|
| 665 | - |
|
| 666 | - foreach ($cles as $k => $v) { |
|
| 667 | - $keys .= "$s\n\t\t$k ($v)"; |
|
| 668 | - if ($k == "PRIMARY KEY") { |
|
| 669 | - $p = $v; |
|
| 670 | - } |
|
| 671 | - $s = ","; |
|
| 672 | - } |
|
| 673 | - $s = ''; |
|
| 674 | - |
|
| 675 | - $character_set = ""; |
|
| 676 | - if (@$GLOBALS['meta']['charset_sql_base']) { |
|
| 677 | - $character_set .= " CHARACTER SET " . $GLOBALS['meta']['charset_sql_base']; |
|
| 678 | - } |
|
| 679 | - if (@$GLOBALS['meta']['charset_collation_sql_base']) { |
|
| 680 | - $character_set .= " COLLATE " . $GLOBALS['meta']['charset_collation_sql_base']; |
|
| 681 | - } |
|
| 682 | - |
|
| 683 | - foreach ($champs as $k => $v) { |
|
| 684 | - $v = _mysql_remplacements_definitions_table($v); |
|
| 685 | - if (preg_match(',([a-z]*\s*(\(\s*[0-9]*\s*\))?(\s*binary)?),i', $v, $defs)) { |
|
| 686 | - if (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); |
|
| 650 | + $query = ''; |
|
| 651 | + $keys = ''; |
|
| 652 | + $s = ''; |
|
| 653 | + $p = ''; |
|
| 654 | + |
|
| 655 | + // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 656 | + // sans les renseigner (laisse le compilo recuperer la description) |
|
| 657 | + if (!is_array($champs) || !is_array($cles)) { |
|
| 658 | + return; |
|
| 659 | + } |
|
| 660 | + |
|
| 661 | + $res = spip_mysql_query("SELECT version() as v", $serveur); |
|
| 662 | + if (($row = mysqli_fetch_array($res)) && (version_compare($row['v'], '5.0', '>='))) { |
|
| 663 | + spip_mysql_query("SET sql_mode=''", $serveur); |
|
| 664 | + } |
|
| 665 | + |
|
| 666 | + foreach ($cles as $k => $v) { |
|
| 667 | + $keys .= "$s\n\t\t$k ($v)"; |
|
| 668 | + if ($k == "PRIMARY KEY") { |
|
| 669 | + $p = $v; |
|
| 670 | + } |
|
| 671 | + $s = ","; |
|
| 672 | + } |
|
| 673 | + $s = ''; |
|
| 674 | + |
|
| 675 | + $character_set = ""; |
|
| 676 | + if (@$GLOBALS['meta']['charset_sql_base']) { |
|
| 677 | + $character_set .= " CHARACTER SET " . $GLOBALS['meta']['charset_sql_base']; |
|
| 678 | + } |
|
| 679 | + if (@$GLOBALS['meta']['charset_collation_sql_base']) { |
|
| 680 | + $character_set .= " COLLATE " . $GLOBALS['meta']['charset_collation_sql_base']; |
|
| 681 | + } |
|
| 682 | + |
|
| 683 | + foreach ($champs as $k => $v) { |
|
| 684 | + $v = _mysql_remplacements_definitions_table($v); |
|
| 685 | + if (preg_match(',([a-z]*\s*(\(\s*[0-9]*\s*\))?(\s*binary)?),i', $v, $defs)) { |
|
| 686 | + if (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 = array( |
|
| 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 = array( |
|
| 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 = array(); |
|
| 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 = array(); |
|
| 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 array('field' => $fields, 'key' => $keys); |
|
| 920 | - } |
|
| 921 | - |
|
| 922 | - $res = spip_mysql_query("SHOW COLUMNS FROM `$nom_table`", $serveur); |
|
| 923 | - if ($res) { |
|
| 924 | - $nfields = array(); |
|
| 925 | - $nkeys = array(); |
|
| 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 array('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 = array(); |
|
| 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 = array(); |
|
| 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 array('field' => $fields, 'key' => $keys); |
|
| 920 | + } |
|
| 921 | + |
|
| 922 | + $res = spip_mysql_query("SHOW COLUMNS FROM `$nom_table`", $serveur); |
|
| 923 | + if ($res) { |
|
| 924 | + $nfields = array(); |
|
| 925 | + $nkeys = array(); |
|
| 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 array('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 = array(), |
|
| 1013 | - $where = array(), |
|
| 1014 | - $groupby = '', |
|
| 1015 | - $having = array(), |
|
| 1016 | - $serveur = '', |
|
| 1017 | - $requeter = true |
|
| 1012 | + $from = array(), |
|
| 1013 | + $where = array(), |
|
| 1014 | + $groupby = '', |
|
| 1015 | + $having = array(), |
|
| 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, array(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, array(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,38 +1146,38 @@ discard block |
||
| 1146 | 1146 | **/ |
| 1147 | 1147 | function spip_mysql_insert($table, $champs, $valeurs, $desc = array(), $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 | - $query = "INSERT INTO $table $champs VALUES $valeurs"; |
|
| 1154 | - if (!$requeter) { |
|
| 1155 | - return $query; |
|
| 1156 | - } |
|
| 1157 | - |
|
| 1158 | - if (isset($_GET['var_profile'])) { |
|
| 1159 | - include_spip('public/tracer'); |
|
| 1160 | - $t = trace_query_start(); |
|
| 1161 | - $e = ''; |
|
| 1162 | - } else { |
|
| 1163 | - $t = 0; |
|
| 1164 | - } |
|
| 1165 | - |
|
| 1166 | - $connexion['last'] = $query; |
|
| 1167 | - #spip_log($query, 'mysql.'._LOG_DEBUG); |
|
| 1168 | - $r = false; |
|
| 1169 | - if (mysqli_query($link, $query)) { |
|
| 1170 | - $r = mysqli_insert_id($link); |
|
| 1171 | - } else { |
|
| 1172 | - // Log de l'erreur eventuelle |
|
| 1173 | - if ($e = spip_mysql_errno($serveur)) { |
|
| 1174 | - $e .= spip_mysql_error($query, $serveur); |
|
| 1175 | - } // et du fautif |
|
| 1176 | - } |
|
| 1177 | - |
|
| 1178 | - return $t ? trace_query_end($query, $t, $r, $e, $serveur) : $r; |
|
| 1179 | - |
|
| 1180 | - // 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 | + $query = "INSERT INTO $table $champs VALUES $valeurs"; |
|
| 1154 | + if (!$requeter) { |
|
| 1155 | + return $query; |
|
| 1156 | + } |
|
| 1157 | + |
|
| 1158 | + if (isset($_GET['var_profile'])) { |
|
| 1159 | + include_spip('public/tracer'); |
|
| 1160 | + $t = trace_query_start(); |
|
| 1161 | + $e = ''; |
|
| 1162 | + } else { |
|
| 1163 | + $t = 0; |
|
| 1164 | + } |
|
| 1165 | + |
|
| 1166 | + $connexion['last'] = $query; |
|
| 1167 | + #spip_log($query, 'mysql.'._LOG_DEBUG); |
|
| 1168 | + $r = false; |
|
| 1169 | + if (mysqli_query($link, $query)) { |
|
| 1170 | + $r = mysqli_insert_id($link); |
|
| 1171 | + } else { |
|
| 1172 | + // Log de l'erreur eventuelle |
|
| 1173 | + if ($e = spip_mysql_errno($serveur)) { |
|
| 1174 | + $e .= spip_mysql_error($query, $serveur); |
|
| 1175 | + } // et du fautif |
|
| 1176 | + } |
|
| 1177 | + |
|
| 1178 | + return $t ? trace_query_end($query, $t, $r, $e, $serveur) : $r; |
|
| 1179 | + |
|
| 1180 | + // return $r ? $r : (($r===0) ? -1 : 0); pb avec le multi-base. |
|
| 1181 | 1181 | } |
| 1182 | 1182 | |
| 1183 | 1183 | /** |
@@ -1202,20 +1202,20 @@ discard block |
||
| 1202 | 1202 | **/ |
| 1203 | 1203 | function spip_mysql_insertq($table, $couples = array(), $desc = array(), $serveur = '', $requeter = true) { |
| 1204 | 1204 | |
| 1205 | - if (!$desc) { |
|
| 1206 | - $desc = description_table($table, $serveur); |
|
| 1207 | - } |
|
| 1208 | - if (!$desc) { |
|
| 1209 | - $couples = array(); |
|
| 1210 | - } |
|
| 1211 | - $fields = isset($desc['field']) ? $desc['field'] : array(); |
|
| 1205 | + if (!$desc) { |
|
| 1206 | + $desc = description_table($table, $serveur); |
|
| 1207 | + } |
|
| 1208 | + if (!$desc) { |
|
| 1209 | + $couples = array(); |
|
| 1210 | + } |
|
| 1211 | + $fields = isset($desc['field']) ? $desc['field'] : array(); |
|
| 1212 | 1212 | |
| 1213 | - foreach ($couples as $champ => $val) { |
|
| 1214 | - $couples[$champ] = spip_mysql_cite($val, $fields[$champ]); |
|
| 1215 | - } |
|
| 1213 | + foreach ($couples as $champ => $val) { |
|
| 1214 | + $couples[$champ] = spip_mysql_cite($val, $fields[$champ]); |
|
| 1215 | + } |
|
| 1216 | 1216 | |
| 1217 | - return spip_mysql_insert($table, "(" . join(',', array_keys($couples)) . ")", "(" . join(',', $couples) . ")", $desc, |
|
| 1218 | - $serveur, $requeter); |
|
| 1217 | + return spip_mysql_insert($table, "(" . join(',', array_keys($couples)) . ")", "(" . join(',', $couples) . ")", $desc, |
|
| 1218 | + $serveur, $requeter); |
|
| 1219 | 1219 | } |
| 1220 | 1220 | |
| 1221 | 1221 | |
@@ -1240,34 +1240,34 @@ discard block |
||
| 1240 | 1240 | **/ |
| 1241 | 1241 | function spip_mysql_insertq_multi($table, $tab_couples = array(), $desc = array(), $serveur = '', $requeter = true) { |
| 1242 | 1242 | |
| 1243 | - if (!$desc) { |
|
| 1244 | - $desc = description_table($table, $serveur); |
|
| 1245 | - } |
|
| 1246 | - if (!$desc) { |
|
| 1247 | - $tab_couples = array(); |
|
| 1248 | - } |
|
| 1249 | - $fields = isset($desc['field']) ? $desc['field'] : array(); |
|
| 1250 | - |
|
| 1251 | - $cles = "(" . join(',', array_keys(reset($tab_couples))) . ')'; |
|
| 1252 | - $valeurs = array(); |
|
| 1253 | - $r = false; |
|
| 1254 | - |
|
| 1255 | - // Quoter et Inserer par groupes de 100 max pour eviter un debordement de pile |
|
| 1256 | - foreach ($tab_couples as $couples) { |
|
| 1257 | - foreach ($couples as $champ => $val) { |
|
| 1258 | - $couples[$champ] = spip_mysql_cite($val, $fields[$champ]); |
|
| 1259 | - } |
|
| 1260 | - $valeurs[] = '(' . join(',', $couples) . ')'; |
|
| 1261 | - if (count($valeurs) >= 100) { |
|
| 1262 | - $r = spip_mysql_insert($table, $cles, join(', ', $valeurs), $desc, $serveur, $requeter); |
|
| 1263 | - $valeurs = array(); |
|
| 1264 | - } |
|
| 1265 | - } |
|
| 1266 | - if (count($valeurs)) { |
|
| 1267 | - $r = spip_mysql_insert($table, $cles, join(', ', $valeurs), $desc, $serveur, $requeter); |
|
| 1268 | - } |
|
| 1269 | - |
|
| 1270 | - return $r; // dans le cas d'une table auto_increment, le dernier insert_id |
|
| 1243 | + if (!$desc) { |
|
| 1244 | + $desc = description_table($table, $serveur); |
|
| 1245 | + } |
|
| 1246 | + if (!$desc) { |
|
| 1247 | + $tab_couples = array(); |
|
| 1248 | + } |
|
| 1249 | + $fields = isset($desc['field']) ? $desc['field'] : array(); |
|
| 1250 | + |
|
| 1251 | + $cles = "(" . join(',', array_keys(reset($tab_couples))) . ')'; |
|
| 1252 | + $valeurs = array(); |
|
| 1253 | + $r = false; |
|
| 1254 | + |
|
| 1255 | + // Quoter et Inserer par groupes de 100 max pour eviter un debordement de pile |
|
| 1256 | + foreach ($tab_couples as $couples) { |
|
| 1257 | + foreach ($couples as $champ => $val) { |
|
| 1258 | + $couples[$champ] = spip_mysql_cite($val, $fields[$champ]); |
|
| 1259 | + } |
|
| 1260 | + $valeurs[] = '(' . join(',', $couples) . ')'; |
|
| 1261 | + if (count($valeurs) >= 100) { |
|
| 1262 | + $r = spip_mysql_insert($table, $cles, join(', ', $valeurs), $desc, $serveur, $requeter); |
|
| 1263 | + $valeurs = array(); |
|
| 1264 | + } |
|
| 1265 | + } |
|
| 1266 | + if (count($valeurs)) { |
|
| 1267 | + $r = spip_mysql_insert($table, $cles, join(', ', $valeurs), $desc, $serveur, $requeter); |
|
| 1268 | + } |
|
| 1269 | + |
|
| 1270 | + return $r; // dans le cas d'une table auto_increment, le dernier insert_id |
|
| 1271 | 1271 | } |
| 1272 | 1272 | |
| 1273 | 1273 | /** |
@@ -1292,17 +1292,17 @@ discard block |
||
| 1292 | 1292 | * - array Tableau décrivant la requête et son temps d'exécution si var_profile est actif |
| 1293 | 1293 | */ |
| 1294 | 1294 | function spip_mysql_update($table, $champs, $where = '', $desc = array(), $serveur = '', $requeter = true) { |
| 1295 | - $set = array(); |
|
| 1296 | - foreach ($champs as $champ => $val) { |
|
| 1297 | - $set[] = $champ . "=$val"; |
|
| 1298 | - } |
|
| 1299 | - if (!empty($set)) { |
|
| 1300 | - return spip_mysql_query( |
|
| 1301 | - calculer_mysql_expression('UPDATE', $table, ',') |
|
| 1302 | - . calculer_mysql_expression('SET', $set, ',') |
|
| 1303 | - . calculer_mysql_expression('WHERE', $where), |
|
| 1304 | - $serveur, $requeter); |
|
| 1305 | - } |
|
| 1295 | + $set = array(); |
|
| 1296 | + foreach ($champs as $champ => $val) { |
|
| 1297 | + $set[] = $champ . "=$val"; |
|
| 1298 | + } |
|
| 1299 | + if (!empty($set)) { |
|
| 1300 | + return spip_mysql_query( |
|
| 1301 | + calculer_mysql_expression('UPDATE', $table, ',') |
|
| 1302 | + . calculer_mysql_expression('SET', $set, ',') |
|
| 1303 | + . calculer_mysql_expression('WHERE', $where), |
|
| 1304 | + $serveur, $requeter); |
|
| 1305 | + } |
|
| 1306 | 1306 | } |
| 1307 | 1307 | |
| 1308 | 1308 | /** |
@@ -1335,27 +1335,27 @@ discard block |
||
| 1335 | 1335 | */ |
| 1336 | 1336 | function spip_mysql_updateq($table, $champs, $where = '', $desc = array(), $serveur = '', $requeter = true) { |
| 1337 | 1337 | |
| 1338 | - if (!$champs) { |
|
| 1339 | - return; |
|
| 1340 | - } |
|
| 1341 | - if (!$desc) { |
|
| 1342 | - $desc = description_table($table, $serveur); |
|
| 1343 | - } |
|
| 1344 | - if (!$desc) { |
|
| 1345 | - $champs = array(); |
|
| 1346 | - } else { |
|
| 1347 | - $fields = $desc['field']; |
|
| 1348 | - } |
|
| 1349 | - $set = array(); |
|
| 1350 | - foreach ($champs as $champ => $val) { |
|
| 1351 | - $set[] = $champ . '=' . spip_mysql_cite($val, @$fields[$champ]); |
|
| 1352 | - } |
|
| 1353 | - |
|
| 1354 | - return spip_mysql_query( |
|
| 1355 | - calculer_mysql_expression('UPDATE', $table, ',') |
|
| 1356 | - . calculer_mysql_expression('SET', $set, ',') |
|
| 1357 | - . calculer_mysql_expression('WHERE', $where), |
|
| 1358 | - $serveur, $requeter); |
|
| 1338 | + if (!$champs) { |
|
| 1339 | + return; |
|
| 1340 | + } |
|
| 1341 | + if (!$desc) { |
|
| 1342 | + $desc = description_table($table, $serveur); |
|
| 1343 | + } |
|
| 1344 | + if (!$desc) { |
|
| 1345 | + $champs = array(); |
|
| 1346 | + } else { |
|
| 1347 | + $fields = $desc['field']; |
|
| 1348 | + } |
|
| 1349 | + $set = array(); |
|
| 1350 | + foreach ($champs as $champ => $val) { |
|
| 1351 | + $set[] = $champ . '=' . spip_mysql_cite($val, @$fields[$champ]); |
|
| 1352 | + } |
|
| 1353 | + |
|
| 1354 | + return spip_mysql_query( |
|
| 1355 | + calculer_mysql_expression('UPDATE', $table, ',') |
|
| 1356 | + . calculer_mysql_expression('SET', $set, ',') |
|
| 1357 | + . calculer_mysql_expression('WHERE', $where), |
|
| 1358 | + $serveur, $requeter); |
|
| 1359 | 1359 | } |
| 1360 | 1360 | |
| 1361 | 1361 | /** |
@@ -1371,20 +1371,20 @@ discard block |
||
| 1371 | 1371 | * - False en cas d'erreur. |
| 1372 | 1372 | **/ |
| 1373 | 1373 | function spip_mysql_delete($table, $where = '', $serveur = '', $requeter = true) { |
| 1374 | - $res = spip_mysql_query( |
|
| 1375 | - calculer_mysql_expression('DELETE FROM', $table, ',') |
|
| 1376 | - . calculer_mysql_expression('WHERE', $where), |
|
| 1377 | - $serveur, $requeter); |
|
| 1378 | - if (!$requeter) { |
|
| 1379 | - return $res; |
|
| 1380 | - } |
|
| 1381 | - if ($res) { |
|
| 1382 | - $link = _mysql_link($serveur); |
|
| 1383 | - |
|
| 1384 | - return mysqli_affected_rows($link); |
|
| 1385 | - } else { |
|
| 1386 | - return false; |
|
| 1387 | - } |
|
| 1374 | + $res = spip_mysql_query( |
|
| 1375 | + calculer_mysql_expression('DELETE FROM', $table, ',') |
|
| 1376 | + . calculer_mysql_expression('WHERE', $where), |
|
| 1377 | + $serveur, $requeter); |
|
| 1378 | + if (!$requeter) { |
|
| 1379 | + return $res; |
|
| 1380 | + } |
|
| 1381 | + if ($res) { |
|
| 1382 | + $link = _mysql_link($serveur); |
|
| 1383 | + |
|
| 1384 | + return mysqli_affected_rows($link); |
|
| 1385 | + } else { |
|
| 1386 | + return false; |
|
| 1387 | + } |
|
| 1388 | 1388 | } |
| 1389 | 1389 | |
| 1390 | 1390 | |
@@ -1413,8 +1413,8 @@ discard block |
||
| 1413 | 1413 | * - False en cas d'erreur. |
| 1414 | 1414 | **/ |
| 1415 | 1415 | function spip_mysql_replace($table, $couples, $desc = array(), $serveur = '', $requeter = true) { |
| 1416 | - return spip_mysql_query("REPLACE $table (" . join(',', array_keys($couples)) . ') VALUES (' . join(',', |
|
| 1417 | - array_map('_q', $couples)) . ')', $serveur, $requeter); |
|
| 1416 | + return spip_mysql_query("REPLACE $table (" . join(',', array_keys($couples)) . ') VALUES (' . join(',', |
|
| 1417 | + array_map('_q', $couples)) . ')', $serveur, $requeter); |
|
| 1418 | 1418 | } |
| 1419 | 1419 | |
| 1420 | 1420 | |
@@ -1443,14 +1443,14 @@ discard block |
||
| 1443 | 1443 | * - False en cas d'erreur. |
| 1444 | 1444 | **/ |
| 1445 | 1445 | function spip_mysql_replace_multi($table, $tab_couples, $desc = array(), $serveur = '', $requeter = true) { |
| 1446 | - $cles = "(" . join(',', array_keys($tab_couples[0])) . ')'; |
|
| 1447 | - $valeurs = array(); |
|
| 1448 | - foreach ($tab_couples as $couples) { |
|
| 1449 | - $valeurs[] = '(' . join(',', array_map('_q', $couples)) . ')'; |
|
| 1450 | - } |
|
| 1451 | - $valeurs = implode(', ', $valeurs); |
|
| 1452 | - |
|
| 1453 | - return spip_mysql_query("REPLACE $table $cles VALUES $valeurs", $serveur, $requeter); |
|
| 1446 | + $cles = "(" . join(',', array_keys($tab_couples[0])) . ')'; |
|
| 1447 | + $valeurs = array(); |
|
| 1448 | + foreach ($tab_couples as $couples) { |
|
| 1449 | + $valeurs[] = '(' . join(',', array_map('_q', $couples)) . ')'; |
|
| 1450 | + } |
|
| 1451 | + $valeurs = implode(', ', $valeurs); |
|
| 1452 | + |
|
| 1453 | + return spip_mysql_query("REPLACE $table $cles VALUES $valeurs", $serveur, $requeter); |
|
| 1454 | 1454 | } |
| 1455 | 1455 | |
| 1456 | 1456 | |
@@ -1465,32 +1465,32 @@ discard block |
||
| 1465 | 1465 | * @return string Texte de sélection pour la requête |
| 1466 | 1466 | */ |
| 1467 | 1467 | function spip_mysql_multi($objet, $lang) { |
| 1468 | - $lengthlang = strlen("[$lang]"); |
|
| 1469 | - $posmulti = "INSTR(" . $objet . ", '<multi>')"; |
|
| 1470 | - $posfinmulti = "INSTR(" . $objet . ", '</multi>')"; |
|
| 1471 | - $debutchaine = "LEFT(" . $objet . ", $posmulti-1)"; |
|
| 1472 | - $finchaine = "RIGHT(" . $objet . ", CHAR_LENGTH(" . $objet . ") -(7+$posfinmulti))"; |
|
| 1473 | - $chainemulti = "TRIM(SUBSTRING(" . $objet . ", $posmulti+7, $posfinmulti -(7+$posmulti)))"; |
|
| 1474 | - $poslang = "INSTR($chainemulti,'[" . $lang . "]')"; |
|
| 1475 | - $poslang = "IF($poslang=0,INSTR($chainemulti,']')+1,$poslang+$lengthlang)"; |
|
| 1476 | - $chainelang = "TRIM(SUBSTRING(" . $objet . ", $posmulti+7+$poslang-1,$posfinmulti -($posmulti+7+$poslang-1) ))"; |
|
| 1477 | - $posfinlang = "INSTR(" . $chainelang . ", '[')"; |
|
| 1478 | - $chainelang = "IF($posfinlang>0,LEFT($chainelang,$posfinlang-1),$chainelang)"; |
|
| 1479 | - //$chainelang = "LEFT($chainelang,$posfinlang-1)"; |
|
| 1480 | - $retour = "(TRIM(IF($posmulti = 0 , " . |
|
| 1481 | - " TRIM(" . $objet . "), " . |
|
| 1482 | - " CONCAT( " . |
|
| 1483 | - " $debutchaine, " . |
|
| 1484 | - " IF( " . |
|
| 1485 | - " $poslang = 0, " . |
|
| 1486 | - " $chainemulti, " . |
|
| 1487 | - " $chainelang" . |
|
| 1488 | - " ), " . |
|
| 1489 | - " $finchaine" . |
|
| 1490 | - " ) " . |
|
| 1491 | - "))) AS multi"; |
|
| 1492 | - |
|
| 1493 | - return $retour; |
|
| 1468 | + $lengthlang = strlen("[$lang]"); |
|
| 1469 | + $posmulti = "INSTR(" . $objet . ", '<multi>')"; |
|
| 1470 | + $posfinmulti = "INSTR(" . $objet . ", '</multi>')"; |
|
| 1471 | + $debutchaine = "LEFT(" . $objet . ", $posmulti-1)"; |
|
| 1472 | + $finchaine = "RIGHT(" . $objet . ", CHAR_LENGTH(" . $objet . ") -(7+$posfinmulti))"; |
|
| 1473 | + $chainemulti = "TRIM(SUBSTRING(" . $objet . ", $posmulti+7, $posfinmulti -(7+$posmulti)))"; |
|
| 1474 | + $poslang = "INSTR($chainemulti,'[" . $lang . "]')"; |
|
| 1475 | + $poslang = "IF($poslang=0,INSTR($chainemulti,']')+1,$poslang+$lengthlang)"; |
|
| 1476 | + $chainelang = "TRIM(SUBSTRING(" . $objet . ", $posmulti+7+$poslang-1,$posfinmulti -($posmulti+7+$poslang-1) ))"; |
|
| 1477 | + $posfinlang = "INSTR(" . $chainelang . ", '[')"; |
|
| 1478 | + $chainelang = "IF($posfinlang>0,LEFT($chainelang,$posfinlang-1),$chainelang)"; |
|
| 1479 | + //$chainelang = "LEFT($chainelang,$posfinlang-1)"; |
|
| 1480 | + $retour = "(TRIM(IF($posmulti = 0 , " . |
|
| 1481 | + " TRIM(" . $objet . "), " . |
|
| 1482 | + " CONCAT( " . |
|
| 1483 | + " $debutchaine, " . |
|
| 1484 | + " IF( " . |
|
| 1485 | + " $poslang = 0, " . |
|
| 1486 | + " $chainemulti, " . |
|
| 1487 | + " $chainelang" . |
|
| 1488 | + " ), " . |
|
| 1489 | + " $finchaine" . |
|
| 1490 | + " ) " . |
|
| 1491 | + "))) AS multi"; |
|
| 1492 | + |
|
| 1493 | + return $retour; |
|
| 1494 | 1494 | } |
| 1495 | 1495 | |
| 1496 | 1496 | /** |
@@ -1504,7 +1504,7 @@ discard block |
||
| 1504 | 1504 | * Valeur hexadécimale pour MySQL |
| 1505 | 1505 | **/ |
| 1506 | 1506 | function spip_mysql_hex($v) { |
| 1507 | - return "0x" . $v; |
|
| 1507 | + return "0x" . $v; |
|
| 1508 | 1508 | } |
| 1509 | 1509 | |
| 1510 | 1510 | /** |
@@ -1520,15 +1520,15 @@ discard block |
||
| 1520 | 1520 | * Donnée prête à être utilisée par le gestionnaire SQL |
| 1521 | 1521 | */ |
| 1522 | 1522 | function spip_mysql_quote($v, $type = '') { |
| 1523 | - if (!is_array($v)) { |
|
| 1524 | - return spip_mysql_cite($v, $type); |
|
| 1525 | - } |
|
| 1526 | - |
|
| 1527 | - // si c'est un tableau, le parcourir en propageant le type |
|
| 1528 | - foreach ($v as $k => $r) { |
|
| 1529 | - $v[$k] = spip_mysql_quote($r, $type); |
|
| 1530 | - } |
|
| 1531 | - return implode(',', $v); |
|
| 1523 | + if (!is_array($v)) { |
|
| 1524 | + return spip_mysql_cite($v, $type); |
|
| 1525 | + } |
|
| 1526 | + |
|
| 1527 | + // si c'est un tableau, le parcourir en propageant le type |
|
| 1528 | + foreach ($v as $k => $r) { |
|
| 1529 | + $v[$k] = spip_mysql_quote($r, $type); |
|
| 1530 | + } |
|
| 1531 | + return implode(',', $v); |
|
| 1532 | 1532 | } |
| 1533 | 1533 | |
| 1534 | 1534 | /** |
@@ -1544,17 +1544,17 @@ discard block |
||
| 1544 | 1544 | * Expression SQL |
| 1545 | 1545 | **/ |
| 1546 | 1546 | function spip_mysql_date_proche($champ, $interval, $unite) { |
| 1547 | - return '(' |
|
| 1548 | - . $champ |
|
| 1549 | - . (($interval <= 0) ? '>' : '<') |
|
| 1550 | - . (($interval <= 0) ? 'DATE_SUB' : 'DATE_ADD') |
|
| 1551 | - . '(' |
|
| 1552 | - . sql_quote(date('Y-m-d H:i:s')) |
|
| 1553 | - . ', INTERVAL ' |
|
| 1554 | - . (($interval > 0) ? $interval : (0 - $interval)) |
|
| 1555 | - . ' ' |
|
| 1556 | - . $unite |
|
| 1557 | - . '))'; |
|
| 1547 | + return '(' |
|
| 1548 | + . $champ |
|
| 1549 | + . (($interval <= 0) ? '>' : '<') |
|
| 1550 | + . (($interval <= 0) ? 'DATE_SUB' : 'DATE_ADD') |
|
| 1551 | + . '(' |
|
| 1552 | + . sql_quote(date('Y-m-d H:i:s')) |
|
| 1553 | + . ', INTERVAL ' |
|
| 1554 | + . (($interval > 0) ? $interval : (0 - $interval)) |
|
| 1555 | + . ' ' |
|
| 1556 | + . $unite |
|
| 1557 | + . '))'; |
|
| 1558 | 1558 | } |
| 1559 | 1559 | |
| 1560 | 1560 | |
@@ -1578,7 +1578,7 @@ discard block |
||
| 1578 | 1578 | * Expression de requête SQL |
| 1579 | 1579 | **/ |
| 1580 | 1580 | function spip_mysql_in($val, $valeurs, $not = '', $serveur = '', $requeter = true) { |
| 1581 | - return "($val $not IN ($valeurs))"; |
|
| 1581 | + return "($val $not IN ($valeurs))"; |
|
| 1582 | 1582 | } |
| 1583 | 1583 | |
| 1584 | 1584 | |
@@ -1590,33 +1590,33 @@ discard block |
||
| 1590 | 1590 | * @return string|number Texte ou nombre échappé |
| 1591 | 1591 | */ |
| 1592 | 1592 | function spip_mysql_cite($v, $type) { |
| 1593 | - if (!$type) { |
|
| 1594 | - if (is_numeric($v)) { |
|
| 1595 | - return strval($v); |
|
| 1596 | - } |
|
| 1597 | - return "'" . addslashes($v) . "'"; |
|
| 1598 | - } |
|
| 1599 | - |
|
| 1600 | - if (is_null($v) |
|
| 1601 | - and stripos($type, "NOT NULL") === false |
|
| 1602 | - ) { |
|
| 1603 | - return 'NULL'; |
|
| 1604 | - } // null php se traduit en NULL SQL |
|
| 1605 | - if (sql_test_date($type) and preg_match('/^\w+\(/', $v)) { |
|
| 1606 | - return $v; |
|
| 1607 | - } |
|
| 1608 | - if (sql_test_int($type)) { |
|
| 1609 | - if (is_numeric($v) or (ctype_xdigit(substr($v, 2)) |
|
| 1610 | - and $v[0] == '0' and $v[1] == 'x') |
|
| 1611 | - ) { |
|
| 1612 | - return $v; |
|
| 1613 | - } // si pas numerique, forcer le intval |
|
| 1614 | - else { |
|
| 1615 | - return intval($v); |
|
| 1616 | - } |
|
| 1617 | - } |
|
| 1618 | - |
|
| 1619 | - return ("'" . addslashes($v) . "'"); |
|
| 1593 | + if (!$type) { |
|
| 1594 | + if (is_numeric($v)) { |
|
| 1595 | + return strval($v); |
|
| 1596 | + } |
|
| 1597 | + return "'" . addslashes($v) . "'"; |
|
| 1598 | + } |
|
| 1599 | + |
|
| 1600 | + if (is_null($v) |
|
| 1601 | + and stripos($type, "NOT NULL") === false |
|
| 1602 | + ) { |
|
| 1603 | + return 'NULL'; |
|
| 1604 | + } // null php se traduit en NULL SQL |
|
| 1605 | + if (sql_test_date($type) and preg_match('/^\w+\(/', $v)) { |
|
| 1606 | + return $v; |
|
| 1607 | + } |
|
| 1608 | + if (sql_test_int($type)) { |
|
| 1609 | + if (is_numeric($v) or (ctype_xdigit(substr($v, 2)) |
|
| 1610 | + and $v[0] == '0' and $v[1] == 'x') |
|
| 1611 | + ) { |
|
| 1612 | + return $v; |
|
| 1613 | + } // si pas numerique, forcer le intval |
|
| 1614 | + else { |
|
| 1615 | + return intval($v); |
|
| 1616 | + } |
|
| 1617 | + } |
|
| 1618 | + |
|
| 1619 | + return ("'" . addslashes($v) . "'"); |
|
| 1620 | 1620 | } |
| 1621 | 1621 | |
| 1622 | 1622 | |
@@ -1640,21 +1640,21 @@ discard block |
||
| 1640 | 1640 | */ |
| 1641 | 1641 | function spip_get_lock($nom, $timeout = 0) { |
| 1642 | 1642 | |
| 1643 | - define('_LOCK_TIME', intval(time() / 3600 - 316982)); |
|
| 1643 | + define('_LOCK_TIME', intval(time() / 3600 - 316982)); |
|
| 1644 | 1644 | |
| 1645 | - $connexion = &$GLOBALS['connexions'][0]; |
|
| 1646 | - $bd = $connexion['db']; |
|
| 1647 | - $prefixe = $connexion['prefixe']; |
|
| 1648 | - $nom = "$bd:$prefixe:$nom" . _LOCK_TIME; |
|
| 1645 | + $connexion = &$GLOBALS['connexions'][0]; |
|
| 1646 | + $bd = $connexion['db']; |
|
| 1647 | + $prefixe = $connexion['prefixe']; |
|
| 1648 | + $nom = "$bd:$prefixe:$nom" . _LOCK_TIME; |
|
| 1649 | 1649 | |
| 1650 | - $connexion['last'] = $q = "SELECT GET_LOCK(" . _q($nom) . ", $timeout) AS n"; |
|
| 1650 | + $connexion['last'] = $q = "SELECT GET_LOCK(" . _q($nom) . ", $timeout) AS n"; |
|
| 1651 | 1651 | |
| 1652 | - $q = @sql_fetch(mysqli_query(_mysql_link(), $q)); |
|
| 1653 | - if (!$q) { |
|
| 1654 | - spip_log("pas de lock sql pour $nom", _LOG_ERREUR); |
|
| 1655 | - } |
|
| 1652 | + $q = @sql_fetch(mysqli_query(_mysql_link(), $q)); |
|
| 1653 | + if (!$q) { |
|
| 1654 | + spip_log("pas de lock sql pour $nom", _LOG_ERREUR); |
|
| 1655 | + } |
|
| 1656 | 1656 | |
| 1657 | - return $q['n']; |
|
| 1657 | + return $q['n']; |
|
| 1658 | 1658 | } |
| 1659 | 1659 | |
| 1660 | 1660 | |
@@ -1671,13 +1671,13 @@ discard block |
||
| 1671 | 1671 | */ |
| 1672 | 1672 | function spip_release_lock($nom) { |
| 1673 | 1673 | |
| 1674 | - $connexion = &$GLOBALS['connexions'][0]; |
|
| 1675 | - $bd = $connexion['db']; |
|
| 1676 | - $prefixe = $connexion['prefixe']; |
|
| 1677 | - $nom = "$bd:$prefixe:$nom" . _LOCK_TIME; |
|
| 1674 | + $connexion = &$GLOBALS['connexions'][0]; |
|
| 1675 | + $bd = $connexion['db']; |
|
| 1676 | + $prefixe = $connexion['prefixe']; |
|
| 1677 | + $nom = "$bd:$prefixe:$nom" . _LOCK_TIME; |
|
| 1678 | 1678 | |
| 1679 | - $connexion['last'] = $q = "SELECT RELEASE_LOCK(" . _q($nom) . ")"; |
|
| 1680 | - mysqli_query(_mysql_link(), $q); |
|
| 1679 | + $connexion['last'] = $q = "SELECT RELEASE_LOCK(" . _q($nom) . ")"; |
|
| 1680 | + mysqli_query(_mysql_link(), $q); |
|
| 1681 | 1681 | } |
| 1682 | 1682 | |
| 1683 | 1683 | |
@@ -1688,7 +1688,7 @@ discard block |
||
| 1688 | 1688 | * True si on a les fonctions, false sinon |
| 1689 | 1689 | */ |
| 1690 | 1690 | function spip_versions_mysql() { |
| 1691 | - return function_exists('mysqli_query'); |
|
| 1691 | + return function_exists('mysqli_query'); |
|
| 1692 | 1692 | } |
| 1693 | 1693 | |
| 1694 | 1694 | |
@@ -1701,20 +1701,20 @@ discard block |
||
| 1701 | 1701 | * - chaîne : code compilé pour le faire désactiver par SPIP sinon |
| 1702 | 1702 | */ |
| 1703 | 1703 | function test_rappel_nom_base_mysql($server_db) { |
| 1704 | - $GLOBALS['mysql_rappel_nom_base'] = true; |
|
| 1705 | - sql_delete('spip_meta', "nom='mysql_rappel_nom_base'", $server_db); |
|
| 1706 | - $ok = spip_query("INSERT INTO spip_meta (nom,valeur) VALUES ('mysql_rappel_nom_base', 'test')", $server_db); |
|
| 1704 | + $GLOBALS['mysql_rappel_nom_base'] = true; |
|
| 1705 | + sql_delete('spip_meta', "nom='mysql_rappel_nom_base'", $server_db); |
|
| 1706 | + $ok = spip_query("INSERT INTO spip_meta (nom,valeur) VALUES ('mysql_rappel_nom_base', 'test')", $server_db); |
|
| 1707 | 1707 | |
| 1708 | - if ($ok) { |
|
| 1709 | - sql_delete('spip_meta', "nom='mysql_rappel_nom_base'", $server_db); |
|
| 1708 | + if ($ok) { |
|
| 1709 | + sql_delete('spip_meta', "nom='mysql_rappel_nom_base'", $server_db); |
|
| 1710 | 1710 | |
| 1711 | - return ''; |
|
| 1712 | - } else { |
|
| 1713 | - $GLOBALS['mysql_rappel_nom_base'] = false; |
|
| 1711 | + return ''; |
|
| 1712 | + } else { |
|
| 1713 | + $GLOBALS['mysql_rappel_nom_base'] = false; |
|
| 1714 | 1714 | |
| 1715 | - return "\$GLOBALS['mysql_rappel_nom_base'] = false; " . |
|
| 1716 | - "/* echec de test_rappel_nom_base_mysql a l'installation. */\n"; |
|
| 1717 | - } |
|
| 1715 | + return "\$GLOBALS['mysql_rappel_nom_base'] = false; " . |
|
| 1716 | + "/* echec de test_rappel_nom_base_mysql a l'installation. */\n"; |
|
| 1717 | + } |
|
| 1718 | 1718 | } |
| 1719 | 1719 | |
| 1720 | 1720 | /** |
@@ -1728,13 +1728,13 @@ discard block |
||
| 1728 | 1728 | * - chaîne : code compilé pour l'indiquer le résultat du test à SPIP |
| 1729 | 1729 | */ |
| 1730 | 1730 | function test_sql_mode_mysql($server_db) { |
| 1731 | - $res = sql_select("version() as v", '', '', '', '', '', '', $server_db); |
|
| 1732 | - $row = sql_fetch($res, $server_db); |
|
| 1733 | - if (version_compare($row['v'], '5.0.0', '>=')) { |
|
| 1734 | - defined('_MYSQL_SET_SQL_MODE') || define('_MYSQL_SET_SQL_MODE', true); |
|
| 1731 | + $res = sql_select("version() as v", '', '', '', '', '', '', $server_db); |
|
| 1732 | + $row = sql_fetch($res, $server_db); |
|
| 1733 | + if (version_compare($row['v'], '5.0.0', '>=')) { |
|
| 1734 | + defined('_MYSQL_SET_SQL_MODE') || define('_MYSQL_SET_SQL_MODE', true); |
|
| 1735 | 1735 | |
| 1736 | - return "defined('_MYSQL_SET_SQL_MODE') || define('_MYSQL_SET_SQL_MODE',true);\n"; |
|
| 1737 | - } |
|
| 1736 | + return "defined('_MYSQL_SET_SQL_MODE') || define('_MYSQL_SET_SQL_MODE',true);\n"; |
|
| 1737 | + } |
|
| 1738 | 1738 | |
| 1739 | - return ''; |
|
| 1739 | + return ''; |
|
| 1740 | 1740 | } |
@@ -48,7 +48,7 @@ discard block |
||
| 48 | 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 | 49 | if ( |
| 50 | 50 | $port and !is_numeric($socket = $port) |
| 51 | - and (!$host or $host=='localhost')) { |
|
| 51 | + and (!$host or $host == 'localhost')) { |
|
| 52 | 52 | $link = @mysqli_connect($host, $login, $pass, '', null, $socket); |
| 53 | 53 | } |
| 54 | 54 | elseif ($port) { |
@@ -59,7 +59,7 @@ discard block |
||
| 59 | 59 | } |
| 60 | 60 | |
| 61 | 61 | if (!$link) { |
| 62 | - spip_log('Echec mysqli_connect. Erreur : ' . mysqli_connect_error(), 'mysql.' . _LOG_HS); |
|
| 62 | + spip_log('Echec mysqli_connect. Erreur : '.mysqli_connect_error(), 'mysql.'._LOG_HS); |
|
| 63 | 63 | |
| 64 | 64 | return false; |
| 65 | 65 | } |
@@ -76,7 +76,7 @@ discard block |
||
| 76 | 76 | } |
| 77 | 77 | } |
| 78 | 78 | |
| 79 | - spip_log("Connexion MySQLi vers $host, base $db, prefixe $prefixe " . ($ok ? "operationnelle" : 'impossible'), |
|
| 79 | + spip_log("Connexion MySQLi vers $host, base $db, prefixe $prefixe ".($ok ? "operationnelle" : 'impossible'), |
|
| 80 | 80 | _LOG_DEBUG); |
| 81 | 81 | |
| 82 | 82 | return !$ok ? false : array( |
@@ -165,9 +165,9 @@ discard block |
||
| 165 | 165 | */ |
| 166 | 166 | function spip_mysql_set_charset($charset, $serveur = '', $requeter = true) { |
| 167 | 167 | $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
| 168 | - spip_log("changement de charset sql : " . "SET NAMES " . _q($charset), _LOG_DEBUG); |
|
| 168 | + spip_log("changement de charset sql : "."SET NAMES "._q($charset), _LOG_DEBUG); |
|
| 169 | 169 | |
| 170 | - return mysqli_query($connexion['link'], $connexion['last'] = "SET NAMES " . _q($charset)); |
|
| 170 | + return mysqli_query($connexion['link'], $connexion['last'] = "SET NAMES "._q($charset)); |
|
| 171 | 171 | } |
| 172 | 172 | |
| 173 | 173 | |
@@ -182,7 +182,7 @@ discard block |
||
| 182 | 182 | function spip_mysql_get_charset($charset = array(), $serveur = '', $requeter = true) { |
| 183 | 183 | $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
| 184 | 184 | $connexion['last'] = $c = "SHOW CHARACTER SET" |
| 185 | - . (!$charset ? '' : (" LIKE " . _q($charset['charset']))); |
|
| 185 | + . (!$charset ? '' : (" LIKE "._q($charset['charset']))); |
|
| 186 | 186 | |
| 187 | 187 | return spip_mysql_fetch(mysqli_query($connexion['link'], $c), null, $serveur); |
| 188 | 188 | } |
@@ -242,14 +242,14 @@ discard block |
||
| 242 | 242 | $debug = ''; |
| 243 | 243 | if (defined('_DEBUG_SLOW_QUERIES') and _DEBUG_SLOW_QUERIES) { |
| 244 | 244 | if (isset($GLOBALS['debug']['aucasou'])) { |
| 245 | - list(, $id, , $infos) = $GLOBALS['debug']['aucasou']; |
|
| 246 | - $debug .= "BOUCLE$id @ " . (isset($infos[0]) ? $infos[0] : '') . " | "; |
|
| 245 | + list(, $id,, $infos) = $GLOBALS['debug']['aucasou']; |
|
| 246 | + $debug .= "BOUCLE$id @ ".(isset($infos[0]) ? $infos[0] : '')." | "; |
|
| 247 | 247 | } |
| 248 | - $debug .= $_SERVER['REQUEST_URI'] . ' + ' . $GLOBALS['ip']; |
|
| 249 | - $debug = ' /* ' . mysqli_real_escape_string($link, str_replace('*/', '@/', $debug)) . ' */'; |
|
| 248 | + $debug .= $_SERVER['REQUEST_URI'].' + '.$GLOBALS['ip']; |
|
| 249 | + $debug = ' /* '.mysqli_real_escape_string($link, str_replace('*/', '@/', $debug)).' */'; |
|
| 250 | 250 | } |
| 251 | 251 | |
| 252 | - $r = mysqli_query($link, $query . $debug); |
|
| 252 | + $r = mysqli_query($link, $query.$debug); |
|
| 253 | 253 | |
| 254 | 254 | //Eviter de propager le GoneAway sur les autres requetes d'un même processus PHP |
| 255 | 255 | if ($e = spip_mysql_errno($serveur)) { // Log d'un Gone Away |
@@ -262,7 +262,7 @@ discard block |
||
| 262 | 262 | $connexion = &$GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
| 263 | 263 | $link = $connexion['link']; |
| 264 | 264 | //On retente au cas où |
| 265 | - $r = mysqli_query($link, $query . $debug); |
|
| 265 | + $r = mysqli_query($link, $query.$debug); |
|
| 266 | 266 | } |
| 267 | 267 | } |
| 268 | 268 | |
@@ -290,7 +290,7 @@ discard block |
||
| 290 | 290 | // d'utiliser ceux-ci, copie-colle de phpmyadmin |
| 291 | 291 | $query = preg_replace(",^TABLE\s*`([^`]*)`,i", "TABLE \\1", $query); |
| 292 | 292 | |
| 293 | - return spip_mysql_query("ALTER " . $query, $serveur, $requeter); # i.e. que PG se debrouille |
|
| 293 | + return spip_mysql_query("ALTER ".$query, $serveur, $requeter); # i.e. que PG se debrouille |
|
| 294 | 294 | } |
| 295 | 295 | |
| 296 | 296 | |
@@ -303,7 +303,7 @@ discard block |
||
| 303 | 303 | * @return bool Toujours true |
| 304 | 304 | */ |
| 305 | 305 | function spip_mysql_optimize($table, $serveur = '', $requeter = true) { |
| 306 | - spip_mysql_query("OPTIMIZE TABLE " . $table); |
|
| 306 | + spip_mysql_query("OPTIMIZE TABLE ".$table); |
|
| 307 | 307 | |
| 308 | 308 | return true; |
| 309 | 309 | } |
@@ -326,7 +326,7 @@ discard block |
||
| 326 | 326 | $link = $connexion['link']; |
| 327 | 327 | $db = $connexion['db']; |
| 328 | 328 | |
| 329 | - $query = 'EXPLAIN ' . _mysql_traite_query($query, $db, $prefixe); |
|
| 329 | + $query = 'EXPLAIN '._mysql_traite_query($query, $db, $prefixe); |
|
| 330 | 330 | $r = mysqli_query($link, $query); |
| 331 | 331 | |
| 332 | 332 | return spip_mysql_fetch($r, null, $serveur); |
@@ -377,7 +377,7 @@ discard block |
||
| 377 | 377 | . calculer_mysql_expression('WHERE', $where) |
| 378 | 378 | . calculer_mysql_expression('GROUP BY', $groupby, ',') |
| 379 | 379 | . calculer_mysql_expression('HAVING', $having) |
| 380 | - . ($orderby ? ("\nORDER BY " . spip_mysql_order($orderby)) : '') |
|
| 380 | + . ($orderby ? ("\nORDER BY ".spip_mysql_order($orderby)) : '') |
|
| 381 | 381 | . ($limit ? "\nLIMIT $limit" : ''); |
| 382 | 382 | |
| 383 | 383 | // renvoyer la requete inerte si demandee |
@@ -467,12 +467,12 @@ discard block |
||
| 467 | 467 | $exp = "\n$expression "; |
| 468 | 468 | |
| 469 | 469 | if (!is_array($v)) { |
| 470 | - return $exp . $v; |
|
| 470 | + return $exp.$v; |
|
| 471 | 471 | } else { |
| 472 | 472 | if (strtoupper($join) === 'AND') { |
| 473 | - return $exp . join("\n\t$join ", array_map('calculer_mysql_where', $v)); |
|
| 473 | + return $exp.join("\n\t$join ", array_map('calculer_mysql_where', $v)); |
|
| 474 | 474 | } else { |
| 475 | - return $exp . join($join, $v); |
|
| 475 | + return $exp.join($join, $v); |
|
| 476 | 476 | } |
| 477 | 477 | } |
| 478 | 478 | } |
@@ -490,17 +490,17 @@ discard block |
||
| 490 | 490 | if (substr($k, -1) == '@') { |
| 491 | 491 | // c'est une jointure qui se refere au from precedent |
| 492 | 492 | // pas de virgule |
| 493 | - $res .= ' ' . $v; |
|
| 493 | + $res .= ' '.$v; |
|
| 494 | 494 | } else { |
| 495 | 495 | if (!is_numeric($k)) { |
| 496 | 496 | $p = strpos($v, " "); |
| 497 | 497 | if ($p) { |
| 498 | - $v = substr($v, 0, $p) . " AS `$k`" . substr($v, $p); |
|
| 498 | + $v = substr($v, 0, $p)." AS `$k`".substr($v, $p); |
|
| 499 | 499 | } else { |
| 500 | 500 | $v .= " AS `$k`"; |
| 501 | 501 | } |
| 502 | 502 | } |
| 503 | - $res .= ', ' . $v; |
|
| 503 | + $res .= ', '.$v; |
|
| 504 | 504 | } |
| 505 | 505 | } |
| 506 | 506 | |
@@ -529,13 +529,13 @@ discard block |
||
| 529 | 529 | function _mysql_traite_query($query, $db = '', $prefixe = '') { |
| 530 | 530 | |
| 531 | 531 | if ($GLOBALS['mysql_rappel_nom_base'] and $db) { |
| 532 | - $pref = '`' . $db . '`.'; |
|
| 532 | + $pref = '`'.$db.'`.'; |
|
| 533 | 533 | } else { |
| 534 | 534 | $pref = ''; |
| 535 | 535 | } |
| 536 | 536 | |
| 537 | 537 | if ($prefixe) { |
| 538 | - $pref .= $prefixe . "_"; |
|
| 538 | + $pref .= $prefixe."_"; |
|
| 539 | 539 | } |
| 540 | 540 | |
| 541 | 541 | if (!preg_match('/\s(SET|VALUES|WHERE|DATABASE)\s/i', $query, $regs)) { |
@@ -549,12 +549,12 @@ discard block |
||
| 549 | 549 | if (stripos($suite, "SELECT") !== false) { |
| 550 | 550 | list($suite, $textes) = query_echappe_textes($suite); |
| 551 | 551 | if (preg_match('/^(.*?)([(]\s*SELECT\b.*)$/si', $suite, $r)) { |
| 552 | - $suite = $r[1] . _mysql_traite_query($r[2], $db, $prefixe); |
|
| 552 | + $suite = $r[1]._mysql_traite_query($r[2], $db, $prefixe); |
|
| 553 | 553 | } |
| 554 | 554 | $suite = query_reinjecte_textes($suite, $textes); |
| 555 | 555 | } |
| 556 | 556 | } |
| 557 | - $r = preg_replace(_SQL_PREFIXE_TABLE_MYSQL, '\1' . $pref, $query) . $suite; |
|
| 557 | + $r = preg_replace(_SQL_PREFIXE_TABLE_MYSQL, '\1'.$pref, $query).$suite; |
|
| 558 | 558 | |
| 559 | 559 | // en option, remplacer les emoji (que mysql ne sait pas gérer) en 💩 |
| 560 | 560 | if (defined('_MYSQL_NOPLANES') and _MYSQL_NOPLANES and lire_meta('charset_sql_connexion') == 'utf8') { |
@@ -584,7 +584,7 @@ discard block |
||
| 584 | 584 | $link = _mysql_link($serveur); |
| 585 | 585 | $ok = mysqli_select_db($link, $db); |
| 586 | 586 | if (!$ok) { |
| 587 | - spip_log('Echec mysqli_selectdb. Erreur : ' . mysqli_error($link), 'mysql.' . _LOG_CRITIQUE); |
|
| 587 | + spip_log('Echec mysqli_selectdb. Erreur : '.mysqli_error($link), 'mysql.'._LOG_CRITIQUE); |
|
| 588 | 588 | } |
| 589 | 589 | |
| 590 | 590 | return $ok; |
@@ -674,10 +674,10 @@ discard block |
||
| 674 | 674 | |
| 675 | 675 | $character_set = ""; |
| 676 | 676 | if (@$GLOBALS['meta']['charset_sql_base']) { |
| 677 | - $character_set .= " CHARACTER SET " . $GLOBALS['meta']['charset_sql_base']; |
|
| 677 | + $character_set .= " CHARACTER SET ".$GLOBALS['meta']['charset_sql_base']; |
|
| 678 | 678 | } |
| 679 | 679 | if (@$GLOBALS['meta']['charset_collation_sql_base']) { |
| 680 | - $character_set .= " COLLATE " . $GLOBALS['meta']['charset_collation_sql_base']; |
|
| 680 | + $character_set .= " COLLATE ".$GLOBALS['meta']['charset_collation_sql_base']; |
|
| 681 | 681 | } |
| 682 | 682 | |
| 683 | 683 | foreach ($champs as $k => $v) { |
@@ -686,7 +686,7 @@ discard block |
||
| 686 | 686 | if (preg_match(',(char|text),i', $defs[1]) |
| 687 | 687 | and !preg_match(',(binary|CHARACTER|COLLATE),i', $v) |
| 688 | 688 | ) { |
| 689 | - $v = $defs[1] . $character_set . ' ' . substr($v, strlen($defs[1])); |
|
| 689 | + $v = $defs[1].$character_set.' '.substr($v, strlen($defs[1])); |
|
| 690 | 690 | } |
| 691 | 691 | } |
| 692 | 692 | |
@@ -698,7 +698,7 @@ discard block |
||
| 698 | 698 | $s = ","; |
| 699 | 699 | } |
| 700 | 700 | $temporary = $temporary ? 'TEMPORARY' : ''; |
| 701 | - $q = "CREATE $temporary TABLE IF NOT EXISTS $nom ($query" . ($keys ? ",$keys" : '') . ")" |
|
| 701 | + $q = "CREATE $temporary TABLE IF NOT EXISTS $nom ($query".($keys ? ",$keys" : '').")" |
|
| 702 | 702 | . " ENGINE=MyISAM" |
| 703 | 703 | . ($character_set ? " DEFAULT $character_set" : "") |
| 704 | 704 | . "\n"; |
@@ -778,7 +778,7 @@ discard block |
||
| 778 | 778 | return false; |
| 779 | 779 | } |
| 780 | 780 | |
| 781 | - $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 781 | + $query = "CREATE VIEW $nom AS ".$query_select; |
|
| 782 | 782 | |
| 783 | 783 | return spip_mysql_query($query, $serveur, $requeter); |
| 784 | 784 | } |
@@ -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 | /** |
@@ -930,22 +930,22 @@ discard block |
||
| 930 | 930 | } |
| 931 | 931 | if ($val['Default'] === '0' || $val['Default']) { |
| 932 | 932 | if (preg_match('/[A-Z_]/', $val['Default'])) { |
| 933 | - $nfields[$val["Field"]] .= ' DEFAULT ' . $val['Default']; |
|
| 933 | + $nfields[$val["Field"]] .= ' DEFAULT '.$val['Default']; |
|
| 934 | 934 | } else { |
| 935 | - $nfields[$val["Field"]] .= " DEFAULT '" . $val['Default'] . "'"; |
|
| 935 | + $nfields[$val["Field"]] .= " DEFAULT '".$val['Default']."'"; |
|
| 936 | 936 | } |
| 937 | 937 | } |
| 938 | 938 | if ($val['Extra']) { |
| 939 | - $nfields[$val["Field"]] .= ' ' . $val['Extra']; |
|
| 939 | + $nfields[$val["Field"]] .= ' '.$val['Extra']; |
|
| 940 | 940 | } |
| 941 | 941 | if ($val['Key'] == 'PRI') { |
| 942 | 942 | $nkeys['PRIMARY KEY'] = $val["Field"]; |
| 943 | 943 | } else { |
| 944 | 944 | if ($val['Key'] == 'MUL') { |
| 945 | - $nkeys['KEY ' . $val["Field"]] = $val["Field"]; |
|
| 945 | + $nkeys['KEY '.$val["Field"]] = $val["Field"]; |
|
| 946 | 946 | } else { |
| 947 | 947 | if ($val['Key'] == 'UNI') { |
| 948 | - $nkeys['UNIQUE KEY ' . $val["Field"]] = $val["Field"]; |
|
| 948 | + $nkeys['UNIQUE KEY '.$val["Field"]] = $val["Field"]; |
|
| 949 | 949 | } |
| 950 | 950 | } |
| 951 | 951 | } |
@@ -1016,7 +1016,7 @@ discard block |
||
| 1016 | 1016 | $serveur = '', |
| 1017 | 1017 | $requeter = true |
| 1018 | 1018 | ) { |
| 1019 | - $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 1019 | + $c = !$groupby ? '*' : ('DISTINCT '.(is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 1020 | 1020 | |
| 1021 | 1021 | $r = spip_mysql_select("COUNT($c)", $from, $where, '', '', '', $having, $serveur, $requeter); |
| 1022 | 1022 | if (!$requeter) { |
@@ -1056,7 +1056,7 @@ discard block |
||
| 1056 | 1056 | if ($s) { |
| 1057 | 1057 | $trace = debug_backtrace(); |
| 1058 | 1058 | if ($trace[0]['function'] != "spip_mysql_error") { |
| 1059 | - spip_log("$s - $query - " . sql_error_backtrace(), 'mysql.' . _LOG_ERREUR); |
|
| 1059 | + spip_log("$s - $query - ".sql_error_backtrace(), 'mysql.'._LOG_ERREUR); |
|
| 1060 | 1060 | } |
| 1061 | 1061 | } |
| 1062 | 1062 | |
@@ -1214,7 +1214,7 @@ discard block |
||
| 1214 | 1214 | $couples[$champ] = spip_mysql_cite($val, $fields[$champ]); |
| 1215 | 1215 | } |
| 1216 | 1216 | |
| 1217 | - return spip_mysql_insert($table, "(" . join(',', array_keys($couples)) . ")", "(" . join(',', $couples) . ")", $desc, |
|
| 1217 | + return spip_mysql_insert($table, "(".join(',', array_keys($couples)).")", "(".join(',', $couples).")", $desc, |
|
| 1218 | 1218 | $serveur, $requeter); |
| 1219 | 1219 | } |
| 1220 | 1220 | |
@@ -1248,7 +1248,7 @@ discard block |
||
| 1248 | 1248 | } |
| 1249 | 1249 | $fields = isset($desc['field']) ? $desc['field'] : array(); |
| 1250 | 1250 | |
| 1251 | - $cles = "(" . join(',', array_keys(reset($tab_couples))) . ')'; |
|
| 1251 | + $cles = "(".join(',', array_keys(reset($tab_couples))).')'; |
|
| 1252 | 1252 | $valeurs = array(); |
| 1253 | 1253 | $r = false; |
| 1254 | 1254 | |
@@ -1257,7 +1257,7 @@ discard block |
||
| 1257 | 1257 | foreach ($couples as $champ => $val) { |
| 1258 | 1258 | $couples[$champ] = spip_mysql_cite($val, $fields[$champ]); |
| 1259 | 1259 | } |
| 1260 | - $valeurs[] = '(' . join(',', $couples) . ')'; |
|
| 1260 | + $valeurs[] = '('.join(',', $couples).')'; |
|
| 1261 | 1261 | if (count($valeurs) >= 100) { |
| 1262 | 1262 | $r = spip_mysql_insert($table, $cles, join(', ', $valeurs), $desc, $serveur, $requeter); |
| 1263 | 1263 | $valeurs = array(); |
@@ -1294,7 +1294,7 @@ discard block |
||
| 1294 | 1294 | function spip_mysql_update($table, $champs, $where = '', $desc = array(), $serveur = '', $requeter = true) { |
| 1295 | 1295 | $set = array(); |
| 1296 | 1296 | foreach ($champs as $champ => $val) { |
| 1297 | - $set[] = $champ . "=$val"; |
|
| 1297 | + $set[] = $champ."=$val"; |
|
| 1298 | 1298 | } |
| 1299 | 1299 | if (!empty($set)) { |
| 1300 | 1300 | return spip_mysql_query( |
@@ -1348,7 +1348,7 @@ discard block |
||
| 1348 | 1348 | } |
| 1349 | 1349 | $set = array(); |
| 1350 | 1350 | foreach ($champs as $champ => $val) { |
| 1351 | - $set[] = $champ . '=' . spip_mysql_cite($val, @$fields[$champ]); |
|
| 1351 | + $set[] = $champ.'='.spip_mysql_cite($val, @$fields[$champ]); |
|
| 1352 | 1352 | } |
| 1353 | 1353 | |
| 1354 | 1354 | return spip_mysql_query( |
@@ -1413,8 +1413,8 @@ discard block |
||
| 1413 | 1413 | * - False en cas d'erreur. |
| 1414 | 1414 | **/ |
| 1415 | 1415 | function spip_mysql_replace($table, $couples, $desc = array(), $serveur = '', $requeter = true) { |
| 1416 | - return spip_mysql_query("REPLACE $table (" . join(',', array_keys($couples)) . ') VALUES (' . join(',', |
|
| 1417 | - array_map('_q', $couples)) . ')', $serveur, $requeter); |
|
| 1416 | + return spip_mysql_query("REPLACE $table (".join(',', array_keys($couples)).') VALUES ('.join(',', |
|
| 1417 | + array_map('_q', $couples)).')', $serveur, $requeter); |
|
| 1418 | 1418 | } |
| 1419 | 1419 | |
| 1420 | 1420 | |
@@ -1443,10 +1443,10 @@ discard block |
||
| 1443 | 1443 | * - False en cas d'erreur. |
| 1444 | 1444 | **/ |
| 1445 | 1445 | function spip_mysql_replace_multi($table, $tab_couples, $desc = array(), $serveur = '', $requeter = true) { |
| 1446 | - $cles = "(" . join(',', array_keys($tab_couples[0])) . ')'; |
|
| 1446 | + $cles = "(".join(',', array_keys($tab_couples[0])).')'; |
|
| 1447 | 1447 | $valeurs = array(); |
| 1448 | 1448 | foreach ($tab_couples as $couples) { |
| 1449 | - $valeurs[] = '(' . join(',', array_map('_q', $couples)) . ')'; |
|
| 1449 | + $valeurs[] = '('.join(',', array_map('_q', $couples)).')'; |
|
| 1450 | 1450 | } |
| 1451 | 1451 | $valeurs = implode(', ', $valeurs); |
| 1452 | 1452 | |
@@ -1466,28 +1466,28 @@ discard block |
||
| 1466 | 1466 | */ |
| 1467 | 1467 | function spip_mysql_multi($objet, $lang) { |
| 1468 | 1468 | $lengthlang = strlen("[$lang]"); |
| 1469 | - $posmulti = "INSTR(" . $objet . ", '<multi>')"; |
|
| 1470 | - $posfinmulti = "INSTR(" . $objet . ", '</multi>')"; |
|
| 1471 | - $debutchaine = "LEFT(" . $objet . ", $posmulti-1)"; |
|
| 1472 | - $finchaine = "RIGHT(" . $objet . ", CHAR_LENGTH(" . $objet . ") -(7+$posfinmulti))"; |
|
| 1473 | - $chainemulti = "TRIM(SUBSTRING(" . $objet . ", $posmulti+7, $posfinmulti -(7+$posmulti)))"; |
|
| 1474 | - $poslang = "INSTR($chainemulti,'[" . $lang . "]')"; |
|
| 1469 | + $posmulti = "INSTR(".$objet.", '<multi>')"; |
|
| 1470 | + $posfinmulti = "INSTR(".$objet.", '</multi>')"; |
|
| 1471 | + $debutchaine = "LEFT(".$objet.", $posmulti-1)"; |
|
| 1472 | + $finchaine = "RIGHT(".$objet.", CHAR_LENGTH(".$objet.") -(7+$posfinmulti))"; |
|
| 1473 | + $chainemulti = "TRIM(SUBSTRING(".$objet.", $posmulti+7, $posfinmulti -(7+$posmulti)))"; |
|
| 1474 | + $poslang = "INSTR($chainemulti,'[".$lang."]')"; |
|
| 1475 | 1475 | $poslang = "IF($poslang=0,INSTR($chainemulti,']')+1,$poslang+$lengthlang)"; |
| 1476 | - $chainelang = "TRIM(SUBSTRING(" . $objet . ", $posmulti+7+$poslang-1,$posfinmulti -($posmulti+7+$poslang-1) ))"; |
|
| 1477 | - $posfinlang = "INSTR(" . $chainelang . ", '[')"; |
|
| 1476 | + $chainelang = "TRIM(SUBSTRING(".$objet.", $posmulti+7+$poslang-1,$posfinmulti -($posmulti+7+$poslang-1) ))"; |
|
| 1477 | + $posfinlang = "INSTR(".$chainelang.", '[')"; |
|
| 1478 | 1478 | $chainelang = "IF($posfinlang>0,LEFT($chainelang,$posfinlang-1),$chainelang)"; |
| 1479 | 1479 | //$chainelang = "LEFT($chainelang,$posfinlang-1)"; |
| 1480 | - $retour = "(TRIM(IF($posmulti = 0 , " . |
|
| 1481 | - " TRIM(" . $objet . "), " . |
|
| 1482 | - " CONCAT( " . |
|
| 1483 | - " $debutchaine, " . |
|
| 1484 | - " IF( " . |
|
| 1485 | - " $poslang = 0, " . |
|
| 1486 | - " $chainemulti, " . |
|
| 1487 | - " $chainelang" . |
|
| 1488 | - " ), " . |
|
| 1489 | - " $finchaine" . |
|
| 1490 | - " ) " . |
|
| 1480 | + $retour = "(TRIM(IF($posmulti = 0 , ". |
|
| 1481 | + " TRIM(".$objet."), ". |
|
| 1482 | + " CONCAT( ". |
|
| 1483 | + " $debutchaine, ". |
|
| 1484 | + " IF( ". |
|
| 1485 | + " $poslang = 0, ". |
|
| 1486 | + " $chainemulti, ". |
|
| 1487 | + " $chainelang". |
|
| 1488 | + " ), ". |
|
| 1489 | + " $finchaine". |
|
| 1490 | + " ) ". |
|
| 1491 | 1491 | "))) AS multi"; |
| 1492 | 1492 | |
| 1493 | 1493 | return $retour; |
@@ -1504,7 +1504,7 @@ discard block |
||
| 1504 | 1504 | * Valeur hexadécimale pour MySQL |
| 1505 | 1505 | **/ |
| 1506 | 1506 | function spip_mysql_hex($v) { |
| 1507 | - return "0x" . $v; |
|
| 1507 | + return "0x".$v; |
|
| 1508 | 1508 | } |
| 1509 | 1509 | |
| 1510 | 1510 | /** |
@@ -1594,7 +1594,7 @@ discard block |
||
| 1594 | 1594 | if (is_numeric($v)) { |
| 1595 | 1595 | return strval($v); |
| 1596 | 1596 | } |
| 1597 | - return "'" . addslashes($v) . "'"; |
|
| 1597 | + return "'".addslashes($v)."'"; |
|
| 1598 | 1598 | } |
| 1599 | 1599 | |
| 1600 | 1600 | if (is_null($v) |
@@ -1616,7 +1616,7 @@ discard block |
||
| 1616 | 1616 | } |
| 1617 | 1617 | } |
| 1618 | 1618 | |
| 1619 | - return ("'" . addslashes($v) . "'"); |
|
| 1619 | + return ("'".addslashes($v)."'"); |
|
| 1620 | 1620 | } |
| 1621 | 1621 | |
| 1622 | 1622 | |
@@ -1645,9 +1645,9 @@ discard block |
||
| 1645 | 1645 | $connexion = &$GLOBALS['connexions'][0]; |
| 1646 | 1646 | $bd = $connexion['db']; |
| 1647 | 1647 | $prefixe = $connexion['prefixe']; |
| 1648 | - $nom = "$bd:$prefixe:$nom" . _LOCK_TIME; |
|
| 1648 | + $nom = "$bd:$prefixe:$nom"._LOCK_TIME; |
|
| 1649 | 1649 | |
| 1650 | - $connexion['last'] = $q = "SELECT GET_LOCK(" . _q($nom) . ", $timeout) AS n"; |
|
| 1650 | + $connexion['last'] = $q = "SELECT GET_LOCK("._q($nom).", $timeout) AS n"; |
|
| 1651 | 1651 | |
| 1652 | 1652 | $q = @sql_fetch(mysqli_query(_mysql_link(), $q)); |
| 1653 | 1653 | if (!$q) { |
@@ -1674,9 +1674,9 @@ discard block |
||
| 1674 | 1674 | $connexion = &$GLOBALS['connexions'][0]; |
| 1675 | 1675 | $bd = $connexion['db']; |
| 1676 | 1676 | $prefixe = $connexion['prefixe']; |
| 1677 | - $nom = "$bd:$prefixe:$nom" . _LOCK_TIME; |
|
| 1677 | + $nom = "$bd:$prefixe:$nom"._LOCK_TIME; |
|
| 1678 | 1678 | |
| 1679 | - $connexion['last'] = $q = "SELECT RELEASE_LOCK(" . _q($nom) . ")"; |
|
| 1679 | + $connexion['last'] = $q = "SELECT RELEASE_LOCK("._q($nom).")"; |
|
| 1680 | 1680 | mysqli_query(_mysql_link(), $q); |
| 1681 | 1681 | } |
| 1682 | 1682 | |
@@ -1712,7 +1712,7 @@ discard block |
||
| 1712 | 1712 | } else { |
| 1713 | 1713 | $GLOBALS['mysql_rappel_nom_base'] = false; |
| 1714 | 1714 | |
| 1715 | - return "\$GLOBALS['mysql_rappel_nom_base'] = false; " . |
|
| 1715 | + return "\$GLOBALS['mysql_rappel_nom_base'] = false; ". |
|
| 1716 | 1716 | "/* echec de test_rappel_nom_base_mysql a l'installation. */\n"; |
| 1717 | 1717 | } |
| 1718 | 1718 | } |
@@ -18,7 +18,7 @@ discard block |
||
| 18 | 18 | */ |
| 19 | 19 | |
| 20 | 20 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 21 | - return; |
|
| 21 | + return; |
|
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | // TODO: get/set_caracteres ? |
@@ -42,91 +42,91 @@ discard block |
||
| 42 | 42 | * @return array|bool |
| 43 | 43 | */ |
| 44 | 44 | function req_sqlite_dist($addr, $port, $login, $pass, $db = '', $prefixe = '', $sqlite_version = '') { |
| 45 | - static $last_connect = array(); |
|
| 46 | - |
|
| 47 | - // si provient de selectdb |
|
| 48 | - // un code pour etre sur que l'on vient de select_db() |
|
| 49 | - if (strpos($db, $code = '@selectdb@') !== false) { |
|
| 50 | - foreach (array('addr', 'port', 'login', 'pass', 'prefixe') as $a) { |
|
| 51 | - $$a = $last_connect[$a]; |
|
| 52 | - } |
|
| 53 | - $db = str_replace($code, '', $db); |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - /* |
|
| 45 | + static $last_connect = array(); |
|
| 46 | + |
|
| 47 | + // si provient de selectdb |
|
| 48 | + // un code pour etre sur que l'on vient de select_db() |
|
| 49 | + if (strpos($db, $code = '@selectdb@') !== false) { |
|
| 50 | + foreach (array('addr', 'port', 'login', 'pass', 'prefixe') as $a) { |
|
| 51 | + $$a = $last_connect[$a]; |
|
| 52 | + } |
|
| 53 | + $db = str_replace($code, '', $db); |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + /* |
|
| 57 | 57 | * En sqlite, seule l'adresse du fichier est importante. |
| 58 | 58 | * Ce sera $db le nom, |
| 59 | 59 | * le path est $addr |
| 60 | 60 | * (_DIR_DB si $addr est vide) |
| 61 | 61 | */ |
| 62 | - _sqlite_init(); |
|
| 63 | - |
|
| 64 | - // determiner le dossier de la base : $addr ou _DIR_DB |
|
| 65 | - $f = _DIR_DB; |
|
| 66 | - if ($addr and strpos($addr, '/') !== false) { |
|
| 67 | - $f = rtrim($addr, '/') . '/'; |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - // un nom de base demande et impossible d'obtenir la base, on s'en va : |
|
| 71 | - // il faut que la base existe ou que le repertoire parent soit writable |
|
| 72 | - if ($db and !is_file($f .= $db . '.sqlite') and !is_writable(dirname($f))) { |
|
| 73 | - spip_log("base $f non trouvee ou droits en ecriture manquants", 'sqlite.' . _LOG_HS); |
|
| 74 | - |
|
| 75 | - return false; |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - // charger les modules sqlite au besoin |
|
| 79 | - if (!_sqlite_charger_version($sqlite_version)) { |
|
| 80 | - spip_log("Impossible de trouver/charger le module SQLite ($sqlite_version)!", 'sqlite.' . _LOG_HS); |
|
| 81 | - |
|
| 82 | - return false; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - // chargement des constantes |
|
| 86 | - // il ne faut pas definir les constantes avant d'avoir charge les modules sqlite |
|
| 87 | - $define = "spip_sqlite" . $sqlite_version . "_constantes"; |
|
| 88 | - $define(); |
|
| 89 | - |
|
| 90 | - $ok = false; |
|
| 91 | - if (!$db) { |
|
| 92 | - // si pas de db -> |
|
| 93 | - // base temporaire tant qu'on ne connait pas son vrai nom |
|
| 94 | - // pour tester la connexion |
|
| 95 | - $db = "_sqlite" . $sqlite_version . "_install"; |
|
| 96 | - $tmp = _DIR_DB . $db . ".sqlite"; |
|
| 97 | - $ok = $link = new \PDO("sqlite:$tmp"); |
|
| 98 | - } else { |
|
| 99 | - // Ouvrir (eventuellement creer la base) |
|
| 100 | - $ok = $link = new \PDO("sqlite:$f"); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - if (!$ok) { |
|
| 104 | - $e = _sqlite_last_error_from_link($link); |
|
| 105 | - spip_log("Impossible d'ouvrir la base SQLite($sqlite_version) $f : $e", 'sqlite.' . _LOG_HS); |
|
| 106 | - |
|
| 107 | - return false; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - if ($link) { |
|
| 111 | - $last_connect = array( |
|
| 112 | - 'addr' => $addr, |
|
| 113 | - 'port' => $port, |
|
| 114 | - 'login' => $login, |
|
| 115 | - 'pass' => $pass, |
|
| 116 | - 'db' => $db, |
|
| 117 | - 'prefixe' => $prefixe, |
|
| 118 | - ); |
|
| 119 | - // etre sur qu'on definit bien les fonctions a chaque nouvelle connexion |
|
| 120 | - include_spip('req/sqlite_fonctions'); |
|
| 121 | - _sqlite_init_functions($link); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - return array( |
|
| 125 | - 'db' => $db, |
|
| 126 | - 'prefixe' => $prefixe ? $prefixe : $db, |
|
| 127 | - 'link' => $link, |
|
| 128 | - 'total_requetes' => 0, |
|
| 129 | - ); |
|
| 62 | + _sqlite_init(); |
|
| 63 | + |
|
| 64 | + // determiner le dossier de la base : $addr ou _DIR_DB |
|
| 65 | + $f = _DIR_DB; |
|
| 66 | + if ($addr and strpos($addr, '/') !== false) { |
|
| 67 | + $f = rtrim($addr, '/') . '/'; |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + // un nom de base demande et impossible d'obtenir la base, on s'en va : |
|
| 71 | + // il faut que la base existe ou que le repertoire parent soit writable |
|
| 72 | + if ($db and !is_file($f .= $db . '.sqlite') and !is_writable(dirname($f))) { |
|
| 73 | + spip_log("base $f non trouvee ou droits en ecriture manquants", 'sqlite.' . _LOG_HS); |
|
| 74 | + |
|
| 75 | + return false; |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + // charger les modules sqlite au besoin |
|
| 79 | + if (!_sqlite_charger_version($sqlite_version)) { |
|
| 80 | + spip_log("Impossible de trouver/charger le module SQLite ($sqlite_version)!", 'sqlite.' . _LOG_HS); |
|
| 81 | + |
|
| 82 | + return false; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + // chargement des constantes |
|
| 86 | + // il ne faut pas definir les constantes avant d'avoir charge les modules sqlite |
|
| 87 | + $define = "spip_sqlite" . $sqlite_version . "_constantes"; |
|
| 88 | + $define(); |
|
| 89 | + |
|
| 90 | + $ok = false; |
|
| 91 | + if (!$db) { |
|
| 92 | + // si pas de db -> |
|
| 93 | + // base temporaire tant qu'on ne connait pas son vrai nom |
|
| 94 | + // pour tester la connexion |
|
| 95 | + $db = "_sqlite" . $sqlite_version . "_install"; |
|
| 96 | + $tmp = _DIR_DB . $db . ".sqlite"; |
|
| 97 | + $ok = $link = new \PDO("sqlite:$tmp"); |
|
| 98 | + } else { |
|
| 99 | + // Ouvrir (eventuellement creer la base) |
|
| 100 | + $ok = $link = new \PDO("sqlite:$f"); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + if (!$ok) { |
|
| 104 | + $e = _sqlite_last_error_from_link($link); |
|
| 105 | + spip_log("Impossible d'ouvrir la base SQLite($sqlite_version) $f : $e", 'sqlite.' . _LOG_HS); |
|
| 106 | + |
|
| 107 | + return false; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + if ($link) { |
|
| 111 | + $last_connect = array( |
|
| 112 | + 'addr' => $addr, |
|
| 113 | + 'port' => $port, |
|
| 114 | + 'login' => $login, |
|
| 115 | + 'pass' => $pass, |
|
| 116 | + 'db' => $db, |
|
| 117 | + 'prefixe' => $prefixe, |
|
| 118 | + ); |
|
| 119 | + // etre sur qu'on definit bien les fonctions a chaque nouvelle connexion |
|
| 120 | + include_spip('req/sqlite_fonctions'); |
|
| 121 | + _sqlite_init_functions($link); |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + return array( |
|
| 125 | + 'db' => $db, |
|
| 126 | + 'prefixe' => $prefixe ? $prefixe : $db, |
|
| 127 | + 'link' => $link, |
|
| 128 | + 'total_requetes' => 0, |
|
| 129 | + ); |
|
| 130 | 130 | } |
| 131 | 131 | |
| 132 | 132 | |
@@ -145,14 +145,14 @@ discard block |
||
| 145 | 145 | * Resultat de la requete |
| 146 | 146 | */ |
| 147 | 147 | function spip_sqlite_query($query, $serveur = '', $requeter = true) { |
| 148 | - #spip_log("spip_sqlite_query() > $query",'sqlite.'._LOG_DEBUG); |
|
| 149 | - #_sqlite_init(); // fait la premiere fois dans spip_sqlite |
|
| 150 | - $query = spip_sqlite::traduire_requete($query, $serveur); |
|
| 151 | - if (!$requeter) { |
|
| 152 | - return $query; |
|
| 153 | - } |
|
| 148 | + #spip_log("spip_sqlite_query() > $query",'sqlite.'._LOG_DEBUG); |
|
| 149 | + #_sqlite_init(); // fait la premiere fois dans spip_sqlite |
|
| 150 | + $query = spip_sqlite::traduire_requete($query, $serveur); |
|
| 151 | + if (!$requeter) { |
|
| 152 | + return $query; |
|
| 153 | + } |
|
| 154 | 154 | |
| 155 | - return spip_sqlite::executer_requete($query, $serveur); |
|
| 155 | + return spip_sqlite::executer_requete($query, $serveur); |
|
| 156 | 156 | } |
| 157 | 157 | |
| 158 | 158 | |
@@ -169,11 +169,11 @@ discard block |
||
| 169 | 169 | */ |
| 170 | 170 | function spip_sqlite_alter($query, $serveur = '', $requeter = true) { |
| 171 | 171 | |
| 172 | - $query = spip_sqlite_query("ALTER $query", $serveur, false); |
|
| 173 | - // traduire la requete pour recuperer les bons noms de table |
|
| 174 | - $query = spip_sqlite::traduire_requete($query, $serveur); |
|
| 172 | + $query = spip_sqlite_query("ALTER $query", $serveur, false); |
|
| 173 | + // traduire la requete pour recuperer les bons noms de table |
|
| 174 | + $query = spip_sqlite::traduire_requete($query, $serveur); |
|
| 175 | 175 | |
| 176 | - /* |
|
| 176 | + /* |
|
| 177 | 177 | * la il faut faire les transformations |
| 178 | 178 | * si ALTER TABLE x (DROP|CHANGE) y |
| 179 | 179 | * |
@@ -182,239 +182,239 @@ discard block |
||
| 182 | 182 | * 3) faire chaque requete independemment |
| 183 | 183 | */ |
| 184 | 184 | |
| 185 | - // 1 |
|
| 186 | - if (preg_match("/\s*(ALTER(\s*IGNORE)?\s*TABLE\s*([^\s]*))\s*(.*)?/is", $query, $regs)) { |
|
| 187 | - $debut = $regs[1]; |
|
| 188 | - $table = $regs[3]; |
|
| 189 | - $suite = $regs[4]; |
|
| 190 | - } else { |
|
| 191 | - spip_log("SQLite : Probleme de ALTER TABLE mal forme dans $query", 'sqlite.' . _LOG_ERREUR); |
|
| 192 | - |
|
| 193 | - return false; |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - // 2 |
|
| 197 | - // il faudrait une regexp pour eviter de spliter ADD PRIMARY KEY (colA, colB) |
|
| 198 | - // tout en cassant "ADD PRIMARY KEY (colA, colB), ADD INDEX (chose)"... en deux |
|
| 199 | - // ou revoir l'api de sql_alter en creant un |
|
| 200 | - // sql_alter_table($table,array($actions)); |
|
| 201 | - $todo = explode(',', $suite); |
|
| 202 | - |
|
| 203 | - // on remet les morceaux dechires ensembles... que c'est laid ! |
|
| 204 | - $todo2 = array(); |
|
| 205 | - $i = 0; |
|
| 206 | - $ouverte = false; |
|
| 207 | - while ($do = array_shift($todo)) { |
|
| 208 | - $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . "," . $do : $do; |
|
| 209 | - $o = (false !== strpos($do, "(")); |
|
| 210 | - $f = (false !== strpos($do, ")")); |
|
| 211 | - if ($o and !$f) { |
|
| 212 | - $ouverte = true; |
|
| 213 | - } elseif ($f) { |
|
| 214 | - $ouverte = false; |
|
| 215 | - } |
|
| 216 | - if (!$ouverte) { |
|
| 217 | - $i++; |
|
| 218 | - } |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - // 3 |
|
| 222 | - $resultats = array(); |
|
| 223 | - foreach ($todo2 as $do) { |
|
| 224 | - $do = trim($do); |
|
| 225 | - if (!preg_match('/(DROP PRIMARY KEY|DROP KEY|DROP INDEX|DROP COLUMN|DROP' |
|
| 226 | - . '|CHANGE COLUMN|CHANGE|MODIFY|RENAME TO|RENAME' |
|
| 227 | - . '|ADD PRIMARY KEY|ADD KEY|ADD INDEX|ADD UNIQUE KEY|ADD UNIQUE' |
|
| 228 | - . '|ADD COLUMN|ADD' |
|
| 229 | - . ')\s*([^\s]*)\s*(.*)?/i', $do, $matches) |
|
| 230 | - ) { |
|
| 231 | - spip_log("SQLite : Probleme de ALTER TABLE, utilisation non reconnue dans : $do \n(requete d'origine : $query)", |
|
| 232 | - 'sqlite.' . _LOG_ERREUR); |
|
| 233 | - |
|
| 234 | - return false; |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - $cle = strtoupper($matches[1]); |
|
| 238 | - $colonne_origine = $matches[2]; |
|
| 239 | - $colonne_destination = ''; |
|
| 240 | - |
|
| 241 | - $def = $matches[3]; |
|
| 242 | - |
|
| 243 | - // eluder une eventuelle clause before|after|first inutilisable |
|
| 244 | - $defr = rtrim(preg_replace('/(BEFORE|AFTER|FIRST)(.*)$/is', '', $def)); |
|
| 245 | - $defo = $defr; // garder la def d'origine pour certains cas |
|
| 246 | - // remplacer les definitions venant de mysql |
|
| 247 | - $defr = _sqlite_remplacements_definitions_table($defr); |
|
| 248 | - |
|
| 249 | - // reinjecter dans le do |
|
| 250 | - $do = str_replace($def, $defr, $do); |
|
| 251 | - $def = $defr; |
|
| 252 | - |
|
| 253 | - switch ($cle) { |
|
| 254 | - // suppression d'un index |
|
| 255 | - case 'DROP KEY': |
|
| 256 | - case 'DROP INDEX': |
|
| 257 | - $nom_index = $colonne_origine; |
|
| 258 | - spip_sqlite_drop_index($nom_index, $table, $serveur); |
|
| 259 | - break; |
|
| 260 | - |
|
| 261 | - // suppression d'une pk |
|
| 262 | - case 'DROP PRIMARY KEY': |
|
| 263 | - if (!_sqlite_modifier_table( |
|
| 264 | - $table, |
|
| 265 | - $colonne_origine, |
|
| 266 | - array('key' => array('PRIMARY KEY' => '')), |
|
| 267 | - $serveur) |
|
| 268 | - ) { |
|
| 269 | - return false; |
|
| 270 | - } |
|
| 271 | - break; |
|
| 272 | - // suppression d'une colonne |
|
| 273 | - case 'DROP COLUMN': |
|
| 274 | - case 'DROP': |
|
| 275 | - if (!_sqlite_modifier_table( |
|
| 276 | - $table, |
|
| 277 | - array($colonne_origine => ""), |
|
| 278 | - array(), |
|
| 279 | - $serveur) |
|
| 280 | - ) { |
|
| 281 | - return false; |
|
| 282 | - } |
|
| 283 | - break; |
|
| 284 | - |
|
| 285 | - case 'CHANGE COLUMN': |
|
| 286 | - case 'CHANGE': |
|
| 287 | - // recuperer le nom de la future colonne |
|
| 288 | - // on reprend la def d'origine car _sqlite_modifier_table va refaire la translation |
|
| 289 | - // en tenant compte de la cle primaire (ce qui est mieux) |
|
| 290 | - $def = trim($defo); |
|
| 291 | - $colonne_destination = substr($def, 0, strpos($def, ' ')); |
|
| 292 | - $def = substr($def, strlen($colonne_destination) + 1); |
|
| 293 | - |
|
| 294 | - if (!_sqlite_modifier_table( |
|
| 295 | - $table, |
|
| 296 | - array($colonne_origine => $colonne_destination), |
|
| 297 | - array('field' => array($colonne_destination => $def)), |
|
| 298 | - $serveur) |
|
| 299 | - ) { |
|
| 300 | - return false; |
|
| 301 | - } |
|
| 302 | - break; |
|
| 303 | - |
|
| 304 | - case 'MODIFY': |
|
| 305 | - // on reprend la def d'origine car _sqlite_modifier_table va refaire la translation |
|
| 306 | - // en tenant compte de la cle primaire (ce qui est mieux) |
|
| 307 | - if (!_sqlite_modifier_table( |
|
| 308 | - $table, |
|
| 309 | - $colonne_origine, |
|
| 310 | - array('field' => array($colonne_origine => $defo)), |
|
| 311 | - $serveur) |
|
| 312 | - ) { |
|
| 313 | - return false; |
|
| 314 | - } |
|
| 315 | - break; |
|
| 316 | - |
|
| 317 | - // pas geres en sqlite2 |
|
| 318 | - case 'RENAME': |
|
| 319 | - $do = "RENAME TO" . substr($do, 6); |
|
| 320 | - case 'RENAME TO': |
|
| 321 | - if (!spip_sqlite::executer_requete("$debut $do", $serveur)) { |
|
| 322 | - spip_log("SQLite : Erreur ALTER TABLE / RENAME : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 323 | - |
|
| 324 | - return false; |
|
| 325 | - } |
|
| 326 | - break; |
|
| 327 | - |
|
| 328 | - // ajout d'une pk |
|
| 329 | - case 'ADD PRIMARY KEY': |
|
| 330 | - $pk = trim(substr($do, 16)); |
|
| 331 | - $pk = ($pk[0] == '(') ? substr($pk, 1, -1) : $pk; |
|
| 332 | - if (!_sqlite_modifier_table( |
|
| 333 | - $table, |
|
| 334 | - $colonne_origine, |
|
| 335 | - array('key' => array('PRIMARY KEY' => $pk)), |
|
| 336 | - $serveur) |
|
| 337 | - ) { |
|
| 338 | - return false; |
|
| 339 | - } |
|
| 340 | - break; |
|
| 341 | - // ajout d'un index |
|
| 342 | - case 'ADD UNIQUE KEY': |
|
| 343 | - case 'ADD UNIQUE': |
|
| 344 | - $unique = true; |
|
| 345 | - case 'ADD INDEX': |
|
| 346 | - case 'ADD KEY': |
|
| 347 | - if (!isset($unique)) { |
|
| 348 | - $unique = false; |
|
| 349 | - } |
|
| 350 | - // peut etre "(colonne)" ou "nom_index (colonnes)" |
|
| 351 | - // bug potentiel si qqn met "(colonne, colonne)" |
|
| 352 | - // |
|
| 353 | - // nom_index (colonnes) |
|
| 354 | - if ($def) { |
|
| 355 | - $colonnes = substr($def, 1, -1); |
|
| 356 | - $nom_index = $colonne_origine; |
|
| 357 | - } else { |
|
| 358 | - // (colonne) |
|
| 359 | - if ($colonne_origine[0] == "(") { |
|
| 360 | - $colonnes = substr($colonne_origine, 1, -1); |
|
| 361 | - if (false !== strpos(",", $colonnes)) { |
|
| 362 | - spip_log(_LOG_GRAVITE_ERREUR, "SQLite : Erreur, impossible de creer un index sur plusieurs colonnes" |
|
| 363 | - . " sans qu'il ait de nom ($table, ($colonnes))", 'sqlite'); |
|
| 364 | - break; |
|
| 365 | - } else { |
|
| 366 | - $nom_index = $colonnes; |
|
| 367 | - } |
|
| 368 | - } // nom_index |
|
| 369 | - else { |
|
| 370 | - $nom_index = $colonnes = $colonne_origine; |
|
| 371 | - } |
|
| 372 | - } |
|
| 373 | - spip_sqlite_create_index($nom_index, $table, $colonnes, $unique, $serveur); |
|
| 374 | - break; |
|
| 375 | - |
|
| 376 | - // pas geres en sqlite2 |
|
| 377 | - case 'ADD COLUMN': |
|
| 378 | - $do = "ADD" . substr($do, 10); |
|
| 379 | - case 'ADD': |
|
| 380 | - default: |
|
| 381 | - if (!preg_match(',primary\s+key,i', $do)) { |
|
| 382 | - if (!spip_sqlite::executer_requete("$debut $do", $serveur)) { |
|
| 383 | - spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 384 | - |
|
| 385 | - return false; |
|
| 386 | - } |
|
| 387 | - break; |
|
| 388 | - |
|
| 389 | - } |
|
| 390 | - // ou si la colonne est aussi primary key |
|
| 391 | - // cas du add id_truc int primary key |
|
| 392 | - // ajout d'une colonne qui passe en primary key directe |
|
| 393 | - else { |
|
| 394 | - $def = trim(substr($do, 3)); |
|
| 395 | - $colonne_ajoutee = substr($def, 0, strpos($def, ' ')); |
|
| 396 | - $def = substr($def, strlen($colonne_ajoutee) + 1); |
|
| 397 | - $opts = array(); |
|
| 398 | - if (preg_match(',primary\s+key,i', $def)) { |
|
| 399 | - $opts['key'] = array('PRIMARY KEY' => $colonne_ajoutee); |
|
| 400 | - $def = preg_replace(',primary\s+key,i', '', $def); |
|
| 401 | - } |
|
| 402 | - $opts['field'] = array($colonne_ajoutee => $def); |
|
| 403 | - if (!_sqlite_modifier_table($table, array($colonne_ajoutee), $opts, $serveur)) { |
|
| 404 | - spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 405 | - |
|
| 406 | - return false; |
|
| 407 | - } |
|
| 408 | - } |
|
| 409 | - break; |
|
| 410 | - } |
|
| 411 | - // tout est bon, ouf ! |
|
| 412 | - spip_log("SQLite ($serveur) : Changements OK : $debut $do", 'sqlite.' . _LOG_INFO); |
|
| 413 | - } |
|
| 414 | - |
|
| 415 | - spip_log("SQLite ($serveur) : fin ALTER TABLE OK !", 'sqlite.' . _LOG_INFO); |
|
| 416 | - |
|
| 417 | - return true; |
|
| 185 | + // 1 |
|
| 186 | + if (preg_match("/\s*(ALTER(\s*IGNORE)?\s*TABLE\s*([^\s]*))\s*(.*)?/is", $query, $regs)) { |
|
| 187 | + $debut = $regs[1]; |
|
| 188 | + $table = $regs[3]; |
|
| 189 | + $suite = $regs[4]; |
|
| 190 | + } else { |
|
| 191 | + spip_log("SQLite : Probleme de ALTER TABLE mal forme dans $query", 'sqlite.' . _LOG_ERREUR); |
|
| 192 | + |
|
| 193 | + return false; |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + // 2 |
|
| 197 | + // il faudrait une regexp pour eviter de spliter ADD PRIMARY KEY (colA, colB) |
|
| 198 | + // tout en cassant "ADD PRIMARY KEY (colA, colB), ADD INDEX (chose)"... en deux |
|
| 199 | + // ou revoir l'api de sql_alter en creant un |
|
| 200 | + // sql_alter_table($table,array($actions)); |
|
| 201 | + $todo = explode(',', $suite); |
|
| 202 | + |
|
| 203 | + // on remet les morceaux dechires ensembles... que c'est laid ! |
|
| 204 | + $todo2 = array(); |
|
| 205 | + $i = 0; |
|
| 206 | + $ouverte = false; |
|
| 207 | + while ($do = array_shift($todo)) { |
|
| 208 | + $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . "," . $do : $do; |
|
| 209 | + $o = (false !== strpos($do, "(")); |
|
| 210 | + $f = (false !== strpos($do, ")")); |
|
| 211 | + if ($o and !$f) { |
|
| 212 | + $ouverte = true; |
|
| 213 | + } elseif ($f) { |
|
| 214 | + $ouverte = false; |
|
| 215 | + } |
|
| 216 | + if (!$ouverte) { |
|
| 217 | + $i++; |
|
| 218 | + } |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + // 3 |
|
| 222 | + $resultats = array(); |
|
| 223 | + foreach ($todo2 as $do) { |
|
| 224 | + $do = trim($do); |
|
| 225 | + if (!preg_match('/(DROP PRIMARY KEY|DROP KEY|DROP INDEX|DROP COLUMN|DROP' |
|
| 226 | + . '|CHANGE COLUMN|CHANGE|MODIFY|RENAME TO|RENAME' |
|
| 227 | + . '|ADD PRIMARY KEY|ADD KEY|ADD INDEX|ADD UNIQUE KEY|ADD UNIQUE' |
|
| 228 | + . '|ADD COLUMN|ADD' |
|
| 229 | + . ')\s*([^\s]*)\s*(.*)?/i', $do, $matches) |
|
| 230 | + ) { |
|
| 231 | + spip_log("SQLite : Probleme de ALTER TABLE, utilisation non reconnue dans : $do \n(requete d'origine : $query)", |
|
| 232 | + 'sqlite.' . _LOG_ERREUR); |
|
| 233 | + |
|
| 234 | + return false; |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + $cle = strtoupper($matches[1]); |
|
| 238 | + $colonne_origine = $matches[2]; |
|
| 239 | + $colonne_destination = ''; |
|
| 240 | + |
|
| 241 | + $def = $matches[3]; |
|
| 242 | + |
|
| 243 | + // eluder une eventuelle clause before|after|first inutilisable |
|
| 244 | + $defr = rtrim(preg_replace('/(BEFORE|AFTER|FIRST)(.*)$/is', '', $def)); |
|
| 245 | + $defo = $defr; // garder la def d'origine pour certains cas |
|
| 246 | + // remplacer les definitions venant de mysql |
|
| 247 | + $defr = _sqlite_remplacements_definitions_table($defr); |
|
| 248 | + |
|
| 249 | + // reinjecter dans le do |
|
| 250 | + $do = str_replace($def, $defr, $do); |
|
| 251 | + $def = $defr; |
|
| 252 | + |
|
| 253 | + switch ($cle) { |
|
| 254 | + // suppression d'un index |
|
| 255 | + case 'DROP KEY': |
|
| 256 | + case 'DROP INDEX': |
|
| 257 | + $nom_index = $colonne_origine; |
|
| 258 | + spip_sqlite_drop_index($nom_index, $table, $serveur); |
|
| 259 | + break; |
|
| 260 | + |
|
| 261 | + // suppression d'une pk |
|
| 262 | + case 'DROP PRIMARY KEY': |
|
| 263 | + if (!_sqlite_modifier_table( |
|
| 264 | + $table, |
|
| 265 | + $colonne_origine, |
|
| 266 | + array('key' => array('PRIMARY KEY' => '')), |
|
| 267 | + $serveur) |
|
| 268 | + ) { |
|
| 269 | + return false; |
|
| 270 | + } |
|
| 271 | + break; |
|
| 272 | + // suppression d'une colonne |
|
| 273 | + case 'DROP COLUMN': |
|
| 274 | + case 'DROP': |
|
| 275 | + if (!_sqlite_modifier_table( |
|
| 276 | + $table, |
|
| 277 | + array($colonne_origine => ""), |
|
| 278 | + array(), |
|
| 279 | + $serveur) |
|
| 280 | + ) { |
|
| 281 | + return false; |
|
| 282 | + } |
|
| 283 | + break; |
|
| 284 | + |
|
| 285 | + case 'CHANGE COLUMN': |
|
| 286 | + case 'CHANGE': |
|
| 287 | + // recuperer le nom de la future colonne |
|
| 288 | + // on reprend la def d'origine car _sqlite_modifier_table va refaire la translation |
|
| 289 | + // en tenant compte de la cle primaire (ce qui est mieux) |
|
| 290 | + $def = trim($defo); |
|
| 291 | + $colonne_destination = substr($def, 0, strpos($def, ' ')); |
|
| 292 | + $def = substr($def, strlen($colonne_destination) + 1); |
|
| 293 | + |
|
| 294 | + if (!_sqlite_modifier_table( |
|
| 295 | + $table, |
|
| 296 | + array($colonne_origine => $colonne_destination), |
|
| 297 | + array('field' => array($colonne_destination => $def)), |
|
| 298 | + $serveur) |
|
| 299 | + ) { |
|
| 300 | + return false; |
|
| 301 | + } |
|
| 302 | + break; |
|
| 303 | + |
|
| 304 | + case 'MODIFY': |
|
| 305 | + // on reprend la def d'origine car _sqlite_modifier_table va refaire la translation |
|
| 306 | + // en tenant compte de la cle primaire (ce qui est mieux) |
|
| 307 | + if (!_sqlite_modifier_table( |
|
| 308 | + $table, |
|
| 309 | + $colonne_origine, |
|
| 310 | + array('field' => array($colonne_origine => $defo)), |
|
| 311 | + $serveur) |
|
| 312 | + ) { |
|
| 313 | + return false; |
|
| 314 | + } |
|
| 315 | + break; |
|
| 316 | + |
|
| 317 | + // pas geres en sqlite2 |
|
| 318 | + case 'RENAME': |
|
| 319 | + $do = "RENAME TO" . substr($do, 6); |
|
| 320 | + case 'RENAME TO': |
|
| 321 | + if (!spip_sqlite::executer_requete("$debut $do", $serveur)) { |
|
| 322 | + spip_log("SQLite : Erreur ALTER TABLE / RENAME : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 323 | + |
|
| 324 | + return false; |
|
| 325 | + } |
|
| 326 | + break; |
|
| 327 | + |
|
| 328 | + // ajout d'une pk |
|
| 329 | + case 'ADD PRIMARY KEY': |
|
| 330 | + $pk = trim(substr($do, 16)); |
|
| 331 | + $pk = ($pk[0] == '(') ? substr($pk, 1, -1) : $pk; |
|
| 332 | + if (!_sqlite_modifier_table( |
|
| 333 | + $table, |
|
| 334 | + $colonne_origine, |
|
| 335 | + array('key' => array('PRIMARY KEY' => $pk)), |
|
| 336 | + $serveur) |
|
| 337 | + ) { |
|
| 338 | + return false; |
|
| 339 | + } |
|
| 340 | + break; |
|
| 341 | + // ajout d'un index |
|
| 342 | + case 'ADD UNIQUE KEY': |
|
| 343 | + case 'ADD UNIQUE': |
|
| 344 | + $unique = true; |
|
| 345 | + case 'ADD INDEX': |
|
| 346 | + case 'ADD KEY': |
|
| 347 | + if (!isset($unique)) { |
|
| 348 | + $unique = false; |
|
| 349 | + } |
|
| 350 | + // peut etre "(colonne)" ou "nom_index (colonnes)" |
|
| 351 | + // bug potentiel si qqn met "(colonne, colonne)" |
|
| 352 | + // |
|
| 353 | + // nom_index (colonnes) |
|
| 354 | + if ($def) { |
|
| 355 | + $colonnes = substr($def, 1, -1); |
|
| 356 | + $nom_index = $colonne_origine; |
|
| 357 | + } else { |
|
| 358 | + // (colonne) |
|
| 359 | + if ($colonne_origine[0] == "(") { |
|
| 360 | + $colonnes = substr($colonne_origine, 1, -1); |
|
| 361 | + if (false !== strpos(",", $colonnes)) { |
|
| 362 | + spip_log(_LOG_GRAVITE_ERREUR, "SQLite : Erreur, impossible de creer un index sur plusieurs colonnes" |
|
| 363 | + . " sans qu'il ait de nom ($table, ($colonnes))", 'sqlite'); |
|
| 364 | + break; |
|
| 365 | + } else { |
|
| 366 | + $nom_index = $colonnes; |
|
| 367 | + } |
|
| 368 | + } // nom_index |
|
| 369 | + else { |
|
| 370 | + $nom_index = $colonnes = $colonne_origine; |
|
| 371 | + } |
|
| 372 | + } |
|
| 373 | + spip_sqlite_create_index($nom_index, $table, $colonnes, $unique, $serveur); |
|
| 374 | + break; |
|
| 375 | + |
|
| 376 | + // pas geres en sqlite2 |
|
| 377 | + case 'ADD COLUMN': |
|
| 378 | + $do = "ADD" . substr($do, 10); |
|
| 379 | + case 'ADD': |
|
| 380 | + default: |
|
| 381 | + if (!preg_match(',primary\s+key,i', $do)) { |
|
| 382 | + if (!spip_sqlite::executer_requete("$debut $do", $serveur)) { |
|
| 383 | + spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 384 | + |
|
| 385 | + return false; |
|
| 386 | + } |
|
| 387 | + break; |
|
| 388 | + |
|
| 389 | + } |
|
| 390 | + // ou si la colonne est aussi primary key |
|
| 391 | + // cas du add id_truc int primary key |
|
| 392 | + // ajout d'une colonne qui passe en primary key directe |
|
| 393 | + else { |
|
| 394 | + $def = trim(substr($do, 3)); |
|
| 395 | + $colonne_ajoutee = substr($def, 0, strpos($def, ' ')); |
|
| 396 | + $def = substr($def, strlen($colonne_ajoutee) + 1); |
|
| 397 | + $opts = array(); |
|
| 398 | + if (preg_match(',primary\s+key,i', $def)) { |
|
| 399 | + $opts['key'] = array('PRIMARY KEY' => $colonne_ajoutee); |
|
| 400 | + $def = preg_replace(',primary\s+key,i', '', $def); |
|
| 401 | + } |
|
| 402 | + $opts['field'] = array($colonne_ajoutee => $def); |
|
| 403 | + if (!_sqlite_modifier_table($table, array($colonne_ajoutee), $opts, $serveur)) { |
|
| 404 | + spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 405 | + |
|
| 406 | + return false; |
|
| 407 | + } |
|
| 408 | + } |
|
| 409 | + break; |
|
| 410 | + } |
|
| 411 | + // tout est bon, ouf ! |
|
| 412 | + spip_log("SQLite ($serveur) : Changements OK : $debut $do", 'sqlite.' . _LOG_INFO); |
|
| 413 | + } |
|
| 414 | + |
|
| 415 | + spip_log("SQLite ($serveur) : fin ALTER TABLE OK !", 'sqlite.' . _LOG_INFO); |
|
| 416 | + |
|
| 417 | + return true; |
|
| 418 | 418 | } |
| 419 | 419 | |
| 420 | 420 | |
@@ -437,38 +437,38 @@ discard block |
||
| 437 | 437 | * - true si la requête réussie, false sinon. |
| 438 | 438 | */ |
| 439 | 439 | function spip_sqlite_create( |
| 440 | - $nom, |
|
| 441 | - $champs, |
|
| 442 | - $cles, |
|
| 443 | - $autoinc = false, |
|
| 444 | - $temporary = false, |
|
| 445 | - $serveur = '', |
|
| 446 | - $requeter = true |
|
| 440 | + $nom, |
|
| 441 | + $champs, |
|
| 442 | + $cles, |
|
| 443 | + $autoinc = false, |
|
| 444 | + $temporary = false, |
|
| 445 | + $serveur = '', |
|
| 446 | + $requeter = true |
|
| 447 | 447 | ) { |
| 448 | - $query = _sqlite_requete_create($nom, $champs, $cles, $autoinc, $temporary, $ifnotexists = true, $serveur, $requeter); |
|
| 449 | - if (!$query) { |
|
| 450 | - return false; |
|
| 451 | - } |
|
| 452 | - $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 453 | - |
|
| 454 | - // SQLite ne cree pas les KEY sur les requetes CREATE TABLE |
|
| 455 | - // il faut donc les faire creer ensuite |
|
| 456 | - if (!$requeter) { |
|
| 457 | - return $res; |
|
| 458 | - } |
|
| 459 | - |
|
| 460 | - $ok = $res ? true : false; |
|
| 461 | - if ($ok) { |
|
| 462 | - foreach ($cles as $k => $v) { |
|
| 463 | - if (preg_match(',^(KEY|UNIQUE)\s,i', $k, $m)) { |
|
| 464 | - $index = trim(substr($k, strlen($m[1]))); |
|
| 465 | - $unique = (strlen($m[1]) > 3); |
|
| 466 | - $ok &= spip_sqlite_create_index($index, $nom, $v, $unique, $serveur); |
|
| 467 | - } |
|
| 468 | - } |
|
| 469 | - } |
|
| 470 | - |
|
| 471 | - return $ok ? true : false; |
|
| 448 | + $query = _sqlite_requete_create($nom, $champs, $cles, $autoinc, $temporary, $ifnotexists = true, $serveur, $requeter); |
|
| 449 | + if (!$query) { |
|
| 450 | + return false; |
|
| 451 | + } |
|
| 452 | + $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 453 | + |
|
| 454 | + // SQLite ne cree pas les KEY sur les requetes CREATE TABLE |
|
| 455 | + // il faut donc les faire creer ensuite |
|
| 456 | + if (!$requeter) { |
|
| 457 | + return $res; |
|
| 458 | + } |
|
| 459 | + |
|
| 460 | + $ok = $res ? true : false; |
|
| 461 | + if ($ok) { |
|
| 462 | + foreach ($cles as $k => $v) { |
|
| 463 | + if (preg_match(',^(KEY|UNIQUE)\s,i', $k, $m)) { |
|
| 464 | + $index = trim(substr($k, strlen($m[1]))); |
|
| 465 | + $unique = (strlen($m[1]) > 3); |
|
| 466 | + $ok &= spip_sqlite_create_index($index, $nom, $v, $unique, $serveur); |
|
| 467 | + } |
|
| 468 | + } |
|
| 469 | + } |
|
| 470 | + |
|
| 471 | + return $ok ? true : false; |
|
| 472 | 472 | } |
| 473 | 473 | |
| 474 | 474 | /** |
@@ -481,21 +481,21 @@ discard block |
||
| 481 | 481 | * @return bool true si la base est créee. |
| 482 | 482 | **/ |
| 483 | 483 | function spip_sqlite_create_base($nom, $serveur = '', $option = true) { |
| 484 | - $f = $nom . '.sqlite'; |
|
| 485 | - if (strpos($nom, "/") === false) { |
|
| 486 | - $f = _DIR_DB . $f; |
|
| 487 | - } |
|
| 484 | + $f = $nom . '.sqlite'; |
|
| 485 | + if (strpos($nom, "/") === false) { |
|
| 486 | + $f = _DIR_DB . $f; |
|
| 487 | + } |
|
| 488 | 488 | |
| 489 | - $ok = new \PDO("sqlite:$f"); |
|
| 489 | + $ok = new \PDO("sqlite:$f"); |
|
| 490 | 490 | |
| 491 | - if ($ok) { |
|
| 492 | - unset($ok); |
|
| 491 | + if ($ok) { |
|
| 492 | + unset($ok); |
|
| 493 | 493 | |
| 494 | - return true; |
|
| 495 | - } |
|
| 496 | - unset($ok); |
|
| 494 | + return true; |
|
| 495 | + } |
|
| 496 | + unset($ok); |
|
| 497 | 497 | |
| 498 | - return false; |
|
| 498 | + return false; |
|
| 499 | 499 | } |
| 500 | 500 | |
| 501 | 501 | |
@@ -516,20 +516,20 @@ discard block |
||
| 516 | 516 | * - string texte de la requête si $requeter vaut false |
| 517 | 517 | */ |
| 518 | 518 | function spip_sqlite_create_view($nom, $query_select, $serveur = '', $requeter = true) { |
| 519 | - if (!$query_select) { |
|
| 520 | - return false; |
|
| 521 | - } |
|
| 522 | - // vue deja presente |
|
| 523 | - if (sql_showtable($nom, false, $serveur)) { |
|
| 524 | - spip_log("Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", |
|
| 525 | - 'sqlite.' . _LOG_ERREUR); |
|
| 519 | + if (!$query_select) { |
|
| 520 | + return false; |
|
| 521 | + } |
|
| 522 | + // vue deja presente |
|
| 523 | + if (sql_showtable($nom, false, $serveur)) { |
|
| 524 | + spip_log("Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", |
|
| 525 | + 'sqlite.' . _LOG_ERREUR); |
|
| 526 | 526 | |
| 527 | - return false; |
|
| 528 | - } |
|
| 527 | + return false; |
|
| 528 | + } |
|
| 529 | 529 | |
| 530 | - $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 530 | + $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 531 | 531 | |
| 532 | - return spip_sqlite_query($query, $serveur, $requeter); |
|
| 532 | + return spip_sqlite_query($query, $serveur, $requeter); |
|
| 533 | 533 | } |
| 534 | 534 | |
| 535 | 535 | /** |
@@ -551,52 +551,52 @@ discard block |
||
| 551 | 551 | * string : requête, false si erreur, true sinon. |
| 552 | 552 | */ |
| 553 | 553 | function spip_sqlite_create_index($nom, $table, $champs, $unique = '', $serveur = '', $requeter = true) { |
| 554 | - if (!($nom or $table or $champs)) { |
|
| 555 | - spip_log("Champ manquant pour creer un index sqlite ($nom, $table, (" . join(',', $champs) . "))", |
|
| 556 | - 'sqlite.' . _LOG_ERREUR); |
|
| 557 | - |
|
| 558 | - return false; |
|
| 559 | - } |
|
| 560 | - |
|
| 561 | - // SQLite ne differentie pas noms des index en fonction des tables |
|
| 562 | - // il faut donc creer des noms uniques d'index pour une base sqlite |
|
| 563 | - $nom = $table . '_' . $nom; |
|
| 564 | - // enlever d'eventuelles parentheses deja presentes sur champs |
|
| 565 | - if (!is_array($champs)) { |
|
| 566 | - if ($champs[0] == "(") { |
|
| 567 | - $champs = substr($champs, 1, -1); |
|
| 568 | - } |
|
| 569 | - $champs = array($champs); |
|
| 570 | - // supprimer l'info de longueur d'index mysql en fin de champ |
|
| 571 | - $champs = preg_replace(",\(\d+\)$,", "", $champs); |
|
| 572 | - } |
|
| 573 | - |
|
| 574 | - $ifnotexists = ""; |
|
| 575 | - $version = spip_sqlite_fetch(spip_sqlite_query("select sqlite_version() AS sqlite_version", $serveur), '', $serveur); |
|
| 576 | - if (!function_exists('spip_version_compare')) { |
|
| 577 | - include_spip('plugins/installer'); |
|
| 578 | - } |
|
| 579 | - |
|
| 580 | - if ($version and spip_version_compare($version['sqlite_version'], '3.3.0', '>=')) { |
|
| 581 | - $ifnotexists = ' IF NOT EXISTS'; |
|
| 582 | - } else { |
|
| 583 | - /* simuler le IF EXISTS - version 2 et sqlite < 3.3a */ |
|
| 584 | - $a = spip_sqlite_showtable($table, $serveur); |
|
| 585 | - if (isset($a['key']['KEY ' . $nom])) { |
|
| 586 | - return true; |
|
| 587 | - } |
|
| 588 | - } |
|
| 589 | - |
|
| 590 | - $query = "CREATE " . ($unique ? "UNIQUE " : "") . "INDEX$ifnotexists $nom ON $table (" . join(',', $champs) . ")"; |
|
| 591 | - $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 592 | - if (!$requeter) { |
|
| 593 | - return $res; |
|
| 594 | - } |
|
| 595 | - if ($res) { |
|
| 596 | - return true; |
|
| 597 | - } else { |
|
| 598 | - return false; |
|
| 599 | - } |
|
| 554 | + if (!($nom or $table or $champs)) { |
|
| 555 | + spip_log("Champ manquant pour creer un index sqlite ($nom, $table, (" . join(',', $champs) . "))", |
|
| 556 | + 'sqlite.' . _LOG_ERREUR); |
|
| 557 | + |
|
| 558 | + return false; |
|
| 559 | + } |
|
| 560 | + |
|
| 561 | + // SQLite ne differentie pas noms des index en fonction des tables |
|
| 562 | + // il faut donc creer des noms uniques d'index pour une base sqlite |
|
| 563 | + $nom = $table . '_' . $nom; |
|
| 564 | + // enlever d'eventuelles parentheses deja presentes sur champs |
|
| 565 | + if (!is_array($champs)) { |
|
| 566 | + if ($champs[0] == "(") { |
|
| 567 | + $champs = substr($champs, 1, -1); |
|
| 568 | + } |
|
| 569 | + $champs = array($champs); |
|
| 570 | + // supprimer l'info de longueur d'index mysql en fin de champ |
|
| 571 | + $champs = preg_replace(",\(\d+\)$,", "", $champs); |
|
| 572 | + } |
|
| 573 | + |
|
| 574 | + $ifnotexists = ""; |
|
| 575 | + $version = spip_sqlite_fetch(spip_sqlite_query("select sqlite_version() AS sqlite_version", $serveur), '', $serveur); |
|
| 576 | + if (!function_exists('spip_version_compare')) { |
|
| 577 | + include_spip('plugins/installer'); |
|
| 578 | + } |
|
| 579 | + |
|
| 580 | + if ($version and spip_version_compare($version['sqlite_version'], '3.3.0', '>=')) { |
|
| 581 | + $ifnotexists = ' IF NOT EXISTS'; |
|
| 582 | + } else { |
|
| 583 | + /* simuler le IF EXISTS - version 2 et sqlite < 3.3a */ |
|
| 584 | + $a = spip_sqlite_showtable($table, $serveur); |
|
| 585 | + if (isset($a['key']['KEY ' . $nom])) { |
|
| 586 | + return true; |
|
| 587 | + } |
|
| 588 | + } |
|
| 589 | + |
|
| 590 | + $query = "CREATE " . ($unique ? "UNIQUE " : "") . "INDEX$ifnotexists $nom ON $table (" . join(',', $champs) . ")"; |
|
| 591 | + $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 592 | + if (!$requeter) { |
|
| 593 | + return $res; |
|
| 594 | + } |
|
| 595 | + if ($res) { |
|
| 596 | + return true; |
|
| 597 | + } else { |
|
| 598 | + return false; |
|
| 599 | + } |
|
| 600 | 600 | } |
| 601 | 601 | |
| 602 | 602 | /** |
@@ -613,31 +613,31 @@ discard block |
||
| 613 | 613 | * @return int Nombre de lignes |
| 614 | 614 | */ |
| 615 | 615 | function spip_sqlite_count($r, $serveur = '', $requeter = true) { |
| 616 | - if (!$r) { |
|
| 617 | - return 0; |
|
| 618 | - } |
|
| 619 | - |
|
| 620 | - // select ou autre (insert, update,...) ? |
|
| 621 | - // (link,requete) a compter |
|
| 622 | - if (is_array($r->spipSqliteRowCount)) { |
|
| 623 | - list($link, $query) = $r->spipSqliteRowCount; |
|
| 624 | - // amelioration possible a tester intensivement : pas de order by pour compter ! |
|
| 625 | - // $query = preg_replace(",ORDER BY .+(LIMIT\s|HAVING\s|GROUP BY\s|$),Uims","\\1",$query); |
|
| 626 | - $query = "SELECT count(*) as zzzzsqlitecount FROM ($query)"; |
|
| 627 | - $l = $link->query($query); |
|
| 628 | - $i = 0; |
|
| 629 | - if ($l and $z = $l->fetch()) { |
|
| 630 | - $i = $z['zzzzsqlitecount']; |
|
| 631 | - } |
|
| 632 | - $r->spipSqliteRowCount = $i; |
|
| 633 | - } |
|
| 634 | - if (isset($r->spipSqliteRowCount)) { |
|
| 635 | - // Ce compte est faux s'il y a des limit dans la requete :( |
|
| 636 | - // il retourne le nombre d'enregistrements sans le limit |
|
| 637 | - return $r->spipSqliteRowCount; |
|
| 638 | - } else { |
|
| 639 | - return $r->rowCount(); |
|
| 640 | - } |
|
| 616 | + if (!$r) { |
|
| 617 | + return 0; |
|
| 618 | + } |
|
| 619 | + |
|
| 620 | + // select ou autre (insert, update,...) ? |
|
| 621 | + // (link,requete) a compter |
|
| 622 | + if (is_array($r->spipSqliteRowCount)) { |
|
| 623 | + list($link, $query) = $r->spipSqliteRowCount; |
|
| 624 | + // amelioration possible a tester intensivement : pas de order by pour compter ! |
|
| 625 | + // $query = preg_replace(",ORDER BY .+(LIMIT\s|HAVING\s|GROUP BY\s|$),Uims","\\1",$query); |
|
| 626 | + $query = "SELECT count(*) as zzzzsqlitecount FROM ($query)"; |
|
| 627 | + $l = $link->query($query); |
|
| 628 | + $i = 0; |
|
| 629 | + if ($l and $z = $l->fetch()) { |
|
| 630 | + $i = $z['zzzzsqlitecount']; |
|
| 631 | + } |
|
| 632 | + $r->spipSqliteRowCount = $i; |
|
| 633 | + } |
|
| 634 | + if (isset($r->spipSqliteRowCount)) { |
|
| 635 | + // Ce compte est faux s'il y a des limit dans la requete :( |
|
| 636 | + // il retourne le nombre d'enregistrements sans le limit |
|
| 637 | + return $r->spipSqliteRowCount; |
|
| 638 | + } else { |
|
| 639 | + return $r->rowCount(); |
|
| 640 | + } |
|
| 641 | 641 | } |
| 642 | 642 | |
| 643 | 643 | |
@@ -656,21 +656,21 @@ discard block |
||
| 656 | 656 | * - false si la requête a échouée |
| 657 | 657 | **/ |
| 658 | 658 | function spip_sqlite_countsel( |
| 659 | - $from = array(), |
|
| 660 | - $where = array(), |
|
| 661 | - $groupby = '', |
|
| 662 | - $having = array(), |
|
| 663 | - $serveur = '', |
|
| 664 | - $requeter = true |
|
| 659 | + $from = array(), |
|
| 660 | + $where = array(), |
|
| 661 | + $groupby = '', |
|
| 662 | + $having = array(), |
|
| 663 | + $serveur = '', |
|
| 664 | + $requeter = true |
|
| 665 | 665 | ) { |
| 666 | - $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 667 | - $r = spip_sqlite_select("COUNT($c)", $from, $where, '', '', '', |
|
| 668 | - $having, $serveur, $requeter); |
|
| 669 | - if ((is_resource($r) or is_object($r)) && $requeter) { // ressource : sqlite2, object : sqlite3 |
|
| 670 | - list($r) = spip_sqlite_fetch($r, SPIP_SQLITE3_NUM, $serveur); |
|
| 671 | - } |
|
| 666 | + $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 667 | + $r = spip_sqlite_select("COUNT($c)", $from, $where, '', '', '', |
|
| 668 | + $having, $serveur, $requeter); |
|
| 669 | + if ((is_resource($r) or is_object($r)) && $requeter) { // ressource : sqlite2, object : sqlite3 |
|
| 670 | + list($r) = spip_sqlite_fetch($r, SPIP_SQLITE3_NUM, $serveur); |
|
| 671 | + } |
|
| 672 | 672 | |
| 673 | - return $r; |
|
| 673 | + return $r; |
|
| 674 | 674 | } |
| 675 | 675 | |
| 676 | 676 | |
@@ -687,22 +687,22 @@ discard block |
||
| 687 | 687 | * - False en cas d'erreur. |
| 688 | 688 | **/ |
| 689 | 689 | function spip_sqlite_delete($table, $where = '', $serveur = '', $requeter = true) { |
| 690 | - $res = spip_sqlite_query( |
|
| 691 | - _sqlite_calculer_expression('DELETE FROM', $table, ',') |
|
| 692 | - . _sqlite_calculer_expression('WHERE', $where), |
|
| 693 | - $serveur, $requeter); |
|
| 690 | + $res = spip_sqlite_query( |
|
| 691 | + _sqlite_calculer_expression('DELETE FROM', $table, ',') |
|
| 692 | + . _sqlite_calculer_expression('WHERE', $where), |
|
| 693 | + $serveur, $requeter); |
|
| 694 | 694 | |
| 695 | - // renvoyer la requete inerte si demandee |
|
| 696 | - if (!$requeter) { |
|
| 697 | - return $res; |
|
| 698 | - } |
|
| 695 | + // renvoyer la requete inerte si demandee |
|
| 696 | + if (!$requeter) { |
|
| 697 | + return $res; |
|
| 698 | + } |
|
| 699 | 699 | |
| 700 | - if ($res) { |
|
| 701 | - $link = _sqlite_link($serveur); |
|
| 702 | - return $res->rowCount(); |
|
| 703 | - } else { |
|
| 704 | - return false; |
|
| 705 | - } |
|
| 700 | + if ($res) { |
|
| 701 | + $link = _sqlite_link($serveur); |
|
| 702 | + return $res->rowCount(); |
|
| 703 | + } else { |
|
| 704 | + return false; |
|
| 705 | + } |
|
| 706 | 706 | } |
| 707 | 707 | |
| 708 | 708 | |
@@ -718,15 +718,15 @@ discard block |
||
| 718 | 718 | * - true si la requête a réussie, false sinon |
| 719 | 719 | */ |
| 720 | 720 | function spip_sqlite_drop_table($table, $exist = '', $serveur = '', $requeter = true) { |
| 721 | - if ($exist) { |
|
| 722 | - $exist = " IF EXISTS"; |
|
| 723 | - } |
|
| 721 | + if ($exist) { |
|
| 722 | + $exist = " IF EXISTS"; |
|
| 723 | + } |
|
| 724 | 724 | |
| 725 | - if (spip_sqlite_query("DROP TABLE$exist $table", $serveur, $requeter)) { |
|
| 726 | - return true; |
|
| 727 | - } else { |
|
| 728 | - return false; |
|
| 729 | - } |
|
| 725 | + if (spip_sqlite_query("DROP TABLE$exist $table", $serveur, $requeter)) { |
|
| 726 | + return true; |
|
| 727 | + } else { |
|
| 728 | + return false; |
|
| 729 | + } |
|
| 730 | 730 | } |
| 731 | 731 | |
| 732 | 732 | |
@@ -742,11 +742,11 @@ discard block |
||
| 742 | 742 | * - true si la requête a réussie, false sinon |
| 743 | 743 | */ |
| 744 | 744 | function spip_sqlite_drop_view($view, $exist = '', $serveur = '', $requeter = true) { |
| 745 | - if ($exist) { |
|
| 746 | - $exist = " IF EXISTS"; |
|
| 747 | - } |
|
| 745 | + if ($exist) { |
|
| 746 | + $exist = " IF EXISTS"; |
|
| 747 | + } |
|
| 748 | 748 | |
| 749 | - return spip_sqlite_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 749 | + return spip_sqlite_query("DROP VIEW$exist $view", $serveur, $requeter); |
|
| 750 | 750 | } |
| 751 | 751 | |
| 752 | 752 | /** |
@@ -760,20 +760,20 @@ discard block |
||
| 760 | 760 | * @return bool ou requete |
| 761 | 761 | */ |
| 762 | 762 | function spip_sqlite_drop_index($nom, $table, $serveur = '', $requeter = true) { |
| 763 | - if (!($nom or $table)) { |
|
| 764 | - spip_log("Champ manquant pour supprimer un index sqlite ($nom, $table)", 'sqlite.' . _LOG_ERREUR); |
|
| 763 | + if (!($nom or $table)) { |
|
| 764 | + spip_log("Champ manquant pour supprimer un index sqlite ($nom, $table)", 'sqlite.' . _LOG_ERREUR); |
|
| 765 | 765 | |
| 766 | - return false; |
|
| 767 | - } |
|
| 766 | + return false; |
|
| 767 | + } |
|
| 768 | 768 | |
| 769 | - // SQLite ne differentie pas noms des index en fonction des tables |
|
| 770 | - // il faut donc creer des noms uniques d'index pour une base sqlite |
|
| 771 | - $index = $table . '_' . $nom; |
|
| 772 | - $exist = " IF EXISTS"; |
|
| 769 | + // SQLite ne differentie pas noms des index en fonction des tables |
|
| 770 | + // il faut donc creer des noms uniques d'index pour une base sqlite |
|
| 771 | + $index = $table . '_' . $nom; |
|
| 772 | + $exist = " IF EXISTS"; |
|
| 773 | 773 | |
| 774 | - $query = "DROP INDEX$exist $index"; |
|
| 774 | + $query = "DROP INDEX$exist $index"; |
|
| 775 | 775 | |
| 776 | - return spip_sqlite_query($query, $serveur, $requeter); |
|
| 776 | + return spip_sqlite_query($query, $serveur, $requeter); |
|
| 777 | 777 | } |
| 778 | 778 | |
| 779 | 779 | /** |
@@ -789,28 +789,28 @@ discard block |
||
| 789 | 789 | * Erreur eventuelle |
| 790 | 790 | **/ |
| 791 | 791 | function spip_sqlite_error($query = '', $serveur = '') { |
| 792 | - $link = _sqlite_link($serveur); |
|
| 792 | + $link = _sqlite_link($serveur); |
|
| 793 | 793 | |
| 794 | - if ($link) { |
|
| 795 | - $errs = $link->errorInfo(); |
|
| 796 | - $s = _sqlite_last_error_from_link($link); |
|
| 797 | - } else { |
|
| 798 | - $s = ": aucune ressource sqlite (link)"; |
|
| 799 | - } |
|
| 800 | - if ($s) { |
|
| 801 | - $trace = debug_backtrace(); |
|
| 802 | - if ($trace[0]['function'] != "spip_sqlite_error") { |
|
| 803 | - spip_log("$s - $query - " . sql_error_backtrace(), 'sqlite.' . _LOG_ERREUR); |
|
| 804 | - } |
|
| 805 | - } |
|
| 794 | + if ($link) { |
|
| 795 | + $errs = $link->errorInfo(); |
|
| 796 | + $s = _sqlite_last_error_from_link($link); |
|
| 797 | + } else { |
|
| 798 | + $s = ": aucune ressource sqlite (link)"; |
|
| 799 | + } |
|
| 800 | + if ($s) { |
|
| 801 | + $trace = debug_backtrace(); |
|
| 802 | + if ($trace[0]['function'] != "spip_sqlite_error") { |
|
| 803 | + spip_log("$s - $query - " . sql_error_backtrace(), 'sqlite.' . _LOG_ERREUR); |
|
| 804 | + } |
|
| 805 | + } |
|
| 806 | 806 | |
| 807 | - return $s; |
|
| 807 | + return $s; |
|
| 808 | 808 | } |
| 809 | 809 | |
| 810 | 810 | function _sqlite_last_error_from_link($link) { |
| 811 | - if ($link) { |
|
| 812 | - $errs = $link->errorInfo(); |
|
| 813 | - /* |
|
| 811 | + if ($link) { |
|
| 812 | + $errs = $link->errorInfo(); |
|
| 813 | + /* |
|
| 814 | 814 | $errs[0] |
| 815 | 815 | numero SQLState ('HY000' souvent lors d'une erreur) |
| 816 | 816 | http://www.easysoft.com/developer/interfaces/odbc/sqlstate_status_return_codes.html |
@@ -820,11 +820,11 @@ discard block |
||
| 820 | 820 | $errs[2] |
| 821 | 821 | Le texte du message d'erreur |
| 822 | 822 | */ |
| 823 | - if (ltrim($errs[0], '0')) { // 00000 si pas d'erreur |
|
| 824 | - return "$errs[2]"; |
|
| 825 | - } |
|
| 826 | - } |
|
| 827 | - return ""; |
|
| 823 | + if (ltrim($errs[0], '0')) { // 00000 si pas d'erreur |
|
| 824 | + return "$errs[2]"; |
|
| 825 | + } |
|
| 826 | + } |
|
| 827 | + return ""; |
|
| 828 | 828 | } |
| 829 | 829 | |
| 830 | 830 | /** |
@@ -841,23 +841,23 @@ discard block |
||
| 841 | 841 | * 'HY000/1' : numéro de l'erreur SQLState / numéro d'erreur interne SQLite (en sqlite 3) |
| 842 | 842 | **/ |
| 843 | 843 | function spip_sqlite_errno($serveur = '') { |
| 844 | - $link = _sqlite_link($serveur); |
|
| 844 | + $link = _sqlite_link($serveur); |
|
| 845 | 845 | |
| 846 | - if ($link) { |
|
| 847 | - $t = $link->errorInfo(); |
|
| 848 | - $s = ltrim($t[0], '0'); // 00000 si pas d'erreur |
|
| 849 | - if ($s) { |
|
| 850 | - $s .= ' / ' . $t[1]; |
|
| 851 | - } // ajoute l'erreur du moteur SQLite |
|
| 852 | - } else { |
|
| 853 | - $s = ": aucune ressource sqlite (link)"; |
|
| 854 | - } |
|
| 846 | + if ($link) { |
|
| 847 | + $t = $link->errorInfo(); |
|
| 848 | + $s = ltrim($t[0], '0'); // 00000 si pas d'erreur |
|
| 849 | + if ($s) { |
|
| 850 | + $s .= ' / ' . $t[1]; |
|
| 851 | + } // ajoute l'erreur du moteur SQLite |
|
| 852 | + } else { |
|
| 853 | + $s = ": aucune ressource sqlite (link)"; |
|
| 854 | + } |
|
| 855 | 855 | |
| 856 | - if ($s) { |
|
| 857 | - spip_log("Erreur sqlite $s", 'sqlite.' . _LOG_ERREUR); |
|
| 858 | - } |
|
| 856 | + if ($s) { |
|
| 857 | + spip_log("Erreur sqlite $s", 'sqlite.' . _LOG_ERREUR); |
|
| 858 | + } |
|
| 859 | 859 | |
| 860 | - return $s ? $s : 0; |
|
| 860 | + return $s ? $s : 0; |
|
| 861 | 861 | } |
| 862 | 862 | |
| 863 | 863 | |
@@ -873,19 +873,19 @@ discard block |
||
| 873 | 873 | * - false si on a pas pu avoir d'explication |
| 874 | 874 | */ |
| 875 | 875 | function spip_sqlite_explain($query, $serveur = '', $requeter = true) { |
| 876 | - if (strpos(ltrim($query), 'SELECT') !== 0) { |
|
| 877 | - return array(); |
|
| 878 | - } |
|
| 876 | + if (strpos(ltrim($query), 'SELECT') !== 0) { |
|
| 877 | + return array(); |
|
| 878 | + } |
|
| 879 | 879 | |
| 880 | - $query = spip_sqlite::traduire_requete($query, $serveur); |
|
| 881 | - $query = 'EXPLAIN ' . $query; |
|
| 882 | - if (!$requeter) { |
|
| 883 | - return $query; |
|
| 884 | - } |
|
| 885 | - // on ne trace pas ces requetes, sinon on obtient un tracage sans fin... |
|
| 886 | - $r = spip_sqlite::executer_requete($query, $serveur, false); |
|
| 880 | + $query = spip_sqlite::traduire_requete($query, $serveur); |
|
| 881 | + $query = 'EXPLAIN ' . $query; |
|
| 882 | + if (!$requeter) { |
|
| 883 | + return $query; |
|
| 884 | + } |
|
| 885 | + // on ne trace pas ces requetes, sinon on obtient un tracage sans fin... |
|
| 886 | + $r = spip_sqlite::executer_requete($query, $serveur, false); |
|
| 887 | 887 | |
| 888 | - return $r ? spip_sqlite_fetch($r, null, $serveur) : false; // hum ? etrange ca... a verifier |
|
| 888 | + return $r ? spip_sqlite_fetch($r, null, $serveur) : false; // hum ? etrange ca... a verifier |
|
| 889 | 889 | } |
| 890 | 890 | |
| 891 | 891 | |
@@ -902,29 +902,29 @@ discard block |
||
| 902 | 902 | */ |
| 903 | 903 | function spip_sqlite_fetch($r, $t = '', $serveur = '', $requeter = true) { |
| 904 | 904 | |
| 905 | - $link = _sqlite_link($serveur); |
|
| 906 | - $t = $t ? $t : SPIP_SQLITE3_ASSOC; |
|
| 905 | + $link = _sqlite_link($serveur); |
|
| 906 | + $t = $t ? $t : SPIP_SQLITE3_ASSOC; |
|
| 907 | 907 | |
| 908 | - $retour = false; |
|
| 909 | - if ($r) { |
|
| 910 | - $retour = $r->fetch($t); |
|
| 911 | - } |
|
| 908 | + $retour = false; |
|
| 909 | + if ($r) { |
|
| 910 | + $retour = $r->fetch($t); |
|
| 911 | + } |
|
| 912 | 912 | |
| 913 | - // Renvoie des 'table.titre' au lieu de 'titre' tout court ! pff ! |
|
| 914 | - // suppression de 'table.' pour toutes les cles (c'est un peu violent !) |
|
| 915 | - // c'est couteux : on ne verifie que la premiere ligne pour voir si on le fait ou non |
|
| 916 | - if ($retour |
|
| 917 | - and strpos(implode('', array_keys($retour)), '.') !== false |
|
| 918 | - ) { |
|
| 919 | - foreach ($retour as $cle => $val) { |
|
| 920 | - if (($pos = strpos($cle, '.')) !== false) { |
|
| 921 | - $retour[substr($cle, $pos + 1)] = &$retour[$cle]; |
|
| 922 | - unset($retour[$cle]); |
|
| 923 | - } |
|
| 924 | - } |
|
| 925 | - } |
|
| 913 | + // Renvoie des 'table.titre' au lieu de 'titre' tout court ! pff ! |
|
| 914 | + // suppression de 'table.' pour toutes les cles (c'est un peu violent !) |
|
| 915 | + // c'est couteux : on ne verifie que la premiere ligne pour voir si on le fait ou non |
|
| 916 | + if ($retour |
|
| 917 | + and strpos(implode('', array_keys($retour)), '.') !== false |
|
| 918 | + ) { |
|
| 919 | + foreach ($retour as $cle => $val) { |
|
| 920 | + if (($pos = strpos($cle, '.')) !== false) { |
|
| 921 | + $retour[substr($cle, $pos + 1)] = &$retour[$cle]; |
|
| 922 | + unset($retour[$cle]); |
|
| 923 | + } |
|
| 924 | + } |
|
| 925 | + } |
|
| 926 | 926 | |
| 927 | - return $retour; |
|
| 927 | + return $retour; |
|
| 928 | 928 | } |
| 929 | 929 | |
| 930 | 930 | /** |
@@ -937,8 +937,8 @@ discard block |
||
| 937 | 937 | * @return bool True si déplacement réussi, false sinon. |
| 938 | 938 | **/ |
| 939 | 939 | function spip_sqlite_seek($r, $row_number, $serveur = '', $requeter = true) { |
| 940 | - // encore un truc de bien fichu : PDO ne PEUT PAS faire de seek ou de rewind... |
|
| 941 | - return false; |
|
| 940 | + // encore un truc de bien fichu : PDO ne PEUT PAS faire de seek ou de rewind... |
|
| 941 | + return false; |
|
| 942 | 942 | } |
| 943 | 943 | |
| 944 | 944 | |
@@ -954,10 +954,10 @@ discard block |
||
| 954 | 954 | * @return bool True si réussi |
| 955 | 955 | */ |
| 956 | 956 | function spip_sqlite_free(&$r, $serveur = '', $requeter = true) { |
| 957 | - unset($r); |
|
| 957 | + unset($r); |
|
| 958 | 958 | |
| 959 | - return true; |
|
| 960 | - //return sqlite_free_result($r); |
|
| 959 | + return true; |
|
| 960 | + //return sqlite_free_result($r); |
|
| 961 | 961 | } |
| 962 | 962 | |
| 963 | 963 | |
@@ -972,8 +972,8 @@ discard block |
||
| 972 | 972 | * @return void |
| 973 | 973 | */ |
| 974 | 974 | function spip_sqlite_get_charset($charset = array(), $serveur = '', $requeter = true) { |
| 975 | - //$c = !$charset ? '' : (" LIKE "._q($charset['charset'])); |
|
| 976 | - //return spip_sqlite_fetch(sqlite_query(_sqlite_link($serveur), "SHOW CHARACTER SET$c"), NULL, $serveur); |
|
| 975 | + //$c = !$charset ? '' : (" LIKE "._q($charset['charset'])); |
|
| 976 | + //return spip_sqlite_fetch(sqlite_query(_sqlite_link($serveur), "SHOW CHARACTER SET$c"), NULL, $serveur); |
|
| 977 | 977 | } |
| 978 | 978 | |
| 979 | 979 | |
@@ -988,7 +988,7 @@ discard block |
||
| 988 | 988 | * Valeur hexadécimale pour SQLite |
| 989 | 989 | **/ |
| 990 | 990 | function spip_sqlite_hex($v) { |
| 991 | - return hexdec($v); |
|
| 991 | + return hexdec($v); |
|
| 992 | 992 | } |
| 993 | 993 | |
| 994 | 994 | |
@@ -1010,7 +1010,7 @@ discard block |
||
| 1010 | 1010 | * Expression de requête SQL |
| 1011 | 1011 | **/ |
| 1012 | 1012 | function spip_sqlite_in($val, $valeurs, $not = '', $serveur = '', $requeter = true) { |
| 1013 | - return "($val $not IN ($valeurs))"; |
|
| 1013 | + return "($val $not IN ($valeurs))"; |
|
| 1014 | 1014 | } |
| 1015 | 1015 | |
| 1016 | 1016 | |
@@ -1038,20 +1038,20 @@ discard block |
||
| 1038 | 1038 | **/ |
| 1039 | 1039 | function spip_sqlite_insert($table, $champs, $valeurs, $desc = array(), $serveur = '', $requeter = true) { |
| 1040 | 1040 | |
| 1041 | - $query = "INSERT INTO $table " . ($champs ? "$champs VALUES $valeurs" : "DEFAULT VALUES"); |
|
| 1042 | - if ($r = spip_sqlite_query($query, $serveur, $requeter)) { |
|
| 1043 | - if (!$requeter) { |
|
| 1044 | - return $r; |
|
| 1045 | - } |
|
| 1046 | - $nb = spip_sqlite::last_insert_id($serveur); |
|
| 1047 | - } else { |
|
| 1048 | - $nb = false; |
|
| 1049 | - } |
|
| 1041 | + $query = "INSERT INTO $table " . ($champs ? "$champs VALUES $valeurs" : "DEFAULT VALUES"); |
|
| 1042 | + if ($r = spip_sqlite_query($query, $serveur, $requeter)) { |
|
| 1043 | + if (!$requeter) { |
|
| 1044 | + return $r; |
|
| 1045 | + } |
|
| 1046 | + $nb = spip_sqlite::last_insert_id($serveur); |
|
| 1047 | + } else { |
|
| 1048 | + $nb = false; |
|
| 1049 | + } |
|
| 1050 | 1050 | |
| 1051 | - $err = spip_sqlite_error($query, $serveur); |
|
| 1051 | + $err = spip_sqlite_error($query, $serveur); |
|
| 1052 | 1052 | |
| 1053 | - // cas particulier : ne pas substituer la reponse spip_sqlite_query si on est en profilage |
|
| 1054 | - return isset($_GET['var_profile']) ? $r : $nb; |
|
| 1053 | + // cas particulier : ne pas substituer la reponse spip_sqlite_query si on est en profilage |
|
| 1054 | + return isset($_GET['var_profile']) ? $r : $nb; |
|
| 1055 | 1055 | |
| 1056 | 1056 | } |
| 1057 | 1057 | |
@@ -1077,28 +1077,28 @@ discard block |
||
| 1077 | 1077 | * - Tableau de description de la requête et du temps d'exécution, si var_profile activé |
| 1078 | 1078 | **/ |
| 1079 | 1079 | function spip_sqlite_insertq($table, $couples = array(), $desc = array(), $serveur = '', $requeter = true) { |
| 1080 | - if (!$desc) { |
|
| 1081 | - $desc = description_table($table, $serveur); |
|
| 1082 | - } |
|
| 1083 | - if (!$desc) { |
|
| 1084 | - die("$table insertion sans description"); |
|
| 1085 | - } |
|
| 1086 | - $fields = isset($desc['field']) ? $desc['field'] : array(); |
|
| 1080 | + if (!$desc) { |
|
| 1081 | + $desc = description_table($table, $serveur); |
|
| 1082 | + } |
|
| 1083 | + if (!$desc) { |
|
| 1084 | + die("$table insertion sans description"); |
|
| 1085 | + } |
|
| 1086 | + $fields = isset($desc['field']) ? $desc['field'] : array(); |
|
| 1087 | 1087 | |
| 1088 | - foreach ($couples as $champ => $val) { |
|
| 1089 | - $couples[$champ] = _sqlite_calculer_cite($val, $fields[$champ]); |
|
| 1090 | - } |
|
| 1088 | + foreach ($couples as $champ => $val) { |
|
| 1089 | + $couples[$champ] = _sqlite_calculer_cite($val, $fields[$champ]); |
|
| 1090 | + } |
|
| 1091 | 1091 | |
| 1092 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1093 | - $couples = _sqlite_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1092 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1093 | + $couples = _sqlite_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1094 | 1094 | |
| 1095 | - $cles = $valeurs = ""; |
|
| 1096 | - if (count($couples)) { |
|
| 1097 | - $cles = "(" . join(',', array_keys($couples)) . ")"; |
|
| 1098 | - $valeurs = "(" . join(',', $couples) . ")"; |
|
| 1099 | - } |
|
| 1095 | + $cles = $valeurs = ""; |
|
| 1096 | + if (count($couples)) { |
|
| 1097 | + $cles = "(" . join(',', array_keys($couples)) . ")"; |
|
| 1098 | + $valeurs = "(" . join(',', $couples) . ")"; |
|
| 1099 | + } |
|
| 1100 | 1100 | |
| 1101 | - return spip_sqlite_insert($table, $cles, $valeurs, $desc, $serveur, $requeter); |
|
| 1101 | + return spip_sqlite_insert($table, $cles, $valeurs, $desc, $serveur, $requeter); |
|
| 1102 | 1102 | } |
| 1103 | 1103 | |
| 1104 | 1104 | |
@@ -1122,70 +1122,70 @@ discard block |
||
| 1122 | 1122 | * - False en cas d'erreur. |
| 1123 | 1123 | **/ |
| 1124 | 1124 | function spip_sqlite_insertq_multi($table, $tab_couples = array(), $desc = array(), $serveur = '', $requeter = true) { |
| 1125 | - if (!$desc) { |
|
| 1126 | - $desc = description_table($table, $serveur); |
|
| 1127 | - } |
|
| 1128 | - if (!$desc) { |
|
| 1129 | - die("$table insertion sans description"); |
|
| 1130 | - } |
|
| 1131 | - if (!isset($desc['field'])) { |
|
| 1132 | - $desc['field'] = array(); |
|
| 1133 | - } |
|
| 1134 | - |
|
| 1135 | - // recuperer les champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1136 | - $maj = _sqlite_ajouter_champs_timestamp($table, array(), $desc, $serveur); |
|
| 1137 | - |
|
| 1138 | - // seul le nom de la table est a traduire ici : |
|
| 1139 | - // le faire une seule fois au debut |
|
| 1140 | - $query_start = "INSERT INTO $table "; |
|
| 1141 | - $query_start = spip_sqlite::traduire_requete($query_start, $serveur); |
|
| 1142 | - |
|
| 1143 | - // ouvrir une transaction |
|
| 1144 | - if ($requeter) { |
|
| 1145 | - spip_sqlite::demarrer_transaction($serveur); |
|
| 1146 | - } |
|
| 1147 | - |
|
| 1148 | - while ($couples = array_shift($tab_couples)) { |
|
| 1149 | - foreach ($couples as $champ => $val) { |
|
| 1150 | - $couples[$champ] = _sqlite_calculer_cite($val, $desc['field'][$champ]); |
|
| 1151 | - } |
|
| 1152 | - |
|
| 1153 | - // inserer les champs timestamp par defaut |
|
| 1154 | - $couples = array_merge($maj, $couples); |
|
| 1155 | - |
|
| 1156 | - $champs = $valeurs = ""; |
|
| 1157 | - if (count($couples)) { |
|
| 1158 | - $champs = "(" . join(',', array_keys($couples)) . ")"; |
|
| 1159 | - $valeurs = "(" . join(',', $couples) . ")"; |
|
| 1160 | - $query = $query_start . "$champs VALUES $valeurs"; |
|
| 1161 | - } else { |
|
| 1162 | - $query = $query_start . "DEFAULT VALUES"; |
|
| 1163 | - } |
|
| 1164 | - |
|
| 1165 | - if ($requeter) { |
|
| 1166 | - $retour = spip_sqlite::executer_requete($query, $serveur); |
|
| 1167 | - } |
|
| 1168 | - |
|
| 1169 | - // sur le dernier couple uniquement |
|
| 1170 | - if (!count($tab_couples)) { |
|
| 1171 | - $nb = 0; |
|
| 1172 | - if ($requeter) { |
|
| 1173 | - $nb = spip_sqlite::last_insert_id($serveur); |
|
| 1174 | - } else { |
|
| 1175 | - return $query; |
|
| 1176 | - } |
|
| 1177 | - } |
|
| 1178 | - |
|
| 1179 | - $err = spip_sqlite_error($query, $serveur); |
|
| 1180 | - } |
|
| 1181 | - |
|
| 1182 | - if ($requeter) { |
|
| 1183 | - spip_sqlite::finir_transaction($serveur); |
|
| 1184 | - } |
|
| 1185 | - |
|
| 1186 | - // renvoie le dernier id d'autoincrement ajoute |
|
| 1187 | - // cas particulier : ne pas substituer la reponse spip_sqlite_query si on est en profilage |
|
| 1188 | - return isset($_GET['var_profile']) ? $retour : $nb; |
|
| 1125 | + if (!$desc) { |
|
| 1126 | + $desc = description_table($table, $serveur); |
|
| 1127 | + } |
|
| 1128 | + if (!$desc) { |
|
| 1129 | + die("$table insertion sans description"); |
|
| 1130 | + } |
|
| 1131 | + if (!isset($desc['field'])) { |
|
| 1132 | + $desc['field'] = array(); |
|
| 1133 | + } |
|
| 1134 | + |
|
| 1135 | + // recuperer les champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1136 | + $maj = _sqlite_ajouter_champs_timestamp($table, array(), $desc, $serveur); |
|
| 1137 | + |
|
| 1138 | + // seul le nom de la table est a traduire ici : |
|
| 1139 | + // le faire une seule fois au debut |
|
| 1140 | + $query_start = "INSERT INTO $table "; |
|
| 1141 | + $query_start = spip_sqlite::traduire_requete($query_start, $serveur); |
|
| 1142 | + |
|
| 1143 | + // ouvrir une transaction |
|
| 1144 | + if ($requeter) { |
|
| 1145 | + spip_sqlite::demarrer_transaction($serveur); |
|
| 1146 | + } |
|
| 1147 | + |
|
| 1148 | + while ($couples = array_shift($tab_couples)) { |
|
| 1149 | + foreach ($couples as $champ => $val) { |
|
| 1150 | + $couples[$champ] = _sqlite_calculer_cite($val, $desc['field'][$champ]); |
|
| 1151 | + } |
|
| 1152 | + |
|
| 1153 | + // inserer les champs timestamp par defaut |
|
| 1154 | + $couples = array_merge($maj, $couples); |
|
| 1155 | + |
|
| 1156 | + $champs = $valeurs = ""; |
|
| 1157 | + if (count($couples)) { |
|
| 1158 | + $champs = "(" . join(',', array_keys($couples)) . ")"; |
|
| 1159 | + $valeurs = "(" . join(',', $couples) . ")"; |
|
| 1160 | + $query = $query_start . "$champs VALUES $valeurs"; |
|
| 1161 | + } else { |
|
| 1162 | + $query = $query_start . "DEFAULT VALUES"; |
|
| 1163 | + } |
|
| 1164 | + |
|
| 1165 | + if ($requeter) { |
|
| 1166 | + $retour = spip_sqlite::executer_requete($query, $serveur); |
|
| 1167 | + } |
|
| 1168 | + |
|
| 1169 | + // sur le dernier couple uniquement |
|
| 1170 | + if (!count($tab_couples)) { |
|
| 1171 | + $nb = 0; |
|
| 1172 | + if ($requeter) { |
|
| 1173 | + $nb = spip_sqlite::last_insert_id($serveur); |
|
| 1174 | + } else { |
|
| 1175 | + return $query; |
|
| 1176 | + } |
|
| 1177 | + } |
|
| 1178 | + |
|
| 1179 | + $err = spip_sqlite_error($query, $serveur); |
|
| 1180 | + } |
|
| 1181 | + |
|
| 1182 | + if ($requeter) { |
|
| 1183 | + spip_sqlite::finir_transaction($serveur); |
|
| 1184 | + } |
|
| 1185 | + |
|
| 1186 | + // renvoie le dernier id d'autoincrement ajoute |
|
| 1187 | + // cas particulier : ne pas substituer la reponse spip_sqlite_query si on est en profilage |
|
| 1188 | + return isset($_GET['var_profile']) ? $retour : $nb; |
|
| 1189 | 1189 | } |
| 1190 | 1190 | |
| 1191 | 1191 | |
@@ -1200,7 +1200,7 @@ discard block |
||
| 1200 | 1200 | * Toujours true. |
| 1201 | 1201 | **/ |
| 1202 | 1202 | function spip_sqlite_preferer_transaction($serveur = '', $requeter = true) { |
| 1203 | - return true; |
|
| 1203 | + return true; |
|
| 1204 | 1204 | } |
| 1205 | 1205 | |
| 1206 | 1206 | /** |
@@ -1217,12 +1217,12 @@ discard block |
||
| 1217 | 1217 | * string si texte de la requête demandé, true sinon |
| 1218 | 1218 | **/ |
| 1219 | 1219 | function spip_sqlite_demarrer_transaction($serveur = '', $requeter = true) { |
| 1220 | - if (!$requeter) { |
|
| 1221 | - return "BEGIN TRANSACTION"; |
|
| 1222 | - } |
|
| 1223 | - spip_sqlite::demarrer_transaction($serveur); |
|
| 1220 | + if (!$requeter) { |
|
| 1221 | + return "BEGIN TRANSACTION"; |
|
| 1222 | + } |
|
| 1223 | + spip_sqlite::demarrer_transaction($serveur); |
|
| 1224 | 1224 | |
| 1225 | - return true; |
|
| 1225 | + return true; |
|
| 1226 | 1226 | } |
| 1227 | 1227 | |
| 1228 | 1228 | /** |
@@ -1236,12 +1236,12 @@ discard block |
||
| 1236 | 1236 | * string si texte de la requête demandé, true sinon |
| 1237 | 1237 | **/ |
| 1238 | 1238 | function spip_sqlite_terminer_transaction($serveur = '', $requeter = true) { |
| 1239 | - if (!$requeter) { |
|
| 1240 | - return "COMMIT"; |
|
| 1241 | - } |
|
| 1242 | - spip_sqlite::finir_transaction($serveur); |
|
| 1239 | + if (!$requeter) { |
|
| 1240 | + return "COMMIT"; |
|
| 1241 | + } |
|
| 1242 | + spip_sqlite::finir_transaction($serveur); |
|
| 1243 | 1243 | |
| 1244 | - return true; |
|
| 1244 | + return true; |
|
| 1245 | 1245 | } |
| 1246 | 1246 | |
| 1247 | 1247 | |
@@ -1256,27 +1256,27 @@ discard block |
||
| 1256 | 1256 | * Liste des noms de bases |
| 1257 | 1257 | **/ |
| 1258 | 1258 | function spip_sqlite_listdbs($serveur = '', $requeter = true) { |
| 1259 | - _sqlite_init(); |
|
| 1259 | + _sqlite_init(); |
|
| 1260 | 1260 | |
| 1261 | - if (!is_dir($d = substr(_DIR_DB, 0, -1))) { |
|
| 1262 | - return array(); |
|
| 1263 | - } |
|
| 1261 | + if (!is_dir($d = substr(_DIR_DB, 0, -1))) { |
|
| 1262 | + return array(); |
|
| 1263 | + } |
|
| 1264 | 1264 | |
| 1265 | - include_spip('inc/flock'); |
|
| 1266 | - $bases = preg_files($d, $pattern = '(.*)\.sqlite$'); |
|
| 1267 | - $bds = array(); |
|
| 1265 | + include_spip('inc/flock'); |
|
| 1266 | + $bases = preg_files($d, $pattern = '(.*)\.sqlite$'); |
|
| 1267 | + $bds = array(); |
|
| 1268 | 1268 | |
| 1269 | - foreach ($bases as $b) { |
|
| 1270 | - // pas de bases commencant pas sqlite |
|
| 1271 | - // (on s'en sert pour l'installation pour simuler la presence d'un serveur) |
|
| 1272 | - // les bases sont de la forme _sqliteX_tmp_spip_install.sqlite |
|
| 1273 | - if (strpos($b, '_sqlite')) { |
|
| 1274 | - continue; |
|
| 1275 | - } |
|
| 1276 | - $bds[] = preg_replace(";.*/$pattern;iS", '$1', $b); |
|
| 1277 | - } |
|
| 1269 | + foreach ($bases as $b) { |
|
| 1270 | + // pas de bases commencant pas sqlite |
|
| 1271 | + // (on s'en sert pour l'installation pour simuler la presence d'un serveur) |
|
| 1272 | + // les bases sont de la forme _sqliteX_tmp_spip_install.sqlite |
|
| 1273 | + if (strpos($b, '_sqlite')) { |
|
| 1274 | + continue; |
|
| 1275 | + } |
|
| 1276 | + $bds[] = preg_replace(";.*/$pattern;iS", '$1', $b); |
|
| 1277 | + } |
|
| 1278 | 1278 | |
| 1279 | - return $bds; |
|
| 1279 | + return $bds; |
|
| 1280 | 1280 | } |
| 1281 | 1281 | |
| 1282 | 1282 | |
@@ -1291,9 +1291,9 @@ discard block |
||
| 1291 | 1291 | * @return string Texte de sélection pour la requête |
| 1292 | 1292 | */ |
| 1293 | 1293 | function spip_sqlite_multi($objet, $lang) { |
| 1294 | - $r = "EXTRAIRE_MULTI(" . $objet . ", '" . $lang . "') AS multi"; |
|
| 1294 | + $r = "EXTRAIRE_MULTI(" . $objet . ", '" . $lang . "') AS multi"; |
|
| 1295 | 1295 | |
| 1296 | - return $r; |
|
| 1296 | + return $r; |
|
| 1297 | 1297 | } |
| 1298 | 1298 | |
| 1299 | 1299 | |
@@ -1310,15 +1310,15 @@ discard block |
||
| 1310 | 1310 | * @return bool|string true / false / requete |
| 1311 | 1311 | **/ |
| 1312 | 1312 | function spip_sqlite_optimize($table, $serveur = '', $requeter = true) { |
| 1313 | - static $do = false; |
|
| 1314 | - if ($requeter and $do) { |
|
| 1315 | - return true; |
|
| 1316 | - } |
|
| 1317 | - if ($requeter) { |
|
| 1318 | - $do = true; |
|
| 1319 | - } |
|
| 1313 | + static $do = false; |
|
| 1314 | + if ($requeter and $do) { |
|
| 1315 | + return true; |
|
| 1316 | + } |
|
| 1317 | + if ($requeter) { |
|
| 1318 | + $do = true; |
|
| 1319 | + } |
|
| 1320 | 1320 | |
| 1321 | - return spip_sqlite_query("VACUUM", $serveur, $requeter); |
|
| 1321 | + return spip_sqlite_query("VACUUM", $serveur, $requeter); |
|
| 1322 | 1322 | } |
| 1323 | 1323 | |
| 1324 | 1324 | |
@@ -1335,15 +1335,15 @@ discard block |
||
| 1335 | 1335 | * Donnée prête à être utilisée par le gestionnaire SQL |
| 1336 | 1336 | */ |
| 1337 | 1337 | function spip_sqlite_quote($v, $type = '') { |
| 1338 | - if (!is_array($v)) { |
|
| 1339 | - return _sqlite_calculer_cite($v, $type); |
|
| 1340 | - } |
|
| 1341 | - // si c'est un tableau, le parcourir en propageant le type |
|
| 1342 | - foreach ($v as $k => $r) { |
|
| 1343 | - $v[$k] = spip_sqlite_quote($r, $type); |
|
| 1344 | - } |
|
| 1338 | + if (!is_array($v)) { |
|
| 1339 | + return _sqlite_calculer_cite($v, $type); |
|
| 1340 | + } |
|
| 1341 | + // si c'est un tableau, le parcourir en propageant le type |
|
| 1342 | + foreach ($v as $k => $r) { |
|
| 1343 | + $v[$k] = spip_sqlite_quote($r, $type); |
|
| 1344 | + } |
|
| 1345 | 1345 | |
| 1346 | - return join(",", $v); |
|
| 1346 | + return join(",", $v); |
|
| 1347 | 1347 | } |
| 1348 | 1348 | |
| 1349 | 1349 | |
@@ -1360,9 +1360,9 @@ discard block |
||
| 1360 | 1360 | * Expression SQL |
| 1361 | 1361 | **/ |
| 1362 | 1362 | function spip_sqlite_date_proche($champ, $interval, $unite) { |
| 1363 | - $op = (($interval <= 0) ? '>' : '<'); |
|
| 1363 | + $op = (($interval <= 0) ? '>' : '<'); |
|
| 1364 | 1364 | |
| 1365 | - return "($champ $op datetime('" . date("Y-m-d H:i:s") . "', '$interval $unite'))"; |
|
| 1365 | + return "($champ $op datetime('" . date("Y-m-d H:i:s") . "', '$interval $unite'))"; |
|
| 1366 | 1366 | } |
| 1367 | 1367 | |
| 1368 | 1368 | |
@@ -1380,44 +1380,44 @@ discard block |
||
| 1380 | 1380 | * l'état de la table après la réparation |
| 1381 | 1381 | */ |
| 1382 | 1382 | function spip_sqlite_repair($table, $serveur = '', $requeter = true) { |
| 1383 | - if ($desc = spip_sqlite_showtable($table, $serveur) |
|
| 1384 | - and isset($desc['field']) |
|
| 1385 | - and is_array($desc['field']) |
|
| 1386 | - ) { |
|
| 1387 | - foreach ($desc['field'] as $c => $d) { |
|
| 1388 | - if (preg_match(",^(tinytext|mediumtext|text|longtext|varchar|char),i", $d) |
|
| 1389 | - and stripos($d, "NOT NULL") !== false |
|
| 1390 | - and stripos($d, "DEFAULT") === false |
|
| 1391 | - /* pas touche aux cles primaires */ |
|
| 1392 | - and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1393 | - ) { |
|
| 1394 | - spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT ''", $serveur); |
|
| 1395 | - spip_log("ALTER $q", "repair" . _LOG_INFO_IMPORTANTE); |
|
| 1396 | - } |
|
| 1397 | - if (preg_match(",^(INTEGER),i", $d) |
|
| 1398 | - and stripos($d, "NOT NULL") !== false |
|
| 1399 | - and stripos($d, "DEFAULT") === false |
|
| 1400 | - /* pas touche aux cles primaires */ |
|
| 1401 | - and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1402 | - ) { |
|
| 1403 | - spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT '0'", $serveur); |
|
| 1404 | - spip_log("ALTER $q", "repair" . _LOG_INFO_IMPORTANTE); |
|
| 1405 | - } |
|
| 1406 | - if (preg_match(",^(datetime),i", $d) |
|
| 1407 | - and stripos($d, "NOT NULL") !== false |
|
| 1408 | - and stripos($d, "DEFAULT") === false |
|
| 1409 | - /* pas touche aux cles primaires */ |
|
| 1410 | - and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1411 | - ) { |
|
| 1412 | - spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT '0000-00-00 00:00:00'", $serveur); |
|
| 1413 | - spip_log("ALTER $q", "repair" . _LOG_INFO_IMPORTANTE); |
|
| 1414 | - } |
|
| 1415 | - } |
|
| 1416 | - |
|
| 1417 | - return array(" OK "); |
|
| 1418 | - } |
|
| 1419 | - |
|
| 1420 | - return array(" ERROR "); |
|
| 1383 | + if ($desc = spip_sqlite_showtable($table, $serveur) |
|
| 1384 | + and isset($desc['field']) |
|
| 1385 | + and is_array($desc['field']) |
|
| 1386 | + ) { |
|
| 1387 | + foreach ($desc['field'] as $c => $d) { |
|
| 1388 | + if (preg_match(",^(tinytext|mediumtext|text|longtext|varchar|char),i", $d) |
|
| 1389 | + and stripos($d, "NOT NULL") !== false |
|
| 1390 | + and stripos($d, "DEFAULT") === false |
|
| 1391 | + /* pas touche aux cles primaires */ |
|
| 1392 | + and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1393 | + ) { |
|
| 1394 | + spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT ''", $serveur); |
|
| 1395 | + spip_log("ALTER $q", "repair" . _LOG_INFO_IMPORTANTE); |
|
| 1396 | + } |
|
| 1397 | + if (preg_match(",^(INTEGER),i", $d) |
|
| 1398 | + and stripos($d, "NOT NULL") !== false |
|
| 1399 | + and stripos($d, "DEFAULT") === false |
|
| 1400 | + /* pas touche aux cles primaires */ |
|
| 1401 | + and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1402 | + ) { |
|
| 1403 | + spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT '0'", $serveur); |
|
| 1404 | + spip_log("ALTER $q", "repair" . _LOG_INFO_IMPORTANTE); |
|
| 1405 | + } |
|
| 1406 | + if (preg_match(",^(datetime),i", $d) |
|
| 1407 | + and stripos($d, "NOT NULL") !== false |
|
| 1408 | + and stripos($d, "DEFAULT") === false |
|
| 1409 | + /* pas touche aux cles primaires */ |
|
| 1410 | + and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
|
| 1411 | + ) { |
|
| 1412 | + spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT '0000-00-00 00:00:00'", $serveur); |
|
| 1413 | + spip_log("ALTER $q", "repair" . _LOG_INFO_IMPORTANTE); |
|
| 1414 | + } |
|
| 1415 | + } |
|
| 1416 | + |
|
| 1417 | + return array(" OK "); |
|
| 1418 | + } |
|
| 1419 | + |
|
| 1420 | + return array(" ERROR "); |
|
| 1421 | 1421 | } |
| 1422 | 1422 | |
| 1423 | 1423 | |
@@ -1446,23 +1446,23 @@ discard block |
||
| 1446 | 1446 | * - False en cas d'erreur. |
| 1447 | 1447 | **/ |
| 1448 | 1448 | function spip_sqlite_replace($table, $couples, $desc = array(), $serveur = '', $requeter = true) { |
| 1449 | - if (!$desc) { |
|
| 1450 | - $desc = description_table($table, $serveur); |
|
| 1451 | - } |
|
| 1452 | - if (!$desc) { |
|
| 1453 | - die("$table insertion sans description"); |
|
| 1454 | - } |
|
| 1455 | - $fields = isset($desc['field']) ? $desc['field'] : array(); |
|
| 1449 | + if (!$desc) { |
|
| 1450 | + $desc = description_table($table, $serveur); |
|
| 1451 | + } |
|
| 1452 | + if (!$desc) { |
|
| 1453 | + die("$table insertion sans description"); |
|
| 1454 | + } |
|
| 1455 | + $fields = isset($desc['field']) ? $desc['field'] : array(); |
|
| 1456 | 1456 | |
| 1457 | - foreach ($couples as $champ => $val) { |
|
| 1458 | - $couples[$champ] = _sqlite_calculer_cite($val, $fields[$champ]); |
|
| 1459 | - } |
|
| 1457 | + foreach ($couples as $champ => $val) { |
|
| 1458 | + $couples[$champ] = _sqlite_calculer_cite($val, $fields[$champ]); |
|
| 1459 | + } |
|
| 1460 | 1460 | |
| 1461 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1462 | - $couples = _sqlite_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1461 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1462 | + $couples = _sqlite_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
|
| 1463 | 1463 | |
| 1464 | - return spip_sqlite_query("REPLACE INTO $table (" . join(',', array_keys($couples)) . ') VALUES (' . join(',', |
|
| 1465 | - $couples) . ')', $serveur); |
|
| 1464 | + return spip_sqlite_query("REPLACE INTO $table (" . join(',', array_keys($couples)) . ') VALUES (' . join(',', |
|
| 1465 | + $couples) . ')', $serveur); |
|
| 1466 | 1466 | } |
| 1467 | 1467 | |
| 1468 | 1468 | |
@@ -1492,13 +1492,13 @@ discard block |
||
| 1492 | 1492 | **/ |
| 1493 | 1493 | function spip_sqlite_replace_multi($table, $tab_couples, $desc = array(), $serveur = '', $requeter = true) { |
| 1494 | 1494 | |
| 1495 | - // boucler pour trainter chaque requete independemment |
|
| 1496 | - foreach ($tab_couples as $couples) { |
|
| 1497 | - $retour = spip_sqlite_replace($table, $couples, $desc, $serveur, $requeter); |
|
| 1498 | - } |
|
| 1495 | + // boucler pour trainter chaque requete independemment |
|
| 1496 | + foreach ($tab_couples as $couples) { |
|
| 1497 | + $retour = spip_sqlite_replace($table, $couples, $desc, $serveur, $requeter); |
|
| 1498 | + } |
|
| 1499 | 1499 | |
| 1500 | - // renvoie le dernier id |
|
| 1501 | - return $retour; |
|
| 1500 | + // renvoie le dernier id |
|
| 1501 | + return $retour; |
|
| 1502 | 1502 | } |
| 1503 | 1503 | |
| 1504 | 1504 | |
@@ -1525,44 +1525,44 @@ discard block |
||
| 1525 | 1525 | * - array : Tableau décrivant requête et temps d'exécution si var_profile actif pour tracer. |
| 1526 | 1526 | */ |
| 1527 | 1527 | function spip_sqlite_select( |
| 1528 | - $select, |
|
| 1529 | - $from, |
|
| 1530 | - $where = '', |
|
| 1531 | - $groupby = '', |
|
| 1532 | - $orderby = '', |
|
| 1533 | - $limit = '', |
|
| 1534 | - $having = '', |
|
| 1535 | - $serveur = '', |
|
| 1536 | - $requeter = true |
|
| 1528 | + $select, |
|
| 1529 | + $from, |
|
| 1530 | + $where = '', |
|
| 1531 | + $groupby = '', |
|
| 1532 | + $orderby = '', |
|
| 1533 | + $limit = '', |
|
| 1534 | + $having = '', |
|
| 1535 | + $serveur = '', |
|
| 1536 | + $requeter = true |
|
| 1537 | 1537 | ) { |
| 1538 | 1538 | |
| 1539 | - // version() n'est pas connu de sqlite |
|
| 1540 | - $select = str_replace('version()', 'sqlite_version()', $select); |
|
| 1539 | + // version() n'est pas connu de sqlite |
|
| 1540 | + $select = str_replace('version()', 'sqlite_version()', $select); |
|
| 1541 | 1541 | |
| 1542 | - // recomposer from |
|
| 1543 | - $from = (!is_array($from) ? $from : _sqlite_calculer_select_as($from)); |
|
| 1542 | + // recomposer from |
|
| 1543 | + $from = (!is_array($from) ? $from : _sqlite_calculer_select_as($from)); |
|
| 1544 | 1544 | |
| 1545 | - $query = |
|
| 1546 | - _sqlite_calculer_expression('SELECT', $select, ', ') |
|
| 1547 | - . _sqlite_calculer_expression('FROM', $from, ', ') |
|
| 1548 | - . _sqlite_calculer_expression('WHERE', $where) |
|
| 1549 | - . _sqlite_calculer_expression('GROUP BY', $groupby, ',') |
|
| 1550 | - . _sqlite_calculer_expression('HAVING', $having) |
|
| 1551 | - . ($orderby ? ("\nORDER BY " . _sqlite_calculer_order($orderby)) : '') |
|
| 1552 | - . ($limit ? "\nLIMIT $limit" : ''); |
|
| 1545 | + $query = |
|
| 1546 | + _sqlite_calculer_expression('SELECT', $select, ', ') |
|
| 1547 | + . _sqlite_calculer_expression('FROM', $from, ', ') |
|
| 1548 | + . _sqlite_calculer_expression('WHERE', $where) |
|
| 1549 | + . _sqlite_calculer_expression('GROUP BY', $groupby, ',') |
|
| 1550 | + . _sqlite_calculer_expression('HAVING', $having) |
|
| 1551 | + . ($orderby ? ("\nORDER BY " . _sqlite_calculer_order($orderby)) : '') |
|
| 1552 | + . ($limit ? "\nLIMIT $limit" : ''); |
|
| 1553 | 1553 | |
| 1554 | - // dans un select, on doit renvoyer la requête en cas d'erreur |
|
| 1555 | - $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1556 | - // texte de la requete demande ? |
|
| 1557 | - if (!$requeter) { |
|
| 1558 | - return $res; |
|
| 1559 | - } |
|
| 1560 | - // erreur survenue ? |
|
| 1561 | - if ($res === false) { |
|
| 1562 | - return spip_sqlite::traduire_requete($query, $serveur); |
|
| 1563 | - } |
|
| 1554 | + // dans un select, on doit renvoyer la requête en cas d'erreur |
|
| 1555 | + $res = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1556 | + // texte de la requete demande ? |
|
| 1557 | + if (!$requeter) { |
|
| 1558 | + return $res; |
|
| 1559 | + } |
|
| 1560 | + // erreur survenue ? |
|
| 1561 | + if ($res === false) { |
|
| 1562 | + return spip_sqlite::traduire_requete($query, $serveur); |
|
| 1563 | + } |
|
| 1564 | 1564 | |
| 1565 | - return $res; |
|
| 1565 | + return $res; |
|
| 1566 | 1566 | } |
| 1567 | 1567 | |
| 1568 | 1568 | |
@@ -1581,31 +1581,31 @@ discard block |
||
| 1581 | 1581 | * - False en cas d'erreur. |
| 1582 | 1582 | **/ |
| 1583 | 1583 | function spip_sqlite_selectdb($db, $serveur = '', $requeter = true) { |
| 1584 | - _sqlite_init(); |
|
| 1584 | + _sqlite_init(); |
|
| 1585 | 1585 | |
| 1586 | - // interdire la creation d'une nouvelle base, |
|
| 1587 | - // sauf si on est dans l'installation |
|
| 1588 | - if (!is_file($f = _DIR_DB . $db . '.sqlite') |
|
| 1589 | - && (!defined('_ECRIRE_INSTALL') || !_ECRIRE_INSTALL) |
|
| 1590 | - ) { |
|
| 1591 | - spip_log("Il est interdit de creer la base $db", 'sqlite.' . _LOG_HS); |
|
| 1586 | + // interdire la creation d'une nouvelle base, |
|
| 1587 | + // sauf si on est dans l'installation |
|
| 1588 | + if (!is_file($f = _DIR_DB . $db . '.sqlite') |
|
| 1589 | + && (!defined('_ECRIRE_INSTALL') || !_ECRIRE_INSTALL) |
|
| 1590 | + ) { |
|
| 1591 | + spip_log("Il est interdit de creer la base $db", 'sqlite.' . _LOG_HS); |
|
| 1592 | 1592 | |
| 1593 | - return false; |
|
| 1594 | - } |
|
| 1593 | + return false; |
|
| 1594 | + } |
|
| 1595 | 1595 | |
| 1596 | - // se connecter a la base indiquee |
|
| 1597 | - // avec les identifiants connus |
|
| 1598 | - $index = $serveur ? $serveur : 0; |
|
| 1596 | + // se connecter a la base indiquee |
|
| 1597 | + // avec les identifiants connus |
|
| 1598 | + $index = $serveur ? $serveur : 0; |
|
| 1599 | 1599 | |
| 1600 | - if ($link = spip_connect_db('', '', '', '', '@selectdb@' . $db, $serveur, '', '')) { |
|
| 1601 | - if (($db == $link['db']) && $GLOBALS['connexions'][$index] = $link) { |
|
| 1602 | - return $db; |
|
| 1603 | - } |
|
| 1604 | - } else { |
|
| 1605 | - spip_log("Impossible de selectionner la base $db", 'sqlite.' . _LOG_HS); |
|
| 1600 | + if ($link = spip_connect_db('', '', '', '', '@selectdb@' . $db, $serveur, '', '')) { |
|
| 1601 | + if (($db == $link['db']) && $GLOBALS['connexions'][$index] = $link) { |
|
| 1602 | + return $db; |
|
| 1603 | + } |
|
| 1604 | + } else { |
|
| 1605 | + spip_log("Impossible de selectionner la base $db", 'sqlite.' . _LOG_HS); |
|
| 1606 | 1606 | |
| 1607 | - return false; |
|
| 1608 | - } |
|
| 1607 | + return false; |
|
| 1608 | + } |
|
| 1609 | 1609 | |
| 1610 | 1610 | } |
| 1611 | 1611 | |
@@ -1621,8 +1621,8 @@ discard block |
||
| 1621 | 1621 | * @return void |
| 1622 | 1622 | */ |
| 1623 | 1623 | function spip_sqlite_set_charset($charset, $serveur = '', $requeter = true) { |
| 1624 | - # spip_log("Gestion charset sql a ecrire : "."SET NAMES "._q($charset), 'sqlite.'._LOG_ERREUR); |
|
| 1625 | - # return spip_sqlite_query("SET NAMES ". spip_sqlite_quote($charset), $serveur); //<-- Passe pas ! |
|
| 1624 | + # spip_log("Gestion charset sql a ecrire : "."SET NAMES "._q($charset), 'sqlite.'._LOG_ERREUR); |
|
| 1625 | + # return spip_sqlite_query("SET NAMES ". spip_sqlite_quote($charset), $serveur); //<-- Passe pas ! |
|
| 1626 | 1626 | } |
| 1627 | 1627 | |
| 1628 | 1628 | |
@@ -1640,21 +1640,21 @@ discard block |
||
| 1640 | 1640 | * Ressource à utiliser avec sql_fetch() |
| 1641 | 1641 | **/ |
| 1642 | 1642 | function spip_sqlite_showbase($match, $serveur = '', $requeter = true) { |
| 1643 | - // type est le type d'entrée : table / index / view |
|
| 1644 | - // on ne retourne que les tables (?) et non les vues... |
|
| 1645 | - # ESCAPE non supporte par les versions sqlite <3 |
|
| 1646 | - # return spip_sqlite_query("SELECT name FROM sqlite_master WHERE type='table' AND tbl_name LIKE "._q($match)." ESCAPE '\'", $serveur, $requeter); |
|
| 1647 | - $match = preg_quote($match); |
|
| 1648 | - $match = str_replace("\\\_", "[[TIRETBAS]]", $match); |
|
| 1649 | - $match = str_replace("\\\%", "[[POURCENT]]", $match); |
|
| 1650 | - $match = str_replace("_", ".", $match); |
|
| 1651 | - $match = str_replace("%", ".*", $match); |
|
| 1652 | - $match = str_replace("[[TIRETBAS]]", "_", $match); |
|
| 1653 | - $match = str_replace("[[POURCENT]]", "%", $match); |
|
| 1654 | - $match = "^$match$"; |
|
| 1655 | - |
|
| 1656 | - return spip_sqlite_query("SELECT name FROM sqlite_master WHERE type='table' AND tbl_name REGEXP " . _q($match), |
|
| 1657 | - $serveur, $requeter); |
|
| 1643 | + // type est le type d'entrée : table / index / view |
|
| 1644 | + // on ne retourne que les tables (?) et non les vues... |
|
| 1645 | + # ESCAPE non supporte par les versions sqlite <3 |
|
| 1646 | + # return spip_sqlite_query("SELECT name FROM sqlite_master WHERE type='table' AND tbl_name LIKE "._q($match)." ESCAPE '\'", $serveur, $requeter); |
|
| 1647 | + $match = preg_quote($match); |
|
| 1648 | + $match = str_replace("\\\_", "[[TIRETBAS]]", $match); |
|
| 1649 | + $match = str_replace("\\\%", "[[POURCENT]]", $match); |
|
| 1650 | + $match = str_replace("_", ".", $match); |
|
| 1651 | + $match = str_replace("%", ".*", $match); |
|
| 1652 | + $match = str_replace("[[TIRETBAS]]", "_", $match); |
|
| 1653 | + $match = str_replace("[[POURCENT]]", "%", $match); |
|
| 1654 | + $match = "^$match$"; |
|
| 1655 | + |
|
| 1656 | + return spip_sqlite_query("SELECT name FROM sqlite_master WHERE type='table' AND tbl_name REGEXP " . _q($match), |
|
| 1657 | + $serveur, $requeter); |
|
| 1658 | 1658 | } |
| 1659 | 1659 | |
| 1660 | 1660 | define('_SQLITE_RE_SHOW_TABLE', '/^[^(),]*\(((?:[^()]*\((?:[^()]*\([^()]*\))?[^()]*\)[^()]*)*[^()]*)\)[^()]*$/'); |
@@ -1677,129 +1677,129 @@ discard block |
||
| 1677 | 1677 | * - array description de la table sinon |
| 1678 | 1678 | */ |
| 1679 | 1679 | function spip_sqlite_showtable($nom_table, $serveur = '', $requeter = true) { |
| 1680 | - $query = |
|
| 1681 | - 'SELECT sql, type FROM' |
|
| 1682 | - . ' (SELECT * FROM sqlite_master UNION ALL' |
|
| 1683 | - . ' SELECT * FROM sqlite_temp_master)' |
|
| 1684 | - . " WHERE tbl_name LIKE '$nom_table'" |
|
| 1685 | - . " AND type!='meta' AND sql NOT NULL AND name NOT LIKE 'sqlite_%'" |
|
| 1686 | - . ' ORDER BY substr(type,2,1), name'; |
|
| 1687 | - |
|
| 1688 | - $a = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1689 | - if (!$a) { |
|
| 1690 | - return ""; |
|
| 1691 | - } |
|
| 1692 | - if (!$requeter) { |
|
| 1693 | - return $a; |
|
| 1694 | - } |
|
| 1695 | - if (!($a = spip_sqlite_fetch($a, null, $serveur))) { |
|
| 1696 | - return ""; |
|
| 1697 | - } |
|
| 1698 | - $vue = ($a['type'] == 'view'); // table | vue |
|
| 1699 | - |
|
| 1700 | - // c'est une table |
|
| 1701 | - // il faut parser le create |
|
| 1702 | - if (!$vue) { |
|
| 1703 | - if (!preg_match(_SQLITE_RE_SHOW_TABLE, array_shift($a), $r)) { |
|
| 1704 | - return ""; |
|
| 1705 | - } else { |
|
| 1706 | - $desc = $r[1]; |
|
| 1707 | - // extraction d'une KEY éventuelle en prenant garde de ne pas |
|
| 1708 | - // relever un champ dont le nom contient KEY (ex. ID_WHISKEY) |
|
| 1709 | - if (preg_match("/^(.*?),([^,]*\sKEY[ (].*)$/s", $desc, $r)) { |
|
| 1710 | - $namedkeys = $r[2]; |
|
| 1711 | - $desc = $r[1]; |
|
| 1712 | - } else { |
|
| 1713 | - $namedkeys = ""; |
|
| 1714 | - } |
|
| 1715 | - |
|
| 1716 | - $fields = array(); |
|
| 1717 | - $keys = array(); |
|
| 1718 | - |
|
| 1719 | - // enlever les contenus des valeurs DEFAULT 'xxx' qui pourraient perturber |
|
| 1720 | - // par exemple s'il contiennent une virgule. |
|
| 1721 | - // /!\ cela peut aussi echapper le nom des champs si la table a eu des operations avec SQLite Manager ! |
|
| 1722 | - list($desc, $echaps) = query_echappe_textes($desc); |
|
| 1723 | - |
|
| 1724 | - // separer toutes les descriptions de champs, separes par des virgules |
|
| 1725 | - # /!\ explode peut exploser aussi DECIMAL(10,2) ! |
|
| 1726 | - $k_precedent = null; |
|
| 1727 | - foreach (explode(",", $desc) as $v) { |
|
| 1728 | - |
|
| 1729 | - preg_match("/^\s*([^\s]+)\s+(.*)/", $v, $r); |
|
| 1730 | - // Les cles de champs peuvent etre entourees |
|
| 1731 | - // de guillements doubles " , simples ', graves ` ou de crochets [ ], ou rien. |
|
| 1732 | - // http://www.sqlite.org/lang_keywords.html |
|
| 1733 | - $k = strtolower(query_reinjecte_textes($r[1], $echaps)); // champ, "champ", [champ]... |
|
| 1734 | - if ($char = strpbrk($k[0], '\'"[`')) { |
|
| 1735 | - $k = trim($k, $char); |
|
| 1736 | - if ($char == '[') { |
|
| 1737 | - $k = rtrim($k, ']'); |
|
| 1738 | - } |
|
| 1739 | - } |
|
| 1740 | - $def = query_reinjecte_textes($r[2], $echaps); // valeur du champ |
|
| 1741 | - |
|
| 1742 | - # rustine pour DECIMAL(10,2) |
|
| 1743 | - if (false !== strpos($k, ')')) { |
|
| 1744 | - $fields[$k_precedent] .= ',' . $k . ' ' . $def; |
|
| 1745 | - continue; |
|
| 1746 | - } |
|
| 1747 | - |
|
| 1748 | - // la primary key peut etre dans une des descriptions de champs |
|
| 1749 | - // et non en fin de table, cas encore decouvert avec Sqlite Manager |
|
| 1750 | - if (stripos($r[2], 'PRIMARY KEY') !== false) { |
|
| 1751 | - $keys['PRIMARY KEY'] = $k; |
|
| 1752 | - } |
|
| 1753 | - |
|
| 1754 | - $fields[$k] = $def; |
|
| 1755 | - $k_precedent = $k; |
|
| 1756 | - } |
|
| 1757 | - // key inclues dans la requete |
|
| 1758 | - foreach (preg_split('/\)\s*(,|$)/', $namedkeys) as $v) { |
|
| 1759 | - if (preg_match("/^\s*([^(]*)\(([^(]*(\(\d+\))?)$/", $v, $r)) { |
|
| 1760 | - $k = str_replace("`", '', trim($r[1])); |
|
| 1761 | - $t = trim(strtolower(str_replace("`", '', $r[2])), '"'); |
|
| 1762 | - if ($k && !isset($keys[$k])) { |
|
| 1763 | - $keys[$k] = $t; |
|
| 1764 | - } else { |
|
| 1765 | - $keys[] = $t; |
|
| 1766 | - } |
|
| 1767 | - } |
|
| 1768 | - } |
|
| 1769 | - // sinon ajouter les key index |
|
| 1770 | - $query = |
|
| 1771 | - 'SELECT name,sql FROM' |
|
| 1772 | - . ' (SELECT * FROM sqlite_master UNION ALL' |
|
| 1773 | - . ' SELECT * FROM sqlite_temp_master)' |
|
| 1774 | - . " WHERE tbl_name LIKE '$nom_table'" |
|
| 1775 | - . " AND type='index' AND name NOT LIKE 'sqlite_%'" |
|
| 1776 | - . 'ORDER BY substr(type,2,1), name'; |
|
| 1777 | - $a = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1778 | - while ($r = spip_sqlite_fetch($a, null, $serveur)) { |
|
| 1779 | - $key = str_replace($nom_table . '_', '', $r['name']); // enlever le nom de la table ajoute a l'index |
|
| 1780 | - $keytype = "KEY"; |
|
| 1781 | - if (strpos($r['sql'], "UNIQUE INDEX") !== false) { |
|
| 1782 | - $keytype = "UNIQUE KEY"; |
|
| 1783 | - } |
|
| 1784 | - $colonnes = preg_replace(',.*\((.*)\).*,', '$1', $r['sql']); |
|
| 1785 | - $keys[$keytype . ' ' . $key] = $colonnes; |
|
| 1786 | - } |
|
| 1787 | - } |
|
| 1788 | - |
|
| 1789 | - } // c'est une vue, on liste les champs disponibles simplement |
|
| 1790 | - else { |
|
| 1791 | - if ($res = sql_fetsel('*', $nom_table, '', '', '', '1', '', $serveur)) { // limit 1 |
|
| 1792 | - $fields = array(); |
|
| 1793 | - foreach ($res as $c => $v) { |
|
| 1794 | - $fields[$c] = ''; |
|
| 1795 | - } |
|
| 1796 | - $keys = array(); |
|
| 1797 | - } else { |
|
| 1798 | - return ""; |
|
| 1799 | - } |
|
| 1800 | - } |
|
| 1801 | - |
|
| 1802 | - return array('field' => $fields, 'key' => $keys); |
|
| 1680 | + $query = |
|
| 1681 | + 'SELECT sql, type FROM' |
|
| 1682 | + . ' (SELECT * FROM sqlite_master UNION ALL' |
|
| 1683 | + . ' SELECT * FROM sqlite_temp_master)' |
|
| 1684 | + . " WHERE tbl_name LIKE '$nom_table'" |
|
| 1685 | + . " AND type!='meta' AND sql NOT NULL AND name NOT LIKE 'sqlite_%'" |
|
| 1686 | + . ' ORDER BY substr(type,2,1), name'; |
|
| 1687 | + |
|
| 1688 | + $a = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1689 | + if (!$a) { |
|
| 1690 | + return ""; |
|
| 1691 | + } |
|
| 1692 | + if (!$requeter) { |
|
| 1693 | + return $a; |
|
| 1694 | + } |
|
| 1695 | + if (!($a = spip_sqlite_fetch($a, null, $serveur))) { |
|
| 1696 | + return ""; |
|
| 1697 | + } |
|
| 1698 | + $vue = ($a['type'] == 'view'); // table | vue |
|
| 1699 | + |
|
| 1700 | + // c'est une table |
|
| 1701 | + // il faut parser le create |
|
| 1702 | + if (!$vue) { |
|
| 1703 | + if (!preg_match(_SQLITE_RE_SHOW_TABLE, array_shift($a), $r)) { |
|
| 1704 | + return ""; |
|
| 1705 | + } else { |
|
| 1706 | + $desc = $r[1]; |
|
| 1707 | + // extraction d'une KEY éventuelle en prenant garde de ne pas |
|
| 1708 | + // relever un champ dont le nom contient KEY (ex. ID_WHISKEY) |
|
| 1709 | + if (preg_match("/^(.*?),([^,]*\sKEY[ (].*)$/s", $desc, $r)) { |
|
| 1710 | + $namedkeys = $r[2]; |
|
| 1711 | + $desc = $r[1]; |
|
| 1712 | + } else { |
|
| 1713 | + $namedkeys = ""; |
|
| 1714 | + } |
|
| 1715 | + |
|
| 1716 | + $fields = array(); |
|
| 1717 | + $keys = array(); |
|
| 1718 | + |
|
| 1719 | + // enlever les contenus des valeurs DEFAULT 'xxx' qui pourraient perturber |
|
| 1720 | + // par exemple s'il contiennent une virgule. |
|
| 1721 | + // /!\ cela peut aussi echapper le nom des champs si la table a eu des operations avec SQLite Manager ! |
|
| 1722 | + list($desc, $echaps) = query_echappe_textes($desc); |
|
| 1723 | + |
|
| 1724 | + // separer toutes les descriptions de champs, separes par des virgules |
|
| 1725 | + # /!\ explode peut exploser aussi DECIMAL(10,2) ! |
|
| 1726 | + $k_precedent = null; |
|
| 1727 | + foreach (explode(",", $desc) as $v) { |
|
| 1728 | + |
|
| 1729 | + preg_match("/^\s*([^\s]+)\s+(.*)/", $v, $r); |
|
| 1730 | + // Les cles de champs peuvent etre entourees |
|
| 1731 | + // de guillements doubles " , simples ', graves ` ou de crochets [ ], ou rien. |
|
| 1732 | + // http://www.sqlite.org/lang_keywords.html |
|
| 1733 | + $k = strtolower(query_reinjecte_textes($r[1], $echaps)); // champ, "champ", [champ]... |
|
| 1734 | + if ($char = strpbrk($k[0], '\'"[`')) { |
|
| 1735 | + $k = trim($k, $char); |
|
| 1736 | + if ($char == '[') { |
|
| 1737 | + $k = rtrim($k, ']'); |
|
| 1738 | + } |
|
| 1739 | + } |
|
| 1740 | + $def = query_reinjecte_textes($r[2], $echaps); // valeur du champ |
|
| 1741 | + |
|
| 1742 | + # rustine pour DECIMAL(10,2) |
|
| 1743 | + if (false !== strpos($k, ')')) { |
|
| 1744 | + $fields[$k_precedent] .= ',' . $k . ' ' . $def; |
|
| 1745 | + continue; |
|
| 1746 | + } |
|
| 1747 | + |
|
| 1748 | + // la primary key peut etre dans une des descriptions de champs |
|
| 1749 | + // et non en fin de table, cas encore decouvert avec Sqlite Manager |
|
| 1750 | + if (stripos($r[2], 'PRIMARY KEY') !== false) { |
|
| 1751 | + $keys['PRIMARY KEY'] = $k; |
|
| 1752 | + } |
|
| 1753 | + |
|
| 1754 | + $fields[$k] = $def; |
|
| 1755 | + $k_precedent = $k; |
|
| 1756 | + } |
|
| 1757 | + // key inclues dans la requete |
|
| 1758 | + foreach (preg_split('/\)\s*(,|$)/', $namedkeys) as $v) { |
|
| 1759 | + if (preg_match("/^\s*([^(]*)\(([^(]*(\(\d+\))?)$/", $v, $r)) { |
|
| 1760 | + $k = str_replace("`", '', trim($r[1])); |
|
| 1761 | + $t = trim(strtolower(str_replace("`", '', $r[2])), '"'); |
|
| 1762 | + if ($k && !isset($keys[$k])) { |
|
| 1763 | + $keys[$k] = $t; |
|
| 1764 | + } else { |
|
| 1765 | + $keys[] = $t; |
|
| 1766 | + } |
|
| 1767 | + } |
|
| 1768 | + } |
|
| 1769 | + // sinon ajouter les key index |
|
| 1770 | + $query = |
|
| 1771 | + 'SELECT name,sql FROM' |
|
| 1772 | + . ' (SELECT * FROM sqlite_master UNION ALL' |
|
| 1773 | + . ' SELECT * FROM sqlite_temp_master)' |
|
| 1774 | + . " WHERE tbl_name LIKE '$nom_table'" |
|
| 1775 | + . " AND type='index' AND name NOT LIKE 'sqlite_%'" |
|
| 1776 | + . 'ORDER BY substr(type,2,1), name'; |
|
| 1777 | + $a = spip_sqlite_query($query, $serveur, $requeter); |
|
| 1778 | + while ($r = spip_sqlite_fetch($a, null, $serveur)) { |
|
| 1779 | + $key = str_replace($nom_table . '_', '', $r['name']); // enlever le nom de la table ajoute a l'index |
|
| 1780 | + $keytype = "KEY"; |
|
| 1781 | + if (strpos($r['sql'], "UNIQUE INDEX") !== false) { |
|
| 1782 | + $keytype = "UNIQUE KEY"; |
|
| 1783 | + } |
|
| 1784 | + $colonnes = preg_replace(',.*\((.*)\).*,', '$1', $r['sql']); |
|
| 1785 | + $keys[$keytype . ' ' . $key] = $colonnes; |
|
| 1786 | + } |
|
| 1787 | + } |
|
| 1788 | + |
|
| 1789 | + } // c'est une vue, on liste les champs disponibles simplement |
|
| 1790 | + else { |
|
| 1791 | + if ($res = sql_fetsel('*', $nom_table, '', '', '', '1', '', $serveur)) { // limit 1 |
|
| 1792 | + $fields = array(); |
|
| 1793 | + foreach ($res as $c => $v) { |
|
| 1794 | + $fields[$c] = ''; |
|
| 1795 | + } |
|
| 1796 | + $keys = array(); |
|
| 1797 | + } else { |
|
| 1798 | + return ""; |
|
| 1799 | + } |
|
| 1800 | + } |
|
| 1801 | + |
|
| 1802 | + return array('field' => $fields, 'key' => $keys); |
|
| 1803 | 1803 | |
| 1804 | 1804 | } |
| 1805 | 1805 | |
@@ -1826,20 +1826,20 @@ discard block |
||
| 1826 | 1826 | * - array Tableau décrivant la requête et son temps d'exécution si var_profile est actif |
| 1827 | 1827 | */ |
| 1828 | 1828 | function spip_sqlite_update($table, $champs, $where = '', $desc = '', $serveur = '', $requeter = true) { |
| 1829 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1830 | - $champs = _sqlite_ajouter_champs_timestamp($table, $champs, $desc, $serveur); |
|
| 1829 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1830 | + $champs = _sqlite_ajouter_champs_timestamp($table, $champs, $desc, $serveur); |
|
| 1831 | 1831 | |
| 1832 | - $set = array(); |
|
| 1833 | - foreach ($champs as $champ => $val) { |
|
| 1834 | - $set[] = $champ . "=$val"; |
|
| 1835 | - } |
|
| 1836 | - if (!empty($set)) { |
|
| 1837 | - return spip_sqlite_query( |
|
| 1838 | - _sqlite_calculer_expression('UPDATE', $table, ',') |
|
| 1839 | - . _sqlite_calculer_expression('SET', $set, ',') |
|
| 1840 | - . _sqlite_calculer_expression('WHERE', $where), |
|
| 1841 | - $serveur, $requeter); |
|
| 1842 | - } |
|
| 1832 | + $set = array(); |
|
| 1833 | + foreach ($champs as $champ => $val) { |
|
| 1834 | + $set[] = $champ . "=$val"; |
|
| 1835 | + } |
|
| 1836 | + if (!empty($set)) { |
|
| 1837 | + return spip_sqlite_query( |
|
| 1838 | + _sqlite_calculer_expression('UPDATE', $table, ',') |
|
| 1839 | + . _sqlite_calculer_expression('SET', $set, ',') |
|
| 1840 | + . _sqlite_calculer_expression('WHERE', $where), |
|
| 1841 | + $serveur, $requeter); |
|
| 1842 | + } |
|
| 1843 | 1843 | } |
| 1844 | 1844 | |
| 1845 | 1845 | |
@@ -1869,30 +1869,30 @@ discard block |
||
| 1869 | 1869 | */ |
| 1870 | 1870 | function spip_sqlite_updateq($table, $champs, $where = '', $desc = array(), $serveur = '', $requeter = true) { |
| 1871 | 1871 | |
| 1872 | - if (!$champs) { |
|
| 1873 | - return; |
|
| 1874 | - } |
|
| 1875 | - if (!$desc) { |
|
| 1876 | - $desc = description_table($table, $serveur); |
|
| 1877 | - } |
|
| 1878 | - if (!$desc) { |
|
| 1879 | - die("$table insertion sans description"); |
|
| 1880 | - } |
|
| 1881 | - $fields = $desc['field']; |
|
| 1872 | + if (!$champs) { |
|
| 1873 | + return; |
|
| 1874 | + } |
|
| 1875 | + if (!$desc) { |
|
| 1876 | + $desc = description_table($table, $serveur); |
|
| 1877 | + } |
|
| 1878 | + if (!$desc) { |
|
| 1879 | + die("$table insertion sans description"); |
|
| 1880 | + } |
|
| 1881 | + $fields = $desc['field']; |
|
| 1882 | 1882 | |
| 1883 | - // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1884 | - $champs = _sqlite_ajouter_champs_timestamp($table, $champs, $desc, $serveur); |
|
| 1883 | + // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
|
| 1884 | + $champs = _sqlite_ajouter_champs_timestamp($table, $champs, $desc, $serveur); |
|
| 1885 | 1885 | |
| 1886 | - $set = array(); |
|
| 1887 | - foreach ($champs as $champ => $val) { |
|
| 1888 | - $set[] = $champ . '=' . _sqlite_calculer_cite($val, isset($fields[$champ]) ? $fields[$champ] : ''); |
|
| 1889 | - } |
|
| 1886 | + $set = array(); |
|
| 1887 | + foreach ($champs as $champ => $val) { |
|
| 1888 | + $set[] = $champ . '=' . _sqlite_calculer_cite($val, isset($fields[$champ]) ? $fields[$champ] : ''); |
|
| 1889 | + } |
|
| 1890 | 1890 | |
| 1891 | - return spip_sqlite_query( |
|
| 1892 | - _sqlite_calculer_expression('UPDATE', $table, ',') |
|
| 1893 | - . _sqlite_calculer_expression('SET', $set, ',') |
|
| 1894 | - . _sqlite_calculer_expression('WHERE', $where), |
|
| 1895 | - $serveur, $requeter); |
|
| 1891 | + return spip_sqlite_query( |
|
| 1892 | + _sqlite_calculer_expression('UPDATE', $table, ',') |
|
| 1893 | + . _sqlite_calculer_expression('SET', $set, ',') |
|
| 1894 | + . _sqlite_calculer_expression('WHERE', $where), |
|
| 1895 | + $serveur, $requeter); |
|
| 1896 | 1896 | } |
| 1897 | 1897 | |
| 1898 | 1898 | |
@@ -1910,17 +1910,17 @@ discard block |
||
| 1910 | 1910 | * @return void |
| 1911 | 1911 | */ |
| 1912 | 1912 | function _sqlite_init() { |
| 1913 | - if (!defined('_DIR_DB')) { |
|
| 1914 | - define('_DIR_DB', _DIR_ETC . 'bases/'); |
|
| 1915 | - } |
|
| 1916 | - if (!defined('_SQLITE_CHMOD')) { |
|
| 1917 | - define('_SQLITE_CHMOD', _SPIP_CHMOD); |
|
| 1918 | - } |
|
| 1913 | + if (!defined('_DIR_DB')) { |
|
| 1914 | + define('_DIR_DB', _DIR_ETC . 'bases/'); |
|
| 1915 | + } |
|
| 1916 | + if (!defined('_SQLITE_CHMOD')) { |
|
| 1917 | + define('_SQLITE_CHMOD', _SPIP_CHMOD); |
|
| 1918 | + } |
|
| 1919 | 1919 | |
| 1920 | - if (!is_dir($d = _DIR_DB)) { |
|
| 1921 | - include_spip('inc/flock'); |
|
| 1922 | - sous_repertoire($d); |
|
| 1923 | - } |
|
| 1920 | + if (!is_dir($d = _DIR_DB)) { |
|
| 1921 | + include_spip('inc/flock'); |
|
| 1922 | + sous_repertoire($d); |
|
| 1923 | + } |
|
| 1924 | 1924 | } |
| 1925 | 1925 | |
| 1926 | 1926 | |
@@ -1934,20 +1934,20 @@ discard block |
||
| 1934 | 1934 | * @return bool|int |
| 1935 | 1935 | */ |
| 1936 | 1936 | function _sqlite_is_version($version = '', $link = '', $serveur = '', $requeter = true) { |
| 1937 | - if ($link === '') { |
|
| 1938 | - $link = _sqlite_link($serveur); |
|
| 1939 | - } |
|
| 1940 | - if (!$link) { |
|
| 1941 | - return false; |
|
| 1942 | - } |
|
| 1937 | + if ($link === '') { |
|
| 1938 | + $link = _sqlite_link($serveur); |
|
| 1939 | + } |
|
| 1940 | + if (!$link) { |
|
| 1941 | + return false; |
|
| 1942 | + } |
|
| 1943 | 1943 | |
| 1944 | - $v = 3; |
|
| 1944 | + $v = 3; |
|
| 1945 | 1945 | |
| 1946 | - if (!$version) { |
|
| 1947 | - return $v; |
|
| 1948 | - } |
|
| 1946 | + if (!$version) { |
|
| 1947 | + return $v; |
|
| 1948 | + } |
|
| 1949 | 1949 | |
| 1950 | - return ($version == $v); |
|
| 1950 | + return ($version == $v); |
|
| 1951 | 1951 | } |
| 1952 | 1952 | |
| 1953 | 1953 | |
@@ -1958,9 +1958,9 @@ discard block |
||
| 1958 | 1958 | * @return Object Information de connexion pour SQLite |
| 1959 | 1959 | */ |
| 1960 | 1960 | function _sqlite_link($serveur = '') { |
| 1961 | - $link = &$GLOBALS['connexions'][$serveur ? $serveur : 0]['link']; |
|
| 1961 | + $link = &$GLOBALS['connexions'][$serveur ? $serveur : 0]['link']; |
|
| 1962 | 1962 | |
| 1963 | - return $link; |
|
| 1963 | + return $link; |
|
| 1964 | 1964 | } |
| 1965 | 1965 | |
| 1966 | 1966 | |
@@ -1975,48 +1975,48 @@ discard block |
||
| 1975 | 1975 | * @return string|number Texte ou nombre échappé |
| 1976 | 1976 | */ |
| 1977 | 1977 | function _sqlite_calculer_cite($v, $type) { |
| 1978 | - if ($type) { |
|
| 1979 | - if (is_null($v) |
|
| 1980 | - and stripos($type, "NOT NULL") === false |
|
| 1981 | - ) { |
|
| 1982 | - return 'NULL'; |
|
| 1983 | - } // null php se traduit en NULL SQL |
|
| 1984 | - |
|
| 1985 | - if (sql_test_date($type) and preg_match('/^\w+\(/', $v)) { |
|
| 1986 | - return $v; |
|
| 1987 | - } |
|
| 1988 | - if (sql_test_int($type)) { |
|
| 1989 | - if (is_numeric($v)) { |
|
| 1990 | - return $v; |
|
| 1991 | - } elseif (ctype_xdigit(substr($v, 2)) and strncmp($v, '0x', 2) == 0) { |
|
| 1992 | - return hexdec(substr($v, 2)); |
|
| 1993 | - } else { |
|
| 1994 | - return intval($v); |
|
| 1995 | - } |
|
| 1996 | - } |
|
| 1997 | - } else { |
|
| 1998 | - // si on ne connait pas le type on le deduit de $v autant que possible |
|
| 1999 | - if (is_numeric($v)) { |
|
| 2000 | - return strval($v); |
|
| 2001 | - } |
|
| 2002 | - } |
|
| 2003 | - |
|
| 2004 | - // trouver un link sqlite pour faire l'echappement |
|
| 2005 | - foreach ($GLOBALS['connexions'] as $s) { |
|
| 2006 | - if ( |
|
| 2007 | - $l = $s['link'] |
|
| 2008 | - and is_object($l) |
|
| 2009 | - and $l instanceof \PDO |
|
| 2010 | - and $l->getAttribute(\PDO::ATTR_DRIVER_NAME) === 'sqlite' |
|
| 2011 | - ) { |
|
| 2012 | - return $l->quote($v); |
|
| 2013 | - } |
|
| 2014 | - } |
|
| 2015 | - |
|
| 2016 | - // echapper les ' en '' |
|
| 2017 | - spip_log("Pas de methode ->quote pour echapper", "sqlite." . _LOG_INFO_IMPORTANTE); |
|
| 2018 | - |
|
| 2019 | - return ("'" . str_replace("'", "''", $v) . "'"); |
|
| 1978 | + if ($type) { |
|
| 1979 | + if (is_null($v) |
|
| 1980 | + and stripos($type, "NOT NULL") === false |
|
| 1981 | + ) { |
|
| 1982 | + return 'NULL'; |
|
| 1983 | + } // null php se traduit en NULL SQL |
|
| 1984 | + |
|
| 1985 | + if (sql_test_date($type) and preg_match('/^\w+\(/', $v)) { |
|
| 1986 | + return $v; |
|
| 1987 | + } |
|
| 1988 | + if (sql_test_int($type)) { |
|
| 1989 | + if (is_numeric($v)) { |
|
| 1990 | + return $v; |
|
| 1991 | + } elseif (ctype_xdigit(substr($v, 2)) and strncmp($v, '0x', 2) == 0) { |
|
| 1992 | + return hexdec(substr($v, 2)); |
|
| 1993 | + } else { |
|
| 1994 | + return intval($v); |
|
| 1995 | + } |
|
| 1996 | + } |
|
| 1997 | + } else { |
|
| 1998 | + // si on ne connait pas le type on le deduit de $v autant que possible |
|
| 1999 | + if (is_numeric($v)) { |
|
| 2000 | + return strval($v); |
|
| 2001 | + } |
|
| 2002 | + } |
|
| 2003 | + |
|
| 2004 | + // trouver un link sqlite pour faire l'echappement |
|
| 2005 | + foreach ($GLOBALS['connexions'] as $s) { |
|
| 2006 | + if ( |
|
| 2007 | + $l = $s['link'] |
|
| 2008 | + and is_object($l) |
|
| 2009 | + and $l instanceof \PDO |
|
| 2010 | + and $l->getAttribute(\PDO::ATTR_DRIVER_NAME) === 'sqlite' |
|
| 2011 | + ) { |
|
| 2012 | + return $l->quote($v); |
|
| 2013 | + } |
|
| 2014 | + } |
|
| 2015 | + |
|
| 2016 | + // echapper les ' en '' |
|
| 2017 | + spip_log("Pas de methode ->quote pour echapper", "sqlite." . _LOG_INFO_IMPORTANTE); |
|
| 2018 | + |
|
| 2019 | + return ("'" . str_replace("'", "''", $v) . "'"); |
|
| 2020 | 2020 | } |
| 2021 | 2021 | |
| 2022 | 2022 | |
@@ -2032,21 +2032,21 @@ discard block |
||
| 2032 | 2032 | * @return string Texte de l'expression, une partie donc, du texte la requête. |
| 2033 | 2033 | */ |
| 2034 | 2034 | function _sqlite_calculer_expression($expression, $v, $join = 'AND') { |
| 2035 | - if (empty($v)) { |
|
| 2036 | - return ''; |
|
| 2037 | - } |
|
| 2035 | + if (empty($v)) { |
|
| 2036 | + return ''; |
|
| 2037 | + } |
|
| 2038 | 2038 | |
| 2039 | - $exp = "\n$expression "; |
|
| 2039 | + $exp = "\n$expression "; |
|
| 2040 | 2040 | |
| 2041 | - if (!is_array($v)) { |
|
| 2042 | - return $exp . $v; |
|
| 2043 | - } else { |
|
| 2044 | - if (strtoupper($join) === 'AND') { |
|
| 2045 | - return $exp . join("\n\t$join ", array_map('_sqlite_calculer_where', $v)); |
|
| 2046 | - } else { |
|
| 2047 | - return $exp . join($join, $v); |
|
| 2048 | - } |
|
| 2049 | - } |
|
| 2041 | + if (!is_array($v)) { |
|
| 2042 | + return $exp . $v; |
|
| 2043 | + } else { |
|
| 2044 | + if (strtoupper($join) === 'AND') { |
|
| 2045 | + return $exp . join("\n\t$join ", array_map('_sqlite_calculer_where', $v)); |
|
| 2046 | + } else { |
|
| 2047 | + return $exp . join($join, $v); |
|
| 2048 | + } |
|
| 2049 | + } |
|
| 2050 | 2050 | } |
| 2051 | 2051 | |
| 2052 | 2052 | |
@@ -2062,7 +2062,7 @@ discard block |
||
| 2062 | 2062 | * @return string Texte du orderby préparé |
| 2063 | 2063 | */ |
| 2064 | 2064 | function _sqlite_calculer_order($orderby) { |
| 2065 | - return (is_array($orderby)) ? join(", ", $orderby) : $orderby; |
|
| 2065 | + return (is_array($orderby)) ? join(", ", $orderby) : $orderby; |
|
| 2066 | 2066 | } |
| 2067 | 2067 | |
| 2068 | 2068 | |
@@ -2073,26 +2073,26 @@ discard block |
||
| 2073 | 2073 | * @return string Sélection de colonnes pour une clause SELECT |
| 2074 | 2074 | */ |
| 2075 | 2075 | function _sqlite_calculer_select_as($args) { |
| 2076 | - $res = ''; |
|
| 2077 | - foreach ($args as $k => $v) { |
|
| 2078 | - if (substr($k, -1) == '@') { |
|
| 2079 | - // c'est une jointure qui se refere au from precedent |
|
| 2080 | - // pas de virgule |
|
| 2081 | - $res .= ' ' . $v; |
|
| 2082 | - } else { |
|
| 2083 | - if (!is_numeric($k)) { |
|
| 2084 | - $p = strpos($v, " "); |
|
| 2085 | - if ($p) { |
|
| 2086 | - $v = substr($v, 0, $p) . " AS '$k'" . substr($v, $p); |
|
| 2087 | - } else { |
|
| 2088 | - $v .= " AS '$k'"; |
|
| 2089 | - } |
|
| 2090 | - } |
|
| 2091 | - $res .= ', ' . $v; |
|
| 2092 | - } |
|
| 2093 | - } |
|
| 2094 | - |
|
| 2095 | - return substr($res, 2); |
|
| 2076 | + $res = ''; |
|
| 2077 | + foreach ($args as $k => $v) { |
|
| 2078 | + if (substr($k, -1) == '@') { |
|
| 2079 | + // c'est une jointure qui se refere au from precedent |
|
| 2080 | + // pas de virgule |
|
| 2081 | + $res .= ' ' . $v; |
|
| 2082 | + } else { |
|
| 2083 | + if (!is_numeric($k)) { |
|
| 2084 | + $p = strpos($v, " "); |
|
| 2085 | + if ($p) { |
|
| 2086 | + $v = substr($v, 0, $p) . " AS '$k'" . substr($v, $p); |
|
| 2087 | + } else { |
|
| 2088 | + $v .= " AS '$k'"; |
|
| 2089 | + } |
|
| 2090 | + } |
|
| 2091 | + $res .= ', ' . $v; |
|
| 2092 | + } |
|
| 2093 | + } |
|
| 2094 | + |
|
| 2095 | + return substr($res, 2); |
|
| 2096 | 2096 | } |
| 2097 | 2097 | |
| 2098 | 2098 | |
@@ -2115,26 +2115,26 @@ discard block |
||
| 2115 | 2115 | * Contrainte pour clause WHERE |
| 2116 | 2116 | */ |
| 2117 | 2117 | function _sqlite_calculer_where($v) { |
| 2118 | - if (!is_array($v)) { |
|
| 2119 | - return $v; |
|
| 2120 | - } |
|
| 2121 | - |
|
| 2122 | - $op = array_shift($v); |
|
| 2123 | - if (!($n = count($v))) { |
|
| 2124 | - return $op; |
|
| 2125 | - } else { |
|
| 2126 | - $arg = _sqlite_calculer_where(array_shift($v)); |
|
| 2127 | - if ($n == 1) { |
|
| 2128 | - return "$op($arg)"; |
|
| 2129 | - } else { |
|
| 2130 | - $arg2 = _sqlite_calculer_where(array_shift($v)); |
|
| 2131 | - if ($n == 2) { |
|
| 2132 | - return "($arg $op $arg2)"; |
|
| 2133 | - } else { |
|
| 2134 | - return "($arg $op ($arg2) : $v[0])"; |
|
| 2135 | - } |
|
| 2136 | - } |
|
| 2137 | - } |
|
| 2118 | + if (!is_array($v)) { |
|
| 2119 | + return $v; |
|
| 2120 | + } |
|
| 2121 | + |
|
| 2122 | + $op = array_shift($v); |
|
| 2123 | + if (!($n = count($v))) { |
|
| 2124 | + return $op; |
|
| 2125 | + } else { |
|
| 2126 | + $arg = _sqlite_calculer_where(array_shift($v)); |
|
| 2127 | + if ($n == 1) { |
|
| 2128 | + return "$op($arg)"; |
|
| 2129 | + } else { |
|
| 2130 | + $arg2 = _sqlite_calculer_where(array_shift($v)); |
|
| 2131 | + if ($n == 2) { |
|
| 2132 | + return "($arg $op $arg2)"; |
|
| 2133 | + } else { |
|
| 2134 | + return "($arg $op ($arg2) : $v[0])"; |
|
| 2135 | + } |
|
| 2136 | + } |
|
| 2137 | + } |
|
| 2138 | 2138 | } |
| 2139 | 2139 | |
| 2140 | 2140 | |
@@ -2149,19 +2149,19 @@ discard block |
||
| 2149 | 2149 | * @return array|bool |
| 2150 | 2150 | */ |
| 2151 | 2151 | function _sqlite_charger_version($version = '') { |
| 2152 | - $versions = array(); |
|
| 2152 | + $versions = array(); |
|
| 2153 | 2153 | |
| 2154 | - // version 3 |
|
| 2155 | - if (!$version || $version == 3) { |
|
| 2156 | - if (extension_loaded('pdo') && extension_loaded('pdo_sqlite')) { |
|
| 2157 | - $versions[] = 3; |
|
| 2158 | - } |
|
| 2159 | - } |
|
| 2160 | - if ($version) { |
|
| 2161 | - return in_array($version, $versions); |
|
| 2162 | - } |
|
| 2154 | + // version 3 |
|
| 2155 | + if (!$version || $version == 3) { |
|
| 2156 | + if (extension_loaded('pdo') && extension_loaded('pdo_sqlite')) { |
|
| 2157 | + $versions[] = 3; |
|
| 2158 | + } |
|
| 2159 | + } |
|
| 2160 | + if ($version) { |
|
| 2161 | + return in_array($version, $versions); |
|
| 2162 | + } |
|
| 2163 | 2163 | |
| 2164 | - return $versions; |
|
| 2164 | + return $versions; |
|
| 2165 | 2165 | } |
| 2166 | 2166 | |
| 2167 | 2167 | |
@@ -2199,146 +2199,146 @@ discard block |
||
| 2199 | 2199 | */ |
| 2200 | 2200 | function _sqlite_modifier_table($table, $colonne, $opt = array(), $serveur = '') { |
| 2201 | 2201 | |
| 2202 | - if (is_array($table)) { |
|
| 2203 | - $table_destination = reset($table); |
|
| 2204 | - $table_origine = key($table); |
|
| 2205 | - } else { |
|
| 2206 | - $table_origine = $table_destination = $table; |
|
| 2207 | - } |
|
| 2208 | - // ne prend actuellement qu'un changement |
|
| 2209 | - // mais pourra etre adapte pour changer plus qu'une colonne a la fois |
|
| 2210 | - if (is_array($colonne)) { |
|
| 2211 | - $colonne_destination = reset($colonne); |
|
| 2212 | - $colonne_origine = key($colonne); |
|
| 2213 | - } else { |
|
| 2214 | - $colonne_origine = $colonne_destination = $colonne; |
|
| 2215 | - } |
|
| 2216 | - if (!isset($opt['field'])) { |
|
| 2217 | - $opt['field'] = array(); |
|
| 2218 | - } |
|
| 2219 | - if (!isset($opt['key'])) { |
|
| 2220 | - $opt['key'] = array(); |
|
| 2221 | - } |
|
| 2222 | - |
|
| 2223 | - // si les noms de tables sont differents, pas besoin de table temporaire |
|
| 2224 | - // on prendra directement le nom de la future table |
|
| 2225 | - $meme_table = ($table_origine == $table_destination); |
|
| 2226 | - |
|
| 2227 | - $def_origine = sql_showtable($table_origine, false, $serveur); |
|
| 2228 | - if (!$def_origine or !isset($def_origine['field'])) { |
|
| 2229 | - spip_log("Alter table impossible sur $table_origine : table non trouvee", 'sqlite' . _LOG_ERREUR); |
|
| 2230 | - |
|
| 2231 | - return false; |
|
| 2232 | - } |
|
| 2233 | - |
|
| 2234 | - |
|
| 2235 | - $table_tmp = $table_origine . '_tmp'; |
|
| 2236 | - |
|
| 2237 | - // 1) creer une table temporaire avec les modifications |
|
| 2238 | - // - DROP : suppression de la colonne |
|
| 2239 | - // - CHANGE : modification de la colonne |
|
| 2240 | - // (foreach pour conserver l'ordre des champs) |
|
| 2241 | - |
|
| 2242 | - // field |
|
| 2243 | - $fields = array(); |
|
| 2244 | - // pour le INSERT INTO plus loin |
|
| 2245 | - // stocker la correspondance nouvelles->anciennes colonnes |
|
| 2246 | - $fields_correspondances = array(); |
|
| 2247 | - foreach ($def_origine['field'] as $c => $d) { |
|
| 2248 | - |
|
| 2249 | - if ($colonne_origine && ($c == $colonne_origine)) { |
|
| 2250 | - // si pas DROP |
|
| 2251 | - if ($colonne_destination) { |
|
| 2252 | - $fields[$colonne_destination] = $opt['field'][$colonne_destination]; |
|
| 2253 | - $fields_correspondances[$colonne_destination] = $c; |
|
| 2254 | - } |
|
| 2255 | - } else { |
|
| 2256 | - $fields[$c] = $d; |
|
| 2257 | - $fields_correspondances[$c] = $c; |
|
| 2258 | - } |
|
| 2259 | - } |
|
| 2260 | - // cas de ADD sqlite2 (ajout du champ en fin de table): |
|
| 2261 | - if (!$colonne_origine && $colonne_destination) { |
|
| 2262 | - $fields[$colonne_destination] = $opt['field'][$colonne_destination]; |
|
| 2263 | - } |
|
| 2264 | - |
|
| 2265 | - // key... |
|
| 2266 | - $keys = array(); |
|
| 2267 | - foreach ($def_origine['key'] as $c => $d) { |
|
| 2268 | - $c = str_replace($colonne_origine, $colonne_destination, $c); |
|
| 2269 | - $d = str_replace($colonne_origine, $colonne_destination, $d); |
|
| 2270 | - // seulement si on ne supprime pas la colonne ! |
|
| 2271 | - if ($d) { |
|
| 2272 | - $keys[$c] = $d; |
|
| 2273 | - } |
|
| 2274 | - } |
|
| 2275 | - |
|
| 2276 | - // autres keys, on merge |
|
| 2277 | - $keys = array_merge($keys, $opt['key']); |
|
| 2278 | - $queries = array(); |
|
| 2279 | - |
|
| 2280 | - // copier dans destination (si differente de origine), sinon tmp |
|
| 2281 | - $table_copie = ($meme_table) ? $table_tmp : $table_destination; |
|
| 2282 | - $autoinc = (isset($keys['PRIMARY KEY']) |
|
| 2283 | - and $keys['PRIMARY KEY'] |
|
| 2284 | - and stripos($keys['PRIMARY KEY'], ',') === false |
|
| 2285 | - and stripos($fields[$keys['PRIMARY KEY']], 'default') === false); |
|
| 2286 | - |
|
| 2287 | - if ($q = _sqlite_requete_create( |
|
| 2288 | - $table_copie, |
|
| 2289 | - $fields, |
|
| 2290 | - $keys, |
|
| 2291 | - $autoinc, |
|
| 2292 | - $temporary = false, |
|
| 2293 | - $ifnotexists = true, |
|
| 2294 | - $serveur) |
|
| 2295 | - ) { |
|
| 2296 | - $queries[] = $q; |
|
| 2297 | - } |
|
| 2298 | - |
|
| 2299 | - |
|
| 2300 | - // 2) y copier les champs qui vont bien |
|
| 2301 | - $champs_dest = join(', ', array_keys($fields_correspondances)); |
|
| 2302 | - $champs_ori = join(', ', $fields_correspondances); |
|
| 2303 | - $queries[] = "INSERT INTO $table_copie ($champs_dest) SELECT $champs_ori FROM $table_origine"; |
|
| 2304 | - |
|
| 2305 | - // 3) supprimer la table d'origine |
|
| 2306 | - $queries[] = "DROP TABLE $table_origine"; |
|
| 2307 | - |
|
| 2308 | - // 4) renommer la table temporaire |
|
| 2309 | - // avec le nom de la table destination |
|
| 2310 | - // si necessaire |
|
| 2311 | - if ($meme_table) { |
|
| 2312 | - $queries[] = "ALTER TABLE $table_copie RENAME TO $table_destination"; |
|
| 2313 | - } |
|
| 2314 | - |
|
| 2315 | - // 5) remettre les index ! |
|
| 2316 | - foreach ($keys as $k => $v) { |
|
| 2317 | - if ($k == 'PRIMARY KEY') { |
|
| 2318 | - } else { |
|
| 2319 | - // enlever KEY |
|
| 2320 | - $k = substr($k, 4); |
|
| 2321 | - $queries[] = "CREATE INDEX $table_destination" . "_$k ON $table_destination ($v)"; |
|
| 2322 | - } |
|
| 2323 | - } |
|
| 2324 | - |
|
| 2325 | - |
|
| 2326 | - if (count($queries)) { |
|
| 2327 | - spip_sqlite::demarrer_transaction($serveur); |
|
| 2328 | - // il faut les faire une par une car $query = join('; ', $queries).";"; ne fonctionne pas |
|
| 2329 | - foreach ($queries as $q) { |
|
| 2330 | - if (!spip_sqlite::executer_requete($q, $serveur)) { |
|
| 2331 | - spip_log(_LOG_GRAVITE_ERREUR, "SQLite : ALTER TABLE table :" |
|
| 2332 | - . " Erreur a l'execution de la requete : $q", 'sqlite'); |
|
| 2333 | - spip_sqlite::annuler_transaction($serveur); |
|
| 2334 | - |
|
| 2335 | - return false; |
|
| 2336 | - } |
|
| 2337 | - } |
|
| 2338 | - spip_sqlite::finir_transaction($serveur); |
|
| 2339 | - } |
|
| 2340 | - |
|
| 2341 | - return true; |
|
| 2202 | + if (is_array($table)) { |
|
| 2203 | + $table_destination = reset($table); |
|
| 2204 | + $table_origine = key($table); |
|
| 2205 | + } else { |
|
| 2206 | + $table_origine = $table_destination = $table; |
|
| 2207 | + } |
|
| 2208 | + // ne prend actuellement qu'un changement |
|
| 2209 | + // mais pourra etre adapte pour changer plus qu'une colonne a la fois |
|
| 2210 | + if (is_array($colonne)) { |
|
| 2211 | + $colonne_destination = reset($colonne); |
|
| 2212 | + $colonne_origine = key($colonne); |
|
| 2213 | + } else { |
|
| 2214 | + $colonne_origine = $colonne_destination = $colonne; |
|
| 2215 | + } |
|
| 2216 | + if (!isset($opt['field'])) { |
|
| 2217 | + $opt['field'] = array(); |
|
| 2218 | + } |
|
| 2219 | + if (!isset($opt['key'])) { |
|
| 2220 | + $opt['key'] = array(); |
|
| 2221 | + } |
|
| 2222 | + |
|
| 2223 | + // si les noms de tables sont differents, pas besoin de table temporaire |
|
| 2224 | + // on prendra directement le nom de la future table |
|
| 2225 | + $meme_table = ($table_origine == $table_destination); |
|
| 2226 | + |
|
| 2227 | + $def_origine = sql_showtable($table_origine, false, $serveur); |
|
| 2228 | + if (!$def_origine or !isset($def_origine['field'])) { |
|
| 2229 | + spip_log("Alter table impossible sur $table_origine : table non trouvee", 'sqlite' . _LOG_ERREUR); |
|
| 2230 | + |
|
| 2231 | + return false; |
|
| 2232 | + } |
|
| 2233 | + |
|
| 2234 | + |
|
| 2235 | + $table_tmp = $table_origine . '_tmp'; |
|
| 2236 | + |
|
| 2237 | + // 1) creer une table temporaire avec les modifications |
|
| 2238 | + // - DROP : suppression de la colonne |
|
| 2239 | + // - CHANGE : modification de la colonne |
|
| 2240 | + // (foreach pour conserver l'ordre des champs) |
|
| 2241 | + |
|
| 2242 | + // field |
|
| 2243 | + $fields = array(); |
|
| 2244 | + // pour le INSERT INTO plus loin |
|
| 2245 | + // stocker la correspondance nouvelles->anciennes colonnes |
|
| 2246 | + $fields_correspondances = array(); |
|
| 2247 | + foreach ($def_origine['field'] as $c => $d) { |
|
| 2248 | + |
|
| 2249 | + if ($colonne_origine && ($c == $colonne_origine)) { |
|
| 2250 | + // si pas DROP |
|
| 2251 | + if ($colonne_destination) { |
|
| 2252 | + $fields[$colonne_destination] = $opt['field'][$colonne_destination]; |
|
| 2253 | + $fields_correspondances[$colonne_destination] = $c; |
|
| 2254 | + } |
|
| 2255 | + } else { |
|
| 2256 | + $fields[$c] = $d; |
|
| 2257 | + $fields_correspondances[$c] = $c; |
|
| 2258 | + } |
|
| 2259 | + } |
|
| 2260 | + // cas de ADD sqlite2 (ajout du champ en fin de table): |
|
| 2261 | + if (!$colonne_origine && $colonne_destination) { |
|
| 2262 | + $fields[$colonne_destination] = $opt['field'][$colonne_destination]; |
|
| 2263 | + } |
|
| 2264 | + |
|
| 2265 | + // key... |
|
| 2266 | + $keys = array(); |
|
| 2267 | + foreach ($def_origine['key'] as $c => $d) { |
|
| 2268 | + $c = str_replace($colonne_origine, $colonne_destination, $c); |
|
| 2269 | + $d = str_replace($colonne_origine, $colonne_destination, $d); |
|
| 2270 | + // seulement si on ne supprime pas la colonne ! |
|
| 2271 | + if ($d) { |
|
| 2272 | + $keys[$c] = $d; |
|
| 2273 | + } |
|
| 2274 | + } |
|
| 2275 | + |
|
| 2276 | + // autres keys, on merge |
|
| 2277 | + $keys = array_merge($keys, $opt['key']); |
|
| 2278 | + $queries = array(); |
|
| 2279 | + |
|
| 2280 | + // copier dans destination (si differente de origine), sinon tmp |
|
| 2281 | + $table_copie = ($meme_table) ? $table_tmp : $table_destination; |
|
| 2282 | + $autoinc = (isset($keys['PRIMARY KEY']) |
|
| 2283 | + and $keys['PRIMARY KEY'] |
|
| 2284 | + and stripos($keys['PRIMARY KEY'], ',') === false |
|
| 2285 | + and stripos($fields[$keys['PRIMARY KEY']], 'default') === false); |
|
| 2286 | + |
|
| 2287 | + if ($q = _sqlite_requete_create( |
|
| 2288 | + $table_copie, |
|
| 2289 | + $fields, |
|
| 2290 | + $keys, |
|
| 2291 | + $autoinc, |
|
| 2292 | + $temporary = false, |
|
| 2293 | + $ifnotexists = true, |
|
| 2294 | + $serveur) |
|
| 2295 | + ) { |
|
| 2296 | + $queries[] = $q; |
|
| 2297 | + } |
|
| 2298 | + |
|
| 2299 | + |
|
| 2300 | + // 2) y copier les champs qui vont bien |
|
| 2301 | + $champs_dest = join(', ', array_keys($fields_correspondances)); |
|
| 2302 | + $champs_ori = join(', ', $fields_correspondances); |
|
| 2303 | + $queries[] = "INSERT INTO $table_copie ($champs_dest) SELECT $champs_ori FROM $table_origine"; |
|
| 2304 | + |
|
| 2305 | + // 3) supprimer la table d'origine |
|
| 2306 | + $queries[] = "DROP TABLE $table_origine"; |
|
| 2307 | + |
|
| 2308 | + // 4) renommer la table temporaire |
|
| 2309 | + // avec le nom de la table destination |
|
| 2310 | + // si necessaire |
|
| 2311 | + if ($meme_table) { |
|
| 2312 | + $queries[] = "ALTER TABLE $table_copie RENAME TO $table_destination"; |
|
| 2313 | + } |
|
| 2314 | + |
|
| 2315 | + // 5) remettre les index ! |
|
| 2316 | + foreach ($keys as $k => $v) { |
|
| 2317 | + if ($k == 'PRIMARY KEY') { |
|
| 2318 | + } else { |
|
| 2319 | + // enlever KEY |
|
| 2320 | + $k = substr($k, 4); |
|
| 2321 | + $queries[] = "CREATE INDEX $table_destination" . "_$k ON $table_destination ($v)"; |
|
| 2322 | + } |
|
| 2323 | + } |
|
| 2324 | + |
|
| 2325 | + |
|
| 2326 | + if (count($queries)) { |
|
| 2327 | + spip_sqlite::demarrer_transaction($serveur); |
|
| 2328 | + // il faut les faire une par une car $query = join('; ', $queries).";"; ne fonctionne pas |
|
| 2329 | + foreach ($queries as $q) { |
|
| 2330 | + if (!spip_sqlite::executer_requete($q, $serveur)) { |
|
| 2331 | + spip_log(_LOG_GRAVITE_ERREUR, "SQLite : ALTER TABLE table :" |
|
| 2332 | + . " Erreur a l'execution de la requete : $q", 'sqlite'); |
|
| 2333 | + spip_sqlite::annuler_transaction($serveur); |
|
| 2334 | + |
|
| 2335 | + return false; |
|
| 2336 | + } |
|
| 2337 | + } |
|
| 2338 | + spip_sqlite::finir_transaction($serveur); |
|
| 2339 | + } |
|
| 2340 | + |
|
| 2341 | + return true; |
|
| 2342 | 2342 | } |
| 2343 | 2343 | |
| 2344 | 2344 | |
@@ -2348,60 +2348,60 @@ discard block |
||
| 2348 | 2348 | * @return array |
| 2349 | 2349 | */ |
| 2350 | 2350 | function _sqlite_ref_fonctions() { |
| 2351 | - $fonctions = array( |
|
| 2352 | - 'alter' => 'spip_sqlite_alter', |
|
| 2353 | - 'count' => 'spip_sqlite_count', |
|
| 2354 | - 'countsel' => 'spip_sqlite_countsel', |
|
| 2355 | - 'create' => 'spip_sqlite_create', |
|
| 2356 | - 'create_base' => 'spip_sqlite_create_base', |
|
| 2357 | - 'create_view' => 'spip_sqlite_create_view', |
|
| 2358 | - 'date_proche' => 'spip_sqlite_date_proche', |
|
| 2359 | - 'delete' => 'spip_sqlite_delete', |
|
| 2360 | - 'drop_table' => 'spip_sqlite_drop_table', |
|
| 2361 | - 'drop_view' => 'spip_sqlite_drop_view', |
|
| 2362 | - 'errno' => 'spip_sqlite_errno', |
|
| 2363 | - 'error' => 'spip_sqlite_error', |
|
| 2364 | - 'explain' => 'spip_sqlite_explain', |
|
| 2365 | - 'fetch' => 'spip_sqlite_fetch', |
|
| 2366 | - 'seek' => 'spip_sqlite_seek', |
|
| 2367 | - 'free' => 'spip_sqlite_free', |
|
| 2368 | - 'hex' => 'spip_sqlite_hex', |
|
| 2369 | - 'in' => 'spip_sqlite_in', |
|
| 2370 | - 'insert' => 'spip_sqlite_insert', |
|
| 2371 | - 'insertq' => 'spip_sqlite_insertq', |
|
| 2372 | - 'insertq_multi' => 'spip_sqlite_insertq_multi', |
|
| 2373 | - 'listdbs' => 'spip_sqlite_listdbs', |
|
| 2374 | - 'multi' => 'spip_sqlite_multi', |
|
| 2375 | - 'optimize' => 'spip_sqlite_optimize', |
|
| 2376 | - 'query' => 'spip_sqlite_query', |
|
| 2377 | - 'quote' => 'spip_sqlite_quote', |
|
| 2378 | - 'repair' => 'spip_sqlite_repair', |
|
| 2379 | - 'replace' => 'spip_sqlite_replace', |
|
| 2380 | - 'replace_multi' => 'spip_sqlite_replace_multi', |
|
| 2381 | - 'select' => 'spip_sqlite_select', |
|
| 2382 | - 'selectdb' => 'spip_sqlite_selectdb', |
|
| 2383 | - 'set_charset' => 'spip_sqlite_set_charset', |
|
| 2384 | - 'get_charset' => 'spip_sqlite_get_charset', |
|
| 2385 | - 'showbase' => 'spip_sqlite_showbase', |
|
| 2386 | - 'showtable' => 'spip_sqlite_showtable', |
|
| 2387 | - 'update' => 'spip_sqlite_update', |
|
| 2388 | - 'updateq' => 'spip_sqlite_updateq', |
|
| 2389 | - 'preferer_transaction' => 'spip_sqlite_preferer_transaction', |
|
| 2390 | - 'demarrer_transaction' => 'spip_sqlite_demarrer_transaction', |
|
| 2391 | - 'terminer_transaction' => 'spip_sqlite_terminer_transaction', |
|
| 2392 | - ); |
|
| 2393 | - |
|
| 2394 | - // association de chaque nom http d'un charset aux couples sqlite |
|
| 2395 | - // SQLite supporte utf-8 et utf-16 uniquement. |
|
| 2396 | - $charsets = array( |
|
| 2397 | - 'utf-8' => array('charset' => 'utf8', 'collation' => 'utf8_general_ci'), |
|
| 2398 | - //'utf-16be'=>array('charset'=>'utf16be','collation'=>'UTF-16BE'),// aucune idee de quoi il faut remplir dans es champs la |
|
| 2399 | - //'utf-16le'=>array('charset'=>'utf16le','collation'=>'UTF-16LE') |
|
| 2400 | - ); |
|
| 2401 | - |
|
| 2402 | - $fonctions['charsets'] = $charsets; |
|
| 2403 | - |
|
| 2404 | - return $fonctions; |
|
| 2351 | + $fonctions = array( |
|
| 2352 | + 'alter' => 'spip_sqlite_alter', |
|
| 2353 | + 'count' => 'spip_sqlite_count', |
|
| 2354 | + 'countsel' => 'spip_sqlite_countsel', |
|
| 2355 | + 'create' => 'spip_sqlite_create', |
|
| 2356 | + 'create_base' => 'spip_sqlite_create_base', |
|
| 2357 | + 'create_view' => 'spip_sqlite_create_view', |
|
| 2358 | + 'date_proche' => 'spip_sqlite_date_proche', |
|
| 2359 | + 'delete' => 'spip_sqlite_delete', |
|
| 2360 | + 'drop_table' => 'spip_sqlite_drop_table', |
|
| 2361 | + 'drop_view' => 'spip_sqlite_drop_view', |
|
| 2362 | + 'errno' => 'spip_sqlite_errno', |
|
| 2363 | + 'error' => 'spip_sqlite_error', |
|
| 2364 | + 'explain' => 'spip_sqlite_explain', |
|
| 2365 | + 'fetch' => 'spip_sqlite_fetch', |
|
| 2366 | + 'seek' => 'spip_sqlite_seek', |
|
| 2367 | + 'free' => 'spip_sqlite_free', |
|
| 2368 | + 'hex' => 'spip_sqlite_hex', |
|
| 2369 | + 'in' => 'spip_sqlite_in', |
|
| 2370 | + 'insert' => 'spip_sqlite_insert', |
|
| 2371 | + 'insertq' => 'spip_sqlite_insertq', |
|
| 2372 | + 'insertq_multi' => 'spip_sqlite_insertq_multi', |
|
| 2373 | + 'listdbs' => 'spip_sqlite_listdbs', |
|
| 2374 | + 'multi' => 'spip_sqlite_multi', |
|
| 2375 | + 'optimize' => 'spip_sqlite_optimize', |
|
| 2376 | + 'query' => 'spip_sqlite_query', |
|
| 2377 | + 'quote' => 'spip_sqlite_quote', |
|
| 2378 | + 'repair' => 'spip_sqlite_repair', |
|
| 2379 | + 'replace' => 'spip_sqlite_replace', |
|
| 2380 | + 'replace_multi' => 'spip_sqlite_replace_multi', |
|
| 2381 | + 'select' => 'spip_sqlite_select', |
|
| 2382 | + 'selectdb' => 'spip_sqlite_selectdb', |
|
| 2383 | + 'set_charset' => 'spip_sqlite_set_charset', |
|
| 2384 | + 'get_charset' => 'spip_sqlite_get_charset', |
|
| 2385 | + 'showbase' => 'spip_sqlite_showbase', |
|
| 2386 | + 'showtable' => 'spip_sqlite_showtable', |
|
| 2387 | + 'update' => 'spip_sqlite_update', |
|
| 2388 | + 'updateq' => 'spip_sqlite_updateq', |
|
| 2389 | + 'preferer_transaction' => 'spip_sqlite_preferer_transaction', |
|
| 2390 | + 'demarrer_transaction' => 'spip_sqlite_demarrer_transaction', |
|
| 2391 | + 'terminer_transaction' => 'spip_sqlite_terminer_transaction', |
|
| 2392 | + ); |
|
| 2393 | + |
|
| 2394 | + // association de chaque nom http d'un charset aux couples sqlite |
|
| 2395 | + // SQLite supporte utf-8 et utf-16 uniquement. |
|
| 2396 | + $charsets = array( |
|
| 2397 | + 'utf-8' => array('charset' => 'utf8', 'collation' => 'utf8_general_ci'), |
|
| 2398 | + //'utf-16be'=>array('charset'=>'utf16be','collation'=>'UTF-16BE'),// aucune idee de quoi il faut remplir dans es champs la |
|
| 2399 | + //'utf-16le'=>array('charset'=>'utf16le','collation'=>'UTF-16LE') |
|
| 2400 | + ); |
|
| 2401 | + |
|
| 2402 | + $fonctions['charsets'] = $charsets; |
|
| 2403 | + |
|
| 2404 | + return $fonctions; |
|
| 2405 | 2405 | } |
| 2406 | 2406 | |
| 2407 | 2407 | |
@@ -2413,55 +2413,55 @@ discard block |
||
| 2413 | 2413 | * @return mixed |
| 2414 | 2414 | */ |
| 2415 | 2415 | function _sqlite_remplacements_definitions_table($query, $autoinc = false) { |
| 2416 | - // quelques remplacements |
|
| 2417 | - $num = "(\s*\([0-9]*\))?"; |
|
| 2418 | - $enum = "(\s*\([^\)]*\))?"; |
|
| 2419 | - |
|
| 2420 | - $remplace = array( |
|
| 2421 | - '/enum' . $enum . '/is' => 'VARCHAR(255)', |
|
| 2422 | - '/COLLATE \w+_bin/is' => 'COLLATE BINARY', |
|
| 2423 | - '/COLLATE \w+_ci/is' => 'COLLATE NOCASE', |
|
| 2424 | - '/auto_increment/is' => '', |
|
| 2425 | - '/(timestamp .* )ON .*$/is' => '\\1', |
|
| 2426 | - '/character set \w+/is' => '', |
|
| 2427 | - '/((big|small|medium|tiny)?int(eger)?)' . $num . '\s*unsigned/is' => '\\1 UNSIGNED', |
|
| 2428 | - '/(text\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
|
| 2429 | - '/((char|varchar)' . $num . '\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
|
| 2430 | - '/(datetime\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00 00:00:00'", |
|
| 2431 | - '/(date\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00'", |
|
| 2432 | - ); |
|
| 2433 | - |
|
| 2434 | - // pour l'autoincrement, il faut des INTEGER NOT NULL PRIMARY KEY |
|
| 2435 | - $remplace_autocinc = array( |
|
| 2436 | - '/(big|small|medium|tiny)?int(eger)?' . $num . '/is' => 'INTEGER' |
|
| 2437 | - ); |
|
| 2438 | - // pour les int non autoincrement, il faut un DEFAULT |
|
| 2439 | - $remplace_nonautocinc = array( |
|
| 2440 | - '/((big|small|medium|tiny)?int(eger)?' . $num . '\s+not\s+null)\s*$/is' => "\\1 DEFAULT 0", |
|
| 2441 | - ); |
|
| 2442 | - |
|
| 2443 | - if (is_string($query)) { |
|
| 2444 | - $query = preg_replace(array_keys($remplace), $remplace, $query); |
|
| 2445 | - if ($autoinc or preg_match(',AUTO_INCREMENT,is', $query)) { |
|
| 2446 | - $query = preg_replace(array_keys($remplace_autocinc), $remplace_autocinc, $query); |
|
| 2447 | - } else { |
|
| 2448 | - $query = preg_replace(array_keys($remplace_nonautocinc), $remplace_nonautocinc, $query); |
|
| 2449 | - $query = _sqlite_collate_ci($query); |
|
| 2450 | - } |
|
| 2451 | - } elseif (is_array($query)) { |
|
| 2452 | - foreach ($query as $k => $q) { |
|
| 2453 | - $ai = ($autoinc ? $k == $autoinc : preg_match(',AUTO_INCREMENT,is', $q)); |
|
| 2454 | - $query[$k] = preg_replace(array_keys($remplace), $remplace, $query[$k]); |
|
| 2455 | - if ($ai) { |
|
| 2456 | - $query[$k] = preg_replace(array_keys($remplace_autocinc), $remplace_autocinc, $query[$k]); |
|
| 2457 | - } else { |
|
| 2458 | - $query[$k] = preg_replace(array_keys($remplace_nonautocinc), $remplace_nonautocinc, $query[$k]); |
|
| 2459 | - $query[$k] = _sqlite_collate_ci($query[$k]); |
|
| 2460 | - } |
|
| 2461 | - } |
|
| 2462 | - } |
|
| 2463 | - |
|
| 2464 | - return $query; |
|
| 2416 | + // quelques remplacements |
|
| 2417 | + $num = "(\s*\([0-9]*\))?"; |
|
| 2418 | + $enum = "(\s*\([^\)]*\))?"; |
|
| 2419 | + |
|
| 2420 | + $remplace = array( |
|
| 2421 | + '/enum' . $enum . '/is' => 'VARCHAR(255)', |
|
| 2422 | + '/COLLATE \w+_bin/is' => 'COLLATE BINARY', |
|
| 2423 | + '/COLLATE \w+_ci/is' => 'COLLATE NOCASE', |
|
| 2424 | + '/auto_increment/is' => '', |
|
| 2425 | + '/(timestamp .* )ON .*$/is' => '\\1', |
|
| 2426 | + '/character set \w+/is' => '', |
|
| 2427 | + '/((big|small|medium|tiny)?int(eger)?)' . $num . '\s*unsigned/is' => '\\1 UNSIGNED', |
|
| 2428 | + '/(text\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
|
| 2429 | + '/((char|varchar)' . $num . '\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
|
| 2430 | + '/(datetime\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00 00:00:00'", |
|
| 2431 | + '/(date\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00'", |
|
| 2432 | + ); |
|
| 2433 | + |
|
| 2434 | + // pour l'autoincrement, il faut des INTEGER NOT NULL PRIMARY KEY |
|
| 2435 | + $remplace_autocinc = array( |
|
| 2436 | + '/(big|small|medium|tiny)?int(eger)?' . $num . '/is' => 'INTEGER' |
|
| 2437 | + ); |
|
| 2438 | + // pour les int non autoincrement, il faut un DEFAULT |
|
| 2439 | + $remplace_nonautocinc = array( |
|
| 2440 | + '/((big|small|medium|tiny)?int(eger)?' . $num . '\s+not\s+null)\s*$/is' => "\\1 DEFAULT 0", |
|
| 2441 | + ); |
|
| 2442 | + |
|
| 2443 | + if (is_string($query)) { |
|
| 2444 | + $query = preg_replace(array_keys($remplace), $remplace, $query); |
|
| 2445 | + if ($autoinc or preg_match(',AUTO_INCREMENT,is', $query)) { |
|
| 2446 | + $query = preg_replace(array_keys($remplace_autocinc), $remplace_autocinc, $query); |
|
| 2447 | + } else { |
|
| 2448 | + $query = preg_replace(array_keys($remplace_nonautocinc), $remplace_nonautocinc, $query); |
|
| 2449 | + $query = _sqlite_collate_ci($query); |
|
| 2450 | + } |
|
| 2451 | + } elseif (is_array($query)) { |
|
| 2452 | + foreach ($query as $k => $q) { |
|
| 2453 | + $ai = ($autoinc ? $k == $autoinc : preg_match(',AUTO_INCREMENT,is', $q)); |
|
| 2454 | + $query[$k] = preg_replace(array_keys($remplace), $remplace, $query[$k]); |
|
| 2455 | + if ($ai) { |
|
| 2456 | + $query[$k] = preg_replace(array_keys($remplace_autocinc), $remplace_autocinc, $query[$k]); |
|
| 2457 | + } else { |
|
| 2458 | + $query[$k] = preg_replace(array_keys($remplace_nonautocinc), $remplace_nonautocinc, $query[$k]); |
|
| 2459 | + $query[$k] = _sqlite_collate_ci($query[$k]); |
|
| 2460 | + } |
|
| 2461 | + } |
|
| 2462 | + } |
|
| 2463 | + |
|
| 2464 | + return $query; |
|
| 2465 | 2465 | } |
| 2466 | 2466 | |
| 2467 | 2467 | /** |
@@ -2472,17 +2472,17 @@ discard block |
||
| 2472 | 2472 | * @return string |
| 2473 | 2473 | */ |
| 2474 | 2474 | function _sqlite_collate_ci($champ) { |
| 2475 | - if (stripos($champ, "COLLATE") !== false) { |
|
| 2476 | - return $champ; |
|
| 2477 | - } |
|
| 2478 | - if (stripos($champ, "BINARY") !== false) { |
|
| 2479 | - return str_ireplace("BINARY", "COLLATE BINARY", $champ); |
|
| 2480 | - } |
|
| 2481 | - if (preg_match(",^(char|varchar|(long|small|medium|tiny)?text),i", $champ)) { |
|
| 2482 | - return $champ . " COLLATE NOCASE"; |
|
| 2483 | - } |
|
| 2475 | + if (stripos($champ, "COLLATE") !== false) { |
|
| 2476 | + return $champ; |
|
| 2477 | + } |
|
| 2478 | + if (stripos($champ, "BINARY") !== false) { |
|
| 2479 | + return str_ireplace("BINARY", "COLLATE BINARY", $champ); |
|
| 2480 | + } |
|
| 2481 | + if (preg_match(",^(char|varchar|(long|small|medium|tiny)?text),i", $champ)) { |
|
| 2482 | + return $champ . " COLLATE NOCASE"; |
|
| 2483 | + } |
|
| 2484 | 2484 | |
| 2485 | - return $champ; |
|
| 2485 | + return $champ; |
|
| 2486 | 2486 | } |
| 2487 | 2487 | |
| 2488 | 2488 | |
@@ -2501,82 +2501,82 @@ discard block |
||
| 2501 | 2501 | * @return bool|string |
| 2502 | 2502 | */ |
| 2503 | 2503 | function _sqlite_requete_create( |
| 2504 | - $nom, |
|
| 2505 | - $champs, |
|
| 2506 | - $cles, |
|
| 2507 | - $autoinc = false, |
|
| 2508 | - $temporary = false, |
|
| 2509 | - $_ifnotexists = true, |
|
| 2510 | - $serveur = '', |
|
| 2511 | - $requeter = true |
|
| 2504 | + $nom, |
|
| 2505 | + $champs, |
|
| 2506 | + $cles, |
|
| 2507 | + $autoinc = false, |
|
| 2508 | + $temporary = false, |
|
| 2509 | + $_ifnotexists = true, |
|
| 2510 | + $serveur = '', |
|
| 2511 | + $requeter = true |
|
| 2512 | 2512 | ) { |
| 2513 | - $query = $keys = $s = $p = ''; |
|
| 2514 | - |
|
| 2515 | - // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 2516 | - // sans les renseigner (laisse le compilo recuperer la description) |
|
| 2517 | - if (!is_array($champs) || !is_array($cles)) { |
|
| 2518 | - return; |
|
| 2519 | - } |
|
| 2520 | - |
|
| 2521 | - // sqlite ne gere pas KEY tout court dans une requete CREATE TABLE |
|
| 2522 | - // il faut passer par des create index |
|
| 2523 | - // Il gere par contre primary key ! |
|
| 2524 | - // Soit la PK est definie dans les cles, soit dans un champs |
|
| 2525 | - // soit faussement dans les 2 (et dans ce cas, il faut l’enlever à un des 2 endroits !) |
|
| 2526 | - $pk = "PRIMARY KEY"; |
|
| 2527 | - // le champ de cle primaire |
|
| 2528 | - $champ_pk = !empty($cles[$pk]) ? $cles[$pk] : ''; |
|
| 2529 | - |
|
| 2530 | - foreach ($champs as $k => $v) { |
|
| 2531 | - if (false !== stripos($v, $pk)) { |
|
| 2532 | - $champ_pk = $k; |
|
| 2533 | - // on n'en a plus besoin dans field, vu que defini dans key |
|
| 2534 | - $champs[$k] = preg_replace("/$pk/is", '', $champs[$k]); |
|
| 2535 | - break; |
|
| 2536 | - } |
|
| 2537 | - } |
|
| 2538 | - |
|
| 2539 | - if ($champ_pk) { |
|
| 2540 | - $keys = "\n\t\t$pk ($champ_pk)"; |
|
| 2541 | - } |
|
| 2542 | - // Pas de DEFAULT 0 sur les cles primaires en auto-increment |
|
| 2543 | - if (isset($champs[$champ_pk]) |
|
| 2544 | - and stripos($champs[$champ_pk], "default 0") !== false |
|
| 2545 | - ) { |
|
| 2546 | - $champs[$champ_pk] = trim(str_ireplace("default 0", "", $champs[$champ_pk])); |
|
| 2547 | - } |
|
| 2548 | - |
|
| 2549 | - $champs = _sqlite_remplacements_definitions_table($champs, $autoinc ? $champ_pk : false); |
|
| 2550 | - foreach ($champs as $k => $v) { |
|
| 2551 | - $query .= "$s\n\t\t$k $v"; |
|
| 2552 | - $s = ","; |
|
| 2553 | - } |
|
| 2554 | - |
|
| 2555 | - $ifnotexists = ""; |
|
| 2556 | - if ($_ifnotexists) { |
|
| 2557 | - |
|
| 2558 | - $version = spip_sqlite_fetch(spip_sqlite_query("select sqlite_version() AS sqlite_version", $serveur), '', |
|
| 2559 | - $serveur); |
|
| 2560 | - if (!function_exists('spip_version_compare')) { |
|
| 2561 | - include_spip('plugins/installer'); |
|
| 2562 | - } |
|
| 2563 | - |
|
| 2564 | - if ($version and spip_version_compare($version['sqlite_version'], '3.3.0', '>=')) { |
|
| 2565 | - $ifnotexists = ' IF NOT EXISTS'; |
|
| 2566 | - } else { |
|
| 2567 | - /* simuler le IF EXISTS - version 2 et sqlite < 3.3a */ |
|
| 2568 | - $a = spip_sqlite_showtable($nom, $serveur); |
|
| 2569 | - if (isset($a['key']['KEY ' . $nom])) { |
|
| 2570 | - return true; |
|
| 2571 | - } |
|
| 2572 | - } |
|
| 2573 | - |
|
| 2574 | - } |
|
| 2575 | - |
|
| 2576 | - $temporary = $temporary ? ' TEMPORARY' : ''; |
|
| 2577 | - $q = "CREATE$temporary TABLE$ifnotexists $nom ($query" . ($keys ? ",$keys" : '') . ")\n"; |
|
| 2578 | - |
|
| 2579 | - return $q; |
|
| 2513 | + $query = $keys = $s = $p = ''; |
|
| 2514 | + |
|
| 2515 | + // certains plugins declarent les tables (permet leur inclusion dans le dump) |
|
| 2516 | + // sans les renseigner (laisse le compilo recuperer la description) |
|
| 2517 | + if (!is_array($champs) || !is_array($cles)) { |
|
| 2518 | + return; |
|
| 2519 | + } |
|
| 2520 | + |
|
| 2521 | + // sqlite ne gere pas KEY tout court dans une requete CREATE TABLE |
|
| 2522 | + // il faut passer par des create index |
|
| 2523 | + // Il gere par contre primary key ! |
|
| 2524 | + // Soit la PK est definie dans les cles, soit dans un champs |
|
| 2525 | + // soit faussement dans les 2 (et dans ce cas, il faut l’enlever à un des 2 endroits !) |
|
| 2526 | + $pk = "PRIMARY KEY"; |
|
| 2527 | + // le champ de cle primaire |
|
| 2528 | + $champ_pk = !empty($cles[$pk]) ? $cles[$pk] : ''; |
|
| 2529 | + |
|
| 2530 | + foreach ($champs as $k => $v) { |
|
| 2531 | + if (false !== stripos($v, $pk)) { |
|
| 2532 | + $champ_pk = $k; |
|
| 2533 | + // on n'en a plus besoin dans field, vu que defini dans key |
|
| 2534 | + $champs[$k] = preg_replace("/$pk/is", '', $champs[$k]); |
|
| 2535 | + break; |
|
| 2536 | + } |
|
| 2537 | + } |
|
| 2538 | + |
|
| 2539 | + if ($champ_pk) { |
|
| 2540 | + $keys = "\n\t\t$pk ($champ_pk)"; |
|
| 2541 | + } |
|
| 2542 | + // Pas de DEFAULT 0 sur les cles primaires en auto-increment |
|
| 2543 | + if (isset($champs[$champ_pk]) |
|
| 2544 | + and stripos($champs[$champ_pk], "default 0") !== false |
|
| 2545 | + ) { |
|
| 2546 | + $champs[$champ_pk] = trim(str_ireplace("default 0", "", $champs[$champ_pk])); |
|
| 2547 | + } |
|
| 2548 | + |
|
| 2549 | + $champs = _sqlite_remplacements_definitions_table($champs, $autoinc ? $champ_pk : false); |
|
| 2550 | + foreach ($champs as $k => $v) { |
|
| 2551 | + $query .= "$s\n\t\t$k $v"; |
|
| 2552 | + $s = ","; |
|
| 2553 | + } |
|
| 2554 | + |
|
| 2555 | + $ifnotexists = ""; |
|
| 2556 | + if ($_ifnotexists) { |
|
| 2557 | + |
|
| 2558 | + $version = spip_sqlite_fetch(spip_sqlite_query("select sqlite_version() AS sqlite_version", $serveur), '', |
|
| 2559 | + $serveur); |
|
| 2560 | + if (!function_exists('spip_version_compare')) { |
|
| 2561 | + include_spip('plugins/installer'); |
|
| 2562 | + } |
|
| 2563 | + |
|
| 2564 | + if ($version and spip_version_compare($version['sqlite_version'], '3.3.0', '>=')) { |
|
| 2565 | + $ifnotexists = ' IF NOT EXISTS'; |
|
| 2566 | + } else { |
|
| 2567 | + /* simuler le IF EXISTS - version 2 et sqlite < 3.3a */ |
|
| 2568 | + $a = spip_sqlite_showtable($nom, $serveur); |
|
| 2569 | + if (isset($a['key']['KEY ' . $nom])) { |
|
| 2570 | + return true; |
|
| 2571 | + } |
|
| 2572 | + } |
|
| 2573 | + |
|
| 2574 | + } |
|
| 2575 | + |
|
| 2576 | + $temporary = $temporary ? ' TEMPORARY' : ''; |
|
| 2577 | + $q = "CREATE$temporary TABLE$ifnotexists $nom ($query" . ($keys ? ",$keys" : '') . ")\n"; |
|
| 2578 | + |
|
| 2579 | + return $q; |
|
| 2580 | 2580 | } |
| 2581 | 2581 | |
| 2582 | 2582 | |
@@ -2595,34 +2595,34 @@ discard block |
||
| 2595 | 2595 | * @return |
| 2596 | 2596 | */ |
| 2597 | 2597 | function _sqlite_ajouter_champs_timestamp($table, $couples, $desc = '', $serveur = '') { |
| 2598 | - static $tables = array(); |
|
| 2598 | + static $tables = array(); |
|
| 2599 | 2599 | |
| 2600 | - if (!isset($tables[$table])) { |
|
| 2600 | + if (!isset($tables[$table])) { |
|
| 2601 | 2601 | |
| 2602 | - if (!$desc) { |
|
| 2603 | - $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 2604 | - $desc = $trouver_table($table, $serveur); |
|
| 2605 | - // si pas de description, on ne fait rien, ou on die() ? |
|
| 2606 | - if (!$desc) { |
|
| 2607 | - return $couples; |
|
| 2608 | - } |
|
| 2609 | - } |
|
| 2602 | + if (!$desc) { |
|
| 2603 | + $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 2604 | + $desc = $trouver_table($table, $serveur); |
|
| 2605 | + // si pas de description, on ne fait rien, ou on die() ? |
|
| 2606 | + if (!$desc) { |
|
| 2607 | + return $couples; |
|
| 2608 | + } |
|
| 2609 | + } |
|
| 2610 | 2610 | |
| 2611 | - // recherche des champs avec simplement 'TIMESTAMP' |
|
| 2612 | - // cependant, il faudra peut etre etendre |
|
| 2613 | - // avec la gestion de DEFAULT et ON UPDATE |
|
| 2614 | - // mais ceux-ci ne sont pas utilises dans le core |
|
| 2615 | - $tables[$table] = array(); |
|
| 2611 | + // recherche des champs avec simplement 'TIMESTAMP' |
|
| 2612 | + // cependant, il faudra peut etre etendre |
|
| 2613 | + // avec la gestion de DEFAULT et ON UPDATE |
|
| 2614 | + // mais ceux-ci ne sont pas utilises dans le core |
|
| 2615 | + $tables[$table] = array(); |
|
| 2616 | 2616 | |
| 2617 | - foreach ($desc['field'] as $k => $v) { |
|
| 2618 | - if (strpos(strtolower(ltrim($v)), 'timestamp') === 0) { |
|
| 2619 | - $tables[$table][$k] = "datetime('now')"; |
|
| 2620 | - } |
|
| 2621 | - } |
|
| 2622 | - } |
|
| 2617 | + foreach ($desc['field'] as $k => $v) { |
|
| 2618 | + if (strpos(strtolower(ltrim($v)), 'timestamp') === 0) { |
|
| 2619 | + $tables[$table][$k] = "datetime('now')"; |
|
| 2620 | + } |
|
| 2621 | + } |
|
| 2622 | + } |
|
| 2623 | 2623 | |
| 2624 | - // ajout des champs type 'timestamp' absents |
|
| 2625 | - return array_merge($tables[$table], $couples); |
|
| 2624 | + // ajout des champs type 'timestamp' absents |
|
| 2625 | + return array_merge($tables[$table], $couples); |
|
| 2626 | 2626 | } |
| 2627 | 2627 | |
| 2628 | 2628 | |
@@ -2633,7 +2633,7 @@ discard block |
||
| 2633 | 2633 | * @return array|bool |
| 2634 | 2634 | */ |
| 2635 | 2635 | function spip_versions_sqlite() { |
| 2636 | - return _sqlite_charger_version(); |
|
| 2636 | + return _sqlite_charger_version(); |
|
| 2637 | 2637 | } |
| 2638 | 2638 | |
| 2639 | 2639 | /** |
@@ -2641,113 +2641,113 @@ discard block |
||
| 2641 | 2641 | * encadrées de transactions. |
| 2642 | 2642 | **/ |
| 2643 | 2643 | class spip_sqlite { |
| 2644 | - /** @var sqlite_requeteur[] Liste des instances de requêteurs créés */ |
|
| 2645 | - public static $requeteurs = array(); |
|
| 2646 | - /** @var bool[] Pour chaque connexion, flag pour savoir si une transaction est en cours */ |
|
| 2647 | - public static $transaction_en_cours = array(); |
|
| 2648 | - |
|
| 2649 | - |
|
| 2650 | - /** |
|
| 2651 | - * Retourne une unique instance du requêteur |
|
| 2652 | - * |
|
| 2653 | - * Retourne une instance unique du requêteur pour une connexion SQLite |
|
| 2654 | - * donnée |
|
| 2655 | - * |
|
| 2656 | - * @param string $serveur |
|
| 2657 | - * Nom du connecteur |
|
| 2658 | - * @return sqlite_requeteur |
|
| 2659 | - * Instance unique du requêteur |
|
| 2660 | - **/ |
|
| 2661 | - public static function requeteur($serveur) { |
|
| 2662 | - if (!isset(spip_sqlite::$requeteurs[$serveur])) { |
|
| 2663 | - spip_sqlite::$requeteurs[$serveur] = new sqlite_requeteur($serveur); |
|
| 2664 | - } |
|
| 2665 | - |
|
| 2666 | - return spip_sqlite::$requeteurs[$serveur]; |
|
| 2667 | - } |
|
| 2668 | - |
|
| 2669 | - /** |
|
| 2670 | - * Prépare le texte d'une requête avant son exécution |
|
| 2671 | - * |
|
| 2672 | - * Adapte la requête au format plus ou moins MySQL par un format |
|
| 2673 | - * compris de SQLite. |
|
| 2674 | - * |
|
| 2675 | - * Change les préfixes de tables SPIP par ceux véritables |
|
| 2676 | - * |
|
| 2677 | - * @param string $query Requête à préparer |
|
| 2678 | - * @param string $serveur Nom de la connexion |
|
| 2679 | - * @return string Requête préparée |
|
| 2680 | - */ |
|
| 2681 | - public static function traduire_requete($query, $serveur) { |
|
| 2682 | - $requeteur = spip_sqlite::requeteur($serveur); |
|
| 2683 | - $traducteur = new sqlite_traducteur($query, $requeteur->prefixe, $requeteur->sqlite_version); |
|
| 2684 | - |
|
| 2685 | - return $traducteur->traduire_requete(); |
|
| 2686 | - } |
|
| 2687 | - |
|
| 2688 | - /** |
|
| 2689 | - * Démarre une transaction |
|
| 2690 | - * |
|
| 2691 | - * @param string $serveur Nom de la connexion |
|
| 2692 | - **/ |
|
| 2693 | - public static function demarrer_transaction($serveur) { |
|
| 2694 | - spip_sqlite::executer_requete("BEGIN TRANSACTION", $serveur); |
|
| 2695 | - spip_sqlite::$transaction_en_cours[$serveur] = true; |
|
| 2696 | - } |
|
| 2697 | - |
|
| 2698 | - /** |
|
| 2699 | - * Exécute la requête donnée |
|
| 2700 | - * |
|
| 2701 | - * @param string $query Requête |
|
| 2702 | - * @param string $serveur Nom de la connexion |
|
| 2703 | - * @param null|bool $tracer Demander des statistiques (temps) ? |
|
| 2704 | - **/ |
|
| 2705 | - public static function executer_requete($query, $serveur, $tracer = null) { |
|
| 2706 | - $requeteur = spip_sqlite::requeteur($serveur); |
|
| 2707 | - |
|
| 2708 | - return $requeteur->executer_requete($query, $tracer); |
|
| 2709 | - } |
|
| 2710 | - |
|
| 2711 | - /** |
|
| 2712 | - * Obtient l'identifiant de la dernière ligne insérée ou modifiée |
|
| 2713 | - * |
|
| 2714 | - * @param string $serveur Nom de la connexion |
|
| 2715 | - * return int Identifiant |
|
| 2716 | - **/ |
|
| 2717 | - public static function last_insert_id($serveur) { |
|
| 2718 | - $requeteur = spip_sqlite::requeteur($serveur); |
|
| 2719 | - |
|
| 2720 | - return $requeteur->last_insert_id($serveur); |
|
| 2721 | - } |
|
| 2722 | - |
|
| 2723 | - /** |
|
| 2724 | - * Annule une transaction |
|
| 2725 | - * |
|
| 2726 | - * @param string $serveur Nom de la connexion |
|
| 2727 | - **/ |
|
| 2728 | - public static function annuler_transaction($serveur) { |
|
| 2729 | - spip_sqlite::executer_requete("ROLLBACK", $serveur); |
|
| 2730 | - spip_sqlite::$transaction_en_cours[$serveur] = false; |
|
| 2731 | - } |
|
| 2732 | - |
|
| 2733 | - /** |
|
| 2734 | - * Termine une transaction |
|
| 2735 | - * |
|
| 2736 | - * @param string $serveur Nom de la connexion |
|
| 2737 | - **/ |
|
| 2738 | - public static function finir_transaction($serveur) { |
|
| 2739 | - // si pas de transaction en cours, ne rien faire et le dire |
|
| 2740 | - if (!isset(spip_sqlite::$transaction_en_cours[$serveur]) |
|
| 2741 | - or spip_sqlite::$transaction_en_cours[$serveur] == false |
|
| 2742 | - ) { |
|
| 2743 | - return false; |
|
| 2744 | - } |
|
| 2745 | - // sinon fermer la transaction et retourner true |
|
| 2746 | - spip_sqlite::executer_requete("COMMIT", $serveur); |
|
| 2747 | - spip_sqlite::$transaction_en_cours[$serveur] = false; |
|
| 2748 | - |
|
| 2749 | - return true; |
|
| 2750 | - } |
|
| 2644 | + /** @var sqlite_requeteur[] Liste des instances de requêteurs créés */ |
|
| 2645 | + public static $requeteurs = array(); |
|
| 2646 | + /** @var bool[] Pour chaque connexion, flag pour savoir si une transaction est en cours */ |
|
| 2647 | + public static $transaction_en_cours = array(); |
|
| 2648 | + |
|
| 2649 | + |
|
| 2650 | + /** |
|
| 2651 | + * Retourne une unique instance du requêteur |
|
| 2652 | + * |
|
| 2653 | + * Retourne une instance unique du requêteur pour une connexion SQLite |
|
| 2654 | + * donnée |
|
| 2655 | + * |
|
| 2656 | + * @param string $serveur |
|
| 2657 | + * Nom du connecteur |
|
| 2658 | + * @return sqlite_requeteur |
|
| 2659 | + * Instance unique du requêteur |
|
| 2660 | + **/ |
|
| 2661 | + public static function requeteur($serveur) { |
|
| 2662 | + if (!isset(spip_sqlite::$requeteurs[$serveur])) { |
|
| 2663 | + spip_sqlite::$requeteurs[$serveur] = new sqlite_requeteur($serveur); |
|
| 2664 | + } |
|
| 2665 | + |
|
| 2666 | + return spip_sqlite::$requeteurs[$serveur]; |
|
| 2667 | + } |
|
| 2668 | + |
|
| 2669 | + /** |
|
| 2670 | + * Prépare le texte d'une requête avant son exécution |
|
| 2671 | + * |
|
| 2672 | + * Adapte la requête au format plus ou moins MySQL par un format |
|
| 2673 | + * compris de SQLite. |
|
| 2674 | + * |
|
| 2675 | + * Change les préfixes de tables SPIP par ceux véritables |
|
| 2676 | + * |
|
| 2677 | + * @param string $query Requête à préparer |
|
| 2678 | + * @param string $serveur Nom de la connexion |
|
| 2679 | + * @return string Requête préparée |
|
| 2680 | + */ |
|
| 2681 | + public static function traduire_requete($query, $serveur) { |
|
| 2682 | + $requeteur = spip_sqlite::requeteur($serveur); |
|
| 2683 | + $traducteur = new sqlite_traducteur($query, $requeteur->prefixe, $requeteur->sqlite_version); |
|
| 2684 | + |
|
| 2685 | + return $traducteur->traduire_requete(); |
|
| 2686 | + } |
|
| 2687 | + |
|
| 2688 | + /** |
|
| 2689 | + * Démarre une transaction |
|
| 2690 | + * |
|
| 2691 | + * @param string $serveur Nom de la connexion |
|
| 2692 | + **/ |
|
| 2693 | + public static function demarrer_transaction($serveur) { |
|
| 2694 | + spip_sqlite::executer_requete("BEGIN TRANSACTION", $serveur); |
|
| 2695 | + spip_sqlite::$transaction_en_cours[$serveur] = true; |
|
| 2696 | + } |
|
| 2697 | + |
|
| 2698 | + /** |
|
| 2699 | + * Exécute la requête donnée |
|
| 2700 | + * |
|
| 2701 | + * @param string $query Requête |
|
| 2702 | + * @param string $serveur Nom de la connexion |
|
| 2703 | + * @param null|bool $tracer Demander des statistiques (temps) ? |
|
| 2704 | + **/ |
|
| 2705 | + public static function executer_requete($query, $serveur, $tracer = null) { |
|
| 2706 | + $requeteur = spip_sqlite::requeteur($serveur); |
|
| 2707 | + |
|
| 2708 | + return $requeteur->executer_requete($query, $tracer); |
|
| 2709 | + } |
|
| 2710 | + |
|
| 2711 | + /** |
|
| 2712 | + * Obtient l'identifiant de la dernière ligne insérée ou modifiée |
|
| 2713 | + * |
|
| 2714 | + * @param string $serveur Nom de la connexion |
|
| 2715 | + * return int Identifiant |
|
| 2716 | + **/ |
|
| 2717 | + public static function last_insert_id($serveur) { |
|
| 2718 | + $requeteur = spip_sqlite::requeteur($serveur); |
|
| 2719 | + |
|
| 2720 | + return $requeteur->last_insert_id($serveur); |
|
| 2721 | + } |
|
| 2722 | + |
|
| 2723 | + /** |
|
| 2724 | + * Annule une transaction |
|
| 2725 | + * |
|
| 2726 | + * @param string $serveur Nom de la connexion |
|
| 2727 | + **/ |
|
| 2728 | + public static function annuler_transaction($serveur) { |
|
| 2729 | + spip_sqlite::executer_requete("ROLLBACK", $serveur); |
|
| 2730 | + spip_sqlite::$transaction_en_cours[$serveur] = false; |
|
| 2731 | + } |
|
| 2732 | + |
|
| 2733 | + /** |
|
| 2734 | + * Termine une transaction |
|
| 2735 | + * |
|
| 2736 | + * @param string $serveur Nom de la connexion |
|
| 2737 | + **/ |
|
| 2738 | + public static function finir_transaction($serveur) { |
|
| 2739 | + // si pas de transaction en cours, ne rien faire et le dire |
|
| 2740 | + if (!isset(spip_sqlite::$transaction_en_cours[$serveur]) |
|
| 2741 | + or spip_sqlite::$transaction_en_cours[$serveur] == false |
|
| 2742 | + ) { |
|
| 2743 | + return false; |
|
| 2744 | + } |
|
| 2745 | + // sinon fermer la transaction et retourner true |
|
| 2746 | + spip_sqlite::executer_requete("COMMIT", $serveur); |
|
| 2747 | + spip_sqlite::$transaction_en_cours[$serveur] = false; |
|
| 2748 | + |
|
| 2749 | + return true; |
|
| 2750 | + } |
|
| 2751 | 2751 | } |
| 2752 | 2752 | |
| 2753 | 2753 | /* |
@@ -2760,119 +2760,119 @@ discard block |
||
| 2760 | 2760 | */ |
| 2761 | 2761 | |
| 2762 | 2762 | class sqlite_requeteur { |
| 2763 | - /** @var string Texte de la requête */ |
|
| 2764 | - public $query = ''; // la requete |
|
| 2765 | - /** @var string Nom de la connexion */ |
|
| 2766 | - public $serveur = ''; |
|
| 2767 | - /** @var Ressource Identifiant de la connexion SQLite */ |
|
| 2768 | - public $link = ''; |
|
| 2769 | - /** @var string Prefixe des tables SPIP */ |
|
| 2770 | - public $prefixe = ''; |
|
| 2771 | - /** @var string Nom de la base de donnée */ |
|
| 2772 | - public $db = ''; |
|
| 2773 | - /** @var bool Doit-on tracer les requetes (var_profile) ? */ |
|
| 2774 | - public $tracer = false; // doit-on tracer les requetes (var_profile) |
|
| 2775 | - |
|
| 2776 | - /** @var string Version de SQLite (2 ou 3) */ |
|
| 2777 | - public $sqlite_version = ''; |
|
| 2778 | - |
|
| 2779 | - /** |
|
| 2780 | - * Constructeur |
|
| 2781 | - * |
|
| 2782 | - * @param string $serveur |
|
| 2783 | - * @return bool |
|
| 2784 | - */ |
|
| 2785 | - public function __construct($serveur = '') { |
|
| 2786 | - _sqlite_init(); |
|
| 2787 | - $this->serveur = strtolower($serveur); |
|
| 2788 | - |
|
| 2789 | - if (!($this->link = _sqlite_link($this->serveur)) && (!defined('_ECRIRE_INSTALL') || !_ECRIRE_INSTALL)) { |
|
| 2790 | - spip_log("Aucune connexion sqlite (link)", 'sqlite.' . _LOG_ERREUR); |
|
| 2791 | - |
|
| 2792 | - return false; |
|
| 2793 | - } |
|
| 2794 | - |
|
| 2795 | - $this->sqlite_version = _sqlite_is_version('', $this->link); |
|
| 2796 | - |
|
| 2797 | - $this->prefixe = $GLOBALS['connexions'][$this->serveur ? $this->serveur : 0]['prefixe']; |
|
| 2798 | - $this->db = $GLOBALS['connexions'][$this->serveur ? $this->serveur : 0]['db']; |
|
| 2799 | - |
|
| 2800 | - // tracage des requetes ? |
|
| 2801 | - $this->tracer = (isset($_GET['var_profile']) && $_GET['var_profile']); |
|
| 2802 | - } |
|
| 2803 | - |
|
| 2804 | - /** |
|
| 2805 | - * Lancer la requête transmise et faire le tracage si demandé |
|
| 2806 | - * |
|
| 2807 | - * @param string $query |
|
| 2808 | - * Requête à exécuter |
|
| 2809 | - * @param bool|null $tracer |
|
| 2810 | - * true pour tracer la requête |
|
| 2811 | - * @return bool|SQLiteResult |
|
| 2812 | - */ |
|
| 2813 | - public function executer_requete($query, $tracer = null) { |
|
| 2814 | - if (is_null($tracer)) { |
|
| 2815 | - $tracer = $this->tracer; |
|
| 2816 | - } |
|
| 2817 | - $err = ""; |
|
| 2818 | - $t = 0; |
|
| 2819 | - if ($tracer) { |
|
| 2820 | - include_spip('public/tracer'); |
|
| 2821 | - $t = trace_query_start(); |
|
| 2822 | - } |
|
| 2823 | - |
|
| 2824 | - # spip_log("requete: $this->serveur >> $query",'sqlite.'._LOG_DEBUG); // boum ? pourquoi ? |
|
| 2825 | - if ($this->link) { |
|
| 2826 | - // memoriser la derniere erreur PHP vue |
|
| 2827 | - $e = (function_exists('error_get_last') ? error_get_last() : ""); |
|
| 2828 | - // sauver la derniere requete |
|
| 2829 | - $GLOBALS['connexions'][$this->serveur ? $this->serveur : 0]['last'] = $query; |
|
| 2830 | - $GLOBALS['connexions'][$this->serveur ? $this->serveur : 0]['total_requetes']++; |
|
| 2831 | - |
|
| 2832 | - $r = $this->link->query($query); |
|
| 2833 | - // sauvegarde de la requete (elle y est deja dans $r->queryString) |
|
| 2834 | - # $r->spipQueryString = $query; |
|
| 2835 | - |
|
| 2836 | - // comptage : oblige de compter le nombre d'entrees retournees |
|
| 2837 | - // par une requete SELECT |
|
| 2838 | - // aucune autre solution ne donne le nombre attendu :( ! |
|
| 2839 | - // particulierement s'il y a des LIMIT dans la requete. |
|
| 2840 | - if (strtoupper(substr(ltrim($query), 0, 6)) == 'SELECT') { |
|
| 2841 | - if ($r) { |
|
| 2842 | - // noter le link et la query pour faire le comptage *si* on en a besoin |
|
| 2843 | - $r->spipSqliteRowCount = array($this->link, $query); |
|
| 2844 | - } elseif ($r instanceof PDOStatement) { |
|
| 2845 | - $r->spipSqliteRowCount = 0; |
|
| 2846 | - } |
|
| 2847 | - } |
|
| 2848 | - |
|
| 2849 | - // loger les warnings/erreurs eventuels de sqlite remontant dans PHP |
|
| 2850 | - if ($err = (function_exists('error_get_last') ? error_get_last() : "") and $err != $e) { |
|
| 2851 | - $err = strip_tags($err['message']) . " in " . $err['file'] . " line " . $err['line']; |
|
| 2852 | - spip_log("$err - " . $query, 'sqlite.' . _LOG_ERREUR); |
|
| 2853 | - } else { |
|
| 2854 | - $err = ""; |
|
| 2855 | - } |
|
| 2856 | - |
|
| 2857 | - } else { |
|
| 2858 | - $r = false; |
|
| 2859 | - } |
|
| 2860 | - |
|
| 2861 | - if (spip_sqlite_errno($this->serveur)) { |
|
| 2862 | - $err .= spip_sqlite_error($query, $this->serveur); |
|
| 2863 | - } |
|
| 2864 | - |
|
| 2865 | - return $t ? trace_query_end($query, $t, $r, $err, $this->serveur) : $r; |
|
| 2866 | - } |
|
| 2867 | - |
|
| 2868 | - /** |
|
| 2869 | - * Obtient l'identifiant de la dernière ligne insérée ou modifiée |
|
| 2870 | - * |
|
| 2871 | - * @return int |
|
| 2872 | - **/ |
|
| 2873 | - public function last_insert_id() { |
|
| 2874 | - return $this->link->lastInsertId(); |
|
| 2875 | - } |
|
| 2763 | + /** @var string Texte de la requête */ |
|
| 2764 | + public $query = ''; // la requete |
|
| 2765 | + /** @var string Nom de la connexion */ |
|
| 2766 | + public $serveur = ''; |
|
| 2767 | + /** @var Ressource Identifiant de la connexion SQLite */ |
|
| 2768 | + public $link = ''; |
|
| 2769 | + /** @var string Prefixe des tables SPIP */ |
|
| 2770 | + public $prefixe = ''; |
|
| 2771 | + /** @var string Nom de la base de donnée */ |
|
| 2772 | + public $db = ''; |
|
| 2773 | + /** @var bool Doit-on tracer les requetes (var_profile) ? */ |
|
| 2774 | + public $tracer = false; // doit-on tracer les requetes (var_profile) |
|
| 2775 | + |
|
| 2776 | + /** @var string Version de SQLite (2 ou 3) */ |
|
| 2777 | + public $sqlite_version = ''; |
|
| 2778 | + |
|
| 2779 | + /** |
|
| 2780 | + * Constructeur |
|
| 2781 | + * |
|
| 2782 | + * @param string $serveur |
|
| 2783 | + * @return bool |
|
| 2784 | + */ |
|
| 2785 | + public function __construct($serveur = '') { |
|
| 2786 | + _sqlite_init(); |
|
| 2787 | + $this->serveur = strtolower($serveur); |
|
| 2788 | + |
|
| 2789 | + if (!($this->link = _sqlite_link($this->serveur)) && (!defined('_ECRIRE_INSTALL') || !_ECRIRE_INSTALL)) { |
|
| 2790 | + spip_log("Aucune connexion sqlite (link)", 'sqlite.' . _LOG_ERREUR); |
|
| 2791 | + |
|
| 2792 | + return false; |
|
| 2793 | + } |
|
| 2794 | + |
|
| 2795 | + $this->sqlite_version = _sqlite_is_version('', $this->link); |
|
| 2796 | + |
|
| 2797 | + $this->prefixe = $GLOBALS['connexions'][$this->serveur ? $this->serveur : 0]['prefixe']; |
|
| 2798 | + $this->db = $GLOBALS['connexions'][$this->serveur ? $this->serveur : 0]['db']; |
|
| 2799 | + |
|
| 2800 | + // tracage des requetes ? |
|
| 2801 | + $this->tracer = (isset($_GET['var_profile']) && $_GET['var_profile']); |
|
| 2802 | + } |
|
| 2803 | + |
|
| 2804 | + /** |
|
| 2805 | + * Lancer la requête transmise et faire le tracage si demandé |
|
| 2806 | + * |
|
| 2807 | + * @param string $query |
|
| 2808 | + * Requête à exécuter |
|
| 2809 | + * @param bool|null $tracer |
|
| 2810 | + * true pour tracer la requête |
|
| 2811 | + * @return bool|SQLiteResult |
|
| 2812 | + */ |
|
| 2813 | + public function executer_requete($query, $tracer = null) { |
|
| 2814 | + if (is_null($tracer)) { |
|
| 2815 | + $tracer = $this->tracer; |
|
| 2816 | + } |
|
| 2817 | + $err = ""; |
|
| 2818 | + $t = 0; |
|
| 2819 | + if ($tracer) { |
|
| 2820 | + include_spip('public/tracer'); |
|
| 2821 | + $t = trace_query_start(); |
|
| 2822 | + } |
|
| 2823 | + |
|
| 2824 | + # spip_log("requete: $this->serveur >> $query",'sqlite.'._LOG_DEBUG); // boum ? pourquoi ? |
|
| 2825 | + if ($this->link) { |
|
| 2826 | + // memoriser la derniere erreur PHP vue |
|
| 2827 | + $e = (function_exists('error_get_last') ? error_get_last() : ""); |
|
| 2828 | + // sauver la derniere requete |
|
| 2829 | + $GLOBALS['connexions'][$this->serveur ? $this->serveur : 0]['last'] = $query; |
|
| 2830 | + $GLOBALS['connexions'][$this->serveur ? $this->serveur : 0]['total_requetes']++; |
|
| 2831 | + |
|
| 2832 | + $r = $this->link->query($query); |
|
| 2833 | + // sauvegarde de la requete (elle y est deja dans $r->queryString) |
|
| 2834 | + # $r->spipQueryString = $query; |
|
| 2835 | + |
|
| 2836 | + // comptage : oblige de compter le nombre d'entrees retournees |
|
| 2837 | + // par une requete SELECT |
|
| 2838 | + // aucune autre solution ne donne le nombre attendu :( ! |
|
| 2839 | + // particulierement s'il y a des LIMIT dans la requete. |
|
| 2840 | + if (strtoupper(substr(ltrim($query), 0, 6)) == 'SELECT') { |
|
| 2841 | + if ($r) { |
|
| 2842 | + // noter le link et la query pour faire le comptage *si* on en a besoin |
|
| 2843 | + $r->spipSqliteRowCount = array($this->link, $query); |
|
| 2844 | + } elseif ($r instanceof PDOStatement) { |
|
| 2845 | + $r->spipSqliteRowCount = 0; |
|
| 2846 | + } |
|
| 2847 | + } |
|
| 2848 | + |
|
| 2849 | + // loger les warnings/erreurs eventuels de sqlite remontant dans PHP |
|
| 2850 | + if ($err = (function_exists('error_get_last') ? error_get_last() : "") and $err != $e) { |
|
| 2851 | + $err = strip_tags($err['message']) . " in " . $err['file'] . " line " . $err['line']; |
|
| 2852 | + spip_log("$err - " . $query, 'sqlite.' . _LOG_ERREUR); |
|
| 2853 | + } else { |
|
| 2854 | + $err = ""; |
|
| 2855 | + } |
|
| 2856 | + |
|
| 2857 | + } else { |
|
| 2858 | + $r = false; |
|
| 2859 | + } |
|
| 2860 | + |
|
| 2861 | + if (spip_sqlite_errno($this->serveur)) { |
|
| 2862 | + $err .= spip_sqlite_error($query, $this->serveur); |
|
| 2863 | + } |
|
| 2864 | + |
|
| 2865 | + return $t ? trace_query_end($query, $t, $r, $err, $this->serveur) : $r; |
|
| 2866 | + } |
|
| 2867 | + |
|
| 2868 | + /** |
|
| 2869 | + * Obtient l'identifiant de la dernière ligne insérée ou modifiée |
|
| 2870 | + * |
|
| 2871 | + * @return int |
|
| 2872 | + **/ |
|
| 2873 | + public function last_insert_id() { |
|
| 2874 | + return $this->link->lastInsertId(); |
|
| 2875 | + } |
|
| 2876 | 2876 | } |
| 2877 | 2877 | |
| 2878 | 2878 | |
@@ -2882,198 +2882,198 @@ discard block |
||
| 2882 | 2882 | * (fonction pour proteger les textes) |
| 2883 | 2883 | */ |
| 2884 | 2884 | class sqlite_traducteur { |
| 2885 | - /** @var string $query Texte de la requête */ |
|
| 2886 | - public $query = ''; |
|
| 2887 | - /** @var string $prefixe Préfixe des tables */ |
|
| 2888 | - public $prefixe = ''; |
|
| 2889 | - /** @var string $sqlite_version Version de sqlite (2 ou 3) */ |
|
| 2890 | - public $sqlite_version = ''; |
|
| 2891 | - |
|
| 2892 | - /** Pour les corrections à effectuer sur les requêtes : array(code=>'texte') trouvé |
|
| 2893 | - * |
|
| 2894 | - * @var array |
|
| 2895 | - */ |
|
| 2896 | - public $textes = array(); |
|
| 2897 | - |
|
| 2898 | - /** |
|
| 2899 | - * Constructeur |
|
| 2900 | - * |
|
| 2901 | - * @param string $query Requête à préparer |
|
| 2902 | - * @param string $prefixe Prefixe des tables à utiliser |
|
| 2903 | - * @param string $sqlite_version Version SQLite (2 ou 3) |
|
| 2904 | - */ |
|
| 2905 | - public function __construct($query, $prefixe, $sqlite_version) { |
|
| 2906 | - $this->query = $query; |
|
| 2907 | - $this->prefixe = $prefixe; |
|
| 2908 | - $this->sqlite_version = $sqlite_version; |
|
| 2909 | - } |
|
| 2910 | - |
|
| 2911 | - /** |
|
| 2912 | - * Transformer la requete pour SQLite |
|
| 2913 | - * |
|
| 2914 | - * Enlève les textes, transforme la requête pour quelle soit |
|
| 2915 | - * bien interprétée par SQLite, puis remet les textes |
|
| 2916 | - * la fonction affecte `$this->query` |
|
| 2917 | - */ |
|
| 2918 | - public function traduire_requete() { |
|
| 2919 | - // |
|
| 2920 | - // 1) Protection des textes en les remplacant par des codes |
|
| 2921 | - // |
|
| 2922 | - // enlever les 'textes' et initialiser avec |
|
| 2923 | - list($this->query, $textes) = query_echappe_textes($this->query); |
|
| 2924 | - |
|
| 2925 | - // |
|
| 2926 | - // 2) Corrections de la requete |
|
| 2927 | - // |
|
| 2928 | - // Correction Create Database |
|
| 2929 | - // Create Database -> requete ignoree |
|
| 2930 | - if (strpos($this->query, 'CREATE DATABASE') === 0) { |
|
| 2931 | - spip_log("Sqlite : requete non executee -> $this->query", 'sqlite.' . _LOG_AVERTISSEMENT); |
|
| 2932 | - $this->query = "SELECT 1"; |
|
| 2933 | - } |
|
| 2934 | - |
|
| 2935 | - // Correction Insert Ignore |
|
| 2936 | - // INSERT IGNORE -> insert (tout court et pas 'insert or replace') |
|
| 2937 | - if (strpos($this->query, 'INSERT IGNORE') === 0) { |
|
| 2938 | - spip_log("Sqlite : requete transformee -> $this->query", 'sqlite.' . _LOG_DEBUG); |
|
| 2939 | - $this->query = 'INSERT ' . substr($this->query, '13'); |
|
| 2940 | - } |
|
| 2941 | - |
|
| 2942 | - // Correction des dates avec INTERVAL |
|
| 2943 | - // utiliser sql_date_proche() de preference |
|
| 2944 | - if (strpos($this->query, 'INTERVAL') !== false) { |
|
| 2945 | - $this->query = preg_replace_callback("/DATE_(ADD|SUB)(.*)INTERVAL\s+(\d+)\s+([a-zA-Z]+)\)/U", |
|
| 2946 | - array(&$this, '_remplacerDateParTime'), |
|
| 2947 | - $this->query); |
|
| 2948 | - } |
|
| 2949 | - |
|
| 2950 | - if (strpos($this->query, 'LEFT(') !== false) { |
|
| 2951 | - $this->query = str_replace('LEFT(', '_LEFT(', $this->query); |
|
| 2952 | - } |
|
| 2953 | - |
|
| 2954 | - if (strpos($this->query, 'TIMESTAMPDIFF(') !== false) { |
|
| 2955 | - $this->query = preg_replace('/TIMESTAMPDIFF\(\s*([^,]*)\s*,/Uims', "TIMESTAMPDIFF('\\1',", $this->query); |
|
| 2956 | - } |
|
| 2957 | - |
|
| 2958 | - |
|
| 2959 | - // Correction Using |
|
| 2960 | - // USING (non reconnu en sqlite2) |
|
| 2961 | - // problematique car la jointure ne se fait pas du coup. |
|
| 2962 | - if (($this->sqlite_version == 2) && (strpos($this->query, "USING") !== false)) { |
|
| 2963 | - spip_log("'USING (champ)' n'est pas reconnu en SQLite 2. Utilisez 'ON table1.champ = table2.champ'", |
|
| 2964 | - 'sqlite.' . _LOG_ERREUR); |
|
| 2965 | - $this->query = preg_replace('/USING\s*\([^\)]*\)/', '', $this->query); |
|
| 2966 | - } |
|
| 2967 | - |
|
| 2968 | - // Correction Field |
|
| 2969 | - // remplace FIELD(table,i,j,k...) par CASE WHEN table=i THEN n ... ELSE 0 END |
|
| 2970 | - if (strpos($this->query, 'FIELD') !== false) { |
|
| 2971 | - $this->query = preg_replace_callback('/FIELD\s*\(([^\)]*)\)/', |
|
| 2972 | - array(&$this, '_remplacerFieldParCase'), |
|
| 2973 | - $this->query); |
|
| 2974 | - } |
|
| 2975 | - |
|
| 2976 | - // Correction des noms de tables FROM |
|
| 2977 | - // mettre les bons noms de table dans from, update, insert, replace... |
|
| 2978 | - if (preg_match('/\s(SET|VALUES|WHERE|DATABASE)\s/iS', $this->query, $regs)) { |
|
| 2979 | - $suite = strstr($this->query, $regs[0]); |
|
| 2980 | - $this->query = substr($this->query, 0, -strlen($suite)); |
|
| 2981 | - } else { |
|
| 2982 | - $suite = ''; |
|
| 2983 | - } |
|
| 2984 | - $pref = ($this->prefixe) ? $this->prefixe . "_" : ""; |
|
| 2985 | - $this->query = preg_replace('/([,\s])spip_/S', '\1' . $pref, $this->query) . $suite; |
|
| 2986 | - |
|
| 2987 | - // Correction zero AS x |
|
| 2988 | - // pg n'aime pas 0+x AS alias, sqlite, dans le meme style, |
|
| 2989 | - // n'apprecie pas du tout SELECT 0 as x ... ORDER BY x |
|
| 2990 | - // il dit que x ne doit pas être un integer dans le order by ! |
|
| 2991 | - // on remplace du coup x par vide() dans ce cas uniquement |
|
| 2992 | - // |
|
| 2993 | - // rien que pour public/vertebrer.php ? |
|
| 2994 | - if ((strpos($this->query, "0 AS") !== false)) { |
|
| 2995 | - // on ne remplace que dans ORDER BY ou GROUP BY |
|
| 2996 | - if (preg_match('/\s(ORDER|GROUP) BY\s/i', $this->query, $regs)) { |
|
| 2997 | - $suite = strstr($this->query, $regs[0]); |
|
| 2998 | - $this->query = substr($this->query, 0, -strlen($suite)); |
|
| 2999 | - |
|
| 3000 | - // on cherche les noms des x dans 0 AS x |
|
| 3001 | - // on remplace dans $suite le nom par vide() |
|
| 3002 | - preg_match_all('/\b0 AS\s*([^\s,]+)/', $this->query, $matches, PREG_PATTERN_ORDER); |
|
| 3003 | - foreach ($matches[1] as $m) { |
|
| 3004 | - $suite = str_replace($m, 'VIDE()', $suite); |
|
| 3005 | - } |
|
| 3006 | - $this->query .= $suite; |
|
| 3007 | - } |
|
| 3008 | - } |
|
| 3009 | - |
|
| 3010 | - // Correction possible des divisions entieres |
|
| 3011 | - // Le standard SQL (lequel? ou?) semble indiquer que |
|
| 3012 | - // a/b=c doit donner c entier si a et b sont entiers 4/3=1. |
|
| 3013 | - // C'est ce que retournent effectivement SQL Server et SQLite |
|
| 3014 | - // Ce n'est pas ce qu'applique MySQL qui retourne un reel : 4/3=1.333... |
|
| 3015 | - // |
|
| 3016 | - // On peut forcer la conversion en multipliant par 1.0 avant la division |
|
| 3017 | - // /!\ SQLite 3.5.9 Debian/Ubuntu est victime d'un bug en plus ! |
|
| 3018 | - // cf. https://bugs.launchpad.net/ubuntu/+source/sqlite3/+bug/254228 |
|
| 3019 | - // http://www.sqlite.org/cvstrac/tktview?tn=3202 |
|
| 3020 | - // (4*1.0/3) n'est pas rendu dans ce cas ! |
|
| 3021 | - # $this->query = str_replace('/','* 1.00 / ',$this->query); |
|
| 3022 | - |
|
| 3023 | - |
|
| 3024 | - // Correction critere REGEXP, non reconnu en sqlite2 |
|
| 3025 | - if (($this->sqlite_version == 2) && (strpos($this->query, 'REGEXP') !== false)) { |
|
| 3026 | - $this->query = preg_replace('/([^\s\(]*)(\s*)REGEXP(\s*)([^\s\)]*)/', 'REGEXP($4, $1)', $this->query); |
|
| 3027 | - } |
|
| 3028 | - |
|
| 3029 | - // |
|
| 3030 | - // 3) Remise en place des textes d'origine |
|
| 3031 | - // |
|
| 3032 | - // Correction Antiquotes et echappements |
|
| 3033 | - // ` => rien |
|
| 3034 | - if (strpos($this->query, '`') !== false) { |
|
| 3035 | - $this->query = str_replace('`', '', $this->query); |
|
| 3036 | - } |
|
| 3037 | - |
|
| 3038 | - $this->query = query_reinjecte_textes($this->query, $textes); |
|
| 3039 | - |
|
| 3040 | - return $this->query; |
|
| 3041 | - } |
|
| 3042 | - |
|
| 3043 | - |
|
| 3044 | - /** |
|
| 3045 | - * Callback pour remplacer `DATE_` / `INTERVAL` |
|
| 3046 | - * par `DATE ... strtotime` |
|
| 3047 | - * |
|
| 3048 | - * @param array $matches Captures |
|
| 3049 | - * @return string Texte de date compris par SQLite |
|
| 3050 | - */ |
|
| 3051 | - public function _remplacerDateParTime($matches) { |
|
| 3052 | - $op = strtoupper($matches[1] == 'ADD') ? '+' : '-'; |
|
| 3053 | - |
|
| 3054 | - return "datetime$matches[2] '$op$matches[3] $matches[4]')"; |
|
| 3055 | - } |
|
| 3056 | - |
|
| 3057 | - /** |
|
| 3058 | - * Callback pour remplacer `FIELD(table,i,j,k...)` |
|
| 3059 | - * par `CASE WHEN table=i THEN n ... ELSE 0 END` |
|
| 3060 | - * |
|
| 3061 | - * @param array $matches Captures |
|
| 3062 | - * @return string Texte de liste ordonnée compris par SQLite |
|
| 3063 | - */ |
|
| 3064 | - public function _remplacerFieldParCase($matches) { |
|
| 3065 | - $fields = substr($matches[0], 6, -1); // ne recuperer que l'interieur X de field(X) |
|
| 3066 | - $t = explode(',', $fields); |
|
| 3067 | - $index = array_shift($t); |
|
| 3068 | - |
|
| 3069 | - $res = ''; |
|
| 3070 | - $n = 0; |
|
| 3071 | - foreach ($t as $v) { |
|
| 3072 | - $n++; |
|
| 3073 | - $res .= "\nWHEN $index=$v THEN $n"; |
|
| 3074 | - } |
|
| 3075 | - |
|
| 3076 | - return "CASE $res ELSE 0 END "; |
|
| 3077 | - } |
|
| 2885 | + /** @var string $query Texte de la requête */ |
|
| 2886 | + public $query = ''; |
|
| 2887 | + /** @var string $prefixe Préfixe des tables */ |
|
| 2888 | + public $prefixe = ''; |
|
| 2889 | + /** @var string $sqlite_version Version de sqlite (2 ou 3) */ |
|
| 2890 | + public $sqlite_version = ''; |
|
| 2891 | + |
|
| 2892 | + /** Pour les corrections à effectuer sur les requêtes : array(code=>'texte') trouvé |
|
| 2893 | + * |
|
| 2894 | + * @var array |
|
| 2895 | + */ |
|
| 2896 | + public $textes = array(); |
|
| 2897 | + |
|
| 2898 | + /** |
|
| 2899 | + * Constructeur |
|
| 2900 | + * |
|
| 2901 | + * @param string $query Requête à préparer |
|
| 2902 | + * @param string $prefixe Prefixe des tables à utiliser |
|
| 2903 | + * @param string $sqlite_version Version SQLite (2 ou 3) |
|
| 2904 | + */ |
|
| 2905 | + public function __construct($query, $prefixe, $sqlite_version) { |
|
| 2906 | + $this->query = $query; |
|
| 2907 | + $this->prefixe = $prefixe; |
|
| 2908 | + $this->sqlite_version = $sqlite_version; |
|
| 2909 | + } |
|
| 2910 | + |
|
| 2911 | + /** |
|
| 2912 | + * Transformer la requete pour SQLite |
|
| 2913 | + * |
|
| 2914 | + * Enlève les textes, transforme la requête pour quelle soit |
|
| 2915 | + * bien interprétée par SQLite, puis remet les textes |
|
| 2916 | + * la fonction affecte `$this->query` |
|
| 2917 | + */ |
|
| 2918 | + public function traduire_requete() { |
|
| 2919 | + // |
|
| 2920 | + // 1) Protection des textes en les remplacant par des codes |
|
| 2921 | + // |
|
| 2922 | + // enlever les 'textes' et initialiser avec |
|
| 2923 | + list($this->query, $textes) = query_echappe_textes($this->query); |
|
| 2924 | + |
|
| 2925 | + // |
|
| 2926 | + // 2) Corrections de la requete |
|
| 2927 | + // |
|
| 2928 | + // Correction Create Database |
|
| 2929 | + // Create Database -> requete ignoree |
|
| 2930 | + if (strpos($this->query, 'CREATE DATABASE') === 0) { |
|
| 2931 | + spip_log("Sqlite : requete non executee -> $this->query", 'sqlite.' . _LOG_AVERTISSEMENT); |
|
| 2932 | + $this->query = "SELECT 1"; |
|
| 2933 | + } |
|
| 2934 | + |
|
| 2935 | + // Correction Insert Ignore |
|
| 2936 | + // INSERT IGNORE -> insert (tout court et pas 'insert or replace') |
|
| 2937 | + if (strpos($this->query, 'INSERT IGNORE') === 0) { |
|
| 2938 | + spip_log("Sqlite : requete transformee -> $this->query", 'sqlite.' . _LOG_DEBUG); |
|
| 2939 | + $this->query = 'INSERT ' . substr($this->query, '13'); |
|
| 2940 | + } |
|
| 2941 | + |
|
| 2942 | + // Correction des dates avec INTERVAL |
|
| 2943 | + // utiliser sql_date_proche() de preference |
|
| 2944 | + if (strpos($this->query, 'INTERVAL') !== false) { |
|
| 2945 | + $this->query = preg_replace_callback("/DATE_(ADD|SUB)(.*)INTERVAL\s+(\d+)\s+([a-zA-Z]+)\)/U", |
|
| 2946 | + array(&$this, '_remplacerDateParTime'), |
|
| 2947 | + $this->query); |
|
| 2948 | + } |
|
| 2949 | + |
|
| 2950 | + if (strpos($this->query, 'LEFT(') !== false) { |
|
| 2951 | + $this->query = str_replace('LEFT(', '_LEFT(', $this->query); |
|
| 2952 | + } |
|
| 2953 | + |
|
| 2954 | + if (strpos($this->query, 'TIMESTAMPDIFF(') !== false) { |
|
| 2955 | + $this->query = preg_replace('/TIMESTAMPDIFF\(\s*([^,]*)\s*,/Uims', "TIMESTAMPDIFF('\\1',", $this->query); |
|
| 2956 | + } |
|
| 2957 | + |
|
| 2958 | + |
|
| 2959 | + // Correction Using |
|
| 2960 | + // USING (non reconnu en sqlite2) |
|
| 2961 | + // problematique car la jointure ne se fait pas du coup. |
|
| 2962 | + if (($this->sqlite_version == 2) && (strpos($this->query, "USING") !== false)) { |
|
| 2963 | + spip_log("'USING (champ)' n'est pas reconnu en SQLite 2. Utilisez 'ON table1.champ = table2.champ'", |
|
| 2964 | + 'sqlite.' . _LOG_ERREUR); |
|
| 2965 | + $this->query = preg_replace('/USING\s*\([^\)]*\)/', '', $this->query); |
|
| 2966 | + } |
|
| 2967 | + |
|
| 2968 | + // Correction Field |
|
| 2969 | + // remplace FIELD(table,i,j,k...) par CASE WHEN table=i THEN n ... ELSE 0 END |
|
| 2970 | + if (strpos($this->query, 'FIELD') !== false) { |
|
| 2971 | + $this->query = preg_replace_callback('/FIELD\s*\(([^\)]*)\)/', |
|
| 2972 | + array(&$this, '_remplacerFieldParCase'), |
|
| 2973 | + $this->query); |
|
| 2974 | + } |
|
| 2975 | + |
|
| 2976 | + // Correction des noms de tables FROM |
|
| 2977 | + // mettre les bons noms de table dans from, update, insert, replace... |
|
| 2978 | + if (preg_match('/\s(SET|VALUES|WHERE|DATABASE)\s/iS', $this->query, $regs)) { |
|
| 2979 | + $suite = strstr($this->query, $regs[0]); |
|
| 2980 | + $this->query = substr($this->query, 0, -strlen($suite)); |
|
| 2981 | + } else { |
|
| 2982 | + $suite = ''; |
|
| 2983 | + } |
|
| 2984 | + $pref = ($this->prefixe) ? $this->prefixe . "_" : ""; |
|
| 2985 | + $this->query = preg_replace('/([,\s])spip_/S', '\1' . $pref, $this->query) . $suite; |
|
| 2986 | + |
|
| 2987 | + // Correction zero AS x |
|
| 2988 | + // pg n'aime pas 0+x AS alias, sqlite, dans le meme style, |
|
| 2989 | + // n'apprecie pas du tout SELECT 0 as x ... ORDER BY x |
|
| 2990 | + // il dit que x ne doit pas être un integer dans le order by ! |
|
| 2991 | + // on remplace du coup x par vide() dans ce cas uniquement |
|
| 2992 | + // |
|
| 2993 | + // rien que pour public/vertebrer.php ? |
|
| 2994 | + if ((strpos($this->query, "0 AS") !== false)) { |
|
| 2995 | + // on ne remplace que dans ORDER BY ou GROUP BY |
|
| 2996 | + if (preg_match('/\s(ORDER|GROUP) BY\s/i', $this->query, $regs)) { |
|
| 2997 | + $suite = strstr($this->query, $regs[0]); |
|
| 2998 | + $this->query = substr($this->query, 0, -strlen($suite)); |
|
| 2999 | + |
|
| 3000 | + // on cherche les noms des x dans 0 AS x |
|
| 3001 | + // on remplace dans $suite le nom par vide() |
|
| 3002 | + preg_match_all('/\b0 AS\s*([^\s,]+)/', $this->query, $matches, PREG_PATTERN_ORDER); |
|
| 3003 | + foreach ($matches[1] as $m) { |
|
| 3004 | + $suite = str_replace($m, 'VIDE()', $suite); |
|
| 3005 | + } |
|
| 3006 | + $this->query .= $suite; |
|
| 3007 | + } |
|
| 3008 | + } |
|
| 3009 | + |
|
| 3010 | + // Correction possible des divisions entieres |
|
| 3011 | + // Le standard SQL (lequel? ou?) semble indiquer que |
|
| 3012 | + // a/b=c doit donner c entier si a et b sont entiers 4/3=1. |
|
| 3013 | + // C'est ce que retournent effectivement SQL Server et SQLite |
|
| 3014 | + // Ce n'est pas ce qu'applique MySQL qui retourne un reel : 4/3=1.333... |
|
| 3015 | + // |
|
| 3016 | + // On peut forcer la conversion en multipliant par 1.0 avant la division |
|
| 3017 | + // /!\ SQLite 3.5.9 Debian/Ubuntu est victime d'un bug en plus ! |
|
| 3018 | + // cf. https://bugs.launchpad.net/ubuntu/+source/sqlite3/+bug/254228 |
|
| 3019 | + // http://www.sqlite.org/cvstrac/tktview?tn=3202 |
|
| 3020 | + // (4*1.0/3) n'est pas rendu dans ce cas ! |
|
| 3021 | + # $this->query = str_replace('/','* 1.00 / ',$this->query); |
|
| 3022 | + |
|
| 3023 | + |
|
| 3024 | + // Correction critere REGEXP, non reconnu en sqlite2 |
|
| 3025 | + if (($this->sqlite_version == 2) && (strpos($this->query, 'REGEXP') !== false)) { |
|
| 3026 | + $this->query = preg_replace('/([^\s\(]*)(\s*)REGEXP(\s*)([^\s\)]*)/', 'REGEXP($4, $1)', $this->query); |
|
| 3027 | + } |
|
| 3028 | + |
|
| 3029 | + // |
|
| 3030 | + // 3) Remise en place des textes d'origine |
|
| 3031 | + // |
|
| 3032 | + // Correction Antiquotes et echappements |
|
| 3033 | + // ` => rien |
|
| 3034 | + if (strpos($this->query, '`') !== false) { |
|
| 3035 | + $this->query = str_replace('`', '', $this->query); |
|
| 3036 | + } |
|
| 3037 | + |
|
| 3038 | + $this->query = query_reinjecte_textes($this->query, $textes); |
|
| 3039 | + |
|
| 3040 | + return $this->query; |
|
| 3041 | + } |
|
| 3042 | + |
|
| 3043 | + |
|
| 3044 | + /** |
|
| 3045 | + * Callback pour remplacer `DATE_` / `INTERVAL` |
|
| 3046 | + * par `DATE ... strtotime` |
|
| 3047 | + * |
|
| 3048 | + * @param array $matches Captures |
|
| 3049 | + * @return string Texte de date compris par SQLite |
|
| 3050 | + */ |
|
| 3051 | + public function _remplacerDateParTime($matches) { |
|
| 3052 | + $op = strtoupper($matches[1] == 'ADD') ? '+' : '-'; |
|
| 3053 | + |
|
| 3054 | + return "datetime$matches[2] '$op$matches[3] $matches[4]')"; |
|
| 3055 | + } |
|
| 3056 | + |
|
| 3057 | + /** |
|
| 3058 | + * Callback pour remplacer `FIELD(table,i,j,k...)` |
|
| 3059 | + * par `CASE WHEN table=i THEN n ... ELSE 0 END` |
|
| 3060 | + * |
|
| 3061 | + * @param array $matches Captures |
|
| 3062 | + * @return string Texte de liste ordonnée compris par SQLite |
|
| 3063 | + */ |
|
| 3064 | + public function _remplacerFieldParCase($matches) { |
|
| 3065 | + $fields = substr($matches[0], 6, -1); // ne recuperer que l'interieur X de field(X) |
|
| 3066 | + $t = explode(',', $fields); |
|
| 3067 | + $index = array_shift($t); |
|
| 3068 | + |
|
| 3069 | + $res = ''; |
|
| 3070 | + $n = 0; |
|
| 3071 | + foreach ($t as $v) { |
|
| 3072 | + $n++; |
|
| 3073 | + $res .= "\nWHEN $index=$v THEN $n"; |
|
| 3074 | + } |
|
| 3075 | + |
|
| 3076 | + return "CASE $res ELSE 0 END "; |
|
| 3077 | + } |
|
| 3078 | 3078 | |
| 3079 | 3079 | } |
@@ -64,27 +64,27 @@ discard block |
||
| 64 | 64 | // determiner le dossier de la base : $addr ou _DIR_DB |
| 65 | 65 | $f = _DIR_DB; |
| 66 | 66 | if ($addr and strpos($addr, '/') !== false) { |
| 67 | - $f = rtrim($addr, '/') . '/'; |
|
| 67 | + $f = rtrim($addr, '/').'/'; |
|
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | // un nom de base demande et impossible d'obtenir la base, on s'en va : |
| 71 | 71 | // il faut que la base existe ou que le repertoire parent soit writable |
| 72 | - if ($db and !is_file($f .= $db . '.sqlite') and !is_writable(dirname($f))) { |
|
| 73 | - spip_log("base $f non trouvee ou droits en ecriture manquants", 'sqlite.' . _LOG_HS); |
|
| 72 | + if ($db and !is_file($f .= $db.'.sqlite') and !is_writable(dirname($f))) { |
|
| 73 | + spip_log("base $f non trouvee ou droits en ecriture manquants", 'sqlite.'._LOG_HS); |
|
| 74 | 74 | |
| 75 | 75 | return false; |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | 78 | // charger les modules sqlite au besoin |
| 79 | 79 | if (!_sqlite_charger_version($sqlite_version)) { |
| 80 | - spip_log("Impossible de trouver/charger le module SQLite ($sqlite_version)!", 'sqlite.' . _LOG_HS); |
|
| 80 | + spip_log("Impossible de trouver/charger le module SQLite ($sqlite_version)!", 'sqlite.'._LOG_HS); |
|
| 81 | 81 | |
| 82 | 82 | return false; |
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | // chargement des constantes |
| 86 | 86 | // il ne faut pas definir les constantes avant d'avoir charge les modules sqlite |
| 87 | - $define = "spip_sqlite" . $sqlite_version . "_constantes"; |
|
| 87 | + $define = "spip_sqlite".$sqlite_version."_constantes"; |
|
| 88 | 88 | $define(); |
| 89 | 89 | |
| 90 | 90 | $ok = false; |
@@ -92,8 +92,8 @@ discard block |
||
| 92 | 92 | // si pas de db -> |
| 93 | 93 | // base temporaire tant qu'on ne connait pas son vrai nom |
| 94 | 94 | // pour tester la connexion |
| 95 | - $db = "_sqlite" . $sqlite_version . "_install"; |
|
| 96 | - $tmp = _DIR_DB . $db . ".sqlite"; |
|
| 95 | + $db = "_sqlite".$sqlite_version."_install"; |
|
| 96 | + $tmp = _DIR_DB.$db.".sqlite"; |
|
| 97 | 97 | $ok = $link = new \PDO("sqlite:$tmp"); |
| 98 | 98 | } else { |
| 99 | 99 | // Ouvrir (eventuellement creer la base) |
@@ -102,7 +102,7 @@ discard block |
||
| 102 | 102 | |
| 103 | 103 | if (!$ok) { |
| 104 | 104 | $e = _sqlite_last_error_from_link($link); |
| 105 | - spip_log("Impossible d'ouvrir la base SQLite($sqlite_version) $f : $e", 'sqlite.' . _LOG_HS); |
|
| 105 | + spip_log("Impossible d'ouvrir la base SQLite($sqlite_version) $f : $e", 'sqlite.'._LOG_HS); |
|
| 106 | 106 | |
| 107 | 107 | return false; |
| 108 | 108 | } |
@@ -188,7 +188,7 @@ discard block |
||
| 188 | 188 | $table = $regs[3]; |
| 189 | 189 | $suite = $regs[4]; |
| 190 | 190 | } else { |
| 191 | - spip_log("SQLite : Probleme de ALTER TABLE mal forme dans $query", 'sqlite.' . _LOG_ERREUR); |
|
| 191 | + spip_log("SQLite : Probleme de ALTER TABLE mal forme dans $query", 'sqlite.'._LOG_ERREUR); |
|
| 192 | 192 | |
| 193 | 193 | return false; |
| 194 | 194 | } |
@@ -205,7 +205,7 @@ discard block |
||
| 205 | 205 | $i = 0; |
| 206 | 206 | $ouverte = false; |
| 207 | 207 | while ($do = array_shift($todo)) { |
| 208 | - $todo2[$i] = isset($todo2[$i]) ? $todo2[$i] . "," . $do : $do; |
|
| 208 | + $todo2[$i] = isset($todo2[$i]) ? $todo2[$i].",".$do : $do; |
|
| 209 | 209 | $o = (false !== strpos($do, "(")); |
| 210 | 210 | $f = (false !== strpos($do, ")")); |
| 211 | 211 | if ($o and !$f) { |
@@ -229,7 +229,7 @@ discard block |
||
| 229 | 229 | . ')\s*([^\s]*)\s*(.*)?/i', $do, $matches) |
| 230 | 230 | ) { |
| 231 | 231 | spip_log("SQLite : Probleme de ALTER TABLE, utilisation non reconnue dans : $do \n(requete d'origine : $query)", |
| 232 | - 'sqlite.' . _LOG_ERREUR); |
|
| 232 | + 'sqlite.'._LOG_ERREUR); |
|
| 233 | 233 | |
| 234 | 234 | return false; |
| 235 | 235 | } |
@@ -316,10 +316,10 @@ discard block |
||
| 316 | 316 | |
| 317 | 317 | // pas geres en sqlite2 |
| 318 | 318 | case 'RENAME': |
| 319 | - $do = "RENAME TO" . substr($do, 6); |
|
| 319 | + $do = "RENAME TO".substr($do, 6); |
|
| 320 | 320 | case 'RENAME TO': |
| 321 | 321 | if (!spip_sqlite::executer_requete("$debut $do", $serveur)) { |
| 322 | - spip_log("SQLite : Erreur ALTER TABLE / RENAME : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 322 | + spip_log("SQLite : Erreur ALTER TABLE / RENAME : $query", 'sqlite.'._LOG_ERREUR); |
|
| 323 | 323 | |
| 324 | 324 | return false; |
| 325 | 325 | } |
@@ -375,12 +375,12 @@ discard block |
||
| 375 | 375 | |
| 376 | 376 | // pas geres en sqlite2 |
| 377 | 377 | case 'ADD COLUMN': |
| 378 | - $do = "ADD" . substr($do, 10); |
|
| 378 | + $do = "ADD".substr($do, 10); |
|
| 379 | 379 | case 'ADD': |
| 380 | 380 | default: |
| 381 | 381 | if (!preg_match(',primary\s+key,i', $do)) { |
| 382 | 382 | if (!spip_sqlite::executer_requete("$debut $do", $serveur)) { |
| 383 | - spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 383 | + spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.'._LOG_ERREUR); |
|
| 384 | 384 | |
| 385 | 385 | return false; |
| 386 | 386 | } |
@@ -401,7 +401,7 @@ discard block |
||
| 401 | 401 | } |
| 402 | 402 | $opts['field'] = array($colonne_ajoutee => $def); |
| 403 | 403 | if (!_sqlite_modifier_table($table, array($colonne_ajoutee), $opts, $serveur)) { |
| 404 | - spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.' . _LOG_ERREUR); |
|
| 404 | + spip_log("SQLite : Erreur ALTER TABLE / ADD : $query", 'sqlite.'._LOG_ERREUR); |
|
| 405 | 405 | |
| 406 | 406 | return false; |
| 407 | 407 | } |
@@ -409,10 +409,10 @@ discard block |
||
| 409 | 409 | break; |
| 410 | 410 | } |
| 411 | 411 | // tout est bon, ouf ! |
| 412 | - spip_log("SQLite ($serveur) : Changements OK : $debut $do", 'sqlite.' . _LOG_INFO); |
|
| 412 | + spip_log("SQLite ($serveur) : Changements OK : $debut $do", 'sqlite.'._LOG_INFO); |
|
| 413 | 413 | } |
| 414 | 414 | |
| 415 | - spip_log("SQLite ($serveur) : fin ALTER TABLE OK !", 'sqlite.' . _LOG_INFO); |
|
| 415 | + spip_log("SQLite ($serveur) : fin ALTER TABLE OK !", 'sqlite.'._LOG_INFO); |
|
| 416 | 416 | |
| 417 | 417 | return true; |
| 418 | 418 | } |
@@ -481,9 +481,9 @@ discard block |
||
| 481 | 481 | * @return bool true si la base est créee. |
| 482 | 482 | **/ |
| 483 | 483 | function spip_sqlite_create_base($nom, $serveur = '', $option = true) { |
| 484 | - $f = $nom . '.sqlite'; |
|
| 484 | + $f = $nom.'.sqlite'; |
|
| 485 | 485 | if (strpos($nom, "/") === false) { |
| 486 | - $f = _DIR_DB . $f; |
|
| 486 | + $f = _DIR_DB.$f; |
|
| 487 | 487 | } |
| 488 | 488 | |
| 489 | 489 | $ok = new \PDO("sqlite:$f"); |
@@ -522,12 +522,12 @@ discard block |
||
| 522 | 522 | // vue deja presente |
| 523 | 523 | if (sql_showtable($nom, false, $serveur)) { |
| 524 | 524 | spip_log("Echec creation d'une vue sql ($nom) car celle-ci existe deja (serveur:$serveur)", |
| 525 | - 'sqlite.' . _LOG_ERREUR); |
|
| 525 | + 'sqlite.'._LOG_ERREUR); |
|
| 526 | 526 | |
| 527 | 527 | return false; |
| 528 | 528 | } |
| 529 | 529 | |
| 530 | - $query = "CREATE VIEW $nom AS " . $query_select; |
|
| 530 | + $query = "CREATE VIEW $nom AS ".$query_select; |
|
| 531 | 531 | |
| 532 | 532 | return spip_sqlite_query($query, $serveur, $requeter); |
| 533 | 533 | } |
@@ -552,15 +552,15 @@ discard block |
||
| 552 | 552 | */ |
| 553 | 553 | function spip_sqlite_create_index($nom, $table, $champs, $unique = '', $serveur = '', $requeter = true) { |
| 554 | 554 | if (!($nom or $table or $champs)) { |
| 555 | - spip_log("Champ manquant pour creer un index sqlite ($nom, $table, (" . join(',', $champs) . "))", |
|
| 556 | - 'sqlite.' . _LOG_ERREUR); |
|
| 555 | + spip_log("Champ manquant pour creer un index sqlite ($nom, $table, (".join(',', $champs)."))", |
|
| 556 | + 'sqlite.'._LOG_ERREUR); |
|
| 557 | 557 | |
| 558 | 558 | return false; |
| 559 | 559 | } |
| 560 | 560 | |
| 561 | 561 | // SQLite ne differentie pas noms des index en fonction des tables |
| 562 | 562 | // il faut donc creer des noms uniques d'index pour une base sqlite |
| 563 | - $nom = $table . '_' . $nom; |
|
| 563 | + $nom = $table.'_'.$nom; |
|
| 564 | 564 | // enlever d'eventuelles parentheses deja presentes sur champs |
| 565 | 565 | if (!is_array($champs)) { |
| 566 | 566 | if ($champs[0] == "(") { |
@@ -582,12 +582,12 @@ discard block |
||
| 582 | 582 | } else { |
| 583 | 583 | /* simuler le IF EXISTS - version 2 et sqlite < 3.3a */ |
| 584 | 584 | $a = spip_sqlite_showtable($table, $serveur); |
| 585 | - if (isset($a['key']['KEY ' . $nom])) { |
|
| 585 | + if (isset($a['key']['KEY '.$nom])) { |
|
| 586 | 586 | return true; |
| 587 | 587 | } |
| 588 | 588 | } |
| 589 | 589 | |
| 590 | - $query = "CREATE " . ($unique ? "UNIQUE " : "") . "INDEX$ifnotexists $nom ON $table (" . join(',', $champs) . ")"; |
|
| 590 | + $query = "CREATE ".($unique ? "UNIQUE " : "")."INDEX$ifnotexists $nom ON $table (".join(',', $champs).")"; |
|
| 591 | 591 | $res = spip_sqlite_query($query, $serveur, $requeter); |
| 592 | 592 | if (!$requeter) { |
| 593 | 593 | return $res; |
@@ -663,7 +663,7 @@ discard block |
||
| 663 | 663 | $serveur = '', |
| 664 | 664 | $requeter = true |
| 665 | 665 | ) { |
| 666 | - $c = !$groupby ? '*' : ('DISTINCT ' . (is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 666 | + $c = !$groupby ? '*' : ('DISTINCT '.(is_string($groupby) ? $groupby : join(',', $groupby))); |
|
| 667 | 667 | $r = spip_sqlite_select("COUNT($c)", $from, $where, '', '', '', |
| 668 | 668 | $having, $serveur, $requeter); |
| 669 | 669 | if ((is_resource($r) or is_object($r)) && $requeter) { // ressource : sqlite2, object : sqlite3 |
@@ -761,14 +761,14 @@ discard block |
||
| 761 | 761 | */ |
| 762 | 762 | function spip_sqlite_drop_index($nom, $table, $serveur = '', $requeter = true) { |
| 763 | 763 | if (!($nom or $table)) { |
| 764 | - spip_log("Champ manquant pour supprimer un index sqlite ($nom, $table)", 'sqlite.' . _LOG_ERREUR); |
|
| 764 | + spip_log("Champ manquant pour supprimer un index sqlite ($nom, $table)", 'sqlite.'._LOG_ERREUR); |
|
| 765 | 765 | |
| 766 | 766 | return false; |
| 767 | 767 | } |
| 768 | 768 | |
| 769 | 769 | // SQLite ne differentie pas noms des index en fonction des tables |
| 770 | 770 | // il faut donc creer des noms uniques d'index pour une base sqlite |
| 771 | - $index = $table . '_' . $nom; |
|
| 771 | + $index = $table.'_'.$nom; |
|
| 772 | 772 | $exist = " IF EXISTS"; |
| 773 | 773 | |
| 774 | 774 | $query = "DROP INDEX$exist $index"; |
@@ -800,7 +800,7 @@ discard block |
||
| 800 | 800 | if ($s) { |
| 801 | 801 | $trace = debug_backtrace(); |
| 802 | 802 | if ($trace[0]['function'] != "spip_sqlite_error") { |
| 803 | - spip_log("$s - $query - " . sql_error_backtrace(), 'sqlite.' . _LOG_ERREUR); |
|
| 803 | + spip_log("$s - $query - ".sql_error_backtrace(), 'sqlite.'._LOG_ERREUR); |
|
| 804 | 804 | } |
| 805 | 805 | } |
| 806 | 806 | |
@@ -847,14 +847,14 @@ discard block |
||
| 847 | 847 | $t = $link->errorInfo(); |
| 848 | 848 | $s = ltrim($t[0], '0'); // 00000 si pas d'erreur |
| 849 | 849 | if ($s) { |
| 850 | - $s .= ' / ' . $t[1]; |
|
| 850 | + $s .= ' / '.$t[1]; |
|
| 851 | 851 | } // ajoute l'erreur du moteur SQLite |
| 852 | 852 | } else { |
| 853 | 853 | $s = ": aucune ressource sqlite (link)"; |
| 854 | 854 | } |
| 855 | 855 | |
| 856 | 856 | if ($s) { |
| 857 | - spip_log("Erreur sqlite $s", 'sqlite.' . _LOG_ERREUR); |
|
| 857 | + spip_log("Erreur sqlite $s", 'sqlite.'._LOG_ERREUR); |
|
| 858 | 858 | } |
| 859 | 859 | |
| 860 | 860 | return $s ? $s : 0; |
@@ -878,7 +878,7 @@ discard block |
||
| 878 | 878 | } |
| 879 | 879 | |
| 880 | 880 | $query = spip_sqlite::traduire_requete($query, $serveur); |
| 881 | - $query = 'EXPLAIN ' . $query; |
|
| 881 | + $query = 'EXPLAIN '.$query; |
|
| 882 | 882 | if (!$requeter) { |
| 883 | 883 | return $query; |
| 884 | 884 | } |
@@ -1038,7 +1038,7 @@ discard block |
||
| 1038 | 1038 | **/ |
| 1039 | 1039 | function spip_sqlite_insert($table, $champs, $valeurs, $desc = array(), $serveur = '', $requeter = true) { |
| 1040 | 1040 | |
| 1041 | - $query = "INSERT INTO $table " . ($champs ? "$champs VALUES $valeurs" : "DEFAULT VALUES"); |
|
| 1041 | + $query = "INSERT INTO $table ".($champs ? "$champs VALUES $valeurs" : "DEFAULT VALUES"); |
|
| 1042 | 1042 | if ($r = spip_sqlite_query($query, $serveur, $requeter)) { |
| 1043 | 1043 | if (!$requeter) { |
| 1044 | 1044 | return $r; |
@@ -1094,8 +1094,8 @@ discard block |
||
| 1094 | 1094 | |
| 1095 | 1095 | $cles = $valeurs = ""; |
| 1096 | 1096 | if (count($couples)) { |
| 1097 | - $cles = "(" . join(',', array_keys($couples)) . ")"; |
|
| 1098 | - $valeurs = "(" . join(',', $couples) . ")"; |
|
| 1097 | + $cles = "(".join(',', array_keys($couples)).")"; |
|
| 1098 | + $valeurs = "(".join(',', $couples).")"; |
|
| 1099 | 1099 | } |
| 1100 | 1100 | |
| 1101 | 1101 | return spip_sqlite_insert($table, $cles, $valeurs, $desc, $serveur, $requeter); |
@@ -1155,11 +1155,11 @@ discard block |
||
| 1155 | 1155 | |
| 1156 | 1156 | $champs = $valeurs = ""; |
| 1157 | 1157 | if (count($couples)) { |
| 1158 | - $champs = "(" . join(',', array_keys($couples)) . ")"; |
|
| 1159 | - $valeurs = "(" . join(',', $couples) . ")"; |
|
| 1160 | - $query = $query_start . "$champs VALUES $valeurs"; |
|
| 1158 | + $champs = "(".join(',', array_keys($couples)).")"; |
|
| 1159 | + $valeurs = "(".join(',', $couples).")"; |
|
| 1160 | + $query = $query_start."$champs VALUES $valeurs"; |
|
| 1161 | 1161 | } else { |
| 1162 | - $query = $query_start . "DEFAULT VALUES"; |
|
| 1162 | + $query = $query_start."DEFAULT VALUES"; |
|
| 1163 | 1163 | } |
| 1164 | 1164 | |
| 1165 | 1165 | if ($requeter) { |
@@ -1291,7 +1291,7 @@ discard block |
||
| 1291 | 1291 | * @return string Texte de sélection pour la requête |
| 1292 | 1292 | */ |
| 1293 | 1293 | function spip_sqlite_multi($objet, $lang) { |
| 1294 | - $r = "EXTRAIRE_MULTI(" . $objet . ", '" . $lang . "') AS multi"; |
|
| 1294 | + $r = "EXTRAIRE_MULTI(".$objet.", '".$lang."') AS multi"; |
|
| 1295 | 1295 | |
| 1296 | 1296 | return $r; |
| 1297 | 1297 | } |
@@ -1362,7 +1362,7 @@ discard block |
||
| 1362 | 1362 | function spip_sqlite_date_proche($champ, $interval, $unite) { |
| 1363 | 1363 | $op = (($interval <= 0) ? '>' : '<'); |
| 1364 | 1364 | |
| 1365 | - return "($champ $op datetime('" . date("Y-m-d H:i:s") . "', '$interval $unite'))"; |
|
| 1365 | + return "($champ $op datetime('".date("Y-m-d H:i:s")."', '$interval $unite'))"; |
|
| 1366 | 1366 | } |
| 1367 | 1367 | |
| 1368 | 1368 | |
@@ -1392,7 +1392,7 @@ discard block |
||
| 1392 | 1392 | and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
| 1393 | 1393 | ) { |
| 1394 | 1394 | spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT ''", $serveur); |
| 1395 | - spip_log("ALTER $q", "repair" . _LOG_INFO_IMPORTANTE); |
|
| 1395 | + spip_log("ALTER $q", "repair"._LOG_INFO_IMPORTANTE); |
|
| 1396 | 1396 | } |
| 1397 | 1397 | if (preg_match(",^(INTEGER),i", $d) |
| 1398 | 1398 | and stripos($d, "NOT NULL") !== false |
@@ -1401,7 +1401,7 @@ discard block |
||
| 1401 | 1401 | and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
| 1402 | 1402 | ) { |
| 1403 | 1403 | spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT '0'", $serveur); |
| 1404 | - spip_log("ALTER $q", "repair" . _LOG_INFO_IMPORTANTE); |
|
| 1404 | + spip_log("ALTER $q", "repair"._LOG_INFO_IMPORTANTE); |
|
| 1405 | 1405 | } |
| 1406 | 1406 | if (preg_match(",^(datetime),i", $d) |
| 1407 | 1407 | and stripos($d, "NOT NULL") !== false |
@@ -1410,7 +1410,7 @@ discard block |
||
| 1410 | 1410 | and (!isset($desc['key']['PRIMARY KEY']) or $desc['key']['PRIMARY KEY'] !== $c) |
| 1411 | 1411 | ) { |
| 1412 | 1412 | spip_sqlite_alter($q = "TABLE $table CHANGE $c $c $d DEFAULT '0000-00-00 00:00:00'", $serveur); |
| 1413 | - spip_log("ALTER $q", "repair" . _LOG_INFO_IMPORTANTE); |
|
| 1413 | + spip_log("ALTER $q", "repair"._LOG_INFO_IMPORTANTE); |
|
| 1414 | 1414 | } |
| 1415 | 1415 | } |
| 1416 | 1416 | |
@@ -1461,8 +1461,8 @@ discard block |
||
| 1461 | 1461 | // recherche de champs 'timestamp' pour mise a jour auto de ceux-ci |
| 1462 | 1462 | $couples = _sqlite_ajouter_champs_timestamp($table, $couples, $desc, $serveur); |
| 1463 | 1463 | |
| 1464 | - return spip_sqlite_query("REPLACE INTO $table (" . join(',', array_keys($couples)) . ') VALUES (' . join(',', |
|
| 1465 | - $couples) . ')', $serveur); |
|
| 1464 | + return spip_sqlite_query("REPLACE INTO $table (".join(',', array_keys($couples)).') VALUES ('.join(',', |
|
| 1465 | + $couples).')', $serveur); |
|
| 1466 | 1466 | } |
| 1467 | 1467 | |
| 1468 | 1468 | |
@@ -1548,7 +1548,7 @@ discard block |
||
| 1548 | 1548 | . _sqlite_calculer_expression('WHERE', $where) |
| 1549 | 1549 | . _sqlite_calculer_expression('GROUP BY', $groupby, ',') |
| 1550 | 1550 | . _sqlite_calculer_expression('HAVING', $having) |
| 1551 | - . ($orderby ? ("\nORDER BY " . _sqlite_calculer_order($orderby)) : '') |
|
| 1551 | + . ($orderby ? ("\nORDER BY "._sqlite_calculer_order($orderby)) : '') |
|
| 1552 | 1552 | . ($limit ? "\nLIMIT $limit" : ''); |
| 1553 | 1553 | |
| 1554 | 1554 | // dans un select, on doit renvoyer la requête en cas d'erreur |
@@ -1585,10 +1585,10 @@ discard block |
||
| 1585 | 1585 | |
| 1586 | 1586 | // interdire la creation d'une nouvelle base, |
| 1587 | 1587 | // sauf si on est dans l'installation |
| 1588 | - if (!is_file($f = _DIR_DB . $db . '.sqlite') |
|
| 1588 | + if (!is_file($f = _DIR_DB.$db.'.sqlite') |
|
| 1589 | 1589 | && (!defined('_ECRIRE_INSTALL') || !_ECRIRE_INSTALL) |
| 1590 | 1590 | ) { |
| 1591 | - spip_log("Il est interdit de creer la base $db", 'sqlite.' . _LOG_HS); |
|
| 1591 | + spip_log("Il est interdit de creer la base $db", 'sqlite.'._LOG_HS); |
|
| 1592 | 1592 | |
| 1593 | 1593 | return false; |
| 1594 | 1594 | } |
@@ -1597,12 +1597,12 @@ discard block |
||
| 1597 | 1597 | // avec les identifiants connus |
| 1598 | 1598 | $index = $serveur ? $serveur : 0; |
| 1599 | 1599 | |
| 1600 | - if ($link = spip_connect_db('', '', '', '', '@selectdb@' . $db, $serveur, '', '')) { |
|
| 1600 | + if ($link = spip_connect_db('', '', '', '', '@selectdb@'.$db, $serveur, '', '')) { |
|
| 1601 | 1601 | if (($db == $link['db']) && $GLOBALS['connexions'][$index] = $link) { |
| 1602 | 1602 | return $db; |
| 1603 | 1603 | } |
| 1604 | 1604 | } else { |
| 1605 | - spip_log("Impossible de selectionner la base $db", 'sqlite.' . _LOG_HS); |
|
| 1605 | + spip_log("Impossible de selectionner la base $db", 'sqlite.'._LOG_HS); |
|
| 1606 | 1606 | |
| 1607 | 1607 | return false; |
| 1608 | 1608 | } |
@@ -1653,7 +1653,7 @@ discard block |
||
| 1653 | 1653 | $match = str_replace("[[POURCENT]]", "%", $match); |
| 1654 | 1654 | $match = "^$match$"; |
| 1655 | 1655 | |
| 1656 | - return spip_sqlite_query("SELECT name FROM sqlite_master WHERE type='table' AND tbl_name REGEXP " . _q($match), |
|
| 1656 | + return spip_sqlite_query("SELECT name FROM sqlite_master WHERE type='table' AND tbl_name REGEXP "._q($match), |
|
| 1657 | 1657 | $serveur, $requeter); |
| 1658 | 1658 | } |
| 1659 | 1659 | |
@@ -1741,7 +1741,7 @@ discard block |
||
| 1741 | 1741 | |
| 1742 | 1742 | # rustine pour DECIMAL(10,2) |
| 1743 | 1743 | if (false !== strpos($k, ')')) { |
| 1744 | - $fields[$k_precedent] .= ',' . $k . ' ' . $def; |
|
| 1744 | + $fields[$k_precedent] .= ','.$k.' '.$def; |
|
| 1745 | 1745 | continue; |
| 1746 | 1746 | } |
| 1747 | 1747 | |
@@ -1776,13 +1776,13 @@ discard block |
||
| 1776 | 1776 | . 'ORDER BY substr(type,2,1), name'; |
| 1777 | 1777 | $a = spip_sqlite_query($query, $serveur, $requeter); |
| 1778 | 1778 | while ($r = spip_sqlite_fetch($a, null, $serveur)) { |
| 1779 | - $key = str_replace($nom_table . '_', '', $r['name']); // enlever le nom de la table ajoute a l'index |
|
| 1779 | + $key = str_replace($nom_table.'_', '', $r['name']); // enlever le nom de la table ajoute a l'index |
|
| 1780 | 1780 | $keytype = "KEY"; |
| 1781 | 1781 | if (strpos($r['sql'], "UNIQUE INDEX") !== false) { |
| 1782 | 1782 | $keytype = "UNIQUE KEY"; |
| 1783 | 1783 | } |
| 1784 | 1784 | $colonnes = preg_replace(',.*\((.*)\).*,', '$1', $r['sql']); |
| 1785 | - $keys[$keytype . ' ' . $key] = $colonnes; |
|
| 1785 | + $keys[$keytype.' '.$key] = $colonnes; |
|
| 1786 | 1786 | } |
| 1787 | 1787 | } |
| 1788 | 1788 | |
@@ -1831,7 +1831,7 @@ discard block |
||
| 1831 | 1831 | |
| 1832 | 1832 | $set = array(); |
| 1833 | 1833 | foreach ($champs as $champ => $val) { |
| 1834 | - $set[] = $champ . "=$val"; |
|
| 1834 | + $set[] = $champ."=$val"; |
|
| 1835 | 1835 | } |
| 1836 | 1836 | if (!empty($set)) { |
| 1837 | 1837 | return spip_sqlite_query( |
@@ -1885,7 +1885,7 @@ discard block |
||
| 1885 | 1885 | |
| 1886 | 1886 | $set = array(); |
| 1887 | 1887 | foreach ($champs as $champ => $val) { |
| 1888 | - $set[] = $champ . '=' . _sqlite_calculer_cite($val, isset($fields[$champ]) ? $fields[$champ] : ''); |
|
| 1888 | + $set[] = $champ.'='._sqlite_calculer_cite($val, isset($fields[$champ]) ? $fields[$champ] : ''); |
|
| 1889 | 1889 | } |
| 1890 | 1890 | |
| 1891 | 1891 | return spip_sqlite_query( |
@@ -1911,7 +1911,7 @@ discard block |
||
| 1911 | 1911 | */ |
| 1912 | 1912 | function _sqlite_init() { |
| 1913 | 1913 | if (!defined('_DIR_DB')) { |
| 1914 | - define('_DIR_DB', _DIR_ETC . 'bases/'); |
|
| 1914 | + define('_DIR_DB', _DIR_ETC.'bases/'); |
|
| 1915 | 1915 | } |
| 1916 | 1916 | if (!defined('_SQLITE_CHMOD')) { |
| 1917 | 1917 | define('_SQLITE_CHMOD', _SPIP_CHMOD); |
@@ -2014,9 +2014,9 @@ discard block |
||
| 2014 | 2014 | } |
| 2015 | 2015 | |
| 2016 | 2016 | // echapper les ' en '' |
| 2017 | - spip_log("Pas de methode ->quote pour echapper", "sqlite." . _LOG_INFO_IMPORTANTE); |
|
| 2017 | + spip_log("Pas de methode ->quote pour echapper", "sqlite."._LOG_INFO_IMPORTANTE); |
|
| 2018 | 2018 | |
| 2019 | - return ("'" . str_replace("'", "''", $v) . "'"); |
|
| 2019 | + return ("'".str_replace("'", "''", $v)."'"); |
|
| 2020 | 2020 | } |
| 2021 | 2021 | |
| 2022 | 2022 | |
@@ -2039,12 +2039,12 @@ discard block |
||
| 2039 | 2039 | $exp = "\n$expression "; |
| 2040 | 2040 | |
| 2041 | 2041 | if (!is_array($v)) { |
| 2042 | - return $exp . $v; |
|
| 2042 | + return $exp.$v; |
|
| 2043 | 2043 | } else { |
| 2044 | 2044 | if (strtoupper($join) === 'AND') { |
| 2045 | - return $exp . join("\n\t$join ", array_map('_sqlite_calculer_where', $v)); |
|
| 2045 | + return $exp.join("\n\t$join ", array_map('_sqlite_calculer_where', $v)); |
|
| 2046 | 2046 | } else { |
| 2047 | - return $exp . join($join, $v); |
|
| 2047 | + return $exp.join($join, $v); |
|
| 2048 | 2048 | } |
| 2049 | 2049 | } |
| 2050 | 2050 | } |
@@ -2078,17 +2078,17 @@ discard block |
||
| 2078 | 2078 | if (substr($k, -1) == '@') { |
| 2079 | 2079 | // c'est une jointure qui se refere au from precedent |
| 2080 | 2080 | // pas de virgule |
| 2081 | - $res .= ' ' . $v; |
|
| 2081 | + $res .= ' '.$v; |
|
| 2082 | 2082 | } else { |
| 2083 | 2083 | if (!is_numeric($k)) { |
| 2084 | 2084 | $p = strpos($v, " "); |
| 2085 | 2085 | if ($p) { |
| 2086 | - $v = substr($v, 0, $p) . " AS '$k'" . substr($v, $p); |
|
| 2086 | + $v = substr($v, 0, $p)." AS '$k'".substr($v, $p); |
|
| 2087 | 2087 | } else { |
| 2088 | 2088 | $v .= " AS '$k'"; |
| 2089 | 2089 | } |
| 2090 | 2090 | } |
| 2091 | - $res .= ', ' . $v; |
|
| 2091 | + $res .= ', '.$v; |
|
| 2092 | 2092 | } |
| 2093 | 2093 | } |
| 2094 | 2094 | |
@@ -2226,13 +2226,13 @@ discard block |
||
| 2226 | 2226 | |
| 2227 | 2227 | $def_origine = sql_showtable($table_origine, false, $serveur); |
| 2228 | 2228 | if (!$def_origine or !isset($def_origine['field'])) { |
| 2229 | - spip_log("Alter table impossible sur $table_origine : table non trouvee", 'sqlite' . _LOG_ERREUR); |
|
| 2229 | + spip_log("Alter table impossible sur $table_origine : table non trouvee", 'sqlite'._LOG_ERREUR); |
|
| 2230 | 2230 | |
| 2231 | 2231 | return false; |
| 2232 | 2232 | } |
| 2233 | 2233 | |
| 2234 | 2234 | |
| 2235 | - $table_tmp = $table_origine . '_tmp'; |
|
| 2235 | + $table_tmp = $table_origine.'_tmp'; |
|
| 2236 | 2236 | |
| 2237 | 2237 | // 1) creer une table temporaire avec les modifications |
| 2238 | 2238 | // - DROP : suppression de la colonne |
@@ -2318,7 +2318,7 @@ discard block |
||
| 2318 | 2318 | } else { |
| 2319 | 2319 | // enlever KEY |
| 2320 | 2320 | $k = substr($k, 4); |
| 2321 | - $queries[] = "CREATE INDEX $table_destination" . "_$k ON $table_destination ($v)"; |
|
| 2321 | + $queries[] = "CREATE INDEX $table_destination"."_$k ON $table_destination ($v)"; |
|
| 2322 | 2322 | } |
| 2323 | 2323 | } |
| 2324 | 2324 | |
@@ -2418,26 +2418,26 @@ discard block |
||
| 2418 | 2418 | $enum = "(\s*\([^\)]*\))?"; |
| 2419 | 2419 | |
| 2420 | 2420 | $remplace = array( |
| 2421 | - '/enum' . $enum . '/is' => 'VARCHAR(255)', |
|
| 2421 | + '/enum'.$enum.'/is' => 'VARCHAR(255)', |
|
| 2422 | 2422 | '/COLLATE \w+_bin/is' => 'COLLATE BINARY', |
| 2423 | 2423 | '/COLLATE \w+_ci/is' => 'COLLATE NOCASE', |
| 2424 | 2424 | '/auto_increment/is' => '', |
| 2425 | 2425 | '/(timestamp .* )ON .*$/is' => '\\1', |
| 2426 | 2426 | '/character set \w+/is' => '', |
| 2427 | - '/((big|small|medium|tiny)?int(eger)?)' . $num . '\s*unsigned/is' => '\\1 UNSIGNED', |
|
| 2427 | + '/((big|small|medium|tiny)?int(eger)?)'.$num.'\s*unsigned/is' => '\\1 UNSIGNED', |
|
| 2428 | 2428 | '/(text\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
| 2429 | - '/((char|varchar)' . $num . '\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
|
| 2429 | + '/((char|varchar)'.$num.'\s+not\s+null(\s+collate\s+\w+)?)\s*$/is' => "\\1 DEFAULT ''", |
|
| 2430 | 2430 | '/(datetime\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00 00:00:00'", |
| 2431 | 2431 | '/(date\s+not\s+null)\s*$/is' => "\\1 DEFAULT '0000-00-00'", |
| 2432 | 2432 | ); |
| 2433 | 2433 | |
| 2434 | 2434 | // pour l'autoincrement, il faut des INTEGER NOT NULL PRIMARY KEY |
| 2435 | 2435 | $remplace_autocinc = array( |
| 2436 | - '/(big|small|medium|tiny)?int(eger)?' . $num . '/is' => 'INTEGER' |
|
| 2436 | + '/(big|small|medium|tiny)?int(eger)?'.$num.'/is' => 'INTEGER' |
|
| 2437 | 2437 | ); |
| 2438 | 2438 | // pour les int non autoincrement, il faut un DEFAULT |
| 2439 | 2439 | $remplace_nonautocinc = array( |
| 2440 | - '/((big|small|medium|tiny)?int(eger)?' . $num . '\s+not\s+null)\s*$/is' => "\\1 DEFAULT 0", |
|
| 2440 | + '/((big|small|medium|tiny)?int(eger)?'.$num.'\s+not\s+null)\s*$/is' => "\\1 DEFAULT 0", |
|
| 2441 | 2441 | ); |
| 2442 | 2442 | |
| 2443 | 2443 | if (is_string($query)) { |
@@ -2479,7 +2479,7 @@ discard block |
||
| 2479 | 2479 | return str_ireplace("BINARY", "COLLATE BINARY", $champ); |
| 2480 | 2480 | } |
| 2481 | 2481 | if (preg_match(",^(char|varchar|(long|small|medium|tiny)?text),i", $champ)) { |
| 2482 | - return $champ . " COLLATE NOCASE"; |
|
| 2482 | + return $champ." COLLATE NOCASE"; |
|
| 2483 | 2483 | } |
| 2484 | 2484 | |
| 2485 | 2485 | return $champ; |
@@ -2566,7 +2566,7 @@ discard block |
||
| 2566 | 2566 | } else { |
| 2567 | 2567 | /* simuler le IF EXISTS - version 2 et sqlite < 3.3a */ |
| 2568 | 2568 | $a = spip_sqlite_showtable($nom, $serveur); |
| 2569 | - if (isset($a['key']['KEY ' . $nom])) { |
|
| 2569 | + if (isset($a['key']['KEY '.$nom])) { |
|
| 2570 | 2570 | return true; |
| 2571 | 2571 | } |
| 2572 | 2572 | } |
@@ -2574,7 +2574,7 @@ discard block |
||
| 2574 | 2574 | } |
| 2575 | 2575 | |
| 2576 | 2576 | $temporary = $temporary ? ' TEMPORARY' : ''; |
| 2577 | - $q = "CREATE$temporary TABLE$ifnotexists $nom ($query" . ($keys ? ",$keys" : '') . ")\n"; |
|
| 2577 | + $q = "CREATE$temporary TABLE$ifnotexists $nom ($query".($keys ? ",$keys" : '').")\n"; |
|
| 2578 | 2578 | |
| 2579 | 2579 | return $q; |
| 2580 | 2580 | } |
@@ -2787,7 +2787,7 @@ discard block |
||
| 2787 | 2787 | $this->serveur = strtolower($serveur); |
| 2788 | 2788 | |
| 2789 | 2789 | if (!($this->link = _sqlite_link($this->serveur)) && (!defined('_ECRIRE_INSTALL') || !_ECRIRE_INSTALL)) { |
| 2790 | - spip_log("Aucune connexion sqlite (link)", 'sqlite.' . _LOG_ERREUR); |
|
| 2790 | + spip_log("Aucune connexion sqlite (link)", 'sqlite.'._LOG_ERREUR); |
|
| 2791 | 2791 | |
| 2792 | 2792 | return false; |
| 2793 | 2793 | } |
@@ -2848,8 +2848,8 @@ discard block |
||
| 2848 | 2848 | |
| 2849 | 2849 | // loger les warnings/erreurs eventuels de sqlite remontant dans PHP |
| 2850 | 2850 | if ($err = (function_exists('error_get_last') ? error_get_last() : "") and $err != $e) { |
| 2851 | - $err = strip_tags($err['message']) . " in " . $err['file'] . " line " . $err['line']; |
|
| 2852 | - spip_log("$err - " . $query, 'sqlite.' . _LOG_ERREUR); |
|
| 2851 | + $err = strip_tags($err['message'])." in ".$err['file']." line ".$err['line']; |
|
| 2852 | + spip_log("$err - ".$query, 'sqlite.'._LOG_ERREUR); |
|
| 2853 | 2853 | } else { |
| 2854 | 2854 | $err = ""; |
| 2855 | 2855 | } |
@@ -2928,15 +2928,15 @@ discard block |
||
| 2928 | 2928 | // Correction Create Database |
| 2929 | 2929 | // Create Database -> requete ignoree |
| 2930 | 2930 | if (strpos($this->query, 'CREATE DATABASE') === 0) { |
| 2931 | - spip_log("Sqlite : requete non executee -> $this->query", 'sqlite.' . _LOG_AVERTISSEMENT); |
|
| 2931 | + spip_log("Sqlite : requete non executee -> $this->query", 'sqlite.'._LOG_AVERTISSEMENT); |
|
| 2932 | 2932 | $this->query = "SELECT 1"; |
| 2933 | 2933 | } |
| 2934 | 2934 | |
| 2935 | 2935 | // Correction Insert Ignore |
| 2936 | 2936 | // INSERT IGNORE -> insert (tout court et pas 'insert or replace') |
| 2937 | 2937 | if (strpos($this->query, 'INSERT IGNORE') === 0) { |
| 2938 | - spip_log("Sqlite : requete transformee -> $this->query", 'sqlite.' . _LOG_DEBUG); |
|
| 2939 | - $this->query = 'INSERT ' . substr($this->query, '13'); |
|
| 2938 | + spip_log("Sqlite : requete transformee -> $this->query", 'sqlite.'._LOG_DEBUG); |
|
| 2939 | + $this->query = 'INSERT '.substr($this->query, '13'); |
|
| 2940 | 2940 | } |
| 2941 | 2941 | |
| 2942 | 2942 | // Correction des dates avec INTERVAL |
@@ -2961,7 +2961,7 @@ discard block |
||
| 2961 | 2961 | // problematique car la jointure ne se fait pas du coup. |
| 2962 | 2962 | if (($this->sqlite_version == 2) && (strpos($this->query, "USING") !== false)) { |
| 2963 | 2963 | spip_log("'USING (champ)' n'est pas reconnu en SQLite 2. Utilisez 'ON table1.champ = table2.champ'", |
| 2964 | - 'sqlite.' . _LOG_ERREUR); |
|
| 2964 | + 'sqlite.'._LOG_ERREUR); |
|
| 2965 | 2965 | $this->query = preg_replace('/USING\s*\([^\)]*\)/', '', $this->query); |
| 2966 | 2966 | } |
| 2967 | 2967 | |
@@ -2981,8 +2981,8 @@ discard block |
||
| 2981 | 2981 | } else { |
| 2982 | 2982 | $suite = ''; |
| 2983 | 2983 | } |
| 2984 | - $pref = ($this->prefixe) ? $this->prefixe . "_" : ""; |
|
| 2985 | - $this->query = preg_replace('/([,\s])spip_/S', '\1' . $pref, $this->query) . $suite; |
|
| 2984 | + $pref = ($this->prefixe) ? $this->prefixe."_" : ""; |
|
| 2985 | + $this->query = preg_replace('/([,\s])spip_/S', '\1'.$pref, $this->query).$suite; |
|
| 2986 | 2986 | |
| 2987 | 2987 | // Correction zero AS x |
| 2988 | 2988 | // pg n'aime pas 0+x AS alias, sqlite, dans le meme style, |
@@ -25,7 +25,7 @@ discard block |
||
| 25 | 25 | */ |
| 26 | 26 | |
| 27 | 27 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 28 | - return; |
|
| 28 | + return; |
|
| 29 | 29 | } |
| 30 | 30 | |
| 31 | 31 | /** Version de l'API SQL */ |
@@ -45,39 +45,39 @@ discard block |
||
| 45 | 45 | * contexte de l'erreur |
| 46 | 46 | **/ |
| 47 | 47 | function sql_error_backtrace($compil_info = false) { |
| 48 | - $trace = debug_backtrace(); |
|
| 49 | - $caller = array_shift($trace); |
|
| 50 | - while (count($trace) and (empty($trace[0]['file']) or $trace[0]['file'] === $caller['file'] or $trace[0]['file'] === __FILE__)) { |
|
| 51 | - array_shift($trace); |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - if ($compil_info) { |
|
| 55 | - $contexte_compil = array( |
|
| 56 | - $trace[0]['file'],// sourcefile |
|
| 57 | - '', //nom |
|
| 58 | - (isset($trace[1]) ? $trace[1]['function'] . "(){\n" : '') |
|
| 59 | - . $trace[0]['function'] . "();" |
|
| 60 | - . (isset($trace[1]) ? "\n}" : ''), //id_boucle |
|
| 61 | - $trace[0]['line'], // ligne |
|
| 62 | - $GLOBALS['spip_lang'], // lang |
|
| 63 | - ); |
|
| 64 | - |
|
| 65 | - return $contexte_compil; |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - $message = count($trace) ? $trace[0]['file'] . " L" . $trace[0]['line'] : ""; |
|
| 69 | - $f = array(); |
|
| 70 | - while (count($trace) and $t = array_shift($trace)) { |
|
| 71 | - if (in_array($t['function'], array('include_once', 'include_spip', 'find_in_path'))) { |
|
| 72 | - break; |
|
| 73 | - } |
|
| 74 | - $f[] = $t['function']; |
|
| 75 | - } |
|
| 76 | - if (count($f)) { |
|
| 77 | - $message .= " [" . implode("(),", $f) . "()]"; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - return $message; |
|
| 48 | + $trace = debug_backtrace(); |
|
| 49 | + $caller = array_shift($trace); |
|
| 50 | + while (count($trace) and (empty($trace[0]['file']) or $trace[0]['file'] === $caller['file'] or $trace[0]['file'] === __FILE__)) { |
|
| 51 | + array_shift($trace); |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + if ($compil_info) { |
|
| 55 | + $contexte_compil = array( |
|
| 56 | + $trace[0]['file'],// sourcefile |
|
| 57 | + '', //nom |
|
| 58 | + (isset($trace[1]) ? $trace[1]['function'] . "(){\n" : '') |
|
| 59 | + . $trace[0]['function'] . "();" |
|
| 60 | + . (isset($trace[1]) ? "\n}" : ''), //id_boucle |
|
| 61 | + $trace[0]['line'], // ligne |
|
| 62 | + $GLOBALS['spip_lang'], // lang |
|
| 63 | + ); |
|
| 64 | + |
|
| 65 | + return $contexte_compil; |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + $message = count($trace) ? $trace[0]['file'] . " L" . $trace[0]['line'] : ""; |
|
| 69 | + $f = array(); |
|
| 70 | + while (count($trace) and $t = array_shift($trace)) { |
|
| 71 | + if (in_array($t['function'], array('include_once', 'include_spip', 'find_in_path'))) { |
|
| 72 | + break; |
|
| 73 | + } |
|
| 74 | + $f[] = $t['function']; |
|
| 75 | + } |
|
| 76 | + if (count($f)) { |
|
| 77 | + $message .= " [" . implode("(),", $f) . "()]"; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + return $message; |
|
| 81 | 81 | } |
| 82 | 82 | |
| 83 | 83 | |
@@ -102,16 +102,16 @@ discard block |
||
| 102 | 102 | * |
| 103 | 103 | **/ |
| 104 | 104 | function sql_serveur($ins_sql = '', $serveur = '', $continue = false) { |
| 105 | - static $sql_serveur = array(); |
|
| 106 | - if (!isset($sql_serveur[$serveur][$ins_sql])) { |
|
| 107 | - $f = spip_connect_sql(sql_ABSTRACT_VERSION, $ins_sql, $serveur, $continue); |
|
| 108 | - if (!is_string($f) or !$f) { |
|
| 109 | - return $f; |
|
| 110 | - } |
|
| 111 | - $sql_serveur[$serveur][$ins_sql] = $f; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - return $sql_serveur[$serveur][$ins_sql]; |
|
| 105 | + static $sql_serveur = array(); |
|
| 106 | + if (!isset($sql_serveur[$serveur][$ins_sql])) { |
|
| 107 | + $f = spip_connect_sql(sql_ABSTRACT_VERSION, $ins_sql, $serveur, $continue); |
|
| 108 | + if (!is_string($f) or !$f) { |
|
| 109 | + return $f; |
|
| 110 | + } |
|
| 111 | + $sql_serveur[$serveur][$ins_sql] = $f; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + return $sql_serveur[$serveur][$ins_sql]; |
|
| 115 | 115 | } |
| 116 | 116 | |
| 117 | 117 | /** |
@@ -133,21 +133,21 @@ discard block |
||
| 133 | 133 | * Retourne le nom du charset si effectivement trouvé, sinon false. |
| 134 | 134 | **/ |
| 135 | 135 | function sql_get_charset($charset, $serveur = '', $option = true) { |
| 136 | - // le nom http du charset differe parfois du nom SQL utf-8 ==> utf8 etc. |
|
| 137 | - $desc = sql_serveur('', $serveur, true, true); |
|
| 138 | - $desc = $desc[sql_ABSTRACT_VERSION]; |
|
| 139 | - $c = $desc['charsets'][$charset]; |
|
| 140 | - if ($c) { |
|
| 141 | - if (function_exists($f = @$desc['get_charset'])) { |
|
| 142 | - if ($f($c, $serveur, $option !== false)) { |
|
| 143 | - return $c; |
|
| 144 | - } |
|
| 145 | - } |
|
| 146 | - } |
|
| 147 | - spip_log("SPIP ne connait pas les Charsets disponibles sur le serveur $serveur. Le serveur choisira seul.", |
|
| 148 | - _LOG_AVERTISSEMENT); |
|
| 149 | - |
|
| 150 | - return false; |
|
| 136 | + // le nom http du charset differe parfois du nom SQL utf-8 ==> utf8 etc. |
|
| 137 | + $desc = sql_serveur('', $serveur, true, true); |
|
| 138 | + $desc = $desc[sql_ABSTRACT_VERSION]; |
|
| 139 | + $c = $desc['charsets'][$charset]; |
|
| 140 | + if ($c) { |
|
| 141 | + if (function_exists($f = @$desc['get_charset'])) { |
|
| 142 | + if ($f($c, $serveur, $option !== false)) { |
|
| 143 | + return $c; |
|
| 144 | + } |
|
| 145 | + } |
|
| 146 | + } |
|
| 147 | + spip_log("SPIP ne connait pas les Charsets disponibles sur le serveur $serveur. Le serveur choisira seul.", |
|
| 148 | + _LOG_AVERTISSEMENT); |
|
| 149 | + |
|
| 150 | + return false; |
|
| 151 | 151 | } |
| 152 | 152 | |
| 153 | 153 | |
@@ -173,12 +173,12 @@ discard block |
||
| 173 | 173 | * Retourne true si elle reussie. |
| 174 | 174 | **/ |
| 175 | 175 | function sql_set_charset($charset, $serveur = '', $option = true) { |
| 176 | - $f = sql_serveur('set_charset', $serveur, $option === 'continue' or $option === false); |
|
| 177 | - if (!is_string($f) or !$f) { |
|
| 178 | - return false; |
|
| 179 | - } |
|
| 176 | + $f = sql_serveur('set_charset', $serveur, $option === 'continue' or $option === false); |
|
| 177 | + if (!is_string($f) or !$f) { |
|
| 178 | + return false; |
|
| 179 | + } |
|
| 180 | 180 | |
| 181 | - return $f($charset, $serveur, $option !== false); |
|
| 181 | + return $f($charset, $serveur, $option !== false); |
|
| 182 | 182 | } |
| 183 | 183 | |
| 184 | 184 | |
@@ -229,50 +229,50 @@ discard block |
||
| 229 | 229 | * |
| 230 | 230 | **/ |
| 231 | 231 | function sql_select( |
| 232 | - $select = array(), |
|
| 233 | - $from = array(), |
|
| 234 | - $where = array(), |
|
| 235 | - $groupby = array(), |
|
| 236 | - $orderby = array(), |
|
| 237 | - $limit = '', |
|
| 238 | - $having = array(), |
|
| 239 | - $serveur = '', |
|
| 240 | - $option = true |
|
| 232 | + $select = array(), |
|
| 233 | + $from = array(), |
|
| 234 | + $where = array(), |
|
| 235 | + $groupby = array(), |
|
| 236 | + $orderby = array(), |
|
| 237 | + $limit = '', |
|
| 238 | + $having = array(), |
|
| 239 | + $serveur = '', |
|
| 240 | + $option = true |
|
| 241 | 241 | ) { |
| 242 | - $f = sql_serveur('select', $serveur, $option === 'continue' or $option === false); |
|
| 243 | - if (!is_string($f) or !$f) { |
|
| 244 | - return false; |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - $debug = (defined('_VAR_MODE') and _VAR_MODE == 'debug'); |
|
| 248 | - if (($option !== false) and !$debug) { |
|
| 249 | - $res = $f($select, $from, $where, $groupby, $orderby, $limit, $having, $serveur, |
|
| 250 | - is_array($option) ? true : $option); |
|
| 251 | - } else { |
|
| 252 | - $query = $f($select, $from, $where, $groupby, $orderby, $limit, $having, $serveur, false); |
|
| 253 | - if (!$option) { |
|
| 254 | - return $query; |
|
| 255 | - } |
|
| 256 | - // le debug, c'est pour ce qui a ete produit par le compilateur |
|
| 257 | - if (isset($GLOBALS['debug']['aucasou'])) { |
|
| 258 | - list($table, $id, ) = $GLOBALS['debug']['aucasou']; |
|
| 259 | - $nom = $GLOBALS['debug_objets']['courant'] . $id; |
|
| 260 | - $GLOBALS['debug_objets']['requete'][$nom] = $query; |
|
| 261 | - } |
|
| 262 | - $res = $f($select, $from, $where, $groupby, $orderby, $limit, $having, $serveur, true); |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - // en cas d'erreur |
|
| 266 | - if (!is_string($res)) { |
|
| 267 | - return $res; |
|
| 268 | - } |
|
| 269 | - // denoncer l'erreur SQL dans sa version brute |
|
| 270 | - spip_sql_erreur($serveur); |
|
| 271 | - // idem dans sa version squelette (prefixe des tables non substitue) |
|
| 272 | - $contexte_compil = sql_error_backtrace(true); |
|
| 273 | - erreur_squelette(array(sql_errno($serveur), sql_error($serveur), $res), $contexte_compil); |
|
| 274 | - |
|
| 275 | - return false; |
|
| 242 | + $f = sql_serveur('select', $serveur, $option === 'continue' or $option === false); |
|
| 243 | + if (!is_string($f) or !$f) { |
|
| 244 | + return false; |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + $debug = (defined('_VAR_MODE') and _VAR_MODE == 'debug'); |
|
| 248 | + if (($option !== false) and !$debug) { |
|
| 249 | + $res = $f($select, $from, $where, $groupby, $orderby, $limit, $having, $serveur, |
|
| 250 | + is_array($option) ? true : $option); |
|
| 251 | + } else { |
|
| 252 | + $query = $f($select, $from, $where, $groupby, $orderby, $limit, $having, $serveur, false); |
|
| 253 | + if (!$option) { |
|
| 254 | + return $query; |
|
| 255 | + } |
|
| 256 | + // le debug, c'est pour ce qui a ete produit par le compilateur |
|
| 257 | + if (isset($GLOBALS['debug']['aucasou'])) { |
|
| 258 | + list($table, $id, ) = $GLOBALS['debug']['aucasou']; |
|
| 259 | + $nom = $GLOBALS['debug_objets']['courant'] . $id; |
|
| 260 | + $GLOBALS['debug_objets']['requete'][$nom] = $query; |
|
| 261 | + } |
|
| 262 | + $res = $f($select, $from, $where, $groupby, $orderby, $limit, $having, $serveur, true); |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + // en cas d'erreur |
|
| 266 | + if (!is_string($res)) { |
|
| 267 | + return $res; |
|
| 268 | + } |
|
| 269 | + // denoncer l'erreur SQL dans sa version brute |
|
| 270 | + spip_sql_erreur($serveur); |
|
| 271 | + // idem dans sa version squelette (prefixe des tables non substitue) |
|
| 272 | + $contexte_compil = sql_error_backtrace(true); |
|
| 273 | + erreur_squelette(array(sql_errno($serveur), sql_error($serveur), $res), $contexte_compil); |
|
| 274 | + |
|
| 275 | + return false; |
|
| 276 | 276 | } |
| 277 | 277 | |
| 278 | 278 | |
@@ -309,16 +309,16 @@ discard block |
||
| 309 | 309 | * |
| 310 | 310 | **/ |
| 311 | 311 | function sql_get_select( |
| 312 | - $select = array(), |
|
| 313 | - $from = array(), |
|
| 314 | - $where = array(), |
|
| 315 | - $groupby = array(), |
|
| 316 | - $orderby = array(), |
|
| 317 | - $limit = '', |
|
| 318 | - $having = array(), |
|
| 319 | - $serveur = '' |
|
| 312 | + $select = array(), |
|
| 313 | + $from = array(), |
|
| 314 | + $where = array(), |
|
| 315 | + $groupby = array(), |
|
| 316 | + $orderby = array(), |
|
| 317 | + $limit = '', |
|
| 318 | + $having = array(), |
|
| 319 | + $serveur = '' |
|
| 320 | 320 | ) { |
| 321 | - return sql_select($select, $from, $where, $groupby, $orderby, $limit, $having, $serveur, false); |
|
| 321 | + return sql_select($select, $from, $where, $groupby, $orderby, $limit, $having, $serveur, false); |
|
| 322 | 322 | } |
| 323 | 323 | |
| 324 | 324 | |
@@ -362,23 +362,23 @@ discard block |
||
| 362 | 362 | * |
| 363 | 363 | **/ |
| 364 | 364 | function sql_countsel( |
| 365 | - $from = array(), |
|
| 366 | - $where = array(), |
|
| 367 | - $groupby = array(), |
|
| 368 | - $having = array(), |
|
| 369 | - $serveur = '', |
|
| 370 | - $option = true |
|
| 365 | + $from = array(), |
|
| 366 | + $where = array(), |
|
| 367 | + $groupby = array(), |
|
| 368 | + $having = array(), |
|
| 369 | + $serveur = '', |
|
| 370 | + $option = true |
|
| 371 | 371 | ) { |
| 372 | - $f = sql_serveur('countsel', $serveur, $option === 'continue' or $option === false); |
|
| 373 | - if (!is_string($f) or !$f) { |
|
| 374 | - return false; |
|
| 375 | - } |
|
| 376 | - $r = $f($from, $where, $groupby, $having, $serveur, $option !== false); |
|
| 377 | - if ($r === false) { |
|
| 378 | - spip_sql_erreur($serveur); |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - return $r; |
|
| 372 | + $f = sql_serveur('countsel', $serveur, $option === 'continue' or $option === false); |
|
| 373 | + if (!is_string($f) or !$f) { |
|
| 374 | + return false; |
|
| 375 | + } |
|
| 376 | + $r = $f($from, $where, $groupby, $having, $serveur, $option !== false); |
|
| 377 | + if ($r === false) { |
|
| 378 | + spip_sql_erreur($serveur); |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + return $r; |
|
| 382 | 382 | } |
| 383 | 383 | |
| 384 | 384 | /** |
@@ -410,16 +410,16 @@ discard block |
||
| 410 | 410 | * Ce retour n'est pas pertinent pour savoir si l'opération est correctement réalisée. |
| 411 | 411 | **/ |
| 412 | 412 | function sql_alter($q, $serveur = '', $option = true) { |
| 413 | - $f = sql_serveur('alter', $serveur, $option === 'continue' or $option === false); |
|
| 414 | - if (!is_string($f) or !$f) { |
|
| 415 | - return false; |
|
| 416 | - } |
|
| 417 | - $r = $f($q, $serveur, $option !== false); |
|
| 418 | - if ($r === false) { |
|
| 419 | - spip_sql_erreur($serveur); |
|
| 420 | - } |
|
| 421 | - |
|
| 422 | - return $r; |
|
| 413 | + $f = sql_serveur('alter', $serveur, $option === 'continue' or $option === false); |
|
| 414 | + if (!is_string($f) or !$f) { |
|
| 415 | + return false; |
|
| 416 | + } |
|
| 417 | + $r = $f($q, $serveur, $option !== false); |
|
| 418 | + if ($r === false) { |
|
| 419 | + spip_sql_erreur($serveur); |
|
| 420 | + } |
|
| 421 | + |
|
| 422 | + return $r; |
|
| 423 | 423 | } |
| 424 | 424 | |
| 425 | 425 | /** |
@@ -442,12 +442,12 @@ discard block |
||
| 442 | 442 | * presentant une ligne de resultat d'une selection |
| 443 | 443 | */ |
| 444 | 444 | function sql_fetch($res, $serveur = '', $option = true) { |
| 445 | - $f = sql_serveur('fetch', $serveur, $option === 'continue' or $option === false); |
|
| 446 | - if (!is_string($f) or !$f) { |
|
| 447 | - return false; |
|
| 448 | - } |
|
| 445 | + $f = sql_serveur('fetch', $serveur, $option === 'continue' or $option === false); |
|
| 446 | + if (!is_string($f) or !$f) { |
|
| 447 | + return false; |
|
| 448 | + } |
|
| 449 | 449 | |
| 450 | - return $f($res, null, $serveur, $option !== false); |
|
| 450 | + return $f($res, null, $serveur, $option !== false); |
|
| 451 | 451 | } |
| 452 | 452 | |
| 453 | 453 | |
@@ -474,20 +474,20 @@ discard block |
||
| 474 | 474 | * presentant une ligne de resultat d'une selection |
| 475 | 475 | */ |
| 476 | 476 | function sql_fetch_all($res, $serveur = '', $option = true) { |
| 477 | - $rows = array(); |
|
| 478 | - if (!$res) { |
|
| 479 | - return $rows; |
|
| 480 | - } |
|
| 481 | - $f = sql_serveur('fetch', $serveur, $option === 'continue' or $option === false); |
|
| 482 | - if (!is_string($f) or !$f) { |
|
| 483 | - return array(); |
|
| 484 | - } |
|
| 485 | - while ($r = $f($res, null, $serveur, $option !== false)) { |
|
| 486 | - $rows[] = $r; |
|
| 487 | - } |
|
| 488 | - sql_free($res, $serveur); |
|
| 489 | - |
|
| 490 | - return $rows; |
|
| 477 | + $rows = array(); |
|
| 478 | + if (!$res) { |
|
| 479 | + return $rows; |
|
| 480 | + } |
|
| 481 | + $f = sql_serveur('fetch', $serveur, $option === 'continue' or $option === false); |
|
| 482 | + if (!is_string($f) or !$f) { |
|
| 483 | + return array(); |
|
| 484 | + } |
|
| 485 | + while ($r = $f($res, null, $serveur, $option !== false)) { |
|
| 486 | + $rows[] = $r; |
|
| 487 | + } |
|
| 488 | + sql_free($res, $serveur); |
|
| 489 | + |
|
| 490 | + return $rows; |
|
| 491 | 491 | } |
| 492 | 492 | |
| 493 | 493 | /** |
@@ -515,16 +515,16 @@ discard block |
||
| 515 | 515 | * Operation effectuée (true), sinon false. |
| 516 | 516 | **/ |
| 517 | 517 | function sql_seek($res, $row_number, $serveur = '', $option = true) { |
| 518 | - $f = sql_serveur('seek', $serveur, $option === 'continue' or $option === false); |
|
| 519 | - if (!is_string($f) or !$f) { |
|
| 520 | - return false; |
|
| 521 | - } |
|
| 522 | - $r = $f($res, $row_number, $serveur, $option !== false); |
|
| 523 | - if ($r === false) { |
|
| 524 | - spip_sql_erreur($serveur); |
|
| 525 | - } |
|
| 526 | - |
|
| 527 | - return $r; |
|
| 518 | + $f = sql_serveur('seek', $serveur, $option === 'continue' or $option === false); |
|
| 519 | + if (!is_string($f) or !$f) { |
|
| 520 | + return false; |
|
| 521 | + } |
|
| 522 | + $r = $f($res, $row_number, $serveur, $option !== false); |
|
| 523 | + if ($r === false) { |
|
| 524 | + spip_sql_erreur($serveur); |
|
| 525 | + } |
|
| 526 | + |
|
| 527 | + return $r; |
|
| 528 | 528 | } |
| 529 | 529 | |
| 530 | 530 | |
@@ -549,16 +549,16 @@ discard block |
||
| 549 | 549 | * False en cas d'erreur. |
| 550 | 550 | **/ |
| 551 | 551 | function sql_listdbs($serveur = '', $option = true) { |
| 552 | - $f = sql_serveur('listdbs', $serveur, $option === 'continue' or $option === false); |
|
| 553 | - if (!is_string($f) or !$f) { |
|
| 554 | - return false; |
|
| 555 | - } |
|
| 556 | - $r = $f($serveur); |
|
| 557 | - if ($r === false) { |
|
| 558 | - spip_sql_erreur($serveur); |
|
| 559 | - } |
|
| 560 | - |
|
| 561 | - return $r; |
|
| 552 | + $f = sql_serveur('listdbs', $serveur, $option === 'continue' or $option === false); |
|
| 553 | + if (!is_string($f) or !$f) { |
|
| 554 | + return false; |
|
| 555 | + } |
|
| 556 | + $r = $f($serveur); |
|
| 557 | + if ($r === false) { |
|
| 558 | + spip_sql_erreur($serveur); |
|
| 559 | + } |
|
| 560 | + |
|
| 561 | + return $r; |
|
| 562 | 562 | } |
| 563 | 563 | |
| 564 | 564 | |
@@ -581,16 +581,16 @@ discard block |
||
| 581 | 581 | * - False en cas d'erreur. |
| 582 | 582 | **/ |
| 583 | 583 | function sql_selectdb($nom, $serveur = '', $option = true) { |
| 584 | - $f = sql_serveur('selectdb', $serveur, $option === 'continue' or $option === false); |
|
| 585 | - if (!is_string($f) or !$f) { |
|
| 586 | - return false; |
|
| 587 | - } |
|
| 588 | - $r = $f($nom, $serveur, $option !== false); |
|
| 589 | - if ($r === false) { |
|
| 590 | - spip_sql_erreur($serveur); |
|
| 591 | - } |
|
| 592 | - |
|
| 593 | - return $r; |
|
| 584 | + $f = sql_serveur('selectdb', $serveur, $option === 'continue' or $option === false); |
|
| 585 | + if (!is_string($f) or !$f) { |
|
| 586 | + return false; |
|
| 587 | + } |
|
| 588 | + $r = $f($nom, $serveur, $option !== false); |
|
| 589 | + if ($r === false) { |
|
| 590 | + spip_sql_erreur($serveur); |
|
| 591 | + } |
|
| 592 | + |
|
| 593 | + return $r; |
|
| 594 | 594 | } |
| 595 | 595 | |
| 596 | 596 | /** |
@@ -615,16 +615,16 @@ discard block |
||
| 615 | 615 | * - false en cas d'erreur. |
| 616 | 616 | **/ |
| 617 | 617 | function sql_count($res, $serveur = '', $option = true) { |
| 618 | - $f = sql_serveur('count', $serveur, $option === 'continue' or $option === false); |
|
| 619 | - if (!is_string($f) or !$f) { |
|
| 620 | - return false; |
|
| 621 | - } |
|
| 622 | - $r = $f($res, $serveur, $option !== false); |
|
| 623 | - if ($r === false) { |
|
| 624 | - spip_sql_erreur($serveur); |
|
| 625 | - } |
|
| 626 | - |
|
| 627 | - return $r; |
|
| 618 | + $f = sql_serveur('count', $serveur, $option === 'continue' or $option === false); |
|
| 619 | + if (!is_string($f) or !$f) { |
|
| 620 | + return false; |
|
| 621 | + } |
|
| 622 | + $r = $f($res, $serveur, $option !== false); |
|
| 623 | + if ($r === false) { |
|
| 624 | + spip_sql_erreur($serveur); |
|
| 625 | + } |
|
| 626 | + |
|
| 627 | + return $r; |
|
| 628 | 628 | } |
| 629 | 629 | |
| 630 | 630 | /** |
@@ -646,12 +646,12 @@ discard block |
||
| 646 | 646 | * True si réussi |
| 647 | 647 | */ |
| 648 | 648 | function sql_free($res, $serveur = '', $option = true) { |
| 649 | - $f = sql_serveur('free', $serveur, $option === 'continue' or $option === false); |
|
| 650 | - if (!is_string($f) or !$f) { |
|
| 651 | - return false; |
|
| 652 | - } |
|
| 649 | + $f = sql_serveur('free', $serveur, $option === 'continue' or $option === false); |
|
| 650 | + if (!is_string($f) or !$f) { |
|
| 651 | + return false; |
|
| 652 | + } |
|
| 653 | 653 | |
| 654 | - return $f($res); |
|
| 654 | + return $f($res); |
|
| 655 | 655 | } |
| 656 | 656 | |
| 657 | 657 | |
@@ -689,17 +689,17 @@ discard block |
||
| 689 | 689 | * - False en cas d'erreur. |
| 690 | 690 | **/ |
| 691 | 691 | function sql_insert($table, $noms, $valeurs, $desc = array(), $serveur = '', $option = true) { |
| 692 | - $f = sql_serveur('insert', $serveur, $option === 'continue' or $option === false); |
|
| 693 | - if (!is_string($f) or !$f) { |
|
| 694 | - return false; |
|
| 695 | - } |
|
| 696 | - $r = $f($table, $noms, $valeurs, $desc, $serveur, $option !== false); |
|
| 697 | - if ($r === false or $r === null) { |
|
| 698 | - spip_sql_erreur($serveur); |
|
| 699 | - $r = false; |
|
| 700 | - } |
|
| 701 | - |
|
| 702 | - return $r; |
|
| 692 | + $f = sql_serveur('insert', $serveur, $option === 'continue' or $option === false); |
|
| 693 | + if (!is_string($f) or !$f) { |
|
| 694 | + return false; |
|
| 695 | + } |
|
| 696 | + $r = $f($table, $noms, $valeurs, $desc, $serveur, $option !== false); |
|
| 697 | + if ($r === false or $r === null) { |
|
| 698 | + spip_sql_erreur($serveur); |
|
| 699 | + $r = false; |
|
| 700 | + } |
|
| 701 | + |
|
| 702 | + return $r; |
|
| 703 | 703 | } |
| 704 | 704 | |
| 705 | 705 | /** |
@@ -740,17 +740,17 @@ discard block |
||
| 740 | 740 | * - False en cas d'erreur. |
| 741 | 741 | **/ |
| 742 | 742 | function sql_insertq($table, $couples = array(), $desc = array(), $serveur = '', $option = true) { |
| 743 | - $f = sql_serveur('insertq', $serveur, $option === 'continue' or $option === false); |
|
| 744 | - if (!is_string($f) or !$f) { |
|
| 745 | - return false; |
|
| 746 | - } |
|
| 747 | - $r = $f($table, $couples, $desc, $serveur, $option !== false); |
|
| 748 | - if ($r === false or $r === null) { |
|
| 749 | - spip_sql_erreur($serveur); |
|
| 750 | - $r = false; |
|
| 751 | - } |
|
| 752 | - |
|
| 753 | - return $r; |
|
| 743 | + $f = sql_serveur('insertq', $serveur, $option === 'continue' or $option === false); |
|
| 744 | + if (!is_string($f) or !$f) { |
|
| 745 | + return false; |
|
| 746 | + } |
|
| 747 | + $r = $f($table, $couples, $desc, $serveur, $option !== false); |
|
| 748 | + if ($r === false or $r === null) { |
|
| 749 | + spip_sql_erreur($serveur); |
|
| 750 | + $r = false; |
|
| 751 | + } |
|
| 752 | + |
|
| 753 | + return $r; |
|
| 754 | 754 | } |
| 755 | 755 | |
| 756 | 756 | /** |
@@ -785,17 +785,17 @@ discard block |
||
| 785 | 785 | * - False en cas d'erreur. |
| 786 | 786 | **/ |
| 787 | 787 | function sql_insertq_multi($table, $couples = array(), $desc = array(), $serveur = '', $option = true) { |
| 788 | - $f = sql_serveur('insertq_multi', $serveur, $option === 'continue' or $option === false); |
|
| 789 | - if (!is_string($f) or !$f) { |
|
| 790 | - return false; |
|
| 791 | - } |
|
| 792 | - $r = $f($table, $couples, $desc, $serveur, $option !== false); |
|
| 793 | - if ($r === false or $r === null) { |
|
| 794 | - spip_sql_erreur($serveur); |
|
| 795 | - $r = false; |
|
| 796 | - } |
|
| 797 | - |
|
| 798 | - return $r; |
|
| 788 | + $f = sql_serveur('insertq_multi', $serveur, $option === 'continue' or $option === false); |
|
| 789 | + if (!is_string($f) or !$f) { |
|
| 790 | + return false; |
|
| 791 | + } |
|
| 792 | + $r = $f($table, $couples, $desc, $serveur, $option !== false); |
|
| 793 | + if ($r === false or $r === null) { |
|
| 794 | + spip_sql_erreur($serveur); |
|
| 795 | + $r = false; |
|
| 796 | + } |
|
| 797 | + |
|
| 798 | + return $r; |
|
| 799 | 799 | } |
| 800 | 800 | |
| 801 | 801 | /** |
@@ -835,16 +835,16 @@ discard block |
||
| 835 | 835 | * - array Tableau décrivant la requête et son temps d'exécution si var_profile est actif |
| 836 | 836 | */ |
| 837 | 837 | function sql_update($table, $exp, $where = '', $desc = array(), $serveur = '', $option = true) { |
| 838 | - $f = sql_serveur('update', $serveur, $option === 'continue' or $option === false); |
|
| 839 | - if (!is_string($f) or !$f) { |
|
| 840 | - return false; |
|
| 841 | - } |
|
| 842 | - $r = $f($table, $exp, $where, $desc, $serveur, $option !== false); |
|
| 843 | - if ($r === false) { |
|
| 844 | - spip_sql_erreur($serveur); |
|
| 845 | - } |
|
| 846 | - |
|
| 847 | - return $r; |
|
| 838 | + $f = sql_serveur('update', $serveur, $option === 'continue' or $option === false); |
|
| 839 | + if (!is_string($f) or !$f) { |
|
| 840 | + return false; |
|
| 841 | + } |
|
| 842 | + $r = $f($table, $exp, $where, $desc, $serveur, $option !== false); |
|
| 843 | + if ($r === false) { |
|
| 844 | + spip_sql_erreur($serveur); |
|
| 845 | + } |
|
| 846 | + |
|
| 847 | + return $r; |
|
| 848 | 848 | } |
| 849 | 849 | |
| 850 | 850 | |
@@ -890,16 +890,16 @@ discard block |
||
| 890 | 890 | * - False en cas d'erreur. |
| 891 | 891 | **/ |
| 892 | 892 | function sql_updateq($table, $exp, $where = '', $desc = array(), $serveur = '', $option = true) { |
| 893 | - $f = sql_serveur('updateq', $serveur, $option === 'continue' or $option === false); |
|
| 894 | - if (!is_string($f) or !$f) { |
|
| 895 | - return false; |
|
| 896 | - } |
|
| 897 | - $r = $f($table, $exp, $where, $desc, $serveur, $option !== false); |
|
| 898 | - if ($r === false) { |
|
| 899 | - spip_sql_erreur($serveur); |
|
| 900 | - } |
|
| 901 | - |
|
| 902 | - return $r; |
|
| 893 | + $f = sql_serveur('updateq', $serveur, $option === 'continue' or $option === false); |
|
| 894 | + if (!is_string($f) or !$f) { |
|
| 895 | + return false; |
|
| 896 | + } |
|
| 897 | + $r = $f($table, $exp, $where, $desc, $serveur, $option !== false); |
|
| 898 | + if ($r === false) { |
|
| 899 | + spip_sql_erreur($serveur); |
|
| 900 | + } |
|
| 901 | + |
|
| 902 | + return $r; |
|
| 903 | 903 | } |
| 904 | 904 | |
| 905 | 905 | /** |
@@ -930,16 +930,16 @@ discard block |
||
| 930 | 930 | * - False en cas d'erreur. |
| 931 | 931 | **/ |
| 932 | 932 | function sql_delete($table, $where = '', $serveur = '', $option = true) { |
| 933 | - $f = sql_serveur('delete', $serveur, $option === 'continue' or $option === false); |
|
| 934 | - if (!is_string($f) or !$f) { |
|
| 935 | - return false; |
|
| 936 | - } |
|
| 937 | - $r = $f($table, $where, $serveur, $option !== false); |
|
| 938 | - if ($r === false) { |
|
| 939 | - spip_sql_erreur($serveur); |
|
| 940 | - } |
|
| 941 | - |
|
| 942 | - return $r; |
|
| 933 | + $f = sql_serveur('delete', $serveur, $option === 'continue' or $option === false); |
|
| 934 | + if (!is_string($f) or !$f) { |
|
| 935 | + return false; |
|
| 936 | + } |
|
| 937 | + $r = $f($table, $where, $serveur, $option !== false); |
|
| 938 | + if ($r === false) { |
|
| 939 | + spip_sql_erreur($serveur); |
|
| 940 | + } |
|
| 941 | + |
|
| 942 | + return $r; |
|
| 943 | 943 | } |
| 944 | 944 | |
| 945 | 945 | /** |
@@ -975,16 +975,16 @@ discard block |
||
| 975 | 975 | * - False en cas d'erreur. |
| 976 | 976 | **/ |
| 977 | 977 | function sql_replace($table, $couples, $desc = array(), $serveur = '', $option = true) { |
| 978 | - $f = sql_serveur('replace', $serveur, $option === 'continue' or $option === false); |
|
| 979 | - if (!is_string($f) or !$f) { |
|
| 980 | - return false; |
|
| 981 | - } |
|
| 982 | - $r = $f($table, $couples, $desc, $serveur, $option !== false); |
|
| 983 | - if ($r === false) { |
|
| 984 | - spip_sql_erreur($serveur); |
|
| 985 | - } |
|
| 986 | - |
|
| 987 | - return $r; |
|
| 978 | + $f = sql_serveur('replace', $serveur, $option === 'continue' or $option === false); |
|
| 979 | + if (!is_string($f) or !$f) { |
|
| 980 | + return false; |
|
| 981 | + } |
|
| 982 | + $r = $f($table, $couples, $desc, $serveur, $option !== false); |
|
| 983 | + if ($r === false) { |
|
| 984 | + spip_sql_erreur($serveur); |
|
| 985 | + } |
|
| 986 | + |
|
| 987 | + return $r; |
|
| 988 | 988 | } |
| 989 | 989 | |
| 990 | 990 | |
@@ -1022,16 +1022,16 @@ discard block |
||
| 1022 | 1022 | * - False en cas d'erreur. |
| 1023 | 1023 | **/ |
| 1024 | 1024 | function sql_replace_multi($table, $tab_couples, $desc = array(), $serveur = '', $option = true) { |
| 1025 | - $f = sql_serveur('replace_multi', $serveur, $option === 'continue' or $option === false); |
|
| 1026 | - if (!is_string($f) or !$f) { |
|
| 1027 | - return false; |
|
| 1028 | - } |
|
| 1029 | - $r = $f($table, $tab_couples, $desc, $serveur, $option !== false); |
|
| 1030 | - if ($r === false) { |
|
| 1031 | - spip_sql_erreur($serveur); |
|
| 1032 | - } |
|
| 1033 | - |
|
| 1034 | - return $r; |
|
| 1025 | + $f = sql_serveur('replace_multi', $serveur, $option === 'continue' or $option === false); |
|
| 1026 | + if (!is_string($f) or !$f) { |
|
| 1027 | + return false; |
|
| 1028 | + } |
|
| 1029 | + $r = $f($table, $tab_couples, $desc, $serveur, $option !== false); |
|
| 1030 | + if ($r === false) { |
|
| 1031 | + spip_sql_erreur($serveur); |
|
| 1032 | + } |
|
| 1033 | + |
|
| 1034 | + return $r; |
|
| 1035 | 1035 | } |
| 1036 | 1036 | |
| 1037 | 1037 | /** |
@@ -1059,16 +1059,16 @@ discard block |
||
| 1059 | 1059 | * - False en cas d'erreur. |
| 1060 | 1060 | **/ |
| 1061 | 1061 | function sql_drop_table($table, $exist = '', $serveur = '', $option = true) { |
| 1062 | - $f = sql_serveur('drop_table', $serveur, $option === 'continue' or $option === false); |
|
| 1063 | - if (!is_string($f) or !$f) { |
|
| 1064 | - return false; |
|
| 1065 | - } |
|
| 1066 | - $r = $f($table, $exist, $serveur, $option !== false); |
|
| 1067 | - if ($r === false) { |
|
| 1068 | - spip_sql_erreur($serveur); |
|
| 1069 | - } |
|
| 1070 | - |
|
| 1071 | - return $r; |
|
| 1062 | + $f = sql_serveur('drop_table', $serveur, $option === 'continue' or $option === false); |
|
| 1063 | + if (!is_string($f) or !$f) { |
|
| 1064 | + return false; |
|
| 1065 | + } |
|
| 1066 | + $r = $f($table, $exist, $serveur, $option !== false); |
|
| 1067 | + if ($r === false) { |
|
| 1068 | + spip_sql_erreur($serveur); |
|
| 1069 | + } |
|
| 1070 | + |
|
| 1071 | + return $r; |
|
| 1072 | 1072 | } |
| 1073 | 1073 | |
| 1074 | 1074 | /** |
@@ -1092,16 +1092,16 @@ discard block |
||
| 1092 | 1092 | * - true si la requête a réussie, false sinon |
| 1093 | 1093 | */ |
| 1094 | 1094 | function sql_drop_view($table, $exist = '', $serveur = '', $option = true) { |
| 1095 | - $f = sql_serveur('drop_view', $serveur, $option === 'continue' or $option === false); |
|
| 1096 | - if (!is_string($f) or !$f) { |
|
| 1097 | - return false; |
|
| 1098 | - } |
|
| 1099 | - $r = $f($table, $exist, $serveur, $option !== false); |
|
| 1100 | - if ($r === false) { |
|
| 1101 | - spip_sql_erreur($serveur); |
|
| 1102 | - } |
|
| 1103 | - |
|
| 1104 | - return $r; |
|
| 1095 | + $f = sql_serveur('drop_view', $serveur, $option === 'continue' or $option === false); |
|
| 1096 | + if (!is_string($f) or !$f) { |
|
| 1097 | + return false; |
|
| 1098 | + } |
|
| 1099 | + $r = $f($table, $exist, $serveur, $option !== false); |
|
| 1100 | + if ($r === false) { |
|
| 1101 | + spip_sql_erreur($serveur); |
|
| 1102 | + } |
|
| 1103 | + |
|
| 1104 | + return $r; |
|
| 1105 | 1105 | } |
| 1106 | 1106 | |
| 1107 | 1107 | /** |
@@ -1125,18 +1125,18 @@ discard block |
||
| 1125 | 1125 | * Ressource à utiliser avec sql_fetch() |
| 1126 | 1126 | **/ |
| 1127 | 1127 | function sql_showbase($spip = null, $serveur = '', $option = true) { |
| 1128 | - $f = sql_serveur('showbase', $serveur, $option === 'continue' or $option === false); |
|
| 1129 | - if (!is_string($f) or !$f) { |
|
| 1130 | - return false; |
|
| 1131 | - } |
|
| 1132 | - |
|
| 1133 | - // la globale n'est remplie qu'apres l'appel de sql_serveur. |
|
| 1134 | - if ($spip == null) { |
|
| 1135 | - $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1136 | - $spip = $connexion['prefixe'] . '\_%'; |
|
| 1137 | - } |
|
| 1138 | - |
|
| 1139 | - return $f($spip, $serveur, $option !== false); |
|
| 1128 | + $f = sql_serveur('showbase', $serveur, $option === 'continue' or $option === false); |
|
| 1129 | + if (!is_string($f) or !$f) { |
|
| 1130 | + return false; |
|
| 1131 | + } |
|
| 1132 | + |
|
| 1133 | + // la globale n'est remplie qu'apres l'appel de sql_serveur. |
|
| 1134 | + if ($spip == null) { |
|
| 1135 | + $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1136 | + $spip = $connexion['prefixe'] . '\_%'; |
|
| 1137 | + } |
|
| 1138 | + |
|
| 1139 | + return $f($spip, $serveur, $option !== false); |
|
| 1140 | 1140 | } |
| 1141 | 1141 | |
| 1142 | 1142 | /** |
@@ -1159,15 +1159,15 @@ discard block |
||
| 1159 | 1159 | * Liste des tables SQL |
| 1160 | 1160 | **/ |
| 1161 | 1161 | function sql_alltable($spip = null, $serveur = '', $option = true) { |
| 1162 | - $q = sql_showbase($spip, $serveur, $option); |
|
| 1163 | - $r = array(); |
|
| 1164 | - if ($q) { |
|
| 1165 | - while ($t = sql_fetch($q, $serveur)) { |
|
| 1166 | - $r[] = array_shift($t); |
|
| 1167 | - } |
|
| 1168 | - } |
|
| 1169 | - |
|
| 1170 | - return $r; |
|
| 1162 | + $q = sql_showbase($spip, $serveur, $option); |
|
| 1163 | + $r = array(); |
|
| 1164 | + if ($q) { |
|
| 1165 | + while ($t = sql_fetch($q, $serveur)) { |
|
| 1166 | + $r[] = array_shift($t); |
|
| 1167 | + } |
|
| 1168 | + } |
|
| 1169 | + |
|
| 1170 | + return $r; |
|
| 1171 | 1171 | } |
| 1172 | 1172 | |
| 1173 | 1173 | /** |
@@ -1200,31 +1200,31 @@ discard block |
||
| 1200 | 1200 | * - 'join' => array() // jointures, si déclarées. |
| 1201 | 1201 | **/ |
| 1202 | 1202 | function sql_showtable($table, $table_spip = false, $serveur = '', $option = true) { |
| 1203 | - $f = sql_serveur('showtable', $serveur, $option === 'continue' or $option === false); |
|
| 1204 | - if (!is_string($f) or !$f) { |
|
| 1205 | - return false; |
|
| 1206 | - } |
|
| 1207 | - |
|
| 1208 | - // la globale n'est remplie qu'apres l'appel de sql_serveur. |
|
| 1209 | - if ($table_spip) { |
|
| 1210 | - $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1211 | - $prefixe = $connexion['prefixe']; |
|
| 1212 | - $vraie_table = prefixer_table_spip($table, $prefixe); |
|
| 1213 | - } else { |
|
| 1214 | - $vraie_table = $table; |
|
| 1215 | - } |
|
| 1216 | - |
|
| 1217 | - $f = $f($vraie_table, $serveur, $option !== false); |
|
| 1218 | - if (!$f) { |
|
| 1219 | - return array(); |
|
| 1220 | - } |
|
| 1221 | - if (isset($GLOBALS['tables_principales'][$table]['join'])) { |
|
| 1222 | - $f['join'] = $GLOBALS['tables_principales'][$table]['join']; |
|
| 1223 | - } elseif (isset($GLOBALS['tables_auxiliaires'][$table]['join'])) { |
|
| 1224 | - $f['join'] = $GLOBALS['tables_auxiliaires'][$table]['join']; |
|
| 1225 | - } |
|
| 1226 | - |
|
| 1227 | - return $f; |
|
| 1203 | + $f = sql_serveur('showtable', $serveur, $option === 'continue' or $option === false); |
|
| 1204 | + if (!is_string($f) or !$f) { |
|
| 1205 | + return false; |
|
| 1206 | + } |
|
| 1207 | + |
|
| 1208 | + // la globale n'est remplie qu'apres l'appel de sql_serveur. |
|
| 1209 | + if ($table_spip) { |
|
| 1210 | + $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
|
| 1211 | + $prefixe = $connexion['prefixe']; |
|
| 1212 | + $vraie_table = prefixer_table_spip($table, $prefixe); |
|
| 1213 | + } else { |
|
| 1214 | + $vraie_table = $table; |
|
| 1215 | + } |
|
| 1216 | + |
|
| 1217 | + $f = $f($vraie_table, $serveur, $option !== false); |
|
| 1218 | + if (!$f) { |
|
| 1219 | + return array(); |
|
| 1220 | + } |
|
| 1221 | + if (isset($GLOBALS['tables_principales'][$table]['join'])) { |
|
| 1222 | + $f['join'] = $GLOBALS['tables_principales'][$table]['join']; |
|
| 1223 | + } elseif (isset($GLOBALS['tables_auxiliaires'][$table]['join'])) { |
|
| 1224 | + $f['join'] = $GLOBALS['tables_auxiliaires'][$table]['join']; |
|
| 1225 | + } |
|
| 1226 | + |
|
| 1227 | + return $f; |
|
| 1228 | 1228 | } |
| 1229 | 1229 | |
| 1230 | 1230 | /** |
@@ -1269,24 +1269,24 @@ discard block |
||
| 1269 | 1269 | * true si succès, false en cas d'echec |
| 1270 | 1270 | **/ |
| 1271 | 1271 | function sql_create( |
| 1272 | - $nom, |
|
| 1273 | - $champs, |
|
| 1274 | - $cles = array(), |
|
| 1275 | - $autoinc = false, |
|
| 1276 | - $temporary = false, |
|
| 1277 | - $serveur = '', |
|
| 1278 | - $option = true |
|
| 1272 | + $nom, |
|
| 1273 | + $champs, |
|
| 1274 | + $cles = array(), |
|
| 1275 | + $autoinc = false, |
|
| 1276 | + $temporary = false, |
|
| 1277 | + $serveur = '', |
|
| 1278 | + $option = true |
|
| 1279 | 1279 | ) { |
| 1280 | - $f = sql_serveur('create', $serveur, $option === 'continue' or $option === false); |
|
| 1281 | - if (!is_string($f) or !$f) { |
|
| 1282 | - return false; |
|
| 1283 | - } |
|
| 1284 | - $r = $f($nom, $champs, $cles, $autoinc, $temporary, $serveur, $option !== false); |
|
| 1285 | - if ($r === false) { |
|
| 1286 | - spip_sql_erreur($serveur); |
|
| 1287 | - } |
|
| 1288 | - |
|
| 1289 | - return $r; |
|
| 1280 | + $f = sql_serveur('create', $serveur, $option === 'continue' or $option === false); |
|
| 1281 | + if (!is_string($f) or !$f) { |
|
| 1282 | + return false; |
|
| 1283 | + } |
|
| 1284 | + $r = $f($nom, $champs, $cles, $autoinc, $temporary, $serveur, $option !== false); |
|
| 1285 | + if ($r === false) { |
|
| 1286 | + spip_sql_erreur($serveur); |
|
| 1287 | + } |
|
| 1288 | + |
|
| 1289 | + return $r; |
|
| 1290 | 1290 | } |
| 1291 | 1291 | |
| 1292 | 1292 | /** |
@@ -1304,16 +1304,16 @@ discard block |
||
| 1304 | 1304 | * @return bool true si la base est créee. |
| 1305 | 1305 | **/ |
| 1306 | 1306 | function sql_create_base($nom, $serveur = '', $option = true) { |
| 1307 | - $f = sql_serveur('create_base', $serveur, $option === 'continue' or $option === false); |
|
| 1308 | - if (!is_string($f) or !$f) { |
|
| 1309 | - return false; |
|
| 1310 | - } |
|
| 1311 | - $r = $f($nom, $serveur, $option !== false); |
|
| 1312 | - if ($r === false) { |
|
| 1313 | - spip_sql_erreur($serveur); |
|
| 1314 | - } |
|
| 1315 | - |
|
| 1316 | - return $r; |
|
| 1307 | + $f = sql_serveur('create_base', $serveur, $option === 'continue' or $option === false); |
|
| 1308 | + if (!is_string($f) or !$f) { |
|
| 1309 | + return false; |
|
| 1310 | + } |
|
| 1311 | + $r = $f($nom, $serveur, $option !== false); |
|
| 1312 | + if ($r === false) { |
|
| 1313 | + spip_sql_erreur($serveur); |
|
| 1314 | + } |
|
| 1315 | + |
|
| 1316 | + return $r; |
|
| 1317 | 1317 | } |
| 1318 | 1318 | |
| 1319 | 1319 | |
@@ -1343,16 +1343,16 @@ discard block |
||
| 1343 | 1343 | * - false en cas d'échec. |
| 1344 | 1344 | **/ |
| 1345 | 1345 | function sql_create_view($nom, $select_query, $serveur = '', $option = true) { |
| 1346 | - $f = sql_serveur('create_view', $serveur, $option === 'continue' or $option === false); |
|
| 1347 | - if (!is_string($f) or !$f) { |
|
| 1348 | - return false; |
|
| 1349 | - } |
|
| 1350 | - $r = $f($nom, $select_query, $serveur, $option !== false); |
|
| 1351 | - if ($r === false) { |
|
| 1352 | - spip_sql_erreur($serveur); |
|
| 1353 | - } |
|
| 1354 | - |
|
| 1355 | - return $r; |
|
| 1346 | + $f = sql_serveur('create_view', $serveur, $option === 'continue' or $option === false); |
|
| 1347 | + if (!is_string($f) or !$f) { |
|
| 1348 | + return false; |
|
| 1349 | + } |
|
| 1350 | + $r = $f($nom, $select_query, $serveur, $option !== false); |
|
| 1351 | + if ($r === false) { |
|
| 1352 | + spip_sql_erreur($serveur); |
|
| 1353 | + } |
|
| 1354 | + |
|
| 1355 | + return $r; |
|
| 1356 | 1356 | } |
| 1357 | 1357 | |
| 1358 | 1358 | /** |
@@ -1381,12 +1381,12 @@ discard block |
||
| 1381 | 1381 | * Texte de sélection pour la requête |
| 1382 | 1382 | */ |
| 1383 | 1383 | function sql_multi($sel, $lang, $serveur = '', $option = true) { |
| 1384 | - $f = sql_serveur('multi', $serveur, $option === 'continue' or $option === false); |
|
| 1385 | - if (!is_string($f) or !$f) { |
|
| 1386 | - return false; |
|
| 1387 | - } |
|
| 1384 | + $f = sql_serveur('multi', $serveur, $option === 'continue' or $option === false); |
|
| 1385 | + if (!is_string($f) or !$f) { |
|
| 1386 | + return false; |
|
| 1387 | + } |
|
| 1388 | 1388 | |
| 1389 | - return $f($sel, $lang); |
|
| 1389 | + return $f($sel, $lang); |
|
| 1390 | 1390 | } |
| 1391 | 1391 | |
| 1392 | 1392 | |
@@ -1401,12 +1401,12 @@ discard block |
||
| 1401 | 1401 | * False si le serveur est indisponible |
| 1402 | 1402 | */ |
| 1403 | 1403 | function sql_error($serveur = '') { |
| 1404 | - $f = sql_serveur('error', $serveur, 'continue'); |
|
| 1405 | - if (!is_string($f) or !$f) { |
|
| 1406 | - return false; |
|
| 1407 | - } |
|
| 1404 | + $f = sql_serveur('error', $serveur, 'continue'); |
|
| 1405 | + if (!is_string($f) or !$f) { |
|
| 1406 | + return false; |
|
| 1407 | + } |
|
| 1408 | 1408 | |
| 1409 | - return $f('query inconnue', $serveur); |
|
| 1409 | + return $f('query inconnue', $serveur); |
|
| 1410 | 1410 | } |
| 1411 | 1411 | |
| 1412 | 1412 | /** |
@@ -1420,12 +1420,12 @@ discard block |
||
| 1420 | 1420 | * False si le serveur est indisponible |
| 1421 | 1421 | */ |
| 1422 | 1422 | function sql_errno($serveur = '') { |
| 1423 | - $f = sql_serveur('errno', $serveur, 'continue'); |
|
| 1424 | - if (!is_string($f) or !$f) { |
|
| 1425 | - return false; |
|
| 1426 | - } |
|
| 1423 | + $f = sql_serveur('errno', $serveur, 'continue'); |
|
| 1424 | + if (!is_string($f) or !$f) { |
|
| 1425 | + return false; |
|
| 1426 | + } |
|
| 1427 | 1427 | |
| 1428 | - return $f($serveur); |
|
| 1428 | + return $f($serveur); |
|
| 1429 | 1429 | } |
| 1430 | 1430 | |
| 1431 | 1431 | /** |
@@ -1443,16 +1443,16 @@ discard block |
||
| 1443 | 1443 | * @return array Tableau de l'explication |
| 1444 | 1444 | */ |
| 1445 | 1445 | function sql_explain($q, $serveur = '', $option = true) { |
| 1446 | - $f = sql_serveur('explain', $serveur, 'continue'); |
|
| 1447 | - if (!is_string($f) or !$f) { |
|
| 1448 | - return false; |
|
| 1449 | - } |
|
| 1450 | - $r = $f($q, $serveur, $option !== false); |
|
| 1451 | - if ($r === false) { |
|
| 1452 | - spip_sql_erreur($serveur); |
|
| 1453 | - } |
|
| 1454 | - |
|
| 1455 | - return $r; |
|
| 1446 | + $f = sql_serveur('explain', $serveur, 'continue'); |
|
| 1447 | + if (!is_string($f) or !$f) { |
|
| 1448 | + return false; |
|
| 1449 | + } |
|
| 1450 | + $r = $f($q, $serveur, $option !== false); |
|
| 1451 | + if ($r === false) { |
|
| 1452 | + spip_sql_erreur($serveur); |
|
| 1453 | + } |
|
| 1454 | + |
|
| 1455 | + return $r; |
|
| 1456 | 1456 | } |
| 1457 | 1457 | |
| 1458 | 1458 | /** |
@@ -1470,16 +1470,16 @@ discard block |
||
| 1470 | 1470 | * @return bool Toujours true |
| 1471 | 1471 | */ |
| 1472 | 1472 | function sql_optimize($table, $serveur = '', $option = true) { |
| 1473 | - $f = sql_serveur('optimize', $serveur, $option === 'continue' or $option === false); |
|
| 1474 | - if (!is_string($f) or !$f) { |
|
| 1475 | - return false; |
|
| 1476 | - } |
|
| 1477 | - $r = $f($table, $serveur, $option !== false); |
|
| 1478 | - if ($r === false) { |
|
| 1479 | - spip_sql_erreur($serveur); |
|
| 1480 | - } |
|
| 1481 | - |
|
| 1482 | - return $r; |
|
| 1473 | + $f = sql_serveur('optimize', $serveur, $option === 'continue' or $option === false); |
|
| 1474 | + if (!is_string($f) or !$f) { |
|
| 1475 | + return false; |
|
| 1476 | + } |
|
| 1477 | + $r = $f($table, $serveur, $option !== false); |
|
| 1478 | + if ($r === false) { |
|
| 1479 | + spip_sql_erreur($serveur); |
|
| 1480 | + } |
|
| 1481 | + |
|
| 1482 | + return $r; |
|
| 1483 | 1483 | } |
| 1484 | 1484 | |
| 1485 | 1485 | /** |
@@ -1499,16 +1499,16 @@ discard block |
||
| 1499 | 1499 | * - true si la requête a réussie, false sinon |
| 1500 | 1500 | */ |
| 1501 | 1501 | function sql_repair($table, $serveur = '', $option = true) { |
| 1502 | - $f = sql_serveur('repair', $serveur, $option === 'continue' or $option === false); |
|
| 1503 | - if (!is_string($f) or !$f) { |
|
| 1504 | - return false; |
|
| 1505 | - } |
|
| 1506 | - $r = $f($table, $serveur, $option !== false); |
|
| 1507 | - if ($r === false) { |
|
| 1508 | - spip_sql_erreur($serveur); |
|
| 1509 | - } |
|
| 1510 | - |
|
| 1511 | - return $r; |
|
| 1502 | + $f = sql_serveur('repair', $serveur, $option === 'continue' or $option === false); |
|
| 1503 | + if (!is_string($f) or !$f) { |
|
| 1504 | + return false; |
|
| 1505 | + } |
|
| 1506 | + $r = $f($table, $serveur, $option !== false); |
|
| 1507 | + if ($r === false) { |
|
| 1508 | + spip_sql_erreur($serveur); |
|
| 1509 | + } |
|
| 1510 | + |
|
| 1511 | + return $r; |
|
| 1512 | 1512 | } |
| 1513 | 1513 | |
| 1514 | 1514 | |
@@ -1533,16 +1533,16 @@ discard block |
||
| 1533 | 1533 | * - array : Tableau décrivant requête et temps d'exécution si var_profile actif pour tracer. |
| 1534 | 1534 | */ |
| 1535 | 1535 | function sql_query($ins, $serveur = '', $option = true) { |
| 1536 | - $f = sql_serveur('query', $serveur, $option === 'continue' or $option === false); |
|
| 1537 | - if (!is_string($f) or !$f) { |
|
| 1538 | - return false; |
|
| 1539 | - } |
|
| 1540 | - $r = $f($ins, $serveur, $option !== false); |
|
| 1541 | - if ($r === false) { |
|
| 1542 | - spip_sql_erreur($serveur); |
|
| 1543 | - } |
|
| 1544 | - |
|
| 1545 | - return $r; |
|
| 1536 | + $f = sql_serveur('query', $serveur, $option === 'continue' or $option === false); |
|
| 1537 | + if (!is_string($f) or !$f) { |
|
| 1538 | + return false; |
|
| 1539 | + } |
|
| 1540 | + $r = $f($ins, $serveur, $option !== false); |
|
| 1541 | + if ($r === false) { |
|
| 1542 | + spip_sql_erreur($serveur); |
|
| 1543 | + } |
|
| 1544 | + |
|
| 1545 | + return $r; |
|
| 1546 | 1546 | } |
| 1547 | 1547 | |
| 1548 | 1548 | /** |
@@ -1590,27 +1590,27 @@ discard block |
||
| 1590 | 1590 | * |
| 1591 | 1591 | **/ |
| 1592 | 1592 | function sql_fetsel( |
| 1593 | - $select = array(), |
|
| 1594 | - $from = array(), |
|
| 1595 | - $where = array(), |
|
| 1596 | - $groupby = array(), |
|
| 1597 | - $orderby = array(), |
|
| 1598 | - $limit = '', |
|
| 1599 | - $having = array(), |
|
| 1600 | - $serveur = '', |
|
| 1601 | - $option = true |
|
| 1593 | + $select = array(), |
|
| 1594 | + $from = array(), |
|
| 1595 | + $where = array(), |
|
| 1596 | + $groupby = array(), |
|
| 1597 | + $orderby = array(), |
|
| 1598 | + $limit = '', |
|
| 1599 | + $having = array(), |
|
| 1600 | + $serveur = '', |
|
| 1601 | + $option = true |
|
| 1602 | 1602 | ) { |
| 1603 | - $q = sql_select($select, $from, $where, $groupby, $orderby, $limit, $having, $serveur, $option); |
|
| 1604 | - if ($option === false) { |
|
| 1605 | - return $q; |
|
| 1606 | - } |
|
| 1607 | - if (!$q) { |
|
| 1608 | - return array(); |
|
| 1609 | - } |
|
| 1610 | - $r = sql_fetch($q, $serveur, $option); |
|
| 1611 | - sql_free($q, $serveur, $option); |
|
| 1612 | - |
|
| 1613 | - return $r; |
|
| 1603 | + $q = sql_select($select, $from, $where, $groupby, $orderby, $limit, $having, $serveur, $option); |
|
| 1604 | + if ($option === false) { |
|
| 1605 | + return $q; |
|
| 1606 | + } |
|
| 1607 | + if (!$q) { |
|
| 1608 | + return array(); |
|
| 1609 | + } |
|
| 1610 | + $r = sql_fetch($q, $serveur, $option); |
|
| 1611 | + sql_free($q, $serveur, $option); |
|
| 1612 | + |
|
| 1613 | + return $r; |
|
| 1614 | 1614 | } |
| 1615 | 1615 | |
| 1616 | 1616 | |
@@ -1667,22 +1667,22 @@ discard block |
||
| 1667 | 1667 | * |
| 1668 | 1668 | **/ |
| 1669 | 1669 | function sql_allfetsel( |
| 1670 | - $select = array(), |
|
| 1671 | - $from = array(), |
|
| 1672 | - $where = array(), |
|
| 1673 | - $groupby = array(), |
|
| 1674 | - $orderby = array(), |
|
| 1675 | - $limit = '', |
|
| 1676 | - $having = array(), |
|
| 1677 | - $serveur = '', |
|
| 1678 | - $option = true |
|
| 1670 | + $select = array(), |
|
| 1671 | + $from = array(), |
|
| 1672 | + $where = array(), |
|
| 1673 | + $groupby = array(), |
|
| 1674 | + $orderby = array(), |
|
| 1675 | + $limit = '', |
|
| 1676 | + $having = array(), |
|
| 1677 | + $serveur = '', |
|
| 1678 | + $option = true |
|
| 1679 | 1679 | ) { |
| 1680 | - $q = sql_select($select, $from, $where, $groupby, $orderby, $limit, $having, $serveur, $option); |
|
| 1681 | - if ($option === false) { |
|
| 1682 | - return $q; |
|
| 1683 | - } |
|
| 1680 | + $q = sql_select($select, $from, $where, $groupby, $orderby, $limit, $having, $serveur, $option); |
|
| 1681 | + if ($option === false) { |
|
| 1682 | + return $q; |
|
| 1683 | + } |
|
| 1684 | 1684 | |
| 1685 | - return sql_fetch_all($q, $serveur, $option); |
|
| 1685 | + return sql_fetch_all($q, $serveur, $option); |
|
| 1686 | 1686 | } |
| 1687 | 1687 | |
| 1688 | 1688 | |
@@ -1729,33 +1729,33 @@ discard block |
||
| 1729 | 1729 | * |
| 1730 | 1730 | **/ |
| 1731 | 1731 | function sql_getfetsel( |
| 1732 | - $select, |
|
| 1733 | - $from = array(), |
|
| 1734 | - $where = array(), |
|
| 1735 | - $groupby = array(), |
|
| 1736 | - $orderby = array(), |
|
| 1737 | - $limit = '', |
|
| 1738 | - $having = array(), |
|
| 1739 | - $serveur = '', |
|
| 1740 | - $option = true |
|
| 1732 | + $select, |
|
| 1733 | + $from = array(), |
|
| 1734 | + $where = array(), |
|
| 1735 | + $groupby = array(), |
|
| 1736 | + $orderby = array(), |
|
| 1737 | + $limit = '', |
|
| 1738 | + $having = array(), |
|
| 1739 | + $serveur = '', |
|
| 1740 | + $option = true |
|
| 1741 | 1741 | ) { |
| 1742 | - if (preg_match('/\s+as\s+(\w+)$/i', $select, $c)) { |
|
| 1743 | - $id = $c[1]; |
|
| 1744 | - } elseif (!preg_match('/\W/', $select)) { |
|
| 1745 | - $id = $select; |
|
| 1746 | - } else { |
|
| 1747 | - $id = 'n'; |
|
| 1748 | - $select .= ' AS n'; |
|
| 1749 | - } |
|
| 1750 | - $r = sql_fetsel($select, $from, $where, $groupby, $orderby, $limit, $having, $serveur, $option); |
|
| 1751 | - if ($option === false) { |
|
| 1752 | - return $r; |
|
| 1753 | - } |
|
| 1754 | - if (!$r) { |
|
| 1755 | - return null; |
|
| 1756 | - } |
|
| 1757 | - |
|
| 1758 | - return $r[$id]; |
|
| 1742 | + if (preg_match('/\s+as\s+(\w+)$/i', $select, $c)) { |
|
| 1743 | + $id = $c[1]; |
|
| 1744 | + } elseif (!preg_match('/\W/', $select)) { |
|
| 1745 | + $id = $select; |
|
| 1746 | + } else { |
|
| 1747 | + $id = 'n'; |
|
| 1748 | + $select .= ' AS n'; |
|
| 1749 | + } |
|
| 1750 | + $r = sql_fetsel($select, $from, $where, $groupby, $orderby, $limit, $having, $serveur, $option); |
|
| 1751 | + if ($option === false) { |
|
| 1752 | + return $r; |
|
| 1753 | + } |
|
| 1754 | + if (!$r) { |
|
| 1755 | + return null; |
|
| 1756 | + } |
|
| 1757 | + |
|
| 1758 | + return $r[$id]; |
|
| 1759 | 1759 | } |
| 1760 | 1760 | |
| 1761 | 1761 | /** |
@@ -1773,9 +1773,9 @@ discard block |
||
| 1773 | 1773 | * Numero de version du serveur SQL |
| 1774 | 1774 | **/ |
| 1775 | 1775 | function sql_version($serveur = '', $option = true) { |
| 1776 | - $row = sql_fetsel("version() AS n", '', '', '', '', '', '', $serveur); |
|
| 1776 | + $row = sql_fetsel("version() AS n", '', '', '', '', '', '', $serveur); |
|
| 1777 | 1777 | |
| 1778 | - return ($row['n']); |
|
| 1778 | + return ($row['n']); |
|
| 1779 | 1779 | } |
| 1780 | 1780 | |
| 1781 | 1781 | /** |
@@ -1811,16 +1811,16 @@ discard block |
||
| 1811 | 1811 | * Le serveur SQL prefere t'il des transactions pour les insertions multiples ? |
| 1812 | 1812 | **/ |
| 1813 | 1813 | function sql_preferer_transaction($serveur = '', $option = true) { |
| 1814 | - $f = sql_serveur('preferer_transaction', $serveur, 'continue'); |
|
| 1815 | - if (!is_string($f) or !$f) { |
|
| 1816 | - return false; |
|
| 1817 | - } |
|
| 1818 | - $r = $f($serveur, $option !== false); |
|
| 1819 | - if ($r === false) { |
|
| 1820 | - spip_sql_erreur($serveur); |
|
| 1821 | - } |
|
| 1822 | - |
|
| 1823 | - return $r; |
|
| 1814 | + $f = sql_serveur('preferer_transaction', $serveur, 'continue'); |
|
| 1815 | + if (!is_string($f) or !$f) { |
|
| 1816 | + return false; |
|
| 1817 | + } |
|
| 1818 | + $r = $f($serveur, $option !== false); |
|
| 1819 | + if ($r === false) { |
|
| 1820 | + spip_sql_erreur($serveur); |
|
| 1821 | + } |
|
| 1822 | + |
|
| 1823 | + return $r; |
|
| 1824 | 1824 | } |
| 1825 | 1825 | |
| 1826 | 1826 | ; |
@@ -1845,16 +1845,16 @@ discard block |
||
| 1845 | 1845 | * false en cas d'erreur |
| 1846 | 1846 | **/ |
| 1847 | 1847 | function sql_demarrer_transaction($serveur = '', $option = true) { |
| 1848 | - $f = sql_serveur('demarrer_transaction', $serveur, 'continue'); |
|
| 1849 | - if (!is_string($f) or !$f) { |
|
| 1850 | - return false; |
|
| 1851 | - } |
|
| 1852 | - $r = $f($serveur, $option !== false); |
|
| 1853 | - if ($r === false) { |
|
| 1854 | - spip_sql_erreur($serveur); |
|
| 1855 | - } |
|
| 1856 | - |
|
| 1857 | - return $r; |
|
| 1848 | + $f = sql_serveur('demarrer_transaction', $serveur, 'continue'); |
|
| 1849 | + if (!is_string($f) or !$f) { |
|
| 1850 | + return false; |
|
| 1851 | + } |
|
| 1852 | + $r = $f($serveur, $option !== false); |
|
| 1853 | + if ($r === false) { |
|
| 1854 | + spip_sql_erreur($serveur); |
|
| 1855 | + } |
|
| 1856 | + |
|
| 1857 | + return $r; |
|
| 1858 | 1858 | } |
| 1859 | 1859 | |
| 1860 | 1860 | ; |
@@ -1879,16 +1879,16 @@ discard block |
||
| 1879 | 1879 | * false en cas d'erreur |
| 1880 | 1880 | **/ |
| 1881 | 1881 | function sql_terminer_transaction($serveur = '', $option = true) { |
| 1882 | - $f = sql_serveur('terminer_transaction', $serveur, 'continue'); |
|
| 1883 | - if (!is_string($f) or !$f) { |
|
| 1884 | - return false; |
|
| 1885 | - } |
|
| 1886 | - $r = $f($serveur, $option !== false); |
|
| 1887 | - if ($r === false) { |
|
| 1888 | - spip_sql_erreur($serveur); |
|
| 1889 | - } |
|
| 1890 | - |
|
| 1891 | - return $r; |
|
| 1882 | + $f = sql_serveur('terminer_transaction', $serveur, 'continue'); |
|
| 1883 | + if (!is_string($f) or !$f) { |
|
| 1884 | + return false; |
|
| 1885 | + } |
|
| 1886 | + $r = $f($serveur, $option !== false); |
|
| 1887 | + if ($r === false) { |
|
| 1888 | + spip_sql_erreur($serveur); |
|
| 1889 | + } |
|
| 1890 | + |
|
| 1891 | + return $r; |
|
| 1892 | 1892 | } |
| 1893 | 1893 | |
| 1894 | 1894 | ; |
@@ -1915,12 +1915,12 @@ discard block |
||
| 1915 | 1915 | * Valeur hexadécimale attendue par le serveur SQL |
| 1916 | 1916 | **/ |
| 1917 | 1917 | function sql_hex($val, $serveur = '', $option = true) { |
| 1918 | - $f = sql_serveur('hex', $serveur, $option === 'continue' or $option === false); |
|
| 1919 | - if (!is_string($f) or !$f) { |
|
| 1920 | - return false; |
|
| 1921 | - } |
|
| 1918 | + $f = sql_serveur('hex', $serveur, $option === 'continue' or $option === false); |
|
| 1919 | + if (!is_string($f) or !$f) { |
|
| 1920 | + return false; |
|
| 1921 | + } |
|
| 1922 | 1922 | |
| 1923 | - return $f($val); |
|
| 1923 | + return $f($val); |
|
| 1924 | 1924 | } |
| 1925 | 1925 | |
| 1926 | 1926 | /** |
@@ -1946,12 +1946,12 @@ discard block |
||
| 1946 | 1946 | * La chaine echappee |
| 1947 | 1947 | **/ |
| 1948 | 1948 | function sql_quote($val, $serveur = '', $type = '') { |
| 1949 | - $f = sql_serveur('quote', $serveur, true); |
|
| 1950 | - if (!is_string($f) or !$f) { |
|
| 1951 | - $f = '_q'; |
|
| 1952 | - } |
|
| 1949 | + $f = sql_serveur('quote', $serveur, true); |
|
| 1950 | + if (!is_string($f) or !$f) { |
|
| 1951 | + $f = '_q'; |
|
| 1952 | + } |
|
| 1953 | 1953 | |
| 1954 | - return $f($val, $type); |
|
| 1954 | + return $f($val, $type); |
|
| 1955 | 1955 | } |
| 1956 | 1956 | |
| 1957 | 1957 | /** |
@@ -1976,12 +1976,12 @@ discard block |
||
| 1976 | 1976 | * - false si le serveur SQL est indisponible |
| 1977 | 1977 | **/ |
| 1978 | 1978 | function sql_date_proche($champ, $interval, $unite, $serveur = '', $option = true) { |
| 1979 | - $f = sql_serveur('date_proche', $serveur, true); |
|
| 1980 | - if (!is_string($f) or !$f) { |
|
| 1981 | - return false; |
|
| 1982 | - } |
|
| 1979 | + $f = sql_serveur('date_proche', $serveur, true); |
|
| 1980 | + if (!is_string($f) or !$f) { |
|
| 1981 | + return false; |
|
| 1982 | + } |
|
| 1983 | 1983 | |
| 1984 | - return $f($champ, $interval, $unite); |
|
| 1984 | + return $f($champ, $interval, $unite); |
|
| 1985 | 1985 | } |
| 1986 | 1986 | |
| 1987 | 1987 | /** |
@@ -2015,30 +2015,30 @@ discard block |
||
| 2015 | 2015 | * Expression de requête SQL |
| 2016 | 2016 | **/ |
| 2017 | 2017 | function sql_in($val, $valeurs, $not = '', $serveur = '', $option = true) { |
| 2018 | - if (is_string($valeurs)) { |
|
| 2019 | - if(isset($valeurs[0]) and $valeurs[0] === ',') { |
|
| 2020 | - $valeurs = substr($valeurs, 1); |
|
| 2021 | - } |
|
| 2022 | - // on explode en tableau pour pouvoir securiser le contenu |
|
| 2023 | - $valeurs = explode(',', $valeurs); |
|
| 2024 | - } |
|
| 2025 | - $f = sql_serveur('quote', $serveur, true); |
|
| 2026 | - if (!is_string($f) or !$f) { |
|
| 2027 | - return false; |
|
| 2028 | - } |
|
| 2029 | - // sql_quote produit une chaine dans tous les cas |
|
| 2030 | - $valeurs = $f(array_unique($valeurs)); |
|
| 2031 | - |
|
| 2032 | - if (!strlen(trim($valeurs))) { |
|
| 2033 | - return ($not ? "0=0" : '0=1'); |
|
| 2034 | - } |
|
| 2035 | - |
|
| 2036 | - $f = sql_serveur('in', $serveur, $option === 'continue' or $option === false); |
|
| 2037 | - if (!is_string($f) or !$f) { |
|
| 2038 | - return false; |
|
| 2039 | - } |
|
| 2040 | - |
|
| 2041 | - return $f($val, $valeurs, $not, $serveur, $option !== false); |
|
| 2018 | + if (is_string($valeurs)) { |
|
| 2019 | + if(isset($valeurs[0]) and $valeurs[0] === ',') { |
|
| 2020 | + $valeurs = substr($valeurs, 1); |
|
| 2021 | + } |
|
| 2022 | + // on explode en tableau pour pouvoir securiser le contenu |
|
| 2023 | + $valeurs = explode(',', $valeurs); |
|
| 2024 | + } |
|
| 2025 | + $f = sql_serveur('quote', $serveur, true); |
|
| 2026 | + if (!is_string($f) or !$f) { |
|
| 2027 | + return false; |
|
| 2028 | + } |
|
| 2029 | + // sql_quote produit une chaine dans tous les cas |
|
| 2030 | + $valeurs = $f(array_unique($valeurs)); |
|
| 2031 | + |
|
| 2032 | + if (!strlen(trim($valeurs))) { |
|
| 2033 | + return ($not ? "0=0" : '0=1'); |
|
| 2034 | + } |
|
| 2035 | + |
|
| 2036 | + $f = sql_serveur('in', $serveur, $option === 'continue' or $option === false); |
|
| 2037 | + if (!is_string($f) or !$f) { |
|
| 2038 | + return false; |
|
| 2039 | + } |
|
| 2040 | + |
|
| 2041 | + return $f($val, $valeurs, $not, $serveur, $option !== false); |
|
| 2042 | 2042 | } |
| 2043 | 2043 | |
| 2044 | 2044 | |
@@ -2078,24 +2078,24 @@ discard block |
||
| 2078 | 2078 | * Expression de requête SQL |
| 2079 | 2079 | **/ |
| 2080 | 2080 | function sql_in_select( |
| 2081 | - $in, |
|
| 2082 | - $select, |
|
| 2083 | - $from = array(), |
|
| 2084 | - $where = array(), |
|
| 2085 | - $groupby = array(), |
|
| 2086 | - $orderby = array(), |
|
| 2087 | - $limit = '', |
|
| 2088 | - $having = array(), |
|
| 2089 | - $serveur = '' |
|
| 2081 | + $in, |
|
| 2082 | + $select, |
|
| 2083 | + $from = array(), |
|
| 2084 | + $where = array(), |
|
| 2085 | + $groupby = array(), |
|
| 2086 | + $orderby = array(), |
|
| 2087 | + $limit = '', |
|
| 2088 | + $having = array(), |
|
| 2089 | + $serveur = '' |
|
| 2090 | 2090 | ) { |
| 2091 | - $liste = array(); |
|
| 2092 | - $res = sql_select($select, $from, $where, $groupby, $orderby, $limit, $having, $serveur); |
|
| 2093 | - while ($r = sql_fetch($res)) { |
|
| 2094 | - $liste[] = array_shift($r); |
|
| 2095 | - } |
|
| 2096 | - sql_free($res); |
|
| 2097 | - |
|
| 2098 | - return sql_in($in, $liste); |
|
| 2091 | + $liste = array(); |
|
| 2092 | + $res = sql_select($select, $from, $where, $groupby, $orderby, $limit, $having, $serveur); |
|
| 2093 | + while ($r = sql_fetch($res)) { |
|
| 2094 | + $liste[] = array_shift($r); |
|
| 2095 | + } |
|
| 2096 | + sql_free($res); |
|
| 2097 | + |
|
| 2098 | + return sql_in($in, $liste); |
|
| 2099 | 2099 | } |
| 2100 | 2100 | |
| 2101 | 2101 | /** |
@@ -2127,29 +2127,29 @@ discard block |
||
| 2127 | 2127 | * Position apres le saut. |
| 2128 | 2128 | */ |
| 2129 | 2129 | function sql_skip($res, $pos, $saut, $count, $serveur = '', $option = true) { |
| 2130 | - // pas de saut en arriere qu'on ne sait pas faire sans sql_seek |
|
| 2131 | - if (($saut = intval($saut)) <= 0) { |
|
| 2132 | - return $pos; |
|
| 2133 | - } |
|
| 2134 | - |
|
| 2135 | - $seek = $pos + $saut; |
|
| 2136 | - // si le saut fait depasser le maxi, on libere la resource |
|
| 2137 | - // et on sort |
|
| 2138 | - if ($seek >= $count) { |
|
| 2139 | - sql_free($res, $serveur, $option); |
|
| 2140 | - |
|
| 2141 | - return $count; |
|
| 2142 | - } |
|
| 2143 | - |
|
| 2144 | - if (sql_seek($res, $seek)) { |
|
| 2145 | - $pos = $seek; |
|
| 2146 | - } else { |
|
| 2147 | - while ($pos < $seek and sql_fetch($res, $serveur, $option)) { |
|
| 2148 | - $pos++; |
|
| 2149 | - } |
|
| 2150 | - } |
|
| 2151 | - |
|
| 2152 | - return $pos; |
|
| 2130 | + // pas de saut en arriere qu'on ne sait pas faire sans sql_seek |
|
| 2131 | + if (($saut = intval($saut)) <= 0) { |
|
| 2132 | + return $pos; |
|
| 2133 | + } |
|
| 2134 | + |
|
| 2135 | + $seek = $pos + $saut; |
|
| 2136 | + // si le saut fait depasser le maxi, on libere la resource |
|
| 2137 | + // et on sort |
|
| 2138 | + if ($seek >= $count) { |
|
| 2139 | + sql_free($res, $serveur, $option); |
|
| 2140 | + |
|
| 2141 | + return $count; |
|
| 2142 | + } |
|
| 2143 | + |
|
| 2144 | + if (sql_seek($res, $seek)) { |
|
| 2145 | + $pos = $seek; |
|
| 2146 | + } else { |
|
| 2147 | + while ($pos < $seek and sql_fetch($res, $serveur, $option)) { |
|
| 2148 | + $pos++; |
|
| 2149 | + } |
|
| 2150 | + } |
|
| 2151 | + |
|
| 2152 | + return $pos; |
|
| 2153 | 2153 | } |
| 2154 | 2154 | |
| 2155 | 2155 | |
@@ -2169,7 +2169,7 @@ discard block |
||
| 2169 | 2169 | * True si le champ est de type entier |
| 2170 | 2170 | */ |
| 2171 | 2171 | function sql_test_int($type, $serveur = '', $option = true) { |
| 2172 | - return preg_match('/^(TINYINT|SMALLINT|MEDIUMINT|INT|INTEGER|BIGINT)/i', trim($type)); |
|
| 2172 | + return preg_match('/^(TINYINT|SMALLINT|MEDIUMINT|INT|INTEGER|BIGINT)/i', trim($type)); |
|
| 2173 | 2173 | } |
| 2174 | 2174 | |
| 2175 | 2175 | /** |
@@ -2188,7 +2188,7 @@ discard block |
||
| 2188 | 2188 | * True si le champ est de type entier |
| 2189 | 2189 | */ |
| 2190 | 2190 | function sql_test_date($type, $serveur = '', $option = true) { |
| 2191 | - return preg_match('/^(DATE|DATETIME|TIMESTAMP|TIME)/i', trim($type)); |
|
| 2191 | + return preg_match('/^(DATE|DATETIME|TIMESTAMP|TIME)/i', trim($type)); |
|
| 2192 | 2192 | } |
| 2193 | 2193 | |
| 2194 | 2194 | /** |
@@ -2210,19 +2210,19 @@ discard block |
||
| 2210 | 2210 | * La date formatee |
| 2211 | 2211 | */ |
| 2212 | 2212 | function sql_format_date($annee = 0, $mois = 0, $jour = 0, $h = 0, $m = 0, $s = 0, $serveur = '') { |
| 2213 | - $annee = sprintf("%04s", $annee); |
|
| 2214 | - $mois = sprintf("%02s", $mois); |
|
| 2215 | - |
|
| 2216 | - if ($annee == "0000") { |
|
| 2217 | - $mois = 0; |
|
| 2218 | - } |
|
| 2219 | - if ($mois == "00") { |
|
| 2220 | - $jour = 0; |
|
| 2221 | - } |
|
| 2222 | - |
|
| 2223 | - return sprintf("%04u", $annee) . '-' . sprintf("%02u", $mois) . '-' |
|
| 2224 | - . sprintf("%02u", $jour) . ' ' . sprintf("%02u", $h) . ':' |
|
| 2225 | - . sprintf("%02u", $m) . ':' . sprintf("%02u", $s); |
|
| 2213 | + $annee = sprintf("%04s", $annee); |
|
| 2214 | + $mois = sprintf("%02s", $mois); |
|
| 2215 | + |
|
| 2216 | + if ($annee == "0000") { |
|
| 2217 | + $mois = 0; |
|
| 2218 | + } |
|
| 2219 | + if ($mois == "00") { |
|
| 2220 | + $jour = 0; |
|
| 2221 | + } |
|
| 2222 | + |
|
| 2223 | + return sprintf("%04u", $annee) . '-' . sprintf("%02u", $mois) . '-' |
|
| 2224 | + . sprintf("%02u", $jour) . ' ' . sprintf("%02u", $h) . ':' |
|
| 2225 | + . sprintf("%02u", $m) . ':' . sprintf("%02u", $s); |
|
| 2226 | 2226 | } |
| 2227 | 2227 | |
| 2228 | 2228 | |
@@ -2245,32 +2245,32 @@ discard block |
||
| 2245 | 2245 | **/ |
| 2246 | 2246 | function description_table($nom, $serveur = '') { |
| 2247 | 2247 | |
| 2248 | - static $trouver_table; |
|
| 2248 | + static $trouver_table; |
|
| 2249 | 2249 | |
| 2250 | - /* toujours utiliser trouver_table |
|
| 2250 | + /* toujours utiliser trouver_table |
|
| 2251 | 2251 | qui renverra la description theorique |
| 2252 | 2252 | car sinon on va se comporter differement selon que la table est declaree |
| 2253 | 2253 | ou non |
| 2254 | 2254 | */ |
| 2255 | - if (!$trouver_table) { |
|
| 2256 | - $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 2257 | - } |
|
| 2258 | - if ($desc = $trouver_table($nom, $serveur)) { |
|
| 2259 | - return $desc; |
|
| 2260 | - } |
|
| 2261 | - |
|
| 2262 | - // sauf a l'installation : |
|
| 2263 | - include_spip('base/serial'); |
|
| 2264 | - if (isset($GLOBALS['tables_principales'][$nom])) { |
|
| 2265 | - return $GLOBALS['tables_principales'][$nom]; |
|
| 2266 | - } |
|
| 2267 | - |
|
| 2268 | - include_spip('base/auxiliaires'); |
|
| 2269 | - if (isset($GLOBALS['tables_auxiliaires'][$nom])) { |
|
| 2270 | - return $GLOBALS['tables_auxiliaires'][$nom]; |
|
| 2271 | - } |
|
| 2272 | - |
|
| 2273 | - return false; |
|
| 2255 | + if (!$trouver_table) { |
|
| 2256 | + $trouver_table = charger_fonction('trouver_table', 'base'); |
|
| 2257 | + } |
|
| 2258 | + if ($desc = $trouver_table($nom, $serveur)) { |
|
| 2259 | + return $desc; |
|
| 2260 | + } |
|
| 2261 | + |
|
| 2262 | + // sauf a l'installation : |
|
| 2263 | + include_spip('base/serial'); |
|
| 2264 | + if (isset($GLOBALS['tables_principales'][$nom])) { |
|
| 2265 | + return $GLOBALS['tables_principales'][$nom]; |
|
| 2266 | + } |
|
| 2267 | + |
|
| 2268 | + include_spip('base/auxiliaires'); |
|
| 2269 | + if (isset($GLOBALS['tables_auxiliaires'][$nom])) { |
|
| 2270 | + return $GLOBALS['tables_auxiliaires'][$nom]; |
|
| 2271 | + } |
|
| 2272 | + |
|
| 2273 | + return false; |
|
| 2274 | 2274 | } |
| 2275 | 2275 | |
| 2276 | 2276 | /** |
@@ -2283,8 +2283,8 @@ discard block |
||
| 2283 | 2283 | * @return string Table sql éventuellement renommée |
| 2284 | 2284 | */ |
| 2285 | 2285 | function prefixer_table_spip($table, $prefixe) { |
| 2286 | - if ($prefixe) { |
|
| 2287 | - $table = preg_replace('/^spip_/', $prefixe . '_', $table); |
|
| 2288 | - } |
|
| 2289 | - return $table; |
|
| 2286 | + if ($prefixe) { |
|
| 2287 | + $table = preg_replace('/^spip_/', $prefixe . '_', $table); |
|
| 2288 | + } |
|
| 2289 | + return $table; |
|
| 2290 | 2290 | } |
| 2291 | 2291 | \ No newline at end of file |
@@ -53,10 +53,10 @@ discard block |
||
| 53 | 53 | |
| 54 | 54 | if ($compil_info) { |
| 55 | 55 | $contexte_compil = array( |
| 56 | - $trace[0]['file'],// sourcefile |
|
| 56 | + $trace[0]['file'], // sourcefile |
|
| 57 | 57 | '', //nom |
| 58 | - (isset($trace[1]) ? $trace[1]['function'] . "(){\n" : '') |
|
| 59 | - . $trace[0]['function'] . "();" |
|
| 58 | + (isset($trace[1]) ? $trace[1]['function']."(){\n" : '') |
|
| 59 | + . $trace[0]['function']."();" |
|
| 60 | 60 | . (isset($trace[1]) ? "\n}" : ''), //id_boucle |
| 61 | 61 | $trace[0]['line'], // ligne |
| 62 | 62 | $GLOBALS['spip_lang'], // lang |
@@ -65,7 +65,7 @@ discard block |
||
| 65 | 65 | return $contexte_compil; |
| 66 | 66 | } |
| 67 | 67 | |
| 68 | - $message = count($trace) ? $trace[0]['file'] . " L" . $trace[0]['line'] : ""; |
|
| 68 | + $message = count($trace) ? $trace[0]['file']." L".$trace[0]['line'] : ""; |
|
| 69 | 69 | $f = array(); |
| 70 | 70 | while (count($trace) and $t = array_shift($trace)) { |
| 71 | 71 | if (in_array($t['function'], array('include_once', 'include_spip', 'find_in_path'))) { |
@@ -74,7 +74,7 @@ discard block |
||
| 74 | 74 | $f[] = $t['function']; |
| 75 | 75 | } |
| 76 | 76 | if (count($f)) { |
| 77 | - $message .= " [" . implode("(),", $f) . "()]"; |
|
| 77 | + $message .= " [".implode("(),", $f)."()]"; |
|
| 78 | 78 | } |
| 79 | 79 | |
| 80 | 80 | return $message; |
@@ -255,8 +255,8 @@ discard block |
||
| 255 | 255 | } |
| 256 | 256 | // le debug, c'est pour ce qui a ete produit par le compilateur |
| 257 | 257 | if (isset($GLOBALS['debug']['aucasou'])) { |
| 258 | - list($table, $id, ) = $GLOBALS['debug']['aucasou']; |
|
| 259 | - $nom = $GLOBALS['debug_objets']['courant'] . $id; |
|
| 258 | + list($table, $id,) = $GLOBALS['debug']['aucasou']; |
|
| 259 | + $nom = $GLOBALS['debug_objets']['courant'].$id; |
|
| 260 | 260 | $GLOBALS['debug_objets']['requete'][$nom] = $query; |
| 261 | 261 | } |
| 262 | 262 | $res = $f($select, $from, $where, $groupby, $orderby, $limit, $having, $serveur, true); |
@@ -1133,7 +1133,7 @@ discard block |
||
| 1133 | 1133 | // la globale n'est remplie qu'apres l'appel de sql_serveur. |
| 1134 | 1134 | if ($spip == null) { |
| 1135 | 1135 | $connexion = $GLOBALS['connexions'][$serveur ? strtolower($serveur) : 0]; |
| 1136 | - $spip = $connexion['prefixe'] . '\_%'; |
|
| 1136 | + $spip = $connexion['prefixe'].'\_%'; |
|
| 1137 | 1137 | } |
| 1138 | 1138 | |
| 1139 | 1139 | return $f($spip, $serveur, $option !== false); |
@@ -2016,7 +2016,7 @@ discard block |
||
| 2016 | 2016 | **/ |
| 2017 | 2017 | function sql_in($val, $valeurs, $not = '', $serveur = '', $option = true) { |
| 2018 | 2018 | if (is_string($valeurs)) { |
| 2019 | - if(isset($valeurs[0]) and $valeurs[0] === ',') { |
|
| 2019 | + if (isset($valeurs[0]) and $valeurs[0] === ',') { |
|
| 2020 | 2020 | $valeurs = substr($valeurs, 1); |
| 2021 | 2021 | } |
| 2022 | 2022 | // on explode en tableau pour pouvoir securiser le contenu |
@@ -2220,9 +2220,9 @@ discard block |
||
| 2220 | 2220 | $jour = 0; |
| 2221 | 2221 | } |
| 2222 | 2222 | |
| 2223 | - return sprintf("%04u", $annee) . '-' . sprintf("%02u", $mois) . '-' |
|
| 2224 | - . sprintf("%02u", $jour) . ' ' . sprintf("%02u", $h) . ':' |
|
| 2225 | - . sprintf("%02u", $m) . ':' . sprintf("%02u", $s); |
|
| 2223 | + return sprintf("%04u", $annee).'-'.sprintf("%02u", $mois).'-' |
|
| 2224 | + . sprintf("%02u", $jour).' '.sprintf("%02u", $h).':' |
|
| 2225 | + . sprintf("%02u", $m).':'.sprintf("%02u", $s); |
|
| 2226 | 2226 | } |
| 2227 | 2227 | |
| 2228 | 2228 | |
@@ -2284,7 +2284,7 @@ discard block |
||
| 2284 | 2284 | */ |
| 2285 | 2285 | function prefixer_table_spip($table, $prefixe) { |
| 2286 | 2286 | if ($prefixe) { |
| 2287 | - $table = preg_replace('/^spip_/', $prefixe . '_', $table); |
|
| 2287 | + $table = preg_replace('/^spip_/', $prefixe.'_', $table); |
|
| 2288 | 2288 | } |
| 2289 | 2289 | return $table; |
| 2290 | 2290 | } |
| 2291 | 2291 | \ No newline at end of file |