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

editer_auteur.php ➔ auteur_instituer()   F

Complexity

Conditions 28
Paths 7201

Size

Total Lines 111
Code Lines 70

Duplication

Lines 7
Ratio 6.31 %

Importance

Changes 0
Metric Value
cc 28
eloc 70
c 0
b 0
f 0
nc 7201
nop 3
dl 7
loc 111
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
// http://doc.spip.org/@action_editer_auteur_dist
16
function action_editer_auteur_dist($arg=null) {
17
18
	if (is_null($arg)){
19
		$securiser_action = charger_fonction('securiser_action', 'inc');
20
		$arg = $securiser_action();
21
	}
22
23
24
	// si id_auteur n'est pas un nombre, c'est une creation
25
	if (!$id_auteur = intval($arg)) {
26
27
		if (($id_auteur = auteur_inserer()) > 0){
28
29
			# cf. GROS HACK
30
			# recuperer l'eventuel logo charge avant la creation
31
			# ils ont un id = 0-id_auteur de la session
32
			$id_hack = 0 - $GLOBALS['visiteur_session']['id_auteur'];
33
			$chercher_logo = charger_fonction('chercher_logo', 'inc');
34 View Code Duplication
			if (list($logo) = $chercher_logo($id_hack, 'id_auteur', 'on'))
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...
35
				rename($logo, str_replace($id_hack, $id_auteur, $logo));
36 View Code Duplication
			if (list($logo) = $chercher_logo($id_hack, 'id_auteur', 'off'))
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...
37
				rename($logo, str_replace($id_hack, $id_auteur, $logo));
38
		}
39
	}
40
41
	// Enregistre l'envoi dans la BD
42
	$err = "";
43
	if ($id_auteur > 0)
44
		$err = auteur_modifier($id_auteur);
45
46
	if ($err)
47
		spip_log("echec editeur auteur: $err",_LOG_ERREUR);
48
49
	return array($id_auteur,$err);
50
}
51
52
/**
53
 * Inserer un auteur en base
54
 * @param string $source
0 ignored issues
show
Documentation introduced by
Should the type for parameter $source not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
55
 * @return int
56
 */
57
function auteur_inserer($source=null) {
58
59
	// Ce qu'on va demander comme modifications
60
	$champs = array();
61
	$champs['source'] = $source?$source:'spip';
62
63
	$champs['login'] = '';
64
	$champs['statut'] = '5poubelle';  // inutilisable tant qu'il n'a pas ete renseigne et institue
65
	$champs['webmestre'] = 'non';
66
67
	// Envoyer aux plugins
68
	$champs = pipeline('pre_insertion',
69
		array(
70
			'args' => array(
71
				'table' => 'spip_auteurs',
72
			),
73
			'data' => $champs
74
		)
75
	);
76
	$id_auteur = sql_insertq("spip_auteurs", $champs);
77
	pipeline('post_insertion',
78
		array(
79
			'args' => array(
80
				'table' => 'spip_auteurs',
81
				'id_objet' => $id_auteur
82
			),
83
			'data' => $champs
84
		)
85
	);
86
	return $id_auteur;
87
}
88
89
90
/**
91
 * Appelle toutes les fonctions de modification d'un auteur
92
 *
93
 * @param int $id_auteur
94
 * @param array $set
0 ignored issues
show
Documentation introduced by
Should the type for parameter $set not be array|null? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
95
 * @param bool $force_update
96
 *   permet de forcer la maj en base des champs fournis, sans passer par instancier
97
 *   utilise par auth/spip
98
 * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|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...
99
 */
