]>
git.defcon.no Git - hermes/blob - api/numbers.php
2 require_once('config.php');
3 require_once('lib/auth_base.php');
4 require_once('lib/common_functions.php');
5 require_once('lib/db_functions.php');
6 require_once('lib/number_functions.php');
8 $config = get_config();
10 $config['sql_link'] = @mysql_connect
(
11 $config['sql_server'],
12 $config['sql_username'],
13 $config['sql_password']
15 if ( !$config['sql_link'] )
17 print json_encode( array( 'response' => 'failed', 'cause' => 'error', 'detail' => 'Database connection failed.'));
23 //*************************************************************************************
24 switch ( $_SERVER['PATH_INFO'] )
27 // List all (distinct) phone MAC-adresses registered...
31 if ( array_key_exists('limit', $_GET ) && is_numeric( $_GET['limit']))
32 $limit = $_GET['limit'];
34 if ( array_key_exists('search', $_GET ) )
35 $search = $_GET['search'];
36 else if ( array_key_exists('random', $_GET ) && (strtolower( $_GET['random'] ) === 'true'))
43 $numbers = get_random_numbers( $limit );
47 $numbers = get_numbers ( $search, $limit );
51 print json_encode( array( 'response' => 'ok', 'list' => $numbers ));
55 print json_encode( array( 'response' => 'failed', 'cause' => 'empty', 'detail' => 'Empty result.' ));
58 if ( array_key_exists('start', $_GET) && array_key_exists('end', $_GET) )
60 $start = $_GET['start'];
61 $end = sql_clean( $_GET['end'] );
62 $result = add_range( $start, $end );
63 if ( $result === 'ok' )
65 print json_encode ( array( 'response' => 'ok') );
69 print json_encode ( array( 'response' => 'failed', 'cause' => 'rejected', 'detail' => $result ) );
74 // TODO: This should return better responses!
75 // Currently, it will fail with "invalid"
76 if ( array_key_exists('number', $_GET))
78 $number = $_GET['number'];
80 if (! verify_e164( $number ) )
82 print json_encode ( array( 'response' => 'failed', 'cause' => 'rejected', 'detail' => "Not a valid e164 number" ));
85 if ( number_inpool( $number ) )
87 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => "Number is already in the pool" ));
91 $result = add_number( $number );
94 print json_encode ( array( 'response' => 'ok', 'detail' => 'Added ' . $number, 'number' => $number ) );
99 print json_encode ( array( 'response' => 'failed', 'cause' => 'rejected' ));
102 print json_encode ( array( 'response' => 'invalid') );
105 if ( array_key_exists('number', $_GET))
107 $number = $_GET['number'];
109 if (! verify_e164( $number ) )
111 print json_encode ( array( 'response' => 'failed', 'cause' => 'rejected', 'detail' => "Not a valid e164 number" ));
114 if ( !number_inpool( $number ) )
116 print json_encode ( array( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => "Number not in pool" ));
119 if ( !remove_number ( $number ) )
121 print json_encode ( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => "Failed to remove number" ));
124 print json_encode ( array( 'response' => 'ok', 'detail' => 'Removed ' . $number, 'number' => $number ) );
127 print json_encode ( array( 'response' => 'invalid') );
130 if ( array_key_exists('number', $_GET))
132 $number = $_GET['number'];
133 if ( number_inpool( $number ) )
134 print json_encode ( array( 'response' => 'ok', 'number' => $number ) );
136 print json_encode ( array( 'response' => 'failed', 'cause' => 'nonexistant') );
140 print json_encode ( array( 'response' => 'invalid') );
142 mysql_close( $config['sql_link'] );