-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrss.php
103 lines (87 loc) · 3.56 KB
/
rss.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
<?php
/**
* PHP Parcel Tracker
*
* Cache-enabled RSS gateway for tracking packages.
* Requires PHP 5 or greater.
*
* @package PHP_Parcel_Tracker
* @author Brian Stanback <[email protected]>
* @author Thom Dyson <[email protected]>
* @copyright Copyright (c) 2008, Brian Stanback, Thom Dyson
* @license http://www.apache.org/licenses/LICENSE-2.0.html Apache 2.0
* @version 3.0 <27 July 2010>
* @filesource
*/
/****************************************************************************
* Copyright 2008 Brian Stanback, Thom Dyson
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
***************************************************************************/
include_once('parceltracker.class.php');
// Configuration overrides, see parceltracker.class.php for all options and descriptions
$config = array(
'cacheDir' => 'cache', // The cache directory
'cacheInterval' => 14400, // The number of seconds before reloading tracking info
// Set to 0 to disable caching
'dateFormat' => 'us',
'showDayOfWeek' => true
);
// Get the requested package details
$carrier = isset($_REQUEST['type']) ? $_REQUEST['type'] : '';
$trackingNumber = isset($_REQUEST['num']) ? $_REQUEST['num'] : '';
// Sanitize the request variables
$carrier = preg_replace('#[^a-z0-9]#', '', strtolower($carrier));
$trackingNumber = preg_replace('#[^A-Z0-9]#', '', strtoupper($trackingNumber));
$cacheFile = '';
$rss = '';
if ($config['cacheInterval'] > 0) {
// Check for cached results
$cacheFile = $config['cacheDir'] . '/' . dechex(crc32($carrier . '_' . $trackingNumber)) . '.xml';
if (file_exists($cacheFile)) {
$modtime = filemtime($cacheFile);
$rss = file_get_contents($cacheFile);
if (strpos($rss, '<b>Arrival:</b>') !== false || time() - $config['cacheInterval'] < $modtime) {
// The package is delivered (no need to re-fetch tracking data) or the cache TTL hasn't expired
$lastmod = gmdate('D, d M Y H:i:s \G\M\T', $modtime);
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && ($_SERVER['HTTP_IF_MODIFIED_SINCE'] == $lastmod)) {
// Cached by the browser
header('Last-Modified: ' . $lastmod, true, 304);
return;
}
// Use file-based cache
header('Last-Modified: ' . $lastmod, true, 200);
} else {
$rss = '';
}
}
}
if (empty($rss)) {
// Perform a new tracking lookup
$tracker = new ParcelTracker($config);
$parcel = $tracker->getDetails($trackingNumber, $carrier);
// DEBUG
//print_r($parcel);
//return;
$rss = $tracker->toRSS($parcel, $config['cacheInterval']);
if ($config['cacheInterval'] > 0) {
// Cache the results
file_put_contents($cacheFile, $rss);
// Output last-modified header to enable browser/device caching
$lastmod = gmdate('D, d M Y H:i:s \G\M\T', filemtime($cacheFile));
header('Last-Modified: ' . $lastmod, true, 200);
}
}
// Output the results
header('Content-type: text/xml');
echo $rss;