From 702466ea529fdf6f5c2053d0c9e86de8d0131572 Mon Sep 17 00:00:00 2001 From: Matthew Vale Date: Tue, 17 Nov 2015 17:01:33 +0000 Subject: [PATCH] Initial Rewrite --- core/install/app_config.php | 28 + core/install/app_languages.php | 557 ++++++++++ core/install/app_menu.php | 10 + core/install/index.php | 102 ++ core/install/install_first_time.php | 377 +++++++ .../resources/classes/detect_switch.php | 155 +++ .../resources/classes/install_fusionpbx.php | 992 ++++++++++++++++++ .../resources/classes/install_switch.php | 252 +++++ .../resources/classes/iso_countries.php | 250 +++++ .../page_parts/install_config_database.php | 251 +++++ .../page_parts/install_config_detail.php | 146 +++ .../page_parts/install_event_socket.php | 101 ++ .../page_parts/install_select_language.php | 69 ++ core/install/root.php | 50 + index.php | 2 +- themes/enhanced/app_defaults.php | 93 +- 16 files changed, 3400 insertions(+), 35 deletions(-) create mode 100644 core/install/app_config.php create mode 100644 core/install/app_languages.php create mode 100644 core/install/app_menu.php create mode 100644 core/install/index.php create mode 100644 core/install/install_first_time.php create mode 100644 core/install/resources/classes/detect_switch.php create mode 100644 core/install/resources/classes/install_fusionpbx.php create mode 100644 core/install/resources/classes/install_switch.php create mode 100644 core/install/resources/classes/iso_countries.php create mode 100644 core/install/resources/page_parts/install_config_database.php create mode 100644 core/install/resources/page_parts/install_config_detail.php create mode 100644 core/install/resources/page_parts/install_event_socket.php create mode 100644 core/install/resources/page_parts/install_select_language.php create mode 100644 core/install/root.php diff --git a/core/install/app_config.php b/core/install/app_config.php new file mode 100644 index 0000000000..a65a67c8bc --- /dev/null +++ b/core/install/app_config.php @@ -0,0 +1,28 @@ + \ No newline at end of file diff --git a/core/install/app_languages.php b/core/install/app_languages.php new file mode 100644 index 0000000000..3a48d9625b --- /dev/null +++ b/core/install/app_languages.php @@ -0,0 +1,557 @@ + \ No newline at end of file diff --git a/core/install/app_menu.php b/core/install/app_menu.php new file mode 100644 index 0000000000..e76450bd70 --- /dev/null +++ b/core/install/app_menu.php @@ -0,0 +1,10 @@ + \ No newline at end of file diff --git a/core/install/index.php b/core/install/index.php new file mode 100644 index 0000000000..9011dd9574 --- /dev/null +++ b/core/install/index.php @@ -0,0 +1,102 @@ + + Portions created by the Initial Developer are Copyright (C) 2008-2012 + the Initial Developer. All Rights Reserved. + + Contributor(s): + Mark J Crane + Matthew Vale +*/ +require_once "root.php"; +require_once "resources/require.php"; +require_once "resources/check_auth.php"; + +//detect install state + +$first_time_install = true; +if (file_exists($_SERVER['DOCUMENT_ROOT'].PROJECT_PATH."/resources/config.php")) { + $first_time_install = false; +} elseif (file_exists("/etc/fusionpbx/config.php")) { + //linux + $first_time_install = false; +} elseif (file_exists("/usr/local/etc/fusionpbx/config.php")) { + $first_time_install = false; +} + +if($first_time_install) { + header("Location: ".PROJECT_PATH."/core/install/install_first_time.php"); + exit; +} +require_once "resources/check_auth.php"; +if (!if_group("superadmin")) { + echo "access denied"; + exit; +} + +//add multi-lingual support + $language = new text; + $text = $language->get(); + +//includes and title + require_once "resources/header.php"; + $document['title'] = $text['title-install']; + + echo "".$text['header-install'].""; + echo "

"; + echo $text['description-install']; + echo "

"; + + echo "
\n"; + echo "\n"; + echo "\n"; + echo " \n"; + echo " \n"; + echo "\n"; + echo "
\n"; + echo " "; + echo " \n"; + echo " \n"; + echo "
\n"; + echo "
\n"; + + echo "
\n"; + echo "\n"; + echo "\n"; + echo " \n"; + echo " \n"; + echo "\n"; + echo "
\n"; + echo " "; + echo " \n"; + echo " \n"; + echo "
\n"; + echo "
\n"; + + echo "

"; + + echo "

WiP

"; + +//include the footer + require_once "resources/footer.php"; +?> \ No newline at end of file diff --git a/core/install/install_first_time.php b/core/install/install_first_time.php new file mode 100644 index 0000000000..18e4d28c3e --- /dev/null +++ b/core/install/install_first_time.php @@ -0,0 +1,377 @@ + + Portions created by the Initial Developer are Copyright (C) 2008-2012 + the Initial Developer. All Rights Reserved. + + Contributor(s): + Mark J Crane + Matthew Vale +*/ +require_once "root.php"; +require_once "resources/functions.php"; +require_once "resources/classes/text.php"; + +//initialize varibles we are going to use + $event_host = ''; + $event_port = ''; + $event_password = ''; + $install_language = 'en-us'; + $admin_username = ''; + $admin_password = ''; + $install_default_country = 'US'; + $install_template_name = ''; + $domain_name = ''; + $db_type = ''; + $db_path = ''; + $db_host = ''; + $db_port = ''; + $db_name = ''; + $db_username = ''; + $db_password = ''; + $db_create_username = ''; + $db_create_password = ''; + +//detect the iso country code from the locale +//TBD $locale = Locale::getDefault(); +$timezone = 'UTC'; +if (is_link('/etc/localtime')) { + // Mac OS X (and older Linuxes) + // /etc/localtime is a symlink to the + // timezone in /usr/share/zoneinfo. + $filename = readlink('/etc/localtime'); + if (strpos($filename, '/usr/share/zoneinfo/') === 0) { + $timezone = substr($filename, 20); + } +} elseif (file_exists('/etc/timezone')) { + // Ubuntu / Debian. + $data = file_get_contents('/etc/timezone'); + if ($data) { + $timezone = $data; + } +} elseif (file_exists('/etc/sysconfig/clock')) { + // RHEL / CentOS + $data = parse_ini_file('/etc/sysconfig/clock'); + if (!empty($data['ZONE'])) { + $timezone = $data['ZONE']; + } +} + +date_default_timezone_set($timezone); + +//detect install state +$first_time_install = true; +if (file_exists($_SERVER['DOCUMENT_ROOT'].PROJECT_PATH."/resources/config.php")) { + $first_time_install = false; +} elseif (file_exists("/etc/fusionpbx/config.php")) { + //linux + $first_time_install = false; +} elseif (file_exists("/usr/local/etc/fusionpbx/config.php")) { + $first_time_install = false; +} + +if(!$first_time_install) { + require_once "resources/require.php"; + require_once "resources/check_auth.php"; + if (!if_group("superadmin")) { + echo "access denied"; + exit; + } +} + +$install_step = ''; +$return_install_step = ''; + +if (count($_POST)>0) { + $install_language = check_str($_POST["install_language"]); + $install_step = check_str($_POST["install_step"]); + $return_install_step = check_str($_POST["return_install_step"]); + if(isset($_POST["event_host"])){ + $event_host = check_str($_POST["event_host"]); + $event_port = check_str($_POST["event_port"]); + $event_password = check_str($_POST["event_password"]); + } + if(isset($_POST["db_type"])){ + $db_type = $_POST["db_type"]; + $admin_username = $_POST["admin_username"]; + $admin_password = $_POST["admin_password"]; + $install_default_country = $_POST["install_default_country"]; + $install_template_name = $_POST["install_template_name"]; + $domain_name = $_POST["domain_name"]; + } +} + +if(!$install_step) { $install_step = 'select_language'; } + + $_SESSION['domain']['language']['code'] = $install_language; + +//add multi-lingual support + $language = new text; + $text = $language->get(); + +//set a default template + $default_template = 'enhanced'; + if (isset($_SESSION['domain']['template']['name']) and strlen($_SESSION['domain']['template']['name']) != 0) { + $default_template = $_SESSION['domain']['template']['name']; + } + +//set a default enviroment if first_time + if($first_time_install){ + //grab the default theme + $set_session_theme = 1; + $domains_processed = 1; + include "themes/enhanced/app_defaults.php"; + unset($set_session_theme, $domains_processed); + //initialize some defaults so we can be 'logged in' + $_SESSION['username'] = 'first_time_install'; + $_SESSION['permissions'][]['permission_name'] = 'superadmin'; + $_SESSION['menu'] = ''; + //initialize some varibles to cut down on warnings + $_SESSION['message'] = ''; + $v_link_label_play = ''; + $v_link_label_pause = ''; + $default_login = 0; + $footer = ''; + $onload = ''; + } + +//get the contents of the template and save it to the template variable + $template = file_get_contents($_SERVER["DOCUMENT_ROOT"].PROJECT_PATH.'/themes/'.$default_template.'/template.php'); + +//buffer the content + ob_end_clean(); //clean the buffer + ob_start(); + + $messages = array(); + if (!extension_loaded('PDO')) { + $messages[] = "PHP PDO was not detected. Please install it before proceeding"; + } + + echo "
\n"; + $msg = ''; + //make sure the includes directory is writable so the config.php file can be written. + if (!is_writable($_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/resources/pdo.php")) { + $messages[] = "Write access to ".$_SERVER["DOCUMENT_ROOT"].PROJECT_PATH." and its sub-directories are required during the install."; + } + //test for selinux + if (file_exists('/usr/sbin/getenforce')) { + $enforcing; + exec('getenforce', $enforcing); + if($enforcing[0] == 'Enforcing'){ + $messages[] = "SELinux is enabled and enforcing you must have a policy installed to let the webserver connect to the switch event socket
". + "You can use the following to find what ports are allowed
semanage port -l | grep '^http_port_t'
"; + } + } + + //action code + if($return_install_step == 'config_detail'){ + //check for all required data + $existing_errors = count($messages); + if (strlen($admin_username) == 0) { $messages[] = "Please provide the Admin Username"; } + if (strlen($admin_password) == 0) { $messages[] = "Please provide the Admin Password"; } + elseif (strlen($admin_password) < 5) { $messages[] = "Please provide an Admin Password that is 5 or more characters.
\n"; } + if ( count($messages) > $existing_errors) { $install_step = 'config_detail'; } + } + + if($install_step =='execute') + { + //start the rocket launch! + + //set the max execution time to 1 hour + ini_set('max_execution_time',3600); + + } + + + + //display messages + if (count($messages)>0) { + echo "
\n"; + echo "
\n"; + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + echo "
Messages
    \n"; + foreach ($messages as $message){ + echo "
  • $message
  • \n"; + } + echo "
