database_edit.php 14 KB

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