xref: /dokuwiki/lib/exe/manifest.php (revision fb1f9089ad53554574c96fe6f86cc029958f87e4)
1<?php
2
3if (!defined('DOKU_INC')) {
4    define('DOKU_INC', __DIR__ . '/../../');
5}
6require_once(DOKU_INC . 'inc/init.php');
7
8class Manifest {
9    public function run() {
10        $manifest = retrieveConfig('manifest', [$this, 'jsonToArray']);
11
12        global $conf;
13
14        if (empty($manifest['name'])) {
15            $manifest['name'] = $conf['title'];
16        }
17
18        if (empty($manifest['short_name'])) {
19            $manifest['short_name'] = $conf['title'];
20        }
21
22        if (empty($manifest['description'])) {
23            $manifest['description'] = $conf['tagline'];
24        }
25
26        if (empty($manifest['start_url'])) {
27            $manifest['start_url'] = DOKU_REL;
28        }
29
30        if (empty($manifest['icons'])) {
31            $manifest['icons'] = [];
32            $look = [
33                ':wiki:logo.png',
34                ':logo.png',
35                'images/logo.png',
36                ':wiki:apple-touch-icon.png',
37                ':apple-touch-icon.png',
38                'images/apple-touch-icon.png',
39                ':wiki:favicon.svg',
40                ':favicon.svg',
41                'images/favicon.svg',
42                ':wiki:favicon.ico',
43                ':favicon.ico',
44                'images/favicon.ico',
45                ':wiki:logo',
46            ];
47
48            $abs = true;
49            foreach($look as $img) {
50                if($img[0] === ':') {
51                    $file    = mediaFN($img);
52                    $ismedia = true;
53                } else {
54                    $file    = tpl_incdir().$img;
55                    $ismedia = false;
56                }
57
58                if (file_exists($file)) {
59                    $imginfo = getimagesize($file);
60                    if($ismedia) {
61                        $url = ml($img, '', true, '', $abs);
62                    } else {
63                        $url = tpl_basedir().$img;
64                        if($abs) $url = DOKU_URL.substr($url, strlen(DOKU_REL));
65                    }
66                    $manifest['icons'][] = [
67                        'src' => $url,
68                        'sizes' => $imginfo[0] . 'x' . $imginfo[1],
69                        'type' => $imginfo['mime'],
70                    ];
71                };
72            }
73        }
74
75        trigger_event('MANIFEST_SEND', $manifest);
76
77        header('Content-Type: application/manifest+json');
78        echo json_encode($manifest);
79    }
80
81    public function jsonToArray($file)
82    {
83        $json = file_get_contents($file);
84
85        $conf = json_decode($json, true);
86
87        $jsonError = json_last_error();
88        if (!is_array($conf) && $jsonError !== JSON_ERROR_NONE) {
89
90            switch ($jsonError) {
91                case JSON_ERROR_DEPTH:
92                    $jsonErrorText = 'The maximum stack depth has been exceeded';
93                    break;
94                case JSON_ERROR_STATE_MISMATCH:
95                    $jsonErrorText = 'Invalid or malformed JSON';
96                    break;
97                case JSON_ERROR_CTRL_CHAR:
98                    $jsonErrorText = 'Control character error, possibly incorrectly encoded';
99                    break;
100                case JSON_ERROR_SYNTAX:
101                    $jsonErrorText = 'Syntax error';
102                    break;
103                case JSON_ERROR_UTF8:
104                    $jsonErrorText = 'Malformed UTF-8 characters, possibly incorrectly encoded';
105                    break;
106                case JSON_ERROR_RECURSION:
107                    $jsonErrorText = 'One or more recursive references in the value to be encoded';
108                    break;
109                case JSON_ERROR_INF_OR_NAN:
110                    $jsonErrorText = 'One or more NAN or INF values in the value to be encoded';
111                    break;
112                case JSON_ERROR_UNSUPPORTED_TYPE:
113                    $jsonErrorText = 'A value of a type that cannot be encoded was given';
114                    break;
115                case JSON_ERROR_INVALID_PROPERTY_NAME:
116                    $jsonErrorText = 'A property name that cannot be encoded was given';
117                    break;
118                case JSON_ERROR_UTF16:
119                    $jsonErrorText = 'Malformed UTF-16 characters, possibly incorrectly encoded';
120                    break;
121                default:
122                    $jsonErrorText = 'Unknown Error Code';
123            }
124
125            trigger_error('JSON decoding error "' . $jsonErrorText . '" for file ' . $file, E_USER_WARNING);
126            return [];
127        }
128
129        return $conf;
130    }
131}
132
133$manifest = new Manifest();
134$manifest->run();
135