Completed
Push — master ( d8d4cc...6b056b )
by cam
07:55
created

auth.php ➔ auth_loger()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 14
nc 6
nop 1
dl 0
loc 25
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
/***************************************************************************\
4
 *  SPIP, Systeme de publication pour l'internet                           *
5
 *                                                                         *
6
 *  Copyright (c) 2001-2017                                                *
7
 *  Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James  *
8
 *                                                                         *
9
 *  Ce programme est un logiciel libre distribue sous licence GNU/GPL.     *
10
 *  Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne.   *
11
\***************************************************************************/
12
13
/**
14
 * Gestion des authentifications
15
 *
16
 * @package SPIP\Core\Authentification
17
 **/
18
19
if (!defined('_ECRIRE_INC_VERSION')) {
20
	return;
21
}
22
23
include_spip('base/abstract_sql');
24
25
/**
26
 * Teste l'authentification d'un visiteur
27
 *
28
 * Cette fonction ne fait pas l'authentification en soit ;
29
 * elle vérifie simplement qu'une personne est connectée ou non.
30
 *
31
 * @return array|int|string
32
 *  - URL de connexion si on ne sait rien (pas de cookie, pas Auth_user);
33
 *  - un tableau si visiteur sans droit (tableau = sa ligne SQL)
34
 *  - code numerique d'erreur SQL
35
 *  - une chaîne vide si autorisation à pénétrer dans l'espace privé.
36
 */
37
function inc_auth_dist() {
38
	$row = auth_mode();
39
40
	if ($row) {
41
		return auth_init_droits($row);
0 ignored issues
show
Bug introduced by
It seems like $row defined by auth_mode() on line 38 can also be of type string; however, auth_init_droits() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
42
	}
43
44
	if (!$GLOBALS['connect_login']) {
45
		return auth_a_loger();
46
	}
47
48
	// Cas ou l'auteur a ete identifie mais on n'a pas d'info sur lui
49
	// C'est soit parce que la base est inutilisable,
50
	// soit parce que la table des auteurs a changee (restauration etc)
51
	// Pas la peine d'insister.
52
	// Renvoyer le nom fautif et une URL de remise a zero
53
54
	if (spip_connect()) {
55
		return array(
56
			'login' => $GLOBALS['connect_login'],
57
			'site' => generer_url_public('', 'action=logout&amp;logout=prive')
58
		);
59
	}
60
61
	$n = intval(sql_errno());
62
	spip_log("Erreur base de donnees $n " . sql_error());
63
64
	return $n ? $n : 1;
65
}
66
67
/**
68
 * fonction appliquee par ecrire/index sur le resultat de la precedente
69
 * en cas de refus de connexion.
70
 * Retourne un message a afficher ou redirige illico.
71
 *
72
 * @param  $raison
73
 * @return array|string
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string[]|string.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
74
 */
75
function auth_echec($raison) {
76
	include_spip('inc/minipres');
77
	include_spip('inc/headers');
78
	// pas authentifie. Pourquoi ?
79
	if (is_string($raison)) {
80
		// redirection vers une page d'authentification
81
		// on ne revient pas de cette fonction
82
		// sauf si pb de header
83
		$raison = redirige_formulaire($raison);
84
	} elseif (is_int($raison)) {
85
		// erreur SQL a afficher
86
		$raison = minipres(
87
			_T('info_travaux_titre'),
88
			_T('titre_probleme_technique') . '<p><tt>' . sql_errno() . ' ' . sql_error() . '</tt></p>'
89
		);
90
	} elseif (@$raison['statut']) {
91
		// un simple visiteur n'a pas acces a l'espace prive
92
		spip_log('connexion refusee a ' . @$raison['id_auteur']);
93
		$raison = minipres(_T('avis_erreur_connexion'), _T('avis_erreur_visiteur'));
94
	} else {
95
		// auteur en fin de droits ...
96
		$h = $raison['site'];
97
		$raison = minipres(
98
			_T('avis_erreur_connexion'),
99
			'<br /><br /><p>'
100
			. _T('texte_inc_auth_1', array('auth_login' => $raison['login']))
101
			. " <a href='$h'>"
102
			. _T('texte_inc_auth_2')
103
			. '</a>'
104
			. _T('texte_inc_auth_3')
105
		);
106
	}
107
108
	return $raison;
109
}
110
111
/**
112
 * Retourne la description d'un authentifie par cookie ou http_auth
113
 * Et affecte la globale $connect_login
114
 *
115
 * @return array|bool|string
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string|array.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
116
 */
