Delete master education.
[platal.git] / modules / api.php
index 5caedbf..bb832f4 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2011 Polytechnique.org                              *
+ *  Copyright (C) 2003-2014 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -26,8 +26,8 @@ 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/search' => $this->make_api_hook('search', AUTH_COOKIE),
+            'api/1/user'   => $this->make_api_hook('user',   AUTH_COOKIE, 'api_user_readonly'),
+            'api/1/search' => $this->make_api_hook('search', AUTH_COOKIE, 'user'),
         );
     }
 
@@ -56,7 +56,30 @@ class ApiModule extends PlModule
             $page->trigError('Malformed search query');
             return PL_BAD_REQUEST;
         }
-        Env::set('quick', $payload['quick']);
+
+        $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]);
@@ -65,11 +88,11 @@ class ApiModule extends PlModule
 
         require_once 'userset.inc.php';
         $view = new QuickSearchSet();
-        $view->addMod('json', 'JSon', true, array('with_score' => true, 'starts_with' => $byletter));
+        $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:
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
 ?>