]>
git.defcon.no Git - hermes/blob - api/numbers.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/auth_base.php');
30 require_once('lib/common_functions.php');
31 require_once('lib/db_functions.php');
32 require_once('lib/number_functions.php');
34 $config = get_config();
36 $config['sql_link'] = @mysql_connect
(
37 $config['sql_server'],
38 $config['sql_username'],
39 $config['sql_password']
41 if ( !$config['sql_link'] )
43 print json_encode( array( 'response' => 'failed', 'cause' => 'error', 'detail' => 'Database connection failed.'));
49 //*************************************************************************************
50 switch ( $_SERVER['PATH_INFO'] )
53 // List all (distinct) phone MAC-adresses registered...
57 if ( array_key_exists('limit', $_POST ) && is_numeric( $_POST['limit']))
58 $limit = $_POST['limit'];
60 if ( array_key_exists('search', $_POST ) )
61 $search = $_POST['search'];
62 else if ( array_key_exists('random', $_POST ) && (strtolower( $_POST['random'] ) === 'true'))
69 $numbers = get_random_numbers( $limit );
73 $numbers = get_numbers ( $search, $limit );
77 print json_encode( array( 'response' => 'ok', 'list' => $numbers ));
81 print json_encode( array( 'response' => 'failed', 'cause' => 'empty', 'detail' => 'Empty result.' ));
84 if ( array_key_exists('start', $_POST) && array_key_exists('end', $_POST) )
86 $start = $_POST['start'];
87 $end = sql_clean( $_POST['end'] );
88 $result = add_range( $start, $end );
89 if ( $result === 'ok' )
91 print json_encode ( array( 'response' => 'ok') );
95 print json_encode ( array( 'response' => 'failed', 'cause' => 'rejected', 'detail' => $result ) );
100 // TODO: This should return better responses!
101 // Currently, it will fail with "invalid"
102 if ( array_key_exists('number', $_POST))
104 $number = $_POST['number'];
106 if (! verify_e164( $number ) )
108 print json_encode ( array( 'response' => 'failed', 'cause' => 'rejected', 'detail' => "Not a valid e164 number" ));
111 if ( number_inpool( $number ) )
113 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => "Number is already in the pool" ));
117 $result = add_number( $number );
120 print json_encode ( array( 'response' => 'ok', 'detail' => 'Added ' . $number, 'number' => $number ) );
125 print json_encode ( array( 'response' => 'failed', 'cause' => 'rejected' ));
128 print json_encode ( array( 'response' => 'invalid') );
131 if ( array_key_exists('number', $_POST))
133 $number = $_POST['number'];
135 if (! verify_e164( $number ) )
137 print json_encode ( array( 'response' => 'failed', 'cause' => 'rejected', 'detail' => "Not a valid e164 number" ));
140 if ( !number_inpool( $number ) )
142 print json_encode ( array( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => "Number not in pool" ));
145 if ( !remove_number ( $number ) )
147 print json_encode ( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => "Failed to remove number" ));
150 print json_encode ( array( 'response' => 'ok', 'detail' => 'Removed ' . $number, 'number' => $number ) );
153 print json_encode ( array( 'response' => 'invalid') );
156 if ( array_key_exists('number', $_POST))
158 $number = $_POST['number'];
159 if ( number_inpool( $number ) )
160 print json_encode ( array( 'response' => 'ok', 'number' => $number ) );
162 print json_encode ( array( 'response' => 'failed', 'cause' => 'nonexistant') );
166 print json_encode ( array( 'response' => 'invalid') );
168 mysql_close( $config['sql_link'] );