$value) define($key, $value); ini_set('display_errors', DEBUG); error_reporting(ERROR_REPORTING); class template { private $buffer; function template($file) { if (preg_match(TEMPLATE_MATCH, $file)) $this->buffer = file_get_contents(TEMPLATE_DIRECTORY.$file.'.template'); else trigger_error('oops', E_USER_ERROR); } function get() { return $this->buffer; } function block($replacement) { $replacement['base'] = HTTP_BASE; return str_replace( array_map(function($name){return '{'.$name.'}';}, array_keys($replacement)), array_values($replacement), $this->buffer ); } } class error { private $errors = array(404, 500); function error($error) { if (in_array($error, $this->errors)) { $template = new template('error'.$error); echo $template->block($_SERVER); } else trigger_error('Invalid error code('.$error.')!', E_USER_ERROR); } } class input { function get(&$method, $key, $default = NULL, $match = NULL) { if (isset($method[$key])) if (preg_match($match, $method[$key])) return $method[$key]; else trigger_error('Wrong format!', E_USER_ERROR); if ($default !== NULL) return $default; else trigger_error('Unknwon key('.$key.')!', E_USER_ERROR); } } function stmt($link, $query, $parameters = NULL) { if ($parameters !== NULL) { $types = ''; $values = array(); foreach ($parameters as $parameter) { $types .= $parameter[0]; $values[] = $parameter[1]; } } if (!$stmt = $link->prepare($query)) trigger_error('Unable to prepare!', E_USER_ERROR); if ($parameters !== NULL) { $params = array(); $params[] = $types; for ($i=0; $iexecute(); $stmt->store_result(); $bind = array(); $results = array(); $meta = $stmt->result_metadata(); while ($column = $meta->fetch_field()) $bind[] = &$results[$column->name]; call_user_func_array(array($stmt, 'bind_result'), $bind); $meta->close(); return array($stmt, $results); } function eh($error_number, $error_string, $error_file, $error_line) { if (!(error_reporting() & $error_number)) return; error_log( $error_number.': '. $error_string.' in '. $error_file.' on line '. $error_line, 0); ob_clean(); header('Content-type: text/plain'); new error(500); ob_end_flush(); die(); } if (!DEBUG) $oeh = set_error_handler('eh'); $db_read = new mysqli(DATABASE_HOST, DATABASE_READ_USER, DATABASE_READ_PASSWORD, DATABASE_NAME); if ($db_read->connect_error) { trigger_error('Error ('.$db_read->connect_errno.') connecting to the database: '.$db_read->connect_error, E_USER_ERROR); } $input = new input(); $mode = $input->get($_GET, 'mode', DEFAULT_MODE, '|[a-z_]{1,50}|'); $template_header = new template('header'); echo $template_header->block(array( 'title' => 'ush.it - a beautiful place' )); unset($template_header); switch ($mode) { case 'article_list': $page = $input->get($_GET, 'page', FALSE, '|[1-9][0-9]{0,3}|'); if ($page == FALSE) $page = 0; $query = 'SELECT COUNT(`id`) FROM `articles` WHERE `post_status` = "publish";'; if ($result = $db_read->query($query)) { $count = $result->fetch_array(MYSQLI_NUM); $count = $count[0]; $result->close(); unset($result); } else trigger_error($db_read->errno.' '.$db_read->error, E_USER_ERROR); if ($page >= ceil($count/ARTICLES_PERPAGE)) { header('HTTP 404 Document not found', TRUE, 404); new error(404); break; } $query = 'SELECT * FROM `articles` WHERE `post_status` = "publish" ORDER BY `id` DESC LIMIT ?,?;'; list($stmt, $article) = stmt($db_read, $query, array( array('i', (ARTICLES_PERPAGE*$page)), array('i', ARTICLES_PERPAGE) )); require_once MACRO_DIRECTORY.'articles.macro'; if ($page < ceil($count/ARTICLES_PERPAGE)-1) { $template_article_list = new template('article.list.previous'); echo $template_article_list->block(array('previous_page' => 'page/'.($page+1).'/')); unset($template_article_list); } if ($page > 0) { $template_article_list = new template('article.list.next'); echo $template_article_list->block(array('next_page' => ($page-1)<=0?'':'page/'.($page-1).'/')); unset($template_article_list); } break; case 'article_archive': $year = $input->get($_GET, 'year', FALSE, '|[12][0-9]{3}|'); if ($year == FALSE) { $year = date('Y'); } else { $year = (int)intval($year); $month = $input->get($_GET, 'month', FALSE, '|[0-9]{2}|'); if ($month < 1) $month = FALSE; if ($month == FALSE) { $month_from = 1; $month_to = 12; } else { $month_from = (int)intval($month); $month_to = $month_from; } } $time_start = mktime(0, 0, 0, $month_from, 1, $year); $time_end = mktime(12, 59, 59, $month_to, date('t', $time_start), $year); $query = 'SELECT * FROM `articles` WHERE `post_status` = "publish" AND `date` > ? AND `date` < ? ORDER BY `date` DESC;'; list($stmt, $article) = stmt($db_read, $query, array( array('i', $time_start), array('i', $time_end) )); require_once MACRO_DIRECTORY.'articles.macro'; break; case 'article_category': $category = $input->get($_GET, 'category', FALSE, '|[a-z]{1,255}|'); if ($category == FALSE) { new error(404); break; } $query = ' SELECT * FROM `articles_categories`, `categories`, `articles` WHERE `articles`.`post_status` = "publish" AND `categories`.`name` = ? AND `articles_categories`.`category` = `categories`.`id` AND `articles_categories`.`article` = `articles`.`id` ORDER BY `date` DESC; '; list($stmt, $article) = stmt($db_read, $query, array( array('s', $category) )); require_once MACRO_DIRECTORY.'articles.macro'; break; case 'page': $page_name = $input->get($_GET, 'page', FALSE, '|[a-z0-9]{1,255}|'); if ($page_name == FALSE) { new error(404); break; } $subpage_name = $input->get($_GET, 'subpage', FALSE, '|[a-z0-9]{1,255}|'); if ($subpage_name !== FALSE) { $query = 'SELECT * FROM `pages` WHERE `name` = ? AND `parent` = (SELECT `id` FROM `pages` WHERE `name` = ?);'; list($stmt, $page) = stmt($db_read, $query, array( array('s', $subpage_name), array('s', $page_name) )); } else { $query = 'SELECT * FROM `pages` WHERE `name` = ? AND `parent` = 0;'; list($stmt, $page) = stmt($db_read, $query, array( array('s', $page_name) )); } if ($stmt->num_rows != 1) { $stmt->close(); new error(404); break; } $template_page = new template('page'); if ($stmt->fetch()) { echo $template_page->block( array_merge($page, array( 'comments' => $page?'':'', 'date' => date('F j, Y', $page['date']), 'pm' => date('g:i a', $page['date']), 'categories' => join(', ', array('aa', 'bb')), 'words' => str_word_count($page['content']), 'reading' => '~'.floor(str_word_count($page['content'])*0.2/60).' minutes', 'content' => $page['content'], 'permalink' => date('Y/m/d/', $page['date']).$page['name'].'/' ) ) ); unset($template_article); } else trigger_error('Fetch failed!', E_USER_ERROR); break; case 'article': $year = $input->get($_GET, 'year', FALSE, '|[12][0-9]{3}|'); $month = $input->get($_GET, 'month', FALSE, '|[0-9]{2}|'); $day = $input->get($_GET, 'day', FALSE, '|[0-9]{2}|'); $article_name = $input->get($_GET, 'article', FALSE, '|[a-z0-9]{1,255}|'); if (!$year|| !$month || !$day || !$article_name) { new error(404); break; } $shift = 60*60; $time_start = mktime(0, 0, 0, $month, $day, $year); $time_end = mktime(12, 59, 59, $month, $day, $year)+$shift*20; $query = 'SELECT * FROM `articles` WHERE `name` = ? AND `date` > ? AND `date` < ? LIMIT 1;'; list($stmt, $article) = stmt($db_read, $query, array( array('s', $article_name), array('i', $time_start), array('i', $time_end) )); if ($stmt->num_rows != 1) { $stmt->close(); new error(404); break; } $template_article = new template('article'); if ($stmt->fetch()) { echo $template_article->block( array_merge($article, array( 'comments' => $article?'':'', 'date' => date('F j, Y', $article['date']), 'pm' => date('g:i a', $article['date']), 'categories' => join(', ', array('aa', 'bb')), 'words' => str_word_count($article['content']), 'reading' => '~'.floor(str_word_count($article['content'])*0.2/60).' minutes', 'content' => $article['content'], 'permalink' => date('Y/m/d/', $article['date']).$article['name'].'/' ) ) ); unset($template_article); } else trigger_error('Fetch failed!', E_USER_ERROR); break; case 'error': $error = $input->get($_GET, 'error', FALSE, '|[0-9]{1,3}|'); if (!$error) $error = 500; new error($error); break; default: trigger_error('Unknown mode!', E_USER_ERROR); } // Processing pages side menu $page_second = array(); $query = 'SELECT `id`, `name`, `title`, `parent` FROM `pages` WHERE `parent` > 0 ORDER BY `title` ASC;'; if ($result = $db_read->query($query)) { while ($page = $result->fetch_array(MYSQLI_ASSOC)) { $page_second[] = $page; } $result->close(); } else trigger_error($db_read->errno.' '.$db_read->error, E_USER_ERROR); unset($page); $page_buffer = ''; $query = 'SELECT `id`, `name`, `title` FROM `pages` WHERE `parent` = 0 ORDER BY `title` ASC;'; if ($result = $db_read->query($query)) { $template_page_item = new template('page.item'); while ($page = $result->fetch_array(MYSQLI_ASSOC)) { $page_buffer .= $template_page_item->block(array_merge(array( 'class' => 'first', 'href' => $page['name'].'/' ), $page)); foreach($page_second as $subpage) { if ($subpage['parent'] == $page['id']) $page_buffer .= $template_page_item->block(array_merge(array( 'class' => 'second', 'href' => $page['name'].'/'.$subpage['name'].'/' ), $subpage)); } } $result->close(); } else trigger_error($db_read->errno.' '.$db_read->error, E_USER_ERROR); unset($page); // Processing archives side menu $archive_dates = array(); $query = 'SELECT `date` FROM `articles` WHERE `post_status` = "publish" ORDER BY `date` DESC;'; if ($result = $db_read->query($query)) { while ($article = $result->fetch_array(MYSQLI_ASSOC)) { $date = date('Y/m/', $article['date']); if (!isset($archives[$date])) $archive_dates[$date] = array( 'month' => date('F', $article['date']), 'year' => date('Y', $article['date']) ); unset($article, $date); } $result->close(); } else trigger_error($db_read->errno.' '.$db_read->error, E_USER_ERROR); $archive_buffer = ''; $last_year = 0; $template_archive_item = new template('archive.item'); foreach ($archive_dates as $date => $data) { if ($last_year != $data['year']) { $archive_buffer .= $template_archive_item->block(array( 'class' => 'first', 'url' => $data['year'].'/', 'month' => $data['year'] )); } $archive_buffer .= $template_archive_item->block(array( 'class' => 'second', 'url' => $date, 'month' => $data['month'], 'year' => $data['year'] )); $last_year = $data['year']; } // Processing categories side menu $category_buffer = ''; $query = ' SELECT *, ( SELECT COUNT(`article`) as `counta` FROM `articles_categories`, `articles` WHERE `category` = `categories`.`id` AND `articles`.`id` = `article` AND `articles`.`post_status` = "publish" ) as `count` FROM `categories` ORDER BY `title`;'; if ($result = $db_read->query($query)) { $template_category_item = new template('category.item'); while ($category = $result->fetch_array(MYSQLI_ASSOC)) { if ($category['count'] > 0) $category_buffer .= $template_category_item->block($category); } $result->close(); } else trigger_error($db_read->errno.' '.$db_read->error, E_USER_ERROR); // Processing meta side menu $meta_buffer = ''; $metas = array( array('title' => 'Entries RSS', 'href' => 'feed/'), array('title' => 'Comments RSS', 'href' => 'feed/comments/'), array('title' => 'ESF Support', 'href' => 'feed/esf/') ); $template_meta_item = new template('meta.item'); foreach($metas as $meta) { $meta_buffer .= $template_meta_item->block($meta); } unset($template_meta_item, $metas); // Buinding footer $template_footer = new template('footer'); echo $template_footer->block(array( 'title' => 'ush.it - a beautiful place', 'page' => $page_buffer, 'archive' => $archive_buffer, 'category' => $category_buffer, 'meta' => $meta_buffer, 'sponsor' => 'Need professional Information Security services? Call ISGroup!', 'banner' => 'Crypto tools: encoding and decoding in 14 formats.' )); unset($template_footer, $archive_buffer, $category_buffer, $meta_buffer); $db_read->close(); ?>