Advertisement
Guest User

Untitled

a guest
Dec 20th, 2012
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.31 KB | None | 0 0
  1. <?php
  2. // Copyright 2004-present Facebook. All Rights Reserved.
  3.  
  4. $STREAM_XML = '<stream:stream '.
  5.   'xmlns:stream="http://etherx.jabber.org/streams" '.
  6.   'version="1.0" xmlns="jabber:client" to="chat.facebook.com" '.
  7.   'xml:lang="en" xmlns:xml="http://www.w3.org/XML/1998/namespace">';
  8.  
  9. $AUTH_XML = '<auth xmlns="urn:ietf:params:xml:ns:xmpp-sasl" '.
  10.   'mechanism="X-FACEBOOK-PLATFORM"></auth>';
  11.  
  12. $CLOSE_XML = '</stream:stream>';
  13.  
  14. $RESOURCE_XML = '<iq type="set" id="3">'.
  15.   '<bind xmlns="urn:ietf:params:xml:ns:xmpp-bind">'.
  16.   '<resource>fb_xmpp_script</resource></bind></iq>';
  17.  
  18. $SESSION_XML = '<iq type="set" id="4" to="chat.facebook.com">'.
  19.   '<session xmlns="urn:ietf:params:xml:ns:xmpp-session"/></iq>';
  20.  
  21. $START_TLS = '<starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"/>';
  22.  
  23.  
  24. function open_connection($server) {
  25.   print "[INFO] Opening connection... ";
  26.  
  27.   $fp = fsockopen($server, 5222, $errno, $errstr);
  28.   if (!$fp) {
  29.     print "$errstr ($errno)<br>";
  30.   } else {
  31.     print "connnection open<br>";
  32.   }
  33.  
  34.   return $fp;
  35. }
  36.  
  37. function send_xml($fp, $xml) {
  38.   fwrite($fp, $xml);
  39. }
  40.  
  41. function recv_xml($fp,  $size=4096) {
  42.   $xml = fread($fp, $size);
  43.   if ($xml === "") {
  44.      return null;
  45.   }
  46.  
  47.   // parses xml
  48.   $xml_parser = xml_parser_create();
  49.   xml_parse_into_struct($xml_parser, $xml, $val, $index);
  50.   xml_parser_free($xml_parser);
  51.  
  52.   return array($val, $index);
  53. }
  54.  
  55. function find_xmpp($fp,  $tag, $value=null, &$ret=null) {
  56.   static $val = null, $index = null;
  57.  
  58.   do {
  59.     if ($val === null && $index === null) {
  60.       list($val, $index) = recv_xml($fp);
  61.       if ($val === null || $index === null) {
  62.         return false;
  63.       }
  64.     }
  65.  
  66.     foreach ($index as $tag_key => $tag_array) {
  67.       if ($tag_key === $tag) {
  68.         if ($value === null) {
  69.           if (isset($val[$tag_array[0]]['value'])) {
  70.             $ret = $val[$tag_array[0]]['value'];
  71.           }
  72.           return true;
  73.         }
  74.         foreach ($tag_array as $i => $pos) {
  75.           if ($val[$pos]['tag'] === $tag && isset($val[$pos]['value']) &&
  76.             $val[$pos]['value'] === $value) {
  77.               $ret = $val[$pos]['value'];
  78.               return true;
  79.           }
  80.         }
  81.       }
  82.     }
  83.     $val = $index = null;
  84.   } while (!feof($fp));
  85.  
  86.   return false;
  87. }
  88.  
  89.  
  90. function xmpp_connect($options, $access_token) {
  91.   global $STREAM_XML, $AUTH_XML, $RESOURCE_XML, $SESSION_XML, $CLOSE_XML, $START_TLS;
  92.  
  93.   $fp = open_connection($options['server']);
  94.   if (!$fp) {
  95.     return false;
  96.   }
  97.  
  98.   // initiates auth process (using X-FACEBOOK_PLATFORM)
  99.   send_xml($fp,  $STREAM_XML);
  100.   echo "-----------------".print_r($fp)."-----------------";
  101.   if (!find_xmpp($fp, 'STREAM:STREAM')) {
  102.     return false;
  103.   }
  104.    echo "2";
  105.   if (!find_xmpp($fp,  'MECHANISM', 'X-FACEBOOK-PLATFORM')) {
  106.     return false;
  107.   }
  108.  
  109.   // starting tls - MANDATORY TO USE OAUTH TOKEN!!!!
  110.   send_xml($fp,  $START_TLS);
  111.   if (!find_xmpp($fp, 'PROCEED', null, $proceed)) {
  112.     return false;
  113.   }
  114.   stream_socket_enable_crypto($fp, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);
  115.  
  116.   send_xml($fp, $STREAM_XML);
  117.   if (!find_xmpp($fp, 'STREAM:STREAM')) {
  118.     return false;
  119.   }
  120.   if (!find_xmpp($fp, 'MECHANISM', 'X-FACEBOOK-PLATFORM')) {
  121.     return false;
  122.   }
  123.  
  124.   // gets challenge from server and decode it
  125.   send_xml($fp, $AUTH_XML);
  126.   if (!find_xmpp($fp,  'CHALLENGE', null, $challenge)) {
  127.     return false;
  128.   }
  129.   $challenge = base64_decode($challenge);
  130.   $challenge = urldecode($challenge);
  131.   parse_str($challenge, $challenge_array);
  132.  
  133.   // creates the response array
  134.   $resp_array = array(
  135.     'method' => $challenge_array['method'],
  136.     'nonce' => $challenge_array['nonce'],
  137.     'access_token' => $access_token,
  138.     'api_key' => $options['app_id'],
  139.     'call_id' => 0,
  140.     'v' => '1.0',
  141.   );
  142.   // creates signature
  143.   $response = http_build_query($resp_array);
  144.  
  145.   // sends the response and waits for success
  146.   $xml = '<response xmlns="urn:ietf:params:xml:ns:xmpp-sasl">'.
  147.     base64_encode($response).'</response>';
  148.   send_xml($fp, $xml);
  149.   if (!find_xmpp($fp, 'SUCCESS')) {
  150.     return false;
  151.   }
  152.  
  153.   // finishes auth process
  154.   send_xml($fp, $STREAM_XML);
  155.   if (!find_xmpp($fp,'STREAM:STREAM')) {
  156.     return false;
  157.   }
  158.   if (!find_xmpp($fp, 'STREAM:FEATURES')) {
  159.     return false;
  160.   }
  161.  send_xml($fp, $RESOURCE_XML);
  162.   if (!find_xmpp($fp, 'JID')) {
  163.     return false;
  164.   }
  165.   send_xml($fp, $SESSION_XML);
  166.   if (!find_xmpp($fp, 'SESSION')) {
  167.     return false;
  168.   }
  169.  
  170.   // we made it!
  171.   send_xml($fp, $CLOSE_XML);
  172.   print ("Authentication complete<br>");
  173.   send_xml($fp, "<message from='cirobsb.USER1@chat.facebook.com' to='USER2@chat.facebook.com' type='chat'>
  174.    <body>teste</body>
  175. </message>");
  176.   fclose($fp);
  177.  
  178.   return true;
  179. }
  180.  
  181.  
  182.  
  183. //Gets access_token with xmpp_login permission
  184. function get_access_token($app_id, $app_secret, $my_url){
  185.    
  186.   $code = $_REQUEST["code"];
  187.  
  188.   if(empty($code)) {
  189.     $dialog_url = "https://www.facebook.com/dialog/oauth?scope=xmpp_login".
  190.      "&client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url) ;
  191.     echo("<script>top.location.href='" . $dialog_url . "'</script>");
  192.   }
  193.    $token_url = "https://graph.facebook.com/oauth/access_token?client_id="
  194.     . $app_id . "&redirect_uri=" . urlencode($my_url)
  195.     . "&client_secret=" . $app_secret
  196.     . "&code=" . $code;
  197.    $access_token = file_get_contents($token_url);
  198.     parse_str($access_token, $output);
  199.    
  200.     return($output['access_token']);
  201. }
  202.  
  203. function _main() {
  204.   print "Test platform connect for XMPP<br>";
  205.   $app_id='APP_ID';
  206.   $app_secret='APP_SECRET';
  207.   $my_url = "APP_URL";
  208.   $uid = 'USER_SENDER';
  209.   //$access_token = get_access_token($app_id,$app_secret,$my_url);
  210.   $access_token = "APP_TOKEN";
  211.   print "access_token: ".$access_token."<br>";
  212.  
  213.   $options = array(
  214.     'uid' => $uid,
  215.     'app_id' => $app_id,
  216.     'server' => 'chat.facebook.com',
  217.      "host"=>"chat.facebook.com",  
  218.      "port"=>5222,  
  219.      "domain"=>"chat.facebook.com"  
  220.    );
  221.  
  222.   // prints options used
  223.   print "server: ".$options['server']."<br>";
  224.   print "uid: ".$options['uid']."<br>";
  225.   print "app id: ".$options['app_id']."<br>";
  226.  
  227.   if (xmpp_connect($options, $access_token)) {
  228.    
  229.     print "Done<br>mandando mensagem";
  230.     //$fp = open_connection($options['server']);
  231.      
  232.   } else {
  233.     print "An error ocurred<br>";
  234.   }
  235.  
  236. }
  237.  
  238. _main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement