]> git.defcon.no Git - hermes/blob - api/numbers.php
Starting to get parts of an auth-framework operational...
[hermes] / api / numbers.php
1 <?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');
7
8 $config = get_config();
9
10 $config['sql_link'] = @mysql_connect(
11 $config['sql_server'],
12 $config['sql_username'],
13 $config['sql_password']
14 );
15 if ( !$config['sql_link'] )
16 {
17 print json_encode( array( 'response' => 'failed', 'cause' => 'error', 'detail' => 'Database connection failed.'));
18 exit;
19 }
20
21 token_auth();
22
23 //*************************************************************************************
24 switch ( $_SERVER['PATH_INFO'] )
25 {
26 case "/list":
27 // List all (distinct) phone MAC-adresses registered...
28 $limit = 0;
29 $random = false;
30 $search = null;
31 if ( array_key_exists('limit', $_GET ) && is_numeric( $_GET['limit']))
32 $limit = $_GET['limit'];
33
34 if ( array_key_exists('search', $_GET ) )
35 $search = $_GET['search'];
36 else if ( array_key_exists('random', $_GET ) && (strtolower( $_GET['random'] ) === 'true'))
37 $random = true;
38
39 $numbers = null;
40
41 if ( $random )
42 {
43 $numbers = get_random_numbers( $limit );
44 }
45 else
46 {
47 $numbers = get_numbers ( $search, $limit );
48 }
49 if ( $numbers )
50 {
51 print json_encode( array( 'response' => 'ok', 'list' => $numbers ));
52 break;
53 }
54
55 print json_encode( array( 'response' => 'failed', 'cause' => 'empty', 'detail' => 'Empty result.' ));
56 break;
57 case "/add_range":
58 if ( array_key_exists('start', $_GET) && array_key_exists('end', $_GET) )
59 {
60 $start = $_GET['start'];
61 $end = sql_clean( $_GET['end'] );
62 $result = add_range( $start, $end );
63 if ( $result === 'ok' )
64 {
65 print json_encode ( array( 'response' => 'ok') );
66 }
67 else
68 {
69 print json_encode ( array( 'response' => 'failed', 'cause' => 'rejected', 'detail' => $result ) );
70 }
71 }
72 break;
73 case "/add":
74 // TODO: This should return better responses!
75 // Currently, it will fail with "invalid"
76 if ( array_key_exists('number', $_GET))
77 {
78 $number = $_GET['number'];
79
80 if (! verify_e164( $number ) )
81 {
82 print json_encode ( array( 'response' => 'failed', 'cause' => 'rejected', 'detail' => "Not a valid e164 number" ));
83 break;
84 }
85 if ( number_inpool( $number ) )
86 {
87 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => "Number is already in the pool" ));
88 break;
89 }
90
91 $result = add_number( $number );
92 if ( $result )
93 {
94 print json_encode ( array( 'response' => 'ok', 'detail' => 'Added ' . $number, 'number' => $number ) );
95 break;
96 }
97 else
98 {
99 print json_encode ( array( 'response' => 'failed', 'cause' => 'rejected' ));
100 }
101 }
102 print json_encode ( array( 'response' => 'invalid') );
103 break;
104 case "/remove":
105 if ( array_key_exists('number', $_GET))
106 {
107 $number = $_GET['number'];
108
109 if (! verify_e164( $number ) )
110 {
111 print json_encode ( array( 'response' => 'failed', 'cause' => 'rejected', 'detail' => "Not a valid e164 number" ));
112 break;
113 }
114 if ( !number_inpool( $number ) )
115 {
116 print json_encode ( array( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => "Number not in pool" ));
117 break;
118 }
119 if ( !remove_number ( $number ) )
120 {
121 print json_encode ( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => "Failed to remove number" ));
122 break;
123 }
124 print json_encode ( array( 'response' => 'ok', 'detail' => 'Removed ' . $number, 'number' => $number ) );
125 break;
126 }
127 print json_encode ( array( 'response' => 'invalid') );
128 break;
129 case "/inpool":
130 if ( array_key_exists('number', $_GET))
131 {
132 $number = $_GET['number'];
133 if ( number_inpool( $number ) )
134 print json_encode ( array( 'response' => 'ok', 'number' => $number ) );
135 else
136 print json_encode ( array( 'response' => 'failed', 'cause' => 'nonexistant') );
137 break;
138 }
139 default:
140 print json_encode ( array( 'response' => 'invalid') );
141 }
142 mysql_close( $config['sql_link'] );
143 ?>