Skip to content
This repository was archived by the owner on Sep 10, 2021. It is now read-only.

Commit 73b4125

Browse files
committed
ENH: refs #0339. Use new ipinfodb web api with configurable api key
1 parent c56b217 commit 73b4125

File tree

6 files changed

+118
-97
lines changed

6 files changed

+118
-97
lines changed

modules/statistics/configs/module.ini

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
; version of the module
33
version = 1.0.0
44
; full name
5-
fullname= Statistics Module
5+
fullname = Statistics Module
66
; description
7-
description=
7+
description =
88
;Category
99
category = Core
1010

1111
piwik.url = "http://localhost/piwik"
1212
piwik.id = "1"
13-
piwik.apikey =
14-
report = "0"
13+
piwik.apikey =
14+
report = "0"
15+
16+
ipinfodb.apikey =
Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,52 @@
11
<?php
22

3+
/** Statistics module configure controller */
34
class Statistics_ConfigController extends Statistics_AppController
45
{
5-
public $_moduleForms=array('Config');
6-
public $_components=array('Utility', 'Date');
7-
8-
/** index action*/
9-
function indexAction()
6+
public $_moduleForms = array('Config');
7+
public $_components = array('Utility', 'Date');
8+
9+
/** index action*/
10+
function indexAction()
1011
{
11-
if(!$this->logged||!$this->userSession->Dao->getAdmin()==1)
12+
if(!$this->logged || !$this->userSession->Dao->getAdmin() == 1)
1213
{
13-
throw new Zend_Exception("You should be an administrator");
14+
throw new Zend_Exception('You should be an administrator');
1415
}
15-
16-
if(file_exists(BASE_PATH."/core/configs/".$this->moduleName.".local.ini"))
16+
17+
if(file_exists(BASE_PATH.'/core/configs/'.$this->moduleName.'.local.ini'))
1718
{
18-
$applicationConfig = parse_ini_file(BASE_PATH."/core/configs/".$this->moduleName.".local.ini", true);
19+
$applicationConfig = parse_ini_file(BASE_PATH.'/core/configs/'.$this->moduleName.'.local.ini', true);
1920
}
2021
else
2122
{
2223
$applicationConfig = parse_ini_file(BASE_PATH.'/modules/'.$this->moduleName.'/configs/module.ini', true);
2324
}
2425
$configForm = $this->ModuleForm->Config->createConfigForm();
25-
26-
$formArray = $this->getFormAsArray($configForm);
26+
27+
$formArray = $this->getFormAsArray($configForm);
2728
$formArray['piwikurl']->setValue($applicationConfig['global']['piwik.url']);
2829
$formArray['piwikapikey']->setValue($applicationConfig['global']['piwik.apikey']);
2930
$formArray['piwikid']->setValue($applicationConfig['global']['piwik.id']);
31+
$formArray['ipinfodbapikey']->setValue($applicationConfig['global']['ipinfodb.apikey']);
3032
$formArray['report']->setValue($applicationConfig['global']['report']);
31-
33+
3234
$this->view->configForm = $formArray;
33-
35+
3436
if($this->_request->isPost())
3537
{
3638
$this->_helper->layout->disableLayout();
3739
$this->_helper->viewRenderer->setNoRender();
3840
$submitConfig = $this->_getParam('submitConfig');
3941
if(isset($submitConfig))
4042
{
41-
if(file_exists(BASE_PATH."/core/configs/".$this->moduleName.".local.ini.old"))
43+
if(file_exists(BASE_PATH.'/core/configs/'.$this->moduleName.'.local.ini.old'))
4244
{
43-
unlink(BASE_PATH."/core/configs/".$this->moduleName.".local.ini.old");
45+
unlink(BASE_PATH.'/core/configs/'.$this->moduleName.'.local.ini.old');
4446
}
45-
if(file_exists(BASE_PATH."/core/configs/".$this->moduleName.".local.ini"))
47+
if(file_exists(BASE_PATH.'/core/configs/'.$this->moduleName.'.local.ini'))
4648
{
47-
rename(BASE_PATH."/core/configs/".$this->moduleName.".local.ini",BASE_PATH."/core/configs/".$this->moduleName.".local.ini.old");
49+
rename(BASE_PATH.'/core/configs/'.$this->moduleName.'.local.ini', BASE_PATH.'/core/configs/'.$this->moduleName.'.local.ini.old');
4850
}
4951
$applicationConfig['global']['piwik.url'] = $this->_getParam('piwikurl');
5052
$applicationConfig['global']['report'] = $this->_getParam('report');
@@ -68,8 +70,8 @@ function indexAction()
6870
$job->setTask('TASK_STATISTICS_SEND_REPORT');
6971
$job->setPriority('1');
7072
$job->setRunOnlyOnce(false);
71-
$job->setFireTime(date( 'Y-m-j', strtotime ('+1 day'.date( 'Y-m-j G:i:s')))." 1:00:00");
72-
$job->setTimeInterval(24*60*60);
73+
$job->setFireTime(date('Y-m-j', strtotime ('+1 day'.date( 'Y-m-j G:i:s'))).' 1:00:00');
74+
$job->setTimeInterval(24 * 60 * 60);
7375
$job->setStatus(SCHEDULER_JOB_STATUS_TORUN);
7476
$job->setParams(JsonComponent::encode(array()));
7577
$jobModel->save($job);
@@ -84,10 +86,11 @@ function indexAction()
8486
}
8587
$applicationConfig['global']['piwik.id'] = $this->_getParam('piwikid');
8688
$applicationConfig['global']['piwik.apikey'] = $this->_getParam('piwikapikey');
87-
$this->Component->Utility->createInitFile(BASE_PATH."/core/configs/".$this->moduleName.".local.ini", $applicationConfig);
88-
echo JsonComponent::encode(array(true, 'Changed saved'));
89+
$applicationConfig['global']['ipinfodb.apikey'] = $this->_getParam('ipinfodbapikey');
90+
$this->Component->Utility->createInitFile(BASE_PATH.'/core/configs/'.$this->moduleName.'.local.ini', $applicationConfig);
91+
echo JsonComponent::encode(array(true, 'Changes saved'));
8992
}
9093
}
91-
}
92-
93-
}//end class
94+
}
95+
96+
}//end class
Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,54 @@
11
<?php
22

