1e2e11d2d2c391d553fd9bd305d0a9a3f2c36ca3
[velocampus/web/www.git] / www / ecrire / action / 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/charsets'); # pour le nom de fichier
16 include_spip('inc/actions');
17
18 // http://doc.spip.org/@action_joindre_dist
19 function action_joindre_dist()
20 {
21 global $redirect;
22 $securiser_action = charger_fonction('securiser_action', 'inc');
23 $arg = $securiser_action();
24
25 if (!preg_match(',^(-?\d+)\D(\d+)\D(\w+)/(\w+)$,',$arg,$r)) {
26 spip_log("action_joindre_dist incompris: " . $arg);
27 $redirect = urldecode(_request('redirect'));
28 return;
29 }
30
31 list(, $id, $id_document, $mode, $type) = $r;
32
33 $actifs = array();
34 $redirect = action_joindre_sous_action($id, $id_document, $mode, $type, $actifs);
35 }
36
37 // http://doc.spip.org/@action_joindre_sous_action
38 function action_joindre_sous_action($id, $id_document, $mode, $type, &$documents_actifs)
39 {
40 $hash = _request('hash');
41 $url = _request('url');
42 $chemin = _request('chemin');
43 $ancre = _request('ancre');
44 $sousaction1 = _request('sousaction1');
45 $sousaction2 = _request('sousaction2');
46 $sousaction3 = _request('sousaction3');
47 $sousaction4 = _request('sousaction4');
48 $sousaction5 = _request('sousaction5'); // decompacter un zip
49 $redirect = _request('redirect');
50 $iframe_redirect = _request('iframe_redirect');
51
52 // pas terrible, mais c'est le pb du bouton Submit qui retourne son texte,
53 // et son transcodage est couteux et perilleux
54 $sousaction =
55 ($sousaction1 ? 1 :
56 ($sousaction2 ? 2 :
57 ($sousaction3 ? 3 :
58 ($sousaction4 ? 4 :
59 $sousaction5 ))));
60
61 $path = ($sousaction1 ? ($_FILES ? $_FILES : $GLOBALS['HTTP_POST_FILES']) :
62 ($sousaction2 ? $url : $chemin));
63
64 $sousaction = charger_fonction('joindre' . $sousaction, 'inc');
65 $type_image = $sousaction($path, $mode, $type, $id, $id_document,
66 $hash, $redirect, $documents_actifs, $iframe_redirect);
67
68 $redirect = urldecode($redirect);
69 if ($documents_actifs) {
70 $redirect = parametre_url($redirect,'show_docs',join(',',$documents_actifs),'&');
71 }
72
73 if (!$ancre) {
74 if ($mode=='image')
75 $ancre = 'images';
76 else if ($type_image)
77 $ancre = 'portfolio';
78 else
79 $ancre = 'documents';
80 }
81
82 $redirect .= '#' . $ancre;
83 if ($type == 'rubrique') {
84 include_spip('inc/rubriques');
85 calculer_rubriques_if($id, array('statut' => 'publie'));
86 }
87
88 if(_request("iframe") == 'iframe') {
89 $redirect = parametre_url(urldecode($iframe_redirect),"show_docs",join(',',$documents_actifs),'&')."&iframe=iframe";
90 }
91 return $redirect;
92 }
93
94 // Cas d'un document distant reference sur internet
95
96 // http://doc.spip.org/@inc_joindre2_dist
97 function inc_joindre2_dist($path, $mode, $type, $id, $id_document,$hash, $redirect, &$actifs, $iframe_redirect)
98 {
99 return joindre_documents(array(
100 array('name' => basename($path),
101 'tmp_name' => $path)
102 ), 'distant', $type, $id, $id_document,
103 $hash, $redirect, $actifs, $iframe_redirect);
104 }
105
106 // Cas d'un fichier transmis
107
108 // http://doc.spip.org/@inc_joindre1_dist
109 function inc_joindre1_dist($path, $mode, $type, $id, $id_document,$hash, $redirect, &$actifs, $iframe_redirect)
110 {
111 $files = array();
112 if (is_array($path))
113 foreach ($path as $file) {
114 //UPLOAD_ERR_NO_FILE
115 if (!($file['error'] == 4) )
116 $files[]=$file;
117 }
118
119 return joindre_documents($files, $mode, $type, $id, $id_document,
120 $hash, $redirect, $actifs, $iframe_redirect);
121 }
122
123 // copie de tout ou partie du repertoire upload
124
125 // http://doc.spip.org/@inc_joindre3_dist
126 function inc_joindre3_dist($path, $mode, $type, $id, $id_document,$hash, $redirect, &$actifs, $iframe_redirect)
127 {
128 if (!$path || strstr($path, '..')) return;
129
130 $upload = determine_upload();
131 if ($path != '/' AND $path != './') $upload .= $path;
132
133 if (!is_dir($upload))
134 // seul un fichier est demande
135 $files = array(array ('name' => basename($upload),
136 'tmp_name' => $upload)
137 );
138 else {
139 include_spip('inc/documents');
140 $files = array();
141 foreach (preg_files($upload) as $fichier) {
142 $files[]= array (
143 'name' => basename($fichier),
144 'tmp_name' => $fichier
145 );
146 }
147 }
148
149 return joindre_documents($files, $mode, $type, $id, $id_document, $hash, $redirect, $actifs, $iframe_redirect);
150 }
151
152 //
153 // Charger la fonction surchargeable receptionnant un fichier
154 // et l'appliquer sur celui ou ceux indiques.
155
156 // http://doc.spip.org/@joindre_documents
157 function joindre_documents($files, $mode, $type, $id, $id_document, $hash, $redirect, &$actifs, $iframe_redirect)
158 {
159 $ajouter_documents = charger_fonction('ajouter_documents', 'inc');
160
161 if (function_exists('gzopen')
162 AND !($mode == 'distant')
163 AND (count($files) == 1)
164 AND (preg_match('/\.zip$/i', $files[0]['name'])
165 OR ($files[0]['type'] == 'application/zip'))) {
166
167 // on pose le fichier dans le repertoire zip
168 // (nota : copier_document n'ecrase pas un fichier avec lui-meme
169 // ca autorise a boucler)
170 $desc = $files[0];
171 $zip = copier_document("zip",
172 $desc['name'],
173 $desc['tmp_name']
174 );
175 // Est-ce qu'on sait le lire ?
176 include_spip('inc/pclzip');
177 $archive = $zip ? new PclZip($zip) : '';
178 if ($archive) {
179 $valables = verifier_compactes($archive);
180 if ($valables) {
181 if (rename($zip, $tmp = _DIR_TMP.basename($zip))) {
182 echo $ajouter_documents($valables, $tmp, $type, $id, $mode, $id_document, $actifs, $hash, $redirect, $iframe_redirect);
183 // a tout de suite en joindre4, joindre5, ou joindre6
184 exit;
185 }
186 }
187 }
188 }
189
190 foreach ($files as $arg) {
191 // verifier l'extension du fichier en fonction de son type mime
192 list($extension,$arg['name']) = fixer_extension_document($arg);
193 check_upload_error($arg['error']);
194 $x = $ajouter_documents($arg['tmp_name'], $arg['name'],
195 $type, $id, $mode, $id_document, $actifs);
196 }
197 // un invalideur a la hussarde qui doit marcher au moins pour article, breve, rubrique
198 include_spip('inc/invalideur');
199 suivre_invalideur("id='id_$type/$id'");
200 return $x;
201 }
202
203 #-----------------------------------------------------------------------
204
205 // sous-actions suite a l'envoi d'un Zip:
206 // la fonction joindre_documents ci-dessus a construit un formulaire
207 // qui renvoie sur une des 3 sous-actions qui suivent.
208
209 // Zip avec confirmation "tel quel"
210
211 // http://doc.spip.org/@inc_joindre5_dist
212 function inc_joindre5_dist($path, $mode, $type, $id, $id_document,$hash, $redirect, &$actifs)
213 {
214 $ajouter_documents = charger_fonction('ajouter_documents', 'inc');
215 return $ajouter_documents($path, basename($path), $type, $id, $mode, $id_document, $actifs);
216 }
217
218 // Zip a deballer.
219
220 // http://doc.spip.org/@inc_joindre6_dist
221 function inc_joindre6_dist($path, $mode, $type, $id, $id_document,$hash, $redirect, &$actifs, $iframe_redirect)
222 {
223 $x = joindre_deballes($path, $mode, $type, $id, $id_document,$hash, $redirect, $actifs);
224 // suppression de l'archive en zip
225 spip_unlink($path);
226 return $x;
227 }
228
229 // Zip avec les 2 options a la fois
230
231 // http://doc.spip.org/@inc_joindre4_dist
232 function inc_joindre4_dist($path, $mode, $type, $id, $id_document,$hash, $redirect, &$actifs, $iframe_redirect)
233 {
234 joindre_deballes($path, $mode, $type, $id, $id_document,$hash, $redirect, $actifs);
235 return inc_joindre5_dist($path, $mode, $type, $id, $id_document,$hash, $redirect, $actifs);
236 }
237
238 // http://doc.spip.org/@joindre_deballes
239 function joindre_deballes($path, $mode, $type, $id, $id_document,$hash, $redirect, &$actifs)
240 {
241 $ajouter_documents = charger_fonction('ajouter_documents', 'inc');
242 define('_tmp_dir', creer_repertoire_documents($hash));
243
244 if (_tmp_dir == _DIR_IMG)
245 {include_spip('inc/minipres');
246 echo minipres(_T('avis_operation_impossible'));
247 exit;
248 }
249 include_spip('inc/pclzip');
250 $archive = new PclZip($path);
251 $archive->extract(
252 PCLZIP_OPT_PATH, _tmp_dir,
253 PCLZIP_CB_PRE_EXTRACT, 'callback_deballe_fichier'
254 );
255 $contenu = verifier_compactes($archive);
256 $titrer = _request('titrer') == 'on';
257 foreach ($contenu as $fichier => $size) {
258 $f = basename($fichier);
259 $x = $ajouter_documents(_tmp_dir. $f, $f,
260 $type, $id, $mode, $id_document, $actifs, $titrer);
261 }
262 effacer_repertoire_temporaire(_tmp_dir);
263 return $x;
264 }
265 ?>