18 Feb 2014

Custom MWEB ADSL Usage Report / Notification by email

A lot of people are on uncapped ADSL packages these days, including myself. But I still like to keep an eye on my account to watch that I don't get throttled. MWEB is my ADSL supplier of choice and the only way to monitor an MWEB account is to login to their website every time you want to see what's going on which can get tedious.

So I put a very quick php / curl script together that can login to my MWEB account for me and then email me a report. I set the script to run at 6 am, so I have an MWEB ADSL usage report waiting in my mailbox every morning - nice and convenient...

I'm not entirely proud about the quality of the code, it could be improved a lot, but as I said, I did put this together very quickly and it works. I would love to host this as a service for other people to make use of, but I think that MWEB would probably take exception to me 'colleting' other people's ADSL login information and block the ability for anyone to login to mweb.co.za with a script.

Anyway, I've included the code below and I'm sure that most people who have their own hosting to run it from will be able to make sense of it. But all you need to do is supply your own details between lines 3 and 21, setup a cron job at the time that you would like it to run and you should be sorted. You may just need to make a few more tweaks depending on how different your MWEB ADSL package is to mine and the exact output you get on your ADSL usage page on the MWEB My Account Service...

<?php
//Fill in your details between these lines=====================================================
//Your MWEB username / email address
$mwebUsername = 'username@mweb.co.za';

//Your MWEB email / ADSL password
$mwebPassword = 'password';

//The URL that displays in your browser when you click on your ADSL usage in My Account at mweb.co.za
$mwebAdslPage = 'https://myaccount.mweb.co.za/mwebcore/myaccount/modules/services/mwebadsl/uncappedview.jsp';

//The email address that you would like to send the usage report to
$sendMailto = 'your.name@domain.com';

//The Start string of the block of text (on the MWEB ADSL usage page) that you would like to include in your usage email
$startText = 'You have used';

//The end string of the block of text (on the MWEB ADSL usage page) that you would like to include in your usage email
$endText = 'Check Usage';
//Fill in your details between these lines===========================================================

//Edit code below this line at your own 'risk' :) ===================================================

//Parse text block for value near search defined by offset and length / end tag
function parse($block,$search,$Offset,$length,$endtag='')
{

$start = strpos($block,$search) + $Offset;
$end = $start + $length;
$value = substr($block, $start, $end - $start );
if ($endtag != '') {
$value = substr($value,0,strpos($value,$endtag)-(strlen($endtag)));
}
return $value;
}


//setup cUrl
$cookiefile = 'cookie.txt';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (Windows; MSIE 6.0; U; Windows NT 5.1)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);

//login to My account at mweb.co.za
curl_setopt($ch, CURLOPT_POSTFIELDS, 'userIdentifier='.urlencode ($mwebUsername).'&password='.$mwebPassword.'&LoginUser=Sign+In');
curl_setopt($ch, CURLOPT_URL, 'https://myaccount.mweb.co.za/mwebcore/myaccount/login/login.jsp');
curl_exec ($ch);

//load the ADSL usage page
curl_setopt($ch, CURLOPT_URL, $mwebAdslPage);
$result = curl_exec ($ch);
curl_close($ch);

//delete temporary cookiefile data (logout)
if (file_exists($cookiefile))
{
unlink($cookiefile);
}

//Prepare the usage area of the page at $mwebAdslPage for mailing...
$usage = parse ($result, $startText, 0, 10000, $endText);
$usage = preg_replace('/\s+/', ' ', $usage);
$usage = str_replace('</p>', '<br>', $usage);
$usage = str_replace('<p>', '<br>', $usage);
$usage = str_replace('<br> <br>', '<br>', $usage);
$usage = strip_tags($usage, '<br>');
$usage = str_replace('<br>', "\n\r", $usage);

//This displays the resulting usage output if you load the script in your browser
echo $usage;

//email the resulting usage output to $sendMailto address with subject 'ADSL usage report'
mail ($sendMailto, 'ADSL usage report', $usage);
?>