# HG changeset patch # User Dan # Date 1223515368 14400 # Node ID b8a3c9c54fbe8ae7ee104b3bf38994b97189278b # Parent 6d8b9497e44beb6d0681f053817fe5c910803cf9 Added login and credentials JSON API for some... upcoming new features. diff -r 6d8b9497e44b -r b8a3c9c54fbe ajax.php --- a/ajax.php Wed Sep 24 13:47:42 2008 -0400 +++ b/ajax.php Wed Oct 08 21:22:48 2008 -0400 @@ -150,3 +150,75 @@ } } +function api_request_handler($httpd, $socket) +{ + global $json; + $httpd->header('Content-type: text/javascript'); + if ( !isset($_POST['request']) ) + { + return json_die("No request specified on POST."); + } + $req = $json->decode($_POST['request']); + if ( !isset($req['action']) ) + { + return json_die("No action specified."); + } + switch($req['action']) + { + case 'check_login': + global $use_auth, $auth_data; + if ( $use_auth ) + { + $return = array( + 'need_login' => true + ); + if ( isset($req['username']) && isset($req['password']) ) + { + $return['auth_valid'] = ( isset($auth_data[$req['username']]) && $auth_data[$req['username']] === $req['password'] ); + } + } + else + { + $return = array( + 'need_login' => false, + 'auth_valid' => true + ); + } + break; + case 'login': + global $use_auth, $auth_data; + if ( $use_auth ) + { + if ( !isset($req['username']) || !isset($req['password']) ) + { + return json_die("Username or password not provided"); + } + if ( $session = login($req['username'], $req['password']) ) + { + $return = array( + 'need_login' => true, + 'login_cookie' => $session + ); + } + else + { + $return = array( + 'need_login' => true, + 'login_cookie' => false + ); + } + } + else + { + $return = array( + 'need_login' => false, + 'login_cookie' => false + ); + } + break; + default: + return json_die("Undefined action '{$req['action']}'."); + break; + } + echo $json->encode($return); +} diff -r 6d8b9497e44b -r b8a3c9c54fbe greyhound.php --- a/greyhound.php Wed Sep 24 13:47:42 2008 -0400 +++ b/greyhound.php Wed Oct 08 21:22:48 2008 -0400 @@ -155,6 +155,7 @@ $httpd->add_handler('config', 'function', 'greyhound_config'); $httpd->add_handler('action.json', 'function', 'ajax_request_handler'); $httpd->add_handler('artwork', 'function', 'artwork_request_handler'); + $httpd->add_handler('api', 'function', 'api_request_handler'); $httpd->add_handler('scripts', 'dir', GREY_ROOT . '/scripts'); $httpd->add_handler('favicon.ico', 'file', GREY_ROOT . '/amarok_icon.ico'); $httpd->add_handler('apple-touch-icon.png', 'file', GREY_ROOT . '/apple-touch-icon.png');