AloWAMP source documentation
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  • Download

Namespaces

  • None
  • PHP
  • Setup
  • VersionSwitch

Classes

  • cURL
  • Downloader
  • Format
  • Handler
  • IO
  • Router
  • Service
  • Settings
  • Setup

Functions

  • _echo
  • get
  1 <?php
  2 
  3    /**
  4     * Downloads stuff
  5     *
  6     * @author Art <a.molcanovas@gmail.com>
  7     */
  8    class Downloader {
  9 
 10       /**
 11        * cURL handler
 12        *
 13        * @var cURL
 14        */
 15       protected $curl;
 16 
 17       /**
 18        * Download destination
 19        *
 20        * @var string
 21        */
 22       protected $dest;
 23 
 24       /**
 25        * Timestamp when we last reported the status
 26        *
 27        * @var int
 28        */
 29       protected $last_report_time;
 30 
 31       /**
 32        * The last reported status
 33        *
 34        * @var string
 35        */
 36       protected $last_report_status;
 37 
 38       /**
 39        * Output
 40        *
 41        * @var resource
 42        */
 43       protected $fp;
 44 
 45       /**
 46        * Instantiates the class
 47        *
 48        * @author Art <a.molcanovas@gmail.com>
 49        *
 50        * @param string $source      Download source
 51        * @param string $destination Download destination
 52        */
 53       function __construct($source, $destination) {
 54          $this->dest = $destination;
 55          $this->curl = new cURL($source);
 56          $this->curl->setProgressFunction([$this, 'progressFunction']);
 57       }
 58 
 59       /**
 60        * The progress function
 61        *
 62        * @author Art <a.molcanovas@gmail.com>
 63        *
 64        * @param resource $resource      Coulsn't find documentation on this one, most likely the curl resource
 65        * @param int      $download_size How much we are downloading
 66        * @param int      $downloaded    How much we have downloaded
 67        * @param int      $upload_size   How much we are uploading
 68        * @param int      $uploaded      How much we have uploaded
 69        */
 70       function progressFunction($resource, $download_size, $downloaded, $upload_size, $uploaded) {
 71          $ed = $size = 0;
 72 
 73          if($download_size > 0 && $downloaded > 0) {
 74             $ed   = $downloaded;
 75             $size = $download_size;
 76          } elseif($upload_size > 0 && $uploaded > 0) {
 77             $ed   = $uploaded;
 78             $size = $upload_size;
 79          }
 80 
 81          if($ed && $size) {
 82             $status = Format::filesize($ed) . '/' . Format::filesize($size) . ' downloaded ['
 83                       . round(($ed / $size) * 100, 3) . ' %]';
 84 
 85             $time = time();
 86             if($status != $this->last_report_status && ($time != $this->last_report_time || $ed == $size)) {
 87                $this->last_report_time   = $time;
 88                $this->last_report_status = $status;
 89                _echo($status);
 90             }
 91          }
 92 
 93          //Unnecessary, but stops the IDE from thinking the variable is unused
 94          unset($resource);
 95       }
 96 
 97       /**
 98        * Starts the download
 99        *
100        * @author Art <a.molcanovas@gmail.com>
101        * @return bool Whther the download was successful (on the cURL side)
102        */
103       function download() {
104          if(file_exists($this->dest)) {
105             unlink($this->dest);
106          }
107 
108          $this->fp = fopen($this->dest, 'w');
109          $this->curl->setopt(CURLOPT_FILE, $this->fp);
110          $this->curl->exec();
111          fclose($this->fp);
112 
113          $errno = $this->curl->errno();
114 
115          if($errno === CURLE_OK) {
116             return true;
117          } else {
118             _echo($this->curl->error());
119 
120             return false;
121          }
122       }
123    }
AloWAMP source documentation API documentation generated byApiGen 2.8.0