/_sohy/classes/sohy.class.php
PHP | 527 lines | 260 code | 140 blank | 127 comment | 52 complexity | e5f7b1f691fbaf0a7c72dfe53015cc42 MD5 | raw file
- <?php
- /*
- ======================
- == Sohy-Sauce Class ==
- ======================
- */
- if(!defined('INDEX')) die();
- use Yaml\Yaml;
- class sohy {
- public function __construct($base = NULL) {
- $this->post = arrToObj($this->sanitize($_POST));
- $this->get = arrToObj($this->sanitize($_GET));
- $this->current_url = current_url();
- $this->base = new stdClass();
- $this->true_base = new stdClass();
- $this->url = new stdClass();
- $this->loaded = new stdClass();
- $this->default = new stdClass();
- // some base settings
- if(!isset($base)) {
- $this->base->dir = $base;
- } elseif(class_exists('admin')) {
- $this->base->dir = '../';
- } else {
- $this->base = false;
- $this->base->dir = false;
- }
- $this->_config();
- $this->_url_params();
- $this->_files();
- // some defaults
- $this->page_count = false;
- $this->loaded->dir = $this->is_cat();
- $this->loaded->files = false;
- }
- /*
- _config()
- =========
- Set's up all config variables.
- Arguments: none.
- */
- protected function _config() {
- global $yaml;
- if(file_exists($this->base->dir.'config.yml')) {
- $this->config = arrToObj(Yaml::parse(file_get_contents($this->base->dir.'config.yml')));
- $this->site_name = $this->config->site->name;
- $this->site_theme = $this->config->site->theme;
- $this->_defaults();
- if(isset($this->config->site->date_format)) {
- $this->date_format = $this->config->site->date_format;
- }
- $this->url->home = home_url();
- $this->url->theme = $this->url->home.'/themes/'.$this->site_theme;
- $this->version = '0.3.2';
- $this->powered_by = '<a href="'.$this->url->home.'">'.$this->site_name.'</a> <em>powered by <a href="http://www.ampersandwich.co.uk/projects/sohy">Sohy</a> '.$this->version.' α</em>';
- $this->meta = $this->config->site->meta;
- }
- if(isset($this->config->site->meta)) {
- $this->get_meta($this->config->site->meta);
- }
- }
- /*
- _defaults()
- ===========
- Set up default variables.
- Arguments: none.
- */
- protected function _defaults() {
- $this->prettylinks = true;
- $this->date_format = 'dS F Y';
- $this->base->content = 'content/';
- $this->file_types = 'yml,yaml,md,markdown,txt,html,htm';
- if(isset($this->config->site->prettylinks)) {
- $this->prettylinks = $this->config->site->prettylinks;
- }
- if(isset($this->config->site->format->date)) {
- $this->date_format = $this->config->site->format->date;
- }
- if(isset($this->config->site->base->content)) {
- $this->base->content = $this->base->content.$this->config->site->base->content;
- }
- }
- /*
- _files()
- ========
- Collects a list of files.
- Arguments: none.
- */
- protected function _files() {
- $this->true_base->categories = glob($this->base->dir.$this->base->content.'*', GLOB_ONLYDIR);
- if($this->is_cat()) {
- $posts = $this->base->dir.$this->check_file().'/*{'.$this->file_types.'}';
- } else {
- $posts = $this->base->dir.$this->base->content.'*.{'.$this->file_types.'}';
- }
- $this->true_base->posts = glob($posts, GLOB_BRACE);
- $this->base->posts = $this->true_base->posts;
- }
- public function _url_params($optional = array()) {
- if($this->prettylinks) {
- $url = str_replace($this->url->home, '', $this->current_url);
- $this->url->params = array_filter(explode('/', $url));
- $flags = array_merge($optional, array('page', 'sitemap', 'rss'));
- $param_key = NULL;
- foreach($this->url->params as $key => $params) {
- if(in_array($params, $flags)) {
- $the_key = $key;
- $param_key = $params;
- if(!isset($this->url->params[$key+1])) {
- unset($this->url->params[$key]);
- $this->{$params} = true;
- }
- } elseif(!is_null($param_key)) {
- unset($this->url->params[$the_key]);
- unset($this->url->params[$key]);
- $this->{$param_key} = $params;
- $param_key = NULL;
- }
- }
- } else {
- $this->url->params = $this->get;
- }
- $request_uri = @implode('/', $this->url->params);
- if($this->prettylinks) {
- $this->url->current = rtrim($this->url->home.'/'.$request_uri, '/');
- } else {
- $request_uri = trim($request_uri, '/');
- $this->url->current = $this->url->home.'/?page='.$request_uri;
- }
- }
- /*
- sanitize()
- ==========
- Strips inputs of undesireable
- characters and replaces them
- with a hyphen (-).
- Arguments:
- $input - The string or array to sanitize.
- */
- public function sanitize($input) {
- if(is_array($input)) {
- foreach($input as $key => $val) {
- $input[$key] = $this->sanitize($val);
- }
- } else {
- $input = strtolower(preg_replace("/[^a-z0-9\-\/]+/i", "-", $input));
- }
- return $input;
- }
- /*
- is_version()
- ============
- Checks if installed version is
- greater-than or equal to $version.
- Arguments:
- $version - Version to check against.
- */
- public function is_version($version) {
- /*
- explode so we can get the current version,
- just incase we're using a beta/"Kinda Nightly"
- */
- $current_version = explode(' ', $this->version);
- if((float) $current_version[0] >= (float) $version) return true;
- return false;
- }
- /*
- is_index()
- ==========
- */
- public function is_index() {
- if(!isset($this->url->params[0])) return true;
- return false;
- }
- /*
- is_post()
- =========
- Checks if the viewer is currently viewing a
- post page.
- Arguments: none.
- */
- public function is_post($file = null) {
- $file = $this->check_file();
- if(is_file($file)) return true;
- return false;
- }
- /*
- is_cat()
- ========
- Checks if the viewer is viewing a
- category page.
- Arguments: none.
- */
- public function is_cat($file = NULL) {
- global $post;
- if(!is_null($file)) {
- $end = strrev($file);
- $end = explode('/', $end, 2);
- $end = strrev($end[0]);
- $file = explode('.', $end);
- if($file[0] == 'index') return true;
- }
- if(!isset($this->get->post)) return false;
- if(is_dir($this->check_file($this->get->post))) return true;
- return false;
- }
- /*
- get_meta()
- ==========
- Get's meta data from the config.yml or
- post file and returns them as a more
- readable, unified format.
- Arguments:
- $data = data to be processed.
- */
- public function get_meta($data = NULL) {
- if(is_array($data)) {
- foreach($data as $key => $val) {
- $this->meta->{$key} = $val;
- }
- }
- }
- /*
- load_file()
- ===========
- returns the file type.
- */
- public function load_file($file = NULL) {
- global $yaml;
- if(is_null($file)) return;
- $type = explode('.', strrev($file), 2);
- $type = strrev($type[0]);
- $file = file_get_contents($file);
- if(in_array($type, array('yml','yaml'))) {
- return Yaml::parse($file);
- } elseif(in_array($type, array('md','markdown', 'txt'))) {
- return $this->markdown_pre_parse($file);
- } elseif($type == 'html') {
- return array('content' => load_file($file));
- }
- }
- /*
- check_file()
- ============
- */
- public function check_file($file = NULL) {
- if($this->url->params) {
- $file = 'content';
- foreach($this->url->params as $param) {
- $file .= '/*.'.$param;
- $dir = glob($file, GLOB_ONLYDIR);
- if(empty($dir[0])) {
- $file = glob($file.'.{'.$this->file_types.'}', GLOB_BRACE);
- if(isset($file[0])) {
- return $file[0];
- } else {
- return;
- }
- } else {
- $file = $dir[0];
- }
- }
- return $dir[0];
- } elseif(count($this->url->params) < 1) {
- return true;
- }
- return NULL;
- }
- /*
- file_slug()
- ===========
- */
- public function file_slug($file) {
- preg_match('/([0-9]{1,})\.(.*?)\.([a-z]{1,})/', $file, $match);
- return $match[2];
- }
- /*
- markdown_pre_parse()
- ====================
- Preparses markdown files and extracts
- variables.
- */
- public function markdown_pre_parse($contents = null) {
- global $markdown;
- if(!is_null($contents)) {
- $parts = explode('------', $contents);
- $lines = explode("\n", $parts[0]);
- $content = array();
- foreach($lines as $line) {
- if(empty($line)) continue;
- $content = array_merge_recursive($content, $this->markdown_vars($line));
- }
- if(!isset($parts[1])) return $content;
- $md_content = $markdown->md($parts[1]);
- /*
- markdown may wrap block elements
- in <p> tags, strip these.
- */
- $md_content = str_ireplace(array('<p><div', '</div></p>'), array('<div','</div>'), $md_content);
- $content['content'] = $md_content;
- return $content;
- }
- }
- /*
- markdown_vars()
- ===============
- */
- private function markdown_vars($array = false) {
- if(is_string($array)) {
- $vals = explode(': ', $array, 2);
- return $this->markdown_vars_create($vals[0], $vals[1]);
- }
- return false;
- }
- /*
- mardown_vars_create()
- =====================
- */
- private function markdown_vars_create($array = false, $value = null) {
- $keys = explode(':', $array, 2);
- if(count($keys) > 1) {
- return array($keys[0] => $this->markdown_vars_create($keys[1], $value));
- } else {
- return array($keys[0] => $value);
- }
- }
- /*
- file_is_index()
- ===============
- Checks if file is index.md/yml
- */
- public function file_is_index($file = NULL) {
- if(is_null($file)) return false;
- if(preg_match('/\/index.[yml|md]/', $file, $match)) return true;
- return false;
- }
- /*
- generate()
- ==========
- Gernerates for an xml output for the website
- sitemap for search engines.
- Arguments: none.
- */
- public function generate($type = NULL) {
- global $posts;
- if(!isset($this->{$type})) return;
- $post_list = $posts->collect_posts();
- $post_list = array_flatten($post_list);
- if(isset($this->config->site->{$type}->exclude)) {
- $exclude_list = $this->config->site->{$type}->exclude;
- }
- $return = array();
- foreach($post_list as $key => $post) {
- $continue = false;
- if($type != 'sitemap') {
- if($this->file_is_index($post)) continue;
- if(isset($exclude_list)) {
- foreach($exclude_list as $exclusion) {
- if(strpos($post, $exclusion) !== false) {
- $continue = true;
- break;
- }
- }
- }
- }
- if($continue) {
- continue;
- }
- $return[$key]['url'] = $posts->create_url($post);
- $return[$key]['data'] = $this->load_file($post);
- $return[$key]['data']['title'] = $posts->create_post_title(@$return[$key]['data']['title'], $post);
- }
- return $return;
- }
- // End of class.
- }
- ?>