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