Issues (1358)

modules/System/events.php (3 issues)

1
<?php
2
/**
3
 * @package    CleverStyle CMS
4
 * @subpackage System module
5
 * @category   modules
6
 * @author     Nazar Mokrynskyi <[email protected]>
7
 * @license    0BSD
8
 */
9
namespace cs;
10
use
11
	h;
12
13
/**
14
 * Multilingual functionality: redirects and necessary meta-tags
15
 */
16 123
Event::instance()
17 123
	->on(
18 123
		'System/Session/init/after',
19
		function () {
20 69
			$Config  = Config::instance();
21 69
			$Session = Session::instance();
22 69
			$user_id = $Session->get_user();
23
			/**
24
			 * If not guest - apply some individual settings
25
			 */
26 69
			if ($user_id != User::GUEST_ID) {
27 24
				$User     = User::instance();
28 24
				$timezone = $User->get('timezone', $user_id);
29 24
				if ($timezone && date_default_timezone_get() != $timezone) {
30
					date_default_timezone_set($timezone);
31
				}
32 24
				$L = Language::instance();
33
				/**
34
				 * Change language if configuration is multilingual and this is not page with localized url
35
				 */
36 24
				$language = $User->get('language', $user_id);
37 24
				if ($Config->core['multilingual'] && !$L->url_language() && $language) {
38
					$L->change($language);
39
				}
40
			}
41 69
			$Request = Request::instance();
42
			/**
43
			 * Security check
44
			 */
45
			if (
46 69
				$Request->header('x-requested-with') !== 'XMLHttpRequest' &&
47 69
				$Request->data('session') != $Session->get_id() &&
0 ignored issues
show
'session' of type string is incompatible with the type array<mixed,string[]>|string[] expected by parameter $name of cs\Request::data(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

47
				$Request->data(/** @scrutinizer ignore-type */ 'session') != $Session->get_id() &&
Loading history...
48 69
				$Request->header('origin') != $Config->core_url()
49
			) {
50 3
				$Request->data = [];
51
			}
52
			/**
53
			 * Show notification if there is any
54
			 */
55 69
			$notification = $Session->get_data('system_notification');
56 69
			if ($notification) {
57
				list($content, $type) = $notification;
58
				Page::instance()->post_Body .= h::{"cs-notify[$type]"}($content);
59
			}
60 123
		}
61
	)
62 123
	->on(
63 123
		'System/User/construct/after',
64
		function () {
65 69
			$Config  = Config::instance();
66 69
			$Request = Request::instance();
67 69
			if (!($Request->regular_path && $Config->core['multilingual'])) {
68 69
				return;
69
			}
70
			$Request          = Request::instance();
71
			$relative_address = $Request->path_normalized;
72
			$Page             = Page::instance();
73
			$core_url         = $Config->core_url();
74
			$base_url         = $Config->base_url();
75
			$Page->Head       .= h::link(
0 ignored issues
show
The method link() does not exist on h. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

75
			$Page->Head       .= h::/** @scrutinizer ignore-call */ link(
Loading history...
76
				[
77
					'hreflang' => 'x-default',
78
					'href'     => $Request->home_page ? $core_url : "$core_url/$relative_address",
79
					'rel'      => 'alternate'
80
				]
81
			);
82
			$clangs           = Cache::instance()->get(
83
				'languages/clangs',
84
				function () use ($Config) {
85
					$clangs = [];
86
					foreach ($Config->core['active_languages'] as $language) {
87
						$clangs[] = file_get_json_nocomments(LANGUAGES."/$language.json")['clang'];
88
					}
89
					return $clangs;
90
				}
91
			);
92
			foreach ($clangs as $clang) {
93
				$Page->Head .= h::link(
94
					[
95
						'hreflang' => $clang,
96
						'href'     => "$base_url/$clang/$relative_address",
97
						'rel'      => 'alternate'
98
					]
99
				);
100
			}
101 123
		}
102
	)
103 123
	->on(
104 123
		'admin/System/Menu',
105
		function () {
106
			$Config    = Config::instance();
107
			$L         = Language::prefix('system_admin_');
108
			$Menu      = Menu::instance();
109
			$Request   = Request::instance();
110
			$structure = $Config->core['simple_admin_mode'] ? file_get_json(__DIR__.'/admin/index_simple.json') : file_get_json(__DIR__.'/admin/index.json');
111
			foreach ($structure as $section => $items) {
112
				$Menu->add_section_item(
113
					'System',
114
					$L->$section,
115
					[
116
						'href'    => "admin/System/$section",
117
						'primary' => $Request->route_path(0) == $section
118
					]
119
				);
120
				foreach ($items as $item) {
121
					$Menu->add_item(
122
						'System',
123
						$L->$item,
124
						[
125
							'href'    => "admin/System/$section/$item",
126
							'primary' => $Request->route_path(0) == $section && $Request->route_path(1) == $item
127
						]
128
					);
129
				}
130
			}
131 123
		}
132
	)
133 123
	->on(
134 123
		'System/Page/requirejs',
135
		function ($data) {
136
			$data['paths'] += [
137 6
				'jssha'         => DIR.'/assets/js/modules/jsSHA-2.1.0',
138 6
				'autosize'      => DIR.'/assets/js/modules/autosize.min',
139 6
				'html5sortable' => DIR.'/assets/js/modules/html5sortable-0.6.3.min',
140 6
				'sprintf-js'    => DIR.'/assets/js/modules/sprintf-1.1.1.min'
141
			];
142 123
		}
143
	)
144 123
	->on(
145 123
		'System/Request/routing_replace/before',
146
		function ($data) {
147
			/** @noinspection NotOptimalIfConditionsInspection */
148
			if (
149
				(
150 63
					strpos($data['rc'], 'bower_components/polymer/') === 0 ||
151 63
					strpos($data['rc'], 'bower_components/shadycss/') === 0 ||
152 63
					strpos($data['rc'], 'node_modules/@polymer/polymer/') === 0 ||
153 63
					strpos($data['rc'], 'node_modules/@webcomponents/shadycss/') === 0
154
				) &&
155 63
				Request::instance()->method == 'GET'
156
			) {
157
				$extension = file_extension(explode('?', $data['rc'])[0]);
158
				if ($extension == 'css' || $extension == 'html') {
159
					$content_type = "text/$extension";
160
				} elseif ($extension == 'js') {
161
					$content_type = 'application/javascript';
162
				}
163
				if (isset($content_type)) {
164
					Response::instance()
165
						->header('Content-Type', $content_type)
0 ignored issues
show
The method header() does not exist on cs\False_class. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

165
						->/** @scrutinizer ignore-call */ header('Content-Type', $content_type)
Loading history...
166
						->header('Cache-Control', 'max-age=2592000, immutable');
167
					throw new ExitException;
168
				}
169
			}
170 123
		}
171
	);
172