From 5d0b494f491fd27f4aa59a3f8108cae15c1b8665 Mon Sep 17 00:00:00 2001 From: sikofitt Date: Sat, 24 Dec 2016 10:04:18 -0800 Subject: [PATCH] Testing stream --- example.php | 85 ++++++- src/Sikofitt/Element/Image.php | 210 ++++++++++++++++++ .../Tor/Collection/ImageCollection.php | 6 +- src/Sikofitt/Tor/Stream/TorStreamWrapper.php | 20 ++ src/Sikofitt/Tor/TorClient.php | 8 +- 5 files changed, 313 insertions(+), 16 deletions(-) create mode 100644 src/Sikofitt/Element/Image.php create mode 100644 src/Sikofitt/Tor/Stream/TorStreamWrapper.php diff --git a/example.php b/example.php index 2b2d096..6990bae 100644 --- a/example.php +++ b/example.php @@ -7,25 +7,88 @@ */ require __DIR__ . '/vendor/autoload.php'; +/* +if(false === is_dir(__DIR__ . '/images')) +{ + mkdir(__DIR__ . '/images'); +} +*/ $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(); -foreach($images as $image) -{ - $result[] = $image->toArray(); +$iterator = $images->getIterator(); +while($iterator->valid()) { + /* + * $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(); +$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); //$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($result); + // dump($image); +//} +//dump($result); +*/ \ No newline at end of file diff --git a/src/Sikofitt/Element/Image.php b/src/Sikofitt/Element/Image.php new file mode 100644 index 0000000..6111f89 --- /dev/null +++ b/src/Sikofitt/Element/Image.php @@ -0,0 +1,210 @@ +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); + } +} \ No newline at end of file diff --git a/src/Sikofitt/Tor/Collection/ImageCollection.php b/src/Sikofitt/Tor/Collection/ImageCollection.php index 1864c95..845c63a 100644 --- a/src/Sikofitt/Tor/Collection/ImageCollection.php +++ b/src/Sikofitt/Tor/Collection/ImageCollection.php @@ -34,7 +34,7 @@ class ImageCollection * @var ArrayCollection * A container for the found images. */ - private $images; + private $images = []; /** * @var Crawler @@ -75,7 +75,6 @@ class ImageCollection $this->client = $client; $this->html = $html; $this->uri = new Uri($uri); - $this->images = new ArrayCollection(); $this->crawler = new Crawler(); } @@ -146,7 +145,8 @@ class ImageCollection $result['src'] = $image->getAttribute('src'); $result['raw'] = $raw; $result['base64'] = base64_encode($raw); - $this->images->set($alt, $result); + $result['html'] = $this->html; + $this->images[$alt] = $result; $this->sleep(); } return $this->images; diff --git a/src/Sikofitt/Tor/Stream/TorStreamWrapper.php b/src/Sikofitt/Tor/Stream/TorStreamWrapper.php new file mode 100644 index 0000000..3b7b7df --- /dev/null +++ b/src/Sikofitt/Tor/Stream/TorStreamWrapper.php @@ -0,0 +1,20 @@ +createHandlerStack(); $this->setClient(); $this->logger->debug('Finished'); + $this->images = new ArrayCollection(); } /** @@ -235,14 +236,17 @@ class TorClient $this->torControl = $torControl; return $this; } - + public function getHtml() + { + return $this->htmlData; + } public function images() { foreach($this->htmlData as $uri => $html) { $image = new ImageCollection($uri, $html, $this->client, 5); - $this->images[] = $image->images(); + $this->images->add($image->images()); } return $this->images;