Webbserverprogrammering 1

Källkod

Följande filer och mappar finns under mappen webbserverprogrammering.
Mappar visas till vänster och filer till höger. Klicka på en fil eller mapp för att öppna nedan eller visa dess innehåll.

webbserverprogrammering/projects/quiz-extended/

26 filer

admin.php
confirm_account.php
create_account.php
create_quiz.php
css/
dbconnection.php
highscore.php
index.php
js/
logbook.php
login.php
mysql_create_table_options.php
mysql_create_table_questions.php
mysql_create_table_quizzes.php
mysql_create_table_results.php
mysql_create_table_submits.php
mysql_create_table_users.php
plan.php
planering.txt
profile.php
projektrapport.txt
quizzes.php
resources/
result.php
send_email.php
session_variable_array_check.php

confirm_account.php

112 lines ASCII Windows (CRLF)
<?php

error_reporting
(-1); // Report all type of errors
ini_set('display_errors'1); // Display all errors 
ini_set('output_buffering'0); // Do not buffer outputs, write directly

function dump($dump) {
 echo 
"<pre>";
 
var_dump($dump);
 echo 
"</pre>";
}
session_start();
include 
"session_variable_array_check.php";

// verification and error statuses
define("VERIFICATION_SUCCESSFUL"0);
define("EXPIRED_TOKEN"1);
define("USER_ALREADY_VERIFIED"2);
define("INVALID_TOKEN"3);
define("MISSING_TOKEN"4);
define("MULTIPLE_MATCHES"5);

$verificationStatus = -1;
$expiryMinutes 15// minutes after registration after which email verification link expires

if (!isset($_GET['tkn'])) {
 
$verificationStatus MISSING_TOKEN;
} else {
 
$regtoken $_GET['tkn'];

 include 
"dbconnection.php";
 
$dbconn->exec("SET time_zone = '+00:00';"); // ensures consistent UTC use with handling timestamps
 
$sql "SELECT user_id, displayname, username, verified, created_date FROM quizext_users WHERE regtoken=?";
 
$stmt $dbconn->prepare($sql);
 
$stmt->execute([$regtoken]);
 
$users $stmt->fetchAll(PDO::FETCH_ASSOC);

 if (
count($users) === 1) {
  
$user $users[0];

  if (
$user['verified'] === "verified")
   
// nothing happens if link is already used
   
$verificationStatus USER_ALREADY_VERIFIED;
  else {
   
// time of expiry is calculated
   
$registerTime strtotime($user['created_date']);
   
$expiryTime $registerTime + ($expiryMinutes 60);
   
$now time();

   
// time is in seconds, bigger is later
   
if ($now $expiryTime) {
    
$verificationStatus EXPIRED_TOKEN;
   } else {

    
// sets verified column to "verified"
    
$sql "UPDATE quizext_users SET verified=? WHERE user_id=?";
    
$stmt $dbconn->prepare($sql);
    
$stmt->execute(["verified"$user['user_id']]);
    
    
$verificationStatus VERIFICATION_SUCCESSFUL;
   }
  }
 } else if (
count($users) === 0) {
  
$verificationStatus INVALID_TOKEN;
 } else {
  
$verificationStatus MULTIPLE_MATCHES;
 }
}


$message "";
switch (
$verificationStatus) {
 case 
VERIFICATION_SUCCESSFUL:
  
$message "Account verification successful for user<br>{$user['displayname']} ({$user['username']})";
  break;
 case 
EXPIRED_TOKEN:
  
$message "This link has expired.";
  break;
 case 
USER_ALREADY_VERIFIED:
  
$message "This link has already been used.";
  break;
 case 
INVALID_TOKEN:
  
$message "Invalid link.";
  break;
 case 
MISSING_TOKEN:
  
$message "Invalid link.";
  break;
 case 
MULTIPLE_MATCHES:
  
$message "System error. Contact webmaster.";
  break;
 default:
  
$message "Unknown error.";
}

?>

<!DOCTYPE html>
<html lang="sv">
<head>
 <title>Verify account - CuriousQuizzes</title>
 <meta charset="utf-8">
 <link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
 <a href="index.php" id="logo">
  CuriousQuizzes
 </a>
 <main>
  <h1><?= $message ?></h1>
 </main>
</body>
</html>