Added ImageDownloader class

This commit is contained in:
R. Eric Wheeler 2016-12-22 12:24:54 -08:00
parent 979b228ec0
commit b02ac1e137
9 changed files with 354 additions and 275 deletions

View File

@ -12,6 +12,7 @@ $finder = PhpCsFixer\Finder::create()
return PhpCsFixer\Config::create()
->setRules([
'@PSR2' => true,
'array_syntax' => array('syntax' => 'long'),
'header_comment' => [
'header' => $header
],

View File

@ -37,10 +37,10 @@ $caps->setCapability(FirefoxDriver::PROFILE, $profile);
$torProcess = TorLauncher::launch($firefoxBinary);
// Wait for Tor to connect
sleep(2);
$this->webDriver = RemoteWebDriver::create('http://localhost:4444/wd/hub', $caps);
$this->webDriver->navigate()->to('http://32b5oz2bbtn6gqj3.onion');
$webDriver = RemoteWebDriver::create('http://localhost:4444/wd/hub', $caps);
$webDriver->navigate()->to('http://32b5oz2bbtn6gqj3.onion');
// Or $this-webDriver->get('http://32b5oz2bbtn6gqj3.onion');
$pageImages = $this->webDriver->findElements(WebDriverBy::tagName('img'));
$pageImages = $webDriver->findElements(WebDriverBy::tagName('img'));
$images = [];
foreach($pageImages as $image)
{

View File

@ -8,11 +8,11 @@
*/
use Sikofitt\WebDriver\FirefoxBinary;
use Sikofitt\WebDriver\ImageDownloader;
use Sikofitt\WebDriver\Remote\DesiredCapabilities;
use Sikofitt\WebDriver\Tor\TorProfile;
use Sikofitt\WebDriver\TorLauncher;
require 'vendor/autoload.php';
/**
@ -21,10 +21,10 @@ require 'vendor/autoload.php';
class SeleniumTorSimpleTests extends PHPUnit_Framework_TestCase
{
protected $webDriver;
protected $urls = [
'http://skunksworkedp2cg.onion/sites.html' => 'A list of onion sites ordered by hostname',
protected $urls = array(
//'http://skunksworkedp2cg.onion/sites.html' => 'A list of onion sites ordered by hostname',
'http://32b5oz2bbtn6gqj3.onion' => 'The Hidden Wiki',
];
);
protected $process;
protected $torProcess;
@ -34,6 +34,7 @@ class SeleniumTorSimpleTests extends PHPUnit_Framework_TestCase
$firefoxBinary = new FirefoxBinary(__DIR__ . '/../tor-browser_en-US/Browser/start-tor-browser');
$caps = DesiredCapabilities::tor($firefoxBinary);
$profile = new TorProfile();
$caps->setCapability('timeout', 3600);
$caps->setCapability(FirefoxDriver::PROFILE, $profile);
@ -46,7 +47,7 @@ class SeleniumTorSimpleTests extends PHPUnit_Framework_TestCase
public function tearDown()
{
$this->webDriver->quit();
//$this->webDriver->quit();
TorLauncher::stop($this->torProcess);
}
@ -57,10 +58,16 @@ class SeleniumTorSimpleTests extends PHPUnit_Framework_TestCase
$this->webDriver->navigate()->to($url);
$elements = $this->webDriver->findElements(WebDriverBy::tagName('img'));
$this->assertEquals($title, $this->webDriver->getTitle());
$images = [];
$images = array();
foreach ($elements as $element) {
$images[] = $element->getAttribute('src');
}
if (isset($images[0])) {
$image = new ImageDownloader();
$image->open($images[0]);
$image->save(__DIR__ . '/../');
$image->save(__DIR__ . '/../', true);
}
file_put_contents(__DIR__ . '/../' . 'data.json', json_encode($images, JSON_PRETTY_PRINT));
}
}

Binary file not shown.

327
composer.lock generated
View File

@ -704,48 +704,6 @@
"description": "A tool to automatically fix PHP code style",
"time": "2016-12-01 06:18:06"
},
{
"name": "myclabs/deep-copy",
"version": "1.5.5",
"source": {
"type": "git",
"url": "https://github.com/myclabs/DeepCopy.git",
"reference": "399c1f9781e222f6eb6cc238796f5200d1b7f108"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/399c1f9781e222f6eb6cc238796f5200d1b7f108",
"reference": "399c1f9781e222f6eb6cc238796f5200d1b7f108",
"shasum": ""
},
"require": {
"php": ">=5.4.0"
},
"require-dev": {
"doctrine/collections": "1.*",
"phpunit/phpunit": "~4.1"
},
"type": "library",
"autoload": {
"psr-4": {
"DeepCopy\\": "src/DeepCopy/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"description": "Create deep copies (clones) of your objects",
"homepage": "https://github.com/myclabs/DeepCopy",
"keywords": [
"clone",
"copy",
"duplicate",
"object",
"object graph"
],
"time": "2016-10-31 17:19:45"
},
{
"name": "phpdocumentor/reflection-common",
"version": "1.0",
@ -957,40 +915,39 @@
},
{
"name": "phpunit/php-code-coverage",
"version": "4.0.4",
"version": "2.2.4",
"source": {
"type": "git",
"url": "https://github.com/sebastianbergmann/php-code-coverage.git",
"reference": "c14196e64a78570034afd0b7a9f3757ba71c2a0a"
"reference": "eabf68b476ac7d0f73793aada060f1c1a9bf8979"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/c14196e64a78570034afd0b7a9f3757ba71c2a0a",
"reference": "c14196e64a78570034afd0b7a9f3757ba71c2a0a",
"url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/eabf68b476ac7d0f73793aada060f1c1a9bf8979",
"reference": "eabf68b476ac7d0f73793aada060f1c1a9bf8979",
"shasum": ""
},
"require": {
"php": "^5.6 || ^7.0",
"php": ">=5.3.3",
"phpunit/php-file-iterator": "~1.3",
"phpunit/php-text-template": "~1.2",
"phpunit/php-token-stream": "^1.4.2",
"sebastian/code-unit-reverse-lookup": "~1.0",
"sebastian/environment": "^1.3.2 || ^2.0",
"sebastian/version": "~1.0|~2.0"
"phpunit/php-token-stream": "~1.3",
"sebastian/environment": "^1.3.2",
"sebastian/version": "~1.0"
},
"require-dev": {
"ext-xdebug": ">=2.1.4",
"phpunit/phpunit": "^5.4"
"phpunit/phpunit": "~4"
},
"suggest": {
"ext-dom": "*",
"ext-xdebug": ">=2.4.0",
"ext-xdebug": ">=2.2.1",
"ext-xmlwriter": "*"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "4.0.x-dev"
"dev-master": "2.2.x-dev"
}
},
"autoload": {
@ -1016,7 +973,7 @@
"testing",
"xunit"
],
"time": "2016-12-20 15:22:42"
"time": "2015-10-06 15:47:00"
},
{
"name": "phpunit/php-file-iterator",
@ -1201,50 +1158,40 @@
},
{
"name": "phpunit/phpunit",
"version": "5.7.4",
"version": "4.8.31",
"source": {
"type": "git",
"url": "https://github.com/sebastianbergmann/phpunit.git",
"reference": "af91da3f2671006ff5d0628023de3b7ac4d1ef09"
"reference": "98b2b39a520766bec663ff5b7ff1b729db9dbfe3"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/af91da3f2671006ff5d0628023de3b7ac4d1ef09",
"reference": "af91da3f2671006ff5d0628023de3b7ac4d1ef09",
"url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/98b2b39a520766bec663ff5b7ff1b729db9dbfe3",
"reference": "98b2b39a520766bec663ff5b7ff1b729db9dbfe3",
"shasum": ""
},
"require": {
"ext-dom": "*",
"ext-json": "*",
"ext-libxml": "*",
"ext-mbstring": "*",
"ext-xml": "*",
"myclabs/deep-copy": "~1.3",
"php": "^5.6 || ^7.0",
"phpspec/prophecy": "^1.6.2",
"phpunit/php-code-coverage": "^4.0.3",
"ext-pcre": "*",
"ext-reflection": "*",
"ext-spl": "*",
"php": ">=5.3.3",
"phpspec/prophecy": "^1.3.1",
"phpunit/php-code-coverage": "~2.1",
"phpunit/php-file-iterator": "~1.4",
"phpunit/php-text-template": "~1.2",
"phpunit/php-timer": "^1.0.6",
"phpunit/phpunit-mock-objects": "^3.2",
"phpunit/phpunit-mock-objects": "~2.3",
"sebastian/comparator": "~1.2.2",
"sebastian/diff": "~1.2",
"sebastian/environment": "^1.3.4 || ^2.0",
"sebastian/exporter": "~2.0",
"sebastian/global-state": "^1.0 || ^2.0",
"sebastian/object-enumerator": "~2.0",
"sebastian/resource-operations": "~1.0",
"sebastian/version": "~1.0|~2.0",
"sebastian/environment": "~1.3",
"sebastian/exporter": "~1.2",
"sebastian/global-state": "~1.0",
"sebastian/version": "~1.0",
"symfony/yaml": "~2.1|~3.0"
},
"conflict": {
"phpdocumentor/reflection-docblock": "3.0.2"
},
"require-dev": {
"ext-pdo": "*"
},
"suggest": {
"ext-xdebug": "*",
"phpunit/php-invoker": "~1.1"
},
"bin": [
@ -1253,7 +1200,7 @@
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "5.7.x-dev"
"dev-master": "4.8.x-dev"
}
},
"autoload": {
@ -1279,33 +1226,30 @@
"testing",
"xunit"
],
"time": "2016-12-13 16:19:44"
"time": "2016-12-09 02:45:31"
},
{
"name": "phpunit/phpunit-mock-objects",
"version": "3.4.3",
"version": "2.3.8",
"source": {
"type": "git",
"url": "https://github.com/sebastianbergmann/phpunit-mock-objects.git",
"reference": "3ab72b65b39b491e0c011e2e09bb2206c2aa8e24"
"reference": "ac8e7a3db35738d56ee9a76e78a4e03d97628983"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/3ab72b65b39b491e0c011e2e09bb2206c2aa8e24",
"reference": "3ab72b65b39b491e0c011e2e09bb2206c2aa8e24",
"url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/ac8e7a3db35738d56ee9a76e78a4e03d97628983",
"reference": "ac8e7a3db35738d56ee9a76e78a4e03d97628983",
"shasum": ""
},
"require": {
"doctrine/instantiator": "^1.0.2",
"php": "^5.6 || ^7.0",
"phpunit/php-text-template": "^1.2",
"sebastian/exporter": "^1.2 || ^2.0"
},
"conflict": {
"phpunit/phpunit": "<5.4.0"
"php": ">=5.3.3",
"phpunit/php-text-template": "~1.2",
"sebastian/exporter": "~1.2"
},
"require-dev": {
"phpunit/phpunit": "^5.4"
"phpunit/phpunit": "~4.4"
},
"suggest": {
"ext-soap": "*"
@ -1313,7 +1257,7 @@
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "3.2.x-dev"
"dev-master": "2.3.x-dev"
}
},
"autoload": {
@ -1338,52 +1282,7 @@
"mock",
"xunit"
],
"time": "2016-12-08 20:27:08"
},
{
"name": "sebastian/code-unit-reverse-lookup",
"version": "1.0.0",
"source": {
"type": "git",
"url": "https://github.com/sebastianbergmann/code-unit-reverse-lookup.git",
"reference": "c36f5e7cfce482fde5bf8d10d41a53591e0198fe"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/code-unit-reverse-lookup/zipball/c36f5e7cfce482fde5bf8d10d41a53591e0198fe",
"reference": "c36f5e7cfce482fde5bf8d10d41a53591e0198fe",
"shasum": ""
},
"require": {
"php": ">=5.6"
},
"require-dev": {
"phpunit/phpunit": "~5"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"autoload": {
"classmap": [
"src/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Sebastian Bergmann",
"email": "sebastian@phpunit.de"
}
],
"description": "Looks up which function or method a line of code belongs to",
"homepage": "https://github.com/sebastianbergmann/code-unit-reverse-lookup/",
"time": "2016-02-13 06:45:14"
"time": "2015-10-02 06:51:40"
},
{
"name": "sebastian/comparator",
@ -1503,28 +1402,28 @@
},
{
"name": "sebastian/environment",
"version": "2.0.0",
"version": "1.3.8",
"source": {
"type": "git",
"url": "https://github.com/sebastianbergmann/environment.git",
"reference": "5795ffe5dc5b02460c3e34222fee8cbe245d8fac"
"reference": "be2c607e43ce4c89ecd60e75c6a85c126e754aea"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/5795ffe5dc5b02460c3e34222fee8cbe245d8fac",
"reference": "5795ffe5dc5b02460c3e34222fee8cbe245d8fac",
"url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/be2c607e43ce4c89ecd60e75c6a85c126e754aea",
"reference": "be2c607e43ce4c89ecd60e75c6a85c126e754aea",
"shasum": ""
},
"require": {
"php": "^5.6 || ^7.0"
"php": "^5.3.3 || ^7.0"
},
"require-dev": {
"phpunit/phpunit": "^5.0"
"phpunit/phpunit": "^4.8 || ^5.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.0.x-dev"
"dev-master": "1.3.x-dev"
}
},
"autoload": {
@ -1549,25 +1448,25 @@
"environment",
"hhvm"
],
"time": "2016-11-26 07:53:53"
"time": "2016-08-18 05:49:44"
},
{
"name": "sebastian/exporter",
"version": "2.0.0",
"version": "1.2.2",
"source": {
"type": "git",
"url": "https://github.com/sebastianbergmann/exporter.git",
"reference": "ce474bdd1a34744d7ac5d6aad3a46d48d9bac4c4"
"reference": "42c4c2eec485ee3e159ec9884f95b431287edde4"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/ce474bdd1a34744d7ac5d6aad3a46d48d9bac4c4",
"reference": "ce474bdd1a34744d7ac5d6aad3a46d48d9bac4c4",
"url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/42c4c2eec485ee3e159ec9884f95b431287edde4",
"reference": "42c4c2eec485ee3e159ec9884f95b431287edde4",
"shasum": ""
},
"require": {
"php": ">=5.3.3",
"sebastian/recursion-context": "~2.0"
"sebastian/recursion-context": "~1.0"
},
"require-dev": {
"ext-mbstring": "*",
@ -1576,7 +1475,7 @@
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.0.x-dev"
"dev-master": "1.3.x-dev"
}
},
"autoload": {
@ -1616,7 +1515,7 @@
"export",
"exporter"
],
"time": "2016-11-19 08:54:04"
"time": "2016-06-17 09:04:28"
},
{
"name": "sebastian/global-state",
@ -1669,64 +1568,18 @@
],
"time": "2015-10-12 03:26:01"
},
{
"name": "sebastian/object-enumerator",
"version": "2.0.0",
"source": {
"type": "git",
"url": "https://github.com/sebastianbergmann/object-enumerator.git",
"reference": "96f8a3f257b69e8128ad74d3a7fd464bcbaa3b35"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/object-enumerator/zipball/96f8a3f257b69e8128ad74d3a7fd464bcbaa3b35",
"reference": "96f8a3f257b69e8128ad74d3a7fd464bcbaa3b35",
"shasum": ""
},
"require": {
"php": ">=5.6",
"sebastian/recursion-context": "~2.0"
},
"require-dev": {
"phpunit/phpunit": "~5"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.0.x-dev"
}
},
"autoload": {
"classmap": [
"src/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Sebastian Bergmann",
"email": "sebastian@phpunit.de"
}
],
"description": "Traverses array structures and object graphs to enumerate all referenced objects",
"homepage": "https://github.com/sebastianbergmann/object-enumerator/",
"time": "2016-11-19 07:35:10"
},
{
"name": "sebastian/recursion-context",
"version": "2.0.0",
"version": "1.0.2",
"source": {
"type": "git",
"url": "https://github.com/sebastianbergmann/recursion-context.git",
"reference": "2c3ba150cbec723aa057506e73a8d33bdb286c9a"
"reference": "913401df809e99e4f47b27cdd781f4a258d58791"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/recursion-context/zipball/2c3ba150cbec723aa057506e73a8d33bdb286c9a",
"reference": "2c3ba150cbec723aa057506e73a8d33bdb286c9a",
"url": "https://api.github.com/repos/sebastianbergmann/recursion-context/zipball/913401df809e99e4f47b27cdd781f4a258d58791",
"reference": "913401df809e99e4f47b27cdd781f4a258d58791",
"shasum": ""
},
"require": {
@ -1738,7 +1591,7 @@
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.0.x-dev"
"dev-master": "1.0.x-dev"
}
},
"autoload": {
@ -1766,73 +1619,23 @@
],
"description": "Provides functionality to recursively process PHP variables",
"homepage": "http://www.github.com/sebastianbergmann/recursion-context",
"time": "2016-11-19 07:33:16"
},
{
"name": "sebastian/resource-operations",
"version": "1.0.0",
"source": {
"type": "git",
"url": "https://github.com/sebastianbergmann/resource-operations.git",
"reference": "ce990bb21759f94aeafd30209e8cfcdfa8bc3f52"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/resource-operations/zipball/ce990bb21759f94aeafd30209e8cfcdfa8bc3f52",
"reference": "ce990bb21759f94aeafd30209e8cfcdfa8bc3f52",
"shasum": ""
},
"require": {
"php": ">=5.6.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"autoload": {
"classmap": [
"src/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Sebastian Bergmann",
"email": "sebastian@phpunit.de"
}
],
"description": "Provides a list of PHP built-in functions that operate on resources",
"homepage": "https://www.github.com/sebastianbergmann/resource-operations",
"time": "2015-07-28 20:34:47"
"time": "2015-11-11 19:50:13"
},
{
"name": "sebastian/version",
"version": "2.0.1",
"version": "1.0.6",
"source": {
"type": "git",
"url": "https://github.com/sebastianbergmann/version.git",
"reference": "99732be0ddb3361e16ad77b68ba41efc8e979019"
"reference": "58b3a85e7999757d6ad81c787a1fbf5ff6c628c6"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/version/zipball/99732be0ddb3361e16ad77b68ba41efc8e979019",
"reference": "99732be0ddb3361e16ad77b68ba41efc8e979019",
"url": "https://api.github.com/repos/sebastianbergmann/version/zipball/58b3a85e7999757d6ad81c787a1fbf5ff6c628c6",
"reference": "58b3a85e7999757d6ad81c787a1fbf5ff6c628c6",
"shasum": ""
},
"require": {
"php": ">=5.6"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.0.x-dev"
}
},
"autoload": {
"classmap": [
"src/"
@ -1851,7 +1654,7 @@
],
"description": "Library that helps with managing the version number of Git-hosted PHP projects",
"homepage": "https://github.com/sebastianbergmann/version",
"time": "2016-10-03 07:35:21"
"time": "2015-06-21 13:59:46"
},
{
"name": "symfony/event-dispatcher",

60
example.php Normal file
View File

@ -0,0 +1,60 @@
<?php
use Sikofitt\WebDriver\FirefoxBinary;
use Sikofitt\WebDriver\ImageDownloader;
use Sikofitt\WebDriver\Remote\DesiredCapabilities;
use Sikofitt\WebDriver\Tor\TorProfile;
use Sikofitt\WebDriver\TorLauncher;
require __DIR__ . '/vendor/autoload.php';
$firefoxBinary = new FirefoxBinary(__DIR__ . '/tor-browser_en-US/Browser/start-tor-browser');
$caps = DesiredCapabilities::tor($firefoxBinary);
$profile = new TorProfile();
$caps->setCapability('timeout', 3600);
$caps->setCapability(FirefoxDriver::PROFILE, $profile);
$torProcess = TorLauncher::launch($firefoxBinary);
// Wait for Tor to connect
$sleepTime = 5;
$torProcess->wait(function() use ($sleepTime) {
sleep($sleepTime);
});
$webDriver = RemoteWebDriver::create('http://localhost:4444/wd/hub', $caps);
$webDriver->manage()->timeouts()->implicitlyWait(5);
$webDriver->navigate()->to('http://32b5oz2bbtn6gqj3.onion');
// Or $this-webDriver->get('http://32b5oz2bbtn6gqj3.onion');
$pageImages = $webDriver->findElements(WebDriverBy::tagName('img'));
$images = [];
$downloader = new ImageDownloader();
foreach($pageImages as $image)
{
$downloader->open($image->getAttribute('src'));
print 'Saving image ' . $downloader->getFileName();
// write some data about the request
file_put_contents(
sys_get_temp_dir() . '/' . $downloader->getFileName() . '.meta.json',
json_encode([
'pathinfo' => $downloader->getPathInfo(),
'curlinfo' => $downloader->getCurlInfo(),
'webdriver' => [
'title' => $webDriver->getTitle(),
'source' => base64_encode($webDriver->getPageSource())
]
], JSON_PRETTY_PRINT)
);
// save the image
$downloader->save(__DIR__ . '/images/');
// save the image in base64 format.
// base64_decode to view the raw image data.
$downloader->save(__DIR__ . '/images/', true);
}
TorLauncher::stop($torProcess);

View File

@ -0,0 +1,208 @@
<?php
/*
* This file is part of Selenium Tor Driver.
* (c) R. Eric Wheeler <eric@rewiv.com>
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Sikofitt\WebDriver;
/**
* Class ImageDownloader
*
* @package Sikofitt\WebDriver
*/
class ImageDownloader
{
const CURLPROXY_SOCKS5_HOSTNAME = 7;
private $fileInfo;
private $curl;
private $data;
private $pathinfo;
private $timeout;
private $proxyUrl;
private $file;
/**
* ImageDownloader constructor.
*
* @param int $timeout
* @param string $proxyUrl
*/
public function __construct($timeout = 120, $proxyUrl = '127.0.0.1:9150')
{
$this->timeout = $timeout;
$this->proxyUrl = $proxyUrl;
}
/**
* @return string
*/
public function getProxyUrl()
{
return $this->proxyUrl;
}
/**
* @param $proxyUrl
*
* @return $this
*/
public function setProxyUrl($proxyUrl)
{
$this->proxyUrl = $proxyUrl;
// Reinitialize file;
if (null !== $this->file) {
$this->open($this->file);
}
return $this;
}
/**
* @param $file
*/
public function open($file)
{
$this->file = $file;
$this->fileInfo = new \SplFileInfo($file);
$this->curl = curl_init($file);
curl_setopt_array($this->curl,
array(
CURLOPT_PROXY => $this->proxyUrl,
CURLOPT_PROXYTYPE => self::CURLPROXY_SOCKS5_HOSTNAME,
CURLOPT_HEADER => 0,
// Has to be 0, if set to 1 saves the header to the image file
CURLOPT_TIMEOUT => $this->timeout,
// .onion urls often takes time to load
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_RETURNTRANSFER => 1,
)
);
$this->pathinfo = parse_url($file);
$this->data = curl_exec($this->curl);
}
/**
* @return int
*/
public function getTimeout()
{
return $this->timeout;
}
/**
* @param $timeout
*
* @return $this
*/
public function setTimeOut($timeout)
{
$this->timeout = $timeout;
// Reinitialize file;
if (null !== $this->file) {
$this->open($this->file);
}
return $this;
}
/**
* @return array
* Fileinfo array
*/
public function getData()
{
return array(
'filename' => $this->fileInfo->getFilename(),
'data' => $this->data,
'pathinfo' => $this->pathinfo,
);
}
public function getData64()
{
return array(
'filename' => $this->fileInfo->getFilename(),
'data' => base64_encode($this->data),
'pathinfo' => $this->pathinfo,
);
}
/**
* @return mixed
*/
public function getPathInfo()
{
return $this->pathinfo;
}
/**
* @param $file
*
* @return $this
*/
public function setFile($file)
{
$this->fileInfo = new \SplFileInfo($file);
return $this;
}
/**
* @return mixed
*/
public function getCurlInfo()
{
return curl_getinfo($this->curl);
}
/**
* @param $path
* The filepath to save to.
* @param bool $base64
* If set to true will save as base64 encoded data.
*
* @return void
*/
public function save($path, $base64 = false)
{
// Make sure we were not passed a filename.
// If we were remove the filename.
if (strlen(pathinfo($path, PATHINFO_EXTENSION)) > 0) {
$path = pathinfo($path, PATHINFO_DIRNAME);
}
// Make sure path exists.
if (!is_dir($path)) {
mkdir($path, 0775, true);
}
// trim leading / if it exists.
$path = rtrim($path, '/');
if (true === $base64) {
$fileContent = base64_encode($this->data);
} else {
$fileContent = $this->data;
}
file_put_contents($path . '/' . $this->getFileName() . (true === $base64 ? '.b64.txt' : ''),
$fileContent);
}
/**
* @return mixed
*/
public function getFileName()
{
return $this->fileInfo->getFileName();
}
/**
* Close the curl handle.
*/
public function __destruct()
{
curl_close($this->curl);
}
}

View File

@ -24,10 +24,10 @@ class DesiredCapabilities extends FaceBookDesiredCapabilities
*/
public static function tor(FirefoxBinary $firefoxBinary = null)
{
$caps = new static([
$caps = new static(array(
WebDriverCapabilityType::BROWSER_NAME => 'firefox',
WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANY,
]);
));
if (null !== $firefoxBinary) {
$caps->setCapability('firefox_binary', $firefoxBinary->getPathname());
}

View File

@ -14,7 +14,7 @@ use Symfony\Component\Process\ProcessBuilder;
class TorLauncher
{
public static function launch(FirefoxBinary $firefoxBinary, $options = [])
public static function launch(FirefoxBinary $firefoxBinary, $options = array())
{
$defaultOptions = array(
'--detach',
@ -38,7 +38,7 @@ class TorLauncher
if ('linux' === strtolower(PHP_OS)) {
$processBuilder = new ProcessBuilder();
$processBuilder->setPrefix('killall')
->setArguments(['firefox'])
->setArguments(array('firefox'))
->getProcess()
->run();
}