function.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * 洛阳赤炎鹰网络科技有限公司
  4. * https://www.cyyvip.com
  5. * Copyright (c) 2022 赤店商城 All rights reserved.
  6. */
  7. function sp_testwrite($d) {
  8. $tfile = "_test.txt";
  9. $fp = @fopen($d . "/" . $tfile, "w");
  10. if (!$fp) {
  11. return false;
  12. }
  13. fclose($fp);
  14. $rs = @unlink($d . "/" . $tfile);
  15. if ($rs) {
  16. return true;
  17. }
  18. return false;
  19. }
  20. function sp_dir_create($path, $mode = 0777) {
  21. if (is_dir($path))
  22. return true;
  23. $ftp_enable = 0;
  24. $path = sp_dir_path($path);
  25. $temp = explode('/', $path);
  26. $cur_dir = '';
  27. $max = count($temp) - 1;
  28. for ($i = 0; $i < $max; $i++) {
  29. $cur_dir .= $temp[$i] . '/';
  30. if (@is_dir($cur_dir))
  31. continue;
  32. @mkdir($cur_dir, 0777, true);
  33. @chmod($cur_dir, 0777);
  34. }
  35. return is_dir($path);
  36. }
  37. function sp_dir_path($path) {
  38. $path = str_replace('\\', '/', $path);
  39. if (substr($path, -1) != '/')
  40. $path = $path . '/';
  41. return $path;
  42. }
  43. function sp_execute_sql($db,$file,$tablepre){
  44. //读取SQL文件
  45. $sql = file_get_contents(MODULE_PATH . 'Data/'.$file);
  46. $sql = str_replace("\r", "\n", $sql);
  47. $sql = explode(";\n", $sql);
  48. //替换表前缀
  49. $default_tablepre = "cmf_";
  50. $sql = str_replace(" `{$default_tablepre}", " `{$tablepre}", $sql);
  51. //开始安装
  52. sp_show_msg('开始安装数据库...');
  53. foreach ($sql as $item) {
  54. $item = trim($item);
  55. if(empty($item)) continue;
  56. preg_match('/CREATE TABLE `([^ ]*)`/', $item, $matches);
  57. if($matches) {
  58. $table_name = $matches[1];
  59. $msg = "创建数据表{$table_name}";
  60. if(false !== $db->execute($item)){
  61. sp_show_msg($msg . ' 完成');
  62. } else {
  63. sp_show_msg($msg . ' 失败!', 'error');
  64. }
  65. } else {
  66. $db->execute($item);
  67. }
  68. }
  69. }
  70. /**
  71. * 显示提示信息
  72. * @param string $msg 提示信息
  73. */
  74. function sp_show_msg($msg, $class = ''){
  75. echo "<script type=\"text/javascript\">showmsg(\"{$msg}\", \"{$class}\")</script>";
  76. flush();
  77. ob_flush();
  78. }
  79. function sp_update_site_configs($db,$table_prefix){
  80. $sitename=I("post.sitename");
  81. $email=I("post.manager_email");
  82. $siteurl=I("post.siteurl");
  83. $seo_keywords=I("post.sitekeywords");
  84. $seo_description=I("post.siteinfo");
  85. $site_options=<<<helllo
  86. {
  87. "site_name":"$sitename",
  88. "site_host":"$siteurl",
  89. "site_root":"",
  90. "site_icp":"",
  91. "site_admin_email":"$email",
  92. "site_tongji":"",
  93. "site_copyright":"",
  94. "site_seo_title":"$sitename",
  95. "site_seo_keywords":"$seo_keywords",
  96. "site_seo_description":"$seo_description"
  97. }
  98. helllo;
  99. $sql="INSERT INTO `{$table_prefix}options` (option_value,option_name) VALUES ('$site_options','site_options')";
  100. $db->execute($sql);
  101. sp_show_msg("网站信息配置成功!");
  102. }
  103. function sp_create_admin_account($db,$table_prefix,$authcode){
  104. $username=I("post.manager");
  105. $password=sp_password(I("post.manager_pwd"),$authcode);
  106. $email=I("post.manager_email");
  107. $create_date=date("Y-m-d h:i:s");
  108. $ip=get_client_ip(0,true);
  109. $sql =<<<hello
  110. INSERT INTO `{$table_prefix}users`
  111. (id,user_login,user_pass,user_nicename,user_email,user_url,create_time,user_activation_key,user_status,last_login_ip,last_login_time) VALUES
  112. ('1', '{$username}', '{$password}', 'admin', '{$email}', '', '{$create_date}', '', '1', '{$ip}','{$create_date}');;
  113. hello;
  114. $db->execute($sql);
  115. sp_show_msg("管理员账号创建成功!");
  116. }
  117. /**
  118. * 写入配置文件
  119. * @param array $config 配置信息
  120. */
  121. function sp_create_config($config){
  122. if(is_array($config)){
  123. //读取配置内容
  124. $conf = file_get_contents(MODULE_PATH . 'Data/db.php');
  125. //替换配置项
  126. foreach ($config as $key => $value) {
  127. $conf = str_replace("#{$key}#", $value, $conf);
  128. }
  129. //$conf = str_replace('#AUTHCODE#', $authcode, $conf);
  130. //$conf = str_replace('#COOKIE_PREFIX#', sp_random_string(6) . "_", $conf);
  131. @touch('../config/db.php');
  132. //写入应用配置文件
  133. if(file_put_contents( '../config/db.php', $conf)){
  134. sp_show_msg('配置文件写入成功');
  135. } else {
  136. sp_show_msg('配置文件写入失败!', 'error');
  137. }
  138. return '';
  139. }
  140. }