3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
6 * Copyright (c) 2001-2014 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
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 \***************************************************************************/
13 if (!defined('_ECRIRE_INC_VERSION')) return;
15 include_spip('inc/charsets'); # pour le nom de fichier
16 include_spip('inc/actions');
18 // http://doc.spip.org/@action_joindre_dist
19 function action_joindre_dist()
22 $securiser_action = charger_fonction('securiser_action', 'inc');
23 $arg = $securiser_action();
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'));
31 list(, $id, $id_document, $mode, $type) = $r;
34 $redirect = action_joindre_sous_action($id, $id_document, $mode, $type, $actifs);
37 // http://doc.spip.org/@action_joindre_sous_action
38 function action_joindre_sous_action($id, $id_document, $mode, $type, &$documents_actifs)
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');
52 // pas terrible, mais c'est le pb du bouton Submit qui retourne son texte,
53 // et son transcodage est couteux et perilleux
61 $path = ($sousaction1 ?
($_FILES ?
$_FILES : $GLOBALS['HTTP_POST_FILES']) :
62 ($sousaction2 ?
$url : $chemin));
64 $sousaction = charger_fonction('joindre' . $sousaction, 'inc');
65 $type_image = $sousaction($path, $mode, $type, $id, $id_document,
66 $hash, $redirect, $documents_actifs, $iframe_redirect);
68 $redirect = urldecode($redirect);
69 if ($documents_actifs) {
70 $redirect = parametre_url($redirect,'show_docs',join(',',$documents_actifs),'&');
82 $redirect .= '#' . $ancre;
83 if ($type == 'rubrique') {
84 include_spip('inc/rubriques');
85 calculer_rubriques_if($id, array('statut' => 'publie'));
88 if(_request("iframe") == 'iframe') {
89 $redirect = parametre_url(urldecode($iframe_redirect),"show_docs",join(',',$documents_actifs),'&')."&iframe=iframe";
94 // Cas d'un document distant reference sur internet
96 // http://doc.spip.org/@inc_joindre2_dist
97 function inc_joindre2_dist($path, $mode, $type, $id, $id_document,$hash, $redirect, &$actifs, $iframe_redirect)
99 return joindre_documents(array(
100 array('name' => basename($path),
102 ), 'distant', $type, $id, $id_document,
103 $hash, $redirect, $actifs, $iframe_redirect);
106 // Cas d'un fichier transmis
108 // http://doc.spip.org/@inc_joindre1_dist
109 function inc_joindre1_dist($path, $mode, $type, $id, $id_document,$hash, $redirect, &$actifs, $iframe_redirect)
113 foreach ($path as $file) {
115 if (!($file['error'] == 4) )
119 return joindre_documents($files, $mode, $type, $id, $id_document,
120 $hash, $redirect, $actifs, $iframe_redirect);
123 // copie de tout ou partie du repertoire upload
125 // http://doc.spip.org/@inc_joindre3_dist
126 function inc_joindre3_dist($path, $mode, $type, $id, $id_document,$hash, $redirect, &$actifs, $iframe_redirect)
128 if (!$path ||
strstr($path, '..')) return;
130 $upload = determine_upload();
131 if ($path != '/' AND $path != './') $upload .= $path;
133 if (!is_dir($upload))
134 // seul un fichier est demande
135 $files = array(array ('name' => basename($upload),
136 'tmp_name' => $upload)
139 include_spip('inc/documents');
141 foreach (preg_files($upload) as $fichier) {
143 'name' => basename($fichier),
144 'tmp_name' => $fichier
149 return joindre_documents($files, $mode, $type, $id, $id_document, $hash, $redirect, $actifs, $iframe_redirect);
153 // Charger la fonction surchargeable receptionnant un fichier
154 // et l'appliquer sur celui ou ceux indiques.
156 // http://doc.spip.org/@joindre_documents
157 function joindre_documents($files, $mode, $type, $id, $id_document, $hash, $redirect, &$actifs, $iframe_redirect)
159 $ajouter_documents = charger_fonction('ajouter_documents', 'inc');
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'))) {
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)
171 $zip = copier_document("zip",
175 // Est-ce qu'on sait le lire ?
176 include_spip('inc/pclzip');
177 $archive = $zip ?
new PclZip($zip) : '';
179 $valables = verifier_compactes($archive);
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
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);
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'");
203 #-----------------------------------------------------------------------
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.
209 // Zip avec confirmation "tel quel"
211 // http://doc.spip.org/@inc_joindre5_dist
212 function inc_joindre5_dist($path, $mode, $type, $id, $id_document,$hash, $redirect, &$actifs)
214 $ajouter_documents = charger_fonction('ajouter_documents', 'inc');
215 return $ajouter_documents($path, basename($path), $type, $id, $mode, $id_document, $actifs);
220 // http://doc.spip.org/@inc_joindre6_dist
221 function inc_joindre6_dist($path, $mode, $type, $id, $id_document,$hash, $redirect, &$actifs, $iframe_redirect)
223 $x = joindre_deballes($path, $mode, $type, $id, $id_document,$hash, $redirect, $actifs);
224 // suppression de l'archive en zip
229 // Zip avec les 2 options a la fois
231 // http://doc.spip.org/@inc_joindre4_dist
232 function inc_joindre4_dist($path, $mode, $type, $id, $id_document,$hash, $redirect, &$actifs, $iframe_redirect)
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);
238 // http://doc.spip.org/@joindre_deballes
239 function joindre_deballes($path, $mode, $type, $id, $id_document,$hash, $redirect, &$actifs)
241 $ajouter_documents = charger_fonction('ajouter_documents', 'inc');
242 define('_tmp_dir', creer_repertoire_documents($hash));
244 if (_tmp_dir
== _DIR_IMG
)
245 {include_spip('inc/minipres');
246 echo minipres(_T('avis_operation_impossible'));
249 include_spip('inc/pclzip');
250 $archive = new PclZip($path);
252 PCLZIP_OPT_PATH
, _tmp_dir
,
253 PCLZIP_CB_PRE_EXTRACT
, 'callback_deballe_fichier'
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);
262 effacer_repertoire_temporaire(_tmp_dir
);