message_media.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /*
  3. FusionPBX
  4. Version: MPL 1.1
  5. The contents of this file are subject to the Mozilla Public License Version
  6. 1.1 (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.mozilla.org/MPL/
  9. Software distributed under the License is distributed on an "AS IS" basis,
  10. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. for the specific language governing rights and limitations under the
  12. License.
  13. The Original Code is FusionPBX
  14. The Initial Developer of the Original Code is
  15. Mark J Crane <[email protected]>
  16. Portions created by the Initial Developer are Copyright (C) 2024
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. */
  21. //includes files
  22. require_once dirname(__DIR__, 2) . "/resources/require.php";
  23. //get media uuid
  24. $message_media_uuid = $_GET['id'];
  25. $action = $_GET['action'];
  26. if (!empty($_GET['width']) && is_numeric(str_replace('px','',$_GET['width']))) {
  27. $width = str_replace('px','',$_GET['width']);
  28. }
  29. //build a list of groups the user is a member of to be used in a SQL in
  30. if (is_uuid($_SESSION['user_uuid'])) {
  31. foreach($_SESSION['user']['groups'] as $group) {
  32. if (is_uuid($group['group_uuid'])) {
  33. $group_uuids[] = $group['group_uuid'];
  34. }
  35. }
  36. $group_uuids_in = "'".implode("','", $group_uuids)."'";
  37. }
  38. //get media
  39. if (is_uuid($message_media_uuid)) {
  40. //connect to the database
  41. $database = database::new();
  42. //get the media details from the database
  43. $sql = "select mm.message_media_name, mm.message_media_type, mm.message_media_url, mm.message_media_content ";
  44. $sql .= "from v_message_media mm ";
  45. $sql .= "JOIN v_messages m ON (m.message_uuid = mm.message_uuid)";
  46. $sql .= "where mm.message_media_uuid = :message_media_uuid ";
  47. if (is_uuid($_SESSION['user_uuid'])) {
  48. $sql .= "and (mm.user_uuid = :user_uuid or m.group_uuid in (".$group_uuids_in."))";
  49. $parameters['user_uuid'] = $_SESSION['user_uuid'];
  50. }
  51. if (is_uuid($_SESSION['domain_uuid'])) {
  52. $sql .= "and (mm.domain_uuid = :domain_uuid or mm.domain_uuid is null) ";
  53. $parameters['domain_uuid'] = $_SESSION['domain_uuid'];
  54. }
  55. $parameters['message_media_uuid'] = $message_media_uuid;
  56. $media = $database->select($sql, $parameters, 'row');
  57. unset($sql, $parameters);
  58. //set the content type
  59. switch (strtolower($media['message_media_type'])) {
  60. case 'jpg': $content_type = 'image/jpeg'; break;
  61. case 'jpeg': $content_type = 'image/jpeg'; break;
  62. case 'png': $content_type = 'image/png'; break;
  63. case 'gif': $content_type = 'image/gif'; break;
  64. case 'aac': $content_type = 'audio/aac'; break;
  65. case 'wav': $content_type = 'audio/wav'; break;
  66. case 'mp3': $content_type = 'audio/mpeg'; break;
  67. case 'mp2': $content_type = 'video/mpeg'; break;
  68. case 'm4v': $content_type = 'video/mp4'; break;
  69. case 'pdf': $content_type = 'application/pdf'; break;
  70. case 'doc': $content_type = 'application/vnd.ms-word'; break;
  71. case 'docx': $content_type = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'; break;
  72. case 'xls': $content_type = 'application/vnd.ms-excel'; break;
  73. case 'xlsx': $content_type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; break;
  74. case 'ppt': $content_type = 'application/vnd.ms-powerpoint'; break;
  75. case 'pptx': $content_type = 'application/vnd.openxmlformats-officedocument.presentationml.presentation'; break;
  76. case 'zip': $content_type = 'application/zip'; break;
  77. default: $content_type = 'application/octet-stream'; break;
  78. }
  79. //show the image inline when the action is not set
  80. if (empty($action) && ($content_type == 'image/jpeg' || $content_type == 'image/png' || $content_type == 'image/gif')) {
  81. $action = 'download-inline';
  82. }
  83. switch ($action) {
  84. case 'download':
  85. //header("Content-type: ".$content_type."; charset=utf-8");
  86. //header("Content-Disposition: attachment; filename=\"".$filename."\"");
  87. header('Content-Description: File Transfer');
  88. header('Content-Type: application/octet-stream');
  89. header("Content-Disposition: attachment; filename=\"".(strlen($media['message_media_name']) <= 36 ? $media['message_media_name'] : $message_media_uuid.'.'.strtolower($media['message_media_type']))."\"");
  90. header('Content-Transfer-Encoding: binary');
  91. header('Expires: 0');
  92. header('Cache-Control: must-revalidate');
  93. header('Pragma: public');
  94. header("Content-Length: ".strlen(base64_decode($media['message_media_content'])));
  95. echo base64_decode($media['message_media_content']);
  96. break;
  97. case 'download-inline':
  98. //header("Content-type: ".$content_type."; charset=utf-8");
  99. //header("Content-Disposition: attachment; filename=\"".$filename."\"");
  100. header("Content-Type: ".$content_type);
  101. header("Content-Disposition: inline; filename=\"".$media['message_media_name']."\"");
  102. header("Content-Length: ".strlen(base64_decode($media['message_media_content'])));
  103. echo base64_decode($media['message_media_content']);
  104. break;
  105. case 'download-original':
  106. header("Content-type: ".$content_type."; charset=utf-8");
  107. header("Content-Disposition: attachment; filename=\"".$media['message_media_name']."\"");
  108. header("Content-Length: ".strlen(base64_decode($media['message_media_content'])));
  109. echo base64_decode($media['message_media_content']);
  110. break;
  111. case 'thumbnail':
  112. if ($content_type = 'image/jpeg' || $content_type = 'image/png' || $content_type = 'image/gif') {
  113. //get the image size
  114. $image_size = getimagesize('data://application/octet-stream;base64,' . $media['message_media_content']);
  115. $source_width = $image_size[0];
  116. $source_height = $image_size[1];
  117. //read the image
  118. $source_image = imagecreatefromstring(base64_decode($media['message_media_content']));
  119. //working
  120. //header('Content-Type: image/jpeg');
  121. //imagejpeg($source_image);
  122. //get the image width and height
  123. $source_width = imagesx($source_image);
  124. $source_height = imagesy($source_image);
  125. //calculate dimensions for the thumbmail
  126. $destination_width = $width;
  127. $destination_height = floor($source_height * ($destination_width / $source_width));
  128. //send content type http header
  129. header('Content-Type: '.$content_type);
  130. //create the image, resample it and then stream binary
  131. $destination_image = imagecreatetruecolor($destination_width, $destination_height);
  132. //imagealphablending($img_dest, false);
  133. //imagesavealpha($img_dest, true);
  134. if ($destination_image !== false) {
  135. imagecopyresampled($destination_image, $source_image, 0, 0, 0, 0, $destination_width, $destination_height, $source_width, $source_height);
  136. if ($content_type = 'image/jpeg') {
  137. imagejpeg($destination_image);
  138. }
  139. if ($content_type = 'image/png') {
  140. imagejpeg($destination_image);
  141. }
  142. if ($content_type = 'image/gif') {
  143. imagejpeg($destination_image);
  144. }
  145. }
  146. }
  147. break;
  148. case 'display':
  149. echo " <table cellpadding='0' cellspacing='0' border='0' width='100%' height='100%'>\n";
  150. echo " <tr>\n";
  151. echo " <td align='center' valign='middle'>\n";
  152. 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";
  153. echo " </td>\n";
  154. echo " </tr>\n";
  155. echo " </table>\n";
  156. break;
  157. }
  158. }
  159. ?>