Skip to content

Commit 1a8e8b2

Browse files
Merge pull request #20 from infinitybrackets/main
Main
2 parents 841625a + 785b7f9 commit 1a8e8b2

File tree

11 files changed

+301
-8
lines changed

11 files changed

+301
-8
lines changed

src/Core/Application.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,35 @@ class Application {
1212
public string $layout = 'app';
1313
public ?Controller $controller = null;
1414
public $config = [];
15+
public $files = [];
1516

1617
public function __construct($config = [])
1718
{
1819
$this->LoadSettings($config);
1920
self::$app = $this;
2021

21-
$this->user = null;
22+
$this->user = NULL;
23+
$this->guest = NULL;
2224
$this->request = new Request();
2325
$this->response = new Response();
26+
$this->storage = new FileStorage();
2427
$this->router = new Router($this->request, $this->response);
2528
$this->database = new Database();
2629
$this->session = new Session($this->config->auth->defaults->session);
2730
$this->view = new View();
2831
$this->services = new ServiceProvider();
32+
$this->helpers = $this->ToObject([
33+
'StringHelper' => '\InfinityBrackets\Helpers\StringHelper'
34+
]);
2935

3036
$userId = Application::$app->session->GetAuth();
3137
if ($userId) {
3238
$this->user = $this->userClass::FindUser($userId);
39+
} else {
40+
$guestUserId = Application::$app->session->GetAuthGuest();
41+
if($guestUserId) {
42+
$this->guest = $this->guestClass::FindGuestUser($guestUserId);
43+
}
3344
}
3445
}
3546

@@ -40,6 +51,7 @@ public function __construct($config = [])
4051
public function LoadSettings($config) {
4152
self::$ROOT_DIR = $config['root'];
4253
$this->userClass = $config['auth']['userClass'];
54+
$this->guestClass = $config['auth']['guestClass'];
4355
$this->config = $config;
4456

4557
// Transform config type (Array) to (Object)
@@ -88,4 +100,11 @@ public function ToJSON($data) {
88100
}
89101
echo json_encode($data);
90102
}
103+
104+
public function HasPermission($name) {
105+
$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()]);
106+
107+
echo ($count > 0 ? '' : 'disabled');
108+
//echo 'disabled';
109+
}
91110
}

src/Core/Controller.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ class Controller
88
{
99
public string $layout = 'app';
1010
public string $action = '';
11-
11+
public $model = NULL;
12+
public $models = [];
1213
protected array $middlewares = [];
1314

1415
public function SetLayout($layout): void
@@ -30,4 +31,15 @@ public function GetMiddlewares(): array
3031
{
3132
return $this->middlewares;
3233
}
34+
35+
public function RegisterModel($model) {
36+
$this->model = new $model();
37+
}
38+
39+
public function BindModel($classes = []) {
40+
foreach($classes as $class) {
41+
$temp = explode('\\', $class);
42+
$this->models[end($temp)] = new $class();
43+
}
44+
}
3345
}

