/protected/components/CmsApplication.php

https://github.com/pimax/picms · PHP · 120 lines · 88 code · 23 blank · 9 comment · 24 complexity · 210a2c53a57f0ffadd2d873bfb371d47 MD5 · raw file

  1. <?php
  2. class CmsApplication extends CWebApplication
  3. {
  4. protected $site = false;
  5. protected $page = false;
  6. public function processRequest()
  7. {
  8. if(is_array($this->catchAllRequest) && isset($this->catchAllRequest[0]))
  9. {
  10. $route=$this->catchAllRequest[0];
  11. foreach(array_splice($this->catchAllRequest,1) as $name=>$value)
  12. $_GET[$name]=$value;
  13. }
  14. else {
  15. $route=$this->getUrlManager()->parseUrl($this->getRequest());
  16. }
  17. //echo '<pre>', print_r($this->getRequest()->getUrl(), true), '</pre>';
  18. if ($this->getRequest()->getUrl() != '/' && mb_strrpos($this->getRequest()->getUrl(), "/", 0, "utf8") === mb_strlen($this->getRequest()->getUrl(), "utf8") - 1) {
  19. $this->getRequest()->redirect(mb_substr($this->getRequest()->getUrl(), 0, mb_strlen($this->getRequest()->getUrl(), "utf8") - 1, "utf8"), true, 301);
  20. }
  21. Yii::import('application.modules.main.*');
  22. Yii::import('application.modules.main.models.*');
  23. Yii::import('application.modules.main.components.*');
  24. Yii::import('application.modules.structure.*');
  25. Yii::import('application.modules.structure.models.*');
  26. Yii::import('application.modules.structure.components.*');
  27. $this->findSite();
  28. $this->findPage($route);
  29. if ($this->getPage()) {
  30. if ($this->getPage()->type_id == 1) {
  31. // simple page
  32. if ($this->getPage()->path == '/'.$route) {
  33. $sRoute = $this->getPage()->type->module->module_alias.'/'.$this->getPage()->type->controller_alias.'/'.$this->getPage()->type->action_alias;
  34. } else {
  35. $sRoute = $route;
  36. }
  37. } else {
  38. // other page types
  39. $sRoute = $this->getPage()->type->module->module_alias.'/'.$this->getPage()->type->controller_alias.'/'.$this->getPage()->type->action_alias;
  40. if ($this->getPage()->path != '/'.$route) {
  41. $_GET['path'] = str_replace($this->page->path.'/', "", '/'.$route);
  42. if (isset($_GET['id'])) {
  43. $_GET['path'] .= '/'.$_GET['id'];
  44. unset($_GET['id']);
  45. }
  46. }
  47. }
  48. } else {
  49. $this->page = new Page();
  50. //$this->findPage("/");
  51. //if ($this->getPage()->type_id == 1) {
  52. $sRoute = $route;
  53. //} else {
  54. // $sRoute = $this->getPage()->type->module->module_alias.'/'.$this->getPage()->type->controller_alias.'/'.$this->getPage()->type->action_alias;
  55. //}
  56. }
  57. $this->runController($sRoute);
  58. }
  59. public function getSite()
  60. {
  61. return $this->site;
  62. }
  63. public function getPage()
  64. {
  65. return $this->page;
  66. }
  67. protected function findSite()
  68. {
  69. if ($this->site = Site::model()->find("url = :url", array(':url'=> $this->getRequest()->getServerName())))
  70. {
  71. return true;
  72. }
  73. return false;
  74. }
  75. protected function findPage($route)
  76. {
  77. if ($route === "") {
  78. // main page
  79. $this->page = Page::model()->findByPk(1);
  80. return true;
  81. } else {
  82. $aRoute = explode("/", $route);
  83. if (count($aRoute) <= 10) {
  84. for ($i = 0; $i < count($aRoute); $i++) {
  85. $sUrl = '';
  86. for ($n = 0; $n < count($aRoute) - $i; $n++) {
  87. $sUrl .= '/'.$aRoute[$n];
  88. }
  89. if ($this->page = Page::model()->find("path = :path and site_id = :site_id", array(":path" => $sUrl, ":site_id" => $this->getSite()->id))) {
  90. return true;
  91. }
  92. }
  93. }
  94. }
  95. return false;
  96. }
  97. }