Last active
August 29, 2015 14:25
-
-
Save mbeall/ecd22c5aa0fbb421f480 to your computer and use it in GitHub Desktop.
fcc-signup extras class
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_flag display_startup_errors on | |
php_flag display_errors on | |
php_flag html_errors on | |
php_flag log_errors on | |
RewriteEngine On | |
RewriteRule ^extra/([^/]*)$ /fcc-signup/extras.php?id=$1 [L] |
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 | |
define('DB_USER', ''); | |
define('DB_PASSWORD', ''); | |
define('DB_HOST', ''); | |
define('DB_NAME', ''); |
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 | |
header('Content-Type: application/json'); | |
include_once('config.php'); | |
include_once('inc/class-fccdb.php'); | |
include_once('inc/class-extra.php'); | |
include_once('functions.php'); | |
$fccdb = new fccdb; | |
if ( !empty( $_REQUEST['method'] ) ) { | |
$method = _method( $_REQUEST['method']); | |
} | |
else { | |
$method = 'get'; | |
} | |
if ( !empty( $_REQUEST['id'] ) ) { | |
$extra_id = (int) $_REQUEST['id']; | |
} | |
else { | |
$extra_id = rand(1,25); | |
} | |
$extra_id = min($extra_id,25); | |
$extra_id = max($extra_id,0); | |
switch ($method) { | |
case 'get' : | |
$extra = Extra::get_instance( $extra_id ); | |
echo json_encode($extra, JSON_PRETTY_PRINT); | |
break; | |
default: | |
break; | |
} |
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 | |
/** | |
* Functions | |
* | |
* All function definitions for the site | |
*/ | |
/** | |
* Sanitize text input and trim to size | |
* | |
* First, make sure only numbers and letters are used. | |
* Next, if length is specificied, trim to length. | |
* | |
* @param string $text The string to sanitize | |
* @param int $length The length of the string | |
* @return string | |
* @var string $new The sanitized string | |
*/ | |
function _text( $text, $length = 0 ) { | |
$new = preg_replace( '[^0-9a-fA-F]', '', $text); | |
$length = (int) $length; | |
if ( $length != 0 ) | |
return substr($new, 0, $length); | |
else | |
return $new; | |
} | |
/** | |
* Sanitize text input and trim to size | |
* | |
* First, make sure only numbers and letters are used. | |
* Next, if length is specificied, trim to length. | |
* | |
* @param string $text The string to sanitize | |
* @param int $length The length of the string | |
* @return string | |
* @var string $new The sanitized string | |
*/ | |
function _method( $text ) { | |
$new = preg_replace( '[^0-9a-fA-F]', '', $text); | |
if ('get' === $new || 'set' === $new || 'new' === $new) { | |
return $new; | |
} | |
else { | |
return false; | |
} | |
} |
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 | |
/** | |
* Defines fccdb class and related functions | |
* | |
* @author Matt Beall <me@rams.colostate.edu> | |
*/ | |
/** | |
* fccdb class | |
* | |
* Connects to database and creates object. | |
* | |
* @author Matt Beall | |
* @since 0.2.0 | |
*/ | |
class fccdb { | |
/** | |
* Connect to database | |
* | |
* @since 0.2.0 | |
* | |
* @param string $dbuser The user connecting to the database | |
* @param string $dbpassword The password for the user connecting to the database | |
* @param string $dbhost The host of the database (i.e. 'localhost') | |
* | |
* @return object PHP Data Object | |
* | |
* @var object $conn PHP Data Object | |
*/ | |
function connect( $dbuser = DB_USER, $dbpassword = DB_PASSWORD, $dbhost = DB_HOST, $dbname = DB_NAME ) { | |
$dbname = empty($dbname) ? $this->dbname : $dbname; | |
$dbuser = empty($dbuser) ? $this->dbuser : $dbuser; | |
$dbpassword = empty($dbpassword) ? $this->dbpassword : $dbpassword; | |
$dbhost = empty($dbhost) ? $this->dbhost : $dbhost; | |
$dbname = empty($dbname) ? $this->dbname : $dbname; | |
$conn = new PDO('mysql:host='.$dbhost.';dbname='.$dbname, $dbuser, $dbpassword, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); | |
return $conn; | |
} | |
/** | |
* Execute query | |
* | |
* Attempt to connect to database and execute SQL query | |
* If successful, return results. | |
* | |
* @since 0.0.1 | |
* | |
* @uses fccdb::connect() | |
* @throws PDOException if connection or query cannot execute | |
* | |
* @param string $query The SQL query to be executed | |
* @return object|array stdClass object or array of stdClass objects containing data from | |
* @var string $conn The PHP Data Object | |
*/ | |
function query( $query ) { | |
$conn = $this->connect(); | |
try { | |
$query = $conn->query($query); | |
do { | |
if ($query->columnCount() > 0) { | |
$results = $query->fetchAll(PDO::FETCH_OBJ); | |
} | |
} | |
while ($query->nextRowset()); | |
$conn = null; | |
return $results; | |
} | |
catch (PDOException $e) { | |
$conn = null; | |
die ('Query failed: ' . $e->getMessage()); | |
} | |
} | |
/** | |
* Execute select statement | |
* | |
* Build a SQL select statement, and execute the statement | |
* | |
* @since 0.0.1 | |
* | |
* @uses fccdb::query() | |
* | |
* @param string $table The database table to query | |
* @param string $columns The columns or data fields to query from the table | |
* @param string $match Search condition for row | |
* @param array $args Additional, optional parameters (see below) | |
* | |
* @return array Data results | |
* @var string $query The select statement to be executed | |
*/ | |
function select( $table, $columns = '*', $match = NULL, $args = array() ) { | |
/** | |
* Default parameters for select statement | |
* | |
* @param string $groupby Group by expression | |
* @param string $having Search condition for group | |
* @param string $orderby Order expression | |
* @param string $order Ascending or descending ('ASC' or 'DESC') | |
*/ | |
$defaults = array( | |
'groupby' => '', | |
'having' => '', | |
'orderby' => '', | |
'order' => 'ASC', | |
); | |
/** | |
* Parse connection arguments | |
*/ | |
$args = array_merge( $defaults, $args ); | |
/** | |
* Build the query | |
*/ | |
$query = ''; | |
$query .= 'SELECT ' . $columns; | |
$query .= ' FROM ' . $table; | |
$query .= !empty($match) ? ' WHERE ' . $match : ''; | |
$query .= !empty($args->groupby) ? ' GROUP BY ' . $args->groupby : ''; | |
$query .= !empty($args->having) ? ' HAVING ' . $args->having : ''; | |
$query .= !empty($args->orderby) ? ' ORDER BY ' . $args->orderby . ' ' . $args->order : ''; | |
$query .= ';'; | |
/** | |
* Execute the query | |
*/ | |
$results = $this->query($query); | |
return $results; | |
} | |
/** | |
* Insert data into the database | |
* | |
* Build a SQL insert statement, and execute the statement | |
* | |
* @since 0.0.1 | |
* | |
* @uses fccdb::query() | |
* | |
* @param string $table The database table that the data will be inserted into | |
* @param string $columns The columns, delimited by commas, that specifies which data will be inserted | |
* @param array $values A one-dimensional array of comma-separated values to be inserted into the database | |
* | |
* @return void | |
* @var string $query The insert statement to be executed | |
* | |
* @todo Change $values to sanitize input and not require strings to be in quotes | |
*/ | |
function insert( $table, $columns, $values ) { | |
/** | |
* Build the query | |
*/ | |
$query = ''; | |
$query .= 'INSERT INTO ' . $table . ' (' . $columns . ')'; | |
$query .= ' VALUES (' . $values . ')'; | |
$query .= ';'; | |
/** | |
* If there are multiple rows, make sure they are comma-separated | |
*/ | |
$query = preg_replace('/\)\(/', '\), \(', $query); | |
/** | |
* Execute the query | |
*/ | |
$results = $this->query($query); | |
return $results; | |
} | |
/** | |
* Update data in database | |
* | |
* Build a SQL update statement, and execute the statement | |
* | |
* @since 0.0.1 | |
* | |
* @uses fccdb::query() | |
* | |
* @param string $table The table where the data will be updated | |
* @param string $new The column name and new value (i.e. "name = 'Bob'") | |
* @param string $match The search condition to limit which rows are updated | |
* | |
* @return void | |
* @var string $query The update statement to be executed | |
* | |
* @todo Change $new to allow multi-dimensional array input | |
*/ | |
function update( $table, $new, $match ) { | |
/** | |
* Build the query | |
*/ | |
$query = ''; | |
$query .= 'UPDATE ' . $table; | |
$query .= ' SET ' . $new; | |
$query .= ' WHERE ' . $match; | |
$query .= ';'; | |
/** | |
* Execute the query | |
*/ | |
$results = $this->query($query); | |
return $results; | |
} | |
} |
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 | |
/** | |
* Defines class Extra and related functions | |
* | |
* @author Matt Beall | |
*/ | |
/** | |
* Extra class | |
* | |
* Connects to database and creates extra object. | |
* | |
* @author Matt Beall | |
* @since 0.2.0 | |
*/ | |
class Extra { | |
/** | |
* @var int $extr_id The ID of the extra | |
*/ | |
public $extr_id; | |
/** | |
* @var string $extr_name The name of the extra | |
*/ | |
public $extr_name = ''; | |
/** | |
* @var float $extr_cost The variable cost of the extra | |
*/ | |
public $extr_cost = 0.00; | |
/** | |
* @var string $extr_desc The description of the extra | |
*/ | |
public $extr_desc = ''; | |
/** | |
* Construct Extra object | |
* | |
* Takes PDO and constructs Extra class | |
* | |
* @since 0.0.4 | |
* | |
* @param object $extras The PHP Data Object | |
*/ | |
public function __construct( $extras ) { | |
foreach ( $extras as $extra ) { | |
get_class($extra); | |
foreach ( $extra as $key => $value ) | |
$this->$key = $value; | |
} | |
} | |
/** | |
* Execute query | |
* | |
* Attempt to connect to database and execute SQL query | |
* If successful, return results. | |
* | |
* @since 0.0.4 | |
* | |
* @uses fccdb::connect() | |
* @throws PDOException if connection or query cannot execute | |
* | |
* @param string $query The SQL query to be executed | |
* @return object Data retrieved from database | |
* @var string $conn The PHP Data Object | |
*/ | |
public static function query( $query ) { | |
global $fccdb; | |
$conn = $fccdb->connect(); | |
try { | |
$query = $conn->query($query); | |
do { | |
if ($query->columnCount() > 0) { | |
$results = $query->fetchAll(PDO::FETCH_OBJ); | |
} | |
} | |
while ($query->nextRowset()); | |
$conn = null; | |
return $results; | |
} | |
catch (PDOException $e) { | |
$conn = null; | |
die ('Query failed: ' . $e->getMessage()); | |
} | |
} | |
/** | |
* Get extra information from database | |
* | |
* Prepare and execute query to select extra from database | |
* | |
* @since 0.0.4 | |
* | |
* @uses self::query() | |
* | |
* @param int $extr_id The primary key of the extra being retrieved from the database | |
* @return object Data retrieved from database | |
* @var string $conn The PHP Data Object for the connection | |
*/ | |
public static function get_instance( $extr_id ) { | |
global $fccdb; | |
$extr_id = (int) $extr_id; | |
if ( ! $extr_id ) | |
return false; | |
$_extra = self::query("SELECT * FROM extras WHERE extr_id = $extr_id LIMIT 1"); | |
return new Extra ( $_extra ); | |
} | |
/** | |
* Insert extra in database | |
* | |
* Prepare and execute query to create extra in extras table | |
* | |
* @since 0.0.4 | |
* | |
* @uses fccdb::insert() | |
* @uses _text() | |
* | |
* @param string $extr_name The name of the extra | |
* @param float $extr_cost The variable cost of the extra | |
* @param string $extr_desc The description of the extra | |
* | |
* @return void | |
* | |
* @var int $extr_id The primary key of the extra being registered, as created in extra database | |
* | |
* @todo Test | |
*/ | |
public static function new_instance( $extr_name, $extr_cost = null, $extra_description = null ) { | |
global $fccdb; | |
$extr_name = _text( $extr_name, 32 ); | |
$extr_cost = !empty($extr_cost) ? floatval($extr_cost) : '777777'; | |
$extr_desc = _text( $extr_desc, 32 ); | |
$fccdb->insert('extras', 'extr_name,extr_cost,extr_desc', "'$extr_name', $extr_cost, '$extr_desc'" ); | |
} | |
/** | |
* Update extra in database | |
* | |
* Prepare and execute query to create extra in extras table | |
* | |
* @since 0.2.0 | |
* | |
* @uses fccdb::insert() | |
* @uses _text() | |
* | |
* @param int $extr_id The ID of the extra to update | |
* @param string $extr_name The name of the extra | |
* @param float $extr_cost The variable cost of the extra | |
* @param string $extr_desc The description of the extra | |
* | |
* @return void | |
* | |
* @var int $extr_id The primary key of the extra being registered, as created in extra database | |
* | |
* @todo Test | |
*/ | |
public static function set_instance( $extr_id, $extr_name = null, $extr_cost = null, $extr_desc = null ) { | |
global $fccdb; | |
$extr_id = (int) $extr_id; | |
$_extra = self::get_instance( $extr_id ); | |
$extr_name = !empty($extr_name) ? _text( $extr_name, 32 ) : $_extra->extr_name; | |
$extr_cost = !empty($extr_cost) ? floatval($extr_cost) : $_extra->extr_cost; | |
$extr_desc = !empty($extr_desc) ? _text( $extr_desc, 32 ) : $_extra->extr_desc; | |
$fccdb->update('extras', 'extr_name,extr_cost,extr_desc', "'$extr_name', $extr_cost, '$extr_desc'", "extr_id = $extr_id" ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment