6dc0e9a07bcff97ac9b111f36e12f1f6

I found a php function that does this, but I didnt like it. So, I recoded it as a class. I'm rather new to classes and OOP, so I'm sure there's a better way I could have gone about this. Any ideas on making my code more efficient will be appreciated.

<?php

/**
 * PHP Class for interfacing with the Tor Network
 */

class Tor
{
	var $url;
	var $userAgent;
	var $timeout;
	var $host;
	var $vector;
	var $payload;
	var $returnData;

	/**
	 * Tor Clas Constructor
	 */
	function Tor()
	{
		$this->url = null;
		$this->userAgent = null;
		$this->timeout = 300;
		$this->host = '127.0.0.1:9050';
		$this->vector = null;
	}

	/**
	 * set url
	 */
	function setUrl($url)
	{
		$this->url = $url;
	}

	/**
	 * read url
	 */
	function readUrl()
	{
		return $this->url;
	}

	/**
	 * set useragent
	 */
	function setUserAgent()
	{
		//list of browsers
		$agentBrowser = array(
				'Firefox',
				'Safari',
				'Opera',
				'Flock',
				'Internet Explorer',
				'Seamonkey',
				'Konqueror',
				'GoogleBot'
				);
				//list of operating systems
				$agentOS = array(
				'Windows 3.1',
				'Windows 95',
				'Windows 98',
				'Windows 2000',
				'Windows NT',
				'Windows XP',
				'Windows Vista',
				'Redhat Linux',
				'Ubuntu',
				'Fedora',
				'AmigaOS',
				'OS 10.5'
				);
				//randomly generate UserAgent
				$this->userAgent = $agentBrowser[rand(0,7)].'/'.rand(1,8).'.'.rand(0,9).' (' .$agentOS[rand(0,11)].' '.rand(1,7).'.'.rand(0,9).'; en-US;)';
	}

	/**
	 * read useragent
	 */
	function readUserAgent()
	{
		return $this->userAgent;
	}

	/**
	 * set timeout
	 */
	function setTimeout($timeout)
	{
		$this->timeout = $timeout;
	}

	/**
	 * read timeout
	 */
	function readTimeout()
	{
		return $this->timeout;
	}

	/**
	 * set host
	 */
	function setHost($ip, $port)
	{
		$this->host = $ip . ":" . $port;
	}

	/**
	 * read host
	 */
	function readHost()
	{
		return $this->host;
	}

	/**
	 * set vector
	 */
	function setVector($vector)
	{
		$this->vector = $vector;
	}

	/**
	 * read vector
	 */
	function readVector()
	{
		return $this->vector;
	}

