Last active
December 16, 2015 20:50
-
-
Save themarcusbattle/5495674 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Options FollowSymLinks | |
RewriteEngine On | |
RewriteCond %{REQUEST_URI} (\.json)$ | |
RewriteRule ^(.*)$ index.php?rt=$1 [L,QSA] | |
RewriteCond %{REQUEST_URI} !(\.gif)|(\.jpg)|(\.png)|(\.css)|(\.js)|(\.php)$ | |
RewriteCond %{REQUEST_FILENAME} !-f | |
RewriteCond %{REQUEST_FILENAME} !-d | |
RewriteRule ^(.*)$ index.php?rt=$1 [L,QSA] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$host = $_SERVER['HTTP_HOST']; | |
$rawURI = $_SERVER['REQUEST_URI']; | |
$requestType = $_SERVER['REQUEST_METHOD']; | |
$apiRoot = '/mb/api/'; | |
/** DEFINE DB **/ | |
$db = new PDO("mysql:host=localhost;dbname=api",'chozinwun',''); | |
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); | |
/** END DEFINE DB **/ | |
/** BEGIN SANITZATION **/ | |
// Prepares our data / params | |
$rawParams = strstr($rawURI,'?'); | |
$rawURI = str_replace($rawParams,'',$rawURI); | |
$rawParams = str_replace('?','',$rawParams); | |
$params = array(); | |
if ($rawParams) { | |
$rawParams = explode('&',$rawParams); | |
foreach($rawParams as $p) { | |
$param = explode('=',$p); | |
$params[$param[0]] = $param[1]; | |
} | |
} | |
// Build uri params | |
$uri = str_replace($apiRoot,'',$rawURI); | |
$uri = explode('/',$uri); | |
$lastIndex = count($uri) - 1; | |
$dataType = str_replace('.','',strstr($uri[$lastIndex],'.')); | |
$uri[$lastIndex] = str_replace('.' . $dataType,'',$uri[$lastIndex]); | |
if(!$dataType) $dataType = 'json'; | |
/** END SANITZATION **/ | |
/** PROCESS REQUEST **/ | |
if (tableExists($uri[0])) { | |
if ($requestType == 'GET') { querySelect($uri[0]); } | |
else if ($requestType == 'POST') {} | |
else if ($requestType == 'PUT') {} | |
else if ($requestType == 'DELETE') {} | |
else { | |
echo "We don't support this type of request"; | |
} | |
} else { | |
echo "object does not exist"; | |
} | |
/** END PROCESS REQUEST **/ | |
function tableExists($table) { | |
global $db; | |
$result = $db->query("SHOW TABLES LIKE '$table'"); | |
return $result->fetch(); | |
} | |
function querySelect($table) { | |
global $uri, $params, $db; | |
$select = ''; | |
// Get all columns in table | |
$qry = $db->query("SHOW FULL COLUMNS FROM $table"); | |
$cols = $qry->fetchAll(); | |
// Define which results can be returned | |
foreach ($cols as $col) { | |
if ($col['Comment'] != 'private') $select .= "," . $col['Field']; | |
} | |
$select = substr($select,1); | |
$sql = "SELECT $select FROM $table"; | |
foreach ($cols as $col) { | |
if (array_key_exists($col['Field'],$params)) { | |
if (strstr($sql,'WHERE')) { | |
$sql .= " AND " . $col['Field'] . " LIKE '%" . $params[$col['Field']] . "%'"; | |
} else { | |
$sql .= " WHERE " . $col['Field'] . " LIKE '%" . $params[$col['Field']] . "%'"; | |
} | |
} | |
} | |
if (isset($params['limit']) && isset($params['start'])) { | |
$sql .= ' LIMIT ' . $params['start'] . ',' . $params['limit']; | |
} else if (isset($params['limit'])) { | |
$sql .= ' LIMIT ' . $params['limit']; | |
} | |
$result = $db->query($sql); | |
printData($result->fetchAll()); | |
} | |
function printData($data) { | |
global $dataType; | |
if ($dataType == 'json') { echo json_encode($data); } | |
else echo "data type not supported. Please make this request for json"; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
API!!!