install.php 12 KB

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