-<?php
-require_once('config.php');
-require_once('lib/common_functions.php');
-require_once('lib/db_functions.php');
-
-function verify_e164 ( $input )
-{
- // A really paranoid E164 test. Starts out with a regexp,
- // where $arr[1] will be set to the numeric part of the
- // e164 content, if valid. The rest is paranoid behaviour,
- // if the regexp matches, the other tests can not fail...
-
- // e164 format is: A plus (+) followed by at least four
- // digits, and no more than 15 digits total.
- if (! preg_match ( '/^\+(\d{4,15})$/', $input, $arr ) )
- return 0;
- $number = $arr[1];
- if ( ! $number )
- return 0;
-
- if ( preg_match( '/\+/', $number ))
- return 0;
-
- if ( !is_numeric( $number ) )
- return 0;
-
- return $number;
-
-}
-
-function number_inpool( $number )
-{
- global $config;
- if (! verify_e164( $number ) )
- {
- return false;
- }
-
- $test = "SELECT COUNT(*) FROM " . $config['numbers_table'] . " WHERE number = '" . $number . "'";
- $result = sql_dbquery($config['provision_db'], $test);
- if ( !$result ) return false;
- $row = mysql_fetch_row($result);
- if ( !$row ) return false;
- $num_r = $row[0];
- if ( $num_r == 1 ) return true;
-
- return false;
-}
-
-function get_random_numbers ( $limit = 0 )
-{
- global $config;
- $query = "SELECT number FROM " . $config['numbers_table'] . " ORDER BY RAND()";
- if ( $limit && is_numeric( $limit ) )
- $query .= " LIMIT " . $limit;
- $result = sql_dbquery( $config['provision_db'], $query );
- print mysql_error();
- if ( !$result ) return null;
- if (mysql_num_rows($result) < 1 ) return null;
- $rows = array();
- while ( $row = mysql_fetch_assoc( $result ) )
- {
- array_push( $rows, $row['number'] );
- }
- return $rows;
-
-}
-
-function get_numbers ( $search=null, $limit = 0 )
-{
- global $config;
- $query = "SELECT number FROM " . $config['numbers_table'];
-
- if ( $search )
- $query .= sprintf(" WHERE number LIKE '%%%s%%'", sql_clean($search));
-
- $query .= " ORDER BY number ASC";
-
- if ( $limit && is_numeric( $limit ) )
- $query .= " LIMIT " . $limit;
-
-
- $result = sql_dbquery( $config['provision_db'], $query );
- print mysql_error();
- if ( !$result ) return null;
- if (mysql_num_rows($result) < 1 ) return null;
- $rows = array();
- while ( $row = mysql_fetch_assoc( $result ) )
- {
- array_push( $rows, $row['number'] );
- }
- return $rows;
-}
-
-function add_range( $start, $end )
-{
- global $config;
- $start_numeric = null;
- $end_numeric = null;
-
- // The start and end must be in the same e164 range. Because
- // of this, they must hav identical length
- if ( strlen( $start ) != strlen( $end ))
- return "Start and end have different lengths.";
-
- // The parameters MUST be in e164 format.
- $start_numeric = verify_e164( $start );
- $end_numeric = verify_e164( $end );
- if (! $start_numeric )
- return "Start of range is not a valid e164 number";
- if (! $end_numeric )
- return "End of range is not a valid e164 number";
-
- // Significant, a simple sanity check.
- if ( ! ($start < $end) )
- return "Start of range is after end of range";
-
- // Hard-coded paranoia: We expect ranges to be less than 10k numbers..
- if ( ($end - $start ) > 9999 )
- return "Range is larger than hard limit permits";
-
- for ( $num = $start_numeric; $num <= $end_numeric; $num++ )
- {
- add_number( '+' . $num );
- }
- return 'ok';
-}
-
-function add_number( $number )
-{
- global $config;
- if (! verify_e164( $number ) )
- {
- return false;
- }
-
- if (number_inpool( $number ) ) return false;
-
- $insert = "INSERT INTO " . $config['numbers_table'] . " (number) VALUES ('" . $number . "')";
- return sql_dbexec( $config['provision_db'], $insert);
-}
-
-function remove_number ( $number )
-{
- global $config;
- if (! verify_e164( $number ) )
- {
- return false;
- }
- if ( !number_inpool( $number ) ) return false;
- $query = "DELETE FROM " . $config['numbers_table'] . " WHERE number = '" . $number . "'";
- return sql_dbexec( $config['provision_db'], $query);
-
-}
-?>