Example #1
0
 function xml_save_all()
 {
     global $db, $config;
     $domain_uuid = $this->domain_uuid;
     $domain_name = $this->domain_name;
     //get the system settings paths and set them as variables
     $settings_array = v_settings();
     foreach ($settings_array as $name => $value) {
         ${$name} = $value;
     }
     //determine the extensions parent directory
     $extension_parent_dir = realpath($_SESSION['switch']['extensions']['dir'] . "/..");
     // delete all old extensions to prepare for new ones
     if ($dh = opendir($_SESSION['switch']['extensions']['dir'])) {
         $files = array();
         while ($file = readdir($dh)) {
             if ($file != "." && $file != ".." && $file[0] != '.') {
                 if (is_dir($dir . "/" . $file)) {
                     //this is a directory do nothing
                 } else {
                     //check if file is an extension; verify the file numeric and the extension is xml
                     if (substr($file, 0, 2) == 'v_' && substr($file, -4) == '.xml') {
                         unlink($_SESSION['switch']['extensions']['dir'] . "/" . $file);
                     }
                 }
             }
         }
         closedir($dh);
     }
     $sql = "select * from v_extensions ";
     $sql .= "where domain_uuid = '{$domain_uuid}' ";
     $sql .= "order by call_group asc ";
     $prep_statement = $db->prepare(check_sql($sql));
     $prep_statement->execute();
     $i = 0;
     $extension_xml_condensed = false;
     if ($extension_xml_condensed) {
         $fout = fopen($_SESSION['switch']['extensions']['dir'] . "/v_extensions.xml", "w");
         $xml = "<include>\n";
     }
     while ($row = $prep_statement->fetch(PDO::FETCH_ASSOC)) {
         $call_group = $row['call_group'];
         $call_group = str_replace(";", ",", $call_group);
         $tmp_array = explode(",", $call_group);
         foreach ($tmp_array as &$tmp_call_group) {
             if (strlen($tmp_call_group) > 0) {
                 if (strlen($call_group_array[$tmp_call_group]) == 0) {
                     $call_group_array[$tmp_call_group] = $row['extension'];
                 } else {
                     $call_group_array[$tmp_call_group] = $call_group_array[$tmp_call_group] . ',' . $row['extension'];
                 }
             }
             $i++;
         }
         if ($row['enabled'] != "false") {
             //$this->import_sql($row);//Do I need to be worried about ghost values? Maybe I should make a new object?
             //if (strlen($switch_account_code)) $this->accountcode=$switch_account_code;
             //$xml.=$this->generate_xml(1);
             $one_row = new fs_directory();
             $one_row->import_sql($row);
             //make a new object to flush ghost rows. And we can call this as static.
             if (strlen($switch_account_code)) {
                 $one_row->accountcode = $switch_account_code;
             }
             $xml .= $one_row->generate_xml(false);
             if (!$extension_xml_condensed) {
                 $xml .= "</include>\n";
                 fwrite($fout, $xml);
                 unset($xml);
                 fclose($fout);
             }
         }
     }
     unset($prep_statement);
     if ($extension_xml_condensed) {
         $xml .= "</include>\n";
         fwrite($fout, $xml);
         unset($xml);
         fclose($fout);
     }
     //define the group members
     $xml = "<!--\n";
     $xml .= "\tNOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE\n";
     $xml .= "\n";
     $xml .= "\tFreeSWITCH works off the concept of users and domains just like email.\n";
     $xml .= "\tYou have users that are in domains for example 1000@domain.com.\n";
     $xml .= "\n";
     $xml .= "\tWhen freeswitch gets a register packet it looks for the user in the directory\n";
     $xml .= "\tbased on the from or to domain in the packet depending on how your sofia profile\n";
     $xml .= "\tis configured.  Out of the box the default domain will be the IP address of the\n";
     $xml .= "\tmachine running FreeSWITCH.  This IP can be found by typing \"sofia status\" at the\n";
     $xml .= "\tCLI.  You will register your phones to the IP and not the hostname by default.\n";
     $xml .= "\tIf you wish to register using the domain please open vars.xml in the root conf\n";
     $xml .= "\tdirectory and set the default domain to the hostname you desire.  Then you would\n";
     $xml .= "\tuse the domain name in the client instead of the IP address to register\n";
     $xml .= "\twith FreeSWITCH.\n";
     $xml .= "\n";
     $xml .= "\tNOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE\n";
     $xml .= "-->\n";
     $xml .= "\n";
     $xml .= "<include>\n";
     $xml .= "\t<!--the domain or ip (the right hand side of the @ in the addr-->\n";
     if ($extension_dir_name == "default") {
         $xml .= "\t<domain name=\"\$\${domain}\">\n";
     } else {
         $xml .= "\t<domain name=\"" . $extension_dir_name . "\">\n";
     }
     $xml .= "\t\t<params>\n";
     //$xml .= "			<param name=\"dial-string\" value=\"{sip_invite_domain=\${domain_name},presence_id=\${dialed_user}@\${dialed_domain}}\${sofia_contact(\${dialed_user}@\${dialed_domain})}\"/>\n";
     $xml .= "\t\t</params>\n";
     $xml .= "\n";
     $xml .= "\t\t<variables>\n";
     $xml .= "\t\t\t<variable name=\"record_stereo\" value=\"true\"/>\n";
     $xml .= "\t\t\t<variable name=\"default_gateway\" value=\"\$\${default_provider}\"/>\n";
     $xml .= "\t\t\t<variable name=\"default_areacode\" value=\"\$\${default_areacode}\"/>\n";
     $xml .= "\t\t\t<variable name=\"transfer_fallback_extension\" value=\"operator\"/>\n";
     $xml .= "\t\t\t<variable name=\"export_vars\" value=\"domain_name\"/>\n";
     $xml .= "\t\t</variables>\n";
     $xml .= "\n";
     $xml .= "\t\t<groups>\n";
     $xml .= "\t\t\t<group name=\"" . $extension_dir_name . "\">\n";
     $xml .= "\t\t\t<users>\n";
     $xml .= "\t\t\t\t<X-PRE-PROCESS cmd=\"include\" data=\"" . $extension_dir_name . "/*.xml\"/>\n";
     $xml .= "\t\t\t</users>\n";
     $xml .= "\t\t\t</group>\n";
     $xml .= "\n";
     $previous_call_group = "";
     foreach ($call_group_array as $key => $value) {
         $call_group = $key;
         $extension_list = $value;
         if (strlen($call_group) > 0) {
             if ($previous_call_group != $call_group) {
                 $xml .= "\t\t\t<group name=\"{$call_group}\">\n";
                 $xml .= "\t\t\t\t<users>\n";
                 $xml .= "\t\t\t\t\t<!--\n";
                 $xml .= "\t\t\t\t\ttype=\"pointer\" is a pointer so you can have the\n";
                 $xml .= "\t\t\t\t\tsame user in multiple groups.  It basically means\n";
                 $xml .= "\t\t\t\t\tto keep searching for the user in the directory.\n";
                 $xml .= "\t\t\t\t\t-->\n";
                 $extension_array = explode(",", $extension_list);
                 foreach ($extension_array as &$tmp_extension) {
                     $xml .= "\t\t\t\t\t<user id=\"{$tmp_extension}\" type=\"pointer\"/>\n";
                 }
                 $xml .= "\t\t\t\t</users>\n";
                 $xml .= "\t\t\t</group>\n";
                 $xml .= "\n";
             }
             $previous_call_group = $call_group;
         }
         unset($call_group);
     }
     $xml .= "\t\t</groups>\n";
     $xml .= "\n";
     $xml .= "\t</domain>\n";
     $xml .= "</include>";
     //remove invalid characters from the file names
     $extension_dir_name = str_replace(" ", "_", $extension_dir_name);
     $extension_dir_name = preg_replace("/[\\*\\:\\/\\<\\>\\|\\'\"\\?]/", "", $extension_dir_name);
     //write the xml file
     $fout = fopen($extension_parent_dir . "/" . $extension_dir_name . ".xml", "w");
     fwrite($fout, $xml);
     unset($xml);
     fclose($fout);
     //syncrhonize the phone directory
     sync_directory();
     //apply settings reminder
     $_SESSION["reload_xml"] = true;
     //call reloadxml direct
     //$cmd = "api reloadxml";
     //event_socket_request_cmd($cmd);
     //unset($cmd);
 }
