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