Skip to content
Merged

Main #20

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/Core/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,35 @@ class Application {
public string $layout = 'app';
public ?Controller $controller = null;
public $config = [];
public $files = [];

public function __construct($config = [])
{
$this->LoadSettings($config);
self::$app = $this;

$this->user = null;
$this->user = NULL;
$this->guest = NULL;
$this->request = new Request();
$this->response = new Response();
$this->storage = new FileStorage();
$this->router = new Router($this->request, $this->response);
$this->database = new Database();
$this->session = new Session($this->config->auth->defaults->session);
$this->view = new View();
$this->services = new ServiceProvider();
$this->helpers = $this->ToObject([
'StringHelper' => '\InfinityBrackets\Helpers\StringHelper'
]);

$userId = Application::$app->session->GetAuth();
if ($userId) {
$this->user = $this->userClass::FindUser($userId);
} else {
$guestUserId = Application::$app->session->GetAuthGuest();
if($guestUserId) {
$this->guest = $this->guestClass::FindGuestUser($guestUserId);
}
}
}

Expand All @@ -40,6 +51,7 @@ public function __construct($config = [])
public function LoadSettings($config) {
self::$ROOT_DIR = $config['root'];
$this->userClass = $config['auth']['userClass'];
$this->guestClass = $config['auth']['guestClass'];
$this->config = $config;

// Transform config type (Array) to (Object)
Expand Down Expand Up @@ -88,4 +100,11 @@ public function ToJSON($data) {
}
echo json_encode($data);
}

public function HasPermission($name) {
$count = $this->database->CountTable("user_permissions", "WHERE `permission_id` IN (SELECT `id` FROM `permissions` WHERE `name` = :in_name) AND `user_id` = :in_user_id", ['in_name' => $name, 'in_user_id' => $this->session->GetAuth()]);

echo ($count > 0 ? '' : 'disabled');
//echo 'disabled';
}
}
14 changes: 13 additions & 1 deletion src/Core/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class Controller
{
public string $layout = 'app';
public string $action = '';

public $model = NULL;
public $models = [];
protected array $middlewares = [];

public function SetLayout($layout): void
Expand All @@ -30,4 +31,15 @@ public function GetMiddlewares(): array
{
return $this->middlewares;
}

public function RegisterModel($model) {
$this->model = new $model();
}

public function BindModel($classes = []) {
foreach($classes as $class) {
$temp = explode('\\', $class);
$this->models[end($temp)] = new $class();
}
}
}
9 changes: 6 additions & 3 deletions src/Core/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public function __construct($config = NULL)
}
}
}
if(is_array($config)) {
$default = $config;
}
if(is_null($default)) {
$default = Application::$app->config->env;
}
Expand Down Expand Up @@ -125,11 +128,11 @@ public function Rollback() {

public function Query($statement = "", $parameters = []) {
try {
$stmt = $this->pdo->query($statement);
$stmt->execute($parameters);
$stmt = $this->ExecuteStatement($statement, $parameters);
$this->results = $stmt->fetchAll();
return $this;
} catch(Exception $e) {
throw new Exception($e->getMessage());
throw new Exception($e->getMessage());
}
}

Expand Down
33 changes: 33 additions & 0 deletions src/Core/FileStorage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace InfinityBrackets\Core;

class FileStorage
{
private static $files = [];

protected function Files() {
return self::$files;
}

public function Push($file)
{
self::$files = $file;
}

public function HasFiles()
{
return count(self::$files) > 0 ? TRUE : FALSE;
}

public function Upload($storage) {
$fileName = self::$files['name'];
$tempLocation = self::$files['tmp_name'];
$fileExtension = pathinfo($fileName, PATHINFO_EXTENSION);
$name = uniqid(true) . '.' . $fileExtension;

$upload = move_uploaded_file($tempLocation, $storage . $name) ? TRUE : FALSE;

return Application::$app->ToObject(['success' => $upload, 'name' => $name]);
}
}
177 changes: 177 additions & 0 deletions src/Core/Pagination.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
<?php

namespace InfinityBrackets\Core;
/*
* This is not for sale but it's free to use for any web application
*
* @package Infinity Brackets
* @author John Vincent Bonza
* @copyright 2021 Infinity Brackets
* @license Free
* @version v3.0
*/
use InfinityBrackets\Core\Pagination;

class Pagination {
public $paginationControls = "";
public $maxControlsPerPage = 5;
public $current = 1;
public $last = 1;
public $queryString = "";

public function __construct($config = []) {
if(array_key_exists('current', $config)) {
$this->current = $config['current'];
}
if(array_key_exists('last', $config)) {
$this->last = $config['last'];
}
if(array_key_exists('queryString', $config)) {
$this->queryString .= $config['queryString'];
}
if(array_key_exists('orderBy', $config)) {
$this->queryString .= '&orderBy=' . $config['orderBy'];
}
}

public function GeneratePagination() {
$this->queryString .='&page=';
if($this->last != 1) {
if ($this->current > 1) {
// First Page
if($this->current >= $this->maxControlsPerPage - 1) {
$this->paginationControls .= '<li class="paginate_button page-item"><a class="page-link" href="' . $this->queryString . '1"><span class="ib_pagination"><i class="fas fa-angle-double-left"></i></a></li>';
}
// Before Active Page
for($i = $this->current - ($this->maxControlsPerPage - 1); $i < $this->current; $i++) {
if($i > 0) {
if($this->current - 3 < $i) {
$this->paginationControls .= '<li class="paginate_button page-item"><a class="page-link" href="' . $this->queryString . $i . '"><span class="ib_pagination">' . $i . '</a></li>';
}
}
}
}
// Active Page
$this->paginationControls .= '<li class="paginate_button page-item active"><a class="page-link" href="javascript:void(0)">' . $this->current . '</a></li>';
// After Active Page
for ($i = $this->current + 1; $i <= $this->last; $i++){
$this->paginationControls .= '<li class="paginate_button page-item"><a class="page-link" href="' . $this->queryString . $i . '">' . $i . '</a></li>';
if($i >= $this->current + 2) {
break;
}
}
// this->last Page
if($this->last >= $this->current + 3) {
$this->paginationControls .= '<li class="paginate_button page-item"><a class="page-link" href="' . $this->queryString . $this->last . '"><span class="ib_pagination"><i class="fas fa-angle-double-right"></i></a></li>';
}
}
return $this;
}

public function Render() {
return $this->paginationControls;
}

public function Paginate($total, $limit, $options = []) {
$last = ceil($total/$limit);
if($last < 1){
$last = 1;
}

// Establish the $pagenum variable
$page = 1;
$link = '?page=';

// Configure options
if($options) {
// querystring
if(array_key_exists('querystring', $options)) {
$link = $options['querystring'] . '&page=';
}
//page
if(array_key_exists('page', $options)) {
$page = $options['page'];
}
}

// Get page from URL vars if it is present, else it is = 1
if(isset($request['page'])) {
$page = preg_replace('#[^0-9]#', '', $request['page']);
}
if ($page < 1) {
$page = 1;
} else if ($page > $last) {
$page = $last;
}

$pages = [];
$current = $page;

if($last != 1) {
// Previous button
if($page == 1) {
$pages[] = [
'page' => NULL,
'link' => NULL,
'type' => 'previous'
];
} else {
$pages[] = [
'page' => $page - 1,
'link' => $link . ($page - 1),
'type' => 'previous'
];
}
if ($current > 1) {
// Before Active Page
for($i = $current - ($this->maxControlsPerPage - 1); $i < $current; $i++) {
if($i > 0) {
if($current - 4 < $i) {
$pages[] = [
'page' => $i,
'link' => $link . $i,
'type' => 'default'
];
}
}
}
}

// Current page
$pages[] = [
'page' => $page,
'link' => $link . $page,
'type' => 'active'
];

// After Active Page
for ($i = $current + 1; $i <= $last; $i++){
$pages[] = [
'page' => $i,
'link' => $link . $i,
'type' => 'default'
];
if($i >= $current + 3) {
break;
}
}

// Next button
if($page == $last) {
$pages[] = [
'page' => NULL,
'link' => NULL,
'type' => 'next'
];
} else {
$pages[] = [
'page' => $page + 1,
'link' => $link . ($page + 1),
'type' => 'next'
];
}
}

return Application::$app->ToObject($pages);
}
}
13 changes: 12 additions & 1 deletion src/Core/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,17 @@ public function GetUrl()

foreach($path as $temp) {
$temprow = explode('=', $temp);

if(in_array('view', $temprow)) {
$path = '/' . $temprow[1];
break;
} else if(in_array('tab', $temprow)) {
$path .= '/' . $temprow[1];
} else if(in_array('action', $temprow)) {
$path .= '/' . $temprow[1];
}
}
}

return $path;
}

Expand Down Expand Up @@ -91,6 +96,12 @@ public function GetBody()
$data[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_SPECIAL_CHARS);
}
}
if($_FILES) {
FileStorage::Push($value);
foreach ($_FILES as $file) {
FileStorage::Push($file);
}
}
return $data;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Core/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function DownloadFile($file = NULL) {
$this->Header($file);
}

protected function Header($url) {
public function Header($url) {
header('location: ' . $url);
}
}
13 changes: 13 additions & 0 deletions src/Core/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Session
{
protected const FLASH_KEY = 'flash_messages';
protected $USER_KEY = 'user';
protected $GUEST_KEY = 'guest';

public function __construct($userKey)
{
Expand Down Expand Up @@ -63,6 +64,18 @@ public function DeAuth() {
$this->Remove($this->USER_KEY);
}

public function AuthGuest($value) {
$this->Set($this->GUEST_KEY, $value);
}

public function GetAuthGuest() {
return $this->Get($this->GUEST_KEY);
}

public function DeAuthGuest() {
$this->Remove($this->GUEST_KEY);
}

public function Set($key, $value)
{
$_SESSION[$key] = $value;
Expand Down
Loading