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