PageRenderTime 37ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/atk4/lib/PageManager.php

https://github.com/mahimarathore/mahi
PHP | 220 lines | 134 code | 12 blank | 74 comment | 1 complexity | 0afeb7c1d01c781be7ada6805168c085 MD5 | raw file
Possible License(s): AGPL-3.0, MPL-2.0-no-copyleft-exception
  1. <?php // vim:ts=4:sw=4:et:fdm=marker
  2. /*
  3. * Undocumented
  4. *
  5. * @link http://agiletoolkit.org/
  6. *//*
  7. ==ATK4===================================================
  8. This file is part of Agile Toolkit 4
  9. http://agiletoolkit.org/
  10. (c) 2008-2013 Agile Toolkit Limited <info@agiletoolkit.org>
  11. Distributed under Affero General Public License v3 and
  12. commercial license.
  13. See LICENSE or LICENSE_COM for more information
  14. =====================================================ATK4=*/
  15. class PageManager extends AbstractController {
  16. /*
  17. * This is a generic page manager. For web applications it calculates
  18. * base URI, sets up path manager with the URI locations, determines
  19. * which page was requested
  20. *
  21. * This class works with PathFinder, ApiWeb, and Location.
  22. */
  23. // you can access variabless below through $this->api->pm->base_url
  24. // concatinate them to get full URL
  25. public $base_url; // http://yoursite.com:81
  26. /*
  27. Base URL defines the absolute destination of our server. Because some
  28. other resources may be located outside of our Base Path, we need to
  29. know a Base URL.
  30. For CLI scripts, you need to set this manually. Also if you are
  31. going to use URLs in emails, you should use this.
  32. See also: URL::useAbsoluteURL();
  33. */
  34. public $base_path; // /admin/
  35. /*
  36. Base PATH points to the top location of our project. Basically it's
  37. where the project is installed in the webroot. This is determined
  38. by thelocation of catch-all file. It is determined by SCRIPT_NAME
  39. which should be supported by most web installations. It will also
  40. work when mod_rewrite is not used.
  41. You can use $base_path in your script to put it say on a logo link
  42. Also - some other parts of the library may have a different path,
  43. for example base_path could be = /admin/, and atk4_path could be /amodules/
  44. If project is installed in web-root, then $base_path will be "/"
  45. path always starts and ends with slash
  46. */
  47. public $page; // user/add
  48. /*
  49. This is a third and a final part of the URLs. This points to a page
  50. which were reuqested. You can pass path to getDestinationURL() function,
  51. as a first argument. Also $path is used to determine which page class
  52. to load.
  53. Page must never start with slash. Also if path is empty, then
  54. the "index" is used automatically.
  55. */
  56. public $base_directory; // /home/web/admin/ - physical path
  57. public $template_filename;
  58. function init(){
  59. parent::init();
  60. $this->page=&$this->api->page; // link both variables
  61. $this->api->pm=$this;
  62. // Firstly, the original URL is retrieved. This function should
  63. // take care of all possible rewrite engines and bring up a real
  64. // URL which matches the one in the browser. Also e will need to
  65. // determine a relative path for the requested page
  66. $this->parseRequestedURL();
  67. // This function will continue initialization of the page itself.
  68. $this->calculatePageName();
  69. }
  70. function calculatePageName(){
  71. // Now. We need to decide what will be the main page to
  72. // to display the whole thing. This page will contain a
  73. // main template and will be responsible for rendering the
  74. // whole page. This function will initialize the object and
  75. // return it
  76. // Lastly we need a sub-class, which would worry only about
  77. // the requested page. This is what we call - a page.
  78. }
  79. function parseRequestedURL(){
  80. $this->base_path=$this->unix_dirname($_SERVER['SCRIPT_NAME']);
  81. // for windows
  82. if(substr($this->base_path,-1)=='\\')$this->base_path=substr($this->base_path,1,-1).'/';
  83. if(substr($this->base_path,-1)!='/')$this->base_path.='/';
  84. // We are assuming that all requests are being redirected though a single file
  85. $this->base_directory=$this->unix_dirname($_SERVER['SCRIPT_FILENAME']).'/';
  86. // This is the re-constructions of teh proper URL.
  87. // 1. Schema
  88. $url=$this->api->getConfig('atk/base_url',null);
  89. if(is_null($url)){
  90. // Detect it
  91. $url = 'http';
  92. $https = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || $_SERVER['SERVER_PORT']==443;
  93. if($https)$url.='s';
  94. // 2. Continue building. We are adding hostname next and port.
  95. $url .= "://".$_SERVER["SERVER_NAME"];
  96. //if($_SERVER["SERVER_PORT"]!="80")$url .= ":".$_SERVER['SERVER_PORT'];
  97. if(($_SERVER["SERVER_PORT"]=="80" && !$https ) || ($_SERVER["SERVER_PORT"]=="443" && $https)){
  98. ;
  99. }else{
  100. $url .= ":".$_SERVER['SERVER_PORT'];
  101. }
  102. }
  103. // We have now arrived at base_url as defined
  104. $this->base_url=$url;
  105. // 3. Next we need a base_part of our URL. There are many different
  106. // variables and approaches we tried it, REDIRECT_URL_ROOT, REDIRECT_URL,
  107. // etc, however most reliable is $this->unix_dirname(SCRIPT_NAME)
  108. $path=$this->unix_dirname($_SERVER['SCRIPT_NAME']);
  109. if(substr($path,-1)!='/')$path.='/';
  110. // We have now arrived at base_path as defined
  111. $this->base_path=$path;
  112. // 4. We now look at RequestURI and extract base_path from the beggining
  113. if(isset($_GET['page'])){
  114. $page=$_GET['page'];
  115. }else{
  116. $request_uri=$this->getRequestURI();
  117. if(strpos($request_uri,$path)!==0){
  118. throw $this->exception("URL matching problem")
  119. ->addMoreInfo('RequestURI',$request_uri)
  120. ->addMoreInfo('BasePath',$path);
  121. }
  122. $page=substr($request_uri,strlen($path));
  123. if(!$page)$page='index';
  124. // Remove postfix from page if any
  125. $page=preg_replace('/\..*$/','',$page);
  126. $page=preg_replace('/\/$/','',$page);
  127. $page=str_replace('/','_',$page);
  128. if(substr($page,-1)=='_')$page=substr($page,0,-1);
  129. }
  130. if(strpos($page,'.')!==false)throw $this->exception('Page may not contain periods (.)')
  131. ->addMoreInfo('page',$page);
  132. // We have now arrived at the page as per specification.
  133. $this->page=str_replace('/','_',$page);
  134. $this->template_filename=$this->page;
  135. if(substr($this->template_filename,-1)=='/')$this->template_filename.="index";
  136. $this->api->pathfinder->base_location->setBaseURL($this->base_path);
  137. $this->debug("base_path=".$this->base_path);
  138. $this->debug("base_directory=".$this->base_directory);
  139. $this->debug("page=".$this->page);
  140. $this->debug("api/page=".$this->api->page);
  141. $this->debug("template_filename=".$this->template_filename);
  142. }
  143. function getRequestURI(){
  144. // WARNING. This function URI excludes query string
  145. if (isset($_SERVER['HTTP_X_REWRITE_URL'])) { // IIS
  146. $request_uri = $_SERVER['HTTP_X_REWRITE_URL'];
  147. } elseif (isset($_SERVER['REQUEST_URI'])) { // Apache
  148. $request_uri = $_SERVER['REQUEST_URI'];
  149. } elseif (isset($_SERVER['ORIG_PATH_INFO'])) { // IIS 5.0, PHP as CGI
  150. $request_uri = $_SERVER['ORIG_PATH_INFO'];
  151. // This one comes without QUERRY string
  152. } else {
  153. throw new BaseException('Unable to determine RequestURI. This shouldn\'t be called at all in CLI');
  154. }
  155. $request_uri=explode('?',$request_uri,2);
  156. return $request_uri[0];
  157. }
  158. function unix_dirname($path){
  159. $chunks=explode('/',$path);
  160. array_pop($chunks);
  161. if(!$chunks)return '/';
  162. return implode('/',$chunks);
  163. }
  164. function getUrlRoot(){
  165. if($r=='')$r='/';
  166. return $r;
  167. }
  168. /* @obsolete since 4.2.2
  169. function getDestinationURL($page){
  170. if($page[0]=='/'){
  171. // Location absolute
  172. return $this->base_path.substr($page,1).'.html';
  173. }
  174. return $page.'.html';
  175. }
  176. */
  177. }