File indexing completed on 2024-12-15 03:45:05

0001 <?php
0002 /*
0003     SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org>
0004 
0005     SPDX-License-Identifier: MIT
0006 */
0007 
0008 require_once('restexception.php');
0009 require_once('utils.php');
0010 
0011 /** Turns REST requests into method calls. */
0012 class RESTDispatcher
0013 {
0014 
0015 public static function dispatch($handler)
0016 {
0017     $prefix = dirname($_SERVER['PHP_SELF']);
0018     $command = explode('/', substr($_SERVER['REQUEST_URI'], strlen($prefix) + 1), 3);
0019 
0020     try {
0021         if (sizeof($command) < 1)
0022             Utils::httpError(400, 'Empty REST command.');
0023 
0024         $method = strtolower($_SERVER['REQUEST_METHOD']) . '_' . $command[0];
0025 
0026         if (!method_exists($handler, $method))
0027             Utils::httpError(400, 'Invalid REST command ' . $method . '.');
0028 
0029         for ($i = 1; $i < count($command); $i++) {
0030             if (is_string($command[$i]))
0031                 $command[$i] = urldecode($command[$i]);
0032         }
0033 
0034         switch(sizeof($command)) {
0035             case 1:
0036                 $handler->$method();
0037                 break;
0038             case 2:
0039                 $handler->$method($command[1]);
0040                 break;
0041             case 3:
0042                 $handler->$method($command[1], $command[2]);
0043                 break;
0044         }
0045     } catch (RESTException $e) {
0046         Utils::httpError($e->getCode(), $e->getMessage());
0047     }
0048 }
0049 
0050 }
0051 
0052 ?>