install.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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) 2019
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. */
  21. //includes
  22. include "root.php";
  23. require_once "resources/functions.php";
  24. //include required classes
  25. require_once "resources/classes/text.php";
  26. require_once "resources/classes/template.php";
  27. //add multi-lingual support
  28. $language = new text;
  29. $text = $language->get();
  30. //set debug to true or false
  31. $debug = false;
  32. //start the session
  33. //ini_set("session.cookie_httponly", True);
  34. session_start();
  35. //set the default domain_uuid
  36. $domain_uuid = uuid();
  37. //$_SESSION["domain_uuid"] = uuid();
  38. //add the menu uuid
  39. $menu_uuid = 'b4750c3f-2a86-b00d-b7d0-345c14eca286';
  40. //error reporting
  41. ini_set('display_errors', '1');
  42. //error_reporting (E_ALL); // Report everything
  43. //error reporting
  44. ini_set('display_errors', '1');
  45. //error_reporting (E_ALL); // Report everything
  46. error_reporting (E_ALL ^ E_NOTICE); // Report everything
  47. //error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING ); //hide notices and warnings
  48. //set the default time zone
  49. date_default_timezone_set('UTC');
  50. //if the config file exists then disable the install page
  51. $config_exists = false;
  52. if (file_exists($_SERVER['DOCUMENT_ROOT'].PROJECT_PATH."/resources/config.php")) {
  53. $config_exists = true;
  54. } elseif (file_exists("/etc/fusionpbx/config.php")) {
  55. //linux
  56. $config_exists = true;
  57. } elseif (file_exists("/usr/local/etc/fusionpbx/config.php")) {
  58. $config_exists = true;
  59. }
  60. if ($config_exists) {
  61. $msg .= "Already Installed";
  62. header("Location: ".PROJECT_PATH."/index.php?msg=".urlencode($msg));
  63. exit;
  64. }
  65. //process and save the data
  66. if (count($_POST) > 0) {
  67. foreach($_POST as $key => $value) {
  68. //$_SESSION['install'][$key] = $value;
  69. if ($key == 'admin_username') {
  70. $_SESSION['install'][$key] = $value;
  71. }
  72. if ($key == 'admin_password') {
  73. $_SESSION['install'][$key] = $value;
  74. }
  75. if ($key == 'domain_name') {
  76. $_SESSION['install'][$key] = $value;
  77. }
  78. if ($key == 'database_host') {
  79. $_SESSION['install'][$key] = $value;
  80. }
  81. if ($key == 'database_port') {
  82. $_SESSION['install'][$key] = $value;
  83. }
  84. if ($key == 'database_name') {
  85. $_SESSION['install'][$key] = $value;
  86. }
  87. if ($key == 'database_username') {
  88. $_SESSION['install'][$key] = $value;
  89. }
  90. if ($key == 'database_password') {
  91. $_SESSION['install'][$key] = $value;
  92. }
  93. //echo "if (\$key == '$key') {\n";
  94. //echo " \$_SESSION['install'][\$key] = \$value;\n";
  95. //echo "}\n";
  96. }
  97. if ($_REQUEST["step"] == "install") {
  98. //show debug information
  99. if ($debug) {
  100. echo "<pre>\n";
  101. print_r($_SESSION['install']);
  102. echo "</pre>\n";
  103. exit;
  104. }
  105. //add the config.php
  106. $config = "<?php\n";
  107. $config .= "\n";
  108. $config .= "/*\n";
  109. $config .= "FusionPBX\n";
  110. $config .= "Version: MPL 1.1\n";
  111. $config .= "\n";
  112. $config .= "The contents of this file are subject to the Mozilla Public License Version\n";
  113. $config .= "1.1 (the \"License\"); you may not use this file except in compliance with\n";
  114. $config .= "the License. You may obtain a copy of the License at\n";
  115. $config .= "http://www.mozilla.org/MPL/\n";
  116. $config .= "\n";
  117. $config .= "Software distributed under the License is distributed on an \"AS IS\" basis,\n";
  118. $config .= "WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License\n";
  119. $config .= "for the specific language governing rights and limitations under the\n";
  120. $config .= "License.\n";
  121. $config .= "\n";
  122. $config .= "The Original Code is FusionPBX\n";
  123. $config .= " Copyright (C) 2008 - 2019\n";
  124. $config .= " Mark J Crane <[email protected]>\n";
  125. $config .= " All rights reserved.\n";
  126. $config .= "*/\n";
  127. $config .= "\n";
  128. $config .= "//-----------------------------------------------------\n";
  129. $config .= "// settings:\n";
  130. $config .= "//-----------------------------------------------------\n";
  131. $config .= "\n";
  132. $config .= " //set the database type\n";
  133. $config .= " \$db_type = 'pgsql'; //sqlite, mysql, pgsql, others with a manually created PDO connection\n";
  134. $config .= "\n";
  135. $config .= "//database connection information\n";
  136. $config .= " \$db_host = '".$_SESSION['install']['database_host']."'; //set the host only if the database is not local\n";
  137. $config .= " \$db_port = '".$_SESSION['install']['database_port']."';\n";
  138. $config .= " \$db_name = '".$_SESSION['install']['database_name']."';\n";
  139. $config .= " \$db_username = '".$_SESSION['install']['database_username']."';\n";
  140. $config .= " \$db_password = '".$_SESSION['install']['database_password']."';\n";
  141. $config .= "\n";
  142. $config .= " //show errors\n";
  143. $config .= " ini_set('display_errors', '1');\n";
  144. $config .= " //error_reporting (E_ALL); // Report everything\n";
  145. $config .= " //error_reporting (E_ALL ^ E_NOTICE); // Report everything\n";
  146. $config .= " error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING ); //hide notices and warnings";
  147. $config .= "\n";
  148. $config .= "?>";
  149. if (is_dir("/etc/fusionpbx")){
  150. $config_path = "/etc/fusionpbx/config.php";
  151. } elseif (is_dir("/usr/local/etc/fusionpbx")){
  152. $config_path = "/usr/local/etc/fusionpbx/config.php";
  153. }
  154. elseif (is_dir($_SERVER['DOCUMENT_ROOT'].PROJECT_PATH."/resources")) {
  155. $config_path = $_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/resources/config.php";
  156. }
  157. else {
  158. $config_path = $_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/resources/config.php";
  159. }
  160. $fout = fopen($config_path,"w");
  161. fwrite($fout, $config);
  162. unset($config);
  163. fclose($fout);
  164. //add the database schema
  165. $output = shell_exec('cd '.$_SERVER["DOCUMENT_ROOT"].' && php /var/www/fusionpbx/core/upgrade/upgrade_schema.php');
  166. //includes - this includes the config.php
  167. require_once "resources/require.php";
  168. //get the domain name
  169. $domain_name = $_SESSION['install']['domain_name'];
  170. //check to see if the domain name exists if it does update the domain_uuid
  171. $sql = "select domain_uuid from v_domains ";
  172. $sql .= "where domain_name = :domain_name ";
  173. $parameters['domain_name'] = $domain_name;
  174. $database = new database;
  175. $domain_uuid = $database->select($sql, $parameters, 'column');
  176. unset($parameters);
  177. //set domain and user_uuid to true or false
  178. if ($domain_uuid == null) {
  179. $domain_uuid = uuid();
  180. $domain_exists = false;
  181. }
  182. else {
  183. $domain_exists = true;
  184. }
  185. //if the domain name does not exist then add the domain name
  186. if (!$domain_exists) {
  187. //add the domain permission
  188. $p = new permissions;
  189. $p->add("domain_add", "temp");
  190. //prepare the array
  191. $array['domains'][0]['domain_uuid'] = $domain_uuid;
  192. $array['domains'][0]['domain_name'] = $domain_name;
  193. $array['domains'][0]['domain_enabled'] = 'true';
  194. //save to the user data
  195. $database = new database;
  196. $database->app_name = 'domains';
  197. $database->app_uuid = 'b31e723a-bf70-670c-a49b-470d2a232f71';
  198. $database->uuid($domain_uuid);
  199. $database->save($array);
  200. $message = $database->message;
  201. unset($array);
  202. //remove the temporary permission
  203. $p->delete("domain_add", "temp");
  204. }
  205. //set the session domain id and name
  206. $_SESSION['domain_uuid'] = $domain_uuid;
  207. $_SESSION['domain_name'] = $domain_name;
  208. //app defaults
  209. $output = shell_exec('cd '.$_SERVER["DOCUMENT_ROOT"].' && php /var/www/fusionpbx/core/upgrade/upgrade_domains.php');
  210. //prepare the user settings
  211. $admin_username = $_SESSION['install']['admin_username'];
  212. $admin_password = $_SESSION['install']['admin_password'];
  213. $user_salt = uuid();
  214. $password_hash = md5($user_salt . $admin_password);
  215. //get the user_uuid if the user exists
  216. $sql = "select user_uuid from v_users ";
  217. $sql .= "where domain_uuid = :domain_uuid ";
  218. $sql .= "and username = :username ";
  219. $parameters['domain_uuid'] = $domain_uuid;
  220. $parameters['username'] = $admin_username;
  221. $database = new database;
  222. $user_uuid = $database->select($sql, $parameters, 'column');
  223. unset($parameters);
  224. //if the user did not exist then get a new uuid
  225. if ($user_uuid == null) {
  226. $domain_exists = false;
  227. $user_uuid = uuid();
  228. }
  229. else {
  230. $user_exists = true;
  231. }
  232. //set the user_uuid
  233. $_SESSION['user_uuid'] = $user_uuid;
  234. //get the superadmin group_uuid
  235. $sql = "select group_uuid from v_groups ";
  236. $sql .= "where group_name = :group_name ";
  237. $parameters['group_name'] = 'superadmin';
  238. $database = new database;
  239. $group_uuid = $database->select($sql, $parameters, 'column');
  240. unset($parameters);
  241. //add the user permission
  242. $p = new permissions;
  243. $p->add("user_add", "temp");
  244. $p->add("user_edit", "temp");
  245. $p->add("user_group_add", "temp");
  246. //save to the user data
  247. $array['users'][0]['domain_uuid'] = $domain_uuid;
  248. $array['users'][0]['user_uuid'] = $user_uuid;
  249. $array['users'][0]['username'] = $admin_username;
  250. $array['users'][0]['password'] = $password_hash;
  251. $array['users'][0]['salt'] = $user_salt;
  252. $array['users'][0]['user_enabled'] = 'true';
  253. $array['user_groups'][0]['user_group_uuid'] = uuid();
  254. $array['user_groups'][0]['domain_uuid'] = $domain_uuid;
  255. $array['user_groups'][0]['group_name'] = 'superadmin';
  256. $array['user_groups'][0]['group_uuid'] = $group_uuid;
  257. $array['user_groups'][0]['user_uuid'] = $user_uuid;
  258. $database = new database;
  259. $database->app_name = 'users';
  260. $database->app_uuid = '112124b3-95c2-5352-7e9d-d14c0b88f207';
  261. $database->uuid($user_uuid);
  262. $database->save($array);
  263. $message = $database->message;
  264. unset($array);
  265. //remove the temporary permission
  266. $p->delete("user_add", "temp");
  267. $p->delete("user_edit", "temp");
  268. $p->delete("user_group_add", "temp");
  269. //copy the files and directories from resources/install
  270. /*
  271. if (!$domain_exists) {
  272. require_once "resources/classes/install.php";
  273. $install = new install;
  274. $install->domain_uuid = $domain_uuid;
  275. $install->domain = $domain_name;
  276. $install->switch_conf_dir = $switch_conf_dir;
  277. $install->copy_conf();
  278. $install->copy();
  279. }
  280. */
  281. //update xml_cdr url, user and password in xml_cdr.conf.xml
  282. if (!$domain_exists) {
  283. if (file_exists($_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/app/xml_cdr")) {
  284. xml_cdr_conf_xml();
  285. }
  286. }
  287. //write the switch.conf.xml file
  288. if (!$domain_exists) {
  289. if (file_exists($switch_conf_dir)) {
  290. switch_conf_xml();
  291. }
  292. }
  293. #app defaults
  294. $output = shell_exec('cd '.$_SERVER["DOCUMENT_ROOT"].' && php /var/www/fusionpbx/core/upgrade/upgrade_domains.php');
  295. //install completed - prompt the user to login
  296. header("Location: /logout.php");
  297. }
  298. }
  299. //set the max execution time to 1 hour
  300. ini_set('max_execution_time',3600);
  301. //set a default template
  302. $_SESSION['domain']['template']['name'] = 'default';
  303. $_SESSION['theme']['menu_brand_image']['text'] = PROJECT_PATH.'/themes/default/images/logo.png';
  304. $_SESSION['theme']['menu_brand_type']['text'] = 'image';
  305. //save an install log if debug is true
  306. //if ($debug) {
  307. // $fp = fopen(sys_get_temp_dir()."/install.log", "w");
  308. //}
  309. //get the domain
  310. $domain_array = explode(":", $_SERVER["HTTP_HOST"]);
  311. $domain_name = $domain_array[0];
  312. //temp directory
  313. $_SESSION['server']['temp']['dir'] = '/tmp';
  314. //initialize a template object
  315. $view = new template();
  316. $view->engine = 'smarty';
  317. $view->template_dir = $_SERVER["DOCUMENT_ROOT"].PROJECT_PATH.'/core/install/resources/views/';
  318. $view->cache_dir = $_SESSION['server']['temp']['dir'];
  319. $view->init();
  320. //assign default values to the template
  321. $view->assign("admin_username", "admin");
  322. $view->assign("admin_password", "");
  323. $view->assign("domain_name", $domain_name);
  324. $view->assign("database_host", "localhost");
  325. $view->assign("database_port", "5432");
  326. $view->assign("database_name", "fusionpbx");
  327. $view->assign("database_username", "fusionpbx");
  328. //add translations
  329. foreach($text as $key => $value) {
  330. $view->assign(str_replace("-", "_", $key), $text[$key]);
  331. //$view->assign("label_username", $text['label-username']);
  332. //$view->assign("label_password", $text['label-password']);
  333. //$view->assign("button_back", $text['button-back']);
  334. }
  335. //debug information
  336. //if ($debug) {
  337. // echo "<pre>\n";
  338. // print_r($text);
  339. // echo "</pre>\n";
  340. //}
  341. //show the views
  342. //if ($_GET["step"] == "" || $_GET["step"] == "1") {
  343. // $content = $view->render('language.htm');
  344. //}
  345. if ($_REQUEST["step"] == "" || $_REQUEST["step"] == "1") {
  346. $content = $view->render('configuration.htm');
  347. }
  348. if ($_REQUEST["step"] == "2") {
  349. $content = $view->render('database.htm');
  350. }
  351. $view->assign("content", $content);
  352. echo $view->render('template.htm');
  353. ?>