[SPIP] +2.1.12
[velocampus/web/www.git] / www / ecrire / inc / joindre.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 include_spip('inc/actions'); // *action_auteur et determine_upload
16 include_spip('base/abstract_sql');
17
18 //
19 // Construire un formulaire pour telecharger un fichier an Ajax
20 //
21
22 // http://doc.spip.org/@inc_joindre_dist
23 function inc_joindre_dist($v) {
24
25 // calculer le formulaire de base
26
27 $res = joindre_formulaire($v);
28
29 if (!empty($v['cadre'])) {
30 $debut_cadre = 'debut_cadre_'.$v['cadre'];
31 $fin_cadre = 'fin_cadre_'.$v['cadre'];
32 $res = $debut_cadre($v['icone'], true, $v['fonction'], $v['titre'])
33 . $res
34 . $fin_cadre(true);
35 }
36
37 $res = "\n<div class='joindre'>".$res."</div>\n";
38 $att = " enctype='multipart/form-data' class='form_upload'";
39 $args = (intval($v['id']) .'/' .intval($v['id_document']) . "/".$v['mode'].'/'.$v['type']);
40 $script = $v['script'];
41
42 // si espace prive, $v a une signification speciale (pas clair)
43 if (test_espace_prive())
44 return redirige_action_post('joindre', $args, $script, $v['args'], $res, $att);
45 else return generer_action_auteur('joindre', $args, $script, $res, "$att method='post'");
46 }
47
48 // http://doc.spip.org/@joindre_formulaire
49 function joindre_formulaire(&$v) {
50 global $spip_lang_right;
51 $depliable = false;
52
53 $mode = $v['mode'];
54 $vignette_de_doc = ($mode == 'vignette' AND $v['id_document']>0);
55 $distant = (($mode == 'document' OR $mode == 'choix') AND $v['type']);
56
57 # indiquer un choix d'upload FTP
58 $dir_ftp = '';
59 if (test_espace_prive()
60 AND ($mode == 'document' OR $mode == 'choix') # si c'est pour un document
61 AND !$vignette_de_doc # pas pour une vignette (NB: la ligne precedente suffit, mais si on la supprime il faut conserver ce test-ci)
62 AND $GLOBALS['flag_upload']) {
63 if ($dir = determine_upload('documents')) {
64 // quels sont les docs accessibles en ftp ?
65 $l = texte_upload_manuel($dir, $mode);
66 // s'il n'y en a pas, on affiche un message d'aide
67 // en mode document, mais pas en mode image
68 if ($l OR ($mode == 'document' OR $mode=='choix'))
69 $dir_ftp = afficher_transferer_upload($l, $dir);
70 }
71 }
72
73 // Add the redirect url when uploading via iframe
74
75 $iframe = "";
76 if($v['iframe_script'])
77 $iframe = "<input type='hidden' name='iframe_redirect' value='".rawurlencode($v['iframe_script'])."' />\n";
78
79 // Un menu depliant si on a une possibilite supplementaire
80
81 if ($dir_ftp OR $distant OR $vignette_de_doc) {
82 $bloc = "ftp_". $mode .'_'. intval($v['id_document']);
83
84 if ($vignette_de_doc)
85 $debut = bouton_block_depliable($v['intitule'],false,$bloc);
86 else
87 $debut = $v['intitule'];
88
89 $milieu = debut_block_depliable(false,$bloc);
90 $fin = "\n\t" . fin_block();
91 $v['titre'] = bouton_block_depliable($v['titre'],false,$bloc);
92
93 } else
94 $debut = $milieu = $fin = '';
95
96 // Lien document distant, jamais en mode image
97 if ($distant) {
98 $distant = "<br />\n<div style='border: 1px #303030 solid; padding: 4px; color: #505050;'>" .
99 "\n\t<img src='". chemin_image('attachment.gif') .
100 "' style='float: $spip_lang_right;' alt=\"\" />\n" .
101 "<label for='url'>" .
102 _T('info_referencer_doc_distant') .
103 "</label><br />\n\t<input name='url' id='url' value='http://' />" .
104 "\n\t<div style='text-align: $spip_lang_right'><input name='sousaction2' type='submit' value='".
105 _T('bouton_choisir').
106 "' /></div>" .
107 "\n</div>";
108 }
109
110 $res = "<input name='fichier' id='fichier_$mode" .'_'. strval($v['id_document']) . "' type='file' class='forml spip_xx-small' size='15' />"
111 . ($v['ancre']
112 ? "\n\t\t<input type='hidden' name='ancre' value='".$v['ancre']."' />"
113 : ''
114 )
115 . "\n\t\t<div style='text-align: $spip_lang_right'><input name='sousaction1' type='submit' value='"
116 . _T('bouton_telecharger')
117 . "' /></div>";
118
119 if ($vignette_de_doc)
120 $res = $milieu . $res;
121 else
122 $res = $res . $milieu;
123
124 return "$iframe$debut$res$dir_ftp$distant$fin";
125 }
126
127
128 //
129 // Retourner le code HTML d'utilisation de fichiers envoyes
130 //
131
132 // http://doc.spip.org/@texte_upload_manuel
133 function texte_upload_manuel($dir, $mode = 'document') {
134 $fichiers = preg_files($dir);
135 $exts = array();
136 $dirs = array();
137 $texte_upload = array();
138
139 // en mode "charger une image", ne proposer que les inclus
140 $inclus = ($mode == 'document' OR $mode =='choix')
141 ? ''
142 : " AND inclus='image'";
143
144 foreach ($fichiers as $f) {
145 $f = preg_replace(",^$dir,",'',$f);
146 if (preg_match(",\.([^.]+)$,", $f, $match)) {
147 $ext = strtolower($match[1]);
148 if (!isset($exts[$ext])) {
149 include_spip('inc/ajouter_documents');
150 $ext = corriger_extension($ext);
151 if (sql_fetsel('extension', 'spip_types_documents', $a = "extension='$ext'" . $inclus))
152 $exts[$ext] = 'oui';
153 else $exts[$ext] = 'non';
154 }
155
156 $k = 2*substr_count($f,'/');
157 $n = strrpos($f, "/");
158 if ($n === false)
159 $lefichier = $f;
160 else {
161 $lefichier = substr($f, $n+1, strlen($f));
162 $ledossier = substr($f, 0, $n);
163 if (!in_array($ledossier, $dirs)) {
164 $texte_upload[] = "\n<option value=\"$ledossier\">"
165 . str_repeat("&nbsp;",$k)
166 ._T('tout_dossier_upload', array('upload' => $ledossier))
167 ."</option>";
168 $dirs[]= $ledossier;
169 }
170 }
171
172 if ($exts[$ext] == 'oui')
173 $texte_upload[] = "\n<option value=\"$f\">" .
174 str_repeat("&nbsp;",$k+2) .
175 $lefichier .
176 "</option>";
177 }
178 }
179
180 $texte = join('', $texte_upload);
181 if (count($texte_upload)>1) {
182 $texte = "\n<option value=\"/\" style='font-weight: bold;'>"
183 ._T('info_installer_tous_documents')
184 ."</option>" . $texte;
185 }
186
187 return $texte;
188 }
189
190
191 // http://doc.spip.org/@afficher_transferer_upload
192 function afficher_transferer_upload($texte_upload, $dir)
193 {
194 $doc = array('upload' => '<b>' . joli_repertoire($dir) . '</b>');
195 if (!$texte_upload) {
196 return "\n<div style='border: 1px #303030 solid; padding: 4px; color: #505050;'>" .
197 _T('info_installer_ftp', $doc) .
198 aide("ins_upload") .
199 "</div>";
200 }
201 else { return
202 "\n<div style='color: #505050;'>"
203 ."<label for='cheminupload'>" . _T('info_selectionner_fichier', $doc)
204 ."</label>&nbsp;:<br />\n" .
205 "\n<select name='chemin' id='cheminupload' size='1' style='width:100%;overflow:hidden;'>"
206 . "<option value=''>&gt;&gt;</option>"
207 . $texte_upload .
208 "\n</select>" .
209 "\n<div style='text-align: ".
210 $GLOBALS['spip_lang_right'] .
211 "'><input name='sousaction3' type='submit' value='" .
212 _T('bouton_choisir').
213 "' /></div>" .
214 "</div>\n";
215 }
216 }
217 ?>