Completed
Push — spip-3.0 ( 5d8b58 )
by cam
53:01 queued 42:30
created

etape_3.php ➔ install_bases()   F

Complexity

Conditions 20
Paths 2885

Size

Total Lines 150
Code Lines 99

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 20
eloc 99
c 0
b 0
f 0
nc 2885
nop 7
dl 0
loc 150
rs 2

How to fix   Long Method    Complexity   

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-2016                                                *
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')) return;
14
15
include_spip('inc/headers');
16
include_spip('base/abstract_sql');
17
18
// http://doc.spip.org/@install_bases
19
function install_bases($adresse_db, $login_db, $pass_db,  $server_db, $choix_db, $sel_db, $chmod_db){
20
	global $spip_version_base;
21
22
	// Prefix des tables :
23
	// contrairement a ce qui est dit dans le message (trop strict mais c'est
24
	// pour notre bien), on va tolerer les chiffres en plus des minuscules
25
	// S'il n'est pas defini par mes_options/inc/mutualiser, on va le creer
26
	// a partir de ce qui est envoye a l'installation
27
	if (!defined('_INSTALL_TABLE_PREFIX')) {
28
		$table_prefix = ($GLOBALS['table_prefix'] != 'spip')
29
		? $GLOBALS['table_prefix']
30
		: trim(preg_replace(',[^a-z0-9],','',strtolower(_request('tprefix'))));
31
		// S'il est vide on remet spip
32
		if (!$table_prefix)
33
			$table_prefix = 'spip';
34
	} else {
35
		$table_prefix = _INSTALL_TABLE_PREFIX;
36
	}
37
38
	$GLOBALS['connexions'][$server_db]
39
	= spip_connect_db($adresse_db, 0, $login_db, $pass_db, '', $server_db);
40
41
	$GLOBALS['connexions'][$server_db][$GLOBALS['spip_sql_version']]
42
	= $GLOBALS['spip_' . $server_db .'_functions_' . $GLOBALS['spip_sql_version']];
43
44
	$fquery = sql_serveur('query', $server_db);
45
	if ($choix_db == "new_spip") {
46
		$re = ',^[a-z_][a-z_0-9-]*$,i';
47
		if (preg_match($re, $sel_db))
48
			sql_create_base($sel_db, $server_db);
49
		else {
50
		  $re = "Le nom de la base doit correspondre a $re";
51
		  spip_log($re);
52
		  return "<p>"._T("avis_connexion_erreur_nom_base")."</p><!--\n$re\n-->";
53
		}
54
	}
55
56
	// on rejoue la connexion apres avoir teste si il faut lui indiquer
57
	// un sql_mode
58
	install_mode_appel($server_db, false);
59
	$GLOBALS['connexions'][$server_db]
60
	= spip_connect_db($adresse_db, $sel_db, $login_db, $pass_db, $sel_db, $server_db);
61
62
	$GLOBALS['connexions'][$server_db][$GLOBALS['spip_sql_version']]
63
	= $GLOBALS['spip_' . $server_db .'_functions_' . $GLOBALS['spip_sql_version']];
64
65
	// Completer le tableau decrivant la connexion
66
67
	$GLOBALS['connexions'][$server_db]['prefixe'] = $table_prefix;
68
	$GLOBALS['connexions'][$server_db]['db'] = $sel_db;
69
70
	$old = sql_showbase($table_prefix  . "_meta", $server_db);
71
	if ($old) $old = sql_fetch($old, $server_db);
72
	if (!$old) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $old 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...
73
74
		// Si possible, demander au serveur d'envoyer les textes
75
		// dans le codage std de SPIP,
76
		$charset = sql_get_charset(_DEFAULT_CHARSET, $server_db);
77
78
		if ($charset) {
79
			sql_set_charset($charset['charset'], $server_db);
80
			$GLOBALS['meta']['charset_sql_base'] = 
81
				$charset['charset'];
82
			$GLOBALS['meta']['charset_collation_sql_base'] = 
83
				$charset['collation'];
84
			$GLOBALS['meta']['charset_sql_connexion'] = 
85
				$charset['charset'];
86
			$charsetbase = $charset['charset'];
87
		} else {
88
			spip_log(_DEFAULT_CHARSET . " inconnu du serveur SQL");
89
			$charsetbase = 'standard';
90
		}
91
		spip_log("Creation des tables. Codage $charsetbase");
92
		creer_base($server_db); // AT LAST
93
		// memoriser avec quel charset on l'a creee
94
95
		if ($charset) {
96
			$t = array('nom' => 'charset_sql_base',
97
				   'valeur' => $charset['charset'],
98
				   'impt' => 'non');
99
			@sql_insertq('spip_meta', $t, '', $server_db);
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...
100
			$t['nom'] = 'charset_collation_sql_base';
101
			$t['valeur'] = $charset['collation'];
102
			@sql_insertq('spip_meta', $t, '', $server_db);
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...
103
			$t['nom'] = 'charset_sql_connexion';
104
			$t['valeur'] = $charset['charset'];
105
			@sql_insertq('spip_meta', $t, '', $server_db);
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...
106
		}
107
		$t = array('nom' => 'version_installee',
108
			   'valeur' => $spip_version_base,
109
			   'impt' => 'non');
110
		@sql_insertq('spip_meta', $t, '', $server_db);
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...
111
		$t['nom'] = 'nouvelle_install';
112
		$t['valeur'] = 1;
113
		@sql_insertq('spip_meta', $t, '', $server_db);
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...
114
		// positionner la langue par defaut du site si un cookie de lang a ete mis
115
		if (isset($_COOKIE['spip_lang_ecrire'])){
116
			@sql_insertq('spip_meta', array('nom'=>'langue_site','valeur'=>$_COOKIE['spip_lang_ecrire']), '', $server_db);
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...
117
		}
118
	} else {
119
120
	  // pour recreer les tables disparues au besoin
121
	  spip_log("Table des Meta deja la. Verification des autres.");
122
	  creer_base($server_db); 
123
	  $fupdateq = sql_serveur('updateq', $server_db);
124
125
	  $r = $fquery("SELECT valeur FROM spip_meta WHERE nom='version_installee'", $server_db);
126
127
	  if ($r) $r = sql_fetch($r, $server_db);
128
	  $version_installee = !$r ? 0 : (double) $r['valeur'];
129
	  if (!$version_installee OR ($spip_version_base < $version_installee)) {
130
	    $fupdateq('spip_meta', array('valeur'=>$spip_version_base, 'impt'=>'non'), "nom='version_installee'",'', $server_db);
131
	    spip_log("nouvelle version installee: $spip_version_base");
132
	  }
133
	  // eliminer la derniere operation d'admin mal terminee
134
	  // notamment la mise a jour 
135
	  @$fquery("DELETE FROM spip_meta WHERE nom='import_all' OR  nom='admin'", $server_db);
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...
136
	}
