-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.php
More file actions
145 lines (106 loc) · 4.46 KB
/
Copy pathapp.php
File metadata and controls
145 lines (106 loc) · 4.46 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
class BG { // this is a singleton CLASS
const VERSION = '1.0';
const DS = DIRECTORY_SEPARATOR;
public $version;
public $baseUrl;
public $basePath;
public $hostname;
public $page; // the permalink page name used to select a custom page function
public $pageTitle;
public $config; // an object that abstracts the config array
private static $_app; // holds the singleton class instantiation
private $_pageFile;
private $_protectedPath;
private $_pagesPath;
public static function createWebApplication($configFilePath) {
if ( !self::$_app ) self::$_app = new BG($configFilePath);
return self::$_app;
}
public static function app() { return self::$_app; }
private function __construct($configFilePath) {
// ToDo: validate $configFilePath and the array that is returned, else ERROR
$config = include $configFilePath;
$this->config = json_decode(json_encode($config)); // create an object from the config array
$config = $this->config;
$this->version = self::VERSION;
$this->hostname = $_SERVER['SERVER_NAME'];
$this->basePath = preg_replace('/(.*)[\/\\\]protected[\/\\\]config[\/\\\]\w+\.php$/','$1',$configFilePath);
$this->baseUrl = preg_replace('/(.*)\/index.php$/','$1',$_SERVER['PHP_SELF']);
$this->_protectedPath = $this->basePath.self::DS.'protected';
$this->_pagesPath = $this->_protectedPath.self::DS.'pages';
// parse the page name from the url
$regex = str_replace('/','\/',strtolower($this->baseUrl)); // change /kevin/home to \/kevin\/home
$this->page = preg_replace('/'.$regex.'(.*)$/','$1',strtolower($_SERVER['REQUEST_URI']));
$this->page = substr($this->page,1); // remove the "/" prefix
if ( !strlen($this->page) ) $this->page = strtolower($config->homePage);
// check if a page file of this name exists in the pages directory, else 404
$this->_pageFile = $this->_pagesPath.self::DS.$this->page.'.php';
if ( !is_file($this->_pageFile) ) {
$this->_pageFile = $this->_pagesPath.self::DS.'404.php';
$this->page = '404';
header('HTTP/1.0 404 Not Found');
}
$this->page = ucfirst($this->page); // change about to About
$this->pageTitle = (strlen($config->siteName) ? $config->siteName.' : ' : '').$this->page;
}
public function buildPage() {
//echo '<pre>'.print_r(self::app(),true).'</pre>';
include_once $this->_pageFile;
}
public function get_header($filename='header.php') {
include_once $this->_protectedPath.self::DS.$filename;
}
public function get_footer($filename='footer.php') {
include_once $this->_protectedPath.self::DS.$filename;
}
public function link($text, $url, $htmlOptions=NULL) {
$url = preg_match('/^http.*/',$url) ? $url : $this->baseUrl.$url;
return '<a href="'.$url.'">'.$text.'</a>';
}
public function jsParams() { // this object is passed to bgUI.js in a data-json attribute in the html tag
return json_encode(array(
'baseUrl' => $this->baseUrl,
'host' => $this->hostname,
'codeStage' => $this->config->codeStage,
'googleAnalytics' => array(
'id'=>$this->config->GoogleAnalytics->id,
'activate' => $this->config->GoogleAnalytics->activate ? 'true' : 'false',
),
));
}
public function html($category, $options=NULL) {
if ( $category == 'widget' ) return self::widgetHtml($options);
else return '';
}
private static function widgetHtml($meta) {
/* returns the following structure
<div class="bg-widget sand">
<h2 class="blue">Widget Title</h2>
<div class="widgetContent padded">
<p>Here is some sample widget content.</p>
</div>
</div>
*/
$defaultMeta = array(
'height'=>'auto',
'padded'=>FALSE,
'class'=>'',
'title'=>'Title',
'titleBackgroundColor'=>'blue',
'widgetBackgroundColor'=>'sand',
'imageFilename'=>false,
'content'=>'Content',
);
$meta = array_merge($defaultMeta,$meta);
$height = $meta['height']=='auto' ? '' : 'height:'.$meta['height'].'px;';
$padded = $meta['padded']==TRUE ? 'padded' : '';
$class = strlen($meta['class']) ? $meta['class'] : '';
$html = array();
$html[] = '<div class="bg-widget '.$meta['widgetBackgroundColor'].' '.$class.'" style="'.$height.'">';
$html[] = '<h2 class="'.$meta['titleBackgroundColor'].'">'.$meta['title'].'</h2>';
$html[] = '<div class="widgetContent '.$padded.'">'.$meta['content'].'</div>';
$html[] = '</div>';
return implode("\n",$html);
}
} // BG class