Router.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\Core\App\Controllers;
  3. class Router
  4. {
  5. use \ThurData\Servers\KerioEmail\Core\Traits\Lang;
  6. use \ThurData\Servers\KerioEmail\Core\Traits\Smarty;
  7. use \ThurData\Servers\KerioEmail\Core\Traits\OutputBuffer;
  8. use \ThurData\Servers\KerioEmail\Core\Traits\IsAdmin;
  9. use \ThurData\Servers\KerioEmail\Core\UI\Traits\RequestObjectHandler;
  10. const ADMIN = 'admin';
  11. const CLIENT = 'client';
  12. protected $controllerClass = null;
  13. protected $controllerMethod = null;
  14. public function __construct()
  15. {
  16. $this->isAdmin();
  17. $this->loadController();
  18. }
  19. /**
  20. * load routing params
  21. */
  22. public function loadController()
  23. {
  24. $this->getControllerClass();
  25. $this->getControllerMethod();
  26. }
  27. /**
  28. * @return string
  29. * class name of the controller
  30. */
  31. public function getControllerClass()
  32. {
  33. if ($this->controllerClass === null)
  34. {
  35. $this->controllerClass = '\ThurData\Servers\KerioEmail\App\Http\\' . ucfirst($this->getControllerType()) . '\\' . ucfirst($this->getController());
  36. }
  37. return $this->controllerClass;
  38. }
  39. /**
  40. * @return string
  41. * admin/client context type
  42. */
  43. public function getControllerType()
  44. {
  45. return $this->isAdminStatus ? self::ADMIN : self::CLIENT;
  46. }
  47. /**
  48. * @return string
  49. * get controller name
  50. */
  51. public function getController()
  52. {
  53. return filter_var($this->getRequestValue('mg-page', 'Home'), FILTER_SANITIZE_SPECIAL_CHARS);
  54. }
  55. /**
  56. * @return string
  57. * controller method name
  58. */
  59. public function getControllerMethod()
  60. {
  61. if ($this->controllerMethod === null)
  62. {
  63. $this->controllerMethod = $this->request->get('mg-action', 'index');
  64. }
  65. return $this->controllerMethod;
  66. }
  67. /**
  68. * @return bool
  69. * determines if controller can be called
  70. */
  71. public function isControllerCallable()
  72. {
  73. if (!class_exists($this->controllerClass))
  74. {
  75. logModuleCall(
  76. 'kerioEmail',
  77. __FUNCTION__,
  78. $this->controllerClass,
  79. 'Debug class_exists',
  80. class_exists($this->controllerClass)
  81. );
  82. return false;
  83. }
  84. if (!method_exists($this->controllerClass, $this->controllerMethod))
  85. {
  86. logModuleCall(
  87. 'kerioEmail',
  88. __FUNCTION__,
  89. $this->controllerMethod,
  90. 'Debug method_exists',
  91. method_exists($this->controllerClass, $this->controllerMethod)
  92. );
  93. return false;
  94. }
  95. if (!is_callable([$this->controllerClass, $this->controllerMethod]))
  96. {
  97. logModuleCall(
  98. 'kerioEmail',
  99. __FUNCTION__,
  100. [$this->controllerClass, $this->controllerMethod],
  101. 'Debug Controller',
  102. is_callable([$this->controllerClass, $this->controllerMethod])
  103. );
  104. return false;
  105. }
  106. return true;
  107. }
  108. }