HRDIODB
[ class tree: HRDIODB ] [ index: HRDIODB ] [ all elements ]

Source for file download_class.php

Documentation is available at download_class.php

  1. <?php
  2. /**
  3.  * File download class..
  4.  * This file contains the download class, which is used for communication
  5.  * PHP running on the web and PHP running at the command line.
  6.  * @package HRDIODB
  7.  */
  8.  
  9. /**
  10.  * This file is part of HRDIODB.
  11.  *
  12.  * HRDIODB is free software; you can redistribute it and/or modify
  13.  * it under the terms of the GNU General Public License as published by
  14.  * the Free Software Foundation; either version 2 of the License, or
  15.  * (at your option) any later version.
  16.  *
  17.  * HRDIODB is distributed in the hope that it will be useful,
  18.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.  * GNU General Public License for more details.
  21.  *
  22.  * You should have received a copy of the GNU General Public License
  23.  * along with HRDIODB; if not, write to the Free Software
  24.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25.  */
  26.  
  27. /** Common class */
  28. require_once("common_class.php");
  29.  
  30. /**
  31.  * Download class.
  32.  * A class used for communication between web based and command line PHP
  33.  * scripts.
  34.  * @package HRDIODB
  35.  */
  36. class download {
  37.   /**
  38.    * Constructor.
  39.    * The constructor is responsible for setting internal {@link hrdi_db}
  40.    * and {@link session} pointers.
  41.    * @param hrdi_db $hdb Reference to a hrdi_db instance
  42.    * @param session $session Reference to a session instance. It's optional
  43.    *  since the command line PHP cannot access session data.
  44.    * @param user $user Reference to a user_class instance. Optional since the
  45.    *  command line PHP cannot access session data.
  46.    */
  47.   function download (&$hdb$session=FALSE$user=FALSE{
  48.     $this->hdb $hdb;
  49.     $this->session $session;
  50.     $this->user $user;
  51.   }
  52.  
  53.   /**
  54.    * Prepares the download object instance.
  55.    * The prepare method needs to be called before any other methods are called
  56.    * in order to set up the {@link download} class instance. The function will
  57.    * return FALSE if it can't find the files or the types array.
  58.    * @param integer $number The file number
  59.    * @param integer $days_per_file Number of days in each file
  60.    * @return TRUE|FALSE
  61.    */
  62.   function prepare($number$days_per_file{
  63.     // Get the requested days and file types
  64.     $days $this->session->get_var("days");
  65.     $types $this->session->get_var("types");
  66.  
  67.     // Make sure both arrays are valid
  68.     if (!is_array($days|| !is_array($types)) {
  69.       return FALSE;
  70.     }
  71.  
  72.     // Figure out which file range we're looking at
  73.     $start $number $days_per_file;
  74.     $end $start $days_per_file;
  75.  
  76.     // Format the days and types for database insertion
  77.     $this->days implode(","array_slice($days$start$end));
  78.     $this->types implode(","$types);
  79.  
  80.     // Create an MD5 hash which we'll use as the file name
  81.     $this->hash md5($this->days." ".$this->types);
  82.  
  83.     // Set the file name
  84.     $this->file "{$this->hash}.zip";
  85.  
  86.     // Set error file name
  87.     $this->error_file "{$this->file}.error";
  88.  
  89.     return TRUE;
  90.   }
  91.  
  92.   /**
  93.    * Starts file compression.
  94.    * This function will start file compression by storing the file information
  95.    * in the HRDI_Downloads table and exec()ing the archiving script.
  96.    */
  97.   function compress() {
  98.     // Get the next download id
  99.     $id = $this->hdb->next_id("download");
  100.  
  101.     // Set the data array for database insertion
  102.     $data["ID"$id;
  103.     $data["Days"$this->days;
  104.     $data["Types"$this->types;
  105.     $data["File"$this->file;
  106.     $data["User"$this->user->get_user_id();
  107.  
  108.     // Add (insert) the download
  109.     $this->hdb->add($data"HRDI_Downloads");
  110.  
  111.     // Prepare the shell argument
  112.     $arg escapeshellarg($id);
  113.  
  114.     // Get the script path
  115.     $path dirname($_SERVER['SCRIPT_FILENAME']);
  116.  
  117.     // Spawn the child process which will create the archive
  118.     exec("php $path/archive.php $arg > /dev/null &");
  119.   }
  120.  
  121.   /**
  122.    * Checks for an archive creation error.
  123.    * This function checks for a file which contains errors from the archive
  124.    * script. If there is an error file, it returns TRUE.
  125.    * @return TRUE|FALSE
  126.    */
  127.   function is_error() {
  128.     return is_file($this->get_error_file_path());
  129.   }
  130.  
  131.   /**
  132.    * Returns the archive creation error.
  133.    * This function returns the contents of the error file that the archive
  134.    * process generated, and then deletes the error file.
  135.    * @return string The archive creation error.
  136.    */
  137.   function get_error() {
  138.     if (!$this->is_error()) {
  139.       return "There is no error!";
  140.     } else {
  141.       $error = file_get_contents($this->get_error_file_path());
  142.       unlink($this->get_error_file_path());
  143.       return $error;
  144.     }
  145.   }
  146.  
  147.   /**
  148.    * Check file compression status.
  149.    * This function is called to check if the file requested is already being
  150.    * compressed.
  151.    * @return TRUE|FALSE
  152.    */
  153.   function is_compressing() {
  154.     return is_file($this->get_temp_file_path());
  155.   }
  156.  
  157.   /**
  158.    * Check if file is available for download.
  159.    * This function checks the file system to see if the file that was being
  160.    * compressed is ready for downloading.
  161.    * @return TRUE|FALSE
  162.    */
  163.   function is_available() {
  164.     return is_file($this->get_archive_file_path());
  165.   }
  166.  
  167.   /**
  168.    * Gets the size of the downloadable file.
  169.    * This function gets the size of the file which the user is offered to
  170.    * download. It converts the number of bytes into a human readable form.
  171.    * @return string The human readable size of the downloadable file.
  172.    */
  173.   function get_file_size() {
  174.     if (is_file($this->get_archive_file_path())) {
  175.       return common::human_size(filesize($this->get_archive_file_path()));
  176.     }
  177.   }
  178.  
  179.   /**
  180.    * Gets the current size of the archive being created.
  181.    * This function gets the current size of the archive which is being created
  182.    * by the archive script. It returns converts the number of bytes of the
  183.    * archive into a human readable form.
  184.    * @return string The human readable size of the archive being created.
  185.    */
  186.   function get_temp_file_size() {
  187.     if (is_file($this->get_temp_file_path())) {
  188.       return common::human_size(filesize($this->get_temp_file_path()));
  189.     }
  190.   }
  191.  
  192.   /**
  193.    * Gets the md5 sum of the downloadable file.
  194.    * This function computes the md5 sum of the file which the user is offered
  195.    * to download.
  196.    * @return string md5 sum of the file.
  197.    */
  198.   function get_md5_sum() {
  199.     if (is_file($this->get_archive_file_path())) {
  200.       return md5_file($this->get_archive_file_path());
  201.     }
  202.   }
  203.  
  204.   /**
  205.    * Gets the downloadable file path.
  206.    * This function gets the path of the file which the user is offered to
  207.    * download. This is the web accessible directory where the file resides.
  208.    * This function should be used to access and link to the downloadable
  209.    * file.
  210.    * @return string The path to the downloadable file.
  211.    */
  212.   function get_download_file_path() {
  213.     global $download_prefix;
  214.     return "$download_prefix/{$this->file}";
  215.   }
  216.  
  217.   /**
  218.    * Gets the temporary file path.
  219.    * This function gets the path to the temporary archive which is being
  220.    * created by the archiving script. This is the archive that will continue
  221.    * to grow in size, and eventually it will be moved to the download
  222.    * directory. This function should be used to access the temporary file.
  223.    * @return string The path to the temporary file.
  224.    */
  225.   function get_temp_file_path() {
  226.     global $temp_prefix;
  227.     return "$temp_prefix/{$this->file}";
  228.   }
  229.  
  230.   /**
  231.    * Gets the archive file path.
  232.    * This function gets the path to the finished archive file. This is the
  233.    * location where the temporary file will be moved when it is finished being
  234.    * compressed. This function should be used to access the finished archive
  235.    * file.
  236.    * @return string The path to the archive file.
  237.    */
  238.   function get_archive_file_path() {
  239.     global $archive_prefix;
  240.     return "$archive_prefix/{$this->file}";
  241.   }
  242.  
  243.   /**
  244.    * Gets the error file path.
  245.    * This function gets the path to the archive script error file. This is the
  246.    * file that will be created if the archive script generates an error. This
  247.    * function should be used to access the error file.
  248.    * @return string The path to the error file.
  249.    */
  250.   function get_error_file_path() {
  251.     global $temp_prefix;
  252.     return "$temp_prefix/{$this->error_file}";
  253.   }
  254.  
  255.   /**
  256.    * Retrieves file download information.
  257.    * This method is called by the command line {@link archive.php} script in
  258.    * order to retrieve information about the file that it needs to compress
  259.    * and make available for download.
  260.    * @param integer $id Download ID
  261.    * @return mixed List consisting of the destination file name and an array of files
  262.    * that are to be compressed.
  263.    */
  264.   function get_download_info($id) {
  265.     $sql = "SELECT *
  266.             FROM HRDI_Downloads
  267.             WHERE ID='$id'";
  268.  
  269.     // Get the download data
  270.     $data = $this->hdb->get_row($sql);
  271.  
  272.     // Recreate the days and types to get
  273.     $days explode(","$data["Days"]);
  274.     $types explode(","$data["Types"]);
  275.  
  276.     // Set the file name
  277.     $file $data["File"];
  278.  
  279.     // Create the file type SQL fragment
  280.     $type_sql '(';
  281.  
  282.     for ($i 0$i count($types)$i++{
  283.       $type_sql .= "Type = '{$types[$i]}'";
  284.  
  285.       if ($i < count($types) - 1) {
  286.         $type_sql .= " OR ";
  287.       }
  288.     }
  289.  
  290.     $type_sql .= ')';
  291.  
  292.     $files = array();
  293.  
  294.     // Get the files for each day
  295.     foreach ($days as $day) {
  296.       $sql = "SELECT Filename
  297.               FROM HRDI_Files
  298.               WHERE UARS_Day = '$day'
  299.               AND $type_sql";
  300.  
  301.       $files = array_merge($files, $this->hdb->get_column($sql));
  302.     }
  303.  
  304.     return array($file, $files);
  305.   }
  306. }

Documentation generated on Tue, 23 Jan 2007 22:57:47 -0500 by phpDocumentor 1.3.0RC6