4 * GForge Attachment manager class
6 * Copyright 1999-2001 (c) VA Linux Systems
7 * The rest Copyright 2002-2005 (c) GForge Team
11 * This file is part of GForge.
13 * GForge is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * GForge is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with GForge; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 by Daniel Perez - 2005
32 require_once('pre.php');
34 class AttachManager extends Error {
36 var $attachs = array(); //the attached files
37 var $msg_id; //the msg_id that links to the attachs
38 var $user_id,$dateline,$filename,$filedata,$filesize,$visible,$filehash,$posthash;
39 var $messages = array();
40 var $ForumMsg; // The Forum object
42 function Setmsgid($id) {
46 function Getmessages() {
47 return $this->messages;
50 function fillvalues($user_id,$dateline,$filename,$filedata,$filesize,$visible,$filehash,$posthash) {
51 $this->user_id = $user_id;
52 $this->dateline = $dateline;
53 $this->filename = $filename;
54 $this->filedata = $filedata;
55 $this->visible = $visible;
56 $this->filehash = $filehash;
57 $this->posthash = $posthash;
61 * Function SetForumMsg
63 * Sets the forum message associated with the attachment
65 function SetForumMsg(&$ForumMsg) {
66 $this->ForumMsg =& $ForumMsg;
70 * Function GetAttachId
72 * Returns the attach id for the message id passed as a parameter or false if error
74 function GetAttachId($msg_id) {
75 $sql = "SELECT attachmentid FROM forum_attachment WHERE msg_id='$msg_id'";
76 $res = db_query($sql);
78 return db_result($res,0,0);
85 * Function PrintHelperFunctions
88 * @return returns the javascript helper functions
91 function PrintHelperFunctions() {
92 return '<script type="text/javascript">
94 function confirmDel() {
95 var agree=confirm("Proceed with deletion? ");
96 if (agree) return true ;
100 function manageattachments(url,del) {
106 newwindow = window.open(url, \'Attach\', \'statusbar=no,menubar=no,toolbar=no,scrollbars=yes,resizable=yes,width=600,height=480\');
107 if (window.focus) {newwindow.focus()}
113 * Function PrintAttachLink
115 * @param object The message.
116 * @param int The group id.
117 * @param int The forum id.
119 * @return returns link to attachment /delete if corresponding; else returns a message about no attachment found
121 function PrintAttachLink(&$msg,$group_id,$forum_id) {
123 //ask if the message has an attachment
124 $msg_id = $msg->getID();
125 if ($msg->isPending()) {
126 $sql = "SELECT attachmentid,filename,userid,counter FROM forum_pending_attachment where msg_id='$msg_id'";
127 $pend = "&pending=yes";
129 $sql = "SELECT attachmentid,filename,userid,counter FROM forum_attachment where msg_id='$msg_id'";
133 $res = db_query($sql);
135 $attachid = db_result($res,0,'attachmentid');
139 <a href=\"javascript:manageattachments('/forum/attachment.php?attachid=$attachid&group_id=$group_id&forum_id=$forum_id$pend','no');\">" . html_image('ic/cfolder15.png',"15","13",array("border"=>"0")) . 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) || ($f->userIsAdmin())) && (!$msg->isPending()) ) { //only permit the user who created the attach to delete it, or an admin
147 $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>";
148 $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>";
152 //add attach for existing message
153 $f = $msg->getForum();
154 if (!$f || !is_object($f)) {
155 exit_error('Error','Could Not Get Forum Object');
157 $attach = html_image('ic/cfolder15.png',"15","13",array("border"=>"0")) . "No attachment found";
158 if ( ((user_getid() == $msg->getPosterID()) || ($f->userIsAdmin())) && (!$msg->isPending()) ) { //only permit an admin or the user who created the message to insert an attach
159 $attach .= " <a href=\"javascript:manageattachments('".$GLOBALS['sys_urlprefix']."/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>";
168 * Function AddToDBOnly : DB Query Only - used for releasing pending messages
172 function AddToDBOnly($userid, $dateline, $filename, $filedata, $filesize, $visible, $filehash, $mimetype) {
175 $result=db_query("SELECT max(msg_id) AS id FROM forum");
176 if (!$result || db_numrows($result) < 1) {
177 $this->messages[] = $Language->getText('forum_attachmngr','err_msgid');
179 $this->msg_id = db_result($result,0,0);
180 $sql = "INSERT INTO forum_attachment (userid, dateline, filename, filedata, filesize, visible, msg_id , filehash, mimetype)
182 ( $userid , " . $dateline . ", '" . $filename . "',
183 '" . $filedata . "', $filesize, $visible, $this->msg_id, '" . $filehash . "', '" . $mimetype . "')";
184 if (db_query($sql)) {
185 $this->messages[] = $Language->getText('forum_attachmngr','uploadok');
187 $this->messages[] = $Language->getText('forum_attachmngr','upload_notok');
196 * Function attach : saves the file in the DB
198 * @param int The file to attach
199 * @param int The group.
200 * @param int Whether we are updating an existing attach (attachid to update or cero for new message (inserts using the hights msg id from forum table)
201 * @param int msg id. if update is 0 and we pass a msg_id <> 0, then we are adding an attach for an existing msg
203 * @return int Attach id on success, false otherwise
206 function attach($attach,$group_id,$update=0,$msg_id=0) {
210 $attachment = trim($attach['tmp_name']);
211 $attachment_name = trim($attach['name']);
212 $attachment_size = trim($attach['size']);
213 $attachment_type = trim($attach['type']);
215 if ($attachment == 'none' OR empty($attachment) OR empty($attachment_name))
217 $this->messages[] = $Language->getText('forum_attachmngr','errnofile');
218 return false; //no point in continuing if thereĀ“s no file
221 $attachment_name2 = strtolower($attachment_name);
222 $extension = substr(strrchr($attachment_name2, '.'), 1);
224 if ($extension == 'exe')
227 $this->messages[] = $Language->getText('forum_attachmngr','err_inv_ext');
229 @unlink($attachment);
233 if (!is_uploaded_file($attachment) || !($filestuff = @file_get_contents($attachment)) )
235 $this->messages[] = $Language->getText('forum_attachmngr','err_upload');
239 if (!session_loggedin()) {
242 $user_id = user_getid();
247 if ($this->ForumMsg->isPending()) {
249 //update the fileinfo
250 /*$sql = "UPDATE forum_pending_attachment SET dateline = '" . time() . "' , filedata = '" . base64_encode($filestuff) . "' , filename = '" . addslashes($attachment_name) . "' , filehash = '" . addslashes(md5($filestuff)) . "' where attachmentid=$update";
251 if (db_query($sql)) {
252 $this->messages[] = $Language->getText('forum_attachmngr','uploadok');
254 $this->messages[] = $Language->getText('forum_attachmngr','upload_notok');
260 $this->msg_id = $msg_id;
262 $result=db_query("SELECT max(msg_id) AS id FROM forum_pending_messages");
263 if (!$result || db_numrows($result) < 1) {
264 $this->messages[] = $Language->getText('forum_attachmngr','err_msgid');
265 @unlink($attachment);
268 $this->msg_id = db_result($result,0,0);
271 $sql = "INSERT INTO forum_pending_attachment (userid, dateline, filename, filedata, filesize, visible, msg_id , filehash, mimetype)
273 ( $user_id , " . time() . ", '" . addslashes($attachment_name) . "',
274 '" . base64_encode($filestuff) . "', $attachment_size, 1, $this->msg_id, '" . addslashes(md5($filestuff)) . "', '". addslashes($attachment_type) ."')";
275 $res = db_query($sql);
277 $this->messages[] = $Language->getText('forum_attachmngr','uploadok');
278 $id = db_insertid($res,'forum_pending_attachment','attachmentid');
280 $this->messages[] = $Language->getText('forum_attachmngr','upload_notok');
285 //update the fileinfo
286 $sql = "UPDATE forum_attachment SET dateline = '" . time() . "' , filedata = '" . base64_encode($filestuff) . "' ,
287 filename = '" . addslashes($attachment_name) . "' ,
288 filehash = '" . addslashes(md5($filestuff)) . "' ,
289 mimetype = '" . addslashes($attachment_type) . "' ,
291 filesize = '" . $attachment_size . "' where attachmentid=$update";
292 if (db_query($sql)) {
293 $this->messages[] = $Language->getText('forum_attachmngr','uploadok');
294 $this->messages[] = $Language->getText('forum_attachmngr','updateok');
297 $this->messages[] = $Language->getText('forum_attachmngr','upload_notok');
302 $this->msg_id = $msg_id;
304 $result=db_query("SELECT max(msg_id) AS id FROM forum_pending_messages");
305 if (!$result || db_numrows($result) < 1) {
306 $this->messages[] = $Language->getText('forum_attachmngr','err_msgid');
307 @unlink($attachment);
310 $this->msg_id = db_result($result,0,0);
313 $sql = "INSERT INTO forum_attachment (userid, dateline, filename, filedata, filesize, visible, msg_id , filehash, mimetype)
315 ( $user_id , " . time() . ", '" . addslashes($attachment_name) . "',
316 '" . base64_encode($filestuff) . "', $attachment_size, 1, $this->msg_id, '" . addslashes(md5($filestuff)) . "', '" . addslashes($attachment_type) . "')";
317 $res = db_query($sql);
319 $this->messages[] = $Language->getText('forum_attachmngr','uploadok');
320 $id = db_insertid($res,'forum_attachment','attachmentid');
322 $this->messages[] = $Language->getText('forum_attachmngr','upload_notok');
326 @unlink($attachment);