3 * FusionForge document manager
5 * Copyright 2000, Quentin Cregan/Sourceforge
6 * Copyright 2002-2003, Tim Perdue/GForge, LLC
7 * Copyright 2009, Roland Mas
8 * Copyright 2010, Franck Villaume
10 * This file is part of FusionForge.
12 * FusionForge is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published
14 * by the Free Software Foundation; either version 2 of the License,
15 * or (at your option) any later version.
17 * FusionForge is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with FusionForge; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
28 require_once $gfcommon.'include/Error.class.php';
29 require_once $gfcommon.'docman/Parsedata.class.php';
31 class Document extends Error {
34 * Associative array of data from db.
36 * @var array $data_array.
46 * The Search engine path.
48 * @var string $engine_path
55 * @param object The Group object to which this document is associated.
56 * @param int The docid.
57 * @param array The associative array of data.
58 * @return boolean success.
60 function Document(&$Group, $docid=false, $arr=false, $engine = "") {
62 if (!$Group || !is_object($Group)) {
63 $this->setNotValidGroupObjectError();
66 if ($Group->isError()) {
67 $this->setError('Document:: '.$Group->getErrorMessage());
70 $this->Group =& $Group;
73 if (!$arr || !is_array($arr)) {
74 if (!$this->fetchData($docid)) {
78 $this->data_array =& $arr;
79 if ($this->data_array['group_id'] != $this->Group->getID()) {
80 $this->setError('Group_id in db result does not match Group Object');
81 $this->data_array = null;
85 if (!$this->isPublic()) {
86 $perm =& $this->Group->getPermission ();
88 if (!$perm || !is_object($perm) || !$perm->isMember()) {
89 $this->setPermissionDeniedError();
90 $this->data_array = null;
95 $this->engine_path = $engine;
100 * create - use this function to create a new entry in the database.
102 * @param string The filename of this document. Can be a URL.
103 * @param string The filetype of this document. If filename is URL, this should be 'URL';
104 * @param string The contents of this document.
105 * @param int The doc_group id of the doc_groups table.
106 * @param string The title of this document.
107 * @param int The language id of the supported_languages table.
108 * @param string The description of this document.
109 * @return boolean success.
111 function create($filename,$filetype,$data,$doc_group,$title,$description) {
112 if (strlen($title) < 5) {
113 $this->setError(_('Title Must Be At Least 5 Characters'));
116 if (strlen($description) < 10) {
117 $this->setError(_('Document Description Must Be At Least 10 Characters'));
121 $user_id = ((session_loggedin()) ? user_getid() : 100);
123 $doc_initstatus = '3';
124 // If Editor - uploaded Documents are ACTIVE
125 if ( session_loggedin() ) {
126 $perm =& $this->Group->getPermission ();
127 if ($perm && is_object($perm) && $perm->isDocEditor()) {
128 $doc_initstatus = '1';
132 // If $filetype is "text/plain", $body convert UTF-8 encoding.
133 if (strcasecmp($filetype,"text/plain") === 0 &&
134 function_exists('mb_convert_encoding') &&
135 function_exists('mb_detect_encoding')) {
136 $data = mb_convert_encoding($data,'UTF-8',mb_detect_encoding($data));
140 // key words for in-document search
141 if ($this->Group->useDocmanSearch()) {
142 $kw = new Parsedata ($this->engine_path);
143 $kwords = $kw->get_parse_data ($data1, htmlspecialchars($title), htmlspecialchars($description), $filetype);
148 $filesize = strlen($data);
151 $result = db_query_params('INSERT INTO doc_data (group_id,title,description,createdate,doc_group,
152 stateid,filename,filetype,filesize,data,data_words,created_by)
153 VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12)',
154 array($this->Group->getId(),
155 htmlspecialchars($title),
156 htmlspecialchars($description),
163 base64_encode($data),
167 $this->setError('Error Adding Document: '.db_error());
171 $docid=db_insertid($result,'doc_data','docid');
172 if (!$this->fetchData($docid)) {
176 $this->sendNotice(true);
182 * fetchData() - re-fetch the data for this document from the database.
184 * @param int The document id.
185 * @return boolean success
187 function fetchData($docid) {
188 $res = db_query_params ('SELECT * FROM docdata_vw WHERE docid=$1 AND group_id=$2',
190 $this->Group->getID()));
191 if (!$res || db_numrows($res) < 1) {
192 $this->setError(_('Document:: Invalid docid'));
195 $this->data_array =& db_fetch_array($res);
196 db_free_result($res);
201 * getGroup - get the Group object this Document is associated with.
203 * @return Object The Group object.
205 function &getGroup() {
210 * getID - get this docid.
212 * @return int The docid.
215 return $this->data_array['docid'];
219 * getName - get the name of this document.
221 * @return string The name of this document.
224 return $this->data_array['title'];
228 * getDescription - the description of this document.
230 * @return string The description.
232 function getDescription() {
233 return $this->data_array['description'];
237 * isURL - whether this document is a URL and not a local file.
239 * @return boolean is_url.
242 return ($this->data_array['filetype'] == 'URL');
246 * isText - whether this document is a text document and not a binary one.
248 * @return boolean is_text.
251 $doctype = $this->data_array['filetype'];
252 if (preg_match('|^text/|i',$doctype)) { // text plain, text html, text x-patch, etc
259 * isHtml - whether this document is a html document.
261 * @return boolean is_html.
264 $doctype = $this->data_array['filetype'];
265 if (preg_match('/html/i',$doctype)) {
272 * isPublic - whether this document is available to the general public.
274 * @return boolean is_public.
276 function isPublic() {
277 return (($this->data_array['stateid'] == 1) ? true : false);
281 * getStateID - get this stateid.
283 * @return int The stateid.
285 function getStateID() {
286 return $this->data_array['stateid'];
290 * getStateName - the statename of this document.
292 * @return string The statename.
294 function getStateName() {
295 return $this->data_array['state_name'];
299 * getDocGroupID - get this doc_group_id.
301 * @return int The doc_group_id.
303 function getDocGroupID() {
304 return $this->data_array['doc_group'];
308 * getDocGroupName - the doc_group_name of this document.
310 * @return string The docgroupname.
312 function getDocGroupName() {
313 return $this->data_array['group_name'];
317 * getCreatorID - get this creator's user_id.
319 * @return int The user_id.
321 function getCreatorID() {
322 return $this->data_array['created_by'];
326 * getCreatorUserName - the unix name of the person who created this document.
328 * @return string The unix name of the creator.
330 function getCreatorUserName() {
331 return $this->data_array['user_name'];
335 * getCreatorRealName - the real name of the person who created this document.
337 * @return string The real name of the creator.
339 function getCreatorRealName() {
340 return $this->data_array['realname'];
344 * getCreatorEmail - the email of the person who created this document.
346 * @return string The email of the creator.
348 function getCreatorEmail() {
349 return $this->data_array['email'];
353 * getFileName - the filename of this document.
355 * @return string The filename.
357 function getFileName() {
358 return $this->data_array['filename'];
362 * getFileType - the filetype of this document.
364 * @return string The filetype.
366 function getFileType() {
367 return $this->data_array['filetype'];
371 * getFileData - the filedata of this document.
373 * @return string The filedata.
375 function getFileData() {
377 // Because this could be a large string, we only fetch if we actually need it
379 $res = db_query_params ('SELECT data FROM doc_data WHERE docid=$1',
380 array ($this->getID())) ;
381 return base64_decode(db_result($res,0,'data'));
385 * getFileSize - Return the size of the document
387 * @return int The file size
389 function getFileSize() {
390 return $this->data_array['filesize'];
393 * getUpdated - get the time this document was updated.
395 * @return int The epoch date this document was updated.
397 function getUpdated() {
398 return $this->data_array['updatedate'];
402 * getCreated - get the time this document was created.
404 * @return int The epoch date this document was created.
406 function getCreated() {
407 return $this->data_array['createdate'];
411 * setState - set the stateid of the document.
413 * @param int The state id of the doc_states table.
414 * @return boolean success.
416 function setState($stateid) {
417 $res = db_query_params ('UPDATE doc_data SET
422 $this->Group->getID(),
425 if (!$res || db_affected_rows($res) < 1) {
426 $this->setOnUpdateError(db_error());
429 $this->sendNotice(false);
434 * update - use this function to update an existing entry in the database.
436 * @param string The filename of this document. Can be a URL.
437 * @param string The filetype of this document. If filename is URL, this should be 'URL';
438 * @param string The contents of this document.
439 * @param int The doc_group id of the doc_groups table.
440 * @param string The title of this document.
441 * @param int The language id of the supported_languages table.
442 * @param string The description of this document.
443 * @param int The state id of the doc_states table.
444 * @return boolean success.
446 function update($filename,$filetype,$data,$doc_group,$title,$description,$stateid) {
447 if (strlen($title) < 5) {
448 $this->setError(_('Title Must Be At Least 5 Characters'));
451 if (strlen($description) < 10) {
452 $this->setError(_('Document Description Must Be At Least 10 Characters'));
456 $perm =& $this->Group->getPermission ();
458 if (!$perm || !is_object($perm) || !$perm->isDocEditor()) {
459 $this->setPermissionDeniedError();
463 $res = db_query_params ('UPDATE doc_data SET
473 array (htmlspecialchars($title),
474 htmlspecialchars($description),
480 $this->Group->getID(),
483 if (!$res || db_affected_rows($res) < 1) {
484 $this->setOnUpdateError(db_error());
491 // key words for in-document search
492 if ($this->Group->useDocmanSearch()) {
493 $kw = new Parsedata ($this->engine_path);
494 $kwords = $kw->get_parse_data ($data1, htmlspecialchars($title), htmlspecialchars($description), $filetype);
499 $res = db_query_params ('UPDATE doc_data SET data=$1, filesize=$2, data_words=$3 WHERE group_id=$4 AND docid=$5',
500 array (base64_encode($data),
503 $this->Group->getID(),
506 if (!$res || db_affected_rows($res) < 1) {
507 $this->setOnUpdateError(db_error());
512 $this->sendNotice(false);
517 * sendNotice - Notifies of document submissions
519 function sendNotice ($new=true) {
520 $BCC = $this->Group->getDocEmailAddress();
521 if (strlen($BCC) > 0) {
522 $subject = '['.$this->Group->getPublicName().'] New document - '.$this->getName();
523 $body = "Project: ".$this->Group->getPublicName()."\n";
524 $body .= "Group: ".$groupname."\n";
525 $body .= "Document title: ".$this->getName()."\n";
526 $body .= "Document description: ".util_unconvert_htmlspecialchars( $this->getDescription() )."\n";
527 $body .= "Submitter: ".$this->getCreatorRealName()." (".$this->getCreatorUserName().") \n";
528 $body .= "\n\n-------------------------------------------------------".
529 "\nFor more info, visit:".
530 "\n\n" . util_make_url('/docman/index.php?group_id='.$this->Group->getID());
532 util_send_message('',$subject,$body,'',$BCC);
539 $perm =& $this->Group->getPermission ();
540 if (!$perm || !is_object($perm) || !$perm->isDocEditor()) {
541 $this->setPermissionDeniedError();
545 $result = db_query_params ('DELETE FROM doc_data WHERE docid=$1',
546 array ($this->getID())) ;
548 $this->setError('Error Deleting Document: '.db_error());
559 // c-file-style: "bsd"