1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
|
<?php
/**
* GForge File Release Facility
*
* Copyright 2002 GForge, LLC
* http://gforge.org/
*
* @version $Id: FRSFile.class 4883 2005-11-03 19:21:27Z danper $
*
* This file is part of GForge.
*
* GForge is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* GForge is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GForge; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
require_once('common/include/Error.class');
class FRSFile extends Error {
/**
* Associative array of data from db.
*
* @var array $data_array.
*/
var $data_array;
/**
* The FRSRelease.
*
* @var object FRSRelease.
*/
var $FRSRelease;
/**
* Constructor.
*
* @param object The FRSRelease object to which this file is associated.
* @param int The file_id.
* @param array The associative array of data.
* @return boolean success.
*/
function FRSFile(&$FRSRelease, $file_id=false, $arr=false) {
$this->Error();
if (!$FRSRelease || !is_object($FRSRelease)) {
$this->setError('FRSFile:: No Valid FRSRelease Object');
return false;
}
if ($FRSRelease->isError()) {
$this->setError('FRSFile:: '.$FRSRelease->getErrorMessage());
return false;
}
$this->FRSRelease =& $FRSRelease;
if ($file_id) {
if (!$arr || !is_array($arr)) {
if (!$this->fetchData($file_id)) {
return false;
}
} else {
$this->data_array =& $arr;
if ($this->data_array['release_id'] != $this->FRSRelease->getID()) {
$this->setError('FRSRelease_id in db result does not match FRSRelease Object');
$this->data_array=null;
return false;
}
}
}
return true;
}
/**
* create - create a new file in this FRSFileRelease/FRSPackage.
*
* @param string The name of this file.
* @param string The location of this file in the local file system.
* @param int The type_id of this file from the frs-file-types table.
* @param int The processor_id of this file from the frs-processor-types table.
* @param int The release_date of this file in unix time (seconds).
* @return boolean success.
*/
function create($name,$file_location,$type_id,$processor_id,$release_time=false) {
global $Language;
if (strlen($name) < 3) {
$this->setError($Language->getText('frs_file','error_min_name_length'));
return false;
}
if (!util_is_valid_filename($name)) {
$this->setError($Language->getText('frs_file','error_name_format'));
return false;
}
//
// Can't really use is_uploaded_file() or move_uploaded_file()
// since we want this to be generalized code
// This is potentially exploitable if you do not validate
// before calling this function
//
if (!is_file($file_location) || !file_exists($file_location)) {
$this->setError($Language->getText('frs_file','error_invalid_file'));
return false;
}
$perm =& $this->FRSRelease->FRSPackage->Group->getPermission( session_get_user() );
if (!$perm || !is_object($perm) || !$perm->isReleaseTechnician()) {
$this->setPermissionDeniedError();
return false;
}
//
// Filename must be unique in this release
//
$resfile=db_query("SELECT filename
FROM frs_file
WHERE
filename='$name'
AND release_id='".$this->FRSRelease->getId()."'");
if (!$resfile || db_numrows($resfile) > 0) {
$this->setError($Language->getText('frs_file','error_already_exists').' '.db_error());
return false;
}
$path_name=$GLOBALS['sys_upload_dir'].'/'.$this->FRSRelease->FRSPackage->Group->getUnixName();
if (!is_dir($path_name)) {
mkdir($path_name,0755);
} else {
if ( fileperms($path_name) != 0x4755 ) {
chmod($path_name,0755);
}
}
$path_name=$path_name.'/'.$this->FRSRelease->FRSPackage->getFileName();
if (!is_dir($path_name)) {
mkdir($path_name,0755);
} else {
if ( fileperms($path_name) != 0x4755 ) {
chmod($path_name,0755);
}
}
$path_name=$path_name.'/'.$this->FRSRelease->getFileName();
if (!is_dir($path_name)) {
mkdir($path_name,0755);
} else {
if ( fileperms($path_name) != 0x4755 ) {
chmod($path_name,0755);
}
}
$file_location=escapeshellcmd($file_location);
$newfilelocation = $GLOBALS['sys_upload_dir'].'/'.
$this->FRSRelease->FRSPackage->Group->getUnixName().'/'.
$this->FRSRelease->FRSPackage->getFileName().'/'.
$this->FRSRelease->getFileName().'/';
//exec("/bin/mkdir $newfilelocation",$out);
//print_r($out);
//exec("/bin/mkdir $newfilelocation",$out);
//print_r($out);
$cmd="/bin/mv $file_location $newfilelocation$name";
exec($cmd,$out);
//echo $cmd;
//print_r($out);
if (!file_exists("$newfilelocation$name")) {
$this->setError($Language->getText('frs_file','error_cannot_move').': '.$newfilelocation.$name);
return false;
}
if (!$release_time) {
$release_time=time();
}
$file_size=filesize("$newfilelocation$name");
$sql="INSERT INTO frs_file(release_id,filename,release_time,
type_id,processor_id,file_size,post_date)
VALUES ('".$this->FRSRelease->getId()."','$name','$release_time',
'$type_id','$processor_id','$file_size','".time()."')";
db_begin();
$result=db_query($sql);
if (!$result) {
db_rollback();
$this->setError('FRSFile::create() Error Adding Release: '.db_error());
return false;
}
$this->file_id=db_insertid($result,'frs_file','file_id');
if (!$this->fetchData($this->file_id)) {
return false;
} else {
db_commit();
return true;
}
}
/**
* fetchData - re-fetch the data for this FRSFile from the database.
*
* @param int The file_id.
* @return boolean success.
*/
function fetchData($file_id) {
$sql="SELECT * FROM frs_file_vw
WHERE file_id='$file_id'
AND release_id='". $this->FRSRelease->getID() ."'";
$res=db_query($sql);
if (!$res || db_numrows($res) < 1) {
$this->setError('FRSFile::fetchData() Invalid file_id');
return false;
}
$this->data_array =& db_fetch_array($res);
db_free_result($res);
return true;
}
/**
* getFRSRelease - get the FRSRelease object this file is associated with.
*
* @return object The FRSRelease object.
*/
function &getFRSRelease() {
return $this->FRSRelease;
}
/**
* getID - get this file_id.
*
* @return int The id of this file.
*/
function getID() {
return $this->data_array['file_id'];
}
/**
* getName - get the name of this file.
*
* @return string The name of this file.
*/
function getName() {
return $this->data_array['filename'];
}
/**
* getSize - get the size of this file.
*
* @return int The size.
*/
function getSize() {
return $this->data_array['size'];
}
/**
* getTypeID - the filetype id.
*
* @return int the filetype id.
*/
function getTypeID() {
return $this->data_array['type_id'];
}
/**
* getTypeName - the filetype name.
*
* @return string The filetype name.
*/
function getFileType() {
return $this->data_array['filetype'];
}
/**
* getProcessorID - the processor id.
*
* @return int the processor id.
*/
function getProcessorID() {
return $this->data_array['processor_id'];
}
/**
* getProcessor - the processor name.
*
* @return string The processor name.
*/
function getProcessor() {
return $this->data_array['processor'];
}
/**
* getDownloads - the number of downloads.
*
* @return int The number of downloads.
*/
function getDownloads() {
return $this->data_array['downloads'];
}
/**
* getReleaseTime - get the releasetime of this file.
*
* @return int The release time in unix time.
*/
function getReleaseTime() {
return $this->data_array['release_time'];
}
/**
* getPostDate - get the post time of this file.
*
* @return int The post time in unix time.
*/
function getPostDate() {
return $this->data_array['post_time'];
}
/**
* delete - Delete this file from the database and file system.
*
* @return boolean success.
*/
function delete() {
$perm =& $this->FRSRelease->FRSPackage->Group->getPermission( session_get_user() );
if (!$perm || !is_object($perm) || !$perm->isReleaseTechnician()) {
$this->setPermissionDeniedError();
return false;
}
$file=$GLOBALS['sys_upload_dir'].'/'.
$this->FRSRelease->FRSPackage->Group->getUnixName() . '/' .
$this->FRSRelease->FRSPackage->getFileName().'/'.
$this->FRSRelease->getFileName().'/'.
$this->getName();
unlink($file);
$result = db_query("DELETE FROM frs_file WHERE file_id='".$this->getID()."'");
if (!$result || db_affected_rows($result) < 1) {
$this->setError("frsDeleteFile()::2 ".db_error());
return false;
} else {
$res=db_query("DELETE FROM frs_dlstats_file WHERE file_id='".$this->getID()."'");
$res=db_query("DELETE FROM frs_dlstats_filetotal_agg WHERE file_id='".$this->getID()."'");
return true;
}
}
/**
* update - update an existing file in this FRSFileRelease/FRSPackage.
*
* @param int The type_id of this file from the frs-file-types table.
* @param int The processor_id of this file from the frs-processor-types table.
* @param int The release_date of this file in unix time (seconds).
* @param int The release_id of the release this file belongs to (if not set, defaults to the release id of this file).
* @return boolean success.
*/
function update($type_id,$processor_id,$release_time,$release_id=false) {
global $Language;
$perm =& $this->FRSRelease->FRSPackage->Group->getPermission( session_get_user() );
if (!$perm || !is_object($perm) || !$perm->isReleaseTechnician()) {
$this->setPermissionDeniedError();
return false;
}
// Sanity checks
if ( $release_id ) {
// Check that the new FRSRelease id exists
if ($FRSRelease=frsrelease_get_object($release_id)) {
// Check that the new FRSRelease id belongs to the group of this FRSFile
if ($FRSRelease->FRSPackage->Group->getID()!=$this->FRSRelease->FRSPackage->Group->getID()) {
$this->setError('FRSFile:: No Valid Group Object');
return false;
}
} else {
$this->setError('FRSFile:: No Valid FRSRelease Object');
return false;
}
} else {
// If release_id is not set, defaults to the release id of this file
$release_id = $this->FRSRelease->getID();
}
// Update database
db_begin();
$res=db_query("UPDATE frs_file SET
type_id='$type_id',
processor_id='$processor_id',
release_time='$release_time',
release_id='$release_id'
WHERE file_id='".$this->getID()."'");
if (!$res || db_affected_rows($res) < 1) {
$this->setError('FRSFile::update() Error On Update: '.db_error());
return false;
}
// Move physically file if needed
if ($release_id != $this->FRSRelease->getID()) {
$old_file_location = $GLOBALS['sys_upload_dir'].'/'.
$this->FRSRelease->FRSPackage->Group->getUnixName().'/'.
$this->FRSRelease->FRSPackage->getFileName().'/'.
$this->FRSRelease->getFileName().'/'.
$this->data_array['filename'];
$new_file_location = $GLOBALS['sys_upload_dir'].'/'.
$FRSRelease->FRSPackage->Group->getUnixName().'/'.
$FRSRelease->FRSPackage->getFileName().'/'.
$FRSRelease->getFileName().'/'.
$this->data_array['filename'];
if (file_exists($new_file_location)) {
db_rollback();
$this->setError($Language->getText('frs_file','error_already_exists'));
return false;
}
$cmd="/bin/mv $old_file_location $new_file_location";
exec($cmd,$out);
if (!file_exists($new_file_location)) {
db_rollback();
$this->setError($Language->getText('frs_file','error_cannot_move').': '.$new_file_location);
return false;
}
}
db_commit();
return true;
}
}
?>
|