common.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 老猫 <thinkcmf@126.com>
  10. // +----------------------------------------------------------------------
  11. function sp_testwrite($d)
  12. {
  13. $tfile = "_test.txt";
  14. $fp = @fopen($d . "/" . $tfile, "w");
  15. if (!$fp) {
  16. return false;
  17. }
  18. fclose($fp);
  19. $rs = @unlink($d . "/" . $tfile);
  20. if ($rs) {
  21. return true;
  22. }
  23. return false;
  24. }
  25. function sp_dir_create($path, $mode = 0777)
  26. {
  27. if (is_dir($path))
  28. return true;
  29. $ftp_enable = 0;
  30. $path = sp_dir_path($path);
  31. $temp = explode('/', $path);
  32. $cur_dir = '';
  33. $max = count($temp) - 1;
  34. for ($i = 0; $i < $max; $i++) {
  35. $cur_dir .= $temp[$i] . '/';
  36. if (@is_dir($cur_dir))
  37. continue;
  38. @mkdir($cur_dir, 0777, true);
  39. @chmod($cur_dir, 0777);
  40. }
  41. return is_dir($path);
  42. }
  43. function sp_dir_path($path)
  44. {
  45. $path = str_replace('\\', '/', $path);
  46. if (substr($path, -1) != '/')
  47. $path = $path . '/';
  48. return $path;
  49. }
  50. function sp_execute_sql($db, $sql)
  51. {
  52. $sql = trim($sql);
  53. preg_match('/CREATE TABLE .+ `([^ ]*)`/', $sql, $matches);
  54. if ($matches) {
  55. $table_name = $matches[1];
  56. $msg = "创建数据表{$table_name}";
  57. try {
  58. $db->execute($sql);
  59. return [
  60. 'error' => 0,
  61. 'message' => $msg . ' 成功!'
  62. ];
  63. } catch (\Exception $e) {
  64. return [
  65. 'error' => 1,
  66. 'message' => $msg . ' 失败!',
  67. 'exception' => $e->getTraceAsString()
  68. ];
  69. }
  70. } else {
  71. try {
  72. $db->execute($sql);
  73. return [
  74. 'error' => 0,
  75. 'message' => 'SQL执行成功!'
  76. ];
  77. } catch (\Exception $e) {
  78. return [
  79. 'error' => 1,
  80. 'message' => 'SQL执行失败!',
  81. 'exception' => $e->getTraceAsString()
  82. ];
  83. }
  84. }
  85. }
  86. /**
  87. * 显示提示信息
  88. * @param string $msg 提示信息
  89. */
  90. function sp_show_msg($msg, $class = '')
  91. {
  92. echo "<script type=\"text/javascript\">showmsg(\"{$msg}\", \"{$class}\")</script>";
  93. flush();
  94. ob_flush();
  95. }
  96. function sp_update_site_configs($db, $table_prefix)
  97. {
  98. $sitename = I("post.sitename");
  99. $email = I("post.manager_email");
  100. $siteurl = I("post.siteurl");
  101. $seo_keywords = I("post.sitekeywords");
  102. $seo_description = I("post.siteinfo");
  103. $site_options = <<<helllo
  104. {
  105. "site_name":"$sitename",
  106. "site_host":"$siteurl",
  107. "site_root":"",
  108. "site_icp":"",
  109. "site_admin_email":"$email",
  110. "site_tongji":"",
  111. "site_copyright":"",
  112. "site_seo_title":"$sitename",
  113. "site_seo_keywords":"$seo_keywords",
  114. "site_seo_description":"$seo_description"
  115. }
  116. helllo;
  117. $sql = "INSERT INTO `{$table_prefix}options` (option_value,option_name) VALUES ('$site_options','site_options')";
  118. $db->execute($sql);
  119. sp_show_msg("网站信息配置成功!");
  120. }
  121. function sp_create_admin_account($db, $table_prefix, $authcode)
  122. {
  123. $username = I("post.manager");
  124. $password = sp_password(I("post.manager_pwd"), $authcode);
  125. $email = I("post.manager_email");
  126. $create_date = date("Y-m-d h:i:s");
  127. $ip = get_client_ip(0, true);
  128. $sql = <<<hello
  129. INSERT INTO `{$table_prefix}users`
  130. (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
  131. ('1', '{$username}', '{$password}', 'admin', '{$email}', '', '{$create_date}', '', '1', '{$ip}','{$create_date}');;
  132. hello;
  133. $db->execute($sql);
  134. sp_show_msg("管理员账号创建成功!");
  135. }
  136. function sp_create_db_config($config)
  137. {
  138. if (is_array($config)) {
  139. //读取配置内容
  140. $conf = file_get_contents(__DIR__ . '/data/config.php');
  141. //替换配置项
  142. foreach ($config as $key => $value) {
  143. $conf = str_replace("#{$key}#", $value, $conf);
  144. }
  145. if (strpos(cmf_version(), '5.0.') === false) {
  146. $confDir = CMF_DATA . 'config/'; // 5.1
  147. } else {
  148. $confDir = CMF_DATA . 'conf/'; // 5.0
  149. }
  150. try {
  151. if (!file_exists($confDir)) {
  152. mkdir($confDir, 0777, true);
  153. }
  154. file_put_contents($confDir . 'database.php', $conf);
  155. } catch (\Exception $e) {
  156. return false;
  157. }
  158. return true;
  159. }
  160. }