Skip to content

Instantly share code, notes, and snippets.

@simonsickle-old
Last active August 29, 2015 14:00
Show Gist options
  • Save simonsickle-old/11333945 to your computer and use it in GitHub Desktop.
Save simonsickle-old/11333945 to your computer and use it in GitHub Desktop.
Carbon Roms PHP api
<?php
$_config = require_once("config.php");
$baseDir = $_config['base_file_dir'];
$hiddenFiles = $_config['hidden_files'];
$dir = $_GET['dir'];
$sort = $_GET['sort'];
$pretty = $_GET['pretty'];
if(substr($dir, 0, 1) == '/' || substr($dir, 0, 3) == '../' || preg_match('/\..\|\.\|\.|resources/',$dir) || strpos($dir,'../') !== false){
$error = array("STATUS" => "Error", "CODE" => "200", "DETAILS" => "Attempting to view out of bounds of directory -ENOPERM");
print_r(json_encode($error));
exit(0);
}
//Get the directory to view. If no directory is
//selected by the GET variable dir, then view the
//base directory only.
if(substr($_GET['dir'], -1) !== '/') {
$dir = $_GET['dir'].= (substr($_GET['dir'], -1) == '/' ? '' : '/');
} else {
$dir = $_GET['dir'];
}
// Set the content type to JSON
header('Content-Type: application/json');
// Set directory variables
$directory = $baseDir.$dir;
$secureDir = "";
$dlDir = preg_replace("#".$baseDir."#", "", $secureDir, 1);
if ( ! is_dir($directory)) {
exit('Invalid diretory path');
}
$objects = array();
foreach (scandir($directory) as $file) {
if (in_array($file, $hiddenFiles)) continue;
if (pathinfo($file, PATHINFO_EXTENSION) == "md5") continue;
if(is_dir($directory.$file)) {
$objects['directories'][] = array('name' => $file);
} else {
if (file_exists($directory.$file.".md5")) {
$md5 = file_get_contents($directory.$file.".md5");
$md5 = substr($md5, 0, strrpos($md5, ' '));
} else {
$md5 = "Not Available";
}
$objects['files'][] = array(
'name' => $file,
'modified' => filemtime($directory.$file),
'md5' => $md5
);
}
}
if ($sort == "reverse") {
$newObjs = array();
if ( $objects['files'] != null )
$revFiles = array_reverse($objects['files']);
if ( $objects['directories'] != null )
$newObjs['directories'] = $objects['directories'];
if ( $objects['files'] != null )
$newObjs['files'] = $revFiles;
if ($pretty == "y")
echo(json_encode($newObjs, JSON_PRETTY_PRINT));
else
echo(json_encode($newObjs));
} else {
if ($pretty == "y")
echo(json_encode($objects, JSON_PRETTY_PRINT));
else
echo(json_encode($objects));
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment