Url.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. if(!defined('APP_KEY')) { exit('Access Denied'); }
  3. class Helper_Url implements Suco_Helper_Interface
  4. {
  5. protected $_url;
  6. protected $_route;
  7. public static function callback($args)
  8. {
  9. return @new self($args[0], $args[1]);
  10. }
  11. public function __construct($url = null, $route = null)
  12. {
  13. if ((stristr($url, '/') || !stristr($url, '='))
  14. && !stristr($url, 'http://')
  15. && !stristr($url, 'https://')
  16. && !stristr($url, '.html')
  17. && substr($url, 0, 1) != '&') {
  18. $url = trim($url, '/');
  19. list($p, $q) = explode('?', $url);
  20. $arr = explode('/', $p);
  21. if ($arr[0] == '.') {
  22. $query = array(
  23. 'action' => (string)$arr[1]
  24. );
  25. } elseif (Suco_Application::instance()->getDispatcher()->isModule($arr[0])) {
  26. $query = array(
  27. 'module' => (string)$arr[0],
  28. 'controller' => (string)$arr[1],
  29. 'action' => (string)$arr[2]
  30. );
  31. } else {
  32. $query = array(
  33. 'controller' => (string)$arr[0],
  34. 'action' => (string)$arr[1]
  35. );
  36. }
  37. $url = http_build_query($query);
  38. if ($q) $url.='&'.$q;
  39. }
  40. $this->_url = $url;
  41. $this->_route = $route;
  42. }
  43. public function encode($param)
  44. {
  45. return Suco_Controller_Router_Route_Abstract::encode($param);
  46. }
  47. public function decode($param)
  48. {
  49. return Suco_Controller_Router_Route_Abstract::decode($param);
  50. }
  51. public function __toString()
  52. {
  53. if (!$this->_url) { return ''; }
  54. list($url, $author) = explode('#', $this->_url);
  55. return Suco_Application::instance()
  56. ->getRouter()
  57. ->reverse($url, $this->_route) . ($author ? '#'.$author : '');
  58. }
  59. }