-
Notifications
You must be signed in to change notification settings - Fork 1
/
get-nnvl-iod.php
512 lines (449 loc) · 23.7 KB
/
get-nnvl-iod.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
<?php
// resize NOAA image-of-the-day from the new NNVL source
// Ken True - [email protected]
//
// Version 1.00 - 06-Aug-2010 -- initial release
// Version 1.01 - 07-Aug-2010 -- corrected link for hi-res image
// Version 1.02 - 09-Aug-2010 -- added UTF-8 to ISO-8859-1 conversion for Title and Text
// Version 1.03 - 26-Aug-2010 -- try to handle issues when IOD posts an MP4 movie instead of a .jpg
// Version 1.04 - 01-Sep-2010 -- allow either PNG or JPG for default image type
// Version 1.05 - 14-Sep-2010 -- autoscale based on new_width, autofind image when .MP4 posted
// Version 1.06 - 21-Nov-2010 -- fix em-dash issue with iconv()
// Version 1.07 - 31-Dec-2010 -- handle missing image when only a movie is posted
// Version 1.08 - 26-Jan-2011 - added support for global $cacheFileDir for new templates
// Version 1.09 - 05-Mar-2011 - replaced split() with explode()
// Version 1.10 - 31-Jan-2012 - Fix for new NNVL page formatting
// Version 1.11 - 31-Aug-2012 - added unchunk for NNVL HTTP page response
// Version 1.12 - 17-Feb-2013 - added detection for non-resizable animated .gif images
// Version 1.13 - 25-Dec-2013 - fix for broken image issue with full NNVL URLs
// Version 1.14 - 28-Dec-2013 - fix for locating hiRes image
//
// Usage:
//
// include_once("get-nnvl-iod.php");
//
// on a PHP page where you'd like the output to appear
//
// Settings ------------------------------------------------------------
$refreshTime = 60*60; // refresh every 60 minutes
$IODdir = './'; // directory for cache files (2 images, 1 html)
// new size for graphic
// $new_width = 640;
// $new_height = 360;
//
$new_width = 620;
$new_height = 349; // note: this will autoadjust based on image W x H px
// --------------------------------------------------------------------
// error_reporting(E_ALL); // uncomment for debugging
// begin code ------
if (isset($_REQUEST['sce']) and 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;
}
global $Debug;
// overrides from Settings.php if available
global $SITE;
if(isset($SITE['cacheFileDir'])) {$IODdir = $SITE['cacheFileDir']; }
// end of overrides from Settings.php
$Debug = "get-nnvl-iod.php V1.14 - 28-Dec-2013<br/>\n";
// Script internal settings .. don't normally change these
$IODpageURL = 'http://www.nnvl.noaa.gov/imageoftheday.php';
$IODpageCache = 'IOD-page-cache.txt'; // cache storage of $IODpage
$IODimageCache = 'IOD-image.jpg'; // cache storage of current IOD image resized to new width/height
$IODimageCacheFS = 'IOD-image-full.jpg'; // cache storage of full-size IOD image
$IODURL = 'http://www.nnvl.noaa.gov/';
//
$hasUrlFopenSet = ini_get('allow_url_fopen');
if(!$hasUrlFopenSet) {
print "<h2>Warning: PHP does not have 'allow_url_fopen = on;' --<br/>image fetch by get-nnvl-iod.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 get-nnvl-iod.php operation.</p>\n";
return;
}
$doRefresh = false;
if (isset($_REQUEST['cache']) and strtolower($_REQUEST['cache']) == 'refresh') {
$refreshTime = 1; // force cache refresh
$doRefresh = true;
$Debug .= "Cache refresh forced <br/>\n";
}
$html = '';
if (file_exists($IODdir.$IODpageCache) and filemtime($IODdir.$IODpageCache) + $refreshTime > time()) {
$Debug .= "cache $IODdir$IODpageCache current<br/>\n";
$rawhtml = file_get_contents($IODdir.$IODpageCache);
list($headers,$html) = explode("\r\n\r\n",$rawhtml);
$Debug .= "html loaded from cache $IODdir$IODpageCache<br/>\n";
} else {
$Debug .= "cache $IODdir$IODpageCache is stale.. reloading<br/>\n";
$doRefresh = true;
}
$RC = '';
if ($doRefresh) { // do a refresh cycle
$rawhtml = getHTTPpageIOD($IODpageURL,false);
list($headers,$html) = explode("\r\n\r\n",$rawhtml);
if (preg_match("|^HTTP\/\S+ (.*)\r\n|",$rawhtml,$matches)) {
$RC = trim($matches[1]);
}
if(preg_match('|200 |',$RC) and strlen($html) > 50) {
$udate = gmdate("D, d M Y H:i:s", time()) . " GMT";
$fp = fopen($IODdir.$IODpageCache, "w");
if ($fp) {
$write = fputs($fp, $rawhtml);
if ($write) {
fputs($fp,"\n<!-- $IODdir$IODpageCache lmod=$udate -->\n");
$Debug .= "$IODdir$IODpageCache page cache updated $udate<br/>\n";
}
fclose($fp);
} else {
$Debug .= "unable to write cache file $IODdir$IODpageCache<br/>\n";
}
} else {
$Debug .= "Problem fetching $IODpageURL with RC=$RC , html length=".strlen($html) . "<br/>\n";
$Debug .= "Headers returned are:\n";
$Debug .= "<pre>\n$headers\n</pre>\n";
$Debug .= "cache not saved.<br/>\n";
}
}
$Debug .= "html size=".strlen($html)." RC=$RC <br/>\n";
if(preg_match('|Transfer-Encoding:\s+chunked|i',$rawhtml)) {
$Debug .= "chunked response, un-chunking\n";
$data = substr($rawhtml, (strpos($rawhtml, "\r\n\r\n")+4));
$Debug .= "in=".strlen($data);
$data = unchunkIOD($data);
$Debug .= " bytes, unchunked=".strlen($data)." bytes\n";
$html = $data;
// $tfile = preg_replace('|\.txt$|','-unchunk.txt',"$IODdir$IODpageCache");
// $fp = fopen($tfile,'w');
// if ($fp) {
// $write = fputs($fp,$data);
// fclose($fp);
// $Debug .= "debug cache written to $tfile\n";
// }
}
preg_match('|charset="{0,1}([^"]+)"{0,1}|i',$headers,$matches);
if (isset($matches[1])) {
$charsetInput = strtoupper($matches[1]);
} else {
$charsetInput = 'ISO-8859-1';
}
$charsetOutput = 'ISO-8859-1';
$Debug .= "using charsetInput='$charsetInput' charsetOutput='$charsetOutput'<br/>\n";
preg_match_all('|<td colspan=\'2\'>(.+)</td>|Uis',$html,$matches);
$Debug .= "main matches:".print_r($matches,true);
/*
[1] => Array
(
[0] => <a href='images/high_resolution/478_MODIS-AOD-Moscow-20100804.jpg'><img style='max-width:900px;' src='images/low_resolution/478_MODIS-AOD-Moscow-20100804.jpg' alt='Data Shows Dangerous Smoke Plumes Over Russia' /></a>
[1] => <big><big>Data Shows Dangerous Smoke Plumes Over Russia</big></big>
[2] => A series of over 500 forest fires across Russia have killed at least 48 people since last week. Air quality in Moscow and the surrounding areas has been severely degraded, with extremely dangerous levels of smoke and carbon monoxide. The cause of these fire outbreaks is due to excessively hot and dry conditions, leading to drought and conditions ripe for the intensification and spread of otherwise normal fires in the Russian forests. These drought conditions can be seen in our real-time data imagery <a href="http://nnvl.noaa.gov/DailyImage.php?product=DroughtRisk_Daily.png%7CDrought+Risk">here</a>. <p>The blanket of smoke resulting from the fires can be seen in satellite Aerosol Optical Depth data, such as from the NASA MODIS sensor, shown here over the period of July 31 - August 2, 2010. Smoke is a combination of gases and small particles called aerosols, which affect the way that energy is transmitted though Earth's atmosphere into space. The MODIS sensor detects these differences. Aerosol optical depths around 0.1 are associated with clear skies, whereas values near 1.0 represent incredibly hazy conditions.
[3] => <a href='images/high_resolution/478_MODIS-AOD-Moscow-20100804.jpg'>View Data Shows Dangerous Smoke Plumes Over Russia - High Resolution Version</a>
[4] =>
[5] => <a href='MediaHome.php?MediaTypeID=1'>Return to List</a>
)
or
[1] => Array
(
[0] => <a href='images/high_resolution/479_August2010-ENSO-SSTAnom.jpg'><img style='max-width:900px;' src='images/low_resolution/479_August2010-ENSO-SSTAnom.jpg' alt='ENSO Cycle: La Niña Conditions Returns in July 2010' /></a>
[1] => <table><tr><td><a href='MediaDetail.php?MediaID=479&MediaTypeID=1'><img src='images/thumbnails/479_August2010-ENSO-SSTAnom-NoLabel-T.jpg' alt='ENSO Cycle: La Niña Conditions Returns in July 2010' /></a>
[2] => <big><big>ENSO Cycle: La Niña Conditions Returns in July 2010</big></big> -->
[3] => Conditions favoring the reemergence of La Niña are developing in the equatorial Pacific. During July, surface and subsurface ocean temperatures dropped well below the average, consistent with the formation of La Niña. In this image, sea surface temperature anomalies are plotted for the first week of August, 2010. Blue areas are indicative of cooler than average temperatures; red areas are warmer than average. Notice the wave of cool temperatures in the equatorial Pacific. These meandering anomalies are characteristic of La Niña. Temperatures in the Atlantic, however, are much warmer than average. These warm seas will fuel the intensification of hurricanes, whereas storms in the Pacific will be less likely to intensify due to the cooler temperatures.
[4] => <a href='images/high_resolution/479_August2010-ENSO-SSTAnom.jpg'>View ENSO Cycle: La Niña Conditions Returns in July 2010 - High Resolution Version</a>
[5] => <a href='images/high_resolution/479-82_August2010-ENSO-SSTAnom-NoLabel.jpg'>View Sea Surface Temperature Anomaly Image - No Labels - High Resolution Version</a>
[6] => -->
[7] => <a href='MediaHome.php?MediaTypeID=1'>Return to List</a> -->
) -->
New version:
[0] => Array
(
[0] => <td colspan='2'><a class='pointer' onclick='shadowboxopen("images/high_resolution/1476v1_20131220-US.png", "Storm System to Bring Hazards to Several Parts of the Nation through this Weekend" )'><img style='height:350px; width:620px;' class='imageMediaPic' src='http://www.nnvl.noaa.gov/images/low_resolution/620x350x1476v1_20131220-US.png.pagespeed.ic.iowPfkKoh2.jpg' alt='Storm System to Bring Hazards to Several Parts of the Nation through this Weekend'/></a></td>
[1] => <td colspan='2'></td></tr></table>
</td>
[2] => <td colspan='2'><table><tr><td colspan='2'>Dec 20, 2013</td>
[3] => <td colspan='2'><div id='mediaPageTitle2'><big><big>Storm System to Bring Hazards to Several Parts of the Nation through this Weekend</big></big></div></td>
[4] => <td colspan='2'><p>A potent storm system will track across the nation through this weekend bringing a variety of weather hazards and impacting holiday travel for much of the country. As the system moves into the Plains, it will be able to draw additional moisture from the Gulf of Mexico increasing the effects of the storm. Heavy rain is forecast from the lower Mississippi River Valley to the Ohio Valley this weekend with a risk for both areal and flash flooding. A wintry mix, including freezing rain and snow, is possible from the central Great Plains, through the Great Lakes and to northern New England this weekend. The NWS Storm Prediction Center (SPC) has indicated a Moderate Risk of severe thunderstorms on Saturday, with possible tornadoes, for portions of the Lower Mississippi Valley. Severe thunderstorms are possible from the Central Gulf Coast/Lower Mississippi Valley into the Ohio Valley Saturday. Unseasonable (perhaps record) warmth continues to build into the southern and eastern U.S. ahead of this storm system, but a powerful cold front will sweep through the Eastern Seaboard bringing a return to normal winter temperatures by Monday. Heavy snow is also forecast for the higher elevations in the Pacific Northwest and Northern Rockies into the weekend. This image was taken by GOES East at 1745Z on December 20, 2013.</p></td>
[5] => <td colspan='2'><div class='title1'><div class='title2' align='left' style='padding-left: 20px;'></div></div></td>
)
[1] => Array
(
[0] => <a class='pointer' onclick='shadowboxopen("images/high_resolution/1476v1_20131220-US.png", "Storm System to Bring Hazards to Several Parts of the Nation through this Weekend" )'><img style='height:350px; width:620px;' class='imageMediaPic' src='http://www.nnvl.noaa.gov/images/low_resolution/620x350x1476v1_20131220-US.png.pagespeed.ic.iowPfkKoh2.jpg' alt='Storm System to Bring Hazards to Several Parts of the Nation through this Weekend'/></a>
[1] => </td></tr></table>
[2] => <table><tr><td colspan='2'>Dec 20, 2013
[3] => <div id='mediaPageTitle2'><big><big>Storm System to Bring Hazards to Several Parts of the Nation through this Weekend</big></big></div>
[4] => <p>A potent storm system will track across the nation through this weekend bringing a variety of weather hazards and impacting holiday travel for much of the country. As the system moves into the Plains, it will be able to draw additional moisture from the Gulf of Mexico increasing the effects of the storm. Heavy rain is forecast from the lower Mississippi River Valley to the Ohio Valley this weekend with a risk for both areal and flash flooding. A wintry mix, including freezing rain and snow, is possible from the central Great Plains, through the Great Lakes and to northern New England this weekend. The NWS Storm Prediction Center (SPC) has indicated a Moderate Risk of severe thunderstorms on Saturday, with possible tornadoes, for portions of the Lower Mississippi Valley. Severe thunderstorms are possible from the Central Gulf Coast/Lower Mississippi Valley into the Ohio Valley Saturday. Unseasonable (perhaps record) warmth continues to build into the southern and eastern U.S. ahead of this storm system, but a powerful cold front will sweep through the Eastern Seaboard bringing a return to normal winter temperatures by Monday. Heavy snow is also forecast for the higher elevations in the Pacific Northwest and Northern Rockies into the weekend. This image was taken by GOES East at 1745Z on December 20, 2013.</p>
[5] => <div class='title1'><div class='title2' align='left' style='padding-left: 20px;'></div></div>
)
)
*/
if(isset($matches[1])) {
for ($i=0;$i<count($matches[1]);$i++) { // loop to find the title line and relative positions
if(preg_match('|<big><big>|is',$matches[1][$i])) {
break;
}
}
$rawImg = $matches[1][0]; // always in the [0] entry
$rawTitle = $matches[1][$i];
$IODimageTitle = strip_tags($rawTitle);
$IODimageText = $matches[1][$i+1];
$IODimageText = preg_replace('/[\r|\n]+/is','',$IODimageText);
$IODimageHiRes = $matches[1][$i+2];
if(!preg_match('|href=|is',$IODimageHiRes)) {
$IODimageHiRes = '';
} else {
$IODimageHiRes = preg_replace('|href=\'|is',"href=\"$IODURL",$IODimageHiRes);
$IODimageHiRes = preg_replace('|\'>|is','">',$IODimageHiRes);
}
preg_match('!shadowboxopen\("([^"]+)"!is',$matches[1][0],$mtemp);
$Debug .= "new hires match: ".print_r($mtemp,true)."<br/>\n";
if(isset($mtemp[1]) and strlen($IODimageHiRes) < 1) {
$IODimageHiRes = $mtemp[1];
if(strpos($IODimageHiRes,$IODURL) == false) {
$IODimageHiRes = $IODURL . $IODimageHiRes;
}
$Debug .= "hiRes: '$IODimageHiRes'<br/>\n";
$IODimageHiRes = "<a href=\"$IODimageHiRes\">High Resolution Image</a>";
}
if(preg_match('|<img.*src=\'([^\']+)\'|Uis',$rawImg,$tmatch)) {
$IODimageURL = $tmatch[1];
if(!preg_match('|http://|',$IODimageURL)) { $IODimageURL = $IODURL . $IODimageURL; }
$Debug .= "found image at '$IODimageURL'<br/>\n";
}
if(!preg_match('/\.(png|jpg)$/i',$IODimageURL) ) {
for ($j=4;$j<count($matches[1]);$j++) {
if(preg_match('|href=\'([^\']+)\'|i',$matches[0][$j],$tmatch) and
preg_match('/\.(png|jpg)$/i',$tmatch[1]) ) {
$IODimageURL = $tmatch[1];
if(!preg_match('|http://|',$IODimageURL)) { $IODimageURL = $IODURL . $IODimageURL; }
$Debug .= "image url not PNG. Substituting HiRes URL '$IODimageURL'<br/>\n";
break;
}
}
}
// $Debug .= "<pre>".print_r($matches,true)."</pre>\n";
} else {
$Debug .= "<pre>".print_r($matches,true)."</pre>\n";
$Debug .= "unable to find image URL or text<br/>\n";
print $Debug;
return;
}
if(function_exists('iconv')) {
error_reporting(E_ALL);
$IODimageTitle = iconv($charsetInput,$charsetOutput.'//TRANSLIT',$IODimageTitle);
$IODimageText = iconv($charsetInput,$charsetOutput.'//TRANSLIT',$IODimageText);
$IODimageHiRes = iconv($charsetInput,$charsetOutput.'//TRANSLIT',$IODimageHiRes);
$Debug .= "$charsetInput to $charsetOutput conversion done<br/>\n";
}
if(isset($IODimageURL) and ! preg_match('|\.gif$|i',$IODimageURL)) {
// get dates
$GraphicTime = file_exists($IODdir.$IODimageCacheFS)?filemtime($IODdir.$IODimageCacheFS):0;
$CacheTime = file_exists($IODdir.$IODimageCache)?filemtime($IODdir.$IODimageCache):0;
$Headers = getHTTPheaders($IODimageURL,1);
// print_r($Headers);
$URLdate = strtotime($Headers['last-modified']);
if ($doRefresh or $URLdate > $GraphicTime) {
$Debug .= "$IODdir$IODimageCacheFS is stale .. downloading new copy from $IODimageURL<br/>\n";
download_file($IODimageURL,$IODdir.$IODimageCacheFS);
}
$GraphicTime = filemtime($IODdir.$IODimageCacheFS);
if ($doRefresh or $GraphicTime > $CacheTime) {
$Debug .= "$IODdir$IODimageCache is stale .. regenerating<br/>\n";
if(preg_match('|\.jpg$|i',$IODimageURL)) {
$image = loadJPEG($IODdir.$IODimageCacheFS); // fetch our full sized image
}
if(preg_match('|\.png$|i',$IODimageURL)) {
$image = loadPNG($IODdir.$IODimageCacheFS); // fetch our full sized image
}
if(preg_match('|\.gif$|i',$IODimageURL)) {
$image = loadGIF($IODdir.$IODimageCacheFS); // fetch our full sized image
}
$MaxX = imagesx($image);
$MaxY = imagesy($image);
$ratio = $MaxX/$new_width;
$new_height = round($MaxY/$ratio,0);
$image_p = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $MaxX, $MaxY);
$tst = imagejpeg($image_p, $IODdir.$IODimageCache, 90);
if(!$tst) {
$Debug .= "$IODdir$IODimageCache NOT saved<br/>\n";
} else {
$Debug .= "$IODdir$IODimageCache saved as $new_width x $new_height px (from $MaxX x $MaxY)<br/>\n";
}
imagedestroy($image);
imagedestroy($image_p);
}
$sorryMsg = '';
} else {
$sorryMsg = '<h3>Sorry... today\'s image is a movie or animated .gif file and cannot be reformatted to show here. Please visit the <a href="'.$IODpageURL.'">NNVL site</a> to view it.</h3>';
}
/* if (file_exists($Cache)) {
header("Content-type: image/jpeg"); // now send to browser
header("Last-modified: " . $Headers['last-modified']);
readfile($Cache);
}
*/
print "<div id=\"NOAAIOD\">\n";
print "<h2>$IODimageTitle</h2>\n";
if($sorryMsg == '') {
print "<img src=\"$IODdir$IODimageCache\" alt=\"$IODimageTitle\" title=\"$IODimageTitle\" height=\"$new_height\" width=\"$new_width\" />\n";
} else {
print $sorryMsg;
}
if (preg_match('|^<p>|i',$IODimageText)) {
print "$IODimageText\n";
} else {
print "<p>$IODimageText</p>\n";
}
if(isset($IODimageHiRes)) {
print "<p><small>$IODimageHiRes</small></p>\n";
}
print "<p><small>Courtesy of <a href=\"$IODpageURL\">NOAA Environmental Visualization Laboratory</a></small></p>\n";
print "</div>\n";
$Debug = "<!-- ". preg_replace('|\n|is'," -->\n<!-- ",$Debug);
$Debug .= "end of debug output -->\n";
$Debug = preg_replace('|<br/>|is','',$Debug);
print $Debug;
return;
function loadJPEG ($imgname) {
global $Debug;
$im = imagecreatefromjpeg ($imgname); /* Attempt to open */
if (!$im) { /* See if it failed */
$Debug .= "Error loading $imgname<br/>\n";
}
return $im;
}
function loadPNG ($imgname) {
global $Debug;
$im = imagecreatefrompng ($imgname); /* Attempt to open */
if (!$im) { /* See if it failed */
$Debug .= "Error loading $imgname<br/>\n";
}
return $im;
}
function loadGIF ($imgname) {
global $Debug;
$im = imagecreatefromgif ($imgname); /* Attempt to open */
if (!$im) { /* See if it failed */
$Debug .= "Error loading $imgname<br/>\n";
}
return $im;
}
function getHTTPheaders($url,$format=0) {
global $Debug;
$url_info=parse_url($url);
$port = isset($url_info['port']) ? $url_info['port'] : 80;
$fp=fsockopen($url_info['host'], $port, $errno, $errstr, 30);
if($fp) {
$head = "HEAD ".@$url_info['path']."?".@$url_info['query'];
$head .= " HTTP/1.0\r\nHost: ".@$url_info['host']."\r\n\r\n";
fputs($fp, $head);
while(!feof($fp)) {
if($header=trim(fgets($fp, 8192))) {
if($format == 1) {
$h2 = explode(': ',$header);
// the first element is the http header type, such as HTTP/1.1 200 OK,
// it doesn't have a separate name, so we have to check for it.
if($h2[0] == $header) {
$headers['status'] = $header;
} else {
$headers[strtolower($h2[0])] = trim($h2[1]);
}
} else {
$headers[] = $header;
}
}
}
fclose($fp);
return $headers;
} else {
return false;
}
} // end of get_headers function def
function getHTTPpageIOD ($url, $useFopen=false) {
global $Debug;
if (! $useFopen) {
// 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
$FullUrl = $url;
$url = str_replace("http://","",$url);
$urlComponents = explode("/",$url);
$domain = $urlComponents[0];
$resourcePath = str_replace($domain,"",$url);
$Debug .= "GET $resourcePath HTTP/1.1 <br/>\n Host: $domain <br/>\n";
// Establish a connection
$socketConnection = fsockopen($domain, 80, $errno, $errstr, $numberOfSeconds);
if (!$socketConnection)
{
// You may wish to remove the following debugging line on a live Web site
$Debug .= "Network error: $errstr ($errno) <br/>\n";
} // end if
else {
$xml = '';
$getString = "GET $resourcePath HTTP/1.1\r\nHost: $domain\r\nConnection: close\r\nUser-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11 global-conditions SaratogaWX)\r\n";
$getString .= "\r\n";
fputs($socketConnection, $getString);
// Loop until end of file
while (!feof($socketConnection))
{
$xml .= fgets($socketConnection, 8192);
} // end while
fclose ($socketConnection);
} // end else
return($xml);
} else {
// print "<!-- using file function -->\n";
$xml = implode('',file($url));
return($xml);
}
} // end getHTTPpageIOD
// ------------------------------------------------------------------
function download_file($file_source, $file_target) {
global $Debug;
$rh = fopen($file_source, 'rb');
if(!$rh) {
$Debug .= "unable to read $file_source<br/>\n";
}
$wh = fopen($file_target, 'wb');
if(!$wh) {
$Debug .= "unable to write $file_target<br/>\n";
}
if ($rh===false || $wh===false) {
// error reading or opening file
return true;
}
while (!feof($rh)) {
if (fwrite($wh, fread($rh, 1024)) === FALSE) {
$Debug .= 'Cannot write to file ('.$file_target.')' ." <br/>\n";
return true;
}
}
fclose($rh);
fclose($wh);
// No error
$Debug .= "loaded $file_target from $file_source<br/>\n";
return false;
}
// unchunk a chunked HTTP response
function unchunkIOD($data) {
$fp = 0;
$outData = "";
while ($fp < strlen($data)) {
$rawnum = substr($data, $fp, strpos(substr($data, $fp), "\r\n") + 2);
$num = hexdec(trim($rawnum));
$fp += strlen($rawnum);
$chunk = substr($data, $fp, $num);
$outData .= $chunk;
$fp += strlen($chunk);
}
return $outData;
}
?>