settings = array(); $this->icon_packs = $this->get_icon_packs(); // For Wordpress 2.0 compability, we have to delete our // cached data item in order to allow updates from the // our configuration page and version upgrades. // -Garett if (function_exists('wp_cache_delete')) { wp_cache_delete('WIsettings', 'options'); wp_cache_delete('WIversion', 'options'); } $this->CheckForUpgrade(); $this->UpdateSettings(); } function CheckForUpgrade() { global $wpdb; // Let's get the version number from the database. // If it is pre 3.0.0 or a new install, it will be // automagically set to 0. // -Garett $WIversionDB = $this->version_num(get_option('WIversion')); // Check to see if the version # is less than or equal to 2.3.1 // (our last production version), upgrade the tables (if they // don't exist) and upgrade settings. // -Garett if ($WIversionDB <= 20301) { $q = <<query($q); $q = <<query($q); // Upgrade pre-3.0.0 settings (and options) if (get_settings('weather_settings') || get_settings('weather_options')) { $oldsettings = get_settings('weather_settings'); $oldoptions = get_settings('weather_options'); $newsettings = array(); foreach ($oldsettings as $index => $val) { switch ($index) { case 'station': $newsettings['locations']['default']['station'] = $val; $newsettings['locations']['default']['latitude'] = '0.0000'; $newsettings['locations']['default']['longitude'] = '0.0000'; $newsettings['locations']['default']['gmt_offset'] = get_option('gmt_offset'); $newsettings['default_location'] = 'default'; break; // Upgrade ICON LOCATION to ICON PACK. case 'icon_location': // Since there is no way to determine the // matching icon pack, set to default $newsettings['icon_pack'] = 'default'; break; // Determine ICON EXTENSION case 'icon_ext': // By default, go with default icon set of icon pack (which is 0) $icon_ext = 0; // Get icon pack data and scan for matching extension $icon_pack_data = $this->icon_packs['default']; foreach ($icon_pack_data['Formats'] as $format_idx => $format) { // If found, use index value if (stristr($val, $format)) $icon_ext = $format_idx; } // Set new index info $newsettings['icon_ext'] = $icon_ext; break; // No longer needed, no file cache support in 3.0.0 case 'cache_type': break; // No longer needed, no file cache support in 3.0.0 case 'cache_path': break; // All measurement configuration is stored under 'units' now. case 'temp': $newsettings['units']['temp'] = $val; break; case 'wind_units': $newsettings['units']['wind'] = $val; break; case 'language': $newsettings['language'] = $val; $newsettings['locations']['default']['language'] = $val; case 'visibility_units': $newsettings['units']['visibility'] = $val; // Use visibilty settings to determine the // best setting for pressure and // cloud height // // I assume the imperial system uses inHg for pressure // since the last plugin used inHg by default and had // hPa in brackets. if ($val == 'km') { $newsettings['units']['clouds'] = 'm'; $newsettings['units']['pressure'] = 'hPa'; } else { $newsettings['units']['clouds'] = 'ft'; $newsettings['units']['pressure'] = 'inHg'; } break; // If not one of these, it hasn't changed ... so save value default: $newsettings[$index] = $val; } } foreach ($oldoptions as $val) { switch ($val) { case 'display_heatindex': $newsettings['display_options'][] = 'display_humidex'; break; // We are no longer getting sunrise/sunset from WeatherUnderground // However, we added an option to control the condition // notifications so we'll add that here case 'fetch_suns': $newsettings['display_options'][] = 'display_conditions'; break; // We've renamed metartime case 'display_metartime': $newsettings['display_options'][] = 'display_updatetime'; break; default: $newsettings['display_options'][] = $val; } } $newsettings['display_options'][] = 'show_current_weather_at_post'; $newsettings['default_time'] = get_settings('time_format'); // save new settings in new names update_option('WIsettings', $newsettings); // delete old settings delete_option('weather_settings'); delete_option('weather_options'); // Wordpress 2.0 compatibility stuff ... // I hate the cache ... if (function_exists('wp_cache_delete')) { wp_cache_delete('weather_settings','options'); wp_cache_delete('weather_options','options'); } } else { // If we can't find old settings, assume new install and reset to defaults update_option('WIsettings', array( 'locations' => array( 'Regina SK CA' => array ( 'station' => 'CYQR', 'latitude' => '50.4333', 'longitude' => '-104.4667', 'gmt_offset' => get_option('gmt_offset'), 'language' => 'english' )), 'default_location' => 'Regina SK CA', 'language' => 'english', 'icon_pack' => 'default', 'icon_ext' => 0, 'units' => array ( 'temp' => 'C', 'wind' => 'km/h', 'visibility' => 'km', 'clouds' => 'm', 'pressure' => 'hPa' ), 'usetime' => 'currenttime', 'sunrise_hour' => '6', 'sunset_hour' => '19', 'cache_timeout' => 1800, 'default_date' => get_settings('date_format'), 'default_time' => get_settings('time_format'), 'display_options' => array ( 'display_icon', 'display_temp', 'display_humidity', 'display_wind', 'display_dewpt', 'display_visibility', 'display_clouds', 'display_conditions', 'display_barometer', 'display_windchill', 'display_humidex' ), )); } } if ($WIversionDB < 29915) { $oldsettings = get_settings('WIsettings'); // Okay, I changed the settings file to use 'units' for all our // units definitions. This code will be removed before production // since it will serve no logical purpose then. This _may_ not // work when I first commit it, but it likley will in the next // revision. if (!in_array('units', $oldsettings)) { // Let's first convert existing Units $oldsettings['units']['temp'] = $oldsettings['temp']; $oldsettings['units']['wind'] = $oldsettings['wind_units']; $oldsettings['units']['visibility'] = $oldsettings['visibility_units']; // Then use the existing Visibility Units to determine what // system we use (metric vs. imperial) and determine the most likely // default setting for cloud height and pressure. I'm assuming you // Yanks use inHg ... tell me if this is not the case ;) if ($oldsettings['visibility_units'] == 'km') { $oldsettings['units']['clouds'] = 'm'; $oldsettings['units']['pressure'] = 'hPa'; } else { $oldsettings['units']['clouds'] = 'ft'; $oldsettings['units']['pressure'] = 'inHg'; } // Okay, also look for display_metartime ... // If they have display_metartime, add renamed one. if (in_array('display_metartime', $oldsettings['display_options'])) { $oldsettings['display_options'][] = 'display_updatetime'; } // We should remove the old variables, but they are only wasting some space // and will be wiped out the second you save settings on the configuration // screen so there really isn't a point to doing it. // // In any case, save it update_option('WIsettings', $oldsettings); } // set WIversionDB to current (to avoid apply other patches below) $WIversionDB = WIversion; } if ($WIversionDB < 29921) { $oldsettings = get_settings('WIsettings'); // Add default settings for new items $oldsettings['latitude'] = '50.433'; $oldsettings['longitude'] = '-104.466'; $oldsettings['gmt_offset'] = get_option('gmt_offset'); update_option('WIsettings', $oldsettings); } if ($WIversionDB < 29924) { // Changed the Cache Directory format $q = <<query($q); $q = <<query($q); } if ($WIversionDB < 29928) { // Added option $oldsettings = get_settings('WIsettings'); // Add default settings for new item $oldsettings['display_options'][] = 'show_current_weather_at_post'; update_option('WIsettings', $oldsettings); } if ($WIversionDB < 29938) { $oldsettings = get_settings('WIsettings'); // Okay, also look for display_metartime ... // If they have display_metartime, add renamed one. if (in_array('display_metartime', $oldsettings['display_options']) || in_array('display_metardate', $oldsettings['display_options'])) { $oldsettings['display_options'][] = 'display_updatetime'; } if (!empty($oldsettings['time_zone'])) { $oldsettings['gmt_offset'] = $oldsettings['time_zone']; } // We should remove the old variables, but they are only wasting some space // and will be wiped out the second you save settings on the configuration // screen so there really isn't a point to doing it. // // In any case, save it update_option('WIsettings', $oldsettings); } if ($WIversionDB < 29941) { $oldsettings = get_settings('WIsettings'); $oldsettings['locations']['default']['station'] = $oldsettings['station']; $oldsettings['locations']['default']['latitude'] = $oldsettings['latitude']; $oldsettings['locations']['default']['longitude'] = $oldsettings['longitude']; $oldsettings['locations']['default']['gmt_offset'] = $oldsettings['gmt_offset']; $oldsettings['default_location'] = 'default'; update_option('WIsettings', $oldsettings); } if ($WIversionDB < 29942) { $oldsettings = get_settings('WIsettings'); foreach ($oldsettings['locations'] as $location) { $location['language'] = $oldsettings['language']; } update_option('WIsettings', $oldsettings); } // Save current version update_option('WIversion', WIversion); } function CheckForNewVersion() { $host = WIRemoteHost; $location = WIRemoteVersion; $request = "GET $location HTTP/1.1\r\n" . "If-Modified-Since: Sat, 29 Oct 1994 09:00:00 GMT\r\n" . "Pragma: no-cache\r\n". "Cache-Control: no-cache\r\n". "Host: $host\r\n" . "Content-Type: text/html\r\n" . "Connection: Close\r\n\r\n"; @$fp = fsockopen($host, 80, $errno, $errstr, 5); if ($fp) { fputs($fp, $request); $httpresponse = fgets($fp, 1024); if (strpos($httpresponse, '200 ')) { # Skip Response Header do { $line = fgets($fp, 1024); } while ($line != "\r\n"); $server_version = fgets($fp, 1024); fclose($fp); // We (hope) we have the latest version // return it return intval($server_version); } } // On failure, assume no upgrade return $this->version_num(WIversion); } function UpdateSettings() { $this->settings = get_settings('WIsettings'); } function ClearCache() { global $wpdb; $q = <<query($q); } function get_i18n_list() { $languages = array(); $languages[] = "english"; $handler = opendir( WIi18ndir ); while ($file = readdir($handler)) { if (strpos($file, '.mo')) { $languages[] = substr($file, 0, strlen($file) - 3); } } closedir($handler); return $languages; } function get_icon_pack_data($WIicon_pack_def) { $WIicon_pack_data = implode('', file(WIicondir . $WIicon_pack_def . "/.icon.ipk")); preg_match("|Icon Pack Name:(.*)|i", $WIicon_pack_data, $icon_pack_name); preg_match("|Icon Pack URI:(.*)|i", $WIicon_pack_data, $icon_pack_uri); preg_match("|Description:(.*)|i", $WIicon_pack_data, $description); preg_match("|Designer:(.*)|i", $WIicon_pack_data, $designer_name); preg_match("|Designer URI:(.*)|i", $WIicon_pack_data, $designer_uri); preg_match("|Formats:(.*)|i", $WIicon_pack_data, $formats); $description = wptexturize(trim($description[1])); $name = trim($icon_pack_name[1]); $icon_pack = $name; if ('' != $icon_pack_uri[1] && '' != $name) { $icon_pack = '' . $icon_pack . ''; } if ('' == $designer_uri[1]) { $designer = trim($designer_name[1]); } else { $designer = '' . trim($designer_name[1]) . ''; } $formats = explode(' ', trim($formats[1])); return array('Name' => $WIicon_pack_def, 'Title' => $icon_pack, 'Description' => $description, 'Designer' => $designer, 'Formats' => $formats); } function get_icon_packs() { $WIicon_packs = array(); $WIicon_pack_defs = array(); $WIicon_dir = @ dir(WIicondir); if ($WIicon_dir) { while(($file = $WIicon_dir->read()) !== false) { if (is_dir(WIicondir . $file)) { // This line is redundant and was causing a bug // -Garett // $WIicon_subdir = @ dir(WIicondir . '/' . $file); // Add ---if it is a subdirectory-- and not ./ or ../ // and if it has an icon definition file // -Garett //if ($WIicon_subdir && !preg_match('/^\.\.?$/', $file) && is_file(WIicondir . $file . '/.icon.ipk')) // We already know that it is a subdirectory // (see is_dir line). // Why repeat the work and it seems to be causing errors. if (!preg_match('/^\.\.?$/', $file) && is_file(WIicondir . $file . '/.icon.ipk')) { $WIicon_pack_defs[] = "$file"; } } } } if (!$WIicon_pack_defs) return $WIicon_pack_defs; sort($WIicon_pack_defs); foreach($WIicon_pack_defs as $WIicon_pack_def) { $WIicon_pack_data = $this->get_icon_pack_data( $WIicon_pack_def ); if (empty($WIicon_pack_data['Title'])) continue; $WIicon_packs[$WIicon_pack_def] = $WIicon_pack_data; } return $WIicon_packs; } function setting_checked($index, $value) { if (strpos($index, '|')) { $values = preg_split('/\|/', $index); $index = $this->settings; foreach ($values as $val) { $index = $index[$val]; } } else { $index = $this->settings[$index]; } if ($index == $value) return 'checked="checked"'; } function setting_selected ($index, $value) { if (strpos($index, '|')) { $values = preg_split('/\|/', $index); $index = $this->settings; foreach ($values as $val) { $index = $index[$val]; } } else { $index = $this->settings[$index]; } if ($index == $value) return 'selected="selected"'; } function unit_selected ($index, $value) { if ($this->settings['units'][$index] == $value) return 'selected="selected"'; } function option_checked ($index) { if (in_array($index, $this->settings['display_options'])) return 'checked="checked"'; } function version_num ($ver) { list($major, $minor, $patch) = split('\.', $ver, 3); return intval(($major * 10000) + ($minor * 100) + $patch); } function get_metar($station, $latitude, $longitude, $gmt_offset, $before, $after) { return new WeatherIconMetar(true, $station, $latitude, $longitude, $gmt_offset, $before, $after); } function bool ($value) { switch (strtoupper($value)) { case "FALSE": return FALSE; break; case "0": return FALSE; break; case "TRUE": return TRUE; break; case "1": return TRUE; break; default: return FALSE; } } } ?>