1<?php
2/**
3 * DokuWiki Plugin webdavclient (Admin Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Boehler <dev@aboehler.at>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11require_once(DOKU_PLUGIN.'admin.php');
12
13class admin_plugin_webdavclient extends DokuWiki_Admin_Plugin {
14
15    protected $hlp = null;
16    protected $error = false;
17    protected $errmsg = '';
18    protected $action = null;
19    protected $result = null;
20
21    /**
22     * Constructor. Load helper plugin
23     */
24    function admin_plugin_webdavclient(){
25        $this->hlp =& plugin_load('helper', 'webdavclient');
26        if(is_null($this->hlp))
27          msg('Error loading WebDAV Client helper module!');
28    }
29
30    function getMenuSort()
31    {
32        return 501;
33    }
34
35    function handle()
36    {
37      global $INPUT;
38
39      if (!isset($_REQUEST['cmd']))
40        return;   // first time - nothing to do
41
42      if (!checkSecurityToken())
43        return;
44      if (!is_array($_REQUEST['cmd']))
45        return;
46
47      $fn = $_REQUEST['cmd'];
48
49      if (is_array($fn))
50      {
51        $cmd = key($fn);
52        $param = is_array($fn[$cmd]) ? key($fn[$cmd]) : null;
53      }
54      else
55      {
56        $cmd = $fn;
57        $param = null;
58      }
59
60      $this->action = $cmd;
61
62      // Parse the command and react accordingly
63      switch($cmd)
64      {
65          case 'forcesync':
66            $connid = $param;
67            if($this->hlp->syncConnection($connid, true, true) === false)
68            {
69                $this->error = true;
70                $this->errmsg = $this->hlp->getLastError();
71            }
72            break;
73          case 'forceresync':
74            $connid = $param;
75            if($this->hlp->syncConnection($connid, true, true, true) === false)
76            {
77                $this->error = true;
78                $this->errmsg = $this->hlp->getLastError();
79            }
80            break;
81          case 'delete':
82            $connid = $param;
83            $this->hlp->deleteConnection($connid);
84            break;
85          case 'modify':
86            $connid = $param;
87            $dn = $_REQUEST['moddn'][$connid];
88            $permission = $_REQUEST['modperm'][$connid];
89            $syncinterval = $_REQUEST['modsyncinterval'][$connid];
90            $write = $_REQUEST['modwrite'][$connid];
91            $active = $_REQUEST['modactive'][$connid];
92            $this->hlp->modifyConnection($connid, $permission, $dn, $syncinterval, $write, $active);
93            break;
94          case 'add':
95            // FIXME: Should we sanity-check the settings and query the server for correctness first?
96            $uri = $_REQUEST['manuri'];
97            $dn = $_REQUEST['mandn'];
98            $username = $_REQUEST['manusername'];
99            $password = $_REQUEST['manpassword'];
100            $type = $_REQUEST['mantype'];
101            $this->hlp->addConnection($uri, $username, $password, $dn, $dn, $type, '3600', false, false);
102            break;
103          case 'addselected':
104            $calendars = $_REQUEST['cb']['calendar'];
105            $addressbooks = $_REQUEST['cb']['addressbook'];
106            if(count($calendars) == 0 && count($addressbooks) == 0)
107            {
108                $this->error = true;
109                $this->errmsg = $this->getLang('nothing_selected');
110                return;
111            }
112            foreach($calendars as $cal)
113            {
114                $idx = intval($cal);
115                $this->hlp->addConnection($_REQUEST['calendar'][$idx], $_REQUEST['addusername'], $_REQUEST['addpassword'], $_REQUEST['calendardn'][$idx], $_REQUEST['calendardn'][$idx], 'calendar', '3600', false, false);
116            }
117            foreach($addressbooks as $addr)
118            {
119                $idx = intval($addr);
120                $this->hlp->addConnection($_REQUEST['addressbook'][$idx], $_REQUEST['addusername'], $_REQUEST['addpassword'], $_REQUEST['addressbookdn'][$idx], $_REQUEST['addressbookdn'][$idx], 'contacts', '3600', false, false);
121            }
122            break;
123          case 'empty':
124            $this->result['connid'] = $param;
125            break;
126          case 'reallyempty':
127            $connid = $param;
128            $this->hlp->deleteAllEntries($connid);
129            break;
130          case 'discover':
131            $username = $INPUT->str('username', '');
132            $password = $INPUT->str('password', '');
133            $uri = $INPUT->str('uri', '');
134            if(($username === '') || ($password === '') || ($uri === ''))
135            {
136                $this->error = true;
137                $this->errmsg = $this->getLang('empty_input');
138                return;
139            }
140            $this->result = $this->hlp->queryServer($uri, $username, $password);
141            $this->result['username'] = $username;
142            $this->result['password'] = $password;
143            $this->result['uri'] = $uri;
144            break;
145          default:
146            break;
147      }
148
149    }
150
151
152    function html()
153    {
154      ptln('<h1>WebDAV Client</h1>');
155
156      ptln('<h2>'.$this->getLang('existing_connections').'</h2>');
157
158      ptln('<form action="'.wl($ID).'" method="post">');
159
160      // output hidden values to ensure dokuwiki will return back to this plugin
161      ptln('  <input type="hidden" name="do"   value="admin" />');
162      ptln('  <input type="hidden" name="page" value="'.$this->getPluginName().'" />');
163      formSecurityToken();
164
165      if($this->error === true)
166      {
167          ptln($this->errmsg);
168      }
169      else
170      {
171          switch($this->action)
172          {
173              case 'empty':
174                ptln($this->getLang('reallyempty'));
175                ptln('<input type="submit" name="cmd[reallyempty]['.$this->result['connid'].']" value="' . $this->getLang('empty').'">');
176              break;
177              case 'discover':
178                if(count($this->result['calendars']) == 0 && count($this->result['addressbooks']) == 0)
179                {
180                    ptln($this->getLang('nothing_found'));
181                    break;
182                }
183                ptln('<h3>'.$this->getLang('calendars_found').'</h3>');
184                ptln('<table>');
185                ptln('<tr><th>'.$this->getLang('select').'</th><th>'.
186                    $this->getLang('name').'</th><th>'.$this->getLang('uri').'</th></tr>');
187                $idx = 0;
188                foreach($this->result['calendars'] as $href => $dn)
189                {
190                    ptln('<tr><td><input type="checkbox" name="cb[calendar][]" value="'.$idx.'"></td><td>'.
191                        '<input type="hidden" name="calendar['.$idx.']" value="'.$href.'">'.
192                        '<input type="hidden" name="calendardn['.$idx.']" value="'.$dn.'">'.
193                        hsc($dn).'</td><td>'.hsc($href).'</td></tr>');
194                    $idx++;
195                }
196                ptln('</table>');
197                ptln('<h3>'.$this->getLang('addressbooks_found').'</h3>');
198                ptln('<table>');
199                ptln('<tr><th>'.$this->getLang('select').'</th><th>'.
200                    $this->getLang('name').'</th><th>'.$this->getLang('uri').'</th></tr>');
201                $idx = 0;
202                foreach($this->result['addressbooks'] as $href => $dn)
203                {
204                    ptln('<tr><td><input type="checkbox" name="cb[addressbook][]" value="'.$idx.'"></td><td>'.
205                        '<input type="hidden" name="addressbook['.$idx.']" value="'.$href.'">'.
206                        '<input type="hidden" name="addressbookdn['.$idx.']" value="'.$dn.'">'.
207                        hsc($dn).'</td><td>'.hsc($href).'</td></tr>');
208                    $idx++;
209                }
210                ptln('</table>');
211                ptln('<input type="hidden" name="addusername" value="'.$this->result['username'].'">');
212                ptln('<input type="hidden" name="addpassword" value="'.$this->result['password'].'">');
213                ptln('<input type="submit" name="cmd[addselected]" value="'.
214                    $this->getLang('add_selected').'">');
215          }
216      }
217
218      ptln('<table>');
219      $connections = $this->hlp->getConnections();
220
221      ptln('<tr>');
222      ptln('<th>'.$this->getLang('id').'</th><th>'.$this->getLang('name').'</th><th>'.
223      $this->getLang('syncinterval').'</th><th>'.$this->getLang('active').
224        '</th><th>'.$this->getLang('write').'</th><th>'.$this->getLang('permission').'</th><th>'.$this->getLang('action').'</th>');
225      ptln('</tr>');
226
227      foreach($connections as $conn)
228      {
229          ptln('<tr>');
230          ptln('  <td>'.hsc($conn['id']).
231            '</td><td><input type="text" name="moddn['.$conn['id'].']" value="'.$conn['displayname'].'">'.
232            '</td><td><input type="text" size="5" name="modsyncinterval['.$conn['id'].']" value="'.$conn['syncinterval'].'">'.
233            '</td><td><select name="modactive['.$conn['id'].']">'.
234            '<option value="1" '. (($conn['active']) ? 'selected="selected"' : '').'>'.
235                $this->getLang('active').'</option>'.
236            '<option value="0" '. (($conn['active']) ? '' : 'selected="selected"').'>'.
237                $this->getLang('inactive').'</option>'.
238            '</select>'.
239            '</td><td><select name="modwrite['.$conn['id'].']"'.
240            (($conn['type'] === 'icsfeed') ? ' disabled' : '').'>'.
241            '<option value="1" '. (($conn['write']) ? 'selected="selected"' : '').'>'.
242                $this->getLang('write').'</option>'.
243            '<option value="0" '. (($conn['write']) ? '' : 'selected="selected"').'>'.
244                $this->getLang('nowrite').'</option>'.
245            '</select>'.
246            '</td><td><input type="text" name="modperm['.$conn['id'].']" value="'.$conn['permission'].'">'.
247            '</td><td><input type="submit" name="cmd[modify]['.$conn['id'].']" value="'.
248            $this->getLang('modify').'" /><input type="submit" name="cmd[delete]['.$conn['id'].']" value="'.
249            $this->getLang('delete').'" /><input type="submit" name="cmd[forcesync]['.$conn['id'].']" value="'.
250            $this->getLang('forcesync').'" /><input type="submit" name="cmd[forceresync]['.
251            $conn['id'].']" value="'.$this->getLang('forceresync').'" /><input type="submit" name="cmd[empty]['.
252            $conn['id'].']" value="'.$this->getLang('empty').'" /></td>');
253          ptln('</tr>');
254      }
255
256      ptln('</table>');
257
258      ptln('<div style="width:46%; float:left">');
259      ptln('<h2>'.$this->getLang('add_connection').'</h2>');
260      ptln($this->getLang('discovery_text'));
261      ptln('<table>');
262      ptln('<tr><td>'.$this->getLang('uri').'</td><td><input type="text" name="uri"></td></tr>');
263      ptln('<tr><td>'.$this->getLang('username').'</td><td><input type="text" name="username"></td></tr>');
264      ptln('<tr><td>'.$this->getLang('password').'</td><td><input type="password" name="password"></td></tr>');
265      ptln('<tr><td></td><td><input type="submit" name="cmd[discover]" value="'.$this->getLang('discover').'">'.
266        '</td></tr>');
267      ptln('</table>');
268      ptln('</div>');
269      ptln('<div style="width:46%; float:left">');
270      ptln('<h2>'.$this->getLang('add_connection_manual').'</h2>');
271      ptln($this->getLang('add_text'));
272      ptln('<table>');
273      ptln('<tr><td>'.$this->getLang('uri').'</td><td><input type="text" name="manuri"></td></tr>');
274      ptln('<tr><td>'.$this->getLang('name').'</td><td><input type="text" name="mandn"></td></tr>');
275      ptln('<tr><td>'.$this->getLang('type').'</td><td><select name="mantype"><option value="calendar">'.
276        $this->getLang('calendar').'</option><option value="contacts">'.
277        $this->getLang('contacts').'</option><option value="icsfeed">'.
278        $this->getLang('icsfeed').'</option></select></td></tr>');
279      ptln('<tr><td>'.$this->getLang('username').'</td><td><input type="text" name="manusername"></td></tr>');
280      ptln('<tr><td>'.$this->getLang('password').'</td><td><input type="password" name="manpassword"></td></tr>');
281      ptln('<tr><td></td><td><input type="submit" name="cmd[add]" value="'.$this->getLang('add').'">'.
282        '</td></tr>');
283      ptln('</table>');
284      ptln('</div>');
285
286
287      ptln('</form>');
288    }
289
290}
291
292// vim:ts=4:sw=4:et:enc=utf-8:
293