Getting Work Done Through People
Getting People Done Through Work
eac_download.class.php PHP Download Manager
This work is licensed under the Creative Commons GNU-LGPL License.eac_download.class is both a secure wrapper for PEAR HTTP/Download.php and a logger of download activity. It validates download requests to ensure only valid requests are processed and it logs requests, successful downloads and failed downloads to SQLite database files.
Included in the archive is a simple reporting script that shows a summary of files downloaded and the detail of successfull and failed download attempts.
Options passed to the constructor (and default values):
$options['dbpath'] = (isset($_SERVER['DOCUMENT_ETC']))
? $_SERVER['DOCUMENT_ETC']
: $_SERVER['DOCUMENT_ROOT'];
$options['filetypes'] = array();
$options['session'] = true;
$options['referral'] = true;
$options['blacklist'] = true;
$options['includepath'] = true;
dbpath is the directory path were the log files are to be stored. Log file names are 'download_yyyymm.log'.
filetypes is an array of allowable file extensions:
$options['filetypes'] = array('zip','pdf');
session, when true, means that there must be an active session. This prevents direct downloads via outside links. This would most likely block crawlers from downloading as well as anyone with cookies disabled (id sessions are managed via cookies).
referral, when true, means that the HTTP_REFERER variable must contain the local domain name. This would also block direct links and outside links as well as anyone stripping the HTTP_REFERER variable.
blacklist, when true, uses PEAR Net/DNSBL.php to do a DNS lookup of the remote IP address at sbl-xbl.spamhaus.org to determine if the IP is listed as a spam/exploit source.
includepath means to search the PHP include path for the file.
Instantiation
include 'eac_download.class.php';
$dl = new download($options);
try {
$dl->start($file,$name,$content);
} catch (Exception $e) {
// whatever you want to do with the error...
header("HTTP/1.1 403 Forbidden");
die("<h3>403 Forbidden</h3>".$e->getMessage());
}
$file is the file path name to be downloaded. If the file is not found, we'll look in the
document root and, optionally, through the PHP include path.
$name is the optional name to download the file as - i.e. it can be downloaded with a different
name. If not used, the actual file name is used.
$content is an optional buffer containing the actual content of the file - i.e. via file_get_contents.
If used, $file is not needed but one of $file or $name is required to name the file content.
Getting download statistics for the current month:
include 'eac_download.class.php';
$dl = new download($options);
$dl->getLog($key);
$key is either the $file or $name used when downloading a file. getLog() returns an associative
array of the counts for the file.
array(
'dlName' // the file name (path if dlLogType is file, else base name)
'dlLogType' // either 'file' or 'name'
'dlAttempt' // the number of attempted downloads
'dlSuccess' // the number of successful downloads
'dlFailure' // the number of failed downloads
'updated' // the date/time of the last download attempt
);
Getting download statistics for all months:
include 'eac_download.class.php';
$dl = new download($options);
$dl->getAllLogs($key);
$key is either the $file or $name used when downloading a file. getLog() returns an associative
array of the counts for the file.
array(
'dlName' // the file name (path if dlLogType is file, else base name)
'dlLogType' // either 'file' or 'name'
'dlAttempt' // the number of attempted downloads
'dlSuccess' // the number of successful downloads
'dlFailure' // the number of failed downloads
'updated' // the date/time of the last download attempt
);