function wptouch_setup_base_content_dir()
{
    global $blog_id;
    global $wptouch_pro;
    $wptouch_upload_dir = wp_upload_dir();
    // Where we want the base content directory to be
    $desirable_dir = '/wptouch-data';
    if (is_multisite()) {
        define('WPTOUCH_BASE_CONTENT_MS_DIR', WP_CONTENT_DIR . $desirable_dir);
        define('WPTOUCH_BASE_CONTENT_MS_URL', wptouch_check_url_ssl(WP_CONTENT_URL) . $desirable_dir);
        if ($blog_id) {
            wptouch_create_directory_if_not_exist(WPTOUCH_BASE_CONTENT_MS_DIR);
            $desirable_dir = $desirable_dir . '/' . $blog_id;
        }
    }
    $undesirable_dir = $wptouch_upload_dir['basedir'] . '/wptouch-data';
    if (file_exists($undesirable_dir)) {
        // Need to migrate here
        //define( 'WPTOUCH_BASE_CONTENT_DIR', $wptouch_upload_dir[ 'basedir' ] . '/wptouch-data' );
        //define( 'WPTOUCH_BASE_CONTENT_URL', wptouch_check_url_ssl( $wptouch_upload_dir[ 'baseurl' ] . '/wptouch-data' ) );
        wptouch_create_directory_if_not_exist(WP_CONTENT_DIR . $desirable_dir);
        $migration_paths = array('themes', 'icons', 'lang', 'uploads', 'add-ons', 'backups');
        foreach ($migration_paths as $path) {
            if (file_exists($undesirable_dir . '/' . $path)) {
                if (!file_exists(WP_CONTENT_DIR . $desirable_dir . '/' . $path)) {
                    // This is a fresh migration, so let's just move it
                    if (!rename($undesirable_dir . '/' . $path, WP_CONTENT_DIR . $desirable_dir . '/' . $path)) {
                        define('WPTOUCH_MIGRATION_OLD_ISSUE', 1);
                    }
                }
            }
        }
    }
    define('WPTOUCH_BASE_CONTENT_DIR', WP_CONTENT_DIR . $desirable_dir);
    define('WPTOUCH_BASE_CONTENT_URL', wptouch_check_url_ssl(WP_CONTENT_URL) . $desirable_dir);
}
function wptouch_pro_copy_theme($theme_name, $theme_location)
{
    global $wptouch_pro;
    require_once WPTOUCH_DIR . '/core/file-operations.php';
    $theme_location = WP_CONTENT_DIR . $theme_location;
    $theme_name = wptouch_convert_to_class_name($theme_name);
    $num = $wptouch_pro->get_theme_copy_num($theme_name);
    $copy_dest = WPTOUCH_CUSTOM_THEME_DIRECTORY . '/' . $theme_name . '-copy-' . $num;
    wptouch_create_directory_if_not_exist($copy_dest);
    $wptouch_pro->recursive_copy($theme_location, $copy_dest);
    $readme_file = $copy_dest . '/readme.txt';
    $readme_info = $wptouch_pro->load_file($readme_file);
    if ($readme_info) {
        if (preg_match('#Theme Name: (.*)#', $readme_info, $matches)) {
            $new_name = $matches[1] . ' Copy #' . $num;
            $readme_info = str_replace($matches[0], 'Theme Name: ' . $new_name, $readme_info);
            $f = fopen($readme_file, "w+t");
            if ($f) {
                fwrite($f, $readme_info);
                fclose($f);
            }
        }
        return array('name' => $new_name, 'location' => $copy_dest);
    } else {
        WPTOUCH_DEBUG(WPTOUCH_ERROR, "Unable to modify readme.txt file after copy");
        return false;
    }
}
 function check_directories()
 {
     require_once WPTOUCH_DIR . '/core/file-operations.php';
     $creation_failure = false;
     $directories_to_create = array(WPTOUCH_BASE_CONTENT_DIR, WPTOUCH_TEMP_DIRECTORY, WPTOUCH_TEMP_DIRECTORY, WPTOUCH_BASE_CONTENT_DIR . '/cache', WPTOUCH_BASE_CONTENT_DIR . '/themes', WPTOUCH_BASE_CONTENT_DIR . '/modules', WPTOUCH_BASE_CONTENT_DIR . '/extensions', WPTOUCH_CUSTOM_SET_DIRECTORY, WPTOUCH_CUSTOM_ICON_DIRECTORY, WPTOUCH_CUSTOM_LANG_DIRECTORY, WPTOUCH_CUSTOM_UPLOAD_DIRECTORY, WPTOUCH_BACKUP_DIRECTORY, WPTOUCH_DEBUG_DIRECTORY);
     // Add extra for multisite
     if (is_multisite()) {
         $multisite_dirs = array(WPTOUCH_BASE_CONTENT_MS_DIR, WPTOUCH_BASE_CONTENT_MS_DIR . '/themes', WPTOUCH_BASE_CONTENT_MS_DIR . '/extensions', WPTOUCH_BASE_CONTENT_MS_DIR . '/lang');
         $directories_to_create = array_merge($multisite_dirs, $directories_to_create);
     }
     $directories_to_check = apply_filters('wptouch_create_directories', $directories_to_create);
     // Loop through all directories
     foreach ($directories_to_check as $dir_name) {
         $creation_failure = $creation_failure | !wptouch_create_directory_if_not_exist($dir_name);
     }
     if ($creation_failure) {
         WPTOUCH_DEBUG(WPTOUCH_WARNING, 'Unable to create one or more directories');
         $this->add_notification(__('Directory Problem', 'wptouch-pro'), __('One or more required directories could not be created', 'wptouch-pro'));
     }
 }
