1 | <?php |
---|
2 | /* |
---|
3 | * forms |
---|
4 | * Gestion de formulaires editables dynamiques |
---|
5 | * |
---|
6 | * Auteurs : |
---|
7 | * Antoine Pitrou |
---|
8 | * Cedric Morin |
---|
9 | * Renato |
---|
10 | * 2005,2006 - Distribue sous licence GNU/GPL |
---|
11 | * |
---|
12 | */ |
---|
13 | |
---|
14 | if ($GLOBALS['spip_version_code']<1.92) |
---|
15 | include_spip('inc/forms_compat_191'); |
---|
16 | |
---|
17 | function Forms_install(){ |
---|
18 | include_spip('base/forms_upgrade'); |
---|
19 | Forms_upgrade(); |
---|
20 | } |
---|
21 | |
---|
22 | function Forms_uninstall(){ |
---|
23 | include_spip('base/forms'); |
---|
24 | include_spip('base/abstract_sql'); |
---|
25 | } |
---|
26 | |
---|
27 | function Forms_structure($id_form){ |
---|
28 | // Preparer la table de traduction code->valeur & mise en table de la structure pour eviter des requettes |
---|
29 | // a chaque ligne |
---|
30 | $structure = array(); |
---|
31 | $res = spip_query("SELECT * FROM spip_forms_champs WHERE id_form="._q($id_form)." ORDER BY rang"); |
---|
32 | while ($row = spip_fetch_array($res)){ |
---|
33 | $type = $row['type']; |
---|
34 | $champ = $row['champ']; |
---|
35 | foreach ($row as $k=>$v) |
---|
36 | $structure[$champ][$k] = $v; |
---|
37 | if (($type == 'select') OR ($type == 'multiple')){ |
---|
38 | $res2 = spip_query("SELECT * FROM spip_forms_champs_choix WHERE id_form="._q($id_form)." AND champ="._q($champ)." ORDER BY rang"); |
---|
39 | while ($row2 = spip_fetch_array($res2)){ |
---|
40 | $structure[$champ]['choix'][$row2['choix']] = $c = trim(textebrut(typo($row2['titre']))); |
---|
41 | $structure[$champ]['choixrev'][$c] = $row2['choix']; |
---|
42 | } |
---|
43 | } |
---|
44 | else if ($type == 'mot') { |
---|
45 | $id_groupe = intval($row['extra_info']); |
---|
46 | $res2 = spip_query("SELECT id_mot, titre FROM spip_mots WHERE id_groupe="._q($id_groupe)); |
---|
47 | while ($row2 = spip_fetch_array($res2)) { |
---|
48 | $structure[$champ]['choix'][$row2['id_mot']] = $c = trim(textebrut(typo($row2['titre']))); |
---|
49 | $structure[$champ]['choixrev'][$c] = $row2['id_mot']; |
---|
50 | } |
---|
51 | } |
---|
52 | } |
---|
53 | return $structure; |
---|
54 | } |
---|
55 | function Forms_valeurs($id_form,$id_donnee){ |
---|
56 | static $unseul = array(); |
---|
57 | $valeurs = array(); |
---|
58 | $res = spip_query("SELECT * FROM spip_forms_donnees_champs AS d JOIN spip_forms_champs AS c ON c.champ=d.champ AND c.id_form="._q($id_form)." WHERE id_donnee="._q($id_donnee)); |
---|
59 | while ($row = spip_fetch_array($res)){ |
---|
60 | if ($row['type']=='multiple') |
---|
61 | $valeurs[$row['champ']][]= $row['valeur']; |
---|
62 | elseif ($row['type']=='mot'){ |
---|
63 | $id_groupe = intval($row['extra_info']); |
---|
64 | if (!isset($unseul[$id_groupe])){ |
---|
65 | $res2 = spip_query("SELECT unseul FROM spip_groupes_mots WHERE id_groupe="._q($id_groupe)); |
---|
66 | $row2=spip_fetch_array($res2); |
---|
67 | $unseul[$id_groupe] = $row2['unseul']; |
---|
68 | } |
---|
69 | if ($unseul[$id_groupe]=='oui') |
---|
70 | $valeurs[$row['champ']]= $row['valeur']; |
---|
71 | else |
---|
72 | $valeurs[$row['champ']][]= $row['valeur']; |
---|
73 | } |
---|
74 | else |
---|
75 | $valeurs[$row['champ']]= $row['valeur']; |
---|
76 | } |
---|
77 | return $valeurs; |
---|
78 | } |
---|
79 | |
---|
80 | function Forms_donnees_vide($id_form){ |
---|
81 | if (!include_spip('inc/autoriser')) |
---|
82 | include_spip('inc/autoriser_compat'); |
---|
83 | if (autoriser('supprimerdonnee','form',$id_form)){ |
---|
84 | spip_query("UPDATE spip_forms_donnees SET statut='poubelle' WHERE id_form="._q($id_form)); |
---|
85 | /*$res = spip_query("SELECT id_donnee FROM spip_forms_donnees WHERE id_form="._q($id_form)); |
---|
86 | while ($row = spip_fetch_array($res)){ |
---|
87 | spip_query("DELETE FROM spip_forms_donnees_champs WHERE id_donnee="._q($row['id_donnee'])); |
---|
88 | } |
---|
89 | spip_query("DELETE FROM spip_forms_donnees WHERE id_form="._q($id_form));*/ |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | function Forms_csvimport_ajoute_table_csv($data, $id_form, $assoc_field, &$erreur, $simu = false){ |
---|
94 | include_spip('inc/forms_type_champs'); |
---|
95 | $assoc = array_flip($assoc_field); |
---|
96 | $res = spip_query("SELECT * FROM spip_forms WHERE id_form="._q($id_form)." AND type_form NOT IN ('','sondage')"); |
---|
97 | if (!$row = spip_fetch_array($res)) { |
---|
98 | $erreur[0][] = _L("Table introuvable"); |
---|
99 | return; |
---|
100 | } |
---|
101 | |
---|
102 | $structure = Forms_structure($id_form); |
---|
103 | $cle = isset($assoc_field['id_donnee'])?$assoc_field['id_donnee']:false; |
---|
104 | |
---|
105 | $output = ""; |
---|
106 | if ($data!=false){ |
---|
107 | $count_lignes = 0; |
---|
108 | foreach($data as $key=>$ligne) { |
---|
109 | $count_lignes ++; |
---|
110 | // creation de la donnee si necessaire |
---|
111 | $creation = true; |
---|
112 | $id_donnee = 0; |
---|
113 | // verifier la validite de l'import |
---|
114 | $c = array(); |
---|
115 | foreach($structure as $champ=>$infos){ |
---|
116 | if ($infos['type'] != 'multiple'){ |
---|
117 | $c[$champ] = ""; |
---|
118 | if ((isset($assoc[$champ]))&&(isset($ligne[$assoc[$champ]]))){ |
---|
119 | $c[$champ] = $ligne[$assoc[$champ]]; |
---|
120 | if (isset($infos['choix']) && !isset($infos['choix'][$c[$champ]]) && isset($infos['choixrev'][$c[$champ]])) |
---|
121 | $c[$champ] = $infos['choixrev'][$c[$champ]]; |
---|
122 | } |
---|
123 | } |
---|
124 | else { |
---|
125 | $c[$champ] = array(); |
---|
126 | foreach($infos['choix'] as $choix=>$t) |
---|
127 | if ((isset($assoc[$choix]))&&(isset($ligne[$assoc[$choix]]))) |
---|
128 | if (strlen($ligne[$assoc[$choix]])) |
---|
129 | $c[$champ][] = $choix; |
---|
130 | } |
---|
131 | } |
---|
132 | $err = Forms_valide_champs_reponse_post($id_auteur, $c , $structure); |
---|
133 | if (is_array($err) && count($err)) $erreur[$count_lignes] = $err; |
---|
134 | else if (!$simu) { |
---|
135 | if ($cle) { |
---|
136 | $id_donnee = $ligne[$cle]; |
---|
137 | $res = spip_query("SELECT * FROM spip_forms_donnees WHERE id_donnee="._q($id_donnee)." AND id_form="._q($id_form)); |
---|
138 | if ($row = spip_fetch_array($res)){ |
---|
139 | $creation = false; |
---|
140 | $set = ""; |
---|
141 | foreach(array('date','url','ip','id_auteur') as $champ) |
---|
142 | if (isset($assoc_field['$champ'])) $set .= "$champ="._q($ligne[$assoc_field['date']]).", "; |
---|
143 | $set.=" maj=NOW()"; |
---|
144 | spip_query("UPDATE spip_forms_donnees $set WHERE id_donnee="._q($id_donnee)." AND id_form="._q($id_form)); |
---|
145 | } |
---|
146 | } |
---|
147 | if ($creation){ |
---|
148 | $id_auteur = $GLOBALS['auteur_session'] ? intval($GLOBALS['auteur_session']['id_auteur']) : 0; |
---|
149 | $ip = $GLOBALS['REMOTE_ADDR']; |
---|
150 | $url = _DIR_RESTREINT_ABS; |
---|
151 | if ($cle){ |
---|
152 | if (intval($id_donnee)) |
---|
153 | spip_abstract_insert("spip_forms_donnees","(id_donnee,id_form,date,ip,id_auteur,url,confirmation,statut,maj)","("._q($id_donnee).","._q($id_form).", NOW(),"._q($ip).","._q($id_auteur).","._q($url).", 'valide', 'publie', NOW() )"); |
---|
154 | } |
---|
155 | else |
---|
156 | spip_abstract_insert("spip_forms_donnees","(id_form,date,ip,id_auteur,url,confirmation,statut,maj)","("._q($id_form).", NOW(),"._q($ip).","._q($id_auteur).","._q($url).", 'valide', 'publie', NOW() )"); |
---|
157 | $id_donnee = spip_insert_id(); |
---|
158 | } |
---|
159 | if ($id_donnee){ |
---|
160 | foreach($c as $champ=>$values){ |
---|
161 | if (!$creation) |
---|
162 | spip_query("DELETE FROM spip_forms_donnees_champs WHERE id_donnee="._q($id_donnee)." AND champ="._q($champ)); |
---|
163 | if (!is_array($values)) $values = array($values); |
---|
164 | foreach($values as $v) |
---|
165 | if (strlen($v)) |
---|
166 | spip_query("INSERT INTO spip_forms_donnees_champs (id_donnee,champ,valeur,maj) VALUES ("._q($id_donnee).","._q($champ).","._q($v).", NOW() )"); |
---|
167 | } |
---|
168 | } |
---|
169 | else |
---|
170 | $erreur[$count_lignes][] = "ajout impossible ::id_donnee nul::<br />"; |
---|
171 | } |
---|
172 | } |
---|
173 | } |
---|
174 | } |
---|
175 | |
---|
176 | function Forms_deplacer_fichier_form($source, $dest) { |
---|
177 | include_spip('inc/getdocument'); |
---|
178 | if ($ok = deplacer_fichier_upload($source, $dest, true)) |
---|
179 | if (file_exists($source)) // argument move pas pris en compte avant spip 1.9.2 |
---|
180 | @unlink($source); |
---|
181 | |
---|
182 | return $ok; |
---|
183 | } |
---|
184 | |
---|
185 | function Forms_nommer_fichier_form($orig, $dir) { |
---|
186 | include_spip("inc/charsets"); |
---|
187 | include_spip("inc/filtres"); |
---|
188 | if (ereg("^(.*)\.([^.]+)$", $orig, $match)) { |
---|
189 | $ext = strtolower($match[2]); |
---|
190 | $orig = $match[1]; |
---|
191 | } |
---|
192 | $base = ereg_replace("[^.a-zA-Z0-9_=-]+", "_", |
---|
193 | translitteration(supprimer_tags(basename($orig)))); |
---|
194 | $n = 0; |
---|
195 | $fichier = $base.'.'.$ext; |
---|
196 | while (@file_exists($dir . $fichier)) { |
---|
197 | $fichier = $base.'-'.(++$n).'.'.$ext; |
---|
198 | } |
---|
199 | return $fichier; |
---|
200 | } |
---|
201 | |
---|
202 | function Forms_type_fichier_autorise($nom_fichier) { |
---|
203 | if (ereg("\.([^.]+)$", $nom_fichier, $match)) { |
---|
204 | $ext = addslashes(strtolower($match[1])); |
---|
205 | switch ($ext) { |
---|
206 | case 'htm': |
---|
207 | $ext = 'html'; |
---|
208 | break; |
---|
209 | case 'jpeg': |
---|
210 | $ext = 'jpg'; |
---|
211 | break; |
---|
212 | case 'tiff': |
---|
213 | $ext = 'tif'; |
---|
214 | break; |
---|
215 | } |
---|
216 | $query = "SELECT * FROM spip_types_documents WHERE extension='$ext' AND upload='oui'"; |
---|
217 | $result = spip_query($query); |
---|
218 | return (spip_num_rows($result) > 0); |
---|
219 | } |
---|
220 | return false; |
---|
221 | } |
---|
222 | |
---|
223 | // Fonction utilitaires |
---|
224 | function Forms_nom_cookie_form($id_form) { |
---|
225 | return $GLOBALS['cookie_prefix'].'cookie_form_'.$id_form; |
---|
226 | } |
---|
227 | |
---|
228 | function Forms_verif_cookie_sondage_utilise($id_form) { |
---|
229 | global $auteur_session; |
---|
230 | $id_auteur = $auteur_session ? intval($auteur_session['id_auteur']) : 0; |
---|
231 | $cookie = $_COOKIE[Forms_nom_cookie_form($id_form)]; |
---|
232 | $q="SELECT id_donnee FROM spip_forms_donnees " . |
---|
233 | "WHERE statut='publie' AND id_form=".intval($id_form)." "; |
---|
234 | if ($cookie) $q.="AND (cookie="._q($cookie)." OR id_auteur="._q($id_auteur).")"; |
---|
235 | else |
---|
236 | if ($id_auteur) |
---|
237 | $q.="AND id_auteur=".$id_auteur; |
---|
238 | else |
---|
239 | return false; |
---|
240 | //On retourne les donnees si auteur ou cookie |
---|
241 | $res = spip_query($q); |
---|
242 | return (spip_num_rows($res)>0); |
---|
243 | } |
---|
244 | |
---|
245 | function Forms_extraire_reponse($id_donnee){ |
---|
246 | // Lire les valeurs entrees |
---|
247 | $result = spip_query("SELECT * FROM spip_forms_donnees_champs AS r |
---|
248 | JOIN spip_forms_champs AS ch ON ch.champ=r.champ |
---|
249 | JOIN spip_forms_donnees AS d ON d.id_donnee = r.id_donnee |
---|
250 | WHERE d.id_form = ch.id_form AND r.id_donnee="._q($id_donnee)." ORDER BY ch.rang"); |
---|
251 | $valeurs = array(); |
---|
252 | $retour = urlencode(self()); |
---|
253 | $libelles = array(); |
---|
254 | $values = array(); |
---|
255 | $url = array(); |
---|
256 | while ($row = spip_fetch_array($result)) { |
---|
257 | $rang = $row['rang']; |
---|
258 | $champ = $row['champ']; |
---|
259 | $libelles[$champ]=$row['titre']; |
---|
260 | $type = $row['type']; |
---|
261 | if ($type == 'fichier') { |
---|
262 | $values[$champ][] = $row['valeur']; |
---|
263 | $url[$champ][] = generer_url_ecrire("forms_telecharger","id_donnee=$id_donnee&champ=$champ&retour=$retour"); |
---|
264 | } |
---|
265 | else if (in_array($type,array('select','multiple'))) { |
---|
266 | if ($row3=spip_fetch_array(spip_query("SELECT titre FROM spip_forms_champs_choix WHERE id_form="._q($row['id_form'])." AND champ="._q($champ)." AND choix="._q($row['valeur'])))) |
---|
267 | $values[$champ][]=$row3['titre']; |
---|
268 | else |
---|
269 | $values[$champ][]= $row['valeur']; |
---|
270 | $url[$champ][] = ''; |
---|
271 | } |
---|
272 | else if ($type == 'mot') { |
---|
273 | $id_groupe = intval($row['extra_info']); |
---|
274 | $id_mot = intval($row['valeur']); |
---|
275 | if ($row3 = spip_fetch_array(spip_query("SELECT id_mot, titre FROM spip_mots WHERE id_groupe="._q($id_groupe)." AND id_mot="._q($id_mot)))){ |
---|
276 | $values[$champ][]=$row3['titre']; |
---|
277 | $url[$champ][]= generer_url_ecrire("mots_edit","id_mot=$id_mot"); |
---|
278 | } |
---|
279 | else { |
---|
280 | $values[$champ][]= $row['valeur']; |
---|
281 | $url[$champ][] = ''; |
---|
282 | } |
---|
283 | } |
---|
284 | else { |
---|
285 | $values[$champ][] = $row['valeur']; |
---|
286 | $url[$champ][] = ''; |
---|
287 | } |
---|
288 | } |
---|
289 | return array($libelles,$values,$url); |
---|
290 | } |
---|
291 | |
---|
292 | // |
---|
293 | // Afficher un pave formulaires dans la colonne de gauche |
---|
294 | // (edition des articles) |
---|
295 | |
---|
296 | function Forms_afficher_insertion_formulaire($id_article) { |
---|
297 | global $connect_id_auteur, $connect_statut; |
---|
298 | global $couleur_foncee, $couleur_claire, $options; |
---|
299 | global $spip_lang_left, $spip_lang_right; |
---|
300 | |
---|
301 | $s = ""; |
---|
302 | // Ajouter un formulaire |
---|
303 | $s .= "\n<p>"; |
---|
304 | $s .= debut_cadre_relief("../"._DIR_PLUGIN_FORMS."img_pack/form-24.png", true); |
---|
305 | |
---|
306 | $s .= "<div style='padding: 2px; background-color: $couleur_claire; text-align: center; color: black;'>"; |
---|
307 | $s .= bouton_block_invisible("ajouter_form"); |
---|
308 | $s .= "<strong class='verdana3' style='text-transform: uppercase;'>" |
---|
309 | ._T("forms:article_inserer_un_formulaire")."</strong>"; |
---|
310 | $s .= "</div>\n"; |
---|
311 | |
---|
312 | $s .= debut_block_invisible("ajouter_form"); |
---|
313 | $s .= "<div class='verdana2'>"; |
---|
314 | $s .= _T("forms:article_inserer_un_formulaire_detail"); |
---|
315 | $s .= "</div>"; |
---|
316 | |
---|
317 | $query = "SELECT id_form, titre FROM spip_forms ORDER BY titre"; |
---|
318 | $result = spip_query($query); |
---|
319 | if (spip_num_rows($result)) { |
---|
320 | $s .= "<br />\n"; |
---|
321 | $s .= "<div class='bandeau_rubriques' style='z-index: 1;'>"; |
---|
322 | $s .= "<div class='plan-articles'>"; |
---|
323 | while ($row = spip_fetch_array($result)) { |
---|
324 | $id_form = $row['id_form']; |
---|
325 | $titre = typo($row['titre']); |
---|
326 | |
---|
327 | $link = generer_url_ecrire('forms_edit',"id_form=$id_form&retour=".urlencode(self())); |
---|
328 | $s .= "<a href='".$link."'>"; |
---|
329 | $s .= $titre."</a>\n"; |
---|
330 | $s .= "<div class='arial1' style='text-align:$spip_lang_right;color: black; padding-$spip_lang_left: 4px;' "."title=\""._T("forms:article_recopier_raccourci")."\">"; |
---|
331 | $s .= "<strong><form".$id_form."></strong>"; |
---|
332 | $s .= "</div>"; |
---|
333 | } |
---|
334 | $s .= "</div>"; |
---|
335 | $s .= "</div>"; |
---|
336 | } |
---|
337 | |
---|
338 | // Creer un formulaire |
---|
339 | if (!include_spip('inc/autoriser')) |
---|
340 | include_spip('inc/autoriser_compat'); |
---|
341 | if (autoriser('creer','form')) { |
---|
342 | $s .= "\n<br />"; |
---|
343 | $link = generer_url_ecrire('forms_edit',"new=oui&retour=".urlencode(self())); |
---|
344 | $s .= icone_horizontale(_T("forms:icone_creer_formulaire"), |
---|
345 | $link, "../"._DIR_PLUGIN_FORMS."img_pack/form-24.png", "creer.gif", false); |
---|
346 | } |
---|
347 | |
---|
348 | $s .= fin_block(); |
---|
349 | |
---|
350 | $s .= fin_cadre_relief(true); |
---|
351 | return $s; |
---|
352 | } |
---|
353 | |
---|
354 | function Forms_insertions_reponse_un_champ($id_form,$id_donnee,$champ,$type,$val,&$erreur,&$ok){ |
---|
355 | $inserts = array(); |
---|
356 | if ($type == 'fichier') { |
---|
357 | if (($val = $_FILES[$champ]) AND ($val['tmp_name'])) { |
---|
358 | // Fichier telecharge : deplacer dans IMG, stocker le chemin dans la base |
---|
359 | $dir = sous_repertoire(_DIR_IMG, "protege"); |
---|
360 | $dir = sous_repertoire($dir, "form".$id_form); |
---|
361 | $source = $val['tmp_name']; |
---|
362 | $dest = $dir.Forms_nommer_fichier_form($val['name'], $dir); |
---|
363 | if (!Forms_deplacer_fichier_form($source, $dest)) { |
---|
364 | $erreur[$champ] = _T("forms:probleme_technique_upload"); |
---|
365 | $ok = false; |
---|
366 | } |
---|
367 | else { |
---|
368 | $inserts[] = "("._q($id_donnee).","._q($champ).","._q($dest).")"; |
---|
369 | } |
---|
370 | } |
---|
371 | } |
---|
372 | else if ($val) { |
---|
373 | // Choix multiples : enregistrer chaque valeur separement |
---|
374 | if (is_array($val)) |
---|
375 | foreach ($val as $v) |
---|
376 | $inserts[] = "("._q($id_donnee).","._q($champ).","._q($v).")"; |
---|
377 | else |
---|
378 | $inserts[] = "("._q($id_donnee).","._q($champ).","._q($val).")"; |
---|
379 | } |
---|
380 | return $inserts; |
---|
381 | } |
---|
382 | |
---|
383 | function Forms_insertions_reponse_post($id_form,$id_donnee,&$erreur,&$ok, $c = NULL){ |
---|
384 | $inserts = array(); |
---|
385 | $res = spip_query("SELECT * FROM spip_forms_champs WHERE id_form="._q($id_form)); |
---|
386 | while($row = spip_fetch_array($res)){ |
---|
387 | $champ = $row['champ']; |
---|
388 | $type = $row['type']; |
---|
389 | if (!$c) |
---|
390 | $val = _request($champ); |
---|
391 | else |
---|
392 | $val = isset($c[$champ])?$c[$champ]:NULL; |
---|
393 | $ins = Forms_insertions_reponse_un_champ($id_form,$id_donnee,$champ,$type,$val,$erreur,$ok); |
---|
394 | $inserts = array_merge($inserts,$ins); |
---|
395 | } |
---|
396 | return $inserts; |
---|
397 | } |
---|
398 | |
---|
399 | function Forms_revision_donnee($id_donnee, $c = NULL) { |
---|
400 | include_spip('base/abstract_sql'); |
---|
401 | $inserts = array(); |
---|
402 | $result = spip_query("SELECT id_form FROM spip_forms_donnees WHERE id_donnee="._q($id_donnee)); |
---|
403 | if (!$row = spip_fetch_array($result)) { |
---|
404 | $erreur['@'] = _T("forms:probleme_technique"); |
---|
405 | } |
---|
406 | $id_form = $row['id_form']; |
---|
407 | $structure = Forms_structure($id_form); |
---|
408 | include_spip("inc/forms_type_champs"); |
---|
409 | |
---|
410 | $erreur = Forms_valide_conformite_champs_reponse_post($id_form, $c, $structure); |
---|
411 | if (!$erreur) { |
---|
412 | $champs_mod = array(); |
---|
413 | foreach($structure as $champ=>$infos){ |
---|
414 | $val = _request($champ,$c); |
---|
415 | if ($val!==NULL){ |
---|
416 | $champs_mod[] = $champ; |
---|
417 | $type = $infos['type']; |
---|
418 | $ins = Forms_insertions_reponse_un_champ($id_form,$id_donnee,$champ,$type,$val,$erreur,$ok); |
---|
419 | $inserts = array_merge($inserts,$ins); |
---|
420 | } |
---|
421 | } |
---|
422 | $in_champs = calcul_mysql_in('champ',join(',',array_map('_q', $champs_mod))); |
---|
423 | spip_query("DELETE FROM spip_forms_donnees_champs WHERE $in_champs AND id_donnee="._q($id_donnee)); |
---|
424 | spip_query("INSERT INTO spip_forms_donnees_champs (id_donnee, champ, valeur) ". |
---|
425 | "VALUES ".join(',', $inserts)); |
---|
426 | } |
---|
427 | else |
---|
428 | spip_log("erreur: ".serialize($erreur)); |
---|
429 | |
---|
430 | return $erreur; |
---|
431 | } |
---|
432 | |
---|
433 | function Forms_enregistrer_reponse_formulaire($id_form, $id_donnee, &$erreur, &$reponse, $script_validation = 'valide_form', $script_args='') { |
---|
434 | $r = ''; |
---|
435 | |
---|
436 | $result = spip_query("SELECT * FROM spip_forms WHERE id_form="._q($id_form)); |
---|
437 | if (!$row = spip_fetch_array($result)) { |
---|
438 | $erreur['@'] = _T("forms:probleme_technique"); |
---|
439 | } |
---|
440 | $moderation = $row['moderation']; |
---|
441 | // Extraction des donnees pour l'envoi des mails eventuels |
---|
442 | // accuse de reception et forward webmaster |
---|
443 | $email = unserialize($row['email']); |
---|
444 | $champconfirm = $row['champconfirm']; |
---|
445 | $mailconfirm = ''; |
---|
446 | |
---|
447 | include_spip("inc/forms_type_champs"); |
---|
448 | $erreur = Forms_valide_champs_reponse_post($id_form); |
---|
449 | |
---|
450 | // Si tout est bon, enregistrer la reponse |
---|
451 | if (!$erreur) { |
---|
452 | global $auteur_session; |
---|
453 | $id_auteur = $auteur_session ? intval($auteur_session['id_auteur']) : 0; |
---|
454 | $url = (_DIR_RESTREINT==_DIR_RESTREINT_ABS)?parametre_url(self(),'id_form',''):_DIR_RESTREINT_ABS; |
---|
455 | $ok = true; |
---|
456 | |
---|
457 | if ($row['type_form']=='sondage') { |
---|
458 | $confirmation = 'attente'; |
---|
459 | $cookie = $GLOBALS['cookie_form']; |
---|
460 | $nom_cookie = Forms_nom_cookie_form($id_form); |
---|
461 | } |
---|
462 | else { |
---|
463 | $confirmation = 'valide'; |
---|
464 | $cookie = ''; |
---|
465 | } |
---|
466 | if ($moderation = 'posteriori') |
---|
467 | $statut='publie'; |
---|
468 | else |
---|
469 | $statut = 'propose'; |
---|
470 | // D'abord creer la reponse dans la base de donnees |
---|
471 | if ($ok) { |
---|
472 | $dejareponse=Forms_verif_cookie_sondage_utilise($id_form); |
---|
473 | if (($row['modifiable'] == 'oui' || !_DIR_RESTREINT) && $dejareponse) { |
---|
474 | $q = "SELECT id_donnee FROM spip_forms_donnees WHERE id_form="._q($id_form). |
---|
475 | " AND (cookie="._q($cookie)." OR id_auteur="._q($id_auteur).")"; |
---|
476 | if ($id_auteur) |
---|
477 | if ($cookie) $q.="AND (cookie="._q($cookie)." OR id_auteur="._q($id_auteur).")"; |
---|
478 | else $q.="AND id_auteur="._q($id_auteur); |
---|
479 | else |
---|
480 | if ($cookie) $q.="AND (cookie="._q($cookie)." OR id_auteur="._q($id_auteur).")"; |
---|
481 | //si unique, ignorer id_donnee, si pas id_donnee, ne renverra rien |
---|
482 | if ($row['multiple']=='oui' || !_DIR_RESTREINT) $q.=" AND donnees_champs.id_donnee="._q($id_donnee); |
---|
483 | $r=spip_query($q); |
---|
484 | if ($r=spip_fetch_array($r)){ |
---|
485 | $id_donnee = $r['id_donnee']; |
---|
486 | spip_query("UPDATE spip_forms_donnees SET date=NOW(), ip="._q($GLOBALS['ip']).", url="._q($url).", '$confirmation', statut="._q($statut).", cookie="._q($cookie)." ". |
---|
487 | "WHERE id_donnee="._q($id_donnee)); |
---|
488 | spip_query("DELETE FROM spip_forms_donnees_champs WHERE id_donnee="._q($id_donnee)); |
---|
489 | } else { |
---|
490 | spip_query("INSERT INTO spip_forms_donnees (id_form, id_auteur, date, ip, url, confirmation,statut, cookie) ". |
---|
491 | "VALUES ("._q($id_form).","._q($id_auteur).", NOW(),"._q($GLOBALS['ip']).","._q($url).", '$confirmation', '$statut',"._q($cookie).")"); |
---|
492 | $id_donnee = spip_insert_id(); |
---|
493 | } |
---|
494 | } elseif (!$id_donnee && (!_DIR_RESTREINT || !($dejareponse && $row['multiple']=='non'))) { |
---|
495 | spip_query("INSERT INTO spip_forms_donnees (id_form, id_auteur, date, ip, url, confirmation,statut, cookie) ". |
---|
496 | "VALUES ("._q($id_form).","._q($id_auteur).", NOW(),"._q($GLOBALS['ip']).","._q($url).", '$confirmation', '$statut',"._q($cookie).")"); |
---|
497 | $id_donnee = spip_insert_id(); |
---|
498 | } |
---|
499 | if (!$id_donnee) { |
---|
500 | $erreur['@'] = _T("forms:probleme_technique"); |
---|
501 | $ok = false; |
---|
502 | } |
---|
503 | } |
---|
504 | // Puis enregistrer les differents champs |
---|
505 | if ($ok) { |
---|
506 | $inserts = Forms_insertions_reponse_post($id_form,$id_donnee,$erreur,$ok); |
---|
507 | if (!count($inserts)) { |
---|
508 | // Reponse vide => annuler |
---|
509 | $erreur['@'] = _T("forms:remplir_un_champ"); |
---|
510 | spip_query("DELETE FROM spip_forms_donnees WHERE id_donnee="._q($id_donnee)); |
---|
511 | $ok = false; |
---|
512 | } |
---|
513 | } |
---|
514 | if ($ok) { |
---|
515 | include_spip('inc/securiser_action'); |
---|
516 | spip_query("DELETE FROM spip_forms_donnees_champs WHERE id_donnee="._q($id_donnee)); |
---|
517 | spip_query("INSERT INTO spip_forms_donnees_champs (id_donnee, champ, valeur) ". |
---|
518 | "VALUES ".join(',', $inserts)); |
---|
519 | if ($champconfirm) |
---|
520 | if ($row=spip_fetch_array(spip_query("SELECT * FROM spip_forms_donnees_champs WHERE id_donnee="._q($id_donnee)." AND champ="._q($champconfirm)))) |
---|
521 | $mailconfirm = $row['valeur']; |
---|
522 | if (($email) || ($mailconfirm)) { |
---|
523 | $hash = calculer_action_auteur("forms confirme reponse $id_donnee"); |
---|
524 | $url = generer_url_public($script_validation,"mel_confirm=oui&id_donnee=$id_donnee&hash=$hash".($script_args?"&$script_args":"")); |
---|
525 | $r = $url; |
---|
526 | } |
---|
527 | if ($row['type_form']=='sondage') { |
---|
528 | $hash = calculer_action_auteur("forms valide reponse sondage $id_donnee"); |
---|
529 | $url = generer_url_public($script_validation,"verif_cookie=oui&id_donnee=$id_donnee&hash=$hash".($script_args?"&$script_args":"")); |
---|
530 | $r = $url; |
---|
531 | } |
---|
532 | } |
---|
533 | } |
---|
534 | |
---|
535 | return $r; |
---|
536 | } |
---|
537 | |
---|
538 | function Forms_generer_mail_reponse_formulaire($id_form, $id_donnee, $env){ |
---|
539 | if (!is_array($env)) $env=array(); |
---|
540 | $modele_mail = 'form_reponse_email'; |
---|
541 | if (isset($env['modele'])) |
---|
542 | $modele_mail = $env['modele']; |
---|
543 | $result = spip_query("SELECT * FROM spip_forms WHERE id_form="._q($id_form)); |
---|
544 | if ($row = spip_fetch_array($result)) { |
---|
545 | $modele = "modeles/$modele_mail"; |
---|
546 | if ($f = find_in_path(($m = "$modele-$id_form").".html")) |
---|
547 | $modele = $m; |
---|
548 | $corps_mail = recuperer_fond($modele,array_merge($env,array('id_donnee'=>$id_donnee))); |
---|
549 | $corps_mail_admin = recuperer_fond($modele,array_merge($env,array('id_donnee'=>$id_donnee,'mail_admin'=>'oui'))); |
---|
550 | $champconfirm = $row['champconfirm']; |
---|
551 | $email = unserialize($row['email']); |
---|
552 | $email_dest = $email['defaut']; |
---|
553 | $mailconfirm = ""; |
---|
554 | |
---|
555 | // recuperer l'email de confirmation |
---|
556 | $result2 = spip_query("SELECT * FROM spip_forms_donnees_champs WHERE id_donnee="._q($id_donnee)." AND champ="._q($champconfirm)); |
---|
557 | if ($row2 = spip_fetch_array($result2)) { |
---|
558 | $mailconfirm = $row2['valeur']; |
---|
559 | } |
---|
560 | |
---|
561 | // recuperer l'email d'admin |
---|
562 | $result2 = spip_query("SELECT * FROM spip_forms_donnees_champs WHERE id_donnee="._q($id_donnee)." AND champ="._q($email['route'])); |
---|
563 | if ($row2 = spip_fetch_array($result2)) { |
---|
564 | if (isset($email[$row2['valeur']])) |
---|
565 | $email_dest = $email[$row2['valeur']]; |
---|
566 | } |
---|
567 | |
---|
568 | include_spip('inc/mail'); |
---|
569 | if ($mailconfirm !== '') { |
---|
570 | $head="From: formulaire@".$_SERVER["HTTP_HOST"]."\n"; |
---|
571 | $sujet = $row['titre']; |
---|
572 | $dest = $mailconfirm; |
---|
573 | // mettre le texte dans un charset acceptable et sans entites |
---|
574 | //$mess_iso = unicode2charset(html2unicode(charset2unicode($corps_mail)),'iso-8859-1'); |
---|
575 | //mail($dest, $sujet, $mess_iso, $head); |
---|
576 | $headers = ""; |
---|
577 | if (preg_match(",<html>(.*)</html>,Uims",$corps_mail,$regs)){ |
---|
578 | $charset = $GLOBALS['meta']['charset']; |
---|
579 | $headers .= |
---|
580 | "MIME-Version: 1.0\n". |
---|
581 | "Content-Type: text/html; charset=$charset\n". |
---|
582 | "Content-Transfer-Encoding: 8bit\n"; |
---|
583 | if (preg_match(",<h[1-6]>(.*)</h[1-6]>,Uims",$regs[1],$hs)) |
---|
584 | $sujet=$hs[1]; |
---|
585 | } |
---|
586 | envoyer_mail($dest, $sujet, $corps_mail, "formulaire@".$_SERVER["HTTP_HOST"], $headers); |
---|
587 | } |
---|
588 | if ($email_dest != '') { |
---|
589 | $head="From: formulaire_$id_form@".$_SERVER["HTTP_HOST"]."\n"; |
---|
590 | $sujet = $row['titre']; |
---|
591 | $dest = $email_dest; |
---|
592 | // mettre le texte dans un charset acceptable et sans entites |
---|
593 | //$mess_iso = unicode2charset(html2unicode(charset2unicode($corps_mail_admin)),'iso-8859-1'); |
---|
594 | //mail($dest, $sujet, $mess_iso, $head); |
---|
595 | $headers = ""; |
---|
596 | if (preg_match(",<html>.*</html>,Uims",$corps_mail_admin,$regs)){ |
---|
597 | $charset = $GLOBALS['meta']['charset']; |
---|
598 | $headers .= |
---|
599 | "MIME-Version: 1.0\n". |
---|
600 | "Content-Type: text/html; charset=$charset\n". |
---|
601 | "Content-Transfer-Encoding: 8bit\n"; |
---|
602 | if (preg_match(",<h[1-6]>(.*)</h[1-6]>,Uims",$regs[1],$hs)) |
---|
603 | $sujet=$hs[1]; |
---|
604 | } |
---|
605 | envoyer_mail($dest, $sujet, $corps_mail_admin, "formulaire@".$_SERVER["HTTP_HOST"], $headers); |
---|
606 | } |
---|
607 | } |
---|
608 | } |
---|
609 | function Forms_obligatoire($row,$forms_obligatoires){ |
---|
610 | $returned=$row; |
---|
611 | global $auteur_session; |
---|
612 | $id_auteur = $auteur_session ? intval($auteur_session['id_auteur']) : 0; |
---|
613 | $form_tab=explode(',',$forms_obligatoires); |
---|
614 | $chercher=true; |
---|
615 | $i=0; |
---|
616 | while ($chercher && $i<count($form_tab)){ |
---|
617 | $form_id=$form_tab[$i]; |
---|
618 | $cookie = $_COOKIE[Forms_nom_cookie_form($form_id)]; |
---|
619 | $q="SELECT id_form FROM spip_forms_donnees WHERE statut='publie' AND id_form="._q($form_id)." "; |
---|
620 | if ($cookie) $q.="AND (cookie="._q($cookie)." OR id_auteur="._q($id_auteur).") "; |
---|
621 | else |
---|
622 | if ($id_auteur) |
---|
623 | $q.="AND id_auteur="._q($id_auteur)." "; |
---|
624 | else |
---|
625 | $q.="AND 0=1 "; |
---|
626 | $res=spip_query($q); |
---|
627 | if (!spip_fetch_array($res)){ |
---|
628 | $res2 = spip_query("SELECT * FROM spip_forms WHERE id_form="._q($form_id)); |
---|
629 | $returned = spip_fetch_array($res2); |
---|
630 | $chercher=false; |
---|
631 | } |
---|
632 | $i++; |
---|
633 | } |
---|
634 | return $returned; |
---|
635 | } |
---|
636 | ?> |
---|