📄 正在查看:twcms/install/function.php
1<?php
2/**
3 * (C)2012-2014 twcms.com TongWang Inc.
4 * Author: wuzhaohuan <kongphp@gmail.com>
5 */
6
7// 递归检测目录/文件是否写
8function _dir_write($dir, $clear = FALSE) {
9 static $ret = array();
10
11 if($clear) $ret = array('yes'=>array(), 'no'=>array());
12
13 if(!is_dir($dir) || _no_writable($dir) || !$dh = opendir($dir)) {
14 $ret['no'][] = array($dir, substr(sprintf('%o', fileperms($dir)), -4));
15 }else{
16 $ret['yes'][] = array($dir, substr(sprintf('%o', fileperms($dir)), -4));
17
18 while(($file = readdir($dh)) !== FALSE) {
19 if($file!='.' && $file!='..') {
20 $fileson = $dir.'/'.$file;
21 if(is_dir($fileson)) {
22 _dir_write($fileson); // 递归检测
23 }elseif(is_file($fileson)) {
24 if(_no_writable($fileson)) {
25 $ret['no'][] = array($fileson, substr(sprintf('%o', fileperms($fileson)), -4));
26 }else{
27 $ret['yes'][] = array($fileson, substr(sprintf('%o', fileperms($fileson)), -4));
28 }
29 }
30 }
31 }
32 closedir($dh);
33 }
34
35 return $ret;
36}
37
38// 不可写返回 TRUE
39function _no_writable($dir) {
40 if(_is_writable($dir)) {
41 return FALSE;
42 }else{
43 function_exists('chmod') && chmod($file, 0777); // 尝试自动修复权限
44 return !_is_writable($dir);
45 }
46}
47
48// 获取所在目录
49function get_webdir() {
50 $str = dirname(dirname(dirname($_SERVER['PHP_SELF'])));
51 if($str == '\\') return '/';
52 if(strlen($str)>1) return $str.'/';
53 else return '/';
54}
55
56// 分割SQL语句
57function split_sql($sql, $tablepre) {
58 $sql = str_replace('pre_', $tablepre, $sql);
59 $sql = str_replace("\r", '', $sql);
60 $ret = array();
61 $num = 0;
62 $queriesarray = explode(";\n", trim($sql));
63 unset($sql);
64 foreach($queriesarray as $query) {
65 $ret[$num] = isset($ret[$num]) ? $ret[$num] : '';
66 $queries = explode("\n", trim($query));
67 foreach($queries as $query) {
68 $ret[$num] .= isset($query[0]) && $query[0] == "#" ? '' : trim(preg_replace('/\#.*/', '', $query));
69 }
70 $num++;
71 }
72 return $ret;
73}
74
75// JS输出
76function js_show($s) {
77 echo '<script type="text/javascript">jsShow(\''.addslashes($s).'\');</script>'."\r\n";
78 flush();
79 ob_flush();
80}
81
82// JS输出提示返回
83function js_back($s) {
84 js_show($s.' <a href="javascript:history.back();">[返回]</a>');
85 exit;
86}
87