setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); if(isset($_GET['action'])){ switch(strtolower($_GET['action'])){ case "get_all_events": $STH = $DBH->query("SELECT * FROM events WHERE archived==0"); $STH->setFetchMode(PDO::FETCH_CLASS, 'Event'); $json['events'] = array(); while($obj = $STH->fetch()) { $json['events'][] = $obj; } break; case "register_seat": if(!isset($_GET['event-id'])) throw new Exception("Event ID is missing."); if(!isset($_GET['email'])) throw new Exception("Email address is missing."); if(Seat::readDB($DBH, $_GET['event-id'], $_GET['email']) != FALSE) throw new Exception("You are already registered."); $event = Event::readDB($DBH, $_GET['event-id']); if($event == FALSE) throw new Exception("No such event."); else if($event->closed) throw new Exception("Event is closed. No new registrations allowed."); $seat = new Seat($_GET['event-id'], $_GET['email']); $seat->saveDB($DBH); sendVerificationMail( $seat ); break; case "verify_seat": if(!isset($_GET['hash'])) throw new Exception('Incomplete verification link.'); $STH = $DBH->prepare("SELECT * FROM seat WHERE verify_hash==:verify_hash"); $STH->setFetchMode(PDO::FETCH_CLASS, 'Seat'); $STH->bindParam(":verify_hash", $_GET['hash']); $STH->execute(); $seat = $STH->fetch(); if($seat != FALSE){ $seat->setVerified(); $seat->saveDB(); } else throw new Exception('Incorrect verification hash.'); break; default: throw new Exception('Unknown action'); } } # close the connection $DBH = null; } catch(Exception $e) { $json['error'] = $e->getMessage(); } // Encode JSON echo json_encode($json); //####################### CLASSES ########################### class Event { public $id; public $name; public $description; protected $seats; protected $backup_seats; public $closed; protected $archived; public $seats_available; public $backup_seats_available; function __construct(){ global $DBH; $this->closed = filter_var($this->closed, FILTER_VALIDATE_BOOLEAN); // Get available seats $registered = $DBH->query("SELECT count(*) FROM seats WHERE event_id==".$this->id)->fetchColumn(); $this->seats_available = max($this->seats - $registered, 0); $this->backup_seats_available = max( $this->backup_seats + $this->seats - $registered, 0); } static function readDB($DBH, $id){ $STH = $DBH->prepare("SELECT * FROM events WHERE id==:id"); $STH->setFetchMode(PDO::FETCH_CLASS, 'Event'); $STH->bindParam(":id", $id); $STH->execute(); return $STH->fetch(); } } class Seat { protected $id; public $event_id; public $date; public $email; protected $verify_hash; function __construct($event_id="", $email=""){ if(!empty($event_id) && !empty($email)){ $this->event_id = $event_id; $this->date = date('Y-m-d H:i:s'); $this->email = $email; $this->verify_hash = sha1("" . $this->event-id . $this->date . $this->email); } } function verified(){ return !empty($this->email) && empty($this->verify_hash); } function setVerified(){ $this->verify_hash = ""; $this->date = date('Y-m-d H:i:s'); } static function readDB($DBH, $event_id, $email){ $STH = $DBH->prepare("SELECT * FROM seats WHERE event_id==:event_id AND email==:email"); $STH->setFetchMode(PDO::FETCH_CLASS, 'Seat'); $STH->bindParam(":event_id", $event_id); $STH->bindParam(":email", $email); $STH->execute(); return $STH->fetch(); } function saveDB($DBH){ if(empty($id)) $STH = $DBH->prepare("INSERT INTO seats (event_id, date, email, verify_hash) VALUES (:event_id, :date, :email, :verify_hash)"); else{ $STH = $DBH->prepare("UPDATE seats SET event_id=:event_id, date=:date, email=:email, verify_hash=:verify_hash WHERE id=:id"); $STH->bindParam(":id", $this->id); } $STH->bindParam(":event_id", $this->event_id); $STH->bindParam(":date", $this->date); $STH->bindParam(":email", $this->email); $STH->bindParam(":verify_hash", $this->verify_hash); $STH->execute(); if(empty($id)) $this->id = $DBH->lastInsertId(); } } function sendMail($to, $subject = "(No subject)", $message = ""){ if (!defined('PHP_EOL')) define ('PHP_EOL', strtoupper(substr(PHP_OS,0,3) == 'WIN') ? "\r\n" : "\n"); $headers = array(); $headers[] = "MIME-Version: 1.0"; //$headers[] = "Content-type: text/plain; charset=iso-8859-1"; $headers[] = "Content-type: text/html; charset=iso-8859-1"; $headers[] = "From: Event Registration "; $headers[] = "Subject: {$subject}"; $headers[] = "X-Mailer: PHP/".phpversion(); mail($to, $subject, $message, implode(PHP_EOL, $headers)); } function sendVerificationMail( $seat ){ sendMail($seat->email , "Verify Registration", " Hi,
Thank you for showing interest in our event.
Note that your registration is not yet finished.

Please press the link below to confirm and finish your registration.
xx?action=verify_seat&hash=".$seat->verfy_hash."
The link will be valid for 24 hours, if you do not confirm within this time your registration will be discarded.

Best Regards " ); } $DB_SETUP = " CREATE TABLE `events` ( `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, `name` TEXT, `date` TEXT, `description` TEXT, `seats` INTEGER, `backup_seats` INTEGER, `closed` NUMERIC DEFAULT '0', `archived` NUMERIC DEFAULT '0' ); CREATE TABLE `seats` ( `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, `event_id` INTEGER NOT NULL, `date` TEXT, `email` TEXT NOT NULL, `verify_hash` TEXT, FOREIGN KEY(`event_id`) REFERENCES events ( id ) ); "; ?>