forked from JoomShaper/SP-Weather
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.php
executable file
·500 lines (429 loc) · 15.9 KB
/
helper.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
<?php
/*------------------------------------------------------------------------
# mod_sp_weather - Weather Module by JoomShaper.com
# ------------------------------------------------------------------------
# author JoomShaper http://www.joomshaper.com
# Copyright (C) 2010 - 2014 JoomShaper.com. All Rights Reserved.
# @license - http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
# Websites: http://www.joomshaper.com
-------------------------------------------------------------------------*/
// no direct access
defined('_JEXEC') or die('Restricted access');
class modSPWeatherHelper
{
private $data = array();
private $forecast = array();
private $woeid;
private $location;
private $params;
private $moduleID;
private $moduledir;
private $nightIDs = array(27,29,31,33);
private $iconURL = 'http://l.yimg.com/os/mit/media/m/weather/images/icons/l/%d%s-100567.png';
/**
* Init Class Params
*
* @param object $params
* @param int $id
*/
public function __construct($params, $id)
{
jimport('joomla.filesystem.file');
jimport('joomla.filesystem.folder');
$this->params = $params;
$this->moduleID = $id;
$this->moduledir = basename(dirname(__FILE__));
$this->getWoeId();
$this->data = $this->_getWeatherData();
$this->forecast = $this->_getForecastData();
}
/**
* Error Container array
*
* @var array
*/
private $errors = array();
/**
* Get Errors, If index is null errors stored as numeric array.
*
* @param int | string $index default is NULL
* @return mixed
*/
public function error($index=null)
{
if( !empty($this->errors) )
{
if( is_null($index) ) return $this->errors;
else
{
if( is_null($this->errors[$index]) ) return false;
else return $this->errors[$index];
}
}
else return false;
}
/**
* Set errors in error variable. If index is null errors stored as numeric array.
*
* @param mixed $msg
* @param mixed $index default is null.
*/
public function setError($msg, $index=null)
{
if( is_null($index) ) $this->errors[] = $msg;
else $this->errors[$index] = $msg;
}
/**
* PHP CURL function
*
* @param string $url
* @param array $query default is array
* @return string
*/
private function getCurl($url, $query=array())
{
$requestURL = $url;
if( !empty($query) and is_array($query) ) $requestURL .= '?'. http_build_query($query,'','&');
if (function_exists('curl_init'))
{
// initializing connection
$curl = curl_init();
// saves us before putting directly results of request
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
// url to get
curl_setopt($curl, CURLOPT_URL, $requestURL );
// timeout in seconds
curl_setopt($curl, CURLOPT_TIMEOUT, 60);
// set useragent
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
if( strtolower(substr( $requestURL , 0, 5))==='https' )
{
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
}
// execute curl
$data = curl_exec($curl);
// closing connection
$error = trim(curl_error($curl));
curl_close($curl);
if( !empty($error) ) $this->setError('"'.$error.'" in module "'.$this->moduledir.'"');
return $data;
}
else
{
$this->setError('cURL extension is not available on your server. in module "'.$this->moduledir.'"');
}
}
/**
* Simple caching function
* @version 1.3
* @param string $file
* @param string | array $datafn e.g: functionname | array( object, function) ,
* @param array $datafnarg default is array e.g: array( arg1, arg2, ...) ,
* @param mixed $time default is 900 = 15 min
* @param mixed $onerror string function or array(object, method )
* @return string
*/
private function Cache( $file, $datafn, $datafnarg=array(), $time=900, $onerror='')
{
if (is_writable(JPATH_CACHE))
{
// check cache dir or create cache dir
if (!JFolder::exists(JPATH_CACHE.'/'.$this->moduledir))
{
JFolder::create(JPATH_CACHE.'/'.$this->moduledir.'/');
}
$cache_file = JPATH_CACHE.'/'.$this->moduledir.'/'.$this->moduleID.'-'.$file;
// check cache file, if not then write cache file
if ( !JFile::exists($cache_file) )
{
$data = call_user_func_array($datafn, $datafnarg);
JFile::write($cache_file, $data);
}
// if cache file expires, then write cache
elseif ( filesize($cache_file) == 0 || ((filemtime($cache_file) + (int) $time ) < time()) )
{
$data = call_user_func_array($datafn, $datafnarg);
JFile::write($cache_file, $data);
}
// read cache file
$data = JFile::read($cache_file);
$params['file'] = $cache_file;
$params['data'] = $data;
if( !empty($onerror) ) call_user_func($onerror, $params);
return $data;
} else {
return call_user_func_array($datafn, $datafnarg);
}
}
private function onDataError($params)
{
$data = json_decode($params['data'],true);
if( isset($data['code']) and $data['code']==500 )
{
JFile::Delete($params['file']);
$this->setError('Cannot retrive data in module "'. $this->moduledir.'".');
}
}
private function onForecastError($params)
{
$data = json_decode($params['data'],true);
if( empty($data['query']['results']['item']['forecast']) )
{
JFile::Delete($params['file']);
$this->setError('Cannot retrive forecast data in module "'. $this->moduledir.'".');
}
}
private function onWoeIdError($params)
{
$data = json_decode($params['data'],true);
if( is_null( $data['query']['results'] ) ){
JFile::Delete($params['file']);
$this->setError( 'Cannot get "'.$this->params->get('location').'" woeid in module "'. $this->moduledir.'".' );
}
}
private function onLocationError($params)
{
$data = json_decode($params['data'],true);
if( is_null( $data['query']['results'] ) ){
JFile::Delete($params['file']);
$this->setError( 'Cannot get "'.$this->params->get('location').'" location id in module "'. $this->moduledir.'".' );
}
}
private function makeYQL($query, $param=array('format'=>'json'))
{
$url = 'http://query.yahooapis.com/v1/public/yql?q=';
$url2 = rawurlencode($query);
$url .= str_replace('%2A','*', $url2 );
$url .= '&'.http_build_query($param,'','&');
return $url;
}
/**
* Get Location woe ID
*
*/
private function getWoeId()
{
$query = "select woeid from geo.places(1) where text='".$this->params->get('location')."'";
$URL = $this->makeYQL($query);
if( $this->params->get('useCache')==='1' )
{
$data = $this->Cache(
'woeid.json',
array($this,'getCurl'),
array($URL),
(60*60*60),
array($this,'onWoeIdError')
);
} else {
$data = $this->getCurl($URL);
}
$data = json_decode($data,true);
$this->woeid = $data['query']['results']['place']['woeid'];
}
/**
* Get place Location
*
*/
private function getLocation()
{
$query = 'select id from xml where url="http://xoap.weather.com/search/search?where='.$this->params->get('location').'" and itemPath="search.loc" limit 1';
$URL = $this->makeYQL($query);
if( $this->params->get('useCache')==='1' )
{
$data = $this->Cache(
'location.json',
array($this,'getCurl'),
array($URL),
(60*60*60),
array($this,'onLocationError')
);
} else {
$data = $this->getCurl($URL);
}
$data = json_decode($data,true);
$this->location = $data['query']['results']['loc']['id'];
}
/**
* Get Weather data
*
*/
private function _getWeatherData()
{
$query = 'select * from xml where url="http://weather.yahooapis.com/forecastrss?w='.$this->woeid.'&u='.$this->params->get('tempUnit').'"';
$URL = $this->makeYQL($query);
if( $this->params->get('useCache')==='1' )
{
$data = $this->Cache(
'weather.json',
array($this,'getCurl'),
array($URL),
(int) $this->params->get('cacheTime'),
array($this,'onDataError')
);
} else {
$data = $this->getCurl($URL);
}
return json_decode($data,true);
}
/**
* Get Weather data
*
*/
private function _getForecastData()
{
$data = $this->_getWeatherData();
$location = explode('_',$data['query']['results']['rss']['channel']['item']['guid']['content']);
$this->location = $location[0];
$query = 'SELECT * FROM rss WHERE url="http://xml.weather.yahoo.com/forecastrss/'.$this->location.'&d='.$this->params->get('forecast').'_'.strtolower($this->params->get('tempUnit')).'.xml"';
$URL = $this->makeYQL($query);
if( $this->params->get('useCache')==='1' )
{
$data = $this->Cache(
'forecast.json',
array($this,'getCurl'),
array($URL),
(int) $this->params->get('cacheTime'),
array($this,'onForecastError')
);
} else {
$data = $this->getCurl($URL);
}
$data = json_decode($data,true);
return $data['query']['results']['item']['forecast'];
}
/**
* Convert numeric number to language
*
* @param int | string $number
* @return language formatted text
*/
public function Numeric2Lang($number, $prefix = 'SP_')
{
$number = (array) str_split($number);
$formated = '';
foreach($number as $no)
{
if (ctype_digit($no)) {
$formated.=JText::_($prefix . $no);
} else $formated.=$no;
}
return $formated;
}
/**
* Weather condition text converter
*
* @param string $text
* @return string
*/
public function txt2lng($text)
{
$trans = array(" " => "_", "/" => "_", "(" => "", ')'=>'');
//return $text;
$text = strtr($text, $trans);
return JText::_('SP_WEATHER_'.strtoupper($text));
}
/**
* Convert temparature
*
* @param mixed $value
* @param mixed $unit
* @param mixed $tempType
*/
public function convertUnit($value, $unit)
{
$txt = $this->Numeric2Lang($value);
$txt .= ( strtolower($unit)=='c') ? JText::_('SP_WEATHER_'. 'C') : JText::_('SP_WEATHER_'. 'F');
return $txt;
}
/**
* weather condition to icon file name
*
* @param mixed $icon
* @param mixed $path
*/
public function icon($condition)
{
$condition = (int) $condition;
$at = in_array($condition, $this->nightIDs, true)?'n':'d';
$icon = sprintf($this->iconURL,$condition,$at);
return $icon;
}
/**
* weather condition to icon font
*
* @param mixed $icon
* @param mixed $path
*/
public function iconFont($condition) {
$night = in_array($condition, $this->nightIDs, true)?'-night':'';
$fontIcon = array(
"0" => 'other',
"1" => 'storm',
"2" => 'storm',
"3" => 'chance-of-storm',
"4" => 'thunderstorm',
"5" => 'rain-and-snow',
"6" => 'sleet',
"7" => 'sleet',
"8" => 'rain',
"9" => 'rain',
"10" => 'rain',
"11" => 'rain',
"12" => 'rain',
"13" => 'chance-of-snow',
"14" => 'snow',
"15" => 'snow',
"16" => 'snow',
"17" => 'chance-of-storm',
"18" => 'rain',
"19" => 'dusty',
"20" => 'foggy',
"21" => 'hazy',
"22" => 'smoke',
"23" => 'cloudy',
"24" => 'cloudy',
"25" => 'snow',
"26" => 'cloudy',
"27" => 'mostly-cloudy',
"28" => 'mostly-cloudy',
"29" => 'partly-cloudy',
"30" => 'partly-cloudy',
"31" => 'sunny',
"32" => 'sunny',
"33" => 'sunny',
"34" => 'partly-cloudy',
"35" => 'thunderstorm',
"36" => 'sunny',
"37" => 'thunderstorm',
"38" => 'chance-of-storm',
"39" => 'chance-of-storm',
"40" => 'rain',
"41" => 'snow',
"42" => 'snow',
"43" => 'snow',
"44" => 'partly-cloudy',
"45" => 'chance-of-storm',
"46" => 'chance-of-snow',
"47" => 'chance-of-storm',
"3200" => 'other'
);
return $fontIcon[$condition] . $night;
}
/**
* Run function to load data from source
* @return string
*/
public function getData()
{
return $this->data;
}
/**
* Run function to load data from source
* @return string
*/
public function getForecastData()
{
return $this->forecast;
}
}