/** * parse the url and search for a matching rule. If match, return new controller object. If page, * add view object to controller. * * @return controller the resolved controller */ public function delegate($uri = false) { $reg = registry::getInstance(); $routes = $this->getSummary(); $isCrud = "/\\.do\$/"; // makes it possible to have the boot strap not in root, // ex http://site.com/project1/lolcat // found at http://www.phpaddiction.com/tags/axial/url-routing-with-php-part-one/ $requestURI = explode('/', $_SERVER['REQUEST_URI']); $scriptName = explode('/', $_SERVER['SCRIPT_NAME']); for ($i = 0, $max = count($scriptName); $i < $max; $i++) { if ($requestURI[$i] == $scriptName[$i]) { unset($requestURI[$i]); } } $path = '/' . join('/', $requestURI); // login halts further execution. if ($path === '/login.do') { require LIBRARY . '/auth/login.php'; die; } if ($path === '/logout.do') { require LIBRARY . '/auth/logout.php'; die; } $action = preg_match($isCrud, $path) ? 'crud' : 'page'; $delegated = false; foreach ($routes as $route) { $matchCorrect = preg_match_all($route['pattern'], $path, $matches); $containsVars = false; $vars = explode(',', $route['args']); foreach ($vars as $v) { if (strlen($v) > 0 && !strpos($v, '=')) { $containVars = true; } } if ($containsVars) { // there is variables in the uri, check them $vars = explode(',', $route['args']); if (is_array($vars) && strlen($vars[0]) == 0) { unset($vars[0]); } $varsCorrect = count($matches) - 1 === count($vars); } else { // no vars in the uri, sign it ok $varsCorrect = true; } if ($matchCorrect != 0 && $varsCorrect) { for ($r = 0, $maxR = count($vars); $r < $maxR; $r++) { if ($vars[$r] != '') { if (strpos($vars[$r], '=')) { // fördefinierad, uri skippas $kv = explode('=', $vars[$r]); $_GET[$kv[0]] = $_REQUEST[$kv[0]] = $kv[1]; } else { // från uri $_GET[$vars[$r]] = $_REQUEST[$vars[$r]] = $matches[$r + 1][0]; } } } $controllers = $this->getEntity($route['id']); $reg['route'] = new page(); foreach ($controllers as $controller) { $reg['route']->addContent($controller['mpId'], new controller_page($controller['service'], $controller['action'])); } $reg['route']->execute(); $delegated = true; break; } } if ($delegated == false) { // try to resolve anonymous controller /* todo: här ska applikationen försöka hitta en ensam page controller. */ try { if ($action == 'crud') { preg_match("/^(.+?)\\/(.+?)\\.do\$/", $path, $matches); $service = $matches[1]; $controller = $matches[2]; $controller = new controller_crud($service, $controller); $controller->execute(); } else { http_response::redir('/'); } } catch (Exception $e) { throw $e; } } }
} else { $matches = array(); $matches[1] = $action; if ($responseType) { $matches[2] = $responseType; } } // stop if model file not found $model_exists = false; if (file_exists(sprintf('%s/model/%s.php', ROOT, $matches[1]))) { $model_exists = true; } else { // try to include crud controller instead if (file_exists(sprintf('%s/service/%s/crud.php', ROOT, $matches[1]))) { $crud = new controller_crud($matches[1]); $crud->execute(); die; } } if (!$model_exists) { rest_utils::sendResponse($data, 501); } // no valid files $modelId = strtr($matches[1], array('/' => '_', '.' => '')); // stop if not valid verb if (!in_array($data->getMethod(), array('get', 'post', 'put'))) { rest_utils::sendResponse($data, 501); } switch ($data->getMethod()) { case 'get': $http_accept = isset($matches[2]) ? strtr($matches[2], array('.' => '')) : $data->getHttpAccept();