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/FRSPackage.class.php';
31 require_once $gfcommon.'frs/FRSRelease.class.php';
32 require_once $gfcommon.'frs/FRSFile.class.php';
34 /* This script can work in one of the following modes:
35 * - file: one specific file (given its file_id)
36 * - latestzip: gets a Zip archive containing all files in the latest
37 * release of a given package (given a package_id)
38 * - latestfile: the version of a file that's in the
39 * latest release of a given package (given the file name and the
43 function send_file ($filename,$filepath,$file_id=NULL) {
44 if (!file_exists($filepath)) {
45 session_redirect404();
48 header('Content-disposition: attachment; filename="'.str_replace('"', '', $filename).'"');
49 sysdebug_off("Content-type: application/binary");
50 $length = filesize($filepath);
51 header("Content-length: $length");
53 readfile_chunked($filepath);
59 if (session_loggedin()) {
60 $s =& session_get_user();
66 $ip = getStringFromServer('REMOTE_ADDR');
67 $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));
70 $normalized_urlprefix = normalized_urlprefix();
71 $pathinfo = substr_replace(getStringFromServer('REQUEST_URI'), '', 0, strlen($normalized_urlprefix)-1);
72 $expl_pathinfo = explode('/', $pathinfo);
74 $mode = $expl_pathinfo[3];
78 // .../download.php/file/123/foo.tar.gz
81 $file_id = $expl_pathinfo[4];
82 $File = frsfile_get_object($file_id);
84 session_redirect404();
87 $Release = $File->FRSRelease;
88 $Package = $Release->FRSPackage;
89 $Group = $Package->Group;
90 if ($Package->isPublic()) {
91 session_require_perm('frs', $Package->Group->getID(), 'read_public');
93 session_require_perm('frs', $Package->Group->getID(), 'read_private');
96 $filename = $File->getName();
97 $filepath = forge_get_config('upload_dir').'/'.$Group->getUnixName().'/'.$Package->getFileName().'/'.$Release->getFileName().'/'.$filename;
99 send_file ($filename, $filepath, $file_id);
104 // .../download.php/latestzip/123/package-latest.zip
106 // package-latest.zip ignored
107 $package_id = $expl_pathinfo[4];
109 $Package = frspackage_get_object($package_id);
110 if (!$Package || !$Package->getNewestRelease()) {
111 session_redirect404();
114 if ($Package->isPublic()) {
115 session_require_perm('frs', $Package->Group->getID(), 'read_public');
117 session_require_perm('frs', $Package->Group->getID(), 'read_private');
120 $filename = $Package->getNewestReleaseZipName();
121 $filepath = $Package->getNewestReleaseZipPath();
122 send_file ($filename, $filepath);
127 // .../download.php/latestfile/123/foo.tar.gz
129 // foo.tar.gz -> file name
130 $package_id = $expl_pathinfo[4];
131 $file_name = $expl_pathinfo[5];
133 $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',
134 array($package_id, $file_name));
136 if (!$res || db_numrows($res) < 1) {
137 session_redirect404();
140 $row = db_fetch_array($res);
141 $file_id = $row['file_id'];
142 $File = frsfile_get_object($file_id);
144 $Release = $File->FRSRelease;
145 $Package = $Release->FRSPackage;
146 $Group = $Package->Group;
147 if ($Package->isPublic()) {
148 session_require_perm('frs', $Package->Group->getID(), 'read_public');
150 session_require_perm('frs', $Package->Group->getID(), 'read_private');
153 $filename = $File->getName();
154 $filepath = forge_get_config('upload_dir').'/'.$Group->getUnixName().'/'.$Package->getFileName().'/'.$Release->getFileName().'/'.$filename;
156 send_file ($filename, $filepath, $file_id);
163 // c-file-style: "bsd"