Пример #1
0
 /**
  * Add a dialplan for call center
  * @var string $domain_uuid		the multi-tenant id
  * @var string $value	string to be cached
  */
 public function dialplan()
 {
     //normalize the fax forward number
     if (strlen($this->fax_forward_number) > 3) {
         //$fax_forward_number = preg_replace("~[^0-9]~", "",$fax_forward_number);
         $this->fax_forward_number = str_replace(" ", "", $this->fax_forward_number);
         $this->fax_forward_number = str_replace("-", "", $this->fax_forward_number);
     }
     //set the forward prefix
     if (strripos($this->fax_forward_number, '$1') === false) {
         $this->forward_prefix = '';
         //not found
     } else {
         $this->forward_prefix = $this->forward_prefix . $this->fax_forward_number . '#';
         //found
     }
     //delete previous dialplan
     if (strlen($this->dialplan_uuid) > 0) {
         //delete the previous dialplan
         $sql = "delete from v_dialplans ";
         $sql .= "where dialplan_uuid = '" . $this->dialplan_uuid . "' ";
         $sql .= "and domain_uuid = '" . $this->domain_uuid . "' ";
         $this->db->exec($sql);
         $sql = "delete from v_dialplan_details ";
         $sql .= "where dialplan_uuid = '" . $this->dialplan_uuid . "' ";
         $sql .= "and domain_uuid = '" . $this->domain_uuid . "' ";
         $this->db->exec($sql);
         unset($sql);
     }
     unset($prep_statement);
     //build the dialplan array
     $dialplan["app_uuid"] = "24108154-4ac3-1db6-1551-4731703a4440";
     $dialplan["domain_uuid"] = $this->domain_uuid;
     $dialplan["dialplan_name"] = $this->fax_name != '' ? $this->fax_name : format_phone($this->destination_number);
     $dialplan["dialplan_number"] = $this->fax_extension;
     $dialplan["dialplan_context"] = $_SESSION['context'];
     $dialplan["dialplan_continue"] = "false";
     $dialplan["dialplan_order"] = "310";
     $dialplan["dialplan_enabled"] = "true";
     $dialplan["dialplan_description"] = $this->fax_description;
     $dialplan_detail_order = 10;
     //add the public condition
     $y = 1;
     $dialplan["dialplan_details"][$y]["domain_uuid"] = $this->domain_uuid;
     $dialplan["dialplan_details"][$y]["dialplan_detail_tag"] = "condition";
     $dialplan["dialplan_details"][$y]["dialplan_detail_type"] = "destination_number";
     $dialplan["dialplan_details"][$y]["dialplan_detail_data"] = "^" . $this->destination_number . "\$";
     $dialplan["dialplan_details"][$y]["dialplan_detail_break"] = "";
     $dialplan["dialplan_details"][$y]["dialplan_detail_group"] = "1";
     $dialplan["dialplan_details"][$y]["dialplan_detail_order"] = $y * 10;
     $y++;
     $dialplan["dialplan_details"][$y]["domain_uuid"] = $this->domain_uuid;
     $dialplan["dialplan_details"][$y]["dialplan_detail_tag"] = "action";
     $dialplan["dialplan_details"][$y]["dialplan_detail_type"] = "answer";
     $dialplan["dialplan_details"][$y]["dialplan_detail_data"] = "";
     $dialplan["dialplan_details"][$y]["dialplan_detail_group"] = "1";
     $dialplan["dialplan_details"][$y]["dialplan_detail_order"] = $y * 10;
     $y++;
     $dialplan["dialplan_details"][$y]["domain_uuid"] = $this->domain_uuid;
     $dialplan["dialplan_details"][$y]["dialplan_detail_tag"] = "action";
     $dialplan["dialplan_details"][$y]["dialplan_detail_type"] = "set";
     $dialplan["dialplan_details"][$y]["dialplan_detail_data"] = "fax_uuid=" . $this->fax_uuid;
     $dialplan["dialplan_details"][$y]["dialplan_detail_group"] = "1";
     $dialplan["dialplan_details"][$y]["dialplan_detail_order"] = $y * 10;
     $y++;
     $dialplan["dialplan_details"][$y]["domain_uuid"] = $this->domain_uuid;
     $dialplan["dialplan_details"][$y]["dialplan_detail_tag"] = "action";
     $dialplan["dialplan_details"][$y]["dialplan_detail_type"] = "set";
     $dialplan["dialplan_details"][$y]["dialplan_detail_data"] = "api_hangup_hook=lua app/fax/resources/scripts/hangup_rx.lua";
     $dialplan["dialplan_details"][$y]["dialplan_detail_group"] = "1";
     $dialplan["dialplan_details"][$y]["dialplan_detail_order"] = $y * 10;
     $y++;
     foreach ($_SESSION['fax']['variable'] as $data) {
         $dialplan["dialplan_details"][$y]["domain_uuid"] = $this->domain_uuid;
         $dialplan["dialplan_details"][$y]["dialplan_detail_tag"] = "action";
         $dialplan["dialplan_details"][$y]["dialplan_detail_type"] = "set";
         if (substr($data, 0, 8) == "inbound:") {
             $dialplan["dialplan_details"][$y]["dialplan_detail_data"] = substr($data, 8, strlen($data));
         } elseif (substr($data, 0, 9) == "outbound:") {
         } else {
             $dialplan["dialplan_details"][$y]["dialplan_detail_data"] = $data;
         }
         $dialplan["dialplan_details"][$y]["dialplan_detail_group"] = "1";
         $dialplan["dialplan_details"][$y]["dialplan_detail_order"] = $y * 10;
         $y++;
     }
     $dialplan["dialplan_details"][$y]["domain_uuid"] = $this->domain_uuid;
     $dialplan["dialplan_details"][$y]["dialplan_detail_tag"] = "action";
     $dialplan["dialplan_details"][$y]["dialplan_detail_type"] = "set";
     if (strlen($_SESSION['fax']['last_fax']['text']) > 0) {
         $dialplan["dialplan_details"][$y]["dialplan_detail_data"] = "last_fax=" . $_SESSION['fax']['last_fax']['text'];
     } else {
         $dialplan["dialplan_details"][$y]["dialplan_detail_data"] = "last_fax=\${caller_id_number}-\${strftime(%Y-%m-%d-%H-%M-%S)}";
     }
     $dialplan["dialplan_details"][$y]["dialplan_detail_group"] = "1";
     $dialplan["dialplan_details"][$y]["dialplan_detail_order"] = $y * 10;
     $y++;
     $dialplan["dialplan_details"][$y]["domain_uuid"] = $this->domain_uuid;
     $dialplan["dialplan_details"][$y]["dialplan_detail_tag"] = "action";
     $dialplan["dialplan_details"][$y]["dialplan_detail_type"] = "playback";
     $dialplan["dialplan_details"][$y]["dialplan_detail_data"] = "silence_stream://2000";
     $dialplan["dialplan_details"][$y]["dialplan_detail_group"] = "1";
     $dialplan["dialplan_details"][$y]["dialplan_detail_order"] = $y * 10;
     $y++;
     $dialplan["dialplan_details"][$y]["domain_uuid"] = $this->domain_uuid;
     $dialplan["dialplan_details"][$y]["dialplan_detail_tag"] = "action";
     $dialplan["dialplan_details"][$y]["dialplan_detail_type"] = "rxfax";
     $dialplan["dialplan_details"][$y]["dialplan_detail_data"] = $_SESSION['switch']['storage']['dir'] . '/fax/' . $_SESSION['domain_name'] . '/' . $this->fax_extension . '/inbox/' . $this->forward_prefix . '${last_fax}.tif';
     $dialplan["dialplan_details"][$y]["dialplan_detail_group"] = "1";
     $dialplan["dialplan_details"][$y]["dialplan_detail_order"] = $y * 10;
     $y++;
     $dialplan["dialplan_details"][$y]["domain_uuid"] = $this->domain_uuid;
     $dialplan["dialplan_details"][$y]["dialplan_detail_tag"] = "action";
     $dialplan["dialplan_details"][$y]["dialplan_detail_type"] = "hangup";
     $dialplan["dialplan_details"][$y]["dialplan_detail_data"] = "";
     $dialplan["dialplan_details"][$y]["dialplan_detail_group"] = "1";
     $dialplan["dialplan_details"][$y]["dialplan_detail_order"] = $y * 10;
     $y++;
     //add the dialplan permission
     $p = new permissions();
     $p->add("dialplan_add", 'temp');
     $p->add("dialplan_detail_add", 'temp');
     $p->add("dialplan_edit", 'temp');
     $p->add("dialplan_detail_edit", 'temp');
     //save the dialplan
     $orm = new orm();
     $orm->name('dialplans');
     $orm->save($dialplan);
     $dialplan_response = $orm->message;
     $this->dialplan_uuid = $dialplan_response['uuid'];
     //if new dialplan uuid then update the call center queue
     $sql = "update v_fax ";
     $sql .= "set dialplan_uuid = '" . $this->dialplan_uuid . "' ";
     $sql .= "where fax_uuid = '" . $this->fax_uuid . "' ";
     $sql .= "and domain_uuid = '" . $this->domain_uuid . "' ";
     $this->db->exec($sql);
     unset($sql);
     //remove the temporary permission
     $p->delete("dialplan_add", 'temp');
     $p->delete("dialplan_detail_add", 'temp');
     $p->delete("dialplan_edit", 'temp');
     $p->delete("dialplan_detail_edit", 'temp');
     //synchronize the xml config
     save_dialplan_xml();
     //clear the cache
     $cache = new cache();
     $cache->delete("dialplan:" . $_SESSION['context']);
     //return the dialplan_uuid
     return $dialplan_response;
 }
