📄 jabber.class
字号:
} else { if ($this->enable_logging) { $this->logfile[] = "<strong>Error:</strong> RosterUpdate() #2"; } return FALSE; } } function RosterAddUser($jid = NULL, $id = NULL, $name = NULL) { $id = ($id) ? $id : "adduser_" . time(); if ($jid) { $payload = " <item jid='$jid'"; $payload .= ($name) ? " name='" . htmlspecialchars($name) . "'" : ""; $payload .= "/>\n"; $packet = $this->SendIq(NULL, "set", $id, "jabber:iq:roster", $payload); if ($this->GetInfoFromIqType($packet) == "result") { $this->RosterUpdate(); return TRUE; } else { if ($this->enable_logging) { $this->logfile[] = "<strong>Error:</strong> RosterAddUser() #2"; } return FALSE; } } else { if ($this->enable_logging) { $this->logfile[] = "<strong>Error:</strong> RosterAddUser() #1"; } return FALSE; } } function RosterRemoveUser($jid = NULL, $id = NULL) { if ($jid && $id) { $packet = $this->SendIq(NULL, "set", $id, "jabber:iq:roster", "<item jid='$jid' subscription='remove'/>"); if ($this->GetInfoFromIqType($packet) == "result") { $this->RosterUpdate(); return TRUE; } else { if ($this->enable_logging) { $this->logfile[] = "<strong>Error:</strong> RosterRemoveUser() #2"; } return FALSE; } } else { if ($this->enable_logging) { $this->logfile[] = "<strong>Error:</strong> RosterRemoveUser() #1"; } return FALSE; } } function GetFirstFromQueue() { reset($this->packet_queue); list($key, $value) = each($this->packet_queue); unset($this->packet_queue[$key]); return (is_array($value)) ? $value : FALSE; } function GetFromQueueById($packet_type, $id) { $found_message = FALSE; foreach ($this->packet_queue as $key => $value) { if ($value["$packet_type"]["@"]["id"] == $id) { $found_message = $value; unset($this->packet_queue[$key]); break; } } return (is_array($found_message)) ? $found_message : FALSE; } function CallHandler($packet = NULL) { $packet_type = $this->_get_packet_type($packet); if ($packet_type == "message") { $type = $packet["message"]["@"]["type"]; $type = ($type != "") ? $type : "normal"; $funcmeth = "Handler_message_$type"; } elseif ($packet_type == "iq") { $this->TraverseXMLize($packet); $namespace = $packet["iq"]["#"]["query"][0]["@"]["xmlns"]; $namespace = str_replace(":", "_", $namespace); $funcmeth = "Handler_iq_$namespace"; } elseif ($packet_type == "presence") { $type = $packet["presence"]["@"]["type"]; $type = ($type != "") ? $type : "available"; $funcmeth = "Handler_presence_$type"; } if ($funcmeth != "") { if (function_exists($funcmeth)) { call_user_func($funcmeth, $packet); } elseif(method_exists($this, $funcmeth)) { call_user_func(array(&$this, $funcmeth), $packet); } elseif ($this->enable_logging) { $this->Handler_NOT_IMPLEMENTED($packet); $this->logfile[] = "<strong>Error:</strong> CallHandler() #1 - neither method nor function $funcmeth() available"; } } } function CruiseControl($seconds = -1) { $count = 0; while ($count != $seconds) { $this->Listen(); do { $packet = $this->GetFirstFromQueue(); $this->CallHandler($packet); } while (count($this->packet_queue) > 1); $count++; sleep(1); } return TRUE; } function SubscriptionAcceptRequest($to = NULL) { return ($to) ? $this->SendPresence("subscribed", $to) : FALSE; } function SubscriptionDenyRequest($to = NULL) { return ($to) ? /* still needs to be done */ TRUE : FALSE; } function Subscribe($to = NULL) { return ($to) ? $this->SendPresence("subscribe", $to) : FALSE; } function Unsubscribe($to = NULL) { return ($to) ? $this->SendPresence("unsubscribe", $to) : FALSE; } function SendIq($to = NULL, $type = "get", $id = NULL, $xmlns = NULL, $payload = NULL) { if (!preg_match("/^(get|set|result|error)$/", $type)) { unset($type); if ($this->enable_logging) { $this->logfile[] = "<strong>Error:</strong> SendIq() #2 - type must be 'get', 'set', 'result' or 'error'"; } return FALSE; } elseif ($id && $xmlns) { $xml = "<iq type='$type' id='$id'"; $xml .= ($to) ? " to='$to'" : ""; $xml .= "> <query xmlns='$xmlns'> $payload </query> </iq>"; $this->SendPacket($xml); sleep($this->iq_sleep_timer); $this->Listen(); return (preg_match("/^(get|set)$/", $type)) ? $this->GetFromQueueById("iq", $id) : TRUE; } else { if ($this->enable_logging) { $this->logfile[] = "<strong>Error:</strong> SendIq() #1 - to, id and xmlns are mandatory"; } return FALSE; } } // ====================================================================== // internal methods // ====================================================================== function _listen_incoming() { 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)); } return $this->xmlize($incoming); } function _check_connected() { $incoming_array = $this->_listen_incoming(); if (is_array($incoming_array)) { if ($incoming_array["stream:stream"]["@"]["from"] == $this->server && $incoming_array["stream:stream"]["@"]["xmlns"] == "jabber:client" && $incoming_array["stream:stream"]["@"]["xmlns:stream"] == "http://etherx.jabber.org/streams") { $this->stream_id = $incoming_array["stream:stream"]["@"]["id"]; return TRUE; } else { if ($this->enable_logging) { $this->logfile[] = "<strong>Error:</strong> _check_connected() #1"; } return FALSE; } } else { if ($this->enable_logging) { $this->logfile[] = "<strong>Error:</strong> _check_connected() #2"; } return FALSE; } } function _get_packet_type($packet = NULL) { if (is_array($packet)) { reset($packet); $packet_type = key($packet); } return ($packet_type) ? $packet_type : FALSE; } function _split_incoming($incoming) { $temp = preg_split("/<(message|iq|presence|stream)/", $incoming, -1, PREG_SPLIT_DELIM_CAPTURE); $array = array(); for ($a = 1; $a < count($temp); $a = $a + 2) { $array[] = "<" . $temp[$a] . $temp[($a + 1)]; } return $array; } // _array_htmlspecialchars() // applies htmlspecialchars() to all values in an array function _array_htmlspecialchars($array) { if (is_array($array)) { foreach ($array as $k => $v) { if (is_array($v)) { $v = $this->_array_htmlspecialchars($v); } else { $v = htmlspecialchars($v); } } } return $array; } // ====================================================================== // <message/> parsers // ====================================================================== function GetInfoFromMessageFrom($packet = NULL) { return (is_array($packet)) ? $packet["message"]["@"]["from"] : FALSE; } function GetInfoFromMessageType($packet = NULL) { return (is_array($packet)) ? $packet["message"]["@"]["type"] : FALSE; } function GetInfoFromMessageId($packet = NULL) { return (is_array($packet)) ? $packet["message"]["@"]["id"] : FALSE; } function GetInfoFromMessageThread($packet = NULL) { return (is_array($packet)) ? $packet["message"]["#"]["thread"][0]["#"] : FALSE; } function GetInfoFromMessageSubject($packet = NULL) { return (is_array($packet)) ? $packet["message"]["#"]["subject"][0]["#"] : FALSE; } function GetInfoFromMessageBody($packet = NULL) { return (is_array($packet)) ? $packet["message"]["#"]["body"][0]["#"] : FALSE; } function GetInfoFromMessageError($packet = NULL) { $error = preg_replace("/^\/$/", "", ($packet["message"]["#"]["error"][0]["@"]["code"] . "/" . $packet["message"]["#"]["error"][0]["#"])); return (is_array($packet)) ? $error : FALSE; } // ====================================================================== // <iq/> parsers // ====================================================================== function GetInfoFromIqFrom($packet = NULL) { return (is_array($packet)) ? $packet["iq"]["@"]["from"] : FALSE; } function GetInfoFromIqType($packet = NULL) { return (is_array($packet)) ? $packet["iq"]["@"]["type"] : FALSE; } function GetInfoFromIqId($packet = NULL) { return (is_array($packet)) ? $packet["iq"]["@"]["id"] : FALSE; } function GetInfoFromIqKey($packet = NULL) { return (is_array($packet)) ? $packet["iq"]["#"]["query"][0]["#"]["key"][0]["#"] : FALSE; } // ====================================================================== // <presence/> parsers // ====================================================================== function GetInfoFromPresenceFrom($packet = NULL) { return (is_array($packet)) ? $packet["presence"]["@"]["from"] : FALSE; } function GetInfoFromPresenceType($packet = NULL) { return (is_array($packet)) ? $packet["presence"]["@"]["type"] : FALSE; } function GetInfoFromPresenceStatus($packet = NULL) { return (is_array($packet)) ? $packet["presence"]["#"]["status"][0]["#"] : FALSE; } function GetInfoFromPresenceShow($packet = NULL) { return (is_array($packet)) ? $packet["presence"]["#"]["show"][0]["#"] : FALSE; } function GetInfoFromPresencePriority($packet = NULL) { return (is_array($packet)) ? $packet["presence"]["#"]["priority"][0]["#"] : FALSE; } // ====================================================================== // <message/> handlers // ====================================================================== function Handler_message_normal($packet) { $from = $packet["message"]["@"]["from"]; $this->logfile[] = "<strong>message</strong> (type normal) from $from"; } function Handler_message_chat($packet) { $from = $packet["message"]["@"]["from"]; $this->logfile[] = "<strong>message</strong> (type chat) from $from"; } function Handler_message_groupchat($packet) { $from = $packet["message"]["@"]["from"];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -