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-2011, Franck Villaume - Capgemini
9 * Copyright 2011-2012, Franck Villaume - TrivialDev
10 * Copyright (C) 2011 Alain Peyrat - Alcatel-Lucent
11 * http://fusionforge.org
13 * This file is part of FusionForge. FusionForge is free software;
14 * you can redistribute it and/or modify it under the terms of the
15 * GNU General Public License as published by the Free Software
16 * Foundation; either version 2 of the Licence, or (at your option)
19 * FusionForge is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License along
25 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
26 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 require_once $gfcommon.'include/Error.class.php';
30 require_once $gfcommon.'docman/Parsedata.class.php';
31 require_once $gfcommon.'docman/DocumentManager.class.php';
32 require_once $gfcommon.'docman/DocumentGroup.class.php';
35 class Document extends Error {
38 * Associative array of data from db.
40 * @var array $data_array.
54 * @param object The Group object to which this document is associated.
55 * @param int The docid.
56 * @param array The associative array of data.
57 * @return boolean success.
59 function Document(&$Group, $docid = false, $arr = false) {
61 if (!$Group || !is_object($Group)) {
62 $this->setNotValidGroupObjectError();
65 if ($Group->isError()) {
66 $this->setError('Document:: '. $Group->getErrorMessage());
69 $this->Group =& $Group;
72 if (!$arr || !is_array($arr)) {
73 if (!$this->fetchData($docid)) {
77 $this->data_array =& $arr;
78 if ($this->data_array['group_id'] != $this->Group->getID()) {
79 $this->setError('Document:: '. _('Group_id in db result does not match Group Object'));
80 $this->data_array = null;
84 if (!$this->isPublic()) {
85 $perm =& $this->Group->getPermission();
87 if (!$perm || !is_object($perm) || !$perm->isDocEditor()) {
88 $this->setPermissionDeniedError();
89 $this->data_array = null;
98 * create - use this function to create a new entry in the database.
100 * @param string The filename of this document. Can be a URL.
101 * @param string The filetype of this document. If filename is URL, this should be 'URL';
102 * @param string The contents of this document.
103 * @param int The doc_group id of the doc_groups table.
104 * @param string The title of this document.
105 * @param string The description of this document.
106 * @param int The state id of the document. At creation, can not be deleted status.
107 * @return boolean success.
109 function create($filename, $filetype, $data, $doc_group, $title, $description, $stateid = 0) {
110 if (strlen($title) < 5) {
111 $this->setError(_('Title Must Be At Least 5 Characters'));
114 if (strlen($description) < 10) {
115 $this->setError(_('Document Description Must Be At Least 10 Characters'));
119 $user_id = ((session_loggedin()) ? user_getid() : 100);
121 $doc_initstatus = '3';
122 $perm =& $this->Group->getPermission();
123 if ($perm && is_object($perm) && $perm->isDocEditor()) {
124 if ($stateid && $stateid != 2) {
125 $doc_initstatus = $stateid;
127 $doc_initstatus = '1';
131 $result = db_query_params('SELECT filename, doc_group from docdata_vw
135 array($filename, $doc_group, $doc_initstatus));
137 if (!$result || db_numrows($result) > 0) {
138 $this->setError(_('Document already published in this directory'));
142 $result = db_query_params('SELECT title FROM docdata_vw where title = $1 AND doc_group = $2',
143 array($title, $doc_group));
144 if (!$result || db_numrows($result) > 0) {
145 $this->setError(_('Document already published in this directory'));
149 // If $filetype is "text/plain", $body convert UTF-8 encoding.
150 if (strcasecmp($filetype,"text/plain") === 0 &&
151 function_exists('mb_convert_encoding') &&
152 function_exists('mb_detect_encoding')) {
153 $data = mb_convert_encoding($data, 'UTF-8', mb_detect_encoding($data));
157 // key words for in-document search
158 if ($this->Group->useDocmanSearch()) {
159 $kw = new Parsedata();
160 $kwords = $kw->get_parse_data($data1, htmlspecialchars($title), htmlspecialchars($description), $filetype);
165 $filesize = strlen($data);
168 $result = db_query_params('INSERT INTO doc_data (group_id,title,description,createdate,doc_group,
169 stateid,filename,filetype,filesize,data_words,created_by)
170 VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11)',
171 array($this->Group->getId(),
172 htmlspecialchars($title),
173 htmlspecialchars($description),
183 $this->setError(_('Error Adding Document:').' '.db_error().$result);
188 $docid = db_insertid($result,'doc_data','docid');
190 switch ($this->Group->getStorageAPI()) {
192 $result = db_query_params('UPDATE doc_data set data = $1 where docid = $2',
193 array(base64_encode($data),$docid));
195 $this->setError(_('Error Adding Document:').' '.db_error().$result);
202 $this->setError(_('Error Adding Document: No Storage API'));
208 if (!$this->fetchData($docid)) {
209 $this->setError(_('Error fetching Document'));
214 $localDg = new DocumentGroup($this->Group, $doc_group);
215 if (!$localDg->update($localDg->getName(), $localDg->getParentID(), 1)) {
216 $this->setError(_('Error updating document group:').$localDg->getErrorMessage());
220 $this->sendNotice(true);
226 * fetchData() - re-fetch the data for this document from the database.
228 * @param int The document id.
229 * @return boolean success
231 function fetchData($docid) {
232 $res = db_query_params('SELECT * FROM docdata_vw WHERE docid=$1 AND group_id=$2',
233 array($docid, $this->Group->getID()));
234 if (!$res || db_numrows($res) < 1) {
235 $this->setError(_('Document: Invalid docid'));
238 $this->data_array = db_fetch_array($res);
239 db_free_result($res);
244 * getGroup - get the Group object this Document is associated with.
246 * @return Object The Group object.
248 function &getGroup() {
253 * getID - get this docid.
255 * @return int The docid.
258 return $this->data_array['docid'];
262 * getName - get the name of this document.
264 * @return string The name of this document.
267 return $this->data_array['title'];
271 * getDescription - the description of this document.
273 * @return string The description.
275 function getDescription() {
276 return $this->data_array['description'];
280 * isURL - whether this document is a URL and not a local file.
282 * @return boolean is_url.
285 return ($this->data_array['filetype'] == 'URL');
289 * isText - whether this document is a text document and not a binary one.
291 * @return boolean is_text.
294 $doctype = $this->data_array['filetype'];
295 if (preg_match('|^text/|i', $doctype)) { // text plain, text html, text x-patch, etc
302 * isHtml - whether this document is a html document.
304 * @return boolean is_html.
307 $doctype = $this->data_array['filetype'];
308 if (preg_match('/html/i',$doctype)) {
315 * isPublic - whether this document is available to the general public.
317 * @return boolean is_public.
319 function isPublic() {
320 return (($this->data_array['stateid'] == 1) ? true : false);
324 * getStateID - get this stateid.
326 * @return int The stateid.
328 function getStateID() {
329 return $this->data_array['stateid'];
333 * getStateName - the statename of this document.
335 * @return string The statename.
337 function getStateName() {
338 return $this->data_array['state_name'];
342 * getDocGroupID - get this doc_group_id.
344 * @return int The doc_group_id.
346 function getDocGroupID() {
347 return $this->data_array['doc_group'];
351 * getDocGroupName - the doc_group_name of this document.
353 * @return string The docgroupname.
355 function getDocGroupName() {
356 return $this->data_array['group_name'];
360 * getCreatorID - get this creator's user_id.
362 * @return int The user_id.
364 function getCreatorID() {
365 return $this->data_array['created_by'];
369 * getCreatorUserName - the unix name of the person who created this document.
371 * @return string The unix name of the creator.
373 function getCreatorUserName() {
374 return $this->data_array['user_name'];
378 * getCreatorRealName - the real name of the person who created this document.
380 * @return string The real name of the creator.
382 function getCreatorRealName() {
383 return $this->data_array['realname'];
387 * getCreatorEmail - the email of the person who created this document.
389 * @return string The email of the creator.
391 function getCreatorEmail() {
392 return $this->data_array['email'];
396 * getFileName - the filename of this document.
398 * @return string The filename.
400 function getFileName() {
401 return $this->data_array['filename'];
405 * getFileType - the filetype of this document.
407 * @return string The filetype.
409 function getFileType() {
410 return $this->data_array['filetype'];
414 * getFileData - the filedata of this document.
416 * @return string The filedata.
418 function getFileData() {
420 // Because this could be a large string, we only fetch if we actually need it
422 $res = db_query_params('SELECT data FROM doc_data WHERE docid=$1', array($this->getID()));
423 return base64_decode(db_result($res, 0, 'data'));
427 * getFileSize - Return the size of the document
429 * @return int The file size
431 function getFileSize() {
432 return $this->data_array['filesize'];
436 * getUpdated - get the time this document was updated.
438 * @return int The epoch date this document was updated.
440 function getUpdated() {
441 return $this->data_array['updatedate'];
445 * getCreated - get the time this document was created.
447 * @return int The epoch date this document was created.
449 function getCreated() {
450 return $this->data_array['createdate'];
454 * getLocked - get the lock status of this document.
456 * @return int The lock status of this document.
458 function getLocked() {
459 return $this->data_array['locked'];
463 * getLockdate - get the lock time of this document.
465 * @return int The lock time of this document.
467 function getLockdate() {
468 return $this->data_array['lockdate'];
472 * getLockedBy - get the user id who set lock on this document.
474 * @return int The user id who set lock on this document.
476 function getLockedBy() {
477 return $this->data_array['locked_by'];
481 * getReservedBy - get the owner of the reversed status of this document.
483 * @return int The owner of the reversed status of this document.
485 function getReservedBy() {
486 return $this->data_array['reserved_by'];
490 * getReserved - get the reversed status of this document.
492 * @return int The reversed status of this document.
494 function getReserved() {
495 return $this->data_array['reserved'];
499 * getMonitoredUserEmailAddress - get the email addresses of users who monitor this file
501 * @return string The list of emails comma separated
503 function getMonitoredUserEmailAddress() {
504 $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()));
505 if (!$result || db_numrows($result) < 1) {
511 while ($arr = db_fetch_array($result)) {
515 $values .= $comma.$arr['email'];
523 * isMonitoredBy - get the monitored status of this document for a specific user id.
526 * @return boolean true if monitored by this user
528 function isMonitoredBy($userid = 'ALL') {
529 if ( $userid == 'ALL' ) {
532 $condition = 'user_id='.$userid.' AND';
534 $result = db_query_params('SELECT * FROM docdata_monitored_docman WHERE '.$condition.' doc_id=$1',
535 array($this->getID()));
537 if (!$result || db_numrows($result) < 1)
544 * removeMonitoredBy - remove this document for a specific user id for monitoring.
547 * @return boolean true if success
549 function removeMonitoredBy($userid) {
550 $result = db_query_params('DELETE FROM docdata_monitored_docman WHERE doc_id=$1 AND user_id=$2',
551 array($this->getID(), $userid));
554 $this->setError(_('Unable To Remove Monitor').' : '.db_error());
561 * addMonitoredBy - add this document for a specific user id for monitoring.
564 * @return boolean true if success
566 function addMonitoredBy($userid) {
567 $result = db_query_params('SELECT * FROM docdata_monitored_docman WHERE user_id=$1 AND doc_id=$2',
568 array($userid, $this->getID()));
570 if (!$result || db_numrows($result) < 1) {
571 $result = db_query_params('INSERT INTO docdata_monitored_docman (doc_id,user_id) VALUES ($1,$2)',
572 array($this->getID(), $userid));
575 $this->setError(_('Unable To Add Monitor').' : '.db_error());
583 * clearMonitor - remove all entries of monitoring for this document.
585 * @return boolean true if success.
587 function clearMonitor() {
588 $result = db_query_params('DELETE FROM docdata_monitored_docman WHERE doc_id = $1',
589 array($this->getID()));
594 * setState - set the stateid of the document.
596 * @param int The state id of the doc_states table.
597 * @return boolean success or not.
599 function setState($stateid) {
600 return $this->__setValueinDB('stateid', $stateid);
605 * setDocGroupID - set the doc_group of the document.
607 * @param int The group_id of this document.
608 * @return boolean success or not.
610 function setDocGroupID($newdocgroupid) {
611 return $this->__setValueinDB('doc_group', $newdocgroupid);
615 * setLock - set the locking status of the document.
617 * @param int The status of the lock.
618 * @param int The userid who set the lock.
619 * @param time the epoch time.
620 * @return boolean success or not.
622 function setLock($stateLock, $userid = NULL, $thistime = 0) {
623 $res = db_query_params('UPDATE doc_data SET
632 $this->Group->getID(),
635 if (!$res || db_affected_rows($res) < 1) {
636 $this->setOnUpdateError(_('Document lock failed').' '.db_error());
639 $this->data_array['locked'] = $stateLock;
640 $this->data_array['locked_by'] = $userid;
641 $this->data_array['lockdate'] = $thistime;
646 * setReservedBy - set the reserved status of the document and the owner
648 * @param int The status of the reserved
649 * @param int The ID of the owner : by default : noone
650 * @return boolean success
652 function setReservedBy($statusReserved, $idReserver = NULL) {
653 $res = db_query_params('UPDATE doc_data SET
658 array($statusReserved,
660 $this->Group->getID(),
663 if (!$res || db_affected_rows($res) < 1) {
664 $this->setOnUpdateError(_('Document reservation failed').' '.db_error());
667 $this->sendNotice(false);
672 * getFileTypeImage - return the file image for icon
674 * @return string the file image name
677 function getFileTypeImage() {
678 switch ($this->getFileType()) {
683 case "image/vnd.microsoft.icon":
684 case "image/svg+xml": {
685 $image = 'docman/file_type_image.png';
689 case "audio/x-vorbis+ogg":
691 $image = "docman/file_type_sound.png";
694 case "application/pdf": {
695 $image = 'docman/file_type_pdf.png';
700 $image = 'docman/file_type_html.png';
705 case "application/xml":
707 $image = 'docman/file_type_plain.png';
710 case "application/msword":
711 case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
712 case "application/vnd.oasis.opendocument.text": {
713 $image = 'docman/file_type_writer.png';
716 case "application/vnd.ms-excel":
717 case "application/vnd.oasis.opendocument.spreadsheet":
718 case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": {
719 $image = 'docman/file_type_spreadsheet.png';
722 case "application/vnd.oasis.opendocument.presentation":
723 case "application/vnd.ms-powerpoint":
724 case "application/vnd.ms-office":
725 case "application/vnd.openxmlformats-officedocument.presentationml.presentation": {
726 $image = 'docman/file_type_presentation.png';
729 case "application/zip":
730 case "application/x-tar":
731 case "application/x-rpm":
732 case "application/x-rar-compressed":
733 case "application/x-bzip2":
734 case "application/x-gzip":
735 case "application/x-lzip":
736 case "application/x-compress":
737 case "application/x-7z-compressed":
738 case "application/x-gtar":
739 case "application/x-stuffitx":
740 case "application/x-lzx":
741 case "application/x-lzh":
742 case "application/x-gca-compressed":
743 case "application/x-apple-diskimage":
744 case "application/x-dgc-compressed":
745 case "application/x-dar":
746 case "application/x-cfs-compressed":
747 case "application/vnd.ms-cab-compressed":
748 case "application/x-alz-compressed":
749 case "application/x-astrotite-afa":
750 case "application/x-ace-compressed":
751 case "application/x-cpio":
752 case "application/x-shar":
753 case "application/x-xz": {
754 $image = 'docman/file_type_archive.png';
758 $image = 'docman/file_type_unknown.png';
765 * update - use this function to update an existing entry in the database.
767 * @param string The filename of this document. Can be a URL.
768 * @param string The filetype of this document. If filename is URL, this should be 'URL';
769 * @param string The contents of this document.
770 * @param int The doc_group id of the doc_groups table.
771 * @param string The title of this document.
772 * @param string The description of this document.
773 * @param int The state id of the doc_states table.
774 * @return boolean success.
776 function update($filename, $filetype, $data, $doc_group, $title, $description, $stateid) {
778 $perm =& $this->Group->getPermission();
779 if (!$perm || !is_object($perm) || !$perm->isDocEditor()) {
780 $this->setPermissionDeniedError();
784 $user = session_get_user();
785 if ($this->getLocked() && ($this->getLockedBy() != $user->getID())) {
786 $this->setPermissionDeniedError();
790 if (strlen($title) < 5) {
791 $this->setError(_('Title Must Be At Least 5 Characters'));
795 if (strlen($description) < 10) {
796 $this->setError(_('Document Description Must Be At Least 10 Characters'));
800 /* TODO : NEED REAL CHECK */
802 $result = db_query_params('SELECT filename, doc_group FROM docdata_vw WHERE filename = $1 AND doc_group = $2 AND stateid = $3 AND docid != $4',
803 array($filename, $doc_group, $stateid, $this->getID()));
804 if (!$result || db_numrows($result) > 0) {
805 $this->setError(_('Document already published in this directory'));
810 $res = db_query_params('UPDATE doc_data SET
822 array(htmlspecialchars($title),
823 htmlspecialchars($description),
831 $this->Group->getID(),
835 if (!$res || db_affected_rows($res) < 1) {
836 $this->setOnUpdateError(db_error());
843 // key words for in-document search
844 if ($this->Group->useDocmanSearch()) {
845 $kw = new Parsedata();
846 $kwords = $kw->get_parse_data($data1, htmlspecialchars($title), htmlspecialchars($description), $filetype);
851 $res = db_query_params('UPDATE doc_data SET filesize=$1, data_words=$2 WHERE group_id=$3 AND docid=$4',
854 $this->Group->getID(),
858 if (!$res || db_affected_rows($res) < 1) {
859 $this->setOnUpdateError(db_error());
863 switch ($this->Group->getStorageAPI()) {
865 $res = db_query_params('UPDATE doc_data SET data = $1 where group_id = $2 and docid = $3',
866 array(base64_encode($data),
867 $this->Group->getID(),
871 if (!$res || db_affected_rows($res) < 1) {
872 $this->setOnUpdateError(db_error());
878 $this->setOnUpdateError(_('No Storage API'));
884 $this->sendNotice(false);
889 * sendNotice - Notifies of document submissions
891 * @param boolean true = new document (default value)
893 function sendNotice($new=true) {
894 $BCC = $this->Group->getDocEmailAddress();
895 if ($this->isMonitoredBy('ALL')) {
896 $BCC .= $this->getMonitoredUserEmailAddress();
898 if (strlen($BCC) > 0) {
899 $sess = session_get_user();
901 $status = _('New document');
903 $status = _('Updated document').' '._('by').' ' . $sess->getRealName();
905 $subject = '['.$this->Group->getPublicName().'] '.$status.' - '.$this->getName();
906 $body = _('Project:').' '.$this->Group->getPublicName()."\n";
907 $body .= _('Directory:').' '.$this->getDocGroupName()."\n";
908 $body .= _('Document title:').' '.$this->getName()."\n";
909 $body .= _('Document description:').' '.util_unconvert_htmlspecialchars($this->getDescription())."\n";
910 $body .= _('Submitter:').' '.$this->getCreatorRealName()." (".$this->getCreatorUserName().") \n";
912 $body .= _('Updated By:').' '. $sess->getRealName ();
914 $body .= "\n\n-------------------------------------------------------\n".
915 _('For more info, visit:').
916 "\n\n" . util_make_url('/docman/?group_id='.$this->Group->getID().'&view=listfile&dirid='.$this->getDocGroupID());
918 util_send_message('', $subject, $body, '', $BCC);
925 * delete - Delete this file
927 * @return boolean success
930 $perm =& $this->Group->getPermission();
931 if (!$perm || !is_object($perm) || !$perm->isDocEditor()) {
932 $this->setPermissionDeniedError();
936 $result = db_query_params('DELETE FROM doc_data WHERE docid=$1',
937 array($this->getID()));
939 $this->setError(_('Error Deleting Document:').' '.db_error());
944 switch ($this->Group->getStorageAPI()) {
949 $this->setError(_('Error Deleting Document: No Storage API'));
955 /** we should be able to send a notice that this doc has been deleted .... but we need to rewrite sendNotice
956 * $this->sendNotice(false);
957 * @TODO delete monitoring this file */
962 * trash - move this file to trash
964 * @return boolean success or not.
967 $this->setState('2');
968 $dm = new DocumentManager($this->Group);
969 $this->setDocGroupID($dm->getTrashID());
971 $this->setReservedBy(0);
972 $this->sendNotice(false);
973 $this->clearMonitor();
978 * __setValueinDB - private function to update columns in db
980 * @param string the column to update
981 * @param int the value to store
982 * @return boolean success or not
985 private function __setValueinDB($column, $value) {
989 $qpa = db_construct_qpa();
990 $qpa = db_construct_qpa($qpa, 'UPDATE doc_data SET ');
991 $qpa = db_construct_qpa($qpa, $column);
992 $qpa = db_construct_qpa($qpa, '=$1
996 $this->Group->getID(),
998 $res = db_query_qpa($qpa);
999 if (!$res || db_affected_rows($res) < 1) {
1000 $this->setOnUpdateError(db_error().print_r($qpa));
1006 $this->setOnUpdateError(_('wrong column name'));
1009 $this->sendNotice(false);
1013 function createVersion() {
1017 function deleteVersion() {
1024 // c-file-style: "bsd"