5 * Copyright 1999-2001 (c) VA Linux Systems
6 * Copyright 2002-2004 (c) GForge Team
7 * Copyright 2010 (c) FusionForge Team
8 * Copyright 2013, Franck Villaume - TrivialDev
9 * http://fusionforge.org/
11 * This file is part of FusionForge. FusionForge is free software;
12 * you can redistribute it and/or modify it under the terms of the
13 * GNU General Public License as published by the Free Software
14 * Foundation; either version 2 of the Licence, or (at your option)
17 * FusionForge is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 require_once '../env.inc.php';
30 require_once $gfcommon.'include/pre.php';
31 require_once $gfcommon.'frs/include/frs_utils.php';
32 require_once $gfcommon.'frs/FRSPackage.class.php';
33 require_once $gfcommon.'frs/FRSRelease.class.php';
34 require_once $gfcommon.'frs/FRSFile.class.php';
36 /* This script can work in one of the following modes:
37 * - file: one specific file (given its file_id)
38 * - latestzip: gets a Zip archive containing all files in the latest
39 * release of a given package (given a package_id)
40 * - latestfile: the version of a file that's in the
41 * latest release of a given package (given the file name and the
45 function send_file($filename, $filepath, $file_id = NULL, $mode = NULL) {
46 if (!file_exists($filepath)) {
47 session_redirect404();
50 header('Content-disposition: attachment; filename="'.str_replace('"', '', $filename).'"');
51 sysdebug_off("Content-type: application/binary");
52 $length = filesize($filepath);
53 header("Content-length: $length");
55 readfile_chunked($filepath);
61 if (session_loggedin()) {
62 $s =& session_get_user();
68 $ip = getStringFromServer('REMOTE_ADDR');
69 if ($mode != 'latestzip') {
70 $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));
72 // here $file_id is a package_id
73 $Package = frspackage_get_object($file_id);
74 $release = $Package->getNewestRelease();
75 $files = $release->getFiles();
76 foreach ($files as $fileObject) {
77 $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, $fileObject->getID(), date('Ym'), date('d'), $us));
82 $normalized_urlprefix = normalized_urlprefix();
83 $pathinfo = substr_replace(getStringFromServer('REQUEST_URI'), '', 0, strlen($normalized_urlprefix)-1);
84 $expl_pathinfo = explode('/', $pathinfo);
86 $mode = $expl_pathinfo[3];
90 // .../download.php/file/123/foo.tar.gz
93 $file_id = $expl_pathinfo[4];
94 $File = frsfile_get_object($file_id);
96 session_redirect404();
99 $Release = $File->FRSRelease;
100 $Package = $Release->FRSPackage;
101 $Group = $Package->Group;
102 if ($Package->isPublic()) {
103 session_require_perm('frs', $Package->Group->getID(), 'read_public');
105 session_require_perm('frs', $Package->Group->getID(), 'read_private');
108 $filename = $File->getName();
109 $filepath = forge_get_config('upload_dir').'/'.$Group->getUnixName().'/'.$Package->getFileName().'/'.$Release->getFileName().'/'.$filename;
111 send_file ($filename, $filepath, $file_id);
116 // .../download.php/latestzip/123/package-latest.zip
118 // package-latest.zip ignored
119 $package_id = $expl_pathinfo[4];
121 $Package = frspackage_get_object($package_id);
122 if (!$Package || !$Package->getNewestRelease()) {
123 session_redirect404();
126 if ($Package->isPublic()) {
127 session_require_perm('frs', $Package->Group->getID(), 'read_public');
129 session_require_perm('frs', $Package->Group->getID(), 'read_private');
132 $filename = $Package->getNewestReleaseZipName();
133 $filepath = $Package->getNewestReleaseZipPath();
134 send_file ($filename, $filepath, $package_id, $mode);
139 // .../download.php/latestfile/123/foo.tar.gz
141 // foo.tar.gz -> file name
142 $package_id = $expl_pathinfo[4];
143 $file_name = $expl_pathinfo[5];
145 $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',
146 array($package_id, $file_name));
148 if (!$res || db_numrows($res) < 1) {
149 session_redirect404();
152 $row = db_fetch_array($res);
153 $file_id = $row['file_id'];
154 $File = frsfile_get_object($file_id);
156 $Release = $File->FRSRelease;
157 $Package = $Release->FRSPackage;
158 $Group = $Package->Group;
159 if ($Package->isPublic()) {
160 session_require_perm('frs', $Package->Group->getID(), 'read_public');
162 session_require_perm('frs', $Package->Group->getID(), 'read_private');
165 $filename = $File->getName();
166 $filepath = forge_get_config('upload_dir').'/'.$Group->getUnixName().'/'.$Package->getFileName().'/'.$Release->getFileName().'/'.$filename;
168 send_file ($filename, $filepath, $file_id);
175 // c-file-style: "bsd"