1
0
Fork 0
mirror of https://github.com/Oreolek/ifhub.club.git synced 2024-07-08 09:24:25 +03:00
ifhub.club/engine/classes/Plugin.class.php

255 lines
7.4 KiB
PHP
Raw Normal View History

<?php
/*-------------------------------------------------------
*
* 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
*
---------------------------------------------------------
*/
/**
* Абстракция плагина, от которой наследуются все плагины
*
*/
abstract class Plugin extends Object {
/**
* Путь к шаблонам с учетом наличия соответствующего skin`a
*
* @var array
*/
static protected $aTemplatePath=array();
/**
* Web-адрес директорий шаблонов с учетом наличия соответствующего skin`a
*
* @var array
*/
static protected $aTemplateWebPath=array();
/**
* Массив делегатов плагина
*
* @var array
*/
protected $aDelegates=array();
public function __construct() {
}
/**
* Функция инициализации плагина
*
*/
public function Init() {
}
/**
* Передает информацию о делегатах на Plugin-модуль
* Вызывается Engine перед инициализацией плагина
*/
final function Delegate() {
/**
* Получаем название плагина
*/
preg_match('/^Plugin([\w]+)$/i',get_class($this),$aMatches);
$sPluginName=strtolower($aMatches[1]);
/**
* Получаем список объектов, подлежащих делегации
*/
$aObjects=$this->Plugin_GetDelegateObjectList();
/**
* Считываем данные из XML файла описания
*/
$sPluginXML = Config::Get('path.root.server').'/plugins/'.$sPluginName.'/'.LsPlugin::PLUGIN_XML_FILE;
if($oXml = @simplexml_load_file($sPluginXML)) {
foreach($aObjects as $sObjectName) {
if(is_array($data=$oXml->xpath("delegate/{$sObjectName}/item"))) {
foreach($data as $aDelegate) {
/**
* Если не указан параметр FROM, копируем значение всего ITEM
*/
2010-04-25 15:07:11 +03:00
if(!$aDelegate->from) $aDelegate->from=$aDelegate;
$this->DelegateWrapper($sObjectName,$aDelegate->from,$aDelegate->to);
}
}
}
}
if(is_array($this->aDelegates) and count($this->aDelegates)) {
foreach ($this->aDelegates as $sObjectName=>$aParams) {
if(is_array($aParams) and count($aParams)) {
foreach ($aParams as $sFrom=>$sTo) {
2010-04-25 15:07:11 +03:00
if (is_int($sFrom)) {
$sFrom=$sTo;
$sTo=null;
}
$this->DelegateWrapper($sObjectName,$sFrom,$sTo);
}
}
}
}
}
2010-04-25 15:07:11 +03:00
public function DelegateWrapper($sObjectName,$sFrom,$sTo=null) {
/**
* Если не указан делегат TO, считаем, что делегатом является
* одноименный объект текущего плагина
*/
if ($sObjectName=='template') {
if(!$sTo) {
$sTo = $this->GetTemplatePath(get_class($this)).$sFrom;
} else {
$sTo=preg_replace("/^_/",$this->GetTemplatePath(get_class($this)),$sTo);
}
} else {
if(!$sTo) $sTo = get_class($this).'_'.$sFrom;
}
$this->Plugin_Delegate($sObjectName,$sFrom,$sTo,get_class($this));
}
/**
* Возвращает массив делегатов
*
* @return array
*/
final function GetDelegates() {
return $this->aDelegates;
}
/**
* Функция активации плагина
*
*/
public function Activate() {
return true;
}
/**
* Функция деактивации плагина
*
*/
public function Deactivate() {
return true;
}
/**
* Транслирует на базу данных запросы из указанного файла
*
* @param string $sFilePath
* @return array
*/
protected function ExportSQL($sFilePath) {
$sFileQuery = @file_get_contents($sFilePath);
/**
* Замена префикса таблиц
*/
$sFileQuery = str_replace('prefix_', Config::Get('db.table.prefix'), $sFileQuery);
/**
* Массивы запросов и пустой контейнер для сбора ошибок
*/
$aErrors = array();
$aQuery=explode(';',$sFileQuery);
/**
* Выполняем запросы по очереди
*/
foreach($aQuery as $sQuery){
$sQuery = trim($sQuery);
/**
* Заменяем движек, если таковой указан в запросе
*/
if(Config::Get('db.tables.engine')!='InnoDB') $sQuery=str_ireplace('ENGINE=InnoDB', "ENGINE=".Config::Get('db.tables.engine'),$sQuery);
if($sQuery!='') {
$bResult=$this->Database_GetConnect()->query($sQuery);
2010-04-17 21:35:10 +03:00
if($bResult===false) $aErrors[] = mysql_error();
}
}
/**
* Возвращаем результат выполнения, взависимости от количества ошибок
*/
if(count($aErrors)==0) {
return array('result'=>true,'errors'=>null);
}
return array('result'=>false,'errors'=>$aErrors);
}
public function __call($sName,$aArgs) {
return Engine::getInstance()->_CallModule($sName,$aArgs);
}
/**
* Возвращает правильный путь к директории шаблонов
*
* @return string
*/
static public function GetTemplatePath($sName) {
$sName = preg_match('/^Plugin([\w]+)$/i',$sName,$aMatches)
? strtolower($aMatches[1])
2010-01-15 20:18:54 +02:00
: strtolower($sName);
if(!isset(self::$aTemplatePath[$sName])) {
2010-04-17 18:12:10 +03:00
$aPaths=glob(Config::Get('path.root.server').'/plugins/'.$sName.'/templates/skin/*',GLOB_ONLYDIR);
$sTemplateName=($aPaths and in_array(Config::Get('view.skin'),array_map('basename',$aPaths)))
? Config::Get('view.skin')
: 'default';
$sDir=Config::Get('path.root.server')."/plugins/{$sName}/templates/skin/{$sTemplateName}/";
self::$aTemplatePath[$sName] = is_dir($sDir) ? $sDir : null;
}
return self::$aTemplatePath[$sName];
}
/**
* Возвращает правильный web-адрес директории шаблонов
*
* @return string
*/
static public function GetTemplateWebPath($sName) {
$sName = preg_match('/^Plugin([\w]+)$/i',$sName,$aMatches)
? strtolower($aMatches[1])
: strtolower($sName);
if(!isset(self::$aTemplateWebPath[$sName])) {
2010-04-17 18:12:10 +03:00
$aPaths=glob(Config::Get('path.root.server').'/plugins/'.$sName.'/templates/skin/*',GLOB_ONLYDIR);
$sTemplateName=($aPaths and in_array(Config::Get('view.skin'),array_map('basename',$aPaths)))
? Config::Get('view.skin')
: 'default';
self::$aTemplateWebPath[$sName]=Config::Get('path.root.web')."/plugins/{$sName}/templates/skin/{$sTemplateName}/";
}
return self::$aTemplateWebPath[$sName];
}
/**
* Устанавливает значение пути до шаблонов плагина
*
* @param string $sName
* @param string $sTemplatePath
* @return bool
*/
static public function SetTemplatePath($sName,$sTemplatePath) {
if(!is_dir($sTemplatePath)) return false;
self::$aTemplatePath[$sName]=$sTemplatePath;
}
/**
* Устанавливает значение web-пути до шаблонов плагина
*
* @param string $sName
* @param string $sTemplatePath
* @return bool
*/
static public function SetTemplateWebPath($sName,$sTemplatePath) {
self::$aTemplateWebPath[$sName]=$sTemplatePath;
}
}
?>