@@ -15,7 +15,7 @@ discard block |
||
| 15 | 15 | * @package SPIP\Core\Rubriques\Edition |
| 16 | 16 | */ |
| 17 | 17 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 18 | - return; |
|
| 18 | + return; |
|
| 19 | 19 | } |
| 20 | 20 | |
| 21 | 21 | include_spip('inc/rubriques'); |
@@ -37,34 +37,34 @@ discard block |
||
| 37 | 37 | */ |
| 38 | 38 | function action_editer_rubrique_dist($arg = null) { |
| 39 | 39 | |
| 40 | - if (is_null($arg)) { |
|
| 41 | - $securiser_action = charger_fonction('securiser_action', 'inc'); |
|
| 42 | - $arg = $securiser_action(); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - if (!$id_rubrique = (int) $arg) { |
|
| 46 | - if ($arg != 'oui') { |
|
| 47 | - include_spip('inc/headers'); |
|
| 48 | - redirige_url_ecrire(); |
|
| 49 | - } |
|
| 50 | - $id_rubrique = rubrique_inserer(_request('id_parent')); |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - $err = rubrique_modifier($id_rubrique); |
|
| 54 | - |
|
| 55 | - if (_request('redirect')) { |
|
| 56 | - $redirect = parametre_url( |
|
| 57 | - urldecode((string) _request('redirect')), |
|
| 58 | - 'id_rubrique', |
|
| 59 | - $id_rubrique, |
|
| 60 | - '&' |
|
| 61 | - ); |
|
| 62 | - |
|
| 63 | - include_spip('inc/headers'); |
|
| 64 | - redirige_par_entete($redirect); |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - return [$id_rubrique, $err]; |
|
| 40 | + if (is_null($arg)) { |
|
| 41 | + $securiser_action = charger_fonction('securiser_action', 'inc'); |
|
| 42 | + $arg = $securiser_action(); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + if (!$id_rubrique = (int) $arg) { |
|
| 46 | + if ($arg != 'oui') { |
|
| 47 | + include_spip('inc/headers'); |
|
| 48 | + redirige_url_ecrire(); |
|
| 49 | + } |
|
| 50 | + $id_rubrique = rubrique_inserer(_request('id_parent')); |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + $err = rubrique_modifier($id_rubrique); |
|
| 54 | + |
|
| 55 | + if (_request('redirect')) { |
|
| 56 | + $redirect = parametre_url( |
|
| 57 | + urldecode((string) _request('redirect')), |
|
| 58 | + 'id_rubrique', |
|
| 59 | + $id_rubrique, |
|
| 60 | + '&' |
|
| 61 | + ); |
|
| 62 | + |
|
| 63 | + include_spip('inc/headers'); |
|
| 64 | + redirige_par_entete($redirect); |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + return [$id_rubrique, $err]; |
|
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | |
@@ -79,64 +79,64 @@ discard block |
||
| 79 | 79 | * Identifiant de la rubrique crée |
| 80 | 80 | */ |
| 81 | 81 | function rubrique_inserer($id_parent, $set = null) { |
| 82 | - $champs = [ |
|
| 83 | - 'titre' => _T('item_nouvelle_rubrique'), |
|
| 84 | - 'id_parent' => (int) $id_parent, |
|
| 85 | - 'statut' => 'prepa' |
|
| 86 | - ]; |
|
| 87 | - |
|
| 88 | - if ($set) { |
|
| 89 | - $champs = array_merge($champs, $set); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - // Envoyer aux plugins |
|
| 93 | - $champs = pipeline( |
|
| 94 | - 'pre_insertion', |
|
| 95 | - [ |
|
| 96 | - 'args' => [ |
|
| 97 | - 'table' => 'spip_rubriques', |
|
| 98 | - ], |
|
| 99 | - 'data' => $champs |
|
| 100 | - ] |
|
| 101 | - ); |
|
| 102 | - |
|
| 103 | - $id_rubrique = sql_insertq('spip_rubriques', $champs); |
|
| 104 | - pipeline( |
|
| 105 | - 'post_insertion', |
|
| 106 | - [ |
|
| 107 | - 'args' => [ |
|
| 108 | - 'table' => 'spip_rubriques', |
|
| 109 | - 'id_objet' => $id_rubrique |
|
| 110 | - ], |
|
| 111 | - 'data' => $champs |
|
| 112 | - ] |
|
| 113 | - ); |
|
| 114 | - propager_les_secteurs(); |
|
| 115 | - calculer_langues_rubriques(); |
|
| 116 | - |
|
| 117 | - // Appeler une notification |
|
| 118 | - if ($notifications = charger_fonction('notifications', 'inc')) { |
|
| 119 | - $notifications( |
|
| 120 | - 'rubrique_inserer', |
|
| 121 | - $id_rubrique, |
|
| 122 | - [ |
|
| 123 | - 'id_parent' => $id_parent, |
|
| 124 | - 'champs' => $champs, |
|
| 125 | - ] |
|
| 126 | - ); |
|
| 127 | - $notifications( |
|
| 128 | - 'objet_inserer', |
|
| 129 | - $id_rubrique, |
|
| 130 | - [ |
|
| 131 | - 'objet' => 'rubrique', |
|
| 132 | - 'id_objet' => $id_rubrique, |
|
| 133 | - 'id_parent' => $id_parent, |
|
| 134 | - 'champs' => $champs, |
|
| 135 | - ] |
|
| 136 | - ); |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - return $id_rubrique; |
|
| 82 | + $champs = [ |
|
| 83 | + 'titre' => _T('item_nouvelle_rubrique'), |
|
| 84 | + 'id_parent' => (int) $id_parent, |
|
| 85 | + 'statut' => 'prepa' |
|
| 86 | + ]; |
|
| 87 | + |
|
| 88 | + if ($set) { |
|
| 89 | + $champs = array_merge($champs, $set); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + // Envoyer aux plugins |
|
| 93 | + $champs = pipeline( |
|
| 94 | + 'pre_insertion', |
|
| 95 | + [ |
|
| 96 | + 'args' => [ |
|
| 97 | + 'table' => 'spip_rubriques', |
|
| 98 | + ], |
|
| 99 | + 'data' => $champs |
|
| 100 | + ] |
|
| 101 | + ); |
|
| 102 | + |
|
| 103 | + $id_rubrique = sql_insertq('spip_rubriques', $champs); |
|
| 104 | + pipeline( |
|
| 105 | + 'post_insertion', |
|
| 106 | + [ |
|
| 107 | + 'args' => [ |
|
| 108 | + 'table' => 'spip_rubriques', |
|
| 109 | + 'id_objet' => $id_rubrique |
|
| 110 | + ], |
|
| 111 | + 'data' => $champs |
|
| 112 | + ] |
|
| 113 | + ); |
|
| 114 | + propager_les_secteurs(); |
|
| 115 | + calculer_langues_rubriques(); |
|
| 116 | + |
|
| 117 | + // Appeler une notification |
|
| 118 | + if ($notifications = charger_fonction('notifications', 'inc')) { |
|
| 119 | + $notifications( |
|
| 120 | + 'rubrique_inserer', |
|
| 121 | + $id_rubrique, |
|
| 122 | + [ |
|
| 123 | + 'id_parent' => $id_parent, |
|
| 124 | + 'champs' => $champs, |
|
| 125 | + ] |
|
| 126 | + ); |
|
| 127 | + $notifications( |
|
| 128 | + 'objet_inserer', |
|
| 129 | + $id_rubrique, |
|
| 130 | + [ |
|
| 131 | + 'objet' => 'rubrique', |
|
| 132 | + 'id_objet' => $id_rubrique, |
|
| 133 | + 'id_parent' => $id_parent, |
|
| 134 | + 'champs' => $champs, |
|
| 135 | + ] |
|
| 136 | + ); |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + return $id_rubrique; |
|
| 140 | 140 | } |
| 141 | 141 | |
| 142 | 142 | /** |
@@ -152,46 +152,46 @@ discard block |
||
| 152 | 152 | * - chaîne : texte d'un message d'erreur |
| 153 | 153 | */ |
| 154 | 154 | function rubrique_modifier($id_rubrique, $set = null) { |
| 155 | - include_spip('inc/autoriser'); |
|
| 156 | - include_spip('inc/filtres'); |
|
| 157 | - |
|
| 158 | - include_spip('inc/modifier'); |
|
| 159 | - $c = collecter_requests( |
|
| 160 | - // include list |
|
| 161 | - objet_info('rubrique', 'champs_editables'), |
|
| 162 | - // exclude list |
|
| 163 | - ['id_parent', 'confirme_deplace'], |
|
| 164 | - // donnees eventuellement fournies |
|
| 165 | - $set |
|
| 166 | - ); |
|
| 167 | - |
|
| 168 | - if ( |
|
| 169 | - $err = objet_modifier_champs( |
|
| 170 | - 'rubrique', |
|
| 171 | - $id_rubrique, |
|
| 172 | - [ |
|
| 173 | - 'data' => $set, |
|
| 174 | - 'nonvide' => ['titre' => _T('titre_nouvelle_rubrique') . ' ' . _T('info_numero_abbreviation') . $id_rubrique] |
|
| 175 | - ], |
|
| 176 | - $c |
|
| 177 | - ) |
|
| 178 | - ) { |
|
| 179 | - return $err; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - $c = collecter_requests(['id_parent', 'confirme_deplace'], [], $set); |
|
| 183 | - // Deplacer la rubrique |
|
| 184 | - if (isset($c['id_parent'])) { |
|
| 185 | - $err = rubrique_instituer($id_rubrique, $c); |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - // invalider les caches marques de cette rubrique |
|
| 189 | - include_spip('inc/invalideur'); |
|
| 190 | - suivre_invalideur("id='rubrique/$id_rubrique'"); |
|
| 191 | - // et celui de menu_rubriques |
|
| 192 | - effacer_meta('date_calcul_rubriques'); |
|
| 193 | - |
|
| 194 | - return $err; |
|
| 155 | + include_spip('inc/autoriser'); |
|
| 156 | + include_spip('inc/filtres'); |
|
| 157 | + |
|
| 158 | + include_spip('inc/modifier'); |
|
| 159 | + $c = collecter_requests( |
|
| 160 | + // include list |
|
| 161 | + objet_info('rubrique', 'champs_editables'), |
|
| 162 | + // exclude list |
|
| 163 | + ['id_parent', 'confirme_deplace'], |
|
| 164 | + // donnees eventuellement fournies |
|
| 165 | + $set |
|
| 166 | + ); |
|
| 167 | + |
|
| 168 | + if ( |
|
| 169 | + $err = objet_modifier_champs( |
|
| 170 | + 'rubrique', |
|
| 171 | + $id_rubrique, |
|
| 172 | + [ |
|
| 173 | + 'data' => $set, |
|
| 174 | + 'nonvide' => ['titre' => _T('titre_nouvelle_rubrique') . ' ' . _T('info_numero_abbreviation') . $id_rubrique] |
|
| 175 | + ], |
|
| 176 | + $c |
|
| 177 | + ) |
|
| 178 | + ) { |
|
| 179 | + return $err; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + $c = collecter_requests(['id_parent', 'confirme_deplace'], [], $set); |
|
| 183 | + // Deplacer la rubrique |
|
| 184 | + if (isset($c['id_parent'])) { |
|
| 185 | + $err = rubrique_instituer($id_rubrique, $c); |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + // invalider les caches marques de cette rubrique |
|
| 189 | + include_spip('inc/invalideur'); |
|
| 190 | + suivre_invalideur("id='rubrique/$id_rubrique'"); |
|
| 191 | + // et celui de menu_rubriques |
|
| 192 | + effacer_meta('date_calcul_rubriques'); |
|
| 193 | + |
|
| 194 | + return $err; |
|
| 195 | 195 | } |
| 196 | 196 | |
| 197 | 197 | /** |
@@ -214,29 +214,29 @@ discard block |
||
| 214 | 214 | * false si la confirmation du déplacement n'est pas présente |
| 215 | 215 | */ |
| 216 | 216 | function editer_rubrique_breves($id_rubrique, $id_parent, $c = []) { |
| 217 | - if (!sql_table_exists('spip_breves')) { |
|
| 218 | - return true; |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - if (!sql_countsel('spip_breves', "id_rubrique=$id_rubrique")) { |
|
| 222 | - return true; |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - if (empty($c['confirme_deplace']) || $c['confirme_deplace'] != 'oui') { |
|
| 226 | - return false; |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - if ( |
|
| 230 | - $id_secteur = sql_getfetsel( |
|
| 231 | - 'id_secteur', |
|
| 232 | - 'spip_rubriques', |
|
| 233 | - "id_rubrique=$id_parent" |
|
| 234 | - ) |
|
| 235 | - ) { |
|
| 236 | - sql_updateq('spip_breves', ['id_rubrique' => $id_secteur], "id_rubrique=$id_rubrique"); |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - return true; |
|
| 217 | + if (!sql_table_exists('spip_breves')) { |
|
| 218 | + return true; |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + if (!sql_countsel('spip_breves', "id_rubrique=$id_rubrique")) { |
|
| 222 | + return true; |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + if (empty($c['confirme_deplace']) || $c['confirme_deplace'] != 'oui') { |
|
| 226 | + return false; |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + if ( |
|
| 230 | + $id_secteur = sql_getfetsel( |
|
| 231 | + 'id_secteur', |
|
| 232 | + 'spip_rubriques', |
|
| 233 | + "id_rubrique=$id_parent" |
|
| 234 | + ) |
|
| 235 | + ) { |
|
| 236 | + sql_updateq('spip_breves', ['id_rubrique' => $id_secteur], "id_rubrique=$id_rubrique"); |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + return true; |
|
| 240 | 240 | } |
| 241 | 241 | |
| 242 | 242 | |
@@ -258,72 +258,72 @@ discard block |
||
| 258 | 258 | * Chaîne : texte du message d'erreur |
| 259 | 259 | */ |
| 260 | 260 | function rubrique_instituer($id_rubrique, $c) { |
| 261 | - // traitement de la rubrique parente |
|
| 262 | - // interdiction de deplacer vers ou a partir d'une rubrique |
|
| 263 | - // qu'on n'administre pas. |
|
| 264 | - |
|
| 265 | - if (null !== ($id_parent = $c['id_parent'])) { |
|
| 266 | - $id_parent = (int) $id_parent; |
|
| 267 | - $filles = calcul_branche_in($id_rubrique); |
|
| 268 | - if (str_contains(",$id_parent,", (string) ",$filles,")) { |
|
| 269 | - spip_log("La rubrique $id_rubrique ne peut etre fille de sa descendante $id_parent"); |
|
| 270 | - } else { |
|
| 271 | - $s = sql_fetsel('id_parent, statut', 'spip_rubriques', "id_rubrique=$id_rubrique"); |
|
| 272 | - $old_parent = $s['id_parent']; |
|
| 273 | - |
|
| 274 | - if ( |
|
| 275 | - !($id_parent != $old_parent |
|
| 276 | - && autoriser('publierdans', 'rubrique', $id_parent) |
|
| 277 | - && autoriser('creerrubriquedans', 'rubrique', $id_parent) |
|
| 278 | - && autoriser('publierdans', 'rubrique', $old_parent) |
|
| 279 | - ) |
|
| 280 | - ) { |
|
| 281 | - if ($s['statut'] != 'prepa') { |
|
| 282 | - spip_log("deplacement de $id_rubrique vers $id_parent refuse a " . $GLOBALS['visiteur_session']['id_auteur'] . ' ' . $GLOBALS['visiteur_session']['statut']); |
|
| 283 | - } |
|
| 284 | - } elseif (editer_rubrique_breves($id_rubrique, $id_parent, $c)) { |
|
| 285 | - $statut_ancien = $s['statut']; |
|
| 286 | - sql_updateq('spip_rubriques', ['id_parent' => $id_parent], "id_rubrique=$id_rubrique"); |
|
| 287 | - |
|
| 288 | - |
|
| 289 | - propager_les_secteurs(); |
|
| 290 | - |
|
| 291 | - // Deplacement d'une rubrique publiee ==> chgt general de leur statut |
|
| 292 | - if ($statut_ancien == 'publie') { |
|
| 293 | - calculer_rubriques_if($old_parent, ['id_rubrique' => $id_parent], ['statut_ancien' => $statut_ancien]); |
|
| 294 | - } |
|
| 295 | - // Creation ou deplacement d'une rubrique non publiee |
|
| 296 | - // invalider le cache de leur menu |
|
| 297 | - elseif (!$statut_ancien || $old_parent != $id_parent) { |
|
| 298 | - effacer_meta('date_calcul_rubriques'); |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - calculer_langues_rubriques(); |
|
| 302 | - |
|
| 303 | - // Appeler une notification |
|
| 304 | - if ($notifications = charger_fonction('notifications', 'inc')) { |
|
| 305 | - $notifications( |
|
| 306 | - 'rubrique_instituer', |
|
| 307 | - $id_rubrique, |
|
| 308 | - [ |
|
| 309 | - 'statut_ancien' => $statut_ancien, |
|
| 310 | - 'id_parent_ancien' => $old_parent, |
|
| 311 | - ] |
|
| 312 | - ); |
|
| 313 | - $notifications( |
|
| 314 | - 'objet_instituer', |
|
| 315 | - $id_rubrique, |
|
| 316 | - [ |
|
| 317 | - 'objet' => 'rubrique', |
|
| 318 | - 'id_objet' => $id_rubrique, |
|
| 319 | - 'statut_ancien' => $statut_ancien, |
|
| 320 | - 'id_parent_ancien' => $old_parent, |
|
| 321 | - ] |
|
| 322 | - ); |
|
| 323 | - } |
|
| 324 | - } |
|
| 325 | - } |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - return ''; // pas d'erreur |
|
| 261 | + // traitement de la rubrique parente |
|
| 262 | + // interdiction de deplacer vers ou a partir d'une rubrique |
|
| 263 | + // qu'on n'administre pas. |
|
| 264 | + |
|
| 265 | + if (null !== ($id_parent = $c['id_parent'])) { |
|
| 266 | + $id_parent = (int) $id_parent; |
|
| 267 | + $filles = calcul_branche_in($id_rubrique); |
|
| 268 | + if (str_contains(",$id_parent,", (string) ",$filles,")) { |
|
| 269 | + spip_log("La rubrique $id_rubrique ne peut etre fille de sa descendante $id_parent"); |
|
| 270 | + } else { |
|
| 271 | + $s = sql_fetsel('id_parent, statut', 'spip_rubriques', "id_rubrique=$id_rubrique"); |
|
| 272 | + $old_parent = $s['id_parent']; |
|
| 273 | + |
|
| 274 | + if ( |
|
| 275 | + !($id_parent != $old_parent |
|
| 276 | + && autoriser('publierdans', 'rubrique', $id_parent) |
|
| 277 | + && autoriser('creerrubriquedans', 'rubrique', $id_parent) |
|
| 278 | + && autoriser('publierdans', 'rubrique', $old_parent) |
|
| 279 | + ) |
|
| 280 | + ) { |
|
| 281 | + if ($s['statut'] != 'prepa') { |
|
| 282 | + spip_log("deplacement de $id_rubrique vers $id_parent refuse a " . $GLOBALS['visiteur_session']['id_auteur'] . ' ' . $GLOBALS['visiteur_session']['statut']); |
|
| 283 | + } |
|
| 284 | + } elseif (editer_rubrique_breves($id_rubrique, $id_parent, $c)) { |
|
| 285 | + $statut_ancien = $s['statut']; |
|
| 286 | + sql_updateq('spip_rubriques', ['id_parent' => $id_parent], "id_rubrique=$id_rubrique"); |
|
| 287 | + |
|
| 288 | + |
|
| 289 | + propager_les_secteurs(); |
|
| 290 | + |
|
| 291 | + // Deplacement d'une rubrique publiee ==> chgt general de leur statut |
|
| 292 | + if ($statut_ancien == 'publie') { |
|
| 293 | + calculer_rubriques_if($old_parent, ['id_rubrique' => $id_parent], ['statut_ancien' => $statut_ancien]); |
|
| 294 | + } |
|
| 295 | + // Creation ou deplacement d'une rubrique non publiee |
|
| 296 | + // invalider le cache de leur menu |
|
| 297 | + elseif (!$statut_ancien || $old_parent != $id_parent) { |
|
| 298 | + effacer_meta('date_calcul_rubriques'); |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + calculer_langues_rubriques(); |
|
| 302 | + |
|
| 303 | + // Appeler une notification |
|
| 304 | + if ($notifications = charger_fonction('notifications', 'inc')) { |
|
| 305 | + $notifications( |
|
| 306 | + 'rubrique_instituer', |
|
| 307 | + $id_rubrique, |
|
| 308 | + [ |
|
| 309 | + 'statut_ancien' => $statut_ancien, |
|
| 310 | + 'id_parent_ancien' => $old_parent, |
|
| 311 | + ] |
|
| 312 | + ); |
|
| 313 | + $notifications( |
|
| 314 | + 'objet_instituer', |
|
| 315 | + $id_rubrique, |
|
| 316 | + [ |
|
| 317 | + 'objet' => 'rubrique', |
|
| 318 | + 'id_objet' => $id_rubrique, |
|
| 319 | + 'statut_ancien' => $statut_ancien, |
|
| 320 | + 'id_parent_ancien' => $old_parent, |
|
| 321 | + ] |
|
| 322 | + ); |
|
| 323 | + } |
|
| 324 | + } |
|
| 325 | + } |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + return ''; // pas d'erreur |
|
| 329 | 329 | } |
@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | **/ |
| 17 | 17 | |
| 18 | 18 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 19 | - return; |
|
| 19 | + return; |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | /** |
@@ -25,13 +25,13 @@ discard block |
||
| 25 | 25 | * @return void |
| 26 | 26 | */ |
| 27 | 27 | function action_annuler_job_dist() { |
| 28 | - $securiser_action = charger_fonction('securiser_action', 'inc'); |
|
| 29 | - $id_job = $securiser_action(); |
|
| 28 | + $securiser_action = charger_fonction('securiser_action', 'inc'); |
|
| 29 | + $id_job = $securiser_action(); |
|
| 30 | 30 | |
| 31 | - if ( |
|
| 32 | - ($id_job = (int) $id_job) |
|
| 33 | - && autoriser('annuler', 'job', $id_job) |
|
| 34 | - ) { |
|
| 35 | - job_queue_remove($id_job); |
|
| 36 | - } |
|
| 31 | + if ( |
|
| 32 | + ($id_job = (int) $id_job) |
|
| 33 | + && autoriser('annuler', 'job', $id_job) |
|
| 34 | + ) { |
|
| 35 | + job_queue_remove($id_job); |
|
| 36 | + } |
|
| 37 | 37 | } |
@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | **/ |
| 17 | 17 | |
| 18 | 18 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 19 | - return; |
|
| 19 | + return; |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | /** |
@@ -27,49 +27,49 @@ discard block |
||
| 27 | 27 | * @return void |
| 28 | 28 | */ |
| 29 | 29 | function enregistre_modif_plugin() { |
| 30 | - include_spip('inc/plugin'); |
|
| 31 | - // recuperer les plugins dans l'ordre des $_POST |
|
| 32 | - $test = []; |
|
| 33 | - foreach (liste_plugin_files() as $file) { |
|
| 34 | - $test['s' . substr(md5(_DIR_PLUGINS . $file), 0, 16)] = $file; |
|
| 35 | - } |
|
| 36 | - if (defined('_DIR_PLUGINS_SUPPL')) { |
|
| 37 | - foreach (liste_plugin_files(_DIR_PLUGINS_SUPPL) as $file) { |
|
| 38 | - $test['s' . substr(md5(_DIR_PLUGINS_SUPPL . $file), 0, 16)] = $file; |
|
| 39 | - } |
|
| 40 | - } |
|
| 30 | + include_spip('inc/plugin'); |
|
| 31 | + // recuperer les plugins dans l'ordre des $_POST |
|
| 32 | + $test = []; |
|
| 33 | + foreach (liste_plugin_files() as $file) { |
|
| 34 | + $test['s' . substr(md5(_DIR_PLUGINS . $file), 0, 16)] = $file; |
|
| 35 | + } |
|
| 36 | + if (defined('_DIR_PLUGINS_SUPPL')) { |
|
| 37 | + foreach (liste_plugin_files(_DIR_PLUGINS_SUPPL) as $file) { |
|
| 38 | + $test['s' . substr(md5(_DIR_PLUGINS_SUPPL . $file), 0, 16)] = $file; |
|
| 39 | + } |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - $plugin = []; |
|
| 42 | + $plugin = []; |
|
| 43 | 43 | |
| 44 | - foreach ($_POST as $choix => $val) { |
|
| 45 | - if (isset($test[$choix]) && $val == 'O') { |
|
| 46 | - $plugin[] = $test[$choix]; |
|
| 47 | - } |
|
| 48 | - } |
|
| 44 | + foreach ($_POST as $choix => $val) { |
|
| 45 | + if (isset($test[$choix]) && $val == 'O') { |
|
| 46 | + $plugin[] = $test[$choix]; |
|
| 47 | + } |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - spip_log("Changement des plugins actifs par l'auteur " . $GLOBALS['visiteur_session']['id_auteur'] . ': ' . implode( |
|
| 51 | - ',', |
|
| 52 | - $plugin |
|
| 53 | - )); |
|
| 54 | - ecrire_plugin_actifs($plugin); |
|
| 50 | + spip_log("Changement des plugins actifs par l'auteur " . $GLOBALS['visiteur_session']['id_auteur'] . ': ' . implode( |
|
| 51 | + ',', |
|
| 52 | + $plugin |
|
| 53 | + )); |
|
| 54 | + ecrire_plugin_actifs($plugin); |
|
| 55 | 55 | |
| 56 | - // Chaque fois que l'on valide des plugins, on memorise la liste de ces plugins comme etant "interessants", avec un score initial, qui sera decremente a chaque tour : ainsi un plugin active pourra reter visible a l'ecran, jusqu'a ce qu'il tombe dans l'oubli. |
|
| 57 | - $plugins_interessants = @unserialize($GLOBALS['meta']['plugins_interessants']); |
|
| 58 | - if (!is_array($plugins_interessants)) { |
|
| 59 | - $plugins_interessants = []; |
|
| 60 | - } |
|
| 56 | + // Chaque fois que l'on valide des plugins, on memorise la liste de ces plugins comme etant "interessants", avec un score initial, qui sera decremente a chaque tour : ainsi un plugin active pourra reter visible a l'ecran, jusqu'a ce qu'il tombe dans l'oubli. |
|
| 57 | + $plugins_interessants = @unserialize($GLOBALS['meta']['plugins_interessants']); |
|
| 58 | + if (!is_array($plugins_interessants)) { |
|
| 59 | + $plugins_interessants = []; |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - $plugins_interessants2 = []; |
|
| 62 | + $plugins_interessants2 = []; |
|
| 63 | 63 | |
| 64 | - foreach ($plugins_interessants as $plug => $score) { |
|
| 65 | - if ($score > 1) { |
|
| 66 | - $plugins_interessants2[$plug] = $score - 1; |
|
| 67 | - } |
|
| 68 | - } |
|
| 69 | - foreach ($plugin as $plug) { |
|
| 70 | - $plugins_interessants2[$plug] = 10; |
|
| 71 | - } // score initial |
|
| 72 | - ecrire_meta('plugins_interessants', serialize($plugins_interessants2)); |
|
| 64 | + foreach ($plugins_interessants as $plug => $score) { |
|
| 65 | + if ($score > 1) { |
|
| 66 | + $plugins_interessants2[$plug] = $score - 1; |
|
| 67 | + } |
|
| 68 | + } |
|
| 69 | + foreach ($plugin as $plug) { |
|
| 70 | + $plugins_interessants2[$plug] = 10; |
|
| 71 | + } // score initial |
|
| 72 | + ecrire_meta('plugins_interessants', serialize($plugins_interessants2)); |
|
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | /** |
@@ -81,13 +81,13 @@ discard block |
||
| 81 | 81 | */ |
| 82 | 82 | function action_activer_plugins_dist() { |
| 83 | 83 | |
| 84 | - $securiser_action = charger_fonction('securiser_action', 'inc'); |
|
| 85 | - $securiser_action(); |
|
| 84 | + $securiser_action = charger_fonction('securiser_action', 'inc'); |
|
| 85 | + $securiser_action(); |
|
| 86 | 86 | |
| 87 | - if (!autoriser('configurer', '_plugins')) { |
|
| 88 | - die('erreur'); |
|
| 89 | - } |
|
| 90 | - // forcer la maj des meta pour les cas de modif de numero de version base via phpmyadmin |
|
| 91 | - lire_metas(); |
|
| 92 | - enregistre_modif_plugin(); |
|
| 87 | + if (!autoriser('configurer', '_plugins')) { |
|
| 88 | + die('erreur'); |
|
| 89 | + } |
|
| 90 | + // forcer la maj des meta pour les cas de modif de numero de version base via phpmyadmin |
|
| 91 | + lire_metas(); |
|
| 92 | + enregistre_modif_plugin(); |
|
| 93 | 93 | } |
@@ -31,11 +31,11 @@ discard block |
||
| 31 | 31 | // recuperer les plugins dans l'ordre des $_POST |
| 32 | 32 | $test = []; |
| 33 | 33 | foreach (liste_plugin_files() as $file) { |
| 34 | - $test['s' . substr(md5(_DIR_PLUGINS . $file), 0, 16)] = $file; |
|
| 34 | + $test['s'.substr(md5(_DIR_PLUGINS.$file), 0, 16)] = $file; |
|
| 35 | 35 | } |
| 36 | 36 | if (defined('_DIR_PLUGINS_SUPPL')) { |
| 37 | 37 | foreach (liste_plugin_files(_DIR_PLUGINS_SUPPL) as $file) { |
| 38 | - $test['s' . substr(md5(_DIR_PLUGINS_SUPPL . $file), 0, 16)] = $file; |
|
| 38 | + $test['s'.substr(md5(_DIR_PLUGINS_SUPPL.$file), 0, 16)] = $file; |
|
| 39 | 39 | } |
| 40 | 40 | } |
| 41 | 41 | |
@@ -47,7 +47,7 @@ discard block |
||
| 47 | 47 | } |
| 48 | 48 | } |
| 49 | 49 | |
| 50 | - spip_log("Changement des plugins actifs par l'auteur " . $GLOBALS['visiteur_session']['id_auteur'] . ': ' . implode( |
|
| 50 | + spip_log("Changement des plugins actifs par l'auteur ".$GLOBALS['visiteur_session']['id_auteur'].': '.implode( |
|
| 51 | 51 | ',', |
| 52 | 52 | $plugin |
| 53 | 53 | )); |
@@ -20,7 +20,7 @@ discard block |
||
| 20 | 20 | |
| 21 | 21 | |
| 22 | 22 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 23 | - return; |
|
| 23 | + return; |
|
| 24 | 24 | } |
| 25 | 25 | |
| 26 | 26 | |
@@ -43,68 +43,68 @@ discard block |
||
| 43 | 43 | * @return array|string |
| 44 | 44 | */ |
| 45 | 45 | function action_inscrire_auteur_dist($statut, $mail_complet, $nom, $options = []) { |
| 46 | - if (!is_array($options)) { |
|
| 47 | - $options = ['id' => $options]; |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - $f = function_exists('test_inscription') |
|
| 51 | - ? 'test_inscription' |
|
| 52 | - : 'test_inscription_dist'; |
|
| 53 | - $desc = $f($statut, $mail_complet, $nom, $options); |
|
| 54 | - |
|
| 55 | - // erreur ? |
|
| 56 | - if (!is_array($desc)) { |
|
| 57 | - return _T($desc); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - include_spip('base/abstract_sql'); |
|
| 61 | - $res = sql_select('statut, id_auteur, login, email, nom', 'spip_auteurs', 'email=' . sql_quote($desc['email'])); |
|
| 62 | - // erreur ? |
|
| 63 | - if (!$res) { |
|
| 64 | - return _T('titre_probleme_technique'); |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - $row = sql_fetch($res); |
|
| 68 | - sql_free($res); |
|
| 69 | - if ($row) { |
|
| 70 | - if (isset($options['force_nouveau']) && $options['force_nouveau'] == true) { |
|
| 71 | - $desc['id_auteur'] = $row['id_auteur']; |
|
| 72 | - $desc = inscription_nouveau($desc); |
|
| 73 | - } else { |
|
| 74 | - $desc = $row; |
|
| 75 | - } |
|
| 76 | - } else // s'il n'existe pas deja, creer les identifiants |
|
| 77 | - { |
|
| 78 | - $desc = inscription_nouveau($desc); |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - // erreur ? |
|
| 82 | - if (!is_array($desc)) { |
|
| 83 | - return $desc; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - |
|
| 87 | - // generer le mot de passe (ou le refaire si compte inutilise) |
|
| 88 | - $desc['pass'] = creer_pass_pour_auteur($desc['id_auteur']); |
|
| 89 | - |
|
| 90 | - // attribuer un jeton pour confirmation par clic sur un lien |
|
| 91 | - $desc['jeton'] = auteur_attribuer_jeton($desc['id_auteur']); |
|
| 92 | - |
|
| 93 | - // charger de suite cette fonction, pour ses utilitaires |
|
| 94 | - $envoyer_inscription = charger_fonction('envoyer_inscription', ''); |
|
| 95 | - [$sujet, $msg, $from, $head] = $envoyer_inscription($desc, $nom, $statut, $options); |
|
| 96 | - |
|
| 97 | - $notifications = charger_fonction('notifications', 'inc'); |
|
| 98 | - notifications_envoyer_mails($mail_complet, $msg, $sujet, $from, $head); |
|
| 99 | - |
|
| 100 | - // Notifications |
|
| 101 | - $notifications( |
|
| 102 | - 'inscription', |
|
| 103 | - $desc['id_auteur'], |
|
| 104 | - ['nom' => $desc['nom'], 'email' => $desc['email']] |
|
| 105 | - ); |
|
| 106 | - |
|
| 107 | - return $desc; |
|
| 46 | + if (!is_array($options)) { |
|
| 47 | + $options = ['id' => $options]; |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + $f = function_exists('test_inscription') |
|
| 51 | + ? 'test_inscription' |
|
| 52 | + : 'test_inscription_dist'; |
|
| 53 | + $desc = $f($statut, $mail_complet, $nom, $options); |
|
| 54 | + |
|
| 55 | + // erreur ? |
|
| 56 | + if (!is_array($desc)) { |
|
| 57 | + return _T($desc); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + include_spip('base/abstract_sql'); |
|
| 61 | + $res = sql_select('statut, id_auteur, login, email, nom', 'spip_auteurs', 'email=' . sql_quote($desc['email'])); |
|
| 62 | + // erreur ? |
|
| 63 | + if (!$res) { |
|
| 64 | + return _T('titre_probleme_technique'); |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + $row = sql_fetch($res); |
|
| 68 | + sql_free($res); |
|
| 69 | + if ($row) { |
|
| 70 | + if (isset($options['force_nouveau']) && $options['force_nouveau'] == true) { |
|
| 71 | + $desc['id_auteur'] = $row['id_auteur']; |
|
| 72 | + $desc = inscription_nouveau($desc); |
|
| 73 | + } else { |
|
| 74 | + $desc = $row; |
|
| 75 | + } |
|
| 76 | + } else // s'il n'existe pas deja, creer les identifiants |
|
| 77 | + { |
|
| 78 | + $desc = inscription_nouveau($desc); |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + // erreur ? |
|
| 82 | + if (!is_array($desc)) { |
|
| 83 | + return $desc; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + |
|
| 87 | + // generer le mot de passe (ou le refaire si compte inutilise) |
|
| 88 | + $desc['pass'] = creer_pass_pour_auteur($desc['id_auteur']); |
|
| 89 | + |
|
| 90 | + // attribuer un jeton pour confirmation par clic sur un lien |
|
| 91 | + $desc['jeton'] = auteur_attribuer_jeton($desc['id_auteur']); |
|
| 92 | + |
|
| 93 | + // charger de suite cette fonction, pour ses utilitaires |
|
| 94 | + $envoyer_inscription = charger_fonction('envoyer_inscription', ''); |
|
| 95 | + [$sujet, $msg, $from, $head] = $envoyer_inscription($desc, $nom, $statut, $options); |
|
| 96 | + |
|
| 97 | + $notifications = charger_fonction('notifications', 'inc'); |
|
| 98 | + notifications_envoyer_mails($mail_complet, $msg, $sujet, $from, $head); |
|
| 99 | + |
|
| 100 | + // Notifications |
|
| 101 | + $notifications( |
|
| 102 | + 'inscription', |
|
| 103 | + $desc['id_auteur'], |
|
| 104 | + ['nom' => $desc['nom'], 'email' => $desc['email']] |
|
| 105 | + ); |
|
| 106 | + |
|
| 107 | + return $desc; |
|
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | |
@@ -127,23 +127,23 @@ discard block |
||
| 127 | 127 | * |
| 128 | 128 | */ |
| 129 | 129 | function test_inscription_dist($statut, $mail, $nom, $options) { |
| 130 | - include_spip('inc/filtres'); |
|
| 131 | - if (!$r = email_valide($mail)) { |
|
| 132 | - return 'info_email_invalide'; |
|
| 133 | - } |
|
| 134 | - $nom = trim((string) corriger_caracteres($nom)); |
|
| 135 | - $res = ['email' => $r, 'nom' => $nom, 'prefs' => $statut]; |
|
| 136 | - if (isset($options['login'])) { |
|
| 137 | - $login = trim((string) corriger_caracteres($options['login'])); |
|
| 138 | - if (strlen($login) >= _LOGIN_TROP_COURT && strlen($nom) <= 64) { |
|
| 139 | - $res['login'] = $login; |
|
| 140 | - } |
|
| 141 | - } |
|
| 142 | - if (!isset($res['login']) && (strlen($nom) < _LOGIN_TROP_COURT || strlen($nom) > 64)) { |
|
| 143 | - return 'ecrire:info_login_trop_court'; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - return $res; |
|
| 130 | + include_spip('inc/filtres'); |
|
| 131 | + if (!$r = email_valide($mail)) { |
|
| 132 | + return 'info_email_invalide'; |
|
| 133 | + } |
|
| 134 | + $nom = trim((string) corriger_caracteres($nom)); |
|
| 135 | + $res = ['email' => $r, 'nom' => $nom, 'prefs' => $statut]; |
|
| 136 | + if (isset($options['login'])) { |
|
| 137 | + $login = trim((string) corriger_caracteres($options['login'])); |
|
| 138 | + if (strlen($login) >= _LOGIN_TROP_COURT && strlen($nom) <= 64) { |
|
| 139 | + $res['login'] = $login; |
|
| 140 | + } |
|
| 141 | + } |
|
| 142 | + if (!isset($res['login']) && (strlen($nom) < _LOGIN_TROP_COURT || strlen($nom) > 64)) { |
|
| 143 | + return 'ecrire:info_login_trop_court'; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + return $res; |
|
| 147 | 147 | } |
| 148 | 148 | |
| 149 | 149 | |
@@ -156,29 +156,29 @@ discard block |
||
| 156 | 156 | * @return mixed|string |
| 157 | 157 | */ |
| 158 | 158 | function inscription_nouveau($desc) { |
| 159 | - if (!isset($desc['login']) || !strlen((string) $desc['login'])) { |
|
| 160 | - $desc['login'] = test_login($desc['nom'], $desc['email']); |
|
| 161 | - } |
|
| 159 | + if (!isset($desc['login']) || !strlen((string) $desc['login'])) { |
|
| 160 | + $desc['login'] = test_login($desc['nom'], $desc['email']); |
|
| 161 | + } |
|
| 162 | 162 | |
| 163 | - $desc['statut'] = 'nouveau'; |
|
| 164 | - include_spip('action/editer_auteur'); |
|
| 165 | - $id_auteur = $desc['id_auteur'] ?? auteur_inserer(); |
|
| 163 | + $desc['statut'] = 'nouveau'; |
|
| 164 | + include_spip('action/editer_auteur'); |
|
| 165 | + $id_auteur = $desc['id_auteur'] ?? auteur_inserer(); |
|
| 166 | 166 | |
| 167 | - if (!$id_auteur) { |
|
| 168 | - return _T('titre_probleme_technique'); |
|
| 169 | - } |
|
| 167 | + if (!$id_auteur) { |
|
| 168 | + return _T('titre_probleme_technique'); |
|
| 169 | + } |
|
| 170 | 170 | |
| 171 | - $desc['lang'] = $GLOBALS['spip_lang']; |
|
| 171 | + $desc['lang'] = $GLOBALS['spip_lang']; |
|
| 172 | 172 | |
| 173 | - include_spip('inc/autoriser'); |
|
| 174 | - // lever l'autorisation pour pouvoir modifier le statut |
|
| 175 | - autoriser_exception('modifier', 'auteur', $id_auteur); |
|
| 176 | - auteur_modifier($id_auteur, $desc); |
|
| 177 | - autoriser_exception('modifier', 'auteur', $id_auteur, false); |
|
| 173 | + include_spip('inc/autoriser'); |
|
| 174 | + // lever l'autorisation pour pouvoir modifier le statut |
|
| 175 | + autoriser_exception('modifier', 'auteur', $id_auteur); |
|
| 176 | + auteur_modifier($id_auteur, $desc); |
|
| 177 | + autoriser_exception('modifier', 'auteur', $id_auteur, false); |
|
| 178 | 178 | |
| 179 | - $desc['id_auteur'] = $id_auteur; |
|
| 179 | + $desc['id_auteur'] = $id_auteur; |
|
| 180 | 180 | |
| 181 | - return $desc; |
|
| 181 | + return $desc; |
|
| 182 | 182 | } |
| 183 | 183 | |
| 184 | 184 | |
@@ -195,27 +195,27 @@ discard block |
||
| 195 | 195 | * @return string |
| 196 | 196 | */ |
| 197 | 197 | function test_login($nom, $mail) { |
| 198 | - include_spip('inc/charsets'); |
|
| 199 | - $nom = strtolower((string) translitteration($nom)); |
|
| 200 | - $login_base = preg_replace('/[^\w\d_]/', '_', $nom); |
|
| 201 | - |
|
| 202 | - // il faut eviter que le login soit vraiment trop court |
|
| 203 | - if (strlen($login_base) < 3) { |
|
| 204 | - $mail = strtolower((string) translitteration(preg_replace('/@.*/', '', $mail))); |
|
| 205 | - $login_base = preg_replace('/[^\w\d]/', '_', $mail); |
|
| 206 | - } |
|
| 207 | - if (strlen($login_base) < 3) { |
|
| 208 | - $login_base = 'user'; |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - $login = $login_base; |
|
| 212 | - |
|
| 213 | - for ($i = 1;; $i++) { |
|
| 214 | - if (!sql_countsel('spip_auteurs', "login='$login'")) { |
|
| 215 | - return $login; |
|
| 216 | - } |
|
| 217 | - $login = $login_base . $i; |
|
| 218 | - } |
|
| 198 | + include_spip('inc/charsets'); |
|
| 199 | + $nom = strtolower((string) translitteration($nom)); |
|
| 200 | + $login_base = preg_replace('/[^\w\d_]/', '_', $nom); |
|
| 201 | + |
|
| 202 | + // il faut eviter que le login soit vraiment trop court |
|
| 203 | + if (strlen($login_base) < 3) { |
|
| 204 | + $mail = strtolower((string) translitteration(preg_replace('/@.*/', '', $mail))); |
|
| 205 | + $login_base = preg_replace('/[^\w\d]/', '_', $mail); |
|
| 206 | + } |
|
| 207 | + if (strlen($login_base) < 3) { |
|
| 208 | + $login_base = 'user'; |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + $login = $login_base; |
|
| 212 | + |
|
| 213 | + for ($i = 1;; $i++) { |
|
| 214 | + if (!sql_countsel('spip_auteurs', "login='$login'")) { |
|
| 215 | + return $login; |
|
| 216 | + } |
|
| 217 | + $login = $login_base . $i; |
|
| 218 | + } |
|
| 219 | 219 | } |
| 220 | 220 | |
| 221 | 221 | |
@@ -233,26 +233,26 @@ discard block |
||
| 233 | 233 | */ |
| 234 | 234 | function envoyer_inscription_dist($desc, $nom, $mode, $options = []) { |
| 235 | 235 | |
| 236 | - $contexte = array_merge($desc, $options); |
|
| 237 | - $contexte['nom'] = $nom; |
|
| 238 | - $contexte['mode'] = $mode; |
|
| 239 | - $contexte['url_confirm'] = generer_url_action('confirmer_inscription', '', true, true); |
|
| 240 | - $contexte['url_confirm'] = parametre_url($contexte['url_confirm'], 'email', $desc['email']); |
|
| 241 | - $contexte['url_confirm'] = parametre_url($contexte['url_confirm'], 'jeton', $desc['jeton']); |
|
| 242 | - // S'il y a l'option redirect, on l'ajoute directement ici |
|
| 243 | - if (isset($options['redirect'])) { |
|
| 244 | - $contexte['url_confirm'] = parametre_url($contexte['url_confirm'], 'redirect', $options['redirect']); |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - $modele_mail = 'modeles/mail_inscription'; |
|
| 248 | - if (isset($options['modele_mail']) && $options['modele_mail']) { |
|
| 249 | - $modele_mail = $options['modele_mail']; |
|
| 250 | - } |
|
| 251 | - $message = recuperer_fond($modele_mail, $contexte); |
|
| 252 | - $from = ($options['from'] ?? ''); |
|
| 253 | - $head = ''; |
|
| 254 | - |
|
| 255 | - return ['', $message, $from, $head]; |
|
| 236 | + $contexte = array_merge($desc, $options); |
|
| 237 | + $contexte['nom'] = $nom; |
|
| 238 | + $contexte['mode'] = $mode; |
|
| 239 | + $contexte['url_confirm'] = generer_url_action('confirmer_inscription', '', true, true); |
|
| 240 | + $contexte['url_confirm'] = parametre_url($contexte['url_confirm'], 'email', $desc['email']); |
|
| 241 | + $contexte['url_confirm'] = parametre_url($contexte['url_confirm'], 'jeton', $desc['jeton']); |
|
| 242 | + // S'il y a l'option redirect, on l'ajoute directement ici |
|
| 243 | + if (isset($options['redirect'])) { |
|
| 244 | + $contexte['url_confirm'] = parametre_url($contexte['url_confirm'], 'redirect', $options['redirect']); |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + $modele_mail = 'modeles/mail_inscription'; |
|
| 248 | + if (isset($options['modele_mail']) && $options['modele_mail']) { |
|
| 249 | + $modele_mail = $options['modele_mail']; |
|
| 250 | + } |
|
| 251 | + $message = recuperer_fond($modele_mail, $contexte); |
|
| 252 | + $from = ($options['from'] ?? ''); |
|
| 253 | + $head = ''; |
|
| 254 | + |
|
| 255 | + return ['', $message, $from, $head]; |
|
| 256 | 256 | } |
| 257 | 257 | |
| 258 | 258 | |
@@ -263,12 +263,12 @@ discard block |
||
| 263 | 263 | * @return string |
| 264 | 264 | */ |
| 265 | 265 | function creer_pass_pour_auteur($id_auteur) { |
| 266 | - include_spip('inc/acces'); |
|
| 267 | - $pass = creer_pass_aleatoire(max(_PASS_LONGUEUR_MINI, 16), $id_auteur); |
|
| 268 | - include_spip('action/editer_auteur'); |
|
| 269 | - auteur_instituer($id_auteur, ['pass' => $pass]); |
|
| 266 | + include_spip('inc/acces'); |
|
| 267 | + $pass = creer_pass_aleatoire(max(_PASS_LONGUEUR_MINI, 16), $id_auteur); |
|
| 268 | + include_spip('action/editer_auteur'); |
|
| 269 | + auteur_instituer($id_auteur, ['pass' => $pass]); |
|
| 270 | 270 | |
| 271 | - return $pass; |
|
| 271 | + return $pass; |
|
| 272 | 272 | } |
| 273 | 273 | |
| 274 | 274 | /** |
@@ -281,17 +281,17 @@ discard block |
||
| 281 | 281 | * @return string |
| 282 | 282 | */ |
| 283 | 283 | function tester_statut_inscription($statut_tmp, $id) { |
| 284 | - include_spip('inc/autoriser'); |
|
| 285 | - if ($statut_tmp) { |
|
| 286 | - return autoriser('inscrireauteur', $statut_tmp, $id) ? $statut_tmp : ''; |
|
| 287 | - } elseif ( |
|
| 288 | - autoriser('inscrireauteur', $statut_tmp = '1comite', $id) |
|
| 289 | - || autoriser('inscrireauteur', $statut_tmp = '6forum', $id) |
|
| 290 | - ) { |
|
| 291 | - return $statut_tmp; |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - return ''; |
|
| 284 | + include_spip('inc/autoriser'); |
|
| 285 | + if ($statut_tmp) { |
|
| 286 | + return autoriser('inscrireauteur', $statut_tmp, $id) ? $statut_tmp : ''; |
|
| 287 | + } elseif ( |
|
| 288 | + autoriser('inscrireauteur', $statut_tmp = '1comite', $id) |
|
| 289 | + || autoriser('inscrireauteur', $statut_tmp = '6forum', $id) |
|
| 290 | + ) { |
|
| 291 | + return $statut_tmp; |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + return ''; |
|
| 295 | 295 | } |
| 296 | 296 | |
| 297 | 297 | |
@@ -305,35 +305,35 @@ discard block |
||
| 305 | 305 | * @return array |
| 306 | 306 | */ |
| 307 | 307 | function confirmer_statut_inscription($auteur) { |
| 308 | - // securite |
|
| 309 | - if ($auteur['statut'] != 'nouveau') { |
|
| 310 | - return $auteur; |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - $s = $auteur['prefs']; |
|
| 314 | - // securite, au cas ou prefs aurait ete corrompu (ou deja ecrase par un tableau serialize) |
|
| 315 | - if (!preg_match(',^\w+$,', (string) $s)) { |
|
| 316 | - $s = '6forum'; |
|
| 317 | - } |
|
| 318 | - include_spip('inc/autoriser'); |
|
| 319 | - if (!autoriser('inscrireauteur', $s)) { |
|
| 320 | - return $auteur; |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - include_spip('inc/autoriser'); |
|
| 324 | - // accorder l'autorisation de modif du statut auteur |
|
| 325 | - autoriser_exception('modifier', 'auteur', $auteur['id_auteur']); |
|
| 326 | - include_spip('action/editer_auteur'); |
|
| 327 | - // changer le statut |
|
| 328 | - auteur_modifier($auteur['id_auteur'], ['statut' => $s]); |
|
| 329 | - unset($_COOKIE['spip_session']); // forcer la maj de la session |
|
| 330 | - // lever l'autorisation de modif du statut auteur |
|
| 331 | - autoriser_exception('modifier', 'auteur', $auteur['id_auteur'], false); |
|
| 332 | - |
|
| 333 | - // mettre a jour le statut |
|
| 334 | - $auteur['statut'] = $s; |
|
| 335 | - |
|
| 336 | - return $auteur; |
|
| 308 | + // securite |
|
| 309 | + if ($auteur['statut'] != 'nouveau') { |
|
| 310 | + return $auteur; |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + $s = $auteur['prefs']; |
|
| 314 | + // securite, au cas ou prefs aurait ete corrompu (ou deja ecrase par un tableau serialize) |
|
| 315 | + if (!preg_match(',^\w+$,', (string) $s)) { |
|
| 316 | + $s = '6forum'; |
|
| 317 | + } |
|
| 318 | + include_spip('inc/autoriser'); |
|
| 319 | + if (!autoriser('inscrireauteur', $s)) { |
|
| 320 | + return $auteur; |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + include_spip('inc/autoriser'); |
|
| 324 | + // accorder l'autorisation de modif du statut auteur |
|
| 325 | + autoriser_exception('modifier', 'auteur', $auteur['id_auteur']); |
|
| 326 | + include_spip('action/editer_auteur'); |
|
| 327 | + // changer le statut |
|
| 328 | + auteur_modifier($auteur['id_auteur'], ['statut' => $s]); |
|
| 329 | + unset($_COOKIE['spip_session']); // forcer la maj de la session |
|
| 330 | + // lever l'autorisation de modif du statut auteur |
|
| 331 | + autoriser_exception('modifier', 'auteur', $auteur['id_auteur'], false); |
|
| 332 | + |
|
| 333 | + // mettre a jour le statut |
|
| 334 | + $auteur['statut'] = $s; |
|
| 335 | + |
|
| 336 | + return $auteur; |
|
| 337 | 337 | } |
| 338 | 338 | |
| 339 | 339 | |
@@ -347,20 +347,20 @@ discard block |
||
| 347 | 347 | * @return string |
| 348 | 348 | */ |
| 349 | 349 | function auteur_attribuer_jeton($id_auteur): string { |
| 350 | - include_spip('base/abstract_sql'); |
|
| 351 | - include_spip('inc/acces'); |
|
| 352 | - |
|
| 353 | - // s'assurer de l'unicite du jeton pour le couple (email,cookie) |
|
| 354 | - do { |
|
| 355 | - // Un morceau du jeton est lisible en bdd pour éviter de devoir déchiffrer |
|
| 356 | - // tous les jetons connus pour vérifier le jeton d’un auteur. |
|
| 357 | - $public = substr((string) creer_uniqid(), 0, 7) . '.'; |
|
| 358 | - $jeton = $public . creer_uniqid(); |
|
| 359 | - $jeton_chiffre_prefixe = $public . Chiffrement::chiffrer($jeton, SpipCles::secret_du_site()); |
|
| 360 | - sql_updateq('spip_auteurs', ['cookie_oubli' => $jeton_chiffre_prefixe], 'id_auteur=' . (int) $id_auteur); |
|
| 361 | - } while (sql_countsel('spip_auteurs', 'cookie_oubli=' . sql_quote($jeton_chiffre_prefixe, '', 'string')) > 1); |
|
| 362 | - |
|
| 363 | - return $jeton; |
|
| 350 | + include_spip('base/abstract_sql'); |
|
| 351 | + include_spip('inc/acces'); |
|
| 352 | + |
|
| 353 | + // s'assurer de l'unicite du jeton pour le couple (email,cookie) |
|
| 354 | + do { |
|
| 355 | + // Un morceau du jeton est lisible en bdd pour éviter de devoir déchiffrer |
|
| 356 | + // tous les jetons connus pour vérifier le jeton d’un auteur. |
|
| 357 | + $public = substr((string) creer_uniqid(), 0, 7) . '.'; |
|
| 358 | + $jeton = $public . creer_uniqid(); |
|
| 359 | + $jeton_chiffre_prefixe = $public . Chiffrement::chiffrer($jeton, SpipCles::secret_du_site()); |
|
| 360 | + sql_updateq('spip_auteurs', ['cookie_oubli' => $jeton_chiffre_prefixe], 'id_auteur=' . (int) $id_auteur); |
|
| 361 | + } while (sql_countsel('spip_auteurs', 'cookie_oubli=' . sql_quote($jeton_chiffre_prefixe, '', 'string')) > 1); |
|
| 362 | + |
|
| 363 | + return $jeton; |
|
| 364 | 364 | } |
| 365 | 365 | |
| 366 | 366 | /** |
@@ -374,19 +374,19 @@ discard block |
||
| 374 | 374 | * @return string|null |
| 375 | 375 | */ |
| 376 | 376 | function auteur_lire_jeton(int $id_auteur, bool $autoInit = false): ?string { |
| 377 | - include_spip('base/abstract_sql'); |
|
| 378 | - $jeton_chiffre_prefixe = sql_getfetsel('cookie_oubli', 'spip_auteurs', 'id_auteur=' . $id_auteur); |
|
| 379 | - if ($jeton_chiffre_prefixe) { |
|
| 380 | - $jeton_chiffre = substr((string) $jeton_chiffre_prefixe, 8); |
|
| 381 | - $jeton = Chiffrement::dechiffrer($jeton_chiffre, SpipCles::secret_du_site()); |
|
| 382 | - if ($jeton) { |
|
| 383 | - return $jeton; |
|
| 384 | - } |
|
| 385 | - } |
|
| 386 | - if ($autoInit) { |
|
| 387 | - return auteur_attribuer_jeton($id_auteur); |
|
| 388 | - } |
|
| 389 | - return null; |
|
| 377 | + include_spip('base/abstract_sql'); |
|
| 378 | + $jeton_chiffre_prefixe = sql_getfetsel('cookie_oubli', 'spip_auteurs', 'id_auteur=' . $id_auteur); |
|
| 379 | + if ($jeton_chiffre_prefixe) { |
|
| 380 | + $jeton_chiffre = substr((string) $jeton_chiffre_prefixe, 8); |
|
| 381 | + $jeton = Chiffrement::dechiffrer($jeton_chiffre, SpipCles::secret_du_site()); |
|
| 382 | + if ($jeton) { |
|
| 383 | + return $jeton; |
|
| 384 | + } |
|
| 385 | + } |
|
| 386 | + if ($autoInit) { |
|
| 387 | + return auteur_attribuer_jeton($id_auteur); |
|
| 388 | + } |
|
| 389 | + return null; |
|
| 390 | 390 | } |
| 391 | 391 | |
| 392 | 392 | /** |
@@ -396,24 +396,24 @@ discard block |
||
| 396 | 396 | * @return array|bool |
| 397 | 397 | */ |
| 398 | 398 | function auteur_verifier_jeton($jeton) { |
| 399 | - // refuser un jeton corrompu |
|
| 400 | - if (preg_match(',[^0-9a-f.],i', $jeton)) { |
|
| 401 | - return false; |
|
| 402 | - } |
|
| 403 | - |
|
| 404 | - include_spip('base/abstract_sql'); |
|
| 405 | - $public = substr($jeton, 0, 8); |
|
| 406 | - |
|
| 407 | - // Les auteurs qui ont un jetons ressemblant |
|
| 408 | - $auteurs = sql_allfetsel('*', 'spip_auteurs', 'cookie_oubli LIKE ' . sql_quote($public . '%')); |
|
| 409 | - foreach ($auteurs as $auteur) { |
|
| 410 | - $jeton_chiffre = substr((string) $auteur['cookie_oubli'], 8); |
|
| 411 | - $_jeton = Chiffrement::dechiffrer($jeton_chiffre, SpipCles::secret_du_site()); |
|
| 412 | - if ($_jeton && hash_equals($jeton, $_jeton)) { |
|
| 413 | - return $auteur; |
|
| 414 | - } |
|
| 415 | - } |
|
| 416 | - return false; |
|
| 399 | + // refuser un jeton corrompu |
|
| 400 | + if (preg_match(',[^0-9a-f.],i', $jeton)) { |
|
| 401 | + return false; |
|
| 402 | + } |
|
| 403 | + |
|
| 404 | + include_spip('base/abstract_sql'); |
|
| 405 | + $public = substr($jeton, 0, 8); |
|
| 406 | + |
|
| 407 | + // Les auteurs qui ont un jetons ressemblant |
|
| 408 | + $auteurs = sql_allfetsel('*', 'spip_auteurs', 'cookie_oubli LIKE ' . sql_quote($public . '%')); |
|
| 409 | + foreach ($auteurs as $auteur) { |
|
| 410 | + $jeton_chiffre = substr((string) $auteur['cookie_oubli'], 8); |
|
| 411 | + $_jeton = Chiffrement::dechiffrer($jeton_chiffre, SpipCles::secret_du_site()); |
|
| 412 | + if ($_jeton && hash_equals($jeton, $_jeton)) { |
|
| 413 | + return $auteur; |
|
| 414 | + } |
|
| 415 | + } |
|
| 416 | + return false; |
|
| 417 | 417 | } |
| 418 | 418 | |
| 419 | 419 | /** |
@@ -423,6 +423,6 @@ discard block |
||
| 423 | 423 | * @return bool |
| 424 | 424 | */ |
| 425 | 425 | function auteur_effacer_jeton($id_auteur) { |
| 426 | - include_spip('base/abstract_sql'); |
|
| 427 | - return sql_updateq('spip_auteurs', ['cookie_oubli' => ''], 'id_auteur=' . (int) $id_auteur); |
|
| 426 | + include_spip('base/abstract_sql'); |
|
| 427 | + return sql_updateq('spip_auteurs', ['cookie_oubli' => ''], 'id_auteur=' . (int) $id_auteur); |
|
| 428 | 428 | } |
@@ -58,7 +58,7 @@ discard block |
||
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | include_spip('base/abstract_sql'); |
| 61 | - $res = sql_select('statut, id_auteur, login, email, nom', 'spip_auteurs', 'email=' . sql_quote($desc['email'])); |
|
| 61 | + $res = sql_select('statut, id_auteur, login, email, nom', 'spip_auteurs', 'email='.sql_quote($desc['email'])); |
|
| 62 | 62 | // erreur ? |
| 63 | 63 | if (!$res) { |
| 64 | 64 | return _T('titre_probleme_technique'); |
@@ -210,11 +210,11 @@ discard block |
||
| 210 | 210 | |
| 211 | 211 | $login = $login_base; |
| 212 | 212 | |
| 213 | - for ($i = 1;; $i++) { |
|
| 213 | + for ($i = 1; ; $i++) { |
|
| 214 | 214 | if (!sql_countsel('spip_auteurs', "login='$login'")) { |
| 215 | 215 | return $login; |
| 216 | 216 | } |
| 217 | - $login = $login_base . $i; |
|
| 217 | + $login = $login_base.$i; |
|
| 218 | 218 | } |
| 219 | 219 | } |
| 220 | 220 | |
@@ -354,11 +354,11 @@ discard block |
||
| 354 | 354 | do { |
| 355 | 355 | // Un morceau du jeton est lisible en bdd pour éviter de devoir déchiffrer |
| 356 | 356 | // tous les jetons connus pour vérifier le jeton d’un auteur. |
| 357 | - $public = substr((string) creer_uniqid(), 0, 7) . '.'; |
|
| 358 | - $jeton = $public . creer_uniqid(); |
|
| 359 | - $jeton_chiffre_prefixe = $public . Chiffrement::chiffrer($jeton, SpipCles::secret_du_site()); |
|
| 360 | - sql_updateq('spip_auteurs', ['cookie_oubli' => $jeton_chiffre_prefixe], 'id_auteur=' . (int) $id_auteur); |
|
| 361 | - } while (sql_countsel('spip_auteurs', 'cookie_oubli=' . sql_quote($jeton_chiffre_prefixe, '', 'string')) > 1); |
|
| 357 | + $public = substr((string) creer_uniqid(), 0, 7).'.'; |
|
| 358 | + $jeton = $public.creer_uniqid(); |
|
| 359 | + $jeton_chiffre_prefixe = $public.Chiffrement::chiffrer($jeton, SpipCles::secret_du_site()); |
|
| 360 | + sql_updateq('spip_auteurs', ['cookie_oubli' => $jeton_chiffre_prefixe], 'id_auteur='.(int) $id_auteur); |
|
| 361 | + } while (sql_countsel('spip_auteurs', 'cookie_oubli='.sql_quote($jeton_chiffre_prefixe, '', 'string')) > 1); |
|
| 362 | 362 | |
| 363 | 363 | return $jeton; |
| 364 | 364 | } |
@@ -375,7 +375,7 @@ discard block |
||
| 375 | 375 | */ |
| 376 | 376 | function auteur_lire_jeton(int $id_auteur, bool $autoInit = false): ?string { |
| 377 | 377 | include_spip('base/abstract_sql'); |
| 378 | - $jeton_chiffre_prefixe = sql_getfetsel('cookie_oubli', 'spip_auteurs', 'id_auteur=' . $id_auteur); |
|
| 378 | + $jeton_chiffre_prefixe = sql_getfetsel('cookie_oubli', 'spip_auteurs', 'id_auteur='.$id_auteur); |
|
| 379 | 379 | if ($jeton_chiffre_prefixe) { |
| 380 | 380 | $jeton_chiffre = substr((string) $jeton_chiffre_prefixe, 8); |
| 381 | 381 | $jeton = Chiffrement::dechiffrer($jeton_chiffre, SpipCles::secret_du_site()); |
@@ -405,7 +405,7 @@ discard block |
||
| 405 | 405 | $public = substr($jeton, 0, 8); |
| 406 | 406 | |
| 407 | 407 | // Les auteurs qui ont un jetons ressemblant |
| 408 | - $auteurs = sql_allfetsel('*', 'spip_auteurs', 'cookie_oubli LIKE ' . sql_quote($public . '%')); |
|
| 408 | + $auteurs = sql_allfetsel('*', 'spip_auteurs', 'cookie_oubli LIKE '.sql_quote($public.'%')); |
|
| 409 | 409 | foreach ($auteurs as $auteur) { |
| 410 | 410 | $jeton_chiffre = substr((string) $auteur['cookie_oubli'], 8); |
| 411 | 411 | $_jeton = Chiffrement::dechiffrer($jeton_chiffre, SpipCles::secret_du_site()); |
@@ -424,5 +424,5 @@ discard block |
||
| 424 | 424 | */ |
| 425 | 425 | function auteur_effacer_jeton($id_auteur) { |
| 426 | 426 | include_spip('base/abstract_sql'); |
| 427 | - return sql_updateq('spip_auteurs', ['cookie_oubli' => ''], 'id_auteur=' . (int) $id_auteur); |
|
| 427 | + return sql_updateq('spip_auteurs', ['cookie_oubli' => ''], 'id_auteur='.(int) $id_auteur); |
|
| 428 | 428 | } |
@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | |
| 18 | 18 | if (!defined('_ECRIRE_INC_VERSION')) { |
| 19 | - return; |
|
| 19 | + return; |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | include_spip('inc/actions'); |
@@ -32,93 +32,93 @@ discard block |
||
| 32 | 32 | * @return void |
| 33 | 33 | */ |
| 34 | 34 | function action_cookie_dist($set_cookie_admin = null, $change_session = null) { |
| 35 | - $redirect_echec = $redirect = null; |
|
| 36 | - $test_echec_cookie = null; |
|
| 37 | - $url = ''; |
|
| 38 | - if (is_null($set_cookie_admin)) { |
|
| 39 | - $set_cookie_admin = _request('cookie_admin'); |
|
| 40 | - $change_session = _request('change_session'); |
|
| 41 | - $test_echec_cookie = _request('test_echec_cookie'); |
|
| 35 | + $redirect_echec = $redirect = null; |
|
| 36 | + $test_echec_cookie = null; |
|
| 37 | + $url = ''; |
|
| 38 | + if (is_null($set_cookie_admin)) { |
|
| 39 | + $set_cookie_admin = _request('cookie_admin'); |
|
| 40 | + $change_session = _request('change_session'); |
|
| 41 | + $test_echec_cookie = _request('test_echec_cookie'); |
|
| 42 | 42 | |
| 43 | - // La cible de notre operation de connexion |
|
| 44 | - $url = securiser_redirect_action(_request('url')); |
|
| 45 | - $redirect = $url ?: generer_url_ecrire('accueil'); |
|
| 46 | - $redirect_echec = _request('url_echec'); |
|
| 47 | - if (!isset($redirect_echec)) { |
|
| 48 | - if (str_contains((string) $redirect, (string) _DIR_RESTREINT_ABS)) { |
|
| 49 | - $redirect_echec = generer_url_public('login', '', true); |
|
| 50 | - } else { |
|
| 51 | - $redirect_echec = $redirect; |
|
| 52 | - } |
|
| 53 | - } |
|
| 54 | - } |
|
| 43 | + // La cible de notre operation de connexion |
|
| 44 | + $url = securiser_redirect_action(_request('url')); |
|
| 45 | + $redirect = $url ?: generer_url_ecrire('accueil'); |
|
| 46 | + $redirect_echec = _request('url_echec'); |
|
| 47 | + if (!isset($redirect_echec)) { |
|
| 48 | + if (str_contains((string) $redirect, (string) _DIR_RESTREINT_ABS)) { |
|
| 49 | + $redirect_echec = generer_url_public('login', '', true); |
|
| 50 | + } else { |
|
| 51 | + $redirect_echec = $redirect; |
|
| 52 | + } |
|
| 53 | + } |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | 56 | |
| 57 | - // rejoue le cookie pour renouveler spip_session |
|
| 58 | - if ($change_session == 'oui') { |
|
| 59 | - $session = charger_fonction('session', 'inc'); |
|
| 60 | - $session(true); |
|
| 61 | - spip_log('statut 204 pour ' . $_SERVER['REQUEST_URI']); |
|
| 62 | - http_response_code(204); // No Content |
|
| 63 | - return; |
|
| 64 | - } |
|
| 57 | + // rejoue le cookie pour renouveler spip_session |
|
| 58 | + if ($change_session == 'oui') { |
|
| 59 | + $session = charger_fonction('session', 'inc'); |
|
| 60 | + $session(true); |
|
| 61 | + spip_log('statut 204 pour ' . $_SERVER['REQUEST_URI']); |
|
| 62 | + http_response_code(204); // No Content |
|
| 63 | + return; |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - // tentative de connexion en auth_http |
|
| 67 | - if (_request('essai_auth_http') && !$GLOBALS['ignore_auth_http']) { |
|
| 68 | - include_spip('inc/auth'); |
|
| 69 | - if ( |
|
| 70 | - @$_SERVER['PHP_AUTH_USER'] |
|
| 71 | - && @$_SERVER['PHP_AUTH_PW'] |
|
| 72 | - && ($auteur = lire_php_auth($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'])) |
|
| 73 | - ) { |
|
| 74 | - auth_loger($auteur); |
|
| 75 | - redirige_par_entete(parametre_url($redirect, 't', time(), '&')); |
|
| 76 | - } else { |
|
| 77 | - ask_php_auth( |
|
| 78 | - _T('info_connexion_refusee'), |
|
| 79 | - _T('login_login_pass_incorrect'), |
|
| 80 | - _T('login_retour_site'), |
|
| 81 | - 'url=' . rawurlencode((string) $redirect), |
|
| 82 | - _T('login_nouvelle_tentative'), |
|
| 83 | - (str_contains((string) $url, (string) _DIR_RESTREINT_ABS)) |
|
| 84 | - ); |
|
| 85 | - } |
|
| 86 | - } else { |
|
| 87 | - // en cas de login sur bonjour=oui, on tente de poser un cookie |
|
| 88 | - // puis de passer au login qui diagnostiquera l'echec de cookie |
|
| 89 | - // le cas echeant. |
|
| 90 | - if ($test_echec_cookie == 'oui') { |
|
| 91 | - spip_setcookie('spip_session', 'test_echec_cookie'); |
|
| 92 | - if ($redirect) { |
|
| 93 | - $redirect = parametre_url( |
|
| 94 | - parametre_url($redirect_echec, 'var_echec_cookie', 'oui', '&'), |
|
| 95 | - 'url', |
|
| 96 | - rawurlencode((string) $redirect), |
|
| 97 | - '&' |
|
| 98 | - ); |
|
| 99 | - } |
|
| 100 | - } else { |
|
| 101 | - $cook = $_COOKIE['spip_admin'] ?? ''; |
|
| 102 | - // Suppression cookie d'admin ? |
|
| 103 | - if ($set_cookie_admin == 'non') { |
|
| 104 | - if ($cook) { |
|
| 105 | - spip_setcookie('spip_admin', $cook, [ |
|
| 106 | - 'expires' => time() - 3600 * 24 |
|
| 107 | - ]); |
|
| 108 | - } |
|
| 109 | - } // Ajout de cookie d'admin |
|
| 110 | - else { |
|
| 111 | - if ($set_cookie_admin && _DUREE_COOKIE_ADMIN) { |
|
| 112 | - spip_setcookie('spip_admin', $set_cookie_admin, [ |
|
| 113 | - 'expires' => time() + max(_DUREE_COOKIE_ADMIN, 2 * _RENOUVELLE_ALEA) |
|
| 114 | - ]); |
|
| 115 | - } |
|
| 116 | - } |
|
| 117 | - } |
|
| 118 | - } |
|
| 66 | + // tentative de connexion en auth_http |
|
| 67 | + if (_request('essai_auth_http') && !$GLOBALS['ignore_auth_http']) { |
|
| 68 | + include_spip('inc/auth'); |
|
| 69 | + if ( |
|
| 70 | + @$_SERVER['PHP_AUTH_USER'] |
|
| 71 | + && @$_SERVER['PHP_AUTH_PW'] |
|
| 72 | + && ($auteur = lire_php_auth($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'])) |
|
| 73 | + ) { |
|
| 74 | + auth_loger($auteur); |
|
| 75 | + redirige_par_entete(parametre_url($redirect, 't', time(), '&')); |
|
| 76 | + } else { |
|
| 77 | + ask_php_auth( |
|
| 78 | + _T('info_connexion_refusee'), |
|
| 79 | + _T('login_login_pass_incorrect'), |
|
| 80 | + _T('login_retour_site'), |
|
| 81 | + 'url=' . rawurlencode((string) $redirect), |
|
| 82 | + _T('login_nouvelle_tentative'), |
|
| 83 | + (str_contains((string) $url, (string) _DIR_RESTREINT_ABS)) |
|
| 84 | + ); |
|
| 85 | + } |
|
| 86 | + } else { |
|
| 87 | + // en cas de login sur bonjour=oui, on tente de poser un cookie |
|
| 88 | + // puis de passer au login qui diagnostiquera l'echec de cookie |
|
| 89 | + // le cas echeant. |
|
| 90 | + if ($test_echec_cookie == 'oui') { |
|
| 91 | + spip_setcookie('spip_session', 'test_echec_cookie'); |
|
| 92 | + if ($redirect) { |
|
| 93 | + $redirect = parametre_url( |
|
| 94 | + parametre_url($redirect_echec, 'var_echec_cookie', 'oui', '&'), |
|
| 95 | + 'url', |
|
| 96 | + rawurlencode((string) $redirect), |
|
| 97 | + '&' |
|
| 98 | + ); |
|
| 99 | + } |
|
| 100 | + } else { |
|
| 101 | + $cook = $_COOKIE['spip_admin'] ?? ''; |
|
| 102 | + // Suppression cookie d'admin ? |
|
| 103 | + if ($set_cookie_admin == 'non') { |
|
| 104 | + if ($cook) { |
|
| 105 | + spip_setcookie('spip_admin', $cook, [ |
|
| 106 | + 'expires' => time() - 3600 * 24 |
|
| 107 | + ]); |
|
| 108 | + } |
|
| 109 | + } // Ajout de cookie d'admin |
|
| 110 | + else { |
|
| 111 | + if ($set_cookie_admin && _DUREE_COOKIE_ADMIN) { |
|
| 112 | + spip_setcookie('spip_admin', $set_cookie_admin, [ |
|
| 113 | + 'expires' => time() + max(_DUREE_COOKIE_ADMIN, 2 * _RENOUVELLE_ALEA) |
|
| 114 | + ]); |
|
| 115 | + } |
|
| 116 | + } |
|
| 117 | + } |
|
| 118 | + } |
|
| 119 | 119 | |
| 120 | - // Redirection finale |
|
| 121 | - if ($redirect) { |
|
| 122 | - redirige_par_entete($redirect, true); |
|
| 123 | - } |
|
| 120 | + // Redirection finale |
|
| 121 | + if ($redirect) { |
|
| 122 | + redirige_par_entete($redirect, true); |
|
| 123 | + } |
|
| 124 | 124 | } |
@@ -58,7 +58,7 @@ discard block |
||
| 58 | 58 | if ($change_session == 'oui') { |
| 59 | 59 | $session = charger_fonction('session', 'inc'); |
| 60 | 60 | $session(true); |
| 61 | - spip_log('statut 204 pour ' . $_SERVER['REQUEST_URI']); |
|
| 61 | + spip_log('statut 204 pour '.$_SERVER['REQUEST_URI']); |
|
| 62 | 62 | http_response_code(204); // No Content |
| 63 | 63 | return; |
| 64 | 64 | } |
@@ -78,7 +78,7 @@ discard block |
||
| 78 | 78 | _T('info_connexion_refusee'), |
| 79 | 79 | _T('login_login_pass_incorrect'), |
| 80 | 80 | _T('login_retour_site'), |
| 81 | - 'url=' . rawurlencode((string) $redirect), |
|
| 81 | + 'url='.rawurlencode((string) $redirect), |
|
| 82 | 82 | _T('login_nouvelle_tentative'), |
| 83 | 83 | (str_contains((string) $url, (string) _DIR_RESTREINT_ABS)) |
| 84 | 84 | ); |
@@ -9,195 +9,195 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class Traducteur |
| 11 | 11 | { |
| 12 | - /** Pour les corrections à effectuer sur les requêtes : array(code=>'texte') trouvé |
|
| 13 | - * |
|
| 14 | - * @var array |
|
| 15 | - */ |
|
| 16 | - public $textes = []; |
|
| 17 | - |
|
| 18 | - /** |
|
| 19 | - * Constructeur |
|
| 20 | - */ |
|
| 21 | - public function __construct( |
|
| 22 | - /** Requête à préparer */ |
|
| 23 | - public string $query, |
|
| 24 | - /** Prefixe des tables à utiliser */ |
|
| 25 | - public string $prefixe, |
|
| 26 | - /** Version SQLite (2 ou 3) */ |
|
| 27 | - public string $sqlite_version |
|
| 28 | - ) { |
|
| 29 | - } |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * Transformer la requete pour SQLite |
|
| 33 | - * |
|
| 34 | - * Enlève les textes, transforme la requête pour quelle soit |
|
| 35 | - * bien interprétée par SQLite, puis remet les textes |
|
| 36 | - * la fonction affecte `$this->query` |
|
| 37 | - */ |
|
| 38 | - public function traduire_requete() { |
|
| 39 | - // |
|
| 40 | - // 1) Protection des textes en les remplacant par des codes |
|
| 41 | - // |
|
| 42 | - // enlever les 'textes' et initialiser avec |
|
| 43 | - [$this->query, $textes] = query_echappe_textes($this->query); |
|
| 44 | - |
|
| 45 | - // |
|
| 46 | - // 2) Corrections de la requete |
|
| 47 | - // |
|
| 48 | - // Correction Create Database |
|
| 49 | - // Create Database -> requete ignoree |
|
| 50 | - if (str_starts_with((string) $this->query, 'CREATE DATABASE')) { |
|
| 51 | - spip_log("Sqlite : requete non executee -> $this->query", 'sqlite.' . _LOG_AVERTISSEMENT); |
|
| 52 | - $this->query = 'SELECT 1'; |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - // Correction Insert Ignore |
|
| 56 | - // INSERT IGNORE -> insert (tout court et pas 'insert or replace') |
|
| 57 | - if (str_starts_with((string) $this->query, 'INSERT IGNORE')) { |
|
| 58 | - spip_log("Sqlite : requete transformee -> $this->query", 'sqlite.' . _LOG_DEBUG); |
|
| 59 | - $this->query = 'INSERT ' . substr((string) $this->query, '13'); |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - // Correction des dates avec INTERVAL |
|
| 63 | - // utiliser sql_date_proche() de preference |
|
| 64 | - if (str_contains((string) $this->query, 'INTERVAL')) { |
|
| 65 | - $this->query = preg_replace_callback( |
|
| 66 | - '/DATE_(ADD|SUB)(.*)INTERVAL\s+(\d+)\s+([a-zA-Z]+)\)/U', |
|
| 67 | - fn(array $matches): string => $this->_remplacerDateParTime($matches), |
|
| 68 | - (string) $this->query |
|
| 69 | - ); |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - if (str_contains((string) $this->query, 'LEFT(')) { |
|
| 73 | - $this->query = str_replace('LEFT(', '_LEFT(', (string) $this->query); |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - if (str_contains((string) $this->query, 'TIMESTAMPDIFF(')) { |
|
| 77 | - $this->query = preg_replace('/TIMESTAMPDIFF\(\s*([^,]*)\s*,/Uims', "TIMESTAMPDIFF('\\1',", (string) $this->query); |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - |
|
| 81 | - // Correction Using |
|
| 82 | - // USING (non reconnu en sqlite2) |
|
| 83 | - // problematique car la jointure ne se fait pas du coup. |
|
| 84 | - if (($this->sqlite_version == 2) && (str_contains((string) $this->query, 'USING'))) { |
|
| 85 | - spip_log( |
|
| 86 | - "'USING (champ)' n'est pas reconnu en SQLite 2. Utilisez 'ON table1.champ = table2.champ'", |
|
| 87 | - 'sqlite.' . _LOG_ERREUR |
|
| 88 | - ); |
|
| 89 | - $this->query = preg_replace('/USING\s*\([^\)]*\)/', '', (string) $this->query); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - // Correction Field |
|
| 93 | - // remplace FIELD(table,i,j,k...) par CASE WHEN table=i THEN n ... ELSE 0 END |
|
| 94 | - if (str_contains((string) $this->query, 'FIELD')) { |
|
| 95 | - $this->query = preg_replace_callback( |
|
| 96 | - '/FIELD\s*\(([^\)]*)\)/', |
|
| 97 | - fn(array $matches): string => $this->_remplacerFieldParCase($matches), |
|
| 98 | - (string) $this->query |
|
| 99 | - ); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - // Correction des noms de tables FROM |
|
| 103 | - // mettre les bons noms de table dans from, update, insert, replace... |
|
| 104 | - if (preg_match('/\s(SET|VALUES|WHERE|DATABASE)\s/iS', (string) $this->query, $regs)) { |
|
| 105 | - $suite = strstr((string) $this->query, $regs[0]); |
|
| 106 | - $this->query = substr((string) $this->query, 0, -strlen($suite)); |
|
| 107 | - } else { |
|
| 108 | - $suite = ''; |
|
| 109 | - } |
|
| 110 | - $pref = ($this->prefixe) ? $this->prefixe . '_' : ''; |
|
| 111 | - $this->query = preg_replace('/([,\s])spip_/S', '\1' . $pref, (string) $this->query) . $suite; |
|
| 112 | - |
|
| 113 | - // Correction zero AS x |
|
| 114 | - // pg n'aime pas 0+x AS alias, sqlite, dans le meme style, |
|
| 115 | - // n'apprecie pas du tout SELECT 0 as x ... ORDER BY x |
|
| 116 | - // il dit que x ne doit pas être un integer dans le order by ! |
|
| 117 | - // on remplace du coup x par vide() dans ce cas uniquement |
|
| 118 | - // |
|
| 119 | - // apparait dans public/vertebrer.php et dans le plugin menu aussi qui genere aussi ce genre de requete via un {par num #GET{tri_num}} |
|
| 120 | - // mais est-ce encore un soucis pour sqlite en 2021 ? (ie commenter le preg_replace marche très bien en sqlite 3.28) |
|
| 121 | - // on ne remplace que dans ORDER BY ou GROUP BY |
|
| 122 | - if (str_contains($this->query, '0 AS') && preg_match('/\s(ORDER|GROUP) BY\s/i', $this->query, $regs)) { |
|
| 123 | - $suite = strstr($this->query, $regs[0]); |
|
| 124 | - $this->query = substr($this->query, 0, -strlen($suite)); |
|
| 125 | - // on cherche les noms des x dans 0 AS x |
|
| 126 | - // on remplace dans $suite le nom par vide() |
|
| 127 | - preg_match_all('/\b0 AS\s*([^\s,]+)/', $this->query, $matches, PREG_PATTERN_ORDER); |
|
| 128 | - foreach ($matches[1] as $m) { |
|
| 129 | - if (str_contains($suite, $m)) { |
|
| 130 | - $suite = preg_replace(",\b$m\b,", 'VIDE()', $suite); |
|
| 131 | - } |
|
| 132 | - } |
|
| 133 | - $this->query .= $suite; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - // Correction possible des divisions entieres |
|
| 137 | - // Le standard SQL (lequel? ou?) semble indiquer que |
|
| 138 | - // a/b=c doit donner c entier si a et b sont entiers 4/3=1. |
|
| 139 | - // C'est ce que retournent effectivement SQL Server et SQLite |
|
| 140 | - // Ce n'est pas ce qu'applique MySQL qui retourne un reel : 4/3=1.333... |
|
| 141 | - // |
|
| 142 | - // On peut forcer la conversion en multipliant par 1.0 avant la division |
|
| 143 | - // /!\ SQLite 3.5.9 Debian/Ubuntu est victime d'un bug en plus ! |
|
| 144 | - // cf. https://bugs.launchpad.net/ubuntu/+source/sqlite3/+bug/254228 |
|
| 145 | - // http://www.sqlite.org/cvstrac/tktview?tn=3202 |
|
| 146 | - // (4*1.0/3) n'est pas rendu dans ce cas ! |
|
| 147 | - # $this->query = str_replace('/','* 1.00 / ',$this->query); |
|
| 148 | - |
|
| 149 | - |
|
| 150 | - // Correction critere REGEXP, non reconnu en sqlite2 |
|
| 151 | - if (($this->sqlite_version == 2) && (str_contains($this->query, 'REGEXP'))) { |
|
| 152 | - $this->query = preg_replace('/([^\s\(]*)(\s*)REGEXP(\s*)([^\s\)]*)/', 'REGEXP($4, $1)', $this->query); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - // |
|
| 156 | - // 3) Remise en place des textes d'origine |
|
| 157 | - // |
|
| 158 | - // Correction Antiquotes et echappements |
|
| 159 | - // ` => rien |
|
| 160 | - if (str_contains($this->query, '`')) { |
|
| 161 | - $this->query = str_replace('`', '', $this->query); |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - $this->query = query_reinjecte_textes($this->query, $textes); |
|
| 165 | - |
|
| 166 | - return $this->query; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * Callback pour remplacer `DATE_` / `INTERVAL` |
|
| 171 | - * par `DATE ... strtotime` |
|
| 172 | - * |
|
| 173 | - * @param array $matches Captures |
|
| 174 | - * @return string texte de date compris par SQLite |
|
| 175 | - */ |
|
| 176 | - public function _remplacerDateParTime($matches) { |
|
| 177 | - $op = strtoupper($matches[1] == 'ADD') ? '+' : '-'; |
|
| 178 | - |
|
| 179 | - return "datetime$matches[2] '$op$matches[3] $matches[4]')"; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * Callback pour remplacer `FIELD(table,i,j,k...)` |
|
| 184 | - * par `CASE WHEN table=i THEN n ... ELSE 0 END` |
|
| 185 | - * |
|
| 186 | - * @param array $matches Captures |
|
| 187 | - * @return string texte de liste ordonnée compris par SQLite |
|
| 188 | - */ |
|
| 189 | - public function _remplacerFieldParCase($matches) { |
|
| 190 | - $fields = substr((string) $matches[0], 6, -1); // ne recuperer que l'interieur X de field(X) |
|
| 191 | - $t = explode(',', $fields); |
|
| 192 | - $index = array_shift($t); |
|
| 193 | - |
|
| 194 | - $res = ''; |
|
| 195 | - $n = 0; |
|
| 196 | - foreach ($t as $v) { |
|
| 197 | - $n++; |
|
| 198 | - $res .= "\nWHEN $index=$v THEN $n"; |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - return "CASE $res ELSE 0 END "; |
|
| 202 | - } |
|
| 12 | + /** Pour les corrections à effectuer sur les requêtes : array(code=>'texte') trouvé |
|
| 13 | + * |
|
| 14 | + * @var array |
|
| 15 | + */ |
|
| 16 | + public $textes = []; |
|
| 17 | + |
|
| 18 | + /** |
|
| 19 | + * Constructeur |
|
| 20 | + */ |
|
| 21 | + public function __construct( |
|
| 22 | + /** Requête à préparer */ |
|
| 23 | + public string $query, |
|
| 24 | + /** Prefixe des tables à utiliser */ |
|
| 25 | + public string $prefixe, |
|
| 26 | + /** Version SQLite (2 ou 3) */ |
|
| 27 | + public string $sqlite_version |
|
| 28 | + ) { |
|
| 29 | + } |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * Transformer la requete pour SQLite |
|
| 33 | + * |
|
| 34 | + * Enlève les textes, transforme la requête pour quelle soit |
|
| 35 | + * bien interprétée par SQLite, puis remet les textes |
|
| 36 | + * la fonction affecte `$this->query` |
|
| 37 | + */ |
|
| 38 | + public function traduire_requete() { |
|
| 39 | + // |
|
| 40 | + // 1) Protection des textes en les remplacant par des codes |
|
| 41 | + // |
|
| 42 | + // enlever les 'textes' et initialiser avec |
|
| 43 | + [$this->query, $textes] = query_echappe_textes($this->query); |
|
| 44 | + |
|
| 45 | + // |
|
| 46 | + // 2) Corrections de la requete |
|
| 47 | + // |
|
| 48 | + // Correction Create Database |
|
| 49 | + // Create Database -> requete ignoree |
|
| 50 | + if (str_starts_with((string) $this->query, 'CREATE DATABASE')) { |
|
| 51 | + spip_log("Sqlite : requete non executee -> $this->query", 'sqlite.' . _LOG_AVERTISSEMENT); |
|
| 52 | + $this->query = 'SELECT 1'; |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + // Correction Insert Ignore |
|
| 56 | + // INSERT IGNORE -> insert (tout court et pas 'insert or replace') |
|
| 57 | + if (str_starts_with((string) $this->query, 'INSERT IGNORE')) { |
|
| 58 | + spip_log("Sqlite : requete transformee -> $this->query", 'sqlite.' . _LOG_DEBUG); |
|
| 59 | + $this->query = 'INSERT ' . substr((string) $this->query, '13'); |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + // Correction des dates avec INTERVAL |
|
| 63 | + // utiliser sql_date_proche() de preference |
|
| 64 | + if (str_contains((string) $this->query, 'INTERVAL')) { |
|
| 65 | + $this->query = preg_replace_callback( |
|
| 66 | + '/DATE_(ADD|SUB)(.*)INTERVAL\s+(\d+)\s+([a-zA-Z]+)\)/U', |
|
| 67 | + fn(array $matches): string => $this->_remplacerDateParTime($matches), |
|
| 68 | + (string) $this->query |
|
| 69 | + ); |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + if (str_contains((string) $this->query, 'LEFT(')) { |
|
| 73 | + $this->query = str_replace('LEFT(', '_LEFT(', (string) $this->query); |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + if (str_contains((string) $this->query, 'TIMESTAMPDIFF(')) { |
|
| 77 | + $this->query = preg_replace('/TIMESTAMPDIFF\(\s*([^,]*)\s*,/Uims', "TIMESTAMPDIFF('\\1',", (string) $this->query); |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + |
|
| 81 | + // Correction Using |
|
| 82 | + // USING (non reconnu en sqlite2) |
|
| 83 | + // problematique car la jointure ne se fait pas du coup. |
|
| 84 | + if (($this->sqlite_version == 2) && (str_contains((string) $this->query, 'USING'))) { |
|
| 85 | + spip_log( |
|
| 86 | + "'USING (champ)' n'est pas reconnu en SQLite 2. Utilisez 'ON table1.champ = table2.champ'", |
|
| 87 | + 'sqlite.' . _LOG_ERREUR |
|
| 88 | + ); |
|
| 89 | + $this->query = preg_replace('/USING\s*\([^\)]*\)/', '', (string) $this->query); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + // Correction Field |
|
| 93 | + // remplace FIELD(table,i,j,k...) par CASE WHEN table=i THEN n ... ELSE 0 END |
|
| 94 | + if (str_contains((string) $this->query, 'FIELD')) { |
|
| 95 | + $this->query = preg_replace_callback( |
|
| 96 | + '/FIELD\s*\(([^\)]*)\)/', |
|
| 97 | + fn(array $matches): string => $this->_remplacerFieldParCase($matches), |
|
| 98 | + (string) $this->query |
|
| 99 | + ); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + // Correction des noms de tables FROM |
|
| 103 | + // mettre les bons noms de table dans from, update, insert, replace... |
|
| 104 | + if (preg_match('/\s(SET|VALUES|WHERE|DATABASE)\s/iS', (string) $this->query, $regs)) { |
|
| 105 | + $suite = strstr((string) $this->query, $regs[0]); |
|
| 106 | + $this->query = substr((string) $this->query, 0, -strlen($suite)); |
|
| 107 | + } else { |
|
| 108 | + $suite = ''; |
|
| 109 | + } |
|
| 110 | + $pref = ($this->prefixe) ? $this->prefixe . '_' : ''; |
|
| 111 | + $this->query = preg_replace('/([,\s])spip_/S', '\1' . $pref, (string) $this->query) . $suite; |
|
| 112 | + |
|
| 113 | + // Correction zero AS x |
|
| 114 | + // pg n'aime pas 0+x AS alias, sqlite, dans le meme style, |
|
| 115 | + // n'apprecie pas du tout SELECT 0 as x ... ORDER BY x |
|
| 116 | + // il dit que x ne doit pas être un integer dans le order by ! |
|
| 117 | + // on remplace du coup x par vide() dans ce cas uniquement |
|
| 118 | + // |
|
| 119 | + // apparait dans public/vertebrer.php et dans le plugin menu aussi qui genere aussi ce genre de requete via un {par num #GET{tri_num}} |
|
| 120 | + // mais est-ce encore un soucis pour sqlite en 2021 ? (ie commenter le preg_replace marche très bien en sqlite 3.28) |
|
| 121 | + // on ne remplace que dans ORDER BY ou GROUP BY |
|
| 122 | + if (str_contains($this->query, '0 AS') && preg_match('/\s(ORDER|GROUP) BY\s/i', $this->query, $regs)) { |
|
| 123 | + $suite = strstr($this->query, $regs[0]); |
|
| 124 | + $this->query = substr($this->query, 0, -strlen($suite)); |
|
| 125 | + // on cherche les noms des x dans 0 AS x |
|
| 126 | + // on remplace dans $suite le nom par vide() |
|
| 127 | + preg_match_all('/\b0 AS\s*([^\s,]+)/', $this->query, $matches, PREG_PATTERN_ORDER); |
|
| 128 | + foreach ($matches[1] as $m) { |
|
| 129 | + if (str_contains($suite, $m)) { |
|
| 130 | + $suite = preg_replace(",\b$m\b,", 'VIDE()', $suite); |
|
| 131 | + } |
|
| 132 | + } |
|
| 133 | + $this->query .= $suite; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + // Correction possible des divisions entieres |
|
| 137 | + // Le standard SQL (lequel? ou?) semble indiquer que |
|
| 138 | + // a/b=c doit donner c entier si a et b sont entiers 4/3=1. |
|
| 139 | + // C'est ce que retournent effectivement SQL Server et SQLite |
|
| 140 | + // Ce n'est pas ce qu'applique MySQL qui retourne un reel : 4/3=1.333... |
|
| 141 | + // |
|
| 142 | + // On peut forcer la conversion en multipliant par 1.0 avant la division |
|
| 143 | + // /!\ SQLite 3.5.9 Debian/Ubuntu est victime d'un bug en plus ! |
|
| 144 | + // cf. https://bugs.launchpad.net/ubuntu/+source/sqlite3/+bug/254228 |
|
| 145 | + // http://www.sqlite.org/cvstrac/tktview?tn=3202 |
|
| 146 | + // (4*1.0/3) n'est pas rendu dans ce cas ! |
|
| 147 | + # $this->query = str_replace('/','* 1.00 / ',$this->query); |
|
| 148 | + |
|
| 149 | + |
|
| 150 | + // Correction critere REGEXP, non reconnu en sqlite2 |
|
| 151 | + if (($this->sqlite_version == 2) && (str_contains($this->query, 'REGEXP'))) { |
|
| 152 | + $this->query = preg_replace('/([^\s\(]*)(\s*)REGEXP(\s*)([^\s\)]*)/', 'REGEXP($4, $1)', $this->query); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + // |
|
| 156 | + // 3) Remise en place des textes d'origine |
|
| 157 | + // |
|
| 158 | + // Correction Antiquotes et echappements |
|
| 159 | + // ` => rien |
|
| 160 | + if (str_contains($this->query, '`')) { |
|
| 161 | + $this->query = str_replace('`', '', $this->query); |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + $this->query = query_reinjecte_textes($this->query, $textes); |
|
| 165 | + |
|
| 166 | + return $this->query; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * Callback pour remplacer `DATE_` / `INTERVAL` |
|
| 171 | + * par `DATE ... strtotime` |
|
| 172 | + * |
|
| 173 | + * @param array $matches Captures |
|
| 174 | + * @return string texte de date compris par SQLite |
|
| 175 | + */ |
|
| 176 | + public function _remplacerDateParTime($matches) { |
|
| 177 | + $op = strtoupper($matches[1] == 'ADD') ? '+' : '-'; |
|
| 178 | + |
|
| 179 | + return "datetime$matches[2] '$op$matches[3] $matches[4]')"; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * Callback pour remplacer `FIELD(table,i,j,k...)` |
|
| 184 | + * par `CASE WHEN table=i THEN n ... ELSE 0 END` |
|
| 185 | + * |
|
| 186 | + * @param array $matches Captures |
|
| 187 | + * @return string texte de liste ordonnée compris par SQLite |
|
| 188 | + */ |
|
| 189 | + public function _remplacerFieldParCase($matches) { |
|
| 190 | + $fields = substr((string) $matches[0], 6, -1); // ne recuperer que l'interieur X de field(X) |
|
| 191 | + $t = explode(',', $fields); |
|
| 192 | + $index = array_shift($t); |
|
| 193 | + |
|
| 194 | + $res = ''; |
|
| 195 | + $n = 0; |
|
| 196 | + foreach ($t as $v) { |
|
| 197 | + $n++; |
|
| 198 | + $res .= "\nWHEN $index=$v THEN $n"; |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + return "CASE $res ELSE 0 END "; |
|
| 202 | + } |
|
| 203 | 203 | } |
@@ -48,15 +48,15 @@ discard block |
||
| 48 | 48 | // Correction Create Database |
| 49 | 49 | // Create Database -> requete ignoree |
| 50 | 50 | if (str_starts_with((string) $this->query, 'CREATE DATABASE')) { |
| 51 | - spip_log("Sqlite : requete non executee -> $this->query", 'sqlite.' . _LOG_AVERTISSEMENT); |
|
| 51 | + spip_log("Sqlite : requete non executee -> $this->query", 'sqlite.'._LOG_AVERTISSEMENT); |
|
| 52 | 52 | $this->query = 'SELECT 1'; |
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | // Correction Insert Ignore |
| 56 | 56 | // INSERT IGNORE -> insert (tout court et pas 'insert or replace') |
| 57 | 57 | if (str_starts_with((string) $this->query, 'INSERT IGNORE')) { |
| 58 | - spip_log("Sqlite : requete transformee -> $this->query", 'sqlite.' . _LOG_DEBUG); |
|
| 59 | - $this->query = 'INSERT ' . substr((string) $this->query, '13'); |
|
| 58 | + spip_log("Sqlite : requete transformee -> $this->query", 'sqlite.'._LOG_DEBUG); |
|
| 59 | + $this->query = 'INSERT '.substr((string) $this->query, '13'); |
|
| 60 | 60 | } |
| 61 | 61 | |
| 62 | 62 | // Correction des dates avec INTERVAL |
@@ -84,7 +84,7 @@ discard block |
||
| 84 | 84 | if (($this->sqlite_version == 2) && (str_contains((string) $this->query, 'USING'))) { |
| 85 | 85 | spip_log( |
| 86 | 86 | "'USING (champ)' n'est pas reconnu en SQLite 2. Utilisez 'ON table1.champ = table2.champ'", |
| 87 | - 'sqlite.' . _LOG_ERREUR |
|
| 87 | + 'sqlite.'._LOG_ERREUR |
|
| 88 | 88 | ); |
| 89 | 89 | $this->query = preg_replace('/USING\s*\([^\)]*\)/', '', (string) $this->query); |
| 90 | 90 | } |
@@ -107,8 +107,8 @@ discard block |
||
| 107 | 107 | } else { |
| 108 | 108 | $suite = ''; |
| 109 | 109 | } |
| 110 | - $pref = ($this->prefixe) ? $this->prefixe . '_' : ''; |
|
| 111 | - $this->query = preg_replace('/([,\s])spip_/S', '\1' . $pref, (string) $this->query) . $suite; |
|
| 110 | + $pref = ($this->prefixe) ? $this->prefixe.'_' : ''; |
|
| 111 | + $this->query = preg_replace('/([,\s])spip_/S', '\1'.$pref, (string) $this->query).$suite; |
|
| 112 | 112 | |
| 113 | 113 | // Correction zero AS x |
| 114 | 114 | // pg n'aime pas 0+x AS alias, sqlite, dans le meme style, |
@@ -12,144 +12,144 @@ |
||
| 12 | 12 | namespace Spip\Texte\Collecteur; |
| 13 | 13 | |
| 14 | 14 | abstract class AbstractCollecteur { |
| 15 | - protected static string $markPrefix = 'COLLECT'; |
|
| 16 | - protected string $markId; |
|
| 17 | - |
|
| 18 | - /** |
|
| 19 | - * Collecteur générique des occurences d'une preg dans un texte avec leurs positions et longueur |
|
| 20 | - * @param string $texte |
|
| 21 | - * texte à analyser pour la collecte |
|
| 22 | - * @param string $if_chars |
|
| 23 | - * caractere(s) à tester avant de tenter la preg |
|
| 24 | - * @param string $start_with |
|
| 25 | - * caractere(s) par lesquels commencent l'expression recherchée (permet de démarrer la preg à la prochaine occurence de cette chaine) |
|
| 26 | - * @param string $preg |
|
| 27 | - * preg utilisée pour la collecte |
|
| 28 | - * @param int $max_items |
|
| 29 | - * pour limiter le nombre de preg collectée (pour la detection simple de présence par exemple) |
|
| 30 | - * @return array |
|
| 31 | - */ |
|
| 32 | - protected static function collecteur(string $texte, string $if_chars, string $start_with, string $preg, int $max_items = 0): array { |
|
| 33 | - |
|
| 34 | - $collection = []; |
|
| 35 | - $pos = 0; |
|
| 36 | - while ( |
|
| 37 | - (!$if_chars || str_contains($texte, $if_chars)) |
|
| 38 | - && ($next = ($start_with ? strpos($texte, $start_with, $pos) : $pos)) !== false |
|
| 39 | - && preg_match($preg, $texte, $r, PREG_OFFSET_CAPTURE, $next) |
|
| 40 | - ) { |
|
| 41 | - $found_pos = $r[0][1]; |
|
| 42 | - $found_length = strlen($r[0][0]); |
|
| 43 | - $match = [ |
|
| 44 | - 'raw' => $r[0][0], |
|
| 45 | - 'match' => array_column($r, 0), |
|
| 46 | - 'pos' => $found_pos, |
|
| 47 | - 'length' => $found_length |
|
| 48 | - ]; |
|
| 49 | - |
|
| 50 | - $collection[] = $match; |
|
| 51 | - |
|
| 52 | - if ($max_items && count($collection) === $max_items) { |
|
| 53 | - break; |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - $pos = $match['pos'] + $match['length']; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - return $collection; |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Sanitizer une collection d'occurences |
|
| 64 | - */ |
|
| 65 | - protected function sanitizer_collection(array $collection, string $sanitize_callback): array { |
|
| 66 | - foreach ($collection as &$c) { |
|
| 67 | - $c['raw'] = $sanitize_callback($c['raw']); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - return $collection; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * @return array |
|
| 75 | - */ |
|
| 76 | - public function collecter(string $texte, array $options = []): array { |
|
| 77 | - return []; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - public function detecter($texte): bool { |
|
| 81 | - if (!empty($this->markId) && str_contains((string) $texte, $this->markId)) { |
|
| 82 | - return true; |
|
| 83 | - } |
|
| 84 | - return !empty($this->collecter($texte, ['detecter_presence' => true])); |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Echapper les occurences de la collecte par un texte neutre du point de vue HTML |
|
| 89 | - * |
|
| 90 | - * @see retablir() |
|
| 91 | - * @param string $texte |
|
| 92 | - * @param array $options |
|
| 93 | - * string $sanitize_callback |
|
| 94 | - * @return array |
|
| 95 | - * texte, marqueur utilise pour echapper les modeles |
|
| 96 | - */ |
|
| 97 | - public function echapper(string $texte, array $options = []): string { |
|
| 98 | - if (!function_exists('creer_uniqid')) { |
|
| 99 | - include_spip('inc/acces'); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - $collection = $this->collecter($texte, $options); |
|
| 103 | - if (!empty($options['sanitize_callback']) && is_callable($options['sanitize_callback'])) { |
|
| 104 | - $collection = $this->sanitizer_collection($collection, $options['sanitize_callback']); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - if ($collection !== []) { |
|
| 108 | - if (empty($this->markId)) { |
|
| 109 | - // generer un marqueur qui n'existe pas dans le texte |
|
| 110 | - do { |
|
| 111 | - $this->markId = substr(md5(uniqid(static::class, 1)), 0, 7); |
|
| 112 | - $this->markId = '@|' . static::$markPrefix . $this->markId . '|'; |
|
| 113 | - } while (str_contains($texte, $this->markId)); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - $offset_pos = 0; |
|
| 117 | - foreach ($collection as $c) { |
|
| 118 | - $rempl = $this->markId . base64_encode((string) $c['raw']) . '|@'; |
|
| 119 | - $texte = substr_replace($texte, $rempl, $c['pos'] + $offset_pos, $c['length']); |
|
| 120 | - $offset_pos += strlen($rempl) - $c['length']; |
|
| 121 | - } |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - return $texte; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Retablir les occurences échappées précédemment |
|
| 130 | - * |
|
| 131 | - * @see echapper() |
|
| 132 | - */ |
|
| 133 | - function retablir(string $texte): string { |
|
| 134 | - |
|
| 135 | - if (!empty($this->markId)) { |
|
| 136 | - $lm = strlen($this->markId); |
|
| 137 | - $pos = 0; |
|
| 138 | - while ( |
|
| 139 | - ($p = strpos($texte, $this->markId, $pos)) !== false |
|
| 140 | - && ($end = strpos($texte, '|@', $p + $lm)) |
|
| 141 | - ) { |
|
| 142 | - $base64 = substr($texte, $p + $lm, $end - ($p + $lm)); |
|
| 143 | - if ($c = base64_decode($base64, true)) { |
|
| 144 | - $texte = substr_replace($texte, $c, $p, $end + 2 - $p); |
|
| 145 | - $pos = $p + strlen($c); |
|
| 146 | - } |
|
| 147 | - else { |
|
| 148 | - $pos = $end; |
|
| 149 | - } |
|
| 150 | - } |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - return $texte; |
|
| 154 | - } |
|
| 15 | + protected static string $markPrefix = 'COLLECT'; |
|
| 16 | + protected string $markId; |
|
| 17 | + |
|
| 18 | + /** |
|
| 19 | + * Collecteur générique des occurences d'une preg dans un texte avec leurs positions et longueur |
|
| 20 | + * @param string $texte |
|
| 21 | + * texte à analyser pour la collecte |
|
| 22 | + * @param string $if_chars |
|
| 23 | + * caractere(s) à tester avant de tenter la preg |
|
| 24 | + * @param string $start_with |
|
| 25 | + * caractere(s) par lesquels commencent l'expression recherchée (permet de démarrer la preg à la prochaine occurence de cette chaine) |
|
| 26 | + * @param string $preg |
|
| 27 | + * preg utilisée pour la collecte |
|
| 28 | + * @param int $max_items |
|
| 29 | + * pour limiter le nombre de preg collectée (pour la detection simple de présence par exemple) |
|
| 30 | + * @return array |
|
| 31 | + */ |
|
| 32 | + protected static function collecteur(string $texte, string $if_chars, string $start_with, string $preg, int $max_items = 0): array { |
|
| 33 | + |
|
| 34 | + $collection = []; |
|
| 35 | + $pos = 0; |
|
| 36 | + while ( |
|
| 37 | + (!$if_chars || str_contains($texte, $if_chars)) |
|
| 38 | + && ($next = ($start_with ? strpos($texte, $start_with, $pos) : $pos)) !== false |
|
| 39 | + && preg_match($preg, $texte, $r, PREG_OFFSET_CAPTURE, $next) |
|
| 40 | + ) { |
|
| 41 | + $found_pos = $r[0][1]; |
|
| 42 | + $found_length = strlen($r[0][0]); |
|
| 43 | + $match = [ |
|
| 44 | + 'raw' => $r[0][0], |
|
| 45 | + 'match' => array_column($r, 0), |
|
| 46 | + 'pos' => $found_pos, |
|
| 47 | + 'length' => $found_length |
|
| 48 | + ]; |
|
| 49 | + |
|
| 50 | + $collection[] = $match; |
|
| 51 | + |
|
| 52 | + if ($max_items && count($collection) === $max_items) { |
|
| 53 | + break; |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + $pos = $match['pos'] + $match['length']; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + return $collection; |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Sanitizer une collection d'occurences |
|
| 64 | + */ |
|
| 65 | + protected function sanitizer_collection(array $collection, string $sanitize_callback): array { |
|
| 66 | + foreach ($collection as &$c) { |
|
| 67 | + $c['raw'] = $sanitize_callback($c['raw']); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + return $collection; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * @return array |
|
| 75 | + */ |
|
| 76 | + public function collecter(string $texte, array $options = []): array { |
|
| 77 | + return []; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + public function detecter($texte): bool { |
|
| 81 | + if (!empty($this->markId) && str_contains((string) $texte, $this->markId)) { |
|
| 82 | + return true; |
|
| 83 | + } |
|
| 84 | + return !empty($this->collecter($texte, ['detecter_presence' => true])); |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Echapper les occurences de la collecte par un texte neutre du point de vue HTML |
|
| 89 | + * |
|
| 90 | + * @see retablir() |
|
| 91 | + * @param string $texte |
|
| 92 | + * @param array $options |
|
| 93 | + * string $sanitize_callback |
|
| 94 | + * @return array |
|
| 95 | + * texte, marqueur utilise pour echapper les modeles |
|
| 96 | + */ |
|
| 97 | + public function echapper(string $texte, array $options = []): string { |
|
| 98 | + if (!function_exists('creer_uniqid')) { |
|
| 99 | + include_spip('inc/acces'); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + $collection = $this->collecter($texte, $options); |
|
| 103 | + if (!empty($options['sanitize_callback']) && is_callable($options['sanitize_callback'])) { |
|
| 104 | + $collection = $this->sanitizer_collection($collection, $options['sanitize_callback']); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + if ($collection !== []) { |
|
| 108 | + if (empty($this->markId)) { |
|
| 109 | + // generer un marqueur qui n'existe pas dans le texte |
|
| 110 | + do { |
|
| 111 | + $this->markId = substr(md5(uniqid(static::class, 1)), 0, 7); |
|
| 112 | + $this->markId = '@|' . static::$markPrefix . $this->markId . '|'; |
|
| 113 | + } while (str_contains($texte, $this->markId)); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + $offset_pos = 0; |
|
| 117 | + foreach ($collection as $c) { |
|
| 118 | + $rempl = $this->markId . base64_encode((string) $c['raw']) . '|@'; |
|
| 119 | + $texte = substr_replace($texte, $rempl, $c['pos'] + $offset_pos, $c['length']); |
|
| 120 | + $offset_pos += strlen($rempl) - $c['length']; |
|
| 121 | + } |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + return $texte; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Retablir les occurences échappées précédemment |
|
| 130 | + * |
|
| 131 | + * @see echapper() |
|
| 132 | + */ |
|
| 133 | + function retablir(string $texte): string { |
|
| 134 | + |
|
| 135 | + if (!empty($this->markId)) { |
|
| 136 | + $lm = strlen($this->markId); |
|
| 137 | + $pos = 0; |
|
| 138 | + while ( |
|
| 139 | + ($p = strpos($texte, $this->markId, $pos)) !== false |
|
| 140 | + && ($end = strpos($texte, '|@', $p + $lm)) |
|
| 141 | + ) { |
|
| 142 | + $base64 = substr($texte, $p + $lm, $end - ($p + $lm)); |
|
| 143 | + if ($c = base64_decode($base64, true)) { |
|
| 144 | + $texte = substr_replace($texte, $c, $p, $end + 2 - $p); |
|
| 145 | + $pos = $p + strlen($c); |
|
| 146 | + } |
|
| 147 | + else { |
|
| 148 | + $pos = $end; |
|
| 149 | + } |
|
| 150 | + } |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + return $texte; |
|
| 154 | + } |
|
| 155 | 155 | } |
@@ -109,13 +109,13 @@ |
||
| 109 | 109 | // generer un marqueur qui n'existe pas dans le texte |
| 110 | 110 | do { |
| 111 | 111 | $this->markId = substr(md5(uniqid(static::class, 1)), 0, 7); |
| 112 | - $this->markId = '@|' . static::$markPrefix . $this->markId . '|'; |
|
| 112 | + $this->markId = '@|'.static::$markPrefix.$this->markId.'|'; |
|
| 113 | 113 | } while (str_contains($texte, $this->markId)); |
| 114 | 114 | } |
| 115 | 115 | |
| 116 | 116 | $offset_pos = 0; |
| 117 | 117 | foreach ($collection as $c) { |
| 118 | - $rempl = $this->markId . base64_encode((string) $c['raw']) . '|@'; |
|
| 118 | + $rempl = $this->markId.base64_encode((string) $c['raw']).'|@'; |
|
| 119 | 119 | $texte = substr_replace($texte, $rempl, $c['pos'] + $offset_pos, $c['length']); |
| 120 | 120 | $offset_pos += strlen($rempl) - $c['length']; |
| 121 | 121 | } |
@@ -20,203 +20,203 @@ |
||
| 20 | 20 | * mais on renvoie les params (pour l'indexation par le moteur de recherche) |
| 21 | 21 | */ |
| 22 | 22 | class Modeles extends AbstractCollecteur { |
| 23 | - protected static string $markPrefix = 'MODELE'; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * La preg pour découper et collecter les modèles |
|
| 27 | - * @var string |
|
| 28 | - */ |
|
| 29 | - protected string $preg_modele; |
|
| 30 | - |
|
| 31 | - public function __construct(?string $preg = null) { |
|
| 32 | - |
|
| 33 | - $this->preg_modele = ($preg ?: |
|
| 34 | - '@<([a-z_-]{3,})' # <modele |
|
| 35 | - . '\s*([0-9]*)\s*' # id |
|
| 36 | - . '([|](?:<[^<>]*>|[^>])*?)?' # |arguments (y compris des tags <...>) |
|
| 37 | - . '\s*/?' . '>@isS' # fin du modele > |
|
| 38 | - ); |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * Sanitizer une collection d'occurences de modèle : on ne fait rien |
|
| 43 | - * |
|
| 44 | - * @param array $collection |
|
| 45 | - * @param string $sanitize_callback |
|
| 46 | - * @return array |
|
| 47 | - */ |
|
| 48 | - protected function sanitizer_collection(array $collection, string $sanitize_callback): array { |
|
| 49 | - |
|
| 50 | - return $collection; |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * @param string $texte |
|
| 55 | - * @param array $options |
|
| 56 | - * bool $collecter_liens |
|
| 57 | - * @return array |
|
| 58 | - */ |
|
| 59 | - public function collecter(string $texte, array $options = []): array { |
|
| 60 | - if (!$texte) { |
|
| 61 | - return []; |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - // collecter les matchs de la preg |
|
| 65 | - $modeles = static::collecteur($texte, '', '<', $this->preg_modele); |
|
| 66 | - |
|
| 67 | - $pos_prev = 0; |
|
| 68 | - foreach ($modeles as $k => &$modele) { |
|
| 69 | - $pos = $modele['pos']; |
|
| 70 | - $modele['type'] = $modele['match'][1]; |
|
| 71 | - $modele['id'] = $modele['match'][2] ?? ''; |
|
| 72 | - $modele['params'] = $modele['match'][3] ?? ''; |
|
| 73 | - |
|
| 74 | - $longueur = $modele['length']; |
|
| 75 | - $end = $pos + $longueur; |
|
| 76 | - |
|
| 77 | - // il faut avoir un id ou des params commençant par un | sinon c'est une simple balise html |
|
| 78 | - if (empty($modele['id']) && empty($modele['params'])) { |
|
| 79 | - unset($modeles[$k]); |
|
| 80 | - continue; |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - // si on veut seulement detecter la présence, on peut retourner tel quel |
|
| 84 | - if (!empty($options['detecter_presence'])) { |
|
| 85 | - break; |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - $modele['lien'] = false; |
|
| 89 | - if ( |
|
| 90 | - !empty($options['collecter_liens']) |
|
| 91 | - && ($pos_fermeture_lien = stripos($texte, '</a>', $end)) |
|
| 92 | - && !strlen(trim(substr($texte, $end, $pos_fermeture_lien - $end))) |
|
| 93 | - ) { |
|
| 94 | - $pos_lien_ouvrant = stripos($texte, '<a', $pos_prev); |
|
| 95 | - if ( |
|
| 96 | - $pos_lien_ouvrant !== false |
|
| 97 | - && $pos_lien_ouvrant < $pos |
|
| 98 | - && preg_match('/<a\s[^<>]*>\s*$/i', substr($texte, $pos_prev, $pos - $pos_prev), $r) |
|
| 99 | - ) { |
|
| 100 | - $modele['lien'] = [ |
|
| 101 | - 'href' => extraire_attribut($r[0], 'href'), |
|
| 102 | - 'class' => extraire_attribut($r[0], 'class'), |
|
| 103 | - 'mime' => extraire_attribut($r[0], 'type'), |
|
| 104 | - 'title' => extraire_attribut($r[0], 'title'), |
|
| 105 | - 'hreflang' => extraire_attribut($r[0], 'hreflang') |
|
| 106 | - ]; |
|
| 107 | - $n = strlen($r[0]); |
|
| 108 | - $pos -= $n; |
|
| 109 | - $longueur = $pos_fermeture_lien - $pos + 4; |
|
| 110 | - $end = $pos + $longueur; |
|
| 111 | - } |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - |
|
| 115 | - $modele['pos'] = $pos; |
|
| 116 | - $modele['length'] = $longueur; |
|
| 117 | - $pos_prev = $end; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - return $modeles; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Traiter les modeles d'un texte |
|
| 125 | - * @param string $texte |
|
| 126 | - * @param array $options |
|
| 127 | - * bool|array $doublons |
|
| 128 | - * string $echap |
|
| 129 | - * ?Spip\Texte\CollecteurLiens $collecteurLiens |
|
| 130 | - * ?array $env |
|
| 131 | - * ?string $connect |
|
| 132 | - * @return string |
|
| 133 | - */ |
|
| 134 | - public function traiter(string $texte, array $options) { |
|
| 135 | - if ($texte) { |
|
| 136 | - $doublons = $options['doublons'] ?? false; |
|
| 137 | - $echap = $options['echap'] ?? ''; |
|
| 138 | - $collecteurLiens = $options['collecteurLiens'] ?? null; |
|
| 139 | - $env = $options['env'] ?? []; |
|
| 140 | - $connect = $options['connect'] ?? ''; |
|
| 141 | - |
|
| 142 | - // preserver la compatibilite : true = recherche des documents |
|
| 143 | - if ($doublons === true) { |
|
| 144 | - $doublons = ['documents' => ['doc', 'emb', 'img']]; |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - $modeles = $this->collecter($texte, ['collecter_liens' => true]); |
|
| 148 | - if ($modeles !== []) { |
|
| 149 | - include_spip('public/assembler'); |
|
| 150 | - $wrap_embed_html = charger_fonction('wrap_embed_html', 'inc', true); |
|
| 151 | - |
|
| 152 | - $offset_pos = 0; |
|
| 153 | - foreach ($modeles as $m) { |
|
| 154 | - // calculer le modele |
|
| 155 | - # hack indexation |
|
| 156 | - if ($doublons) { |
|
| 157 | - $texte .= preg_replace(',[|][^|=]*,s', ' ', (string) $m['params']); |
|
| 158 | - } # version normale |
|
| 159 | - else { |
|
| 160 | - // si un tableau de liens a ete passe, reinjecter le contenu d'origine |
|
| 161 | - // dans les parametres, plutot que les liens echappes |
|
| 162 | - $params = $m['params']; |
|
| 163 | - if (!is_null($collecteurLiens)) { |
|
| 164 | - $params = $collecteurLiens->retablir($params); |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - $modele = inclure_modele($m['type'], $m['id'], $params, $m['lien'], $connect ?? '', $env); |
|
| 168 | - |
|
| 169 | - // en cas d'echec, |
|
| 170 | - // si l'objet demande a une url, |
|
| 171 | - // creer un petit encadre vers elle |
|
| 172 | - if ($modele === false) { |
|
| 173 | - $modele = $m['raw']; |
|
| 174 | - |
|
| 175 | - if (!is_null($collecteurLiens)) { |
|
| 176 | - $modele = $collecteurLiens->retablir($modele); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - $contexte = array_merge($env, ['id' => $m['id'], 'type' => $m['type'], 'modele' => $modele]); |
|
| 180 | - |
|
| 181 | - if (!empty($m['lien'])) { |
|
| 182 | - # un eventuel guillemet (") sera reechappe par #ENV |
|
| 183 | - $contexte['lien'] = str_replace('"', '"', (string) $m['lien']['href']); |
|
| 184 | - $contexte['lien_class'] = $m['lien']['class']; |
|
| 185 | - $contexte['lien_mime'] = $m['lien']['mime']; |
|
| 186 | - $contexte['lien_title'] = $m['lien']['title']; |
|
| 187 | - $contexte['lien_hreflang'] = $m['lien']['hreflang']; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - $modele = recuperer_fond('modeles/dist', $contexte, [], $connect ?? ''); |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - // le remplacer dans le texte |
|
| 194 | - if ($modele !== false) { |
|
| 195 | - $modele = protege_js_modeles($modele); |
|
| 196 | - |
|
| 197 | - if ($wrap_embed_html) { |
|
| 198 | - $modele = $wrap_embed_html($m['raw'], $modele); |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - $rempl = code_echappement($modele, $echap); |
|
| 202 | - $texte = substr_replace($texte, (string) $rempl, $m['pos'] + $offset_pos, $m['length']); |
|
| 203 | - $offset_pos += strlen((string) $rempl) - $m['length']; |
|
| 204 | - } |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - // hack pour tout l'espace prive |
|
| 208 | - if ((test_espace_prive() || $doublons) && !empty($m['id'])) { |
|
| 209 | - $type = strtolower((string) $m['type']); |
|
| 210 | - foreach ($doublons ?: ['documents' => ['doc', 'emb', 'img']] as $quoi => $type_modeles) { |
|
| 211 | - if (in_array($type, $type_modeles)) { |
|
| 212 | - $GLOBALS["doublons_{$quoi}_inclus"][] = $m['id']; |
|
| 213 | - } |
|
| 214 | - } |
|
| 215 | - } |
|
| 216 | - } |
|
| 217 | - } |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - return $texte; |
|
| 221 | - } |
|
| 23 | + protected static string $markPrefix = 'MODELE'; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * La preg pour découper et collecter les modèles |
|
| 27 | + * @var string |
|
| 28 | + */ |
|
| 29 | + protected string $preg_modele; |
|
| 30 | + |
|
| 31 | + public function __construct(?string $preg = null) { |
|
| 32 | + |
|
| 33 | + $this->preg_modele = ($preg ?: |
|
| 34 | + '@<([a-z_-]{3,})' # <modele |
|
| 35 | + . '\s*([0-9]*)\s*' # id |
|
| 36 | + . '([|](?:<[^<>]*>|[^>])*?)?' # |arguments (y compris des tags <...>) |
|
| 37 | + . '\s*/?' . '>@isS' # fin du modele > |
|
| 38 | + ); |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * Sanitizer une collection d'occurences de modèle : on ne fait rien |
|
| 43 | + * |
|
| 44 | + * @param array $collection |
|
| 45 | + * @param string $sanitize_callback |
|
| 46 | + * @return array |
|
| 47 | + */ |
|
| 48 | + protected function sanitizer_collection(array $collection, string $sanitize_callback): array { |
|
| 49 | + |
|
| 50 | + return $collection; |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * @param string $texte |
|
| 55 | + * @param array $options |
|
| 56 | + * bool $collecter_liens |
|
| 57 | + * @return array |
|
| 58 | + */ |
|
| 59 | + public function collecter(string $texte, array $options = []): array { |
|
| 60 | + if (!$texte) { |
|
| 61 | + return []; |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + // collecter les matchs de la preg |
|
| 65 | + $modeles = static::collecteur($texte, '', '<', $this->preg_modele); |
|
| 66 | + |
|
| 67 | + $pos_prev = 0; |
|
| 68 | + foreach ($modeles as $k => &$modele) { |
|
| 69 | + $pos = $modele['pos']; |
|
| 70 | + $modele['type'] = $modele['match'][1]; |
|
| 71 | + $modele['id'] = $modele['match'][2] ?? ''; |
|
| 72 | + $modele['params'] = $modele['match'][3] ?? ''; |
|
| 73 | + |
|
| 74 | + $longueur = $modele['length']; |
|
| 75 | + $end = $pos + $longueur; |
|
| 76 | + |
|
| 77 | + // il faut avoir un id ou des params commençant par un | sinon c'est une simple balise html |
|
| 78 | + if (empty($modele['id']) && empty($modele['params'])) { |
|
| 79 | + unset($modeles[$k]); |
|
| 80 | + continue; |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + // si on veut seulement detecter la présence, on peut retourner tel quel |
|
| 84 | + if (!empty($options['detecter_presence'])) { |
|
| 85 | + break; |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + $modele['lien'] = false; |
|
| 89 | + if ( |
|
| 90 | + !empty($options['collecter_liens']) |
|
| 91 | + && ($pos_fermeture_lien = stripos($texte, '</a>', $end)) |
|
| 92 | + && !strlen(trim(substr($texte, $end, $pos_fermeture_lien - $end))) |
|
| 93 | + ) { |
|
| 94 | + $pos_lien_ouvrant = stripos($texte, '<a', $pos_prev); |
|
| 95 | + if ( |
|
| 96 | + $pos_lien_ouvrant !== false |
|
| 97 | + && $pos_lien_ouvrant < $pos |
|
| 98 | + && preg_match('/<a\s[^<>]*>\s*$/i', substr($texte, $pos_prev, $pos - $pos_prev), $r) |
|
| 99 | + ) { |
|
| 100 | + $modele['lien'] = [ |
|
| 101 | + 'href' => extraire_attribut($r[0], 'href'), |
|
| 102 | + 'class' => extraire_attribut($r[0], 'class'), |
|
| 103 | + 'mime' => extraire_attribut($r[0], 'type'), |
|
| 104 | + 'title' => extraire_attribut($r[0], 'title'), |
|
| 105 | + 'hreflang' => extraire_attribut($r[0], 'hreflang') |
|
| 106 | + ]; |
|
| 107 | + $n = strlen($r[0]); |
|
| 108 | + $pos -= $n; |
|
| 109 | + $longueur = $pos_fermeture_lien - $pos + 4; |
|
| 110 | + $end = $pos + $longueur; |
|
| 111 | + } |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + |
|
| 115 | + $modele['pos'] = $pos; |
|
| 116 | + $modele['length'] = $longueur; |
|
| 117 | + $pos_prev = $end; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + return $modeles; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Traiter les modeles d'un texte |
|
| 125 | + * @param string $texte |
|
| 126 | + * @param array $options |
|
| 127 | + * bool|array $doublons |
|
| 128 | + * string $echap |
|
| 129 | + * ?Spip\Texte\CollecteurLiens $collecteurLiens |
|
| 130 | + * ?array $env |
|
| 131 | + * ?string $connect |
|
| 132 | + * @return string |
|
| 133 | + */ |
|
| 134 | + public function traiter(string $texte, array $options) { |
|
| 135 | + if ($texte) { |
|
| 136 | + $doublons = $options['doublons'] ?? false; |
|
| 137 | + $echap = $options['echap'] ?? ''; |
|
| 138 | + $collecteurLiens = $options['collecteurLiens'] ?? null; |
|
| 139 | + $env = $options['env'] ?? []; |
|
| 140 | + $connect = $options['connect'] ?? ''; |
|
| 141 | + |
|
| 142 | + // preserver la compatibilite : true = recherche des documents |
|
| 143 | + if ($doublons === true) { |
|
| 144 | + $doublons = ['documents' => ['doc', 'emb', 'img']]; |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + $modeles = $this->collecter($texte, ['collecter_liens' => true]); |
|
| 148 | + if ($modeles !== []) { |
|
| 149 | + include_spip('public/assembler'); |
|
| 150 | + $wrap_embed_html = charger_fonction('wrap_embed_html', 'inc', true); |
|
| 151 | + |
|
| 152 | + $offset_pos = 0; |
|
| 153 | + foreach ($modeles as $m) { |
|
| 154 | + // calculer le modele |
|
| 155 | + # hack indexation |
|
| 156 | + if ($doublons) { |
|
| 157 | + $texte .= preg_replace(',[|][^|=]*,s', ' ', (string) $m['params']); |
|
| 158 | + } # version normale |
|
| 159 | + else { |
|
| 160 | + // si un tableau de liens a ete passe, reinjecter le contenu d'origine |
|
| 161 | + // dans les parametres, plutot que les liens echappes |
|
| 162 | + $params = $m['params']; |
|
| 163 | + if (!is_null($collecteurLiens)) { |
|
| 164 | + $params = $collecteurLiens->retablir($params); |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + $modele = inclure_modele($m['type'], $m['id'], $params, $m['lien'], $connect ?? '', $env); |
|
| 168 | + |
|
| 169 | + // en cas d'echec, |
|
| 170 | + // si l'objet demande a une url, |
|
| 171 | + // creer un petit encadre vers elle |
|
| 172 | + if ($modele === false) { |
|
| 173 | + $modele = $m['raw']; |
|
| 174 | + |
|
| 175 | + if (!is_null($collecteurLiens)) { |
|
| 176 | + $modele = $collecteurLiens->retablir($modele); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + $contexte = array_merge($env, ['id' => $m['id'], 'type' => $m['type'], 'modele' => $modele]); |
|
| 180 | + |
|
| 181 | + if (!empty($m['lien'])) { |
|
| 182 | + # un eventuel guillemet (") sera reechappe par #ENV |
|
| 183 | + $contexte['lien'] = str_replace('"', '"', (string) $m['lien']['href']); |
|
| 184 | + $contexte['lien_class'] = $m['lien']['class']; |
|
| 185 | + $contexte['lien_mime'] = $m['lien']['mime']; |
|
| 186 | + $contexte['lien_title'] = $m['lien']['title']; |
|
| 187 | + $contexte['lien_hreflang'] = $m['lien']['hreflang']; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + $modele = recuperer_fond('modeles/dist', $contexte, [], $connect ?? ''); |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + // le remplacer dans le texte |
|
| 194 | + if ($modele !== false) { |
|
| 195 | + $modele = protege_js_modeles($modele); |
|
| 196 | + |
|
| 197 | + if ($wrap_embed_html) { |
|
| 198 | + $modele = $wrap_embed_html($m['raw'], $modele); |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + $rempl = code_echappement($modele, $echap); |
|
| 202 | + $texte = substr_replace($texte, (string) $rempl, $m['pos'] + $offset_pos, $m['length']); |
|
| 203 | + $offset_pos += strlen((string) $rempl) - $m['length']; |
|
| 204 | + } |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + // hack pour tout l'espace prive |
|
| 208 | + if ((test_espace_prive() || $doublons) && !empty($m['id'])) { |
|
| 209 | + $type = strtolower((string) $m['type']); |
|
| 210 | + foreach ($doublons ?: ['documents' => ['doc', 'emb', 'img']] as $quoi => $type_modeles) { |
|
| 211 | + if (in_array($type, $type_modeles)) { |
|
| 212 | + $GLOBALS["doublons_{$quoi}_inclus"][] = $m['id']; |
|
| 213 | + } |
|
| 214 | + } |
|
| 215 | + } |
|
| 216 | + } |
|
| 217 | + } |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + return $texte; |
|
| 221 | + } |
|
| 222 | 222 | } |
@@ -15,98 +15,98 @@ |
||
| 15 | 15 | * Collecte les raccourcis liens [titre->url] de SPIP |
| 16 | 16 | */ |
| 17 | 17 | class Liens extends AbstractCollecteur { |
| 18 | - protected static string $markPrefix = 'LIEN'; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * La preg pour découper et collecter les modèles |
|
| 22 | - * @var string |
|
| 23 | - */ |
|
| 24 | - protected string $preg_lien; |
|
| 25 | - |
|
| 26 | - public function __construct(?string $preg = null) { |
|
| 27 | - |
|
| 28 | - // Regexp des raccourcis, aussi utilisee pour la fusion de sauvegarde Spip |
|
| 29 | - // Laisser passer des paires de crochets pour la balise multi |
|
| 30 | - // mais refuser plus d'imbrications ou de mauvaises imbrications |
|
| 31 | - // sinon les crochets ne peuvent plus servir qu'a ce type de raccourci |
|
| 32 | - $this->preg_lien = ($preg ?: '/\[([^][]*?([[][^]>-]*[]][^][]*)*)->(>?)([^]]*)\]/msS'); |
|
| 33 | - } |
|
| 34 | - |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * Sanitizer une collection d'occurences de liens : il faut sanitizer le href et le texte uniquement |
|
| 38 | - * |
|
| 39 | - * @param array $collection |
|
| 40 | - * @param string $sanitize_callback |
|
| 41 | - * @return array |
|
| 42 | - */ |
|
| 43 | - protected function sanitizer_collection(array $collection, string $sanitize_callback): array { |
|
| 44 | - foreach ($collection as &$lien) { |
|
| 45 | - $t = $sanitize_callback($lien['texte']); |
|
| 46 | - if ($t !== $lien['texte']) { |
|
| 47 | - $lien['raw'] = str_replace($lien['texte'], $t, (string) $lien['raw']); |
|
| 48 | - $lien['texte'] = $t; |
|
| 49 | - } |
|
| 50 | - $href = $sanitize_callback($lien['href']); |
|
| 51 | - if ($href !== $lien['href']) { |
|
| 52 | - $lien['raw'] = str_replace($lien['href'], $href, (string) $lien['raw']); |
|
| 53 | - $lien['href'] = $href; |
|
| 54 | - } |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - return $collection; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * @param string $texte |
|
| 62 | - * @param array $options |
|
| 63 | - * bool $collecter_liens |
|
| 64 | - * @return array |
|
| 65 | - */ |
|
| 66 | - public function collecter(string $texte, array $options = []): array { |
|
| 67 | - if (!$texte) { |
|
| 68 | - return []; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - $liens = []; |
|
| 72 | - if (str_contains($texte, '->')) { |
|
| 73 | - $desechappe_crochets = false; |
|
| 74 | - // si il y a un crochet ouvrant échappé ou un crochet fermant échappé, les substituer pour les ignorer |
|
| 75 | - if (str_contains($texte, '\[') || str_contains($texte, '\]')) { |
|
| 76 | - $texte = str_replace(['\[', '\]'], ["\x1\x5", "\x1\x6"], $texte); |
|
| 77 | - $desechappe_crochets = true; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - // collecter les matchs de la preg |
|
| 81 | - $liens = static::collecteur($texte, '->', '[', $this->preg_lien, empty($options['detecter_presence']) ? 0 : 1); |
|
| 82 | - |
|
| 83 | - // si on veut seulement detecter la présence, on peut retourner tel quel |
|
| 84 | - if (empty($options['detecter_presence'])) { |
|
| 85 | - foreach ($liens as $k => &$lien) { |
|
| 86 | - $lien['href'] = end($lien['match']); |
|
| 87 | - $lien['texte'] = $lien['match'][1]; |
|
| 88 | - $lien['ouvrant'] = $lien['match'][3] ?? ''; |
|
| 89 | - |
|
| 90 | - // la mise en lien automatique est passee par la a tort ! |
|
| 91 | - // corrigeons pour eviter d'avoir un <a...> dans un href... |
|
| 92 | - if (str_starts_with((string) $lien['href'], '<a')) { |
|
| 93 | - $href = extraire_attribut($lien['href'], 'href'); |
|
| 94 | - // remplacons dans la source qui peut etre reinjectee dans les arguments |
|
| 95 | - // d'un modele |
|
| 96 | - $lien['raw'] = str_replace($lien['href'], $href, (string) $lien['raw']); |
|
| 97 | - // et prenons le href comme la vraie url a linker |
|
| 98 | - $lien['href'] = $href; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - if ($desechappe_crochets && str_contains((string) $lien['raw'], "\x1")) { |
|
| 102 | - $lien['raw'] = str_replace(["\x1\x5", "\x1\x6"], ['[', ']'], (string) $lien['raw']); |
|
| 103 | - $lien['texte'] = str_replace(["\x1\x5", "\x1\x6"], ['[', ']'], (string) $lien['texte']); |
|
| 104 | - $lien['href'] = str_replace(["\x1\x5", "\x1\x6"], ['[', ']'], (string) $lien['href']); |
|
| 105 | - } |
|
| 106 | - } |
|
| 107 | - } |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - return $liens; |
|
| 111 | - } |
|
| 18 | + protected static string $markPrefix = 'LIEN'; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * La preg pour découper et collecter les modèles |
|
| 22 | + * @var string |
|
| 23 | + */ |
|
| 24 | + protected string $preg_lien; |
|
| 25 | + |
|
| 26 | + public function __construct(?string $preg = null) { |
|
| 27 | + |
|
| 28 | + // Regexp des raccourcis, aussi utilisee pour la fusion de sauvegarde Spip |
|
| 29 | + // Laisser passer des paires de crochets pour la balise multi |
|
| 30 | + // mais refuser plus d'imbrications ou de mauvaises imbrications |
|
| 31 | + // sinon les crochets ne peuvent plus servir qu'a ce type de raccourci |
|
| 32 | + $this->preg_lien = ($preg ?: '/\[([^][]*?([[][^]>-]*[]][^][]*)*)->(>?)([^]]*)\]/msS'); |
|
| 33 | + } |
|
| 34 | + |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * Sanitizer une collection d'occurences de liens : il faut sanitizer le href et le texte uniquement |
|
| 38 | + * |
|
| 39 | + * @param array $collection |
|
| 40 | + * @param string $sanitize_callback |
|
| 41 | + * @return array |
|
| 42 | + */ |
|
| 43 | + protected function sanitizer_collection(array $collection, string $sanitize_callback): array { |
|
| 44 | + foreach ($collection as &$lien) { |
|
| 45 | + $t = $sanitize_callback($lien['texte']); |
|
| 46 | + if ($t !== $lien['texte']) { |
|
| 47 | + $lien['raw'] = str_replace($lien['texte'], $t, (string) $lien['raw']); |
|
| 48 | + $lien['texte'] = $t; |
|
| 49 | + } |
|
| 50 | + $href = $sanitize_callback($lien['href']); |
|
| 51 | + if ($href !== $lien['href']) { |
|
| 52 | + $lien['raw'] = str_replace($lien['href'], $href, (string) $lien['raw']); |
|
| 53 | + $lien['href'] = $href; |
|
| 54 | + } |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + return $collection; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * @param string $texte |
|
| 62 | + * @param array $options |
|
| 63 | + * bool $collecter_liens |
|
| 64 | + * @return array |
|
| 65 | + */ |
|
| 66 | + public function collecter(string $texte, array $options = []): array { |
|
| 67 | + if (!$texte) { |
|
| 68 | + return []; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + $liens = []; |
|
| 72 | + if (str_contains($texte, '->')) { |
|
| 73 | + $desechappe_crochets = false; |
|
| 74 | + // si il y a un crochet ouvrant échappé ou un crochet fermant échappé, les substituer pour les ignorer |
|
| 75 | + if (str_contains($texte, '\[') || str_contains($texte, '\]')) { |
|
| 76 | + $texte = str_replace(['\[', '\]'], ["\x1\x5", "\x1\x6"], $texte); |
|
| 77 | + $desechappe_crochets = true; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + // collecter les matchs de la preg |
|
| 81 | + $liens = static::collecteur($texte, '->', '[', $this->preg_lien, empty($options['detecter_presence']) ? 0 : 1); |
|
| 82 | + |
|
| 83 | + // si on veut seulement detecter la présence, on peut retourner tel quel |
|
| 84 | + if (empty($options['detecter_presence'])) { |
|
| 85 | + foreach ($liens as $k => &$lien) { |
|
| 86 | + $lien['href'] = end($lien['match']); |
|
| 87 | + $lien['texte'] = $lien['match'][1]; |
|
| 88 | + $lien['ouvrant'] = $lien['match'][3] ?? ''; |
|
| 89 | + |
|
| 90 | + // la mise en lien automatique est passee par la a tort ! |
|
| 91 | + // corrigeons pour eviter d'avoir un <a...> dans un href... |
|
| 92 | + if (str_starts_with((string) $lien['href'], '<a')) { |
|
| 93 | + $href = extraire_attribut($lien['href'], 'href'); |
|
| 94 | + // remplacons dans la source qui peut etre reinjectee dans les arguments |
|
| 95 | + // d'un modele |
|
| 96 | + $lien['raw'] = str_replace($lien['href'], $href, (string) $lien['raw']); |
|
| 97 | + // et prenons le href comme la vraie url a linker |
|
| 98 | + $lien['href'] = $href; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + if ($desechappe_crochets && str_contains((string) $lien['raw'], "\x1")) { |
|
| 102 | + $lien['raw'] = str_replace(["\x1\x5", "\x1\x6"], ['[', ']'], (string) $lien['raw']); |
|
| 103 | + $lien['texte'] = str_replace(["\x1\x5", "\x1\x6"], ['[', ']'], (string) $lien['texte']); |
|
| 104 | + $lien['href'] = str_replace(["\x1\x5", "\x1\x6"], ['[', ']'], (string) $lien['href']); |
|
| 105 | + } |
|
| 106 | + } |
|
| 107 | + } |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + return $liens; |
|
| 111 | + } |
|
| 112 | 112 | } |