xref: /plugin/oauth/helper.php (revision 6d9a8a49e2ddf3e7f5338a74a91c704edb6dc965)
1<?php
2/**
3 * DokuWiki Plugin oauth (Helper Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Gohr <andi@splitbrain.org>
7 */
8
9use dokuwiki\Extension\Event;
10use dokuwiki\plugin\oauth\Service;
11
12require_once(__DIR__ . '/vendor/autoload.php');
13
14/**
15 * Basic helper methods for the oauth flow
16 */
17class helper_plugin_oauth extends DokuWiki_Plugin
18{
19
20    /**
21     * Load the needed libraries and initialize the named oAuth service
22     *
23     * @param string $servicename
24     * @return null|Service
25     */
26    public function loadService($servicename)
27    {
28        $services = $this->listServices(true);
29        if (!isset($services[$servicename])) return null;
30        return $services[$servicename];
31    }
32
33    /**
34     * The redirect URI used in all oAuth requests
35     *
36     * @return string
37     */
38    public function redirectURI()
39    {
40        if ($this->getConf('custom-redirectURI') !== '') {
41            return $this->getConf('custom-redirectURI');
42        } else {
43            return DOKU_URL . DOKU_SCRIPT;
44        }
45    }
46
47    /**
48     * List available Services
49     *
50     * Services returned here, do not have initialized oAuth providers yet!
51     *
52     * @param bool $enabledonly list only services that have been configured
53     * @triggers PLUGIN_OAUTH_BACKEND_REGISTER
54     * @return Service[] list of service objects
55     */
56    public function listServices($enabledonly = true)
57    {
58        $services = [];
59        $event = new Event('PLUGIN_OAUTH_BACKEND_REGISTER', $services);
60        $event->advise_before(false);
61        $event->advise_after();
62
63        // filter out unconfigured services
64        if ($enabledonly) {
65            $services = array_filter($services, function ($service) {
66                /** @var Service $service */
67                return (bool)$service->getKey();
68            });
69        }
70
71        return $services;
72    }
73
74    /**
75     * @return array
76     */
77    public function getValidDomains()
78    {
79        if ($this->getConf('mailRestriction') === '') {
80            return array();
81        }
82        $validDomains = explode(',', trim($this->getConf('mailRestriction'), ','));
83        $validDomains = array_map('trim', $validDomains);
84        return $validDomains;
85    }
86
87    /**
88     * @param string $mail
89     *
90     * @return bool
91     */
92    public function checkMail($mail)
93    {
94        $hostedDomains = $this->getValidDomains();
95
96        foreach ($hostedDomains as $validDomain) {
97            if (substr($mail, -strlen($validDomain)) === $validDomain) {
98                return true;
99            }
100        }
101        return false;
102    }
103
104    /**
105     * @return bool
106     */
107    public function isGETRequest()
108    {
109        global $INPUT;
110        return $INPUT->server->str('REQUEST_METHOD') === 'GET';
111    }
112
113    /**
114     * check if we are handling a request to doku.php. Only doku.php defines $updateVersion
115     *
116     * @return bool
117     */
118    public function isDokuPHP()
119    {
120        global $updateVersion;
121        return isset($updateVersion);
122    }
123
124    /**
125     * Display an exception to the user
126     *
127     * @param Exception $e
128     * @param string $prefix - user friendly explanation if available
129     */
130    public function showException(\Exception $e, $prefix = '')
131    {
132        global $conf;
133        msg('OAuth: ' . $prefix . ' ' . hsc($e->getMessage()), -1);
134        if ($conf['allowdebug']) {
135            msg('<pre>' . hsc($e->getTraceAsString()) . '</pre>', -1);
136        }
137    }
138}
139