<?php

if (!wptouch_can_cloud_install(true)) {
    ?>
	<div class="cloud-update-issue"><i class="icon-cloud"></i> <?php 
    echo sprintf(__('Your server configuration is preventing WPtouch Pro from installing and updating from the Cloud. %sPlease visit %sthis article%s to follow the steps to enable Cloud install, or you can manually download and install into the wptouch-data/%s directory.', 'wptouch-pro'), '<br />', '<a href="http://www.wptouch.com/support/knowledgebase/themes-or-extensions-cannot-be-downloaded/">', '</a>', 'extensions');
    ?>
</div>
<?php 
}
?>

<?php 
$cloud_themes = wptouch_get_available_cloud_themes();
foreach ($cloud_themes as $theme => $details) {
    $cloud_themes[$theme] = $theme;
}
?>

<ul class="cloud-browser">
	<?php 
while (wptouch_has_themes(true)) {
    ?>
		<?php 
    wptouch_the_theme();
    ?>
			<?php 
    if (!wptouch_is_theme_in_cloud()) {
        ?>
				<?php 
        include 'theme-browser-item.php';
 function get_available_themes($include_cloud = false)
 {
     $themes = array();
     $cloud_themes = array();
     $theme_directories = $this->get_theme_directories();
     $custom = false;
     foreach ($theme_directories as $theme_dir) {
         $list_dir = @opendir($theme_dir[0]);
         if ($list_dir) {
             while (($f = readdir($list_dir)) !== false) {
                 // Skip common files in each directory
                 if ($this->should_skip_file($f)) {
                     continue;
                 }
                 $theme_info = $this->get_theme_information($theme_dir[0] . '/' . $f, $theme_dir[1] . '/' . $f, $custom);
                 if ($theme_info) {
                     $themes[$theme_info->name] = $theme_info;
                 }
             }
             closedir($list_dir);
         }
         if (!$custom) {
             $custom = true;
         }
     }
     if ($include_cloud) {
         if (false === ($cloud_themes = get_transient('_wptouch_available_cloud_themes'))) {
             $cloud_themes = wptouch_get_available_cloud_themes();
             set_transient('_wptouch_available_cloud_themes', $cloud_themes, WPTOUCH_THEME_ADDON_TRANSIENT_TIME);
         }
         $to_add = array();
         if (is_array($cloud_themes) && count($cloud_themes)) {
             foreach ($cloud_themes as $cloud_theme) {
                 if (!isset($themes[$cloud_theme['name']])) {
                     $this_theme = new stdClass();
                     $this_theme->name = $cloud_theme['name'];
                     $this_theme->description = $cloud_theme['description'];
                     $this_theme->author = $cloud_theme['author'];
                     $this_theme->version = $cloud_theme['version'];
                     $this_theme->screenshot = $cloud_theme['screenshot'];
                     $this_theme->base = $cloud_theme['base'];
                     $this_theme->location = 'cloud';
                     $this_theme->custom_theme = true;
                     if (isset($cloud_theme['theme_type'])) {
                         $this_theme->theme_type = $cloud_theme['theme_type'];
                     }
                     if (isset($cloud_theme['info_url'])) {
                         $this_theme->info_url = $cloud_theme['info_url'];
                     }
                     if (isset($cloud_theme['upgrade_url'])) {
                         $this_theme->download_url = $cloud_theme['upgrade_url'];
                     }
                     if (isset($cloud_theme['buy_url'])) {
                         $this_theme->buy_url = $cloud_theme['buy_url'];
                     }
                     $to_add[$this_theme->name] = $this_theme;
                 } else {
                     $this_theme = $themes[$cloud_theme['name']];
                     $this_theme->cloud_version = $cloud_theme['version'];
                     if (isset($cloud_theme['upgrade_url'])) {
                         $this_theme->upgrade_available = $cloud_theme['version'] != $this_theme->version;
                         $this_theme->download_url = $cloud_theme['upgrade_url'];
                     }
                     $themes[$cloud_theme['name']] = $this_theme;
                 }
             }
             $themes = array_merge($themes, $to_add);
         }
     }
     ksort($themes);
     return apply_filters('wptouch_available_themes', $themes);
 }
 function get_available_themes($include_cloud = false)
 {
     $themes = array();
     $cloud_themes = array();
     $theme_directories = $this->get_theme_directories();
     foreach ($theme_directories as $theme_dir) {
         $list_dir = @opendir($theme_dir[0]);
         if ($list_dir) {
             while (($f = readdir($list_dir)) !== false) {
                 // Skip common files in each directory
                 if ($this->should_skip_file($f)) {
                     continue;
                 }
                 $theme_info = $this->get_theme_information($theme_dir[0] . DIRECTORY_SEPARATOR . $f, $theme_dir[1] . DIRECTORY_SEPARATOR . $f);
                 if ($theme_info) {
                     $themes[$theme_info->name] = $theme_info;
                 }
             }
             closedir($list_dir);
         }
     }
     if ($include_cloud) {
         if (defined('WPTOUCH_FORCE_CLOUD_REFRESH')) {
             delete_site_transient('_wptouch_available_cloud_themes');
         }
         if (false === ($cloud_themes = get_site_transient('_wptouch_available_cloud_themes'))) {
             $cloud_themes = wptouch_get_available_cloud_themes();
             set_site_transient('_wptouch_available_cloud_themes', $cloud_themes, WPTOUCH_THEME_ADDON_TRANSIENT_TIME);
         }
         $to_add = array();
         if (is_array($cloud_themes) && count($cloud_themes)) {
             foreach ($cloud_themes as $cloud_theme) {
                 if (!isset($themes[$cloud_theme['name']])) {
                     $this_theme = new stdClass();
                     $this_theme->name = $cloud_theme['name'];
                     $this_theme->description = $cloud_theme['description'];
                     $this_theme->author = $cloud_theme['author'];
                     $this_theme->version = $cloud_theme['version'];
                     $this_theme->screenshot = $cloud_theme['screenshot'];
                     $this_theme->base = $cloud_theme['base'];
                     $this_theme->location = 'cloud';
                     if (isset($cloud_theme['preview_images'])) {
                         $this_theme->preview_images = unserialize($cloud_theme['preview_images']);
                     }
                     $this_theme->changelog = '';
                     $this_theme->long_description = '';
                     if (isset($cloud_theme['long_description'])) {
                         $this_theme->long_description = $cloud_theme['long_description'];
                     }
                     if (isset($cloud_theme['theme_type'])) {
                         $this_theme->theme_type = $cloud_theme['theme_type'];
                     }
                     if (isset($cloud_theme['info_url'])) {
                         $this_theme->info_url = $cloud_theme['info_url'];
                     }
                     if (isset($cloud_theme['upgrade_url'])) {
                         $this_theme->download_url = $cloud_theme['upgrade_url'];
                     }
                     if (isset($cloud_theme['buy_url'])) {
                         $this_theme->buy_url = $cloud_theme['buy_url'];
                     }
                     $to_add[$this_theme->name] = $this_theme;
                 } else {
                     $this_theme = $themes[$cloud_theme['name']];
                     $this_theme->cloud_version = $cloud_theme['version'];
                     if (isset($cloud_theme['upgrade_url'])) {
                         $this_theme->theme_upgrade_available = version_compare($cloud_theme['version'], $this_theme->version, '>');
                         $this_theme->download_url = $cloud_theme['upgrade_url'];
                     }
                     if ($this_theme->long_description == '' && isset($cloud_theme['long_description'])) {
                         $this_theme->long_description = $cloud_theme['long_description'];
                     }
                     if (isset($cloud_theme['changelog'])) {
                         $this_theme->changelog = $cloud_theme['changelog'];
                     } else {
                         $this_theme->changelog = '';
                     }
                     if (isset($cloud_theme['preview_images'])) {
                         $this_theme->preview_images = unserialize($cloud_theme['preview_images']);
                     }
                     if (isset($this_theme->theme_upgrade_available) && $this_theme->theme_upgrade_available) {
                         $this->theme_upgrades_available = true;
                     }
                     $themes[$cloud_theme['name']] = $this_theme;
                 }
             }
             $themes = array_merge($themes, $to_add);
         }
     }
     // Move current theme to the front of the list and alphabetize the rest
     $settings = $this->get_settings();
     // Check to make sure the active theme exists - it may not, in which case we will autodownload it later
     if (isset($themes[$settings->current_theme_friendly_name])) {
         $current_theme = $themes[$settings->current_theme_friendly_name];
         unset($themes[$settings->current_theme_friendly_name]);
         ksort($themes);
         $first_theme = array($settings->current_theme_friendly_name => $current_theme);
         $themes = array_merge($first_theme, $themes);
     } else {
         ksort($themes);
     }
     return apply_filters('wptouch_available_themes', $themes);
 }