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 - Capgemini
9 * http://fusionforge.org
11 * This file is part of FusionForge.
13 * FusionForge is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published
15 * by the Free Software Foundation; either version 2 of the License,
16 * or (at your option) any later version.
18 * FusionForge is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with FusionForge; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 require_once $gfcommon.'include/Error.class.php';
30 require_once $gfcommon.'docman/Parsedata.class.php';
32 class Document extends Error {
35 * Associative array of data from db.
37 * @var array $data_array.
49 * The Search engine path.
51 * @var string $engine_path
58 * @param object The Group object to which this document is associated.
59 * @param int The docid.
60 * @param array The associative array of data.
61 * @return boolean success.
63 function Document(&$Group, $docid = false, $arr = false, $engine = '') {
65 if (!$Group || !is_object($Group)) {
66 $this->setNotValidGroupObjectError();
69 if ($Group->isError()) {
70 $this->setError('Document:: '. $Group->getErrorMessage());
73 $this->Group =& $Group;
76 if (!$arr || !is_array($arr)) {
77 if (!$this->fetchData($docid)) {
81 $this->data_array =& $arr;
82 if ($this->data_array['group_id'] != $this->Group->getID()) {
83 $this->setError('Document:: '. _('Group_id in db result does not match Group Object'));
84 $this->data_array = null;
88 if (!$this->isPublic()) {
89 $perm =& $this->Group->getPermission();
91 if (!$perm || !is_object($perm) || !$perm->isMember()) {
92 $this->setPermissionDeniedError();
93 $this->data_array = null;
98 $this->engine_path = $engine;
103 * create - use this function to create a new entry in the database.
105 * @param string The filename of this document. Can be a URL.
106 * @param string The filetype of this document. If filename is URL, this should be 'URL';
107 * @param string The contents of this document.
108 * @param int The doc_group id of the doc_groups table.
109 * @param string The title of this document.
110 * @param string The description of this document.
111 * @return boolean success.
113 function create($filename, $filetype, $data, $doc_group, $title, $description) {
114 if (strlen($title) < 5) {
115 $this->setError(_('Title Must Be At Least 5 Characters'));
118 if (strlen($description) < 10) {
119 $this->setError(_('Document Description Must Be At Least 10 Characters'));
123 $result = db_query_params('SELECT filename, doc_group from docdata_vw where filename = $1 and doc_group = $2',
124 array($filename, $doc_group));
126 if (!$result || db_numrows($result) > 0) {
127 $this->setError(_('Document already published in this directory'));
131 $user_id = ((session_loggedin()) ? user_getid() : 100);
133 $doc_initstatus = '3';
134 // If Editor - uploaded Documents are ACTIVE
135 if (session_loggedin()) {
136 $perm =& $this->Group->getPermission ();
137 if ($perm && is_object($perm) && $perm->isDocEditor()) {
138 $doc_initstatus = '1';
142 // If $filetype is "text/plain", $body convert UTF-8 encoding.
143 if (strcasecmp($filetype,"text/plain") === 0 &&
144 function_exists('mb_convert_encoding') &&
145 function_exists('mb_detect_encoding')) {
146 $data = mb_convert_encoding($data, 'UTF-8', mb_detect_encoding($data));
150 // key words for in-document search
151 if ($this->Group->useDocmanSearch()) {
152 $kw = new Parsedata($this->engine_path);
153 $kwords = $kw->get_parse_data($data1, htmlspecialchars($title), htmlspecialchars($description), $filetype);
158 $filesize = strlen($data);
161 $result = db_query_params('INSERT INTO doc_data (group_id,title,description,createdate,doc_group,
162 stateid,filename,filetype,filesize,data_words,created_by)
163 VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11)',
164 array($this->Group->getId(),
165 htmlspecialchars($title),
166 htmlspecialchars($description),
176 $this->setError(_('Error Adding Document:').' '.db_error().$result);
181 $docid = db_insertid($result,'doc_data','docid');
183 switch ($this->Group->getStorageAPI()) {
185 $result = db_query_params('UPDATE doc_data set data = $1 where docid = $2',
186 array(base64_encode($data),$docid));
188 $this->setError(_('Error Adding Document:').' '.db_error().$result);
195 $this->setError(_('Error Adding Document: No Storage API'));
201 if (!$this->fetchData($docid)) {
205 $this->sendNotice(true);
211 * fetchData() - re-fetch the data for this document from the database.
213 * @param int The document id.
214 * @return boolean success
216 function fetchData($docid) {
217 $res = db_query_params('SELECT * FROM docdata_vw WHERE docid=$1 AND group_id=$2',
218 array($docid, $this->Group->getID()));
219 if (!$res || db_numrows($res) < 1) {
220 $this->setError(_('Document:: Invalid docid'));
223 $this->data_array = db_fetch_array($res);
224 db_free_result($res);
229 * getGroup - get the Group object this Document is associated with.
231 * @return Object The Group object.
233 function &getGroup() {
238 * getID - get this docid.
240 * @return int The docid.
243 return $this->data_array['docid'];
247 * getName - get the name of this document.
249 * @return string The name of this document.
252 return $this->data_array['title'];
256 * getDescription - the description of this document.
258 * @return string The description.
260 function getDescription() {
261 return $this->data_array['description'];
265 * isURL - whether this document is a URL and not a local file.
267 * @return boolean is_url.
270 return ($this->data_array['filetype'] == 'URL');
274 * isText - whether this document is a text document and not a binary one.
276 * @return boolean is_text.
279 $doctype = $this->data_array['filetype'];
280 if (preg_match('|^text/|i', $doctype)) { // text plain, text html, text x-patch, etc
287 * isHtml - whether this document is a html document.
289 * @return boolean is_html.
292 $doctype = $this->data_array['filetype'];
293 if (preg_match('/html/i',$doctype)) {
300 * isPublic - whether this document is available to the general public.
302 * @return boolean is_public.
304 function isPublic() {
305 return (($this->data_array['stateid'] == 1) ? true : false);
309 * getStateID - get this stateid.
311 * @return int The stateid.
313 function getStateID() {
314 return $this->data_array['stateid'];
318 * getStateName - the statename of this document.
320 * @return string The statename.
322 function getStateName() {
323 return $this->data_array['state_name'];
327 * getDocGroupID - get this doc_group_id.
329 * @return int The doc_group_id.
331 function getDocGroupID() {
332 return $this->data_array['doc_group'];
336 * getDocGroupName - the doc_group_name of this document.
338 * @return string The docgroupname.
340 function getDocGroupName() {
341 return $this->data_array['group_name'];
345 * getCreatorID - get this creator's user_id.
347 * @return int The user_id.
349 function getCreatorID() {
350 return $this->data_array['created_by'];
354 * getCreatorUserName - the unix name of the person who created this document.
356 * @return string The unix name of the creator.
358 function getCreatorUserName() {
359 return $this->data_array['user_name'];
363 * getCreatorRealName - the real name of the person who created this document.
365 * @return string The real name of the creator.
367 function getCreatorRealName() {
368 return $this->data_array['realname'];
372 * getCreatorEmail - the email of the person who created this document.
374 * @return string The email of the creator.
376 function getCreatorEmail() {
377 return $this->data_array['email'];
381 * getFileName - the filename of this document.
383 * @return string The filename.
385 function getFileName() {
386 return $this->data_array['filename'];
390 * getFileType - the filetype of this document.
392 * @return string The filetype.
394 function getFileType() {
395 return $this->data_array['filetype'];
399 * getFileData - the filedata of this document.
401 * @return string The filedata.
403 function getFileData() {
405 // Because this could be a large string, we only fetch if we actually need it
407 $res = db_query_params('SELECT data FROM doc_data WHERE docid=$1',array($this->getID()));
408 return base64_decode(db_result($res,0,'data'));
412 * getFileSize - Return the size of the document
414 * @return int The file size
416 function getFileSize() {
417 return $this->data_array['filesize'];
421 * getUpdated - get the time this document was updated.
423 * @return int The epoch date this document was updated.
425 function getUpdated() {
426 return $this->data_array['updatedate'];
430 * getCreated - get the time this document was created.
432 * @return int The epoch date this document was created.
434 function getCreated() {
435 return $this->data_array['createdate'];
439 * getLocked - get the lock status of this document.
441 * @return int The lock status of this document.
443 function getLocked() {
444 return $this->data_array['locked'];
448 * getLockdate - get the lock time of this document.
450 * @return int The lock time of this document.
452 function getLockdate() {
453 return $this->data_array['lockdate'];
457 * getLockedBy - get the user id who set lock on this document.
459 * @return int The user id who set lock on this document.
461 function getLockedBy() {
462 return $this->data_array['locked_by'];
466 * getReservedBy - get the owner of the reversed status of this document.
468 * @return int The owner of the reversed status of this document.
470 function getReservedBy() {
471 return $this->data_array['reserved_by'];
475 * getReserved - get the reversed status of this document.
477 * @return int The reversed status of this document.
479 function getReserved() {
480 return $this->data_array['reserved'];
484 * getMonitoredUserEmailAddress - get the email addresses of users who monitor this file
486 * @return string The list of emails comma separated
488 function getMonitoredUserEmailAddress() {
489 $result = db_query_params('select users.email from users,docdata_monitored_docman where users.user_id = docdata_monitored_docman.user_id and docdata_monitored_docman.doc_id = $1', array ($this->getID()));
490 if (!$result || db_numrows($result) < 1) {
496 while ($arr = db_fetch_array($result)) {
500 $values .= $comma.$arr['email'];
508 * isMonitoredBy - get the monitored status of this document for a specific user id.
511 * @return boolean true if monitored by this user
513 function isMonitoredBy($userid = 'ALL') {
514 if ( $userid == 'ALL' ) {
517 $condition = 'user_id='.$userid.' AND';
519 $result = db_query_params('SELECT * FROM docdata_monitored_docman WHERE '.$condition.' doc_id=$1',
520 array($this->getID()));
522 if (!$result || db_numrows($result) < 1)
529 * removeMonitoredBy - remove this document for a specific user id for monitoring.
532 * @return boolean true if success
534 function removeMonitoredBy($userid) {
535 $result = db_query_params('DELETE FROM docdata_monitored_docman WHERE doc_id=$1 AND user_id=$2',
536 array ($this->getID(), $userid));
539 $this->setError(_('Unable To Remove Monitor').' : '.db_error());
546 * addMonitoredBy - add this document for a specific user id for monitoring.
549 * @return boolean true if success
551 function addMonitoredBy($userid) {
552 $result = db_query_params('SELECT * FROM docdata_monitored_docman WHERE user_id=$1 AND doc_id=$2',
553 array($userid, $this->getID()));
555 if (!$result || db_numrows($result) < 1) {
556 $result = db_query_params('INSERT INTO docdata_monitored_docman (doc_id,user_id) VALUES ($1,$2)',
557 array($this->getID(), $userid));
560 $this->setError(_('Unable To Add Monitor').' : '.db_error());
568 * setState - set the stateid of the document.
570 * @param int The state id of the doc_states table.
571 * @return boolean success.
573 function setState($stateid) {
574 $res = db_query_params('UPDATE doc_data SET
579 $this->Group->getID(),
582 if (!$res || db_affected_rows($res) < 1) {
583 $this->setOnUpdateError(db_error());
586 $this->sendNotice(false);
591 * setLock - set the locking status of the document
593 * @param int The status of the lock
594 * @param int The userid who set the lock
595 * @param time the epoch time
596 * @return boolean success
598 function setLock($stateLock, $userid = NULL, $thistime = 0) {
599 $res = db_query_params('UPDATE doc_data SET
608 $this->Group->getID(),
611 if (!$res || db_affected_rows($res) < 1) {
612 $this->setOnUpdateError(_('Document lock failed').' '.db_error());
615 $this->data_array['locked'] = $stateLock;
616 $this->data_array['locked_by'] = $userid;
617 $this->data_array['lockdate'] = $thistime;
622 * setReservedBy - set the reserved status of the document and the owner
624 * @param int The status of the reserved
625 * @param int The ID of the owner : by default : noone
626 * @return boolean success
628 function setReservedBy($statusReserved, $idReserver = NULL) {
629 $res = db_query_params('UPDATE doc_data SET
634 array($statusReserved,
636 $this->Group->getID(),
639 if (!$res || db_affected_rows($res) < 1) {
640 $this->setOnUpdateError(_('Document reservation failed').' '.db_error());
643 $this->sendNotice(false);
648 * update - use this function to update an existing entry in the database.
650 * @param string The filename of this document. Can be a URL.
651 * @param string The filetype of this document. If filename is URL, this should be 'URL';
652 * @param string The contents of this document.
653 * @param int The doc_group id of the doc_groups table.
654 * @param string The title of this document.
655 * @param string The description of this document.
656 * @param int The state id of the doc_states table.
657 * @return boolean success.
659 function update($filename, $filetype, $data, $doc_group, $title, $description, $stateid) {
662 $perm =& $this->Group->getPermission();
663 if (!$perm || !is_object($perm) || !$perm->isDocEditor()) {
664 $this->setPermissionDeniedError();
668 if ($this->getLockedBy() != $LUSER->getID()) {
669 $this->setPermissionDeniedError();
673 if (strlen($title) < 5) {
674 $this->setError(_('Title Must Be At Least 5 Characters'));
678 if (strlen($description) < 10) {
679 $this->setError(_('Document Description Must Be At Least 10 Characters'));
684 $result = db_query_params('SELECT filename, doc_group FROM docdata_vw WHERE filename = $1 and doc_group = $2 and stateid = $3',
685 array($filename, $doc_group, $stateid));
686 if (!$result || db_numrows($result) > 0) {
687 $this->setError(_('Document already published in this directory'));
692 $res = db_query_params('UPDATE doc_data SET
704 array(htmlspecialchars($title),
705 htmlspecialchars($description),
713 $this->Group->getID(),
717 if (!$res || db_affected_rows($res) < 1) {
718 $this->setOnUpdateError(db_error());
725 // key words for in-document search
726 if ($this->Group->useDocmanSearch()) {
727 $kw = new Parsedata($this->engine_path);
728 $kwords = $kw->get_parse_data($data1, htmlspecialchars($title), htmlspecialchars($description), $filetype);
733 $res = db_query_params('UPDATE doc_data SET filesize=$1, data_words=$2 WHERE group_id=$3 AND docid=$4',
736 $this->Group->getID(),
740 if (!$res || db_affected_rows($res) < 1) {
741 $this->setOnUpdateError(db_error());
745 switch ($this->Group->getStorageAPI()) {
747 $res = db_query_params('UPDATE doc_data SET data = $1 where group_id = $2 and docid = $3',
748 array(base64_encode($data),
749 $this->Group->getID(),
753 if (!$res || db_affected_rows($res) < 1) {
754 $this->setOnUpdateError(db_error());
760 $this->setOnUpdateError(_('No Storage API'));
766 $this->sendNotice(false);
771 * sendNotice - Notifies of document submissions
773 * @param boolean true = new document (default value)
775 function sendNotice ($new=true) {
776 $BCC = $this->Group->getDocEmailAddress();
777 if ($this->isMonitoredBy('ALL')) {
778 $BCC .= $this->getMonitoredUserEmailAddress();
780 if (strlen($BCC) > 0) {
782 $status = _('New document');
784 $status = _('Updated document');
786 $subject = '['.$this->Group->getPublicName().'] '.$status.' - '.$this->getName();
787 $body = _('Project:').' '.$this->Group->getPublicName()."\n";
788 $body .= _('Directory:').' '.$this->getDocGroupName()."\n";
789 $body .= _('Document title:').' '.$this->getName()."\n";
790 $body .= _('Document description:').' '.util_unconvert_htmlspecialchars($this->getDescription())."\n";
791 $body .= _('Submitter:').' '.$this->getCreatorRealName()." (".$this->getCreatorUserName().") \n";
792 $body .= "\n\n-------------------------------------------------------\n".
793 _('For more info, visit:').
794 "\n\n" . util_make_url('/docman/?group_id='.$this->Group->getID().'&view=listfile&dirid='.$this->getDocGroupID());
796 util_send_message('', $subject, $body, '', $BCC);
803 * delete - Delete this file
805 * @return boolean success
808 $perm =& $this->Group->getPermission();
809 if (!$perm || !is_object($perm) || !$perm->isDocEditor()) {
810 $this->setPermissionDeniedError();
814 $result = db_query_params('DELETE FROM doc_data WHERE docid=$1',
815 array($this->getID()));
817 $this->setError(_('Error Deleting Document:').' '.db_error());
822 switch ($this->Group->getStorageAPI()) {
827 $this->setError(_('Error Deleting Document: No Storage API'));
833 // we should be able to send a notice that this doc has been deleted .... but we need to rewrite sendNotice
834 //$this->sendNotice(false);
841 // c-file-style: "bsd"