-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathPlugin.php
152 lines (138 loc) · 5.15 KB
/
Plugin.php
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
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) {
exit;
}
/**
* Typecho Restful 插件
*
* @package Restful
* @author MoeFront Studio
* @version 1.2.0
* @link https://moefront.github.io
*/
class Restful_Plugin implements Typecho_Plugin_Interface
{
const ACTION_CLASS = 'Restful_Action';
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function activate()
{
$routes = call_user_func(array(self::ACTION_CLASS, 'getRoutes'));
foreach ($routes as $route) {
Helper::addRoute($route['name'], $route['uri'], self::ACTION_CLASS, $route['action']);
}
Typecho_Plugin::factory('Widget_Feedback')->comment = array(__CLASS__, 'comment');
return '_(:з」∠)_';
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function deactivate()
{
$routes = call_user_func(array(self::ACTION_CLASS, 'getRoutes'));
foreach ($routes as $route) {
Helper::removeRoute($route['name']);
}
return '( ≧Д≦)';
}
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form)
{
echo '<button type="button" class="btn" style="outline: 0" onclick="restfulUpgrade(this)">' . _t('检查并更新插件'). '</button>';
$prefix = defined('__TYPECHO_RESTFUL_PREFIX__') ? __TYPECHO_RESTFUL_PREFIX__ : '/api/';
/* API switcher */
$routes = call_user_func(array(self::ACTION_CLASS, 'getRoutes'));
echo '<h3>API 状态设置</h3>';
foreach ($routes as $route) {
if ($route['shortName'] == 'upgrade') {
continue;
}
$tmp = new Typecho_Widget_Helper_Form_Element_Radio($route['shortName'], array(
0 => _t('禁用'),
1 => _t('启用'),
), 1, $route['uri'], _t($route['description']));
$form->addInput($tmp);
}
/* cross-origin settings */
$origin = new Typecho_Widget_Helper_Form_Element_Textarea('origin', null, null, _t('域名列表'), _t('一行一个<br>以下是例子qwq<br>http://localhost:8080<br>https://blog.example.com<br>若不限制跨域域名,请使用通配符 *。'));
$form->addInput($origin);
/* custom field privacy */
$fieldsPrivacy = new Typecho_Widget_Helper_Form_Element_Text('fieldsPrivacy', null, null, _t('自定义字段过滤'), _t('过滤掉不希望在获取文章信息时显示的自定义字段名称。使用半角英文逗号分隔,例如 fields1,fields2 .'));
$form->addInput($fieldsPrivacy);
/* allowed options attribute */
$allowedOptions = new Typecho_Widget_Helper_Form_Element_Text('allowedOptions', null, null, _t('自定义设置项白名单'), _t('默认情况下 /api/settings 只会返回一些安全的站点设置信息。若有需要你可以在这里指定允许返回的存在于 typecho_options 表中的字段,并通过 ?key= 参数请求。使用半角英文逗号分隔每个 key, 例如 keywords,theme .'));
$form->addInput($allowedOptions);
/* CSRF token salt */
$csrfSalt = new Typecho_Widget_Helper_Form_Element_Text('csrfSalt', null, '05faabd6637f7e30c797973a558d4372', _t('CSRF加密盐'), _t('请务必修改本参数,以防止跨站攻击。'));
$form->addInput($csrfSalt);
?>
<script>
function restfulUpgrade(e) {
var originalText = e.innerHTML;
var waitingText = '<?php echo _t('请稍后...');?>';
if (e.innerHTML === waitingText) {
return;
}
e.innerHTML = waitingText;
var x = new XMLHttpRequest();
x.open('GET', '<?php echo rtrim(Helper::options()->index, '/') . $prefix . 'upgrade';?>', true);
x.onload = function() {
var data = JSON.parse(x.responseText);
if (x.status >= 200 && x.status < 400) {
if (data.status === 'success') {
alert('<?php echo _t('更新成功,您可能需要禁用插件再启用。');?>');
} else {
alert(data.message);
}
} else {
alert(data.message);
}
};
x.onerror = function() {
alert('<?php echo _t('网络异常,请稍后再试');?>');
};
x.send();
}
</script>
<?php
}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form)
{}
/**
* 构造评论真实IP
*
* @return array
*/
public static function comment($comment, $post)
{
$request = Typecho_Request::getInstance();
$customIp = $request->getServer('HTTP_X_TYPECHO_RESTFUL_IP');
if ($customIp != null) {
$comment['ip'] = $customIp;
}
return $comment;
}
}