1
0
Fork 0
mirror of https://github.com/Oreolek/ifhub.club.git synced 2024-07-06 00:14:25 +03:00
ifhub.club/engine/modules/text/Text.class.php

235 lines
7.6 KiB
PHP
Raw Normal View History

<?php
2008-09-21 09:36:57 +03:00
/*-------------------------------------------------------
*
* LiveStreet Engine Social Networking
* Copyright © 2008 Mzhelskiy Maxim
*
*--------------------------------------------------------
*
* Official site: www.livestreet.ru
* Contact e-mail: rus.engine@gmail.com
*
* GNU General Public License, version 2:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*
---------------------------------------------------------
*/
require_once(Config::Get('path.root.engine').'/lib/external/Jevix/jevix.class.php');
2008-09-21 09:36:57 +03:00
/**
* Модуль обработки текста на основе типографа Jevix
*
*/
class ModuleText extends Module {
2008-09-21 09:36:57 +03:00
/**
* Объект типографа
*
* @var Jevix
*/
protected $oJevix;
/**
* Инициализация модуля
*
*/
public function Init() {
/**
* Создаем объект типографа и запускаем его конфигурацию
*/
$this->oJevix = new Jevix();
$this->JevixConfig();
2008-09-21 09:36:57 +03:00
}
/**
* Конфигурирует типограф
*
*/
protected function JevixConfig() {
// загружаем конфиг
$this->LoadJevixConfig();
}
/**
* Загружает конфиг Jevix'а
*
* @param string $sType Тип конфига
* @param bool $bClear
*/
public function LoadJevixConfig($sType='default',$bClear=true) {
if ($bClear) {
$this->oJevix->tagsRules=array();
}
$aConfig=Config::Get('jevix.'.$sType);
if (is_array($aConfig)) {
foreach ($aConfig as $sMethod => $aExec) {
foreach ($aExec as $aParams) {
if (in_array(strtolower($sMethod),array_map("strtolower",array('cfgSetTagCallbackFull','cfgSetTagCallback')))) {
if (isset($aParams[1][0]) and $aParams[1][0]=='_this_') {
$aParams[1][0]=$this;
}
}
call_user_func_array(array($this->oJevix,$sMethod), $aParams);
}
}
/**
* Хардкодим некоторые параметры
*/
unset($this->oJevix->entities1['&']); // разрешаем в параметрах символ &
if (Config::Get('view.noindex') and isset($this->oJevix->tagsRules['a'])) {
$this->oJevix->cfgSetTagParamDefault('a','rel','nofollow',true);
}
}
2008-09-21 09:36:57 +03:00
}
/**
* Возвращает объект Jevix
*
* @return unknown
*/
public function GetJevix() {
return $this->oJevix;
}
2008-09-21 09:36:57 +03:00
/**
* Парсинг текста с помощью Jevix
*
* @param string $sText
* @param array $aError
* @return string
*/
public function JevixParser($sText,&$aError=null) {
// Если конфиг пустой, то загружаем его
if (!count($this->oJevix->tagsRules)) {
$this->LoadJevixConfig();
}
$sResult=$this->oJevix->parse($sText,$aError);
2008-09-21 09:36:57 +03:00
return $sResult;
}
/**
* Парсинг текста на предмет видео
*
* @param string $sText
* @return string
*/
2009-09-13 18:42:16 +03:00
public function VideoParser($sText) {
2008-09-21 09:36:57 +03:00
/**
* youtube.com
*/
2011-02-14 22:16:47 +02:00
$sText = preg_replace('/<video>http:\/\/(?:www\.|)youtube\.com\/watch\?v=([a-zA-Z0-9_\-]+)(&.+)?<\/video>/Ui', '<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/$1&hl=en"></param><param name="wmode" value="opaque"></param><embed src="http://www.youtube.com/v/$1&hl=en" type="application/x-shockwave-flash" wmode="opaque" width="425" height="344"></embed></object>', $sText);
2008-09-21 09:36:57 +03:00
/**
* rutube.ru
*/
2011-02-14 22:16:47 +02:00
$sText = preg_replace('/<video>http:\/\/(?:www\.|)rutube.ru\/tracks\/\d+.html\?v=([a-zA-Z0-9_\-]+)(&.+)?<\/video>/Ui', '<OBJECT width="470" height="353"><PARAM name="movie" value="http://video.rutube.ru/$1"></PARAM><PARAM name="wmode" value="opaque"></PARAM><PARAM name="allowFullScreen" value="true"></PARAM><PARAM name="flashVars" value="uid=662118"></PARAM><EMBED src="http://video.rutube.ru/$1" type="application/x-shockwave-flash" wmode="opaque" width="470" height="353" allowFullScreen="true" flashVars="uid=662118"></EMBED></OBJECT>', $sText);
return $sText;
2008-09-21 09:36:57 +03:00
}
2009-08-18 12:10:14 +03:00
2008-09-21 09:36:57 +03:00
/**
* Парсит текст
*
* @param string $sText
*/
public function Parser($sText) {
$sResult=$this->FlashParamParser($sText);
$sResult=$this->JevixParser($sResult);
2009-08-18 12:10:14 +03:00
$sResult=$this->VideoParser($sResult);
$sResult=$this->CodeSourceParser($sResult);
2008-09-21 09:36:57 +03:00
return $sResult;
}
/**
* Заменяет все вхождения короткого тега <param/> на длиную версию <param></param>
2009-02-22 22:45:21 +02:00
* Заменяет все вхождения короткого тега <embed/> на длиную версию <embed></embed>
*
*/
protected function FlashParamParser($sText) {
2010-07-07 09:06:37 +03:00
if (preg_match_all("@(<\s*param\s*name\s*=\s*(?:\"|').*(?:\"|')\s*value\s*=\s*(?:\"|').*(?:\"|'))\s*/?\s*>(?!</param>)@Ui",$sText,$aMatch)) {
foreach ($aMatch[1] as $key => $str) {
$str_new=$str.'></param>';
$sText=str_replace($aMatch[0][$key],$str_new,$sText);
}
2009-02-22 22:45:21 +02:00
}
if (preg_match_all("@(<\s*embed\s*.*)\s*/?\s*>(?!</embed>)@Ui",$sText,$aMatch)) {
2009-02-22 22:45:21 +02:00
foreach ($aMatch[1] as $key => $str) {
$str_new=$str.'></embed>';
$sText=str_replace($aMatch[0][$key],$str_new,$sText);
}
}
/**
* Удаляем все <param name="wmode" value="*"></param>
*/
2010-07-07 09:06:37 +03:00
if (preg_match_all("@(<param\s.*name=(?:\"|')wmode(?:\"|').*>\s*</param>)@Ui",$sText,$aMatch)) {
foreach ($aMatch[1] as $key => $str) {
$sText=str_replace($aMatch[0][$key],'',$sText);
}
}
/**
* А теперь после <object> добавляем <param name="wmode" value="opaque"></param>
* Решение не фантан, но главное работает :)
*/
if (preg_match_all("@(<object\s.*>)@Ui",$sText,$aMatch)) {
foreach ($aMatch[1] as $key => $str) {
$sText=str_replace($aMatch[0][$key],$aMatch[0][$key].'<param name="wmode" value="opaque"></param>',$sText);
}
}
return $sText;
}
public function CodeSourceParser($sText) {
$sText=str_replace("<code>",'<pre class="prettyprint"><code>',$sText);
$sText=str_replace("</code>",'</code></pre>',$sText);
return $sText;
}
/**
* Производить резрезание текста по тегу <cut>.
* Возвращаем массив вида:
* array(
* $sTextShort - текст до тега <cut>
* $sTextNew - весь текст за исключением удаленного тега
* $sTextCut - именованное значение <cut>
* )
*
* @param string $sText
* @return array
*/
public function Cut($sText) {
$sTextShort = $sText;
$sTextNew = $sText;
$sTextCut = null;
2010-05-18 16:36:32 +03:00
$sTextTemp=str_replace("\r\n",'[<rn>]',$sText);
$sTextTemp=str_replace("\n",'[<n>]',$sTextTemp);
if (preg_match("/^(.*)<cut(.*)>(.*)$/Ui",$sTextTemp,$aMatch)) {
$aMatch[1]=str_replace('[<rn>]',"\r\n",$aMatch[1]);
$aMatch[1]=str_replace('[<n>]',"\r\n",$aMatch[1]);
$aMatch[3]=str_replace('[<rn>]',"\r\n",$aMatch[3]);
$aMatch[3]=str_replace('[<n>]',"\r\n",$aMatch[3]);
2010-05-18 16:36:32 +03:00
$sTextShort=$aMatch[1];
2010-07-10 10:21:51 +03:00
$sTextNew=$aMatch[1].' <a name="cut"></a> '.$aMatch[3];
if (preg_match('/^\s*name\s*=\s*"(.+)"\s*\/?$/Ui',$aMatch[2],$aMatchCut)) {
$sTextCut=trim($aMatchCut[1]);
}
}
2011-04-06 12:27:50 +03:00
return array($sTextShort,$sTextNew,$sTextCut ? htmlspecialchars($sTextCut) : null);
}
/**
* Обработка тега <ls> в тексте
*
* @param unknown_type $sTag
* @param unknown_type $aParams
* @return unknown
*/
public function CallbackTagLs($sTag,$aParams) {
$sText='';
if (isset($aParams['user'])) {
if ($oUser=$this->User_getUserByLogin($aParams['user'])) {
$sText.="<a href=\"{$oUser->getUserWebPath()}\" class=\"ls-user\">{$oUser->getLogin()}</a> ";
}
}
return $sText;
}
2008-09-21 09:36:57 +03:00
}
?>