function wptouch_pro_handle_admin_command()
{
    global $wptouch_pro;
    if (isset($wptouch_pro->get['admin_command'])) {
        $admin_menu_nonce = $wptouch_pro->get['admin_menu_nonce'];
        if (wptouch_admin_menu_nonce_is_valid($admin_menu_nonce)) {
            // check user permissions
            if (current_user_can('switch_themes')) {
                switch ($wptouch_pro->get['admin_command']) {
                    case 'activate_theme':
                        WPTOUCH_DEBUG(WPTOUCH_INFO, 'Activating theme [' . $wptouch_pro->get['theme_name'] . ']');
                        $theme_to_activate = $wptouch_pro->get['theme_name'];
                        if ($theme_to_activate) {
                            $settings = $wptouch_pro->get_settings();
                            $paths = explode('/', ltrim(rtrim($wptouch_pro->get['theme_location'], '/'), '/'));
                            $settings->current_theme_name = $paths[count($paths) - 1];
                            unset($paths[count($paths) - 1]);
                            $settings->current_theme_location = '/' . implode('/', $paths);
                            $settings->current_theme_friendly_name = $wptouch_pro->get['theme_name'];
                            $settings->save();
                        }
                        break;
                    case 'activate_addon':
                        WPTOUCH_DEBUG(WPTOUCH_INFO, 'Activating add-on [' . $wptouch_pro->get['addon_name'] . ']');
                        $addon_to_activate = $wptouch_pro->get['addon_name'];
                        if ($addon_to_activate) {
                            $settings = $wptouch_pro->get_settings();
                            if (!isset($settings->active_addons[$addon_to_activate])) {
                                $paths = explode('/', ltrim(rtrim($wptouch_pro->get['addon_location'], DIRECTORY_SEPARATOR), DIRECTORY_SEPARATOR));
                                $addon_info = new stdClass();
                                $addon_info->addon_name = $paths[count($paths) - 1];
                                unset($paths[count($paths) - 1]);
                                $addon_info->location = '/' . implode('/', $paths);
                                $settings->active_addons[$addon_to_activate] = $addon_info;
                                $settings->save();
                            }
                        }
                        break;
                    case 'deactivate_addon':
                        WPTOUCH_DEBUG(WPTOUCH_INFO, 'Deactivating add-on [' . $wptouch_pro->get['addon_name'] . ']');
                        $addon_to_deactivate = $wptouch_pro->get['addon_name'];
                        if ($addon_to_deactivate) {
                            $settings = $wptouch_pro->get_settings();
                            if (isset($settings->active_addons[$addon_to_deactivate])) {
                                unset($settings->active_addons[$addon_to_deactivate]);
                                $settings->save();
                            }
                        }
                        break;
                    case 'copy_theme':
                        WPTOUCH_DEBUG(WPTOUCH_INFO, 'Copying theme [' . $wptouch_pro->get['theme_name'] . ']');
                        require_once WPTOUCH_DIR . '/core/file-operations.php';
                        $copy_src = WP_CONTENT_DIR . $wptouch_pro->get['theme_location'];
                        $theme_name = wptouch_convert_to_class_name($wptouch_pro->get['theme_name']);
                        $num = $wptouch_pro->get_theme_copy_num($theme_name);
                        $copy_dest = WPTOUCH_CUSTOM_THEME_DIRECTORY . '/' . $theme_name . '-copy-' . $num;
                        wptouch_create_directory_if_not_exist($copy_dest);
                        $wptouch_pro->recursive_copy($copy_src, $copy_dest);
                        $readme_file = $copy_dest . '/readme.txt';
                        $readme_info = $wptouch_pro->load_file($readme_file);
                        if ($readme_info) {
                            if (preg_match('#Theme Name: (.*)#', $readme_info, $matches)) {
                                $readme_info = str_replace($matches[0], 'Theme Name: ' . $matches[1] . ' Copy #' . $num, $readme_info);
                                $f = fopen($readme_file, "w+t");
                                if ($f) {
                                    fwrite($f, $readme_info);
                                    fclose($f);
                                }
                            }
                        } else {
                            WPTOUCH_DEBUG(WPTOUCH_ERROR, "Unable to modify readme.txt file after copy");
                        }
                        break;
                    case 'delete_theme':
                        WPTOUCH_DEBUG(WPTOUCH_INFO, 'Deleting theme [' . $wptouch_pro->get['theme_location'] . ']');
                        require_once WPTOUCH_DIR . '/core/file-operations.php';
                        $theme_location = WP_CONTENT_DIR . $wptouch_pro->get['theme_location'];
                        $wptouch_pro->recursive_delete($theme_location);
                        break;
                }
            }
        }
        $used_query_args = array('admin_menu_nonce', 'admin_command', 'theme_name', 'theme_location');
        header('Location: ' . remove_query_arg($used_query_args));
        die;
    }
}