Action.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /**
  3. * The Action class
  4. */
  5. abstract class LtAction
  6. {
  7. /**
  8. * The context object
  9. *
  10. * @var object
  11. */
  12. public $context;
  13. public $viewDir;
  14. public $viewTplDir;
  15. public $viewTplAutoCompile;
  16. /**
  17. * The dtd config for validator
  18. *
  19. * @var array
  20. */
  21. protected $dtds = array();
  22. /**
  23. * The Access Control List
  24. *
  25. * @var array
  26. */
  27. protected $acl;
  28. /**
  29. * The current user's roles
  30. *
  31. * @var array
  32. */
  33. protected $roles = array();
  34. /**
  35. * A flag to indicate if subclass call LtAction::__construct()
  36. *
  37. * @var boolean
  38. */
  39. protected $constructed = false;
  40. /**
  41. * The response type
  42. *
  43. * @var string
  44. */
  45. protected $responseType = "html";
  46. /**
  47. * Result properties
  48. */
  49. protected $code;
  50. protected $message;
  51. public $data;
  52. protected $view;
  53. protected $layout;
  54. /**
  55. * The constructor function, initialize the URI property
  56. */
  57. public function __construct()
  58. {
  59. $this->constructed = true;
  60. }
  61. public function executeChain()
  62. {
  63. if (!$this->constructed)
  64. {
  65. //DebugHelper::debug('SUBCLASS_NOT_CALL_PARENT_CONSTRUCTOR', array('class' => $actionClassName));
  66. trigger_error('SUBCLASS_NOT_CALL_PARENT_CONSTRUCTOR');
  67. }
  68. $this->afterConstruct();
  69. $validateResult = $this->validateInput();
  70. if (0 == $validateResult["error_total"])
  71. {
  72. if ($this->checkPrivilege())
  73. {
  74. $this->beforeExecute();
  75. $this->execute();
  76. }
  77. else
  78. {
  79. $this->code = 403;
  80. $this->message = "Access denied";
  81. }
  82. }
  83. else
  84. {
  85. $this->code = 407;
  86. $this->message = "Invalid input";
  87. $this->data['error_messages'] = $validateResult["error_messages"];
  88. }
  89. $this->writeResponse();
  90. }
  91. /**
  92. * Do something after subClass::__construct().
  93. */
  94. protected function afterConstruct()
  95. {
  96. }
  97. /**
  98. * Validate the data from client
  99. *
  100. * @return array
  101. */
  102. protected function validateInput()
  103. {
  104. $validateResult = array("error_total" => 0, "error_messages" => array());
  105. if (!empty($this->dtds) && class_exists('LtValidator'))
  106. {
  107. $validator = new LtValidator;
  108. $validator->init();
  109. foreach ($this->dtds as $variable => $dtd)
  110. {
  111. $from = isset($dtd->from) ? $dtd->from : 'request';
  112. foreach ($dtd->rules as $ruleKey => $ruleValue)
  113. {
  114. if ($ruleValue instanceof ConfigExpression)
  115. {
  116. eval('$_ruleValue = ' . $ruleValue->__toString());
  117. $dtd->rules[$ruleKey] = $_ruleValue;
  118. }
  119. }
  120. $error_messages = $validator->validate($this->context->$from($variable), $dtd);
  121. if (!empty($error_messages))
  122. {
  123. $validateResult['error_total'] ++;
  124. $validateResult['error_messages'][$variable] = $error_messages;
  125. }
  126. }
  127. }
  128. return $validateResult;
  129. }
  130. /**
  131. * Check if current user have privilege to do this
  132. *
  133. * @return boolen
  134. */
  135. protected function checkPrivilege()
  136. {
  137. $allow = true;
  138. if (!empty($this->roles) && class_exists('LtRbac'))
  139. {
  140. $module = $this->context->uri["module"];
  141. $action = $this->context->uri["action"];
  142. $roles = array_merge(array("*"), $this->roles);
  143. $rbac = new LtRbac();
  144. $rbac->init();
  145. $allow = $rbac->checkAcl($roles, "$module/$action");
  146. }
  147. return $allow;
  148. }
  149. /**
  150. * Do something before subClass::execute().
  151. */
  152. protected function beforeExecute()
  153. {
  154. }
  155. protected function execute()
  156. {
  157. }
  158. protected function writeResponse()
  159. {
  160. switch ($this->responseType)
  161. {
  162. case 'json':
  163. echo json_encode(array("code" => $this->code,
  164. "message" => $this->message,
  165. "data" => $this->data
  166. ));
  167. exit; //
  168. break;
  169. case 'tpl':
  170. if (null === $this->view)
  171. {
  172. $this->view = new LtTemplateView;
  173. }
  174. $this->view->component = false; // 是否组件
  175. $this->view->context = $this->context;
  176. $this->view->code = $this->code;
  177. $this->view->message = $this->message;
  178. $this->view->data = $this->data;
  179. $this->view->layoutDir = $this->viewDir . "layout/";
  180. $this->view->layout = $this->layout;
  181. $this->view->templateDir = $this->viewDir;
  182. $this->view->compiledDir = $this->viewTplDir;
  183. $this->view->autoCompile = $this->viewTplAutoCompile;
  184. if (empty($this->template))
  185. {
  186. $this->template = $this->context->uri["module"] . "-" . $this->context->uri["action"];
  187. }
  188. $this->view->template = $this->template;
  189. $this->view->render();
  190. break;
  191. case 'html':
  192. case 'wml':
  193. default:
  194. if (null === $this->view)
  195. {
  196. $this->view = new LtView;
  197. }
  198. $this->view->context = $this->context;
  199. $this->view->code = $this->code;
  200. $this->view->message = $this->message;
  201. $this->view->data = $this->data;
  202. $this->view->layoutDir = $this->viewDir . "layout/";
  203. $this->view->layout = $this->layout;
  204. $this->view->templateDir = $this->viewDir;
  205. if (empty($this->template))
  206. {
  207. $this->template = $this->context->uri["module"] . "-" . $this->context->uri["action"];
  208. }
  209. $this->view->template = $this->template;
  210. $this->view->render();
  211. break;
  212. }
  213. }
  214. }