contact_attachment_edit.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. //validate the token
  51. $token = new token;
  52. if (!$token->validate($_SERVER['PHP_SELF'])) {
  53. message::add($text['message-invalid_token'],'negative');
  54. header('Location: contacts.php');
  55. exit;
  56. }
  57. if (!is_array($attachment) || sizeof($attachment) == 0) {
  58. $attachment_type = strtolower(pathinfo($_POST['attachment_filename'], PATHINFO_EXTENSION));
  59. }
  60. else {
  61. $attachment_type = strtolower(pathinfo($attachment['name'], PATHINFO_EXTENSION));
  62. }
  63. //unflag others as primary
  64. $allowed_primary_attachment = false;
  65. if ($attachment_primary && ($attachment_type == 'jpg' || $attachment_type == 'jpeg' || $attachment_type == 'gif' || $attachment_type == 'png')) {
  66. $sql = "update v_contact_attachments set attachment_primary = 0 ";
  67. $sql .= "where domain_uuid = :domain_uuid ";
  68. $sql .= "and contact_uuid = :contact_uuid ";
  69. $parameters['domain_uuid'] = $domain_uuid;
  70. $parameters['contact_uuid'] = $contact_uuid;
  71. $database = new database;
  72. $database->execute($sql, $parameters);
  73. unset($sql, $parameters);
  74. $allowed_primary_attachment = true;
  75. }
  76. //format array
  77. $allowed_extensions = array_keys(json_decode($_SESSION['contact']['allowed_attachment_types']['text'], true));
  78. $array['contact_attachments'][$index]['contact_attachment_uuid'] = $action == 'update' ? $contact_attachment_uuid : uuid();
  79. $array['contact_attachments'][$index]['domain_uuid'] = $_SESSION['domain_uuid'];
  80. $array['contact_attachments'][$index]['contact_uuid'] = $contact_uuid;
  81. $array['contact_attachments'][$index]['attachment_primary'] = $allowed_primary_attachment ? 1 : 0;
  82. if ($attachment['error'] == '0' && in_array(strtolower(pathinfo($attachment['name'], PATHINFO_EXTENSION)), $allowed_extensions)) {
  83. $array['contact_attachments'][$index]['attachment_filename'] = $attachment['name'];
  84. $array['contact_attachments'][$index]['attachment_content'] = base64_encode(file_get_contents($attachment['tmp_name']));
  85. }
  86. $array['contact_attachments'][$index]['attachment_description'] = $attachment_description;
  87. if ($action == 'add') {
  88. $array['contact_attachments'][$index]['attachment_uploaded_date'] = 'now()';
  89. $array['contact_attachments'][$index]['attachment_uploaded_user_uuid'] = $_SESSION['user_uuid'];
  90. }
  91. //save data
  92. $database = new database;
  93. $database->app_name = 'contacts';
  94. $database->app_uuid = '04481e0e-a478-c559-adad-52bd4174574c';
  95. $database->save($array);
  96. unset($array);
  97. //redirect
  98. message::add($text['message-message_'.($action == 'update' ? 'updated' : 'added')]);
  99. header('Location: contact_edit.php?id='.$contact_uuid);
  100. exit;
  101. }
  102. //get form data
  103. if (is_array($_GET) && sizeof($_GET) != 0) {
  104. $sql = "select * from v_contact_attachments ";
  105. $sql .= "where domain_uuid = :domain_uuid ";
  106. $sql .= "and contact_attachment_uuid = :contact_attachment_uuid ";
  107. $parameters['domain_uuid'] = $domain_uuid;
  108. $parameters['contact_attachment_uuid'] = $contact_attachment_uuid;
  109. $database = new database;
  110. $row = $database->select($sql, $parameters, 'row');
  111. if (is_array($row) && @sizeof($row) != 0) {
  112. $attachment_primary = $row["attachment_primary"];
  113. $attachment_filename = $row["attachment_filename"];
  114. $attachment_content = $row["attachment_content"];
  115. $attachment_description = $row["attachment_description"];
  116. }
  117. unset($sql, $parameters, $row);
  118. }
  119. //create token
  120. $object = new token;
  121. $token = $object->create($_SERVER['PHP_SELF']);
  122. //show the header
  123. if ($action == "update") {
  124. $document['title'] = $text['title-contact_attachment-edit'];
  125. }
  126. else if ($action == "add") {
  127. $document['title'] = $text['title-contact_attachment-add'];
  128. }
  129. require_once "resources/header.php";
  130. //show the content
  131. echo "<form method='post' name='frm' id='frm' enctype='multipart/form-data'>\n";
  132. echo "<div class='action_bar' id='action_bar'>\n";
  133. echo " <div class='heading'>";
  134. if ($action == "update") {
  135. echo "<b>".$text['header-contact_attachment-edit']."</b>";
  136. }
  137. else if ($action == "add") {
  138. echo "<b>".$text['header-contact_attachment-add']."</b>";
  139. }
  140. echo " </div>\n";
  141. echo " <div class='actions'>\n";
  142. echo button::create(['type'=>'button','label'=>$text['button-back'],'icon'=>$_SESSION['theme']['button_icon_back'],'id'=>'btn_back','style'=>'margin-right: 15px;','link'=>'contact_edit.php?id='.urlencode($contact_uuid)]);
  143. echo button::create(['type'=>'submit','label'=>$text['button-save'],'icon'=>$_SESSION['theme']['button_icon_save'],'id'=>'btn_save']);
  144. echo " </div>\n";
  145. echo " <div style='clear: both;'></div>\n";
  146. echo "</div>\n";
  147. echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
  148. echo "<tr>\n";
  149. echo "<td width='30%' class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  150. echo " ".$text['label-attachment']."\n";
  151. echo "</td>\n";
  152. echo "<td width='70%' class='vtable' align='left'>\n";
  153. $attachment_type = strtolower(pathinfo($attachment_filename, PATHINFO_EXTENSION));
  154. if ($action == 'update') {
  155. echo "<input type='hidden' name='attachment_filename' value=\"".escape($attachment_filename)."\">\n";
  156. if ($attachment_type == 'jpg' || $attachment_type == 'jpeg' || $attachment_type == 'gif' || $attachment_type == 'png') {
  157. 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;\">";
  158. }
  159. else {
  160. echo "<a href='contact_attachment.php?id=".$contact_attachment_uuid."&action=download' style='font-size: 120%;'>".$attachment_filename."</a>";
  161. }
  162. }
  163. else {
  164. $allowed_attachment_types = json_decode($_SESSION['contact']['allowed_attachment_types']['text'], true);
  165. echo " <input type='file' class='formfld' name='attachment' id='attachment' accept='.".implode(',.',array_keys($allowed_attachment_types))."'>\n";
  166. echo " <span style='display: inline-block; margin-top: 5px; font-size: 80%;'>".strtoupper(implode(', ', array_keys($allowed_attachment_types)))."</span>";
  167. }
  168. echo "</td>\n";
  169. echo "</tr>\n";
  170. if ($action == 'update' && ($attachment_type == 'jpg' || $attachment_type == 'jpeg' || $attachment_type == 'gif' || $attachment_type == 'png')) {
  171. echo "<tr>\n";
  172. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  173. echo " ".$text['label-attachment_filename']."\n";
  174. echo "</td>\n";
  175. echo "<td class='vtable' align='left'>\n";
  176. echo " <a href='contact_attachment.php?id=".$contact_attachment_uuid."&action=download' style='font-size: 120%;'>".$attachment_filename."</a>";
  177. echo "</td>\n";
  178. echo "</tr>\n";
  179. }
  180. echo "<tr>\n";
  181. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  182. echo " ".$text['label-primary']."\n";
  183. echo "</td>\n";
  184. echo "<td class='vtable' align='left'>\n";
  185. echo " <select class='formfld' name='attachment_primary' id='attachment_primary'>\n";
  186. echo " <option value='0'>".$text['option-false']."</option>\n";
  187. echo " <option value='1' ".(($attachment_primary) ? "selected" : null).">".$text['option-true']."</option>\n";
  188. echo " </select>\n";
  189. echo "</td>\n";
  190. echo "</tr>\n";
  191. echo "<tr>\n";
  192. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  193. echo " ".$text['label-attachment_description']."\n";
  194. echo "</td>\n";
  195. echo "<td class='vtable' align='left'>\n";
  196. echo " <input class='formfld' type='text' name='attachment_description' maxlength='255' value=\"".escape($attachment_description)."\">\n";
  197. echo "</td>\n";
  198. echo "</tr>\n";
  199. echo "</table>";
  200. echo "<br><br>";
  201. echo "<input type='hidden' name='contact_uuid' value='".escape($contact_uuid)."'>\n";
  202. if ($action == "update") {
  203. echo "<input type='hidden' name='contact_attachment_uuid' value='".escape($contact_attachment_uuid)."'>\n";
  204. }
  205. echo "<input type='hidden' name='".$token['name']."' value='".$token['hash']."'>\n";
  206. echo "</form>";
  207. //include the footer
  208. require_once "resources/footer.php";
  209. ?>