100
function auteur_modifier($id_auteur, $set = null, $force_update=false) {
101
102
	include_spip('inc/modifier');
103
	include_spip('inc/filtres');
104
	$c = collecter_requests(
105
		// white list
106
		objet_info('auteur','champs_editables'),
107
		// black list
108
		$force_update?array():array('webmestre','pass','login'),
109
		// donnees eventuellement fournies
110
		$set
111
	);
112
113
	if ($err = objet_modifier_champs('auteur', $id_auteur,
114
		array(
115
			'data' => $set,
116
			'nonvide' => array('nom' => _T('ecrire:item_nouvel_auteur'))
117
		),
118
		$c))
119
		return $err;
120
	$session = $c;
121
122
	$err = '';
123
	if (!$force_update){
124
		// Modification de statut, changement de rubrique ?
125
		$c = collecter_requests(
126
			// white list
127
			array(
128
			 'statut', 'new_login','new_pass','login','pass','webmestre','restreintes','id_parent'
129
			),
130
			// black list
131
			array(),
132
			// donnees eventuellement fournies
133
			$set
134
		);
135
		if (isset($c['new_login']) AND !isset($c['login']))
136
			$c['login'] = $c['new_login'];
137
		if (isset($c['new_pass']) AND !isset($c['pass']))
138
			$c['pass'] = $c['new_pass'];
139
		$err = auteur_instituer($id_auteur, $c);
140
		$session = array_merge($session,$c);
141
	}
142
143
	// .. mettre a jour les sessions de cet auteur
144
	include_spip('inc/session');
145
	$session['id_auteur'] = $id_auteur;
146
	unset($session['new_login']);
147
	unset($session['new_pass']);
148
	actualiser_sessions($session);
149
150
	return $err;
151
}
152
153
/**
154
 * Associer un auteur a des objets listes sous forme
155
 * array($objet=>$id_objets,...)
156
 * $id_objets peut lui meme etre un scalaire ou un tableau pour une liste d'objets du meme type
157
 *
158
 * on peut passer optionnellement une qualification du (des) lien(s) qui sera
159
 * alors appliquee dans la foulee.
160
 * En cas de lot de liens, c'est la meme qualification qui est appliquee a tous
161
 *
162
 * @param int $id_auteur
163
 * @param array $objets
164
 * @param array $qualif
0 ignored issues
show
Documentation introduced by
Should the type for parameter $qualif not be array|null? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
165
 * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|integer|array? Also, consider making the array more specific, something like array<String>, or 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.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
166
 */
167
function auteur_associer($id_auteur,$objets, $qualif = null){
168
	include_spip('action/editer_liens');
169
	return objet_associer(array('auteur'=>$id_auteur), $objets, $qualif);
170
}
171
172
173
/**
174
 * Ancien nommage pour compatibilite
175
 * @param int $id_auteur
176
 * @param array $c
177
 * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|integer|array? Also, consider making the array more specific, something like array<String>, or 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.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
178
 */
179
function auteur_referent($id_auteur,$c){
180
	return auteur_associer($id_auteur,$c);
181
}
182
183
/**
184
 * Dossocier un auteur des objets listes sous forme
185
 * array($objet=>$id_objets,...)
186
 * $id_objets peut lui meme etre un scalaire ou un tableau pour une liste d'objets du meme type
187
 *
188
 * un * pour $id_auteur,$objet,$id_objet permet de traiter par lot
189
 *
190
 * @param int $id_auteur
191
 * @param array $objets
192
 * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|integer|array? Also, consider making the array more specific, something like array<String>, or 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.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
193
 */
194
function auteur_dissocier($id_auteur,$objets){
195
	include_spip('action/editer_liens');
196
	return objet_dissocier(array('auteur'=>$id_auteur), $objets);
197
}
198
199
/**
200
 * Qualifier le lien d'un auteur avec les objets listes
201
 * array($objet=>$id_objets,...)
202
 * $id_objets peut lui meme etre un scalaire ou un tableau pour une liste d'objets du meme type
203
 * exemple :
204
 * $c = array('vu'=>'oui');
205
 * un * pour $id_auteur,$objet,$id_objet permet de traiter par lot
206
 *
207
 * @param int $id_auteur
208
 * @param array $objets
209
 * @param array $qualif
210
 * @return bool|int
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|integer|array? Also, consider making the array more specific, something like array<String>, or 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.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
211
 */
212
function auteur_qualifier($id_auteur,$objets,$qualif){
213
	include_spip('action/editer_liens');
214
	return objet_qualifier_liens(array('auteur'=>$id_auteur), $objets, $qualif);
215
}
216
217
218
/**
219
 * Modifier le statut d'un auteur, ou son login/pass
220
 * http://doc.spip.org/@instituer_auteur
221
 * @param  $id_auteur
222
 * @param  $c
223
 * @param bool $force_webmestre
224
 * @return bool|string
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use false|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...
225
 */
