X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=modules%2Fapi.php;h=11630cc4f5e066973bb1f4fe998d6d30c9e331bd;hb=10a54c4e0ccd0972a2364055982d900c04cfd820;hp=1fa485f00b0dbd8eac61655703c9c291cbfb4cf6;hpb=fc34b3d17fdbc520ddc88f57b9b1eb9dbab0013a;p=platal.git diff --git a/modules/api.php b/modules/api.php index 1fa485f..11630cc 100644 --- a/modules/api.php +++ b/modules/api.php @@ -26,13 +26,14 @@ class ApiModule extends PlModule return array( // TODO(vzanotti): Extend the plat/al engine to support placeholders // in handler urls, for instance "api/1/user/%forlife/isRegistered". - 'api/1/user' => $this->make_api_hook('user', AUTH_COOKIE, 'api_user_readonly'), + 'api/1/user' => $this->make_api_hook('user', AUTH_COOKIE, 'api_user_readonly'), + 'api/1/search' => $this->make_api_hook('search', AUTH_COOKIE, 'user'), ); } // This handler supports the following URL patterns: // /api/1/user/{forlife}/isRegistered - function handler_user(PlPage& $page, PlUser& $authUser, $payload, $user = null, $selector = null) + function handler_user(PlPage $page, PlUser $authUser, $payload, $user = null, $selector = null) { // Retrieve the PlUser referenced in the request. Note that this is the // target user, not the authenticated user. @@ -48,6 +49,49 @@ class ApiModule extends PlModule return PL_NOT_FOUND; } } + + function handler_search(PlPage $page, PlUser $authUser, $payload, $mode = 'quick') + { + if (!isset($payload['quick'])) { + $page->trigError('Malformed search query'); + return PL_BAD_REQUEST; + } + + $query = trim($payload['quick']); + if (@$payload['allow_special']) { + if (starts_with($query, 'admin:')) { + $page->jsonAssign('link_type', 'admin'); + $query = substr($query, 6); + } else if (starts_with($query, 'adm:')) { + $page->jsonAssign('link_type', 'admin'); + $query = substr($query, 4); + } else if (starts_with('admin', $query) || strpos($query, ':') !== false) { + $page->jsonAssign('profile_count', -1); + $page->jsonAssign('profiles', array()); + return PL_JSON; + } else { + $page->jsonAssign('link_type', 'profile'); + } + } + if (strlen($query) < 3) { + $page->jsonAssign('profile_count', -1); + $page->jsonAssign('profiles', array()); + return PL_JSON; + } + + Env::set('quick', $query); + foreach (array('with_soundex', 'exact') as $key) { + if (isset($payload[$key])) { + Env::set($key, $payload[$key]); + } + } + + require_once 'userset.inc.php'; + $view = new QuickSearchSet(); + $view->addMod('json', 'JSon', true, $payload); + $view->apply('api/1/search', $page, 'json'); + return PL_JSON; + } } // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: