/**
  * Add a mapping of nice short permalinks to a full long path
  *
  * <code>
  * DocumentationPermalinks::add(array(
  * 	'debugging' => 'current/en/sapphire/topics/debugging'
  * ));
  * </code>
  *
  * Do not need to include the language or the version current as it 
  * will add it based off the language or version in the session
  *
  * @param array
  */
 public static function add($map = array())
 {
     if (ArrayLib::is_associative($map)) {
         self::$mapping = array_merge(self::$mapping, $map);
     } else {
         user_error("DocumentationPermalinks::add() requires an associative array", E_USER_ERROR);
     }
 }
 /**
  * Tests to make sure short codes get translated to full paths.
  *
  */
 public function testRedirectingMapping()
 {
     DocumentationPermalinks::add(array('foo' => 'en/framework/subfolder/foo', 'bar' => 'en/cms/bar'));
     $this->autoFollowRedirection = false;
     $v = new DocumentationViewer();
     $response = $v->handleRequest(new SS_HTTPRequest('GET', 'foo'), DataModel::inst());
     $this->assertEquals('301', $response->getStatusCode());
     $this->assertContains('en/framework/subfolder/foo', $response->getHeader('Location'));
 }
 /**
  * Tests to make sure short codes get translated to full paths
  */
 function testRedirectingMapping()
 {
     // testing the viewer class but clearer here
     DocumentationPermalinks::add(array('foo' => 'current/en/sapphire/subfolder/foo', 'bar' => 'current/en/cms/bar'));
     $this->autoFollowRedirection = false;
     $v = new DocumentationViewer();
     $response = $v->handleRequest(new SS_HTTPRequest('GET', 'foo'));
     $this->assertEquals('301', $response->getStatusCode());
     $this->assertContains('current/en/sapphire/subfolder/foo', $response->getHeader('Location'));
 }
 /**
  * Overloaded to avoid "action doesn't exist" errors - all URL parts in
  * this controller are virtual and handled through handleRequest(), not
  * controller methods.
  *
  * @param $request
  * @param $action
  *
  * @return SS_HTTPResponse
  */
 public function handleAction($request, $action)
 {
     // if we submitted a form, let that pass
     if (!$request->isGET()) {
         return parent::handleAction($request, $action);
     }
     $url = $request->getURL();
     //
     // If the current request has an extension attached to it, strip that
     // off and redirect the user to the page without an extension.
     //
     if (DocumentationHelper::get_extension($url)) {
         $this->response = new SS_HTTPResponse();
         $this->response->redirect(DocumentationHelper::trim_extension_off($url) . '/', 301);
         $request->shift();
         $request->shift();
         return $this->response;
     }
     //
     // Strip off the base url
     //
     $base = ltrim(Config::inst()->get('DocumentationViewer', 'link_base'), '/');
     if ($base && strpos($url, $base) !== false) {
         $url = substr(ltrim($url, '/'), strlen($base));
     } else {
     }
     //
     // Handle any permanent redirections that the developer has defined.
     //
     if ($link = DocumentationPermalinks::map($url)) {
         // the first param is a shortcode for a page so redirect the user to
         // the short code.
         $this->response = new SS_HTTPResponse();
         $this->response->redirect($link, 301);
         $request->shift();
         $request->shift();
         return $this->response;
     }
     //
     // Validate the language provided. Language is a required URL parameter.
     // as we use it for generic interfaces and language selection. If
     // language is not set, redirects to 'en'
     //
     $languages = i18n::get_common_languages();
     if (!($lang = $request->param('Lang'))) {
         $lang = $request->param('Action');
         $action = $request->param('ID');
     } else {
         $action = $request->param('Action');
     }
     if (!$lang) {
         return $this->redirect($this->Link('en'));
     } else {
         if (!isset($languages[$lang])) {
             return $this->httpError(404);
         }
     }
     $request->shift(10);
     $allowed = $this->config()->allowed_actions;
     if (in_array($action, $allowed)) {
         //
         // if it's one of the allowed actions such as search or all then the
         // URL must be prefixed with one of the allowed languages.
         //
         return parent::handleAction($request, $action);
     } else {
         //
         // look up the manifest to see find the nearest match against the
         // list of the URL. If the URL exists then set that as the current
         // page to match against.
         // strip off any extensions.
         // if($cleaned !== $url) {
         // 	$redirect = new SS_HTTPResponse();
         // 	return $redirect->redirect($cleaned, 302);
         // }
         if ($record = $this->getManifest()->getPage($url)) {
             $this->record = $record;
             $this->init();
             $type = get_class($this->record);
             $body = $this->renderWith(array("DocumentationViewer_{$type}", "DocumentationViewer"));
             return new SS_HTTPResponse($body, 200);
         } else {
             if ($redirect = $this->getManifest()->getRedirect($url)) {
                 $response = new SS_HTTPResponse();
                 $to = Controller::join_links(Director::baseURL(), $base, $redirect);
                 return $response->redirect($to, 301);
             } else {
                 if (!$url || $url == $lang) {
                     $body = $this->renderWith(array("DocumentationViewer_DocumentationFolder", "DocumentationViewer"));
                     return new SS_HTTPResponse($body, 200);
                 }
             }
         }
     }
     return $this->httpError(404);
 }
 /**
  * Overloaded to avoid "action doesn't exist" errors - all URL parts in this controller are virtual and handled through handleRequest(), not controller methods.
  * @param {SS_HTTPRequest} $request
  * @param {string} $action
  * @return {SS_HTTPResponse}
  */
 public function handleAction($request, $action)
 {
     // if we submitted a form, let that pass
     if (!$request->isGET()) {
         return parent::handleAction($request, $action);
     }
     $url = $request->getURL();
     //If the current request has an extension attached to it, strip that off and redirect the user to the page without an extension.
     if (DocumentationHelper::get_extension($url)) {
         $request->shift();
         $request->shift();
         return $this->redirect(Director::absoluteURL(DocumentationHelper::trim_extension_off($url)) . '/', 301);
     }
     //Strip off the base url
     $base = ltrim(Config::inst()->get('DocumentationViewer', 'link_base'), '/');
     if ($base && strpos($url, $base) !== false) {
         $url = substr(ltrim($url, '/'), strlen($base));
     }
     //Handle any permanent redirections that the developer has defined.
     if ($link = DocumentationPermalinks::map($url)) {
         $request->shift();
         $request->shift();
         //the first param is a shortcode for a page so redirect the user to the short code.
         return $this->redirect($link, 301);
     }
     //Validate the language provided. Language is a required URL parameter. as we use it for generic interfaces and language selection.
     //If language is not set, redirects to 'en'
     $languages = i18n::get_common_languages();
     if (!($lang = $request->param('Lang'))) {
         $lang = $request->param('Action');
         $action = $request->param('ID');
     } else {
         $action = $request->param('Action');
     }
     if (!$lang) {
         return $this->redirect($this->Link('en'));
     } else {
         if (!isset($languages[$lang])) {
             return $this->httpError(404);
         }
     }
     $request->shift(10);
     $allowed = $this->config()->allowed_actions;
     if (in_array($action, $allowed)) {
         //if it's one of the allowed actions such as search or all then the URL must be prefixed with one of the allowed languages.
         return parent::handleAction($request, $action);
     } else {
         //look up the manifest to see find the nearest match against the list of the URL. If the URL exists then set that as the current page to match against. strip off any extensions.
         if ($record = $this->getManifest()->getPage($url)) {
             $this->record = $record;
             return $this->getResponseNegotiator()->respond($request);
         } else {
             if ($redirect = $this->getManifest()->getRedirect($url)) {
                 $to = Controller::join_links(Director::baseURL(), $base, $redirect);
                 return $this->redirect($to, 301);
             } else {
                 if (!$url || $url == $lang) {
                     return $this->getResponseNegotiator()->respond($request);
                 } else {
                     $url = explode('/', $url);
                     $url = implode('/', array_map(function ($a) {
                         return DocumentationHelper::clean_page_url($a);
                     }, $url));
                     if ($record = $this->getManifest()->getPage($url)) {
                         return $this->redirect($record->Link(), 301);
                     }
                 }
             }
         }
     }
     return $this->httpError(404);
 }
 /**
  * Handle the url parsing for the documentation. In order to make this
  * user friendly this does some tricky things..
  *
  * The urls which should work
  * / - index page
  * /en/sapphire - the index page of sapphire (shows versions)
  * /2.4/en/sapphire - the docs for 2.4 sapphire.
  * /2.4/en/sapphire/installation/
  *
  * @return SS_HTTPResponse
  */
 public function handleRequest(SS_HTTPRequest $request)
 {
     // if we submitted a form, let that pass
     if (!$request->isGET() || isset($_GET['action_results'])) {
         return parent::handleRequest($request);
     }
     $firstParam = $request->param('Action') ? $request->param('Action') : $request->shift();
     $secondParam = $request->shift();
     $thirdParam = $request->shift();
     $this->Remaining = $request->shift(10);
     DocumentationService::load_automatic_registration();
     // if no params passed at all then it's the homepage
     if (!$firstParam && !$secondParam && !$thirdParam) {
         return parent::handleRequest($request);
     }
     if ($firstParam) {
         // allow assets
         if ($firstParam == "assets") {
             return parent::handleRequest($request);
         }
         // check for permalinks
         if ($link = DocumentationPermalinks::map($firstParam)) {
             // the first param is a shortcode for a page so redirect the user to
             // the short code.
             $this->response = new SS_HTTPResponse();
             $this->redirect($link, 301);
             // 301 permanent redirect
             return $this->response;
         }
         // check to see if the module is a valid module. If it isn't, then we
         // need to throw a 404.
         if (!DocumentationService::is_registered_entity($firstParam)) {
             return $this->throw404();
         }
         $this->entity = $firstParam;
         $this->language = $secondParam;
         if (isset($thirdParam) && (is_numeric($thirdParam) || in_array($thirdParam, array('master', 'trunk')))) {
             $this->version = $thirdParam;
         } else {
             // current version so store one area para
             array_unshift($this->Remaining, $thirdParam);
             $this->version = false;
         }
     }
     // 'current' version mapping
     $entity = DocumentationService::is_registered_entity($this->entity, null, $this->getLang());
     if ($entity) {
         $current = $entity->getStableVersion();
         $version = $this->getVersion();
         if (!$version) {
             $this->version = $current;
         }
         // Check if page exists, otherwise return 404
         if (!$this->locationExists()) {
             return $this->throw404();
         }
         return parent::handleRequest($request);
     }
     return $this->throw404();
 }