Пример #2
0
         //update dialplan detail action
         $dialplan_detail_tag = 'action';
         //condition, action, antiaction
         $dialplan_detail_type = 'set';
         $dialplan_detail_data = "api_hangup_hook=lua app/fax/resources/scripts/hangup_rx.lua";
         $sql = "update v_dialplan_details set ";
         $sql .= "dialplan_detail_data = '" . check_str($dialplan_detail_data) . "' ";
         $sql .= "where domain_uuid = '" . $_SESSION['domain_uuid'] . "' ";
         $sql .= "and dialplan_detail_tag = 'action' ";
         $sql .= "and dialplan_detail_type = 'set' ";
         $sql .= "and dialplan_uuid = '{$dialplan_uuid}' ";
         $sql .= "and dialplan_detail_data like 'api_hangup_hook=%' ";
         $db->query(check_sql($sql));
     }
     //save the xml
     save_dialplan_xml();
     //apply settings reminder
     $_SESSION["reload_xml"] = true;
     //clear the cache
     $cache = new cache();
     $cache->delete("dialplan:" . $_SESSION["context"]);
     //redirect the browser
     if ($action == "update" && permission_exists('fax_extension_edit')) {
         $_SESSION["message"] = $text['confirm-update'];
     }
     if ($action == "add" && permission_exists('fax_extension_add')) {
         $_SESSION["message"] = $text['confirm-add'];
     }
     header("Location: fax.php");
     return;
 }
