forked from heyman/mms-decoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
231 lines (191 loc) · 5.99 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
/**
* Copyright (C) 2004 Jonatan Heyman
*
* This file is part of MMS Decoder Example Application.
* A collection of functions used by the apllication.
*
* MMS Decoder is free software; you can redistribute it and/or
* modify it under the terms of the Affero General Public License as
* published by Affero, Inc.; either version 1 of the License, or
* (at your option) any later version.
*
* MMS Decoder is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Affero General Public License for more details.
*
* You should have received a copy of the Affero General Public
* License in the COPYING file that comes with The Affero Project; if
* not, write to Affero, Inc., 510 Third Street, Suite 225, San
* Francisco, CA 94107 USA.
*/
/**
* It is safe to assume that the config.php file has already been included here
*/
// Save an MMS and its parts to the database
// Takes only a reference variable to a MMSDecode object as argument
function mms_save(&$mms) {
// save MMS in database
$sql =
INSERT INTO " . TABLE_MMS . "
(
`from`,
`to`,
`subject`,
`content_type`
) VALUES (
'" . mysql_escape_string($mms->FROM) . "',
'" . mysql_escape_string($mms->TO) . "',
'" . mysql_escape_string($mms->SUBJECT) . "',
'" . mysql_escape_string($mms->CONTENTTYPE) . "'
)
";
//echo $sql;
if (!mysql_query($sql))
log_error('Database error: Could not insert MMS data into database. MySQL: ' . mysql_error());
$mmsid = mysql_insert_id();
// loop thru parts and save them in database
foreach ($mms->PARTS as $part) {
$result = mysql_query("INSERT INTO " . TABLE_PARTS . " (mmsid, datalen, content_type, data) VALUES ('$mmsid', '" . $part->DATALEN . "', '" . $part->CONTENTTYPE . "', '" . mysql_escape_string($part->DATA) . "')");
if (!$result)
log_error('Database error: Could not insert part data into database. MySQL: ' . mysql_error());
}
}
function mms_list($ret = 0) {
$sql =
SELECT
" . TABLE_MMS . ".from,
" . TABLE_MMS . ".to,
" . TABLE_MMS . ".subject,
" . TABLE_MMS . ".content_type as mms_content_type,
" . TABLE_PARTS . ".id,
" . TABLE_PARTS . ".mmsid,
" . TABLE_PARTS . ".datalen,
" . TABLE_PARTS . ".content_type
FROM
" . TABLE_MMS . ", " . TABLE_PARTS .
WHERE
" . TABLE_PARTS . ".mmsid = " . TABLE_MMS . ".id
ORDER BY
" . TABLE_PARTS . ".mmsid DESC
";
$result = mysql_query($sql);
$lastid = -1;
$html = '<table border="1">';
while ($rsdata = mysql_fetch_assoc($result)) {
if ($lastid != $rsdata['mmsid']) {
$html .=
<tr><td>
Subject: ' . $rsdata['subject'] . '<br>
From: ' . $rsdata['from'] . '<br>
To: ' . $rsdata['to'] . '<br>
Content-type: ' . $rsdata['mms_content_type'] .
<br><br>
';
}
$html .= '<a href="dload.php?id=' . $rsdata['id'] . '">' . $rsdata['content_type'] . '</a> ' . $rsdata['datalen'] . ' bytes<br>';
switch ($rsdata['content_type']) {
case "image/gif":
case "image/jpeg":
case "image/png":
case "image/tiff":
$html .= '<img src="dload.php?id=' . $rsdata['id'] . '"><br><br>';
break;
}
$lastid = $rsdata['mmsid'];
if ($lastid != $rsdata['mmsid'])
$html .= '</td></tr>';
}
$html .= '</td></tr></table>';
if (!$ret)
echo $html;
else
return $html;
}
/*
// Get the MMS and their parts from the database and genereate some HTML
function mms_list($ret = 0) {
$sql = "
SELECT
" . TABLE_MMS . ".from,
" . TABLE_MMS . ".to,
" . TABLE_MMS . ".subject,
" . TABLE_MMS . ".content_type as mms_content_type,
" . TABLE_PARTS . ".id,
" . TABLE_PARTS . ".mmsid,
" . TABLE_PARTS . ".datalen,
" . TABLE_PARTS . ".content_type
FROM
" . TABLE_MMS . ", " . TABLE_PARTS . "
WHERE
" . TABLE_PARTS . ".mmsid = " . TABLE_MMS . ".id
ORDER BY
" . TABLE_PARTS . ".mmsid DESC
";
$result = mysql_query($sql);
$lastid = -1;
$html = '<table border="1">';
while ($rsdata = mysql_fetch_assoc($result)) {
if ($lastid != $rsdata[mmsid]) {
$html .= '
<tr><td>
Subject: ' . $rsdata[subject] . '<br>
From: ' . $rsdata[from] . '<br>
To: ' . $rsdata[to] . '<br>
Content-type: ' . $rsdata[mms_content_type] . '
<br><br>
';
}
$html .= '<a href="dload.php?id=' . $rsdata[id] . '">' . $rsdata[content_type] . '</a> ' . $rsdata[datalen] . ' bytes<br>';
// check if part-content-type is png, jpeg, tiff or gif.. then output picture
if ($rsdata[content_type] == 'image/gif' || $rsdata[content_type] == 'image/jpeg' || $rsdata[content_type] == 'image/png' || $rsdata[content_type] == 'image/tiff')
$html .= '<img src="dload.php?id=' . $rsdata[id] . '"><br><br>';
$lastid = $rsdata[mmsid];
if ($lastid != $rsdata[mmsid])
$html .= '</td></tr>';
}
$html .= '</td></tr></table>';
if (!$ret)
echo $html;
else
return $html;
}
*/
function mms_part_dload($id) {
$result = mysql_query("SELECT content_type, data FROM " . TABLE_PARTS . " WHERE id='$id'");
if (!$result)
echo 'Part not found!';
else {
$rsdata = mysql_fetch_assoc($result);
header('Content-type: ' . $rsdata['content_type']);
header('Content-Disposition: inline; filename="part"');
echo $rsdata['data'];
}
}
// log stuff so it doesn't need to be written out
function log_error($str) {
echo $str . '<br>';
$f = fopen("error.log", "w");
fwrite($f, $str . "\n");
fclose($f);
}
// connect to database
function db_connect() {
// connect
if (!mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS)) {
log('Could not connect to database!');
return 0;
}
// select database
if (!mysql_select_db(MYSQL_DB)) {
log('Database ' . MYSQL_DB . ' not found!');
return 0;
}
return 1;
}
// close database link
function db_close() {
mysql_close();
}
?>