4 * FusionForge Attachment manager class
6 * Copyright 1999-2001 (c) VA Linux Systems
7 * The rest Copyright 2002-2005 (c) GForge Team
8 * Copyright (C) 2010-2011 Alain Peyrat - Alcatel-Lucent
9 * http://fusionforge.org/
11 * This file is part of FusionForge. FusionForge is free software;
12 * you can redistribute it and/or modify it under the terms of the
13 * GNU General Public License as published by the Free Software
14 * Foundation; either version 2 of the Licence, or (at your option)
17 * FusionForge is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 by Daniel Perez - 2005
31 class AttachManager extends Error {
33 var $attachs = array(); //the attached files
34 var $msg_id; //the msg_id that links to the attachs
35 var $user_id,$dateline,$filename,$filedata,$filesize,$visible,$filehash,$posthash;
36 var $messages = array();
37 var $ForumMsg; // The Forum object
39 function Setmsgid($id) {
43 function Getmessages() {
44 return $this->messages;
47 function fillvalues($user_id,$dateline,$filename,$filedata,$filesize,$visible,$filehash,$posthash) {
48 $this->user_id = $user_id;
49 $this->dateline = $dateline;
50 $this->filename = $filename;
51 $this->filedata = $filedata;
52 $this->visible = $visible;
53 $this->filehash = $filehash;
54 $this->posthash = $posthash;
58 * Function SetForumMsg
60 * Sets the forum message associated with the attachment
62 function SetForumMsg(&$ForumMsg) {
63 $this->ForumMsg =& $ForumMsg;
67 * Function GetAttachId
69 * Returns the attach id for the message id passed as a parameter or false if error
71 function GetAttachId($msg_id) {
72 $res = db_query_params ('SELECT attachmentid FROM forum_attachment WHERE msg_id=$1',
75 return db_result($res,0,0);
82 * Function PrintHelperFunctions
85 * @return returns the javascript helper functions
88 function PrintHelperFunctions() {
89 return '<script type="text/javascript">/* <![CDATA[ */
91 function confirmDel() {
92 var agree=confirm("Proceed with deletion? ");
93 if (agree) return true ;
97 function manageattachments(url,del) {
103 newwindow = window.open(url, \'Attach\', \'statusbar=no,menubar=no,toolbar=no,scrollbars=yes,resizable=yes,width=600,height=480\');
104 if (window.focus) {newwindow.focus()}
110 * Function PrintAttachLink
112 * @param object $msg The message.
113 * @param int $group_id The group id.
114 * @param int $forum_id The forum id.
116 * @return returns link to attachment /delete if corresponding; else returns a message about no attachment found
118 function PrintAttachLink(&$msg,$group_id,$forum_id) {
120 //ask if the message has an attachment
121 $msg_id = $msg->getID();
122 if ($msg->isPending()) {
123 $res = db_query_params ('SELECT attachmentid,filename,userid,counter FROM forum_pending_attachment where msg_id=$1',
125 $pend = "&pending=yes";
127 $res = db_query_params ('SELECT attachmentid,filename,userid,counter FROM forum_attachment where msg_id=$1',
134 if ($res && db_numrows($res)) {
135 $attachid = db_result($res,0,'attachmentid');
139 <a href=\"javascript:manageattachments('".util_make_url("/forum/attachment.php?attachid=$attachid&group_id=$group_id&forum_id=$forum_id$pend")."','no');\">" . html_image('ic/cfolder15.png',"15","13") . db_result($res,0,'filename') . "</a> (" . db_result($res,0,'counter') . ") downloads";
140 $attach_userid = db_result($res,0,'userid');
142 $f = $msg->getForum();
143 if (!$f || !is_object($f)) {
144 exit_error('Error', _('Could Not Get Forum Object'));
146 if ( ((user_getid() == $attach_userid)
147 || (forge_check_perm ('forum_admin', $f->Group->getID())))
148 && (!$msg->isPending()) ) { //only permit the user who created the attach to delete it, or an admin
149 $attach .= " <a href=\"javascript:manageattachments('/forum/attachment.php?attachid=$attachid&group_id=$group_id&forum_id=$forum_id&msg_id=$msg_id&edit=yes','no');\">" . "<font size=\"-3\">" . html_image('ic/forum_edit.gif','37','15',array('alt'=>_("Edit"))) . "</font></a>";
150 $attach .= " <a href=\"javascript:manageattachments('/forum/attachment.php?attachid=$attachid&group_id=$group_id&forum_id=$forum_id&delete=yes','yes');\">" . "<font size=\"-3\">" . html_image('ic/forum_delete.gif','16','18',array('alt'=>_("Delete"))) . "</font></a>";
154 //add attach for existing message
155 $f = $msg->getForum();
156 if (!$f || !is_object($f)) {
157 exit_error('Error','Could Not Get Forum Object');
159 // $attach = html_image('ic/cfolder15.png',"15","13") . _('No attachment found');
161 if ( ((user_getid() == $msg->getPosterID())
162 || (forge_check_perm ('forum_admin', $f->Group->getID())))
163 && (!$msg->isPending()) ) { //only permit the user who created the message to insert an attach
164 $attach .= " <a href=\"javascript:manageattachments('".util_make_url ("/forum/attachment.php?attachid=0&group_id=$group_id&forum_id=$forum_id&msg_id=$msg_id&edit=yes")."','no');\">" . "<font size=\"-3\">" . html_image('ic/forum_add.gif','37','15',array('alt'=>_("Add"))) . "</font></a>";
173 * Function AddToDBOnly : DB Query Only - used for releasing pending messages
177 function AddToDBOnly($userid, $dateline, $filename, $filedata, $filesize, $visible, $filehash, $mimetype) {
178 $result=db_query_params ('SELECT max(msg_id) AS id FROM forum',
180 if (!$result || db_numrows($result) < 1) {
181 $this->messages[] = _('Could not get message id');
183 $this->msg_id = db_result($result,0,0);
184 if (db_query_params ('INSERT INTO forum_attachment (userid, dateline, filename, filedata, filesize, visible, msg_id , filehash, mimetype)
187 $4, $5, $6, $7, $8, $9)',
197 $this->messages[] = _('File uploaded');
199 $this->messages[] = _('File not uploaded');
206 * Function attach : saves the file in the DB
208 * @param int $attach The file to attach
209 * @param int $group_id The group.
210 * @param int $update Whether we are updating an existing attach (attachid to update or zero for new message (inserts using the hights msg id from forum table)
211 * @param int $msg_id msg id. if update is 0 and we pass a msg_id <> 0, then we are adding an attach for an existing msg
213 * @return int Attach id on success, false otherwise
216 function attach($attach,$group_id,$update=0,$msg_id=0) {
219 $attachment = trim($attach['tmp_name']);
220 $attachment_name = trim($attach['name']);
221 $attachment_size = trim($attach['size']);
222 $attachment_type = trim($attach['type']);
224 if ($attachment == 'none' OR empty($attachment) OR empty($attachment_name))
226 return false; //no point in continuing if there's no file
229 $attachment_name2 = strtolower($attachment_name);
230 $extension = substr(strrchr($attachment_name2, '.'), 1);
232 if ($extension == 'exe')
235 $this->messages[] = _('Invalid Extension');
237 @unlink($attachment);
241 if (!is_uploaded_file($attachment) || !($filestuff = @file_get_contents($attachment)) )
243 $this->messages[] = _('Error, problem with the attachment file uploaded into the server');
247 if (!session_loggedin()) {
250 $user_id = user_getid();
255 if ($this->ForumMsg->isPending()) {
257 //update the fileinfo
262 $this->msg_id = $msg_id;
264 $result=db_query_params ('SELECT max(msg_id) AS id FROM forum_pending_messages',
266 if (!$result || db_numrows($result) < 1) {
267 $this->messages[] = _('Could not get message id');
268 @unlink($attachment);
271 $this->msg_id = db_result($result,0,0);
274 $res = db_query_params ('INSERT INTO forum_pending_attachment (userid, dateline, filename, filedata, filesize, visible, msg_id , filehash, mimetype)
277 $4, $5, 1, $6, $7, $8)',
281 base64_encode($filestuff) ,
287 $this->messages[] = _('File uploaded');
288 $id = db_insertid($res,'forum_pending_attachment','attachmentid');
290 $this->messages[] = _('File not uploaded');
295 //update the fileinfo
296 if (db_query_params ('UPDATE forum_attachment SET dateline = $1 , filedata = $2 ,
301 filesize = $6 where attachmentid=$7',
303 base64_encode($filestuff) ,
309 $this->messages[] = _('File uploaded');
310 $this->messages[] = _('File Updated Successfully');
313 $this->messages[] = _('File not uploaded');
318 $this->msg_id = $msg_id;
320 $result=db_query_params ('SELECT max(msg_id) AS id FROM forum_pending_messages',
322 if (!$result || db_numrows($result) < 1) {
323 $this->messages[] = _('Could not get message id');
324 @unlink($attachment);
327 $this->msg_id = db_result($result,0,0);
330 $res = db_query_params ('INSERT INTO forum_attachment (userid, dateline, filename, filedata, filesize, visible, msg_id , filehash, mimetype)
333 $4, $5, 1, $6, $7, $8)',
337 base64_encode($filestuff) ,
343 $this->messages[] = _('File uploaded');
344 $id = db_insertid($res,'forum_attachment','attachmentid');
346 $this->messages[] = _('File not uploaded');
350 @unlink($attachment);
357 // c-file-style: "bsd"