better-staridc-MNBT
1<?php
2/**
3 * 售卖前端 - 工具函数库
4 * 提供:配置读取、URL 生成、用户/套餐查询、统一视图渲染。
5 */
6if (!defined('IN_CRONLITE')) { exit; }
7
8function shop_frontend_asset_url(string $path): string {
9 $slug = 'shop_frontend';
10 if (function_exists('mnbt_plugin_url')) {
11 return mnbt_plugin_url($slug, 'assets/' . ltrim($path, '/'));
12 }
13 return '../app_plugins/' . $slug . '/assets/' . ltrim($path, '/');
14}
15
16function shop_frontend_option(string $key, $default = null) {
17 if (function_exists('mnbt_plugin_option_get')) {
18 return mnbt_plugin_option_get('shop_frontend', $key, $default);
19 }
20 return $default;
21}
22
23function shop_frontend_get_current_user(): ?array {
24 if (empty($_COOKIE['account_token'])) return null;
25 $token = $_COOKIE['account_token'];
26 $decoded = authcode($token, 'DECODE', SYS_KEY);
27 if (empty($decoded)) return null;
28 $parts = explode("\t", $decoded, 2);
29 if (count($parts) < 2) return null;
30 [$uid, $hash] = $parts;
31 $uid = (int)$uid;
32 if ($uid <= 0) return null;
33 global $DB;
34 $user = $DB->get_row_prepare("SELECT * FROM MN_plugin_user WHERE id=? AND status=1 LIMIT 1", [$uid]);
35 if (!$user) return null;
36 if (md5($uid . $user['password_hash'] . SYS_KEY) !== $hash) return null;
37 return $user;
38}
39
40function shop_frontend_get_plans(): array {
41 global $DB;
42 // hosting_shop 未启用或表不存在时不报错,直接返回空
43 if (!function_exists('mnbt_plugin_enabled') || !mnbt_plugin_enabled('hosting_shop')) {
44 return [];
45 }
46 $rows = @$DB->get_all_prepare("SELECT * FROM MN_plugin_hosting_plan WHERE status='active' ORDER BY sort ASC, id ASC") ?: [];
47 return $rows;
48}
49
50/**
51 * 生成带站点 base path 前缀的 URL(index.php?_r=/path)。
52 * 与 user_info / balance / hosting_shop 插件 URL 格式完全一致。
53 */
54function shop_frontend_url(string $path = ''): string {
55 $scriptName = isset($_SERVER['SCRIPT_NAME']) ? str_replace('\\', '/', $_SERVER['SCRIPT_NAME']) : '';
56 $basePath = rtrim(str_replace('\\', '/', dirname($scriptName)), '/');
57 if ($basePath === '.' || $basePath === '/') { $basePath = ''; }
58 $p = ltrim($path, '/');
59 $qpos = strpos($p, '?');
60 if ($qpos !== false) {
61 $route = substr($p, 0, $qpos);
62 $query = substr($p, $qpos + 1);
63 return $basePath . '/index.php?_r=/' . $route . '&' . $query;
64 }
65 return $basePath . '/index.php?_r=/' . $p;
66}
67
68/**
69 * 生成核心物理文件 URL(如 /user/idcdl.php),带站点 base path。
70 */
71function shop_frontend_core_url(string $path = ''): string {
72 $scriptName = isset($_SERVER['SCRIPT_NAME']) ? str_replace('\\', '/', $_SERVER['SCRIPT_NAME']) : '';
73 $basePath = rtrim(str_replace('\\', '/', dirname($scriptName)), '/');
74 if ($basePath === '.' || $basePath === '/') { $basePath = ''; }
75 return $basePath . '/' . ltrim($path, '/');
76}
77
78/**
79 * 上传到本插件 assets 的图标 URL 追加版本号(避免浏览器缓存旧图)。
80 * $value 为站点配置里保存的值;$type 为 'logo' 或 'favicon'。
81 */
82function shop_frontend_cached_icon_url($value, string $type): string {
83 if (!is_string($value) || $value === '') return '';
84 $base = 'assets/' . $type . '.ico';
85 if (strpos($value, $base) === false) return $value; // 非本插件上传的自定义 URL 原样返回
86 $file = mnbt_plugin_path('shop_frontend') . $base;
87 if (is_file($file)) {
88 $v = @filemtime($file);
89 if ($v) {
90 return mnbt_plugin_url('shop_frontend', $base) . '?v=' . $v;
91 }
92 }
93 return $value;
94}
95
96/**
97 * 站点品牌信息(后台「前端设置」可配置)。
98 */
99function shop_frontend_brand(): array {
100 return [
101 'title' => shop_frontend_option('site_title', '') ?: 'MNBT 云服务',
102 'logo' => shop_frontend_cached_icon_url(shop_frontend_option('site_logo', ''), 'logo'),
103 'primary' => shop_frontend_option('site_primary', '') ?: '#4f46e5',
104 'footer' => shop_frontend_option('site_footer', ''),
105 ];
106}
107
108/**
109 * 站点 Favicon URL(带缓存版本号)。
110 */
111function shop_frontend_favicon(): string {
112 return shop_frontend_cached_icon_url(shop_frontend_option('site_favicon', ''), 'favicon');
113}
114
115/**
116 * 支付同步返回后应跳转的统一前端地址。
117 * 根据订单业务类型(MN_dd.lx)决定目的地:充值→余额页,主机→订单列表,默认订单列表。
118 */
119function shop_frontend_pay_return_url(): string {
120 global $DB;
121 $no = isset($_GET['out_trade_no']) ? trim((string)$_GET['out_trade_no']) : '';
122 if ($no !== '' && isset($DB)) {
123 $row = @$DB->get_row_prepare("SELECT lx FROM MN_dd WHERE ddh=? LIMIT 1", [$no]);
124 if (is_array($row)) {
125 if (($row['lx'] ?? '') === 'recharge') {
126 return shop_frontend_url('balance');
127 }
128 if (($row['lx'] ?? '') === 'hosting') {
129 return shop_frontend_url('shop/orders');
130 }
131 }
132 }
133 return shop_frontend_url('shop/orders');
134}
135
136/**
137 * 生成品牌 Logo 缩写标记(最多 2 个字符,兼容无 mbstring 环境)。
138 */
139function shop_frontend_brand_mark(string $title): string {
140 if ($title === '') return 'MN';
141 if (function_exists('mb_substr')) return mb_substr($title, 0, 2, 'UTF-8');
142 return substr($title, 0, 2);
143}
144
145/**
146 * 渲染统一视图(视图末尾 include layout.php)。
147 *
148 * 注入变量:current_user / asset_url / has_balance / has_hosting /
149 * brand(站点标题)/ brand_logo / brand_primary / brand_footer / brand_favicon
150 * 以及各页面传入的业务数据。
151 */
152function shop_frontend_render(string $view, array $vars = []): void {
153 $brandInfo = shop_frontend_brand();
154 $vars['current_user'] = $vars['current_user'] ?? shop_frontend_get_current_user();
155 $vars['asset_url'] = function_exists('mnbt_plugin_url') ? mnbt_plugin_url('shop_frontend', 'assets/') : '/app_plugins/shop_frontend/assets/';
156 $vars['has_balance'] = function_exists('balance_get');
157 $vars['has_hosting'] = function_exists('hosting_plan_list_active');
158 $vars['brand'] = $brandInfo['title'];
159 $vars['brand_logo'] = $brandInfo['logo'];
160 $vars['brand_primary'] = $brandInfo['primary'];
161 $vars['brand_footer'] = $brandInfo['footer'];
162 $vars['brand_favicon'] = shop_frontend_favicon();
163 extract($vars, EXTR_SKIP);
164 $viewFile = mnbt_plugin_path('shop_frontend') . 'views/' . $view . '.php';
165 if (!is_file($viewFile)) {
166 http_response_code(500);
167 echo 'View not found: ' . htmlspecialchars($view);
168 return;
169 }
170 include $viewFile;
171}