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,2021, 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 $data The result array, if it's passed in
33 * @return object|bool FRSFile object, false in case of error
35 function &frsfile_get_object($file_id, $data = array()) {
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 $arr The associative array of data.
75 function __construct(&$FRSRelease, $file_id = false, $arr = array()) {
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 $this->fetchData($file_id);
91 $this->data_array =& $arr;
92 if ($this->data_array['release_id'] != $this->FRSRelease->getID()) {
93 $this->setError('FRSRelease_id in db result does not match FRSRelease Object');
94 $this->data_array = null;
101 * create - create a new file in this FRSFileRelease/FRSPackage.
103 * @param string $name The name of this file.
104 * @param string $file_location The location of this file in the local file system.
105 * @param int $type_id The type_id of this file from the frs-file-types table.
106 * @param int $processor_id The processor_id of this file from the frs-processor-types table.
107 * @param int|bool $release_time The release_date of this file in unix time (seconds).
108 * @return bool success.
110 function create($name,$file_location,$type_id,$processor_id,$release_time=false) {
111 if (strlen($name) < 3) {
112 $this->setError(_('Name is too short. It must be at least 3 characters.'));
115 if (!util_is_valid_filename($name)) {
116 $this->setError(_('Filename can only be alphanumeric and “-”, “_”, “+”, “.”, “~” characters.'));
120 // Can't really use is_uploaded_file() or move_uploaded_file()
121 // since we want this to be generalized code
122 // This is potentially exploitable if you do not validate
123 // before calling this function
125 if (!is_file($file_location) || !file_exists($file_location)) {
126 $this->setError(_('FRSFile appears to be invalid.'));
130 if (!forge_check_perm('frs', $this->FRSRelease->FRSPackage->getID(), 'file')) {
131 $this->setPermissionDeniedError();
136 // Filename must be unique in this release
138 $resfile = db_query_params('SELECT filename FROM frs_file WHERE filename=$1 AND release_id=$2',
140 $this->FRSRelease->getID()));
141 if (!$resfile || db_numrows($resfile) > 0) {
142 $this->setError(_('That filename already exists in this release').' '.db_error());
146 $path_name = forge_get_config('upload_dir').'/'.$this->FRSRelease->FRSPackage->Group->getUnixName();
147 if (!is_dir($path_name)) {
148 mkdir($path_name, 0755, true);
150 if (fileperms($path_name) != 0x4755) {
151 chmod($path_name, 0755);
154 $path_name = $path_name.'/'.$this->FRSRelease->FRSPackage->getFileName();
155 if (!is_dir($path_name)) {
156 mkdir($path_name, 0755);
158 if (fileperms($path_name) != 0x4755) {
159 chmod($path_name, 0755);
162 $path_name = $path_name.'/'.$this->FRSRelease->getFileName();
163 if (!is_dir($path_name)) {
164 mkdir($path_name, 0755);
166 if (fileperms($path_name) != 0x4755) {
167 chmod($path_name, 0755);
171 $newfilelocation = forge_get_config('upload_dir').'/'.
172 $this->FRSRelease->FRSPackage->Group->getUnixName().'/'.
173 $this->FRSRelease->FRSPackage->getFileName().'/'.
174 $this->FRSRelease->getFileName().'/';
176 $ret = rename($file_location, $newfilelocation.$name);
178 $this->setError(_('File cannot be moved to the permanent location')._(': ').$newfilelocation.$name);
182 if (!$release_time) {
183 $release_time = time();
185 $file_size = filesize($newfilelocation.$name);
188 $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)',
189 array ($this->FRSRelease->getID(),
197 $this->setError(_('Error Adding File')._(': ').db_error());
201 $this->file_id = db_insertid($result, 'frs_file', 'file_id');
202 if (!$this->fetchData($this->file_id)) {
206 if ($this->FRSRelease->FRSPackage->createReleaseFilesAsZip($this->FRSRelease->getID())) {
210 $this->setError($this->FRSRelease->FRSPackage->getErrorMessage());
216 * fetchData - re-fetch the data for this FRSFile from the database.
218 * @param int $file_id The file_id.
219 * @return bool success.
221 function fetchData($file_id) {
222 $res = db_query_params ('SELECT * FROM frs_file_vw WHERE file_id=$1 AND release_id=$2',
224 $this->FRSRelease->getID())) ;
225 if (!$res || db_numrows($res) < 1) {
226 $this->setError(_('Invalid file_id'));
229 $this->data_array = db_fetch_array($res);
230 db_free_result($res);
235 * getFRSRelease - get the FRSRelease object this file is associated with.
237 * @return object The FRSRelease object.
239 function &getFRSRelease() {
240 return $this->FRSRelease;
244 * getID - get this file_id.
246 * @return int The id of this file.
249 return $this->data_array['file_id'];
253 * getName - get the name of this file.
255 * @return string The name of this file.
258 return $this->data_array['filename'];
262 * getSize - get the size of this file.
264 * @return int The size.
267 return $this->data_array['file_size'];
271 * getTypeID - the filetype id.
273 * @return int the filetype id.
275 function getTypeID() {
276 return $this->data_array['type_id'];
280 * getTypeName - the filetype name.
282 * @return string The filetype name.
284 // FIXME : Undefined index: filetype in .../common/frs/FRSFile.class.php on line 293
285 function getFileType() {
286 return $this->data_array['filetype'];
290 * getProcessorID - the processor id.
292 * @return int the processor id.
294 function getProcessorID() {
295 return $this->data_array['processor_id'];
299 * getProcessor - the processor name.
301 * @return string The processor name.
303 function getProcessor() {
304 return $this->data_array['processor'];
308 * getDownloads - the number of downloads.
310 * @return int The number of downloads.
312 function getDownloads() {
313 return $this->data_array['downloads'];
317 * getReleaseTime - get the releasetime of this file.
319 * @return int The release time in unix time.
321 function getReleaseTime() {
322 return $this->data_array['release_time'];
326 * getPostDate - get the post date of this file.
328 * @return int The post date in unix time.
330 function getPostDate() {
331 return $this->data_array['post_date'];
335 * delete - Delete this file from the database and file system.
337 * @return bool success.
340 if (!forge_check_perm('frs', $this->FRSRelease->FRSPackage->getID(), 'file')) {
341 $this->setPermissionDeniedError();
345 $file = forge_get_config('upload_dir').'/'.
346 $this->FRSRelease->FRSPackage->Group->getUnixName().'/'.
347 $this->FRSRelease->FRSPackage->getFileName().'/'.
348 $this->FRSRelease->getFileName().'/'.
351 if (file_exists($file)) {
354 if (isset($this->FRSRelease->release_files[$this->getID()])) {
355 unset($this->FRSRelease->release_files[$this->getID()]);
357 $result = db_query_params('DELETE FROM frs_file WHERE file_id=$1', array($this->getID()));
358 if (!$result || db_affected_rows($result) < 1) {
359 $this->setError("frsDeleteFile()::2 ".db_error());
362 db_query_params('DELETE FROM frs_dlstats_file WHERE file_id=$1', array($this->getID()));
363 db_query_params('DELETE FROM frs_dlstats_filetotal_agg WHERE file_id=$1', array($this->getID()));
364 if ($this->FRSRelease->hasFiles()) {
365 $this->FRSRelease->FRSPackage->createReleaseFilesAsZip($this->FRSRelease->getID());
367 $this->FRSRelease->FRSPackage->deleteReleaseFilesAsZip($this->FRSRelease->getID());
374 * update - update an existing file in this FRSFileRelease/FRSPackage.
376 * @param int $type_id The type_id of this file from the frs-file-types table.
377 * @param int $processor_id The processor_id of this file from the frs-processor-types table.
378 * @param int $release_time The release_date of this file in unix time (seconds).
379 * @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).
380 * @return bool success.
382 function update($type_id, $processor_id, $release_time, $release_id = false) {
383 if (!forge_check_perm('frs', $this->FRSRelease->FRSPackage->getID(), 'file')) {
384 $this->setPermissionDeniedError();
390 // Check that the new FRSRelease id exists
391 if ($FRSRelease = frsrelease_get_object($release_id)) {
392 // Check that the new FRSRelease id belongs to the group of this FRSFile
393 if ($FRSRelease->FRSPackage->Group->getID() != $this->FRSRelease->FRSPackage->Group->getID()) {
394 $this->setError(_('Invalid Project'));
398 $this->setError(_('Invalid FRS Release Object'));
402 // If release_id is not set, defaults to the release id of this file
403 $release_id = $this->FRSRelease->getID();
408 $res = db_query_params('UPDATE frs_file SET type_id=$1,processor_id=$2,release_time=$3,release_id=$4 WHERE file_id=$5',
415 if (!$res || db_affected_rows($res) < 1) {
416 $this->setError(_('Error On Update')._(': ').db_error());
421 // Move physically file if needed
422 if ($release_id != $this->FRSRelease->getID()) {
423 $old_file_location = forge_get_config('upload_dir').'/'.
424 $this->FRSRelease->FRSPackage->Group->getUnixName().'/'.
425 $this->FRSRelease->FRSPackage->getFileName().'/'.
426 $this->FRSRelease->getFileName().'/'.
427 $this->data_array['filename'];
428 $new_file_location = forge_get_config('upload_dir').'/'.
429 $FRSRelease->FRSPackage->Group->getUnixName().'/'.
430 $FRSRelease->FRSPackage->getFileName().'/'.
431 $FRSRelease->getFileName().'/'.
432 $this->data_array['filename'];
433 if (file_exists($new_file_location)) {
434 $this->setError(_('That filename already exists in this project space'));
438 $ret = rename($old_file_location, $new_file_location);
440 $this->setError(_('File cannot be moved to the permanent location')._(': ').$new_file_location);
444 if (isset($this->FRSRelease->release_files[$this->getID()])) {
445 unset($this->FRSRelease->release_files[$this->getID()]);
449 if (!$FRSRelease->FRSPackage->createReleaseFilesAsZip($FRSRelease->getID())) {
450 $this->setError($FRSRelease->FRSPackage->getErrorMessage());
455 if ($this->FRSRelease->FRSPackage->createReleaseFilesAsZip($this->FRSRelease->getID())) {
459 $this->setError($this->FRSRelease->FRSPackage->getErrorMessage());
467 // c-file-style: "bsd"