Example #2
0
 function sync_directory()
 {
     global $v_id, $db;
     $v_settings_array = v_settings();
     foreach ($v_settings_array as $name => $value) {
         ${$name} = $value;
     }
     $tmp = "include(\"config.js\");\n";
     $tmp .= "//var sounds_dir\n";
     $tmp .= "var admin_pin = \"\";\n";
     $tmp .= "var search_type = \"\";\n";
     $tmp .= "//var tmp_dir\n";
     $tmp .= "var digitmaxlength = 0;\n";
     $tmp .= "var timeoutpin = 5000;\n";
     $tmp .= "var timeouttransfer = 5000;\n";
     $tmp .= "\n";
     $tmp .= "var dtmf = new Object( );\n";
     $tmp .= "dtmf.digits = \"\";\n";
     $tmp .= "\n";
     $tmp .= "function mycb( session, type, obj, arg ) {\n";
     $tmp .= "\ttry {\n";
     $tmp .= "\t\tif ( type == \"dtmf\" ) {\n";
     $tmp .= "\t\t\tconsole_log( \"info\", \"digit: \"+obj.digit+\"\\n\" );\n";
     $tmp .= "\t\t\tif ( obj.digit == \"#\" ) {\n";
     $tmp .= "\t\t\t\t//console_log( \"info\", \"detected pound sign.\\n\" );\n";
     $tmp .= "\t\t\t\texit = true;\n";
     $tmp .= "\t\t\t\treturn( false );\n";
     $tmp .= "\t\t\t}\n";
     $tmp .= "\t\t\tif ( obj.digit == \"*\" ) {\n";
     $tmp .= "\t\t\t\t//console_log( \"info\", \"detected pound sign.\\n\" );\n";
     $tmp .= "\t\t\t\texit = true;\n";
     $tmp .= "\t\t\t\treturn( false );\n";
     $tmp .= "\t\t\t}\n";
     $tmp .= "\t\t\tdtmf.digits += obj.digit;\n";
     $tmp .= "\t\t\tif ( dtmf.digits.length >= digitmaxlength ) {\n";
     $tmp .= "\t\t\t\texit = true;\n";
     $tmp .= "\t\t\t\treturn( false );\n";
     $tmp .= "\t\t\t}\n";
     $tmp .= "\t\t}\n";
     $tmp .= "\t} catch (e) {\n";
     $tmp .= "\t\tconsole_log( \"err\", e+\"\\n\" );\n";
     $tmp .= "\t}\n";
     $tmp .= "\treturn( true );\n";
     $tmp .= "} //end function mycb\n";
     $tmp .= "\n";
     $tmp .= "function directory_search(search_type) {\n";
     $tmp .= "\n";
     $tmp .= "\tdigitmaxlength = 3;\n";
     $tmp .= "\tsession.streamFile( sounds_dir+\"/en/us/callie/directory/48000/dir-enter-person.wav\");\n";
     $tmp .= "\tif (search_type == \"last_name\") {\n";
     $tmp .= "\t\tsession.streamFile( sounds_dir+\"/en/us/callie/directory/48000/dir-last_name.wav\", mycb, \"dtmf\");\n";
     $tmp .= "\t\tsession.streamFile( sounds_dir+\"/en/us/callie/directory/48000/dir-to_search_by.wav\", mycb, \"dtmf\");\n";
     $tmp .= "\t\tsession.streamFile( sounds_dir+\"/en/us/callie/directory/48000/dir-first_name.wav\", mycb, \"dtmf\");\n";
     $tmp .= "\t}\n";
     $tmp .= "\tif (search_type == \"first_name\") {\n";
     $tmp .= "\t\tsession.streamFile( sounds_dir+\"/en/us/callie/directory/48000/dir-first_name.wav\", mycb, \"dtmf\");\n";
     $tmp .= "\t\tsession.streamFile( sounds_dir+\"/en/us/callie/directory/48000/dir-to_search_by.wav\", mycb, \"dtmf\");\n";
     $tmp .= "\t\tsession.streamFile( sounds_dir+\"/en/us/callie/directory/48000/dir-last_name.wav\", mycb, \"dtmf\");\n";
     $tmp .= "\t}\n";
     $tmp .= "\tsession.streamFile( sounds_dir+\"/en/us/callie/directory/48000/dir-press.wav\", mycb, \"dtmf\");\n";
     $tmp .= "\tsession.execute(\"say\", \"en name_spelled iterated 1\");\n";
     $tmp .= "\tsession.collectInput( mycb, dtmf, timeoutpin );\n";
     $tmp .= "\tvar dtmf_search = dtmf.digits;\n";
     $tmp .= "\t//console_log( \"info\", \"--\" + dtmf.digits + \"--\\n\" );\n";
     $tmp .= "\tif (dtmf_search == \"1\") {\n";
     $tmp .= "\t\t//console_log( \"info\", \"press 1 detected: \" + dtmf.digits + \"\\n\" );\n";
     $tmp .= "\t\t//console_log( \"info\", \"press 1 detected: \" + search_type + \"\\n\" );\n";
     $tmp .= "\t\tif (search_type == \"last_name\") {\n";
     $tmp .= "\t\t\t//console_log( \"info\", \"press 1 detected last_name: \" + search_type + \"\\n\" );\n";
     $tmp .= "\t\t\tsearch_type = \"first_name\";\n";
     $tmp .= "\t\t}\n";
     $tmp .= "\t\telse {\n";
     $tmp .= "\t\t\t//console_log( \"info\", \"press 1 detected first_name: \" + search_type + \"\\n\" );\n";
     $tmp .= "\t\t\tsearch_type = \"last_name\";\n";
     $tmp .= "\t\t}\n";
     $tmp .= "\t\tdtmf_search = \"\";\n";
     $tmp .= "\t\tdtmf.digits = \"\";\n";
     $tmp .= "\t\tdirectory_search(search_type);\n";
     $tmp .= "\t\treturn;\n";
     $tmp .= "\t}\n";
     $tmp .= "\tconsole_log( \"info\", \"first 3 letters of first or last name: \" + dtmf.digits + \"\\n\" );\n";
     $tmp .= "\n";
     $tmp .= "\t//session.execute(\"say\", \"en name_spelled pronounced mark\");\n";
     $tmp .= "\t//<action application=\"say\" data=\"en name_spelled iterated \${destination_number}\"/>\n";
     $tmp .= "\t//session.execute(\"say\", \"en number iterated 12345\");\n";
     $tmp .= "\t//session.execute(\"say\", \"en number pronounced 1001\");\n";
     $tmp .= "\t//session.execute(\"say\", \"en short_date_time pronounced [timestamp]\");\n";
     $tmp .= "\t//session.execute(\"say\", \"en CURRENT_TIME pronounced CURRENT_TIME\");\n";
     $tmp .= "\t//session.execute(\"say\", \"en CURRENT_DATE pronounced CURRENT_DATE\");\n";
     $tmp .= "\t//session.execute(\"say\", \"en CURRENT_DATE_TIME pronounced CURRENT_DATE_TIME\");\n";
     $tmp .= "\n";
     $tmp .= "\n";
     $tmp .= "\t//take each name and convert it to the equivalent number in php when this file is generated\n";
     $tmp .= "\t//then test each number see if it matches the user dtmf search keys\n";
     $tmp .= "\n";
     $tmp .= "\tvar result_array = new Array();\n";
     $tmp .= "\tvar x = 0;\n";
     //get a list of extensions and the users assigned to them
     $sql = "";
     $sql .= " select * from v_extensions ";
     $sql .= "where v_id = '{$v_id}' ";
     $prepstatement = $db->prepare(check_sql($sql));
     $prepstatement->execute();
     $x = 0;
     $result = $prepstatement->fetchAll();
     foreach ($result as &$row) {
         //print_r($row);
         $extension = $row["extension"];
         $effective_caller_id_name = $row["effective_caller_id_name"];
         $user_list = $row["user_list"];
         $user_list = trim($user_list, "|");
         //echo $user_list."<br />\n";
         $username_array = explode("|", $user_list);
         //print_r($username_array);
         foreach ($username_array as &$username) {
             if (strlen($username) > 0) {
                 $sql = "";
                 $sql .= "select * from v_users ";
                 $sql .= "where v_id = '{$v_id}' ";
                 $sql .= "and username = '******' ";
                 $prepstatement = $db->prepare(check_sql($sql));
                 $prepstatement->execute();
                 $tmp_result = $prepstatement->fetchAll();
                 foreach ($tmp_result as &$row_tmp) {
                     $userfirstname = $row_tmp["userfirstname"];
                     $userlastname = $row_tmp["userlastname"];
                     if ($userfirstname == "na") {
                         $userfirstname = "";
                     }
                     if ($userlastname == "na") {
                         $userlastname = "";
                     }
                     if (strlen($userfirstname . $userlastname) == 0) {
                         $name_array = explode(" ", $effective_caller_id_name);
                         $userfirstname = $name_array[0];
                         if (count($name_array) > 1) {
                             $userlastname = $name_array[1];
                         }
                     }
                     break;
                     //limit to 1 row
                 }
                 $f1 = phone_letter_to_number(substr($userfirstname, 0, 1));
                 $f2 = phone_letter_to_number(substr($userfirstname, 1, 1));
                 $f3 = phone_letter_to_number(substr($userfirstname, 2, 1));
                 $l1 = phone_letter_to_number(substr($userlastname, 0, 1));
                 $l2 = phone_letter_to_number(substr($userlastname, 1, 1));
                 $l3 = phone_letter_to_number(substr($userlastname, 2, 1));
                 //echo $sql." extension: $extension  firstname $userfirstname lastname $userlastname $tmp<br />";
                 $tmp .= "\tif (search_type == \"first_name\" && dtmf_search == \"" . $f1 . $f2 . $f3 . "\" || search_type == \"last_name\" && dtmf_search == \"" . $l1 . $l2 . $l3 . "\") {\n";
                 $tmp .= "\t\tresult_array[x]=new Array()\n";
                 $tmp .= "\t\tresult_array[x]['first_name'] =\"" . $userfirstname . "\";\n";
                 $tmp .= "\t\tresult_array[x]['last_name'] =\"" . $userlastname . "\";\n";
                 $tmp .= "\t\tresult_array[x]['extension'] = \"" . $extension . "\";\n";
                 $tmp .= "\t\t//console_log( \"info\", \"found: " . $userfirstname . " " . $userlastname . "\\n\" );\n";
                 $tmp .= "\t\tx++;\n";
                 $tmp .= "\t}\n";
             }
         }
     }
     unset($prepstatement);
     $tmp .= "\n";
     $tmp .= "\n";
     $tmp .= "\t//say the number of results that matched\n";
     $tmp .= "\t\$result_count = result_array.length;\n";
     $tmp .= "\tsession.execute(\"say\", \"en number iterated \"+\$result_count);\n";
     $tmp .= "\tsession.streamFile( sounds_dir+\"/en/us/callie/directory/48000/dir-result_match.wav\", mycb, \"dtmf\");\n";
     $tmp .= "\n";
     $tmp .= "\t//clear values\n";
     $tmp .= "\tdtmf_search = 0;\n";
     $tmp .= "\tdtmf.digits = '';\n";
     $tmp .= "\n";
     $tmp .= "\tif (\$result_count == 0) {\n";
     $tmp .= "\t\t//session.execute(\"transfer\", \"*347 XML default\");\n";
     $tmp .= "\t\tdirectory_search(search_type);\n";
     $tmp .= "\t\treturn;\n";
     $tmp .= "\t}\n";
     $tmp .= "\n";
     $tmp .= "\tsession.execute(\"set\", \"tts_engine=flite\");\n";
     $tmp .= "\tsession.execute(\"set\", \"tts_voice=rms\");  //rms //kal //awb //slt\n";
     $tmp .= "\tsession.execute(\"set\", \"playback_terminators=#\");\n";
     $tmp .= "\t//session.speak(\"flite\",\"kal\",\"Thanks for.. calling\");\n";
     $tmp .= "\n";
     $tmp .= "\ti=1;\n";
     $tmp .= "\tfor ( i in result_array ) {\n";
     $tmp .= "\n";
     $tmp .= "\t\t//say first name and last name is at extension 1001\n";
     $tmp .= "\t\t//session.execute(\"speak\", result_array[i]['first_name']);\n";
     $tmp .= "\t\t//session.execute(\"speak\", result_array[i]['last_name']);\n";
     $tmp .= "\t\tsession.execute(\"say\", \"en name_spelled pronounced \"+result_array[i]['first_name']);\n";
     $tmp .= "\t\tsession.execute(\"sleep\", \"500\");\n";
     $tmp .= "\t\tsession.execute(\"say\", \"en name_spelled pronounced \"+result_array[i]['last_name']);\n";
     $tmp .= "\t\tsession.streamFile( sounds_dir+\"/en/us/callie/directory/48000/dir-at_extension.wav\", mycb, \"dtmf\");\n";
     $tmp .= "\t\tsession.execute(\"say\", \"en number pronounced \"+result_array[i]['extension']);\n";
     $tmp .= "\n";
     $tmp .= "\t\t//to select this entry press 1\n";
     $tmp .= "\t\tsession.streamFile( sounds_dir+\"/en/us/callie/directory/48000/dir-to_select_entry.wav\", mycb, \"dtmf\");\n";
     $tmp .= "\t\tsession.streamFile( sounds_dir+\"/en/us/callie/directory/48000/dir-press.wav\", mycb, \"dtmf\");\n";
     $tmp .= "\t\tsession.execute(\"say\", \"en number iterated 1\");\n";
     $tmp .= "\n";
     $tmp .= "\t\t//console_log( \"info\", \"first name: \" + result_array[i]['first_name'] + \"\\n\" );\n";
     $tmp .= "\t\t//console_log( \"info\", \"last name: \" + result_array[i]['last_name'] + \"\\n\" );\n";
     $tmp .= "\t\t//console_log( \"info\", \"extension: \" + result_array[i]['extension'] + \"\\n\" );\n";
     $tmp .= "\n";
     $tmp .= "\t\t//if 1 is pressed then transfer the call\n";
     $tmp .= "\t\tdtmf.digits = session.getDigits(1, \"#\", 3000);\n";
     $tmp .= "\t\tif (dtmf.digits == \"1\") {\n";
     $tmp .= "\t\t\tconsole_log( \"info\", \"directory: call transfered to: \" + result_array[i]['extension'] + \"\\n\" );\n";
     $tmp .= "\t\t\tsession.execute(\"transfer\", result_array[i]['extension']+\" XML default\");\n";
     $tmp .= "\t\t}\n";
     $tmp .= "\n";
     $tmp .= "\t}\n";
     $tmp .= "}\n";
     $tmp .= "\n";
     $tmp .= "\n";
     $tmp .= "if ( session.ready() ) {\n";
     $tmp .= "\tsession.answer();\n";
     $tmp .= "\tsearch_type = \"last_name\";\n";
     $tmp .= "\tdirectory_search(search_type);\n";
     $tmp .= "\tsession.hangup(\"NORMAL_CLEARING\");\n";
     $tmp .= "}\n";
     $tmp .= "";
     //write the file
     $fout = fopen($v_scripts_dir . "/directory.js", "w");
     fwrite($fout, $tmp);
     fclose($fout);
 }
