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
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
<?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->auth($data[1]);
} 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->auth($data[1], func_get_arg(1));
} else {
$this->account['login'] = func_get_arg(0);
$this->account['password'] = func_get_arg(1);
$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'
));
if(preg_match('/\/tm\/[0-9a-f]{32}/', $res)) {
preg_match('/\/tm\/([0-9a-f]{32})/', $res, $tm);
$this->auth($tm[1]);
} else
$this->toLog('Неверный логин или пароль!', self::ERROR);
}
break;
case 3:
$this->account['login'] = func_get_arg(0);
$this->account['password'] = func_get_arg(1);
$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'
));
if(preg_match('/\/tm\/[0-9a-f]{32}/', $res)) {
preg_match('/\/tm\/([0-9a-f]{32})/', $res, $tm);
$this->auth($tm[1], func_get_arg(2));
} else
$this->toLog('Неверный логин или пароль!', self::ERROR);
break;
}
}
public function fileUpload() {
$res = $this->request('/files/?r=main/upload');
preg_match('/action="(.*?)"/', $res, $action);
$action = htmlspecialchars_decode($action[1]);
echo getcwd();
echo $this->request($action, array(
'myFile' => '@xuj.txt'
));
echo curl_error($this->ch);
}
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 => !preg_match('/^http/', $link) ? self::HOST . $link : $link,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $post
));
else
curl_setopt($this->ch, CURLOPT_URL, !preg_match('/^http/', $link) ? self::HOST . $link : $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;
}
}