Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
post.php
1<?php
8namespace Bitrix\Blog\Item;
9
10use Bitrix\Main;
16
17Loc::loadMessages(__FILE__);
18
19class Post
20{
21 public $id = false;
22
23 private $perm;
24 private $permByOpenGroup;
25 private $fields;
26
27 public function __construct()
28 {
29 $this->perm = Permissions::DENY;
30 $this->permByOpenGroup = false;
31 $this->fields = array();
32 $this->id = false;
33 }
34
35 public static function getById($postId = 0, $params = array())
36 {
37 static $cachedFields = array();
38
39 $postItem = false;
40 $postId = intval($postId);
41
42 $useStaticCache = (
43 !empty($params['USE_STATIC_CACHE'])
44 && $params['USE_STATIC_CACHE'] === true
45 );
46
47 if ($postId > 0)
48 {
49 $postItem = new Post;
50 $postFields = array();
51
52 if (
53 $useStaticCache
54 && isset($cachedFields[$postId])
55 )
56 {
57 $postFields = $cachedFields[$postId];
58 }
59 else
60 {
61 $select = array('*', 'UF_BLOG_POST_URL_PRV');
62
63 if (
64 \Bitrix\Main\Config\Option::get('disk', 'successfully_converted', false)
66 )
67 {
68 $select[] = 'UF_BLOG_POST_FILE';
69 }
70
71 if (
72 Loader::includeModule('vote')
73 && Main\ModuleManager::isModuleInstalled('socialnetwork')
74 )
75 {
76 $select[] = 'UF_BLOG_POST_VOTE';
77 }
78
79 $res = PostTable::getList(array(
80 'filter' => array('=ID' => $postId),
81 'select' => $select
82 ));
83 if ($fields = $res->fetch())
84 {
85 $postFields = $fields;
86
87 if ($postFields['DATE_CREATE'] instanceof \Bitrix\Main\Type\DateTime)
88 {
89 $postFields['DATE_CREATE'] = $postFields['DATE_CREATE']->toString();
90 }
91 if ($postFields['DATE_PUBLISH'] instanceof \Bitrix\Main\Type\DateTime)
92 {
93 $postFields['DATE_PUBLISH'] = $postFields['DATE_PUBLISH']->toString();
94 }
95 }
96
97 $cachedFields[$postId] = $postFields;
98 }
99
100 $postItem->setFields($postFields);
101 if (
102 isset($postFields['ID'])
103 && intval($postFields['ID']) > 0
104 )
105 {
106 $postItem->setId(intval($postFields['ID']));
107 }
108 }
109
110 return $postItem;
111 }
112
113 public function setFields($fields = array())
114 {
115 $this->fields = $fields;
116 }
117
118 public function getFields()
119 {
120 return $this->fields;
121 }
122
123 public function setId($id = 0)
124 {
125 $this->id = $id;
126 }
127
128 public function getId()
129 {
130 return $this->id;
131 }
132
133 public function getSonetPerms($params = array())
134 {
135 global $USER;
136
137 static $cache, $userAccessCodeCache;
138
139 if (!Loader::includeModule('socialnetwork'))
140 {
141 throw new Main\SystemException('Socialnetwork module not installed.');
142 }
143
144 $fields = $this->getFields();
145 if (
146 empty($fields)
147 || empty($fields['ID'])
148 || intval($fields['ID']) <= 0
149 )
150 {
151 throw new Main\SystemException('Empty post.');
152 }
153
154 $cacheId = md5($fields['ID'].serialize($params));
155
156 if (!empty($cache[$cacheId]))
157 {
158 return $cache[$cacheId];
159 }
160
161 $currentUser = false;
162 $userId = (isset($params["USER_ID"]) ? intval($params["USER_ID"]) : 0);
163 if($userId <= 0)
164 {
165 $userId = intval($USER->getId());
166 $currentUser = true;
167 }
168
169 $perms = Permissions::DENY;
170 $permsAvailable = array_keys($GLOBALS["AR_BLOG_PERMS"]);
171
172 if($currentUser)
173 {
174 if (\CSocNetUser::isCurrentUserModuleAdmin())
175 {
176 $perms = $permsAvailable[count($permsAvailable) - 1];
177 }
178 }
179 elseif(\CSocNetUser::isUserModuleAdmin($userId))
180 {
181 $perms = $permsAvailable[count($permsAvailable) - 1];
182 }
183
184 if(
185 isset($fields["AUTHOR_ID"])
186 && $fields["AUTHOR_ID"] == $userId
187 )
188 {
189 $perms = Permissions::FULL;
190 }
191
192 $openedWorkgroupsList = [];
193 $readByOpenSonetGroup = false;
194 $alreadyFound = false;
195
196 if ($perms <= Permissions::DENY)
197 {
198 $permsList = \CBlogPost::getSocNetPerms($fields['ID']);
199
200 if (
201 $userId > 0
203 ) // check for email authorization users
204 {
205 $select = array("ID", "EXTERNAL_AUTH_ID");
206 if (Main\ModuleManager::isModuleInstalled('intranet'))
207 {
208 $select[] = "UF_DEPARTMENT";
209 }
210 $res = Main\UserTable::getList(array(
211 'filter' => array(
212 "=ID" => $userId
213 ),
214 'select' => $select
215 ));
216
217 if($userFields = $res->fetch())
218 {
219 if ($userFields["EXTERNAL_AUTH_ID"] == 'email')
220 {
221 $alreadyFound = true;
222 $perms = (isset($permsList["U"]) && isset($permsList["U"][$userId]) ? Permissions::READ : Permissions::DENY);
223 }
224 elseif (
225 isset($params['PUBLIC']) && $params['PUBLIC']
226 && isset($userFields["UF_DEPARTMENT"]) // intranet installed
227 && (
228 !is_array($userFields["UF_DEPARTMENT"])
229 || empty($userFields["UF_DEPARTMENT"])
230 || intval($userFields["UF_DEPARTMENT"][0]) <= 0
231 )
232 && Loader::includeModule('extranet')
233 && ($extranetSiteId = \CExtranet::getExtranetSiteID()) // for extranet users in public section
234 )
235 {
236 if (isset($params['LOG_ID']) && intval($params['LOG_ID']) > 0)
237 {
238 $postSiteList = array();
239 $res = \CSocNetLog::getSite(intval($params['LOG_ID']));
240 while ($logSite = $res->fetch())
241 {
242 $postSiteList[] = $logSite["LID"];
243 }
244
245 if (!in_array($extranetSiteId, $postSiteList))
246 {
247 $alreadyFound = true;
248 $perms = Permissions::DENY;
249 }
250 }
251 else
252 {
253 $alreadyFound = true;
254 $perms = Permissions::DENY;
255 }
256 }
257 }
258 else
259 {
260 $alreadyFound = true;
261 $perms = Permissions::DENY;
262 }
263 }
264
265 $entityList = array();
266
267 if (!$alreadyFound)
268 {
269 if (!empty($userAccessCodeCache[$userId]))
270 {
271 $entityList = $userAccessCodeCache[$userId];
272 }
273 else
274 {
275 $codeList = \CAccess::getUserCodesArray($userId);
276 foreach($codeList as $code)
277 {
278 if (
279 preg_match('/^DR([0-9]+)/', $code, $match)
280 || preg_match('/^D([0-9]+)/', $code, $match)
281 || preg_match('/^IU([0-9]+)/', $code, $match)
282 )
283 {
284 $entityList["DR"][$code] = $code;
285 }
286 elseif (preg_match('/^SG([0-9]+)_([A-Z])/', $code, $match))
287 {
288 $entityList["SG"][$match[1]][$match[2]] = $match[2];
289 }
290 }
291 $userAccessCodeCache[$userId] = $entityList;
292 }
293
294 foreach($permsList as $key => $value)
295 {
296 foreach($value as $id => $p)
297 {
298 if(!is_array($p))
299 {
300 $p = array();
301 }
302
303 if($userId > 0 && $key == "U" && $userId == $id)
304 {
305 $perms = (
306 in_array("US".$userId, $p) // author
309 );
310 break;
311 }
312
313 if(in_array("G2", $p))
314 {
315 $perms = Permissions::WRITE;
316 break;
317 }
318
319 if($userId > 0 && in_array("AU", $p))
320 {
321 $perms = Permissions::WRITE;
322 break;
323 }
324
325 if($key == "SG")
326 {
327 if(!empty($entityList["SG"][$id]))
328 {
329 foreach($entityList["SG"][$id] as $sonetGroupId)
330 {
331 if(in_array("SG".$id."_".$sonetGroupId, $p))
332 {
333 $perms = Permissions::READ;
334 break;
335 }
336 }
337 }
338 }
339
340 if($key == "DR" && !empty($entityList["DR"]))
341 {
342 if(in_array("DR".$id, $entityList["DR"]))
343 {
344 $perms = Permissions::WRITE;
345 break;
346 }
347 }
348 }
349
350 if($perms > Permissions::DENY)
351 {
352 break;
353 }
354 }
355
356 if (
357 $perms <= Permissions::READ
358 && !empty($permsList['SG'])
359 ) // check open sonet groups
360 {
361 foreach ($permsList['SG'] as $sonetGroupPermList)
362 {
363 if (empty($sonetGroupPermList))
364 {
365 continue;
366 }
367
368 foreach ($sonetGroupPermList as $sonetGroupPerm)
369 {
370 if (!preg_match('/^OSG(\d+)_'.(!$userId ? SONET_ROLES_ALL : SONET_ROLES_AUTHORIZED).'$/', $sonetGroupPerm, $matches))
371 {
372 continue;
373 }
374 $openedWorkgroupsList[] = (int)$matches[1];
375 }
376 }
377
378 if (
379 !empty($openedWorkgroupsList)
380 && Loader::includeModule('socialnetwork')
381 && \Bitrix\Socialnetwork\Helper\Workgroup::checkAnyOpened($openedWorkgroupsList)
382 )
383 {
384 $perms = Permissions::READ;
385 $readByOpenSonetGroup = true;
386 }
387 }
388
389 if (
390 isset($params['CHECK_FULL_PERMS'])
391 && $params['CHECK_FULL_PERMS']
392 && $perms < Permissions::FULL
393 )
394 {
395 $sonetGroupIdList = Array();
396 if(!empty($permsList["SG"]))
397 {
398 foreach($permsList["SG"] as $groupId => $val)
399 {
400 if(!empty($entityList["SG"][$groupId]))
401 {
402 $sonetGroupIdList[] = $groupId;
403 }
404 }
405 }
406
407 if(!empty($sonetGroupIdList))
408 {
409 $operationList = array("full_post", "moderate_post", "write_post", "premoderate_post");
410 foreach($operationList as $operation)
411 {
412 if($perms > Permissions::READ)
413 {
414 break;
415 }
416
417 $sonetGroupPermList = \CSocNetFeaturesPerms::getOperationPerm(SONET_ENTITY_GROUP, $sonetGroupIdList, "blog", $operation);
418 if(is_array($sonetGroupPermList))
419 {
420 foreach($sonetGroupPermList as $groupId => $role)
421 {
422 if (in_array($role, $entityList["SG"][$groupId]))
423 {
424 switch($operation)
425 {
426 case "full_post":
427 $perms = Permissions::FULL;
428 break;
429 case "moderate_post":
430 $perms = Permissions::MODERATE;
431 break;
432 case "write_post":
433 $perms = Permissions::WRITE;
434 break;
435 case "premoderate_post":
437 break;
438 }
439 }
440 }
441 }
442 }
443 }
444 }
445 }
446 }
447
448 $cache[$cacheId] = $result = [
449 'PERM' => $perms,
450 'READ_BY_OSG' => $readByOpenSonetGroup,
451 ];
452
453 return $result;
454 }
455
461 public function detectTags()
462 {
463 static $parser = null;
464
465 $result = array();
466
467 $fields = $this->getFields();
468 $searchFields = array('DETAIL_TEXT');
469 if (
470 !isset($fields['MICRO'])
471 || $fields['MICRO'] != 'Y'
472 )
473 {
474 $searchFields[] = 'TITLE';
475 }
476
477 foreach ($searchFields as $fieldCode)
478 {
479 if (
480 empty($fieldCode)
481 || empty($fields[$fieldCode])
482 )
483 {
484 continue;
485 }
486
487 if ($parser === null)
488 {
489 $parser = new \CTextParser();
490 }
491
492 $result = array_merge($result, $parser->detectTags($fields[$fieldCode]));
493 }
494
495 return array_unique($result);
496 }
497
498 public function getTags()
499 {
500 $result = array();
501
502 $fields = $this->getFields();
503
504 if (
505 empty($fields)
506 || empty($fields['ID'])
507 || intval($fields['ID']) <= 0
508 )
509 {
510 return false;
511 }
512
513 $res = \CBlogPostCategory::getList(
514 array(),
515 array(
516 'POST_ID' => $fields['ID']
517 ),
518 false,
519 false,
520 array('NAME')
521 );
522
523 while ($category = $res->fetch())
524 {
525 $result[] = $category['NAME'];
526 }
527
528 return $result;
529 }
530
535 public function getLogId(array $params = [])
536 {
537 $result = false;
538
539 $postId = $this->getId();
540 if ($postId <= 0)
541 {
542 return $result;
543 }
544
545 if (!Loader::includeModule('socialnetwork'))
546 {
547 return $result;
548 }
549
550 $provider = Livefeed\Provider::init(array(
551 'ENTITY_TYPE' => Livefeed\Provider::DATA_ENTITY_TYPE_BLOG_POST,
552 'ENTITY_ID' => $postId,
553 ));
554 if (!$provider)
555 {
556 return $result;
557 }
558
559 return $provider->getLogId($params);
560 }
561
567 public function deactivateLogEntry()
568 {
569 $result = false;
570
571 if (!Loader::includeModule('socialnetwork'))
572 {
573 return $result;
574 }
575
576 $logId = $this->getLogId();
577 if (intval($logId) <= 0)
578 {
579 return $result;
580 }
581
582 LogTable::update($logId, [
583 'INACTIVE' => 'Y'
584 ]);
585
586 return true;
587 }
588
594 public function activateLogEntry()
595 {
596 $result = false;
597
598 if (!Loader::includeModule('socialnetwork'))
599 {
600 return $result;
601 }
602
603 $logId = $this->getLogId([
604 'inactive' => true
605 ]);
606 if (intval($logId) <= 0)
607 {
608 return $result;
609 }
610
611 $currentDateTime = new \Bitrix\Main\DB\SqlExpression(\Bitrix\Main\Application::getInstance()->getConnection()->getSqlHelper()->getCurrentDateTimeFunction());
612 LogTable::update($logId, [
613 'INACTIVE' => 'N',
614 'LOG_DATE' => $currentDateTime,
615 'LOG_UPDATE' => $currentDateTime
616 ]);
617
618 return true;
619 }
620}
Definition post.php:20
__construct()
Definition post.php:27
detectTags()
Definition post.php:461
activateLogEntry()
Definition post.php:594
getId()
Definition post.php:128
setFields($fields=array())
Definition post.php:113
getLogId(array $params=[])
Definition post.php:535
getSonetPerms($params=array())
Definition post.php:133
deactivateLogEntry()
Definition post.php:567
static getById($postId=0, $params=array())
Definition post.php:35
setId($id=0)
Definition post.php:123
getTags()
Definition post.php:498
$id
Definition post.php:21
getFields()
Definition post.php:118
Definition post.php:33
static loadMessages($file)
Definition loc.php:64
static isModuleInstalled($moduleName)
$GLOBALS['____1444769544']
Definition license.php:1