137
138
	$ligne_rappel = install_mode_appel($server_db);
139
140
	$result_ok = @$fquery("SELECT COUNT(*) FROM spip_meta", $server_db);
141
	if (!$result_ok) return "<!--\nvielle = $old rappel= $ligne_rappel\n-->";
142
143
	if ($chmod_db) {
144
		install_fichier_connexion(_FILE_CHMOD_TMP, "if (!defined('_SPIP_CHMOD')) define('_SPIP_CHMOD', ". sprintf('0%3o',$chmod_db).");\n");
145
	}
146
147
	if (preg_match(',(.*):(.*),', $adresse_db, $r))
148
		list(,$adresse_db, $port) = $r;
149
	else $port = '';
150
151
	// si ce fichier existe a cette etape c'est qu'il provient
152
	// d'une installation qui ne l'a pas cree correctement.
153
	// Le supprimer pour que _FILE_CONNECT_TMP prime.
154
155
	if (_FILE_CONNECT AND file_exists(_FILE_CONNECT))
156
		spip_unlink(_FILE_CONNECT);
157
158
	install_fichier_connexion(_FILE_CONNECT_TMP, 
159
				  $ligne_rappel
160
				  . install_connexion($adresse_db,
161
						      $port,
162
						      $login_db,
163
						      $pass_db,
164
						      $sel_db,
165
						      $server_db,
166
						      $table_prefix));
167
	return '';
