contact_url_edit.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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-2024
  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 files
  23. require_once dirname(__DIR__, 2) . "/resources/require.php";
  24. require_once "resources/check_auth.php";
  25. //check permissions
  26. if (permission_exists('contact_url_edit') || permission_exists('contact_url_add')) {
  27. //access granted
  28. }
  29. else {
  30. echo "access denied";
  31. exit;
  32. }
  33. //add multi-lingual support
  34. $language = new text;
  35. $text = $language->get();
  36. //set the defaults
  37. $url_label = '';
  38. $url_label_custom = '';
  39. $url_address = '';
  40. $url_primary = '';
  41. $url_description = '';
  42. //action add or update
  43. if (!empty($_REQUEST["id"]) && is_uuid($_REQUEST["id"])) {
  44. $action = "update";
  45. $contact_url_uuid = $_REQUEST["id"];
  46. }
  47. else {
  48. $action = "add";
  49. }
  50. //get the contact uuid
  51. if (!empty($_GET["contact_uuid"]) && is_uuid($_GET["contact_uuid"])) {
  52. $contact_uuid = $_GET["contact_uuid"];
  53. }
  54. //get http post variables and set them to php variables
  55. if (!empty($_POST)) {
  56. $url_label = $_POST["url_label"];
  57. $url_label_custom = $_POST["url_label_custom"];
  58. $url_address = $_POST["url_address"];
  59. $url_primary = $_POST["url_primary"];
  60. $url_description = $_POST["url_description"];
  61. //use custom label if set
  62. $url_label = !empty($url_label_custom) ? $url_label_custom : $url_label;
  63. }
  64. //process the form data
  65. if (!empty($_POST) && empty($_POST["persistformvar"])) {
  66. //set the uuid
  67. if ($action == "update") {
  68. $contact_url_uuid = $_POST["contact_url_uuid"];
  69. }
  70. //validate the token
  71. $token = new token;
  72. if (!$token->validate($_SERVER['PHP_SELF'])) {
  73. message::add($text['message-invalid_token'],'negative');
  74. header('Location: contacts.php');
  75. exit;
  76. }
  77. //check for all required data
  78. $msg = '';
  79. if (!empty($msg) && empty($_POST["persistformvar"])) {
  80. require_once "resources/header.php";
  81. require_once "resources/persist_form_var.php";
  82. echo "<div align='center'>\n";
  83. echo "<table><tr><td>\n";
  84. echo $msg."<br />";
  85. echo "</td></tr></table>\n";
  86. persistformvar($_POST);
  87. echo "</div>\n";
  88. require_once "resources/footer.php";
  89. return;
  90. }
  91. //add or update the database
  92. if (empty($_POST["persistformvar"])) {
  93. //update last modified
  94. $array['contacts'][0]['contact_uuid'] = $contact_uuid;
  95. $array['contacts'][0]['domain_uuid'] = $domain_uuid;
  96. $array['contacts'][0]['last_mod_date'] = 'now()';
  97. $array['contacts'][0]['last_mod_user'] = $_SESSION['username'];
  98. $p = permissions::new();
  99. $p->add('contact_edit', 'temp');
  100. $database = new database;
  101. $database->app_name = 'contacts';
  102. $database->app_uuid = '04481e0e-a478-c559-adad-52bd4174574c';
  103. $database->save($array);
  104. unset($array);
  105. $p->delete('contact_edit', 'temp');
  106. //if primary, unmark other primary urls
  107. if ($url_primary) {
  108. $sql = "update v_contact_urls set url_primary = 0 ";
  109. $sql .= "where domain_uuid = :domain_uuid ";
  110. $sql .= "and contact_uuid = :contact_uuid ";
  111. $parameters['domain_uuid'] = $domain_uuid;
  112. $parameters['contact_uuid'] = $contact_uuid;
  113. $database = new database;
  114. $database->execute($sql, $parameters ?? null);
  115. unset($sql, $parameters);
  116. }
  117. if ($action == "add" && permission_exists('contact_url_add')) {
  118. $contact_url_uuid = uuid();
  119. $array['contact_urls'][0]['contact_url_uuid'] = $contact_url_uuid;
  120. message::add($text['message-add']);
  121. }
  122. if ($action == "update" && permission_exists('contact_url_edit')) {
  123. $array['contact_urls'][0]['contact_url_uuid'] = $contact_url_uuid;
  124. message::add($text['message-update']);
  125. }
  126. if (!empty($array)) {
  127. $array['contact_urls'][0]['domain_uuid'] = $_SESSION['domain_uuid'];
  128. $array['contact_urls'][0]['contact_uuid'] = $contact_uuid;
  129. $array['contact_urls'][0]['url_label'] = $url_label;
  130. $array['contact_urls'][0]['url_address'] = $url_address;
  131. $array['contact_urls'][0]['url_primary'] = $url_primary ? 1 : 0;
  132. $array['contact_urls'][0]['url_description'] = $url_description;
  133. $database = new database;
  134. $database->app_name = 'contacts';
  135. $database->app_uuid = '04481e0e-a478-c559-adad-52bd4174574c';
  136. $database->save($array);
  137. unset($array);
  138. }
  139. header("Location: contact_edit.php?id=".$contact_uuid);
  140. exit;
  141. }
  142. }
  143. //pre-populate the form
  144. if (!empty($_GET) && empty($_POST["persistformvar"])) {
  145. $contact_url_uuid = $_GET["id"] ?? '';
  146. $sql = "select * from v_contact_urls ";
  147. $sql .= "where domain_uuid = :domain_uuid ";
  148. $sql .= "and contact_url_uuid = :contact_url_uuid ";
  149. $parameters['domain_uuid'] = $_SESSION['domain_uuid'];
  150. $parameters['contact_url_uuid'] = $contact_url_uuid;
  151. $database = new database;
  152. $row = $database->select($sql, $parameters, 'row');
  153. if (!empty($row)) {
  154. $url_label = $row["url_label"];
  155. $url_address = $row["url_address"];
  156. $url_primary = $row["url_primary"];
  157. $url_description = $row["url_description"];
  158. }
  159. unset($sql, $parameters, $row);
  160. }
  161. //create token
  162. $object = new token;
  163. $token = $object->create($_SERVER['PHP_SELF']);
  164. //show the header
  165. if ($action == "update") {
  166. $document['title'] = $text['title-contact_url-edit'];
  167. }
  168. else if ($action == "add") {
  169. $document['title'] = $text['title-contact_url-add'];
  170. }
  171. require_once "resources/header.php";
  172. //javascript to toggle input/select boxes
  173. echo "<script type='text/javascript'>";
  174. echo " function toggle_custom(field) {";
  175. echo " $('#'+field).toggle();";
  176. echo " document.getElementById(field).selectedIndex = 0;";
  177. echo " document.getElementById(field+'_custom').value = '';";
  178. echo " $('#'+field+'_custom').toggle();";
  179. echo " if ($('#'+field+'_custom').is(':visible')) { $('#'+field+'_custom').trigger('focus'); } else { $('#'+field).trigger('focus'); }";
  180. echo " }";
  181. echo "</script>";
  182. //show the content
  183. echo "<form method='post' name='frm' id='frm'>\n";
  184. echo "<div class='action_bar' id='action_bar'>\n";
  185. echo " <div class='heading'>";
  186. if ($action == "update") {
  187. echo "<b>".$text['header-contact_url-edit']."</b>";
  188. }
  189. else if ($action == "add") {
  190. echo "<b>".$text['header-contact_url-add']."</b>";
  191. }
  192. echo " </div>\n";
  193. echo " <div class='actions'>\n";
  194. 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 ?? '')]);
  195. echo button::create(['type'=>'submit','label'=>$text['button-save'],'icon'=>$_SESSION['theme']['button_icon_save'],'id'=>'btn_save']);
  196. echo " </div>\n";
  197. echo " <div style='clear: both;'></div>\n";
  198. echo "</div>\n";
  199. if ($action == "update") {
  200. echo $text['description-contact_url-edit'];
  201. }
  202. else if ($action == "add") {
  203. echo $text['description-contact_url-add'];
  204. }
  205. echo "<br /><br />\n";
  206. echo "<div class='card'>\n";
  207. echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
  208. echo "<tr>\n";
  209. echo "<td width='30%' class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  210. echo " ".$text['label-url_label']."\n";
  211. echo "</td>\n";
  212. echo "<td width='70%' class='vtable' align='left'>\n";
  213. if (!empty($_SESSION["contact"]["url_label"])) {
  214. sort($_SESSION["contact"]["url_label"]);
  215. foreach($_SESSION["contact"]["url_label"] as $row) {
  216. $url_label_options[] = "<option value='".$row."' ".(($row == $url_label) ? "selected='selected'" : null).">".$row."</option>";
  217. }
  218. $url_label_found = (in_array($url_label, $_SESSION["contact"]["url_label"])) ? true : false;
  219. }
  220. else {
  221. $selected[$url_label] = "selected";
  222. $default_labels[] = $text['option-work'];
  223. $default_labels[] = $text['option-personal'];
  224. $default_labels[] = $text['option-other'];
  225. foreach ($default_labels as $default_label) {
  226. $url_label_options[] = "<option value='".$default_label."' ".!empty($selected[$default_label]).">".$default_label."</option>";
  227. }
  228. $url_label_found = (in_array($url_label, $default_labels)) ? true : false;
  229. }
  230. echo " <select class='formfld' ".((!empty($url_label) && !$url_label_found) ? "style='display: none;'" : null)." name='url_label' id='url_label' onchange=\"getElementById('url_label_custom').value='';\">\n";
  231. echo " <option value=''></option>\n";
  232. echo (!empty($url_label_options)) ? implode("\n", $url_label_options) : null;
  233. echo " </select>\n";
  234. echo " <input type='text' class='formfld' ".((empty($url_label) || $url_label_found) ? "style='display: none;'" : null)." name='url_label_custom' id='url_label_custom' value=\"".((!$url_label_found) ? htmlentities($url_label ?? '') : null)."\">\n";
  235. echo " <input type='button' id='btn_toggle_label' class='btn' alt='".$text['button-back']."' value='&#9665;' onclick=\"toggle_custom('url_label');\">\n";
  236. echo "<br />\n";
  237. echo !empty($text['description-url_label'])."\n";
  238. echo "</td>\n";
  239. echo "</tr>\n";
  240. echo "<tr>\n";
  241. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  242. echo " ".$text['label-url_address']."\n";
  243. echo "</td>\n";
  244. echo "<td class='vtable' align='left'>\n";
  245. echo " <input class='formfld' type='url' name='url_address' maxlength='255' value=\"".escape($url_address)."\" placeholder='http://...'>\n";
  246. echo "<br />\n";
  247. echo !empty($text['description-url_address'])."\n";
  248. echo "</td>\n";
  249. echo "</tr>\n";
  250. echo "<tr>\n";
  251. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  252. echo " ".$text['label-primary']."\n";
  253. echo "</td>\n";
  254. echo "<td class='vtable' align='left'>\n";
  255. echo " <select class='formfld' name='url_primary' id='url_primary'>\n";
  256. echo " <option value='0'>".$text['option-false']."</option>\n";
  257. echo " <option value='1' ".(($url_primary) ? "selected" : null).">".$text['option-true']."</option>\n";
  258. echo " </select>\n";
  259. echo "<br />\n";
  260. echo $text['description-url_primary']."\n";
  261. echo "</td>\n";
  262. echo "</tr>\n";
  263. echo "<tr>\n";
  264. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  265. echo " ".$text['label-url_description']."\n";
  266. echo "</td>\n";
  267. echo "<td class='vtable' align='left'>\n";
  268. echo " <input class='formfld' type='text' name='url_description' maxlength='255' value=\"".escape($url_description)."\">\n";
  269. echo "<br />\n";
  270. echo !empty($text['description-url_description'])."\n";
  271. echo "</td>\n";
  272. echo "</tr>\n";
  273. echo "</table>";
  274. echo "</div>\n";
  275. echo "<br><br>";
  276. echo "<input type='hidden' name='contact_uuid' value='".escape($contact_uuid ?? '')."'>\n";
  277. if ($action == "update") {
  278. echo "<input type='hidden' name='contact_url_uuid' value='".escape($contact_url_uuid)."'>\n";
  279. }
  280. echo "<input type='hidden' name='".$token['name']."' value='".$token['hash']."'>\n";
  281. echo "</form>";
  282. //include the footer
  283. require_once "resources/footer.php";
  284. ?>