Ejemplo n.º 1
0
function DownloadLanguage()
{
    global $context, $sourcedir, $forum_version, $boarddir, $txt, $smcFunc, $scripturl, $modSettings;
    loadLanguage('ManageSettings');
    require_once $sourcedir . '/Subs-Package.php';
    // Clearly we need to know what to request.
    if (!isset($_GET['did'])) {
        fatal_lang_error('no_access', false);
    }
    // Some lovely context.
    $context['download_id'] = $_GET['did'];
    $context['sub_template'] = 'download_language';
    $context['menu_data_' . $context['admin_menu_id']]['current_subsection'] = 'add';
    // Can we actually do the installation - and do they want to?
    if (!empty($_POST['do_install']) && !empty($_POST['copy_file'])) {
        checkSession('get');
        $chmod_files = array();
        $install_files = array();
        // Check writable status.
        foreach ($_POST['copy_file'] as $file) {
            // Check it's not very bad.
            if (strpos($file, '..') !== false || substr($file, 0, 6) != 'Themes' && !preg_match('~agreement\\.[A-Za-z-_0-9]+\\.txt$~', $file)) {
                fatal_error($txt['languages_download_illegal_paths']);
            }
            $chmod_files[] = $boarddir . '/' . $file;
            $install_files[] = $file;
        }
        // Call this in case we have work to do.
        $file_status = create_chmod_control($chmod_files);
        $files_left = $file_status['files']['notwritable'];
        // Something not writable?
        if (!empty($files_left)) {
            $context['error_message'] = $txt['languages_download_not_chmod'];
        } elseif (!empty($install_files)) {
            $archive_content = read_tgz_file('http://download.simplemachines.org/fetch_language.php?version=' . urlencode(strtr($forum_version, array('SMF ' => ''))) . ';fetch=' . urlencode($_GET['did']), $boarddir, false, true, $install_files);
            // Make sure the files aren't stuck in the cache.
            package_flush_cache();
            $context['install_complete'] = sprintf($txt['languages_download_complete_desc'], $scripturl . '?action=admin;area=languages');
            return;
        }
    }
    // Open up the old china.
    if (!isset($archive_content)) {
        $archive_content = read_tgz_file('http://download.simplemachines.org/fetch_language.php?version=' . urlencode(strtr($forum_version, array('SMF ' => ''))) . ';fetch=' . urlencode($_GET['did']), null);
    }
    if (empty($archive_content)) {
        fatal_error($txt['add_language_error_no_response']);
    }
    // Now for each of the files, let's do some *stuff*
    $context['files'] = array('lang' => array(), 'other' => array());
    $context['make_writable'] = array();
    foreach ($archive_content as $file) {
        $dirname = dirname($file['filename']);
        $filename = basename($file['filename']);
        $extension = substr($filename, strrpos($filename, '.') + 1);
        // Don't do anything with files we don't understand.
        if (!in_array($extension, array('php', 'jpg', 'gif', 'jpeg', 'png', 'txt'))) {
            continue;
        }
        // Basic data.
        $context_data = array('name' => $filename, 'destination' => $boarddir . '/' . $file['filename'], 'generaldest' => $file['filename'], 'size' => $file['size'], 'writable' => false, 'default_copy' => true, 'exists' => false);
        // Does the file exist, is it different and can we overwrite?
        if (file_exists($boarddir . '/' . $file['filename'])) {
            if (is_writable($boarddir . '/' . $file['filename'])) {
                $context_data['writable'] = true;
            }
            // Finally, do we actually think the content has changed?
            if ($file['size'] == filesize($boarddir . '/' . $file['filename']) && $file['md5'] == md5_file($boarddir . '/' . $file['filename'])) {
                $context_data['exists'] = 'same';
                $context_data['default_copy'] = false;
            } elseif ($file['md5'] == md5(preg_replace("~[\r]?\n~", "\r\n", file_get_contents($boarddir . '/' . $file['filename'])))) {
                $context_data['exists'] = 'same';
                $context_data['default_copy'] = false;
            } else {
                $context_data['exists'] = 'different';
            }
        } else {
            // Can we at least stick it in the directory...
            if (is_writable($boarddir . '/' . $dirname)) {
                $context_data['writable'] = true;
            }
        }
        // I love PHP files, that's why I'm a developer and not an artistic type spending my time drinking absinth and living a life of sin...
        if ($extension == 'php' && preg_match('~\\w+\\.\\w+(?:-utf8)?\\.php~', $filename)) {
            $context_data += array('version' => '??', 'cur_version' => false, 'version_compare' => 'newer');
            list($name, $language) = explode('.', $filename);
            // Let's get the new version, I like versions, they tell me that I'm up to date.
            if (preg_match('~\\s*Version:\\s+(.+?);\\s*' . preg_quote($name, '~') . '~i', $file['preview'], $match) == 1) {
                $context_data['version'] = $match[1];
            }
            // Now does the old file exist - if so what is it's version?
            if (file_exists($boarddir . '/' . $file['filename'])) {
                // OK - what is the current version?
                $fp = fopen($boarddir . '/' . $file['filename'], 'rb');
                $header = fread($fp, 768);
                fclose($fp);
                // Find the version.
                if (preg_match('~(?://|/\\*)\\s*Version:\\s+(.+?);\\s*' . preg_quote($name, '~') . '(?:[\\s]{2}|\\*/)~i', $header, $match) == 1) {
                    $context_data['cur_version'] = $match[1];
                    // How does this compare?
                    if ($context_data['cur_version'] == $context_data['version']) {
                        $context_data['version_compare'] = 'same';
                    } elseif ($context_data['cur_version'] > $context_data['version']) {
                        $context_data['version_compare'] = 'older';
                    }
                    // Don't recommend copying if the version is the same.
                    if ($context_data['version_compare'] != 'newer') {
                        $context_data['default_copy'] = false;
                    }
                }
            }
            // Add the context data to the main set.
            $context['files']['lang'][] = $context_data;
        } else {
            // If we think it's a theme thing, work out what the theme is.
            if (substr($dirname, 0, 6) == 'Themes' && preg_match('~Themes[\\/]([^\\/]+)[\\/]~', $dirname, $match)) {
                $theme_name = $match[1];
            } else {
                $theme_name = 'misc';
            }
            // Assume it's an image, could be an acceptance note etc but rare.
            $context['files']['images'][$theme_name][] = $context_data;
        }
        // Collect together all non-writable areas.
        if (!$context_data['writable']) {
            $context['make_writable'][] = $context_data['destination'];
        }
    }
    // So, I'm a perfectionist - let's get the theme names.
    $theme_indexes = array();
    foreach ($context['files']['images'] as $k => $dummy) {
        $indexes[] = $k;
    }
    $context['theme_names'] = array();
    if (!empty($indexes)) {
        $value_data = array('query' => array(), 'params' => array());
        foreach ($indexes as $k => $index) {
            $value_data['query'][] = 'value LIKE {string:value_' . $k . '}';
            $value_data['params']['value_' . $k] = '%' . $index;
        }
        $request = $smcFunc['db_query']('', '
			SELECT id_theme, value
			FROM {db_prefix}themes
			WHERE id_member = {int:no_member}
				AND variable = {string:theme_dir}
				AND (' . implode(' OR ', $value_data['query']) . ')', array_merge($value_data['params'], array('no_member' => 0, 'theme_dir' => 'theme_dir', 'index_compare_explode' => 'value LIKE \'%' . implode('\' OR value LIKE \'%', $indexes) . '\'')));
        $themes = array();
        while ($row = $smcFunc['db_fetch_assoc']($request)) {
            // Find the right one.
            foreach ($indexes as $index) {
                if (strpos($row['value'], $index) !== false) {
                    $themes[$row['id_theme']] = $index;
                }
            }
        }
        $smcFunc['db_free_result']($request);
        if (!empty($themes)) {
            // Now we have the id_theme we can get the pretty description.
            $request = $smcFunc['db_query']('', '
				SELECT id_theme, value
				FROM {db_prefix}themes
				WHERE id_member = {int:no_member}
					AND variable = {string:name}
					AND id_theme IN ({array_int:theme_list})', array('theme_list' => array_keys($themes), 'no_member' => 0, 'name' => 'name'));
            while ($row = $smcFunc['db_fetch_assoc']($request)) {
                // Now we have it...
                $context['theme_names'][$themes[$row['id_theme']]] = $row['value'];
            }
            $smcFunc['db_free_result']($request);
        }
    }
    // Before we go to far can we make anything writable, eh, eh?
    if (!empty($context['make_writable'])) {
        // What is left to be made writable?
        $file_status = create_chmod_control($context['make_writable']);
        $context['still_not_writable'] = $file_status['files']['notwritable'];
        // Mark those which are now writable as such.
        foreach ($context['files'] as $type => $data) {
            if ($type == 'lang') {
                foreach ($data as $k => $file) {
                    if (!$file['writable'] && !in_array($file['destination'], $context['still_not_writable'])) {
                        $context['files'][$type][$k]['writable'] = true;
                    }
                }
            } else {
                foreach ($data as $theme => $files) {
                    foreach ($files as $k => $file) {
                        if (!$file['writable'] && !in_array($file['destination'], $context['still_not_writable'])) {
                            $context['files'][$type][$theme][$k]['writable'] = true;
                        }
                    }
                }
            }
        }
        // Are we going to need more language stuff?
        if (!empty($context['still_not_writable'])) {
            loadLanguage('Packages');
        }
    }
    // This is the list for the main files.
    $listOptions = array('id' => 'lang_main_files_list', 'title' => $txt['languages_download_main_files'], 'get_items' => array('function' => create_function('', '
				global $context;
				return $context[\'files\'][\'lang\'];
			')), 'columns' => array('name' => array('header' => array('value' => $txt['languages_download_filename']), 'data' => array('function' => create_function('$rowData', '
						global $context, $txt;

						return \'<strong>\' . $rowData[\'name\'] . \'</strong><br /><span class="smalltext">\' . $txt[\'languages_download_dest\'] . \': \' . $rowData[\'destination\'] . \'</span>\' . ($rowData[\'version_compare\'] == \'older\' ? \'<br />\' . $txt[\'languages_download_older\'] : \'\');
					'))), 'writable' => array('header' => array('value' => $txt['languages_download_writable']), 'data' => array('function' => create_function('$rowData', '
						global $txt;

						return \'<span style="color: \' . ($rowData[\'writable\'] ? \'green\' : \'red\') . \';">\' . ($rowData[\'writable\'] ? $txt[\'yes\'] : $txt[\'no\']) . \'</span>\';
					'), 'style' => 'text-align: center')), 'version' => array('header' => array('value' => $txt['languages_download_version']), 'data' => array('function' => create_function('$rowData', '
						global $txt;

						return \'<span style="color: \' . ($rowData[\'version_compare\'] == \'older\' ? \'red\' : ($rowData[\'version_compare\'] == \'same\' ? \'orange\' : \'green\')) . \';">\' . $rowData[\'version\'] . \'</span>\';
					'))), 'exists' => array('header' => array('value' => $txt['languages_download_exists']), 'data' => array('function' => create_function('$rowData', '
						global $txt;

						return $rowData[\'exists\'] ? ($rowData[\'exists\'] == \'same\' ? $txt[\'languages_download_exists_same\'] : $txt[\'languages_download_exists_different\']) : $txt[\'no\'];
					'))), 'copy' => array('header' => array('value' => $txt['languages_download_copy']), 'data' => array('function' => create_function('$rowData', '
						return \'<input type="checkbox" name="copy_file[]" value="\' . $rowData[\'generaldest\'] . \'" \' . ($rowData[\'default_copy\'] ? \'checked="checked"\' : \'\') . \' class="input_check" />\';
					'), 'style' => 'text-align: center; width: 4%;'))));
    // Kill the cache, as it is now invalid..
    if (!empty($modSettings['cache_enable'])) {
        cache_put_data('known_languages', null, !empty($modSettings['cache_enable']) && $modSettings['cache_enable'] < 1 ? 86400 : 3600);
        cache_put_data('known_languages_all', null, !empty($modSettings['cache_enable']) && $modSettings['cache_enable'] < 1 ? 86400 : 3600);
    }
    require_once $sourcedir . '/Subs-List.php';
    createList($listOptions);
    $context['default_list'] = 'lang_main_files_list';
}
Ejemplo n.º 2
0
function read_zip_data($data, $destination, $single_file = false, $overwrite = false, $files_to_extract = null)
{
    umask(0);
    if ($destination !== null && !file_exists($destination) && !$single_file) {
        mktree($destination, 0777);
    }
    // Look for the PK header...
    if (substr($data, 0, 2) != 'PK') {
        return false;
    }
    // Find the central whosamawhatsit at the end; if there's a comment it's a pain.
    if (substr($data, -22, 4) == 'PK' . chr(5) . chr(6)) {
        $p = -22;
    } else {
        // Have to find where the comment begins, ugh.
        for ($p = -22; $p > -strlen($data); $p--) {
            if (substr($data, $p, 4) == 'PK' . chr(5) . chr(6)) {
                break;
            }
        }
    }
    $return = array();
    // Get the basic zip file info.
    $zip_info = unpack('vfiles/Vsize/Voffset', substr($data, $p + 10, 10));
    $p = $zip_info['offset'];
    for ($i = 0; $i < $zip_info['files']; $i++) {
        // Make sure this is a file entry...
        if (substr($data, $p, 4) != 'PK' . chr(1) . chr(2)) {
            return false;
        }
        // Get all the important file information.
        $file_info = unpack('Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len/vcomment_len/vdisk/vinternal/Vexternal/Voffset', substr($data, $p + 16, 30));
        $file_info['filename'] = substr($data, $p + 46, $file_info['filename_len']);
        // Skip all the information we don't care about anyway.
        $p += 46 + $file_info['filename_len'] + $file_info['extra_len'] + $file_info['comment_len'];
        // If this is a file, and it doesn't exist.... happy days!
        if (substr($file_info['filename'], -1, 1) != '/' && !file_exists($destination . '/' . $file_info['filename'])) {
            $write_this = true;
        } elseif (substr($file_info['filename'], -1, 1) != '/') {
            $write_this = $overwrite;
        } elseif ($destination !== null && !$single_file) {
            // Just a little accident prevention, don't mind me.
            $file_info['filename'] = strtr($file_info['filename'], array('../' => '', '/..' => ''));
            if (!file_exists($destination . '/' . $file_info['filename'])) {
                mktree($destination . '/' . $file_info['filename'], 0777);
            }
            $write_this = false;
        } else {
            $write_this = false;
        }
        // Check that the data is there and does exist.
        if (substr($data, $file_info['offset'], 4) != 'PK' . chr(3) . chr(4)) {
            return false;
        }
        // Get the actual compressed data.
        $file_info['data'] = substr($data, $file_info['offset'] + 30 + $file_info['filename_len'], $file_info['compressed_size']);
        // Only inflate it if we need to ;).
        if ($file_info['compressed_size'] != $file_info['size']) {
            $file_info['data'] = @gzinflate($file_info['data']);
        }
        // Okay!  We can write this file, looks good from here...
        if ($write_this && $destination !== null) {
            if (strpos($file_info['filename'], '/') !== false && !$single_file) {
                mktree($destination . '/' . dirname($file_info['filename']), 0777);
            }
            // If we're looking for a specific file, and this is it... ka-bam, baby.
            if ($single_file && ($destination == $file_info['filename'] || $destination == '*/' . basename($file_info['filename']))) {
                return $file_info['data'];
            } elseif ($single_file) {
                continue;
            } elseif ($files_to_extract !== null && !in_array($file_info['filename'], $files_to_extract)) {
                continue;
            }
            package_put_contents($destination . '/' . $file_info['filename'], $file_info['data']);
        }
        if (substr($file_info['filename'], -1, 1) != '/') {
            $return[] = array('filename' => $file_info['filename'], 'md5' => md5($file_info['data']), 'preview' => substr($file_info['data'], 0, 100), 'size' => $file_info['size'], 'skipped' => false);
        }
    }
    if ($destination !== null && !$single_file) {
        package_flush_cache();
    }
    if ($single_file) {
        return false;
    } else {
        return $return;
    }
}
Ejemplo n.º 3
0
function ThemeInstall()
{
    global $sourcedir, $boarddir, $boardurl, $txt, $context, $settings, $modSettings, $smcFunc;
    checkSession('request');
    isAllowedTo('admin_forum');
    checkSession('request');
    require_once $sourcedir . '/Subs-Package.php';
    loadTemplate('Themes');
    if (isset($_GET['theme_id'])) {
        $result = $smcFunc['db_query']('', '
			SELECT value
			FROM {db_prefix}themes
			WHERE id_theme = {int:current_theme}
				AND id_member = {int:no_member}
				AND variable = {string:name}
			LIMIT 1', array('current_theme' => (int) $_GET['theme_id'], 'no_member' => 0, 'name' => 'name'));
        list($theme_name) = $smcFunc['db_fetch_row']($result);
        $smcFunc['db_free_result']($result);
        $context['sub_template'] = 'installed';
        $context['page_title'] = $txt['theme_installed'];
        $context['installed_theme'] = array('id' => (int) $_GET['theme_id'], 'name' => $theme_name);
        return;
    }
    if (!empty($_FILES['theme_gz']) && (!isset($_FILES['theme_gz']['error']) || $_FILES['theme_gz']['error'] != 4) || !empty($_REQUEST['theme_gz'])) {
        $method = 'upload';
    } elseif (isset($_REQUEST['theme_dir']) && rtrim(realpath($_REQUEST['theme_dir']), '/\\') != realpath($boarddir . '/Themes') && file_exists($_REQUEST['theme_dir'])) {
        $method = 'path';
    } else {
        $method = 'copy';
    }
    if (!empty($_REQUEST['copy']) && $method == 'copy') {
        // Hopefully the themes directory is writable, or we might have a problem.
        if (!is_writable($boarddir . '/Themes')) {
            fatal_lang_error('theme_install_write_error', 'critical');
        }
        $theme_dir = $boarddir . '/Themes/' . preg_replace('~[^A-Za-z0-9_\\- ]~', '', $_REQUEST['copy']);
        umask(0);
        mkdir($theme_dir, 0777);
        @set_time_limit(600);
        if (function_exists('apache_reset_timeout')) {
            @apache_reset_timeout();
        }
        // Create subdirectories for css and javascript files.
        mkdir($theme_dir . '/css', 0777);
        mkdir($theme_dir . '/scripts', 0777);
        // Copy over the default non-theme files.
        $to_copy = array('/index.php', '/index.template.php', '/css/index.css', '/css/rtl.css', '/scripts/theme.js');
        foreach ($to_copy as $file) {
            copy($settings['default_theme_dir'] . $file, $theme_dir . $file);
            @chmod($theme_dir . $file, 0777);
        }
        // And now the entire images directory!
        copytree($settings['default_theme_dir'] . '/images', $theme_dir . '/images');
        package_flush_cache();
        $theme_name = $_REQUEST['copy'];
        $images_url = $boardurl . '/Themes/' . basename($theme_dir) . '/images';
        $theme_dir = realpath($theme_dir);
        // Lets get some data for the new theme.
        $request = $smcFunc['db_query']('', '
			SELECT variable, value
			FROM {db_prefix}themes
			WHERE variable IN ({string:theme_templates}, {string:theme_layers})
				AND id_member = {int:no_member}
				AND id_theme = {int:default_theme}', array('no_member' => 0, 'default_theme' => 1, 'theme_templates' => 'theme_templates', 'theme_layers' => 'theme_layers'));
        while ($row = $smcFunc['db_fetch_assoc']($request)) {
            if ($row['variable'] == 'theme_templates') {
                $theme_templates = $row['value'];
            } elseif ($row['variable'] == 'theme_layers') {
                $theme_layers = $row['value'];
            } else {
                continue;
            }
        }
        $smcFunc['db_free_result']($request);
        // Lets add a theme_info.xml to this theme.
        $xml_info = '<' . '?xml version="1.0"?' . '>
<theme-info xmlns="http://www.simplemachines.org/xml/theme-info" xmlns:smf="http://www.simplemachines.org/">
	<!-- For the id, always use something unique - put your name, a colon, and then the package name. -->
	<id>smf:' . $smcFunc['strtolower'](str_replace(array(' '), '_', $_REQUEST['copy'])) . '</id>
	<version>' . $modSettings['smfVersion'] . '</version>
	<!-- Theme name, used purely for aesthetics. -->
	<name>' . $_REQUEST['copy'] . '</name>
	<!-- Author: your email address or contact information. The name attribute is optional. -->
	<author name="Simple Machines">info@simplemachines.org</author>
	<!-- Website... where to get updates and more information. -->
	<website>http://www.simplemachines.org/</website>
	<!-- Template layers to use, defaults to "html,body". -->
	<layers>' . (empty($theme_layers) ? 'html,body' : $theme_layers) . '</layers>
	<!-- Templates to load on startup. Default is "index". -->
	<templates>' . (empty($theme_templates) ? 'index' : $theme_templates) . '</templates>
	<!-- Base this theme off another? Default is blank, or no. It could be "default". -->
	<based-on></based-on>
</theme-info>';
        // Now write it.
        $fp = @fopen($theme_dir . '/theme_info.xml', 'w+');
        if ($fp) {
            fwrite($fp, $xml_info);
            fclose($fp);
        }
    } elseif (isset($_REQUEST['theme_dir']) && $method == 'path') {
        if (!is_dir($_REQUEST['theme_dir']) || !file_exists($_REQUEST['theme_dir'] . '/theme_info.xml')) {
            fatal_lang_error('theme_install_error', false);
        }
        $theme_name = basename($_REQUEST['theme_dir']);
        $theme_dir = $_REQUEST['theme_dir'];
    } elseif ($method = 'upload') {
        // Hopefully the themes directory is writable, or we might have a problem.
        if (!is_writable($boarddir . '/Themes')) {
            fatal_lang_error('theme_install_write_error', 'critical');
        }
        require_once $sourcedir . '/Subs-Package.php';
        // Set the default settings...
        $theme_name = strtok(basename(isset($_FILES['theme_gz']) ? $_FILES['theme_gz']['name'] : $_REQUEST['theme_gz']), '.');
        $theme_name = preg_replace(array('/\\s/', '/\\.[\\.]+/', '/[^\\w_\\.\\-]/'), array('_', '.', ''), $theme_name);
        $theme_dir = $boarddir . '/Themes/' . $theme_name;
        if (isset($_FILES['theme_gz']) && is_uploaded_file($_FILES['theme_gz']['tmp_name']) && (@ini_get('open_basedir') != '' || file_exists($_FILES['theme_gz']['tmp_name']))) {
            $extracted = read_tgz_file($_FILES['theme_gz']['tmp_name'], $boarddir . '/Themes/' . $theme_name, false, true);
        } elseif (isset($_REQUEST['theme_gz'])) {
            // Check that the theme is from simplemachines.org, for now... maybe add mirroring later.
            if (preg_match('~^http://[\\w_\\-]+\\.simplemachines\\.org/~', $_REQUEST['theme_gz']) == 0 || strpos($_REQUEST['theme_gz'], 'dlattach') !== false) {
                fatal_lang_error('not_on_simplemachines');
            }
            $extracted = read_tgz_file($_REQUEST['theme_gz'], $boarddir . '/Themes/' . $theme_name, false, true);
        } else {
            redirectexit('action=admin;area=theme;sa=admin;' . $context['session_var'] . '=' . $context['session_id']);
        }
    }
    // Something go wrong?
    if ($theme_dir != '' && basename($theme_dir) != 'Themes') {
        // Defaults.
        $install_info = array('theme_url' => $boardurl . '/Themes/' . basename($theme_dir), 'images_url' => isset($images_url) ? $images_url : $boardurl . '/Themes/' . basename($theme_dir) . '/images', 'theme_dir' => $theme_dir, 'name' => $theme_name);
        if (file_exists($theme_dir . '/theme_info.xml')) {
            $theme_info = file_get_contents($theme_dir . '/theme_info.xml');
            $xml_elements = array('name' => 'name', 'theme_layers' => 'layers', 'theme_templates' => 'templates', 'based_on' => 'based-on');
            foreach ($xml_elements as $var => $name) {
                if (preg_match('~<' . $name . '>(?:<!\\[CDATA\\[)?(.+?)(?:\\]\\]>)?</' . $name . '>~', $theme_info, $match) == 1) {
                    $install_info[$var] = $match[1];
                }
            }
            if (preg_match('~<images>(?:<!\\[CDATA\\[)?(.+?)(?:\\]\\]>)?</images>~', $theme_info, $match) == 1) {
                $install_info['images_url'] = $install_info['theme_url'] . '/' . $match[1];
                $explicit_images = true;
            }
            if (preg_match('~<extra>(?:<!\\[CDATA\\[)?(.+?)(?:\\]\\]>)?</extra>~', $theme_info, $match) == 1) {
                $install_info += unserialize($match[1]);
            }
        }
        if (isset($install_info['based_on'])) {
            if ($install_info['based_on'] == 'default') {
                $install_info['theme_url'] = $settings['default_theme_url'];
                $install_info['images_url'] = $settings['default_images_url'];
            } elseif ($install_info['based_on'] != '') {
                $install_info['based_on'] = preg_replace('~[^A-Za-z0-9\\-_ ]~', '', $install_info['based_on']);
                $request = $smcFunc['db_query']('', '
					SELECT th.value AS base_theme_dir, th2.value AS base_theme_url' . (!empty($explicit_images) ? '' : ', th3.value AS images_url') . '
					FROM {db_prefix}themes AS th
						INNER JOIN {db_prefix}themes AS th2 ON (th2.id_theme = th.id_theme
							AND th2.id_member = {int:no_member}
							AND th2.variable = {string:theme_url})' . (!empty($explicit_images) ? '' : '
						INNER JOIN {db_prefix}themes AS th3 ON (th3.id_theme = th.id_theme
							AND th3.id_member = {int:no_member}
							AND th3.variable = {string:images_url})') . '
					WHERE th.id_member = {int:no_member}
						AND (th.value LIKE {string:based_on} OR th.value LIKE {string:based_on_path})
						AND th.variable = {string:theme_dir}
					LIMIT 1', array('no_member' => 0, 'theme_url' => 'theme_url', 'images_url' => 'images_url', 'theme_dir' => 'theme_dir', 'based_on' => '%/' . $install_info['based_on'], 'based_on_path' => '%' . "\\" . $install_info['based_on']));
                $temp = $smcFunc['db_fetch_assoc']($request);
                $smcFunc['db_free_result']($request);
                // !!! An error otherwise?
                if (is_array($temp)) {
                    $install_info = $temp + $install_info;
                    if (empty($explicit_images) && !empty($install_info['base_theme_url'])) {
                        $install_info['theme_url'] = $install_info['base_theme_url'];
                    }
                }
            }
            unset($install_info['based_on']);
        }
        // Find the newest id_theme.
        $result = $smcFunc['db_query']('', '
			SELECT MAX(id_theme)
			FROM {db_prefix}themes', array());
        list($id_theme) = $smcFunc['db_fetch_row']($result);
        $smcFunc['db_free_result']($result);
        // This will be theme number...
        $id_theme++;
        $inserts = array();
        foreach ($install_info as $var => $val) {
            $inserts[] = array($id_theme, $var, $val);
        }
        if (!empty($inserts)) {
            $smcFunc['db_insert']('insert', '{db_prefix}themes', array('id_theme' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'), $inserts, array('id_theme', 'variable'));
        }
        updateSettings(array('knownThemes' => strtr($modSettings['knownThemes'] . ',' . $id_theme, array(',,' => ','))));
    }
    redirectexit('action=admin;area=theme;sa=install;theme_id=' . $id_theme . ';' . $context['session_var'] . '=' . $context['session_id']);
}
Ejemplo n.º 4
0
 /**
  * Apply another type of (avatar, language, etc.) package.
  */
 public function action_install2()
 {
     global $txt, $context, $boardurl, $scripturl, $modSettings;
     // Make sure we don't install this mod twice.
     checkSubmitOnce('check');
     checkSession();
     // If there's no file, what are we installing?
     if (!isset($_REQUEST['package']) || $_REQUEST['package'] == '') {
         redirectexit('action=admin;area=packages');
     }
     $context['filename'] = $_REQUEST['package'];
     // If this is an uninstall, we'll have an id.
     $context['install_id'] = isset($_REQUEST['pid']) ? (int) $_REQUEST['pid'] : 0;
     require_once SUBSDIR . '/Package.subs.php';
     require_once SUBSDIR . '/Themes.subs.php';
     // @todo Perhaps do it in steps, if necessary?
     $context['uninstalling'] = $_REQUEST['sa'] == 'uninstall2';
     // Set up the linktree for other.
     $context['linktree'][count($context['linktree']) - 1] = array('url' => $scripturl . '?action=admin;area=packages;sa=browse', 'name' => $context['uninstalling'] ? $txt['uninstall'] : $txt['extracting']);
     $context['page_title'] .= ' - ' . ($context['uninstalling'] ? $txt['uninstall'] : $txt['extracting']);
     $context['sub_template'] = 'extract_package';
     if (!file_exists(BOARDDIR . '/packages/' . $context['filename'])) {
         fatal_lang_error('package_no_file', false);
     }
     // Load up the package FTP information?
     create_chmod_control(array(), array('destination_url' => $scripturl . '?action=admin;area=packages;sa=' . $_REQUEST['sa'] . ';package=' . $_REQUEST['package']));
     // Make sure temp directory exists and is empty!
     if (file_exists(BOARDDIR . '/packages/temp')) {
         deltree(BOARDDIR . '/packages/temp', false);
     } else {
         mktree(BOARDDIR . '/packages/temp', 0777);
     }
     // Let the unpacker do the work.
     if (is_file(BOARDDIR . '/packages/' . $context['filename'])) {
         $context['extracted_files'] = read_tgz_file(BOARDDIR . '/packages/' . $context['filename'], BOARDDIR . '/packages/temp');
         if (!file_exists(BOARDDIR . '/packages/temp/package-info.xml')) {
             foreach ($context['extracted_files'] as $file) {
                 if (basename($file['filename']) == 'package-info.xml') {
                     $context['base_path'] = dirname($file['filename']) . '/';
                     break;
                 }
             }
         }
         if (!isset($context['base_path'])) {
             $context['base_path'] = '';
         }
     } elseif (is_dir(BOARDDIR . '/packages/' . $context['filename'])) {
         copytree(BOARDDIR . '/packages/' . $context['filename'], BOARDDIR . '/packages/temp');
         $context['extracted_files'] = listtree(BOARDDIR . '/packages/temp');
         $context['base_path'] = '';
     } else {
         fatal_lang_error('no_access', false);
     }
     // Are we installing this into any custom themes?
     $custom_themes = array(1);
     $known_themes = explode(',', $modSettings['knownThemes']);
     if (!empty($_POST['custom_theme'])) {
         foreach ($_POST['custom_theme'] as $tid) {
             if (in_array($tid, $known_themes)) {
                 $custom_themes[] = (int) $tid;
             }
         }
     }
     // Now load up the paths of the themes that we need to know about.
     $theme_paths = getThemesPathbyID($custom_themes);
     $themes_installed = array(1);
     // Are there any theme copying that we want to take place?
     $context['theme_copies'] = array('require-file' => array(), 'require-dir' => array());
     if (!empty($_POST['theme_changes'])) {
         foreach ($_POST['theme_changes'] as $change) {
             if (empty($change)) {
                 continue;
             }
             $theme_data = unserialize(base64_decode($change));
             if (empty($theme_data['type'])) {
                 continue;
             }
             $themes_installed[] = $theme_data['id'];
             $context['theme_copies'][$theme_data['type']][$theme_data['orig']][] = $theme_data['future'];
         }
     }
     // Get the package info...
     $packageInfo = getPackageInfo($context['filename']);
     if (!is_array($packageInfo)) {
         fatal_lang_error($packageInfo);
     }
     $packageInfo['filename'] = $context['filename'];
     // Set the type of extraction...
     $context['extract_type'] = isset($packageInfo['type']) ? $packageInfo['type'] : 'modification';
     // Create a backup file to roll back to! (but if they do this more than once, don't run it a zillion times.)
     if (!empty($modSettings['package_make_full_backups']) && (!isset($_SESSION['last_backup_for']) || $_SESSION['last_backup_for'] != $context['filename'] . ($context['uninstalling'] ? '$$' : '$'))) {
         $_SESSION['last_backup_for'] = $context['filename'] . ($context['uninstalling'] ? '$$' : '$');
         // @todo Internationalize this?
         package_create_backup(($context['uninstalling'] ? 'backup_' : 'before_') . strtok($context['filename'], '.'));
     }
     // The mod isn't installed.... unless proven otherwise.
     $context['is_installed'] = false;
     // Is it actually installed?
     $package_installed = isPackageInstalled($packageInfo['id']);
     // Wait, it's not installed yet!
     // @todo Replace with a better error message!
     if (!isset($package_installed['old_version']) && $context['uninstalling']) {
         deltree(BOARDDIR . '/packages/temp');
         fatal_error('Hacker?', false);
     } elseif ($context['uninstalling']) {
         $install_log = parsePackageInfo($packageInfo['xml'], false, 'uninstall');
         // Gadzooks!  There's no uninstaller at all!?
         if (empty($install_log)) {
             fatal_lang_error('package_uninstall_cannot', false);
         }
         // They can only uninstall from what it was originally installed into.
         foreach ($theme_paths as $id => $data) {
             if ($id != 1 && !in_array($id, $package_installed['old_themes'])) {
                 unset($theme_paths[$id]);
             }
         }
     } elseif (isset($package_installed['old_version']) && $package_installed['old_version'] != $packageInfo['version']) {
         // Look for an upgrade...
         $install_log = parsePackageInfo($packageInfo['xml'], false, 'upgrade', $package_installed['old_version']);
         // There was no upgrade....
         if (empty($install_log)) {
             $context['is_installed'] = true;
         } else {
             // Upgrade previous themes only!
             foreach ($theme_paths as $id => $data) {
                 if ($id != 1 && !in_array($id, $package_installed['old_themes'])) {
                     unset($theme_paths[$id]);
                 }
             }
         }
     } elseif (isset($package_installed['old_version']) && $package_installed['old_version'] == $packageInfo['version']) {
         $context['is_installed'] = true;
     }
     if (!isset($package_installed['old_version']) || $context['is_installed']) {
         $install_log = parsePackageInfo($packageInfo['xml'], false, 'install');
     }
     $context['install_finished'] = false;
     // We're gonna be needing the table db functions! ...Sometimes.
     $table_installer = db_table();
     // @todo Make a log of any errors that occurred and output them?
     if (!empty($install_log)) {
         $failed_steps = array();
         $failed_count = 0;
         foreach ($install_log as $action) {
             $failed_count++;
             if ($action['type'] == 'modification' && !empty($action['filename'])) {
                 if ($action['boardmod']) {
                     $mod_actions = parseBoardMod(file_get_contents(BOARDDIR . '/packages/temp/' . $context['base_path'] . $action['filename']), false, $action['reverse'], $theme_paths);
                 } else {
                     $mod_actions = parseModification(file_get_contents(BOARDDIR . '/packages/temp/' . $context['base_path'] . $action['filename']), false, $action['reverse'], $theme_paths);
                 }
                 // Any errors worth noting?
                 foreach ($mod_actions as $key => $action) {
                     if ($action['type'] == 'failure') {
                         $failed_steps[] = array('file' => $action['filename'], 'large_step' => $failed_count, 'sub_step' => $key, 'theme' => 1);
                     }
                     // Gather the themes we installed into.
                     if (!empty($action['is_custom'])) {
                         $themes_installed[] = $action['is_custom'];
                     }
                 }
             } elseif ($action['type'] == 'code' && !empty($action['filename'])) {
                 // This is just here as reference for what is available.
                 global $txt, $modSettings, $context;
                 // Now include the file and be done with it ;).
                 if (file_exists(BOARDDIR . '/packages/temp/' . $context['base_path'] . $action['filename'])) {
                     require BOARDDIR . '/packages/temp/' . $context['base_path'] . $action['filename'];
                 }
             } elseif ($action['type'] == 'credits') {
                 // Time to build the billboard
                 $credits_tag = array('url' => $action['url'], 'license' => $action['license'], 'copyright' => $action['copyright'], 'title' => $action['title']);
             } elseif ($action['type'] == 'hook' && isset($action['hook'], $action['function'])) {
                 if ($action['reverse']) {
                     remove_integration_function($action['hook'], $action['function'], $action['include_file']);
                 } else {
                     add_integration_function($action['hook'], $action['function'], $action['include_file']);
                 }
             } elseif ($action['type'] == 'database' && !empty($action['filename']) && (!$context['uninstalling'] || !empty($_POST['do_db_changes']))) {
                 // These can also be there for database changes.
                 global $txt, $modSettings, $context;
                 // Let the file work its magic ;)
                 if (file_exists(BOARDDIR . '/packages/temp/' . $context['base_path'] . $action['filename'])) {
                     require BOARDDIR . '/packages/temp/' . $context['base_path'] . $action['filename'];
                 }
             } elseif ($action['type'] == 'redirect' && !empty($action['redirect_url'])) {
                 $context['redirect_url'] = $action['redirect_url'];
                 $context['redirect_text'] = !empty($action['filename']) && file_exists(BOARDDIR . '/packages/temp/' . $context['base_path'] . $action['filename']) ? file_get_contents(BOARDDIR . '/packages/temp/' . $context['base_path'] . $action['filename']) : ($context['uninstalling'] ? $txt['package_uninstall_done'] : $txt['package_installed_done']);
                 $context['redirect_timeout'] = $action['redirect_timeout'];
                 // Parse out a couple of common urls.
                 $urls = array('$boardurl' => $boardurl, '$scripturl' => $scripturl, '$session_var' => $context['session_var'], '$session_id' => $context['session_id']);
                 $context['redirect_url'] = strtr($context['redirect_url'], $urls);
             }
         }
         package_flush_cache();
         // First, ensure this change doesn't get removed by putting a stake in the ground (So to speak).
         package_put_contents(BOARDDIR . '/packages/installed.list', time());
         // See if this is already installed
         $is_upgrade = false;
         $old_db_changes = array();
         $package_check = isPackageInstalled($packageInfo['id']);
         // Change the installed state as required.
         if (!empty($package_check['install_state'])) {
             if ($context['uninstalling']) {
                 setPackageState($package_check['package_id']);
             } else {
                 // not uninstalling so must be an upgrade
                 $is_upgrade = true;
                 $old_db_changes = empty($package_check['db_changes']) ? array() : $package_check['db_changes'];
             }
         }
         // Assuming we're not uninstalling, add the entry.
         if (!$context['uninstalling']) {
             // Any db changes from older version?
             $table_log = $table_installer->package_log();
             if (!empty($old_db_changes)) {
                 $db_package_log = empty($table_log) ? $old_db_changes : array_merge($old_db_changes, $table_log);
             } else {
                 $db_package_log = $table_log;
             }
             // If there are some database changes we might want to remove then filter them out.
             if (!empty($db_package_log)) {
                 // We're really just checking for entries which are create table AND add columns (etc).
                 $tables = array();
                 usort($db_package_log, array($this, '_sort_table_first'));
                 foreach ($db_package_log as $k => $log) {
                     if ($log[0] == 'remove_table') {
                         $tables[] = $log[1];
                     } elseif (in_array($log[1], $tables)) {
                         unset($db_package_log[$k]);
                     }
                 }
                 $package_installed['db_changes'] = serialize($db_package_log);
             } else {
                 $package_installed['db_changes'] = '';
             }
             // What themes did we actually install?
             $themes_installed = array_unique($themes_installed);
             $themes_installed = implode(',', $themes_installed);
             // What failed steps?
             $failed_step_insert = serialize($failed_steps);
             // Credits tag?
             $credits_tag = empty($credits_tag) ? '' : serialize($credits_tag);
             // Add to the log packages
             addPackageLog($packageInfo, $failed_step_insert, $themes_installed, $package_installed['db_changes'], $is_upgrade, $credits_tag);
         }
         $context['install_finished'] = true;
     }
     // If there's database changes - and they want them removed - let's do it last!
     if (!empty($package_installed['db_changes']) && !empty($_POST['do_db_changes'])) {
         foreach ($package_installed['db_changes'] as $change) {
             if ($change[0] == 'remove_table' && isset($change[1])) {
                 $table_installer->db_drop_table($change[1]);
             } elseif ($change[0] == 'remove_column' && isset($change[2])) {
                 $table_installer->db_remove_column($change[1], $change[2]);
             } elseif ($change[0] == 'remove_index' && isset($change[2])) {
                 $table_installer->db_remove_index($change[1], $change[2]);
             }
         }
     }
     // Clean house... get rid of the evidence ;).
     if (file_exists(BOARDDIR . '/packages/temp')) {
         deltree(BOARDDIR . '/packages/temp');
     }
     // Log what we just did.
     logAction($context['uninstalling'] ? 'uninstall_package' : (!empty($is_upgrade) ? 'upgrade_package' : 'install_package'), array('package' => Util::htmlspecialchars($packageInfo['name']), 'version' => Util::htmlspecialchars($packageInfo['version'])), 'admin');
     // Just in case, let's clear the whole cache to avoid anything going up the swanny.
     clean_cache();
     // Restore file permissions?
     create_chmod_control(array(), array(), true);
 }
Ejemplo n.º 5
0
function PackageInstall()
{
    global $boarddir, $txt, $context, $boardurl, $scripturl, $sourcedir, $modSettings;
    checkSession('post');
    // If there's no file, what are we installing?
    if (empty($_REQUEST['package']) || preg_match('~[^\\w0-9.\\-_]~', $_REQUEST['package']) === 1 || strpos($_REQUEST['package'], '..') !== false) {
        redirectexit('action=packages');
    }
    $context['filename'] = $_REQUEST['package'];
    require_once $sourcedir . '/Subs-Package.php';
    // !!! TODO: Perhaps do it in steps, if necessary?
    $context['uninstalling'] = $_REQUEST['sa'] == 'uninstall2';
    // Set up the linktree for other.
    $context['linktree'][] = array('url' => $scripturl . '?action=packages;sa=browse', 'name' => $context['uninstalling'] ? $txt['smf198b'] : $txt['package37']);
    $context['page_title'] .= ' - ' . ($context['uninstalling'] ? $txt['smf198b'] : $txt['package37']);
    $context['sub_template'] = 'extract_package';
    if (!file_exists($boarddir . '/Packages/' . $context['filename'])) {
        fatal_lang_error('package_no_file', false);
    }
    // Load up the package FTP information?
    if (isset($_SESSION['pack_ftp'])) {
        packageRequireFTP($scripturl . '?action=packages;sa=' . $_REQUEST['sa'] . ';package=' . $_REQUEST['package']);
    }
    // Make sure temp directory exists and is empty!
    if (file_exists($boarddir . '/Packages/temp')) {
        deltree($boarddir . '/Packages/temp', false);
    } else {
        mktree($boarddir . '/Packages/temp', 0777);
    }
    // Let the unpacker do the work.
    if (is_file($boarddir . '/Packages/' . $context['filename'])) {
        $context['extracted_files'] = read_tgz_file($boarddir . '/Packages/' . $context['filename'], $boarddir . '/Packages/temp');
        if (!file_exists($boarddir . '/Packages/temp/package-info.xml')) {
            foreach ($context['extracted_files'] as $file) {
                if (basename($file['filename']) == 'package-info.xml') {
                    $context['base_path'] = dirname($file['filename']) . '/';
                    break;
                }
            }
        }
        if (!isset($context['base_path'])) {
            $context['base_path'] = '';
        }
    } elseif (is_dir($boarddir . '/Packages/' . $context['filename'])) {
        copytree($boarddir . '/Packages/' . $context['filename'], $boarddir . '/Packages/temp');
        $context['extracted_files'] = listtree($boarddir . '/Packages/temp');
        $context['base_path'] = '';
    } else {
        fatal_lang_error(1, false);
    }
    // Get the package info...
    $packageInfo = getPackageInfo($context['filename']);
    $packageInfo['filename'] = $context['filename'];
    // Set the type of extraction...
    $context['extract_type'] = isset($packageInfo['type']) ? $packageInfo['type'] : 'modification';
    // Create a backup file to roll back to! (but if they do this more than once, don't run it a zillion times.)
    if (!empty($modSettings['package_make_backups']) && (!isset($_SESSION['last_backup_for']) || $_SESSION['last_backup_for'] != $context['filename'] . ($context['uninstalling'] ? '$$' : '$'))) {
        $_SESSION['last_backup_for'] = $context['filename'] . ($context['uninstalling'] ? '$$' : '$');
        // !!! Internationalize this?
        package_create_backup(($context['uninstalling'] ? 'backup_' : 'before_') . strtok($context['filename'], '.'));
    }
    $instmods = loadInstalledPackages();
    // The mod isn't installed.... unless proven otherwise.
    $context['is_installed'] = false;
    foreach ($instmods as $installed_mod) {
        if ($installed_mod['id'] == $packageInfo['id']) {
            $old_version = $installed_mod['version'];
        }
    }
    // Wait, it's not installed yet!
    // !!! TODO: Replace with a better error message!
    if (!isset($old_version) && $context['uninstalling']) {
        deltree($boarddir . '/Packages/temp');
        fatal_error('Hacker?', false);
    } elseif ($context['uninstalling']) {
        $install_log = parsePackageInfo($packageInfo['xml'], false, 'uninstall');
        // Gadzooks!  There's no uninstaller at all!?
        if (empty($install_log)) {
            fatal_lang_error('package_uninstall_cannot', false);
        }
    } elseif (isset($old_version) && $old_version != $packageInfo['version']) {
        // Look for an upgrade...
        $install_log = parsePackageInfo($packageInfo['xml'], false, 'upgrade', $old_version);
        // There was no upgrade....
        if (empty($install_log)) {
            $context['is_installed'] = true;
        }
    } elseif (isset($old_version) && $old_version == $packageInfo['version']) {
        $context['is_installed'] = true;
    }
    if (!isset($old_version) || $context['is_installed']) {
        $install_log = parsePackageInfo($packageInfo['xml'], false, 'install');
    }
    $context['install_finished'] = false;
    // !!! TODO: Make a log of any errors that occurred and output them?
    if (!empty($install_log)) {
        foreach ($install_log as $action) {
            if ($action['type'] == 'modification' && !empty($action['filename'])) {
                if ($action['boardmod']) {
                    parseBoardMod(file_get_contents($boarddir . '/Packages/temp/' . $context['base_path'] . $action['filename']), false, $action['reverse']);
                } else {
                    parseModification(file_get_contents($boarddir . '/Packages/temp/' . $context['base_path'] . $action['filename']), false, $action['reverse']);
                }
            } elseif ($action['type'] == 'code' && !empty($action['filename'])) {
                // This is just here as reference for what is available.
                global $txt, $boarddir, $sourcedir, $modSettings, $context, $settings, $db_prefix, $forum_version;
                // Now include the file and be done with it ;).
                require $boarddir . '/Packages/temp/' . $context['base_path'] . $action['filename'];
            } elseif ($action['type'] == 'redirect' && !empty($action['redirect_url'])) {
                $context['redirect_url'] = $action['redirect_url'];
                $context['redirect_text'] = file_get_contents($boarddir . '/Packages/temp/' . $context['base_path'] . $action['filename']);
                $context['redirect_timeout'] = $action['redirect_timeout'];
                // Parse out a couple of common urls.
                $urls = array('$boardurl' => $boardurl, '$scripturl' => $scripturl, '$session_id' => $context['session_id']);
                $context['redirect_url'] = strtr($context['redirect_url'], $urls);
            }
        }
        package_flush_cache();
        // Check if the mod has been installed.
        $seen = false;
        // Look through the list of installed mods...
        foreach ($instmods as $i => $installed_mod) {
            if ($installed_mod['id'] == $packageInfo['id']) {
                if ($context['uninstalling']) {
                    $instmods[$i] = array();
                } else {
                    $instmods[$i]['version'] = $packageInfo['version'];
                    $seen = true;
                }
                break;
            }
        }
        // Hasn't.... make it show as installed.
        if (!$seen && !$context['uninstalling']) {
            $instmods[] = $packageInfo;
        }
        saveInstalledPackages($instmods);
        $context['install_finished'] = true;
    }
    // Clean house... get rid of the evidence ;).
    if (file_exists($boarddir . '/Packages/temp')) {
        deltree($boarddir . '/Packages/temp');
    }
}
Ejemplo n.º 6
0
function PackageInstall()
{
    global $boarddir, $txt, $context, $boardurl, $scripturl, $sourcedir, $modSettings;
    global $user_info, $smcFunc;
    // Make sure we don't install this mod twice.
    checkSubmitOnce('check');
    checkSession();
    // If there's no file, what are we installing?
    if (!isset($_REQUEST['package']) || $_REQUEST['package'] == '') {
        redirectexit('action=admin;area=packages');
    }
    $context['filename'] = $_REQUEST['package'];
    // If this is an uninstall, we'll have an id.
    $context['install_id'] = isset($_REQUEST['pid']) ? (int) $_REQUEST['pid'] : 0;
    require_once $sourcedir . '/lib/Subs-Package.php';
    // !!! TODO: Perhaps do it in steps, if necessary?
    $context['uninstalling'] = $_REQUEST['sa'] == 'uninstall2';
    // Set up the linktree for other.
    $context['linktree'][count($context['linktree']) - 1] = array('url' => $scripturl . '?action=admin;area=packages;sa=browse', 'name' => $context['uninstalling'] ? $txt['uninstall'] : $txt['extracting']);
    $context['page_title'] .= ' - ' . ($context['uninstalling'] ? $txt['uninstall'] : $txt['extracting']);
    $context['sub_template'] = 'extract_package';
    if (!file_exists($boarddir . '/Packages/' . $context['filename'])) {
        fatal_lang_error('package_no_file', false);
    }
    // Load up the package FTP information?
    create_chmod_control(array(), array('destination_url' => $scripturl . '?action=admin;area=packages;sa=' . $_REQUEST['sa'] . ';package=' . $_REQUEST['package']));
    // Make sure temp directory exists and is empty!
    if (file_exists($boarddir . '/Packages/temp')) {
        deltree($boarddir . '/Packages/temp', false);
    } else {
        mktree($boarddir . '/Packages/temp', 0777);
    }
    // Let the unpacker do the work.
    if (is_file($boarddir . '/Packages/' . $context['filename'])) {
        $context['extracted_files'] = read_tgz_file($boarddir . '/Packages/' . $context['filename'], $boarddir . '/Packages/temp');
        if (!file_exists($boarddir . '/Packages/temp/package-info.xml')) {
            foreach ($context['extracted_files'] as $file) {
                if (basename($file['filename']) == 'package-info.xml') {
                    $context['base_path'] = dirname($file['filename']) . '/';
                    break;
                }
            }
        }
        if (!isset($context['base_path'])) {
            $context['base_path'] = '';
        }
    } elseif (is_dir($boarddir . '/Packages/' . $context['filename'])) {
        copytree($boarddir . '/Packages/' . $context['filename'], $boarddir . '/Packages/temp');
        $context['extracted_files'] = listtree($boarddir . '/Packages/temp');
        $context['base_path'] = '';
    } else {
        fatal_lang_error('no_access', false);
    }
    // Are we installing this into any custom themes?
    $custom_themes = array(1);
    $known_themes = explode(',', $modSettings['knownThemes']);
    if (!empty($_POST['custom_theme'])) {
        foreach ($_POST['custom_theme'] as $tid) {
            if (in_array($tid, $known_themes)) {
                $custom_themes[] = (int) $tid;
            }
        }
    }
    // Now load up the paths of the themes that we need to know about.
    $request = smf_db_query('
		SELECT id_theme, variable, value
		FROM {db_prefix}themes
		WHERE id_theme IN ({array_int:custom_themes})
			AND variable IN ({string:name}, {string:theme_dir})', array('custom_themes' => $custom_themes, 'name' => 'name', 'theme_dir' => 'theme_dir'));
    $theme_paths = array();
    $themes_installed = array(1);
    while ($row = mysql_fetch_assoc($request)) {
        $theme_paths[$row['id_theme']][$row['variable']] = $row['value'];
    }
    mysql_free_result($request);
    // Are there any theme copying that we want to take place?
    $context['theme_copies'] = array('require-file' => array(), 'require-dir' => array());
    if (!empty($_POST['theme_changes'])) {
        foreach ($_POST['theme_changes'] as $change) {
            if (empty($change)) {
                continue;
            }
            $theme_data = unserialize(base64_decode($change));
            if (empty($theme_data['type'])) {
                continue;
            }
            $themes_installed[] = $theme_data['id'];
            $context['theme_copies'][$theme_data['type']][$theme_data['orig']][] = $theme_data['future'];
        }
    }
    // Get the package info...
    $packageInfo = getPackageInfo($context['filename']);
    if (!is_array($packageInfo)) {
        fatal_lang_error($packageInfo);
    }
    $packageInfo['filename'] = $context['filename'];
    // Set the type of extraction...
    $context['extract_type'] = isset($packageInfo['type']) ? $packageInfo['type'] : 'modification';
    // Create a backup file to roll back to! (but if they do this more than once, don't run it a zillion times.)
    if (!empty($modSettings['package_make_backups']) && (!isset($_SESSION['last_backup_for']) || $_SESSION['last_backup_for'] != $context['filename'] . ($context['uninstalling'] ? '$$' : '$'))) {
        $_SESSION['last_backup_for'] = $context['filename'] . ($context['uninstalling'] ? '$$' : '$');
        // !!! Internationalize this?
        package_create_backup(($context['uninstalling'] ? 'backup_' : 'before_') . strtok($context['filename'], '.'));
    }
    // The mod isn't installed.... unless proven otherwise.
    $context['is_installed'] = false;
    // Is it actually installed?
    $request = smf_db_query('
		SELECT version, themes_installed, db_changes
		FROM {db_prefix}log_packages
		WHERE package_id = {string:current_package}
			AND install_state != {int:not_installed}
		ORDER BY time_installed DESC
		LIMIT 1', array('not_installed' => 0, 'current_package' => $packageInfo['id']));
    while ($row = mysql_fetch_assoc($request)) {
        $old_themes = explode(',', $row['themes_installed']);
        $old_version = $row['version'];
        $db_changes = empty($row['db_changes']) ? array() : unserialize($row['db_changes']);
    }
    mysql_free_result($request);
    // Wait, it's not installed yet!
    // !!! TODO: Replace with a better error message!
    if (!isset($old_version) && $context['uninstalling']) {
        deltree($boarddir . '/Packages/temp');
        fatal_error('Hacker?', false);
    } elseif ($context['uninstalling']) {
        $install_log = parsePackageInfo($packageInfo['xml'], false, 'uninstall');
        // Gadzooks!  There's no uninstaller at all!?
        if (empty($install_log)) {
            fatal_lang_error('package_uninstall_cannot', false);
        }
        // They can only uninstall from what it was originally installed into.
        foreach ($theme_paths as $id => $data) {
            if ($id != 1 && !in_array($id, $old_themes)) {
                unset($theme_paths[$id]);
            }
        }
    } elseif (isset($old_version) && $old_version != $packageInfo['version']) {
        // Look for an upgrade...
        $install_log = parsePackageInfo($packageInfo['xml'], false, 'upgrade', $old_version);
        // There was no upgrade....
        if (empty($install_log)) {
            $context['is_installed'] = true;
        } else {
            // Upgrade previous themes only!
            foreach ($theme_paths as $id => $data) {
                if ($id != 1 && !in_array($id, $old_themes)) {
                    unset($theme_paths[$id]);
                }
            }
        }
    } elseif (isset($old_version) && $old_version == $packageInfo['version']) {
        $context['is_installed'] = true;
    }
    if (!isset($old_version) || $context['is_installed']) {
        $install_log = parsePackageInfo($packageInfo['xml'], false, 'install');
    }
    $context['install_finished'] = false;
    // !!! TODO: Make a log of any errors that occurred and output them?
    if (!empty($install_log)) {
        $failed_steps = array();
        $failed_count = 0;
        foreach ($install_log as $action) {
            $failed_count++;
            if ($action['type'] == 'modification' && !empty($action['filename'])) {
                if ($action['boardmod']) {
                    $mod_actions = parseBoardMod(file_get_contents($boarddir . '/Packages/temp/' . $context['base_path'] . $action['filename']), false, $action['reverse'], $theme_paths);
                } else {
                    $mod_actions = parseModification(file_get_contents($boarddir . '/Packages/temp/' . $context['base_path'] . $action['filename']), false, $action['reverse'], $theme_paths);
                }
                // Any errors worth noting?
                foreach ($mod_actions as $key => $action) {
                    if ($action['type'] == 'failure') {
                        $failed_steps[] = array('file' => $action['filename'], 'large_step' => $failed_count, 'sub_step' => $key, 'theme' => 1);
                    }
                    // Gather the themes we installed into.
                    if (!empty($action['is_custom'])) {
                        $themes_installed[] = $action['is_custom'];
                    }
                }
            } elseif ($action['type'] == 'code' && !empty($action['filename'])) {
                // This is just here as reference for what is available.
                global $txt, $boarddir, $sourcedir, $modSettings, $context, $settings, $forum_version, $smcFunc;
                // Now include the file and be done with it ;).
                require $boarddir . '/Packages/temp/' . $context['base_path'] . $action['filename'];
            } elseif ($action['type'] == 'database' && !empty($action['filename']) && (!$context['uninstalling'] || !empty($_POST['do_db_changes']))) {
                // These can also be there for database changes.
                global $txt, $boarddir, $sourcedir, $modSettings, $context, $settings, $forum_version, $smcFunc;
                global $db_package_log;
                // We'll likely want the package specific database functionality!
                db_extend('packages');
                // Let the file work its magic ;)
                require $boarddir . '/Packages/temp/' . $context['base_path'] . $action['filename'];
            } elseif ($action['type'] == 'redirect' && !empty($action['redirect_url'])) {
                $context['redirect_url'] = $action['redirect_url'];
                $context['redirect_text'] = !empty($action['filename']) && file_exists($boarddir . '/Packages/temp/' . $context['base_path'] . $action['filename']) ? file_get_contents($boarddir . '/Packages/temp/' . $context['base_path'] . $action['filename']) : ($context['uninstalling'] ? $txt['package_uninstall_done'] : $txt['package_installed_done']);
                $context['redirect_timeout'] = $action['redirect_timeout'];
                // Parse out a couple of common urls.
                $urls = array('$boardurl' => $boardurl, '$scripturl' => $scripturl, '$session_var' => $context['session_var'], '$session_id' => $context['session_id']);
                $context['redirect_url'] = strtr($context['redirect_url'], $urls);
            }
        }
        package_flush_cache();
        // First, ensure this change doesn't get removed by putting a stake in the ground (So to speak).
        package_put_contents($boarddir . '/Packages/installed.list', time());
        // See if this is already installed, and change it's state as required.
        $request = smf_db_query('
			SELECT package_id, install_state, db_changes
			FROM {db_prefix}log_packages
			WHERE install_state != {int:not_installed}
				AND package_id = {string:current_package}
				' . ($context['install_id'] ? ' AND id_install = {int:install_id} ' : '') . '
			ORDER BY time_installed DESC
			LIMIT 1', array('not_installed' => 0, 'install_id' => $context['install_id'], 'current_package' => $packageInfo['id']));
        $is_upgrade = false;
        while ($row = mysql_fetch_assoc($request)) {
            // Uninstalling?
            if ($context['uninstalling']) {
                smf_db_query('
					UPDATE {db_prefix}log_packages
					SET install_state = {int:not_installed}, member_removed = {string:member_name}, id_member_removed = {int:current_member},
						time_removed = {int:current_time}
					WHERE package_id = {string:package_id}', array('current_member' => $user_info['id'], 'not_installed' => 0, 'current_time' => time(), 'package_id' => $row['package_id'], 'member_name' => $user_info['name']));
            } else {
                $is_upgrade = true;
                $old_db_changes = empty($row['db_changes']) ? array() : unserialize($row['db_changes']);
            }
        }
        // Assuming we're not uninstalling, add the entry.
        if (!$context['uninstalling']) {
            // Any db changes from older version?
            if (!empty($old_db_changes)) {
                $db_package_log = empty($db_package_log) ? $old_db_changes : array_merge($old_db_changes, $db_package_log);
            }
            // If there are some database changes we might want to remove then filter them out.
            if (!empty($db_package_log)) {
                // We're really just checking for entries which are create table AND add columns (etc).
                $tables = array();
                function sort_table_first($a, $b)
                {
                    if ($a[0] == $b[0]) {
                        return 0;
                    }
                    return $a[0] == 'remove_table' ? -1 : 1;
                }
                usort($db_package_log, 'sort_table_first');
                foreach ($db_package_log as $k => $log) {
                    if ($log[0] == 'remove_table') {
                        $tables[] = $log[1];
                    } elseif (in_array($log[1], $tables)) {
                        unset($db_package_log[$k]);
                    }
                }
                $db_changes = serialize($db_package_log);
            } else {
                $db_changes = '';
            }
            // What themes did we actually install?
            $themes_installed = array_unique($themes_installed);
            $themes_installed = implode(',', $themes_installed);
            // What failed steps?
            $failed_step_insert = serialize($failed_steps);
            smf_db_insert('', '{db_prefix}log_packages', array('filename' => 'string', 'name' => 'string', 'package_id' => 'string', 'version' => 'string', 'id_member_installed' => 'int', 'member_installed' => 'string', 'time_installed' => 'int', 'install_state' => 'int', 'failed_steps' => 'string', 'themes_installed' => 'string', 'member_removed' => 'int', 'db_changes' => 'string'), array($packageInfo['filename'], $packageInfo['name'], $packageInfo['id'], $packageInfo['version'], $user_info['id'], $user_info['name'], time(), $is_upgrade ? 2 : 1, $failed_step_insert, $themes_installed, 0, $db_changes), array('id_install'));
        }
        mysql_free_result($request);
        $context['install_finished'] = true;
    }
    // If there's database changes - and they want them removed - let's do it last!
    if (!empty($db_changes) && !empty($_POST['do_db_changes'])) {
        // We're gonna be needing the package db functions!
        db_extend('packages');
        foreach ($db_changes as $change) {
            if ($change[0] == 'remove_table' && isset($change[1])) {
                smf_db_drop_table($change[1]);
            } elseif ($change[0] == 'remove_column' && isset($change[2])) {
                smf_db_remove_column($change[1], $change[2]);
            } elseif ($change[0] == 'remove_index' && isset($change[2])) {
                smf_db_remove_index($change[1], $change[2]);
            }
        }
    }
    // Clean house... get rid of the evidence ;).
    if (file_exists($boarddir . '/Packages/temp')) {
        deltree($boarddir . '/Packages/temp');
    }
    // Log what we just did.
    logAction($context['uninstalling'] ? 'uninstall_package' : (!empty($is_upgrade) ? 'upgrade_package' : 'install_package'), array('package' => commonAPI::htmlspecialchars($packageInfo['name']), 'version' => commonAPI::htmlspecialchars($packageInfo['version'])), 'admin');
    // Just in case, let's clear the whole cache to avoid anything going up the swanny.
    clean_cache();
    // Restore file permissions?
    create_chmod_control(array(), array(), true);
}
Ejemplo n.º 7
0
/**
 * Extract zip data.
 *
 * - If destination is null, return a listing.
 *
 * @package Packages
 * @param string $data
 * @param string $destination
 * @param bool $single_file
 * @param bool $overwrite
 * @param string[]|null $files_to_extract
 */
function read_zip_data($data, $destination, $single_file = false, $overwrite = false, $files_to_extract = null)
{
    umask(0);
    if ($destination !== null && !file_exists($destination) && !$single_file) {
        mktree($destination, 0777);
    }
    // Look for the end of directory signature 0x06054b50
    $data_ecr = explode("PK", $data);
    if (!isset($data_ecr[1])) {
        return false;
    }
    $return = array();
    // Get all the basic zip file info since we are here
    $zip_info = unpack('vdisknum/vdisks/vrecords/vfiles/Vsize/Voffset/vcomment_length', $data_ecr[1]);
    $zip_info['comment'] = substr($data_ecr[1], 18, $zip_info['comment_length']);
    // Cut file at the central directory file header signature -- 0x02014b50, use unpack if you want any of the data, we don't
    $file_sections = explode("PK", $data);
    // Cut the result on each local file header -- 0x04034b50 so we have each file in the archive as an element.
    $file_sections = explode("PK", $file_sections[0]);
    array_shift($file_sections);
    // Sections and count from the signature must match or the zip file is bad
    if (count($file_sections) != $zip_info['files']) {
        return false;
    }
    // Go though each file in the archive
    foreach ($file_sections as $data) {
        // Get all the important file information.
        $file_info = unpack('vversion/vgeneral_purpose/vcompress_method/vfile_time/vfile_date/Vcrc/Vcompressed_size/Vsize/vfilename_length/vextrafield_length', $data);
        $file_info['filename'] = substr($data, 26, $file_info['filename_length']);
        $file_info['dir'] = $destination . '/' . dirname($file_info['filename']);
        // If bit 3 (0x08) of the general-purpose flag is set, then the CRC and file size were not available when the header was written
        // In this case the CRC and size are instead appended in a 12-byte structure immediately after the compressed data
        if ($file_info['general_purpose'] & 0x8) {
            $unzipped2 = unpack('Vcrc/Vcompressed_size/Vsize', substr($data, -12));
            $file_info['crc'] = $unzipped2['crc'];
            $file_info['compressed_size'] = $unzipped2['compressed_size'];
            $file_info['size'] = $unzipped2['size'];
            unset($unzipped2);
        }
        // If this is a file, and it doesn't exist.... happy days!
        if (substr($file_info['filename'], -1) != '/' && !file_exists($destination . '/' . $file_info['filename'])) {
            $write_this = true;
        } elseif (substr($file_info['filename'], -1) != '/') {
            $write_this = $overwrite;
        } elseif ($destination !== null && !$single_file) {
            // Just a little accident prevention, don't mind me.
            $file_info['filename'] = strtr($file_info['filename'], array('../' => '', '/..' => ''));
            if (!file_exists($destination . '/' . $file_info['filename'])) {
                mktree($destination . '/' . $file_info['filename'], 0777);
            }
            $write_this = false;
        } else {
            $write_this = false;
        }
        // Get the actual compressed data.
        $file_info['data'] = substr($data, 26 + $file_info['filename_length'] + $file_info['extrafield_length']);
        // Only inflate it if we need to ;)
        if (!empty($file_info['compress_method']) || $file_info['compressed_size'] != $file_info['size']) {
            $file_info['data'] = gzinflate($file_info['data']);
        }
        // Okay!  We can write this file, looks good from here...
        if ($write_this && $destination !== null) {
            if (strpos($file_info['filename'], '/') !== false && !$single_file || !$single_file && !is_dir($file_info['dir'])) {
                mktree($file_info['dir'], 0777);
            }
            // If we're looking for a specific file, and this is it... ka-bam, baby.
            if ($single_file && ($destination == $file_info['filename'] || $destination == '*/' . basename($file_info['filename']))) {
                return $file_info['data'];
            } elseif ($single_file) {
                continue;
            } elseif ($files_to_extract !== null && !in_array($file_info['filename'], $files_to_extract)) {
                continue;
            }
            package_put_contents($destination . '/' . $file_info['filename'], $file_info['data']);
        }
        // Not a directory, add it to our results
        if (substr($file_info['filename'], -1, 1) != '/') {
            $return[] = array('filename' => $file_info['filename'], 'md5' => md5($file_info['data']), 'preview' => substr($file_info['data'], 0, 100), 'size' => $file_info['size'], 'skipped' => false);
        }
    }
    if ($destination !== null && !$single_file) {
        package_flush_cache();
    }
    if ($single_file) {
        return false;
    } else {
        return $return;
    }
}
Ejemplo n.º 8
0
function CleanupMods()
{
    global $db_prefix, $modSettings, $upcontext, $boarddir, $sourcedir, $settings, $smcFunc, $command_line;
    // Sorry. Not supported for command line users.
    if ($command_line) {
        return true;
    }
    // Skipping first?
    if (!empty($_POST['skip'])) {
        unset($_POST['skip']);
        return true;
    }
    // If we get here withOUT SSI we need to redirect to ensure we get it!
    if (!isset($_GET['ssi']) || !function_exists('mktree')) {
        redirectLocation('&ssi=1');
    }
    $upcontext['sub_template'] = 'clean_mods';
    $upcontext['page_title'] = 'Cleanup Modifications';
    // This can be skipped.
    $upcontext['skip'] = true;
    // If we're on the second redirect continue...
    if (isset($_POST['cleandone2'])) {
        return true;
    }
    // Do we already know about some writable files?
    if (isset($_POST['writable_files'])) {
        $writable_files = unserialize(base64_decode($_POST['writable_files']));
        if (!makeFilesWritable($writable_files)) {
            // What have we left?
            $upcontext['writable_files'] = $writable_files;
            return false;
        }
    }
    // Load all theme paths....
    $request = $smcFunc['db_query']('', '
		SELECT id_theme, variable, value
		FROM {db_prefix}themes
		WHERE id_member = {int:id_member}
			AND variable IN ({string:theme_dir}, {string:images_url})', array('id_member' => 0, 'theme_dir' => 'theme_dir', 'images_url' => 'images_url', 'db_error_skip' => true));
    $theme_paths = array();
    while ($row = $smcFunc['db_fetch_assoc']($request)) {
        if ($row['id_theme'] == 1) {
            $settings['default_' . $row['variable']] = $row['value'];
        } elseif ($row['variable'] == 'theme_dir') {
            $theme_paths[$row['id_theme']][$row['variable']] = $row['value'];
        }
    }
    $smcFunc['db_free_result']($request);
    // Are there are mods installed that may need uninstalling?
    $request = $smcFunc['db_query']('', '
		SELECT id_install, filename, name, themes_installed, version
		FROM {db_prefix}log_packages
		WHERE install_state = {int:installed}
		ORDER BY time_installed DESC', array('installed' => 1, 'db_error_skip' => true));
    $upcontext['packages'] = array();
    while ($row = $smcFunc['db_fetch_assoc']($request)) {
        // Work out the status.
        if (!file_exists($boarddir . '/Packages/' . $row['filename'])) {
            $status = 'Missing';
            $status_color = 'red';
            $result = 'Removed';
        } else {
            $status = 'Installed';
            $status_color = 'green';
            $result = 'No Action Needed';
        }
        $upcontext['packages'][$row['id_install']] = array('id' => $row['id_install'], 'themes' => explode(',', $row['themes_installed']), 'name' => $row['name'], 'filename' => $row['filename'], 'missing_file' => file_exists($boarddir . '/Packages/' . $row['filename']) ? 0 : 1, 'files' => array(), 'file_count' => 0, 'status' => $status, 'result' => $result, 'color' => $status_color, 'version' => $row['version'], 'needs_removing' => false);
    }
    $smcFunc['db_free_result']($request);
    // Don't carry on if there are none.
    if (empty($upcontext['packages'])) {
        return true;
    }
    // Setup some basics.
    if (!empty($upcontext['user']['version'])) {
        $_SESSION['version_emulate'] = $upcontext['user']['version'];
    }
    // Before we get started, don't report notice errors.
    $oldErrorReporting = error_reporting(E_ALL ^ E_NOTICE);
    if (!mktree($boarddir . '/Packages/temp', 0755)) {
        deltree($boarddir . '/Packages/temp', false);
        if (!mktree($boarddir . '/Packages/temp', 0777)) {
            deltree($boarddir . '/Packages/temp', false);
            //!!! Error here - plus chmod!
        }
    }
    // Anything which reinstalled should not have its entry removed.
    $reinstall_worked = array();
    // We're gonna be doing some removin'
    $test = isset($_POST['cleandone']) ? false : true;
    foreach ($upcontext['packages'] as $id => $package) {
        // Can't do anything about this....
        if ($package['missing_file']) {
            continue;
        }
        // Not testing *and* this wasn't checked?
        if (!$test && (!isset($_POST['remove']) || !isset($_POST['remove'][$id]))) {
            continue;
        }
        // What are the themes this was installed into?
        $cur_theme_paths = array();
        foreach ($theme_paths as $tid => $data) {
            if ($tid != 1 && in_array($tid, $package['themes'])) {
                $cur_theme_paths[$tid] = $data;
            }
        }
        // Get the modifications data if applicable.
        $filename = $package['filename'];
        $packageInfo = getPackageInfo($filename);
        if (!is_array($packageInfo)) {
            continue;
        }
        $info = parsePackageInfo($packageInfo['xml'], $test, 'uninstall');
        // Also get the reinstall details...
        if (isset($_POST['remove'])) {
            $infoInstall = parsePackageInfo($packageInfo['xml'], true);
        }
        if (is_file($boarddir . '/Packages/' . $filename)) {
            read_tgz_file($boarddir . '/Packages/' . $filename, $boarddir . '/Packages/temp');
        } else {
            copytree($boarddir . '/Packages/' . $filename, $boarddir . '/Packages/temp');
        }
        // Work out how we uninstall...
        $files = array();
        foreach ($info as $change) {
            // Work out two things:
            // 1) Whether it's installed at the moment - and if so whether its fully installed, and:
            // 2) Whether it could be installed on the new version.
            if ($change['type'] == 'modification') {
                $contents = @file_get_contents($boarddir . '/Packages/temp/' . $upcontext['base_path'] . $change['filename']);
                if ($change['boardmod']) {
                    $results = parseBoardMod($contents, $test, $change['reverse'], $cur_theme_paths);
                } else {
                    $results = parseModification($contents, $test, $change['reverse'], $cur_theme_paths);
                }
                foreach ($results as $action) {
                    // Something we can remove? Probably means it existed!
                    if (($action['type'] == 'replace' || $action['type'] == 'append' || !empty($action['filename']) && $action['type'] == 'failure') && !in_array($action['filename'], $files)) {
                        $files[] = $action['filename'];
                    }
                    if ($action['type'] == 'failure') {
                        $upcontext['packages'][$id]['needs_removing'] = true;
                        $upcontext['packages'][$id]['status'] = 'Reinstall Required';
                        $upcontext['packages'][$id]['color'] = '#FD6435';
                    }
                }
            }
        }
        // Store this info for the template as appropriate.
        $upcontext['packages'][$id]['files'] = $files;
        $upcontext['packages'][$id]['file_count'] = count($files);
        // If we've done something save the changes!
        if (!$test) {
            package_flush_cache();
        }
        // Are we attempting to reinstall this thing?
        if (isset($_POST['remove']) && !$test && isset($infoInstall)) {
            // Need to extract again I'm afraid.
            if (is_file($boarddir . '/Packages/' . $filename)) {
                read_tgz_file($boarddir . '/Packages/' . $filename, $boarddir . '/Packages/temp');
            } else {
                copytree($boarddir . '/Packages/' . $filename, $boarddir . '/Packages/temp');
            }
            $errors = false;
            $upcontext['packages'][$id]['result'] = 'Removed';
            foreach ($infoInstall as $change) {
                if ($change['type'] == 'modification') {
                    $contents = @file_get_contents($boarddir . '/Packages/temp/' . $upcontext['base_path'] . $change['filename']);
                    if ($change['boardmod']) {
                        $results = parseBoardMod($contents, true, $change['reverse'], $cur_theme_paths);
                    } else {
                        $results = parseModification($contents, true, $change['reverse'], $cur_theme_paths);
                    }
                    // Are there any errors?
                    foreach ($results as $action) {
                        if ($action['type'] == 'failure') {
                            $errors = true;
                        }
                    }
                }
            }
            if (!$errors) {
                $reinstall_worked[] = $id;
                $upcontext['packages'][$id]['result'] = 'Reinstalled';
                $upcontext['packages'][$id]['color'] = 'green';
                foreach ($infoInstall as $change) {
                    if ($change['type'] == 'modification') {
                        $contents = @file_get_contents($boarddir . '/Packages/temp/' . $upcontext['base_path'] . $change['filename']);
                        if ($change['boardmod']) {
                            $results = parseBoardMod($contents, false, $change['reverse'], $cur_theme_paths);
                        } else {
                            $results = parseModification($contents, false, $change['reverse'], $cur_theme_paths);
                        }
                    }
                }
                // Save the changes.
                package_flush_cache();
            }
        }
    }
    // Put errors back on a sec.
    error_reporting($oldErrorReporting);
    // Check everything is writable.
    if ($test && !empty($upcontext['packages'])) {
        $writable_files = array();
        foreach ($upcontext['packages'] as $package) {
            if (!empty($package['files'])) {
                foreach ($package['files'] as $file) {
                    $writable_files[] = $file;
                }
            }
        }
        if (!empty($writable_files)) {
            $writable_files = array_unique($writable_files);
            $upcontext['writable_files'] = $writable_files;
            if (!makeFilesWritable($writable_files)) {
                return false;
            }
        }
    }
    if (file_exists($boarddir . '/Packages/temp')) {
        deltree($boarddir . '/Packages/temp');
    }
    // Removing/Reinstalling any packages?
    if (isset($_POST['remove'])) {
        $deletes = array();
        foreach ($_POST['remove'] as $id => $dummy) {
            if (!in_array((int) $id, $reinstall_worked)) {
                $deletes[] = (int) $id;
            }
        }
        if (!empty($deletes)) {
            upgrade_query('
				UPDATE ' . $db_prefix . 'log_packages
				SET install_state = 0
				WHERE id_install IN (' . implode(',', $deletes) . ')');
        }
        // Ensure we don't lose our changes!
        package_put_contents($boarddir . '/Packages/installed.list', time());
        $upcontext['sub_template'] = 'cleanup_done';
        return false;
    } else {
        $allgood = true;
        // Is there actually anything that needs our attention?
        foreach ($upcontext['packages'] as $package) {
            if ($package['color'] != 'green') {
                $allgood = false;
            }
        }
        if ($allgood) {
            return true;
        }
    }
    $_GET['substep'] = 0;
    return isset($_POST['cleandone']) ? true : false;
}
Ejemplo n.º 9
0
 /**
  * Class controller, calls the functions in required order
  *
  * @return boolean|mixed[]
  */
 public function read_zip_data()
 {
     // Make sure we have a zip file
     if ($this->check_valid_zip() === false) {
         return false;
     }
     // The the overall zip information for this archive
     $this->_read_endof_cdr();
     // Load the actual CDR as defined by offset in the ecdr record
     $this->_data_cdr = substr($this->data, $this->_zip_info['cdr_offset'], $this->_zip_info['cdr_size']);
     // Load the file list from the central directory record
     if ($this->_load_file_headers() === false) {
         return false;
     }
     // The file records in the CDR point to the files location in the archive
     $this->_process_files();
     // Looking for a single file and this is it
     if ($this->_found && $this->single_file) {
         return $this->_crc_check ? $this->_found : false;
     }
     // Wanted many files then we need to clean up
     if ($this->destination !== null && !$this->single_file) {
         package_flush_cache();
     }
     if ($this->single_file) {
         return false;
     } else {
         return $this->return;
     }
 }
Ejemplo n.º 10
0
 /**
  * Installs new themes, either from a gzip or copy of the default.
  *
  * What it does:
  * - Puts themes in $boardurl/themes.
  * - Assumes the gzip has a root directory in it. (ie default.)
  * - Requires admin_forum.
  * - Accessed with ?action=admin;area=theme;sa=install.
  *
  * @uses ManageThemes template
  */
 public function action_install()
 {
     global $boardurl, $txt, $context, $settings, $modSettings;
     checkSession('request');
     require_once SUBSDIR . '/Themes.subs.php';
     require_once SUBSDIR . '/Package.subs.php';
     loadTemplate('ManageThemes');
     // Passed an ID, then the install is complete, lets redirect and show them
     if (isset($_GET['theme_id'])) {
         $_GET['theme_id'] = (int) $_GET['theme_id'];
         $context['sub_template'] = 'installed';
         $context['page_title'] = $txt['theme_installed'];
         $context['installed_theme'] = array('id' => $_GET['theme_id'], 'name' => getThemeName($_GET['theme_id']));
         return;
     }
     // How are we going to install this theme, from a dir, zip, copy of default?
     if (!empty($_FILES['theme_gz']) && (!isset($_FILES['theme_gz']['error']) || $_FILES['theme_gz']['error'] != 4) || !empty($_REQUEST['theme_gz'])) {
         $method = 'upload';
     } elseif (isset($_REQUEST['theme_dir']) && rtrim(realpath($_REQUEST['theme_dir']), '/\\') != realpath(BOARDDIR . '/themes') && file_exists($_REQUEST['theme_dir'])) {
         $method = 'path';
     } else {
         $method = 'copy';
     }
     // Copy the default theme?
     if (!empty($_REQUEST['copy']) && $method == 'copy') {
         // Hopefully the themes directory is writable, or we might have a problem.
         if (!is_writable(BOARDDIR . '/themes')) {
             fatal_lang_error('theme_install_write_error', 'critical');
         }
         // Make the new directory, standard characters only
         $theme_dir = BOARDDIR . '/themes/' . preg_replace('~[^A-Za-z0-9_\\- ]~', '', $_REQUEST['copy']);
         umask(0);
         mkdir($theme_dir, 0777);
         // Get some more time if we can
         @set_time_limit(600);
         if (function_exists('apache_reset_timeout')) {
             @apache_reset_timeout();
         }
         // Create the subdirectories for css, javascript and font files.
         mkdir($theme_dir . '/css', 0777);
         mkdir($theme_dir . '/scripts', 0777);
         mkdir($theme_dir . '/webfonts', 0777);
         // Copy over the default non-theme files.
         $to_copy = array('/index.php', '/index.template.php', '/scripts/theme.js');
         foreach ($to_copy as $file) {
             copy($settings['default_theme_dir'] . $file, $theme_dir . $file);
             @chmod($theme_dir . $file, 0777);
         }
         // And now the entire css, images and webfonts directories!
         copytree($settings['default_theme_dir'] . '/css', $theme_dir . '/css');
         copytree($settings['default_theme_dir'] . '/images', $theme_dir . '/images');
         copytree($settings['default_theme_dir'] . '/webfonts', $theme_dir . '/webfonts');
         package_flush_cache();
         $theme_name = $_REQUEST['copy'];
         $images_url = $boardurl . '/themes/' . basename($theme_dir) . '/images';
         $theme_dir = realpath($theme_dir);
         // Lets get some data for the new theme (default theme (1), default settings (0)).
         $theme_values = loadThemeOptionsInto(1, 0, array(), array('theme_templates', 'theme_layers'));
         // Lets add a theme_info.xml to this theme.
         write_theme_info($_REQUEST['copy'], $modSettings['elkVersion'], $theme_dir, $theme_values);
     } elseif (isset($_REQUEST['theme_dir']) && $method == 'path') {
         if (!is_dir($_REQUEST['theme_dir']) || !file_exists($_REQUEST['theme_dir'] . '/theme_info.xml')) {
             fatal_lang_error('theme_install_error', false);
         }
         $theme_name = basename($_REQUEST['theme_dir']);
         $theme_dir = $_REQUEST['theme_dir'];
     } elseif ($method == 'upload') {
         // Hopefully the themes directory is writable, or we might have a problem.
         if (!is_writable(BOARDDIR . '/themes')) {
             fatal_lang_error('theme_install_write_error', 'critical');
         }
         // This happens when the admin session is gone and the user has to login again
         if (empty($_FILES['theme_gz']) && empty($_REQUEST['theme_gz'])) {
             redirectexit('action=admin;area=theme;sa=admin;' . $context['session_var'] . '=' . $context['session_id']);
         }
         // Set the default settings...
         $theme_name = strtok(basename(isset($_FILES['theme_gz']) ? $_FILES['theme_gz']['name'] : $_REQUEST['theme_gz']), '.');
         $theme_name = preg_replace(array('/\\s/', '/\\.[\\.]+/', '/[^\\w_\\.\\-]/'), array('_', '.', ''), $theme_name);
         $theme_dir = BOARDDIR . '/themes/' . $theme_name;
         if (isset($_FILES['theme_gz']) && is_uploaded_file($_FILES['theme_gz']['tmp_name']) && (ini_get('open_basedir') != '' || file_exists($_FILES['theme_gz']['tmp_name']))) {
             read_tgz_file($_FILES['theme_gz']['tmp_name'], BOARDDIR . '/themes/' . $theme_name, false, true);
         } elseif (isset($_REQUEST['theme_gz'])) {
             if (!isAuthorizedServer($_REQUEST['theme_gz'])) {
                 fatal_lang_error('not_valid_server');
             }
             read_tgz_file($_REQUEST['theme_gz'], BOARDDIR . '/themes/' . $theme_name, false, true);
         } else {
             redirectexit('action=admin;area=theme;sa=admin;' . $context['session_var'] . '=' . $context['session_id']);
         }
     } else {
         fatal_lang_error('theme_install_general', false);
     }
     // Something go wrong?
     if ($theme_dir != '' && basename($theme_dir) != 'themes') {
         // Defaults.
         $install_info = array('theme_url' => $boardurl . '/themes/' . basename($theme_dir), 'images_url' => isset($images_url) ? $images_url : $boardurl . '/themes/' . basename($theme_dir) . '/images', 'theme_dir' => $theme_dir, 'name' => $theme_name);
         $explicit_images = false;
         if (file_exists($theme_dir . '/theme_info.xml')) {
             $theme_info = file_get_contents($theme_dir . '/theme_info.xml');
             // Parse theme-info.xml into an Xml_Array.
             require_once SUBSDIR . '/XmlArray.class.php';
             $theme_info_xml = new Xml_Array($theme_info);
             // @todo Error message of some sort?
             if (!$theme_info_xml->exists('theme-info[0]')) {
                 return 'package_get_error_packageinfo_corrupt';
             }
             $theme_info_xml = $theme_info_xml->path('theme-info[0]');
             $theme_info_xml = $theme_info_xml->to_array();
             $xml_elements = array('name' => 'name', 'theme_layers' => 'layers', 'theme_templates' => 'templates', 'based_on' => 'based-on');
             foreach ($xml_elements as $var => $name) {
                 if (!empty($theme_info_xml[$name])) {
                     $install_info[$var] = $theme_info_xml[$name];
                 }
             }
             if (!empty($theme_info_xml['images'])) {
                 $install_info['images_url'] = $install_info['theme_url'] . '/' . $theme_info_xml['images'];
                 $explicit_images = true;
             }
             if (!empty($theme_info_xml['extra'])) {
                 $install_info += unserialize($theme_info_xml['extra']);
             }
         }
         if (isset($install_info['based_on'])) {
             if ($install_info['based_on'] == 'default') {
                 $install_info['theme_url'] = $settings['default_theme_url'];
                 $install_info['images_url'] = $settings['default_images_url'];
             } elseif ($install_info['based_on'] != '') {
                 $install_info['based_on'] = preg_replace('~[^A-Za-z0-9\\-_ ]~', '', $install_info['based_on']);
                 $temp = loadBasedOnTheme($install_info['based_on'], $explicit_images);
                 // @todo An error otherwise?
                 if (is_array($temp)) {
                     $install_info = $temp + $install_info;
                     if (empty($explicit_images) && !empty($install_info['base_theme_url'])) {
                         $install_info['theme_url'] = $install_info['base_theme_url'];
                     }
                 }
             }
             unset($install_info['based_on']);
         }
         // Find the newest id_theme.
         $id_theme = nextTheme();
         $inserts = array();
         foreach ($install_info as $var => $val) {
             $inserts[] = array($id_theme, $var, $val);
         }
         if (!empty($inserts)) {
             addTheme($inserts);
         }
         updateSettings(array('knownThemes' => strtr($modSettings['knownThemes'] . ',' . $id_theme, array(',,' => ','))));
     }
     redirectexit('action=admin;area=theme;sa=install;theme_id=' . $id_theme . ';' . $context['session_var'] . '=' . $context['session_id']);
 }
Ejemplo n.º 11
0
 /**
  * Install a smiley set.
  */
 public function action_install()
 {
     global $modSettings, $scripturl, $context, $txt, $user_info;
     isAllowedTo('manage_smileys');
     checkSession('request');
     // One of these two may be necessary
     loadLanguage('Errors');
     loadLanguage('Packages');
     require_once SUBSDIR . '/Smileys.subs.php';
     require_once SUBSDIR . '/Package.subs.php';
     // Installing unless proven otherwise
     $testing = false;
     $destination = '';
     $name = '';
     if (isset($_REQUEST['set_gz'])) {
         $base_name = strtr(basename($_REQUEST['set_gz']), ':/', '-_');
         $name = Util::htmlspecialchars(strtok(basename($_REQUEST['set_gz']), '.'));
         $name_pr = preg_replace(array('/\\s/', '/\\.[\\.]+/', '/[^\\w_\\.\\-]/'), array('_', '.', ''), $name);
         $context['filename'] = $base_name;
         // Check that the smiley is from simplemachines.org, for now... maybe add mirroring later.
         if (!isAuthorizedServer($_REQUEST['set_gz']) == 0) {
             fatal_lang_error('not_valid_server');
         }
         $destination = BOARDDIR . '/packages/' . $base_name;
         if (file_exists($destination)) {
             fatal_lang_error('package_upload_error_exists');
         }
         // Let's copy it to the packages directory
         file_put_contents($destination, fetch_web_data($_REQUEST['set_gz']));
         $testing = true;
     } elseif (isset($_REQUEST['package'])) {
         $base_name = basename($_REQUEST['package']);
         $name = Util::htmlspecialchars(strtok(basename($_REQUEST['package']), '.'));
         $name_pr = preg_replace(array('/\\s/', '/\\.[\\.]+/', '/[^\\w_\\.\\-]/'), array('_', '.', ''), $name);
         $context['filename'] = $base_name;
         $destination = BOARDDIR . '/packages/' . basename($_REQUEST['package']);
     }
     if (!file_exists($destination)) {
         fatal_lang_error('package_no_file', false);
     }
     // Make sure temp directory exists and is empty.
     if (file_exists(BOARDDIR . '/packages/temp')) {
         deltree(BOARDDIR . '/packages/temp', false);
     }
     if (!mktree(BOARDDIR . '/packages/temp', 0755)) {
         deltree(BOARDDIR . '/packages/temp', false);
         if (!mktree(BOARDDIR . '/packages/temp', 0777)) {
             deltree(BOARDDIR . '/packages/temp', false);
             // @todo not sure about url in destination_url
             create_chmod_control(array(BOARDDIR . '/packages/temp/delme.tmp'), array('destination_url' => $scripturl . '?action=admin;area=smileys;sa=install;set_gz=' . $_REQUEST['set_gz'], 'crash_on_error' => true));
             deltree(BOARDDIR . '/packages/temp', false);
             if (!mktree(BOARDDIR . '/packages/temp', 0777)) {
                 fatal_lang_error('package_cant_download', false);
             }
         }
     }
     $extracted = read_tgz_file($destination, BOARDDIR . '/packages/temp');
     // @todo needs to change the URL in the next line ;)
     if (!$extracted) {
         fatal_lang_error('packageget_unable', false, array('http://custom.elkarte.net/index.php?action=search;type=12;basic_search=' . $name));
     }
     if ($extracted && !file_exists(BOARDDIR . '/packages/temp/package-info.xml')) {
         foreach ($extracted as $file) {
             if (basename($file['filename']) == 'package-info.xml') {
                 $base_path = dirname($file['filename']) . '/';
                 break;
             }
         }
     }
     if (!isset($base_path)) {
         $base_path = '';
     }
     if (!file_exists(BOARDDIR . '/packages/temp/' . $base_path . 'package-info.xml')) {
         fatal_lang_error('package_get_error_missing_xml', false);
     }
     $smileyInfo = getPackageInfo($context['filename']);
     if (!is_array($smileyInfo)) {
         fatal_lang_error($smileyInfo);
     }
     // See if it is installed?
     if (isSmileySetInstalled($smileyInfo['id'])) {
         fata_lang_error('package_installed_warning1');
     }
     // Everything is fine, now it's time to do something, first we test
     $actions = parsePackageInfo($smileyInfo['xml'], true, 'install');
     $context['post_url'] = $scripturl . '?action=admin;area=smileys;sa=install;package=' . $base_name;
     $context['has_failure'] = false;
     $context['actions'] = array();
     $context['ftp_needed'] = false;
     foreach ($actions as $action) {
         if ($action['type'] == 'readme' || $action['type'] == 'license') {
             $type = 'package_' . $action['type'];
             if (file_exists(BOARDDIR . '/packages/temp/' . $base_path . $action['filename'])) {
                 $context[$type] = htmlspecialchars(trim(file_get_contents(BOARDDIR . '/packages/temp/' . $base_path . $action['filename']), "\n\r"), ENT_COMPAT, 'UTF-8');
             } elseif (file_exists($action['filename'])) {
                 $context[$type] = htmlspecialchars(trim(file_get_contents($action['filename']), "\n\r"), ENT_COMPAT, 'UTF-8');
             }
             if (!empty($action['parse_bbc'])) {
                 require_once SUBSDIR . '/Post.subs.php';
                 preparsecode($context[$type]);
                 $context[$type] = parse_bbc($context[$type]);
             } else {
                 $context[$type] = nl2br($context[$type]);
             }
             continue;
         } elseif ($action['type'] == 'require-dir') {
             // Do this one...
             $thisAction = array('type' => $txt['package_extract'] . ' ' . ($action['type'] == 'require-dir' ? $txt['package_tree'] : $txt['package_file']), 'action' => Util::htmlspecialchars(strtr($action['destination'], array(BOARDDIR => '.'))));
             $file = BOARDDIR . '/packages/temp/' . $base_path . $action['filename'];
             if (isset($action['filename']) && (!file_exists($file) || !is_writable(dirname($action['destination'])))) {
                 $context['has_failure'] = true;
                 $thisAction += array('description' => $txt['package_action_error'], 'failed' => true);
             }
             // Show a description for the action if one is provided
             if (empty($thisAction['description'])) {
                 $thisAction['description'] = isset($action['description']) ? $action['description'] : '';
             }
             $context['actions'][] = $thisAction;
         } elseif ($action['type'] == 'credits') {
             // Time to build the billboard
             $credits_tag = array('url' => $action['url'], 'license' => $action['license'], 'copyright' => $action['copyright'], 'title' => $action['title']);
         }
     }
     if ($testing) {
         $context['sub_template'] = 'view_package';
         $context['uninstalling'] = false;
         $context['is_installed'] = false;
         $context['package_name'] = $smileyInfo['name'];
         loadTemplate('Packages');
     } else {
         $actions = parsePackageInfo($smileyInfo['xml'], false, 'install');
         foreach ($context['actions'] as $action) {
             updateSettings(array('smiley_sets_known' => $modSettings['smiley_sets_known'] . ',' . basename($action['action']), 'smiley_sets_names' => $modSettings['smiley_sets_names'] . "\n" . $smileyInfo['name'] . (count($context['actions']) > 1 ? ' ' . (!empty($action['description']) ? Util::htmlspecialchars($action['description']) : basename($action['action'])) : '')));
         }
         package_flush_cache();
         // Time to tell pacman we have a new package installed!
         package_put_contents(BOARDDIR . '/packages/installed.list', time());
         // Credits tag?
         $credits_tag = empty($credits_tag) ? '' : serialize($credits_tag);
         $installed = array('filename' => $smileyInfo['filename'], 'name' => $smileyInfo['name'], 'package_id' => $smileyInfo['id'], 'version' => $smileyInfo['filename'], 'id_member' => $user_info['id'], 'member_name' => $user_info['name'], 'credits_tag' => $credits_tag);
         logPackageInstall($installed);
         logAction('install_package', array('package' => Util::htmlspecialchars($smileyInfo['name']), 'version' => Util::htmlspecialchars($smileyInfo['version'])), 'admin');
         cache_put_data('parsing_smileys', null, 480);
         cache_put_data('posting_smileys', null, 480);
     }
     if (file_exists(BOARDDIR . '/packages/temp')) {
         deltree(BOARDDIR . '/packages/temp');
     }
     if (!$testing) {
         redirectexit('action=admin;area=smileys');
     }
 }
Ejemplo n.º 12
0
 /**
  * Class controller, calls the ungzip / untar functions in required order
  *
  * @return boolean|mixed[]
  */
 public function read_tgz_data()
 {
     // Snif test that this is a .tgz tar.gz file
     if (empty($this->_header) && $this->check_valid_tgz() == false) {
         return false;
     }
     // The tgz information for this archive
     if ($this->_read_header_tgz() === false) {
         return false;
     }
     // With the offset found, read and deflate the archive data
     if ($this->_ungzip_data() === false) {
         return false;
     }
     // With the archive data in hand, we need to un tarball it
     $this->_process_files();
     // Looking for a single file and this is it
     if ($this->_found && $this->single_file) {
         return $this->_crc_check ? $this->_found : false;
     }
     // Wanted many files then we need to clean up
     if ($this->destination !== null && !$this->single_file) {
         package_flush_cache();
     }
     if ($this->single_file) {
         return false;
     } else {
         return $this->return;
     }
 }
Ejemplo n.º 13
0
function ThemeInstall()
{
    global $sourcedir, $boarddir, $boardurl, $db_prefix, $txt, $context, $settings, $modSettings;
    checkSession('request');
    isAllowedTo('admin_forum');
    checkSession('request');
    require_once $sourcedir . '/Subs-Package.php';
    loadTemplate('Themes');
    if (isset($_GET['theme_id'])) {
        adminIndex('manage_themes');
        $result = db_query("\n\t\t\tSELECT value\n\t\t\tFROM {$db_prefix}themes\n\t\t\tWHERE ID_THEME = " . (int) $_GET['theme_id'] . "\n\t\t\t\tAND ID_MEMBER = 0\n\t\t\t\tAND variable = 'name'\n\t\t\tLIMIT 1", __FILE__, __LINE__);
        list($theme_name) = mysql_fetch_row($result);
        mysql_free_result($result);
        $context['sub_template'] = 'installed';
        $context['page_title'] = $txt['theme_installed'];
        $context['installed_theme'] = array('id' => (int) $_GET['theme_id'], 'name' => $theme_name);
        return;
    }
    if (!empty($_FILES['theme_gz']) && (!isset($_FILES['theme_gz']['error']) || $_FILES['theme_gz']['error'] != 4) || !empty($_REQUEST['theme_gz'])) {
        $method = 'upload';
    } elseif (isset($_REQUEST['theme_dir']) && realpath(stripslashes($_REQUEST['theme_dir'])) != realpath($boarddir . '/Themes') && file_exists(stripslashes($_REQUEST['theme_dir']))) {
        $method = 'path';
    } else {
        $method = 'copy';
    }
    if (!empty($_REQUEST['copy']) && $method == 'copy') {
        // Hopefully the themes directory is writable, or we might have a problem.
        if (!is_writable($boarddir . '/Themes')) {
            fatal_lang_error('theme_install_write_error');
        }
        $theme_dir = $boarddir . '/Themes/' . preg_replace('~[^A-Za-z0-9_\\- ]~', '', $_REQUEST['copy']);
        umask(0);
        mkdir($theme_dir, 0777);
        // Copy over the default non-theme files.
        $to_copy = array('/style.css', '/index.php', '/index.template.php');
        foreach ($to_copy as $file) {
            copy($settings['default_theme_dir'] . $file, $theme_dir . $file);
            @chmod($theme_dir . $file, 0777);
        }
        // And now the entire images directory!
        copytree($settings['default_theme_dir'] . '/images', $theme_dir . '/images');
        package_flush_cache();
        $theme_name = $_REQUEST['copy'];
        $images_url = $boardurl . '/Themes/' . basename($theme_dir) . '/images';
        $theme_dir = realpath($theme_dir);
    } elseif (isset($_REQUEST['theme_dir']) && $method == 'path') {
        if (!is_dir(stripslashes($_REQUEST['theme_dir'])) || !file_exists(stripslashes($_REQUEST['theme_dir']) . '/theme_info.xml')) {
            fatal_lang_error('theme_install_error', false);
        }
        $theme_name = basename($_REQUEST['theme_dir']);
        $theme_dir = stripslashes($_REQUEST['theme_dir']);
    } elseif ($method = 'upload') {
        // Hopefully the themes directory is writable, or we might have a problem.
        if (!is_writable($boarddir . '/Themes')) {
            fatal_lang_error('theme_install_write_error');
        }
        require_once $sourcedir . '/Subs-Package.php';
        // Set the default settings...
        $theme_name = strtok(basename(isset($_FILES['theme_gz']) ? $_FILES['theme_gz']['name'] : $_REQUEST['theme_gz']), '.');
        $theme_name = preg_replace(array('/\\s/', '/\\.[\\.]+/', '/[^\\w_\\.\\-]/'), array('_', '.', ''), $theme_name);
        $theme_dir = $boarddir . '/Themes/' . $theme_name;
        if (isset($_FILES['theme_gz']) && is_uploaded_file($_FILES['theme_gz']['tmp_name']) && (@ini_get('open_basedir') != '' || file_exists($_FILES['theme_gz']['tmp_name']))) {
            $extracted = read_tgz_file($_FILES['theme_gz']['tmp_name'], $boarddir . '/Themes/' . $theme_name, false, true);
        } elseif (isset($_REQUEST['theme_gz'])) {
            // Check that the theme is from simplemachines.org, for now... maybe add mirroring later.
            if (preg_match('~^http://[\\w_\\-]+\\.simplemachines\\.org/~', $_REQUEST['theme_gz']) == 0 || strpos($_REQUEST['theme_gz'], 'dlattach') !== false) {
                fatal_lang_error('not_on_simplemachines');
            }
            $extracted = read_tgz_file($_REQUEST['theme_gz'], $boarddir . '/Themes/' . $theme_name, false, true);
        } else {
            redirectexit('action=theme;sa=admin;sesc=' . $context['session_id']);
        }
    }
    // Something go wrong?
    if ($theme_dir != '' && basename($theme_dir) != 'Themes') {
        // Defaults.
        $install_info = array('theme_url' => $boardurl . '/Themes/' . basename($theme_dir), 'images_url' => isset($images_url) ? $images_url : $boardurl . '/Themes/' . basename($theme_dir) . '/images', 'theme_dir' => $theme_dir, 'name' => $theme_name);
        if (file_exists($theme_dir . '/theme_info.xml')) {
            $theme_info = file_get_contents($theme_dir . '/theme_info.xml');
            $xml_elements = array('name' => 'name', 'theme_layers' => 'layers', 'theme_templates' => 'templates', 'based_on' => 'based-on');
            foreach ($xml_elements as $var => $name) {
                if (preg_match('~<' . $name . '>(?:<!\\[CDATA\\[)?(.+?)(?:\\]\\]>)?</' . $name . '>~', $theme_info, $match) == 1) {
                    $install_info[$var] = $match[1];
                }
            }
            if (preg_match('~<images>(?:<!\\[CDATA\\[)?(.+?)(?:\\]\\]>)?</images>~', $theme_info, $match) == 1) {
                $install_info['images_url'] = $install_info['theme_url'] . '/' . $match[1];
                $explicit_images = true;
            }
            if (preg_match('~<extra>(?:<!\\[CDATA\\[)?(.+?)(?:\\]\\]>)?</extra>~', $theme_info, $match) == 1) {
                $install_info += unserialize($match[1]);
            }
        }
        if (isset($install_info['based_on'])) {
            if ($install_info['based_on'] == 'default') {
                $install_info['theme_url'] = $settings['default_theme_url'];
                $install_info['images_url'] = $settings['default_images_url'];
            } elseif ($install_info['based_on'] != '') {
                $install_info['based_on'] = preg_replace('~[^A-Za-z0-9\\-_ ]~', '', $install_info['based_on']);
                $request = db_query("\n\t\t\t\t\tSELECT th.value AS base_theme_dir, th2.value AS base_theme_url" . (!empty($explicit_images) ? '' : ", th3.value AS images_url") . "\n\t\t\t\t\tFROM ({$db_prefix}themes AS th, {$db_prefix}themes AS th2" . (!empty($explicit_images) ? '' : ", {$db_prefix}themes AS th3") . ")\n\t\t\t\t\tWHERE th.ID_MEMBER = 0\n\t\t\t\t\t\tAND (th.value LIKE '%/{$install_info['based_on']}' OR th.value LIKE '%\\{$install_info['based_on']}')\n\t\t\t\t\t\tAND th.variable = 'theme_dir'\n\t\t\t\t\t\tAND th.ID_THEME = th2.ID_THEME\n\t\t\t\t\t\tAND th2.ID_MEMBER = 0\n\t\t\t\t\t\tAND th2.variable = 'theme_url'" . (!empty($explicit_images) ? '' : "\n\t\t\t\t\t\tAND th.ID_THEME = th3.ID_THEME\n\t\t\t\t\t\tAND th3.ID_MEMBER = 0\n\t\t\t\t\t\tAND th3.variable = 'images_url'") . "\n\t\t\t\t\tLIMIT 1", __FILE__, __LINE__);
                $temp = mysql_fetch_assoc($request);
                mysql_free_result($request);
                // !!! An error otherwise?
                if (is_array($temp)) {
                    $install_info = $temp + $install_info;
                    if (empty($explicit_images) && !empty($install_info['base_theme_url'])) {
                        $install_info['theme_url'] = $install_info['base_theme_url'];
                    }
                }
            }
            unset($install_info['based_on']);
        }
        // Find the newest ID_THEME.
        $result = db_query("\n\t\t\tSELECT MAX(ID_THEME)\n\t\t\tFROM {$db_prefix}themes", __FILE__, __LINE__);
        list($ID_THEME) = mysql_fetch_row($result);
        mysql_free_result($result);
        // This will be theme number...
        $ID_THEME++;
        $setString = '';
        foreach ($install_info as $var => $val) {
            $setString .= "\n\t\t\t\t({$ID_THEME}, SUBSTRING('" . addslashes($var) . "', 1, 255), SUBSTRING('" . addslashes($val) . "', 1, 65534)),";
        }
        $setString = substr($setString, 0, -1);
        db_query("\n\t\t\tINSERT INTO {$db_prefix}themes\n\t\t\t\t(ID_THEME, variable, value)\n\t\t\tVALUES{$setString}", __FILE__, __LINE__);
        updateSettings(array('knownThemes' => strtr($modSettings['knownThemes'] . ',' . $ID_THEME, array(',,' => ','))));
    }
    redirectexit('action=theme;sa=install;theme_id=' . $ID_THEME . ';sesc=' . $context['session_id']);
}