Skip to content

Commit 9700c25

Browse files
authored
Merge pull request #11 from iazaran/features/enhancements
Features/enhancements
2 parents 53781b2 + 5a8a35f commit 9700c25

File tree

5 files changed

+98
-45
lines changed

5 files changed

+98
-45
lines changed

public/js/main.js

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,17 @@ $(document).ready(function () {
1313
/**
1414
* Enable bootstrap toast with options
1515
*/
16-
$(".toast").toast({delay: 4000});
16+
const toastElement = $(".toast");
17+
toastElement.toast({delay: 4000});
18+
19+
/**
20+
* Check if message cookie exist to show it
21+
*/
22+
const message = getCookie("message");
23+
if (message !== "") {
24+
toastElement.toast("show");
25+
$(".toast-body").text(decodeURI(message));
26+
}
1727

1828
/**
1929
* Enable tooltips everywhere
@@ -63,13 +73,13 @@ $(document).ready(function () {
6373
if (result["status"] === "OK") {
6474
window.location.replace("/");
6575
} else {
66-
$(".toast").toast("show");
76+
toastElement.toast("show");
6777
$(".toast-body").text(result["message"]);
6878
}
6979
},
7080
error(xhr, status, error) {
7181
// alert("responseText: " + xhr.responseText);
72-
$(".toast").toast("show");
82+
toastElement.toast("show");
7383
$(".toast-body").text(result["message"]);
7484
}
7585
});
@@ -106,15 +116,52 @@ $(document).ready(function () {
106116
if (result["status"] === "OK") {
107117
window.location.replace("/");
108118
} else {
109-
$(".toast").toast("show");
119+
toastElement.toast("show");
110120
$(".toast-body").text(result["message"]);
111121
}
112122
},
113123
error(xhr, status, error) {
114124
// alert("responseText: " + xhr.responseText);
115-
$(".toast").toast("show");
125+
toastElement.toast("show");
116126
$(".toast-body").text(result["message"]);
117127
}
118128
});
119129
});
120130
});
131+
132+
/**
133+
* Set cookie
134+
*
135+
* @param name
136+
* @param value
137+
* @param expiresDay
138+
*/
139+
function setCookie(name, value, expiresDay) {
140+
const d = new Date();
141+
d.setTime(d.getTime() + (expiresDay * 24 * 60 * 60 * 1000));
142+
let expires = "expires="+d.toUTCString();
143+
144+
document.cookie = name + "=" + value + ";" + expires + ";path=/";
145+
}
146+
147+
/**
148+
* Get cookie
149+
*
150+
* @param name
151+
* @returns {string}
152+
*/
153+
function getCookie(name) {
154+
let cookieName = name + "=";
155+
let ca = document.cookie.split(";");
156+
for(let i = 0; i < ca.length; i++) {
157+
let c = ca[i];
158+
while (c.charAt(0) === " ") {
159+
c = c.substring(1);
160+
}
161+
if (c.indexOf(cookieName) === 0) {
162+
return c.substring(cookieName.length, c.length);
163+
}
164+
}
165+
166+
return "";
167+
}

public/js/main.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Controllers/AuthController.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,21 @@ public function register()
5454
[$request->tagline, 'required', 'Please enter a tagline to introduce yourself!'],
5555
]);
5656