168
}
169
170
// http://doc.spip.org/@install_propose_ldap
171
function install_propose_ldap()
172
{
173
	return generer_form_ecrire('install', (
174
			fieldset(_T('info_authentification_externe'),
175
				array(
176
				'etape' => array(
177
					'label' => _T('texte_annuaire_ldap_1'),
178
					'valeur' => 'ldap1',
179
					'hidden' => true
180
					)),
181
				 bouton_suivant(_T('bouton_acces_ldap'))
182
				 )));
183
}
184
185
186
// http://doc.spip.org/@install_premier_auteur
187
function install_premier_auteur($email, $login, $nom, $pass, $hidden, $auteur_obligatoire)
188
{
189
	return info_progression_etape(3,'etape_','install/') .
190
		info_etape(_T('info_informations_personnelles'),
191
192
		     "<b>"._T('texte_informations_personnelles_1')."</b>" .
193
			     aide ("install5", true) .
194
			     "<p>" .
195
			     ($auteur_obligatoire?
196
				     ''
197
				     :
198
				     _T('texte_informations_personnelles_2') . " " . _T('info_laisser_champs_vides')
199
			     ))
200
	. generer_form_ecrire('install', (
201
			  "\n<input type='hidden' name='etape' value='3b' />"
202
			  . $hidden
203
			  . fieldset(_T('info_identification_publique'),
204
				    array(
205
					  'nom' => array(
206
							 'label' => "<b>"._T('entree_signature')."</b><br />\n"._T('entree_nom_pseudo_1')."\n",
207
							 'valeur' => $nom,
208
						   'required' => $auteur_obligatoire,
209
							 ),
210
					  'email' => array(
211
							   'label' => "<b>"._T('entree_adresse_email')."</b>\n",
212
							   'valeur' => $email,
213
							   )
214
					  )
215
				    )
216
217
			  . fieldset(_T('entree_identifiants_connexion'),
218
				   array(
219
					 'login' => array(
220
							  'label' => "<b>"._T('entree_login')."</b><br />\n"._T('info_login_trop_court_car_pluriel', array('nb' => _LOGIN_TROP_COURT))."\n",
221
							  'valeur' => $login,
222
	              'required' => $auteur_obligatoire,
223
							  ),
224
					 'pass' => array(
225
							 'label' => "<b>"._T('entree_mot_passe')."</b><br />\n"._T('info_passe_trop_court_car_pluriel', array('nb' => _PASS_LONGUEUR_MINI))."\n",
226
							 'valeur' => $pass,
227
	             'required' => $auteur_obligatoire,
228
							 ),
229
					 'pass_verif' => array(
230
							       'label' => "<b>"._T('info_confirmer_passe')."</b><br />\n",
231
							       'valeur' => $pass,
232
	                   'required' => $auteur_obligatoire,
233
							       )
234
					 )
235
				     )
236
			  . bouton_suivant()));
237
}
238
239
// http://doc.spip.org/@install_etape_3_dist
240
function install_etape_3_dist()
241
{
242
	$ldap_present = _request('ldap_present');
243
244
	if (!$ldap_present) {
245
		$adresse_db = defined('_INSTALL_HOST_DB')
246
		? _INSTALL_HOST_DB
247
		: _request('adresse_db');
248
249
		$login_db = defined('_INSTALL_USER_DB')
250
		? _INSTALL_USER_DB
251
		: _request('login_db');
252
253
		$pass_db = defined('_INSTALL_PASS_DB')
254
		? _INSTALL_PASS_DB
255
		: _request('pass_db');
256
257
		$server_db = defined('_INSTALL_SERVER_DB')
258
		? _INSTALL_SERVER_DB
259
		: _request('server_db');
260
261
		$chmod_db = defined('_SPIP_CHMOD')
262
		? _SPIP_CHMOD
263
		: _request('chmod');
264
265
		$choix_db = defined('_INSTALL_NAME_DB')
266
		? _INSTALL_NAME_DB
267
		: _request('choix_db');
268
269
		$sel_db = ($choix_db == "new_spip")
270
		? _request('table_new') : $choix_db;
271
272
		$res = install_bases($adresse_db, $login_db, $pass_db,  $server_db, $choix_db, $sel_db, $chmod_db);
273
274
		if ($res) {
275
			$res = info_progression_etape(2,'etape_','install/', true)
276
				. "<div class='error'><h3>"._T('avis_operation_echec')."</h3>"
277
			  . $res
278
			  . "<p>"._T('texte_operation_echec')."</p>"
279
			  . "</div>";
280
		}
281
	
282
	} else { 
283
		$res = '';
284
		list($adresse_db, $login_db, $pass_db, $sel_db, $server_db) = analyse_fichier_connection(_FILE_CONNECT_TMP);
285
		$GLOBALS['connexions'][$server_db] = spip_connect_db($adresse_db, $sel_db, $login_db, $pass_db, $sel_db, $server_db);
286
	}
287
288
	if (!$res) {
289
		if (file_exists(_FILE_CONNECT_TMP))
290
			include(_FILE_CONNECT_TMP);
291
		else
292
			redirige_url_ecrire('install');
293
	
294
		if (file_exists(_FILE_CHMOD_TMP))
295
			include(_FILE_CHMOD_TMP);
296
		else
297
			redirige_url_ecrire('install');
298
299
		$hidden = predef_ou_cache($adresse_db, $login_db, $pass_db, $server_db)
300
		  . (defined('_INSTALL_NAME_DB') ? ''
301
		     : "\n<input type='hidden' name='sel_db' value='$sel_db' />");
302
303
		$auteur_obligatoire = ($ldap_present?0:!sql_countsel('spip_auteurs','','','',$server_db));
304
305
		$res =  "<div class='success'><b>"
306
		. _T('info_base_installee')
307
		. "</b></div>"
308
		. install_premier_auteur(_request('email'),
309
					_request('login'),
310
					_request('nom'),
311
					_request('pass'),
312
					 $hidden, $auteur_obligatoire)
313
		  . (($ldap_present  OR !function_exists('ldap_connect'))
314
		     ?  '' : install_propose_ldap());
315
	}
316
317
	echo install_debut_html();
318
	echo $res;
319
	echo install_fin_html();
320
}
321
322
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
323