Page principale | Liste alphabétique | Liste des classes | Liste des fichiers | Membres de classe | Membres de fichier

htdocs/lib/jabber/class.jabber.php

Aller à la documentation de ce fichier.
00001 <?php 00002 00003 /*************************************************************************** 00004 00005 Class.Jabber.PHP v0.4 00006 (c) 2002 Carlo "Gossip" Zottmann 00007 http://phpjabber.g-blog.net *** gossip@jabber.g-blog.net 00008 00009 The FULL documentation and examples for this software can be found at 00010 http://phpjabber.g-blog.net (not many doc comments in here, sorry) 00011 00012 last modified: 27.04.2003 13:01:53 CET 00013 00014 ***************************************************************************/ 00015 00016 /*************************************************************************** 00017 * 00018 00019 * 00020 ***************************************************************************/ 00021 00022 /* 00023 Jabber::Connect() 00024 Jabber::Disconnect() 00025 Jabber::SendAuth() 00026 Jabber::AccountRegistration($reg_email {string}, $reg_name {string}) 00027 00028 Jabber::Listen() 00029 Jabber::SendPacket($xml {string}) 00030 00031 Jabber::RosterUpdate() 00032 Jabber::RosterAddUser($jid {string}, $id {string}, $name {string}) 00033 Jabber::RosterRemoveUser($jid {string}, $id {string}) 00034 Jabber::RosterExistsJID($jid {string}) 00035 00036 Jabber::Subscribe($jid {string}) 00037 Jabber::Unsubscribe($jid {string}) 00038 00039 Jabber::CallHandler($message {array}) 00040 Jabber::CruiseControl([$seconds {number}]) 00041 00042 Jabber::SubscriptionApproveRequest($to {string}) 00043 Jabber::SubscriptionDenyRequest($to {string}) 00044 00045 Jabber::GetFirstFromQueue() 00046 Jabber::GetFromQueueById($packet_type {string}, $id {string}) 00047 00048 Jabber::SendMessage($to {string}, $id {number}, $type {string}, $content {array}[, $payload {array}]) 00049 Jabber::SendIq($to {string}, $type {string}, $id {string}, $xmlns {string}[, $payload {string}]) 00050 Jabber::SendPresence($type {string}[, $to {string}[, $status {string}[, $show {string}[, $priority {number}]]]]) 00051 00052 Jabber::SendError($to {string}, $id {string}, $error_number {number}[, $error_message {string}]) 00053 00054 Jabber::TransportRegistrationDetails($transport {string}) 00055 Jabber::TransportRegistration($transport {string}, $details {array}) 00056 00057 Jabber::GetvCard($jid {string}[, $id {string}]) -- EXPERIMENTAL -- 00058 00059 Jabber::GetInfoFromMessageFrom($packet {array}) 00060 Jabber::GetInfoFromMessageType($packet {array}) 00061 Jabber::GetInfoFromMessageId($packet {array}) 00062 Jabber::GetInfoFromMessageThread($packet {array}) 00063 Jabber::GetInfoFromMessageSubject($packet {array}) 00064 Jabber::GetInfoFromMessageBody($packet {array}) 00065 Jabber::GetInfoFromMessageError($packet {array}) 00066 00067 Jabber::GetInfoFromIqFrom($packet {array}) 00068 Jabber::GetInfoFromIqType($packet {array}) 00069 Jabber::GetInfoFromIqId($packet {array}) 00070 Jabber::GetInfoFromIqKey($packet {array}) 00071 Jabber::GetInfoFromIqError($packet {array}) 00072 00073 Jabber::GetInfoFromPresenceFrom($packet {array}) 00074 Jabber::GetInfoFromPresenceType($packet {array}) 00075 Jabber::GetInfoFromPresenceStatus($packet {array}) 00076 Jabber::GetInfoFromPresenceShow($packet {array}) 00077 Jabber::GetInfoFromPresencePriority($packet {array}) 00078 00079 Jabber::AddToLog($string {string}) 00080 Jabber::PrintLog() 00081 00082 MakeXML::AddPacketDetails($string {string}[, $value {string/number}]) 00083 MakeXML::BuildPacket([$array {array}]) 00084 */ 00085 00095 class Jabber 00096 { 00097 var $server; 00098 var $port; 00099 var $username; 00100 var $password; 00101 var $resource; 00102 var $jid; 00103 00104 var $connection; 00105 var $delay_disconnect; 00106 00107 var $stream_id; 00108 var $roster; 00109 00110 var $enable_logging; 00111 var $log_array; 00112 var $log_filename; 00113 var $log_filehandler; 00114 00115 var $iq_sleep_timer; 00116 var $last_ping_time; 00117 00118 var $packet_queue; 00119 var $subscription_queue; 00120 00121 var $iq_version_name; 00122 var $iq_version_os; 00123 var $iq_version_version; 00124 00125 var $error_codes; 00126 00127 var $connected; 00128 var $keep_alive_id; 00129 var $returned_keep_alive; 00130 var $txnid; 00131 00132 var $CONNECTOR; 00133 00134 00135 00136 function Jabber() 00137 { 00138 $this->server = "localhost"; 00139 $this->port = "5222"; 00140 00141 $this->username = "larry"; 00142 $this->password = "curly"; 00143 $this->resource = NULL; 00144 00145 $this->enable_logging = FALSE; 00146 $this->log_array = array(); 00147 $this->log_filename = ''; 00148 $this->log_filehandler = FALSE; 00149 00150 $this->packet_queue = array(); 00151 $this->subscription_queue = array(); 00152 00153 $this->iq_sleep_timer = 1; 00154 $this->delay_disconnect = 1; 00155 00156 $this->returned_keep_alive = TRUE; 00157 $this->txnid = 0; 00158 00159 $this->iq_version_name = "Class.Jabber.PHP -- http://phpjabber.g-blog.net -- by Carlo 'Gossip' Zottmann, gossip@jabber.g-blog.net"; 00160 $this->iq_version_version = "0.4"; 00161 $this->iq_version_os = $_SERVER['SERVER_SOFTWARE']; 00162 00163 $this->connection_class = "CJP_StandardConnector"; 00164 00165 $this->error_codes = array(400 => "Bad Request", 00166 401 => "Unauthorized", 00167 402 => "Payment Required", 00168 403 => "Forbidden", 00169 404 => "Not Found", 00170 405 => "Not Allowed", 00171 406 => "Not Acceptable", 00172 407 => "Registration Required", 00173 408 => "Request Timeout", 00174 409 => "Conflict", 00175 500 => "Internal Server Error", 00176 501 => "Not Implemented", 00177 502 => "Remove Server Error", 00178 503 => "Service Unavailable", 00179 504 => "Remove Server Timeout", 00180 510 => "Disconnected"); 00181 } 00182 00183 00184 00185 function Connect() 00186 { 00187 $this->_create_logfile(); 00188 00189 $this->CONNECTOR = new $this->connection_class; 00190 00191 if ($this->CONNECTOR->OpenSocket($this->server, $this->port)) 00192 { 00193 $this->SendPacket("<?xml version='1.0' encoding='UTF-8' ?" . ">\n"); 00194 $this->SendPacket("<stream:stream to='{$this->server}' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'>\n"); 00195 00196 sleep(2); 00197 00198 if ($this->_check_connected()) 00199 { 00200 $this->connected = TRUE; // Nathan Fritz 00201 return TRUE; 00202 } 00203 else 00204 { 00205 $this->AddToLog("ERROR: Connect() #1"); 00206 return FALSE; 00207 } 00208 } 00209 else 00210 { 00211 $this->AddToLog("ERROR: Connect() #2"); 00212 return FALSE; 00213 } 00214 } 00215 00216 00217 00218 function Disconnect() 00219 { 00220 if (is_int($this->delay_disconnect)) 00221 { 00222 sleep($this->delay_disconnect); 00223 } 00224 00225 $this->SendPacket("</stream:stream>"); 00226 $this->CONNECTOR->CloseSocket(); 00227 00228 $this->_close_logfile(); 00229 $this->PrintLog(); 00230 } 00231 00232 00233 00234 function SendAuth() 00235 { 00236 $this->auth_id = "auth_" . md5(time() . $_SERVER['REMOTE_ADDR']); 00237 00238 $this->resource = ($this->resource != NULL) ? $this->resource : ("Class.Jabber.PHP " . md5($this->auth_id)); 00239 $this->jid = "{$this->username}@{$this->server}/{$this->resource}"; 00240 00241 // request available authentication methods 00242 $payload = "<username>{$this->username}</username>"; 00243 $packet = $this->SendIq(NULL, 'get', $this->auth_id, "jabber:iq:auth", $payload); 00244 00245 // was a result returned? 00246 if ($this->GetInfoFromIqType($packet) == 'result' && $this->GetInfoFromIqId($packet) == $this->auth_id) 00247 { 00248 // yes, now check for auth method availability in descending order (best to worst) 00249 00250 if (!function_exists(mhash)) 00251 { 00252 $this->AddToLog("ATTENTION: SendAuth() - mhash() is not available; screw 0k and digest method, we need to go with plaintext auth"); 00253 } 00254 00255 // auth_0k 00256 if (function_exists(mhash) && isset($packet['iq']['#']['query'][0]['#']['sequence'][0]["#"]) && isset($packet['iq']['#']['query'][0]['#']['token'][0]["#"])) 00257 { 00258 return $this->_sendauth_0k($packet['iq']['#']['query'][0]['#']['token'][0]["#"], $packet['iq']['#']['query'][0]['#']['sequence'][0]["#"]); 00259 } 00260 // digest 00261 elseif (function_exists(mhash) && isset($packet['iq']['#']['query'][0]['#']['digest'])) 00262 { 00263 return $this->_sendauth_digest(); 00264 } 00265 // plain text 00266 elseif ($packet['iq']['#']['query'][0]['#']['password']) 00267 { 00268 return $this->_sendauth_plaintext(); 00269 } 00270 // dude, you're fucked 00271 { 00272 $this->AddToLog("ERROR: SendAuth() #2 - No auth method available!"); 00273 return FALSE; 00274 } 00275 } 00276 else 00277 { 00278 // no result returned 00279 $this->AddToLog("ERROR: SendAuth() #1"); 00280 return FALSE; 00281 } 00282 } 00283 00284 00285 00286 function AccountRegistration($reg_email = NULL, $reg_name = NULL) 00287 { 00288 $packet = $this->SendIq($this->server, 'get', 'reg_01', 'jabber:iq:register'); 00289 00290 if ($packet) 00291 { 00292 $key = $this->GetInfoFromIqKey($packet); // just in case a key was passed back from the server 00293 unset($packet); 00294 00295 $payload = "<username>{$this->username}</username> 00296 <password>{$this->password}</password> 00297 <email>$reg_email</email> 00298 <name>$reg_name</name>\n"; 00299 00300 $payload .= ($key) ? "<key>$key</key>\n" : ''; 00301 00302 $packet = $this->SendIq($this->server, 'set', "reg_01", "jabber:iq:register", $payload); 00303 00304 if ($this->GetInfoFromIqType($packet) == 'result') 00305 { 00306 if (isset($packet['iq']['#']['query'][0]['#']['registered'][0]['#'])) 00307 { 00308 $return_code = 1; 00309 } 00310 else 00311 { 00312 $return_code = 2; 00313 } 00314 00315 if ($this->resource) 00316 { 00317 $this->jid = "{$this->username}@{$this->server}/{$this->resource}"; 00318 } 00319 else 00320 { 00321 $this->jid = "{$this->username}@{$this->server}"; 00322 } 00323 00324 } 00325 elseif ($this->GetInfoFromIqType($packet) == 'error' && isset($packet['iq']['#']['error'][0]['#'])) 00326 { 00327 // "conflict" error, i.e. already registered 00328 if ($packet['iq']['#']['error'][0]['@']['code'] == '409') 00329 { 00330 $return_code = 1; 00331 } 00332 else 00333 { 00334 $return_code = "Error " . $packet['iq']['#']['error'][0]['@']['code'] . ": " . $packet['iq']['#']['error'][0]['#']; 00335 } 00336 } 00337 00338 return $return_code; 00339 00340 } 00341 else 00342 { 00343 return 3; 00344 } 00345 } 00346 00347 00348 00349 function SendPacket($xml) 00350 { 00351 $xml = trim($xml); 00352 00353 if ($this->CONNECTOR->WriteToSocket($xml)) 00354 { 00355 $this->AddToLog("SEND: $xml"); 00356 return TRUE; 00357 } 00358 else 00359 { 00360 $this->AddToLog('ERROR: SendPacket() #1'); 00361 return FALSE; 00362 } 00363 } 00364 00365 00366 00367 function Listen() 00368 { 00369 unset($incoming); 00370 00371 while ($line = $this->CONNECTOR->ReadFromSocket(4096)) 00372 { 00373 $incoming .= $line; 00374 } 00375 00376 $incoming = trim($incoming); 00377 00378 if ($incoming != "") 00379 { 00380 $this->AddToLog("RECV: $incoming"); 00381 } 00382 00383 if ($incoming != "") 00384 { 00385 $temp = $this->_split_incoming($incoming); 00386 00387 for ($a = 0; $a < count($temp); $a++) 00388 { 00389 $this->packet_queue[] = $this->xmlize($temp[$a]); 00390 } 00391 } 00392 00393 return TRUE; 00394 } 00395 00396 00397 00398 function StripJID($jid = NULL) 00399 { 00400 preg_match("/(.*)\/(.*)/Ui", $jid, $temp); 00401 return ($temp[1] != "") ? $temp[1] : $jid; 00402 } 00403 00404 00405 00406 function SendMessage($to, $type = "normal", $id = NULL, $content = NULL, $payload = NULL) 00407 { 00408 if ($to && is_array($content)) 00409 { 00410 if (!$id) 00411 { 00412 $id = $type . "_" . time(); 00413 } 00414 00415 $content = $this->_array_htmlspecialchars($content); 00416 00417 $xml = "<message to='$to' type='$type' id='$id'>\n"; 00418 00419 if ($content['subject']) 00420 { 00421 $xml .= "<subject>" . $content['subject'] . "</subject>\n"; 00422 } 00423 00424 if ($content['thread']) 00425 { 00426 $xml .= "<thread>" . $content['thread'] . "</thread>\n"; 00427 } 00428 00429 $xml .= "<body>" . $content['body'] . "</body>\n"; 00430 $xml .= $payload; 00431 $xml .= "</message>\n"; 00432 00433 00434 if ($this->SendPacket($xml)) 00435 { 00436 return TRUE; 00437 } 00438 else 00439 { 00440 $this->AddToLog("ERROR: SendMessage() #1"); 00441 return FALSE; 00442 } 00443 } 00444 else 00445 { 00446 $this->AddToLog("ERROR: SendMessage() #2"); 00447 return FALSE; 00448 } 00449 } 00450 00451 00452 00453 function SendPresence($type = NULL, $to = NULL, $status = NULL, $show = NULL, $priority = NULL) 00454 { 00455 $xml = "<presence"; 00456 $xml .= ($to) ? " to='$to'" : ''; 00457 $xml .= ($type) ? " type='$type'" : ''; 00458 $xml .= ($status || $show || $priority) ? ">\n" : " />\n"; 00459 00460 $xml .= ($status) ? " <status>$status</status>\n" : ''; 00461 $xml .= ($show) ? " <show>$show</show>\n" : ''; 00462 $xml .= ($priority) ? " <priority>$priority</priority>\n" : ''; 00463 00464 $xml .= ($status || $show || $priority) ? "</presence>\n" : ''; 00465 00466 if ($this->SendPacket($xml)) 00467 { 00468 return TRUE; 00469 } 00470 else 00471 { 00472 $this->AddToLog("ERROR: SendPresence() #1"); 00473 return FALSE; 00474 } 00475 } 00476 00477 00478 00479 function SendError($to, $id = NULL, $error_number, $error_message = NULL) 00480 { 00481 $xml = "<iq type='error' to='$to'"; 00482 $xml .= ($id) ? " id='$id'" : ''; 00483 $xml .= ">\n"; 00484 $xml .= " <error code='$error_number'>"; 00485 $xml .= ($error_message) ? $error_message : $this->error_codes[$error_number]; 00486 $xml .= "</error>\n"; 00487 $xml .= "</iq>"; 00488 00489 $this->SendPacket($xml); 00490 } 00491 00492 00493 00494 function RosterUpdate() 00495 { 00496 $roster_request_id = "roster_" . time(); 00497 00498 $incoming_array = $this->SendIq(NULL, 'get', $roster_request_id, "jabber:iq:roster"); 00499 00500 if (is_array($incoming_array)) 00501 { 00502 if ($incoming_array['iq']['@']['type'] == 'result' 00503 && $incoming_array['iq']['@']['id'] == $roster_request_id 00504 && $incoming_array['iq']['#']['query']['0']['@']['xmlns'] == "jabber:iq:roster") 00505 { 00506 $number_of_contacts = count($incoming_array['iq']['#']['query'][0]['#']['item']); 00507 $this->roster = array(); 00508 00509 for ($a = 0; $a < $number_of_contacts; $a++) 00510 { 00511 $this->roster[$a] = array( "jid" => strtolower($incoming_array['iq']['#']['query'][0]['#']['item'][$a]['@']['jid']), 00512 "name" => $incoming_array['iq']['#']['query'][0]['#']['item'][$a]['@']['name'], 00513 "subscription" => $incoming_array['iq']['#']['query'][0]['#']['item'][$a]['@']['subscription'], 00514 "group" => $incoming_array['iq']['#']['query'][0]['#']['item'][$a]['#']['group'][0]['#'] 00515 ); 00516 } 00517 00518 return TRUE; 00519 } 00520 else 00521 { 00522 $this->AddToLog("ERROR: RosterUpdate() #1"); 00523 return FALSE; 00524 } 00525 } 00526 else 00527 { 00528 $this->AddToLog("ERROR: RosterUpdate() #2"); 00529 return FALSE; 00530 } 00531 } 00532 00533 00534 00535 function RosterAddUser($jid = NULL, $id = NULL, $name = NULL) 00536 { 00537 $id = ($id) ? $id : "adduser_" . time(); 00538 00539 if ($jid) 00540 { 00541 $payload = " <item jid='$jid'"; 00542 $payload .= ($name) ? " name='" . htmlspecialchars($name) . "'" : ''; 00543 $payload .= "/>\n"; 00544 00545 $packet = $this->SendIq(NULL, 'set', $id, "jabber:iq:roster", $payload); 00546 00547 if ($this->GetInfoFromIqType($packet) == 'result') 00548 { 00549 $this->RosterUpdate(); 00550 return TRUE; 00551 } 00552 else 00553 { 00554 $this->AddToLog("ERROR: RosterAddUser() #2"); 00555 return FALSE; 00556 } 00557 } 00558 else 00559 { 00560 $this->AddToLog("ERROR: RosterAddUser() #1"); 00561 return FALSE; 00562 } 00563 } 00564 00565 00566 00567 function RosterRemoveUser($jid = NULL, $id = NULL) 00568 { 00569 $id = ($id) ? $id : 'deluser_' . time(); 00570 00571 if ($jid && $id) 00572 { 00573 $packet = $this->SendIq(NULL, 'set', $id, "jabber:iq:roster", "<item jid='$jid' subscription='remove'/>"); 00574 00575 if ($this->GetInfoFromIqType($packet) == 'result') 00576 { 00577 $this->RosterUpdate(); 00578 return TRUE; 00579 } 00580 else 00581 { 00582 $this->AddToLog("ERROR: RosterRemoveUser() #2"); 00583 return FALSE; 00584 } 00585 } 00586 else 00587 { 00588 $this->AddToLog("ERROR: RosterRemoveUser() #1"); 00589 return FALSE; 00590 } 00591 } 00592 00593 00594 00595 function RosterExistsJID($jid = NULL) 00596 { 00597 if ($jid) 00598 { 00599 if ($this->roster) 00600 { 00601 for ($a = 0; $a < count($this->roster); $a++) 00602 { 00603 if ($this->roster[$a]['jid'] == strtolower($jid)) 00604 { 00605 return $a; 00606 } 00607 } 00608 } 00609 else 00610 { 00611 $this->AddToLog("ERROR: RosterExistsJID() #2"); 00612 return FALSE; 00613 } 00614 } 00615 else 00616 { 00617 $this->AddToLog("ERROR: RosterExistsJID() #1"); 00618 return FALSE; 00619 } 00620 } 00621 00622 00623 00624 function GetFirstFromQueue() 00625 { 00626 return array_shift($this->packet_queue); 00627 } 00628 00629 00630 00631 function GetFromQueueById($packet_type, $id) 00632 { 00633 $found_message = FALSE; 00634 00635 foreach ($this->packet_queue as $key => $value) 00636 { 00637 if ($value[$packet_type]['@']['id'] == $id) 00638 { 00639 $found_message = $value; 00640 unset($this->packet_queue[$key]); 00641 00642 break; 00643 } 00644 } 00645 00646 return (is_array($found_message)) ? $found_message : FALSE; 00647 } 00648 00649 00650 00651 function CallHandler($packet = NULL) 00652 { 00653 $packet_type = $this->_get_packet_type($packet); 00654 00655 if ($packet_type == "message") 00656 { 00657 $type = $packet['message']['@']['type']; 00658 $type = ($type != "") ? $type : "normal"; 00659 $funcmeth = "Handler_message_$type"; 00660 } 00661 elseif ($packet_type == "iq") 00662 { 00663 $namespace = $packet['iq']['#']['query'][0]['@']['xmlns']; 00664 $namespace = str_replace(":", "_", $namespace); 00665 $funcmeth = "Handler_iq_$namespace"; 00666 } 00667 elseif ($packet_type == "presence") 00668 { 00669 $type = $packet['presence']['@']['type']; 00670 $type = ($type != "") ? $type : "available"; 00671 $funcmeth = "Handler_presence_$type"; 00672 } 00673 00674 00675 if ($funcmeth != '') 00676 { 00677 if (function_exists($funcmeth)) 00678 { 00679 call_user_func($funcmeth, $packet); 00680 } 00681 elseif (method_exists($this, $funcmeth)) 00682 { 00683 call_user_func(array(&$this, $funcmeth), $packet); 00684 } 00685 else 00686 { 00687 $this->Handler_NOT_IMPLEMENTED($packet); 00688 $this->AddToLog("ERROR: CallHandler() #1 - neither method nor function $funcmeth() available"); 00689 } 00690 } 00691 } 00692 00693 00694 00695 function CruiseControl($seconds = -1) 00696 { 00697 $count = 0; 00698 00699 while ($count != $seconds) 00700 { 00701 $this->Listen(); 00702 00703 do { 00704 $packet = $this->GetFirstFromQueue(); 00705 00706 if ($packet) { 00707 $this->CallHandler($packet); 00708 } 00709 00710 } while (count($this->packet_queue) > 1); 00711 00712 $count += 0.25; 00713 usleep(250000); 00714 00715 if ($this->last_ping_time != date('H:i')) 00716 { 00717 // Modified by Nathan Fritz 00718 if ($this->returned_keep_alive == FALSE) 00719 { 00720 $this->connected = FALSE; 00721 $this->AddToLog('EVENT: Disconnected'); 00722 } 00723 00724 $this->returned_keep_alive = FALSE; 00725 $this->keep_alive_id = 'keep_alive_' . time(); 00726 $this->SendPacket("<iq id='{$this->keep_alive_id}'/>", 'CruiseControl'); 00727 // ** 00728 00729 $this->last_ping_time = date("H:i"); 00730 } 00731 } 00732 00733 return TRUE; 00734 } 00735 00736 00737 00738 function SubscriptionAcceptRequest($to = NULL) 00739 { 00740 return ($to) ? $this->SendPresence("subscribed", $to) : FALSE; 00741 } 00742 00743 00744 00745 function SubscriptionDenyRequest($to = NULL) 00746 { 00747 return ($to) ? $this->SendPresence("unsubscribed", $to) : FALSE; 00748 } 00749 00750 00751 00752 function Subscribe($to = NULL) 00753 { 00754 return ($to) ? $this->SendPresence("subscribe", $to) : FALSE; 00755 } 00756 00757 00758 00759 function Unsubscribe($to = NULL) 00760 { 00761 return ($to) ? $this->SendPresence("unsubscribe", $to) : FALSE; 00762 } 00763 00764 00765 00766 function SendIq($to = NULL, $type = 'get', $id = NULL, $xmlns = NULL, $payload = NULL, $from = NULL) 00767 { 00768 if (!preg_match("/^(get|set|result|error)$/", $type)) 00769 { 00770 unset($type); 00771 00772 $this->AddToLog("ERROR: SendIq() #2 - type must be 'get', 'set', 'result' or 'error'"); 00773 return FALSE; 00774 } 00775 elseif ($id && $xmlns) 00776 { 00777 $xml = "<iq type='$type' id='$id'"; 00778 $xml .= ($to) ? " to='$to'" : ''; 00779 $xml .= ($from) ? " from='$from'" : ''; 00780 $xml .= "> 00781 <query xmlns='$xmlns'> 00782 $payload 00783 </query> 00784 </iq>"; 00785 00786 $this->SendPacket($xml); 00787 sleep($this->iq_sleep_timer); 00788 $this->Listen(); 00789 00790 return (preg_match("/^(get|set)$/", $type)) ? $this->GetFromQueueById("iq", $id) : TRUE; 00791 } 00792 else 00793 { 00794 $this->AddToLog("ERROR: SendIq() #1 - to, id and xmlns are mandatory"); 00795 return FALSE; 00796 } 00797 } 00798 00799 00800 00801 // get the transport registration fields 00802 // method written by Steve Blinch, http://www.blitzaffe.com 00803 function TransportRegistrationDetails($transport) 00804 { 00805 $this->txnid++; 00806 $packet = $this->SendIq($transport, 'get', "reg_{$this->txnid}", "jabber:iq:register", NULL, $this->jid); 00807 00808 if ($packet) 00809 { 00810 $res = array(); 00811 00812 foreach ($packet['iq']['#']['query'][0]['#'] as $element => $data) 00813 { 00814 if ($element != 'instructions' && $element != 'key') 00815 { 00816 $res[] = $element; 00817 } 00818 } 00819 00820 return $res; 00821 } 00822 else 00823 { 00824 return 3; 00825 } 00826 } 00827 00828 00829 00830 // register with the transport 00831 // method written by Steve Blinch, http://www.blitzaffe.com 00832 function TransportRegistration($transport, $details) 00833 { 00834 $this->txnid++; 00835 $packet = $this->SendIq($transport, 'get', "reg_{$this->txnid}", "jabber:iq:register", NULL, $this->jid); 00836 00837 if ($packet) 00838 { 00839 $key = $this->GetInfoFromIqKey($packet); // just in case a key was passed back from the server 00840 unset($packet); 00841 00842 $payload = ($key) ? "<key>$key</key>\n" : ''; 00843 foreach ($details as $element => $value) 00844 { 00845 $payload .= "<$element>$value</$element>\n"; 00846 } 00847 00848 $packet = $this->SendIq($transport, 'set', "reg_{$this->txnid}", "jabber:iq:register", $payload); 00849 00850 if ($this->GetInfoFromIqType($packet) == 'result') 00851 { 00852 if (isset($packet['iq']['#']['query'][0]['#']['registered'][0]['#'])) 00853 { 00854 $return_code = 1; 00855 } 00856 else 00857 { 00858 $return_code = 2; 00859 } 00860 } 00861 elseif ($this->GetInfoFromIqType($packet) == 'error') 00862 { 00863 if (isset($packet['iq']['#']['error'][0]['#'])) 00864 { 00865 $return_code = "Error " . $packet['iq']['#']['error'][0]['@']['code'] . ": " . $packet['iq']['#']['error'][0]['#']; 00866 $this->AddToLog('ERROR: TransportRegistration()'); 00867 } 00868 } 00869 00870 return $return_code; 00871 } 00872 else 00873 { 00874 return 3; 00875 } 00876 } 00877 00878 00879 00880 function GetvCard($jid = NULL, $id = NULL) 00881 { 00882 if (!$id) 00883 { 00884 $id = "vCard_" . md5(time() . $_SERVER['REMOTE_ADDR']); 00885 } 00886 00887 if ($jid) 00888 { 00889 $xml = "<iq type='get' to='$jid' id='$id'> 00890 <vCard xmlns='vcard-temp'/> 00891 </iq>"; 00892 00893 $this->SendPacket($xml); 00894 sleep($this->iq_sleep_timer); 00895 $this->Listen(); 00896 00897 return $this->GetFromQueueById("iq", $id); 00898 } 00899 else 00900 { 00901 $this->AddToLog("ERROR: GetvCard() #1 - to and id are mandatory"); 00902 return FALSE; 00903 } 00904 } 00905 00906 00907 00908 function PrintLog() 00909 { 00910 if ($this->enable_logging) 00911 { 00912 if ($this->log_filehandler) 00913 { 00914 echo "<h2>Logging enabled, logged events have been written to the file {$this->log_filename}.</h2>\n"; 00915 } 00916 else 00917 { 00918 echo "<h2>Logging enabled, logged events below:</h2>\n"; 00919 echo "<pre>\n"; 00920 echo (count($this->log_array) > 0) ? implode("\n\n\n", $this->log_array) : "No logged events."; 00921 echo "</pre>\n"; 00922 } 00923 } 00924 } 00925 00926 00927 00928 // ====================================================================== 00929 // private methods 00930 // ====================================================================== 00931 00932 00933 00934 function _sendauth_0k($zerok_token, $zerok_sequence) 00935 { 00936 // initial hash of password 00937 $zerok_hash = mhash(MHASH_SHA1, $this->password); 00938 $zerok_hash = bin2hex($zerok_hash); 00939 00940 // sequence 0: hash of hashed-password and token 00941 $zerok_hash = mhash(MHASH_SHA1, $zerok_hash . $zerok_token); 00942 $zerok_hash = bin2hex($zerok_hash); 00943 00944 // repeat as often as needed 00945 for ($a = 0; $a < $zerok_sequence; $a++) 00946 { 00947 $zerok_hash = mhash(MHASH_SHA1, $zerok_hash); 00948 $zerok_hash = bin2hex($zerok_hash); 00949 } 00950 00951 $payload = "<username>{$this->username}</username> 00952 <hash>$zerok_hash</hash> 00953 <resource>{$this->resource}</resource>"; 00954 00955 $packet = $this->SendIq(NULL, 'set', $this->auth_id, "jabber:iq:auth", $payload); 00956 00957 // was a result returned? 00958 if ($this->GetInfoFromIqType($packet) == 'result' && $this->GetInfoFromIqId($packet) == $this->auth_id) 00959 { 00960 return TRUE; 00961 } 00962 else 00963 { 00964 $this->AddToLog("ERROR: _sendauth_0k() #1"); 00965 return FALSE; 00966 } 00967 } 00968 00969 00970 00971 function _sendauth_digest() 00972 { 00973 $payload = "<username>{$this->username}</username> 00974 <resource>{$this->resource}</resource> 00975 <digest>" . bin2hex(mhash(MHASH_SHA1, $this->stream_id . $this->password)) . "</digest>"; 00976 00977 $packet = $this->SendIq(NULL, 'set', $this->auth_id, "jabber:iq:auth", $payload); 00978 00979 // was a result returned? 00980 if ($this->GetInfoFromIqType($packet) == 'result' && $this->GetInfoFromIqId($packet) == $this->auth_id) 00981 { 00982 return TRUE; 00983 } 00984 else 00985 { 00986 $this->AddToLog("ERROR: _sendauth_digest() #1"); 00987 return FALSE; 00988 } 00989 } 00990 00991 00992 00993 function _sendauth_plaintext() 00994 { 00995 $payload = "<username>{$this->username}</username> 00996 <password>{$this->password}</password> 00997 <resource>{$this->resource}</resource>"; 00998 00999 $packet = $this->SendIq(NULL, 'set', $this->auth_id, "jabber:iq:auth", $payload); 01000 01001 // was a result returned? 01002 if ($this->GetInfoFromIqType($packet) == 'result' && $this->GetInfoFromIqId($packet) == $this->auth_id) 01003 { 01004 return TRUE; 01005 } 01006 else 01007 { 01008 $this->AddToLog("ERROR: _sendauth_plaintext() #1"); 01009 return FALSE; 01010 } 01011 } 01012 01013 01014 01015 function _listen_incoming() 01016 { 01017 unset($incoming); 01018 01019 while ($line = $this->CONNECTOR->ReadFromSocket(4096)) 01020 { 01021 $incoming .= $line; 01022 } 01023 01024 $incoming = trim($incoming); 01025 01026 if ($incoming != "") 01027 { 01028 $this->AddToLog("RECV: $incoming"); 01029 } 01030 01031 return $this->xmlize($incoming); 01032 } 01033 01034 01035 01036 function _check_connected() 01037 { 01038 $incoming_array = $this->_listen_incoming(); 01039 01040 if (is_array($incoming_array)) 01041 { 01042 if ($incoming_array["stream:stream"]['@']['from'] == $this->server 01043 && $incoming_array["stream:stream"]['@']['xmlns'] == "jabber:client" 01044 && $incoming_array["stream:stream"]['@']["xmlns:stream"] == "http://etherx.jabber.org/streams") 01045 { 01046 $this->stream_id = $incoming_array["stream:stream"]['@']['id']; 01047 01048 return TRUE; 01049 } 01050 else 01051 { 01052 $this->AddToLog("ERROR: _check_connected() #1"); 01053 return FALSE; 01054 } 01055 } 01056 else 01057 { 01058 $this->AddToLog("ERROR: _check_connected() #2"); 01059 return FALSE; 01060 } 01061 } 01062 01063 01064 01065 function _get_packet_type($packet = NULL) 01066 { 01067 if (is_array($packet)) 01068 { 01069 reset($packet); 01070 $packet_type = key($packet); 01071 } 01072 01073 return ($packet_type) ? $packet_type : FALSE; 01074 } 01075 01076 01077 01078 function _split_incoming($incoming) 01079 { 01080 $temp = preg_split("/<(message|iq|presence|stream)/", $incoming, -1, PREG_SPLIT_DELIM_CAPTURE); 01081 $array = array(); 01082 01083 for ($a = 1; $a < count($temp); $a = $a + 2) 01084 { 01085 $array[] = "<" . $temp[$a] . $temp[($a + 1)]; 01086 } 01087 01088 return $array; 01089 } 01090 01091 01092 01093 function _create_logfile() 01094 { 01095 if ($this->log_filename != '' && $this->enable_logging) 01096 { 01097 $this->log_filehandler = fopen($this->log_filename, 'w'); 01098 } 01099 } 01100 01101 01102 01103 function AddToLog($string) 01104 { 01105 if ($this->enable_logging) 01106 { 01107 if ($this->log_filehandler) 01108 { 01109 fwrite($this->log_filehandler, $string . "\n\n"); 01110 } 01111 else 01112 { 01113 $this->log_array[] = htmlspecialchars($string); 01114 } 01115 } 01116 } 01117 01118 01119 01120 function _close_logfile() 01121 { 01122 if ($this->log_filehandler) 01123 { 01124 fclose($this->log_filehandler); 01125 } 01126 } 01127 01128 01129 01130 // _array_htmlspecialchars() 01131 // applies htmlspecialchars() to all values in an array 01132 01133 function _array_htmlspecialchars($array) 01134 { 01135 if (is_array($array)) 01136 { 01137 foreach ($array as $k => $v) 01138 { 01139 if (is_array($v)) 01140 { 01141 $v = $this->_array_htmlspecialchars($v); 01142 } 01143 else 01144 { 01145 $v = htmlspecialchars($v); 01146 } 01147 } 01148 } 01149 01150 return $array; 01151 } 01152 01153 01154 01155 // ====================================================================== 01156 // <message/> parsers 01157 // ====================================================================== 01158 01159 01160 01161 function GetInfoFromMessageFrom($packet = NULL) 01162 { 01163 return (is_array($packet)) ? $packet['message']['@']['from'] : FALSE; 01164 } 01165 01166 01167 01168 function GetInfoFromMessageType($packet = NULL) 01169 { 01170 return (is_array($packet)) ? $packet['message']['@']['type'] : FALSE; 01171 } 01172 01173 01174 01175 function GetInfoFromMessageId($packet = NULL) 01176 { 01177 return (is_array($packet)) ? $packet['message']['@']['id'] : FALSE; 01178 } 01179 01180 01181 01182 function GetInfoFromMessageThread($packet = NULL) 01183 { 01184 return (is_array($packet)) ? $packet['message']['#']['thread'][0]['#'] : FALSE; 01185 } 01186 01187 01188 01189 function GetInfoFromMessageSubject($packet = NULL) 01190 { 01191 return (is_array($packet)) ? $packet['message']['#']['subject'][0]['#'] : FALSE; 01192 } 01193 01194 01195 01196 function GetInfoFromMessageBody($packet = NULL) 01197 { 01198 return (is_array($packet)) ? $packet['message']['#']['body'][0]['#'] : FALSE; 01199 } 01200 01201 01202 01203 function GetInfoFromMessageError($packet = NULL) 01204 { 01205 $error = preg_replace("/^\/$/", "", ($packet['message']['#']['error'][0]['@']['code'] . "/" . $packet['message']['#']['error'][0]['#'])); 01206 return (is_array($packet)) ? $error : FALSE; 01207 } 01208 01209 01210 01211 // ====================================================================== 01212 // <iq/> parsers 01213 // ====================================================================== 01214 01215 01216 01217 function GetInfoFromIqFrom($packet = NULL) 01218 { 01219 return (is_array($packet)) ? $packet['iq']['@']['from'] : FALSE; 01220 } 01221 01222 01223 01224 function GetInfoFromIqType($packet = NULL) 01225 { 01226 return (is_array($packet)) ? $packet['iq']['@']['type'] : FALSE; 01227 } 01228 01229 01230 01231 function GetInfoFromIqId($packet = NULL) 01232 { 01233 return (is_array($packet)) ? $packet['iq']['@']['id'] : FALSE; 01234 } 01235 01236 01237 01238 function GetInfoFromIqKey($packet = NULL) 01239 { 01240 return (is_array($packet)) ? $packet['iq']['#']['query'][0]['#']['key'][0]['#'] : FALSE; 01241 } 01242 01243 01244 01245 function GetInfoFromIqError($packet = NULL) 01246 { 01247 $error = preg_replace("/^\/$/", "", ($packet['iq']['#']['error'][0]['@']['code'] . "/" . $packet['iq']['#']['error'][0]['#'])); 01248 return (is_array($packet)) ? $error : FALSE; 01249 } 01250 01251 01252 01253 // ====================================================================== 01254 // <presence/> parsers 01255 // ====================================================================== 01256 01257 01258 01259 function GetInfoFromPresenceFrom($packet = NULL) 01260 { 01261 return (is_array($packet)) ? $packet['presence']['@']['from'] : FALSE; 01262 } 01263 01264 01265 01266 function GetInfoFromPresenceType($packet = NULL) 01267 { 01268 return (is_array($packet)) ? $packet['presence']['@']['type'] : FALSE; 01269 } 01270 01271 01272 01273 function GetInfoFromPresenceStatus($packet = NULL) 01274 { 01275 return (is_array($packet)) ? $packet['presence']['#']['status'][0]['#'] : FALSE; 01276 } 01277 01278 01279 01280 function GetInfoFromPresenceShow($packet = NULL) 01281 { 01282 return (is_array($packet)) ? $packet['presence']['#']['show'][0]['#'] : FALSE; 01283 } 01284 01285 01286 01287 function GetInfoFromPresencePriority($packet = NULL) 01288 { 01289 return (is_array($packet)) ? $packet['presence']['#']['priority'][0]['#'] : FALSE; 01290 } 01291 01292 01293 01294 // ====================================================================== 01295 // <message/> handlers 01296 // ====================================================================== 01297 01298 01299 01300 function Handler_message_normal($packet) 01301 { 01302 $from = $packet['message']['@']['from']; 01303 $this->AddToLog("EVENT: Message (type normal) from $from"); 01304 } 01305 01306 01307 01308 function Handler_message_chat($packet) 01309 { 01310 $from = $packet['message']['@']['from']; 01311 $this->AddToLog("EVENT: Message (type chat) from $from"); 01312 } 01313 01314 01315 01316 function Handler_message_groupchat($packet) 01317 { 01318 $from = $packet['message']['@']['from']; 01319 $this->AddToLog("EVENT: Message (type groupchat) from $from"); 01320 } 01321 01322 01323 01324 function Handler_message_headline($packet) 01325 { 01326 $from = $packet['message']['@']['from']; 01327 $this->AddToLog("EVENT: Message (type headline) from $from"); 01328 } 01329 01330 01331 01332 function Handler_message_error($packet) 01333 { 01334 $from = $packet['message']['@']['from']; 01335 $this->AddToLog("EVENT: Message (type error) from $from"); 01336 } 01337 01338 01339 01340 // ====================================================================== 01341 // <iq/> handlers 01342 // ====================================================================== 01343 01344 01345 01346 // application version updates 01347 function Handler_iq_jabber_iq_autoupdate($packet) 01348 { 01349 $from = $this->GetInfoFromIqFrom($packet); 01350 $id = $this->GetInfoFromIqId($packet); 01351 01352 $this->SendError($from, $id, 501); 01353 $this->AddToLog("EVENT: jabber:iq:autoupdate from $from"); 01354 } 01355 01356 01357 01358 // interactive server component properties 01359 function Handler_iq_jabber_iq_agent($packet) 01360 { 01361 $from = $this->GetInfoFromIqFrom($packet); 01362 $id = $this->GetInfoFromIqId($packet); 01363 01364 $this->SendError($from, $id, 501); 01365 $this->AddToLog("EVENT: jabber:iq:agent from $from"); 01366 } 01367 01368 01369 01370 // method to query interactive server components 01371 function Handler_iq_jabber_iq_agents($packet) 01372 { 01373 $from = $this->GetInfoFromIqFrom($packet); 01374 $id = $this->GetInfoFromIqId($packet); 01375 01376 $this->SendError($from, $id, 501); 01377 $this->AddToLog("EVENT: jabber:iq:agents from $from"); 01378 } 01379 01380 01381 01382 // simple client authentication 01383 function Handler_iq_jabber_iq_auth($packet) 01384 { 01385 $from = $this->GetInfoFromIqFrom($packet); 01386 $id = $this->GetInfoFromIqId($packet); 01387 01388 $this->SendError($from, $id, 501); 01389 $this->AddToLog("EVENT: jabber:iq:auth from $from"); 01390 } 01391 01392 01393 01394 // out of band data 01395 function Handler_iq_jabber_iq_oob($packet) 01396 { 01397 $from = $this->GetInfoFromIqFrom($packet); 01398 $id = $this->GetInfoFromIqId($packet); 01399 01400 $this->SendError($from, $id, 501); 01401 $this->AddToLog("EVENT: jabber:iq:oob from $from"); 01402 } 01403 01404 01405 01406 // method to store private data on the server 01407 function Handler_iq_jabber_iq_private($packet) 01408 { 01409 $from = $this->GetInfoFromIqFrom($packet); 01410 $id = $this->GetInfoFromIqId($packet); 01411 01412 $this->SendError($from, $id, 501); 01413 $this->AddToLog("EVENT: jabber:iq:private from $from"); 01414 } 01415 01416 01417 01418 // method for interactive registration 01419 function Handler_iq_jabber_iq_register($packet) 01420 { 01421 $from = $this->GetInfoFromIqFrom($packet); 01422 $id = $this->GetInfoFromIqId($packet); 01423 01424 $this->SendError($from, $id, 501); 01425 $this->AddToLog("EVENT: jabber:iq:register from $from"); 01426 } 01427 01428 01429 01430 // client roster management 01431 function Handler_iq_jabber_iq_roster($packet) 01432 { 01433 $from = $this->GetInfoFromIqFrom($packet); 01434 $id = $this->GetInfoFromIqId($packet); 01435 01436 $this->SendError($from, $id, 501); 01437 $this->AddToLog("EVENT: jabber:iq:roster from $from"); 01438 } 01439 01440 01441 01442 // method for searching a user database 01443 function Handler_iq_jabber_iq_search($packet) 01444 { 01445 $from = $this->GetInfoFromIqFrom($packet); 01446 $id = $this->GetInfoFromIqId($packet); 01447 01448 $this->SendError($from, $id, 501); 01449 $this->AddToLog("EVENT: jabber:iq:search from $from"); 01450 } 01451 01452 01453 01454 // method for requesting the current time 01455 function Handler_iq_jabber_iq_time($packet) 01456 { 01457 $type = $this->GetInfoFromIqType($packet); 01458 $from = $this->GetInfoFromIqFrom($packet); 01459 $id = $this->GetInfoFromIqId($packet); 01460 $id = ($id != "") ? $id : "time_" . time(); 01461 01462 if ($type == 'get') 01463 { 01464 $payload = "<utc>" . gmdate("Ydm\TH:i:s") . "</utc> 01465 <tz>" . date("T") . "</tz> 01466 <display>" . date("Y/d/m h:i:s A") . "</display>"; 01467 01468 $this->SendIq($from, 'result', $id, "jabber:iq:time", $payload); 01469 } 01470 01471 $this->AddToLog("EVENT: jabber:iq:time (type $type) from $from"); 01472 } 01473 01474 01475 01476 // method for requesting version 01477 function Handler_iq_jabber_iq_version($packet) 01478 { 01479 $type = $this->GetInfoFromIqType($packet); 01480 $from = $this->GetInfoFromIqFrom($packet); 01481 $id = $this->GetInfoFromIqId($packet); 01482 $id = ($id != "") ? $id : "version_" . time(); 01483 01484 if ($type == 'get') 01485 { 01486 $payload = "<name>{$this->iq_version_name}</name> 01487 <os>{$this->iq_version_os}</os> 01488 <version>{$this->iq_version_version}</version>"; 01489 01490 $this->SendIq($from, 'result', $id, "jabber:iq:version", $payload); 01491 } 01492 01493 $this->AddToLog("EVENT: jabber:iq:version (type $type) from $from"); 01494 } 01495 01496 01497 01498 // keepalive method, added by Nathan Fritz 01499 function Handler_iq_($packet) 01500 { 01501 if ($this->keep_alive_id == $this->GetInfoFromIqId($packet)) 01502 { 01503 $this->returned_keep_alive = TRUE; 01504 $this->AddToLog('EVENT: Keep-Alive returned, connection alive.'); 01505 } 01506 } 01507 01508 01509 01510 // ====================================================================== 01511 // <presence/> handlers 01512 // ====================================================================== 01513 01514 01515 01516 function Handler_presence_available($packet) 01517 { 01518 $from = $this->GetInfoFromPresenceFrom($packet); 01519 01520 $show_status = $this->GetInfoFromPresenceStatus($packet) . " / " . $this->GetInfoFromPresenceShow($packet); 01521 $show_status = ($show_status != " / ") ? " ($addendum)" : ''; 01522 01523 $this->AddToLog("EVENT: Presence (type: available) - $from is available $show_status"); 01524 } 01525 01526 01527 01528 function Handler_presence_unavailable($packet) 01529 { 01530 $from = $this->GetInfoFromPresenceFrom($packet); 01531 01532 $show_status = $this->GetInfoFromPresenceStatus($packet) . " / " . $this->GetInfoFromPresenceShow($packet); 01533 $show_status = ($show_status != " / ") ? " ($addendum)" : ''; 01534 01535 $this->AddToLog("EVENT: Presence (type: unavailable) - $from is unavailable $show_status"); 01536 } 01537 01538 01539 01540 function Handler_presence_subscribe($packet) 01541 { 01542 $from = $this->GetInfoFromPresenceFrom($packet); 01543 $this->SubscriptionAcceptRequest($from); 01544 $this->RosterUpdate(); 01545 01546 $this->log_array[] = "<b>Presence:</b> (type: subscribe) - Subscription request from $from, was added to \$this->subscription_queue, roster updated"; 01547 } 01548 01549 01550 01551 function Handler_presence_subscribed($packet) 01552 { 01553 $from = $this->GetInfoFromPresenceFrom($packet); 01554 $this->RosterUpdate(); 01555 01556 $this->AddToLog("EVENT: Presence (type: subscribed) - Subscription allowed by $from, roster updated"); 01557 } 01558 01559 01560 01561 function Handler_presence_unsubscribe($packet) 01562 { 01563 $from = $this->GetInfoFromPresenceFrom($packet); 01564 $this->SendPresence("unsubscribed", $from); 01565 $this->RosterUpdate(); 01566 01567 $this->AddToLog("EVENT: Presence (type: unsubscribe) - Request to unsubscribe from $from, was automatically approved, roster updated"); 01568 } 01569 01570 01571 01572 function Handler_presence_unsubscribed($packet) 01573 { 01574 $from = $this->GetInfoFromPresenceFrom($packet); 01575 $this->RosterUpdate(); 01576 01577 $this->AddToLog("EVENT: Presence (type: unsubscribed) - Unsubscribed from $from's presence"); 01578 } 01579 01580 01581 01582 // Added By Nathan Fritz 01583 function Handler_presence_error($packet) 01584 { 01585 $from = $this->GetInfoFromPresenceFrom($packet); 01586 $this->AddToLog("EVENT: Presence (type: error) - Error in $from's presence"); 01587 } 01588 01589 01590 01591 // ====================================================================== 01592 // Generic handlers 01593 // ====================================================================== 01594 01595 01596 01597 // Generic handler for unsupported requests 01598 function Handler_NOT_IMPLEMENTED($packet) 01599 { 01600 $packet_type = $this->_get_packet_type($packet); 01601 $from = call_user_func(array(&$this, "GetInfoFrom" . ucfirst($packet_type) . "From"), $packet); 01602 $id = call_user_func(array(&$this, "GetInfoFrom" . ucfirst($packet_type) . "Id"), $packet); 01603 01604 $this->SendError($from, $id, 501); 01605 $this->AddToLog("EVENT: Unrecognized <$packet_type/> from $from"); 01606 } 01607 01608 01609 01610 // ====================================================================== 01611 // Third party code 01612 // m@d pr0ps to the coders ;) 01613 // ====================================================================== 01614 01615 01616 01617 // xmlize() 01618 // (c) Hans Anderson / http://www.hansanderson.com/php/xml/ 01619 01620 function xmlize($data) 01621 { 01622 $vals = $index = $array = array(); 01623 $parser = xml_parser_create(); 01624 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); 01625 xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); 01626 xml_parse_into_struct($parser, $data, $vals, $index); 01627 xml_parser_free($parser); 01628 01629 $i = 0; 01630 01631 $tagname = $vals[$i]['tag']; 01632 $array[$tagname]['@'] = $vals[$i]['attributes']; 01633 $array[$tagname]['#'] = $this->_xml_depth($vals, $i); 01634 01635 return $array; 01636 } 01637 01638 01639 01640 // _xml_depth() 01641 // (c) Hans Anderson / http://www.hansanderson.com/php/xml/ 01642 01643 function _xml_depth($vals, &$i) 01644 { 01645 $children = array(); 01646 01647 if ($vals[$i]['value']) 01648 { 01649 array_push($children, trim($vals[$i]['value'])); 01650 } 01651 01652 while (++$i < count($vals)) 01653 { 01654 switch ($vals[$i]['type']) 01655 { 01656 case 'cdata': 01657 array_push($children, trim($vals[$i]['value'])); 01658 break; 01659 01660 case 'complete': 01661 $tagname = $vals[$i]['tag']; 01662 $size = sizeof($children[$tagname]); 01663 $children[$tagname][$size]['#'] = trim($vals[$i]['value']); 01664 if ($vals[$i]['attributes']) 01665 { 01666 $children[$tagname][$size]['@'] = $vals[$i]['attributes']; 01667 } 01668 break; 01669 01670 case 'open': 01671 $tagname = $vals[$i]['tag']; 01672 $size = sizeof($children[$tagname]); 01673 if ($vals[$i]['attributes']) 01674 { 01675 $children[$tagname][$size]['@'] = $vals[$i]['attributes']; 01676 $children[$tagname][$size]['#'] = $this->_xml_depth($vals, $i); 01677 } 01678 else 01679 { 01680 $children[$tagname][$size]['#'] = $this->_xml_depth($vals, $i); 01681 } 01682 break; 01683 01684 case 'close': 01685 return $children; 01686 break; 01687 } 01688 } 01689 01690 return $children; 01691 } 01692 01693 01694 01695 // TraverseXMLize() 01696 // (c) acebone@f2s.com, a HUGE help! 01697 01698 function TraverseXMLize($array, $arrName = "array", $level = 0) 01699 { 01700 if ($level == 0) 01701 { 01702 echo "<pre>"; 01703 } 01704 01705 while (list($key, $val) = @each($array)) 01706 { 01707 if (is_array($val)) 01708 { 01709 $this->TraverseXMLize($val, $arrName . "[" . $key . "]", $level + 1); 01710 } 01711 else 01712 { 01713 echo '$' . $arrName . '[' . $key . '] = "' . $val . "\"\n"; 01714 } 01715 } 01716 01717 if ($level == 0) 01718 { 01719 echo "</pre>"; 01720 } 01721 } 01722 } 01723 01724 01725 01726 class MakeXML extends Jabber 01727 { 01728 var $nodes; 01729 01730 01731 function MakeXML() 01732 { 01733 $nodes = array(); 01734 } 01735 01736 01737 01738 function AddPacketDetails($string, $value = NULL) 01739 { 01740 if (preg_match("/\(([0-9]*)\)$/i", $string)) 01741 { 01742 $string .= "/[\"#\"]"; 01743 } 01744 01745 $temp = @explode("/", $string); 01746 01747 for ($a = 0; $a < count($temp); $a++) 01748 { 01749 $temp[$a] = preg_replace("/^[@]{1}([a-z0-9_]*)$/i", "[\"@\"][\"\\1\"]", $temp[$a]); 01750 $temp[$a] = preg_replace("/^([a-z0-9_]*)\(([0-9]*)\)$/i", "[\"\\1\"][\\2]", $temp[$a]); 01751 $temp[$a] = preg_replace("/^([a-z0-9_]*)$/i", "[\"\\1\"]", $temp[$a]); 01752 } 01753 01754 $node = implode("", $temp); 01755 01756 // Yeahyeahyeah, I know it's ugly... get over it. ;) 01757 echo "\$this->nodes$node = \"" . htmlspecialchars($value) . "\";<br/>"; 01758 eval("\$this->nodes$node = \"" . htmlspecialchars($value) . "\";"); 01759 } 01760 01761 01762 01763 function BuildPacket($array = NULL) 01764 { 01765 01766 if (!$array) 01767 { 01768 $array = $this->nodes; 01769 } 01770 01771 if (is_array($array)) 01772 { 01773 array_multisort($array, SORT_ASC, SORT_STRING); 01774 01775 foreach ($array as $key => $value) 01776 { 01777 if (is_array($value) && $key == "@") 01778 { 01779 foreach ($value as $subkey => $subvalue) 01780 { 01781 $subvalue = htmlspecialchars($subvalue); 01782 $text .= " $subkey='$subvalue'"; 01783 } 01784 01785 $text .= ">\n"; 01786 01787 } 01788 elseif ($key == "#") 01789 { 01790 $text .= htmlspecialchars($value); 01791 } 01792 elseif (is_array($value)) 01793 { 01794 for ($a = 0; $a < count($value); $a++) 01795 { 01796 $text .= "<$key"; 01797 01798 if (!$this->_preg_grep_keys("/^@/", $value[$a])) 01799 { 01800 $text .= ">"; 01801 } 01802 01803 $text .= $this->BuildPacket($value[$a]); 01804 01805 $text .= "</$key>\n"; 01806 } 01807 } 01808 else 01809 { 01810 $value = htmlspecialchars($value); 01811 $text .= "<$key>$value</$key>\n"; 01812 } 01813 } 01814 01815 return $text; 01816 } 01817 } 01818 01819 01820 01821 function _preg_grep_keys($pattern, $array) 01822 { 01823 while (list($key, $val) = each($array)) 01824 { 01825 if (preg_match($pattern, $key)) 01826 { 01827 $newarray[$key] = $val; 01828 } 01829 } 01830 return (is_array($newarray)) ? $newarray : FALSE; 01831 } 01832 } 01833 01834 01835 01836 class CJP_StandardConnector 01837 { 01838 var $active_socket; 01839 01840 function OpenSocket($server, $port) 01841 { 01842 if ($this->active_socket = fsockopen($server, $port)) 01843 { 01844 socket_set_blocking($this->active_socket, 0); 01845 socket_set_timeout($this->active_socket, 31536000); 01846 01847 return TRUE; 01848 } 01849 else 01850 { 01851 return FALSE; 01852 } 01853 } 01854 01855 01856 01857 function CloseSocket() 01858 { 01859 return fclose($this->active_socket); 01860 } 01861 01862 01863 01864 function WriteToSocket($data) 01865 { 01866 return fwrite($this->active_socket, $data); 01867 } 01868 01869 01870 01871 function ReadFromSocket($chunksize) 01872 { 01873 set_magic_quotes_runtime(0); 01874 $buffer = fread($this->active_socket, $chunksize); 01875 set_magic_quotes_runtime(get_magic_quotes_gpc()); 01876 01877 return $buffer; 01878 } 01879 } 01880 01881 01882 01883 ?>

Généré le Fri Jul 16 08:51:52 2004 pour dolibarr par doxygen 1.3.7