]>
git.defcon.no Git - hermes/blob - api/lib/number_functions.php
3 # Copyright (c) 2012, Gjøvik University College
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.
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.
28 require_once('config.php');
29 require_once('lib/common_functions.php');
30 require_once('lib/db_functions.php');
32 function verify_e164 ( $input )
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...
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 ) )
47 if ( preg_match( '/\+/', $number ))
50 if ( !is_numeric( $number ) )
57 function number_inpool( $number )
60 if (! verify_e164( $number ) )
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;
71 if ( $num_r == 1 ) return true;
76 function get_random_numbers ( $limit = 0 )
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 );
84 if ( !$result ) return null;
85 if ( @mysql_num_rows
($result) < 1 ) return null;
87 while ( $row = mysql_fetch_assoc( $result ) )
89 array_push( $rows, $row['number'] );
95 function get_numbers ( $search=null, $limit = 0 )
98 $query = "SELECT number FROM " . $config['numbers_table'];
101 $query .= sprintf(" WHERE number LIKE '%%%s%%'", sql_clean($search));
103 $query .= " ORDER BY number ASC";
105 if ( $limit && is_numeric( $limit ) )
106 $query .= " LIMIT " . $limit;
109 $result = sql_dbquery( $config['hermes_db'], $query );
111 if ( !$result ) return null;
112 if ( @mysql_num_rows
($result) < 1 ) return null;
114 while ( $row = mysql_fetch_assoc( $result ) )
116 array_push( $rows, $row['number'] );
121 function add_range( $start, $end )
124 $start_numeric = null;
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.";
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";
138 return "End of range is not a valid e164 number";
140 // Significant, a simple sanity check.
141 if ( ! ($start < $end) )
142 return "Start of range is after end of range";
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";
148 for ( $num = $start_numeric; $num <= $end_numeric; $num++
)
150 add_number( '+' . $num );
155 function add_number( $number )
158 if (! verify_e164( $number ) )
163 if (number_inpool( $number ) ) return false;
165 $insert = "INSERT INTO " . $config['numbers_table'] . " (number) VALUES ('" . $number . "')";
166 return sql_dbexec( $config['hermes_db'], $insert);
169 function remove_number ( $number )
172 if (! verify_e164( $number ) )
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);