\n"; + echo "
\n"; + } + +//includes and title + $document['title'] = $text['title-install_first_time']; + + //view code + if($install_step == 'select_language'){ + include "resources/page_parts/install_select_language.php"; + echo "
\n"; + echo " \n"; + echo " \n"; + echo " \n"; + echo "
\n"; + echo " \n"; + echo "
\n"; + echo "
\n"; + }elseif($install_step == 'detect_config'){ + include "resources/page_parts/install_event_socket.php"; + if($detect_ok){ + echo "
\n"; + echo " \n"; + echo " \n"; + echo " \n"; + echo " \n"; + echo " \n"; + echo " \n"; + echo "
\n"; + echo " \n"; + echo " \n"; + echo "
\n"; + echo "
\n"; + } + } + elseif($install_step == 'config_detail'){ + if(!$domain_name){ + //get the domain + $domain_array = explode(":", $_SERVER["HTTP_HOST"]); + $domain_name = $domain_array[0]; + } + include "resources/page_parts/install_config_detail.php"; + } + elseif($install_step == 'config_database'){ + include "resources/page_parts/install_config_database.php"; + } + elseif($install_step == 'execute'){ + echo "

".$text['header-installing']."

\n"; + //$protocol = 'http'; + //if($_SERVER['HTTPS']) { $protocol = 'https'; } + //echo ""; + require_once "core/install/resources/classes/detect_switch.php"; + $switch_detect = new detect_switch($event_host, $event_port, $event_password); + $detect_ok = true; + try { + $switch_detect->detect(); + } catch(Exception $e){ + echo "

Failed to detect confgiuration detect_switch reported: " . $e->getMessage() . "

