database_edit.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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-2023
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. */
  21. //includes files
  22. require_once dirname(__DIR__, 2) . "/resources/require.php";
  23. require_once "resources/check_auth.php";
  24. //check permissions
  25. if (permission_exists('database_add') || permission_exists('database_edit')) {
  26. //access granted
  27. }
  28. else {
  29. echo "access denied";
  30. exit;
  31. }
  32. //connect to the database
  33. $database = new database;
  34. //add multi-lingual support
  35. $language = new text;
  36. $text = $language->get();
  37. //action add or update
  38. if (!empty($_REQUEST["id"]) && is_uuid($_REQUEST["id"])) {
  39. $action = "update";
  40. $database_uuid = $_REQUEST["id"];
  41. }
  42. else {
  43. $action = "add";
  44. }
  45. //clear the values
  46. $database_driver = '';
  47. $database_type = '';
  48. $database_host = '';
  49. $database_port = '';
  50. $database_name = '';
  51. $database_username = '';
  52. $database_password = '';
  53. $database_path = '';
  54. $database_description = '';
  55. //get http post variables and set them to php variables
  56. if (count($_POST)>0) {
  57. $database_driver = $_POST["database_driver"];
  58. $database_type = $_POST["database_type"];
  59. $database_host = $_POST["database_host"];
  60. $database_port = $_POST["database_port"];
  61. $database_name = $_POST["database_name"];
  62. $database_username = $_POST["database_username"];
  63. $database_password = $_POST["database_password"];
  64. $database_path = $_POST["database_path"];
  65. $database_description = $_POST["database_description"];
  66. }
  67. if (count($_POST)>0 && empty($_POST["persistformvar"])) {
  68. $msg = '';
  69. if ($action == "update") {
  70. $database_uuid = $_POST["database_uuid"];
  71. }
  72. //delete the database
  73. if (permission_exists('database_delete')) {
  74. if (!empty($_POST['action']) && $_POST['action'] == 'delete' && is_uuid($database_uuid)) {
  75. //prepare
  76. $array[0]['checked'] = 'true';
  77. $array[0]['uuid'] = $database_uuid;
  78. //delete
  79. $obj = new databases;
  80. $obj->delete($array);
  81. //redirect
  82. header('Location: databases.php');
  83. exit;
  84. }
  85. }
  86. //validate the token
  87. $token = new token;
  88. if (!$token->validate($_SERVER['PHP_SELF'])) {
  89. message::add($text['message-invalid_token'],'negative');
  90. header('Location: databases.php');
  91. exit;
  92. }
  93. //check for all required data
  94. //if (empty($database_driver)) { $msg .= $text['message-required'].$text['label-driver']."<br>\n"; }
  95. //if (empty($database_type)) { $msg .= $text['message-required'].$text['label-type']."<br>\n"; }
  96. //if (empty($database_host)) { $msg .= $text['message-required'].$text['label-host']."<br>\n"; }
  97. //if (empty($database_port)) { $msg .= $text['message-required'].$text['label-port']."<br>\n"; }
  98. //if (empty($database_name)) { $msg .= $text['message-required'].$text['label-name']."<br>\n"; }
  99. //if (empty($database_username)) { $msg .= $text['message-required'].$text['label-username']."<br>\n"; }
  100. //if (empty($database_password)) { $msg .= $text['message-required'].$text['label-password']."<br>\n"; }
  101. //if (empty($database_path)) { $msg .= $text['message-required'].$text['label-path']."<br>\n"; }
  102. //if (empty($database_description)) { $msg .= $text['message-required'].$text['label-description']."<br>\n"; }
  103. if (!empty($msg) && empty($_POST["persistformvar"])) {
  104. require_once "resources/header.php";
  105. require_once "resources/persist_form_var.php";
  106. echo "<div align='center'>\n";
  107. echo "<table><tr><td>\n";
  108. echo $msg."<br />";
  109. echo "</td></tr></table>\n";
  110. persistformvar($_POST);
  111. echo "</div>\n";
  112. require_once "resources/footer.php";
  113. return;
  114. }
  115. //add or update the database
  116. if (empty($_POST["persistformvar"])) {
  117. //begin array
  118. $array['databases'][0]['database_driver'] = $database_driver;
  119. $array['databases'][0]['database_type'] = $database_type;
  120. $array['databases'][0]['database_host'] = $database_host;
  121. $array['databases'][0]['database_port'] = $database_port;
  122. $array['databases'][0]['database_name'] = $database_name;
  123. $array['databases'][0]['database_username'] = $database_username;
  124. $array['databases'][0]['database_password'] = $database_password;
  125. $array['databases'][0]['database_path'] = $database_path;
  126. $array['databases'][0]['database_description'] = $database_description;
  127. if ($action == "add") {
  128. //add new uuid
  129. $array['databases'][0]['database_uuid'] = uuid();
  130. $database->app_name = 'databases';
  131. $database->app_uuid = '8d229b6d-1383-fcec-74c6-4ce1682479e2';
  132. $database->save($array);
  133. unset($array);
  134. //set the defaults
  135. require_once "app_defaults.php";
  136. //redirect the browser
  137. message::add($text['message-add']);
  138. header("Location: databases.php");
  139. exit;
  140. }
  141. if ($action == "update") {
  142. //add uuid to update
  143. $array['databases'][0]['database_uuid'] = $database_uuid;
  144. $database->app_name = 'databases';
  145. $database->app_uuid = '8d229b6d-1383-fcec-74c6-4ce1682479e2';
  146. $database->save($array);
  147. unset($array);
  148. //set the defaults
  149. $domains_processed = 1;
  150. require_once "app_defaults.php";
  151. //redirect the browser
  152. message::add($text['message-update']);
  153. header("Location: databases.php");
  154. exit;
  155. }
  156. }
  157. }
  158. //pre-populate the form
  159. if (count($_GET)>0 && empty($_POST["persistformvar"])) {
  160. $database_uuid = $_GET["id"];
  161. $sql = "select * from v_databases ";
  162. $sql .= "where database_uuid = :database_uuid ";
  163. $parameters['database_uuid'] = $database_uuid;
  164. $row = $database->select($sql, $parameters, 'row');
  165. if (is_array($row) && sizeof($row) != 0) {
  166. $database_driver = $row["database_driver"];
  167. $database_type = $row["database_type"];
  168. $database_host = $row["database_host"];
  169. $database_port = $row["database_port"];
  170. $database_name = $row["database_name"];
  171. $database_username = $row["database_username"];
  172. $database_password = $row["database_password"];
  173. $database_path = $row["database_path"];
  174. $database_description = $row["database_description"];
  175. }
  176. unset($sql, $parameters, $row);
  177. }
  178. //create token
  179. $object = new token;
  180. $token = $object->create($_SERVER['PHP_SELF']);
  181. //include the header
  182. if ($action == "update") {
  183. $document['title'] = $text['title-database-edit'];
  184. }
  185. if ($action == "add") {
  186. $document['title'] = $text['title-database-add'];
  187. }
  188. require_once "resources/header.php";
  189. //show the content
  190. echo "<form method='post' name='frm' id='frm'>\n";
  191. echo "<div class='action_bar' id='action_bar'>\n";
  192. echo " <div class='heading'>";
  193. if ($action == "add") {
  194. echo "<b>".$text['header-database-add']."</b>";
  195. }
  196. if ($action == "update") {
  197. echo "<b>".$text['header-database-edit']."</b>";
  198. }
  199. echo " </div>\n";
  200. echo " <div class='actions'>\n";
  201. echo button::create(['type'=>'button','label'=>$text['button-back'],'icon'=>$_SESSION['theme']['button_icon_back'],'id'=>'btn_back','style'=>'margin-right: 15px;','link'=>'databases.php']);
  202. if ($action == 'update' && permission_exists('database_delete')) {
  203. echo button::create(['type'=>'button','label'=>$text['button-delete'],'icon'=>$_SESSION['theme']['button_icon_delete'],'name'=>'btn_delete','style'=>'margin-right: 15px;','onclick'=>"modal_open('modal-delete','btn_delete');"]);
  204. }
  205. echo button::create(['type'=>'submit','label'=>$text['button-save'],'icon'=>$_SESSION['theme']['button_icon_save'],'id'=>'btn_save','name'=>'action','value'=>'save']);
  206. echo " </div>\n";
  207. echo " <div style='clear: both;'></div>\n";
  208. echo "</div>\n";
  209. if ($action == 'update' && permission_exists('database_delete')) {
  210. 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();"])]);
  211. }
  212. if ($action == "add") {
  213. echo $text['description-database-add']."\n";
  214. }
  215. if ($action == "update") {
  216. echo $text['description-database-edit']."\n";
  217. }
  218. echo "<br /><br />\n";
  219. echo "<div class='card'>\n";
  220. echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
  221. echo "<tr>\n";
  222. echo "<td width='30%' class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  223. echo " ".$text['label-driver']."\n";
  224. echo "</td>\n";
  225. echo "<td width='70%' class='vtable' align='left'>\n";
  226. echo " <select class='formfld' name='database_driver'>\n";
  227. echo " <option value=''></option>\n";
  228. if ($database_driver == "sqlite") {
  229. echo " &nbsp; &nbsp;<option value='sqlite' selected='selected'>SQLite</option>\n";
  230. }
  231. else {
  232. echo " &nbsp; &nbsp;<option value='sqlite'>SQLite</option>\n";
  233. }
  234. if ($database_driver == "pgsql") {
  235. echo " &nbsp; &nbsp;<option value='pgsql' selected='selected'>PostgreSQL</option>\n";
  236. }
  237. else {
  238. echo " &nbsp; &nbsp;<option value='pgsql'>PostgreSQL</option>\n";
  239. }
  240. if ($database_driver == "mysql") {
  241. echo " &nbsp; &nbsp;<option value='mysql' selected='selected'>MySQL</option>\n";
  242. }
  243. else {
  244. echo " &nbsp; &nbsp;<option value='mysql'>MySQL</option>\n";
  245. }
  246. if ($database_driver == "odbc") {
  247. echo " &nbsp; &nbsp;<option value='odbc' selected='selected'>ODBC</option>\n";
  248. }
  249. else {
  250. echo " &nbsp; &nbsp;<option value='odbc'>ODBC</option>\n";
  251. }
  252. echo " </select>\n";
  253. echo "<br />\n";
  254. echo $text['description-driver']."\n";
  255. echo "</td>\n";
  256. echo "</tr>\n";
  257. echo "<tr>\n";
  258. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  259. echo " ".$text['label-type']."\n";
  260. echo "</td>\n";
  261. echo "<td class='vtable' align='left'>\n";
  262. echo " <select class='formfld' name='database_type'>\n";
  263. echo " <option value=''></option>\n";
  264. if ($database_type == "sqlite") {
  265. echo " &nbsp; &nbsp;<option value='sqlite' selected='selected'>SQLite</option>\n";
  266. }
  267. else {
  268. echo " &nbsp; &nbsp;<option value='sqlite'>SQLite</option>\n";
  269. }
  270. if ($database_type == "pgsql") {
  271. echo " &nbsp; &nbsp;<option value='pgsql' selected='selected'>PostgreSQL</option>\n";
  272. }
  273. else {
  274. echo " &nbsp; &nbsp;<option value='pgsql'>PostgreSQL</option>\n";
  275. }
  276. if ($database_type == "mysql") {
  277. echo " &nbsp; &nbsp;<option value='mysql' selected='selected'>MySQL</option>\n";
  278. }
  279. else {
  280. echo " &nbsp; &nbsp;<option value='mysql'>MySQL</option>\n";
  281. }
  282. if ($database_type == "mssql") {
  283. echo " &nbsp; &nbsp;<option value='mssql' selected='selected'>Microsoft SQL Server</option>\n";
  284. }
  285. else {
  286. echo " &nbsp; &nbsp;<option value='mssql'>Microsoft SQL Server</option>\n";
  287. }
  288. echo " </select>\n";
  289. echo "<br />\n";
  290. echo $text['description-type']."\n";
  291. echo "</td>\n";
  292. echo "</tr>\n";
  293. echo "<tr>\n";
  294. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  295. echo " ".$text['label-host']."\n";
  296. echo "</td>\n";
  297. echo "<td class='vtable' align='left'>\n";
  298. echo " <input class='formfld' type='text' name='database_host' maxlength='255' value=\"".escape($database_host)."\">\n";
  299. echo "<br />\n";
  300. echo $text['description-host']."\n";
  301. echo "</td>\n";
  302. echo "</tr>\n";
  303. echo "<tr>\n";
  304. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  305. echo " ".$text['label-port']."\n";
  306. echo "</td>\n";
  307. echo "<td class='vtable' align='left'>\n";
  308. echo " <input class='formfld' type='text' name='database_port' maxlength='255' value=\"".escape($database_port)."\">\n";
  309. echo "<br />\n";
  310. echo $text['description-port']."\n";
  311. echo "</td>\n";
  312. echo "</tr>\n";
  313. echo "<tr>\n";
  314. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  315. echo " ".$text['label-name']."\n";
  316. echo "</td>\n";
  317. echo "<td class='vtable' align='left'>\n";
  318. echo " <input class='formfld' type='text' name='database_name' maxlength='255' value=\"".escape($database_name)."\">\n";
  319. echo "<br />\n";
  320. echo $text['description-name']."\n";
  321. echo "</td>\n";
  322. echo "</tr>\n";
  323. echo "<tr>\n";
  324. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  325. echo " ".$text['label-username']."\n";
  326. echo "</td>\n";
  327. echo "<td class='vtable' align='left'>\n";
  328. echo " <input class='formfld' type='text' name='database_username' maxlength='255' value=\"".escape($database_username)."\">\n";
  329. echo "<br />\n";
  330. echo $text['description-username']."\n";
  331. echo "</td>\n";
  332. echo "</tr>\n";
  333. echo "<tr>\n";
  334. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  335. echo " ".$text['label-password']."\n";
  336. echo "</td>\n";
  337. echo "<td class='vtable' align='left'>\n";
  338. echo " <input class='formfld' type='text' name='database_password' maxlength='255' value=\"".escape($database_password)."\">\n";
  339. echo "<br />\n";
  340. echo $text['description-password']."\n";
  341. echo "</td>\n";
  342. echo "</tr>\n";
  343. echo "<tr>\n";
  344. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  345. echo " ".$text['label-path']."\n";
  346. echo "</td>\n";
  347. echo "<td class='vtable' align='left'>\n";
  348. echo " <input class='formfld' type='text' name='database_path' maxlength='255' value=\"".escape($database_path)."\">\n";
  349. echo "<br />\n";
  350. echo $text['description-path']."\n";
  351. echo "</td>\n";
  352. echo "</tr>\n";
  353. echo "<tr>\n";
  354. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  355. echo " ".$text['label-description']."\n";
  356. echo "</td>\n";
  357. echo "<td class='vtable' align='left'>\n";
  358. echo " <input class='formfld' type='text' name='database_description' maxlength='255' value=\"".escape($database_description)."\">\n";
  359. echo "<br />\n";
  360. echo $text['description-description']."\n";
  361. echo "</td>\n";
  362. echo "</tr>\n";
  363. echo "</table>";
  364. echo "</div>";
  365. echo "<br><br>";
  366. if ($action == "update") {
  367. echo "<input type='hidden' name='database_uuid' value='".escape($database_uuid)."'>\n";
  368. }
  369. echo "<input type='hidden' name='".$token['name']."' value='".$token['hash']."'>\n";
  370. echo "</form>";
  371. //include the footer
  372. require_once "resources/footer.php";
  373. ?>