<!-- Holly McFarland - 000778232 -->
|
|
<!-- 2019-09-26 -->
|
|
<!-- The result page for the fortune cookie generator -->
|
|
|
|
<?php
|
|
$name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING);
|
|
|
|
if ($name === null || $name === '') {
|
|
$name = 'you';
|
|
$name_input = '';
|
|
} else {
|
|
$name_input = $name;
|
|
}
|
|
|
|
$num = filter_input(INPUT_POST, 'num', FILTER_VALIDATE_INT);
|
|
$colour = filter_input(INPUT_POST, 'colour', FILTER_SANITIZE_STRING);
|
|
$min = filter_input(INPUT_POST, 'min', FILTER_VALIDATE_INT);
|
|
$max = filter_input(INPUT_POST, 'max', FILTER_VALIDATE_INT);
|
|
?>
|
|
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<title>Cookies for <?php echo $name; ?></title>
|
|
<link rel="stylesheet" type="text/css" href="style.css">
|
|
</head>
|
|
<body>
|
|
<?php
|
|
$error = false;
|
|
|
|
if ($num === null) {
|
|
echo '<p class="error">Number of cookies must be supplied.</p>';
|
|
$error = true;
|
|
} elseif ($num < 1) {
|
|
echo '<p class="error">Number of cookies must be at least 1.</p>';
|
|
$error = true;
|
|
} elseif ($num > 100) {
|
|
echo '<p class="error">Number of cookies must be at most 100.</p>';
|
|
$error = true;
|
|
}
|
|
|
|
if ($colour === null) {
|
|
echo '<p class="error">Colour of cookies must be supplied.</p>';
|
|
$error = true;
|
|
}
|
|
|
|
if ($min === null) {
|
|
echo '<p class="error">Lucky number minimum must be supplied.</p>';
|
|
$error = true;
|
|
} elseif ($min < 1) {
|
|
echo '<p class="error">Lucky number minimum must be at least 1.</p>';
|
|
$error = true;
|
|
} elseif ($min > 93) {
|
|
echo '<p class="error">Lucky number minimum must be at most 93.</p>';
|
|
$error = true;
|
|
}
|
|
|
|
if ($max === null) {
|
|
echo '<p class="error">Lucky number maximum must be supplied.</p>';
|
|
$error = true;
|
|
} elseif ($max > 99) {
|
|
echo '<p class="error">Lucky number maximum must at most 99.</p>';
|
|
$error = true;
|
|
} elseif ($min !== null && $max - $min < 6) {
|
|
echo '<p class="error">Lucky number maximum must be at least 7 more than the minimum.</p>';
|
|
$error = true;
|
|
}
|
|
|
|
if ($error) {
|
|
echo '<form action="index.php" method="post" class="hidden">';
|
|
echo '<input type="text" name="name" value="'.$name_input.'">';
|
|
echo '<input type="number" name="num" value="'.$num.'" >';
|
|
if ($colour !== null) {
|
|
echo '<input type="color" name="colour" value="'.$colour.'">';
|
|
}
|
|
echo '<input type="number" name="min" value="'.$min.'">';
|
|
echo '<input type="number" name="max" value="'.$max.'">';
|
|
echo '<input type="submit" value="Go back">';
|
|
echo '</form>';
|
|
echo '</body>';
|
|
echo '</html>';
|
|
exit;
|
|
}
|
|
|
|
$fortunefile = fopen("fortunes.txt", "r") or die("Unable to open file!"); // fortunes sourced from the `fortune` linux utility
|
|
$fortunes = explode("%", fread($fortunefile,filesize("fortunes.txt")));
|
|
fclose($fortunefile);
|
|
|
|
class FortuneCookie {
|
|
private $fortune;
|
|
private $numbers;
|
|
|
|
function __construct($fortune, $min, $max) {
|
|
$this->fortune = $fortune;
|
|
$this->numbers = array_rand(array_flip(range($min, $max)), 7); // choose numbers in range, sorted, without repititon
|
|
}
|
|
|
|
function get_fortune() {
|
|
return $this->fortune;
|
|
}
|
|
|
|
function get_numbers() {
|
|
return $this->numbers;
|
|
}
|
|
}
|
|
|
|
for ($i=0; $i<$num; $i++) {
|
|
$cookies[$i] = new FortuneCookie($fortunes[rand(0, count($fortunes)-1)], $min, $max);
|
|
}
|
|
?>
|
|
|
|
<h1>Your Cookies</h1>
|
|
|
|
<div class="metacontainer">
|
|
<div class="container">
|
|
<?php
|
|
for ($i=0; $i<$num; $i++) {
|
|
if ($i % 2 == 0 && $i !== 0) {
|
|
echo '</div>';
|
|
if ($i % 4 == 0 && $i !== 0) {
|
|
echo '</div><div class="metacontainer">';
|
|
}
|
|
echo '<div class="container">';
|
|
}
|
|
echo '<div class="cookie" style="background-color: '.$colour.';">';
|
|
echo '<p>'.$cookies[$i]->get_fortune().'</p>';
|
|
echo '<p>Your lucky numbers are: ';
|
|
echo join(', ', $cookies[$i]->get_numbers());
|
|
echo '</p>';
|
|
echo '</div>';
|
|
}
|
|
?>
|
|
</div>
|
|
</div>
|
|
|
|
<form action="cookies.php" method="post" class="hidden">
|
|
<input type="hidden" name="name" value="<?php echo $name_input; ?>">
|
|
<input type="hidden" name="num" value="<?php echo $num; ?>" >
|
|
<input type="hidden" name="colour" value="<?php echo $colour; ?>">
|
|
<input type="hidden" name="min" value="<?php echo $min; ?>">
|
|
<input type="hidden" name="max" value="<?php echo $max; ?>">
|
|
<input type="submit" value="Get new cookies!">
|
|
</form>
|
|
|
|
<form action="index.php" method="post" class="hidden">
|
|
<input type="hidden" name="name" value="<?php echo $name_input; ?>">
|
|
<input type="hidden" name="num" value="<?php echo $num; ?>" >
|
|
<input type="hidden" name="colour" value="<?php echo $colour; ?>">
|
|
<input type="hidden" name="min" value="<?php echo $min; ?>">
|
|
<input type="hidden" name="max" value="<?php echo $max; ?>">
|
|
<input type="submit" value="Go back">
|
|
</form>
|
|
</body>
|
|
</html>
|