PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/socialnetwork/lib/util.php

https://gitlab.com/alexprowars/bitrix
PHP | 286 lines | 247 code | 39 blank | 0 comment | 44 complexity | 1bc21f2023f5dc48f32483899de17730 MD5 | raw file
  1. <?php
  2. namespace Bitrix\Socialnetwork;
  3. use Bitrix\Main\Loader;
  4. use Bitrix\Main\ModuleManager;
  5. use Bitrix\Main;
  6. use Bitrix\Main\Localization\Loc;
  7. class Util
  8. {
  9. public static function getPermissionsByExternalAuthId($authId)
  10. {
  11. $result = array(
  12. 'message' => true
  13. );
  14. if ($authId == 'email')
  15. {
  16. $result['message'] = (!ModuleManager::isModuleInstalled('mail'));
  17. }
  18. return $result;
  19. }
  20. public static function getSiteIdByLogId($logId)
  21. {
  22. $extranetSiteId = false;
  23. if (Loader::includeModule('extranet'))
  24. {
  25. $extranetSiteId = \CExtranet::getExtranetSiteId();
  26. }
  27. $logSiteId = array();
  28. $res = \CSocNetLog::getSite($logId);
  29. while ($logSite = $res->fetch())
  30. {
  31. $logSiteId[] = $logSite["LID"];
  32. }
  33. return (
  34. $extranetSiteId
  35. && count($logSiteId) == 1
  36. && $logSiteId[0] == $extranetSiteId
  37. ? $extranetSiteId
  38. : $logSiteId[0]
  39. );
  40. }
  41. public static function notifyMail($fields)
  42. {
  43. if (!Loader::includeModule('mail'))
  44. {
  45. return false;
  46. }
  47. if (
  48. !isset($fields["logEntryId"])
  49. || intval($fields["logEntryId"]) <= 0
  50. || !isset($fields["userId"])
  51. || !isset($fields["logEntryUrl"])
  52. || $fields["logEntryUrl"] == ''
  53. )
  54. {
  55. return false;
  56. }
  57. if (!is_array($fields["userId"]))
  58. {
  59. $fields["userId"] = array($fields["userId"]);
  60. }
  61. if (!isset($fields["siteId"]))
  62. {
  63. $fields["siteId"] = SITE_ID;
  64. }
  65. $nameTemplate = \CSite::getNameFormat("", $fields["siteId"]);
  66. $authorName = "";
  67. if (!empty($fields["authorId"]))
  68. {
  69. $res = \CUser::getById($fields["authorId"]);
  70. if ($author = $res->fetch())
  71. {
  72. $authorName = \CUser::formatName(
  73. $nameTemplate,
  74. $author,
  75. true,
  76. false
  77. );
  78. }
  79. else
  80. {
  81. $authorName = '';
  82. }
  83. if (check_email($authorName))
  84. {
  85. $authorName = '"'.$authorName.'"';
  86. }
  87. foreach($fields["userId"] as $key => $val)
  88. {
  89. if (intval($val) == intval($fields["authorId"]))
  90. {
  91. unset($fields["userId"][$key]);
  92. }
  93. }
  94. }
  95. if (empty($fields["userId"]))
  96. {
  97. return false;
  98. }
  99. if (
  100. !isset($fields["type"])
  101. || !in_array(mb_strtoupper($fields["type"]), array("LOG_ENTRY", "LOG_COMMENT"))
  102. )
  103. {
  104. $fields["type"] = "LOG_COMMENT";
  105. }
  106. $arEmail = \Bitrix\Mail\User::getUserData($fields["userId"], $nameTemplate);
  107. if (empty($arEmail))
  108. {
  109. return false;
  110. }
  111. $arLogEntry = \CSocNetLog::getByID(intval($fields["logEntryId"]));
  112. if (!$arLogEntry)
  113. {
  114. return false;
  115. }
  116. $logEntryTitle = str_replace(array("\r\n", "\n"), " ", ($arLogEntry["TITLE"] != '__EMPTY__' ? $arLogEntry["TITLE"] : $arLogEntry["MESSAGE"]));
  117. $logEntryTitle = truncateText($logEntryTitle, 100);
  118. switch(mb_strtoupper($fields["type"]))
  119. {
  120. case "LOG_COMMENT":
  121. $mailMessageId = "<LOG_COMMENT_".$fields["logCommentId"]."@".$GLOBALS["SERVER_NAME"].">";
  122. $mailTemplateType = "SONET_LOG_NEW_COMMENT";
  123. break;
  124. default:
  125. $mailMessageId = "<LOG_ENTRY_".$fields["logEntryId"]."@".$GLOBALS["SERVER_NAME"].">";
  126. $mailTemplateType = "SONET_LOG_NEW_ENTRY";
  127. }
  128. $mailMessageInReplyTo = "<LOG_ENTRY_".$fields["logEntryId"]."@".$GLOBALS["SERVER_NAME"].">";
  129. $defaultEmailFrom = \Bitrix\Mail\User::getDefaultEmailFrom();
  130. foreach ($arEmail as $userId => $user)
  131. {
  132. $email = $user["EMAIL"];
  133. $nameFormatted = str_replace(array('<', '>', '"'), '', $user["NAME_FORMATTED"]);
  134. if (
  135. intval($userId) <= 0
  136. && $email == ''
  137. )
  138. {
  139. continue;
  140. }
  141. $res = \Bitrix\Mail\User::getReplyTo(
  142. $fields["siteId"],
  143. $userId,
  144. 'LOG_ENTRY',
  145. $fields["logEntryId"],
  146. $fields["logEntryUrl"]
  147. );
  148. if (is_array($res))
  149. {
  150. list($replyTo, $backUrl) = $res;
  151. if (
  152. $replyTo
  153. && $backUrl
  154. )
  155. {
  156. $authorName = str_replace(array('<', '>', '"'), '', $authorName);
  157. \CEvent::send(
  158. $mailTemplateType,
  159. $fields["siteId"],
  160. array(
  161. "=Reply-To" => $authorName.' <'.$replyTo.'>',
  162. "=Message-Id" => $mailMessageId,
  163. "=In-Reply-To" => $mailMessageInReplyTo,
  164. "EMAIL_FROM" => $authorName.' <'.$defaultEmailFrom.'>',
  165. "EMAIL_TO" => (!empty($nameFormatted) ? ''.$nameFormatted.' <'.$email.'>' : $email),
  166. "RECIPIENT_ID" => $userId,
  167. "COMMENT_ID" => (isset($fields["logCommentId"]) ? intval($fields["logCommentId"]) : false),
  168. "LOG_ENTRY_ID" => intval($fields["logEntryId"]),
  169. "LOG_ENTRY_TITLE" => $logEntryTitle,
  170. "URL" => $fields["logEntryUrl"]
  171. )
  172. );
  173. }
  174. }
  175. }
  176. return true;
  177. }
  178. public static function getEqualityFields(&$fields)
  179. {
  180. $fields1 = array();
  181. foreach ($fields as $key => $value)
  182. {
  183. if (mb_substr($key, 0, 1) == "=")
  184. {
  185. $fields1[mb_substr($key, 1)] = $value;
  186. unset($fields[$key]);
  187. }
  188. }
  189. return $fields1;
  190. }
  191. public static function processEqualityFieldsToInsert($fields1, &$insert)
  192. {
  193. foreach ($fields1 as $key => $value)
  194. {
  195. if ($insert[0] <> '')
  196. {
  197. $insert[0] .= ", ";
  198. }
  199. $insert[0] .= $key;
  200. if ($insert[1] <> '')
  201. {
  202. $insert[1] .= ", ";
  203. }
  204. $insert[1] .= $value;
  205. }
  206. }
  207. public static function processEqualityFieldsToUpdate($fields1, &$update)
  208. {
  209. foreach ($fields1 as $key => $value)
  210. {
  211. if ($update <> '')
  212. {
  213. $update .= ", ";
  214. }
  215. $update .= $key."=".$value." ";
  216. }
  217. }
  218. public static function detectTags($fieldList, $codeList = array())
  219. {
  220. static $parser = null;
  221. $result = array();
  222. if (
  223. !is_array($fieldList)
  224. || !is_array($codeList)
  225. )
  226. {
  227. return false;
  228. }
  229. foreach($codeList as $code)
  230. {
  231. if (
  232. empty($code)
  233. || empty($fieldList[$code])
  234. )
  235. {
  236. continue;
  237. }
  238. if ($parser === null)
  239. {
  240. $parser = new \CTextParser();
  241. }
  242. $result = array_merge($result, $parser->detectTags($fieldList[$code]));
  243. }
  244. return array_unique($result);
  245. }
  246. }
  247. ?>