2010-11-29 22:41:08 +00:00
|
|
|
<?php defined('SYSPATH') or die('No direct access allowed.');
|
|
|
|
|
|
|
|
/**
|
2013-01-11 10:51:37 +00:00
|
|
|
* This class is able to collect traffic information for Exetel hspa
|
2010-11-29 22:41:08 +00:00
|
|
|
*
|
|
|
|
* Traffic information is collected by each service, and cached,
|
|
|
|
* returning just the date in question.
|
|
|
|
*
|
2013-03-19 22:35:19 +00:00
|
|
|
* @package ADSL
|
|
|
|
* @category Service
|
2010-11-29 22:41:08 +00:00
|
|
|
* @author Deon George
|
2013-03-19 22:35:19 +00:00
|
|
|
* @copyright (c) 2009-2013 Open Source Billing
|
2010-11-29 22:41:08 +00:00
|
|
|
* @license http://dev.osbill.net/license.html
|
|
|
|
*/
|
2013-01-11 10:51:37 +00:00
|
|
|
class Service_Traffic_Adsl_Exetelhspa extends Service_Traffic_Adsl {
|
2010-11-29 22:41:08 +00:00
|
|
|
private $login_user_field = 'login_name';
|
|
|
|
private $login_pass_field = 'password';
|
|
|
|
private $date_field = 'date';
|
|
|
|
|
2013-04-05 12:50:08 +00:00
|
|
|
static $result = array();
|
2010-11-29 22:41:08 +00:00
|
|
|
|
|
|
|
/**
|
2013-01-11 10:51:37 +00:00
|
|
|
* Get the data for Exetel hspa services
|
2010-11-29 22:41:08 +00:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getdata($date) {
|
|
|
|
// Assume we have a good fetch, unless otherwise specified.
|
|
|
|
$this->fetchresult = TRUE;
|
|
|
|
|
|
|
|
// If we have already collected the date data, return it.
|
2013-04-05 12:50:08 +00:00
|
|
|
if (! empty(Service_Traffic_Adsl_Exetelhspa::$result[$date]))
|
|
|
|
return Service_Traffic_Adsl_Exetelhspa::$result[$date];
|
2010-11-29 22:41:08 +00:00
|
|
|
|
2011-08-03 14:24:38 +00:00
|
|
|
include_once 'includes/kohana/modules/simplehtmldom/classes/simple_html_dom.php';
|
|
|
|
|
2010-11-29 22:41:08 +00:00
|
|
|
// Find our services that need to be collected this way.
|
|
|
|
$update = array();
|
2011-08-03 14:24:38 +00:00
|
|
|
|
2010-11-29 22:41:08 +00:00
|
|
|
foreach ($this->so->services() as $so) {
|
2012-11-09 23:13:57 +00:00
|
|
|
if (Minion_CLI::options('verbose'))
|
2012-02-22 08:15:46 +00:00
|
|
|
echo " = Service: ".$so->name()."\n";
|
|
|
|
|
2011-09-28 06:46:22 +00:00
|
|
|
if ($so->plugin()->service_stats_collect AND $so->plugin()->service_stats_lastupdate < $date) {
|
2010-11-29 22:41:08 +00:00
|
|
|
// Start Session
|
2011-08-03 14:24:38 +00:00
|
|
|
$request = Request::factory($this->so->stats_url)
|
|
|
|
->method('POST')
|
2011-09-28 06:46:22 +00:00
|
|
|
->post($this->login_user_field,$so->plugin()->service_username)
|
|
|
|
->post($this->login_pass_field,$so->plugin()->service_password)
|
2011-08-03 14:24:38 +00:00
|
|
|
->post('doLogin',1)
|
|
|
|
->post('submit','Login');
|
|
|
|
|
2012-11-09 23:13:57 +00:00
|
|
|
$request->client()->options($this->curlopts+array(
|
2011-09-28 06:46:22 +00:00
|
|
|
CURLOPT_COOKIEJAR=>sprintf('/tmp/usage.cookies.%s.txt',$so->plugin()->service_number),
|
2011-08-03 14:24:38 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
$response = $request->execute();
|
|
|
|
$data = $response->body();
|
2010-11-29 22:41:08 +00:00
|
|
|
|
|
|
|
if (! $data) {
|
|
|
|
// @todo Log into a log file
|
2011-09-28 06:46:22 +00:00
|
|
|
printf('Bad fetch for %s [%s]',$so->plugin()->service_number,$this->so->stats_lastupdate);
|
2010-11-29 22:41:08 +00:00
|
|
|
#$html = new simple_html_dom();
|
|
|
|
#$html->load($data);
|
2011-09-28 06:46:22 +00:00
|
|
|
#$html->save(sprintf('/afs/local/tmp/usage.%s.%s.login.html',$so->plugin()->service_number,'login'));
|
2010-11-29 22:41:08 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for ($servicedate=date('Y-m-d',strtotime($this->so->stats_lastupdate.'+1 day'));
|
|
|
|
$servicedate <= $this->today;
|
|
|
|
$servicedate=date('Y-m-d',strtotime('+1 month',strtotime(date('Y-m',strtotime($servicedate)).'-01')))) {
|
2011-09-28 06:46:22 +00:00
|
|
|
#print_r(array('sn'=>$so->plugin()->service_number,'sd'=>$servicedate));
|
2010-11-29 22:41:08 +00:00
|
|
|
|
|
|
|
$lastday = date('Y-m-d',strtotime('-1 second',strtotime('+1 month',strtotime(date('Y-m',strtotime($servicedate)).'-01'))));
|
|
|
|
if (strtotime($lastday) > time())
|
|
|
|
$lastday = date('Y-m-d',strtotime('yesterday'));
|
|
|
|
|
|
|
|
$html = new simple_html_dom();
|
|
|
|
$notdebug = TRUE;
|
|
|
|
if ($notdebug) {
|
2011-08-03 14:24:38 +00:00
|
|
|
$request = Request::factory($this->so->stats_url.'usage_customize_query.php')
|
|
|
|
->method('POST')
|
|
|
|
->post('year_search_key',date('Y',strtotime($servicedate)))
|
|
|
|
->post('month_search_key',date('m',strtotime($servicedate)))
|
|
|
|
->post('start_day_search_key',date('d',strtotime($servicedate)))
|
|
|
|
->post('end_day_search_key',date('d',strtotime($lastday)))
|
|
|
|
->post('do_usage_search',1);
|
|
|
|
|
2012-11-09 23:13:57 +00:00
|
|
|
$request->client()->options($this->curlopts+array(
|
2011-09-28 06:46:22 +00:00
|
|
|
CURLOPT_COOKIEFILE=>sprintf('/tmp/usage.cookies.%s.txt',$so->plugin()->service_number),
|
|
|
|
));
|
2011-08-03 14:24:38 +00:00
|
|
|
|
|
|
|
$response = $request->execute();
|
|
|
|
$result = $response->body();
|
2010-11-29 22:41:08 +00:00
|
|
|
|
|
|
|
$html->load($result);
|
2011-09-28 06:46:22 +00:00
|
|
|
#$html->save(sprintf('/afs/local/tmp/usage.%s.%s.html',$so->plugin()->service_number,$servicedate)); die();
|
2010-11-29 22:41:08 +00:00
|
|
|
} else {
|
2011-09-28 06:46:22 +00:00
|
|
|
$html->load_file(sprintf('/afs/local/tmp/usage.%s.%s.html',$so->plugin()->service_number,$servicedate));
|
2010-11-29 22:41:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$header = array();
|
|
|
|
$data = array();
|
|
|
|
|
|
|
|
foreach ($html->find('fieldset') as $index => $fieldset) {
|
|
|
|
if (! preg_match('/^Usage Detail/',$fieldset->find('legend',0)->plaintext))
|
|
|
|
continue;
|
|
|
|
|
2011-09-28 06:46:22 +00:00
|
|
|
#echo "X:";print_r($fieldset->find('table',0)->find('tr')->plaintext); echo "\n";
|
2010-11-29 22:41:08 +00:00
|
|
|
foreach ($fieldset->find('table',0)->find('tr') as $key => $values) {
|
|
|
|
foreach ($values->children() as $a => $b) {
|
|
|
|
#print_r(array('a'=>$a,'b'=>$b));
|
|
|
|
|
|
|
|
# Header
|
|
|
|
if ($key == 0) {
|
|
|
|
switch ($b->plaintext) {
|
|
|
|
case 'Date': $header[$a] = 'date'; break;
|
|
|
|
case 'OffPeak Upload': $header[$a] = 'up_offpeak'; break;
|
|
|
|
case 'Peak Upload': $header[$a] = 'up_peak'; break;
|
|
|
|
case 'Upload': $header[$a] = 'up_peak'; break;
|
|
|
|
case 'OffPeak Download': $header[$a] = 'down_offpeak'; break;
|
|
|
|
case 'Peak Download': $header[$a] = 'down_peak'; break;
|
|
|
|
case 'Download': $header[$a] = 'down_peak'; break;
|
|
|
|
case 'Duration': break;
|
2011-09-28 06:46:22 +00:00
|
|
|
case 'Total': break;
|
2010-11-29 22:41:08 +00:00
|
|
|
default:
|
|
|
|
printf('Unkown header :%s',$b->plaintext);
|
|
|
|
$this->fetchresult = FALSE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#echo "INDEX: $key:$a\n";
|
|
|
|
#echo "TAG: ".$b->tag."\n";
|
|
|
|
#echo "HEADER: ".$b->plaintext."\n";
|
|
|
|
|
|
|
|
# Data
|
|
|
|
} else {
|
|
|
|
if (isset($header[$a]))
|
|
|
|
$data[$header[$a]] = $b->plaintext;
|
|
|
|
#echo "INDEX: $key:$a\n";
|
|
|
|
#echo "TAG: ".$b->tag."\n";
|
|
|
|
#echo "VALUES: ".$b->plaintext."\n";
|
|
|
|
}
|
|
|
|
}
|
2011-09-28 06:46:22 +00:00
|
|
|
#print_r($data);
|
2010-11-29 22:41:08 +00:00
|
|
|
|
|
|
|
if (isset($data['date']) && preg_match('/^[0-9]{4}/',$data['date'])) {
|
|
|
|
$sdate = date('Y-m-d',strtotime($data['date']));
|
|
|
|
unset($data['date']);
|
|
|
|
|
2011-09-28 06:46:22 +00:00
|
|
|
if (isset($update[$so->plugin()->service_number][$sdate]))
|
2010-11-29 22:41:08 +00:00
|
|
|
foreach ($data as $key => $value)
|
2011-09-28 06:46:22 +00:00
|
|
|
$update[$so->plugin()->service_number][$sdate][$key] += $value;
|
2010-11-29 22:41:08 +00:00
|
|
|
else
|
2011-09-28 06:46:22 +00:00
|
|
|
$update[$so->plugin()->service_number][$sdate] = $data;
|
2010-11-29 22:41:08 +00:00
|
|
|
|
2011-09-28 06:46:22 +00:00
|
|
|
$update[$so->plugin()->service_number][$sdate]['service'] = $so->plugin()->service_number;
|
|
|
|
$update[$so->plugin()->service_number][$sdate]['date'] = $sdate;
|
2010-11-29 22:41:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-01 12:43:33 +00:00
|
|
|
|
2010-11-29 22:41:08 +00:00
|
|
|
// If we got here and have data, we had a good fetch, update the stats date
|
2011-09-28 06:46:22 +00:00
|
|
|
$so->plugin()->service_stats_lastupdate = $lastday;
|
|
|
|
$so->plugin()->save();
|
2010-11-29 22:41:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are no updates, return an empty array
|
|
|
|
if (! $update)
|
|
|
|
return array();
|
|
|
|
|
|
|
|
// Reformat the data into date order.
|
|
|
|
foreach ($update as $service => $sdata)
|
|
|
|
foreach ($sdata as $sdate => $details)
|
2013-04-05 12:50:08 +00:00
|
|
|
Service_Traffic_Adsl_Exetelhspa::$result[$sdate][$service] = $details;
|
2010-11-29 22:41:08 +00:00
|
|
|
|
|
|
|
// If the date we want is empty, return an array
|
2013-04-05 12:50:08 +00:00
|
|
|
if (empty(Service_Traffic_Adsl_Exetelhspa::$result[$date]))
|
2010-11-29 22:41:08 +00:00
|
|
|
return array();
|
|
|
|
|
|
|
|
// Return the date we asked for
|
2013-04-05 12:50:08 +00:00
|
|
|
return Service_Traffic_Adsl_Exetelhspa::$result[$date];
|
2010-11-29 22:41:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|