3 * FusionForge file release system
5 * Copyright 2002, Tim Perdue/GForge, LLC
7 * This file is part of FusionForge.
9 * FusionForge is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published
11 * by the Free Software Foundation; either version 2 of the License,
12 * or (at your option) any later version.
14 * FusionForge is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with FusionForge; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
25 require_once $gfcommon.'include/Error.class.php';
26 require_once $gfcommon.'frs/FRSFile.class.php';
29 * Factory method which creates a FRSRelease from an release id
31 * @param int The release id
32 * @param array The result array, if it's passed in
33 * @return object FRSRelease object
35 function &frsrelease_get_object($release_id, $data=false) {
36 global $FRSRELEASE_OBJ;
37 if (!isset($FRSRELEASE_OBJ['_'.$release_id.'_'])) {
39 //the db result handle was passed in
41 $res=db_query("SELECT * FROM frs_release WHERE
42 release_id='$release_id'");
43 if (db_numrows($res)<1 ) {
44 $FRSRELEASE_OBJ['_'.$release_id.'_']=false;
47 $data =& db_fetch_array($res);
49 $FRSPackage =& frspackage_get_object($data['package_id']);
50 $FRSRELEASE_OBJ['_'.$release_id.'_']= new FRSRelease($FRSPackage,$data['release_id'],$data);
52 return $FRSRELEASE_OBJ['_'.$release_id.'_'];
55 class FRSRelease extends Error {
58 * Associative array of data from db.
60 * @var array $data_array.
67 * @var object FRSPacakge.
75 * @param object The FRSPackage object to which this release is associated.
76 * @param int The release_id.
77 * @param array The associative array of data.
78 * @return boolean success.
80 function FRSRelease(&$FRSPackage, $release_id=false, $arr=false) {
82 if (!$FRSPackage || !is_object($FRSPackage)) {
83 $this->setError('FRSRelease:: No Valid FRSPackage Object');
86 if ($FRSPackage->isError()) {
87 $this->setError('FRSRelease:: '.$FRSPackage->getErrorMessage());
90 $this->FRSPackage =& $FRSPackage;
93 if (!$arr || !is_array($arr)) {
94 if (!$this->fetchData($release_id)) {
98 $this->data_array =& $arr;
99 if ($this->data_array['package_id'] != $this->FRSPackage->getID()) {
100 $this->setError('FRSPackage_id in db result does not match FRSPackage Object');
101 $this->data_array=null;
110 * create - create a new release in the database.
112 * @param string The name of the release.
113 * @param string The release notes for the release.
114 * @param string The change log for the release.
115 * @param int Whether the notes/log are preformatted with \n chars (1) true (0) false.
116 * @param int The unix date of the release.
117 * @return boolean success.
119 function create($name,$notes,$changes,$preformatted,$release_date=false) {
120 if (strlen($name) < 3) {
121 $this->setError(_('FRSPackage Name Must Be At Least 3 Characters'));
131 $perm =& $this->FRSPackage->Group->getPermission( session_get_user() );
133 if (!$perm || !is_object($perm) || !$perm->isReleaseTechnician()) {
134 $this->setPermissionDeniedError();
138 if (!$release_date) {
139 $release_date=time();
141 $res=db_query("SELECT * FROM frs_release WHERE package_id='".$this->FRSPackage->getID()."'
142 AND name='".htmlspecialchars($name)."'");
143 if (db_numrows($res)) {
144 $this->setError('FRSRelease::create() Error Adding Release: Name Already Exists');
148 $sql="INSERT INTO frs_release(package_id,notes,changes,
149 preformatted,name,release_date,released_by,status_id)
150 VALUES ('".$this->FRSPackage->getId()."','".htmlspecialchars($notes)."','".htmlspecialchars($changes)."',
151 '$preformatted','".htmlspecialchars($name)."','$release_date','".user_getid()."','1')";
154 $result=db_query($sql);
157 $this->setError('FRSRelease::create() Error Adding Release: '.db_error());
160 $this->release_id=db_insertid($result,'frs_release','release_id');
161 if (!$this->fetchData($this->release_id)) {
165 $newdirlocation = $GLOBALS['sys_upload_dir'].'/'.$this->FRSPackage->Group->getUnixName().'/'.$this->FRSPackage->getFileName().'/'.$this->getFileName();
166 exec("/bin/mkdir $newdirlocation",$out);
173 * fetchData - re-fetch the data for this Release from the database.
175 * @param int The release_id.
176 * @return boolean success.
178 function fetchData($release_id) {
179 $sql="SELECT * FROM frs_release
180 WHERE release_id='$release_id'
181 AND package_id='". $this->FRSPackage->getID() ."'";
183 if (!$res || db_numrows($res) < 1) {
184 $this->setError('FRSRelease::fetchData() Invalid release_id');
187 $this->data_array =& db_fetch_array($res);
188 db_free_result($res);
193 * getFRSPackage - get the FRSPackage object this release is associated with.
195 * @return object The FRSPackage object.
197 function &getFRSPackage() {
198 return $this->FRSPackage;
202 * getID - get this release_id.
204 * @return int The id of this release.
207 return $this->data_array['release_id'];
211 * getName - get the name of this release.
213 * @return string The name of this release.
216 return $this->data_array['name'];
220 * getFileName - get the filename of this release.
222 * @return string The filename of this release.
224 function getFileName() {
225 return eregi_replace("[^-A-Z0-9_\.]",'',$this->data_array['name']);
229 * getStatus - get the status of this release.
231 * @return int The status.
233 function getStatus() {
234 return $this->data_array['status_id'];
238 * getNotes - get the release notes of this release.
240 * @return string The release notes.
242 function getNotes() {
243 return $this->data_array['notes'];
247 * getChanges - get the changelog of this release.
249 * @return string The changelog.
251 function getChanges() {
252 return $this->data_array['changes'];
256 * getPreformatted - get the preformatted option of this release.
258 * @return boolean preserve_formatting.
260 function getPreformatted() {
261 return $this->data_array['preformatted'];
265 * getReleaseDate - get the releasedate of this release.
267 * @return int The release date in unix time.
269 function getReleaseDate() {
270 return $this->data_array['release_date'];
274 * sendNotice - the logic to send an email/jabber notice for a release.
276 * @return boolean success.
278 function sendNotice() {
279 $arr =& $this->FRSPackage->getMonitorIDs();
281 $date = date('Y-m-d H:i',time());
283 $subject = sprintf (_('[%1$s Release] %2$s'),
284 $this->FRSPackage->Group->getUnixName(),
285 $this->FRSPackage->getName());
286 $text = stripcslashes(sprintf(_('Project %1$s (%2$s) has released a new version of package "%3$s".
287 You can download it by following this link:
291 You receive this email because you requested to be notified when new
292 versions of this package were released. If you don\'t wish to be
293 notified in the future, please login to %5$s and click this link:
296 $this->FRSPackage->Group->getPublicName(),
297 $this->FRSPackage->Group->getUnixName(),
298 $this->FRSPackage->getName(),
299 util_make_url ("/frs/?group_id=". $this->FRSPackage->Group->getID() ."&release_id=". $this->getID()),
300 $GLOBALS['sys_name'],
301 util_make_url ("/frs/monitor.php?filemodule_id=".$this->FRSPackage->getID()."&group_id=".$this->FRSPackage->Group->getID()."&stop=1")));
302 $text = util_line_wrap($text);
304 util_handle_message(array_unique($arr),$subject,$text);
310 * getFiles - gets all the file objects for files in this release.
312 * return array Array of FRSFile Objects.
314 function &getFiles() {
315 if (!is_array($this->release_files) || count($this->release_files) < 1) {
316 $this->release_files=array();
317 $res=db_query("SELECT * FROM frs_file_vw WHERE release_id='".$this->getID()."'");
318 while ($arr = db_fetch_array($res)) {
319 $this->release_files[]=new FRSFile($this,$arr['file_id'],$arr);
322 return $this->release_files;
326 * delete - delete this release and all its related data.
328 * @param bool I'm Sure.
329 * @param bool I'm REALLY sure.
330 * @return bool true/false;
332 function delete($sure, $really_sure) {
333 if (!$sure || !$really_sure) {
334 $this->setMissingParamsError();
337 $perm =& $this->FRSPackage->Group->getPermission( session_get_user() );
339 if (!$perm || !is_object($perm) || !$perm->isReleaseTechnician()) {
340 $this->setPermissionDeniedError();
343 $f =& $this->getFiles();
344 for ($i=0; $i<count($f); $i++) {
345 if (!is_object($f[$i]) || $f[$i]->isError() || !$f[$i]->delete()) {
346 $this->setError('File Error: '.$f[$i]->getName().':'.$f[$i]->getErrorMessage());
350 $dir=$GLOBALS['sys_upload_dir'].'/'.
351 $this->FRSPackage->Group->getUnixName() . '/' .
352 $this->FRSPackage->getFileName().'/'.
353 $this->getFileName().'/';
355 // double-check we're not trying to remove root dir
356 if (util_is_root_dir($dir)) {
357 $this->setError('Release::delete error: trying to delete root dir');
360 exec('rm -rf '.$dir);
362 db_query("DELETE FROM frs_release WHERE release_id='".$this->getID()."'
363 AND package_id='".$this->FRSPackage->getID()."'");
368 * create - create a new release in the database.
370 * @param int The status of this release from the frs_status table.
371 * @param string The name of the release.
372 * @param string The release notes for the release.
373 * @param string The change log for the release.
374 * @param int Whether the notes/log are preformatted with \n chars (1) true (0) false.
375 * @param int The unix date of the release.
376 * @return boolean success.
378 function update($status,$name,$notes,$changes,$preformatted,$release_date) {
379 if (strlen($name) < 3) {
380 $this->setError(_('FRSPackage Name Must Be At Least 3 Characters'));
384 $perm =& $this->FRSPackage->Group->getPermission( session_get_user() );
386 if (!$perm || !is_object($perm) || !$perm->isReleaseTechnician()) {
387 $this->setPermissionDeniedError();
397 if($this->getName()!=htmlspecialchars($name)) {
398 $res=db_query("SELECT * FROM frs_release WHERE package_id='".$this->FRSPackage->getID()."'
399 AND name='".htmlspecialchars($name)."'");
400 if (db_numrows($res)) {
401 $this->setError('FRSRelease::create() Error Adding Release: Name Already Exists');
406 $res=db_query("UPDATE frs_release
408 name='".htmlspecialchars($name)."',
410 notes='".htmlspecialchars($notes)."',
411 changes='".htmlspecialchars($changes)."',
412 preformatted='$preformatted',
413 release_date='$release_date',
414 released_by='". user_getid() ."'
415 WHERE package_id='".$this->FRSPackage->getID()."'
416 AND release_id='".$this->getID()."'");
418 if (!$res || db_affected_rows($res) < 1) {
420 $this->setError('FRSRelease::update() Error On Update: '.db_error());
424 $oldfilename = $this->getFileName();
425 if(!$this->fetchData($this->getID())){
427 $this->setError('FRSRelease::update() Error Updating Release: CouldnĀ“t fetch data');
430 $newfilename = $this->getFileName();
431 $olddirlocation = $GLOBALS['sys_upload_dir'].'/'.$this->FRSPackage->Group->getUnixName().'/'.$this->FRSPackage->getFileName().'/'.$oldfilename;
432 $newdirlocation = $GLOBALS['sys_upload_dir'].'/'.$this->FRSPackage->Group->getUnixName().'/'.$this->FRSPackage->getFileName().'/'.$newfilename;
434 if(($oldfilename!=$newfilename)){
435 if(is_dir($newdirlocation)){
437 $this->setError('FRSRelease::update() Error Updating Release: Directory Already Exists');
440 if(!rename($olddirlocation,$newdirlocation)) {
442 $this->setError('FRSRelease::update() Error Updating Release: CouldnĀ“t rename dir');
455 // c-file-style: "bsd"