BOSH.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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__ . DIRECTORY_SEPARATOR . '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 int
  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 = [];
  64. /**
  65. * @var string
  66. */
  67. protected $session = false;
  68. /**
  69. * @var int
  70. */
  71. protected $inactivity;
  72. public function __construct(
  73. string $host,
  74. int $port,
  75. string $user,
  76. string $password,
  77. string $resource,
  78. ?string $server = null,
  79. bool $print_log = false,
  80. ?string $log_level = null
  81. ) {
  82. parent::__construct($host, $port, $user, $password, $resource, $server, $print_log, $log_level);
  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. }
  91. /**
  92. * Connect
  93. *
  94. * @param bool $persistent
  95. * @param bool $send_init
  96. * @param int $timeout
  97. * @throws Exception
  98. */
  99. public function connect(bool $persistent = false, bool $send_init = true, int $timeout = 30): void
  100. {
  101. $this->use_encryption = false;
  102. $this->session = $persistent;
  103. $this->rid = 3001;
  104. $this->sid = null;
  105. $this->inactivity = 0;
  106. if ($persistent) {
  107. $this->loadSession();
  108. }
  109. if (!$this->sid) {
  110. $body = $this->__buildBody();
  111. $body->addAttribute('hold', '1');
  112. $body->addAttribute('to', $this->server);
  113. $body->addAttribute('route', 'xmpp:' . $this->host . ':' . $this->port);
  114. $body->addAttribute('secure', 'true');
  115. $body->addAttribute('xmpp:version', '1.0', 'urn:xmpp:xbosh');
  116. $body->addAttribute('wait', strval($timeout));
  117. $body->addAttribute('ack', '1');
  118. $body->addAttribute('xmlns:xmpp', 'urn:xmpp:xbosh');
  119. $buff = '<stream:stream xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams">';
  120. xml_parse($this->parser, $buff, false);
  121. $response = $this->__sendBody($body);
  122. $rxml = new SimpleXMLElement($response);
  123. $this->sid = $rxml['sid'];
  124. $this->inactivity = $rxml['inactivity'];
  125. } else {
  126. $buff = '<stream:stream xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams">';
  127. xml_parse($this->parser, $buff, false);
  128. }
  129. }
  130. /**
  131. * Load session
  132. *
  133. */
  134. public function loadSession(): void
  135. {
  136. if ($this->session == 'ON_FILE') {
  137. // Session not started so use session_file
  138. $session_file = $this->getSessionFile();
  139. // manage multiple accesses
  140. if (!file_exists($session_file)) {
  141. file_put_contents($session_file, '');
  142. }
  143. $session_file_fp = fopen($session_file, 'r');
  144. flock($session_file_fp, LOCK_EX);
  145. $session_serialized = file_get_contents($session_file, null, null, 6);
  146. flock($session_file_fp, LOCK_UN);
  147. fclose($session_file_fp);
  148. $this->log->log('SESSION: reading ' . $session_serialized . ' from ' . $session_file, Log::LEVEL_VERBOSE);
  149. if ($session_serialized != '') {
  150. $_SESSION['XMPPHP_BOSH'] = unserialize($session_serialized);
  151. }
  152. }
  153. if (isset($_SESSION['XMPPHP_BOSH']['inactivity'])) {
  154. $this->inactivity = $_SESSION['XMPPHP_BOSH']['inactivity'];
  155. }
  156. $this->lat = (time() - (isset($_SESSION['XMPPHP_BOSH']['lat']))) ? $_SESSION['XMPPHP_BOSH']['lat'] : 0;
  157. if ($this->lat < $this->inactivity) {
  158. if (isset($_SESSION['XMPPHP_BOSH']['RID'])) {
  159. $this->rid = $_SESSION['XMPPHP_BOSH']['RID'];
  160. }
  161. if (isset($_SESSION['XMPPHP_BOSH']['SID'])) {
  162. $this->sid = $_SESSION['XMPPHP_BOSH']['SID'];
  163. }
  164. if (isset($_SESSION['XMPPHP_BOSH']['authed'])) {
  165. $this->authed = $_SESSION['XMPPHP_BOSH']['authed'];
  166. }
  167. if (isset($_SESSION['XMPPHP_BOSH']['basejid'])) {
  168. $this->basejid = $_SESSION['XMPPHP_BOSH']['basejid'];
  169. }
  170. if (isset($_SESSION['XMPPHP_BOSH']['fulljid'])) {
  171. $this->fulljid = $_SESSION['XMPPHP_BOSH']['fulljid'];
  172. }
  173. }
  174. }
  175. /**
  176. * Get the session file location
  177. *
  178. */
  179. public function getSessionFile(): string
  180. {
  181. return sys_get_temp_dir() . '/' . $this->user . '_' . $this->server . '_session';
  182. }
  183. /**
  184. * Build body
  185. *
  186. * @param SimpleXMLElement|null $sub
  187. * @return SimpleXMLElement
  188. */
  189. private function __buildBody(?SimpleXMLElement $sub = null): SimpleXMLElement
  190. {
  191. $xml = new SimpleXMLElement('<body xmlns="http://jabber.org/protocol/httpbind" xmlns:xmpp="urn:xmpp:xbosh" />');
  192. $xml->addAttribute('content', 'text/xml; charset=utf-8');
  193. $xml->addAttribute('rid', $this->rid);
  194. ++$this->rid;
  195. if ($this->sid) {
  196. $xml->addAttribute('sid', $this->sid);
  197. }
  198. $xml->addAttribute('xml:lang', 'en');
  199. if ($sub !== null) {
  200. // Ok, so simplexml is lame
  201. $parent = dom_import_simplexml($xml);
  202. $content = dom_import_simplexml($sub);
  203. $child = $parent->ownerDocument->importNode($content, true);
  204. $parent->appendChild($child);
  205. $xml = simplexml_import_dom($parent);
  206. }
  207. return $xml;
  208. }
  209. /**
  210. * Send body
  211. *
  212. * @param SimpleXMLElement|null $body
  213. * @param bool $recv
  214. * @return bool|string
  215. * @throws Exception
  216. */
  217. private function __sendBody(?SimpleXMLElement $body = null, bool $recv = true)
  218. {
  219. if (!$body) {
  220. $body = $this->__buildBody();
  221. }
  222. $output = '';
  223. $header = ['Accept-Encoding: gzip, deflate', 'Content-Type: text/xml; charset=utf-8'];
  224. $ch = curl_init();
  225. curl_setopt($ch, CURLOPT_URL, $this->http_server);
  226. curl_setopt($ch, CURLOPT_HEADER, 0);
  227. curl_setopt($ch, CURLOPT_POST, 1);
  228. curl_setopt($ch, CURLOPT_POSTFIELDS, $body->asXML());
  229. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  230. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  231. curl_setopt($ch, CURLOPT_VERBOSE, 0);
  232. if ($recv) {
  233. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  234. $output = curl_exec($ch);
  235. if (curl_getinfo($ch, CURLINFO_HTTP_CODE) != '200') {
  236. throw new Exception('Wrong response from server!');
  237. }
  238. $this->http_buffer[] = $output;
  239. }
  240. curl_close($ch);
  241. return $output;
  242. }
  243. /**
  244. * Process
  245. *
  246. * @param $null1
  247. * @param $null2
  248. *
  249. * null params are not used and just to statify Strict Function Declaration
  250. * @return bool
  251. * @throws Exception
  252. * @throws Exception
  253. */
  254. private function __process($null1 = null, $null2 = null)
  255. {
  256. if ($this->http_buffer) {
  257. $this->__parseBuffer();
  258. } else {
  259. $this->__sendBody();
  260. $this->__parseBuffer();
  261. }
  262. $this->saveSession();
  263. return true;
  264. }
  265. private function __parseBuffer()
  266. {
  267. while ($this->http_buffer) {
  268. $idx = key($this->http_buffer);
  269. $buffer = $this->http_buffer[$idx];
  270. unset($this->http_buffer[$idx]);
  271. if ($buffer) {
  272. $xml = new SimpleXMLElement($buffer);
  273. $children = $xml->xpath('child::node()');
  274. foreach ($children as $child) {
  275. $buff = $child->asXML();
  276. $this->log->log('RECV: ' . $buff, Log::LEVEL_VERBOSE);
  277. xml_parse($this->parser, $buff, false);
  278. }
  279. }
  280. }
  281. }
  282. /**
  283. * Save session
  284. *
  285. */
  286. public function saveSession(): void
  287. {
  288. $_SESSION['XMPPHP_BOSH']['RID'] = (string)$this->rid;
  289. $_SESSION['XMPPHP_BOSH']['SID'] = (string)$this->sid;
  290. $_SESSION['XMPPHP_BOSH']['authed'] = (boolean)$this->authed;
  291. $_SESSION['XMPPHP_BOSH']['basejid'] = (string)$this->basejid;
  292. $_SESSION['XMPPHP_BOSH']['fulljid'] = (string)$this->fulljid;
  293. $_SESSION['XMPPHP_BOSH']['inactivity'] = (string)$this->inactivity;
  294. $_SESSION['XMPPHP_BOSH']['lat'] = (string)time();
  295. if ($this->session == 'ON_FILE') {
  296. $session_file = $this->getSessionFile();
  297. $session_file_fp = fopen($session_file, 'r');
  298. flock($session_file_fp, LOCK_EX);
  299. // <?php prefix used to mask the content of the session file
  300. $session_serialized = '<?php ' . serialize($_SESSION);
  301. file_put_contents($session_file, $session_serialized);
  302. flock($session_file_fp, LOCK_UN);
  303. fclose($session_file_fp);
  304. }
  305. }
  306. /**
  307. * Process
  308. *
  309. * @param $msg
  310. * @param int|null $_ unused
  311. * @throws Exception
  312. */
  313. public function send($msg, ?int $_ = null)
  314. {
  315. $this->log->log('SEND: ' . $msg, Log::LEVEL_VERBOSE);
  316. $msg = new SimpleXMLElement($msg);
  317. $this->__sendBody($this->__buildBody($msg), true);
  318. }
  319. /**
  320. * Reset
  321. *
  322. * @throws Exception
  323. */
  324. public function reset(): void
  325. {
  326. $this->xml_depth = 0;
  327. unset($this->xmlobj);
  328. $this->xmlobj = [];
  329. $this->setupParser();
  330. $body = $this->__buildBody();
  331. $body->addAttribute('to', $this->host);
  332. $body->addAttribute('xmpp:restart', 'true', 'urn:xmpp:xbosh');
  333. $buff = '<stream:stream xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams">';
  334. $this->__sendBody($body);
  335. $this->been_reset = true;
  336. xml_parse($this->parser, $buff, false);
  337. }
  338. /**
  339. * Disconnect
  340. *
  341. * @throws Exception
  342. */
  343. public function disconnect(): void
  344. {
  345. parent::disconnect();
  346. if ($this->session == 'ON_FILE') {
  347. unlink($this->getSessionFile());
  348. } else {
  349. $keys = ['RID', 'SID', 'authed', 'basejid', 'fulljid', 'inactivity', 'lat'];
  350. foreach ($keys as $key) {
  351. unset($_SESSION['XMPPHP_BOSH'][$key]);
  352. }
  353. }
  354. }
  355. }