Prevent warning
[infodrom.org/service.infodrom.org] / src / ajax.php
1 <?php
2 require_once('config.php');
3 require_once('future.php');
4
5 function route_request()
6 {
7   if (empty($_SERVER['REMOTE_USER']))
8     return ajax_error('Die Sitzung ist abgelaufen, Sie werden zur Startseite weitergeleiet.', true);
9
10   list($class, $function) = explode('/', $_POST['route'], 2);
11   $method = 'ajax'.$function;
12
13   if (!class_exists($class))
14     return ajax_error('Klasse '.htmlspecialchars($class).' existiert nicht.');
15
16   try {
17     if (array_key_exists('id', $_POST))
18       $object = Factory::get($class, $_POST['id']);
19     else
20       $object = Factory::get($class);
21   } catch (Exception $e) {
22     return ajax_error('Klasse '.htmlspecialchars($class).'kann nicht instanziiert werden:<br>'.htmlspecialchars($e->getMessage()));
23   }
24
25   if (!method_exists($object, $method))
26     return ajax_error('Klasse '.htmlspecialchars($class).':<br>AJAX-Backend '.htmlspecialchars($function).' existiert nicht.');
27
28   $return = $object->$method($_POST);
29   if (is_bool($return)) $return = array('status' => $return);
30   return $return;
31 }
32
33 if (strlen($_POST['route'])) {
34   $data = route_request();
35   if (!array_key_exists('status', $data))
36     $data['status'] = true;
37 } else {
38   $data = array('status' => false, 'error' => 'Keine Route angegeben');
39 }
40
41 json_return($data);