コード例 #1
0
    $broadcast_caller_id_name = $row["broadcast_caller_id_name"];
    $broadcast_caller_id_number = $row["broadcast_caller_id_number"];
    $broadcast_destination_type = $row["broadcast_destination_type"];
    $broadcast_destination_data = $row["broadcast_destination_data"];
    break;
    //limit to 1 row
}
unset($prepstatement);
if (strlen($broadcast_caller_id_name) == 0) {
    $broadcast_caller_id_name = "anonymous";
}
if (strlen($broadcast_caller_id_number) == 0) {
    $broadcast_caller_id_number = "0000000000";
}
//get the recording name
$recording_filename = get_recording_filename($recordingid);
//remove unsafe characters from the name
$broadcast_name = str_replace(" ", "", $broadcast_name);
$broadcast_name = str_replace("'", "", $broadcast_name);
//$tmpjs .= "function extract_numbers(str){\n";
//$tmpjs .= "	return str.match(/\\d+/g);\n";
//start the count at 0
$response = exec($bin_dir . "/fs_cli -x \"global_setvar broadcast_" . $broadcast_name . "_count=0\"");
echo "<div align='center'>";
echo "<table width='100%' border='0' cellpadding='0' cellspacing='2'>\n";
echo "<tr class='border'>\n";
echo "\t<td align=\"center\">\n";
echo "\t\t<br>";
echo "<table width='100%' border='0'><tr>\n";
echo "<td width='50%' nowrap><b>Contact List</b></td>\n";
echo "<td width='50%' align='right'>&nbsp;</td>\n";
コード例 #2
0
ファイル: lib_switch.php プロジェクト: petekelly/fusionpbx
function sync_package_v_auto_attendant()
{
    global $db, $v_id, $host;
    $v_settings_array = v_settings();
    foreach ($v_settings_array as $name => $value) {
        ${$name} = $value;
    }
    $db->beginTransaction();
    //prepare for auto attendant .js files to be written. delete all auto attendants that are prefixed with autoattendant_ and have a file extension of .js
    $v_prefix = 'autoattendant_';
    if ($dh = opendir($v_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, -3) == '.js') {
                        //echo "file: $file<br />\n";
                        //echo "extension: ".substr($file,-3)."<br />";
                        unlink($v_scripts_dir . '/' . $file);
                    }
                }
            }
        }
        closedir($dh);
    }
    //loop through all auto attendants
    $sql = "";
    $sql .= "select * from v_auto_attendant ";
    $sql .= "where v_id = '{$v_id}' ";
    $prepstatement = $db->prepare(check_sql($sql));
    $prepstatement->execute();
    $result = $prepstatement->fetchAll();
    foreach ($result as &$row) {
        //add the auto attendant to the dialplan
        if (strlen($row['auto_attendant_id']) > 0) {
            $action = 'add';
            //set default action to add
            $sql = "";
            $sql .= "select * from v_dialplan_includes ";
            $sql .= "where v_id = '{$v_id}' ";
            $sql .= "and opt1name = 'auto_attendant_id' ";
            $sql .= "and opt1value = '" . $row['auto_attendant_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);
        }
        if ($action == 'add') {
            //create auto attendant extension in the dialplan
            $extensionname = $row['aaextension'];
            $dialplanorder = '9001';
            $context = $row['aacontext'];
            $enabled = 'true';
            $descr = 'auto attendant';
            $opt1name = 'auto_attendant_id';
            $opt1value = $row['auto_attendant_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['aaextension'] . '$';
            $fieldorder = '000';
            v_dialplan_includes_details_add($v_id, $dialplan_include_id, $tag, $fieldorder, $fieldtype, $fielddata);
            $tag = 'action';
            //condition, action, antiaction
            $fieldtype = 'javascript';
            $fielddata = 'autoattendant_' . $row['aaextension'] . '.js';
            $fieldorder = '001';
            v_dialplan_includes_details_add($v_id, $dialplan_include_id, $tag, $fieldorder, $fieldtype, $fielddata);
        }
        if ($action == 'update') {
            $extensionname = $row['aaextension'];
            $dialplanorder = '9001';
            $context = $row['aacontext'];
            $enabled = 'true';
            $descr = 'auto attendant';
            $auto_attendant_id = $row['auto_attendant_id'];
            //update the main dialplan entry
            $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 = 'auto_attendant_id' ";
            $sql .= "and opt1value = '{$auto_attendant_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['aaextension'] . "\$' ";
            $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 />";
            $db->query($sql);
            unset($sql);
            //update the action
            $sql = "";
            $sql = "update v_dialplan_includes_details set ";
            $sql .= "fielddata = 'autoattendant_" . $row['aaextension'] . ".js' ";
            $sql .= "where v_id = '{$v_id}' ";
            $sql .= "and tag = 'action' ";
            $sql .= "and fieldtype = 'javascript' ";
            $sql .= "and dialplan_include_id = '{$dialplan_include_id}' ";
            //echo $sql."<br />";
            $db->query($sql);
            unset($sql);
            unset($ent);
            unset($extensionname);
            unset($dialplanorder);
            unset($context);
            unset($enabled);
            unset($descr);
            unset($opt1name);
            unset($opt1value);
        }
        sync_package_v_dialplan_includes();
        unset($dialplanincludeid);
        // Build the auto attendant javascript
        $recording_action_filename = get_recording_filename($row['recordingidaction']);
        $recording_antiaction_filename = get_recording_filename($row['recordingidantiaction']);
        $sql = "";
        $sql .= "select * from v_settings ";
        $sql .= "where v_id = '{$v_id}' ";
        $prepstatement2 = $db->prepare($sql);
        $prepstatement2->execute();
        while ($row2 = $prepstatement2->fetch()) {
            $event_socket_ip_address = $row2["event_socket_ip_address"];
            $event_socket_port = $row2["event_socket_port"];
            $event_socket_password = $row2["event_socket_password"];
        }
        unset($prepstatement2);
        if (pkg_is_service_running('freeswitch')) {
            $fp = event_socket_create($event_socket_ip_address, $event_socket_port, $event_socket_password);
            $cmd = "api global_getvar domain";
            $domain = trim(event_socket_request($fp, $cmd));
        }
        $tmp = "";
        //make sure the variable starts with no value
        $tmp .= "\n";
        $tmp .= " var condition = true;\n";
        $tmp .= "\n";
        $tmp .= " var domain = \"" . $domain . "\"; //by default this is the ipv4 address of FreeSWITCH used for transfer to voicemail\n";
        $tmp .= " var digitmaxlength = 0;\n";
        $tmp .= " var objdate = new Date();\n";
        $tmp .= "\n";
        $tmp .= " var adjusthours = 0; //Adjust Server time that is set to GMT 7 hours\n";
        $tmp .= " var adjustoperator = \"-\"; //+ or -\n";
        $tmp .= "\n";
        $tmp .= " if (adjustoperator == \"-\") {\n";
        $tmp .= "   var objdate2 = new Date(objdate.getFullYear(),objdate.getMonth(),objdate.getDate(),(objdate.getHours() - adjusthours),objdate.getMinutes(),objdate.getSeconds());\n";
        $tmp .= " }\n";
        $tmp .= " if (adjustoperator == \"+\") {\n";
        $tmp .= "   var objdate2 = new Date(objdate.getFullYear(),objdate.getMonth(),objdate.getDate(),(objdate.getHours() + adjusthours),objdate.getMinutes(),objdate.getSeconds());\n";
        $tmp .= " }\n";
        $tmp .= "\n";
        $tmp .= " var Hours = objdate2.getHours();\n";
        $tmp .= " var Mins = objdate2.getMinutes();\n";
        $tmp .= " var Seconds = objdate2.getSeconds();\n";
        $tmp .= " var Month = objdate2.getMonth() + 1;\n";
        $tmp .= " var Date = objdate2.getDate();\n";
        $tmp .= " var Year = objdate2.getYear()\n";
        $tmp .= " var Day = objdate2.getDay()+1;\n";
        $tmp .= " var exit = false;\n";
        $tmp .= "\n";
        $tmp .= " dialed_extension = session.getVariable(\"dialed_extension\");\n";
        $tmp .= " domain_name = session.getVariable(\"domain_name\");\n";
        $tmp .= " domain = session.getVariable(\"domain\");\n";
        $tmp .= " us_ring = session.getVariable(\"us-ring\");\n";
        $tmp .= " caller_id_name = session.getVariable(\"caller_id_name\");\n";
        $tmp .= " caller_id_number = session.getVariable(\"caller_id_number\");\n";
        $tmp .= " effective_caller_id_name = session.getVariable(\"effective_caller_id_name\");\n";
        $tmp .= " effective_caller_id_number = session.getVariable(\"effective_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";
        //set caller id prefix
        if (strlen($row['aacidnameprefix']) > 0) {
            $tmp .= "session.execute(\"set\", \"caller_id_name=" . $row['aacidnameprefix'] . "\"+caller_id_name);\n";
            $tmp .= "session.execute(\"set\", \"effective_caller_id_name=" . $row['aacidnameprefix'] . "\"+effective_caller_id_name);\n";
            $tmp .= "session.execute(\"set\", \"outbound_caller_id_name=" . $row['aacidnameprefix'] . "\"+outbound_caller_id_name);\n";
        }
        $tmp .= "\n";
        $tmp .= "session.execute(\"set\", \"ignore_early_media=true\");\n";
        $tmp .= " session.execute(\"set\", \"hangup_after_bridge=true\");\n";
        $tmp .= " session.execute(\"set\", \"continue_on_fail=true\");\n";
        if (strlen($row['aacalltimeout']) == 0) {
            $tmp .= " session.execute(\"set\", \"call_timeout=30\");\n";
            //aacalltimeout
            $tmp .= " session.execute(\"export\", \"call_timeout=30\");\n";
            //aacalltimeout
        } else {
            $tmp .= " session.execute(\"set\", \"call_timeout=" . $row['aacalltimeout'] . "\");\n";
            //aacalltimeout
            $tmp .= " session.execute(\"export\", \"call_timeout=" . $row['aacalltimeout'] . "\");\n";
            //aacalltimeout
        }
        if (isset($row['aaringback'])) {
            if ($row['aaringback'] == "ring") {
                $tmp .= " session.execute(\"set\", \"ringback=\"+us_ring);          //set to ringtone\n";
                $tmp .= " session.execute(\"set\", \"transfer_ringback=\"+us_ring); //set to ringtone\n";
            }
            if ($row['aaringback'] == "music") {
                $tmp .= " session.execute(\"set\", \"ringback=\${hold_music}\");          //set to ringtone\n";
                $tmp .= " session.execute(\"set\", \"transfer_ringback=\${hold_music}\"); //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";
        }
        $tmp .= "\n";
        $tmp .= "//console_log( \"info\", \"Auto Attendant Server Time is: \"+Hours+\":\"+Mins+\" \\n\" );\n";
        $tmp .= "\n";
        $tmp .= " function get_sofia_contact(extension,domain_name, profile){\n";
        $tmp .= "\tif (profile == \"auto\") {\n";
        $i = 0;
        foreach (ListFiles($v_conf_dir . '/sip_profiles') as $key => $sip_profile_file) {
            $sip_profile_name = str_replace(".xml", "", $sip_profile_file);
            if ($i == 0) {
                $tmp .= "\t\t\tprofile = \"" . $sip_profile_name . "\";\n";
                $tmp .= "\t\t\tsession.execute(\"set\", \"sofia_contact_\"+extension+\"=\${sofia_contact(\"+profile+\"/\"+extension+\"@\"+domain_name+\")}\");\n";
                $tmp .= "\t\t\tsofia_contact = session.getVariable(\"sofia_contact_\"+extension);\n";
            } else {
                $tmp .= "\n";
                $tmp .= "\t\t\tif (sofia_contact == \"error/user_not_registered\") {\n";
                $tmp .= "\t\t\t\tprofile = \"" . $sip_profile_name . "\";\n";
                $tmp .= "\t\t\t\tsession.execute(\"set\", \"sofia_contact_\"+extension+\"=\${sofia_contact(\"+profile+\"/\"+extension+\"@\"+domain_name+\")}\");\n";
                $tmp .= "\t\t\t\tsofia_contact = session.getVariable(\"sofia_contact_\"+extension);\n";
                $tmp .= "\t\t\t}\n";
            }
            $i++;
        }
        unset($i);
        $tmp .= "\t}\n";
        $tmp .= "\telse {\n";
        $tmp .= "\t\tsession.execute(\"set\", \"sofia_contact_\"+extension+\"=\${sofia_contact(\"+profile+\"/\"+extension+\"@\"+domain_name+\")}\");\n";
        $tmp .= "\t\tsofia_contact = session.getVariable(\"sofia_contact_\"+extension);\n";
        $tmp .= "\t}\n";
        $tmp .= "\tconsole_log( \"info\", \"sofia_contact \"+profile+\": \"+sofia_contact+\".\\n\" );\n";
        $tmp .= "\treturn sofia_contact;\n";
        $tmp .= " }\n";
        $tmp .= "\n";
        $tmp .= " function mycb( session, type, obj, arg ) {\n";
        $tmp .= "    try {\n";
        $tmp .= "        if ( type == \"dtmf\" ) {\n";
        $tmp .= "          console_log( \"info\", \"digit: \"+obj.digit+\"\\n\" );\n";
        $tmp .= "          if ( obj.digit == \"#\" ) {\n";
        $tmp .= "            //console_log( \"info\", \"detected pound sign.\\n\" );\n";
        $tmp .= "            exit = true;\n";
        $tmp .= "            return( false );\n";
        $tmp .= "          }\n";
        $tmp .= "\n";
        $tmp .= "          dtmf.digits += obj.digit;\n";
        $tmp .= "\n";
        $tmp .= "          if ( dtmf.digits.length >= digitmaxlength ) {\n";
        $tmp .= "            exit = true;\n";
        $tmp .= "            return( false );\n";
        $tmp .= "          }\n";
        $tmp .= "        }\n";
        $tmp .= "    } catch (e) {\n";
        $tmp .= "        console_log( \"err\", e+\"\\n\" );\n";
        $tmp .= "    }\n";
        $tmp .= "    return( true );\n";
        $tmp .= " } //end function mycb\n";
        $tmp .= "\n";
        //condition
        $tmp .= $row['aaconditionjs'];
        $tmp .= "\n";
        $tmp .= "\n";
        //$tmp .= " //condition = true; //debugging\n";
        $actiondirect = false;
        $actiondefault = false;
        $actioncount = 0;
        $sql = "";
        $sql .= "select * from v_auto_attendant_options ";
        $sql .= "where v_id = '{$v_id}' ";
        $sql .= "and auto_attendant_id = '" . $row['auto_attendant_id'] . "' ";
        //echo $sql;
        $prepstatement2 = $db->prepare($sql);
        $prepstatement2->execute();
        while ($row2 = $prepstatement2->fetch()) {
            //$auto_attendant_id = $row2["auto_attendant_id"];
            //$optionaction = $row2["optionaction"];
            //$optionnumber = $row2["optionnumber"];
            //$optiontype = $row2["optiontype"];
            //$optionprofile = $row2["optionprofile"];
            //$optiondata = $row2["optiondata"];
            //$optiondescr = $row2["optiondescr"];
            if ($row2['optionaction'] == "action") {
                $actioncount++;
                if (strtolower($row2['optionnumber']) == "n") {
                    //direct the call now don't wait for dtmf
                    //echo "now found\n";
                    $actiondirect = true;
                    $actiondirecttype = $row2['optiontype'];
                    $actiondirectprofile = $row2['optionprofile'];
                    $actiondirectdest = $row2['optiondata'];
                    $actiondirectdesc = $row2['optiondesc'];
                }
                if (strtolower($row2['optionnumber']) == "d") {
                    //default option used when dtmf doesn't match any other option
                    //echo "default found\n";
                    $actiondefault = true;
                    $actiondefaulttype = $row2['optiontype'];
                    $actiondefaultprofile = $row2['optionprofile'];
                    $actiondefaultdest = $row2['optiondata'];
                    $actiondefaultdesc = $row2['optiondesc'];
                    $actiondefaultrecording = $row2['optionrecording'];
                }
            }
        }
        //end while
        unset($prepstatement2);
        //$tmp .= "action count: ".$actioncount."<br />\n";
        if ($actioncount > 0) {
            if ($actiondirect) {
                $tmp .= " if (condition) {\n";
                $tmp .= "    //direct\n";
                $tmp .= "    //console_log( \"info\", \"action direct\\n\" );\n";
                //play the option recording if it exists
                if (strlen($row2['optionrecording']) > 0) {
                    $option_recording_filename = get_recording_filename($row2['optionrecording']);
                    $tmp .= "    session.streamFile( \"" . $v_recordings_dir . "/" . $option_recording_filename . "\" );\n";
                }
                $tmp .= "    session.execute(\"" . $actiondirecttype . "\", \"" . $actiondirectdest . "\"); //" . $actiondirectdesc . "\n";
                //if ($actiondirecttype == "extension") {
                //	$tmp .= "    sofia_contact_".$actiondirectdest." = get_sofia_contact(\"".$actiondirectdest."\",domain_name, \"".$actiondirectprofile."\");\n";
                //	$tmp .= "    session.execute(\"bridge\", sofia_contact_".$actiondirectdest."); //".$actiondirectdest."\n";
                //	if ($actiondirectprofile == "auto") {
                //		$tmp .= "    session.execute(\"voicemail\", \"default \${domain} ".$actiondirectdest."\");\n";
                //	}
                //	else {
                //		$tmp .= "    session.execute(\"voicemail\", \"default \${domain} ".$actiondirectdest."\");\n";
                //	}
                //}
                //if ($actiondirecttype == "voicemail") {
                //	if ($actiondirectprofile == "auto") {
                //		$tmp .= "    session.execute(\"voicemail\", \"default \${domain} ".$actiondirectdest."\");\n";
                //	}
                //	else {
                //		$tmp .= "    session.execute(\"voicemail\", \"default \${domain} ".$actiondirectdest."\");\n";
                //	}
                //}
                //if ($actiondirecttype == "sip uri") {
                //	$tmp .= "    session.execute(\"bridge\", \"".$actiondirectdest."\"); //".$actiondirectdest."\n";
                //}
                $tmp .= "}\n";
            } else {
                $tmp .= " if (condition) {\n";
                $tmp .= "    //action\n";
                $tmp .= "\n";
                $tmp .= "     //console_log( \"info\", \"action call now don't wait for dtmf\\n\" );\n";
                $tmp .= "      var dtmf = new Object( );\n";
                $tmp .= "     dtmf.digits = \"\";\n";
                $tmp .= "     if ( session.ready( ) ) {\n";
                $tmp .= "         session.answer( );\n";
                $tmp .= "\n";
                $tmp .= "         digitmaxlength = 1;\n";
                $tmp .= "         while (session.ready() && ! exit ) {\n";
                $tmp .= "           session.streamFile( \"" . $v_recordings_dir . "/" . $recording_action_filename . "\", mycb, \"dtmf " . $row['aatimeout'] . "\" );\n";
                $tmp .= "           if (session.ready()) {\n";
                $tmp .= "           \tif (dtmf.digits.length == 0) {\n";
                $tmp .= "           \t\tdtmf.digits +=  session.getDigits(1, \"#\", " . $row['aatimeout'] * 1000 . "); // " . $row['aatimeout'] . " seconds\n";
                $tmp .= "           \t\tif (dtmf.digits.length == 0) {\n";
                //$tmp .= "           			console_log( "info", "time out option: " + dtmf.digits + "\n" );\n";
                //find the timeout auto attendant options with the correct action
                $sql = "";
                $sql .= "select * from v_auto_attendant_options ";
                $sql .= "where auto_attendant_id = '" . $row['auto_attendant_id'] . "' ";
                $sql .= "and v_id = '{$v_id}' ";
                $prepstatement2 = $db->prepare($sql);
                $prepstatement2->execute();
                while ($row2 = $prepstatement2->fetch()) {
                    //$auto_attendant_id = $row2["auto_attendant_id"];
                    //$optionaction = $row2["optionaction"];
                    //$optionnumber = $row2["optionnumber"];
                    //$optiontype = $row2["optiontype"];
                    //$optiondata = $row2["optiondata"];
                    //$optionprofile = $row2["optionprofile"];
                    //$optiondescr = $row2["optiondescr"];
                    if ($row2['optionaction'] == "action") {
                        if (strtolower($row2['optionnumber']) == "t") {
                            //play the option recording if it exists
                            if (strlen($row2['optionrecording']) > 0) {
                                $option_recording_filename = get_recording_filename($row2['optionrecording']);
                                $tmp .= "                 \tsession.streamFile( \"" . $v_recordings_dir . "/" . $option_recording_filename . "\" );\n";
                            }
                            $tmp .= "                 \tsession.execute(\"" . $row2['optiontype'] . "\", \"" . $row2['optiondata'] . "\"); //" . $row2['optiondescr'] . "\n";
                            //if ($row2['optiontype'] == "extension") {
                            //	$tmp .= "                 	sofia_contact_".$row2['optiondata']." = get_sofia_contact(\"".$row2['optiondata']."\",domain_name, \"".$row2['optionprofile']."\");\n";
                            //	$tmp .= "                 	session.execute(\"bridge\", sofia_contact_".$row2['optiondata']."); //".$row2['optiondescr']."\n";
                            //	if ($row2['optionprofile'] == "auto") {
                            //		$tmp .= "                 	session.execute(\"voicemail\", \"default \${domain} ".$row2['optiondata']."\");\n";
                            //	}
                            //	else {
                            //		$tmp .= "                 	session.execute(\"voicemail\", \"default \${domain} ".$row2['optiondata']."\");\n";
                            //	}
                            //}
                            //if ($row2['optiontype'] == "voicemail") {
                            //	if ($row2['optionprofile'] == "auto") {
                            //		$tmp .= "                 	session.execute(\"voicemail\", \"default \${domain} ".$row2['optiondata']."\"); //".$row2['optiondescr']."\n";
                            //	}
                            //	else {
                            //		$tmp .= "                 	session.execute(\"voicemail\", \"default \${domain} ".$row2['optiondata']."\"); //".$row2['optiondescr']."\n";
                            //	}
                            //}
                            //if ($row2['optiontype'] == "sip uri") {
                            //	$tmp .= "                 	session.execute(\"bridge\", \"".$row2['optiondata']."\"); //".$row2['optiondescr']."\n";
                            //}
                        }
                    }
                    //end anti-action
                }
                //end while
                unset($prepstatement2);
                $tmp .= "           \t\t}\n";
                $tmp .= "           \t\telse {\n";
                $tmp .= "           \t\t\tbreak; //dtmf found end the while loop\n";
                $tmp .= "           \t\t}\n";
                $tmp .= "           \t}\n";
                $tmp .= "           }\n";
                $tmp .= "         }\n";
                $tmp .= "\n";
                $tmp .= "         //pickup the remaining digits\n";
                //$tmp .= "         //http://wiki.freeswitch.org/wiki/Session_getDigits\n";
                //$tmp .= "         //getDigits(length, terminators, timeout, digit_timeout, abs_timeout)\n";
                //$tmp .= "       //dtmf.digits +=  session.getDigits(2, \"#\", 3000); //allow up to 3 digits\n";
                $tmp .= "         dtmf.digits +=  session.getDigits(4, \"#\", 3000); //allow up to 5 digits\n";
                $tmp .= "\n";
                $tmp .= "\n";
                //$tmp .= "         console_log( \"info\", \"Auto Attendant Digit Pressed: \" + dtmf.digits + \"\\n\" );\n";
                //action
                $tmpaction = "";
                $tmp .= "         if ( dtmf.digits.length > \"0\" ) {\n\n";
                $x = 0;
                $sql = "";
                $sql .= "select * from v_auto_attendant_options ";
                $sql .= "where auto_attendant_id = '" . $row['auto_attendant_id'] . "' ";
                $sql .= "and v_id = '{$v_id}' ";
                $prepstatement2 = $db->prepare($sql);
                $prepstatement2->execute();
                while ($row2 = $prepstatement2->fetch()) {
                    //$auto_attendant_id = $row2["auto_attendant_id"];
                    //$optionaction = $row2["optionaction"];
                    //$optionnumber = $row2["optionnumber"];
                    //$optiontype = $row2["optiontype"];
                    //$optiondata = $row2["optiondata"];
                    //$optionprofile = $row2["optionprofile"];
                    //$optiondescr = $row2["optiondescr"];
                    $tmpactiondefault = "";
                    if ($row2['optionaction'] == "action") {
                        //$tmpaction .= "\n";
                        switch ($row2['optionnumber']) {
                            //case "t":
                            //		break;
                            //case "d":
                            //		break;
                            default:
                                //$tmpaction .= "             //console_log( \"info\", \"Auto Attendant Detected 1 digit \\n\" );\n";
                                if ($x == 0) {
                                    $tmpaction .= "             if ( dtmf.digits == \"" . $row2['optionnumber'] . "\" ) { //" . $row2['optiondescr'] . "\n";
                                } else {
                                    $tmpaction .= "             else if ( dtmf.digits == \"" . $row2['optionnumber'] . "\" ) { //" . $row2['optiondescr'] . "\n";
                                }
                                //play the option recording if it was provided
                                if (strlen($row2['optionrecording']) > 0) {
                                    $option_recording_filename = get_recording_filename($row2['optionrecording']);
                                    $tmpaction .= "                 session.streamFile( \"" . $v_recordings_dir . "/" . $option_recording_filename . "\" );\n";
                                }
                                $tmpaction .= "                 session.execute(\"" . $row2['optiontype'] . "\", \"" . $row2['optiondata'] . "\"); //" . $row2['optiondescr'] . "\n";
                                //if ($row2['optiontype'] == "extension") {
                                //	$tmpaction .= "                 sofia_contact_".$row2['optiondata']." = get_sofia_contact(\"".$row2['optiondata']."\",domain_name, \"".$row2['optionprofile']."\");\n";
                                //	$tmpaction .= "                 session.execute(\"bridge\", sofia_contact_".$row2['optiondata']."); //".$row2['optiondescr']."\n";
                                //	if ($row2['optionprofile'] == "auto") {
                                //		$tmpaction .= "                 session.execute(\"voicemail\", \"default \${domain} ".$row2['optiondata']."\");\n";
                                //	}
                                //	else {
                                //		$tmpaction .= "                 session.execute(\"voicemail\", \"default \${domain} ".$row2['optiondata']."\"); //".$row2['optiondescr']."\n";
                                //	}
                                //}
                                //if ($row2['optiontype'] == "voicemail") {
                                //	if ($row2['optionprofile'] == "auto") {
                                //		$tmpaction .= "                 session.execute(\"voicemail\", \"default \${domain} ".$row2['optiondata']."\"); //".$row2['optiondescr']."\n";
                                //	}
                                //	else {
                                //		$tmpaction .= "                 session.execute(\"voicemail\", \"default \${domain} ".$row2['optiondata']."\"); //".$row2['optiondescr']."\n";
                                //	}
                                //}
                                //if ($row2['optiontype'] == "sip uri") {
                                //	$tmpaction .= "                 session.execute(\"bridge\", \"".$row2['optiondata']."\"); //".$row2['optiondescr']."\n";
                                //}
                                $tmpaction .= "             }\n";
                        }
                        $x++;
                    }
                    //end auto_attendant_id
                }
                //end while
                unset($prepstatement2);
                $tmp .= $tmpaction;
                if ($row['aadirectdial'] == "true") {
                    $tmp .= "             else {\n";
                    $tmp .= "\t                  session.execute(\"transfer\", dtmf.digits+\" XML default\");\n";
                    //$tmp .= $tmpactiondefault;
                    $tmp .= "             }\n";
                } else {
                    if ($actiondefault) {
                        $tmp .= "             else {\n";
                        $tmp .= "\t                  //console_log( \"info\", \"default option when there is no matching dtmf found\\n\" );\n";
                        //play the option recording if it exists
                        if (strlen($actiondefaultrecording) > 0) {
                            $option_recording_filename = get_recording_filename($actiondefaultrecording);
                            $tmp .= "\t                  session.streamFile( \"" . $v_recordings_dir . "/" . $option_recording_filename . "\" );\n";
                        }
                        $tmp .= "\t                  session.execute(\"" . $actiondefaulttype . "\", \"" . $actiondefaultdest . "\"); //" . $actiondefaultdesc . "\n";
                        //if ($actiondefaulttype == "extension") {
                        //	$tmp .= "	                  sofia_contact_".$actiondefaultdest." = get_sofia_contact(\"".$actiondefaultdest."\",domain_name, \"".$actiondefaultprofile."\");\n";
                        //	$tmp .= "	                  session.execute(\"bridge\", sofia_contact_".$actiondefaultdest."); //".$actiondefaultdest."\n";
                        //	if ($actiondirectprofile == "auto") {
                        //		$tmp .= "	                  session.execute(\"voicemail\", \"default \${domain} ".$actiondefaultdest."\");\n";
                        //	}
                        //	else {
                        //		$tmp .= "	                  session.execute(\"voicemail\", \"default \${domain} ".$actiondefaultdest."\");\n";
                        //	}
                        //}
                        //if ($actiondefaulttype == "voicemail") {
                        //	if ($actiondirectprofile == "auto") {
                        //		$tmp .= "	                  session.execute(\"voicemail\", \"default \${domain} ".$actiondefaultdest."\");\n";
                        //	}
                        //	else {
                        //		$tmp .= "	                  session.execute(\"voicemail\", \"default \${domain} ".$actiondefaultdest."\");\n";
                        //	}
                        //}
                        //if ($actiondefaulttype == "sip uri") {
                        //	$tmp .= "	                  session.execute(\"bridge\", \"".$actiondefaultdest."\"); //".$actiondefaultdest."\n";
                        //}
                        $tmp .= "             }\n";
                    }
                }
                $tmp .= "\n";
                unset($tmpaction);
                $tmp .= "          } \n";
                //$tmp .= "          else if ( dtmf.digits.length == \"4\" ) {\n";
                //$tmp .= "	                  //Transfer to the extension the caller\n";
                //$tmp .= "	                  session.execute(\"transfer\", dtmf.digits+\" XML default\");\n";
                //$tmp .= "          } else {\n";
                //$tmp .= $tmpactiondefault;
                //$tmp .= "          }\n";
                $tmp .= "\n";
                $tmp .= "     } //end if session.ready\n";
                $tmp .= "\n";
                $tmp .= " }\n";
                //end if condition
            }
            //if ($actiondirect)
        }
        //actioncount
        $antiactiondirect = false;
        $antiactiondefault = false;
        $antiactioncount = 0;
        $sql = "";
        $sql .= "select * from v_auto_attendant_options ";
        $sql .= "where auto_attendant_id = '" . $row['auto_attendant_id'] . "' ";
        $sql .= "and v_id = '{$v_id}' ";
        $prepstatement2 = $db->prepare($sql);
        $prepstatement2->execute();
        while ($row2 = $prepstatement2->fetch()) {
            //$auto_attendant_id = $row2["auto_attendant_id"];
            //$optionaction = $row2["optionaction"];
            //$optionnumber = $row2["optionnumber"];
            //$optiontype = $row2["optiontype"];
            //$optionprofile = $row2["optionprofile"];
            //$optiondata = $row2["optiondata"];
            //$optiondescr = $row2["optiondescr"];
            if ($row2['optionaction'] == "anti-action") {
                $antiactioncount++;
                if (strtolower($row2['optionnumber']) == "n") {
                    //direct the call now don't wait for dtmf
                    $antiactiondirect = true;
                    $antiactiondirecttype = $row2['optiontype'];
                    $antiactiondirectdest = $row2['optiondata'];
                    $antiactiondirectdesc = $row2['optiondesc'];
                    $antiactiondirectprofile = $row2['optionprofile'];
                }
                if (strtolower($row2['optionnumber']) == "d") {
                    //default option used when an dtmf doesn't match any option
                    $antiactiondefault = true;
                    $antiactiondefaulttype = $row2['optiontype'];
                    $antiactiondefaultdest = $row2['optiondata'];
                    $antiactiondefaultdesc = $row2['optiondesc'];
                    $antiactiondefaultrecording = $row2['optionrecording'];
                    $antiactiondefaultprofile = $row2['optionprofile'];
                }
            }
        }
        //end while
        unset($prepstatement2);
        //$tmp .= "anti-action count: ".$antiactioncount."<br />\n";
        if ($antiactioncount > 0) {
            if ($antiactiondirect) {
                $tmp .= " else {\n";
                $tmp .= "     //console_log( \"info\", \"anti-action call now don't wait for dtmf\\n\" );\n";
                $tmp .= "     session.execute(\"" . $antiactiondirecttype . "\", \"" . $antiactiondirectdest . "\"); //" . $antiactiondefaultdesc . "\n";
                //if ($antiactiondirecttype == "extension") {
                //	$tmp .= "    sofia_contact_".$antiactiondirectdest." = get_sofia_contact(\"".$antiactiondirectdest."\",domain_name, \"".$antiactiondirectprofile."\");\n";
                //	$tmp .= "    session.execute(\"bridge\", sofia_contact_".$antiactiondirectdest."); //".$antiactiondirectdest."\n";
                //	if ($antiactiondirectprofile == "auto") {
                //		$tmp .= "    session.execute(\"voicemail\", \"default \${domain} ".$antiactiondirectdest."\");\n";
                //	}
                //	else {
                //		$tmp .= "    session.execute(\"voicemail\", \"default \${domain} ".$antiactiondirectdest."\");\n";
                //	}
                //}
                //if ($antiactiondirecttype == "voicemail") {
                //	if ($antiactiondirectprofile == "auto") {
                //		$tmp .= "    session.execute(\"voicemail\", \"default \${domain} ".$antiactiondirectdest."\");\n";
                //	}
                //	else {
                //		$tmp .= "    session.execute(\"voicemail\", \"default \${domain} ".$antiactiondirectdest."\");\n";
                //	}
                //}
                //if ($antiactiondirecttype == "sip uri") {
                //	$tmp .= "    session.execute(\"bridge\", \"".$antiactiondirectdest."\"); //".$antiactiondirectdest."\n";
                //}
                $tmp .= "}\n";
            } else {
                $tmp .= " else {\n";
                $tmp .= "     //anti-action\n";
                $tmp .= "     //console_log( \"info\", \"anti-action options\\n\" );\n";
                $tmp .= "\n";
                $tmp .= "     var dtmf = new Object( );\n";
                $tmp .= "     dtmf.digits = \"\";\n";
                $tmp .= "     if ( session.ready( ) ) {\n";
                $tmp .= "         session.answer( );\n";
                $tmp .= "\n";
                $tmp .= "         digitmaxlength = 1;\n";
                $tmp .= "         while (session.ready() && ! exit ) {\n";
                $tmp .= "           session.streamFile( \"" . $v_recordings_dir . "/" . $recording_antiaction_filename . "\", mycb, \"dtmf " . $row['aatimeout'] . "\" );\n";
                $tmp .= "           if (session.ready()) {\n";
                $tmp .= "           \tif (dtmf.digits.length == 0) {\n";
                $tmp .= "           \t\tdtmf.digits +=  session.getDigits(1, \"#\", " . $row['aatimeout'] * 1000 . "); // " . $row['aatimeout'] . " seconds\n";
                $tmp .= "           \t\tif (dtmf.digits.length == 0) {\n";
                //$tmp .= "           			console_log( "info", "time out option: " + dtmf.digits + "\n" );\n";
                //find the timeout auto attendant options with the correct action
                $sql = "";
                $sql .= "select * from v_auto_attendant_options ";
                $sql .= "where auto_attendant_id = '" . $row['auto_attendant_id'] . "' ";
                $sql .= "and v_id = '{$v_id}' ";
                $prepstatement2 = $db->prepare($sql);
                $prepstatement2->execute();
                while ($row2 = $prepstatement2->fetch()) {
                    $auto_attendant_id = $row2["auto_attendant_id"];
                    $optionaction = $row2["optionaction"];
                    $optionnumber = $row2["optionnumber"];
                    $optiontype = $row2["optiontype"];
                    $optionprofile = $row2["optionprofile"];
                    $optiondata = $row2["optiondata"];
                    $optiondescr = $row2["optiondescr"];
                    if ($row2['optionaction'] == "anti-action") {
                        if (strtolower($row2['optionnumber']) == "t") {
                            //play the option recording if it exists
                            if (strlen($row2['optionrecording']) > 0) {
                                $option_recording_filename = get_recording_filename($row2['optionrecording']);
                                $tmp .= "                 \tsession.streamFile( \"" . $v_recordings_dir . "/" . $option_recording_filename . "\" );\n";
                            }
                            $tmp .= "                 \tsession.execute(\"" . $row2['optiontype'] . "\", \"" . $row2['optiondata'] . "\"); //" . $row2['optiondescr'] . "\n";
                            //if ($row2['optiontype'] == "extension") {
                            //	$tmp .= "                 	sofia_contact_".$row2['optiondata']." = get_sofia_contact(\"".$row2['optiondata']."\",domain_name, \"".$row2['optionprofile']."\");\n";
                            //	$tmp .= "                 	session.execute(\"bridge\", sofia_contact_".$row2['optiondata']."); //".$row2['optiondescr']."\n";
                            //	if ($row2['optionprofile'] == "auto") {
                            //		$tmp .= "                 	session.execute(\"voicemail\", \"default \${domain} ".$row2['optiondata']."\");\n";
                            //	}
                            //	else {
                            //		$tmp .= "                 	session.execute(\"voicemail\", \"default \${domain} ".$row2['optiondata']."\");\n";
                            //	}
                            //}
                            //if ($row2['optiontype'] == "voicemail") {
                            //	if ($row2['optionprofile'] == "auto") {
                            //		$tmp .= "                 	session.execute(\"voicemail\", \"default \${domain} ".$row2['optiondata']."\"); //".$row2['optiondescr']."\n";
                            //	}
                            //	else {
                            //		$tmp .= "                 	session.execute(\"voicemail\", \"default \${domain} ".$row2['optiondata']."\"); //".$row2['optiondescr']."\n";
                            //	}
                            //}
                            //if ($row2['optiontype'] == "sip uri") {
                            //	$tmp .= "                 	session.execute(\"bridge\", \"".$row2['optiondata']."\"); //".$row2['optiondescr']."\n";
                            //}
                        }
                    }
                    //end anti-action
                }
                //end while
                unset($prepstatement2);
                $tmp .= "           \t\t}\n";
                $tmp .= "           \t\telse {\n";
                $tmp .= "           \t\t\tbreak; //dtmf found end the while loop\n";
                $tmp .= "           \t\t}\n";
                $tmp .= "           \t}\n";
                $tmp .= "           }\n";
                $tmp .= "         }\n";
                $tmp .= "\n";
                $tmp .= "         //pickup the remaining digits\n";
                $tmp .= "         //http://wiki.freeswitch.org/wiki/Session_getDigits\n";
                $tmp .= "         //getDigits(length, terminators, timeout, digit_timeout, abs_timeout)\n";
                $tmp .= "         dtmf.digits +=  session.getDigits(4, \"#\", 3000);\n";
                $tmp .= "\n";
                $tmp .= "         console_log( \"info\", \"Auto Attendant Digit Pressed: \" + dtmf.digits + \"\\n\" );\n";
                $tmp .= "\n";
                $tmpantiaction = "";
                $tmp .= "         if ( dtmf.digits.length > \"0\" ) {\n\n";
                $x = 0;
                $sql = "";
                $sql .= "select * from v_auto_attendant_options ";
                $sql .= "where auto_attendant_id = '" . $row['auto_attendant_id'] . "' ";
                $sql .= "and v_id = '{$v_id}' ";
                $prepstatement2 = $db->prepare($sql);
                $prepstatement2->execute();
                while ($row2 = $prepstatement2->fetch()) {
                    $auto_attendant_id = $row2["auto_attendant_id"];
                    $optionaction = $row2["optionaction"];
                    $optionnumber = $row2["optionnumber"];
                    $optiontype = $row2["optiontype"];
                    $optionprofile = $row2["optionprofile"];
                    $optiondata = $row2["optiondata"];
                    $optiondescr = $row2["optiondescr"];
                    //find the correct auto attendant options with the correct action
                    if ($row2['optionaction'] == "anti-action") {
                        switch ($row2['optionnumber']) {
                            //case "t":
                            //		//break;
                            //case "d":
                            //		//break;
                            default:
                                //$tmpantiaction .= "             //console_log( \"info\", \"Auto Attendant Detected 1 digit \\n\" );\n";
                                if ($x == 0) {
                                    $tmpantiaction .= "             if ( dtmf.digits == \"" . $row2['optionnumber'] . "\" ) { //" . $row2['optiondescr'] . "\n";
                                } else {
                                    $tmpantiaction .= "             else if ( dtmf.digits == \"" . $row2['optionnumber'] . "\" ) { //" . $row2['optiondescr'] . "\n";
                                }
                                //play the option recording if it was provided
                                if (strlen($row2['optionrecording']) > 0) {
                                    $option_recording_filename = get_recording_filename($row2['optionrecording']);
                                    $tmpantiaction .= "             session.streamFile( \"" . $v_recordings_dir . "/" . $option_recording_filename . "\" );\n\n";
                                }
                                $tmpantiaction .= "                 session.execute(\"" . $row2['optiontype'] . "\", \"" . $row2['optiondata'] . "\"); //" . $row2['optiondescr'] . "\n";
                                //if ($row2['optiontype'] == "extension") {
                                //	$tmpantiaction .= "                 sofia_contact_".$row2['optiondata']." = get_sofia_contact(\"".$row2['optiondata']."\",domain_name, \"".$row2['optionprofile']."\");\n";
                                //	$tmpantiaction .= "                 session.execute(\"bridge\", sofia_contact_".$row2['optiondata']."); //".$row2['optiondescr']."\n";
                                //	if ($row2['optionprofile'] == "auto") {
                                //		$tmpantiaction .= "                 session.execute(\"voicemail\", \"default \${domain} ".$row2['optiondata']."\");\n";
                                //	}
                                //	else {
                                //		$tmpantiaction .= "                 session.execute(\"voicemail\", \"default \${domain} ".$row2['optiondata']."\");\n";
                                //	}
                                //}
                                //if ($row2['optiontype'] == "voicemail") {
                                //	if ($row2['optionprofile'] == "auto") {
                                //		$tmpantiaction .= "                 session.execute(\"voicemail\", \"default \${domain} ".$row2['optiondata']."\"); //".$row2['optiondescr']."\n";
                                //	}
                                //	else {
                                //		$tmpantiaction .= "                 session.execute(\"voicemail\", \"default \${domain} ".$row2['optiondata']."\"); //".$row2['optiondescr']."\n";
                                //	}
                                //}
                                //if ($row2['optiontype'] == "sip uri") {
                                //	$tmpantiaction .= "                 session.execute(\"bridge\", \"".$row2['optiondata']."\"); //".$row2['optiondescr']."\n";
                                //}
                                $tmpantiaction .= "             }\n";
                        }
                        //end switch
                        $x++;
                    }
                    //end anti-action
                }
                //end while
                unset($prepstatement2);
                $tmp .= $tmpantiaction;
                if ($row['aadirectdial'] == "true") {
                    $tmp .= "             else {\n";
                    $tmp .= "\t                  session.execute(\"transfer\", dtmf.digits+\" XML default\");\n";
                    //$tmp .= $tmpantiactiondefault;
                    $tmp .= "             }\n";
                } else {
                    if ($antiactiondefault) {
                        $tmp .= "             else {\n";
                        $tmp .= "\t                  //console_log( \"info\", \"default option used when dtmf doesn't match any other option\\n\" );\n";
                        //play the option recording if it exists
                        if (strlen($antiactiondefaultrecording) > 0) {
                            $option_recording_filename = get_recording_filename($antiactiondefaultrecording);
                            $tmp .= "\t                  session.streamFile( \"" . $v_recordings_dir . "/" . $option_recording_filename . "\" );\n";
                        }
                        $tmp .= "\t                  session.execute(\"" . $antiactiondefaulttype . "\", \"" . $antiactiondefaultdest . "\"); //" . $antiactiondefaultdesc . "\n";
                        //if ($antiactiondefaulttype == "extension") {
                        //	$tmp .= "	                  sofia_contact_".$antiactiondefaultdest." = get_sofia_contact(\"".$antiactiondefaultdest."\",domain_name, \"".$actiondirectprofile."\");\n";
                        //	$tmp .= "	                  session.execute(\"bridge\", sofia_contact_".$antiactiondefaultdest."); //".$antiactiondefaultdest."\n";
                        //	if ($actiondirectprofile == "auto") {
                        //		$tmp .= "	                  session.execute(\"voicemail\", \"default \${domain} ".$antiactiondefaultdest."\");\n";
                        //	}
                        //	else {
                        //		$tmp .= "	                  session.execute(\"voicemail\", \"default \${domain} ".$antiactiondefaultdest."\");\n";
                        //	}
                        //}
                        //if ($antiactiondefaulttype == "voicemail") {
                        //	if ($actiondirectprofile == "auto") {
                        //		$tmp .= "	                  session.execute(\"voicemail\", \"default \${domain} ".$antiactiondefaultdest."\");\n";
                        //	}
                        //	else {
                        //		$tmp .= "	                  session.execute(\"voicemail\", \"default \${domain} ".$antiactiondefaultdest."\");\n";
                        //	}
                        //}
                        //if ($antiactiondefaulttype == "sip uri") {
                        //	$tmp .= "	                  session.execute(\"bridge\", \"".$antiactiondefaultdest."\"); //".$antiactiondefaultdest."\n";
                        //}
                        $tmp .= "             }\n";
                    }
                }
                $tmp .= "\n";
                unset($tmpantiaction);
                $tmp .= "          } \n";
                //$tmp .= "          else if ( dtmf.digits.length == \"3\" ) {\n";
                //$tmp .= "                //Transfer to the extension the caller chose\n";
                //$tmp .= "                session.execute(\"transfer\", dtmf.digits+\" XML default\"); \n";
                //$tmp .= "          }\n";
                //$tmp .= "          else {\n";
                //$tmp .= $tmpantiactiondefault;
                //$tmp .= "          }\n";
                $tmp .= "\n";
                $tmp .= "     } //end if session.ready\n";
                $tmp .= "\n";
                $tmp .= " } //end if condition";
            }
            //if ($antiactiondirect)
        }
        //antiactioncount
        unset($tmpactiondefault);
        unset($tmpantiactiondefault);
        if (strlen($row['aaextension']) > 0) {
            $aafilename = "autoattendant_" . $row['aaextension'] . ".js";
            $fout = fopen($v_scripts_dir . "/" . $aafilename, "w");
            fwrite($fout, $tmp);
            unset($aafilename);
            fclose($fout);
        }
    }
    //end while
    $db->commit();
}