xref: /plugin/davcal/action/ajax.php (revision d71c99346f37a0cdcbce20489c6915b99714a98d)
1a1a3b679SAndreas Boehler<?php
2a1a3b679SAndreas Boehler
3cb71a62aSAndreas Boehler/**
4cb71a62aSAndreas Boehler * DokuWiki DAVCal PlugIn - Ajax component
5cb71a62aSAndreas Boehler */
6cb71a62aSAndreas Boehler
7a1a3b679SAndreas Boehlerif(!defined('DOKU_INC')) die();
8a1a3b679SAndreas Boehler
9a1a3b679SAndreas Boehlerclass action_plugin_davcal_ajax extends DokuWiki_Action_Plugin {
10a1a3b679SAndreas Boehler
11a1a3b679SAndreas Boehler    /**
12cb71a62aSAndreas Boehler     * @var helper_plugin_davcal
13a1a3b679SAndreas Boehler     */
14a1a3b679SAndreas Boehler    private $hlp = null;
15a1a3b679SAndreas Boehler
16a1a3b679SAndreas Boehler    function __construct() {
17a1a3b679SAndreas Boehler        $this->hlp =& plugin_load('helper','davcal');
18a1a3b679SAndreas Boehler    }
19a1a3b679SAndreas Boehler
20a1a3b679SAndreas Boehler    function register(Doku_Event_Handler $controller) {
21a1a3b679SAndreas Boehler        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax_call_unknown');
22a1a3b679SAndreas Boehler    }
23a1a3b679SAndreas Boehler
24a1a3b679SAndreas Boehler    function handle_ajax_call_unknown(&$event, $param) {
25a1a3b679SAndreas Boehler      if($event->data != 'plugin_davcal') return;
26a1a3b679SAndreas Boehler
27a1a3b679SAndreas Boehler      $event->preventDefault();
28a1a3b679SAndreas Boehler      $event->stopPropagation();
29a1a3b679SAndreas Boehler      global $INPUT;
30a1a3b679SAndreas Boehler
31a1a3b679SAndreas Boehler      $action = trim($INPUT->post->str('action'));
32a1a3b679SAndreas Boehler      $id = trim($INPUT->post->str('id'));
33f3942fbbSAndreas Boehler      $page = trim($INPUT->post->str('page'));
34a1a3b679SAndreas Boehler      $params = $INPUT->post->arr('params');
3534a47953SAndreas Boehler      if(isset($_SERVER['REMOTE_USER']) && !is_null($_SERVER['REMOTE_USER']))
36a1a3b679SAndreas Boehler        $user = $_SERVER['REMOTE_USER'];
3734a47953SAndreas Boehler      else
3834a47953SAndreas Boehler        $user = null;
39a1a3b679SAndreas Boehler      $write = false;
40a1a3b679SAndreas Boehler
41daa99e9eSAndreas Boehler      if(!checkSecurityToken())
42daa99e9eSAndreas Boehler      {
43daa99e9eSAndreas Boehler          echo "CSRF Attack.";
44daa99e9eSAndreas Boehler          return;
45daa99e9eSAndreas Boehler      }
46daa99e9eSAndreas Boehler
47a1a3b679SAndreas Boehler      $data = array();
48a1a3b679SAndreas Boehler
49a1a3b679SAndreas Boehler      $data['result'] = false;
50a1a3b679SAndreas Boehler      $data['html'] = $this->getLang('unknown_error');
51a1a3b679SAndreas Boehler
52cb71a62aSAndreas Boehler      // Check if we have access to the calendar ($id is given by parameters,
53cb71a62aSAndreas Boehler      // that's not necessarily the page we come from)
54*d71c9934SAndreas Boehler
55*d71c9934SAndreas Boehler      $acl = $this->hlp->checkCalendarPermission($id);
56a1a3b679SAndreas Boehler      if($acl > AUTH_READ)
57a1a3b679SAndreas Boehler      {
58a1a3b679SAndreas Boehler          $write = true;
59a1a3b679SAndreas Boehler      }
60ed764890SAndreas Boehler      elseif($acl < AUTH_READ)
61ed764890SAndreas Boehler      {
62ed764890SAndreas Boehler          $data['result'] = false;
63ed764890SAndreas Boehler          $data['html'] = $this->getLang('no_permission');
64ed764890SAndreas Boehler          // Set to an invalid action in order to just return the result
65ed764890SAndreas Boehler          $action = 'invalid';
66ed764890SAndreas Boehler      }
67a1a3b679SAndreas Boehler
68185e2535SAndreas Boehler      // Retrieve the calendar pages based on the meta data
69f3942fbbSAndreas Boehler      $calendarPages = $this->hlp->getCalendarPagesByMeta($page);
70185e2535SAndreas Boehler      if($calendarPages === false)
71185e2535SAndreas Boehler      {
724a2bf5eeSAndreas Boehler          $calendarPages = array($page => null);
73185e2535SAndreas Boehler      }
74185e2535SAndreas Boehler
75cb71a62aSAndreas Boehler      // Parse the requested action
76a1a3b679SAndreas Boehler      switch($action)
77a1a3b679SAndreas Boehler      {
78cb71a62aSAndreas Boehler          // Add a new Event
79a1a3b679SAndreas Boehler          case 'newEvent':
80a1a3b679SAndreas Boehler              if($write)
81a1a3b679SAndreas Boehler              {
82809cb0faSAndreas Boehler                  $res = $this->hlp->addCalendarEntryToCalendarForPage($id, $user, $params);
83809cb0faSAndreas Boehler                  if($res === true)
84809cb0faSAndreas Boehler                  {
85a1a3b679SAndreas Boehler                    $data['result'] = true;
86a1a3b679SAndreas Boehler                    $data['html'] = $this->getLang('event_added');
87809cb0faSAndreas Boehler                  }
88809cb0faSAndreas Boehler                  else
89809cb0faSAndreas Boehler                  {
90809cb0faSAndreas Boehler                    $data['result'] = false;
91809cb0faSAndreas Boehler                    $data['html'] = $this->getLang('unknown_error');
92809cb0faSAndreas Boehler                  }
93a1a3b679SAndreas Boehler              }
94a1a3b679SAndreas Boehler              else
95a1a3b679SAndreas Boehler              {
96a1a3b679SAndreas Boehler                  $data['result'] = false;
97a1a3b679SAndreas Boehler                  $data['html'] = $this->getLang('no_permission');
98a1a3b679SAndreas Boehler              }
99a1a3b679SAndreas Boehler          break;
100cb71a62aSAndreas Boehler          // Retrieve existing Events
101a1a3b679SAndreas Boehler          case 'getEvents':
102a1a3b679SAndreas Boehler              $startDate = $INPUT->post->str('start');
103a1a3b679SAndreas Boehler              $endDate = $INPUT->post->str('end');
10482a48dfbSAndreas Boehler              $timezone = $INPUT->post->str('timezone');
105185e2535SAndreas Boehler              $data = array();
1064a2bf5eeSAndreas Boehler              foreach($calendarPages as $calPage => $color)
107185e2535SAndreas Boehler              {
108f3942fbbSAndreas Boehler                  $data = array_merge($data, $this->hlp->getEventsWithinDateRange($calPage,
1094a2bf5eeSAndreas Boehler                                      $user, $startDate, $endDate, $timezone, $color));
110185e2535SAndreas Boehler              }
111a1a3b679SAndreas Boehler          break;
112cb71a62aSAndreas Boehler          // Edit an event
113a1a3b679SAndreas Boehler          case 'editEvent':
114a1a3b679SAndreas Boehler              if($write)
115a1a3b679SAndreas Boehler              {
116809cb0faSAndreas Boehler                  $res = $this->hlp->editCalendarEntryForPage($id, $user, $params);
117809cb0faSAndreas Boehler                  if($res === true)
118809cb0faSAndreas Boehler                  {
119a1a3b679SAndreas Boehler                    $data['result'] = true;
120a1a3b679SAndreas Boehler                    $data['html'] = $this->getLang('event_edited');
121809cb0faSAndreas Boehler                  }
122809cb0faSAndreas Boehler                  else
123809cb0faSAndreas Boehler                  {
124809cb0faSAndreas Boehler                    $data['result'] = false;
125809cb0faSAndreas Boehler                    $data['html'] = $this->getLang('unknown_error');
126809cb0faSAndreas Boehler                  }
127a1a3b679SAndreas Boehler              }
128a1a3b679SAndreas Boehler              else
129a1a3b679SAndreas Boehler              {
130a1a3b679SAndreas Boehler                  $data['result'] = false;
131a1a3b679SAndreas Boehler                  $data['html'] = $this->getLang('no_permission');
132a1a3b679SAndreas Boehler              }
133a1a3b679SAndreas Boehler          break;
134cb71a62aSAndreas Boehler          // Delete an Event
135a1a3b679SAndreas Boehler          case 'deleteEvent':
136a1a3b679SAndreas Boehler              if($write)
137a1a3b679SAndreas Boehler              {
138809cb0faSAndreas Boehler                  $res = $this->hlp->deleteCalendarEntryForPage($id, $params);
139809cb0faSAndreas Boehler                  if($res === true)
140809cb0faSAndreas Boehler                  {
141a1a3b679SAndreas Boehler                    $data['result'] = true;
142a1a3b679SAndreas Boehler                    $data['html'] = $this->getLang('event_deleted');
143809cb0faSAndreas Boehler                  }
144809cb0faSAndreas Boehler                  else
145809cb0faSAndreas Boehler                  {
146809cb0faSAndreas Boehler                    $data['result'] = false;
147809cb0faSAndreas Boehler                    $data['html'] = $this->getLang('unknown_error');
148809cb0faSAndreas Boehler                  }
149a1a3b679SAndreas Boehler              }
150a1a3b679SAndreas Boehler              else
151a1a3b679SAndreas Boehler              {
152a1a3b679SAndreas Boehler                  $data['result'] = false;
153a1a3b679SAndreas Boehler                  $data['html'] = $this->getLang('no_permission');
154a1a3b679SAndreas Boehler              }
155a1a3b679SAndreas Boehler          break;
156cb71a62aSAndreas Boehler          // Get personal settings
157a495d34cSAndreas Boehler          case 'getSettings':
158a495d34cSAndreas Boehler              $data['result'] = true;
159a495d34cSAndreas Boehler              $data['settings'] = $this->hlp->getPersonalSettings($user);
160185e2535SAndreas Boehler              $data['settings']['calids'] = $this->hlp->getCalendarMapForIDs($calendarPages);
16173b331a3SAndreas Boehler              $data['settings']['readonly'] = !$write;
162f3942fbbSAndreas Boehler              $data['settings']['syncurl'] = $this->hlp->getSyncUrlForPage($page, $user);
163f3942fbbSAndreas Boehler              $data['settings']['privateurl'] = $this->hlp->getPrivateURLForPage($page);
164e86c8dd3SAndreas Boehler              $data['settings']['principalurl'] = $this->hlp->getPrincipalUrlForUser($user);
165f3942fbbSAndreas Boehler              $data['settings']['meta'] = $this->hlp->getCalendarMetaForPage($page);
166a495d34cSAndreas Boehler          break;
167cb71a62aSAndreas Boehler          // Save personal settings
168a495d34cSAndreas Boehler          case 'saveSettings':
169a495d34cSAndreas Boehler              $settings = array();
170a495d34cSAndreas Boehler              $settings['weeknumbers'] = $params['weeknumbers'];
171a495d34cSAndreas Boehler              $settings['timezone'] = $params['timezone'];
172a495d34cSAndreas Boehler              $settings['workweek'] = $params['workweek'];
173185e2535SAndreas Boehler              $settings['monday'] = $params['monday'];
1741d5bdcd0SAndreas Boehler              $settings['timeformat'] = $params['timeformat'];
175a495d34cSAndreas Boehler              if($this->hlp->savePersonalSettings($settings, $user))
176a495d34cSAndreas Boehler              {
177a495d34cSAndreas Boehler                  $data['result'] = true;
178a495d34cSAndreas Boehler                  $data['html'] = $this->getLang('settings_saved');
179a495d34cSAndreas Boehler              }
180a495d34cSAndreas Boehler              else
181a495d34cSAndreas Boehler              {
182a495d34cSAndreas Boehler                  $data['result'] = false;
183a495d34cSAndreas Boehler                  $data['html'] = $this->getLang('error_saving');
184a495d34cSAndreas Boehler              }
185a495d34cSAndreas Boehler          break;
186a1a3b679SAndreas Boehler      }
187a1a3b679SAndreas Boehler
188a1a3b679SAndreas Boehler      // If we are still here, JSON output is requested
189a1a3b679SAndreas Boehler
190a1a3b679SAndreas Boehler      //json library of DokuWiki
191a1a3b679SAndreas Boehler      require_once DOKU_INC . 'inc/JSON.php';
192a1a3b679SAndreas Boehler      $json = new JSON();
193a1a3b679SAndreas Boehler
194a1a3b679SAndreas Boehler      //set content type
195a1a3b679SAndreas Boehler      header('Content-Type: application/json');
196a1a3b679SAndreas Boehler      echo $json->encode($data);
197a1a3b679SAndreas Boehler    }
198a1a3b679SAndreas Boehler
199a1a3b679SAndreas Boehler}
200