-
Notifications
You must be signed in to change notification settings - Fork 1
/
misskey-Settings.php
113 lines (104 loc) · 3.95 KB
/
misskey-Settings.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
<?php
class MisskeySettings {
public const OptionName = Misskey::Slag.'_options';
private $options;
protected $defaults = [
'url' => 'https://misskey.xyz',
'appSecret' => '',
'i' => ''
];
public function __construct() {
$this->options = get_option(MisskeySettings::OptionName, $this->defaults);
add_action('admin_menu', [$this, 'addMenu']);
}
public function addMenu() {
add_options_page(
Misskey::Name,
Misskey::Name,
'administrator',
Misskey::Slag,
[$this, 'load_plugin_admin_page']
);
}
private function optionUpdates() {
$url = filter_input(INPUT_POST, 'url');
$appSecret = filter_input(INPUT_POST, 'appSecret');
if ($url !== false && $url !== null)
$this->options['url'] = $url;
if ($appSecret !== false && $appSecret !== null)
$this->options['appSecret'] = $appSecret;
update_option(MisskeySettings::OptionName, $this->options);
}
private function setI($i) {
$this->options['i'] = $i;
update_option(MisskeySettings::OptionName, $this->options);
}
private function Auth(&$url) {
$auth = filter_input(INPUT_POST, 'auth');
$authIdx = filter_input(INPUT_POST, 'authIdx');
require_once(__DIR__ . '/lib/Auth.php');
$ma = new MisskeyAuth($this->options['url'], $this->options['appSecret']);
if(!isset($authIdx))
$authIdx = null;
if ($auth !== false && $auth !== null) {
switch($authIdx) {
case 0:
$url = $ma->auth();
$ma->saveSession();
break;
case 1:
$ma->loadSession();
$ma->dropSession();
$at = $ma->getAccessToken();
$this->setI($ma->getI());
break;
case 2:
break;
default:
$authIdx = -1;
}
$authIdx += 0;
++$authIdx;
}else {
$authIdx = -1;
}
return $authIdx;
}
public function load_plugin_admin_page() {
$authURL = "";
$this->optionUpdates();
$authIdx = $this->Auth($authURL);
?>
<h1><?= Misskey::Name ?></h1>
<h2>認証設定</h2>
<form method="post">
<table>
<tr>
<th>URL<br>(URLの末尾に<code>/</code>を含めないでください)</th>
<td><input type="text" name="url" value="<?=esc_attr($this->options['url'])?>"></td>
</tr>
<tr>
<th>appSecret <a href="<?= esc_attr($this->options['url']) ?>/dev" target="_blank">アプリを作る</a><br><code>アカウントの情報を見る</code>,<code>投稿する</code>を権限で付けてください</th>
<td><input type="text" name="appSecret" value="<?= esc_attr($this->options['appSecret']) ?>"></td>
</tr>
</table>
<?php if ($authIdx === 1) : ?>
<p><a href="<?=$authURL?>" target="_blank">こちら</a>を開き、認証してください。</p>
<?php elseif($authIdx === 2): ?>
<p>認証が完了しました(多分</p>
<?php elseif(isset($this->options['i'])):?>
<p>すでに認証しています。</p>
<?php endif; ?>
<p>クソコードなのでたまにスカるときがあります。(正常です)何度か挑戦してみてください</p>
<input type="hidden" name="authIdx" value="<?=$authIdx?>">
<?php
if(!isset($authIdx) || !isset($authURL) || $authURL === "")
submit_button('保存して認証を開始する', 'primary', 'auth');
else
submit_button('認証を続ける', 'primary', 'auth');
submit_button('保存');
?>
</form>
<?php
}
}