BOSH.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. <?php
  2. /**
  3. * XMPPHP: The PHP XMPP Library
  4. * Copyright (C) 2008 Nathanael C. Fritz
  5. * This file is part of SleekXMPP.
  6. *
  7. * XMPPHP is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * XMPPHP is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with XMPPHP; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category xmpphp
  22. * @package XMPPHP
  23. * @author Nathanael C. Fritz <JID: fritzy@netflint.net>
  24. * @author Stephan Wentz <JID: stephan@jabber.wentz.it>
  25. * @author Michael Garvin <JID: gar@netflint.net>
  26. * @author Alexander Birkner (https://github.com/BirknerAlex)
  27. * @author zorn-v (https://github.com/zorn-v/xmpphp/)
  28. * @author GNU social
  29. * @copyright 2008 Nathanael C. Fritz
  30. */
  31. namespace XMPPHP;
  32. use SimpleXMLElement;
  33. /** XMPPHP_XMLStream */
  34. require_once __DIR__ . "/XMPP.php";
  35. /**
  36. * XMPPHP BOSH
  37. *
  38. * @property int lat
  39. * @package XMPPHP
  40. * @author Nathanael C. Fritz <JID: fritzy@netflint.net>
  41. * @author Stephan Wentz <JID: stephan@jabber.wentz.it>
  42. * @author Michael Garvin <JID: gar@netflint.net>
  43. * @copyright 2008 Nathanael C. Fritz
  44. * @version $Id$
  45. */
  46. class BOSH extends XMPP
  47. {
  48. /**
  49. * @var integer
  50. */
  51. protected $rid;
  52. /**
  53. * @var string
  54. */
  55. protected $sid;
  56. /**
  57. * @var string
  58. */
  59. protected $http_server;
  60. /**
  61. * @var array
  62. */
  63. protected $http_buffer = array();
  64. /**
  65. * @var string
  66. */
  67. protected $session = false;
  68. /**
  69. * @var integer
  70. */
  71. protected $inactivity;
  72. /**
  73. * Connect
  74. *
  75. * @param $server
  76. * @param $wait
  77. * @param $session
  78. * @throws Exception
  79. * @throws Exception
  80. */
  81. public function connect($server = null, $wait = '1', $session = false)
  82. {
  83. if (is_null($server)) {
  84. // If we aren't given the server http url, try and guess it
  85. $port_string = ($this->port AND $this->port != 80) ? ':' . $this->port : '';
  86. $this->http_server = 'http://' . $this->host . $port_string . '/http-bind/';
  87. } else {
  88. $this->http_server = $server;
  89. }
  90. $this->use_encryption = false;
  91. $this->session = $session;
  92. $this->rid = 3001;
  93. $this->sid = null;
  94. $this->inactivity = 0;
  95. if ($session) {
  96. $this->loadSession();
  97. }
  98. if (!$this->sid) {
  99. $body = $this->__buildBody();
  100. $body->addAttribute('hold', '1');
  101. $body->addAttribute('to', $this->server);
  102. $body->addAttribute('route', 'xmpp:' . $this->host . ':' . $this->port);
  103. $body->addAttribute('secure', 'true');
  104. $body->addAttribute('xmpp:version', '1.0', 'urn:xmpp:xbosh');
  105. $body->addAttribute('wait', strval($wait));
  106. $body->addAttribute('ack', '1');
  107. $body->addAttribute('xmlns:xmpp', 'urn:xmpp:xbosh');
  108. $buff = '<stream:stream xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams">';
  109. xml_parse($this->parser, $buff, false);
  110. $response = $this->__sendBody($body);
  111. $rxml = new SimpleXMLElement($response);
  112. $this->sid = $rxml['sid'];
  113. $this->inactivity = $rxml['inactivity'];
  114. } else {
  115. $buff = '<stream:stream xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams">';
  116. xml_parse($this->parser, $buff, false);
  117. }
  118. }
  119. /**
  120. * Load session
  121. *
  122. */
  123. public function loadSession()
  124. {
  125. if ($this->session == 'ON_FILE') {
  126. // Session not started so use session_file
  127. $session_file = $this->getSessionFile();
  128. // manage multiple accesses
  129. if (!file_exists($session_file)) {
  130. file_put_contents($session_file, '');
  131. }
  132. $session_file_fp = fopen($session_file, 'r');
  133. flock($session_file_fp, LOCK_EX);
  134. $session_serialized = file_get_contents($session_file, null, null, 6);
  135. flock($session_file_fp, LOCK_UN);
  136. fclose($session_file_fp);
  137. $this->log->log('SESSION: reading ' . $session_serialized . ' from ' . $session_file, Log::LEVEL_VERBOSE);
  138. if ($session_serialized != '') {
  139. $_SESSION['XMPPHP_BOSH'] = unserialize($session_serialized);
  140. }
  141. }
  142. if (isset($_SESSION['XMPPHP_BOSH']['inactivity'])) {
  143. $this->inactivity = $_SESSION['XMPPHP_BOSH']['inactivity'];
  144. }
  145. $this->lat = (time() - (isset($_SESSION['XMPPHP_BOSH']['lat']))) ? $_SESSION['XMPPHP_BOSH']['lat'] : 0;
  146. if ($this->lat < $this->inactivity) {
  147. if (isset($_SESSION['XMPPHP_BOSH']['RID'])) {
  148. $this->rid = $_SESSION['XMPPHP_BOSH']['RID'];
  149. }
  150. if (isset($_SESSION['XMPPHP_BOSH']['SID'])) {
  151. $this->sid = $_SESSION['XMPPHP_BOSH']['SID'];
  152. }
  153. if (isset($_SESSION['XMPPHP_BOSH']['authed'])) {
  154. $this->authed = $_SESSION['XMPPHP_BOSH']['authed'];
  155. }
  156. if (isset($_SESSION['XMPPHP_BOSH']['basejid'])) {
  157. $this->basejid = $_SESSION['XMPPHP_BOSH']['basejid'];
  158. }
  159. if (isset($_SESSION['XMPPHP_BOSH']['fulljid'])) {
  160. $this->fulljid = $_SESSION['XMPPHP_BOSH']['fulljid'];
  161. }
  162. }
  163. }
  164. /**
  165. * Get the session file
  166. *
  167. */
  168. public function getSessionFile()
  169. {
  170. return sys_get_temp_dir() . '/' . $this->user . '_' . $this->server . '_session';
  171. }
  172. /**
  173. * Build body
  174. *
  175. * @param $sub
  176. * @return SimpleXMLElement|string
  177. */
  178. public function __buildBody($sub = null)
  179. {
  180. $xml = '<body xmlns="http://jabber.org/protocol/httpbind" xmlns:xmpp="urn:xmpp:xbosh" />';
  181. $xml = new SimpleXMLElement($xml);
  182. $xml->addAttribute('content', 'text/xml; charset=utf-8');
  183. $xml->addAttribute('rid', $this->rid);
  184. $this->rid++;
  185. if ($this->sid) {
  186. $xml->addAttribute('sid', $this->sid);
  187. }
  188. $xml->addAttribute('xml:lang', 'en');
  189. if ($sub !== null) {
  190. // Ok, so simplexml is lame
  191. $parent = dom_import_simplexml($xml);
  192. $content = dom_import_simplexml($sub);
  193. $child = $parent->ownerDocument->importNode($content, true);
  194. $parent->appendChild($child);
  195. $xml = simplexml_import_dom($parent);
  196. }
  197. return $xml;
  198. }
  199. /**
  200. * Send body
  201. *
  202. * @param $body
  203. * @param $recv
  204. * @return bool|string
  205. * @throws Exception
  206. * @throws Exception
  207. */
  208. public function __sendBody($body = null, $recv = true)
  209. {
  210. if (!$body) {
  211. $body = $this->__buildBody();
  212. }
  213. $output = '';
  214. $header = array('Accept-Encoding: gzip, deflate', 'Content-Type: text/xml; charset=utf-8');
  215. $ch = curl_init();
  216. curl_setopt($ch, CURLOPT_URL, $this->http_server);
  217. curl_setopt($ch, CURLOPT_HEADER, 0);
  218. curl_setopt($ch, CURLOPT_POST, 1);
  219. curl_setopt($ch, CURLOPT_POSTFIELDS, $body->asXML());
  220. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  221. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  222. curl_setopt($ch, CURLOPT_VERBOSE, 0);
  223. if ($recv) {
  224. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  225. $output = curl_exec($ch);
  226. if (curl_getinfo($ch, CURLINFO_HTTP_CODE) != '200') {
  227. throw new Exception('Wrong response from server!');
  228. }
  229. $this->http_buffer[] = $output;
  230. }
  231. curl_close($ch);
  232. return $output;
  233. }
  234. /**
  235. * Process
  236. *
  237. * @param $null1
  238. * @param $null2
  239. *
  240. * null params are not used and just to statify Strict Function Declaration
  241. * @return bool
  242. * @throws Exception
  243. * @throws Exception
  244. */
  245. public function __process($null1 = null, $null2 = null)
  246. {
  247. if ($this->http_buffer) {
  248. $this->__parseBuffer();
  249. } else {
  250. $this->__sendBody();
  251. $this->__parseBuffer();
  252. }
  253. $this->saveSession();
  254. return true;
  255. }
  256. public function __parseBuffer()
  257. {
  258. while ($this->http_buffer) {
  259. $idx = key($this->http_buffer);
  260. $buffer = $this->http_buffer[$idx];
  261. unset($this->http_buffer[$idx]);
  262. if ($buffer) {
  263. $xml = new SimpleXMLElement($buffer);
  264. $children = $xml->xpath('child::node()');
  265. foreach ($children as $child) {
  266. $buff = $child->asXML();
  267. $this->log->log('RECV: ' . $buff, Log::LEVEL_VERBOSE);
  268. xml_parse($this->parser, $buff, false);
  269. }
  270. }
  271. }
  272. }
  273. /**
  274. * Save session
  275. *
  276. */
  277. public function saveSession()
  278. {
  279. $_SESSION['XMPPHP_BOSH']['RID'] = (string)$this->rid;
  280. $_SESSION['XMPPHP_BOSH']['SID'] = (string)$this->sid;
  281. $_SESSION['XMPPHP_BOSH']['authed'] = (boolean)$this->authed;
  282. $_SESSION['XMPPHP_BOSH']['basejid'] = (string)$this->basejid;
  283. $_SESSION['XMPPHP_BOSH']['fulljid'] = (string)$this->fulljid;
  284. $_SESSION['XMPPHP_BOSH']['inactivity'] = (string)$this->inactivity;
  285. $_SESSION['XMPPHP_BOSH']['lat'] = (string)time();
  286. if ($this->session == 'ON_FILE') {
  287. $session_file = $this->getSessionFile();
  288. $session_file_fp = fopen($session_file, 'r');
  289. flock($session_file_fp, LOCK_EX);
  290. // <?php prefix used to mask the content of the session file
  291. $session_serialized = '<?php ' . serialize($_SESSION);
  292. file_put_contents($session_file, $session_serialized);
  293. flock($session_file_fp, LOCK_UN);
  294. fclose($session_file_fp);
  295. }
  296. }
  297. /**
  298. * Process
  299. *
  300. * @param $msg
  301. * @param $null
  302. *
  303. * null param are not used and just to statify Strict Function Declaration
  304. * @throws Exception
  305. * @throws Exception
  306. */
  307. public function send($msg, $null = null)
  308. {
  309. $this->log->log('SEND: ' . $msg, Log::LEVEL_VERBOSE);
  310. $msg = new SimpleXMLElement($msg);
  311. $this->__sendBody($this->__buildBody($msg), true);
  312. }
  313. /**
  314. * Reset
  315. *
  316. * @throws Exception
  317. */
  318. public function reset()
  319. {
  320. $this->xml_depth = 0;
  321. unset($this->xmlobj);
  322. $this->xmlobj = array();
  323. $this->setupParser();
  324. $body = $this->__buildBody();
  325. $body->addAttribute('to', $this->host);
  326. $body->addAttribute('xmpp:restart', 'true', 'urn:xmpp:xbosh');
  327. $buff = '<stream:stream xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams">';
  328. $response = $this->__sendBody($body);
  329. $this->been_reset = true;
  330. xml_parse($this->parser, $buff, false);
  331. }
  332. /**
  333. * Disconnect
  334. *
  335. * @throws Exception
  336. */
  337. public function disconnect()
  338. {
  339. parent::disconnect();
  340. if ($this->session == 'ON_FILE') {
  341. unlink($this->getSessionFile());
  342. } else {
  343. $keys = array('RID', 'SID', 'authed', 'basejid', 'fulljid', 'inactivity', 'lat');
  344. foreach ($keys as $key) {
  345. unset($_SESSION['XMPPHP_BOSH'][$key]);
  346. }
  347. }
  348. }
  349. }