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
|
|
|
'upload_max_img_size' => $settings['upload_max_img_size'] * 1024, |
80
|
|
|
'upload_max_number_of_uploads' => (int)$settings['upload_max_number_of_uploads'], |
81
|
|
|
'theme' => $View->getTheme(), |
82
|
|
|
'apiroot' => $request->getAttribute('webroot') . 'api/v2/', |
83
|
|
|
'webroot' => $request->getAttribute('webroot') |
84
|
|
|
] |
85
|
|
|
], |
86
|
|
|
'request' => [ |
87
|
|
|
'action' => $request->getParam('action'), |
88
|
|
|
'controller' => $request->getParam('controller'), |
89
|
|
|
'isMobile' => $request->isMobile(), |
90
|
|
|
'isPreview' => $request->isPreview(), |
91
|
|
|
'csrf' => $this->_getCsrf($View) |
92
|
|
|
], |
93
|
|
|
'currentUser' => [ |
94
|
|
|
'id' => (int)$CurrentUser->get('id'), |
95
|
|
|
'username' => $CurrentUser->get('username'), |
96
|
|
|
'user_show_inline' => $CurrentUser->get('inline_view_on_click') || false, |
97
|
|
|
'user_show_thread_collapsed' => $CurrentUser->get('user_show_thread_collapsed') || false |
98
|
|
|
], |
99
|
|
|
'callbacks' => [ |
100
|
|
|
'beforeAppInit' => [], |
101
|
|
|
'afterAppInit' => [], |
102
|
|
|
'afterViewInit' => [] |
103
|
|
|
] |
104
|
|
|
]; |
105
|
|
|
$out = 'var SaitoApp = ' . json_encode($js); |
106
|
|
|
$out .= '; SaitoApp.timeAppStart = new Date().getTime();'; |
107
|
|
|
|
108
|
|
|
return $out; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get CSRF-config |
113
|
|
|
* |
114
|
|
|
* @param View $View View |
115
|
|
|
* @return array |
116
|
|
|
* - 'header' HTTP header for CSRF-token |
117
|
|
|
* - 'token' CSRF-token |
118
|
|
|
*/ |
119
|
|
|
protected function _getCsrf(View $View) |
120
|
|
|
{ |
121
|
|
|
$key = 'csrfToken'; |
122
|
|
|
$token = $View->getRequest()->getCookie($key); |
123
|
|
|
if (empty($token)) { |
124
|
|
|
$token = $View->getResponse()->getCookie($key)['value']; |
125
|
|
|
} |
126
|
|
|
$header = 'X-CSRF-Token'; |
127
|
|
|
|
128
|
|
|
return compact('header', 'token'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Passes method calls on to JsData |
133
|
|
|
* |
134
|
|
|
* {@inheritDoc} |
135
|
|
|
*/ |
136
|
|
View Code Duplication |
public function __call($method, $params) |
|
|
|
|
137
|
|
|
{ |
138
|
|
|
$proxy = [$this->_JsData, $method]; |
139
|
|
|
if (is_callable($proxy)) { |
140
|
|
|
return call_user_func_array($proxy, $params); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
parent::__call($method, $params); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.