/engine/_classes/class.clean_url.php

https://github.com/raphaelbastide/berta · PHP · 156 lines · 57 code · 19 blank · 80 comment · 6 complexity · 4dbf5e1e4b766b804c507422e4bc8635 MD5 · raw file

  1. <?php
  2. /* uzlabota: ernesto */
  3. /******
  4. ** CleanURL
  5. ** Make your URLs User & Google friendly
  6. ** Version 0.9
  7. ******
  8. ** Author: Huda M Elmatsani
  9. ** Email: justhuda ** netscape ** net
  10. **
  11. ** 2/Nov/2004
  12. ******
  13. ** Copyright (c) 2004 Huda M Elmatsani All rights reserved.
  14. ** This program is free for any purpose use.
  15. ********
  16. **
  17. ** The scope of this class is to change the way writing URL Query String like this:
  18. ** from
  19. ** news.php?id=120&page=2
  20. ** to
  21. ** news/120/2
  22. **
  23. ** edit your .htaccess and use directive RewriteRule
  24. ** Example:
  25. **
  26. ** RewriteEngine on
  27. ** RewriteRule ^news(\/.*)*$ /news.php
  28. **
  29. **
  30. ** To create clean URL:
  31. ** $clean->makeClean('news.php?id=120&page=2');
  32. ** results 'news/120/2';
  33. **
  34. ** To read clean URL
  35. ** $clean->parseURL();
  36. ** $clean->setRelative('relativeslash'); //relativeslash is variable name
  37. ** $clean->setParts('id','page');
  38. **
  39. ** What is relative slash?
  40. ** think this img tag on index.php file directory:
  41. ** <img src="images/logo.jpeg">
  42. ** if you access your image logo from deeper directory, should be like this:
  43. ** <img src="../../images/logo.jpeg">
  44. ** Clean URL class makes the URL deeper because of using slashes
  45. ** the solution is placing variable that contains 'relative slashes'.
  46. ** <img src="<?=$relativeslash?>images/logo.jpeg">
  47. **
  48. ** Note on setParts()
  49. ** $clean->setParts('id','page');
  50. ** this method will produce pairs of query string variables and values,
  51. ** using eval() function, webpage will read as:
  52. ** $id = 120;
  53. ** $page = 2;
  54. **
  55. **
  56. ** EXAMPLES:
  57. ** To create clean URL
  58. ** original: <a href="news.php?id=<?=$id?>&page=<?=$page?>">;
  59. ** convert: <a href="<? CleanURL::makeClean("news.php?id=$id&page=$page")?>">
  60. ** result on browser: <a href="news/120/2">;
  61. **
  62. ** To parse clean URL as above
  63. ** $clean = new CleanURL;
  64. ** $clean->parseURL();
  65. ** $clean->setRelative('relativeslash');
  66. ** $clean->setParts('id','page');
  67. **
  68. ************/
  69. class CleanURL {
  70. var $basename;
  71. var $uri;
  72. var $parts;
  73. var $slashes;
  74. function parseURL($urlStr = '') {
  75. /* grab URL query string and script name */
  76. if(!$urlStr) $urlStr = $_SERVER['REQUEST_URI'];
  77. $uri = strpos($urlStr, '?') !== false ? substr($urlStr, 0, strpos($urlStr, '?')) : $urlStr;
  78. $script = $_SERVER['SCRIPT_NAME'];
  79. /* get extension */
  80. $scriptArr = explode(".",$script);
  81. $ext = end($scriptArr);
  82. /* if extension is found in URL, eliminate it */
  83. if(strstr($uri,".")) {
  84. $arr_uri = explode('.', $uri);
  85. /* get last part */
  86. $last = end($arr_uri);
  87. if($last == $ext){
  88. array_pop($arr_uri);
  89. $uri = implode('.', $arr_uri);
  90. }
  91. }
  92. /* pick the name without extension */
  93. $basename = basename ($script, '.'.$ext);
  94. /* slicing query string */
  95. $temp = explode('/',$uri);
  96. $key = array_search($basename,$temp);
  97. $parts = array_slice ($temp, $key+1);
  98. $this->basename = $basename;
  99. $this->parts = $parts;
  100. $this->uri = $uri;
  101. }
  102. function setRelative($relativevar) {
  103. /* count the number of slash
  104. to define relative path */
  105. $numslash = count($this->parts);
  106. $slashes="";
  107. for($i=0;$i<$numslash;$i++){
  108. $slashes .= "../";
  109. }
  110. $this->slashes = $slashes;
  111. /* make relative path variable available for webpage */
  112. eval("\$GLOBALS['$relativevar'] = '$slashes';");
  113. }
  114. function getParts() {
  115. /* return array of sliced query string */
  116. return $this->parts;
  117. }
  118. function setParts() {
  119. /* pair off query string variable and query string value */
  120. $numargs = func_num_args();
  121. $arg_list = func_get_args();
  122. $urlparts = $this->getParts();
  123. for ($i = 0; $i < $numargs; $i++) {
  124. /* make them available for webpage */
  125. eval ('$GLOBALS["' . $arg_list[$i] . '"]= ' . (!empty($urlparts[$i]) ? "'$urlparts[$i]'" : 'false') . ';');
  126. }
  127. }
  128. function makeClean($stringurl) {
  129. /* convert normal URL query string to clean URL */
  130. $url=parse_url($stringurl);
  131. $strurl = ''; //basename($url['path'],".php");
  132. $qstring = parse_str($url['query'],$vars);
  133. while(list($k,$v) = each($vars)) $strurl .= "/".$v;
  134. return $strurl;
  135. }
  136. }
  137. ?>