Source for file download_class.php
Documentation is available at download_class.php
* This file contains the download class, which is used for communication
* PHP running on the web and PHP running at the command line.
* This file is part of HRDIODB.
* HRDIODB 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.
* HRDIODB 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 HRDIODB; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
require_once("common_class.php");
* A class used for communication between web based and command line PHP
* The constructor is responsible for setting internal {@link hrdi_db}
* and {@link session} pointers.
* @param hrdi_db $hdb Reference to a hrdi_db instance
* @param session $session Reference to a session instance. It's optional
* since the command line PHP cannot access session data.
* @param user $user Reference to a user_class instance. Optional since the
* command line PHP cannot access session data.
function download (&$hdb, $session= FALSE, $user= FALSE) {
$this->session = $session;
* Prepares the download object instance.
* The prepare method needs to be called before any other methods are called
* in order to set up the {@link download} class instance. The function will
* return FALSE if it can't find the files or the types array.
* @param integer $number The file number
* @param integer $days_per_file Number of days in each file
function prepare($number, $days_per_file) {
// Get the requested days and file types
$days = $this->session->get_var("days");
$types = $this->session->get_var("types");
// Make sure both arrays are valid
// Figure out which file range we're looking at
$start = $number * $days_per_file;
$end = $start + $days_per_file;
// Format the days and types for database insertion
$this->types = implode(",", $types);
// Create an MD5 hash which we'll use as the file name
$this->hash = md5($this->days. " ". $this->types);
$this->file = "{ $this->hash}. zip";
$this->error_file = "{ $this->file}. error";
* Starts file compression.
* This function will start file compression by storing the file information
* in the HRDI_Downloads table and exec()ing the archiving script.
// Get the next download id
$id = $this->hdb->next_id("download");
// Set the data array for database insertion
$data["Days"] = $this->days;
$data["Types"] = $this->types;
$data["File"] = $this->file;
$data["User"] = $this->user->get_user_id();
// Add (insert) the download
$this->hdb->add($data, "HRDI_Downloads");
// Prepare the shell argument
$arg = escapeshellarg($id);
$path = dirname($_SERVER['SCRIPT_FILENAME']);
// Spawn the child process which will create the archive
exec("php $path/archive.php $arg > /dev/null &");
* Checks for an archive creation error.
* This function checks for a file which contains errors from the archive
* script. If there is an error file, it returns TRUE.
* Returns the archive creation error.
* This function returns the contents of the error file that the archive
* process generated, and then deletes the error file.
* @return string The archive creation error.
return "There is no error!";
* Check file compression status.
* This function is called to check if the file requested is already being
function is_compressing() {
* Check if file is available for download.
* This function checks the file system to see if the file that was being
* compressed is ready for downloading.
function is_available() {
* Gets the size of the downloadable file.
* This function gets the size of the file which the user is offered to
* download. It converts the number of bytes into a human readable form.
* @return string The human readable size of the downloadable file.
function get_file_size() {
* Gets the current size of the archive being created.
* This function gets the current size of the archive which is being created
* by the archive script. It returns converts the number of bytes of the
* archive into a human readable form.
* @return string The human readable size of the archive being created.
function get_temp_file_size() {
* Gets the md5 sum of the downloadable file.
* This function computes the md5 sum of the file which the user is offered
* @return string md5 sum of the file.
* Gets the downloadable file path.
* This function gets the path of the file which the user is offered to
* download. This is the web accessible directory where the file resides.
* This function should be used to access and link to the downloadable
* @return string The path to the downloadable file.
function get_download_file_path() {
return "$download_prefix/{ $this->file}";
* Gets the temporary file path.
* This function gets the path to the temporary archive which is being
* created by the archiving script. This is the archive that will continue
* to grow in size, and eventually it will be moved to the download
* directory. This function should be used to access the temporary file.
* @return string The path to the temporary file.
function get_temp_file_path() {
return "$temp_prefix/{ $this->file}";
* Gets the archive file path.
* This function gets the path to the finished archive file. This is the
* location where the temporary file will be moved when it is finished being
* compressed. This function should be used to access the finished archive
* @return string The path to the archive file.
function get_archive_file_path() {
return "$archive_prefix/{ $this->file}";
* Gets the error file path.
* This function gets the path to the archive script error file. This is the
* file that will be created if the archive script generates an error. This
* function should be used to access the error file.
* @return string The path to the error file.
function get_error_file_path() {
return "$temp_prefix/{ $this->error_file}";
* Retrieves file download information.
* This method is called by the command line {@link archive.php} script in
* order to retrieve information about the file that it needs to compress
* and make available for download.
* @param integer $id Download ID
* @return mixed List consisting of the destination file name and an array of files
* that are to be compressed.
function get_download_info($id) {
$data = $this->hdb->get_row($sql);
// Recreate the days and types to get
$days = explode(",", $data["Days"]);
$types = explode(",", $data["Types"]);
// Create the file type SQL fragment
for ($i = 0; $i < count($types); $i++ ) {
$type_sql .= "Type = '{ $types[$i]}'";
if ($i < count($types) - 1) {
// Get the files for each day
foreach ($days as $day) {
$files = array_merge($files, $this->hdb->get_column($sql));
return array($file, $files);
|