\n"; + $detect_ok = false; + } + if($detect_ok){ + $install_ok = true; + echo "
\n";
+			function error_handler($err_severity, $errstr, $errfile, $errline ) {
+				if (0 === error_reporting()) { return false;}
+				switch($err_severity)
+				{
+					case E_ERROR:               throw new Exception ($errstr . " in $errfile line: $errline");
+					case E_PARSE:               throw new Exception ($errstr . " in $errfile line: $errline");
+					case E_CORE_ERROR:          throw new Exception ($errstr . " in $errfile line: $errline");
+					case E_COMPILE_ERROR:       throw new Exception ($errstr . " in $errfile line: $errline");
+					case E_USER_ERROR:          throw new Exception ($errstr . " in $errfile line: $errline");
+					case E_STRICT:              throw new Exception ($errstr . " in $errfile line: $errline");
+					case E_RECOVERABLE_ERROR:   throw new Exception ($errstr . " in $errfile line: $errline");
+					default: 					return false;
+				}
+			}
+			set_error_handler("error_handler");
+			try {
+				$domain_uuid = uuid();
+				require_once "resources/classes/install_fusionpbx.php";
+				$fusionPBX = new install_fusionpbx($domain_name, $domain_uuid, $switch_detect);
+				$fusionPBX->debug = true;
+				$fusionPBX->admin_username = $admin_username;
+				$fusionPBX->admin_password = $admin_password;
+				$fusionPBX->default_country = $install_default_country;
+				$fusionPBX->install_language = $install_language;
+				$fusionPBX->template_name = $install_template_name;
+				foreach($_POST as $key=>$value){
+					if(substr($key,0,3) == "db_"){
+						$fusionPBX->$key = $value;
+					}
+				}
+				$fusionPBX->install();
+		
+				require_once "resources/classes/install_switch.php";
+				$switch = new install_switch($domain_name, $domain_uuid, $switch_detect);
+				$switch->debug = true;
+				$switch->install();
+			}catch(Exception $e){
+				echo "
\n"; + echo "

Failed to install
" . $e->getMessage() . "

\n"; + try { + require_once "resources/classes/install_fusionpbx.php"; + $fusionPBX = new install_fusionpbx($domain_name, $domain_uuid, $switch_detect); + $fusionPBX->remove_config(); + }catch(Exception $e){ + echo "

Failed to remove config: " . $e->getMessage() . "

\n"; + } + $install_ok = false; + } + restore_error_handler(); + if($install_ok){ + echo "\n"; + header("Location: ".PROJECT_PATH."/logout.php"); + $_SESSION['message'] = 'Install complete'; + }else{ + echo "
\n"; + echo "
\n"; + echo " \n"; + echo " \n"; + echo "
\n"; + echo "
\n"; + } + } + }else{ + echo "

Unkown install_step '$install_step'

\n"; + } + +// add the content to the template and then send output + $body = ob_get_contents(); //get the output from the buffer + ob_end_clean(); //clean the buffer + + //replace known used constants + $body = str_replace ("", PROJECT_PATH, $body); //defined in /resources/menu.php + + ob_start(); + eval('?>' . $template . '", $document['title'], $template); // defined in each individual page + $output = str_replace ("", $custom_head, $output); // defined in each individual page + $output = str_replace ("", $_SESSION["menu"], $output); //defined in /resources/menu.php + $output = str_replace ("", PROJECT_PATH, $output); //defined in /resources/menu.php + + $pos = strrpos($output, ""); + if ($pos === false) { + $output = $body; //if tag not found just show the body + } + else { + //replace the body + $output = str_replace ("", $body, $output); + } + + echo $output; + unset($output); + +?> \ No newline at end of file diff --git a/core/install/resources/classes/detect_switch.php b/core/install/resources/classes/detect_switch.php new file mode 100644 index 0000000000..9d3dd3775c --- /dev/null +++ b/core/install/resources/classes/detect_switch.php @@ -0,0 +1,155 @@ + + Copyright (C) 2010-2015 + All Rights Reserved. + + Contributor(s): + Matthew Vale + +*/ +require_once "root.php"; +require_once "resources/classes/EventSocket.php"; + +//define the install class + class detect_switch { + + // version information + protected $_major; + protected $_minor; + + // folders + protected $_base_dir = ''; + protected $_cache_dir = ''; + protected $_certs_dir = ''; + protected $_conf_dir = ''; + protected $_db_dir = ''; + protected $_external_ssl_dir = ''; + protected $_grammar_dir = ''; + protected $_htdocs_dir = ''; + protected $_internal_ssl_dir = ''; + protected $_log_dir = ''; + protected $_mod_dir = ''; + protected $_recordings_dir = ''; + protected $_run_dir = ''; + protected $_script_dir = ''; + protected $_sounds_dir = ''; + protected $_storage_dir = ''; + protected $_temp_dir = ''; + + function major() { return $this->_major; } + function minor() { return $this->_minor; } + function base_dir() { return $this->_base_dir; } + function cache_dir() { return $this->_cache_dir; } + function certs_dir() { return $this->_certs_dir; } + function conf_dir() { return $this->_conf_dir; } + function db_dir() { return $this->_db_dir; } + function external_ssl_dir() { return $this->_external_ssl_dir; } + function grammar_dir() { return $this->_grammar_dir; } + function htdocs_dir() { return $this->_htdocs_dir; } + function internal_ssl_dir() { return $this->_internal_ssl_dir; } + function log_dir() { return $this->_log_dir; } + function mod_dir() { return $this->_mod_dir; } + function recordings_dir() { return $this->_recordings_dir; } + function run_dir() { return $this->_run_dir; } + function script_dir() { return $this->_script_dir; } + function sounds_dir() { return $this->_sounds_dir; } + function storage_dir() { return $this->_storage_dir; } + function temp_dir() { return $this->_temp_dir; } + + // event socket + public $event_host = 'localhost'; + public $event_port = '8021'; + public $event_password = 'ClueCon'; + protected $event_socket; + + public function __construct($event_host, $event_port, $event_password) { + if($event_host){ $this->event_host = $event_host; } + if($event_port){ $this->event_port = $event_port; } + if($event_password){ $this->event_password = $event_password; } + $this->connect_event_socket(); + if(!$this->event_socket){ + $this->detect_event_socket(); + } + } + + protected function detect_event_socket() { + //perform searches for user's config here + } + + public function detect() { + $this->connect_event_socket(); + if(!$this->event_socket){ + throw new Exception('Failed to use event socket'); + } + $FS_Version = $this->event_socket_request('api version'); + preg_match("/FreeSWITCH Version (\d+)\.(\d(?:\.\d+)?)/", $FS_Version, $matches); + $this->_major = $matches[1]; + $this->_minor = $matches[2]; + $FS_Vars = $this->event_socket_request('api global_getvar'); + foreach (explode("\n",$FS_Vars) as $FS_Var){ + preg_match("/(\w+_dir)=(.*)/", $FS_Var, $matches); + if(count($matches) > 0 and property_exists($this, "_" . $matches[1])){ + $field = "_" . $matches[1]; + $this->$field = $matches[2]; + } + } + } + + public function show_config() { + $buffer = ''; + $buffer .= "FreeSWITCH Verson => " . $this->_major . "." . $this->_minor . "\n"; + $buffer .= "base_dir => " . $this->_base_dir ."\n"; + $buffer .= "cache_dir => " . $this->_cache_dir ."\n"; + $buffer .= "certs_dir => " . $this->_certs_dir ."\n"; + $buffer .= "conf_dir => " . $this->_conf_dir ."\n"; + $buffer .= "db_dir => " . $this->_db_dir ."\n"; + $buffer .= "external_ssl_dir => " . $this->_external_ssl_dir ."\n"; + $buffer .= "grammar_dir => " . $this->_grammar_dir ."\n"; + $buffer .= "htdocs_dir => " . $this->_htdocs_dir ."\n"; + $buffer .= "internal_ssl_dir => " . $this->_internal_ssl_dir ."\n"; + $buffer .= "log_dir => " . $this->_log_dir ."\n"; + $buffer .= "mod_dir => " . $this->_mod_dir ."\n"; + $buffer .= "recordings_dir => " . $this->_recordings_dir ."\n"; + $buffer .= "run_dir => " . $this->_run_dir ."\n"; + $buffer .= "script_dir => " . $this->_script_dir ."\n"; + $buffer .= "sounds_dir => " . $this->_sounds_dir ."\n"; + $buffer .= "storage_dir => " . $this->_storage_dir ."\n"; + $buffer .= "temp_dir => " . $this->_temp_dir ."\n"; + return $buffer; + } + + protected function connect_event_socket(){ + $esl = new EventSocket; + if ($esl->connect($this->event_host, $this->event_port, $this->event_password)) { + $this->event_socket = $esl->reset_fp(); + return true; + } + return false; + } + + protected function event_socket_request($cmd) { + $esl = new EventSocket($this->event_socket); + $result = $esl->request($cmd); + $esl->reset_fp(); + return $result; + } + + } +?> \ No newline at end of file diff --git a/core/install/resources/classes/install_fusionpbx.php b/core/install/resources/classes/install_fusionpbx.php new file mode 100644 index 0000000000..0d4e74d75e --- /dev/null +++ b/core/install/resources/classes/install_fusionpbx.php @@ -0,0 +1,992 @@ + + Copyright (C) 2010-2015 + All Rights Reserved. + + Contributor(s): + Mark J Crane + Matthew Vale +*/ +include "root.php"; + +//define the install class + class install_fusionpbx { + + protected $domain_uuid; + protected $domain_name; + protected $detect_switch; + protected $config_php; + protected $menu_uuid = 'b4750c3f-2a86-b00d-b7d0-345c14eca286'; + protected $dbh; + + public $debug = false; + + public $install_msg; + public $install_language = 'en-us'; + public $admin_username; + public $admin_password; + public $default_country = 'US'; + public $template_name = 'enhanced'; + + public $db_type; + public $db_path; + public $db_host; + public $db_port; + public $db_name; + public $db_username; + public $db_password; + + function __construct($domain_name, $domain_uuid, $detect_switch) { + if(!is_a($detect_switch, 'detect_switch')){ + throw new Exception('The parameter $detect_switch must be a detect_switch object (or a subclass of)'); + } + $this->domain_uuid = $domain_uuid; + $this->domain_name = $domain_name; + $this->detect_switch = $detect_switch; + if (is_dir("/etc/fusionpbx")){ + $this->config_php = "/etc/fusionpbx/config.php"; + } elseif (is_dir("/usr/local/etc/fusionpbx")){ + $this->config_php = "/usr/local/etc/fusionpbx/config.php"; + } + elseif (is_dir($_SERVER['DOCUMENT_ROOT'].PROJECT_PATH."/resources")) { + $this->config_php = $_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/resources/config.php"; + } + else { + $this->config_php = $_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/resources/config.php"; + } + } + + function write_debug($message) { + if($this->debug){ + echo "$message\n"; + } + } + + function write_progress($message) { + echo "$message\n"; + } + + function install() { + ini_set('max_execution_time',3600); + $this->create_config_php(); + $this->create_database(); + $this->create_domain(); + $this->create_superuser(); + require "resources/require.php"; + $this->create_menus(); + $this->post_create(); + } + + protected function create_config_php() { + $tmp_config = "db_type."'; //sqlite, mysql, pgsql, others with a manually created PDO connection\n"; + $tmp_config .= "\n"; + if ($this->db_type == "sqlite") { + $tmp_config .= " //sqlite: the db_name and db_path are automatically assigned however the values can be overidden by setting the values here.\n"; + $tmp_config .= " \$db_name = '".$this->db_name."'; //host name/ip address + '.db' is the default database filename\n"; + $tmp_config .= " \$db_path = '".$this->db_path."'; //the path is determined by a php variable\n"; + } + $tmp_config .= "\n"; + $tmp_config .= " //mysql: database connection information\n"; + if ($this->db_type == "mysql") { + if ($this->db_host == "localhost") { + //if localhost is used it defaults to a Unix Socket which doesn't seem to work. + //replace localhost with 127.0.0.1 so that it will connect using TCP + $this->db_host = "127.0.0.1"; + } + $tmp_config .= " \$db_host = '".$this->db_host."';\n"; + $tmp_config .= " \$db_port = '".$this->db_port."';\n"; + $tmp_config .= " \$db_name = '".$this->db_name."';\n"; + $tmp_config .= " \$db_username = '".$this->db_username."';\n"; + $tmp_config .= " \$db_password = '".$this->db_password."';\n"; + } + else { + $tmp_config .= " //\$db_host = '';\n"; + $tmp_config .= " //\$db_port = '';\n"; + $tmp_config .= " //\$db_name = '';\n"; + $tmp_config .= " //\$db_username = '';\n"; + $tmp_config .= " //\$db_password = '';\n"; + } + $tmp_config .= "\n"; + $tmp_config .= " //pgsql: database connection information\n"; + if ($this->db_type == "pgsql") { + $tmp_config .= " \$db_host = '".$this->db_host."'; //set the host only if the database is not local\n"; + $tmp_config .= " \$db_port = '".$this->db_port."';\n"; + $tmp_config .= " \$db_name = '".$this->db_name."';\n"; + $tmp_config .= " \$db_username = '".$this->db_username."';\n"; + $tmp_config .= " \$db_password = '".$this->db_password."';\n"; + } + else { + $tmp_config .= " //\$db_host = '".$this->db_host."'; //set the host only if the database is not local\n"; + $tmp_config .= " //\$db_port = '".$this->db_port."';\n"; + $tmp_config .= " //\$db_name = '".$this->db_name."';\n"; + $tmp_config .= " //\$db_username = '".$this->db_username."';\n"; + $tmp_config .= " //\$db_password = '".$this->db_password."';\n"; + } + $tmp_config .= "\n"; + $tmp_config .= " //show errors\n"; + $tmp_config .= " ini_set('display_errors', '1');\n"; + $tmp_config .= " //error_reporting (E_ALL); // Report everything\n"; + $tmp_config .= " //error_reporting (E_ALL ^ E_NOTICE); // Report everything\n"; + $tmp_config .= " error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING ); //hide notices and warnings"; + $tmp_config .= "\n"; + $tmp_config .= "?>"; + + if((file_exists($this->config_php) and !is_writable($this->config_php)) + or !is_writable(dirname($this->config_php)) + ){ + throw new Exception("cannot write to '" . $this->config_php . "'" ); + } + $this->write_progress("Creating " . $this->config_php); + $fout = fopen($this->config_php,"w"); + fwrite($fout, $tmp_config); + unset($tmp_config); + fclose($fout); + } + + protected function create_database() { + require $this->config_php; + $this->write_progress("creating database as " . $this->db_type); + $function = "create_database_" . $this->db_type; + $this->$function(); + global $db; + $db = $this->dbh; + } + protected function create_database_sqlite() { + //sqlite database will be created when the config.php is loaded and only if the database file does not exist + try { + $this->dbh = new PDO('sqlite:'.$this->db_path.'/'.$this->db_name); //sqlite 3 + //$this->dbh = new PDO('sqlite::memory:'); //sqlite 3 + } + catch (PDOException $error) { + throw Exception("Failed to create database: " . $error->getMessage()); + } + + //add additional functions to SQLite - bool PDO::sqliteCreateFunction ( string function_name, callback callback [, int num_args] ) + if (!function_exists('php_now')) { + function php_now() { + if(function_exists("date_default_timezone_set") and function_exists("date_default_timezone_get")) { + @date_default_timezone_set(@date_default_timezone_get()); + } + return date("Y-m-d H:i:s"); + } + } + $this->dbh->sqliteCreateFunction('now', 'php_now', 0); + + //add the database structure + require_once "resources/classes/schema.php"; + $schema = new schema; + $schema->db = $this->dbh; + $schema->db_type = $this->db_type; + $schema->sql(); + $schema->exec(); + + //get the contents of the sql file + if (file_exists('/usr/share/examples/fusionpbx/resources/install/sql/sqlite.sql')){ + $filename = "/usr/share/examples/fusionpbx/resources/install/sql/sqlite.sql"; + } + else { + $filename = $_SERVER["DOCUMENT_ROOT"].PROJECT_PATH.'/resources/install/sql/sqlite.sql'; + } + $file_contents = file_get_contents($filename); + unset($filename); + + //replace \r\n with \n then explode on \n + $file_contents = str_replace("\r\n", "\n", $file_contents); + + //loop line by line through all the lines of sql code + $this->dbh->beginTransaction(); + $string_array = explode("\n", $file_contents); + $x = 0; + foreach($string_array as $sql) { + try { + $this->dbh->query($sql); + } + catch (PDOException $error) { + throw new Exception("error creating database: " . $error->getMessage() . "\n" . $sql ); + } + $x++; + } + unset ($file_contents, $sql); + $this->dbh->commit(); + + //set the file permissions + chmod($this->db_path.'/'.$this->db_name, 0777); + } + + protected function create_database_pgsql() { + + //if $this->db_create_username provided, attempt to create new PG role and database + if (strlen($this->db_create_username) > 0) { + try { + if (strlen($this->db_port) == 0) { $this->db_port = "5432"; } + if (strlen($this->db_host) > 0) { + $this->dbh = new PDO("pgsql:host={$this->db_host} port={$this->db_port} user={".$this->db_create_username."} password={".$this->db_create_password."} dbname=template1"); + } else { + $this->dbh = new PDO("pgsql:host=localhost port={$this->db_port} user={".$this->db_create_username."} password={".$this->db_create_password."} dbname=template1"); + } + } catch (PDOException $error) { + throw new Exception("error connecting to database: " . $error->getMessage()); + } + + //create the database, user, grant perms + $this->dbh->exec("CREATE DATABASE {$this->db_name}"); + $this->dbh->exec("CREATE USER {$this->db_username} WITH PASSWORD '{$this->db_password}'"); + $this->dbh->exec("GRANT ALL ON {$this->db_name} TO {$this->db_username}"); + + //close database connection_aborted + $this->dbh = null; + } + + //open database connection with $this->db_name + try { + if (strlen($this->db_port) == 0) { $this->db_port = "5432"; } + if (strlen($this->db_host) > 0) { + $this->dbh = new PDO("pgsql:host={$this->db_host} port={$this->db_port} dbname={$this->db_name} user={$this->db_username} password={$this->db_password}"); + } else { + $this->dbh = new PDO("pgsql:host=localhost port={$this->db_port} user={$this->db_username} password={$this->db_password} dbname={$this->db_name}"); + } + } + catch (PDOException $error) { + throw new Exception("error connecting to database: " . $error->getMessage()); + } + + //add the database structure + require_once "resources/classes/schema.php"; + $schema = new schema; + $schema->db = $this->dbh; + $schema->db_type = $this->db_type; + $schema->sql(); + $schema->exec(); + + //get the contents of the sql file + if (file_exists('/usr/share/examples/fusionpbx/resources/install/sql/pgsql.sql')){ + $filename = "/usr/share/examples/fusionpbx/resources/install/sql/pgsql.sql"; + } + else { + $filename = $_SERVER["DOCUMENT_ROOT"].PROJECT_PATH.'/resources/install/sql/pgsql.sql'; + } + $file_contents = file_get_contents($filename); + + //replace \r\n with \n then explode on \n + $file_contents = str_replace("\r\n", "\n", $file_contents); + + //loop line by line through all the lines of sql code + $string_array = explode("\n", $file_contents); + $x = 0; + foreach($string_array as $sql) { + if (strlen($sql) > 3) { + try { + $this->dbh->query($sql); + } + catch (PDOException $error) { + throw new Exception("error creating database: " . $error->getMessage() . "\n" . $sql ); + } + } + $x++; + } + unset ($file_contents, $sql); + } + + protected function create_database_mysql() { + //database connection + try { + if (strlen($this->db_host) == 0 && strlen($this->db_port) == 0) { + //if both host and port are empty use the unix socket + if (strlen($this->db_create_username) == 0) { + $this->dbh = new PDO("mysql:host=$this->db_host;unix_socket=/var/run/mysqld/mysqld.sock;", $this->db_username, $this->db_password, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8')); + } + else { + $this->dbh = new PDO("mysql:host=$this->db_host;unix_socket=/var/run/mysqld/mysqld.sock;", $this->db_create_username, $this->db_create_password, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8')); + } + } + else { + if (strlen($this->db_port) == 0) { + //leave out port if it is empty + if (strlen($this->db_create_username) == 0) { + $this->dbh = new PDO("mysql:host=$this->db_host;", $this->db_username, $this->db_password, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8')); + } + else { + $this->dbh = new PDO("mysql:host=$this->db_host;", $this->db_create_username, $this->db_create_password, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8')); } + } + else { + if (strlen($this->db_create_username) == 0) { + $this->dbh = new PDO("mysql:host=$this->db_host;port=$this->db_port;", $this->db_username, $this->db_password, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8')); + } + else { + $this->dbh = new PDO("mysql:host=$this->db_host;port=$this->db_port;", $this->db_create_username, $this->db_create_password, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8')); + } + } + } + $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + $this->dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); + } + catch (PDOException $error) { + throw new Exception("error creating database: " . $error->getMessage() . "\n" . $sql ); + } + + //create the table, user and set the permissions only if the db_create_username was provided + if (strlen($this->db_create_username) > 0) { + //select the mysql database + try { + $this->dbh->query("USE mysql;"); + } + catch (PDOException $error) { + if ($this->debug) { + throw new Exception("error conencting to database: " . $error->getMessage()); + } + } + + //create user and set the permissions + try { + $tmp_sql = "CREATE USER '".$this->db_username."'@'%' IDENTIFIED BY '".$this->db_password."'; "; + $this->dbh->query($tmp_sql); + } + catch (PDOException $error) { + if ($this->debug) { + print "error: " . $error->getMessage() . "
"; + } + } + + //set account to unlimited use + try { + if ($this->db_host == "localhost" || $this->db_host == "127.0.0.1") { + $tmp_sql = "GRANT USAGE ON * . * TO '".$this->db_username."'@'localhost' "; + $tmp_sql .= "IDENTIFIED BY '".$this->db_password."' "; + $tmp_sql .= "WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0; "; + $this->dbh->query($tmp_sql); + + $tmp_sql = "GRANT USAGE ON * . * TO '".$this->db_username."'@'127.0.0.1' "; + $tmp_sql .= "IDENTIFIED BY '".$this->db_password."' "; + $tmp_sql .= "WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0; "; + $this->dbh->query($tmp_sql); + } + else { + $tmp_sql = "GRANT USAGE ON * . * TO '".$this->db_username."'@'".$this->db_host."' "; + $tmp_sql .= "IDENTIFIED BY '".$this->db_password."' "; + $tmp_sql .= "WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0; "; + $this->dbh->query($tmp_sql); + } + } + catch (PDOException $error) { + if ($this->debug) { + print "error: " . $error->getMessage() . "
"; + } + } + + //create the database and set the create user with permissions + try { + $tmp_sql = "CREATE DATABASE IF NOT EXISTS ".$this->db_name."; "; + $this->dbh->query($tmp_sql); + } + catch (PDOException $error) { + if ($this->debug) { + print "error: " . $error->getMessage() . "
"; + } + } + + //set user permissions + try { + $this->dbh->query("GRANT ALL PRIVILEGES ON ".$this->db_name.".* TO '".$this->db_username."'@'%'; "); + } + catch (PDOException $error) { + if ($this->debug) { + print "error: " . $error->getMessage() . "
"; + } + } + + //make the changes active + try { + $tmp_sql = "FLUSH PRIVILEGES; "; + $this->dbh->query($tmp_sql); + } + catch (PDOException $error) { + if ($this->debug) { + print "error: " . $error->getMessage() . "
"; + } + } + + } //if (strlen($this->db_create_username) > 0) + + //select the database + try { + $this->dbh->query("USE ".$this->db_name.";"); + } + catch (PDOException $error) { + if ($this->debug) { + print "error: " . $error->getMessage() . "
"; + } + } + + //add the database structure + require_once "resources/classes/schema.php"; + $schema = new schema; + $schema->db = $this->dbh; + $schema->db_type = $this->db_type; + $schema->sql(); + $schema->exec(); + + //add the defaults data into the database + //get the contents of the sql file + if (file_exists('/usr/share/examples/fusionpbx/resources/install/sql/mysql.sql')){ + $filename = "/usr/share/examples/fusionpbx/resources/install/sql/mysql.sql"; + } + else { + $filename = $_SERVER["DOCUMENT_ROOT"].PROJECT_PATH.'/resources/install/sql/mysql.sql'; + } + $file_contents = file_get_contents($filename); + + //replace \r\n with \n then explode on \n + $file_contents = str_replace("\r\n", "\n", $file_contents); + + //loop line by line through all the lines of sql code + $string_array = explode("\n", $file_contents); + $x = 0; + foreach($string_array as $sql) { + if (strlen($sql) > 3) { + try { + if ($this->debug) { + $this->write_debug( $sql."\n"); + } + $this->dbh->query($sql); + } + catch (PDOException $error) { + //echo "error on line $x: " . $error->getMessage() . " sql: $sql
"; + //die(); + } + } + $x++; + } + unset ($file_contents, $sql); + } + + + protected function create_domain() { + $this->write_progress("checking if domain exists '" . $this->domain_name . "'"); + $sql = "select count(*) from v_domains "; + $sql .= "where domain_name = '".$this->domain_name."' "; + + $this->write_debug($sql); + $prep_statement = $this->dbh->prepare(check_sql($sql)); + $prep_statement->execute(); + $row = $prep_statement->fetch(PDO::FETCH_ASSOC); + unset($sql, $prep_statement); + if ($row['num_rows'] == 0) { + $this->write_progress("creating domain '" . $this->domain_name . "'"); + $sql = "insert into v_domains "; + $sql .= "("; + $sql .= "domain_uuid, "; + $sql .= "domain_name, "; + $sql .= "domain_description "; + $sql .= ") "; + $sql .= "values "; + $sql .= "("; + $sql .= "'".$this->domain_uuid."', "; + $sql .= "'".$this->domain_name."', "; + $sql .= "'' "; + $sql .= ");"; + + $this->write_debug($sql); + $this->dbh->exec(check_sql($sql)); + unset($sql); + + //domain settings + $x = 0; + $tmp[$x]['name'] = 'uuid'; + $tmp[$x]['value'] = $this->menu_uuid; + $tmp[$x]['category'] = 'domain'; + $tmp[$x]['subcategory'] = 'menu'; + $tmp[$x]['enabled'] = 'true'; + $x++; + $tmp[$x]['name'] = 'name'; + $tmp[$x]['category'] = 'domain'; + $tmp[$x]['subcategory'] = 'time_zone'; + $tmp[$x]['enabled'] = 'true'; + $x++; + $tmp[$x]['name'] = 'code'; + $tmp[$x]['value'] = 'en-us'; + $tmp[$x]['category'] = 'domain'; + $tmp[$x]['subcategory'] = 'language'; + $tmp[$x]['enabled'] = 'true'; + $x++; + $tmp[$x]['name'] = 'iso_code'; + $tmp[$x]['value'] = $this->default_country; + $tmp[$x]['category'] = 'domain'; + $tmp[$x]['subcategory'] = 'country'; + $tmp[$x]['enabled'] = 'true'; + $x++; + $tmp[$x]['name'] = 'name'; + $tmp[$x]['value'] = $this->template_name; + $tmp[$x]['category'] = 'domain'; + $tmp[$x]['subcategory'] = 'template'; + $tmp[$x]['enabled'] = 'true'; + $x++; + + //switch settings + $tmp[$x]['name'] = 'dir'; + $tmp[$x]['value'] = $switch_bin_dir; + $tmp[$x]['category'] = 'switch'; + $tmp[$x]['subcategory'] = 'bin'; + $tmp[$x]['enabled'] = 'true'; + $x++; + $tmp[$x]['name'] = 'dir'; + $tmp[$x]['value'] = $this->detect_switch->base_dir(); + $tmp[$x]['category'] = 'switch'; + $tmp[$x]['subcategory'] = 'base'; + $tmp[$x]['enabled'] = 'true'; + $x++; + $tmp[$x]['name'] = 'dir'; + $tmp[$x]['value'] = $this->detect_switch->conf_dir(); + $tmp[$x]['category'] = 'switch'; + $tmp[$x]['subcategory'] = 'conf'; + $tmp[$x]['enabled'] = 'true'; + $x++; + $tmp[$x]['name'] = 'dir'; + $tmp[$x]['value'] = $this->detect_switch->db_dir(); + $tmp[$x]['category'] = 'switch'; + $tmp[$x]['subcategory'] = 'db'; + $tmp[$x]['enabled'] = 'true'; + $x++; + $tmp[$x]['name'] = 'dir'; + $tmp[$x]['value'] = $this->detect_switch->log_dir(); + $tmp[$x]['category'] = 'switch'; + $tmp[$x]['subcategory'] = 'log'; + $tmp[$x]['enabled'] = 'true'; + $x++; + $tmp[$x]['name'] = 'dir'; + $tmp[$x]['value'] = $this->detect_switch->mod_dir(); + $tmp[$x]['category'] = 'switch'; + $tmp[$x]['subcategory'] = 'mod'; + $tmp[$x]['enabled'] = 'true'; + $x++; + $tmp[$x]['name'] = 'dir'; + $tmp[$x]['value'] = $this->detect_switch->script_dir(); + $tmp[$x]['category'] = 'switch'; + $tmp[$x]['subcategory'] = 'scripts'; + $tmp[$x]['enabled'] = 'true'; + $x++; + $tmp[$x]['name'] = 'dir'; + $tmp[$x]['value'] = $this->detect_switch->grammar_dir(); + $tmp[$x]['category'] = 'switch'; + $tmp[$x]['subcategory'] = 'grammar'; + $tmp[$x]['enabled'] = 'true'; + $x++; + $tmp[$x]['name'] = 'dir'; + $tmp[$x]['value'] = $this->detect_switch->storage_dir(); + $tmp[$x]['category'] = 'switch'; + $tmp[$x]['subcategory'] = 'storage'; + $tmp[$x]['enabled'] = 'true'; + $x++; + $tmp[$x]['name'] = 'dir'; + $tmp[$x]['value'] = join( DIRECTORY_SEPARATOR, $this->detect_switch->storage_dir(), 'voicemail'); + $tmp[$x]['category'] = 'switch'; + $tmp[$x]['subcategory'] = 'voicemail'; + $tmp[$x]['enabled'] = 'true'; + $x++; + $tmp[$x]['name'] = 'dir'; + $tmp[$x]['value'] = $this->detect_switch->recordings_dir(); + $tmp[$x]['category'] = 'switch'; + $tmp[$x]['subcategory'] = 'recordings'; + $tmp[$x]['enabled'] = 'true'; + $x++; + $tmp[$x]['name'] = 'dir'; + $tmp[$x]['value'] = $this->detect_switch->sounds_dir(); + $tmp[$x]['category'] = 'switch'; + $tmp[$x]['subcategory'] = 'sounds'; + $tmp[$x]['enabled'] = 'true'; + $x++; + $tmp[$x]['name'] = 'dir'; + $tmp[$x]['value'] = ''; + $tmp[$x]['category'] = 'switch'; + $tmp[$x]['subcategory'] = 'provision'; + $tmp[$x]['enabled'] = 'false'; + $x++; + $tmp[$x]['name'] = 'dir'; + $tmp[$x]['value'] = join( DIRECTORY_SEPARATOR, $this->detect_switch->conf_dir(), "/directory"); + $tmp[$x]['category'] = 'switch'; + $tmp[$x]['subcategory'] = 'extensions'; + $tmp[$x]['enabled'] = 'false'; + $x++; + $tmp[$x]['name'] = 'dir'; + $tmp[$x]['value'] = join( DIRECTORY_SEPARATOR, $this->detect_switch->conf_dir(), "/sip_profiles"); + $tmp[$x]['category'] = 'switch'; + $tmp[$x]['subcategory'] = 'sip_profiles'; + $tmp[$x]['enabled'] = 'false'; + $x++; + $tmp[$x]['name'] = 'dir'; + $tmp[$x]['value'] = join( DIRECTORY_SEPARATOR, $this->detect_switch->conf_dir(), "/dialplan"); + $tmp[$x]['category'] = 'switch'; + $tmp[$x]['subcategory'] = 'dialplan'; + $tmp[$x]['enabled'] = 'false'; + $x++; + + //server settings + $tmp[$x]['name'] = 'dir'; + $tmp[$x]['value'] = $this->detect_switch->temp_dir(); + $tmp[$x]['category'] = 'server'; + $tmp[$x]['subcategory'] = 'temp'; + $tmp[$x]['enabled'] = 'true'; + $x++; + #throw new Exception("I don't know how to find /etc/init.d for server > startup_scripts"); + $tmp[$x]['name'] = 'dir'; + $tmp[$x]['value'] = ''; + $tmp[$x]['category'] = 'server'; + $tmp[$x]['subcategory'] = 'startup_script'; + $tmp[$x]['enabled'] = 'true'; + $x++; + $tmp[$x]['name'] = 'dir'; + $tmp[$x]['value'] = sys_get_temp_dir(); + $tmp[$x]['category'] = 'server'; + $tmp[$x]['subcategory'] = 'backup'; + $tmp[$x]['enabled'] = 'true'; + $x++; + + $this->dbh->beginTransaction(); + foreach($tmp as $row) { + $sql = "insert into v_default_settings "; + $sql .= "("; + $sql .= "default_setting_uuid, "; + $sql .= "default_setting_name, "; + $sql .= "default_setting_value, "; + $sql .= "default_setting_category, "; + $sql .= "default_setting_subcategory, "; + $sql .= "default_setting_enabled "; + $sql .= ") "; + $sql .= "values "; + $sql .= "("; + $sql .= "'".uuid()."', "; + $sql .= "'".$row['name']."', "; + $sql .= "'".$row['value']."', "; + $sql .= "'".$row['category']."', "; + $sql .= "'".$row['subcategory']."', "; + $sql .= "'".$row['enabled']."' "; + $sql .= ");"; + $this->write_debug($sql); + $this->dbh->exec(check_sql($sql)); + unset($sql); + } + $this->dbh->commit(); + unset($tmp); + + //get the list of installed apps from the core and mod directories + $config_list = glob($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/*/*/app_config.php"); + $x=0; + foreach ($config_list as $config_path) { + include($config_path); + $x++; + } + + //add the groups + $x = 0; + $tmp[$x]['group_name'] = 'superadmin'; + $tmp[$x]['group_description'] = 'Super Administrator Group'; + $x++; + $tmp[$x]['group_name'] = 'admin'; + $tmp[$x]['group_description'] = 'Administrator Group'; + $x++; + $tmp[$x]['group_name'] = 'user'; + $tmp[$x]['group_description'] = 'User Group'; + $x++; + $tmp[$x]['group_name'] = 'public'; + $tmp[$x]['group_description'] = 'Public Group'; + $x++; + $tmp[$x]['group_name'] = 'agent'; + $tmp[$x]['group_description'] = 'Call Center Agent Group'; + $this->dbh->beginTransaction(); + foreach($tmp as $row) { + $sql = "insert into v_groups "; + $sql .= "("; + $sql .= "group_uuid, "; + $sql .= "group_name, "; + $sql .= "group_description "; + $sql .= ") "; + $sql .= "values "; + $sql .= "("; + $sql .= "'".uuid()."', "; + $sql .= "'".$row['group_name']."', "; + $sql .= "'".$row['group_description']."' "; + $sql .= ");"; + $this->write_debug($sql); + $this->dbh->exec(check_sql($sql)); + unset($sql); + } + unset($tmp); + $this->dbh->commit(); + //assign the default permissions to the groups + $this->dbh->beginTransaction(); + foreach($apps as $app) { + if ($app['permissions']) { + foreach ($app['permissions'] as $row) { + if ($this->debug) { + $this->write_debug( "v_group_permissions\n"); + $this->write_debug( json_encode($row)."\n\n"); + } + if ($row['groups']) { + foreach ($row['groups'] as $group) { + //add the record + $sql = "insert into v_group_permissions "; + $sql .= "("; + $sql .= "group_permission_uuid, "; + $sql .= "permission_name, "; + $sql .= "group_name "; + $sql .= ") "; + $sql .= "values "; + $sql .= "("; + $sql .= "'".uuid()."', "; + $sql .= "'".$row['name']."', "; + $sql .= "'".$group."' "; + $sql .= ");"; + if ($this->debug) { + $this->write_debug( $sql."\n"); + } + $this->dbh->exec(check_sql($sql)); + unset($sql); + } + } + } + } + } + $this->dbh->commit(); + } + } + + protected function create_superuser() { + //check if it exists first? + $this->write_progress("creating super user '" . $this->admin_username . "'"); + //add a user and then add the user to the superadmin group + //prepare the values + $this->admin_uuid = uuid(); + $contact_uuid = uuid(); + //set a sessiong variable + $_SESSION["user_uuid"] = $user_uuid; + //salt used with the password to create a one way hash + $salt = generate_password('20', '4'); + //add the user account + $sql = "insert into v_users "; + $sql .= "("; + $sql .= "domain_uuid, "; + $sql .= "user_uuid, "; + $sql .= "contact_uuid, "; + $sql .= "username, "; + $sql .= "password, "; + $sql .= "salt, "; + $sql .= "add_date, "; + $sql .= "add_user "; + $sql .= ") "; + $sql .= "values "; + $sql .= "("; + $sql .= "'".$this->domain_uuid."', "; + $sql .= "'".$this->admin_uuid."', "; + $sql .= "'$contact_uuid', "; + $sql .= "'".$this->admin_username."', "; + $sql .= "'".md5($salt.$this->admin_password)."', "; + $sql .= "'$salt', "; + $sql .= "now(), "; + $sql .= "'".$this->admin_username."' "; + $sql .= ");"; + $this->write_debug( $sql."\n"); + $this->dbh->exec(check_sql($sql)); + unset($sql); + + //add to contacts + $sql = "insert into v_contacts "; + $sql .= "("; + $sql .= "domain_uuid, "; + $sql .= "contact_uuid, "; + $sql .= "contact_type, "; + $sql .= "contact_name_given, "; + $sql .= "contact_nickname "; + $sql .= ") "; + $sql .= "values "; + $sql .= "("; + $sql .= "'".$this->domain_uuid."', "; + $sql .= "'$contact_uuid', "; + $sql .= "'user', "; + $sql .= "'".$this->admin_username."', "; + $sql .= "'".$this->admin_username."' "; + $sql .= ")"; + $this->dbh->exec(check_sql($sql)); + unset($sql); + + //add the user to the superadmin group + $sql = "insert into v_group_users "; + $sql .= "("; + $sql .= "group_user_uuid, "; + $sql .= "domain_uuid, "; + $sql .= "user_uuid, "; + $sql .= "group_name "; + $sql .= ") "; + $sql .= "values "; + $sql .= "("; + $sql .= "'".uuid()."', "; + $sql .= "'".$this->domain_uuid."', "; + $sql .= "'".$this->admin_uuid."', "; + $sql .= "'superadmin' "; + $sql .= ");"; + $this->write_debug( $sql."\n"); + $this->dbh->exec(check_sql($sql)); + unset($sql); + } + + protected function create_menus() { + $this->write_progress("creating menus"); + //set the defaults + $menu_name = 'default'; + $menu_language = 'en-us'; + $menu_description = 'Default Menu Set'; + //add the parent menu + $sql = "insert into v_menus "; + $sql .= "("; + $sql .= "menu_uuid, "; + $sql .= "menu_name, "; + $sql .= "menu_language, "; + $sql .= "menu_description "; + $sql .= ") "; + $sql .= "values "; + $sql .= "("; + $sql .= "'".$this->menu_uuid."', "; + $sql .= "'$menu_name', "; + $sql .= "'$menu_language', "; + $sql .= "'$menu_description' "; + $sql .= ");"; + if ($this->debug) { + $this->write_debug( $sql."\n"); + } + $this->dbh->exec(check_sql($sql)); + unset($sql); + + //add the menu items + require_once "resources/classes/menu.php"; + $menu = new menu; + $menu->db = $this->dbh; + $menu->menu_uuid = $this->menu_uuid; + $menu->restore(); + unset($menu); + } + + protected function post_create() { + $this->write_progress("running post steps"); + //login the user account + $_SESSION["username"] = $this->admin_username; + + //get the groups assigned to the user and then set the groups in $_SESSION["groups"] + $sql = "SELECT * FROM v_group_users "; + $sql .= "where domain_uuid=:domain_uuid "; + $sql .= "and user_uuid=:user_uuid "; + $prep_statement = $this->dbh->prepare(check_sql($sql)); + $prep_statement->bindParam(':domain_uuid', $this->domain_uuid); + $prep_statement->bindParam(':user_uuid', $this->admin_uuid); + $prep_statement->execute(); + $result = $prep_statement->fetchAll(PDO::FETCH_NAMED); + $_SESSION["groups"] = $result; + unset($sql, $row_count, $prep_statement); + + //get the permissions assigned to the groups that the user is a member of set the permissions in $_SESSION['permissions'] + $x = 0; + $sql = "select distinct(permission_name) from v_group_permissions "; + foreach($_SESSION["groups"] as $field) { + if (strlen($field['group_name']) > 0) { + if ($x == 0) { + $sql .= "where (domain_uuid = '".$this->domain_uuid."' and group_name = '".$field['group_name']."') "; + } + else { + $sql .= "or (domain_uuid = '".$this->domain_uuid."' and group_name = '".$field['group_name']."') "; + } + $x++; + } + } + $prep_statementsub = $this->dbh->prepare($sql); + $prep_statementsub->execute(); + $_SESSION['permissions'] = $prep_statementsub->fetchAll(PDO::FETCH_NAMED); + unset($sql, $prep_statementsub); + + //make sure the database schema and installation have performed all necessary tasks + $display_results = false; + $display_type = 'none'; + require_once "resources/classes/schema.php"; + $obj = new schema; + $obj->schema($this->dbh, $this->db_type, $this->db_name, $display_type); + + //run all app_defaults.php files + $default_language = $this->install_language; + require_once "resources/classes/domains.php"; + $domain = new domains; + $domain->upgrade(); + + //synchronize the config with the saved settings + save_switch_xml(); + + //do not show the apply settings reminder on the login page + $_SESSION["reload_xml"] = false; + + //clear the menu + $_SESSION["menu"] = ""; + } + + public function remove_config() { + if (file_exists('/bin/rm')) { + $this->write_debug('rm -f ' . $this->config_php); + exec ('rm -f ' . $this->config_php); + } + elseif(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'){ + $this->write_debug("del /S /F /Q '$dir'"); + exec("del /F /Q '" . $this->config_php . "'"); + } + else { + $this->write_debug("delete file: ".$file); + unlink($this->config_php); + } + clearstatcache(); + } + } +?> \ No newline at end of file diff --git a/core/install/resources/classes/install_switch.php b/core/install/resources/classes/install_switch.php new file mode 100644 index 0000000000..61cad71e9a --- /dev/null +++ b/core/install/resources/classes/install_switch.php @@ -0,0 +1,252 @@ + + Copyright (C) 2010-2015 + All Rights Reserved. + + Contributor(s): + Mark J Crane +*/ +include "root.php"; + +//define the install class + class install_switch { + + protected $domain_uuid; + protected $domain_name; + protected $detect_switch; + + public $debug = false; + + function __construct($domain_name, $domain_uuid, $detect_switch) { + if(!is_a($detect_switch, 'detect_switch')){ + throw new Exception('The parameter $detect_switch must be a detect_switch object (or a subclass of)'); + } + $this->domain_uuid = $domain_uuid; + $this->domain = $domain_name; + $this->detect_switch = $detect_switch; + } + + //utility Functions + + function write_debug($message) { + if($this->debug){ + echo "$message\n"; + } + } + + function write_progress($message) { + echo "$message\n"; + } + + //$options '-n' --no-clobber + protected function recursive_copy($src, $dst, $options = '') { + if (file_exists('/bin/cp')) { + if (strtoupper(substr(PHP_OS, 0, 3)) === 'SUN') { + //copy -R recursive, preserve attributes for SUN + $cmd = 'cp -Rp '.$src.'/* '.$dst; + } else { + //copy -R recursive, -L follow symbolic links, -p preserve attributes for other Posix systemss + $cmd = 'cp -RLp '.$options.' '.$src.'/* '.$dst; + } + $this->write_debug($cmd); + exec ($cmd); + } + elseif(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'){ + exec("copy /L '$src' '$dst'"); + } + else { + $dir = opendir($src); + if (!$dir) { + if (!mkdir($src, 0755, true)) { + throw new Exception("recursive_copy() source directory '".$src."' does not exist."); + } + } + if (!is_dir($dst)) { + if (!mkdir($dst, 0755, true)) { + throw new Exception("recursive_copy() failed to create destination directory '".$dst."'"); + } + } + //This looks wrong, essentially if we can't use /bin/cp it manually fils dirs, not correct + $scripts_dir_target = $_SESSION['switch']['scripts']['dir']; + $scripts_dir_source = realpath($_SERVER["DOCUMENT_ROOT"].PROJECT_PATH.'/resources/install/scripts'); + foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($src)) as $file_path_source) { + if ( + substr_count($file_path_source, '/..') == 0 && + substr_count($file_path_source, '/.') == 0 && + substr_count($file_path_source, '/.svn') == 0 && + substr_count($file_path_source, '/.git') == 0 + ) { + if ($dst != $src.'/resources/config.lua') { + $this->write_debug($file_path_source.' ---> '.$dst); + copy($file_path_source, $dst); + chmod($dst, 0755); + } + } + } + + while(false !== ($file = readdir($dir))) { + if (($file != '.') && ($file != '..')) { + if (is_dir($src.'/'.$file)) { + $this->recursive_copy($src.'/'.$file, $dst.'/'.$file); + } + else { + //copy only missing files -n --no-clobber + if (strpos($options,'-n') !== false) { + if (!file_exists($dst.'/'.$file)) { + $this->write_debug("copy(".$src."/".$file.", ".$dst."/".$file.")"); + copy($src.'/'.$file, $dst.'/'.$file); + } + } + else { + copy($src.'/'.$file, $dst.'/'.$file); + } + } + } + } + closedir($dir); + } + } + + protected function recursive_delete($dir) { + if (file_exists('/bin/rm')) { + $this->write_debug('rm -Rf '.$dir.'/*'); + exec ('rm -Rf '.$dir.'/*'); + } + elseif(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'){ + $this->write_debug("del /S /F /Q '$dir'"); + exec("del /S /F /Q '$dir'"); + } + else { + foreach (glob($dir) as $file) { + if (is_dir($file)) { + $this->write_debug("rm dir: ".$file); + $this->recursive_delete("$file/*"); + rmdir($file); + } else { + $this->write_debug("delete file: ".$file); + unlink($file); + } + } + } + clearstatcache(); + } + + protected function backup_dir($dir, $backup_name){ + if (!is_readable($dir)) { + throw new Exception("backup_dir() source directory '".$dir."' does not exist."); + } + $dst_tar = join( DIRECTORY_SEPARATOR, array(sys_get_temp_dir(), "$backup_name.tar")); + //pharData is the correct ay to do it, but it keeps creating incomplete archives + //$tar = new PharData($dst_tar); + //$tar->buildFromDirectory($dir); + $this->write_debug("backingup to $dst_tar"); + if (file_exists('/bin/tar')) { + exec('tar -cvf ' .$dst_tar. ' -C '.$dir .' .'); + }else{ + $this->write_debug('WARN: old config could not be compressed'); + $dst_dir = join( DIRECTORY_SEPARATOR, array(sys_get_temp_dir(), "$backup_name")); + recursive_copy($dir, $dst_dir); + } + } + + function install() { + $this->copy_conf(); + $this->copy_scripts(); + } + + function upgrade() { + $this->copy_scripts(); + } + + function copy_conf() { + $this->write_progress("Copying Config"); + //make a backup of the config + if (file_exists($this->detect_switch->conf_dir())) { + $this->backup_dir($this->detect_switch->conf_dir(), 'fusionpbx_switch_config'); + $this->recursive_delete($this->detect_switch->conf_dir()); + } + //make sure the conf directory exists + if (!is_dir($this->detect_switch->conf_dir())) { + if (!mkdir($this->detect_switch->conf_dir(), 0774, true)) { + throw new Exception("Failed to create the switch conf directory '".$this->detect_switch->conf_dir()."'. "); + } + } + //copy resources/templates/conf to the freeswitch conf dir + if (file_exists('/usr/share/examples/fusionpbx/resources/templates/conf')){ + $src_dir = "/usr/share/examples/fusionpbx/resources/templates/conf"; + } + else { + $src_dir = $_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/resources/templates/conf"; + } + $dst_dir = $this->detect_switch->conf_dir(); + if (is_readable($dst_dir)) { + $this->recursive_copy($src_dir, $dst_dir); + unset($src_dir, $dst_dir); + } + $fax_dir = join( DIRECTORY_SEPARATOR, array($this->detect_switch->storage_dir(), 'fax')); + if (!is_readable($fax_dir)) { mkdir($fax_dir,0777,true); } + $voicemail_dir = join( DIRECTORY_SEPARATOR, array($this->detect_switch->storage_dir(), 'voicemail')); + if (!is_readable($voicemail_dir)) { mkdir($voicemail_dir,0777,true); } + + //create the dialplan/default.xml for single tenant or dialplan/domain.xml + if (file_exists($_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/app/dialplan")) { + $dialplan = new dialplan; + $dialplan->domain_uuid = $this->domain_uuid; + $dialplan->domain = $this->domain_name; + $dialplan->switch_dialplan_dir = join( DIRECTORY_SEPARATOR, array($this->detect_switch->conf_dir(), "/dialplan")); + $dialplan->restore_advanced_xml(); + if($this->_debug){ + print_r($dialplan->result, $message); + $this->write_debug($message); + } + } + + //write the xml_cdr.conf.xml file + if (file_exists($_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/app/xml_cdr")) { + xml_cdr_conf_xml(); + } + + //write the switch.conf.xml file + if (file_exists($this->detect_switch->conf_dir())) { + switch_conf_xml(); + } + + } + + function copy_scripts() { + $this->write_progress("Copying Scripts"); + if (file_exists($this->detect_switch->script_dir())) { + if (file_exists('/usr/share/examples/fusionpbx/resources/install/scripts')){ + $src_dir = '/usr/share/examples/fusionpbx/resources/install/scripts'; + } + else { + $src_dir = $_SERVER["DOCUMENT_ROOT"].PROJECT_PATH.'/resources/install/scripts'; + } + $dst_dir = $this->detect_switch->script_dir(); + if (is_readable($this->detect_switch->script_dir())) { + $this->recursive_copy($src_dir, $dst_dir, $_SESSION['scripts']['options']['text']); + unset($src_dir, $dst_dir); + } + chmod($dst_dir, 0774); + } + } + + } +?> \ No newline at end of file diff --git a/core/install/resources/classes/iso_countries.php b/core/install/resources/classes/iso_countries.php new file mode 100644 index 0000000000..3609754ab7 --- /dev/null +++ b/core/install/resources/classes/iso_countries.php @@ -0,0 +1,250 @@ + 'Afghanistan', + 'AX' => 'Aland Islands', + 'AL' => 'Albania', + 'DZ' => 'Algeria', + 'AS' => 'American Samoa', + 'AD' => 'Andorra', + 'AO' => 'Angola', + 'AI' => 'Anguilla', + 'AQ' => 'Antarctica', + 'AG' => 'Antigua And Barbuda', + 'AR' => 'Argentina', + 'AM' => 'Armenia', + 'AW' => 'Aruba', + 'AU' => 'Australia', + 'AT' => 'Austria', + 'AZ' => 'Azerbaijan', + 'BS' => 'Bahamas', + 'BH' => 'Bahrain', + 'BD' => 'Bangladesh', + 'BB' => 'Barbados', + 'BY' => 'Belarus', + 'BE' => 'Belgium', + 'BZ' => 'Belize', + 'BJ' => 'Benin', + 'BM' => 'Bermuda', + 'BT' => 'Bhutan', + 'BO' => 'Bolivia', + 'BA' => 'Bosnia And Herzegovina', + 'BW' => 'Botswana', + 'BV' => 'Bouvet Island', + 'BR' => 'Brazil', + 'IO' => 'British Indian Ocean Territory', + 'BN' => 'Brunei Darussalam', + 'BG' => 'Bulgaria', + 'BF' => 'Burkina Faso', + 'BI' => 'Burundi', + 'KH' => 'Cambodia', + 'CM' => 'Cameroon', + 'CA' => 'Canada', + 'CV' => 'Cape Verde', + 'KY' => 'Cayman Islands', + 'CF' => 'Central African Republic', + 'TD' => 'Chad', + 'CL' => 'Chile', + 'CN' => 'China', + 'CX' => 'Christmas Island', + 'CC' => 'Cocos (Keeling) Islands', + 'CO' => 'Colombia', + 'KM' => 'Comoros', + 'CG' => 'Congo', + 'CD' => 'Congo, Democratic Republic', + 'CK' => 'Cook Islands', + 'CR' => 'Costa Rica', + 'CI' => 'Cote D\'Ivoire', + 'HR' => 'Croatia', + 'CU' => 'Cuba', + 'CY' => 'Cyprus', + 'CZ' => 'Czech Republic', + 'DK' => 'Denmark', + 'DJ' => 'Djibouti', + 'DM' => 'Dominica', + 'DO' => 'Dominican Republic', + 'EC' => 'Ecuador', + 'EG' => 'Egypt', + 'SV' => 'El Salvador', + 'GQ' => 'Equatorial Guinea', + 'ER' => 'Eritrea', + 'EE' => 'Estonia', + 'ET' => 'Ethiopia', + 'FK' => 'Falkland Islands (Malvinas)', + 'FO' => 'Faroe Islands', + 'FJ' => 'Fiji', + 'FI' => 'Finland', + 'FR' => 'France', + 'GF' => 'French Guiana', + 'PF' => 'French Polynesia', + 'TF' => 'French Southern Territories', + 'GA' => 'Gabon', + 'GM' => 'Gambia', + 'GE' => 'Georgia', + 'DE' => 'Germany', + 'GH' => 'Ghana', + 'GI' => 'Gibraltar', + 'GR' => 'Greece', + 'GL' => 'Greenland', + 'GD' => 'Grenada', + 'GP' => 'Guadeloupe', + 'GU' => 'Guam', + 'GT' => 'Guatemala', + 'GG' => 'Guernsey', + 'GN' => 'Guinea', + 'GW' => 'Guinea-Bissau', + 'GY' => 'Guyana', + 'HT' => 'Haiti', + 'HM' => 'Heard Island & Mcdonald Islands', + 'VA' => 'Holy See (Vatican City State)', + 'HN' => 'Honduras', + 'HK' => 'Hong Kong', + 'HU' => 'Hungary', + 'IS' => 'Iceland', + 'IN' => 'India', + 'ID' => 'Indonesia', + 'IR' => 'Iran, Islamic Republic Of', + 'IQ' => 'Iraq', + 'IE' => 'Ireland', + 'IM' => 'Isle Of Man', + 'IL' => 'Israel', + 'IT' => 'Italy', + 'JM' => 'Jamaica', + 'JP' => 'Japan', + 'JE' => 'Jersey', + 'JO' => 'Jordan', + 'KZ' => 'Kazakhstan', + 'KE' => 'Kenya', + 'KI' => 'Kiribati', + 'KR' => 'Korea', + 'KW' => 'Kuwait', + 'KG' => 'Kyrgyzstan', + 'LA' => 'Lao People\'s Democratic Republic', + 'LV' => 'Latvia', + 'LB' => 'Lebanon', + 'LS' => 'Lesotho', + 'LR' => 'Liberia', + 'LY' => 'Libyan Arab Jamahiriya', + 'LI' => 'Liechtenstein', + 'LT' => 'Lithuania', + 'LU' => 'Luxembourg', + 'MO' => 'Macao', + 'MK' => 'Macedonia', + 'MG' => 'Madagascar', + 'MW' => 'Malawi', + 'MY' => 'Malaysia', + 'MV' => 'Maldives', + 'ML' => 'Mali', + 'MT' => 'Malta', + 'MH' => 'Marshall Islands', + 'MQ' => 'Martinique', + 'MR' => 'Mauritania', + 'MU' => 'Mauritius', + 'YT' => 'Mayotte', + 'MX' => 'Mexico', + 'FM' => 'Micronesia, Federated States Of', + 'MD' => 'Moldova', + 'MC' => 'Monaco', + 'MN' => 'Mongolia', + 'ME' => 'Montenegro', + 'MS' => 'Montserrat', + 'MA' => 'Morocco', + 'MZ' => 'Mozambique', + 'MM' => 'Myanmar', + 'NA' => 'Namibia', + 'NR' => 'Nauru', + 'NP' => 'Nepal', + 'NL' => 'Netherlands', + 'AN' => 'Netherlands Antilles', + 'NC' => 'New Caledonia', + 'NZ' => 'New Zealand', + 'NI' => 'Nicaragua', + 'NE' => 'Niger', + 'NG' => 'Nigeria', + 'NU' => 'Niue', + 'NF' => 'Norfolk Island', + 'MP' => 'Northern Mariana Islands', + 'NO' => 'Norway', + 'OM' => 'Oman', + 'PK' => 'Pakistan', + 'PW' => 'Palau', + 'PS' => 'Palestinian Territory, Occupied', + 'PA' => 'Panama', + 'PG' => 'Papua New Guinea', + 'PY' => 'Paraguay', + 'PE' => 'Peru', + 'PH' => 'Philippines', + 'PN' => 'Pitcairn', + 'PL' => 'Poland', + 'PT' => 'Portugal', + 'PR' => 'Puerto Rico', + 'QA' => 'Qatar', + 'RE' => 'Reunion', + 'RO' => 'Romania', + 'RU' => 'Russian Federation', + 'RW' => 'Rwanda', + 'BL' => 'Saint Barthelemy', + 'SH' => 'Saint Helena', + 'KN' => 'Saint Kitts And Nevis', + 'LC' => 'Saint Lucia', + 'MF' => 'Saint Martin', + 'PM' => 'Saint Pierre And Miquelon', + 'VC' => 'Saint Vincent And Grenadines', + 'WS' => 'Samoa', + 'SM' => 'San Marino', + 'ST' => 'Sao Tome And Principe', + 'SA' => 'Saudi Arabia', + 'SN' => 'Senegal', + 'RS' => 'Serbia', + 'SC' => 'Seychelles', + 'SL' => 'Sierra Leone', + 'SG' => 'Singapore', + 'SK' => 'Slovakia', + 'SI' => 'Slovenia', + 'SB' => 'Solomon Islands', + 'SO' => 'Somalia', + 'ZA' => 'South Africa', + 'GS' => 'South Georgia And Sandwich Isl.', + 'ES' => 'Spain', + 'LK' => 'Sri Lanka', + 'SD' => 'Sudan', + 'SR' => 'Suriname', + 'SJ' => 'Svalbard And Jan Mayen', + 'SZ' => 'Swaziland', + 'SE' => 'Sweden', + 'CH' => 'Switzerland', + 'SY' => 'Syrian Arab Republic', + 'TW' => 'Taiwan', + 'TJ' => 'Tajikistan', + 'TZ' => 'Tanzania', + 'TH' => 'Thailand', + 'TL' => 'Timor-Leste', + 'TG' => 'Togo', + 'TK' => 'Tokelau', + 'TO' => 'Tonga', + 'TT' => 'Trinidad And Tobago', + 'TN' => 'Tunisia', + 'TR' => 'Turkey', + 'TM' => 'Turkmenistan', + 'TC' => 'Turks And Caicos Islands', + 'TV' => 'Tuvalu', + 'UG' => 'Uganda', + 'UA' => 'Ukraine', + 'AE' => 'United Arab Emirates', + 'GB' => 'United Kingdom', + 'US' => 'United States', + 'UM' => 'United States Outlying Islands', + 'UY' => 'Uruguay', + 'UZ' => 'Uzbekistan', + 'VU' => 'Vanuatu', + 'VE' => 'Venezuela', + 'VN' => 'Viet Nam', + 'VG' => 'Virgin Islands, British', + 'VI' => 'Virgin Islands, U.S.', + 'WF' => 'Wallis And Futuna', + 'EH' => 'Western Sahara', + 'YE' => 'Yemen', + 'ZM' => 'Zambia', + 'ZW' => 'Zimbabwe', +); +?> \ No newline at end of file diff --git a/core/install/resources/page_parts/install_config_database.php b/core/install/resources/page_parts/install_config_database.php new file mode 100644 index 0000000000..ad8e632734 --- /dev/null +++ b/core/install/resources/page_parts/install_config_database.php @@ -0,0 +1,251 @@ + + Portions created by the Initial Developer are Copyright (C) 2008-2012 + the Initial Developer. All Rights Reserved. + + Contributor(s): + Mark J Crane + Matthew Vale +*/ + + echo "
\n"; + echo "\n"; + echo "\n"; + echo "\n"; + + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + + echo "\n"; + + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + + if ($db_type == "sqlite") { + + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + + } + + if ($db_type == "mysql") { + + //set defaults + if (strlen($db_host) == 0) { $db_host = 'localhost'; } + if (strlen($db_port) == 0) { $db_port = '3306'; } + //if (strlen($db_name) == 0) { $db_name = 'fusionpbx'; } + + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + + } + + if ($db_type == "pgsql") { + if (strlen($db_host) == 0) { $db_host = 'localhost'; } + if (strlen($db_port) == 0) { $db_port = '5432'; } + if (strlen($db_name) == 0) { $db_name = 'fusionpbx'; } + + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + } + + echo "
".$text['header-config_database']." 
\n"; + echo " Database Filename\n"; + echo "\n"; + echo "
\n"; + echo " Set the database filename. The file extension should be '.db'.\n"; + echo "\n"; + echo "
\n"; + echo " Database Directory\n"; + echo "\n"; + echo "
\n"; + echo " Set the path to the database directory.\n"; + echo "
\n"; + echo " Database Host\n"; + echo "\n"; + echo "
\n"; + echo " Enter the host address for the database server.\n"; + echo "\n"; + echo "
\n"; + echo " Database Port\n"; + echo "\n"; + echo "
\n"; + echo " Enter the port number. It is optional if the database is using the default port.\n"; + echo "\n"; + echo "
\n"; + echo " Database Name\n"; + echo "\n"; + echo "
\n"; + echo " Enter the name of the database.\n"; + echo "\n"; + echo "
\n"; + echo " Database Username\n"; + echo "\n"; + echo "
\n"; + echo " Enter the database username. \n"; + echo "\n"; + echo "
\n"; + echo " Database Password\n"; + echo "\n"; + echo "
\n"; + echo " Enter the database password.\n"; + echo "\n"; + echo "
\n"; + echo " Create Database Username\n"; + echo "\n"; + echo "
\n"; + echo " Optional, this username is used to create the database, a database user and set the permissions. \n"; + echo " By default this username is 'root' however it can be any account with permission to add a database, user, and grant permissions. \n"; + echo "
\n"; + echo " Create Database Password\n"; + echo "\n"; + echo "
\n"; + echo " Enter the create database password.\n"; + echo "\n"; + echo "
\n"; + echo " Database Host\n"; + echo "\n"; + echo "
\n"; + echo " Enter the host address for the database server.\n"; + echo "\n"; + echo "
\n"; + echo " Database Port\n"; + echo "\n"; + echo "
\n"; + echo " Enter the port number. It is optional if the database is using the default port.\n"; + echo "\n"; + echo "
\n"; + echo " Database Name\n"; + echo "\n"; + echo "
\n"; + echo " Enter the name of the database.\n"; + echo "\n"; + echo "
\n"; + echo " Database Username\n"; + echo "\n"; + echo "
\n"; + echo " Enter the database username.\n"; + echo "\n"; + echo "
\n"; + echo " Database Password\n"; + echo "\n"; + echo "
\n"; + echo " Enter the database password.\n"; + echo "\n"; + echo "
\n"; + echo " Create Database Username\n"; + echo "\n"; + echo "
\n"; + echo " Optional, this username is used to create the database, a database user and set the permissions. \n"; + echo " By default this username is 'pgsql' however it can be any account with permission to add a database, user, and grant permissions. \n"; + echo " Leave blank if the user and empty database already exist and you do not want them created. \n"; + echo "
\n"; + echo " Create Database Password\n"; + echo "\n"; + echo "
\n"; + echo " Enter the create database password.\n"; + echo "\n"; + echo "
"; + echo "
\n"; + echo " \n"; + echo " \n"; + echo "
\n"; + echo "
\n"; +?> \ No newline at end of file diff --git a/core/install/resources/page_parts/install_config_detail.php b/core/install/resources/page_parts/install_config_detail.php new file mode 100644 index 0000000000..e59ddb1a80 --- /dev/null +++ b/core/install/resources/page_parts/install_config_detail.php @@ -0,0 +1,146 @@ + + Portions created by the Initial Developer are Copyright (C) 2008-2012 + the Initial Developer. All Rights Reserved. + + Contributor(s): + Mark J Crane + Matthew Vale +*/ + + echo "
\n"; + echo "\n"; + echo "\n"; + echo "\n"; + + echo "\n"; + echo "\n"; + echo "\n"; + + echo "\n"; + + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + + echo " \n"; + echo " \n"; + echo " \n"; + echo " \n"; + + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + + echo "
".$text['header-config_detail']." 
\n"; + echo " Username\n"; + echo "\n"; + echo "
\n"; + echo " Enter the username to use when logging in with the browser.
\n"; + echo "
\n"; + echo " Password\n"; + echo "\n"; + echo "
\n"; + echo " Enter the password to use when logging in with the browser.
\n"; + echo "
\n"; + echo " Country\n"; + echo "\n"; + echo " \n"; + echo "
\n"; + echo " Select ISO country code used to initialize calling contry code variables.
\n"; + echo "
\n"; + echo " Theme: \n"; + echo " \n"; + echo " \n"; + echo "
\n"; + echo " Select a theme to set as the default.
\n"; + echo "
\n"; + echo " Domain name\n"; + echo "\n"; + echo "
\n"; + echo " Enter the default domain name. \n"; + echo "\n"; + echo "
\n"; + echo " Database Type\n"; + echo "\n"; + echo "
\n"; + echo " Select the database type.\n"; + echo "\n"; + echo "
"; + echo "
\n"; + echo " \n"; + echo " \n"; + echo "
\n"; + echo "
\n"; +?> \ No newline at end of file diff --git a/core/install/resources/page_parts/install_event_socket.php b/core/install/resources/page_parts/install_event_socket.php new file mode 100644 index 0000000000..b191eb914e --- /dev/null +++ b/core/install/resources/page_parts/install_event_socket.php @@ -0,0 +1,101 @@ + + Portions created by the Initial Developer are Copyright (C) 2008-2012 + the Initial Developer. All Rights Reserved. + + Contributor(s): + Matthew Vale +*/ + echo "

