function upgrade12_redothemes() { global $db, $output, $config, $mybb; $output->print_header("Converting themes"); if (!@is_dir(MYBB_ROOT . 'cache/')) { @mkdir(MYBB_ROOT . 'cache/', 077); // Add in empty index.html! $fp = @fopen(MYBB_ROOT . "cache/index.html", "w"); @fwrite($fp, ""); @fclose($fp); } $cachewritable = @fopen(MYBB_ROOT . 'cache/test.write', 'w'); if (!$cachewritable) { $not_writable = true; @fclose($cachewritable); } else { @fclose($cachewritable); @my_chmod(MYBB_ROOT . 'cache', '0777'); @my_chmod(MYBB_ROOT . 'cache/test.write', '0777'); @unlink(MYBB_ROOT . 'cache/test.write'); } if ($not_writable) { echo "<p><span style=\"color: red; font-weight: bold;\">Unable to write to the cache/ directory.</span><br />Before the upgrade process can continue you need to make sure this directory exists and is writable (chmod 777)</p>"; $output->print_footer("12_redothemes"); exit; } $not_writable = false; if (!@is_dir(MYBB_ROOT . 'cache/themes/')) { @mkdir(MYBB_ROOT . 'cache/themes/', 077); // Add in empty index.html! $fp = @fopen(MYBB_ROOT . "cache/themes/index.html", "w"); @fwrite($fp, ""); @fclose($fp); } $themewritable = @fopen(MYBB_ROOT . 'cache/themes/test.write', 'w'); if (!$themewritable) { $not_writable = true; @fclose($themewritable); } else { @fclose($themewritable); @my_chmod(MYBB_ROOT . 'cache/themes', '0777'); @my_chmod(MYBB_ROOT . 'cache/themes/test.write', '0777'); @unlink(MYBB_ROOT . 'cache/themes/test.write'); } if ($not_writable) { echo "<p><span style=\"color: red; font-weight: bold;\">Unable to write to the cache/themes/ directory.</span><br />Before the upgrade process can continue you need to make sure this directory exists and is writable (chmod 777)</p>"; $output->print_footer("12_redothemes"); exit; } if ($db->field_exists('themebits', "themes") && !$db->field_exists('properties', "themes")) { $db->write_query("ALTER TABLE " . TABLE_PREFIX . "themes CHANGE themebits properties text NOT NULL"); } if ($db->field_exists('cssbits', "themes")) { $db->write_query("ALTER TABLE " . TABLE_PREFIX . "themes DROP cssbits"); } if ($db->field_exists('csscached', "themes")) { $db->write_query("ALTER TABLE " . TABLE_PREFIX . "themes DROP csscached"); } if ($db->field_exists('stylesheets', "themes")) { $db->write_query("ALTER TABLE " . TABLE_PREFIX . "themes DROP stylesheets"); } $db->write_query("ALTER TABLE " . TABLE_PREFIX . "themes ADD stylesheets text NOT NULL AFTER properties"); if ($db->table_exists("themestylesheets")) { $db->drop_table("themestylesheets"); } $db->write_query("CREATE TABLE " . TABLE_PREFIX . "themestylesheets(\n\t\tsid int unsigned NOT NULL auto_increment,\n\t\tname varchar(30) NOT NULL default '',\n\t\ttid int unsigned NOT NULL default '0',\n\t\tattachedto text NOT NULL,\n\t\tstylesheet text NOT NULL,\n\t\tcachefile varchar(100) NOT NULL default '',\n\t\tlastmodified bigint(30) NOT NULL default '0',\n\t\tPRIMARY KEY(sid)\n\t) ENGINE=MyISAM{$collation};"); // Define our default stylesheets - MyBB 1.4 contains additional stylesheets that our converted themes will also need $contents = @file_get_contents(INSTALL_ROOT . 'resources/mybb_theme.xml'); if (file_exists(MYBB_ROOT . $mybb->config['admin_dir'] . "/inc/functions_themes.php")) { require_once MYBB_ROOT . $mybb->config['admin_dir'] . "/inc/functions_themes.php"; } else { if (file_exists(MYBB_ROOT . "admin/inc/functions_themes.php")) { require_once MYBB_ROOT . "admin/inc/functions_themes.php"; } else { $output->print_error("Please make sure your admin directory is uploaded correctly."); } } // Import master theme if (import_theme_xml($contents, array("tid" => 1, "no_templates" => 1, "version_compat" => 1)) === -1) { $output->print_error("Please make sure your install/resources/mybb_theme.xml file is uploaded correctly."); } // Fetch out default stylesheets from master $query = $db->simple_select("themes", "*", "tid=1"); $master_theme = $db->fetch_array($query); $master_stylesheets = unserialize($master_theme['stylesheets']); if (is_array($master_stylesheets)) { // Note: 1.4 only ships with one global|global stylesheet foreach ($master_stylesheets as $location => $sheets) { foreach ($sheets as $action => $sheets) { foreach ($sheets as $stylesheet) { if ($location == "global" && $action == "global") { continue; // Skip global } $default_stylesheets[$location][$action][] = $stylesheet; $default_stylesheets['inherited']["{$location}_{$action}"][$stylesheet] = 1; // This stylesheet is inherited from the master } } } } $query = $db->simple_select("themes"); while ($theme = $db->fetch_array($query)) { if (!$theme['css']) { continue; } $theme['css'] .= "\n\n" . $theme['extracss']; $theme['css'] = upgrade_css_120_to_140($theme['css']); // Create stylesheets $cache_file = cache_stylesheet($theme['tid'], "global.css", $theme['css']); $new_stylesheet = array("tid" => $theme['tid'], "name" => "global.css", "attachedto" => "", "stylesheet" => $db->escape_string($theme['css']), "cachefile" => "global.css", "lastmodified" => TIME_NOW); $sid = $db->insert_query("themestylesheets", $new_stylesheet); $css_url = "css.php?stylesheet={$sid}"; if ($cache_file) { $css_url = $cache_file; } // Now we go and update the stylesheets column for this theme $stylesheets = $default_stylesheets; // Add in our local for this theme $stylesheets['global']['global'][] = $css_url; // Update the theme $db->update_query("themes", array("stylesheets" => $db->escape_string(serialize($stylesheets))), "tid='{$theme['tid']}'"); } if ($db->field_exists('css', "themes")) { $db->write_query("ALTER TABLE " . TABLE_PREFIX . "themes DROP css"); } if ($db->field_exists('extracss', "themes")) { $db->write_query("ALTER TABLE " . TABLE_PREFIX . "themes DROP extracss"); } // We need to replace this for our themes css to show up // <link rel="stylesheet" type="text/css" href="{$theme['css_url']}" /> must be present in the old template (it usually is) $query = $db->simple_select("templates", "tid,template", "title='headerinclude'"); while ($template = $db->fetch_array($query)) { $template['template'] = str_replace('<link rel="stylesheet" type="text/css" href="{$theme[\'css_url\']}" />', '{$stylesheets}', $template['template']); $db->update_query("templates", array('template' => $db->escape_string($template['template'])), "tid='{$template['tid']}'"); } echo "<p>Your themes have successfully been converted to the new theme system.</p>"; echo "<p>Click next to continue with the upgrade process.</p>"; global $footer_extra; //$footer_extra = "<script type=\"text/javascript\">window.onload = function() { var button = $$('.submit_button'); if(button[0]) { button[0].value = 'Automatically Redirecting...'; button[0].disabled = true; button[0].style.color = '#aaa'; button[0].style.borderColor = '#aaa'; document.forms[0].submit(); }}</script>"; $output->print_footer("12_done"); }
/** * Import an entire theme (stylesheets, properties & templates) from an XML file. * * @param string The contents of the XML file * @param array Optional array of options or overrides * @return boolean True on success, false on failure */ function import_theme_xml($xml, $options = array()) { global $mybb, $db; require_once MYBB_ROOT . "inc/class_xml.php"; $parser = new XMLParser($xml); $tree = $parser->get_tree(); if (!is_array($tree) || !is_array($tree['theme'])) { return -1; } $theme = $tree['theme']; // Do we have MyBB 1.2 template's we're importing? $css_120 = ""; if (is_array($theme['cssbits'])) { $cssbits = kill_tags($theme['cssbits']); foreach ($cssbits as $name => $values) { $css_120 .= "{$name} {\n"; foreach ($values as $property => $value) { if (is_array($value)) { $property = str_replace('_', ':', $property); $css_120 .= "}\n{$name} {$property} {\n"; foreach ($value as $property2 => $value2) { $css_120 .= "\t{$property2}: {$value2}\n"; } } else { $css_120 .= "\t{$property}: {$value}\n"; } } $css_120 .= "}\n"; } } if (is_array($theme['themebits'])) { $themebits = kill_tags($theme['themebits']); $theme['properties']['tag'] = 'properties'; foreach ($themebits as $name => $value) { if ($name == "extracss") { $css_120 .= $value; continue; } $theme['properties'][$name] = $value; } } if ($css_120) { $css_120 = upgrade_css_120_to_140($css_120); $theme['stylesheets']['tag'] = 'stylesheets'; $theme['stylesheets']['stylesheet'][0]['tag'] = 'stylesheet'; $theme['stylesheets']['stylesheet'][0]['attributes'] = array('name' => 'global.css', 'version' => $mybb->version_code); $theme['stylesheets']['stylesheet'][0]['value'] = $css_120; unset($theme['cssbits']); unset($theme['themebits']); } if (is_array($theme['properties'])) { foreach ($theme['properties'] as $property => $value) { if ($property == "tag" || $property == "value") { continue; } $properties[$property] = $value['value']; } } if (empty($mybb->input['name'])) { $name = $theme['attributes']['name']; } else { $name = $mybb->input['name']; } $version = $theme['attributes']['version']; $query = $db->simple_select("themes", "tid", "name='" . $db->escape_string($name) . "'", array("limit" => 1)); $existingtheme = $db->fetch_array($query); if ($options['force_name_check'] && $existingtheme['tid']) { return -3; } else { if ($existingtheme['tid']) { $options['tid'] = $existingtheme['tid']; } } if ($mybb->version_code != $version && $options['version_compat'] != 1) { return -2; } // Do we have any templates to insert? if (!empty($theme['templates']['template']) && !$options['no_templates']) { if ($options['templateset']) { $sid = $options['templateset']; } else { $sid = $db->insert_query("templatesets", array('title' => $db->escape_string($name) . " Templates")); } $templates = $theme['templates']['template']; if (is_array($templates)) { // Theme only has one custom template if (array_key_exists("attributes", $templates)) { $templates = array($templates); } } $security_check = false; $templatecache = array(); foreach ($templates as $template) { if (check_template($template['value'])) { $security_check = true; break; } $templatecache[] = array("title" => $db->escape_string($template['attributes']['name']), "template" => $db->escape_string($template['value']), "sid" => $db->escape_string($sid), "version" => $db->escape_string($template['attributes']['version']), "dateline" => TIME_NOW); } if ($security_check == true) { return -4; } foreach ($templatecache as $template) { // PostgreSQL causes apache to stop sending content sometimes and // causes the page to stop loading during many queries all at one time if ($db->engine == "pgsql") { echo " "; flush(); } $db->insert_query("templates", $template); } $properties['templateset'] = $sid; } // Not overriding an existing theme if (!$options['tid']) { // Insert the theme $theme_id = build_new_theme($name, $properties, $options['parent']); } else { $db->delete_query("themestylesheets", "tid='{$options['tid']}'"); $db->update_query("themes", array("properties" => $db->escape_string(serialize($properties))), "tid='{$options['tid']}'"); $theme_id = $options['tid']; } // If we have any stylesheets, process them if (!empty($theme['stylesheets']['stylesheet']) && !$options['no_stylesheets']) { // Are we dealing with a single stylesheet? if (isset($theme['stylesheets']['stylesheet']['tag'])) { // Trick the system into thinking we have a good array =P $theme['stylesheets']['stylesheet'] = array($theme['stylesheets']['stylesheet']); } foreach ($theme['stylesheets']['stylesheet'] as $stylesheet) { if (substr($stylesheet['attributes']['name'], -4) != ".css") { continue; } if (!$stylesheet['attributes']['lastmodified']) { $stylesheet['attributes']['lastmodified'] = TIME_NOW; } $new_stylesheet = array("name" => $db->escape_string($stylesheet['attributes']['name']), "tid" => $theme_id, "attachedto" => $db->escape_string($stylesheet['attributes']['attachedto']), "stylesheet" => $db->escape_string($stylesheet['value']), "lastmodified" => intval($stylesheet['attributes']['lastmodified']), "cachefile" => $db->escape_string($stylesheet['attributes']['name'])); $sid = $db->insert_query("themestylesheets", $new_stylesheet); $css_url = "css.php?stylesheet={$sid}"; $cached = cache_stylesheet($theme_id, $stylesheet['attributes']['name'], $stylesheet['value']); if ($cached) { $css_url = $cached; } $attachedto = $stylesheet['attributes']['attachedto']; if (!$attachedto) { $attachedto = "global"; } // private.php?compose,folders|usercp.php,global|global $attachedto = explode("|", $attachedto); foreach ($attachedto as $attached_file) { $attached_actions = explode(",", $attached_file); $attached_file = array_shift($attached_actions); if (count($attached_actions) == 0) { $attached_actions = array("global"); } foreach ($attached_actions as $action) { $theme_stylesheets[$attached_file][$action][] = $css_url; } } } // Now we have our list of built stylesheets, save them $updated_theme = array("stylesheets" => $db->escape_string(serialize($theme_stylesheets))); $db->update_query("themes", $updated_theme, "tid='{$theme_id}'"); } update_theme_stylesheet_list($theme_id); // And done? return $theme_id; }