forked from fc2blog/blog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlogPluginsModel.php
More file actions
433 lines (399 loc) · 14.5 KB
/
Copy pathBlogPluginsModel.php
File metadata and controls
433 lines (399 loc) · 14.5 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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
<?php
namespace Fc2blog\Model;
use Fc2blog\App;
use Fc2blog\Config;
use Fc2blog\Util\PhpCodeLinter;
use Fc2blog\Web\Session;
class BlogPluginsModel extends Model
{
public static $instance = null;
public function __construct()
{
}
public static function getInstance()
{
if (!self::$instance) {
self::$instance = new BlogPluginsModel();
}
return self::$instance;
}
public function getTableName(): string
{
return 'blog_plugins';
}
public function getAutoIncrementCompositeKey(): string
{
return 'blog_id';
}
/**
* バリデート処理
* @param array $data
* @param array|null $valid_data
* @param array $white_list
* @return array
*/
public function validate(array $data, ?array &$valid_data = [], array $white_list = []): array
{
// バリデートを定義
$this->validates = array(
'title' => array(
'required' => true,
'maxlength' => array('max' => 50),
),
'title_align' => array(
'default_value' => 'left',
'in_array' => array('values' => array_keys(self::getAttributeAlign())),
),
'title_color' => array(
'default_value' => '',
'in_array' => array('values' => array_keys(self::getAttributeColor())),
),
'contents' => array(
'required' => true,
'maxlength' => array('max' => 100000),
'own' => array('method' => 'fc2PluginSyntax')
),
'contents_align' => array(
'default_value' => 'left',
'in_array' => array('values' => array_keys(self::getAttributeAlign())),
),
'contents_color' => array(
'default_value' => '',
'in_array' => array('values' => array_keys(self::getAttributeColor())),
),
'device_type' => array(
'default_value' => Config::get('DEVICE_PC'),
'in_array' => array('values' => array_keys(Config::get('DEVICE_NAME'))),
),
'category' => array(
'default_value' => 1,
'in_array' => array('values' => array(1, 2, 3)),
),
);
$errors = parent::validate($data, $valid_data, $white_list);
if (!empty($errors)) {
return $errors;
}
// title_align,title_color...をattributesに纏める
if (in_array('title_align', $white_list) || in_array('title_color', $white_list)
|| in_array('contents_align', $white_list) || in_array('contents_color', $white_list)
) {
$attribute = array(
'title_align' => $valid_data['title_align'],
'title_color' => $valid_data['title_color'],
'contents_align' => $valid_data['contents_align'],
'contents_color' => $valid_data['contents_color'],
);
$valid_data['attribute'] = json_encode($attribute);
unset($valid_data['title_align'], $valid_data['title_color'], $valid_data['contents_align'], $valid_data['contents_color']);
}
return $errors;
}
/**
* 文字方向の設定
*/
public static function getAttributeAlign(): array
{
return array(
'left' => __('Flush left'),
'center' => __('Center justification'),
'right' => __('Right justification'),
);
}
/**
* 文字色の設定
* @param bool $text
* @return array
*/
public static function getAttributeColor($text = false): array
{
if ($text) {
return array(
'' => __('Nothing'),
'red' => __('Red'),
'green' => __('Green'),
'blue' => __('Blue'),
'purple' => __('Purple'),
'pink' => __('Pink'),
'orange' => __('Orange'),
'navy' => __('Navy'),
'gray' => __('Gray'),
);
}
return array(
'' => __('Nothing'),
'red' => '<span style="color:red">■</span>',
'green' => '<span style="color:green">■</span>',
'blue' => '<span style="color:blue">■</span>',
'purple' => '<span style="color:purple">■</span>',
'pink' => '<span style="color:pink">■</span>',
'orange' => '<span style="color:orange">■</span>',
'navy' => '<span style="color:navy">■</span>',
'gray' => '<span style="color:gray">■</span>',
);
}
/**
* FC2テンプレートの構文チェック
* @param string $php_code
* @return string|true
*/
public static function fc2PluginSyntax(string $php_code)
{
// フォルダが存在しない場合作成
$plugin_path = Config::get('BLOG_TEMPLATE_DIR') . App::getBlogLayer(Session::get('blog_id')) . '/plugins/syntax.php';
$plugin_dir = dirname($plugin_path);
if (!file_exists($plugin_dir)) {
mkdir($plugin_dir, 0777, true);
}
// HTMLをPHPテンプレートに変換してテンプレートファイルの作成
$html = BlogTemplatesModel::convertFC2Template($php_code);
file_put_contents($plugin_path, $html);
chmod($plugin_path, 0777);
// PHPのシンタックスチェック
if (PhpCodeLinter::isParsablePhpCode($html)) {
return true;
} else {
return __('There may be a problem with the template or plug-in, installed in the blog.');
}
}
/**
* カテゴリー毎のプラグイン一覧
* @param $blog_id
* @param $device_type
* @return array[]
*/
public function getCategoryPlugins($blog_id, $device_type): array
{
$options = array(
'where' => 'blog_id=? AND device_type=?',
'params' => array($blog_id, $device_type),
'order' => 'category ASC, plugin_order ASC',
);
$blog_plugins = $this->find('all', $options);
$category_blog_plugins = array(1 => array());
if ($device_type == Config::get('DEVICE_PC')) {
// PC版のみ3つまでカテゴリーが存在する
$category_blog_plugins = array(1 => array(), 2 => array(), 3 => array());
}
foreach ($blog_plugins as $blog_plugin) {
$category_blog_plugins[$blog_plugin['category']][] = $blog_plugin;
}
return $category_blog_plugins;
}
/**
* idとblog_idの複合キーからデータを取得
* attributeデータを振り分け
* @param $id
* @param string|null $blog_id
* @param array $options
* @return array|mixed
*/
public function findByIdAndBlogId($id, ?string $blog_id, $options = []): array
{
$data = parent::findByIdAndBlogId($id, $blog_id, $options);
if (empty($data)) {
return $data;
}
$attribute = json_decode($data['attribute']);
if (empty($attribute)) {
$data['title_align'] = $data['contents_align'] = 'left';
$data['title_color'] = $data['contents_color'] = '';
} else {
$data['title_align'] = $attribute->title_align;
$data['title_color'] = $attribute->title_color;
$data['contents_align'] = $attribute->contents_align;
$data['contents_color'] = $attribute->contents_color;
}
return $data;
}
/**
* デバイスタイプとカテゴリーの条件にマッチしたプラグインを返却
* @param $device_type
* @param $category
* @param $blog_id
* @return mixed
*/
public function findByDeviceTypeAndCategory($device_type, $category, $blog_id)
{
$options = array(
'where' => 'blog_id=? AND device_type=? AND category=? AND display=' . Config::get('APP.DISPLAY.SHOW'),
'params' => array($blog_id, $device_type, $category),
'order' => 'plugin_order ASC',
);
$plugins = $this->find('all', $options);
foreach ($plugins as $key => $value) {
$attribute = json_decode($value['attribute']);
if (empty($attribute)) {
$plugins[$key]['title_align'] = $plugins[$key]['contents_align'] = 'left';
$plugins[$key]['title_color'] = $plugins[$key]['contents_color'] = '';
} else {
$plugins[$key]['title_align'] = $attribute->title_align;
$plugins[$key]['title_color'] = $attribute->title_color;
$plugins[$key]['contents_align'] = $attribute->contents_align;
$plugins[$key]['contents_color'] = $attribute->contents_color;
}
}
return $plugins;
}
/**
* 最後の表示順を取得する
* @param $blog_id
* @param $device_type
* @param $category
* @return int
*/
public function getNextPluginOrder($blog_id, $device_type, $category): int
{
$plugin_order = $this->find('one', array(
'fields' => 'plugin_order',
'where' => 'blog_id=? AND device_type=? AND category=?',
'params' => array($blog_id, $device_type, $category),
'order' => 'plugin_order DESC',
'limit' => 1,
));
if (empty($plugin_order)) {
return 0;
}
return $plugin_order + 1;
}
/**
* テンプレートの作成
* @param $values
* @param array $options
* @return array|false|int|mixed
*/
public function insert($values, $options = array())
{
$default_values = [
'list' => '',
'attribute' => '',
];
$values += $default_values;
$values['updated_at'] = $values['created_at'] = date('Y-m-d H:i:s');
$values['plugin_order'] = $this->getNextPluginOrder($values['blog_id'], $values['device_type'], $values['category']);
$id = parent::insert($values, $options);
if ($id) {
// プラグインのPHPファイル作成
self::createPlugin($values['contents'], $values['blog_id'], $id);
}
return $id;
}
/**
* テンプレートの更新
* @param array $values
* @param $id
* @param string $blog_id
* @param array $options
* @return bool
*/
public function updateByIdAndBlogId(array $values, $id, string $blog_id, array $options = array()): bool
{
$values['updated_at'] = date('Y-m-d H:i:s');
if (!parent::updateByIdAndBlogId($values, $id, $blog_id, $options)) {
return false;
}
// プラグインのPHPファイル作成
if (isset($values['contents'])) {
self::createPlugin($values['contents'], $blog_id, $id);
}
return true;
}
/**
* 表示方法の変更を行う
* params => array([id=>display], ...)の形式
* @param array $params
* @param string $blog_id
* @return bool
*/
public function updateDisplay(array $params, string $blog_id)
{
if (!count($params)) {
return false;
}
$displays = array();
$displays[Config::get('APP.DISPLAY.SHOW')] = array();
$displays[Config::get('APP.DISPLAY.HIDE')] = array();
foreach ($params as $id => $display) {
// show,hide以外のdisplayは更新対象としない
if (isset($displays[$display])) {
$displays[$display][] = $id;
}
}
$ret = true;
foreach ($displays as $display => $values) {
if (!count($values)) {
continue;
}
$where = 'blog_id=? AND id IN (' . implode(',', array_fill(0, count($values), '?')) . ')';
$ret = $ret && $this->update(array('display' => $display), $where, array_merge(array($blog_id), $values));
}
return $ret;
}
/**
* idとblog_idをキーとした削除 + ファイル削除も行う
* @param $id
* @param string $blog_id
* @param array $options
* @return array|false|int|mixed
*/
public function deleteByIdAndBlogId($id, string $blog_id, array $options = array())
{
// プラグインファイルの削除
$plugin_file = App::getPluginFilePath($blog_id, $id);
is_file($plugin_file) && unlink($plugin_file);
// 本体削除
return parent::deleteByIdAndBlogId($id, $blog_id, $options);
}
/**
* 並べ替えて更新
* [id] => array(order=>x, category=>x)の形
* @param array $sort_values
* @param string $device_type
* @param string $blog_id
*/
public function sort(array $sort_values, string $device_type, string $blog_id)
{
$blog_plugins = $this->find('all', array(
'fields' => array('id, plugin_order, category'),
'where' => 'blog_id=? AND device_type=?',
'params' => array($blog_id, $device_type),
));
$ids = array_keys($sort_values);
foreach ($blog_plugins as $blog_plugin) {
if (!in_array($blog_plugin['id'], $ids)) {
$this->deleteByIdAndBlogId($blog_plugin['id'], $blog_id);
continue;
}
$values = $sort_values[$blog_plugin['id']];
if ($blog_plugin['plugin_order'] == $values['order'] && $blog_plugin['category'] == $values['category']) {
// 順序、カテゴリーに変更なし
continue;
}
// 並べ替えの順序で更新
$params = array(
'plugin_order' => intval($values['order']),
'category' => intval($values['category']),
);
$this->updateByIdAndBlogId($params, $blog_plugin['id'], $blog_id);
}
}
/**
* テンプレートを作成
* @param string $html
* @param string $blog_id
* @param string $id
*/
public static function createPlugin(string $html, string $blog_id, string $id = 'preview'): void
{
// フォルダが存在しない場合作成
$plugin_path = App::getPluginFilePath($blog_id, $id);
$plugin_dir = dirname($plugin_path);
if (!file_exists($plugin_dir)) {
mkdir($plugin_dir, 0777, true);
}
// HTMLをPHPテンプレートに変換してテンプレートファイルの作成
$html = BlogTemplatesModel::convertFC2Template($html);
file_put_contents($plugin_path, $html);
chmod($plugin_path, 0777);
}
}