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 * Copyright 2012, Franck Villaume - TrivialDev
12 * http://fusionforge.org
14 * This file is part of FusionForge. FusionForge is free software;
15 * you can redistribute it and/or modify it under the terms of the
16 * GNU General Public License as published by the Free Software
17 * Foundation; either version 2 of the Licence, or (at your option)
20 * FusionForge is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License along
26 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
27 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 require_once $gfcommon.'include/Error.class.php';
31 require_once $gfcommon.'docman/Parsedata.class.php';
32 require_once $gfcommon.'docman/DocumentManager.class.php';
33 require_once $gfcommon.'docman/DocumentGroup.class.php';
36 class Document extends Error {
39 * Associative array of data from db.
41 * @var array $data_array.
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) {
62 if (!$Group || !is_object($Group)) {
63 $this->setError('Document:: '. _('No Valid Group Object'));
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('Document:: '. _('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->isDocEditor()) {
89 $this->setPermissionDeniedError();
90 $this->data_array = null;
99 * create - use this function to create a new entry in the database.
101 * @param string The filename of this document. Can be a URL.
102 * @param string The filetype of this document. If filename is URL, this should be 'URL';
103 * @param string The contents of this document.
104 * @param int The doc_group id of the doc_groups table.
105 * @param string The title of this document.
106 * @param string The description of this document.
107 * @param int The state id of the document. At creation, can not be deleted status.
108 * @return boolean success.
110 function create($filename, $filetype, $data, $doc_group, $title, $description, $stateid = 0) {
111 if (strlen($title) < 5) {
112 $this->setError(_('Title Must Be At Least 5 Characters'));
115 if (strlen($description) < 10) {
116 $this->setError(_('Document Description Must Be At Least 10 Characters'));
120 $user_id = ((session_loggedin()) ? user_getid() : 100);
122 $doc_initstatus = '3';
123 $perm =& $this->Group->getPermission();
124 if ($perm && is_object($perm) && $perm->isDocEditor()) {
125 if ($stateid && $stateid != 2) {
126 $doc_initstatus = $stateid;
128 $doc_initstatus = '1';
132 $result = db_query_params('SELECT filename, doc_group from docdata_vw
136 array($filename, $doc_group, $doc_initstatus));
138 if (!$result || db_numrows($result) > 0) {
139 $this->setError(_('Document already published in this directory'));
143 $result = db_query_params('SELECT title FROM docdata_vw where title = $1 AND doc_group = $2',
144 array($title, $doc_group));
145 if (!$result || db_numrows($result) > 0) {
146 $this->setError(_('Document already published in this directory'));
150 // If $filetype is "text/plain", $body convert UTF-8 encoding.
151 if (strcasecmp($filetype,"text/plain") === 0 &&
152 function_exists('mb_convert_encoding') &&
153 function_exists('mb_detect_encoding')) {
154 $data = mb_convert_encoding($data, 'UTF-8', mb_detect_encoding($data));
158 // key words for in-document search
159 if ($this->Group->useDocmanSearch()) {
160 $kw = new Parsedata();
161 $kwords = $kw->get_parse_data($data1, htmlspecialchars($title), htmlspecialchars($description), $filetype);
166 $filesize = strlen($data);
169 $result = db_query_params('INSERT INTO doc_data (group_id,title,description,createdate,doc_group,
170 stateid,filename,filetype,filesize,data_words,created_by)
171 VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11)',
172 array($this->Group->getId(),
173 htmlspecialchars($title),
174 htmlspecialchars($description),
184 $this->setError(_('Error Adding Document:').' '.db_error().$result);
189 $docid = db_insertid($result,'doc_data','docid');
191 switch ($this->Group->getStorageAPI()) {
193 $result = db_query_params('UPDATE doc_data set data = $1 where docid = $2',
194 array(base64_encode($data),$docid));
196 $this->setError(_('Error Adding Document:').' '.db_error().$result);
203 $this->setError(_('Error Adding Document: No Storage API'));
209 if (!$this->fetchData($docid)) {
210 $this->setError(_('Error fetching Document'));
215 $localDg = new DocumentGroup($this->Group, $doc_group);
216 if (!$localDg->update($localDg->getName(), $localDg->getParentID(), 1)) {
217 $this->setError(_('Error updating document group:').$localDg->getErrorMessage());
221 $this->sendNotice(true);
227 * fetchData() - re-fetch the data for this document from the database.
229 * @param int The document id.
230 * @return boolean success
232 function fetchData($docid) {
233 $res = db_query_params('SELECT * FROM docdata_vw WHERE docid=$1 AND group_id=$2',
234 array($docid, $this->Group->getID()));
235 if (!$res || db_numrows($res) < 1) {
236 $this->setError(_('Document: Invalid docid'));
239 $this->data_array = db_fetch_array($res);
240 db_free_result($res);
245 * getGroup - get the Group object this Document is associated with.
247 * @return Object The Group object.
249 function &getGroup() {
254 * getID - get this docid.
256 * @return int The docid.
259 return $this->data_array['docid'];
263 * getName - get the name of this document.
265 * @return string The name of this document.
268 return $this->data_array['title'];
272 * getDescription - the description of this document.
274 * @return string The description.
276 function getDescription() {
277 return $this->data_array['description'];
281 * isURL - whether this document is a URL and not a local file.
283 * @return boolean is_url.
286 return ($this->data_array['filetype'] == 'URL');
290 * isText - whether this document is a text document and not a binary one.
292 * @return boolean is_text.
295 $doctype = $this->data_array['filetype'];
296 if (preg_match('|^text/|i', $doctype)) { // text plain, text html, text x-patch, etc
303 * isHtml - whether this document is a html document.
305 * @return boolean is_html.
308 $doctype = $this->data_array['filetype'];
309 if (preg_match('/html/i',$doctype)) {
316 * isPublic - whether this document is available to the general public.
318 * @return boolean is_public.
320 function isPublic() {
321 return (($this->data_array['stateid'] == 1) ? true : false);
325 * getStateID - get this stateid.
327 * @return int The stateid.
329 function getStateID() {
330 return $this->data_array['stateid'];
334 * getStateName - the statename of this document.
336 * @return string The statename.
338 function getStateName() {
339 return $this->data_array['state_name'];
343 * getDocGroupID - get this doc_group_id.
345 * @return int The doc_group_id.
347 function getDocGroupID() {
348 return $this->data_array['doc_group'];
352 * getDocGroupName - the doc_group_name of this document.
354 * @return string The docgroupname.
356 function getDocGroupName() {
357 return $this->data_array['group_name'];
361 * getCreatorID - get this creator's user_id.
363 * @return int The user_id.
365 function getCreatorID() {
366 return $this->data_array['created_by'];
370 * getCreatorUserName - the unix name of the person who created this document.
372 * @return string The unix name of the creator.
374 function getCreatorUserName() {
375 return $this->data_array['user_name'];
379 * getCreatorRealName - the real name of the person who created this document.
381 * @return string The real name of the creator.
383 function getCreatorRealName() {
384 return $this->data_array['realname'];
388 * getCreatorEmail - the email of the person who created this document.
390 * @return string The email of the creator.
392 function getCreatorEmail() {
393 return $this->data_array['email'];
397 * getFileName - the filename of this document.
399 * @return string The filename.
401 function getFileName() {
402 return $this->data_array['filename'];
406 * getFileType - the filetype of this document.
408 * @return string The filetype.
410 function getFileType() {
411 return $this->data_array['filetype'];
415 * getFileData - the filedata of this document.
417 * @return string The filedata.
419 function getFileData() {
421 // Because this could be a large string, we only fetch if we actually need it
423 $res = db_query_params('SELECT data FROM doc_data WHERE docid=$1', array($this->getID()));
424 return base64_decode(db_result($res, 0, 'data'));
428 * getFileSize - Return the size of the document
430 * @return int The file size
432 function getFileSize() {
433 return $this->data_array['filesize'];
437 * getUpdated - get the time this document was updated.
439 * @return int The epoch date this document was updated.
441 function getUpdated() {
442 return $this->data_array['updatedate'];
446 * getCreated - get the time this document was created.
448 * @return int The epoch date this document was created.
450 function getCreated() {
451 return $this->data_array['createdate'];
455 * getLocked - get the lock status of this document.
457 * @return int The lock status of this document.
459 function getLocked() {
460 return $this->data_array['locked'];
464 * getLockdate - get the lock time of this document.
466 * @return int The lock time of this document.
468 function getLockdate() {
469 return $this->data_array['lockdate'];
473 * getLockedBy - get the user id who set lock on this document.
475 * @return int The user id who set lock on this document.
477 function getLockedBy() {
478 return $this->data_array['locked_by'];
482 * getReservedBy - get the owner of the reversed status of this document.
484 * @return int The owner of the reversed status of this document.
486 function getReservedBy() {
487 return $this->data_array['reserved_by'];
491 * getReserved - get the reversed status of this document.
493 * @return int The reversed status of this document.
495 function getReserved() {
496 return $this->data_array['reserved'];
500 * getMonitoredUserEmailAddress - get the email addresses of users who monitor this file
502 * @return string The list of emails comma separated
504 function getMonitoredUserEmailAddress() {
505 $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()));
506 if (!$result || db_numrows($result) < 1) {
512 while ($arr = db_fetch_array($result)) {
516 $values .= $comma.$arr['email'];
524 * isMonitoredBy - get the monitored status of this document for a specific user id.
527 * @return boolean true if monitored by this user
529 function isMonitoredBy($userid = 'ALL') {
530 if ( $userid == 'ALL' ) {
533 $condition = 'user_id='.$userid.' AND';
535 $result = db_query_params('SELECT * FROM docdata_monitored_docman WHERE '.$condition.' doc_id=$1',
536 array($this->getID()));
538 if (!$result || db_numrows($result) < 1)
545 * removeMonitoredBy - remove this document for a specific user id for monitoring.
548 * @return boolean true if success
550 function removeMonitoredBy($userid) {
551 $result = db_query_params('DELETE FROM docdata_monitored_docman WHERE doc_id=$1 AND user_id=$2',
552 array($this->getID(), $userid));
555 $this->setError(_('Unable To Remove Monitor').' : '.db_error());
562 * addMonitoredBy - add this document for a specific user id for monitoring.
565 * @return boolean true if success
567 function addMonitoredBy($userid) {
568 $result = db_query_params('SELECT * FROM docdata_monitored_docman WHERE user_id=$1 AND doc_id=$2',
569 array($userid, $this->getID()));
571 if (!$result || db_numrows($result) < 1) {
572 $result = db_query_params('INSERT INTO docdata_monitored_docman (doc_id,user_id) VALUES ($1,$2)',
573 array($this->getID(), $userid));
576 $this->setError(_('Unable To Add Monitor').' : '.db_error());
584 * clearMonitor - remove all entries of monitoring for this document.
586 * @return boolean true if success.
588 function clearMonitor() {
589 $result = db_query_params('DELETE FROM docdata_monitored_docman WHERE doc_id = $1',
590 array($this->getID()));
595 * setState - set the stateid of the document.
597 * @param int The state id of the doc_states table.
598 * @return boolean success or not.
600 function setState($stateid) {
601 return $this->__setValueinDB('stateid', $stateid);
606 * setDocGroupID - set the doc_group of the document.
608 * @param int The group_id of this document.
609 * @return boolean success or not.
611 function setDocGroupID($newdocgroupid) {
612 return $this->__setValueinDB('doc_group', $newdocgroupid);
616 * setLock - set the locking status of the document.
618 * @param int The status of the lock.
619 * @param int The userid who set the lock.
620 * @param time the epoch time.
621 * @return boolean success or not.
623 function setLock($stateLock, $userid = NULL, $thistime = 0) {
624 $res = db_query_params('UPDATE doc_data SET
633 $this->Group->getID(),
636 if (!$res || db_affected_rows($res) < 1) {
637 $this->setOnUpdateError(_('Document lock failed').' '.db_error());
640 $this->data_array['locked'] = $stateLock;
641 $this->data_array['locked_by'] = $userid;
642 $this->data_array['lockdate'] = $thistime;
647 * setReservedBy - set the reserved status of the document and the owner
649 * @param int The status of the reserved
650 * @param int The ID of the owner : by default : noone
651 * @return boolean success
653 function setReservedBy($statusReserved, $idReserver = NULL) {
654 $res = db_query_params('UPDATE doc_data SET
659 array($statusReserved,
661 $this->Group->getID(),
664 if (!$res || db_affected_rows($res) < 1) {
665 $this->setOnUpdateError(_('Document reservation failed').' '.db_error());
668 $this->sendNotice(false);
673 * getFileTypeImage - return the file image for icon
675 * @return string the file image name
678 function getFileTypeImage() {
679 switch ($this->getFileType()) {
684 case "image/vnd.microsoft.icon":
685 case "image/svg+xml": {
686 $image = 'docman/file_type_image.png';
690 case "audio/x-vorbis+ogg":
692 $image = "docman/file_type_sound.png";
695 case "application/pdf": {
696 $image = 'docman/file_type_pdf.png';
701 $image = 'docman/file_type_html.png';
706 case "application/xml":
708 $image = 'docman/file_type_plain.png';
711 case "application/msword":
712 case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
713 case "application/vnd.oasis.opendocument.text": {
714 $image = 'docman/file_type_writer.png';
717 case "application/vnd.ms-excel":
718 case "application/vnd.oasis.opendocument.spreadsheet":
719 case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": {
720 $image = 'docman/file_type_spreadsheet.png';
723 case "application/vnd.oasis.opendocument.presentation":
724 case "application/vnd.ms-powerpoint":
725 case "application/vnd.ms-office":
726 case "application/vnd.openxmlformats-officedocument.presentationml.presentation": {
727 $image = 'docman/file_type_presentation.png';
730 case "application/zip":
731 case "application/x-tar":
732 case "application/x-rpm":
733 case "application/x-rar-compressed":
734 case "application/x-bzip2":
735 case "application/x-gzip":
736 case "application/x-lzip":
737 case "application/x-compress":
738 case "application/x-7z-compressed":
739 case "application/x-gtar":
740 case "application/x-stuffitx":
741 case "application/x-lzx":
742 case "application/x-lzh":
743 case "application/x-gca-compressed":
744 case "application/x-apple-diskimage":
745 case "application/x-dgc-compressed":
746 case "application/x-dar":
747 case "application/x-cfs-compressed":
748 case "application/vnd.ms-cab-compressed":
749 case "application/x-alz-compressed":
750 case "application/x-astrotite-afa":
751 case "application/x-ace-compressed":
752 case "application/x-cpio":
753 case "application/x-shar":
754 case "application/x-xz": {
755 $image = 'docman/file_type_archive.png';
759 $image = 'docman/file_type_unknown.png';
766 * update - use this function to update an existing entry in the database.
768 * @param string The filename of this document. Can be a URL.
769 * @param string The filetype of this document. If filename is URL, this should be 'URL';
770 * @param string The contents of this document.
771 * @param int The doc_group id of the doc_groups table.
772 * @param string The title of this document.
773 * @param string The description of this document.
774 * @param int The state id of the doc_states table.
775 * @return boolean success.
777 function update($filename, $filetype, $data, $doc_group, $title, $description, $stateid) {
779 $perm =& $this->Group->getPermission();
780 if (!$perm || !is_object($perm) || !$perm->isDocEditor()) {
781 $this->setPermissionDeniedError();
785 $user = session_get_user();
786 if ($this->getLocked() && ($this->getLockedBy() != $user->getID())) {
787 $this->setPermissionDeniedError();
791 if (strlen($title) < 5) {
792 $this->setError(_('Title Must Be At Least 5 Characters'));
796 if (strlen($description) < 10) {
797 $this->setError(_('Document Description Must Be At Least 10 Characters'));
801 /* TODO : NEED REAL CHECK */
803 $result = db_query_params('SELECT filename, doc_group FROM docdata_vw WHERE filename = $1 AND doc_group = $2 AND stateid = $3 AND docid != $4',
804 array($filename, $doc_group, $stateid, $this->getID()));
805 if (!$result || db_numrows($result) > 0) {
806 $this->setError(_('Document already published in this directory'));
811 $res = db_query_params('UPDATE doc_data SET
823 array(htmlspecialchars($title),
824 htmlspecialchars($description),
832 $this->Group->getID(),
836 if (!$res || db_affected_rows($res) < 1) {
837 $this->setOnUpdateError(db_error());
844 // key words for in-document search
845 if ($this->Group->useDocmanSearch()) {
846 $kw = new Parsedata();
847 $kwords = $kw->get_parse_data($data1, htmlspecialchars($title), htmlspecialchars($description), $filetype);
852 $res = db_query_params('UPDATE doc_data SET filesize=$1, data_words=$2 WHERE group_id=$3 AND docid=$4',
855 $this->Group->getID(),
859 if (!$res || db_affected_rows($res) < 1) {
860 $this->setOnUpdateError(db_error());
864 switch ($this->Group->getStorageAPI()) {
866 $res = db_query_params('UPDATE doc_data SET data = $1 where group_id = $2 and docid = $3',
867 array(base64_encode($data),
868 $this->Group->getID(),
872 if (!$res || db_affected_rows($res) < 1) {
873 $this->setOnUpdateError(db_error());
879 $this->setOnUpdateError(_('No Storage API'));
884 $this->fetchData($this->getID());
885 $this->sendNotice(false);
890 * sendNotice - Notifies of document submissions
892 * @param boolean true = new document (default value)
894 function sendNotice($new = true) {
895 $BCC = $this->Group->getDocEmailAddress();
896 if ($this->isMonitoredBy('ALL')) {
897 $BCC .= $this->getMonitoredUserEmailAddress();
899 if (strlen($BCC) > 0) {
900 $sess = session_get_user();
902 $status = _('New document');
904 $status = _('Updated document').' '._('by').' ' . $sess->getRealName();
905 $BCCarray = explode(',',$BCC);
906 foreach ($BCCarray as $dest_email) {
908 $status = _('New document');
910 $status = _('Updated document');
912 $subject = '['.$this->Group->getPublicName().'] '.$status.' - '.$this->getName();
913 $body = _('Project:').' '.$this->Group->getPublicName()."\n";
914 $body .= _('Directory:').' '.$this->getDocGroupName()."\n";
915 $body .= _('Document title:').' '.$this->getName()."\n";
916 $body .= _('Document description:').' '.util_unconvert_htmlspecialchars($this->getDescription())."\n";
917 $body .= _('Submitter:').' '.$this->getCreatorRealName()." (".$this->getCreatorUserName().") \n";
918 $body .= "\n\n-------------------------------------------------------\n".
919 _('For more info, visit:').
920 "\n\n" . util_make_url('/docman/?group_id='.$this->Group->getID().'&view=listfile&dirid='.$this->getDocGroupID());
922 util_send_message($dest_email, $subject, $body, 'noreply@'.forge_get_config('web_host'), '', _('Docman'));
930 * delete - Delete this file
932 * @return boolean success
935 $perm =& $this->Group->getPermission();
936 if (!$perm || !is_object($perm) || !$perm->isDocEditor()) {
937 $this->setPermissionDeniedError();
941 $result = db_query_params('DELETE FROM doc_data WHERE docid=$1',
942 array($this->getID()));
944 $this->setError(_('Error Deleting Document:').' '.db_error());
949 switch ($this->Group->getStorageAPI()) {
954 $this->setError(_('Error Deleting Document: No Storage API'));
960 /** we should be able to send a notice that this doc has been deleted .... but we need to rewrite sendNotice
961 * $this->sendNotice(false);
962 * @TODO delete monitoring this file */
967 * trash - move this file to trash
969 * @return boolean success or not.
972 $this->setState('2');
973 $dm = new DocumentManager($this->Group);
974 $this->setDocGroupID($dm->getTrashID());
976 $this->setReservedBy(0);
977 $this->sendNotice(false);
978 $this->clearMonitor();
983 * __setValueinDB - private function to update columns in db
985 * @param string the column to update
986 * @param int the value to store
987 * @return boolean success or not
990 private function __setValueinDB($column, $value) {
994 $qpa = db_construct_qpa();
995 $qpa = db_construct_qpa($qpa, 'UPDATE doc_data SET ');
996 $qpa = db_construct_qpa($qpa, $column);
997 $qpa = db_construct_qpa($qpa, '=$1
1001 $this->Group->getID(),
1003 $res = db_query_qpa($qpa);
1004 if (!$res || db_affected_rows($res) < 1) {
1005 $this->setOnUpdateError(db_error().print_r($qpa));
1011 $this->setOnUpdateError(_('wrong column name'));
1014 $this->sendNotice(false);
1018 function createVersion() {
1022 function deleteVersion() {
1029 // c-file-style: "bsd"