Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
secretary.php
1<?php
3
10
12{
13 public static function getDirectMessageUrl(int $messageId): string
14 {
15 return sprintf('/mail/message/%u', $messageId);
16 }
17
18 public static function getMessage(int $id): \Bitrix\Mail\Item\Message
19 {
20 return self::getMessageStorage()->getMessage($id);
21 }
22
23 public static function getMessageUrlForChat(int $messageId, int $chatId): ?string
24 {
25 return self::getMessageUrl($messageId, Message::ENTITY_TYPE_IM_CHAT, $chatId);
26 }
27
28 public static function getMessageUrlForCalendarEvent(int $messageId, int $eventId): ?string
29 {
30 return self::getMessageUrl($messageId, Message::ENTITY_TYPE_CALENDAR_EVENT, $eventId);
31 }
32
33 // public static function getMessageUrlForUser(int $messageId, int $userId): ?string
34 // {
35 // return self::getMessageUrl($messageId, null, null, $userId);
36 // }
37
38 public static function getMailboxIdForMessage(int $messageId)
39 {
40 $mailboxId = MailMessageTable::getList([
41 'select' => [
42 'MAILBOX_ID'
43 ],
44 'filter' => [
45 '=ID' => $messageId,
46 ],
47 ])->fetchAll();
48
49 if(isset($mailboxId[0]['MAILBOX_ID']))
50 {
51 return (int)$mailboxId[0]['MAILBOX_ID'];
52 }
53
54 return false;
55 }
56
57 private static function getMessageUrl(int $messageId, string $entityType, int $entityId, ?int $userId = null): ?string
58 {
59 $message = MailMessageTable::getList([
60 'select' => [
61 '*',
62 'MAILBOX_EMAIL' => 'MAILBOX.EMAIL',
63 'MAILBOX_NAME' => 'MAILBOX.NAME',
64 'MAILBOX_LOGIN' => 'MAILBOX.LOGIN',
65 ],
66 'filter' => [
67 '=ID' => $messageId,
68 ],
69 ])->fetch();
70
71 if (!$message)
72 {
73 return null;
74 }
75
76 $filter = [
77 '=MAILBOX_ID' => $message['MAILBOX_ID'],
78 '=MESSAGE_ID' => $message['ID'],
79 // '=ENTITY_UF_ID' => $userField['ID'],
80 ];
81
82 if ($entityType)
83 {
84 $filter['=ENTITY_TYPE'] = $entityType;
85 $filter['=ENTITY_ID'] = $entityId;
86 }
87
88 $access = MessageAccessTable::getList([
89 'filter' => $filter,
90 'limit' => 1,
91 ])->fetch();
92
93 if (!$access)
94 {
95 return null;
96 }
97
98 \Bitrix\Mail\Helper\Message::prepare($message);
99
100 $signer = new \Bitrix\Main\Security\Sign\Signer(new \Bitrix\Main\Security\Sign\HmacAlgorithm('md5'));
101
102 $message['__href'] = \CHTTP::urlAddParams(
103 $message['__href'],
104 array(
105 'mail_uf_message_token' => sprintf(
106 '%s:%s',
107 $access['TOKEN'],
108 $signer->getSignature($access['SECRET'], Message::getSaltByEntityType($entityType, $entityId, $userId))
109 ),
110 ),
111 array(
112 'encode' => true,
113 )
114 );
115
116 return $message['__href'];
117 }
118
119 public static function provideAccessToMessage(int $mailMessageId, string $entityType, int $entityId, int $userId): bool
120 {
121 $message = MailMessageTable::getList([
122 'select' => [
123 'ID', 'MAILBOX_ID',
124 ],
125 'filter' => [
126 '=ID' => $mailMessageId,
127 ],
128 ])->fetch();
129
130 if (MessageAccess::isMailboxOwner($message['MAILBOX_ID'], $userId))
131 {
133 MessageAccessTable::add([
134 'TOKEN' => MessageAccess::createToken($message['MAILBOX_ID'], $mailMessageId, $entityType, $entityId, '0'),
135 'MAILBOX_ID' => $message['MAILBOX_ID'],
136 'MESSAGE_ID' => $mailMessageId,
137 'ENTITY_UF_ID' => '0',
138 'ENTITY_TYPE' => $entityType,
139 'ENTITY_ID' => $entityId,
140 'SECRET' => MessageAccess::createSecret(),
141 'OPTIONS' => [],
142 ]);
143
144 return true;
145 }
146
147 return false;
148 }
149
150 public static function onTaskDelete($taskId)
151 {
152 $messageAccessQuery = MessageAccessTable::query()
153 ->setSelect([
154 'TOKEN',
155 'MESSAGE_ID',
156 'MAILBOX_ID',
157 ])
158 ->setFilter([
159 '=ENTITY_TYPE' => MessageAccessTable::ENTITY_TYPE_TASKS_TASK,
160 '=ENTITY_ID' => $taskId,
161 ]);;
162
163 while ($messageAccess = $messageAccessQuery->fetch())
164 {
165 $messageId = $messageAccess['MESSAGE_ID'];
166 $mailboxId = $messageAccess['MAILBOX_ID'];
167 MessageAccessTable::delete(['TOKEN' => $messageAccess['TOKEN']]);
168
169 if (Loader::includeModule('pull'))
170 {
171 if($mailboxId)
172 {
173 \CPullWatch::addToStack(
174 'mail_mailbox_' . $mailboxId,
175 [
176 'module_id' => 'mail',
177 'command' => 'messageBindingDeleted',
178 'params' => [
179 'messageId' => $messageId,
180 'mailboxId' => $mailboxId,
181 'entityType' => MessageAccessTable::ENTITY_TYPE_TASKS_TASK,
182 'entityId' => $taskId,
183 ],
184 ]
185 );
186 }
187 }
188 }
189 }
190
191 private static function getMessageStorage(): IMessageStorage
192 {
193 return new \Bitrix\Mail\Storage\Message();
194 }
195
196}
static isMailboxOwner(int $mailboxId, int $userId)
static createToken($mailBoxId, $messageId, $entityType, $entityId, $ufId='0')
static getSaltByEntityType(string $entityType, int $entityId, ?int $userId=null)
Definition message.php:731
static getMessageUrlForChat(int $messageId, int $chatId)
Definition secretary.php:23
static getDirectMessageUrl(int $messageId)
Definition secretary.php:13
static getMessageUrlForCalendarEvent(int $messageId, int $eventId)
Definition secretary.php:28
static getMailboxIdForMessage(int $messageId)
Definition secretary.php:38
static provideAccessToMessage(int $mailMessageId, string $entityType, int $entityId, int $userId)