message_media.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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) 2023
  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. foreach($_SESSION['user']['groups'] as $group) {
  31. if (is_uuid($group['group_uuid'])) {
  32. $group_uuids[] = $group['group_uuid'];
  33. }
  34. }
  35. $group_uuids_in = "'".implode("','", $group_uuids)."'";
  36. //get media
  37. if (is_uuid($message_media_uuid)) {
  38. //get the media details from the database
  39. $sql = "select v_message_media.message_media_name, v_message_media.message_media_type, v_message_media.message_media_url, v_message_media.message_media_content ";
  40. $sql .= "from v_message_media ";
  41. $sql .= "JOIN v_messages ON (v_messages.message_uuid = v_message_media.message_uuid)";
  42. $sql .= "where v_message_media.message_media_uuid = :message_media_uuid ";
  43. if (is_uuid($_SESSION['user_uuid'])) {
  44. $sql .= "and (v_message_media.user_uuid = :user_uuid or v_messages.group_uuid in (".$group_uuids_in."))";
  45. $parameters['user_uuid'] = $_SESSION['user_uuid'];
  46. }
  47. if (is_uuid($_SESSION['domain_uuid'])) {
  48. $sql .= "and (v_message_media.domain_uuid = :domain_uuid or v_message_media.domain_uuid is null) ";
  49. $parameters['domain_uuid'] = $_SESSION['domain_uuid'];
  50. }
  51. $parameters['message_media_uuid'] = $message_media_uuid;
  52. $database = new database;
  53. $media = $database->select($sql, $parameters, 'row');
  54. unset($sql, $parameters);
  55. //set the content type
  56. switch (strtolower($media['message_media_type'])) {
  57. case 'jpg': $content_type = 'image/jpeg'; break;
  58. case 'jpeg': $content_type = 'image/jpeg'; break;
  59. case 'png': $content_type = 'image/png'; break;
  60. case 'gif': $content_type = 'image/gif'; break;
  61. case 'aac': $content_type = 'audio/aac'; break;
  62. case 'wav': $content_type = 'audio/wav'; break;
  63. case 'mp3': $content_type = 'audio/mpeg'; break;
  64. case 'mp2': $content_type = 'video/mpeg'; break;
  65. case 'm4v': $content_type = 'video/mp4'; break;
  66. case 'pdf': $content_type = 'application/pdf'; break;
  67. case 'doc': $content_type = 'application/vnd.ms-word'; break;
  68. case 'docx': $content_type = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'; break;
  69. case 'xls': $content_type = 'application/vnd.ms-excel'; break;
  70. case 'xlsx': $content_type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; break;
  71. case 'ppt': $content_type = 'application/vnd.ms-powerpoint'; break;
  72. case 'pptx': $content_type = 'application/vnd.openxmlformats-officedocument.presentationml.presentation'; break;
  73. case 'zip': $content_type = 'application/zip'; break;
  74. default: $content_type = 'application/octet-stream'; break;
  75. }
  76. //show the image inline when the action is not set
  77. if (empty($action) && ($content_type == 'image/jpeg' || $content_type == 'image/png' || $content_type == 'image/gif')) {
  78. $action = 'download-inline';
  79. }
  80. switch ($action) {
  81. case 'download':
  82. //header("Content-type: ".$content_type."; charset=utf-8");
  83. //header("Content-Disposition: attachment; filename=\"".$filename."\"");
  84. header('Content-Description: File Transfer');
  85. header('Content-Type: application/octet-stream');
  86. header("Content-Disposition: attachment; filename=\"".(strlen($media['message_media_name']) <= 36 ? $media['message_media_name'] : $message_media_uuid.'.'.strtolower($media['message_media_type']))."\"");
  87. header('Content-Transfer-Encoding: binary');
  88. header('Expires: 0');
  89. header('Cache-Control: must-revalidate');
  90. header('Pragma: public');
  91. header("Content-Length: ".strlen(base64_decode($media['message_media_content'])));
  92. echo base64_decode($media['message_media_content']);
  93. break;
  94. case 'download-inline':
  95. //header("Content-type: ".$content_type."; charset=utf-8");
  96. //header("Content-Disposition: attachment; filename=\"".$filename."\"");
  97. header("Content-Type: ".$content_type);
  98. header("Content-Disposition: inline; filename=\"".$media['message_media_name']."\"");
  99. header("Content-Length: ".strlen(base64_decode($media['message_media_content'])));
  100. echo base64_decode($media['message_media_content']);
  101. break;
  102. case 'download-original':
  103. header("Content-type: ".$content_type."; charset=utf-8");
  104. header("Content-Disposition: attachment; filename=\"".$media['message_media_name']."\"");
  105. header("Content-Length: ".strlen(base64_decode($media['message_media_content'])));
  106. echo base64_decode($media['message_media_content']);
  107. break;
  108. case 'thumbnail':
  109. if ($content_type = 'image/jpeg' || $content_type = 'image/png' || $content_type = 'image/gif') {
  110. //get the image size
  111. $image_size = getimagesize('data://application/octet-stream;base64,' . $media['message_media_content']);
  112. $source_width = $image_size[0];
  113. $source_height = $image_size[1];
  114. //read the image
  115. $source_image = imagecreatefromstring(base64_decode($media['message_media_content']));
  116. //working
  117. //header('Content-Type: image/jpeg');
  118. //imagejpeg($source_image);
  119. //get the image width and height
  120. $source_width = imagesx($source_image);
  121. $source_height = imagesy($source_image);
  122. //calculate dimensions for the thumbmail
  123. $destination_width = $width;
  124. $destination_height = floor($source_height * ($destination_width / $source_width));
  125. //send content type http header
  126. header('Content-Type: '.$content_type);
  127. //create the image, resample it and then stream binary
  128. $destination_image = imagecreatetruecolor($destination_width, $destination_height);
  129. //imagealphablending($img_dest, false);
  130. //imagesavealpha($img_dest, true);
  131. if ($destination_image !== false) {
  132. imagecopyresampled($destination_image, $source_image, 0, 0, 0, 0, $destination_width, $destination_height, $source_width, $source_height);
  133. if ($content_type = 'image/jpeg') {
  134. imagejpeg($destination_image);
  135. }
  136. if ($content_type = 'image/png') {
  137. imagejpeg($destination_image);
  138. }
  139. if ($content_type = 'image/gif') {
  140. imagejpeg($destination_image);
  141. }
  142. }
  143. }
  144. break;
  145. case 'display':
  146. echo " <table cellpadding='0' cellspacing='0' border='0' width='100%' height='100%'>\n";
  147. echo " <tr>\n";
  148. echo " <td align='center' valign='middle'>\n";
  149. 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";
  150. echo " </td>\n";
  151. echo " </tr>\n";
  152. echo " </table>\n";
  153. break;
  154. }
  155. }
  156. ?>