diff options
author | Aleksander Machniak <alec@alec.pl> | 2012-01-16 12:22:13 (GMT) |
---|---|---|
committer | Aleksander Machniak <alec@alec.pl> | 2012-01-16 12:22:13 (GMT) |
commit | e8eaeb86f077deaffdaa0c3262284e4f540cada6 (patch) | |
tree | b54715ba9be402684a86ce1f92dbf41c6ab66be7 | |
parent | be19966794ae6f9074fed51af8b902d03eacdf73 (diff) | |
download | kolab-wap-e8eaeb86f077deaffdaa0c3262284e4f540cada6.tar.gz |
CS fixes
-rw-r--r-- | lib/functions.php | 16 | ||||
-rw-r--r-- | lib/kolab_admin_api_controller.php | 394 | ||||
-rw-r--r-- | lib/kolab_admin_api_service.php | 32 | ||||
-rw-r--r-- | lib/kolab_admin_json_output.php | 60 | ||||
-rw-r--r-- | lib/kolab_client_task.php | 2 | ||||
-rw-r--r-- | public_html/api/index.php | 25 |
6 files changed, 273 insertions, 256 deletions
diff --git a/lib/functions.php b/lib/functions.php index 29d6b60..a912e48 100644 --- a/lib/functions.php +++ b/lib/functions.php @@ -38,11 +38,12 @@ mb_internal_encoding(KADM_CHARSET); @mb_regex_encoding(KADM_CHARSET); // register autoloader -function class_autoloader($classname) { +function class_autoloader($classname) +{ $classname = preg_replace('/(Net|MDB2|HTTP)_(.+)/', "\\1/\\2", $classname); if ($fp = @fopen("$classname.php", 'r', true)) { - include_once("$classname.php"); + include_once "$classname.php"; fclose($fp); return true; } @@ -52,8 +53,9 @@ function class_autoloader($classname) { spl_autoload_register('class_autoloader'); -function query($query, $_conn = 'kolab_wap') { - require_once('SQL.php'); +function query($query, $_conn = 'kolab_wap') +{ + require_once 'SQL.php'; $sql = SQL::get_instance($_conn); @@ -63,7 +65,8 @@ function query($query, $_conn = 'kolab_wap') { /** * Prints debug info into the 'console' log */ -function console() { +function console() +{ $args = func_get_args(); $msg = array(); @@ -81,7 +84,8 @@ function console() { * @param string $name Name of the log file * @param mixed $line Line to append */ -function write_log($name, $line) { +function write_log($name, $line) +{ if (!is_string($line)) { $line = var_export($line, true); } diff --git a/lib/kolab_admin_api_controller.php b/lib/kolab_admin_api_controller.php index f3f957f..f921a42 100644 --- a/lib/kolab_admin_api_controller.php +++ b/lib/kolab_admin_api_controller.php @@ -1,249 +1,263 @@ <?php - /** - * Main controller class to serve the Kolab Admin API - */ - class kolab_admin_api_controller +/** + * Main controller class to serve the Kolab Admin API + */ +class kolab_admin_api_controller +{ + public $output; + + private $uid; + private $request = array(); + private $services = array(); + private $domains = array('localhost.localdomain'); + + public function __construct() { - public $output; - - private $uid; - private $request = Array(); - private $services = Array(); - private $domains = Array('localhost.localdomain'); - - public function __construct() - { - $this->output = new kolab_admin_json_output(); - - if (isset($_GET['service']) && !empty($_GET['service'])) { - if (isset($_GET['method']) && !empty($_GET['method'])) { - $this->request = Array( - 'service' => $_GET['service'], - 'method' => $_GET['method'] - ); - } else { - throw new Exception("Unknown service", 400); - } + $this->output = new kolab_admin_json_output(); - } else { - throw new Exception("Unknown service", 400); + if (!empty($_GET['service'])) { + if (!empty($_GET['method'])) { + $this->request = array( + 'service' => $_GET['service'], + 'method' => $_GET['method'] + ); } - - // TODO: register services based on config or whatsoever - $this->add_service('form_value', 'kolab_admin_form_value_actions'); - $this->add_service('group_types', 'kolab_admin_group_types_actions'); - $this->add_service('group', 'kolab_admin_group_actions'); - $this->add_service('groups', 'kolab_admin_groups_actions'); - $this->add_service('user_types', 'kolab_admin_user_types_actions'); - $this->add_service('user', 'kolab_admin_user_actions'); - $this->add_service('users', 'kolab_admin_users_actions'); - $this->add_service('domains', 'kolab_admin_domains_actions'); - } - - /** - * Register a class that serves a particular backend service - */ - public function add_service($service, $handler) - { - if ($this->services[$service]) { - error_log("Service $service is already registered."); - return false; + else { + throw new Exception("Unknown methods", 400); } - - $this->services[$service] = $handler; + } + else { + throw new Exception("Unknown service", 400); } - /** - * Getter for a certain service object - */ - public function get_service($service) - { - error_log($service); - - // we are the system! - if ($service == 'system') - return $this; + // TODO: register services based on config or whatsoever + $this->add_service('form_value', 'kolab_admin_form_value_actions'); + $this->add_service('group_types', 'kolab_admin_group_types_actions'); + $this->add_service('group', 'kolab_admin_group_actions'); + $this->add_service('groups', 'kolab_admin_groups_actions'); + $this->add_service('user_types', 'kolab_admin_user_types_actions'); + $this->add_service('user', 'kolab_admin_user_actions'); + $this->add_service('users', 'kolab_admin_users_actions'); + $this->add_service('domains', 'kolab_admin_domains_actions'); + } - if ($handler = $this->services[$service]) { - if (is_string($handler)) - $handler = $this->services[$service] = new $handler($this); + /** + * Register a class that serves a particular backend service + */ + public function add_service($service, $handler) + { + if ($this->services[$service]) { + error_log("Service $service is already registered."); + return false; + } - if (is_a($handler, 'kolab_admin_api_service')) - return $handler; - } + $this->services[$service] = $handler; + } - error_log("Unknown service $service"); + /** + * Getter for a certain service object + */ + public function get_service($service) + { + error_log($service); - throw new Exception("Unknown service", 400); + // we are the system! + if ($service == 'system') { + return $this; } + if ($handler = $this->services[$service]) { + if (is_string($handler)) { + $handler = $this->services[$service] = new $handler($this); + } - /** - * Getter for the authenticated user (ID) - */ - public function get_uid() - { - return $this->uid; + if (is_a($handler, 'kolab_admin_api_service')) { + return $handler; + } } + error_log("Unknown service $service"); - /** - * Process the request and dispatch it to the requested service - */ - public function dispatch($postdata) - { - $service = $this->request['service']; - $method = $this->request['method']; + throw new Exception("Unknown service", 400); + } - console("Calling method " . $method . " on service . " . $service); - // validate user session - if ($method != 'authenticate') { - if (!$this->session_validate($postdata)) { - throw new Exception("Invalid session", 403); - } - } - // call service method - $service_handler = $this->get_service($service); + /** + * Getter for the authenticated user (ID) + */ + public function get_uid() + { + return $this->uid; + } + - if (method_exists($service_handler, $method)) { - $result = $service_handler->$method($_GET, $postdata); - } elseif (method_exists($service_handler, $service . "_" . $method)) { - $call_method = $service . "_" . $method; - $result = $service_handler->$call_method($_GET, $postdata); - } else { - throw new Exception("Unknown method", 405); + /** + * Process the request and dispatch it to the requested service + */ + public function dispatch($postdata) + { + $service = $this->request['service']; + $method = $this->request['method']; + + console("Calling method " . $method . " on service . " . $service); + // validate user session + if ($method != 'authenticate') { + if (!$this->session_validate($postdata)) { + throw new Exception("Invalid session", 403); } + } + + // call service method + $service_handler = $this->get_service($service); - // send response - if ($result !== false) - $this->output->success($result); - else - $this->output->error("Internal error", 500); + if (method_exists($service_handler, $method)) { + $result = $service_handler->$method($_GET, $postdata); + } + else if (method_exists($service_handler, $service . "_" . $method)) { + $call_method = $service . "_" . $method; + $result = $service_handler->$call_method($_GET, $postdata); + } + else { + throw new Exception("Unknown method", 405); } + // send response + if ($result !== false) { + $this->output->success($result); + } + else { + $this->output->error("Internal error", 500); + } + } - /** - * Validate the submitted session token - */ - private function session_validate($postdata) - { - $sess_id = !empty($postdata['session_token']) ? $postdata['session_token'] : kolab_utils::get_request_header('X-Session-Token'); - if (empty($sess_id)) - return false; + /** + * Validate the submitted session token + */ + private function session_validate($postdata) + { + if (!empty($postdata['session_token'])) { + $sess_id = $postdata['session_token']; + } + else { + $sess_id = kolab_utils::get_request_header('X-Session-Token'); + } - session_id($sess_id); - session_start(); + if (empty($sess_id)) { + return false; + } - if (isset($_SESSION['user']) && $_SESSION['user']->authenticated()) { - return true; - } + session_id($sess_id); + session_start(); - return false; + if (isset($_SESSION['user']) && $_SESSION['user']->authenticated()) { + return true; } + return false; + } - /* ======== system.* method handlers ======== */ + /* ======== system.* method handlers ======== */ - /** - * Authenticate a user with the given credentials - * - * @param array GET request parameters - * @param array POST data - */ - private function authenticate($request, $postdata) - { - $valid = false; - // destroy old session - if ($this->session_validate($postdata)) - session_destroy(); + /** + * Authenticate a user with the given credentials + * + * @param array GET request parameters + * @param array POST data + */ + private function authenticate($request, $postdata) + { + $valid = false; - session_start(); + // destroy old session + if ($this->session_validate($postdata)) { + session_destroy(); + } - $_SESSION['user'] = new User(); - $valid = $_SESSION['user']->authenticate($postdata['username'], $postdata['password']); + session_start(); - // start new (PHP) session - if ($valid) { - $_SESSION['start'] = time(); - return Array( - 'user' => $_SESSION['user']->get_username(), - 'domain' => $_SESSION['user']->get_domain(), - 'session_token' => session_id() - ); - } + $_SESSION['user'] = new User(); + $valid = $_SESSION['user']->authenticate($postdata['username'], $postdata['password']); - return false; + // start new (PHP) session + if ($valid) { + $_SESSION['start'] = time(); + return array( + 'user' => $_SESSION['user']->get_username(), + 'domain' => $_SESSION['user']->get_domain(), + 'session_token' => session_id() + ); } + return false; + } - /** - * Provide a list of capabilities the backend provides to the current user - */ - private function capabilities() - { - $auth = Auth::get_instance(); - $this->domains = $auth->normalize_result($auth->list_domains()); - $result = array(); + /** + * Provide a list of capabilities the backend provides to the current user + */ + private function capabilities() + { + $auth = Auth::get_instance(); + $this->domains = $auth->normalize_result($auth->list_domains()); - // Should we have no permissions to list domain name spaces, - // we should always return our own. - if (count($this->domains) < 1) { - $this->domains[] = $_SESSION['user']->get_domain(); - } + $result = array(); - // add capabilities of all registered services - foreach ($this->domains as $domain) { - // define our very own capabilities - $actions = array( - array('action' => 'system.quit', 'type' => 'w'), - ); + // Should we have no permissions to list domain name spaces, + // we should always return our own. + if (count($this->domains) < 1) { + $this->domains[] = $_SESSION['user']->get_domain(); + } - foreach ($this->services as $sname => $handler) { - $service = $this->get_service($sname); - foreach ($service->capabilities($domain) as $method => $type) { - $actions[] = array('action' => "$sname.$method", 'type' => $type); - } + // add capabilities of all registered services + foreach ($this->domains as $domain) { + // define our very own capabilities + $actions = array( + array('action' => 'system.quit', 'type' => 'w'), + ); + + foreach ($this->services as $sname => $handler) { + $service = $this->get_service($sname); + foreach ($service->capabilities($domain) as $method => $type) { + $actions[] = array('action' => "$sname.$method", 'type' => $type); } - - // TODO: 'associateddomain' is very specific to 389ds based deployments, and this - // is supposed to be very generic. - $result[] = array('domain' => $domain['associateddomain'], 'actions' => $actions); } - return array('capabilities' => $result); + // TODO: 'associateddomain' is very specific to 389ds based deployments, and this + // is supposed to be very generic. + $result[] = array('domain' => $domain['associateddomain'], 'actions' => $actions); } - private function get_domain() { - return Array('domain' => $_SESSION['user']->get_domain()); - } + return array('capabilities' => $result); + } - /** - * End the current user ession - */ - private function quit() - { - session_destroy(); + private function get_domain() { + return array('domain' => $_SESSION['user']->get_domain()); + } + + /** + * End the current user ession + */ + private function quit() + { + session_destroy(); + return true; + } + + private function select_domain($getdata) { + if (isset($getdata['domain'])) { + $_SESSION['user']->set_domain($getdata['domain']); return true; } - - private function select_domain($getdata) { - if (isset($getdata['domain'])) { - $_SESSION['user']->set_domain($getdata['domain']); - return true; - } else { - return false; - } + else { + return false; } + } - /* ======== Utility functions ======== */ - + /* ======== Utility functions ======== */ - } -?> +} diff --git a/lib/kolab_admin_api_service.php b/lib/kolab_admin_api_service.php index 7b871cb..a80bd5c 100644 --- a/lib/kolab_admin_api_service.php +++ b/lib/kolab_admin_api_service.php @@ -1,22 +1,20 @@ <?php - /** - * Interface class for Kolab Admin Services - */ - abstract class kolab_admin_api_service - { - protected $controller; - - public function __construct($ctrl) - { - $this->controller = $ctrl; - } - - /** - * Advertise this service's capabilities - */ - abstract public function capabilities($domain); +/** + * Interface class for Kolab Admin Services + */ +abstract class kolab_admin_api_service +{ + protected $controller; + public function __construct($ctrl) + { + $this->controller = $ctrl; } -?> + /** + * Advertise this service's capabilities + */ + abstract public function capabilities($domain); + +} diff --git a/lib/kolab_admin_json_output.php b/lib/kolab_admin_json_output.php index 8dc26f1..3a7f1d6 100644 --- a/lib/kolab_admin_json_output.php +++ b/lib/kolab_admin_json_output.php @@ -1,45 +1,45 @@ <?php +/** + * View class generating JSON output + */ +class kolab_admin_json_output +{ + /** - * View class generating JSON output + * */ - class kolab_admin_json_output + public function success($data) { - - /** - * - */ - public function success($data) - { - if (!is_array($data)) - $data = array(); - - $this->send(array('status' => 'OK', 'result' => $data)); + if (!is_array($data)) { + $data = array(); } + $this->send(array('status' => 'OK', 'result' => $data)); + } - /** - * - */ - public function error($errdata, $code = 400) - { - if (is_string($errdata)) - $errdata = array('reason' => $errdata); - $this->send(array('status' => 'ERROR', 'code' => $code) + $errdata); + /** + * + */ + public function error($errdata, $code = 400) + { + if (is_string($errdata)) { + $errdata = array('reason' => $errdata); } + $this->send(array('status' => 'ERROR', 'code' => $code) + $errdata); + } - /** - * - */ - public function send($data) - { - header("Content-Type: application/json"); - echo json_encode($data); - exit; - } + /** + * + */ + public function send($data) + { + header("Content-Type: application/json"); + echo json_encode($data); + exit; } -?> +} diff --git a/lib/kolab_client_task.php b/lib/kolab_client_task.php index 8b48e6c..9479641 100644 --- a/lib/kolab_client_task.php +++ b/lib/kolab_client_task.php @@ -419,7 +419,7 @@ class kolab_client_task if (!isset($_SESSION['user_types'])) { $result = $this->api->post('user_types.list'); $list = $result->get(); - +console($list); if (is_array($list)) { $_SESSION['user_types'] = $list; } diff --git a/public_html/api/index.php b/public_html/api/index.php index 54ae2cb..1349a1c 100644 --- a/public_html/api/index.php +++ b/public_html/api/index.php @@ -1,18 +1,19 @@ <?php - require_once( dirname(__FILE__) . "/../../lib/functions.php"); - // init frontend controller - $controller = new kolab_admin_api_controller; +require_once dirname(__FILE__) . "/../../lib/functions.php"; - try { - $postdata = $_SERVER['REQUEST_METHOD'] == 'POST' ? @json_decode(file_get_contents('php://input'), true) : null; - $controller->dispatch($postdata); - } catch(Exception $e) { - error_log($e->getMessage()); - $controller->output->error($e->getMessage(), $e->getCode()); - } +// init frontend controller +$controller = new kolab_admin_api_controller; - // if we arrive here the controller didn't generate output - $controller->output->error("Invalid request"); +try { + $postdata = $_SERVER['REQUEST_METHOD'] == 'POST' ? @json_decode(file_get_contents('php://input'), true) : null; + $controller->dispatch($postdata); +} catch(Exception $e) { + error_log($e->getMessage()); + $controller->output->error($e->getMessage(), $e->getCode()); +} + +// if we arrive here the controller didn't generate output +$controller->output->error("Invalid request"); ?> |