33
class Statistics_IndexController extends Statistics_AppController
4-
{
5-
public $_moduleModels=array('Download');
6-
public $_models=array('Errorlog', 'Assetstore');
7-
public $_components = array('Utility');
8-
/** index action*/
9-
function indexAction()
4+
{
5+
public $_moduleModels=array('Download');
6+
public $_models=array('Errorlog', 'Assetstore');
7+
public $_components = array('Utility');
8+
9+
/** index action*/
10+
function indexAction()
1011
{
1112
if(!$this->logged||!$this->userSession->Dao->getAdmin()==1)
1213
{
1314
throw new Zend_Exception("You should be an administrator");
1415
}
15-
16+
1617
$assetstores = $this->Assetstore->getAll();
1718
$defaultSet = false;
1819
foreach($assetstores as $key => $assetstore)
1920
{
20-
2121
// Check if we can access the path
2222
if(file_exists($assetstore->getPath()))
23-
{
23+
{
2424
$assetstores[$key]->totalSpace = disk_total_space($assetstore->getPath());
2525
$assetstores[$key]->usedSpace = disk_total_space($assetstore->getPath()) - disk_free_space($assetstore->getPath());
2626
$assetstores[$key]->freeSpace = disk_free_space($assetstore->getPath());
2727
$assetstores[$key]->usedSpaceText = round(($assetstores[$key]->usedSpace / $assetstores[$key]->totalSpace)*100, 2) ;
2828
$assetstores[$key]->freeSpaceText = round((disk_free_space($assetstore->getPath()) / $assetstores[$key]->totalSpace)*100, 2) ;
29-
}
30-
else
29+
}
30+
else
3131
{
3232
$assetstores[$key]->totalSpaceText = false;
3333
}
3434
}
35-
35+
3636
$jqplotAssetstoreArray = array();
3737
foreach($assetstores as $assetstore)
3838
{
39-
$jqplotAssetstoreArray[] = array($assetstore->getName().', '.$assetstore->getPath(),
40-
array(
41-
array('Free Space: '.$this->Component->Utility->formatSize($assetstore->freeSpace), $assetstore->freeSpaceText),
42-
array('Used Space: '.$this->Component->Utility->formatSize($assetstore->usedSpace), $assetstore->usedSpaceText)
43-
)
44-
);
39+
$jqplotAssetstoreArray[] =
40+
array($assetstore->getName().', '.$assetstore->getPath(),
41+
array(
42+
array('Free Space: '.$this->Component->Utility->formatSize($assetstore->freeSpace), $assetstore->freeSpaceText),
43+
array('Used Space: '.$this->Component->Utility->formatSize($assetstore->usedSpace), $assetstore->usedSpaceText)
44+
)
45+
);
4546
}
46-
47-
$errors = $this->Errorlog->getLog(date( 'c', strtotime ('-20 day'.date( 'Y-m-j G:i:s'))), date('c'), 'all', 2);
47+
48+
$errors = $this->Errorlog->getLog(date( 'c', strtotime ('-20 day'.date( 'Y-m-j G:i:s'))), date('c'), 'all', 2);
4849
$arrayErrors = array();
4950

50-
$format = 'Y-m-j';
51+
$format = 'Y-m-j';
5152
for($i = 0; $i<21; $i++)
5253
{
5354
$key = date($format, strtotime(date( 'c', strtotime ('-'.$i.' day'.date( 'Y-m-j G:i:s')))));
@@ -58,19 +59,19 @@ function indexAction()
5859
$key = date($format, strtotime($error->getDatetime()));
5960
$arrayErrors[$key]++;
6061
}
61-
62+
6263
$jqplotArray = array();
6364
foreach($arrayErrors as $key => $value)
6465
{
6566
$jqplotArray[] = array($key.' 8:00AM', $value);
6667
}
67-
68+
6869
$this->view->json['stats']['errors'] = $jqplotArray;
6970
$this->view->json['stats']['assetstores'] = $jqplotAssetstoreArray;
7071
$modulesConfig = Zend_Registry::get('configsModules');
7172
$this->view->piwikUrl = $modulesConfig['statistics']->piwik->url;
7273
$this->view->piwikId = $modulesConfig['statistics']->piwik->id;
7374
$this->view->piwikKey = $modulesConfig['statistics']->piwik->apikey;
74-
}
75-
76-
}//end class
75+
}
76+
77+
}//end class
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
<?php
22
class Statistics_ConfigForm extends AppForm
33
{
4-
4+
55
/** create form */
66
public function createConfigForm()
77
{
88
$form = new Zend_Form;
99

1010
$form->setAction($this->webroot.'/statistics/config/index')
11-
->setMethod('post');
12-
11+
->setMethod('post');
12+
1313
$piwik = new Zend_Form_Element_Text('piwikurl');
1414
$piwikid = new Zend_Form_Element_Text('piwikid');
1515
$piwikapikey = new Zend_Form_Element_Text('piwikapikey');
16+
$ipinfodbapikey = new Zend_Form_Element_Text('ipinfodbapikey');
1617
$report = new Zend_Form_Element_Checkbox('report');
17-
18+
1819
$submit = new Zend_Form_Element_Submit('submitConfig');
19-
$submit ->setLabel('Save configuration');
20-
21-
$form->addElements(array($report, $piwikapikey, $piwik, $piwikid, $submit));
20+
$submit->setLabel('Save configuration');
21+
22+
$form->addElements(array($report, $piwikapikey, $piwik, $piwikid, $ipinfodbapikey, $submit));
2223
return $form;
2324
}
24-
25+
2526
} // end class
2627
?>

