File: Document.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 (484 lines) | stat: -rw-r--r-- 12,486 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
<?php
/**
 * GForge Doc Mgr Facility
 *
 * Copyright 2002 GForge, LLC
 * http://gforge.org/
 *
 * @version   $Id: Document.class 5356 2006-03-08 14:20:11Z tperdue $
 *
 * 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
 */


/*
	Document Manager

	by Quentin Cregan, SourceForge 06/2000

	Complete OO rewrite by Tim Perdue 1/2003
*/

require_once('common/include/Error.class');

class Document extends Error {

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

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

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

		if ($docid) {
			if (!$arr || !is_array($arr)) {
				if (!$this->fetchData($docid)) {
					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;
				}
			}
			if (!$this->isPublic()) {
				$perm =& $this->Group->getPermission( session_get_user() );

				if (!$perm || !is_object($perm) || !$perm->isMember()) {
					$this->setPermissionDeniedError();
					$this->data_array = null;
					return false;
				}
			}
		}
		return true;
	}

	/**
	 *	create - use this function to create a new entry in the database.
	 *
	 *	@param	string	The filename of this document. Can be a URL.
	 *	@param	string	The filetype of this document. If filename is URL, this should be 'URL';
	 *	@param	string	The contents of this document (should be addslashes()'d before entry).
	 *	@param	int	The doc_group id of the doc_groups table.
	 *	@param	string	The title of this document.
	 *	@param	int	The language id of the supported_languages table.
	 *	@param	string	The description of this document.
	 *	@return	boolean	success.
	 */
	function create($filename,$filetype,$data,$doc_group,$title,$language_id,$description) {

		global $Language;
		if (strlen($title) < 5) {
			$this->setError($Language->getText('docman_common','error_min_title_length'));
			return false;
		}
		if (strlen($description) < 10) {
			$this->setError($Language->getText('docman_common','error_min_desc_length'));
			return false;
		}

/*
		$perm =& $this->Group->getPermission( session_get_user() );
		if (!$perm || !is_object($perm) || !$perm->isDocEditor()) {
			$this->setPermissionDeniedError();
			return false;
		}
*/
		$user_id = ((session_loggedin()) ? user_getid() : 100);

		$doc_initstatus = '3';
		// If Editor - uploaded Documents are ACTIVE
		if ( session_loggedin() ) {
			$perm =& $this->Group->getPermission( session_get_user() );
			if ($perm && is_object($perm) && $perm->isDocEditor()) {
				$doc_initstatus = '1';
			}
		}

		// If $filetype is "text/plain", $body convert UTF-8 encoding.
		if (strcasecmp($filetype,"text/plain") === 0 &&
			function_exists('mb_convert_encoding') &&
			function_exists('mb_detect_encoding')) {
			$data = mb_convert_encoding($data,'UTF-8',mb_detect_encoding($data));
		}
		
		$filesize = strlen($data);

		$sql="INSERT INTO doc_data (group_id,title,description,createdate,doc_group,
			stateid,language_id,filename,filetype,filesize,data,created_by)
			VALUES ('".$this->Group->getId()."',
			'". htmlspecialchars($title) ."',
			'". htmlspecialchars($description) ."',
			'". time() ."',
			'$doc_group',
			'$doc_initstatus',
			'$language_id',
			'$filename',
			'$filetype',
			'$filesize',
			'". base64_encode(stripslashes($data)) ."',
			'$user_id')";

		db_begin();
		$result=db_query($sql);
		if (!$result) {
			$this->setError('Error Adding Document: '.db_error());
			db_rollback();
			return false;
		}
		$docid=db_insertid($result,'doc_data','docid');
		if (!$this->fetchData($docid)) {
			db_rollback();
			return false;
		}
		$this->sendNotice(true);
		db_commit();
		return true;
	}

	/**
	 *  fetchData() - re-fetch the data for this document from the database.
	 *
	 *  @param  int	 The document id.
	 *	@return	boolean	success
	 */
	function fetchData($docid) {
		global $Language;
		$res=db_query("SELECT * FROM docdata_vw
			WHERE docid='$docid'
			AND group_id='". $this->Group->getID() ."'");
		if (!$res || db_numrows($res) < 1) {
			$this->setError($Language->getText('docman_common_doc','invalid_docid'));
			return false;
		}
		$this->data_array =& db_fetch_array($res);
		db_free_result($res);
		return true;
	}

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

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

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

	/**
	 *	getDescription - the description of this document.
	 *
	 *	@return string	The description.
	 */
	function getDescription() {
		return $this->data_array['description'];
	}

	/**
	 *	isURL - whether this document is a URL and not a local file.
	 *
	 *	@return	boolean	is_url.
	 */
	function isURL() {
		return ($this->data_array['filetype'] == 'URL');
	}

	/**
	 *	isPublic - whether this document is available to the general public.
	 *
	 *	@return	boolean	is_public.
	 */
	function isPublic() {
		return (($this->data_array['stateid'] == 1) ? true  : false);
	}

	/**
	 *	getStateID - get this stateid.
	 *
	 *	@return	int	The stateid.
	 */
	function getStateID() {
		return $this->data_array['stateid'];
	}

	/**
	 *	getStateName - the statename of this document.
	 *
	 *	@return string	The statename.
	 */
	function getStateName() {
		return $this->data_array['state_name'];
	}

	/**
	 *	getLanguageID - get this language_id.
	 *
	 *	@return	int	The language_id.
	 */
	function getLanguageID() {
		return $this->data_array['language_id'];
	}

	/**
	 *	getLanguageName - the language_name of this document.
	 *
	 *	@return string	The language_name.
	 */
	function getLanguageName() {
		return $this->data_array['language_name'];
	}

	/**
	 *	getDocGroupID - get this doc_group_id.
	 *
	 *	@return	int	The doc_group_id.
	 */
	function getDocGroupID() {
		return $this->data_array['doc_group'];
	}

	/**
	 *	getDocGroupName - the doc_group_name of this document.
	 *
	 *	@return string	The docgroupname.
	 */
	function getDocGroupName() {
		return $this->data_array['group_name'];
	}

	/**
	 *	getCreatorID - get this creator's user_id.
	 *
	 *	@return	int	The user_id.
	 */
	function getCreatorID() {
		return $this->data_array['created_by'];
	}

	/**
	 *	getCreatorUserName - the unix name of the person who created this document.
	 *
	 *	@return string	The unix name of the creator.
	 */
	function getCreatorUserName() {
		return $this->data_array['user_name'];
	}

	/**
	 *	getCreatorRealName - the real name of the person who created this document.
	 *
	 *	@return string	The real name of the creator.
	 */
	function getCreatorRealName() {
		return $this->data_array['realname'];
	}

	/**
	 *	getCreatorEmail - the email of the person who created this document.
	 *
	 *	@return string	The email of the creator.
	 */
	function getCreatorEmail() {
		return $this->data_array['email'];
	}

	/**
	 *	getFileName - the filename of this document.
	 *
	 *	@return string	The filename.
	 */
	function getFileName() {
		return $this->data_array['filename'];
	}

	/**
	 *	getFileType - the filetype of this document.
	 *
	 *	@return string	The filetype.
	 */
	function getFileType() {
		return $this->data_array['filetype'];
	}

	/**
	 *	getFileData - the filedata of this document.
	 *
	 *	@return string	The filedata.
	 */
	function getFileData() {
		//
		//	Because this could be a large string, we only fetch if we actually need it
		//
		$res=db_query("SELECT data FROM doc_data WHERE docid='".$this->getID()."'");
		return base64_decode(db_result($res,0,'data'));
	}
	
	/**
	* getFileSize - Return the size of the document
	*
	* @return	int	The file size
	*/
	function getFileSize() {
		return $this->data_array['filesize'];
	}

	/**
	 *	update - use this function to update an existing entry in the database.
	 *
	 *	@param	string	The filename of this document. Can be a URL.
	 *	@param	string	The filetype of this document. If filename is URL, this should be 'URL';
	 *	@param	string	The contents of this document (should be addslashes()'d before entry).
	 *	@param	int	The doc_group id of the doc_groups table.
	 *	@param	string	The title of this document.
	 *	@param	int	The language id of the supported_languages table.
	 *	@param	string	The description of this document.
	 *	@param	int	The state id of the doc_states table.
	 *	@return	boolean	success.
	 */
	function update($filename,$filetype,$data,$doc_group,$title,$language_id,$description,$stateid) {
		global $Language;
		if (strlen($title) < 5) {
			$this->setError($Language->getText('docman_common','error_min_title_length'));
			return false;
		}
		if (strlen($description) < 10) {
			$this->setError($Language->getText('docman_common','error_min_desc_length'));
			return false;
		}

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

		if (!$perm || !is_object($perm) || !$perm->isDocEditor()) {
			$this->setPermissionDeniedError();
			return false;
		}
		if ($data) {
			$filesize = strlen($data);
			$datastr="data='". base64_encode(stripslashes($data)) ."', filesize='".$filesize."',";
		}

		$res=db_query("UPDATE doc_data SET
			title='". htmlspecialchars($title) ."',
			description='". htmlspecialchars($description) ."',
			stateid='$stateid',
			doc_group='$doc_group',
			filetype='$filetype',
			filename='$filename',
			$datastr
			language_id='$language_id',
			updatedate='". time() ."'
			WHERE group_id='".$this->Group->getID()."'
			AND docid='".$this->getID()."'");

		if (!$res || db_affected_rows($res) < 1) {
			$this->setOnUpdateError(db_error());
			return false;
		}
		$this->sendNotice(false);
		return true;
	}

	/**
	*   sendNotice - Notifies of document submissions
	*/
	function sendNotice ($new=true) {
		$BCC = $this->Group->getDocEmailAddress();
		if (strlen($BCC) > 0) {
			$subject = '['.$this->Group->getPublicName().'] New document - '.$this->getName();
			$body = "Project: ".$this->Group->getPublicName()."\n";
			$body .= "Group: ".$groupname."\n";
			$body .= "Document title: ".$this->getName()."\n";
			$body .= "Document description: ".util_unconvert_htmlspecialchars( $this->getDescription() )."\n";
			$body .= "Submitter: ".$this->getCreatorRealName()." (".$this->getCreatorUserName().") \n";
			$body .= "\n\n-------------------------------------------------------".
				"\nFor more info, visit:".
				"\n\nhttp://".$GLOBALS['sys_default_domain']."/docman/index.php?group_id=".$this->Group->getID();

			util_send_message('',$subject,$body,'',$BCC);
		}

		return true;
	}
	
	function delete() {
		$perm =& $this->Group->getPermission( session_get_user() );
		if (!$perm || !is_object($perm) || !$perm->isDocEditor()) {
			$this->setPermissionDeniedError();
			return false;
		}
		
		$sql = 'DELETE FROM doc_data WHERE docid='.$this->getID();
		$result = db_query($sql);
		if (!$result) {
			$this->setError('Error Deleting Document: '.db_error());
			db_rollback();
			return false;
		}
		
		return true;
	}
}

?>