3 * FusionForge file release system
5 * Copyright 2002, Tim Perdue/GForge, LLC
6 * Copyright 2009, Roland Mas
8 * This file is part of FusionForge.
10 * FusionForge is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published
12 * by the Free Software Foundation; either version 2 of the License,
13 * or (at your option) any later version.
15 * FusionForge is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with FusionForge; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 require_once $gfcommon.'include/Error.class.php';
27 require_once $gfcommon.'frs/FRSFile.class.php';
30 * Factory method which creates a FRSRelease from an release id
32 * @param int The release id
33 * @param array The result array, if it's passed in
34 * @return object FRSRelease object
36 function &frsrelease_get_object($release_id, $data=false) {
37 global $FRSRELEASE_OBJ;
38 if (!isset($FRSRELEASE_OBJ['_'.$release_id.'_'])) {
40 //the db result handle was passed in
42 $res = db_query_params ('SELECT * FROM frs_release WHERE release_id=$1',
43 array ($release_id)) ;
44 if (db_numrows($res)<1 ) {
45 $FRSRELEASE_OBJ['_'.$release_id.'_']=false;
48 $data =& db_fetch_array($res);
50 $FRSPackage =& frspackage_get_object($data['package_id']);
51 $FRSRELEASE_OBJ['_'.$release_id.'_']= new FRSRelease($FRSPackage,$data['release_id'],$data);
53 return $FRSRELEASE_OBJ['_'.$release_id.'_'];
56 class FRSRelease extends Error {
59 * Associative array of data from db.
61 * @var array $data_array.
68 * @var object FRSPacakge.
76 * @param object The FRSPackage object to which this release is associated.
77 * @param int The release_id.
78 * @param array The associative array of data.
79 * @return boolean success.
81 function FRSRelease(&$FRSPackage, $release_id=false, $arr=false) {
83 if (!$FRSPackage || !is_object($FRSPackage)) {
84 $this->setError('FRSRelease:: No Valid FRSPackage Object');
87 if ($FRSPackage->isError()) {
88 $this->setError('FRSRelease:: '.$FRSPackage->getErrorMessage());
91 $this->FRSPackage =& $FRSPackage;
94 if (!$arr || !is_array($arr)) {
95 if (!$this->fetchData($release_id)) {
99 $this->data_array =& $arr;
100 if ($this->data_array['package_id'] != $this->FRSPackage->getID()) {
101 $this->setError('FRSPackage_id in db result does not match FRSPackage Object');
102 $this->data_array=null;
111 * create - create a new release in the database.
113 * @param string The name of the release.
114 * @param string The release notes for the release.
115 * @param string The change log for the release.
116 * @param int Whether the notes/log are preformatted with \n chars (1) true (0) false.
117 * @param int The unix date of the release.
118 * @return boolean success.
120 function create($name,$notes,$changes,$preformatted,$release_date=false) {
121 if (strlen($name) < 3) {
122 $this->setError(_('FRSPackage Name Must Be At Least 3 Characters'));
132 $perm =& $this->FRSPackage->Group->getPermission( session_get_user() );
134 if (!$perm || !is_object($perm) || !$perm->isReleaseTechnician()) {
135 $this->setPermissionDeniedError();
139 if (!$release_date) {
140 $release_date=time();
142 $res = db_query_params ('SELECT * FROM frs_release WHERE package_id=$1 AND name=$2',
143 array ($this->FRSPackage->getID(),
144 htmlspecialchars($name))) ;
145 if (db_numrows($res)) {
146 $this->setError('FRSRelease::create() Error Adding Release: Name Already Exists');
151 $result=db_query_params ('INSERT INTO frs_release(package_id,notes,changes,preformatted,name,release_date,released_by,status_id) VALUES ($1,$2,$3,$4,$5,$6,$7,$8)',
152 array ($this->FRSPackage->getId(),
153 htmlspecialchars($notes),
154 htmlspecialchars($changes),
156 htmlspecialchars($name),
162 $this->setError('FRSRelease::create() Error Adding Release: '.db_error());
165 $this->release_id=db_insertid($result,'frs_release','release_id');
166 if (!$this->fetchData($this->release_id)) {
170 $newdirlocation = $GLOBALS['sys_upload_dir'].'/'.$this->FRSPackage->Group->getUnixName().'/'.$this->FRSPackage->getFileName().'/'.$this->getFileName();
171 if (!is_dir($newdirlocation)) {
172 @mkdir($newdirlocation);
180 * fetchData - re-fetch the data for this Release from the database.
182 * @param int The release_id.
183 * @return boolean success.
185 function fetchData($release_id) {
186 $res = db_query_params ('SELECT * FROM frs_release WHERE release_id=$1 AND package_id=$2',
188 $this->FRSPackage->getID())) ;
189 if (!$res || db_numrows($res) < 1) {
190 $this->setError('FRSRelease::fetchData() Invalid release_id');
193 $this->data_array =& db_fetch_array($res);
194 db_free_result($res);
199 * getFRSPackage - get the FRSPackage object this release is associated with.
201 * @return object The FRSPackage object.
203 function &getFRSPackage() {
204 return $this->FRSPackage;
208 * getID - get this release_id.
210 * @return int The id of this release.
213 return $this->data_array['release_id'];
217 * getName - get the name of this release.
219 * @return string The name of this release.
222 return $this->data_array['name'];
226 * getFileName - get the filename of this release.
228 * @return string The filename of this release.
230 function getFileName() {
231 return eregi_replace("[^-A-Z0-9_\.]",'',$this->data_array['name']);
235 * getStatus - get the status of this release.
237 * @return int The status.
239 function getStatus() {
240 return $this->data_array['status_id'];
244 * getNotes - get the release notes of this release.
246 * @return string The release notes.
248 function getNotes() {
249 return $this->data_array['notes'];
253 * getChanges - get the changelog of this release.
255 * @return string The changelog.
257 function getChanges() {
258 return $this->data_array['changes'];
262 * getPreformatted - get the preformatted option of this release.
264 * @return boolean preserve_formatting.
266 function getPreformatted() {
267 return $this->data_array['preformatted'];
271 * getReleaseDate - get the releasedate of this release.
273 * @return int The release date in unix time.
275 function getReleaseDate() {
276 return $this->data_array['release_date'];
280 * sendNotice - the logic to send an email/jabber notice for a release.
282 * @return boolean success.
284 function sendNotice() {
285 $arr =& $this->FRSPackage->getMonitorIDs();
287 $date = date('Y-m-d H:i',time());
289 $subject = sprintf (_('[%1$s Release] %2$s'),
290 $this->FRSPackage->Group->getUnixName(),
291 $this->FRSPackage->getName());
292 $text = stripcslashes(sprintf(_('Project %1$s (%2$s) has released a new version of package "%3$s".
293 You can download it by following this link:
297 You receive this email because you requested to be notified when new
298 versions of this package were released. If you don\'t wish to be
299 notified in the future, please login to %5$s and click this link:
302 $this->FRSPackage->Group->getPublicName(),
303 $this->FRSPackage->Group->getUnixName(),
304 $this->FRSPackage->getName(),
305 util_make_url ("/frs/?group_id=". $this->FRSPackage->Group->getID() ."&release_id=". $this->getID()),
306 $GLOBALS['sys_name'],
307 util_make_url ("/frs/monitor.php?filemodule_id=".$this->FRSPackage->getID()."&group_id=".$this->FRSPackage->Group->getID()."&stop=1")));
308 // $text = util_line_wrap($text);
310 util_handle_message(array_unique($arr),$subject,$text);
316 * getFiles - gets all the file objects for files in this release.
318 * return array Array of FRSFile Objects.
320 function &getFiles() {
321 if (!is_array($this->release_files) || count($this->release_files) < 1) {
322 $this->release_files=array();
323 $res = db_query_params ('SELECT * FROM frs_file_vw WHERE release_id=$1',
324 array ($this->getID())) ;
325 while ($arr = db_fetch_array($res)) {
326 $this->release_files[]=new FRSFile($this,$arr['file_id'],$arr);
329 return $this->release_files;
333 * delete - delete this release and all its related data.
335 * @param bool I'm Sure.
336 * @param bool I'm REALLY sure.
337 * @return bool true/false;
339 function delete($sure, $really_sure) {
340 if (!$sure || !$really_sure) {
341 $this->setMissingParamsError();
344 $perm =& $this->FRSPackage->Group->getPermission( session_get_user() );
346 if (!$perm || !is_object($perm) || !$perm->isReleaseTechnician()) {
347 $this->setPermissionDeniedError();
350 $f =& $this->getFiles();
351 for ($i=0; $i<count($f); $i++) {
352 if (!is_object($f[$i]) || $f[$i]->isError() || !$f[$i]->delete()) {
353 $this->setError('File Error: '.$f[$i]->getName().':'.$f[$i]->getErrorMessage());
357 $dir=$GLOBALS['sys_upload_dir'].'/'.
358 $this->FRSPackage->Group->getUnixName() . '/' .
359 $this->FRSPackage->getFileName().'/'.
360 $this->getFileName().'/';
362 // double-check we're not trying to remove root dir
363 if (util_is_root_dir($dir)) {
364 $this->setError('Release::delete error: trying to delete root dir');
367 exec('rm -rf '.$dir);
369 db_query_params ('DELETE FROM frs_release WHERE release_id=$1 AND package_id=$2',
370 array ($this->getID(),
371 $this->FRSPackage->getID())) ;
376 * update - update a new release in the database.
378 * @param int The status of this release from the frs_status table.
379 * @param string The name of the release.
380 * @param string The release notes for the release.
381 * @param string The change log for the release.
382 * @param int Whether the notes/log are preformatted with \n chars (1) true (0) false.
383 * @param int The unix date of the release.
384 * @return boolean success.
386 function update($status,$name,$notes,$changes,$preformatted,$release_date) {
387 if (strlen($name) < 3) {
388 $this->setError(_('FRSPackage Name Must Be At Least 3 Characters'));
392 $perm =& $this->FRSPackage->Group->getPermission( session_get_user() );
394 if (!$perm || !is_object($perm) || !$perm->isReleaseTechnician()) {
395 $this->setPermissionDeniedError();
405 if($this->getName()!=htmlspecialchars($name)) {
406 $res = db_query_params ('SELECT * FROM frs_release WHERE package_id=$1 AND name=$2',
407 array ($this->FRSPackage->getID(),
408 htmlspecialchars($name))) ;
409 if (db_numrows($res)) {
410 $this->setError('FRSRelease::update() Error On Update: Name Already Exists');
415 $res = db_query_params ('UPDATE frs_release SET name=$1,status_id=$2,notes=$3,
416 changes=$4,preformatted=$5,release_date=$6,released_by=$7
417 WHERE package_id=$8 AND release_id=$9',
418 array (htmlspecialchars($name),
420 htmlspecialchars($notes),
421 htmlspecialchars($changes),
425 $this->FRSPackage->getID(),
428 if (!$res || db_affected_rows($res) < 1) {
430 $this->setError('FRSRelease::update() Error On Update: '.db_error());
434 $oldfilename = $this->getFileName();
435 if(!$this->fetchData($this->getID())){
437 $this->setError("FRSRelease::update() Error Updating Release: Couldn't fetch data");
440 $newfilename = $this->getFileName();
441 $olddirlocation = $GLOBALS['sys_upload_dir'].'/'.$this->FRSPackage->Group->getUnixName().'/'.$this->FRSPackage->getFileName().'/'.$oldfilename;
442 $newdirlocation = $GLOBALS['sys_upload_dir'].'/'.$this->FRSPackage->Group->getUnixName().'/'.$this->FRSPackage->getFileName().'/'.$newfilename;
444 if (($oldfilename!=$newfilename) && is_dir($olddirlocation)) {
445 if (is_dir($newdirlocation)) {
447 $this->setError('FRSRelease::update() Error Updating Release: Directory Already Exists');
450 if(!rename($olddirlocation,$newdirlocation)) {
452 $this->setError("FRSRelease::update() Error Updating Release: Couldn't rename dir");
465 // c-file-style: "bsd"