PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/controller/util.php

https://gitlab.com/imxieke/XCloud
PHP | 280 lines | 258 code | 9 blank | 13 comment | 17 complexity | def5703c2cbb47b4d227c65586840b97 MD5 | raw file
  1. <?php
  2. /*
  3. * @link http://www.kalcaddle.com/
  4. * @author warlee | e-mail:kalcaddle@qq.com
  5. * @copyright warlee 2014.(Shanghai)Co.,Ltd
  6. * @license http://kalcaddle.com/tools/licenses/license.txt
  7. */
  8. //处理成标准目录
  9. function _DIR_CLEAR($path){
  10. $path = htmlspecial_decode($path);
  11. $path = str_replace('\\','/',trim($path));
  12. if (strstr($path,'../')) {//preg耗性能
  13. $path = preg_replace('/\.+\/+/', '/', $path);
  14. }
  15. $path = preg_replace('/\/+/', '/', $path);
  16. return $path;
  17. }
  18. //处理成用户目录,并且不允许相对目录的请求操作
  19. function _DIR($path){
  20. $path = _DIR_CLEAR(rawurldecode($path));
  21. $path = iconv_system($path);
  22. if (substr($path,0,strlen('*recycle*/')) == '*recycle*/') {
  23. return USER_RECYCLE.str_replace('*recycle*/','',$path);
  24. }
  25. if (substr($path,0,strlen('*public*/')) == '*public*/') {
  26. return PUBLIC_PATH.str_replace('*public*/','',$path);
  27. }
  28. if (substr($path,0,strlen('*share*/')) == '*share*/') {
  29. return "*share*/";
  30. }
  31. $path = HOME.$path;
  32. if (is_dir($path)) $path = rtrim($path,'/').'/';
  33. return $path;
  34. }
  35. //处理成用户目录输出
  36. function _DIR_OUT(&$arr){
  37. xxsClear($arr);
  38. if (isset($GLOBALS['is_root'])&&$GLOBALS['is_root']) return;
  39. if (is_array($arr)) {
  40. foreach ($arr['filelist'] as $key => $value) {
  41. $arr['filelist'][$key]['path'] = pre_clear($value['path']);
  42. }
  43. foreach ($arr['folderlist'] as $key => $value) {
  44. $arr['folderlist'][$key]['path'] = pre_clear($value['path']);
  45. }
  46. }else{
  47. $arr = pre_clear($arr);
  48. }
  49. }
  50. //前缀处理 非root用户目录/从HOME开始
  51. function pre_clear($path){
  52. if (ST=='share') {
  53. return str_replace(HOME,'',$path);
  54. }
  55. if (substr($path,0,strlen(PUBLIC_PATH)) == PUBLIC_PATH) {
  56. return '*public*/'.str_replace(PUBLIC_PATH,'',$path);
  57. }
  58. if (substr($path,0,strlen(USER_RECYCLE)) == USER_RECYCLE) {
  59. return '*recycle*/'.str_replace(USER_RECYCLE,'',$path);
  60. }
  61. return str_replace(HOME,'',$path);
  62. }
  63. function xxsClear(&$list){
  64. if (is_array($list)) {
  65. foreach ($list['filelist'] as $key => $value) {
  66. $list['filelist'][$key]['ext'] = htmlspecial($value['ext']);
  67. $list['filelist'][$key]['path'] = htmlspecial($value['path']);
  68. $list['filelist'][$key]['name'] = htmlspecial($value['name']);
  69. }
  70. foreach ($list['folderlist'] as $key => $value) {
  71. $list['folderlist'][$key]['path'] = htmlspecial($value['path']);
  72. $list['folderlist'][$key]['name'] = htmlspecial($value['name']);
  73. }
  74. }else{
  75. $list = htmlspecial($list);
  76. }
  77. }
  78. function htmlspecial($str){
  79. return str_replace(
  80. array('<','>','"',"'"),
  81. array('&lt;','&gt;','&quot;','&#039;','&amp;'),
  82. $str
  83. );
  84. }
  85. function htmlspecial_decode($str){
  86. return str_replace(
  87. array('&lt;','&gt;','&quot;','&#039;'),
  88. array('<','>','"',"'"),
  89. $str
  90. );
  91. }
  92. //扩展名权限判断
  93. function checkExtUnzip($s,$info){
  94. return checkExt($info['stored_filename']);
  95. }
  96. //扩展名权限判断 有权限则返回1 不是true
  97. function checkExt($file,$changExt=false){
  98. if (strstr($file,'<') || strstr($file,'>') || $file=='') {
  99. return 0;
  100. }
  101. if ($GLOBALS['is_root'] == 1) return 1;
  102. $not_allow = $GLOBALS['auth']['ext_not_allow'];
  103. $ext_arr = explode('|',$not_allow);
  104. foreach ($ext_arr as $current) {
  105. if ($current !== '' && stristr($file,'.'.$current)){//含有扩展名
  106. return 0;
  107. }
  108. }
  109. return 1;
  110. }
  111. function get_charset(&$str) {
  112. if ($str == '') return 'utf-8';
  113. //前面检测成功则,自动忽略后面
  114. $charset=strtolower(mb_detect_encoding($str,$GLOBALS['config']['check_charset']));
  115. if (substr($str,0,3)==chr(0xEF).chr(0xBB).chr(0xBF)){
  116. $charset='utf-8';
  117. }else if($charset=='cp936'){
  118. $charset='gbk';
  119. }
  120. if ($charset == 'ascii') $charset = 'utf-8';
  121. return strtolower($charset);
  122. }
  123. function php_env_check(){
  124. $L = $GLOBALS['L'];
  125. $error = '';
  126. $base_path = get_path_this(BASIC_PATH).'/';
  127. if(!function_exists('iconv')) $error.= '<li>'.$L['php_env_error_iconv'].'</li>';
  128. if(!function_exists('mb_convert_encoding')) $error.= '<li>'.$L['php_env_error_mb_string'].'</li>';
  129. if(!version_compare(PHP_VERSION,'5.0','>=')) $error.= '<li>'.$L['php_env_error_version'].'</li>';
  130. if(!function_exists('file_get_contents')) $error.='<li>'.$L['php_env_error_file'].'</li>';
  131. if(!path_writable(BASIC_PATH)) $error.= '<li>'.$base_path.' '.$L['php_env_error_path'].'</li>';
  132. if(!path_writable(BASIC_PATH.'data')) $error.= '<li>'.$base_path.'data '.$L['php_env_error_path'].'</li>';
  133. if(!path_writable(BASIC_PATH.'data/system')) $error.= '<li>'.$base_path.'data/system '.$L['php_env_error_path'].'</li>';
  134. if(!path_writable(BASIC_PATH.'data/User')) $error.= '<li>'.$base_path.'data/User '.$L['php_env_error_path'].'</li>';
  135. if(!path_writable(BASIC_PATH.'data/thumb')) $error.= '<li>'.$base_path.'data/thumb '.$L['php_env_error_path'].'</li>';
  136. if( !function_exists('imagecreatefromjpeg')||
  137. !function_exists('imagecreatefromgif')||
  138. !function_exists('imagecreatefrompng')||
  139. !function_exists('imagecolorallocate')){
  140. $error.= '<li>'.$L['php_env_error_gd'].'</li>';
  141. }
  142. return $error;
  143. }
  144. //语言包加载:优先级:cookie获取>自动识别
  145. //首次没有cookie则自动识别——存入cookie,过期时间无限
  146. function init_lang(){
  147. if (isset($_COOKIE['kod_user_language'])) {
  148. $lang = $_COOKIE['kod_user_language'];
  149. }else{//没有cookie
  150. preg_match('/^([a-z\-]+)/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $matches);
  151. $lang = $matches[1];
  152. switch (substr($lang,0,2)) {
  153. case 'zh':
  154. if ($lang != 'zn-TW'){
  155. $lang = 'zh-CN';
  156. }
  157. break;
  158. case 'en':$lang = 'en';break;
  159. default:$lang = 'en';break;
  160. }
  161. $lang = str_replace('-', '_',$lang);
  162. setcookie('kod_user_language',$lang, time()+3600*24*365);
  163. }
  164. if ($lang == '') $lang = 'en';
  165. $lang = str_replace(array('/','\\','..','.'),'',$lang);
  166. define('LANGUAGE_TYPE', $lang);
  167. include(LANGUAGE_PATH.$lang.'/main.php');
  168. $GLOBALS['L'] = $L;
  169. }
  170. function init_setting(){
  171. $setting_file = USER_SYSTEM.'system_setting.php';
  172. if (!file_exists($setting_file)){//不存在则建立
  173. $setting = $GLOBALS['config']['setting_system_default'];
  174. $setting['menu'] = $GLOBALS['config']['setting_menu_default'];
  175. fileCache::save($setting_file,$setting);
  176. }else{
  177. $setting = fileCache::load($setting_file);
  178. }
  179. if (!is_array($setting)) {
  180. $setting = $GLOBALS['config']['setting_system_default'];
  181. }
  182. if (!is_array($setting['menu'])) {
  183. $setting['menu'] = $GLOBALS['config']['setting_menu_default'];
  184. }
  185. $GLOBALS['app']->setDefaultController($setting['first_in']);//设置默认控制器
  186. $GLOBALS['app']->setDefaultAction('index'); //设置默认控制器函数
  187. $GLOBALS['config']['setting_system'] = $setting;//全局
  188. $GLOBALS['L']['kod_name'] = $setting['system_name'];
  189. $GLOBALS['L']['kod_name_desc'] = $setting['system_desc'];
  190. if (isset($setting['powerby'])) {
  191. $GLOBALS['L']['kod_power_by'] = $setting['powerby'];
  192. }
  193. //加载用户自定义配置
  194. $setting_user = BASIC_PATH.'config/setting_user.php';
  195. if (file_exists($setting_user)) {
  196. include($setting_user);
  197. }
  198. }
  199. //防止恶意请求
  200. function check_post_many(){
  201. $check_time = 4;
  202. $maxt_num = 40;//5秒内最大请求次数。超过则自动退出
  203. $total_time = 60;//10nmin
  204. $total_time_num = 500;
  205. //管理员不受限制
  206. if( isset($_SESSION['kod_user']) &&
  207. $_SESSION['kod_user']['role']=='root'){
  208. return;
  209. }
  210. //上传不受限制
  211. $URI = $GLOBALS['in']['URLremote'];
  212. if (isset($URI[1]) && $URI[1] =='fileUpload') {
  213. return;
  214. }
  215. $session_key = 'check_post_many';
  216. $_SESSION['check_session_has'] = 'kodexplorer';
  217. if (!isset($_SESSION[$session_key])) {
  218. $_SESSION[$session_key] = array('last_time'=>time(),'total_num'=>0,'max_time'=>time(),'max_num'=>0);
  219. }else{
  220. $info = $_SESSION[$session_key];
  221. //----短期内并发控制
  222. if (time()-$info['last_time'] >=$check_time) {//大于时长s 则清空
  223. $info = array('last_time'=>time(),'total_num'=>0,'max_time'=>time(),'max_num'=>0);
  224. }else{
  225. if ($info['total_num'] >=$maxt_num) {//大于100次则直接退出
  226. user_logout();
  227. }else{
  228. $info['total_num'] +=1;
  229. }
  230. }
  231. //----总量控制
  232. if (time()-$info['max_time'] >=$total_time) {//大于时长s 则清空
  233. $info = array('last_time'=>time(),'total_num'=>0,'max_time'=>time(),'max_num'=>0);
  234. }else{
  235. if ($info['total_num'] >=$total_time_num) {//大于100次则直接退出
  236. user_logout();
  237. }else{
  238. $info['max_num'] +=1;
  239. }
  240. }
  241. $_SESSION[$session_key] = $info;
  242. }
  243. }
  244. function is_wap(){
  245. if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|iphone|ipad|ipod|android|xoom)/i',
  246. strtolower($_SERVER['HTTP_USER_AGENT']))){
  247. return true;
  248. }
  249. if((isset($_SERVER['HTTP_ACCEPT'])) &&
  250. (strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') !== false)){
  251. return true;
  252. }
  253. return false;
  254. }
  255. function user_logout(){
  256. setcookie('PHPSESSID', '', time()-3600,'/');
  257. setcookie('kod_name', '', time()-3600);
  258. setcookie('kod_token', '', time()-3600);
  259. setcookie('kod_user_language', '', time()-3600);
  260. session_destroy();
  261. header('location:./index.php?user/login');
  262. exit;
  263. }