]> git.defcon.no Git - hermes/blob - api/lib/number_functions.php
Moved API-files to a separate API directory.
[hermes] / api / lib / number_functions.php
1 <?php
2 require_once('config.php');
3 require_once('lib/common_functions.php');
4 require_once('lib/db_functions.php');
5
6 function verify_e164 ( $input )
7 {
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...
12
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 ) )
16 return 0;
17 $number = $arr[1];
18 if ( ! $number )
19 return 0;
20
21 if ( preg_match( '/\+/', $number ))
22 return 0;
23
24 if ( !is_numeric( $number ) )
25 return 0;
26
27 return $number;
28
29 }
30
31 function number_inpool( $number )
32 {
33 global $config;
34 if (! verify_e164( $number ) )
35 {
36 return false;
37 }
38
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;
44 $num_r = $row[0];
45 if ( $num_r == 1 ) return true;
46
47 return false;
48 }
49
50 function get_random_numbers ( $limit = 0 )
51 {
52 global $config;
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 );
57 print mysql_error();
58 if ( !$result ) return null;
59 if (mysql_num_rows($result) < 1 ) return null;
60 $rows = array();
61 while ( $row = mysql_fetch_assoc( $result ) )
62 {
63 array_push( $rows, $row['number'] );
64 }
65 return $rows;
66
67 }
68
69 function get_numbers ( $search=null, $limit = 0 )
70 {
71 global $config;
72 $query = "SELECT number FROM " . $config['numbers_table'];
73
74 if ( $search )
75 $query .= sprintf(" WHERE number LIKE '%%%s%%'", sql_clean($search));
76
77 $query .= " ORDER BY number ASC";
78
79 if ( $limit && is_numeric( $limit ) )
80 $query .= " LIMIT " . $limit;
81
82
83 $result = sql_dbquery( $config['provision_db'], $query );
84 print mysql_error();
85 if ( !$result ) return null;
86 if (mysql_num_rows($result) < 1 ) return null;
87 $rows = array();
88 while ( $row = mysql_fetch_assoc( $result ) )
89 {
90 array_push( $rows, $row['number'] );
91 }
92 return $rows;
93 }
94
95 function add_range( $start, $end )
96 {
97 global $config;
98 $start_numeric = null;
99 $end_numeric = null;
100
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.";
105
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";
111 if (! $end_numeric )
112 return "End of range is not a valid e164 number";
113
114 // Significant, a simple sanity check.
115 if ( ! ($start < $end) )
116 return "Start of range is after end of range";
117
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";
121
122 for ( $num = $start_numeric; $num <= $end_numeric; $num++ )
123 {
124 add_number( '+' . $num );
125 }
126 return 'ok';
127 }
128
129 function add_number( $number )
130 {
131 global $config;
132 if (! verify_e164( $number ) )
133 {
134 return false;
135 }
136
137 if (number_inpool( $number ) ) return false;
138
139 $insert = "INSERT INTO " . $config['numbers_table'] . " (number) VALUES ('" . $number . "')";
140 return sql_dbexec( $config['provision_db'], $insert);
141 }
142
143 function remove_number ( $number )
144 {
145 global $config;
146 if (! verify_e164( $number ) )
147 {
148 return false;
149 }
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);
153
154 }
155 ?>