148 default: |
148 default: |
149 return json_die("Undefined action: $action"); |
149 return json_die("Undefined action: $action"); |
150 } |
150 } |
151 } |
151 } |
152 |
152 |
|
153 function api_request_handler($httpd, $socket) |
|
154 { |
|
155 global $json; |
|
156 $httpd->header('Content-type: text/javascript'); |
|
157 if ( !isset($_POST['request']) ) |
|
158 { |
|
159 return json_die("No request specified on POST."); |
|
160 } |
|
161 $req = $json->decode($_POST['request']); |
|
162 if ( !isset($req['action']) ) |
|
163 { |
|
164 return json_die("No action specified."); |
|
165 } |
|
166 switch($req['action']) |
|
167 { |
|
168 case 'check_login': |
|
169 global $use_auth, $auth_data; |
|
170 if ( $use_auth ) |
|
171 { |
|
172 $return = array( |
|
173 'need_login' => true |
|
174 ); |
|
175 if ( isset($req['username']) && isset($req['password']) ) |
|
176 { |
|
177 $return['auth_valid'] = ( isset($auth_data[$req['username']]) && $auth_data[$req['username']] === $req['password'] ); |
|
178 } |
|
179 } |
|
180 else |
|
181 { |
|
182 $return = array( |
|
183 'need_login' => false, |
|
184 'auth_valid' => true |
|
185 ); |
|
186 } |
|
187 break; |
|
188 case 'login': |
|
189 global $use_auth, $auth_data; |
|
190 if ( $use_auth ) |
|
191 { |
|
192 if ( !isset($req['username']) || !isset($req['password']) ) |
|
193 { |
|
194 return json_die("Username or password not provided"); |
|
195 } |
|
196 if ( $session = login($req['username'], $req['password']) ) |
|
197 { |
|
198 $return = array( |
|
199 'need_login' => true, |
|
200 'login_cookie' => $session |
|
201 ); |
|
202 } |
|
203 else |
|
204 { |
|
205 $return = array( |
|
206 'need_login' => true, |
|
207 'login_cookie' => false |
|
208 ); |
|
209 } |
|
210 } |
|
211 else |
|
212 { |
|
213 $return = array( |
|
214 'need_login' => false, |
|
215 'login_cookie' => false |
|
216 ); |
|
217 } |
|
218 break; |
|
219 default: |
|
220 return json_die("Undefined action '{$req['action']}'."); |
|
221 break; |
|
222 } |
|
223 echo $json->encode($return); |
|
224 } |