-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.php
84 lines (73 loc) · 2.46 KB
/
app.php
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
<?php
/**
* 1. Setup
* 2. Initialize Smarty object
* 3. Interpret URL and load template.
*/
// 1a. Setup enviroment
//error_reporting(E_ERROR | E_WARNING | E_PARSE);
error_reporting(E_ALL|E_STRICT);
ini_set('display_errors', '1');
// Remember path to the app implementation directory
$appPath = dirname(__FILE__).DIRECTORY_SEPARATOR;
// Find PHP classes.
set_include_path($appPath.'../Smarty'); // Include Smarty files
spl_autoload_extensions(spl_autoload_extensions().',.class.php');
spl_autoload_register();
// 1b. We're doing Smarty
// Smarty oversite, in case this isn't set.
date_default_timezone_set('America/Los_Angeles');
$smarty = new Smarty();
// In case we want to put tempates in a subdir, called templates
if (is_dir($appPath.'templates'))
$smarty->addTemplateDir($appPath.'templates');
$smarty->addTemplateDir($appPath);
// $smarty->setConfigDir($appPath.'configs/');
// Set up working directory: $appVarPath
$umask = umask(0); // Allow access by diff user
$appVarPath = $appPath.'var'.DIRECTORY_SEPARATOR;
if (!is_dir($appVarPath))
mkdir($appVarPath, 0775);
if (!is_dir($appVarPath.'templates_c/'))
mkdir($appVarPath.'templates_c/', 0775);
umask($umask);
$smarty->setCompileDir($appVarPath.'templates_c/');
// $smarty->setCacheDir($appVarPath.'cache/');
// [END] Setup
// 2. Setup request state
if (!isset($_SERVER['SCRIPT_URL']))
$_SERVER['SCRIPT_URL'] = $_SERVER['QUERY_STRING']
? substr($_SERVER['REQUEST_URI'],0,-strlen($_SERVER['QUERY_STRING'])-1)
: $_SERVER['REQUEST_URI'];
// Get the URL to the app's root
$urlRootPath = dirname($_SERVER['SCRIPT_NAME']);
if ($urlRootPath != '/') // Suffix w/'/'
$urlRootPath .= '/';
// The part of the path after the URL root
$urlOffset = strpos($_SERVER['SCRIPT_URL'],$urlRootPath) === 0
? substr($_SERVER['SCRIPT_URL'], strlen($urlRootPath))
: $_SERVER['SCRIPT_URL'];
$urlComponenets = explode('/',$urlOffset);
if (!isset($urlComponenets[0]) || $urlComponenets[0] == '') // Default
$urlComponenets[0] = 'index';
// 3. Display smarty
// Determine template name based on URL
$template = $urlComponenets[0].'.tpl';
if (stream_resolve_include_path($urlComponenets[0].'.php'))
{
include $urlComponenets[0].'.php';
}
elseif ($smarty->templateExists($template))
{
// $smarty->assign('name','Ned');
$smarty->display($template);
}
else
{
header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found', TRUE, 404);
echo "$template not found.";
}
// echo '<pre>';
// print_r($_SERVER);
// print_r($_ENV);
// echo '</pre>';