forked from opencaching/opencaching-pl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeoKretyAPI.php
331 lines (298 loc) · 12.7 KB
/
GeoKretyAPI.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
<?php
/**
* This class contain methods used to communicate with Geokrety, via Geokrety Api
* (http://geokrety.org/api.php)
*
* @author Andrzej Łza Woźniak 2012, 2013
*
*/
class GeoKretyApi
{
private $secid = null;
private $cacheWpt = null;
private $maxID = null;
private $server = null;
private $rtEmailAddress = null;
private $GeoKretyDeveloperEmailAddress = null;
private $operationTypes = array(
'TakeUserGeokrets' => 1,
'TakeGeoKretsInCache' => 2,
'LogGeokrety' => 3,
);
private $connectionTimeout = 16;
function __construct($secid = null, $cacheWpt = null)
{
include 'lib/settings.inc.php';
$this->server = $absolute_server_URI;
$this->secid = $secid;
$this->cacheWpt = $cacheWpt;
$this->rtEmailAddress = $dberrormail;
$this->geoKretyDeveloperEmailAddress = $geoKretyDeveloperEmailAddress;
}
/**
* sends request to geokrety and receive all geokrets in user inventory
*
* @return array contains all geokrets in user inventory
*/
private function TakeUserGeokrets()
{
$url = "http://geokrety.org/export2.php?secid=$this->secid&inventory=1";
$xml = $this->connect($url, $this->operationTypes[__FUNCTION__]);
libxml_use_internal_errors(true);
if ($xml) {
try {
$result = simplexml_load_string($xml);
} catch (Exception $e) {
$this->storeErrorsInDb($this->operationTypes[__FUNCTION__], $url);
// $this->emailOnError($e->getMessage(), $url, $result, 'function: '.__FUNCTION__ .'line # '.__LINE__.' in '.__FILE__);
return false;
}
} else {
$result = false;
}
return $result;
}
/**
* sends request to geokrety and receive all geokrets in specified cache
*
* @return array contains all geokrets in cache
*/
private function TakeGeoKretsInCache()
{
$url = "http://geokrety.org/export2.php?wpt=$this->cacheWpt";
$xml = $this->connect($url, $this->operationTypes[__FUNCTION__]);
if ($xml) {
libxml_use_internal_errors(true);
try {
$result = simplexml_load_string($xml);
} catch (Exception $e) {
$this->storeErrorsInDb($this->operationTypes[__FUNCTION__], $url);
//$this->emailOnError($e->getMessage(), $url, $result, 'function: '.__FUNCTION__ .' line # '.__LINE__.' in '.__FILE__);
return false;
}
} else {
$result = false;
}
return $result;
}
/**
* Make html table-formatted list of user geokrets. ready to display anywhere.
* @return string (html)
*/
public function MakeGeokretList()
{
$krety = $this->TakeUserGeokrets();
$lista = tr('GKApi23') . ': ' . count($krety->geokrety->geokret) . '<br>';
$lista .= '<table>';
foreach ($krety->geokrety->geokret as $kret) {
$lista .= '<tr><td></td><td><a href="http://geokrety.org/konkret.php?id=' . $kret->attributes()->id . '">' . $kret . '</a></td></tr>';
}
$lista .= '</table>';
echo $lista;
}
/**
* generate html-formatted list of all geokrets in user inventory.
* This string is used in logging cache (log.php, log_cache.tpl.php)
*
* @return string (html)
*/
public function MakeGeokretSelector($cachename)
{
$krety = $this->TakeUserGeokrets();
if ($krety === false)
return tr('GKApi28');
$selector = '<table>';
$MaxNr = 0;
$jsclear = 'onclick=this.value="" onblur="formDefault(this)"';
foreach ($krety->geokrety->geokret as $kret) {
$MaxNr++;
$selector .= '<tr>
<td>
<a href="http://geokrety.org/konkret.php?id=' . $kret->attributes()->id . '">' . $kret . '</a>
</td>
<td>
<select id="GeoKretSelector' . $MaxNr . '" name="GeoKretIDAction' . $MaxNr . '[action]" onchange="GkActionMoved(' . $MaxNr . ')"><option value="-1">' . tr('GKApi13') . '</option><option value="0">' . tr('GKApi12') . '</option><option value="5">' . tr('GKApi14') . '</option></select>
<input type="hidden" name="GeoKretIDAction' . $MaxNr . '[nr]" value="' . $kret->attributes()->nr . '"><span id="GKtxt' . $MaxNr . '" style="display: none">' . tr('GKApi25') . ': <input type="text" name="GeoKretIDAction' . $MaxNr . '[tx]" maxlength="80" size="50" value="' . tr('GKApi24') . ' ' . $cachename . '" ' . $jsclear . ' /></span>
<input type="hidden" name="GeoKretIDAction' . $MaxNr . '[id]" value="' . $kret->attributes()->id . '">
<input type="hidden" name="GeoKretIDAction' . $MaxNr . '[nm]" value="' . $kret . '" />
</td>
</tr>';
}
$selector .= '</table>';
$selector .= '<input type="hidden" name=MaxNr value="' . $MaxNr . '">';
$this->maxID = $MaxNr; //value set for use in MakeGeokretInCacheSelector method.
return $selector;
}
public function MakeGeokretInCacheSelector($cachename)
{
$krety = $this->TakeGeoKretsInCache();
if ($krety == false)
return tr('GKApi28');
if (count($krety->geokrety->geokret) == 0)
return tr('GKApi29');
$selector = '<table>';
$MaxNr = $this->maxID;
$jsclear = 'onclick=this.value="" onblur="formDefault(this)"';
foreach ($krety->geokrety->geokret as $kret) {
$MaxNr++;
$selector .= '<tr>
<td>
<a href="http://geokrety.org/konkret.php?id=' . $kret->attributes()->id . '">' . $kret . '</a>
</td>
<td>
<select id="GeoKretSelector' . $MaxNr . '" name="GeoKretIDAction' . $MaxNr . '[action]" onchange="GkActionMoved(' . $MaxNr . ')"><option value="-1">' . tr('GKApi13') . '</option><option value="1">' . tr('GKApi15') . '</option><option value="2">' . tr('GKApi16') . '</option><option value="3">' . tr('GKApi17') . '</option></select>
<span id="GKtxt' . $MaxNr . '" style="display: none"> tracking code: <input type="text" maxlength="6" size="6" name="GeoKretIDAction' . $MaxNr . '[nr]"> ' . tr('GKApi25') . ': <input type="text" name="GeoKretIDAction' . $MaxNr . '[tx]" maxlength="40" size="50" value="' . tr('GKApi26') . ' ' . $cachename . '" ' . $jsclear . ' /></span>
<input type="hidden" name="GeoKretIDAction' . $MaxNr . '[id]" value="' . $kret->attributes()->id . '" />
<input type="hidden" name="GeoKretIDAction' . $MaxNr . '[nm]" value="' . $kret . '" />
</td>
</tr>';
}
$selector .= '</table>';
$selector .= '<input type="hidden" name=MaxNr value="' . $MaxNr . '">';
return $selector;
}
/**
* Function logs Geokret on geokrety.org using GeoKretyApi.
* @author Łza
* @param array $GeokretyArray
* @return boolean
*/
public function LogGeokrety($GeokretyArray, $retry = false)
{
// dataBase::debugOC('#'.__line__.' ', $this->connectionTimeout);
if (!$GeokretyArray) { // array from datbase is epmty
$r['errors'][]['error'] = 'array from datbase is epmty';
$postdata = '';
} else {
$postdata = http_build_query($GeokretyArray);
}
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata,
'timeout' => $this->connectionTimeout,
),
);
$context = stream_context_create($opts);
@$result = file_get_contents('http://geokrety.org/ruchy.php', false, $context);
// print $result;
// print '----------<pre>'; print_r($resultarray); print '</pre>-------------';
if (!$result) {
if (!$retry)
$this->storeErrorsInDb($this->operationTypes[__FUNCTION__], $GeokretyArray);
}
try {
@$resultarray = simplexml_load_string($result);
} catch (Exception $e) {
if (!$retry)
$this->storeErrorsInDb($operationType, $GeokretyArray);
}
if ($resultarray) {
$r = $this->xml2array($resultarray);
} else {
$r['errors'][]['error'] = array('GKApi22' => 1, '$GeokretyArray' => $GeokretyArray);
}
$r['geokretId'] = $GeokretyArray['id'];
$r['geokretName'] = $GeokretyArray['nm'];
return $r;
}
private function emailOnError($error = '', $Tablica = '', $result, $errorLocation = 'Unknown error location')
{
$message = "GeoKretyApi error report: \r\n " . $error . "\n
\r\n location of error: $errorLocation \n
\r\n Array sent to geokrety:\r\n\r\n $Tablica \r\n\r\n geokrety.org returned wrong result (ansfer is not in xml, or there is no ansfer). \r\n
date/time: " . date('Y-m-d H:i:s') . "
GeoKrety.org ansfer (there was no ansfer if empty): \r\n \r\n $result ";
$headers = 'From: GeoKretyAPI on opencaching' . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
// check if working developer server or production.
if ($this->server == 'http://local.opencaching.pl/') {
$rtAddress = array('user@ocpl-devel');
} else {
$rtAddress = array($this->rtEmailAddress, $this->geoKretyDeveloperEmailAddress);
}
foreach ($rtAddress as $email) {
mail($email, 'GeoKretyApi returned error', $message, $headers);
}
}
private function xml2array($xml)
{
$arr = array();
foreach ($xml->children() as $r) {
$t = array();
if (count($r->children()) == 0) {
$arr[$r->getName()] = strval($r);
} else {
$arr[$r->getName()][] = $this->xml2array($r);
}
}
return $arr;
}
private function connect($url, $operationType)
{
$opts = array('http' =>
array(
'header' => 'Content-type: application/x-www-form-urlencoded',
'timeout' => $this->connectionTimeout,
)
);
$context = stream_context_create($opts);
@$response = file_get_contents($url, false, $context);
if ($response) {
$result = $response;
} else {
$this->storeErrorsInDb($operationType, $url);
$result = false;
}
return $result;
}
private function storeErrorsInDb($operationType, $dataSent, $response = null)
{
$db = new dataBase;
$query = "INSERT INTO `GeoKretyAPIerrors`(`dateTime`, `operationType`, `dataSent`, `response`)
VALUES (NOW(),:1,:2,:3)";
$db->multiVariableQuery($query, $operationType, addslashes(serialize($dataSent)), addslashes(serialize($response)));
}
public function setGeokretyTimeout($newTimeout)
{
$this->connectionTimeout = $newTimeout;
}
public static function getErrorsFromDb()
{
$db = new dataBase;
$query = "SELECT * FROM `GeoKretyAPIerrors` WHERE 1";
$db->simpleQuery($query);
return $db->dbResultFetchAll();
}
public static function removeDbRows($rowsString)
{
$db = new dataBase;
$query = "DELETE FROM `GeoKretyAPIerrors` WHERE id in ($rowsString)";
$db->simpleQuery($query);
}
public function mailToRT($errorArray)
{
$message = "GeoKrety Api timeout errors: date/time: " . date('Y-m-d H:i:s') . "\r\n \r\n";
$message .= print_r($errorArray, true);
$headers = 'From: GeoKretyAPI on opencaching.pl <[email protected]>' . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$rtAddress = array(
// $this->rtEmailAddress, // send also debug email to rt
$this->geoKretyDeveloperEmailAddress
);
foreach ($rtAddress as $email) {
$send = mail($email, 'GeoKretyApi errors report ' . date('Y-m-d H:i:s'), $message, $headers);
if (!$send)
return false;
}
return true;
}
public function getOperationTypes()
{
return $this->operationTypes;
}
}
?>