Advertisement
Guest User

api.php /apis/

a guest
Jan 31st, 2015
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. <?php
  2.  
  3. /* Require SSL */
  4. if (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == 'off')
  5. {
  6. header('HTTP/1.1 403 Forbidden', true, 403);
  7. print 'SSL is required to use this API';
  8. exit();
  9. }
  10.  
  11. /* Read the type of request */
  12. $apiName = (isset($_GET['apiName'])) ? $_GET['apiName'] : NULL;
  13. $apiVersion = (isset($_GET['apiVersion'])) ? $_GET['apiVersion'] : NULL;
  14. $requestType = (isset($_GET['requestType'])) ? $_GET['requestType'] : NULL;
  15. if ( !isset($apiName) || !preg_match('/^[a-zA-Z0-9-]+$/', $apiName)
  16. || !isset($apiVersion) || !preg_match('/^\d+$/', $apiVersion)
  17. || !isset($requestType) || !preg_match('/^[a-zA-Z0-9-]+$/', $requestType) )
  18. {
  19. header('HTTP/1.1 400 Bad Request', true, 400);
  20. print 'All requests must include the apiVersion and a requestType value in the GET parameters';
  21. exit();
  22. }
  23.  
  24. /* Find the script file for the request type */
  25. $requestScript = $apiName.'api_v'.$apiVersion.'/'.$requestType.'.php';
  26. if ( !file_exists($requestScript) )
  27. {
  28. header('HTTP/1.1 404 Not Found', true, 404);
  29. print 'The specified API request type "'.$requestType.'" does not exist';
  30. exit();
  31. }
  32.  
  33. /*! Setup API directories */
  34. define('COMMON_DIR', realpath('./common').'/');
  35. define('CACHE_DIR', realpath('../cache').'/');
  36. define('CONFIG_DIR', realpath('../config').'/');
  37. define('PACKAGES_DIR', realpath('../packages').'/');
  38.  
  39. if ( COMMON_DIR == '/' || CACHE_DIR == '/' || CONFIG_DIR == '/' || PACKAGES_DIR == '/' )
  40. {
  41. header('HTTP/1.1 500 Internal Server Error', true, 500);
  42. print 'Directories are not correctly configured';
  43. exit();
  44. }
  45.  
  46. /* Include common script files */
  47. define('API_INITIALISED', 1);
  48. require_once ( COMMON_DIR.'functions.php' );
  49.  
  50. /* Read any request data and execute the request */
  51. $requestData = @json_decode((isset($_POST['data']) ? $_POST['data'] : '{}'), true);
  52. require $requestScript;
  53.  
  54. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement