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( session_get_user() );
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,$language_id,$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'));
122 $perm =& $this->Group->getPermission( session_get_user() );
123 if (!$perm || !is_object($perm) || !$perm->isDocEditor()) {
124 $this->setPermissionDeniedError();
128 $user_id = ((session_loggedin()) ? user_getid() : 100);
130 $doc_initstatus = '3';
131 // If Editor - uploaded Documents are ACTIVE
132 if ( session_loggedin() ) {
133 $perm =& $this->Group->getPermission( session_get_user() );
134 if ($perm && is_object($perm) && $perm->isDocEditor()) {
135 $doc_initstatus = '1';
139 // If $filetype is "text/plain", $body convert UTF-8 encoding.
140 if (strcasecmp($filetype,"text/plain") === 0 &&
141 function_exists('mb_convert_encoding') &&
142 function_exists('mb_detect_encoding')) {
143 $data = mb_convert_encoding($data,'UTF-8',mb_detect_encoding($data));
147 // key words for in-document search
148 $kw = new Parsedata ($this->engine_path);
149 $kwords = $kw->get_parse_data ($data1, htmlspecialchars($title), htmlspecialchars($description), $filetype);
151 $filesize = strlen($data);
154 $result = db_query_params('INSERT INTO doc_data (group_id,title,description,createdate,doc_group,
155 stateid,language_id,filename,filetype,filesize,data,data_words,created_by)
156 VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13)',
157 array($this->Group->getId(),
158 htmlspecialchars($title),
159 htmlspecialchars($description),
167 base64_encode($data),
171 $this->setError('Error Adding Document: '.db_error());
175 $docid=db_insertid($result,'doc_data','docid');
176 if (!$this->fetchData($docid)) {
180 $this->sendNotice(true);
186 * fetchData() - re-fetch the data for this document from the database.
188 * @param int The document id.
189 * @return boolean success
191 function fetchData($docid) {
192 $res = db_query_params ('SELECT * FROM docdata_vw WHERE docid=$1 AND group_id=$2',
194 $this->Group->getID()));
195 if (!$res || db_numrows($res) < 1) {
196 $this->setError(_('Document:: Invalid docid'));
199 $this->data_array =& db_fetch_array($res);
200 db_free_result($res);
205 * getGroup - get the Group object this Document is associated with.
207 * @return Object The Group object.
209 function &getGroup() {
214 * getID - get this docid.
216 * @return int The docid.
219 return $this->data_array['docid'];
223 * getName - get the name of this document.
225 * @return string The name of this document.
228 return $this->data_array['title'];
232 * getDescription - the description of this document.
234 * @return string The description.
236 function getDescription() {
237 return $this->data_array['description'];
241 * isURL - whether this document is a URL and not a local file.
243 * @return boolean is_url.
246 return ($this->data_array['filetype'] == 'URL');
250 * isText - whether this document is a text document and not a binary one.
252 * @return boolean is_text.
255 $doctype = $this->data_array['filetype'];
256 if (preg_match('/text/i',$doctype)) { // text plain, text html, text x-patch, etc
263 * isHtml - whether this document is a html document.
265 * @return boolean is_html.
268 $doctype = $this->data_array['filetype'];
269 if (preg_match('/html/i',$doctype)) {
276 * isPublic - whether this document is available to the general public.
278 * @return boolean is_public.
280 function isPublic() {
281 return (($this->data_array['stateid'] == 1) ? true : false);
285 * getStateID - get this stateid.
287 * @return int The stateid.
289 function getStateID() {
290 return $this->data_array['stateid'];
294 * getStateName - the statename of this document.
296 * @return string The statename.
298 function getStateName() {
299 return $this->data_array['state_name'];
303 * getLanguageID - get this language_id.
305 * @return int The language_id.
307 function getLanguageID() {
308 return $this->data_array['language_id'];
312 * getLanguageName - the language_name of this document.
314 * @return string The language_name.
316 function getLanguageName() {
317 return $this->data_array['language_name'];
321 * getDocGroupID - get this doc_group_id.
323 * @return int The doc_group_id.
325 function getDocGroupID() {
326 return $this->data_array['doc_group'];
330 * getDocGroupName - the doc_group_name of this document.
332 * @return string The docgroupname.
334 function getDocGroupName() {
335 return $this->data_array['group_name'];
339 * getCreatorID - get this creator's user_id.
341 * @return int The user_id.
343 function getCreatorID() {
344 return $this->data_array['created_by'];
348 * getCreatorUserName - the unix name of the person who created this document.
350 * @return string The unix name of the creator.
352 function getCreatorUserName() {
353 return $this->data_array['user_name'];
357 * getCreatorRealName - the real name of the person who created this document.
359 * @return string The real name of the creator.
361 function getCreatorRealName() {
362 return $this->data_array['realname'];
366 * getCreatorEmail - the email of the person who created this document.
368 * @return string The email of the creator.
370 function getCreatorEmail() {
371 return $this->data_array['email'];
375 * getFileName - the filename of this document.
377 * @return string The filename.
379 function getFileName() {
380 return $this->data_array['filename'];
384 * getFileType - the filetype of this document.
386 * @return string The filetype.
388 function getFileType() {
389 return $this->data_array['filetype'];
393 * getFileData - the filedata of this document.
395 * @return string The filedata.
397 function getFileData() {
399 // Because this could be a large string, we only fetch if we actually need it
401 $res = db_query_params ('SELECT data FROM doc_data WHERE docid=$1',
402 array ($this->getID())) ;
403 return base64_decode(db_result($res,0,'data'));
407 * getFileSize - Return the size of the document
409 * @return int The file size
411 function getFileSize() {
412 return $this->data_array['filesize'];
415 * getUpdated - get the time this document was updated.
417 * @return int The epoch date this document was updated.
419 function getUpdated() {
420 return $this->data_array['updatedate'];
424 * getCreated - get the time this document was created.
426 * @return int The epoch date this document was created.
428 function getCreated() {
429 return $this->data_array['createdate'];
433 * update - use this function to update an existing entry in the database.
435 * @param string The filename of this document. Can be a URL.
436 * @param string The filetype of this document. If filename is URL, this should be 'URL';
437 * @param string The contents of this document.
438 * @param int The doc_group id of the doc_groups table.
439 * @param string The title of this document.
440 * @param int The language id of the supported_languages table.
441 * @param string The description of this document.
442 * @param int The state id of the doc_states table.
443 * @return boolean success.
445 function update($filename,$filetype,$data,$doc_group,$title,$language_id,$description,$stateid) {
446 if (strlen($title) < 5) {
447 $this->setError(_('Title Must Be At Least 5 Characters'));
450 if (strlen($description) < 10) {
451 $this->setError(_('Document Description Must Be At Least 10 Characters'));
455 $perm =& $this->Group->getPermission( session_get_user() );
457 if (!$perm || !is_object($perm) || !$perm->isDocEditor()) {
458 $this->setPermissionDeniedError();
462 $res = db_query_params ('UPDATE doc_data SET
473 array (htmlspecialchars($title),
474 htmlspecialchars($description),
481 $this->Group->getID(),
484 if (!$res || db_affected_rows($res) < 1) {
485 $this->setOnUpdateError(db_error());
492 // key words for in-document search
493 $kw = new Parsedata ($this->engine_path);
494 $kwords = $kw->get_parse_data ($data1, htmlspecialchars($title), htmlspecialchars($description), $filetype);
496 $res = db_query_params ('UPDATE doc_data SET data=$1, filesize=$2, data_words=$3 WHERE group_id=$4 AND docid=$5',
497 array (base64_encode($data),
500 $this->Group->getID(),
503 if (!$res || db_affected_rows($res) < 1) {
504 $this->setOnUpdateError(db_error());
509 $this->sendNotice(false);
514 * sendNotice - Notifies of document submissions
516 function sendNotice ($new=true) {
517 $BCC = $this->Group->getDocEmailAddress();
518 if (strlen($BCC) > 0) {
519 $subject = '['.$this->Group->getPublicName().'] New document - '.$this->getName();
520 $body = "Project: ".$this->Group->getPublicName()."\n";
521 $body .= "Group: ".$groupname."\n";
522 $body .= "Document title: ".$this->getName()."\n";
523 $body .= "Document description: ".util_unconvert_htmlspecialchars( $this->getDescription() )."\n";
524 $body .= "Submitter: ".$this->getCreatorRealName()." (".$this->getCreatorUserName().") \n";
525 $body .= "\n\n-------------------------------------------------------".
526 "\nFor more info, visit:".
527 "\n\n" . util_make_url('/docman/index.php?group_id='.$this->Group->getID());
529 util_send_message('',$subject,$body,'',$BCC);
536 $perm =& $this->Group->getPermission( session_get_user() );
537 if (!$perm || !is_object($perm) || !$perm->isDocEditor()) {
538 $this->setPermissionDeniedError();
542 $result = db_query_params ('DELETE FROM doc_data WHERE docid=$1',
543 array ($this->getID())) ;
545 $this->setError('Error Deleting Document: '.db_error());
556 // c-file-style: "bsd"