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