modules/statistics/models/base/DownloadBase.php

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
PURPOSE. See the above copyright notices for more information.
1111
=========================================================================*/
1212

13+
/** Download model base */
1314
class Statistics_DownloadModelBase extends Statistics_AppModel
1415
{
16+
1517
public function __construct()
1618
{
1719
parent::__construct();
@@ -33,12 +35,12 @@ public function __construct()
3335
} // end __construct()
3436

3537

36-
/** add a download */
38+
/** add a download record for the given item */
3739
public function addDownload($item, $user)
3840
{
3941
if(!$item instanceof ItemDao)
4042
{
41-
throw new Zend_Exception("Error Params");
43+
throw new Zend_Exception('Error: item parameter is not an item dao');
4244
}
4345
$this->loadDaoClass('DownloadDao', 'statistics');
4446
$download = new Statistics_DownloadDao();
@@ -48,14 +50,14 @@ public function addDownload($item, $user)
4850
$download->setUserId($user->getKey());
4951
}
5052
$ip = $_SERVER['REMOTE_ADDR'];
51-
if( isset ( $_SERVER['HTTP_X_FORWARDED_FOR']))
53+
if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
5254
{
5355
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
5456
}
5557
$download->setIp($ip);
5658
$download->setDate(date('c'));
57-
58-
$position = $this->_getGeolocation( $ip);
59+
60+
$position = $this->_getGeolocation($ip);
5961
$longitude = $position['longitude'];
6062
if($longitude != '')
6163
{
@@ -66,36 +68,42 @@ public function addDownload($item, $user)
6668
{
6769
$download->setLatitude($latitude);
6870
}
69-
71+
7072
$this->save($download);
7173
}
72-
73-
74-
/** Get the geolocation from IP address */
75-
private function _getGeolocation( $ip)
74+
75+
/** Get the geolocation from IP address */
76+
private function _getGeolocation($ip)
7677
{
77-
if(function_exists( "curl_init") == FALSE)
78+
if(function_exists('curl_init') == false)
7879
{
79-
$location['latitude'] = "";
80-
$location['longitude'] = "";
80+
$location['latitude'] = '';
81+
$location['longitude'] = '';
8182
return $location;
8283
}
83-
84+
85+
$applicationConfig = parse_ini_file(BASE_PATH.'/core/configs/'.$this->moduleName.'.local.ini', true);
86+
$apiKey = $applicationConfig['global']['ipinfodb.apikey'];
87+
$url = "http://api.ipinfodb.com/v3/ip-city/?key=$apiKey&ip=$ip&format=json";
88+
8489
$curl = curl_init();
85-
// This url should not be hardcoded. It should be in a config file TODO FIXME HACK
86-
curl_setopt( $curl, CURLOPT_URL, "http://www.ipinfodb.com/ip_query.php?ip=$ip&output=xml");
87-
ob_start();
88-
curl_exec( $curl);
89-
$d = ob_get_contents();
90-
ob_end_clean();
91-
$result=array('latitude'=>'','longitude'=>'');
92-
if (!$d||empty( $d)) return $result; // Failed to open connection
93-
$answer = new SimpleXMLElement( $d);
94-
if ( $answer->Status != 'OK') return $result; // Invalid status code
95-
96-
$result['latitude']=$latitude = $answer->Latitude;
97-
$result['longitude']=$latitude = $answer->Longitude;
90+
curl_setopt($curl, CURLOPT_URL, $url);
91+
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
92+
$resp = curl_exec($curl);
93+
94+
$result = array('latitude' => '', 'longitude' => '');
95+
if(!$resp || empty($resp))
96+
{
97+
return $result; // Failed to open connection
98+
}
99+
$answer = json_decode($resp);
100+
if($answer->statusCode == 'OK')
101+
{
102+
$result['latitude'] = $answer->latitude;
103+
$result['longitude'] = $answer->longitude;
104+
}
98105
return $result;
99106
}
100-
} // end class AssetstoreModelBase
101-
?>
107+
108+
} // end class
109+
?>

0 commit comments

Comments
 (0)