|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * |
| 4 | + * phpBB Browser Push Notifications. An extension for the phpBB Forum Software package. |
| 5 | + * |
| 6 | + * @copyright (c) 2024, phpBB Limited <https://www.phpbb.com> |
| 7 | + * @license GNU General Public License, version 2 (GPL-2.0) |
| 8 | + * |
| 9 | + */ |
| 10 | + |
| 11 | +namespace phpbb\webpushnotifications\controller; |
| 12 | + |
| 13 | +use phpbb\config\config; |
| 14 | +use phpbb\exception\http_exception; |
| 15 | +use phpbb\language\language; |
| 16 | +use phpbb\path_helper; |
| 17 | +use phpbb\user; |
| 18 | +use Symfony\Component\HttpFoundation\JsonResponse; |
| 19 | +use Symfony\Component\HttpFoundation\Response; |
| 20 | + |
| 21 | +class manifest |
| 22 | +{ |
| 23 | + /** @var config */ |
| 24 | + protected $config; |
| 25 | + |
| 26 | + /** @var language */ |
| 27 | + protected $language; |
| 28 | + |
| 29 | + /** @var path_helper */ |
| 30 | + protected $path_helper; |
| 31 | + |
| 32 | + /** @var user */ |
| 33 | + protected $user; |
| 34 | + |
| 35 | + /** |
| 36 | + * Constructor for webpush controller |
| 37 | + * |
| 38 | + * @param config $config |
| 39 | + * @param path_helper $path_helper |
| 40 | + * @param language $language |
| 41 | + * @param user $user |
| 42 | + */ |
| 43 | + public function __construct(config $config, language $language, path_helper $path_helper, user $user) |
| 44 | + { |
| 45 | + $this->config = $config; |
| 46 | + $this->path_helper = $path_helper; |
| 47 | + $this->language = $language; |
| 48 | + $this->user = $user; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Handle creation of a manifest json file for progressive web-app support |
| 53 | + * |
| 54 | + * @return JsonResponse |
| 55 | + */ |
| 56 | + public function handle(): JsonResponse |
| 57 | + { |
| 58 | + if ($this->user->data['is_bot'] || $this->user->data['user_type'] == USER_INACTIVE) |
| 59 | + { |
| 60 | + throw new http_exception(Response::HTTP_FORBIDDEN, 'Forbidden'); |
| 61 | + } |
| 62 | + |
| 63 | + $board_path = $this->config['force_server_vars'] ? $this->config['script_path'] : $this->path_helper->get_web_root_path(); |
| 64 | + |
| 65 | + $manifest = [ |
| 66 | + 'name' => $this->config['sitename'], |
| 67 | + 'short_name' => $this->config['pwa_short_name'] ?: substr($this->config['sitename'], 0, 12), |
| 68 | + 'display' => 'standalone', |
| 69 | + 'orientation' => 'portrait', |
| 70 | + 'dir' => $this->language->lang('DIRECTION'), |
| 71 | + 'start_url' => $board_path, |
| 72 | + 'scope' => $board_path, |
| 73 | + ]; |
| 74 | + |
| 75 | + if (!empty($this->config['pwa_icon_small']) && !empty($this->config['pwa_icon_large'])) |
| 76 | + { |
| 77 | + $manifest['icons'] = [ |
| 78 | + [ |
| 79 | + 'src' => $this->config['icons_path'] . '/' . $this->config['pwa_icon_small'], |
| 80 | + 'sizes' => '192x192', |
| 81 | + 'type' => 'image/png' |
| 82 | + ], |
| 83 | + [ |
| 84 | + 'src' => $this->config['icons_path'] . '/' . $this->config['pwa_icon_large'], |
| 85 | + 'sizes' => '512x512', |
| 86 | + 'type' => 'image/png' |
| 87 | + ] |
| 88 | + ]; |
| 89 | + } |
| 90 | + |
| 91 | + return new JsonResponse($manifest); |
| 92 | + } |
| 93 | +} |
0 commit comments