forked from Luccifer/FB-Source-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmppMessage.php.txt
248 lines (199 loc) · 6.21 KB
/
xmppMessage.php.txt
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
class xmppMessage {
// Copyright 2004-present Facebook. All Rights Reserved.
private $STREAM_XML = '<stream:stream xmlns:stream="http://etherx.jabber.org/streams" version="1.0" xmlns="jabber:client" to="chat.facebook.com" xml:lang="en" xmlns:xml="http://www.w3.org/XML/1998/namespace">';
private $AUTH_XML = '<auth xmlns="urn:ietf:params:xml:ns:xmpp-sasl" mechanism="X-FACEBOOK-PLATFORM"></auth>';
private $CLOSE_XML = '</stream:stream>';
private $RESOURCE_XML = '<iq type="set" id="3"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"><resource>fb_xmpp_script</resource></bind></iq>';
private $SESSION_XML = '<iq type="set" id="4" to="chat.facebook.com"><session xmlns="urn:ietf:params:xml:ns:xmpp-session"/></iq>';
private $START_TLS = '<starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"/>';
private function open_connection($server) {
print "[INFO] Opening connection... ";
$fp = fsockopen($server, 5222, $errno, $errstr);
if (!$fp) {
print "$errstr ($errno)<br>";
} else {
print "connnection open<br>";
}
return $fp;
}
private function send_xml($fp, $xml) {
fwrite($fp, $xml);
}
private function recv_xml($fp, $size=4096) {
$xml = fread($fp, $size);
if ($xml === "") {
return null;
}
// parses xml
$xml_parser = xml_parser_create();
xml_parse_into_struct($xml_parser, $xml, $val, $index);
xml_parser_free($xml_parser);
while (count($val) == 0) {
// The parse failed. We probably got a partial packet. Read more
$xml .= fread($fp, $size);
// Have to create a new parser each time the XML changes.
$xml_parser = xml_parser_create();
xml_parse_into_struct($xml_parser, $xml, $val, $index);
xml_parser_free($xml_parser);
}
return array($val, $index);
}
private function find_xmpp($fp, $tag, $value=null, &$ret=null) {
static $val = null, $index = null;
do {
if ($val === null && $index === null) {
list($val, $index) = $this->recv_xml($fp);
if ($val === null || $index === null) {
return false;
}
}
foreach ($index as $tag_key => $tag_array) {
if ($tag_key === $tag) {
if ($value === null) {
if (isset($val[$tag_array[0]]['value'])) {
$ret = $val[$tag_array[0]]['value'];
}
return true;
}
foreach ($tag_array as $i => $pos) {
if ($val[$pos]['tag'] === $tag && isset($val[$pos]['value']) &&
$val[$pos]['value'] === $value) {
$ret = $val[$pos]['value'];
return true;
}
}
}
}
$val = $index = null;
} while (!feof($fp));
return false;
}
private function xmpp_connect($options, $access_token) {
$fp = $this->open_connection($options['server']);
if (!$fp) {
return false;
}
else
echo "111111";
// initiates auth process (using X-FACEBOOK_PLATFORM)
$this->send_xml($fp, $this->STREAM_XML);
if (!$this->find_xmpp($fp, 'STREAM:STREAM')) {
return false;
}
else
echo "2222222";
if (!$this->find_xmpp($fp, 'MECHANISM', 'X-FACEBOOK-PLATFORM')) {
return false;
}
else
echo "33333333";
// starting tls - MANDATORY TO USE OAUTH TOKEN!!!!
$this->send_xml($fp, $this->START_TLS);
if (!$this->find_xmpp($fp, 'PROCEED', null, $proceed)) {
return false;
}
else
echo "444444444";
stream_socket_enable_crypto($fp, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);
$this->send_xml($fp, $this->STREAM_XML);
if (!$this->find_xmpp($fp, 'STREAM:STREAM')) {
return false;
}
else
echo "55555555555";
if (!$this->find_xmpp($fp, 'MECHANISM', 'X-FACEBOOK-PLATFORM')) {
return false;
} else
echo "66666666666";
// gets challenge from server and decode it
$this->send_xml($fp, $this->AUTH_XML);
if (!$this->find_xmpp($fp, 'CHALLENGE', null, $challenge)) {
return false;
}
else
echo "77777777777";
$challenge = base64_decode($challenge);
$challenge = urldecode($challenge);
parse_str($challenge, $challenge_array);
// creates the response array
$resp_array = array(
'method' => $challenge_array['method'],
'nonce' => $challenge_array['nonce'],
'access_token' => $access_token,
'api_key' => $options['app_id'],
'call_id' => 0,
'v' => '1.0',
);
// creates signature
$response = http_build_query($resp_array);
// sends the response and waits for success
$xml = '<response xmlns="urn:ietf:params:xml:ns:xmpp-sasl">'.
base64_encode($response).'</response>';
$this->send_xml($fp, $xml);
if (!$this->find_xmpp($fp, 'SUCCESS')) {
return false;
}
else
echo "8888888888";
// finishes auth process
$this->send_xml($fp, $this->STREAM_XML);
if (!$this->find_xmpp($fp,'STREAM:STREAM')) {
return false;
}
else
echo "99999999999";
if (!$this->find_xmpp($fp, 'STREAM:FEATURES')) {
return false;
}
else
echo "10101010";
$this->send_xml($fp, $this->RESOURCE_XML);
if (!$this->find_xmpp($fp, 'JID')) {
return false;
}
else
echo "11111111";
$this->send_xml($fp, $this->SESSION_XML);
if (!$this->find_xmpp($fp, 'SESSION')) {
return false;
}
else
echo "12121212121212";
$MESSAGE = '<message from="-' . $options['uid'] . '@chat.facebook.com" to="[email protected]"><body>Testmessage</body></message>';
$this->send_xml($fp, $MESSAGE);
if (!$this->find_xmpp($fp, 'BODY')) {
return false;
}
else
echo "1313131313";
// we made it!
$this->send_xml($fp, $this->CLOSE_XML);
print ("Authentication complete<br>");
fclose($fp);
return true;
}
public function sendMessage() {
print "Test platform connect for XMPP<br>";
$app_id='xxxxxxxxxx';
$uid = '1000009533542xxx';
$access_token = user::$currentUserFbAccessToken;
$options = array(
'uid' => $uid,
'app_id' => $app_id,
'server' => 'chat.facebook.com',
);
// prints options used
print "server: ".$options['server']."<br>";
print "uid: ".$options['uid']."<br>";
print "app id: ".$options['app_id']."<br>";
print "token: ".user::$currentUserFbAccessToken."<br>";
if (empty(user::$currentUserFbAccessToken))
return false;
if ($this->xmpp_connect($options, $access_token)) {
print "Done<br>";
} else {
print "An error ocurred<br>";
}
}
}