1<?php
2// must be run within Dokuwiki
3if(!defined('DOKU_INC')) die();
4
5if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
6require_once(DOKU_PLUGIN.'admin.php');
7require_once(DOKU_INC.'inc/changelog.php');
8
9/**
10 * All DokuWiki plugins to extend the admin function
11 * need to inherit from this class
12 */
13class admin_plugin_oiddelegate extends DokuWiki_Admin_Plugin {
14
15    /**
16     * return some info
17     */
18    function getInfo(){
19        return confToHash(dirname(__FILE__).'/plugin.info.txt');
20    }
21
22
23    /**
24     * return sort order for position in admin menu
25     */
26    function getMenuSort() {
27        return 80;
28    }
29
30    /**
31     * handle user request
32     */
33    function handle() {
34        if(!is_array($_REQUEST['oiddel'])) return;
35
36
37        // new entry? try to detect values
38        if(is_array($_REQUEST['oidadd'])){
39            $new = array();
40            $new[0] = trim($_REQUEST['oidadd'][0]);
41            $new[1]  = trim($_REQUEST['oidadd'][1]);
42            if($new[0] && $new[1]){
43                if(!preg_match('#^https?://#',$new[1])) $new[1] = 'http://'.$new[1];
44
45                // get OID page
46                require_once(DOKU_INC.'inc/HTTPClient.php');
47                $http = new DokuHTTPClient();
48                $http->max_bodysize = 1024*5; // should be enough
49                $http->max_bodysize_abort = false;
50                $data = $http->get($new[1]);
51
52                if(!$data){
53                    msg(sprintf($this->getLang('httperror'),hsc($http->error),-1));
54                }
55
56                // match values
57                if(preg_match('/<.*?(rel=["\']openid.server["\']).*?>/i',$data,$match)){
58                    if(preg_match('/href=["\'](.*?)["\']/i',$match[0],$match)){
59                        $new[2] = $match[1];
60                    }
61                }
62                if(preg_match('/<.*?(rel=["\']openid2.provider["\']).*?>/i',$data,$match)){
63                    if(preg_match('/href=["\'](.*?)["\']/i',$match[0],$match)){
64                        $new[3] = $match[1];
65                    }
66                }
67                if(preg_match('/<.*?(http-equiv=["\']X-XRDS-Location["\']).*?>/i',$data,$match)){
68                    if(preg_match('/content=["\'](.*?)["\']/i',$match[0],$match)){
69                        $new[4] = $match[1];
70                    }
71                }
72                if($http->resp_headers['x-xrds-location']){
73                    $new[4] = $http->resp_headers['x-xrds-location'];
74                }
75            }else{
76                unset($new);
77            }
78        }
79
80        // prepare new config file
81        $data = "# OpenID Delegation Setup\n\n";
82        foreach($_REQUEST['oiddel'] as $line){
83            $line = array_map('trim',$line);
84            if(!$line[0]) continue;
85
86            // make sure OpenIDs and servers are given as full qualified URLs
87            for($i=1; $i<5; $i++){
88                if($line[$i] && !preg_match('#^https?://#',$line[$i])){
89                    $line[$i] = 'http://'.$line[$i];
90                }
91            }
92
93            $data .= join("\t",$line)."\n";
94        }
95        // add new entry
96        if($new){
97            $data .= join("\t",$new)."\n";
98        }
99
100
101        //save it
102        if(io_saveFile(DOKU_CONF.'openid-delegates.conf',$data)){
103            msg($this->getLang('saved'),1);
104        }
105    }
106
107    /**
108     * output appropriate html
109     */
110    function html() {
111        global $lang;
112
113        echo $this->locale_xhtml('intro');
114
115
116        echo '<form action="" method="post">';
117        echo '<table class="inline" id="openid__delegates">';
118
119        echo '<tr>';
120        echo '<th>'.$this->getLang('page').'</th>';
121        echo '<th>'.$this->getLang('oid').'</th>';
122        echo '<th>'.$this->getLang('server').'</th>';
123        echo '<th>'.$this->getLang('provider').'</th>';
124        echo '<th>'.$this->getLang('xrds').'</th>';
125        echo '</tr>';
126
127        $delegates = confToHash(DOKU_CONF.'openid-delegates.conf');
128        ksort($delegates);
129        $row = 0;
130        foreach($delegates as $page => $delegate){
131            list($oid,$server,$provider,$xrds) = preg_split('/\s+/',$delegate,4);
132            $oid      = trim($oid);
133            $server   = trim($server);
134            $provider = trim($provider);
135            $xrds     = trim($xrds);
136
137            echo '<tr>';
138            echo '<td><input type="text" class="edit" name="oiddel['.$row.'][0]" value="'.hsc($page).'" /></td>';
139            echo '<td><input type="text" class="edit" name="oiddel['.$row.'][1]" value="'.hsc($oid).'" /></td>';
140            echo '<td><input type="text" class="edit" name="oiddel['.$row.'][2]" value="'.hsc($server).'" /></td>';
141            echo '<td><input type="text" class="edit" name="oiddel['.$row.'][3]" value="'.hsc($provider).'" /></td>';
142            echo '<td><input type="text" class="edit" name="oiddel['.$row.'][4]" value="'.hsc($xrds).'" /></td>';
143            echo '</tr>';
144
145            $row++;
146        }
147
148
149        echo '<tr>';
150        echo '<td><input type="text" class="edit" name="oidadd[0]" value="" /></td>';
151        echo '<td><input type="text" class="edit" name="oidadd[1]" value="" /></td>';
152        echo '<th colspan="3">'.$this->getLang('add').'</th>';
153        echo '</tr>';
154
155        echo '<tr>';
156        echo '<th colspan="5" align="center">';
157        echo '<input type="submit" value="'.$this->getLang('submit').'" class="button" />';
158        echo '</th>';
159        echo '</tr>';
160
161        echo '</table>';
162        echo '</form>';
163    }
164
165
166}
167//Setup VIM: ex: et ts=4 enc=utf-8 :
168