1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
<?php
namespace OCA\OJSXC\StanzaHandlers;
use OCA\OJSXC\Db\MessageMapper;
use Sabre\Xml\Reader;
use Sabre\Xml\Writer;
use OCA\OJSXC\Db\Message as MessageEntity;
/**
* Class Message
*
* @package OCA\OJSXC\StanzaHandlers
*/
class Message extends StanzaHandler {
/**
* @var MessageMapper $messageMapper
*/
private $messageMapper;
/**
* @var string $type
*/
private $type;
/**
* @var array $values
*/
private $values;
/**
* @var string $msgId
*/
private $msgId;
/**
* Message constructor.
*
* @param string $userId
* @param string $host
* @param MessageMapper $messageMapper
*/
public function __construct($userId, $host, MessageMapper $messageMapper) {
parent::__construct($userId, $host);
$this->messageMapper = $messageMapper;
}
/**
* @param array $stanza
*/
public function handle(array $stanza) {
$to = $this->getAttribute($stanza, 'to');
$pos = strpos($to, '@');
$this->to = substr($to, 0, $pos);
foreach($stanza['value'] as $keyRaw=>$value) {
// remove namespace from key as it is unneeded and cause problems
$key = substr($keyRaw, strpos($keyRaw, '}') + 1, strlen($keyRaw));
// fetch namespace from key to readd it
$ns = substr($keyRaw, 1, strpos($keyRaw, '}')-1);
$this->values[] = [
"name" => $key,
"value" => (string)$value,
"attributes" => ["xmlns" => $ns]
];
}
$this->type = $this->getAttribute($stanza, 'type');
$this->msgId = $this->getAttribute($stanza, 'id');
$message = new MessageEntity();
$message->setTo($this->to);
$message->setFrom($this->from);
$message->setValue($this->values);
$message->setType($this->type);
$this->messageMapper->insert($message);
$this->values = [];
}
}
|