1<?php
2/**
3 * Federated Login for DokuWiki - helper class
4 *
5 * Enables your DokuWiki to provide users with
6 * Hybrid OAuth + OpenId federated login.
7 *
8 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
9 * @link       http://www.dokuwiki.org/plugin:fedauth
10 * @author     Aoi Karasu <aoikarasu@gmail.com>
11 */
12
13// must be run within Dokuwiki
14if (!defined('DOKU_INC')) die();
15
16if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
17if (!defined('FEDAUTH_PLUGIN')) define ('FEDAUTH_PLUGIN', DOKU_PLUGIN . 'fedauth/');
18if (!defined('ADMIN_CMD_SCOPE')) define('ADMIN_CMD_SCOPE', 'adm');
19
20require_once(DOKU_PLUGIN . 'admin.php');
21require_once(FEDAUTH_PLUGIN . 'common.php');
22require_once(FEDAUTH_PLUGIN . "classes/fa_base.class.php");
23require_once(FEDAUTH_PLUGIN . "classes/adm/fa_manage.adm.class.php");
24
25class admin_plugin_fedauth extends DokuWiki_Admin_Plugin {
26
27    var $provid = '';
28    var $cmd = '';
29    var $handler = null;
30
31    var $providers = null;
32
33    var $functions = array('details','movedn','moveup','remove','uselarge','usesmall'); // require a provider id
34    var $commands = array('add','manage','restore','toggle'); // don't require a provider id
35
36    var $msg = '';
37    var $err = '';
38
39    /**
40     * Returns the plugin meta information.
41     */
42    function getInfo() {
43        return array(
44            'author' => 'Aoi Karasu',
45            'email'  => 'aoikarasu@gmail.com',
46            'date'   => '2012-06-09',
47            'name'   => 'Federated Login Plugin',
48            'desc'   => 'Functions to configure authorization providers',
49            'url'    => 'http://www.dokuwiki.org/plugin:fedauth',
50        );
51    }
52
53    /**
54     * Restricts the access to admins only.
55     */
56    function forAdminOnly() {
57        return $this->getConf('adminsonly');
58    }
59
60    /**
61     * Handles configuration page requests.
62     */
63    function handle() {
64        // enable direct access to language strings
65        $this->setupLocale();
66
67        $fa = $_REQUEST['fa'];
68        if (is_array($fa)) {
69            $this->cmd = key($fa);
70            $this->provid = is_array($fa[$this->cmd]) ? key($fa[$this->cmd]) : null;
71        } else {
72            $this->cmd = $fa;
73            $this->provid = null;
74        }
75
76        // load helper plugin
77        if ($helper =& plugin_load('helper', 'fedauth')) {
78            $this->providers = $helper->getProviders();
79        }
80
81        // verify $_REQUEST vars
82        if (in_array($this->cmd, $this->commands)) {
83            $this->provid = '';
84        } else if (!in_array($this->cmd, $this->functions) || !$this->providers->get($this->provid)) {
85            $this->cmd = 'manage';
86            $this->provid = '';
87        }
88
89        if(($this->cmd != 'manage' || $this->provid != '') && !checkSecurityToken()){
90            $this->cmd = 'manage';
91            $this->provid = '';
92        }
93
94        // load command class and process the command
95        $this->handler =& load_handler_class($this, $this->cmd, ADMIN_CMD_SCOPE, $this->provid, 'manage');
96        $result = $this->handler->process();
97        if (is_array($result) && empty($_REQUEST['ajax'])) {
98            msg($result['msg'], $result['code']);
99        }
100    }
101
102    /**
103     * Outputs data for AJAX call.
104     */
105    function ajax() {
106        if (!$this->getConf('useajax')) return;
107
108        // enable direct access to language strings
109        $this->setupLocale();
110
111        if ($this->handler === NULL) $this->handler = new fa_manage($this, $this->cmd, $this->provid);
112
113        if (!$this->handler->ajax()) {
114            print "Unrecognized ajax call: " . $this->cmd;
115        }
116    }
117
118    /**
119     * Outputs configuration page.
120     */
121    function html() {
122        // enable direct access to language strings
123        $this->setupLocale();
124
125        if ($this->handler === NULL) $this->handler = new fa_manage($this, $this->provid);
126
127        ptln('<div id="fedauth__manager">');
128        if ($this->getConf('useajax')) {
129            print plugin_script_block('admin');
130        }
131        $this->handler->html();
132        ptln('</div><!-- #fedauth__manager -->');
133    }
134
135} /* admin_plugin_federate */
136
137/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
138