<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | PHP version 4.0 |
// +----------------------------------------------------------------------+
// | Copyright (c) 2003 Moritz Heidkamp |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | hide@address.com so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Author: Moritz Heidkamp <hide@address.com> |
// +----------------------------------------------------------------------+
//
// $Id: iviDownloader.class.php, v0.5 2003/08/17 $
//
// Class for retrieving linked files from given URL(s)
// Requires the clsHTTP class by Tiago Serafim (http://www.phpclasses.org/browse.html/package/1119.html).
//
require_once('cHTTP.php');
class iviDownloader
{
var $_http;
var $files = array();
/**
* iviDownloader::iviDownloader()
*
* Constructor.
* Initalizes HTTP client.
*
**/
function iviDownloader()
{
$this->_http =& new clsHTTP();
}
/**
* iviDownloader::get()
*
* Retrieves links of linked files in given URL(s).
* The files found are being stored in $this->files.
*
* @param mixed $urls - URL string or array of URLs that should be searched for files
* @param boolean $clear - whether to clear the last results or add new results to last results (default is true)
* @param string $types - valid extension types to be searched for (seperated by '|' - regular expression format) (default is 'jpe?g' which includes *.jpg and *.jpeg files)
* @return - amount of files found
**/
function get($urls, $clear = true, $types = 'jpe?g')
{
if ($clear) {
$this->clear();
}
if (!is_array($urls)) {
$urls = array($urls);
}
$count = 0;
foreach ($urls as $url) {
$this->_http->getPage($url);
preg_match_all("/href=\"?([^\"' >]+\.($types))/i", $this->_http->getContent(), $files);
if (count($files[1]) > 0 && !in_array($url, array_keys($this->files))) {
$absPath = substr($url, 0, strrpos($url, '/'));
foreach($files[1] as $link) {
if ($link {0} == '/') {
$this->files[$url][] = $absPath . $link;
}
else
if (substr($link, 0, 7) != 'http://') {
$this->files[$url][] = $absPath . '/' . $link;
}
else {
$this->files[$url][] = $link;
}
$count++;
}
}
}
return $count;
}
/**
* iviDownloader::save()
*
* Save files found with iviDownloader::get() to a specified path
* to sub-directories with ascending number (each source URL will get
* one directory). The URL from which the links was taken is used
* as the referer. A file called "info.txt" is created for each
* directory by setting $info to true - it contains some information
* about the download process and source.
*
* @param string $path - save path
* @param boolean $numeric - whether the filenames should be changed to an ascending number or to keep original names (default is false)
* @param boolean $info - whether to create info.txt or not (default is false)
* @return array - array holding new created directory names
**/
function save($path, $numeric = false, $info = false)
{
if (substr($path, -1) != '/') {
$path .= '/';
}
$newDirs = array();
$dirCount = 1;
foreach ($this->files as $url => $files) {
while (is_dir($path . str_pad($dirCount, 4, '0', STR_PAD_LEFT))) {
$dirCount++;
}
$dir = str_pad($dirCount, 4, '0', STR_PAD_LEFT);
mkdir($path . $dir);
$this->_http->setReferer($url);
$fileCount = 1;
$newDirs[] = $dir;
$elapsed = time();
foreach ($files as $file) {
$this->_http->getPage($file);
$fp = fopen($path . $dir . '/' . ($numeric ? str_pad($fileCount, strlen($this->getCount()), '0', STR_PAD_LEFT) . substr($file, -4) : basename($file)), 'w');
fputs($fp, $this->_http->getContent());
fclose($fp);
$fileCount++;
}
$elapsed = time() - $elapsed;
$fp = fopen($path . $dir . '/info.txt', 'w');
fputs($fp, "URL=$url\nCount=" . count($files) . "\nDate=" . date('Y-m-d H:i') . "\nElapsed=" . floor(floor($elapsed / 60) / 60). ':' . floor($elapsed / 60) . ':' . floor($elapsed % 60));
fclose($fp);
}
return $newDirs;
}
/**
* iviDownloader::add()
*
* Manually add a file's URL to current list.
*
* @param mixed $url - URL string or array of URLs that should be added
* @param string $referer - referer needed to download file (optional)
**/
function add($urls, $referer = '')
{
if (!is_array($urls)) {
$urls = array($urls);
}
if (empty($referer)) {
$this->files[] = $urls;
}
else {
if (isset($this->files[$referer])) {
$this->files[$referer] = array_merge($this->files[$referer], $urls);
}
else {
$this->files[$referer] = $url;
}
}
}
/**
* iviDownloader::clear()
*
* Clears the current file list
*
**/
function clear()
{
$this->files = array();
}
/**
* iviDownloader::getCount()
*
* Retrieve total amount of files found
*
* @return int - file count
**/
function getCount()
{
$count = 0;
foreach ($this->files as $files) {
$count += count($files);
}
return $count;
}
}
?>