Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
mailhandler.php
1<?php
2
4
11
12Loc::loadMessages(__FILE__);
13
20final class MailHandler
21{
28 public static function handleReplyReceivedBlogPost(\Bitrix\Main\Event $event)
29 {
30 $siteId = intval($event->getParameter('site_id'));
31 $postId = intval($event->getParameter('entity_id'));
32 $userId = intval($event->getParameter('from'));
33 $message = trim($event->getParameter('content'));
34 $attachments = $event->getParameter('attachments');
35
36 if (
37 $message == ''
38 && count($attachments) > 0
39 )
40 {
41 $message = Loc::getMessage('BLOG_MAILHANDLER_ATTACHMENTS');
42 }
43
44 if (
45 $postId <= 0
46 || $userId <= 0
47 || $message == ''
48 )
49 {
50 return false;
51 }
52
53 $res = \CBlogPost::getList(
54 array(),
55 array(
56 "ID" => $postId
57 ),
58 false,
59 false,
60 array("BLOG_ID", "AUTHOR_ID", "BLOG_OWNER_ID")
61 );
62
63 if (!($blogPost = $res->fetch()))
64 {
65 return false;
66 }
67
68 $perm = BLOG_PERMS_DENY;
69
70 if ($blogPost["AUTHOR_ID"] == $userId)
71 {
72 $perm = BLOG_PERMS_FULL;
73 }
74 else
75 {
76 $postPerm = \CBlogPost::getSocNetPostPerms($postId, false, $userId, $blogPost["AUTHOR_ID"]);
77 if ($postPerm > Permissions::DENY)
78 {
79 $perm = \CBlogComment::getSocNetUserPerms($postId, $blogPost["AUTHOR_ID"], $userId);
80 }
81 }
82
83 if ($perm == Permissions::DENY)
84 {
85 return false;
86 }
87
88 if (!\Bitrix\Blog\Item\Comment::checkDuplicate(array(
89 'MESSAGE' => $message,
90 'BLOG_ID' => $blogPost["BLOG_ID"],
91 'POST_ID' => $postId,
92 'AUTHOR_ID' => $userId,
93 )))
94 {
95 return false;
96 }
97
98 $fields = Array(
99 "POST_ID" => $postId,
100 "BLOG_ID" => $blogPost["BLOG_ID"],
101 "TITLE" => '',
102 "POST_TEXT" => $message,
103 "AUTHOR_ID" => $userId,
104 "DATE_CREATE" => convertTimeStamp(time() + \CTimeZone::getOffset(), "FULL")
105 );
106
107 if (!empty($siteId))
108 {
109 $fields["SEARCH_GROUP_ID"] = \Bitrix\Main\Config\Option::get("socialnetwork", "userbloggroup_id", false, $siteId);
110 }
111
112 if ($perm == Permissions::PREMODERATE)
113 {
114 $fields["PUBLISH_STATUS"] = BLOG_PUBLISH_STATUS_READY;
115 }
116
117 $ufCode = (
118 isModuleInstalled("webdav")
119 || isModuleInstalled("disk")
120 ? "UF_BLOG_COMMENT_FILE"
121 : "UF_BLOG_COMMENT_DOC"
122 );
123 $fields[$ufCode] = array();
124
125 $type = false;
126 $attachmentRelations = array();
127
128 foreach ($attachments as $key => $attachedFile)
129 {
130 $resultId = \CSocNetLogComponent::saveFileToUF($attachedFile, $type, $userId);
131 if ($resultId)
132 {
133 $fields[$ufCode][] = $resultId;
134 $attachmentRelations[$key] = $resultId;
135 }
136 }
137
138 $fields["POST_TEXT"] = preg_replace_callback(
139 "/\[ATTACHMENT\s*=\s*([^\]]*)\]/is".BX_UTF_PCRE_MODIFIER,
140 function ($matches) use ($attachmentRelations)
141 {
142 if (isset($attachmentRelations[$matches[1]]))
143 {
144 return "[DISK FILE ID=".$attachmentRelations[$matches[1]]."]";
145 }
146 },
147 $fields["POST_TEXT"]
148 );
149
150 if (Loader::includeModule('disk'))
151 {
152 \Bitrix\Disk\Uf\FileUserType::setValueForAllowEdit("BLOG_COMMENT", true);
153 }
154
155 $commentId = \CBlogComment::add($fields);
156
157 if ($commentId)
158 {
159 \Bitrix\Blog\Item\Comment::actionsAfter(array(
160 'MESSAGE' => $message,
161 'BLOG_ID' => $blogPost["BLOG_ID"],
162 'BLOG_OWNER_ID' => $blogPost["BLOG_OWNER_ID"],
163 'POST_ID' => $postId,
164 'POST_TITLE' => htmlspecialcharsBack($blogPost["TITLE"]),
165 'POST_AUTHOR_ID' => $blogPost["AUTHOR_ID"],
166 'COMMENT_ID' => $commentId,
167 'AUTHOR_ID' => $userId,
168 ));
169 }
170
171 return $commentId;
172 }
179 public static function handleForwardReceivedBlogPost(\Bitrix\Main\Event $event)
180 {
181 $userId = intval($event->getParameter('from'));
182 $message = trim($event->getParameter('content'));
183 $subject = trim($event->getParameter('subject'));
184 $attachments = $event->getParameter('attachments');
185 $siteId = $event->getParameter('site_id');
186
187 if (
188 $message == ''
189 && count($attachments) > 0
190 )
191 {
192 $message = Loc::getMessage('BLOG_MAILHANDLER_ATTACHMENTS');
193 }
194
195 if (
196 $userId <= 0
197 || $message == ''
198 || $siteId == ''
199 )
200 {
201 return false;
202 }
203
204 if (!Loader::includeModule('socialnetwork'))
205 {
206 throw new Main\SystemException("Could not load 'socialnetwork' module.");
207 }
208
209 $pathToPost = Config\Option::get("socialnetwork", "userblogpost_page", '', $siteId);
210 $postId = false;
211
212 $blog = Blog::getByUser(array(
213 "GROUP_ID" => Config\Option::get("socialnetwork", "userbloggroup_id", false, $siteId),
214 "SITE_ID" => $siteId,
215 "USER_ID" => $userId,
216 "CREATE" => "Y",
217 ));
218
219 if (!$blog)
220 {
221 return $postId;
222 }
223
224 $connection = \Bitrix\Main\Application::getConnection();
225 $helper = $connection->getSqlHelper();
226
227 $fields = Array(
228 "BLOG_ID" => $blog["ID"],
229 "AUTHOR_ID" => $userId,
230 "=DATE_CREATE" => $helper->getCurrentDateTimeFunction(),
231 "=DATE_PUBLISH" => $helper->getCurrentDateTimeFunction(),
232 "MICRO" => "N",
233 "TITLE" => $subject,
234 "DETAIL_TEXT" => $message,
235 "DETAIL_TEXT_TYPE" => "text",
236 "PUBLISH_STATUS" => BLOG_PUBLISH_STATUS_PUBLISH,
237 "HAS_IMAGES" => "N",
238 "HAS_TAGS" => "N",
239 "HAS_SOCNET_ALL" => "N",
240 "SOCNET_RIGHTS" => array("U".$userId)
241 );
242
243 if ($fields["TITLE"] == '')
244 {
245 $fields["MICRO"] = "Y";
246 $fields["TITLE"] = preg_replace("/\[ATTACHMENT\s*=\s*[^\]]*\]/is".BX_UTF_PCRE_MODIFIER, "", \blogTextParser::killAllTags($fields["DETAIL_TEXT"]));
247 $fields["TITLE"] = TruncateText(trim(preg_replace(array("/\n+/is".BX_UTF_PCRE_MODIFIER, "/\s+/is".BX_UTF_PCRE_MODIFIER), " ", $fields["TITLE"])), 100);
248 if($fields["TITLE"] == '')
249 {
250 $fields["TITLE"] = Loc::getMessage("BLOG_MAILHANDLER_EMPTY_TITLE_PLACEHOLDER");
251 }
252 }
253
254 $ufCode = (
255 isModuleInstalled("webdav")
256 || isModuleInstalled("disk")
257 ? "UF_BLOG_POST_FILE"
258 : "UF_BLOG_POST_DOC"
259 );
260 $fields[$ufCode] = array();
261
262 $type = false;
263 $attachmentRelations = array();
264
265 foreach ($attachments as $key => $attachedFile)
266 {
267 $resultId = \CSocNetLogComponent::saveFileToUF($attachedFile, $type, $userId);
268 if ($resultId)
269 {
270 $fields[$ufCode][] = $resultId;
271 $attachmentRelations[$key] = $resultId;
272 }
273 }
274
275 $fields["HAS_PROPS"] = (!empty($attachmentRelations) ? "Y" :"N");
276
277 $fields["DETAIL_TEXT"] = preg_replace_callback(
278 "/\[ATTACHMENT\s*=\s*([^\]]*)\]/is".BX_UTF_PCRE_MODIFIER,
279 function ($matches) use ($attachmentRelations)
280 {
281 return (
282 isset($attachmentRelations[$matches[1]])
283 ? "[DISK FILE ID=".$attachmentRelations[$matches[1]]."]"
284 : ""
285 );
286 },
287 $fields["DETAIL_TEXT"]
288 );
289
290 if (Loader::includeModule('disk'))
291 {
292 \Bitrix\Disk\Uf\FileUserType::setValueForAllowEdit("BLOG_POST", true);
293 }
294
295 $postId = \CBlogPost::add($fields);
296
297 if (!$postId)
298 {
299 return $postId;
300 }
301
302 BXClearCache(true, "/".$siteId."/blog/last_messages_list/");
303
304 $fields["ID"] = $postId;
305 $paramsNotify = array(
306 "bSoNet" => true,
307 "UserID" => $userId,
308 "allowVideo" => "N",
309 "PATH_TO_SMILE" => Config\Option::get("socialnetwork", "smile_page", '', $siteId),
310 "PATH_TO_POST" => $pathToPost,
311 "user_id" => $userId,
312 "NAME_TEMPLATE" => \CSite::getNameFormat(null, $siteId),
313 "SHOW_LOGIN" => "Y",
314 "SEND_COUNTER_TO_AUTHOR" => "Y"
315 );
316 \CBlogPost::notify($fields, $blog, $paramsNotify);
317
318 if (Loader::includeModule('im'))
319 {
320 $postUrl = \CComponentEngine::makePathFromTemplate($pathToPost, array(
321 "post_id" => $postId,
322 "user_id" => $userId
323 ));
324
325 $processedPathData = \CSocNetLogTools::processPath(array("POST_URL" => $postUrl), $userId, $siteId);
326 $serverName = $processedPathData["SERVER_NAME"];
327 $postUrl = $processedPathData["URLS"]["POST_URL"];
328
329 \CIMNotify::add(array(
330 "MESSAGE_TYPE" => IM_MESSAGE_SYSTEM,
331 "NOTIFY_TYPE" => IM_NOTIFY_SYSTEM,
332 "NOTIFY_MODULE" => "blog",
333 "NOTIFY_EVENT" => "post_mail",
334 "NOTIFY_TAG" => "BLOG|POST|".$postId,
335 "TO_USER_ID" => $userId,
336 "NOTIFY_MESSAGE" => Loc::getMessage("BLOG_MAILHANDLER_NEW_POST", array(
337 "#TITLE#" => "<a href=\"".$postUrl."\">".$fields["TITLE"]."</a>"
338 )),
339 "NOTIFY_MESSAGE_OUT" => Loc::getMessage("BLOG_MAILHANDLER_NEW_POST", array(
340 "#TITLE#" => $fields["TITLE"]
341 )).' '.$serverName.$postUrl
342 ));
343 }
344
345 $categoryIdList = \Bitrix\Socialnetwork\Component\BlogPostEdit\Tag::parseTagsFromFields([
346 'postFields' => $fields,
347 'blogId' => $blog['ID'],
348 ]);
349 if (!empty($categoryIdList))
350 {
351 foreach ($categoryIdList as $categoryId)
352 {
353 \CBlogPostCategory::add([
354 'BLOG_ID' => $fields['BLOG_ID'],
355 'POST_ID' => $postId,
356 'CATEGORY_ID' => $categoryId
357 ]);
358 }
359
360 \CBlogPost::update(
361 $postId,
362 [
363 'CATEGORY_ID' => implode(',', $categoryIdList),
364 'HAS_TAGS' => 'Y',
365 ]
366 );
367 }
368
369 return $postId;
370 }
371}
static handleReplyReceivedBlogPost(\Bitrix\Main\Event $event)
static handleForwardReceivedBlogPost(\Bitrix\Main\Event $event)
static getByUser(array $params)
Definition blog.php:18
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29