1<?php
2/**
3 * DokuWiki Plugin evesso (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
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12class helper_plugin_evesso extends DokuWiki_Plugin {
13
14    public const CORPORATION_PREFIX = '(corporation)';
15    public const ALLIANCE_PREFIX = '(alliance)';
16    public const FACTION_PREFIX = '(faction)';
17
18    /**
19     * Load the needed libraries and initialize the named oAuth service
20     *
21     * @param string $servicename
22     * @return null|\OAuth\Plugin\AbstractAdapter
23     */
24    public function loadService($servicename = true) {
25        if(!$servicename) return null;
26
27        require_once(__DIR__.'/phpoauthlib/src/OAuth/bootstrap.php');
28        require_once(__DIR__.'/classes/AbstractAdapter.php');
29        require_once(__DIR__.'/classes/oAuthHTTPClient.php');
30        require_once(__DIR__.'/classes/oAuthStorage.php');
31        require_once(__DIR__.'/classes/EveOnlineAdapter.php');
32        /** @var \OAuth\Plugin\AbstractAdapter $service */
33        $service = new \OAuth\Plugin\EveOnlineAdapter();
34        if(!$service->isInitialized()) {
35            msg("Failed to initialize $service authentication service. Check credentials", -1);
36            return null;
37        }
38
39        // The generic service can be externally configured
40        if(is_a($service->oAuth, 'OAuth\\OAuth2\\Service\\Generic')) {
41            $service->oAuth->setAuthorizationEndpoint($this->getAuthEndpoint());
42            $service->oAuth->setAccessTokenEndpoint($this->getTokenEndpoint());
43        }
44
45        return $service;
46
47    }
48
49    /**
50     * The redirect URI used in all oAuth requests
51     *
52     * @return string
53     */
54    public function getRedirectURI() {
55        if ($this->getConf('custom-redirectURI') !== '') {
56            return $this->getConf('custom-redirectURI');
57        } else {
58            return DOKU_URL . DOKU_SCRIPT;
59        }
60    }
61
62    /**
63     * Get service name
64     *
65     * @return string
66     */
67    public function getService() {
68        return 'EveOnline';
69    }
70
71    public function isAuthPlain() {
72        return $this->getConf('singleService') == '';
73    }
74
75    public function isEveAuth() {
76        return $this->getConf('singleService') != '';
77    }
78
79    public function isEveAuthDirect() {
80        return $this->getConf('singleService') == 'EveOnline';
81    }
82
83    /**
84     * Return the configured key for the given service
85     *
86     * @param $service
87     * @return string
88     */
89    public function getKey() {
90        return $this->getConf('eveonline-key');
91    }
92
93    /**
94     * Return the configured secret for the given service
95     *
96     * @param $service
97     * @return string
98     */
99    public function getSecret() {
100        return $this->getConf('eveonline-secret');
101    }
102
103    /**
104     * Return the configured Authentication Endpoint URL for the given service
105     *
106     * @param $service
107     * @return string
108     */
109    public function getAuthEndpoint() {
110        return $this->getConf('eveonline-authurl');
111    }
112
113    /**
114     * Return the configured Access Token Endpoint URL for the given service
115     *
116     * @param $service
117     * @return string
118     */
119    public function getTokenEndpoint() {
120        return $this->getConf('eveonline-tokenurl');
121    }
122
123    /**
124     * @return array
125     */
126    public function getGroup($name) {
127        if ($this->getConf($name) === '') {
128            return array();
129        }
130        $validGroups = explode(',', trim($this->getConf($name), ','));
131        $validGroups = array_map('trim', $validGroups);
132        return $validGroups;
133    }
134
135    /**
136     * @param array $groups
137     *
138     * @return bool
139     */
140    public function inGroup($groups, $names, $empty = true) {
141        $validGroups = array();
142        foreach ($names as $name => $prefix) {
143            foreach ($this->getGroup($name) as $group) {
144                $validGroups[] = $prefix.$group;
145            }
146        }
147
148        if (count($validGroups) == 0) {
149            return $empty; //nothing set
150        }
151
152        foreach ($validGroups as $validGroup) {
153            if (in_array($validGroup, $groups, true)) {
154                return true;
155            }
156        }
157        return false;
158    }
159    /**
160     * @param array $groups
161     *
162     * @return bool
163     */
164    public function checkGroups($groups) {
165        if (in_array('admin', $groups, true)) { //Always allow admins
166            return true;
167        }
168        $require = array(
169            'require-corporation' => helper_plugin_evesso::CORPORATION_PREFIX,
170            'require-alliance' => helper_plugin_evesso::ALLIANCE_PREFIX,
171            'require-faction' => helper_plugin_evesso::FACTION_PREFIX,
172            );
173        if ($this->inGroup($groups, $require)) {
174            return true;
175        }
176        return false;
177    }
178
179    /**
180     * @param array $session cookie auth session
181     *
182     * @return bool
183     */
184    public function validBrowserID ($session) {
185        return $session['buid'] == auth_browseruid();
186    }
187
188    /**
189     * @param array $session cookie auth session
190     *
191     * @return bool
192     */
193    public function isSessionTimedOut ($session) {
194        global $conf;
195        return $session['time'] < time() - $conf['auth_security_timeout'];
196    }
197
198    /**
199     * @return bool
200     */
201    public function isGETRequest () {
202        global $INPUT;
203        $result = $INPUT->server->str('REQUEST_METHOD') === 'GET';
204        return $result;
205    }
206
207    /**
208     * check if we are handling a request to doku.php. Only doku.php defines $updateVersion
209     *
210     * @return bool
211     */
212    public function isDokuPHP() {
213        global $updateVersion;
214        return isset($updateVersion);
215    }
216}
217
218// vim:ts=4:sw=4:et:
219