226
function auteur_instituer($id_auteur, $c, $force_webmestre = false) {
227
	if (!$id_auteur=intval($id_auteur))
228
		return false;
229
	$erreurs = array(); // contiendra les differentes erreurs a traduire par _T()
230
	$champs = array();
231
232
	// les memoriser pour les faire passer dans le pipeline pre_edition
233 View Code Duplication
	if (isset($c['login']) AND strlen($c['login']))
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...
234
		$champs['login'] = $c['login'];
235 View Code Duplication
	if (isset($c['pass']) AND strlen($c['pass']))
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...
236
		$champs['pass'] = $c['pass'];
237
238
	$statut =	$statut_ancien = sql_getfetsel('statut','spip_auteurs','id_auteur='.intval($id_auteur));
239
	
240
	if (isset($c['statut'])
241
	  AND (autoriser('modifier', 'auteur', $id_auteur,null, array('statut' => $c['statut']))))
242
		$statut = $champs['statut'] = $c['statut'];
243
244
	// Restreindre avant de declarer l'auteur
245
	// (section critique sur les droits)
246
	if ($c['id_parent']) {
247
		if (is_array($c['restreintes']))
248
			$c['restreintes'][] = $c['id_parent'];
249
		else
250
			$c['restreintes'] = array($c['id_parent']);
251
	}
252
253
	if (isset($c['webmestre'])
254
	  AND ($force_webmestre OR autoriser('modifier', 'auteur', $id_auteur,null, array('webmestre' => '?'))))
255
		$champs['webmestre'] = $c['webmestre']=='oui'?'oui':'non';
256
	
257
	// Envoyer aux plugins
258
	$champs = pipeline('pre_edition',
259
		array(
260
			'args' => array(
261
				'table' => 'spip_auteurs',
262
				'id_objet' => $id_auteur,
263
				'action' => 'instituer',
264
				'statut_ancien' => $statut_ancien,
265
			),
266
			'data' => $champs
267
		)
268
	);
269
	
270
	if (is_array($c['restreintes'])
271
	AND autoriser('modifier', 'auteur', $id_auteur, NULL, array('restreint'=>$c['restreintes']))) {
272
		$rubriques = array_map('intval',$c['restreintes']);
273
		$rubriques = array_unique($rubriques);
274
		$rubriques = array_diff($rubriques,array(0));
275
		auteur_dissocier($id_auteur, array('rubrique'=>'*'));
276
		auteur_associer($id_auteur,array('rubrique'=>$rubriques));
277
	}
278
279
	$flag_ecrire_acces = false;
280
	// commencer par traiter les cas particuliers des logins et pass
281
	// avant les autres ecritures en base
282
	if (isset($champs['login']) OR isset($champs['pass'])){
283
		$auth_methode = sql_getfetsel('source','spip_auteurs','id_auteur='.intval($id_auteur));
284
		include_spip('inc/auth');
285 View Code Duplication
		if (isset($champs['login']) AND strlen($champs['login']))
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...
286
			if (!auth_modifier_login($auth_methode, $champs['login'], $id_auteur))
287
				$erreurs[] = 'ecrire:impossible_modifier_login_auteur';
288
		if (isset($champs['pass']) AND strlen($champs['pass'])){
289
			$champs['login'] = sql_getfetsel('login','spip_auteurs','id_auteur='.intval($id_auteur));
290
			if (!auth_modifier_pass($auth_methode, $champs['login'], $champs['pass'], $id_auteur))
291
				$erreurs[] = 'ecrire:impossible_modifier_pass_auteur';
292
		}
293
		unset($champs['login']);
294
		unset($champs['pass']);
295
		$flag_ecrire_acces = true;
296
	}
297
298
	if (!count($champs)) return implode(' ', array_map('_T', $erreurs));
299
	sql_updateq('spip_auteurs', $champs , 'id_auteur='.$id_auteur);
300
301
	// .. mettre a jour les fichiers .htpasswd et .htpasswd-admin
302
	if ($flag_ecrire_acces
303
	  OR isset($champs['statut'])
304
	  ) {
305
		include_spip('inc/acces');
306
		ecrire_acces();
307
	}
308
309
	// Invalider les caches
310
	include_spip('inc/invalideur');
311
	suivre_invalideur("id='auteur/$id_auteur'");
312
	
313
	// Pipeline
314
	pipeline('post_edition',
315
		array(
316
			'args' => array(
317
				'table' => 'spip_auteurs',
318
				'id_objet' => $id_auteur,
319
				'action' => 'instituer',
320
				'statut_ancien' => $statut_ancien,
321
			),
322
			'data' => $champs
323
		)
324
	);
325
326
327
	// Notifications
328
	if ($notifications = charger_fonction('notifications', 'inc')) {
329
		$notifications('instituerauteur', $id_auteur,
330
			array('statut' => $statut, 'statut_ancien' => $statut_ancien)
331
		);
332
	}
333
334
	return implode(' ', array_map('_T', $erreurs));
335
336
}
337
338
339
340
341
function insert_auteur($source=null) {
342
	return auteur_inserer($source);
343
}
344
function auteurs_set($id_auteur, $set = null) {
345
	return auteur_modifier($id_auteur,$set);
346
}
347
function instituer_auteur($id_auteur, $c, $force_webmestre = false) {
348
	return auteur_instituer($id_auteur,$c,$force_webmestre);
349
}
350
// http://doc.spip.org/@revision_auteur
351
function revision_auteur($id_auteur, $c=false) {
352
	return auteur_modifier($id_auteur,$c);
353
}
354
355
?>
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...
356