]>
git.defcon.no Git - hermes/blob - api/lib/number_functions.php
2 require_once('config.php');
3 require_once('lib/common_functions.php');
4 require_once('lib/db_functions.php');
6 function verify_e164 ( $input )
8 // A really paranoid E164 test. Starts out with a regexp,
9 // where $arr[1] will be set to the numeric part of the
10 // e164 content, if valid. The rest is paranoid behaviour,
11 // if the regexp matches, the other tests can not fail...
13 // e164 format is: A plus (+) followed by at least four
14 // digits, and no more than 15 digits total.
15 if (! preg_match ( '/^\+(\d{4,15})$/', $input, $arr ) )
21 if ( preg_match( '/\+/', $number ))
24 if ( !is_numeric( $number ) )
31 function number_inpool( $number )
34 if (! verify_e164( $number ) )
39 $test = "SELECT COUNT(*) FROM " . $config['numbers_table'] . " WHERE number = '" . $number . "'";
40 $result = sql_dbquery($config['provision_db'], $test);
41 if ( !$result ) return false;
42 $row = mysql_fetch_row($result);
43 if ( !$row ) return false;
45 if ( $num_r == 1 ) return true;
50 function get_random_numbers ( $limit = 0 )
53 $query = "SELECT number FROM " . $config['numbers_table'] . " ORDER BY RAND()";
54 if ( $limit && is_numeric( $limit ) )
55 $query .= " LIMIT " . $limit;
56 $result = sql_dbquery( $config['provision_db'], $query );
58 if ( !$result ) return null;
59 if (mysql_num_rows($result) < 1 ) return null;
61 while ( $row = mysql_fetch_assoc( $result ) )
63 array_push( $rows, $row['number'] );
69 function get_numbers ( $search=null, $limit = 0 )
72 $query = "SELECT number FROM " . $config['numbers_table'];
75 $query .= sprintf(" WHERE number LIKE '%%%s%%'", sql_clean($search));
77 $query .= " ORDER BY number ASC";
79 if ( $limit && is_numeric( $limit ) )
80 $query .= " LIMIT " . $limit;
83 $result = sql_dbquery( $config['provision_db'], $query );
85 if ( !$result ) return null;
86 if (mysql_num_rows($result) < 1 ) return null;
88 while ( $row = mysql_fetch_assoc( $result ) )
90 array_push( $rows, $row['number'] );
95 function add_range( $start, $end )
98 $start_numeric = null;
101 // The start and end must be in the same e164 range. Because
102 // of this, they must hav identical length
103 if ( strlen( $start ) != strlen( $end ))
104 return "Start and end have different lengths.";
106 // The parameters MUST be in e164 format.
107 $start_numeric = verify_e164( $start );
108 $end_numeric = verify_e164( $end );
109 if (! $start_numeric )
110 return "Start of range is not a valid e164 number";
112 return "End of range is not a valid e164 number";
114 // Significant, a simple sanity check.
115 if ( ! ($start < $end) )
116 return "Start of range is after end of range";
118 // Hard-coded paranoia: We expect ranges to be less than 10k numbers..
119 if ( ($end - $start ) > 9999 )
120 return "Range is larger than hard limit permits";
122 for ( $num = $start_numeric; $num <= $end_numeric; $num++
)
124 add_number( '+' . $num );
129 function add_number( $number )
132 if (! verify_e164( $number ) )
137 if (number_inpool( $number ) ) return false;
139 $insert = "INSERT INTO " . $config['numbers_table'] . " (number) VALUES ('" . $number . "')";
140 return sql_dbexec( $config['provision_db'], $insert);
143 function remove_number ( $number )
146 if (! verify_e164( $number ) )
150 if ( !number_inpool( $number ) ) return false;
151 $query = "DELETE FROM " . $config['numbers_table'] . " WHERE number = '" . $number . "'";
152 return sql_dbexec( $config['provision_db'], $query);