Server IP : 80.87.202.40 / Your IP : 216.73.216.169 Web Server : Apache System : Linux rospirotorg.ru 5.14.0-539.el9.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Dec 5 22:26:13 UTC 2024 x86_64 User : bitrix ( 600) PHP Version : 8.2.27 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /home/bitrix/ext_www/rospirotorg.ru/bitrix/modules/burlakastudio.realcommenter/lib/ |
Upload File : |
<?php /** * ����� "��������� ����������� 2.0" ��� ������� * �������� ���� �����: www.realcommenter.com * �������� ���� �����������: burlaka.studio * ����� � ����������: ������� ������� (AlexeyGfi) -> alexeygfi@gmail.com */ namespace Burlakastudio\Realcommenter; use Bitrix\Main\Text\BinaryString; class PARSER { /** * �� ���� � �����, ������ ��� ������� � ��. * ����� ��� �������� �� ���������: * � ���� ����� �����-�����-��������: * - �����-����� � ������ �����-������� * � ����� �������� �����: * - ����� * * @param string $text * * @return string */ public static function processMsgForPublish(&$text = '') { if (!$text) { return $text; } $pinMemory = []; $pinMemCnt = 0; /* * ������� ���� (������ ���� � � ����) �� |=|TG:XXX|=| */ $text = preg_replace_callback( '~</?([a-z]+)[^>]*>~i' . BX_UTF_PCRE_MODIFIER, function ($matches) use (&$pinMemory, &$pinMemCnt) { $pin = '|=|TG:' . $pinMemCnt . '|=|'; $pinMemory[$pin] = $matches[0]; $pinMemCnt++; return $pin; }, $text ); /** * ��������� �����-����� */ $allow_detecting_video = !!OPTIONS::get('allow_detecting_video'); if ($allow_detecting_video) { $video_frame_width = OPTIONS::get('video_frame_width'); $video_frame_width = (intval($video_frame_width)) ? intval($video_frame_width) : '560'; $video_frame_height = OPTIONS::get('video_frame_height'); $video_frame_height = (intval($video_frame_height)) ? intval($video_frame_height) : '315'; $text = preg_replace_callback( '~https?:\/\/(youtu\.be|vimeo\.com|www\.youtube\.com|youtube\.com)\/(watch\?v=)?([^\s.,!\&"\']+)([^><\s.,!?"\'|]*)~i' . BX_UTF_PCRE_MODIFIER, function ($matches) use (&$pinMemory, &$pinMemCnt, &$video_frame_width, &$video_frame_height) { $video_frame = ''; if (strpos($matches[1], 'youtu') !== false) { $video_frame = str_replace( [ '#WIDTH#', '#HEIGHT#', '#CODE#', ], [ $video_frame_width, $video_frame_height, $matches[3] ], HTML_TEMPLATER::get('video_frame_youtube') ); } else if (strpos($matches[1], 'vimeo') !== false) { $video_frame = str_replace( [ '#WIDTH#', '#HEIGHT#', '#CODE#', ], [ $video_frame_width, $video_frame_height, $matches[3] ], HTML_TEMPLATER::get('video_frame_vimeo') ); } $pin = '|=|VF:' . $pinMemCnt . '|=|'; $pinMemory[$pin] = $video_frame; $pinMemCnt++; return $pin; }, $text ); } /** * �������� �����? * * � ��� ����� ���, ������ �������� ������ ��� div-� � �����-����� */ $use_link_detector = !!OPTIONS::get('use_link_detector'); if ($use_link_detector) { $text = preg_replace_callback( '~https?:\/\/(www.)?([^\s<>,!"\'|]+)~i' . BX_UTF_PCRE_MODIFIER, function ($matches) use (&$pinMemory, &$pinMemCnt) { return sprintf('<a href="%1$s">%1$s</a>', $matches[0]); }, $text ); } /** * ������: * - * � */ $text = str_replace(' - ', ' — ', $text); /** * ��������� �� �� ����� */ if (!empty($pinMemory)) { $text = str_replace( array_keys($pinMemory), array_values($pinMemory), $text ); } /** * �� ������� ���-�� ������? */ $close_link_by_noindex = !!OPTIONS::get('close_link_by_noindex'); $close_rel_by_nofollow = !!OPTIONS::get('close_rel_by_nofollow'); /** * ������� ��������� ����� �� �����, ������ ����� �� ��������� ����� */ if ($close_link_by_noindex || $close_rel_by_nofollow) { $text = preg_replace_callback('~(<)(a)([^>]+)(>)(?!<\/a>)(.*?)(<\/\2>)~i' . BX_UTF_PCRE_MODIFIER, function ($matches) use (&$close_link_by_noindex, &$close_rel_by_nofollow) { if (self::href_to_current_domain($matches[3])) { return $matches[0]; } else { if ($close_rel_by_nofollow) { $matches[2] .= ' rel="nofollow" '; } array_shift($matches); if ($close_link_by_noindex) { return '<noindex>' . implode('', $matches) . '</noindex>'; } else { return implode('', $matches); } } }, $text ); } } /** * ��������� ��� ���� �� ������ �����? * * @param string $a_body * * @return false|int */ private static function href_to_current_domain(&$a_body = '') { $same_domain = false; if (preg_match('~href\s*=\s*[\'"](https?:)?(\/\/)?([^\'"\/]+)[\'"\/]~', $a_body, $matches)) { //if( $matches[ 3 ] && strpos( $matches[ 3 ], $_SERVER[ 'HTTP_HOST' ] ) === 0 ) { if ($matches[3] && $matches[3] == $_SERVER['HTTP_HOST']) { $same_domain = true; } } return $same_domain; // return preg_replace_callback( '~href\s*=\s*[\'"]([^\'"]+)[\'"]~', // function( $matches ) use ( $same_domain ) { // if( strpos( $matches[ 1 ], $_SERVER[ 'HTTP_HOST' ] ) === 0 ) { // $same_domain= true; // } // // return $matches[ 0 ]; // }, $a_body ); } /** * @param string $text */ public static function simplify(&$text = '') { $text = preg_replace('~<[^>]+?>~is' . BX_UTF_PCRE_MODIFIER, ' ', $text); $text = str_replace(["\n", "\r", "\t"], ' ', $text); $text = preg_replace('~\s\s+~', ' ', $text); } public static function trimmer(&$text = '', $length = 100, $tail = '...') { if (BinaryString::getLength($text) > $length) { $precisionParts = explode(' ', $text); $substr = BinaryString::getSubstring($text, 0, $length); $parts = explode(' ', $substr); $theLastPiece = array_pop($parts); // We cut exactly in space (have not broken word) if ($theLastPiece === $precisionParts[count($parts)]) { $parts[] = $theLastPiece; } // if somethig if (count($parts) > 0) { $text = implode(' ', $parts); } if ($tail) { $text.= $tail; } } } }