1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2014 Fabian Grutschus. All rights reserved. |
5
|
|
|
* |
6
|
|
|
* Redistribution and use in source and binary forms, with or without modification, |
7
|
|
|
* are permitted provided that the following conditions are met: |
8
|
|
|
* |
9
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this |
10
|
|
|
* list of conditions and the following disclaimer. |
11
|
|
|
* |
12
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice, |
13
|
|
|
* this list of conditions and the following disclaimer in the documentation |
14
|
|
|
* and/or other materials provided with the distribution. |
15
|
|
|
* |
16
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
17
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
18
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
19
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
20
|
|
|
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
21
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
22
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
23
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
25
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26
|
|
|
* |
27
|
|
|
* The views and conclusions contained in the software and documentation are those |
28
|
|
|
* of the authors and should not be interpreted as representing official policies, |
29
|
|
|
* either expressed or implied, of the copyright holders. |
30
|
|
|
* |
31
|
|
|
* @author Fabian Grutschus <[email protected]> |
32
|
|
|
* @copyright 2014 Fabian Grutschus. All rights reserved. |
33
|
|
|
* @license BSD |
34
|
|
|
* @link http://github.com/fabiang/xmpp |
35
|
|
|
*/ |
36
|
|
|
|
37
|
|
|
namespace Fabiang\Xmpp; |
38
|
|
|
|
39
|
|
|
use Fabiang\Xmpp\Options; |
40
|
|
|
use Fabiang\Xmpp\Connection\ConnectionInterface; |
41
|
|
|
use Fabiang\Xmpp\Connection\Socket; |
42
|
|
|
use Fabiang\Xmpp\Protocol\ProtocolImplementationInterface; |
43
|
|
|
use Fabiang\Xmpp\Event\EventManagerAwareInterface; |
44
|
|
|
use Fabiang\Xmpp\Event\EventManagerInterface; |
45
|
|
|
use Fabiang\Xmpp\Event\EventManager; |
46
|
|
|
use Fabiang\Xmpp\EventListener\Logger; |
47
|
|
|
use Fabiang\Xmpp\Exception\TimeoutException; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Xmpp connection client. |
51
|
|
|
* |
52
|
|
|
* @package Xmpp |
53
|
|
|
*/ |
54
|
|
|
class Client implements EventManagerAwareInterface |
55
|
|
|
{ |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Eventmanager. |
59
|
|
|
* |
60
|
|
|
* @var EventManagerInterface |
61
|
|
|
*/ |
62
|
|
|
protected $eventManager; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Options. |
66
|
|
|
* |
67
|
|
|
* @var Options |
68
|
|
|
*/ |
69
|
|
|
protected $options; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var ConnectionInterface |
73
|
|
|
*/ |
74
|
|
|
protected $connection; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Constructor. |
78
|
|
|
* |
79
|
|
|
* @param Options $options Client options |
80
|
|
|
* @param EventManagerInterface $eventManager Event manager |
81
|
6 |
|
*/ |
82
|
|
|
public function __construct(Options $options, EventManagerInterface $eventManager = null) |
83
|
|
|
{ |
84
|
6 |
|
// create default connection |
85
|
6 |
|
if (null !== $options->getConnection()) { |
86
|
6 |
|
$connection = $options->getConnection(); |
87
|
3 |
|
} else { |
88
|
3 |
|
$connection = Socket::factory($options); |
89
|
|
|
$options->setConnection($connection); |
90
|
6 |
|
} |
91
|
6 |
|
$this->options = $options; |
92
|
|
|
$this->connection = $connection; |
93
|
6 |
|
|
94
|
3 |
|
if (null === $eventManager) { |
95
|
3 |
|
$eventManager = new EventManager(); |
96
|
6 |
|
} |
97
|
|
|
$this->eventManager = $eventManager; |
98
|
6 |
|
|
99
|
6 |
|
$this->setupImplementation(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Setup implementation. |
104
|
|
|
* |
105
|
|
|
* @return void |
106
|
3 |
|
*/ |
107
|
|
|
protected function setupImplementation() |
108
|
3 |
|
{ |
109
|
3 |
|
$this->connection->setEventManager($this->eventManager); |
110
|
|
|
$this->connection->setOptions($this->options); |
111
|
3 |
|
|
112
|
3 |
|
$implementation = $this->options->getImplementation(); |
113
|
3 |
|
$implementation->setEventManager($this->eventManager); |
114
|
3 |
|
$implementation->setOptions($this->options); |
115
|
|
|
$implementation->register(); |
116
|
3 |
|
|
117
|
3 |
|
$implementation->registerListener(new Logger()); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Connect to server. |
122
|
|
|
* |
123
|
|
|
* @return void |
124
|
3 |
|
*/ |
125
|
|
|
public function connect() |
126
|
3 |
|
{ |
127
|
3 |
|
$this->connection->connect(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Disconnect from server. |
132
|
|
|
* |
133
|
|
|
* @return void |
134
|
3 |
|
*/ |
135
|
|
|
public function disconnect() |
136
|
3 |
|
{ |
137
|
3 |
|
$this->connection->disconnect(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Send data to server. |
142
|
|
|
* |
143
|
|
|
* @param ProtocolImplementationInterface $interface Interface |
144
|
|
|
* @return void |
145
|
3 |
|
*/ |
146
|
|
|
public function send(ProtocolImplementationInterface $interface) |
147
|
3 |
|
{ |
148
|
3 |
|
$data = $interface->toString(); |
149
|
3 |
|
$this->connection->send($data); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* {@inheritDoc} |
154
|
3 |
|
*/ |
155
|
|
|
public function getEventManager() |
156
|
3 |
|
{ |
157
|
|
|
return $this->eventManager; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* {@inheritDoc} |
162
|
3 |
|
*/ |
163
|
|
|
public function setEventManager(EventManagerInterface $eventManager) |
164
|
3 |
|
{ |
165
|
3 |
|
$this->eventManager = $eventManager; |
166
|
|
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Get options. |
171
|
|
|
* |
172
|
|
|
* @return Options |
173
|
3 |
|
*/ |
174
|
|
|
public function getOptions() |
175
|
3 |
|
{ |
176
|
|
|
return $this->options; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @return ConnectionInterface |
181
|
3 |
|
*/ |
182
|
|
|
public function getConnection() |
183
|
3 |
|
{ |
184
|
|
|
return $this->connection; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @return array |
189
|
|
|
*/ |
190
|
|
|
public function getMessages() |
191
|
|
|
{ |
192
|
|
|
$result = []; |
193
|
|
|
$connection = $this->getConnection(); |
194
|
|
|
$connection->getSocket()->setBlocking(false); |
195
|
|
|
$input = null; |
196
|
|
|
try { |
197
|
|
|
$input = $connection->receive(); |
198
|
|
|
} catch (TimeoutException $e) {} |
|
|
|
|
199
|
|
|
$node = $connection->getInputStream()->parse($input); |
200
|
|
|
$messages = $node->getElementsByTagName('message'); |
201
|
|
|
foreach ($messages as $message) { |
202
|
|
|
if (in_array($message->getAttribute('type'), ['chat', 'groupchat'])) { |
203
|
|
|
$from = $message->getAttribute('from'); |
204
|
|
|
$body = $message->getElementsByTagName('body'); |
205
|
|
|
if (isset($body[0]->textContent)) { |
206
|
|
|
$body = $body[0]->textContent; |
207
|
|
|
} else { |
208
|
|
|
$body = $message->textContent; |
209
|
|
|
} |
210
|
|
|
$result[] = [ |
211
|
|
|
'from' => $from, |
212
|
|
|
'message' => $body, |
213
|
|
|
]; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
return $result; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|