Subversion Repositories cheapmusic

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
103 - 1
<?php
2
 
3
/***************************************************************************
4
 *   Copyright (C) 2009-2011 by Geo Varghese(www.seopanel.in)			   *
5
 *   sendtogeo@gmail.com   												   *
6
 *                                                                         *
7
 *   This program is free software; you can redistribute it and/or modify  *
8
 *   it under the terms of the GNU General Public License as published by  *
9
 *   the Free Software Foundation; either version 2 of the License, or     *
10
 *   (at your option) any later version.                                   *
11
 *                                                                         *
12
 *   This program is distributed in the hope that it will be useful,       *
13
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
14
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
15
 *   GNU General Public License for more details.                          *
16
 *                                                                         *
17
 *   You should have received a copy of the GNU General Public License     *
18
 *   along with this program; if not, write to the                         *
19
 *   Free Software Foundation, Inc.,                                       *
20
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
21
 ***************************************************************************/
22
 
23
# class defines all proxy controller functions
24
class ProxyController extends Controller{
25
 
26
	# func to show proxy list
27
	function listProxy($info=''){
28
 
29
		$userId = isLoggedIn();
30
		$sql = "select * from proxylist where 1=1";
31
 
32
		if (isset($info['status'])) {
33
			if (($info['status']== 'active') || ($info['status']== 'inactive')) {
34
				$statVal = ($info['status']=='active') ? 1 : 0;
35
				$conditions .= " and status=$statVal";
36
				$urlParams .= "&status=".$info['status'];
37
			}
38
		} else {
39
			$info['status'] = '';
40
		}
41
		$this->set('statVal', $info['status']);
42
 
43
		if (empty($info['keyword'])) {
44
			$info['keyword'] =  '';
45
		} else {
46
			$info['keyword'] = urldecode($info['keyword']);
47
			$conditions .= " and proxy like '%".addslashes($info['keyword'])."%'";
48
			$urlParams .= "&keyword=".urlencode($info['keyword']);
49
		}
50
		$this->set('keyword', $info['keyword']);
51
 
52
		$sql .= " $conditions order by id";
53
 
54
		# pagination setup
55
		$this->db->query($sql, true);
56
		$this->paging->setDivClass('pagingdiv');
57
		$this->paging->loadPaging($this->db->noRows, SP_PAGINGNO);
58
		$pagingDiv = $this->paging->printPages('proxy.php', '', 'scriptDoLoad', 'content', $urlParams);
59
		$this->set('pagingDiv', $pagingDiv);
60
		$sql .= " limit ".$this->paging->start .",". $this->paging->per_page;
61
 
62
		$proxyList = $this->db->select($sql);
63
		$this->set('pageNo', $info['pageno']);
64
		$this->set('list', $proxyList);
65
		$this->set('urlParams', $urlParams);
66
		$this->render('proxy/proxylist');
67
	}
68
 
69
	# func to show import proxy form
70
	function showImportProxy($info = ''){
71
		$this->render('proxy/importproxy');
72
	}
73
 
74
	#funvtion to import proxy
75
	function importProxy($data = "") {
76
		$errMsg['proxy_list'] = formatErrorMsg($this->validate->checkBlank($data['proxy_list']));
77
		if(!$this->validate->flagErr){
78
			$resInfo['total'] = $resInfo['valid'] = $resInfo['existing'] = 0;
79
			$proxyMaxId = $this->db->getMaxId('proxylist');
80
			$proxyList = explode("\n", $data['proxy_list']);
81
			foreach ($proxyList as $proxy) {
82
				if(!preg_match('/\w+/', $proxy)) continue;
83
				$listInfo = explode(",", $proxy);
84
				$proxyInfo['proxy'] = trim($listInfo[0]);
85
				if (!empty($proxyInfo['proxy'])) {
86
					$resInfo['total']++;
87
					$proxyInfo['port'] = trim($listInfo[1]);
88
					$proxyInfo['proxy_username'] = trim($listInfo[2]);
89
					$proxyInfo['proxy_password'] = trim($listInfo[3]);
90
					if ($this->__checkProxy($proxyInfo['proxy'], $proxyInfo['port'])) {
91
						$resInfo['existing']++;
92
					} else {
93
						$proxyInfo['proxy_auth'] = (!empty($proxyInfo['proxy_username']) || !empty($proxyInfo['proxy_password'])) ? 1 : 0;
94
						$this->insertProxy($proxyInfo);
95
						$resInfo['valid']++;
96
					}
97
 
98
				}
99
			}
100
 
101
			// if imported proxies needs to be checked
102
			$proxyList = array();
103
			if (!empty($data['check_status'])) {
104
				$this->updateProxyCheckedStatus(0, "");
105
				$condition = " and id > $proxyMaxId";
106
				$sql = "select * from proxylist where 1=1 ". $condition;
107
				$proxyList = $this->db->select($sql);
108
			}
109
 
110
			$this->set('proxyList', $proxyList);
111
			$this->set('resInfo', $resInfo);
112
			$this->set('proxyMaxId', $proxyMaxId);
113
			$this->render('proxy/importresult');
114
 
115
		} else {
116
			showErrorMsg("Please enter valid proxy list.");
117
		}
118
	}
119
 
120
	# func to check status of all proxy list
121
	function showcheckAllStatus($info=''){
122
		$this->render('proxy/showcheckallstatus');
123
	}
124
 
125
	# function to check all proxy status
126
	function checkAllProxyStatus($info = '') {
127
		$isStatusCheck = false;
128
		$this->updateProxyCheckedStatus(0, "");
129
		if ($info['status'] != "") {
130
			$isStatusCheck = true;
131
			$status = ($info['status'] == 'active') ? 1 : 0;
132
			$this->set('status', $status);
133
		}
134
 
135
		$proxyList = $this->__getAllProxys($isStatusCheck, $status);
136
		$this->set('activeCount', $this->__getProxyCount(" where status=1 and checked=1"));
137
		$this->set('inActiveCount', $this->__getProxyCount(" where status=0 and checked=1"));
138
		$this->set('proxyList', $proxyList);
139
		$this->render('proxy/checkallstatus');
140
 
141
	}
142
 
143
	# func to check status of all proxy list
144
	function runCheckStatus($info = '') {
145
		$proxyId = intval($info['id']);
146
		$this->checkStatus($proxyId);
147
		$this->updateProxyCheckedStatus(1, " where id=$proxyId");
148
 
149
		$where = "";
150
		if (isset($info['status'])) {
151
			$status = intval($info['status']);
152
			$where = " and status=$status";
153
			$this->set('status', $status);
154
		}
155
 
156
		// if max id is set
157
		if (isset($info['proxy_max_id'])) {
158
			$where = " and id > ".intval($info['proxy_max_id']);
159
			$this->set('proxyMaxId', $info['proxy_max_id']);
160
		}
161
 
162
		$sql = "select * from proxylist where checked=0 $where order by id";
163
		$proxyList = $this->db->select($sql);
164
		$this->set('activeCount', $this->__getProxyCount(" where status=1 and checked=1"));
165
		$this->set('inActiveCount', $this->__getProxyCount(" where status=0 and checked=1"));
166
		$this->set('checkedCount', $this->__getProxyCount(" where checked=1"));
167
		$this->set('proxyList', $proxyList);
168
		$this->render('proxy/runcheckstatus');
169
	}
170
 
171
	# function to update checked status of the proxy list
172
	function updateProxyCheckedStatus($checkedVal = 0, $where = "") {
173
		$sql = "update proxylist set checked=".intval($checkedVal);
174
		$sql .= empty($where) ? "" : $where;
175
		$this->db->query($sql);
176
	}
177
 
178
	# function to get proxy count
179
	function __getProxyCount($where = '') {
180
		$sql = "select count(*) as count from proxylist $where";
181
		$listInfo = $this->db->select($sql, true);
182
		return $listInfo['count'];
183
	}
184
 
185
	# func to get all Proxys
186
	function __getAllProxys($isStatusCheck=true, $status=1){
187
		$sql = "select * from proxylist where 1=1";
188
		if($isStatusCheck){
189
			$sql .= " and status=".intval($status);
190
		}
191
		$sql .= " order by id";
192
		$proxyList = $this->db->select($sql);
193
		return $proxyList;
194
	}
195
 
196
	# func to change status
197
	function __changeStatus($proxyId, $status){
198
		$sql = "update proxylist set status=$status where id=".intval($proxyId);
199
		$this->db->query($sql);
200
	}
201
 
202
	# func to change status
203
	function __deleteProxy($proxyId){
204
		$sql = "delete from proxylist where id=".intval($proxyId);
205
		$this->db->query($sql);
206
	}
207
 
208
	function __checkProxy($name, $port=80){
209
		$sql = "select id from proxylist where proxy='".addslashes($name)."' and port=".intval($port);
210
		$listInfo = $this->db->select($sql, true);
211
		return empty($listInfo['id']) ? false :  $listInfo['id'];
212
	}
213
 
214
 
215
	function newProxy($listInfo=''){
216
		if (!isset($listInfo['port'])) {
217
			$listInfo['port'] = 80;
218
		}
219
 
220
		$this->set('post', $listInfo);
221
		$this->render('proxy/newproxy');
222
	}
223
 
224
	function createProxy($listInfo){
225
 
226
		$errMsg['proxy'] = formatErrorMsg($this->validate->checkBlank($listInfo['proxy']));
227
		$errMsg['port'] = formatErrorMsg($this->validate->checkNumber($listInfo['port']));
228
		if (!empty($listInfo['proxy_auth'])) {
229
			$errMsg['proxy_username'] = formatErrorMsg($this->validate->checkBlank($listInfo['proxy_username']));
230
			$errMsg['proxy_password'] = formatErrorMsg($this->validate->checkBlank($listInfo['proxy_password']));
231
		}
232
		if(!$this->validate->flagErr){
233
			if (!$this->__checkProxy($listInfo['proxy'], $listInfo['port'])) {
234
				$this->insertProxy($listInfo);
235
				$this->listProxy();
236
				exit;
237
			}else{
238
				$errMsg['proxy'] = formatErrorMsg($this->spTextProxy['Proxyalreadyexist']);
239
			}
240
		}
241
		$this->set('errMsg', $errMsg);
242
		$this->newProxy($listInfo);
243
	}
244
 
245
	function insertProxy($listInfo) {
246
		$proxyAuth = empty($listInfo['proxy_auth']) ? 0 : 1;
247
		$sql = "insert into proxylist(proxy,port,proxy_auth,proxy_username,proxy_password,status)
248
		values('".addslashes($listInfo['proxy'])."', '".intval($listInfo['port'])."', $proxyAuth, '".addslashes($listInfo['proxy_username'])."', '".addslashes($listInfo['proxy_password'])."', 0)";
249
		$this->db->query($sql);
250
	}
251
 
252
	function __getProxyInfo($proxyId){
253
		$sql = "select * from proxylist where id=".intval($proxyId);
254
		$listInfo = $this->db->select($sql, true);
255
		$listInfo['proxy_username'] = stripslashes($listInfo['proxy_username']);
256
		$listInfo['proxy_password'] = stripslashes($listInfo['proxy_password']);
257
		return empty($listInfo['id']) ? false :  $listInfo;
258
	}
259
 
260
	function editProxy($proxyId, $listInfo=''){
261
 
262
		if(!empty($proxyId)){
263
			if(empty($listInfo)){
264
				$listInfo = $this->__getProxyInfo($proxyId);
265
				$listInfo['oldProxy'] = $listInfo['proxy'];
266
			}
267
			$this->set('post', $listInfo);
268
 
269
			$this->render('proxy/editproxy');
270
			exit;
271
		}
272
		$this->listProxy();
273
	}
274
 
275
	function updateProxy($listInfo){
276
		$listInfo['id'] = intval($listInfo['id']);
277
		$this->set('post', $listInfo);
278
		$errMsg['proxy'] = formatErrorMsg($this->validate->checkBlank($listInfo['proxy']));
279
		$errMsg['port'] = formatErrorMsg($this->validate->checkNumber($listInfo['port']));
280
		if (!empty($listInfo['proxy_auth'])) {
281
			$errMsg['proxy_username'] = formatErrorMsg($this->validate->checkBlank($listInfo['proxy_username']));
282
			$errMsg['proxy_password'] = formatErrorMsg($this->validate->checkBlank($listInfo['proxy_password']));
283
		}
284
		if(!$this->validate->flagErr){
285
 
286
			if($listInfo['proxy'] != $listInfo['oldProxy']){
287
				if ($this->__checkProxy($listInfo['proxy'], $listInfo['port'])) {
288
					$errMsg['proxy'] = formatErrorMsg($this->spTextProxy['Proxyalreadyexist']);
289
					$this->validate->flagErr = true;
290
				}
291
			}
292
 
293
			if (!$this->validate->flagErr) {
294
				$proxyAuth = empty($listInfo['proxy_auth']) ? 0 : 1;
295
				$sql = "update proxylist set
296
						proxy = '".addslashes($listInfo['proxy'])."',
297
						port = '".intval($listInfo['port'])."',
298
						status = '".intval($listInfo['status'])."',
299
						proxy_auth = $proxyAuth,
300
						proxy_username = '".addslashes($listInfo['proxy_username'])."',
301
						proxy_password = '".addslashes($listInfo['proxy_password'])."'
302
						where id={$listInfo['id']}";
303
				$this->db->query($sql);
304
				$this->listProxy();
305
				exit;
306
			}
307
		}
308
		$this->set('errMsg', $errMsg);
309
		$this->editProxy($listInfo['id'], $listInfo);
310
	}
311
 
312
	# func to check whether proxy is active or not
313
	function __isProxyActive($proxyId) {
314
 
315
		$proxyInfo = $this->__getProxyInfo($proxyId);
316
		$ret = $this->spider->checkProxy($proxyInfo);
317
		return empty($ret['error']) ? 1 : 0;
318
 
319
	}
320
 
321
	function checkStatus($proxyId) {
322
		$status = $this->__isProxyActive($proxyId);
323
		$this->__changeStatus($proxyId, $status);
324
	}
325
 
326
	function getRandomProxy() {
327
		$sql = "SELECT * FROM proxylist where status=1 ORDER BY RAND() LIMIT 1";
328
		$listInfo = $this->db->select($sql, true);
329
		$listInfo['proxy_username'] = stripslashes($listInfo['proxy_username']);
330
		$listInfo['proxy_password'] = stripslashes($listInfo['proxy_password']);
331
		return empty($listInfo['id']) ? false :  $listInfo;
332
	}
333
 
334
	// function to show cron command
335
	function showCronCommand(){
336
 
337
		$this->render('proxy/croncommand');
338
	}
339
	/**
340
	 * function to show perfomance of a proxy
341
	 * @param Array $info	Contains all search info details
342
	 */
343
	function showProxyPerfomance($info = '') {
344
 
345
		$sql = "select p.id as proxy_id, p.proxy, p.port, count(*) count, sum(crawl_status) success, avg(crawl_status) avg_score,
346
		count(*) - sum(crawl_status) fail from crawl_log t join proxylist p on p.id=t.proxy_id where 1=1";
347
 
348
		$conditions = "";
349
		if (empty($info['keyword'])) {
350
			$info['keyword'] =  '';
351
		} else {
352
			$info['keyword'] = urldecode($info['keyword']);
353
			$searchKeyword = addslashes($info['keyword']);
354
			$conditions .= " and p.proxy like '%$searchKeyword%'";
355
			$urlParams .= "&keyword=".urlencode($info['keyword']);
356
		}
357
 
358
		$this->set('keyword', $info['keyword']);
359
 
360
		if (!empty ($info['from_time'])) {
361
			$fromTime = strtotime($info['from_time'] . ' 00:00:00');
362
		} else {
363
			$fromTime = mktime(0, 0, 0, date('m'), date('d') - 90, date('Y'));
364
		}
365
 
366
		if (!empty ($info['to_time'])) {
367
			$toTime = strtotime($info['to_time'] . ' 00:00:00');
368
		} else {
369
			$toTime = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
370
		}
371
 
372
		$fromTimeLabel = date('Y-m-d', $fromTime);
373
		$toTimeLabel = date('Y-m-d', $toTime);
374
		$this->set('fromTime', $fromTimeLabel);
375
		$this->set('toTime', $toTimeLabel);
376
		$urlParams .= "&from_time=$fromTimeLabel&to_time=$toTimeLabel";
377
 
378
		// set status
379
		$urlParams .= "&order_by=".$info['order_by'];
380
		$this->set('statVal', $info['order_by']);
381
 
382
		// sql created using param
383
		$sql .= " $conditions and crawl_time >='$fromTimeLabel 00:00:00' and crawl_time<='$toTimeLabel 23:59:59' group by proxy_id order by avg_score";
384
		$sql .= ($info['order_by'] == 'fail') ? " ASC" : " DESC";
385
 
386
		// pagination setup
387
		$this->db->query($sql, true);
388
		$this->paging->setDivClass('pagingdiv');
389
		$this->paging->loadPaging($this->db->noRows, SP_PAGINGNO);
390
		$pagingDiv = $this->paging->printPages('proxy.php?sec=perfomance', '', 'scriptDoLoad', 'content', $urlParams);
391
		$this->set('pagingDiv', $pagingDiv);
392
		$sql .= " limit ".$this->paging->start .",". $this->paging->per_page;
393
 
394
		$logList = $this->db->select($sql);
395
		$this->set('pageNo', $info['pageno']);
396
		$this->set('list', $logList);
397
		$this->set('urlParams', $urlParams);
398
 
399
		$this->render('proxy/proxyperfomance');
400
	}
401
 
402
	// function to add proxy to curl handle
403
	public static function addProxyToCurlHandle($curlHandle, $noProxy = false) {
404
		$proxyId = 0;
405
 
406
		// to use proxy if proxy enabled
407
		if (SP_ENABLE_PROXY && !$noProxy) {
408
 
409
			$proxyCtrler = New self();
410
			if ($proxyInfo = $proxyCtrler->getRandomProxy()) {
411
				$proxyId = $proxyInfo['id'];
412
				curl_setopt($curlHandle, CURLOPT_PROXY, $proxyInfo['proxy'].":".$proxyInfo['port']);
413
 
414
				if (CURLOPT_HTTPPROXYTUNNEL_VAL) {
415
					curl_setopt($curlHandle, CURLOPT_HTTPPROXYTUNNEL, CURLOPT_HTTPPROXYTUNNEL_VAL);
416
				}
417
 
418
				if (!empty($proxyInfo['proxy_auth'])) {
419
					curl_setopt ($curlHandle, CURLOPT_PROXYUSERPWD, $proxyInfo['proxy_username'].":".$proxyInfo['proxy_password']);
420
				}
421
 
422
			} else {
423
				showErrorMsg("No active proxies found!! Please check your proxy settings from Admin Panel.");
424
			}
425
 
426
		}
427
 
428
		// to fix the ssl related issues
429
		curl_setopt($curlHandle, CURLOPT_SSL_VERIFYHOST, 0);
430
		curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, 0);
431
 
432
		return array($curlHandle, $proxyId);
433
 
434
	}
435
 
436
	// changes for proxy after curl execute
437
	public static function processProxyStatus($ret, $proxyId) {
438
 
439
		// disable proxy if not working
440
		if (SP_ENABLE_PROXY && $proxyId && !empty($ret['error'])) {
441
			$proxyCtrler = New self();
442
 
443
			// deactivate proxy
444
			if (PROXY_DEACTIVATE_CRAWL) {
445
				$proxyCtrler->__changeStatus($proxyId, 0);
446
			}
447
 
448
		}
449
 
450
	}
451
 
452
}
453
?>