-
Notifications
You must be signed in to change notification settings - Fork 122
/
BcUtil.php
542 lines (501 loc) · 13.3 KB
/
BcUtil.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
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
<?php
/**
* baserCMS : Based Website Development Project <https://basercms.net>
* Copyright (c) baserCMS Users Community <https://basercms.net/community/>
*
* @copyright Copyright (c) baserCMS Users Community
* @link https://basercms.net baserCMS Project
* @package Baser.Lib
* @since baserCMS v 3.0.7
* @license https://basercms.net/license/index.html
*/
App::uses('BcAuthComponent', 'Controller/Component');
App::uses('Router', 'Routing');
/**
* Class BcUtil
*
* @package Baser.Lib
*/
class BcUtil extends CakeObject
{
/**
* 管理システムかチェック
*
* 《注意》by ryuring
* 処理の内容にCakeRequest や、Router::parse() を使おうとしたが、
* Router::parse() を利用すると、Routing情報が書き換えられてしまうので利用できない。
* Router::reload() や、Router::setRequestInfo() で調整しようとしたがうまくいかなかった。
*
* @return boolean
*/
public static function isAdminSystem($url = null)
{
if (!$url) {
$request = Router::getRequest(true);
if ($request) {
$url = $request->url;
} else {
return false;
}
}
$adminPrefix = Configure::read('Routing.prefixes.0');
return (boolean)(preg_match('/^(|\/)' . $adminPrefix . '\//', $url) || preg_match('/^(|\/)' . $adminPrefix . '$/', $url));
}
/**
* 管理ユーザーかチェック
*
* @return boolean
*/
public static function isAdminUser()
{
$user = self::loginUser('admin');
if (empty($user['UserGroup']['id'])) {
return false;
}
return ($user['UserGroup']['id'] == Configure::read('BcApp.adminGroupId'));
}
/**
* ユーザーが編集可能なユーザーかチェック
* @return bool
*/
public static function isUserEditableUser()
{
if(BcUtil::isAdminUser()) return true;
$user = BcUtil::loginUser();
if(!$user) return false;
/* @var Permission $permissionModel */
$permissionModel = ClassRegistry::init('Permission');
return $permissionModel->check('/admin/users/edit/', $user['UserGroup']['id']);
}
/**
* ログインユーザーのデータを取得する
*
* @return array
*/
public static function loginUser($prefix = 'admin')
{
// map ファイルへのリクエストの際、PHPのセッションを書き換えてしまい
// ログイン状態が継続できなくなってしまうため処理を実行しない
if(isset($_SERVER['REQUEST_URI']) && preg_match('/\.map$/', $_SERVER['REQUEST_URI'])) return null;
$Session = new CakeSession();
$sessionKey = BcUtil::authSessionKey($prefix);
$user = $Session->read('Auth.' . $sessionKey);
if (!$user) {
if (!empty($_SESSION['Auth'][$sessionKey])) {
$user = $_SESSION['Auth'][$sessionKey];
}
}
return $user;
}
/**
* 現在ログインしているユーザーのユーザーグループ情報を取得する
*
* @param string $prefix ログイン認証プレフィックス
* @return bool|mixed ユーザーグループ情報
*/
public static function loginUserGroup($prefix = 'admin')
{
$loginUser = self::loginUser($prefix);
if (!empty($loginUser['UserGroup'])) {
return $loginUser['UserGroup'];
} else {
return false;
}
}
/**
* 認証用のキーを取得
*
* @param string $prefix
* @return mixed
*/
public static function authSessionKey($prefix = 'admin')
{
return Configure::read('BcAuthPrefix.' . $prefix . '.sessionKey');
}
/**
* ログインしているユーザーのセッションキーを取得
*
* @return string
*/
public static function getLoginUserSessionKey()
{
list(, $sessionKey) = explode('.', BcAuthComponent::$sessionKey);
return $sessionKey;
}
/**
* ログインしているユーザー名を取得
*
* @return string
*/
public static function loginUserName()
{
$user = self::loginUser();
if (!empty($user['name'])) {
return $user['name'];
} else {
return '';
}
}
/**
* 現在適用しているテーマ梱包プラグインのリストを取得する
*
* @return array プラグインリスト
*/
public static function getCurrentThemesPlugins()
{
return BcUtil::getThemesPlugins(Configure::read('BcSite.theme'));
}
/**
* テーマ梱包プラグインのリストを取得する
*
* @param string $theme テーマ名
* @return array プラグインリスト
*/
public static function getThemesPlugins($theme)
{
$path = BASER_THEMES . $theme . DS . 'Plugin';
if (is_dir($path)) {
$Folder = new Folder($path);
$files = $Folder->read(true, true, false);
if (!empty($files[0])) {
return $files[0];
}
}
return [];
}
/**
* スキーマ情報のパスを取得する
*
* @param string $plugin プラグイン名
* @return string Or false
*/
public static function getSchemaPath($plugin = null)
{
if (!$plugin) {
$plugin = 'Core';
} else {
$plugin = Inflector::camelize($plugin);
}
if ($plugin == 'Core') {
return BASER_CONFIGS . 'Schema';
}
$paths = App::path('Plugin');
foreach($paths as $path) {
$_path = $path . $plugin . DS . 'Config' . DS . 'Schema';
if (is_dir($_path)) {
return $_path;
}
}
return false;
}
/**
* 初期データのパスを取得する
*
* 初期データのフォルダは アンダースコア区切り推奨
*
* @param string $plugin プラグイン名
* @param string $theme テーマ名
* @param string $pattern 初期データの類型
* @return string Or false
*/
public static function getDefaultDataPath($plugin = null, $theme = null, $pattern = null)
{
if (!$plugin) {
$plugin = 'Core';
} else {
$plugin = Inflector::camelize($plugin);
}
if (!$theme) {
$theme = 'core';
}
if (!$pattern) {
$pattern = 'default';
}
if ($plugin == 'Core') {
$paths = [BASER_CONFIGS . 'data' . DS . $pattern];
if ($theme != 'core') {
$paths = array_merge([
BASER_THEMES . $theme . DS . 'Config' . DS . 'data' . DS . $pattern,
BASER_THEMES . $theme . DS . 'Config' . DS . 'Data' . DS . $pattern,
BASER_THEMES . $theme . DS . 'Config' . DS . 'Data' . DS . Inflector::camelize($pattern),
BASER_CONFIGS . 'theme' . DS . $theme . DS . 'Config' . DS . 'data' . DS . $pattern,
BASER_THEMES . $theme . DS . 'Config' . DS . 'data' . DS . 'default',
BASER_THEMES . $theme . DS . 'Config' . DS . 'Data' . DS . 'default',
], $paths);
}
} else {
$pluginPaths = App::path('Plugin');
foreach($pluginPaths as $pluginPath) {
$pluginPath .= $plugin;
if (is_dir($pluginPath)) {
break;
}
$pluginPath = null;
}
if (!$pluginPath) {
return false;
}
$paths = [
$pluginPath . DS . 'Config' . DS . 'data' . DS . $pattern,
$pluginPath . DS . 'Config' . DS . 'Data' . DS . $pattern,
$pluginPath . DS . 'Config' . DS . 'Data' . DS . Inflector::camelize($pattern),
$pluginPath . DS . 'sql',
$pluginPath . DS . 'Config' . DS . 'data' . DS . 'default',
$pluginPath . DS . 'Config' . DS . 'Data' . DS . 'default',
];
if ($theme != 'core') {
$paths = array_merge([
BASER_THEMES . $theme . DS . 'Config' . DS . 'data' . DS . $pattern . DS . $plugin,
BASER_THEMES . $theme . DS . 'Config' . DS . 'Data' . DS . $pattern . DS . $plugin,
BASER_THEMES . $theme . DS . 'Config' . DS . 'Data' . DS . Inflector::camelize($pattern) . DS . $plugin,
BASER_CONFIGS . 'theme' . DS . $theme . DS . 'Config' . DS . 'data' . DS . $pattern . DS . $plugin,
BASER_THEMES . $theme . DS . 'Config' . DS . 'data' . DS . 'default' . DS . $plugin,
BASER_THEMES . $theme . DS . 'Config' . DS . 'Data' . DS . 'default' . DS . $plugin,
], $paths);
}
}
foreach($paths as $path) {
if (is_dir($path)) {
return $path;
}
}
return false;
}
/**
* シリアライズ
*
* @param mixed $value 対象文字列
* @return string
*/
public static function serialize($value)
{
return base64_encode(serialize($value));
}
/**
* アンシリアライズ
* base64_decode が前提
*
* @param mixed $value 対象文字列
* @return mixed
*/
public static function unserialize($value)
{
$_value = $value;
// unserializeに失敗した場合noticをを発生させfalseが戻る
$value = unserialize(base64_decode($value));
// 下位互換の為、しばらくの間、失敗した場合の再変換を行う v.3.0.2
if ($value === false) {
$value = unserialize($_value);
if($value === false) {
return '';
}
}
return $value;
}
/**
* URL用に文字列を変換する
*
* できるだけ可読性を高める為、不要な記号は除外する
*
* @param $value
* @return string
*/
public static function urlencode($value)
{
$value = str_replace([
' ', ' ', ' ', '\\', '\'', '|', '`', '^', '"', ')', '(', '}', '{', ']', '[', ';',
'/', '?', ':', '@', '&', '=', '+', '$', ',', '%', '<', '>', '#', '!'
], '_', $value);
$value = preg_replace('/\_{2,}/', '_', $value);
$value = preg_replace('/(^_|_$)/', '', $value);
return urlencode($value);
}
/**
* レイアウトテンプレートのリストを取得する
*
* @param string $path
* @param string $plugin
* @param string $theme
* @return array
*/
public static function getTemplateList($path, $plugin, $theme)
{
if ($plugin) {
$templatesPathes = App::path('View', $plugin);
} else {
$templatesPathes = App::path('View');
if ($theme) {
array_unshift($templatesPathes, WWW_ROOT . 'theme' . DS . $theme . DS);
}
}
$_templates = [];
foreach($templatesPathes as $templatesPath) {
$templatesPath .= $path . DS;
$folder = new Folder($templatesPath);
$files = $folder->read(true, true);
$foler = null;
if ($files[1]) {
if ($_templates) {
$_templates = array_merge($_templates, $files[1]);
} else {
$_templates = $files[1];
}
}
}
$templates = [];
foreach($_templates as $template) {
$ext = Configure::read('BcApp.templateExt');
if ($template != 'installations' . $ext) {
$template = basename($template, $ext);
$templates[$template] = $template;
}
}
return $templates;
}
/**
* 全てのテーマを取得する
* @return array
*/
public static function getAllThemeList()
{
$paths = [WWW_ROOT . 'theme', BASER_VIEWS . 'Themed'];
$themes = [];
foreach($paths as $path) {
$folder = new Folder($path);
$files = $folder->read(true, true);
if ($files[0]) {
foreach($files[0] as $theme) {
if ($theme !== 'core' && $theme !== '_notes') {
$themes[$theme] = $theme;
}
}
}
}
return $themes;
}
/**
* テーマリストを取得する
*
* @return array
*/
public static function getThemeList()
{
$themes = self::getAllThemeList();
foreach($themes as $key => $theme) {
if (preg_match('/^admin\-/', $theme)) {
unset($themes[$key]);
}
}
return $themes;
}
/**
* テーマリストを取得する
*
* @return array
*/
public static function getAdminThemeList()
{
$themes = self::getAllThemeList();
foreach($themes as $key => $theme) {
if (!preg_match('/^admin\-/', $theme)) {
unset($themes[$key]);
}
}
return $themes;
}
/**
* サブドメインを取得する
*
* @return string
*/
public static function getSubDomain($host = null)
{
$currentDomain = BcUtil::getCurrentDomain();
if (!$currentDomain && !$host) {
return '';
}
if (!$host) {
$host = $currentDomain;
}
if (strpos($host, '.') === false) {
return '';
}
$mainHost = BcUtil::getMainDomain();
if ($host == $mainHost) {
return '';
}
if (strpos($host, $mainHost) === false) {
return '';
}
$subDomain = str_replace($mainHost, '', $host);
if ($subDomain) {
return preg_replace('/\.$/', '', $subDomain);
}
return '';
}
/**
* 指定したURLのドメインを取得する
*
* @param $url URL
* @return string
*/
public static function getDomain($url)
{
$mainUrlInfo = parse_url($url);
$host = $mainUrlInfo['host'];
if (!empty($mainUrlInfo['port'])) {
$host .= ':' . $mainUrlInfo['port'];
}
return $host;
}
/**
* メインとなるドメインを取得する
*
* @return string
*/
public static function getMainDomain()
{
$mainDomain = Configure::read('BcEnv.mainDomain');
if ($mainDomain) {
return $mainDomain;
} else {
return BcUtil::getDomain(Configure::read('BcEnv.siteUrl'));
}
}
/**
* 現在のドメインを取得する
*
* @return string
*/
public static function getCurrentDomain()
{
return Configure::read('BcEnv.host');
}
/**
* 管理画面用のプレフィックスを取得する
*
* @return string
*/
public static function getAdminPrefix()
{
return Configure::read('BcAuthPrefix.admin.alias');
}
/**
* 文字列よりスクリプトタグを除去する
*
* @param string $value
* @return string
*/
public static function stripScriptTag($value)
{
$allows = [
'a', 'abbr', 'address', 'area', 'b', 'blockquote', 'body', 'br', 'button', 'caption', 'cite', 'code',
'col', 'colgroup', 'dd', 'del', 'dfn', 'div', 'dl', 'dt', 'em', 'fieldset', 'form', 'h1', 'h2', 'h3',
'h4', 'h5', 'h6', 'hr', 'i', 'iframe', 'img', 'input', 'ins', 'kbd', 'label', 'legend', 'li', 'link',
'map', 'meta', 'noscript', 'object', 'ol', 'optgroup', 'option', 'p', 'pre', 'q', 'samp', 'select',
'small', 'span', 'strong', 'sub', 'sup', 'table', 'tbody', 'td', 'textarea', 'tfoot', 'th', 'thead',
'title', 'tr', 'ul', 'var', 'style','ruby', 'rt', 'rp'
];
return strip_tags($value, '<' . implode('><', $allows) . '>');
}
}