5 * Copyright 1999-2001 (c) VA Linux Systems
6 * Copyright 2002-2004 (c) GForge Team
7 * Copyright 2010 (c) FusionForge Team
8 * http://fusionforge.org/
10 * This file is part of FusionForge. FusionForge is free software;
11 * you can redistribute it and/or modify it under the terms of the
12 * GNU General Public License as published by the Free Software
13 * Foundation; either version 2 of the Licence, or (at your option)
16 * FusionForge is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 require_once '../env.inc.php';
29 require_once $gfcommon.'include/pre.php';
30 require_once $gfcommon.'frs/include/frs_utils.php';
31 require_once $gfcommon.'frs/FRSPackage.class.php';
32 require_once $gfcommon.'frs/FRSRelease.class.php';
33 require_once $gfcommon.'frs/FRSFile.class.php';
35 /* This script can work in one of the following modes:
36 * - file: one specific file (given its file_id)
37 * - latestzip: gets a Zip archive containing all files in the latest
38 * release of a given package (given a package_id)
39 * - latestfile: the version of a file that's in the
40 * latest release of a given package (given the file name and the
44 function send_file ($filename,$filepath,$file_id=NULL) {
45 if (!file_exists($filepath)) {
46 session_redirect404();
49 header('Content-disposition: attachment; filename="'.str_replace('"', '', $filename).'"');
50 sysdebug_off("Content-type: application/binary");
51 $length = filesize($filepath);
52 header("Content-length: $length");
54 readfile_chunked($filepath);
60 if (session_loggedin()) {
61 $s =& session_get_user();
67 $ip = getStringFromServer('REMOTE_ADDR');
68 $res = db_query_params("INSERT INTO frs_dlstats_file (ip_address,file_id,month,day,user_id) VALUES ($1, $2, $3, $4, $5)", array($ip,$file_id,date('Ym'),date('d'),$us));
71 $normalized_urlprefix = normalized_urlprefix();
72 $pathinfo = substr_replace(getStringFromServer('REQUEST_URI'), '', 0, strlen($normalized_urlprefix)-1);
73 $expl_pathinfo = explode('/', $pathinfo);
75 $mode = $expl_pathinfo[3];
79 // .../download.php/file/123/foo.tar.gz
82 $file_id = $expl_pathinfo[4];
83 $File = frsfile_get_object($file_id);
85 session_redirect404();
88 $Release = $File->FRSRelease;
89 $Package = $Release->FRSPackage;
90 $Group = $Package->Group;
91 if ($Package->isPublic()) {
92 session_require_perm('frs', $Package->Group->getID(), 'read_public');
94 session_require_perm('frs', $Package->Group->getID(), 'read_private');
97 $filename = $File->getName();
98 $filepath = forge_get_config('upload_dir').'/'.$Group->getUnixName().'/'.$Package->getFileName().'/'.$Release->getFileName().'/'.$filename;
100 send_file ($filename, $filepath, $file_id);
105 // .../download.php/latestzip/123/package-latest.zip
107 // package-latest.zip ignored
108 $package_id = $expl_pathinfo[4];
110 $Package = frspackage_get_object($package_id);
111 if (!$Package || !$Package->getNewestRelease()) {
112 session_redirect404();
115 if ($Package->isPublic()) {
116 session_require_perm('frs', $Package->Group->getID(), 'read_public');
118 session_require_perm('frs', $Package->Group->getID(), 'read_private');
121 $filename = $Package->getNewestReleaseZipName();
122 $filepath = $Package->getNewestReleaseZipPath();
123 send_file ($filename, $filepath);
128 // .../download.php/latestfile/123/foo.tar.gz
130 // foo.tar.gz -> file name
131 $package_id = $expl_pathinfo[4];
132 $file_name = $expl_pathinfo[5];
134 $res = db_query_params ('SELECT f.file_id FROM frs_file f, frs_release r, frs_package p WHERE f.release_id = r.release_id AND r.package_id = p.package_id AND p.package_id = $1 AND f.filename = $2 ORDER BY f.release_id DESC',
135 array($package_id, $file_name));
137 if (!$res || db_numrows($res) < 1) {
138 session_redirect404();
141 $row = db_fetch_array($res);
142 $file_id = $row['file_id'];
143 $File = frsfile_get_object($file_id);
145 $Release = $File->FRSRelease;
146 $Package = $Release->FRSPackage;
147 $Group = $Package->Group;
148 if ($Package->isPublic()) {
149 session_require_perm('frs', $Package->Group->getID(), 'read_public');
151 session_require_perm('frs', $Package->Group->getID(), 'read_private');
154 $filename = $File->getName();
155 $filepath = forge_get_config('upload_dir').'/'.$Group->getUnixName().'/'.$Package->getFileName().'/'.$Release->getFileName().'/'.$filename;
157 send_file ($filename, $filepath, $file_id);
164 // c-file-style: "bsd"