?? jabber.class
字號:
<?php/*************************************************************************** Class.Jabber.PHP v0.1.3.1 (c) 2002 Carlo "Gossip" Zottmann http://phpjabber.g-blog.net *** gossip@jabber.g-blog.net The FULL documentation and examples for this software can be found at http://phpjabber.g-blog.net (not many doc comments in here, sorry) last modified: 2002-10-09 21:57:20 NOTE: If you want to write addons or extensions, please follow the coding style recommendations @ http://www.phpbuilder.net/columns/tim20010101.php3 ***************************************************************************//*************************************************************************** * * The Notice below must appear in each file of the Source Code of any copy * you distribute of the Licensed Product or any Modifications thereto. * Contributors to any Modifications may add their own copyright notices to * identify their own contributions. * * License * * The contents of this file are subject to the Jabber Open Source License * Version 1.0 (the "License"). You may not copy or use this file, in either * source code or executable form, except in compliance with the License. You * may obtain a copy of the License at http://www.jabber.com/license/ or at * http://www.opensource.org/. * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * Copyrights * * Portions created by or assigned to Jabber.com, Inc. are * Copyright (c) 2000 Jabber.com, Inc. All Rights Reserved. Contact * information for Jabber.com, Inc. is available at http://www.jabber.com/. * * Portions Copyright (c) 2002-present Carlo Zottmann, * http://phpjabber.g-blog.net * * Other portions copyright their respective owners. * * Acknowledgements * * Special thanks to the Jabber Open Source Contributors for their * suggestions and support of Jabber. * ***************************************************************************//* Jabber::Connect() Jabber::Disconnect() Jabber::SendAuth() Jabber::AccountRegistration($reg_email {string}, $reg_name {string}) Jabber::Listen() Jabber::SendPacket($xml {string}) Jabber::RosterUpdate() Jabber::RosterAddUser($jid {string}, $id {string}, $name {string}) Jabber::RosterRemoveUser($jid {string}, $id {string}) Jabber::Subscribe($jid {string}) Jabber::Unsubscribe($jid {string}) Jabber::CallHandler($message {array}) Jabber::CruiseControl([$seconds {number}]) Jabber::SubscriptionApproveRequest($to {string}) Jabber::SubscriptionDenyRequest($to {string}) Jabber::GetFirstFromQueue() Jabber::GetFromQueueById($packet_type {string}, $id {string}) Jabber::SendMessage($to {string}, $id {number}, $type {string}, $content {array}[, $payload {array}]) Jabber::SendIq($to {string}, $type {string}, $id {string}, $xmlns {string}[, $payload {string}]) Jabber::SendPresence($type {string}[, $to {string}[, $status {string}[, $show {string}[, $priority {number}]]]]) Jabber::SendError($to {string}, $id {string}, $error_number {number}[, $error_message {string}]) Jabber::GetInfoFromMessageFrom($message {array}) Jabber::GetInfoFromMessageType($message {array}) Jabber::GetInfoFromMessageId($message {array}) Jabber::GetInfoFromMessageThread($message {array}) Jabber::GetInfoFromMessageSubject($message {array}) Jabber::GetInfoFromMessageBody($message {array}) Jabber::GetInfoFromMessageError($message {array}) Jabber::GetInfoFromIqFrom($message {array}) Jabber::GetInfoFromIqType($message {array}) Jabber::GetInfoFromIqId($message {array}) Jabber::GetInfoFromIqKey($message {array}) Jabber::GetInfoFromPresenceFrom($message {array}) Jabber::GetInfoFromPresenceType($message {array}) Jabber::GetInfoFromPresenceStatus($message {array}) Jabber::GetInfoFromPresenceShow($message {array}) Jabber::GetInfoFromPresencePriority($message {array}) MakeXML::AddPacketDetails($string {string}[, $value {string/number}]) MakeXML::BuildPacket([$array {array}])*/class Jabber{ var $server; var $port; var $username; var $password; var $resource; var $jid; var $connection; var $stream_id; var $roster; var $enable_logging; var $logfile; var $iq_sleep_timer; var $packet_queue; var $subscription_queue; var $iq_version_name; var $iq_version_os; var $iq_version_version; var $error_codes; var $CONNECTOR; function Jabber() { $this->server = $GLOBALS['sys_jabber_server']; $this->port = $GLOBALS['sys_jabber_port']; $this->username = $GLOBALS['sys_jabber_user']; $this->password = $GLOBALS['sys_jabber_pass']; $this->resource = 'home'; $this->enable_logging = FALSE; $this->logfile = array(); $this->packet_queue = array(); $this->subscription_queue = array(); $this->iq_sleep_timer = 1; $this->iq_version_name = "Class.Jabber.PHP by Carlo 'Gossip' Zottmann, gossip@jabber.g-blog.net"; $this->iq_version_version = "0.1.3"; $this->iq_version_os = $_SERVER["SERVER_SOFTWARE"]; $this->connection_class = "CJP_StandardConnector"; $this->error_codes = array(400 => "Bad Request", 401 => "Unauthorized", 402 => "Payment Required", 403 => "Forbidden", 404 => "Not Found", 405 => "Not Allowed", 406 => "Not Acceptable", 407 => "Registration Required", 408 => "Request Timeout", 409 => "Conflict", 500 => "Internal Server Error", 501 => "Not Implemented", 502 => "Remove Server Error", 503 => "Service Unavailable", 504 => "Remove Server Timeout", 510 => "Disconnected"); } function Connect() { $this->CONNECTOR = new $this->connection_class; $this->connection = $this->CONNECTOR->OpenSocket($this->server, $this->port); if ($this->connection) { socket_set_blocking($this->connection, 0); socket_set_timeout($this->connection, 31536000); $this->SendPacket("<?xml version='1.0' encoding='UTF-8' ?>\n"); $this->SendPacket("<stream:stream to='" . $this->server . "' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'>\n"); sleep(2); if ($this->_check_connected()) { return TRUE; } else { if ($this->enable_logging) { $this->logfile[] = "<strong>Error:</strong> Connect() #1"; } return FALSE; } } else { if ($this->enable_logging) { $this->logfile[] = "<strong>Error:</strong> Connect() #2"; } return FALSE; } } function Disconnect() { $this->SendPacket("</stream:stream>"); $this->CONNECTOR->CloseSocket($this->connection); if ($this->enable_logging) { echo "<h2>logging enabled, logged events below:</h2>\n"; echo (count($this->logfile) > 0) ? implode("<br /><br />\n", $this->logfile) : "No logged events."; } } function SendAuth() { // Currently, we only support plaintext authentication. This ain't // perfect, but it works. I'll add <digest/> support later on... if ($this->resource) { $this->jid = $this->username . "@" . $this->server . "/" . $this->resource; } else { $this->jid = $this->username . "@" . $this->server; } $auth_id = "auth_" . time(); $payload = "<username>" . $this->username . "</username> <password>" . $this->password . "</password> <resource>" . $this->resource . "</resource>"; $packet = $this->SendIq(NULL, "set", $auth_id, "jabber:iq:auth", $payload); if ($this->GetInfoFromIqType($packet) == "result" && $this->GetInfoFromIqId($packet) == $auth_id) { return TRUE; } else { if ($this->enable_logging) { $this->logfile[] = "<strong>Error:</strong> SendAuth() #1"; } return FALSE; } } function AccountRegistration($reg_email = NULL, $reg_name = NULL) { $packet = $this->SendIq($this->server, "get", "reg_01", "jabber:iq:register"); if ($packet) { $key = $this->GetInfoFromIqKey($packet); // just in case a key was passed back from the server unset($packet); $payload = "<username>" . $this->username . "</username> <password>" . $this->password . "</password> <email>$reg_email</email> <name>$reg_name</name>\n"; $payload .= ($key) ? "<key>$key</key>\n" : ""; $packet = $this->SendIq($this->server, "set", "reg_01", "jabber:iq:register", $payload); if ($this->GetInfoFromIqType($packet) == "result") { if (isset($packet["iq"]["#"]["query"][0]["#"]["registered"][0]["#"])) { $return_code = 1; } else { $return_code = 2; } if ($this->resource) { $this->jid = $this->username . "@" . $this->server . "/" . $this->resource; } else { $this->jid = $this->username . "@" . $this->server; } } elseif ($this->GetInfoFromIqType($packet) == "error") { if (isset($packet["iq"]["#"]["error"][0]["#"])) { $return_code = "Error " . $packet["iq"]["#"]["error"][0]["@"]["code"] . ": " . $packet["iq"]["#"]["error"][0]["#"]; } } return $return_code; } else { return 3; } } function SendPacket($xml) { $xml = trim($xml); if ($this->CONNECTOR->WriteToSocket($this->connection, $xml)) { if ($this->enable_logging) { $this->logfile[] = "<strong>SEND:</strong> " . nl2br(htmlspecialchars($xml)); } return TRUE; } else { if ($this->enable_logging) { $this->logfile[] = "<strong>Error:</strong> SendPacket() #1"; } return FALSE; } } function Listen() { unset($incoming); while ($line = $this->CONNECTOR->ReadFromSocket($this->connection, 4096)) { $incoming .= $line; } $incoming = trim($incoming); if ($this->enable_logging && $incoming != "") { $this->logfile[] = "<strong>RECV:</strong> " . nl2br(htmlspecialchars($incoming)); } if ($incoming != "") { $temp = $this->_split_incoming($incoming); for ($a = 0; $a < count($temp); $a++) { $this->packet_queue[] = $this->xmlize($temp[$a]); } } return TRUE; } function StripJID($jid = NULL) { preg_match("/(.*)\/(.*)/Ui", $jid, $temp); return ($temp[1] != "") ? $temp[1] : $jid; } function SendMessage($to, $type = "normal", $id = NULL, $content = NULL, $payload = NULL) { if ($to && is_array($content)) { if (!$id) { $id = $type . "_" . time(); } $content = $this->_array_htmlspecialchars($content); $xml = "<message to='$to' type='$type' id='$id'>\n"; if ($content["thread"]) { $xml .= "<thread>" . $content["thread"] . "</thread>\n"; } if ($content['subject']) { $xml .= "<subject>" . $content['subject'] . "</subject>\n"; } $xml .= "<body>" . $content["body"] . "</body>\n"; $xml .= $payload; $xml .= "</message>\n"; if ($this->SendPacket($xml)) { return TRUE; } else { if ($this->enable_logging) { $this->logfile[] = "<strong>Error:</strong> SendMessage() #1"; } return FALSE; } } else { if ($this->enable_logging) { $this->logfile[] = "<strong>Error:</strong> SendMessage() #2"; } return FALSE; } } function SendPresence($type = NULL, $to = NULL, $status = NULL, $show = NULL, $priority = NULL) { $xml = "<presence"; $xml .= ($to) ? " to='$to'" : ""; $xml .= ($type) ? " type='$type'" : ""; $xml .= ($status || $show || $priority) ? ">\n" : " />\n"; $xml .= ($status) ? " <status>$status</status>\n" : ""; $xml .= ($show) ? " <show>$show</show>\n" : ""; $xml .= ($priority) ? " <priority>$priority</priority>\n" : ""; $xml .= ($status || $show || $priority) ? "</presence>\n" : ""; if ($this->SendPacket($xml)) { return TRUE; } else { if ($this->enable_logging) { $this->logfile[] = "<strong>Error:</strong> SendPresence() #1"; } return FALSE; } } function SendError($to, $id = NULL, $error_number, $error_message = NULL) { $xml = "<iq type='error' to='$to'"; $xml .= ($id) ? " id='$id'" : ""; $xml .= ">\n"; $xml .= " <error code='$error_number'>"; $xml .= ($error_message) ? $error_message : $this->error_codes[$error_number]; $xml .= "</error>\n"; $xml .= "</iq>"; $this->SendPacket($xml); } function RosterUpdate() { $roster_request_id = "roster_" . time(); $incoming_array = $this->SendIq(NULL, "get", $roster_request_id, "jabber:iq:roster"); if (is_array($incoming_array)) { if ($incoming_array["iq"]["@"]["type"] == "result" && $incoming_array["iq"]["@"]["id"] == $roster_request_id && $incoming_array["iq"]["#"]["query"]["0"]["@"]["xmlns"] == "jabber:iq:roster") { $number_of_contacts = count($incoming_array["iq"]["#"]["query"][0]["#"]["item"]); $this->roster = array(); for ($a = 0; $a < $number_of_contacts; $a++) { $this->roster[$a] = array( "jid" => $incoming_array["iq"]["#"]["query"][0]["#"]["item"][$a]["@"]["jid"], "name" => $incoming_array["iq"]["#"]["query"][0]["#"]["item"][$a]["@"]["name"], "subscription" => $incoming_array["iq"]["#"]["query"][0]["#"]["item"][$a]["@"]["subscription"], "group" => $incoming_array["iq"]["#"]["query"][0]["#"]["item"][$a]["#"]["group"][0]["#"] ); } return TRUE; } else { if ($this->enable_logging) { $this->logfile[] = "<strong>Error:</strong> RosterUpdate() #1"; } return FALSE; }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -