Källkod till PHP Moment 4 & Labb 3 i Databaser troligen godkänd! [Del 4 av 4]
[DEL 4 AV 4]: Källkod till PHP Moment 4 (skapa mapparna "css", "include" och "class" inuti "include" och kör sedan "install.php" och sedan "index.php"):
p-moment4-new-blogposts.php
<?php include("include/config.php");?>
<?php $currentBtnAdmin=2; $currentBtn=7; // Mark current page in <nav> (see c-nav.php) & Moment 4 nav
?>
<title><?php
if(isLoggedIn()){
echo pageTitle("Moment 4 - " . strval($_SESSION['username']) . " inloggad");
}else {
echo pageTitle("Moment 4 - Ej inloggad");}?>
</title>
<?php include("include/c-header.php");?>
<?php include("include/c-nav.php");?>
<h2 id="specialh2">Moment 4 - Dataanslutningar med PHP | <?php echo isLoggedIn() ? strval($_SESSION['username']) . " inloggad" : " Ej inloggad" ?></h2>
<div class="uppgifts-div" id="special1">
<form action="p-moment4-logout.php" method="POST" id="logoutBtnSection">
<?php
// Show success message after succeeding logging in
showSessionSuccess('loginSuccess','Du är nu inloggad!');
// Show failure message when trying to logout without using log out button
showSessionError('logOutWithoutBtn','Logga ut med Logga ut-knappen!');
?>
<span style="font-size: 0.8rem;"><?php
if(isLoggedIn()){ // Show username if logged in
echo "Inloggad: " . strval($_SESSION['username']);
} else { // Else show info that you are not logged in
echo "Ej inloggad";
}
?> </span>
<?php // If logged in then show button for log out
if(isLoggedIn()){
?>
<input type="submit" name="loginOut" value="Logga ut" id="logoutBtn">
<?php } else { // Otherwise, show links to login or register
?>
<a class="backA" style="color: #1bbb85;" href="p-moment4-login.php">Inloggning</a>
<a class="backA" style="color: #1bbb85;" href="p-moment4-register.php">Registrera</a>
<?php }?>
</form>
<ul id="nav-ul2">
<li class="menu-btn2 <?php echo setCurrentBtnAdmin(1);?>">
<a href="p-moment4.php">Startsida</a></li>
<li class="menu-btn2 <?php echo setCurrentBtnAdmin(2);?>">
<a href="p-moment4-new-blogposts.php">Nya inlägg</a></li>
<?php if(isLoggedIn()){ ?>
<li class="menu-btn2 <?php echo setCurrentBtnAdmin(3);?>">
<a href="p-moment4-manage-blogposts.php">Hantera inlägg</a>
</li>
<?php }?>
</ul>
<?php showSessionError('id-error','Du kan inte visa inget inlägg!');?>
<hr>
<h3 style="margin-top:10px;">Nya blogginlägg</h3>
<?php
// Retrieve the two latest published blog posts
$currentBlogposts = $DB->getAllBlogPosts("");
// and check if 0 blog posts exist by checking length of array
if(count($currentBlogposts) == 0){
// If zero blog posts exist, echo info about that
echo showError('Inga blogginlägg finns!');
}
// If blogposts do exist, output them with foreach()
// substr() is used to only show YYYY-MM-DD HH:MM by removing :SS(seconds)
// mb_substr() only shows first 200 characters of each blog post
else {
foreach($currentBlogposts as $blogpost){
?>
<h4 style="margin-top:20px;"><?= htmlspecialchars($blogpost['blogpost_title'], ENT_QUOTES, 'UTF-8');?></h4>
<p style="font-size:0.9rem; font-weight:bold; margin-bottom:5px;"><?= "Postad: " . blogPublishDate($blogpost['blogpost_created']) . $blogpost['blogpost_created_by'];?></p>
<p style="font-size:1rem;"><?php
// Only cut length of blogpost and add ... when over 200 characters long
if(mb_strlen(htmlspecialchars($blogpost['blogpost_text'],ENT_QUOTES, 'UTF-8')) > 200){
echo mb_substr(htmlspecialchars($blogpost['blogpost_text'], ENT_QUOTES, 'UTF-8'),0,200) . "...";
} else { echo htmlspecialchars($blogpost['blogpost_text'], ENT_QUOTES, 'UTF-8');} ?></p>
<p style="text-align:right;">
<a class="backA" style="color: #1bbb85;" href="p-moment4-view-blogpost.php?id=<?=$blogpost['id'];?>">Läs mer</a></p>
<?php
}
}
?>
<hr>
</div>
<?php include("include/secret.php")?>
<?php include("include/c-footer.php");?>
p-moment4-register.php
<?php include("include/config.php");
// WHEN REGISTER SUCCEEDS!
// Clicked on Registrera?
if(clickedP('register')){
// Check the following first, all must be TRUE
if(
// Valid Username Length
Validate::validUsernameLength($_POST['RegisterUsername'])
// Valid Username Type
&& Validate::validUsernameType($_POST['RegisterUsername'])
// Valid Email
&& Validate::isEmail($_POST['RegisterEmail'])
// Both Password Fields are same
&& Validate::IsTwoSame($_POST['RegisterPass'],$_POST['RegisterPassCheck'])
// Both Password Fields are valid
&& Validate::validPassword($_POST['RegisterPass'])
&& Validate::validPassword($_POST['RegisterPassCheck'])
// Username doesn't already exist
&& !$DB->userAlreadyExist($_POST['RegisterUsername'])
// Email doesn't already exist
&& !$DB->emailAlreadyExist($_POST['RegisterEmail'])
) // Iff All of Above is TRUE then and only then...
{ // Register new user in database
if($DB->registerUser($_POST['RegisterUsername'],$_POST['RegisterEmail'],$_POST['RegisterPass'])){
// Set Session so user is taken to login page after registration
$_SESSION['registerSuccess'] = 1;}}}
// Check if user registered and then send them to login page
if(isset($_SESSION['registerSuccess'])){ header("Location: p-moment4-login.php");}?>
<?php $currentBtn=7; // Mark current page in <nav> (see c-nav.php)?>
<title><?= pageTitle("Moment 4 - Registrera dig först!"); ?></title>
<?php include("include/c-header.php");?>
<?php include("include/c-nav.php");?>
<h2 id="specialh2">Moment 4 - Dataanslutningar med PHP | Registrering</h2>
<div class="uppgifts-div" id="special1">
<h3 style="text-align: left;">Registrera dig</h3>
<form id="form3" action="p-moment4-register.php" method="POST">
<div class="login-rows2">
<div class="login-parts">
<input id="dataRegisterName" class="login-fields fieldListen" type="text"
name="RegisterUsername" value="<?php echo previousFieldvalueP('register','RegisterUsername'); ?>">
<label for="dataRegisterName">Användarnamn</label>
<?php
// Demanding to fill out empty field
fillOutEmptyFieldP("register","RegisterUsername","Ange ett användarnamn");
// Clicked on Registrera?
if(clickedP('register')){
// If string length of Username is not zero but is not at least 6 characters long or more than 21 characters
if(strlen($_POST['RegisterUsername']) != 0 && !Validate::validUsernameLength($_POST['RegisterUsername'])){
// Then show error on how to fix
echo showError("Användarnamn ska vara mellan 6 och 21 tecken långt.");
}
// If string of username is not zero but contains forbidden characters
if(strlen($_POST['RegisterUsername']) != 0 && !Validate::validUsernameType($_POST['RegisterUsername'])){
// Then show error on how to fix
echo showError("Användarnamn får endast innehålla små bokstäver av a-z.");
}
// If Username is already in use in database
if(strlen($_POST['RegisterUsername']) != 0 && $DB->userAlreadyExist($_POST['RegisterUsername'])){
// Then show error on how to fix
echo showError("Användarnamnet används redan. Välj ett annat!");
}
if(Validate::validUsernameLength($_POST['RegisterUsername']) && Validate::validUsernameType($_POST['RegisterUsername']) && !$DB->userAlreadyExist($_POST['RegisterUsername'])){
echo showSuccess("Användarnamn är ledigt och giltigt.");
}
}
?>
</div>
<div class="login-parts">
<input id="dataRegisterEmail" class="login-fields fieldListen" type="text"
name="RegisterEmail" value="<?php echo previousFieldvalueP('register','RegisterEmail'); ?>">
<label for="dataRegisterEmail">E-post</label>
<?php
// Demanding to fill out empty field
fillOutEmptyFieldP("register","RegisterEmail","Ange en giltig e-postadress först");
// Clicked on Registrera?
if(clickedP('register')){
// If string length of email is not 0 but is still invalid
if(strlen($_POST['RegisterEmail']) != 0 && !Validate::isEmail($_POST['RegisterEmail'])){
// Then show error on how to fix
echo showError("E-post ska vara i stil med: exempel@domän.se");
}
// If Email is already in use in database
if(strlen($_POST['RegisterEmail']) != 0 && $DB->emailAlreadyExist($_POST['RegisterEmail'])){
// Then show error on how to fix
echo showError("E-postadressen används redan. Ange en annan!");
}
if(strlen($_POST['RegisterEmail']) != 0 && Validate::isEmail($_POST['RegisterEmail']) && !$DB->emailAlreadyExist($_POST['RegisterEmail'])){
echo showSuccess("E-postadressen är ledig och giltig.");
}
}
?>
</div>
<div class="login-parts">
<input id="dataLoginPass" class="login-fields fieldListen" type="password"
name="RegisterPass">
<label for="dataLoginPass">Lösenord</label>
<?php
// Demanding to fill out empty field
fillOutEmptyFieldP("register","RegisterPass","Ange ett giltigt lösenord först");
// Clicked on Registrera?
if(clickedP('register')){
// If string length of Password (first field) is not zero but still invalid
if(strlen($_POST['RegisterPass']) != 0 && !Validate::validPassword($_POST['RegisterPass'])){
// Then show error on how to fix
echo showError("Lösenord ska vara mellan 12-24 tecken långt.<br>- Minst en siffra<br>- Minst en stor bokstav<br>- Minst en liten bokstav<br>- Minst ett specialtecken<br>- A-Z, 0-9 och specialtecknen _?!- får användas!");
}
}
?>
</div>
<div class="login-parts">
<input id="dataRegisterPassCheck" class="login-fields fieldListen" type="password"
name="RegisterPassCheck">
<label for="dataRegisterPassCheck">Upprepa lösenord</label>
<?php
// Demanding to fill out empty field
fillOutEmptyFieldP("register","RegisterPassCheck","Ange ett upprepat giltigt lösenord först");
// Clicked on Registrera?
if(clickedP('register')){
// If both Passwords fields are not empty then...
if($_POST['RegisterPass'] != "" && $_POST['RegisterPassCheck'] != ""){
// ... check if both Password Fields do NOT contain same values...
if(!Validate::IsTwoSame($_POST['RegisterPass'],$_POST['RegisterPassCheck'])){
// And Then show error on how to fix
echo showError("Både lösenordsfältet och upprepning ska vara lika.");
}} else if ($_POST['RegisterPassCheck'] != ""){
echo showError("Mata in samma giltiga lösenord här som ovan.");
}
}
?>
</div>
<div style="display: flex; flex-direction: row; justify-content: space-between; width: 100%;">
<input type="submit" name="register" value="Registrera" id="registerBtn">
<p><a style="display:inline-block; line-height: 3;" class="backA" href="p-moment4.php">Logga in</a></p>
</div>
</div>
<?php
?>
</form>
</div>
<?php include("include/secret.php")?>
<?php include("include/c-footer.php");?>
p-moment4-view-blogpost.php
<?php include("include/config.php");?>
<?php $currentBtnAdmin=0; $currentBtn=7; // Mark current page in <nav> (see c-nav.php)
// Check if logged in, otherwise redirect to login page
// Trying to access view-blogpost without an id
if(!isset($_GET['id'])){
$_SESSION['id-error'] = 1; // Set error message
header("Location: p-moment4.php"); // to be shown on admin page
}
?>
<title><?= pageTitle("Moment 4 - " . strval($_SESSION['username']) . " inloggad"); ?></title>
<?php include("include/c-header.php");?>
<?php include("include/c-nav.php");?>
<h2 id="specialh2">Moment 4 - Dataanslutningar med PHP | <?php echo isLoggedIn() ? strval($_SESSION['username']) . " inloggad" : " Ej inloggad" ?></h2>
<div class="uppgifts-div" id="special1">
<form action="p-moment4-logout.php" method="POST" id="logoutBtnSection">
<?php
showSessionSuccess('loginSuccess','Du är nu inloggad!');
showSessionError('logOutWithoutBtn','Logga ut med Logga ut-knappen!');
?>
<span style="font-size: 0.8rem;"><?php
if(isLoggedin()){
echo "Inloggad: " . strval($_SESSION['username']);
} else {
echo "Ej inloggad";
}
?> </span>
<?php if(isLoggedIn()){ ?>
<input type="submit" name="loginOut" value="Logga ut" id="logoutBtn">
<?php } else {
?>
<a class="backA" style="color: #1bbb85;" href="p-moment4-login.php">Inloggning</a>
<a class="backA" style="color: #1bbb85;" href="p-moment4-register.php">Registrera</a>
<?php }?>
</form>
<ul id="nav-ul2">
<li class="menu-btn2 <?php echo setCurrentBtnAdmin(1);?>">
<a href="p-moment4.php">Startsida</a></li>
<li class="menu-btn2 <?php echo setCurrentBtnAdmin(2);?>">
<a href="p-moment4-new-blogposts.php">Nya inlägg</a></li>
<?php if(isLoggedin()){ ?>
<li class="menu-btn2 <?php echo setCurrentBtnAdmin(3);?>">
<a href="p-moment4-manage-blogposts.php">Hantera inlägg</a>
</li>
<?php }?>
</ul>
<?php showSessionError('id-error','Du kan inte visa inget inlägg!');?>
<hr>
<?php
// Retrieve blog post by id
$currentBlogpost = $DB->getBlogPostById($_GET['id'],"");
// and check if 0 blog posts exist by checking length of array
if(count($currentBlogpost) == 0){
// If zero blog posts exist, echo info about that
echo "<p>Blogginlägget finns inte!</p>";
}
// If blogposts do exist, output them with foreach()
// substr() is used to only show YYYY-MM-DD HH:MM by removing :SS(seconds)
// CSS class "blogpost-feature" uses line-clamp
else {
foreach($currentBlogpost as $blogpost){
?>
<h3 style="margin-top:20px;"><?= htmlspecialchars($blogpost['blogpost_title'], ENT_QUOTES, 'UTF-8');?></h3>
<p style="font-size:0.9rem; font-weight:bold; margin-bottom:5px;"><?= "Postad: " . blogPublishDate($blogpost['blogpost_created']) . $blogpost['blogpost_created_by'];?></p>
<p style="font-size:1rem;"><?= htmlspecialchars($blogpost['blogpost_text'], ENT_QUOTES, 'UTF-8') ?></p>
<p style="text-align:right;">
<?php
// Only show the ability to edit a blogpost if allow editing a blogpost if correct user is logged in! Thus checking with blogposted_created_by from database!
if(isset($_SESSION['username']))
{
if($_SESSION['username'] === $blogpost['blogpost_created_by']){?>
<a class="backA" style="color: #1bbb85;" href="p-moment4-edit-blogpost.php?editid=<?=$blogpost['id'];?>">Ändra inlägg</a></p>
<?php }}
}
}
?>
<hr>
</div>
<?php include("include/secret.php")?>
<?php include("include/c-footer.php");?>
p-moment4.php
<?php include("include/config.php");?>
<?php $currentBtnAdmin=1; $currentBtn=7; // Mark current page in <nav> (see c-nav.php) & Moment 4 nav
?>
<title><?php
if(isLoggedIn()){
echo pageTitle("Moment 4 - " . strval($_SESSION['username']) . " inloggad");
}else {
echo pageTitle("Moment 4 - Ej inloggad");}?>
</title>
<?php include("include/c-header.php");?>
<?php include("include/c-nav.php");?>
<h2 id="specialh2">Moment 4 - Dataanslutningar med PHP | <?php echo isLoggedIn() ? strval($_SESSION['username']) . " inloggad" : " Ej inloggad" ?></h2>
<div class="uppgifts-div" id="special1">
<form action="p-moment4-logout.php" method="POST" id="logoutBtnSection">
<?php
showSessionSuccess('loginSuccess','Du är nu inloggad!');
showSessionError('logOutWithoutBtn','Logga ut med Logga ut-knappen!');
?>
<span style="font-size: 0.8rem;"><?php
if(isLoggedin()){ // Show logged in as
echo "Inloggad: " . strval($_SESSION['username']);
} else { // Or not logged in
echo "Ej inloggad";
}
?> </span>
<?php // If logged in then show button for log out
if(isLoggedIn()){
?>
<input type="submit" name="loginOut" value="Logga ut" id="logoutBtn">
<?php } else { // Otherwise, show links to login or register
?>
<a class="backA" style="color: #1bbb85;" href="p-moment4-login.php">Inloggning</a>
<a class="backA" style="color: #1bbb85;" href="p-moment4-register.php">Registrera</a>
<?php }?>
</form>
<ul id="nav-ul2">
<li class="menu-btn2 <?php echo setCurrentBtnAdmin(1);?>">
<a href="p-moment4.php">Startsida</a></li>
<li class="menu-btn2 <?php echo setCurrentBtnAdmin(2);?>">
<a href="p-moment4-new-blogposts.php">Nya inlägg</a></li>
<?php if(isLoggedin()){ ?>
<li class="menu-btn2 <?php // This button is only shown when logged in as it is administrative
echo setCurrentBtnAdmin(3);?>">
<a href="p-moment4-manage-blogposts.php">Hantera inlägg</a>
</li>
<?php }?>
</ul>
<?php showSessionError('id-error','Du kan inte visa inget inlägg!');?>
<hr>
<h3 style="margin-top:10px;">De 2 senaste blogginläggen</h3>
<?php
// Retrieve the two latest published blog posts
$currentBlogposts = $DB->get2LatestBlogPosts("");
// and check if 0 blog posts exist by checking length of array
if(count($currentBlogposts) == 0){
// If zero blog posts exist, echo info about that
echo showError('Inga blogginlägg finns!');
}
// If blogposts do exist, output them with foreach()
// substr() is used to only show YYYY-MM-DD HH:MM by removing :SS(seconds)
// mb_substr() only shows first 200 characters of each blog post
else {
foreach($currentBlogposts as $blogpost){
?>
<h4 style="margin-top:20px;"><?= htmlspecialchars($blogpost['blogpost_title'], ENT_QUOTES, 'UTF-8');?></h4>
<p style="font-size:0.9rem; font-weight:bold; margin-bottom:5px;"><?= "Postad: " . blogPublishDate($blogpost['blogpost_created']) . $blogpost['blogpost_created_by'];?></p>
<p style="font-size:1rem;"><?php
// Only cut length of blogpost and add "..." when over 200 characters long
if(mb_strlen(htmlspecialchars($blogpost['blogpost_text'], ENT_QUOTES, 'UTF-8')) > 200){
echo mb_substr(htmlspecialchars($blogpost['blogpost_text'], ENT_QUOTES, 'UTF-8'),0,200) . "...";
} else { echo htmlspecialchars($blogpost['blogpost_text'], ENT_QUOTES, 'UTF-8');} ?></p>
<p style="text-align:right;">
<a class="backA" style="color: #1bbb85;" href="p-moment4-view-blogpost.php?id=<?=$blogpost['id'];?>">Läs mer</a></p>
<?php
}
}
?>
<hr>
</div>
<?php include("include/secret.php")?>
<?php include("include/c-footer.php");?>
På återseende!
Mvh,
WKL.