Completed
Push — master ( d64163...2218f7 )
by cam
04:20
created

actions.php ➔ ajax_retour()   B

Complexity

Conditions 11
Paths 24

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
nc 24
nop 2
dl 0
loc 24
rs 7.3166
c 0
b 0
f 0

How to fix   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, Système de publication pour l'internet                           *
5
 *                                                                         *
6
 *  Copyright © avec tendresse depuis 2001                                 *
7
 *  Arnaud Martin, Antoine Pitrou, Philippe Rivière, Emmanuel Saint-James  *
8
 *                                                                         *
9
 *  Ce programme est un logiciel libre distribué sous licence GNU/GPL.     *
10
 *  Pour plus de détails voir le fichier COPYING.txt ou l'aide en ligne.   *
11
\***************************************************************************/
12
13
/**
14
 * Gestion des actions sécurisées
15
 *
16
 * @package SPIP\Core\Actions
17
 **/
18
19
if (!defined('_ECRIRE_INC_VERSION')) {
20
	return;
21
}
22
23
/**
24
 * Retourne une URL ou un formulaire securisé
25
 *
26
 * @uses inc_securiser_action_dist()
27
 *
28
 * @param string $action
29
 *     Nom du fichier/action appelé (dans le répertoire action)
30
 * @param string $arg
31
 *     Arguments pour l'action sécurisée, peut etre vide
32
 * @param string $redirect
33
 *     Adresse de redirection souhaitée à la fin du bon déroulement de l’action
34
 * @param bool|int|string $mode
35
 *     - -1 : renvoyer action, arg et hash sous forme de array()
36
 *     - true ou false : renvoyer une url, avec `&amp;` (false) ou `&` (true)
37
 *     - string : renvoyer un formulaire
38
 * @param string|int $att
39
 *     - id_auteur pour lequel générer l'action en mode url ou array()
40
 *     - attributs du formulaire en mode formulaire
41
 * @param bool $public
42
 * @return array|string
43
 *     URL, code HTML du formulaire ou tableau (action, arg, hash)
44
 */
45
function generer_action_auteur($action, $arg = '', $redirect = '', $mode = false, $att = '', $public = false) {
46
	$securiser_action = charger_fonction('securiser_action', 'inc');
47
48
	return $securiser_action($action, $arg, $redirect, $mode, $att, $public);
49
}
50
51
/**
52
 * Génère une URL ou un formulaire dirigé vers un fichier action (action/xx.php)
53
 *
54
 * Le génère à condition que $mode="texte".
55
 *
56
 * @uses generer_action_auteur()
57
 *
58
 * @api
59
 * @param string $action
60
 *     Nom du fichier action/xx.php
61
 * @param string $arg
62
 *     Argument passé à l'action, qui sera récupéré par la fonction
63
 *     `securiser_action()`
64
 * @param string $ret
65
 *     Nom du script exec sur lequel on revient après l'action (redirection),
66
 *     que l'on peut récupérer dans une fonction d'action par `_request('redirect')`
67
 * @param string $gra
68
 *     Arguments transmis au script exec de retour `arg1=yy&arg2=zz`
69
 * @param bool|string|int $mode
70
 *     - -1 : renvoyer action, arg et hash sous forme de array()
71
 *     - true ou false : renvoyer une url, avec `&amp;` (false) ou `&` (true)
72
 *     - string : renvoyer un formulaire
73
 * @param string $atts ?
74
 * @param bool $public
75
 *     true produit une URL d'espace public
76
 *     false (par défaut) produit une URL d'espace privé
77
 * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be array|string? 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...
78
 *     Code HTML du formulaire
79
 */
80
function redirige_action_auteur($action, $arg, $ret, $gra = '', $mode = false, $atts = '', $public = false) {
81
	$r = ($public ? _DIR_RESTREINT_ABS : _DIR_RESTREINT) . generer_url_ecrire($ret, $gra, true, true);
82
83
	return generer_action_auteur($action, $arg, $r, $mode, $atts, $public);
84
}
85
86
/**
87
 * Retourne une URL ou un formulaire sécurisé en méthode POST
88
 *
89
 * @param string $action
90
 *     Nom du fichier/action appelé (dans le répertoire action)
91
 * @param string $arg
92
 *     Arguments pour l'action sécurisée
93
 * @param string $ret
94
 *     Adresse de redirection souhaitée à la fin du bon déroulement de l’action
95
 * @param string $gra
96
 *     Arguments à transmettre, tel que `arg1=yy&arg2=zz`
97
 * @param bool|int|string $corps
98
 *     - -1 : renvoyer action, arg et hash sous forme de array()
99
 *     - true ou false : renvoyer une url, avec `&amp;` (false) ou `&` (true)
100
 *     - string : renvoyer un formulaire
101
 * @param string|int $att
102
 *     - id_auteur pour lequel générer l'action en mode url ou array()
103
 *     - attributs du formulaire en mode formulaire
104
 * @return array|string
105
 *     URL, code HTML du formulaire ou tableau (action, arg, hash)
106
 */
107
function redirige_action_post($action, $arg, $ret, $gra, $corps, $att = '') {
108
	$r = _DIR_RESTREINT . generer_url_ecrire($ret, $gra, false, true);
109
110
	return generer_action_auteur($action, $arg, $r, $corps, $att . " method='post'");
111
}
112
113
114
/**
115
 * Fonction de formatage du contenu renvoyé en ajax
116
 *
117
 * @param string $corps
118
 * @param string $content_type
0 ignored issues
show
Documentation introduced by
Should the type for parameter $content_type 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...
119
 *   permet de definir le type de contenu renvoye.
120
 *   Si rien de précisé, ou si true c'est "text/html" avec un entete xml en plus.
121
 *   La valeur speciale false fournit text/html sans entete xml. Elle equivaut a
122
 *   passer "text/html" comme $content_type
123
 */
124
function ajax_retour($corps, $content_type = null) {
125
	$xml = false;
126
	if (is_null($content_type) or $content_type === true) {
127
		$xml = true;
128
		$content_type = 'text/html';
129
	} elseif (!$content_type or !is_string($content_type) or strpos($content_type, '/') === false) {
130
		$content_type = 'text/html';
131
	}
132
133
	$e = '';
134
	if (isset($_COOKIE['spip_admin'])
135
		and ((_request('var_mode') == 'debug') or !empty($GLOBALS['tableau_des_temps']))
136
	) {
137
		$e = erreur_squelette();
138
	}
139
140
	$c = $GLOBALS['meta']['charset'];
141
	header('Content-Type: ' . $content_type . '; charset=' . $c);
142
	$debut = (($xml and strlen(trim($corps))) ? '<' . "?xml version='1.0' encoding='" . $c . "'?" . ">\n" : '');
143
	$fin = '';
144
145
	echo $debut, $corps, $fin, $e;
146
147
}
148