]>
git.defcon.no Git - hermes/blob - numbers.php
2 require_once('config.php');
3 //require_once('lib/user_functions.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.'));
21 //*************************************************************************************
22 switch ( $_SERVER['PATH_INFO'] )
25 // List all (distinct) phone MAC-adresses registered...
29 if ( array_key_exists('limit', $_GET ) && is_numeric( $_GET['limit']))
30 $limit = $_GET['limit'];
32 if ( array_key_exists('search', $_GET ) )
33 $search = $_GET['search'];
34 else if ( array_key_exists('random', $_GET ) && (strtolower( $_GET['random'] ) === 'true'))
41 $numbers = get_random_numbers( $limit );
45 $numbers = get_numbers ( $search, $limit );
49 print json_encode( array( 'response' => 'ok', 'list' => $numbers ));
53 print json_encode( array( 'response' => 'failed', 'cause' => 'empty', 'detail' => 'Empty result.' ));
56 if ( array_key_exists('start', $_GET) && array_key_exists('end', $_GET) )
58 $start = $_GET['start'];
59 $end = sql_clean( $_GET['end'] );
60 $result = add_range( $start, $end );
61 if ( $result === 'ok' )
63 print json_encode ( array( 'response' => 'ok') );
67 print json_encode ( array( 'response' => 'failed', 'cause' => 'rejected', 'detail' => $result ) );
72 // TODO: This should return better responses!
73 // Currently, it will fail with "invalid"
74 if ( array_key_exists('number', $_GET))
76 $number = $_GET['number'];
78 if (! verify_e164( $number ) )
80 print json_encode ( array( 'response' => 'failed', 'cause' => 'rejected', 'detail' => "Not a valid e164 number" ));
83 if ( number_inpool( $number ) )
85 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => "Number is already in the pool" ));
89 $result = add_number( $number );
92 print json_encode ( array( 'response' => 'ok', 'detail' => 'Added ' . $number, 'number' => $number ) );
97 print json_encode ( array( 'response' => 'failed', 'cause' => 'rejected' ));
100 print json_encode ( array( 'response' => 'invalid') );
103 if ( array_key_exists('number', $_GET))
105 $number = $_GET['number'];
107 if (! verify_e164( $number ) )
109 print json_encode ( array( 'response' => 'failed', 'cause' => 'rejected', 'detail' => "Not a valid e164 number" ));
112 if ( !number_inpool( $number ) )
114 print json_encode ( array( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => "Number not in pool" ));
117 if ( !remove_number ( $number ) )
119 print json_encode ( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => "Failed to remove number" ));
122 print json_encode ( array( 'response' => 'ok', 'detail' => 'Removed ' . $number, 'number' => $number ) );
125 print json_encode ( array( 'response' => 'invalid') );
128 if ( array_key_exists('number', $_GET))
130 $number = $_GET['number'];
131 if ( number_inpool( $number ) )
132 print json_encode ( array( 'response' => 'ok', 'number' => $number ) );
134 print json_encode ( array( 'response' => 'failed', 'cause' => 'nonexistant') );
138 print json_encode ( array( 'response' => 'invalid') );
140 mysql_close( $config['sql_link'] );