Пример #3
0
 function save_switch_xml()
 {
     if (is_readable($_SESSION['switch']['dialplan']['dir'])) {
         save_dialplan_xml();
     }
     if (is_readable($_SESSION['switch']['extensions']['dir'])) {
         if (file_exists($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/app/extensions/resources/classes/extension.php")) {
             require_once $_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "app/extensions/resources/classes/extension.php";
             $extension = new extension();
             $extension->xml();
         }
     }
     if (is_readable($_SESSION['switch']['conf']['dir'])) {
         if (file_exists($_SERVER['DOCUMENT_ROOT'] . PROJECT_PATH . "/app/settings/app_config.php")) {
             save_setting_xml();
         }
         if (file_exists($_SERVER['DOCUMENT_ROOT'] . PROJECT_PATH . "/app/modules/app_config.php")) {
             save_module_xml();
         }
         if (file_exists($_SERVER['DOCUMENT_ROOT'] . PROJECT_PATH . "/app/vars/app_config.php")) {
             save_var_xml();
         }
         if (file_exists($_SERVER['DOCUMENT_ROOT'] . PROJECT_PATH . "/app/call_center/app_config.php")) {
             save_call_center_xml();
         }
         if (file_exists($_SERVER['DOCUMENT_ROOT'] . PROJECT_PATH . "/app/gateways/app_config.php")) {
             save_gateway_xml();
         }
         //if (file_exists($_SERVER['DOCUMENT_ROOT'].PROJECT_PATH."/app/ivr_menu/app_config.php")) {
         //	save_ivr_menu_xml();
         //}
         if (file_exists($_SERVER['DOCUMENT_ROOT'] . PROJECT_PATH . "/app/sip_profiles/app_config.php")) {
             save_sip_profile_xml();
         }
     }
 }
Пример #4
0
 public function upgrade()
 {
     //get the db variables
     $config = new config();
     $config_exists = $config->exists();
     $config_path = $config->find();
     $config->get();
     $db_type = $config->db_type;
     $db_name = $config->db_name;
     $db_username = $config->db_username;
     $db_password = $config->db_password;
     $db_host = $config->db_host;
     $db_path = $config->db_path;
     $db_port = $config->db_port;
     //get the PROJECT PATH
     include "root.php";
     //get the list of installed apps from the core and app directories (note: GLOB_BRACE doesn't work on some systems)
     $config_list_1 = glob($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/*/*/app_config.php");
     $config_list_2 = glob($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/*/*/app_menu.php");
     $config_list = array_merge((array) $config_list_1, (array) $config_list_2);
     unset($config_list_1, $config_list_2);
     $db = $this->db;
     $x = 0;
     foreach ($config_list as &$config_path) {
         include $config_path;
         $x++;
     }
     //get the domains
     $sql = "select * from v_domains ";
     $prep_statement = $this->db->prepare($sql);
     $prep_statement->execute();
     $domains = $prep_statement->fetchAll(PDO::FETCH_NAMED);
     unset($prep_statement);
     //get the domain_settings
     $sql = "select * from v_domain_settings ";
     $sql .= "where domain_setting_enabled = 'true' ";
     $prep_statement = $this->db->prepare($sql);
     $prep_statement->execute();
     $domain_settings = $prep_statement->fetchAll(PDO::FETCH_NAMED);
     unset($prep_statement);
     //get the default settings
     $sql = "select * from v_default_settings ";
     $sql .= "where default_setting_enabled = 'true' ";
     $prep_statement = $this->db->prepare($sql);
     $prep_statement->execute();
     $database_default_settings = $prep_statement->fetchAll(PDO::FETCH_NAMED);
     unset($prep_statement);
     //get the domain_uuid
     foreach ($domains as $row) {
         if (count($domains) == 1) {
             $_SESSION["domain_uuid"] = $row["domain_uuid"];
             $_SESSION["domain_name"] = $row['domain_name'];
         } else {
             if (lower_case($row['domain_name']) == lower_case($domain_array[0]) || lower_case($row['domain_name']) == lower_case('www.' . $domain_array[0])) {
                 $_SESSION["domain_uuid"] = $row["domain_uuid"];
                 $_SESSION["domain_name"] = $row['domain_name'];
             }
             $_SESSION['domains'][$row['domain_uuid']]['domain_uuid'] = $row['domain_uuid'];
             $_SESSION['domains'][$row['domain_uuid']]['domain_name'] = $row['domain_name'];
         }
     }
     //loop through all domains
     $domain_count = count($domains);
     $domains_processed = 1;
     foreach ($domains as &$row) {
         //get the values from database and set them as php variables
         $domain_uuid = $row["domain_uuid"];
         $domain_name = $row["domain_name"];
         //get the context
         $context = $domain_name;
         //show the domain when display_type is set to text
         if ($display_type == "text") {
             echo "\n";
             echo $domain_name;
             echo "\n";
         }
         //get the default settings - this needs to be done to reset the session values back to the defaults for each domain in the loop
         foreach ($database_default_settings as $row) {
             $name = $row['default_setting_name'];
             $category = $row['default_setting_category'];
             $subcategory = $row['default_setting_subcategory'];
             if (strlen($subcategory) == 0) {
                 if ($name == "array") {
                     $_SESSION[$category][] = $row['default_setting_value'];
                 } else {
                     $_SESSION[$category][$name] = $row['default_setting_value'];
                 }
             } else {
                 if ($name == "array") {
                     $_SESSION[$category][$subcategory][] = $row['default_setting_value'];
                 } else {
                     $_SESSION[$category][$subcategory]['uuid'] = $row['default_setting_uuid'];
                     $_SESSION[$category][$subcategory][$name] = $row['default_setting_value'];
                 }
             }
         }
         //get the domains settings for the current domain
         foreach ($domain_settings as $row) {
             if ($row['domain_uuid'] == $domain_uuid) {
                 $name = $row['domain_setting_name'];
                 $category = $row['domain_setting_category'];
                 $subcategory = $row['domain_setting_subcategory'];
                 if (strlen($subcategory) == 0) {
                     //$$category[$name] = $row['domain_setting_value'];
                     $_SESSION[$category][$name] = $row['domain_setting_value'];
                 } else {
                     //$$category[$subcategory][$name] = $row['domain_setting_value'];
                     $_SESSION[$category][$subcategory][$name] = $row['domain_setting_value'];
                 }
             }
         }
         //get the list of installed apps from the core and mod directories and execute the php code in app_defaults.php
         $default_list = glob($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/*/*/app_defaults.php");
         foreach ($default_list as &$default_path) {
             include $default_path;
         }
         //track of the number of domains processed
         $domains_processed++;
     }
     //synchronize the dialplan
     if (function_exists('save_dialplan_xml')) {
         save_dialplan_xml();
     }
     //update config.lua
     if (file_exists($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . '/app/scripts/resources/classes/scripts.php')) {
         $obj = new scripts();
         $obj->write_config();
     }
     //clear the session variables
     unset($_SESSION['domain']);
     unset($_SESSION['switch']);
 }
Пример #5
0
 /**
  * Add a dialplan for call center
  * @var string $domain_uuid		the multi-tenant id
  * @var string $value	string to be cached
  */
 public function dialplan()
 {
     //delete previous dialplan
     if (strlen($this->dialplan_uuid) > 0) {
         //delete the previous dialplan
         $sql = "delete from v_dialplans ";
         $sql .= "where dialplan_uuid = '" . $this->dialplan_uuid . "' ";
         $sql .= "and domain_uuid = '" . $this->domain_uuid . "' ";
         $this->db->exec($sql);
         $sql = "delete from v_dialplan_details ";
         $sql .= "where dialplan_uuid = '" . $this->dialplan_uuid . "' ";
         $sql .= "and domain_uuid = '" . $this->domain_uuid . "' ";
         $this->db->exec($sql);
         unset($sql);
     }
     unset($prep_statement);
     //build the dialplan array
     $dialplan["app_uuid"] = "95788e50-9500-079e-2807-fd530b0ea370";
     $dialplan["domain_uuid"] = $this->domain_uuid;
     $dialplan["dialplan_name"] = $this->queue_name != '' ? $this->queue_name : format_phone($this->destination_number);
     $dialplan["dialplan_number"] = $this->destination_number;
     $dialplan["dialplan_context"] = $_SESSION['context'];
     $dialplan["dialplan_continue"] = "false";
     $dialplan["dialplan_order"] = "210";
     $dialplan["dialplan_enabled"] = "true";
     $dialplan["dialplan_description"] = $this->queue_description;
     $dialplan_detail_order = 10;
     //add the public condition
     $y = 1;
     $dialplan["dialplan_details"][$y]["domain_uuid"] = $this->domain_uuid;
     $dialplan["dialplan_details"][$y]["dialplan_detail_tag"] = "condition";
     $dialplan["dialplan_details"][$y]["dialplan_detail_type"] = "\${caller_id_name}";
     $dialplan["dialplan_details"][$y]["dialplan_detail_data"] = "^([^#]+#)(.*)\$";
     $dialplan["dialplan_details"][$y]["dialplan_detail_break"] = "never";
     $dialplan["dialplan_details"][$y]["dialplan_detail_group"] = "1";
     $dialplan["dialplan_details"][$y]["dialplan_detail_order"] = $y * 10;
     $y++;
     $dialplan["dialplan_details"][$y]["domain_uuid"] = $this->domain_uuid;
     $dialplan["dialplan_details"][$y]["dialplan_detail_tag"] = "action";
     $dialplan["dialplan_details"][$y]["dialplan_detail_type"] = "set";
     $dialplan["dialplan_details"][$y]["dialplan_detail_data"] = "caller_id_name=\$2";
     $dialplan["dialplan_details"][$y]["dialplan_detail_group"] = "1";
     $dialplan["dialplan_details"][$y]["dialplan_detail_order"] = $y * 10;
     $y++;
     $dialplan["dialplan_details"][$y]["domain_uuid"] = $this->domain_uuid;
     $dialplan["dialplan_details"][$y]["dialplan_detail_tag"] = "condition";
     $dialplan["dialplan_details"][$y]["dialplan_detail_type"] = "destination_number";
     $dialplan["dialplan_details"][$y]["dialplan_detail_data"] = "^" . $this->destination_number . "\$";
     $dialplan["dialplan_details"][$y]["dialplan_detail_break"] = "";
     $dialplan["dialplan_details"][$y]["dialplan_detail_group"] = "2";
     $dialplan["dialplan_details"][$y]["dialplan_detail_order"] = $y * 10;
     $y++;
     $dialplan["dialplan_details"][$y]["domain_uuid"] = $this->domain_uuid;
     $dialplan["dialplan_details"][$y]["dialplan_detail_tag"] = "action";
     $dialplan["dialplan_details"][$y]["dialplan_detail_type"] = "answer";
     $dialplan["dialplan_details"][$y]["dialplan_detail_data"] = "";
     $dialplan["dialplan_details"][$y]["dialplan_detail_group"] = "2";
     $dialplan["dialplan_details"][$y]["dialplan_detail_order"] = $y * 10;
     $y++;
     $dialplan["dialplan_details"][$y]["domain_uuid"] = $this->domain_uuid;
     $dialplan["dialplan_details"][$y]["dialplan_detail_tag"] = "action";
     $dialplan["dialplan_details"][$y]["dialplan_detail_type"] = "set";
     $dialplan["dialplan_details"][$y]["dialplan_detail_data"] = "hangup_after_bridge=true";
     $dialplan["dialplan_details"][$y]["dialplan_detail_group"] = "2";
     $dialplan["dialplan_details"][$y]["dialplan_detail_order"] = $y * 10;
     $y++;
     if (strlen($this->queue_cid_prefix) > 0) {
         $dialplan["dialplan_details"][$y]["domain_uuid"] = $this->domain_uuid;
         $dialplan["dialplan_details"][$y]["dialplan_detail_tag"] = "action";
         $dialplan["dialplan_details"][$y]["dialplan_detail_type"] = "set";
         $dialplan["dialplan_details"][$y]["dialplan_detail_data"] = "effective_caller_id_name=" . $this->queue_cid_prefix . "#\${caller_id_name}";
         $dialplan["dialplan_details"][$y]["dialplan_detail_group"] = "2";
         $dialplan["dialplan_details"][$y]["dialplan_detail_order"] = $y * 10;
         $y++;
     }
     $dialplan["dialplan_details"][$y]["domain_uuid"] = $this->domain_uuid;
     $dialplan["dialplan_details"][$y]["dialplan_detail_tag"] = "action";
     $dialplan["dialplan_details"][$y]["dialplan_detail_type"] = "callcenter";
     $dialplan["dialplan_details"][$y]["dialplan_detail_data"] = $this->queue_name . '@' . $_SESSION["domain_name"];
     $dialplan["dialplan_details"][$y]["dialplan_detail_group"] = "2";
     $dialplan["dialplan_details"][$y]["dialplan_detail_order"] = $y * 10;
     $y++;
     if (strlen($this->queue_timeout_action) > 0) {
         $action_array = explode(":", $this->queue_timeout_action);
         $dialplan["dialplan_details"][$y]["domain_uuid"] = $this->domain_uuid;
         $dialplan["dialplan_details"][$y]["dialplan_detail_tag"] = "action";
         $dialplan["dialplan_details"][$y]["dialplan_detail_type"] = $action_array[0];
         $dialplan["dialplan_details"][$y]["dialplan_detail_data"] = substr($this->queue_timeout_action, strlen($action_array[0]) + 1, strlen($this->queue_timeout_action));
         $dialplan["dialplan_details"][$y]["dialplan_detail_group"] = "2";
         $dialplan["dialplan_details"][$y]["dialplan_detail_order"] = $y * 10;
         $y++;
     }
     $dialplan["dialplan_details"][$y]["domain_uuid"] = $this->domain_uuid;
     $dialplan["dialplan_details"][$y]["dialplan_detail_tag"] = "action";
     $dialplan["dialplan_details"][$y]["dialplan_detail_type"] = "hangup";
     $dialplan["dialplan_details"][$y]["dialplan_detail_data"] = "";
     $dialplan["dialplan_details"][$y]["dialplan_detail_group"] = "2";
     $dialplan["dialplan_details"][$y]["dialplan_detail_order"] = $y * 10;
     //add the dialplan permission
     $p = new permissions();
     $p->add("dialplan_add", 'temp');
     $p->add("dialplan_detail_add", 'temp');
     $p->add("dialplan_edit", 'temp');
     $p->add("dialplan_detail_edit", 'temp');
     //save the dialplan
     $orm = new orm();
     $orm->name('dialplans');
     $orm->save($dialplan);
     $dialplan_response = $orm->message;
     $this->dialplan_uuid = $dialplan_response['uuid'];
     //if new dialplan uuid then update the call center queue
     $sql = "update v_call_center_queues ";
     $sql .= "set dialplan_uuid = '" . $this->dialplan_uuid . "' ";
     $sql .= "where call_center_queue_uuid = '" . $this->call_center_queue_uuid . "' ";
     $sql .= "and domain_uuid = '" . $this->domain_uuid . "' ";
     $this->db->exec($sql);
     unset($sql);
     //remove the temporary permission
     $p->delete("dialplan_add", 'temp');
     $p->delete("dialplan_detail_add", 'temp');
     $p->delete("dialplan_edit", 'temp');
     $p->delete("dialplan_detail_edit", 'temp');
     //synchronize the xml config
     save_dialplan_xml();
     //clear the cache
     $cache = new cache();
     $cache->delete("dialplan:" . $_SESSION['context']);
     //return the dialplan_uuid
     return $dialplan_response;
 }
Пример #6
0
 function save_ivr_menu_xml()
 {
     global $db, $domain_uuid;
     //prepare for dialplan .xml files to be written. delete all dialplan files that are prefixed with dialplan_ and have a file extension of .xml
     if (count($_SESSION["domains"]) > 1) {
         $v_needle = 'v_' . $_SESSION['domain_name'] . '_';
     } else {
         $v_needle = 'v_';
     }
     if ($dh = opendir($_SESSION['switch']['conf']['dir'] . "/ivr_menus/")) {
         $files = array();
         while ($file = readdir($dh)) {
             if ($file != "." && $file != ".." && $file[0] != '.') {
                 if (is_dir($dir . "/" . $file)) {
                     //this is a directory
                 } else {
                     if (strpos($file, $v_needle) !== false && substr($file, -4) == '.xml') {
                         //echo "file: $file<br />\n";
                         unlink($_SESSION['switch']['conf']['dir'] . "/ivr_menus/" . $file);
                     }
                 }
             }
         }
         closedir($dh);
     }
     $sql = "select * from v_ivr_menus ";
     $sql .= " where domain_uuid = '{$domain_uuid}' ";
     $prep_statement = $db->prepare(check_sql($sql));
     $prep_statement->execute();
     $result = $prep_statement->fetchAll(PDO::FETCH_ASSOC);
     $result_count = count($result);
     unset($prep_statement, $sql);
     if ($result_count > 0) {
         foreach ($result as $row) {
             $dialplan_uuid = $row["dialplan_uuid"];
             $ivr_menu_uuid = $row["ivr_menu_uuid"];
             $ivr_menu_name = check_str($row["ivr_menu_name"]);
             $ivr_menu_extension = $row["ivr_menu_extension"];
             $ivr_menu_greet_long = $row["ivr_menu_greet_long"];
             $ivr_menu_greet_short = $row["ivr_menu_greet_short"];
             $ivr_menu_invalid_sound = $row["ivr_menu_invalid_sound"];
             $ivr_menu_exit_sound = $row["ivr_menu_exit_sound"];
             $ivr_menu_confirm_macro = $row["ivr_menu_confirm_macro"];
             $ivr_menu_confirm_key = $row["ivr_menu_confirm_key"];
             $ivr_menu_tts_engine = $row["ivr_menu_tts_engine"];
             $ivr_menu_tts_voice = $row["ivr_menu_tts_voice"];
             $ivr_menu_confirm_attempts = $row["ivr_menu_confirm_attempts"];
             $ivr_menu_timeout = $row["ivr_menu_timeout"];
             $ivr_menu_exit_app = $row["ivr_menu_exit_app"];
             $ivr_menu_exit_data = $row["ivr_menu_exit_data"];
             $ivr_menu_inter_digit_timeout = $row["ivr_menu_inter_digit_timeout"];
             $ivr_menu_max_failures = $row["ivr_menu_max_failures"];
             $ivr_menu_max_timeouts = $row["ivr_menu_max_timeouts"];
             $ivr_menu_digit_len = $row["ivr_menu_digit_len"];
             $ivr_menu_direct_dial = $row["ivr_menu_direct_dial"];
             $ivr_menu_enabled = $row["ivr_menu_enabled"];
             $ivr_menu_description = check_str($row["ivr_menu_description"]);
             //replace space with an underscore
             $ivr_menu_name = str_replace(" ", "_", $ivr_menu_name);
             //add each IVR menu to the XML config
             $tmp = "<include>\n";
             if (strlen($ivr_menu_description) > 0) {
                 $tmp .= "\t<!-- {$ivr_menu_description} -->\n";
             }
             if (count($_SESSION["domains"]) > 1) {
                 $tmp .= "\t<menu name=\"" . $_SESSION['domains'][$domain_uuid]['domain_name'] . "-" . $ivr_menu_name . "\"\n";
             } else {
                 $tmp .= "\t<menu name=\"{$ivr_menu_name}\"\n";
             }
             if (stripos($ivr_menu_greet_long, 'mp3') !== false || stripos($ivr_menu_greet_long, 'wav') !== false) {
                 //found wav or mp3
                 $tmp .= "\t\tgreet-long=\"" . $ivr_menu_greet_long . "\"\n";
             } else {
                 //not found
                 $tmp .= "\t\tgreet-long=\"" . $ivr_menu_greet_long . "\"\n";
             }
             if (stripos($ivr_menu_greet_short, 'mp3') !== false || stripos($ivr_menu_greet_short, 'wav') !== false) {
                 if (strlen($ivr_menu_greet_short) > 0) {
                     $tmp .= "\t\tgreet-short=\"" . $ivr_menu_greet_short . "\"\n";
                 }
             } else {
                 //not found
                 if (strlen($ivr_menu_greet_short) > 0) {
                     $tmp .= "\t\tgreet-short=\"" . $ivr_menu_greet_short . "\"\n";
                 }
             }
             $tmp .= "\t\tinvalid-sound=\"{$ivr_menu_invalid_sound}\"\n";
             $tmp .= "\t\texit-sound=\"{$ivr_menu_exit_sound}\"\n";
             $tmp .= "\t\tconfirm-macro=\"{$ivr_menu_confirm_macro}\"\n";
             $tmp .= "\t\tconfirm-key=\"{$ivr_menu_confirm_key}\"\n";
             $tmp .= "\t\ttts-engine=\"{$ivr_menu_tts_engine}\"\n";
             $tmp .= "\t\ttts-voice=\"{$ivr_menu_tts_voice}\"\n";
             $tmp .= "\t\tconfirm-attempts=\"{$ivr_menu_confirm_attempts}\"\n";
             $tmp .= "\t\ttimeout=\"{$ivr_menu_timeout}\"\n";
             $tmp .= "\t\tinter-digit-timeout=\"{$ivr_menu_inter_digit_timeout}\"\n";
             $tmp .= "\t\tmax-failures=\"{$ivr_menu_max_failures}\"\n";
             $tmp .= "\t\tmax-timeouts=\"{$ivr_menu_max_timeouts}\"\n";
             $tmp .= "\t\tdigit-len=\"{$ivr_menu_digit_len}\">\n";
             $sub_sql = "select * from v_ivr_menu_options ";
             $sub_sql .= "where ivr_menu_uuid = '{$ivr_menu_uuid}' ";
             $sub_sql .= "and domain_uuid = '{$domain_uuid}' ";
             $sub_sql .= "order by ivr_menu_option_order asc ";
             $sub_prep_statement = $db->prepare(check_sql($sub_sql));
             $sub_prep_statement->execute();
             $sub_result = $sub_prep_statement->fetchAll(PDO::FETCH_ASSOC);
             foreach ($sub_result as &$sub_row) {
                 //$ivr_menu_uuid = $sub_row["ivr_menu_uuid"];
                 $ivr_menu_option_digits = $sub_row["ivr_menu_option_digits"];
                 $ivr_menu_option_action = $sub_row["ivr_menu_option_action"];
                 $ivr_menu_option_param = $sub_row["ivr_menu_option_param"];
                 $ivr_menu_option_description = $sub_row["ivr_menu_option_description"];
                 $tmp .= "\t\t<entry action=\"{$ivr_menu_option_action}\" digits=\"{$ivr_menu_option_digits}\" param=\"{$ivr_menu_option_param}\"/>";
                 if (strlen($ivr_menu_option_description) == 0) {
                     $tmp .= "\n";
                 } else {
                     $tmp .= "\t<!-- {$ivr_menu_option_description} -->\n";
                 }
             }
             unset($sub_prep_statement, $sub_row);
             if ($ivr_menu_direct_dial == "true") {
                 $tmp .= "\t\t<entry action=\"menu-exec-app\" digits=\"/(^\\d{3,6}\$)/\" param=\"transfer \$1 XML " . $_SESSION["context"] . "\"/>\n";
             }
             $tmp .= "\t</menu>\n";
             $tmp .= "</include>\n";
             //remove invalid characters from the file names
             $ivr_menu_name = str_replace(" ", "_", $ivr_menu_name);
             $ivr_menu_name = preg_replace("/[\\*\\:\\/\\<\\>\\|\\'\"\\?]/", "", $ivr_menu_name);
             //write the file
             if (count($_SESSION["domains"]) > 1) {
                 $fout = fopen($_SESSION['switch']['conf']['dir'] . "/ivr_menus/v_" . $_SESSION['domains'][$row['domain_uuid']]['domain_name'] . "_" . $ivr_menu_name . ".xml", "w");
             } else {
                 $fout = fopen($_SESSION['switch']['conf']['dir'] . "/ivr_menus/v_" . $ivr_menu_name . ".xml", "w");
             }
             fwrite($fout, $tmp);
             fclose($fout);
         }
     }
     save_dialplan_xml();
     //apply settings
     $_SESSION["reload_xml"] = true;
 }
Пример #7
0
 public function dialplan($field)
 {
     //set global variables
     global $db;
     //action add or update
     if (strlen($field['dialplan_uuid']) == 0) {
         $field['dialplan_uuid'] = uuid();
         $action = "add";
     } else {
         $action = "update";
     }
     //delete child data
     if ($action == "update") {
         $sql = "delete from v_dialplan_details ";
         $sql .= "where dialplan_uuid = '" . $field["dialplan_uuid"] . "'; ";
         $db->query($sql);
         unset($sql);
     }
     //get dialplan details from the database
     if ($action == "update") {
         $sql = "select * from v_dialplan_details ";
         $sql .= "where dialplan_uuid = '" . $field["dialplan_uuid"] . "' ";
         $prep_statement = $db->prepare($sql);
         if ($prep_statement) {
             $prep_statement->execute();
             $dialplan_details = $prep_statement->fetchAll(PDO::FETCH_NAMED);
         }
         unset($sql, $prep_statement, $row);
     }
     //get the array from the database
     /*
     $database = new database;
     if ($this->db) { $database->db = $this->db; }
     $database->table = "v_ivr_menu_details";
     $database->where[0]['name'] = 'ivr_menu_uuid';
     $database->where[0]['value'] = $field["ivr_menu_uuid"];
     $database->where[0]['operator'] = '=';
     $ivr_menu_details = $database->find();
     */
     //add the details array
     $x = 0;
     $details[$x]['dialplan_detail_tag'] = 'condition';
     //condition, action, antiaction
     $details[$x]['dialplan_detail_type'] = 'destination_number';
     $details[$x]['dialplan_detail_data'] = '^' . $field['ivr_menu_extension'] . '$';
     $details[$x]['dialplan_detail_order'] = '005';
     $x++;
     $details[$x]['dialplan_detail_tag'] = 'action';
     //condition, action, antiaction
     $details[$x]['dialplan_detail_type'] = 'answer';
     $details[$x]['dialplan_detail_data'] = '';
     $details[$x]['dialplan_detail_order'] = '010';
     $x++;
     $details[$x]['dialplan_detail_tag'] = 'action';
     //condition, action, antiaction
     $details[$x]['dialplan_detail_type'] = 'sleep';
     $details[$x]['dialplan_detail_data'] = '1000';
     $details[$x]['dialplan_detail_order'] = '015';
     $x++;
     $details[$x]['dialplan_detail_tag'] = 'action';
     //condition, action, antiaction
     $details[$x]['dialplan_detail_type'] = 'set';
     $details[$x]['dialplan_detail_data'] = 'hangup_after_bridge=true';
     $details[$x]['dialplan_detail_order'] = '020';
     $x++;
     $details[$x]['dialplan_detail_tag'] = 'action';
     //condition, action, antiaction
     $details[$x]['dialplan_detail_type'] = 'set';
     if ($field['ivr_menu_ringback'] == "music" || $field['ivr_menu_ringback'] == "") {
         $details[$x]['dialplan_detail_data'] = 'ringback=${hold_music}';
     } else {
         $details[$x]['dialplan_detail_data'] = 'ringback=' . $field['ivr_menu_ringback'];
     }
     $details[$x]['dialplan_detail_order'] = '025';
     $x++;
     $details[$x]['dialplan_detail_tag'] = 'action';
     //condition, action, antiaction
     $details[$x]['dialplan_detail_type'] = 'set';
     if ($field['ivr_menu_ringback'] == "music" || $field['ivr_menu_ringback'] == "") {
         $details[$x]['dialplan_detail_data'] = 'transfer_ringback=${hold_music}';
     } else {
         $details[$x]['dialplan_detail_data'] = 'transfer_ringback=' . $field['ivr_menu_ringback'];
     }
     $details[$x]['dialplan_detail_order'] = '030';
     $x++;
     $details[$x]['dialplan_detail_tag'] = 'action';
     //condition, action, antiaction
     $details[$x]['dialplan_detail_type'] = 'set';
     $details[$x]['dialplan_detail_data'] = 'ivr_menu_uuid=' . $this->ivr_menu_uuid;
     $details[$x]['dialplan_detail_order'] = '035';
     $x++;
     $details[$x]['dialplan_detail_tag'] = 'action';
     //condition, action, antiaction
     if ($_SESSION['ivr menu']['application']['text'] == "lua") {
         $details[$x]['dialplan_detail_type'] = 'lua';
         $details[$x]['dialplan_detail_data'] = 'ivr_menu.lua';
     } else {
         $details[$x]['dialplan_detail_type'] = 'ivr';
         $details[$x]['dialplan_detail_data'] = $this->ivr_menu_uuid;
     }
     $details[$x]['dialplan_detail_order'] = '040';
     if (strlen($field['ivr_menu_exit_app']) > 0) {
         $x++;
         $details[$x]['dialplan_detail_tag'] = 'action';
         //condition, action, antiaction
         $details[$x]['dialplan_detail_type'] = $field['ivr_menu_exit_app'];
         $details[$x]['dialplan_detail_data'] = $field['ivr_menu_exit_data'];
         $details[$x]['dialplan_detail_order'] = '045';
         $x++;
     }
     //build the array
     $array['domain_uuid'] = $field['domain_uuid'];
     $array['dialplan_uuid'] = $field['dialplan_uuid'];
     $array['dialplan_name'] = $field['ivr_menu_name'];
     $array['dialplan_order'] = '333';
     $array['dialplan_context'] = $_SESSION['context'];
     $array['dialplan_enabled'] = $field['ivr_menu_enabled'];
     $array['dialplan_description'] = $field['ivr_menu_description'];
     $array['app_uuid'] = $this->app_uuid;
     $x = 0;
     foreach ($details as $row) {
         //find the matching uuid
         /*
         foreach ($dialplan_details as $database) {
         	if ($database['dialplan_detail_tag'] == $row['dialplan_detail_tag']
         		&& $database['dialplan_detail_type'] == $row['dialplan_detail_type']
         		&& $database['dialplan_detail_data'] == $row['dialplan_detail_data']) {
         			$array['dialplan_details'][$x]['dialplan_detail_uuid'] = $database['dialplan_detail_uuid'];
         	}
         }
         */
         //add to the array
         $array['dialplan_details'][$x]['domain_uuid'] = $field['domain_uuid'];
         $array['dialplan_details'][$x]['dialplan_uuid'] = $field['dialplan_uuid'];
         $array['dialplan_details'][$x]['dialplan_detail_tag'] = $row['dialplan_detail_tag'];
         $array['dialplan_details'][$x]['dialplan_detail_type'] = $row['dialplan_detail_type'];
         $array['dialplan_details'][$x]['dialplan_detail_data'] = $row['dialplan_detail_data'];
         $array['dialplan_details'][$x]['dialplan_detail_order'] = $row['dialplan_detail_order'];
         //increment the row
         $x++;
     }
     //debug
     //echo "<pre>\n";
     //print_r($dialplan_details);
     //print_r($array);
     //echo "</pre>\n";
     //exit;
     //save the dialplan
     $orm = new orm();
     $orm->name('dialplans');
     $orm->save($array);
     $response = $orm->message;
     if (strlen($response['uuid']) > 0) {
         $this->dialplan_uuid = $response['uuid'];
     }
     //debug
     //echo "<pre>\n";
     //print_r($response);
     //echo "</pre>\n";
     //exit;
     //synchronize the xml config
     save_dialplan_xml();
     //delete the dialplan context from memcache
     //$fp = event_socket_create($_SESSION['event_socket_ip_address'], $_SESSION['event_socket_port'], $_SESSION['event_socket_password']);
     //if ($fp) {
     //	$switch_cmd .= "memcache delete dialplan:".$_SESSION["context"]."@".$_SESSION['domain_name'];
     //	$switch_result = event_socket_request($fp, 'api '.$switch_cmd);
     //}
 }
Пример #8
0
 public function upgrade()
 {
     //set the global variable
     global $db, $db_type, $db_name, $db_username, $db_password, $db_host, $db_path, $db_port;
     //get the PROJECT PATH
     include "root.php";
     //get the list of installed apps from the core and app directories (note: GLOB_BRACE doesn't work on some systems)
     $config_list_1 = glob($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/*/*/app_config.php");
     $config_list_2 = glob($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/*/*/app_menu.php");
     $config_list = array_merge((array) $config_list_1, (array) $config_list_2);
     unset($config_list_1, $config_list_2);
     $x = 0;
     foreach ($config_list as &$config_path) {
         include $config_path;
         $x++;
     }
     //get the domain_uuid
     $sql = "select * from v_domains ";
     $prep_statement = $db->prepare($sql);
     $prep_statement->execute();
     $result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
     foreach ($result as $row) {
         if (count($result) == 1) {
             $_SESSION["domain_uuid"] = $row["domain_uuid"];
             $_SESSION["domain_name"] = $row['domain_name'];
         } else {
             if (lower_case($row['domain_name']) == lower_case($domain_array[0]) || lower_case($row['domain_name']) == lower_case('www.' . $domain_array[0])) {
                 $_SESSION["domain_uuid"] = $row["domain_uuid"];
                 $_SESSION["domain_name"] = $row['domain_name'];
             }
             $_SESSION['domains'][$row['domain_uuid']]['domain_uuid'] = $row['domain_uuid'];
             $_SESSION['domains'][$row['domain_uuid']]['domain_name'] = $row['domain_name'];
         }
     }
     unset($result, $prep_statement);
     //get the default settings
     $sql = "select * from v_default_settings ";
     $sql .= "where default_setting_enabled = 'true' ";
     $prep_statement = $db->prepare($sql);
     $prep_statement->execute();
     $result_default_settings = $prep_statement->fetchAll(PDO::FETCH_NAMED);
     //get the default recordings directory
     foreach ($result_default_settings as $row) {
         $name = $row['default_setting_name'];
         $category = $row['default_setting_category'];
         $subcategory = $row['default_setting_subcategory'];
         if ($category == 'switch' && $subcategory == 'recordings' && $name == 'dir') {
             $switch_recordings_dir = $row['default_setting_value'];
         }
     }
     //loop through all domains
     $sql = "select * from v_domains ";
     $v_prep_statement = $db->prepare(check_sql($sql));
     $v_prep_statement->execute();
     $main_result = $v_prep_statement->fetchAll(PDO::FETCH_ASSOC);
     $domain_count = count($main_result);
     $domains_processed = 1;
     foreach ($main_result as &$row) {
         //get the values from database and set them as php variables
         $domain_uuid = $row["domain_uuid"];
         $domain_name = $row["domain_name"];
         //get the context
         $context = $domain_name;
         //show the domain when display_type is set to text
         if ($display_type == "text") {
             echo "\n";
             echo $domain_name;
             echo "\n";
         }
         //get the default settings - this needs to be done to reset the session values back to the defaults for each domain in the loop
         foreach ($result_defaults_settings as $row) {
             $name = $row['default_setting_name'];
             $category = $row['default_setting_category'];
             $subcategory = $row['default_setting_subcategory'];
             if (strlen($subcategory) == 0) {
                 if ($name == "array") {
                     $_SESSION[$category][] = $row['default_setting_value'];
                 } else {
                     $_SESSION[$category][$name] = $row['default_setting_value'];
                 }
             } else {
                 if ($name == "array") {
                     $_SESSION[$category][$subcategory][] = $row['default_setting_value'];
                 } else {
                     $_SESSION[$category][$subcategory]['uuid'] = $row['default_setting_uuid'];
                     $_SESSION[$category][$subcategory][$name] = $row['default_setting_value'];
                 }
             }
         }
         //get the domains settings
         $sql = "select * from v_domain_settings ";
         $sql .= "where domain_uuid = '" . $domain_uuid . "' ";
         $sql .= "and domain_setting_enabled = 'true' ";
         $prep_statement = $db->prepare($sql);
         $prep_statement->execute();
         $result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
         foreach ($result as $row) {
             $name = $row['domain_setting_name'];
             $category = $row['domain_setting_category'];
             $subcategory = $row['domain_setting_subcategory'];
             if (strlen($subcategory) == 0) {
                 //$$category[$name] = $row['domain_setting_value'];
                 $_SESSION[$category][$name] = $row['domain_setting_value'];
             } else {
                 //$$category[$subcategory][$name] = $row['domain_setting_value'];
                 $_SESSION[$category][$subcategory][$name] = $row['domain_setting_value'];
             }
         }
         //set the recordings directory
         if (strlen($switch_recordings_dir) > 1 && count($_SESSION["domains"]) > 1) {
             $_SESSION['switch']['recordings']['dir'] = $switch_recordings_dir . "/" . $domain_name;
         }
         //get the list of installed apps from the core and mod directories and execute the php code in app_defaults.php
         $default_list = glob($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/*/*/app_defaults.php");
         foreach ($default_list as &$default_path) {
             include $default_path;
         }
         //track of the number of domains processed
         $domains_processed++;
     }
     unset($v_prep_statement);
     //synchronize the dialplan
     if (function_exists('save_dialplan_xml')) {
         save_dialplan_xml();
     }
     //clear the session variables
     unset($_SESSION['domain']);
     unset($_SESSION['switch']);
 }
function save_hunt_group()
{
    //Hunt Group Lua Notes:
    //get the domain
    //loop through all Hunt Groups
    //get the Hunt Group information such as the name and description
    //add each Hunt Group to the dialplan
    //get the list of destinations then build the Hunt Group lua
    //get the domain
    global $db, $domain_uuid, $host, $config;
    $tmp = "";
    $tmp .= "\n";
    $tmp .= " domain_name = \"" . $domain . "\"; //by default this is the ipv4 address of FreeSWITCH used for transfer to voicemail\n";
    $tmp .= "\n";
    $tmp .= "\n";
    //prepare for hunt group .lua files to be written. delete all hunt groups that are prefixed with huntgroup_ and have a file extension of .lua
    $v_prefix = 'v_huntgroup_';
    if ($dh = opendir($_SESSION['switch']['scripts']['dir'])) {
        $files = array();
        while ($file = readdir($dh)) {
            if ($file != "." && $file != ".." && $file[0] != '.') {
                if (is_dir($dir . "/" . $file)) {
                    //this is a directory
                } else {
                    if (substr($file, 0, strlen($v_prefix)) == $v_prefix && substr($file, -4) == '.lua') {
                        if ($file != "huntgroup_originate.lua") {
                            unlink($_SESSION['switch']['scripts']['dir'] . '/' . $file);
                        }
                    }
                }
            }
        }
        closedir($dh);
    }
    //loop through all Hunt Groups
    $x = 0;
    $sql = "select * from v_hunt_groups ";
    $prep_statement = $db->prepare(check_sql($sql));
    $prep_statement->execute();
    $result = $prep_statement->fetchAll(PDO::FETCH_ASSOC);
    foreach ($result as &$row) {
        //get the Hunt Group information such as the name and description
        //$row['hunt_group_uuid']
        //$row['hunt_group_extension']
        //$row['hunt_group_name']
        //$row['hunt_group_type']
        //$row['hunt_group_timeout']
        //$row['hunt_group_context']
        //$row['hunt_group_ringback']
        //$row['hunt_group_cid_name_prefix']
        //$row['hunt_group_pin']
        //$row['hunt_group_caller_announce']
        //$row['hunt_group_enabled']
        //$row['hunt_group_description']
        $domain_uuid = $row['domain_uuid'];
        $dialplan_uuid = $row['dialplan_uuid'];
        //add each hunt group to the dialplan
        if (strlen($row['hunt_group_uuid']) > 0) {
            //set default action to add
            $action = 'add';
            //check whether the dialplan entry exists in the database
            $action = 'add';
            //set default action to add
            $i = 0;
            $sql = "select count(*) as num_rows from v_dialplans ";
            $sql .= "where domain_uuid = '" . $domain_uuid . "' ";
            $sql .= "and dialplan_uuid = '" . $dialplan_uuid . "' ";
            $prep_statement_2 = $db->prepare(check_sql($sql));
            if ($prep_statement_2) {
                $prep_statement_2->execute();
                $row2 = $prep_statement_2->fetch(PDO::FETCH_ASSOC);
                if ($row2['num_rows'] > 0) {
                    //$num_rows = $row2['num_rows'];
                    $action = 'update';
                }
            }
            unset($prep_statement, $result);
            if ($action == 'add') {
                //create dialplan entry for each huntgroup
                $app_uuid = '0610f841-2e27-4c5f-7926-08ab3aad02e0';
                if ($row['hunt_group_enabled'] == "false") {
                    $dialplan_enabled = 'false';
                } else {
                    $dialplan_enabled = 'true';
                }
                if (strlen($dialplan_uuid) == 0) {
                    //create a dialplan uuid
                    $dialplan_uuid = uuid();
                    //update the hunt groups table with the database
                    $sql = "update v_hunt_groups ";
                    $sql .= "set dialplan_uuid = '" . $dialplan_uuid . "' ";
                    $sql .= "where domain_uuid = '" . $domain_uuid . "' ";
                    $sql .= "and hunt_group_uuid = '" . $row['hunt_group_uuid'] . "' ";
                    $db->query($sql);
                    unset($sql);
                }
                require_once "resources/classes/dialplan.php";
                $dialplan = new dialplan();
                $dialplan->domain_uuid = $domain_uuid;
                $dialplan->app_uuid = $app_uuid;
                $dialplan->dialplan_uuid = $dialplan_uuid;
                $dialplan->dialplan_name = $row['hunt_group_name'];
                //$dialplan->dialplan_continue = $dialplan_continue;
                $dialplan->dialplan_order = '330';
                $dialplan->dialplan_context = $_SESSION['context'];
                $dialplan->dialplan_enabled = $dialplan_enabled;
                $dialplan->dialplan_description = $row['hunt_group_description'];
                $dialplan->dialplan_add();
                unset($dialplan);
            }
            if ($action == 'update') {
                //update the huntgroup fifo
                $dialplan_name = $row['hunt_group_name'];
                $dialplan_order = '330';
                $context = $row['hunt_group_context'];
                if ($row['hunt_group_enabled'] == "false") {
                    $enabled = 'false';
                } else {
                    $enabled = 'true';
                }
                $description = $row['hunt_group_description'];
                $hunt_group_uuid = $row['hunt_group_uuid'];
                $sql = "update v_dialplans set ";
                $sql .= "dialplan_name = '{$dialplan_name}', ";
                $sql .= "dialplan_order = '{$dialplan_order}', ";
                $sql .= "dialplan_context = '{$context}', ";
                $sql .= "dialplan_enabled = '{$enabled}', ";
                $sql .= "dialplan_description = '{$description}' ";
                $sql .= "where domain_uuid = '" . $domain_uuid . "' ";
                $sql .= "and dialplan_uuid = '" . $dialplan_uuid . "' ";
                $db->query($sql);
                unset($sql);
                $sql = "delete from v_dialplan_details ";
                $sql .= "where domain_uuid = '{$domain_uuid}' ";
                $sql .= "and dialplan_uuid = '{$dialplan_uuid}' ";
                $db->query($sql);
                unset($sql);
            }
            //if action is add or update
            if ($action == 'add' || $action == 'update') {
                require_once "resources/classes/dialplan.php";
                $dialplan = new dialplan();
                $dialplan->domain_uuid = $domain_uuid;
                $dialplan->dialplan_uuid = $dialplan_uuid;
                $dialplan->dialplan_detail_tag = 'condition';
                //condition, action, antiaction
                $dialplan->dialplan_detail_type = 'destination_number';
                $dialplan->dialplan_detail_data = '^' . $row['hunt_group_extension'] . '$';
                //$dialplan->dialplan_detail_break = '';
                //$dialplan->dialplan_detail_inline = '';
                $dialplan->dialplan_detail_group = '1';
                $dialplan->dialplan_detail_order = '010';
                $dialplan->dialplan_detail_add();
                unset($dialplan);
                $dialplan = new dialplan();
                $dialplan->domain_uuid = $domain_uuid;
                $dialplan->dialplan_uuid = $dialplan_uuid;
                $dialplan->dialplan_detail_tag = 'action';
                //condition, action, antiaction
                $dialplan->dialplan_detail_type = 'lua';
                $dialplan->dialplan_detail_data = 'v_huntgroup_' . $_SESSION['domains'][$domain_uuid]['domain_name'] . '_' . $row['hunt_group_extension'] . '.lua';
                //$dialplan->dialplan_detail_break = '';
                //$dialplan->dialplan_detail_inline = '';
                $dialplan->dialplan_detail_group = '1';
                $dialplan->dialplan_detail_order = '020';
                $dialplan->dialplan_detail_add();
                unset($dialplan);
                $dialplan = new dialplan();
                $dialplan->domain_uuid = $domain_uuid;
                $dialplan->dialplan_uuid = $dialplan_uuid;
                $dialplan->dialplan_detail_tag = 'condition';
                //condition, action, antiaction
                $dialplan->dialplan_detail_type = 'destination_number';
                $dialplan->dialplan_detail_data = '^\\*' . $row['hunt_group_extension'] . '$';
                $dialplan->dialplan_detail_break = 'on-true';
                //$dialplan->dialplan_detail_inline = '';
                $dialplan->dialplan_detail_group = '2';
                $dialplan->dialplan_detail_order = '020';
                $dialplan->dialplan_detail_add();
                unset($dialplan);
                $dialplan = new dialplan();
                $dialplan->domain_uuid = $domain_uuid;
                $dialplan->dialplan_uuid = $dialplan_uuid;
                $dialplan->dialplan_detail_tag = 'action';
                //condition, action, antiaction
                $dialplan->dialplan_detail_type = 'set';
                $dialplan->dialplan_detail_data = 'fifo_music=$${hold_music}';
                //$dialplan->dialplan_detail_break = '';
                //$dialplan->dialplan_detail_inline = '';
                $dialplan->dialplan_detail_group = '2';
                $dialplan->dialplan_detail_order = '020';
                $dialplan->dialplan_detail_add();
                unset($dialplan);
                $hunt_group_timeout_type = $row['hunt_group_timeout_type'];
                $hunt_group_timeout_destination = $row['hunt_group_timeout_destination'];
                if ($hunt_group_timeout_type == "voicemail") {
                    $hunt_group_timeout_destination = '*99' . $hunt_group_timeout_destination;
                }
                $dialplan = new dialplan();
                $dialplan->domain_uuid = $domain_uuid;
                $dialplan->dialplan_uuid = $dialplan_uuid;
                $dialplan->dialplan_detail_tag = 'action';
                //condition, action, antiaction
                $dialplan->dialplan_detail_type = 'set';
                $dialplan->dialplan_detail_data = 'fifo_orbit_exten=' . $hunt_group_timeout_destination . ':' . $row['hunt_group_timeout'];
                //$dialplan->dialplan_detail_break = '';
                //$dialplan->dialplan_detail_inline = '';
                $dialplan->dialplan_detail_group = '2';
                $dialplan->dialplan_detail_order = '030';
                $dialplan->dialplan_detail_add();
                unset($dialplan);
                $dialplan = new dialplan();
                $dialplan->domain_uuid = $domain_uuid;
                $dialplan->dialplan_uuid = $dialplan_uuid;
                $dialplan->dialplan_detail_tag = 'action';
                //condition, action, antiaction
                $dialplan->dialplan_detail_type = 'fifo';
                $dialplan->dialplan_detail_data = $row['hunt_group_extension'] . '@${domain_name} in';
                //$dialplan->dialplan_detail_break = '';
                //$dialplan->dialplan_detail_inline = '';
                $dialplan->dialplan_detail_group = '2';
                $dialplan->dialplan_detail_order = '040';
                $dialplan->dialplan_detail_add();
                unset($dialplan);
            }
        }
        //end if strlen hunt_group_uuid; add the Hunt Group to the dialplan
        //get the list of destinations then build the Hunt Group Lua
        $tmp = "\n";
        $tmp .= "session:preAnswer();\n";
        $tmp .= "extension = '" . $row['hunt_group_extension'] . "';\n";
        $tmp .= "result = '';\n";
        $tmp .= "timeoutpin = 7500;\n";
        $tmp .= "sip_profile = 'internal';\n";
        $tmp .= "\n";
        $tmp .= "function extension_registered(domain_name, sip_profile, extension)\n";
        $tmp .= "\tapi = freeswitch.API();\n";
        $tmp .= "\tresult = api:execute(\"sofia_contact\", sip_profile..\"/\"..extension..\"@\"..domain_name);\n";
        $tmp .= "\tif (result == \"error/user_not_registered\") then\n";
        $tmp .= "\t\treturn false;\n";
        $tmp .= "\telse\n";
        $tmp .= "\t\treturn true;\n";
        $tmp .= "\tend\n";
        $tmp .= "end\n";
        $tmp .= "\n";
        $tmp .= "\n";
        $tmp .= "sounds_dir = session:getVariable(\"sounds_dir\");\n";
        $tmp .= "uuid = session:getVariable(\"uuid\");\n";
        $tmp .= "dialed_extension = session:getVariable(\"dialed_extension\");\n";
        $tmp .= "domain_name = session:getVariable(\"domain_name\");\n";
        $tmp .= "caller_id_name = session:getVariable(\"caller_id_name\");\n";
        $tmp .= "caller_id_number = session:getVariable(\"caller_id_number\");\n";
        $tmp .= "outbound_caller_id_name = session:getVariable(\"outbound_caller_id_name\");\n";
        $tmp .= "outbound_caller_id_number = session:getVariable(\"outbound_caller_id_number\");\n";
        $tmp .= "\n";
        $tmp .= "--set the sounds path for the language, dialect and voice\n";
        $tmp .= "\tdefault_language = session:getVariable(\"default_language\");\n";
        $tmp .= "\tdefault_dialect = session:getVariable(\"default_dialect\");\n";
        $tmp .= "\tdefault_voice = session:getVariable(\"default_voice\");\n";
        $tmp .= "\tif (not default_language) then default_language = 'en'; end\n";
        $tmp .= "\tif (not default_dialect) then default_dialect = 'us'; end\n";
        $tmp .= "\tif (not default_voice) then default_voice = 'callie'; end\n";
        $tmp .= "\n";
        //pin number requested from caller if provided
        if (strlen($row['hunt_group_pin']) > 0) {
            $tmp .= "pin = '" . $row['hunt_group_pin'] . "';\n";
            $tmp .= "digits = session:playAndGetDigits(" . strlen($row['hunt_group_pin']) . ", " . strlen($row['hunt_group_pin']) . ", 3, 3000, \"#\", sounds_dir..\"/\"..default_language..\"/\"..default_dialect..\"/\"..default_voice..\"/custom/please_enter_the_pin_number.wav\", \"\", \"\\\\d+\");\n";
            $tmp .= "\n";
            $tmp .= "\n";
            $tmp .= "if (digits == pin) then\n";
            $tmp .= "\t--continue\n";
            $tmp .= "\n";
        }
        //caller announce requested from caller if provided
        if ($row['hunt_group_caller_announce'] == "true" || $row['hunt_group_call_prompt'] == "true") {
            if ($row['hunt_group_caller_announce'] == "true") {
                $tmp .= "function originate(domain_name, session, sipuri, extension, caller_id_name, caller_id_number, caller_announce) \n";
            } else {
                $tmp .= "function originate(domain_name, session, sipuri, extension, caller_id_name, caller_id_number) \n";
            }
            $tmp .= "\t--caller_id_name = caller_id_name.replace(\" \", \"..\");\n";
            $tmp .= "\tcaller_id_name = string.gsub(caller_id_name, \" \", \"..\");\n";
            //$tmp .=	"	--session:execute(\"luarun\", \"huntgroup_originate.lua \"..domain_name..\" \"..uuid..\" \"..sipuri..\" \"..extension..\" \"..caller_id_name..\" \"..caller_id_number..\" \"..caller_announce);\n";
            $tmp .= "\tapi = freeswitch.API();\n";
            if ($row['hunt_group_caller_announce'] == "true") {
                $tmp .= "\tresult = api:execute(\"luarun\", \"huntgroup_originate.lua \"..domain_name..\" \"..uuid..\" \"..sipuri..\" \"..extension..\" \"..caller_id_name..\" \"..caller_id_number..\" \"..caller_announce);\n";
            } else {
                $tmp .= "\tresult = api:execute(\"luarun\", \"huntgroup_originate.lua \"..domain_name..\" \"..uuid..\" \"..sipuri..\" \"..extension..\" \"..caller_id_name..\" \"..caller_id_number..\"\");\n";
            }
            $tmp .= "end";
            $tmp .= "\n";
            if ($row['hunt_group_caller_announce'] == "true") {
                $tmp .= "caller_announce = \"" . $tmp_dir . "/\"..extension..\"_\"..uuid..\".wav\";\n";
                $tmp .= "session:streamFile(sounds_dir..\"/\"..default_language..\"/\"..default_dialect..\"/\"..default_voice..\"/custom/please_say_your_name_and_reason_for_calling.wav\");\n";
                $tmp .= "session:execute(\"gentones\", \"%(1000, 0, 640)\");\n";
                $tmp .= "session:execute(\"set\", \"playback_terminators=#\");\n";
                $tmp .= "session:execute(\"record\", caller_announce..\" 180 200\");\n";
            }
            $tmp .= "\n";
            $tmp .= "session:setAutoHangup(false);\n";
            $tmp .= "session:execute(\"transfer\", \"*\"..extension..\" XML " . $_SESSION["context"] . "\");\n";
            $tmp .= "\n";
        }
        //set caller id
        if (strlen($row['hunt_group_cid_name_prefix']) > 0) {
            $tmp .= "session:execute(\"set\", \"effective_caller_id_name=" . $row['hunt_group_cid_name_prefix'] . "#\"..caller_id_name);\n";
            $tmp .= "session:execute(\"set\", \"outbound_caller_id_name=" . $row['hunt_group_cid_name_prefix'] . "#\"..caller_id_name);\n";
        }
        //set ring back
        if (isset($row['hunt_group_ringback'])) {
            if ($row['hunt_group_ringback'] == "music") {
                $tmp .= "session:execute(\"set\", \"ringback=\${hold_music}\");          --set to music\n";
                $tmp .= "session:execute(\"set\", \"transfer_ringback=\${hold_music}\"); --set to music\n";
            } else {
                $tmp .= "session:execute(\"set\", \"ringback=" . $row['hunt_group_ringback'] . "\"); --set to ringtone\n";
                $tmp .= "session:execute(\"set\", \"transfer_ringback=" . $row['hunt_group_ringback'] . "\"); --set to ringtone\n";
            }
            if ($row['hunt_group_ringback'] == "ring") {
                $tmp .= "session:execute(\"set\", \"ringback=\${us-ring}\"); --set to ringtone\n";
                $tmp .= "session:execute(\"set\", \"transfer_ringback=\${us-ring}\"); --set to ringtone\n";
            }
        } else {
            $tmp .= "session:execute(\"set\", \"ringback=\${hold_music}\");          --set to ringtone\n";
            $tmp .= "session:execute(\"set\", \"transfer_ringback=\${hold_music}\"); --set to ringtone\n";
        }
        if ($row['hunt_group_timeout'] > 0) {
            //$tmp .= "session:setVariable(\"call_timeout\", \"".$row['hunt_group_timeout']."\");\n";
            $tmp .= "session:setVariable(\"continue_on_fail\", \"true\");\n";
        }
        $tmp .= "session:setVariable(\"hangup_after_bridge\", \"true\");\n";
        $tmp .= "\n";
        $tmp .= "--freeswitch.consoleLog( \"info\", \"dialed extension:\"..dialed_extension..\"\\n\" );\n";
        $tmp .= "--freeswitch.consoleLog( \"info\", \"domain: \"..domain..\"\\n\" );\n";
        $tmp .= "--freeswitch.consoleLog( \"info\", \"us_ring: \"..us_ring..\"\\n\" );\n";
        $tmp .= "--freeswitch.consoleLog( \"info\", \"domain_name: \"..domain_name..\"\\n\" );\n";
        $tmp .= "\n";
        $tmp .= "--freeswitch.consoleLog( \"info\", \"action call now don't wait for dtmf\\n\" );\n";
        if ($row['hunt_group_caller_announce'] == "true" || $row['hunt_group_call_prompt'] == "true") {
            //do nothing
        } else {
            $tmp .= "if session:ready() then\n";
            //$tmp .= "	session.answer();\n";
        }
        $tmp .= "\n";
        $i = 0;
        $sql = "select * from v_hunt_group_destinations ";
        $sql .= "where hunt_group_uuid = '" . $row['hunt_group_uuid'] . "' ";
        $sql .= "and domain_uuid = '{$domain_uuid}' ";
        //$sql .= "and destination_enabled = 'true' ";
        $sql .= "order by destination_order asc ";
        $prep_statement_2 = $db->prepare($sql);
        $prep_statement_2->execute();
        while ($ent = $prep_statement_2->fetch(PDO::FETCH_ASSOC)) {
            //$ent['hunt_group_uuid']
            //$ent['destination_data']
            //$ent['destination_type']
            //$ent['destination_profile']
            //$ent['destination_order']
            //$ent['destination_enabled']
            //$ent['destination_description']
            $destination_timeout = $ent['destination_timeout'];
            $hunt_group_cid_name_prefix = $row['hunt_group_cid_name_prefix'];
            if (strlen($hunt_group_cid_name_prefix) > 0) {
                $hunt_group_cid_name_prefix .= "#";
            }
            if (strlen($destination_timeout) == 0) {
                if (strlen($row['hunt_group_timeout']) == 0) {
                    $destination_timeout = '30';
                } else {
                    $destination_timeout = $row['hunt_group_timeout'];
                }
            }
            //set the default profile
            if (strlen($ent['destination_data']) == 0) {
                $ent['destination_data'] = "internal";
            }
            if ($ent['destination_type'] == "extension") {
                //$tmp .= "	sofia_contact_".$ent['destination_data']." = \"\${sofia_contact(".$ent['destination_profile']."/".$ent['destination_data']."@\"..domain_name..\")}\";\n";
                $tmp_sub_array["application"] = "bridge";
                $tmp_sub_array["type"] = "extension";
                $tmp_sub_array["extension"] = $ent['destination_data'];
                //$tmp_sub_array["data"] = "\"[leg_timeout=$destination_timeout]\"..sofia_contact_".$ent['destination_data'];
                $tmp_sub_array["data"] = "\"[leg_timeout={$destination_timeout},origination_caller_id_name='" . $hunt_group_cid_name_prefix . "\"..caller_id_name..\"',origination_caller_id_number=\"..caller_id_number..\"]user/" . $ent['destination_data'] . "@\"..domain_name";
                $tmp_array[$i] = $tmp_sub_array;
                unset($tmp_sub_array);
            }
            if ($ent['destination_type'] == "voicemail") {
                $tmp_sub_array["application"] = "voicemail";
                $tmp_sub_array["type"] = "voicemail";
                $tmp .= "\tsession:answer();\n";
                $tmp .= "\tsession:execute(\"transfer\", \"*99" . $ent['destination_data'] . " XML " . $_SESSION["context"] . " \");\n";
                //$tmp_sub_array["application"] = "voicemail";
                //$tmp_sub_array["data"] = "default \${domain_name} ".$ent['destination_data'];
                //$tmp_array[$i] = $tmp_sub_array;
                unset($tmp_sub_array);
            }
            if ($ent['destination_type'] == "sip uri") {
                $tmp_sub_array["application"] = "bridge";
                $tmp_sub_array["type"] = "sip uri";
                //$destination_data = "{user=foo}loopback/".$ent['destination_data']."/default/XML";
                $bridge_array = outbound_route_to_bridge($domain_uuid, $ent['destination_data']);
                $destination_data = $bridge_array[0];
                $tmp_sub_array["application"] = "bridge";
                $tmp_sub_array["data"] = "\"[leg_timeout={$destination_timeout},origination_caller_id_name='" . $hunt_group_cid_name_prefix . "\"..caller_id_name..\"',origination_caller_id_number=\"..caller_id_number..\"]" . $destination_data . "\"";
                $tmp_array[$i] = $tmp_sub_array;
                unset($tmp_sub_array);
                unset($destination_data);
            }
            $i++;
        }
        //end while
        unset($sql, $prep_statement_2);
        unset($i, $ent);
        $i = 0;
        if (count($tmp_array) > 0) {
            foreach ($tmp_array as $ent) {
                $tmpdata = $ent["data"];
                if ($ent["application"] == "voicemail") {
                    $tmpdata = "*99" . $tmpdata;
                }
                if ($i < 1) {
                    $tmp_buffer = $tmpdata;
                } else {
                    $tmp_buffer .= "..\",\".." . $tmpdata;
                }
                $i++;
            }
        }
        unset($i);
        $tmp_application = $tmp_array[0]["application"];
        if ($row['hunt_group_type'] == "simultaneous" || $row['hunt_group_type'] == "follow_me_simultaneous" || $row['hunt_group_type'] == "call_forward") {
            $tmp_switch = "simultaneous";
        }
        if ($row['hunt_group_type'] == "sequence" || $row['hunt_group_type'] == "follow_me_sequence" || $row['hunt_group_type'] == "sequentially") {
            $tmp_switch = "sequence";
        }
        switch ($tmp_switch) {
            case "simultaneous":
                if ($row['hunt_group_caller_announce'] == "true" || $row['hunt_group_call_prompt'] == "true") {
                    $i = 0;
                    if (count($tmp_array) > 0) {
                        foreach ($tmp_array as $tmp_row) {
                            $tmpdata = $tmp_row["data"];
                            if ($tmp_row["application"] == "voicemail") {
                                $tmpdata = "*99" . $tmpdata;
                            } else {
                                if ($tmp_row["type"] == "extension") {
                                    $tmp .= "if (extension_registered(domain_name, sip_profile, '" . $tmp_row["extension"] . "')) then\n";
                                    $tmp .= "\t";
                                }
                                if ($row['hunt_group_caller_announce'] == "true") {
                                    $tmp .= "result = originate (domain_name, session, " . $tmpdata . ", extension, caller_id_name, caller_id_number, caller_announce);\n";
                                } else {
                                    $tmp .= "result = originate (domain_name, session, " . $tmpdata . ", extension, caller_id_name, caller_id_number);\n";
                                }
                                if ($tmp_row["type"] == "extension") {
                                    $tmp .= "end\n";
                                }
                            }
                        }
                    }
                } else {
                    $tmp .= "\n";
                    if (strlen($tmp_buffer) > 0) {
                        $tmp .= "\tsession:execute(\"" . $tmp_application . "\", {$tmp_buffer});\n";
                    }
                }
                break;
            case "sequence":
                $tmp .= "\n";
                $i = 0;
                if (count($tmp_array) > 0) {
                    if ($row['hunt_group_caller_announce'] == "true" || $row['hunt_group_call_prompt'] == "true") {
                        $i = 0;
                        if (count($tmp_array) > 0) {
                            foreach ($tmp_array as $tmp_row) {
                                $tmpdata = $tmp_row["data"];
                                if ($tmp_row["application"] == "voicemail") {
                                    $tmpdata = "*99" . $tmpdata;
                                } else {
                                    if ($tmp_row["type"] == "extension") {
                                        $tmp .= "if (extension_registered(domain_name, sip_profile, '" . $tmp_row["extension"] . "')) then\n";
                                        $tmp .= "\t";
                                    }
                                    if ($row['hunt_group_caller_announce'] == "true") {
                                        $tmp .= "result = originate (domain_name, session, " . $tmpdata . ", extension, caller_id_name, caller_id_number, caller_announce);\n";
                                    } else {
                                        $tmp .= "result = originate (domain_name, session, " . $tmpdata . ", extension, caller_id_name, caller_id_number);\n";
                                    }
                                    if ($tmp_row["type"] == "extension") {
                                        $tmp .= "end\n";
                                    }
                                }
                            }
                        }
                    } else {
                        foreach ($tmp_array as $tmp_row) {
                            if (strlen($tmp_row["data"]) > 0) {
                                $tmp .= "\tsession:execute(\"" . $tmp_application . "\", " . $tmp_row["data"] . ");\n";
                            }
                        }
                    }
                    unset($tmp_row);
                }
                break;
        }
        unset($tmp_switch, $tmp_buffer, $tmp_array);
        //set the timeout destination
        $hunt_group_timeout_destination = $row['hunt_group_timeout_destination'];
        if ($row['hunt_group_timeout_type'] == "extension") {
            $hunt_group_timeout_type = "transfer";
        }
        if ($row['hunt_group_timeout_type'] == "voicemail") {
            $hunt_group_timeout_type = "transfer";
            $hunt_group_timeout_destination = "*99" . $hunt_group_timeout_destination . " XML " . $_SESSION["context"];
        }
        if ($row['hunt_group_timeout_type'] == "sip uri") {
            $hunt_group_timeout_type = "bridge";
        }
        $tmp .= "\n";
        if ($row['hunt_group_caller_announce'] == "true" || $row['hunt_group_call_prompt'] == "true") {
            //do nothing
        } else {
            $tmp .= "\t--timeout\n";
            if ($row['hunt_group_type'] != 'dnd') {
                $tmp .= "\toriginate_disposition = session:getVariable(\"originate_disposition\");\n";
                $tmp .= "\tif originate_disposition ~= \"SUCCESS\" then\n";
            }
            if ($row['hunt_group_timeout_type'] == "voicemail") {
                $tmp .= "\t\t\tsession:answer();\n";
            }
            $tmp .= "\t\t\tsession:execute(\"" . $hunt_group_timeout_type . "\", \"" . $hunt_group_timeout_destination . "\");\n";
            if ($row['hunt_group_type'] != 'dnd') {
                $tmp .= "\tend\n";
            }
        }
        if ($row['hunt_group_caller_announce'] == "true" || $row['hunt_group_call_prompt'] == "true") {
            //do nothing
        } else {
            $tmp .= "end --end if session:ready\n";
        }
        $tmp .= "\n";
        //pin number requested from caller if provided
        if (strlen($row['hunt_group_pin']) > 0) {
            $tmp .= "else \n";
            $tmp .= "\tsession:streamFile(sounds_dir..\"/\"..default_language..\"/\"..default_dialect..\"/\"..default_voice..\"/custom/your_pin_number_is_incorect_goodbye.wav\");\n";
            $tmp .= "\tsession:hangup();\n";
            $tmp .= "end\n";
            $tmp .= "\n";
        }
        //unset variables
        $tmp .= "\n";
        $tmp .= "--clear variables\n";
        $tmp .= "dialed_extension = \"\";\n";
        $tmp .= "new_extension = \"\";\n";
        $tmp .= "domain_name = \"\";\n";
        $tmp .= "\n";
        //remove invalid characters from the file names
        $huntgroup_extension = $row['hunt_group_extension'];
        $huntgroup_extension = str_replace(" ", "_", $huntgroup_extension);
        $huntgroup_extension = preg_replace("/[\\*\\:\\/\\<\\>\\|\\'\"\\?]/", "", $huntgroup_extension);
        //write the hungroup lua script
        if (strlen($row['hunt_group_extension']) > 0) {
            if ($row['hunt_group_enabled'] != "false") {
                $hunt_group_filename = "v_huntgroup_" . $_SESSION['domains'][$domain_uuid]['domain_name'] . "_" . $huntgroup_extension . ".lua";
                //echo "location".$_SESSION['switch']['scripts']['dir']."/".$hunt_group_filename;
                $fout = fopen($_SESSION['switch']['scripts']['dir'] . "/" . $hunt_group_filename, "w");
                fwrite($fout, $tmp);
                unset($hunt_group_filename);
                fclose($fout);
            }
        }
    }
    //end while
    //save the dialplan xml files
    save_dialplan_xml();
}