Browse Source

Merge 6a3ff6d491a6065222b99795308255f88b961cf2 into df4d37bf13dd0de5d1aff933005dfd4616ee9167

Mathieu Rochette 12 years ago
parent
commit
0e52649a60
1 changed files with 29 additions and 8 deletions
  1. 29 8
      lib/pico.php

+ 29 - 8
lib/pico.php

@@ -12,13 +12,29 @@ class Pico {
 		// Get our url path and trim the / of the left and the right
 		// Get our url path and trim the / of the left and the right
 		if($request_url != $script_url) $url = trim(preg_replace('/'. str_replace('/', '\/', str_replace('index.php', '', $script_url)) .'/', '', $request_url, 1), '/');
 		if($request_url != $script_url) $url = trim(preg_replace('/'. str_replace('/', '\/', str_replace('index.php', '', $script_url)) .'/', '', $request_url, 1), '/');
 
 
+		// Load the settings
+		$settings = $this->get_config();
+		$env = array('autoescape' => false);
+		if($settings['enable_cache']) $env['cache'] = CACHE_DIR;
+
+		if($settings['draft_auth'] !== false && strpos($url, '?draft') - strlen($url) + 6 === 0) $ext = '.draft';
+		else $ext = '.txt';
+
+		if($ext === '.draft') {
+			if(!$this->draft_auth($settings['draft_auth'])) {
+				header('WWW-Authenticate: Basic realm="'.$settings['site_title'].'"', true, 401);
+				exit;
+			}
+			$url = substr($url, 0, strlen($url) - 6);
+		}
+
 		// Get the file path
 		// Get the file path
 		if($url) $file = strtolower(CONTENT_DIR . $url);
 		if($url) $file = strtolower(CONTENT_DIR . $url);
 		else $file = CONTENT_DIR .'index';
 		else $file = CONTENT_DIR .'index';
 
 
 		// Load the file
 		// Load the file
-		if(is_dir($file)) $file = CONTENT_DIR . $url .'/index.txt';
-		else $file .= '.txt';
+		if(is_dir($file)) $file = CONTENT_DIR . $url . '/index.' . $ext;
+		else $file .= $ext;
 
 
 		if(file_exists($file)) $content = file_get_contents($file);
 		if(file_exists($file)) $content = file_get_contents($file);
 		else $content = file_get_contents(CONTENT_DIR .'404.txt');
 		else $content = file_get_contents(CONTENT_DIR .'404.txt');
@@ -26,11 +42,6 @@ class Pico {
 		$meta = $this->read_file_meta($content);
 		$meta = $this->read_file_meta($content);
 		$content = preg_replace('#/\*.+?\*/#s', '', $content); // Remove comments and meta
 		$content = preg_replace('#/\*.+?\*/#s', '', $content); // Remove comments and meta
 		$content = $this->parse_content($content);
 		$content = $this->parse_content($content);
-
-		// Load the settings
-		$settings = $this->get_config();
-		$env = array('autoescape' => false);
-		if($settings['enable_cache']) $env['cache'] = CACHE_DIR;
 		
 		
 		// Load the theme
 		// Load the theme
 		Twig_Autoloader::register();
 		Twig_Autoloader::register();
@@ -48,6 +59,15 @@ class Pico {
 		));
 		));
 	}
 	}
 
 
+	function draft_auth($credentials) {
+		if(!isset($_SERVER['HTTP_AUTHORIZATION']))
+		{
+			return false;
+		}
+
+		return $_SERVER['HTTP_AUTHORIZATION'] === 'Basic '.base64_encode($credentials);
+	}
+
 	function parse_content($content)
 	function parse_content($content)
 	{
 	{
 		$content = str_replace('%base_url%', $this->base_url(), $content);
 		$content = str_replace('%base_url%', $this->base_url(), $content);
@@ -83,7 +103,8 @@ class Pico {
 			'site_title' => 'Pico',
 			'site_title' => 'Pico',
 			'base_url' => $this->base_url(),
 			'base_url' => $this->base_url(),
 			'theme' => 'default',
 			'theme' => 'default',
-			'enable_cache' => false
+			'enable_cache' => false,
+			'draft_auth' => false
 		);
 		);
 
 
 		foreach($defaults as $key=>$val){
 		foreach($defaults as $key=>$val){