Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * txt2tags-web
- *
- * A very small framework that let you build the static parts of your site
- * with t2t files without stress. ;-)
- *
- * The only requirement is a PHP environment.
- * It uses Google AppEngine to convert the t2t files to HTML. The files are
- * converted just on the first request and are cached for futher use. You
- * change the t2t files and just see the result on browser.
- *
- * @package txt2tags-web
- * @author Felipe Oliveira Carvalho
- * @copyright Copyright (c) 2008, Felipe Oliveira Carvalho
- * @license MIT License
- * @link
- */
- /*
- |---------------------------------------------------------------
- | BASE URI
- |---------------------------------------------------------------
- |
- | URI to the folder of this file.
- | WITH trailing slash:
- |
- | /
- | /my_site/
- |
- */
- $BASE_URI = "/txt2tags-web/";
- $use_htaccess = TRUE;
- $URL_SUFFIX = 't2t';
- //$show_the_t2t_files = TRUE;
- $common_request_trigger = '__common_request_';
- // -------------------------------------------------------------
- /**
- * URI Class - Based on CodeIgniter URI_Class
- *
- * Parses URIs
- */
- class URI
- {
- public $base_uri;
- public $uri_string;
- public $query_string = '';
- public $segments = array();
- function URI()
- {
- // Sets the BASE_URI
- $this->_uri_safe_cpy($this->base_uri, $GLOBALS['BASE_URI']);
- // Gets the uri_string
- $this->uri_string = $this->_parse_request_uri();
- }
- /**
- * Parse the REQUEST_URI
- *
- * Removes the base_uri and the query_string.
- */
- private function _parse_request_uri()
- {
- if ( ! isset($_SERVER['REQUEST_URI']) OR $_SERVER['REQUEST_URI'] == '')
- return '';
- $this->_uri_safe_cpy($request_uri, $_SERVER['REQUEST_URI']);
- if ($request_uri == '')
- return '';
- // Grab the Query String
- if (strpos($request_uri, '?')) {
- $pieces = explode('?', $request_uri);
- $request_uri = $pieces[0];
- $query_string = $pieces[1];
- /* Removes the unecessary & */
- if ($query_string != '' AND $query_string[strlen($query_string) - 1] == '&')
- $this->query_string = substr($query_string, 0, -1);
- }
- // Explode the URI in segments
- $this->_explode_segments($request_uri);
- $segments = &$this->segments;
- // Removes the base_uri from the uri_string
- $i = 0;
- foreach(explode("/", $this->base_uri) as $segment) {
- if (isset($segments[$i]) AND $segment == $segment[$i])
- $i++;
- }
- // Removes the "index.php" from the URI if it exists
- if (isset($segments[$i] ) AND basename(__FILE__) == $segments[$i])
- $i++;
- $segments = array_slice($segments, $i);
- return implode('/', $segments);
- }
- /* Safe copy for URIs */
- private function _uri_safe_cpy(&$destinaton, $source)
- {
- $destinaton = preg_replace("|/(.*)|", "\\1", str_replace("\\", "/", $source));
- }
- /* Filter segments for malicious characters */
- private function _filter_uri($str)
- {
- if ($str != ''
- AND ! preg_match("|^[a-z 0-9\?&\=~%\.\:_-]+$|i", $str)) {
- exit('The URI you submitted has disallowed characters.');
- }
- return $str;
- }
- /**
- * Explode the URI Segments. The individual segments will
- * be stored in the $this->segments array.
- */
- private function _explode_segments($request_uri)
- {
- foreach(explode("/", preg_replace("|/*(.+?)/*$|", "\\1", $request_uri)) as $val) {
- // Filter segments for security
- $val = trim($this->_filter_uri($val));
- if ($val != '')
- $this->segments[] = $val;
- }
- }
- /* Returns the last segment of the URI */
- function basename()
- {
- return end($this->segments);
- }
- /* Grab the file extension of the URI */
- function get_file_extension()
- {
- $x = explode('.', $this->basename());
- if (count($x) > 0)
- return end($x);
- return '';
- }
- /* Returns the entire Request URI without index.php inside */
- function get_request_uri()
- {
- return '/'.$this->base_uri.$this->uri_string.$this->query_string;
- }
- }
- /**
- * Request class
- *
- * This class receives an URI obect and performs the request.
- */
- class Request
- {
- public $uri;
- function Request(URI $uri)
- {
- $this->uri = $uri;
- $uri_string = $uri->uri_string;
- if ($uri_string == '') {
- if (file_exists('./index.t2t')) {
- $this->render_t2t_file_as_html('./index.t2t');
- } else {
- /* Avoiding infinite redirect */
- if (__FILE__ == 'index.php')
- exit('index.t2t Not Found');
- else
- $this->common_request();
- }
- } else {
- $extension = $uri->get_file_extension();
- if ($extension == 'html' ) {
- $this->html_request();
- } else if ($extension == 't2t') {
- $this->t2t_request();
- } else if (@is_dir($uri->uri_string)) {
- $this->directory_request();
- } else {
- $this->common_request();
- }
- }
- }
- /**
- * Looks for a t2t file with the same name of the HTML file in URI and
- * render it converted to HTML. If the HTML file exists it will be rendered.
- */
- private function html_request()
- {
- if (file_exists($this->uri->uri_string)) {
- $this->render_html_file($this->uri->uri_string);
- } else {
- /* Try to render the file using the extension .t2t instead .html */
- $file = substr($this->uri->uri_string, 0, -4);
- $file .= 't2t';
- if (file_exists($file)) {
- $this->render_t2t_file_as_html($file);
- } else if (@is_dir($this->uri->uri_string)) {
- $this->directory_request();
- } else {
- $this->show_404();
- }
- }
- }
- private function t2t_request()
- {
- if (file_exists($this->uri->uri_string) AND $GLOBALS['show_the_t2t_files']) {
- echo file_get_contents($this->uri->uri_string);
- } else {
- $this->show_404();
- }
- }
- /* Looks for a index.t2t -> index.html file */
- private function directory_request()
- {
- $dir = './'.$this->uri->uri_string;
- if (file_exists($dir.'/index.html')) {
- $this->render_html_file($dir.'/index.html');
- } else if (file_exists($dir.'/index.t2t')) {
- $this->render_t2t_file_as_html($dir.'/index.t2t');
- } else if ($dir == './') {
- /* Avoiding Infinite Redirect on root dir */
- $this->show_404();
- } else {
- $this->common_request();
- }
- }
- /**
- * The common behavior for a request.
- * Just let the Web Server care about the request.
- */
- private function common_request()
- {
- $request_uri = $this->uri->get_request_uri();
- /* If the user uses .htaccess to redirect things to index.php we have to
- * tell the .htaccess to not redirect. We use common_request_trigger as a
- * variable in the Query String */
- if ($GLOBALS['use_htaccess']) {
- $query_string = $this->uri->query_string;
- if ($query_string == '')
- $request_uri .= '?';
- else
- $request_uri .= '&';
- $request_uri .= $GLOBALS['common_request_trigger'];
- }
- header('Location: '.$request_uri);
- }
- // -------------------------------------------------------------
- private function render_html_file($file)
- {
- echo file_get_contents($file);
- }
- /* The magic! */
- private function render_t2t_file_as_html($file)
- {
- echo 't2t converted';
- return;
- $txt2tags = new Txt2tags($file);
- echo $txt2tags->get_html();
- }
- // -------------------------------------------------------------
- private function show_404()
- {
- header("HTTP/1.1 404 Not Found");
- exit("404 Not Found");
- }
- }
- /* Here we go! */
- function main()
- {
- $uri = new URI();
- new Request(&$uri);
- //print_r($uri);
- }
- main();
- ?>
Add Comment
Please, Sign In to add comment