Testing stream

This commit is contained in:
R. Eric Wheeler 2016-12-24 10:04:18 -08:00
parent c876202c20
commit 5d0b494f49
5 changed files with 313 additions and 16 deletions

View File

@ -7,25 +7,88 @@
*/ */
require __DIR__ . '/vendor/autoload.php'; require __DIR__ . '/vendor/autoload.php';
/*
if(false === is_dir(__DIR__ . '/images'))
{
mkdir(__DIR__ . '/images');
}
*/
$client = new \Sikofitt\Tor\TorClient(); $client = new \Sikofitt\Tor\TorClient();
$client->get('http://32b5oz2bbtn6gqj3.onion/index.php/Main_Page');
$resource = \GuzzleHttp\Psr7\StreamWrapper::getResource(new \GuzzleHttp\Psr7\Stream(fopen('http://4sy6ebszykvcv2n6.onion/', 'rb')));
/*$client->get('http://32b5oz2bbtn6gqj3.onion/index.php/Main_Page');
$images = $client->images(); $images = $client->images();
foreach($images as $image) $iterator = $images->getIterator();
{ while($iterator->valid()) {
$result[] = $image->toArray(); /*
* $iterator->current(); is an array of
* $result['fqd'] fully qualified domain uri - example.onion/path/filename.image
* $result['src'] = the image src element, could be fqd or just /path/filename.image
* $result['raw'] = the raw image data
* $result['base64'] = base64 encoded raw data - file_put_contents(base64_decode(base64), file.image);
* $result['html'] = the html of the page the image was retrieved from.
*/
/* foreach($iterator->current() as $key => $value)
{
file_put_contents(__DIR__ . '/images/' .
pathinfo($value['src'], PATHINFO_FILENAME) . '.' .
pathinfo($value['src'], PATHINFO_EXTENSION),
$value['raw']);
}
$iterator->next();
} }
$client->get('http://4sy6ebszykvcv2n6.onion/'); */
/*$client->get('http://4sy6ebszykvcv2n6.onion/');
$images = $client->images(); $images = $client->images();
$html = $client->getHtml();
dump($html);
*/
//dump($resource);
$j = stream_get_contents($resource);
fclose($resource);
dump($j);
/*
$iterator = $images->getIterator();
while($iterator->valid()) {
foreach($iterator->current() as $key => $value)
{
file_put_contents(__DIR__ . '/images/' .
pathinfo($value['src'], PATHINFO_FILENAME) . '.' .
pathinfo($value['src'], PATHINFO_EXTENSION),
$value['raw']);
}
$iterator->next();
}
$testArray = [
'fqd' => null,
'src' => null,
'alt' => null,
'base64' => null,
'html' => null
];
$image = new \Sikofitt\Element\Image('https://www.google.com/file/robots.txt', $testArray);
dump($image);
//$images = $client->images();
//dump($images); //dump($images);
//$client->get('https://de.indymedia.org/index.shtml'); //$client->get('https://de.indymedia.org/index.shtml');
foreach($images as $image) { //foreach($images as $image) {
//$image = new \Doctrine\Common\Collections\ArrayCollection();
$result[] = $image->toArray(); // dump($image);
} //}
dump($result); //dump($result);
*/

View File

