Skip to content

Commit d9ec181

Browse files
committed
Committed changes
0 parents  commit d9ec181

File tree

1,407 files changed

+255168
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,407 files changed

+255168
-0
lines changed

.htaccess

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# If the mod_rewrite module is enabled (in the central Apache configuration file "httpd.conf")
2+
<IfModule mod_rewrite.c>
3+
# Enable the mod_rewrite engine, allowing URL rewriting rules to take effect
4+
RewriteEngine On
5+
6+
# Allow the server to follow symbolic links, Prevent Indexing (i.e. directory listing)
7+
Options +FollowSymLinks -Indexes
8+
9+
# Handle Front Controller...
10+
# The rewrite conditions (the conditions when rewrite rule are required to be activated)
11+
# These rewrite conditions ensure that the rewrite rule is only applied if the requested URL does not correspond to an existing directory or file
12+
# If the requested filename is not a directory (!-d)
13+
RewriteCond %{REQUEST_FILENAME} !-d
14+
# If the requested filename is not a regular file (!-f)
15+
RewriteCond %{REQUEST_FILENAME} !-f
16+
17+
# Then, the rewrite rule is
18+
19+
# The rewrite rule
20+
RewriteRule ^(.*)$ index.php [QSA,L]
21+
# The [QSA] flag stands for "Query String Append." When this flag is used in a RewriteRule, it instructs Apache to append the existing query string from the original URL to the rewritten URL. The [L] flag indicates that this is the last rule to process if the conditions are met.
22+
23+
# ^(.*)$ Regular Expression. ^ is a metacharacter that denotes the start of a string or line. $ is a metacharacter that denotes the end of a string or line. () is a capturing group that captures the matched sequence of characters. . matches any single character except for a newline character. * is a quantifier that matches zero or more occurrences of the preceding element (in this case, .). Combining these elements, ^(.*)$ will match any sequence of characters from the beginning (^) to the end ($) of a string and capture it within the parentheses. It effectively captures the entire input string.
24+
# Summary: If the requested URL doesn't correspond to an existing directory or file, redirect all requests (anything) to 'index.php'. Also, prevent directory indexing and allow the server to follow symbolic links.
25+
</IfModule>
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace App\Controllers\Admin;
4+
5+
use System\Controller;
6+
7+
class AccessController extends Controller
8+
{
9+
/**
10+
* Check User Permissions to access admin pages
11+
*
12+
* @return void
13+
*/
14+
public function index()
15+
{
16+
$loginModel = $this->load->model('Login');
17+
18+
$ignoredPages = ['/admin/login', '/admin/login/submit'];
19+
20+
$currentRoute = $this->route->getCurrentRouteUrl();
21+
22+
// First Scenario :
23+
// User is not logged in and he is not requesting login page
24+
// then we will redirect him to login page
25+
if (($isNotLogged = ! $loginModel->isLogged()) AND ! in_array($currentRoute , $ignoredPages)) {
26+
return $this->url->redirectTo('/admin/login');
27+
}
28+
29+
// On going to this line
30+
// it means that there are two possibilities
31+
// First One the user is not logged in and he is requesting login page
32+
// Second One the user is logged in successfully and he is requesting
33+
// an admin page
34+
35+
if ($isNotLogged) {
36+
return false;
37+
}
38+
39+
$user = $loginModel->user();
40+
41+
$usersGroupsModel = $this->load->model('UsersGroups');
42+
43+
$usersGroup = $usersGroupsModel->get($user->users_group_id);
44+
45+
// If the user doesn't have permissions to access this page
46+
// then he will be redirected to 404 page
47+
if (! in_array($currentRoute, $usersGroup->pages)) {
48+
// we may create access-denied page
49+
return $this->url->redirectTo('/404');
50+
}
51+
}
52+
}
+210
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
<?php
2+
3+
namespace App\Controllers\Admin;
4+
5+
use System\Controller;
6+
7+
class AdsController extends Controller
8+
{
9+
/**
10+
* Display Ads List
11+
*
12+
* @return mixed
13+
*/
14+
public function index()
15+
{
16+
$this->html->setTitle('Ads');
17+
18+
$data['ads'] = $this->load->model('Ads')->all();
19+
20+
$data['success'] = $this->session->has('success') ? $this->session->pull('success') : null;
21+
22+
$view = $this->view->render('admin/ads/list', $data);
23+
24+
return $this->adminLayout->render($view);
25+
}
26+
27+
/**
28+
* Open Ads Form
29+
*
30+
* @return string
31+
*/
32+
public function add()
33+
{
34+
return $this->form();
35+
}
36+
37+
/**
38+
* Submit for creating new ad
39+
*
40+
* @return string | json
41+
*/
42+
public function submit()
43+
{
44+
$json = [];
45+
46+
if ($this->isValid()) {
47+
// it means there are no errors in form validation
48+
$this->load->model('Ads')->create();
49+
50+
$json['success'] = 'Ad Has Been Created Successfully';
51+
52+
$json['redirectTo'] = $this->url->link('/admin/ads');
53+
} else {
54+
// it means there are errors in form validation
55+
$json['errors'] = $this->validator->flattenMessages();
56+
}
57+
58+
return $this->json($json);
59+
}
60+
61+
/**
62+
* Display Edit Form
63+
*
64+
* @param int $id
65+
* @return string
66+
*/
67+
public function edit($id)
68+
{
69+
$adsModel = $this->load->model('Ads');
70+
71+
if (! $adsModel->exists($id)) {
72+
return $this->url->redirectTo('/404');
73+
}
74+
75+
$ad = $adsModel->get($id);
76+
77+
return $this->form($ad);
78+
}
79+
80+
/**
81+
* Display Form
82+
*
83+
* @param \stdClass $ad
84+
*/
85+
private function form($ad = null)
86+
{
87+
if ($ad) {
88+
// editing form
89+
$data['target'] = 'edit-ad-' . $ad->id;
90+
91+
$data['action'] = $this->url->link('/admin/ads/save/' . $ad->id);
92+
93+
$data['heading'] = 'Edit ' . $ad->title;
94+
} else {
95+
// adding form
96+
$data['target'] = 'add-ad-form';
97+
98+
$data['action'] = $this->url->link('/admin/ads/submit');
99+
100+
$data['heading'] = 'Add New Ad';
101+
}
102+
103+
$ad = (array) $ad;
104+
105+
$data['link'] = array_get($ad, 'link');
106+
$data['name'] = array_get($ad, 'name');
107+
$data['ad_page'] = array_get($ad, 'page');
108+
$data['status'] = array_get($ad, 'status', 'enabled');
109+
110+
$data['start_at'] = ! empty($ad['start_at']) ? date('d-m-Y', $ad['start_at']) : false;
111+
$data['end_at'] = ! empty($ad['end_at']) ? date('d-m-Y', $ad['end_at']) : false;
112+
113+
$data['image'] = '';
114+
115+
if (! empty($ad['image'])) {
116+
// default path to upload ad image : public/images
117+
$data['image'] = $this->url->link('public/images/' . $ad['image']);
118+
}
119+
120+
$data['pages'] = $this->getPermissionPages();
121+
122+
return $this->view->render('admin/ads/form', $data);
123+
}
124+
125+
/**
126+
* Get All Permission Pages
127+
*
128+
* @return array
129+
*/
130+
private function getPermissionPages()
131+
{
132+
$permissions = [];
133+
134+
foreach ($this->route->routes() AS $route) {
135+
if (strpos($route['url'], '/admin') !== 0) {
136+
$permissions[] = $route['url'];
137+
}
138+
}
139+
140+
return $permissions;
141+
}
142+
143+
/**
144+
* Submit for creating new ad
145+
*
146+
* @return string | json
147+
*/
148+
public function save($id)
149+
{
150+
$json = [];
151+
152+
if ($this->isValid($id)) {
153+
// it means there are no errors in form validation
154+
$this->load->model('Ads')->update($id);
155+
156+
$json['success'] = 'Ads Has Been Updated Successfully';
157+
158+
$json['redirectTo'] = $this->url->link('/admin/ads');
159+
} else {
160+
// it means there are errors in form validation
161+
$json['errors'] = $this->validator->flattenMessages();
162+
}
163+
164+
return $this->json($json);
165+
}
166+
167+
/**
168+
* Delete Record
169+
*
170+
* @param int $id
171+
* @return mixed
172+
*/
173+
public function delete($id)
174+
{
175+
$adsModel = $this->load->model('Ads');
176+
177+
if (! $adsModel->exists($id)) {
178+
return $this->url->redirectTo('/404');
179+
}
180+
181+
$adsModel->delete($id);
182+
183+
$json['success'] = 'Ad Has Been Deleted Successfully';
184+
185+
return $this->json($json);
186+
}
187+
188+
/**
189+
* Validate the form
190+
*
191+
* @param int $id
192+
* @return bool
193+
*/
194+
private function isValid($id = null)
195+
{
196+
$this->validator->required('name');
197+
$this->validator->required('link');
198+
$this->validator->required('page');
199+
$this->validator->required('start_at');
200+
$this->validator->required('end_at');
201+
202+
if (is_null($id)) {
203+
$this->validator->requiredFile('image')->image('image');
204+
} else {
205+
$this->validator->image('image');
206+
}
207+
208+
return $this->validator->passes();
209+
}
210+
}

0 commit comments

Comments
 (0)