117
function auth_mode() {
118
	//
119
	// Initialiser variables (eviter hacks par URL)
120
	//
121
	$GLOBALS['connect_login'] = '';
122
	$id_auteur = null;
123
	$GLOBALS['auth_can_disconnect'] = false;
124
125
	//
126
	// Recuperer les donnees d'identification
127
	//
128
	include_spip('inc/session');
129
	// Session valide en cours ?
130
	if (isset($_COOKIE['spip_session'])) {
131
		$session = charger_fonction('session', 'inc');
132
		if ($id_auteur = $session()
133
			or $id_auteur === 0 // reprise sur restauration
134
		) {
135
			$GLOBALS['auth_can_disconnect'] = true;
136
			$GLOBALS['connect_login'] = session_get('login');
137
		} else {
138
			unset($_COOKIE['spip_session']);
139
		}
140
	}
141
142
	// Essayer auth http si significatif
143
	// (ignorer les login d'intranet independants de spip)
144
	if (!$GLOBALS['ignore_auth_http']) {
145
		if (
146
			(isset($_SERVER['PHP_AUTH_USER']) and isset($_SERVER['PHP_AUTH_PW'])
147
				and $r = lire_php_auth($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']))
148
			or
149
			// Si auth http differtente de basic, PHP_AUTH_PW
150
			// est indisponible mais tentons quand meme pour
151
			// autocreation via LDAP
152
			(isset($_SERVER['REMOTE_USER'])
153
				and $r = lire_php_auth($_SERVER['PHP_AUTH_USER'] = $_SERVER['REMOTE_USER'], ''))
154
		) {
155
			if (!$id_auteur) {
156
				$_SERVER['PHP_AUTH_PW'] = '';
157
				$GLOBALS['auth_can_disconnect'] = true;
158
				$GLOBALS['visiteur_session'] = $r;
159
				$GLOBALS['connect_login'] = session_get('login');
160
				$id_auteur = $r['id_auteur'];
161
			} else {
162
				// cas de la session en plus de PHP_AUTH
163
				/*				  if ($id_auteur != $r['id_auteur']){
164
					spip_log("vol de session $id_auteur" . join(', ', $r));
165
				unset($_COOKIE['spip_session']);
166
				$id_auteur = '';
167
				} */
168
			}
169
		} else {
170
			// Authentification .htaccess old style, car .htaccess semble
171
			// souvent definir *aussi* PHP_AUTH_USER et PHP_AUTH_PW
172
			if (isset($_SERVER['REMOTE_USER'])) {
173
				$GLOBALS['connect_login'] = $_SERVER['REMOTE_USER'];
174
			}
175
		}
176
	}
177
178
	$where = (is_numeric($id_auteur)
179
		/*AND $id_auteur>0*/ // reprise lors des restaurations
180
	) ?
181
		"id_auteur=$id_auteur" :
182
		(!strlen($GLOBALS['connect_login']) ? '' : 'login=' . sql_quote($GLOBALS['connect_login'], '', 'text'));
183
184
	if (!$where) {
185
		return '';
186
	}
187
188
	// Trouver les autres infos dans la table auteurs.
189
	// le champ 'quand' est utilise par l'agenda
190
191
	return sql_fetsel('*, en_ligne AS quand', 'spip_auteurs', "$where AND statut!='5poubelle'");
192
}
193
194
/**
195
 * Initialisation des globales pour tout l'espace privé si visiteur connu
196
 *
197
 * Le tableau global visiteur_session contient toutes les infos pertinentes et
198
 * à jour (tandis que `$visiteur_session` peut avoir des valeurs un peu datées
199
 * s'il est pris dans le fichier de session)
200
 *
201
 * Les plus utiles sont aussi dans les variables simples ci-dessus
202
 * si la globale est vide ce n'est pas un tableau, on la force pour empêcher un warning.
203
 *
204
 * @param array $row
205
 * @return array|string|bool
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use false|array|string.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
206
 */
207
function auth_init_droits($row) {
208
209
	include_spip('inc/autoriser');
210
	if (!autoriser('loger', '', 0, $row)) {
211
		return false;
212
	}
213
214
215
	if ($row['statut'] == 'nouveau') {
216
		include_spip('action/inscrire_auteur');
217
		$row = confirmer_statut_inscription($row);
218
	}
219
220
	$GLOBALS['connect_id_auteur'] = $row['id_auteur'];
221
	$GLOBALS['connect_login'] = $row['login'];
222
	$GLOBALS['connect_statut'] = $row['statut'];
223
224
	$GLOBALS['visiteur_session'] = array_merge((array)$GLOBALS['visiteur_session'], $row);
225
226
	// au cas ou : ne pas memoriser les champs sensibles
227
	unset($GLOBALS['visiteur_session']['pass']);
228
	unset($GLOBALS['visiteur_session']['htpass']);
229
	unset($GLOBALS['visiteur_session']['alea_actuel']);
230
	unset($GLOBALS['visiteur_session']['alea_futur']);
231
232
	// creer la session au besoin
233
	if (!isset($_COOKIE['spip_session'])) {
234
		$session = charger_fonction('session', 'inc');
235
		$spip_session = $session($row);
0 ignored issues
show
Unused Code introduced by
$spip_session is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
236
	}
237
238
	// reinjecter les preferences_auteur apres le reset de spip_session
239
	// car utilisees au retour par auth_loger()
240
	$r = @unserialize($row['prefs']);
241
	$GLOBALS['visiteur_session']['prefs'] = ($r ? $r : array());
242
	// si prefs pas definies, les definir par defaut
243
	if (!isset($GLOBALS['visiteur_session']['prefs']['couleur'])) {
244
		$GLOBALS['visiteur_session']['prefs']['couleur'] = 9;
245
		$GLOBALS['visiteur_session']['prefs']['display'] = 2;
246
		$GLOBALS['visiteur_session']['prefs']['display_navigation'] = 'navigation_avec_icones';
247
		$GLOBALS['visiteur_session']['prefs']['display_outils'] = 'oui';
248
	}
249
250
	$GLOBALS['visiteur_session'] = pipeline(
251
		'preparer_visiteur_session',
252
		array('args' => array('row' => $row),
253
		'data' => $GLOBALS['visiteur_session'])
254
	);
255
256
	// Etablir les droits selon le codage attendu
257
	// dans ecrire/index.php ecrire/prive.php
258
259
	// Pas autorise a acceder a ecrire ? renvoyer le tableau
260
	// A noter : le premier appel a autoriser() a le bon gout
261
	// d'initialiser $GLOBALS['visiteur_session']['restreint'],
262
	// qui ne figure pas dans le fichier de session
263
264
	if (!autoriser('ecrire')) {
265
		return $row;
266
	}
267
268
	// autoriser('ecrire') ne laisse passer que les Admin et les Redac
269
270
	auth_trace($row);
271
272
	// Administrateurs
273
	if (in_array($GLOBALS['connect_statut'], explode(',', _STATUT_AUTEUR_RUBRIQUE))) {
274
		if (is_array($GLOBALS['visiteur_session']['restreint'])) {
275
			$GLOBALS['connect_id_rubrique'] = $GLOBALS['visiteur_session']['restreint'];
276
		}
277
		if ($GLOBALS['connect_statut'] == '0minirezo') {
278
			$GLOBALS['connect_toutes_rubriques'] = !$GLOBALS['connect_id_rubrique'];
279
		}
280
	}
281
282
	// Pour les redacteurs, inc_version a fait l'initialisation minimale
283
284
	return ''; // i.e. pas de pb.
285
}
286
287
/**
288
 * Retourne l'url de connexion
289
 *
290
 * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be null|string|string[]?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
291
 */
292
function auth_a_loger() {
293
	$redirect = generer_url_public('login', 'url=' . rawurlencode(self('&', true)), '&');
294
295
	// un echec au "bonjour" (login initial) quand le statut est
296
	// inconnu signale sans doute un probleme de cookies
297
	if (isset($_GET['bonjour'])) {
298
		$redirect = parametre_url(
299
			$redirect,
300
			'var_erreur',
301
			(!isset($GLOBALS['visiteur_session']['statut'])
302
				? 'cookie'
303
				: 'statut'
304
			),
305
			'&'
306
		);
307
	}
308
309
	return $redirect;
310
}
311
312
/**
313
 * Tracer en base la date de dernière connexion de l'auteur
314
 *
315
 * @pipeline_appel trig_auth_trace
316
 *
317
 * @param array $row
318
 * @param null|string $date
319
 */
320
function auth_trace($row, $date = null) {
321
	// Indiquer la connexion. A la minute pres ca suffit.
322
	if (!is_numeric($connect_quand = isset($row['quand']) ? $row['quand'] : '')) {
323
		$connect_quand = strtotime($connect_quand);
324
	}
325
326
	if (is_null($date)) {
327
		$date = date('Y-m-d H:i:s');
328
	}
329
330
	if (abs(strtotime($date) - $connect_quand) >= 60) {
331
		sql_updateq('spip_auteurs', array('en_ligne' => $date), 'id_auteur=' . intval($row['id_auteur']));
332
		$row['en_ligne'] = $date;
333
	}
334
335
	pipeline('trig_auth_trace', array('args' => array('row' => $row, 'date' => $date)));
336
}
337
338
339
/** ----------------------------------------------------------------------------
340
 * API Authentification, gestion des identites centralisees
341
 */
342
343
/**
344
 * Fonction privée d'aiguillage des fonctions d'authentification
345
 *
346
 * Charge une fonction d'authentification présente dans un répertoire `auth/`.
347
 * Ainsi, utiliser `auth_administrer('informer_login', array('spip', ...)` appellera
348
 * `auth_spip_informer_login()` de `ecrire/auth/spip.php`.
349
 *
350
 * @uses charger_fonction()
351
 *
352
 * @param string $fonction
353
 *        Nom de la fonction d'authentification
354
 * @param array $args
355
 *        Le premier élément du tableau doit être le nom du système d'authentification
356
 *        choisi, tel que `spip` (par défaut) ou encore `ldap`.
357
 * @param mixed $defaut
358
 * @return mixed
359
 */
360
function auth_administrer($fonction, $args, $defaut = false) {
361
	$auth_methode = array_shift($args);
362
	$auth_methode = $auth_methode ? $auth_methode : 'spip'; // valeur par defaut au cas ou
363
	if ($auth = charger_fonction($auth_methode, 'auth', true)
364
		and function_exists($f = "auth_{$auth_methode}_$fonction")
365
	) {
366
		$res = call_user_func_array($f, $args);
367
	} else {
368
		$res = $defaut;
369
	}
370
	$res = pipeline(
371
		'auth_administrer',
372
		array(
373
			'args' => array(
374
				'fonction' => $fonction,
375
				'methode' => $auth_methode,
376
				'args' => $args
377
			),
378
			'data' => $res
379
		)
380
	);
381
	return $res;
382
}
383
384
/**
385
 * Pipeline pour inserer du contenu dans le formulaire de login
386
 *
387
 * @param array $flux
388
 * @return array
389
 */
390
function auth_formulaire_login($flux) {
391
	foreach ($GLOBALS['liste_des_authentifications'] as $methode) {
392
		$flux = auth_administrer('formulaire_login', array($methode, $flux), $flux);
393
	}
394
395
	return $flux;
396
}
397
398
399
/**
400
 * Retrouver le login interne lie a une info login saisie
401
 * la saisie peut correspondre a un login delegue
402
 * qui sera alors converti en login interne apres verification
403
 *
404
 * @param string $login
405
 * @param string $serveur
406
 * @return string/bool
0 ignored issues
show
Documentation introduced by
The doc-type string/bool could not be parsed: Unknown type name "string/bool" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
407
 */
408
function auth_retrouver_login($login, $serveur = '') {
409
	if (!spip_connect($serveur)) {
410
		include_spip('inc/minipres');
411
		echo minipres(_T('info_travaux_titre'), _T('titre_probleme_technique'));
412
		exit;
413
	}
414
415
	foreach ($GLOBALS['liste_des_authentifications'] as $methode) {
416
		if ($auteur = auth_administrer('retrouver_login', array($methode, $login, $serveur))) {
417
			return $auteur;
418
		}
419
	}
420
421
	return false;
422
}
423
424
/**
425
 * informer sur un login
426
 * Ce dernier transmet le tableau ci-dessous a la fonction JS informer_auteur
427
 * Il est invoque par la fonction JS actualise_auteur via la globale JS
428
 * page_auteur=#URL_PAGE{informer_auteur} dans le squelette login
429
 * N'y aurait-il pas plus simple ?
430
 *
431
 * @param string $login
432
 * @param string $serveur
433
 * @return array
434
 */
435
function auth_informer_login($login, $serveur = '') {
436
	if (!$login
437
		or !$login_base = auth_retrouver_login($login, $serveur)
438
		or !$row = sql_fetsel('*', 'spip_auteurs', 'login=' . sql_quote($login_base, $serveur, 'text'), '', '', '', '', $serveur)
439
	) {
440
441
		// generer de fausses infos, mais credibles, pour eviter une attaque
442
		// https://core.spip.net/issues/1758 + https://core.spip.net/issues/3691
443
		include_spip('inc/securiser_action');
444
		$fauxalea1 = md5('fauxalea' . secret_du_site() . $login . floor(date('U') / 86400));
445
		$fauxalea2 = md5('fauxalea' . secret_du_site() . $login . ceil(date('U') / 86400));
446
447
		$row = array(
448
			'login' => $login,
449
			'cnx' => '0',
450
			'logo' => '',
451
			'alea_actuel' => substr_replace($fauxalea1, '.', 24, 0),
452
			'alea_futur' => substr_replace($fauxalea2, '.', 24, 0)
453
		);
454
455
		// permettre d'autoriser l'envoi de password non crypte lorsque
456
		// l'auteur n'est pas (encore) declare dans SPIP, par exemple pour les cas
457
		// de premiere authentification via SPIP a une autre application.
458
		if (defined('_AUTORISER_AUTH_FAIBLE') and _AUTORISER_AUTH_FAIBLE) {
459
			$row['alea_actuel'] = '';
460
			$row['alea_futur'] = '';
461
		}
462
463
		return $row;
464
	}
465
466
	$prefs = unserialize($row['prefs']);
467
	$infos = array(
468
		'id_auteur' => $row['id_auteur'],
469
		'login' => $row['login'],
470
		'cnx' => ($prefs['cnx'] == 'perma') ? '1' : '0',
471
		'logo' => recuperer_fond('formulaires/inc-logo_auteur', $row),
472
	);
473
474
	// desactiver le hash md5 si pas auteur spip ?
475
	if ($row['source'] !== 'spip') {
476
		$row['alea_actuel'] = '';
477
		$row['alea_futur'] = '';
478
	}
479
	verifier_visiteur();
480
481
	return auth_administrer('informer_login', array($row['source'], $infos, $row, $serveur), $infos);
482
}
483
484
485
/**
486
 * Essayer les differentes sources d'authenfication dans l'ordre specifie.
487
 * S'en souvenir dans visiteur_session['auth']
488
 *
489
 * @param string $login
490
 * @param string $password
491
 * @param string $serveur
492
 * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array|string.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
493
 */
494
function auth_identifier_login($login, $password, $serveur = '') {
495
	$erreur = '';
496
	foreach ($GLOBALS['liste_des_authentifications'] as $methode) {
497
		if ($auth = charger_fonction($methode, 'auth', true)) {
498
			$auteur = $auth($login, $password, $serveur);
499
			if (is_array($auteur) and count($auteur)) {
500
				spip_log("connexion de $login par methode $methode");
501
				$auteur['auth'] = $methode;
502
				return $auteur;
503
			} elseif (is_string($auteur)) {
504
				$erreur .= "$auteur ";
505
			}
506
		}
507
	}
508
509
	return $erreur;
510
}
511
512
/**
513
 * Fournir une url de retour apres login par un SSO
514
 * pour finir l'authentification
515
 *
516
 * @param string $auth_methode
517
 * @param string $login
518
 * @param string $redirect
519
 * @param string $serveur
520
 * @return string
521
 */
522
function auth_url_retour_login($auth_methode, $login, $redirect = '', $serveur = '') {
0 ignored issues
show
Unused Code introduced by
The parameter $serveur is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
523
	$securiser_action = charger_fonction('securiser_action', 'inc');
524
	return $securiser_action('auth', "$auth_methode/$login", $redirect, true);
525
}
526
527
/**
528
 * Terminer l'action d'authentification d'un auteur
529
 *
530
 * @uses auth_administrer()
531
 *
532
 * @param string $auth_methode
533
 * @param string $login
534
 * @param string $serveur
535
 * @return mixed
536
 */
537
function auth_terminer_identifier_login($auth_methode, $login, $serveur = '') {
0 ignored issues
show
Unused Code introduced by
The parameter $auth_methode is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $login is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $serveur is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
538
	$args = func_get_args();
539
	$auteur = auth_administrer('terminer_identifier_login', $args);
540
	return $auteur;
541
}
542
543
/**
544
 * Loger un auteur suite a son identification
545
 *
546
 * @param array $auteur
547
 * @return bool
548
 */
549
function auth_loger($auteur) {
550
	if (!is_array($auteur) or !count($auteur)) {
551
		return false;
552
	}
553
554
	// initialiser et poser le cookie de session
555
	unset($_COOKIE['spip_session']);
556
	if (auth_init_droits($auteur) === false) {
557
		return false;
558
	}
559
560
	// initialiser les prefs
561
	$p = $GLOBALS['visiteur_session']['prefs'];
562
	$p['cnx'] = (isset($auteur['cookie']) and $auteur['cookie'] == 'oui') ? 'perma' : '';
563
564
	sql_updateq(
565
		'spip_auteurs',
566
		array('prefs' => serialize($p)),
567
		'id_auteur=' . intval($auteur['id_auteur'])
568
	);
569
570
	//  bloquer ici le visiteur qui tente d'abuser de ses droits
571
	verifier_visiteur();
572
	return true;
573
}
574
575
/**
576
 * Déconnexion de l'auteur
577
 *
578
 * @uses action_logout_dist()
579
 * return void
580
 **/
581
function auth_deloger() {
582
	$logout = charger_fonction('logout', 'action');
583
	$logout();
584
}
585
586
/**
587
 * Tester la possibilité de modifier le login d'authentification
588
 * pour la méthode donnée
589
 *
590
 * @uses auth_administrer()
591
 *
592
 * @param string $auth_methode
593
 * @param string $serveur
594
 * @return bool
595
 */
596
function auth_autoriser_modifier_login($auth_methode, $serveur = '') {
0 ignored issues
show
Unused Code introduced by
The parameter $auth_methode is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $serveur is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
597
	$args = func_get_args();
598
	return auth_administrer('autoriser_modifier_login', $args);
599
}
600
601
/**
602
 * Verifier la validite d'un nouveau login pour modification
603
 * pour la methode donnee
604
 *
605
 * @param string $auth_methode
606
 * @param string $new_login
607
 * @param int $id_auteur
608
 * @param string $serveur
609
 * @return string
610
 *  message d'erreur ou chaine vide si pas d'erreur
611
 */
612
function auth_verifier_login($auth_methode, $new_login, $id_auteur = 0, $serveur = '') {
0 ignored issues
show
Unused Code introduced by
The parameter $auth_methode is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $new_login is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $id_auteur is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $serveur is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
613
	$args = func_get_args();
614
	return auth_administrer('verifier_login', $args, '');
615
}
616
617
/**
618
 * Modifier le login d'un auteur pour la methode donnee
619
 *
620
 * @param string $auth_methode
621
 * @param string $new_login
622
 * @param int $id_auteur
623
 * @param string $serveur
624
 * @return bool
625
 */
626
function auth_modifier_login($auth_methode, $new_login, $id_auteur, $serveur = '') {
0 ignored issues
show
Unused Code introduced by
The parameter $auth_methode is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $new_login is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $id_auteur is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $serveur is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
627
	$args = func_get_args();
628
	return auth_administrer('modifier_login', $args);
629
}
630
631
/**
632
 * Tester la possibilité de modifier le pass
633
 * pour la méthode donnée
634
 *
635
 * @uses auth_administrer()
636
 *
637
 * @param string $auth_methode
638
 * @param string $serveur
639
 * @return bool
640
 *  succès ou échec
641
 */
642
function auth_autoriser_modifier_pass($auth_methode, $serveur = '') {
0 ignored issues
show
Unused Code introduced by
The parameter $auth_methode is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $serveur is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
643
	$args = func_get_args();
644
	return auth_administrer('autoriser_modifier_pass', $args);
645
}
646
647
/**
648
 * Verifier la validite d'un pass propose pour modification
649
 * pour la methode donnee
650
 *
651
 * @param string $auth_methode
652
 * @param string $login
653
 * @param string $new_pass
654
 * @param int $id_auteur
655
 * @param string $serveur
656
 * @return string
657
 *  message d'erreur ou chaine vide si pas d'erreur
658
 */
659
function auth_verifier_pass($auth_methode, $login, $new_pass, $id_auteur = 0, $serveur = '') {
0 ignored issues
show
Unused Code introduced by
The parameter $auth_methode is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $login is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $new_pass is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $id_auteur is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $serveur is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
660
	$args = func_get_args();
661
	return auth_administrer('verifier_pass', $args, '');
662
}
663
664
/**
665
 * Modifier le mot de passe d'un auteur
666
 * pour la methode donnee
667
 *
668
 * @param string $auth_methode
669
 * @param string $login
670
 * @param string $new_pass
671
 * @param int $id_auteur
672
 * @param string $serveur
673
 * @return bool
674
 *  succes ou echec
675
 */
676
function auth_modifier_pass($auth_methode, $login, $new_pass, $id_auteur, $serveur = '') {
0 ignored issues
show
Unused Code introduced by
The parameter $auth_methode is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $login is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $new_pass is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $id_auteur is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $serveur is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
677
	$args = func_get_args();
678
	return auth_administrer('modifier_pass', $args);
679
}
680
681
/**
682
 * Synchroniser un compte sur une base distante pour la methode
683
 * donnée lorsque des modifications sont faites dans la base auteur
684
 *
685
 * @param string|bool $auth_methode
686
 *   ici true permet de forcer la synchronisation de tous les acces pour toutes les methodes
687
 * @param int $id_auteur
688
 * @param array $champs
689
 * @param array $options
690
 * @param string $serveur
691
 * @return void
692
 */
693
function auth_synchroniser_distant(
694
	$auth_methode = true,
695
	$id_auteur = 0,
696
	$champs = array(),
697
	$options = array(),
698
	$serveur = ''
699
) {
700
	$args = func_get_args();
701
	if ($auth_methode === true or (isset($options['all']) and $options['all'] == true)) {
702
		$options['all'] = true; // ajouter une option all=>true pour chaque auth
703
		$args = array(true, $id_auteur, $champs, $options, $serveur);
704
		foreach ($GLOBALS['liste_des_authentifications'] as $methode) {
705
			array_shift($args);
706
			array_unshift($args, $methode);
707
			auth_administrer('synchroniser_distant', $args);
708
		}
709
	} else {
710
		auth_administrer('synchroniser_distant', $args);
711
	}
712
}
713
714
715
/**
716
 * Vérifier si l'auteur est bien authentifié
717
 *
718
 * @param string $login
719
 * @param string $pw
720
 * @param string $serveur
721
 * @return array|bool
722
 */
723
function lire_php_auth($login, $pw, $serveur = '') {
724
	if (
725
		!$login
726
		or !$login = auth_retrouver_login($login, $serveur)
727
	) {
728
		return false;
729
	}
730
731
	$row = sql_fetsel('*', 'spip_auteurs', 'login=' . sql_quote($login, $serveur, 'text'), '', '', '', '', $serveur);
732
733
	if (!$row) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $row of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
734
		if (spip_connect_ldap($serveur)
0 ignored issues
show
Deprecated Code introduced by
The function spip_connect_ldap() has been deprecated with message: Utiliser l'authentification LDAP de auth/ldap

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
735
			and $auth_ldap = charger_fonction('ldap', 'auth', true)
736
		) {
737
			return $auth_ldap($login, $pw, $serveur, true);
738
		}
739
740
		return false;
741
	}
742
	// su pas de source definie
743
	// ou auth/xxx introuvable, utiliser 'spip'
744
	if (!$auth_methode = $row['source']
745
		or !$auth = charger_fonction($auth_methode, 'auth', true)
746
	) {
747
		$auth = charger_fonction('spip', 'auth', true);
748
	}
749
750
	$auteur = '';
751
	if ($auth) {
752
		$auteur = $auth($login, $pw, $serveur, true);
753
	}
754
	// verifier que ce n'est pas un message d'erreur
755
	if (is_array($auteur) and count($auteur)) {
756
		return $auteur;
757
	}
758
759
	return false;
760
}
761
762
/**
763
 * entête php_auth (est-encore utilisé ?)
764
 *
765
 * @uses minipres()
766
 *
767
 * @param string $pb
768
 * @param string $raison
769
 * @param string $retour
770
 * @param string $url
771
 * @param string $re
772
 * @param string $lien
773
 */
774
function ask_php_auth($pb, $raison, $retour = '', $url = '', $re = '', $lien = '') {
775
	@Header('WWW-Authenticate: Basic realm="espace prive"');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
776
	@Header('HTTP/1.0 401 Unauthorized');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
777
	$corps = '';
778
	$public = generer_url_public();
779
	$ecrire = generer_url_ecrire();
780
	$retour = $retour ? $retour : _T('icone_retour');
781
	$corps .= "<p>$raison</p>[<a href='$public'>$retour</a>] ";
782
	if ($url) {
783
		$corps .= "[<a href='" . generer_url_action('cookie', "essai_auth_http=oui&$url") . "'>$re</a>]";
784
	}
785
786
	if ($lien) {
787
		$corps .= " [<a href='$ecrire'>" . _T('login_espace_prive') . '</a>]';
788
	}
789
	include_spip('inc/minipres');
790
	echo minipres($pb, $corps);
791
	exit;
792
}
793