forked from fuad-tareq/bluemix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather.php
948 lines (810 loc) · 34.6 KB
/
weather.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
<!---------------------------------------------------------------------------------------------
- The content of this page is part of the Weather Underground API and was partially modified -
- for the purpose of this assignment. -
--------------------------------------------------------------------------------------------->
<form style="float: right" action="main.php" method="POST">
<input type="hidden" name="idtoken" id="idtoken" value="a<?php echo $_POST['idtoken'] ?>" />
<input type="submit" value="Return to Previous Page" required />
</form>
<?php
$WUAPIkey = 'd8153d0e25a78780';
$iconDir ='';
$iconType = '.jpg';
$WU_URL = 'http://www.wunderground.com/q/zmw:00000.1.71697';
$maxWidth = '640px'; // max width of tables (could be '100%')
$maxIcons = 10; // max number of icons to display
$maxForecasts = 14; // max number of Text forecast periods to display
$maxForecastLegendWords = 4; // more words in forecast legend than this number will use our forecast words
$numIconsInFoldedRow = 5; // if words cause overflow of $maxWidth pixels, then put this num of icons in rows
$autoSetTemplate = true; // =true set icons based on wide/narrow template design
$cacheFileDir = './'; // default cache file directory
$cacheName = "WU-forecast-json.txt"; // locally cached page from WU
$refetchSeconds = 60; // cache lifetime (3600sec = 60 minutes)
$xlateCOP = 'Chance of precipitation'; // change to local language if needed like
//$xlateCOP = 'Kans op neerslag'; // Dutch example
$showTempsAs = 'C'; // under icons/forecast, 'C'=Centigrade, 'F'=Fahrenheit
$charsetOutput = 'ISO-8859-1'; // default character encoding of output
$lang = 'en'; // default language
$foldIconRow = true; // display icons in rows of 5 if long texts are found
$RTLlang = ',he,jp,cn,'; // languages that use right-to-left order
if (isset($_REQUEST['sce']) && strtolower($_REQUEST['sce']) == 'view' ) {
//--self downloader --
$filenameReal = __FILE__;
$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;
}
if(preg_match('|specify|i',$WUAPIkey)) {
print "<p>Note: the WU-forecast.php script requires an API key from WeatherUnderground to operate.<br/>";
print "Visit <a href=\"http://www.wunderground.com/weather/api/\">Weather Underground</a> to ";
print "register for an API key.</p>\n";
if( isset($SITE['fcsturlWU']) ) {
print "<p>Insert in Settings.php an entry for:<br/><br/>\n";
print "\$SITE['WUAPIkey'] = '<i>your-key-here</i>';<br/><br/>\n";
print "replacing <i>your-key-here</i> with your WU API key.</p>\n";
}
return;
}
$NWSiconlist = array(
// WU Icon name => NWS icon name // WU meaning
'chanceflurries.gif' => array('sn.jpg','Chance flurries'), // Chance of Flurries
'chancerain.gif' => array('hi_shwrs.jpg','Chance rain'), // Chance of Rain
'chancesleet.gif' => array('ip.jpg','Chance sleet'), // Chance of Sleet
'chancesnow.gif' => array('sn.jpg','Chance snow'), // Chance of Snow
'chancetstorms.gif' => array('hi_tsra.jpg','Chance thunderstorms'), // Chance of Thunderstorms
'clear.gif' => array('skc.jpg','Clear'), // Clear
'cloudy.gif' => array('ovc.jpg','Cloudy'), // Cloudy
'flurries.gif' => array('sn.jpg','Flurries'), // Flurries
'fog.gif' => array('fg.jpg','Fog'), // Fog
'hazy.gif' => array('fg.jpg','Hazy'), // Hazy
'mostlycloudy.gif' => array('bkn.jpg','Mostly cloudy'), // Mostly Cloudy
'mostlysunny.gif' => array('sct.jpg','Partly cloudy'), // Mostly Sunny
'partlycloudy.gif' => array('sct.jpg','Partly cloudy'), // Partly Cloudy
'partlysunny.gif' => array('bkn.jpg','Mostly sunny'), // Partly Sunny
'rain.gif' => array('ra.jpg','Rain'), // Rain
'sleet.gif' => array('ip.jpg','Sleet'), // Sleet
'sleat.gif' => array('ip.jpg','Sleet'), // Sleet (other spelling)
'snow.gif' => array('sn.jpg','Snow'), // Snow
'sunny.gif' => array('skc.jpg','Sunny'), // Sunny
'tstorms.gif' => array('tsra.jpg','Thunderstorms'), // Thunderstorms
'unknown.gif' => array('na.jpg',''), // Unknown
'nt_chanceflurries.gif' => array('nsn.jpg','Chance flurries'), // Chance of Flurries
'nt_chancerain.gif' => array('hi_nshwrs.jpg','Chance rain'), // Chance of Rain
'nt_chancesleet.gif' => array('ip.jpg','Chance sleet'), // Chance of Sleet
'nt_chancesnow.gif' => array('nsn.jpg','Chance snow'), // Chance of Snow
'nt_chancetstorms.gif' => array('hi_ntsra.jpg','Chance thunderstorms'), // Chance of Thunderstorms
'nt_clear.gif' => array('nskc.jpg','Clear'), // Clear
'nt_cloudy.gif' => array('novc.jpg','Cloudy'), // Cloudy
'nt_flurries.gif' => array('nsn.jpg','Flurries'), // Flurries
'nt_fog.gif' => array('nfg.jpg','Fog'), // Fog
'nt_hazy.gif' => array('nfg.jpg','Hazy'), // Hazy
'nt_mostlycloudy.gif' => array('nbkn.jpg','Mostly cloudy'), // Mostly Cloudy
'nt_mostlysunny.gif' => array('nsct.jpg','Partly cloudy'), // Mostly Sunny
'nt_partlycloudy.gif' => array('nsct.jpg','Partly cloudy'), // Partly Cloudy
'nt_partlysunny.gif' => array('nbkn.jpg','Mostly cloudy'), // Partly Sunny
'nt_rain.gif' => array('nra.jpg','Rain'), // Rain
'nt_sleet.gif' => array('ip.jpg','Sleet'), // Sleet
'nt_sleat.gif' => array('ip.jpg','Sleet'), // sleet (other spelling)
'nt_snow.gif' => array('nsn.jpg','Snow'), // Snow
'nt_sunny.gif' => array('nskc.jpg','Sunny'), // Sunny
'nt_tstorms.gif' => array('ntsra.jpg','Thunderstorms'), // Thunderstorms
'nt_unknown.gif' => array('na.jpg',''), // Unknown
'.gif' => array('na.jpg',''), // Unknown
) ;
if(!function_exists('langtransstr')) {
// shim function if not running in template set
function langtransstr($input) { return($input); }
}
if(!function_exists('json_last_error')) {
// shim function if not running PHP 5.3+
function json_last_error() { return('- N/A'); }
$Status .= "<!-- php V".phpversion()." json_last_error() stub defined -->\n";
if(!defined('JSON_ERROR_NONE')) { define('JSON_ERROR_NONE',0); }
if(!defined('JSON_ERROR_DEPTH')) { define('JSON_ERROR_DEPTH',1); }
if(!defined('JSON_ERROR_STATE_MISMATCH')) { define('JSON_ERROR_STATE_MISMATCH',2); }
if(!defined('JSON_ERROR_CTRL_CHAR')) { define('JSON_ERROR_CTRL_CHAR',3); }
if(!defined('JSON_ERROR_SYNTAX')) { define('JSON_ERROR_SYNTAX',4); }
if(!defined('JSON_ERROR_UTF8')) { define('JSON_ERROR_UTF8',5); }
}
WU_loadLangDefaults (); // set up the language defaults
$WULANG = 'EN'; // Default to English for API
$lang = strtolower($lang);
if( isset($WUlanguages[$lang]) ) { // if $lang is specified, use it
$SITE['lang'] = $lang;
$WULANG = $WUlanguages[$lang];
$charsetOutput = (isset($WUlangCharsets[$lang]))?$WUlangCharsets[$lang]:$charsetOutput;
}
if(isset($_GET['lang']) and isset($WUlanguages[strtolower($_GET['lang'])]) ) { // template override
$lang = strtolower($_GET['lang']);
$SITE['lang'] = $lang;
$WULANG = $WUlanguages[$lang];
$charsetOutput = (isset($WUlangCharsets[$lang]))?$WUlangCharsets[$lang]:$charsetOutput;
}
$doRTL = (strpos($RTLlang,$lang) !== false)?true:false; // format RTL language in Right-to-left in output
// get the selected forecast location code
$haveIndex = '0';
if (!empty($_GET['z']) && preg_match("/^[0-9]+$/i", htmlspecialchars($_GET['z']))) {
$haveIndex = htmlspecialchars(strip_tags($_GET['z'])); // valid zone syntax from input
}
if(!isset($WUforecasts[0])) {
// print "<!-- making NWSforecasts array default -->\n";
$WUforecasts = array("|$WU_URL"); // create default entry
}
// print "<!-- NWSforecasts\n".print_r($WUforecasts,true). " -->\n";
// Set the default zone. The first entry in the $SITE['NWSforecasts'] array.
list($Nl,$Nn) = explode('|',$WUforecasts[0].'|||');
$FCSTlocation = $Nl;
$WU_URL = $Nn;
if(!isset($WUforecasts[$haveIndex])) {
$haveIndex = 0;
}
// locations added to the drop down menu and set selected zone values
$dDownMenu = '';
for ($m=0;$m<count($WUforecasts);$m++) { // for each locations
list($Nlocation,$Nname) = explode('|',$WUforecasts[$m].'|||');
$seltext = '';
if($haveIndex == $m) {
$FCSTlocation = $Nlocation;
$WU_URL = $Nname;
$seltext = ' selected="selected" ';
}
$dDownMenu .= " <option value=\"$m\"$seltext>".langtransstr($Nlocation)."</option>\n";
}
// build the drop down menu
$ddMenu = '';
// create menu if at least two locations are listed in the array
if (isset($WUforecasts[0]) and isset($WUforecasts[1])) {
if($doRTL) {$RTLopt = ' style="direction: rtl;"'; } else {$RTLopt = '';};
$ddMenu .= '<tr align="center">
<td style="font-size: 14px; font-family: Arial, Helvetica, sans-serif">
<script type="text/javascript">
<!--
function menu_goto( menuform ){
selecteditem = menuform.logfile.selectedIndex ;
logfile = menuform.logfile.options[ selecteditem ].value ;
if (logfile.length != 0) {
location.href = logfile ;
}
}
//-->
</script>
<form action="" method="get">
<p><select name="z" onchange="this.form.submit()"'.$RTLopt.'>
<option value=""> - '.langtransstr('Select Forecast').' - </option>
' . $dDownMenu .
$ddMenu . ' </select></p>
<div><noscript><pre><input name="submit" type="submit" value="'.langtransstr('Get Forecast').'" /></pre></noscript></div>
</form>
</td>
</tr>
';
}
$Force = false;
if (isset($_REQUEST['force']) and $_REQUEST['force']=="1" ) {
$Force = true;
}
$doDebug = false;
if (isset($_REQUEST['debug']) and strtolower($_REQUEST['debug'])=='y' ) {
$doDebug = true;
}
$fileName = $WU_URL;
if ($doDebug) {
$Status .= "<!-- WU URL: $fileName -->\n";
}
if (isset($uomTemp) and $showTempsAs <> 'B') { // use Settings.php Temp units only
$showTempsAs = preg_match('|C|i',$uomTemp) ? 'C' : 'F';
$Status .= "<!-- temps in $showTempsAs -->\n";
}
if ($autoSetTemplate and isset($_SESSION['CSSwidescreen'])) {
if($_SESSION['CSSwidescreen'] == true) {
$maxWidth = '900px';
$maxIcons = 14;
$maxForecasts = 14;
$numIconsInFoldedRow = 7;
$Status .= "<!-- autoSetTemplate using ".$SITE['CSSwideOrNarrowDefault']." aspect. -->\n";
}
if($_SESSION['CSSwidescreen'] == false) {
$maxWidth = '640px';
$maxIcons = 10;
$maxForecasts = 14;
$numIconsInFoldedRow = 5;
$Status .= "<!-- autoSetTemplate using ".$SITE['CSSwideOrNarrowDefault']." aspect. -->\n";
}
}
$cacheName = $cacheFileDir . $cacheName;
$cacheName = preg_replace('|\.txt|is',"-$haveIndex-$lang.txt",$cacheName); // unique cache per language used
$APIfileName = WU_get_APIURL($fileName); // transform WU page URL to API query URL
if (! $Force and file_exists($cacheName) and filemtime($cacheName) + $refetchSeconds > time()) {
$html = implode('', file($cacheName));
$Status .= "<!-- loading from $cacheName (" . strlen($html) . " bytes) -->\n";
} else {
$Status .= "<!-- loading from $APIfileName. -->\n";
$html = WU_fetchUrlWithoutHanging($APIfileName,$cacheName);
$RC = '';
if (preg_match("|^HTTP\/\S+ (.*)\r\n|",$html,$matches)) {
$RC = trim($matches[1]);
}
$Status .= "<!-- RC=$RC, bytes=" . strlen($html) . " -->\n";
if (preg_match('|30\d |',$RC)) { // handle possible blocked redirect
preg_match('|Location: (\S+)|is',$html,$matches);
if(isset($matches[1])) {
$sURL = $matches[1];
if(preg_match('|opendns.com|i',$sURL)) {
$Status .= "<!-- NOT following to $sURL --->\n";
} else {
$Status .= "<!-- following to $sURL --->\n";
$html = WU_fetchUrlWithoutHanging($sURL,false);
$RC = '';
if (preg_match("|^HTTP\/\S+ (.*)\r\n|",$html,$matches)) {
$RC = trim($matches[1]);
}
$Status .= "<!-- RC=$RC, bytes=" . strlen($html) . " -->\n";
}
}
}
$fp = fopen($cacheName, "w");
if (!$fp) {
$Status .= "<!-- unable to open $cacheName for writing. -->\n";
} else {
$write = fputs($fp, $html);
fclose($fp);
$Status .= "<!-- saved cache to $cacheName (". strlen($html) . " bytes) -->\n";
}
}
preg_match('|charset="{0,1}(\S+)"{0,1}|i',$html,$matches);
if (isset($matches[1])) {
$charsetInput = strtoupper($matches[1]);
} else {
$charsetInput = 'UTF-8';
}
$doIconv = ($charsetInput == $charsetOutput)?false:true; // only do iconv() if sets are different
$Status .= "<!-- using charsetInput='$charsetInput' charsetOutput='$charsetOutput' doIconv='$doIconv' doRTL='$doRTL' -->\n";
$i = strpos($html,"\r\n\r\n");
$headers = substr($html,0,$i-1);
$content = substr($html,$i+4);
if(preg_match('|Transfer-Encoding: chunke|Ui',$headers)) {
$Status .= "<!-- unchunking response -->\n";
$Status .= "<!-- in=".strlen($html);
$html = preg_replace("|\r\n[0-9a-fA-F]+\r\n|is",'',$html); // kludge, but should get them all :)
$Status .= " out=".strlen($html). " bytes -->\n";
}
// process the file .. select out the 7-day forecast part of the page
$UnSupported = false;
// --------------------------------------------------------------------------------------------------
$Status .= "<!-- processing JSON entries for forecast -->\n";
$i = strpos($html,"\r\n\r\n");
$headers = substr($html,0,$i-1);
$content = substr($html,$i+4);
$rawJSON = $content;
$Status .= "<!-- rawJSON size is ".strlen($rawJSON). " bytes -->\n";
$rawJSON = WU_prepareJSON($rawJSON);
$JSON = json_decode($rawJSON,true); // get as associative array
$Status .= WU_decode_JSON_error();
//$Status .= "<!-- JSON\n".print_r($JSON,true)." -->\n";
/*
$Status = htmlentities($Status);
$Status = preg_replace("|\n|is","<br/>\n",$Status);
print $Status;
return;
*/
if(json_last_error() === JSON_ERROR_NONE) { // got good JSON .. process it
$UnSupported = false;
$WUforecastcity = trim(trim($JSON['location']['city']));
if($doIconv) {$WUforecastcity = iconv($charsetInput,$charsetOutput.'//TRANSLIT',$WUforecastcity);}
if($doDebug) {
$Status .= "<!-- WUforecastcity='$WUforecastcity' -->\n";
}
$WUtitle = "5-day Forecast";
// Process the Period forecasts for High/Low temperatures (one period=1 day)
$n = 0;
foreach ($JSON['forecast']['simpleforecast']['forecastday'] as $i => $FCpart) {
$sT = ($showTempsAs == 'C')?'celsius':'fahrenheit';
$WUforecasttemp[$n] = "<span style=\"color: #ff0000;\">".$FCpart['high'][$sT]."°$showTempsAs</span>";
$n++;
$WUforecasttemp[$n] = "<span style=\"color: #0000ff;\">".$FCpart['low'][$sT]."°$showTempsAs</span>";
$n++;
}
// process the forecast details from the 1/2 day periods (day, night, etc)
$n = 0;
foreach ($JSON['forecast']['txt_forecast']['forecastday'] as $i => $FCpart) { //
if ( $doDebug) {
$Status .= "<!-- processing forecastday[$i]='" . $FCpart['title'] . "' -->\n";
}
$sT = ($showTempsAs == 'C')?'fcttext_metric':'fcttext';
if($i >= $maxForecasts or strlen($FCpart['title'])+strlen($FCpart[$sT]) < 10) {
break;
}
$WUforecasticon[$n] = $FCpart['icon'].'.gif';
if ($doDebug) {
$Status .= "<!-- WUforecasticon[$n]='" . $WUforecasticon[$n] . "' -->\n";
}
$WUforecastday[$n] = trim($FCpart['title']);
if($doIconv) {$WUforecastday[$n] = iconv($charsetInput,$charsetOutput.'//TRANSLIT',$WUforecastday[$n]);}
$WUforecasttitles[$n] = $WUforecastday[$n];
if ($doDebug) {
$Status .= "<!-- WUforecastday[$n]='" . $WUforecastday[$n] . "' -->\n";
}
$WUforecasttext[$n] = trim($FCpart[$sT]);
if($doIconv) {$WUforecasttext[$n] = preg_replace('/(Chance of Precip.|Chance of precipitation:)/s',iconv($charsetOutput,$charsetInput,$xlateCOP).':',$WUforecasttext[$n]);}
if($doIconv) {$WUforecasttext[$n] = iconv($charsetInput,$charsetOutput.'//TRANSLIT',$WUforecasttext[$n]);}
if ($doDebug) {
$Status .= "<!-- WUforecasttext[$n]='" . $WUforecasttext[$n] . "' -->\n";
}
$WUforecastpop[$n] = $FCpart['pop'];
if ($doDebug) {
$Status .= "<!-- WUforecastpop[$n]='" . $WUforecastpop[$n] . "' -->\n";
}
$temp = explode('.',$WUforecasttext[$n]); // split as sentences (sort of).
$WUforecastcond[$n] = trim($temp[0]); // take first one as summary.
if ($doDebug) {
$Status .= "<!-- forecastcond[$n]='" . $WUforecastcond[$n] . "' -->\n";
}
if (count(explode(' ',$WUforecastcond[$n])) > $maxForecastLegendWords) {
if(function_exists('langtransstr')) {
$WUforecastcond[$n] = langtransstr($NWSiconlist[$WUforecasticon[$n]][1]); // use our description instead
} else {
$WUforecastcond[$n] = $NWSiconlist[$WUforecasticon[$n]][1]; // use our description instead
}
$Status .= "<!-- replaced forecastcond[$n]='" . $WUforecastcond[$n] . "' -->\n";
}
$WUforecasticons[$n] = $WUforecastday[$n] . "<br/>" .
WU_img_replace($WUforecasticon[$n],$WUforecastcond[$n],$WUforecastpop[$n]) . "<br/>" .
$WUforecastcond[$n];
$n++;
}
}
// All finished with parsing, now prepare to print
$wdth = intval(100/count($WUforecasticons));
$ndays = intval(count($WUforecasticon)/2);
$WUtitle = preg_replace('|5|i',$ndays,$WUtitle,1);
$doNumIcons = $maxIcons;
if(count($WUforecasticons) < $maxIcons) { $doNumIcons = count($WUforecasticons); }
$IncludeMode = false;
$PrintMode = true;
if (isset($doPrintWU) && ! $doPrintWU ) {
print $Status;
return;
}
if (isset($_REQUEST['inc']) &&
strtolower($_REQUEST['inc']) == 'noprint' ) {
print $Status;
return;
}
if (isset($_REQUEST['inc']) && strtolower($_REQUEST['inc']) == 'y') {
$IncludeMode = true;
}
if (isset($doIncludeWU)) {
$IncludeMode = $doIncludeWU;
}
$printHeading = true;
$printIcons = true;
$printText = true;
if (isset($doPrintHeadingWU)) {
$printHeading = $doPrintHeadingWU;
}
if (isset($_REQUEST['heading']) ) {
$printHeading = substr(strtolower($_REQUEST['heading']),0,1) == 'y';
}
if (isset($doPrintIconsWU)) {
$printIcons = $doPrintIconsWU;
}
if (isset($_REQUEST['icons']) ) {
$printIcons = substr(strtolower($_REQUEST['icons']),0,1) == 'y';
}
if (isset($doPrintTextWU)) {
$printText = $doPrintTextWU;
}
if (isset($_REQUEST['text']) ) {
$printText = substr(strtolower($_REQUEST['text']),0,1) == 'y';
}
if (! $IncludeMode and $PrintMode) { ?>
<!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>
<title>WeatherUnderground <?php echo $WUtitle . ' - ' . $WUforecastcity; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo $charsetOutput; ?>" />
</head>
<body style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; background-color:#FFFFFF">
<?php
}
print $Status;
// if the forecast text is blank, prompt the visitor to force an update
if($UnSupported) {
print <<< EONAG
<h1>Sorry.. this <a href="$WU_URL">forecast</a> can not be processed at this time.</h1>
EONAG
;
}
if (strlen($WUforecasttext[0])<2 and $PrintMode and ! $UnSupported ) {
echo '<br/><br/>Forecast blank? <a href="' . $PHP_SELF . '?force=1">Force Update</a><br/><br/>';
}
if ($PrintMode and ($printHeading or $printIcons)) { ?>
<table width="<?php print $maxWidth; ?>" style="border: none;" class="WUforecast">
<?php echo $ddMenu ?>
<?php if($printHeading) { ?>
<tr align="center" style="background-color: #FFFFFF;">
<td><b>WeatherUnderground <?php echo $WUtitle; ?>: </b><span style="color: green;">
<?php echo $WUforecastcity; ?></span>
</td>
</tr>
<?php } // end print heading
if ($printIcons) {
?>
<tr>
<td align="center">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<?php
// see if we need to fold the icon rows due to long text length
$doFoldRow = false; // don't assume we have to fold the row..
if($foldIconRow) {
$iTitleLen =0;
$iTempLen = 0;
$iCondLen = 0;
for($i=0;$i<$doNumIcons;$i++) {
$iTitleLen += strlen(strip_tags($WUforecasttitles[$i]));
$iCondLen += strlen(strip_tags($WUforecastcond[$i]));
$iTempLen += strlen(strip_tags($WUforecasttemp[$i]));
}
print "<!-- lengths title=$iTitleLen cond=$iCondLen temps=$iTempLen -->\n";
$maxChars = 135;
if($iTitleLen >= $maxChars or
$iCondLen >= $maxChars or
$iTempLen >= $maxChars ) {
print "<!-- folding icon row -->\n";
$doFoldRow = true;
}
}
$startIcon = 0;
$finIcon = $doNumIcons;
$incr = $doNumIcons;
if ($doFoldRow) { $wdth = $wdth*2; $incr = $numIconsInFoldedRow; }
for ($k=$startIcon;$k<$doNumIcons-1;$k+=$incr) { // loop over icon rows, 5 at a time until done
$startIcon = $k;
if ($doFoldRow) { $finIcon = $startIcon+$numIconsInFoldedRow; } else { $finIcon = $doNumIcons; }
$finIcon = min($finIcon,$doNumIcons);
print "<!-- start=$startIcon fin=$finIcon num=$doNumIcons -->\n";
if(!$doRTL) {
print " <tr valign=\"top\" align=\"center\">\n";
} else {
print " <tr valign=\"top\" align=\"center\" style=\"direction: rtl\">\n";
}
for ($i=$startIcon;$i<$finIcon;$i++) {
$ni = $doRTL?$numIconsInFoldedRow-1-$i+$startIcon+$k:$i;
print "<td style=\"width: $wdth%; text-align: center;\"><span style=\"font-size: 8pt;\">$WUforecasttitles[$ni]</span></td>\n";
}
print " </tr>\n";
if(!$doRTL) {
print " <tr valign=\"top\" align=\"center\">\n";
} else {
print " <tr valign=\"top\" align=\"center\" style=\"direction: rtl\">\n";
}
for ($i=$startIcon;$i<$finIcon;$i++) {
$ni = $doRTL?$numIconsInFoldedRow-1-$i+$startIcon+$k:$i;
print "<td style=\"width: $wdth%;\">" . WU_img_replace($WUforecasticon[$ni],$WUforecastcond[$ni],$WUforecastpop[$ni]) . "</td>\n";
}
?>
</tr>
<tr valign ="top" align="center">
<?php
for ($i=$startIcon;$i<$finIcon;$i++) {
$ni = $doRTL?$numIconsInFoldedRow-1-$i+$startIcon+$k:$i;
print "<td style=\"width: $wdth%; text-align: center;\"><span style=\"font-size: 8pt;\">$WUforecastcond[$ni]</span></td>\n";
}
print " </tr>\n";
if(!$doRTL) {
print " <tr valign=\"top\" align=\"center\">\n";
} else {
print " <tr valign=\"top\" align=\"center\" style=\"direction: rtl\">\n";
}
for ($i=$startIcon;$i<$finIcon;$i++) {
$ni = $doRTL?$numIconsInFoldedRow-1-$i+$startIcon+$k:$i;
print "<td style=\"width: $wdth%; text-align: center;\">$WUforecasttemp[$ni]</td>\n";
}
?>
</tr>
<?php if(! $iconDir) { // print a PoP row since they aren't using icons
if(!$doRTL) {
print " <tr valign=\"top\" align=\"center\">\n";
} else {
print " <tr valign=\"top\" align=\"center\" style=\"direction: rtl\">\n";
}
for ($i=$startIcon;$i<$finIcon;$i++) {
$ni = $doRTL?$numIconsInFoldedRow-1-$i+$startIcon+$k:$i;
print "<td style=\"width: $wdth%; text-align: center;\">";
if($WUforecastpop[$ni] > 0) {
print "<span style=\"font-size: 8pt; color: #009900;\">PoP: $WUforecastpop[$ni]%</span>";
} else {
print " ";
}
print "</td>\n";
}
?>
</tr>
<?php } // end if iconDir ?>
<?php if ($doFoldRow) {
if(!$doRTL) {
print " <tr valign=\"top\" align=\"center\">\n";
} else {
print " <tr valign=\"top\" align=\"center\" style=\"direction: rtl\">\n";
}
for ($i=$startIcon;$i<$finIcon;$i++) {
print "<td style=\"width: $wdth%; text-align: center;\"> </td>\n";
}
print "</tr>\n";
} // end doFoldRow ?>
<?php } // end of foldIcon loop ?>
</table><!-- end icon table -->
</td>
</tr><!-- end print icons -->
<?php } // end print icons ?>
</table>
<p> </p>
<?php } // end print header or icons
if ($PrintMode and $printText) { ?>
<table style="border: 0" width="<?php print $maxWidth; ?>" class="WUforecast">
<?php
for ($i=0;$i<count($WUforecasttitles);$i++) {
print "<tr valign =\"top\" align=\"left\">\n";
if(!$doRTL) { // normal Left-to-right
print "<td style=\"width: 20%;\"><b>$WUforecasttitles[$i]</b><br /> <br /></td>\n";
print "<td style=\"width: 80%;\">$WUforecasttext[$i]</td>\n";
} else { // print RTL format
print "<td style=\"width: 80%; text-align: right;\">$WUforecasttext[$i]</td>\n";
print "<td style=\"width: 20%; text-align: right;\"><b>$WUforecasttitles[$i]</b><br /> <br /></td>\n";
}
print "</tr>\n";
}
?>
</table>
<?php } // end print text ?>
<?php if ($PrintMode) { ?>
<p> </p>
<p>Forecast from <a href="<?php echo htmlspecialchars($fileName); ?>">WeatherUnderground</a>
for <?php echo $WUforecastcity; ?>.
<?php if($iconType <> '.jpg') {
print "<br/>Animated forecast icons courtesy of <a href=\"http://www.meteotreviglio.com/\">www.meteotreviglio.com</a>.";
}
?>
</p>
<?php
}
if (! $IncludeMode and $PrintMode ) { ?>
</body>
</html>
<?php
}
// Functions --------------------------------------------------------------------------------
function WU_fetchUrlWithoutHanging($url,$cacheurl)
{
global $Status;
// Set maximum number of seconds (can have floating-point) to wait for feed before displaying page without feed
$numberOfSeconds=4;
// Suppress error reporting so Web site visitors are unaware if the feed fails
error_reporting(0);
// Extract resource path and domain from URL ready for fsockopen
$url = str_replace("http://","",$url);
$urlComponents = explode("/",$url);
$domain = $urlComponents[0];
$resourcePath = str_replace($domain,"",$url);
$xml = '';
// Establish a connection
$socketConnection = fsockopen($domain, 80, $errno, $errstr, $numberOfSeconds);
if (!$socketConnection)
{
$Status .= "<!-- Network error: $errstr ($errno) -->\n";
}
else {
$xml = '';
fputs($socketConnection, "GET $resourcePath HTTP/1.1\r\nHost: $domain\r\nConnection: Close\r\nCookie: Units=metric;\r\nUser-agent: PHP,WU-forecast.php,saratoga-weather.org\r\n\r\n");
// Loop until end of file
while (!feof($socketConnection))
{
$xml .= fgets($socketConnection, 4096);
}
fclose ($socketConnection);
}
$xml = preg_replace('|\r\n2000\r\n|is','',$xml); // this funky string started appearing in WU website
return($xml);
}
// -------------------------------------------------------------------------------------------
function WU_img_replace ( $WUimage, $WUcondtext,$WUpop) {
//
// optionally replace the WeatherUnderground icon with an NWS icon instead.
//
global $NWSiconlist,$iconDir,$iconType,$Status;
// $WU_URL = 'http://icons-aa.wxug.com/graphics/conds/';
// $WU_URL = 'http://icons.wunderground.com/graphics/conds/2005/';
$WUiconURL = 'http://icons-pe.wxug.com/i/c/50/';
$curicon = isset($NWSiconlist[$WUimage][0])?$NWSiconlist[$WUimage][0]:''; // translated icon (if any)
if (!$iconDir or !$curicon) { // no change.. use WU icon
return("<img src=\"$WUiconURL$WUimage\" width=\"50\" height=\"50\"
alt=\"$WUcondtext\" title=\"$WUcondtext\"/>");
}
if($iconType <> '.jpg') {
$curicon = preg_replace('|\.jpg|',$iconType,$curicon);
}
$Status .= "<!-- replace icon '$WUimage' with ";
if ($WUpop > 0) {
$testicon = preg_replace('|'.$iconType.'|',$WUpop.$iconType,$curicon);
if (file_exists("$iconDir$testicon")) {
$newicon = $testicon;
} else {
$newicon = $curicon;
}
} else {
$newicon = $curicon;
}
$Status .= "'$newicon' pop=$WUpop -->\n";
return("<img src=\"$iconDir$newicon\" width=\"55\" height=\"58\"
alt=\"$WUcondtext\" title=\"$WUcondtext\"/>");
}
// -------------------------------------------------------------------------------------------
function WU_prepareJSON($input) {
global $Status;
//This will convert ASCII/ISO-8859-1 to UTF-8.
//Be careful with the third parameter (encoding detect list), because
//if set wrong, some input encodings will get garbled (including UTF-8!)
list($isUTF8,$offset,$msg) = WU_check_utf8($input);
if(!$isUTF8) {
$Status .= "<!-- WU_prepareJSON: Oops, non UTF-8 char detected at $offset. $msg. Doing utf8_encode() -->\n";
$str = utf8_encode($input);
list($isUTF8,$offset,$msg) = WU_check_utf8($str);
$Status .= "<!-- WU_prepareJSON: after utf8_encode, i=$offset. $msg. -->\n";
} else {
$Status .= "<!-- WU_prepareJSON: $msg. -->\n";
$str = $input;
}
//Remove UTF-8 BOM if present, json_decode() does not like it.
if(substr($str, 0, 3) == pack("CCC", 0xEF, 0xBB, 0xBF)) $str = substr($str, 3);
return $str;
}
// -------------------------------------------------------------------------------------------
function WU_check_utf8($str) {
// check all the characters for UTF-8 compliance so json_decode() won't choke
// Sometimes, an ISO international character slips in the WU text string.
$len = strlen($str);
for($i = 0; $i < $len; $i++){
$c = ord($str[$i]);
if ($c > 128) {
if (($c > 247)) return array(false,$i,"c>247 c='$c'");
elseif ($c > 239) $bytes = 4;
elseif ($c > 223) $bytes = 3;
elseif ($c > 191) $bytes = 2;
else return false;
if (($i + $bytes) > $len) return array(false,$i,"i+bytes>len bytes=$bytes,len=$len");
while ($bytes > 1) {
$i++;
$b = ord($str[$i]);
if ($b < 128 || $b > 191) return array(false,$i,"128<b or b>191 b=$b");
$bytes--;
}
}
}
return array(true,$i,"Success. Valid UTF-8");
}
// -------------------------------------------------------------------------------------------
function WU_decode_JSON_error() {
$Status = '';
$Status .= "<!-- json_decode returns ";
switch (json_last_error()) {
case JSON_ERROR_NONE:
$Status .= ' - No errors';
break;
case JSON_ERROR_DEPTH:
$Status .= ' - Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
$Status .= ' - Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
$Status .= ' - Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
$Status .= ' - Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
$Status .= ' - Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
$Status .= ' - Unknown error, json_last_error() returns \''.json_last_error(). "'";
break;
}
$Status .= " -->\n";
return($Status);
}
// -------------------------------------------------------------------------------------------
function WU_get_APIURL ($rawURL) {
global $WUAPIkey,$WULANG,$Status,$doDebug;
$newURL = 'http://api.wunderground.com/api/%s/forecast10day/geolookup/lang:%s/q/%s.json';
$Status .= "<!-- WU_API Raw URL='$rawURL' -->\n";
if(preg_match("|query=([^\&]+)|i",$rawURL,$matches)) {
$rawQuery = urldecode(trim($matches[1]));
if(preg_match('|^[\d\-\.\,]+$|',$rawQuery)) { // likely lat,long query.. use it
} else { // likely a City, State query
$t = explode(', ',$rawQuery);
if(isset($t[1])) { $rawQuery = $t[1].'/'.$t[0]; }
$rawQuery = preg_replace('| |','_',$rawQuery);
}
if($doDebug) {$Status .= "<!-- query='$rawQuery' -->\n"; }
$newURL = sprintf($newURL,$WUAPIkey,$WULANG,$rawQuery);
$Status .= "<!-- WU API New URL='$newURL' -->\n";
return($newURL);
}
if(preg_match('|global/stations/(\d+).html|i',$rawURL,$matches)) {
$rawQuery = 'zmw:00000.1.'.trim($matches[1]);
$newURL = sprintf($newURL,$WUAPIkey,$WULANG,$rawQuery);
$Status .= "<!-- WU API New URL='$newURL' -->\n";
return($newURL);
}
if(preg_match('|weather-forecast/zmw:([\d\.]+)|i',$rawURL,$matches)) {
$rawQuery = 'zmw:'.trim($matches[1]);
$newURL = sprintf($newURL,$WUAPIkey,$WULANG,$rawQuery);
$Status .= "<!-- WU API New URL='$newURL' -->\n";
return($newURL);
}
if(preg_match('|/q/([^\s]+)|i',$rawURL,$matches)) { // handle alternate locid:, zmw:
$rawQuery = trim($matches[1]);
$newURL = sprintf($newURL,$WUAPIkey,$WULANG,$rawQuery);
$Status .= "<!-- WU API New URL='$newURL' -->\n";
return($newURL);
}
if(preg_match('|/US/(.*)$|i',$rawURL,$matches)) { // handle US ST/Cityname
$rawQuery = trim($matches[1]);
$newURL = sprintf($newURL,$WUAPIkey,$WULANG,$rawQuery);
$Status .= "<!-- WU API New URL='$newURL' -->\n";
return($newURL);
}
if(preg_match('|/global/([^\s]+)\.html|i',$rawURL,$matches)) { // handle US ST/Cityname
$rawQuery = trim($matches[1]);
$newURL = sprintf($newURL,$WUAPIkey,$WULANG,$rawQuery);
$Status .= "<!-- WU API New URL='$newURL' -->\n";
return($newURL);
}
return('');
}
function WU_loadLangDefaults () {
global $WUlanguages, $WUlangCharsets;
$WUlanguages = array(
'af' => 'AF',
'bg' => 'BU',
'ct' => 'CA',
'dk' => 'DK',
'nl' => 'NL',
'en' => 'EN',
'fi' => 'FI',
'fr' => 'FR',
'de' => 'DL',
'el' => 'GR',
'ga' => 'IR',
'it' => 'IT',
'he' => 'IL',
'hu' => 'HU',
'no' => 'NO',
'pl' => 'PL',
'pt' => 'BR',
'ro' => 'RO',
'es' => 'SP',
'se' => 'SW',
'si' => 'SL',
);
$WUlangCharsets = array(
'bg' => 'ISO-8859-5',
'el' => 'ISO-8859-7',
'he' => 'UTF-8',
'hu' => 'ISO-8859-2',
'ro' => 'ISO-8859-2',
'pl' => 'ISO-8859-2',
'si' => 'ISO-8859-2',
'ru' => 'ISO-8859-5'
);
}
// End of functions --------------------------------------------------------------------------
?>