xref: /plugin/oauth/helper.php (revision 6d9a8a49e2ddf3e7f5338a74a91c704edb6dc965)
180852c15SAndreas Gohr<?php
280852c15SAndreas Gohr/**
380852c15SAndreas Gohr * DokuWiki Plugin oauth (Helper Component)
480852c15SAndreas Gohr *
580852c15SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
680852c15SAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
780852c15SAndreas Gohr */
880852c15SAndreas Gohr
998a36116SAndreas Gohruse dokuwiki\Extension\Event;
1098a36116SAndreas Gohruse dokuwiki\plugin\oauth\Service;
1180852c15SAndreas Gohr
1298a36116SAndreas Gohrrequire_once(__DIR__ . '/vendor/autoload.php');
1398a36116SAndreas Gohr
1498a36116SAndreas Gohr/**
1598a36116SAndreas Gohr * Basic helper methods for the oauth flow
1698a36116SAndreas Gohr */
1798a36116SAndreas Gohrclass helper_plugin_oauth extends DokuWiki_Plugin
1898a36116SAndreas Gohr{
1980852c15SAndreas Gohr
2080852c15SAndreas Gohr    /**
21f10e09e2SAndreas Gohr     * Load the needed libraries and initialize the named oAuth service
2280852c15SAndreas Gohr     *
23f10e09e2SAndreas Gohr     * @param string $servicename
2498a36116SAndreas Gohr     * @return null|Service
2580852c15SAndreas Gohr     */
2698a36116SAndreas Gohr    public function loadService($servicename)
2798a36116SAndreas Gohr    {
2898a36116SAndreas Gohr        $services = $this->listServices(true);
2998a36116SAndreas Gohr        if (!isset($services[$servicename])) return null;
30*6d9a8a49SAndreas Gohr        return $services[$servicename];
31f10e09e2SAndreas Gohr    }
32f10e09e2SAndreas Gohr
33a90c044eSAndreas Gohr    /**
34a90c044eSAndreas Gohr     * The redirect URI used in all oAuth requests
35a90c044eSAndreas Gohr     *
36a90c044eSAndreas Gohr     * @return string
37a90c044eSAndreas Gohr     */
3898a36116SAndreas Gohr    public function redirectURI()
3998a36116SAndreas Gohr    {
409683193cSMichael Große        if ($this->getConf('custom-redirectURI') !== '') {
419683193cSMichael Große            return $this->getConf('custom-redirectURI');
429683193cSMichael Große        } else {
432e94f0b8SAndreas Gohr            return DOKU_URL . DOKU_SCRIPT;
442e94f0b8SAndreas Gohr        }
459683193cSMichael Große    }
462e94f0b8SAndreas Gohr
47dfbdd519SAndreas Gohr    /**
48dfbdd519SAndreas Gohr     * List available Services
49dfbdd519SAndreas Gohr     *
50*6d9a8a49SAndreas Gohr     * Services returned here, do not have initialized oAuth providers yet!
51*6d9a8a49SAndreas Gohr     *
5298a36116SAndreas Gohr     * @param bool $enabledonly list only services that have been configured
5398a36116SAndreas Gohr     * @triggers PLUGIN_OAUTH_BACKEND_REGISTER
5498a36116SAndreas Gohr     * @return Service[] list of service objects
55dfbdd519SAndreas Gohr     */
5698a36116SAndreas Gohr    public function listServices($enabledonly = true)
5798a36116SAndreas Gohr    {
5898a36116SAndreas Gohr        $services = [];
5998a36116SAndreas Gohr        $event = new Event('PLUGIN_OAUTH_BACKEND_REGISTER', $services);
6098a36116SAndreas Gohr        $event->advise_before(false);
6198a36116SAndreas Gohr        $event->advise_after();
62dfbdd519SAndreas Gohr
6398a36116SAndreas Gohr        // filter out unconfigured services
6498a36116SAndreas Gohr        if ($enabledonly) {
6598a36116SAndreas Gohr            $services = array_filter($services, function ($service) {
6698a36116SAndreas Gohr                /** @var Service $service */
6798a36116SAndreas Gohr                return (bool)$service->getKey();
6898a36116SAndreas Gohr            });
69dfbdd519SAndreas Gohr        }
70dfbdd519SAndreas Gohr
71dfbdd519SAndreas Gohr        return $services;
72dfbdd519SAndreas Gohr    }
73f10e09e2SAndreas Gohr
74f10e09e2SAndreas Gohr    /**
75ebf681d1SMichael Große     * @return array
76d9818adbSMichael Große     */
7798a36116SAndreas Gohr    public function getValidDomains()
7898a36116SAndreas Gohr    {
793c0c1b14SMichael Große        if ($this->getConf('mailRestriction') === '') {
803c0c1b14SMichael Große            return array();
813c0c1b14SMichael Große        }
82d9818adbSMichael Große        $validDomains = explode(',', trim($this->getConf('mailRestriction'), ','));
83ebf681d1SMichael Große        $validDomains = array_map('trim', $validDomains);
84d9818adbSMichael Große        return $validDomains;
85d9818adbSMichael Große    }
86d9818adbSMichael Große
87d9818adbSMichael Große    /**
88d9818adbSMichael Große     * @param string $mail
89d9818adbSMichael Große     *
90d9818adbSMichael Große     * @return bool
91d9818adbSMichael Große     */
9298a36116SAndreas Gohr    public function checkMail($mail)
9398a36116SAndreas Gohr    {
94d9818adbSMichael Große        $hostedDomains = $this->getValidDomains();
95d9818adbSMichael Große
96d9818adbSMichael Große        foreach ($hostedDomains as $validDomain) {
97d9818adbSMichael Große            if (substr($mail, -strlen($validDomain)) === $validDomain) {
98d9818adbSMichael Große                return true;
99d9818adbSMichael Große            }
100d9818adbSMichael Große        }
101d9818adbSMichael Große        return false;
102d9818adbSMichael Große    }
103f2e164b0SMichael Große
104f2e164b0SMichael Große    /**
105f2e164b0SMichael Große     * @return bool
106f2e164b0SMichael Große     */
10798a36116SAndreas Gohr    public function isGETRequest()
10898a36116SAndreas Gohr    {
109f2e164b0SMichael Große        global $INPUT;
11098a36116SAndreas Gohr        return $INPUT->server->str('REQUEST_METHOD') === 'GET';
111f2e164b0SMichael Große    }
112f2e164b0SMichael Große
113f2e164b0SMichael Große    /**
114f2e164b0SMichael Große     * check if we are handling a request to doku.php. Only doku.php defines $updateVersion
115f2e164b0SMichael Große     *
116f2e164b0SMichael Große     * @return bool
117f2e164b0SMichael Große     */
11898a36116SAndreas Gohr    public function isDokuPHP()
11998a36116SAndreas Gohr    {
120f2e164b0SMichael Große        global $updateVersion;
121f2e164b0SMichael Große        return isset($updateVersion);
122f2e164b0SMichael Große    }
1233e7ac5b1SAndreas Gohr
1243e7ac5b1SAndreas Gohr    /**
1253e7ac5b1SAndreas Gohr     * Display an exception to the user
1263e7ac5b1SAndreas Gohr     *
1273e7ac5b1SAndreas Gohr     * @param Exception $e
1283e7ac5b1SAndreas Gohr     * @param string $prefix - user friendly explanation if available
1293e7ac5b1SAndreas Gohr     */
1303e7ac5b1SAndreas Gohr    public function showException(\Exception $e, $prefix = '')
1313e7ac5b1SAndreas Gohr    {
1323e7ac5b1SAndreas Gohr        global $conf;
1333e7ac5b1SAndreas Gohr        msg('OAuth: ' . $prefix . ' ' . hsc($e->getMessage()), -1);
1343e7ac5b1SAndreas Gohr        if ($conf['allowdebug']) {
1353e7ac5b1SAndreas Gohr            msg('<pre>' . hsc($e->getTraceAsString()) . '</pre>', -1);
1363e7ac5b1SAndreas Gohr        }
1373e7ac5b1SAndreas Gohr    }
13880852c15SAndreas Gohr}
139