Skip to content

Instantly share code, notes, and snippets.

@leekiernan
Created April 24, 2013 15:11
Show Gist options
  • Save leekiernan/5452880 to your computer and use it in GitHub Desktop.
Save leekiernan/5452880 to your computer and use it in GitHub Desktop.
<?php
date_default_timezone_set('UTC');
class Form {
protected $dbh, $db_table;
protected $required_data, $received_data, $errors;
public $email_body, $email_template, $email_to, $email_subject, $email_from;
public function __construct( $connection_data = null ) {
if( isset($connection_data) ) {
$this->db_connect( $connection_data );
}
}
public function required_data( $array ) {
if( isset($array) ) {
if( !is_array($array) ) {
// Convert CSV.
$array = explode( ",", $array);
}
return $this->required_data = $array;
}
else {
return $this->required_data;
}
}
public function check_required_data($array) {
if( !isset($this->required_data) ) {
return false;
}
foreach( $this->required_data as $f ) {
// Ensure it is passed in (from config) and the value exists(from form)
// fromformfromformfromform
// print_r( $array );
if( !in_array($f, array_keys($array)) ||
!isset($array[$f]) ||
$array[$f] == null ) {
$this->errors[] .= "Please complete $f field.";
}
}
if( sizeof($this->errors) > 0 ) {
foreach( $this->errors as $e ) { echo "$e\n"; }
return false;
} else {
return true;
}
}
public function db_connect( $connect ) {
// TODO: sqlite no uname/pw
if( !isset($connect['type']) ||
!isset($connect['location']) ||
!isset($connect['dbname']) ||
!isset($connect['username']) ||
!isset($connect['password']) )
{
exit( "Required connection data: type, location, name, username, password" );
}
if( isset($connect['table']) ) {
$this->db_table = $connect['table'];
}
try {
$this->dbh = new PDO( $connect['type'] .
(isset($connect['location']) ? ":host=". $connect['location'] : null) .
(isset($connect['dbname']) ? ";dbname=". $connect['dbname'] : null),
$connect['username'], $connect['password'] );
$this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
// Throw error?
exit( "Failed to connect to database. Please try again later." );
return false;
}
return true;
}
public function add_to_db( $array ) {
if( !isset($this->dbh) ) {
exit("Cannot find database connection. Please re-instantiate.");
}
if( !isset($array) ) {
exit( "column and value data required: array( key => value, key => value )" );
}
$columns = implode( ", ", array_keys( $array ));
foreach( $array as $key => $value ) {
// Replace key with PDO friendly lookup.
array_shift($array);
$array[":$key"] = $value;
}
$values = implode( ", ", array_keys($array) );
try {
$query = $this->dbh->prepare( "INSERT INTO $this->db_table ($columns) VALUES ($values)" );
$query->execute( $array );
}
catch(PDOException $e) {
return 'database';
}
return "success";
}
public function query_db() { }
public function email_body( $string ) {
return ( isset($string) ? $this->email_body = $string : $this->email_body );
}
public function email_template( $file ) {
return ( isset($string) ? $this->email_template = $string : $this->email_template );
}
public function email_to( $string ) {
return ( isset($string) ? $this->email_to = $string : $this->email_to );
}
public function email_subject( $string ) {
return ( isset($string) ? $this->email_subject = $string : $this->email_subject );
}
public function email_from( $string ) {
return ( isset($string) ? $this->email_from = $string : $this->email_from );
}
public function send_email( $array = null ) {
// $params = func_get_args();
( isset($array["email_to"]) ? $this->email_to = $array["email_to"] : null );
( isset($array["email_template"]) ? $this->email_template = $array["email_template"] : null );
( isset($array["email_body"]) ? $this->email_body = $array["email_body"] : null );
( isset($array["email_subject"]) ? $this->email_subject = $array["email_subject"] : null );
if( !isset( $this->email_to ) || !isset( $this->email_body ) || !isset( $this->email_subject ) ) {
exit( "Required data missing:: email_to = $this->email_to / email_body = $this->email_body / email_subject = $this->email_subject" );
}
$from = ( isset($this->email_from) ? $this->email_from : $this->email_to );
$header = 'MIME-VERSION: 1.0' ."\r\n".
'Content-type: text/html; charset=iso-8859-1' . "\r\n" .
'From:'. $this->email_to ."\r\n" .
'Reply-To:'. $from ."\r\n";
ini_set( 'sendmail_from', $from );
mail( $this->email_to, $this->email_subject, $this->email_body, $header, '-f'. $from );
return true;
}
public function upload_file( $file, $path, $salt ) {
// Unchecked.
$accept = array( "application/msword","application/doc","application/vnd.openxmlformats-officedocument.wordprocessingml.document","application/pdf","application/x-pdf","application/acrobat","applications/vnd.pdf","text/pdf","text/x-pdf","application/x-iwork-pages-sffpages","text/plain","text/richtext","text/rtf","application/rtf","application/richtext" );
preg_match( "/([^.]+)$/i", $file["file"]['name'], $ext );
$save_path = $path . hash('sha256', $salt) .'.'. $ext[1];
if( !in_array($file['type'], $accept) ) {
exit( "Uploaded file cannot be accepted" );
}
if( !move_uploaded_file(
$file["tmp_name"],
$_SERVER['DOCUMENT_ROOT'] . $save_path) )
{
exit( "Upload failed.");
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment