Passed
Push — development ( f9d3d6...da8715 )
by Spuds
01:09 queued 25s
created

ManifestMinimus::getLanguageDirection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * This class is responsible for generating and sending a JSON manifest file for a Progressive Web App.
5
 *
6
 * @package   ElkArte Forum
7
 * @copyright ElkArte Forum contributors
8
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file)
9
 *
10
 * @version 2.0 dev
11
 *
12
 */
13
14
namespace ElkArte;
15
16
use ElkArte\Http\Headers;
17
18
/**
19
 * Class ManifestMinimus
20
 *
21
 * The manifest file contains the information needed to configure how the PWA
22
 * will look when it is added/installed to the home screen of the device, and configures how
23
 * it will behave when launched.
24
 *
25
 * The minimal information needed is:
26
 * - The canonical name of the website
27
 * - A short version of that name (for icons)
28
 * - The theme color of the website for OS integration
29
 * - The background color of the website for OS integration
30
 * - The URL scope that the progressive web app is limited to
31
 * - The start URL that new instances of the progressive web app will implicitly load
32
 * - A human-readable description
33
 * - Orientation restrictions (it is unwise to change this from "any" without a hard technical limit)
34
 * - Any icons for your website to be used on the home screen (192 and 512 are required)
35
 */
36
class ManifestMinimus
37
{
38
	public function __construct()
39
	{
40
	}
41
42
	public function create()
43
	{
44
		$this->prepareAndSendHeaders();
45
46
		echo json_encode($this->getManifestParts(), JSON_PRETTY_PRINT);
47
	}
48
49
	protected function prepareAndSendHeaders()
50
	{
51
		$headers = Headers::instance();
52
53
		$expires = gmdate('D, d M Y H:i:s', time() + 86400);
54
		$lastModified = gmdate('D, d M Y H:i:s', time());
55
56
		$headers
57
			->contentType('application/manifest+json')
58
			->header('Expires', $expires . ' GMT')
59
			->header('Last-Modified', $lastModified . ' GMT')
60
			->header('Cache-Control', 'private, max-age=86400')
61
			->send();
62
	}
63
64
	protected function getManifestParts(): array
65
	{
66
		global $mbname;
67
68
		$manifest = [];
69
70
		$manifest['name'] = un_htmlspecialchars($mbname);
71
		$manifest['short_name'] = $this->getShortname();
72
		$manifest['description'] = $this->getDescription();
73
		$manifest['lang'] = $this->getLanguageCode();
74
		$manifest['dir'] = $this->getLanguageDirection();
75
		$manifest['display'] = $this->getDisplay();
76
		$manifest['orientation'] = $this->getOrientation();
77
		$manifest['id'] = $this->getId();
78
		$manifest['start_url'] = $this->getStartUrl();
79
		$manifest['scope'] = $this->getScope();
80
		$manifest['background_color'] = $this->getBackgroundColor();
81
		$manifest['theme_color'] = $this->getThemeColor();
82
		$manifest['icons'] = $this->getManifestIcons();
83
84
		return array_filter($manifest);
85
	}
86
87
	protected function getDescription()
88
	{
89
		global $settings, $mbname;
90
91
		$description = un_htmlspecialchars($settings['site_slogan'] ?? $mbname);
92
		$description = str_replace(['<br>', '<br />'], ' ', $description);
93
94
		return strip_tags($description);
95
96
	}
97
98
	protected function getShortname()
99
	{
100
		global $modSettings, $mbname;
101
102
		return un_htmlspecialchars($modSettings['pwa_short_name'] ?? $mbname);
103
	}
104
105
	protected function getLanguageCode()
106
	{
107
		global $txt;
108
109
		$lang = $txt['lang_locale'] ?? 'en-US';
110
111
		return str_replace(['.utf8', '_'], ['', '-'], trim($lang));
112
	}
113
114
	protected function getLanguageDirection()
115
	{
116
		global $txt;
117
118
		return !empty($txt['lang_rtl']) ? 'rtl' : 'ltr';
119
	}
120
121
	protected function getDisplay(): string
122
	{
123
		return 'standalone';
124
	}
125
126
	protected function getOrientation(): string
127
	{
128
		return 'any';
129
	}
130
131
	protected function getId()
132
	{
133
		return trim($this->getScope(), '/') . '?elk_pwa=1';
134
	}
135
136
	protected function getScope()
137
	{
138
		return $this->getStartUrl();
139
	}
140
141
	protected function getStartUrl()
142
	{
143
		global $boardurl;
144
145
		$parts = parse_url($boardurl);
146
147
		return empty($parts['path']) ? '/' : '/' . trim($parts['path'], '/') . '/';
148
	}
149
150
	protected function getBackgroundColor()
151
	{
152
		global $modSettings;
153
154
		return $modSettings['pwa_background_color'] ?? '#fafafa';
155
	}
156
157
	protected function getThemeColor()
158
	{
159
		global $modSettings;
160
161
		return $modSettings['pwa_theme_color'] ?? '#3d6e32';
162
	}
163
164
	protected function getManifestIcons(): array
165
	{
166
		global $modSettings, $settings;
167
168
		$icons = [];
169
170
		$iconSmallUrl = $modSettings['pwa_small_icon'] ?? $settings['default_images_url'] . '\icon_pwa_small.png';
171
		$iconUrlLarge = $modSettings['pwa_large_icon'] ?? $settings['default_images_url'] . '\icon_pwa_large.png';
172
173
		if ($iconSmallUrl)
174
		{
175
			$icon = [
176
				'src' => $iconSmallUrl,
177
				'sizes' => '192x192',
178
				'purpose' => 'any'
179
			];
180
			$icons[] = $icon;
181
		}
182
183
		if ($iconUrlLarge)
184
		{
185
			$iconLarge = [
186
				'src' => $iconUrlLarge,
187
				'sizes' => '512x512',
188
				'purpose' => 'any'
189
			];
190
			$icons[] = $iconLarge;
191
		}
192
193
		return $icons;
194
	}
195
}
196