1<?php
2
3/**
4 * DokuWiki DAVCal PlugIn - Ajax component
5 */
6
7if(!defined('DOKU_INC')) die();
8
9class action_plugin_davcal_ajax extends DokuWiki_Action_Plugin {
10
11    /**
12     * @var helper_plugin_davcal
13     */
14    private $hlp = null;
15
16    function __construct() {
17        $this->hlp =& plugin_load('helper','davcal');
18    }
19
20    function register(Doku_Event_Handler $controller) {
21        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax_call_unknown');
22    }
23
24    function handle_ajax_call_unknown(&$event, $param) {
25      if($event->data != 'plugin_davcal') return;
26
27      $event->preventDefault();
28      $event->stopPropagation();
29      global $INPUT;
30
31      $action = trim($INPUT->post->str('action'));
32      $id = trim($INPUT->post->str('id'));
33      $page = trim($INPUT->post->str('page'));
34      $params = $INPUT->post->arr('params');
35      if(isset($_SERVER['REMOTE_USER']) && !is_null($_SERVER['REMOTE_USER']))
36        $user = $_SERVER['REMOTE_USER'];
37      else
38        $user = null;
39      $write = false;
40
41      if(!checkSecurityToken())
42      {
43          echo "CSRF Attack.";
44          return;
45      }
46
47      $data = array();
48
49      $data['result'] = false;
50      $data['html'] = $this->getLang('unknown_error');
51
52      // Check if we have access to the calendar ($id is given by parameters,
53      // that's not necessarily the page we come from)
54
55      $acl = $this->hlp->checkCalendarPermission($id);
56      if($acl > AUTH_READ)
57      {
58          $write = true;
59      }
60      elseif($acl < AUTH_READ)
61      {
62          $data['result'] = false;
63          $data['html'] = $this->getLang('no_permission');
64          // Set to an invalid action in order to just return the result
65          $action = 'invalid';
66      }
67
68      // Retrieve the calendar pages based on the meta data
69      $calendarPages = $this->hlp->getCalendarPagesByMeta($page);
70      if($calendarPages === false)
71      {
72          $calendarPages = array($page => null);
73      }
74
75      // Parse the requested action
76      switch($action)
77      {
78          // Add a new Event
79          case 'newEvent':
80              if($write)
81              {
82                  $res = $this->hlp->addCalendarEntryToCalendarForPage($id, $user, $params);
83                  if($res === true)
84                  {
85                    $data['result'] = true;
86                    $data['html'] = $this->getLang('event_added');
87                  }
88                  else
89                  {
90                    $data['result'] = false;
91                    $data['html'] = $this->getLang('unknown_error');
92                  }
93              }
94              else
95              {
96                  $data['result'] = false;
97                  $data['html'] = $this->getLang('no_permission');
98              }
99          break;
100          // Retrieve existing Events
101          case 'getEvents':
102              $startDate = $INPUT->post->str('start');
103              $endDate = $INPUT->post->str('end');
104              $timezone = $INPUT->post->str('timezone');
105              $data = array();
106              foreach($calendarPages as $calPage => $color)
107              {
108                  $data = array_merge($data, $this->hlp->getEventsWithinDateRange($calPage,
109                                      $user, $startDate, $endDate, $timezone, $color));
110              }
111          break;
112          // Edit an event
113          case 'editEvent':
114              if($write)
115              {
116                  $res = $this->hlp->editCalendarEntryForPage($id, $user, $params);
117                  if($res === true)
118                  {
119                    $data['result'] = true;
120                    $data['html'] = $this->getLang('event_edited');
121                  }
122                  else
123                  {
124                    $data['result'] = false;
125                    $data['html'] = $this->getLang('unknown_error');
126                  }
127              }
128              else
129              {
130                  $data['result'] = false;
131                  $data['html'] = $this->getLang('no_permission');
132              }
133          break;
134          // Delete an Event
135          case 'deleteEvent':
136              if($write)
137              {
138                  $res = $this->hlp->deleteCalendarEntryForPage($id, $params);
139                  if($res === true)
140                  {
141                    $data['result'] = true;
142                    $data['html'] = $this->getLang('event_deleted');
143                  }
144                  else
145                  {
146                    $data['result'] = false;
147                    $data['html'] = $this->getLang('unknown_error');
148                  }
149              }
150              else
151              {
152                  $data['result'] = false;
153                  $data['html'] = $this->getLang('no_permission');
154              }
155          break;
156          // Get personal settings
157          case 'getSettings':
158              $data['result'] = true;
159              $data['settings'] = $this->hlp->getPersonalSettings($user);
160              $data['settings']['calids'] = $this->hlp->getCalendarMapForIDs($calendarPages);
161              $data['settings']['readonly'] = !$write;
162              $data['settings']['syncurl'] = $this->hlp->getSyncUrlForPage($page, $user);
163              $data['settings']['privateurl'] = $this->hlp->getPrivateURLForPage($page);
164              $data['settings']['principalurl'] = $this->hlp->getPrincipalUrlForUser($user);
165              $data['settings']['meta'] = $this->hlp->getCalendarMetaForPage($page);
166          break;
167          // Save personal settings
168          case 'saveSettings':
169              $settings = array();
170              $settings['weeknumbers'] = $params['weeknumbers'];
171              $settings['timezone'] = $params['timezone'];
172              $settings['workweek'] = $params['workweek'];
173              $settings['monday'] = $params['monday'];
174              $settings['timeformat'] = $params['timeformat'];
175              if($this->hlp->savePersonalSettings($settings, $user))
176              {
177                  $data['result'] = true;
178                  $data['html'] = $this->getLang('settings_saved');
179              }
180              else
181              {
182                  $data['result'] = false;
183                  $data['html'] = $this->getLang('error_saving');
184              }
185          break;
186      }
187
188      // If we are still here, JSON output is requested
189
190      //json library of DokuWiki
191      require_once DOKU_INC . 'inc/JSON.php';
192      $json = new JSON();
193
194      //set content type
195      header('Content-Type: application/json');
196      echo $json->encode($data);
197    }
198
199}
200