/claroline/inc/lib/core/url.lib.php

https://github.com/TeamRocketScience/Claroline-TRS-Edition · PHP · 304 lines · 175 code · 40 blank · 89 comment · 22 complexity · 5a51b72053fb35f837fe5e9a574ef0c9 MD5 · raw file

  1. <?php // $Id: url.lib.php 12923 2011-03-03 14:23:57Z abourguignon $
  2. // vim: expandtab sw=4 ts=4 sts=4:
  3. /**
  4. * Url manipulation library
  5. *
  6. * @version 1.10 $Revision: 12923 $
  7. * @copyright (c) 2001-2011, Universite catholique de Louvain (UCL)
  8. * @author Claroline Team <info@claroline.net>
  9. * @author Frederic Minne <zefredz@claroline.net>
  10. * @license http://www.gnu.org/copyleft/gpl.html
  11. * GNU GENERAL PUBLIC LICENSE version 2 or later
  12. * @package kernel.core
  13. */
  14. /**
  15. * Class to manipulate Urls
  16. */
  17. class Url
  18. {
  19. protected $url = array(
  20. 'scheme' => '',
  21. 'host' => '',
  22. 'port' => '',
  23. 'user' => '',
  24. 'pass' => '',
  25. 'path' => '',
  26. 'query' => array(),
  27. 'fragment' => ''
  28. );
  29. /**
  30. * Constructor
  31. * @param string url base url (use PHP_SELF if missing)
  32. */
  33. public function __construct( $url = '' )
  34. {
  35. $url = empty($url)
  36. ? $_SERVER['PHP_SELF']
  37. : $url
  38. ;
  39. $url = htmlspecialchars_decode( $url );
  40. $urlArr = @parse_url( $url );
  41. $queryArr = array();
  42. if ( !empty($urlArr['query']) )
  43. {
  44. @parse_str($urlArr['query'], $queryArr );
  45. }
  46. unset ($urlArr['suery']);
  47. $this->url = array_merge( $this->url, $urlArr );
  48. $this->url['query'] = $queryArr;
  49. }
  50. public function __get( $name )
  51. {
  52. if ( isset( $this->url[$name] ) )
  53. {
  54. return $this->url[$name];
  55. }
  56. else
  57. {
  58. return null;
  59. }
  60. }
  61. public function __set( $name, $value )
  62. {
  63. if ( isset( $this->url[$name] ) )
  64. {
  65. $this->url[$name] = $value;
  66. }
  67. }
  68. /**
  69. * Relay Claroline current Url context in urls
  70. * @return $this
  71. */
  72. public function relayCurrentContext()
  73. {
  74. /* $context = Claro_Context::getCurrentUrlContext();
  75. if ( array_key_exists( 'cid', $context )
  76. && ! array_key_exists( 'cidReq', $context ) )
  77. {
  78. $context['cidReq'] = $context['cid'];
  79. unset( $context['cid'] );
  80. }
  81. if ( array_key_exists( 'gid', $context )
  82. && ! array_key_exists( 'gidReq', $context ) )
  83. {
  84. $context['gidReq'] = $context['gid'];
  85. unset( $context['gid'] );
  86. } */
  87. $this->addParamList( Claro_Context::getCurrentUrlContext() );
  88. return $this;
  89. }
  90. /**
  91. * Relay given Url context in urls
  92. * @param array context
  93. * @return $this
  94. */
  95. public function relayContext( $context )
  96. {
  97. $this->addParamList( $context );
  98. return $this;
  99. }
  100. /**
  101. * Add a list of parameters to the current url
  102. * @param array $paramList associative array of parameters name=>value
  103. * @param boolean $overwrite will overwrite the value of an existing
  104. * parameter if set to true
  105. * @return $this
  106. */
  107. public function addParamList( $paramList, $overwrite = false )
  108. {
  109. if ( !empty( $paramList ) && is_array( $paramList ) )
  110. {
  111. foreach ( $paramList as $name => $value )
  112. {
  113. if ( !$overwrite && !empty( $value ) )
  114. {
  115. $this->addParam( $name, $value );
  116. }
  117. elseif ( $overwrite )
  118. {
  119. $this->replaceParam( $name, $value, true );
  120. }
  121. }
  122. }
  123. return $this;
  124. }
  125. /**
  126. * Add one parameter to the current url
  127. * @param string $name parameter name
  128. * @param string $value parameter value
  129. * @return $this
  130. */
  131. public function addParam( $name, $value )
  132. {
  133. if ( !array_key_exists($name, $this->url['query'] ) )
  134. {
  135. $this->url['query'][$name] = $value;
  136. }
  137. return $this;
  138. }
  139. /**
  140. * Replace the value of the given parameter with the given value
  141. * @param string $name parameter name
  142. * @param string $value parameter value
  143. * @param boolean $addIfMissing add the parameter if missing (default false)
  144. * @return $this
  145. * @throws Exception if trying to modify a non existent parameter with
  146. * $addIfMissing set to false (default)
  147. */
  148. public function replaceParam( $name, $value, $addIfMissing = false )
  149. {
  150. if ( $addIfMissing || array_key_exists( $name, $this->url['query'] ) )
  151. {
  152. $this->addParam( $name, $value );
  153. return $this;
  154. }
  155. else
  156. {
  157. throw new Exception("Cannot replace parameter {$name} : not found");
  158. }
  159. }
  160. /**
  161. * Remove the given parameter
  162. * @param string $name parameter name
  163. * @param boolean $ignoreMissing if set to true, the method invokation
  164. * will ignore a missing parameter. If set to false (default) removing a
  165. * non existent parameter will generate an exception
  166. * @return $this
  167. * @throws Exception if trying to remove a non existent parameter with
  168. * $ignoreMissing set to false (default)
  169. */
  170. public function removeParam( $name, $ignoreMissing = false )
  171. {
  172. if ( array_key_exists( $name, $this->url['query'] ) )
  173. {
  174. unset( $this->url['query'] );
  175. return $this;
  176. }
  177. elseif ( $ignoreMissing)
  178. {
  179. return $this;
  180. }
  181. else
  182. {
  183. throw new Exception("Cannot remove parameter {$name} : not found");
  184. }
  185. }
  186. /**
  187. * Convert the current Url object to an URL string
  188. * @return string
  189. */
  190. public function toUrl()
  191. {
  192. $url = '';
  193. if ( !empty($this->url['scheme']) )
  194. {
  195. if ( $this->url['scheme'] != 'mailto' )
  196. {
  197. $url .= $this->url['scheme'] . '://';
  198. }
  199. else
  200. {
  201. $url .= $this->url['scheme'] . ':';
  202. }
  203. }
  204. if ( !empty( $this->url['user'] ) )
  205. {
  206. $url .= $this->url['user'];
  207. if ( !empty( $this->url['pass'] ) )
  208. {
  209. $url .= ":{$this->url['pass']}";
  210. }
  211. $url .= '@';
  212. }
  213. if ( !empty ( $this->url['host']))
  214. {
  215. $url .= $this->url['host'];
  216. }
  217. if ( !empty ( $this->url['port']))
  218. {
  219. $url .= ':'.$this->url['port'];
  220. }
  221. if ( !empty ( $this->url['path']))
  222. {
  223. $url .= $this->url['path'];
  224. }
  225. if ( !empty($this->url['query']) )
  226. {
  227. $url .= '?' . http_build_query( $this->url['query'] );
  228. }
  229. if ( !empty ( $this->url['fragment']))
  230. {
  231. $url .= '#' . $this->url['fragment'];
  232. }
  233. return $url;
  234. }
  235. /**
  236. * @since Claroline 1.9
  237. * @return string
  238. */
  239. public function __toString()
  240. {
  241. return $this->toUrl();
  242. }
  243. /**
  244. * Add current execution context to the given URL
  245. * @param string $url
  246. * @param array $context
  247. * @return string
  248. */
  249. public static function Contextualize( $url, $context = null )
  250. {
  251. $urlObj = new self($url);
  252. if ( empty( $context ) )
  253. {
  254. $urlObj->relayCurrentContext();
  255. }
  256. else
  257. {
  258. $urlObj->relayContext( $context );
  259. }
  260. return $urlObj->toUrl();
  261. }
  262. }