Hello all,
I'm wondering if someone could help me with the following script...It's coming up with a couple errors at the moment.
The errors returned include:
Notice: Undefined index: sce in C:\Sites\Single25\litohin\webroot\chart00.php on line 85
Notice: Undefined variable: PHP_SELF in C:\Sites\Single25\litohin\webroot\chart00.php on line 100
Notice: Undefined variable: doPrint in C:\Sites\Single25\litohin\webroot\chart00.php on line 105
Notice: Undefined index: lang in C:\Sites\Single25\litohin\webroot\chart00.php on line 108
Notice: Undefined variable: doLang in C:\Sites\Single25\litohin\webroot\chart00.php on line 109
Notice: Undefined variable: DefaultLang in C:\Sites\Single25\litohin\webroot\chart00.php on line 110
The code is as follows:
<?
$ECURL = 'http://www.weatheroffice.ec.gc.ca/forecast/city_e.html?on-107&unit=m&b_templatePrint=true';
$defaultLang = 'en'; // set to 'fr' for french default language
$printIt = true; // set to false if you want to manually print on your page
$imagedir = "ec-icons/";
$cacheName = 'ec-forecast.txt';
$refetchSeconds = 600; // get new forecast from EC
if ( strtolower($_REQUEST['sce']) == 'view' ) {
$filenameReal = $_SERVER["PATH_TRANSLATED"];
$download_size = filesize($filenameReal);
header('Pragma: public');
header('Cache-Control: private');
header('Cache-Control: no-cache, must-revalidate');
header("Content-type: text/plain");
header("Accept-Ranges: bytes");
header("Content-Length: $download_size");
header('Connection: close');
readfile($filenameReal);
exit;
}
$t = pathinfo($PHP_SELF); // get our program name for the HTML comments
$Program = $t['basename'];
$Status = "<!-- $Program - $Version -->\n";
if(defined($doPrint)) { $printIt = $doPrint; }
$Lang = strtolower($_REQUEST['lang']);
if ($doLang) {$Lang = $doLang;};
if (! $Lang) {$Lang = $DefaultLang;};
if ($Lang == 'fr') {
$LMode = 'f';
$ECNAME = "Environnement Canada";
$ECHEAD = 'Prévisions sur 5 jours';
} else {
$Lang = 'en';
$LMode = 'e';
$ECNAME = "Environment Canada";
$ECHEAD = '5-day Forecast';
}
// support both french and english caches
$ECURL = preg_replace('|city_.\.html|',"city_$LMode.html",$ECURL);
$cacheName = preg_replace('|.txt$|',"-$Lang.txt",$cacheName);
if (file_exists($cacheName) and filemtime($cacheName) + $refetchSeconds > time()) {
$Status .= "<!-- using Cached version from $cacheName -->\n";
$site = implode('', file($cacheName));
} else {
$Status .= "<!-- loading $cacheName from\n '$ECURL' -->\n";
$site = fetchUrlWithoutHang($ECURL);
$fp = fopen($cacheName, "w");
if ($fp) {
$write = fputs($fp, $site);
fclose($fp);
$Status .= "<!-- loading finished. New cache saved to $cacheName -->\n";
} else {
$Status .= "<!-- unable to open $cacheName for writing .. cache not saved -->\n";
$Status .= "<!-- loading finished -->\n";
}
}
$site = str_replace(" ", "", $site);
$site = str_replace(" ", "", $site);
$site = str_replace("\n", "", $site);
$site = str_replace("\r", "", $site);
$site = str_replace("<li class=\"pop", "<font color=\"green", $site);
$site = str_replace("<li class=\"high", "<font color=\"red", $site);
$site = str_replace("<li class=\"low", "<font color=\"blue", $site);
$site = str_replace("</li>", "</font>", $site);
$site = str_replace("</font><font", "</font><Br><font", $site);
$startgrab = strpos($site , 'id="cityf">');
$start = strpos($site , '<div id="f1">', $startgrab);
$finish = strpos($site , '</div></div>', $start) + 8;
$length = $finish-$start;
$code=Substr($site , $start, $length);
preg_match_all("'<h3>(.*?)/p></div>'", $code, $matches);
$matches = $matches[1];
if ($matches){
foreach ($matches as $b => $r){
preg_match_all('/(.*?)<\/h3>.*?\/weathericons\/(.*?)".*?<ul>(.*?)<\/ul><p>(.*?)</', $matches[$b], $matches[$b]);
}
$wdth = intval(100/count($matches));
$forecasticons = array();
$forecasttemp = array();
$forecastpop = array();
$forecasttitles = array();
$forecast = array();
// slice up one forecast icon set
$i = 0;
$striptags = '!<font.*>|</font>|<br>!Uis';
foreach ($matches as $b => $r) { // loop over icon parts
$forecasttitles[$i] = ucwords($matches[$b][1][0]);
$forecasticon[$i] = $matches[$b][2][0];
$t = $matches[$b][3][0]; // temperature and maybe pop
preg_match_all('|<font color=\"(.*)\">(.*)</font>|Uis',$t,$tmatch);
$colors = $tmatch[1];
$values = $tmatch[2];
$forecasttemp[$i] = '';
$forecastpop[$i] = '';
foreach ($colors as $c => $v) {
$tc = $colors[$c];
$tv = $values[$c];
if (preg_match('/blue|red/i',$tc) ) {
$forecasttemp[$i] .= "<span style=\"color: $tc;\">$tv</span>\n";
} else {
$forecastpop[$i] .= "$tv\n";
}
}
$forecasttemp[$i] = preg_replace("|\n|U","<br />",trim($forecasttemp[$i]));
$forecastpop[$i] = preg_replace('|[^\d]+|i','',trim($forecastpop[$i]));
$forecasticon[$i] = replace_icon($forecasticon[$i],$forecastpop[$i]);
$forecasttext[$i] = ucwords($matches[$b][4][0]);
$i++;
}
$weather = "<table width=\"100%\" class=\"ECtable\">\n";
$weather .= "<tr>\n";
foreach ($forecasticon as $i => $v) {
$weather .= " <td align=\"center\" valign=\"top\" >" .
$forecasttitles[$i] . "<br />\n";
$alttext = $forecasttext[$i];
if ($forecastpop[$i] <> '') {
$alttext .= " - (" . $forecastpop[$i] ."%)";
}
$forecasticon[$i] = " <img src=\"$imagedir" . $forecasticon[$i] .
"\"\n" .
" height=\"51\" width=\"60\" \n" .
" alt=\"$alttext\"\n" .
" title=\"$alttext\" /> <br />\n";
$weather .= "<br />" . $forecasticon[$i] . "\n";
$weather .= " " . $forecasttemp[$i] . "<br />\n";
$weather .= " " . $forecasttext[$i] . "\n";
$weather .= " </td>\n";
$forecast[$i] = $forecasttitles[$i] . "<br />\n" .
$forecasticon[$i] . "<br />\n" .
$forecasttemp[$i] . "<br />\n" .
$forecasttext[$i] . "\n";
}
$weather .= "</tr>\n</table>\n";
}
if ($Lang == 'en') {
$start = strpos($site , '<h4>Text Forecast', $startgrab);
} else {
$start = strpos($site , '<h4> Textes des pr', $startgrab);
}
$finish = strpos($site , '<div id="sltFooter">', $start) + 8;
$length = $finish-$start;
$code=Substr($site , $start, $length);
preg_match_all("'<dt>(.*?)</dt>'", $code, $matches);
$forecastdays = $matches[1];
preg_match_all("'<dd>(.*?)</dd>'", $code, $matches);
$forecastdetail = $matches[1];
preg_match_all("'<h3>(.*?)</h3>'",$code,$matches);
$title = $matches[1][0];
for ($g=0;$g < count($forecastdays);$g++) {
$forecastdays[$g] = preg_replace('|<a name=.*>|','',$forecastdays[$g]);
}
$textfcsthead = 'Current Forecast';
if ($Lang == 'fr') {$textfcsthead = 'Textes des prévisions'; }
$textforecast = "<div class=\"ECforecast\"><h2>$textfcsthead</h2>\n
<h6>$title</h6>\n<dl>\n";
for ($g=0;$g < count($forecastdays);$g++) {
$textforecast .= " <dt><b>" . $forecastdays[$g] ."</b></dt>\n";
$textforecast .= " <dd>" . $forecastdetail[$g] . "</dd>\n";
}
$textforecast .= "</dl></div>\n";
if ($printIt && ! $doInclude) {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title><?php print "$ECHEAD - $ECNAME"; ?></title>
</head>
<body>
<?php
}
if ($printIt) {
$ECURL = preg_replace('|&|Ui','&',$ECURL); // make link XHTML compatible
print $Status;
print $weather;
print $textforecast;
print "<p><a href=\"$ECURL\">$ECNAME</a></p>\n";
}
if ($printIt && ! $doInclude) {?>
</body>
</html>
<?php
}
function fetchUrlWithoutHang($url) {
$numberOfSeconds=4;
error_reporting(0);
$url = str_replace("http://","",$url);
$urlComponents = explode("/",$url);
$domain = $urlComponents[0];
$resourcePath = str_replace($domain,"",$url);
$socketConnection = fsockopen($domain, 80, $errno, $errstr, $numberOfSeconds);
if (!$socketConnection)
{
}
else {
$xml = '';
fputs($socketConnection, "GET $resourcePath HTTP/1.0\r\nHost: $domain\r\n\r\n");
while (!feof($socketConnection))
{
$xml .= fgets($socketConnection, 128);
}
fclose ($socketConnection);
}
return($xml);
}
// -------------------------------------------------------
function replace_icon($icon,$pop) {
global $imagedir;
$curicon = $icon;
if ($pop > 0) {
$testicon = preg_replace("|.gif|","p$pop.gif",$curicon);
if (file_exists("$imagedir/$testicon")) {
$newicon = $testicon;
} else {
$newicon = $curicon;
}
} else {
$newicon = $curicon;
}
return($newicon);
}
?>