function.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | OneThink [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: 小梦科技资源 <zuojiazi@vip.qq.com> <https://www.nanodreamtech.com>
  8. // +----------------------------------------------------------------------
  9. // 检测环境是否支持可写
  10. define('IS_WRITE', APP_MODE !== 'sae');
  11. /**
  12. * 生成系统AUTH_KEY
  13. * @author 小梦科技资源 <zuojiazi@vip.qq.com>
  14. */
  15. function build_auth_key()
  16. {
  17. $chars = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  18. $chars .= '`~!@#$%^&*()_+-=[]{};:"|,.<>/?';
  19. $chars = str_shuffle($chars);
  20. return substr($chars, 0, 40);
  21. }
  22. /**
  23. * 系统环境检测
  24. * @return array 系统环境数据
  25. */
  26. function check_env()
  27. {
  28. $items = array(
  29. 'os' => array('操作系统', '不限制', '类Unix', PHP_OS, 'success'),
  30. 'php' => array('PHP版本', '5.3', '5.3+', PHP_VERSION, 'success'),
  31. //'mysql' => array('MYSQL版本', '5.0', '5.0+', '未知', 'success'), //PHP5.5不支持mysql版本检测
  32. 'upload' => array('附件上传', '不限制', '2M+', '未知', 'success'),
  33. 'gd' => array('GD库', '2.0', '2.0+', '未知', 'success'),
  34. 'curl' => array('Curl扩展', '开启', '不限制', '未知', 'success'),
  35. 'disk' => array('磁盘空间', '5M', '不限制', '未知', 'success'),
  36. );
  37. //PHP环境检测
  38. if ($items['php'][3] < $items['php'][1]) {
  39. $items['php'][4] = 'remove';
  40. session('error', true);
  41. }
  42. //附件上传检测
  43. if (@ini_get('file_uploads'))
  44. $items['upload'][3] = ini_get('upload_max_filesize');
  45. //GD库检测
  46. $tmp = function_exists('gd_info') ? gd_info() : array();
  47. if (empty($tmp['GD Version'])) {
  48. $items['gd'][3] = '未安装';
  49. $items['gd'][4] = 'remove';
  50. session('error', true);
  51. } else {
  52. $items['gd'][3] = $tmp['GD Version'];
  53. }
  54. unset($tmp);
  55. $tmp = function_exists('curl_init') ? curl_version() : array();
  56. if (empty($tmp['version'])) {
  57. $items['curl'][3] = '未安装';
  58. $items['curl'][4] = 'remove';
  59. session('curl', true);
  60. } else {
  61. $items['curl'][3] = $tmp['version'];
  62. }
  63. unset($tmp);
  64. //磁盘空间检测
  65. if (function_exists('disk_free_space')) {
  66. $items['disk'][3] = floor(disk_free_space(INSTALL_APP_PATH) / (1024 * 1024)) . 'M';
  67. }
  68. return $items;
  69. }
  70. /**
  71. * 目录,文件读写检测
  72. * @return array 检测数据
  73. */
  74. function check_dirfile()
  75. {
  76. $items = array(
  77. array('dir', '可写', 'ok', './Uploads/image'),
  78. array('dir', '可写', 'ok', './Runtime'),
  79. array('file', '可写', 'ok', './Modules/Common/Conf/')
  80. );
  81. foreach ($items as &$val) {
  82. if ('dir' == $val[0]) {
  83. if (!is_writable(INSTALL_APP_PATH . $val[3])) {
  84. if (is_dir($items[1])) {
  85. $val[1] = '可读';
  86. $val[2] = 'remove';
  87. session('error', true);
  88. } else {
  89. $val[1] = '不存在或者不可写';
  90. $val[2] = 'remove';
  91. session('error', true);
  92. }
  93. }
  94. } else {
  95. if (file_exists(INSTALL_APP_PATH . $val[3])) {
  96. if (!is_writable(INSTALL_APP_PATH . $val[3])) {
  97. $val[1] = '存在但不可写';
  98. $val[2] = 'remove';
  99. session('error', true);
  100. }
  101. } else {
  102. if (!is_writable(dirname(INSTALL_APP_PATH . $val[3]))) {
  103. $val[1] = '不存在或者不可写';
  104. $val[2] = 'remove';
  105. session('error', true);
  106. }
  107. }
  108. }
  109. }
  110. return $items;
  111. }
  112. /**
  113. * 函数检测
  114. * @return array 检测数据
  115. */
  116. function check_func()
  117. {
  118. $items = array(
  119. array('mysql_connect', '支持', 'ok'),
  120. array('file_get_contents', '支持', 'ok'),
  121. array('mb_strlen', '支持', 'ok'),
  122. array('curl_init', '支持', 'ok'),
  123. );
  124. foreach ($items as &$val) {
  125. if (!function_exists($val[0])) {
  126. $val[1] = '不支持';
  127. $val[2] = 'remove';
  128. $val[3] = '开启';
  129. session('error', true);
  130. }
  131. }
  132. return $items;
  133. }
  134. /**
  135. * 写入配置文件
  136. * @param array $config 配置信息
  137. */
  138. function write_config($config, $auth)
  139. {
  140. if (is_array($config)) {
  141. //读取配置内容
  142. $conf = file_get_contents(MODULE_PATH . 'Data/db.tpl');
  143. //替换配置项
  144. foreach ($config as $name => $value) {
  145. $conf = str_replace("[{$name}]", $value, $conf);
  146. }
  147. $conf = str_replace('[AUTH_KEY]', $auth, $conf);
  148. //写入应用配置文件
  149. if (!IS_WRITE) {
  150. return '由于您的环境不可写,请复制下面的配置文件内容覆盖到相关的配置文件,然后再登录后台。<p>' . realpath('') . './Modules/Common/Conf/db.php</p>
  151. <textarea name="" style="width:650px;height:185px">' . $conf . '</textarea>';
  152. } else {
  153. if (file_put_contents('./Modules/Common/Conf/db.php', $conf)) {
  154. chmod('./Modules/Common/Conf/db.php', 0777);
  155. show_msg('配置文件写入成功');
  156. } else {
  157. show_msg('配置文件写入失败!', 'error');
  158. session('error', true);
  159. }
  160. return '';
  161. }
  162. }
  163. }
  164. /**
  165. * 创建数据表
  166. * @param resource $db 数据库连接资源
  167. */
  168. function create_tables($db, $prefix = '')
  169. {
  170. //读取SQL文件
  171. $sql = file_get_contents(MODULE_PATH . 'Data/install.sql');
  172. $sql = str_replace("\r", "\n", $sql);
  173. $sql = explode(";\n", $sql);
  174. //替换表前缀
  175. $orginal = C('ORIGINAL_TABLE_PREFIX');
  176. $sql = str_replace(" `{$orginal}", " `{$prefix}", $sql);
  177. //开始安装
  178. show_msg('开始安装数据库...');
  179. foreach ($sql as $value) {
  180. $value = trim($value);
  181. if (empty($value)) continue;
  182. if (substr($value, 0, 12) == 'CREATE TABLE') {
  183. $name = preg_replace("/^CREATE TABLE IF NOT EXISTS `(\w+)` .*/s", "\\1", $value);
  184. $msg = "创建数据表{$name}";
  185. if (false !== $db->execute($value)) {
  186. show_msg($msg . '...成功');
  187. } else {
  188. show_msg($msg . '...失败!', 'error');
  189. session('error', true);
  190. }
  191. } else {
  192. $db->execute($value);
  193. }
  194. }
  195. }
  196. function register_administrator($db, $prefix, $admin, $auth)
  197. {
  198. show_msg('开始注册创始人帐号...');
  199. /*插入用户*/
  200. $sql = <<<sql
  201. REPLACE INTO `[PREFIX]admin` ( `a_uname`, `a_passwd`, `a_email`, `a_create_time`, `a_status`) VALUES
  202. ('[NAME]', '[PASS]','[EMAIL]', '[TIME]',1);
  203. sql;
  204. $password = think_ucenter_encrypt($admin['password'], $auth);
  205. $sql = str_replace(
  206. array('[PREFIX]', '[NAME]', '[PASS]', '[EMAIL]', '[TIME]'),
  207. array($prefix, $admin['username'], $password, $admin['email'], NOW_TIME),
  208. $sql);
  209. //执行sql
  210. $db->execute($sql);
  211. /*插入pwd_key资料*/
  212. $sql = <<<sql
  213. REPLACE INTO `[PREFIX]config` (`name`, `config_group`, `create_time`, `value`) VALUES
  214. ('[NAME]','site','[TIME]','[PASS]');
  215. sql;
  216. $sql = str_replace(
  217. array('[PREFIX]', '[NAME]', '[TIME]', '[PASS]'),
  218. array($prefix,'PWD_KEY', NOW_TIME, $auth),
  219. $sql);
  220. $db->execute($sql);
  221. show_msg('创始人帐号注册完成!');
  222. }
  223. /**
  224. * 及时显示提示信息
  225. * @param string $msg 提示信息
  226. */
  227. function show_msg($msg, $class = '')
  228. {
  229. echo "<script type=\"text/javascript\">showmsg(\"{$msg}\", \"{$class}\")</script>";
  230. ob_flush();
  231. flush();
  232. }
  233. /**
  234. * 系统非常规MD5加密方法
  235. * @param string $str 要加密的字符串
  236. * @return string
  237. */
  238. function user_md5($str, $key = '')
  239. {
  240. return '' === $str ? '' : md5(sha1($str) . $key);
  241. }