setOptions ($options); } } function download ($source, $target = null) { $true_source = ($this->hasSourceDir ()) ? $this->getSourceDir () . $source : $source; if (!isset ($target) and $this->hasTargetDir()) { $true_target = $this->getTargetDir(); if ($this->hasSourceDir ()) { $true_target .= $source; } else { $true_target .= basename ($source); } } else { $true_target = $target; } if ($this->getDryRun ()) { echo $true_source, " ", $true_target, "\n"; return true; } try { $true_source_file = new File ($true_source); $true_source_file->open (File::OPEN_MODE_READ); } catch (File_Exception $e) { throw new Downloader_Exception ("Cannot download file $true_source."); } if (empty ($true_target)) { throw new Downloader_Exception ("Target is empty."); } else { $download_if_zero = file_exists ($true_target) && (filesize ($true_target) == 0) && $this->hasOption (self::OPTION_DOWNLOAD_IF_EMPTY); $download_if_exists = $this->hasOption (self::OPTION_FORCE_DOWNLOAD); if (!file_exists ($true_target) or $download_if_exists or $download_if_zero) { $dir = dirname ($true_target); if (! file_exists ($dir)) { if ($this->hasOption (self::OPTION_CREATE_DIRECTORY)) { $saved_umask = umask (0); mkdir ($dir, 0777, true); umask ($saved_umask); } else { throw new Downloader_Exception (sprintf ('Directory %s does not exist.', $dir)); } } $local_file = new File ($true_target); $local_file->open (File::OPEN_MODE_WRITE); $local_file->write ($true_source_file->getContents ()); $local_file->close (); } } } function setOptions () { $arguments = func_get_args (); foreach (new RecursiveIteratorIterator (new RecursiveArrayIterator ($arguments)) as $argument) { $this->options [] = $argument; } } function getOptions () { return $this->options; } function hasOption ($option) { return in_array ($option, $this->getOptions ()); } function setTargetDir ($target_dir) { $this->target_dir = $target_dir; } function getTargetDir () { return $this->target_dir; } function hasTargetDir () { return isset($this->target_dir); } function setSourceDir ($source_dir) { $this->source_dir = $source_dir; } function getSourceDir () { return $this->source_dir; } function hasSourceDir () { return isset($this->source_dir); } function setDryRun ($dry_run) { $this->dry_run = $dry_run; } function getDryRun () { return $this->dry_run; } }