dialplan_tool_edit.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <?php
  2. /*
  3. Copyright (c) 2019-2025 Mark J Crane <[email protected]>
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. 1. Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. 2. Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
  13. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  14. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  15. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  16. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  17. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  18. OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  19. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  20. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  21. OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  22. SUCH DAMAGE.
  23. */
  24. //includes files
  25. require_once dirname(__DIR__, 2) . "/resources/require.php";
  26. //check permissions
  27. require_once "resources/check_auth.php";
  28. if (!permission_exists('dialplan_tool_add') && !permission_exists('dialplan_tool_edit')) {
  29. echo "access denied";
  30. exit;
  31. }
  32. //add multi-lingual support
  33. $language = new text;
  34. $text = $language->get();
  35. //action add or update
  36. if (!empty($_REQUEST["id"]) && is_uuid($_REQUEST["id"])) {
  37. $action = "update";
  38. $dialplan_tool_uuid = $_REQUEST["id"];
  39. }
  40. else {
  41. $action = "add";
  42. }
  43. //get http post variables and set them to php variables
  44. if (!empty($_POST) && is_array($_POST)) {
  45. $name = $_POST["name"];
  46. $application = $_POST["application"];
  47. $data = $_POST["data"];
  48. $enabled = $_POST["enabled"];
  49. $description = $_POST["description"];
  50. }
  51. //process the user data and save it to the database
  52. if (!empty($_POST) && count($_POST) > 0 && (empty($_POST["persistformvar"]) || strlen($_POST["persistformvar"]) == 0)) {
  53. //validate the token
  54. $token = new token;
  55. if (!$token->validate($_SERVER['PHP_SELF'])) {
  56. message::add($text['message-invalid_token'],'negative');
  57. header('Location: dialplan_tools.php');
  58. exit;
  59. }
  60. //process the http post data by submitted action
  61. if (!empty($_POST['action']) && strlen($_POST['action']) > 0) {
  62. //prepare the array(s)
  63. //send the array to the database class
  64. switch ($_POST['action']) {
  65. case 'copy':
  66. if (permission_exists('dialplan_tool_add')) {
  67. $obj = new database;
  68. $obj->copy($array);
  69. }
  70. break;
  71. case 'delete':
  72. if (permission_exists('dialplan_tool_delete')) {
  73. $obj = new database;
  74. $obj->delete($array);
  75. }
  76. break;
  77. case 'toggle':
  78. if (permission_exists('dialplan_tool_update')) {
  79. $obj = new database;
  80. $obj->toggle($array);
  81. }
  82. break;
  83. }
  84. //redirect the user
  85. if (in_array($_POST['action'], array('copy', 'delete', 'toggle'))) {
  86. header('Location: dialplan_tool_edit.php?id='.$id);
  87. exit;
  88. }
  89. }
  90. //check for all required data
  91. $msg = '';
  92. if (strlen($name) == 0) { $msg .= $text['message-required']." ".$text['label-name']."<br>\n"; }
  93. if (strlen($application) == 0) { $msg .= $text['message-required']." ".$text['label-application']."<br>\n"; }
  94. //if (strlen($data) == 0) { $msg .= $text['message-required']." ".$text['label-data']."<br>\n"; }
  95. if (strlen($enabled) == 0) { $msg .= $text['message-required']." ".$text['label-enabled']."<br>\n"; }
  96. //if (strlen($description) == 0) { $msg .= $text['message-required']." ".$text['label-description']."<br>\n"; }
  97. if (!empty($msg) && strlen($msg) > 0 && (empty($_POST["persistformvar"]) || strlen($_POST["persistformvar"]) == 0)) {
  98. require_once "resources/header.php";
  99. require_once "resources/persist_form_var.php";
  100. echo "<div align='center'>\n";
  101. echo "<table><tr><td>\n";
  102. echo $msg."<br />";
  103. echo "</td></tr></table>\n";
  104. persistformvar($_POST);
  105. echo "</div>\n";
  106. require_once "resources/footer.php";
  107. return;
  108. }
  109. //add the dialplan_tool_uuid
  110. if (!is_uuid($_POST["dialplan_tool_uuid"])) {
  111. $dialplan_tool_uuid = uuid();
  112. }
  113. //prepare the array
  114. $array['dialplan_tools'][0]['dialplan_tool_uuid'] = $dialplan_tool_uuid;
  115. if (permission_exists('dialplan_tool_domain')) {
  116. if (is_uuid($_POST["domain_uuid"])) {
  117. $array['dialplan_tools'][0]['domain_uuid'] = $_POST['domain_uuid'];
  118. }
  119. else {
  120. $array['dialplan_tools'][0]['domain_uuid'] = ''; //global
  121. }
  122. }
  123. else {
  124. $array['dialplan_tools'][0]['domain_uuid'] = $_SESSION['domain_uuid'];
  125. }
  126. $array['dialplan_tools'][0]['name'] = $name;
  127. if (!preg_match("/system/i", $application)) {
  128. $array['dialplan_tools'][0]['application'] = $application;
  129. }
  130. if (!preg_match("/system/i", $data)) {
  131. $array['dialplan_tools'][0]['data'] = $data;
  132. }
  133. $array['dialplan_tools'][0]['enabled'] = $enabled;
  134. $array['dialplan_tools'][0]['description'] = $description;
  135. //save the data
  136. $database = new database;
  137. $database->app_name = 'dialplan tools';
  138. $database->app_uuid = 'dbe1a32f-4cf2-4986-af22-154ef66abfae';
  139. $database->save($array);
  140. //redirect the user
  141. if (isset($action)) {
  142. if ($action == "add") {
  143. $_SESSION["message"] = $text['message-add'];
  144. }
  145. if ($action == "update") {
  146. $_SESSION["message"] = $text['message-update'];
  147. }
  148. //header('Location: dialplan_tools.php');
  149. header('Location: dialplan_tool_edit.php?id='.urlencode($dialplan_tool_uuid));
  150. return;
  151. }
  152. }
  153. //get the list of applications
  154. $fp = event_socket_create();
  155. if ($fp) {
  156. $result = event_socket_request($fp, 'api show application as json');
  157. if (is_array($result)) {
  158. $result = $result['Content'];
  159. }
  160. $array = json_decode($result, true);
  161. $dialplan_tools = $array['rows'];
  162. unset($result, $fp);
  163. }
  164. //pre-populate the form
  165. if (!empty($_GET) && is_array($_GET) && (empty($_POST["persistformvar"]) || $_POST["persistformvar"] != "true")) {
  166. $sql = "select ";
  167. $sql .= "domain_uuid, ";
  168. $sql .= "dialplan_tool_uuid, ";
  169. $sql .= "name, ";
  170. $sql .= "application, ";
  171. $sql .= "data, ";
  172. $sql .= "enabled, ";
  173. $sql .= "description ";
  174. $sql .= "from v_dialplan_tools ";
  175. $sql .= "where dialplan_tool_uuid = :dialplan_tool_uuid ";
  176. //$sql .= "and (domain_uuid = :domain_uuid or domain_uuid is null) ";
  177. //$parameters['domain_uuid'] = $_SESSION['domain_uuid'];
  178. $parameters['dialplan_tool_uuid'] = $dialplan_tool_uuid;
  179. $database = new database;
  180. $row = $database->select($sql, $parameters ?? null, 'row');
  181. if (is_array($row) && @sizeof($row) != 0) {
  182. $domain_uuid = $row["domain_uuid"];
  183. $name = $row["name"];
  184. $application = $row["application"];
  185. $data = $row["data"];
  186. $enabled = $row["enabled"] ?? false;
  187. $description = $row["description"];
  188. }
  189. unset($sql, $parameters, $row);
  190. }
  191. //create token
  192. $object = new token;
  193. $token = $object->create($_SERVER['PHP_SELF']);
  194. //show the header
  195. $document['title'] = $text['title-dialplan_tool'];
  196. require_once "resources/header.php";
  197. //show the content
  198. echo "<form name='frm' id='frm' method='post' action=''>\n";
  199. echo "<input class='formfld' type='hidden' name='dialplan_tool_uuid' value='".escape($dialplan_tool_uuid ?? '')."'>\n";
  200. echo "<div class='action_bar' id='action_bar'>\n";
  201. echo " <div class='heading'><b>".$text['title-dialplan_tool']."</b></div>\n";
  202. echo " <div class='actions'>\n";
  203. echo button::create(['type'=>'button','label'=>$text['button-back'],'icon'=>$settings->get('theme','button_icon_back'),'id'=>'btn_back','collapse'=>'hide-xs','style'=>'margin-right: 15px;','link'=>'dialplan_tools.php']);
  204. if ($action == 'update') {
  205. if (permission_exists('_add')) {
  206. echo button::create(['type'=>'button','label'=>$text['button-copy'],'icon'=>$settings->get('theme','button_icon_copy'),'id'=>'btn_copy','name'=>'btn_copy','style'=>'display: none;','onclick'=>"modal_open('modal-copy','btn_copy');"]);
  207. }
  208. if (permission_exists('_delete')) {
  209. echo button::create(['type'=>'button','label'=>$text['button-delete'],'icon'=>$settings->get('theme','button_icon_delete'),'id'=>'btn_delete','name'=>'btn_delete','style'=>'display: none; margin-right: 15px;','onclick'=>"modal_open('modal-delete','btn_delete');"]);
  210. }
  211. }
  212. echo button::create(['type'=>'submit','label'=>$text['button-save'],'icon'=>$settings->get('theme','button_icon_save'),'id'=>'btn_save','collapse'=>'hide-xs']);
  213. echo " </div>\n";
  214. echo " <div style='clear: both;'></div>\n";
  215. echo "</div>\n";
  216. echo $text['title_description-dialplan_tools']."\n";
  217. echo "<br /><br />\n";
  218. if ($action == 'update') {
  219. if (permission_exists('dialplan_tool_add')) {
  220. echo modal::create(['id'=>'modal-copy','type'=>'copy','actions'=>button::create(['type'=>'submit','label'=>$text['button-continue'],'icon'=>'check','id'=>'btn_copy','style'=>'float: right; margin-left: 15px;','collapse'=>'never','name'=>'action','value'=>'copy','onclick'=>"modal_close();"])]);
  221. }
  222. if (permission_exists('dialplan_tool_delete')) {
  223. echo modal::create(['id'=>'modal-delete','type'=>'delete','actions'=>button::create(['type'=>'submit','label'=>$text['button-continue'],'icon'=>'check','id'=>'btn_delete','style'=>'float: right; margin-left: 15px;','collapse'=>'never','name'=>'action','value'=>'delete','onclick'=>"modal_close();"])]);
  224. }
  225. }
  226. echo "<div class='card'>\n";
  227. echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
  228. echo "<tr>\n";
  229. echo "<td class='vncellreq' valign='top' align='left' nowrap='nowrap'>\n";
  230. echo " ".$text['label-name']."\n";
  231. echo "</td>\n";
  232. echo "<td class='vtable' style='position: relative;' align='left'>\n";
  233. echo " <input class='formfld' type='text' name='name' maxlength='255' value='".escape($name ?? '')."'>\n";
  234. echo "<br />\n";
  235. echo $text['description-name']."\n";
  236. echo "</td>\n";
  237. echo "</tr>\n";
  238. echo "<tr>\n";
  239. echo "<td class='vncellreq' valign='top' align='left' nowrap='nowrap'>\n";
  240. echo " ".$text['label-application']."\n";
  241. echo "</td>\n";
  242. echo "<td class='vtable' style='position: relative;' align='left'>\n";
  243. echo " <select name='application' class='formfld' style='width: auto; ".($element['visibility'] ?? '')."' onchange='change_to_input(this);'>\n";
  244. if (!empty($application) && strlen($application) > 0) {
  245. echo " <option value=\"".escape($application)."\" selected=\"selected\">".escape($application)."</option>\n";
  246. }
  247. else {
  248. echo " <option value=''></option>\n";
  249. }
  250. if (is_array($dialplan_tools)) {
  251. foreach ($dialplan_tools as $row) {
  252. if ($row['name'] != "name" && $row['name'] != "system") {
  253. echo " <option value='".escape($row['name'])."'>".escape($row['name'])."</option>\n";
  254. }
  255. }
  256. }
  257. echo " </select>\n";
  258. echo " <br />\n";
  259. echo " ".$text['description-application']."\n";
  260. echo "</td>\n";
  261. echo "</tr>\n";
  262. echo "<tr>\n";
  263. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  264. echo " ".$text['label-data']."\n";
  265. echo "</td>\n";
  266. echo "<td class='vtable' style='position: relative;' align='left'>\n";
  267. echo " <input class='formfld' type='text' name='data' maxlength='255' value='".escape($data ?? '')."'>\n";
  268. echo "<br />\n";
  269. echo $text['description-data']."\n";
  270. echo "</td>\n";
  271. echo "</tr>\n";
  272. if (permission_exists('dialplan_tool_domain')) {
  273. echo " <tr>\n";
  274. echo " <td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  275. echo " ".$text['label-domain']."\n";
  276. echo " </td>\n";
  277. echo " <td class='vtable' align='left'>\n";
  278. echo " <select class='formfld' name='domain_uuid'>\n";
  279. if (!is_uuid($domain_uuid)) {
  280. echo " <option value='' selected='selected'>".$text['label-global']."</option>\n";
  281. }
  282. else {
  283. echo " <option value=''>".$text['label-global']."</option>\n";
  284. }
  285. if (is_array($_SESSION['domains']) && @sizeof($_SESSION['domains']) != 0) {
  286. foreach ($_SESSION['domains'] as $row) {
  287. if ($row['domain_uuid'] == $domain_uuid) {
  288. echo " <option value='".escape($row['domain_uuid'])."' selected='selected'>".escape($row['domain_name'])."</option>\n";
  289. }
  290. else {
  291. echo " <option value='".escape($row['domain_uuid'])."'>".escape($row['domain_name'])."</option>\n";
  292. }
  293. }
  294. }
  295. echo " </select>\n";
  296. echo " <br />\n";
  297. //echo " ".$text['description-domain_name']."\n";
  298. echo " </td>\n";
  299. echo " </tr>\n";
  300. }
  301. echo "<tr>\n";
  302. echo "<td class='vncellreq' valign='top' align='left' nowrap='nowrap'>\n";
  303. echo " ".$text['label-enabled']."\n";
  304. echo "</td>\n";
  305. echo "<td class='vtable' style='position: relative;' align='left'>\n";
  306. if ($input_toggle_style_switch) {
  307. echo " <span class='switch'>\n";
  308. }
  309. echo " <select class='formfld' id='enabled' name='enabled'>\n";
  310. echo " <option value='true' ".($enabled == true ? "selected='selected'" : null).">".$text['option-true']."</option>\n";
  311. echo " <option value='false' ".($enabled == false ? "selected='selected'" : null).">".$text['option-false']."</option>\n";
  312. echo " </select>\n";
  313. if ($input_toggle_style_switch) {
  314. echo " <span class='slider'></span>\n";
  315. echo " </span>\n";
  316. }
  317. echo "<br />\n";
  318. echo $text['description-enabled']."\n";
  319. echo "</td>\n";
  320. echo "</tr>\n";
  321. echo "<tr>\n";
  322. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  323. echo " ".$text['label-description']."\n";
  324. echo "</td>\n";
  325. echo "<td class='vtable' style='position: relative;' align='left'>\n";
  326. echo " <input class='formfld' type='text' name='description' maxlength='255' value='".escape($description ?? '')."'>\n";
  327. echo "<br />\n";
  328. echo $text['description-description']."\n";
  329. echo "</td>\n";
  330. echo "</tr>\n";
  331. echo "</table>";
  332. echo "</div>\n";
  333. echo "<br /><br />";
  334. echo "<input type='hidden' name='".$token['name']."' value='".$token['hash']."'>\n";
  335. echo "</form>";
  336. //include the footer
  337. require_once "resources/footer.php";