| 1 | <?php |
| 2 | |
| 3 | /***************************************************************************\ |
| 4 | * SPIP, Systeme de publication pour l'internet * |
| 5 | * * |
| 6 | * Copyright (c) 2001-2007 * |
| 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('base/abstract_sql'); |
| 17 | |
| 18 | // acces aux documents joints securise |
| 19 | // est appelee avec arg comme parametre CGI |
| 20 | // mais peu aussi etre appele avec le parametre file directement |
| 21 | // il verifie soit que le demandeur est authentifie |
| 22 | // soit que le fichier est joint a au moins 1 article, breve ou rubrique publie |
| 23 | |
| 24 | // http://doc.spip.org/@action_autoriser_dist |
| 25 | function action_autoriser_dist() |
| 26 | { |
| 27 | global $auteur_session; // positionne par verifier_visiteur dans inc_version |
| 28 | if ($auteur_session['statut'] == '0minirezo' |
| 29 | OR $auteur_session['statut'] == '1comite') |
| 30 | $auth_login = $auteur_session['login']; |
| 31 | else $auth_login = ""; |
| 32 | |
| 33 | $file = rawurldecode(_request('file')); |
| 34 | $arg = rawurldecode(_request('arg')); |
| 35 | |
| 36 | $refus = $dcc = false; |
| 37 | if (strpos($file,'../') !== false) |
| 38 | $refus = 1; |
| 39 | else |
| 40 | { |
| 41 | if (!$arg) { |
| 42 | $arg =spip_query("SELECT id_document, descriptif FROM spip_documents AS documents WHERE documents.fichier=" . _q($file)); |
| 43 | $arg = spip_fetch_array($arg); |
| 44 | if (!$arg) $refus = 2; |
| 45 | $dcc = $arg['descriptif']; |
| 46 | $arg = $arg['id_document']; |
| 47 | } else { |
| 48 | $arg = intval($arg); |
| 49 | $file = spip_query("SELECT fichier, descriptif FROM spip_documents AS documents WHERE id_document='". $arg ."'"); |
| 50 | $file = spip_fetch_array($file); |
| 51 | if (!$file) $refus = 3; |
| 52 | $dcc = $file['descriptif']; |
| 53 | $file = $file['fichier']; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | // Si le document existe et que le visiteur n'est pas redacteur |
| 58 | // chercher un objet publié le referencant |
| 59 | if (!$refus AND !$auth_login) { |
| 60 | $n = spip_num_rows(spip_query("SELECT articles.id_article FROM spip_documents_articles AS rel_articles, spip_articles AS articles WHERE rel_articles.id_article = articles.id_article AND articles.statut = 'publie' AND rel_articles.id_document = $arg LIMIT 1")); |
| 61 | if (!$n) { |
| 62 | $n = spip_num_rows(spip_query("SELECT rubriques.id_rubrique FROM spip_documents_rubriques AS rel_rubriques, spip_rubriques AS rubriques WHERE rel_rubriques.id_rubrique = rubriques.id_rubrique AND rubriques.statut = 'publie' AND rel_rubriques.id_document = $arg LIMIT 1")); |
| 63 | if (!$n) { |
| 64 | $n =spip_num_rows(spip_query("SELECT breves.id_breve FROM spip_documents_breves AS rel_breves, spip_breves AS breves WHERE rel_breves.id_breve = breves.id_breve AND breves.statut = 'publie' AND rel_breves.id_document = $arg LIMIT 1")); |
| 65 | if (!$n) |
| 66 | $refus = 4; } } } |
| 67 | |
| 68 | if (is_int($refus)) { |
| 69 | spip_log("Acces refuse (erreur $refus) au document " . $arg . ': ' . $file); |
| 70 | redirige_par_entete('./?page=404'); |
| 71 | } |
| 72 | else |
| 73 | { |
| 74 | if (!function_exists('mime_content_type')) { |
| 75 | // http://doc.spip.org/@mime_content_type |
| 76 | function mime_content_type($f) {preg_match("/\.(\w+)/",$f,$r); return $r[1];} |
| 77 | } |
| 78 | $ct = mime_content_type($file); |
| 79 | $cl = filesize($file); |
| 80 | $filename = basename($file); |
| 81 | header("Content-Type: ". $ct); |
| 82 | header("Content-Disposition: attachment; filename=\"". $filename ."\";"); |
| 83 | if ($dcc) header("Content-Description: " . $dcc); |
| 84 | if ($cl) header("Content-Length: ". $cl); |
| 85 | |
| 86 | header("Content-Transfer-Encoding: binary"); |
| 87 | readfile($file); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | ?> |