Completed
Push — master ( 1a3b2f...b50fb4 )
by cam
09:59
created

etape_ldap4.php ➔ install_etape_ldap4_dist()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 6
nop 0
dl 0
loc 56
rs 8.9599
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/***************************************************************************\
4
 *  SPIP, Systeme de publication pour l'internet                           *
5
 *                                                                         *
6
 *  Copyright (c) 2001-2018                                                *
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
if (!defined('_ECRIRE_INC_VERSION')) {
14
	return;
15
}
16
17
include_spip('auth/ldap');
18
19
// http://code.spip.net/@install_etape_ldap4_dist
20
function install_etape_ldap4_dist() {
21
	$adresse_ldap = _request('adresse_ldap');
22
	$login_ldap = _request('login_ldap');
23
	$pass_ldap = _request('pass_ldap');
24
	$port_ldap = _request('port_ldap');
25
	$base_ldap = _request('base_ldap');
26
	$base_ldap_text = _request('base_ldap_text');
27
	if (!$base_ldap) {
28
		$base_ldap = $base_ldap_text;
29
	}
30
31
	echo install_debut_html('AUTO', ' onload="document.getElementById(\'suivant\').focus();return false;"');
32
33
	$ldap_link = ldap_connect($adresse_ldap, $port_ldap);
34
	@ldap_bind($ldap_link, $login_ldap, $pass_ldap);
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...
35
36
	// Essayer de verifier le chemin fourni
37
	$r = @ldap_compare($ldap_link, $base_ldap, 'objectClass', '');
0 ignored issues
show
Unused Code introduced by
$r 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...
38
	$fail = (ldap_errno($ldap_link) == 32);
39
40
	if ($fail) {
41
		echo info_etape(_T('info_chemin_acces_annuaire')),
42
		info_progression_etape(3, 'etape_ldap', 'install/', true),
43
			"<div class='error'><p><b>" . _T('avis_operation_echec') . '</b></p><p>' . _T('avis_chemin_invalide_1'),
44
			' (<tt>' . spip_htmlspecialchars($base_ldap) . '</tt>) ' . _T('avis_chemin_invalide_2') . '</p></div>';
45
	} else {
46
		info_etape(_T('info_reglage_ldap'));
0 ignored issues
show
Unused Code introduced by
The call to the function info_etape() seems unnecessary as the function has no side-effects.
Loading history...
47
		echo info_progression_etape(4, 'etape_ldap', 'install/');
48
49
		$statuts = liste_statuts_ldap();
50
		$statut_ldap = defined('_INSTALL_STATUT_LDAP')
51
			? _INSTALL_STATUT_LDAP
52
			: $GLOBALS['liste_des_statuts']['info_redacteurs'];
53
54
55
		$res = install_propager(array('adresse_ldap', 'port_ldap', 'login_ldap', 'pass_ldap', 'protocole_ldap', 'tls_ldap'))
56
			. "<input type='hidden' name='etape' value='ldap5' />"
57
			. "<input type='hidden' name='base_ldap' value='" . spip_htmlentities($base_ldap) . "' />"
58
			. fieldset(
59
				_T('info_statut_utilisateurs_1'),
60
				array(
61
					'statut_ldap' => array(
62
						'label' => _T('info_statut_utilisateurs_2') . '<br />',
63
						'valeur' => $statut_ldap,
64
						'alternatives' => $statuts
65
					)
66
				)
67
			)
68
			. install_ldap_correspondances()
69
			. bouton_suivant();
70
71
		echo generer_form_ecrire('install', $res);
72
	}
73
74
	echo install_fin_html();
75
}
76
77
// http://code.spip.net/@liste_statuts_ldap
78
function liste_statuts_ldap() {
79
	$recom = array(
80
		'info_administrateurs' => ('<b>' . _T('info_administrateur_1') . '</b> ' . _T('info_administrateur_2') . '<br />'),
81
		'info_redacteurs' => ('<b>' . _T('info_redacteur_1') . '</b> ' . _T('info_redacteur_2') . '<br />'),
82
		'info_visiteurs' => ('<b>' . _T('info_visiteur_1') . '</b> ' . _T('info_visiteur_2') . '<br />')
83
	);
84
85
	$res = array();
86
	foreach ($GLOBALS['liste_des_statuts'] as $k => $v) {
87
		if (isset($recom[$k])) {
88
			$res[$v] = $recom[$k];
89
		}
90
	}
91
92
	return $res;
93
}
94
95
function install_ldap_correspondances() {
96
	$champs = array();
97
	foreach (is_array($GLOBALS['ldap_attributes']) ? $GLOBALS['ldap_attributes'] : array() as $champ => $v) {
98
		$nom = 'ldap_' . $champ;
99
		$val = is_array($v) ? join(',', $v) : strval($v);
100
		$champs[$nom] = array(
101
			'label' => _T('ldap_correspondance', array('champ' => "<tt>$champ</tt>")) . '<br />',
102
			'valeur' => $val
103
		);
104
	}
105
106
	return !$champs ?
107
		'' : fieldset(_T('ldap_correspondance_1'), $champs, '', _T('ldap_correspondance_2') . '<br /><br />');
108
}
109