1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
rest.php
См. документацию.
1<?
3
4if(!CModule::IncludeModule('rest'))
5 return;
6
8{
9 const PERM_DENY = 'D';
10 const PERM_READ = 'R';
11 const PERM_WRITE = 'W';
12
13 public static function OnRestServiceBuildDescription()
14 {
15 return array(
16 "forum" => array(
17 "forum.message.user.get" => array('callback' => array(__CLASS__, 'getUserMessage'), 'options' => array('private' => true)),
18 "forum.message.delete" => array(__CLASS__, "deleteMessage")
19 )
20 );
21 }
22
23 public static function getUserMessage($arParams, $offset, CRestServer $server)
24 {
25 $arParams = array_change_key_case($arParams, CASE_UPPER);
26
27 global $USER;
28
29 $result = array(
30 'MESSAGES' => array(),
31 'FILES' => array(),
32 );
33
34 $userId = (
35 isset($arParams["USER_ID"])
36 && intval($arParams["USER_ID"]) > 0
37 && self::isAdmin()
38 ? $arParams["USER_ID"]
39 : $USER->getId()
40 );
41
42 $otherUserMode = ($userId != $USER->getId());
43
44 if ($userId <= 0)
45 {
46 throw new Bitrix\Rest\RestException("User ID can't be empty", "ID_EMPTY", CRestServer::STATUS_WRONG_REQUEST);
47 }
48
49 if (isset($arParams['FIRST_ID']))
50 {
51 $options['FIRST_ID'] = intval($arParams['FIRST_ID']);
52 }
53 else
54 {
55 $options['LAST_ID'] = isset($arParams['LAST_ID']) && intval($arParams['LAST_ID']) > 0? intval($arParams['LAST_ID']): 0;
56 }
57
58 $options['LIMIT'] = isset($arParams['LIMIT'])? (intval($arParams['LIMIT']) > 1000? 1000: intval($arParams['LIMIT'])): 100;
59
60 $filter = Array(
61 '=AUTHOR_ID' => $userId
62 );
63
64 if (isset($options['FIRST_ID']))
65 {
66 $order = array();
67
68 if (intval($options['FIRST_ID']) > 0)
69 {
70 $filter['>ID'] = $options['FIRST_ID'];
71 }
72 }
73 else
74 {
75 $order = array('ID' => 'DESC');
76
77 if (isset($options['LAST_ID']) && intval($options['LAST_ID']) > 0)
78 {
79 $filter['<ID'] = intval($options['LAST_ID']);
80 }
81 }
82
83 $res = Bitrix\Forum\MessageTable::getList(array(
84 'filter' => $filter,
85 'select' => array(
86 'ID', 'POST_DATE', 'POST_MESSAGE', 'UF_FORUM_MESSAGE_DOC'
87 ),
88 'order' => $order,
89 'limit' => $options['LIMIT']
90 ));
91
92 $attachedIdList = array();
93 $messageAttachedList = array();
94
95 while($messageFields = $res->fetch())
96 {
97 $result['MESSAGES'][$messageFields['ID']] = array(
98 'ID' => (int)$messageFields['ID'],
99 'MESSAGE_ID' => (int)$messageFields['ID'],
100 'DATE' => $messageFields['POST_DATE'],
101 'MESSAGE' => ($otherUserMode ? '' : (string)$messageFields['POST_MESSAGE']),
102 'ATTACH' => array()
103 );
104
105 if (!empty($messageFields['UF_FORUM_MESSAGE_DOC']))
106 {
107 if (is_array($messageFields['UF_FORUM_MESSAGE_DOC']))
108 {
109 $attached = $messageFields['UF_FORUM_MESSAGE_DOC'];
110 }
111 elseif (intval($messageFields['UF_FORUM_MESSAGE_DOC']) > 0)
112 {
113 $attached = array(intval($messageFields['UF_FORUM_MESSAGE_DOC']));
114 }
115 else
116 {
117 $attached = array();
118 }
119
120 if (!empty($attached))
121 {
122 $attachedIdList = array_merge($attachedIdList, $attached);
123 }
124
125 $messageAttachedList[$messageFields['ID']] = $attached;
126 }
127 }
128
129 $attachedObjectList = array();
130
131 if (
132 !empty($attachedIdList)
133 && Loader::includeModule('disk')
134 )
135 {
136 $res = Bitrix\Disk\AttachedObject::getList(array(
137 'filter' => array(
138 '@ID' => array_unique($attachedIdList)
139 ),
140 'select' => array('ID', 'OBJECT_ID')
141 ));
142 while($attachedObjectFields = $res->fetch())
143 {
144 $diskObjectId = $attachedObjectFields['OBJECT_ID'];
145
146 if ($fileData = self::getFileData($diskObjectId))
147 {
148 $attachedObjectList[$attachedObjectFields['ID']] = $diskObjectId;
149 $result['FILES'][$diskObjectId] = $fileData;
150 }
151 }
152 }
153
154 foreach ($result['MESSAGES'] as $key => $value)
155 {
156 if ($value['DATE'] instanceof \Bitrix\Main\Type\DateTime)
157 {
158 $result['MESSAGES'][$key]['DATE'] = date('c', $value['DATE']->getTimestamp());
159 }
160
161 if (!empty($messageAttachedList[$key]))
162 {
163 foreach($messageAttachedList[$key] as $attachedId)
164 {
165 if (!empty($attachedObjectList[$attachedId]))
166 {
167 $result['MESSAGES'][$key]['ATTACH'][] = $attachedObjectList[$attachedId];
168 }
169 }
170 }
171
172 $result['MESSAGES'][$key] = array_change_key_case($result['MESSAGES'][$key], CASE_LOWER);
173 }
174
175 $result['MESSAGES'] = array_values($result['MESSAGES']);
176 $result['FILES'] = self::convertFileData($result['FILES']);
177
178 return $result;
179 }
180
181 public static function deleteMessage($arFields)
182 {
183 global $USER;
184 static $obCache = null;
185
186 $messageId = intval($arFields['MESSAGE_ID']);
187
188 if($messageId <= 0)
189 {
190 throw new Exception('Wrong message ID');
191 }
192
193 $currentUserId = (
194 isset($arFields["USER_ID"])
195 && intval($arFields["USER_ID"]) > 0
196 && self::isAdmin()
197 ? $arFields["USER_ID"]
198 : $USER->getId()
199 );
200
201 $arMessage = self::getForumMessageFields($messageId);
202 if (empty($arMessage))
203 {
204 throw new Exception('No message found');
205 }
206
207 $currentUserPerm = self::getForumMessagePerm(array(
208 'USER_ID' => $currentUserId,
209 'MESSAGE_ID' => $messageId
210 ));
211
212 if ($currentUserPerm < self::PERM_WRITE)
213 {
214 throw new Exception('No write perms');
215 }
216
217 if (
219 && Loader::includeModule('socialnetwork')
220 )
221 {
222 $logIdList = array();
223
225 'filter' => array(
226 '=SOURCE_ID' => $messageId,
227 '@EVENT_ID' => array('forum') // replace with provider getEventId
228 ),
229 'select' => array('ID')
230 ));
231 while ($logFields = $res->fetch())
232 {
233 if (CSocNetLog::delete($logFields['ID']))
234 {
235 $logIdList[] = intval($logFields['ID']);
236 }
237 }
238
239 if (empty($logIdList))
240 {
242 'filter' => array(
243 '=SOURCE_ID' => $messageId,
244 '@EVENT_ID' => array('forum', 'tasks_comment', 'calendar_comment', 'timeman_entry_comment', 'report_comment', 'photo_comment', 'wiki_comment', 'lists_new_element_comment') // replace with provider getEventId
245 ),
246 'select' => array('ID', 'LOG_ID')
247 ));
248 while ($logCommentFields = $res->fetch())
249 {
250 if (CSocNetLogComments::delete($logCommentFields['ID']))
251 {
252 $logIdList[] = intval($logFields['LOG_ID']);
253 }
254 }
255 }
256
257 if (!empty($logIdList))
258 {
259 foreach($logIdList as $logId)
260 {
261 if ($obCache === null)
262 {
263 $obCache = new CPHPCache;
264 }
265 $obCache->CleanDir("/sonet/log/".intval($logId / 1000)."/".$logId."/comments/");
266 }
267 }
268 }
269
270 return (bool)$result;
271 }
272
273 private static function getForumMessagePerm($arFields)
274 {
275 global $USER;
276
277 $result = self::PERM_DENY;
278
279 $messageId = $arFields['MESSAGE_ID'];
280
281 $currentUserId = (
282 isset($arFields["USER_ID"])
283 && intval($arFields["USER_ID"]) > 0
284 && $USER->isAdmin()
285 ? $arFields["USER_ID"]
286 : $USER->getId()
287 );
288
289 $arMessage = self::getForumMessageFields($messageId);
290 if (empty($arMessage))
291 {
292 return $result;
293 }
294
295 if (
296 $arMessage["AUTHOR_ID"] == $currentUserId
297 || (
298 Loader::includeModule('socialnetwork')
299 && CSocNetUser::isUserModuleAdmin($currentUserId, SITE_ID)
300 )
301 )
302 {
303 $result = self::PERM_WRITE;
304 }
305
306 return $result;
307 }
308
309 private static function getForumMessageFields($messageId)
310 {
311 $result = array();
312
313 $res = \Bitrix\Forum\MessageTable::getList(array(
314 'filter' => array(
315 '=ID' => $messageId
316 ),
317 'select' => array('*')
318 ));
319 if ($messageFields = $res->fetch())
320 {
322 }
323 return $result;
324 }
325
326 private static function isAdmin()
327 {
328 global $USER;
329 return (
330 $USER->isAdmin()
331 || (
332 Loader::includeModule('bitrix24')
333 && \CBitrix24::isPortalAdmin($USER->getId())
334 )
335 );
336 }
337
338 private static function getFileData($diskObjectId)
339 {
340 $result = false;
341
342 $diskObjectId = intval($diskObjectId);
343 if ($diskObjectId <= 0)
344 {
345 return $result;
346 }
347
348 if ($fileModel = \Bitrix\Disk\File::getById($diskObjectId))
349 {
351 $contentType = 'file';
352 $imageParams = false;
353 if (\Bitrix\Disk\TypeFile::isImage($fileModel))
354 {
355 $contentType = 'image';
356 $params = $fileModel->getFile();
357 $imageParams = Array(
358 'width' => (int)$params['WIDTH'],
359 'height' => (int)$params['HEIGHT'],
360 );
361 }
362 else if (\Bitrix\Disk\TypeFile::isVideo($fileModel->getName()))
363 {
364 $contentType = 'video';
365 $params = $fileModel->getView()->getPreviewData();
366 $imageParams = Array(
367 'width' => (int)$params['WIDTH'],
368 'height' => (int)$params['HEIGHT'],
369 );
370 }
371
372 $isImage = \Bitrix\Disk\TypeFile::isImage($fileModel);
373 $urlManager = \Bitrix\Disk\Driver::getInstance()->getUrlManager();
374
375 $result = array(
376 'id' => (int)$fileModel->getId(),
377 'date' => $fileModel->getCreateTime(),
378 'type' => $contentType,
379 'name' => $fileModel->getName(),
380 'size' => (int)$fileModel->getSize(),
381 'image' => $imageParams,
382 'authorId' => (int)$fileModel->getCreatedBy(),
383 'authorName' => CUser::FormatName(CSite::getNameFormat(false), $fileModel->getCreateUser(), true, true),
384 'urlPreview' => (
385 $fileModel->getPreviewId()
386 ? $urlManager->getUrlForShowPreview($fileModel, [ 'width' => 640, 'height' => 640])
387 : (
388 $isImage
389 ? $urlManager->getUrlForShowFile($fileModel, [ 'width' => 640, 'height' => 640])
390 : null
391 )
392 ),
393 'urlShow' => ($isImage ? $urlManager->getUrlForShowFile($fileModel) : $urlManager->getUrlForDownloadFile($fileModel)),
394 'urlDownload' => $urlManager->getUrlForDownloadFile($fileModel)
395 );
396 }
397
398 return $result;
399 }
400
401 private static function convertFileData($fileData)
402 {
403 if (!is_array($fileData))
404 {
405 return array();
406 }
407
408 foreach ($fileData as $key => $value)
409 {
410 if ($value['date'] instanceof \Bitrix\Main\Type\DateTime)
411 {
412 $fileData[$key]['date'] = date('c', $value['date']->getTimestamp());
413 }
414
415 foreach (['urlPreview', 'urlShow', 'urlDownload'] as $field)
416 {
417 $url = $fileData[$key][$field];
418 if (is_string($url) && $url && mb_strpos($url, 'http') !== 0)
419 {
420 $fileData[$key][$field] = self::getPublicDomain().$url;
421 }
422 }
423 }
424
425 return $fileData;
426 }
427
428 private static function getPublicDomain()
429 {
430 static $result = null;
431 if ($result === null)
432 {
433 $result = (\Bitrix\Main\Context::getCurrent()->getRequest()->isHttps() ? "https" : "http")."://".((defined("SITE_SERVER_NAME") && SITE_SERVER_NAME <> '') ? SITE_SERVER_NAME : \Bitrix\Main\Config\Option::get("main", "server_name", $_SERVER['SERVER_NAME']));
434 }
435
436 return $result;
437 }
438}
439?>
$arParams
Определения access_dialog.php:21
$messageFields
Определения callback_ednaru.php:22
if(! $messageFields||!isset($messageFields['message_id'])||!isset($messageFields['status'])||!CModule::IncludeModule("messageservice")) $messageId
Определения callback_ismscenter.php:26
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
Определения loader.php:13
static getList(array $parameters=array())
Определения datamanager.php:431
static Delete($ID)
Определения message.php:528
Определения rest.php:8
static deleteMessage($arFields)
Определения rest.php:181
const PERM_READ
Определения rest.php:10
const PERM_DENY
Определения rest.php:9
static OnRestServiceBuildDescription()
Определения rest.php:13
static getUserMessage($arParams, $offset, CRestServer $server)
Определения rest.php:23
const PERM_WRITE
Определения rest.php:11
Определения cache.php:11
Определения rest.php:24
Определения rest.php:896
$options
Определения commerceml2.php:49
$arFields
Определения dblapprove.php:5
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$res
Определения filter_act.php:7
$result
Определения get_property_values.php:14
$filter
Определения iblock_catalog_list.php:54
$_SERVER["DOCUMENT_ROOT"]
Определения cron_frame.php:9
global $USER
Определения csv_new_run.php:40
$order
Определения payment.php:8
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
if(empty($signedUserToken)) $key
Определения quickway.php:257
$contentType
Определения quickway.php:301
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799
const SITE_ID
Определения sonet_set_content_view.php:12
$url
Определения iframe.php:7