[SPIP] +2.1.12
[velocampus/web/www.git] / www / plugins / auto / cfg / cfg / classes / cfg_fichier.php
1 <?php
2
3 /**
4 * Plugin générique de configuration pour SPIP
5 *
6 * @license GNU/GPL
7 * @package plugins
8 * @subpackage cfg
9 * @category outils
10 * @copyright (c) toggg, marcimat 2007-2008
11 * @link http://www.spip-contrib.net/
12 * @version $Id$
13 */
14
15 if (!defined("_ECRIRE_INC_VERSION")) return;
16
17 /**
18 * si non definie, _COMPAT_CFG_192 vaut "_COMPAT_CFG_192" :(
19 */
20 define ('_COMPAT_CFG_192',false);
21
22 /**
23 * Vérification du fichier $nom
24 *
25 * @param string $nom
26 * @param Object $cfg
27 * @return Object
28 */
29 function cfg_pre_verifier_cfg_fichier($nom, &$cfg){
30 $f = cfg_get_info_fichier_upload($nom);
31 // si pas de fichier envoye, on ne traite pas le champ
32 if (!$f['tmp_name']) {
33 unset ($cfg->champs[$nom], $cfg->extensions['cfg_fichier'][$nom]);
34 // sinon indiquer un changement
35 // pour eviter le message d'erreur "pas de changement"
36 } else {
37 set_request($nom, '<OLD>'. $cfg->val[$nom]);
38 }
39 return $cfg;
40 }
41
42 /**
43 * Pré-traitement du fichier $nom
44 *
45 * @param string $nom
46 * @param Object $cfg
47 * @return Object
48 */
49 function cfg_pre_traiter_cfg_fichier($nom, &$cfg){
50 include_spip('inc/flock');
51
52 // enlever <OLD>
53 $cfg->val[$nom] = str_replace('<OLD>','', $cfg->val[$nom]);
54
55 // effacement
56 if (_request('_cfg_delete')){
57 $supprimer_fichier = _COMPAT_CFG_192 ? 'cfg_supprimer_fichier' : 'supprimer_fichier';
58 if (!$supprimer_fichier(get_spip_doc($cfg->val[$nom]))) {
59 $cfg->messages['erreurs'][$nom] = _T('cfg:erreur_suppression_fichier', array('fichier'=>get_spip_doc($cfg->val[$nom])));
60 }
61 // ajout ou modification
62 } else {
63 $f = cfg_get_info_fichier_upload($nom);
64 if ($f['tmp_name']) {
65 // suppression de l'ancien fichier
66 $supprimer_fichier = _COMPAT_CFG_192 ? 'cfg_supprimer_fichier' : 'supprimer_fichier';
67 if ($cfg->val[$nom] && !$supprimer_fichier(get_spip_doc($cfg->val[$nom]))) {
68 $cfg->messages['erreurs'][$nom] = _T('cfg:erreur_suppression_fichier', array('fichier'=>get_spip_doc($cfg->val[$nom])));
69 } else {
70 if (!$fichier = cfg_ajoute_un_document($f['tmp_name'],$f['name'],$nom, 'config/'.$cfg->vue)){
71 $cfg->messages['erreurs'][$nom] = _T('cfg:erreur_copie_fichier', array('fichier'=>'config/'.$cfg->vue . '/' . $f['name']));
72 } else {
73 $cfg->val[$nom] = set_spip_doc($fichier);
74 }
75 }
76 }
77 }
78
79 return $cfg;
80 }
81
82 /**
83 *
84 * @param string $nom
85 * @return string
86 */
87 function cfg_get_info_fichier_upload($nom){
88 return $_FILES ? $_FILES[$nom] : $GLOBALS['HTTP_POST_FILES'][$nom];
89 }
90
91 /**
92 * Ajouter un document (au format $_FILES)<br>
93 * (n'ajoute pas le contenu en base dans spip_documents...)
94 *
95 * @param string $source Le fichier sur le serveur (/var/tmp/xyz34)
96 * @param string $nom_envoye Son nom chez le client (portequoi.pdf)
97 * @param string $nom_dest Le nom sous lequel le sauvegarder
98 * @param string $dans Où l'enregistrer
99 * @return string
100 */
101 function cfg_ajoute_un_document($source, $nom_envoye, $nom_dest, $dans='config') {
102
103 include_spip('inc/modifier');
104 include_spip('inc/ajouter_documents');
105
106 $type_image = ''; // au pire
107 // tester le type de document :
108 // - interdit a l'upload ?
109 // - quelle extension dans spip_types_documents ?
110 // - est-ce "inclus" comme une image ?
111
112 preg_match(",^(.*)\.([^.]+)$,", $nom_envoye, $match);
113 @list(,$titre,$ext) = $match;
114 $ext = corriger_extension(strtolower($ext));
115 // ajouter l'extension au nom propose...
116 $row = sql_fetsel("inclus", "spip_types_documents", "extension=" . sql_quote($ext) . " AND upload='oui'");
117
118 if ($row) {
119 $type_inclus_image = ($row['inclus'] == 'image');
120 // hum stocke dans IMG/$ext ?
121 $fichier = cfg_copier_document($ext, $nom_dest.'.'.$ext, $source, $dans);
122 } else {
123
124 /* STOCKER LES DOCUMENTS INCONNUS AU FORMAT .ZIP */
125 $type_inclus_image = false;
126
127 if (!sql_countsel("spip_types_documents", "extension='zip' AND upload='oui'")) {
128 spip_log("Extension $ext interdite a l'upload");
129 return;
130 }
131
132 $ext = 'zip';
133 if (!$tmp_dir = tempnam(_DIR_TMP, 'tmp_upload')) return;
134 spip_unlink($tmp_dir); @mkdir($tmp_dir);
135 $tmp = $tmp_dir.'/'.translitteration($nom_envoye);
136 $nom_envoye .= '.zip'; # conserver l'extension dans le nom de fichier, par exemple toto.js => toto.js.zip
137 _COMPAT_CFG_192 ? cfg_deplacer_fichier_upload($source, $tmp) : deplacer_fichier_upload($source, $tmp);
138 include_spip('inc/pclzip');
139 $source = _DIR_TMP . 'archive.zip';
140 $archive = new PclZip($source);
141 $v_list = $archive->create($tmp,
142 PCLZIP_OPT_REMOVE_PATH, $tmp_dir,
143 PCLZIP_OPT_ADD_PATH, '');
144 effacer_repertoire_temporaire($tmp_dir);
145 if (!$v_list) {
146 spip_log("Echec creation du zip ");
147 return;
148 }
149 // hum too ?
150 $fichier = cfg_copier_document($ext, $nom_dest.'.zip', $source, $dans);
151 spip_unlink($source);
152 }
153
154 if ($ext == "svg") {
155 // supprimer les scripts
156 traite_svg($fichier);
157 } elseif ($ext != "mov") {// image ?
158 // Si c'est une image, recuperer sa taille et son type (detecte aussi swf)
159 $size_image = @getimagesize($fichier);
160 $type_image = decoder_type_image($size_image[2]);
161 }
162
163 // Quelques infos sur le fichier
164 if (!$fichier OR !@file_exists($fichier)
165 OR !$taille = @intval(filesize($fichier))) {
166 spip_log ("Echec copie du fichier $fichier");
167 return;
168 }
169
170 if (!$type_image) {
171 if (_DOC_MAX_SIZE > 0
172 AND $taille > _DOC_MAX_SIZE*1024) {
173 spip_unlink ($fichier);
174 check_upload_error(6,
175 _T('info_logo_max_poids',
176 array('maxi' => taille_en_octets(_DOC_MAX_SIZE*1024),
177 'actuel' => taille_en_octets($taille))));
178 }
179 }
180 else { // image
181 if (_IMG_MAX_SIZE > 0
182 AND $taille > _IMG_MAX_SIZE*1024) {
183 spip_unlink ($fichier);
184 check_upload_error(6,
185 _T('info_logo_max_poids',
186 array('maxi' => taille_en_octets(_IMG_MAX_SIZE*1024),
187 'actuel' => taille_en_octets($taille))));
188 }
189
190 if (_IMG_MAX_WIDTH * _IMG_MAX_HEIGHT
191 AND ($size_image[0] > _IMG_MAX_WIDTH
192 OR $size_image[1] > _IMG_MAX_HEIGHT)) {
193 spip_unlink ($fichier);
194 check_upload_error(6,
195 _T('info_logo_max_taille',
196 array(
197 'maxi' =>
198 _T('info_largeur_vignette',
199 array('largeur_vignette' => _IMG_MAX_WIDTH,
200 'hauteur_vignette' => _IMG_MAX_HEIGHT)),
201 'actuel' =>
202 _T('info_largeur_vignette',
203 array('largeur_vignette' => $size_image[0],
204 'hauteur_vignette' => $size_image[1]))
205 )));
206 }
207 }
208
209 return $fichier;
210 }
211
212
213 /**
214 * Copier un document
215 *
216 * @param string $ext L'extension du fichier
217 * @param string $dest le nom sous lequel le sauvegarder
218 * @param string $source le fichier sur le serveur (/var/tmp/xyz34)
219 * @param string $dans Où le copier
220 * @return string
221 */
222 function cfg_copier_document($ext, $dest, $source, $dans='_cfg') {
223
224 $dest = preg_replace(',\.\.+,', '.', $dest); // pas de .. dans le nom du doc
225 $dir = cfg_creer_repertoire_cfg($dans);
226 $dest = preg_replace("/[^._=-\w\d]+/", "_",
227 translitteration(preg_replace("/\.([^.]+)$/", "",
228 preg_replace("/<[^>]*>/", '', basename($dest)))));
229
230 // ne pas accepter de noms de la forme -r90.jpg qui sont reserves
231 // pour les images transformees par rotation (action/documenter)
232 $dest = preg_replace(',-r(90|180|270)$,', '', $dest);
233
234 $newFile = $dir . $dest .'.'.$ext;
235
236 return _COMPAT_CFG_192 ? cfg_deplacer_fichier_upload($source, $newFile) : deplacer_fichier_upload($source, $newFile);
237 }
238
239 /**
240 * Creer IMG/config/vue
241 * comme "creer_repertoire_documents" mais avec 2 profondeurs
242 *
243 * @param string $ext
244 * @return string
245 */
246 function cfg_creer_repertoire_cfg($ext) {
247 list($racine, $vue) = explode('/',$ext,2);
248 if ($rep = sous_repertoire(_DIR_IMG, $racine)){
249 $rep = sous_repertoire(_DIR_IMG.$racine, $vue);
250 }
251
252 if (!$ext OR !$rep) {
253 spip_log("creer_repertoire_cfg interdit");
254 exit;
255 }
256
257 // Cette variable de configuration peut etre posee par un plugin
258 // par exemple acces_restreint
259 if ($GLOBALS['meta']["creer_htaccess"] == 'oui') {
260 include_spip('inc/acces');
261 verifier_htaccess($rep);
262 }
263
264 return $rep;
265 }
266
267 /*
268 * compat 1.9.2 :
269 * il y a plein de fonctions qui ont change !!
270 */
271 if (_COMPAT_CFG_192) {
272
273 /**
274 * pas de securite tuante sur .. comme en 1.9.3<br>
275 *
276 * @deprecated depuis SPIP 2.0
277 * @param string $source Le nom du fichier source
278 * @param string $dest Le nom du fichier de destination
279 * @param boolean $move TRUE si on le déplace
280 * @return boolean|string La destination comme 1.9.3
281 */
282 function cfg_deplacer_fichier_upload($source, $dest, $move=false) {
283 // Securite
284 if (substr($dest,0,strlen(_DIR_RACINE))==_DIR_RACINE)
285 $dest = _DIR_RACINE.preg_replace(',\.\.+,', '.', substr($dest,strlen(_DIR_RACINE)));
286 else
287 $dest = preg_replace(',\.\.+,', '.', $dest);
288
289 if ($move) $ok = @rename($source, $dest);
290 else $ok = @copy($source, $dest);
291 if (!$ok) $ok = @move_uploaded_file($source, $dest);
292 if ($ok)
293 @chmod($dest, _SPIP_CHMOD & ~0111);
294 else {
295 $f = @fopen($dest,'w');
296 if ($f) {
297 fclose ($f);
298 } else {
299 include_spip('inc/headers');
300 redirige_par_entete(generer_url_action("test_dirs", "test_dir=". dirname($dest), true));
301 }
302 @unlink($dest);
303 }
304 return $ok ? $dest : false;
305 }
306
307
308 /**
309 * Supprimer le fichier de maniere sympa (flock)
310 *
311 * @deprecated depuis SPIP 2.0
312 * @param string $fichier Le nom du fichier à supprimer
313 * @return boolean
314 */
315 function cfg_supprimer_fichier($fichier) {
316 if (!@file_exists($fichier))
317 return true;
318
319 // verrouiller le fichier destination
320 if ($fp = @fopen($fichier, 'a'))
321 @flock($fp, LOCK_EX);
322 else
323 return false;
324
325 // liberer le verrou
326 @flock($fp, LOCK_UN);
327 @fclose($fp);
328
329 // supprimer
330 return @unlink($fichier);
331 }
332
333
334 if (!function_exists('set_spip_doc')){
335 /**
336 * donne le chemin du fichier relatif a _DIR_IMG<br>
337 * pour stockage 'tel quel' dans la base de donnees
338 *
339 * @deprecated depuis SPIP 2.0
340 * @param string $fichier
341 * @return string
342 */
343 function set_spip_doc($fichier) {
344 if (strpos($fichier, _DIR_IMG) === 0)
345 return substr($fichier, strlen(_DIR_IMG));
346 else
347 return $fichier; // ex: fichier distant
348 }
349 }
350
351
352 if (!function_exists('get_spip_doc')){
353 /**
354 * donne le chemin complet du fichier
355 *
356 * @deprecated depuis SPIP 2.0
357 * @param string $fichier
358 * @return string
359 */
360 function get_spip_doc($fichier) {
361 // fichier distant
362 if (preg_match(',^\w+://,', $fichier))
363 return $fichier;
364
365 // gestion d'erreurs, fichier=''
366 if (!strlen($fichier))
367 return false;
368
369 // fichier normal
370 return (strpos($fichier, _DIR_IMG) === false)
371 ? _DIR_IMG . $fichier
372 : $fichier;
373 }
374 }
375 }
376 ?>