File: FRSPackage.class

package info (click to toggle)
gforge 4.5.14-22etch13
  • links: PTS
  • area: main
  • in suites: etch
  • size: 13,004 kB
  • ctags: 11,918
  • sloc: php: 36,047; sql: 29,050; sh: 10,538; perl: 6,496; xml: 3,810; makefile: 341; python: 263; ansic: 256
file content (488 lines) | stat: -rw-r--r-- 13,531 bytes parent folder | download | duplicates (2)
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
<?php
/**
 * GForge File Release Facility
 *
 * Copyright 2002 GForge, LLC
 * http://gforge.org/
 *
 * @version   $Id: FRSPackage.class 4853 2005-10-31 18:17:58Z lcorso $
 *
 * 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');
require_once('common/frs/FRSRelease.class');

function &get_frs_packages($Group) {
	$res=db_query("SELECT * FROM frs_package WHERE group_id='".$Group->getID()."'");
	if (db_numrows($res) < 1) {
		return false;
	}
	$ps = array();
	while($arr = db_fetch_array($res)) {
		$ps[]=new FRSPackage($Group,$arr['package_id'],$arr);
	}
	return $ps;
}

/**
 * Gets a FRSPackage object from the given package id
 * 
 * @param package_id	the package id
 * @param data	the DB handle if passed in (optional)
 * @return	the FRSPackage object	
 */
