Skip to content

Commit

Permalink
prepping for lucky charm release
Browse files Browse the repository at this point in the history
  • Loading branch information
mychidarko committed Aug 15, 2020
1 parent a8f97fa commit db24108
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 78 deletions.
6 changes: 3 additions & 3 deletions src/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ public function returnErrors() {
/**
* Upload a file
*
* @param string $path The path to save the file in
* @param string $file The file to upload
* @param string $path The path to save the file in
* @param array $config Configuration options for file upload
*
* @return string|bool
*/
public function file_upload($path, $file, $config = []) {
return \Leaf\Fs::upload_file($path, $file, $config);
public function file_upload($file, $path, $config = []) {
return \Leaf\Fs::upload_file($file, $path, $config);
}
}
17 changes: 1 addition & 16 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
*/
namespace Leaf;

// Ensure mcrypt constants are defined even if mcrypt extension is not loaded
if (!defined('MCRYPT_MODE_CBC')) define('MCRYPT_MODE_CBC', 0);
if (!defined('MCRYPT_RIJNDAEL_256')) define('MCRYPT_RIJNDAEL_256', 0);

/**
* Leaf Core package
* @package Leaf
Expand Down Expand Up @@ -63,7 +59,7 @@ class App
'leaf.after.router' => array(array()),
'leaf.after' => array(array())
);

/********************************************************************************
* Instantiation and Configuration
*******************************************************************************/
Expand Down Expand Up @@ -245,17 +241,6 @@ public static function getDefaultSettings()
'log.writer' => null,
'log.level' => \Leaf\Log::DEBUG,
'log.enabled' => true,
// Cookies
'cookies.encrypt' => false,
'cookies.lifetime' => '20 minutes',
'cookies.path' => '/',
'cookies.domain' => null,
'cookies.secure' => false,
'cookies.httponly' => false,
// Encryption
'cookies.secret_key' => 'CHANGE_ME',
'cookies.cipher' => MCRYPT_RIJNDAEL_256,
'cookies.cipher_mode' => MCRYPT_MODE_CBC,
// HTTP
'http.version' => '1.1'
);
Expand Down
7 changes: 4 additions & 3 deletions src/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,14 @@ public function render(string $templateName, array $data = [], array $merge_data
/**
* Upload a file
*
* @param string $path The path to save the file in
* @param string $file The file to upload
* @param string $path The path to save the file in
* @param array $config Configuration options for file upload
*
* @return string|bool
*/
public function file_upload($path, $file, $config = []) {
return \Leaf\Fs::upload_file($path, $file, $config);
public function file_upload($file, $path, $config = [])
{
return \Leaf\Fs::upload_file($file, $path, $config);
}
}
2 changes: 1 addition & 1 deletion src/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ public function execute() {
if (count($uniques) > 0 && ($this->queryData["type"] != "select" || $this->queryData["type"] != "delete")) {
foreach ($uniques as $unique) {
if (!isset($paramValues[$unique])) {
$this->response->respond(["error" => "$unique not found, Add $unique to your \$db->add items or check your spelling."]);
$this->response->throwErr(["error" => "$unique not found, Add $unique to your \$db->add items or check your spelling."]);
exit();
} else {
if (mysqli_fetch_object($this->connection->query("SELECT * FROM {$this->queryData["table"]} WHERE $unique = '$paramValues[$unique]'"))) {
Expand Down
6 changes: 3 additions & 3 deletions src/FS.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,14 @@ public static function create_file($filename) {
/**
* Upload a file
*
* @param string $path The path to save the file in
* @param string $file The file to upload
* @param string $path The path to save the file in
* @param string $file_category The type of file
* @param array $config Configuration options for file upload
*
* @return string|bool
*/
public static function upload_file($path, $file, $config = [])
public static function upload_file($file, $path, $config = [])
{
if (!is_dir($path)) {
if (isset($config["verify_dir"]) && $config["verify_dir"] == true) {
Expand Down Expand Up @@ -216,7 +216,7 @@ public static function upload_file($path, $file, $config = [])
return false;
}

if (isset($config["validate"])) {
if (isset($config["validate"]) && $config["validate"] == true) {
foreach (self::$extensions as $category => $exts) {
if ($file_category == $category) $extensions = $exts;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Form extends Request {

public function __construct()
{
$this->response = new \Leaf\Http\Response();
$this->response = new \Leaf\Http\Response;
}

public $errorsArray = array();
Expand Down Expand Up @@ -84,7 +84,7 @@ public function validate(array $rules, array $messages = []) {

if (!in_array($rule, $supportedRules)) {
echo $rule." is not a supported rule<br>";
echo "Supported rules are ".json_encode($supportedRules);
echo "Supported rules are ". json_encode($supportedRules);
exit();
}
return $this->validateField($field["name"], $field["value"], $rule);
Expand All @@ -94,7 +94,7 @@ public function validate(array $rules, array $messages = []) {

if (!in_array($field["rule"], $supportedRules)) {
echo $field["rule"]." is not a supported rule<br>";
echo "Supported rules are ".json_encode($supportedRules);
echo "Supported rules are ". json_encode($supportedRules);
exit();
}
return $this->validateField($field["name"], $field["value"], $field["rule"]);
Expand Down
32 changes: 15 additions & 17 deletions src/Http/Headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,20 @@ class Headers
protected static $http_code;

/**
* Set an HTTP code for response
* Get or Set an HTTP code for response
*
* @param int|null $http_code The current response code.
*/
public static function status($http_code)
public static function status($http_code = null)
{
if (!$http_code) return self::$http_code;
self::$http_code = $http_code;
}

/**
* Force an HTTP code for response using PHP's `http_response_code`
*/
public static function resStatus($http_code = null)
public static function resetStatus($http_code = null)
{
return http_response_code($http_code);
}
Expand All @@ -34,15 +37,10 @@ public static function resStatus($http_code = null)
*
* @param bool $safeOutput Try to sanitize header data
*/
public static function all($safeOutput = true) : array
public static function all($safeOutput = false) : array
{
if ($safeOutput === false) return self::findHeaders();

$headers = [];
foreach (self::findHeaders() as $key => $value) {
$headers[$key] = $value;
}
return \Leaf\Util::sanitize($headers);
return \Leaf\Util::sanitize(self::findHeaders());
}

/**
Expand All @@ -53,7 +51,7 @@ public static function all($safeOutput = true) : array
*
* @return string|array
*/
public static function get($params, $safeOutput = true)
public static function get($params, $safeOutput = false)
{
if (is_string($params)) return self::all($safeOutput)[$params] ?? null;

Expand Down Expand Up @@ -91,23 +89,23 @@ public static function remove($keys)

public static function contentPlain($code = null) : void
{
self::set("Content-Type", "text/plain", true, $code);
self::set("Content-Type", "text/plain", true, $code ?? self::$http_code);
}

public static function contentHtml($code = null): void
{
self::set("Content-Type", "text/html", true, $code);
self::set("Content-Type", "text/html", true, $code ?? self::$http_code);
}

public static function contentJSON($code = null) : void
{
self::set("Content-Type", "application/json", true, $code);
self::set("Content-Type", "application/json", true, $code ?? self::$http_code);
}

public static function accessControl($key, $value = "", $code = null)
{
if (is_string($key)) {
self::set("Access-Control-$key", $value, true, $code);
self::set("Access-Control-$key", $value, true, $code ?? self::$http_code);
} else {
foreach ($key as $header => $header_value) {
self::accessControl($header, $header_value, $code);
Expand All @@ -116,9 +114,9 @@ public static function accessControl($key, $value = "", $code = null)
}

protected static function findHeaders() {
return getallheaders();
if (getallheaders()) return getallheaders();

// Method getallheaders() not available or went wrong: manually extract 'm
$headers = [];
foreach ($_SERVER as $name => $value) {
if ((substr($name, 0, 5) == 'HTTP_') || ($name == 'CONTENT_TYPE') || ($name == 'CONTENT_LENGTH')) {
$headers[str_replace([' ', 'Http'], ['-', 'HTTP'], ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
Expand Down
21 changes: 9 additions & 12 deletions src/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,7 @@ public function typeIs(string $type)
*/
public function hasHeader(String $header)
{
if (isset($this->headers[$header])) {
return true;
}

if ($this->headers->get($header)) return true;
return false;
}

Expand All @@ -99,11 +96,8 @@ public function hasHeader(String $header)
*/
public function isAjax()
{
if ($this->params('isajax')) {
return true;
} elseif (isset($this->headers['X_REQUESTED_WITH']) && $this->headers['X_REQUESTED_WITH'] === 'XMLHttpRequest') {
return true;
}
if ($this->params('isajax')) return true;
if ($this->headers->get('X_REQUESTED_WITH') && $this->headers->get('X_REQUESTED_WITH') === 'XMLHttpRequest') return true;

return false;
}
Expand Down Expand Up @@ -165,15 +159,18 @@ public function get($params, bool $safeData = true)
public function body(bool $safeData = true)
{
$req = is_array(json_decode($this->request, true)) ? json_decode($this->request, true) : [];
return $safeData ? \Leaf\Util::sanitize(array_merge($_GET, $_POST, $req, $_FILES)) : array_merge($_GET, $_POST, $req, $_FILES);
return $safeData ? \Leaf\Util::sanitize(array_merge($_GET, $_FILES, $_POST, $req)) : array_merge($_GET, $_FILES, $_POST, $req);
}

/**
* Get all files passed into the request.
*
* @param string $filename The file(s) you want to get
* @param string|array $filenames The file(s) you want to get
*/
public function files(string ...$filenames) {
public function files($filenames = null) {
if ($filenames == null) return $_FILES;
if (is_string($filenames)) return $_FILES[$filenames] ?? null;

$files = [];
foreach ($filenames as $filename) {
$files[$filename] = $_FILES[$filename] ?? null;
Expand Down
65 changes: 54 additions & 11 deletions src/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,60 +89,95 @@ class Response

public function __construct()
{
$this->headers = new \Leaf\Http\Headers;
$this->headers = new Headers;
Headers::contentHtml();
}

/**
* Output neatly parsed json
* [**DEPRECATION WARNING: USE `json()` INSTEAD**]
*/
public function respond($data)
{
Headers::contentJSON(200);
echo json_encode($data);
$this->json($data);
}

/**
* Output json encoded data with an HTTP code/message
* [**DEPRECATION WARNING: USE `json()` INSTEAD**]
*/
public function respondWithCode($data, int $code = 200, bool $use_message = false)
public function respondWithCode($data, int $code = 200, bool $useMessage = false)
{
if ($use_message) {
$dataToPrint = ["data" => $data, "message" => isset(self::$messages[$code]) ? self::$messages[$code] : $code];
} else {
$this->json($data, $code, true, $useMessage);
}

/**
* Output json encoded data with an HTTP code/message
*
* @param mixed $data The data to output
* @param int $code The response status code
* @param bool $showCode Show response code in body?
* @param bool $useMessage Show message instead of code
*/
public function json($data, int $code = 200, bool $showCode = false, bool $useMessage = false)
{
if ($showCode) {
$dataToPrint = ["data" => $data, "code" => $code];

if ($useMessage) {
$dataToPrint = ["data" => $data, "message" => isset(self::$messages[$code]) ? self::$messages[$code] : $code];
}
} else {
$dataToPrint = $data;
}

Headers::contentJSON($code);
echo json_encode($dataToPrint);
}

/**
* Throw an error and break the application
*/
public function throwErr($error, int $code = 500, bool $use_message = false)
public function throwErr($error, int $code = 500, bool $useMessage = false)
{
$dataToPrint = ["error" => $error, "code" => $code];
if ($use_message) $dataToPrint = ["error" => $error, "message" => isset(self::$messages[$code]) ? self::$messages[$code] : $code];
if ($useMessage) $dataToPrint = ["error" => $error, "message" => isset(self::$messages[$code]) ? self::$messages[$code] : $code];

Headers::contentJSON($code);
echo json_encode($dataToPrint);
exit();
}

public function renderPage(String $file, int $code = null)
public function page(string $file, int $code = null)
{
Headers::contentHtml($code);
require $file;
}

public function renderMarkup(String $markup, int $code = null)
/**
* [DEPRECATION WARNING: USE `page` INSTEAD]
*/
public function renderPage(String $file, int $code = null)
{
$this->page($file, $code);
}

public function markup(String $markup, int $code = null)
{
Headers::contentHtml($code);
echo <<<EOT
$markup
EOT;
}

/**
* [DEPRECATION WARNING: USE `markup` INSTEAD]
*/
public function renderMarkup(string $markup, int $code = null)
{
$this->markup($markup, $code);
}

public function cors(String $allow_origin = "*", String $allow_headers = "*")
{
Headers::accessControl(["Allow-Origin" => $allow_origin, "Allow-Headers" => $allow_headers]);
Expand All @@ -160,6 +195,14 @@ public function header($name, $value = null)
return Headers::get($name);
}

/**
* Set HTTP status code
*/
public function status($code = null)
{
return Headers::status($code);
}

/**
* Set cookie
*
Expand Down
Loading

0 comments on commit db24108

Please sign in to comment.