From eaf949de8861c6b243cc587ed922ac12c5d1a6fa Mon Sep 17 00:00:00 2001 From: fritzy Date: Fri, 28 Nov 2008 19:43:05 +0000 Subject: [PATCH] * added vcard support git-svn-id: svn://netflint.net/xmpphp@71 ef36c318-a008-4979-b6e8-6b496270793b --- README | 4 ---- XMPPHP/XMPP.php | 41 +++++++++++++++++++++++++++++++++++++++-- cli_longrun_example.php | 37 +++++++++++++++++++++++++++++++++++-- 3 files changed, 74 insertions(+), 8 deletions(-) diff --git a/README b/README index 00d2ff5..0c2f53d 100644 --- a/README +++ b/README @@ -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 =============================================================================== diff --git a/XMPPHP/XMPP.php b/XMPPHP/XMPP.php index e88441f..ed7f55b 100644 --- a/XMPPHP/XMPP.php +++ b/XMPPHP/XMPP.php @@ -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(""); + } else { + $this->send(""); + } + } + + /** + * 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); + } } diff --git a/cli_longrun_example.php b/cli_longrun_example.php index 6c40319..ce7218b 100644 --- a/cli_longrun_example.php +++ b/cli_longrun_example.php @@ -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(""); + if($cmd[0] == 'quit') $conn->disconnect(); + if($cmd[0] == 'break') $conn->send(""); + 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; } } }