4 * Plugin générique de configuration pour SPIP
10 * @copyright (c) toggg, marcimat 2007-2008
11 * @link http://www.spip-contrib.net/
15 if (!defined("_ECRIRE_INC_VERSION")) return;
18 * si non definie, _COMPAT_CFG_192 vaut "_COMPAT_CFG_192" :(
20 define ('_COMPAT_CFG_192',false);
23 * Vérification du fichier $nom
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"
37 set_request($nom, '<OLD>'. $cfg->val
[$nom]);
43 * Pré-traitement du fichier $nom
49 function cfg_pre_traiter_cfg_fichier($nom, &$cfg){
50 include_spip('inc/flock');
53 $cfg->val
[$nom] = str_replace('<OLD>','', $cfg->val
[$nom]);
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])));
61 // ajout ou modification
63 $f = cfg_get_info_fichier_upload($nom);
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])));
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']));
73 $cfg->val
[$nom] = set_spip_doc($fichier);
87 function cfg_get_info_fichier_upload($nom){
88 return $_FILES ?
$_FILES[$nom] : $GLOBALS['HTTP_POST_FILES'][$nom];
92 * Ajouter un document (au format $_FILES)<br>
93 * (n'ajoute pas le contenu en base dans spip_documents...)
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
101 function cfg_ajoute_un_document($source, $nom_envoye, $nom_dest, $dans='config') {
103 include_spip('inc/modifier');
104 include_spip('inc/ajouter_documents');
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 ?
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'");
119 $type_inclus_image = ($row['inclus'] == 'image');
120 // hum stocke dans IMG/$ext ?
121 $fichier = cfg_copier_document($ext, $nom_dest.'.'.$ext, $source, $dans);
124 /* STOCKER LES DOCUMENTS INCONNUS AU FORMAT .ZIP */
125 $type_inclus_image = false;
127 if (!sql_countsel("spip_types_documents", "extension='zip' AND upload='oui'")) {
128 spip_log("Extension $ext interdite a l'upload");
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);
146 spip_log("Echec creation du zip ");
150 $fichier = cfg_copier_document($ext, $nom_dest.'.zip', $source, $dans);
151 spip_unlink($source);
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]);
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");
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))));
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))));
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',
198 _T('info_largeur_vignette',
199 array('largeur_vignette' => _IMG_MAX_WIDTH
,
200 'hauteur_vignette' => _IMG_MAX_HEIGHT
)),
202 _T('info_largeur_vignette',
203 array('largeur_vignette' => $size_image[0],
204 'hauteur_vignette' => $size_image[1]))
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
222 function cfg_copier_document($ext, $dest, $source, $dans='_cfg') {
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)))));
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);
234 $newFile = $dir . $dest .'.'.$ext;
236 return _COMPAT_CFG_192 ?
cfg_deplacer_fichier_upload($source, $newFile) : deplacer_fichier_upload($source, $newFile);
240 * Creer IMG/config/vue
241 * comme "creer_repertoire_documents" mais avec 2 profondeurs
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);
252 if (!$ext OR !$rep) {
253 spip_log("creer_repertoire_cfg interdit");
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);
269 * il y a plein de fonctions qui ont change !!
271 if (_COMPAT_CFG_192
) {
274 * pas de securite tuante sur .. comme en 1.9.3<br>
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
282 function cfg_deplacer_fichier_upload($source, $dest, $move=false) {
284 if (substr($dest,0,strlen(_DIR_RACINE
))==_DIR_RACINE
)
285 $dest = _DIR_RACINE
.preg_replace(',\.\.+,', '.', substr($dest,strlen(_DIR_RACINE
)));
287 $dest = preg_replace(',\.\.+,', '.', $dest);
289 if ($move) $ok = @rename
($source, $dest);
290 else $ok = @copy
($source, $dest);
291 if (!$ok) $ok = @move_uploaded_file
($source, $dest);
293 @chmod
($dest, _SPIP_CHMOD
& ~
0111);
295 $f = @fopen
($dest,'w');
299 include_spip('inc/headers');
300 redirige_par_entete(generer_url_action("test_dirs", "test_dir=". dirname($dest), true));
304 return $ok ?
$dest : false;
309 * Supprimer le fichier de maniere sympa (flock)
311 * @deprecated depuis SPIP 2.0
312 * @param string $fichier Le nom du fichier à supprimer
315 function cfg_supprimer_fichier($fichier) {
316 if (!@file_exists
($fichier))
319 // verrouiller le fichier destination
320 if ($fp = @fopen
($fichier, 'a'))
321 @flock
($fp, LOCK_EX
);
326 @flock
($fp, LOCK_UN
);
330 return @unlink
($fichier);
334 if (!function_exists('set_spip_doc')){
336 * donne le chemin du fichier relatif a _DIR_IMG<br>
337 * pour stockage 'tel quel' dans la base de donnees
339 * @deprecated depuis SPIP 2.0
340 * @param string $fichier
343 function set_spip_doc($fichier) {
344 if (strpos($fichier, _DIR_IMG
) === 0)
345 return substr($fichier, strlen(_DIR_IMG
));
347 return $fichier; // ex: fichier distant
352 if (!function_exists('get_spip_doc')){
354 * donne le chemin complet du fichier
356 * @deprecated depuis SPIP 2.0
357 * @param string $fichier
360 function get_spip_doc($fichier) {
362 if (preg_match(',^\w+://,', $fichier))
365 // gestion d'erreurs, fichier=''
366 if (!strlen($fichier))
370 return (strpos($fichier, _DIR_IMG
) === false)
371 ? _DIR_IMG
. $fichier