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
94 $file_id = $expl_pathinfo[4];
96 // Allow alternate content-type rendering by hook
97 $default_content_type = 'application/binary';
99 $script = 'frs_download_file';
100 $content_type = util_negociate_alternate_content_types($script, $default_content_type);
102 if($content_type != $default_content_type) {
103 $hook_params = array();
104 $hook_params['accept'] = $content_type;
105 $hook_params['group_id'] = $group_id;
106 $hook_params['file_id'] = $file_id;
107 $hook_params['return'] = '';
108 $hook_params['content_type'] = '';
109 plugin_hook_by_reference('content_negociated_frs_download_file', $hook_params);
110 if($hook_params['content_type'] != ''){
111 header('Content-type: '. $hook_params['content_type']);
112 echo $hook_params['content'];
115 header('HTTP/1.1 406 Not Acceptable',true,406);
120 $File = frsfile_get_object($file_id);
122 session_redirect404();
125 $Release = $File->FRSRelease;
126 $Package = $Release->FRSPackage;
127 $Group = $Package->Group;
128 if ($Package->isPublic()) {
129 session_require_perm('frs', $Package->Group->getID(), 'read_public');
131 session_require_perm('frs', $Package->Group->getID(), 'read_private');
134 $filename = $File->getName();
135 $filepath = forge_get_config('upload_dir').'/'.$Group->getUnixName().'/'.$Package->getFileName().'/'.$Release->getFileName().'/'.$filename;
137 send_file ($filename, $filepath, $file_id);
142 // .../download.php/latestzip/123/package-latest.zip
144 // package-latest.zip ignored
145 $package_id = $expl_pathinfo[4];
147 $Package = frspackage_get_object($package_id);
148 if (!$Package || !$Package->getNewestRelease()) {
149 session_redirect404();
152 if ($Package->isPublic()) {
153 session_require_perm('frs', $Package->Group->getID(), 'read_public');
155 session_require_perm('frs', $Package->Group->getID(), 'read_private');
158 $filename = $Package->getNewestReleaseZipName();
159 $filepath = $Package->getNewestReleaseZipPath();
160 send_file ($filename, $filepath, $package_id, $mode);
165 // .../download.php/latestfile/123/foo.tar.gz
167 // foo.tar.gz -> file name
168 $package_id = $expl_pathinfo[4];
169 $file_name = $expl_pathinfo[5];
171 $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',
172 array($package_id, $file_name));
174 if (!$res || db_numrows($res) < 1) {
175 session_redirect404();
178 $row = db_fetch_array($res);
179 $file_id = $row['file_id'];
180 $File = frsfile_get_object($file_id);
182 $Release = $File->FRSRelease;
183 $Package = $Release->FRSPackage;
184 $Group = $Package->Group;
185 if ($Package->isPublic()) {
186 session_require_perm('frs', $Package->Group->getID(), 'read_public');
188 session_require_perm('frs', $Package->Group->getID(), 'read_private');
191 $filename = $File->getName();
192 $filepath = forge_get_config('upload_dir').'/'.$Group->getUnixName().'/'.$Package->getFileName().'/'.$Release->getFileName().'/'.$filename;
194 send_file ($filename, $filepath, $file_id);
201 // c-file-style: "bsd"