3 * FusionForge file release system
5 * Copyright 2002, Tim Perdue/GForge, LLC
6 * Copyright 2009, Roland Mas
7 * Copyright (C) 2012 Alain Peyrat - Alcatel-Lucent
8 * Copyright 2012-2014, Franck Villaume - TrivialDev
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.
26 require_once $gfcommon.'include/FFError.class.php';
29 * Factory method which creates a FRSFile from an file id
31 * @param int $file_id The file id
32 * @param array|bool $data The result array, if it's passed in
33 * @return object FRSFile object
35 function &frsfile_get_object($file_id, $data=false) {
37 if (!isset($FRSFILE_OBJ['_'.$file_id.'_'])) {
39 //the db result handle was passed in
41 $res = db_query_params('SELECT * FROM frs_file_vw WHERE file_id=$1', array($file_id));
42 if (db_numrows($res) < 1) {
43 $FRSFILE_OBJ['_'.$file_id.'_']=false;
46 $data = db_fetch_array($res);
48 $FRSRelease = frsrelease_get_object($data['release_id']);
49 $FRSFILE_OBJ['_'.$file_id.'_']= new FRSFile($FRSRelease,$data['file_id'], $data);
51 return $FRSFILE_OBJ['_'.$file_id.'_'];
54 class FRSFile extends FFError {
57 * Associative array of data from db.
59 * @var array $data_array.
66 * @var object FRSRelease.
71 * @param object $FRSRelease The FRSRelease object to which this file is associated.
72 * @param int|bool $file_id The file_id.
73 * @param array|bool $arr The associative array of data.
75 function __construct(&$FRSRelease, $file_id=false, $arr=false) {
76 parent::__construct();
77 if (!$FRSRelease || !is_object($FRSRelease)) {
78 $this->setError(_('Invalid FRS Release Object'));
81 if ($FRSRelease->isError()) {
82 $this->setError('FRSFile: '.$FRSRelease->getErrorMessage());
85 $this->FRSRelease =& $FRSRelease;
88 if (!$arr || !is_array($arr)) {
89 if (!$this->fetchData($file_id)) {
93 $this->data_array =& $arr;
94 if ($this->data_array['release_id'] != $this->FRSRelease->getID()) {
95 $this->setError('FRSRelease_id in db result does not match FRSRelease Object');
96 $this->data_array=null;
104 * create - create a new file in this FRSFileRelease/FRSPackage.
106 * @param string $name The name of this file.
107 * @param string $file_location The location of this file in the local file system.
108 * @param int $type_id The type_id of this file from the frs-file-types table.
109 * @param int $processor_id The processor_id of this file from the frs-processor-types table.
110 * @param int|bool $release_time The release_date of this file in unix time (seconds).
111 * @param string $mime_type The mime type of the file (default: application/octet-stream)
112 * @param bool $is_remote True if file is an URL and not an uploaded file (default: false)
113 * @return bool success.
115 function create($name,$file_location,$type_id,$processor_id,$release_time=false) {
116 if (strlen($name) < 3) {
117 $this->setError(_('Name is too short. It must be at least 3 characters.'));
120 if (!util_is_valid_filename($name)) {
121 $this->setError(_('Filename can only be alphanumeric and “-”, “_”, “+”, “.”, “~” characters.'));
125 // Can't really use is_uploaded_file() or move_uploaded_file()
126 // since we want this to be generalized code
127 // This is potentially exploitable if you do not validate
128 // before calling this function
130 if (!is_file($file_location) || !file_exists($file_location)) {
131 $this->setError(_('FRSFile appears to be invalid.'));
135 if (!forge_check_perm('frs', $this->FRSRelease->FRSPackage->getID(), 'file')) {
136 $this->setPermissionDeniedError();
141 // Filename must be unique in this release
143 $resfile = db_query_params('SELECT filename FROM frs_file WHERE filename=$1 AND release_id=$2',
145 $this->FRSRelease->getID()));
146 if (!$resfile || db_numrows($resfile) > 0) {
147 $this->setError(_('That filename already exists in this release').' '.db_error());
151 $path_name = forge_get_config('upload_dir').'/'.$this->FRSRelease->FRSPackage->Group->getUnixName();
152 if (!is_dir($path_name)) {
153 mkdir($path_name, 0755, true);
155 if (fileperms($path_name) != 0x4755) {
156 chmod($path_name, 0755);
159 $path_name = $path_name.'/'.$this->FRSRelease->FRSPackage->getFileName();
160 if (!is_dir($path_name)) {
161 mkdir($path_name, 0755);
163 if (fileperms($path_name) != 0x4755) {
164 chmod($path_name, 0755);
167 $path_name = $path_name.'/'.$this->FRSRelease->getFileName();
168 if (!is_dir($path_name)) {
169 mkdir($path_name, 0755);
171 if (fileperms($path_name) != 0x4755) {
172 chmod($path_name, 0755);
176 $newfilelocation = forge_get_config('upload_dir').'/'.
177 $this->FRSRelease->FRSPackage->Group->getUnixName().'/'.
178 $this->FRSRelease->FRSPackage->getFileName().'/'.
179 $this->FRSRelease->getFileName().'/';
181 $ret = rename($file_location, $newfilelocation.$name);
183 $this->setError(_('File cannot be moved to the permanent location')._(': ').$newfilelocation.$name);
187 if (!$release_time) {
188 $release_time = time();
190 $file_size = filesize($newfilelocation.$name);
193 $result = db_query_params('INSERT INTO frs_file(release_id,filename,release_time,type_id,processor_id,file_size,post_date) VALUES ($1,$2,$3,$4,$5,$6,$7)',
194 array ($this->FRSRelease->getID(),
202 $this->setError(_('Error Adding File')._(': ').db_error());
206 $this->file_id = db_insertid($result, 'frs_file', 'file_id');
207 if (!$this->fetchData($this->file_id)) {
211 if ($this->FRSRelease->FRSPackage->createReleaseFilesAsZip($this->FRSRelease->getID())) {
215 $this->setError($this->FRSRelease->FRSPackage->getErrorMessage());
221 * fetchData - re-fetch the data for this FRSFile from the database.
223 * @param int $file_id The file_id.
224 * @return boolean success.
226 function fetchData($file_id) {
227 $res = db_query_params ('SELECT * FROM frs_file_vw WHERE file_id=$1 AND release_id=$2',
229 $this->FRSRelease->getID())) ;
230 if (!$res || db_numrows($res) < 1) {
231 $this->setError(_('Invalid file_id'));
234 $this->data_array = db_fetch_array($res);
235 db_free_result($res);
240 * getFRSRelease - get the FRSRelease object this file is associated with.
242 * @return object The FRSRelease object.
244 function &getFRSRelease() {
245 return $this->FRSRelease;
249 * getID - get this file_id.
251 * @return int The id of this file.
254 return $this->data_array['file_id'];
258 * getName - get the name of this file.
260 * @return string The name of this file.
263 return $this->data_array['filename'];
267 * getSize - get the size of this file.
269 * @return int The size.
272 return $this->data_array['file_size'];
276 * getTypeID - the filetype id.
278 * @return int the filetype id.
280 function getTypeID() {
281 return $this->data_array['type_id'];
285 * getTypeName - the filetype name.
287 * @return string The filetype name.
289 // FIXME : Undefined index: filetype in .../common/frs/FRSFile.class.php on line 293
290 function getFileType() {
291 return $this->data_array['filetype'];
295 * getProcessorID - the processor id.
297 * @return int the processor id.
299 function getProcessorID() {
300 return $this->data_array['processor_id'];
304 * getProcessor - the processor name.
306 * @return string The processor name.
308 function getProcessor() {
309 return $this->data_array['processor'];
313 * getDownloads - the number of downloads.
315 * @return int The number of downloads.
317 function getDownloads() {
318 return $this->data_array['downloads'];
322 * getReleaseTime - get the releasetime of this file.
324 * @return int The release time in unix time.
326 function getReleaseTime() {
327 return $this->data_array['release_time'];
331 * getPostDate - get the post date of this file.
333 * @return int The post date in unix time.
335 function getPostDate() {
336 return $this->data_array['post_date'];
340 * delete - Delete this file from the database and file system.
342 * @return boolean success.
345 if (!forge_check_perm('frs', $this->FRSRelease->FRSPackage->getID(), 'file')) {
346 $this->setPermissionDeniedError();
350 $file = forge_get_config('upload_dir').'/'.
351 $this->FRSRelease->FRSPackage->Group->getUnixName().'/'.
352 $this->FRSRelease->FRSPackage->getFileName().'/'.
353 $this->FRSRelease->getFileName().'/'.
356 if (file_exists($file))
359 if (isset($this->FRSRelease->release_files[$this->getID()])) {
360 unset($this->FRSRelease->release_files[$this->getID()]);
362 $result = db_query_params('DELETE FROM frs_file WHERE file_id=$1', array($this->getID()));
363 if (!$result || db_affected_rows($result) < 1) {
364 $this->setError("frsDeleteFile()::2 ".db_error());
367 db_query_params('DELETE FROM frs_dlstats_file WHERE file_id=$1', array($this->getID()));
368 db_query_params('DELETE FROM frs_dlstats_filetotal_agg WHERE file_id=$1', array($this->getID()));
369 if ($this->FRSRelease->hasFiles()) {
370 $this->FRSRelease->FRSPackage->createReleaseFilesAsZip($this->FRSRelease->getID());
372 $this->FRSRelease->FRSPackage->deleteReleaseFilesAsZip($this->FRSRelease->getID());
379 * update - update an existing file in this FRSFileRelease/FRSPackage.
381 * @param int $type_id The type_id of this file from the frs-file-types table.
382 * @param int $processor_id The processor_id of this file from the frs-processor-types table.
383 * @param int $release_time The release_date of this file in unix time (seconds).
384 * @param int|bool $release_id The release_id of the release this file belongs to (if not set, defaults to the release id of this file).
385 * @return boolean success.
387 function update($type_id, $processor_id, $release_time, $release_id = false) {
388 if (!forge_check_perm('frs', $this->FRSRelease->FRSPackage->getID(), 'file')) {
389 $this->setPermissionDeniedError();
395 // Check that the new FRSRelease id exists
396 if ($FRSRelease = frsrelease_get_object($release_id)) {
397 // Check that the new FRSRelease id belongs to the group of this FRSFile
398 if ($FRSRelease->FRSPackage->Group->getID() != $this->FRSRelease->FRSPackage->Group->getID()) {
399 $this->setError(_('No Valid Group Object'));
403 $this->setError(_('Invalid FRS Release Object'));
407 // If release_id is not set, defaults to the release id of this file
408 $release_id = $this->FRSRelease->getID();
413 $res = db_query_params('UPDATE frs_file SET type_id=$1,processor_id=$2,release_time=$3,release_id=$4 WHERE file_id=$5',
420 if (!$res || db_affected_rows($res) < 1) {
421 $this->setError(_('Error On Update')._(': ').db_error());
426 // Move physically file if needed
427 if ($release_id != $this->FRSRelease->getID()) {
428 $old_file_location = forge_get_config('upload_dir').'/'.
429 $this->FRSRelease->FRSPackage->Group->getUnixName().'/'.
430 $this->FRSRelease->FRSPackage->getFileName().'/'.
431 $this->FRSRelease->getFileName().'/'.
432 $this->data_array['filename'];
433 $new_file_location = forge_get_config('upload_dir').'/'.
434 $FRSRelease->FRSPackage->Group->getUnixName().'/'.
435 $FRSRelease->FRSPackage->getFileName().'/'.
436 $FRSRelease->getFileName().'/'.
437 $this->data_array['filename'];
438 if (file_exists($new_file_location)) {
439 $this->setError(_('That filename already exists in this project space'));
443 $ret = rename($old_file_location, $new_file_location);
445 $this->setError(_('File cannot be moved to the permanent location')._(': ').$new_file_location);
449 if (isset($this->FRSRelease->release_files[$this->getID()])) {
450 unset($this->FRSRelease->release_files[$this->getID()]);
454 if (!$FRSRelease->FRSPackage->createReleaseFilesAsZip($FRSRelease->getID())) {
455 $this->setError($FRSRelease->FRSPackage->getErrorMessage());
460 if ($this->FRSRelease->FRSPackage->createReleaseFilesAsZip($this->FRSRelease->getID())) {
464 $this->setError($this->FRSRelease->FRSPackage->getErrorMessage());
472 // c-file-style: "bsd"