Ejemplo n.º 1
0
function xmpp_connect($options, $access_token)
{
    global $STREAM_XML, $AUTH_XML, $RESOURCE_XML, $SESSION_XML, $CLOSE_XML, $START_TLS;
    $fp = open_connection($options['server']);
    if (!$fp) {
        return false;
    }
    // initiates auth process (using X-FACEBOOK_PLATFORM)
    send_xml($fp, $STREAM_XML);
    if (!find_xmpp($fp, 'STREAM:STREAM')) {
        return false;
    }
    if (!find_xmpp($fp, 'MECHANISM', 'X-FACEBOOK-PLATFORM')) {
        return false;
    }
    // starting tls - MANDATORY TO USE OAUTH TOKEN!!!!
    send_xml($fp, $START_TLS);
    if (!find_xmpp($fp, 'PROCEED', null, $proceed)) {
        return false;
    }
    stream_socket_enable_crypto($fp, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);
    send_xml($fp, $STREAM_XML);
    if (!find_xmpp($fp, 'STREAM:STREAM')) {
        return false;
    }
    if (!find_xmpp($fp, 'MECHANISM', 'X-FACEBOOK-PLATFORM')) {
        return false;
    }
    // gets challenge from server and decode it
    send_xml($fp, $AUTH_XML);
    if (!find_xmpp($fp, 'CHALLENGE', null, $challenge)) {
        return false;
    }
    $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>';
    send_xml($fp, $xml);
    if (!find_xmpp($fp, 'SUCCESS')) {
        return false;
    }
    // finishes auth process
    send_xml($fp, $STREAM_XML);
    if (!find_xmpp($fp, 'STREAM:STREAM')) {
        return false;
    }
    if (!find_xmpp($fp, 'STREAM:FEATURES')) {
        return false;
    }
    send_xml($fp, $RESOURCE_XML);
    if (!find_xmpp($fp, 'JID')) {
        return false;
    }
    send_xml($fp, $SESSION_XML);
    if (!find_xmpp($fp, 'SESSION')) {
        return false;
    }
    // we made it!
    send_xml($fp, $CLOSE_XML);
    print "Authentication complete<br>";
    fclose($fp);
    return true;
}
function xmpp_send_msg($fp, $options)
{
    global $CLOSE_XML, $MESSAGE_XML;
    echo 'Sent message: <strong>' . $options['msg'] . '</strong>, from ' . $options['uid'] . ' to ' . $options['recv_id'];
    send_xml($fp, sprintf($MESSAGE_XML, $options['uid'], $options['recv_id'], $options['msg']));
    send_xml($fp, $CLOSE_XML);
}