1<?php
2
3use dokuwiki\Extension\AdminPlugin;
4use dokuwiki\Form\Form;
5
6/**
7 * DokuWiki Plugin cosmocode (Admin Component)
8 *
9 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
10 * @author Andreas Gohr <dokuwiki@cosmocode.de>
11 */
12class admin_plugin_cosmocode extends AdminPlugin
13{
14
15    /** @var helper_plugin_cosmocode_support */
16    protected $hlp_support;
17    /** @var helper_plugin_cosmocode_partner */
18    protected $hlp_partner;
19
20    public function __construct()
21    {
22        $this->hlp_support = $this->loadHelper('cosmocode_support');
23        $this->hlp_partner = $this->loadHelper('cosmocode_partner');
24    }
25
26    /** @inheritDoc */
27    public function handle()
28    {
29        // FIXME data processing
30    }
31
32    /** @inheritDoc */
33    public function html()
34    {
35        global $ID;
36        global $INPUT;
37
38        $tabs = [
39            'support' => wl($ID, ['do' => 'admin', 'page' => 'cosmocode', 'tab' => 'support']),
40            'partner' => wl($ID, ['do' => 'admin', 'page' => 'cosmocode', 'tab' => 'partner']),
41        ];
42        $current = $INPUT->str('tab', 'support');
43
44        echo '<div class="plugin_cosmocode">';
45        echo '<h1>' . $this->getLang('menu') . '</h1>';
46        echo '<ul class="tabs">';
47        foreach ($tabs as $tab => $url) {
48            $class = ($current === $tab) ? 'active' : '';
49
50            echo "<li class='$class'>";
51            echo '<a href="' . $url . '">' . $this->getLang('tab_' . $tab) . '</a>';
52            echo '</li>';
53        }
54        echo '</ul>';
55        echo '<br>';
56
57        switch ($current) {
58            case 'partner':
59                $this->showPartnerTab();
60                break;
61            case 'support':
62                $this->showSupportTab();
63                break;
64        }
65        echo '</div>';
66    }
67
68    protected function showPartnerTab()
69    {
70        $this->showTokenInfo();
71        $this->showFeed();
72    }
73
74    protected function showSupportTab()
75    {
76        echo $this->locale_xhtml('support');
77
78        global $conf;
79        $data = [
80            'dt' => dformat(null, '%Y-%m-%d'),
81            'partner' => array_values($this->hlp_partner->getTokens()),
82            'dokuwiki' => getVersionData(),
83            'conf' => array_merge(
84                array_intersect_key($conf, array_flip(
85                    ['title', 'tagline', 'baseurl', 'basedir', 'savedir', 'useacl', 'authtype', 'template']
86                )),
87                [
88                    'wiki-id' => md5(auth_cookiesalt()),
89                    'inc' => DOKU_INC,
90                ],
91            ),
92            'environment' => $this->hlp_support->getRuntimeVersions(),
93            'plugins' => $this->hlp_support->getPlugins(),
94            'extensions' => get_loaded_extensions(),
95        ];
96
97        echo '<div class="envdata">';
98        echo json_encode($data, JSON_PRETTY_PRINT);
99        echo '</div>';
100    }
101
102
103    /**
104     * Show the list of available extensions
105     */
106    protected function showFeed()
107    {
108        try {
109            $extensions = $this->hlp_partner->getExtensions();
110        } catch (\Exception $e) {
111            msg(nl2br(hsc($e->getMessage())), -1);
112            return;
113        }
114
115        echo '<ul class="extensions">';
116        foreach ($extensions as $ext) {
117            echo '<li>';
118            echo '<div class="li">';
119            echo '<h2>';
120            echo '<a href="' . hsc($ext['url']) . '" class="urlextern" target="_blank">' . hsc($ext['name']) . '</a>';
121            echo ' <span>' . hsc($ext['date']) . '</span>';
122            echo '</h2>';
123            echo '<p>' . hsc($ext['desc']) . '</p>';
124
125            // FIXME check if this version is newer than the installed one and highlight updates
126
127            if ($ext['token']) {
128                echo $this->getInstallForm($ext);
129            }
130
131            echo '</div>';
132            echo '</li>';
133        }
134        echo '</ul>';
135    }
136
137    /**
138     * Tell the user about their current partner status
139     * @return void
140     */
141    protected function showTokenInfo()
142    {
143        $tokens = $this->hlp_partner->getTokens();
144        if (!$tokens) {
145            echo $this->locale_xhtml('partner-no');
146            return;
147        }
148        echo $this->locale_xhtml('partner-yes');
149
150        echo '<ul class="tokens">';
151        foreach ($tokens as $token => $data) {
152            echo '<li class="token"><div class="li">';
153            if ($data['scopes'][0] === '') {
154                echo 'Partner Token';
155            } else {
156                echo hsc($data['scopes'][0]) . ' Token';
157            }
158            echo ' ' . sprintf($this->getLang('valid_until'), dformat($data['exp']));
159            echo '</div></li>';
160        }
161        echo '</ul>';
162    }
163
164
165    /**
166     * Create a form that submits the special download URL to the extension manager
167     *
168     * @param array $ext
169     * @return string
170     */
171    protected function getInstallForm($ext)
172    {
173        global $ID;
174        $dl = $this->hlp_partner->getDownloadUrl($ext['base'], $ext['token']);
175        $action = wl($ID, ['do' => 'admin', 'page' => 'extension', 'tab' => 'install'], false, '&');
176        $form = new Form(['action' => $action]);
177        $form->setHiddenField('installurl', $dl);
178        $form->setHiddenField('overwrite', 1);
179        $form->addButton('submit', $this->getLang('btn_install'))->attr('type', 'submit');
180        return $form->toHTML();
181    }
182}
183