pico.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. class Pico {
  3. function __construct()
  4. {
  5. // Get request url and script url
  6. $url = '';
  7. $request_url = (isset($_SERVER['REQUEST_URI'])) ? $_SERVER['REQUEST_URI'] : '';
  8. $script_url = (isset($_SERVER['PHP_SELF'])) ? $_SERVER['PHP_SELF'] : '';
  9. // Get our url path and trim the / of the left and the right
  10. if($request_url != $script_url) $url = trim(preg_replace('/'. str_replace('/', '\/', str_replace('index.php', '', $script_url)) .'/', '', $request_url, 1), '/');
  11. // Get the file path
  12. if($url) $file = strtolower(CONTENT_DIR . $url);
  13. else $file = CONTENT_DIR .'index';
  14. // Load the file
  15. if(is_dir($file)) $file = CONTENT_DIR . $url .'/index.txt';
  16. else $file .= '.txt';
  17. if(file_exists($file)) $content = file_get_contents($file);
  18. else $content = file_get_contents(CONTENT_DIR .'404.txt');
  19. $meta = $this->read_file_meta($content);
  20. $content = preg_replace('#/\*.+?\*/#s', '', $content); // Remove comments and meta
  21. $content = $this->parse_content($content);
  22. // Load the settings
  23. $settings = $this->get_config();
  24. $env = array('autoescape' => false);
  25. if($settings['enable_cache']) $env['cache'] = CACHE_DIR;
  26. // Load the theme
  27. Twig_Autoloader::register();
  28. $loader = new Twig_Loader_Filesystem(THEMES_DIR . $settings['theme']);
  29. $twig = new Twig_Environment($loader, $env);
  30. echo $twig->render('index.html', array(
  31. 'config' => $settings,
  32. 'base_dir' => rtrim(ROOT_DIR, '/'),
  33. 'base_url' => $settings['base_url'],
  34. 'theme_dir' => THEMES_DIR . $settings['theme'],
  35. 'theme_url' => $settings['base_url'] .'/'. basename(THEMES_DIR) .'/'. $settings['theme'],
  36. 'site_title' => $settings['site_title'],
  37. 'meta' => $meta,
  38. 'content' => $content
  39. ));
  40. }
  41. function parse_content($content)
  42. {
  43. $content = str_replace('%base_url%', $this->base_url(), $content);
  44. $content = Markdown($content);
  45. return $content;
  46. }
  47. function read_file_meta($content)
  48. {
  49. $headers = array(
  50. 'title' => 'Title',
  51. 'description' => 'Description',
  52. 'robots' => 'Robots'
  53. );
  54. foreach ($headers as $field => $regex){
  55. if (preg_match('/^[ \t\/*#@]*' . preg_quote($regex, '/') . ':(.*)$/mi', $content, $match) && $match[1]){
  56. $headers[ $field ] = trim(preg_replace("/\s*(?:\*\/|\?>).*/", '', $match[1]));
  57. } else {
  58. $headers[ $field ] = '';
  59. }
  60. }
  61. return $headers;
  62. }
  63. function get_config()
  64. {
  65. global $config;
  66. $defaults = array(
  67. 'site_title' => 'Pico',
  68. 'base_url' => $this->base_url(),
  69. 'theme' => 'default',
  70. 'enable_cache' => false
  71. );
  72. foreach($defaults as $key=>$val){
  73. if(isset($config[$key]) && $config[$key]) $defaults[$key] = $config[$key];
  74. }
  75. return $defaults;
  76. }
  77. function base_url()
  78. {
  79. global $config;
  80. if(isset($config['base_url']) && $config['base_url']) return $config['base_url'];
  81. $url = '';
  82. $request_url = (isset($_SERVER['REQUEST_URI'])) ? $_SERVER['REQUEST_URI'] : '';
  83. $script_url = (isset($_SERVER['PHP_SELF'])) ? $_SERVER['PHP_SELF'] : '';
  84. if($request_url != $script_url) $url = trim(preg_replace('/'. str_replace('/', '\/', str_replace('index.php', '', $script_url)) .'/', '', $request_url, 1), '/');
  85. return rtrim(str_replace($url, '', "//" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']), '/');
  86. }
  87. }
  88. ?>