PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/Extend/Mode/Rest/Action.class.php

http://thinkphp.googlecode.com/
PHP | 339 lines | 168 code | 17 blank | 154 comment | 27 complexity | a2f46f5c30b7437777deb5d3b9188871 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  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. // $Id: Action.class.php 2832 2012-03-21 01:05:36Z huangdijia $
  12. /**
  13. +------------------------------------------------------------------------------
  14. * ThinkPHP RESTFul ????? ???
  15. +------------------------------------------------------------------------------
  16. * @category Think
  17. * @package Think
  18. * @subpackage Core
  19. * @author liu21st <liu21st@gmail.com>
  20. * @version $Id: Action.class.php 2832 2012-03-21 01:05:36Z huangdijia $
  21. +------------------------------------------------------------------------------
  22. */
  23. abstract class Action {
  24. // ??Action??
  25. private $name = '';
  26. // ????
  27. protected $view = null;
  28. protected $_method = ''; // ??????
  29. protected $_type = ''; // ??????
  30. // ????
  31. protected $_types = array();
  32. /**
  33. +----------------------------------------------------------
  34. * ???? ????????
  35. +----------------------------------------------------------
  36. * @access public
  37. +----------------------------------------------------------
  38. */
  39. public function __construct() {
  40. //??????
  41. $this->view = Think::instance('View');
  42. defined('__EXT__') or define('__EXT__','');
  43. if(''== __EXT__ || false === stripos(C('REST_CONTENT_TYPE_LIST'),__EXT__)) {
  44. // ???????????? ??????????
  45. $this->_type = C('REST_DEFAULT_TYPE');
  46. }else{
  47. $this->_type = __EXT__;
  48. }
  49. // ??????
  50. $method = strtolower($_SERVER['REQUEST_METHOD']);
  51. if(false === stripos(C('REST_METHOD_LIST'),$method)) {
  52. // ?????? ????????
  53. $method = C('REST_DEFAULT_METHOD');
  54. }
  55. $this->_method = $method;
  56. // ?????????
  57. $this->_types = C('REST_OUTPUT_TYPE');
  58. //??????
  59. if(method_exists($this,'_initialize'))
  60. $this->_initialize();
  61. }
  62. /**
  63. +----------------------------------------------------------
  64. * ????Action??
  65. +----------------------------------------------------------
  66. * @access protected
  67. +----------------------------------------------------------
  68. */
  69. protected function getActionName() {
  70. if(empty($this->name)) {
  71. // ??Action??
  72. $this->name = substr(get_class($this),0,-6);
  73. }
  74. return $this->name;
  75. }
  76. /**
  77. +----------------------------------------------------------
  78. * ??AJAX??
  79. +----------------------------------------------------------
  80. * @access protected
  81. +----------------------------------------------------------
  82. * @return bool
  83. +----------------------------------------------------------
  84. */
  85. protected function isAjax() {
  86. if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) ) {
  87. if('xmlhttprequest' == strtolower($_SERVER['HTTP_X_REQUESTED_WITH']))
  88. return true;
  89. }
  90. if(!empty($_POST[C('VAR_AJAX_SUBMIT')]) || !empty($_GET[C('VAR_AJAX_SUBMIT')]))
  91. // ??Ajax????
  92. return true;
  93. return false;
  94. }
  95. /**
  96. +----------------------------------------------------------
  97. * ???? ????????????
  98. +----------------------------------------------------------
  99. * @access public
  100. +----------------------------------------------------------
  101. * @param string $method ???
  102. * @param array $args ??
  103. +----------------------------------------------------------
  104. * @return mixed
  105. +----------------------------------------------------------
  106. */
  107. public function __call($method,$args) {
  108. if( 0 === strcasecmp($method,ACTION_NAME)) {
  109. if(method_exists($this,$method.'_'.$this->_method.'_'.$this->_type)) { // RESTFul????
  110. $fun = $method.'_'.$this->_method.'_'.$this->_type;
  111. $this->$fun();
  112. }elseif($this->_method == C('REST_DEFAULT_METHOD') && method_exists($this,$method.'_'.$this->_type) ){
  113. $fun = $method.'_'.$this->_type;
  114. $this->$fun();
  115. }elseif($this->_type == C('REST_DEFAULT_TYPE') && method_exists($this,$method.'_'.$this->_method) ){
  116. $fun = $method.'_'.$this->_method;
  117. $this->$fun();
  118. }elseif(method_exists($this,'_empty')) {
  119. // ?????_empty?? ???
  120. $this->_empty($method,$args);
  121. }elseif(file_exists_case(C('TMPL_FILE_NAME'))){
  122. // ?????????? ?????????
  123. $this->display();
  124. }else{
  125. // ????
  126. throw_exception(L('_ERROR_ACTION_').ACTION_NAME);
  127. }
  128. }else{
  129. switch(strtolower($method)) {
  130. // ???? ???????? ???? $this->_post($key,$filter,$default);
  131. case '_get': $input =& $_GET;break;
  132. case '_post':$input =& $_POST;break;
  133. case '_put':
  134. case '_delete':parse_str(file_get_contents('php://input'), $input);break;
  135. case '_request': $input =& $_REQUEST;break;
  136. case '_session': $input =& $_SESSION;break;
  137. case '_cookie': $input =& $_COOKIE;break;
  138. case '_server': $input =& $_SERVER;break;
  139. default:
  140. throw_exception(__CLASS__.':'.$method.L('_METHOD_NOT_EXIST_'));
  141. }
  142. if(isset($input[$args[0]])) { // ????
  143. $data = $input[$args[0]];
  144. $fun = $args[1]?$args[1]:C('DEFAULT_FILTER');
  145. $data = $fun($data); // ????
  146. }else{ // ?????
  147. $data = isset($args[2])?$args[2]:NULL;
  148. }
  149. return $data;
  150. }
  151. }
  152. /**
  153. +----------------------------------------------------------
  154. * ????
  155. * ??????????????
  156. +----------------------------------------------------------
  157. * @access protected
  158. +----------------------------------------------------------
  159. * @param string $templateFile ??????????
  160. * ???? ???????????
  161. * @param string $charset ????
  162. * @param string $contentType ????
  163. +----------------------------------------------------------
  164. * @return void
  165. +----------------------------------------------------------
  166. */
  167. protected function display($templateFile='',$charset='',$contentType='') {
  168. $this->view->display($templateFile,$charset,$contentType);
  169. }
  170. /**
  171. +----------------------------------------------------------
  172. * ??????
  173. +----------------------------------------------------------
  174. * @access protected
  175. +----------------------------------------------------------
  176. * @param mixed $name ????????
  177. * @param mixed $value ????
  178. +----------------------------------------------------------
  179. * @return void
  180. +----------------------------------------------------------
  181. */
  182. protected function assign($name,$value='') {
  183. $this->view->assign($name,$value);
  184. }
  185. public function __set($name,$value) {
  186. $this->view->assign($name,$value);
  187. }
  188. /**
  189. +----------------------------------------------------------
  190. * ???????CONTENT_TYPE???
  191. +----------------------------------------------------------
  192. * @access public
  193. +----------------------------------------------------------
  194. * @param string $type content_type ????????
  195. * @param string $charset ??????
  196. +----------------------------------------------------------
  197. * @return void
  198. +----------------------------------------------------------
  199. */
  200. public function setContentType($type, $charset=''){
  201. if(headers_sent()) return;
  202. if(empty($charset)) $charset = C('DEFAULT_CHARSET');
  203. $type = strtolower($type);
  204. if(isset($this->_types[$type])) //??content_type
  205. header('Content-Type: '.$this->_types[$type].'; charset='.$charset);
  206. }
  207. /**
  208. +----------------------------------------------------------
  209. * ??????
  210. +----------------------------------------------------------
  211. * @access protected
  212. +----------------------------------------------------------
  213. * @param mixed $data ??????
  214. * @param String $type ???? JSON XML
  215. * @param integer $code HTTP??
  216. +----------------------------------------------------------
  217. * @return void
  218. +----------------------------------------------------------
  219. */
  220. protected function response($data,$type='',$code=200) {
  221. $this->sendHttpStatus($code);
  222. exit($this->encodeData($data,strtolower($type)));
  223. }
  224. /**
  225. +----------------------------------------------------------
  226. * ????
  227. +----------------------------------------------------------
  228. * @access protected
  229. +----------------------------------------------------------
  230. * @param mixed $data ??????
  231. * @param String $type ???? JSON XML
  232. +----------------------------------------------------------
  233. * @return void
  234. +----------------------------------------------------------
  235. */
  236. protected function encodeData($data,$type='') {
  237. if(empty($data)) return '';
  238. if(empty($type)) $type = $this->_type;
  239. if('json' == $type) {
  240. // ??JSON???????? ??????
  241. $data = json_encode($data);
  242. }elseif('xml' == $type){
  243. // ??xml????
  244. $data = xml_encode($data);
  245. }elseif('php'==$type){
  246. $data = serialize($data);
  247. }// ??????
  248. $this->setContentType($type);
  249. header('Content-Length: ' . strlen($data));
  250. return $data;
  251. }
  252. // ??Http????
  253. protected function sendHttpStatus($code) {
  254. static $_status = array(
  255. // Informational 1xx
  256. 100 => 'Continue',
  257. 101 => 'Switching Protocols',
  258. // Success 2xx
  259. 200 => 'OK',
  260. 201 => 'Created',
  261. 202 => 'Accepted',
  262. 203 => 'Non-Authoritative Information',
  263. 204 => 'No Content',
  264. 205 => 'Reset Content',
  265. 206 => 'Partial Content',
  266. // Redirection 3xx
  267. 300 => 'Multiple Choices',
  268. 301 => 'Moved Permanently',
  269. 302 => 'Moved Temporarily ', // 1.1
  270. 303 => 'See Other',
  271. 304 => 'Not Modified',
  272. 305 => 'Use Proxy',
  273. // 306 is deprecated but reserved
  274. 307 => 'Temporary Redirect',
  275. // Client Error 4xx
  276. 400 => 'Bad Request',
  277. 401 => 'Unauthorized',
  278. 402 => 'Payment Required',
  279. 403 => 'Forbidden',
  280. 404 => 'Not Found',
  281. 405 => 'Method Not Allowed',
  282. 406 => 'Not Acceptable',
  283. 407 => 'Proxy Authentication Required',
  284. 408 => 'Request Timeout',
  285. 409 => 'Conflict',
  286. 410 => 'Gone',
  287. 411 => 'Length Required',
  288. 412 => 'Precondition Failed',
  289. 413 => 'Request Entity Too Large',
  290. 414 => 'Request-URI Too Long',
  291. 415 => 'Unsupported Media Type',
  292. 416 => 'Requested Range Not Satisfiable',
  293. 417 => 'Expectation Failed',
  294. // Server Error 5xx
  295. 500 => 'Internal Server Error',
  296. 501 => 'Not Implemented',
  297. 502 => 'Bad Gateway',
  298. 503 => 'Service Unavailable',
  299. 504 => 'Gateway Timeout',
  300. 505 => 'HTTP Version Not Supported',
  301. 509 => 'Bandwidth Limit Exceeded'
  302. );
  303. if(isset($_status[$code])) {
  304. header('HTTP/1.1 '.$code.' '.$_status[$code]);
  305. // ??FastCGI?????
  306. header('Status:'.$code.' '.$_status[$code]);
  307. }
  308. }
  309. /**
  310. +----------------------------------------------------------
  311. * ????
  312. +----------------------------------------------------------
  313. * @access public
  314. +----------------------------------------------------------
  315. */
  316. public function __destruct() {
  317. // ????
  318. if(C('LOG_RECORD')) Log::save();
  319. // ??????
  320. tag('action_end');
  321. }
  322. }