1
<?php
2
/**
3
* Manager class file.
4
*
5
* @author Zhou Yuan <yuanzhou19@gmail.com>
6
* @link http://www.infopotato.com/
7
* @copyright Copyright © 2009-2012 Zhou Yuan
8
* @license http://www.opensource.org/licenses/mit-license.php MIT Licence
9
*/
10
class Manager {
11
/**
12
* Key-value array of HTTP POST parameters
13
*
14
* @var array
15
*/
16
public $_POST_DATA = array();
17
18
/**
19
* Key-value array of uploaded files info
20
*
21
* @var array
22
*/
23
public $_FILES_DATA = array();
24
25
/**
26
* Render template and return output as string
27
*
28
* If your template is located in a sub-folder, include the relative path from your templates folder.
29
*
30
* @param string $template template file path and file name
31
* @param array $template_vars (optional) template variables
32
* @return string rendered contents of template
33
*/
34
protected function render_template($template, array $template_vars = NULL) {
35
$orig_template = strtolower($template);
36
37
// Is the template in a sub-folder? If so, parse out the filename and path.
38
if (strpos($template, '/')) {
39
// str_replace is faster than preg_replace, but strtr is faster than str_replace by a factor of 4
40
$template = strtr(pathinfo($orig_template, PATHINFO_DIRNAME), '/', DS).DS.substr(strrchr($orig_template, '/'), 1);
41
}
42
43
$template_file_path = APP_TEMPLATE_DIR.$template.'.php';
44
45
if ( ! file_exists($template_file_path)) {
46
halt('A System Error Was Encountered', "Unknown template file '{$orig_template}'", 'sys_error');
47
} else {
48
if (count($template_vars) > 0) {
49
// Import the template variables to local namespace
50
// If there is a collision, overwrite the existing variable
51
// Needs to think carefully wheather to use EXTR_OVERWRITE or EXTR_SKIP
52
extract($template_vars, EXTR_OVERWRITE);
53
}
54
55
// Capture the rendered output
56
ob_start();
57
// NOTE: don't use require_once here.
58
// require_once will cause problem if the same sub template/element
59
// needs to be rendered more than once in the same scope, because require_once
60
// will not include the sub template/element file again if it has already been included
61
// so only the first call can be rendered as a result of the use of require_once
62
require $template_file_path;
63
$content = ob_get_contents();
64
ob_end_clean();
65
66
return $content;
67
}
68
}
69
70
/**
71
* Output the rendered template to browser or some requesting web services
72
*
73
* @param array $config options
74
*
75
* $config['content']: (string required) content to output
76
*
77
* $config['type']: (string required) specify the character encoding of the text document, like html, css, plain text
78
*
79
* $config['extra_headers']: (array optional) any extra headers to response
80
*
81
* $config['disable_cache']: (boolean optional) Sets the correct headers to instruct the client to not cache the response
82
*
83
* @return NULL
84
*/
85
protected function response(array $config = NULL) {
86
if (isset($config['content']) && isset($config['type'])) {
87
// Response headers
88
$headers = array();
89
90
// Common MIME types that need utf-8 charset encoding
91
$mime_types = array(
92
'text/html',
93
'text/plain',
94
'text/css',
95
'text/javascript',
96
'application/xml',
97
'application/json',
98
);
99
100
// Explicitly specify the charset parameter (utf-8) of the text document
101
// The value of charset should be case insensitive - browsers shouldn't care.
102
$headers['Content-Type'] = in_array($config['type'], $mime_types)
103
? $config['type'].'; charset=utf-8'
104
: $config['type'];
105
106
// HTTP Cache, sets the correct headers to instruct the client to not cache the response
107
if (isset($config['disable_cache']) && $config['disable_cache'] === TRUE) {
108
$headers['Expires'] = 'Mon, 26 Jul 1997 05:00:00 GMT';
109
$headers['Last-Modified'] = gmdate("D, d M Y H:i:s") . " GMT";
110
$headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0';
111
$headers['Pragma'] = 'no-cache';
112
}
113
114
// Send server response headers
115
// Like other headers, cookies must be sent before any output from your script
116
// In InfoPotato, you should use Cookie class before $this->response()
117
if (isset($config['extra_headers']) && is_array($config['extra_headers'])) {
118
$headers = array_merge($headers, $config['extra_headers']);
119
}
120
foreach ($headers as $name => $val) {
121
header($name.': '.$val);
122
}
123
124
// Output the uncompressed content
125
// You can use apache's mod_gzip module to compress the output if you want
126
echo $config['content'];
127
}
128
}
129
130
/**
131
* Data Object Loader
132
*
133
* If your data is located in a sub-folder, include the relative path from your data folder.
134
*
135
* @param string $data the name of the data class
136
* @param string $alias the optional property name alias
137
* @return boolean
138
*/
139
protected function load_data($data, $alias = '') {
140
$data = strtolower($data);
141
142
$orig_data = $data;
143
144
// Is the data in a sub-folder? If so, parse out the filename and path.
145
if (strpos($data, '/') === FALSE) {
146
$path = '';
147
} else {
148
$path = strtr(pathinfo($data, PATHINFO_DIRNAME), '/', DS).DS;
149
$data = substr(strrchr($data, '/'), 1);
150
}
151
152
// If no alias, use the data name
153
if ($alias === '') {
154
$alias = $data;
155
}
156
157
if (method_exists($this, $alias)) {
158
halt('A System Error Was Encountered', "Data name '{$alias}' is an invalid (reserved) name", 'sys_error');
159
}
160
161
// Data already loaded? silently skip
162
if ( ! isset($this->$alias)) {
163
$file_path = APP_DATA_DIR.$path.$data.'.php';
164
165
if ( ! file_exists($file_path)) {
166
halt('A System Error Was Encountered', "Unknown data file name '{$orig_data}'", 'sys_error');
167
}
168
require_once $file_path;
169
170
// Class name must be the same as the data name
171
if ( ! class_exists($data)) {
172
halt('A System Error Was Encountered', "Unknown class name '{$data}'", 'sys_error');
173
}
174
175
// Instantiate the data object as a worker's property
176
// The names of user-defined classes are case-insensitive
177
$this->{$alias} = new $data;
178
}
179
}
180
181
/**
182
* Library Class Loader
183
*
184
* This function lets users load and instantiate classes.
185
* It is designed to be called from a user's app controllers.
186
*
187
* If library is located in a sub-folder, include the relative path from libraries folder.
188
*
189
* @param string $scope 'SYS' or 'APP'
190
* @param string $library the name of the class
191
* @param string $alias (optional) alias name
192
* @param array $config the optional config parameters
193
* @return void
194
*/
195
protected function load_library($scope, $library, $alias = '', array $config = NULL) {
196
$library = strtolower($library);
197
198
$orig_library = $library;
199
200
// Is the library in a sub-folder? If so, parse out the filename and path.
201
if (strpos($library, '/') === FALSE) {
202
$path = '';
203
} else {
204
$path = strtr(pathinfo($library, PATHINFO_DIRNAME), '/', DS).DS;
205
$library = substr(strrchr($library, '/'), 1);
206
}
207
208
// If no alias, use the library name
209
if ($alias === '') {
210
$alias = $library;
211
}
212
213
if (method_exists($this, $alias)) {
214
halt('A System Error Was Encountered', "Library name '{$alias}' is an invalid (reserved) name", 'sys_error');
215
}
216
217
// Library already loaded? silently skip
218
if ( ! isset($this->$alias)) {
219
if ($scope === 'SYS') {
220
$file_path = SYS_LIBRARY_DIR.$path.$library.'.php';
221
} elseif ($scope === 'APP') {
222
$file_path = APP_LIBRARY_DIR.$path.$library.'.php';
223
} else {
224
halt('A System Error Was Encountered', "The location of the library must be specified, either 'SYS' or 'APP'", 'sys_error');
225
}
226
227
if ( ! file_exists($file_path)) {
228
halt('A System Error Was Encountered', "Unknown library file name '{$orig_library}'", 'sys_error');
229
}
230
require_once $file_path;
231
232
// Class name must be the same as the library name
233
if ( ! class_exists($library)) {
234
halt('A System Error Was Encountered', "Unknown class name '{$library}'", 'sys_error');
235
}
236
237
// Instantiate the library object as a manager's property
238
// An empty array is considered as a NULL variable
239
// The names of user-defined classes are case-insensitive
240
// Don't create static properties or methods for library
241
$this->{$alias} = new $library($config);
242
}
243
}
244
245
/**
246
* Load user-defined function
247
*
248
* If function script is located in a sub-folder, include the relative path from functions folder
249
*
250
* @param string $scope 'SYS' or 'APP'
251
* @param string $func the function script name
252
* @return void
253
*/
254
protected function load_function($scope, $func) {
255
$orig_func = strtolower($func);
256
257
// Is the script in a sub-folder? If so, parse out the filename and path.
258
if (strpos($func, '/') === FALSE) {
259
$path = '';
260
} else {
261
$path = strtr(pathinfo($func, PATHINFO_DIRNAME), '/', DS).DS;
262
$func = substr(strrchr($func, '/'), 1);
263
}
264
265
if ($scope === 'SYS') {
266
$file_path = SYS_FUNCTION_DIR.$path.$func.'.php';
267
} elseif ($scope === 'APP') {
268
$file_path = APP_FUNCTION_DIR.$path.$func.'.php';
269
} else {
270
halt('A System Error Was Encountered', "The location of the functions folder must be specified, either 'SYS' or 'APP'", 'sys_error');
271
}
272
273
if ( ! file_exists($file_path)) {
274
halt('An Error Was Encountered', "Unknown function script '{$orig_func}'", 'sys_error');
275
}
276
// The require_once() statement will check if the file has already been included,
277
// and if so, not include (require) it again
278
require_once $file_path;
279
}
280
281
}
282
283
// End of file: ./system/core/manager.php
284
Page URI: http://www.infopotato.com/index.php/code/core/manager/
