PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/atk4/lib/URL.php

https://github.com/mahimarathore/mahi
PHP | 204 lines | 129 code | 30 blank | 45 comment | 26 complexity | d44e54206687fa713ffdd3610a46a822 MD5 | raw file
Possible License(s): AGPL-3.0, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /***********************************************************
  3. When $api->url() is called, this object is used to avoid
  4. double-encoding. Return URL when converting to string.
  5. Reference:
  6. http://agiletoolkit.org/doc/ref
  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 URL extends AbstractModel {
  16. // Page is a location of destination page. It have to be absolute and relative to project root
  17. protected $page=null;
  18. protected $arguments=array();
  19. protected $extension='.html';
  20. protected $absolute=false; // if true then will return full URL (for external documents)
  21. public $base_url=null;
  22. function init(){
  23. parent::init();
  24. $this->setPage(null);
  25. $this->addStickyArguments();
  26. $this->extension=$this->api->getConfig('url_postfix',$this->extension);
  27. }
  28. /** [private] add arguments set as sticky through API */
  29. function addStickyArguments(){
  30. $sticky=$this->api->getStickyArguments();
  31. $args=array();
  32. if($sticky && is_array($sticky)){
  33. foreach($sticky as $key=>$val){
  34. if($val===true){
  35. if(isset($_GET[$key])){
  36. $val=$_GET[$key];
  37. }else{
  38. continue;
  39. }
  40. }
  41. if(!isset($args[$key])){
  42. $args[$key]=$val;
  43. }
  44. }
  45. }
  46. $this->setArguments($args);
  47. }
  48. /** Call this if you want full URL, not relative */
  49. function useAbsoluteURL(){
  50. /*
  51. Produced URL will contain absolute, rather than relative address:
  52. http://mysite:123/install/dir/my/page.html
  53. */
  54. $this->absolute=true;
  55. return $this;
  56. }
  57. /** [private] automatically called with 1st argument of api->url() */
  58. function setPage($page=null){
  59. // The following argument formats are supported:
  60. //
  61. // null = set current page
  62. // '.' = set current page
  63. // 'page' = sets webroot/page.html
  64. // './page' = set page relatively to current page
  65. // '..' = parent page
  66. // '../page' = page besides our own (foo/bar -> foo/page)
  67. // 'index' = properly points to the index page defined in API
  68. // '/admin/' = will not be converted
  69. $destination='';
  70. if(substr($page,-1)=='/'){
  71. return $this->setBaseURL(str_replace('//','/',$this->api->pm->base_path.$page));
  72. }
  73. if(is_null($page))$page='.';
  74. $path=explode('/',$page);
  75. foreach($path as $component){
  76. if($component=='')continue;
  77. if($component=='.' && $destination==''){
  78. if($this->api->page=='index')continue;
  79. $destination=str_replace('_','/',$this->api->page);
  80. continue;
  81. }
  82. if($component=='..'){
  83. if(!$destination)$destination=str_replace('_','/',$this->api->page);
  84. $tmp=explode('/',$destination);
  85. array_pop($tmp);
  86. $destination=join('/',$tmp);
  87. continue;
  88. }
  89. if($component=='index' && $destination==''){
  90. $destination=$this->api->index_page;
  91. continue;
  92. }
  93. $destination=$destination?$destination.'/'.$component:$component;
  94. }
  95. if($destination==='')$destination=$this->api->index_page;
  96. $this->page=$destination;
  97. return $this;
  98. }
  99. /** Set additional arguments */
  100. function set($argument,$value=null){
  101. if(!is_array($argument))$argument=array($argument=>$value);
  102. return $this->setArguments($argument);
  103. }
  104. /** Get value of an argument */
  105. function get($argument){
  106. return $this->arguments[$argument];
  107. }
  108. /** Set arguments to specified array */
  109. function setArguments($arguments=array()){
  110. // add additional arguments
  111. if(is_null($arguments))$arguments=array();
  112. if(!is_array($arguments)){
  113. throw new BaseException('Arguments must be always an array');
  114. }
  115. $this->arguments=$args=array_merge($this->arguments,$arguments);
  116. foreach($args as $arg=>$val){
  117. if(is_null($val))unset($this->arguments[$arg]);
  118. }
  119. return $this;
  120. }
  121. function __toString(){
  122. return $this->getURL();
  123. }
  124. function setURL($url){
  125. return $this->setBaseURL($url);
  126. }
  127. /** By default uses detected base_url, but you can use this to redefine */
  128. function setBaseURL($base){
  129. $this->base_url=$base;
  130. return $this;
  131. }
  132. function getBaseURL(){
  133. // Oherwise - calculate from detected values
  134. $url='';
  135. // add absolute if necessary
  136. if($this->absolute)$url.=$this->api->pm->base_url;
  137. // add base path
  138. $url.=$this->api->pm->base_path;
  139. return $url;
  140. }
  141. function getExtension(){
  142. return $this->extension;
  143. }
  144. function getArguments($url=null){
  145. $tmp=array();
  146. foreach($this->arguments as $key=>$value){
  147. if($value===false)continue;
  148. $tmp[]=$key.'='.urlencode($value);
  149. }
  150. $arguments='';
  151. if($tmp)$arguments=(strpos($url,'?')!==false?'&':'?').join('&',$tmp);
  152. return $arguments;
  153. }
  154. function getURL(){
  155. if($this->base_url)return $this->base_url.$this->getArguments($this->base_url);
  156. $url=$this->getBaseURL();
  157. if($this->page && $this->page!='index'){
  158. // add prefix if defined in config
  159. $url.=$this->api->getConfig('url_prefix','');
  160. $url.=$this->page;
  161. $url.=$this->getExtension();
  162. }
  163. $url.=$this->getArguments($url);
  164. return $url;
  165. }
  166. /** Returns html-encoded URL */
  167. function getHTMLURL(){
  168. return htmlentities($this->getURL());
  169. }
  170. }