Action.class.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. /**
  12. * ThinkPHP Action控制器基类 抽象类
  13. * @category Think
  14. * @package Think
  15. * @subpackage Core
  16. * @author liu21st <liu21st@gmail.com>
  17. */
  18. abstract class Action {
  19. /**
  20. * 视图实例对象
  21. * @var view
  22. * @access protected
  23. */
  24. protected $view = null;
  25. /**
  26. * 当前控制器名称
  27. * @var name
  28. * @access protected
  29. */
  30. private $name = '';
  31. /**
  32. * 控制器参数
  33. * @var config
  34. * @access protected
  35. */
  36. protected $config = array();
  37. /**
  38. * 架构函数 取得模板对象实例
  39. * @access public
  40. */
  41. public function __construct() {
  42. tag('action_begin',$this->config);
  43. //实例化视图类
  44. $this->view = Think::instance('View');
  45. //控制器初始化
  46. if(method_exists($this,'_initialize'))
  47. $this->_initialize();
  48. }
  49. /**
  50. * 获取当前Action名称
  51. * @access protected
  52. */
  53. protected function getActionName() {
  54. if(empty($this->name)) {
  55. // 获取Action名称
  56. $this->name = substr(get_class($this),0,-6);
  57. }
  58. return $this->name;
  59. }
  60. /**
  61. * 是否AJAX请求
  62. * @access protected
  63. * @return bool
  64. */
  65. protected function isAjax() {
  66. if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) ) {
  67. if('xmlhttprequest' == strtolower($_SERVER['HTTP_X_REQUESTED_WITH']))
  68. return true;
  69. }
  70. if(!empty($_POST[C('VAR_AJAX_SUBMIT')]) || !empty($_GET[C('VAR_AJAX_SUBMIT')]))
  71. // 判断Ajax方式提交
  72. return true;
  73. return false;
  74. }
  75. /**
  76. * 模板显示 调用内置的模板引擎显示方法,
  77. * @access protected
  78. * @param string $templateFile 指定要调用的模板文件
  79. * 默认为空 由系统自动定位模板文件
  80. * @param string $charset 输出编码
  81. * @param string $contentType 输出类型
  82. * @param string $content 输出内容
  83. * @param string $prefix 模板缓存前缀
  84. * @return void
  85. */
  86. protected function display($templateFile='',$charset='',$contentType='',$content='',$prefix='') {
  87. $this->view->display($templateFile,$charset,$contentType,$content,$prefix);
  88. }
  89. /**
  90. * 输出内容文本可以包括Html 并支持内容解析
  91. * @access protected
  92. * @param string $content 输出内容
  93. * @param string $charset 模板输出字符集
  94. * @param string $contentType 输出类型
  95. * @param string $prefix 模板缓存前缀
  96. * @return mixed
  97. */
  98. protected function show($content,$charset='',$contentType='',$prefix='') {
  99. $this->view->display('',$charset,$contentType,$content,$prefix);
  100. }
  101. /**
  102. * 获取输出页面内容
  103. * 调用内置的模板引擎fetch方法,
  104. * @access protected
  105. * @param string $templateFile 指定要调用的模板文件
  106. * 默认为空 由系统自动定位模板文件
  107. * @param string $content 模板输出内容
  108. * @param string $prefix 模板缓存前缀*
  109. * @return string
  110. */
  111. protected function fetch($templateFile='',$content='',$prefix='') {
  112. return $this->view->fetch($templateFile,$content,$prefix);
  113. }
  114. /**
  115. * 创建静态页面
  116. * @access protected
  117. * @htmlfile 生成的静态文件名称
  118. * @htmlpath 生成的静态文件路径
  119. * @param string $templateFile 指定要调用的模板文件
  120. * 默认为空 由系统自动定位模板文件
  121. * @return string
  122. */
  123. protected function buildHtml($htmlfile='',$htmlpath='',$templateFile='') {
  124. $content = $this->fetch($templateFile);
  125. $htmlpath = !empty($htmlpath)?$htmlpath:HTML_PATH;
  126. $htmlfile = $htmlpath.$htmlfile.C('HTML_FILE_SUFFIX');
  127. if(!is_dir(dirname($htmlfile)))
  128. // 如果静态目录不存在 则创建
  129. mkdir(dirname($htmlfile),0755,true);
  130. if(false === file_put_contents($htmlfile,$content))
  131. throw_exception(L('_CACHE_WRITE_ERROR_').':'.$htmlfile);
  132. return $content;
  133. }
  134. /**
  135. * 模板主题设置
  136. * @access protected
  137. * @param string $theme 模版主题
  138. * @return Action
  139. */
  140. protected function theme($theme){
  141. $this->view->theme($theme);
  142. return $this;
  143. }
  144. /**
  145. * 模板变量赋值
  146. * @access protected
  147. * @param mixed $name 要显示的模板变量
  148. * @param mixed $value 变量的值
  149. * @return Action
  150. */
  151. protected function assign($name,$value='') {
  152. $this->view->assign($name,$value);
  153. return $this;
  154. }
  155. public function __set($name,$value) {
  156. $this->assign($name,$value);
  157. }
  158. /**
  159. * 取得模板显示变量的值
  160. * @access protected
  161. * @param string $name 模板显示变量
  162. * @return mixed
  163. */
  164. public function get($name='') {
  165. return $this->view->get($name);
  166. }
  167. public function __get($name) {
  168. return $this->get($name);
  169. }
  170. /**
  171. * 检测模板变量的值
  172. * @access public
  173. * @param string $name 名称
  174. * @return boolean
  175. */
  176. public function __isset($name) {
  177. return $this->get($name);
  178. }
  179. /**
  180. * 魔术方法 有不存在的操作的时候执行
  181. * @access public
  182. * @param string $method 方法名
  183. * @param array $args 参数
  184. * @return mixed
  185. */
  186. public function __call($method,$args) {
  187. if( 0 === strcasecmp($method,ACTION_NAME.C('ACTION_SUFFIX'))) {
  188. if(method_exists($this,'_empty')) {
  189. // 如果定义了_empty操作 则调用
  190. $this->_empty($method,$args);
  191. }elseif(file_exists_case($this->view->parseTemplate())){
  192. // 检查是否存在默认模版 如果有直接输出模版
  193. $this->display();
  194. }elseif(function_exists('__hack_action')) {
  195. // hack 方式定义扩展操作
  196. __hack_action();
  197. }else{
  198. _404(L('_ERROR_ACTION_').':'.ACTION_NAME);
  199. }
  200. }else{
  201. switch(strtolower($method)) {
  202. // 判断提交方式
  203. case 'ispost' :
  204. case 'isget' :
  205. case 'ishead' :
  206. case 'isdelete' :
  207. case 'isput' :
  208. return strtolower($_SERVER['REQUEST_METHOD']) == strtolower(substr($method,2));
  209. // 获取变量 支持过滤和默认值 调用方式 $this->_post($key,$filter,$default);
  210. case '_get' : $input =& $_GET;break;
  211. case '_post' : $input =& $_POST;break;
  212. case '_put' : parse_str(file_get_contents('php://input'), $input);break;
  213. case '_param' :
  214. switch($_SERVER['REQUEST_METHOD']) {
  215. case 'POST':
  216. $input = $_POST;
  217. break;
  218. case 'PUT':
  219. parse_str(file_get_contents('php://input'), $input);
  220. break;
  221. default:
  222. $input = $_GET;
  223. }
  224. if(C('VAR_URL_PARAMS') && isset($_GET[C('VAR_URL_PARAMS')])){
  225. $input = array_merge($input,$_GET[C('VAR_URL_PARAMS')]);
  226. }
  227. break;
  228. case '_request' : $input =& $_REQUEST; break;
  229. case '_session' : $input =& $_SESSION; break;
  230. case '_cookie' : $input =& $_COOKIE; break;
  231. case '_server' : $input =& $_SERVER; break;
  232. case '_globals' : $input =& $GLOBALS; break;
  233. default:
  234. throw_exception(__CLASS__.':'.$method.L('_METHOD_NOT_EXIST_'));
  235. }
  236. if(!isset($args[0])) { // 获取全局变量
  237. $data = $input; // 由VAR_FILTERS配置进行过滤
  238. }elseif(isset($input[$args[0]])) { // 取值操作
  239. $data = $input[$args[0]];
  240. $filters = isset($args[1])?$args[1]:C('DEFAULT_FILTER');
  241. if($filters) {// 2012/3/23 增加多方法过滤支持
  242. $filters = explode(',',$filters);
  243. foreach($filters as $filter){
  244. if(function_exists($filter)) {
  245. $data = is_array($data)?array_map($filter,$data):$filter($data); // 参数过滤
  246. }
  247. }
  248. }
  249. }else{ // 变量默认值
  250. $data = isset($args[2])?$args[2]:NULL;
  251. }
  252. Log::record('建议使用I方法替代'.$method,Log::NOTICE);
  253. return $data;
  254. }
  255. }
  256. /**
  257. * 操作错误跳转的快捷方法
  258. * @access protected
  259. * @param string $message 错误信息
  260. * @param string $jumpUrl 页面跳转地址
  261. * @param mixed $ajax 是否为Ajax方式 当数字时指定跳转时间
  262. * @return void
  263. */
  264. protected function error($message='',$jumpUrl='',$ajax=false) {
  265. $this->dispatchJump($message,0,$jumpUrl,$ajax);
  266. }
  267. /**
  268. * 操作成功跳转的快捷方法
  269. * @access protected
  270. * @param string $message 提示信息
  271. * @param string $jumpUrl 页面跳转地址
  272. * @param mixed $ajax 是否为Ajax方式 当数字时指定跳转时间
  273. * @return void
  274. */
  275. protected function success($message='',$jumpUrl='',$ajax=false) {
  276. $this->dispatchJump($message,1,$jumpUrl,$ajax);
  277. }
  278. /**
  279. * Ajax方式返回数据到客户端
  280. * @access protected
  281. * @param mixed $data 要返回的数据
  282. * @param String $type AJAX返回数据格式
  283. * @return void
  284. */
  285. protected function ajaxReturn($data,$type='') {
  286. if(func_num_args()>2) {// 兼容3.0之前用法
  287. $args = func_get_args();
  288. array_shift($args);
  289. $info = array();
  290. $info['data'] = $data;
  291. $info['info'] = array_shift($args);
  292. $info['status'] = array_shift($args);
  293. $data = $info;
  294. $type = $args?array_shift($args):'';
  295. }
  296. if(empty($type)) $type = C('DEFAULT_AJAX_RETURN');
  297. switch (strtoupper($type)){
  298. case 'JSON' :
  299. // 返回JSON数据格式到客户端 包含状态信息
  300. header('Content-Type:application/json; charset=utf-8');
  301. exit(json_encode($data));
  302. case 'XML' :
  303. // 返回xml格式数据
  304. header('Content-Type:text/xml; charset=utf-8');
  305. exit(xml_encode($data));
  306. case 'JSONP':
  307. // 返回JSON数据格式到客户端 包含状态信息
  308. header('Content-Type:application/json; charset=utf-8');
  309. $handler = isset($_GET[C('VAR_JSONP_HANDLER')]) ? $_GET[C('VAR_JSONP_HANDLER')] : C('DEFAULT_JSONP_HANDLER');
  310. exit($handler.'('.json_encode($data).');');
  311. case 'EVAL' :
  312. // 返回可执行的js脚本
  313. header('Content-Type:text/html; charset=utf-8');
  314. exit($data);
  315. default :
  316. // 用于扩展其他返回格式数据
  317. tag('ajax_return',$data);
  318. }
  319. }
  320. /**
  321. * Action跳转(URL重定向) 支持指定模块和延时跳转
  322. * @access protected
  323. * @param string $url 跳转的URL表达式
  324. * @param array $params 其它URL参数
  325. * @param integer $delay 延时跳转的时间 单位为秒
  326. * @param string $msg 跳转提示信息
  327. * @return void
  328. */
  329. protected function redirect($url,$params=array(),$delay=0,$msg='') {
  330. $url = U($url,$params);
  331. redirect($url,$delay,$msg);
  332. }
  333. /**
  334. * 默认跳转操作 支持错误导向和正确跳转
  335. * 调用模板显示 默认为public目录下面的success页面
  336. * 提示页面为可配置 支持模板标签
  337. * @param string $message 提示信息
  338. * @param Boolean $status 状态
  339. * @param string $jumpUrl 页面跳转地址
  340. * @param mixed $ajax 是否为Ajax方式 当数字时指定跳转时间
  341. * @access private
  342. * @return void
  343. */
  344. private function dispatchJump($message,$status=1,$jumpUrl='',$ajax=false) {
  345. if(true === $ajax || IS_AJAX) {// AJAX提交
  346. $data = is_array($ajax)?$ajax:array();
  347. $data['info'] = $message;
  348. $data['status'] = $status;
  349. $data['url'] = $jumpUrl;
  350. $this->ajaxReturn($data);
  351. }
  352. if(is_int($ajax)) $this->assign('waitSecond',$ajax);
  353. if(!empty($jumpUrl)) $this->assign('jumpUrl',$jumpUrl);
  354. // 提示标题
  355. $this->assign('msgTitle',$status? L('_OPERATION_SUCCESS_') : L('_OPERATION_FAIL_'));
  356. //如果设置了关闭窗口,则提示完毕后自动关闭窗口
  357. if($this->get('closeWin')) $this->assign('jumpUrl','javascript:window.close();');
  358. $this->assign('status',$status); // 状态
  359. //保证输出不受静态缓存影响
  360. C('HTML_CACHE_ON',false);
  361. if($status) { //发送成功信息
  362. $this->assign('message',$message);// 提示信息
  363. // 成功操作后默认停留1秒
  364. // if(!isset($this->waitSecond)) $this->assign('waitSecond','1');
  365. if(!isset($this->waitSecond)) $this->assign('waitSecond','5');
  366. // 默认操作成功自动返回操作前页面
  367. if(!isset($this->jumpUrl)) $this->assign("jumpUrl",$_SERVER["HTTP_REFERER"]);
  368. $this->display(C('TMPL_ACTION_SUCCESS'));
  369. }else{
  370. $this->assign('error',$message);// 提示信息
  371. //发生错误时候默认停留3秒
  372. if(!isset($this->waitSecond)) $this->assign('waitSecond','3');
  373. // 默认发生错误的话自动返回上页
  374. if(!isset($this->jumpUrl)) $this->assign('jumpUrl',"javascript:history.back(-1);");
  375. $this->display(C('TMPL_ACTION_ERROR'));
  376. // 中止执行 避免出错后继续执行
  377. exit ;
  378. }
  379. }
  380. /**
  381. * 析构方法
  382. * @access public
  383. */
  384. public function __destruct() {
  385. // 执行后续操作
  386. tag('action_end');
  387. }
  388. }