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