Created
November 19, 2015 14:13
-
-
Save remino/1ef5567180660514560d to your computer and use it in GitHub Desktop.
Simple Contact Form API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
# ContactFormAPI | |
# | |
# Simple script with PHP class to send via e-mail the details | |
# of a form submitted to it via HTTP POST. PHP 5.5+ required. | |
# | |
# <form action="/contact_form_api.php" method="POST" accept-charset="UTF-8"> | |
# <input name="utf8" type="hidden" value="✓"> | |
# <!-- ... --> | |
# </form> | |
# | |
# This is nothing big or intensive. Not everything was tested | |
# rigurously. There is no validation of the fields except for | |
# the redirect URL. No spam prevention. USE IT AT YOUR OWN RISK. | |
# | |
# License: MIT | |
class ContactFormAPI { | |
const EMAIL_TO = 'your.address@example.com'; | |
const REDIRECT_DOMAIN = 'example.com'; | |
const SUBJECT_DEFAULT = 'Contact Form'; | |
const SUBJECT_FORMAT = '[SITE] %s'; | |
const TIME_FORMAT = 'Y-m-d H:i:s e'; | |
const TIME_ZONE = 'GMT'; | |
function email_to() { return self::EMAIL_TO; } | |
function redirect_domain() { return self::REDIRECT_DOMAIN; } | |
function subject_default() { return self::SUBJECT_DEFAULT; } | |
function subject_format() { return self::SUBJECT_FORMAT; } | |
function time_format() { return self::TIME_FORMAT; } | |
function time_zone() { return self::TIME_ZONE; } | |
function __construct($fields = array()) { | |
$this->fields = $fields; | |
} | |
function current_time() { | |
$date = new DateTime('now', new DateTimeZone($this->time_zone())); | |
return $date->format($this->time_format()); | |
} | |
function mail() { | |
return mail($this->email_to(), $this->mail_subject(), | |
$this->mail_body()); | |
} | |
function mail_body() { | |
$output = ''; | |
foreach($this->fields as $key => $value) { | |
$output .= sprintf("%s:\n%s\n\n", $key, $value); | |
} | |
$output .= sprintf("IP:\n%s\n\n", $_SERVER['REMOTE_ADDR']); | |
$output .= sprintf("Referrer:\n%s\n\n", $_SERVER['HTTP_REFERER']); | |
$output .= sprintf("Time:\n%s\n\n", $this->current_time()); | |
return $output; | |
} | |
function mail_subject() { | |
$subject = $this->fields['subject'] ?: $this->subject_default(); | |
return sprintf($this->subject_format(), $subject); | |
} | |
function go_back() { | |
return $this->redirect($_SERVER['HTTP_REFERER']); | |
} | |
function go_forward() { | |
return $this->redirect($this->redirect_url()); | |
} | |
function redirect($url) { | |
if(!$this->validate_redirect_url($url)) return false; | |
header('Location: ' . $url); | |
} | |
function redirect_url() { | |
return $this->fields['redirect_url'] ?: $_SERVER['HTTP_REFERER']; | |
} | |
function respond() { | |
try { | |
$this->mail() ? $this->go_forward() : $this->go_back(); | |
} catch(Exception $e) { | |
return $this->response_error($e->getMessage()); | |
} | |
} | |
function response_error($msg) { | |
http_response_code(500); | |
header('Content-Type: application/json'); | |
echo json_encode(array('error' => $msg)); | |
} | |
function validate_redirect_url($url) { | |
$parsed = parse_url($url); | |
$domain = $parsed['host']; | |
if($domain != $this->redirect_domain()) { | |
throw new Exception("Forbidden redirection domain: " . $url); | |
return false; | |
} | |
return true; | |
} | |
} | |
if(php_sapi_name() != 'cli') { | |
$contact_form_api = new ContactFormAPI($_POST); | |
$contact_form_api->respond(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment