PageRenderTime 68ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/_sohy/classes/sohy.class.php

https://github.com/ampersarnie/Sohy
PHP | 527 lines | 260 code | 140 blank | 127 comment | 52 complexity | e5f7b1f691fbaf0a7c72dfe53015cc42 MD5 | raw file
  1. <?php
  2. /*
  3. ======================
  4. == Sohy-Sauce Class ==
  5. ======================
  6. */
  7. if(!defined('INDEX')) die();
  8. use Yaml\Yaml;
  9. class sohy {
  10. public function __construct($base = NULL) {
  11. $this->post = arrToObj($this->sanitize($_POST));
  12. $this->get = arrToObj($this->sanitize($_GET));
  13. $this->current_url = current_url();
  14. $this->base = new stdClass();
  15. $this->true_base = new stdClass();
  16. $this->url = new stdClass();
  17. $this->loaded = new stdClass();
  18. $this->default = new stdClass();
  19. // some base settings
  20. if(!isset($base)) {
  21. $this->base->dir = $base;
  22. } elseif(class_exists('admin')) {
  23. $this->base->dir = '../';
  24. } else {
  25. $this->base = false;
  26. $this->base->dir = false;
  27. }
  28. $this->_config();
  29. $this->_url_params();
  30. $this->_files();
  31. // some defaults
  32. $this->page_count = false;
  33. $this->loaded->dir = $this->is_cat();
  34. $this->loaded->files = false;
  35. }
  36. /*
  37. _config()
  38. =========
  39. Set's up all config variables.
  40. Arguments: none.
  41. */
  42. protected function _config() {
  43. global $yaml;
  44. if(file_exists($this->base->dir.'config.yml')) {
  45. $this->config = arrToObj(Yaml::parse(file_get_contents($this->base->dir.'config.yml')));
  46. $this->site_name = $this->config->site->name;
  47. $this->site_theme = $this->config->site->theme;
  48. $this->_defaults();
  49. if(isset($this->config->site->date_format)) {
  50. $this->date_format = $this->config->site->date_format;
  51. }
  52. $this->url->home = home_url();
  53. $this->url->theme = $this->url->home.'/themes/'.$this->site_theme;
  54. $this->version = '0.3.2';
  55. $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.' &alpha;</em>';
  56. $this->meta = $this->config->site->meta;
  57. }
  58. if(isset($this->config->site->meta)) {
  59. $this->get_meta($this->config->site->meta);
  60. }
  61. }
  62. /*
  63. _defaults()
  64. ===========
  65. Set up default variables.
  66. Arguments: none.
  67. */
  68. protected function _defaults() {
  69. $this->prettylinks = true;
  70. $this->date_format = 'dS F Y';
  71. $this->base->content = 'content/';
  72. $this->file_types = 'yml,yaml,md,markdown,txt,html,htm';
  73. if(isset($this->config->site->prettylinks)) {
  74. $this->prettylinks = $this->config->site->prettylinks;
  75. }
  76. if(isset($this->config->site->format->date)) {
  77. $this->date_format = $this->config->site->format->date;
  78. }
  79. if(isset($this->config->site->base->content)) {
  80. $this->base->content = $this->base->content.$this->config->site->base->content;
  81. }
  82. }
  83. /*
  84. _files()
  85. ========
  86. Collects a list of files.
  87. Arguments: none.
  88. */
  89. protected function _files() {
  90. $this->true_base->categories = glob($this->base->dir.$this->base->content.'*', GLOB_ONLYDIR);
  91. if($this->is_cat()) {
  92. $posts = $this->base->dir.$this->check_file().'/*{'.$this->file_types.'}';
  93. } else {
  94. $posts = $this->base->dir.$this->base->content.'*.{'.$this->file_types.'}';
  95. }
  96. $this->true_base->posts = glob($posts, GLOB_BRACE);
  97. $this->base->posts = $this->true_base->posts;
  98. }
  99. public function _url_params($optional = array()) {
  100. if($this->prettylinks) {
  101. $url = str_replace($this->url->home, '', $this->current_url);
  102. $this->url->params = array_filter(explode('/', $url));
  103. $flags = array_merge($optional, array('page', 'sitemap', 'rss'));
  104. $param_key = NULL;
  105. foreach($this->url->params as $key => $params) {
  106. if(in_array($params, $flags)) {
  107. $the_key = $key;
  108. $param_key = $params;
  109. if(!isset($this->url->params[$key+1])) {
  110. unset($this->url->params[$key]);
  111. $this->{$params} = true;
  112. }
  113. } elseif(!is_null($param_key)) {
  114. unset($this->url->params[$the_key]);
  115. unset($this->url->params[$key]);
  116. $this->{$param_key} = $params;
  117. $param_key = NULL;
  118. }
  119. }
  120. } else {
  121. $this->url->params = $this->get;
  122. }
  123. $request_uri = @implode('/', $this->url->params);
  124. if($this->prettylinks) {
  125. $this->url->current = rtrim($this->url->home.'/'.$request_uri, '/');
  126. } else {
  127. $request_uri = trim($request_uri, '/');
  128. $this->url->current = $this->url->home.'/?page='.$request_uri;
  129. }
  130. }
  131. /*
  132. sanitize()
  133. ==========
  134. Strips inputs of undesireable
  135. characters and replaces them
  136. with a hyphen (-).
  137. Arguments:
  138. $input - The string or array to sanitize.
  139. */
  140. public function sanitize($input) {
  141. if(is_array($input)) {
  142. foreach($input as $key => $val) {
  143. $input[$key] = $this->sanitize($val);
  144. }
  145. } else {
  146. $input = strtolower(preg_replace("/[^a-z0-9\-\/]+/i", "-", $input));
  147. }
  148. return $input;
  149. }
  150. /*
  151. is_version()
  152. ============
  153. Checks if installed version is
  154. greater-than or equal to $version.
  155. Arguments:
  156. $version - Version to check against.
  157. */
  158. public function is_version($version) {
  159. /*
  160. explode so we can get the current version,
  161. just incase we're using a beta/"Kinda Nightly"
  162. */
  163. $current_version = explode(' ', $this->version);
  164. if((float) $current_version[0] >= (float) $version) return true;
  165. return false;
  166. }
  167. /*
  168. is_index()
  169. ==========
  170. */
  171. public function is_index() {
  172. if(!isset($this->url->params[0])) return true;
  173. return false;
  174. }
  175. /*
  176. is_post()
  177. =========
  178. Checks if the viewer is currently viewing a
  179. post page.
  180. Arguments: none.
  181. */
  182. public function is_post($file = null) {
  183. $file = $this->check_file();
  184. if(is_file($file)) return true;
  185. return false;
  186. }
  187. /*
  188. is_cat()
  189. ========
  190. Checks if the viewer is viewing a
  191. category page.
  192. Arguments: none.
  193. */
  194. public function is_cat($file = NULL) {
  195. global $post;
  196. if(!is_null($file)) {
  197. $end = strrev($file);
  198. $end = explode('/', $end, 2);
  199. $end = strrev($end[0]);
  200. $file = explode('.', $end);
  201. if($file[0] == 'index') return true;
  202. }
  203. if(!isset($this->get->post)) return false;
  204. if(is_dir($this->check_file($this->get->post))) return true;
  205. return false;
  206. }
  207. /*
  208. get_meta()
  209. ==========
  210. Get's meta data from the config.yml or
  211. post file and returns them as a more
  212. readable, unified format.
  213. Arguments:
  214. $data = data to be processed.
  215. */
  216. public function get_meta($data = NULL) {
  217. if(is_array($data)) {
  218. foreach($data as $key => $val) {
  219. $this->meta->{$key} = $val;
  220. }
  221. }
  222. }
  223. /*
  224. load_file()
  225. ===========
  226. returns the file type.
  227. */
  228. public function load_file($file = NULL) {
  229. global $yaml;
  230. if(is_null($file)) return;
  231. $type = explode('.', strrev($file), 2);
  232. $type = strrev($type[0]);
  233. $file = file_get_contents($file);
  234. if(in_array($type, array('yml','yaml'))) {
  235. return Yaml::parse($file);
  236. } elseif(in_array($type, array('md','markdown', 'txt'))) {
  237. return $this->markdown_pre_parse($file);
  238. } elseif($type == 'html') {
  239. return array('content' => load_file($file));
  240. }
  241. }
  242. /*
  243. check_file()
  244. ============
  245. */
  246. public function check_file($file = NULL) {
  247. if($this->url->params) {
  248. $file = 'content';
  249. foreach($this->url->params as $param) {
  250. $file .= '/*.'.$param;
  251. $dir = glob($file, GLOB_ONLYDIR);
  252. if(empty($dir[0])) {
  253. $file = glob($file.'.{'.$this->file_types.'}', GLOB_BRACE);
  254. if(isset($file[0])) {
  255. return $file[0];
  256. } else {
  257. return;
  258. }
  259. } else {
  260. $file = $dir[0];
  261. }
  262. }
  263. return $dir[0];
  264. } elseif(count($this->url->params) < 1) {
  265. return true;
  266. }
  267. return NULL;
  268. }
  269. /*
  270. file_slug()
  271. ===========
  272. */
  273. public function file_slug($file) {
  274. preg_match('/([0-9]{1,})\.(.*?)\.([a-z]{1,})/', $file, $match);
  275. return $match[2];
  276. }
  277. /*
  278. markdown_pre_parse()
  279. ====================
  280. Preparses markdown files and extracts
  281. variables.
  282. */
  283. public function markdown_pre_parse($contents = null) {
  284. global $markdown;
  285. if(!is_null($contents)) {
  286. $parts = explode('------', $contents);
  287. $lines = explode("\n", $parts[0]);
  288. $content = array();
  289. foreach($lines as $line) {
  290. if(empty($line)) continue;
  291. $content = array_merge_recursive($content, $this->markdown_vars($line));
  292. }
  293. if(!isset($parts[1])) return $content;
  294. $md_content = $markdown->md($parts[1]);
  295. /*
  296. markdown may wrap block elements
  297. in <p> tags, strip these.
  298. */
  299. $md_content = str_ireplace(array('<p><div', '</div></p>'), array('<div','</div>'), $md_content);
  300. $content['content'] = $md_content;
  301. return $content;
  302. }
  303. }
  304. /*
  305. markdown_vars()
  306. ===============
  307. */
  308. private function markdown_vars($array = false) {
  309. if(is_string($array)) {
  310. $vals = explode(': ', $array, 2);
  311. return $this->markdown_vars_create($vals[0], $vals[1]);
  312. }
  313. return false;
  314. }
  315. /*
  316. mardown_vars_create()
  317. =====================
  318. */
  319. private function markdown_vars_create($array = false, $value = null) {
  320. $keys = explode(':', $array, 2);
  321. if(count($keys) > 1) {
  322. return array($keys[0] => $this->markdown_vars_create($keys[1], $value));
  323. } else {
  324. return array($keys[0] => $value);
  325. }
  326. }
  327. /*
  328. file_is_index()
  329. ===============
  330. Checks if file is index.md/yml
  331. */
  332. public function file_is_index($file = NULL) {
  333. if(is_null($file)) return false;
  334. if(preg_match('/\/index.[yml|md]/', $file, $match)) return true;
  335. return false;
  336. }
  337. /*
  338. generate()
  339. ==========
  340. Gernerates for an xml output for the website
  341. sitemap for search engines.
  342. Arguments: none.
  343. */
  344. public function generate($type = NULL) {
  345. global $posts;
  346. if(!isset($this->{$type})) return;
  347. $post_list = $posts->collect_posts();
  348. $post_list = array_flatten($post_list);
  349. if(isset($this->config->site->{$type}->exclude)) {
  350. $exclude_list = $this->config->site->{$type}->exclude;
  351. }
  352. $return = array();
  353. foreach($post_list as $key => $post) {
  354. $continue = false;
  355. if($type != 'sitemap') {
  356. if($this->file_is_index($post)) continue;
  357. if(isset($exclude_list)) {
  358. foreach($exclude_list as $exclusion) {
  359. if(strpos($post, $exclusion) !== false) {
  360. $continue = true;
  361. break;
  362. }
  363. }
  364. }
  365. }
  366. if($continue) {
  367. continue;
  368. }
  369. $return[$key]['url'] = $posts->create_url($post);
  370. $return[$key]['data'] = $this->load_file($post);
  371. $return[$key]['data']['title'] = $posts->create_post_title(@$return[$key]['data']['title'], $post);
  372. }
  373. return $return;
  374. }
  375. // End of class.
  376. }
  377. ?>