-
Notifications
You must be signed in to change notification settings - Fork 3
/
phatso.inc
110 lines (99 loc) · 2.42 KB
/
phatso.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/**
* Phatso - A PHP Micro Framework
* Copyright (C) 2008, Judd Vinet <[email protected]>
*
* Version 0.1 :: 2008-10-03
* - initial release
* Version 0.2 :: 2009-04-30
* - optimizations by Woody Gilk <[email protected]>
* - auto-detect base web root for relative URLs
*
*/
function debug($arg) {
$args = func_get_args();
echo '<pre>';
foreach($args as $arg) {
echo '(', gettype($arg), ') ', print_r($arg, TRUE)."<br/>\n";
}
echo '</pre>';
}
class Phatso
{
var $template_layout = 'layout.php';
var $template_vars = array();
var $web_root = '';
/**
* Dispatch web request to correct function, as defined by
* URL route array.
*/
function run($urls) {
$ctrl = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
for($i = 0; $i <= strlen($ctrl); $i++) {
if($ctrl[$i] != $_SERVER['SCRIPT_NAME'][$i]) break;
}
$this->web_root = substr($ctrl, 0, $i);
$ctrl = trim(substr($ctrl, $i), '/').'/';
if($ctrl[0] != '/') $ctrl = "/$ctrl";
$action = '';
$params = array();
foreach($urls as $request=>$route) {
if(preg_match('#^'.$request.'$#', $ctrl, $matches)) {
$action = $route;
if(!empty($matches[1])) {
$params = explode('/', trim($matches[1], '/'));
}
break;
}
}
if(!function_exists("exec_{$action}")) {
$this->status('404', 'File not found');
}
call_user_func("exec_{$action}", $this, $params);
}
/**
* Set HTTP status code and exit.
*/
function status($code, $msg) {
header("{$_SERVER['SERVER_PROTOCOL']} $code");
die($msg);
}
/**
* Redirect to a new URL
*/
function redirect($url) {
$url = $_SERVER['SCRIPT_NAME'].$url;
header("Location: $url");
die;
}
/**
* Set a template variable.
*/
function set($name, $val) {
$this->template_vars[$name] = $val;
}
/**
* Render a template and return the content.
*/
function fetch($template_filename, $vars=array())
{
$vars = array_merge($this->template_vars, $vars);
ob_start();
extract($vars, EXTR_SKIP);
require 'templates/'.$template_filename;
return str_replace('/.../', $this->web_root, ob_get_clean());
}
/**
* Render a template (with optional layout) and send the
* content to the browser.
*/
function render($filename, $vars=array(), $layout='')
{
if(empty($layout)) $layout = $this->template_layout;
if($layout) {
$vars['CONTENT_FOR_LAYOUT'] = $this->fetch($filename, $vars);
$filename = $layout;
}
echo $this->fetch($filename, $vars);
}
}