57-
if ($request->password1 !== $request->password2) {
58-
$output['status'] = 'ERROR';
59-
$output['message'] = 'Please repeat password in confirmation field!';
60-
} elseif (Auth::existed($request->email)) {
61-
$output['status'] = 'ERROR';
62-
$output['message'] = 'This Email registered before!';
63-
} elseif ($output['status'] == 'OK' && Helper::csrf($request->token) && Auth::register($request)) {
64-
Helper::mailto($request->email, 'Welcome to PHPMVC! Email Verification', '<p>Hi dear friend,</p><hr /><p>Please click on this link to verify your email</p><hr /><p>Good luck,</p><p><a href="http://localhost:8080?email=' . $request->email . '&user_token=' . $user_token . '" target="_blank" rel="noopener">Verify your email at PHPMVC</a></p>');
65-
66-
setcookie('message', 'Verification has been sent to your email, please check your inbox.', time() + 60);
67-
} else {
68-
$output['status'] = 'ERROR';
69-
$output['message'] = 'There is an error! Please try again.';
57+
if ($output['status'] == 'OK') {
58+
if ($request->password1 !== $request->password2) {
59+
$output['status'] = 'ERROR';
60+
$output['message'] = 'Please repeat password in confirmation field!';
61+
} elseif (Auth::existed($request->email)) {
62+
$output['status'] = 'ERROR';
63+
$output['message'] = 'This Email registered before!';
64+
} elseif (Helper::csrf($request->token) && Auth::register($request)) {
65+
Helper::mailto($request->email, 'Welcome to PHPMVC! Email Verification', '<p>Hi dear friend,</p><hr /><p>Please click on this link to verify your email</p><hr /><p>Good luck,</p><p><a href="http://localhost:8080/verify?email=' . $request->email . '&user_token=' . $user_token . '" target="_blank" rel="noopener">Verify your email at PHPMVC</a></p>');
66+
67+
setcookie('message', 'Verification has been sent to your email, please check your inbox.', time() + 60);
68+
} else {
69+
$output['status'] = 'ERROR';
70+
$output['message'] = 'There is an error! Please try again.';
71+
}
7072
}
7173

7274
unset($_POST);
@@ -80,7 +82,7 @@ public function register()
8082
*/
8183
public function verify()
8284
{
83-
$request = json_decode(json_encode($_POST));
85+
$request = json_decode(json_encode($_GET));
8486

8587
if (Auth::verify($request) && $secret = Auth::getSecret($request)) {
8688
Helper::mailto($request->email, 'PHPMVC! Your API secret key', '<p>Hi dear friend,</p><hr /><p>This is your API secret key to access authenticated API routes:</p><p><strong>' . $secret . '</strong></p><p>Please keep it in a safe place.</p><hr /><p>Good luck,</p><p><a href="http://localhost:8080" target="_blank" rel="noopener">PHPMVC</a></p>');

src/Controllers/BlogController.php

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -100,17 +100,19 @@ public function store()
100100
[$request->body, 'required', 'Please enter a body for the post!'],
101101
]);
102102

103-
if ($output['status'] == 'OK' && Helper::csrf($request->token) && Blog::store($request)) {
104-
if (isset($_FILES['image']['type'])) {
105-
HandleForm::upload($_FILES['image'], ['jpeg', 'jpg','png'], 5000000, '../public/assets/images/', 85, Helper::slug($request->title, '-', false));
103+
if ($output['status'] == 'OK') {
104+
if (Helper::csrf($request->token) && Blog::store($request)) {
105+
if (isset($_FILES['image']['type'])) {
106+
HandleForm::upload($_FILES['image'], ['jpeg', 'jpg', 'png'], 5000000, '../public/assets/images/', 85, Helper::slug($request->title, '-', false));
107+
}
108+
109+
unset($_POST);
110+
XmlGenerator::feed();
111+
Cache::clearCache(['index', 'blog.index', 'api.index']);
112+
} else {
113+
$output['status'] = 'ERROR';
114+
$output['message'] = 'There is an error! Please try again.';
106115
}
107-
108-
unset($_POST);
109-
XmlGenerator::feed();
110-
Cache::clearCache(['index', 'blog.index', 'api.index']);
111-
} else {
112-
$output['status'] = 'ERROR';
113-
$output['message'] = 'There is an error! Please try again.';
114116
}
115117

116118
echo json_encode($output);
@@ -162,23 +164,25 @@ public function update()
162164
[$request->body, 'required', 'Please enter a body for the post!'],
163165
]);
164166

165-
if ($output['status'] == 'OK' && Helper::csrf($request->token) && Blog::update($request)) {
166-
Database::query("SELECT * FROM posts WHERE id = :id");
167-
Database::bind(':id', $request->id);
167+
if ($output['status'] == 'OK') {
168+
if ($output['status'] == 'OK' && Helper::csrf($request->token) && Blog::update($request)) {
169+
Database::query("SELECT * FROM posts WHERE id = :id");
170+
Database::bind(':id', $request->id);
168171

169-
$currentPost = Database::fetch();
172+
$currentPost = Database::fetch();
170173

171-
if (isset($_FILES['image']['type'])) {
172-
HandleForm::upload($_FILES['image'], ['jpeg', 'jpg','png'], 5000000, '../public/assets/images/', 85, substr($currentPost['slug'], 0, -11));
173-
}
174+
if (isset($_FILES['image']['type'])) {
175+
HandleForm::upload($_FILES['image'], ['jpeg', 'jpg', 'png'], 5000000, '../public/assets/images/', 85, substr($currentPost['slug'], 0, -11));
176+
}
174177

175-
unset($_POST);
176-
XmlGenerator::feed();
177-
Cache::clearCache('blog.show.' . $currentPost['slug']);
178-
Cache::clearCache(['index', 'blog.index', 'api.index']);
179-
} else {
180-
$output['status'] = 'ERROR';
181-
$output['message'] = 'There is an error! Please try again.';
178+
unset($_POST);
179+
XmlGenerator::feed();
180+
Cache::clearCache('blog.show.' . $currentPost['slug']);
181+
Cache::clearCache(['index', 'blog.index', 'api.index']);
182+
} else {
183+
$output['status'] = 'ERROR';
184+
$output['message'] = 'There is an error! Please try again.';
185+
}
182186
}
183187

184188
echo json_encode($output);

src/Models/Auth.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static function register(object $request): bool
2424
`secret`,
2525
`user_token`,
2626
`tagline`
27-
) VALUES (:email, :password, :secret, :tagline)");
27+
) VALUES (:email, :password, :secret, :user_token, :tagline)");
2828
Database::bind([
2929
':email' => $request->email,
3030
':password' => password_hash($request->password1, PASSWORD_DEFAULT),

0 commit comments

Comments
 (0)