Permalink
Browse files

gmt related bugfixes. method now exposed via method parameter (1-7). …

…please see the readme for new updates to the settings.inc file.
  • Loading branch information...
1 parent 5a43613 commit a35e40ed441a83307137a7b22e7d498b50f48241 @ahmedre ahmedre committed Apr 25, 2010
Showing with 74 additions and 26 deletions.
  1. +10 −0 README
  2. +7 −1 index.php
  3. +52 −21 prayertimes.inc
  4. +2 −2 views/footer.inc
  5. +3 −2 views/main.inc
View
10 README
@@ -20,6 +20,16 @@ requirements:
$appid = ''; // put your yahoo app id here
$gaCode = ''; // put your google analytics code here
+ define('GA_CODE', $gaCode);
+ define('YAHOO_APPID', $appid);
+ define('USERNAME', 'geonames_username');
+
+ * note - the username parameter was added to geonames to prevent abuse
+ of their webservice. you can make one for free on the geonames site,
+ or comment out the json part of prayertimes.inc and uncomment the xml
+ part. for more information, please see:
+ http://geonames.wordpress.com/2010/03/16/ddos-part-ii/
+
- for apache, you need to use the .htaccess file for cleaner links
- for lighttpd, something like this under the host should work:
url.rewrite = ( "^/rss(.*)" => "/index.php$1&rss=1",
View
@@ -15,7 +15,13 @@
include 'prayertimes.inc';
$q = $_GET['q'];
if (strlen($q) > 0){
- $data = PrayerTimes::getPrayerTimes($q);
+ $method = 4;
+ if (isset($_GET['method'])){
+ $meth = $_GET['method'];
+ if ((is_numeric($meth)) && ($meth > 0) && ($meth < 8))
+ $method = $meth;
+ }
+ $data = PrayerTimes::getPrayerTimes($q, $method);
if ($data['type']=='error'){
print $data['msg'];
$data = null;
View
@@ -1,15 +1,16 @@
<?php
+include_once 'settings.inc';
+
class PrayerTimes {
+
public static function getLocations($q){
- include 'settings.inc'; // todo: make these defines and use them
$url = "http://local.yahooapis.com/MapsService/V1/geocode" .
- "?appid=$appid&location=";
+ "?appid=" . YAHOO_APPID . "&location=";
$q = urlencode($_GET['q']);
$format = isset($_GET['rss'])? 1 : 0;
$url = $url . $q . "&output=php";
-
if (strlen($q) == 0) return;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
@@ -20,10 +21,12 @@ class PrayerTimes {
}
public static function getTimezone($lat, $long){
- // geonames' json api is currently broken - switching to xml api
- // $url = "http://ws.geonames.org/timezoneJSON?lat=$lat&lng=$long";
+ $url = "http://ws.geonames.org/timezoneJSON?lat=$lat&lng=$long";
+
+ // xml interface
+ // $url = "http://ws.geonames.org/timezone?lat=$lat&lng=$long";
- $url = "http://ws.geonames.org/timezone?lat=$lat&lng=$long";
+ $url .= "&username=" . USERNAME;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
@@ -33,7 +36,7 @@ class PrayerTimes {
$ret = $info['http_code'];
curl_close($ch);
- // specifically for xml interface
+ /* specifically for xml interface
$result = array();
if (preg_match('/\<timezoneId>(.*)<\/timezoneId>/', $res, $matches))
$result['timezoneId'] = $matches[1];
@@ -42,8 +45,8 @@ class PrayerTimes {
if (preg_match('/\<dstOffset>(.*)</dstOffset>/', $res, $matches))
$result['dstOffset'] = $matches[1];
- // return array('rescode' => $ret, 'result' => json_decode($res, true));
- return array('rescode' => $ret, 'result' => $result);
+ return array('rescode' => $ret, 'result' => $result); */
+ return array('rescode' => $ret, 'result' => json_decode($res, true));
}
public static function getPrayerTimes($q, $method = 4){
@@ -86,27 +89,56 @@ class PrayerTimes {
return array('type' => 'error',
'msg' => $errmsg, 'data' => array());
}
-
+
$tz_data = $tz_arr['result'];
- $gmt_offset = $tz_data['gmtOffset'];
$timezone_id = $tz_data['timezoneId'];
-
- // unfortunately, the dstOffset returned by geonames
- // is not necessarily accurate (ex with Egypt). so
- // have to keep update zoneinfo packages and compare
- // to whatever php says.
+
+ // unfortunately, the time offsets returned by geonames
+ // are not necessarily accurate (ex dst with Egypt, gmt
+ // with Sydney). we consequently ignore geonames' offsets
+ // and figure out the current offset and whether or not we
+ // are in dst, since that's all we need. all this means is
+ // that we have to keep the zoneinfo package on the system
+ // updated.
try {
$tz = new DateTimeZone($timezone_id);
$date = new DateTime();
$date->setTimezone($tz);
- $dst_offset = $date->getOffset()/3600;
+ $cur_offset = $date->getOffset()/3600;
+
+ $i = 1;
+ $now = time();
+ $transitions = $tz->getTransitions();
+ $max = count($transitions);
+ while (true){
+ // i make no guarantees about this code after october 2037.
+ // if it gets to that point, fallback to geonames.
+ if ($i >= $max) throw new Exception('date exception');
+ if (!isset($transitions[$i+1]))
+ throw new Exception('date exception');
+
+ if (($now >= $transitions[$i]['ts']) &&
+ ($now < $transitions[$i+1]['ts'])){
+ $dst = $transitions[$i]['isdst'];
+ $gmt_offset = $cur_offset - ($dst? 1 : 0);
+ break;
+ }
+ else $i++;
+ }
}
catch (Exception $e){
- $dst_offset = $tz_data['dstOffset'];
- }
+ $cur_offset = $tz_data['dstOffset'];
+ $gmt_offset = $tz_data['gmtOffset'];
+ $dst = ($gmt_offset != $cur_offset);
- $dst = ($gmt_offset != $dst_offset);
+ if ($dst && (($gmt_offset + 1) != $cur_offset)){
+ // handle reverse dst case
+ $dst = false;
+ $gmt_offset = $cur_offset;
+ }
+ }
+ $method = $method + 0;
/* methods
|| 1 || Egyptian General Authority of Survey ||
|| 2 || University of Islamic Sciences, Karachi (Shaf'i) ||
@@ -116,7 +148,6 @@ class PrayerTimes {
|| 6 || Umm Al-Qurra (Saudi Arabia ||
|| 7 || Fixed Isha Interval (always 90) ||
*/
- $method = 4; // todo: make method and dates options
$prayers = itl_get_prayer_times($long, $lat, $gmt_offset, $method,
date('j'), date('n'), date('Y'), $dst);
return array('type' => 'prayertimes',
View
@@ -12,8 +12,8 @@ document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.
</script>
<script type="text/javascript">
try {
-var pageTracker = _gat._getTracker("<?php echo $gaCode; ?>");
+var pageTracker = _gat._getTracker("<?php echo GA_CODE; ?>");
pageTracker._trackPageview();
} catch(err) {}</script>
</body>
-</html>
+</html>
View
@@ -9,7 +9,8 @@
<div id="calculating"></div>
<div id="prayertimes">
<?php
- if (!is_null($search_results)) include 'views/locsearch.inc';
+if ((isset($search_results)) && (!is_null($search_results)))
+ include 'views/locsearch.inc';
else if (!is_null($data)) include 'views/salatimes.inc';
?>
</div>
@@ -58,4 +59,4 @@
init();
-->
</script>
-<?php include 'views/footer.inc'; ?>
+<?php include 'views/footer.inc'; ?>

0 comments on commit a35e40e

Please sign in to comment.