This repository was archived by the owner on Apr 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathec-radar.php
More file actions
1247 lines (1116 loc) · 46.7 KB
/
ec-radar.php
File metadata and controls
1247 lines (1116 loc) · 46.7 KB
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
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// PHP script by Ken True, webmaster@saratoga-weather.org
// ec-radar.php version 1.00 - 29-Dec-2006
// version 1.01 - 31-Dec-2006 - added realpath() to make better use of cache file specs
// commented out 'display rings' code for cleaner detail display
// Version 1.02 - 06-Aug-2007 - corrected PHP delimiter at top of script (missing php)
// Version 1.03 - 20-Aug-2007 - added switches for overlay images, mods to getting correct directory
// Version 1.04 - 21-Feb-2008 - added support for common SITE variables
// Version 1.05 - 04-Aug-2008 - added support for EC Website changes
// Version 1.06 - 26-Sep-2008 - fixes for EC Website changes for XHTML 1.0-Strict output, improved cache handling
// Version 1.07 - 30-Apr-2009 - fixed minor bug for PHP 5+ (missing php after <? marker)
// Version 1.08 - 30-Sep-2010 - added new debugging features and an updated image parsing logic (from Pablo Sanchez)
// Version 1.09 - 17-Apr-2013 - changes for new EC website design, UTF-8 convert+new path method
// Version 1.10 - 19-Sep-2013 - changes for revised EC website radar colors -default for 14-color radar
// Version 1.11 - 18-Oct-2014 - changes for new EC website design
// Version 1.12 - 03-Mar-2015 - changes for EC website design
// Version 2.00 - 05-Nov-2015 - major changes: repl EC image.php overlays, support for 8 or 14 color images
// Version 2.01 - 30-Nov-2015 - changes for revised EC website (timezone extract+use meteo.gc.ca for french)
// Version 2.02 - 22-Feb-2017 - use cURL fetch, HTTPS to EC website, improved error handling
// Version 2.03 - 28-Jul-2018 - update for EC website change (get latest image issue)
// Version 2.04 - 17-Apr-2019 - added list of operating radar sites and new CASxx sites for display
// Version 2.05 - 27-Aug-2019 - updated radar site allowed list
// Version 2.06 - 27-Jan-2021 - updated radar site allowed list + minor URT-8 discovery fix
//
$Version = "V2.06 - 27-Jan-2021";
// error_reporting(E_ALL);
//
// Settings:
// --------- start of settings ----------
// you need to set the $ECURL to the radar image for your site
//
// Go to http://weather.gc.ca/ and select your language (sorry, I don't
// speak French, so these instructions are all in English)
//
// Go to http://weather.gc.ca/radar/index_e.html
// Click on the radar circle around your city/area.
// You should see a radar page with an url like
// http://weather.gc.ca/radar/index_e.html?id=xxx
// copy the three letter radar id=XXX into $siteID = 'XXX'; below
//
$siteID = 'WKR'; // set to default Site for radar (same as id=xxx on EC website)
//
$defaultLang = 'en'; // set to 'fr' for french default language
// // set to 'en' for english default language
//
$cacheName = 'ec-radar.txt'; // note: will be changed to -en.txt or
// -fr.txt depending on language choice and stored in $radarDir
$radarDir = './radar/'; // directory for storing radar-XXX-0.png to radar-XXX-6.png images
// note: relative to document root.
//
$refetchSeconds = 300; // look for new radar from EC every 5 minutes (300 seconds)
// NOTE: EC may take up to 20 minutes to publish new images
$noRadarMinutes = 25; // minutes to wait before declaring the radar site as 'N/O -not operational'
//
$aniSec = 1; // number of seconds between animations
//
$linkToPage = ''; // detail url to link to when map link is clicked
// default '' will link to this program (ec-radar.php)
$showRivers = true; // set to true to include rivers in display
$showRoads = true; // set to true to show major roads (default)
$showRoadLabels = false; // set to true to show road numbers/labels
$showRoadNumber = false; // set to true to show road numbers in display
$showRadarRings = false; // set to true to include range rings in images
$showTowns = true; // set to true to include major towns (default)
$showAdditTowns = true; // set to true to include additional towns in display
$showRegionalTowns = true; // show towns on Regional and National maps
$show14Color = true; // set to false to show new 8-color maps, set to true for original 14-color maps
//
$charsetOutput = 'ISO-8859-1'; // default character encoding of output
// ---------- end of settings -----------
//------------------------------------------------
// overrides from Settings.php if available
global $SITE;
if (isset($SITE['ecradar'])) {$siteID = $SITE['ecradar'];}
if (isset($SITE['defaultlang'])) {$defaultLang = $SITE['defaultlang'];}
if (isset($SITE['charset'])) {$charsetOutput = strtoupper($SITE['charset']); }
// end of overrides from Settings.php if available
//
// ---------- main code -----------------
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;
}
// Current EC radar list as of Wed, 27 Jan 2021 13:30:52 -0800
// based on radars available at https://weather.gc.ca/radar/index_e.html
//
$allowedSites = array(
'NAT' => 'National Map',
'PAC' => 'Pacific',
'WUJ' => 'Aldergrove (near Vancouver)',
'XPG' => 'Prince George',
'XSS' => 'Silver Star Mountain (near Vernon)',
'XSI' => 'Victoria',
'WRN' => 'Prairies',
'CASBE' => 'Bethune (near Regina)',
'WHK' => 'Carvel (near Edmonton)',
'CASFW' => 'Foxwarren (near Brandon)',
'WHN' => 'Jimmy Lake (near Cold Lake)',
'CASRA' => 'Radisson (near Saskatoon)',
'CASSU' => 'Schuler (near Medicine Hat)',
'CASSR' => 'Spirit River (near Grande Prairie)',
'CASSM' => 'Strathmore (near Calgary)',
'CASWL' => 'Woodlands (near Winnipeg)',
'ONT' => 'Ontario',
'WBI' => 'Britt (near Sudbury)',
'CASDR' => 'Dryden',
'CASET' => 'Exeter (near London)',
'XFT' => 'Franktown (near Ottawa)',
'WKR' => 'King City (near Toronto)',
'CASMR' => 'Montreal River (near Sault Ste Marie)',
'CASRF' => 'Smooth Rock Falls (near Timmins)',
'XNI' => 'Superior West (near Thunder Bay)',
'QUE' => 'Quebec',
'WMB' => 'Lac Castor (near Saguenay)',
'CASLA' => 'Landrienne (near Rouyn-Noranda)',
'CASBV' => 'Blainville (near Montréal)',
'CASVD' => 'Val d\'Irène (near Mont Joli)',
'CASSF' => 'Villeroy (near Trois-Rivières)',
'ERN' => 'Atlantic',
'CASCM' => 'Chipman (near Fredericton)',
'XGO' => 'Halifax',
'CASHR' => 'Holyrood (near St. John\'s)',
'XME' => 'Marble Mountain',
'CASMB' => 'Marion Bridge (near Sydney)',
); // end of list of allowed sites
// error_reporting(E_ALL & ~E_NOTICE); // uncomment to turn on full error reporting
$hasUrlFopenSet = ini_get('allow_url_fopen');
if(!$hasUrlFopenSet) {
print "<h2>Warning: PHP does not have 'allow_url_fopen = on;' --<br/>image fetch by ec-radar.php is not possible.</h2>\n";
print "<p>To fix, add the statement: <pre>allow_url_fopen = on;\n\n</pre>to your php.ini file to enable ec-radar.php operation.</p>\n";
return;
}
$t = pathinfo(__FILE__); // get our program name for the HTML comments
$Program = $t['basename'];
$Status = "<!-- ec-radar.php - $Version -->\n";
$BasePath = $t['dirname'];
//$Status .= "<!-- basepath='$BasePath' -->\n";
$printIt = true;
if(isset($_REQUEST['inc']) && strtolower($_REQUEST['inc']) == 'y' or
(isset($doInclude) and $doInclude)) {$doInclude = true;}
if(isset($doPrint)) { $printIt = $doPrint; }
if(! isset($doInclude)) {$doInclude = false; }
if(isset($_REQUEST['site'])) { $siteID = strtoupper($_REQUEST['site']); }
$siteID = preg_replace('|[^A-Z]+|s','',$siteID); // Make sure only alpha in siteID
if(!isset($allowedSites[$siteID])) {
print "<p>Sorry... site id '$siteID' is not a valid EC radar site name.</p>\n";
return;
}
if (isset($_REQUEST['cache']) && (strtolower($_REQUEST['cache']) == 'no') ) {
$forceRefresh = true;
} else {
$forceRefresh = false;
}
if (isset($doAutoPlay)) {
$autoPlay = $doAutoPlay;
} elseif (isset($_REQUEST['play']) && (strtolower($_REQUEST['play']) == 'no') ) {
$autoPlay = false;
} else {
$autoPlay = true;
}
if (isset($_REQUEST['imgonly']) && (strtolower($_REQUEST['imgonly']) == 'y')) {
$imageOnly = true; // just return the latest thumbnail image after processing
$printIt = false; // and don't spoil the image with any other stuff
} else {
$imageOnly = false;
}
if (isset($_REQUEST['lang'])) {
$Lang = strtolower($_REQUEST['lang']);
}
if (isset($doLang)) {$Lang = $doLang;};
if (! isset($Lang)) {$Lang = $defaultLang;};
if ($Lang == 'fr') {
$LMode = 'f';
$ECNAME = "Environnement Canada";
$ECHEAD = 'Radar météo';
$ECNO = 'N/O - Non opérationnel';
$LNoJS = 'Pour voir l\'animation, il faut que JavaScript soit en fonction.';
$LPlay = 'Animer - Pause';
$LPrev = 'Image précédente';
$LNext = 'Prochaine image';
} else {
$Lang = 'en';
$LMode = 'e';
$ECNAME = "Environment Canada";
$ECHEAD = 'Weather Radar';
$ECNO = 'N/O - Non-operational';
$LNoJS = 'Please enable JavaScript to view the animation.';
$LPlay = 'Play - Stop';
$LPrev = 'Previous';
$LNext = 'Next';
}
$cacheName = preg_replace('|.txt$|',"-$Lang.txt",$cacheName);
//
if (isset($_SERVER['DOCUMENT_ROOT'])) {
$ROOTDIR = $_SERVER['DOCUMENT_ROOT'];
} else {
$ROOTDIR = '.';
}
$TradarDir = $radarDir;
if (substr($TradarDir,0,1) == '.') {$TradarDir = substr($TradarDir,1); } //prune off '.' from './' if need be
if (substr($TradarDir,0,1) <> '/') {$TradarDir = '/' . $TradarDir; } // put on leading slash if missing
$cacheDir = $BasePath . $TradarDir;
$imageDir = $radarDir;
$Status .= "<!-- cacheDir='$cacheDir' -->\n<!-- imageDir='$imageDir' -->\n";
date_default_timezone_set( @date_default_timezone_get());
$Status .= "<!-- date default timezone='".date_default_timezone_get()."' -->\n";
// Default radar image sizes from EC
$new_width = 580;
$new_height = 480;
$thumb_width = 290;
$thumb_height = 240;
if (! preg_match('|^[WXC]|',$siteID) || ($siteID == 'WRN') ) {
// the regional summary sizes
$new_width = 573;
$new_height = 300;
$thumb_width = 290;
$thumb_height = 150;
}
if ($siteID == 'NAT') {
// the national summary size
$new_width = 600;
$new_height = 522;
$thumb_width = 300;
$thumb_height = 261;
}
if (!$linkToPage) {
$linkToPage = $_SERVER['PHP_SELF'];
}
if (isset($_REQUEST['linkto'])) {
$linkToPage = $_REQUEST['linkto'];
}
// all settings and overrides now loaded ... begin processing
$Status .= "<!-- siteID='$siteID' -->\n";
$cacheName = preg_replace('|.txt$|',"-$siteID.txt",$cacheName);
$RawImgURL = "https://weather.gc.ca/data/radar/detailed/temp_image/$siteID/%s";
$RawOvlURL = "https://weather.gc.ca";
$compositeSites = array(
'NAT' => 'nat',
'PAC' => 'pyr',
'WRN' => 'pnr',
'ONT' => 'ont',
'QUE' => 'que',
'ERN' => 'ern',
);
if(isset($compositeSites[$siteID])) {
$RawImgURL = "https://weather.gc.ca/data/radar/temp_image/COMPOSITE_$siteID/%s";
}
$ECURL = 'https://weather.gc.ca/radar/index_' . $LMode . '.html?id=' . $siteID;
if($Lang == 'fr') {
$RawImgURL = preg_replace('|weather|i','meteo',$RawImgURL);
$ECURL = preg_replace('|weather|i','meteo',$ECURL);
$Status .= "<!-- french language - using meteo.gc.ca for data -->\n";
}
$RealCacheName = $cacheDir . $cacheName;
$tpath = realpath($RealCacheName);
if ($tpath) {
$Status .= "<!-- changed '$RealCacheName' \n to: '$tpath' -->\n";
$RealCacheName = $tpath;
}
$reloadImages = false; // assume we don't have to reload unless a newer image set is around
if(file_exists($RealCacheName)) {
$lastCacheTime = filemtime($RealCacheName);
} else {
$lastCacheTime = time();
$forceRefresh = true;
}
$lastCacheTimeHM = gmdate("Y-m-d H:i:s",$lastCacheTime) . " UTC";
$NOWgmtHM = gmdate("Y-m-d H:i:s",time()) . " UTC";
$diffSecs = time() - $lastCacheTime;
$Status .= "<!-- now='$NOWgmtHM' page cached='$lastCacheTimeHM' ($diffSecs seconds ago) -->\n";
if(isset($_GET['force']) | isset($_GET['cache'])) {$refetchSeconds = 0;}
if($diffSecs > $refetchSeconds) {$forceRefresh = true;}
$Status .= "<!-- forceRefresh=";
$Status .= $forceRefresh?'true':'false';
$Status .= " -->\n";
// refresh cached copy of page if needed
// fetch/cache code by Tom at carterlake.org
if (! $forceRefresh) {
$Status .= "<!-- using Cached version from $cacheName -->\n";
$site = implode('', file($RealCacheName));
$forceRefresh = true;
} else {
$Status .= "<!-- loading $cacheName from\n '$ECURL' -->\n";
$site = ECR_fetchUrlWithoutHanging($ECURL,false);
$fp = fopen($RealCacheName, "w");
if (strlen($site) and $fp) {
$write = fputs($fp, $site);
fclose($fp);
$Status .= "<!-- loading finished. New page cache saved to $cacheName ".strlen($site)." bytes -->\n";
$reloadImages = true;
} else {
$Status .= "<!-- unable to open $cacheName for writing ".strlen($site)." bytes.. cache not saved -->\n";
$Status .= "<!-- file: '$RealCacheName' -->\n";
$Status .= "<!-- html loading finished -->\n";
}
}
if(strlen($site) < 100) {
print "<p>Sorry. Incomplete file received from Environment Canada website.</p>\n";
print $Status;
return;
}
preg_match('|charset="{0,1}([^"\n]+)"{0,1}\n|i',$site,$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' -->\n";
// find the site name
//
preg_match_all('|<title>(.*)</title>|',$site,$matches);
// $Status .= "<!-- matches\n" . print_r($matches,true) . " -->\n";
$siteTitle = $matches[1][0];
if($doIconv and $siteTitle) {
$siteTitle = iconv($charsetInput,$charsetOutput.'//TRANSLIT',$siteTitle);
}
preg_match_all('|<h1 id="wb-cont" property="name">(.*)</h1>|',$site,$matches);
$siteHeading = $matches[1][0];
if($doIconv and $siteHeading) {
$siteHeading = iconv($charsetInput,$charsetOutput.'//TRANSLIT',$siteHeading);
}
if(preg_match_all('|<noscript>\s+<p[^>]+>(.*)</p>\s+</noscript>>|',$site,$matches) ) {
$siteDescr = $matches[1][0];
} else {
$siteDescr = '';
}
if($doIconv and $siteDescr) {
$siteDescr = iconv($charsetInput,$charsetOutput.'//TRANSLIT',$siteDescr);
}
// used to be "timezone":{"id":"CDT","title":"Central Daylight Time","offset":"-5"}
// now is "timezone":{"id":"Canada\/Eastern","title":"","offset":""}
preg_match_all('|"timezone"\:\{"id":"([^"]+)","title":"([^"]*)","offset":"([^"]*)"\}|Uis',$site,$matches);
// $Status .= "<!-- timezone matches \n" . print_r($matches,true) . " -->\n";
if (!$matches[3][0]) { // oops.. no offset. use text to calculate proper values.
// look for latest image text description
// <p class="text-center margin-bottom-none">PRECIPET - Rain 2015-11-29, 07:40 PM EST</p>
preg_match('|<p class="text-center margin-bottom-none">([^<]+)</p>|is',$site,$matches);
// $Status .= "<!-- img tz matches\n".print_r($matches,true)." -->\n";
$tStr = substr($matches[1],strpos($matches[1],'2'));
$Status .= "<!-- latest image local = '$tStr' -->\n";
// tStr = '2015-11-30, 18:50 UTC' (national)
// tStr = '2015-11-30, 02:00 PM EST' (English)
// tStr = '2015-11-30, 14:00 HNE' (French)
$tStr = str_replace(',','',$tStr);
$TZ = substr($tStr,strlen($tStr)-3); // peel off the text TZ abbreviation
$TZName = $TZ;
$tStr = substr($tStr,0,strlen($tStr)-4);
//$Status .= "<!-- tStr = '$tStr' -->\n";
//find UTC time from latest image url
preg_match('!<img id="animation-image".*src=".*_([\d|_]+).GIF"!Uis',$site,$matches);
//$Status .= "<!-- img GIF matches\n".print_r($matches,true)." -->\n";
$tStrUTC = $matches[1];
// 2015_11_30_21_30
//$Status .= "<!-- tStrUTC ='$tStrUTC' -->\n";
$t = explode('_',$tStrUTC);
$tStrUTC = $t[0].'-'.$t[1].'-'.$t[2].' '.$t[3].':'.$t[4];
$Status .= "<!-- latest image UTC = '$tStrUTC' -->\n";
$TZOffsetSecs = strtotime($tStr.' GMT')-strtotime($tStrUTC.' GMT');
$TZHrs = $TZOffsetSecs / 3600;
} else {
$TZ = $matches[1][0];
$TZName = $matches[2][0];
$TZHrs = $matches[3][0];
$TZOffsetSecs = $TZHrs * 3600;
}
$Status .= "<!-- TZ='$TZ' TZHrs='$TZHrs' TZOffsetSecs='$TZOffsetSecs' TZName='$TZName' -->\n";
//this is for NAT/regional maps only
if(preg_match_all('|<map(.*)</map>|Uis',$site,$matches)) {
// $Status .= "<!-- map matches \n" . print_r($matches,true) . " -->\n";
$MapDef = implode('',$matches[0]); // extract image map text from page
$MapDef = preg_replace('|/radar/index_[ef]\.html\?id=|is',"$linkToPage?lang=$Lang&site=",$MapDef);
// $MapDef = preg_replace('|<area ([^>]+)>|is',"<area $1>",$MapDef);
$MapDef = preg_replace('| xmlns:html="http://www.w3.org/Profiles/XHTML-transitional"|','',$MapDef);
$MapDef = preg_replace('|<map (.*) alt="[^"]+" ([^>]+)>|','<map $1 $2 >',$MapDef);
// $MapDef = preg_replace('|name="([^"]+)"|i','name="$1" id="$1"',$MapDef);
preg_match_all('|\s+name="(.*)"\s*|Ui',$MapDef,$matches2);
// $Status .= "<!-- map matches2 \n" . print_r($matches2,true) . " -->\n";
$MapName = $matches2[1][0];
if($doIconv) {
$MapName = iconv($charsetInput,$charsetOutput.'//TRANSLIT',$MapName);
$MapDef = iconv($charsetInput,$charsetOutput.'//TRANSLIT',$MapDef);
}
// $Status .= "<!-- map \n" . print_r($MapDef,true) . " -->\n";
// $Status .= "<!-- mapname = '$MapName' -->\n";
} else {
$MapDef = '';
$MapName = '';
}
// V1.12 -- special find of current image as background in animation
$mostRecentImg = '';
if(preg_match('|src="/data/radar/temp_image//\S+/(\S+).GIF"|is',$site,$tmatch)) {
$mostRecentImg = $tmatch[1];
$Status .= "<!-- most recent='$mostRecentImg' -->\n";
}
// find all the radar images available
// in two passes.. get the base and the short image list.
$start = strpos($site, '<div class="col-lg-3 col-md-4 col-xs-5">');
$finish = strpos($site, '</ul>',$start);
$length = $finish-$start;
$shortList = substr($site, $start, $length);
// $Status .= "<!-- start=$start finish=$finish length=$length len(shortList)=".strlen($shortList)." -->\n";
// $Status .= "<!-- shortList = '$shortList' -->\n";
// now pick up the short list
// $start = strpos($site, 'image-list-title">');
// $finish = strpos($site, '</div>',$start);
// $length = $finish-$start;
// $shortList .= substr($site, $start, $length);
// $Status .= "<!-- start=$start finish=$finish length=$length len(shortList)=".strlen($shortList)." -->\n";
$number_found = preg_match_all("!(display|base)\=\'(.*?)\'!", $shortList, $matches);
// $Status .= "<!-- display/base matches = $number_found matches=".print_r($matches,true)."\n MapName '$MapName' -->\n";
//
// New EC Radar HTML has logic to handle if JavaSript is disabled
// which causes the above regular expression to generate two `base'
// .GIF's.
//
// The work-around is to determine whether cell 1 is a `base', if so, we
// delete cell 0. To plan for possible future logic, we keep
// deleting `base' entries until there's only one remaining.
//
if ($number_found > 2 && $matches[1][1] == "base") {
for ($i = 1; $i <= $number_found; $i++) {
// Remove the first set of `base' values.
unset($matches[0][0]);
unset($matches[1][0]);
unset($matches[2][0]);
// Do we end our for-loop?
if ($i < $number_found && $matches[1][1] != "base") {
break;
}
}
// Reset index numbers so data sorts properly by time.
$matches[0] = array_values($matches[0]);
$matches[1] = array_values($matches[1]);
$matches[2] = array_values($matches[2]);
}
// $Status .= "<!-- finished matches\n" . print_r($matches,true) . " -->\n";
// V1.12 -- add in most recent image to array if available
if($mostRecentImg <> '') {
$imglist[] = $mostRecentImg; // add to first entry
foreach ($matches[2] as $i => $ourImg) {
$imglist[] = $ourImg;
}
} else {
$imglist = $matches[2];
}
$Status .= "<!-- imglist\n" . print_r($imglist,true) . " -->\n";
// $imglistText = array();
$total_time = 0;
$newestRadarCacheFile = '';
$lastRadarGMTText ='';
$newestRadarImgHTML = '';
$numImages = 0;
$NOWgmt = time();
$NOWdate = gmdate("D, d M Y H:i:s", $NOWgmt);
$imglistText[0] = '';
$lastRadarGMT = 0;
if($reloadImages) {
$default_opts = array(
'http'=>array(
'method'=>"GET",
'protocol_version' => 1.1,
'header'=>"Cache-Control: no-cache, must-revalidate\r\n" .
"Cache-control: max-age=0\r\n" .
"Connection: close\r\n" .
"User-agent: Mozilla/5.0 (ec-radar.php - saratoga-weather.org)\r\n" .
"Accept: text/html,text/plain\r\n"
),
'ssl'=>array(
'method'=>"GET",
'protocol_version' => 1.1,
'verify_peer' => false,
'header'=>"Cache-Control: no-cache, must-revalidate\r\n" .
"Cache-control: max-age=0\r\n" .
"Connection: close\r\n" .
"User-agent: Mozilla/5.0 (ec-radar.php - saratoga-weather.org)\r\n" .
"Accept: text/html,text/plain\r\n"
)
);
$default = stream_context_set_default($default_opts);
// Now generate overlays in the same order as used by EC
/*
/cacheable/images/radar/layers/rivers/wkr_rivers.gif
/cacheable/images/radar/layers/roads/WKR_roads.gif
/cacheable/images/radar/layers/road_labels/wkr_labs.gif
/cacheable/images/radar/layers/radar_circle/radar_circle.gif
/cacheable/images/radar/layers/additional_cities/wkr_towns.gif
/cacheable/images/radar/layers/default_cities/wkr_towns.gif
towns overlay for summary files.
nat: /cacheable/images/radar/layers/composite_cities/nat_composite.gif
pac: /cacheable/images/radar/layers/composite_cities/pyr_composite.gif
wrn: /cacheable/images/radar/layers/composite_cities/pnr_composite.gif
ont: /cacheable/images/radar/layers/composite_cities/ont_composite.gif
que: /cacheable/images/radar/layers/composite_cities/que_composite.gif
atl: /cacheable/images/radar/layers/composite_cities/atl_composite.gif
*/
$townOverlays = array(
'NAT' => 'nat',
'PAC' => 'pyr',
'WRN' => 'pnr',
'ONT' => 'ont',
'QUE' => 'que',
'ERN' => 'ern',
);
$imgType = $show14Color ? '14-Color':'8-Color';
$u8c = $show14Color ? '':'_detailed';
// V2.00 -- generate the overlay directly using GD functions
$Status .= "<!-- generating overlay(s) -->\n";
$Status .= "<!-- image sizes are w=$new_width h=$new_height. Thumbnail is w=$thumb_width h=$thumb_height. -->\n";
$Status .= "<!-- using $imgType radar $u8c overlay(s) -->\n";
$overlayIMG = imagecreatetruecolor($new_width,$new_height);
$overlayBGColor = imagecolorallocatealpha($overlayIMG,255,255,255,127); // transparency=full
imagecolortransparent($overlayIMG,$overlayBGColor); // what the EC uses...
imagefill($overlayIMG,0,0,$overlayBGColor); // make image background transparent
// Enable blend mode and save full alpha channel
imagealphablending($overlayIMG, true);
imagesavealpha($overlayIMG, true);
$didOverlay = '';
if(isset($townOverlays[$siteID]) and $showRegionalTowns) { // summary site.. use only one overlay
$tIMGraw = file_get_contents($RawOvlURL.
'/cacheable/images/radar/layers'.$u8c.'/composite_cities/' .
$townOverlays[$siteID] . '_composite.gif');
$tIMG = imagecreatefromstring($tIMGraw);
if($tIMG) {
displayTrans($tIMG,'rivers');
imagecopy($overlayIMG,$tIMG,0,0,0,0,$new_width,$new_height);
imagedestroy($tIMG);
$didOverlay .= 'Composite cities, ';
} else {
print "<!-- unable to load composite towns overlay -->\n";
}
}
if(!isset($townOverlays[$siteID])) { // not a summary site
if ($showRivers) {
$tIMGraw = file_get_contents($RawOvlURL.
'/cacheable/images/radar/layers'.$u8c.'/rivers/' . strtolower($siteID) . '_rivers.gif');
$tIMG = imagecreatefromstring($tIMGraw);
if($tIMG) {
displayTrans($tIMG,'rivers');
imagecopy($overlayIMG,$tIMG,0,0,0,0,$new_width,$new_height);
imagedestroy($tIMG);
$didOverlay .= 'Rivers, ';
} else {
print "<!-- unable to load rivers overlay -->\n";
}
}
if ($showRoads) {
$tIMGraw = file_get_contents($RawOvlURL.
'/cacheable/images/radar/layers'.$u8c.'/roads/'. $siteID . '_roads.gif');
$tIMG = imagecreatefromstring($tIMGraw);
if($tIMG) {
displayTrans($tIMG,'roads');
imagecopy($overlayIMG,$tIMG,0,0,0,0,$new_width,$new_height);
imagedestroy($tIMG);
$didOverlay .= 'Roads, ';
} else {
print "<!-- unable to load roads overlay -->\n";
}
}
if ($showRoadLabels) {
$tIMGraw = file_get_contents($RawOvlURL.
'/cacheable/images/radar/layers'.$u8c.'/road_labels/' . strtolower($siteID) . '_labs.gif');
$tIMG = imagecreatefromstring($tIMGraw);
if($tIMG) {
displayTrans($tIMG,'road labels');
imagecopy($overlayIMG,$tIMG,0,0,0,0,$new_width,$new_height);
imagedestroy($tIMG);
$didOverlay .= 'Road Labels, ';
} else {
print "<!-- unable to load road labels overlay -->\n";
}
}
if ($showRadarRings) {
$tIMGraw = file_get_contents($RawOvlURL.
'/cacheable/images/radar/layers'.$u8c.'/radar_circle/radar_circle.gif');
$tIMG = imagecreatefromstring($tIMGraw);
if($tIMG) {
displayTrans($tIMG,'radar circles');
imagecopy($overlayIMG,$tIMG,0,0,0,0,$new_width,$new_height);
imagedestroy($tIMG);
$didOverlay .= 'Radar Circles, ';
} else {
print "<!-- unable to load radar rings overlay -->\n";
}
}
if ($showAdditTowns) {
$tIMGraw = file_get_contents($RawOvlURL.
'/cacheable/images/radar/layers'.$u8c.'/additional_cities/' . strtolower($siteID) .'_towns.gif');
$tIMG = imagecreatefromstring($tIMGraw);
if($tIMG) {
displayTrans($tIMG,'addit. cities');
imagecopy($overlayIMG,$tIMG,0,0,0,0,$new_width,$new_height);
imagedestroy($tIMG);
$didOverlay .= 'Additional Cities, ';
} else {
print "<!-- unable to load additional cities overlay -->\n";
}
}
if ($showTowns) {
$tIMGraw = file_get_contents($RawOvlURL.
'/cacheable/images/radar/layers'.$u8c.'/default_cities/' . strtolower($siteID) .'_towns.gif');
$tIMG = imagecreatefromstring($tIMGraw);
if($tIMG) {
displayTrans($tIMG,'cities');
imagecopy($overlayIMG,$tIMG,0,0,0,0,$new_width,$new_height);
imagedestroy($tIMG);
$didOverlay .= 'Default Cities, ';
} else {
print "<!-- unable to load default cities overlay -->\n";
}
}
} // end of overlay creation for regular radar sites
if(strlen($didOverlay) > 1) {
$didOverlay = substr($didOverlay,0,strlen($didOverlay)-2);
$Status .= "<!-- overlay(s) generated: $didOverlay -->\n";
}
imagepng($overlayIMG,$cacheDir.'overlay-'.$siteID.'.png'); // save it
}
//print "<b>now process</b> \n";
//print "<pre>\n" . print_r($imglist,true) . "</pre>\n";
// process image file list from EC radar page
foreach ($imglist as $i => $ourImg ) {
$ourImg .= '.GIF'; // have to add back the .GIF since the preg_match_all removes it.
$radarCacheFile = "radar-$siteID-$i.png";
$RealRadarCacheFile = $cacheDir . $radarCacheFile;
$tpath = realpath($RealRadarCacheFile);
if ($tpath <> '') {
$RealRadarCacheFile = $tpath;
}
$imgURL = sprintf($RawImgURL,$ourImg);
// $Status .= "<!-- image='$ourImg' -->\n";
// $Status .= "<!-- cache='$radarCacheFile' -->\n";
preg_match('|_(\d+)_(\d+)_(\d+)_(\d+)_(\d+).GIF|',$ourImg,$matches);
// $Status .= "<!-- matches\n" . print_r($matches,true) . " -->\n";
$RadarGMT = gmmktime($matches[4],$matches[5],0,$matches[2],$matches[3],$matches[1]);
$RadarGMTText = gmdate("D, d M Y H:i:s", $RadarGMT);
// $Status .= "<!-- rdr=$RadarGMTText ($RadarGMT) -->\n";
$imglistText[$i] = gmdate("Y-m-d H:i ",$RadarGMT+$TZOffsetSecs) . $TZ;
if ($i == 0) { // newest image processing
$lastRadarGMT = $RadarGMT;
$lastRadarGMTText = $RadarGMTText;
$diff = $NOWgmt - $lastRadarGMT;
$Status .= "<!-- now=$NOWdate ($NOWgmt) \n rdr=$lastRadarGMTText ($lastRadarGMT) age=$diff secs -->\n";
} // end img=0 processing
if ($reloadImages) { // do the reload
$didIt = false;
$time_start = ECR_fetch_microtime();
if($show14Color) {
$imgURL = preg_replace('|/detailed/|','/',$imgURL); // get old detailed images only
} else {
$imgURL = preg_replace('|/radar/temp_image/|i','/radar/detailed/temp_image/',$imgURL);
}
$Status .= "<!-- Loading $imgType $imgURL \n to $radarCacheFile \n dir=$cacheDir -->\n";
$didIt = download_file($imgURL,$cacheDir,$radarCacheFile,$overlayIMG,$new_width,$new_height);
$time_stop = ECR_fetch_microtime();
$total_time += ($time_stop - $time_start);
$time_fetch = sprintf("%01.3f",round($time_stop - $time_start,3));
if ($didIt) {
$Status .= "<!-- reloaded $radarCacheFile in $time_fetch secs. ($RadarGMTText UTC) -->\n";
} else {
$Status .= "<!-- unable to reload $radarCacheFile ($time_fetch secs.) -->\n";
}
} // end if reloadImages
$numImages++;
} // end foreach imglist
if ($reloadImages) { // make thumbnail too for latest image
$imgname = "radar-$siteID-0.png"; // get latest image name
$thumbname = preg_replace('|\.png|','-sm.png',$imgname);
$time_start = ECR_fetch_microtime();
$image = imagecreatefrompng ($cacheDir . $imgname);; // fetch our radar
if (! $image ) { // oops... no existing image, create a dummy one
$image = imagecreate ($new_width, $new_height); /* Create a blank image */
$bgc = imagecolorallocate ($image, 128, 128, 128);
imagefilledrectangle ($image, 0, 0, $new_width, $new_height, $bgc);
}
$MaxX = imagesx($image);
$MaxY = imagesy($image);
$image_p = imagecreatetruecolor($thumb_width, $thumb_height);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $MaxX, $MaxY);
if (time() > ($lastRadarGMT + $noRadarMinutes*60 + $refetchSeconds + 15)) {
// stale radar if > 25 minutes + refetchTime + 15 seconds old
$text_color = imagecolorallocate ($image_p, 192,51,51);
$bgcolor = imagecolorallocate ($image, 128, 128, 128);
imagefilledrectangle($image_p, 5, 95, 230, 140,$bgcolor);
imagestring ($image_p, 5, 15, 100, "$ECNO", $text_color);
imagestring ($image_p, 5, 15, 120, $imglistText[0], $text_color);
}
imagepng($image_p, $cacheDir . $thumbname);
imagedestroy($image);
imagedestroy($image_p);
$time_stop = ECR_fetch_microtime();
$total_time += ($time_stop - $time_start);
$time_fetch = sprintf("%01.3f",round($time_stop - $time_start,3));
$Status .= "<!-- small image w=$thumb_width h=$thumb_height saved to $thumbname in $time_fetch secs. -->\n";
$Status .= "<!-- image files cached in ".sprintf("%01.3f",round($total_time))." secs. -->\n";
}
if(isset($overlayIMG)) {imagedestroy($overlayIMG);}
if ($imageOnly) {
$ourImg = $cacheDir . "radar-$siteID-0-sm.png";
if (file_exists($ourImg)) {
$ourImgSize = filesize($ourImg);
$ourImgGMT = filectime($ourImg);
header("Content-type: image/png"); // now send to browser
header("Content-length: " . $ourImgSize);
header("Last-modified: " . gmdate("D, d M Y H:i:s", $ourImgGMT) . ' GMT');
header("Expires: " . gmdate("D, d M Y H:i:s", $ourImgGMT+$refetchSeconds) . ' GMT');
readfile($ourImg);
}
exit;
}
// print it out:
if ($printIt && ! $doInclude) {
//------------------------------------------------
header("Cache-Control: no-cache,no-store, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
$NOWdate = gmdate("D, d M Y H:i:s", time());
header("Expires: $NOWdate GMT");
header("Last-Modified: $NOWdate GMT");
?>
<!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="Refresh" content="300" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Cache-Control" content="no-cache" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title><?php print "$siteTitle"; ?></title>
<style type="text/css">
body {
background-color: #FFFFFF;
}
.ECradar {
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:12px;
color: #000000;
}
.ECradar p {
text-align:center;
}
</style>
</head>
<body>
<?php
}
print $Status;
print "<!-- autoplay=";
print $autoPlay?'true':'false';
print " -->\n";
if ($printIt) {
$ECURL = preg_replace('|&|Ui','&',$ECURL); // make link XHTML compatible
// print "<!-- imglistTxt \n" . print_r($imglistText,true) . " -->\n";
// print $newestRadarImgHTML;
print "<div class=\"ECradar\">\n";
gen_animation($numImages, $siteID, $radarDir,$aniSec);
// print $imgHTML;
print "<p><a href=\"$ECURL\">$siteHeading - $ECNAME</a></p>\n</div> <!-- end of ECradar -->\n";
}
if ($printIt && ! $doInclude) {?>
</body>
</html>
<?php
}
// ----------------------------functions -----------------------------------
function ECR_fetchUrlWithoutHanging($url,$useFopen) {
// get contents from one URL and return as string
global $Status, $needCookie;
$overall_start = time();
if (! $useFopen) {
// Set maximum number of seconds (can have floating-point) to wait for feed before displaying page without feed
$numberOfSeconds=6;
// Thanks to Curly from ricksturf.com for the cURL fetch functions
$data = '';
$domain = parse_url($url,PHP_URL_HOST);
$theURL = str_replace('nocache','?'.$overall_start,$url); // add cache-buster to URL if needed
$Status .= "<!-- curl fetching '$theURL' -->\n";
$ch = curl_init(); // initialize a cURL session
curl_setopt($ch, CURLOPT_URL, $theURL); // connect to provided URL
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // don't verify peer certificate
curl_setopt($ch, CURLOPT_USERAGENT,
'Mozilla/5.0 (ec-radar.php - saratoga-weather.org)');
curl_setopt($ch,CURLOPT_HTTPHEADER, // request LD-JSON format
array (
"Accept: text/html,text/plain"
));
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $numberOfSeconds); // connection timeout
curl_setopt($ch, CURLOPT_TIMEOUT, $numberOfSeconds); // data timeout
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return the data transfer
curl_setopt($ch, CURLOPT_NOBODY, false); // set nobody
curl_setopt($ch, CURLOPT_HEADER, true); // include header information
// curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow Location: redirect
// curl_setopt($ch, CURLOPT_MAXREDIRS, 1); // but only one time
if (isset($needCookie[$domain])) {
curl_setopt($ch, $needCookie[$domain]); // set the cookie for this request
curl_setopt($ch, CURLOPT_COOKIESESSION, true); // and ignore prior cookies
$Status .= "<!-- cookie used '" . $needCookie[$domain] . "' for GET to $domain -->\n";
}
$data = curl_exec($ch); // execute session
if(curl_error($ch) <> '') { // IF there is an error
$Status .= "<!-- curl Error: ". curl_error($ch) ." -->\n"; // display error notice
}
$cinfo = curl_getinfo($ch); // get info on curl exec.
/*
curl info sample
Array
(
[url] => http://saratoga-weather.net/clientraw.txt
[content_type] => text/plain
[http_code] => 200
[header_size] => 266
[request_size] => 141
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.125
[namelookup_time] => 0.016
[connect_time] => 0.063
[pretransfer_time] => 0.063
[size_upload] => 0
[size_download] => 758
[speed_download] => 6064
[speed_upload] => 0
[download_content_length] => 758
[upload_content_length] => -1
[starttransfer_time] => 0.125
[redirect_time] => 0
[redirect_url] =>
[primary_ip] => 74.208.149.102
[certinfo] => Array
(
)
[primary_port] => 80
[local_ip] => 192.168.1.104
[local_port] => 54156
)
*/
$Status .= "<!-- HTTP stats: " .
" RC=".$cinfo['http_code'] .
" dest=".$cinfo['primary_ip'] ;
if(isset($cinfo['primary_port'])) {
$Status .= " port=".$cinfo['primary_port'] ;
}
if(isset($cinfo['local_ip'])) {
$Status .= " (from sce=" . $cinfo['local_ip'] . ")";
}
$Status .=
"\n Times:" .
" dns=".sprintf("%01.3f",round($cinfo['namelookup_time'],3)).
" conn=".sprintf("%01.3f",round($cinfo['connect_time'],3)).
" pxfer=".sprintf("%01.3f",round($cinfo['pretransfer_time'],3));
if($cinfo['total_time'] - $cinfo['pretransfer_time'] > 0.0000) {
$Status .=
" get=". sprintf("%01.3f",round($cinfo['total_time'] - $cinfo['pretransfer_time'],3));
}
$Status .= " total=".sprintf("%01.3f",round($cinfo['total_time'],3)) .
" secs -->\n";
//$Status .= "<!-- curl info\n".print_r($cinfo,true)." -->\n";
curl_close($ch); // close the cURL session
//$Status .= "<!-- raw data\n".$data."\n -->\n";
$i = strpos($data,"\r\n\r\n");
$headers = substr($data,0,$i);
$content = substr($data,$i+4);
if($cinfo['http_code'] <> '200') {
$Status .= "<!-- headers returned:\n".$headers."\n -->\n";
}
return $data; // return headers+contents
} else {
// print "<!-- using file_get_contents function -->\n";
$STRopts = array(
'http'=>array(
'method'=>"GET",
'protocol_version' => 1.1,
'header'=>"Cache-Control: no-cache, must-revalidate\r\n" .
"Cache-control: max-age=0\r\n" .
"Connection: close\r\n" .
"User-agent: Mozilla/5.0 (ec-radar.php - saratoga-weather.org)\r\n" .
"Accept: text/html,text/plain\r\n"
),
'ssl'=>array(
'method'=>"GET",
'protocol_version' => 1.1,
'verify_peer' => false,
'header'=>"Cache-Control: no-cache, must-revalidate\r\n" .
"Cache-control: max-age=0\r\n" .
"Connection: close\r\n" .
"User-agent: Mozilla/5.0 (ec-radar.php - saratoga-weather.org)\r\n" .
"Accept: text/html,text/plain\r\n"
)
);
$STRcontext = stream_context_create($STRopts);
$T_start = ECR_fetch_microtime();