	/**
	 * launch payload
	 */
	function launchPayload()
	{
		//set randomized parameters
		$this->setUserAgent();
			
		//concatinate url
		$this->payload = $this->url . $this->vector;
			
		//run curl action against url
		$action = curl_init();
		curl_setopt($action, CURLOPT_PROXY, $this->host);
		curl_setopt($action, CURLOPT_URL, $this->payload);
		curl_setopt($action, CURLOPT_HEADER, 1);
		curl_setopt($action, CURLOPT_USERAGENT, $this->userAgent);
		curl_setopt($action, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($action, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($action, CURLOPT_TIMEOUT, $this->timeout);
		$this->returnData = curl_exec($action);
		curl_close($action);
	}

	function viewReturn()
	{
		return $this->returnData;
	}
}

?>

Refactorings

No refactoring yet !

F288a8afe5302a16a366d5e9d34f2fec

Joe Grossberg

June 29, 2008, June 29, 2008 00:33, permalink

No rating. Login to rate!

Not a refactoring, but a few comments:

* Most of your comments are unnecessary. For example, it is obvious, through your good method name, what setUrl does.

* Second, it's traditional to name getters and setters with the names getFoo and setFoo, not readFoo and setFoo. This convention shouldn't be broken without good reason.

* Third, it appears that your use of getter and setter methods is unnecessary. None of the instance variables is private or protected, and none of the getters or setters (other than setHost) does anything other than retrieve or set the variable. I would ditch them.

* Lastly, I would move all the curl_setopt calls into another method, maybe something called setCurlDefaults

6dc0e9a07bcff97ac9b111f36e12f1f6

Ishkur

June 29, 2008, June 29, 2008 02:03, permalink

No rating. Login to rate!

Well, what I was going for was being able to set parameters and being able to call them back out later. But thanks, I wasn't aware of that naming convention clash i was doing. I did plan however, on setting some of these methods to be private because a few of them aren't really meant to be set by hand.

EDIT: I took Joe's advice and redid my code accordingly. I fixed my naming conventions as well as condensed all of my 'get''s into a single method returning an array.

I also moved all of the cURL into its own method, and I've updated the status of most of the methods and set them to private.

I have not however, had a chance to test it; though it should work just as before.

I think that it a little better, but im sure more can be done.

<?php

/**
 * PHP5 class for interfacing with the Tor network
 * by Josh Sandlin <josh@thenullbyte.org>
 * 
 * Licensed: MIT/X11
 * 
 * NOTE: The proxy host is configurable by the user, 
 * so therefore one i not limited to only the Tor
 * network. The default setting however is to run
 * all data through the Tor/Privoxy network.
 * 
 */

class Tor
{
    private $url;
    private $userAgent;
    private $timeout;
    private $proxy;
    private $vector;
    private $payload;
    private $returnData;

    public function Tor()
    {
        $this->url = null;
        $this->userAgent = null;
        $this->timeout = 300;
        $this->proxy = '127.0.0.1:9050';
        $this->vector = null;
        $this->payload = null;
        $this->returnData = null;
    }

    private function setUrl($url)
    {
        $this->url = $url;
    }

    private function setUserAgent()
    {
        //list of browsers
        $agentBrowser = array(
                'Firefox',
                'Safari',
                'Opera',
                'Flock',
                'Internet Explorer',
                'Seamonkey',
                'Konqueror',
                'GoogleBot'
        );
        //list of operating systems
        $agentOS = array(
                'Windows 3.1',
                'Windows 95',
                'Windows 98',
                'Windows 2000',
                'Windows NT',
                'Windows XP',
                'Windows Vista',
                'Redhat Linux',
                'Ubuntu',
                'Fedora',
                'AmigaOS',
                'OS 10.5'
        );
        //randomly generate UserAgent
        $this->userAgent = $agentBrowser[rand(0,7)].'/'.rand(1,8).'.'.rand(0,9).' (' .$agentOS[rand(0,11)].' '.rand(1,7).'.'.rand(0,9).'; en-US;)';
    }

    private function setTimeout($timeout)
    {
        $this->timeout = $timeout;
    }

    public function setProxy($ip, $port)
    {
        $this->proxy = $ip .":". $port;
    }

    private function setVector($vector)
    {
        $this->vector = $vector;
    }

    private function setCurl()
    {
        $action = curl_init();
        curl_setopt($action, CURLOPT_PROXY, $this->proxy);
        curl_setopt($action, CURLOPT_URL, $this->payload);
        curl_setopt($action, CURLOPT_HEADER, 1);
        curl_setopt($action, CURLOPT_USERAGENT, $this->userAgent);
        curl_setopt($action, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($action, CURLOPT_FOLLOLOCATION, 1);
        curl_setopt($action, CURLOPT_TIMEOUT, $this->timeout);
        $this->returnData = curl_exec($action);
        curl_close($action);
    }
    
    private function setPayload()
    {
        $this->payload = $this->url . $this->vector;
    }

    public function launch($url, $vector, $timeout = null)
    {
        //set parameters
        $this->setUrl($url);
        $this->setVector($vector);
        $this->setUserAgent();
            
        //set payload
        $this->setPayload();
            
        //if a timeout is set in the args, use it
        if(isset($timeout))
        {
            $this->setTimeout($timeout);
        }
            
        //run cURL action against url
        $this->setCurl();
    }

    public function getTorData()
    {
        return array(
                'url' => $this->url,
                'userAgent' => $this->userAgent,
                'timeout' => $this->timeout,
                'proxy' => $this->proxy,
                'payload' => $this->payload,
                'return' => $this->returnData
        );
    }
}

?>
Cc5528b9b1f5f0266bd160f6126ad6e4

Rodsdrorn

April 17, 2009, April 17, 2009 15:14, permalink

No rating. Login to rate!

Hello,
http://google.com - google

D41d8cd98f00b204e9800998ecf8427e

SuDolar

June 24, 2009, June 24, 2009 18:18, permalink

No rating. Login to rate!

Nice code, will try to implement it on my site. How's it performance-wise?
<a href="http://www.sudolar.com">Cotizacion Dolar</a> http://www.sudolar.com

D41d8cd98f00b204e9800998ecf8427e

dolar cotizacion

May 5, 2010, May 05, 2010 21:11, permalink

No rating. Login to rate!

Neat and clean code. Thanks.

D41d8cd98f00b204e9800998ecf8427e

bboy

September 11, 2010, September 11, 2010 21:20, permalink

No rating. Login to rate!

How I can run this class? .. Can someone post example of usage? Thank you

28c08be5625baaaae134156efef35b9d

mixdev

June 7, 2011, June 07, 2011 13:04, permalink

No rating. Login to rate!

You need to add (conditional) POST support.

curl_setopt($action, CURLOPT_POST, 1);

28c08be5625baaaae134156efef35b9d

mixdev

June 7, 2011, June 07, 2011 13:05, permalink

No rating. Login to rate!

You need to add (conditional) POST support.

curl_setopt($action, CURLOPT_POST, 1);
37c10e65d9f3ac29e9588d726657d0c3

TauffBekBaige

June 25, 2011, June 25, 2011 00:21, permalink

No rating. Login to rate!

industrial fashion zippers http://luxefashion.us/replay-brand47.html madhawa atapattu fashion garments ltd vietnam 547460

87bf718367054528ddaebd2b9420701f

Dave

August 13, 2011, August 13, 2011 18:27, permalink

No rating. Login to rate!

thanks for the code, gave me a good starting point. Tried using this however kept getting error

"It appears you have configured your web browser to use Tor as an HTTP proxy" when using it, turns out you need to set curl into SOCK5 mode.

Here's some class usage for anyone who can't work it out

require_once("tor.php");
$tor = new Tor();
$tor->launch('http://automation.whatismyip.com/n09230945.asp', '');
$data = $tor->getTorData();
echo $data['return'];

<?php

/**
 * PHP5 class for interfacing with the Tor network
 * by Josh Sandlin <josh@thenullbyte.org>
 * 
 * Licensed: MIT/X11
 * 
 * NOTE: The proxy host is configurable by the user, 
 * so therefore one i not limited to only the Tor
 * network. The default setting however is to run
 * all data through the Tor/Privoxy network.
 * 
 */

class Tor
{
    private $url;
    private $userAgent;
    private $timeout;
    private $proxy;
    private $vector;
    private $payload;
    private $returnData;

    public function Tor()
    {
        $this->url = null;
        $this->userAgent = null;
        $this->timeout = 300;
        $this->proxy = '127.0.0.1:9050';
        $this->vector = null;
        $this->payload = null;
        $this->returnData = null;
    }

    private function setUrl($url)
    {
        $this->url = $url;
    }

    private function setUserAgent()
    {
        //list of browsers
        $agentBrowser = array(
                'Firefox',
                'Safari',
                'Opera',
                'Flock',
                'Internet Explorer',
                'Seamonkey',
                'Konqueror',
                'GoogleBot'
        );
        //list of operating systems
        $agentOS = array(
                'Windows 3.1',
                'Windows 95',
                'Windows 98',
                'Windows 2000',
                'Windows NT',
                'Windows XP',
                'Windows Vista',
                'Redhat Linux',
                'Ubuntu',
                'Fedora',
                'AmigaOS',
                'OS 10.5'
        );
        //randomly generate UserAgent
        $this->userAgent = $agentBrowser[rand(0,7)].'/'.rand(1,8).'.'.rand(0,9).' (' .$agentOS[rand(0,11)].' '.rand(1,7).'.'.rand(0,9).'; en-US;)';
    }

    private function setTimeout($timeout)
    {
        $this->timeout = $timeout;
    }

    public function setProxy($ip, $port)
    {
        $this->proxy = $ip .":". $port;
    }

    private function setVector($vector)
    {
        $this->vector = $vector;
    }

    private function setCurl()
    {
        $action = curl_init();
        curl_setopt($action, CURLOPT_PROXY, $this->proxy);
        curl_setopt($action, CURLOPT_URL, $this->payload);
        curl_setopt($action, CURLOPT_HEADER, 1);
        curl_setopt($action, CURLOPT_USERAGENT, $this->userAgent);
        curl_setopt($action, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($action, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($action, CURLOPT_TIMEOUT, $this->timeout);
        curl_setopt($action, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
        $this->returnData = curl_exec($action);
        curl_close($action);
    }
    
    private function setPayload()
    {
        $this->payload = $this->url . $this->vector;
    }

    public function launch($url, $vector, $timeout = null)
    {
        //set parameters
        $this->setUrl($url);
        $this->setVector($vector);
        $this->setUserAgent();
            
        //set payload
        $this->setPayload();
            
        //if a timeout is set in the args, use it
        if(isset($timeout))
        {
            $this->setTimeout($timeout);
        }
            
        //run cURL action against url
        $this->setCurl();
    }

    public function getTorData()
    {
        return array(
                'url' => $this->url,
                'userAgent' => $this->userAgent,
                'timeout' => $this->timeout,
                'proxy' => $this->proxy,
                'payload' => $this->payload,
                'return' => $this->returnData
        );
    }
}

?>
Ff39a476c7e79591123d539e56b097d6

cesinscrert

September 18, 2011, September 18, 2011 04:33, permalink

No rating. Login to rate!

ocean city movie theater http://movieszone.eu/ the simpsons movie review 817371

Db58a3304a6e38a94db6c19431967f88

esseptise

October 12, 2011, October 12, 2011 05:13, permalink

No rating. Login to rate!

aarp national mail order pharmacy http://cheaplegalmedications.com/products/lithium-carbonate.htm applied pharmacy services list

Bcb7be26e8bd46968024fb36a6c48d5e

LP

November 19, 2011, November 19, 2011 22:19, permalink

No rating. Login to rate!

version and last modified date is a must! Found several copies of this online, but didnt know if they are the same or some updated version.

Bcb7be26e8bd46968024fb36a6c48d5e

LP

November 19, 2011, November 19, 2011 22:19, permalink

No rating. Login to rate!

version and last modified date is a must! Found several copies of this online, but didnt know if they are the same or some updated version.

861371c57a3145bd9ea39f82c274047b

Alexander

December 3, 2011, December 03, 2011 06:07, permalink

No rating. Login to rate!

I was missing some POST functionality, and also the possibility to use --newnym. (I was writing a script to cheat on a internet poll)

<?php
/**
 * PHP5 class for interfacing with the Tor network
 * by Josh Sandlin <josh@thenullbyte.org>
 * refactored by Alexander Kuzmin <alex_123461@hotmail.com>
 *
 * Licensed: MIT/X11
 * 
 * 2011-12-03
 */

class Tor {
	private $url;
	private $userAgent;
	private $timeout;
	private $proxy;
	private $vector;
	private $payload;
	private $returnData;
	
	private $post;
	private $postString;
	private $postCount;
	
	private $ip;
	private $controlPort;
	private $authCode;
	
	public function __construct() {
		$this->ip = '127.0.0.1';
		$this->controlPort = '9051';
		$this->authCode = '';
	
		$this->post = false;
		
		$this->url = null;
		$this->userAgent = null;
		$this->timeout = 300;
		$this->proxy = '127.0.0.1:9050';
		$this->vector = null;
		$this->payload = null;
		$this->returnData = null;
	}
	public function newIdentity() {
		$fp = fsockopen($this->ip, $this->controlPort, $errno, $errstr, 30);
		if (!$fp) throw new Exception('can\'t connect to control port');
		
		fputs($fp, "AUTHENTICATE " . $this->authCode . "\r\n");
		$response = fread($fp, 1024);
		list($code, $text) = explode(' ', $response, 2);
		if ($code != '250') throw new Exception('Authentication failed');
	 
		//send the request to for new identity
		fputs($fp, "signal NEWNYM\r\n");
		$response = fread($fp, 1024);
		list($code, $text) = explode(' ', $response, 2);
		if ($code != '250') throw new Exception('signal failed');
	 
		fclose($fp);
	}
	
	private function setUrl($url) {
		$this->url = $url;
	}

	private function setUserAgent() {
		//list of browsers
		$agentBrowser = array(
			'Firefox',
			'Safari',
			'Opera',
			'Internet Explorer',
			'Seamonkey',
		);
		//list of operating systems
		$agentOS = array(
			'Windows XP',
			'Windows Vista',
			'Windows 7'
		);
		//randomly generate UserAgent
		$this->userAgent = $agentBrowser[rand(0,count($agentBrowser)-1)].'/'.rand(1,8).'.'.rand(0,9).' (' .$agentOS[rand(0,count($agentOS)-1)].' '.rand(1,7).'.'.rand(0,9).'; en-US;)';
	}

	private function setTimeout($timeout) {
		$this->timeout = $timeout;
	}

	public function setProxy($ip, $port) {
		$this->proxy = $ip .":". $port;
	}
	
	public function setPost($postData) {
		$this->post = true;
		//url-ify the data for the POST
		$fields_string = '';
		foreach($postData as $key=>$value) { 
			$fields_string .= $key.'='.$value.'&'; 
		}
		$fields_string = rtrim($fields_string,'&');
		
		$this->postString = $fields_string;
		$this->postCount = count($postData);
	}
	public function clearPost() {
		$this->post = false;
		$this->postString = null;
		$this->postCount = null;
	}
	
	private function setVector($vector) {
		$this->vector = $vector;
	}

	private function setCurl() {
		$action = curl_init();
		curl_setopt($action, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5); 
		curl_setopt($action, CURLOPT_PROXY, $this->proxy);
		curl_setopt($action, CURLOPT_URL, $this->payload);
		curl_setopt($action, CURLOPT_HEADER, 1);
		curl_setopt($action, CURLOPT_USERAGENT, $this->userAgent);
		curl_setopt($action, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($action, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($action, CURLOPT_TIMEOUT, $this->timeout);
		
		if($this->post) {
			curl_setopt($action, CURLOPT_POST, $this->postCount);
			curl_setopt($action, CURLOPT_POSTFIELDS, $this->postString);
		}
		
		$this->returnData = curl_exec($action);
		curl_close($action);
	}
	
	private function setPayload() {
		$this->payload = $this->url . $this->vector;
	}

	public function launch($url, $vector, $timeout = null) {
		//set parameters
		$this->setUrl($url);
		$this->setVector($vector);
		$this->setUserAgent();
			
		//set payload
		$this->setPayload();
			
		//if a timeout is set in the args, use it
		if(isset($timeout)) {
			$this->setTimeout($timeout);
		}
			
		//run cURL action against url
		$this->setCurl();
	}

	public function getTorData() {
		return array(
			'url' => $this->url,
			'postString' => $this->postString,
			'userAgent' => $this->userAgent,
			'timeout' => $this->timeout,
			'proxy' => $this->proxy,
			'payload' => $this->payload,
			'return' => $this->returnData
		);
	}
}

?>
861371c57a3145bd9ea39f82c274047b

Alexander

December 3, 2011, December 03, 2011 06:07, permalink

No rating. Login to rate!

I was missing some POST functionality, and also the possibility to use --newnym. (I was writing a script to cheat on a internet poll)

<?php
/**
 * PHP5 class for interfacing with the Tor network
 * by Josh Sandlin <josh@thenullbyte.org>
 * refactored by Alexander Kuzmin <alex_123461@hotmail.com>
 *
 * Licensed: MIT/X11
 * 
 * 2011-12-03
 */

class Tor {
	private $url;
	private $userAgent;
	private $timeout;
	private $proxy;
	private $vector;
	private $payload;
	private $returnData;
	
	private $post;
	private $postString;
	private $postCount;
	
	private $ip;
	private $controlPort;
	private $authCode;
	
	public function __construct() {
		$this->ip = '127.0.0.1';
		$this->controlPort = '9051';
		$this->authCode = '';
	
		$this->post = false;
		
		$this->url = null;
		$this->userAgent = null;
		$this->timeout = 300;
		$this->proxy = '127.0.0.1:9050';
		$this->vector = null;
		$this->payload = null;
		$this->returnData = null;
	}
	public function newIdentity() {
		$fp = fsockopen($this->ip, $this->controlPort, $errno, $errstr, 30);
		if (!$fp) throw new Exception('can\'t connect to control port');
		
		fputs($fp, "AUTHENTICATE " . $this->authCode . "\r\n");
		$response = fread($fp, 1024);
		list($code, $text) = explode(' ', $response, 2);
		if ($code != '250') throw new Exception('Authentication failed');
	 
		//send the request to for new identity
		fputs($fp, "signal NEWNYM\r\n");
		$response = fread($fp, 1024);
		list($code, $text) = explode(' ', $response, 2);
		if ($code != '250') throw new Exception('signal failed');
	 
		fclose($fp);
	}
	
	private function setUrl($url) {
		$this->url = $url;
	}

	private function setUserAgent() {
		//list of browsers
		$agentBrowser = array(
			'Firefox',
			'Safari',
			'Opera',
			'Internet Explorer',
			'Seamonkey',
		);
		//list of operating systems
		$agentOS = array(
			'Windows XP',
			'Windows Vista',
			'Windows 7'
		);
		//randomly generate UserAgent
		$this->userAgent = $agentBrowser[rand(0,count($agentBrowser)-1)].'/'.rand(1,8).'.'.rand(0,9).' (' .$agentOS[rand(0,count($agentOS)-1)].' '.rand(1,7).'.'.rand(0,9).'; en-US;)';
	}

	private function setTimeout($timeout) {
		$this->timeout = $timeout;
	}

	public function setProxy($ip, $port) {
		$this->proxy = $ip .":". $port;
	}
	
	public function setPost($postData) {
		$this->post = true;
		//url-ify the data for the POST
		$fields_string = '';
		foreach($postData as $key=>$value) { 
			$fields_string .= $key.'='.$value.'&'; 
		}
		$fields_string = rtrim($fields_string,'&');
		
		$this->postString = $fields_string;
		$this->postCount = count($postData);
	}
	public function clearPost() {
		$this->post = false;
		$this->postString = null;
		$this->postCount = null;
	}
	
	private function setVector($vector) {
		$this->vector = $vector;
	}

	private function setCurl() {
		$action = curl_init();
		curl_setopt($action, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5); 
		curl_setopt($action, CURLOPT_PROXY, $this->proxy);
		curl_setopt($action, CURLOPT_URL, $this->payload);
		curl_setopt($action, CURLOPT_HEADER, 1);
		curl_setopt($action, CURLOPT_USERAGENT, $this->userAgent);
		curl_setopt($action, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($action, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($action, CURLOPT_TIMEOUT, $this->timeout);
		
		if($this->post) {
			curl_setopt($action, CURLOPT_POST, $this->postCount);
			curl_setopt($action, CURLOPT_POSTFIELDS, $this->postString);
		}
		
		$this->returnData = curl_exec($action);
		curl_close($action);
	}
	
	private function setPayload() {
		$this->payload = $this->url . $this->vector;
	}

	public function launch($url, $vector, $timeout = null) {
		//set parameters
		$this->setUrl($url);
		$this->setVector($vector);
		$this->setUserAgent();
			
		//set payload
		$this->setPayload();
			
		//if a timeout is set in the args, use it
		if(isset($timeout)) {
			$this->setTimeout($timeout);
		}
			
		//run cURL action against url
		$this->setCurl();
	}

	public function getTorData() {
		return array(
			'url' => $this->url,
			'postString' => $this->postString,
			'userAgent' => $this->userAgent,
			'timeout' => $this->timeout,
			'proxy' => $this->proxy,
			'payload' => $this->payload,
			'return' => $this->returnData
		);
	}
}

?>
861371c57a3145bd9ea39f82c274047b

Alexander

December 3, 2011, December 03, 2011 06:07, permalink

No rating. Login to rate!

I was missing some POST functionality, and also the possibility to use --newnym. (I was writing a script to cheat on a internet poll)

<?php
/**
 * PHP5 class for interfacing with the Tor network
 * by Josh Sandlin <josh@thenullbyte.org>
 * refactored by Alexander Kuzmin <alex_123461@hotmail.com>
 *
 * Licensed: MIT/X11
 * 
 * 2011-12-03
 */

class Tor {
	private $url;
	private $userAgent;
	private $timeout;
	private $proxy;
	private $vector;
	private $payload;
	private $returnData;
	
	private $post;
	private $postString;
	private $postCount;
	
	private $ip;
	private $controlPort;
	private $authCode;
	
	public function __construct() {
		$this->ip = '127.0.0.1';
		$this->controlPort = '9051';
		$this->authCode = '';
	
		$this->post = false;
		
		$this->url = null;
		$this->userAgent = null;
		$this->timeout = 300;
		$this->proxy = '127.0.0.1:9050';
		$this->vector = null;
		$this->payload = null;
		$this->returnData = null;
	}
	public function newIdentity() {
		$fp = fsockopen($this->ip, $this->controlPort, $errno, $errstr, 30);
		if (!$fp) throw new Exception('can\'t connect to control port');
		
		fputs($fp, "AUTHENTICATE " . $this->authCode . "\r\n");
		$response = fread($fp, 1024);
		list($code, $text) = explode(' ', $response, 2);
		if ($code != '250') throw new Exception('Authentication failed');
	 
		//send the request to for new identity
		fputs($fp, "signal NEWNYM\r\n");
		$response = fread($fp, 1024);
		list($code, $text) = explode(' ', $response, 2);
		if ($code != '250') throw new Exception('signal failed');
	 
		fclose($fp);
	}
	
	private function setUrl($url) {
		$this->url = $url;
	}

	private function setUserAgent() {
		//list of browsers
		$agentBrowser = array(
			'Firefox',
			'Safari',
			'Opera',
			'Internet Explorer',
			'Seamonkey',
		);
		//list of operating systems
		$agentOS = array(
			'Windows XP',
			'Windows Vista',
			'Windows 7'
		);
		//randomly generate UserAgent
		$this->userAgent = $agentBrowser[rand(0,count($agentBrowser)-1)].'/'.rand(1,8).'.'.rand(0,9).' (' .$agentOS[rand(0,count($agentOS)-1)].' '.rand(1,7).'.'.rand(0,9).'; en-US;)';
	}

	private function setTimeout($timeout) {
		$this->timeout = $timeout;
	}

	public function setProxy($ip, $port) {
		$this->proxy = $ip .":". $port;
	}
	
	public function setPost($postData) {
		$this->post = true;
		//url-ify the data for the POST
		$fields_string = '';
		foreach($postData as $key=>$value) { 
			$fields_string .= $key.'='.$value.'&'; 
		}
		$fields_string = rtrim($fields_string,'&');
		
		$this->postString = $fields_string;
		$this->postCount = count($postData);
	}
	public function clearPost() {
		$this->post = false;
		$this->postString = null;
		$this->postCount = null;
	}
	
	private function setVector($vector) {
		$this->vector = $vector;
	}

	private function setCurl() {
		$action = curl_init();
		curl_setopt($action, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5); 
		curl_setopt($action, CURLOPT_PROXY, $this->proxy);
		curl_setopt($action, CURLOPT_URL, $this->payload);
		curl_setopt($action, CURLOPT_HEADER, 1);
		curl_setopt($action, CURLOPT_USERAGENT, $this->userAgent);
		curl_setopt($action, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($action, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($action, CURLOPT_TIMEOUT, $this->timeout);
		
		if($this->post) {
			curl_setopt($action, CURLOPT_POST, $this->postCount);
			curl_setopt($action, CURLOPT_POSTFIELDS, $this->postString);
		}
		
		$this->returnData = curl_exec($action);
		curl_close($action);
	}
	
	private function setPayload() {
		$this->payload = $this->url . $this->vector;
	}

	public function launch($url, $vector, $timeout = null) {
		//set parameters
		$this->setUrl($url);
		$this->setVector($vector);
		$this->setUserAgent();
			
		//set payload
		$this->setPayload();
			
		//if a timeout is set in the args, use it
		if(isset($timeout)) {
			$this->setTimeout($timeout);
		}
			
		//run cURL action against url
		$this->setCurl();
	}

	public function getTorData() {
		return array(
			'url' => $this->url,
			'postString' => $this->postString,
			'userAgent' => $this->userAgent,
			'timeout' => $this->timeout,
			'proxy' => $this->proxy,
			'payload' => $this->payload,
			'return' => $this->returnData
		);
	}
}

?>
54d1a5e36f7164aee3d3b2556a0a19e9

Yancy

January 5, 2012, January 05, 2012 06:59, permalink

No rating. Login to rate!

You rlelay saved my skin with this information. Thanks!

?
54d1a5e36f7164aee3d3b2556a0a19e9

Yancy

January 5, 2012, January 05, 2012 06:59, permalink

No rating. Login to rate!

You rlelay saved my skin with this information. Thanks!

?
54d1a5e36f7164aee3d3b2556a0a19e9

Yancy

January 5, 2012, January 05, 2012 06:59, permalink

No rating. Login to rate!

You rlelay saved my skin with this information. Thanks!

?
A48f6c8b5558e2a47b06533e5fb17272

tietryLeappy

February 2, 2012, February 02, 2012 19:53, permalink

No rating. Login to rate!

nevada pharmacy technician certification http://legalexclusivepharmacy.com/catalog/Muscle_Relaxant/Skelaxin.htm massachusetts college of pharmacy and health sciences

Your refactoring





Format Copy from initial code

or Cancel