[go: up one dir, main page]

Menu

[r5665]: / tools / templateconv.php  Maximize  Restore  History

Download this file

298 lines (241 with data), 8.3 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
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
<?php
## Template conversion tool
# @author legolas559
# @version 0.2
#
# Simple conversion tool which converts a Limbo/Mambo/Joomla template to a Drake CMS compatible one
# CSS is not yet updated!
# needs PHP5 (stripos used) to work
/*** BEGINS TOOL FUNCTIONS ***/
// old CSS class -> new CSS class
$css_conv = array (
'inputbox' => 'dk_inputbox',
'button' => 'dk_button',
'componentheading' => 'dkcom_heading',
'moduletable' => 'dk_module',
'moduletable th' => 'dk_moduletitle',
'moduletable td' => 'dk_modulecontent',
'sectiontableentry1' =>
array (
0 => 'dkcom_polls_tablerow1',
1 => 'dkcom_content_tablerow1',
2 => 'dkmod_polls_row1',
3 => 'dkcom_event_entryitem1',
),
'sectiontableentry2' =>
array (
0 => 'dkcom_polls_tablerow2',
1 => 'dkcom_content_tablerow2',
2 => 'dkmod_polls_row2',
3 => 'dkcom_event_entryitem2',
),
'contentpaneopen' => 'dkcom_content_section',
'contentdescription' => 'dkcom_content_description',
'small' => 'dk_small',
'contentheading' => 'dkcom_content_messagesent',
'contentpane' => 'dkcom_content',
'mainlevel' => 'dkmod_menu_mainlevel',
'sublevel' => 'dkmod_menu_sublevel',
'pollstableborder' => 'dkcom_polls_table',
'sectiontableheader' =>
array (
0 => 'dkcom_polls_tableheader',
1 => 'dkcom_content_tableheader',
),
'search' => 'dkmod_search',
);
/* $css_conv =
$rows = explode("\n", $css_conv);
$css_conv = array();
foreach($rows as $row) {
$row = explode(' -> ', $row);
$targ = array_map('trim', explode(', ', $row[1]));
if (!isset($targ[1])) $targ = $targ[0];
$css_conv[$row[0]] = $targ;
}
echo '<pre>';var_export($css_conv);echo '</pre>';die;
*/
// parses the CSS file content in $css into two separate arrays $definitions and $contents
// $definitions is an array of subarrays
// $contents contains the corresponding CSS definition content
function parse_css(&$css, &$arr) {
if (!preg_match_all('/((\\s*[\\w#\\.:>]+\\s*,?)+)\\{([^\\}]*)\\}/', $css, $m))
return false;
unset($m[0]);
unset($m[2]);
$arr = array();
reset($m[3]);
foreach($m[1] as $definition) {
$definition = array_map('trim', explode(',',$definition));
$ct = current($m[3]);
foreach($definition as $def) {
$arr[$def] = $ct;
}
next($m[3]);
}
return true;
}
$dk_raw_css = (string)@file_get_contents('drake.css');
if (!isset($dk_raw_css[0])) {
echo "The script folder should contain a drake.css file\n";
die;
}
parse_css($dk_raw_css, $dk_css);
function convert_mambo_css(&$raw_css, &$dk_raw_css) {
global $conv_css, $dk_css;
parse_css($raw_css, $css);
$raw_css = '';
$filtered = array();
// loop through each template_css.css definition
foreach($css as $def => $ct) {
if (!isset($dk_css[$def])) {
$raw_css .= "\n$def {\n$ct\n}\n";
continue;
}
$dk_css[$def] = $ct;
}
$dk_raw_css = '';
foreach($dk_css as $def => $ct) {
$dk_raw_css .= "\n$def {\n$ct\n}\n";
}
return true;
}
function convert_mambo_template(&$template) {
// save useful PHP, script and link tags from the head
$p = stripos($template, '<head>');
if ($p===false)
return false;
$p += 6;
$ep = stripos($template, '</head>', $p);
if ($ep===false)
return false;
$head = substr($template, $p, $ep-$p);
/* $n_head = '<?php $d->ShowHead(); ?>'."\n"; */
$n_head = '';
/*
if (preg_match_all('/<script.*?>((.|\\n)*?)<\\/script>/i', $head, $m)) {
foreach($m as $row) {
$n_head.=$row[0]."\n";
}
}
*/
// returns the stripped tags
function custom_strip_tag(&$s, $start, $end) {
$found = array();
$sl = strlen($start);
$el = strlen($end);
$p = 0;
while (false !== ($p = stripos($s, $start, $p))) {
$et = stripos($s, $end, $p+$sl);
if ($et===false)
break;
$found[] = trim(substr($s, $p, $et-$p+$el));
$s = substr_replace($s, '', $p, $et-$p+$el);
}
return $found;
}
// save info about additional css files
if (preg_match_all('/\\w+\\.css/i', $head, $m)) {
foreach($m[0] as $css) {
if ($css == 'template_css.css')
continue;
$n_head = '<link href="<?php echo $d_subpath;?>/templates/<?php echo $d_template; ?>/css/'.$css.'" rel="stylesheet" type="text/css"/>'."\n";
}
}
// save the link and script tags
$links = custom_strip_tag($head, '<link', '/>');
$scripts = custom_strip_tag($head, '<script', '</script>');
$n_head .= implode("\n", $links)."\n".implode("\n", $scripts);
// save the PHP tags
$offset = 0;
$n_head .= "<?php\n";
while (false !== ($p = strpos($head, '<?php', $offset))) {
$p += 6;
$et = strpos($head, '?>', $p);
if ($et===false)
break;
$php = substr($head, $p, $et-$p);
if (strpos($php, '_ISO')===false)
$n_head.=$php."\n\n";
$offset = $et+2;
}
$n_head .= "?>";
unset($head);
// rebuild the template with the new head
$template = '<?php if(!defined(\'_VALID\')){header(\'Status: 404 Not Found\');die;}
echo \'<?xml version="1.0" encoding="\'. $d->Encoding() .\'"?\' .\'>\';
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">'."\n<head>\n".$n_head."\n</head>\n".strstr($template, '<body');
$src = array('_VALID_MOS', '_VALID_LM', 'cur_template', 'mosConfig_absolute_path',
'mosConfig_live_site',
'lm_website',
'lm_template',
'mosConfig_sitename', 'mosCountModules', 'mosLoadModules', 'mosShowHead', 'mosPathWay', 'mosMainBody', 'mosCurrentDate', 'mosLoadComponent',
'src="templates/'
);
$dst = array('_VALID', '_VALID', 'd_template', 'd_root',
'd_subpath',
'd_subpath',
'd_template',
'd_title', '$d->CountModules', '$d->LoadModules', '$d->ShowHead', '$d->PathWay', '$d->MainBody', '$d->CurrentDate', '$d->LoadComponent',
'src="<?php echo $d_subpath; ?>templates/'
);
// replace old variables with new variables
$template = str_replace($src, $dst, $template);
$find = array('/GLOBALS\\[(\'|")?(\\w+)(\'|")?\\]/',
'/(include|require|include_once|require_once).*includes\\/footer\\.php.*;/i',
'/'.str_replace(' ', '\\s*', preg_quote('if ( $my->id ) { initEditor(); }')).'/i',
'/\\<\\?php\\s*\\?\\>/',
'/\\<meta\\s*http-equiv="Content-Type".*\\/\\>/i',
'/sefRelToAbs\\s*\\(/'
);
$repl = array('\\2',
'$d->LoadModules(\'debug\')',
'',
'',
'',
'('
);
$template = preg_replace($find, $repl, $template);
// relativize all paths
$template = preg_replace('/templates\\/(\\w+)\\//i', 'templates/<?php echo $d_template; ?>/', $template);
return true; // conversion complete
}
/*** ENDS TOOL FUNCTIONS ***/
/*** BEGINS APPLICATION ***/
header('Content-Type: text/plain');
$template = 'template';
$dest = '../templates/converted';
// read the template file
$index = (string)@file_get_contents($template.'/index.php');
if (!isset($index[0])) {
echo 'Please put an index.php file in the '.$template.' folder as '.__FILE__.' and run again this script'."\n";
echo 'The output files (index.php and CSS files) will be saved into '.$dest."/\n";
exit;
}
// eventually remove slashes if the template was submit through POST
//if (get_magic_quotes_gpc())// $template = stripslashes($template);
$css = (string)@file_get_contents($template.'/css/template_css.css');
if (!isset($css[0])) {
echo "Could not read the $template/css/template_css.css file\n";
exit;
}
if (!convert_mambo_css($css, $dk_raw_css)) {
echo 'Could not convert the template CSS';
exit;
}
if (!convert_mambo_template($index)) {
echo 'There was an error processing the original template';
exit;
}
$f = fopen($dest.'/index.php', 'wb');
fputs($f, $index); fclose($f); unset($index);
echo "$dest/index.php created successfully\n";
$f = fopen($dest.'/css/template_css.css', 'wb');
fputs($f, $css); fclose($f); unset($css);
echo "$dest/css/template_css.css created\n";
$f = fopen($dest.'/css/drake.css', 'wb');
fputs($f, $dk_raw_css); fclose($f); unset($dk_raw_css);
echo "$dest/css/drake.css created\n";
echo "please manually copy the $template/images/ folder to $dest/images/\n";
?>