* @copyright: Copyright (c) 2010, Bunzia Alexander
* @version: 1.0
* @license: http://www.gnu.org/copyleft/gpl.html GNU/GPL
* @package HiLo CMS
*/
//класс формирования странички
class cms{
public $info = array();
private $js_list = array();
private $css_list = array();
static $meta = array();
static $system_config = array();
// объект создаётся только изнутри
private function __construct($array){
if ( sizeof($array)==0 ){
sys_error('500',E_EMPTY_ARRAY);
}
$this -> info = $array;
/*// подгружаем кэш
include_once(MAIN_SOURCE_PATH.'/cache/cache.php');
if ( empty($cache) || !is_array($cache) ){
return ;
}
foreach( $cache as $r ){
list($table,$key,$array) = ($r);
$this -> set_cache($table,$key,$array);
}*/
}
// загружаем объект по ключу p_name
static function load_pname($p_name){
include_once(MAIN_SOURCE_PATH.'/modules/admin/inc/class.pages.php');
$p = new pagesQuery('f');
$p -> include_tmpl('t',false);
//$p -> set_debug(1);
if ( !$p -> where_pname($p_name) ){
return FALSE;
}
if ( true===MAIN_MULTILANG )
{
$p -> where_lang(MAIN_USER_LANG);
}
//$p -> set_debug(1);
$p -> get('*');
// файл не найден в базе
if ( $p -> get_count_rows()==0 ){
return FALSE;
}
return new cms($p -> row());
}
public function set_key($v){
//$this -> page_key = $v;
HL::$action = $v;
}
// генерируем модуль
public function module(){
global $MAIN_TMPL,$MAIN_PAGE;
if ( MAIN_DEBUG_MODE==1){
$time_begin = microtime() ;
}
include(MAIN_SOURCE_PATH.'/modules/'.$this -> info['m_dirname'].'/lang/'.MAIN_USER_LANG.'/lang.php');
include(MAIN_SOURCE_PATH.'/modules/'.$this -> info['m_dirname'].'/'.$this -> info['p_name'].'.php');
include_once(MAIN_PATH.'/tmp/meta/'.$this -> info['m_dirname'].'.php');
if ( !class_exists($this -> info['p_name']) ){
sys_error(ERROR_500,sprintf(E_EMPTY_MODULE_CLASS,$this -> info['p_name']));
}
// отрабатывает логика модуля
// try
// {
$m = new $this -> info['p_name'];
if ( !method_exists($m,'get') ){
sys_error(ERROR_500,sprintf(E_EMPTY_METODGET,$this -> info['p_name']));
}
$result = $m -> get();
if ( !empty($this -> info['t_page_content_var']) && false!==$result ){
$MAIN_TMPL -> set($this -> info['t_page_content_var'], $result);
}
//}catch( new Exception $e){
// обработка ошибок
//}
// мета данные
$this -> set_title(false, $this -> meta_title() );
$title = array();
foreach( $this -> title as $t=>$k){
$title[]=$k['title'];
}
$title = array_reverse($title);
$MAIN_TMPL -> meta_title = implode(' - ',$title);
$MAIN_TMPL -> meta_keywords = HL::get_meta('keywords') ;
$MAIN_TMPL -> meta_description = HL::get_meta('desc') ;
$MAIN_TMPL -> charset = $this -> charset();
$MAIN_TMPL -> content_type = $this -> content_type();
//$MAIN_TMPL -> setTemplate( MAIN_SOURCE_PATH.'/tmpl/'.$this -> tmpl() );
// костыль
if ( empty( $this -> info['t_url'] ))
{
$MAIN_TMPL -> setSource( $this -> content() );
}
else
{
$MAIN_TMPL -> setSource( file_get_contents(MAIN_SOURCE_PATH.'/tmpl/'.$this -> info['t_url']) );
}
$MAIN_TMPL -> main_js = $this -> js();
$MAIN_TMPL -> main_css = $this -> css();
// особое сообщение, обрабатывается внутри класса
// так как стандартная функция не способна
if ( MAIN_DEBUG_MODE==1){
sys_error(TIME_MSG,array('text'=>M_WORK_MODULE_END,'time'=>sys_timer($time_begin) ));
}
return TRUE;
}
/**
* Генерируем указанный блок
*
* @var: str $func Функция генерирующая блок
* @var: array $input Данные передаваемые в функцию
* @return: str сгенерированный html код блока
*/
public function load_block($func,$input){
global $MAIN_DB;
$sql = "
SELECT
b.b_id,b.b_name,b.m_path,b.b_func,b.b_tmpl,b.b_title, b.b_options,b.b_parser_name,b.b_content,b.b_type_content,b.b_access,
b.b_cache,b.b_time_cache,b.b_time_update_cache
FROM ".MAIN_BLK_TBL." b
WHERE b.b_parser_name = '".$func."' ";
$row = $MAIN_DB -> fetch_array( $MAIN_DB -> query($sql) );
if ( sizeof($input)>0 ){
$row['b_options'] = $input;
}
return $this -> make_block( $row );
}
/**
* Генерируем блок и возвращаем результат его работы
*
*/
private function make_block($row){
global $MAIN_USER,$MAIN_DB;
if ( MAIN_DEBUG_MODE ){
$b_time = microtime();
}
if ( empty($row['b_id']) ){
sys_error( ERROR_MESSAGE,sprintf(M_BLOCK_DATA_ERROR, var_export($row,true) ) );
return false;
}
// блок имеет ограничения по правам
if ( empty($row['b_access']) ){
if ( empty($MAIN_USER) ){
$MAIN_USER = users::auth();
}
if ( !$MAIN_USER -> check_right_block($row['b_id']) ){
//sys_error( ERROR_MESSAGE,sprintf(M_CMS_BLOCK_ACCESS_ERROR,$row['b_name']) );
return '';
}
}
if ( MAIN_CACHE_MODE && $row['b_time_update_cache']>MAIN_TIME && !empty($row['b_time_cache']) ){
$result = $row['b_cache'];
}else{
// язык
if ( !empty($row['m_path']) ){
$b_lang_url = MAIN_SOURCE_PATH.'/modules/'.$row['m_path'].'/lang/'.MAIN_USER_LANG.'/blocks.php';
include_once($b_lang_url);
}
// функция
if ( !empty($row['m_path']) && !empty($row['b_func']) ){
$b_func_url = MAIN_SOURCE_PATH.'/modules/'.$row['m_path'].'/blocks/'.$row['b_func'].'.php';
include_once($b_func_url);
}
// не определена функция
if ( !empty($row['b_func']) && !function_exists($row['b_func']) ){
sys_error(ERROR_MESSAGE,sprintf(M_CMS_FUNCTION_NO,$row['b_func']) );
return false;
// исполняем функцию
}elseif( !empty($row['b_func']) ){
$result = $row['b_func']( $row['options'] );
}else{
if ( MAIN_DEVELOPE_MODE==1 ){
//$row['b_content'] = $row['b_content']
//$row['b_content'] = '';
}
if ( $row['b_type_content']=='H' ){
$result = $row['b_content'];
}elseif ( $row['b_type_content']=='P' ){
ob_start();
eval( $row['b_content'] );
$result = ob_get_clean();
}else{
$t = new PHPTAL( false);
$t -> setSource($row['b_content']);
$result = $t -> execute();
unset($t);
}
}
if ( MAIN_CACHE_MODE && $row['b_time_cache']>0 ){
$sql = "
UPDATE ".MAIN_BLK_TBL."
SET
b_cache='".$result."',
b_time_update_cache='".($row['b_time_cache']+MAIN_TIME)."'
WHERE b_id='".$row['b_id']."'";
$MAIN_DB -> query($sql);
}
}
if ( false===$result ){
sys_error(ERROR_MESSAGE,sprintf(M_CMS_BLOCKS_ERROR,$row['b_name']) );
return false;
}
// обёртываем результат в шаблон для блока
if ( !empty($row['b_tmpl']) ){
$tmpl_block = new PHPTAL( TMPL_PATH.'/'.$row['b_tmpl']);
$tmpl_block -> content = $result;
$tmpl_block -> title = $row['b_title'];
$result = $tmpl_block -> execute();
}
if ( MAIN_DEBUG_MODE ){
sys_error(TIME_MSG,array('text'=>sprintf(M_CMS_END_BLOCK,$row['b_name']),'time'=> sys_timer($b_time)) );
}
return $result;
}
/**
* Генерируем блоки и помещаем их в шаблон
*
*/
public function blocks(){
global $MAIN_TMPL,$MAIN_DB;
$time_begin = microtime() ;
if ( defined('SYS_BLOCKS_GENERATE_OK') ){
return false;
}
sys_block_disable();
$sql = "
SELECT
b.b_id,b.b_name,b.m_path,b.b_func,b.b_tmpl,b.b_title, b.b_options,b.b_parser_name,b.b_content,b.b_type_content,b.b_access,gfl.var,gfl.options,
b.b_cache,b.b_time_cache,b.b_time_update_cache
FROM
".MAIN_PLB_TBL." gfl
LEFT JOIN
".MAIN_BLK_TBL." b
ON
b.b_id = gfl.b_id
WHERE
gfl.t_id = '".$this -> tmpl_id()."' ORDER BY gfl.sort ASC";
$res = $MAIN_DB -> query($sql);
$ITEMS = array();
while( $row = $MAIN_DB -> fetch_array($res) ){
$result = $this -> make_block($row);
@$ITEMS[$row['var']].=$result;
//$MAIN_TMPL -> $row['b_parser_name'] = $result;
define($row['b_parser_name'],$result);
}// end while
foreach( $ITEMS AS $k=>$v ){
define($k,$v);
//$MAIN_TMPL -> $k = $v;
}
sys_error(TIME_MSG,array('text'=>M_CMS_BLOCKS_GENERATED,'time'=>sys_timer($time_begin) ) );
}
// меняем шаблон вывода для текущего
function set_page_content_var($v){
$this -> info['t_page_content_var'] = $v;
return true;
}
// меняем шаблон вывода для текущего
function set_tmpl_id($v){
$v = intval($v);
if ( empty($v) ){
sys_error(ERROR_500);
}
$this -> info['t_id'] = $v;
return true;
}
function set_content($v){
$this -> info['t_content'] = $v;
return true;
}
/*function set_meta($v){
if ( !is_array($v) ){
return false;
}
meta::$array = array_merge($this -> meta,$v);
return true;
}*/
/* function get_meta($mod,$act=''){
$act = !empty($act) ? $act : $this -> page_key;
if ( !empty( $this -> meta[$mod][$act]) )
{
return $this -> meta[$mod][$act];
}
return FALSE;
}*/
function set_meta_title($v){
$this -> info['f_meta_title'] = $v;
return true;
}
function set_meta_keywords($v){
$this -> info['f_meta_keywords'] = $v;
return true;
}
function set_meta_description($v){
$this -> info['f_meta_description'] = $v;
return true;
}
function set_tmpl($v){
if ( empty($v) ){
sys_error(ERROR_500);
}
$this -> info['t_path'] = $v;
return true;
}
public function active(){
if ( empty($this -> info['f_active']) ){
return FALSE;
}
return TRUE;
}
// свободный доступ к странице
// 0 - нет 1 - да
public function free_access(){
if ( $this -> info['f_access'] ){
return TRUE;
}
return FALSE;
}
public function tmpl(){
return $this -> info['t_path'];
}
public function content(){
return $this -> info['t_content'];
}
public function page_name(){
return $this -> info['p_name'];
}
public function tmpl_id(){
return $this -> info['t_id'];
}
public function encoding(){
if ( $this -> info['f_encoding'] == 'windows-1251'){
return 'cp1251';
}elseif ( $this -> info['f_encoding'] == 'utf-8'){
return 'utf8';
}else{
return $this -> info['f_encoding'];
}
}
public function charset(){
return $this -> info['f_encoding'];
}
public function meta_title($key){
//$key = !empty($key) ? $key : $this -> page_key ;
return HL::get_meta('title',$key); //$this -> meta['title'][ $key ];
}
/*public function meta_keywords(){
return $this -> info['f_meta_keywords'];
}
public function meta_description(){
return $this -> info['f_meta_description'];
}*/
public function content_type(){
return "text/html; charset=".$this -> charset();
}
public function dirname(){
return $this -> info['m_dirname'];
}
public function id(){
return $this -> info['f_id'];
}
// возвращаем список js файлов
public function js(){
$result = '';
foreach( $this -> js_list AS $url ){
$result.="";
}
return $result;
}
public function css(){
$result = '';
foreach( $this -> css_list AS $url ){
$result.=' ';
}
return $result;
}
// CONFIG
//устанавливаем переменную
static function set_config($var,$value){
self::$system_config[$var]=$value;
}
static function config($var){
if ( !isset(self::$system_config[$var]) ){
sys_error(ERROR_500,sprintf(E_CMS_EMPTY_CONFIG,$var) );
}
return stripslashes(self::$system_config[$var]);
}
public function set_js($v){
$this -> js_list[] = $v;
}
public function set_css($v){
$this -> css_list[] = $v;
}
// кэш таблиц
public function set_cache($table,$key,$array){
$this -> system_cache[$table][$key]=$array;
}
public function cache($table,$key=false){
if ( !isset($this -> system_cache[$table]) ){
sys_error(ERROR_500,sprintf(E_CMS_EMPTY_CACHE,$table) );
}
// возвращаем всю таблицу
if ( false===$key ){
return $this -> system_cache[$table];
}
if ( !isset($this -> system_cache[$table][$key]) ){
sys_error(ERROR_500,sprintf(E_CMS_EMPTY_CACHE,$key) );
}
// возвращаем по указанному ключу
return $this -> system_cache[$table][$key];
}
// автоматическая генерация заголовков и хлебных крошек
public function set_title($level,$title,$url=''){
if ( empty($title) )
{
return false;
}
if ( empty($level) )
{
$this -> title[] = array('title'=>$title,'url'=>$url);
}
else
{
$this -> title[$level] = array('title'=>$title,'url'=>$url);
}
}
}
class HL
{
static $action;
static $table_cache = array();
static $meta_cache = array();
static $url_cache = array();
static $css_list = array();
static $js_list = array();
static function ajaxWindow( $title,$msg )
{
global $MAIN_PAGE ;
header('Content-Type: text/xml; charset='.$MAIN_PAGE -> charset() );
echo '';
exit();
}
static function ajaxMsg( $msg )
{
global $MAIN_PAGE ;
header('Content-Type: text/xml; charset='.$MAIN_PAGE -> charset() );
echo '';
exit();
}
static function ajaxError( $msg )
{
global $MAIN_PAGE ;
header('Content-Type: text/xml; charset='.$MAIN_PAGE -> charset() );
echo '';
exit();
}
static function ajaxDebug( $msg )
{
global $MAIN_PAGE;
if ( is_array($msg) ){
$msg = var_export($msg,true);
}
header('Content-Type: text/xml; charset='.$MAIN_PAGE -> charset() );
echo '';
exit();
}
static function ajaxContent( $msg )
{
global $MAIN_PAGE ;
header('Content-Type: text/xml; charset='.$MAIN_PAGE -> charset() );
echo '';
exit();
}
static function set_meta( $type,$value,$key='',$page='',$lang='')
{
$key = !empty($key) ? $key : HL::$action;
$page = !empty($page) ? $page : MAIN_PAGE;
$lang = !empty($lang) ? $lang : MAIN_USER_LANG;
HL::$meta_cache[$page][$key][$type][$lang] = $value;
}
static function get_meta($type,$key='')
{
$key = !empty($key) ? $key : HL::$action;
if ( !empty(HL::$meta_cache[MAIN_PAGE][$key][$type][MAIN_USER_LANG]) )
{
return HL::$meta_cache[MAIN_PAGE][$key][$type][MAIN_USER_LANG];
}
else
{
return false;
}
}
static function lang()
{
if ( empty($_GET['lng_set']) ){
return MAIN_LANG;
}elseif( sys_check_lang($_GET['lng_set']) ){
return $_GET['lng_set'];
}else{
return MAIN_LANG;
}
}
static function cache($e)
{
/*if ( file_exists(MAIN_PATH.'/tmp/cache/tables.php') )
{
include_once(MAIN_PATH.'/tmp/cache/tables.php');
}*/
return false;
}
static function triggerEvents($e,$options = array() )
{
global $MAIN_DB;
$rows = HL::cache(MAIN_EVENTS_TBL);
if ( false!==$rows )
{
foreach( $rows as $row )
{
if ( $row['e_type']!=$e )
{
continue;
}
include_once(MAIN_SOURCE_PATH.'/modules/'.$row['m_path'].'/events/'.$row['e_name'].'.php');
if ( !function_exists($row['e_name']) )
{
continue;
}
$row['e_name']($options);
}
}
else
{
$sql = "SELECT m_path,e_name FROM ".MAIN_EVENTS_TBL." WHERE e_type='".$e."'";
$res = $MAIN_DB -> query($sql);
while($row = $MAIN_DB -> fetch_array($res) )
{
include_once(MAIN_SOURCE_PATH.'/modules/'.$row['m_path'].'/events/'.$row['e_name'].'.php');
if ( !function_exists($row['e_name']) ){
continue;
}
$row['e_name']($options);
}
}
}
static function set_config($var,$value){
cms::$system_config[$var]=$value;
}
static function config($var){
if ( !isset(cms::$system_config[$var]) ){
sys_error(ERROR_500,sprintf(E_CMS_EMPTY_CONFIG,$var) );
}
return stripslashes(cms::$system_config[$var]);
}
/*static function registerCss($file)
{
self::$css_list[$file]=$file;
}
static function registerJs($file)
{
self::$js_list[$file]=$file;
}
static function loadCss()
{
return implode("\n",self::$css_list );
}
static function loadJs()
{
return implode("\n",self::$js_list );
}*/
/*static loadJS($file){
}*/
/*static function set_config($var,$value){
$this -> system_config[$var]=$value;
}
static function config($var){
if ( !isset($this -> system_config[$var]) ){
sys_error(ERROR_500,sprintf(E_CMS_EMPTY_CONFIG,$var) );
}
return stripslashes($this -> system_config[$var]);
}*/
/* static function set_url( $key,$array = array() )
{
if ( empty($key) )
{
throw new Exception('Ошибка: пустой ключ!');
}
elseif( !empty(HL::$url_cache[$key]) )
{
throw new Exception('Ошибка: ключ "'.$key.'" уже существует!');
}
HL::$url_cache[$key] = $array;
return true;
}
static function get_url( $key,$arg=array() )
{
global $MAIN_USER
if ( empty($key) )
{
throw new Exception('Ошибка: пустой ключ!');
}
elseif( !isset(HL::$url_cache[$key]) )
{
throw new Exception('Ошибка: ключ "'.$key.'" не существует!');
}
$data = HL::$url_cache[$key];
$url = MAIN_URL;
// catalog -> http://site.ru/catalog/
if ( !empty($data['path']) )
{
$url.= $data['path'].'/';
}
// index.html -> http://site.ru/catalog/index.html
if ( !empty($data['file']) )
{
$url.= $data['file'];
}
// array(act, p_name) -> http://site.ru/catalog/index.html?act=
if ( !empty($arg) && !empty($data['arg']) )
{
$url.= $data['file'];
}
$par = array();
$lang = $MAIN_USER -> lang();
if ( MAIN_MULTILANG===true && MAIN_LANG!=$lang ){
$par['lng']=$lang;
}
if ( empty($key) )
{
throw new Exception('Ошибка: пустой ключ!');
}
elseif( !empty(HL::$url_cache[$key]) )
{
throw new Exception('Ошибка: ключ "'.$key.'" уже существует!');
}
HL::$url_cache[$key] = $array;
return true;
}*/
}
?>