initial commit

This commit is contained in:
Carly Ho 2023-06-29 17:49:52 -05:00 committed by GitHub
parent e4e8b2d546
commit bd6c7360db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 194 additions and 0 deletions

3
README.md Normal file
View File

@ -0,0 +1,3 @@
# How To
Basically, you can drop this onto a php-enabled webserver and have it Just Work, with one caveat. You have to have ImageMagick installed along with the php Imagick module.

19
config.php Normal file
View File

@ -0,0 +1,19 @@
<?php
// Change the second argument for better database security so that
// only you can enter work records
define("FORM_PAGE", "addwork");
// Database location; you can change it if you want
define("DB_LOCATION", "./kudos.db");
// Something to show in page titles. You could change this to "Kudos for [My Name]", etc.
define("SITE_NAME", "Kudos");
// Text Drawing Settings
$id = new ImagickDraw();
$id->setFont("./fonts/open-sans.ttf");
$id->setFontSize(16);
$id->setStrokeOpacity(1);
$id->setStrokeColor("#000000");
$id->setFillColor("#000000");

8
db.php Normal file
View File

@ -0,0 +1,8 @@
<?php
require_once('./config.php');
class KudosDB extends SQLite3 {
function __construct() {
$this->open(DB_LOCATION);
}
}

BIN
fonts/open-sans.ttf Normal file

Binary file not shown.

9
form.php Normal file
View File

@ -0,0 +1,9 @@
<form method="POST" action="kudos.php">
<div class="form-row">
<label for="newwork">ID of New Work</label>
<input name="newwork" type="text" required />
</div>
<div class="form-row">
<button type="submit">Add</button>
</div>
</form>

BIN
kudos.db Normal file

Binary file not shown.

75
kudos.php Normal file
View File

@ -0,0 +1,75 @@
<?php
require_once('./config.php');
require_once('./db.php');
// Open database
$db = new KudosDB();
// Get the user's IP address and the work ID, whatever it is
$ip = $_SERVER['REMOTE_ADDR'];
if (array_key_exists('give', $_GET)) {
// We're giving kudos
// the work ID is a text string so it can be a number, it can be an abbreviation,
// it just can't have weird characters in it that would fuck up the database
$work = filter_var($_GET['give'], FILTER_SANITIZE_STRING);
// check if the work exists so people can't fill the db full of junk
$exists = $db->query("select count(*) from works where id={$work};");
// check if the IP address has already left kudos
$prevkudos = $db->query("select count(*) from kudos where workid={$work} and ipaddr={$ip};");
if ($exists > 0 && $prevkudos == 0) {
$db->query("insert into kudos (workid, ipaddr) values ({$work}, {$ip});");
}
} else if (array_key_exists('show', $_GET)) {
// We're showing the number of kudos
$work = filter_var($_GET['show'], FILTER_SANITIZE_STRING);
$count = $db->query("select count(*) from works where id={$work};");
// creates a new image
$image = new Imagick();
// writes the number
$id->annotation(0, 0, $count);
$image->newImage(50, 300, "none");
$image->setImageFormat("png");
$image->drawImage($id);
$image->trimImage(0);
// serves this as an image
header("Content-Type: image/png");
echo $imagick->getImageBlob();
} else if (array_key_exists(FORM_PAGE, $_GET)) {
// We're showing a simple form for creating new works
$content = file_get_contents("./form.php");
require("./template.php");
die(1);
} else if (array_key_exists('newwork', $_POST)) {
$work = filter_var($_POST['newwork'], FILTER_SANITIZE_STRING);
// check if the work exists already since we can't do duplicates
$exists = $db->query("select count(*) from works where id={$work};");
$content = "";
if ($exists == 0) {
// it doesn't already exist, so add it to the database
$result = $db->query("insert into works (id) values ('{$work}');");
// show a response with a message that tells us if it worked or not
if ($result) {
$content = "<p>Your new work has been added! The work ID is {$work}.</p>";
$content .= "<p>Link to give kudos: <a href=\"//{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}?give={$work}\">//{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}?give={$work}</a></p>";
$content .= "<p>Link to kudos counter: <a href=\"//{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}?show={$work}\">//{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}?show={$work}</a></p>";
$content .= file_get_contents("./form.php");
} else {
$content = "<p class='error'>Whoops! It seems like something went wrong; try again.</p>";
$content .= file_get_contents("./form.php");
}
} else {
$content = "<p class='error'>You've already got a work by this name; try a different ID.</p>";
$content .= file_get_contents("./form.php");
}
require("./template.php");
die(1);
}

64
style.css Normal file
View File

@ -0,0 +1,64 @@
* {
box-sizing: border-box;
}
body {
background-color: #DDD;
color: #111;
display: flex;
align-items: center;
justify-content: center;
padding: 50px;
font-family: "Open Sans", sans-serif;
font-size: 16px;
}
#container {
background-color: white;
padding: 20px;
border: 1px #111 solid;
display: block;
max-width: 100%;
width: 600px;
text-align: left;
}
h1 {
font-size: 1.5em;
font-weight: normal;
border-bottom: 1px #111 solid;
padding-bottom: 5px;
margin-bottom: 5px;
margin-top: 0;
}
form {
margin: 0;
}
.form-row {
margin: 5px 0;
}
label {
display: block;
font-weight: bold;
font-size: 1em;
}
input[type="text"] {
border: 1px #666 solid;
background-color: white;
padding: 2px;
font-size: 1em;
}
button[type="submit"] {
font-family: "Open Sans", sans-serif;
font-size: 1em;
border: none;
background-color: #111;
color: #DDD;
padding: 3px 15px;
border-radius: 15px;
}

16
template.php Normal file
View File

@ -0,0 +1,16 @@
<?php // you can edit this file if you want to make it fit in with your site etc. ?>
<!doctype html>
<html lang="en">
<head>
<title><?= SITE_NAME; ?></title>
<link rel="stylesheet" type="text/css" href="./style.css" />
</head>
<body>
<div id="container">
<h1><?= SITE_NAME; ?></h1>
<div class="response">
<?= $content; ?>
</div>
</div>
</body>
</html>