xref: /plugin/oauth/helper.php (revision 3c0138db6331751d1ea0948f418a981582962f18)
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
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12class helper_plugin_oauth extends DokuWiki_Plugin {
13
14    /**
15     * Load the needed libraries and initialize the named oAuth service
16     *
17     * @param string $servicename
18     * @return null|\OAuth\Plugin\AbstractAdapter
19     */
20    public function loadService(&$servicename) {
21        $id = getID(); // $ID isn't set in trustExternal, yet
22
23        $servicename = preg_replace('/[^a-zA-Z_]+/', '', $servicename);
24        if(!$servicename) return null;
25
26        require_once(__DIR__.'/phpoauthlib/src/OAuth/bootstrap.php');
27        require_once(__DIR__.'/classes/AbstractAdapter.php');
28        require_once(__DIR__.'/classes/oAuthHTTPClient.php');
29
30        $file = __DIR__.'/classes/'.$servicename.'Adapter.php';
31        if(!file_exists($file)) return null;
32        require_once($file);
33        $class = '\\OAuth\\Plugin\\'.$servicename.'Adapter';
34
35        /** @var \OAuth\Plugin\AbstractAdapter $service */
36        $rdurl = wl($id, array('oa' => $servicename), true, '&');
37        dbglog($rdurl);
38        $service = new $class($rdurl);
39        if(!$service->isInitialized()) {
40            msg("Failed to initialize $service authentication service. Check credentials", -1);
41            return null;
42        }
43
44        return $service;
45    }
46
47    /**
48     * List available Services
49     *
50     * @param bool $enabledonly list only enabled services
51     * @return array
52     */
53    public function listServices($enabledonly = true) {
54        $services = array();
55        $files    = glob(__DIR__.'/classes/*Adapter.php');
56
57        foreach($files as $file) {
58            $file = basename($file, 'Adapter.php');
59            if($file == 'Abstract') continue;
60            if($enabledonly && !$this->getKey($file)) continue;
61            $services[] = $file;
62        }
63
64        return $services;
65    }
66
67    /**
68     * Return the configured key for the given service
69     *
70     * @param $service
71     * @return string
72     */
73    public function getKey($service) {
74        $service = strtolower($service);
75        return $this->getConf($service.'-key');
76    }
77
78    /**
79     * Return the configured secret for the given service
80     *
81     * @param $service
82     * @return string
83     */
84    public function getSecret($service) {
85        $service = strtolower($service);
86        return $this->getConf($service.'-secret');
87    }
88
89}
90
91// vim:ts=4:sw=4:et:
92