Subversion Repositories Applications.papyrus

Rev

Rev 1372 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
831 florian 1
<?php
2
 
3
////////////////////////////////////////////////////////////////////////////////
4
//                                                                            //
5
//   Copyright (C) 2006  Phorum Development Team                              //
6
//   http://www.phorum.org                                                    //
7
//                                                                            //
8
//   This program is free software. You can redistribute it and/or modify     //
9
//   it under the terms of either the current Phorum License (viewable at     //
10
//   phorum.org) or the Phorum License that was distributed with this file    //
11
//                                                                            //
12
//   This program is distributed in the hope that it will be useful,          //
13
//   but WITHOUT ANY WARRANTY, without even the implied warranty of           //
14
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     //
15
//                                                                            //
16
//   You should have received a copy of the Phorum License                    //
17
//   along with this program.                                                 //
18
////////////////////////////////////////////////////////////////////////////////
19
 
20
if(!defined("PHORUM")) return;
21
 
22
if ($do_detach)
23
{
24
    // Find the message to detach.
25
    foreach ($message["attachments"] as $id => $info)
26
    {
27
        if ($info["file_id"] == $do_detach && $info["keep"])
28
        {
29
            // Attachments which are not yet linked to a message
30
            // can be deleted immediately. Linked attachments should
31
            // be kept in the db, in case the users clicks "Cancel".
32
            if (! $info["linked"]) {
33
                phorum_db_file_delete($info["file_id"]);
34
                unset($message["attachments"][$id]);
35
            } else {
36
                $message["attachments"][$id]["keep"] = false;
37
            }
38
 
39
            // Run the after_detach hook.
40
            list($message,$info) =
41
                phorum_hook("after_detach", array($message,$info));
42
 
43
            $attach_count--;
44
 
45
            break;
46
        }
47
    }
48
}
49
 
50
// Attachment(s) uploaded.
51
elseif ($do_attach && ! empty($_FILES))
52
{
53
    // The editor template that I use only supports one upload
54
    // at a time. This code supports multiple uploads.
55
    $attached = 0;
56
    foreach ($_FILES as $file)
57
    {
58
        // Not too many attachments?
59
        if ($attach_count >= $PHORUM["max_attachments"]) break;
60
 
61
        // Check if the tempfile is an uploaded file?
62
        if(! is_uploaded_file($file["tmp_name"])) continue;
63
 
64
        // Some problems in uploading result in files which are
65
        // zero in size. We asume that people who upload zero byte
66
        // files will almost always have problems uploading.
67
        if ($file["size"] == 0) continue;
68
 
69
        // check with PHP and MySQL on attachment size
70
        $php_limit = ini_get('upload_max_filesize')*1024;
71
        $max_packetsize = phorum_db_maxpacketsize();
72
        if ($max_packetsize == NULL) {
73
            $db_limit = $php_limit;
74
        } else {
75
            $db_limit = $max_packetsize/1024*.6;
76
        }
77
        if($PHORUM["max_attachment_size"]==0){
78
            $PHORUM["max_attachment_size"] = min($php_limit, $db_limit);
79
        } else {
80
            $PHORUM["max_attachment_size"] = min($PHORUM["max_attachment_size"], $php_limit, $db_limit);
81
        }
82
 
83
        // Isn't the attachment too large?
84
        if ($PHORUM["max_attachment_size"] > 0 &&
85
            $file["size"] > $PHORUM["max_attachment_size"]*1024) {
86
            $PHORUM["DATA"]["ERROR"] = str_replace(
87
                '%size%',
88
                phorum_filesize($PHORUM["max_attachment_size"] * 1024),
89
                $PHORUM["DATA"]["LANG"]["AttachFileSize"]
90
            );
91
            phorum_filesize($PHORUM["max_attachment_size"] * 1024);
92
            $error_flag = true;
93
            break;
94
        }
95
 
96
        // Isn't the total attachment size too large?
97
        if ($PHORUM["max_totalattachment_size"] > 0 &&
98
            ($file["size"] + $attach_totalsize) > $PHORUM["max_totalattachment_size"]*1024) {
99
            $PHORUM["DATA"]["ERROR"] = str_replace(
100
                '%size%',
101
                phorum_filesize($PHORUM["max_totalattachment_size"] * 1024),
102
                $PHORUM["DATA"]["LANG"]["AttachTotalFileSize"]
103
            );
104
            $error_flag = true;
105
            break;
106
        }
107
 
108
        // Is the type of file acceptable?
109
        if(! empty($PHORUM["allow_attachment_types"]))
110
        {
111
            $ext=substr($file["name"], strrpos($file["name"], ".")+1);
112
            $allowed_exts=explode(";", $PHORUM["allow_attachment_types"]);
113
            if (! in_array(strtolower($ext), $allowed_exts)) {
114
                $PHORUM["DATA"]["ERROR"] =
115
                    $PHORUM["DATA"]["LANG"]["AttachInvalidType"] . " ".
116
                    str_replace('%types%', str_replace(";", ", ", $PHORUM["allow_attachment_types"]), $PHORUM["DATA"]["LANG"]["AttachFileTypes"]);
117
                $error_flag = true;
118
                break;
119
            }
120
        }
121
 
122
        // Read in the file.
123
        $fp = fopen($file["tmp_name"], "r");
124
        if (! $fp) continue;
125
        $file["data"] = base64_encode(fread($fp, $file["size"]));
126
        fclose($fp);
127
 
128
        // copy the current user_id to the $file array for the hook
129
        $file["user_id"]=$PHORUM["user"]["user_id"];
130
 
131
        // Run the before_attach hook.
132
        list($message, $file) =
133
            phorum_hook("before_attach", array($message, $file));
134
 
135
        // Add the file to the database. We add it using message_id
136
        // 0 (zero). Only when the message gets saved definitely,
137
        // the message_id will be updated to link the file to the
138
        // forum message. This is mainly done so we can support
139
        // attachments for new messages, which do not yet have
140
        // a message_id assigned.
141
        $file_id = phorum_db_file_save(
142
            $PHORUM["user"]["user_id"],
143
            $file["name"], $file["size"],
144
            $file["data"], 0, PHORUM_LINK_EDITOR
145
        );
146
 
147
        // Create new attachment information.
148
        $new_attachment = array(
149
            "file_id" => $file_id,
150
            "name"    => $file["name"],
151
            "size"    => $file["size"],
152
            "keep"    => true,
153
            "linked"  => false,
154
        );
155
 
156
        // Run the after_attach hook.
157
        list($message, $new_attachment) =
158
            phorum_hook("after_attach", array($message, $new_attachment));
159
 
160
        // Add the attachment to the message.
161
        $message['attachments'][] = $new_attachment;
162
        $attach_totalsize += $new_attachment["size"];
163
        $attach_count++;
164
        $attached++;
165
    }
166
 
167
    // Show a generic error message if nothing was attached and
168
    // no specific message was set.
169
    if (! $error_flag && ! $attached) {
170
        $PHORUM["DATA"]["ERROR"] =
171
            $PHORUM["DATA"]["LANG"]["AttachmentsMissing"];
172
        $error_flag = true;
173
    }
174
 
175
    // Show a success message in case an attachment is added.
176
    if (! $error_flag && $attached) {
177
        $PHORUM["DATA"]["OKMSG"] = $PHORUM["DATA"]["LANG"]["AttachmentAdded"];
178
 
179
    }
180
}
181
?>