]> git.defcon.no Git - hermes/blob - api/numbers.php
First stab at a naive permissions-tool
[hermes] / api / numbers.php
1 <?php
2 /*
3 # Copyright (c) 2012, Gjøvik University College
4 # All rights reserved.
5
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.
16 #
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.
27 */
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');
33
34 $config = get_config();
35
36 $config['sql_link'] = @mysql_connect(
37 $config['sql_server'],
38 $config['sql_username'],
39 $config['sql_password']
40 );
41 if ( !$config['sql_link'] )
42 {
43 print json_encode( array( 'response' => 'failed', 'cause' => 'error', 'detail' => 'Database connection failed.'));
44 exit;
45 }
46
47 token_auth();
48
49 //*************************************************************************************
50 switch ( $_SERVER['PATH_INFO'] )
51 {
52 case "/list":
53 // List all (distinct) phone MAC-adresses registered...
54 $limit = 0;
55 $random = false;
56 $search = null;
57 if ( array_key_exists('limit', $_POST ) && is_numeric( $_POST['limit']))
58 $limit = $_POST['limit'];
59
60 if ( array_key_exists('search', $_POST ) )
61 $search = $_POST['search'];
62 else if ( array_key_exists('random', $_POST ) && (strtolower( $_POST['random'] ) === 'true'))
63 $random = true;
64
65 $numbers = null;
66
67 if ( $random )
68 {
69 $numbers = get_random_numbers( $limit );
70 }
71 else
72 {
73 $numbers = get_numbers ( $search, $limit );
74 }
75 if ( $numbers )
76 {
77 print json_encode( array( 'response' => 'ok', 'list' => $numbers ));
78 break;
79 }
80
81 print json_encode( array( 'response' => 'failed', 'cause' => 'empty', 'detail' => 'Empty result.' ));
82 break;
83 case "/add_range":
84 if ( array_key_exists('start', $_POST) && array_key_exists('end', $_POST) )
85 {
86 $start = $_POST['start'];
87 $end = sql_clean( $_POST['end'] );
88 $result = add_range( $start, $end );
89 if ( $result === 'ok' )
90 {
91 print json_encode ( array( 'response' => 'ok') );
92 }
93 else
94 {
95 print json_encode ( array( 'response' => 'failed', 'cause' => 'rejected', 'detail' => $result ) );
96 }
97 }
98 break;
99 case "/add":
100 // TODO: This should return better responses!
101 // Currently, it will fail with "invalid"
102 if ( array_key_exists('number', $_POST))
103 {
104 $number = $_POST['number'];
105
106 if (! verify_e164( $number ) )
107 {
108 print json_encode ( array( 'response' => 'failed', 'cause' => 'rejected', 'detail' => "Not a valid e164 number" ));
109 break;
110 }
111 if ( number_inpool( $number ) )
112 {
113 print json_encode ( array( 'response' => 'failed', 'cause' => 'exists', 'detail' => "Number is already in the pool" ));
114 break;
115 }
116
117 $result = add_number( $number );
118 if ( $result )
119 {
120 print json_encode ( array( 'response' => 'ok', 'detail' => 'Added ' . $number, 'number' => $number ) );
121 break;
122 }
123 else
124 {
125 print json_encode ( array( 'response' => 'failed', 'cause' => 'rejected' ));
126 }
127 }
128 print json_encode ( array( 'response' => 'invalid') );
129 break;
130 case "/remove":
131 if ( array_key_exists('number', $_POST))
132 {
133 $number = $_POST['number'];
134
135 if (! verify_e164( $number ) )
136 {
137 print json_encode ( array( 'response' => 'failed', 'cause' => 'rejected', 'detail' => "Not a valid e164 number" ));
138 break;
139 }
140 if ( !number_inpool( $number ) )
141 {
142 print json_encode ( array( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => "Number not in pool" ));
143 break;
144 }
145 if ( !remove_number ( $number ) )
146 {
147 print json_encode ( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => "Failed to remove number" ));
148 break;
149 }
150 print json_encode ( array( 'response' => 'ok', 'detail' => 'Removed ' . $number, 'number' => $number ) );
151 break;
152 }
153 print json_encode ( array( 'response' => 'invalid') );
154 break;
155 case "/inpool":
156 if ( array_key_exists('number', $_POST))
157 {
158 $number = $_POST['number'];
159 if ( number_inpool( $number ) )
160 print json_encode ( array( 'response' => 'ok', 'number' => $number ) );
161 else
162 print json_encode ( array( 'response' => 'failed', 'cause' => 'nonexistant') );
163 break;
164 }
165 default:
166 print json_encode ( array( 'response' => 'invalid') );
167 }
168 mysql_close( $config['sql_link'] );
169 ?>