From f702886c454d13fa50cd43feac9135951778f277 Mon Sep 17 00:00:00 2001 From: Lncvrt Date: Fri, 7 Mar 2025 23:44:25 -0700 Subject: [PATCH] Initial commit --- database.sql | 60 ++++++++++++++++++ webroot/config/captcha.php | 3 + webroot/config/connection.php | 6 ++ webroot/incl/db.php | 15 +++++ webroot/index.php | 116 ++++++++++++++++++++++++++++++++++ webroot/upload.php | 79 +++++++++++++++++++++++ 6 files changed, 279 insertions(+) create mode 100644 database.sql create mode 100644 webroot/config/captcha.php create mode 100644 webroot/config/connection.php create mode 100644 webroot/incl/db.php create mode 100644 webroot/index.php create mode 100644 webroot/upload.php diff --git a/database.sql b/database.sql new file mode 100644 index 0000000..64097a8 --- /dev/null +++ b/database.sql @@ -0,0 +1,60 @@ +-- phpMyAdmin SQL Dump +-- version 5.2.2 +-- https://www.phpmyadmin.net/ +-- +-- Host: 127.0.0.1:3306 +-- Generation Time: Mar 08, 2025 at 06:43 AM +-- Server version: 11.4.3-MariaDB-1 +-- PHP Version: 8.3.11 + +SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; +START TRANSACTION; +SET time_zone = "+00:00"; + + +/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; +/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; +/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; +/*!40101 SET NAMES utf8mb4 */; + +-- +-- Database: `nameacolor` +-- + +-- -------------------------------------------------------- + +-- +-- Table structure for table `colors` +-- + +CREATE TABLE `colors` ( + `id` int(11) NOT NULL, + `name` text NOT NULL, + `color` char(6) NOT NULL, + `timestamp` int(11) NOT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci; + +-- +-- Indexes for dumped tables +-- + +-- +-- Indexes for table `colors` +-- +ALTER TABLE `colors` + ADD PRIMARY KEY (`id`); + +-- +-- AUTO_INCREMENT for dumped tables +-- + +-- +-- AUTO_INCREMENT for table `colors` +-- +ALTER TABLE `colors` + MODIFY `id` int(11) NOT NULL AUTO_INCREMENT; +COMMIT; + +/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; +/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; +/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; diff --git a/webroot/config/captcha.php b/webroot/config/captcha.php new file mode 100644 index 0000000..14429aa --- /dev/null +++ b/webroot/config/captcha.php @@ -0,0 +1,3 @@ + PDO::ERRMODE_EXCEPTION, + PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, + ]; + $conn = new PDO($dsn, $dbusername, $dbpass, $options); + } catch (PDOException $e) { + die("Failed to connect to database: " . $e->getMessage()); + } + return $conn; +} \ No newline at end of file diff --git a/webroot/index.php b/webroot/index.php new file mode 100644 index 0000000..19ebf14 --- /dev/null +++ b/webroot/index.php @@ -0,0 +1,116 @@ +prepare("SELECT * FROM colors"); +$stmt->execute(); +$result = $stmt->fetchAll(PDO::FETCH_ASSOC); + +usort($result, function($a, $b) { + return strtotime($b['timestamp']) - strtotime($a['timestamp']); +}); + +$lastInsertId = $_GET['highlight'] ?? null; +?> + + + + + + Name A Color + + + + + + + + +

Name A Color!

+
+

Name your own color!

+
+

This color has already been named!

+ + + + + +
+
+
+

Colors that were named

+ "; + echo "
"; + echo "

" . htmlspecialchars(base64_decode($row['name'])) . " • #". $row["color"] ."

"; + echo ""; + } + ?> + + \ No newline at end of file diff --git a/webroot/upload.php b/webroot/upload.php new file mode 100644 index 0000000..dad2829 --- /dev/null +++ b/webroot/upload.php @@ -0,0 +1,79 @@ + $secretkey, + 'response' => $token, + 'remoteip' => $ip, + ]; + + $ch = curl_init($url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); + + $result = curl_exec($ch); + curl_close($ch); + + if ($result !== false) { + $outcome = json_decode($result, true); + + if (!isset($outcome['success']) || !$outcome['success']) { + echo "Invalid Captcha"; + exit; + } + } else { + echo "Couldn't validate Captcha"; + exit; + } + + $conn = connectToDatabase(); + + $color = $_POST['color']; + $name = base64_encode($_POST['name'] ?? ''); + + $color = ltrim($color, '#'); + if (!preg_match('/^[a-f0-9]{6}$/i', $color)) { + echo "Invalid color"; + exit; + } + + if (strlen($name) < 3) { + echo "Name too short"; + exit; + } + + if (strlen($name) > 50) { + echo "Name too long"; + exit; + } + + $stmt = $conn->prepare("SELECT name FROM colors WHERE color = :color"); + $stmt->bindParam(':color', $color); + $stmt->execute(); + + if ($stmt->rowCount() > 0) { + echo "Color already taken"; + exit; + } + + $stmt = $conn->prepare("INSERT INTO colors (color, name, timestamp) VALUES (:color, :name, :time)"); + $stmt->bindParam(':color', $color); + $stmt->bindParam(':name', $name); + $stmt->bindParam(':time', $time); + $stmt->execute(); + $lastInsertId = $conn->lastInsertId(); + header("Location: /?highlight=$lastInsertId"); + exit; +} + +header("Location: /"); \ No newline at end of file