Skip to content

Instantly share code, notes, and snippets.

@jcinis
Last active December 14, 2015 15:59
Show Gist options
  • Save jcinis/5111689 to your computer and use it in GitHub Desktop.
Save jcinis/5111689 to your computer and use it in GitHub Desktop.
Quick and dirty email signup in php and sqlite
<?php
class EmailSignup {
private $db;
// defaults can be overwritten on instantiation
private $filename = "emailsignups.sqlite";
private $tablename = "emails";
function __construct($filename = '', $tablename = ''){
// Check for changed filename or tablename
if($filename != '') $this->filename = $filename;
if($tablename != '') $this->tablename = $tablename;
// instantiate sqlite connection
if ($db = new SQLiteDatabase($this->filename)) {
// Check for existing table
$sql = "SELECT name FROM sqlite_master WHERE type='table' AND name='".$this->tablename."'";
$query = $db->query($sql);
// Create email table if it doesn't exist
if(!$result = $query->numRows()){
$sql = "CREATE TABLE ".$this->tablename." (email CHAR(255) PRIMARY KEY, name CHAR(255), created DATETIME)";
$query = $db->query($sql);
}
// Set instance database connection
$this->db = $db;
}
}
function subscribe($email,$name=""){
$sql = 'INSERT INTO '.$this->tablename.' VALUES ("'.$email.'","'.$name.'", DATETIME("now"))';
$query = @$this->db->queryExec($sql,$error);
if($query) {
return true;
}
return $rtn;
}
function unsubscribe($email){
$sql = 'DELETE FROM '.$this->tablename.' WHERE email="'.$email.'" LIMIT 1';
$query = $this->db->queryExec($sql,$error);
if($query){
return true;
}
return false;
}
function getList(){
$sql = 'SELECT * FROM '.$this->tablename;
$query = $this->db->query($sql);
$result = $query->fetchAll(SQLITE_ASSOC);
return $result;
}
function getByEmail($email){
$sql = 'SELECT * FROM '.$this->tablename.' WHERE email = "'.$email.'"';
$query = $this->db->query($sql);
if($query->numRows() > 0){
return $query->fetchAll(SQLITE_ASSOC);
}
return false;
}
function formatName($name){ return trim($name); }
function formatEmail($email){ return strtolower(trim($email)); }
/**
* Uber validation method found at:
* http://www.linuxjournal.com/article/9585?page=0,3
*
*/
function validEmail($email, $checkDNS=false) {
$isValid = true;
$atIndex = strrpos($email, "@");
if (is_bool($atIndex) && !$atIndex) {
$isValid = false;
} else {
$domain = substr($email, $atIndex+1);
$local = substr($email, 0, $atIndex);
$localLen = strlen($local);
$domainLen = strlen($domain);
if ($localLen < 1 || $localLen > 64) {
// local part length exceeded
$isValid = false;
} else if ($domainLen < 1 || $domainLen > 255) {
// domain part length exceeded
$isValid = false;
} else if ($local[0] == '.' || $local[$localLen-1] == '.') {
// local part starts or ends with '.'
$isValid = false;
} else if (preg_match('/\\.\\./', $local)) {
// local part has two consecutive dots
$isValid = false;
} else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)) {
// character not valid in domain part
$isValid = false;
} else if (preg_match('/\\.\\./', $domain)) {
// domain part has two consecutive dots
$isValid = false;
} else if (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\","",$local))) {
// character not valid in local part unless
// local part is quoted
if (!preg_match('/^"(\\\\"|[^"])+"$/', str_replace("\\\\","",$local))) {
$isValid = false;
}
}
if($checkDNS) {
if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A"))) {
//domain not found in DNS
$isValid = false;
}
}
}
return $isValid;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment