[SPIP] ~2.1.12 -->2.1.25
[velocampus/web/www.git] / www / ecrire / inc / actions.php
1 <?php
2
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
5 * *
6 * Copyright (c) 2001-2014 *
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
16 // http://doc.spip.org/@generer_action_auteur
17 function generer_action_auteur($action, $arg, $redirect="", $mode=false, $att='', $public=false)
18 {
19 $securiser_action = charger_fonction('securiser_action', 'inc');
20 return $securiser_action($action, $arg, $redirect, $mode, $att, $public);
21 }
22
23 // http://doc.spip.org/@redirige_action_auteur
24 function redirige_action_auteur($action, $arg, $ret, $gra='', $mode=false, $atts='', $public=false) {
25
26 $r = ($public ? _DIR_RESTREINT_ABS : _DIR_RESTREINT) .
27 generer_url_ecrire($ret, $gra, true, true);
28
29 return generer_action_auteur($action, $arg, $r, $mode, $atts, $public);
30 }
31
32 // http://doc.spip.org/@redirige_action_post
33 function redirige_action_post($action, $arg, $ret, $gra, $corps, $att='') {
34 $r = _DIR_RESTREINT . generer_url_ecrire($ret, $gra, false, true);
35 return generer_action_auteur($action, $arg, $r, $corps, $att . " method='post'");
36 }
37
38 // Retourne un formulaire d'execution de $action sur $id,
39 // revenant a l'envoyeur $script d'arguments $args.
40 // Utilise Ajax si dispo, en ecrivant le resultat dans le innerHTML du noeud
41 // d'attribut id = $action-$id (cf. AjaxSqueeze dans layer.js)
42
43 // http://doc.spip.org/@ajax_action_auteur
44 function ajax_action_auteur($action, $id, $script, $args='', $corps=false, $args_ajax='', $fct_ajax='')
45 {
46 if (strpos($args,"#")===FALSE)
47 $ancre = "$action-" . intval($id);
48 else {
49 $ancre = explode("#",$args);
50 $args = $ancre[0];
51 $ancre = $ancre[1];
52 }
53
54 // Formulaire (POST)
55 // methodes traditionnelle et ajax a unifier...
56 if (is_string($corps)) {
57
58 // Methode traditionnelle
59 if (_SPIP_AJAX !== 1) {
60 return redirige_action_post($action,
61 $id,
62 $script,
63 "$args#$ancre",
64 $corps);
65 }
66
67 // Methode Ajax
68 else {
69 if ($args AND !$args_ajax) $args_ajax = "&$args";
70 if (isset($_GET['var_profile']))
71 $args_ajax .= '&var_profile=1';
72 return redirige_action_post($action,
73 $id,
74 $action,
75 "script=$script$args_ajax",
76 $corps,
77 (" onsubmit="
78 . ajax_action_declencheur('this', $ancre, $fct_ajax)));
79
80 }
81 }
82
83 // Lien (GET)
84 else {
85 $href = redirige_action_auteur($action,
86 $id,
87 $script,
88 "$args#$ancre",
89 false);
90
91 if ($args AND !$args_ajax) $args_ajax = "&$args";
92 if (isset($_GET['var_profile']))
93 $args_ajax .= '&var_profile=1';
94
95 $ajax = redirige_action_auteur($action,
96 $id,
97 $action,
98 "script=$script$args_ajax");
99
100 $cli = array_shift($corps);
101 return "<a href='$href'\nonclick="
102 . ajax_action_declencheur($ajax, $ancre, $fct_ajax)
103 . ">"
104 . (!$corps ? $cli : ("\n<span" . $corps[0] . ">$cli</span>"))
105 . "</a>";
106 }
107 }
108
109 // Comme ci-dessus, mais reduit au cas POST et on fournit le bouton Submit.
110 //
111 // http://doc.spip.org/@ajax_action_post
112 function ajax_action_post($action, $arg, $retour, $gra, $corps, $clic='', $atts_i='', $atts_span = "", $args_ajax='', $fct_ajax='')
113 {
114 global $spip_lang_right;
115
116 if (strpos($gra,"#")===FALSE) {
117 // A etudier: prendre systematiquement arg en trancodant les \W
118 $n = intval($arg);
119 $ancre = "$action-" . ($n ? $n : $arg);
120 } else {
121 $ancre = explode("#",$gra);
122 $args = $ancre[0];
123 $ancre = $ancre[1];
124 }
125
126 if (!$atts_i)
127 $atts_i = " style='float: $spip_lang_right'";
128
129 if (is_array($clic)) {
130 $submit = "";
131 $atts_i .= "\nonclick='AjaxNamedSubmit(this)'";
132 foreach($clic as $n => $c)
133 $submit .= "\n<input type='submit' name='$n' value='$c' $atts_i />";
134 } else {
135 if (!$clic) $clic = _T('bouton_valider');
136 $submit = "<input type='submit' value='$clic' $atts_i />";
137 }
138 $corps = "<div>"
139 . $corps
140 . "<span"
141 . $atts_span
142 . ">"
143 . $submit
144 . "</span></div>";
145
146 if (_SPIP_AJAX !== 1) {
147 return redirige_action_post($action, $arg, $retour,
148 ($gra . '#' . $ancre),
149 $corps);
150 } else {
151
152 if ($gra AND !$args_ajax) $args_ajax = "&$gra";
153 if (isset($GLOBALS['var_profile']))
154 $args_ajax .= '&var_profile=1';
155
156 $js = ajax_action_declencheur('this', $ancre, $fct_ajax);
157 return redirige_action_post($action,
158 $arg,
159 $action,
160 "script=$retour$args_ajax",
161 $corps,
162 " onsubmit=$js");
163 }
164 }
165
166 //
167 // Attention pour que Safari puisse manipuler cet evenement
168 // il faut onsubmit="return AjaxSqueeze(x,'truc',...)"
169 // et non pas onsubmit='return AjaxSqueeze(x,"truc",...)'
170 //
171 // http://doc.spip.org/@ajax_action_declencheur
172 function ajax_action_declencheur($request, $noeud, $fct_ajax='') {
173 if (strpos($request, 'this') !== 0)
174 $request = "'".$request."'";
175
176 return '"return AjaxSqueeze('
177 . $request
178 . ",'"
179 . $noeud
180 . "',"
181 . ($fct_ajax ? $fct_ajax : "''")
182 . ',event)"';
183 }
184
185 // Place un element HTML dans une div nommee,
186 // sauf si c'est un appel Ajax car alors la div y est deja
187 // $fonction : denomination semantique du bloc, que l'on retouve en attribut class
188 // $id : id de l'objet concerne si il y a lieu ou "", sert a construire un identifiant unique au bloc ("fonction-id")
189 // http://doc.spip.org/@ajax_action_greffe
190 function ajax_action_greffe($fonction, $id, $corps)
191 {
192 $idom = $fonction.(strlen($id)?"-$id":"");
193 return _AJAX
194 ? "$corps"
195 : "\n<div id='$idom' class='ajax-action $fonction'>$corps\n</div>\n";
196 }
197
198 // http://doc.spip.org/@ajax_retour
199 function ajax_retour($corps, $xml = true)
200 {
201 if (isset($_COOKIE['spip_admin']) AND $GLOBALS['tableau_des_temps'])
202 erreur_squelette();
203 else {
204 if ($GLOBALS['exec'] == 'valider_xml') {
205 $debut = _DOCTYPE_ECRIRE
206 . "<html><head><title>Debug Spip Ajax</title></head>"
207 . "<body><div>\n\n"
208 . "<!-- %%%%%%%%%%%%%%%%%%% Ajax %%%%%%%%%%%%%%%%%%% -->\n";
209
210 $fin = '</div></body></html>';
211
212 } else {
213 $c = $GLOBALS['meta']["charset"];
214 header('Content-Type: text/html; charset='. $c);
215 $debut = (($xml AND strlen(trim($corps)))?'<' . "?xml version='1.0' encoding='" . $c . "'?" . ">\n":'');
216 $fin = '';
217 }
218 echo $debut, $corps, $fin;
219 }
220 }
221
222 // http://doc.spip.org/@determine_upload
223 function determine_upload($type='') {
224 include_spip('inc/autoriser');
225
226 if (!autoriser('chargerftp')
227 OR $type == 'logos') # on ne le permet pas pour les logos
228 return false;
229
230 $repertoire = _DIR_TRANSFERT;
231 if (!@is_dir($repertoire)) {
232 $repertoire = str_replace(_DIR_TMP, '', $repertoire);
233 $repertoire = sous_repertoire(_DIR_TMP, $repertoire);
234 }
235
236 if (!$GLOBALS['visiteur_session']['restreint'])
237 return $repertoire;
238 else
239 return sous_repertoire($repertoire, $GLOBALS['visiteur_session']['login']);
240 }
241 ?>