xref: /plugin/davcal/action/ajax.php (revision eabb1849ef5d3bf0546cfb3502ac79e6ce3833dd)
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      $multi = false;
41
42      if(!checkSecurityToken())
43      {
44          echo "CSRF Attack.";
45          return;
46      }
47
48      $data = array();
49
50      $data['result'] = false;
51      $data['html'] = $this->getLang('unknown_error');
52
53      // Check if we have access to the calendar ($id is given by parameters,
54      // that's not necessarily the page we come from)
55      $acl = auth_quickaclcheck($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      if(count($calendarPages) > 1)
75        $multi = true;
76
77      // Parse the requested action
78      switch($action)
79      {
80          // Add a new Event
81          case 'newEvent':
82              if($write)
83              {
84                  $data['result'] = true;
85                  $data['html'] = $this->getLang('event_added');
86                  $this->hlp->addCalendarEntryToCalendarForPage($id, $user, $params);
87              }
88              else
89              {
90                  $data['result'] = false;
91                  $data['html'] = $this->getLang('no_permission');
92              }
93          break;
94          // Retrieve existing Events
95          case 'getEvents':
96              $startDate = $INPUT->post->str('start');
97              $endDate = $INPUT->post->str('end');
98              $timezone = $INPUT->post->str('timezone');
99              $data = array();
100              foreach($calendarPages as $calPage => $color)
101              {
102                  $data = array_merge($data, $this->hlp->getEventsWithinDateRange($calPage,
103                                      $user, $startDate, $endDate, $timezone, $color));
104              }
105          break;
106          // Edit an event
107          case 'editEvent':
108              if($write)
109              {
110                  $data['result'] = true;
111                  $data['html'] = $this->getLang('event_edited');
112                  $this->hlp->editCalendarEntryForPage($id, $user, $params);
113              }
114              else
115              {
116                  $data['result'] = false;
117                  $data['html'] = $this->getLang('no_permission');
118              }
119          break;
120          // Delete an Event
121          case 'deleteEvent':
122              if($write)
123              {
124                  $data['result'] = true;
125                  $data['html'] = $this->getLang('event_deleted');
126                  $this->hlp->deleteCalendarEntryForPage($id, $params);
127              }
128              else
129              {
130                  $data['result'] = false;
131                  $data['html'] = $this->getLang('no_permission');
132              }
133          break;
134          // Get personal settings
135          case 'getSettings':
136              $data['result'] = true;
137              $data['settings'] = $this->hlp->getPersonalSettings($user);
138              $data['settings']['multi'] = $multi;
139              $data['settings']['calids'] = $this->hlp->getCalendarMapForIDs($calendarPages);
140              $data['settings']['readonly'] = !$write;
141              $data['settings']['syncurl'] = $this->hlp->getSyncUrlForPage($page, $user);
142              $data['settings']['privateurl'] = $this->hlp->getPrivateURLForPage($page);
143              $data['settings']['principalurl'] = $this->hlp->getPrincipalUrlForUser($user);
144              $data['settings']['meta'] = $this->hlp->getCalendarMetaForPage($page);
145          break;
146          // Save personal settings
147          case 'saveSettings':
148              $settings = array();
149              $settings['weeknumbers'] = $params['weeknumbers'];
150              $settings['timezone'] = $params['timezone'];
151              $settings['workweek'] = $params['workweek'];
152              $settings['monday'] = $params['monday'];
153              $settings['timeformat'] = $params['timeformat'];
154              if($this->hlp->savePersonalSettings($settings, $user))
155              {
156                  $data['result'] = true;
157                  $data['html'] = $this->getLang('settings_saved');
158              }
159              else
160              {
161                  $data['result'] = false;
162                  $data['html'] = $this->getLang('error_saving');
163              }
164          break;
165      }
166
167      // If we are still here, JSON output is requested
168
169      //json library of DokuWiki
170      require_once DOKU_INC . 'inc/JSON.php';
171      $json = new JSON();
172
173      //set content type
174      header('Content-Type: application/json');
175      echo $json->encode($data);
176    }
177
178}
179