Skip to content
Merged
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
57 changes: 52 additions & 5 deletions public/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,17 @@ $(document).ready(function () {
/**
* Enable bootstrap toast with options
*/
$(".toast").toast({delay: 4000});
const toastElement = $(".toast");
toastElement.toast({delay: 4000});

/**
* Check if message cookie exist to show it
*/
const message = getCookie("message");
if (message !== "") {
toastElement.toast("show");
$(".toast-body").text(decodeURI(message));
}

/**
* Enable tooltips everywhere
Expand Down Expand Up @@ -63,13 +73,13 @@ $(document).ready(function () {
if (result["status"] === "OK") {
window.location.replace("/");
} else {
$(".toast").toast("show");
toastElement.toast("show");
$(".toast-body").text(result["message"]);
}
},
error(xhr, status, error) {
// alert("responseText: " + xhr.responseText);
$(".toast").toast("show");
toastElement.toast("show");
$(".toast-body").text(result["message"]);
}
});
Expand Down Expand Up @@ -106,15 +116,52 @@ $(document).ready(function () {
if (result["status"] === "OK") {
window.location.replace("/");
} else {
$(".toast").toast("show");
toastElement.toast("show");
$(".toast-body").text(result["message"]);
}
},
error(xhr, status, error) {
// alert("responseText: " + xhr.responseText);
$(".toast").toast("show");
toastElement.toast("show");
$(".toast-body").text(result["message"]);
}
});
});
});

/**
* Set cookie
*
* @param name
* @param value
* @param expiresDay
*/
function setCookie(name, value, expiresDay) {
const d = new Date();
d.setTime(d.getTime() + (expiresDay * 24 * 60 * 60 * 1000));
let expires = "expires="+d.toUTCString();

document.cookie = name + "=" + value + ";" + expires + ";path=/";
}

/**
* Get cookie
*
* @param name
* @returns {string}
*/
function getCookie(name) {
let cookieName = name + "=";
let ca = document.cookie.split(";");
for(let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) === " ") {
c = c.substring(1);
}
if (c.indexOf(cookieName) === 0) {
return c.substring(cookieName.length, c.length);
}
}

return "";
}
2 changes: 1 addition & 1 deletion public/js/main.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 16 additions & 14 deletions src/Controllers/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,21 @@ public function register()
[$request->tagline, 'required', 'Please enter a tagline to introduce yourself!'],
]);

if ($request->password1 !== $request->password2) {
$output['status'] = 'ERROR';
$output['message'] = 'Please repeat password in confirmation field!';
} elseif (Auth::existed($request->email)) {
$output['status'] = 'ERROR';
$output['message'] = 'This Email registered before!';
} elseif ($output['status'] == 'OK' && Helper::csrf($request->token) && Auth::register($request)) {
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>');

setcookie('message', 'Verification has been sent to your email, please check your inbox.', time() + 60);
} else {
$output['status'] = 'ERROR';
$output['message'] = 'There is an error! Please try again.';
if ($output['status'] == 'OK') {
if ($request->password1 !== $request->password2) {
$output['status'] = 'ERROR';
$output['message'] = 'Please repeat password in confirmation field!';
} elseif (Auth::existed($request->email)) {
$output['status'] = 'ERROR';
$output['message'] = 'This Email registered before!';
} elseif (Helper::csrf($request->token) && Auth::register($request)) {
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>');

setcookie('message', 'Verification has been sent to your email, please check your inbox.', time() + 60);
} else {
$output['status'] = 'ERROR';
$output['message'] = 'There is an error! Please try again.';
}
}

unset($_POST);
Expand All @@ -80,7 +82,7 @@ public function register()
*/
public function verify()
{
$request = json_decode(json_encode($_POST));
$request = json_decode(json_encode($_GET));

if (Auth::verify($request) && $secret = Auth::getSecret($request)) {
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>');
Expand Down
52 changes: 28 additions & 24 deletions src/Controllers/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,19 @@ public function store()
[$request->body, 'required', 'Please enter a body for the post!'],
]);

if ($output['status'] == 'OK' && Helper::csrf($request->token) && Blog::store($request)) {
if (isset($_FILES['image']['type'])) {
HandleForm::upload($_FILES['image'], ['jpeg', 'jpg','png'], 5000000, '../public/assets/images/', 85, Helper::slug($request->title, '-', false));
if ($output['status'] == 'OK') {
if (Helper::csrf($request->token) && Blog::store($request)) {
if (isset($_FILES['image']['type'])) {
HandleForm::upload($_FILES['image'], ['jpeg', 'jpg', 'png'], 5000000, '../public/assets/images/', 85, Helper::slug($request->title, '-', false));
}

unset($_POST);
XmlGenerator::feed();
Cache::clearCache(['index', 'blog.index', 'api.index']);
} else {
$output['status'] = 'ERROR';
$output['message'] = 'There is an error! Please try again.';
}

unset($_POST);
XmlGenerator::feed();
Cache::clearCache(['index', 'blog.index', 'api.index']);
} else {
$output['status'] = 'ERROR';
$output['message'] = 'There is an error! Please try again.';
}

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

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

$currentPost = Database::fetch();
$currentPost = Database::fetch();

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

unset($_POST);
XmlGenerator::feed();
Cache::clearCache('blog.show.' . $currentPost['slug']);
Cache::clearCache(['index', 'blog.index', 'api.index']);
} else {
$output['status'] = 'ERROR';
$output['message'] = 'There is an error! Please try again.';
unset($_POST);
XmlGenerator::feed();
Cache::clearCache('blog.show.' . $currentPost['slug']);
Cache::clearCache(['index', 'blog.index', 'api.index']);
} else {
$output['status'] = 'ERROR';
$output['message'] = 'There is an error! Please try again.';
}
}

echo json_encode($output);
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static function register(object $request): bool
`secret`,
`user_token`,
`tagline`
) VALUES (:email, :password, :secret, :tagline)");
) VALUES (:email, :password, :secret, :user_token, :tagline)");
Database::bind([
':email' => $request->email,
':password' => password_hash($request->password1, PASSWORD_DEFAULT),
Expand Down