-
Notifications
You must be signed in to change notification settings - Fork 35
/
prayertimes.inc
296 lines (254 loc) · 10.1 KB
/
prayertimes.inc
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
<?php
include_once 'settings.inc';
class PrayerTimes {
public static function getLocationFromCache($link, $q){
$qs = "select * from geocodeCache where query='$q'";
$res = mysqli_query($link, $qs) or null;
$ret = null;
if (($res != null) && ($row = mysqli_fetch_assoc($res))){
$ret['ResultSet'] = array();
$ret['ResultSet']['Result'] =
array('Latitude' => $row['latitude'],
'Longitude' => $row['longitude'],
'addrstr' => $row['address']);
}
return $ret;
}
public static function getLocations($q){
return PrayerTimes::getLocationsFromGoogle($q);
}
public static function getLocationsFromGoogle($q){
$url = "http://maps.googleapis.com/maps/api/geocode/json?" .
"sensor=false&address=";
$q = urlencode($_GET['q']);
if (strlen($q) == 0) return;
$url = $url . $q;
if (preg_match("/^\p{Arabic}/u", $_GET['q']) > 0) {
$url = $url . '&language=ar';
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($ch);
$info = curl_getinfo($ch);
$ret = $info['http_code'];
curl_close($ch);
$out = json_decode($res, true);
if ($out == null) return null;
$locations = array();
$locations['ResultSet'] = array();
$locations['ResultSet']['Result'] = array();
$gres = $out['results'];
foreach ($gres as $res){
$loc = $res['geometry']['location'];
$addr = $res['formatted_address'];
// lat, lng
$ret = array('Latitude' => $loc['lat'],
'Longitude' => $loc['lng'], 'addrstr' => $addr);
$locations['ResultSet']['Result'][] = $ret;
}
if (count($locations['ResultSet']['Result']) == 1)
$locations['ResultSet']['Result'] =
$locations['ResultSet']['Result'][0];
$locations['dataSource'] = 'google';
return $locations;
}
public static function getTimezoneInfoFromCache($link, $geohash){
$q = 'select dst_offset, gmt_offset, timezone from ' .
"timezoneCache where geohash='$geohash'";
$res = mysqli_query($link, $q) or null;
$ret = null;
if (($res != null) & ($row = mysqli_fetch_assoc($res))){
$ret = array();
$ret['dstOffset'] = $row['dst_offset'];
$ret['gmtOffset'] = $row['gmt_offset'];
$ret['timezoneId'] = $row['timezone'];
}
if ($ret != null)
return array('rescode' => 200, 'result' => $ret);
return $ret;
}
public static function saveTimezoneInfoToCache($link, $geohash, $json){
$lat = $json['lat'];
$lng = $json['lng'];
$tz = mysqli_real_escape_string($link, $json['timezoneId']);
$rawOffset = $json['rawOffset'];
$dstOffset = $json['dstOffset'];
$gmtOffset = $json['gmtOffset'];
$q = 'insert into timezoneCache(geohash, latitude, longitude, ' .
'timezone, raw_offset, dst_offset, gmt_offset) values(' .
"'$geohash', $lat, $lng, '$tz', $rawOffset, $dstOffset, " .
"$gmtOffset)";
mysqli_query($link, $q);
}
public static function storeLocationInCache($link, $q, $lat, $long,
$addr, $src){
$addr = mysqli_real_escape_string($link, $addr);
$qs = 'insert into geocodeCache(query, latitude, longitude, ' .
"address, source) values('$q', $lat, $long, '$addr', $src)";
mysqli_query($link, $qs);
}
public static function getTimezone($link, $lat, $long){
require_once('geo.php');
$geohash = GeoHashUtils::geoHashize($lat, $long);
$ret = PrayerTimes::getTimezoneInfoFromCache($link, $geohash);
if ($ret != null) return $ret;
$url = "https://ws.geonames.net/timezoneJSON?lat=$lat&lng=$long";
$url .= "&username=" . USERNAME;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($ch);
$info = curl_getinfo($ch);
$ret = $info['http_code'];
curl_close($ch);
$json = json_decode($res, true);
if (!isset($json['lat'])){
$str = print_r($json, true);
error_log("could not find json info for $lat $long - $ret : $str");
}
else PrayerTimes::saveTimezoneInfoToCache($link, $geohash, $json);
return array('rescode' => $ret, 'result' => $json);
}
public static function getPrayerTimes($q, $method = 4){
$link = mysqli_connect('waqt-db', 'waqt', WAQT_SQL_PASSWORD);
mysqli_select_db($link, 'waqt');
mysqli_query($link, "set names 'utf8'");
$q = mysqli_real_escape_string($link, $q);
$pos = strpos($q, "loc:");
if ($pos!==false){
$q = substr($q, $pos+4);
if (empty($q)) return array('type' => 'error',
'err' => 'invalid query string', 'data' => array());
list($lat, $long) = explode(',', $q);
$addr = "loc:$lat,$long";
}
else {
$stored = true;
$locations = PrayerTimes::getLocationFromCache($link, $q);
if ($locations == null){
$stored = false;
$locations = PrayerTimes::getLocations($q);
if ($locations == false){
$errmsg = 'one of the apis which we depend on is ' .
'currently broken, please try again later.';
return array('type' => 'error',
'msg' => $errmsg, 'data' => array());
}
if (isset($locations['ResultSet']['Result'][1])){
$locs = $locations['ResultSet']['Result'];
$vals = array();
foreach ($locs as $loc)
$vals[] = PrayerTimes::calc_addr($loc);
return array('type' => 'search_results', 'data' => $vals);
}
if (!isset($locations['ResultSet']['Result'])){
mysqli_close($link);
error_log("error getting rs for location: $q");
return array('type' => 'error',
'msg' => 'place not found.', 'data' => array());
}
}
$res = $locations['ResultSet']['Result'];
$lat = $res['Latitude'];
$long = $res['Longitude'];
$addr = PrayerTimes::calc_addr($res);
# 0 is yahoo, 1 is google
$src = isset($locations['dataSource'])? 1 : 0;
if (!$stored)
PrayerTimes::storeLocationInCache($link, $q, $lat, $long, $addr, $src);
}
$tz_arr = PrayerTimes::getTimezone($link, $lat, $long);
if ($tz_arr['rescode']!=200){
mysqli_close($link);
$errmsg = 'the geonames api which we depend on is ' .
'currently broken, please try again later.';
return array('type' => 'error',
'msg' => $errmsg, 'data' => array());
}
$tz_data = $tz_arr['result'];
$timezone_id = $tz_data['timezoneId'];
$res = PrayerTimes::calculateTimeOffsets($timezone_id, $tz_data);
$gmt_offset = $res['gmt_offset'];
$dst = $res['dst'];
$method = $method + 0;
/* methods
|| 1 || Egyptian General Authority of Survey ||
|| 2 || University of Islamic Sciences, Karachi (Shaf'i) ||
|| 3 || University of Islamic Sciences, Karachi (Hanafi) ||
|| 4 || Islamic Society of North America ||
|| 5 || Muslim World League (MWL) ||
|| 6 || Umm Al-Qurra (Saudi Arabia ||
|| 7 || Fixed Isha Interval (always 90) ||
*/
$prayers = itl_get_prayer_times($long, $lat, $gmt_offset, $method,
date('j'), date('n'), date('Y'), $dst);
mysqli_close($link);
return array('type' => 'prayertimes',
'data' => $prayers, 'location' => $addr);
}
public static function calculateTimeOffsets($timezone_id, $tz_data){
$dst = 0;
$cur_offset = 0;
$gmt_offset = 0;
// 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);
$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.
// means we reach the end with no applicable switch rule.
// in this case, use the cur offset and assume no dst.
if (!isset($transitions[$i+1])){
$dst = 0;
$gmt_offset = $cur_offset;
break;
}
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){
$cur_offset = $tz_data['dstOffset'];
$gmt_offset = $tz_data['gmtOffset'];
$dst = ($gmt_offset != $cur_offset);
if ($dst && (($gmt_offset + 1) != $cur_offset)){
// handle reverse dst case
$dst = false;
$gmt_offset = $cur_offset;
}
}
return array('gmt_offset' => $gmt_offset, 'dst' => $dst);
}
public static function calc_addr($res){
if (isset($res['addrstr']))
return $res['addrstr'];
$city = $res['City'];
$state = $res['State'];
$zip = $res['Zip'];
$country = $res['Country'];
$loc = '';
if (!empty($city)) $loc = $city;
if (!empty($state)) $loc .= (empty($loc)? $state : ", $state");
if (!empty($zip)) $loc .= (empty($loc)? $zip : " $zip");
return $loc;
}
}