Repository navigation
Expand file tree
/
Copy pathAction.php
More file actions
288 lines (247 loc) · 8.86 KB
/
Copy pathAction.php
File metadata and controls
288 lines (247 loc) · 8.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* 友链RSS Action处理器
*/
class FriendsRSS_Action extends Typecho_Widget
{
/**
* 执行Action
*/
public function execute()
{
$this->on($this->request->is('do=rss'))->rss();
$this->on($this->request->is('do=page'))->pageData();
$this->on($this->request->is('do=pageview'))->page();
$this->on($this->request->is('do=clear'))->clearCache();
$this->on($this->request->is('do=detect'))->detectRSS();
$this->on($this->request->is('do=refresh'))->refreshData();
$this->on($this->request->is('do=stats'))->getStats();
$this->on($this->request->is('do=cron'))->cronTask();
}
/**
* 输出RSS
*/
public function rss()
{
require_once __DIR__ . '/Core.php';
$core = new FriendsRSS_Core();
$articles = $core->getAggregatedArticles(false); // 不强制刷新,只读取缓存
header('Content-Type: application/rss+xml; charset=utf-8');
$options = Typecho_Widget::widget('Widget_Options');
$siteUrl = $options->siteUrl;
$title = $options->title . ' - 友链RSS聚合';
$description = '来自友链博客的最新文章聚合';
echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
echo '<rss version="2.0">' . "\n";
echo '<channel>' . "\n";
echo '<title>' . htmlspecialchars($title) . '</title>' . "\n";
echo '<link>' . htmlspecialchars($siteUrl) . '</link>' . "\n";
echo '<description>' . htmlspecialchars($description) . '</description>' . "\n";
echo '<language>zh-CN</language>' . "\n";
echo '<lastBuildDate>' . date('r') . '</lastBuildDate>' . "\n";
foreach ($articles as $article) {
echo '<item>' . "\n";
echo '<title>' . htmlspecialchars($article['title']) . '</title>' . "\n";
echo '<link>' . htmlspecialchars($article['link']) . '</link>' . "\n";
echo '<description>' . htmlspecialchars($article['description']) . '</description>' . "\n";
echo '<author>' . htmlspecialchars($article['author']) . '</author>' . "\n";
echo '<pubDate>' . date('r', $article['pubDate']) . '</pubDate>' . "\n";
echo '<guid>' . htmlspecialchars($article['link']) . '</guid>' . "\n";
echo '</item>' . "\n";
}
echo '</channel>' . "\n";
echo '</rss>' . "\n";
exit;
}
/**
* 返回页面数据 (JSON API)
*/
public function pageData()
{
header('Content-Type: application/json; charset=utf-8');
try {
$options = Typecho_Widget::widget('Widget_Options');
$pluginOptions = $options->plugin('FriendsRSS');
if (!$pluginOptions->enableFrontend) {
echo json_encode([
'success' => false,
'error' => '前台页面已禁用'
]);
exit;
}
require_once __DIR__ . '/Core.php';
$core = new FriendsRSS_Core();
// 检查是否强制刷新
$forceRefresh = isset($_GET['refresh']) && $_GET['refresh'] == '1';
$articles = $core->getAggregatedArticles($forceRefresh);
$stats = $core->getStats();
echo json_encode([
'success' => true,
'articles' => $articles,
'stats' => $stats
]);
} catch (Exception $e) {
echo json_encode([
'success' => false,
'error' => $e->getMessage()
]);
}
exit;
}
/**
* 显示前台页面
*/
public function page()
{
$options = Typecho_Widget::widget('Widget_Options');
$pluginOptions = $options->plugin('FriendsRSS');
if (!$pluginOptions->enableFrontend) {
throw new Typecho_Widget_Exception('前台页面已禁用', 404);
}
require_once __DIR__ . '/Core.php';
$core = new FriendsRSS_Core();
$articles = $core->getAggregatedArticles(false); // 不强制刷新,只读取缓存
$stats = $core->getStats();
// 创建模拟的$this对象供模板使用
$templateThis = new stdClass();
$templateThis->options = $options;
include_once __DIR__ . '/template/page.php';
exit;
}
/**
* 清除缓存
*/
public function clearCache()
{
// 检查权限
$user = Typecho_Widget::widget('Widget_User');
if (!$user->hasLogin() || !$user->pass('administrator')) {
throw new Typecho_Widget_Exception('权限不足', 403);
}
require_once __DIR__ . '/Core.php';
$core = new FriendsRSS_Core();
$result = $core->clearCache();
$this->response->goBack();
}
/**
* RSS检测API
*/
public function detectRSS()
{
header('Content-Type: application/json; charset=utf-8');
try {
// 检查权限
$user = Typecho_Widget::widget('Widget_User');
if (!$user->hasLogin() || !$user->pass('administrator')) {
echo json_encode([
'success' => false,
'error' => '权限不足'
]);
exit;
}
require_once __DIR__ . '/Core.php';
$core = new FriendsRSS_Core();
// 获取友链列表
$links = $core->getFriendLinks();
if (empty($links)) {
echo json_encode([
'success' => false,
'error' => '没有找到友链'
]);
exit;
}
// 批量检测RSS
$results = $core->batchDetectRSS($links);
echo json_encode([
'success' => true,
'results' => $results,
'total' => count($links),
'detected' => array_sum(array_column($results, 'success'))
]);
} catch (Exception $e) {
echo json_encode([
'success' => false,
'error' => $e->getMessage()
]);
}
exit;
}
/**
* 刷新数据API
*/
public function refreshData()
{
header('Content-Type: application/json; charset=utf-8');
try {
require_once __DIR__ . '/Core.php';
$core = new FriendsRSS_Core();
// 强制刷新聚合文章
$articles = $core->getAggregatedArticles(true);
$stats = $core->getStats();
echo json_encode([
'success' => true,
'articles' => $articles,
'stats' => $stats,
'timestamp' => time()
]);
} catch (Exception $e) {
echo json_encode([
'success' => false,
'error' => $e->getMessage()
]);
}
exit;
}
/**
* 获取统计信息API
*/
public function getStats()
{
header('Content-Type: application/json; charset=utf-8');
try {
require_once __DIR__ . '/Core.php';
$core = new FriendsRSS_Core();
$stats = $core->getStats();
$cacheStats = $core->getCacheStats();
echo json_encode([
'success' => true,
'stats' => $stats,
'cache' => $cacheStats
]);
} catch (Exception $e) {
echo json_encode([
'success' => false,
'error' => $e->getMessage()
]);
}
exit;
}
/**
* 定时任务处理器(检测 + 解析)
*/
public function cronTask()
{
// 软定时/后台任务:客户端断开后继续跑完
ignore_user_abort(true);
@set_time_limit(600);
// 检查是否有定时任务密钥(可选的安全验证)
$secret = isset($_GET['secret']) ? $_GET['secret'] : '';
$options = Typecho_Widget::widget('Widget_Options');
// 如果设置了密钥,需要验证
if ($secret && $secret !== md5($options->siteUrl . 'friends_rss_cron')) {
http_response_code(403);
exit('Access Denied');
}
try {
require_once __DIR__ . '/Core.php';
$core = new FriendsRSS_Core();
$result = $core->runScheduledTasks(false);
echo implode('; ', $result['messages']);
} catch (Exception $e) {
http_response_code(500);
echo "Cron error: " . $e->getMessage();
}
exit;
}
}