Skip to content

Commit

Permalink
Added basic API implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Marc Gehring committed May 21, 2014
1 parent 2fe2162 commit d52ddf4
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions interface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/**
* crunchDB - A simple file based database system
* written in pure PHP. Useful for smaller applications.
*
* Written and copyrighted 2014+
* by Sven Marc 'cybrox' Gehring
*
* Licensed under CC BY-NC-SA
* Named after delicious cookies!
*/

require_once('./src/crunchdb.class.php');
require_once('./src/crunchroot.class.php');
require_once('./src/crunchtable.class.php');

$reqMethd = (!empty($_GET['method'])) ? $_GET['method'] : '';
$reqTable = (!empty($_GET['table'])) ? $_GET['table'] : '';
$reqArgms = array($reqTable);

$postArgs = $_POST;
ksort($postArgs);
foreach($postArgs as $a) array_push($reqArgms, $a);

$cdb = new crunchDB();
$smooth = true;

try { $res = call_user_func_array(array($cdb, $reqMethd), $reqArgms); }
catch (Exception $e) {
handleResponse(false, 'Caught exception: '.$e->getMessage());
$smooth = false;
}

if($smooth){
if(gettype($res) == 'boolean') handleResponse($res, 'performed action');
else handleResponse(true, $res);
}


function handleResponse($success, $message){
$data = ($success) ? $message : '';
$erro = ($success) ? '' : $message;
echo json_encode(array(
'success' => $success,
'error' => $erro,
'data' => $data
));
}

?>

0 comments on commit d52ddf4

Please sign in to comment.