<?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";
?>