dialplan_tool_edit.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <?php
  2. /*
  3. Copyright (c) 2019-2024 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. //access granted
  30. }
  31. else {
  32. echo "access denied";
  33. exit;
  34. }
  35. //add multi-lingual support
  36. $language = new text;
  37. $text = $language->get();
  38. //action add or update
  39. if (!empty($_REQUEST["id"]) && is_uuid($_REQUEST["id"])) {
  40. $action = "update";
  41. $dialplan_tool_uuid = $_REQUEST["id"];
  42. }
  43. else {
  44. $action = "add";
  45. }
  46. //get http post variables and set them to php variables
  47. if (!empty($_POST) && is_array($_POST)) {
  48. $name = $_POST["name"];
  49. $application = $_POST["application"];
  50. $data = $_POST["data"];
  51. $enabled = $_POST["enabled"];
  52. $description = $_POST["description"];
  53. }
  54. //process the user data and save it to the database
  55. if (!empty($_POST) && count($_POST) > 0 && (empty($_POST["persistformvar"]) || strlen($_POST["persistformvar"]) == 0)) {
  56. //validate the token
  57. $token = new token;
  58. if (!$token->validate($_SERVER['PHP_SELF'])) {
  59. message::add($text['message-invalid_token'],'negative');
  60. header('Location: dialplan_tools.php');
  61. exit;
  62. }
  63. //process the http post data by submitted action
  64. if (!empty($_POST['action']) && strlen($_POST['action']) > 0) {
  65. //prepare the array(s)
  66. //send the array to the database class
  67. switch ($_POST['action']) {
  68. case 'copy':
  69. if (permission_exists('dialplan_tool_add')) {
  70. $obj = new database;
  71. $obj->copy($array);
  72. }
  73. break;
  74. case 'delete':
  75. if (permission_exists('dialplan_tool_delete')) {
  76. $obj = new database;
  77. $obj->delete($array);
  78. }
  79. break;
  80. case 'toggle':
  81. if (permission_exists('dialplan_tool_update')) {
  82. $obj = new database;
  83. $obj->toggle($array);
  84. }
  85. break;
  86. }
  87. //redirect the user
  88. if (in_array($_POST['action'], array('copy', 'delete', 'toggle'))) {
  89. header('Location: dialplan_tool_edit.php?id='.$id);
  90. exit;
  91. }
  92. }
  93. //check for all required data
  94. $msg = '';
  95. if (strlen($name) == 0) { $msg .= $text['message-required']." ".$text['label-name']."<br>\n"; }
  96. if (strlen($application) == 0) { $msg .= $text['message-required']." ".$text['label-application']."<br>\n"; }
  97. //if (strlen($data) == 0) { $msg .= $text['message-required']." ".$text['label-data']."<br>\n"; }
  98. if (strlen($enabled) == 0) { $msg .= $text['message-required']." ".$text['label-enabled']."<br>\n"; }
  99. //if (strlen($description) == 0) { $msg .= $text['message-required']." ".$text['label-description']."<br>\n"; }
  100. if (!empty($msg) && strlen($msg) > 0 && (empty($_POST["persistformvar"]) || strlen($_POST["persistformvar"]) == 0)) {
  101. require_once "resources/header.php";
  102. require_once "resources/persist_form_var.php";
  103. echo "<div align='center'>\n";
  104. echo "<table><tr><td>\n";
  105. echo $msg."<br />";
  106. echo "</td></tr></table>\n";
  107. persistformvar($_POST);
  108. echo "</div>\n";
  109. require_once "resources/footer.php";
  110. return;
  111. }
  112. //add the dialplan_tool_uuid
  113. if (!is_uuid($_POST["dialplan_tool_uuid"])) {
  114. $dialplan_tool_uuid = uuid();
  115. }
  116. //prepare the array
  117. $array['dialplan_tools'][0]['dialplan_tool_uuid'] = $dialplan_tool_uuid;
  118. if (permission_exists('dialplan_tool_domain')) {
  119. if (is_uuid($_POST["domain_uuid"])) {
  120. $array['dialplan_tools'][0]['domain_uuid'] = $_POST['domain_uuid'];
  121. }
  122. else {
  123. $array['dialplan_tools'][0]['domain_uuid'] = ''; //global
  124. }
  125. }
  126. else {
  127. $array['dialplan_tools'][0]['domain_uuid'] = $_SESSION['domain_uuid'];
  128. }
  129. $array['dialplan_tools'][0]['name'] = $name;
  130. if (!preg_match("/system/i", $application)) {
  131. $array['dialplan_tools'][0]['application'] = $application;
  132. }
  133. if (!preg_match("/system/i", $data)) {
  134. $array['dialplan_tools'][0]['data'] = $data;
  135. }
  136. $array['dialplan_tools'][0]['enabled'] = $enabled;
  137. $array['dialplan_tools'][0]['description'] = $description;
  138. //save the data
  139. $database = new database;
  140. $database->app_name = 'dialplan tools';
  141. $database->app_uuid = 'dbe1a32f-4cf2-4986-af22-154ef66abfae';
  142. $database->save($array);
  143. //redirect the user
  144. if (isset($action)) {
  145. if ($action == "add") {
  146. $_SESSION["message"] = $text['message-add'];
  147. }
  148. if ($action == "update") {
  149. $_SESSION["message"] = $text['message-update'];
  150. }
  151. //header('Location: dialplan_tools.php');
  152. header('Location: dialplan_tool_edit.php?id='.urlencode($dialplan_tool_uuid));
  153. return;
  154. }
  155. }
  156. //get the list of applications
  157. $fp = event_socket_create();
  158. if ($fp) {
  159. $result = event_socket_request($fp, 'api show application as json');
  160. if (is_array($result)) {
  161. $result = $result['Content'];
  162. }
  163. $array = json_decode($result, true);
  164. $dialplan_tools = $array['rows'];
  165. unset($result, $fp);
  166. }
  167. //pre-populate the form
  168. if (!empty($_GET) && is_array($_GET) && (empty($_POST["persistformvar"]) || $_POST["persistformvar"] != "true")) {
  169. $sql = "select ";
  170. $sql .= "domain_uuid, ";
  171. $sql .= "dialplan_tool_uuid, ";
  172. $sql .= "name, ";
  173. $sql .= "application, ";
  174. $sql .= "data, ";
  175. $sql .= "cast(enabled as text), ";
  176. $sql .= "description ";
  177. $sql .= "from v_dialplan_tools ";
  178. $sql .= "where dialplan_tool_uuid = :dialplan_tool_uuid ";
  179. //$sql .= "and (domain_uuid = :domain_uuid or domain_uuid is null) ";
  180. //$parameters['domain_uuid'] = $_SESSION['domain_uuid'];
  181. $parameters['dialplan_tool_uuid'] = $dialplan_tool_uuid;
  182. $database = new database;
  183. $row = $database->select($sql, $parameters ?? null, 'row');
  184. if (is_array($row) && @sizeof($row) != 0) {
  185. $domain_uuid = $row["domain_uuid"];
  186. $name = $row["name"];
  187. $application = $row["application"];
  188. $data = $row["data"];
  189. $enabled = $row["enabled"];
  190. $description = $row["description"];
  191. }
  192. unset($sql, $parameters, $row);
  193. }
  194. //create token
  195. $object = new token;
  196. $token = $object->create($_SERVER['PHP_SELF']);
  197. //show the header
  198. $document['title'] = $text['title-dialplan_tool'];
  199. require_once "resources/header.php";
  200. //show the content
  201. echo "<form name='frm' id='frm' method='post' action=''>\n";
  202. echo "<input class='formfld' type='hidden' name='dialplan_tool_uuid' value='".escape($dialplan_tool_uuid ?? '')."'>\n";
  203. echo "<div class='action_bar' id='action_bar'>\n";
  204. echo " <div class='heading'><b>".$text['title-dialplan_tool']."</b></div>\n";
  205. echo " <div class='actions'>\n";
  206. echo button::create(['type'=>'button','label'=>$text['button-back'],'icon'=>$_SESSION['theme']['button_icon_back'],'id'=>'btn_back','collapse'=>'hide-xs','style'=>'margin-right: 15px;','link'=>'dialplan_tools.php']);
  207. if ($action == 'update') {
  208. if (permission_exists('_add')) {
  209. echo button::create(['type'=>'button','label'=>$text['button-copy'],'icon'=>$_SESSION['theme']['button_icon_copy'],'id'=>'btn_copy','name'=>'btn_copy','style'=>'display: none;','onclick'=>"modal_open('modal-copy','btn_copy');"]);
  210. }
  211. if (permission_exists('_delete')) {
  212. echo button::create(['type'=>'button','label'=>$text['button-delete'],'icon'=>$_SESSION['theme']['button_icon_delete'],'id'=>'btn_delete','name'=>'btn_delete','style'=>'display: none; margin-right: 15px;','onclick'=>"modal_open('modal-delete','btn_delete');"]);
  213. }
  214. }
  215. echo button::create(['type'=>'submit','label'=>$text['button-save'],'icon'=>$_SESSION['theme']['button_icon_save'],'id'=>'btn_save','collapse'=>'hide-xs']);
  216. echo " </div>\n";
  217. echo " <div style='clear: both;'></div>\n";
  218. echo "</div>\n";
  219. echo $text['title_description-dialplan_tools']."\n";
  220. echo "<br /><br />\n";
  221. if ($action == 'update') {
  222. if (permission_exists('dialplan_tool_add')) {
  223. 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();"])]);
  224. }
  225. if (permission_exists('dialplan_tool_delete')) {
  226. 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();"])]);
  227. }
  228. }
  229. echo "<div class='card'>\n";
  230. echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
  231. echo "<tr>\n";
  232. echo "<td class='vncellreq' valign='top' align='left' nowrap='nowrap'>\n";
  233. echo " ".$text['label-name']."\n";
  234. echo "</td>\n";
  235. echo "<td class='vtable' style='position: relative;' align='left'>\n";
  236. echo " <input class='formfld' type='text' name='name' maxlength='255' value='".escape($name ?? '')."'>\n";
  237. echo "<br />\n";
  238. echo $text['description-name']."\n";
  239. echo "</td>\n";
  240. echo "</tr>\n";
  241. echo "<tr>\n";
  242. echo "<td class='vncellreq' valign='top' align='left' nowrap='nowrap'>\n";
  243. echo " ".$text['label-application']."\n";
  244. echo "</td>\n";
  245. echo "<td class='vtable' style='position: relative;' align='left'>\n";
  246. echo " <select name='application' class='formfld' style='width: auto; ".($element['visibility'] ?? '')."' onchange='change_to_input(this);'>\n";
  247. if (!empty($application) && strlen($application) > 0) {
  248. echo " <option value=\"".escape($application)."\" selected=\"selected\">".escape($application)."</option>\n";
  249. }
  250. else {
  251. echo " <option value=''></option>\n";
  252. }
  253. if (is_array($dialplan_tools)) {
  254. foreach ($dialplan_tools as $row) {
  255. if ($row['name'] != "name" && $row['name'] != "system") {
  256. echo " <option value='".escape($row['name'])."'>".escape($row['name'])."</option>\n";
  257. }
  258. }
  259. }
  260. echo " </select>\n";
  261. echo " <br />\n";
  262. echo " ".$text['description-application']."\n";
  263. echo "</td>\n";
  264. echo "</tr>\n";
  265. echo "<tr>\n";
  266. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  267. echo " ".$text['label-data']."\n";
  268. echo "</td>\n";
  269. echo "<td class='vtable' style='position: relative;' align='left'>\n";
  270. echo " <input class='formfld' type='text' name='data' maxlength='255' value='".escape($data ?? '')."'>\n";
  271. echo "<br />\n";
  272. echo $text['description-data']."\n";
  273. echo "</td>\n";
  274. echo "</tr>\n";
  275. if (permission_exists('dialplan_tool_domain')) {
  276. echo " <tr>\n";
  277. echo " <td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  278. echo " ".$text['label-domain']."\n";
  279. echo " </td>\n";
  280. echo " <td class='vtable' align='left'>\n";
  281. echo " <select class='formfld' name='domain_uuid'>\n";
  282. if (!is_uuid($domain_uuid)) {
  283. echo " <option value='' selected='selected'>".$text['label-global']."</option>\n";
  284. }
  285. else {
  286. echo " <option value=''>".$text['label-global']."</option>\n";
  287. }
  288. if (is_array($_SESSION['domains']) && @sizeof($_SESSION['domains']) != 0) {
  289. foreach ($_SESSION['domains'] as $row) {
  290. if ($row['domain_uuid'] == $domain_uuid) {
  291. echo " <option value='".escape($row['domain_uuid'])."' selected='selected'>".escape($row['domain_name'])."</option>\n";
  292. }
  293. else {
  294. echo " <option value='".escape($row['domain_uuid'])."'>".escape($row['domain_name'])."</option>\n";
  295. }
  296. }
  297. }
  298. echo " </select>\n";
  299. echo " <br />\n";
  300. //echo " ".$text['description-domain_name']."\n";
  301. echo " </td>\n";
  302. echo " </tr>\n";
  303. }
  304. echo "<tr>\n";
  305. echo "<td class='vncellreq' valign='top' align='left' nowrap='nowrap'>\n";
  306. echo " ".$text['label-enabled']."\n";
  307. echo "</td>\n";
  308. echo "<td class='vtable' style='position: relative;' align='left'>\n";
  309. echo " <select class='formfld' name='enabled'>\n";
  310. if (!empty($enabled) && $enabled == "true") {
  311. echo " <option value='true' selected='selected'>".$text['label-true']."</option>\n";
  312. }
  313. else {
  314. echo " <option value='true'>".$text['label-true']."</option>\n";
  315. }
  316. if (!empty($enabled) && $enabled == "false") {
  317. echo " <option value='false' selected='selected'>".$text['label-false']."</option>\n";
  318. }
  319. else {
  320. echo " <option value='false'>".$text['label-false']."</option>\n";
  321. }
  322. echo " </select>\n";
  323. echo "<br />\n";
  324. echo $text['description-enabled']."\n";
  325. echo "</td>\n";
  326. echo "</tr>\n";
  327. echo "<tr>\n";
  328. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  329. echo " ".$text['label-description']."\n";
  330. echo "</td>\n";
  331. echo "<td class='vtable' style='position: relative;' align='left'>\n";
  332. echo " <input class='formfld' type='text' name='description' maxlength='255' value='".escape($description ?? '')."'>\n";
  333. echo "<br />\n";
  334. echo $text['description-description']."\n";
  335. echo "</td>\n";
  336. echo "</tr>\n";
  337. echo "</table>";
  338. echo "</div>\n";
  339. echo "<br /><br />";
  340. echo "<input type='hidden' name='".$token['name']."' value='".$token['hash']."'>\n";
  341. echo "</form>";
  342. //include the footer
  343. require_once "resources/footer.php";
  344. ?>