Completed
Push — spip-2.1 ( b6b097 )
by cam
42:28 queued 30:44
created

etape_3.php ➔ install_bases()   F

Complexity

Conditions 23
Paths 12820

Size

Total Lines 164
Code Lines 110

Duplication

Lines 10
Ratio 6.1 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 23
eloc 110
nc 12820
nop 7
dl 10
loc 164
rs 2
c 1
b 0
f 0

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://code.spip.net/@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 View Code Duplication
	if (!defined('_INSTALL_TABLE_PREFIX')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
	if (preg_match(',(.*):(.*),', $adresse_db, $r))
39
		list(,$adresse_db, $port) = $r;
40
	else $port = '';
41
42
	$GLOBALS['connexions'][$server_db]
43
	= spip_connect_db($adresse_db, $port, $login_db, $pass_db, '', $server_db);
44
45
	$GLOBALS['connexions'][$server_db][$GLOBALS['spip_sql_version']]
46
	= $GLOBALS['spip_' . $server_db .'_functions_' . $GLOBALS['spip_sql_version']];
47
48
	$fquery = sql_serveur('query', $server_db);
49
	if ($choix_db == "new_spip") {
50
		$re = ',^[a-z_][a-z_0-9-]*$,i';
51
		if (preg_match($re, $sel_db)) {
52
			$ok = sql_create_base($sel_db, $server_db);
53
			if (!$ok) {
54
				$re = "Impossible de creer la base $re";
55
				spip_log($re);
56
				return "<p>"._T("avis_connexion_erreur_creer_base")."</p><!--\n$re\n-->";
57
			}
58
		} else {
59
			$re = "Le nom de la base doit correspondre a $re";
60
			spip_log($re);
61
			return "<p>"._T("avis_connexion_erreur_nom_base")."</p><!--\n$re\n-->";
62
		}
63
	}
64
65
	// on rejoue la connexion apres avoir teste si il faut lui indiquer
66
	// un sql_mode
67
	install_mode_appel($server_db, false);
68
	$GLOBALS['connexions'][$server_db]
69
	= spip_connect_db($adresse_db, $port, $login_db, $pass_db, $sel_db, $server_db);
70
71
	$GLOBALS['connexions'][$server_db][$GLOBALS['spip_sql_version']]
72
	= $GLOBALS['spip_' . $server_db .'_functions_' . $GLOBALS['spip_sql_version']];
73
74
	// Completer le tableau decrivant la connexion
75
76
	$GLOBALS['connexions'][$server_db]['prefixe'] = $table_prefix;
77
	$GLOBALS['connexions'][$server_db]['db'] = $sel_db;
78
79
	$old = sql_showbase($table_prefix  . "_meta", $server_db);
80
	if ($old) $old = sql_fetch($old, $server_db);
81
	if (!$old) {
82
83
		// Si possible, demander au serveur d'envoyer les textes
84
		// dans le codage std de SPIP,
85
		$charset = sql_get_charset(_DEFAULT_CHARSET, $server_db);
86
87
		if ($charset) {
88
			sql_set_charset($charset['charset'], $server_db);
89
			$GLOBALS['meta']['charset_sql_base'] = 
90
				$charset['charset'];
91
			$GLOBALS['meta']['charset_collation_sql_base'] = 
92
				$charset['collation'];
93
			$GLOBALS['meta']['charset_sql_connexion'] = 
94
				$charset['charset'];
95
			$charsetbase = $charset['charset'];
96
		} else {
97
			spip_log(_DEFAULT_CHARSET . " inconnu du serveur SQL");
98
			$charsetbase = 'standard';
99
		}
100
		spip_log("Creation des tables. Codage $charsetbase");
101
		creer_base($server_db); // AT LAST
102
		// memoriser avec quel charset on l'a creee
103
104
		if ($charset) {
105
			$t = array('nom' => 'charset_sql_base',
106
				   'valeur' => $charset['charset'],
107
				   'impt' => 'non');
108
			@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...
109
			$t['nom'] = 'charset_collation_sql_base';
110
			$t['valeur'] = $charset['collation'];
111
			@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...
112
			$t['nom'] = 'charset_sql_connexion';
113
			$t['valeur'] = $charset['charset'];
114
			@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...
115
		}
116
		$t = array('nom' => 'version_installee',
117
			   'valeur' => $spip_version_base,
118
			   'impt' => 'non');
119
		@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...
120
		$t['nom'] = 'nouvelle_install';
121
		$t['valeur'] = 1;
122
		@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...
123
		// positionner la langue par defaut du site si un cookie de lang a ete mis
124
		if (isset($_COOKIE['spip_lang_ecrire'])){
125
			@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...
126
		}
127
	} else {
128
129
	  // pour recreer les tables disparues au besoin
130
	  spip_log("Table des Meta deja la. Verification des autres.");
131
      creer_base($server_db); 
132
      creer_base_types_doc($server_db);
133
	  $fupdateq = sql_serveur('updateq', $server_db);
134
135
	  $r = $fquery("SELECT valeur FROM spip_meta WHERE nom='version_installee'", $server_db);
136
137
	  if ($r) $r = sql_fetch($r, $server_db);
138
	  $version_installee = !$r ? 0 : (double) $r['valeur'];
139
	  if (!$version_installee OR ($spip_version_base < $version_installee)) {
140
	    $fupdateq('spip_meta', array('valeur'=>$spip_version_base, 'impt'=>'non'), "nom='version_installee'",'', $server_db);
141
	    spip_log("nouvelle version installee: $spip_version_base");
142
	  }
143
	  // eliminer la derniere operation d'admin mal terminee
144
	  // notamment la mise a jour 
145
	  @$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...
146
	}
147
148
	// recuperer le charset de la connexion dans les meta
149
	$charset = '';
150
	$r = $fquery("SELECT valeur FROM spip_meta WHERE nom='charset_sql_connexion'", $server_db);
151
	if ($r) $r = sql_fetch($r, $server_db);
152
	if ($r) $charset = $r['valeur'];
153
154
	$ligne_rappel = install_mode_appel($server_db);
155
156
	$result_ok = @$fquery("SELECT COUNT(*) FROM spip_meta", $server_db);
157
	if (!$result_ok) return "<!--\nvielle = $old rappel= $ligne_rappel\n-->";
158
159
	if ($chmod_db) {
160
		install_fichier_connexion(_FILE_CHMOD_TMP, "if (!defined('_SPIP_CHMOD')) define('_SPIP_CHMOD', ". sprintf('0%3o',$chmod_db).");\n");
161
	}
162
163
	// si ce fichier existe a cette etape c'est qu'il provient
164
	// d'une installation qui ne l'a pas cree correctement.
165
	// Le supprimer pour que _FILE_CONNECT_TMP prime.
166
167
	if (_FILE_CONNECT AND file_exists(_FILE_CONNECT))
168
		spip_unlink(_FILE_CONNECT);
169
170
	install_fichier_connexion(_FILE_CONNECT_TMP, 
171
				  $ligne_rappel
172
				  . install_connexion($adresse_db,
173
						      $port,
174
						      $login_db,
175
						      $pass_db,
176
						      $sel_db,
177
						      $server_db,
178
						      $table_prefix,
179
					        '',
180
					        $charset));
0 ignored issues
show
Unused Code introduced by
The call to install_connexion() has too many arguments starting with $charset.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
181
	return '';
182
}
183
184
// http://code.spip.net/@install_propose_ldap
185
function install_propose_ldap()
186
{
187
	return generer_form_ecrire('install', (
188
			fieldset(_T('info_authentification_externe'),
189
				array(
190
				'etape' => array(
191
					'label' => _T('texte_annuaire_ldap_1'),
192
					'valeur' => 'ldap1',
193
					'hidden' => true
194
					)),
195
				 bouton_suivant(_T('bouton_acces_ldap'))
196
				 )));
197
}
198
199
200
// http://code.spip.net/@install_premier_auteur
201
function install_premier_auteur($email, $login, $nom, $pass, $hidden, $auteur_obligatoire)
202
{
203
	return info_progression_etape(3,'etape_','install/') .
204
		info_etape(_T('info_informations_personnelles'),
205
206
		     "<b>"._T('texte_informations_personnelles_1')."</b>" .
207
			     aide ("install5", true) .
0 ignored issues
show
Unused Code introduced by
The call to aide() has too many arguments starting with true.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
208
			     "<p>" .
209
			     ($auteur_obligatoire?
210
				     ''
211
				     :
212
				     _T('texte_informations_personnelles_2') . " " . _T('info_laisser_champs_vides')
213
			     ))
214
	. generer_form_ecrire('install', (
215
			  "\n<input type='hidden' name='etape' value='3b' />"
216
			  . $hidden
217
			  . fieldset(_T('info_identification_publique'),
218
				    array(
219
					  'nom' => array(
220
							 'label' => "<b>"._T('entree_signature')."</b><br />\n"._T('entree_nom_pseudo_1')."\n",
221
							 'valeur' => $nom,
222
						   'required' => $auteur_obligatoire,
223
							 ),
224
					  'email' => array(
225
							   'label' => "<b>"._T('entree_adresse_email')."</b>\n",
226
							   'valeur' => $email,
227
							   )
228
					  )
229
				    )
230
231
			  . fieldset(_T('entree_identifiants_connexion'),
232
				   array(
233
					 'login' => array(
234
							  'label' => "<b>"._T('entree_login')."</b><br />\n"._T('info_login_trop_court_car_pluriel', array('nb' => _LOGIN_TROP_COURT))."\n",
235
							  'valeur' => $login,
236
	              'required' => $auteur_obligatoire,
237
							  ),
238
					 'pass' => array(
239
							 'label' => "<b>"._T('entree_mot_passe')."</b><br />\n"._T('info_passe_trop_court_car_pluriel', array('nb' => _PASS_LONGUEUR_MINI))."\n",
240
							 'valeur' => $pass,
241
	             'required' => $auteur_obligatoire,
242
							 ),
243
					 'pass_verif' => array(
244
							       'label' => "<b>"._T('info_confirmer_passe')."</b><br />\n",
245
							       'valeur' => $pass,
246
	                   'required' => $auteur_obligatoire,
247
							       )
248
					 )
249
				     )
250
			  . bouton_suivant()));
251
}
252
253
// http://code.spip.net/@install_etape_3_dist
254
function install_etape_3_dist()
255
{
256
	$ldap_present = _request('ldap_present');
257
258
	if (!$ldap_present) {
259
		$adresse_db = defined('_INSTALL_HOST_DB')
260
		? _INSTALL_HOST_DB
261
		: _request('adresse_db');
262
263
		$login_db = defined('_INSTALL_USER_DB')
264
		? _INSTALL_USER_DB
265
		: _request('login_db');
266
267
		$pass_db = defined('_INSTALL_PASS_DB')
268
		? _INSTALL_PASS_DB
269
		: _request('pass_db');
270
271
		$server_db = defined('_INSTALL_SERVER_DB')
272
		? _INSTALL_SERVER_DB
273
		: _request('server_db');
274
275
		$chmod_db = defined('_SPIP_CHMOD')
276
		? _SPIP_CHMOD
277
		: _request('chmod');
278
279
		$choix_db = defined('_INSTALL_NAME_DB')
280
		? _INSTALL_NAME_DB
281
		: _request('choix_db');
282
283
		$sel_db = ($choix_db == "new_spip")
284
		? _request('table_new') : $choix_db;
285
286
		$res = install_bases($adresse_db, $login_db, $pass_db,  $server_db, $choix_db, $sel_db, $chmod_db);
287
288
		if ($res) {
289
			$res = info_progression_etape(2,'etape_','install/', true)
290
				. "<div class='error'><h3>"._T('avis_operation_echec')."</h3>"
291
			  . $res
292
			  . "<p>"._T('texte_operation_echec')."</p>"
293
			  . "</div>";
294
		}
295
	
296
	} else { 
297
		$res = '';
298
		list($adresse_db, $login_db, $pass_db, $sel_db, $server_db) = analyse_fichier_connection(_FILE_CONNECT_TMP);
299
		$GLOBALS['connexions'][$server_db] = spip_connect_db($adresse_db, $sel_db, $login_db, $pass_db, $sel_db, $server_db);
300
	}
301
302
	if (!$res) {
303
		if (file_exists(_FILE_CONNECT_TMP))
304
			include(_FILE_CONNECT_TMP);
305
		else
306
			redirige_url_ecrire('install');
307
	
308
		if (file_exists(_FILE_CHMOD_TMP))
309
			include(_FILE_CHMOD_TMP);
310
		else
311
			redirige_url_ecrire('install');
312
313
		$hidden = predef_ou_cache($adresse_db, $login_db, $pass_db, $server_db)
314
		  . (defined('_INSTALL_NAME_DB') ? ''
315
		     : "\n<input type='hidden' name='sel_db' value='$sel_db' />");
316
317
		$auteur_obligatoire = ($ldap_present?0:!sql_countsel('spip_auteurs','','','',$server_db));
318
319
		$res =  "<div class='success'><b>"
320
		. _T('info_base_installee')
321
		. "</b></div>"
322
		. install_premier_auteur(_request('email'),
323
					_request('login'),
324
					_request('nom'),
325
					_request('pass'),
326
					 $hidden, $auteur_obligatoire)
327
		  . (($ldap_present  OR !function_exists('ldap_connect'))
328
		     ?  '' : install_propose_ldap());
329
	}
330
331
	echo install_debut_html();
332
	echo $res;
333
	echo install_fin_html();
334
}
335
336
?>
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...
337