All pastes #230404 Raw Edit

Juggie

public text v1 · immutable
#230404 ·published 2006-10-31 07:43 UTC
rendered paste body
<?PHP
/* #!/usr/bin/php -q */
include("config.php");
include("clsDB.php");

/**
 *
 *      Asterisk Least Call Routing PHP Script.
 *      This script is to be called from Asterisk using the CURL function.
 *      This script will return the technology/route to use for the selected
 *      call based on NPA+Nxx and the minium cost for this call. 
 *
 *      +-------------+---------------------+------+-----+---------+----------------+
 *      | Field       | Type                | Null | Key | Default | Extra          |
 *      +-------------+---------------------+------+-----+---------+----------------+
 *      | lcr_id      | bigint(20) unsigned | NO   | PRI | NULL    | auto_increment |
 *      | description | varchar(35)         | NO   |     | NULL    |                |
 *      | npa         | varchar(3)          | NO   | MUL | NULL    |                |
 *      | nxx         | varchar(3)          | NO   |     | NULL    |                |
 *      | cost_cents  | float(7,4)          | NO   |     | NULL    |                |
 *      | technology  | varchar(64)         | NO   |     | NULL    |                |
 *      | active      | tinyint(1)          | NO   |     | 1       |                |
 *      +-------------+---------------------+------+-----+---------+----------------+
 *
 *      @author         Timothy Lorens (icebrkr@cyberdyne.org)
 *      @version        v1.0 - 10/30/2006
 *
 */


/**
 *      Create DB connection using parameters found in config.php
 */
$conn = new clsDB($_CONFIG);


/**
 *
 *      chkData
 *
 *      Just to make sure inbound data is what we're looking for.
 *
 *      @param  interger $val Should be numeric and only 3 digits long
 *      @return bool Returns True if inbound data is correct, False if not.
 *
 */
function chkdata($val)
{
    if ( (strlen($val)==3)  && is_numeric($val) )
    {
        return TRUE;
    }
    else
    {
        return FALSE;
                }
} //END chkData()


/**
 *
 *      ##### MAIN #####
 *
 */

if ($argc==1)
{
    die("Usage: ".$argv[0]." [npa] [nxx]\n\n");  
}
elseif ($argc > 2)
{
                $npa = $argv[1];
    $nxx = $argv[2];
}
else
{
    $npa = $_REQUEST['n'];
    $nxx = $_REQUEST['x'];
    if ($npa=="" || $nxx=="")
    {
        header("HTTP/1.0 400 Bad Request");
        die();
    }
}

if (chkdata($npa) && chkdata($nxx))
{
    $query = "SELECT 
               technology
               FROM 
               lcr
               WHERE 
               npa = '$npa'
                                AND 
               nxx = '$nxx'
               AND 
               cost_cents = 
               (
                SELECT 
                min( cost_cents )
                FROM 
                lcr 
                ) 
                 AND 
               active=1";
    
    if ($num_rows = $conn->query($query))
    {
        $row = $conn->fetchRow();
        echo $row['technology'];
    }
    else
    {
        //** Didn't find valid route.  Fetch lowest cost default route.
        $query = "SELECT 
                   technology
                   FROM 
                   lcr
                   WHERE 
                   npa = '000'
                   AND 
                   nxx = '000'
                   AND 
                   cost_cents = 
                   (
                    SELECT 
                    min( cost_cents )
                    FROM 
                    lcr 
                    ) 
                     AND 
                   active=1";
        if ($num_rows = $conn->query($query))
        {
            $row = $conn->fetchRow();
            echo $row['technology'];
        }
        else
        {
            echo 'failed';
        }
    }                            
}
?>