Example #3
0
 function sync_package_v_ivr_menu()
 {
     global $db, $v_id;
     $v_settings_array = v_settings();
     foreach ($v_settings_array as $name => $value) {
         ${$name} = $value;
     }
     //prepare for dialplan .xml files to be written. delete all dialplan files that are prefixed with dialplan_ and have a file extension of .xml
     $v_needle = 'v_';
     if ($dh = opendir($v_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($v_conf_dir . "/ivr_menus/" . $file);
                     }
                 }
             }
         }
         closedir($dh);
     }
     $sql = "";
     $sql .= " select * from v_ivr_menu ";
     $sql .= " where v_id = '{$v_id}' ";
     if (strlen($orderby) > 0) {
         $sql .= "order by {$orderby} {$order} ";
     }
     $sql .= " ";
     $prepstatement = $db->prepare(check_sql($sql));
     $prepstatement->execute();
     $result = $prepstatement->fetchAll();
     $resultcount = count($result);
     unset($prepstatement, $sql);
     if ($resultcount == 0) {
         //no results
     } else {
         //received results
         foreach ($result as $row) {
             $ivr_menu_id = $row["ivr_menu_id"];
             $ivr_menu_name = $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_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_desc = $row["ivr_menu_desc"];
             //replace space with an underscore
             $ivr_menu_name = str_replace(" ", "_", $ivr_menu_name);
             //add each IVR Menu to the dialplan
             if (strlen($row['ivr_menu_id']) > 0) {
                 $action = 'add';
                 //set default action to add
                 $i = 0;
                 $sql = "";
                 $sql .= "select * from v_dialplan_includes ";
                 $sql .= "where v_id = '{$v_id}' ";
                 $sql .= "and opt1name = 'ivr_menu_id' ";
                 $sql .= "and opt1value = '" . $row['ivr_menu_id'] . "' ";
                 $prepstatement2 = $db->prepare($sql);
                 $prepstatement2->execute();
                 while ($row2 = $prepstatement2->fetch()) {
                     $action = 'update';
                     $dialplan_include_id = $row2['dialplan_include_id'];
                     break;
                     //limit to 1 row
                 }
                 unset($sql, $prepstatement2);
                 //echo "sql: ".$sql."<br />";
                 if ($action == 'add') {
                     //create IVR Menu extension in the dialplan
                     $extensionname = $ivr_menu_name;
                     $dialplanorder = '9001';
                     //$context = $row['ivr_menu_context'];
                     $context = 'default';
                     $enabled = 'true';
                     $descr = 'ivr_menu';
                     $opt1name = 'ivr_menu_id';
                     $opt1value = $row['ivr_menu_id'];
                     $dialplan_include_id = v_dialplan_includes_add($v_id, $extensionname, $dialplanorder, $context, $enabled, $descr, $opt1name, $opt1value);
                     $tag = 'condition';
                     //condition, action, antiaction
                     $fieldtype = 'destination_number';
                     $fielddata = '^' . $row['ivr_menu_extension'] . '$';
                     $fieldorder = '000';
                     v_dialplan_includes_details_add($v_id, $dialplan_include_id, $tag, $fieldorder, $fieldtype, $fielddata);
                     $tag = 'action';
                     //condition, action, antiaction
                     $fieldtype = 'answer';
                     $fielddata = '';
                     $fieldorder = '001';
                     v_dialplan_includes_details_add($v_id, $dialplan_include_id, $tag, $fieldorder, $fieldtype, $fielddata);
                     $tag = 'action';
                     //condition, action, antiaction
                     $fieldtype = 'sleep';
                     $fielddata = '2000';
                     $fieldorder = '002';
                     v_dialplan_includes_details_add($v_id, $dialplan_include_id, $tag, $fieldorder, $fieldtype, $fielddata);
                     $tag = 'action';
                     //condition, action, antiaction
                     $fieldtype = 'ivr';
                     $fielddata = $ivr_menu_name;
                     $fieldorder = '003';
                     v_dialplan_includes_details_add($v_id, $dialplan_include_id, $tag, $fieldorder, $fieldtype, $fielddata);
                 }
                 if ($action == 'update') {
                     //update the IVR menu
                     $extensionname = $ivr_menu_name;
                     $dialplanorder = '9001';
                     //$context = $row['ivr_menu_context'];
                     $context = 'default';
                     $enabled = 'true';
                     $descr = 'ivr_menu';
                     $ivr_menu_id = $row['ivr_menu_id'];
                     $sql = "";
                     $sql = "update v_dialplan_includes set ";
                     $sql .= "extensionname = '{$extensionname}', ";
                     $sql .= "dialplanorder = '{$dialplanorder}', ";
                     $sql .= "context = '{$context}', ";
                     $sql .= "enabled = '{$enabled}', ";
                     $sql .= "descr = '{$descr}' ";
                     $sql .= "where v_id = '{$v_id}' ";
                     $sql .= "and opt1name = 'ivr_menu_id' ";
                     $sql .= "and opt1value = '{$ivr_menu_id}' ";
                     //echo "sql: ".$sql."<br />";
                     //exit;
                     $db->query($sql);
                     unset($sql);
                     //update the condition
                     $sql = "";
                     $sql = "update v_dialplan_includes_details set ";
                     $sql .= "fielddata = '^" . $row['ivr_menu_extension'] . "\$' ";
                     $sql .= "where v_id = '{$v_id}' ";
                     $sql .= "and tag = 'condition' ";
                     $sql .= "and fieldtype = 'destination_number' ";
                     $sql .= "and dialplan_include_id = '{$dialplan_include_id}' ";
                     //echo $sql."<br />";
                     //exit;
                     $db->query($sql);
                     unset($sql);
                     //update the action
                     $sql = "";
                     $sql = "update v_dialplan_includes_details set ";
                     $sql .= "fielddata = '" . $ivr_menu_name . "' ";
                     $sql .= "where v_id = '{$v_id}' ";
                     $sql .= "and tag = 'action' ";
                     $sql .= "and fieldtype = 'ivr' ";
                     $sql .= "and dialplan_include_id = '{$dialplan_include_id}' ";
                     //echo $sql."<br />";
                     $db->query($sql);
                     unset($extensionname);
                     unset($order);
                     unset($context);
                     unset($enabled);
                     unset($descr);
                     unset($opt1name);
                     unset($opt1value);
                 }
                 unset($action);
                 unset($dialplanincludeid);
             }
             //end if strlen ivr_menu_id; add the IVR Menu to the dialplan
             //add each IVR menu to the XML config
             $tmp = "\t<!-- {$ivr_menu_desc} -->\n";
             $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 = "";
             $sub_sql .= "select * from v_ivr_menu_options ";
             $sub_sql .= "where ivr_menu_id = '{$ivr_menu_id}' ";
             $sub_sql .= "and v_id = '{$v_id}' ";
             $sub_sql .= "order by ivr_menu_options_order asc ";
             $sub_prepstatement = $db->prepare(check_sql($sub_sql));
             $sub_prepstatement->execute();
             $sub_result = $sub_prepstatement->fetchAll();
             foreach ($sub_result as &$sub_row) {
                 //$ivr_menu_id = $sub_row["ivr_menu_id"];
                 $ivr_menu_options_digits = $sub_row["ivr_menu_options_digits"];
                 $ivr_menu_options_action = $sub_row["ivr_menu_options_action"];
                 $ivr_menu_options_param = $sub_row["ivr_menu_options_param"];
                 $ivr_menu_options_desc = $sub_row["ivr_menu_options_desc"];
                 $tmp .= "\t\t<entry action=\"{$ivr_menu_options_action}\" digits=\"{$ivr_menu_options_digits}\" param=\"{$ivr_menu_options_param}\"/>";
                 if (strlen($ivr_menu_options_desc) == 0) {
                     $tmp .= "\n";
                 } else {
                     $tmp .= "\t<!-- {$ivr_menu_options_desc} -->\n";
                 }
             }
             unset($sub_prepstatement, $sub_row);
             if ($ivr_menu_direct_dial == "true") {
                 $tmp .= "\t\t<entry action=\"menu-exec-app\" digits=\"/(^\\*\\d{3,5}\$|^\\d{3,5}\$)/\" param=\"transfer \$1 XML default\"/>\n";
             }
             $tmp .= "\t</menu>";
             //write the file
             $fout = fopen($v_conf_dir . "/ivr_menus/v_" . $ivr_menu_name . ".xml", "w");
             fwrite($fout, $tmp);
             fclose($fout);
         }
     }
     sync_package_v_dialplan_includes();
 }