openConnection(); } function __get($name) { $out = ""; if( $name == "err" || $name == "bindVars" || $name == "sql" ) { $out = $this->$name; } return $out; } function __set($name,$value) { if( $name == "sql" || $name == "bindVars" ) { $this->$name = $value; } } private function setDatabaseProperties() { if( $_SERVER["SERVER_NAME"] == "comics.ken.com" ) { $this->server = "localhost"; $this->name = "keneriksen"; $this->user = "root"; $this->password = base64_decode("Z2R0MXJmYiYydQ=="); } else{ $this->server = "localhost"; $this->name = "keneriksen"; $this->user = "keneriksen"; $this->password = base64_decode("cHJlbTlzdWNrczE="); } } private function openConnection() { $this->err = ""; $this->setDatabaseProperties(); try{ if( $this->pdo = @new PDO("mysql:host=" . $this->server . ";dbname=" . $this->name . ";charset=utf8", $this->user, $this->password ) ) { $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); } } catch(PDOException $e) { $this->err = $e->getMessage(); } } private function closeConnection() { $this->pdo = null; } public function readData() { $records = array(); if( ! $this->err ) { $stmt; try{ if( $this->bindVars != null ) { $stmt = $this->pdo->prepare($this->sql); $stmt->execute($this->bindVars); $records = $stmt->fetchAll(PDO::FETCH_ASSOC); } else{ $stmt = $this->pdo->query($this->sql); $records = $stmt->fetchAll(PDO::FETCH_ASSOC); } } catch(PDOException $e) { $this->err = $e->getMessage(); } finally{ //$this->bindVars = null; $stmt = null; } } return $records; } public function writeData() { $insertId = 0; if( ! $this->err ) { if( $this->pdo->query($this->sql) ) { $insertId = $this->pdo->insert_id; } } return $insertId; } function __destruct() { $this->closeConnection(); } } /************************************************************************************************ ************************************************************************************************* ************************************************************************************************/ class DataHandler{ private $db; private $site; private $data; private $err; private $path_info; private $sql_select; private $sql_from; private $sql_where; private $sql_order; private $pag; private $action; private $comic_type_id; private $comic_publisher_id; private $comic_title; private $title_keyword_search; private $comic_title_id; private $comic_year; private $create_date; private $recent_additions_search; private $util; /********************************************************************************************************/ function __construct($site, $action, $path_info, $util) { $this->site = $site; $this->db = new Database(); $this->err = $this->db->err; $this->path_info = $path_info; $this->action = $action; $this->util = $util; if( empty($this->err) ) { //$this->recordPageView(); $this->$action(); } } /********************************************************************************************************/ function __get($name) { $out; if( $name == "data" || $name == "err" || $name == "pag" || $name == "comic_type_id" || $name == "comic_publisher_id" || $name == "comic_title" || $name == "title_keyword_search" || $name == "comic_title_id" || $name == "comic_year" || $name == "create_date" || $name == "recent_additions_search" ) { $out = $this->$name; } return $out; } /********************************************************************************************************/ private function recordPageView() { $pageview['DATE_TIME'] = date("Y-m-d H:i:s"); $pageview['REMOTE_ADDR'] = $_SERVER['REMOTE_ADDR']; $pageview["REMOTE_PORT"] = $_SERVER["REMOTE_PORT"]; $pageview['HTTP_VIA'] = $_SERVER['HTTP_VIA']; $pageview['HTTP_X_FORWARDED_FOR'] = $_SERVER['HTTP_X_FORWARDED_FOR']; $pageview['HTTP_CLIENT_IP'] = $_SERVER['HTTP_CLIENT_IP']; $pageview['HTTP_REFERER'] = $_SERVER['HTTP_REFERER']; $pageview['REQUEST_METHOD'] = $_SERVER['REQUEST_METHOD']; $pageview['DOMAIN'] = $_SERVER['DOMAIN']; $pageview['REQUEST_URI'] = $_SERVER['REQUEST_URI']; $pageview["HTTP_ORIGIN"] = $_SERVER["HTTP_ORIGIN"]; $pageview['HTTP_USER_AGENT'] = $_SERVER['HTTP_USER_AGENT']; $pageview['SERVER_ADDR'] = $_SERVER['SERVER_ADDR']; $pageview['POST_VARS'] = $_POST; $columns = ""; $values = ""; $maxlength = 0; foreach($pageview as $key=>$value) { if( $key == "POST_VARS" ) { if( count($value) > 0 ) { $newvalue = ""; foreach( $value as $akey=>$avalue ) { $newvalue .= $akey . "=" . trim($avalue) . ";"; } $value = $newvalue; } else{ $value = ""; } } if( trim($value) != "" ) { if( $columns != "" ) { $columns .= ","; } $columns .= $key; if( $values != "" ) { $values .= ","; } if( $key == "DATE_TIME" ) { $values .= "'" . $value . "'"; } else { if( $key == "REMOTE_ADDR" || $key == "REMOTE_PORT" || $key == "HTTP_VIA" || $key == "HTTP_X_FORWARDED_FOR" || $key == "HTTP_CLIENT_IP" || $key == "REQUEST_METHOD" || $key == "DOMAIN" || $key == "HTTP_ORIGIN" || $key == "SERVER_ADDR") { $maxlength = 255; } else if($key == "HTTP_REFERER" || $Key == "REQUEST_URI" || $key == "HTTP_USER_AGENT" ) { $maxlength = 2000; } else if( $key == "POST_VARS" ) { $maxlength = 5000; } $values .= "'" . str_replace("'","''", substr(trim($value), 0, $maxlength)) . "'"; } } } $db = new Database(); $this->db->sql = "INSERT INTO comic_pageviews(" . $columns . ") VALUES(" . $values . ")"; $this->db->writeData(); } /********************************************************************************************************/ private function comic() { $this->sql_select = "SELECT a.comic_id, a.comic_year, a.comic_volume, a.comic_issue, a.comic_cover_price, a.comic_comments, a.create_date, a.comic_hrn, b.comic_type, b.comic_type_id, c.comic_publisher, c.comic_publisher_id, d.comic_title, d.comic_title_id "; $this->sql_from = " FROM comics a, comic_types b, comic_publishers c, comic_titles d "; $this->sql_where = " WHERE a.comic_title_id = d.comic_title_id AND d.comic_type_id = b.comic_type_id AND d.comic_publisher_id = c.comic_publisher_id "; if( isset($this->path_info[2]) ) { $this->comic_title_id = $this->path_info[2]; } if( isset($this->path_info[3]) ) { $this->comic_type_id = $this->path_info[3]; } if( isset($this->path_info[4]) ) { $this->comic_publisher_id = $this->path_info[4]; } if( isset($this->path_info[5]) ) { $this->comic_year = $this->path_info[5]; } if( isset($this->path_info[6]) ) { $this->create_date = $this->path_info[6]; } if( strlen($this->comic_title_id) > 0 && is_numeric($this->comic_title_id) && $this->comic_title_id > 0 ) { $this->sql_where .= " AND a.comic_title_id = " . $this->comic_title_id; } else{ $this->comic_title_id = 0; } if( strlen($this->comic_type_id) > 0 && is_numeric($this->comic_type_id) && $this->comic_type_id > 0 ) { $this->sql_where .= " AND b.comic_type_id = " . $this->comic_type_id; } else{ $this->comic_type_id = 0; } if( strlen($this->comic_publisher_id) > 0 && is_numeric($this->comic_publisher_id) && $this->comic_publisher_id > 0 ) { $this->sql_where .= " AND c.comic_publisher_id = " . $this->comic_publisher_id; } else{ $this->comic_publisher_id = 0; } if( strlen($this->comic_year) > 0 ) { $aYears = explode("-",$this->comic_year); if( count($aYears) == 2 ) { if( is_numeric($aYears[0]) && is_numeric($aYears[1]) ) { if( $aYears[0] > 0 && $aYears[1] > 0 ) { if( $aYears[0] < $aYears[1] ) { $this->sql_where .= " AND a.comic_year >= " . $aYears[0] . " AND a.comic_year <= " . $aYears[1] . " "; } } } } else if( is_numeric($aYears[0]) && $aYears[0] > 0 ) { $this->sql_where .= " AND a.comic_year = " . $aYears[0]; } } else{ $this->comic_year = 0; } $this->create_date = base64_decode($this->create_date); if( strlen($this->create_date) > 0 ) { $dateParts = $this->util->getDateParts( $this->create_date ); $this->sql_where .= " AND ( DATE_FORMAT(a.create_date, '%Y') = '" . $dateParts['year'] . "' AND DATE_FORMAT(a.create_date, '%m') = '" . $dateParts['month'] . "' AND DATE_FORMAT(a.create_date, '%d') = '" . $dateParts['day'] . "' ) "; $this->recent_additions_search = true; } $this->sql_order .= " ORDER BY d.comic_title, c.comic_publisher, a.comic_volume, a.comic_issue"; $this->pagination_helper() ; $this->db->sql = $this->sql_select . " " . $this->sql_from . " " . $this->sql_where; $this->db->sql .= " " . $this->sql_order . " LIMIT " . ($this->pag['max_row_number']- $this->pag['max_records_to_return']) . "," . $this->pag['max_records_to_return']; $this->data = $this->db->readData(); if( count($this->data) > 0 && $this->pag['pagination_on'] ) { $this->pag['num_pages'] = ceil( $this->pag['total_records_available'] / $this->pag['max_records_to_return'] ); } } /********************************************************************************************************/ private function title() { $this->sql_select = "SELECT a.comic_title_id, a.comic_title, a.comic_type_id, a.comic_title_comments, a.series_complete, a.has_logo, b.comic_type, a.comic_publisher_id, c.comic_publisher, (SELECT COUNT(comic_id) FROM comics WHERE comic_title_id = a.comic_title_id) AS comic_count, (SELECT comic_id FROM comics WHERE comic_title_id = a.comic_title_id ORDER BY comic_issue LIMIT 1) AS lowest_comic_issue_id "; $this->sql_from = "FROM comic_titles a, comic_types b, comic_publishers c "; $this->sql_where = "WHERE a.comic_type_id = b.comic_type_id AND a.comic_publisher_id = c.comic_publisher_id "; if( isset($this->path_info[2]) ) { $this->comic_type_id = $this->path_info[2]; } if( isset($this->path_info[3]) ) { $this->comic_publisher_id = $this->path_info[3]; } if( isset($this->path_info[4]) ) { $this->comic_title = base64_decode($this->path_info[4]); $this->comic_title = str_replace("<", "<", $this->comic_title); $this->comic_title = str_replace(">", ">", $this->comic_title); } if( $this->comic_title != "null" && $this->comic_title != "" ) { if( $this->comic_title[0] == "~" ) { $this->title_keyword_search = true; } $this->comic_title = trim($this->comic_title); if( strlen($this->comic_title) > 0 ) { $kw = "%" . str_replace("~", "", $this->comic_title) . "%"; if( $this->title_keyword_search ) { $this->sql_where .= " AND (a.comic_title LIKE :comic_title "; $this->sql_where .= " OR a.comic_title_id IN (SELECT comic_title_id FROM comic_keyword_map WHERE keyword LIKE :comic_titleB )) "; $this->db->bindVars = array(":comic_title"=>$kw, ":comic_titleB"=>$kw); } else{ $this->sql_where .= " AND a.comic_title LIKE :comic_title "; $this->db->bindVars = array(":comic_title"=>$kw); } } } else{ $this->comic_title = "null"; } if( is_numeric($this->comic_type_id) && $this->comic_type_id > 0 ) { $this->sql_where .= " AND a.comic_type_id = " . $this->comic_type_id; } else{ $this->comic_type_id = 0; } if( is_numeric($this->comic_publisher_id) && $this->comic_publisher_id > 0 ) { $this->sql_where .= " AND a.comic_publisher_id = " . $this->comic_publisher_id; } else{ $this->comic_publisher_id = 0; } $this->sql_order .= " ORDER BY a.comic_title, a.comic_title_id "; $this->pagination_helper() ; $this->db->sql = $this->sql_select . " " . $this->sql_from . " " . $this->sql_where; $this->db->sql .= " " . $this->sql_order . " LIMIT " . ($this->pag['max_row_number']- $this->pag['max_records_to_return']) . "," . $this->pag['max_records_to_return']; $this->data = $this->db->readData(); if( count($this->data) > 0 && $this->pag['pagination_on'] ) { $this->pag['num_pages'] = ceil( $this->pag['total_records_available'] / $this->pag['max_records_to_return'] ); } } /********************************************************************************************************/ private function publisher() { $this->sql_select = "SELECT a.comic_publisher_id, a.comic_publisher, a.has_logo, (SELECT COUNT(*) FROM comic_titles WHERE comic_publisher_id = a.comic_publisher_id) AS title_count, ( SELECT COUNT(cc.comic_id) AS comic_count FROM comic_titles ct, comics cc WHERE ct.comic_title_id = cc.comic_title_id AND ct.comic_publisher_id = a.comic_publisher_id ) AS comic_count "; $this->sql_from = " FROM comic_publishers a "; $this->sql_order = " ORDER BY a.comic_publisher "; $this->pagination_helper() ; $this->db->sql = $this->sql_select . " " . $this->sql_from . " " . $this->sql_where; $this->db->sql .= " " . $this->sql_order . " LIMIT " . ($this->pag['max_row_number']- $this->pag['max_records_to_return']) . "," . $this->pag['max_records_to_return']; $this->data = $this->db->readData(); if( count($this->data) > 0 && $this->pag['pagination_on'] ) { $this->pag['num_pages'] = ceil( $this->pag['total_records_available'] / $this->pag['max_records_to_return'] ); } } /********************************************************************************************************/ private function pagination_helper() { $this->pag['max_records_to_return'] = 25; $this->pag['pagination_on'] = false; $this->pag['total_records_available'] = 0; $this->pag['current_page'] = 1; $this->pag['max_row_number'] = $this->pag['max_records_to_return']; $this->pag['num_pages'] = 0; $this->pag['num_page_links_to_display'] = 4; $this->pag['url'] = ""; if( isset($this->path_info[1] ) && is_numeric($this->path_info[1]) ) { $this->pag['current_page'] = ceil($this->path_info[1]); } if( $this->pag['current_page'] < 1 ) { $this->pag['current_page'] = 1; } $this->pag['url'] = $this->action . "/" . $this->pag['current_page']; if( $this->action == "title" ) { $this->pag['url'] .= "/" . $this->comic_type_id . "/" . $this->comic_publisher_id . "/" . base64_encode($this->comic_title); } else if( $this->action == "comic" ) { $this->pag['url'] .= "/" . $this->comic_title_id . "/" . $this->comic_type_id . "/" . $this->comic_publisher_id . "/" . $this->comic_year; if( strlen($this->create_date) > 0 ) { $this->pag['url'] .= "/" . base64_encode($this->create_date); } } if( $this->pag['current_page'] > 0 ) { $this->pag['max_row_number'] = $this->pag['current_page'] * $this->pag['max_records_to_return']; } $this->db->sql = "SELECT count(*) as resultcount " . $this->sql_from . $this->sql_where; $record = $this->db->readData(); if( count( $record ) > 0 ) { $this->pag['total_records_available'] = $record[0]['resultcount']; } if( $this->pag['total_records_available'] > 0 ) { if( $this->pag['total_records_available'] > $this->pag['max_records_to_return'] ) { $this->pag['pagination_on'] = true; } if( $this->pag['total_records_available'] < $this->pag['max_row_number'] ) { $this->pag['current_page'] = ceil($this->pag['total_records_available']/$this->pag['max_records_to_return']); $this->pag['max_row_number'] = ceil($this->pag['total_records_available']/$this->pag['max_records_to_return']) * $this->pag['max_records_to_return']; } } } /********************************************************************************************************/ private function home() { $this->db->sql = "SELECT a.create_date, a.comic_id, a.comic_title_id, a.comic_year, a.comic_cover_price, "; $this->db->sql .= "a.comic_volume, a.comic_issue, a.comic_hrn, b.comic_title, c.comic_publisher, c.comic_publisher_id "; $this->db->sql .= "FROM comics a, comic_titles b, comic_publishers c "; $this->db->sql .= "WHERE a.comic_title_id = b.comic_title_id AND b.comic_publisher_id = c.comic_publisher_id "; $this->db->sql .= "AND a.create_date = ( SELECT MAX(create_date) FROM comics ) "; $this->db->sql .= "ORDER BY Rand() LIMIT 12 "; $this->data['new_additions'] = $this->db->readData(); if( strtotime($this->data['new_additions'][0]['create_date']) > strtotime($this->site->last_update) ) { $this->site->last_update = $this->data['new_additions'][0]['create_date']; } $this->db->sql = "SELECT count(a.comic_id) as comic_count, count(DISTINCT b.comic_title_id) AS title_count, "; $this->db->sql .= " count(DISTINCT c.comic_publisher_id) AS publisher_count, "; $this->db->sql .= "(SELECT count(comic_id) FROM comics WHERE create_date = (SELECT MAX(create_date) FROM comics)) AS new_count,"; $this->db->sql .= "(SELECT MIN(comic_year) FROM comics) AS min_comic_year, "; $this->db->sql .= "(SELECT MAX(comic_year) FROM comics) AS max_comic_year, "; $this->db->sql .= "(SELECT COUNT(*) FROM comics WHERE comic_year >= 1956 AND comic_year <= 1985) AS silver_bronze_count "; $this->db->sql .= "FROM comics a, comic_titles b, comic_publishers c "; $this->db->sql .= "WHERE a.comic_title_id = b.comic_title_id AND b.comic_publisher_id = c.comic_publisher_id"; $this->data['counts'] = $this->db->readData(); $this->db->sql = "SELECT a.comic_title, a.comic_title_id, COUNT(b.comic_id) AS comic_count "; $this->db->sql .= "FROM comic_titles a "; $this->db->sql .= "INNER JOIN comics b ON a.comic_title_id = b.comic_title_id "; $this->db->sql .= "GROUP BY a.comic_title "; $this->db->sql .= "ORDER BY comic_count DESC LIMIT 5"; $this->data['top_five_titles'] = $this->db->readData(); $this->db->sql = "SELECT a.comic_publisher, a.comic_publisher_id, COUNT(comic_id) AS comic_count "; $this->db->sql .= "FROM comic_publishers a "; $this->db->sql .= "INNER JOIN comic_titles b ON a.comic_publisher_id = b.comic_publisher_id "; $this->db->sql .= "INNER JOIN comics c ON b.comic_title_id = c.comic_title_id "; $this->db->sql .= "GROUP BY a.comic_publisher "; $this->db->sql .= "ORDER BY comic_count DESC LIMIT 5"; $this->data['top_five_publishers'] = $this->db->readData(); $this->data['eras'] = array( 'platinum'=>array( 'min_year'=>null, 'max_year'=>1937, 'random_comics'=>null, 'count'=>null ), 'golden'=>array( 'min_year'=>1938, 'max_year'=>1955, 'random_comics'=>null, 'count'=>null ), 'silver'=>array( 'min_year'=>1956, 'max_year'=>1972, 'random_comics'=>null, 'count'=>null ), 'bronze'=>array( 'min_year'=>1973, 'max_year'=>1985, 'random_comics'=>null, 'count'=>null ), 'copper'=>array( 'min_year'=>1986, 'max_year'=>1992, 'random_comics'=>null, 'count'=>null ), 'modern'=>array( 'min_year'=>1993, 'max_year'=>null, 'random_comics'=>null, 'count'=>null ) ); foreach($this->data['eras'] as &$era) { $clause = ""; if( isset($era['min_year']) ) { $clause .= " comic_year >= " . $era['min_year']; } if( isset($era['max_year']) ) { if( strlen($clause) > 0 ) { $clause .= " AND "; } $clause .= " comic_year <= " . $era['max_year']; } $this->db->sql = "SELECT a.comic_id, a.comic_volume, a.comic_issue, a.comic_hrn, b.comic_title "; $this->db->sql .= "FROM comics a INNER JOIN comic_titles b ON a.comic_title_id = b.comic_title_id "; $this->db->sql .= "WHERE " . $clause . " AND (a.no_image IS NULL || a.no_image = FALSE) ORDER BY RAND() LIMIT 6 "; $era['random_comics'] = $this->db->readData(); $this->db->sql = "SELECT COUNT(*) AS count FROM comics WHERE " . $clause; $era['count'] = $this->db->readData()[0]['count']; } $this->db->sql = "SELECT b.comic_title, a.comic_id, a.comic_volume, a.comic_issue, a.comic_hrn "; $this->db->sql .= "FROM comics a "; $this->db->sql .= "INNER JOIN comic_titles b ON a.comic_title_id = b.comic_title_id "; $this->db->sql .= " WHERE a.is_key = true ORDER BY RAND() LIMIT 18 "; $this->data['key_issues'] = $this->db->readData(); $this->data['publishers'] = array( 'marvel'=>array( 'id' => 36, 'random_comics'=>null, 'comic_count'=>null, 'title_count'=>null ), 'dc'=>array( 'id' => 15, 'random_comics'=>null, 'comic_count'=>null, 'title_count'=>null ), 'archie'=>array( 'id' => 5, 'random_comics'=>null, 'comic_count'=>null, 'title_count'=>null ), 'charlton'=>array( 'id' => 8, 'random_comics'=>null, 'comic_count'=>null, 'title_count'=>null ), 'dell'=>array( 'id' => 16, 'random_comics'=>null, 'comic_count'=>null, 'title_count'=>null ), 'fawcett'=>array( 'id' => 20, 'random_comics'=>null, 'comic_count'=>null, 'title_count'=>null ), 'harvey'=>array( 'id' => 29, 'random_comics'=>null, 'comic_count'=>null, 'title_count'=>null ), 'whitman'=>array( 'id' => 54, 'random_comics'=>null, 'comic_count'=>null, 'title_count'=>null ) ); foreach($this->data['publishers'] as &$publisher) { $this->db->sql = "SELECT a.comic_id, a.comic_title_id, a.comic_volume, a.comic_issue, a.comic_hrn, b.comic_title FROM comics a "; $this->db->sql .= "INNER JOIN comic_titles b ON a.comic_title_id = b.comic_title_id "; $this->db->sql .= "WHERE b.comic_publisher_id = " . $publisher['id'] . " AND (a.no_image IS NULL || a.no_image = FALSE) ORDER BY RAND() LIMIT 6 "; $publisher['random_comics'] = $this->db->readData(); $this->db->sql = "SELECT COUNT(*) AS count FROM comics a INNER JOIN comic_titles b ON a.comic_title_id = b.comic_title_id "; $this->db->sql .= "WHERE b.comic_publisher_id = " . $publisher['id']; $publisher['comic_count'] = $this->db->readData()[0]['count']; $this->db->sql = "SELECT COUNT(*) AS count FROM comic_titles WHERE comic_publisher_id = " . $publisher['id']; $publisher['title_count'] = $this->db->readData()[0]['count']; } } public function getComicType() { $retVal = ""; $this->db->sql = "SELECT comic_type FROM comic_types WHERE comic_type_id = " . $this->comic_type_id; $record = $this->db->readData(); if( count($record) > 0 ) { $retVal = trim($record[0]['comic_type']); } return $retVal; } public function getPublisher() { $retVal = ""; $this->db->sql = "SELECT comic_publisher FROM comic_publishers WHERE comic_publisher_id = " . $this->comic_publisher_id; $record = $this->db->readData(); if( count($record) > 0 ) { $retVal = trim($record[0]['comic_publisher']); } return $retVal; } public function getTitle() { $retVal = ""; $this->db->sql = "SELECT comic_title FROM comic_titles WHERE comic_title_id = " . $this->comic_title_id; $record = $this->db->readData(); if( count($record) > 0 ) { $retVal = trim($record[0]['comic_title']); } return $retVal; } public function getTitleInfo($comic_title_info_id = 0) { if( $comic_title_info_id == 0 ) { $comic_title_info_id = $this->comic_title_id; } $this->db->sql = "SELECT a.comic_title, a.comic_title_id, a.comic_title_comments, a.series_complete, a.has_logo, b.comic_type, b.comic_type_id, c.comic_publisher, c.comic_publisher_id, "; $this->db->sql .= "(SELECT COUNT(*) FROM comics WHERE comic_title_id = a.comic_title_id) AS comic_count "; $this->db->sql .= "FROM comic_titles a INNER JOIN comic_types b ON a.comic_type_id = b.comic_type_id "; $this->db->sql .= "INNER JOIN comic_publishers c ON a.comic_publisher_id = c.comic_publisher_id "; $this->db->sql .= "WHERE a.comic_title_id = " . $comic_title_info_id; $retVal = $this->db->readData(); return $retVal; } public function getMaxCreateDate() { $this->db->sql = "SELECT MAX(create_date) AS max_create_date FROM comics "; $record = $this->db->readData(); return $record[0]["max_create_date"]; } /********************************************************************************************************/ function __destruct() { unset($db); } } /************************************************************************************************ ************************************************************************************************* ************************************************************************************************/ class Site{ private $email = "webmaster@keneriksen.com"; private $title = "Ken Eriksen's Comic Book Collection"; private $domain; private $img; public $last_update; private $local; private $index; private $sysRootPath; private $compressHtmlOutput; private $request_scheme; /********************************************************************************************************/ function __construct() { $this->domain = $_SERVER["SERVER_NAME"]; $this->last_update = "5/09/2016 01:02 PM"; $this->request_scheme = $_SERVER['REQUEST_SCHEME']; if( $this->domain == "comics.ken.com" ) { //$this->index = "/index.php"; $this->index = ""; $this->img = "comics.ken.com/images/"; $this->sysRootPath = "C:\\ken\\apache_vhosts\\comic\\"; $this->local = true; $this->compressHtmlOutput = false; } else{ if( $this->domain != "comics.keneriksen.com" ) { header("Location: " . $this->request_scheme . "://comics.keneriksen.com"); } $this->index = ""; $this->img = $this->domain . "/images/"; $this->sysRootPath = $_SERVER['SITE_ROOT'] . "/var/www/comic/"; $this->local = false; $this->compressHtmlOutput = true; } } /********************************************************************************************************/ function __set($name, $value) { if( $name == "last_update" ) { $this->$name = $value; } } /********************************************************************************************************/ function __get($name) { $out = $this->$name; return $out; } } /************************************************************************************************ ************************************************************************************************* ************************************************************************************************/ class Request{ private $action; private $valid_actions; private $path_info; private $site; /********************************************************************************************************/ function __construct($site) { $this->site = $site; $this->controller(); } /********************************************************************************************************/ function __get($name) { $out = ""; if( $name == "action" ) { $out = $this->$name; } return $out; } /********************************************************************************************************/ private function controller() { $this->action = "home"; $this->valid_actions = array("comic", "comics", "title", "titles", "publisher", "publishers", "contact", "missing", "apache403" ); /* path_info = 0:action/1:page publisher: no other path vars title: 2:type/3:publisher4:search term comic: 2:title/3:type/4:publisher/5:year/6:create_date/7:resetcrumbs */ $this->path_info = explode("/", str_replace( str_replace(".php", "", $_SERVER["SCRIPT_NAME"]), "", str_replace(".php", "", $_SERVER["REQUEST_URI"] ) ) ); if( empty($this->path_info[0]) ) { unset($this->path_info[0]); } if( empty( $this->path_info[count($this->path_info)] ) ) { unset( $this->path_info[count($this->path_info)] ); } $this->path_info = array_values($this->path_info); $alien_referer = true; if( isset($_SERVER['HTTP_REFERER']) ) { $parsedUrl = parse_url($_SERVER["HTTP_REFERER"]); if( $parsedUrl['host'] == $this->site->domain ) { $alien_referer = false; } } if( count($this->path_info) > 0 ) { if( in_array(trim($this->path_info[0]),$this->valid_actions) ) { $this->action = trim($this->path_info[0]); } else{ $this->action = "missing"; } } if( $this->action == "title" || $this->action == "titles" ) { $this->action = "title"; } else if( $this->action == "comic" || $this->action == "comics" ) { $this->action = "comic"; if( isset($this->path_info[7]) ) { unset($_SESSION['crumbs']); } } else if( $this->action == "publisher" || $this->action == "publishers" ) { $this->action = "publisher"; } else if( $this->action == "contact" ) { if( $_SERVER['REQUEST_METHOD'] == "POST" && ! $alien_referer ) { new Contact($this->site); exit; } else{ $this->action = "missing"; } } $util = new Utilities($this->site); $page = new Page(); if( $this->action != "apache403" && $this->action != "missing") { $data_handler = new DataHandler($this->site, $this->action, $this->path_info, $util ); $last_update = $data_handler->getMaxCreateDate(); if( $last_update > $this->site->last_update ) { $site->last_update = $last_update; } $page->site = $this->site; $page->action = $this->action; $page->data = $data_handler->data; $page->err = $data_handler->err; $page->pag = $data_handler->pag; $page->util = $util; $page->comic_type_id = $data_handler->comic_type_id; $page->comic_publisher_id = $data_handler->comic_publisher_id; $page->comic_title = $data_handler->comic_title; $page->title_keyword_search = $data_handler->title_keyword_search; if( $data_handler->comic_type_id > 0 ) { $page->comic_type = $data_handler->getComicType(); } if( $data_handler->comic_publisher_id > 0 ) { $page->comic_publisher = $data_handler->getPublisher(); } $page->comic_title_id = $data_handler->comic_title_id; $page->comic_year = $data_handler->comic_year; $page->create_date = $data_handler->create_date; $page->recent_additions_search = $data_handler->recent_additions_search; if( $data_handler->comic_title_id > 0 ) { $page->comic_title = $data_handler->getTitle(); $page->title_info = $data_handler->getTitleInfo(); } else if( $this->action == "comic" ) { $temp_title_id = 0; $comic_title_info; for( $i = 0; $i < count($data_handler->data); $i++ ) { if( $data_handler->data[$i]['comic_title_id'] != $temp_title_id ) { $temp_title_id = $data_handler->data[$i]['comic_title_id']; $comic_title_info[$data_handler->data[$i]['comic_title_id']] = $data_handler->getTitleInfo($data_handler->data[$i]['comic_title_id']); } } $page->comic_title_info = json_encode($comic_title_info); } $page->loadPage(); } else if( $this->action == "missing" ) { header('HTTP/1.0 404 Not Found'); $page->site = $this->site; $page->action = $this->action; $page->loadPage(); } else if($this->action == "apache403" ) { //mod_evasive causing 403 error so changing it to a 429 instead header('HTTP/1.1 429 Too many requests'); echo(""); echo("
HTTP/1.1 429 ERROR - Too many requests
Slow down! You're following those links way too fast and the server thinks this might be a possible DoS attempt.
"); echo("
The page you requested will automatically load in 10 seconds.
"); echo(""); echo(""); exit; } } } /************************************************************************************************ ************************************************************************************************* ************************************************************************************************/ class Utilities{ private $site; /********************************************************************************************************/ function __construct($site) { $this->site = $site; } /********************************************************************************************************/ public function getComicScanUrl($id,$size) { $url = ""; if( strlen($id) > 1 ) { $dir = substr($id,0,2); } else{ $dir = $id . "0"; } if( $size == 1 ) { $url = $this->site->request_scheme . "://" . $this->site->img . "scans/150/" . $dir . "/" . $id . "-150.jpg"; } elseif( $size == 2 ) { $url = $this->site->request_scheme . "://" . $this->site->img . "scans/700/" . $dir . "/" . $id . "-700.jpg"; } return $url; } /********************************************************************************************************/ public function buildTitleText($title, $volume, $issue, $hrn, $title_modal=false, $title_id = 0 ){ $title_text = trim($title); if( $title_modal ) { $title_text = "" . $title_text . ""; } if( strlen( $volume ) > 0 ) { $title_text .= " Vol. " . $volume; } if( strlen( $issue ) > 0 ) { $title_text .= " #" . $issue; } if( strlen( $hrn ) > 0 ) { $title_text .= " HRN " . $hrn; } return $title_text; } /********************************************************************************************************/ public function getDateParts($dateText) { $date = strtotime($dateText); $date_parts['year'] = date("Y", $date); $date_parts['month'] = date("m",$date); $date_parts['day'] = date("d",$date); return $date_parts; } /********************************************************************************************************/ public function getRandomNew() { $imgnew = array(); $imgnew[0] = "new-01.jpg"; $imgnew[1] = "new-02.jpg"; $imgnew[2] = "new-03.jpg"; $imgnew[3] = "new-04.jpg"; $imgnew[4] = "new-05.jpg"; $imgnew[5] = "new-06.jpg"; $imgnew[6] = "new-07.jpg"; $imgnew[7] = "new-08.jpg"; $imgnew[8] = "new-09.jpg"; $imgnew[9] = "new-10.jpg"; $imgnew[10] = "new-11.jpg"; $imgnew[11] = "new-12.jpg"; $imgnew[12] = "new-13.jpg"; $imgnew[13] = "new-14.jpg"; $imgnew[14] = "new-15.jpg"; $imgnew[15] = "new-16.jpg"; $imgnew[16] = "new-17.jpg"; $imgnew[17] = "new-18.jpg"; $imgnew[18] = "new-19.jpg"; $imgnew[19] = "new-20.jpg"; $imgnew[20] = "new-21.jpg"; $imgnew[21] = "new-22.jpg"; $imgnew[22] = "new-23.jpg"; $imgnew[23] = "new-24.jpg"; $imgnew[24] = "new-25.jpg"; $imgnew[25] = "new-26.jpg"; $imgnew[26] = "new-27.jpg"; $imgnew[27] = "new-28.jpg"; $imgnew[28] = "new-29.jpg"; $imgnew[29] = "new-30.jpg"; $imgnew[30] = "new-31.jpg"; $imgnew[31] = "new-32.jpg"; $imgnew[32] = "new-33.jpg"; $imgnew[33] = "new-34.jpg"; $imgnew[34] = "new-35.jpg"; $imgnew[35] = "new-36.jpg"; $random_new = $imgnew[rand(0,count($imgnew)-1)]; return $random_new; } } /************************************************************************************************ ************************************************************************************************* ************************************************************************************************/ class Page{ private $site; private $action; private $data; private $title; private $err; private $util; private $header_text; private $pag; private $comic_type_id; private $comic_publisher_id; private $comic_title_id; private $comic_title; private $title_keyword_search; private $bc; private $comic_type; private $comic_publisher; private $title_info; private $comic_title_info; /********************************************************************************************************/ function __construct() { } function loadPage() { if( $this->action != "home" && $this->action != "missing" ) { $action = $this->action; $this->$action(); } $this->bc = new breadcrumb_handler($this->action, $this->site->domain, $this->title, $this->comic_publisher_id, $this->comic_type_id, $this->comic_title_id, $this->site->request_scheme); if( empty($this->title) ) { $this->title = $this->site->title; } if( $this->site->compressHtmlOutput ) { ob_start(array('Page','compress_page')); } $this->loadMainTemplate(); if( $this->site->compressHtmlOutput ) { ob_end_flush(); } } function __set($name, $value) { $this->$name = $value; } /********************************************************************************************************/ private function comic() { if( $this->recent_additions_search ) { $this->title = "Recent Additions"; $this->header_text = $this->title; } else{ if( $this->comic_title_id > 0 ) { $this->title = $this->comic_title; $this->header_text = $this->title; } if( strlen($this->comic_year) > 0 && $this->comic_year > 0 ) { if( $this->title != "" ) { $this->title .= " ~ "; $this->header_text .= "
"; } switch($this->comic_year) { case "1880-1937": $this->title .= "Platinum Age Comics (" . $this->comic_year . ")"; $this->header_text .= "Platinum Age Comics (" . $this->comic_year . ")"; break; case "1938-1955": $this->title .= "Golden Age Comics (" . $this->comic_year . ")"; $this->header_text .= "Golden Age Comics (" . $this->comic_year . ")"; break; case "1956-1972": $this->title .= "Silver Age Comics (" . $this->comic_year . ")"; $this->header_text .= "Silver Age Comics (" . $this->comic_year . ")"; break; case "1973-1985": $this->title .= "Bronze Age Comics (" . $this->comic_year . ")"; $this->header_text .= "Bronze Age Comics (" . $this->comic_year . ")"; break; case "1986-1992": $this->title .= "Copper Age Comics (" . $this->comic_year . ")"; $this->header_text .= "Copper Age Comics (" . $this->comic_year . ")"; break; case "1993-" . date("Y"): $this->title .= "Modern Age Comics (" . $this->comic_year . ")"; $this->header_text .= "Modern Age Comics (" . $this->comic_year . ")"; break; default: $this->title .= "Comics from " . $this->comic_year; $this->header_text .= "Comics from " . $this->comic_year; } } if( $this->comic_type_id > 0 ) { if( $this->title != "" ) { $this->title .= " ~ "; $this->header_text .= "
"; } switch($this->comic_type) { case "Comic": $this->title .= "Comic Books"; $this->header_text .= "Comic Books"; break; case "Big Little Book": $this->title .= "Big Little Books"; $this->header_text .= "Big Little Books"; break; case "Digest": $this->title .= "Digest Size Comics"; $this->header_text .= "Digest Size Comics"; break; case "Magazine": $this->title .= "Comic Magazines"; $this->header_text .= "Comic Magazines"; break; case "Promotional": $this->title .= "Promotional Comics"; $this->header_text .= "Promotional Comics"; break; case "Oversized Comic": $this->title .= "Oversized/Treasury Comics"; $this->header_text .= "Oversized/Treasury Comics"; break; case "Paperback": $this->title .= "Paperback Comics"; $this->header_text .= "Paperback Comics"; break; default: $this->title .= $this->comic_type; $this->header_text .= $this->comic_type; } } if( $this->comic_publisher_id > 0 ) { if( $this->title != "" ) { $this->title .= " ~ "; $this->header_text .= "
"; } $this->header_text .= str_replace("Comics","", trim($this->comic_publisher)) . ' Comics'; $this->title .= str_replace("Comics","", trim($this->comic_publisher)) . ' Comics'; } if( $this->title == "" ) { $this->title = "Entire Collection"; $this->header_text = $this->title; } } } /********************************************************************************************************/ private function title() { if( strlen($this->comic_type) > 0 ) { switch($this->comic_type) { case "Comic": $this->title = "Comic Book Titles"; $this->header_text = $this->title; break; case "Big Little Book": $this->title = "Big Little Book Titles"; $this->header_text = $this->title; break; case "Digest": $this->title = "Digest Size Titles"; $this->header_text = $this->title; break; case "Magazine": $this->title = "Comic Magazine Titles"; $this->header_text = $this->title; break; case "Promotional": $this->title = "Promotional Comic Titles"; $this->header_text = $this->title; break; case "Oversized Comic": $this->title = "Oversized/Treasury Titles"; $this->header_text = $this->title; break; case "Paperback": $this->title = "Paperback Comic Titles"; $this->header_text = $this->title; break; default: $this->title = $this->comic_type . " Titles"; $this->header_text = $this->title; } } if( strlen($this->comic_publisher) > 0 ) { $this->title .= trim($this->comic_publisher) . " Titles"; $this->header_text = $this->title; } if( strlen($this->comic_title) > 0 && $this->comic_title != 'null' ) { $this->title .= 'Title Contains: "' . str_replace("''","'",str_replace("~","",$this->comic_title)) . '"'; $this->header_text = $this->title; } if( empty($this->title) ) { $this->title = "All Titles"; $this->header_text = $this->title; } } /********************************************************************************************************/ private function publisher() { if( empty($this->title) ) { $this->title = "Publishers"; $this->header_text = $this->title; } } /********************************************************************************************************/ private function loadMainTemplate() { require_once("templ/index_templ.php"); } /********************************************************************************************************/ public static function compress_page($buffer) { $search = array( "/ +/" => " ", "/||[\t\r\n]||\/\/ ||\]\]>|\/\/\]\]>|\/\/ "" ); $buffer = preg_replace(array_keys($search), array_values($search), $buffer); return $buffer; } } /************************************************************************************************ ************************************************************************************************* ************************************************************************************************/ class breadcrumb_handler{ private $crumbs = array(); private $action; private $domain; private $page_title; private $comic_publisher_id; private $comic_type_id; private $comic_title_id; public $reset; private $request_scheme; /********************************************************************************************************/ function __construct($action, $domain, $page_title, $comic_publisher_id, $comic_type_id, $comic_title_id, $request_scheme) { $this->action = $action; $this->domain = $domain; $this->page_title = $page_title; $this->comic_publisher_id = $comic_publisher_id; $this->comic_type_id = $comic_type_id; $this->comic_title_id = $comic_title_id; $this->request_scheme = $request_scheme; $this->setCrumbs(); } /********************************************************************************************************/ function __get($name) { $out = ""; if( $name == "crumbs" ) { $out = $this->$name; } return $out; } /********************************************************************************************************/ private function setCrumbs() { $this->crumbs[] = array('url'=>$this->request_scheme . '://' . $this->domain, 'title'=>'Home'); if( $this->action == 'publisher' ) { $this->crumbs[] = array('url'=>'', 'title'=>'Publishers'); $_SESSION['crumbs']['publisher'] = array('title'=>$this->page_title,'url'=>$_SERVER['REQUEST_URI']); } else if( $this->action == 'title' ) { if( isset($_SESSION['crumbs']['publisher']) && $this->comic_publisher_id > 0 ) { $this->crumbs[] = array('url'=>$this->request_scheme . '://' . $this->domain . $_SESSION['crumbs']['publisher']['url'], 'title'=>$_SESSION['crumbs']['publisher']['title']); } $this->crumbs[] = array('url'=>'', 'title'=>$this->page_title); $_SESSION['crumbs']['title'] = array('title'=>$this->page_title,'url'=>$_SERVER['REQUEST_URI']); $_SESSION['crumbs']['title'] ['comic_publisher_id'] = $this->comic_publisher_id; $_SESSION['crumbs']['title'] ['comic_type_id'] = $this->comic_type_id; } else if( $this->action == 'comic' ) { if( isset($_SESSION['crumbs']['publisher']) && ($this->comic_publisher_id > 0 || $this->comic_title_id > 0) ) { $this->crumbs[] = array('url'=>$this->request_scheme . '://' . $this->domain . $_SESSION['crumbs']['publisher']['url'], 'title'=>$_SESSION['crumbs']['publisher']['title']); } if( isset($_SESSION['crumbs']['title']) ) { if( $this->comic_publisher_id > 0 && ($this->comic_publisher_id == $_SESSION['crumbs']['title']['comic_publisher_id']) ) { $this->crumbs[] = array('url'=>$this->request_scheme . '://' . $this->domain . $_SESSION['crumbs']['title']['url'], 'title'=>$_SESSION['crumbs']['title']['title']); } else if( $this->comic_type_id > 0 && ($this->comic_type_id == $_SESSION['crumbs']['title']['comic_type_id']) ) { $this->crumbs[] = array('url'=>$this->request_scheme . '://' . $this>domain . $_SESSION['crumbs']['title']['url'], 'title'=>$_SESSION['crumbs']['title']['title']); } else if( $this->comic_title_id > 0 ) { $this->crumbs[] = array('url'=>$this->request_scheme . '://' . $this->domain . $_SESSION['crumbs']['title']['url'], 'title'=>$_SESSION['crumbs']['title']['title']); } } $this->crumbs[] = array('url'=>'', 'title'=>$this->page_title); } else if ($this->action == "home" ) { unset($_SESSION['crumbs']); } } } /************************************************************************************************ ************************************************************************************************* ************************************************************************************************/ class Contact { private $err = array(); private $name = ""; private $email = ""; private $msg = ""; private $to = ""; private $processed = false; private $result = array(); private $site; /********************************************************************************************************/ function __construct($site) { $this->site = $site; $this->processPost(); } /********************************************************************************************************/ private function processPost() { $this->name = trim(stripslashes($_POST["name"])); $this->email = trim(stripslashes($_POST["email"])); $this->msg = trim(stripslashes($_POST["msg"])); if( ! strlen( $this->name) ) { $this->err ['name'] = "Name is required."; } if( ! strlen( $this->email ) ) { $this->err ['email'] = "Email Address is required."; } elseif( ! $this->isValidEmail() ) { $this->err ['email'] = "Email Address is invalid. A valid email address is in the form name@domain.com."; } if( ! strlen( $this->msg ) ) { $this->err ['msg'] = "Message is required."; } if( count( $this->err ) == 0 ) { $this->sendMessage(); $this->result['sent'] = true; } else{ $this->result['sent'] = false; $this->result['err'] = $this->err; } echo json_encode($this->result); exit; } /********************************************************************************************************/ private function sendMessage() { $message_for_me = "Name: " . $this->name . "\nEmail: " . $this->email . "\nServer Time: " . date("m-d-Y h:i:s A T") . "\nSender IP: " . @$_SERVER['REMOTE_ADDR'] . "\nPosted From: " . @$_SERVER['HTTP_REFERER'] . "\nMessage:\n===================\n\n" . htmlentities($this->msg) . "\n\n==================="; @mail($this->site->email, "Message from web site", $message_for_me, "FROM: " . $this->site->email); } /********************************************************************************************************/ private function isValidEmail() { if ( filter_var($this->email, FILTER_VALIDATE_EMAIL) ) { return true; } else{ return false; } } } /************************************************************************************************ ************************************************************************************************* ************************************************************************************************/ $site = new Site(); $req = new Request($site); ?>