Detect the host type for PostgreSQL so it works with host or ip address. Also improve the code consistency by correcting the indentation and adding a few more comments.

This commit is contained in:
markjcrane
2015-12-06 12:25:34 -07:00
parent c7ce4dade0
commit b4e3ee5b0d
2 changed files with 147 additions and 136 deletions

View File

@@ -35,7 +35,7 @@ require_once "root.php";
public function get_switch_dirs() { return $this->_switch_dirs; }
public function get_switch_vdirs() { return $this->_switch_vdirs; }
// dirs - detected by from the switch
// dirs - detected from the switch
protected $_switch_base_dir = '';
protected $_switch_cache_dir = '';
protected $_switch_certs_dir = '';
@@ -157,7 +157,7 @@ require_once "root.php";
$this->_switch_event_host = $_SESSION['event_socket_ip_address'];
$this->_switch_event_port = $_SESSION['event_socket_port'];
$this->_switch_event_password = $_SESSION['event_socket_password'];
// domain info
$this->_domain_name = $_SESSION['domain_name'];
$this->_domain_uuid = $_SESSION['domain_uuid'];

View File

@@ -285,149 +285,160 @@ include "root.php";
}
public function create_config_lua() {
$this->write_progress("\tCreating " . $this->config_lua);
$path = dirname($this->config_lua);
$parent_dir = basename($path);
if($parent_dir == 'resources' and !file_exists($path)){
$this->write_progress("\t... creating missing '$path'");
if (!mkdir($path, 0755, true)) {
throw new Exception("Failed to create the missing resources directory '$path'");
}
}
global $db;
//get the odbc information
$sql = "select count(*) as num_rows from v_databases ";
$sql .= "where database_driver = 'odbc' ";
if (strlen($order_by)> 0) { $sql .= "order by $order_by $order "; }
$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) {
$odbc_num_rows = $row['num_rows'];
$sql = "select * from v_databases ";
$sql .= "where database_driver = 'odbc' ";
$prep_statement = $db->prepare(check_sql($sql));
$prep_statement->execute();
$result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
foreach ($result as &$row) {
$dsn_name = $row["database_name"];
$dsn_username = $row["database_username"];
$dsn_password = $row["database_password"];
break; //limit to 1 row
//define the database connection as global
global $db;
//send progress
$this->write_progress("\tCreating " . $this->config_lua);
//set the directories
$path = dirname($this->config_lua);
$parent_dir = basename($path);
if ($parent_dir == 'resources' and !file_exists($path)){
$this->write_progress("\t... creating missing '$path'");
if (!mkdir($path, 0755, true)) {
throw new Exception("Failed to create the missing resources directory '$path'");
}
unset ($prep_statement);
}
//get the odbc information
$sql = "select count(*) as num_rows from v_databases ";
$sql .= "where database_driver = 'odbc' ";
if (strlen($order_by)> 0) { $sql .= "order by $order_by $order "; }
$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) {
$odbc_num_rows = $row['num_rows'];
$sql = "select * from v_databases ";
$sql .= "where database_driver = 'odbc' ";
$prep_statement = $db->prepare(check_sql($sql));
$prep_statement->execute();
$result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
foreach ($result as &$row) {
$dsn_name = $row["database_name"];
$dsn_username = $row["database_username"];
$dsn_password = $row["database_password"];
break; //limit to 1 row
}
unset ($prep_statement);
}
else {
$odbc_num_rows = '0';
}
}
//config.lua
$fout = fopen($this->config_lua,"w");
if(!$fout){
throw new Exception("Failed to open '".$this->config_lua."' for writing");
}
$tmp = "\n";
$tmp .= "--set the variables\n";
if (strlen($this->global_settings->switch_sounds_dir()) > 0) {
$tmp .= normalize_path_to_os(" sounds_dir = [[".$this->global_settings->switch_sounds_dir()."]];\n");
}
if (strlen($this->global_settings->switch_phrases_vdir()) > 0) {
$tmp .= normalize_path_to_os(" phrases_dir = [[".$this->global_settings->switch_phrases_vdir()."]];\n");
}
if (strlen($this->global_settings->switch_db_dir()) > 0) {
$tmp .= normalize_path_to_os(" database_dir = [[".$this->global_settings->switch_db_dir()."]];\n");
}
if (strlen($this->global_settings->switch_recordings_dir()) > 0) {
$tmp .= normalize_path_to_os(" recordings_dir = [[".$this->global_settings->switch_recordings_dir()."]];\n");
}
if (strlen($this->global_settings->switch_storage_dir()) > 0) {
$tmp .= normalize_path_to_os(" storage_dir = [[".$this->global_settings->switch_storage_dir()."]];\n");
}
if (strlen($this->global_settings->switch_voicemail_vdir()) > 0) {
$tmp .= normalize_path_to_os(" voicemail_dir = [[".$this->global_settings->switch_voicemail_vdir()."]];\n");
}
if (strlen($this->global_settings->switch_script_dir()) > 0) {
$tmp .= normalize_path_to_os(" scripts_dir = [[".$this->global_settings->switch_script_dir()."]];\n");
}
$tmp .= normalize_path_to_os(" php_dir = [[".PHP_BINDIR."]];\n");
if (substr(strtoupper(PHP_OS), 0, 3) == "WIN") {
$tmp .= " php_bin = \"php.exe\";\n";
}
else {
$odbc_num_rows = '0';
$tmp .= " php_bin = \"php\";\n";
}
}
$tmp .= normalize_path_to_os(" document_root = [[".$_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."]];\n");
$tmp .= "\n";
//config.lua
$fout = fopen($this->config_lua,"w");
if(!$fout){
throw new Exception("Failed to open '".$this->config_lua."' for writing");
}
$tmp = "\n";
$tmp .= "--set the variables\n";
if (strlen($this->global_settings->switch_sounds_dir()) > 0) {
$tmp .= normalize_path_to_os(" sounds_dir = [[".$this->global_settings->switch_sounds_dir()."]];\n");
}
if (strlen($this->global_settings->switch_phrases_vdir()) > 0) {
$tmp .= normalize_path_to_os(" phrases_dir = [[".$this->global_settings->switch_phrases_vdir()."]];\n");
}
if (strlen($this->global_settings->switch_db_dir()) > 0) {
$tmp .= normalize_path_to_os(" database_dir = [[".$this->global_settings->switch_db_dir()."]];\n");
}
if (strlen($this->global_settings->switch_recordings_dir()) > 0) {
$tmp .= normalize_path_to_os(" recordings_dir = [[".$this->global_settings->switch_recordings_dir()."]];\n");
}
if (strlen($this->global_settings->switch_storage_dir()) > 0) {
$tmp .= normalize_path_to_os(" storage_dir = [[".$this->global_settings->switch_storage_dir()."]];\n");
}
if (strlen($this->global_settings->switch_voicemail_vdir()) > 0) {
$tmp .= normalize_path_to_os(" voicemail_dir = [[".$this->global_settings->switch_voicemail_vdir()."]];\n");
}
if (strlen($this->global_settings->switch_script_dir()) > 0) {
$tmp .= normalize_path_to_os(" scripts_dir = [[".$this->global_settings->switch_script_dir()."]];\n");
}
$tmp .= normalize_path_to_os(" php_dir = [[".PHP_BINDIR."]];\n");
if (substr(strtoupper(PHP_OS), 0, 3) == "WIN") {
$tmp .= " php_bin = \"php.exe\";\n";
}
else {
$tmp .= " php_bin = \"php\";\n";
}
$tmp .= normalize_path_to_os(" document_root = [[".$_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."]];\n");
$tmp .= "\n";
if ((strlen($this->global_settings->db_type()) > 0) || (strlen($dsn_name) > 0)) {
$tmp .= "--database information\n";
$tmp .= " database = {}\n";
$tmp .= " database[\"type\"] = \"".$this->global_settings->db_type()."\";\n";
$tmp .= " database[\"name\"] = \"".$this->global_settings->db_name()."\";\n";
$tmp .= normalize_path_to_os(" database[\"path\"] = [[".$this->global_settings->db_path()."]];\n");
if ((strlen($this->global_settings->db_type()) > 0) || (strlen($dsn_name) > 0)) {
$tmp .= "--database information\n";
$tmp .= " database = {}\n";
$tmp .= " database[\"type\"] = \"".$this->global_settings->db_type()."\";\n";
$tmp .= " database[\"name\"] = \"".$this->global_settings->db_name()."\";\n";
$tmp .= normalize_path_to_os(" database[\"path\"] = [[".$this->global_settings->db_path()."]];\n");
if (strlen($dsn_name) > 0) {
$tmp .= " database[\"system\"] = \"odbc://".$dsn_name.":".$dsn_username.":".$dsn_password."\";\n";
$tmp .= " database[\"switch\"] = \"odbc://freeswitch:".$dsn_username.":".$dsn_password."\";\n";
if (strlen($dsn_name) > 0) {
$tmp .= " database[\"system\"] = \"odbc://".$dsn_name.":".$dsn_username.":".$dsn_password."\";\n";
$tmp .= " database[\"switch\"] = \"odbc://freeswitch:".$dsn_username.":".$dsn_password."\";\n";
}
elseif ($this->global_settings->db_type() == "pgsql") {
$db_host = $this->global_settings->db_host();
if (filter_var($db_host, FILTER_VALIDATE_IP)) {
$host_type = "hostaddr";
}
else {
$host_type = "host";
}
if($db_host == 'localhost') { $db_host = '127.0.0.1'; } // lua cannot resolve localhost
$tmp .= " database[\"system\"] = \"pgsql://".$host_type."=".$db_host." port=".$this->global_settings->db_port()." dbname=".$this->global_settings->db_name()." user=".$this->global_settings->db_username()." password=".$this->global_settings->db_password()." options='' application_name='".$this->global_settings->db_name()."'\";\n";
$tmp .= " database[\"switch\"] = \"pgsql://".$host_type."=".$db_host." port=".$this->global_settings->db_port()." dbname=freeswitch user=".$this->global_settings->db_username()." password=".$this->global_settings->db_password()." options='' application_name='freeswitch'\";\n";
}
elseif ($this->global_settings->db_type() == "sqlite") {
$tmp .= " database[\"system\"] = \"sqlite://".$this->global_settings->db_path()."/".$this->global_settings->db_name()."\";\n";
$tmp .= " database[\"switch\"] = \"sqlite://".$_SESSION['switch']['db']['dir']."\";\n";
}
elseif ($this->global_settings->db_type() == "mysql") {
$tmp .= " database[\"system\"] = \"\";\n";
$tmp .= " database[\"switch\"] = \"\";\n";
}
$tmp .= "\n";
}
elseif ($this->global_settings->db_type() == "pgsql") {
$lua_db_host = $this->global_settings->db_host();
if($lua_db_host == 'localhost') { $lua_db_host = '127.0.0.1'; } // lua cannot resolve localhost
$tmp .= " database[\"system\"] = \"pgsql://hostaddr=".$lua_db_host." port=".$this->global_settings->db_port()." dbname=".$this->global_settings->db_name()." user=".$this->global_settings->db_username()." password=".$this->global_settings->db_password()." options='' application_name='".$this->global_settings->db_name()."'\";\n";
$tmp .= " database[\"switch\"] = \"pgsql://hostaddr=".$lua_db_host." port=".$this->global_settings->db_port()." dbname=freeswitch user=".$this->global_settings->db_username()." password=".$this->global_settings->db_password()." options='' application_name='freeswitch'\";\n";
}
elseif ($this->global_settings->db_type() == "sqlite") {
$tmp .= " database[\"system\"] = \"sqlite://".$this->global_settings->db_path()."/".$this->global_settings->db_name()."\";\n";
$tmp .= " database[\"switch\"] = \"sqlite://".$_SESSION['switch']['db']['dir']."\";\n";
}
elseif ($this->global_settings->db_type() == "mysql") {
$tmp .= " database[\"system\"] = \"\";\n";
$tmp .= " database[\"switch\"] = \"\";\n";
$tmp .= "--set defaults\n";
$tmp .= " expire = {}\n";
$tmp .= " expire[\"directory\"] = \"3600\";\n";
$tmp .= " expire[\"dialplan\"] = \"3600\";\n";
$tmp .= " expire[\"languages\"] = \"3600\";\n";
$tmp .= " expire[\"sofia.conf\"] = \"3600\";\n";
$tmp .= " expire[\"acl.conf\"] = \"3600\";\n";
$tmp .= "\n";
$tmp .= "--set xml_handler\n";
$tmp .= " xml_handler = {}\n";
$tmp .= " xml_handler[\"fs_path\"] = false;\n";
$tmp .= "\n";
$tmp .= "--set the debug options\n";
$tmp .= " debug[\"params\"] = false;\n";
$tmp .= " debug[\"sql\"] = false;\n";
$tmp .= " debug[\"xml_request\"] = false;\n";
$tmp .= " debug[\"xml_string\"] = false;\n";
$tmp .= " debug[\"cache\"] = false;\n";
$tmp .= "\n";
$tmp .= "--additional info\n";
$tmp .= " domain_count = ".$this->global_settings->domain_count().";\n";
$tmp .= normalize_path_to_os(" temp_dir = [[".$this->global_settings->switch_temp_dir()."]];\n");
if (isset($_SESSION['domain']['dial_string']['text'])) {
$tmp .= " dial_string = \"".$_SESSION['domain']['dial_string']['text']."\";\n";
}
$tmp .= "\n";
}
$tmp .= "--set defaults\n";
$tmp .= " expire = {}\n";
$tmp .= " expire[\"directory\"] = \"3600\";\n";
$tmp .= " expire[\"dialplan\"] = \"3600\";\n";
$tmp .= " expire[\"languages\"] = \"3600\";\n";
$tmp .= " expire[\"sofia.conf\"] = \"3600\";\n";
$tmp .= " expire[\"acl.conf\"] = \"3600\";\n";
$tmp .= "\n";
$tmp .= "--set xml_handler\n";
$tmp .= " xml_handler = {}\n";
$tmp .= " xml_handler[\"fs_path\"] = false;\n";
$tmp .= "\n";
$tmp .= "--set the debug options\n";
$tmp .= " debug[\"params\"] = false;\n";
$tmp .= " debug[\"sql\"] = false;\n";
$tmp .= " debug[\"xml_request\"] = false;\n";
$tmp .= " debug[\"xml_string\"] = false;\n";
$tmp .= " debug[\"cache\"] = false;\n";
$tmp .= "\n";
$tmp .= "--additional info\n";
$tmp .= " domain_count = ".$this->global_settings->domain_count().";\n";
$tmp .= normalize_path_to_os(" temp_dir = [[".$this->global_settings->switch_temp_dir()."]];\n");
if (isset($_SESSION['domain']['dial_string']['text'])) {
$tmp .= " dial_string = \"".$_SESSION['domain']['dial_string']['text']."\";\n";
}
$tmp .= "\n";
$tmp .= "--include local.lua\n";
$tmp .= " require(\"resources.functions.file_exists\");\n";
$tmp .= " if (file_exists(\"/etc/fusionpbx/local.lua\")) then\n";
$tmp .= " dofile(\"/etc/fusionpbx/local.lua\");\n";
$tmp .= " elseif (file_exists(\"/usr/local/etc/fusionpbx/local.lua\")) then\n";
$tmp .= " dofile(\"/usr/local/etc/fusionpbx/local.lua\");\n";
$tmp .= " elseif (file_exists(scripts_dir..\"/resources/local.lua\")) then\n";
$tmp .= " require(\"resources.local\");\n";
$tmp .= " end\n";
fwrite($fout, $tmp);
unset($tmp);
fclose($fout);
$tmp .= "--include local.lua\n";
$tmp .= " require(\"resources.functions.file_exists\");\n";
$tmp .= " if (file_exists(\"/etc/fusionpbx/local.lua\")) then\n";
$tmp .= " dofile(\"/etc/fusionpbx/local.lua\");\n";
$tmp .= " elseif (file_exists(\"/usr/local/etc/fusionpbx/local.lua\")) then\n";
$tmp .= " dofile(\"/usr/local/etc/fusionpbx/local.lua\");\n";
$tmp .= " elseif (file_exists(scripts_dir..\"/resources/local.lua\")) then\n";
$tmp .= " require(\"resources.local\");\n";
$tmp .= " end\n";
fwrite($fout, $tmp);
unset($tmp);
fclose($fout);
}
protected function restart_switch() {