Here's a good reason to never put passwords in source code. Apparently Yahoo's php server was down today. On the other hand, some commenting would be better than what is shown here. Next post: The ethics of posting other peoples code.
include 'Yahoo/Frontpage/Weather.php';
define('WEATHER_COOKIE', 'FPWL');
define('COOKIE_DOMAIN', 'www.yahoo.com');
define('PIXEL_WIDTH', '145');
$linkviews = array();
function track_linkview ($link) {
global $linkviews;
array_push($linkviews, $link);
}
function log_linkviews () {
global $linkviews;
if (count($linkviews)) {
yahoo_track_page_params(array('link' => join("\002", $linkviews)));
}
}
function quote ($str) {
return preg_replace('/([\\\"\'])/', '\\\$1', $str);
}
function js_start () {
print "<script language=\"javascript\" type=\"text/javascript\">\n";
print "d=document;\n";
print "d.domain='yahoo.com';\n";
print "p=parent.document;\n";
print "lc=p.getElementById('lc');\n"; // weather content
print "ca=p.getElementById('ca');\n"; // change location link
print "cl=p.getElementById('cl');\n"; // change location
print "wt=p.getElementById('wt');\n"; // module title
print "wa=p.getElementById('wa');\n"; // alert
print "wg=p.getElementById('wg');\n"; // weather message
}
function js_end () {
print "</script>\n";
}
function js_set ($key, $value) {
return "$key=\"" . quote($value) . "\";\n";
}
function weather_result ($label, $url, $cond) {
$rd = $label == 'Today' ? 'r/w3' : 'r/w5';
$lo = $cond["lo"];
$hi = $cond["hi"];
$sky = $cond["sky"];
$fc = $cond["forecast"];
$result = "<a href=\"$rd/*$url\"><b>$label</b></a>";
$result .= "<img src=$sky alt=\"$fc\">";
$result .= "$fc<br>";
$result .= "hi <span class=hi>$hi°<span class=sc>F</span></span> - ";
$result .= "lo <span class=lo>$lo°<span class=sc>F</span></span>";
track_linkview($rd);
return $result;
}
function show_results ($city_state, $weather) {
$url = $weather->url;
$today = weather_result("Today", $url, $weather->today);
$tomorrow = weather_result("Tomorrow", $url, $weather->tomorrow);
$xfc =
"<span class=mr>" .
"<a href=\"r/w7/*$url\"><b>»</b> Extended Weather Forecast</a>" .
"</span>";
print js_set("wt.innerHTML",
htmlentities(fp_limit_location($city_state, PIXEL_WIDTH)));
print js_set("wt.href", "r/wt/*$url");
print js_set("ca.style.display", "inline");
print js_set("ca.className", "");
print js_set("lc.innerHTML",
"<div class=b>$today</div>" .
"<div class=b>$tomorrow</div>" .
$xfc);
//print "wt.focus();wt.blur();parent.clo=0;\n";
print "parent.clo=0;\n";
// U.S. location for local search, disabled
if (0 && preg_match('/\b\w{2}$/', $city_state)) {
print js_set("parent._wcs", $city_state);
print "if (p.sf1.csz) " . js_set("p.sf1.csz.value", $city_state);
}
yahoo_track_page_params(array("wrs" => $weather->id));
track_linkview("r/w7");
}
function show_error ($status) {
$code = "error";
if (!is_null($status)) {
$code .= ":$status";
}
print js_set("wa.innerHTML",
"<strong>Your search produced no matches.</strong><br>".
"Please re-enter a City or U.S. Zip Code:");
print js_set("wg.style.display", "none");
print js_set("wa.style.display", "block");
yahoo_track_page_params(array("wrs" => $code));
}
function show_picklist ($list) {
$ccc_file = "/home/y/share/htdocs/includes/weather/ccc2full.cdb";
$ccc_map = dba_open($ccc_file, "r-", "cdb");
$choices = "";
## re-order USA first
if (array_key_exists('USA', $list)) {
$newlist = array();
$newlist['USA'] = $list['USA'];
foreach ($list as $ccc => $region) {
$newlist[$ccc] = $region;
}
$list = $newlist;
}
foreach ($list as $ccc => $region) {
$country = $ccc;
if ($ccc_map !== FALSE &&
($full = dba_fetch($ccc, $ccc_map)) !== FALSE) {
$country = $full;
}
$choices .= "<div>$country</div>";
foreach ($region as $loc) {
list($id, $cs) = $loc;
$param = quote($id);
$choices .= "<a href=\"javascript:parent.zp('$param');\">$cs</a>";
}
}
if ($ccc_map !== FALSE) {
dba_close($ccc_map);
}
print js_set("wa.innerHTML",
"<span>" .
"<strong>Your search matched multiple cities.</strong><br>" .
"Please select the appropriate location below:" .
"</span>" .
"<span id=ls class=wil>" .
$choices .
"</span>");
print js_set("wg.style.display", "block");
print js_set("wa.style.display", "block");
yahoo_track_page_params(array("wrs" => "picklist"));
}
function retry_validate_location ($lm, $csz, $limit) {
$attempt = 0;
do {
if ($attempt) usleep(500000); // 0.5 seconds
$attempt++;
$return = $lm->validatelocation($csz, 1);
} while ($return[0] == '-5' and $attempt < $limit);
if ($return[0] != 1 and $return[0] != -1 and $return[0] != -2) {
error_log("validatelocation($csz) failed: $return[0]");
}
return $return;
}
function find_location ($lm, $wtr, $csz) {
$attempts = 3;
$return = retry_validate_location($lm, $csz, $attempts);
$results = array();
if (($return[0] == -2 or $return[0] >= 0) &&
array_key_exists('geolist', $return)) {
foreach ($return['geolist'] as $location) {
if ($wtr->load($location)) {
array_push($results, $location);
}
else {
error_log("no weather data for: " . fp_city_state($location));
}
}
}
return $results;
}
function set_weather_cookie ($value) {
$expire = time() + 90 * 86400;
setcookie(WEATHER_COOKIE, $value, $expire, "/", COOKIE_DOMAIN, 0);
}
function clear_weather_cookie () {
setcookie(WEATHER_COOKIE, "", 0, "/", COOKIE_DOMAIN, 0);
}
## main ()
$v = $_GET;
$location = new yahoo_location_manager();
$weather = new YahooFrontpageWeather(array('pixel_width' => PIXEL_WIDTH));
## /yahoo/site/main/weather (non-frontpage traffic)
yadl_spaceid('P#97167465');
yahoo_track_page_params(array('PL' =>
yahoo_full_cookie('PL') === FALSE ? 0 : 1));
if (array_key_exists('test', $v)) {
$version = 'v' . yahoo_get_data(YIV_GET, 'test');
yahoo_track_page_params(array('test' => $version));
}
if (array_key_exists('wl', $v)) {
$pixel = 'http://us.i1.yimg.com/us.yimg.com/i/space.gif';
$wlocid = yahoo_get_data(YIV_GET, 'wl');
if ($weather->load($wlocid)) {
set_weather_cookie($wlocid);
}
header("Location: $pixel");
}
else if (array_key_exists('p', $v) and
(($csz =
yahoo_get_data(YIV_GET, 'p', YIV_FILTER_UNSAFE_RAW)) != '')) {
$results = find_location($location, $weather, $csz);
## invalid location
if (count($results) == 0) {
if ($csz == 'delete') {
// clear weather cookie
if (yahoo_full_cookie(WEATHER_COOKIE) !== FALSE) {
clear_weather_cookie();
}
// clear location manager cookie
if (yahoo_full_cookie('PL') != FALSE) {
setcookie('PL', '', 0, '/', ".yahoo.com");
}
}
js_start();
show_error('l');
js_end();
}
## single result
else if (count($results) == 1) {
$loc = $results[0];
$cs = fp_city_state($loc);
$id = $loc['uniqueId'];
if ($weather->load($loc)) {
if (array_key_exists('sv', $v) && $v['sv'] == 'on') {
// clear weather cookie
if (yahoo_full_cookie(WEATHER_COOKIE) !== FALSE) {
clear_weather_cookie();
}
// clear PL older cookie scoped on www.yahoo.com
if (yahoo_cookie_part('PL', 'V') == '1.0') {
setcookie('PL', '', 0, '/', COOKIE_DOMAIN);
}
$return = $location->setLocation($id, "weather", 1);
if ($return[0] != 1) {
error_log("setLocation($id) failed: $return[0]");
}
yahoo_track_page_params(array('wsv' => 1));
}
if ($cs == '') {
$cs = $weather->location;
}
js_start();
show_results($cs, $weather);
js_end();
}
else {
js_start();
show_error('w');
js_end();
}
}
## pick list
else {
$list = array();
// group by country
foreach ($results as $loc) {
$cs = fp_city_state($loc);
$id = $loc['uniqueId'];
$ccc = $loc['country'];
$item = array($id, $cs);
if (array_key_exists($ccc, $list)) {
array_push($list[$ccc], $item);
}
else {
$list[$ccc] = array($item);
}
}
js_start();
if (count($list) == 0) {
show_error('pl');
}
else {
show_picklist($list);
}
js_end();
}
}
## initial pageview
else {
$loc = fp_get_weather_location();
if ($loc !== FALSE) {
$cs = fp_city_state($loc);
if ($weather->load($loc)) {
if ($cs == '') {
$cs = $weather->location;
}
js_start();
show_results($cs, $weather);
js_end();
}
}
}
log_linkviews();
?>