]> git.defcon.no Git - hermes/blob - api/lib/number_functions.php
Added license text
[hermes] / api / lib / number_functions.php
1 <?php
2 /*
3 # Copyright (c) 2012, Gjøvik University College
4 # All rights reserved.
5
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above copyright
11 # notice, this list of conditions and the following disclaimer in the
12 # documentation and/or other materials provided with the distribution.
13 # * Neither the name of the Gjøvik University College nor the
14 # names of its contributors may be used to endorse or promote products
15 # derived from this software without specific prior written permission.
16 #
17 # THIS SOFTWARE IS PROVIDED BY Gjøvik University College ''AS IS'' AND ANY
18 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 # DISCLAIMED. IN NO EVENT SHALL Gjøvik University College BE LIABLE FOR ANY
21 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 require_once('config.php');
29 require_once('lib/common_functions.php');
30 require_once('lib/db_functions.php');
31
32 function verify_e164 ( $input )
33 {
34 // A really paranoid E164 test. Starts out with a regexp,
35 // where $arr[1] will be set to the numeric part of the
36 // e164 content, if valid. The rest is paranoid behaviour,
37 // if the regexp matches, the other tests can not fail...
38
39 // e164 format is: A plus (+) followed by at least four
40 // digits, and no more than 15 digits total.
41 if (! preg_match ( '/^\+(\d{4,15})$/', $input, $arr ) )
42 return 0;
43 $number = $arr[1];
44 if ( ! $number )
45 return 0;
46
47 if ( preg_match( '/\+/', $number ))
48 return 0;
49
50 if ( !is_numeric( $number ) )
51 return 0;
52
53 return $number;
54
55 }
56
57 function number_inpool( $number )
58 {
59 global $config;
60 if (! verify_e164( $number ) )
61 {
62 return false;
63 }
64
65 $test = "SELECT COUNT(*) FROM " . $config['numbers_table'] . " WHERE number = '" . $number . "'";
66 $result = sql_dbquery($config['hermes_db'], $test);
67 if ( !$result ) return false;
68 $row = @mysql_fetch_row($result);
69 if ( !$row ) return false;
70 $num_r = $row[0];
71 if ( $num_r == 1 ) return true;
72
73 return false;
74 }
75
76 function get_random_numbers ( $limit = 0 )
77 {
78 global $config;
79 $query = "SELECT number FROM " . $config['numbers_table'] . " ORDER BY RAND()";
80 if ( $limit && is_numeric( $limit ) )
81 $query .= " LIMIT " . $limit;
82 $result = sql_dbquery( $config['hermes_db'], $query );
83
84 if ( !$result ) return null;
85 if ( @mysql_num_rows($result) < 1 ) return null;
86 $rows = array();
87 while ( $row = mysql_fetch_assoc( $result ) )
88 {
89 array_push( $rows, $row['number'] );
90 }
91 return $rows;
92
93 }
94
95 function get_numbers ( $search=null, $limit = 0 )
96 {
97 global $config;
98 $query = "SELECT number FROM " . $config['numbers_table'];
99
100 if ( $search )
101 $query .= sprintf(" WHERE number LIKE '%%%s%%'", sql_clean($search));
102
103 $query .= " ORDER BY number ASC";
104
105 if ( $limit && is_numeric( $limit ) )
106 $query .= " LIMIT " . $limit;
107
108
109 $result = sql_dbquery( $config['hermes_db'], $query );
110
111 if ( !$result ) return null;
112 if ( @mysql_num_rows($result) < 1 ) return null;
113 $rows = array();
114 while ( $row = mysql_fetch_assoc( $result ) )
115 {
116 array_push( $rows, $row['number'] );
117 }
118 return $rows;
119 }
120
121 function add_range( $start, $end )
122 {
123 global $config;
124 $start_numeric = null;
125 $end_numeric = null;
126
127 // The start and end must be in the same e164 range. Because
128 // of this, they must hav identical length
129 if ( strlen( $start ) != strlen( $end ))
130 return "Start and end have different lengths.";
131
132 // The parameters MUST be in e164 format.
133 $start_numeric = verify_e164( $start );
134 $end_numeric = verify_e164( $end );
135 if (! $start_numeric )
136 return "Start of range is not a valid e164 number";
137 if (! $end_numeric )
138 return "End of range is not a valid e164 number";
139
140 // Significant, a simple sanity check.
141 if ( ! ($start < $end) )
142 return "Start of range is after end of range";
143
144 // Hard-coded paranoia: We expect ranges to be less than 10k numbers..
145 if ( ($end - $start ) > 9999 )
146 return "Range is larger than hard limit permits";
147
148 for ( $num = $start_numeric; $num <= $end_numeric; $num++ )
149 {
150 add_number( '+' . $num );
151 }
152 return 'ok';
153 }
154
155 function add_number( $number )
156 {
157 global $config;
158 if (! verify_e164( $number ) )
159 {
160 return false;
161 }
162
163 if (number_inpool( $number ) ) return false;
164
165 $insert = "INSERT INTO " . $config['numbers_table'] . " (number) VALUES ('" . $number . "')";
166 return sql_dbexec( $config['hermes_db'], $insert);
167 }
168
169 function remove_number ( $number )
170 {
171 global $config;
172 if (! verify_e164( $number ) )
173 {
174 return false;
175 }
176 if ( !number_inpool( $number ) ) return false;
177 $query = "DELETE FROM " . $config['numbers_table'] . " WHERE number = '" . $number . "'";
178 return sql_dbexec( $config['hermes_db'], $query);
179
180 }
181 ?>