Guest User

Untitled

a guest
May 27th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.29 KB | None | 0 0
  1. <?php
  2. /**
  3. * txt2tags-web
  4. *
  5. * A very small framework that let you build the static parts of your site
  6. * with t2t files without stress. ;-)
  7. *
  8. * The only requirement is a PHP environment.
  9. * It uses Google AppEngine to convert the t2t files to HTML. The files are
  10. * converted just on the first request and are cached for futher use. You
  11. * change the t2t files and just see the result on browser.
  12. *
  13. * @package txt2tags-web
  14. * @author Felipe Oliveira Carvalho
  15. * @copyright Copyright (c) 2008, Felipe Oliveira Carvalho
  16. * @license MIT License
  17. * @link
  18. */
  19.  
  20. /*
  21. |---------------------------------------------------------------
  22. | BASE URI
  23. |---------------------------------------------------------------
  24. |
  25. | URI to the folder of this file.
  26. | WITH trailing slash:
  27. |
  28. | /
  29. | /my_site/
  30. |
  31. */
  32. $BASE_URI = "/txt2tags-web/";
  33.  
  34. $use_htaccess = TRUE;
  35. $URL_SUFFIX = 't2t';
  36. //$show_the_t2t_files = TRUE;
  37. $common_request_trigger = '__common_request_';
  38.  
  39. // -------------------------------------------------------------
  40.  
  41. /**
  42. * URI Class - Based on CodeIgniter URI_Class
  43. *
  44. * Parses URIs
  45. */
  46. class URI
  47. {
  48. public $base_uri;
  49. public $uri_string;
  50. public $query_string = '';
  51. public $segments = array();
  52.  
  53. function URI()
  54. {
  55. // Sets the BASE_URI
  56. $this->_uri_safe_cpy($this->base_uri, $GLOBALS['BASE_URI']);
  57.  
  58. // Gets the uri_string
  59. $this->uri_string = $this->_parse_request_uri();
  60. }
  61.  
  62. /**
  63. * Parse the REQUEST_URI
  64. *
  65. * Removes the base_uri and the query_string.
  66. */
  67. private function _parse_request_uri()
  68. {
  69. if ( ! isset($_SERVER['REQUEST_URI']) OR $_SERVER['REQUEST_URI'] == '')
  70. return '';
  71.  
  72. $this->_uri_safe_cpy($request_uri, $_SERVER['REQUEST_URI']);
  73.  
  74. if ($request_uri == '')
  75. return '';
  76.  
  77. // Grab the Query String
  78. if (strpos($request_uri, '?')) {
  79. $pieces = explode('?', $request_uri);
  80. $request_uri = $pieces[0];
  81. $query_string = $pieces[1];
  82. /* Removes the unecessary & */
  83. if ($query_string != '' AND $query_string[strlen($query_string) - 1] == '&')
  84. $this->query_string = substr($query_string, 0, -1);
  85. }
  86.  
  87. // Explode the URI in segments
  88. $this->_explode_segments($request_uri);
  89. $segments = &$this->segments;
  90.  
  91. // Removes the base_uri from the uri_string
  92. $i = 0;
  93. foreach(explode("/", $this->base_uri) as $segment) {
  94. if (isset($segments[$i]) AND $segment == $segment[$i])
  95. $i++;
  96. }
  97. // Removes the "index.php" from the URI if it exists
  98. if (isset($segments[$i] ) AND basename(__FILE__) == $segments[$i])
  99. $i++;
  100. $segments = array_slice($segments, $i);
  101.  
  102. return implode('/', $segments);
  103. }
  104.  
  105. /* Safe copy for URIs */
  106. private function _uri_safe_cpy(&$destinaton, $source)
  107. {
  108. $destinaton = preg_replace("|/(.*)|", "\\1", str_replace("\\", "/", $source));
  109. }
  110.  
  111. /* Filter segments for malicious characters */
  112. private function _filter_uri($str)
  113. {
  114. if ($str != ''
  115. AND ! preg_match("|^[a-z 0-9\?&\=~%\.\:_-]+$|i", $str)) {
  116. exit('The URI you submitted has disallowed characters.');
  117. }
  118.  
  119. return $str;
  120. }
  121.  
  122. /**
  123. * Explode the URI Segments. The individual segments will
  124. * be stored in the $this->segments array.
  125. */
  126. private function _explode_segments($request_uri)
  127. {
  128. foreach(explode("/", preg_replace("|/*(.+?)/*$|", "\\1", $request_uri)) as $val) {
  129. // Filter segments for security
  130. $val = trim($this->_filter_uri($val));
  131.  
  132. if ($val != '')
  133. $this->segments[] = $val;
  134. }
  135. }
  136.  
  137. /* Returns the last segment of the URI */
  138. function basename()
  139. {
  140. return end($this->segments);
  141. }
  142.  
  143. /* Grab the file extension of the URI */
  144. function get_file_extension()
  145. {
  146. $x = explode('.', $this->basename());
  147. if (count($x) > 0)
  148. return end($x);
  149. return '';
  150. }
  151.  
  152. /* Returns the entire Request URI without index.php inside */
  153. function get_request_uri()
  154. {
  155. return '/'.$this->base_uri.$this->uri_string.$this->query_string;
  156. }
  157. }
  158.  
  159. /**
  160. * Request class
  161. *
  162. * This class receives an URI obect and performs the request.
  163. */
  164. class Request
  165. {
  166. public $uri;
  167.  
  168. function Request(URI $uri)
  169. {
  170. $this->uri = $uri;
  171.  
  172. $uri_string = $uri->uri_string;
  173. if ($uri_string == '') {
  174. if (file_exists('./index.t2t')) {
  175. $this->render_t2t_file_as_html('./index.t2t');
  176. } else {
  177. /* Avoiding infinite redirect */
  178. if (__FILE__ == 'index.php')
  179. exit('index.t2t Not Found');
  180. else
  181. $this->common_request();
  182. }
  183. } else {
  184. $extension = $uri->get_file_extension();
  185. if ($extension == 'html' ) {
  186. $this->html_request();
  187. } else if ($extension == 't2t') {
  188. $this->t2t_request();
  189. } else if (@is_dir($uri->uri_string)) {
  190. $this->directory_request();
  191. } else {
  192. $this->common_request();
  193. }
  194. }
  195. }
  196.  
  197. /**
  198. * Looks for a t2t file with the same name of the HTML file in URI and
  199. * render it converted to HTML. If the HTML file exists it will be rendered.
  200. */
  201. private function html_request()
  202. {
  203. if (file_exists($this->uri->uri_string)) {
  204. $this->render_html_file($this->uri->uri_string);
  205. } else {
  206. /* Try to render the file using the extension .t2t instead .html */
  207. $file = substr($this->uri->uri_string, 0, -4);
  208. $file .= 't2t';
  209. if (file_exists($file)) {
  210. $this->render_t2t_file_as_html($file);
  211. } else if (@is_dir($this->uri->uri_string)) {
  212. $this->directory_request();
  213. } else {
  214. $this->show_404();
  215. }
  216. }
  217. }
  218.  
  219. private function t2t_request()
  220. {
  221. if (file_exists($this->uri->uri_string) AND $GLOBALS['show_the_t2t_files']) {
  222. echo file_get_contents($this->uri->uri_string);
  223. } else {
  224. $this->show_404();
  225. }
  226. }
  227.  
  228. /* Looks for a index.t2t -> index.html file */
  229. private function directory_request()
  230. {
  231. $dir = './'.$this->uri->uri_string;
  232. if (file_exists($dir.'/index.html')) {
  233. $this->render_html_file($dir.'/index.html');
  234. } else if (file_exists($dir.'/index.t2t')) {
  235. $this->render_t2t_file_as_html($dir.'/index.t2t');
  236. } else if ($dir == './') {
  237. /* Avoiding Infinite Redirect on root dir */
  238. $this->show_404();
  239. } else {
  240. $this->common_request();
  241. }
  242. }
  243.  
  244. /**
  245. * The common behavior for a request.
  246. * Just let the Web Server care about the request.
  247. */
  248. private function common_request()
  249. {
  250. $request_uri = $this->uri->get_request_uri();
  251.  
  252. /* If the user uses .htaccess to redirect things to index.php we have to
  253. * tell the .htaccess to not redirect. We use common_request_trigger as a
  254. * variable in the Query String */
  255. if ($GLOBALS['use_htaccess']) {
  256. $query_string = $this->uri->query_string;
  257. if ($query_string == '')
  258. $request_uri .= '?';
  259. else
  260. $request_uri .= '&';
  261. $request_uri .= $GLOBALS['common_request_trigger'];
  262. }
  263.  
  264. header('Location: '.$request_uri);
  265. }
  266.  
  267. // -------------------------------------------------------------
  268.  
  269. private function render_html_file($file)
  270. {
  271. echo file_get_contents($file);
  272. }
  273.  
  274. /* The magic! */
  275. private function render_t2t_file_as_html($file)
  276. {
  277. echo 't2t converted';
  278. return;
  279. $txt2tags = new Txt2tags($file);
  280. echo $txt2tags->get_html();
  281. }
  282.  
  283. // -------------------------------------------------------------
  284.  
  285. private function show_404()
  286. {
  287. header("HTTP/1.1 404 Not Found");
  288. exit("404 Not Found");
  289. }
  290. }
  291.  
  292. /* Here we go! */
  293. function main()
  294. {
  295. $uri = new URI();
  296. new Request(&$uri);
  297. //print_r($uri);
  298. }
  299.  
  300. main();
  301. ?>
Add Comment
Please, Sign In to add comment