function &frspackage_get_object($package_id, $data=false) {
	global $FRSPACKAGE_OBJ;
	if (!isset($FRSPACKAGE_OBJ['_'.$package_id.'_'])) {
		if ($data) {
			//the db result handle was passed in
		} else {
			$res=db_query("SELECT * FROM frs_package
				WHERE package_id='$package_id'");
			if (db_numrows($res)<1) {
				$FRSPACKAGE_OBJ['_'.$package_id.'_']=false;
				return false;
			}
			$data =& db_fetch_array($res);			
		}
		$Group =& group_get_object($data['group_id']);
		$FRSPACKAGE_OBJ['_'.$package_id.'_']= new FRSPackage($Group,$data['package_id'],$data);
	}
	return $FRSPACKAGE_OBJ['_'.$package_id.'_'];
}

class FRSPackage extends Error {

	/**
	 * Associative array of data from db.
	 *
	 * @var  array   $data_array.
	 */
	var $data_array;
	var $package_releases;

	/**
	 * The Group object.
	 *
	 * @var  object  $Group.
	 */
	var $Group; //group object

	/**
	 *  Constructor.
	 *
	 *  @param  object  The Group object to which this FRSPackage is associated.
	 *  @param  int  The package_id.
	 *  @param  array   The associative array of data.
	 *	@return	boolean	success.
	 */
	function FRSPackage(&$Group, $package_id=false, $arr=false) {
		$this->Error();
		if (!$Group || !is_object($Group)) {
			$this->setError('FRSPackage:: No Valid Group Object');
			return false;
		}
		if ($Group->isError()) {
			$this->setError('FRSPackage:: '.$Group->getErrorMessage());
			return false;
		}
		$this->Group =& $Group;

		if ($package_id) {
			if (!$arr || !is_array($arr)) {
				if (!$this->fetchData($package_id)) {
					return false;
				}
			} else {
				$this->data_array =& $arr;
				if ($this->data_array['group_id'] != $this->Group->getID()) {
					$this->setError('Group_id in db result does not match Group Object');
					$this->data_array=null;
					return false;
				}
//
//	Add an is_public check here
//
			}
		}
		return true;
	}

	/**
	 *	create - create a new FRSPackage in the database.
	 *
	 *	@param	string	The name of this package.
	 *	@param	boolean	Whether it's public or not. 1=public 0=private.
	 *	@return	boolean success.
	 */
	function create($name,$is_public=1) {
		global $Language,$sys_apache_user,$sys_apache_group;
		if (strlen($name) < 3) {
			$this->setError($Language->getText('frs_package','error_min_name_length'));
			return false;
		}
		if (!util_is_valid_filename($name)) {
			$this->setError($Language->getText('frs_package','error_name_format'));
		}
		$perm =& $this->Group->getPermission( session_get_user() );

		if (!$perm || !is_object($perm) || !$perm->isReleaseTechnician()) {
			$this->setPermissionDeniedError();
			return false;
		}

		$res=db_query("SELECT * FROM frs_package WHERE group_id='".$this->Group->getID()."'
			AND name='".htmlspecialchars($name)."'");
		if (db_numrows($res)) {
			$this->setError('FRSPackage::create() Error Adding Package: Name Already Exists');
			return false;
		}

		$sql="INSERT INTO frs_package(group_id,name,status_id,is_public)
			VALUES ('".$this->Group->getId()."','".htmlspecialchars($name)."','1','$is_public')";

		db_begin();
		$result=db_query($sql);
		if (!$result) {
			db_rollback();
			$this->setError('FRSPackage::create() Error Adding Package: '.db_error());
			return false;
		}
		$this->package_id=db_insertid($result,'frs_package','package_id');
		if (!$this->fetchData($this->package_id)) {
			db_rollback();
			return false;
		} else {

			//make groupdir if it doesn't exist
			$groupdir = $GLOBALS['sys_upload_dir'].'/'.$this->Group->getUnixName();
			if (!is_dir($groupdir)) {
				@mkdir($groupdir);
			}

			$newdirlocation = $GLOBALS['sys_upload_dir'].'/'.$this->Group->getUnixName().'/'.$this->getFileName();
			exec("/bin/mkdir $newdirlocation",$out);
			// this 2 should normally silently fail (because its called with the apache user) but if its root calling the create() method, then the owner and group for the directory should be changed
			@chown($newdirlocation,$sys_apache_user);
			@chgrp($newdirlocation,$sys_apache_group);
			db_commit();
			return true;
		}
	}

	/**
	 *  fetchData - re-fetch the data for this Package from the database.
	 *
	 *  @param  int  The package_id.
	 *  @return boolean	success.
	 */
	function fetchData($package_id) {
		$res=db_query("SELECT * FROM frs_package
			WHERE package_id='$package_id'
			AND group_id='". $this->Group->getID() ."'");
		if (!$res || db_numrows($res) < 1) {
			$this->setError('FRSPackage::fetchData()  Invalid package_id'.db_error());
			return false;
		}
		$this->data_array =& db_fetch_array($res);
		db_free_result($res);
		return true;
	}

	/**
	 *  getGroup - get the Group object this FRSPackage is associated with.
	 *
	 *  @return	object	The Group object.
	 */
	function &getGroup() {
		return $this->Group;
	}

	/**
	 *  getID - get this package_id.
	 *
	 *  @return	int	The id of this package.
	 */
	function getID() {
		return $this->data_array['package_id'];
	}

	/**
	 *  getName - get the name of this package.
	 *
	 *  @return string  The name of this package.
	 */
	function getName() {
		return $this->data_array['name'];
	}

	/**
	 *  getFileName - get the filename of this package.
	 *
	 *  @return string  The name of this package.
	 */
	function getFileName() {
		return eregi_replace("[^-A-Z0-9_\.]",'',$this->data_array['name']);
	}

	/**
	 *  getStatus - get the status of this package.
	 *
	 *  @return int	The status.
	 */
	function getStatus() {
		return $this->data_array['status_id'];
	}

	/**
	 *	isPublic - whether non-group-members can view.
	 *
	 *	@return boolean   is_public.
	 */
	function isPublic() {
		return $this->data_array['is_public'];
	}

	/**
	 *  setMonitor - Add the current user to the list of people monitoring this package.
	 *
	 *  @return	boolean	success.
	 */
	function setMonitor() {
		global $Language;
		if (!session_loggedin()) {
			$this->setError($Language->getText('frs_package','error_set_monitor'));
			return false;
		}
		$sql="SELECT * FROM filemodule_monitor
			WHERE user_id='".user_getid()."'
			AND filemodule_id='".$this->getID()."';";
		$result = db_query($sql);

		if (!$result || db_numrows($result) < 1) {
			/*
				User is not already monitoring thread, so
				insert a row so monitoring can begin
			*/
			$sql="INSERT INTO filemodule_monitor (filemodule_id,user_id)
				VALUES ('".$this->getID()."','".user_getid()."')";

			$result = db_query($sql);

			if (!$result) {
				$this->setError('Unable to add monitor: '.db_error());
				return false;
			}

		}
		return true;
	}

	/**
	 *  stopMonitor - Remove the current user from the list of people monitoring this package.
	 *
	 *  @return	boolean	success.
	 */
	function stopMonitor() {
		global $Language;
		if (!session_loggedin()) {
			$this->setError($Language->getText('frs_package','error_set_monitor'));
			return false;
		}
		$sql="DELETE FROM filemodule_monitor
			WHERE user_id='".user_getid()."'
			AND filemodule_id='".$this->getID()."';";
		return db_query($sql);
	}

	/**
	 *	getMonitorCount - Get the count of people monitoring this package
	 *
	 *	@return int the count
	 */
	function getMonitorCount() {
		$sql = "select count(*) as count from filemodule_monitor where filemodule_id = ".$this->getID();
		$res = db_result(db_query($sql), 0, 0);
		if ($res < 0) {
			$this->setError('FRSPackage::getMonitorCount() Error On querying monitor count: '.db_error());
			return false;
		}
		return $res;
	}	

	/**
	 *  isMonitoring - Is the current user in the list of people monitoring this package.
	 *
	 *  @return	boolean	is_monitoring.
	 */
	function isMonitoring() {
		if (!session_loggedin()) {
			return false;
		}
		$sql="SELECT * FROM filemodule_monitor
			WHERE user_id='".user_getid()."'
			AND filemodule_id='".$this->getID()."';";

		$result = db_query($sql);

		if (!$result || db_numrows($result) < 1) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 *  getMonitorIDs - Return an array of user_id's of the list of people monitoring this package.
	 *
	 *  @return	array	The array of user_id's.
	 */
	function &getMonitorIDs() {
		$res=db_query("SELECT user_id
			FROM filemodule_monitor
			WHERE filemodule_id='".$this->getID()."'");
		return util_result_column_to_array($res);
	}

	/**
	 *	update - update an FRSPackage in the database.
	 *
	 *	@param	string	The name of this package.
	 *	@param	int	The status_id of this package from frs_status table.
	 *	@return	boolean success.
	 */
	function update($name,$status) {
		global $Language;
		if (strlen($name) < 3) {
			$this->setError($Language->getText('frs_package','error_min_name_length'));
			return false;
		}

		$perm =& $this->Group->getPermission( session_get_user() );

		if (!$perm || !is_object($perm) || !$perm->isReleaseTechnician()) {
			$this->setPermissionDeniedError();
			return false;
		}		
		if($this->getName()!=htmlspecialchars($name)) {
			$res=db_query("SELECT * FROM frs_package WHERE group_id='".$this->Group->getID()."'
			AND name='".htmlspecialchars($name)."'");
			if (db_numrows($res)) {
				$this->setError('FRSPackage::update() Error Updating Package: Name Already Exists');
				return false;
			}
		}
		db_begin();
		$res=db_query("UPDATE frs_package SET
			name='".htmlspecialchars($name)."',
			status_id='$status'
			WHERE group_id='".$this->Group->getID()."'
			AND package_id='".$this->getID()."'");
		if (!$res || db_affected_rows($res) < 1) {
			db_rollback();
			$this->setError('FRSPackage::update() Error On Update: '.db_error());
			return false;
		}

		$olddirname = $this->getFileName();
		if(!$this->fetchData($this->getID())){
			db_rollback();
			$this->setError('FRSPackage::update() Error Updating Package: Couldnt fetch data');
			return false;
		}
		$newdirname = $this->getFileName();
		$olddirlocation = $GLOBALS['sys_upload_dir'].'/'.$this->Group->getUnixName().'/'.$olddirname;
		$newdirlocation = $GLOBALS['sys_upload_dir'].'/'.$this->Group->getUnixName().'/'.$newdirname;
		
		if(($olddirname!=$newdirname)){
			if(is_dir($newdirlocation)){
				db_rollback();
				$this->setError('FRSPackage::update() Error Updating Package: Directory Already Exists');
				return false;	
			} else {
				if(!@rename($olddirlocation,$newdirlocation)) {
					db_rollback();
					$this->setError('FRSPackage::update() Error Updating Package: Couldnt rename dir');
					return false;
				}
			}
		}	
		db_commit();
		return true;
	}

	/**
	 *	getReleases - gets Release objects for all the releases in this package.
	 *
	 *  return  array   Array of FRSRelease Objects.
	 */
	function &getReleases() {
		if (!is_array($this->package_releases) || count($this->package_releases) < 1) {
			$this->package_releases=array();
			$res=db_query("SELECT * FROM frs_release WHERE package_id='".$this->getID()."'");
			while ($arr = db_fetch_array($res)) {
				$this->package_releases[]=new FRSRelease($this,$arr['release_id'],$arr);
			}
		}
		return $this->package_releases;
	}

	/**
	 *  delete - delete this package and all its related data.
	 *
	 *  @param  bool	I'm Sure.
	 *  @param  bool	I'm REALLY sure.
	 *  @return   bool true/false;
	 */
	function delete($sure, $really_sure) {
		if (!$sure || !$really_sure) {
			$this->setMissingParamsError();
			return false;
		}
		$perm =& $this->Group->getPermission( session_get_user() );

		if (!$perm || !is_object($perm) || !$perm->isReleaseTechnician()) {
			$this->setPermissionDeniedError();
			return false;
		}
		$r =& $this->getReleases();
		for ($i=0; $i<count($r); $i++) {
			if (!is_object($r[$i]) || $r[$i]->isError() || !$r[$i]->delete($sure, $really_sure)) {
				$this->setError('Release Error: '.$r[$i]->getName().':'.$r[$i]->getErrorMessage());
				return false;
			}
		}
		$dir=$GLOBALS['sys_upload_dir'].'/'.
			$this->Group->getUnixName() . '/' .
			$this->getFileName().'/';

		// double-check we're not trying to remove root dir
		if (util_is_root_dir($dir)) {
			$this->setError('Package::delete error: trying to delete root dir');
			return false;
		}
		exec('rm -rf '.$dir);

		db_query("DELETE FROM frs_package WHERE package_id='".$this->getID()."'
			AND group_id='".$this->Group->getID()."'");
		return true;
	}

}

?>