db_init.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /**
  3. * BCJD 导航 - 数据库初始化脚本
  4. *
  5. * 使用 OOP 类库重构。
  6. * 访问此文件一次即可自动建库建表并导入现有数据。
  7. * ⚠️ 执行完毕后建议删除或限制访问此文件!
  8. */
  9. require_once __DIR__ . '/init.php';
  10. use BCJD\Database;
  11. header('Content-Type: text/html; charset=utf-8');
  12. try {
  13. // 1. 先创建数据库
  14. $pdo = Database::rawConnection();
  15. $pdo->exec(sprintf(
  16. 'CREATE DATABASE IF NOT EXISTS `%s` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci',
  17. DB_NAME
  18. ));
  19. $pdo->exec(sprintf('USE `%s`', DB_NAME));
  20. echo "✅ 数据库创建/确认成功<br>\n";
  21. // 2. 创建 links 表
  22. $pdo->exec("
  23. CREATE TABLE IF NOT EXISTS `links` (
  24. `id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  25. `title` VARCHAR(100) NOT NULL COMMENT '链接标题',
  26. `url` VARCHAR(500) NOT NULL COMMENT '链接地址',
  27. `description` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '描述',
  28. `icon_svg` TEXT NOT NULL COMMENT 'SVG 图标代码',
  29. `group_name` VARCHAR(50) NOT NULL DEFAULT 'default' COMMENT '分组名称',
  30. `group_label` VARCHAR(50) NOT NULL DEFAULT '' COMMENT '分组显示标签',
  31. `group_hint` VARCHAR(100) NOT NULL DEFAULT '' COMMENT '分组提示文字',
  32. `keywords` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '搜索关键词',
  33. `tag_label` VARCHAR(30) NOT NULL DEFAULT '' COMMENT '标签文字',
  34. `tag_class` VARCHAR(30) NOT NULL DEFAULT '' COMMENT '标签样式类',
  35. `meta_domain` VARCHAR(100) NOT NULL DEFAULT '' COMMENT '域名/元信息',
  36. `sort_order` INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '排序',
  37. `is_private` TINYINT(1) NOT NULL DEFAULT 0 COMMENT '1=需登录,0=公开',
  38. `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  39. INDEX `idx_group` (`group_name`),
  40. INDEX `idx_private` (`is_private`),
  41. INDEX `idx_sort` (`sort_order`)
  42. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
  43. ");
  44. echo "✅ links 表创建/确认成功<br>\n";
  45. // 3. 导入初始数据(如果表为空)
  46. $count = $pdo->query("SELECT COUNT(*) FROM links")->fetchColumn();
  47. if ($count > 0) {
  48. echo "⚠️ links 表已有 {$count} 条记录,跳过数据导入<br>\n";
  49. } else {
  50. $stmt = $pdo->prepare("
  51. INSERT INTO `links` (`title`, `url`, `description`, `icon_svg`, `group_name`, `group_label`, `group_hint`, `keywords`, `tag_label`, `tag_class`, `meta_domain`, `sort_order`, `is_private`)
  52. VALUES (:title, :url, :description, :icon_svg, :group_name, :group_label, :group_hint, :keywords, :tag_label, :tag_class, :meta_domain, :sort_order, :is_private)
  53. ");
  54. $links = [
  55. // ===== 应用 - 公开 =====
  56. [
  57. 'title' => 'Monica',
  58. 'url' => 'https://monica.bcjd.net',
  59. 'description' => '个人关系/联系人管理类应用入口。',
  60. 'icon_svg' => '<svg viewBox="0 0 24 24" fill="none"><path d="M12 21s-7-4.4-9-9.2C1.3 8 3.4 5 6.8 5c1.8 0 3 .9 3.7 1.8C11.2 5.9 12.4 5 14.2 5 17.6 5 19.7 8 21 11.8 19 16.6 12 21 12 21Z" stroke="currentColor" stroke-width="1.7"/></svg>',
  61. 'group_name' => 'apps',
  62. 'group_label' => '应用',
  63. 'group_hint' => '常用入口',
  64. 'keywords' => 'monica crm 联系人 个人 管理',
  65. 'tag_label' => 'App',
  66. 'tag_class' => 'ok',
  67. 'meta_domain' => 'monica.bcjd.net',
  68. 'sort_order' => 1,
  69. 'is_private' => 0,
  70. ],
  71. [
  72. 'title' => 'Gogs',
  73. 'url' => 'https://gogs.bcjd.net',
  74. 'description' => 'Git 仓库服务入口(代码托管/协作)。',
  75. 'icon_svg' => '<svg viewBox="0 0 24 24" fill="none"><path d="M7 7h10v10H7V7Z" stroke="currentColor" stroke-width="1.7"/><path d="M9 9h6M9 12h6M9 15h4" stroke="currentColor" stroke-width="1.7" stroke-linecap="round"/></svg>',
  76. 'group_name' => 'apps',
  77. 'group_label' => '应用',
  78. 'group_hint' => '常用入口',
  79. 'keywords' => 'gogs git 代码 仓库 dev',
  80. 'tag_label' => 'Dev',
  81. 'tag_class' => 'ok',
  82. 'meta_domain' => 'gogs.bcjd.net',
  83. 'sort_order' => 2,
  84. 'is_private' => 0,
  85. ],
  86. [
  87. 'title' => 'Docker',
  88. 'url' => 'https://docker.bcjd.net',
  89. 'description' => '容器管理入口(例如 Portainer)。建议仅管理员使用。',
  90. 'icon_svg' => '<svg viewBox="0 0 24 24" fill="none"><path d="M4 7.5 12 3l8 4.5v9L12 21l-8-4.5v-9Z" stroke="currentColor" stroke-width="1.7" stroke-linejoin="round"/><path d="M12 3v18" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" opacity=".75"/><path d="M4 7.5 12 12l8-4.5" stroke="currentColor" stroke-width="1.7" stroke-linejoin="round" opacity=".75"/></svg>',
  91. 'group_name' => 'apps',
  92. 'group_label' => '应用',
  93. 'group_hint' => '常用入口',
  94. 'keywords' => 'docker portainer 容器 运维 管理',
  95. 'tag_label' => 'Ops',
  96. 'tag_class' => 'ok',
  97. 'meta_domain' => 'docker.bcjd.net',
  98. 'sort_order' => 3,
  99. 'is_private' => 1,
  100. ],
  101. // ===== 运维/管理 - 私有 =====
  102. [
  103. 'title' => 'Webmin',
  104. 'url' => 'https://webmin.bcjd.net',
  105. 'description' => '服务器管理面板入口。请确认账号安全与访问来源。',
  106. 'icon_svg' => '<svg viewBox="0 0 24 24" fill="none"><path d="M12 2l8 4v6c0 5-3.5 9.4-8 10-4.5-.6-8-5-8-10V6l8-4Z" stroke="currentColor" stroke-width="1.7"/></svg>',
  107. 'group_name' => 'ops',
  108. 'group_label' => '运维 / 管理',
  109. 'group_hint' => '<span style="color:var(--danger)">谨慎操作</span>(建议仅管理员使用)',
  110. 'keywords' => 'webmin 运维 管理 面板 linux',
  111. 'tag_label' => 'Admin',
  112. 'tag_class' => 'warn',
  113. 'meta_domain' => 'webmin.bcjd.net',
  114. 'sort_order' => 10,
  115. 'is_private' => 1,
  116. ],
  117. [
  118. 'title' => 'phpMyAdmin',
  119. 'url' => 'https://phpmyadmin.bcjd.net',
  120. 'description' => '数据库管理入口。请避免在不可信网络下使用。',
  121. 'icon_svg' => '<svg viewBox="0 0 24 24" fill="none"><path d="M6 7c0 2.2 2.7 4 6 4s6-1.8 6-4-2.7-4-6-4-6 1.8-6 4Z" stroke="currentColor" stroke-width="1.7"/><path d="M6 7v10c0 2.2 2.7 4 6 4s6-1.8 6-4V7" stroke="currentColor" stroke-width="1.7"/></svg>',
  122. 'group_name' => 'ops',
  123. 'group_label' => '运维 / 管理',
  124. 'group_hint' => '<span style="color:var(--danger)">谨慎操作</span>(建议仅管理员使用)',
  125. 'keywords' => 'phpmyadmin mysql mariadb 数据库 管理',
  126. 'tag_label' => 'DB',
  127. 'tag_class' => 'warn',
  128. 'meta_domain' => 'phpmyadmin.bcjd.net',
  129. 'sort_order' => 11,
  130. 'is_private' => 1,
  131. ],
  132. [
  133. 'title' => 'Mail',
  134. 'url' => 'https://mail.bcjd.net',
  135. 'description' => '邮箱系统入口(WebMail/邮件访问)。建议注意账号与设备安全。',
  136. 'icon_svg' => '<svg viewBox="0 0 24 24" fill="none"><path d="M4 7h16v10H4V7Z" stroke="currentColor" stroke-width="1.7"/><path d="M4 8l8 6 8-6" stroke="currentColor" stroke-width="1.7" stroke-linejoin="round"/></svg>',
  137. 'group_name' => 'ops',
  138. 'group_label' => '运维 / 管理',
  139. 'group_hint' => '<span style="color:var(--danger)">谨慎操作</span>(建议仅管理员使用)',
  140. 'keywords' => 'mail 邮箱 邮件 webmail 收件箱 管理 运维 smtp imap',
  141. 'tag_label' => 'Mail',
  142. 'tag_class' => 'warn',
  143. 'meta_domain' => 'mail.bcjd.net',
  144. 'sort_order' => 12,
  145. 'is_private' => 1,
  146. ],
  147. // ===== 站点 - 公开 =====
  148. [
  149. 'title' => 'www.bcjd.net',
  150. 'url' => 'https://www.bcjd.net',
  151. 'description' => '网站主页/内容入口。你可以把此导航页部署在这里。',
  152. 'icon_svg' => '<svg viewBox="0 0 24 24" fill="none"><path d="M4 10.5 12 4l8 6.5V20a1 1 0 0 1-1 1h-5v-6H10v6H5a1 1 0 0 1-1-1v-9.5Z" stroke="currentColor" stroke-width="1.7" stroke-linejoin="round"/></svg>',
  153. 'group_name' => 'sites',
  154. 'group_label' => '站点',
  155. 'group_hint' => '对外主页 / 内容入口',
  156. 'keywords' => 'bcjd 主站 www 首页',
  157. 'tag_label' => 'Web',
  158. 'tag_class' => 'ok',
  159. 'meta_domain' => '主站入口',
  160. 'sort_order' => 20,
  161. 'is_private' => 0,
  162. ],
  163. [
  164. 'title' => 'www.qyqy.org',
  165. 'url' => 'https://www.qyqy.org',
  166. 'description' => 'qyqy.org 的 www 入口。',
  167. 'icon_svg' => '<svg viewBox="0 0 24 24" fill="none"><path d="M4 6h16v12H4V6Z" stroke="currentColor" stroke-width="1.7"/><path d="M7 9h10M7 12h8M7 15h6" stroke="currentColor" stroke-width="1.7" stroke-linecap="round"/></svg>',
  168. 'group_name' => 'sites',
  169. 'group_label' => '站点',
  170. 'group_hint' => '对外主页 / 内容入口',
  171. 'keywords' => 'qyqy 网站 www',
  172. 'tag_label' => 'Web',
  173. 'tag_class' => '',
  174. 'meta_domain' => '站点入口',
  175. 'sort_order' => 21,
  176. 'is_private' => 0,
  177. ],
  178. [
  179. 'title' => 'www.mahaoyun.com',
  180. 'url' => 'https://www.mahaoyun.com',
  181. 'description' => 'mahaoyun.com 的 www 入口。',
  182. 'icon_svg' => '<svg viewBox="0 0 24 24" fill="none"><path d="M12 3l9 6-9 6-9-6 9-6Z" stroke="currentColor" stroke-width="1.7"/><path d="M3 9v8l9 6 9-6V9" stroke="currentColor" stroke-width="1.7"/></svg>',
  183. 'group_name' => 'sites',
  184. 'group_label' => '站点',
  185. 'group_hint' => '对外主页 / 内容入口',
  186. 'keywords' => 'mahaoyun 网站 www',
  187. 'tag_label' => 'Web',
  188. 'tag_class' => '',
  189. 'meta_domain' => '站点入口',
  190. 'sort_order' => 22,
  191. 'is_private' => 0,
  192. ],
  193. ];
  194. foreach ($links as $link) {
  195. $stmt->execute($link);
  196. }
  197. echo '✅ 已导入 ' . count($links) . ' 条链接数据<br>';
  198. }
  199. echo '<hr><p style="color:green;font-weight:bold">🎉 数据库初始化完成!</p>';
  200. echo '<p>👉 <a href="public/index.php">访问首页</a> | 👉 <a href="public/admin.php">管理面板</a></p>';
  201. echo '<p style="color:red;font-weight:bold">⚠️ 出于安全考虑,建议你现在删除或重命名本文件(db_init.php)!</p>';
  202. } catch (\PDOException $e) {
  203. echo '<p style="color:red">❌ 数据库错误:' . htmlspecialchars($e->getMessage(), ENT_QUOTES, 'UTF-8') . '</p>';
  204. }