src/Core/Database.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public function __construct($config = NULL)
3333
}
3434
}
3535
}
36+
if(is_array($config)) {
37+
$default = $config;
38+
}
3639
if(is_null($default)) {
3740
$default = Application::$app->config->env;
3841
}
@@ -125,11 +128,11 @@ public function Rollback() {
125128

126129
public function Query($statement = "", $parameters = []) {
127130
try {
128-
$stmt = $this->pdo->query($statement);
129-
$stmt->execute($parameters);
131+
$stmt = $this->ExecuteStatement($statement, $parameters);
132+
$this->results = $stmt->fetchAll();
130133
return $this;
131134
} catch(Exception $e) {
132-
throw new Exception($e->getMessage());
135+
throw new Exception($e->getMessage());
133136
}
134137
}
135138

src/Core/FileStorage.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace InfinityBrackets\Core;
4+
5+
class FileStorage
6+
{
7+
private static $files = [];
8+
9+
protected function Files() {
10+
return self::$files;
11+
}
12+
13+
public function Push($file)
14+
{
15+
self::$files = $file;
16+
}
17+
18+
public function HasFiles()
19+
{
20+
return count(self::$files) > 0 ? TRUE : FALSE;
21+
}
22+
23+
public function Upload($storage) {
24+
$fileName = self::$files['name'];
25+
$tempLocation = self::$files['tmp_name'];
26+
$fileExtension = pathinfo($fileName, PATHINFO_EXTENSION);
27+
$name = uniqid(true) . '.' . $fileExtension;
28+
29+
$upload = move_uploaded_file($tempLocation, $storage . $name) ? TRUE : FALSE;
30+
31+
return Application::$app->ToObject(['success' => $upload, 'name' => $name]);
32+
}
33+
}

src/Core/Pagination.php

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
<?php
2+
3+
namespace InfinityBrackets\Core;
4+
/*
5+
* This is not for sale but it's free to use for any web application
6+
*
7+
* @package Infinity Brackets
8+
* @author John Vincent Bonza
9+
* @copyright 2021 Infinity Brackets
10+
* @license Free
11+
* @version v3.0
12+
*/
13+
use InfinityBrackets\Core\Pagination;
14+
15+
class Pagination {
16+
public $paginationControls = "";
17+
public $maxControlsPerPage = 5;
18+
public $current = 1;
19+
public $last = 1;
20+
public $queryString = "";
21+
22+
public function __construct($config = []) {
23+
if(array_key_exists('current', $config)) {
24+
$this->current = $config['current'];
25+
}
26+
if(array_key_exists('last', $config)) {
27+
$this->last = $config['last'];
28+
}
29+
if(array_key_exists('queryString', $config)) {
30+
$this->queryString .= $config['queryString'];
31+
}
32+
if(array_key_exists('orderBy', $config)) {
33+
$this->queryString .= '&orderBy=' . $config['orderBy'];
34+
}
35+
}
36+
37+
public function GeneratePagination() {
38+
$this->queryString .='&page=';
39+
if($this->last != 1) {
40+
if ($this->current > 1) {
41+
// First Page
42+
if($this->current >= $this->maxControlsPerPage - 1) {
43+
$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>';
44+
}
45+
// Before Active Page
46+
for($i = $this->current - ($this->maxControlsPerPage - 1); $i < $this->current; $i++) {
47+
if($i > 0) {
48+
if($this->current - 3 < $i) {
49+
$this->paginationControls .= '<li class="paginate_button page-item"><a class="page-link" href="' . $this->queryString . $i . '"><span class="ib_pagination">' . $i . '</a></li>';
50+
}
51+
}
52+
}
53+
}
54+
// Active Page
55+
$this->paginationControls .= '<li class="paginate_button page-item active"><a class="page-link" href="javascript:void(0)">' . $this->current . '</a></li>';
56+
// After Active Page
57+
for ($i = $this->current + 1; $i <= $this->last; $i++){
58+
$this->paginationControls .= '<li class="paginate_button page-item"><a class="page-link" href="' . $this->queryString . $i . '">' . $i . '</a></li>';
59+
if($i >= $this->current + 2) {
60+
break;
61+
}
62+
}
63+
// this->last Page
64+
if($this->last >= $this->current + 3) {
65+
$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>';
66+
}
67+
}
68+
return $this;
69+
}
70+
71+
public function Render() {
72+
return $this->paginationControls;
73+
}
74+
75+
public function Paginate($total, $limit, $options = []) {
76+
$last = ceil($total/$limit);
77+
if($last < 1){
78+
$last = 1;
79+
}
80+
81+
// Establish the $pagenum variable
82+
$page = 1;
83+
$link = '?page=';
84+
85+
// Configure options
86+
if($options) {
87+
// querystring
88+
if(array_key_exists('querystring', $options)) {
89+
$link = $options['querystring'] . '&page=';
90+
}
91+
//page
92+
if(array_key_exists('page', $options)) {
93+
$page = $options['page'];
94+
}
95+
}
96+
97+
// Get page from URL vars if it is present, else it is = 1
98+
if(isset($request['page'])) {
99+
$page = preg_replace('#[^0-9]#', '', $request['page']);
100+
}
101+
if ($page < 1) {
102+
$page = 1;
103+
} else if ($page > $last) {
104+
$page = $last;
105+
}
106+
107+
$pages = [];
108+
$current = $page;
109+
110+
if($last != 1) {
111+
// Previous button
112+
if($page == 1) {
113+
$pages[] = [
114+
'page' => NULL,
115+
'link' => NULL,
116+
'type' => 'previous'
117+
];
118+
} else {
119+
$pages[] = [
120+
'page' => $page - 1,
121+
'link' => $link . ($page - 1),
122+
'type' => 'previous'
123+
];
124+
}
125+
if ($current > 1) {
126+
// Before Active Page
127+
for($i = $current - ($this->maxControlsPerPage - 1); $i < $current; $i++) {
128+
if($i > 0) {
129+
if($current - 4 < $i) {
130+
$pages[] = [
131+
'page' => $i,
132+
'link' => $link . $i,
133+
'type' => 'default'
134+
];
135+
}
136+
}
137+
}
138+
}
139+
140+
// Current page
141+
$pages[] = [
142+
'page' => $page,
143+
'link' => $link . $page,
144+
'type' => 'active'
145+
];
146+
147+
// After Active Page
148+
for ($i = $current + 1; $i <= $last; $i++){
149+
$pages[] = [
150+
'page' => $i,
151+
'link' => $link . $i,
152+
'type' => 'default'
153+
];
154+
if($i >= $current + 3) {
155+
break;
156+
}
157+
}
158+
159+
// Next button
160+
if($page == $last) {
161+
$pages[] = [
162+
'page' => NULL,
163+
'link' => NULL,
164+
'type' => 'next'
165+
];
166+
} else {
167+
$pages[] = [
168+
'page' => $page + 1,
169+
'link' => $link . ($page + 1),
170+
'type' => 'next'
171+
];
172+
}
173+
}
174+
175+
return Application::$app->ToObject($pages);
176+
}
177+
}

src/Core/Request.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,17 @@ public function GetUrl()
3939

4040
foreach($path as $temp) {
4141
$temprow = explode('=', $temp);
42+
4243
if(in_array('view', $temprow)) {
4344
$path = '/' . $temprow[1];
44-
break;
45+
} else if(in_array('tab', $temprow)) {
46+
$path .= '/' . $temprow[1];
47+
} else if(in_array('action', $temprow)) {
48+
$path .= '/' . $temprow[1];
4549
}
4650
}
4751
}
52+
4853
return $path;
4954
}
5055

