php class Spaces protected ch error false action null hits_per_interva

  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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
class Spaces {
protected $ch, $error = false, $action = null, $hits_per_interval = 3, $request_interval = 1, $last_request_time = 0, $hits_count = 0, $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';
protected $pn_valid = false;
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))
return false;
else
return true;
} 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
return false;
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))
return false;
else
return true;
} 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
return false;
}
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
return false;
break;
}
return true;
}
public function fileUpload($file) {
if(!array_key_exists('path', $file) || !file_exists($file['path']))
return false;
if($this->action === null) {
$res = $this->request('/files/?r=main/upload');
preg_match('/action="(.*?)"/', $res, $action);
$this->action = htmlspecialchars_decode($action[1]);
}
$res = $this->request($this->action, array(
'myFile' => '@' . $file['path'] . (array_key_exists('name', $file) ? ';filename=' . $file['name'] : ''),
'dir' => array_key_exists('dir', $file) ? (String) $file['dir'] : '0',
'file_descr' => array_key_exists('descr', $file) ? $file['descr'] : '',
'compat' => array_key_exists('compat', $file) ? $file['compat'] : '',
'adult' => array_key_exists('adult', $file) ? (String) $file['adult'] : '0'
));
if(!preg_match('/document has moved/', $res))
return false;
preg_match('/read=(\d+)/i', $res, $read);
$read = $read[1];
return $read;
}
public function screenUpload($fid, $screen) {
$res = $this->request('http://spaces.ru/files/?r=screenshot/add&Add_screen=' . $fid);
preg_match('/action="(.*?)"/', $res, $action);
preg_match('/"screen_md5sum" value="([a-f0-9]{32})"/', $res, $screen_md5sum);
preg_match('/"screen_sig" value="([a-f0-9]{32})"/', $res, $screen_sig);
$res = $this->request(htmlspecialchars_decode($action[1]), array(
'myScreen' => '@' . $screen . ';type=image/jpeg',
'screen_md5sum' => $screen_md5sum[1],
'screen_sig' => $screen_sig[1],
'screen_file_id' => $fid
));
if(preg_match('/document has moved/', $res))
return true;
else
return false;
}
public function toSZ($fid, $cid) {
$res = $this->request('/shared_zone/?file_add=' . $fid . '&dir=' . $cid);
preg_match('/CK=(\d+)/', $res, $CK);
$res = $this->request('/shared_zone/?file_add=' . $fid . '&dir=' . $cid . '&CK=' . $CK[1]);
if(preg_match('/document has moved/', $res))
return true;
else
return false;
}
private function pn_nums() {
if(array_key_exists('pn_nums', $this->account))
$this->request('/mysite/?', array(
'pn_nums' => $this->account['pn_nums']
));
else
return false;
return true;
}
public function checkPnNums() {
if($this->request('/mysite/') || $this->pn_valid)
return true;
return false;
}
public function logout() {
$this->request('/?logout=1');
curl_setopt($this->ch, CURLOPT_COOKIE, false);
$this->pn_valid = false;
}
public function request($link, $post = false, $guest = 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(!$guest) {
if(preg_match('/неверные цифры/', $res))
return false;
elseif(preg_match('/превысили допустимое/', $res))
return false;
elseif(preg_match('/name="pn_nums" size="10"/', $res)) {
if(!$this->pn_nums())
return false;
$res = $this->request($link, $post);
}
}
if(curl_errno($this->ch) == 28)
return false;
return $res;
}
}