|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Saito - The Threaded Web Forum |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (c) the Saito Project Developers 2015 |
|
6
|
|
|
* @link https://github.com/Schlaefer/Saito |
|
7
|
|
|
* @license http://opensource.org/licenses/MIT |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace App\View\Helper; |
|
11
|
|
|
|
|
12
|
|
|
use App\Controller\Component\CurrentUserComponent; |
|
13
|
|
|
use Cake\Core\Configure; |
|
14
|
|
|
use Cake\Event\Event; |
|
15
|
|
|
use Cake\View\Helper; |
|
16
|
|
|
use Cake\View\View; |
|
17
|
|
|
use Saito\JsData\JsData; |
|
18
|
|
|
use Saito\User\ForumsUserInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Javascript Data Helper |
|
22
|
|
|
*/ |
|
23
|
|
|
class JsDataHelper extends AppHelper |
|
24
|
|
|
{ |
|
25
|
|
|
|
|
26
|
|
|
public $helpers = ['Url']; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* JsData |
|
30
|
|
|
* |
|
31
|
|
|
* @var JsData |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $_JsData; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* CakePHP beforeRender event-handler |
|
37
|
|
|
* |
|
38
|
|
|
* @param Event $event event |
|
39
|
|
|
* @param mixed $viewFile view file |
|
40
|
|
|
* @return void |
|
41
|
|
|
*/ |
|
42
|
|
|
public function beforeRender(Event $event, $viewFile): void |
|
|
|
|
|
|
43
|
|
|
{ |
|
44
|
|
|
$View = $event->getSubject(); |
|
45
|
|
|
$this->_JsData = $View->get('jsData'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* get app js |
|
50
|
|
|
* |
|
51
|
|
|
* @param View $View view |
|
52
|
|
|
* @param ForumsUserInterface $CurrentUser user |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getAppJs(View $View, ForumsUserInterface $CurrentUser) |
|
56
|
|
|
{ |
|
57
|
|
|
$settings = Configure::read('Saito.Settings'); |
|
58
|
|
|
$request = $View->getRequest(); |
|
59
|
|
|
|
|
60
|
|
|
$js = $this->_JsData->getJs(); |
|
61
|
|
|
$js += [ |
|
62
|
|
|
'app' => [ |
|
63
|
|
|
'version' => Configure::read('Saito.v'), |
|
64
|
|
|
'settings' => [ |
|
65
|
|
|
'autoPageReload' => (isset($View->viewVars['autoPageReload']) ? $View->viewVars['autoPageReload'] : 0), |
|
|
|
|
|
|
66
|
|
|
'editPeriod' => (int)Configure::read( |
|
67
|
|
|
'Saito.Settings.edit_period' |
|
68
|
|
|
), |
|
69
|
|
|
'language' => Configure::read('Saito.language'), |
|
70
|
|
|
'notificationIcon' => $this->Url->assetUrl( |
|
71
|
|
|
'html5-notification-icon.png', |
|
72
|
|
|
[ |
|
73
|
|
|
'pathPrefix' => Configure::read('App.imageBaseUrl'), |
|
74
|
|
|
'fullBase' => true |
|
75
|
|
|
] |
|
76
|
|
|
), |
|
77
|
|
|
'quote_symbol' => $settings['quote_symbol'], |
|
78
|
|
|
'subject_maxlength' => $settings['subject_maxlength'], |
|
79
|
|
|
'theme' => $View->getTheme(), |
|
80
|
|
|
'apiroot' => $request->getAttribute('webroot') . 'api/v2/', |
|
81
|
|
|
'webroot' => $request->getAttribute('webroot') |
|
82
|
|
|
] |
|
83
|
|
|
], |
|
84
|
|
|
'request' => [ |
|
85
|
|
|
'action' => $request->getParam('action'), |
|
86
|
|
|
'controller' => $request->getParam('controller'), |
|
87
|
|
|
'isMobile' => $request->isMobile(), |
|
88
|
|
|
'isPreview' => $request->isPreview(), |
|
89
|
|
|
'csrf' => $this->_getCsrf($View) |
|
90
|
|
|
], |
|
91
|
|
|
'currentUser' => [ |
|
92
|
|
|
'id' => (int)$CurrentUser->get('id'), |
|
93
|
|
|
'username' => $CurrentUser->get('username'), |
|
94
|
|
|
'user_show_inline' => $CurrentUser->get('inline_view_on_click') || false, |
|
95
|
|
|
'user_show_thread_collapsed' => $CurrentUser->get('user_show_thread_collapsed') || false |
|
96
|
|
|
], |
|
97
|
|
|
'callbacks' => [ |
|
98
|
|
|
'beforeAppInit' => [], |
|
99
|
|
|
'afterAppInit' => [], |
|
100
|
|
|
'afterViewInit' => [] |
|
101
|
|
|
] |
|
102
|
|
|
]; |
|
103
|
|
|
$out = 'var SaitoApp = ' . json_encode($js); |
|
104
|
|
|
$out .= '; SaitoApp.timeAppStart = new Date().getTime();'; |
|
105
|
|
|
|
|
106
|
|
|
return $out; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Get CSRF-config |
|
111
|
|
|
* |
|
112
|
|
|
* @param View $View View |
|
113
|
|
|
* @return array |
|
114
|
|
|
* - 'header' HTTP header for CSRF-token |
|
115
|
|
|
* - 'token' CSRF-token |
|
116
|
|
|
*/ |
|
117
|
|
|
protected function _getCsrf(View $View) |
|
118
|
|
|
{ |
|
119
|
|
|
$key = 'csrfToken'; |
|
120
|
|
|
$token = $View->getRequest()->getCookie($key); |
|
121
|
|
|
if (empty($token)) { |
|
122
|
|
|
$token = $View->getResponse()->getCookie($key)['value']; |
|
123
|
|
|
} |
|
124
|
|
|
$header = 'X-CSRF-Token'; |
|
125
|
|
|
|
|
126
|
|
|
return compact('header', 'token'); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Passes method calls on to JsData |
|
131
|
|
|
* |
|
132
|
|
|
* {@inheritDoc} |
|
133
|
|
|
*/ |
|
134
|
|
View Code Duplication |
public function __call($method, $params) |
|
|
|
|
|
|
135
|
|
|
{ |
|
136
|
|
|
$proxy = [$this->_JsData, $method]; |
|
137
|
|
|
if (is_callable($proxy)) { |
|
138
|
|
|
return call_user_func_array($proxy, $params); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
parent::__call($method, $params); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.