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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
<?php
namespace OCA\OJSXC\StanzaHandlers;
use OCA\OJSXC\AppInfo\Application;
use OCA\OJSXC\Db\MessageMapper;
use OCA\OJSXC\IUserProvider;
use OCP\ILogger;
use OCA\OJSXC\Db\Message as MessageEntity;
/**
* Class Message
*
* @package OCA\OJSXC\StanzaHandlers
*/
class Message extends StanzaHandler
{
/**
* @var MessageMapper $messageMapper
*/
private $messageMapper;
/**
* @var IUserProvider $userProvider
*/
private $userProvider;
/**
* @var string $type
*/
private $type;
/**
* @var array $values
*/
private $values;
/**
* @var string $msgId
*/
private $msgId;
/**
* @var ILogger $logger
*/
private $logger;
/**
* Message constructor.
*
* @param string $userId
* @param string $host
* @param MessageMapper $messageMapper
* @param IUserProvider $userProvider
*/
public function __construct($userId, $host, MessageMapper $messageMapper, IUserProvider $userProvider, ILogger $logger)
{
parent::__construct($userId, $host);
$this->messageMapper = $messageMapper;
$this->userProvider = $userProvider;
$this->logger = $logger;
}
/**
* @param array $stanza
*/
public function handle(array $stanza)
{
// Parse the username from the XML stanza to a NC userid
$to = $this->getAttribute($stanza, 'to');
$pos = strrpos($to, '@');
$this->to = substr($to, 0, $pos);
$this->to = Application::convertToRealUID(Application::deSanitize($this->to));
if (!$this->userProvider->hasUserByUID($this->to)) {
$this->logger->warning('User ' . $this->userId . ' is trying to send a message to ' . $this->to . ' but this isn\'t allowed');
return;
}
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 read 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->userId);
$message->setValue($this->values);
$message->setType($this->type);
$this->messageMapper->insert($message);
$this->values = [];
}
}
|