* added vcard support

git-svn-id: svn://netflint.net/xmpphp@71 ef36c318-a008-4979-b6e8-6b496270793b
This commit is contained in:
fritzy 2008-11-28 19:43:05 +00:00
parent b13761eb26
commit eaf949de88
3 changed files with 74 additions and 8 deletions

4
README
View File

@ -31,12 +31,8 @@ documentation on the website.
TODO
================================================================================
* Rosters (I know, a big one)
* Documentation
* TLS Cert Inspection (Even possible in PHP?)
* MUC Support
* Plugins
* XML Masking Handlers
License Exception
===============================================================================

View File

@ -45,12 +45,12 @@ class XMPPHP_XMPP extends XMPPHP_XMLStream {
/**
* @var string
*/
protected $server;
public $server;
/**
* @var string
*/
protected $user;
public $user;
/**
* @var string
@ -380,4 +380,41 @@ class XMPPHP_XMPP extends XMPPHP_XMLStream {
stream_socket_enable_crypto($this->socket, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT);
$this->reset();
}
/**
* Retrieves the vcard
*
*/
public function getVCard($jid = Null) {
$id = $this->getID();
$this->addIdHandler($id, 'vcard_get_handler');
if($jid) {
$this->send("<iq type='get' id='$id' to='$jid'><vCard xmlns='vcard-temp' /></iq>");
} else {
$this->send("<iq type='get' id='$id'><vCard xmlns='vcard-temp' /></iq>");
}
}
/**
* VCard retrieval handler
*
* @param XML Object $xml
*/
protected function vcard_get_handler($xml) {
$vcard_array = array();
$vcard = $xml->sub('vcard');
// go through all of the sub elements and add them to the vcard array
foreach ($vcard->subs as $sub) {
if ($sub->subs) {
$vcard_array[$sub->name] = array();
foreach ($sub->subs as $sub_child) {
$vcard_array[$sub->name][$sub_child->name] = $sub_child->data;
}
} else {
$vcard_array[$sub->name] = $sub->data;
}
}
$vcard_array['from'] = $xml->attrs['from'];
$this->event('vcard', $vcard_array);
}
}

View File

@ -10,6 +10,8 @@ include 'XMPPHP/XMPP.php';
$conn = new XMPPHP_XMPP('talk.google.com', 5222, 'username', 'password', 'xmpphp', 'gmail.com', $printlog=true, $loglevel=XMPPHP_Log::LEVEL_INFO);
$conn->autoSubscribe();
$vcard_request = array();
try {
$conn->connect();
while(!$conn->isDisconnected()) {
@ -24,8 +26,15 @@ try {
print $pl['body'] . "\n";
print "---------------------------------------------------------------------------------\n";
$conn->message($pl['from'], $body="Thanks for sending me \"{$pl['body']}\".", $type=$pl['type']);
if($pl['body'] == 'quit') $conn->disconnect();
if($pl['body'] == 'break') $conn->send("</end>");
if($cmd[0] == 'quit') $conn->disconnect();
if($cmd[0] == 'break') $conn->send("</end>");
if($cmd[0] == 'vcard') {
if(!($cmd[1])) $cmd[1] = $conn->user . '@' . $conn->server;
// take a note which user requested which vcard
$vcard_request[$pl['from']] = $cmd[1];
// request the vcard
$conn->getVCard($cmd[1]);
}
break;
case 'presence':
print "Presence: {$pl['from']} [{$pl['show']}] {$pl['status']}\n";
@ -35,6 +44,30 @@ try {
$conn->getRoster();
$conn->presence($status="Cheese!");
break;
case 'vcard':
// check to see who requested this vcard
$deliver = array_keys($vcard_request, $pl['from']);
// work through the array to generate a message
print_r($pl);
$msg = '';
foreach($pl as $key => $item) {
$msg .= "$key: ";
if(is_array($item)) {
$msg .= "\n";
foreach($item as $subkey => $subitem) {
$msg .= " $subkey: $subitem\n";
}
} else {
$msg .= "$item\n";
}
}
// deliver the vcard msg to everyone that requested that vcard
foreach($deliver as $sendjid) {
// remove the note on requests as we send out the message
unset($vcard_request[$sendjid]);
$conn->message($sendjid, $msg, 'chat');
}
break;
}
}
}