function glue_response($req, $response) { $template_vars = is_array($response) ? array_merge($req, $response) : array_merge($req, array('content' => $response)); $headers = array_merge(array('content-type' => 'text/html'), response_headers($response)); if (isset($template_vars['template'])) { list($handler, $template) = template_resolver($template_vars['template']); unset($template_vars['template']); //TODO: feels liks a ugly hack to assume func from template but works well for handler-less (template-only) routes if (!isset($template_vars['handler'])) { $template_vars['handler'] = $handler; } if (!isset($template_vars['func'])) { $template_vars['func'] = $template; } if (template_file_exists(handler_template($handler, $template))) { if (isset($template_vars['layout'])) { $layout = $template_vars['layout']; unset($template_vars['layout']); if (is_equal(false, $layout)) { return response_(response_status_code($template_vars), $headers, template_render(handler_template($handler, $template), $template_vars)); } else { list($layout_handler, $layout_template) = template_resolver($layout); return response_(response_status_code($template_vars), $headers, template_compose(handler_template($handler, $template), $template_vars, handler_template($layout_handler, $layout_template), $template_vars)); } } else { return response_(response_status_code($template_vars), $headers, template_compose(handler_template($handler, $template), $template_vars, handler_layout($handler), $template_vars)); } } } return _200_plain(print_r($response, true)); }
private function postLogin() { // clear errors $this->errors = array(); // check a username and password were given if (!isset($_POST["diea_username"]) || empty($_POST["diea_username"]) || !isset($_POST["diea_password"]) || empty($_POST["diea_password"])) { $this->errors[] = "Edshare username or password not given"; $this->getLogin(); return; } $curl = curl_init(); curl_setopt_array($curl, array(CURLOPT_URL => "http://" . DIEA_EDSHARE_HOST . "/sword-app/servicedocument", CURLOPT_POST => false, CURLOPT_HEADER => true, CURLOPT_USERPWD => $_POST["diea_username"] . ":" . $_POST["diea_password"], CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => array("Expect: "))); $response = curl_exec($curl); $responseinfo = curl_getinfo($curl); $code = $responseinfo["http_code"]; $headers = response_headers($response); $body = response_body($response); switch ($code) { case 401: // bad username/password unset($_SESSION[SESSION_PREFIX . "diea_username"], $_SESSION[SESSION_PREFIX . "diea_password"], $_SESSION[SESSION_PREFIX . "diea_servicedocxml"]); $this->errors[] = "There was an authorization problem" . (isset($headers["X-Error-Code"]) ? ": " . $headers["X-Error-Code"] : ""); $this->getLogic(); return; case 200: // parse XML response $_SESSION[SESSION_PREFIX . "diea_username"] = $_POST["diea_username"]; $_SESSION[SESSION_PREFIX . "diea_password"] = $_POST["diea_password"]; $_SESSION[SESSION_PREFIX . "diea_servicedocxml"] = $body; $this->getLogic(); return; default: // unexpected response $this->errors[] = "Unexpected response: " . $responseinfo["http_code"] . ". Response headers follow:"; foreach ($headers as $k => $v) { $this->errors[] = "{$k}: {$v}"; } $this->getLogic(); return; } }