Introduce new AJAX framework with improved class use
[infodrom.org/service.infodrom.org] / src / ajax.php
index c8926f4..c613aab 100644 (file)
@@ -2,19 +2,40 @@
 require_once('config.php');
 require_once('future.php');
 
-$backend = new AJAXBackend();
+function route_request()
+{
+  if (empty($_SERVER['REMOTE_USER']))
+    return ajax_error('Die Sitzung ist abgelaufen, Sie werden zur Startseite weitergeleiet.', true);
 
-$data = array();
+  list($class, $function) = explode('/', $_POST['route'], 2);
+  $method = 'ajax'.$function;
 
-if (strlen($_POST['func'])) {
-  if (method_exists($backend, $_POST['func'])) {
-    $func = $_POST['func'];
-    $data = $backend->$func();
-  } else {
-    $data = array('error' => 'Unbekannte Funktion '.$_POST['func'].'.');
+  if (!class_exists($class))
+    return ajax_error('Klasse '.htmlspecialchars($class).' existiert nicht.');
+
+  try {
+    if (array_key_exists('id', $_POST))
+      $object = Factory::get($class, $_POST['id']);
+    else
+      $object = Factory::get($class);
+  } catch (Exception $e) {
+    return ajax_error('Klasse '.htmlspecialchars($class).'kann nicht instanziiert werden:<br>'.htmlspecialchars($e->getMessage()));
   }
+
+  if (!method_exists($object, $method))
+    return ajax_error('Klasse '.htmlspecialchars($class).':<br>AJAX-Backend '.htmlspecialchars($function).' existiert nicht.');
+
+  $return = $object->$method($_POST);
+  if (is_bool($return)) $return = array('status' => $return);
+  return $return;
 }
 
-json_return($data);
+if (strlen($_POST['route'])) {
+  $data = route_request();
+  if (!array_key_exists('status', $data))
+    $data['status'] = true;
+} else {
+  $data = array('status' => false, 'error' => 'Keine Route angegeben');
+}
 
-?>
+json_return($data);