|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Contains theme specific tools |
|
5
|
|
|
* |
|
6
|
|
|
* PHP Version 5 |
|
7
|
|
|
* |
|
8
|
|
|
* @category Core |
|
9
|
|
|
* @package Template |
|
10
|
|
|
* @author Hans-Joachim Piepereit <[email protected]> |
|
11
|
|
|
* @copyright 2013 cSphere Team |
|
12
|
|
|
* @license http://opensource.org/licenses/bsd-license Simplified BSD License |
|
13
|
|
|
* @link http://www.csphere.eu |
|
14
|
|
|
**/ |
|
15
|
|
|
|
|
16
|
|
|
namespace csphere\core\template; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Contains theme specific tools |
|
20
|
|
|
* |
|
21
|
|
|
* @category Core |
|
22
|
|
|
* @package Template |
|
23
|
|
|
* @author Hans-Joachim Piepereit <[email protected]> |
|
24
|
|
|
* @copyright 2013 cSphere Team |
|
25
|
|
|
* @license http://opensource.org/licenses/bsd-license Simplified BSD License |
|
26
|
|
|
* @link http://www.csphere.eu |
|
27
|
|
|
**/ |
|
28
|
|
|
|
|
29
|
|
|
abstract class Theme |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* Convert array of source files to string |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $type Must be javascripts or stylesheets |
|
35
|
|
|
* @param array $sources File sources as an array |
|
36
|
|
|
* |
|
37
|
|
|
* @return string |
|
38
|
|
|
**/ |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
private static function _src($type, array $sources) |
|
41
|
|
|
{ |
|
42
|
|
|
$string = ''; |
|
43
|
|
|
|
|
44
|
|
|
// Set start and end tag for javascripts and stylesheets |
|
45
|
|
|
if ($type == 'stylesheets') { |
|
46
|
|
|
|
|
47
|
|
|
$tag1 = '<link href="'; |
|
48
|
|
|
$tag2 = '" rel="stylesheet" type="text/css">'; |
|
49
|
|
|
|
|
50
|
|
|
} else { |
|
51
|
|
|
|
|
52
|
|
|
$tag1 = '<script src="'; |
|
53
|
|
|
$tag2 = '" type="text/javascript"></script>'; |
|
54
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
// Convert arrays to strings |
|
58
|
|
|
foreach ($sources AS $file) { |
|
59
|
|
|
|
|
60
|
|
|
$string .= $tag1 . $file . $tag2 . "\n"; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $string; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Light version of combine features for themes |
|
68
|
|
|
* |
|
69
|
|
|
* @param array $array Template file as an array |
|
70
|
|
|
* |
|
71
|
|
|
* @return array |
|
72
|
|
|
**/ |
|
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
private static function _light(array $array) |
|
75
|
|
|
{ |
|
76
|
|
|
// Combine array of parts |
|
77
|
|
|
$result = []; |
|
78
|
|
|
|
|
79
|
|
|
foreach ($array AS $part) { |
|
80
|
|
|
|
|
81
|
|
|
// Leave text and page placeholders untouched |
|
82
|
|
|
if ($part['cmd'] == 'text' || $part['cmd'] == 'page') { |
|
83
|
|
|
|
|
84
|
|
|
$result[] = $part; |
|
85
|
|
|
|
|
86
|
|
|
} else { |
|
87
|
|
|
|
|
88
|
|
|
// Everything else can be transformed |
|
89
|
|
|
$cmd = $part['cmd']; |
|
90
|
|
|
|
|
91
|
|
|
$string = \csphere\core\template\CMD_Parse::$cmd($part, []); |
|
92
|
|
|
|
|
93
|
|
|
$result[] = ['cmd' => 'text', 'text' => $string]; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $result; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Get boxes out of a prepared theme array |
|
102
|
|
|
* |
|
103
|
|
|
* @param array $theme Theme after prepare is used on it |
|
104
|
|
|
* |
|
105
|
|
|
* @return array |
|
106
|
|
|
**/ |
|
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
public static function boxes(array $theme) |
|
109
|
|
|
{ |
|
110
|
|
|
$add = []; |
|
111
|
|
|
|
|
112
|
|
|
// Skip all parts except boxes |
|
113
|
|
|
foreach ($theme AS $part) { |
|
114
|
|
|
|
|
115
|
|
|
if ($part['cmd'] == 'box') { |
|
116
|
|
|
|
|
117
|
|
|
$add[] = $part; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return $add; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Parses a theme file |
|
126
|
|
|
* |
|
127
|
|
|
* @param array $theme Theme content as an array |
|
128
|
|
|
* @param string $content The content to put into the theme file |
|
129
|
|
|
* |
|
130
|
|
|
* @throws \Exception |
|
131
|
|
|
* |
|
132
|
|
|
* @return string |
|
133
|
|
|
**/ |
|
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
public static function parse(array $theme, $content) |
|
136
|
|
|
{ |
|
137
|
|
|
// Replace everything except for page placeholders |
|
138
|
|
|
$theme = self::_light($theme); |
|
139
|
|
|
|
|
140
|
|
|
// Get page placeholders from hooks and append content |
|
141
|
|
|
$data = \csphere\core\template\Hooks::export(); |
|
142
|
|
|
|
|
143
|
|
|
$data['debug'] = '<div id="csphere_debug">' . $data['debug'] . '</div>'; |
|
144
|
|
|
|
|
145
|
|
|
$start = '<div id="csphere_content">' . $data['content']; |
|
146
|
|
|
$data['content'] = $start . $content . '</div>'; |
|
147
|
|
|
|
|
148
|
|
|
// Javascript and stylesheet arrays to strings |
|
149
|
|
|
$data['javascripts'] = self::_src('javascripts', $data['javascripts']); |
|
150
|
|
|
$data['stylesheets'] = self::_src('stylesheets', $data['stylesheets']); |
|
151
|
|
|
|
|
152
|
|
|
// Replace page placeholders |
|
153
|
|
|
$result = ''; |
|
154
|
|
|
|
|
155
|
|
|
foreach ($theme AS $part) { |
|
156
|
|
|
|
|
157
|
|
|
if ($part['cmd'] == 'page') { |
|
158
|
|
|
|
|
159
|
|
|
$result .= isset($data[$part['key']]) ? $data[$part['key']] : ''; |
|
160
|
|
|
|
|
161
|
|
|
} elseif ($part['cmd'] == 'text') { |
|
162
|
|
|
|
|
163
|
|
|
$result .= $part['text']; |
|
164
|
|
|
|
|
165
|
|
|
} else { |
|
166
|
|
|
throw new \Exception('Command not found: ' . $part['cmd']); |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
return $result; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Corrections for theme files to make them preparable |
|
175
|
|
|
* |
|
176
|
|
|
* @param string $string Theme content as a string |
|
177
|
|
|
* @param string $theme Theme directory as a string |
|
178
|
|
|
* |
|
179
|
|
|
* @return array |
|
180
|
|
|
**/ |
|
|
|
|
|
|
181
|
|
|
|
|
182
|
|
|
public static function prepare($string, $theme) |
|
183
|
|
|
{ |
|
184
|
|
|
// Try to find or attach room for javascript and stylesheets |
|
185
|
|
|
if (strpos($string, '{* page stylesheets *}') === false) { |
|
186
|
|
|
|
|
187
|
|
|
$change = '{* page stylesheets *}</head>'; |
|
188
|
|
|
$string = str_ireplace('</head>', $change, $string); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
$generator = '<meta name="generator" content="cSphere">' |
|
192
|
|
|
. "\n" . '</head>'; |
|
193
|
|
|
$string = str_ireplace('</head>', $generator, $string); |
|
194
|
|
|
|
|
195
|
|
|
if (strpos($string, '{* page javascripts *}') === false) { |
|
196
|
|
|
|
|
197
|
|
|
$change = '{* page javascripts *}</body>'; |
|
198
|
|
|
$string = str_ireplace('</body>', $change, $string); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
// Check for debug placeholder |
|
202
|
|
|
$debug = ''; |
|
203
|
|
|
|
|
204
|
|
|
if (strpos($string, '{* page debug *}') === false) { |
|
205
|
|
|
|
|
206
|
|
|
$debug = "\n" . '{* page debug *}'; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
$change = "<body\\1>" . $debug; |
|
210
|
|
|
$string = preg_replace('=\<body(.*?)\>=si', $change, $string, 1); |
|
211
|
|
|
|
|
212
|
|
|
// Repair html link tag href attributes |
|
213
|
|
|
$search = "=\<link(.*?)href\=\"(?!http|\/)(.*?)\"(.*?)\>=i"; |
|
214
|
|
|
$change = "<link\\1href=\"" . $theme . "\\2\"\\3>"; |
|
215
|
|
|
$string = preg_replace($search, $change, $string); |
|
216
|
|
|
|
|
217
|
|
|
// Repair html background and src attributes |
|
218
|
|
|
$search = "=(background|src)\=\"(?!http|\/)(.*?)\"=i"; |
|
219
|
|
|
$change = "\\1=\"" . $theme . "\\2\""; |
|
220
|
|
|
$string = preg_replace($search, $change, $string); |
|
221
|
|
|
|
|
222
|
|
|
// Placeholder detection |
|
223
|
|
|
$result = \csphere\core\template\Prepare::placeholders((string)$string, ''); |
|
224
|
|
|
|
|
225
|
|
|
return $result; |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|