-
Notifications
You must be signed in to change notification settings - Fork 175
/
View.php
308 lines (247 loc) · 9.29 KB
/
View.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php
/**
* @package ImpressPages
* @copyright Copyright (C) 2011 ImpressPages LTD.
* @license GNU/GPL, see ip_license.html
*/
namespace Ip;
if (!defined('CMS')) exit;
/**
*
* View class
*
*/
class View{
const DOCTYPE_XHTML1_STRICT = 1;
const DOCTYPE_XHTML1_TRANSITIONAL = 2;
const DOCTYPE_XHTML1_FRAMESET = 3;
const DOCTYPE_HTML4_STRICT = 4;
const DOCTYPE_HTML4_TRANSITIONAL = 5;
const DOCTYPE_HTML4_FRAMESET = 6;
const DOCTYPE_HTML5 = 7;
private $file;
private $data;
private $doctype;
/**
*
* Create view file
* @param string $file
* @param array $data
*/
private function __construct($file, $data = array()) {
$this->file = $file;
$this->data = $data;
eval('$this->doctype = self::'.DEFAULT_DOCTYPE.';');
}
/**
*
* Create new view object with the same doctype, but different view file and data
* Use it to include another view file within the view
* @param string $file
* @param array $data
*/
public function subview($file, $data = array()) {
$foundFile = self::findView($file);
self::checkData($data);
$view = new \Ip\View($foundFile, $data);
$view->setDoctype($this->getDoctype());
return $view;
}
public static function create($file, $data = array()) {
$foundFile = self::findView($file);
self::checkData($data);
return new \Ip\View($foundFile, $data);
}
public function renderWidget($widgetName, $data = array(), $layout = null) {
require_once(BASE_DIR.MODULE_DIR.'standard/content_management/model.php');
$answer = \Modules\standard\content_management\Model::generateWidgetPreviewFromStaticData($widgetName, $data, $layout);
return $answer;
}
/**
* Escape and echo text
* @param string $text
*/
public function esc($text, $variables = null){
if (!empty($variables) && is_array($variables)) {
foreach($variables as $variableKey => $variableValue) {
$text = str_replace('[[' . $variableKey . ']]', $variableValue, $text);
}
}
return htmlspecialchars($text);
}
/**
* Escape and echo parameter
* @param string $parameterKey
*/
public function escPar($parameterKey, $variables = null){
return $this->esc($this->par($parameterKey), $variables);
}
public function par($parameterKey, $variables = null){
global $parametersMod;
$parts = explode('/', $parameterKey);
if (count($parts) != 4) {
if (DEVELOPMENT_ENVIRONMENT) {
throw new \Ip\CoreException("Can't find parameter: '" . $parameterKey . "'", \Ip\CoreException::VIEW);
} else {
return '';
}
}
$value = $parametersMod->getValue($parts[0], $parts[1], $parts[2], $parts[3]);
if (!empty($variables) && is_array($variables)) {
foreach($variables as $variableKey => $variableValue) {
$value = str_replace('[[' . $variableKey . ']]', $variableValue, $value);
}
}
return $value;
}
/**
*
* Set view data
* @param array $data
*/
public function setData($data) {
$this->data = $data;
}
public function getData() {
return $this->data;
}
public function render () {
global $site;
global $log;
global $dispatcher;
global $parametersMod;
global $session;
foreach ($this->data as $key => $value) {
eval(' $'.$key.' = $value;');
}
$found = false;
ob_start();
require ($this->file); //file existance has been checked in constructor
$output = ob_get_contents();
ob_end_clean();
return $output;
}
public function __toString()
{
return $this->render();
}
public function setDoctype ($doctype) {
$this->doctype = $doctype;
}
public function getDoctype () {
return $this->doctype;
}
public function doctypeDeclaration($doctype = null) {
if ($doctype === null) {
$doctype = $this->getDoctype();
}
switch ($doctype) {
case self::DOCTYPE_XHTML1_STRICT:
return '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
break;
case self::DOCTYPE_XHTML1_TRANSITIONAL:
return '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
break;
case self::DOCTYPE_XHTML1_FRAMESET:
return '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">';
break;
case self::DOCTYPE_HTML4_STRICT:
return '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">';
break;
case self::DOCTYPE_HTML4_TRANSITIONAL:
return '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">';
break;
case self::DOCTYPE_HTML4_FRAMESET:
return '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">';
break;
case self::DOCTYPE_HTML5:
return '<!DOCTYPE html>';
break;
default:
throw new CoreException('Unknown doctype: '.$doctype, CoreException::VIEW);
}
}
public function htmlAttributes($doctype = null) {
global $site;
if ($doctype === null) {
$doctype = $this->getDoctype();
}
switch ($doctype) {
case self::DOCTYPE_XHTML1_STRICT:
case self::DOCTYPE_XHTML1_TRANSITIONAL:
case self::DOCTYPE_XHTML1_FRAMESET:
$lang = $site->getCurrentLanguage()->getCode();
return ' xmlns="http://www.w3.org/1999/xhtml" xml:lang="'.$lang.'" lang="'.$lang.'"';
break;
case self::DOCTYPE_HTML4_STRICT:
case self::DOCTYPE_HTML4_TRANSITIONAL:
case self::DOCTYPE_HTML4_FRAMESET:
default:
return '';
break;
case self::DOCTYPE_HTML5:
$lang = $site->getCurrentLanguage()->getCode();
return ' lang="'.$lang.'"';
break;
}
}
private static function findView($file) {
$backtrace = debug_backtrace();
if(!isset($backtrace[1]['file']) || !isset($backtrace[1]['line'])) {
throw new CoreException("Can't find caller", CoreException::VIEW);
}
$sourceFile = $backtrace[1]['file'];
if (DIRECTORY_SEPARATOR != '/') {
$sourceFile = str_replace(DIRECTORY_SEPARATOR, '/', $sourceFile);
}
$foundFile = self::findFile($file, $sourceFile);
if ($foundFile === false) {
throw new CoreException('Can\'t find view file \''.$file. '\' (Error source: '.$backtrace[1]['file'].' line: '.$backtrace[1]['line'].' )', CoreException::VIEW);
}
return $foundFile;
}
private static function findFile($file, $sourceFile) {
if (strpos($file, BASE_DIR) !== 0) {
$file = dirname($sourceFile).'/'.$file;
}
$moduleView = ''; //relative link to view according to modules root.
if (strpos($file, BASE_DIR.MODULE_DIR) === 0) {
$moduleView = substr($file, strlen(BASE_DIR.MODULE_DIR));
}
if ($moduleView == '' && strpos($file, BASE_DIR.PLUGIN_DIR) === 0) {
$moduleView = substr($file, strlen(BASE_DIR.PLUGIN_DIR));
}
if ($moduleView == '' && strpos($file, BASE_DIR.THEME_DIR.'modules/') === 0) {
$moduleView = substr($file, strlen(BASE_DIR.THEME_DIR.'modules/'));
}
if ($moduleView != '') {
if (file_exists(BASE_DIR.THEME_DIR.THEME.'/modules/'.$moduleView)) {
return BASE_DIR.THEME_DIR.THEME.'/modules/'.$moduleView;
}
if (file_exists(BASE_DIR.PLUGIN_DIR.$moduleView)) {
return(BASE_DIR.PLUGIN_DIR.$moduleView);
}
if (file_exists(BASE_DIR.MODULE_DIR.$moduleView)) {
return(BASE_DIR.MODULE_DIR.$moduleView);
}
} else {
if (file_exists($file)) {
return $file;
} else {
return false;
}
}
return false;
}
private static function checkData ($data) {
foreach ($data as $key => $value) {
if (! preg_match('/^[a-zA-Z0-9_-]+$/', $key) || $key == '') {
$source = '';
if(isset($backtrace[0]['file']) && $backtrace[0]['line']) {
$source = "(Error source: ".($backtrace[0]['file'])." line: ".($backtrace[0]['line'])." ) ";
}
throw new CoreException("Incorrect view variable name '".$key."' ".$source, CoreException::VIEW);
}
}
}
}