Action.class.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. <?php
  2. // +--------------------------------------------------------------------------
  3. // | Senthot [ DEVELOPED BY ME ]
  4. // +--------------------------------------------------------------------------
  5. // | Copyright (c) 2005-2013 http://www.senthot.com All rights reserved.
  6. // | License ( http://www.apache.org/licenses/LICENSE-2.0 )
  7. // | Author: ms134n ( [email protected] )
  8. // +--------------------------------------------------------------------------
  9. /**
  10. * Senthot Action Controller base class Abstract class
  11. * @category Sen
  12. * @package Sen
  13. * @subpackage Core
  14. * @author ms134n <[email protected]>
  15. */
  16. abstract class Action {
  17. /**
  18. * View instance
  19. * @var view
  20. * @access protected
  21. */
  22. protected $view = null;
  23. /**
  24. * Current controller name
  25. * @var name
  26. * @access protected
  27. */
  28. private $name = '';
  29. /**
  30. * Template Variables
  31. * @var tVar
  32. * @access protected
  33. */
  34. protected $tVar = array();
  35. /**
  36. * Controller parameters
  37. * @var config
  38. * @access protected
  39. */
  40. protected $config = array();
  41. /**
  42. * Architecture function Get a template object instance
  43. * @access public
  44. */
  45. public function __construct() {
  46. tag('action_begin',$this->config);
  47. //Controller initialization
  48. if(method_exists($this,'_initialize'))
  49. $this->_initialize();
  50. }
  51. /**
  52. * Gets the name of the current Action
  53. * @access protected
  54. */
  55. protected function getActionName() {
  56. if(empty($this->name)) {
  57. // Get Action Name
  58. $this->name = substr(get_class($this),0,-6);
  59. }
  60. return $this->name;
  61. }
  62. /**
  63. * Whether AJAX request
  64. * @access protected
  65. * @return bool
  66. */
  67. protected function isAjax() {
  68. if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) ) {
  69. if('xmlhttprequest' == strtolower($_SERVER['HTTP_X_REQUESTED_WITH']))
  70. return true;
  71. }
  72. if(!empty($_POST[C('VAR_AJAX_SUBMIT')]) || !empty($_GET[C('VAR_AJAX_SUBMIT')]))
  73. // Judgment Ajax submission
  74. return true;
  75. return false;
  76. }
  77. /**
  78. * Template Display Invoke the built-in template engine display method
  79. * @access protected
  80. * @param string $templateFile Specifies the template file to be invoked
  81. * The default is empty By the system automatically locates the template file
  82. * @param string $charset Output Coding
  83. * @param string $contentType Output Type
  84. * @param string $content Output
  85. * @param string $prefix Template cache prefix
  86. * @return void
  87. */
  88. protected function display($templateFile='',$charset='',$contentType='',$content='',$prefix='') {
  89. $this->initView();
  90. $this->view->display($templateFile,$charset,$contentType,$content,$prefix);
  91. }
  92. /**
  93. * Html output text can include Parse the content and Support
  94. * @access protected
  95. * @param string $content Output
  96. * @param string $charset Template output character set
  97. * @param string $contentType Output Type
  98. * @param string $prefix Template cache prefix
  99. * @return mixed
  100. */
  101. protected function show($content,$charset='',$contentType='',$prefix='') {
  102. $this->initView();
  103. $this->view->display('',$charset,$contentType,$content,$prefix);
  104. }
  105. /**
  106. * Get the output page content
  107. * Invoke the built-in template engine fetch method
  108. * @access protected
  109. * @param string $templateFile Specifies the template file to be invoked
  110. * The default is empty By the system automatically locates the template file
  111. * @param string $content Template output
  112. * @param string $prefix Template cache prefix*
  113. * @return string
  114. */
  115. protected function fetch($templateFile='',$content='',$prefix='') {
  116. $this->initView();
  117. return $this->view->fetch($templateFile,$content,$prefix);
  118. }
  119. /**
  120. * Initialize the view
  121. * @access private
  122. * @return void
  123. */
  124. private function initView(){
  125. //View class is instantiated
  126. if(!$this->view) $this->view = Sen::instance('View');
  127. // Template variables by value
  128. if($this->tVar) $this->view->assign($this->tVar);
  129. }
  130. /**
  131. * Create static pages
  132. * @access protected
  133. * @htmlfile Generated static file names
  134. * @htmlpath Generated static file path
  135. * @param string $templateFile Specifies the template file to be invoked
  136. * The default is empty By the system automatically locates the template file
  137. * @return string
  138. */
  139. protected function buildHtml($htmlfile='',$htmlpath='',$templateFile='') {
  140. $content = $this->fetch($templateFile);
  141. $htmlpath = !empty($htmlpath)?$htmlpath:HTML_PATH;
  142. $htmlfile = $htmlpath.$htmlfile.C('HTML_FILE_SUFFIX');
  143. if(!is_dir(dirname($htmlfile)))
  144. // If the static directory does not exist Is created
  145. mkdir(dirname($htmlfile),0755,true);
  146. if(false === file_put_contents($htmlfile,$content))
  147. throw_exception(L('_CACHE_WRITE_ERROR_').':'.$htmlfile);
  148. return $content;
  149. }
  150. /**
  151. * Template variable assignment
  152. * @access protected
  153. * @param mixed $name To display template variables
  154. * @param mixed $value Variable
  155. * @return void
  156. */
  157. protected function assign($name,$value='') {
  158. if(is_array($name)) {
  159. $this->tVar = array_merge($this->tVar,$name);
  160. }else {
  161. $this->tVar[$name] = $value;
  162. }
  163. }
  164. public function __set($name,$value) {
  165. $this->assign($name,$value);
  166. }
  167. /**
  168. * Get template displays the values of variables
  169. * @access protected
  170. * @param string $name Template display variable
  171. * @return mixed
  172. */
  173. public function get($name='') {
  174. if('' === $name) {
  175. return $this->tVar;
  176. }
  177. return isset($this->tVar[$name])?$this->tVar[$name]:false;
  178. }
  179. public function __get($name) {
  180. return $this->get($name);
  181. }
  182. /**
  183. * Detection template variable value
  184. * @access public
  185. * @param string $name Name
  186. * @return boolean
  187. */
  188. public function __isset($name) {
  189. return isset($this->tVar[$name]);
  190. }
  191. /**
  192. * Magic Methods There does not exist when the operation performed
  193. * @access public
  194. * @param string $method Method name
  195. * @param array $args Parameter
  196. * @return mixed
  197. */
  198. public function __call($method,$args) {
  199. if( 0 === strcasecmp($method,ACTION_NAME.C('ACTION_SUFFIX'))) {
  200. if(method_exists($this,'_empty')) {
  201. // If you define _empty operation the call
  202. $this->_empty($method,$args);
  203. }elseif(file_exists_case(C('TEMPLATE_NAME'))){
  204. // Check if there is a default template If there is a direct output template
  205. $this->display();
  206. }elseif(function_exists('__hack_action')) {
  207. // hack Define the extended operation
  208. __hack_action();
  209. }else{
  210. _404(L('_ERROR_ACTION_').':'.ACTION_NAME);
  211. }
  212. }else{
  213. switch(strtolower($method)) {
  214. // Judgment submission
  215. case 'ispost' :
  216. case 'isget' :
  217. case 'ishead' :
  218. case 'isdelete' :
  219. case 'isput' :
  220. return strtolower($_SERVER['REQUEST_METHOD']) == strtolower(substr($method,2));
  221. // Get Variables Support filtering and default values Invocation $this->_post($key,$filter,$default);
  222. case '_get' : $input =& $_GET;break;
  223. case '_post' : $input =& $_POST;break;
  224. case '_put' : parse_str(file_get_contents('php://input'), $input);break;
  225. case '_param' :
  226. switch($_SERVER['REQUEST_METHOD']) {
  227. case 'POST':
  228. $input = $_POST;
  229. break;
  230. case 'PUT':
  231. parse_str(file_get_contents('php://input'), $input);
  232. break;
  233. default:
  234. $input = $_GET;
  235. }
  236. if(C('VAR_URL_PARAMS')){
  237. $params = $_GET[C('VAR_URL_PARAMS')];
  238. $input = array_merge($input,$params);
  239. }
  240. break;
  241. case '_request' : $input =& $_REQUEST; break;
  242. case '_session' : $input =& $_SESSION; break;
  243. case '_cookie' : $input =& $_COOKIE; break;
  244. case '_server' : $input =& $_SERVER; break;
  245. case '_globals' : $input =& $GLOBALS; break;
  246. default:
  247. throw_exception(__CLASS__.':'.$method.L('_METHOD_NOT_EXIST_'));
  248. }
  249. if(!isset($args[0])) { // Access to global variables
  250. $data = $input; // Filtered by the VAR_FILTERS configuration
  251. }elseif(isset($input[$args[0]])) { // Value Operation
  252. $data = $input[$args[0]];
  253. $filters = isset($args[1])?$args[1]:C('DEFAULT_FILTER');
  254. if($filters) {// 2012/3/23 Increase the number of ways filtration Support
  255. $filters = explode(',',$filters);
  256. foreach($filters as $filter){
  257. if(function_exists($filter)) {
  258. $data = is_array($data)?array_map($filter,$data):$filter($data); // Parameter filter
  259. }
  260. }
  261. }
  262. }else{ // Variable Default
  263. $data = isset($args[2])?$args[2]:NULL;
  264. }
  265. return $data;
  266. }
  267. }
  268. /**
  269. * A quick way to jump operator error
  270. * @access protected
  271. * @param string $message Error Messages
  272. * @param string $jumpUrl Page jump address
  273. * @param mixed $ajax Whether the Ajax way Jump designated time when the digital
  274. * @return void
  275. */
  276. protected function error($message,$jumpUrl='',$ajax=false) {
  277. $this->dispatchJump($message,0,$jumpUrl,$ajax);
  278. }
  279. /**
  280. * Successful operation of a quick way to jump
  281. * @access protected
  282. * @param string $message Tips
  283. * @param string $jumpUrl Page jump address
  284. * @param mixed $ajax Whether the Ajax way Jump designated time when the digital
  285. * @return void
  286. */
  287. protected function success($message,$jumpUrl='',$ajax=false) {
  288. $this->dispatchJump($message,1,$jumpUrl,$ajax);
  289. }
  290. /**
  291. * Ajax way to return data to the client
  292. * @access protected
  293. * @param mixed $data Data to be returned
  294. * @param String $type AJAX return data format
  295. * @return void
  296. */
  297. protected function ajaxReturn($data,$type='') {
  298. if(func_num_args()>2) {// Compatible 2.0 before use
  299. $args = func_get_args();
  300. array_shift($args);
  301. $info = array();
  302. $info['data'] = $data;
  303. $info['info'] = array_shift($args);
  304. $info['status'] = array_shift($args);
  305. $data = $info;
  306. $type = $args?array_shift($args):'';
  307. }
  308. if(empty($type)) $type = C('DEFAULT_AJAX_RETURN');
  309. switch (strtoupper($type)){
  310. case 'JSON' :
  311. // Return JSON data format to the client Contains status information
  312. header('Content-Type:application/json; charset=utf-8');
  313. exit(json_encode($data));
  314. case 'XML' :
  315. // Back xml format data
  316. header('Content-Type:text/xml; charset=utf-8');
  317. exit(xml_encode($data));
  318. case 'JSONP':
  319. // Return JSON data format to the client Contains status information
  320. header('Content-Type:application/json; charset=utf-8');
  321. $handler = isset($_GET[C('VAR_JSONP_HANDLER')]) ? $_GET[C('VAR_JSONP_HANDLER')] : C('DEFAULT_JSONP_HANDLER');
  322. exit($handler.'('.json_encode($data).');');
  323. case 'EVAL' :
  324. // Back js script executable
  325. header('Content-Type:text/html; charset=utf-8');
  326. exit($data);
  327. default :
  328. // Back to format data for the expansion of other
  329. tag('ajax_return',$data);
  330. }
  331. }
  332. /**
  333. * Action Jump(URL Redirection) Support jumps specified module and delay
  334. * @access protected
  335. * @param string $url Jump to URL expression
  336. * @param array $params Other URL parameters
  337. * @param integer $delay The time delay jump seconds
  338. * @param string $msg Jump message
  339. * @return void
  340. */
  341. protected function redirect($url,$params=array(),$delay=0,$msg='') {
  342. $url = U($url,$params);
  343. redirect($url,$delay,$msg);
  344. }
  345. /**
  346. * Default jump operations Support orientation and correct errors Jump
  347. * Call template display The default directory for the public following success page
  348. * Prompt page as configurable Support Template Tags
  349. * @param string $message Tips
  350. * @param Boolean $status State
  351. * @param string $jumpUrl Page jump address
  352. * @param mixed $ajax Whether the Ajax way Jump designated time when the digital
  353. * @access private
  354. * @return void
  355. */
  356. private function dispatchJump($message,$status=1,$jumpUrl='',$ajax=false) {
  357. if(true === $ajax || IS_AJAX) {// AJAX submit
  358. $data = is_array($ajax)?$ajax:array();
  359. $data['info'] = $message;
  360. $data['status'] = $status;
  361. $data['url'] = $jumpUrl;
  362. $this->ajaxReturn($data);
  363. }
  364. if(is_int($ajax)) $this->assign('waitSecond',$ajax);
  365. if(!empty($jumpUrl)) $this->assign('jumpUrl',$jumpUrl);
  366. // Tips title
  367. $this->assign('msgTitle',$status? L('_OPERATION_SUCCESS_') : L('_OPERATION_FAIL_'));
  368. //If you set up close the window, you are prompted to close the window automatically after
  369. if($this->get('closeWin')) $this->assign('jumpUrl','javascript:window.close();');
  370. $this->assign('status',$status); // State
  371. //Ensure that the output is not affected static cache
  372. C('HTML_CACHE_ON',false);
  373. if($status) { //Information sent successfully
  374. $this->assign('message',$message);// Tips
  375. // After the successful operation of the default stay one second
  376. if(!isset($this->waitSecond)) $this->assign('waitSecond','1');
  377. // Automatically returns to the default operation is successful before the operation page
  378. if(!isset($this->jumpUrl)) $this->assign("jumpUrl",$_SERVER["HTTP_REFERER"]);
  379. $this->display(C('TMPL_ACTION_SUCCESS'));
  380. }else{
  381. $this->assign('error',$message);// Tips
  382. //An error occurred when the default stay 3 seconds
  383. if(!isset($this->waitSecond)) $this->assign('waitSecond','3');
  384. // Default if an error occurs automatically Back
  385. if(!isset($this->jumpUrl)) $this->assign('jumpUrl',"javascript:history.back(-1);");
  386. $this->display(C('TMPL_ACTION_ERROR'));
  387. // Suspend the enforcement Avoid mistakes continue
  388. exit ;
  389. }
  390. }
  391. /**
  392. * Destructor
  393. * @access public
  394. */
  395. public function __destruct() {
  396. // Save the log
  397. if(C('LOG_RECORD')) Log::save();
  398. // Subsequent operations
  399. tag('action_end');
  400. }
  401. }