@@ -91,6 +96,12 @@ public function GetBody()
9196
$data[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_SPECIAL_CHARS);
9297
}
9398
}
99+
if($_FILES) {
100+
FileStorage::Push($value);
101+
foreach ($_FILES as $file) {
102+
FileStorage::Push($file);
103+
}
104+
}
94105
return $data;
95106
}
96107

src/Core/Response.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function DownloadFile($file = NULL) {
4949
$this->Header($file);
5050
}
5151

52-
protected function Header($url) {
52+
public function Header($url) {
5353
header('location: ' . $url);
5454
}
5555
}

src/Core/Session.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class Session
66
{
77
protected const FLASH_KEY = 'flash_messages';
88
protected $USER_KEY = 'user';
9+
protected $GUEST_KEY = 'guest';
910

1011
public function __construct($userKey)
1112
{
@@ -63,6 +64,18 @@ public function DeAuth() {
6364
$this->Remove($this->USER_KEY);
6465
}
6566

67+
public function AuthGuest($value) {
68+
$this->Set($this->GUEST_KEY, $value);
69+
}
70+
71+
public function GetAuthGuest() {
72+
return $this->Get($this->GUEST_KEY);
73+
}
74+
75+
public function DeAuthGuest() {
76+
$this->Remove($this->GUEST_KEY);
77+
}
78+
6679
public function Set($key, $value)
6780
{
6881
$_SESSION[$key] = $value;

0 commit comments

Comments
 (0)