-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathInstagram.php
221 lines (186 loc) · 6.9 KB
/
Instagram.php
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/*
* Pace Usa Offical Bot © 2013 Made This
*
* developerpaceusa@gmail.com -
*
*/
require_once 'config.php';
class Instagram
{
protected $username;
protected $password;
protected $uuid;
protected $device_id;
protected $username_id;
protected $token;
protected $isLoggedIn = false;
protected $rank_token;
public $IGDataPath;
public function __construct()
{
global $Pace;
$this->username = $FADILUS['ig'][0];
$this->password = $FADILUS['ig'][1];
$this->uuid = $this->generateUUID(true);
$this->device_id = $this->generateDeviceId(md5($FADILUS['ig'][0].$Pace['ig'][1]));
if (!file_exists( 'cache' )) {
mkdir('cache/' , 0777);
}
$this->IGDataPath = 'cache/';
if ((file_exists($this->IGDataPath."$this->username-cookies.log")) && (file_exists($this->IGDataPath."$this->username-userId.log"))
&& (file_exists($this->IGDataPath."$this->username-token.log"))) {
$this->isLoggedIn = true;
$this->username_id = trim(file_get_contents($this->IGDataPath."$this->username-userId.log"));
$this->rank_token = $this->username_id.'_'.$this->uuid;
$this->token = trim(file_get_contents($this->IGDataPath."$this->username-token.log"));
}
}
public function login()
{
if (!$this->isLoggedIn) {
$fetch = $this->request('si/fetch_headers/?challenge_type=signup&guid='.$this->generateUUID(false), null, true);
preg_match('#Set-Cookie: csrftoken=([^;]+)#', $fetch[0], $token);
$data = [
'device_id' => $this->device_id,
'guid' => $this->uuid,
'phone_id' => $this->generateUUID(true),
'username' => $this->username,
'password' => $this->password,
'login_attempt_count' => '0',
];
$login = $this->request('accounts/login/', $this->generateSignature(json_encode($data)), true);
$this->isLoggedIn = true;
$this->username_id = $login[1]['logged_in_user']['pk'];
file_put_contents($this->IGDataPath.$this->username.'-userId.log', $this->username_id);
$this->rank_token = $this->username_id.'_'.$this->uuid;
preg_match('#Set-Cookie: csrftoken=([^;]+)#', $login[0], $match);
$this->token = $match[1];
file_put_contents($this->IGDataPath.$this->username.'-token.log', $this->token);
return $login[1];
}
}
public function timelineFeed()
{
return $this->request('feed/timeline/')[1];
}
public function getRecentActivity()
{
$activity = $this->request('news/inbox/?')[1];
return $activity;
}
public function getUserId()
{
return $this->username_id;
}
public function getUserFeed($usernameId)
{
$userFeed = $this->request("feed/user/$usernameId/?rank_token=$this->rank_token&ranked_content=true&")[1];
return $userFeed;
}
public function like($mediaId)
{
$data = json_encode([
'_uuid' => $this->uuid,
'_uid' => $this->username_id,
'_csrftoken' => $this->token,
'media_id' => $mediaId,
]);
return $this->request("media/$mediaId/like/", $this->generateSignature($data))[1];
}
public function comment($mediaId, $commentText)
{
$data = json_encode([
'_uuid' => $this->uuid,
'_uid' => $this->username_id,
'_csrftoken' => $this->token,
'comment_text' => $commentText,
]);
return $this->request("media/$mediaId/comment/", $this->generateSignature($data))[1];
}
public function follow($userId)
{
$data = json_encode([
'_uuid' => $this->uuid,
'_uid' => $this->username_id,
'user_id' => $userId,
'_csrftoken' => $this->token,
]);
return $this->request("friendships/create/$userId/", $this->generateSignature($data))[1];
}
public function generateSignature($data)
{
global $Pace;
$hash = hash_hmac('sha256', $data, $FADILUS['ig'][4]);
return 'ig_sig_key_version=4&signed_body='.$hash.'.'.urlencode($data);
}
public function generateDeviceId($seed)
{
$volatile_seed = filemtime(__DIR__);
return 'android-'.substr(md5($seed.$volatile_seed), 16);
}
public function generateUUID($type)
{
$uuid = sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0x0fff) | 0x4000,
mt_rand(0, 0x3fff) | 0x8000,
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
);
return $type ? $uuid : str_replace('-', '', $uuid);
}
protected function buildBody($bodies, $boundary)
{
$body = '';
foreach ($bodies as $b) {
$body .= '--'.$boundary."\r\n";
$body .= 'Content-Disposition: '.$b['type'].'; name="'.$b['name'].'"';
if (isset($b['filename'])) {
$ext = pathinfo($b['filename'], PATHINFO_EXTENSION);
$body .= '; filename="'.'pending_media_'.number_format(round(microtime(true) * 1000), 0, '', '').'.'.$ext.'"';
}
if (isset($b['headers']) && is_array($b['headers'])) {
foreach ($b['headers'] as $header) {
$body .= "\r\n".$header;
}
}
$body .= "\r\n\r\n".$b['data']."\r\n";
}
$body .= '--'.$boundary.'--';
return $body;
}
protected function request($endpoint, $post = null, $login = false)
{
global $FADILUS;
$headers = [
'Connection: close',
'Accept: */*',
'Content-type: application/x-www-form-urlencoded; charset=UTF-8',
'Cookie2: $Version=1',
'Accept-Language: en-US',
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $FADILUS['ig'][2].$endpoint);
curl_setopt($ch, CURLOPT_USERAGENT, $FADILUS['ig'][3]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_COOKIEFILE, $this->IGDataPath."$this->username-cookies.log");
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->IGDataPath."$this->username-cookies.log");
if ($post) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}
$resp = curl_exec($ch);
$header_len = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($resp, 0, $header_len);
$body = substr($resp, $header_len);
curl_close($ch);
return [$header, json_decode($body, true)];
}
}