-
Notifications
You must be signed in to change notification settings - Fork 7
/
message_media.php
184 lines (159 loc) · 7.62 KB
/
message_media.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
<?php
/*
FusionPBX
Version: MPL 1.1
The contents of this file are subject to the Mozilla Public License Version
1.1 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
for the specific language governing rights and limitations under the
License.
The Original Code is FusionPBX
The Initial Developer of the Original Code is
Mark J Crane <[email protected]>
Portions created by the Initial Developer are Copyright (C) 2024
the Initial Developer. All Rights Reserved.
Contributor(s):
Mark J Crane <[email protected]>
*/
//includes files
require_once dirname(__DIR__, 2) . "/resources/require.php";
//get media uuid
$message_media_uuid = $_GET['id'];
$action = $_GET['action'];
if (!empty($_GET['width']) && is_numeric(str_replace('px','',$_GET['width']))) {
$width = str_replace('px','',$_GET['width']);
}
//build a list of groups the user is a member of to be used in a SQL in
if (is_uuid($_SESSION['user_uuid'])) {
foreach($_SESSION['user']['groups'] as $group) {
if (is_uuid($group['group_uuid'])) {
$group_uuids[] = $group['group_uuid'];
}
}
$group_uuids_in = "'".implode("','", $group_uuids)."'";
}
//get media
if (is_uuid($message_media_uuid)) {
//connect to the database
$database = database::new();
//get the media details from the database
$sql = "select mm.message_media_name, mm.message_media_type, mm.message_media_url, mm.message_media_content ";
$sql .= "from v_message_media mm ";
$sql .= "JOIN v_messages m ON (m.message_uuid = mm.message_uuid)";
$sql .= "where mm.message_media_uuid = :message_media_uuid ";
if (is_uuid($_SESSION['user_uuid'])) {
$sql .= "and (mm.user_uuid = :user_uuid or m.group_uuid in (".$group_uuids_in."))";
$parameters['user_uuid'] = $_SESSION['user_uuid'];
}
if (is_uuid($_SESSION['domain_uuid'])) {
$sql .= "and (mm.domain_uuid = :domain_uuid or mm.domain_uuid is null) ";
$parameters['domain_uuid'] = $_SESSION['domain_uuid'];
}
$parameters['message_media_uuid'] = $message_media_uuid;
$media = $database->select($sql, $parameters, 'row');
unset($sql, $parameters);
//set the content type
switch (strtolower($media['message_media_type'])) {
case 'jpg': $content_type = 'image/jpeg'; break;
case 'jpeg': $content_type = 'image/jpeg'; break;
case 'png': $content_type = 'image/png'; break;
case 'gif': $content_type = 'image/gif'; break;
case 'aac': $content_type = 'audio/aac'; break;
case 'wav': $content_type = 'audio/wav'; break;
case 'mp3': $content_type = 'audio/mpeg'; break;
case 'mp2': $content_type = 'video/mpeg'; break;
case 'm4v': $content_type = 'video/mp4'; break;
case 'pdf': $content_type = 'application/pdf'; break;
case 'doc': $content_type = 'application/vnd.ms-word'; break;
case 'docx': $content_type = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'; break;
case 'xls': $content_type = 'application/vnd.ms-excel'; break;
case 'xlsx': $content_type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; break;
case 'ppt': $content_type = 'application/vnd.ms-powerpoint'; break;
case 'pptx': $content_type = 'application/vnd.openxmlformats-officedocument.presentationml.presentation'; break;
case 'zip': $content_type = 'application/zip'; break;
default: $content_type = 'application/octet-stream'; break;
}
//show the image inline when the action is not set
if (empty($action) && ($content_type == 'image/jpeg' || $content_type == 'image/png' || $content_type == 'image/gif')) {
$action = 'download-inline';
}
switch ($action) {
case 'download':
//header("Content-type: ".$content_type."; charset=utf-8");
//header("Content-Disposition: attachment; filename=\"".$filename."\"");
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header("Content-Disposition: attachment; filename=\"".(strlen($media['message_media_name']) <= 36 ? $media['message_media_name'] : $message_media_uuid.'.'.strtolower($media['message_media_type']))."\"");
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header("Content-Length: ".strlen(base64_decode($media['message_media_content'])));
echo base64_decode($media['message_media_content']);
break;
case 'download-inline':
//header("Content-type: ".$content_type."; charset=utf-8");
//header("Content-Disposition: attachment; filename=\"".$filename."\"");
header("Content-Type: ".$content_type);
header("Content-Disposition: inline; filename=\"".$media['message_media_name']."\"");
header("Content-Length: ".strlen(base64_decode($media['message_media_content'])));
echo base64_decode($media['message_media_content']);
break;
case 'download-original':
header("Content-type: ".$content_type."; charset=utf-8");
header("Content-Disposition: attachment; filename=\"".$media['message_media_name']."\"");
header("Content-Length: ".strlen(base64_decode($media['message_media_content'])));
echo base64_decode($media['message_media_content']);
break;
case 'thumbnail':
if ($content_type = 'image/jpeg' || $content_type = 'image/png' || $content_type = 'image/gif') {
//get the image size
$image_size = getimagesize('data://application/octet-stream;base64,' . $media['message_media_content']);
$source_width = $image_size[0];
$source_height = $image_size[1];
//read the image
$source_image = imagecreatefromstring(base64_decode($media['message_media_content']));
//working
//header('Content-Type: image/jpeg');
//imagejpeg($source_image);
//get the image width and height
$source_width = imagesx($source_image);
$source_height = imagesy($source_image);
//calculate dimensions for the thumbmail
$destination_width = $width;
$destination_height = floor($source_height * ($destination_width / $source_width));
//send content type http header
header('Content-Type: '.$content_type);
//create the image, resample it and then stream binary
$destination_image = imagecreatetruecolor($destination_width, $destination_height);
//imagealphablending($img_dest, false);
//imagesavealpha($img_dest, true);
if ($destination_image !== false) {
imagecopyresampled($destination_image, $source_image, 0, 0, 0, 0, $destination_width, $destination_height, $source_width, $source_height);
if ($content_type = 'image/jpeg') {
imagejpeg($destination_image);
}
if ($content_type = 'image/png') {
imagejpeg($destination_image);
}
if ($content_type = 'image/gif') {
imagejpeg($destination_image);
}
}
}
break;
case 'display':
echo " <table cellpadding='0' cellspacing='0' border='0' width='100%' height='100%'>\n";
echo " <tr>\n";
echo " <td align='center' valign='middle'>\n";
echo " <img src=\"data:".$content_type.";base64,".$media['message_media_content']."\" style='width: auto; max-width: 95%; height: auto; max-height: 800px; box-shadow: 0px 1px 20px #888; cursor: pointer;' onclick=\"$('#message_media_layer').fadeOut(200);\" oncontextmenu=\"window.open('message_media.php?id=".$message_media_uuid."&src=".$message_media_source."&action=download'); return false;\" title=\"Click to Close, Right-Click to Save\">\n";
echo " </td>\n";
echo " </tr>\n";
echo " </table>\n";
break;
}
}
?>