Repository navigation
Expand file tree
/
Copy pathplugin.php
More file actions
326 lines (284 loc) · 12.1 KB
/
Copy pathplugin.php
File metadata and controls
326 lines (284 loc) · 12.1 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<?php
class pluginSettingsPlus extends Plugin
{
private $pluginName;
private $cssFile;
private $uploadsDir;
private $faviconAdminPath = '';
public function init()
{
// プラグイン名
$this->pluginName = substr(__CLASS__, 6); //先頭のpluginを除いたクラス名
// プラグインフォルダ
$this->cssFile = $this->domainPath() . 'css/settingsplus.css';
// Bludit の標準アップロードディレクトリ
$this->uploadsDir = PATH_UPLOADS . $this->pluginName . DS;
if (!is_dir($this->uploadsDir)) {
@mkdir($this->uploadsDir, 0755, true);
}
// adminのファビコンパスを初期化
$this->faviconAdminPath = HTML_PATH_UPLOADS . $this->pluginName . '/';
// データベースフィールドの初期値を設定
$this->dbFields = array(
'displaySiteTitle' => true,
'displayContentCategoryName' => false,
'faviconAdmin' => '',
'enableFaviconAdmin' => false,
'useFontAwesome' => false,
'awesomeURL' => 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/7.3.1/css/all.min.css',
'maintenanceMode' => false,
'maintenanceMessage' => 'The site is currently in maintenance mode.',
'hideLoginPage' => false,
'loginToken' => 'OpenSesami'
);
}
/* ---------------------------------------------------------
* 管理画面フォーム
* --------------------------------------------------------- */
public function form()
{
global $page;
global $layout;
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
// HTML生成は render_form.php に委譲
// includeなので$pageや$layout変数をそのまま使用できる
$html_render = ''; // use in render_form.php
include($this->phpPath() . 'render_form.php');
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
return $html_render;
}
/* ---------------------------------------------------------
* 管理画面 POST
* --------------------------------------------------------- */
public function post()
{
// 親クラスの post() を呼び出して自動マッピングを実行
// これにより displaySiteTitle などが自動的にDBに保存される
parent::post();
// 追加処理が必要な場合は以下に記述する
$file = $_FILES['faviconAdminFile'] ?? null;
// ファイルアップロード処理
if (isset($file)) {
$uploadDir = PATH_UPLOADS . $this->pluginName . DS;
if (!is_dir($uploadDir)) {
@mkdir($uploadDir, 0755, true);
}
$allowedTypes = ['image/png'];
$allowedExtensions = ['png'];
// ファイルタイプチェック
if (!in_array($file['type'], $allowedTypes)) {
return;
}
// ファイル拡張子チェック
$ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
if (!in_array($ext, $allowedExtensions)) {
return;
}
// 新しいファイル名を生成
$newFilename = 'faviconAdmin.' . $ext;
$filepath = $uploadDir . $newFilename;
// パスをデータベースに保存(相対パス)
$this->db['faviconAdmin'] = HTML_PATH_UPLOADS . $this->pluginName . '/' . $newFilename;
// ファイルを移動
if (move_uploaded_file($file['tmp_name'], $filepath)) {
// 古いファイルを削除
$oldFaviconPath = $this->getValue('faviconAdmin');
if (!empty($oldFaviconPath)) {
// getValue() が返すのは URL パス(例:uploads/SettingsPlus/favicon.ico)
// 実際のファイルパスは PATH_UPLOADS + SettingsPlus + ファイル名
$oldFilename = basename($oldFaviconPath);
$oldFilepath = $uploadDir . $oldFilename;
if (file_exists($oldFilepath) && $oldFilepath !== $filepath) {
@unlink($oldFilepath);
}
}
}
else {
// ファイル移動失敗時は新しいパス設定を取り消す
$this->db['faviconAdmin'] = '';
}
}
return $this->save();
}
/* ---------------------------------------------------------
* Web site のHTMLを拡張
* --------------------------------------------------------- */
public function siteHead()
{
// // use Font Awesome
// if ($this->getValue('useFontAwesome')) {
// echo '<link rel="stylesheet" href="' . $this->getValue('awesomeURL') . '">';
// }
}
public function siteBodyBegin()
{
ob_start();
}
public function siteBodyEnd()
{
$html = ob_get_clean();
$maintenanceMessage = $this->getValue('maintenanceMessage');
// Maintenance mode notice
if ($this->getValue('maintenanceMode')) {
$html = '<html><body>' .
'<div class="shadow-lg alert alert-info mx-5 px-5 text-center" role="alert">' .
'<h4 class="alert-heading">Maintenance Mode</h4>' .
$maintenanceMessage . '</div>' .
'</body></html>';
}
return $html;
}
/* ---------------------------------------------------------
* Admin ページのHTMLを拡張
* --------------------------------------------------------- */
public function beforeAdminLoad()
{
global $layout;
// Hide login page
if($layout['view'] == 'login') {
if ($this->getValue('hideLoginPage')) {
$query = $_SERVER['QUERY_STRING'];
$token = isset($_GET['token']) ? $_GET['token'] : '';
if ($token !== $this->getValue('loginToken')) {
header('HTTP/1.1 404 Not Found');
exit;
}
}
}
// Favicon in the admin
if (!$this->getValue('enableFaviconAdmin')) {
return;
}
ob_start();
}
public function afterAdminLoad()
{
// Favicon in the admin
if (!$this->getValue('enableFaviconAdmin')) {
return;
}
$html = ob_get_clean();
$replaceMessage = '<!-- Favicon replaced by SettingsPlus -->';
$faviconURL = PATH_UPLOADS . $this->pluginName . DS . basename($this->getValue('faviconAdmin'));
if (file_exists($faviconURL))
{
$faviconWidth = $faviconURL ? getimagesize($faviconURL)[0] : 0;
$faviconHeight = $faviconURL ? getimagesize($faviconURL)[1] : 0;
$newFavicon = '<link rel="icon" type="image/png"' . ' sizes="' . $faviconWidth . 'x' . $faviconHeight . '" href="' . DOMAIN_BASE . ltrim($this->getValue('faviconAdmin'), '/') . '?' . time() . '">';
$pattern = '/<link[^>]*rel="[^"]*"[^>]*href="[^"]*favicon[^"]*"[^>]*>/i';
if (preg_match($pattern, $html)) {
// マッチした(元々あった)タグを、新しいタグに完全に置換する
$html = preg_replace($pattern, $replaceMessage . $newFavicon, $html);
} else {
// 万が一マッチしなかった場合の保険として、</head> の直前に強制挿入
$html = str_replace('</head>', $replaceMessage . $newFavicon . "\n</head>", $html);
}
}
echo $html;
}
public function adminHead()
{
// Font Awesome の読み込み
if ($this->getValue('useFontAwesome')) {
echo '<link rel="stylesheet" href="' . $this->getValue('awesomeURL') . '">';
}
}
public function adminBodyBegin()
{
// Maintenance mode notice
if ($this->getValue('maintenanceMode')) {
echo '<div class="alert alert-warning my-0 py-1" role="alert">' . 'The site is currently in maintenance mode.' . '</div>';
}
}
public function adminBodyEnd()
{
global $site, $layout, $categories, $pages;
$html = '';
if($layout['view'] == 'content' && $this->getValue('displayContentCategoryName')) {
// ページとカテゴリのマッピングを作成
$items = $pages->getList(1, -1, true);
$pageCategoryMap = [];
foreach ($items as $key) {
$p = buildPage($key);
$k = $p->key();
$c = $p->category();
if(!empty($c)) {
$pageCategoryMap[$k] = $c;
}
else {
// カテゴリが空の場合はカテゴリマップに追加しない
// $pageCategoryMap[$k] = '';
}
// echo $p->category() . ': ' . $p->key() . '<br>';
}
// JSONとしてJS変数を定義
$jsCategories = json_encode($pageCategoryMap);
$html .= '<!-- Display content category name -->';
$html .= '<script> var pageCategoryMap = ' . $jsCategories . ';</script>';
$html .= '<script>
$(document).ready(function() {
$("tbody td.contentURL a").each(function() {
var title = $(this).attr("title");
if (title) {
// スラッシュ以降の文字列をキーとして抽出
var parts = title.split("/");
var pageKey = parts.pop();
// マップに存在すればカテゴリを表示
if (pageCategoryMap.hasOwnProperty(pageKey)) {
var categoryName = pageCategoryMap[pageKey];
$(this).closest("td.contentURL").prepend(
\'<span class="align-middle mr-1">\' + "(" + categoryName + ")" + \'</span>\'
);
}
}
});
});
</script>';
}
if ($this->getValue('displaySiteTitle')) {
// Site title in the admin sidebar
$title = $site->title();
$html .= '<script>
$(document).ready(function() {
// BLUDIT テキストを含む li を探す
var bluditLi = $(".nav-item").filter(function() {
return $(this).text().includes("BLUDIT");
});
if (bluditLi.length > 0) {
// 新しい li 要素を作成
var newLi = $("<li>").addClass("nav-item")
.append(
$("<span>").addClass("nav-link alert alert-secondary text-center rounded-pill py-0")
.text("' . htmlspecialchars($title) . '")
);
// BLUDIT の次に挿入
bluditLi.after(newLi);
}
});
</script>';
}
return $html;
}
public function adminSidebar()
{
global $page;
$admin_dir = ADMIN_URI_FILTER;
$pluginName = (__CLASS__);// ここはクラス名そのまま
$url = HTML_PATH_ADMIN_ROOT.'configure-plugin/'.$pluginName;
// サイドバーに「Settings Plus」リンクを追加
$html = '<li class="nav-item">
<a class="nav-link" href="' . $url . '"><span class="fa fa-gears"></span>Settings Plus</a>
</li>';
return $html;
}
public function adminView()
{
}
/* ---------------------------------------------------------
* Utility
* --------------------------------------------------------- */
private function uuid()
{
return bin2hex(random_bytes(16));
}
}