-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathConsole.php
executable file
·157 lines (135 loc) · 3.89 KB
/
Console.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
153
154
155
156
157
<?php
namespace TypechoPlugin\CommentToMail;
use \Typecho\{Widget};
use \Typecho\Widget\Helper\Form;
use \Typecho\Widget\Helper\Form\Element\{Text, Hidden, Submit, Textarea};
/**
* CommentToMail
* Typecho 异步评论邮件提醒插件
*
* @copyright Copyright (c) 2022 xcsoft
* @license GNU General Public License 3.0
*/
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* Console
*
* @package CommentToMail
*/
class Console extends Widget
{
/**
* 模板文件目录
*
* @var string
*/
private string $_template_dir = __DIR__ . '/template/';
/**
* 当前文件
*
* @var string
*/
private string $_currentFile;
/**
* 执行函数
*
* @return void
* @throws \Typecho\Widget\Exception
*/
public function execute()
{
$this->widget('Widget_User')->pass('administrator');
$files = glob($this->_template_dir . '*.html');
$this->_currentFile = $this->request->get('file', 'owner.html');
if (preg_match("/^([_0-9a-z-\.\ ])+$/i", $this->_currentFile) && file_exists($this->_template_dir . $this->_currentFile)) {
foreach ($files as $file) {
if (!file_exists($file)) continue;
$file = basename($file);
$this->push(array(
'file' => $file,
'current' => ($file == $this->_currentFile)
));
}
return;
}
throw new \Typecho\Widget\Exception('模板文件不存在', 404);
}
/**
* 获取菜单标题
*
* @return string
*/
public function getMenuTitle(): string
{
return _t('编辑文件 %s', $this->_currentFile);
}
/**
* 获取文件内容
*
* @return string
*/
public function currentContent(): string
{
return htmlspecialchars(file_get_contents($this->_template_dir . $this->_currentFile));
}
/**
* 获取文件是否可读
*
* @return string
*/
public function currentIsWriteable(): string
{
return is_writeable($this->_template_dir . $this->_currentFile);
}
/**
* 获取当前文件
*
* @return string
*/
public function currentFile(): string
{
return $this->_currentFile;
}
/**
* 邮件测试表单
*
* @return Form
*/
public function testMailForm(): Form
{
/** 构建表单 */
$options = Widget::widget('Widget_Options');
$form = new Form(
\Typecho\Common::url('/action/' . Plugin::$_action, $options->index),
Form::POST_METHOD
);
/** 收件人名称 */
$toName = new Text('toName', NULL, NULL, _t('收件人名称'), _t('为空则使用博主昵称'));
$form->addInput($toName);
/** 收件人邮箱 */
$to = new Text('to', NULL, NULL, _t('收件人邮箱'), _t('为空则使用博主邮箱'));
$form->addInput($to);
/** 邮件标题 */
$title = new Text('title', NULL, NULL, _t('邮件标题 *'));
$form->addInput($title);
/** 邮件内容 */
$content = new Textarea('content', NULL, NULL, _t('邮件内容 *'));
$content->input->setAttribute('class', 'w-100 mono');
$form->addInput($content);
/** 动作 */
$do = new Hidden('do');
$form->addInput($do);
/** 提交按钮 */
$submit = new Submit();
$submit->input->setAttribute('class', 'btn primary');
$form->addItem($submit);
/** 设置值 */
$do->value('testMail');
$submit->value('发送邮件');
/** 添加规则 */
$to->addRule('email', _t('非法的邮件地址'));
$title->addRule('required', _t('邮件标题不能为空'));
$content->addRule('required', _t('邮件内容不能为空'));
return $form;
}
}