Detected Configuration

"; + //fetch the values + require_once "core/install/resources/classes/detect_switch.php"; + $switch_detect = new detect_switch($event_host, $event_port, $event_password); + //$switch_detect->event_port = 2021; + echo "
";
+	$detect_ok = true;
+	try {
+		$switch_detect->detect();
+	} catch(Exception $e){
+		echo "Failed to detect confgiuration detect_switch reported: " . $e->getMessage() . "\n";
+		$detect_ok = false;
+	}
+	if($detect_ok){
+		echo $switch_detect->show_config();
+	}
+	echo "
"; + + echo "
\n"; + echo "\n"; + echo "\n"; + echo "\n"; + + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + + echo " \n"; + echo " \n"; + echo " "; + + echo "
".$text['header-event_socket']."

"; + echo " \n"; + echo "
\n"; + echo " ".$text['label-event_host']."\n"; + echo "\n"; + echo " event_host."\" onchange='JavaScript:disable_next()'/>\n"; + echo "
\n"; + echo $text['description-event_host']."\n"; + echo "
\n"; + echo " ".$text['label-event_port']."\n"; + echo "\n"; + echo " event_port."\" onchange='JavaScript:disable_next()'/>\n"; + echo "
\n"; + echo $text['description-event_port']."\n"; + echo "
\n"; + echo " ".$text['label-event_password']."\n"; + echo "\n"; + echo " event_password."\" onchange='JavaScript:disable_next()'/>\n"; + echo "
\n"; + echo $text['description-event_password']."\n"; + echo "
\n"; + echo "
"; + echo "
"; + echo "
"; +?> \ No newline at end of file diff --git a/core/install/resources/page_parts/install_select_language.php b/core/install/resources/page_parts/install_select_language.php new file mode 100644 index 0000000000..bcb5628c53 --- /dev/null +++ b/core/install/resources/page_parts/install_select_language.php @@ -0,0 +1,69 @@ + + Portions created by the Initial Developer are Copyright (C) 2008-2012 + the Initial Developer. All Rights Reserved. + + Contributor(s): + Matthew Vale +*/ + + echo "
\n"; + echo "\n"; + + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + + echo "\n"; + echo "\n"; + echo "\n"; + echo "\n"; + + echo "
".$text['header-select_language']."

