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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?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

Avatar

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

Avatar

dolar cotizacion

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

No rating. Login to rate!

Neat and clean code. Thanks.

Your refactoring





Format Copy from initial code

or Cancel