rockFun.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. /**
  3. *****************************************************************
  4. * 联系QQ: 290802026 *
  5. * 版 本: V2.0 *
  6. * 开发者:雨中磐石工作室 *
  7. * 邮 箱: admin@rockoa.com *
  8. * 网 址: http://www.rockoa.com/ *
  9. * 说 明: 定义常用的方法 *
  10. * 备 注: 未经允许不得商业出售,代码欢迎参考纠正 *
  11. *****************************************************************
  12. */
  13. /**
  14. * m 读取数据模型,操作数据库的
  15. * $name 表名/文件名
  16. */
  17. function m($name)
  18. {
  19. $cls = NULL;
  20. $pats = $nac = '';
  21. $nas = $name;
  22. $asq = explode(':', $nas);
  23. if(count($asq)>1){
  24. $nas = $asq[1];
  25. $nac = $asq[0];
  26. $pats = $nac.'/';
  27. $_pats = ''.ROOT_PATH.'/'.PROJECT.'/model/'.$nac.'/'.$nac.'.php';
  28. if(file_exists($_pats)){
  29. include_once($_pats);
  30. $class = ''.$nac.'Model';
  31. $cls = new $class($nas);
  32. }
  33. }
  34. $class = ''.$nas.'ClassModel';
  35. $path = ''.ROOT_PATH.'/'.PROJECT.'/model/'.$pats.''.$nas.'Model.php';
  36. if(file_exists($path)){
  37. include_once($path);
  38. if($nac!='')$class= $nac.'_'.$class;
  39. $cls = new $class($nas);
  40. }
  41. if($cls==NULL)$cls = new sModel($nas);
  42. return $cls;
  43. }
  44. /**
  45. * 引入插件
  46. * $name 插件名称
  47. * $inbo 是否初始化
  48. * $param1,2,参数
  49. */
  50. function c($name, $inbo=true, $param1='', $param2='')
  51. {
  52. $class = ''.$name.'Chajian';
  53. $path = ''.ROOT_PATH.'/include/chajian/'.$class.'.php';
  54. $cls = NULL;
  55. if(file_exists($path)){
  56. include_once($path);
  57. if($inbo)$cls = new $class($param1, $param2);
  58. }
  59. return $cls;
  60. }
  61. /**
  62. * 引入class文件
  63. */
  64. function import($name, $inbo=true)
  65. {
  66. $class = ''.$name.'Class';
  67. $path = ''.ROOT_PATH.'/include/class/'.$class.'.php';
  68. $cls = NULL;
  69. if(file_exists($path)){
  70. include_once($path);
  71. if($inbo){
  72. $cls = new $class();
  73. }
  74. }
  75. return $cls;
  76. }
  77. /**
  78. * 读取配置
  79. */
  80. function getconfig($key, $dev='')
  81. {
  82. $a = array();
  83. if(isset($GLOBALS['config']))$a = $GLOBALS['config'];
  84. $s = '';
  85. if(isset($a[$key]))$s = $a[$key];
  86. if($s==='')$s = $dev;
  87. return $s;
  88. }
  89. /**
  90. * 判断变量是否为空
  91. * @return boolean
  92. */
  93. function isempt($str)
  94. {
  95. $bool=false;
  96. if( ($str==''||$str==NULL||empty($str)) && (!is_numeric($str)) )$bool=true;
  97. return $bool;
  98. }
  99. /**
  100. * 判断变量是否包含在另一变量里面
  101. * @return boolean
  102. */
  103. function contain($str,$a)
  104. {
  105. $bool=false;
  106. if(!isempt($a) && !isempt($str)){
  107. $ad=strpos($str,$a);
  108. if($ad>0||!is_bool($ad))$bool=true;
  109. }
  110. return $bool;
  111. }
  112. /**
  113. * 获取请求的头
  114. * @return string/array
  115. */
  116. function getheader($key='')
  117. {
  118. $arr = array();
  119. if(function_exists('getallheaders'))$arr = getallheaders();
  120. if($key=='')return $arr;
  121. return arrvalue($arr, $key);
  122. }
  123. /**
  124. * 是否ajax请求
  125. * @return boolean
  126. */
  127. function isajax()
  128. {
  129. if(strtolower(getheader('X-Requested-With'))=='xmlhttprequest'){
  130. return true;
  131. }else{
  132. return false;
  133. };
  134. }
  135. function backmsg($msg='', $demsg='处理成功', $da=array())
  136. {
  137. $code = 201;
  138. if($msg == ''){
  139. $msg = $demsg;
  140. $code= 200;
  141. }
  142. showreturn($da, $msg, $code);
  143. }
  144. /**
  145. * 返回错误信息
  146. */
  147. function returnerror($msg='', $code=201, $carr=array())
  148. {
  149. $carr['msg'] = $msg;
  150. $carr['code'] = $code;
  151. $carr['success'] = false;
  152. $carr['data'] = '';
  153. return $carr;
  154. }
  155. /**
  156. * 返回正确信息
  157. */
  158. function returnsuccess($data=array(),$msg='')
  159. {
  160. $carr['msg'] = $msg;
  161. $carr['code'] = 200;
  162. $carr['success'] = true;
  163. $carr['data'] = $data;
  164. return $carr;
  165. }
  166. function showreturn($arr='', $msg='', $code=200)
  167. {
  168. $callback = @$_GET['callback'];
  169. $success = true;
  170. if($code != 200)$success = false;
  171. $result = json_encode(array(
  172. 'code' => $code,
  173. 'msg' => $msg,
  174. 'data' => $arr,
  175. 'success'=> $success
  176. ));
  177. if(!isempt($callback)){
  178. echo ''.$callback.'('.$result.')';
  179. }else{
  180. echo $result;
  181. }
  182. exit();
  183. }
  184. /**
  185. * 错误处理
  186. */
  187. function rockerror($errno, $errstr,$err_file = '', $err_line = 0){
  188. $str = "File:".$err_file." Line:[$err_line] Error: [$errno] $errstr";
  189. echo $str;exit();
  190. }
  191. /**
  192. * 在数组里读取变量
  193. * @return value
  194. */
  195. function arrvalue($arr, $k, $dev='')
  196. {
  197. $val = $dev;
  198. if(isset($arr[$k]))$val= $arr[$k];
  199. return $val;
  200. }
  201. /**
  202. * 在对象里读取变量
  203. * @return value
  204. */
  205. function objvalue($arr, $k, $dev='')
  206. {
  207. $val = $dev;
  208. if(isset($arr->$k))$val= $arr->$k;
  209. return $val;
  210. }
  211. /**
  212. * 过滤字符串的空格
  213. */
  214. function trimstr($str)
  215. {
  216. return trim(str_replace(' ','',$str));
  217. }
  218. /**
  219. * getallheaders不存在时
  220. */
  221. if (!function_exists('getallheaders')){
  222. function getallheaders(){
  223. $headers = array();
  224. foreach ($_SERVER as $name => $value) {
  225. if(substr($name, 0, 5) == 'HTTP_') {
  226. $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
  227. }
  228. }
  229. return $headers;
  230. }
  231. }
  232. function getparams($key, $dev='')
  233. {
  234. if(PHP_SAPI != 'cli'){
  235. return arrvalue($_GET,$key,$dev);
  236. }
  237. $arr = arrvalue($GLOBALS, 'argv');
  238. $sss = '';
  239. if($arr)for($i=2;$i<count($arr);$i++){
  240. $str = $arr[$i];
  241. if(!isempt($str)){
  242. $stra = explode('=', $str);
  243. if($stra[0]=='-'.$key.''){
  244. $sss = arrvalue($stra, 1);
  245. break;
  246. }
  247. }
  248. }
  249. if(isempt($sss))$sss = $dev;
  250. return $sss;
  251. }
  252. /**
  253. * 多语言返回
  254. */
  255. function lang($str,$lx='')
  256. {
  257. if(!$str)return $str;
  258. $da = arrvalue($GLOBALS,'langdata');
  259. if(!$da)return $str;
  260. $xuhao = $da['xuhao'];
  261. $key = 'data'.$lx.'';
  262. if(!isset($da[$key])){
  263. $path = ''.ROOT_PATH.'/include/langlocal/langphp/lang'.$lx.'.php';
  264. $data = array();
  265. if(file_exists($path))$data = require($path);
  266. $GLOBALS['langdata'][$key] = $data;
  267. }else{
  268. $data = $da[$key];
  269. }
  270. if(!isset($data[$str]))return $str;
  271. $nstr = arrvalue($data[$str], $xuhao);
  272. if(!$nstr)$nstr = $str;
  273. return $nstr;
  274. }
  275. /**
  276. * 为空替换为''
  277. */
  278. function repempt($str,$dev='')
  279. {
  280. $s = $str;
  281. if($s===null)$s=$dev;
  282. return $s;
  283. }
粤ICP备19079148号