"; + echo " \n"; + echo "
\n"; + echo " ".$text['label-select_language']."\n"; + echo "\n"; + echo "
"; + foreach($_SESSION['app']['languages'] as $lang_code){ + echo "
"; + echo " \n"; + echo "
"; + } + echo "
"; + echo "
\n"; + echo $text['description-select_language']."\n"; + echo "
"; + echo "

"; + echo "
"; +?> \ No newline at end of file diff --git a/core/install/root.php b/core/install/root.php new file mode 100644 index 0000000000..884d2b08a3 --- /dev/null +++ b/core/install/root.php @@ -0,0 +1,50 @@ + + Portions created by the Initial Developer are Copyright (C) 2008-2012 + the Initial Developer. All Rights Reserved. + + Contributor(s): + Mark J Crane +*/ + +// make sure the PATH_SEPARATOR is defined + if (!defined("PATH_SEPARATOR")) { + if ( strpos( $_ENV[ "OS" ], "Win" ) !== false ) { define("PATH_SEPARATOR", ";"); } else { define("PATH_SEPARATOR", ":"); } + } + +// make sure the document_root is set + $_SERVER["SCRIPT_FILENAME"] = str_replace("\\", "/", $_SERVER["SCRIPT_FILENAME"]); + $_SERVER["DOCUMENT_ROOT"] = str_replace($_SERVER["PHP_SELF"], "", $_SERVER["SCRIPT_FILENAME"]); + $_SERVER["DOCUMENT_ROOT"] = realpath($_SERVER["DOCUMENT_ROOT"]); + //echo "DOCUMENT_ROOT: ".$_SERVER["DOCUMENT_ROOT"]."
\n"; + //echo "PHP_SELF: ".$_SERVER["PHP_SELF"]."
\n"; + //echo "SCRIPT_FILENAME: ".$_SERVER["SCRIPT_FILENAME"]."
\n"; + +// if the project directory exists then add it to the include path otherwise add the document root to the include path + if (is_dir($_SERVER["DOCUMENT_ROOT"].'/fusionpbx')){ + if(!defined('PROJECT_PATH')) { define('PROJECT_PATH', '/fusionpbx'); } + set_include_path( get_include_path() . PATH_SEPARATOR . $_SERVER["DOCUMENT_ROOT"].'/fusionpbx' ); + } + else { + if(!defined('PROJECT_PATH')) { define('PROJECT_PATH', ''); } + set_include_path( get_include_path() . PATH_SEPARATOR . $_SERVER['DOCUMENT_ROOT'] ); + } + +?> \ No newline at end of file diff --git a/index.php b/index.php index 0e64a38080..d8b23953cb 100644 --- a/index.php +++ b/index.php @@ -41,7 +41,7 @@ include "root.php"; } elseif (file_exists("/usr/local/etc/fusionpbx/config.php")) { //bsd } else { - header("Location: ".PROJECT_PATH."/resources/install.php"); + header("Location: ".PROJECT_PATH."/core/install/install_first_time.php"); exit; } diff --git a/themes/enhanced/app_defaults.php b/themes/enhanced/app_defaults.php index f08e217176..ed8a9dad06 100644 --- a/themes/enhanced/app_defaults.php +++ b/themes/enhanced/app_defaults.php @@ -182,24 +182,37 @@ if ($domains_processed == 1) { $array[$x]['default_setting_enabled'] = 'false'; $array[$x]['default_setting_description'] = 'Set the opacity of the main menu (decimal, Minimized theme only).'; - //iterate and add each, if necessary - foreach ($array as $index => $default_settings) { - //add theme default settings - $sql = "select count(*) as num_rows from v_default_settings "; - $sql .= "where default_setting_category = 'theme' "; - $sql .= "and default_setting_subcategory = '".$default_settings['default_setting_subcategory']."' "; - $prep_statement = $db->prepare($sql); - if ($prep_statement) { - $prep_statement->execute(); - $row = $prep_statement->fetch(PDO::FETCH_ASSOC); - unset($prep_statement); - if ($row['num_rows'] == 0) { - $orm = new orm; - $orm->name('default_settings'); - $orm->save($array[$index]); - $message = $orm->message; + if($set_session_theme){ + foreach ($array as $index => $default_settings) { + $sub_category = $array[$index]['default_setting_subcategory']; + $name = $array[$index]['default_setting_name']; + if($array[$index]['default_setting_enabled'] == 'true'){ + $_SESSION['theme'][$sub_category][$name] = $array[$index]['default_setting_value']; + }else{ + $_SESSION['theme'][$sub_category][$name] = ''; + } + } + } + else{ + //iterate and add each, if necessary + foreach ($array as $index => $default_settings) { + //add theme default settings + $sql = "select count(*) as num_rows from v_default_settings "; + $sql .= "where default_setting_category = 'theme' "; + $sql .= "and default_setting_subcategory = '".$default_settings['default_setting_subcategory']."' "; + $prep_statement = $db->prepare($sql); + if ($prep_statement) { + $prep_statement->execute(); + $row = $prep_statement->fetch(PDO::FETCH_ASSOC); + unset($prep_statement); + if ($row['num_rows'] == 0) { + $orm = new orm; + $orm->name('default_settings'); + $orm->save($array[$index]); + $message = $orm->message; + } + unset($row); } - unset($row); } } @@ -222,25 +235,37 @@ if ($domains_processed == 1) { $array[$x]['default_setting_enabled'] = 'true'; $array[$x]['default_setting_description'] = 'Set a secondary background (HTML compatible) color, for a gradient effect.'; - //add secondary background color separately, if missing - $sql = "select count(*) as num_rows from v_default_settings "; - $sql .= "where default_setting_category = 'theme' "; - $sql .= "and default_setting_subcategory = 'background_color' "; - $prep_statement = $db->prepare($sql); - if ($prep_statement) { - $prep_statement->execute(); - $row = $prep_statement->fetch(PDO::FETCH_ASSOC); - unset($prep_statement); - if ($row['num_rows'] == 0) { - $orm = new orm; - $orm->name('default_settings'); - foreach ($array as $index => $null) { - $orm->save($array[$index]); + if($set_session_theme){ + foreach ($array as $index => $default_settings) { + $sub_category = $array[$index]['default_setting_subcategory']; + $idx = $array[$index]['default_setting_order']; + if($array[$index]['default_setting_enabled'] == 'true'){ + $_SESSION['theme'][$sub_category][$idx] = $array[$index]['default_setting_value']; } - $message = $orm->message; - //print_r($message); } - unset($row); + return; + } + else{ + //add secondary background color separately, if missing + $sql = "select count(*) as num_rows from v_default_settings "; + $sql .= "where default_setting_category = 'theme' "; + $sql .= "and default_setting_subcategory = 'background_color' "; + $prep_statement = $db->prepare($sql); + if ($prep_statement) { + $prep_statement->execute(); + $row = $prep_statement->fetch(PDO::FETCH_ASSOC); + unset($prep_statement); + if ($row['num_rows'] == 0) { + $orm = new orm; + $orm->name('default_settings'); + foreach ($array as $index => $null) { + $orm->save($array[$index]); + } + $message = $orm->message; + //print_r($message); + } + unset($row); + } } //get the background images