PageRenderTime 59ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/rxwandc/application/ad.rxwan.com/classes/controller/base.php

https://bitbucket.org/i1598/caiyun_stat
PHP | 268 lines | 197 code | 37 blank | 34 comment | 31 complexity | 1d88d7eeeb3c08cd42c2faa4bcc8418c MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. defined('SYSPATH') or die('No direct script access.');
  3. class Controller_Base extends Controller {
  4. public $_response;
  5. public $_success = false;
  6. public $_index;
  7. public $_file = "common";
  8. public $_data = array();
  9. public $_totalCount = NULL;
  10. protected $_msg;
  11. public function __construct(Request $request, Response $response) {
  12. parent::__construct($request, $response);
  13. //Session::instance();
  14. //$this->checkLogin();
  15. }
  16. protected function checkLogin(){
  17. if(!isset($_SESSION['username'])){
  18. echo json_encode(array('success'=>'false'));exit;
  19. }
  20. }
  21. public function message( $file="" ){
  22. $file = empty($file) ? $this->_file : $file;
  23. return Kohana::message( $this->_file, $this->_index );
  24. }
  25. public function _response( $result=NULL, $file="", $data=array()){
  26. $this->_response["success"] = is_null($result) ? $this->_success : $result;
  27. if(!empty($this->_index))
  28. $this->_response["message"] = $this->message( $file );
  29. if(!is_null($this->_totalCount))
  30. $this->_response["results"] = $this->_totalCount;
  31. $this->_response["data"] = empty($data) ? $this->_data : $data;
  32. //var_dump($this->request->controller());exit;
  33. $this->response->body( json_encode($this->_response) );
  34. }
  35. public function _param( $keys, $default=NULL ){
  36. $data = array();
  37. if(!empty($keys)){
  38. foreach($keys as $k=>$key){
  39. $val = is_array($default) ? $default[$k] : $default;
  40. $data[$key]=Arr::get( $_REQUEST, $key, $val );
  41. }
  42. }
  43. $data = array_filter($data,array($this,'_filter'));
  44. return $data;
  45. }
  46. protected function _param1(){
  47. // $jsonStr = Arr::get($_REQUEST,'json','');
  48. $jsonStr = file_get_contents("php://input");
  49. //var_dump($jsonStr);exit;
  50. return json_decode($jsonStr,true);
  51. }
  52. protected function _operation( $model, $type, $data=array(), $key="uuid" ){
  53. $id = Arr::get( $_REQUEST, $key );
  54. if( is_null( $id ) ) $id = Arr::get( $data, $key );
  55. if( isset( $data[$key] )&& $type!='create' ) unset( $data[$key] );
  56. $this->_index = $type . ".fail";
  57. if($type=="create")
  58. list($id, $row) = $model->add( $data );
  59. elseif($type=="edit")
  60. $row = $model->edit( $id, $data );
  61. elseif($type=="del")
  62. $row = $model->del( $id, $data );
  63. //if(empty($row))
  64. //return $this->_response( false );
  65. //$this->_index = $type . ".success";
  66. //$this->_data = $model->getDetail( $id );
  67. //return $this->_response( true );
  68. }
  69. protected function _responseBody( $success=false, $param = "", $type="message"){
  70. $json = array();
  71. $json["success"] = $success;
  72. $json[$type] = empty($param) ? $this->_msg : $param;
  73. $json["data"] = $this->_data;
  74. $this->_output( json_encode($json) );
  75. }
  76. protected function _output( $json ){
  77. // $this->response->body( $json );
  78. echo $json;
  79. }
  80. /*
  81. * 过滤掉空字符串
  82. */
  83. protected function _filter($data1){
  84. return is_null($data1)?false:true;
  85. }
  86. /**
  87. * 验证uuid
  88. */
  89. protected function check_uuid($uuid) {
  90. if (! preg_match('/^[a-f0-9]{32}$/',$uuid)) {
  91. return false;
  92. }
  93. return $uuid;
  94. }
  95. /**
  96. * 验证IP
  97. */
  98. protected function check_ip($ip){
  99. if ($ip != '127.0.0.1' && $this->is_ip($ip)) {
  100. $output = sprintf ( "%u", ip2long($ip) );
  101. }else{
  102. $output = sprintf ( "%u", ip2long($this->ip()) );
  103. }
  104. return $output;
  105. }
  106. /**
  107. * IP
  108. */
  109. protected function get_ip(){
  110. return sprintf ( "%u", ip2long( $this->ip( ) ) );
  111. }
  112. /**
  113. * 获取IP
  114. */
  115. protected function ip() {
  116. $ip = '';
  117. if (isset ( $_SERVER ['HTTP_X_FORWARDED_FOR'] )) {
  118. $ip = $_SERVER ['HTTP_X_FORWARDED_FOR'];
  119. } elseif (isset ( $_SERVER ['HTTP_CLIENT_IP'] )) {
  120. $ip = $_SERVER ['HTTP_CLIENT_IP'];
  121. } else {
  122. $ip = $_SERVER ['REMOTE_ADDR'];
  123. }
  124. return $ip;
  125. }
  126. /**
  127. * 匹配是否是IP
  128. */
  129. protected function is_ip($ip){
  130. $flag = TRUE;
  131. $intip = ip2long($ip);
  132. if($intip == -1 || $intip === FALSE){
  133. $flag = FALSE;
  134. }
  135. return $flag;
  136. }
  137. protected function is_version($string){
  138. $flag = false;
  139. if(preg_match('/^[v0-9\.]{3,20}$/', $string)){
  140. if(strpos($string, '.')){ // 以 .开头的字符串,也不算版本
  141. $flag = true;
  142. }
  143. }
  144. return $flag;
  145. }
  146. protected function explode_package($pkg_name){
  147. $pkg_info = array(
  148. 'pkg'=>$pkg_name,
  149. 'ver'=>'',
  150. 'coop'=>'0' //默认为0,就是我们公司的
  151. );
  152. $protect_members = array('caiyun', 'update', 'setup', 'install','installer', 'uninstall', 'inst', 'uninst', 'log', 'login', 'beta');
  153. $pkg_name = strtolower(trim($this->sbc2abc($pkg_name)));
  154. if(!empty($pkg_name)){
  155. $pkg_name_info = substr($pkg_name, 0, strlen($pkg_name)-4);
  156. $pkg_name_infos = explode('_',$pkg_name_info);
  157. $pkg_name_infos_length = count($pkg_name_infos);
  158. if($pkg_name_infos_length>2){
  159. $coopinfo = $this->coopfilter($pkg_name_infos[$pkg_name_infos_length-1]);
  160. if(!in_array($coopinfo, $protect_members)){
  161. if($this->is_version($coopinfo)){
  162. $pkg_info['ver']=$coopinfo;
  163. }elseif($this->is_cooper($coopinfo, $protect_members)){
  164. $pkg_info['coop'] = $coopinfo;
  165. }
  166. }
  167. if($this->is_version($pkg_name_infos[$pkg_name_infos_length-2])){
  168. $pkg_info['ver'] = $pkg_name_infos[$pkg_name_infos_length-2];
  169. }
  170. }
  171. }
  172. return $pkg_info;
  173. }
  174. protected function is_cooper($cooper, $protect_members){
  175. $flag = strlen($cooper)>1? true:false;
  176. if ($flag) {
  177. foreach ($protect_members as $protect_member){
  178. if(strpos($cooper, $protect_member)!==FALSE){ //只要出现,就不是了
  179. $flag = false;
  180. break;
  181. }
  182. }
  183. }
  184. return $flag;
  185. }
  186. protected function sbc2abc($str) {
  187. $f = array (' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '.', '-', '_', '@', '(', ')', '【', '】', '[', ']', '{', '}', '=', '+', ':', ';', ',', '。','《', '》');
  188. $t = array (' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '.', '-', '_', '@' , '(', ')', '[', ']', '[', ']','{','}','=','+',':',';',',','.','<','>');
  189. $str = str_replace ( $f, $t, $str );
  190. return $str;
  191. }
  192. // sbc2abc 在原始字符串的时候,就替换过了
  193. // 过滤掉非字母等字符,并且输出全部是小写
  194. // 过滤掉了空格和汉字等
  195. // 从左到右,会过滤掉后面的非法字符,也就是左合法,到右边,依次不合法
  196. // 例如:asdfasf我要合作商---> asdfasf, 我要mdsfuowef合作msfasom商---->mdsfuowef
  197. protected function coopfilter($string){
  198. $coops = explode('.exe', strtolower($string));
  199. $string = preg_replace(array('/(?:\[|\(|\{)[0-9a-z\.\-\_\,\;\[\{\(\)\}\]]+(?:\}|\)|\])/','/(?:\[|\(|\{)/','/(?:\}|\)|\])/' ), '', $coops[0]);
  200. $length = strlen($string);
  201. $avaliable_alpha = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '-', '_', '@' ,'.');
  202. $output = '';
  203. for ($i = 0; $i < $length; $i++) {
  204. if (in_array($string[$i], $avaliable_alpha)) {
  205. $output .= $string[$i];
  206. }else{
  207. if(!empty($output)){
  208. break;
  209. }
  210. }
  211. }
  212. return trim($output);
  213. }
  214. protected function logtofile($file_name='', $title="", $content="", $is_exit=false, $message=''){
  215. global $logfile;
  216. $output = "======================================\r\n";
  217. $output .= date("r")."\t".$title."\r\n";
  218. $output .= "Content: ".$content."\r\n";
  219. $output .= "--------------------------------------\r\n";
  220. if(empty($file_name)){
  221. $file_name = $logfile;
  222. }
  223. file_put_contents($file_name, $output, FILE_APPEND);
  224. if ($message) {
  225. echo $message;
  226. }
  227. if($is_exit){
  228. exit;
  229. }
  230. }
  231. }