@ -0,0 +1,210 @@
<?php
/**
* Created by PhpStorm.
* User: eric
* Date: 12/24/16
* Time: 8:11 AM
*/
namespace Sikofitt\Element;
/*
$result['fqd'] fully qualified domain uri - example.onion/path/filename.image
* $result['src'] = the image src element, could be fqd or just /path/filename.image
* $result['raw'] = the raw image data
* $result['base64'] = base64 encoded raw data - file_put_contents(base64_decode(base64), file.image);
* $result['html'] = the html of the page the image was retrieved from.
*/
use GuzzleHttp\Psr7\Uri;
/**
* Class Image
* @package Sikofitt\Element
*/
class Image extends \SplFileInfo implements \JsonSerializable
{
/**
* @var
*/
private $fileName;
/**
* @var mixed
*/
private $alt;
/**
* @var mixed
*/
private $fqd;
/**
* @var mixed
*/
private $src;
/**
* @var mixed
*/
private $base64;
/**
* @var mixed
*/
private $html;
/**
* @var Uri
*/
private $uri;
/**
* @var mixed
*/
private $pathinfo;
/**
* @var mixed
*/
private $phpUrl;
/**
* Image constructor.
* @param $fileName
* @param array $data
* @throws \InvalidArgumentException
*/
public function __construct($fileName, array $data = array())
{
parent::__construct($fileName);
$keys = [
'fqd',
'src',
'alt',
'base64',
'html'
];
if(false === empty(array_diff($keys, array_keys($data)))) {
throw new \InvalidArgumentException('Missing values in constructor');
}
$this->phpUrl = parse_url($fileName);
$this->uri = new Uri($fileName);
$this->pathinfo = pathinfo($fileName);
$this->fileName = $fileName;
$this->alt = $data['alt'];
$this->fqd = $data['fqd'];
$this->src = $data['src'];
$this->base64 = $data['base64'];
$this->html = $data['html'];
}
/**
* @return Uri
*/
public function getUri()
{
// Images shouldn't have queries... I know they do, but thats stupid.
return $this->uri->withQuery('');
}
/**
* @return null|string
*/
public function getQuery()
{
if(0 === strcmp($this->uri->getQuery(), '')) {
return null;
}
if('' === $this->uri->getQuery())
{
return null;
}
return $this->uri->getQuery();
}
/**
* @return string
*/
public function getScheme()
{
return $this->uri->getScheme();
}
/**
* @return string
*/
public function getHost()
{
return $this->uri->getHost();
}
/**
* @return mixed
*/
public function getAlt()
{
return $this->alt;
}
/**
* @return mixed
*/
public function getFqd()
{
return $this->fqd;
}
/**
* @return mixed
*/
public function getSrc()
{
return $this->src;
}
/**
* @return string
*/
public function getRaw()
{
return base64_decode($this->base64);
}
/**
* @return mixed
*/
public function getBase64()
{
return $this->base64;
}
/**
* @return mixed
*/
public function getHtml()
{
return $this->html;
}
/**
* @return string
*/
public function jsonSerialize()
{
return json_encode($this);
}
/**
* @return string
*/
public function serialize()
{
return serialize($this);
}
/**
* @param $serialized
*/
public function unserialize($serialized)
{
unserialize($serialized);
}
}

View File

@ -34,7 +34,7 @@ class ImageCollection
* @var ArrayCollection * @var ArrayCollection
* A container for the found images. * A container for the found images.
*/ */
private $images; private $images = [];
/** /**
* @var Crawler * @var Crawler
@ -75,7 +75,6 @@ class ImageCollection
$this->client = $client; $this->client = $client;
$this->html = $html; $this->html = $html;
$this->uri = new Uri($uri); $this->uri = new Uri($uri);
$this->images = new ArrayCollection();
$this->crawler = new Crawler(); $this->crawler = new Crawler();
} }
@ -146,7 +145,8 @@ class ImageCollection
$result['src'] = $image->getAttribute('src'); $result['src'] = $image->getAttribute('src');
$result['raw'] = $raw; $result['raw'] = $raw;
$result['base64'] = base64_encode($raw); $result['base64'] = base64_encode($raw);
$this->images->set($alt, $result); $result['html'] = $this->html;
$this->images[$alt] = $result;
$this->sleep(); $this->sleep();
} }
return $this->images; return $this->images;

View File

@ -0,0 +1,20 @@
<?php
/**
* Created by PhpStorm.
* User: eric
* Date: 12/24/16
* Time: 9:37 AM
*/
namespace Sikofitt\Tor\Stream;
use GuzzleHttp\Psr7\StreamDecoratorTrait;
use Psr\Http\Message\StreamInterface;
class TorStreamWrapper implements StreamInterface
{
use StreamDecoratorTrait;
}

View File

@ -75,6 +75,7 @@ class TorClient
$this->createHandlerStack(); $this->createHandlerStack();
$this->setClient(); $this->setClient();
$this->logger->debug('Finished'); $this->logger->debug('Finished');
$this->images = new ArrayCollection();
} }
/** /**
@ -235,14 +236,17 @@ class TorClient
$this->torControl = $torControl; $this->torControl = $torControl;
return $this; return $this;
} }
public function getHtml()
{
return $this->htmlData;
}
public function images() public function images()
{ {
foreach($this->htmlData as $uri => $html) foreach($this->htmlData as $uri => $html)
{ $image = new ImageCollection($uri, $html, $this->client, 5); { $image = new ImageCollection($uri, $html, $this->client, 5);
$this->images[] = $image->images(); $this->images->add($image->images());
} }
return $this->images; return $this->images;