php class Spaces extends Logger protected ch hits_per_interval request

  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
<?php
class Spaces extends Logger {
protected $ch, $hits_per_interval = 3, $request_interval = 1, $last_request_time = 0, $hits_count = 0, $old_data, $account = array();
const HOST = 'http://spaces.ru';
const USERAGENT = 'Opera/9.80 (J2ME/MIDP; Opera Mini/4.2.24721/30.3316; U; ru) Presto/2.8.119 Version/11.10';
public function __construct() {
$this->ch = curl_init();
curl_setopt_array($this->ch, array(
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_HEADER => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_USERAGENT => self::USERAGENT
));
}
public function auth() {
switch (func_num_args()) {
case 1:
if(preg_match('/^\d{16}$/', func_get_arg(0))) {
curl_setopt($this->ch, CURLOPT_COOKIE, 'sid=' . func_get_arg(0));
$this->account['sid'] = func_get_arg(0);
$res = $this->request('/mysite/');
if(!preg_match('/Активность/', $res))
$this->toLog('Невозможно авторизоваться!', self::ERROR);
else
$this->toLog('Успешная авторизация!');
} elseif(preg_match('/^[0-9a-f]{32}$/', func_get_arg(0))) {
$res = $this->request('/tm/' . func_get_arg(0));
preg_match('/sid=(\d+)/', $res, $data);
$this->account['tm'] = func_get_arg(0);
$this->account['sid'] = $data[1];
$this->auth($this->account['sid']);
} else
$this->toLog('Неверные данные для входа', self::ERROR);
break;
case 2:
if(preg_match('/^\d{16}$/', func_get_arg(0)) && preg_match('/^\d{4}$/', func_get_arg(1))) {
curl_setopt($this->ch, CURLOPT_COOKIE, 'sid=' . func_get_arg(0));
$this->account['sid'] = func_get_arg(0);
$this->account['pn_nums'] = func_get_arg(1);
$res = $this->request('/mysite/');
if(!preg_match('/Активность/', $res))
$this->toLog('Невозможно авторизоваться!', self::ERROR);
else
$this->toLog('Успешная авторизация!');
} elseif(preg_match('/^[0-9a-f]{32}$/', func_get_arg(0)) && preg_match('/^\d{4}$/', func_get_arg(1))) {
$res = $this->request('/tm/' . func_get_arg(0));
preg_match('/sid=(\d+)/', $res, $data);
$this->account['tm'] = func_get_arg(0);
$this->account['pn_nums'] = func_get_arg(1);
$this->account['sid'] = $data[1];
$this->auth($this->account['sid'], $this->account['pn_nums']);
} else {
$res = $this->request('/registration/?r=reg/loginform');
preg_match('/name="CK" value="(\d+)"/', $res, $ck);
preg_match('/sid=(\d+)/', $res, $sid);
$ck = $ck[1];
$sid = $sid[1];
$res = $this->request('/registration/?r=reg/loginform', array(
'contact' => func_get_arg(0),
'password' => func_get_arg(1),
'CK' => $ck,
'cfms' => 'Auth'
));
echo $res;
}
break;
}
}
private function pn_nums() {
$this->toLog('Проверка последних 4 цифр номера...');
if(array_key_exists('pn_nums', $this->account))
$this->request('/mysite/?', array(
'pn_nums' => $this->account['pn_nums']
));
else
$this->toLog('Введите последние 4 цифры номера для аккаунта', self::ERROR);
$this->toLog('Успех!');
}
private function request($link, $post = false) {
$time_delta = microtime() - $this->last_request_time;
if($time_delta < $this->request_interval) {
if($this->hits_count >= $this->hits_per_interval){
usleep(($this->request_interval - $time_delta) * 1000000);
$this->hits_count = 0;
}
}else
$this->hits_count = 0;
if($post)
curl_setopt_array($this->ch, array(
CURLOPT_URL => self::HOST . $link,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $post
));
else
curl_setopt($this->ch, CURLOPT_URL, self::HOST . $link);
$res = curl_exec($this->ch);
if($post)
curl_setopt($this->ch, CURLOPT_POST, false);
$this->last_request_time = microtime();
++$this->hits_count;
if(preg_match('/неверные цифры/', $res))
$this->toLog('Введите верные последние 4 цифры номера!', self::ERROR);
elseif(preg_match('/превысили допустимое/', $res))
$this->toLog('Превышено допустимое количество вводов последних цифр номера.', self::ERROR);
elseif(preg_match('/name="pn_nums" size="10"/', $res)) {
$this->pn_nums();
$res = $this->request($link, $post);
}
if(curl_errno($this->ch) == 28)
$this->toLog('Нет связи с сервером!', self::ERROR);
return $res;
}
}