Compare with Previous | Blame | View Log
#!/usr/bin/php
<?php
/**
* memberdirect-getstatements.php
*
* A PHP script to batch-download "Electronic Statements" from Canadian
* Credit Union online banking sites that use the MemberDirect
* (http://www.memberdirect.ca/) system.
*
* Requirements:
* - PHP (http://www.php.net) - on a Mac you have this; on a Linux host you probably do
* - cURL (http://curl.haxx.se/) - you likely already have this
* - PHP Simple HTML DOM Parser (http://simplehtmldom.sourceforge.net/index.htm)
*
* =======================================================================
* WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
* -----------------------------------------------------------------------
*
* This script prompts you for your MemberDirect Account Number and
* Personal Access Code (PAC). While this information is *not* stored
* and is sent securly to the MemberDirect server via HTTPS, beware of the
* following vulnerabilities:
*
* 1. If this script is modified by someone else, they could easily
* store your account information, send it to themselves, etc. So
* ensure that the script hasn't been modified, especially if you
* are on a host that others have access to.
*
* 2. It's possible, depending on your setup, that even though the
* private information isn't store on the filesystem, it may be
* available from system memory should others have access to
* your host.
*
* Best practice would be to use this script *only* from a host that
* you control access to.
*
* -----------------------------------------------------------------------
* WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
* =======================================================================
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*
* @version 0.1, 6 July 2009
* @link http://ruk.ca/wiki/Automating_Metro_Credit_Union_Online_Banking
* @author Peter Rukavina <peter@rukavina.net>
* @copyright Reinvented Inc., 2009
* @license http://www.fsf.org/licensing/licenses/gpl.txt GNU Public License
*/
// ---------------------------------------------------
// User-configurable options
// ---------------------------------------------------
$baseurl = "https://www.metrocreditu.com/"; // The base URL of your MemberDirect site
$branch = "907"; // Your Credit Union "Branch Number" (Metro Credit Union in Charlottetown is 907)
// ---------------------------------------------------
// End of user-configurable options
// ---------------------------------------------------
// ---------------------------------------------------
// Get the Account Number from the command line
// (so we don't need to store it in the script).
// ---------------------------------------------------
print "Enter your MemberDirect Account Number: ";
$acctnum = fgets(STDIN);
if (trim($acctnum) == '') {
die("You must enter your Account Number to use this script. It is not stored.");
}
else {
$acctnum = trim($acctnum);
}
// ---------------------------------------------------
// Get the Personal Access Code (PAC) from the
// command line (so we don't need to store it in the
// script itself).
// ---------------------------------------------------
print "Enter your MemberDirect Personal Access Code: ";
$pac = fgets(STDIN);
if (trim($pac) == '') {
die("You must enter your Personal Access Code (PAC) to use this script. It is not stored.");
}
else {
$pac = trim($pac);
}
// Clear the screen (so the private data isn't hanging around).
// This might not work properly in all environments.
echo chr(27), "[H", chr(27), "[2J";
// And away we go...
print "Retrieving statements from MemberDirect. Please wait.\n";
// Requirement: PHP Simple HTML DOM Parser
// Get it from http://simplehtmldom.sourceforge.net/index.htm
// Place it in the same directory as this script
require_once './simple_html_dom.php';
exec("curl -s -c ./tmp/creditunion-cookies.txt -L -X POST -d 'LOGON=LOGON2&action=logon5&fromUsecase=Logon&fromStep=Step1&branch=$branch&acctnum=$acctnum&pac=$pac' $baseurl/Home/OnlineBanking/Accounts/");
exec("curl -s -c ./tmp/creditunion-cookies.txt -b ./tmp/creditunion-cookies.txt -o ./tmp/creditunion-statementindex.html -L $baseurl/Home/OnlineBanking/Accounts/EStatement/?action=goto&fromDownloadWarning=Yes&fromUsecase=ElectronicStatements&fromStep=Step1");
// Grab the main Electronic Statement index, parse the HTML
$html = file_get_html('./tmp/creditunion-statementindex.html');
// Find all the links to other year's statements
foreach($html->find('a[href^=/Home/OnlineBanking/Accounts/EStatement/?action]') as $e) {
$e->href = str_replace("&","&",$e->href);
$otheryears[] = $e->href;
}
// Parse current year's statement index
parseStatementIndex($html);
// Grab previous year's statement indices and parse
foreach($otheryears as $url) {
list($start,$year) = split("selectedYear=",$url);
exec("curl -s -c ./tmp/creditunion-cookies.txt -b ./tmp/creditunion-cookies.txt -o ./tmp/creditunion-statementindex-$year.html -L \"$baseurl/Home/OnlineBanking/Accounts/EStatement/?action=goto&fromDownloadWarning=Yes&fromUsecase=ElectronicStatements&fromStep=Step1&selectedYear=$year\"");
$html = file_get_html("./tmp/creditunion-statementindex-$year.html");
$thisyear = array();
foreach($html->find('a[href^=/MDContent/EStatement?docid=]') as $e) {
$thisyear[] = $e->href;
}
getStatements($thisyear);
}
// Remove all files from the ./tmp directory
exec("rm -f ./tmp/*");
/**
* Parse out links to PDF statements from HTML and retrieve.
* @param object $html PHP Simple HTML DOM Parser object
*/
function parseStatementIndex($html) {
$thisyear = array();
foreach($html->find('a[href^=/MDContent/EStatement?docid=]') as $e) {
$thisyear[] = $e->href;
}
getStatements($thisyear);
}
/**
* Download a batch of Electronic Statement URLs
* @param array $urls the URLs of the PDF files to download
*/
function getStatements($urls) {
global $baseurl;
foreach($urls as $url) {
list($start,$filename) = split("FileName=",$url);
exec("curl -s -c ./tmp/creditunion-cookies.txt -b ./tmp/creditunion-cookies.txt -o ./statements/$filename.pdf -L $baseurl$url");
print "Retrieved $filename.pdf\n";
}
}