Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
alias.php
1<?php
2namespace Bitrix\Im;
3
4use Bitrix\Main\Entity\ExpressionField;
6
7class Alias
8{
9 const ENTITY_TYPE_USER = 'USER';
10 const ENTITY_TYPE_CHAT = 'CHAT';
11 const ENTITY_TYPE_OPEN_LINE = 'LINES';
12 const ENTITY_TYPE_LIVECHAT = 'LIVECHAT';
13 const ENTITY_TYPE_VIDEOCONF = 'VIDEOCONF';
14 const ENTITY_TYPE_JITSICONF = 'JITSICONF';
15 const ENTITY_TYPE_OTHER = 'OTHER';
16
17 const CACHE_TTL = 31536000;
18 const CACHE_PATH = '/bx/im/alias/';
19
20 const FILTER_BY_ALIAS = 'alias';
21 const FILTER_BY_ID = 'id';
22
23 public static function add(array $fields)
24 {
25 $alias = self::prepareAlias($fields['ALIAS']);
26 $entityType = $fields['ENTITY_TYPE'];
27 $entityId = $fields['ENTITY_ID'];
28
29 if (
30 ($fields['ENTITY_TYPE'] !== self::ENTITY_TYPE_VIDEOCONF && empty($entityId))
31 || empty($entityType)
32 || empty($alias))
33 {
34 return false;
35 }
36
37 $aliasData = self::get($alias);
38 if ($aliasData)
39 return false;
40
41 $result = \Bitrix\Im\Model\AliasTable::add(Array(
42 'ALIAS' => $alias,
43 'ENTITY_TYPE' => $entityType,
44 'ENTITY_ID' => $entityId,
45 'DATE_CREATE' => new DateTime()
46 ));
47 if (!$result->isSuccess())
48 {
49 return false;
50 }
51
52 return $result->getId();
53 }
54
55 public static function addUnique(array $fields)
56 {
57 $alias = \Bitrix\Im\Alias::prepareAlias(self::generateUnique());
58 $fields['ALIAS'] = $alias;
59
60 $id = self::add($fields);
61 if (!$id)
62 {
63 return self::addUnique($fields);
64 }
65
66 return Array(
67 'ID' => $id,
68 'ALIAS' => $alias,
69 'LINK' => self::getPublicLink($fields['ENTITY_TYPE'], $alias)
70 );
71 }
72
73 public static function update($id, $fields)
74 {
75 $id = intval($id);
76 if ($id <= 0)
77 return false;
78
79 $update = Array();
80 if (isset($fields['ALIAS']))
81 {
82 $update['ALIAS'] = self::prepareAlias($fields['ALIAS']);
83 $result = self::get($update['ALIAS']);
84 if ($result)
85 {
86 return false;
87 }
88 }
89
90 if (isset($fields['ENTITY_TYPE']))
91 {
92 $update['ENTITY_TYPE'] = $fields['ENTITY_TYPE'];
93 }
94 if (isset($fields['ENTITY_ID']))
95 {
96 $update['ENTITY_ID'] = $fields['ENTITY_ID'];
97 }
98
99 if (empty($update))
100 return false;
101
102 \Bitrix\Im\Model\AliasTable::update($id, $update);
103
104 return true;
105 }
106
107 public static function delete($id, $filter = self::FILTER_BY_ID)
108 {
109 if ($filter == self::FILTER_BY_ALIAS)
110 {
111 $aliasData = self::get($id);
112 if (!$aliasData)
113 return false;
114 }
115 else
116 {
117 $aliasData['ID'] = intval($id);
118 }
119
120 \Bitrix\Im\Model\AliasTable::delete($aliasData['ID']);
121
122 return true;
123 }
124
125 public static function get($alias)
126 {
127 $alias = self::prepareAlias($alias);
128 if (empty($alias))
129 {
130 return false;
131 }
132
133 $query = \Bitrix\Im\Model\AliasTable::query();
134
135 $connection = \Bitrix\Main\Application::getConnection();
136 if ($connection instanceof \Bitrix\Main\DB\PgsqlConnection)
137 {
138 $alias = $connection->getSqlHelper()->forSql($alias);
139 $query
140 ->setSelect(['*'])
141 ->whereExpr("LOWER(%s) = LOWER('{$alias}')", ['ALIAS'])
142 ;
143 }
144 else
145 {
146 $query
147 ->setSelect(['*'])
148 ->where('ALIAS', $alias)
149 ;
150 }
151
152 $result = $query->exec()->fetch();
153
154 if (!$result)
155 {
156 return false;
157 }
158
159 $result['LINK'] = self::getPublicLink($result['ENTITY_TYPE'], $result['ALIAS']);
160
161 return $result;
162 }
163
164 public static function getByIdAndCode($id, $code)
165 {
166 $query = \Bitrix\Im\Model\AliasTable::query();
167 $query
168 ->setSelect(['*'])
169 ->where('ID', $id)
170 ;
171
172 $connection = \Bitrix\Main\Application::getConnection();
173 if ($connection instanceof \Bitrix\Main\DB\PgsqlConnection)
174 {
175 $code = $connection->getSqlHelper()->forSql($code);
176 $query->whereExpr("LOWER(%s) = LOWER('{$code}')", ['ALIAS']);
177 }
178 else
179 {
180 $query->where('ALIAS', $code);
181 }
182
183 return $query->exec()->fetch();
184 }
185
186 public static function getByEntity($entityType, $entityId)
187 {
188 $result = \Bitrix\Im\Model\AliasTable::getList(Array(
189 'filter' => ['=ENTITY_TYPE' => $entityType, '=ENTITY_ID' => $entityId]
190 ))->fetch();
191
192 if (!$result)
193 {
194 return false;
195 }
196
197 $result['LINK'] = self::getPublicLink($result['ENTITY_TYPE'], $result['ALIAS']);
198
199 return $result;
200 }
201
202 public static function prepareAlias($alias)
203 {
204 $alias = preg_replace("/[^\.\-0-9a-zA-Z]+/", "", $alias);
205 $alias = mb_substr($alias, 0, 255);
206
207 return $alias;
208 }
209
210 public static function getPublicLink($type, $alias)
211 {
212 $path = '/online/';
213
214 if ($type === self::ENTITY_TYPE_VIDEOCONF || $type === self::ENTITY_TYPE_JITSICONF)
215 {
216 $path = '/video/';
217 }
218 else if ($type === self::ENTITY_TYPE_LIVECHAT)
219 {
220 return '';
221 }
222
223 return \Bitrix\Im\Common::getPublicDomain() . $path . $alias;
224 }
225
226 public static function generateUnique()
227 {
228 if (\Bitrix\Main\Loader::includeModule('security'))
229 {
230 return \Bitrix\Main\Security\Random::getString(8, true);
231 }
232 else
233 {
234 return mb_substr(uniqid(),-8);
235 }
236 }
237}
const ENTITY_TYPE_LIVECHAT
Definition alias.php:12
static generateUnique()
Definition alias.php:226
const CACHE_PATH
Definition alias.php:18
static getByIdAndCode($id, $code)
Definition alias.php:164
const ENTITY_TYPE_USER
Definition alias.php:9
const ENTITY_TYPE_VIDEOCONF
Definition alias.php:13
static update($id, $fields)
Definition alias.php:73
static addUnique(array $fields)
Definition alias.php:55
const FILTER_BY_ID
Definition alias.php:21
const ENTITY_TYPE_JITSICONF
Definition alias.php:14
static getByEntity($entityType, $entityId)
Definition alias.php:186
const CACHE_TTL
Definition alias.php:17
const ENTITY_TYPE_OPEN_LINE
Definition alias.php:11
static prepareAlias($alias)
Definition alias.php:202
const ENTITY_TYPE_OTHER
Definition alias.php:15
static getPublicLink($type, $alias)
Definition alias.php:210
static add(array $fields)
Definition alias.php:23
const ENTITY_TYPE_CHAT
Definition alias.php:10
static get($alias)
Definition alias.php:125
const FILTER_BY_ALIAS
Definition alias.php:20