contact_attachment_edit.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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) 2008-2018
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. Luis Daniel Lucio Quiroz <[email protected]>
  21. */
  22. //includes
  23. require_once "root.php";
  24. require_once "resources/require.php";
  25. require_once "resources/check_auth.php";
  26. //check permissions
  27. if (!permission_exists('contact_attachment_edit') && !permission_exists('contact_attachment_add')) {
  28. echo "access denied"; exit;
  29. }
  30. //add multi-lingual support
  31. $language = new text;
  32. $text = $language->get();
  33. //action add or update
  34. $contact_attachment_uuid = $_REQUEST['id'];
  35. $contact_uuid = $_REQUEST['contact_uuid'];
  36. if (is_uuid($contact_attachment_uuid) && is_uuid($contact_uuid)) {
  37. $action = 'update';
  38. }
  39. else if (is_uuid($contact_uuid)) {
  40. $action = 'add';
  41. }
  42. else {
  43. exit;
  44. }
  45. //get http post variables and set them to php variables
  46. if (is_array($_POST) && sizeof($_POST) != 0) {
  47. $attachment = $_FILES['attachment'];
  48. $attachment_primary = $_POST['attachment_primary'];
  49. $attachment_description = $_POST['attachment_description'];
  50. if (!is_array($attachment) || sizeof($attachment) == 0) {
  51. $attachment_type = strtolower(pathinfo($_POST['attachment_filename'], PATHINFO_EXTENSION));
  52. }
  53. else {
  54. $attachment_type = strtolower(pathinfo($attachment['name'], PATHINFO_EXTENSION));
  55. }
  56. //unflag others as primary
  57. $allowed_primary_attachment = false;
  58. if ($attachment_primary && ($attachment_type == 'jpg' || $attachment_type == 'jpeg' || $attachment_type == 'gif' || $attachment_type == 'png')) {
  59. $sql = "update v_contact_attachments set attachment_primary = 0 ";
  60. $sql .= "where domain_uuid = :domain_uuid ";
  61. $sql .= "and contact_uuid = :contact_uuid ";
  62. $parameters['domain_uuid'] = $domain_uuid;
  63. $parameters['contact_uuid'] = $contact_uuid;
  64. $database = new database;
  65. $database->execute($sql, $parameters);
  66. unset($sql, $parameters);
  67. $allowed_primary_attachment = true;
  68. }
  69. //format array
  70. $allowed_extensions = array_keys(json_decode($_SESSION['contact']['allowed_attachment_types']['text'], true));
  71. $array['contact_attachments'][$index]['contact_attachment_uuid'] = $action == 'update' ? $contact_attachment_uuid : uuid();
  72. $array['contact_attachments'][$index]['domain_uuid'] = $_SESSION['domain_uuid'];
  73. $array['contact_attachments'][$index]['contact_uuid'] = $contact_uuid;
  74. $array['contact_attachments'][$index]['attachment_primary'] = $allowed_primary_attachment ? 1 : 0;
  75. if ($attachment['error'] == '0' && in_array(strtolower(pathinfo($attachment['name'], PATHINFO_EXTENSION)), $allowed_extensions)) {
  76. $array['contact_attachments'][$index]['attachment_filename'] = $attachment['name'];
  77. $array['contact_attachments'][$index]['attachment_content'] = base64_encode(file_get_contents($attachment['tmp_name']));
  78. }
  79. $array['contact_attachments'][$index]['attachment_description'] = $attachment_description;
  80. if ($action == 'add') {
  81. $array['contact_attachments'][$index]['attachment_uploaded_date'] = 'now()';
  82. $array['contact_attachments'][$index]['attachment_uploaded_user_uuid'] = $_SESSION['user_uuid'];
  83. }
  84. //save data
  85. $database = new database;
  86. $database->app_name = 'contacts';
  87. $database->app_uuid = '04481e0e-a478-c559-adad-52bd4174574c';
  88. $database->save($array);
  89. unset($array);
  90. //redirect
  91. message::add($text['message-message_'.($action == 'update' ? 'updated' : 'added')]);
  92. header('Location: contact_edit.php?id='.$contact_uuid);
  93. exit;
  94. }
  95. //get form data
  96. if (is_array($_GET) && sizeof($_GET) != 0) {
  97. $sql = "select * from v_contact_attachments ";
  98. $sql .= "where domain_uuid = :domain_uuid ";
  99. $sql .= "and contact_attachment_uuid = :contact_attachment_uuid ";
  100. $parameters['domain_uuid'] = $domain_uuid;
  101. $parameters['contact_attachment_uuid'] = $contact_attachment_uuid;
  102. $database = new database;
  103. $row = $database->select($sql, $parameters, 'row');
  104. if (is_array($row) && @sizeof($row) != 0) {
  105. $attachment_primary = $row["attachment_primary"];
  106. $attachment_filename = $row["attachment_filename"];
  107. $attachment_content = $row["attachment_content"];
  108. $attachment_description = $row["attachment_description"];
  109. }
  110. unset($sql, $parameters, $row);
  111. }
  112. //show the header
  113. require_once "resources/header.php";
  114. if ($action == "update") {
  115. $document['title'] = $text['title-contact_attachment-edit'];
  116. }
  117. else if ($action == "add") {
  118. $document['title'] = $text['title-contact_attachment-add'];
  119. }
  120. //show the content
  121. echo "<form method='post' name='frm' enctype='multipart/form-data' action=''>\n";
  122. echo "<input type='hidden' name='contact_uuid' value='".escape($contact_uuid)."'>\n";
  123. if ($action == "update") {
  124. echo "<input type='hidden' name='contact_attachment_uuid' value='".escape($contact_attachment_uuid)."'>\n";
  125. }
  126. echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
  127. echo "<tr>\n";
  128. echo "<td align='left' valign='top' nowrap='nowrap'><b>";
  129. if ($action == "update") {
  130. echo $text['header-contact_attachment-edit'];
  131. }
  132. else if ($action == "add") {
  133. echo $text['header-contact_attachment-add'];
  134. }
  135. echo "</b></td>\n";
  136. echo "<td align='right' valign='top'>";
  137. echo " <input type='button' class='btn' name='' alt='".$text['button-back']."' onclick=\"window.location='contact_edit.php?id=$contact_uuid'\" value='".$text['button-back']."'>";
  138. echo " <input type='submit' name='submit' class='btn' value='".$text['button-save']."'>\n";
  139. echo "</td>\n";
  140. echo "</tr>\n";
  141. echo "</table>\n";
  142. echo "<br>\n";
  143. echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
  144. echo "<tr>\n";
  145. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  146. echo " ".$text['label-attachment']."\n";
  147. echo "</td>\n";
  148. echo "<td class='vtable' align='left'>\n";
  149. $attachment_type = strtolower(pathinfo($attachment_filename, PATHINFO_EXTENSION));
  150. if ($action == 'update') {
  151. echo "<input type='hidden' name='attachment_filename' value=\"".escape($attachment_filename)."\">\n";
  152. if ($attachment_type == 'jpg' || $attachment_type == 'jpeg' || $attachment_type == 'gif' || $attachment_type == 'png') {
  153. echo "<img src='data:image/".$attachment_type.";base64,".$attachment_content."' style='border: none; width: auto; max-height: 400px;' oncontextmenu=\"window.open('contact_attachment.php?id=".$contact_attachment_uuid."&action=download'); return false;\">";
  154. }
  155. else {
  156. echo "<a href='contact_attachment.php?id=".$contact_attachment_uuid."&action=download' style='font-size: 120%;'>".$attachment_filename."</a>";
  157. }
  158. }
  159. else {
  160. $allowed_attachment_types = json_decode($_SESSION['contact']['allowed_attachment_types']['text'], true);
  161. echo " <input type='file' class='formfld' name='attachment' id='attachment' accept='.".implode(',.',array_keys($allowed_attachment_types))."'>\n";
  162. echo " <span style='display: inline-block; margin-top: 5px; font-size: 80%;'>".strtoupper(implode(', ', array_keys($allowed_attachment_types)))."</span>";
  163. }
  164. echo "</td>\n";
  165. echo "</tr>\n";
  166. if ($action == 'update' && ($attachment_type == 'jpg' || $attachment_type == 'jpeg' || $attachment_type == 'gif' || $attachment_type == 'png')) {
  167. echo "<tr>\n";
  168. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  169. echo " ".$text['label-attachment_filename']."\n";
  170. echo "</td>\n";
  171. echo "<td class='vtable' align='left'>\n";
  172. echo " <a href='contact_attachment.php?id=".$contact_attachment_uuid."&action=download' style='font-size: 120%;'>".$attachment_filename."</a>";
  173. echo "</td>\n";
  174. echo "</tr>\n";
  175. }
  176. echo "<tr>\n";
  177. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  178. echo " ".$text['label-primary']."\n";
  179. echo "</td>\n";
  180. echo "<td class='vtable' align='left'>\n";
  181. echo " <select class='formfld' name='attachment_primary' id='attachment_primary'>\n";
  182. echo " <option value='0'>".$text['option-false']."</option>\n";
  183. echo " <option value='1' ".(($attachment_primary) ? "selected" : null).">".$text['option-true']."</option>\n";
  184. echo " </select>\n";
  185. echo "</td>\n";
  186. echo "</tr>\n";
  187. echo "<tr>\n";
  188. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  189. echo " ".$text['label-attachment_description']."\n";
  190. echo "</td>\n";
  191. echo "<td class='vtable' align='left'>\n";
  192. echo " <input class='formfld' type='text' name='attachment_description' maxlength='255' value=\"".escape($attachment_description)."\">\n";
  193. echo "</td>\n";
  194. echo "</tr>\n";
  195. echo " <tr>\n";
  196. echo " <td colspan='2' align='right'>\n";
  197. echo " <br>\n";
  198. echo " <input type='submit' class='btn' value='".$text['button-save']."'>\n";
  199. echo " </td>\n";
  200. echo " </tr>";
  201. echo "</table>";
  202. echo "<br><br>";
  203. echo "</form>";
  204. //include the footer
  205. require_once "resources/footer.php";
  206. ?>